FileQuota.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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\Event;
  22. use function App\Core\I18n\_m;
  23. use App\Core\Modules\Plugin;
  24. use App\Util\Common;
  25. use App\Util\Exception\ClientException;
  26. use App\Util\Exception\ServerException;
  27. /**
  28. * Check attachment file size quotas
  29. *
  30. * @package GNUsocial
  31. * @ccategory Attachment
  32. *
  33. * @author Hugo Sales <hugo@hsal.es>
  34. * @copyright 2021 Free Software Foundation, Inc http://www.fsf.org
  35. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  36. */
  37. class FileQuota extends Plugin
  38. {
  39. public function version(): string
  40. {
  41. return '1.0.0';
  42. }
  43. /**
  44. * Check file size to ensure it respects configured file size
  45. * quotas. Handles per file, per user and per user-month quotas.
  46. * Throws on quota violations
  47. *
  48. * @param int $filesize
  49. * @param int $user_id
  50. *
  51. * @throws ClientException
  52. * @throws ServerException
  53. *
  54. * @return bool
  55. */
  56. public function onEnforceUserFileQuota(int $filesize, int $user_id): bool
  57. {
  58. $query = <<<END
  59. select sum(at.size) as total
  60. from attachment at
  61. join gsactor_to_attachment ua with at.id = ua.attachment_id
  62. where ua.gsactor_id = :actor_id and at.size is not null
  63. END;
  64. $max_user_quota = Common::config('attachments', 'user_quota');
  65. if ($max_user_quota !== false) { // If not disabled
  66. $cache_key_user_total = "FileQuota-total-user-{$user_id}";
  67. $user_total = Cache::get($cache_key_user_total, fn () => DB::dql($query, ['actor_id' => $user_id])[0]['total']);
  68. Cache::set($cache_key_user_total, $user_total + $filesize);
  69. if ($user_total + $filesize > $max_user_quota) {
  70. // TRANS: Message given if an upload would exceed user quota.
  71. throw new ClientException(_m('A file this large would exceed your user quota of {quota} bytes.', ['quota' => $max_user_quota]));
  72. }
  73. }
  74. $query .= ' AND MONTH(at.modified) = MONTH(CURRENT_DATE())'
  75. . ' AND YEAR(at.modified) = YEAR(CURRENT_DATE())';
  76. $monthly_quota = Common::config('attachments', 'monthly_quota');
  77. if ($monthly_quota !== false) { // If not disabled
  78. $cache_key_user_monthly = "FileQuota-monthly-user-{$user_id}";
  79. $monthly_total = Cache::get($cache_key_user_monthly, fn () => DB::dql($query, ['actor_id' => $user_id])[0]['total']);
  80. Cache::set($cache_key_user_monthly, $monthly_total + $filesize);
  81. if ($monthly_total + $filesize > $monthly_quota) {
  82. // TRANS: Message given if an upload would exceed user quota.
  83. throw new ClientException(_m('A file this large would exceed your monthly quota of {quota} bytes.', ['quota' => $monthly_quota]));
  84. }
  85. }
  86. return Event::next;
  87. }
  88. /**
  89. * Event raised when GNU social polls the plugin for information about it.
  90. * Adds this plugin's version information to $versions array
  91. *
  92. * @param &$versions array inherited from parent
  93. *
  94. * @return bool true hook value
  95. */
  96. public function onPluginVersion(array &$versions): bool
  97. {
  98. $versions[] = [
  99. 'name' => 'FileQuota',
  100. 'version' => $this->version(),
  101. 'author' => 'Hugo Sales',
  102. 'homepage' => GNUSOCIAL_PROJECT_URL,
  103. 'description' => // TRANS: Plugin description.
  104. _m('Plugin to manage user quotas.'),
  105. ];
  106. return Event::next;
  107. }
  108. }