install.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. function isAjaxRequest()
  3. {
  4. if (isset($_SERVER['HTTP_REFERER'])) {
  5. return true;
  6. } else {
  7. return false;
  8. }
  9. }
  10. if (file_exists("./installed.lock")) {
  11. if (isAjaxRequest()) {
  12. echo json_encode(['code' => 1000, 'msg' => '系统已安装,请勿重复安装!'], JSON_UNESCAPED_UNICODE);
  13. exit;
  14. }
  15. header("Location: /");
  16. exit;
  17. }
  18. class Install
  19. {
  20. protected $defError = [
  21. 1045 => [
  22. 'reason' => '用户名或密码错误,或用户没有访问权限。'
  23. ],
  24. 2002 => [
  25. 'reason' => '数据库服务器未运行或无法连接到指定的主机和端口。'
  26. ],
  27. 1049 => [
  28. 'reason' => '指定的数据库不存在。'
  29. ],
  30. 1044 => [
  31. 'reason' => '用户没有访问指定数据库的权限。'
  32. ],
  33. 2003 => [
  34. 'reason' => '无法连接到 MySQL 服务器,可能是防火墙阻止了连接。'
  35. ]
  36. ];
  37. function __construct() {}
  38. function json($arr)
  39. {
  40. return json_encode($arr, JSON_UNESCAPED_UNICODE);
  41. }
  42. function index(): string
  43. {
  44. $var = [
  45. 'title' => 'mTab新标签页安装程序',
  46. 'customHead|raw' => '',
  47. 'keywords' => '',
  48. 'description' => '',
  49. 'favicon' => '/static/mtab.png',
  50. 'version' => time()
  51. ];
  52. //正则替换模板变量{$title},{$customHead|raw}
  53. $template = file_get_contents(__DIR__ . '/dist/index.html');
  54. return preg_replace_callback('/{\$([\w|]+)}/', function ($matches) use ($var) {
  55. $key = $matches[1];
  56. return $var[$key] ?? '';
  57. }, $template);
  58. }
  59. function connect($form, $tableName = null): mysqli
  60. {
  61. //将上面的值提取到数据库配置中
  62. $db_host = $form['db_host'];
  63. $db_username = $form['db_username'];
  64. $db_password = $form['db_password'];
  65. $db_port = $form['db_port'];
  66. $conn = @new mysqli($db_host, $db_username, $db_password, $tableName, $db_port);
  67. if ($conn->connect_error) {
  68. $errorCode = $conn->connect_errno; // 获取错误代码
  69. // 检查 defError 中是否存在该错误代码
  70. $errorMessage = isset($this->defError[$errorCode])
  71. ? $this->defError[$errorCode]['reason']
  72. : $conn->connect_error; // 如果不存在,返回原始错误信息
  73. throw new Exception($errorMessage, 500);
  74. }
  75. return $conn;
  76. }
  77. function ext()
  78. {
  79. $phpVersion = phpversion();
  80. $php_version = false;
  81. if (version_compare($phpVersion, '7.4', '>')) {
  82. $php_version = true;
  83. }
  84. $fileinfo_ext = false;
  85. if (extension_loaded('fileinfo')) {
  86. $fileinfo_ext = true;
  87. }
  88. $zip_ext = false;
  89. if (extension_loaded('zip')) {
  90. $zip_ext = true;
  91. }
  92. $mysqli_ext = false;
  93. if (extension_loaded('mysqli')) {
  94. $mysqli_ext = true;
  95. }
  96. $curl_ext = false;
  97. if (extension_loaded('curl')) {
  98. $curl_ext = true;
  99. }
  100. return $this->json(['code' => 200, 'data' => [
  101. 'php_version' => $php_version,
  102. 'fileinfo_ext' => $fileinfo_ext,
  103. 'zip_ext' => $zip_ext,
  104. 'mysqli_ext' => $mysqli_ext,
  105. 'curl_ext' => $curl_ext
  106. ]]);
  107. }
  108. function testDb()
  109. {
  110. $data = json_decode(file_get_contents('php://input'), true);
  111. try {
  112. $conn = $this->connect($data);
  113. $conn->close();
  114. return $this->json(['code' => 200, 'msg' => '连接成功']);
  115. } catch (Exception $e) {
  116. return $this->json(['code' => 500, 'msg' => $e->getMessage()]);
  117. }
  118. }
  119. function install()
  120. {
  121. $form = json_decode(file_get_contents('php://input'), true);
  122. $database_type = $form['database_type'];
  123. $table_name = $form['table_name'];
  124. $db_host = $form['db_host'];
  125. $db_username = $form['db_username'];
  126. $db_password = $form['db_password'];
  127. $db_port = $form['db_port'];
  128. $admin_password = $form['admin_password'];
  129. $admin_email = $form['admin_email'];
  130. try {
  131. $conn = $this->connect($form);
  132. } catch (\Exception $e) {
  133. return $this->json(['code' => 500, 'msg' => $e->getMessage()]);
  134. }
  135. if ($database_type == 1) { //全新安装
  136. $sql = "DROP DATABASE $table_name"; //删除原来的
  137. $conn->query($sql);
  138. $sql = "CREATE DATABASE $table_name"; //创建新的
  139. if ($conn->query($sql) !== TRUE) {
  140. return $this->json(['code' => 500, 'msg' => '数据表创建失败']);
  141. }
  142. $conn->close();
  143. $conn = $this->connect($form, $table_name);
  144. //数据库的格式内容数据
  145. $sql_file_content = file_get_contents('../install.sql');
  146. // 解析SQL文件内容并执行
  147. $sql_statements = explode(';', trim($sql_file_content));
  148. foreach ($sql_statements as $sql_statement) {
  149. if (!empty($sql_statement)) {
  150. try {
  151. $conn->query($sql_statement);
  152. } catch (Exception $exception) {
  153. }
  154. }
  155. }
  156. //默认的一些基础数据
  157. $sql_file_content = file_get_contents('../defaultData.sql');
  158. // 解析SQL文件内容并执行
  159. $sql_statements = explode(';', trim($sql_file_content));
  160. foreach ($sql_statements as $sql_statement) {
  161. if (!empty($sql_statement)) {
  162. try {
  163. $conn->query($sql_statement);
  164. } catch (Exception $exception) {
  165. }
  166. }
  167. }
  168. $admin_password = md5($admin_password);
  169. //添加默认管理员
  170. $AdminSql = ("
  171. INSERT INTO user (mail, password, create_time, login_ip, register_ip, manager, login_fail_count, login_time)
  172. VALUES ('$admin_email', '$admin_password', null, null, null, 1, DEFAULT, null);
  173. ");
  174. $conn->query($AdminSql);
  175. $conn->close();
  176. }
  177. $env = <<<EOF
  178. APP_DEBUG = false
  179. [APP]
  180. [DATABASE]
  181. TYPE = mysql
  182. HOSTNAME = {$db_host}
  183. DATABASE = {$table_name}
  184. USERNAME = {$db_username}
  185. PASSWORD = {$db_password}
  186. HOSTPORT = {$db_port}
  187. CHARSET = utf8mb4
  188. DEBUG = false
  189. [CACHE]
  190. DRIVER = file
  191. EOF;
  192. file_put_contents('../.env', $env);
  193. file_put_contents('./installed.lock', 'installed');
  194. return $this->json(['code' => 200, 'msg' => '安装成功']);
  195. }
  196. }
  197. $handle = new Install();
  198. $path = $_GET['s'];
  199. switch ($path):
  200. case '/testDb':
  201. echo $handle->testDb();
  202. break;
  203. case '/install':
  204. echo $handle->install();
  205. break;
  206. case '/ext':
  207. echo $handle->ext();
  208. break;
  209. default:
  210. echo $handle->index();
  211. break;
  212. endswitch;
  213. exit;