From 5067bcd074e215405d353b41421829b733e8019c Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Fri, 30 Apr 2021 13:14:16 +0000 Subject: [PATCH] [ImageEncoder][FileQuota] Move quota enforcement to it's own plugin, so it can be easily shared and disabled --- plugins/FileQuota/FileQuota.php | 93 +++++++++++++++++++++++++++ plugins/ImageEncoder/ImageEncoder.php | 59 ++++------------- 2 files changed, 107 insertions(+), 45 deletions(-) create mode 100644 plugins/FileQuota/FileQuota.php diff --git a/plugins/FileQuota/FileQuota.php b/plugins/FileQuota/FileQuota.php new file mode 100644 index 0000000000..7810e5c06e --- /dev/null +++ b/plugins/FileQuota/FileQuota.php @@ -0,0 +1,93 @@ +. + +// }}} + +namespace Plugin\FileQuota; + +use App\Core\Cache; +use App\Core\DB\DB; +use App\Core\Modules\Plugin; +use App\Util\Common; +use App\Util\Exception\ClientException; + +/** + * Check attachment file size quotas + * + * @package GNUsocial + * @ccategory Attachment + * + * @authir Hugo Sales + * + * @copyright 2021 Free Software Foundation, Inc http://www.fsf.org + * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later + */ +class FileQuota extends Plugin +{ + /** + * Check file size to ensure it repects configured file size + * quotas. Handles per file, per user and per user-month quotas. + * Throws on quota violations + */ + public function onEnforceQuota(int $filesize) + { + $file_quota = Common::config('attachments', 'file_quota'); + if ($filesize > $file_quota) { + // TRANS: Message given if an upload is larger than the configured maximum. + throw new ClientException(_m('No file may be larger than {quota} bytes and the file you sent was {size} bytes. ' . + 'Try to upload a smaller version.', ['quota' => $file_quota, 'size' => $filesize])); + } + + $user = Common::user(); + $query = <<getId() . 'file-quota'; + $user_total = Cache::get($cache_key_user_total, fn () => DB::dql($query, ['actor_id' => $user->getId()])[0]['total']); + Cache::set($cache_key_user_total, $user_total + $filesize); + + if ($user_total + $filesize > $user_quota) { + // TRANS: Message given if an upload would exceed user quota. + throw new ClientException(_m('A file this large would exceed your user quota of {quota} bytes.', ['quota' => $user_quota])); + } + } + + $query .= ' AND MONTH(at.modified) = MONTH(CURRENT_DATE())' + . ' AND YEAR(at.modified) = YEAR(CURRENT_DATE())'; + + $monthly_quota = Common::config('attachments', 'monthly_quota'); + if ($monthly_quota != false) { + $cache_key_user_monthly = 'user-' . $user->getId() . 'monthly-file-quota'; + $monthly_total = Cache::get($cache_key_user_monthly, fn () => DB::dql($query, ['actor_id' => $user->getId()])[0]['total']); + Cache::set($cache_key_user_monthly, $monthly_total + $filesize); + + if ($monthly_total + $filesize > $monthly_quota) { + // TRANS: Message given if an upload would exceed user quota. + throw new ClientException(_m('A file this large would exceed your monthly quota of {quota} bytes.', ['quota' => $monthly_quota])); + } + } + } +} diff --git a/plugins/ImageEncoder/ImageEncoder.php b/plugins/ImageEncoder/ImageEncoder.php index 798a9ec5db..e777f458a1 100644 --- a/plugins/ImageEncoder/ImageEncoder.php +++ b/plugins/ImageEncoder/ImageEncoder.php @@ -19,8 +19,6 @@ namespace Plugin\ImageEncoder; -use App\Core\Cache; -use App\Core\DB\DB; use App\Core\Event; use App\Core\GSFile; use function App\Core\I18n\_m; @@ -33,6 +31,18 @@ use App\Util\TemporaryFile; use Exception; use Jcupitt\Vips; +/** + * Create thumbnails and validate image attachments + * + * @package GNUsocial + * @ccategory Attachment + * + * @author Diogo Peralta Cordeiro + * @authir Hugo Sales + * + * @copyright 2021 Free Software Foundation, Inc http://www.fsf.org + * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later + */ class ImageEncoder extends Plugin { /** @@ -66,7 +76,7 @@ class ImageEncoder extends Plugin if ($mimetype != $original_mimetype) { // If title seems to be a filename with an extension if (preg_match('/\.[a-z0-9]/i', $title) === 1) { - $title = substr($title, 0, strrpos($title, '.') - 1) . $extension; + $title = substr($title, 0, strrpos($title, '.')) . $extension; } } @@ -80,48 +90,7 @@ class ImageEncoder extends Plugin $filepath = $file->getRealPath(); @unlink($filepath); - $file_quota = Common::config('attachments', 'file_quota'); - if ($filesize > $file_quota) { - // TRANS: Message given if an upload is larger than the configured maximum. - throw new ClientException(_m('No file may be larger than {quota} bytes and the file you sent was {size} bytes. ' . - 'Try to upload a smaller version.', ['quota' => $file_quota, 'size' => $filesize])); - } - - $user = Common::user(); - $query = <<getId() . 'file-quota'; - $user_total = Cache::get($cache_key_user_total, fn () => DB::dql($query, ['actor_id' => $user->getId()])[0]['total']); - Cache::set($cache_key_user_total, $user_total + $filesize); - - if ($user_total + $filesize > $user_quota) { - // TRANS: Message given if an upload would exceed user quota. - throw new ClientException(_m('A file this large would exceed your user quota of {quota} bytes.', ['quota' => $user_quota])); - } - } - - $query .= ' AND MONTH(at.modified) = MONTH(CURRENT_DATE())' - . ' AND YEAR(at.modified) = YEAR(CURRENT_DATE())'; - - $monthly_quota = Common::config('attachments', 'monthly_quota'); - if ($monthly_quota != false) { - $cache_key_user_monthly = 'user-' . $user->getId() . 'monthly-file-quota'; - $monthly_total = Cache::get($cache_key_user_monthly, fn () => DB::dql($query, ['actor_id' => $user->getId()])[0]['total']); - Cache::set($cache_key_user_monthly, $monthly_total + $filesize); - - if ($monthly_total + $filesize > $monthly_quota) { - // TRANS: Message given if an upload would exceed user quota. - throw new ClientException(_m('A file this large would exceed your monthly quota of {quota} bytes.', ['quota' => $monthly_quota])); - } - } + Event::handle('EnforceQuota', [$filesize]); $temp->commit($filepath);