/** * @brief 根据操作类型创建并返回dbo对象, * @param string $type 操作类型 * @return PDO */ private function createDB($type = '') { if ($type == 'SELECT' or $type == 'SHOW') { if (self::$wdb != null) { return self::$wdb; } else { if (self::$rdb != null) { return self::$rdb; } else { try { $db_config = tool::getConfig('database'); $rdb_config = $db_config['slave']; $rdb_config[] = $db_config['master']; $num = rand(0, count($rdb_config) - 1); self::$rdb = new \PDO('mysql:dbname=' . $rdb_config[$num]['database'] . ';host=' . $rdb_config[$num]['host'] . ';charset=utf8', $rdb_config[$num]['user'], $rdb_config[$num]['password']); self::$rdb->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); } catch (PDOException $e) { exit($e->getMessage()); } return self::$rdb; } } } else { if (self::$wdb != null) { return self::$wdb; } else { try { $db_config = tool::getConfig(array('database', 'master')); self::$wdb = new \PDO('mysql:dbname=' . $db_config['database'] . ';host=' . $db_config['host'] . ';charset=utf8', $db_config['user'], $db_config['password']); self::$wdb->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); } catch (PDOException $e) { exit($e->getMessage()); } return self::$wdb; } } }
/** * 返回配置文件中主机名对应的基础路径 * @param string $name 主机名 * @return string */ public static function getConfigHost($name) { $host_list = tool::getGlobalConfig('host'); return isset($host_list[$name]) ? $host_list[$name] : ''; }
/** * 子账户添加处理 */ public function doSubAccAction() { if (IS_POST) { $data = array(); $data['user_id'] = safe::filterPost('id', 'int', 0); $data['pid'] = $this->user_id; $data['username'] = safe::filterPost('username'); $data['mobile'] = safe::filterPost('mobile', '/^\\d+$/'); $data['email'] = safe::filterPost('email', 'email'); $data['password'] = safe::filterPost('password', '/^\\S{6,20}$/'); $data['repassword'] = safe::filterPost('repassword', '/^\\S{6,20}$/'); $data['head_photo'] = tool::setImgApp(safe::filterPost('imgfile1')); $data['status'] = safe::filterPost('status', 'int'); $userModel = new UserModel(); if ($data['user_id'] == 0) { //新增子账户 $res = $userModel->subAccReg($data); } else { //更新子账户 if ($data['password'] == '') { //账户密码为空则不修改密码 unset($data['password']); unset($data['repassword']); } $res = $userModel->subAccUpdate($data); } if (isset($res['success']) && $res['success'] == 1) { $this->redirect('subAccList'); } else { echo $res['info']; } } return false; }