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

Размер файла: 7.66Kb
  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. namespace App\Http\Controllers\Admin;
  6.  
  7. use App\Classes\Validator;
  8. use Illuminate\Http\RedirectResponse;
  9. use Illuminate\Http\Request;
  10. use Illuminate\Support\Str;
  11. use Illuminate\View\View;
  12.  
  13. class FileController extends AdminController
  14. {
  15. private $file;
  16. private $path;
  17.  
  18. /**
  19. * Конструктор
  20. *
  21. * @param Request $request
  22. */
  23. public function __construct(Request $request)
  24. {
  25. $this->file = ltrim(check($request->input('file')), '/');
  26. $this->path = rtrim(check($request->input('path')), '/');
  27.  
  28. if (
  29. empty($this->path)
  30. || Str::contains($this->path, '.')
  31. || Str::startsWith($this->path, '/')
  32. || ! file_exists(resource_path('views/' . $this->path))
  33. || ! is_dir(resource_path('views/' . $this->path))
  34. ) {
  35. $this->path = null;
  36. }
  37. }
  38.  
  39. /**
  40. * Главная страница
  41. *
  42. * @return View
  43. */
  44. public function index(): View
  45. {
  46. $path = $this->path;
  47. $elements = preg_grep('/^([^.])/', scandir(resource_path('views/' . $path . $this->file), SCANDIR_SORT_ASCENDING));
  48.  
  49. $folders = [];
  50. $files = [];
  51.  
  52. foreach ($elements as $element) {
  53. if (is_dir(resource_path('views/' . $path . '/' . $element))) {
  54. $folders[] = $element;
  55. } else {
  56. $files[] = $element;
  57. }
  58. }
  59.  
  60. $files = array_merge($folders, $files);
  61.  
  62. $directories = explode('/', (string) $path);
  63.  
  64. return view('admin/files/index', compact('files', 'path', 'directories'));
  65. }
  66.  
  67. /**
  68. * Редактирование файла
  69. *
  70. * @param Request $request
  71. * @param Validator $validator
  72. *
  73. * @return View|RedirectResponse
  74. */
  75. public function edit(Request $request, Validator $validator)
  76. {
  77. $path = $this->path;
  78. $file = $path ? '/' . $this->file : $this->file;
  79. $writable = is_writable(resource_path('views/' . $path . $file . '.blade.php'));
  80.  
  81. if (
  82. ($this->path && ! preg_match('#^([a-z0-9_\-/]+|)$#', $this->path))
  83. || ! preg_match('#^[a-z0-9_\-/]+$#', $this->file)
  84. ) {
  85. abort(404, __('admin.files.file_invalid'));
  86. }
  87.  
  88. if (! file_exists(resource_path('views/' . $this->path . $file . '.blade.php'))) {
  89. abort(404, __('admin.files.file_not_exist'));
  90. }
  91.  
  92. if ($request->isMethod('post')) {
  93. $msg = $request->input('msg');
  94.  
  95. $validator->equal($request->input('_token'), csrf_token(), __('validator.token'))
  96. ->true($writable, ['msg' => __('admin.files.writable')]);
  97.  
  98. if ($validator->isValid()) {
  99. file_put_contents(resource_path('views/' . $this->path . $file . '.blade.php'), $msg);
  100.  
  101. setFlash('success', __('admin.files.file_success_saved'));
  102.  
  103. return redirect('admin/files/edit?path=' . $this->path . '&file=' . $this->file);
  104. }
  105.  
  106. setInput($request->all());
  107. setFlash('danger', $validator->getErrors());
  108. }
  109.  
  110. $contest = file_get_contents(resource_path('views/' . $path . $file . '.blade.php'));
  111.  
  112. return view('admin/files/edit', compact('contest', 'path', 'file', 'writable'));
  113. }
  114.  
  115. /**
  116. * Создание файла
  117. *
  118. * @param Request $request
  119. * @param Validator $validator
  120. *
  121. * @return View|RedirectResponse
  122. */
  123. public function create(Request $request, Validator $validator)
  124. {
  125. if (! is_writable(resource_path('views/' . $this->path))) {
  126. abort(200, __('admin.files.directory_not_writable', ['dir' => $this->path]));
  127. }
  128.  
  129. if ($request->isMethod('post')) {
  130. $filename = check($request->input('filename'));
  131. $dirname = check($request->input('dirname'));
  132.  
  133. $fileName = $this->path ? '/' . $filename : $filename;
  134. $dirName = $this->path ? '/' . $dirname : $dirname;
  135.  
  136. $validator->equal($request->input('_token'), csrf_token(), __('validator.token'));
  137.  
  138. if ($filename) {
  139. $validator->length($filename, 1, 30, ['filename' => __('admin.files.file_required')]);
  140. $validator->false(file_exists(resource_path('views/' . $this->path . $fileName . '.blade.php')), ['filename' => __('admin.files.file_exist')]);
  141. $validator->regex($filename, '|^[a-z0-9_\-]+$|', ['filename' => __('admin.files.file_invalid')]);
  142. } else {
  143. $validator->length($dirname, 1, 30, ['dirname' => __('admin.files.directory_required')]);
  144. $validator->false(file_exists(resource_path('views/' . $this->path . $dirName)), ['dirname' => __('admin.files.directory_exist')]);
  145. $validator->regex($dirname, '|^[a-z0-9_\-]+$|', ['dirname' => __('admin.files.directory_invalid')]);
  146. }
  147.  
  148. if ($validator->isValid()) {
  149. if ($filename) {
  150. file_put_contents(resource_path('views/' . $this->path . $fileName . '.blade.php'), '');
  151. chmod(resource_path('views/' . $this->path . $fileName . '.blade.php'), 0666);
  152.  
  153. setFlash('success', __('admin.files.file_success_created'));
  154.  
  155. return redirect('admin/files/edit?path=' . $this->path . '&file=' . $filename);
  156. }
  157.  
  158. $old = umask(0);
  159. mkdir(resource_path('views/' . $this->path . $dirName), 0777, true);
  160. umask($old);
  161. setFlash('success', __('admin.files.directory_success_created'));
  162.  
  163. return redirect('admin/files?path=' . $this->path . $dirName);
  164. }
  165.  
  166. setInput($request->all());
  167. setFlash('danger', $validator->getErrors());
  168. }
  169.  
  170. return view('admin/files/create', ['path' => $this->path]);
  171. }
  172.  
  173. /**
  174. * Удаление файла
  175. *
  176. * @param Request $request
  177. * @param Validator $validator
  178. *
  179. * @return RedirectResponse
  180. */
  181. public function delete(Request $request, Validator $validator): RedirectResponse
  182. {
  183. if (! is_writable(resource_path('views/' . $this->path))) {
  184. abort(200, __('admin.files.directory_not_writable', ['dir' => $this->path]));
  185. }
  186.  
  187. $filename = check($request->input('filename'));
  188. $dirname = check($request->input('dirname'));
  189.  
  190. $fileName = $this->path ? '/' . $filename : $filename;
  191. $dirName = $this->path ? '/' . $dirname : $dirname;
  192.  
  193. $validator->equal($request->input('_token'), csrf_token(), __('validator.token'));
  194.  
  195. if ($filename) {
  196. $validator->true(file_exists(resource_path('views/' . $this->path . $fileName . '.blade.php')), __('admin.files.file_not_exist'));
  197. $validator->regex($filename, '|^[a-z0-9_\-]+$|', __('admin.files.file_invalid'));
  198. } else {
  199. $validator->true(file_exists(resource_path('views/' . $this->path . $dirName)), __('admin.files.directory_not_exist'));
  200. $validator->regex($dirname, '|^[a-z0-9_\-]+$|', __('admin.files.directory_invalid'));
  201. }
  202.  
  203. if ($validator->isValid()) {
  204. if ($filename) {
  205. unlink(resource_path('views/' . $this->path . $fileName . '.blade.php'));
  206. setFlash('success', __('admin.files.file_success_deleted'));
  207. } else {
  208. deleteDir(resource_path('views/' . $this->path . $dirName));
  209. setFlash('success', __('admin.files.directory_success_deleted'));
  210. }
  211. } else {
  212. setFlash('danger', $validator->getErrors());
  213. }
  214.  
  215. return redirect('admin/files?path=' . $this->path);
  216. }
  217. }