From d1030450f5f2ddca88f1ef17cc7b949541e64bd8 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Mon, 21 Sep 2015 08:54:57 +0200 Subject: [ticket/14168] Move function for attachment deletion into class PHPBB3-14168 --- phpBB/phpbb/attachment/delete.php | 398 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 398 insertions(+) create mode 100644 phpBB/phpbb/attachment/delete.php (limited to 'phpBB/phpbb/attachment/delete.php') diff --git a/phpBB/phpbb/attachment/delete.php b/phpBB/phpbb/attachment/delete.php new file mode 100644 index 0000000000..a2fe1a8cdf --- /dev/null +++ b/phpBB/phpbb/attachment/delete.php @@ -0,0 +1,398 @@ + + * @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\attachment; + +use \phpbb\config\config; +use \phpbb\db\driver\driver_interface; +use \phpbb\event\dispatcher; + +/** + * Attachment delete class + */ + +class delete +{ + /** @var \phpbb\config\config */ + protected $config; + + /** @var \phpbb\db\driver\driver_interface */ + protected $db; + + /** @var \phpbb\event\dispatcher */ + protected $dispatcher; + + /** @var array Attachement IDs */ + protected $ids; + + /** + * Attachment delete class constructor + * + * @param config $config + * @param driver_interface $db + * @param dispatcher>>>>>>> 85b6020... [ticket/14168] Move function for attachment deletion into class + */ + public function __construct(config $config, driver_interface $db, dispatcher $dispatcher) + { + $this->config = $config; + $this->db = $db; + $this->dispatcher = $dispatcher; + } + + /** + * Delete Attachments + * + * @param string $mode can be: post|message|topic|attach|user + * @param mixed $ids can be: post_ids, message_ids, topic_ids, attach_ids, user_ids + * @param bool $resync set this to false if you are deleting posts or topics + * + * @return int|bool Number of deleted attachments or false if something + * went wrong during attachment deletion + */ + function delete($mode, $ids, $resync = true) + { + if (!$this->set_attachment_ids($ids)) + { + return false; + } + + $sql_where = ''; + + switch ($mode) + { + case 'post': + case 'message': + $sql_id = 'post_msg_id'; + $sql_where = ' AND in_message = ' . ($mode == 'message' ? 1 : 0); + break; + + case 'topic': + $sql_id = 'topic_id'; + break; + + case 'user': + $sql_id = 'poster_id'; + break; + + case 'attach': + default: + $sql_id = 'attach_id'; + break; + } + + $post_ids = $message_ids = $topic_ids = $physical = array(); + + /** + * Perform additional actions before collecting data for attachment(s) deletion + * + * @event core.delete_attachments_collect_data_before + * @var string mode Variable containing attachments deletion mode, can be: post|message|topic|attach|user + * @var mixed ids Array or comma separated list of ids corresponding to the mode + * @var bool resync Flag indicating if posts/messages/topics should be synchronized + * @var string sql_id The field name to collect/delete data for depending on the mode + * @since 3.1.7-RC1 + */ + $vars = array( + 'mode', + 'ids', + 'resync', + 'sql_id', + ); + extract($this->dispatcher->trigger_event('core.delete_attachments_collect_data_before', compact($vars))); + + // Collect post and topic ids for later use if we need to touch remaining entries (if resync is enabled) + $sql = 'SELECT post_msg_id, topic_id, in_message, physical_filename, thumbnail, filesize, is_orphan + FROM ' . ATTACHMENTS_TABLE . ' + WHERE ' . $this->db->sql_in_set($sql_id, $ids); + + $sql .= $sql_where; + + $result = $this->db->sql_query($sql); + + while ($row = $this->db->sql_fetchrow($result)) + { + // We only need to store post/message/topic ids if resync is enabled and the file is not orphaned + if ($resync && !$row['is_orphan']) + { + if (!$row['in_message']) + { + $post_ids[] = $row['post_msg_id']; + $topic_ids[] = $row['topic_id']; + } + else + { + $message_ids[] = $row['post_msg_id']; + } + } + + $physical[] = array('filename' => $row['physical_filename'], 'thumbnail' => $row['thumbnail'], 'filesize' => $row['filesize'], 'is_orphan' => $row['is_orphan']); + } + $this->db->sql_freeresult($result); + + /** + * Perform additional actions before attachment(s) deletion + * + * @event core.delete_attachments_before + * @var string mode Variable containing attachments deletion mode, can be: post|message|topic|attach|user + * @var mixed ids Array or comma separated list of ids corresponding to the mode + * @var bool resync Flag indicating if posts/messages/topics should be synchronized + * @var string sql_id The field name to collect/delete data for depending on the mode + * @var array post_ids Array with post ids for deleted attachment(s) + * @var array topic_ids Array with topic ids for deleted attachment(s) + * @var array message_ids Array with private message ids for deleted attachment(s) + * @var array physical Array with deleted attachment(s) physical file(s) data + * @since 3.1.7-RC1 + */ + $vars = array( + 'mode', + 'ids', + 'resync', + 'sql_id', + 'post_ids', + 'topic_ids', + 'message_ids', + 'physical', + ); + extract($this->dispatcher->trigger_event('core.delete_attachments_before', compact($vars))); + + // Delete attachments + $sql = 'DELETE FROM ' . ATTACHMENTS_TABLE . ' + WHERE ' . $this->db->sql_in_set($sql_id, $ids); + + $sql .= $sql_where; + + $this->db->sql_query($sql); + $num_deleted = $this->db->sql_affectedrows(); + + /** + * Perform additional actions after attachment(s) deletion from the database + * + * @event core.delete_attachments_from_database_after + * @var string mode Variable containing attachments deletion mode, can be: post|message|topic|attach|user + * @var mixed ids Array or comma separated list of ids corresponding to the mode + * @var bool resync Flag indicating if posts/messages/topics should be synchronized + * @var string sql_id The field name to collect/delete data for depending on the mode + * @var array post_ids Array with post ids for deleted attachment(s) + * @var array topic_ids Array with topic ids for deleted attachment(s) + * @var array message_ids Array with private message ids for deleted attachment(s) + * @var array physical Array with deleted attachment(s) physical file(s) data + * @var int num_deleted The number of deleted attachment(s) from the database + * @since 3.1.7-RC1 + */ + $vars = array( + 'mode', + 'ids', + 'resync', + 'sql_id', + 'post_ids', + 'topic_ids', + 'message_ids', + 'physical', + 'num_deleted', + ); + extract($this->dispatcher->trigger_event('core.delete_attachments_from_database_after', compact($vars))); + + if (!$num_deleted) + { + return 0; + } + + // Delete attachments from filesystem + $space_removed = $files_removed = 0; + foreach ($physical as $file_ary) + { + if (phpbb_unlink($file_ary['filename'], 'file', true) && !$file_ary['is_orphan']) + { + // Only non-orphaned files count to the file size + $space_removed += $file_ary['filesize']; + $files_removed++; + } + + if ($file_ary['thumbnail']) + { + phpbb_unlink($file_ary['filename'], 'thumbnail', true); + } + } + + /** + * Perform additional actions after attachment(s) deletion from the filesystem + * + * @event core.delete_attachments_from_filesystem_after + * @var string mode Variable containing attachments deletion mode, can be: post|message|topic|attach|user + * @var mixed ids Array or comma separated list of ids corresponding to the mode + * @var bool resync Flag indicating if posts/messages/topics should be synchronized + * @var string sql_id The field name to collect/delete data for depending on the mode + * @var array post_ids Array with post ids for deleted attachment(s) + * @var array topic_ids Array with topic ids for deleted attachment(s) + * @var array message_ids Array with private message ids for deleted attachment(s) + * @var array physical Array with deleted attachment(s) physical file(s) data + * @var int num_deleted The number of deleted attachment(s) from the database + * @var int space_removed The size of deleted files(s) from the filesystem + * @var int files_removed The number of deleted file(s) from the filesystem + * @since 3.1.7-RC1 + */ + $vars = array( + 'mode', + 'ids', + 'resync', + 'sql_id', + 'post_ids', + 'topic_ids', + 'message_ids', + 'physical', + 'num_deleted', + 'space_removed', + 'files_removed', + ); + extract($this->dispatcher->trigger_event('core.delete_attachments_from_filesystem_after', compact($vars))); + + if ($space_removed || $files_removed) + { + $this->config->increment('upload_dir_size', $space_removed * (-1), false); + $this->config->increment('num_files', $files_removed * (-1), false); + } + + // If we do not resync, we do not need to adjust any message, post, topic or user entries + if (!$resync) + { + return $num_deleted; + } + + // No more use for the original ids + unset($ids); + + // Now, we need to resync posts, messages, topics. We go through every one of them + $post_ids = array_unique($post_ids); + $message_ids = array_unique($message_ids); + $topic_ids = array_unique($topic_ids); + + // Update post indicators for posts now no longer having attachments + if (sizeof($post_ids)) + { + // Just check which posts are still having an assigned attachment not orphaned by querying the attachments table + $sql = 'SELECT post_msg_id + FROM ' . ATTACHMENTS_TABLE . ' + WHERE ' . $this->db->sql_in_set('post_msg_id', $post_ids) . ' + AND in_message = 0 + AND is_orphan = 0'; + $result = $this->db->sql_query($sql); + + $remaining_ids = array(); + while ($row = $this->db->sql_fetchrow($result)) + { + $remaining_ids[] = $row['post_msg_id']; + } + $this->db->sql_freeresult($result); + + // Now only unset those ids remaining + $post_ids = array_diff($post_ids, $remaining_ids); + + if (sizeof($post_ids)) + { + $sql = 'UPDATE ' . POSTS_TABLE . ' + SET post_attachment = 0 + WHERE ' . $this->db->sql_in_set('post_id', $post_ids); + $this->db->sql_query($sql); + } + } + + // Update message table if messages are affected + if (sizeof($message_ids)) + { + // Just check which messages are still having an assigned attachment not orphaned by querying the attachments table + $sql = 'SELECT post_msg_id + FROM ' . ATTACHMENTS_TABLE . ' + WHERE ' . $this->db->sql_in_set('post_msg_id', $message_ids) . ' + AND in_message = 1 + AND is_orphan = 0'; + $result = $this->db->sql_query($sql); + + $remaining_ids = array(); + while ($row = $this->db->sql_fetchrow($result)) + { + $remaining_ids[] = $row['post_msg_id']; + } + $this->db->sql_freeresult($result); + + // Now only unset those ids remaining + $message_ids = array_diff($message_ids, $remaining_ids); + + if (sizeof($message_ids)) + { + $sql = 'UPDATE ' . PRIVMSGS_TABLE . ' + SET message_attachment = 0 + WHERE ' . $this->db->sql_in_set('msg_id', $message_ids); + $this->db->sql_query($sql); + } + } + + // Now update the topics. This is a bit trickier, because there could be posts still having attachments within the topic + if (sizeof($topic_ids)) + { + // Just check which topics are still having an assigned attachment not orphaned by querying the attachments table (much less entries expected) + $sql = 'SELECT topic_id + FROM ' . ATTACHMENTS_TABLE . ' + WHERE ' . $this->db->sql_in_set('topic_id', $topic_ids) . ' + AND is_orphan = 0'; + $result = $this->db->sql_query($sql); + + $remaining_ids = array(); + while ($row = $this->db->sql_fetchrow($result)) + { + $remaining_ids[] = $row['topic_id']; + } + $this->db->sql_freeresult($result); + + // Now only unset those ids remaining + $topic_ids = array_diff($topic_ids, $remaining_ids); + + if (sizeof($topic_ids)) + { + $sql = 'UPDATE ' . TOPICS_TABLE . ' + SET topic_attachment = 0 + WHERE ' . $this->db->sql_in_set('topic_id', $topic_ids); + $this->db->sql_query($sql); + } + } + + return $num_deleted; + } + + /** + * Set attachment IDs + * + * @param array $ids + * + * @return bool True if attachment IDs were set, false if not + */ + protected function set_attachment_ids($ids) + { + // 0 is as bad as an empty array + if (empty($ids)) + { + return false; + } + + if (is_array($ids)) + { + $ids = array_unique($ids); + $this->ids = array_map('intval', $ids); + } + else + { + $this->ids = array((int) $ids); + } + + return true; + } +} \ No newline at end of file -- cgit v1.2.1 From ebfdd69525cc10446fba6c6e8600b44e1124a64d Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Mon, 21 Sep 2015 10:28:09 +0200 Subject: [ticket/14168] Further split up attachment delete class PHPBB3-14168 --- phpBB/phpbb/attachment/delete.php | 379 ++++++++++++++++++++++---------------- 1 file changed, 218 insertions(+), 161 deletions(-) (limited to 'phpBB/phpbb/attachment/delete.php') diff --git a/phpBB/phpbb/attachment/delete.php b/phpBB/phpbb/attachment/delete.php index a2fe1a8cdf..7a168c2243 100644 --- a/phpBB/phpbb/attachment/delete.php +++ b/phpBB/phpbb/attachment/delete.php @@ -35,6 +35,27 @@ class delete /** @var array Attachement IDs */ protected $ids; + /** @var string SQL ID string */ + private $sql_id; + + /** @var string SQL where string */ + private $sql_where = ''; + + /** @var int Number of deleted items */ + private $num_deleted; + + /** @var array Post IDs */ + private $post_ids = array(); + + /** @var array Message IDs */ + private $message_ids = array(); + + /** @var array Topic IDs */ + private $topic_ids = array(); + + /** @var array Info of physical file */ + private $physical = array(); + /** * Attachment delete class constructor * @@ -66,31 +87,7 @@ class delete return false; } - $sql_where = ''; - - switch ($mode) - { - case 'post': - case 'message': - $sql_id = 'post_msg_id'; - $sql_where = ' AND in_message = ' . ($mode == 'message' ? 1 : 0); - break; - - case 'topic': - $sql_id = 'topic_id'; - break; - - case 'user': - $sql_id = 'poster_id'; - break; - - case 'attach': - default: - $sql_id = 'attach_id'; - break; - } - - $post_ids = $message_ids = $topic_ids = $physical = array(); + $this->set_sql_constraints($mode); /** * Perform additional actions before collecting data for attachment(s) deletion @@ -111,68 +108,10 @@ class delete extract($this->dispatcher->trigger_event('core.delete_attachments_collect_data_before', compact($vars))); // Collect post and topic ids for later use if we need to touch remaining entries (if resync is enabled) - $sql = 'SELECT post_msg_id, topic_id, in_message, physical_filename, thumbnail, filesize, is_orphan - FROM ' . ATTACHMENTS_TABLE . ' - WHERE ' . $this->db->sql_in_set($sql_id, $ids); - - $sql .= $sql_where; - - $result = $this->db->sql_query($sql); - - while ($row = $this->db->sql_fetchrow($result)) - { - // We only need to store post/message/topic ids if resync is enabled and the file is not orphaned - if ($resync && !$row['is_orphan']) - { - if (!$row['in_message']) - { - $post_ids[] = $row['post_msg_id']; - $topic_ids[] = $row['topic_id']; - } - else - { - $message_ids[] = $row['post_msg_id']; - } - } - - $physical[] = array('filename' => $row['physical_filename'], 'thumbnail' => $row['thumbnail'], 'filesize' => $row['filesize'], 'is_orphan' => $row['is_orphan']); - } - $this->db->sql_freeresult($result); - - /** - * Perform additional actions before attachment(s) deletion - * - * @event core.delete_attachments_before - * @var string mode Variable containing attachments deletion mode, can be: post|message|topic|attach|user - * @var mixed ids Array or comma separated list of ids corresponding to the mode - * @var bool resync Flag indicating if posts/messages/topics should be synchronized - * @var string sql_id The field name to collect/delete data for depending on the mode - * @var array post_ids Array with post ids for deleted attachment(s) - * @var array topic_ids Array with topic ids for deleted attachment(s) - * @var array message_ids Array with private message ids for deleted attachment(s) - * @var array physical Array with deleted attachment(s) physical file(s) data - * @since 3.1.7-RC1 - */ - $vars = array( - 'mode', - 'ids', - 'resync', - 'sql_id', - 'post_ids', - 'topic_ids', - 'message_ids', - 'physical', - ); - extract($this->dispatcher->trigger_event('core.delete_attachments_before', compact($vars))); + $this->collect_attachment_info($resync); - // Delete attachments - $sql = 'DELETE FROM ' . ATTACHMENTS_TABLE . ' - WHERE ' . $this->db->sql_in_set($sql_id, $ids); - - $sql .= $sql_where; - - $this->db->sql_query($sql); - $num_deleted = $this->db->sql_affectedrows(); + // Delete attachments from database + $this->delete_attachments_from_db(); /** * Perform additional actions after attachment(s) deletion from the database @@ -202,87 +141,31 @@ class delete ); extract($this->dispatcher->trigger_event('core.delete_attachments_from_database_after', compact($vars))); - if (!$num_deleted) + if (!$this->num_deleted) { return 0; } // Delete attachments from filesystem - $space_removed = $files_removed = 0; - foreach ($physical as $file_ary) - { - if (phpbb_unlink($file_ary['filename'], 'file', true) && !$file_ary['is_orphan']) - { - // Only non-orphaned files count to the file size - $space_removed += $file_ary['filesize']; - $files_removed++; - } - - if ($file_ary['thumbnail']) - { - phpbb_unlink($file_ary['filename'], 'thumbnail', true); - } - } - - /** - * Perform additional actions after attachment(s) deletion from the filesystem - * - * @event core.delete_attachments_from_filesystem_after - * @var string mode Variable containing attachments deletion mode, can be: post|message|topic|attach|user - * @var mixed ids Array or comma separated list of ids corresponding to the mode - * @var bool resync Flag indicating if posts/messages/topics should be synchronized - * @var string sql_id The field name to collect/delete data for depending on the mode - * @var array post_ids Array with post ids for deleted attachment(s) - * @var array topic_ids Array with topic ids for deleted attachment(s) - * @var array message_ids Array with private message ids for deleted attachment(s) - * @var array physical Array with deleted attachment(s) physical file(s) data - * @var int num_deleted The number of deleted attachment(s) from the database - * @var int space_removed The size of deleted files(s) from the filesystem - * @var int files_removed The number of deleted file(s) from the filesystem - * @since 3.1.7-RC1 - */ - $vars = array( - 'mode', - 'ids', - 'resync', - 'sql_id', - 'post_ids', - 'topic_ids', - 'message_ids', - 'physical', - 'num_deleted', - 'space_removed', - 'files_removed', - ); - extract($this->dispatcher->trigger_event('core.delete_attachments_from_filesystem_after', compact($vars))); - - if ($space_removed || $files_removed) - { - $this->config->increment('upload_dir_size', $space_removed * (-1), false); - $this->config->increment('num_files', $files_removed * (-1), false); - } + $this->delete_attachments_from_filesystem(); // If we do not resync, we do not need to adjust any message, post, topic or user entries if (!$resync) { - return $num_deleted; + return $this->num_deleted; } // No more use for the original ids unset($ids); // Now, we need to resync posts, messages, topics. We go through every one of them - $post_ids = array_unique($post_ids); - $message_ids = array_unique($message_ids); - $topic_ids = array_unique($topic_ids); - // Update post indicators for posts now no longer having attachments - if (sizeof($post_ids)) + if (sizeof($this->post_ids)) { // Just check which posts are still having an assigned attachment not orphaned by querying the attachments table $sql = 'SELECT post_msg_id FROM ' . ATTACHMENTS_TABLE . ' - WHERE ' . $this->db->sql_in_set('post_msg_id', $post_ids) . ' + WHERE ' . $this->db->sql_in_set('post_msg_id', $this->post_ids) . ' AND in_message = 0 AND is_orphan = 0'; $result = $this->db->sql_query($sql); @@ -295,24 +178,24 @@ class delete $this->db->sql_freeresult($result); // Now only unset those ids remaining - $post_ids = array_diff($post_ids, $remaining_ids); + $this->post_ids = array_diff($this->post_ids, $remaining_ids); - if (sizeof($post_ids)) + if (sizeof($this->post_ids)) { $sql = 'UPDATE ' . POSTS_TABLE . ' SET post_attachment = 0 - WHERE ' . $this->db->sql_in_set('post_id', $post_ids); + WHERE ' . $this->db->sql_in_set('post_id', $this->post_ids); $this->db->sql_query($sql); } } // Update message table if messages are affected - if (sizeof($message_ids)) + if (sizeof($this->message_ids)) { // Just check which messages are still having an assigned attachment not orphaned by querying the attachments table $sql = 'SELECT post_msg_id FROM ' . ATTACHMENTS_TABLE . ' - WHERE ' . $this->db->sql_in_set('post_msg_id', $message_ids) . ' + WHERE ' . $this->db->sql_in_set('post_msg_id', $this->message_ids) . ' AND in_message = 1 AND is_orphan = 0'; $result = $this->db->sql_query($sql); @@ -325,24 +208,24 @@ class delete $this->db->sql_freeresult($result); // Now only unset those ids remaining - $message_ids = array_diff($message_ids, $remaining_ids); + $this->message_ids = array_diff($this->message_ids, $remaining_ids); - if (sizeof($message_ids)) + if (sizeof($this->message_ids)) { $sql = 'UPDATE ' . PRIVMSGS_TABLE . ' SET message_attachment = 0 - WHERE ' . $this->db->sql_in_set('msg_id', $message_ids); + WHERE ' . $this->db->sql_in_set('msg_id', $this->message_ids); $this->db->sql_query($sql); } } // Now update the topics. This is a bit trickier, because there could be posts still having attachments within the topic - if (sizeof($topic_ids)) + if (sizeof($this->topic_ids)) { // Just check which topics are still having an assigned attachment not orphaned by querying the attachments table (much less entries expected) $sql = 'SELECT topic_id FROM ' . ATTACHMENTS_TABLE . ' - WHERE ' . $this->db->sql_in_set('topic_id', $topic_ids) . ' + WHERE ' . $this->db->sql_in_set('topic_id', $this->topic_ids) . ' AND is_orphan = 0'; $result = $this->db->sql_query($sql); @@ -354,18 +237,18 @@ class delete $this->db->sql_freeresult($result); // Now only unset those ids remaining - $topic_ids = array_diff($topic_ids, $remaining_ids); + $this->topic_ids = array_diff($this->topic_ids, $remaining_ids); - if (sizeof($topic_ids)) + if (sizeof($this->topic_ids)) { $sql = 'UPDATE ' . TOPICS_TABLE . ' SET topic_attachment = 0 - WHERE ' . $this->db->sql_in_set('topic_id', $topic_ids); + WHERE ' . $this->db->sql_in_set('topic_id', $this->topic_ids); $this->db->sql_query($sql); } } - return $num_deleted; + return $this->num_deleted; } /** @@ -395,4 +278,178 @@ class delete return true; } + + /** + * Set SQL constraints based on mode + * + * @param string $mode Delete mode; can be: post|message|topic|attach|user + */ + private function set_sql_constraints($mode) + { + switch ($mode) + { + case 'post': + case 'message': + $this->sql_id = 'post_msg_id'; + $this->sql_where = ' AND in_message = ' . ($mode == 'message' ? 1 : 0); + break; + + case 'topic': + $this->sql_id = 'topic_id'; + break; + + case 'user': + $this->sql_id = 'poster_id'; + break; + + case 'attach': + default: + $this->sql_id = 'attach_id'; + break; + } + } + + /** + * Collect info about attachment IDs + * + * @param bool $resync Whether topics/posts should be resynced after delete + */ + protected function collect_attachment_info($resync) + { + // Collect post and topic ids for later use if we need to touch remaining entries (if resync is enabled) + $sql = 'SELECT post_msg_id, topic_id, in_message, physical_filename, thumbnail, filesize, is_orphan + FROM ' . ATTACHMENTS_TABLE . ' + WHERE ' . $this->db->sql_in_set($this->sql_id, $this->ids); + + $sql .= $this->sql_where; + + $result = $this->db->sql_query($sql); + + while ($row = $this->db->sql_fetchrow($result)) + { + // We only need to store post/message/topic ids if resync is enabled and the file is not orphaned + if ($resync && !$row['is_orphan']) + { + if (!$row['in_message']) + { + $this->post_ids[] = $row['post_msg_id']; + $this->topic_ids[] = $row['topic_id']; + } + else + { + $this->message_ids[] = $row['post_msg_id']; + } + } + + $this->physical[] = array('filename' => $row['physical_filename'], 'thumbnail' => $row['thumbnail'], 'filesize' => $row['filesize'], 'is_orphan' => $row['is_orphan']); + } + $this->db->sql_freeresult($result); + + // IDs should be unique + $this->post_ids = array_unique($this->post_ids); + $this->message_ids = array_unique($this->message_ids); + $this->topic_ids = array_unique($this->topic_ids); + } + + /** + * Delete attachments from database table + */ + protected function delete_attachments_from_db() + { + /** + * Perform additional actions before attachment(s) deletion + * + * @event core.delete_attachments_before + * @var string mode Variable containing attachments deletion mode, can be: post|message|topic|attach|user + * @var mixed ids Array or comma separated list of ids corresponding to the mode + * @var bool resync Flag indicating if posts/messages/topics should be synchronized + * @var string sql_id The field name to collect/delete data for depending on the mode + * @var array post_ids Array with post ids for deleted attachment(s) + * @var array topic_ids Array with topic ids for deleted attachment(s) + * @var array message_ids Array with private message ids for deleted attachment(s) + * @var array physical Array with deleted attachment(s) physical file(s) data + * @since 3.1.7-RC1 + */ + $vars = array( + 'mode', + 'ids', + 'resync', + 'sql_id', + 'post_ids', + 'topic_ids', + 'message_ids', + 'physical', + ); + extract($this->dispatcher->trigger_event('core.delete_attachments_before', compact($vars))); + + // Delete attachments + $sql = 'DELETE FROM ' . ATTACHMENTS_TABLE . ' + WHERE ' . $this->db->sql_in_set($this->sql_id, $this->ids); + + $sql .= $this->sql_where; + + $this->db->sql_query($sql); + $this->num_deleted = $this->db->sql_affectedrows(); + } + + /** + * Delete attachments from filesystem + */ + protected function delete_attachments_from_filesystem() + { + $space_removed = $files_removed = 0; + + foreach ($this->physical as $file_ary) + { + if (phpbb_unlink($file_ary['filename'], 'file', true) && !$file_ary['is_orphan']) + { + // Only non-orphaned files count to the file size + $space_removed += $file_ary['filesize']; + $files_removed++; + } + + if ($file_ary['thumbnail']) + { + phpbb_unlink($file_ary['filename'], 'thumbnail', true); + } + } + + /** + * Perform additional actions after attachment(s) deletion from the filesystem + * + * @event core.delete_attachments_from_filesystem_after + * @var string mode Variable containing attachments deletion mode, can be: post|message|topic|attach|user + * @var mixed ids Array or comma separated list of ids corresponding to the mode + * @var bool resync Flag indicating if posts/messages/topics should be synchronized + * @var string sql_id The field name to collect/delete data for depending on the mode + * @var array post_ids Array with post ids for deleted attachment(s) + * @var array topic_ids Array with topic ids for deleted attachment(s) + * @var array message_ids Array with private message ids for deleted attachment(s) + * @var array physical Array with deleted attachment(s) physical file(s) data + * @var int num_deleted The number of deleted attachment(s) from the database + * @var int space_removed The size of deleted files(s) from the filesystem + * @var int files_removed The number of deleted file(s) from the filesystem + * @since 3.1.7-RC1 + */ + $vars = array( + 'mode', + 'ids', + 'resync', + 'sql_id', + 'post_ids', + 'topic_ids', + 'message_ids', + 'physical', + 'num_deleted', + 'space_removed', + 'files_removed', + ); + extract($this->dispatcher->trigger_event('core.delete_attachments_from_filesystem_after', compact($vars))); + + if ($space_removed || $files_removed) + { + $this->config->increment('upload_dir_size', $space_removed * (-1), false); + $this->config->increment('num_files', $files_removed * (-1), false); + } + } } \ No newline at end of file -- cgit v1.2.1 From 40117c77304468120245ae05b6b99f1d2a68c9f6 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Mon, 21 Sep 2015 10:56:32 +0200 Subject: [ticket/14168] Add attachment resync class PHPBB3-14168 --- phpBB/phpbb/attachment/delete.php | 98 +++++---------------------------------- 1 file changed, 11 insertions(+), 87 deletions(-) (limited to 'phpBB/phpbb/attachment/delete.php') diff --git a/phpBB/phpbb/attachment/delete.php b/phpBB/phpbb/attachment/delete.php index 7a168c2243..4ef320eab4 100644 --- a/phpBB/phpbb/attachment/delete.php +++ b/phpBB/phpbb/attachment/delete.php @@ -32,6 +32,9 @@ class delete /** @var \phpbb\event\dispatcher */ protected $dispatcher; + /** @var \phpbb\attachment\resync */ + protected $resync; + /** @var array Attachement IDs */ protected $ids; @@ -61,13 +64,15 @@ class delete * * @param config $config * @param driver_interface $db - * @param dispatcher>>>>>>> 85b6020... [ticket/14168] Move function for attachment deletion into class + * @param dispatcher $dispatcher + * @param resync $resync */ - public function __construct(config $config, driver_interface $db, dispatcher $dispatcher) + public function __construct(config $config, driver_interface $db, dispatcher $dispatcher, resync $resync) { $this->config = $config; $this->db = $db; $this->dispatcher = $dispatcher; + $this->resync = $resync; } /** @@ -158,95 +163,14 @@ class delete // No more use for the original ids unset($ids); - // Now, we need to resync posts, messages, topics. We go through every one of them // Update post indicators for posts now no longer having attachments - if (sizeof($this->post_ids)) - { - // Just check which posts are still having an assigned attachment not orphaned by querying the attachments table - $sql = 'SELECT post_msg_id - FROM ' . ATTACHMENTS_TABLE . ' - WHERE ' . $this->db->sql_in_set('post_msg_id', $this->post_ids) . ' - AND in_message = 0 - AND is_orphan = 0'; - $result = $this->db->sql_query($sql); - - $remaining_ids = array(); - while ($row = $this->db->sql_fetchrow($result)) - { - $remaining_ids[] = $row['post_msg_id']; - } - $this->db->sql_freeresult($result); - - // Now only unset those ids remaining - $this->post_ids = array_diff($this->post_ids, $remaining_ids); - - if (sizeof($this->post_ids)) - { - $sql = 'UPDATE ' . POSTS_TABLE . ' - SET post_attachment = 0 - WHERE ' . $this->db->sql_in_set('post_id', $this->post_ids); - $this->db->sql_query($sql); - } - } + $this->resync->resync('post', $this->post_ids); // Update message table if messages are affected - if (sizeof($this->message_ids)) - { - // Just check which messages are still having an assigned attachment not orphaned by querying the attachments table - $sql = 'SELECT post_msg_id - FROM ' . ATTACHMENTS_TABLE . ' - WHERE ' . $this->db->sql_in_set('post_msg_id', $this->message_ids) . ' - AND in_message = 1 - AND is_orphan = 0'; - $result = $this->db->sql_query($sql); - - $remaining_ids = array(); - while ($row = $this->db->sql_fetchrow($result)) - { - $remaining_ids[] = $row['post_msg_id']; - } - $this->db->sql_freeresult($result); - - // Now only unset those ids remaining - $this->message_ids = array_diff($this->message_ids, $remaining_ids); - - if (sizeof($this->message_ids)) - { - $sql = 'UPDATE ' . PRIVMSGS_TABLE . ' - SET message_attachment = 0 - WHERE ' . $this->db->sql_in_set('msg_id', $this->message_ids); - $this->db->sql_query($sql); - } - } + $this->resync->resync('message', $this->message_ids); // Now update the topics. This is a bit trickier, because there could be posts still having attachments within the topic - if (sizeof($this->topic_ids)) - { - // Just check which topics are still having an assigned attachment not orphaned by querying the attachments table (much less entries expected) - $sql = 'SELECT topic_id - FROM ' . ATTACHMENTS_TABLE . ' - WHERE ' . $this->db->sql_in_set('topic_id', $this->topic_ids) . ' - AND is_orphan = 0'; - $result = $this->db->sql_query($sql); - - $remaining_ids = array(); - while ($row = $this->db->sql_fetchrow($result)) - { - $remaining_ids[] = $row['topic_id']; - } - $this->db->sql_freeresult($result); - - // Now only unset those ids remaining - $this->topic_ids = array_diff($this->topic_ids, $remaining_ids); - - if (sizeof($this->topic_ids)) - { - $sql = 'UPDATE ' . TOPICS_TABLE . ' - SET topic_attachment = 0 - WHERE ' . $this->db->sql_in_set('topic_id', $this->topic_ids); - $this->db->sql_query($sql); - } - } + $this->resync->resync('topic', $this->topic_ids); return $this->num_deleted; } @@ -384,7 +308,7 @@ class delete // Delete attachments $sql = 'DELETE FROM ' . ATTACHMENTS_TABLE . ' - WHERE ' . $this->db->sql_in_set($this->sql_id, $this->ids); + WHERE ' . $this->db->sql_in_set($this->sql_id, $this->ids); $sql .= $this->sql_where; -- cgit v1.2.1 From e158db5daa8b9eedfea9d17b5d678320ac7f8f61 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Mon, 21 Sep 2015 11:01:01 +0200 Subject: [ticket/14168] Minor coding style fixes PHPBB3-14168 --- phpBB/phpbb/attachment/delete.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'phpBB/phpbb/attachment/delete.php') diff --git a/phpBB/phpbb/attachment/delete.php b/phpBB/phpbb/attachment/delete.php index 4ef320eab4..32469c124c 100644 --- a/phpBB/phpbb/attachment/delete.php +++ b/phpBB/phpbb/attachment/delete.php @@ -376,4 +376,4 @@ class delete $this->config->increment('num_files', $files_removed * (-1), false); } } -} \ No newline at end of file +} -- cgit v1.2.1 From cebc064f4c15b9c53210cc3729756b4d7888041b Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Mon, 21 Sep 2015 14:51:39 +0200 Subject: [ticket/14168] Coding guidelines fixes PHPBB3-14168 --- phpBB/phpbb/attachment/delete.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'phpBB/phpbb/attachment/delete.php') diff --git a/phpBB/phpbb/attachment/delete.php b/phpBB/phpbb/attachment/delete.php index 32469c124c..4c863a2205 100644 --- a/phpBB/phpbb/attachment/delete.php +++ b/phpBB/phpbb/attachment/delete.php @@ -216,20 +216,20 @@ class delete case 'message': $this->sql_id = 'post_msg_id'; $this->sql_where = ' AND in_message = ' . ($mode == 'message' ? 1 : 0); - break; + break; case 'topic': $this->sql_id = 'topic_id'; - break; + break; case 'user': $this->sql_id = 'poster_id'; - break; + break; case 'attach': default: $this->sql_id = 'attach_id'; - break; + break; } } -- cgit v1.2.1 From b00c7511f022dfafb32f570db1f5fe762a62ef21 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Mon, 21 Sep 2015 22:16:23 +0200 Subject: [ticket/14168] Add tests for attachment delete class PHPBB3-14168 --- phpBB/phpbb/attachment/delete.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'phpBB/phpbb/attachment/delete.php') diff --git a/phpBB/phpbb/attachment/delete.php b/phpBB/phpbb/attachment/delete.php index 4c863a2205..372495aef0 100644 --- a/phpBB/phpbb/attachment/delete.php +++ b/phpBB/phpbb/attachment/delete.php @@ -85,7 +85,7 @@ class delete * @return int|bool Number of deleted attachments or false if something * went wrong during attachment deletion */ - function delete($mode, $ids, $resync = true) + public function delete($mode, $ids, $resync = true) { if (!$this->set_attachment_ids($ids)) { -- cgit v1.2.1 From 04786313892df25f5adce555ebae6b2e5ad4d222 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Wed, 23 Sep 2015 10:17:38 +0200 Subject: [ticket/14168] Move phpbb_unlink() into attachment delete class PHPBB3-14168 --- phpBB/phpbb/attachment/delete.php | 70 ++++++++++++++++++++++++++++++++++----- 1 file changed, 62 insertions(+), 8 deletions(-) (limited to 'phpBB/phpbb/attachment/delete.php') diff --git a/phpBB/phpbb/attachment/delete.php b/phpBB/phpbb/attachment/delete.php index 372495aef0..ad385861a1 100644 --- a/phpBB/phpbb/attachment/delete.php +++ b/phpBB/phpbb/attachment/delete.php @@ -16,6 +16,7 @@ namespace phpbb\attachment; use \phpbb\config\config; use \phpbb\db\driver\driver_interface; use \phpbb\event\dispatcher; +use \phpbb\filesystem\filesystem; /** * Attachment delete class @@ -23,18 +24,24 @@ use \phpbb\event\dispatcher; class delete { - /** @var \phpbb\config\config */ + /** @var config */ protected $config; - /** @var \phpbb\db\driver\driver_interface */ + /** @var driver_interface */ protected $db; /** @var \phpbb\event\dispatcher */ protected $dispatcher; - /** @var \phpbb\attachment\resync */ + /** @var filesystem */ + protected $filesystem; + + /** @var resync */ protected $resync; + /** @var string phpBB root path */ + protected $phpbb_root_path; + /** @var array Attachement IDs */ protected $ids; @@ -65,14 +72,18 @@ class delete * @param config $config * @param driver_interface $db * @param dispatcher $dispatcher + * @param filesystem $filesystem * @param resync $resync + * @param string $phpbb_root_path */ - public function __construct(config $config, driver_interface $db, dispatcher $dispatcher, resync $resync) + public function __construct(config $config, driver_interface $db, dispatcher $dispatcher, filesystem $filesystem, resync $resync, $phpbb_root_path) { $this->config = $config; $this->db = $db; $this->dispatcher = $dispatcher; + $this->filesystem = $filesystem; $this->resync = $resync; + $this->phpbb_root_path = $phpbb_root_path; } /** @@ -152,7 +163,7 @@ class delete } // Delete attachments from filesystem - $this->delete_attachments_from_filesystem(); + $this->remove_from_filesystem(); // If we do not resync, we do not need to adjust any message, post, topic or user entries if (!$resync) @@ -319,13 +330,13 @@ class delete /** * Delete attachments from filesystem */ - protected function delete_attachments_from_filesystem() + protected function remove_from_filesystem() { $space_removed = $files_removed = 0; foreach ($this->physical as $file_ary) { - if (phpbb_unlink($file_ary['filename'], 'file', true) && !$file_ary['is_orphan']) + if ($this->unlink_attachment($file_ary['filename'], 'file', true) && !$file_ary['is_orphan']) { // Only non-orphaned files count to the file size $space_removed += $file_ary['filesize']; @@ -334,7 +345,7 @@ class delete if ($file_ary['thumbnail']) { - phpbb_unlink($file_ary['filename'], 'thumbnail', true); + $this->unlink_attachment($file_ary['filename'], 'thumbnail', true); } } @@ -376,4 +387,47 @@ class delete $this->config->increment('num_files', $files_removed * (-1), false); } } + + /** + * Delete attachment from filesystem + * + * @param string $filename Filename of attachment + * @param string $mode Delete mode + * @param bool $entry_removed Whether entry was removed. Defaults to false + * @return bool True if file was removed, false if not + */ + public function unlink_attachment($filename, $mode = 'file', $entry_removed = false) + { + // Because of copying topics or modifications a physical filename could be assigned more than once. If so, do not remove the file itself. + $sql = 'SELECT COUNT(attach_id) AS num_entries + FROM ' . ATTACHMENTS_TABLE . " + WHERE physical_filename = '" . $this->db->sql_escape(utf8_basename($filename)) . "'"; + $result = $this->db->sql_query($sql); + $num_entries = (int) $this->db->sql_fetchfield('num_entries'); + $this->db->sql_freeresult($result); + + // Do not remove file if at least one additional entry with the same name exist. + if (($entry_removed && $num_entries > 0) || (!$entry_removed && $num_entries > 1)) + { + return false; + } + + $filename = ($mode == 'thumbnail') ? 'thumb_' . utf8_basename($filename) : utf8_basename($filename); + $filepath = $this->phpbb_root_path . $this->config['upload_path'] . '/' . $filename; + + try + { + if ($this->filesystem->exists($filepath)) + { + $this->filesystem->remove($this->phpbb_root_path . $this->config['upload_path'] . '/' . $filename); + return true; + } + } + catch (\phpbb\filesystem\exception\filesystem_exception $exception) + { + // Fail is covered by return statement below + } + + return false; + } } -- cgit v1.2.1 From 80e3c79f383366915b799675c6bcd3e15715780e Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Wed, 7 Oct 2015 11:04:15 +0200 Subject: [ticket/14168] Minor coding style fixes PHPBB3-14168 --- phpBB/phpbb/attachment/delete.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'phpBB/phpbb/attachment/delete.php') diff --git a/phpBB/phpbb/attachment/delete.php b/phpBB/phpbb/attachment/delete.php index ad385861a1..e093da8865 100644 --- a/phpBB/phpbb/attachment/delete.php +++ b/phpBB/phpbb/attachment/delete.php @@ -21,7 +21,6 @@ use \phpbb\filesystem\filesystem; /** * Attachment delete class */ - class delete { /** @var config */ @@ -189,7 +188,7 @@ class delete /** * Set attachment IDs * - * @param array $ids + * @param mixed $ids ID or array of IDs * * @return bool True if attachment IDs were set, false if not */ -- cgit v1.2.1 From 4614cc972f9a00b67bfc982c2da7b81dfed8c6a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1t=C3=A9=20Bartus?= Date: Wed, 13 Apr 2016 19:02:43 +0200 Subject: [ticket/14589] Fix @var usage PHPBB3-14589 --- phpBB/phpbb/attachment/delete.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'phpBB/phpbb/attachment/delete.php') diff --git a/phpBB/phpbb/attachment/delete.php b/phpBB/phpbb/attachment/delete.php index e093da8865..922f24b5dc 100644 --- a/phpBB/phpbb/attachment/delete.php +++ b/phpBB/phpbb/attachment/delete.php @@ -29,7 +29,7 @@ class delete /** @var driver_interface */ protected $db; - /** @var \phpbb\event\dispatcher */ + /** @var dispatcher */ protected $dispatcher; /** @var filesystem */ -- cgit v1.2.1 From fc674873c7ad0acaf7773003534fd2279a4dd1bf Mon Sep 17 00:00:00 2001 From: rxu Date: Sun, 12 Aug 2018 21:54:13 +0700 Subject: [ticket/15755] Fix event vars in /phpbb/attachment/delete.php PHPBB3-15755 --- phpBB/phpbb/attachment/delete.php | 56 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 52 insertions(+), 4 deletions(-) (limited to 'phpBB/phpbb/attachment/delete.php') diff --git a/phpBB/phpbb/attachment/delete.php b/phpBB/phpbb/attachment/delete.php index 922f24b5dc..3c98e21587 100644 --- a/phpBB/phpbb/attachment/delete.php +++ b/phpBB/phpbb/attachment/delete.php @@ -104,6 +104,8 @@ class delete $this->set_sql_constraints($mode); + $sql_id = $this->sql_id; + /** * Perform additional actions before collecting data for attachment(s) deletion * @@ -122,11 +124,21 @@ class delete ); extract($this->dispatcher->trigger_event('core.delete_attachments_collect_data_before', compact($vars))); + $this->sql_id = $sql_id; + unset($sql_id); + // Collect post and topic ids for later use if we need to touch remaining entries (if resync is enabled) $this->collect_attachment_info($resync); // Delete attachments from database - $this->delete_attachments_from_db(); + $this->delete_attachments_from_db($mode, $ids, $resync); + + $sql_id = $this->sql_id; + $post_ids = $this->post_ids; + $topic_ids = $this->topic_ids; + $message_ids = $this->message_ids; + $physical = $this->physical; + $num_deleted = $this->num_deleted; /** * Perform additional actions after attachment(s) deletion from the database @@ -156,13 +168,21 @@ class delete ); extract($this->dispatcher->trigger_event('core.delete_attachments_from_database_after', compact($vars))); + $this->sql_id = $sql_id; + $this->post_ids = $post_ids; + $this->topic_ids = $topic_ids; + $this->message_ids = $message_ids; + $this->physical = $physical; + $this->num_deleted = $num_deleted; + unset($sql_id, $post_ids, $topic_ids, $message_ids, $physical, $num_deleted); + if (!$this->num_deleted) { return 0; } // Delete attachments from filesystem - $this->remove_from_filesystem(); + $this->remove_from_filesystem($mode, $ids, $resync); // If we do not resync, we do not need to adjust any message, post, topic or user entries if (!$resync) @@ -288,8 +308,14 @@ class delete /** * Delete attachments from database table */ - protected function delete_attachments_from_db() + protected function delete_attachments_from_db($mode, $ids, $resync) { + $sql_id = $this->sql_id; + $post_ids = $this->post_ids; + $topic_ids = $this->topic_ids; + $message_ids = $this->message_ids; + $physical = $this->physical; + /** * Perform additional actions before attachment(s) deletion * @@ -316,6 +342,13 @@ class delete ); extract($this->dispatcher->trigger_event('core.delete_attachments_before', compact($vars))); + $this->sql_id = $sql_id; + $this->post_ids = $post_ids; + $this->topic_ids = $topic_ids; + $this->message_ids = $message_ids; + $this->physical = $physical; + unset($sql_id, $post_ids, $topic_ids, $message_ids, $physical); + // Delete attachments $sql = 'DELETE FROM ' . ATTACHMENTS_TABLE . ' WHERE ' . $this->db->sql_in_set($this->sql_id, $this->ids); @@ -329,7 +362,7 @@ class delete /** * Delete attachments from filesystem */ - protected function remove_from_filesystem() + protected function remove_from_filesystem($mode, $ids, $resync) { $space_removed = $files_removed = 0; @@ -348,6 +381,13 @@ class delete } } + $sql_id = $this->sql_id; + $post_ids = $this->post_ids; + $topic_ids = $this->topic_ids; + $message_ids = $this->message_ids; + $physical = $this->physical; + $num_deleted = $this->num_deleted; + /** * Perform additional actions after attachment(s) deletion from the filesystem * @@ -380,6 +420,14 @@ class delete ); extract($this->dispatcher->trigger_event('core.delete_attachments_from_filesystem_after', compact($vars))); + $this->sql_id = $sql_id; + $this->post_ids = $post_ids; + $this->topic_ids = $topic_ids; + $this->message_ids = $message_ids; + $this->physical = $physical; + $this->num_deleted = $num_deleted; + unset($sql_id, $post_ids, $topic_ids, $message_ids, $physical, $num_deleted); + if ($space_removed || $files_removed) { $this->config->increment('upload_dir_size', $space_removed * (-1), false); -- cgit v1.2.1