1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-02-24 03:54:10 +01:00

[ticket/10714] Use phpbb_log class in view_log()

PHPBB3-10714
This commit is contained in:
Joas Schilling 2012-03-24 16:39:03 +01:00
parent 55b94af82e
commit 97290647fa
2 changed files with 87 additions and 295 deletions

View File

@ -2488,275 +2488,34 @@ function cache_moderators()
*/
function view_log($mode, &$log, &$log_count, $limit = 0, $offset = 0, $forum_id = 0, $topic_id = 0, $user_id = 0, $limit_days = 0, $sort_by = 'l.log_time DESC', $keywords = '')
{
global $db, $user, $auth, $phpEx, $phpbb_root_path, $phpbb_admin_path;
// This is all just an ugly hack to add "Dependency Injection" to a function
// the only real code is the function call which maps this function to a method.
static $static_log = null;
$topic_id_list = $reportee_id_list = $is_auth = $is_mod = array();
$profile_url = (defined('IN_ADMIN')) ? append_sid("{$phpbb_admin_path}index.$phpEx", 'i=users&mode=overview') : append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=viewprofile');
switch ($mode)
if ($mode instanceof phpbb_log_interface)
{
case 'admin':
$log_type = LOG_ADMIN;
$sql_forum = '';
break;
case 'mod':
$log_type = LOG_MOD;
$sql_forum = '';
if ($topic_id)
{
$sql_forum = 'AND l.topic_id = ' . (int) $topic_id;
}
else if (is_array($forum_id))
{
$sql_forum = 'AND ' . $db->sql_in_set('l.forum_id', array_map('intval', $forum_id));
}
else if ($forum_id)
{
$sql_forum = 'AND l.forum_id = ' . (int) $forum_id;
}
break;
case 'user':
$log_type = LOG_USERS;
$sql_forum = 'AND l.reportee_id = ' . (int) $user_id;
break;
case 'users':
$log_type = LOG_USERS;
$sql_forum = '';
break;
case 'critical':
$log_type = LOG_CRITICAL;
$sql_forum = '';
break;
default:
return;
$static_log = $mode;
return true;
}
else if ($mode === false)
{
return false;
}
// Use no preg_quote for $keywords because this would lead to sole backslashes being added
// We also use an OR connection here for spaces and the | string. Currently, regex is not supported for searching (but may come later).
$keywords = preg_split('#[\s|]+#u', utf8_strtolower($keywords), 0, PREG_SPLIT_NO_EMPTY);
$sql_keywords = '';
$tmp_log = $static_log;
if (!empty($keywords))
// no log class set, create a temporary one ourselves to keep backwards compatability
if ($tmp_log === null)
{
$keywords_pattern = array();
// Build pattern and keywords...
for ($i = 0, $num_keywords = sizeof($keywords); $i < $num_keywords; $i++)
{
$keywords_pattern[] = preg_quote($keywords[$i], '#');
$keywords[$i] = $db->sql_like_expression($db->any_char . $keywords[$i] . $db->any_char);
}
$keywords_pattern = '#' . implode('|', $keywords_pattern) . '#ui';
$operations = array();
foreach ($user->lang as $key => $value)
{
if (substr($key, 0, 4) == 'LOG_' && preg_match($keywords_pattern, $value))
{
$operations[] = $key;
}
}
$sql_keywords = 'AND (';
if (!empty($operations))
{
$sql_keywords .= $db->sql_in_set('l.log_operation', $operations) . ' OR ';
}
$sql_lower = $db->sql_lower_text('l.log_data');
$sql_keywords .= "$sql_lower " . implode(" OR $sql_lower ", $keywords) . ')';
$tmp_log = new phpbb_log(LOG_TABLE);
}
if ($log_count !== false)
{
$sql = 'SELECT COUNT(l.log_id) AS total_entries
FROM ' . LOG_TABLE . ' l, ' . USERS_TABLE . " u
WHERE l.log_type = $log_type
AND l.user_id = u.user_id
AND l.log_time >= $limit_days
$sql_keywords
$sql_forum";
$result = $db->sql_query($sql);
$log_count = (int) $db->sql_fetchfield('total_entries');
$db->sql_freeresult($result);
}
$count_logs = ($log_count !== false);
// $log_count may be false here if false was passed in for it,
// because in this case we did not run the COUNT() query above.
// If we ran the COUNT() query and it returned zero rows, return;
// otherwise query for logs below.
if ($log_count === 0)
{
// Save the queries, because there are no logs to display
return 0;
}
$log = $tmp_log->get_logs($mode, $count_logs, $limit, $offset, $forum_id, $topic_id, $user_id, $limit_days, $sort_by, $keywords);
$log_count = $tmp_log->get_log_count();
if ($log_count)
{
// Return the user to the last page that is valid
while ($offset >= $log_count)
{
$offset = ($offset - $limit < 0) ? 0 : $offset - $limit;
}
}
$sql = "SELECT l.*, u.username, u.username_clean, u.user_colour
FROM " . LOG_TABLE . " l, " . USERS_TABLE . " u
WHERE l.log_type = $log_type
AND u.user_id = l.user_id
" . (($limit_days) ? "AND l.log_time >= $limit_days" : '') . "
$sql_keywords
$sql_forum
ORDER BY $sort_by";
$result = $db->sql_query_limit($sql, $limit, $offset);
$i = 0;
$log = array();
while ($row = $db->sql_fetchrow($result))
{
$row['forum_id'] = (int) $row['forum_id'];
if ($row['topic_id'])
{
$topic_id_list[] = $row['topic_id'];
}
if ($row['reportee_id'])
{
$reportee_id_list[] = $row['reportee_id'];
}
$log[$i] = array(
'id' => (int) $row['log_id'],
'reportee_id' => (int) $row['reportee_id'],
'reportee_username' => '',
'reportee_username_full'=> '',
'user_id' => (int) $row['user_id'],
'username' => $row['username'],
'username_full' => get_username_string('full', $row['user_id'], $row['username'], $row['user_colour'], false, $profile_url),
'ip' => $row['log_ip'],
'time' => (int) $row['log_time'],
'forum_id' => (int) $row['forum_id'],
'topic_id' => (int) $row['topic_id'],
'viewforum' => ($row['forum_id'] && $auth->acl_get('f_read', $row['forum_id'])) ? append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $row['forum_id']) : false,
'action' => (isset($user->lang[$row['log_operation']])) ? $user->lang[$row['log_operation']] : '{' . ucfirst(str_replace('_', ' ', $row['log_operation'])) . '}',
);
if (!empty($row['log_data']))
{
$log_data_ary = @unserialize($row['log_data']);
$log_data_ary = ($log_data_ary === false) ? array() : $log_data_ary;
if (isset($user->lang[$row['log_operation']]))
{
// Check if there are more occurrences of % than arguments, if there are we fill out the arguments array
// It doesn't matter if we add more arguments than placeholders
if ((substr_count($log[$i]['action'], '%') - sizeof($log_data_ary)) > 0)
{
$log_data_ary = array_merge($log_data_ary, array_fill(0, substr_count($log[$i]['action'], '%') - sizeof($log_data_ary), ''));
}
$log[$i]['action'] = vsprintf($log[$i]['action'], $log_data_ary);
// If within the admin panel we do not censor text out
if (defined('IN_ADMIN'))
{
$log[$i]['action'] = bbcode_nl2br($log[$i]['action']);
}
else
{
$log[$i]['action'] = bbcode_nl2br(censor_text($log[$i]['action']));
}
}
else if (!empty($log_data_ary))
{
$log[$i]['action'] .= '<br />' . implode('', $log_data_ary);
}
/* Apply make_clickable... has to be seen if it is for good. :/
// Seems to be not for the moment, reconsider later...
$log[$i]['action'] = make_clickable($log[$i]['action']);
*/
}
$i++;
}
$db->sql_freeresult($result);
if (sizeof($topic_id_list))
{
$topic_id_list = array_unique($topic_id_list);
// This query is not really needed if move_topics() updates the forum_id field,
// although it's also used to determine if the topic still exists in the database
$sql = 'SELECT topic_id, forum_id
FROM ' . TOPICS_TABLE . '
WHERE ' . $db->sql_in_set('topic_id', array_map('intval', $topic_id_list));
$result = $db->sql_query($sql);
$default_forum_id = 0;
while ($row = $db->sql_fetchrow($result))
{
$row['forum_id'] = (int) $row['forum_id'];
if ($auth->acl_get('f_read', $row['forum_id']))
{
$is_auth[$row['topic_id']] = $row['forum_id'];
}
if ($auth->acl_gets('a_', 'm_', $row['forum_id']))
{
$is_mod[$row['topic_id']] = $row['forum_id'];
}
}
$db->sql_freeresult($result);
foreach ($log as $key => $row)
{
$log[$key]['viewtopic'] = (isset($is_auth[$row['topic_id']])) ? append_sid("{$phpbb_root_path}viewtopic.$phpEx", 'f=' . $is_auth[$row['topic_id']] . '&amp;t=' . $row['topic_id']) : false;
$log[$key]['viewlogs'] = (isset($is_mod[$row['topic_id']])) ? append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=logs&amp;mode=topic_logs&amp;t=' . $row['topic_id'], true, $user->session_id) : false;
}
}
if (sizeof($reportee_id_list))
{
$reportee_id_list = array_unique($reportee_id_list);
$reportee_names_list = array();
$sql = 'SELECT user_id, username, user_colour
FROM ' . USERS_TABLE . '
WHERE ' . $db->sql_in_set('user_id', $reportee_id_list);
$result = $db->sql_query($sql);
while ($row = $db->sql_fetchrow($result))
{
$reportee_names_list[$row['user_id']] = $row;
}
$db->sql_freeresult($result);
foreach ($log as $key => $row)
{
if (!isset($reportee_names_list[$row['reportee_id']]))
{
continue;
}
$log[$key]['reportee_username'] = $reportee_names_list[$row['reportee_id']]['username'];
$log[$key]['reportee_username_full'] = get_username_string('full', $row['reportee_id'], $reportee_names_list[$row['reportee_id']]['username'], $reportee_names_list[$row['reportee_id']]['user_colour'], false, $profile_url);
}
}
return $offset;
return $tmp_log->get_valid_offset();
}
/**

View File

@ -25,7 +25,7 @@ class phpbb_log implements phpbb_log_interface
/**
* Keeps the status of the log-system. Is the log enabled or disabled?
*/
private $enabled;
private $disabled_logs;
/**
* Keeps the total log count of the last call to get_logs()
@ -56,31 +56,70 @@ class phpbb_log implements phpbb_log_interface
/**
* This function returns the state of the log-system.
*
* @return bool True if log is enabled
* @param string $type The log type we want to check. Empty to get global log status.
*
* @return bool True if log for the type is enabled
*/
public function is_enabled()
public function is_enabled($type = '')
{
return $this->enabled;
if ($type == '' || $type == 'all')
{
return !isset($this->disabled_logs['all']);
}
return !isset($this->disabled_logs[$type]) && !isset($this->disabled_logs['all']);
}
/**
* This function allows disable the log-system. When add_log is called, the log will not be added to the database.
*
* @param mixed $type The log type we want to enable. Empty to disable all logs.
* Can also be an array of types
*
* @return null
*/
public function disable()
public function disable($type = '')
{
$this->enabled = false;
if (is_array($type))
{
foreach ($type as $disable_type)
{
$this->disable($disable_type);
}
return;
}
if ($type == '' || $type == 'all')
{
$this->disabled_logs['all'] = true;
return;
}
$this->disabled_logs[$type] = true;
}
/**
* This function allows re-enable the log-system.
*
* @param mixed $type The log type we want to enable. Empty to enable all logs.
*
* @return null
*/
public function enable()
public function enable($type = '')
{
$this->enabled = true;
if (is_array($type))
{
foreach ($type as $enable_type)
{
$this->enable($enable_type);
}
return;
}
if ($type == '' || $type == 'all')
{
$this->disabled_logs = array();
return;
}
unset($this->disabled_logs[$type]);
}
/**
@ -90,7 +129,7 @@ class phpbb_log implements phpbb_log_interface
*/
public function add($mode, $user_id, $log_ip, $log_operation, $log_time = false, $additional_data = array())
{
if (!$this->is_enabled())
if (!$this->is_enabled($mode))
{
return false;
}
@ -119,7 +158,7 @@ class phpbb_log implements phpbb_log_interface
case 'admin':
$sql_ary += array(
'log_type' => LOG_ADMIN,
'log_data' => (!sizeof($additional_data)) ? '' : serialize($additional_data),
'log_data' => (!empty($additional_data)) ? serialize($additional_data) : '',
);
break;
@ -132,7 +171,7 @@ class phpbb_log implements phpbb_log_interface
'log_type' => LOG_MOD,
'forum_id' => $forum_id,
'topic_id' => $topic_id,
'log_data' => (!sizeof($additional_data)) ? '' : serialize($additional_data),
'log_data' => (!empty($additional_data)) ? serialize($additional_data) : '',
);
break;
@ -143,14 +182,14 @@ class phpbb_log implements phpbb_log_interface
$sql_ary += array(
'log_type' => LOG_USERS,
'reportee_id' => $reportee_id,
'log_data' => (!sizeof($additional_data)) ? '' : serialize($additional_data),
'log_data' => (!empty($additional_data)) ? serialize($additional_data) : '',
);
break;
case 'critical':
$sql_ary += array(
'log_type' => LOG_CRITICAL,
'log_data' => (!sizeof($additional_data)) ? '' : serialize($additional_data),
'log_data' => (!empty($additional_data)) ? serialize($additional_data) : '',
);
break;
@ -161,9 +200,7 @@ class phpbb_log implements phpbb_log_interface
if ($phpbb_dispatcher != null)
{
$vars = array('mode', 'user_id', 'log_ip', 'log_operation', 'log_time', 'additional_data', 'sql_ary');
$event = new phpbb_event_data(compact($vars));
$phpbb_dispatcher->dispatch('core.add_log_case', $event);
extract($event->get_data_filtered($vars));
extract($phpbb_dispatcher->trigger_event('core.add_log_case', $vars, $vars));
}
*/
@ -180,9 +217,7 @@ class phpbb_log implements phpbb_log_interface
if ($phpbb_dispatcher != null)
{
$vars = array('mode', 'user_id', 'log_ip', 'log_operation', 'log_time', 'additional_data', 'sql_ary');
$event = new phpbb_event_data(compact($vars));
$phpbb_dispatcher->dispatch('core.add_log', $event);
extract($event->get_data_filtered($vars));
extract($phpbb_dispatcher->trigger_event('core.add_log', $vars, $vars));
}
*/
@ -199,6 +234,11 @@ class phpbb_log implements phpbb_log_interface
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 = '')
{
global $db, $user, $auth, $phpEx, $phpbb_root_path, $phpbb_admin_path;
/**
* @todo: enable when events are merged
*
global $db, $user, $auth, $phpEx, $phpbb_root_path, $phpbb_admin_path, $phpbb_dispatcher;
*/
$this->logs_total = 0;
$this->logs_offset = $offset;
@ -256,9 +296,7 @@ class phpbb_log implements phpbb_log_interface
if ($phpbb_dispatcher != null)
{
$vars = array('mode', 'count_logs', 'limit', 'offset', 'forum_id', 'topic_id', 'user_id', 'log_time', 'sort_by', 'keywords', 'profile_url', 'log_type', 'sql_additional');
$event = new phpbb_event_data(compact($vars));
$phpbb_dispatcher->dispatch('core.get_logs_switch_mode', $event);
extract($event->get_data_filtered($vars));
extract($phpbb_dispatcher->trigger_event('core.get_logs_switch_mode', $vars, $vars));
}
*/
@ -275,9 +313,7 @@ class phpbb_log implements phpbb_log_interface
if ($phpbb_dispatcher != null)
{
$vars = array('mode', 'count_logs', 'limit', 'offset', 'forum_id', 'topic_id', 'user_id', 'log_time', 'sort_by', 'keywords', 'profile_url', 'log_type', 'sql_additional');
$event = new phpbb_event_data(compact($vars));
$phpbb_dispatcher->dispatch('core.get_logs_after_get_type', $event);
extract($event->get_data_filtered($vars));
extract($phpbb_dispatcher->trigger_event('core.get_logs_after_get_type', $vars, $vars));
}
*/
@ -311,12 +347,12 @@ class phpbb_log implements phpbb_log_interface
// Return the user to the last page that is valid
while ($this->logs_offset >= $this->logs_total)
{
$this->logs_offset = ($this->logs_offset - $limit < 0) ? 0 : $this->logs_offset - $limit;
$this->logs_offset = max(0, $this->logs_offset - $limit);
}
}
$sql = "SELECT l.*, u.username, u.username_clean, u.user_colour
FROM " . LOG_TABLE . " l, " . USERS_TABLE . " u
$sql = 'SELECT l.*, u.username, u.username_clean, u.user_colour
FROM ' . LOG_TABLE . ' l, ' . USERS_TABLE . " u
WHERE l.log_type = $log_type
AND u.user_id = l.user_id
" . (($log_time) ? "AND l.log_time >= $log_time" : '') . "
@ -366,9 +402,7 @@ class phpbb_log implements phpbb_log_interface
if ($phpbb_dispatcher != null)
{
$vars = array('log_entry_data', 'row');
$event = new phpbb_event_data(compact($vars));
$phpbb_dispatcher->dispatch('core.get_logs_entry_data', $event);
extract($event->get_data_filtered($vars));
extract($phpbb_dispatcher->trigger_event('core.get_logs_entry_data', $vars, $vars));
}
*/
@ -377,7 +411,7 @@ class phpbb_log implements phpbb_log_interface
if (!empty($row['log_data']))
{
$log_data_ary = @unserialize($row['log_data']);
$log_data_ary = ($log_data_ary === false) ? array() : $log_data_ary;
$log_data_ary = ($log_data_ary !== false) ? $log_data_ary : array();
if (isset($user->lang[$row['log_operation']]))
{
@ -421,9 +455,7 @@ class phpbb_log implements phpbb_log_interface
if ($phpbb_dispatcher != null)
{
$vars = array('log', 'topic_id_list', 'reportee_id_list');
$event = new phpbb_event_data(compact($vars));
$phpbb_dispatcher->dispatch('core.get_logs_additional_data', $event);
extract($event->get_data_filtered($vars));
extract($phpbb_dispatcher->trigger_event('core.get_logs_additional_data', $vars, $vars));
}
*/
@ -498,7 +530,8 @@ class phpbb_log implements phpbb_log_interface
{
$sql_keywords .= $db->sql_in_set('l.log_operation', $operations) . ' OR ';
}
$sql_keywords .= 'LOWER(l.log_data) ' . implode(' OR LOWER(l.log_data) ', $keywords) . ')';
$sql_lower = $db->sql_lower_text('l.log_data');
$sql_keywords .= " $sql_lower " . implode(" OR $sql_lower ", $keywords) . ')';
}
return $sql_keywords;