作者 Karson

优化在非80端口下的跨域判断

优化安装脚本
@@ -199,19 +199,23 @@ class Install extends Command @@ -199,19 +199,23 @@ class Install extends Command
199 // 后台入口文件 199 // 后台入口文件
200 $adminFile = ROOT_PATH . 'public' . DS . 'admin.php'; 200 $adminFile = ROOT_PATH . 'public' . DS . 'admin.php';
201 201
202 - // 生成数据库Env配置文件  
203 - $envFile = ROOT_PATH . '.env.sample';  
204 - $envStr = @file_get_contents($envFile); 202 + // 数据库配置文件
  203 + $dbConfigFile = APP_PATH . 'database.php';
  204 + $config = @file_get_contents($dbConfigFile);
205 $callback = function ($matches) use ($mysqlHostname, $mysqlHostport, $mysqlUsername, $mysqlPassword, $mysqlDatabase, $mysqlPrefix) { 205 $callback = function ($matches) use ($mysqlHostname, $mysqlHostport, $mysqlUsername, $mysqlPassword, $mysqlDatabase, $mysqlPrefix) {
206 $field = "mysql" . ucfirst($matches[1]); 206 $field = "mysql" . ucfirst($matches[1]);
207 $replace = $$field; 207 $replace = $$field;
208 - return "{$matches[1]} = {$replace}" . PHP_EOL; 208 + if ($matches[1] == 'hostport' && $mysqlHostport == 3306) {
  209 + $replace = '';
  210 + }
  211 + return "'{$matches[1]}'{$matches[2]}=>{$matches[3]}Env::get('database.{$matches[1]}', '{$replace}'),";
209 }; 212 };
210 - $envConf = preg_replace_callback('/(hostname|database|username|password|hostport|prefix)\s*=\s*(.*?)\n/', $callback, $envStr); 213 + $config = preg_replace_callback("/'(hostname|database|username|password|hostport|prefix)'(\s+)=>(\s+)Env::get\((.*)\)\,/", $callback, $config);
211 214
212 - $result = @file_put_contents(ROOT_PATH . '.env', $envConf); 215 + // 检测能否成功写入数据库配置
  216 + $result = @file_put_contents($dbConfigFile, $config);
213 if (!$result) { 217 if (!$result) {
214 - throw new Exception(__('The current permissions are insufficient to write the file %s', '.env')); 218 + throw new Exception(__('The current permissions are insufficient to write the file %s', 'application/database.php'));
215 } 219 }
216 220
217 // 变更默认管理员密码 221 // 变更默认管理员密码
@@ -275,15 +279,17 @@ class Install extends Command @@ -275,15 +279,17 @@ class Install extends Command
275 'public' . DS . 'assets' . DS . 'libs' 279 'public' . DS . 'assets' . DS . 'libs'
276 ]; 280 ];
277 281
  282 + //数据库配置文件
  283 + $dbConfigFile = APP_PATH . 'database.php';
  284 +
278 if (version_compare(PHP_VERSION, '7.0.0', '<')) { 285 if (version_compare(PHP_VERSION, '7.0.0', '<')) {
279 throw new Exception(__("The current version %s is too low, please use PHP 7.0 or higher", PHP_VERSION)); 286 throw new Exception(__("The current version %s is too low, please use PHP 7.0 or higher", PHP_VERSION));
280 } 287 }
281 if (!extension_loaded("PDO")) { 288 if (!extension_loaded("PDO")) {
282 throw new Exception(__("PDO is not currently installed and cannot be installed")); 289 throw new Exception(__("PDO is not currently installed and cannot be installed"));
283 } 290 }
284 - $envConfFile = ROOT_PATH . '.env';  
285 - if (is_file($envConfFile) && !is_really_writable($envConfFile)) {  
286 - throw new Exception(__('The current permissions are insufficient to write the file %s', '.env')); 291 + if (!is_really_writable($dbConfigFile)) {
  292 + throw new Exception(__('The current permissions are insufficient to write the configuration file application/database.php'));
287 } 293 }
288 foreach ($checkDirs as $k => $v) { 294 foreach ($checkDirs as $k => $v) {
289 if (!is_dir(ROOT_PATH . $v)) { 295 if (!is_dir(ROOT_PATH . $v)) {
@@ -370,7 +370,7 @@ if (!function_exists('check_cors_request')) { @@ -370,7 +370,7 @@ if (!function_exists('check_cors_request')) {
370 if (isset($_SERVER['HTTP_ORIGIN']) && $_SERVER['HTTP_ORIGIN']) { 370 if (isset($_SERVER['HTTP_ORIGIN']) && $_SERVER['HTTP_ORIGIN']) {
371 $info = parse_url($_SERVER['HTTP_ORIGIN']); 371 $info = parse_url($_SERVER['HTTP_ORIGIN']);
372 $domainArr = explode(',', config('fastadmin.cors_request_domain')); 372 $domainArr = explode(',', config('fastadmin.cors_request_domain'));
373 - $domainArr[] = request()->host(); 373 + $domainArr[] = request()->host(true);
374 if (in_array("*", $domainArr) || in_array($_SERVER['HTTP_ORIGIN'], $domainArr) || (isset($info['host']) && in_array($info['host'], $domainArr))) { 374 if (in_array("*", $domainArr) || in_array($_SERVER['HTTP_ORIGIN'], $domainArr) || (isset($info['host']) && in_array($info['host'], $domainArr))) {
375 header("Access-Control-Allow-Origin: " . $_SERVER['HTTP_ORIGIN']); 375 header("Access-Control-Allow-Origin: " . $_SERVER['HTTP_ORIGIN']);
376 } else { 376 } else {
@@ -290,7 +290,7 @@ return [ @@ -290,7 +290,7 @@ return [
290 //允许跨域的域名,多个以,分隔 290 //允许跨域的域名,多个以,分隔
291 'cors_request_domain' => 'localhost,127.0.0.1', 291 'cors_request_domain' => 'localhost,127.0.0.1',
292 //版本号 292 //版本号
293 - 'version' => '1.2.0.20201001_beta', 293 + 'version' => '1.2.0.20201008_beta',
294 //API接口地址 294 //API接口地址
295 'api_url' => 'https://api.fastadmin.net', 295 'api_url' => 'https://api.fastadmin.net',
296 ], 296 ],