1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-04-20 15:53:14 +02:00

Merge pull request #2433 from Nicofuma/ticket/10899

[ticket/10899] Using Delete All in log viewer with keyword search

* Nicofuma/ticket/10899:
  [ticket/10899] Update doc block
  [ticket/10899] Use isset($field_value['IN'])
  [ticket/10899] Add event core.delete_log
  [ticket/10899] Remove trailing ;
  [ticket/10899] Fix typo in the class name
  [ticket/10899] Add unit tests
  [ticket/10899] Get $phpbb_log from the container
  [ticket/10899] Remove extra ';'
  [ticket/10899] Typo
  [ticket/10899] Refactoring in \phpbb\log\log_interface
  [ticket/10899] Using Delete All in log viewer with keyword search
This commit is contained in:
Joas Schilling 2014-05-30 23:13:00 +02:00
commit 40218ba5e6
7 changed files with 445 additions and 26 deletions

View File

@ -54,7 +54,7 @@ class acp_logs
{
if (confirm_box(true))
{
$where_sql = '';
$conditions = array();
if ($deletemark && sizeof($marked))
{
@ -63,19 +63,25 @@ class acp_logs
{
$sql_in[] = $mark;
}
$where_sql = ' AND ' . $db->sql_in_set('log_id', $sql_in);
$conditions['log_id'] = $sql_in;
unset($sql_in);
}
if ($where_sql || $deleteall)
if ($deleteall)
{
$sql = 'DELETE FROM ' . LOG_TABLE . "
WHERE log_type = {$this->log_type}
$where_sql";
$db->sql_query($sql);
if ($sort_days)
{
$conditions['log_time'] = array('>=', time() - ($sort_days * 86400));
}
add_log('admin', 'LOG_CLEAR_' . strtoupper($mode));
$keywords = utf8_normalize_nfc(request_var('keywords', '', true));
$conditions['keywords'] = $keywords;
}
$conditions['log_type'] = $this->log_type;
$phpbb_log = $phpbb_container->get('log');
$phpbb_log->delete($mode, $conditions);
}
else
{

View File

@ -36,7 +36,7 @@ class mcp_logs
function main($id, $mode)
{
global $auth, $db, $user, $template;
global $config, $phpbb_root_path, $phpEx, $phpbb_container;
global $config, $phpbb_root_path, $phpEx, $phpbb_container, $phpbb_log;
$user->add_lang('acp/common');
@ -114,27 +114,35 @@ class mcp_logs
{
if ($deletemark && sizeof($marked))
{
$sql = 'DELETE FROM ' . LOG_TABLE . '
WHERE log_type = ' . LOG_MOD . '
AND ' . $db->sql_in_set('forum_id', $forum_list) . '
AND ' . $db->sql_in_set('log_id', $marked);
$db->sql_query($sql);
$conditions = array(
'log_type' => LOG_MOD,
'forum_id' => $forum_list,
'log_id' => $marked,
);
add_log('admin', 'LOG_CLEAR_MOD');
$phpbb_log->delete('mod', $conditions);
}
else if ($deleteall)
{
$sql = 'DELETE FROM ' . LOG_TABLE . '
WHERE log_type = ' . LOG_MOD . '
AND ' . $db->sql_in_set('forum_id', $forum_list);
$keywords = utf8_normalize_nfc(request_var('keywords', '', true));
$conditions = array(
'log_type' => LOG_MOD,
'forum_id' => $forum_list,
'keywords' => $keywords,
);
if ($sort_days)
{
$conditions['log_time'] = array('>=', time() - ($sort_days * 86400));
}
if ($mode == 'topic_logs')
{
$sql .= ' AND topic_id = ' . $topic_id;
$conditions['topic_logs'] = $topic_id;
}
$db->sql_query($sql);
add_log('admin', 'LOG_CLEAR_MOD');
$phpbb_log->delete('mod', $conditions);
}
}
else

View File

@ -331,6 +331,98 @@ class log implements \phpbb\log\log_interface
return $this->db->sql_nextid();
}
/**
* {@inheritDoc}
*/
public function delete($mode, $conditions = array())
{
switch ($mode)
{
case 'admin':
$log_type = LOG_ADMIN;
break;
case 'mod':
$log_type = LOG_MOD;
break;
case 'user':
$log_type = LOG_USERS;
break;
case 'users':
$log_type = LOG_USERS;
break;
case 'critical':
$log_type = LOG_CRITICAL;
break;
default:
$log_type = false;
}
/**
* Allows to modify log data before we delete it from the database
*
* NOTE: if sql_ary does not contain a log_type value, the entry will
* not be deleted in the database. So ensure to set it, if needed.
*
* @event core.delete_log
* @var string mode Mode of the entry we log
* @var string log_type Type ID of the log (should be different than false)
* @var array conditions An array of conditions, 3 different forms are accepted
* 1) <key> => <value> transformed into 'AND <key> = <value>' (value should be an integer)
* 2) <key> => array(<operator>, <value>) transformed into 'AND <key> <operator> <value>' (values can't be an array)
* 3) <key> => array('IN' => array(<values>)) transformed into 'AND <key> IN <values>'
* 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.
* @since 3.1.0-b4
*/
$vars = array(
'mode',
'log_type',
'conditions',
);
extract($this->dispatcher->trigger_event('core.delete_log', compact($vars)));
if ($log_type === false)
{
return;
}
$sql_where = 'WHERE log_type = ' . $log_type;
foreach ($conditions as $field => $field_value)
{
$sql_where .= ' AND ';
if ($field == 'keywords')
{
$sql_where .= $this->generate_sql_keyword($field_value, '', '');
}
else
{
if (is_array($field_value) && sizeof($field_value) == 2 && !is_array($field_value[1]))
{
$sql_where .= $field . ' ' . $field_value[0] . ' ' . $field_value[1];
}
else if (is_array($field_value) && isset($field_value['IN']) && is_array($field_value['IN']))
{
$sql_where .= $this->db->sql_in_set($field, $field_value['IN']);
}
else
{
$sql_where .= $field . ' = ' . $field_value;
}
}
}
$sql = 'DELETE FROM ' . LOG_TABLE . "
$sql_where";
$this->db->sql_query($sql);
$this->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_CLEAR_' . strtoupper($mode));
}
/**
* Grab the logs from the database
*
@ -638,11 +730,13 @@ class log implements \phpbb\log\log_interface
/**
* Generates a sql condition for the specified keywords
*
* @param string $keywords The keywords the user specified to search for
* @param string $keywords The keywords the user specified to search for
* @param string $table_alias The alias of the logs' table ('l.' by default)
* @param string $statement_operator The operator used to prefix the statement ('AND' by default)
*
* @return string Returns the SQL condition searching for the keywords
*/
protected function generate_sql_keyword($keywords)
protected function generate_sql_keyword($keywords, $table_alias = 'l.', $statement_operator = 'AND')
{
// Use no preg_quote for $keywords because this would lead to sole
// backslashes being added. We also use an OR connection here for
@ -687,12 +781,12 @@ class log implements \phpbb\log\log_interface
}
}
$sql_keywords = 'AND (';
$sql_keywords = $statement_operator . ' (';
if (!empty($operations))
{
$sql_keywords .= $this->db->sql_in_set('l.log_operation', $operations) . ' OR ';
$sql_keywords .= $this->db->sql_in_set($table_alias . 'log_operation', $operations) . ' OR ';
}
$sql_lower = $this->db->sql_lower_text('l.log_data');
$sql_lower = $this->db->sql_lower_text($table_alias . 'log_data');
$sql_keywords .= " $sql_lower " . implode(" OR $sql_lower ", $keywords) . ')';
}

