diff --git a/module/install/control.php b/module/install/control.php index 260b1b9856..7deb5a4620 100644 --- a/module/install/control.php +++ b/module/install/control.php @@ -23,9 +23,17 @@ */ class install extends control { + /* 构造函数,检查是否是通过安装入口调用。*/ + public function __construct() + { + if(!defined('IN_INSTALL')) die(); + parent::__construct(); + } + /* 安装程序首页。*/ public function index() { + if(!isset($this->config->installed) or !$this->config->installed) $this->session->set('installing', true); $this->view->header->title = $this->lang->install->welcome; $this->display(); } @@ -50,7 +58,6 @@ class install extends control public function step2() { $this->view->header->title = $this->lang->install->setConfig; - $this->view->webRoot = $this->install->getWebRoot(); $this->display(); } @@ -59,10 +66,49 @@ class install extends control { if(!empty($_POST)) { - $this->view = (object)$_POST; - $this->view->lang = $this->lang; - $this->view->config = $this->config; - $this->view->header->title = $this->lang->install->saveConfig; + $return = $this->install->checkConfig(); + if($return->result == 'ok') + { + $this->view = (object)$_POST; + $this->view->lang = $this->lang; + $this->view->config = $this->config; + $this->view->header->title = $this->lang->install->saveConfig; + $this->display(); + } + else + { + $this->view->header->title = $this->lang->install->saveConfig; + $this->view->error = $return->error; + $this->display(); + } + } + else + { + $this->locate($this->createLink('install')); + } + } + + /* 第四步,创建公司,生成管理员帐号。*/ + public function step4() + { + if(!empty($_POST)) + { + $this->install->grantPriv(); + if(dao::isError()) die(js::error(dao::getError())); + echo (js::alert($this->lang->install->success)); + unset($_SESSION['installing']); + die(js::locate('index.php', 'parent')); + } + + $this->view->header->title = $this->lang->install->getPriv; + if(!isset($this->config->installed) or !$this->config->installed) + { + $this->view->error = $this->lang->install->errorNotSaveConfig; + $this->display(); + } + else + { + $this->view->pmsDomain = $this->server->HTTP_HOST; $this->display(); } } diff --git a/module/install/lang/zh-cn.php b/module/install/lang/zh-cn.php index 3b68b7655c..b21d46305e 100644 --- a/module/install/lang/zh-cn.php +++ b/module/install/lang/zh-cn.php @@ -22,20 +22,20 @@ * @link http://www.zentao.cn */ $lang->install->common = '安装'; +$lang->install->next = '下一步'; +$lang->install->pre = '返回'; +$lang->install->reload = '刷新'; +$lang->install->error = '错误 '; + +$lang->install->start = '开始安装'; $lang->install->welcome = '欢迎使用禅道项目管理软件!'; $lang->install->desc = <<install->start = '开始安装'; -$lang->install->next = '下一步'; -$lang->install->reload = '刷新'; + $lang->install->checking = '系统检查'; -$lang->install->setConfig = '生成配置文件'; -$lang->install->saveConfig = '保存配置文件'; -$lang->install->settingDB = '设置数据库'; -$lang->install->save2File = '拷贝上面文本框中的内容,将其保存到"%s"中。'; $lang->install->ok = '检查通过(√)'; $lang->install->fail = '检查失败(×)'; $lang->install->loaded = '已加载'; @@ -44,12 +44,11 @@ $lang->install->exists = '目录存在 '; $lang->install->notExists = '目录不存在 '; $lang->install->writable = '目录可写 '; $lang->install->notWritable= '目录不可写 '; +$lang->install->phpINI = 'PHP配置文件'; $lang->install->checkItem = '检查项'; $lang->install->current = '当前配置'; $lang->install->result = '检查结果'; $lang->install->action = '如何修改'; -$lang->install->key = '配置项'; -$lang->install->value = '值'; $lang->install->phpVersion = 'PHP版本'; $lang->install->phpFail = 'PHP版本必须大于5.2.0'; @@ -63,6 +62,7 @@ $lang->install->dataRoot = '上传文件目录'; $lang->install->mkdir = '

需要创建目录%s。
linux下面命令为:
mkdir -p %s

'; $lang->install->chmod = '需要修改目录 "%s" 的权限。
linux下面命令为:
chmod o=rwx -R %s'; +$lang->install->settingDB = '设置数据库'; $lang->install->webRoot = 'PMS所在网站目录'; $lang->install->requestType = 'URL方式'; $lang->install->requestTypes['GET'] = '普通方式'; @@ -73,3 +73,27 @@ $lang->install->dbUser = '数据库用户名'; $lang->install->dbPassword = '数据库密码'; $lang->install->dbName = 'PMS使用的库'; $lang->install->dbPrefix = '建表使用的前缀'; +$lang->install->createDB = '自动创建数据库'; +$lang->install->clearDB = '清空现有数据'; + +$lang->install->errorConnectDB = '数据库连接失败 '; +$lang->install->errorCreateDB = '数据库创建失败'; +$lang->install->errorCreateTable = '创建表失败'; + +$lang->install->setConfig = '生成配置文件'; +$lang->install->key = '配置项'; +$lang->install->value = '值'; +$lang->install->saveConfig = '保存配置文件'; +$lang->install->save2File = '拷贝上面文本框中的内容,将其保存到 " %s "中。'; +$lang->install->errorNotSaveConfig = '还没有保存配置文件'; + +$lang->install->getPriv = '设置帐号'; +$lang->install->company = '公司名称'; +$lang->install->pms = 'PMS地址'; +$lang->install->pmsNote = '即通过什么地址可以访问到禅道项目管理,设置域名或者IP地址即可,不需要http'; +$lang->install->account = '管理员帐号'; +$lang->install->password = '管理员密码'; +$lang->install->errorEmptyPassword = '密码不能为空'; + +$lang->install->success = "安装成功!请删除install.php,登录禅道管理系统,设置用户及分组!"; + diff --git a/module/install/model.php b/module/install/model.php index c6d7074f54..e33a98551f 100644 --- a/module/install/model.php +++ b/module/install/model.php @@ -98,4 +98,142 @@ class installModel extends model { return rtrim(pathinfo($_SERVER['SCRIPT_NAME'], PATHINFO_DIRNAME), '/') . '/'; } + + /* 检查配置。*/ + public function checkConfig() + { + $return->result = 'ok'; + + /* 连接到数据库。*/ + $this->setDBParam(); + $this->dbh = $this->connectDB(); + if(!is_object($this->dbh)) + { + $return->result = 'fail'; + $return->error = $this->lang->install->errorConnectDB . $this->dbh; + return $return; + } + + /* 获得数据库版本。*/ + $version = $this->getMysqlVersion(); + + /* 数据库不存在,尝试建之。*/ + if(!$this->dbExists()) + { + if(!$this->createDB($version)) + { + $return->result = 'fail'; + $return->error = $this->lang->install->errorCreateDB; + return $return; + } + } + + /* 创建表。*/ + if(!$this->createTable($version)) + { + $return->result = 'fail'; + $return->error = $this->lang->install->errorCreateTable; + return $return; + } + return $return; + } + + /* 设置数据库参数。*/ + public function setDBParam() + { + $this->config->db->host = $this->post->dbHost; + $this->config->db->name = $this->post->dbName; + $this->config->db->user = $this->post->dbUser; + $this->config->db->password = $this->post->dbPassword; + $this->config->db->port = $this->post->dbPort; + $this->config->db->prefix = $this->post->dbPrefix; + + } + /* 连接到数据库。*/ + public function connectDB() + { + $dsn = "mysql:host={$this->config->db->host}; port={$this->config->db->port}"; + try + { + $dbh = new PDO($dsn, $this->config->db->user, $this->config->db->password); + $dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ); + $dbh->setAttribute(PDO::ATTR_ERRMODE, $this->config->db->errorMode); + $dbh->exec("SET NAMES {$this->config->db->encoding}"); + return $dbh; + } + catch (PDOException $exception) + { + return $exception->getMessage(); + } + } + + /* 判断数据库是否存在。*/ + public function dbExists() + { + $sql = "SHOW DATABASES like '{$this->config->db->name}'"; + return $this->dbh->query($sql)->fetch(); + } + + /* 获得mysql的版本号。*/ + public function getMysqlVersion() + { + $sql = "SELECT VERSION() AS version"; + $result = $this->dbh->query($sql)->fetch(); + return substr($result->version, 0, 3); + } + + /* 创建数据库。*/ + public function createDB($version) + { + $sql = "CREATE DATABASE `{$this->config->db->name}`"; + if($version > 4.1) $sql .= " DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci"; + return $this->dbh->query($sql); + } + + /* 创建表。*/ + public function createTable($version) + { + $dbFile = $this->app->getAppRoot() . 'db' . $this->app->getPathFix() . 'zentao.sql'; + $tables = explode(';', file_get_contents($dbFile)); + foreach($tables as $table) + { + $table = trim($table); + if(empty($table)) continue; + + if(strpos($table, 'CREATE') !== false and $version <= 4.1) + { + $table = str_replace('DEFAULT CHARSET=utf8', '', $table); + } + elseif(strpos($table, 'DROP') !== false and $this->post->clearDB != false) + { + $table = str_replace('--', '', $table); + } + $table = str_replace('`zt_', $this->config->db->name . '.`zt_', $table); + $table = str_replace('zt_', $this->config->db->prefix, $table); + if(!$this->dbh->query($table)) return false; + } + return true; + } + + /* 生成公司,设立管理员帐号。*/ + public function grantPriv() + { + if($this->post->password == '') die(js::error($this->lang->install->errorEmptyPassword)); + $admin->account = $this->post->account; + $admin->realname = $this->post->account; + $admin->password = md5($this->post->password); + $this->dao->replace(TABLE_USER)->data($admin)->autoCheck()->check('account', 'notempty')->exec(); + if(!dao::isError()) + { + $company->name = $this->post->company; + $company->pms = $this->post->pms; + $company->admins = ",$admin->account,"; + $this->dao->replace(TABLE_COMPANY)->data($company)->autoCheck()->batchCheck('name, pms', 'notempty')->exec(); + if(!dao::isError()) + { + $companyID = $this->dbh->lastInsertID(); + $this->dao->update(TABLE_USER)->set('company')->eq($companyID)->where('account')->eq($admin->account)->limit(1)->exec(); + } + } + } } diff --git a/module/install/view/footer.html.php b/module/install/view/footer.html.php index fe663d81c7..6292825e92 100644 --- a/module/install/view/footer.html.php +++ b/module/install/view/footer.html.php @@ -1,6 +1,6 @@
- +