Controller.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace App\Controllers;
  3. use App\Database\DB;
  4. use App\Web\Lang;
  5. use App\Web\Session;
  6. use App\Web\View;
  7. use DI\Container;
  8. use DI\DependencyException;
  9. use DI\NotFoundException;
  10. use League\Flysystem\FileNotFoundException;
  11. use League\Flysystem\Filesystem;
  12. use Monolog\Logger;
  13. use Psr\Http\Message\ServerRequestInterface as Request;
  14. use Slim\Exception\HttpNotFoundException;
  15. use Slim\Exception\HttpUnauthorizedException;
  16. /**
  17. * @property Session|null session
  18. * @property View view
  19. * @property DB|null database
  20. * @property Logger|null logger
  21. * @property Filesystem|null storage
  22. * @property Lang lang
  23. * @property array config
  24. */
  25. abstract class Controller
  26. {
  27. /** @var Container */
  28. protected $container;
  29. public function __construct(Container $container)
  30. {
  31. $this->container = $container;
  32. }
  33. /**
  34. * @param $name
  35. *
  36. * @return mixed|null
  37. * @throws NotFoundException
  38. *
  39. * @throws DependencyException
  40. */
  41. public function __get($name)
  42. {
  43. if ($this->container->has($name)) {
  44. return $this->container->get($name);
  45. }
  46. return null;
  47. }
  48. /**
  49. * @param $id
  50. *
  51. * @return object
  52. */
  53. protected function getUsedSpaceByUser($id)
  54. {
  55. return $this->database->query('SELECT `current_disk_quota`, `max_disk_quota` FROM `users` WHERE `id` = ?', $id)->fetch();
  56. }
  57. /**
  58. * @param Request $request
  59. * @param $userId
  60. * @param $fileSize
  61. * @param bool $dec
  62. * @return bool
  63. * @throws HttpNotFoundException
  64. * @throws HttpUnauthorizedException
  65. */
  66. protected function updateUserQuota(Request $request, $userId, $fileSize, $dec = false)
  67. {
  68. $user = $this->getUser($request, $userId);
  69. if ($dec) {
  70. $tot = max($user->current_disk_quota - $fileSize, 0);
  71. } else {
  72. $tot = $user->current_disk_quota + $fileSize;
  73. $quotaEnabled = $this->database->query('SELECT `value` FROM `settings` WHERE `key` = \'quota_enabled\'')->fetch()->value ?? 'off';
  74. if ($quotaEnabled === 'on' && $user->max_disk_quota > 0 && $user->max_disk_quota < $tot) {
  75. return false;
  76. }
  77. }
  78. $this->database->query('UPDATE `users` SET `current_disk_quota`=? WHERE `id` = ?', [
  79. $tot,
  80. $user->id,
  81. ]);
  82. return true;
  83. }
  84. /**
  85. * @param Request $request
  86. * @param $id
  87. * @param bool $authorize
  88. *
  89. * @return mixed
  90. * @throws HttpUnauthorizedException
  91. *
  92. * @throws HttpNotFoundException
  93. */
  94. protected function getUser(Request $request, $id, $authorize = false)
  95. {
  96. $user = $this->database->query('SELECT * FROM `users` WHERE `id` = ? LIMIT 1', $id)->fetch();
  97. if (!$user) {
  98. throw new HttpNotFoundException($request);
  99. }
  100. if ($authorize && $user->id !== $this->session->get('user_id') && !$this->session->get('admin', false)) {
  101. throw new HttpUnauthorizedException($request);
  102. }
  103. return $user;
  104. }
  105. /**
  106. * @param $userId
  107. * @throws \Exception
  108. */
  109. protected function refreshRememberCookie($userId)
  110. {
  111. $selector = bin2hex(random_bytes(8));
  112. $token = bin2hex(random_bytes(32));
  113. $expire = time() + 604800; // a week
  114. $this->database->query('UPDATE `users` SET `remember_selector`=?, `remember_token`=?, `remember_expire`=? WHERE `id`=?', [
  115. $selector,
  116. password_hash($token, PASSWORD_DEFAULT),
  117. date('Y-m-d\TH:i:s', $expire),
  118. $userId,
  119. ]);
  120. // Workaround for php <= 7.3
  121. if (PHP_VERSION_ID < 70300) {
  122. setcookie('remember', "{$selector}:{$token}", $expire, '; SameSite=Lax', '', false, true);
  123. } else {
  124. setcookie('remember', "{$selector}:{$token}", [
  125. 'expires' => $expire,
  126. 'httponly' => true,
  127. 'samesite' => 'Lax',
  128. ]);
  129. }
  130. }
  131. /**
  132. * @return string
  133. */
  134. protected function generateUserUploadToken(): string
  135. {
  136. do {
  137. $token = 'token_'.md5(uniqid('', true));
  138. } while ($this->database->query('SELECT COUNT(*) AS `count` FROM `users` WHERE `token` = ?', $token)->fetch()->count > 0);
  139. return $token;
  140. }
  141. }