PageRenderTime 59ms CodeModel.GetById 6ms RepoModel.GetById 1ms app.codeStats 0ms

/mcp.php

https://code.google.com/p/phpbbex/
PHP | 927 lines | 695 code | 159 blank | 73 comment | 148 complexity | 6d73fd330f73735a5811aae881ab2bb1 MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /**
  3. *
  4. * @package mcp
  5. * @version $Id$
  6. * @copyright (c) 2005 phpBB Group
  7. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  8. *
  9. */
  10. /**
  11. * @ignore
  12. */
  13. define('IN_PHPBB', true);
  14. $phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
  15. $phpEx = substr(strrchr(__FILE__, '.'), 1);
  16. include($phpbb_root_path . 'common.' . $phpEx);
  17. include($phpbb_root_path . 'includes/functions_admin.' . $phpEx);
  18. require($phpbb_root_path . 'includes/functions_module.' . $phpEx);
  19. // Start session management
  20. $user->session_begin();
  21. $auth->acl($user->data);
  22. $user->setup('mcp');
  23. $module = new p_master();
  24. // Setting a variable to let the style designer know where he is...
  25. $template->assign_var('S_IN_MCP', true);
  26. // Basic parameter data
  27. $id = request_var('i', '');
  28. if (isset($_REQUEST['mode']) && is_array($_REQUEST['mode']))
  29. {
  30. $mode = request_var('mode', array(''));
  31. list($mode, ) = each($mode);
  32. }
  33. else
  34. {
  35. $mode = request_var('mode', '');
  36. }
  37. // Only Moderators can go beyond this point
  38. if (!$user->data['is_registered'])
  39. {
  40. if ($user->data['is_bot'])
  41. {
  42. redirect(append_sid("{$phpbb_root_path}index.$phpEx"));
  43. }
  44. login_box('', $user->lang['LOGIN_EXPLAIN_MCP']);
  45. }
  46. $quickmod = (isset($_REQUEST['quickmod'])) ? true : false;
  47. $action = request_var('action', '');
  48. $action_ary = request_var('action', array('' => 0));
  49. $forum_action = request_var('forum_action', '');
  50. if ($forum_action !== '' && !empty($_POST['sort']))
  51. {
  52. $action = $forum_action;
  53. }
  54. if (sizeof($action_ary))
  55. {
  56. list($action, ) = each($action_ary);
  57. }
  58. unset($action_ary);
  59. if ($mode == 'topic_logs')
  60. {
  61. $id = 'logs';
  62. $quickmod = false;
  63. }
  64. $post_id = request_var('p', 0);
  65. $topic_id = request_var('t', 0);
  66. $forum_id = request_var('f', 0);
  67. $report_id = request_var('r', 0);
  68. $user_id = request_var('u', 0);
  69. $warning_id = request_var('warning_id', 0);
  70. $username = utf8_normalize_nfc(request_var('username', '', true));
  71. if ($post_id)
  72. {
  73. // We determine the topic and forum id here, to make sure the moderator really has moderative rights on this post
  74. $sql = 'SELECT topic_id, forum_id
  75. FROM ' . POSTS_TABLE . "
  76. WHERE post_id = $post_id";
  77. $result = $db->sql_query($sql);
  78. $row = $db->sql_fetchrow($result);
  79. $db->sql_freeresult($result);
  80. $topic_id = (int) $row['topic_id'];
  81. $forum_id = (int) ($row['forum_id']) ? $row['forum_id'] : $forum_id;
  82. }
  83. else if ($topic_id)
  84. {
  85. $sql = 'SELECT forum_id
  86. FROM ' . TOPICS_TABLE . "
  87. WHERE topic_id = $topic_id";
  88. $result = $db->sql_query($sql);
  89. $row = $db->sql_fetchrow($result);
  90. $db->sql_freeresult($result);
  91. $forum_id = (int) $row['forum_id'];
  92. }
  93. // If the user doesn't have any moderator powers (globally or locally) he can't access the mcp
  94. if (!$auth->acl_getf_global('m_'))
  95. {
  96. // Except he is using one of the quickmod tools for users
  97. $user_quickmod_actions = array(
  98. 'lock' => 'f_user_lock',
  99. 'make_sticky' => 'f_sticky',
  100. 'make_announce' => 'f_announce',
  101. 'make_global' => 'f_announce',
  102. 'make_normal' => array('f_announce', 'f_sticky')
  103. );
  104. $allow_user = false;
  105. if ($quickmod && isset($user_quickmod_actions[$action]) && $user->data['is_registered'] && $auth->acl_gets($user_quickmod_actions[$action], $forum_id))
  106. {
  107. $topic_info = get_topic_data(array($topic_id));
  108. if ($topic_info[$topic_id]['topic_poster'] == $user->data['user_id'])
  109. {
  110. $allow_user = true;
  111. }
  112. }
  113. if (!$allow_user)
  114. {
  115. trigger_error('NOT_AUTHORISED');
  116. }
  117. }
  118. // if the user cannot read the forum he tries to access then we won't allow mcp access either
  119. if ($forum_id && !$auth->acl_get('f_read', $forum_id))
  120. {
  121. trigger_error('NOT_AUTHORISED');
  122. }
  123. if ($forum_id)
  124. {
  125. $module->acl_forum_id = $forum_id;
  126. }
  127. // Instantiate module system and generate list of available modules
  128. $module->list_modules('mcp');
  129. if ($quickmod)
  130. {
  131. $mode = 'quickmod';
  132. switch ($action)
  133. {
  134. case 'lock':
  135. case 'unlock':
  136. case 'lock_post':
  137. case 'unlock_post':
  138. case 'make_sticky':
  139. case 'make_announce':
  140. case 'make_global':
  141. case 'make_normal':
  142. case 'fork':
  143. case 'move':
  144. case 'delete_post':
  145. case 'delete_topic':
  146. $module->load('mcp', 'main', 'quickmod');
  147. return;
  148. break;
  149. case 'topic_logs':
  150. // Reset start parameter if we jumped from the quickmod dropdown
  151. if (request_var('start', 0))
  152. {
  153. $_REQUEST['start'] = 0;
  154. }
  155. $module->set_active('logs', 'topic_logs');
  156. break;
  157. case 'merge_topic':
  158. $module->set_active('main', 'forum_view');
  159. break;
  160. case 'split':
  161. case 'merge':
  162. $module->set_active('main', 'topic_view');
  163. break;
  164. default:
  165. trigger_error("$action not allowed as quickmod", E_USER_ERROR);
  166. break;
  167. }
  168. }
  169. else
  170. {
  171. // Select the active module
  172. $module->set_active($id, $mode);
  173. }
  174. // Hide some of the options if we don't have the relevant information to use them
  175. if (!$post_id)
  176. {
  177. $module->set_display('main', 'post_details', false);
  178. $module->set_display('warn', 'warn_post', false);
  179. }
  180. if ($mode == '' || $mode == 'unapproved_topics' || $mode == 'unapproved_posts')
  181. {
  182. $module->set_display('queue', 'approve_details', false);
  183. }
  184. if ($mode == '' || $mode == 'reports' || $mode == 'reports_closed' || $mode == 'pm_reports' || $mode == 'pm_reports_closed' || $mode == 'pm_report_details')
  185. {
  186. $module->set_display('reports', 'report_details', false);
  187. }
  188. if ($mode == '' || $mode == 'reports' || $mode == 'reports_closed' || $mode == 'pm_reports' || $mode == 'pm_reports_closed' || $mode == 'report_details')
  189. {
  190. $module->set_display('pm_reports', 'pm_report_details', false);
  191. }
  192. if (!$topic_id)
  193. {
  194. $module->set_display('main', 'topic_view', false);
  195. $module->set_display('logs', 'topic_logs', false);
  196. }
  197. if (!$forum_id)
  198. {
  199. $module->set_display('main', 'forum_view', false);
  200. $module->set_display('logs', 'forum_logs', false);
  201. }
  202. if (!$user_id && $username == '')
  203. {
  204. $module->set_display('notes', 'user_notes', false);
  205. $module->set_display('warn', 'warn_user', false);
  206. }
  207. if (!$warning_id)
  208. {
  209. $module->set_display('warn', 'warn_edit', false);
  210. }
  211. // Load and execute the relevant module
  212. $module->load_active();
  213. // Assign data to the template engine for the list of modules
  214. $module->assign_tpl_vars(append_sid("{$phpbb_root_path}mcp.$phpEx"));
  215. // Generate urls for letting the moderation control panel being accessed in different modes
  216. $template->assign_vars(array(
  217. 'U_MCP' => append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=main'),
  218. 'U_MCP_FORUM' => ($forum_id) ? append_sid("{$phpbb_root_path}mcp.$phpEx", "i=main&amp;mode=forum_view&amp;f=$forum_id") : '',
  219. 'U_MCP_TOPIC' => ($forum_id && $topic_id) ? append_sid("{$phpbb_root_path}mcp.$phpEx", "i=main&amp;mode=topic_view&amp;t=$topic_id") : '',
  220. 'U_MCP_POST' => ($forum_id && $topic_id && $post_id) ? append_sid("{$phpbb_root_path}mcp.$phpEx", "i=main&amp;mode=post_details&amp;t=$topic_id&amp;p=$post_id") : '',
  221. ));
  222. // Generate the page, do not display/query online list
  223. $module->display($module->get_page_title(), false);
  224. /**
  225. * Functions used to generate additional URL paramters
  226. */
  227. function _module__url($mode, &$module_row)
  228. {
  229. return extra_url();
  230. }
  231. function _module_notes_url($mode, &$module_row)
  232. {
  233. if ($mode == 'front')
  234. {
  235. return '';
  236. }
  237. global $user_id;
  238. return ($user_id) ? "&amp;u=$user_id" : '';
  239. }
  240. function _module_warn_url($mode, &$module_row)
  241. {
  242. if ($mode == 'front' || $mode == 'list')
  243. {
  244. global $forum_id;
  245. return ($forum_id) ? "&amp;f=$forum_id" : '';
  246. }
  247. if ($mode == 'warn_post')
  248. {
  249. global $forum_id, $post_id;
  250. $url_extra = ($forum_id) ? "&amp;f=$forum_id" : '';
  251. $url_extra .= ($post_id) ? "&amp;p=$post_id" : '';
  252. return $url_extra;
  253. }
  254. elseif ($mode == 'warn_edit')
  255. {
  256. global $warning_id;
  257. return ($warning_id) ? "&amp;warning_id=$warning_id" : '';
  258. }
  259. else
  260. {
  261. global $user_id;
  262. return ($user_id) ? "&amp;u=$user_id" : '';
  263. }
  264. }
  265. function _module_main_url($mode, &$module_row)
  266. {
  267. return extra_url();
  268. }
  269. function _module_logs_url($mode, &$module_row)
  270. {
  271. return extra_url();
  272. }
  273. function _module_ban_url($mode, &$module_row)
  274. {
  275. return extra_url();
  276. }
  277. function _module_queue_url($mode, &$module_row)
  278. {
  279. return extra_url();
  280. }
  281. function _module_reports_url($mode, &$module_row)
  282. {
  283. return extra_url();
  284. }
  285. function extra_url()
  286. {
  287. global $forum_id, $topic_id, $post_id, $report_id, $user_id;
  288. $url_extra = '';
  289. $url_extra .= ($forum_id) ? "&amp;f=$forum_id" : '';
  290. $url_extra .= ($topic_id) ? "&amp;t=$topic_id" : '';
  291. $url_extra .= ($post_id) ? "&amp;p=$post_id" : '';
  292. $url_extra .= ($user_id) ? "&amp;u=$user_id" : '';
  293. $url_extra .= ($report_id) ? "&amp;r=$report_id" : '';
  294. return $url_extra;
  295. }
  296. /**
  297. * Get simple topic data
  298. */
  299. function get_topic_data($topic_ids, $acl_list = false, $read_tracking = false)
  300. {
  301. global $auth, $db, $config, $user;
  302. static $rowset = array();
  303. $topics = array();
  304. if (!sizeof($topic_ids))
  305. {
  306. return array();
  307. }
  308. // cache might not contain read tracking info, so we can't use it if read
  309. // tracking information is requested
  310. if (!$read_tracking)
  311. {
  312. $cache_topic_ids = array_intersect($topic_ids, array_keys($rowset));
  313. $topic_ids = array_diff($topic_ids, array_keys($rowset));
  314. }
  315. else
  316. {
  317. $cache_topic_ids = array();
  318. }
  319. if (sizeof($topic_ids))
  320. {
  321. $sql_array = array(
  322. 'SELECT' => 't.*, f.*',
  323. 'FROM' => array(
  324. TOPICS_TABLE => 't',
  325. ),
  326. 'LEFT_JOIN' => array(
  327. array(
  328. 'FROM' => array(FORUMS_TABLE => 'f'),
  329. 'ON' => 'f.forum_id = t.forum_id'
  330. )
  331. ),
  332. 'WHERE' => $db->sql_in_set('t.topic_id', $topic_ids)
  333. );
  334. if ($read_tracking && $config['load_db_lastread'])
  335. {
  336. $sql_array['SELECT'] .= ', tt.mark_time, ft.mark_time as forum_mark_time';
  337. $sql_array['LEFT_JOIN'][] = array(
  338. 'FROM' => array(TOPICS_TRACK_TABLE => 'tt'),
  339. 'ON' => 'tt.user_id = ' . $user->data['user_id'] . ' AND t.topic_id = tt.topic_id'
  340. );
  341. $sql_array['LEFT_JOIN'][] = array(
  342. 'FROM' => array(FORUMS_TRACK_TABLE => 'ft'),
  343. 'ON' => 'ft.user_id = ' . $user->data['user_id'] . ' AND t.forum_id = ft.forum_id'
  344. );
  345. }
  346. $sql = $db->sql_build_query('SELECT', $sql_array);
  347. $result = $db->sql_query($sql);
  348. while ($row = $db->sql_fetchrow($result))
  349. {
  350. if (!$row['forum_id'])
  351. {
  352. // Global Announcement?
  353. $row['forum_id'] = request_var('f', 0);
  354. }
  355. $rowset[$row['topic_id']] = $row;
  356. if ($acl_list && !$auth->acl_gets($acl_list, $row['forum_id']))
  357. {
  358. continue;
  359. }
  360. $topics[$row['topic_id']] = $row;
  361. }
  362. $db->sql_freeresult($result);
  363. }
  364. foreach ($cache_topic_ids as $id)
  365. {
  366. if (!$acl_list || $auth->acl_gets($acl_list, $rowset[$id]['forum_id']))
  367. {
  368. $topics[$id] = $rowset[$id];
  369. }
  370. }
  371. return $topics;
  372. }
  373. /**
  374. * Get simple post data
  375. */
  376. function get_post_data($post_ids, $acl_list = false, $read_tracking = false)
  377. {
  378. global $db, $auth, $config, $user;
  379. $rowset = array();
  380. if (!sizeof($post_ids))
  381. {
  382. return array();
  383. }
  384. $sql_array = array(
  385. 'SELECT' => 'p.*, u.*, t.*, f.*',
  386. 'FROM' => array(
  387. USERS_TABLE => 'u',
  388. POSTS_TABLE => 'p',
  389. TOPICS_TABLE => 't',
  390. ),
  391. 'LEFT_JOIN' => array(
  392. array(
  393. 'FROM' => array(FORUMS_TABLE => 'f'),
  394. 'ON' => 'f.forum_id = t.forum_id'
  395. )
  396. ),
  397. 'WHERE' => $db->sql_in_set('p.post_id', $post_ids) . '
  398. AND u.user_id = p.poster_id
  399. AND t.topic_id = p.topic_id',
  400. );
  401. if ($read_tracking && $config['load_db_lastread'])
  402. {
  403. $sql_array['SELECT'] .= ', tt.mark_time, ft.mark_time as forum_mark_time';
  404. $sql_array['LEFT_JOIN'][] = array(
  405. 'FROM' => array(TOPICS_TRACK_TABLE => 'tt'),
  406. 'ON' => 'tt.user_id = ' . $user->data['user_id'] . ' AND t.topic_id = tt.topic_id'
  407. );
  408. $sql_array['LEFT_JOIN'][] = array(
  409. 'FROM' => array(FORUMS_TRACK_TABLE => 'ft'),
  410. 'ON' => 'ft.user_id = ' . $user->data['user_id'] . ' AND t.forum_id = ft.forum_id'
  411. );
  412. }
  413. $sql = $db->sql_build_query('SELECT', $sql_array);
  414. $result = $db->sql_query($sql);
  415. unset($sql_array);
  416. while ($row = $db->sql_fetchrow($result))
  417. {
  418. if (!$row['forum_id'])
  419. {
  420. // Global Announcement?
  421. $row['forum_id'] = request_var('f', 0);
  422. }
  423. if ($acl_list && !$auth->acl_gets($acl_list, $row['forum_id']))
  424. {
  425. continue;
  426. }
  427. if (!$row['post_approved'] && !$auth->acl_get('m_approve', $row['forum_id']))
  428. {
  429. // Moderators without the permission to approve post should at least not see them. ;)
  430. continue;
  431. }
  432. $rowset[$row['post_id']] = $row;
  433. }
  434. $db->sql_freeresult($result);
  435. return $rowset;
  436. }
  437. /**
  438. * Get simple forum data
  439. */
  440. function get_forum_data($forum_id, $acl_list = 'f_list', $read_tracking = false)
  441. {
  442. global $auth, $db, $user, $config;
  443. $rowset = array();
  444. if (!is_array($forum_id))
  445. {
  446. $forum_id = array($forum_id);
  447. }
  448. if (!sizeof($forum_id))
  449. {
  450. return array();
  451. }
  452. if ($read_tracking && $config['load_db_lastread'])
  453. {
  454. $read_tracking_join = ' LEFT JOIN ' . FORUMS_TRACK_TABLE . ' ft ON (ft.user_id = ' . $user->data['user_id'] . '
  455. AND ft.forum_id = f.forum_id)';
  456. $read_tracking_select = ', ft.mark_time';
  457. }
  458. else
  459. {
  460. $read_tracking_join = $read_tracking_select = '';
  461. }
  462. $sql = "SELECT f.* $read_tracking_select
  463. FROM " . FORUMS_TABLE . " f$read_tracking_join
  464. WHERE " . $db->sql_in_set('f.forum_id', $forum_id);
  465. $result = $db->sql_query($sql);
  466. while ($row = $db->sql_fetchrow($result))
  467. {
  468. if ($acl_list && !$auth->acl_gets($acl_list, $row['forum_id']))
  469. {
  470. continue;
  471. }
  472. if ($auth->acl_get('m_approve', $row['forum_id']))
  473. {
  474. $row['forum_topics'] = $row['forum_topics_real'];
  475. }
  476. $rowset[$row['forum_id']] = $row;
  477. }
  478. $db->sql_freeresult($result);
  479. return $rowset;
  480. }
  481. /**
  482. * Get simple pm data
  483. */
  484. function get_pm_data($pm_ids)
  485. {
  486. global $db, $auth, $config, $user;
  487. $rowset = array();
  488. if (!sizeof($pm_ids))
  489. {
  490. return array();
  491. }
  492. $sql_array = array(
  493. 'SELECT' => 'p.*, u.*',
  494. 'FROM' => array(
  495. USERS_TABLE => 'u',
  496. PRIVMSGS_TABLE => 'p',
  497. ),
  498. 'WHERE' => $db->sql_in_set('p.msg_id', $pm_ids) . '
  499. AND u.user_id = p.author_id',
  500. );
  501. $sql = $db->sql_build_query('SELECT', $sql_array);
  502. $result = $db->sql_query($sql);
  503. unset($sql_array);
  504. while ($row = $db->sql_fetchrow($result))
  505. {
  506. $rowset[$row['msg_id']] = $row;
  507. }
  508. $db->sql_freeresult($result);
  509. return $rowset;
  510. }
  511. /**
  512. * sorting in mcp
  513. *
  514. * @param string $where_sql should either be WHERE (default if ommited) or end with AND or OR
  515. *
  516. * $mode reports and reports_closed: the $where parameters uses aliases p for posts table and r for report table
  517. * $mode unapproved_posts: the $where parameters uses aliases p for posts table and t for topic table
  518. */
  519. function mcp_sorting($mode, &$sort_days, &$sort_key, &$sort_dir, &$sort_by_sql, &$sort_order_sql, &$total, $forum_id = 0, $topic_id = 0, $where_sql = 'WHERE')
  520. {
  521. global $db, $user, $auth, $template;
  522. $sort_days = request_var('st', 0);
  523. $min_time = ($sort_days) ? time() - ($sort_days * 86400) : 0;
  524. switch ($mode)
  525. {
  526. case 'viewforum':
  527. $type = 'topics';
  528. $default_key = 't';
  529. $default_dir = 'd';
  530. $sql = 'SELECT COUNT(topic_id) AS total
  531. FROM ' . TOPICS_TABLE . "
  532. $where_sql forum_id = $forum_id
  533. AND topic_type NOT IN (" . POST_ANNOUNCE . ', ' . POST_GLOBAL . ")
  534. AND topic_last_post_time >= $min_time";
  535. if (!$auth->acl_get('m_approve', $forum_id))
  536. {
  537. $sql .= 'AND topic_approved = 1';
  538. }
  539. break;
  540. case 'viewtopic':
  541. $type = 'posts';
  542. $default_key = 't';
  543. $default_dir = 'a';
  544. $sql = 'SELECT COUNT(post_id) AS total
  545. FROM ' . POSTS_TABLE . "
  546. $where_sql topic_id = $topic_id
  547. AND post_time >= $min_time";
  548. if (!$auth->acl_get('m_approve', $forum_id))
  549. {
  550. $sql .= 'AND post_approved = 1';
  551. }
  552. break;
  553. case 'unapproved_posts':
  554. $type = 'posts';
  555. $default_key = 't';
  556. $default_dir = 'd';
  557. $where_sql .= ($topic_id) ? ' p.topic_id = ' . $topic_id . ' AND' : '';
  558. $sql = 'SELECT COUNT(p.post_id) AS total
  559. FROM ' . POSTS_TABLE . ' p, ' . TOPICS_TABLE . " t
  560. $where_sql " . $db->sql_in_set('p.forum_id', ($forum_id) ? array($forum_id) : array_intersect(get_forum_list('f_read'), get_forum_list('m_approve'))) . '
  561. AND p.post_approved = 0
  562. AND t.topic_id = p.topic_id
  563. AND t.topic_first_post_id <> p.post_id';
  564. if ($min_time)
  565. {
  566. $sql .= ' AND post_time >= ' . $min_time;
  567. }
  568. break;
  569. case 'unapproved_topics':
  570. $type = 'topics';
  571. $default_key = 't';
  572. $default_dir = 'd';
  573. $sql = 'SELECT COUNT(topic_id) AS total
  574. FROM ' . TOPICS_TABLE . "
  575. $where_sql " . $db->sql_in_set('forum_id', ($forum_id) ? array($forum_id) : array_intersect(get_forum_list('f_read'), get_forum_list('m_approve'))) . '
  576. AND topic_approved = 0';
  577. if ($min_time)
  578. {
  579. $sql .= ' AND topic_time >= ' . $min_time;
  580. }
  581. break;
  582. case 'pm_reports':
  583. case 'pm_reports_closed':
  584. case 'reports':
  585. case 'reports_closed':
  586. $pm = (strpos($mode, 'pm_') === 0) ? true : false;
  587. $type = ($pm) ? 'pm_reports' : 'reports';
  588. $default_key = 't';
  589. $default_dir = 'd';
  590. $limit_time_sql = ($min_time) ? "AND r.report_time >= $min_time" : '';
  591. if ($topic_id)
  592. {
  593. $where_sql .= ' p.topic_id = ' . $topic_id . ' AND ';
  594. }
  595. else if ($forum_id)
  596. {
  597. $where_sql .= ' p.forum_id = ' . $forum_id . ' AND ';
  598. }
  599. else if (!$pm)
  600. {
  601. $where_sql .= ' ' . $db->sql_in_set('p.forum_id', get_forum_list(array('!f_read', '!m_report')), true, true) . ' AND ';
  602. }
  603. if ($mode == 'reports' || $mode == 'pm_reports')
  604. {
  605. $where_sql .= ' r.report_closed = 0 AND ';
  606. }
  607. else
  608. {
  609. $where_sql .= ' r.report_closed = 1 AND ';
  610. }
  611. if ($pm)
  612. {
  613. $sql = 'SELECT COUNT(r.report_id) AS total
  614. FROM ' . REPORTS_TABLE . ' r, ' . PRIVMSGS_TABLE . " p
  615. $where_sql r.post_id = 0
  616. AND p.msg_id = r.pm_id
  617. $limit_time_sql";
  618. }
  619. else
  620. {
  621. $sql = 'SELECT COUNT(r.report_id) AS total
  622. FROM ' . REPORTS_TABLE . ' r, ' . POSTS_TABLE . " p
  623. $where_sql r.pm_id = 0
  624. AND p.post_id = r.post_id
  625. $limit_time_sql";
  626. }
  627. break;
  628. case 'viewlogs':
  629. $type = 'logs';
  630. $default_key = 't';
  631. $default_dir = 'd';
  632. $sql = 'SELECT COUNT(log_id) AS total
  633. FROM ' . LOG_TABLE . "
  634. $where_sql " . $db->sql_in_set('forum_id', ($forum_id) ? array($forum_id) : array_intersect(get_forum_list('f_read'), get_forum_list('m_'))) . '
  635. AND log_time >= ' . $min_time . '
  636. AND log_type = ' . LOG_MOD;
  637. break;
  638. }
  639. $sort_key = request_var('sk', $default_key);
  640. $sort_dir = request_var('sd', $default_dir);
  641. $sort_dir_text = array('a' => $user->lang['ASCENDING'], 'd' => $user->lang['DESCENDING']);
  642. switch ($type)
  643. {
  644. case 'topics':
  645. $limit_days = array(0 => $user->lang['ALL_TOPICS'], 1 => $user->lang['1_DAY'], 7 => $user->lang['7_DAYS'], 14 => $user->lang['2_WEEKS'], 30 => $user->lang['1_MONTH'], 90 => $user->lang['3_MONTHS'], 180 => $user->lang['6_MONTHS'], 365 => $user->lang['1_YEAR']);
  646. $sort_by_text = array('a' => $user->lang['AUTHOR'], 't' => $user->lang['POST_TIME'], 'tt' => $user->lang['TOPIC_TIME'], 'r' => $user->lang['REPLIES'], 's' => $user->lang['SUBJECT'], 'v' => $user->lang['VIEWS']);
  647. $sort_by_sql = array('a' => 't.topic_first_poster_name', 't' => 't.topic_last_post_time', 'tt' => 't.topic_time', 'r' => (($auth->acl_get('m_approve', $forum_id)) ? 't.topic_replies_real' : 't.topic_replies'), 's' => 't.topic_title', 'v' => 't.topic_views');
  648. $limit_time_sql = ($min_time) ? "AND t.topic_last_post_time >= $min_time" : '';
  649. break;
  650. case 'posts':
  651. $limit_days = array(0 => $user->lang['ALL_POSTS'], 1 => $user->lang['1_DAY'], 7 => $user->lang['7_DAYS'], 14 => $user->lang['2_WEEKS'], 30 => $user->lang['1_MONTH'], 90 => $user->lang['3_MONTHS'], 180 => $user->lang['6_MONTHS'], 365 => $user->lang['1_YEAR']);
  652. $sort_by_text = array('a' => $user->lang['AUTHOR'], 't' => $user->lang['POST_TIME'], 's' => $user->lang['SUBJECT']);
  653. $sort_by_sql = array('a' => 'u.username_clean', 't' => 'p.post_time', 's' => 'p.post_subject');
  654. $limit_time_sql = ($min_time) ? "AND p.post_time >= $min_time" : '';
  655. break;
  656. case 'reports':
  657. $limit_days = array(0 => $user->lang['ALL_REPORTS'], 1 => $user->lang['1_DAY'], 7 => $user->lang['7_DAYS'], 14 => $user->lang['2_WEEKS'], 30 => $user->lang['1_MONTH'], 90 => $user->lang['3_MONTHS'], 180 => $user->lang['6_MONTHS'], 365 => $user->lang['1_YEAR']);
  658. $sort_by_text = array('a' => $user->lang['AUTHOR'], 'r' => $user->lang['REPORTER'], 'p' => $user->lang['POST_TIME'], 't' => $user->lang['REPORT_TIME'], 's' => $user->lang['SUBJECT']);
  659. $sort_by_sql = array('a' => 'u.username_clean', 'r' => 'ru.username', 'p' => 'p.post_time', 't' => 'r.report_time', 's' => 'p.post_subject');
  660. break;
  661. case 'pm_reports':
  662. $limit_days = array(0 => $user->lang['ALL_REPORTS'], 1 => $user->lang['1_DAY'], 7 => $user->lang['7_DAYS'], 14 => $user->lang['2_WEEKS'], 30 => $user->lang['1_MONTH'], 90 => $user->lang['3_MONTHS'], 180 => $user->lang['6_MONTHS'], 365 => $user->lang['1_YEAR']);
  663. $sort_by_text = array('a' => $user->lang['AUTHOR'], 'r' => $user->lang['REPORTER'], 'p' => $user->lang['POST_TIME'], 't' => $user->lang['REPORT_TIME'], 's' => $user->lang['SUBJECT']);
  664. $sort_by_sql = array('a' => 'u.username_clean', 'r' => 'ru.username', 'p' => 'p.message_time', 't' => 'r.report_time', 's' => 'p.message_subject');
  665. break;
  666. case 'logs':
  667. $limit_days = array(0 => $user->lang['ALL_ENTRIES'], 1 => $user->lang['1_DAY'], 7 => $user->lang['7_DAYS'], 14 => $user->lang['2_WEEKS'], 30 => $user->lang['1_MONTH'], 90 => $user->lang['3_MONTHS'], 180 => $user->lang['6_MONTHS'], 365 => $user->lang['1_YEAR']);
  668. $sort_by_text = array('u' => $user->lang['SORT_USERNAME'], 't' => $user->lang['SORT_DATE'], 'i' => $user->lang['SORT_IP'], 'o' => $user->lang['SORT_ACTION']);
  669. $sort_by_sql = array('u' => 'u.username_clean', 't' => 'l.log_time', 'i' => 'l.log_ip', 'o' => 'l.log_operation');
  670. $limit_time_sql = ($min_time) ? "AND l.log_time >= $min_time" : '';
  671. break;
  672. }
  673. if (!isset($sort_by_sql[$sort_key]))
  674. {
  675. $sort_key = $default_key;
  676. }
  677. $sort_order_sql = $sort_by_sql[$sort_key] . ' ' . (($sort_dir == 'd') ? 'DESC' : 'ASC');
  678. $s_limit_days = $s_sort_key = $s_sort_dir = $sort_url = '';
  679. gen_sort_selects($limit_days, $sort_by_text, $sort_days, $sort_key, $sort_dir, $s_limit_days, $s_sort_key, $s_sort_dir, $sort_url);
  680. $template->assign_vars(array(
  681. 'S_SELECT_SORT_DIR' => $s_sort_dir,
  682. 'S_SELECT_SORT_KEY' => $s_sort_key,
  683. 'S_SELECT_SORT_DAYS' => $s_limit_days)
  684. );
  685. if (($sort_days && $mode != 'viewlogs') || in_array($mode, array('reports', 'unapproved_topics', 'unapproved_posts')) || $where_sql != 'WHERE')
  686. {
  687. $result = $db->sql_query($sql);
  688. $total = (int) $db->sql_fetchfield('total');
  689. $db->sql_freeresult($result);
  690. }
  691. else
  692. {
  693. $total = -1;
  694. }
  695. }
  696. /**
  697. * Validate ids
  698. *
  699. * @param array &$ids The relevant ids to check
  700. * @param string $table The table to find the ids in
  701. * @param string $sql_id The ids relevant column name
  702. * @param array $acl_list A list of permissions the user need to have
  703. * @param mixed $singe_forum Limit to one forum id (int) or the first forum found (true)
  704. *
  705. * @return mixed False if no ids were able to be retrieved, true if at least one id left.
  706. * Additionally, this value can be the forum_id assigned if $single_forum was set.
  707. * Therefore checking the result for with !== false is the best method.
  708. */
  709. function check_ids(&$ids, $table, $sql_id, $acl_list = false, $single_forum = false)
  710. {
  711. global $db, $auth;
  712. if (!is_array($ids) || empty($ids))
  713. {
  714. return false;
  715. }
  716. $sql = "SELECT $sql_id, forum_id FROM $table
  717. WHERE " . $db->sql_in_set($sql_id, $ids);
  718. $result = $db->sql_query($sql);
  719. $ids = array();
  720. $forum_id = false;
  721. while ($row = $db->sql_fetchrow($result))
  722. {
  723. if ($acl_list && $row['forum_id'] && !$auth->acl_gets($acl_list, $row['forum_id']))
  724. {
  725. continue;
  726. }
  727. if ($acl_list && !$row['forum_id'] && !$auth->acl_getf_global($acl_list))
  728. {
  729. continue;
  730. }
  731. // Limit forum? If not, just assign the id.
  732. if ($single_forum === false)
  733. {
  734. $ids[] = $row[$sql_id];
  735. continue;
  736. }
  737. // Limit forum to a specific forum id?
  738. // This can get really tricky, because we do not want to create a failure on global topics. :)
  739. if ($row['forum_id'])
  740. {
  741. if ($single_forum !== true && $row['forum_id'] == (int) $single_forum)
  742. {
  743. $forum_id = (int) $single_forum;
  744. }
  745. else if ($forum_id === false)
  746. {
  747. $forum_id = $row['forum_id'];
  748. }
  749. if ($row['forum_id'] == $forum_id)
  750. {
  751. $ids[] = $row[$sql_id];
  752. }
  753. }
  754. else
  755. {
  756. // Always add a global topic
  757. $ids[] = $row[$sql_id];
  758. }
  759. }
  760. $db->sql_freeresult($result);
  761. if (!sizeof($ids))
  762. {
  763. return false;
  764. }
  765. // If forum id is false and ids populated we may have only global announcements selected (returning 0 because of (int) $forum_id)
  766. return ($single_forum === false) ? true : (int) $forum_id;
  767. }
  768. ?>