View File

@ -68,6 +68,18 @@ interface log_interface
*/
public function add($mode, $user_id, $log_ip, $log_operation, $log_time = false, $additional_data = array());
/**
* Delete entries in the logs
*
* @param string $mode The mode defines which log_type is used and from which log the entries are deleted
* @param array $conditions An array of conditions, 3 different forms are accepted
* 1) <key> => <value> transformed into 'AND <key> = <value>' (value should be an integer)
* 2) <key> => array(<operator>, <value>) transformed into 'AND <key> <operator> <value>' (values can't be an array)
* 3) <key> => array('IN' => array(<values>)) transformed into 'AND <key> IN <values>'
* 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.
*/
public function delete($mode, $conditions = array());
/**
* Grab the logs from the database
*

View File

@ -48,6 +48,13 @@ class null implements log_interface
return false;
}
/**
* {@inheritdoc}
*/
public function delete($mode, $conditions = array())
{
}
/**
* {@inheritdoc}
*/

60
tests/log/delete_test.php Normal file
View File

@ -0,0 +1,60 @@
<?php
/**
*
* @package testing
* @copyright (c) 2012 phpBB Group
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/
require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php';
require_once dirname(__FILE__) . '/../../phpBB/includes/functions_content.php';
require_once dirname(__FILE__) . '/../../phpBB/includes/utf/utf_tools.php';
class phpbb_log_delete_test extends phpbb_database_test_case
{
public function getDataSet()
{
return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/delete_log.xml');
}
public function test_log_delete()
{
global $phpbb_root_path, $phpEx, $db, $phpbb_dispatcher, $auth;
$db = $this->new_dbal();
$cache = new phpbb_mock_cache;
$phpbb_dispatcher = new phpbb_mock_event_dispatcher();
$user = $this->getMock('\phpbb\user');
$user->data['user_id'] = 1;
$auth = $this->getMock('\phpbb\auth\auth');
$log = new \phpbb\log\log($db, $user, $auth, $phpbb_dispatcher, $phpbb_root_path, 'adm/', $phpEx, LOG_TABLE);
// Delete all admin logs
$this->assertCount(2, $log->get_logs('admin'));
$log->delete('admin');
// One entry is added to the admin log when the logs are purged
$this->assertCount(1, $log->get_logs('admin'));
// Delete with keyword
$this->assertCount(1, $log->get_logs('mod', false, 0, 0, 0, 0, 0, 0, 'l.log_time DESC', 'guest'));
$log->delete('mod', array('keywords' => 'guest'));
$this->assertEmpty($log->get_logs('mod', false, 0, 0, 0, 0, 0, 0, 'l.log_time DESC', 'guest'));
// Delete with simples conditions
$this->assertCount(3, $log->get_logs('mod', false, 0, 0, 12, 0, 1, 0, 'l.log_time DESC'));
$log->delete('mod', array('forum_id' => 12, 'user_id' => 1));
$this->assertEmpty($log->get_logs('mod', false, 0, 0, 12, 0, 1, 0, 'l.log_time DESC'));
// Delete with IN condition
$this->assertCount(2, $log->get_logs('mod', false, 0, 0, array(13, 14), 0, 0, 0, 'l.log_time DESC'));
$log->delete('mod', array('forum_id' => array('IN' => array(14, 13))));
$this->assertEmpty($log->get_logs('mod', false, 0, 0, array(13, 14), 0, 0, 0, 'l.log_time DESC'));
// Delete with a custom condition (ie: WHERE x >= 10)
$this->assertCount(3, $log->get_logs('critical', false, 0, 0, 0, 0, 0, 0, 'l.log_time DESC'));
$log->delete('critical', array('user_id' => array('>', 1)));
$this->assertCount(1, $log->get_logs('critical', false, 0, 0, 0, 0, 0, 0, 'l.log_time DESC'));
}
}

View File

@ -0,0 +1,232 @@
<?xml version="1.0" encoding="UTF-8" ?>
<dataset>
<table name="phpbb_log">
<column>log_id</column>
<column>log_type</column>
<column>user_id</column>
<column>forum_id</column>
<column>topic_id</column>
<column>reportee_id</column>
<column>log_ip</column>
<column>log_time</column>
<column>log_operation</column>
<column>log_data</column>
<row>
<value>1</value>
<value>0</value>
<value>1</value>
<value>0</value>
<value>0</value>
<value>0</value>
<value>127.0.0.1</value>
<value>1</value>
<value>LOG_INSTALL_INSTALLED</value>
<value>a:1:{i:0;s:9:"3.1.0-dev";}</value>
</row>
<row>
<value>2</value>
<value>0</value>
<value>1</value>
<value>0</value>
<value>0</value>
<value>0</value>
<value>127.0.0.1</value>
<value>1</value>
<value>LOG_KEY_NOT_EXISTS</value>
<value>a:1:{i:0;s:15:"additional_data";}</value>
</row>
<row>
<value>3</value>
<value>2</value>
<value>1</value>
<value>0</value>
<value>0</value>
<value>0</value>
<value>127.0.0.1</value>
<value>1</value>
<value>LOG_CRITICAL</value>
<value>a:1:{i:0;s:13:"critical data";}</value>
</row>
<row>
<value>4</value>
<value>1</value>
<value>1</value>
<value>12</value>
<value>34</value>
<value>0</value>
<value>127.0.0.1</value>
<value>1</value>
<value>LOG_MOD</value>
<value></value>
</row>
<row>
<value>5</value>
<value>1</value>
<value>1</value>
<value>12</value>
<value>45</value>
<value>0</value>
<value>127.0.0.1</value>
<value>1</value>
<value>LOG_MOD</value>
<value></value>
</row>
<row>
<value>6</value>
<value>1</value>
<value>1</value>
<value>23</value>
<value>56</value>
<value>0</value>
<value>127.0.0.1</value>
<value>1</value>
<value>LOG_MOD</value>
<value></value>
</row>
<row>
<value>7</value>
<value>1</value>
<value>1</value>
<value>12</value>
<value>45</value>
<value>0</value>
<value>127.0.0.1</value>
<value>1</value>
<value>LOG_MOD2</value>
<value></value>
</row>
<row>
<value>8</value>
<value>3</value>
<value>1</value>
<value>0</value>
<value>0</value>
<value>2</value>
<value>127.0.0.1</value>
<value>1</value>
<value>LOG_USER</value>
<value>a:1:{i:0;s:5:"admin";}</value>
</row>
<row>
<value>9</value>
<value>3</value>
<value>1</value>
<value>0</value>
<value>0</value>
<value>1</value>
<value>127.0.0.1</value>
<value>1</value>
<value>LOG_USER</value>
<value>a:1:{i:0;s:5:"guest";}</value>
</row>
<row>
<value>10</value>
<value>3</value>
<value>1</value>
<value>0</value>
<value>0</value>
<value>0</value>
<value>127.0.0.1</value>
<value>1</value>
<value>LOG_SINGULAR_PLURAL</value>
<value>a:1:{i:0;i:2;}</value>
</row>
<row>
<value>11</value>
<value>1</value>
<value>1</value>
<value>15</value>
<value>3</value>
<value>0</value>
<value>127.0.0.1</value>
<value>1</value>
<value>LOG_MOD3</value>
<value>a:1:{i:0;s:5:"guest";}</value>
</row>
<row>
<value>12</value>
<value>1</value>
<value>1</value>
<value>13</value>
<value>0</value>
<value>0</value>
<value>127.0.0.1</value>
<value>1</value>
<value></value>
<value></value>
</row>
<row>
<value>13</value>
<value>1</value>
<value>1</value>
<value>14</value>
<value>0</value>
<value>0</value>
<value>127.0.0.1</value>
<value>1</value>
<value></value>
<value></value>
</row>
<row>
<value>14</value>
<value>2</value>
<value>2</value>
<value>0</value>
<value>0</value>
<value>0</value>
<value>127.0.0.1</value>
<value>1</value>
<value></value>
<value></value>
</row>
<row>
<value>15</value>
<value>2</value>
<value>2</value>
<value>0</value>
<value>0</value>
<value>0</value>
<value>127.0.0.1</value>
<value>1</value>
<value></value>
<value></value>
</row>
</table>
<table name="phpbb_users">
<column>user_id</column>
<column>username</column>
<column>username_clean</column>
<column>user_permissions</column>
<column>user_sig</column>
<row>
<value>1</value>
<value>Anonymous</value>
<value>Anonymous</value>
<value></value>
<value></value>
</row>
<row>
<value>2</value>
<value>admin</value>
<value>admin</value>
<value></value>
<value></value>
</row>
</table>
<table name="phpbb_topics">
<column>topic_id</column>
<column>forum_id</column>
<row>
<value>34</value>
<value>12</value>
</row>
<row>
<value>45</value>
<value>12</value>
</row>
<row>
<value>56</value>
<value>23</value>
</row>
</table>
</dataset>