V2boardInstall.php 3.3 KB

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