FileQuota.php 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. // {{{ License
  3. // This file is part of GNU social - https://www.gnu.org/software/social
  4. //
  5. // GNU social is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Affero General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // GNU social is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Affero General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  17. // }}}
  18. namespace Plugin\FileQuota;
  19. use App\Core\Cache;
  20. use App\Core\DB\DB;
  21. use App\Core\Modules\Plugin;
  22. use App\Util\Common;
  23. use App\Util\Exception\ClientException;
  24. /**
  25. * Check attachment file size quotas
  26. *
  27. * @package GNUsocial
  28. * @ccategory Attachment
  29. *
  30. * @authir Hugo Sales <hugo@hsal.es>
  31. *
  32. * @copyright 2021 Free Software Foundation, Inc http://www.fsf.org
  33. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  34. */
  35. class FileQuota extends Plugin
  36. {
  37. /**
  38. * Check file size to ensure it repects configured file size
  39. * quotas. Handles per file, per user and per user-month quotas.
  40. * Throws on quota violations
  41. */
  42. public function onEnforceQuota(int $filesize)
  43. {
  44. $file_quota = Common::config('attachments', 'file_quota');
  45. if ($filesize > $file_quota) {
  46. // TRANS: Message given if an upload is larger than the configured maximum.
  47. throw new ClientException(_m('No file may be larger than {quota} bytes and the file you sent was {size} bytes. ' .
  48. 'Try to upload a smaller version.', ['quota' => $file_quota, 'size' => $filesize]));
  49. }
  50. $user = Common::user();
  51. $query = <<<END
  52. select sum(at.size) as total
  53. from attachment at
  54. join attachment_to_note an with at.id = an.attachment_id
  55. join note n with an.note_id = n.id
  56. where n.gsactor_id = :actor_id and at.size is not null
  57. END;
  58. $user_quota = Common::config('attachments', 'user_quota');
  59. if ($user_quota != false) {
  60. $cache_key_user_total = 'user-' . $user->getId() . 'file-quota';
  61. $user_total = Cache::get($cache_key_user_total, fn () => DB::dql($query, ['actor_id' => $user->getId()])[0]['total']);
  62. Cache::set($cache_key_user_total, $user_total + $filesize);
  63. if ($user_total + $filesize > $user_quota) {
  64. // TRANS: Message given if an upload would exceed user quota.
  65. throw new ClientException(_m('A file this large would exceed your user quota of {quota} bytes.', ['quota' => $user_quota]));
  66. }
  67. }
  68. $query .= ' AND MONTH(at.modified) = MONTH(CURRENT_DATE())'
  69. . ' AND YEAR(at.modified) = YEAR(CURRENT_DATE())';
  70. $monthly_quota = Common::config('attachments', 'monthly_quota');
  71. if ($monthly_quota != false) {
  72. $cache_key_user_monthly = 'user-' . $user->getId() . 'monthly-file-quota';
  73. $monthly_total = Cache::get($cache_key_user_monthly, fn () => DB::dql($query, ['actor_id' => $user->getId()])[0]['total']);
  74. Cache::set($cache_key_user_monthly, $monthly_total + $filesize);
  75. if ($monthly_total + $filesize > $monthly_quota) {
  76. // TRANS: Message given if an upload would exceed user quota.
  77. throw new ClientException(_m('A file this large would exceed your monthly quota of {quota} bytes.', ['quota' => $monthly_quota]));
  78. }
  79. }
  80. }
  81. }