AdminController.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace App\Controllers;
  3. use App\Database\Migrator;
  4. use League\Flysystem\FileNotFoundException;
  5. use Psr\Http\Message\ResponseInterface as Response;
  6. use Psr\Http\Message\ServerRequestInterface as Request;
  7. class AdminController extends Controller
  8. {
  9. /**
  10. * @param Request $request
  11. * @param Response $response
  12. *
  13. * @return Response
  14. * @throws \Twig\Error\LoaderError
  15. * @throws \Twig\Error\RuntimeError
  16. * @throws \Twig\Error\SyntaxError
  17. *
  18. */
  19. public function system(Request $request, Response $response): Response
  20. {
  21. return view()->render($response, 'dashboard/system.twig', [
  22. 'usersCount' => $usersCount = $this->database->query('SELECT COUNT(*) AS `count` FROM `users`')->fetch()->count,
  23. 'mediasCount' => $mediasCount = $this->database->query('SELECT COUNT(*) AS `count` FROM `uploads`')->fetch()->count,
  24. 'orphanFilesCount' => $orphanFilesCount = $this->database->query('SELECT COUNT(*) AS `count` FROM `uploads` WHERE `user_id` IS NULL')->fetch()->count,
  25. 'totalSize' => humanFileSize($totalSize = $this->database->query('SELECT SUM(`current_disk_quota`) AS `sum` FROM `users`')->fetch()->sum ?? 0),
  26. 'post_max_size' => ini_get('post_max_size'),
  27. 'upload_max_filesize' => ini_get('upload_max_filesize'),
  28. 'installed_lang' => $this->lang->getList(),
  29. 'forced_lang' => $request->getAttribute('forced_lang'),
  30. 'php_version' => phpversion(),
  31. 'max_memory' => ini_get('memory_limit'),
  32. 'register_enabled' => $this->getSetting('register_enabled', 'off'),
  33. 'hide_by_default' => $this->getSetting('hide_by_default', 'off'),
  34. 'copy_url_behavior' => $this->getSetting('copy_url_behavior', 'off'),
  35. 'quota_enabled' => $this->getSetting('quota_enabled', 'off'),
  36. 'default_user_quota' => humanFileSize($this->getSetting('default_user_quota', stringToBytes('1G')), 0, true),
  37. ]);
  38. }
  39. /**
  40. * @param Response $response
  41. *
  42. * @return Response
  43. */
  44. public function deleteOrphanFiles(Response $response): Response
  45. {
  46. $orphans = $this->database->query('SELECT * FROM `uploads` WHERE `user_id` IS NULL')->fetchAll();
  47. $filesystem = $this->storage;
  48. $deleted = 0;
  49. foreach ($orphans as $orphan) {
  50. try {
  51. $filesystem->delete($orphan->storage_path);
  52. $deleted++;
  53. } catch (FileNotFoundException $e) {
  54. }
  55. }
  56. $this->database->query('DELETE FROM `uploads` WHERE `user_id` IS NULL');
  57. $this->session->alert(lang('deleted_orphans', [$deleted]));
  58. return redirect($response, route('system'));
  59. }
  60. /**
  61. * @param Response $response
  62. *
  63. * @return Response
  64. */
  65. public function getThemes(Response $response): Response
  66. {
  67. $apiJson = json_decode(file_get_contents('https://bootswatch.com/api/4.json'));
  68. $out = [];
  69. $out['Default - Bootstrap 4 default theme'] = 'https://bootswatch.com/_vendor/bootstrap/dist/css/bootstrap.min.css';
  70. foreach ($apiJson->themes as $theme) {
  71. $out["{$theme->name} - {$theme->description}"] = $theme->cssMin;
  72. }
  73. return json($response, $out);
  74. }
  75. /**
  76. * @param Response $response
  77. * @return Response
  78. */
  79. public function recalculateUserQuota(Response $response): Response
  80. {
  81. $migrator = new Migrator($this->database, null);
  82. $migrator->reSyncQuotas($this->storage);
  83. $this->session->alert(lang('quota_recalculated'));
  84. return redirect($response, route('system'));
  85. }
  86. }