install.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. <?php
  2. function params($key, $default_value = '')
  3. {
  4. return $_POST[$key] ?? $default_value;
  5. }
  6. if (file_exists('./installed.lock')) {//如果没有安装的就提示安装
  7. header('Location: /');
  8. }
  9. // 获取当前PHP版本
  10. $phpVersion = phpversion();
  11. // 检查是否大于7.3
  12. $php_version = false;
  13. if (version_compare($phpVersion, '7.4', '>')) {
  14. $php_version = true;
  15. }
  16. $redis_ext = false;
  17. if (extension_loaded('redis')) {
  18. $redis_ext = true;
  19. }
  20. $fileinfo_ext = false;
  21. if (extension_loaded('fileinfo')) {
  22. $fileinfo_ext = true;
  23. }
  24. // 连接数据库
  25. $servername = 'localhost';
  26. $db_username = params('db_username', false);
  27. $db_password = params('db_password', false);
  28. $db_host = params('db_host', '127.0.0.1');
  29. $db_port = params('db_port', 3306);
  30. $table_name = params('table_name', 'mtab');
  31. $admin_email = params('admin_email', '');
  32. $admin_password = params('admin_password', '');
  33. $database_type = params('database_type', 1);//1=全新安装,2=使用已存在数据库不安装数据库
  34. $redis_host = params('redis_host', '127.0.0.1');
  35. $redis_port = params('redis_port', 6379);
  36. $redis_password = params('redis_password', '');
  37. $error = false;
  38. $conn = null;
  39. $status = false;
  40. if ($db_username && $php_version && $redis_ext && $fileinfo_ext) {
  41. $conn = new mysqli($db_host, $db_username, $db_password, null, $db_port);
  42. if ($conn->connect_error) {
  43. $error = '数据库连接失败';
  44. } else {
  45. $redis = null;
  46. $redis = new Redis();
  47. try {
  48. $redis->connect($redis_host, $redis_port);
  49. if ($redis_password) {
  50. $redis->auth($redis_password);
  51. }
  52. $redis->close();
  53. } catch (Exception $e) {
  54. $error = 'Redis连接失败';
  55. }
  56. if (!$error) {//如果没有错误
  57. if ($database_type == 1) {//全新安装
  58. $sql = "DROP DATABASE $table_name";//删除原来的
  59. $conn->query($sql);
  60. $sql = "CREATE DATABASE $table_name";//创建新的
  61. if ($conn->query($sql) !== TRUE) {
  62. $error = '数据表创建失败';
  63. }
  64. $conn = new mysqli($db_host, $db_username, $db_password, $table_name, $db_port);
  65. $sql_file_content = file_get_contents('../install.sql');
  66. // 解析SQL文件内容并执行
  67. $sql_statements = explode(';', trim($sql_file_content));
  68. foreach ($sql_statements as $sql_statement) {
  69. if (!empty($sql_statement)) {
  70. $conn->query($sql_statement);
  71. }
  72. }
  73. $admin_password = md5($admin_password);
  74. //添加默认管理员
  75. $AdminSql = ("
  76. INSERT INTO user (mail, password, create_time, login_ip, register_ip, manager, login_fail_count, login_time)
  77. VALUES ('$admin_email', '$admin_password', null, null, null, 1, DEFAULT, null);
  78. ");
  79. $conn->query($AdminSql);
  80. $conn->close();
  81. }
  82. file_put_contents('./installed.lock', 'installed');
  83. $status = true;
  84. }
  85. }
  86. }
  87. if ($status) {
  88. $env = <<<EOF
  89. APP_DEBUG = false
  90. [APP]
  91. [DATABASE]
  92. TYPE = mysql
  93. HOSTNAME = {$db_host}
  94. DATABASE = {$table_name}
  95. USERNAME = {$db_username}
  96. PASSWORD = {$db_password}
  97. HOSTPORT = {$db_port}
  98. CHARSET = utf8mb4
  99. DEBUG = false
  100. [REDIS]
  101. HOST = {$redis_host}
  102. PORT = {$redis_port}
  103. PASSWORD = {$redis_password}
  104. SELECT = 0
  105. EOF;
  106. file_put_contents('../.env', $env);
  107. }
  108. ?>
  109. <?php if ($status === false) { ?>
  110. <!DOCTYPE html>
  111. <html lang="zh">
  112. <head>
  113. <title>Mtab安装页面</title>
  114. <meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no'>
  115. <style>
  116. body {
  117. font-family: Arial, sans-serif;
  118. background: url("static/background.jpeg") no-repeat center/cover;
  119. }
  120. form {
  121. max-width: 900px;
  122. margin: 0 auto 100px;
  123. background-color: #fff;
  124. padding: 20px 20px 30px 20px;
  125. border-radius: 12px;
  126. box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  127. }
  128. label {
  129. display: block;
  130. margin-bottom: 10px;
  131. font-weight: bold;
  132. margin-top: 15px;
  133. }
  134. input[type='text'], input[type='password'], input[type='number'] {
  135. text-indent: 15px;
  136. width: calc(100% - 8px);
  137. height: 45px;
  138. line-height: 30px;
  139. border: 2px solid transparent;
  140. border-radius: 10px;
  141. outline: none;
  142. background-color: #f3f3f3;
  143. color: #0d0c22;
  144. transition: .5s ease;
  145. font-size: 16px;
  146. }
  147. input[type='submit'] {
  148. width: 100%;
  149. background-color: rgb(255, 171, 84);
  150. color: #fff;
  151. border-radius: 0.9em;
  152. border: none;
  153. padding: 0.8em 1.2em 0.8em 1em;
  154. transition: all ease-in-out 0.2s;
  155. font-size: 16px;
  156. }
  157. .input:focus, input:hover {
  158. outline: none;
  159. border-color: rgba(255, 171, 84, 0.85);
  160. background-color: #fff;
  161. box-shadow: 0 0 0 5px rgba(253, 224, 99, 0.3);
  162. }
  163. input[type='submit']:hover {
  164. background-color: rgb(255, 171, 84);
  165. }
  166. #error-popup {
  167. position: fixed;
  168. left: 0;
  169. right: 0;
  170. top: 0;
  171. bottom: 0;
  172. margin: auto;
  173. width: 250px;
  174. height: fit-content;
  175. padding: 20px;
  176. background-color: rgb(211, 82, 0);
  177. color: #fff;
  178. border-radius: 12px;
  179. justify-content: center;
  180. z-index: 9999;
  181. }
  182. </style>
  183. <link rel='icon' href='favicon.png'>
  184. </head>
  185. <body>
  186. <?php if ($error) { ?>
  187. <div id='error-popup'>
  188. <div style='text-align: center'><b>遇到错误了!</b></div>
  189. <p style='text-align: center;font-size: 18px'><?php echo $error ?></p>
  190. </div>
  191. <script>
  192. setTimeout(function () {
  193. document.querySelector("#error-popup").style.display = "none";
  194. }, 3000);
  195. </script>
  196. }
  197. <?php } ?>
  198. <h1 style="text-align: center;color: #fff">MTAB书签安装程序</h1>
  199. <form method='post' action='install.php'>
  200. <div style="margin-bottom: 30px">
  201. <b style="margin-right: 40px">请检查并安装以下php扩展:</b>
  202. <b>
  203. php版本大于7.4
  204. <?php if ($php_version) { ?>
  205. <span style='color: limegreen'>✔</span>
  206. <?php } else { ?>
  207. <span style='color: red'>✘</span>
  208. <?php } ?>
  209. </b>
  210. <b style="margin-left: 50px">
  211. redis扩展
  212. <?php if ($redis_ext) { ?>
  213. <span style='color: limegreen'>✔</span>
  214. <?php } else { ?>
  215. <span style='color: red'>✘</span>
  216. <?php } ?>
  217. </b>
  218. <b style='margin-left: 50px'>
  219. fileinfo扩展
  220. <?php if ($fileinfo_ext) { ?>
  221. <span style='color: limegreen'>✔</span>
  222. <?php } else { ?>
  223. <span style='color: red'>✘</span>
  224. <?php } ?>
  225. </b>
  226. </div>
  227. <label for='db_host'>数据库地址:</label>
  228. <input value="<?php echo $db_host; ?>" placeholder="127.0.0.1" type='text' name='db_host' id='db_host' required><br>
  229. <label for='db_port'>数据库端口号:</label>
  230. <input type='number' value="<?php echo $db_port; ?>" placeholder='默认 3306' name='db_port' id='db_port'
  231. required><br>
  232. <label for='db_username'>数据库用户名:</label>
  233. <input type='text' placeholder="请输入数据库用户名" value="<?php echo $db_username; ?>" name='db_username'
  234. id='db_username' required><br>
  235. <label for='db_password'>数据库密码:</label>
  236. <input type='text' name='db_password' value="<?php echo $db_password; ?>" placeholder="请输入数据库密码"
  237. id='db_password' required><br>
  238. <label for='table_name'>数据表名称:</label>
  239. <input type='text' value="<?php echo $table_name; ?>" name='table_name' id='table_name' required><br>
  240. <label for='redis_host'>Redis 地址:</label>
  241. <input type='text' value="<?php echo $redis_host; ?>" name='redis_host' id='redis_host'><br>
  242. <label for='redis_password'>Redis 密码:</label>
  243. <input type='text' value="<?php echo $redis_password; ?>" name='redis_password' placeholder="没有密码请留空"
  244. id='redis_password'><br>
  245. <label for='redis_port'>Redis 端口号:</label>
  246. <input type='number' value="<?php echo $redis_port; ?>" placeholder='默认 6379' name='redis_port'
  247. id='redis_port'
  248. required><br>
  249. <label for='redis_port'>管理员邮箱账号:</label>
  250. <input type='text' value="<?php echo $admin_email; ?>" placeholder='请输入邮箱' name='admin_email'
  251. id='redis_port'
  252. required><br>
  253. <label for='redis_port'>管理员密码:</label>
  254. <input type='text' value="<?php echo $admin_password; ?>" placeholder='请设置管理员账号密码'
  255. name='admin_password'
  256. id='redis_port'
  257. required><br>
  258. <label for='redis_port'>数据库安装其他选项</label>
  259. <label for='install_other'></label>
  260. <label>
  261. <input type='radio' name='database_type' value='1' required>
  262. 全新安装(如果数据库存在则删除原来的数据库,重新安装)
  263. </label>
  264. <label>
  265. <input type='radio' name='database_type' value='2' required>
  266. 使用已存在数据库(不会覆盖数据库,仅安装代码,一般用于负载均衡部署)
  267. </label>
  268. <input type='submit' value='安装' style="margin-top: 50px"></form>
  269. </body>
  270. </html>
  271. <?php } else { ?>
  272. <!DOCTYPE html>
  273. <html lang="zh">
  274. <head>
  275. <meta charset="UTF-8">
  276. <title>网站安装完毕</title>
  277. <style>
  278. body {
  279. font-family: Arial, sans-serif;
  280. background-color: #fff;
  281. text-align: center;
  282. margin: 0;
  283. padding: 0;
  284. }
  285. .container {
  286. background-color: #fff;
  287. padding: 20px;
  288. border-radius: 10px;
  289. box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
  290. margin: 50px auto;
  291. max-width: 800px;
  292. }
  293. h1 {
  294. color: #333;
  295. }
  296. p {
  297. color: #666;
  298. font-size: 18px;
  299. }
  300. .btn-container {
  301. margin-top: 20px;
  302. }
  303. .btn {
  304. display: inline-block;
  305. padding: 10px 20px;
  306. margin-right: 10px;
  307. background-color: rgb(255, 171, 84);
  308. color: #fff;
  309. text-decoration: none;
  310. border-radius: 5px;
  311. transition: background-color 0.3s;
  312. }
  313. .btn:hover {
  314. background-color: rgb(255, 147, 38);
  315. }
  316. </style>
  317. <link rel='icon' href='favicon.png'>
  318. </head>
  319. <body>
  320. <div class='container'>
  321. <h1 style="align-content: center">网站安装完毕</h1>
  322. <div style="display: flex;justify-content: center">
  323. <svg style="width: 100px" t='1694607796889' class='icon' viewBox='0 0 1024 1024' version='1.1'
  324. xmlns='http://www.w3.org/2000/svg'
  325. p-id='40460' width='128' height='128'>
  326. <path d='M512 0C230.4 0 0 230.4 0 512c0 281.6 230.4 512 512 512 281.6 0 512-230.4 512-512C1024 230.4 793.6 0 512 0zM512 960c-249.6 0-448-204.8-448-448 0-249.6 204.8-448 448-448 249.6 0 448 198.4 448 448C960 761.6 761.6 960 512 960zM691.2 339.2 454.4 576 332.8 454.4c-19.2-19.2-51.2-19.2-76.8 0C243.2 480 243.2 512 262.4 531.2l153.6 153.6c19.2 19.2 51.2 19.2 70.4 0l51.2-51.2 224-224c19.2-19.2 25.6-51.2 0-70.4C742.4 320 710.4 320 691.2 339.2z'
  327. fill='#54E283' p-id='40461'></path>
  328. </svg>
  329. </div>
  330. <p>欢迎使用Mtab书签,<br>点击下方按钮跳转到首页。</p>
  331. <div class='btn-container'>
  332. <a class='btn' href='/'>进入首页</a>
  333. </div>
  334. <p>后台进入方式,需要用管理员账户登录客户端<br/> <b>进入设置->个人中心->管理后台</b></p>
  335. </div>
  336. </body>
  337. </html>
  338. <?php } ?>