From 2edf7056f0ebb797dd82896eb08ad6ccb8906a45 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Tue, 28 Mar 2017 22:10:03 +0200 Subject: [PATCH 1/4] [ticket/8116] Add pagination for IP tables on post info Diff from previous patch by nickvergessen. PHPBB3-8116 --- phpBB/includes/mcp/mcp_post.php | 128 +++++++++++++++++- phpBB/styles/prosilver/template/mcp_post.html | 44 +++++- 2 files changed, 164 insertions(+), 8 deletions(-) diff --git a/phpBB/includes/mcp/mcp_post.php b/phpBB/includes/mcp/mcp_post.php index 2dcfcd608b..88f6381023 100644 --- a/phpBB/includes/mcp/mcp_post.php +++ b/phpBB/includes/mcp/mcp_post.php @@ -24,10 +24,13 @@ if (!defined('IN_PHPBB')) */ function mcp_post_details($id, $mode, $action) { - global $phpEx, $phpbb_root_path, $config; - global $template, $db, $user, $auth, $cache; + global $phpEx, $phpbb_root_path, $config, $request; + global $template, $db, $user, $auth, $cache, $phpbb_container; global $phpbb_dispatcher; + /** @var \phpbb\pagination $pagination */ + $pagination = $phpbb_container->get('pagination'); + $user->add_lang('posting'); $post_id = request_var('p', 0); @@ -355,7 +358,8 @@ function mcp_post_details($id, $mode, $action) // Get IP if ($auth->acl_get('m_info', $post_info['forum_id'])) { - $rdns_ip_num = request_var('rdns', ''); + $rdns_ip_num = $request->variable('rdns', ''); + $start_users = $request->variable('start_users', 0); if ($rdns_ip_num != 'all') { @@ -364,16 +368,26 @@ function mcp_post_details($id, $mode, $action) ); } + $num_users = false; + if ($start_users) + { + $num_users = phpbb_get_num_posters_for_ip($db, $post_info['poster_ip']); + $start_users = $pagination->validate_start($start_users, $config['posts_per_page'], $num_users); + } + // Get other users who've posted under this IP $sql = 'SELECT poster_id, COUNT(poster_id) as postings FROM ' . POSTS_TABLE . " WHERE poster_ip = '" . $db->sql_escape($post_info['poster_ip']) . "' GROUP BY poster_id - ORDER BY postings DESC"; - $result = $db->sql_query($sql); + ORDER BY postings DESC, poster_id ASC"; + $result = $db->sql_query_limit($sql, $config['posts_per_page'], $start_users); + $users = 0; while ($row = $db->sql_fetchrow($result)) { + $users++; + // Fill the user select list with users who have posted under this IP if ($row['poster_id'] != $post_info['poster_id']) { @@ -382,6 +396,23 @@ function mcp_post_details($id, $mode, $action) } $db->sql_freeresult($result); + if ($users == $config['posts_per_page'] || $start_users) + { + if ($num_users === false) + { + $num_users = phpbb_get_num_posters_for_ip($db, $post_info['poster_ip']); + } + + $pagination->generate_template_pagination( + $url . '&i=main&mode=post_details', + 'pagination', + 'start_users', + $num_users, + $config['posts_per_page'], + $start_users + ); + } + if (sizeof($users_ary)) { // Get the usernames @@ -415,16 +446,26 @@ function mcp_post_details($id, $mode, $action) // A compound index on poster_id, poster_ip (posts table) would help speed up this query a lot, // but the extra size is only valuable if there are persons having more than a thousands posts. // This is better left to the really really big forums. + $start_ips = $request->variable('start_ips', 0); + + $num_ips = false; + if ($start_ips) + { + $num_ips = phpbb_get_num_ips_for_poster($db, $post_info['poster_id']); + $start_ips = $pagination->validate_start($start_ips, $config['posts_per_page'], $num_ips); + } $sql = 'SELECT poster_ip, COUNT(poster_ip) AS postings FROM ' . POSTS_TABLE . ' WHERE poster_id = ' . $post_info['poster_id'] . " GROUP BY poster_ip - ORDER BY postings DESC"; - $result = $db->sql_query($sql); + ORDER BY postings DESC, poster_ip ASC"; + $result = $db->sql_query_limit($sql, $config['posts_per_page'], $start_ips); + $ips = 0; while ($row = $db->sql_fetchrow($result)) { + $ips++; $hostname = (($rdns_ip_num == $row['poster_ip'] || $rdns_ip_num == 'all') && $row['poster_ip']) ? @gethostbyaddr($row['poster_ip']) : ''; $template->assign_block_vars('iprow', array( @@ -439,6 +480,23 @@ function mcp_post_details($id, $mode, $action) } $db->sql_freeresult($result); + if ($ips == $config['posts_per_page'] || $start_ips) + { + if ($num_ips === false) + { + $num_ips = phpbb_get_num_ips_for_poster($db, $post_info['poster_id']); + } + + $pagination->generate_template_pagination( + $url . '&i=main&mode=post_details', + 'pagination_ips', + 'start_ips', + $num_ips, + $config['posts_per_page'], + $start_ips + ); + } + $user_select = ''; if (sizeof($usernames_ary)) @@ -456,6 +514,62 @@ function mcp_post_details($id, $mode, $action) } +/** + * Get the number of posters for a given ip + * + * @param \phpbb\db\driver\driver_interface $db + * @param string $poster_ip + * @return int + */ +function phpbb_get_num_posters_for_ip(\phpbb\db\driver\driver_interface $db, $poster_ip) +{ + if ($db->get_sql_layer() == 'sqlite' || $db->get_sql_layer() == 'sqlite3') + { + $sql = 'SELECT COUNT(poster_id) as num_users + FROM (SELECT DISTINCT poster_id'; + } + else + { + $sql = 'SELECT COUNT(DISTINCT poster_id) as num_users'; + } + + $sql .= ' FROM ' . POSTS_TABLE . " + WHERE poster_ip = '" . $db->sql_escape($poster_ip) . "'"; + $result = $db->sql_query($sql); + $num_users = (int) $db->sql_fetchfield('num_users'); + $db->sql_freeresult($result); + + return $num_users; +} + +/** + * Get the number of ips for a given poster + * + * @param \phpbb\db\driver\driver_interface $db + * @param int $poster_id + * @return int + */ +function phpbb_get_num_ips_for_poster(\phpbb\db\driver\driver_interface $db, $poster_id) +{ + if ($db->get_sql_layer() == 'sqlite' || $db->get_sql_layer() == 'sqlite3') + { + $sql = 'SELECT COUNT(poster_ip) as num_ips + FROM (SELECT DISTINCT poster_ip'; + } + else + { + $sql = 'SELECT COUNT(DISTINCT poster_ip) as num_ips'; + } + + $sql .= ' FROM ' . POSTS_TABLE . ' + WHERE poster_id = ' . (int) $poster_id; + $result = $db->sql_query($sql); + $num_ips = (int) $db->sql_fetchfield('num_ips'); + $db->sql_freeresult($result); + + return $num_ips; +} + /** * Change a post's poster */ diff --git a/phpBB/styles/prosilver/template/mcp_post.html b/phpBB/styles/prosilver/template/mcp_post.html index e5777d206a..fc4a5b0a9d 100644 --- a/phpBB/styles/prosilver/template/mcp_post.html +++ b/phpBB/styles/prosilver/template/mcp_post.html @@ -294,6 +294,28 @@ + + + + +
+
@@ -315,7 +337,27 @@
-

{L_LOOKUP_ALL}

+ + +
From e6a0e4caed0262507926bf70c86533efd2512e26 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Wed, 29 Mar 2017 19:53:58 +0200 Subject: [PATCH 2/4] [ticket/8116] Improve code and add tests for helper functions PHPBB3-8116 --- phpBB/includes/mcp/mcp_post.php | 54 ++++++++---------------- tests/mcp/fixtures/post_ip.xml | 73 +++++++++++++++++++++++++++++++++ tests/mcp/post_ip_test.php | 67 ++++++++++++++++++++++++++++++ 3 files changed, 158 insertions(+), 36 deletions(-) create mode 100644 tests/mcp/fixtures/post_ip.xml create mode 100644 tests/mcp/post_ip_test.php diff --git a/phpBB/includes/mcp/mcp_post.php b/phpBB/includes/mcp/mcp_post.php index 88f6381023..dfc43d6cac 100644 --- a/phpBB/includes/mcp/mcp_post.php +++ b/phpBB/includes/mcp/mcp_post.php @@ -28,9 +28,6 @@ function mcp_post_details($id, $mode, $action) global $template, $db, $user, $auth, $cache, $phpbb_container; global $phpbb_dispatcher; - /** @var \phpbb\pagination $pagination */ - $pagination = $phpbb_container->get('pagination'); - $user->add_lang('posting'); $post_id = request_var('p', 0); @@ -358,6 +355,9 @@ function mcp_post_details($id, $mode, $action) // Get IP if ($auth->acl_get('m_info', $post_info['forum_id'])) { + /** @var \phpbb\pagination $pagination */ + $pagination = $phpbb_container->get('pagination'); + $rdns_ip_num = $request->variable('rdns', ''); $start_users = $request->variable('start_users', 0); @@ -383,10 +383,10 @@ function mcp_post_details($id, $mode, $action) ORDER BY postings DESC, poster_id ASC"; $result = $db->sql_query_limit($sql, $config['posts_per_page'], $start_users); - $users = 0; + $page_users = 0; while ($row = $db->sql_fetchrow($result)) { - $users++; + $page_users++; // Fill the user select list with users who have posted under this IP if ($row['poster_id'] != $post_info['poster_id']) @@ -396,7 +396,7 @@ function mcp_post_details($id, $mode, $action) } $db->sql_freeresult($result); - if ($users == $config['posts_per_page'] || $start_users) + if ($page_users == $config['posts_per_page'] || $start_users) { if ($num_users === false) { @@ -462,10 +462,10 @@ function mcp_post_details($id, $mode, $action) ORDER BY postings DESC, poster_ip ASC"; $result = $db->sql_query_limit($sql, $config['posts_per_page'], $start_ips); - $ips = 0; + $page_ips = 0; while ($row = $db->sql_fetchrow($result)) { - $ips++; + $page_ips++; $hostname = (($rdns_ip_num == $row['poster_ip'] || $rdns_ip_num == 'all') && $row['poster_ip']) ? @gethostbyaddr($row['poster_ip']) : ''; $template->assign_block_vars('iprow', array( @@ -480,7 +480,7 @@ function mcp_post_details($id, $mode, $action) } $db->sql_freeresult($result); - if ($ips == $config['posts_per_page'] || $start_ips) + if ($page_ips == $config['posts_per_page'] || $start_ips) { if ($num_ips === false) { @@ -517,23 +517,14 @@ function mcp_post_details($id, $mode, $action) /** * Get the number of posters for a given ip * - * @param \phpbb\db\driver\driver_interface $db - * @param string $poster_ip - * @return int + * @param \phpbb\db\driver\driver_interface $db DBAL interface + * @param string $poster_ip IP + * @return int Number of posters */ function phpbb_get_num_posters_for_ip(\phpbb\db\driver\driver_interface $db, $poster_ip) { - if ($db->get_sql_layer() == 'sqlite' || $db->get_sql_layer() == 'sqlite3') - { - $sql = 'SELECT COUNT(poster_id) as num_users - FROM (SELECT DISTINCT poster_id'; - } - else - { - $sql = 'SELECT COUNT(DISTINCT poster_id) as num_users'; - } - - $sql .= ' FROM ' . POSTS_TABLE . " + $sql = 'SELECT COUNT(DISTINCT poster_id) as num_users + FROM ' . POSTS_TABLE . " WHERE poster_ip = '" . $db->sql_escape($poster_ip) . "'"; $result = $db->sql_query($sql); $num_users = (int) $db->sql_fetchfield('num_users'); @@ -546,22 +537,13 @@ function phpbb_get_num_posters_for_ip(\phpbb\db\driver\driver_interface $db, $po * Get the number of ips for a given poster * * @param \phpbb\db\driver\driver_interface $db - * @param int $poster_id - * @return int + * @param int $poster_id Poster user ID + * @return int Number of IPs for given poster */ function phpbb_get_num_ips_for_poster(\phpbb\db\driver\driver_interface $db, $poster_id) { - if ($db->get_sql_layer() == 'sqlite' || $db->get_sql_layer() == 'sqlite3') - { - $sql = 'SELECT COUNT(poster_ip) as num_ips - FROM (SELECT DISTINCT poster_ip'; - } - else - { - $sql = 'SELECT COUNT(DISTINCT poster_ip) as num_ips'; - } - - $sql .= ' FROM ' . POSTS_TABLE . ' + $sql = 'SELECT COUNT(DISTINCT poster_ip) as num_ips + FROM ' . POSTS_TABLE . ' WHERE poster_id = ' . (int) $poster_id; $result = $db->sql_query($sql); $num_ips = (int) $db->sql_fetchfield('num_ips'); diff --git a/tests/mcp/fixtures/post_ip.xml b/tests/mcp/fixtures/post_ip.xml new file mode 100644 index 0000000000..fad2193396 --- /dev/null +++ b/tests/mcp/fixtures/post_ip.xml @@ -0,0 +1,73 @@ + + + + post_id + poster_id + post_edit_user + post_delete_user + post_username + topic_id + forum_id + post_visibility + post_time + post_text + post_reported + poster_ip + + 1 + 2 + 2 + 2 + + 1 + 1 + 1 + 1 + + 1 + 127.0.0.1 + + + 2 + 1 + 1 + 1 + Other + 2 + 2 + 1 + 1 + + 1 + 127.0.0.2 + + + 3 + 2 + 2 + 2 + + 3 + 3 + 1 + 1 + + 1 + 127.0.0.3 + + + 4 + 1 + 1 + 1 + Other + 4 + 4 + 1 + 1 + + 1 + 127.0.0.1 + +
+
diff --git a/tests/mcp/post_ip_test.php b/tests/mcp/post_ip_test.php new file mode 100644 index 0000000000..865b15bcb2 --- /dev/null +++ b/tests/mcp/post_ip_test.php @@ -0,0 +1,67 @@ + + * @license GNU General Public License, version 2 (GPL-2.0) + * + * For full copyright and license information, please see + * the docs/CREDITS.txt file. + * + */ + +require_once dirname(__FILE__) . '/../../phpBB/includes/mcp/mcp_post.php'; + +class phpbb_mcp_post_ip_test extends phpbb_database_test_case +{ + /** @var \phpbb\db\driver\driver_interface */ + protected $db; + + public function getDataSet() + { + return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/post_ip.xml'); + } + + protected function setUp() + { + parent::setUp(); + + $this->db = $this->new_dbal(); + } + + public function data_get_num_ips() + { + return array( + array(2, 1), + array(2, 2), + array(0, 3), + ); + } + + /** + * @dataProvider data_get_num_ips + */ + public function test_get_num_ips($expected, $poster_id) + { + $this->assertSame($expected, phpbb_get_num_ips_for_poster($this->db, $poster_id)); + } + + public function data_get_num_posters() + { + return array( + array(2, '127.0.0.1'), + array(1, '127.0.0.2'), + array(1, '127.0.0.3'), + array(0, '127.0.0.4'), + ); + } + + /** + * @dataProvider data_get_num_posters + */ + public function test_get_num_posters($expected, $ip) + { + $this->assertSame($expected, phpbb_get_num_posters_for_ip($this->db, $ip)); + } +} \ No newline at end of file From 771be9310de71978c81ef9ddf7b61fb849069ce2 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Tue, 18 Apr 2017 19:33:05 +0200 Subject: [PATCH 3/4] [ticket/8116] Correctly get other users who posted under IP PHPBB3-8116 --- phpBB/includes/mcp/mcp_post.php | 8 ++------ phpBB/styles/prosilver/template/mcp_post.html | 16 +--------------- 2 files changed, 3 insertions(+), 21 deletions(-) diff --git a/phpBB/includes/mcp/mcp_post.php b/phpBB/includes/mcp/mcp_post.php index dfc43d6cac..b5f2bf4f02 100644 --- a/phpBB/includes/mcp/mcp_post.php +++ b/phpBB/includes/mcp/mcp_post.php @@ -379,6 +379,7 @@ function mcp_post_details($id, $mode, $action) $sql = 'SELECT poster_id, COUNT(poster_id) as postings FROM ' . POSTS_TABLE . " WHERE poster_ip = '" . $db->sql_escape($post_info['poster_ip']) . "' + AND poster_id <> " . (int) $post_info['poster_id'] . " GROUP BY poster_id ORDER BY postings DESC, poster_id ASC"; $result = $db->sql_query_limit($sql, $config['posts_per_page'], $start_users); @@ -387,12 +388,7 @@ function mcp_post_details($id, $mode, $action) while ($row = $db->sql_fetchrow($result)) { $page_users++; - - // Fill the user select list with users who have posted under this IP - if ($row['poster_id'] != $post_info['poster_id']) - { - $users_ary[$row['poster_id']] = $row; - } + $users_ary[$row['poster_id']] = $row; } $db->sql_freeresult($result); diff --git a/phpBB/styles/prosilver/template/mcp_post.html b/phpBB/styles/prosilver/template/mcp_post.html index fc4a5b0a9d..5acdcef859 100644 --- a/phpBB/styles/prosilver/template/mcp_post.html +++ b/phpBB/styles/prosilver/template/mcp_post.html @@ -295,21 +295,7 @@ From e72f6d42366c6a0cbc128de406474192bc7cd8e2 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Wed, 19 Apr 2017 17:13:18 +0200 Subject: [PATCH 4/4] [ticket/8116] Add missing new line PHPBB3-8116 --- tests/mcp/post_ip_test.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/mcp/post_ip_test.php b/tests/mcp/post_ip_test.php index 865b15bcb2..72a9f62774 100644 --- a/tests/mcp/post_ip_test.php +++ b/tests/mcp/post_ip_test.php @@ -64,4 +64,4 @@ class phpbb_mcp_post_ip_test extends phpbb_database_test_case { $this->assertSame($expected, phpbb_get_num_posters_for_ip($this->db, $ip)); } -} \ No newline at end of file +}