V2boardInstall.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Encryption\Encrypter;
  5. use App\Models\User;
  6. use App\Utils\Helper;
  7. use Illuminate\Support\Facades\DB;
  8. class V2boardInstall extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'v2board:install';
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = 'v2board 安装';
  22. /**
  23. * Create a new command instance.
  24. *
  25. * @return void
  26. */
  27. public function __construct()
  28. {
  29. parent::__construct();
  30. }
  31. /**
  32. * Execute the console command.
  33. *
  34. * @return mixed
  35. */
  36. public function handle()
  37. {
  38. try {
  39. $this->info("
  40. __ ______ ____ _
  41. \ \ / /___ \| __ ) ___ __ _ _ __ __| |
  42. \ \ / / __) | _ \ / _ \ / _` | '__/ _` |
  43. \ V / / __/| |_) | (_) | (_| | | | (_| |
  44. \_/ |_____|____/ \___/ \__,_|_| \__,_|
  45. ");
  46. if (\File::exists(base_path() . '/.lock')) {
  47. abort(500, 'V2board 已安装,如需重新安装请删除目录下.lock文件');
  48. }
  49. if (!\File::exists(base_path() . '/.env')) {
  50. if (!copy(base_path() . '/.env.example', base_path() . '/.env')) {
  51. abort(500, '复制环境文件失败,请检查目录权限');
  52. }
  53. }
  54. $this->saveToEnv([
  55. 'APP_KEY' => 'base64:' . base64_encode(Encrypter::generateKey('AES-256-CBC')),
  56. 'DB_HOST' => $this->ask('请输入数据库地址(默认:localhost)', 'localhost'),
  57. 'DB_DATABASE' => $this->ask('请输入数据库名'),
  58. 'DB_USERNAME' => $this->ask('请输入数据库用户名'),
  59. 'DB_PASSWORD' => $this->ask('请输入数据库密码')
  60. ]);
  61. \Artisan::call('config:clear');
  62. \Artisan::call('config:cache');
  63. try {
  64. DB::connection()->getPdo();
  65. } catch (\Exception $e) {
  66. abort(500, '数据库连接失败');
  67. }
  68. $file = \File::get(base_path() . '/database/install.sql');
  69. if (!$file) {
  70. abort(500, '数据库文件不存在');
  71. }
  72. $sql = str_replace("\n", "", $file);
  73. $sql = preg_split("/;/", $sql);
  74. if (!is_array($sql)) {
  75. abort(500, '数据库文件格式有误');
  76. }
  77. $this->info('正在导入数据库请稍等...');
  78. foreach ($sql as $item) {
  79. try {
  80. DB::select(DB::raw($item));
  81. } catch (\Exception $e) {
  82. }
  83. }
  84. $this->info('数据库导入完成');
  85. $email = '';
  86. while (!$email) {
  87. $email = $this->ask('请输入管理员邮箱?');
  88. }
  89. $password = '';
  90. while (!$password) {
  91. $password = $this->ask('请输入管理员密码?');
  92. }
  93. if (!$this->registerAdmin($email, $password)) {
  94. abort(500, '管理员账号注册失败,请重试');
  95. }
  96. $this->info('一切就绪');
  97. \File::put(base_path() . '/.lock', time());
  98. } catch (\Exception $e) {
  99. $this->error($e->getMessage());
  100. }
  101. }
  102. private function registerAdmin($email, $password)
  103. {
  104. $user = new User();
  105. $user->email = $email;
  106. if (strlen($password) < 8) {
  107. abort(500, '管理员密码长度最小为8位字符');
  108. }
  109. $user->password = password_hash($password, PASSWORD_DEFAULT);
  110. $user->v2ray_uuid = Helper::guid(true);
  111. $user->token = Helper::guid();
  112. $user->is_admin = 1;
  113. return $user->save();
  114. }
  115. private function saveToEnv($data = [])
  116. {
  117. function set_env_var($key, $value)
  118. {
  119. if (! is_bool(strpos($value, ' '))) {
  120. $value = '"' . $value . '"';
  121. }
  122. $key = strtoupper($key);
  123. $envPath = app()->environmentFilePath();
  124. $contents = file_get_contents($envPath);
  125. preg_match("/^{$key}=[^\r\n]*/m", $contents, $matches);
  126. $oldValue = count($matches) ? $matches[0] : '';
  127. if ($oldValue) {
  128. $contents = str_replace("{$oldValue}", "{$key}={$value}", $contents);
  129. } else {
  130. $contents = $contents . "\n{$key}={$value}\n";
  131. }
  132. $file = fopen($envPath, 'w');
  133. fwrite($file, $contents);
  134. return fclose($file);
  135. }
  136. foreach($data as $key => $value) {
  137. set_env_var($key, $value);
  138. }
  139. return true;
  140. }
  141. }