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

Размер файла: 8.76Kb
  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. namespace App\Http\Controllers;
  6.  
  7. use App\Classes\Validator;
  8. use App\Models\News;
  9. use App\Models\Setting;
  10. use App\Models\User;
  11. use Illuminate\Http\RedirectResponse;
  12. use Illuminate\Http\Request;
  13. use Illuminate\Support\Facades\Artisan;
  14. use Illuminate\Support\Facades\Lang;
  15. use Illuminate\Support\Facades\Schema;
  16. use Illuminate\View\View;
  17.  
  18. class InstallController extends Controller
  19. {
  20. /**
  21. * Конструктор
  22. */
  23. public function __construct(Request $request)
  24. {
  25. $lang = $request->input('lang', 'ru');
  26.  
  27. Lang::setLocale($lang);
  28.  
  29. view()->share('lang', $lang);
  30. }
  31.  
  32. /**
  33. * Главная страница
  34. *
  35. * @return View
  36. */
  37. public function index(): View
  38. {
  39. $keys = [
  40. 'APP_ENV',
  41. 'APP_DEBUG',
  42. 'DB_CONNECTION',
  43. 'DB_HOST',
  44. 'DB_PORT',
  45. 'DB_DATABASE',
  46. 'DB_USERNAME',
  47. 'APP_URL',
  48. 'APP_EMAIL',
  49. 'APP_ADMIN',
  50. ];
  51.  
  52. $versions = [
  53. 'php' => '8.0.2',
  54. 'mysql' => '5.7.8',
  55. 'maria' => '10.2.7',
  56. 'pgsql' => '9.2',
  57. ];
  58.  
  59. $storage = glob(storage_path('{*,*/*,*/*/*}'), GLOB_BRACE | GLOB_ONLYDIR);
  60. $uploads = glob(public_path('uploads/*'), GLOB_ONLYDIR);
  61. $dirs = [public_path('assets/modules'), base_path('bootstrap/cache')];
  62.  
  63. $dirs = array_merge($storage, $uploads, $dirs);
  64. $languages = array_map('basename', glob(resource_path('lang/*'), GLOB_ONLYDIR));
  65.  
  66. return view('install/index', compact('keys', 'languages', 'versions', 'dirs'));
  67. }
  68.  
  69. /**
  70. * Проверка статуса
  71. *
  72. * @return View
  73. */
  74. public function status(): View
  75. {
  76. if (! Schema::hasTable('migrations')) {
  77. Artisan::call('migrate:install');
  78. }
  79.  
  80. Artisan::call('migrate:status');
  81. $output = Artisan::output();
  82.  
  83. return view('install/status', compact('output'));
  84. }
  85.  
  86. /**
  87. * Выполнение миграций
  88. *
  89. * @return View
  90. */
  91. public function migrate(): View
  92. {
  93. Artisan::call('migrate', ['--force' => true]);
  94. $output = Artisan::output();
  95.  
  96. if (! setting('app_installed')) {
  97. Artisan::call('key:generate', ['--force' => true]);
  98. }
  99.  
  100. Artisan::call('cache:clear');
  101. Artisan::call('route:clear');
  102. Artisan::call('config:clear');
  103.  
  104. return view('install/migrate', compact('output'));
  105. }
  106.  
  107. /**
  108. * Заполнение БД
  109. *
  110. * @return View
  111. */
  112. public function seed(): View
  113. {
  114. Artisan::call('db:seed', ['--force' => true]);
  115. $output = Artisan::output();
  116.  
  117. Artisan::call('cache:clear');
  118. Artisan::call('route:clear');
  119. Artisan::call('config:clear');
  120.  
  121. return view('install/seed', compact('output'));
  122. }
  123.  
  124. /**
  125. * Создание администратора
  126. *
  127. * @param Request $request
  128. * @param Validator $validator
  129. *
  130. * @return View|RedirectResponse
  131. */
  132. public function account(Request $request, Validator $validator)
  133. {
  134. $lang = $request->input('lang', 'ru');
  135. $login = $request->input('login');
  136. $password = $request->input('password');
  137. $password2 = $request->input('password2');
  138. $email = strtolower((string) $request->input('email'));
  139.  
  140. if ($request->isMethod('post')) {
  141. $validator->regex($login, '|^[a-z0-9\-]+$|i', ['login' => __('validator.login')])
  142. ->regex(utfSubstr($login, 0, 1), '|^[a-z0-9]+$|i', ['login' => __('users.login_begin_requirements')])
  143. ->email($email, ['email' => __('validator.email')])
  144. ->length($login, 3, 20, ['login' => __('users.login_length_requirements')])
  145. ->length($password, 6, 20, ['password' => __('users.password_length_requirements')])
  146. ->equal($password, $password2, ['password2' => __('users.passwords_different')])
  147. ->false(ctype_digit($login), ['login' => __('users.field_characters_requirements')])
  148. ->false(ctype_digit($password), ['password' => __('users.field_characters_requirements')])
  149. ->false(substr_count($login, '-') > 2, ['login' => __('users.login_hyphens_requirements')]);
  150.  
  151. if ($validator->isValid()) {
  152. // Проверка логина на существование
  153. $checkLogin = User::query()->where('login', $login)->exists();
  154. $validator->false($checkLogin, ['login' => __('users.login_already_exists')]);
  155.  
  156. // Проверка email на существование
  157. $checkMail = User::query()->where('email', $email)->exists();
  158. $validator->false($checkMail, ['email' => __('users.email_already_exists')]);
  159. }
  160.  
  161. if ($validator->isValid()) {
  162. /** @var User $user */
  163. $user = User::query()->create([
  164. 'login' => $login,
  165. 'password' => password_hash($password, PASSWORD_BCRYPT),
  166. 'email' => $email,
  167. 'level' => User::BOSS,
  168. 'gender' => User::MALE,
  169. 'themes' => 'default',
  170. 'point' => 500,
  171. 'money' => 100000,
  172. 'status' => 'Boss',
  173. 'language' => $lang,
  174. 'created_at' => SITETIME,
  175. ]);
  176.  
  177. // ------------- Авторизация -----------//
  178. User::auth($login, $password);
  179.  
  180. // -------------- Приват ---------------//
  181. $text = __('install.text_message', ['login' => $login]);
  182. $user->sendMessage(null, $text);
  183.  
  184. // -------------- Новость ---------------//
  185. $textnews = __('install.text_news');
  186.  
  187. News::query()->create([
  188. 'title' => __('install.welcome'),
  189. 'text' => $textnews,
  190. 'user_id' => $user->id,
  191. 'created_at' => SITETIME,
  192. ]);
  193.  
  194. clearCache(['statNews', 'lastNews', 'statNewsDate', 'NewsFeed']);
  195.  
  196. return redirect('/install/finish');
  197. }
  198.  
  199. setInput($request->all());
  200. setFlash('danger', $validator->getErrors());
  201. }
  202.  
  203. return view('install/account', compact('login', 'email'));
  204. }
  205.  
  206. /**
  207. * Завершение установки
  208. *
  209. * @return View
  210. */
  211. public function finish(): View
  212. {
  213. // -------------- Установка -------------//
  214. Setting::query()
  215. ->where('name', 'app_installed')
  216. ->update([
  217. 'value' => 1,
  218. ]);
  219.  
  220. clearCache('settings');
  221.  
  222. return view('install/finish');
  223. }
  224.  
  225. /**
  226. * Parse PHP modules
  227. *
  228. * @return array
  229. */
  230. private static function parsePhpModules(): array
  231. {
  232. ob_start();
  233. phpinfo(INFO_MODULES);
  234. $s = ob_get_clean();
  235. $s = strip_tags($s, '<h2><th><td>');
  236. $s = preg_replace('/<th[^>]*>([^<]+)<\/th>/', "<info>\\1</info>", $s);
  237. $s = preg_replace('/<td[^>]*>([^<]+)<\/td>/', "<info>\\1</info>", $s);
  238. $vTmp = preg_split('/(<h2[^>]*>[^<]+<\/h2>)/', $s, -1, PREG_SPLIT_DELIM_CAPTURE);
  239. $vModules = [];
  240. $iMax = count($vTmp);
  241.  
  242. for ($i = 1; $i < $iMax; $i++) {
  243. if (preg_match('/<h2[^>]*>([^<]+)<\/h2>/', $vTmp[$i], $vMat)) {
  244. $vName = trim($vMat[1]);
  245. $vTmp2 = explode("\n", $vTmp[$i + 1]);
  246. foreach ($vTmp2 as $vOne) {
  247. $vPat = '<info>([^<]+)<\/info>';
  248. $vPat3 = "/$vPat\s*$vPat\s*$vPat/";
  249. $vPat2 = "/$vPat\s*$vPat/";
  250. if (preg_match($vPat3, $vOne, $vMat)) {
  251. $vModules[$vName][trim($vMat[1])] = [trim($vMat[2]), trim($vMat[3])];
  252. } elseif (preg_match($vPat2, $vOne, $vMat)) {
  253. $vModules[$vName][trim($vMat[1])] = trim($vMat[2]);
  254. }
  255. }
  256. }
  257. }
  258.  
  259. return $vModules;
  260. }
  261.  
  262. /**
  263. * Get PHP module setting
  264. *
  265. * @param string $pModuleName
  266. * @param array $pSettings
  267. *
  268. * @return string
  269. */
  270. public static function getModuleSetting(string $pModuleName, array $pSettings): string
  271. {
  272. $vModules = self::parsePhpModules();
  273.  
  274. foreach ($pSettings as $pSetting) {
  275. if (isset($vModules[$pModuleName][$pSetting])) {
  276. return $vModules[$pModuleName][$pSetting];
  277. }
  278. }
  279.  
  280. return __('main.undefined');
  281. }
  282. }