V2boardInstall.php 4.1 KB

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