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

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