From a02fa1170be6911cd4c85bc9dd88f216b1384f52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Calvo?= Date: Sun, 11 Feb 2018 11:33:04 +0100 Subject: [PATCH 01/34] [ticket/15342] Track storage files PHPBB3-15342 --- .../default/container/services_storage.yml | 3 + phpBB/config/default/container/tables.yml | 1 + phpBB/includes/constants.php | 1 + .../db/migration/data/v330/storage_track.php | 43 +++++ phpBB/phpbb/storage/storage.php | 156 +++++++++++++++++- tests/avatar/manager_test.php | 3 +- 6 files changed, 199 insertions(+), 8 deletions(-) create mode 100644 phpBB/phpbb/db/migration/data/v330/storage_track.php diff --git a/phpBB/config/default/container/services_storage.yml b/phpBB/config/default/container/services_storage.yml index 7216b4b5c4..4223bdb2bf 100644 --- a/phpBB/config/default/container/services_storage.yml +++ b/phpBB/config/default/container/services_storage.yml @@ -12,14 +12,17 @@ services: storage.avatar: class: phpbb\storage\storage arguments: + - '@dbal.conn' - '@storage.adapter.factory' - 'avatar' + - '%tables.storage%' tags: - { name: storage } storage.backup: class: phpbb\storage\storage arguments: + - '@dbal.conn' - '@storage.adapter.factory' - 'backup' tags: diff --git a/phpBB/config/default/container/tables.yml b/phpBB/config/default/container/tables.yml index 6adacca0d0..f3c2282de9 100644 --- a/phpBB/config/default/container/tables.yml +++ b/phpBB/config/default/container/tables.yml @@ -60,6 +60,7 @@ parameters: tables.sitelist: '%core.table_prefix%sitelist' tables.smilies: '%core.table_prefix%smilies' tables.sphinx: '%core.table_prefix%sphinx' + tables.storage: '%core.table_prefix%storage' tables.styles: '%core.table_prefix%styles' tables.styles_template: '%core.table_prefix%styles_template' tables.styles_template_data: '%core.table_prefix%styles_template_data' diff --git a/phpBB/includes/constants.php b/phpBB/includes/constants.php index ee8b642b9b..dcab741366 100644 --- a/phpBB/includes/constants.php +++ b/phpBB/includes/constants.php @@ -288,6 +288,7 @@ define('SESSIONS_KEYS_TABLE', $table_prefix . 'sessions_keys'); define('SITELIST_TABLE', $table_prefix . 'sitelist'); define('SMILIES_TABLE', $table_prefix . 'smilies'); define('SPHINX_TABLE', $table_prefix . 'sphinx'); +define('STORAGE_TABLE', $table_prefix . 'storage'); define('STYLES_TABLE', $table_prefix . 'styles'); define('STYLES_TEMPLATE_TABLE', $table_prefix . 'styles_template'); define('STYLES_TEMPLATE_DATA_TABLE',$table_prefix . 'styles_template_data'); diff --git a/phpBB/phpbb/db/migration/data/v330/storage_track.php b/phpBB/phpbb/db/migration/data/v330/storage_track.php new file mode 100644 index 0000000000..455fa4e94f --- /dev/null +++ b/phpBB/phpbb/db/migration/data/v330/storage_track.php @@ -0,0 +1,43 @@ + +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file. +* +*/ + +namespace phpbb\db\migration\data\v330; + +class storage_track extends \phpbb\db\migration\migration +{ + public function update_schema() + { + return array( + 'add_tables' => array( + $this->table_prefix . 'storage' => array( + 'COLUMNS' => array( + 'file_id' => array('UINT', null, 'auto_increment'), + 'file_path' => array('VCHAR', ''), + 'storage' => array('VCHAR', ''), + 'filesize' => array('UINT:20', 0), + ), + 'PRIMARY_KEY' => 'file_id', + ), + ), + ); + } + + public function revert_schema() + { + return array( + 'drop_tables' => array( + $this->table_prefix . 'storage', + ), + ); + } +} diff --git a/phpBB/phpbb/storage/storage.php b/phpBB/phpbb/storage/storage.php index 862b039c6a..cab2b26b4e 100644 --- a/phpBB/phpbb/storage/storage.php +++ b/phpBB/phpbb/storage/storage.php @@ -13,16 +13,28 @@ namespace phpbb\storage; +use phpbb\db\driver\driver_interface; + /** * @internal Experimental */ class storage { + /** + * @var \phpbb\db\driver\driver_interface + */ + protected $db; + /** * @var string */ protected $storage_name; + /** + * @var string + */ + protected $storage_table; + /** * @var \phpbb\storage\adapter_factory */ @@ -36,13 +48,16 @@ class storage /** * Constructor * + * @param \phpbb\db\driver\driver_interface $db * @param \phpbb\storage\adapter_factory $factory * @param string $storage_name */ - public function __construct(adapter_factory $factory, $storage_name) + public function __construct(driver_interface $db, adapter_factory $factory, $storage_name, $storage_table) { + $this->db = $db; $this->factory = $factory; $this->storage_name = $storage_name; + $this->storage_table = $storage_table; } /** @@ -81,7 +96,23 @@ class storage */ public function put_contents($path, $content) { - $this->get_adapter()->put_contents($path, $content); + try + { + $this->get_adapter()->put_contents($path, $content); + + $sql_ary = array( + 'file_path' => $path, + 'storage' => $this->get_name(), + 'filesize' => strlen($content), + ); + + $sql = 'INSERT INTO ' . $this->storage_table . $this->db->sql_build_array('INSERT', $sql_ary); + $this->db->sql_query($sql); + } + catch (\Exception $e) + { + throw $e; + } } /** @@ -121,7 +152,23 @@ class storage */ public function delete($path) { - $this->get_adapter()->delete($path); + try + { + $this->get_adapter()->delete($path); + + $sql_ary = array( + 'file_path' => $path, + 'storage' => $this->get_name(), + ); + + $sql = 'DELETE FROM ' . $this->storage_table . ' + WHERE ' . $this->db->sql_build_array('DELETE', $sql_ary); + $this->db->sql_query($sql); + } + catch (\Exception $e) + { + throw $e; + } } /** @@ -135,7 +182,28 @@ class storage */ public function rename($path_orig, $path_dest) { - $this->get_adapter()->rename($path_orig, $path_dest); + try + { + $this->get_adapter()->rename($path_orig, $path_dest); + + $sql_ary1 = array( + 'file_path' => $path_dest, + ); + + $sql_ary2 = array( + 'file_path' => $path_orig, + 'storage' => $this->get_name(), + ); + + $sql = 'UPDATE ' . $this->storage_table . ' + SET ' . $this->db->sql_build_array('UPDATE', $sql_ary1) . ' + WHERE ' . $this->db->sql_build_array('SELECT', $sql_ary2); + $this->db->sql_query($sql); + } + catch (\Exception $e) + { + throw $e; + } } /** @@ -149,7 +217,34 @@ class storage */ public function copy($path_orig, $path_dest) { - $this->get_adapter()->copy($path_orig, $path_dest); + try + { + $this->get_adapter()->copy($path_orig, $path_dest); + + $sql_ary = array( + 'file_path' => $path_orig, + 'storage' => $this->get_name(), + ); + + $sql = 'SELECT filesize FROM ' . $this->storage_table . ' + WHERE ' . $this->db->sql_build_array('SELECT', $sql_ary); + $result = $this->db->sql_query($sql); + $row = $this->db->sql_fetchrow($result); + $this->db->sql_freeresult($result); + + $sql_ary = array( + 'file_path' => $path_dest, + 'storage' => $this->get_name(), + 'filesize' => (int) $row['filesize'], + ); + + $sql = 'INSERT INTO ' . $this->storage_table . $this->db->sql_build_array('INSERT', $sql_ary); + $this->db->sql_query($sql); + } + catch (\Exception $e) + { + throw $e; + } } /** @@ -158,7 +253,7 @@ class storage * @param string $path File to read * * @throws \phpbb\storage\exception\exception When unable to open file - + * * @return resource Returns a file pointer */ public function read_stream($path) @@ -186,7 +281,8 @@ class storage * * @param string $path The target file * @param resource $resource The resource - * When target file cannot be created + * + * @throws \phpbb\storage\exception\exception When target file cannot be created */ public function write_stream($path, $resource) { @@ -195,6 +291,33 @@ class storage if ($adapter instanceof stream_interface) { $adapter->write_stream($path, $resource); + + $sql_ary = array( + 'file_path' => $path, + 'storage' => $this->get_name(), + ); + + // Get file, if exist update filesize, if not add new record + $sql = 'SELECT * FROM ' . $this->storage_table . ' + WHERE ' . $this->db->sql_build_array('SELECT', $sql_ary); + $result = $this->db->sql_query($sql); + $row = $this->db->sql_fetchrow($result); + $this->db->sql_freeresult($result); + + if ($row) + { + $sql = 'UPDATE ' . $this->storage_table . ' + SET filesize = filesize + ' . strlen($content) . ' + WHERE ' . $this->db->sql_build_array('SELECT', $sql_ary); + $this->db->sql_query($sql); + } + else + { + $sql_ary['filesize'] = strlen($content); + + $sql = 'INSERT INTO ' . $this->storage_table . $this->db->sql_build_array('INSERT', $sql_ary); + $this->db->sql_query($sql); + } } else { @@ -229,4 +352,23 @@ class storage { return $this->get_adapter()->get_link($path); } + + /** + * Get total storage size. + * + * @param string $path The file + * + * @return int Size in bytes + */ + public function get_size() + { + $sql = 'SELECT SUM(filesize) AS total + FROM ' . $this->storage_table . ' + WHERE storage = ' . $this->get_name(); + $result = $this->db->sql_query($sql); + $row = $this->db->sql_fetchrow($result); + $this->db->sql_freeresult($result); + + return $row['total']; + } } diff --git a/tests/avatar/manager_test.php b/tests/avatar/manager_test.php index 2866a1673d..30ef42e835 100644 --- a/tests/avatar/manager_test.php +++ b/tests/avatar/manager_test.php @@ -38,11 +38,12 @@ class phpbb_avatar_manager_test extends \phpbb_database_test_case $filesystem = new \phpbb\filesystem\filesystem(); $adapter = new \phpbb\storage\adapter\local($filesystem, new \FastImageSize\FastImageSize(), new \phpbb\mimetype\guesser(array(new \phpbb\mimetype\extension_guesser)), $phpbb_root_path); $adapter->configure(['path' => 'images/avatars/upload']); + $db = $this->createMock('\phpbb\db\driver\driver_interface'); $adapter_factory_mock = $this->createMock('\phpbb\storage\adapter_factory'); $adapter_factory_mock->expects($this->any()) ->method('get') ->willReturn($adapter); - $storage = new \phpbb\storage\storage($adapter_factory_mock, ''); + $storage = new \phpbb\storage\storage($db, $adapter_factory_mock, '', ''); // Prepare dependencies for avatar manager and driver $this->config = new \phpbb\config\config(array()); From 5a1b4f559d53f65befdd894c678536113c5415ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Calvo?= Date: Sun, 11 Feb 2018 13:22:18 +0100 Subject: [PATCH 02/34] [ticket/15342] Add avatar storage size to acp_main PHPBB3-15342 --- phpBB/includes/acp/acp_main.php | 6 ++++-- phpBB/phpbb/storage/storage.php | 16 +++++++++------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/phpBB/includes/acp/acp_main.php b/phpBB/includes/acp/acp_main.php index 99675e2b29..b914b6adf5 100644 --- a/phpBB/includes/acp/acp_main.php +++ b/phpBB/includes/acp/acp_main.php @@ -500,10 +500,12 @@ class acp_main $users_per_day = sprintf('%.2f', $total_users / $boarddays); $files_per_day = sprintf('%.2f', $total_files / $boarddays); + //$storage_attachment = $phpbb_container->get('storage.attachments'); + //$upload_dir_size = get_formatted_filesize($storage_attachment->get_size()); $upload_dir_size = get_formatted_filesize($config['upload_dir_size']); - // Couldn't open Avatar dir. - $avatar_dir_size = $user->lang['NOT_AVAILABLE']; + $storage_avatar = $phpbb_container->get('storage.avatar'); + $avatar_dir_size = get_formatted_filesize($storage_avatar->get_size()); if ($posts_per_day > $total_posts) { diff --git a/phpBB/phpbb/storage/storage.php b/phpBB/phpbb/storage/storage.php index cab2b26b4e..73b9218d93 100644 --- a/phpBB/phpbb/storage/storage.php +++ b/phpBB/phpbb/storage/storage.php @@ -306,18 +306,20 @@ class storage if ($row) { - $sql = 'UPDATE ' . $this->storage_table . ' - SET filesize = filesize + ' . strlen($content) . ' - WHERE ' . $this->db->sql_build_array('SELECT', $sql_ary); - $this->db->sql_query($sql); + //$sql = 'UPDATE ' . $this->storage_table . ' + // SET filesize = filesize + ' . strlen($content) . ' + // WHERE ' . $this->db->sql_build_array('SELECT', $sql_ary); + //$this->db->sql_query($sql); } else { - $sql_ary['filesize'] = strlen($content); + //$sql_ary['filesize'] = strlen($content); + $sql_ary['filesize'] = 0; $sql = 'INSERT INTO ' . $this->storage_table . $this->db->sql_build_array('INSERT', $sql_ary); $this->db->sql_query($sql); } + } else { @@ -363,8 +365,8 @@ class storage public function get_size() { $sql = 'SELECT SUM(filesize) AS total - FROM ' . $this->storage_table . ' - WHERE storage = ' . $this->get_name(); + FROM ' . $this->storage_table . " + WHERE storage = '" . $this->get_name() . "'"; $result = $this->db->sql_query($sql); $row = $this->db->sql_fetchrow($result); $this->db->sql_freeresult($result); From e9dd717b8b235b404b28e02574c9b764b01f4a95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Calvo?= Date: Thu, 15 Feb 2018 09:33:12 +0100 Subject: [PATCH 03/34] [ticket/15342] Track stream write and move tracking to separate methods PHPBB3-15342 --- phpBB/phpbb/storage/storage.php | 131 ++++++++++++-------------------- 1 file changed, 50 insertions(+), 81 deletions(-) diff --git a/phpBB/phpbb/storage/storage.php b/phpBB/phpbb/storage/storage.php index 73b9218d93..6ad658065a 100644 --- a/phpBB/phpbb/storage/storage.php +++ b/phpBB/phpbb/storage/storage.php @@ -99,15 +99,7 @@ class storage try { $this->get_adapter()->put_contents($path, $content); - - $sql_ary = array( - 'file_path' => $path, - 'storage' => $this->get_name(), - 'filesize' => strlen($content), - ); - - $sql = 'INSERT INTO ' . $this->storage_table . $this->db->sql_build_array('INSERT', $sql_ary); - $this->db->sql_query($sql); + $this->track_file($path); } catch (\Exception $e) { @@ -155,15 +147,7 @@ class storage try { $this->get_adapter()->delete($path); - - $sql_ary = array( - 'file_path' => $path, - 'storage' => $this->get_name(), - ); - - $sql = 'DELETE FROM ' . $this->storage_table . ' - WHERE ' . $this->db->sql_build_array('DELETE', $sql_ary); - $this->db->sql_query($sql); + $this->untrack_file($path); } catch (\Exception $e) { @@ -185,20 +169,8 @@ class storage try { $this->get_adapter()->rename($path_orig, $path_dest); - - $sql_ary1 = array( - 'file_path' => $path_dest, - ); - - $sql_ary2 = array( - 'file_path' => $path_orig, - 'storage' => $this->get_name(), - ); - - $sql = 'UPDATE ' . $this->storage_table . ' - SET ' . $this->db->sql_build_array('UPDATE', $sql_ary1) . ' - WHERE ' . $this->db->sql_build_array('SELECT', $sql_ary2); - $this->db->sql_query($sql); + $this->untrack_file($path_orig); + $this->track_file($path_dest); } catch (\Exception $e) { @@ -220,26 +192,7 @@ class storage try { $this->get_adapter()->copy($path_orig, $path_dest); - - $sql_ary = array( - 'file_path' => $path_orig, - 'storage' => $this->get_name(), - ); - - $sql = 'SELECT filesize FROM ' . $this->storage_table . ' - WHERE ' . $this->db->sql_build_array('SELECT', $sql_ary); - $result = $this->db->sql_query($sql); - $row = $this->db->sql_fetchrow($result); - $this->db->sql_freeresult($result); - - $sql_ary = array( - 'file_path' => $path_dest, - 'storage' => $this->get_name(), - 'filesize' => (int) $row['filesize'], - ); - - $sql = 'INSERT INTO ' . $this->storage_table . $this->db->sql_build_array('INSERT', $sql_ary); - $this->db->sql_query($sql); + $this->track_file($path_dest); } catch (\Exception $e) { @@ -291,35 +244,7 @@ class storage if ($adapter instanceof stream_interface) { $adapter->write_stream($path, $resource); - - $sql_ary = array( - 'file_path' => $path, - 'storage' => $this->get_name(), - ); - - // Get file, if exist update filesize, if not add new record - $sql = 'SELECT * FROM ' . $this->storage_table . ' - WHERE ' . $this->db->sql_build_array('SELECT', $sql_ary); - $result = $this->db->sql_query($sql); - $row = $this->db->sql_fetchrow($result); - $this->db->sql_freeresult($result); - - if ($row) - { - //$sql = 'UPDATE ' . $this->storage_table . ' - // SET filesize = filesize + ' . strlen($content) . ' - // WHERE ' . $this->db->sql_build_array('SELECT', $sql_ary); - //$this->db->sql_query($sql); - } - else - { - //$sql_ary['filesize'] = strlen($content); - $sql_ary['filesize'] = 0; - - $sql = 'INSERT INTO ' . $this->storage_table . $this->db->sql_build_array('INSERT', $sql_ary); - $this->db->sql_query($sql); - } - + $this->track_file($path); // Not sure if here, or after close the file } else { @@ -328,6 +253,50 @@ class storage } } + protected function track_file($path, $update = false) + { + $sql_ary = array( + 'file_path' => $path, + 'storage' => $this->get_name(), + ); + + // Get file, if exist update filesize, if not add new record + $sql = 'SELECT * FROM ' . $this->storage_table . ' + WHERE ' . $this->db->sql_build_array('SELECT', $sql_ary); + $result = $this->db->sql_query($sql); + $row = $this->db->sql_fetchrow($result); + $this->db->sql_freeresult($result); + + if (!$row) + { + $file = $this->file_info($path); + $sql_ary['filesize'] = $file->size; + + $sql = 'INSERT INTO ' . $this->storage_table . $this->db->sql_build_array('INSERT', $sql_ary); + $this->db->sql_query($sql); + } + else if ($update) + { + $file = $this->file_info($path); + $sql = 'UPDATE ' . $this->storage_table . ' + SET filesize = ' . $file->size . ' + WHERE ' . $this->db->sql_build_array('SELECT', $sql_ary); + $this->db->sql_query($sql); + } + } + + protected function untrack_file($path) + { + $sql_ary = array( + 'file_path' => $path, + 'storage' => $this->get_name(), + ); + + $sql = 'DELETE FROM ' . $this->storage_table . ' + WHERE ' . $this->db->sql_build_array('DELETE', $sql_ary); + $this->db->sql_query($sql); + } + /** * Get file info. * From 91026b56b9c9cebcd707d69a02db1b03708d0175 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Calvo?= Date: Thu, 15 Feb 2018 10:09:53 +0100 Subject: [PATCH 04/34] [ticket/15342] Track current uploaded files PHPBB3-15342 --- .../db/migration/data/v330/storage_track.php | 24 +++++++++++++++++++ phpBB/phpbb/storage/storage.php | 4 ++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/phpBB/phpbb/db/migration/data/v330/storage_track.php b/phpBB/phpbb/db/migration/data/v330/storage_track.php index 455fa4e94f..c488041243 100644 --- a/phpBB/phpbb/db/migration/data/v330/storage_track.php +++ b/phpBB/phpbb/db/migration/data/v330/storage_track.php @@ -40,4 +40,28 @@ class storage_track extends \phpbb\db\migration\migration ), ); } + + public function update_data() + { + return [ + ['custom', [[$this, 'track_avatars']]], + ['custom', [[$this, 'track_attachments']]], + ['custom', [[$this, 'track_backups']]], + ]; + } + + public function track_avatars() + { + + } + + public function track_attachments() + { + + } + + public function track_backups() + { + + } } diff --git a/phpBB/phpbb/storage/storage.php b/phpBB/phpbb/storage/storage.php index 6ad658065a..6e00520b3c 100644 --- a/phpBB/phpbb/storage/storage.php +++ b/phpBB/phpbb/storage/storage.php @@ -253,7 +253,7 @@ class storage } } - protected function track_file($path, $update = false) + public function track_file($path, $update = false) { $sql_ary = array( 'file_path' => $path, @@ -285,7 +285,7 @@ class storage } } - protected function untrack_file($path) + public function untrack_file($path) { $sql_ary = array( 'file_path' => $path, From 171b56b0ac73c99448b942448e6d6c92a2912b3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Calvo?= Date: Wed, 16 May 2018 23:54:42 +0200 Subject: [PATCH 05/34] [ticket/15342] Add missing services dependencies PHPBB3-15342 --- phpBB/config/default/container/services_storage.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/phpBB/config/default/container/services_storage.yml b/phpBB/config/default/container/services_storage.yml index 4223bdb2bf..b80a394ecc 100644 --- a/phpBB/config/default/container/services_storage.yml +++ b/phpBB/config/default/container/services_storage.yml @@ -4,8 +4,10 @@ services: storage.attachment: class: phpbb\storage\storage arguments: + - '@dbal.conn' - '@storage.adapter.factory' - 'attachment' + - '%tables.storage%' tags: - { name: storage } @@ -25,6 +27,7 @@ services: - '@dbal.conn' - '@storage.adapter.factory' - 'backup' + - '%tables.storage%' tags: - { name: storage } From b91ce7610bff37a525f4689f43eb50049acf00e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Calvo?= Date: Thu, 17 May 2018 02:02:12 +0200 Subject: [PATCH 06/34] [ticket/15342] Fix tests PHPBB3-15342 --- tests/attachment/delete_test.php | 2 +- tests/functions_user/delete_user_test.php | 2 +- tests/notification/submit_post_base.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/attachment/delete_test.php b/tests/attachment/delete_test.php index cefe076bbf..0718281d59 100644 --- a/tests/attachment/delete_test.php +++ b/tests/attachment/delete_test.php @@ -61,7 +61,7 @@ class phpbb_attachment_delete_test extends \phpbb_database_test_case $adapter_factory_mock->expects($this->any()) ->method('get') ->willReturn($adapter); - $this->storage = new \phpbb\storage\storage($adapter_factory_mock, ''); + $this->storage = new \phpbb\storage\storage($db, $adapter_factory_mock, '', ''); $this->dispatcher = new \phpbb_mock_event_dispatcher(); $this->attachment_delete = new \phpbb\attachment\delete($this->config, $this->db, $this->dispatcher, $this->resync, $this->storage); } diff --git a/tests/functions_user/delete_user_test.php b/tests/functions_user/delete_user_test.php index b51dabbba0..489aec01cf 100644 --- a/tests/functions_user/delete_user_test.php +++ b/tests/functions_user/delete_user_test.php @@ -41,7 +41,7 @@ class phpbb_functions_user_delete_user_test extends phpbb_database_test_case $adapter_factory_mock->expects($this->any()) ->method('get') ->willReturn($adapter); - $storage = new \phpbb\storage\storage($adapter_factory_mock, ''); + $storage = new \phpbb\storage\storage($db, $adapter_factory_mock, '', ''); // Works as a workaround for tests $phpbb_container->set('attachment.manager', new \phpbb\attachment\delete($config, $db, new \phpbb_mock_event_dispatcher(), new \phpbb\attachment\resync($db), $storage)); diff --git a/tests/notification/submit_post_base.php b/tests/notification/submit_post_base.php index d882c106ac..db65c5df07 100644 --- a/tests/notification/submit_post_base.php +++ b/tests/notification/submit_post_base.php @@ -98,7 +98,7 @@ abstract class phpbb_notification_submit_post_base extends phpbb_database_test_c $adapter_factory_mock->expects($this->any()) ->method('get') ->willReturn($adapter); - $storage = new \phpbb\storage\storage($adapter_factory_mock, ''); + $storage = new \phpbb\storage\storage($db, $adapter_factory_mock, '', ''); // User $user = $this->createMock('\phpbb\user', array(), array( From 28c0db86cff62bdd21e84ead2fb729e6d3fcaad4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Calvo?= Date: Sat, 9 Jun 2018 13:57:38 +0200 Subject: [PATCH 07/34] [ticket/15342] Add dependencies to migration PHPBB3-15342 --- phpBB/phpbb/db/migration/data/v330/storage_track.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/phpBB/phpbb/db/migration/data/v330/storage_track.php b/phpBB/phpbb/db/migration/data/v330/storage_track.php index c488041243..6d52affdf5 100644 --- a/phpBB/phpbb/db/migration/data/v330/storage_track.php +++ b/phpBB/phpbb/db/migration/data/v330/storage_track.php @@ -15,6 +15,15 @@ namespace phpbb\db\migration\data\v330; class storage_track extends \phpbb\db\migration\migration { + static public function depends_on() + { + return array( + '\phpbb\db\migration\data\v330\storage_attachment', + '\phpbb\db\migration\data\v330\storage_avatar', + '\phpbb\db\migration\data\v330\storage_backup', + ); + } + public function update_schema() { return array( From cc7178a6e0731d03ada3d0425b4b59b3976f17a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Calvo?= Date: Sat, 9 Jun 2018 16:37:57 +0200 Subject: [PATCH 08/34] [ticket/15342] Small improvements to storage PHPBB3-15342 --- phpBB/includes/acp/acp_database.php | 2 ++ phpBB/phpbb/attachment/upload.php | 1 + phpBB/phpbb/storage/storage.php | 52 +++++++---------------------- 3 files changed, 15 insertions(+), 40 deletions(-) diff --git a/phpBB/includes/acp/acp_database.php b/phpBB/includes/acp/acp_database.php index c26ecdb849..b6dbc4bec2 100644 --- a/phpBB/includes/acp/acp_database.php +++ b/phpBB/includes/acp/acp_database.php @@ -189,6 +189,8 @@ class acp_database fclose($fp); + $storage->track_file($file); + // Remove file from tmp @unlink($temp_dir . '/' . $file); diff --git a/phpBB/phpbb/attachment/upload.php b/phpBB/phpbb/attachment/upload.php index 54515fd0a6..3a983addc3 100644 --- a/phpBB/phpbb/attachment/upload.php +++ b/phpBB/phpbb/attachment/upload.php @@ -248,6 +248,7 @@ class upload $fp = fopen($destination, 'rb'); $this->storage->write_stream($destination_name, $fp); fclose($fp); + $this->storage->track_file($destination_name); } else { diff --git a/phpBB/phpbb/storage/storage.php b/phpBB/phpbb/storage/storage.php index 6e00520b3c..a9f48827ae 100644 --- a/phpBB/phpbb/storage/storage.php +++ b/phpBB/phpbb/storage/storage.php @@ -96,15 +96,8 @@ class storage */ public function put_contents($path, $content) { - try - { - $this->get_adapter()->put_contents($path, $content); - $this->track_file($path); - } - catch (\Exception $e) - { - throw $e; - } + $this->get_adapter()->put_contents($path, $content); + $this->track_file($path); } /** @@ -144,15 +137,8 @@ class storage */ public function delete($path) { - try - { - $this->get_adapter()->delete($path); - $this->untrack_file($path); - } - catch (\Exception $e) - { - throw $e; - } + $this->get_adapter()->delete($path); + $this->untrack_file($path); } /** @@ -166,16 +152,9 @@ class storage */ public function rename($path_orig, $path_dest) { - try - { - $this->get_adapter()->rename($path_orig, $path_dest); - $this->untrack_file($path_orig); - $this->track_file($path_dest); - } - catch (\Exception $e) - { - throw $e; - } + $this->get_adapter()->rename($path_orig, $path_dest); + $this->untrack_file($path_orig); + $this->track_file($path_dest); } /** @@ -189,15 +168,8 @@ class storage */ public function copy($path_orig, $path_dest) { - try - { - $this->get_adapter()->copy($path_orig, $path_dest); - $this->track_file($path_dest); - } - catch (\Exception $e) - { - throw $e; - } + $this->get_adapter()->copy($path_orig, $path_dest); + $this->track_file($path_dest); } /** @@ -231,6 +203,7 @@ class storage /** * Writes a new file using a stream. + * You have to track file after use this method * * @param string $path The target file * @param resource $resource The resource @@ -244,7 +217,6 @@ class storage if ($adapter instanceof stream_interface) { $adapter->write_stream($path, $resource); - $this->track_file($path); // Not sure if here, or after close the file } else { @@ -337,9 +309,9 @@ class storage FROM ' . $this->storage_table . " WHERE storage = '" . $this->get_name() . "'"; $result = $this->db->sql_query($sql); - $row = $this->db->sql_fetchrow($result); + $total = (int) $this->db->sql_fetchfield('total'); $this->db->sql_freeresult($result); - return $row['total']; + return $row; } } From b823ae85cf55ec3de0428c0d2bb4873303a6a854 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Calvo?= Date: Sat, 9 Jun 2018 16:38:43 +0200 Subject: [PATCH 09/34] [ticket/15342] Track current uploaded files PHPBB3-15342 --- .../db/migration/data/v330/storage_track.php | 41 ++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/phpBB/phpbb/db/migration/data/v330/storage_track.php b/phpBB/phpbb/db/migration/data/v330/storage_track.php index 6d52affdf5..12edc9bc3f 100644 --- a/phpBB/phpbb/db/migration/data/v330/storage_track.php +++ b/phpBB/phpbb/db/migration/data/v330/storage_track.php @@ -13,6 +13,8 @@ namespace phpbb\db\migration\data\v330; +use phpbb\storage\storage; + class storage_track extends \phpbb\db\migration\migration { static public function depends_on() @@ -28,7 +30,7 @@ class storage_track extends \phpbb\db\migration\migration { return array( 'add_tables' => array( - $this->table_prefix . 'storage' => array( + STORAGE_TABLE => array( 'COLUMNS' => array( 'file_id' => array('UINT', null, 'auto_increment'), 'file_path' => array('VCHAR', ''), @@ -61,16 +63,53 @@ class storage_track extends \phpbb\db\migration\migration public function track_avatars() { + /** @var storage $storage */ + $storage = $this->container->get('storage.avatar'); + $sql = 'SELECT user_avatar + FROM ' . USERS_TABLE . " + WHERE user_avatar_type = 'avatar.driver.upload'"; + + $result = $this->db->sql_query($sql); + + while ($row = $this->db->sql_fetchrow($result)) + { + $storage->track_file($row['user_avatar']); + } + $this->db->sql_freeresult($result); } public function track_attachments() { + /** @var storage $storage */ + $storage = $this->container->get('storage.attachment'); + $sql = 'SELECT physical_filename + FROM ' . ATTACHMENTS_TABLE; + + $result = $this->db->sql_query($sql); + + while ($row = $this->db->sql_fetchrow($result)) + { + $storage->track_file($row['physical_filename']); + } + $this->db->sql_freeresult($result); } public function track_backups() { + /** @var storage $storage */ + $storage = $this->container->get('storage.backup'); + $sql = 'SELECT filename + FROM ' . BACKUPS_TABLE; + + $result = $this->db->sql_query($sql); + + while ($row = $this->db->sql_fetchrow($result)) + { + $storage->track_file($row['filename']); + } + $this->db->sql_freeresult($result); } } From 83d559afc58c76c54dc8241bd3ef51a1b533fe01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Calvo?= Date: Sat, 9 Jun 2018 17:13:58 +0200 Subject: [PATCH 10/34] [ticket/15342] Track attachments thumbnails PHPBB3-15342 --- phpBB/phpbb/db/migration/data/v330/storage_track.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/phpBB/phpbb/db/migration/data/v330/storage_track.php b/phpBB/phpbb/db/migration/data/v330/storage_track.php index 12edc9bc3f..c3f8d128ec 100644 --- a/phpBB/phpbb/db/migration/data/v330/storage_track.php +++ b/phpBB/phpbb/db/migration/data/v330/storage_track.php @@ -84,7 +84,7 @@ class storage_track extends \phpbb\db\migration\migration /** @var storage $storage */ $storage = $this->container->get('storage.attachment'); - $sql = 'SELECT physical_filename + $sql = 'SELECT physical_filename, thumbnail FROM ' . ATTACHMENTS_TABLE; $result = $this->db->sql_query($sql); @@ -92,6 +92,11 @@ class storage_track extends \phpbb\db\migration\migration while ($row = $this->db->sql_fetchrow($result)) { $storage->track_file($row['physical_filename']); + + if($row['thumbnail'] == 1) + { + $storage->track_file('thumb_' . $row['physical_filename']); + } } $this->db->sql_freeresult($result); } From 80e5fe255bc11030d6c23780ef42e8f57a0ab6ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Calvo?= Date: Sun, 10 Jun 2018 13:57:07 +0200 Subject: [PATCH 11/34] [ticket/15342] Add method to storage to get number of tracked files PHPBB3-15342 --- phpBB/phpbb/storage/storage.php | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/phpBB/phpbb/storage/storage.php b/phpBB/phpbb/storage/storage.php index a9f48827ae..16920ee129 100644 --- a/phpBB/phpbb/storage/storage.php +++ b/phpBB/phpbb/storage/storage.php @@ -299,8 +299,6 @@ class storage /** * Get total storage size. * - * @param string $path The file - * * @return int Size in bytes */ public function get_size() @@ -314,4 +312,21 @@ class storage return $row; } + + /** + * Get number of storage files. + * + * @return int Number of files + */ + public function get_num_files() + { + $sql = 'SELECT COUNT(file_id) AS total + FROM ' . $this->storage_table . " + WHERE storage = '" . $this->get_name() . "'"; + $result = $this->db->sql_query($sql); + $total = (int) $this->db->sql_fetchfield('total'); + $this->db->sql_freeresult($result); + + return $total; + } } From 9184d34a51f935d51a03c7f25d633bbec310ff5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Calvo?= Date: Sun, 10 Jun 2018 20:26:22 +0200 Subject: [PATCH 12/34] [ticket/15342] Use cache to get the number of files and the size of any storage PHPBB3-15342 --- .../default/container/services_storage.yml | 3 + phpBB/phpbb/storage/storage.php | 83 +++++++++++++------ 2 files changed, 60 insertions(+), 26 deletions(-) diff --git a/phpBB/config/default/container/services_storage.yml b/phpBB/config/default/container/services_storage.yml index b80a394ecc..92f31779e6 100644 --- a/phpBB/config/default/container/services_storage.yml +++ b/phpBB/config/default/container/services_storage.yml @@ -5,6 +5,7 @@ services: class: phpbb\storage\storage arguments: - '@dbal.conn' + - '@cache.driver' - '@storage.adapter.factory' - 'attachment' - '%tables.storage%' @@ -15,6 +16,7 @@ services: class: phpbb\storage\storage arguments: - '@dbal.conn' + - '@cache.driver' - '@storage.adapter.factory' - 'avatar' - '%tables.storage%' @@ -25,6 +27,7 @@ services: class: phpbb\storage\storage arguments: - '@dbal.conn' + - '@cache.driver' - '@storage.adapter.factory' - 'backup' - '%tables.storage%' diff --git a/phpBB/phpbb/storage/storage.php b/phpBB/phpbb/storage/storage.php index 16920ee129..833e398ba4 100644 --- a/phpBB/phpbb/storage/storage.php +++ b/phpBB/phpbb/storage/storage.php @@ -13,18 +13,35 @@ namespace phpbb\storage; -use phpbb\db\driver\driver_interface; +use phpbb\cache\driver\driver_interface as cache; +use phpbb\db\driver\driver_interface as db; /** * @internal Experimental */ class storage { + /** + * @var \phpbb\storage\adapter\adapter_interface + */ + protected $adapter; + /** * @var \phpbb\db\driver\driver_interface */ protected $db; + /** + * Cache driver + * @var \phpbb\cache\driver\driver_interface + */ + protected $cache; + + /** + * @var \phpbb\storage\adapter_factory + */ + protected $factory; + /** * @var string */ @@ -35,26 +52,18 @@ class storage */ protected $storage_table; - /** - * @var \phpbb\storage\adapter_factory - */ - protected $factory; - - /** - * @var \phpbb\storage\adapter\adapter_interface - */ - protected $adapter; - /** * Constructor * + * @param \phpbb\cache\driver\driver_interface $db * @param \phpbb\db\driver\driver_interface $db * @param \phpbb\storage\adapter_factory $factory * @param string $storage_name */ - public function __construct(driver_interface $db, adapter_factory $factory, $storage_name, $storage_table) + public function __construct(db $db, cache $cache, adapter_factory $factory, $storage_name, $storage_table) { $this->db = $db; + $this->cache = $cache; $this->factory = $factory; $this->storage_name = $storage_name; $this->storage_table = $storage_table; @@ -255,6 +264,9 @@ class storage WHERE ' . $this->db->sql_build_array('SELECT', $sql_ary); $this->db->sql_query($sql); } + + $this->cache->destroy('_storage_' . $this->get_name() . '_totalsize'); + $this->cache->destroy('_storage_' . $this->get_name() . '_numfiles'); } public function untrack_file($path) @@ -267,6 +279,9 @@ class storage $sql = 'DELETE FROM ' . $this->storage_table . ' WHERE ' . $this->db->sql_build_array('DELETE', $sql_ary); $this->db->sql_query($sql); + + $this->cache->destroy('_storage_' . $this->get_name() . '_totalsize'); + $this->cache->destroy('_storage_' . $this->get_name() . '_numfiles'); } /** @@ -303,14 +318,22 @@ class storage */ public function get_size() { - $sql = 'SELECT SUM(filesize) AS total - FROM ' . $this->storage_table . " - WHERE storage = '" . $this->get_name() . "'"; - $result = $this->db->sql_query($sql); - $total = (int) $this->db->sql_fetchfield('total'); - $this->db->sql_freeresult($result); + $total_size = $this->cache->get('_storage_' . $this->get_name() . '_totalsize'); - return $row; + if ($total_size === false) + { + $sql = 'SELECT SUM(filesize) AS totalsize + FROM ' . $this->storage_table . " + WHERE storage = '" . $this->get_name() . "'"; + $result = $this->db->sql_query($sql); + + $total_size = (int) $this->db->sql_fetchfield('totalsize'); + $this->cache->put('_storage_' . $this->get_name() . '_totalsize', $total_size); + + $this->db->sql_freeresult($result); + } + + return $total_size; } /** @@ -320,13 +343,21 @@ class storage */ public function get_num_files() { - $sql = 'SELECT COUNT(file_id) AS total - FROM ' . $this->storage_table . " - WHERE storage = '" . $this->get_name() . "'"; - $result = $this->db->sql_query($sql); - $total = (int) $this->db->sql_fetchfield('total'); - $this->db->sql_freeresult($result); + $number_files = $this->cache->get('_storage_' . $this->get_name() . '_numfiles'); - return $total; + if ($number_files === false) + { + $sql = 'SELECT COUNT(file_id) AS numfiles + FROM ' . $this->storage_table . " + WHERE storage = '" . $this->get_name() . "'"; + $result = $this->db->sql_query($sql); + + $number_files = (int) $this->db->sql_fetchfield('numfiles'); + $this->cache->put('_storage_' . $this->get_name() . '_numfiles', $number_files); + + $this->db->sql_freeresult($result); + } + + return $number_files; } } From 1d686fecb54fa8074be964fbe5d9d0d078eea8d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Calvo?= Date: Tue, 12 Jun 2018 00:21:51 +0200 Subject: [PATCH 13/34] [ticket/15342] Fix parameter that could be null PHPBB3-15342 --- phpBB/phpbb/storage/storage.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpBB/phpbb/storage/storage.php b/phpBB/phpbb/storage/storage.php index 833e398ba4..91f08c50f4 100644 --- a/phpBB/phpbb/storage/storage.php +++ b/phpBB/phpbb/storage/storage.php @@ -295,7 +295,7 @@ class storage */ public function file_info($path) { - return new file_info($this->adapter, $path); + return new file_info($this->get_adapter(), $path); } /** From 944432d382fb1008fea732252c0bf74047ec6ada Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Calvo?= Date: Tue, 12 Jun 2018 09:40:12 +0200 Subject: [PATCH 14/34] [ticket/15342] Remove comment since they are a little diferent things PHPBB3-15342 --- phpBB/includes/acp/acp_main.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/phpBB/includes/acp/acp_main.php b/phpBB/includes/acp/acp_main.php index b914b6adf5..4ab2c409ab 100644 --- a/phpBB/includes/acp/acp_main.php +++ b/phpBB/includes/acp/acp_main.php @@ -500,8 +500,6 @@ class acp_main $users_per_day = sprintf('%.2f', $total_users / $boarddays); $files_per_day = sprintf('%.2f', $total_files / $boarddays); - //$storage_attachment = $phpbb_container->get('storage.attachments'); - //$upload_dir_size = get_formatted_filesize($storage_attachment->get_size()); $upload_dir_size = get_formatted_filesize($config['upload_dir_size']); $storage_avatar = $phpbb_container->get('storage.avatar'); From bb0d705ebf59c4c263b5229488b00cf21e770127 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Calvo?= Date: Tue, 12 Jun 2018 09:41:08 +0200 Subject: [PATCH 15/34] [ticket/15342] Fix files not being tracked PHPBB3-15342 --- phpBB/phpbb/files/filespec_storage.php | 1 + 1 file changed, 1 insertion(+) diff --git a/phpBB/phpbb/files/filespec_storage.php b/phpBB/phpbb/files/filespec_storage.php index 8b6194fe99..a6870c4a05 100644 --- a/phpBB/phpbb/files/filespec_storage.php +++ b/phpBB/phpbb/files/filespec_storage.php @@ -447,6 +447,7 @@ class filespec_storage $fp = fopen($this->filename, 'rb'); $storage->write_stream($this->destination_file, $fp); fclose($fp); + $storage->track_file($this->destination_file); } catch (\phpbb\storage\exception\exception $e) { From 4cff370f8d7c201874de2b7f79b4dd59506ce008 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Calvo?= Date: Tue, 12 Jun 2018 09:41:52 +0200 Subject: [PATCH 16/34] [ticket/15342] Add method to get free space PHPBB3-15342 --- .../storage/adapter/adapter_interface.php | 9 +++++++++ phpBB/phpbb/storage/adapter/local.php | 19 +++++++++++++++++++ phpBB/phpbb/storage/storage.php | 13 +++++++++++++ 3 files changed, 41 insertions(+) diff --git a/phpBB/phpbb/storage/adapter/adapter_interface.php b/phpBB/phpbb/storage/adapter/adapter_interface.php index d57c2ba587..96617c8e6a 100644 --- a/phpBB/phpbb/storage/adapter/adapter_interface.php +++ b/phpBB/phpbb/storage/adapter/adapter_interface.php @@ -95,4 +95,13 @@ interface adapter_interface * */ public function get_link($path); + + /* + * Get space available in bytes. + * + * @throws \phpbb\storage\exception\exception When can't get available space + * + * @return int Returns available space + */ + public function free_space(); } diff --git a/phpBB/phpbb/storage/adapter/local.php b/phpBB/phpbb/storage/adapter/local.php index bfcdebc565..fd0fbe84d8 100644 --- a/phpBB/phpbb/storage/adapter/local.php +++ b/phpBB/phpbb/storage/adapter/local.php @@ -424,4 +424,23 @@ class local implements adapter_interface, stream_interface { return generate_board_url() . $this->path . $path; } + + /* + * Get space available in bytes. + * + * @throws \phpbb\storage\exception\exception When can't get available space + * + * @return int Returns available space + */ + public function free_space() + { + $free_space = @disk_free_space($this->root_path); + + if ($free_space === false) + { + throw new exception('STORAGE_CANNOT_GET_FREE_SPACE'); + } + + return (int) $free_space; + } } diff --git a/phpBB/phpbb/storage/storage.php b/phpBB/phpbb/storage/storage.php index 91f08c50f4..327bb60ab1 100644 --- a/phpBB/phpbb/storage/storage.php +++ b/phpBB/phpbb/storage/storage.php @@ -360,4 +360,17 @@ class storage return $number_files; } + + /** + * Get space available in bytes. + * + * @throws \phpbb\storage\exception\exception When can't get available space + * + * @return int Returns available space + */ + public function free_space() + { + return $this->get_adapter()->free_space(); + } + } From 22b2e955319a51d8ff9376710aa6c3088c88a45d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Calvo?= Date: Tue, 12 Jun 2018 09:42:56 +0200 Subject: [PATCH 17/34] [ticket/15342] Add again check of free space when uploading attachments PHPBB3-15342 --- phpBB/phpbb/attachment/upload.php | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/phpBB/phpbb/attachment/upload.php b/phpBB/phpbb/attachment/upload.php index 3a983addc3..8d4b68c7be 100644 --- a/phpBB/phpbb/attachment/upload.php +++ b/phpBB/phpbb/attachment/upload.php @@ -327,10 +327,36 @@ class upload /** * Check if there is enough free space available on disk * - * @return bool True if disk space is available, false if not + * @return bool True if disk space is available or can't get it, false if not */ protected function check_disk_space() { + try + { + $free_space = $this->storage->free_space(); + + if ($free_space <= $this->file->get('filesize')) + { + if ($this->auth->acl_get('a_')) + { + $this->file_data['error'][] = $this->language->lang('ATTACH_DISK_FULL'); + } + else + { + $this->file_data['error'][] = $this->language->lang('ATTACH_QUOTA_REACHED'); + } + $this->file_data['post_attach'] = false; + + $this->file->remove($this->storage); + + return false; + } + } + catch (\phpbb\storage\exception\exception $e) + { + // Nothing + } + return true; } From f9c19013179b2a2e6a8e4de017e7d459de613142 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Calvo?= Date: Tue, 19 Jun 2018 18:31:21 +0200 Subject: [PATCH 18/34] [ticket/15342] Fix test PHPBB3-15342 --- phpBB/phpbb/db/migration/data/v330/storage_track.php | 4 ++-- phpBB/phpbb/storage/storage.php | 3 ++- tests/attachment/delete_test.php | 5 +++-- tests/attachment/upload_test.php | 4 +++- tests/avatar/manager_test.php | 5 +++-- tests/content_visibility/delete_post_test.php | 2 +- tests/functions_user/delete_user_test.php | 8 +------- tests/notification/submit_post_base.php | 9 ++------- tests/privmsgs/delete_user_pms_test.php | 5 ++++- 9 files changed, 21 insertions(+), 24 deletions(-) diff --git a/phpBB/phpbb/db/migration/data/v330/storage_track.php b/phpBB/phpbb/db/migration/data/v330/storage_track.php index c3f8d128ec..0c4f620c28 100644 --- a/phpBB/phpbb/db/migration/data/v330/storage_track.php +++ b/phpBB/phpbb/db/migration/data/v330/storage_track.php @@ -30,7 +30,7 @@ class storage_track extends \phpbb\db\migration\migration { return array( 'add_tables' => array( - STORAGE_TABLE => array( + $this->table_prefix . 'storage' => array( 'COLUMNS' => array( 'file_id' => array('UINT', null, 'auto_increment'), 'file_path' => array('VCHAR', ''), @@ -93,7 +93,7 @@ class storage_track extends \phpbb\db\migration\migration { $storage->track_file($row['physical_filename']); - if($row['thumbnail'] == 1) + if ($row['thumbnail'] == 1) { $storage->track_file('thumb_' . $row['physical_filename']); } diff --git a/phpBB/phpbb/storage/storage.php b/phpBB/phpbb/storage/storage.php index 327bb60ab1..63369af7e8 100644 --- a/phpBB/phpbb/storage/storage.php +++ b/phpBB/phpbb/storage/storage.php @@ -55,10 +55,11 @@ class storage /** * Constructor * - * @param \phpbb\cache\driver\driver_interface $db * @param \phpbb\db\driver\driver_interface $db + * @param \phpbb\cache\driver\driver_interface $cache * @param \phpbb\storage\adapter_factory $factory * @param string $storage_name + * @param string $storage_table */ public function __construct(db $db, cache $cache, adapter_factory $factory, $storage_name, $storage_table) { diff --git a/tests/attachment/delete_test.php b/tests/attachment/delete_test.php index 0718281d59..35dbf5d47d 100644 --- a/tests/attachment/delete_test.php +++ b/tests/attachment/delete_test.php @@ -44,9 +44,10 @@ class phpbb_attachment_delete_test extends \phpbb_database_test_case parent::setUp(); + $cache = $this->createMock('\phpbb\cache\driver\driver_interface'); $this->config = new \phpbb\config\config(array()); $this->db = $this->new_dbal(); - $db = $this->db; + $db_mock = $this->createMock('\phpbb\db\driver\driver_interface'); $this->resync = new \phpbb\attachment\resync($this->db); $this->filesystem = $this->createMock('\phpbb\filesystem\filesystem', array('remove', 'exists')); $this->filesystem->expects($this->any()) @@ -61,7 +62,7 @@ class phpbb_attachment_delete_test extends \phpbb_database_test_case $adapter_factory_mock->expects($this->any()) ->method('get') ->willReturn($adapter); - $this->storage = new \phpbb\storage\storage($db, $adapter_factory_mock, '', ''); + $this->storage = new \phpbb\storage\storage($db_mock, $cache, $adapter_factory_mock, '', ''); $this->dispatcher = new \phpbb_mock_event_dispatcher(); $this->attachment_delete = new \phpbb\attachment\delete($this->config, $this->db, $this->dispatcher, $this->resync, $this->storage); } diff --git a/tests/attachment/upload_test.php b/tests/attachment/upload_test.php index a9567510c8..f1df81bc8a 100644 --- a/tests/attachment/upload_test.php +++ b/tests/attachment/upload_test.php @@ -86,7 +86,9 @@ class phpbb_attachment_upload_test extends \phpbb_database_test_case )); $config = $this->config; $this->db = $this->new_dbal(); + $db_mock = $this->createMock('\phpbb\db\driver\driver_interface'); $this->cache = new \phpbb\cache\service(new \phpbb\cache\driver\dummy(), $this->config, $this->db, $phpbb_root_path, $phpEx); + $cache_mock = $this->createMock('\phpbb\cache\driver\driver_interface'); $this->request = $this->createMock('\phpbb\request\request'); $this->filesystem = new \phpbb\filesystem\filesystem(); @@ -109,7 +111,7 @@ class phpbb_attachment_upload_test extends \phpbb_database_test_case $adapter_factory_mock->expects($this->any()) ->method('get') ->willReturn($adapter); - $this->storage = new \phpbb\storage\storage($adapter_factory_mock, ''); + $this->storage = new \phpbb\storage\storage($db_mock, $cache_mock, $adapter_factory_mock, '', ''); $factory_mock = $this->getMockBuilder('\phpbb\files\factory') ->disableOriginalConstructor() diff --git a/tests/avatar/manager_test.php b/tests/avatar/manager_test.php index 30ef42e835..c390eac883 100644 --- a/tests/avatar/manager_test.php +++ b/tests/avatar/manager_test.php @@ -30,6 +30,8 @@ class phpbb_avatar_manager_test extends \phpbb_database_test_case global $phpbb_root_path, $phpEx; // Mock phpbb_container + + $cache = $this->createMock('\phpbb\cache\driver\driver_interface'); $phpbb_container = $this->createMock('Symfony\Component\DependencyInjection\ContainerInterface'); $phpbb_container->expects($this->any()) ->method('get') @@ -43,11 +45,10 @@ class phpbb_avatar_manager_test extends \phpbb_database_test_case $adapter_factory_mock->expects($this->any()) ->method('get') ->willReturn($adapter); - $storage = new \phpbb\storage\storage($db, $adapter_factory_mock, '', ''); + $storage = new \phpbb\storage\storage($db, $cache, $adapter_factory_mock, '', ''); // Prepare dependencies for avatar manager and driver $this->config = new \phpbb\config\config(array()); - $cache = $this->createMock('\phpbb\cache\driver\driver_interface'); $path_helper = new \phpbb\path_helper( new \phpbb\symfony_request( new phpbb_mock_request() diff --git a/tests/content_visibility/delete_post_test.php b/tests/content_visibility/delete_post_test.php index d4feeaad22..9454c90202 100644 --- a/tests/content_visibility/delete_post_test.php +++ b/tests/content_visibility/delete_post_test.php @@ -304,7 +304,7 @@ class phpbb_content_visibility_delete_post_test extends phpbb_database_test_case $adapter_factory_mock->expects($this->any()) ->method('get') ->willReturn($adapter); - $storage = new \phpbb\storage\storage($adapter_factory_mock, ''); + $storage = $this->createMock('\phpbb\storage\storage'); // Create auth mock $auth = $this->createMock('\phpbb\auth\auth'); diff --git a/tests/functions_user/delete_user_test.php b/tests/functions_user/delete_user_test.php index 489aec01cf..e6610cbabb 100644 --- a/tests/functions_user/delete_user_test.php +++ b/tests/functions_user/delete_user_test.php @@ -35,13 +35,7 @@ class phpbb_functions_user_delete_user_test extends phpbb_database_test_case $phpbb_container = new phpbb_mock_container_builder(); $phpbb_container->set('notification_manager', new phpbb_mock_notification_manager()); - $adapter = new \phpbb\storage\adapter\local(new \phpbb\filesystem\filesystem(), new \FastImageSize\FastImageSize(), new \phpbb\mimetype\guesser(array(new \phpbb\mimetype\extension_guesser)), $phpbb_root_path); - $adapter->configure(['path' => 'files']); - $adapter_factory_mock = $this->createMock('\phpbb\storage\adapter_factory'); - $adapter_factory_mock->expects($this->any()) - ->method('get') - ->willReturn($adapter); - $storage = new \phpbb\storage\storage($db, $adapter_factory_mock, '', ''); + $storage = $this->createMock('\phpbb\storage\storage'); // Works as a workaround for tests $phpbb_container->set('attachment.manager', new \phpbb\attachment\delete($config, $db, new \phpbb_mock_event_dispatcher(), new \phpbb\attachment\resync($db), $storage)); diff --git a/tests/notification/submit_post_base.php b/tests/notification/submit_post_base.php index db65c5df07..255d52bde0 100644 --- a/tests/notification/submit_post_base.php +++ b/tests/notification/submit_post_base.php @@ -56,6 +56,7 @@ abstract class phpbb_notification_submit_post_base extends phpbb_database_test_c // Database $this->db = $this->new_dbal(); $db = $this->db; + $db_mock = $this->createMock('\phpbb\db\driver\driver_interface'); // Auth $auth = $this->createMock('\phpbb\auth\auth'); @@ -92,13 +93,7 @@ abstract class phpbb_notification_submit_post_base extends phpbb_database_test_c $lang = new \phpbb\language\language(new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx)); // Storage - $adapter = new \phpbb\storage\adapter\local(new \phpbb\filesystem\filesystem(), new \FastImageSize\FastImageSize(), new \phpbb\mimetype\guesser(array(new \phpbb\mimetype\extension_guesser)), $phpbb_root_path); - $adapter->configure(['path' => 'files']); - $adapter_factory_mock = $this->createMock('\phpbb\storage\adapter_factory'); - $adapter_factory_mock->expects($this->any()) - ->method('get') - ->willReturn($adapter); - $storage = new \phpbb\storage\storage($db, $adapter_factory_mock, '', ''); + $storage = $this->createMock('\phpbb\storage\storage'); // User $user = $this->createMock('\phpbb\user', array(), array( diff --git a/tests/privmsgs/delete_user_pms_test.php b/tests/privmsgs/delete_user_pms_test.php index b35510113a..50761d72ca 100644 --- a/tests/privmsgs/delete_user_pms_test.php +++ b/tests/privmsgs/delete_user_pms_test.php @@ -87,7 +87,10 @@ class phpbb_privmsgs_delete_user_pms_test extends phpbb_database_test_case { global $db, $phpbb_container, $phpbb_root_path; + $cache = $this->createMock('\phpbb\cache\driver\driver_interface'); + $db = $this->new_dbal(); + $db_mock = $this->createMock('\phpbb\db\driver\driver_interface'); $phpbb_container = new phpbb_mock_container_builder(); $phpbb_container->set('notification_manager', new phpbb_mock_notification_manager()); @@ -98,7 +101,7 @@ class phpbb_privmsgs_delete_user_pms_test extends phpbb_database_test_case $adapter_factory_mock->expects($this->any()) ->method('get') ->willReturn($adapter); - $storage = new \phpbb\storage\storage($adapter_factory_mock, ''); + $storage = new \phpbb\storage\storage($db_mock, $cache, $adapter_factory_mock, '', ''); // Works as a workaround for tests $phpbb_container->set('attachment.manager', new \phpbb\attachment\delete(new \phpbb\config\config(array()), $db, new \phpbb_mock_event_dispatcher(), new \phpbb\attachment\resync($db), $storage)); From a3d949c3d0d596b977a7d38f68e047feab8ff805 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Calvo?= Date: Wed, 20 Jun 2018 23:57:19 +0200 Subject: [PATCH 19/34] [ticket/15342] Fix migration PHPBB3-15342 --- .../db/migration/data/v330/storage_track.php | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/phpBB/phpbb/db/migration/data/v330/storage_track.php b/phpBB/phpbb/db/migration/data/v330/storage_track.php index 0c4f620c28..7463e4641b 100644 --- a/phpBB/phpbb/db/migration/data/v330/storage_track.php +++ b/phpBB/phpbb/db/migration/data/v330/storage_track.php @@ -15,7 +15,7 @@ namespace phpbb\db\migration\data\v330; use phpbb\storage\storage; -class storage_track extends \phpbb\db\migration\migration +class storage_track extends \phpbb\db\migration\container_aware_migration { static public function depends_on() { @@ -74,7 +74,19 @@ class storage_track extends \phpbb\db\migration\migration while ($row = $this->db->sql_fetchrow($result)) { - $storage->track_file($row['user_avatar']); + $avatar_group = false; + $filename = $row['user_avatar']; + + if (isset($filename[0]) && $filename[0] === 'g') + { + $avatar_group = true; + $filename = substr($filename, 1); + } + + $ext = substr(strrchr($filename, '.'), 1); + $filename = (int) $filename; + + $storage->track_file($this->config['avatar_salt'] . '_' . ($avatar_group ? 'g' : '') . $filename . '.' . $ext); } $this->db->sql_freeresult($result); } From 238247b5fc390e1172f31f021deb1aaa937b3484 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Calvo?= Date: Mon, 25 Jun 2018 13:18:27 +0200 Subject: [PATCH 20/34] [ticket/15342] Remove unused variables PHPBB3-15342 --- tests/content_visibility/delete_post_test.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/tests/content_visibility/delete_post_test.php b/tests/content_visibility/delete_post_test.php index 9454c90202..2007aad194 100644 --- a/tests/content_visibility/delete_post_test.php +++ b/tests/content_visibility/delete_post_test.php @@ -298,12 +298,6 @@ class phpbb_content_visibility_delete_post_test extends phpbb_database_test_case $db = $this->new_dbal(); $phpbb_dispatcher = new phpbb_mock_event_dispatcher(); - $adapter = new \phpbb\storage\adapter\local(new \phpbb\filesystem\filesystem(), new \FastImageSize\FastImageSize(), new \phpbb\mimetype\guesser(array(new \phpbb\mimetype\extension_guesser)), $phpbb_root_path); - $adapter->configure(['path' => 'files']); - $adapter_factory_mock = $this->createMock('\phpbb\storage\adapter_factory'); - $adapter_factory_mock->expects($this->any()) - ->method('get') - ->willReturn($adapter); $storage = $this->createMock('\phpbb\storage\storage'); // Create auth mock From db9daf1deac3a9aadef1afd76ca0c72b94dccd04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Calvo?= Date: Mon, 25 Jun 2018 17:35:01 +0200 Subject: [PATCH 21/34] [ticket/15342] Fix docblock PHPBB3-15342 --- phpBB/phpbb/storage/adapter/adapter_interface.php | 2 +- phpBB/phpbb/storage/adapter/local.php | 8 ++------ phpBB/phpbb/storage/storage.php | 11 +++++++++++ 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/phpBB/phpbb/storage/adapter/adapter_interface.php b/phpBB/phpbb/storage/adapter/adapter_interface.php index 96617c8e6a..6584a7255f 100644 --- a/phpBB/phpbb/storage/adapter/adapter_interface.php +++ b/phpBB/phpbb/storage/adapter/adapter_interface.php @@ -99,7 +99,7 @@ interface adapter_interface /* * Get space available in bytes. * - * @throws \phpbb\storage\exception\exception When can't get available space + * @throws \phpbb\storage\exception\exception When unable to retrieve available storage spac * * @return int Returns available space */ diff --git a/phpBB/phpbb/storage/adapter/local.php b/phpBB/phpbb/storage/adapter/local.php index fd0fbe84d8..b23d251ee7 100644 --- a/phpBB/phpbb/storage/adapter/local.php +++ b/phpBB/phpbb/storage/adapter/local.php @@ -425,12 +425,8 @@ class local implements adapter_interface, stream_interface return generate_board_url() . $this->path . $path; } - /* - * Get space available in bytes. - * - * @throws \phpbb\storage\exception\exception When can't get available space - * - * @return int Returns available space + /** + * {@inheritdoc} */ public function free_space() { diff --git a/phpBB/phpbb/storage/storage.php b/phpBB/phpbb/storage/storage.php index 63369af7e8..71d3ea5ed2 100644 --- a/phpBB/phpbb/storage/storage.php +++ b/phpBB/phpbb/storage/storage.php @@ -235,6 +235,12 @@ class storage } } + /** + * Track file into database. + * + * @param string $path The target file + * @param bool $update Update file size when already tracked + */ public function track_file($path, $update = false) { $sql_ary = array( @@ -270,6 +276,11 @@ class storage $this->cache->destroy('_storage_' . $this->get_name() . '_numfiles'); } + /** + * Untrack file. + * + * @param string $path The target file + */ public function untrack_file($path) { $sql_ary = array( From 3ed63388b4e2e1292a38e6870a8d56a7abe9b42b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Calvo?= Date: Mon, 25 Jun 2018 17:40:11 +0200 Subject: [PATCH 22/34] [ticket/15342] Use track_rename when renaming files PHPBB3-15342 --- phpBB/phpbb/storage/storage.php | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/phpBB/phpbb/storage/storage.php b/phpBB/phpbb/storage/storage.php index 71d3ea5ed2..c1f40af7cb 100644 --- a/phpBB/phpbb/storage/storage.php +++ b/phpBB/phpbb/storage/storage.php @@ -163,8 +163,7 @@ class storage public function rename($path_orig, $path_dest) { $this->get_adapter()->rename($path_orig, $path_dest); - $this->untrack_file($path_orig); - $this->track_file($path_dest); + $this->track_rename($path_orig, $path_dest); } /** @@ -296,6 +295,20 @@ class storage $this->cache->destroy('_storage_' . $this->get_name() . '_numfiles'); } + /** + * Rename tracked file. + * + * @param string $path_orig The original file/direcotry + * @param string $path_dest The target file/directory + */ + protected function track_rename($path_orig, $path_dest) + { + $sql = 'UPDATE ' . $this->storage_table . " + SET file_path = '" . $path_dest . "' + WHERE file_path = '" . $path_orig . "'"; + $this->db->sql_query($sql); + } + /** * Get file info. * @@ -345,7 +358,7 @@ class storage $this->db->sql_freeresult($result); } - return $total_size; + return (int) $total_size; } /** From 522ff2f79200b76b9eb34e6327f539aa7c23cf7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Calvo?= Date: Mon, 25 Jun 2018 18:42:45 +0200 Subject: [PATCH 23/34] [ticket/15342] Fix sql condition PHPBB3-15342 --- phpBB/phpbb/storage/storage.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/phpBB/phpbb/storage/storage.php b/phpBB/phpbb/storage/storage.php index c1f40af7cb..75cfac7813 100644 --- a/phpBB/phpbb/storage/storage.php +++ b/phpBB/phpbb/storage/storage.php @@ -305,7 +305,8 @@ class storage { $sql = 'UPDATE ' . $this->storage_table . " SET file_path = '" . $path_dest . "' - WHERE file_path = '" . $path_orig . "'"; + WHERE file_path = '" . $path_orig . "' + AND storage = '" . $this->storage_name . "'"; $this->db->sql_query($sql); } From bdf3a0c913b14b1a9dda8e5f434cb6deabf14670 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Calvo?= Date: Tue, 26 Jun 2018 13:26:19 +0200 Subject: [PATCH 24/34] [ticket/15342] Escape strings in sql querys PHPBB3-15342 --- phpBB/phpbb/storage/adapter/adapter_interface.php | 2 +- phpBB/phpbb/storage/storage.php | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/phpBB/phpbb/storage/adapter/adapter_interface.php b/phpBB/phpbb/storage/adapter/adapter_interface.php index 6584a7255f..3208b5efc8 100644 --- a/phpBB/phpbb/storage/adapter/adapter_interface.php +++ b/phpBB/phpbb/storage/adapter/adapter_interface.php @@ -99,7 +99,7 @@ interface adapter_interface /* * Get space available in bytes. * - * @throws \phpbb\storage\exception\exception When unable to retrieve available storage spac + * @throws \phpbb\storage\exception\exception When unable to retrieve available storage space * * @return int Returns available space */ diff --git a/phpBB/phpbb/storage/storage.php b/phpBB/phpbb/storage/storage.php index 75cfac7813..652b144907 100644 --- a/phpBB/phpbb/storage/storage.php +++ b/phpBB/phpbb/storage/storage.php @@ -304,9 +304,9 @@ class storage protected function track_rename($path_orig, $path_dest) { $sql = 'UPDATE ' . $this->storage_table . " - SET file_path = '" . $path_dest . "' - WHERE file_path = '" . $path_orig . "' - AND storage = '" . $this->storage_name . "'"; + SET file_path = '" . $this->db->sql_escape($path_dest) . "' + WHERE file_path = '" . $this->db->sql_escape($path_orig) . "' + AND storage = '" . $this->db->sql_escape($this->get_name()) . "'"; $this->db->sql_query($sql); } @@ -350,7 +350,7 @@ class storage { $sql = 'SELECT SUM(filesize) AS totalsize FROM ' . $this->storage_table . " - WHERE storage = '" . $this->get_name() . "'"; + WHERE storage = '" . $this->db->sql_escape($this->get_name()) . "'"; $result = $this->db->sql_query($sql); $total_size = (int) $this->db->sql_fetchfield('totalsize'); @@ -375,7 +375,7 @@ class storage { $sql = 'SELECT COUNT(file_id) AS numfiles FROM ' . $this->storage_table . " - WHERE storage = '" . $this->get_name() . "'"; + WHERE storage = '" . $this->db->sql_escape($this->get_name()) . "'"; $result = $this->db->sql_query($sql); $number_files = (int) $this->db->sql_fetchfield('numfiles'); From 0cc77d2c992ec99c2abccacd4a0c7be3927178d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Calvo?= Date: Wed, 27 Jun 2018 17:44:41 +0200 Subject: [PATCH 25/34] [ticket/15342] Check if is resource before close it PHPBB3-15342 --- phpBB/includes/acp/acp_database.php | 5 ++++- phpBB/phpbb/attachment/upload.php | 8 +++++++- phpBB/phpbb/files/filespec_storage.php | 8 +++++++- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/phpBB/includes/acp/acp_database.php b/phpBB/includes/acp/acp_database.php index b6dbc4bec2..cb40ac547e 100644 --- a/phpBB/includes/acp/acp_database.php +++ b/phpBB/includes/acp/acp_database.php @@ -187,7 +187,10 @@ class acp_database $storage->write_stream($file, $fp); - fclose($fp); + if (is_resource($fp)) + { + fclose($fp); + } $storage->track_file($file); diff --git a/phpBB/phpbb/attachment/upload.php b/phpBB/phpbb/attachment/upload.php index 8d4b68c7be..b4e59f5c1f 100644 --- a/phpBB/phpbb/attachment/upload.php +++ b/phpBB/phpbb/attachment/upload.php @@ -246,8 +246,14 @@ class upload { // Move the thumbnail from temp folder to the storage $fp = fopen($destination, 'rb'); + $this->storage->write_stream($destination_name, $fp); - fclose($fp); + + if (is_resource($fp)) + { + fclose($fp); + } + $this->storage->track_file($destination_name); } else diff --git a/phpBB/phpbb/files/filespec_storage.php b/phpBB/phpbb/files/filespec_storage.php index a6870c4a05..49c9618844 100644 --- a/phpBB/phpbb/files/filespec_storage.php +++ b/phpBB/phpbb/files/filespec_storage.php @@ -445,8 +445,14 @@ class filespec_storage try { $fp = fopen($this->filename, 'rb'); + $storage->write_stream($this->destination_file, $fp); - fclose($fp); + + if (is_resource($fp)) + { + fclose($fp); + } + $storage->track_file($this->destination_file); } catch (\phpbb\storage\exception\exception $e) From 6d2aae5853ae15fcb036a88bdc3a539b50e87740 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Calvo?= Date: Fri, 29 Jun 2018 14:01:24 +0200 Subject: [PATCH 26/34] [ticket/15342] Fix docblock PHPBB3-15342 --- phpBB/phpbb/attachment/upload.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpBB/phpbb/attachment/upload.php b/phpBB/phpbb/attachment/upload.php index b4e59f5c1f..592712c643 100644 --- a/phpBB/phpbb/attachment/upload.php +++ b/phpBB/phpbb/attachment/upload.php @@ -333,7 +333,7 @@ class upload /** * Check if there is enough free space available on disk * - * @return bool True if disk space is available or can't get it, false if not + * @return bool True if disk space is available or not limited, false if not */ protected function check_disk_space() { From c039fcba1859de35d8520b347c2fd4298ddb87b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Calvo?= Date: Sun, 1 Jul 2018 18:46:48 +0200 Subject: [PATCH 27/34] [ticket/15342] free_space() now return false if there is any error PHPBB3-15342 --- phpBB/phpbb/storage/adapter/adapter_interface.php | 4 +--- phpBB/phpbb/storage/adapter/local.php | 7 +------ phpBB/phpbb/storage/storage.php | 6 +++--- 3 files changed, 5 insertions(+), 12 deletions(-) diff --git a/phpBB/phpbb/storage/adapter/adapter_interface.php b/phpBB/phpbb/storage/adapter/adapter_interface.php index 3208b5efc8..66de8c7dc7 100644 --- a/phpBB/phpbb/storage/adapter/adapter_interface.php +++ b/phpBB/phpbb/storage/adapter/adapter_interface.php @@ -99,9 +99,7 @@ interface adapter_interface /* * Get space available in bytes. * - * @throws \phpbb\storage\exception\exception When unable to retrieve available storage space - * - * @return int Returns available space + * @return mixed Returns available space or null when unable to retrieve available space */ public function free_space(); } diff --git a/phpBB/phpbb/storage/adapter/local.php b/phpBB/phpbb/storage/adapter/local.php index b23d251ee7..3cde5a4fbb 100644 --- a/phpBB/phpbb/storage/adapter/local.php +++ b/phpBB/phpbb/storage/adapter/local.php @@ -432,11 +432,6 @@ class local implements adapter_interface, stream_interface { $free_space = @disk_free_space($this->root_path); - if ($free_space === false) - { - throw new exception('STORAGE_CANNOT_GET_FREE_SPACE'); - } - - return (int) $free_space; + return $free_space; } } diff --git a/phpBB/phpbb/storage/storage.php b/phpBB/phpbb/storage/storage.php index 652b144907..f79b5bc32a 100644 --- a/phpBB/phpbb/storage/storage.php +++ b/phpBB/phpbb/storage/storage.php @@ -43,7 +43,7 @@ class storage protected $factory; /** - * @var string + * @var stringshould be caste */ protected $storage_name; @@ -384,7 +384,7 @@ class storage $this->db->sql_freeresult($result); } - return $number_files; + return (int) $number_files; } /** @@ -392,7 +392,7 @@ class storage * * @throws \phpbb\storage\exception\exception When can't get available space * - * @return int Returns available space + * @return mixed Returns available space or null when unable to retrieve available space */ public function free_space() { From 7d7217c38187bfa5b644d49a061e18533520cf5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Calvo?= Date: Sun, 1 Jul 2018 19:20:20 +0200 Subject: [PATCH 28/34] [ticket/15342] Move call to remove file PHPBB3-15342 --- phpBB/phpbb/attachment/upload.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/phpBB/phpbb/attachment/upload.php b/phpBB/phpbb/attachment/upload.php index 592712c643..1f3c8c762e 100644 --- a/phpBB/phpbb/attachment/upload.php +++ b/phpBB/phpbb/attachment/upload.php @@ -199,6 +199,7 @@ class upload // Check for attachment quota and free space if (!$this->check_attach_quota() || !$this->check_disk_space()) { + $this->file->remove($this->storage); return $this->file_data; } @@ -321,8 +322,6 @@ class upload $this->file_data['error'][] = $this->language->lang('ATTACH_QUOTA_REACHED'); $this->file_data['post_attach'] = false; - $this->file->remove($this->storage); - return false; } } @@ -353,8 +352,6 @@ class upload } $this->file_data['post_attach'] = false; - $this->file->remove($this->storage); - return false; } } From 78c86746438a17215ea24d0e4cfca8e34f8e1ef3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Calvo?= Date: Sun, 1 Jul 2018 21:08:23 +0200 Subject: [PATCH 29/34] [ticket/15342] Fix check_disk_space PHPBB3-15342 --- phpBB/phpbb/attachment/upload.php | 31 ++++++++++++------------------- 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/phpBB/phpbb/attachment/upload.php b/phpBB/phpbb/attachment/upload.php index 1f3c8c762e..3fa6544353 100644 --- a/phpBB/phpbb/attachment/upload.php +++ b/phpBB/phpbb/attachment/upload.php @@ -336,28 +336,21 @@ class upload */ protected function check_disk_space() { - try - { - $free_space = $this->storage->free_space(); + $free_space = $this->storage->free_space(); - if ($free_space <= $this->file->get('filesize')) + if ($free_space !== false && $free_space <= $this->file->get('filesize')) + { + if ($this->auth->acl_get('a_')) { - if ($this->auth->acl_get('a_')) - { - $this->file_data['error'][] = $this->language->lang('ATTACH_DISK_FULL'); - } - else - { - $this->file_data['error'][] = $this->language->lang('ATTACH_QUOTA_REACHED'); - } - $this->file_data['post_attach'] = false; - - return false; + $this->file_data['error'][] = $this->language->lang('ATTACH_DISK_FULL'); } - } - catch (\phpbb\storage\exception\exception $e) - { - // Nothing + else + { + $this->file_data['error'][] = $this->language->lang('ATTACH_QUOTA_REACHED'); + } + $this->file_data['post_attach'] = false; + + return false; } return true; From 0e0e157e871e31f4e07ef5f7f821bc026cd13ed9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Calvo?= Date: Sun, 1 Jul 2018 21:41:14 +0200 Subject: [PATCH 30/34] [ticket/15342] If the file haven't been moved, remove it from the filesystem PHPBB3-15342 --- phpBB/phpbb/files/filespec_storage.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/phpBB/phpbb/files/filespec_storage.php b/phpBB/phpbb/files/filespec_storage.php index 49c9618844..5f3b895a05 100644 --- a/phpBB/phpbb/files/filespec_storage.php +++ b/phpBB/phpbb/files/filespec_storage.php @@ -288,6 +288,10 @@ class filespec_storage { $storage->delete($this->destination_file); } + else + { + @unlink($this->filename); + } } /** From 117d9e69cece6b07c1ac064981339194347229d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Calvo?= Date: Mon, 9 Jul 2018 01:56:31 +0200 Subject: [PATCH 31/34] [ticket/15342] Fix comments PHPBB3-15342 --- phpBB/phpbb/storage/adapter/adapter_interface.php | 2 +- phpBB/phpbb/storage/storage.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/phpBB/phpbb/storage/adapter/adapter_interface.php b/phpBB/phpbb/storage/adapter/adapter_interface.php index 66de8c7dc7..a52585a42f 100644 --- a/phpBB/phpbb/storage/adapter/adapter_interface.php +++ b/phpBB/phpbb/storage/adapter/adapter_interface.php @@ -99,7 +99,7 @@ interface adapter_interface /* * Get space available in bytes. * - * @return mixed Returns available space or null when unable to retrieve available space + * @return mixed Returns available space or false when unable to retrieve available space */ public function free_space(); } diff --git a/phpBB/phpbb/storage/storage.php b/phpBB/phpbb/storage/storage.php index f79b5bc32a..ad5d0ed240 100644 --- a/phpBB/phpbb/storage/storage.php +++ b/phpBB/phpbb/storage/storage.php @@ -43,7 +43,7 @@ class storage protected $factory; /** - * @var stringshould be caste + * @var string */ protected $storage_name; @@ -392,7 +392,7 @@ class storage * * @throws \phpbb\storage\exception\exception When can't get available space * - * @return mixed Returns available space or null when unable to retrieve available space + * @return mixed Returns available space or false when unable to retrieve available space */ public function free_space() { From e5d8b1600b8af932172396f6ec38cb771032a94b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Calvo?= Date: Mon, 9 Jul 2018 15:27:16 +0200 Subject: [PATCH 32/34] [ticket/15342] free_space return float and throws exception PHPBB3-15342 --- phpBB/phpbb/storage/adapter/adapter_interface.php | 4 +++- phpBB/phpbb/storage/adapter/local.php | 5 +++++ phpBB/phpbb/storage/storage.php | 5 ++--- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/phpBB/phpbb/storage/adapter/adapter_interface.php b/phpBB/phpbb/storage/adapter/adapter_interface.php index a52585a42f..fa824703b7 100644 --- a/phpBB/phpbb/storage/adapter/adapter_interface.php +++ b/phpBB/phpbb/storage/adapter/adapter_interface.php @@ -99,7 +99,9 @@ interface adapter_interface /* * Get space available in bytes. * - * @return mixed Returns available space or false when unable to retrieve available space + * @throws \phpbb\storage\exception\exception When unable to retrieve available storage space + * + * @return float Returns available space */ public function free_space(); } diff --git a/phpBB/phpbb/storage/adapter/local.php b/phpBB/phpbb/storage/adapter/local.php index 3cde5a4fbb..fbbb92fb17 100644 --- a/phpBB/phpbb/storage/adapter/local.php +++ b/phpBB/phpbb/storage/adapter/local.php @@ -432,6 +432,11 @@ class local implements adapter_interface, stream_interface { $free_space = @disk_free_space($this->root_path); + if ($free_space === false) + { + throw new exception('STORAGE_CANNOT_GET_FREE_SPACE'); + } + return $free_space; } } diff --git a/phpBB/phpbb/storage/storage.php b/phpBB/phpbb/storage/storage.php index ad5d0ed240..7e84e49e62 100644 --- a/phpBB/phpbb/storage/storage.php +++ b/phpBB/phpbb/storage/storage.php @@ -390,13 +390,12 @@ class storage /** * Get space available in bytes. * - * @throws \phpbb\storage\exception\exception When can't get available space + * @throws \phpbb\storage\exception\exception When unable to retrieve available storage space * - * @return mixed Returns available space or false when unable to retrieve available space + * @return float Returns available space */ public function free_space() { return $this->get_adapter()->free_space(); } - } From af7e3662b834ac41131467464b5952627a36d6a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Calvo?= Date: Tue, 10 Jul 2018 06:59:52 +0200 Subject: [PATCH 33/34] [ticket/15342] Catch exception in check_disk_space PHPBB3-15342 --- phpBB/phpbb/attachment/upload.php | 34 +++++++++++++++++++------------ 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/phpBB/phpbb/attachment/upload.php b/phpBB/phpbb/attachment/upload.php index 3fa6544353..6202798e05 100644 --- a/phpBB/phpbb/attachment/upload.php +++ b/phpBB/phpbb/attachment/upload.php @@ -336,21 +336,29 @@ class upload */ protected function check_disk_space() { - $free_space = $this->storage->free_space(); - - if ($free_space !== false && $free_space <= $this->file->get('filesize')) + try { - if ($this->auth->acl_get('a_')) - { - $this->file_data['error'][] = $this->language->lang('ATTACH_DISK_FULL'); - } - else - { - $this->file_data['error'][] = $this->language->lang('ATTACH_QUOTA_REACHED'); - } - $this->file_data['post_attach'] = false; + $free_space = $this->storage->free_space(); - return false; + if ($free_space <= $this->file->get('filesize')) + { + if ($this->auth->acl_get('a_')) + { + $this->file_data['error'][] = $this->language->lang('ATTACH_DISK_FULL'); + } + else + { + $this->file_data['error'][] = $this->language->lang('ATTACH_QUOTA_REACHED'); + } + + $this->file_data['post_attach'] = false; + + return false; + } + } + catch (\phpbb\storage\exception\exception $e) + { + // Do nothing } return true; From e40daea4bdec4bf4f39efdfe166428b0834fc261 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Calvo?= Date: Thu, 12 Jul 2018 15:56:17 +0200 Subject: [PATCH 34/34] [ticket/15342] Update comments PHPBB3-15342 --- .../storage/adapter/adapter_interface.php | 14 ++++----- phpBB/phpbb/storage/adapter/local.php | 10 +++---- phpBB/phpbb/storage/exception/exception.php | 10 +++---- phpBB/phpbb/storage/file_info.php | 4 +-- .../storage/provider/provider_interface.php | 8 ++--- phpBB/phpbb/storage/storage.php | 30 +++++++++---------- phpBB/phpbb/storage/stream_interface.php | 4 +-- 7 files changed, 40 insertions(+), 40 deletions(-) diff --git a/phpBB/phpbb/storage/adapter/adapter_interface.php b/phpBB/phpbb/storage/adapter/adapter_interface.php index fa824703b7..6d8b561c25 100644 --- a/phpBB/phpbb/storage/adapter/adapter_interface.php +++ b/phpBB/phpbb/storage/adapter/adapter_interface.php @@ -23,7 +23,7 @@ interface adapter_interface public function configure($options); /** - * Dumps content into a file. + * Dumps content into a file * * @param string path The file to be written to. * @param string content The data to write into the file. @@ -47,7 +47,7 @@ interface adapter_interface public function get_contents($path); /** - * Checks the existence of files or directories. + * Checks the existence of files or directories * * @param string $path file/directory to check * @@ -56,7 +56,7 @@ interface adapter_interface public function exists($path); /** - * Removes files or directories. + * Removes files or directories * * @param string $path file/directory to remove * @@ -65,7 +65,7 @@ interface adapter_interface public function delete($path); /** - * Rename a file or a directory. + * Rename a file or a directory * * @param string $path_orig The original file/direcotry * @param string $path_dest The target file/directory @@ -76,7 +76,7 @@ interface adapter_interface public function rename($path_orig, $path_dest); /** - * Copies a file. + * Copies a file * * @param string $path_orig The original filename * @param string $path_dest The target filename @@ -87,7 +87,7 @@ interface adapter_interface public function copy($path_orig, $path_dest); /** - * Get direct link. + * Get direct link * * @param string $path The file * @@ -97,7 +97,7 @@ interface adapter_interface public function get_link($path); /* - * Get space available in bytes. + * Get space available in bytes * * @throws \phpbb\storage\exception\exception When unable to retrieve available storage space * diff --git a/phpBB/phpbb/storage/adapter/local.php b/phpBB/phpbb/storage/adapter/local.php index fbbb92fb17..c4a95da25b 100644 --- a/phpBB/phpbb/storage/adapter/local.php +++ b/phpBB/phpbb/storage/adapter/local.php @@ -340,7 +340,7 @@ class local implements adapter_interface, stream_interface } /** - * Get file size. + * Get file size * * @param string $path The file * @@ -361,7 +361,7 @@ class local implements adapter_interface, stream_interface } /** - * Get file mimetype. + * Get file mimetype * * @param string $path The file * @@ -373,7 +373,7 @@ class local implements adapter_interface, stream_interface } /** - * Get image dimensions. + * Get image dimensions * * @param string $path The file * @@ -394,7 +394,7 @@ class local implements adapter_interface, stream_interface } /** - * Get image width. + * Get image width * * @param string $path The file * @@ -406,7 +406,7 @@ class local implements adapter_interface, stream_interface } /** - * Get image height. + * Get image height * * @param string $path The file * diff --git a/phpBB/phpbb/storage/exception/exception.php b/phpBB/phpbb/storage/exception/exception.php index 4eca403cc1..3a587bea3f 100644 --- a/phpBB/phpbb/storage/exception/exception.php +++ b/phpBB/phpbb/storage/exception/exception.php @@ -20,11 +20,11 @@ class exception extends runtime_exception /** * Constructor * - * @param string $message The Exception message to throw (must be a language variable). - * @param string $filename The file that caused the error. - * @param array $parameters The parameters to use with the language var. - * @param \Exception $previous The previous runtime_exception used for the runtime_exception chaining. - * @param integer $code The Exception code. + * @param string $message The Exception message to throw (must be a language variable) + * @param string $filename The file that caused the error + * @param array $parameters The parameters to use with the language var + * @param \Exception $previous The previous runtime_exception used for the runtime_exception chaining + * @param integer $code The Exception code */ public function __construct($message = '', $filename = '', $parameters = [], \Exception $previous = null, $code = 0) { diff --git a/phpBB/phpbb/storage/file_info.php b/phpBB/phpbb/storage/file_info.php index ae2bd5169d..6b10e6892a 100644 --- a/phpBB/phpbb/storage/file_info.php +++ b/phpBB/phpbb/storage/file_info.php @@ -34,7 +34,7 @@ class file_info * Stores the properties of $path file, so dont have to be consulted multiple times. * For example, when you need the width of an image, using getimagesize() you get * both dimensions, so you store both here, and when you get the height, you dont have - * to call getimagesize() again. + * to call getimagesize() again * * @var array */ @@ -54,7 +54,7 @@ class file_info } /** - * Load propertys lazily. + * Load propertys lazily * * @param string name The property name. * diff --git a/phpBB/phpbb/storage/provider/provider_interface.php b/phpBB/phpbb/storage/provider/provider_interface.php index a61845bccc..428b6eb187 100644 --- a/phpBB/phpbb/storage/provider/provider_interface.php +++ b/phpBB/phpbb/storage/provider/provider_interface.php @@ -16,28 +16,28 @@ namespace phpbb\storage\provider; interface provider_interface { /** - * Gets adapter name. + * Gets adapter name * * @return string */ public function get_name(); /** - * Gets adapter class. + * Gets adapter class * * @return \phpbb\storage\adapter\adapter_interface */ public function get_adapter_class(); /** - * Gets adapter options. + * Gets adapter options * * @return array Configuration keys */ public function get_options(); /** - * Return true if the adapter is available. + * Return true if the adapter is available * * @return bool */ diff --git a/phpBB/phpbb/storage/storage.php b/phpBB/phpbb/storage/storage.php index 7e84e49e62..97d71048ee 100644 --- a/phpBB/phpbb/storage/storage.php +++ b/phpBB/phpbb/storage/storage.php @@ -96,7 +96,7 @@ class storage } /** - * Dumps content into a file. + * Dumps content into a file * * @param string path The file to be written to. * @param string content The data to write into the file. @@ -127,7 +127,7 @@ class storage } /** - * Checks the existence of files or directories. + * Checks the existence of files or directories * * @param string $path file/directory to check * @@ -139,7 +139,7 @@ class storage } /** - * Removes files or directories. + * Removes files or directories * * @param string $path file/directory to remove * @@ -152,7 +152,7 @@ class storage } /** - * Rename a file or a directory. + * Rename a file or a directory * * @param string $path_orig The original file/direcotry * @param string $path_dest The target file/directory @@ -167,7 +167,7 @@ class storage } /** - * Copies a file. + * Copies a file * * @param string $path_orig The original filename * @param string $path_dest The target filename @@ -182,7 +182,7 @@ class storage } /** - * Reads a file as a stream. + * Reads a file as a stream * * @param string $path File to read * @@ -211,8 +211,8 @@ class storage } /** - * Writes a new file using a stream. - * You have to track file after use this method + * Writes a new file using a stream + * The file needs to be tracked after using this method * * @param string $path The target file * @param resource $resource The resource @@ -235,7 +235,7 @@ class storage } /** - * Track file into database. + * Track file in database * * @param string $path The target file * @param bool $update Update file size when already tracked @@ -276,7 +276,7 @@ class storage } /** - * Untrack file. + * Untrack file * * @param string $path The target file */ @@ -296,7 +296,7 @@ class storage } /** - * Rename tracked file. + * Rename tracked file * * @param string $path_orig The original file/direcotry * @param string $path_dest The target file/directory @@ -311,7 +311,7 @@ class storage } /** - * Get file info. + * Get file info * * @param string $path The file * @@ -338,7 +338,7 @@ class storage } /** - * Get total storage size. + * Get total storage size * * @return int Size in bytes */ @@ -363,7 +363,7 @@ class storage } /** - * Get number of storage files. + * Get number of storage files * * @return int Number of files */ @@ -388,7 +388,7 @@ class storage } /** - * Get space available in bytes. + * Get space available in bytes * * @throws \phpbb\storage\exception\exception When unable to retrieve available storage space * diff --git a/phpBB/phpbb/storage/stream_interface.php b/phpBB/phpbb/storage/stream_interface.php index 0ba866777a..6d0af71550 100644 --- a/phpBB/phpbb/storage/stream_interface.php +++ b/phpBB/phpbb/storage/stream_interface.php @@ -16,7 +16,7 @@ namespace phpbb\storage; interface stream_interface { /** - * Reads a file as a stream. + * Reads a file as a stream * * @param string $path File to read * @@ -27,7 +27,7 @@ interface stream_interface public function read_stream($path); /** - * Writes a new file using a stream. + * Writes a new file using a stream * * @param string $path The target file * @param resource $resource The resource