InstallController.class.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | OneThink [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: 小梦科技资源 <zuojiazi@vip.qq.com> <https://www.nanodreamtech.com>
  8. // +----------------------------------------------------------------------
  9. namespace Install\Controller;
  10. use Think\Controller;
  11. use Think\Db;
  12. use Think\Storage;
  13. class InstallController extends Controller{
  14. protected function _initialize(){
  15. if(Storage::has( 'Conf/install.lock')){
  16. $this->error('已经成功安装,请不要重复安装!');
  17. }
  18. }
  19. //安装第一步,检测运行所需的环境设置
  20. public function step1(){
  21. session('error', false);
  22. //环境检测
  23. $env = check_env();
  24. //目录文件读写检测
  25. if(IS_WRITE){
  26. $dirfile = check_dirfile();
  27. $this->assign('dirfile', $dirfile);
  28. }
  29. //函数检测
  30. $func = check_func();
  31. session('step', 1);
  32. $this->assign('env', $env);
  33. $this->assign('func', $func);
  34. $this->display();
  35. }
  36. //安装第二步,创建数据库
  37. public function step2($db = null, $admin = null){
  38. if(IS_POST){
  39. //检测管理员信息
  40. if(!is_array($admin) || empty($admin[0]) || empty($admin[1]) || empty($admin[3])){
  41. $this->error('请填写完整管理员信息');
  42. } else if($admin[1] != $admin[2]){
  43. $this->error('确认密码和密码不一致');
  44. } else {
  45. $info = array();
  46. list($info['username'], $info['password'], $info['repassword'], $info['email'])
  47. = $admin;
  48. //缓存管理员信息
  49. session('admin_info', $info);
  50. }
  51. //检测数据库配置
  52. if(!is_array($db) || empty($db[0]) || empty($db[1]) || empty($db[2]) || empty($db[3])){
  53. $this->error('请填写完整的数据库配置');
  54. } else {
  55. $DB = array();
  56. list($DB['DB_TYPE'], $DB['DB_HOST'], $DB['DB_NAME'], $DB['DB_USER'], $DB['DB_PWD'],
  57. $DB['DB_PORT'], $DB['DB_PREFIX']) = $db;
  58. //缓存数据库配置
  59. cookie('db_config',$DB);
  60. //创建数据库
  61. $dbname = $DB['DB_NAME'];
  62. unset($DB['DB_NAME']);
  63. $db = Db::getInstance($DB);
  64. $sql = "CREATE DATABASE IF NOT EXISTS `{$dbname}` DEFAULT CHARACTER SET utf8";
  65. try{
  66. $db->execute($sql);
  67. }catch (\Think\Exception $e){
  68. if(strpos($e->getMessage(),'getaddrinfo failed')!==false){
  69. $this->error( '数据库服务器(数据库服务器IP) 填写错误。','很遗憾,创建数据库失败,失败原因');// 提示信息
  70. }
  71. if(strpos($e->getMessage(),'Access denied for user')!==false){
  72. $this->error('数据库用户名或密码 填写错误。','很遗憾,创建数据库失败,失败原因');// 提示信息
  73. }else{
  74. $this->error( $e->getMessage());// 提示信息
  75. }
  76. }
  77. session('step',2);
  78. // $this->error($db->getError());exit;
  79. }
  80. //跳转到数据库安装页面
  81. $this->redirect('step3');
  82. } else {
  83. session('error') && $this->error('环境检测没有通过,请调整环境后重试!');
  84. $step = session('step');
  85. if($step != 1 && $step != 2){
  86. // $this->redirect('step1');
  87. }
  88. session('step', 2);
  89. $this->display();
  90. }
  91. }
  92. //安装第三步,安装数据表,创建配置文件
  93. public function step3(){
  94. /* if(session('step') != 2){
  95. $this->redirect('step2');
  96. }*/
  97. $this->display();
  98. //连接数据库
  99. $dbconfig = cookie('db_config');
  100. $db = Db::getInstance($dbconfig);
  101. //创建数据表
  102. //dump($dbconfig);die;
  103. create_tables($db, $dbconfig['DB_PREFIX']);
  104. //注册创始人帐号
  105. $auth = build_auth_key();
  106. $admin = session('admin_info');
  107. //dump($admin);die;
  108. register_administrator($db, $dbconfig['DB_PREFIX'], $admin, $auth);
  109. //创建配置文件
  110. $conf = write_config($dbconfig, $auth);
  111. session('config_file',$conf);
  112. if(session('error')){
  113. //show_msg();
  114. } else {
  115. session('step', 3);
  116. echo "<script type=\"text/javascript\">setTimeout(function(){location.href='".U('Index/complete')."'},5000)</script>";
  117. ob_flush();
  118. flush();
  119. //$this->redirect('Index/complete');
  120. }
  121. }
  122. public function error($info,$title='很遗憾,安装失败,失败原因'){
  123. $this->assign('info',$info);// 提示信息
  124. $this->assign('title',$title);
  125. $this->display('error');exit;
  126. }
  127. }