install.php 15 KB

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