AdminController.php 4.1 KB

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