install.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. // error_reporting(0);
  3. include "model.php";
  4. include "YxEnv.php";
  5. define('install', true);
  6. define('INSTALL_ROOT', __DIR__);
  7. define('TESTING_TABLE', 'config');
  8. $step = $_GET['step'] ?? 1;
  9. $installDir = "install";
  10. $modelInstall = new installModel();
  11. // Env设置
  12. $yxEnv = new YxEnv();
  13. // 检查是否有安装过
  14. $envFilePath = $modelInstall->getAppRoot() . '/.env';
  15. if ($modelInstall->appIsInstalled() && in_array($step, [1, 2, 3, 4])) {
  16. die('可能已经安装过本系统了,请删除配置目录下面的install.lock文件再尝试');
  17. }
  18. // 加载Example文件
  19. $yxEnv->load($modelInstall->getAppRoot() . '/.example.env');
  20. //尝试生成.env
  21. $yxEnv->makeEnv($modelInstall->getAppRoot() . '/.env');
  22. $post = [
  23. 'host' => $_POST['host'] ?? '127.0.0.1',
  24. 'port' => $_POST['port'] ?? '3306',
  25. 'user' => $_POST['user'] ?? 'root',
  26. 'password' => $_POST['password'] ?? '',
  27. 'name' => $_POST['name'] ?? 'likeshopb2c',
  28. 'admin_user' => $_POST['admin_user'] ?? '',
  29. 'admin_password' => $_POST['admin_password'] ?? '',
  30. 'admin_confirm_password' => $_POST['admin_confirm_password'] ?? '',
  31. 'prefix' => $_POST['prefix'] ?? 'ls_',
  32. 'import_test_data' => $_POST['import_test_data'] ?? 'off',
  33. 'clear_db' => $_POST['clear_db'] ?? 'off',
  34. ];
  35. $message = '';
  36. // 检查数据库正确性
  37. if ($step == 4) {
  38. $canNext = true;
  39. if (empty($post['prefix'])) {
  40. $canNext = false;
  41. $message = '数据表前缀不能为空';
  42. } elseif(str_contains($post['name'], '-')) {
  43. $canNext = false;
  44. $message = '数据库名不能包含字符 -';
  45. } elseif(str_contains($post['user'], '-')) {
  46. $canNext = false;
  47. $message = '数据用户名名不能包含字符 -';
  48. } elseif ($post['admin_user'] == '') {
  49. $canNext = false;
  50. $message = '请填写管理员用户名';
  51. } elseif (empty(trim($post['admin_password']))) {
  52. $canNext = false;
  53. $message = '管理员密码不能为空';
  54. } elseif ($post['admin_password'] != $post['admin_confirm_password']) {
  55. $canNext = false;
  56. $message = '两次密码不一致';
  57. } else {
  58. // 检查 数据库信息
  59. $result = $modelInstall->checkConfig($post['name'], $post);
  60. if ($result->result == 'fail') {
  61. $canNext = false;
  62. $message = $result->error;
  63. }
  64. // 导入测试数据
  65. if ($canNext == true && $post['import_test_data'] == 'on') {
  66. if (!$modelInstall->importDemoData()) {
  67. $canNext = false;
  68. $message = '导入测试数据错误';
  69. }
  70. }
  71. // 写配置文件
  72. if ($canNext) {
  73. $yxEnv->putEnv($envFilePath, $post);
  74. $modelInstall->mkLockFile();
  75. }
  76. // 恢复admin和index入口
  77. if ($canNext) {
  78. $modelInstall->restoreIndexLock();
  79. }
  80. }
  81. if (!$canNext)
  82. $step = 3;
  83. }
  84. if ($step == 5) {
  85. $modelInstall->installLog();
  86. }
  87. // 取得安装成功的表
  88. $successTables = $modelInstall->getSuccessTable();
  89. $nextStep = $step + 1;
  90. include __DIR__ . "/template/main.php";