install.php 13 KB

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