From e62e429302c6980c43a2558cbbce964f3db486fe Mon Sep 17 00:00:00 2001 From: Alexei Sorokin Date: Thu, 13 Aug 2020 23:56:31 +0300 Subject: [PATCH] [File] Fix file deletion violating foreign keys --- classes/File.php | 25 ++++++++++++------------- plugins/Embed/EmbedPlugin.php | 6 ++++++ 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/classes/File.php b/classes/File.php index 63fe6e8f4f..4e37f5f187 100644 --- a/classes/File.php +++ b/classes/File.php @@ -850,20 +850,19 @@ class File extends Managed_DataObject } // Clear out related things in the database and filesystem, such as thumbnails - if (Event::handle('FileDeleteRelated', array($this))) { - $thumbs = new File_thumbnail(); - $thumbs->file_id = $this->id; - if ($thumbs->find()) { - while ($thumbs->fetch()) { - $thumbs->delete(); - } - } + $related = [ + 'File_redirection', + 'File_thumbnail', + 'File_to_post', + ]; + Event::handle('FileDeleteRelated', [$this, &$related]); - $f2p = new File_to_post(); - $f2p->file_id = $this->id; - if ($f2p->find()) { - while ($f2p->fetch()) { - $f2p->delete(); + foreach ($related as $cls) { + $inst = new $cls(); + $inst->file_id = $this->id; + if ($inst->find()) { + while ($inst->fetch()) { + $inst->delete(); } } } diff --git a/plugins/Embed/EmbedPlugin.php b/plugins/Embed/EmbedPlugin.php index 232d7af55b..5475b8ef25 100644 --- a/plugins/Embed/EmbedPlugin.php +++ b/plugins/Embed/EmbedPlugin.php @@ -440,6 +440,12 @@ class EmbedPlugin extends Plugin return !file_exists($imgPath); } + public function onFileDeleteRelated(File $file, array &$related): bool + { + $related[] = 'File_embed'; + return true; + } + /** * @return bool false on no check made, provider name on success * @throws ServerException if check is made but fails