From 2201ea79cff0ad1828fee3050b5536e8a0559461 Mon Sep 17 00:00:00 2001 From: Matthias Schmidt Date: Tue, 31 May 2016 20:56:48 +0200 Subject: [PATCH] Catch exceptions thrown when processing attachment files --- .../attachment/AttachmentAction.class.php | 119 ++++++++++-------- 1 file changed, 65 insertions(+), 54 deletions(-) diff --git a/wcfsetup/install/files/lib/data/attachment/AttachmentAction.class.php b/wcfsetup/install/files/lib/data/attachment/AttachmentAction.class.php index 7e0c53160e..82cbf571a2 100644 --- a/wcfsetup/install/files/lib/data/attachment/AttachmentAction.class.php +++ b/wcfsetup/install/files/lib/data/attachment/AttachmentAction.class.php @@ -6,6 +6,7 @@ use wcf\system\attachment\AttachmentHandler; use wcf\system\database\util\PreparedStatementConditionBuilder; use wcf\system\event\EventHandler; use wcf\system\exception\PermissionDeniedException; +use wcf\system\exception\SystemException; use wcf\system\exception\UserInputException; use wcf\system\image\ImageHandler; use wcf\system\request\LinkHandler; @@ -169,70 +170,80 @@ class AttachmentAction extends AbstractDatabaseObjectAction { // move uploaded file if (@move_uploaded_file($file->getLocation(), $attachment->getLocation())) { - if ($attachment->isImage) { - $thumbnails[] = $attachment; - - // rotate image based on the exif data - $neededMemory = $attachment->width * $attachment->height * ($attachment->fileType == 'image/png' ? 4 : 3) * 2.1; - if (FileUtil::getMemoryLimit() == -1 || FileUtil::getMemoryLimit() > (memory_get_usage() + $neededMemory)) { - $exifData = ExifUtil::getExifData($attachment->getLocation()); - if (!empty($exifData)) { - $orientation = ExifUtil::getOrientation($exifData); - if ($orientation != ExifUtil::ORIENTATION_ORIGINAL) { - $adapter = ImageHandler::getInstance()->getAdapter(); - $adapter->loadFile($attachment->getLocation()); - - $newImage = null; - switch ($orientation) { - case ExifUtil::ORIENTATION_180_ROTATE: - $newImage = $adapter->rotate(180); - break; + try { + if ($attachment->isImage) { + // rotate image based on the exif data + $neededMemory = $attachment->width * $attachment->height * ($attachment->fileType == 'image/png' ? 4 : 3) * 2.1; + if (FileUtil::getMemoryLimit() == -1 || FileUtil::getMemoryLimit() > (memory_get_usage() + $neededMemory)) { + $exifData = ExifUtil::getExifData($attachment->getLocation()); + if (!empty($exifData)) { + $orientation = ExifUtil::getOrientation($exifData); + if ($orientation != ExifUtil::ORIENTATION_ORIGINAL) { + $adapter = ImageHandler::getInstance()->getAdapter(); + $adapter->loadFile($attachment->getLocation()); - case ExifUtil::ORIENTATION_90_ROTATE: - $newImage = $adapter->rotate(90); - break; + $newImage = null; + switch ($orientation) { + case ExifUtil::ORIENTATION_180_ROTATE: + $newImage = $adapter->rotate(180); + break; + + case ExifUtil::ORIENTATION_90_ROTATE: + $newImage = $adapter->rotate(90); + break; + + case ExifUtil::ORIENTATION_270_ROTATE: + $newImage = $adapter->rotate(270); + break; + + case ExifUtil::ORIENTATION_HORIZONTAL_FLIP: + case ExifUtil::ORIENTATION_VERTICAL_FLIP: + case ExifUtil::ORIENTATION_VERTICAL_FLIP_270_ROTATE: + case ExifUtil::ORIENTATION_HORIZONTAL_FLIP_270_ROTATE: + // unsupported + break; + } - case ExifUtil::ORIENTATION_270_ROTATE: - $newImage = $adapter->rotate(270); - break; + if ($newImage !== null) { + $adapter->load($newImage, $adapter->getType()); + } - case ExifUtil::ORIENTATION_HORIZONTAL_FLIP: - case ExifUtil::ORIENTATION_VERTICAL_FLIP: - case ExifUtil::ORIENTATION_VERTICAL_FLIP_270_ROTATE: - case ExifUtil::ORIENTATION_HORIZONTAL_FLIP_270_ROTATE: - // unsupported - break; - } - - if ($newImage !== null) { - $adapter->load($newImage, $adapter->getType()); - } - - $adapter->writeImage($attachment->getLocation()); - - if ($newImage !== null) { - // update width, height and filesize of the attachment - if ($orientation == ExifUtil::ORIENTATION_90_ROTATE || $orientation == ExifUtil::ORIENTATION_270_ROTATE) { - $attachmentEditor = new AttachmentEditor($attachment); - $attachmentEditor->update(array( - 'height' => $attachment->width, - 'width' => $attachment->height, - 'filesize' => filesize($attachment->getLocation()) - )); + $adapter->writeImage($attachment->getLocation()); + + if ($newImage !== null) { + // update width, height and filesize of the attachment + if ($orientation == ExifUtil::ORIENTATION_90_ROTATE || $orientation == ExifUtil::ORIENTATION_270_ROTATE) { + $attachmentEditor = new AttachmentEditor($attachment); + $attachmentEditor->update(array( + 'height' => $attachment->width, + 'width' => $attachment->height, + 'filesize' => filesize($attachment->getLocation()) + )); + } } } } } + + $thumbnails[] = $attachment; } + else { + // check whether we can create thumbnails for this file + $this->eventAttachment = $attachment; + $this->eventData = array('hasThumbnail' => false); + EventHandler::getInstance()->fireAction($this, 'checkThumbnail'); + if ($this->eventData['hasThumbnail']) $thumbnails[] = $attachment; + } + $attachments[$file->getInternalFileID()] = $attachment; } - else { - // check whether we can create thumbnails for this file - $this->eventAttachment = $attachment; - $this->eventData = array('hasThumbnail' => false); - EventHandler::getInstance()->fireAction($this, 'checkThumbnail'); - if ($this->eventData['hasThumbnail']) $thumbnails[] = $attachment; + catch (SystemException $e) { + // processing failed; delete attachment + $editor = new AttachmentEditor($attachment); + $editor->delete(); + + // log error + $e->getExceptionID(); } - $attachments[$file->getInternalFileID()] = $attachment; } else { // moving failed; delete attachment -- 2.20.1