Controller.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /*
  3. * @copyright Copyright (c) 2019 Sergio Brighenti <sergio@brighenti.me>
  4. *
  5. * @author Sergio Brighenti <sergio@brighenti.me>
  6. *
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. */
  21. namespace App\Controllers;
  22. use App\Database\DB;
  23. use App\Web\Lang;
  24. use App\Web\Session;
  25. use App\Web\View;
  26. use DI\Container;
  27. use DI\DependencyException;
  28. use DI\NotFoundException;
  29. use League\Flysystem\FileNotFoundException;
  30. use League\Flysystem\Filesystem;
  31. use Monolog\Logger;
  32. use Psr\Http\Message\ServerRequestInterface as Request;
  33. use Slim\Exception\HttpNotFoundException;
  34. use Slim\Exception\HttpUnauthorizedException;
  35. /**
  36. * @property Session|null session
  37. * @property View view
  38. * @property DB|null database
  39. * @property Logger|null logger
  40. * @property Filesystem|null storage
  41. * @property Lang lang
  42. * @property array config
  43. */
  44. abstract class Controller
  45. {
  46. /** @var Container */
  47. protected $container;
  48. public function __construct(Container $container)
  49. {
  50. $this->container = $container;
  51. }
  52. /**
  53. * @param $name
  54. *
  55. * @throws DependencyException
  56. * @throws NotFoundException
  57. *
  58. * @return mixed|null
  59. */
  60. public function __get($name)
  61. {
  62. if ($this->container->has($name)) {
  63. return $this->container->get($name);
  64. }
  65. }
  66. /**
  67. * @param $id
  68. *
  69. * @return int
  70. */
  71. protected function getUsedSpaceByUser($id): int
  72. {
  73. $medias = $this->database->query('SELECT `uploads`.`storage_path` FROM `uploads` WHERE `user_id` = ?', $id);
  74. $totalSize = 0;
  75. $filesystem = $this->storage;
  76. foreach ($medias as $media) {
  77. try {
  78. $totalSize += $filesystem->getSize($media->storage_path);
  79. } catch (FileNotFoundException $e) {
  80. $this->logger->error('Error calculating file size', array('exception' => $e));
  81. }
  82. }
  83. return $totalSize;
  84. }
  85. /**
  86. * @param Request $request
  87. * @param $id
  88. * @param bool $authorize
  89. *
  90. * @throws HttpNotFoundException
  91. * @throws HttpUnauthorizedException
  92. *
  93. * @return mixed
  94. */
  95. protected function getUser(Request $request, $id, $authorize = false)
  96. {
  97. $user = $this->database->query('SELECT * FROM `users` WHERE `id` = ? LIMIT 1', $id)->fetch();
  98. if (!$user) {
  99. throw new HttpNotFoundException($request);
  100. }
  101. if ($authorize && $user->id !== $this->session->get('user_id') && !$this->session->get('admin', false)) {
  102. throw new HttpUnauthorizedException($request);
  103. }
  104. return $user;
  105. }
  106. }