From c5a4ad3d31047f9580b19b3401ef523b0fd53733 Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Sat, 10 May 2014 16:58:11 +0200 Subject: [ticket/10899] Using Delete All in log viewer with keyword search https://tracker.phpbb.com/browse/PHPBB3-10899 PHPBB3-10899 --- phpBB/phpbb/log/log.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'phpBB/phpbb/log/log.php') diff --git a/phpBB/phpbb/log/log.php b/phpBB/phpbb/log/log.php index e4c5ce47d9..e3a0fa0261 100644 --- a/phpBB/phpbb/log/log.php +++ b/phpBB/phpbb/log/log.php @@ -636,11 +636,12 @@ 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) * * @return string Returns the SQL condition searching for the keywords */ - protected function generate_sql_keyword($keywords) + public function generate_sql_keyword($keywords, $table_alias = 'l.') { // Use no preg_quote for $keywords because this would lead to sole // backslashes being added. We also use an OR connection here for @@ -688,9 +689,9 @@ class log implements \phpbb\log\log_interface $sql_keywords = 'AND ('; 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) . ')'; } -- cgit v1.2.1 From c6d7875b9b76a931e27e8dbf742ad7af25fe19cf Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Sat, 10 May 2014 18:24:07 +0200 Subject: [ticket/10899] Refactoring in \phpbb\log\log_interface PHPBB3-10899 --- phpBB/phpbb/log/log.php | 57 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 53 insertions(+), 4 deletions(-) (limited to 'phpBB/phpbb/log/log.php') diff --git a/phpBB/phpbb/log/log.php b/phpBB/phpbb/log/log.php index e3a0fa0261..a0d399ccee 100644 --- a/phpBB/phpbb/log/log.php +++ b/phpBB/phpbb/log/log.php @@ -329,6 +329,54 @@ class log implements \phpbb\log\log_interface return $this->db->sql_nextid(); } + /** + * {@inheritDoc} + */ + public function delete($mode, $conditions = array()) + { + $sql_where = ''; + $started = false; + foreach ($conditions as $field => $field_value) + { + if ($started) + { + $sql_where .= ' AND '; + } + else + { + $sql_where = 'WHERE '; + $started = true; + } + + if ($field == 'keywords') + { + $sql_where .= $this->generate_sql_keyword($field_value, '', ''); + } + else + { + if (is_array($field_value) && sizeof($field_value) == 2) + { + $sql_where .= $field . ' ' . $field_value[0] . ' ' . $field_value[1]; + } + else if (is_array($field_value) && sizeof($field_value) > 2) + { + $sql_where .= $this->db->sql_in_set($field, $field_value);; + } + 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 * @@ -636,12 +684,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 $table_alias The alias of the logs' table ('l.' by default) + * @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 */ - public function generate_sql_keyword($keywords, $table_alias = 'l.') + 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 @@ -686,7 +735,7 @@ class log implements \phpbb\log\log_interface } } - $sql_keywords = 'AND ('; + $sql_keywords = $statement_operator . ' ('; if (!empty($operations)) { $sql_keywords .= $this->db->sql_in_set($table_alias . 'log_operation', $operations) . ' OR '; -- cgit v1.2.1 From 164f52c3f30d6afd85e1f25cea7ebc75f327d12d Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Sat, 10 May 2014 19:04:37 +0200 Subject: [ticket/10899] Remove extra ';' PHPBB3-10899 --- phpBB/phpbb/log/log.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'phpBB/phpbb/log/log.php') diff --git a/phpBB/phpbb/log/log.php b/phpBB/phpbb/log/log.php index a0d399ccee..4b1a8e40ad 100644 --- a/phpBB/phpbb/log/log.php +++ b/phpBB/phpbb/log/log.php @@ -360,7 +360,7 @@ class log implements \phpbb\log\log_interface } else if (is_array($field_value) && sizeof($field_value) > 2) { - $sql_where .= $this->db->sql_in_set($field, $field_value);; + $sql_where .= $this->db->sql_in_set($field, $field_value); } else { -- cgit v1.2.1 From 05e76e55e149936d112ccb3441f88bbd3ce235fb Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Wed, 28 May 2014 00:54:22 +0200 Subject: [ticket/10899] Add unit tests PHPBB3-10899 --- phpBB/phpbb/log/log.php | 49 +++++++++++++++++++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 14 deletions(-) (limited to 'phpBB/phpbb/log/log.php') diff --git a/phpBB/phpbb/log/log.php b/phpBB/phpbb/log/log.php index 4b1a8e40ad..8b51ed7758 100644 --- a/phpBB/phpbb/log/log.php +++ b/phpBB/phpbb/log/log.php @@ -334,19 +334,41 @@ class log implements \phpbb\log\log_interface */ public function delete($mode, $conditions = array()) { - $sql_where = ''; - $started = false; + 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; + } + + if ($log_type === false) + { + return; + } + + $sql_where = 'WHERE log_type = ' . $log_type; foreach ($conditions as $field => $field_value) { - if ($started) - { - $sql_where .= ' AND '; - } - else - { - $sql_where = 'WHERE '; - $started = true; - } + $sql_where .= ' AND '; if ($field == 'keywords') { @@ -354,11 +376,11 @@ class log implements \phpbb\log\log_interface } else { - if (is_array($field_value) && sizeof($field_value) == 2) + if (is_array($field_value) && sizeof($field_value) == 2 && is_string($field_value[0])) { $sql_where .= $field . ' ' . $field_value[0] . ' ' . $field_value[1]; } - else if (is_array($field_value) && sizeof($field_value) > 2) + else if (is_array($field_value)) { $sql_where .= $this->db->sql_in_set($field, $field_value); } @@ -366,7 +388,6 @@ class log implements \phpbb\log\log_interface { $sql_where .= $field . ' = ' . $field_value; } - } } -- cgit v1.2.1 From d3f4dbedde157c316fe5a675de923e9b1e73d6f2 Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Wed, 28 May 2014 11:50:13 +0200 Subject: [ticket/10899] Remove trailing ; PHPBB3-10899 --- phpBB/phpbb/log/log.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'phpBB/phpbb/log/log.php') diff --git a/phpBB/phpbb/log/log.php b/phpBB/phpbb/log/log.php index 8b51ed7758..206a665283 100644 --- a/phpBB/phpbb/log/log.php +++ b/phpBB/phpbb/log/log.php @@ -353,7 +353,7 @@ class log implements \phpbb\log\log_interface break; case 'critical': - $log_type = LOG_CRITICAL;; + $log_type = LOG_CRITICAL; break; default: -- cgit v1.2.1 From 59f39273d42bb863ffa0d46e017919bdbe39d8d5 Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Wed, 28 May 2014 23:20:50 +0200 Subject: [ticket/10899] Add event core.delete_log PHPBB3-10899 --- phpBB/phpbb/log/log.php | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) (limited to 'phpBB/phpbb/log/log.php') diff --git a/phpBB/phpbb/log/log.php b/phpBB/phpbb/log/log.php index 206a665283..6c0c160b58 100644 --- a/phpBB/phpbb/log/log.php +++ b/phpBB/phpbb/log/log.php @@ -360,6 +360,29 @@ class log implements \phpbb\log\log_interface $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.add_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) => transformed into 'AND = ' (value should be an integer) + * 2) => array(, ) transformed into 'AND ' (values can't be an array) + * 3) => array('IN' => array()) transformed into 'AND IN ' + * 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; @@ -376,13 +399,13 @@ class log implements \phpbb\log\log_interface } else { - if (is_array($field_value) && sizeof($field_value) == 2 && is_string($field_value[0])) + 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)) + else if (is_array($field_value) && sizeof($field_value) == 1 && is_array($field_value['IN'])) { - $sql_where .= $this->db->sql_in_set($field, $field_value); + $sql_where .= $this->db->sql_in_set($field, $field_value['IN']); } else { -- cgit v1.2.1 From ff5fd3442d9e985e0b3c48f5a13254448d27d929 Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Wed, 28 May 2014 23:30:47 +0200 Subject: [ticket/10899] Use isset($field_value['IN']) PHPBB3-10899 --- phpBB/phpbb/log/log.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'phpBB/phpbb/log/log.php') diff --git a/phpBB/phpbb/log/log.php b/phpBB/phpbb/log/log.php index 6c0c160b58..879a1b49e8 100644 --- a/phpBB/phpbb/log/log.php +++ b/phpBB/phpbb/log/log.php @@ -403,7 +403,7 @@ class log implements \phpbb\log\log_interface { $sql_where .= $field . ' ' . $field_value[0] . ' ' . $field_value[1]; } - else if (is_array($field_value) && sizeof($field_value) == 1 && is_array($field_value['IN'])) + 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']); } -- cgit v1.2.1 From 4b3bba6693d96fe5ce918a635af8ff5ec7a8c1f0 Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Thu, 29 May 2014 02:30:15 +0200 Subject: [ticket/10899] Update doc block PHPBB3-10899 --- phpBB/phpbb/log/log.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'phpBB/phpbb/log/log.php') diff --git a/phpBB/phpbb/log/log.php b/phpBB/phpbb/log/log.php index 879a1b49e8..e7875e1a12 100644 --- a/phpBB/phpbb/log/log.php +++ b/phpBB/phpbb/log/log.php @@ -366,7 +366,7 @@ class log implements \phpbb\log\log_interface * 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.add_log + * @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 -- cgit v1.2.1