PageRenderTime 66ms CodeModel.GetById 34ms RepoModel.GetById 1ms app.codeStats 0ms

/wwwroot/phpbb/phpbb/log/log.php

https://github.com/spring/spring-website
PHP | 962 lines | 548 code | 116 blank | 298 comment | 73 complexity | d36e3955fb105ffbf2ed61e67dbd0345 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, Apache-2.0, LGPL-3.0, BSD-3-Clause
  1. <?php
  2. /**
  3. *
  4. * This file is part of the phpBB Forum Software package.
  5. *
  6. * @copyright (c) phpBB Limited <https://www.phpbb.com>
  7. * @license GNU General Public License, version 2 (GPL-2.0)
  8. *
  9. * For full copyright and license information, please see
  10. * the docs/CREDITS.txt file.
  11. *
  12. */
  13. namespace phpbb\log;
  14. /**
  15. * This class is used to add entries into the log table.
  16. */
  17. class log implements \phpbb\log\log_interface
  18. {
  19. /**
  20. * If set, administrative user profile links will be returned and messages
  21. * will not be censored.
  22. * @var bool
  23. */
  24. protected $is_in_admin;
  25. /**
  26. * An array with the disabled log types. Logs of such types will not be
  27. * added when add_log() is called.
  28. * @var array
  29. */
  30. protected $disabled_types;
  31. /**
  32. * Keeps the total log count of the last call to get_logs()
  33. * @var int
  34. */
  35. protected $entry_count;
  36. /**
  37. * Keeps the offset of the last valid page of the last call to get_logs()
  38. * @var int
  39. */
  40. protected $last_page_offset;
  41. /**
  42. * The table we use to store our logs.
  43. * @var string
  44. */
  45. protected $log_table;
  46. /**
  47. * Database object
  48. * @var \phpbb\db\driver\driver
  49. */
  50. protected $db;
  51. /**
  52. * User object
  53. * @var \phpbb\user
  54. */
  55. protected $user;
  56. /**
  57. * Auth object
  58. * @var \phpbb\auth\auth
  59. */
  60. protected $auth;
  61. /**
  62. * Event dispatcher object
  63. * @var \phpbb\event\dispatcher_interface
  64. */
  65. protected $dispatcher;
  66. /**
  67. * phpBB root path
  68. * @var string
  69. */
  70. protected $phpbb_root_path;
  71. /**
  72. * Admin root path
  73. * @var string
  74. */
  75. protected $phpbb_admin_path;
  76. /**
  77. * PHP Extension
  78. * @var string
  79. */
  80. protected $php_ext;
  81. /**
  82. * Constructor
  83. *
  84. * @param \phpbb\db\driver\driver_interface $db Database object
  85. * @param \phpbb\user $user User object
  86. * @param \phpbb\auth\auth $auth Auth object
  87. * @param \phpbb\event\dispatcher_interface $phpbb_dispatcher Event dispatcher
  88. * @param string $phpbb_root_path Root path
  89. * @param string $relative_admin_path Relative admin root path
  90. * @param string $php_ext PHP Extension
  91. * @param string $log_table Name of the table we use to store our logs
  92. */
  93. public function __construct($db, $user, $auth, $phpbb_dispatcher, $phpbb_root_path, $relative_admin_path, $php_ext, $log_table)
  94. {
  95. $this->db = $db;
  96. $this->user = $user;
  97. $this->auth = $auth;
  98. $this->dispatcher = $phpbb_dispatcher;
  99. $this->phpbb_root_path = $phpbb_root_path;
  100. $this->phpbb_admin_path = $this->phpbb_root_path . $relative_admin_path;
  101. $this->php_ext = $php_ext;
  102. $this->log_table = $log_table;
  103. /*
  104. * IN_ADMIN is set after the session is created,
  105. * so we need to take ADMIN_START into account as well, otherwise
  106. * it will not work for the \phpbb\log\log object we create in common.php
  107. */
  108. $this->set_is_admin((defined('ADMIN_START') && ADMIN_START) || (defined('IN_ADMIN') && IN_ADMIN));
  109. $this->enable();
  110. }
  111. /**
  112. * Set is_in_admin in order to return administrative user profile links
  113. * in get_logs()
  114. *
  115. * @param bool $is_in_admin Are we called from within the acp?
  116. * @return null
  117. */
  118. public function set_is_admin($is_in_admin)
  119. {
  120. $this->is_in_admin = (bool) $is_in_admin;
  121. }
  122. /**
  123. * Returns the is_in_admin option
  124. *
  125. * @return bool
  126. */
  127. public function get_is_admin()
  128. {
  129. return $this->is_in_admin;
  130. }
  131. /**
  132. * Set table name
  133. *
  134. * @param string $log_table Can overwrite the table to use for the logs
  135. * @return null
  136. */
  137. public function set_log_table($log_table)
  138. {
  139. $this->log_table = $log_table;
  140. }
  141. /**
  142. * {@inheritDoc}
  143. */
  144. public function is_enabled($type = '')
  145. {
  146. if ($type == '' || $type == 'all')
  147. {
  148. return !isset($this->disabled_types['all']);
  149. }
  150. return !isset($this->disabled_types[$type]) && !isset($this->disabled_types['all']);
  151. }
  152. /**
  153. * {@inheritDoc}
  154. */
  155. public function disable($type = '')
  156. {
  157. if (is_array($type))
  158. {
  159. foreach ($type as $disable_type)
  160. {
  161. $this->disable($disable_type);
  162. }
  163. return;
  164. }
  165. // Empty string is an equivalent for all types.
  166. if ($type == '')
  167. {
  168. $type = 'all';
  169. }
  170. $this->disabled_types[$type] = true;
  171. }
  172. /**
  173. * {@inheritDoc}
  174. */
  175. public function enable($type = '')
  176. {
  177. if (is_array($type))
  178. {
  179. foreach ($type as $enable_type)
  180. {
  181. $this->enable($enable_type);
  182. }
  183. return;
  184. }
  185. if ($type == '' || $type == 'all')
  186. {
  187. $this->disabled_types = array();
  188. return;
  189. }
  190. unset($this->disabled_types[$type]);
  191. }
  192. /**
  193. * {@inheritDoc}
  194. */
  195. public function add($mode, $user_id, $log_ip, $log_operation, $log_time = false, $additional_data = array())
  196. {
  197. if (!$this->is_enabled($mode))
  198. {
  199. return false;
  200. }
  201. if ($log_time == false)
  202. {
  203. $log_time = time();
  204. }
  205. $sql_ary = array(
  206. 'user_id' => $user_id,
  207. 'log_ip' => $log_ip,
  208. 'log_time' => $log_time,
  209. 'log_operation' => $log_operation,
  210. );
  211. switch ($mode)
  212. {
  213. case 'admin':
  214. $sql_ary += array(
  215. 'log_type' => LOG_ADMIN,
  216. 'log_data' => (!empty($additional_data)) ? serialize($additional_data) : '',
  217. );
  218. break;
  219. case 'mod':
  220. $forum_id = isset($additional_data['forum_id']) ? (int) $additional_data['forum_id'] : 0;
  221. unset($additional_data['forum_id']);
  222. $topic_id = isset($additional_data['topic_id']) ? (int) $additional_data['topic_id'] : 0;
  223. unset($additional_data['topic_id']);
  224. $sql_ary += array(
  225. 'log_type' => LOG_MOD,
  226. 'forum_id' => $forum_id,
  227. 'topic_id' => $topic_id,
  228. 'log_data' => (!empty($additional_data)) ? serialize($additional_data) : '',
  229. );
  230. break;
  231. case 'user':
  232. $reportee_id = (int) $additional_data['reportee_id'];
  233. unset($additional_data['reportee_id']);
  234. $sql_ary += array(
  235. 'log_type' => LOG_USERS,
  236. 'reportee_id' => $reportee_id,
  237. 'log_data' => (!empty($additional_data)) ? serialize($additional_data) : '',
  238. );
  239. break;
  240. case 'critical':
  241. $sql_ary += array(
  242. 'log_type' => LOG_CRITICAL,
  243. 'log_data' => (!empty($additional_data)) ? serialize($additional_data) : '',
  244. );
  245. break;
  246. }
  247. /**
  248. * Allows to modify log data before we add it to the database
  249. *
  250. * NOTE: if sql_ary does not contain a log_type value, the entry will
  251. * not be stored in the database. So ensure to set it, if needed.
  252. *
  253. * @event core.add_log
  254. * @var string mode Mode of the entry we log
  255. * @var int user_id ID of the user who triggered the log
  256. * @var string log_ip IP of the user who triggered the log
  257. * @var string log_operation Language key of the log operation
  258. * @var int log_time Timestamp, when the log was added
  259. * @var array additional_data Array with additional log data
  260. * @var array sql_ary Array with log data we insert into the
  261. * database. If sql_ary[log_type] is not set,
  262. * we won't add the entry to the database.
  263. * @since 3.1.0-a1
  264. */
  265. $vars = array(
  266. 'mode',
  267. 'user_id',
  268. 'log_ip',
  269. 'log_operation',
  270. 'log_time',
  271. 'additional_data',
  272. 'sql_ary',
  273. );
  274. extract($this->dispatcher->trigger_event('core.add_log', compact($vars)));
  275. // We didn't find a log_type, so we don't save it in the database.
  276. if (!isset($sql_ary['log_type']))
  277. {
  278. return false;
  279. }
  280. $this->db->sql_query('INSERT INTO ' . $this->log_table . ' ' . $this->db->sql_build_array('INSERT', $sql_ary));
  281. return $this->db->sql_nextid();
  282. }
  283. /**
  284. * {@inheritDoc}
  285. */
  286. public function delete($mode, $conditions = array())
  287. {
  288. switch ($mode)
  289. {
  290. case 'admin':
  291. $log_type = LOG_ADMIN;
  292. break;
  293. case 'mod':
  294. $log_type = LOG_MOD;
  295. break;
  296. case 'user':
  297. $log_type = LOG_USERS;
  298. break;
  299. case 'users':
  300. $log_type = LOG_USERS;
  301. break;
  302. case 'critical':
  303. $log_type = LOG_CRITICAL;
  304. break;
  305. default:
  306. $log_type = false;
  307. }
  308. /**
  309. * Allows to modify log data before we delete it from the database
  310. *
  311. * NOTE: if sql_ary does not contain a log_type value, the entry will
  312. * not be deleted in the database. So ensure to set it, if needed.
  313. *
  314. * @event core.delete_log
  315. * @var string mode Mode of the entry we log
  316. * @var string log_type Type ID of the log (should be different than false)
  317. * @var array conditions An array of conditions, 3 different forms are accepted
  318. * 1) <key> => <value> transformed into 'AND <key> = <value>' (value should be an integer)
  319. * 2) <key> => array(<operator>, <value>) transformed into 'AND <key> <operator> <value>' (values can't be an array)
  320. * 3) <key> => array('IN' => array(<values>)) transformed into 'AND <key> IN <values>'
  321. * A special field, keywords, can also be defined. In this case only the log entries that have the keywords in log_operation or log_data will be deleted.
  322. * @since 3.1.0-b4
  323. */
  324. $vars = array(
  325. 'mode',
  326. 'log_type',
  327. 'conditions',
  328. );
  329. extract($this->dispatcher->trigger_event('core.delete_log', compact($vars)));
  330. if ($log_type === false)
  331. {
  332. return;
  333. }
  334. $sql_where = 'WHERE log_type = ' . $log_type;
  335. if (isset($conditions['keywords']))
  336. {
  337. $sql_where .= $this->generate_sql_keyword($conditions['keywords'], '');
  338. unset($conditions['keywords']);
  339. }
  340. foreach ($conditions as $field => $field_value)
  341. {
  342. $sql_where .= ' AND ';
  343. if (is_array($field_value) && sizeof($field_value) == 2 && !is_array($field_value[1]))
  344. {
  345. $sql_where .= $field . ' ' . $field_value[0] . ' ' . $field_value[1];
  346. }
  347. else if (is_array($field_value) && isset($field_value['IN']) && is_array($field_value['IN']))
  348. {
  349. $sql_where .= $this->db->sql_in_set($field, $field_value['IN']);
  350. }
  351. else
  352. {
  353. $sql_where .= $field . ' = ' . $field_value;
  354. }
  355. }
  356. $sql = 'DELETE FROM ' . LOG_TABLE . "
  357. $sql_where";
  358. $this->db->sql_query($sql);
  359. $this->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_CLEAR_' . strtoupper($mode));
  360. }
  361. /**
  362. * {@inheritDoc}
  363. */
  364. public function get_logs($mode, $count_logs = true, $limit = 0, $offset = 0, $forum_id = 0, $topic_id = 0, $user_id = 0, $log_time = 0, $sort_by = 'l.log_time DESC', $keywords = '')
  365. {
  366. $this->entry_count = 0;
  367. $this->last_page_offset = $offset;
  368. $topic_id_list = $reportee_id_list = array();
  369. $profile_url = ($this->get_is_admin() && $this->phpbb_admin_path) ? append_sid("{$this->phpbb_admin_path}index.{$this->php_ext}", 'i=users&amp;mode=overview') : append_sid("{$this->phpbb_root_path}memberlist.{$this->php_ext}", 'mode=viewprofile');
  370. switch ($mode)
  371. {
  372. case 'admin':
  373. $log_type = LOG_ADMIN;
  374. $sql_additional = '';
  375. break;
  376. case 'mod':
  377. $log_type = LOG_MOD;
  378. $sql_additional = '';
  379. if ($topic_id)
  380. {
  381. $sql_additional = 'AND l.topic_id = ' . (int) $topic_id;
  382. }
  383. else if (is_array($forum_id))
  384. {
  385. $sql_additional = 'AND ' . $this->db->sql_in_set('l.forum_id', array_map('intval', $forum_id));
  386. }
  387. else if ($forum_id)
  388. {
  389. $sql_additional = 'AND l.forum_id = ' . (int) $forum_id;
  390. }
  391. break;
  392. case 'user':
  393. $log_type = LOG_USERS;
  394. $sql_additional = 'AND l.reportee_id = ' . (int) $user_id;
  395. break;
  396. case 'users':
  397. $log_type = LOG_USERS;
  398. $sql_additional = '';
  399. break;
  400. case 'critical':
  401. $log_type = LOG_CRITICAL;
  402. $sql_additional = '';
  403. break;
  404. default:
  405. $log_type = false;
  406. $sql_additional = '';
  407. }
  408. /**
  409. * Overwrite log type and limitations before we count and get the logs
  410. *
  411. * NOTE: if log_type is false, no entries will be returned.
  412. *
  413. * @event core.get_logs_modify_type
  414. * @var string mode Mode of the entries we display
  415. * @var bool count_logs Do we count all matching entries?
  416. * @var int limit Limit the number of entries
  417. * @var int offset Offset when fetching the entries
  418. * @var mixed forum_id Limit entries to the forum_id,
  419. * can also be an array of forum_ids
  420. * @var int topic_id Limit entries to the topic_id
  421. * @var int user_id Limit entries to the user_id
  422. * @var int log_time Limit maximum age of log entries
  423. * @var string sort_by SQL order option
  424. * @var string keywords Will only return entries that have the
  425. * keywords in log_operation or log_data
  426. * @var string profile_url URL to the users profile
  427. * @var int log_type Limit logs to a certain type. If log_type
  428. * is false, no entries will be returned.
  429. * @var string sql_additional Additional conditions for the entries,
  430. * e.g.: 'AND l.forum_id = 1'
  431. * @since 3.1.0-a1
  432. */
  433. $vars = array(
  434. 'mode',
  435. 'count_logs',
  436. 'limit',
  437. 'offset',
  438. 'forum_id',
  439. 'topic_id',
  440. 'user_id',
  441. 'log_time',
  442. 'sort_by',
  443. 'keywords',
  444. 'profile_url',
  445. 'log_type',
  446. 'sql_additional',
  447. );
  448. extract($this->dispatcher->trigger_event('core.get_logs_modify_type', compact($vars)));
  449. if ($log_type === false)
  450. {
  451. $this->last_page_offset = 0;
  452. return array();
  453. }
  454. $sql_keywords = '';
  455. if (!empty($keywords))
  456. {
  457. // Get the SQL condition for our keywords
  458. $sql_keywords = $this->generate_sql_keyword($keywords);
  459. }
  460. $get_logs_sql_ary = array(
  461. 'SELECT' => 'l.*, u.username, u.username_clean, u.user_colour',
  462. 'FROM' => array(
  463. $this->log_table => 'l',
  464. USERS_TABLE => 'u',
  465. ),
  466. 'WHERE' => 'l.log_type = ' . (int) $log_type . "
  467. AND l.user_id = u.user_id
  468. $sql_keywords
  469. $sql_additional",
  470. 'ORDER_BY' => $sort_by,
  471. );
  472. if($log_time)
  473. {
  474. $get_logs_sql_ary['WHERE'] = 'l.log_time >= ' . (int) $log_time . '
  475. AND ' . $get_logs_sql_ary['WHERE'];
  476. }
  477. /**
  478. * Modify the query to obtain the logs data
  479. *
  480. * @event core.get_logs_main_query_before
  481. * @var array get_logs_sql_ary The array in the format of the query builder with the query
  482. * to get the log count and the log list
  483. * @var string mode Mode of the entries we display
  484. * @var bool count_logs Do we count all matching entries?
  485. * @var int limit Limit the number of entries
  486. * @var int offset Offset when fetching the entries
  487. * @var mixed forum_id Limit entries to the forum_id,
  488. * can also be an array of forum_ids
  489. * @var int topic_id Limit entries to the topic_id
  490. * @var int user_id Limit entries to the user_id
  491. * @var int log_time Limit maximum age of log entries
  492. * @var string sort_by SQL order option
  493. * @var string keywords Will only return entries that have the
  494. * keywords in log_operation or log_data
  495. * @var string profile_url URL to the users profile
  496. * @var int log_type Limit logs to a certain type. If log_type
  497. * is false, no entries will be returned.
  498. * @var string sql_additional Additional conditions for the entries,
  499. * e.g.: 'AND l.forum_id = 1'
  500. * @since 3.1.5-RC1
  501. */
  502. $vars = array(
  503. 'get_logs_sql_ary',
  504. 'mode',
  505. 'count_logs',
  506. 'limit',
  507. 'offset',
  508. 'forum_id',
  509. 'topic_id',
  510. 'user_id',
  511. 'log_time',
  512. 'sort_by',
  513. 'keywords',
  514. 'profile_url',
  515. 'log_type',
  516. 'sql_additional',
  517. );
  518. extract($this->dispatcher->trigger_event('core.get_logs_main_query_before', compact($vars)));
  519. if ($count_logs)
  520. {
  521. $count_logs_sql_ary = $get_logs_sql_ary;
  522. $count_logs_sql_ary['SELECT'] = 'COUNT(l.log_id) AS total_entries';
  523. unset($count_logs_sql_ary['ORDER_BY']);
  524. $sql = $this->db->sql_build_query('SELECT', $count_logs_sql_ary);
  525. $result = $this->db->sql_query($sql);
  526. $this->entry_count = (int) $this->db->sql_fetchfield('total_entries');
  527. $this->db->sql_freeresult($result);
  528. if ($this->entry_count == 0)
  529. {
  530. // Save the queries, because there are no logs to display
  531. $this->last_page_offset = 0;
  532. return array();
  533. }
  534. // Return the user to the last page that is valid
  535. while ($this->last_page_offset >= $this->entry_count)
  536. {
  537. $this->last_page_offset = max(0, $this->last_page_offset - $limit);
  538. }
  539. }
  540. $sql = $this->db->sql_build_query('SELECT', $get_logs_sql_ary);
  541. $result = $this->db->sql_query_limit($sql, $limit, $this->last_page_offset);
  542. $i = 0;
  543. $log = array();
  544. while ($row = $this->db->sql_fetchrow($result))
  545. {
  546. $row['forum_id'] = (int) $row['forum_id'];
  547. if ($row['topic_id'])
  548. {
  549. $topic_id_list[] = (int) $row['topic_id'];
  550. }
  551. if ($row['reportee_id'])
  552. {
  553. $reportee_id_list[] = (int) $row['reportee_id'];
  554. }
  555. $log_entry_data = array(
  556. 'id' => (int) $row['log_id'],
  557. 'reportee_id' => (int) $row['reportee_id'],
  558. 'reportee_username' => '',
  559. 'reportee_username_full'=> '',
  560. 'user_id' => (int) $row['user_id'],
  561. 'username' => $row['username'],
  562. 'username_full' => get_username_string('full', $row['user_id'], $row['username'], $row['user_colour'], false, $profile_url),
  563. 'ip' => $row['log_ip'],
  564. 'time' => (int) $row['log_time'],
  565. 'forum_id' => (int) $row['forum_id'],
  566. 'topic_id' => (int) $row['topic_id'],
  567. 'viewforum' => ($row['forum_id'] && $this->auth->acl_get('f_read', $row['forum_id'])) ? append_sid("{$this->phpbb_root_path}viewforum.{$this->php_ext}", 'f=' . $row['forum_id']) : false,
  568. 'action' => (isset($this->user->lang[$row['log_operation']])) ? $row['log_operation'] : '{' . ucfirst(str_replace('_', ' ', $row['log_operation'])) . '}',
  569. );
  570. /**
  571. * Modify the entry's data before it is returned
  572. *
  573. * @event core.get_logs_modify_entry_data
  574. * @var array row Entry data from the database
  575. * @var array log_entry_data Entry's data which is returned
  576. * @since 3.1.0-a1
  577. */
  578. $vars = array('row', 'log_entry_data');
  579. extract($this->dispatcher->trigger_event('core.get_logs_modify_entry_data', compact($vars)));
  580. $log[$i] = $log_entry_data;
  581. if (!empty($row['log_data']))
  582. {
  583. $log_data_ary = unserialize($row['log_data']);
  584. $log_data_ary = ($log_data_ary !== false) ? $log_data_ary : array();
  585. if (isset($this->user->lang[$row['log_operation']]))
  586. {
  587. // Check if there are more occurrences of % than
  588. // arguments, if there are we fill out the arguments
  589. // array. It doesn't matter if we add more arguments than
  590. // placeholders.
  591. $num_args = 0;
  592. if (!is_array($this->user->lang[$row['log_operation']]))
  593. {
  594. $num_args = substr_count($this->user->lang[$row['log_operation']], '%');
  595. }
  596. else
  597. {
  598. foreach ($this->user->lang[$row['log_operation']] as $case => $plural_string)
  599. {
  600. $num_args = max($num_args, substr_count($plural_string, '%'));
  601. }
  602. }
  603. if (($num_args - sizeof($log_data_ary)) > 0)
  604. {
  605. $log_data_ary = array_merge($log_data_ary, array_fill(0, $num_args - sizeof($log_data_ary), ''));
  606. }
  607. $lang_arguments = array_merge(array($log[$i]['action']), $log_data_ary);
  608. $log[$i]['action'] = call_user_func_array(array($this->user, 'lang'), $lang_arguments);
  609. // If within the admin panel we do not censor text out
  610. if ($this->get_is_admin())
  611. {
  612. $log[$i]['action'] = bbcode_nl2br($log[$i]['action']);
  613. }
  614. else
  615. {
  616. $log[$i]['action'] = bbcode_nl2br(censor_text($log[$i]['action']));
  617. }
  618. }
  619. else if (!empty($log_data_ary))
  620. {
  621. $log[$i]['action'] .= '<br />' . implode('', $log_data_ary);
  622. }
  623. /* Apply make_clickable... has to be seen if it is for good. :/
  624. // Seems to be not for the moment, reconsider later...
  625. $log[$i]['action'] = make_clickable($log[$i]['action']);
  626. */
  627. }
  628. else
  629. {
  630. $log[$i]['action'] = $this->user->lang($log[$i]['action']);
  631. }
  632. $i++;
  633. }
  634. $this->db->sql_freeresult($result);
  635. /**
  636. * Get some additional data after we got all log entries
  637. *
  638. * @event core.get_logs_get_additional_data
  639. * @var array log Array with all our log entries
  640. * @var array topic_id_list Array of topic ids, for which we
  641. * get the permission data
  642. * @var array reportee_id_list Array of additional user IDs we
  643. * get the username strings for
  644. * @since 3.1.0-a1
  645. */
  646. $vars = array('log', 'topic_id_list', 'reportee_id_list');
  647. extract($this->dispatcher->trigger_event('core.get_logs_get_additional_data', compact($vars)));
  648. if (sizeof($topic_id_list))
  649. {
  650. $topic_auth = $this->get_topic_auth($topic_id_list);
  651. foreach ($log as $key => $row)
  652. {
  653. $log[$key]['viewtopic'] = (isset($topic_auth['f_read'][$row['topic_id']])) ? append_sid("{$this->phpbb_root_path}viewtopic.{$this->php_ext}", 'f=' . $topic_auth['f_read'][$row['topic_id']] . '&amp;t=' . $row['topic_id']) : false;
  654. $log[$key]['viewlogs'] = (isset($topic_auth['m_'][$row['topic_id']])) ? append_sid("{$this->phpbb_root_path}mcp.{$this->php_ext}", 'i=logs&amp;mode=topic_logs&amp;t=' . $row['topic_id'], true, $this->user->session_id) : false;
  655. }
  656. }
  657. if (sizeof($reportee_id_list))
  658. {
  659. $reportee_data_list = $this->get_reportee_data($reportee_id_list);
  660. foreach ($log as $key => $row)
  661. {
  662. if (!isset($reportee_data_list[$row['reportee_id']]))
  663. {
  664. continue;
  665. }
  666. $log[$key]['reportee_username'] = $reportee_data_list[$row['reportee_id']]['username'];
  667. $log[$key]['reportee_username_full'] = get_username_string('full', $row['reportee_id'], $reportee_data_list[$row['reportee_id']]['username'], $reportee_data_list[$row['reportee_id']]['user_colour'], false, $profile_url);
  668. }
  669. }
  670. /**
  671. * Allow modifying or execute extra final filter on log entries
  672. *
  673. * @event core.get_logs_after
  674. * @var array log Array with all our log entries
  675. * @var array topic_id_list Array of topic ids, for which we
  676. * get the permission data
  677. * @var array reportee_id_list Array of additional user IDs we
  678. * get the username strings for
  679. * @var string mode Mode of the entries we display
  680. * @var bool count_logs Do we count all matching entries?
  681. * @var int limit Limit the number of entries
  682. * @var int offset Offset when fetching the entries
  683. * @var mixed forum_id Limit entries to the forum_id,
  684. * can also be an array of forum_ids
  685. * @var int topic_id Limit entries to the topic_id
  686. * @var int user_id Limit entries to the user_id
  687. * @var int log_time Limit maximum age of log entries
  688. * @var string sort_by SQL order option
  689. * @var string keywords Will only return entries that have the
  690. * keywords in log_operation or log_data
  691. * @var string profile_url URL to the users profile
  692. * @var int log_type The type of logs it was filtered
  693. * @since 3.1.3-RC1
  694. */
  695. $vars = array(
  696. 'log',
  697. 'topic_id_list',
  698. 'reportee_id_list',
  699. 'mode',
  700. 'count_logs',
  701. 'limit',
  702. 'offset',
  703. 'forum_id',
  704. 'topic_id',
  705. 'user_id',
  706. 'log_time',
  707. 'sort_by',
  708. 'keywords',
  709. 'profile_url',
  710. 'log_type',
  711. );
  712. extract($this->dispatcher->trigger_event('core.get_logs_after', compact($vars)));
  713. return $log;
  714. }
  715. /**
  716. * Generates a sql condition for the specified keywords
  717. *
  718. * @param string $keywords The keywords the user specified to search for
  719. * @param string $table_alias The alias of the logs' table ('l.' by default)
  720. * @param string $statement_operator The operator used to prefix the statement ('AND' by default)
  721. *
  722. * @return string Returns the SQL condition searching for the keywords
  723. */
  724. protected function generate_sql_keyword($keywords, $table_alias = 'l.', $statement_operator = 'AND')
  725. {
  726. // Use no preg_quote for $keywords because this would lead to sole
  727. // backslashes being added. We also use an OR connection here for
  728. // spaces and the | string. Currently, regex is not supported for
  729. // searching (but may come later).
  730. $keywords = preg_split('#[\s|]+#u', utf8_strtolower($keywords), 0, PREG_SPLIT_NO_EMPTY);
  731. $sql_keywords = '';
  732. if (!empty($keywords))
  733. {
  734. $keywords_pattern = array();
  735. // Build pattern and keywords...
  736. for ($i = 0, $num_keywords = sizeof($keywords); $i < $num_keywords; $i++)
  737. {
  738. $keywords_pattern[] = preg_quote($keywords[$i], '#');
  739. $keywords[$i] = $this->db->sql_like_expression($this->db->get_any_char() . $keywords[$i] . $this->db->get_any_char());
  740. }
  741. $keywords_pattern = '#' . implode('|', $keywords_pattern) . '#ui';
  742. $operations = array();
  743. foreach ($this->user->lang as $key => $value)
  744. {
  745. if (substr($key, 0, 4) == 'LOG_')
  746. {
  747. if (is_array($value))
  748. {
  749. foreach ($value as $plural_value)
  750. {
  751. if (preg_match($keywords_pattern, $plural_value))
  752. {
  753. $operations[] = $key;
  754. break;
  755. }
  756. }
  757. }
  758. else if (preg_match($keywords_pattern, $value))
  759. {
  760. $operations[] = $key;
  761. }
  762. }
  763. }
  764. $sql_keywords = ' ' . $statement_operator . ' (';
  765. if (!empty($operations))
  766. {
  767. $sql_keywords .= $this->db->sql_in_set($table_alias . 'log_operation', $operations) . ' OR ';
  768. }
  769. $sql_lower = $this->db->sql_lower_text($table_alias . 'log_data');
  770. $sql_keywords .= " $sql_lower " . implode(" OR $sql_lower ", $keywords) . ')';
  771. }
  772. return $sql_keywords;
  773. }
  774. /**
  775. * Determine whether the user is allowed to read and/or moderate the forum of the topic
  776. *
  777. * @param array $topic_ids Array with the topic ids
  778. *
  779. * @return array Returns an array with two keys 'm_' and 'read_f' which are also an array of topic_id => forum_id sets when the permissions are given. Sample:
  780. * array(
  781. * 'permission' => array(
  782. * topic_id => forum_id
  783. * ),
  784. * ),
  785. */
  786. protected function get_topic_auth(array $topic_ids)
  787. {
  788. $forum_auth = array('f_read' => array(), 'm_' => array());
  789. $topic_ids = array_unique($topic_ids);
  790. $sql = 'SELECT topic_id, forum_id
  791. FROM ' . TOPICS_TABLE . '
  792. WHERE ' . $this->db->sql_in_set('topic_id', array_map('intval', $topic_ids));
  793. $result = $this->db->sql_query($sql);
  794. while ($row = $this->db->sql_fetchrow($result))
  795. {
  796. $row['topic_id'] = (int) $row['topic_id'];
  797. $row['forum_id'] = (int) $row['forum_id'];
  798. if ($this->auth->acl_get('f_read', $row['forum_id']))
  799. {
  800. $forum_auth['f_read'][$row['topic_id']] = $row['forum_id'];
  801. }
  802. if ($this->auth->acl_gets('a_', 'm_', $row['forum_id']))
  803. {
  804. $forum_auth['m_'][$row['topic_id']] = $row['forum_id'];
  805. }
  806. }
  807. $this->db->sql_freeresult($result);
  808. return $forum_auth;
  809. }
  810. /**
  811. * Get the data for all reportee from the database
  812. *
  813. * @param array $reportee_ids Array with the user ids of the reportees
  814. *
  815. * @return array Returns an array with the reportee data
  816. */
  817. protected function get_reportee_data(array $reportee_ids)
  818. {
  819. $reportee_ids = array_unique($reportee_ids);
  820. $reportee_data_list = array();
  821. $sql = 'SELECT user_id, username, user_colour
  822. FROM ' . USERS_TABLE . '
  823. WHERE ' . $this->db->sql_in_set('user_id', $reportee_ids);
  824. $result = $this->db->sql_query($sql);
  825. while ($row = $this->db->sql_fetchrow($result))
  826. {
  827. $reportee_data_list[$row['user_id']] = $row;
  828. }
  829. $this->db->sql_freeresult($result);
  830. return $reportee_data_list;
  831. }
  832. /**
  833. * {@inheritDoc}
  834. */
  835. public function get_log_count()
  836. {
  837. return ($this->entry_count) ? $this->entry_count : 0;
  838. }
  839. /**
  840. * {@inheritDoc}
  841. */
  842. public function get_valid_offset()
  843. {
  844. return ($this->last_page_offset) ? $this->last_page_offset : 0;
  845. }
  846. }