$elem) { $content .= "[" . $key . "]\n"; foreach ($elem as $key2 => $elem2) { if (is_array($elem2)) { for ($i = 0; $i < count($elem2); $i++) { $content .= $key2 . "[] = \"" . $elem2[$i] . "\"\n"; } } elseif ($elem2 == "") $content .= $key2 . " = \n"; else $content .= $key2 . " = \"" . $elem2 . "\"\n"; } } } else { foreach ($assoc_arr as $key => $elem) { if (is_array($elem)) { for ($i = 0; $i < count($elem); $i++) { $content .= $key . "[] = \"" . $elem[$i] . "\"\n"; } } elseif ($elem == "") $content .= $key . " = \n"; else $content .= $key . " = \"" . $elem . "\"\n"; } } if (!$handle = fopen($path, 'w')) { return false; } if (!fwrite($handle, $content)) { return false; } fclose($handle); return true; } $sitename = "OtakuCloud"; // 检测目录是否存在 $checkDirs = [ 'vendor', ]; // 错误信息 $errInfo = ''; // 数据库配置文件 $ConfigFile = ROOT_PATH . '.env'; // 数据库标准配置文件 $exampleConfigFile = ROOT_PATH . '.env.example'; // 锁定的文件 $lockFile = ROOT_PATH . '.env'; if (is_file($lockFile)) { $errInfo = "当前已经安装{$sitename},如果需要重新安装,请手动移除.env文件"; } elseif (version_compare(PHP_VERSION, '7.1.3', '<')) { $errInfo = "当前PHP版本(" . PHP_VERSION . ")过低,请使用PHP7.1.3及以上版本"; } elseif (!is_file($exampleConfigFile)) { $errInfo = "缺失标准配置文件.env.example"; } elseif (!extension_loaded("PDO")) { $errInfo = "当前PHP环境未启用PDO组件,无法进行安装"; } elseif (!is_really_writable(ROOT_PATH)) { $open_basedir = ini_get('open_basedir'); if ($open_basedir) { $dirArr = explode(PATH_SEPARATOR, $open_basedir); if ($dirArr && in_array(__DIR__, $dirArr)) { $errInfo = '当前服务器因配置了open_basedir,导致无法读取应用根目录'; } } if (!$errInfo) { $errInfo = '权限不足,无法写入配置文件.env'; } } else { $dirArr = []; foreach ($checkDirs as $k => $v) { if (!is_dir(ROOT_PATH . $v)) { $errInfo = '请先在' . $sitename . '根目录下执行 php composer.phar install 安装依赖'; break; } } } // 当前是POST请求 if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'POST') { if ($errInfo) { echo $errInfo; exit; } $err = ''; $APP_KEY = md5(time() . mt_rand(1, 1000000)); $DB_HOST = isset($_POST['mysqlHost']) ? $_POST['mysqlHost'] : '127.0.0.1'; $DB_PORT = isset($_POST['mysqlHostport']) ? $_POST['mysqlHostport'] : 3306; $hostArr = explode(':', $DB_HOST); if (count($hostArr) > 1) { $DB_HOST = $hostArr[0]; $DB_PORT = $hostArr[1]; } $DB_USERNAME = isset($_POST['mysqlUsername']) ? $_POST['mysqlUsername'] : 'root'; $DB_PASSWORD = isset($_POST['mysqlPassword']) ? $_POST['mysqlPassword'] : ''; $DB_DATABASE = isset($_POST['mysqlDatabase']) ? $_POST['mysqlDatabase'] : 'ssrpanel'; // $adminUsername = isset($_POST['adminUsername']) ? $_POST['adminUsername'] : 'admin'; // $adminPassword = isset($_POST['adminPassword']) ? $_POST['adminPassword'] : 'admin'; // $adminPasswordConfirmation = isset($_POST['adminPasswordConfirmation']) ? $_POST['adminPasswordConfirmation'] : 'admin'; // $adminEmail = isset($_POST['adminEmail']) ? $_POST['adminEmail'] : 'admin@admin.com'; // if ($adminPassword !== $adminPasswordConfirmation) { // echo "两次输入的密码不一致"; // exit; // } else if (!preg_match("/^\w+$/", $adminUsername)) { // echo "用户名只能输入字母、数字、下划线"; // exit; // } else if (!preg_match("/^[\S]+$/", $adminPassword)) { // echo "密码不能包含空格"; // exit; // } else if (strlen($adminUsername) < 3 || strlen($adminUsername) > 12) { // echo "用户名请输入3~12位字符"; // exit; // } else if (strlen($adminPassword) < 6 || strlen($adminPassword) > 16 || stripos($adminPassword, ' ') !== false) { // echo "密码请输入6~16位字符,不能包含空格"; // exit; // } try { // 检测能否读取安装文件 $sql = @file_get_contents(INSTALL_PATH . 'db.sql'); if (!$sql) { throw new Exception("无法读取所需的sql/db.sql,请检查是否有读权限"); } $pdo = new PDO("mysql:host={$DB_HOST};port={$DB_PORT}", $DB_USERNAME, $DB_PASSWORD, [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8" ]); // 检测是否支持innodb存储引擎 $pdoStatement = $pdo->query("SHOW VARIABLES LIKE 'innodb_version'"); $result = $pdoStatement->fetch(); if (!$result) { throw new Exception("当前数据库不支持innodb存储引擎,请开启后再重新尝试安装"); } $pdo->query("CREATE DATABASE IF NOT EXISTS `{$DB_DATABASE}` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"); $pdo->query("USE `{$DB_DATABASE}`"); $pdo->exec($sql); $config = @file_get_contents($exampleConfigFile); if (!$config) { throw new Exception("无法写入读取配置.env.example文件,请检查是否有读权限"); } $callback = function ($matches) use ($APP_KEY, $DB_HOST, $DB_PORT, $DB_USERNAME, $DB_PASSWORD, $DB_DATABASE) { $field = $matches[1]; $replace = ${"{$field}"}; return "{$matches[1]}={$replace}" . PHP_EOL; }; $config = preg_replace_callback("/(APP_KEY|DB_HOST|DB_DATABASE|DB_USERNAME|DB_PASSWORD|DB_PORT)=(.*)(\s+)/", $callback, $config); // 检测能否成功写入数据库配置 $result = @file_put_contents($ConfigFile, $config); if (!$result) { throw new Exception("无法写入数据库信息到.env文件,请检查是否有写权限"); } echo "success"; } catch (PDOException $e) { $err = $e->getMessage(); } catch (Exception $e) { $err = $e->getMessage(); } echo $err; exit; } ?> 安装<?php echo $sitename; ?>

安装