V2boardInstall.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. $this->info("\ \ / /___ \| __ ) ___ __ _ _ __ __| | ");
  41. $this->info(" \ \ / / __) | _ \ / _ \ / _` | '__/ _` | ");
  42. $this->info(" \ V / / __/| |_) | (_) | (_| | | | (_| | ");
  43. $this->info(" \_/ |_____|____/ \___/ \__,_|_| \__,_| ");
  44. if (\File::exists(base_path() . '/.lock')) {
  45. abort(500, 'V2board 已安装,如需重新安装请删除目录下.lock文件');
  46. }
  47. if (!\File::exists(base_path() . '/.env')) {
  48. if (!copy(base_path() . '/.env.example', base_path() . '/.env')) {
  49. abort(500, '复制环境文件失败,请检查目录权限');
  50. }
  51. }
  52. $this->saveToEnv([
  53. 'APP_KEY' => 'base64:' . base64_encode(Encrypter::generateKey('AES-256-CBC')),
  54. 'DB_HOST' => $this->ask('请输入数据库地址(默认:localhost)', 'localhost'),
  55. 'DB_DATABASE' => $this->ask('请输入数据库名'),
  56. 'DB_USERNAME' => $this->ask('请输入数据库用户名'),
  57. 'DB_PASSWORD' => $this->ask('请输入数据库密码')
  58. ]);
  59. \Artisan::call('config:clear');
  60. \Artisan::call('config:cache');
  61. try {
  62. DB::connection()->getPdo();
  63. } catch (\Exception $e) {
  64. abort(500, '数据库连接失败');
  65. }
  66. $file = \File::get(base_path() . '/database/install.sql');
  67. if (!$file) {
  68. abort(500, '数据库文件不存在');
  69. }
  70. $sql = str_replace("\n", "", $file);
  71. $sql = preg_split("/;/", $sql);
  72. if (!is_array($sql)) {
  73. abort(500, '数据库文件格式有误');
  74. }
  75. $this->info('正在导入数据库请稍等...');
  76. foreach ($sql as $item) {
  77. try {
  78. DB::select(DB::raw($item));
  79. } catch (\Exception $e) {
  80. }
  81. }
  82. $this->info('数据库导入完成');
  83. $email = '';
  84. while (!$email) {
  85. $email = $this->ask('请输入管理员邮箱?');
  86. }
  87. $password = '';
  88. while (!$password) {
  89. $password = $this->ask('请输入管理员密码?');
  90. }
  91. if (!$this->registerAdmin($email, $password)) {
  92. abort(500, '管理员账号注册失败,请重试');
  93. }
  94. $this->info('一切就绪');
  95. $this->info('访问 http(s)://你的站点/admin 进入管理面板');
  96. \File::put(base_path() . '/.lock', time());
  97. } catch (\Exception $e) {
  98. $this->error($e->getMessage());
  99. }
  100. }
  101. private function registerAdmin($email, $password)
  102. {
  103. $user = new User();
  104. $user->email = $email;
  105. if (strlen($password) < 8) {
  106. abort(500, '管理员密码长度最小为8位字符');
  107. }
  108. $user->password = password_hash($password, PASSWORD_DEFAULT);
  109. $user->v2ray_uuid = Helper::guid(true);
  110. $user->token = Helper::guid();
  111. $user->is_admin = 1;
  112. return $user->save();
  113. }
  114. private function saveToEnv($data = [])
  115. {
  116. function set_env_var($key, $value)
  117. {
  118. if (! is_bool(strpos($value, ' '))) {
  119. $value = '"' . $value . '"';
  120. }
  121. $key = strtoupper($key);
  122. $envPath = app()->environmentFilePath();
  123. $contents = file_get_contents($envPath);
  124. preg_match("/^{$key}=[^\r\n]*/m", $contents, $matches);
  125. $oldValue = count($matches) ? $matches[0] : '';
  126. if ($oldValue) {
  127. $contents = str_replace("{$oldValue}", "{$key}={$value}", $contents);
  128. } else {
  129. $contents = $contents . "\n{$key}={$value}\n";
  130. }
  131. $file = fopen($envPath, 'w');
  132. fwrite($file, $contents);
  133. return fclose($file);
  134. }
  135. foreach($data as $key => $value) {
  136. set_env_var($key, $value);
  137. }
  138. return true;
  139. }
  140. }