PageRenderTime 24ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/upload/ajax.php

http://torrentpier2.googlecode.com/
PHP | 408 lines | 290 code | 70 blank | 48 comment | 26 complexity | c743323cb3c49e83dcdc586a6fb1f492 MD5 | raw file
  1. <?php
  2. define('BB_SCRIPT', 'ajax');
  3. define('IN_AJAX', true);
  4. $ajax = new ajax_common();
  5. require('./common.php');
  6. $ajax->init();
  7. // Handle "board disabled via ON/OFF trigger"
  8. if (file_exists(BB_DISABLED) || $bb_cfg['board_disable'])
  9. {
  10. $ajax->ajax_die($bb_cfg['board_disabled_msg']);
  11. }
  12. // Load actions required modules
  13. switch ($ajax->action)
  14. {
  15. case 'view_post':
  16. require(INC_DIR . 'bbcode.php');
  17. break;
  18. case 'posts':
  19. case 'post_mod_comment':
  20. require(INC_DIR . 'bbcode.php');
  21. require(INC_DIR . 'functions_post.php');
  22. require(INC_DIR . 'functions_admin.php');
  23. break;
  24. case 'view_torrent':
  25. case 'mod_action':
  26. case 'change_tor_status':
  27. case 'gen_passkey':
  28. require(BB_ROOT . 'attach_mod/attachment_mod.php');
  29. require(INC_DIR . 'functions_torrent.php');
  30. break;
  31. case 'change_torrent':
  32. require(BB_ROOT . 'attach_mod/attachment_mod.php');
  33. require(INC_DIR . 'functions_torrent.php');
  34. break;
  35. case 'user_register':
  36. require(INC_DIR . 'functions_validate.php');
  37. break;
  38. case 'manage_user':
  39. case 'manage_admin':
  40. require(INC_DIR . 'functions_admin.php');
  41. break;
  42. case 'group_membership':
  43. require(INC_DIR . 'functions_group.php');
  44. break;
  45. }
  46. // position in $ajax->valid_actions['xxx']
  47. define('AJAX_AUTH', 0); // 'guest', 'user', 'mod', 'admin', 'super_admin'
  48. $user->session_start();
  49. $ajax->exec();
  50. //
  51. // Ajax
  52. //
  53. class ajax_common
  54. {
  55. var $request = array();
  56. var $response = array();
  57. var $valid_actions = array(
  58. // ACTION NAME AJAX_AUTH
  59. 'edit_user_profile' => array('admin'),
  60. 'change_user_rank' => array('admin'),
  61. 'change_user_opt' => array('admin'),
  62. 'manage_user' => array('admin'),
  63. 'manage_admin' => array('admin'),
  64. 'mod_action' => array('mod'),
  65. 'topic_tpl' => array('mod'),
  66. 'group_membership' => array('mod'),
  67. 'post_mod_comment' => array('mod'),
  68. 'avatar' => array('user'),
  69. 'gen_passkey' => array('user'),
  70. 'change_torrent' => array('user'),
  71. 'change_tor_status' => array('user'),
  72. 'view_profile' => array('user'),
  73. 'view_post' => array('guest'),
  74. 'view_torrent' => array('guest'),
  75. 'user_register' => array('guest'),
  76. 'posts' => array('guest'),
  77. 'index_data' => array('guest'),
  78. );
  79. var $action = null;
  80. /**
  81. * Constructor
  82. */
  83. function ajax_common()
  84. {
  85. ob_start(array(&$this, 'ob_handler'));
  86. header('Content-Type: text/plain');
  87. }
  88. /**
  89. * Perform action
  90. */
  91. function exec()
  92. {
  93. global $lang;
  94. // Exit if we already have errors
  95. if (!empty($this->response['error_code']))
  96. {
  97. $this->send();
  98. }
  99. // Check that requested action is valid
  100. $action = $this->action;
  101. if (!$action || !is_string($action))
  102. {
  103. $this->ajax_die('no action specified');
  104. }
  105. elseif (!$action_params =& $this->valid_actions[$action])
  106. {
  107. $this->ajax_die('invalid action: ' . $action);
  108. }
  109. // Auth check
  110. switch ($action_params[AJAX_AUTH])
  111. {
  112. // GUEST
  113. case 'guest':
  114. break;
  115. // USER
  116. case 'user':
  117. if (IS_GUEST)
  118. {
  119. $this->ajax_die($lang['NEED_TO_LOGIN_FIRST']);
  120. }
  121. break;
  122. // MOD
  123. case 'mod':
  124. if (!IS_AM)
  125. {
  126. $this->ajax_die($lang['ONLY_FOR_MOD']);
  127. }
  128. $this->check_admin_session();
  129. break;
  130. // ADMIN
  131. case 'admin':
  132. if (!IS_ADMIN)
  133. {
  134. $this->ajax_die($lang['ONLY_FOR_ADMIN']);
  135. }
  136. $this->check_admin_session();
  137. break;
  138. // SUPER_ADMIN
  139. case 'super_admin':
  140. if (!IS_SUPER_ADMIN)
  141. {
  142. $this->ajax_die($lang['ONLY_FOR_SUPER_ADMIN']);
  143. }
  144. $this->check_admin_session();
  145. break;
  146. default:
  147. trigger_error("invalid auth type for $action", E_USER_ERROR);
  148. }
  149. // Run action
  150. $this->$action();
  151. // Send output
  152. $this->send();
  153. }
  154. /**
  155. * Exit on error
  156. */
  157. function ajax_die($error_msg, $error_code = E_AJAX_GENERAL_ERROR)
  158. {
  159. $this->response['error_code'] = $error_code;
  160. $this->response['error_msg'] = $error_msg;
  161. $this->send();
  162. }
  163. /**
  164. * Initialization
  165. */
  166. function init()
  167. {
  168. $this->request = $_POST;
  169. $this->action =& $this->request['action'];
  170. }
  171. /**
  172. * Send data
  173. */
  174. function send()
  175. {
  176. $this->response['action'] = $this->action;
  177. if (DBG_USER && SQL_DEBUG && !empty($_COOKIE['sql_log']))
  178. {
  179. $this->response['sql_log'] = get_sql_log();
  180. }
  181. // sending output will be handled by $this->ob_handler()
  182. exit();
  183. }
  184. /**
  185. * OB Handler
  186. */
  187. function ob_handler($contents)
  188. {
  189. if (DBG_USER)
  190. {
  191. if ($contents)
  192. {
  193. $this->response['raw_output'] = $contents;
  194. }
  195. }
  196. $response_js = bb_json_encode($this->response);
  197. if (GZIP_OUTPUT_ALLOWED && !defined('NO_GZIP'))
  198. {
  199. if (UA_GZIP_SUPPORTED && strlen($response_js) > 2000)
  200. {
  201. header('Content-Encoding: gzip');
  202. $response_js = gzencode($response_js, 1);
  203. }
  204. }
  205. return $response_js;
  206. }
  207. /**
  208. * Admin session
  209. */
  210. function check_admin_session()
  211. {
  212. global $user;
  213. if (!$user->data['session_admin'])
  214. {
  215. if (empty($this->request['user_password']))
  216. {
  217. $this->prompt_for_password();
  218. }
  219. else
  220. {
  221. $login_args = array(
  222. 'login_username' => $user->data['username'],
  223. 'login_password' => $_POST['user_password'],
  224. );
  225. if (!$user->login($login_args, true))
  226. {
  227. $this->ajax_die('Wrong password');
  228. }
  229. }
  230. }
  231. }
  232. /**
  233. * Prompt for password
  234. */
  235. function prompt_for_password()
  236. {
  237. $this->response['prompt_password'] = 1;
  238. $this->send();
  239. }
  240. /**
  241. * Prompt for confirmation
  242. */
  243. function prompt_for_confirm($confirm_msg)
  244. {
  245. if (empty($confirm_msg)) $this->ajax_die('false');
  246. $this->response['prompt_confirm'] = 1;
  247. $this->response['confirm_msg'] = $confirm_msg;
  248. $this->send();
  249. }
  250. /**
  251. * Verify mod rights
  252. */
  253. function verify_mod_rights($forum_id)
  254. {
  255. global $userdata, $lang;
  256. $is_auth = auth(AUTH_MOD, $forum_id, $userdata);
  257. if (!$is_auth['auth_mod'])
  258. {
  259. $this->ajax_die($lang['ONLY_FOR_MOD']);
  260. }
  261. }
  262. function edit_user_profile()
  263. {
  264. require(AJAX_DIR . 'edit_user_profile.php');
  265. }
  266. function change_user_rank()
  267. {
  268. require(AJAX_DIR . 'change_user_rank.php');
  269. }
  270. function change_user_opt()
  271. {
  272. require(AJAX_DIR . 'change_user_opt.php');
  273. }
  274. function gen_passkey()
  275. {
  276. require(AJAX_DIR . 'gen_passkey.php');
  277. }
  278. function group_membership()
  279. {
  280. require(AJAX_DIR . 'group_membership.php');
  281. }
  282. function post_mod_comment()
  283. {
  284. require(AJAX_DIR . 'post_mod_comment.php');
  285. }
  286. function view_post()
  287. {
  288. require(AJAX_DIR . 'view_post.php');
  289. }
  290. function change_tor_status()
  291. {
  292. require(AJAX_DIR . 'change_tor_status.php');
  293. }
  294. function change_torrent()
  295. {
  296. require(AJAX_DIR . 'change_torrent.php');
  297. }
  298. function view_torrent()
  299. {
  300. require(AJAX_DIR . 'view_torrent.php');
  301. }
  302. function user_register()
  303. {
  304. require(AJAX_DIR . 'user_register.php');
  305. }
  306. function mod_action()
  307. {
  308. require(AJAX_DIR . 'mod_action.php');
  309. }
  310. function posts()
  311. {
  312. require(AJAX_DIR . 'posts.php');
  313. }
  314. function manage_user()
  315. {
  316. require(AJAX_DIR . 'manage_user.php');
  317. }
  318. function manage_admin()
  319. {
  320. require(AJAX_DIR . 'manage_admin.php');
  321. }
  322. function topic_tpl()
  323. {
  324. require(AJAX_DIR . 'topic_tpl.php');
  325. }
  326. function index_data()
  327. {
  328. require(AJAX_DIR . 'index_data.php');
  329. }
  330. function view_profile()
  331. {
  332. require(AJAX_DIR . 'view_profile.php');
  333. }
  334. function avatar()
  335. {
  336. require(AJAX_DIR . 'avatar.php');
  337. }
  338. }