From 3c90597ca8635eec714680c5b4442f332d5bb633 Mon Sep 17 00:00:00 2001 From: Shish Date: Tue, 9 Jan 2024 04:22:59 +0000 Subject: [PATCH] [core] inline some single-use functions --- core/extension.php | 24 +++++++++--------------- core/imageboard/image.php | 5 ++--- ext/handle_svg/main.php | 32 ++++++++++++++++++++------------ 3 files changed, 31 insertions(+), 30 deletions(-) diff --git a/core/extension.php b/core/extension.php index 0db1f8af..20906825 100644 --- a/core/extension.php +++ b/core/extension.php @@ -287,19 +287,6 @@ abstract class DataHandlerExtension extends Extension { protected array $SUPPORTED_MIME = []; - protected function move_upload_to_archive(DataUploadEvent $event): string - { - $target = warehouse_path(Image::IMAGE_DIR, $event->hash); - if (!@copy($event->tmpname, $target)) { - $errors = error_get_last(); - throw new UploadException( - "Failed to copy file from uploads ({$event->tmpname}) to archive ($target): ". - "{$errors['type']} / {$errors['message']}" - ); - } - return $target; - } - public function onDataUpload(DataUploadEvent $event) { global $config; @@ -329,8 +316,15 @@ abstract class DataHandlerExtension extends Extension $filename = $event->tmpname; // FIXME: this should happen after ImageAdditionEvent, but the thumbnail // code assumes the file is in the archive already instead of using - // the image->tmp_file field - $filename = $this->move_upload_to_archive($event); + // the image->get_image_filename() function + $filename = warehouse_path(Image::IMAGE_DIR, $event->hash); + if (!@copy($event->tmpname, $filename)) { + $errors = error_get_last(); + throw new UploadException( + "Failed to copy file from uploads ({$event->tmpname}) to archive ($filename): ". + "{$errors['type']} / {$errors['message']}" + ); + } assert(is_readable($filename)); $image = new Image(); diff --git a/core/imageboard/image.php b/core/imageboard/image.php index a1091edc..66b4a95b 100644 --- a/core/imageboard/image.php +++ b/core/imageboard/image.php @@ -583,7 +583,7 @@ class Image * This function removes an image (and thumbnail) from the DISK ONLY. * It DOES NOT remove anything from the database. */ - public function remove_image_only(bool $quiet=false): void + public function remove_image_only(bool $quiet = false): void { $img_del = @unlink($this->get_image_filename()); $thumb_del = @unlink($this->get_thumb_filename()); @@ -591,8 +591,7 @@ class Image if(!$quiet) { log_info("core_image", "Deleted files for Post #{$this->id} ({$this->hash})"); } - } - else { + } else { $img = $img_del ? '' : ' image'; $thumb = $thumb_del ? '' : ' thumbnail'; log_error('core_image', "Failed to delete files for Post #{$this->id}{$img}{$thumb}"); diff --git a/ext/handle_svg/main.php b/ext/handle_svg/main.php index 448fb73e..26c08f20 100644 --- a/ext/handle_svg/main.php +++ b/ext/handle_svg/main.php @@ -32,6 +32,26 @@ class SVGFileHandler extends DataHandlerExtension } } + public function onDataUpload(DataUploadEvent $event) + { + global $config; + + if ($this->supported_mime($event->mime)) { + // If the SVG handler intends to handle this file, + // then sanitise it before touching it + $sanitizer = new Sanitizer(); + $sanitizer->removeRemoteReferences(true); + $dirtySVG = file_get_contents($event->tmpname); + $cleanSVG = $sanitizer->sanitize($dirtySVG); + $event->hash = md5($cleanSVG); + $new_tmpname = tempnam(sys_get_temp_dir(), "shimmie_svg"); + file_put_contents($new_tmpname, $cleanSVG); + $event->set_tmpname($new_tmpname); + + parent::onDataUpload($event); + } + } + protected function media_check_properties(MediaCheckPropertiesEvent $event): void { $event->image->lossless = true; @@ -44,18 +64,6 @@ class SVGFileHandler extends DataHandlerExtension $event->image->height = $msp->height; } - protected function move_upload_to_archive(DataUploadEvent $event): string - { - $sanitizer = new Sanitizer(); - $sanitizer->removeRemoteReferences(true); - $dirtySVG = file_get_contents($event->tmpname); - $cleanSVG = $sanitizer->sanitize($dirtySVG); - $event->hash = md5($cleanSVG); - $filename = warehouse_path(Image::IMAGE_DIR, $event->hash); - file_put_contents($filename, $cleanSVG); - return $filename; - } - protected function create_thumb(string $hash, string $mime): bool { try {