setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $pdo->query("CREATE DATABASE IF NOT EXISTS `{$mysqlDatabase}` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"); $dbConfig = Config::get('database'); $dbConfig['connections']['mysql'] = array_merge($dbConfig['connections']['mysql'], [ 'type' => "{$config['type']}", 'hostname' => "{$mysqlHostname}", 'hostport' => "{$mysqlHostport}", 'database' => "{$mysqlDatabase}", 'username' => "{$mysqlUsername}", 'password' => "{$mysqlPassword}", 'prefix' => "{$mysqlPrefix}", ]); // 连接install命令中指定的数据库 Config::set(['connections' => $dbConfig['connections']], 'database'); $connect = Db::connect('mysql'); // 查询一次SQL,判断连接是否正常 $connect->execute("SELECT 1"); // 调用原生PDO对象进行批量查询 $connect->getPdo()->exec($sql); } catch (\PDOException $e) { throw new Exception($e->getMessage()); } // 后台入口文件 $adminFile = public_path() . 'admin.php'; // 数据库配置文件 $dbConfigFile = app()->getConfigPath() . 'database.php'; $dbConfigText = @file_get_contents($dbConfigFile); $callback = function ($matches) use ($mysqlHostname, $mysqlHostport, $mysqlUsername, $mysqlPassword, $mysqlDatabase, $mysqlPrefix) { $field = "mysql" . ucfirst($matches[1]); $replace = $$field; if ($matches[1] == 'hostport' && $mysqlHostport == 3306) { $replace = ''; } return "'{$matches[1]}'{$matches[2]}=>{$matches[3]}env('database.{$matches[1]}', '{$replace}'),"; }; $dbConfigText = preg_replace_callback("/'(hostname|database|username|password|hostport|prefix)'(\s+)=>(\s+)env\((.*)\)\,/", $callback, $dbConfigText); // 检测能否成功写入数据库配置 $result = @file_put_contents($dbConfigFile, $dbConfigText); if (!$result) { throw new Exception('当前权限不足,无法写入文件 config/database.php'); } // 写入.example.env文件 $envFile = root_path() . '.example.env'; $envFileContent = @file_get_contents($envFile); if ($envFileContent) { $databasePos = stripos($envFileContent, '[DATABASE]'); if ($databasePos !== false) { // 清理已有数据库配置 $envFileContent = substr($envFileContent, 0, $databasePos); } $envFileContent .= "\n" . '[DATABASE]' . "\n"; $envFileContent .= 'TYPE = mysql' . "\n"; $envFileContent .= 'HOSTNAME = ' . $mysqlHostname . "\n"; $envFileContent .= 'DATABASE = ' . $mysqlDatabase . "\n"; $envFileContent .= 'USERNAME = ' . $mysqlUsername . "\n"; $envFileContent .= 'PASSWORD = ' . $mysqlPassword . "\n"; $envFileContent .= 'HOSTPORT = ' . $mysqlHostport . "\n"; $envFileContent .= 'PREFIX = ' . $mysqlPrefix . "\n"; $envFileContent .= 'CHARSET = utf8mb4' . "\n"; $envFileContent .= 'DEBUG = true' . "\n"; $result = @file_put_contents($envFile, $envFileContent); if (!$result) { throw new Exception('当前权限不足,无法写入文件 .example.env'); } } // 设置新的Token随机密钥key $oldTokenKey = Config::get('token.key'); $newTokenKey = Random::alnum(32); $coreConfigFile = app()->getConfigPath() . 'token.php'; $coreConfigText = @file_get_contents($coreConfigFile); $coreConfigText = preg_replace("/'key'(\s+)=>(\s+)'{$oldTokenKey}'/", "'key'\$1=>\$2'{$newTokenKey}'", $coreConfigText); $result = @file_put_contents($coreConfigFile, $coreConfigText); if (!$result) { throw new Exception('当前权限不足,无法写入文件 config/token.php'); } // 变更默认管理员密码 $adminPassword = $adminPassword ? $adminPassword : Random::alnum(8); $adminEmail = $adminEmail ? $adminEmail : "admin@admin.com"; $newSalt = substr(md5(uniqid(true)), 0, 6); $newPassword = encrypt_password($adminPassword, $newSalt); $data = ['username' => $adminUsername, 'email' => $adminEmail, 'password' => $newPassword, 'encrypt' => $newSalt]; $connect->name('admin')->where('username', 'admin')->update($data); // 变更前台默认用户的密码,随机生成 $newSalt = substr(md5(uniqid(true)), 0, 6); $newPassword = encrypt_password(Random::alnum(8), $newSalt); $connect->name('user')->where('username', 'admin')->update(['password' => $newPassword, 'encrypt' => $newSalt]); // 修改后台入口 $adminName = ''; if (is_file($adminFile)) { $adminName = Random::alnum(10) . '.php'; rename($adminFile, public_path() . $adminName); } $installLockFile = INSTALL_PATH . "install.lock"; //检测能否成功写入lock文件 $result = @file_put_contents($installLockFile, 1); if (!$result) { throw new Exception('当前权限不足,无法写入文件 app/admin/command/Install/install.lock'); } try { //删除安装脚本 @unlink(public_path() . 'install.php'); } catch (\Exception $e) { } return $adminName; } /** * 检测环境 */ public static function checkenv() { //数据库配置文件 $dbConfigFile = app()->getConfigPath() . 'database.php'; if (version_compare(PHP_VERSION, '8.0.0', '<')) { throw new Exception("当前版本" . PHP_VERSION . "过低,请使用PHP8.0.0以上版本"); } if (!extension_loaded("PDO")) { throw new Exception("当前未开启PDO,无法进行安装"); } if (!File::is_really_writable($dbConfigFile)) { throw new Exception("当前权限不足,无法写入文件 config/database.php"); } return true; } }