From 60cd53895ddaf53eb3df1312532c72a3fdd5fa08 Mon Sep 17 00:00:00 2001 From: HFO4 <912394456@qq.com> Date: Mon, 5 Feb 2018 10:48:11 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=89=E8=A3=85=E5=BC=95=E5=AF=BC=E8=84=9A?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CloudreveInstaller/Installer.php | 109 +++++++++++++++++++++++++ CloudreveInstaller/database_sample.php | 53 ++++++++++++ composer.json | 47 ++++++----- 3 files changed, 190 insertions(+), 19 deletions(-) create mode 100644 CloudreveInstaller/Installer.php create mode 100644 CloudreveInstaller/database_sample.php diff --git a/CloudreveInstaller/Installer.php b/CloudreveInstaller/Installer.php new file mode 100644 index 0000000..409c80c --- /dev/null +++ b/CloudreveInstaller/Installer.php @@ -0,0 +1,109 @@ +getIO(); + $welcomMsg = " + ___ _ _ + / __\ | ___ _ _ __| |_ __ _____ _____ + / / | |/ _ \| | | |/ _` | '__/ _ \ \ / / _ \ +/ /___| | (_) | |_| | (_| | | | __/\ V / __/ +\____/|_|\___/ \__,_|\__,_|_| \___| \_/ \___| + + Ver $version +================================================ +"; + $ioContext->write($welcomMsg); + $sqlInfo = self::getSqlInformation($event); + $ioContext->write(""); + $siteUrl=$ioContext->ask("The full-url to access to your Cloudreve (e.g. https://pan.aoaoao.me/ , 'http' must be included in the front and '/' must be included at the end):"); + $ioContext->write(""); + if(!file_exists('mysql.sql')){ + $ioContext->writeError("[Error] The file mysql.sql not exist.\nInstaller will exit.To retry, run 'composer install'"); + exit(); + } + $sqlSource = file_get_contents('mysql.sql'); + $sqlSource = str_replace("https://cloudreve.org/", $siteUrl, $sqlSource); + $mysqli = @new \mysqli($sqlInfo["hostname"], $sqlInfo["username"], $sqlInfo["password"], $sqlInfo["database"], $sqlInfo["hostport"]); + $ioContext->write("======================="); + $ioContext->write("Starting import sql file..."); + if ($mysqli->multi_query($sqlSource)) { + $ioContext->write("Writing complete."); + $ioContext->write("Writing database.php..."); + if(file_exists('application/database.php')){ + $ioContext->writeError("[Error] The file database.php already exist.\nInstaller will exit.To retry, run 'composer install'"); + $ioContext->write("======================="); + exit(); + } + self::writrConfig($event,$sqlInfo); + $ioContext->write("======================="); + }else{ + $ioContext->writeError("[Error] Writing failed.Installer will exit. To retry, run 'composer install'"); + $ioContext->write("======================="); + } + $ioContext->write(""); + $ioContext->write("Congratulations! Cloudreve has benn install successfully."); + $ioContext->write(""); + $ioContext->write("Here's some informatioin about yor Cloudreve:"); + $ioContext->write("Homepage: $siteUrl"); + $ioContext->write("Admin Panel: ".$siteUrl."Admin"); + $ioContext->write("Default username: admin@cloudreve.org"); + $ioContext->write("Default password: admin"); + $ioContext->write(""); + $ioContext->write("======================="); + $ioContext->write("IMPORTANT! You may still have to configure the URL Rewrite to set everthing to work."); + $ioContext->write("Refer to the install manual for more informatioin."); + $ioContext->write("======================="); + } + + public static function writrConfig(Event $event,$sqlInfo){ + $ioContext = $event->getIO(); + try { + $fileContent = file_get_contents("CloudreveInstaller/database_sample.php"); + $replacement = array( + '{hostname}' => $sqlInfo["hostname"], + '{database}' => $sqlInfo["database"], + '{username}' => $sqlInfo["username"], + '{password}' => $sqlInfo["password"], + '{hostport}' => $sqlInfo["hostport"], + ); + $fileContent = strtr($fileContent,$replacement); + file_put_contents('application/database.php',$fileContent); + }catch (Exception $e) { + $ioContext->writeError("[Error] Writing failed.Installer will exit. To retry, run 'composer install'"); + } + $ioContext->write("Writing complete."); + } + + public static function getSqlInformation(Event $event){ + $ioContext = $event->getIO(); + $hostname=$ioContext->ask("Input the hostname of your MySQL server (Default:127.0.0.1):","127.0.0.1"); + $database=$ioContext->ask("The database name:","127.0.0.1"); + $username=$ioContext->ask("The username of your MySQL server (Default:root):","root"); + $password=$ioContext->askAndHideAnswer("The password of your MySQL server:"); + $hostport=$ioContext->ask("The hostport of your MySQL server (Default:3306):","3306"); + $mysqli = @new \mysqli($hostname, $username, $password, $database, $hostport); + if ($mysqli->connect_error) { + $ioContext->writeError("[Error] Cannot connect to MySQL server, Message:".$mysqli->connect_error); + $ioContext->write(""); + $ioContext->write("Please confirm your connection informatioin:"); + @$mysqli->close(); + return self::getSqlInformation($event); + } + return [ + "hostname" => $hostname, + "database" => $database, + "username" => $username, + "password" => $password, + "hostport" => $hostport, + ]; + } + +} +?> \ No newline at end of file diff --git a/CloudreveInstaller/database_sample.php b/CloudreveInstaller/database_sample.php new file mode 100644 index 0000000..b1c9cf3 --- /dev/null +++ b/CloudreveInstaller/database_sample.php @@ -0,0 +1,53 @@ + +// +---------------------------------------------------------------------- + +return [ + // 数据库类型 + 'type' => 'mysql', + // 服务器地址 + 'hostname' => '{hostname}', + // 数据库名 + 'database' => '{database}', + // 用户名 + 'username' => '{username}', + // 密码 + 'password' => '{password}', + // 端口 + 'hostport' => '{hostport}', + // 连接dsn + 'dsn' => '', + // 数据库连接参数 + 'params' => [], + // 数据库编码默认采用utf8 + 'charset' => 'utf8', + // 数据库表前缀 + 'prefix' => 'sd_', + // 数据库调试模式 + 'debug' => true, + // 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器) + 'deploy' => 0, + // 数据库读写是否分离 主从式有效 + 'rw_separate' => false, + // 读写分离后 主服务器数量 + 'master_num' => 1, + // 指定从服务器序号 + 'slave_no' => '', + // 是否严格检查字段是否存在 + 'fields_strict' => true, + // 数据集返回类型 + 'resultset_type' => 'array', + // 自动写入时间戳字段 + 'auto_timestamp' => false, + // 时间字段取出后的默认时间格式 + 'datetime_format' => 'Y-m-d H:i:s', + // 是否需要进行SQL性能分析 + 'sql_explain' => false, +]; diff --git a/composer.json b/composer.json index 510a8bd..27bbfac 100644 --- a/composer.json +++ b/composer.json @@ -6,33 +6,42 @@ "type": "project", "license": "GPL-3.0-only", "authors": [ - { - "name": "Aaron", - "email": "abslant@foxmail.com", - "homepage": "https://aoaoao.me", - "role": "Developer" - }, - { - "name": "Qnner", - "email": "admin@qnner.com", - "homepage": "http://me.qnner.com", - "role": "Developer" - }, - { - "name": "dune", - "email": "rainaysann@gmail.com", - "homepage": "https://github.com/rainays", - "role": "Developer" - } + { + "name": "Aaron", + "email": "abslant@foxmail.com", + "homepage": "https://aoaoao.me", + "role": "Developer" + }, + { + "name": "Qnner", + "email": "admin@qnner.com", + "homepage": "http://me.qnner.com", + "role": "Developer" + }, + { + "name": "dune", + "email": "rainaysann@gmail.com", + "homepage": "https://github.com/rainays", + "role": "Developer" + } ], "require": { "ext-curl":"*", "ext-fileinfo":"*", "ext-gd":"*", "topthink/think-captcha":"1.*", - "endroid/qrcode": "1.*", "aliyuncs/oss-sdk-php": "~2.0", "sabre/dav":"~3.2.0", "upyun/sdk": "^3.3" + }, + "autoload": { + "psr-0": { + "CloudreveInstaller\\Installer" : "" + } + }, + "scripts": { + "post-install-cmd": [ + "CloudreveInstaller\\Installer::startInstall" + ] } } -- GitLab