FileQuota.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. declare(strict_types = 1);
  3. // {{{ License
  4. // This file is part of GNU social - https://www.gnu.org/software/social
  5. //
  6. // GNU social is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // GNU social is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  18. // }}}
  19. namespace Plugin\FileQuota;
  20. use App\Core\Cache;
  21. use App\Core\DB\DB;
  22. use App\Core\Event;
  23. use function App\Core\I18n\_m;
  24. use App\Core\Modules\Plugin;
  25. use App\Util\Common;
  26. use App\Util\Exception\ClientException;
  27. use App\Util\Exception\ServerException;
  28. /**
  29. * Check attachment file size quotas
  30. *
  31. * @package GNUsocial
  32. * @ccategory Attachment
  33. *
  34. * @author Hugo Sales <hugo@hsal.es>
  35. * @copyright 2021 Free Software Foundation, Inc http://www.fsf.org
  36. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  37. */
  38. class FileQuota extends Plugin
  39. {
  40. public function version(): string
  41. {
  42. return '1.0.0';
  43. }
  44. /**
  45. * Check file size to ensure it respects configured file size
  46. * quotas. Handles per file, per user and per user-month quotas.
  47. * Throws on quota violations
  48. *
  49. * @throws ClientException
  50. * @throws ServerException
  51. */
  52. public function onEnforceUserFileQuota(int $filesize, int $user_id): bool
  53. {
  54. $query = <<<'END'
  55. select sum(at.size) as total
  56. from attachment at
  57. join actor_to_attachment ua with at.id = ua.attachment_id
  58. where ua.actor_id = :actor_id and at.size is not null
  59. END;
  60. $max_user_quota = Common::config('attachments', 'user_quota');
  61. if ($max_user_quota !== false) { // If not disabled
  62. $cache_key_user_total = "FileQuota-total-user-{$user_id}";
  63. $user_total = Cache::get($cache_key_user_total, fn () => DB::dql($query, ['actor_id' => $user_id])[0]['total']);
  64. Cache::set($cache_key_user_total, $user_total + $filesize);
  65. if ($user_total + $filesize > $max_user_quota) {
  66. // TRANS: Message given if an upload would exceed user quota.
  67. throw new ClientException(_m('A file this large would exceed your user quota of {quota} bytes.', ['quota' => $max_user_quota]));
  68. }
  69. }
  70. $query .= ' AND MONTH(at.modified) = MONTH(CURRENT_DATE())'
  71. . ' AND YEAR(at.modified) = YEAR(CURRENT_DATE())';
  72. $monthly_quota = Common::config('attachments', 'monthly_quota');
  73. if ($monthly_quota !== false) { // If not disabled
  74. $cache_key_user_monthly = "FileQuota-monthly-user-{$user_id}";
  75. $monthly_total = Cache::get($cache_key_user_monthly, fn () => DB::dql($query, ['actor_id' => $user_id])[0]['total']);
  76. Cache::set($cache_key_user_monthly, $monthly_total + $filesize);
  77. if ($monthly_total + $filesize > $monthly_quota) {
  78. // TRANS: Message given if an upload would exceed user quota.
  79. throw new ClientException(_m('A file this large would exceed your monthly quota of {quota} bytes.', ['quota' => $monthly_quota]));
  80. }
  81. }
  82. return Event::next;
  83. }
  84. /**
  85. * Event raised when GNU social polls the plugin for information about it.
  86. * Adds this plugin's version information to $versions array
  87. *
  88. * @param array $versions inherited from parent
  89. *
  90. * @return bool true hook value
  91. */
  92. public function onPluginVersion(array &$versions): bool
  93. {
  94. $versions[] = [
  95. 'name' => 'FileQuota',
  96. 'version' => $this->version(),
  97. 'author' => 'Hugo Sales',
  98. 'homepage' => GNUSOCIAL_PROJECT_URL,
  99. 'description', // TRANS: Plugin description. => _m('Plugin to manage user quotas.'),
  100. ];
  101. return Event::next;
  102. }
  103. }