You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
3.8 KiB

1 year ago
  1. <?php
  2. namespace app\controller;
  3. use PDO;
  4. use Exception;
  5. use app\BaseController;
  6. use think\facade\View;
  7. use think\facade\Cache;
  8. class Install extends BaseController
  9. {
  10. public function index()
  11. {
  12. if (file_exists(app()->getRootPath().'.env')){
  13. return '当前已经安装成功,如果需要重新安装,请手动删除根目录.env文件';
  14. }
  15. if(request()->isPost()){
  16. $mysql_host = input('post.mysql_host', null, 'trim');
  17. $mysql_port = intval(input('post.mysql_port', '3306'));
  18. $mysql_user = input('post.mysql_user', null, 'trim');
  19. $mysql_pwd = input('post.mysql_pwd', null, 'trim');
  20. $mysql_name = input('post.mysql_name', null, 'trim');
  21. $mysql_prefix = input('post.mysql_prefix', 'cloud_', 'trim');
  22. $admin_username = input('post.admin_username', null, 'trim');
  23. $admin_password = input('post.admin_password', null, 'trim');
  24. if(!$mysql_host || !$mysql_user || !$mysql_pwd || !$mysql_name || !$admin_username || !$admin_password){
  25. return json(['code'=>0, 'msg'=>'必填项不能为空']);
  26. }
  27. $configdata = file_get_contents(app()->getRootPath().'.env.example');
  28. $configdata = str_replace(['{dbhost}','{dbname}','{dbuser}','{dbpwd}','{dbport}','{dbprefix}'], [$mysql_host, $mysql_name, $mysql_user, $mysql_pwd, $mysql_port, $mysql_prefix], $configdata);
  29. try{
  30. $DB=new PDO("mysql:host=".$mysql_host.";dbname=".$mysql_name.";port=".$mysql_port,$mysql_user,$mysql_pwd);
  31. }catch(Exception $e){
  32. if($e->getCode() == 2002){
  33. $errorMsg='连接数据库失败:数据库地址填写错误!';
  34. }elseif($e->getCode() == 1045){
  35. $errorMsg='连接数据库失败:数据库用户名或密码填写错误!';
  36. }elseif($e->getCode() == 1049){
  37. $errorMsg='连接数据库失败:数据库名不存在!';
  38. }else{
  39. $errorMsg='连接数据库失败:'.$e->getMessage();
  40. }
  41. return json(['code'=>0, 'msg'=>$errorMsg]);
  42. }
  43. $DB->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
  44. $DB->exec("set sql_mode = ''");
  45. $DB->exec("set names utf8");
  46. $sqls=file_get_contents(app()->getRootPath().'install.sql');
  47. $sqls=explode(';', $sqls);
  48. $sqls[]="REPLACE INTO `".$mysql_prefix."config` VALUES ('syskey', '".random(16)."')";
  49. $sqls[]="REPLACE INTO `".$mysql_prefix."config` VALUES ('admin_username', '".addslashes($admin_username)."')";
  50. $sqls[]="REPLACE INTO `".$mysql_prefix."config` VALUES ('admin_password', '".addslashes($admin_password)."')";
  51. $success=0;$error=0;$errorMsg=null;
  52. foreach ($sqls as $value) {
  53. $value=trim($value);
  54. if(empty($value))continue;
  55. $value = str_replace('cloud_',$mysql_prefix,$value);
  56. if($DB->exec($value)===false){
  57. $error++;
  58. $dberror=$DB->errorInfo();
  59. $errorMsg.=$dberror[2]."\n";
  60. }else{
  61. $success++;
  62. }
  63. }
  64. if(empty($errorMsg)){
  65. if(!file_put_contents(app()->getRootPath().'.env', $configdata)){
  66. return json(['code'=>0, 'msg'=>'保存失败,请确保网站根目录有写入权限']);
  67. }
  68. Cache::clear();
  69. return json(['code'=>1, 'msg'=>'安装完成!成功执行SQL语句'.$success.'条']);
  70. }else{
  71. return json(['code'=>0, 'msg'=>$errorMsg]);
  72. }
  73. }
  74. return view();
  75. }
  76. }