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

/phpBB/mcp.php

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