[SensitiveContent] Fix bug where having this plugin activated would not size thumbnails correctly

This commit is contained in:
Hugo Sales 2020-04-06 17:53:15 +00:00 committed by Diogo Peralta Cordeiro
parent b7ed15c865
commit 5b3105402e

View File

@ -1,33 +1,34 @@
<?php <?php
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
/** /**
* GNU social - a federating social network
*
* Abstraction for an image file * Abstraction for an image file
* *
* LICENCE: This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @category Image * @category Image
* @package GNUsocial * @package GNUsocial
*
* @author Evan Prodromou <evan@status.net> * @author Evan Prodromou <evan@status.net>
* @author Zach Copley <zach@status.net> * @author Zach Copley <zach@status.net>
* @author Mikael Nordfeldth <mmn@hethane.se> * @author Mikael Nordfeldth <mmn@hethane.se>
* @author Miguel Dantas <biodantasgs@gmail.com> * @author Miguel Dantas <biodantasgs@gmail.com>
* @copyright 2008, 2019 Free Software Foundation http://fsf.org * @copyright 2008, 2019 Free Software Foundation http://fsf.org
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link https://www.gnu.org/software/social/ *
* @see https://www.gnu.org/software/social/
*/ */
defined('GNUSOCIAL') || die(); defined('GNUSOCIAL') || die();
use Intervention\Image\ImageManagerStatic as Image; use Intervention\Image\ImageManagerStatic as Image;
@ -42,7 +43,8 @@ use Intervention\Image\ImageManagerStatic as Image;
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @author Evan Prodromou <evan@status.net> * @author Evan Prodromou <evan@status.net>
* @author Zach Copley <zach@status.net> * @author Zach Copley <zach@status.net>
* @link https://www.gnu.org/software/social/ *
* @see https://www.gnu.org/software/social/
*/ */
class ImageFile extends MediaFile class ImageFile extends MediaFile
{ {
@ -69,9 +71,8 @@ class ImageFile extends MediaFile
if ($obj->mimetype == image_type_to_mime_type($type)) { if ($obj->mimetype == image_type_to_mime_type($type)) {
$obj->type = $type; $obj->type = $type;
return true; return true;
} else {
return false;
} }
return false;
}; };
if (!(($cmp($this, IMAGETYPE_GIF) && function_exists('imagecreatefromgif')) || if (!(($cmp($this, IMAGETYPE_GIF) && function_exists('imagecreatefromgif')) ||
($cmp($this, IMAGETYPE_JPEG) && function_exists('imagecreatefromjpeg')) || ($cmp($this, IMAGETYPE_JPEG) && function_exists('imagecreatefromjpeg')) ||
@ -98,7 +99,7 @@ class ImageFile extends MediaFile
// Orientation value to rotate thumbnails properly // Orientation value to rotate thumbnails properly
$exif = @$img->exif(); $exif = @$img->exif();
if (is_array($exif) && isset($exif['Orientation'])) { if (is_array($exif) && isset($exif['Orientation'])) {
switch (intval($exif['Orientation'])) { switch ((int) ($exif['Orientation'])) {
case 1: // top is top case 1: // top is top
$this->rotate = 0; $this->rotate = 0;
break; break;
@ -118,7 +119,7 @@ class ImageFile extends MediaFile
$this->animated = $this->isAnimatedGif(); $this->animated = $this->isAnimatedGif();
} }
Event::handle('FillImageFileMetadata', array($this)); Event::handle('FillImageFileMetadata', [$this]);
$img->destroy(); $img->destroy();
ini_set('memory_limit', $old_limit); // Restore the old memory limit ini_set('memory_limit', $old_limit); // Restore the old memory limit
@ -133,7 +134,7 @@ class ImageFile extends MediaFile
{ {
$imgPath = null; $imgPath = null;
$media = common_get_mime_media($file->mimetype); $media = common_get_mime_media($file->mimetype);
if (Event::handle('CreateFileImageThumbnailSource', array($file, &$imgPath, $media))) { if (Event::handle('CreateFileImageThumbnailSource', [$file, &$imgPath, $media])) {
if (empty($file->filename) && !file_exists($imgPath)) { if (empty($file->filename) && !file_exists($imgPath)) {
throw new FileNotFoundException($imgPath); throw new FileNotFoundException($imgPath);
} }
@ -147,7 +148,8 @@ class ImageFile extends MediaFile
// And we'll only consider it an image if it has such a media type // And we'll only consider it an image if it has such a media type
if ($media !== 'image') { if ($media !== 'image') {
throw new UnsupportedMediaException(_m('Unsupported media format.'), $file->getPath()); throw new UnsupportedMediaException(_m('Unsupported media format.'), $file->getPath());
} else if (!empty($file->filename)) { }
if (!empty($file->filename)) {
$imgPath = $file->getPath(); $imgPath = $file->getPath();
} }
} }
@ -157,7 +159,7 @@ class ImageFile extends MediaFile
} }
try { try {
$image = new ImageFile($file->getID(), $imgPath); $image = new self($file->getID(), $imgPath);
} catch (Exception $e) { } catch (Exception $e) {
// Avoid deleting the original // Avoid deleting the original
try { try {
@ -198,15 +200,18 @@ class ImageFile extends MediaFile
* *
* Uses MediaFile's `fromUpload` to do the majority of the work and reencodes the image, * Uses MediaFile's `fromUpload` to do the majority of the work and reencodes the image,
* to mitigate injection attacks. * to mitigate injection attacks.
*
* @param string $param * @param string $param
* @param Profile|null $scoped * @param null|Profile $scoped
* @return ImageFile|MediaFile *
* @throws ClientException * @throws ClientException
* @throws NoResultException * @throws NoResultException
* @throws NoUploadedMediaException * @throws NoUploadedMediaException
* @throws ServerException * @throws ServerException
* @throws UnsupportedMediaException * @throws UnsupportedMediaException
* @throws UseFileAsThumbnailException * @throws UseFileAsThumbnailException
*
* @return ImageFile|MediaFile
*/ */
public static function fromUpload(string $param = 'upload', Profile $scoped = null) public static function fromUpload(string $param = 'upload', Profile $scoped = null)
{ {
@ -239,16 +244,18 @@ class ImageFile extends MediaFile
* returned ImageFile object to read metadata (width, height etc.) * returned ImageFile object to read metadata (width, height etc.)
* *
* @param string $outpath * @param string $outpath
* @return ImageFile the image stored at target path *
* @throws ClientException * @throws ClientException
* @throws NoResultException * @throws NoResultException
* @throws ServerException * @throws ServerException
* @throws UnsupportedMediaException * @throws UnsupportedMediaException
* @throws UseFileAsThumbnailException * @throws UseFileAsThumbnailException
*
* @return ImageFile the image stored at target path
*/ */
public function copyTo($outpath) public function copyTo($outpath)
{ {
return new ImageFile(null, $this->resizeTo($outpath)); return new self(null, $this->resizeTo($outpath));
} }
/** /**
@ -259,8 +266,10 @@ class ImageFile extends MediaFile
* @return string full local filesystem filename * @return string full local filesystem filename
* @throws UnsupportedMediaException * @throws UnsupportedMediaException
* @throws UseFileAsThumbnailException * @throws UseFileAsThumbnailException
*
* @return string full local filesystem filename
*/ */
public function resizeTo($outpath, array $box=array()) public function resizeTo($outpath, array $box = [])
{ {
$box['width'] = isset($box['width']) ? intval($box['width']) : $this->width; $box['width'] = isset($box['width']) ? intval($box['width']) : $this->width;
$box['height'] = isset($box['height']) ? intval($box['height']) : $this->height; $box['height'] = isset($box['height']) ? intval($box['height']) : $this->height;
@ -298,7 +307,10 @@ class ImageFile extends MediaFile
} }
} }
if (Event::handle('StartResizeImageFile', array($this, $outpath, $box))) { $this->height = $box['h'];
$this->width = $box['w'];
if (Event::handle('StartResizeImageFile', [$this, $outpath, $box])) {
$outpath = $this->resizeToFile($outpath, $box); $outpath = $this->resizeToFile($outpath, $box);
} }
@ -321,8 +333,10 @@ class ImageFile extends MediaFile
* Increases the 'memory_limit' to the one in the 'attachments' section in the config, to * Increases the 'memory_limit' to the one in the 'attachments' section in the config, to
* enable the handling of bigger images, which can cause a peak of memory consumption, while * enable the handling of bigger images, which can cause a peak of memory consumption, while
* encoding * encoding
*
* @param $outpath * @param $outpath
* @param array $box * @param array $box
*
* @throws Exception * @throws Exception
*/ */
protected function resizeToFile(string $outpath, array $box): string protected function resizeToFile(string $outpath, array $box): string
@ -406,8 +420,10 @@ class ImageFile extends MediaFile
* @param $maxH int Resulting max height * @param $maxH int Resulting max height
* @param $crop int Crop to the size (not preserving aspect ratio) * @param $crop int Crop to the size (not preserving aspect ratio)
* @param int $rotate * @param int $rotate
* @return array *
* @throws ServerException * @throws ServerException
*
* @return array
*/ */
public static function getScalingValues( public static function getScalingValues(
$width, $width,
@ -422,7 +438,8 @@ class ImageFile extends MediaFile
if ($maxW < 1 || ($maxH !== null && $maxH < 1)) { if ($maxW < 1 || ($maxH !== null && $maxH < 1)) {
throw new ServerException('Bad parameters for ImageFile::getScalingValues'); throw new ServerException('Bad parameters for ImageFile::getScalingValues');
} elseif ($maxH === null) { }
if ($maxH === null) {
// if maxH is null, we set maxH to equal maxW and enable crop // if maxH is null, we set maxH to equal maxW and enable crop
$maxH = $maxW; $maxH = $maxW;
$crop = true; $crop = true;
@ -544,12 +561,15 @@ class ImageFile extends MediaFile
// which we don't want to do because we like original, untouched data! // which we don't want to do because we like original, untouched data!
list($width, $height, $x, $y, $w, $h) = $this->scaleToFit($width, $height, $crop); list($width, $height, $x, $y, $w, $h) = $this->scaleToFit($width, $height, $crop);
$thumb = File_thumbnail::pkeyGet(array( $thumb = File_thumbnail::pkeyGet([
'file_id' => $this->fileRecord->getID(), 'file_id' => $this->fileRecord->getID(),
'width' => $width, 'width' => $width,
'height' => $height, 'height' => $height,
)); ]);
if ($thumb instanceof File_thumbnail) { if ($thumb instanceof File_thumbnail) {
$this->height = $height;
$this->width = $width;
return $thumb; return $thumb;
} }
@ -564,7 +584,7 @@ class ImageFile extends MediaFile
$box = [ $box = [
'width' => $width, 'height' => $height, 'width' => $width, 'height' => $height,
'x' => $x, 'y' => $y, 'x' => $x, 'y' => $y,
'w'=>$w, 'h'=>$h 'w' => $w, 'h' => $h,
]; ];
$outpath = File_thumbnail::path( $outpath = File_thumbnail::path(
@ -587,6 +607,9 @@ class ImageFile extends MediaFile
$height $height
)); ));
$this->height = $box['height'];
$this->width = $box['width'];
// Perform resize and store into file // Perform resize and store into file
$outpath = $this->resizeTo($outpath, $box); $outpath = $this->resizeTo($outpath, $box);
$outname = basename($outpath); $outname = basename($outpath);