install.php 11 KB

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