From 095063dcadad86b5927b9259c1eca85ecf88a295 Mon Sep 17 00:00:00 2001 From: net909 Date: Sat, 17 Jan 2026 22:36:34 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B7=B2=E9=85=8D=E7=BD=AE=E5=A5=BD.env?= =?UTF-8?q?=E7=9A=84=E6=83=85=E5=86=B5=E4=B8=8B=E5=AE=89=E8=A3=85=E4=B8=8D?= =?UTF-8?q?=E9=9C=80=E8=A6=81=E9=85=8D=E7=BD=AE=E6=95=B0=E6=8D=AE=E5=BA=93?= =?UTF-8?q?=E8=BF=9E=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controller/Install.php | 166 ++++++++++++++++++++++------------ app/middleware/AuthUser.php | 2 +- app/middleware/LoadConfig.php | 10 ++ app/view/install/index.html | 3 +- 4 files changed, 119 insertions(+), 62 deletions(-) diff --git a/app/controller/Install.php b/app/controller/Install.php index 94be257..caba923 100644 --- a/app/controller/Install.php +++ b/app/controller/Install.php @@ -7,82 +7,128 @@ use Exception; use app\BaseController; use think\facade\Cache; use think\facade\Request; +use think\facade\View; +use think\facade\Db; class Install extends BaseController { public function index() { + $dbconfig = '0'; if (file_exists(app()->getRootPath() . '.env')) { - return '当前已经安装成功,如果需要重新安装,请手动删除根目录.env文件'; + if (checkTableExists('config') || checkTableExists('user') || checkTableExists('domain')) { + return '当前已经安装成功,如果需要重新安装,请手动删除根目录.env文件'; + } else { + $dbconfig = '1'; + } } if (Request::isPost()) { - $mysql_host = input('post.mysql_host', null, 'trim'); - $mysql_port = intval(input('post.mysql_port', '3306')); - $mysql_user = input('post.mysql_user', null, 'trim'); - $mysql_pwd = input('post.mysql_pwd', null, 'trim'); - $mysql_name = input('post.mysql_name', null, 'trim'); - $mysql_prefix = input('post.mysql_prefix', 'cloud_', 'trim'); - $admin_username = input('post.admin_username', null, 'trim'); - $admin_password = input('post.admin_password', null, 'trim'); + if ($dbconfig == '1') { + $admin_username = input('post.admin_username', null, 'trim'); + $admin_password = input('post.admin_password', null, 'trim'); - if (!$mysql_host || !$mysql_user || !$mysql_pwd || !$mysql_name || !$admin_username || !$admin_password) { - return json(['code' => 0, 'msg' => '必填项不能为空']); - } + if (!$admin_username || !$admin_password) { + return json(['code' => 0, 'msg' => '必填项不能为空']); + } - $configData = file_get_contents(app()->getRootPath() . '.example.env'); - $configData = str_replace(['{dbhost}', '{dbname}', '{dbuser}', '{dbpwd}', '{dbport}', '{dbprefix}'], [$mysql_host, $mysql_name, $mysql_user, $mysql_pwd, $mysql_port, $mysql_prefix], $configData); + $sqls = file_get_contents(app()->getAppPath() . 'sql/install.sql'); + $sqls = explode(';', $sqls); + $mysql_prefix = env('database.prefix', 'dnsmgr_'); - try { - $DB = new PDO("mysql:host=" . $mysql_host . ";dbname=" . $mysql_name . ";port=" . $mysql_port, $mysql_user, $mysql_pwd); - } catch (Exception $e) { - if ($e->getCode() == 2002) { - $errorMsg = '连接数据库失败:数据库地址填写错误!'; - } elseif ($e->getCode() == 1045) { - $errorMsg = '连接数据库失败:数据库用户名或密码填写错误!'; - } elseif ($e->getCode() == 1049) { - $errorMsg = '连接数据库失败:数据库名不存在!'; + $password = password_hash($admin_password, PASSWORD_DEFAULT); + $sqls[] = "REPLACE INTO `" . $mysql_prefix . "config` VALUES ('sys_key', '" . random(16) . "')"; + $sqls[] = "INSERT INTO `" . $mysql_prefix . "user` (`username`,`password`,`level`,`regtime`,`lasttime`,`status`) VALUES ('" . addslashes($admin_username) . "', '$password', 2, NOW(), NOW(), 1)"; + + $success = 0; + $error = 0; + $errorMsg = null; + foreach ($sqls as $value) { + $value = trim($value); + if (empty($value)) continue; + $value = str_replace('dnsmgr_', $mysql_prefix, $value); + if (Db::execute($value) === false) { + $error++; + $dberror = Db::getErrorInfo(); + $errorMsg .= $dberror . "\n"; + } else { + $success++; + } + } + if (empty($errorMsg)) { + Cache::clear(); + return json(['code' => 1, 'msg' => '安装完成!成功执行SQL语句' . $success . '条']); } else { - $errorMsg = '连接数据库失败:' . $e->getMessage(); + return json(['code' => 0, 'msg' => $errorMsg]); } - return json(['code' => 0, 'msg' => $errorMsg]); - } - $DB->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT); - $DB->exec("set sql_mode = ''"); - $DB->exec("set names utf8"); - - $sqls = file_get_contents(app()->getAppPath() . 'sql/install.sql'); - $sqls = explode(';', $sqls); - - $password = password_hash($admin_password, PASSWORD_DEFAULT); - $sqls[] = "REPLACE INTO `" . $mysql_prefix . "config` VALUES ('sys_key', '" . random(16) . "')"; - $sqls[] = "INSERT INTO `" . $mysql_prefix . "user` (`username`,`password`,`level`,`regtime`,`lasttime`,`status`) VALUES ('" . addslashes($admin_username) . "', '$password', 2, NOW(), NOW(), 1)"; - - $success = 0; - $error = 0; - $errorMsg = null; - foreach ($sqls as $value) { - $value = trim($value); - if (empty($value)) continue; - $value = str_replace('dnsmgr_', $mysql_prefix, $value); - if ($DB->exec($value) === false) { - $error++; - $dberror = $DB->errorInfo(); - $errorMsg .= $dberror[2] . "\n"; - } else { - $success++; - } - } - if (empty($errorMsg)) { - if (!file_put_contents(app()->getRootPath() . '.env', $configData)) { - return json(['code' => 0, 'msg' => '保存失败,请确保网站根目录有写入权限']); - } - Cache::clear(); - return json(['code' => 1, 'msg' => '安装完成!成功执行SQL语句' . $success . '条']); } else { - return json(['code' => 0, 'msg' => $errorMsg]); + $mysql_host = input('post.mysql_host', null, 'trim'); + $mysql_port = intval(input('post.mysql_port', '3306')); + $mysql_user = input('post.mysql_user', null, 'trim'); + $mysql_pwd = input('post.mysql_pwd', null, 'trim'); + $mysql_name = input('post.mysql_name', null, 'trim'); + $mysql_prefix = input('post.mysql_prefix', 'cloud_', 'trim'); + $admin_username = input('post.admin_username', null, 'trim'); + $admin_password = input('post.admin_password', null, 'trim'); + + if (!$mysql_host || !$mysql_user || !$mysql_pwd || !$mysql_name || !$admin_username || !$admin_password) { + return json(['code' => 0, 'msg' => '必填项不能为空']); + } + + $configData = file_get_contents(app()->getRootPath() . '.example.env'); + $configData = str_replace(['{dbhost}', '{dbname}', '{dbuser}', '{dbpwd}', '{dbport}', '{dbprefix}'], [$mysql_host, $mysql_name, $mysql_user, $mysql_pwd, $mysql_port, $mysql_prefix], $configData); + + try { + $DB = new PDO("mysql:host=" . $mysql_host . ";dbname=" . $mysql_name . ";port=" . $mysql_port, $mysql_user, $mysql_pwd); + } catch (Exception $e) { + if ($e->getCode() == 2002) { + $errorMsg = '连接数据库失败:数据库地址填写错误!'; + } elseif ($e->getCode() == 1045) { + $errorMsg = '连接数据库失败:数据库用户名或密码填写错误!'; + } elseif ($e->getCode() == 1049) { + $errorMsg = '连接数据库失败:数据库名不存在!'; + } else { + $errorMsg = '连接数据库失败:' . $e->getMessage(); + } + return json(['code' => 0, 'msg' => $errorMsg]); + } + $DB->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT); + $DB->exec("set sql_mode = ''"); + $DB->exec("set names utf8"); + + $sqls = file_get_contents(app()->getAppPath() . 'sql/install.sql'); + $sqls = explode(';', $sqls); + + $password = password_hash($admin_password, PASSWORD_DEFAULT); + $sqls[] = "REPLACE INTO `" . $mysql_prefix . "config` VALUES ('sys_key', '" . random(16) . "')"; + $sqls[] = "INSERT INTO `" . $mysql_prefix . "user` (`username`,`password`,`level`,`regtime`,`lasttime`,`status`) VALUES ('" . addslashes($admin_username) . "', '$password', 2, NOW(), NOW(), 1)"; + + $success = 0; + $error = 0; + $errorMsg = null; + foreach ($sqls as $value) { + $value = trim($value); + if (empty($value)) continue; + $value = str_replace('dnsmgr_', $mysql_prefix, $value); + if ($DB->exec($value) === false) { + $error++; + $dberror = $DB->errorInfo(); + $errorMsg .= $dberror[2] . "\n"; + } else { + $success++; + } + } + if (empty($errorMsg)) { + if (!file_put_contents(app()->getRootPath() . '.env', $configData)) { + return json(['code' => 0, 'msg' => '保存失败,请确保网站根目录有写入权限']); + } + Cache::clear(); + return json(['code' => 1, 'msg' => '安装完成!成功执行SQL语句' . $success . '条']); + } else { + return json(['code' => 0, 'msg' => $errorMsg]); + } } } + View::assign('dbconfig', $dbconfig); return view(); } - } diff --git a/app/middleware/AuthUser.php b/app/middleware/AuthUser.php index c2a9d3f..93f1a82 100644 --- a/app/middleware/AuthUser.php +++ b/app/middleware/AuthUser.php @@ -13,7 +13,7 @@ class AuthUser $islogin = false; $cookie = cookie('user_token'); $user = null; - if ($cookie && config_get('sys_key')) { + if ($cookie && config_get('sys_key') && strpos($request->url(), '/install') === false) { $token = authcode($cookie, 'DECODE', config_get('sys_key')); if ($token) { list($type, $uid, $sid, $expiretime) = explode("\t", $token); diff --git a/app/middleware/LoadConfig.php b/app/middleware/LoadConfig.php index 92b0e04..c377dea 100644 --- a/app/middleware/LoadConfig.php +++ b/app/middleware/LoadConfig.php @@ -30,6 +30,16 @@ class LoadConfig return $next($request); } } + if (!checkTableExists('config') && !checkTableExists('user')) { + if (strpos($request->url(), '/install') === false) { + return redirect((string)url('/install'))->header([ + 'Cache-Control' => 'no-store, no-cache, must-revalidate', + 'Pragma' => 'no-cache', + ]); + } else { + return $next($request); + } + } try { $res = Db::name('config')->cache('configs', 0)->column('value', 'key'); diff --git a/app/view/install/index.html b/app/view/install/index.html index 3aac674..da0de21 100644 --- a/app/view/install/index.html +++ b/app/view/install/index.html @@ -162,7 +162,7 @@
- + {if $dbconfig!='1'}
@@ -194,6 +194,7 @@
+ {/if}