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

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