install.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. <?php
  2. /**
  3. * OtakuCloud安装程序
  4. *
  5. * 安装完成后建议删除此文件
  6. *
  7. * @author Heron
  8. */
  9. // error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
  10. // ini_set('display_errors', '1');
  11. // 定义目录分隔符
  12. define('DS', DIRECTORY_SEPARATOR);
  13. // 定义根目录
  14. define('ROOT_PATH', __DIR__ . DS . '..' . DS);
  15. // 定义应用目录
  16. define('APP_PATH', ROOT_PATH . 'app' . DS);
  17. // 安装包目录
  18. define('INSTALL_PATH', ROOT_PATH . 'sql' . DS);
  19. // 判断文件或目录是否有写的权限
  20. function is_really_writable($file)
  21. {
  22. if (DIRECTORY_SEPARATOR == '/' and @ ini_get("safe_mode") == false) {
  23. return is_writable($file);
  24. }
  25. if (!is_file($file) or ($fp = @fopen($file, "r+")) === false) {
  26. return false;
  27. }
  28. fclose($fp);
  29. return true;
  30. }
  31. // 写配置文件
  32. function write_ini_file($assoc_arr, $path, $has_sections = false)
  33. {
  34. $content = "";
  35. if ($has_sections) {
  36. foreach ($assoc_arr as $key => $elem) {
  37. $content .= "[" . $key . "]\n";
  38. foreach ($elem as $key2 => $elem2) {
  39. if (is_array($elem2)) {
  40. for ($i = 0; $i < count($elem2); $i++) {
  41. $content .= $key2 . "[] = \"" . $elem2[$i] . "\"\n";
  42. }
  43. } elseif ($elem2 == "")
  44. $content .= $key2 . " = \n";
  45. else
  46. $content .= $key2 . " = \"" . $elem2 . "\"\n";
  47. }
  48. }
  49. } else {
  50. foreach ($assoc_arr as $key => $elem) {
  51. if (is_array($elem)) {
  52. for ($i = 0; $i < count($elem); $i++) {
  53. $content .= $key . "[] = \"" . $elem[$i] . "\"\n";
  54. }
  55. } elseif ($elem == "")
  56. $content .= $key . " = \n";
  57. else
  58. $content .= $key . " = \"" . $elem . "\"\n";
  59. }
  60. }
  61. if (!$handle = fopen($path, 'w')) {
  62. return false;
  63. }
  64. if (!fwrite($handle, $content)) {
  65. return false;
  66. }
  67. fclose($handle);
  68. return true;
  69. }
  70. $sitename = "OtakuCloud";
  71. // 检测目录是否存在
  72. $checkDirs = [
  73. 'vendor',
  74. ];
  75. // 错误信息
  76. $errInfo = '';
  77. // 数据库配置文件
  78. $ConfigFile = ROOT_PATH . '.env';
  79. // 数据库标准配置文件
  80. $exampleConfigFile = ROOT_PATH . '.env.example';
  81. // 锁定的文件
  82. $lockFile = ROOT_PATH . '.env';
  83. if (is_file($lockFile)) {
  84. $errInfo = "当前已经安装{$sitename},如果需要重新安装,请手动移除.env文件";
  85. } elseif (version_compare(PHP_VERSION, '7.1.3', '<')) {
  86. $errInfo = "当前PHP版本(" . PHP_VERSION . ")过低,请使用PHP7.1.3及以上版本";
  87. } elseif (!is_file($exampleConfigFile)) {
  88. $errInfo = "缺失标准配置文件.env.example";
  89. } elseif (!extension_loaded("PDO")) {
  90. $errInfo = "当前PHP环境未启用PDO组件,无法进行安装";
  91. } elseif (!is_really_writable(ROOT_PATH)) {
  92. $open_basedir = ini_get('open_basedir');
  93. if ($open_basedir) {
  94. $dirArr = explode(PATH_SEPARATOR, $open_basedir);
  95. if ($dirArr && in_array(__DIR__, $dirArr)) {
  96. $errInfo = '当前服务器因配置了open_basedir,导致无法读取应用根目录';
  97. }
  98. }
  99. if (!$errInfo) {
  100. $errInfo = '权限不足,无法写入配置文件.env';
  101. }
  102. } else {
  103. $dirArr = [];
  104. foreach ($checkDirs as $k => $v) {
  105. if (!is_dir(ROOT_PATH . $v)) {
  106. $errInfo = '请先在' . $sitename . '根目录下执行 php composer.phar install 安装依赖';
  107. break;
  108. }
  109. }
  110. }
  111. // 当前是POST请求
  112. if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'POST') {
  113. if ($errInfo) {
  114. echo $errInfo;
  115. exit;
  116. }
  117. $err = '';
  118. $APP_KEY = md5(time() . mt_rand(1, 1000000));
  119. $DB_HOST = isset($_POST['mysqlHost']) ? $_POST['mysqlHost'] : '127.0.0.1';
  120. $DB_PORT = isset($_POST['mysqlHostport']) ? $_POST['mysqlHostport'] : 3306;
  121. $hostArr = explode(':', $DB_HOST);
  122. if (count($hostArr) > 1) {
  123. $DB_HOST = $hostArr[0];
  124. $DB_PORT = $hostArr[1];
  125. }
  126. $DB_USERNAME = isset($_POST['mysqlUsername']) ? $_POST['mysqlUsername'] : 'root';
  127. $DB_PASSWORD = isset($_POST['mysqlPassword']) ? $_POST['mysqlPassword'] : '';
  128. $DB_DATABASE = isset($_POST['mysqlDatabase']) ? $_POST['mysqlDatabase'] : 'ssrpanel';
  129. // $adminUsername = isset($_POST['adminUsername']) ? $_POST['adminUsername'] : 'admin';
  130. // $adminPassword = isset($_POST['adminPassword']) ? $_POST['adminPassword'] : 'admin';
  131. // $adminPasswordConfirmation = isset($_POST['adminPasswordConfirmation']) ? $_POST['adminPasswordConfirmation'] : 'admin';
  132. // $adminEmail = isset($_POST['adminEmail']) ? $_POST['adminEmail'] : 'admin@admin.com';
  133. // if ($adminPassword !== $adminPasswordConfirmation) {
  134. // echo "两次输入的密码不一致";
  135. // exit;
  136. // } else if (!preg_match("/^\w+$/", $adminUsername)) {
  137. // echo "用户名只能输入字母、数字、下划线";
  138. // exit;
  139. // } else if (!preg_match("/^[\S]+$/", $adminPassword)) {
  140. // echo "密码不能包含空格";
  141. // exit;
  142. // } else if (strlen($adminUsername) < 3 || strlen($adminUsername) > 12) {
  143. // echo "用户名请输入3~12位字符";
  144. // exit;
  145. // } else if (strlen($adminPassword) < 6 || strlen($adminPassword) > 16 || stripos($adminPassword, ' ') !== false) {
  146. // echo "密码请输入6~16位字符,不能包含空格";
  147. // exit;
  148. // }
  149. try {
  150. // 检测能否读取安装文件
  151. $sql = @file_get_contents(INSTALL_PATH . 'db.sql');
  152. if (!$sql) {
  153. throw new Exception("无法读取所需的sql/db.sql,请检查是否有读权限");
  154. }
  155. $pdo = new PDO("mysql:host={$DB_HOST};port={$DB_PORT}", $DB_USERNAME, $DB_PASSWORD, [
  156. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  157. PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"
  158. ]);
  159. // 检测是否支持innodb存储引擎
  160. $pdoStatement = $pdo->query("SHOW VARIABLES LIKE 'innodb_version'");
  161. $result = $pdoStatement->fetch();
  162. if (!$result) {
  163. throw new Exception("当前数据库不支持innodb存储引擎,请开启后再重新尝试安装");
  164. }
  165. $pdo->query("CREATE DATABASE IF NOT EXISTS `{$DB_DATABASE}` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;");
  166. $pdo->query("USE `{$DB_DATABASE}`");
  167. $pdo->exec($sql);
  168. $config = @file_get_contents($exampleConfigFile);
  169. if (!$config) {
  170. throw new Exception("无法写入读取配置.env.example文件,请检查是否有读权限");
  171. }
  172. $callback = function ($matches) use ($APP_KEY, $DB_HOST, $DB_PORT, $DB_USERNAME, $DB_PASSWORD, $DB_DATABASE) {
  173. $field = $matches[1];
  174. $replace = ${"{$field}"};
  175. return "{$matches[1]}={$replace}" . PHP_EOL;
  176. };
  177. $config = preg_replace_callback("/(APP_KEY|DB_HOST|DB_DATABASE|DB_USERNAME|DB_PASSWORD|DB_PORT)=(.*)(\s+)/", $callback, $config);
  178. // 检测能否成功写入数据库配置
  179. $result = @file_put_contents($ConfigFile, $config);
  180. if (!$result) {
  181. throw new Exception("无法写入数据库信息到.env文件,请检查是否有写权限");
  182. }
  183. echo "success";
  184. } catch (PDOException $e) {
  185. $err = $e->getMessage();
  186. } catch (Exception $e) {
  187. $err = $e->getMessage();
  188. }
  189. echo $err;
  190. exit;
  191. }
  192. ?>
  193. <!doctype html>
  194. <html>
  195. <head>
  196. <meta charset="utf-8">
  197. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  198. <title>安装<?php echo $sitename; ?></title>
  199. <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1">
  200. <meta name="renderer" content="webkit">
  201. <style type="text/css">
  202. body {
  203. background: #5c97bd;
  204. margin: 0;
  205. padding: 0;
  206. line-height: 1.5;
  207. }
  208. body, input, button {
  209. font-family: 'Open Sans', sans-serif;
  210. font-size: 16px;
  211. color: #fff;
  212. }
  213. .container {
  214. max-width: 515px;
  215. margin: 0 auto;
  216. padding: 20px;
  217. text-align: center;
  218. }
  219. a {
  220. color: #fff7d0;
  221. text-decoration: none;
  222. }
  223. a:hover {
  224. text-decoration: underline;
  225. }
  226. h1 {
  227. margin-top: 0;
  228. margin-bottom: 10px;
  229. }
  230. h2 {
  231. font-size: 28px;
  232. font-weight: normal;
  233. color: #fff;
  234. margin-bottom: 0;
  235. }
  236. form {
  237. margin-top: 40px;
  238. }
  239. .form-group {
  240. margin-bottom: 20px;
  241. }
  242. .form-group .form-field:first-child input {
  243. border-top-left-radius: 4px;
  244. border-top-right-radius: 4px;
  245. }
  246. .form-group .form-field:last-child input {
  247. border-bottom-left-radius: 4px;
  248. border-bottom-right-radius: 4px;
  249. }
  250. .form-field input {
  251. background: #6ba3c8;
  252. margin: 0 0 1px;
  253. border: 2px solid transparent;
  254. transition: background 0.2s, border-color 0.2s, color 0.2s;
  255. width: 100%;
  256. padding: 15px 15px 15px 180px;
  257. box-sizing: border-box;
  258. }
  259. .form-field input:focus {
  260. border-color: #e8f6ff;
  261. outline: none;
  262. }
  263. .form-field label {
  264. float: left;
  265. width: 160px;
  266. text-align: right;
  267. margin-right: -160px;
  268. position: relative;
  269. margin-top: 18px;
  270. font-size: 14px;
  271. pointer-events: none;
  272. opacity: 0.7;
  273. }
  274. button, .btn {
  275. background: #fff;
  276. color: #6ba3ca;
  277. border: 0;
  278. font-weight: bold;
  279. border-radius: 4px;
  280. cursor: pointer;
  281. padding: 15px 30px;
  282. -webkit-appearance: none;
  283. }
  284. button[disabled] {
  285. opacity: 0.5;
  286. }
  287. #error, .error, #success, .success {
  288. background: #d66c6c;
  289. color: #fff;
  290. padding: 15px 20px;
  291. border-radius: 4px;
  292. margin-bottom: 20px;
  293. }
  294. #success {
  295. background: #3C5675;
  296. }
  297. #error a, .error a {
  298. color: white;
  299. text-decoration: underline;
  300. }
  301. </style>
  302. </head>
  303. <body>
  304. <div class="container">
  305. <h2>安装 <?php echo $sitename; ?></h2>
  306. <div>
  307. <form method="post">
  308. <?php if ($errInfo): ?>
  309. <div class="error">
  310. <?php echo $errInfo; ?>
  311. </div>
  312. <?php endif; ?>
  313. <div id="error" style="display:none"></div>
  314. <div id="success" style="display:none"></div>
  315. <div class="form-group">
  316. <div class="form-field">
  317. <label>MySQL 数据库地址</label>
  318. <input type="text" name="mysqlHost" value="127.0.0.1" required="">
  319. </div>
  320. <div class="form-field">
  321. <label>MySQL 数据库名</label>
  322. <input type="text" name="mysqlDatabase" value="ssrpanel" required="">
  323. </div>
  324. <div class="form-field">
  325. <label>MySQL 用户名</label>
  326. <input type="text" name="mysqlUsername" value="ssrpanel" required="">
  327. </div>
  328. <div class="form-field">
  329. <label>MySQL 密码</label>
  330. <input type="password" name="mysqlPassword">
  331. </div>
  332. <div class="form-field">
  333. <label>MySQL 端口号</label>
  334. <input type="number" name="mysqlHostport" value="3306">
  335. </div>
  336. </div>
  337. <!-- <div class="form-group">-->
  338. <!-- <div class="form-field">-->
  339. <!-- <label>管理者用户名</label>-->
  340. <!-- <input name="adminUsername" value="admin" required=""/>-->
  341. <!-- </div>-->
  342. <!---->
  343. <!-- <div class="form-field">-->
  344. <!-- <label>管理者Email</label>-->
  345. <!-- <input name="adminEmail" value="admin@admin.com" required="">-->
  346. <!-- </div>-->
  347. <!---->
  348. <!-- <div class="form-field">-->
  349. <!-- <label>管理者密码</label>-->
  350. <!-- <input type="password" name="adminPassword" required="">-->
  351. <!-- </div>-->
  352. <!---->
  353. <!-- <div class="form-field">-->
  354. <!-- <label>重复密码</label>-->
  355. <!-- <input type="password" name="adminPasswordConfirmation" required="">-->
  356. <!-- </div>-->
  357. <!-- </div>-->
  358. <div class="form-buttons">
  359. <button type="submit" <?php echo $errInfo ? 'disabled' : '' ?>>点击安装</button>
  360. </div>
  361. </form>
  362. <!-- jQuery -->
  363. <script src="//cdn.staticfile.org/jquery/2.1.4/jquery.min.js" type="text/javascript"></script>
  364. <script type="text/javascript">
  365. $(function () {
  366. $('form').on('submit', function (e) {
  367. e.preventDefault();
  368. var $button = $(this).find('button')
  369. .text('安装中...')
  370. .prop('disabled', true);
  371. $.post('', $(this).serialize())
  372. .done(function (ret) {
  373. if (ret === 'success') {
  374. $('#error').hide();
  375. $("#success").text("<?php echo $sitename; ?>安装成功,请使用默认用户名admin、密码123456登录,并尽快修改密码并重置订阅地址。").show();
  376. $('<a class="btn" href="./">进入SSRPanel</a>').insertAfter($button);
  377. $button.remove();
  378. localStorage.setItem("fastep", "installed");
  379. } else {
  380. $('#error').show().text(ret);
  381. $button.prop('disabled', false).text('点击安装');
  382. $("html,body").animate({
  383. scrollTop: 0
  384. }, 500);
  385. }
  386. })
  387. .fail(function (data) {
  388. $('#error').show().text('发生错误:\n\n' + data.responseText);
  389. $button.prop('disabled', false).text('点击安装');
  390. $("html,body").animate({
  391. scrollTop: 0
  392. }, 500);
  393. });
  394. return false;
  395. });
  396. });
  397. </script>
  398. </div>
  399. </div>
  400. </body>
  401. </html>