Просмотр файла app/Controllers/Admin/CheckerController.php

Размер файла: 2.33Kb
  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. namespace App\Controllers\Admin;
  6.  
  7. use App\Models\User;
  8. use Illuminate\Http\Request;
  9. use Symfony\Component\Finder\Finder;
  10. use Symfony\Component\Finder\SplFileInfo;
  11.  
  12. class CheckerController extends AdminController
  13. {
  14. /**
  15. * Конструктор
  16. */
  17. public function __construct()
  18. {
  19. parent::__construct();
  20.  
  21. if (! isAdmin(User::BOSS)) {
  22. abort(403, __('errors.forbidden'));
  23. }
  24. }
  25.  
  26. /**
  27. * Главная страница
  28. *
  29. * @return string
  30. */
  31. public function index(): string
  32. {
  33. $diff = [];
  34.  
  35. if (file_exists(STORAGE . '/caches/checker.php')) {
  36. $files = $this->scanFiles(BASEDIR);
  37. $filesScan = json_decode(file_get_contents(STORAGE . '/caches/checker.php'), true);
  38.  
  39. $diff['left'] = array_diff($files, $filesScan);
  40. $diff['right'] = array_diff($filesScan, $files);
  41. }
  42.  
  43. return view('admin/checkers/index', compact('diff'));
  44. }
  45.  
  46. /**
  47. * Сканирование сайта
  48. *
  49. * @param Request $request
  50. *
  51. * @return void
  52. */
  53. public function scan(Request $request): void
  54. {
  55. if ($request->input('token') === $_SESSION['token']) {
  56. $files = $this->scanFiles(BASEDIR);
  57.  
  58. file_put_contents(STORAGE . '/caches/checker.php', json_encode($files));
  59.  
  60. setFlash('success', __('admin.checkers.success_crawled'));
  61. } else {
  62. setFlash('danger', __('validator.token'));
  63. }
  64.  
  65. redirect('/admin/checkers');
  66. }
  67.  
  68. /**
  69. * Сканирует директорию сайта
  70. *
  71. * @param string $dir
  72. *
  73. * @return array
  74. */
  75. private function scanFiles($dir): array
  76. {
  77. $state = [];
  78. $excludeFiles = preg_filter('/^/', '*.', explode(',', setting('nocheck')));
  79.  
  80. $finder = new Finder();
  81. $files = $finder->in($dir)
  82. ->files()
  83. ->notName($excludeFiles);
  84.  
  85. if (file_exists(BASEDIR . '/.gitignore')) {
  86. $files->ignoreVCSIgnored(true);
  87. }
  88.  
  89. /** @var SplFileInfo $file */
  90. foreach ($files as $file) {
  91. $state[] = $file->getRelativePathname() . ' / ' . dateFixed($file->getMTime(), 'd.m.y H:i', true) . ' / ' . formatSize($file->getSize());
  92. }
  93.  
  94. return $state;
  95. }
  96. }