/** * 应用程序初始化 * */ private function _initApp() { // 得到APP配置 $appCfg = QP_Sys::getAppCfg(); // 设置时区 date_default_timezone_set($appCfg['timezone']); }
/** * 构造函数 * * @param string $viewBasePath 视图文件的根目录,为空时默认为:"Application/Views/Script/" */ public function __construct($viewBasePath = null) { // 初始化模板根目录 parent::__construct($viewBasePath); // APP配置 $appCfg = QP_Sys::getAppCfg(); // 是否安装了 SMARTY 库文件 $smartyFile = QUICKPHP_PATH . '/Smarty/Smarty/Smarty.class.php'; if (!file_exists($smartyFile)) { throw new QP_Exception('<hr/> 请到 <a href="http://http://www.smarty.net/download" target="_blank">Smarty 官网</a> 或到 <a href="http://www.vquickphp.com" target="_blank">QuickPHP 官网</a> 下载 Smarty 放到 QuickPHP/Smarty/Smarty 目录下 <br/> 注意:<font color="red">只支持 Smarty 2 Releases 的版本</font>(足够用啦,Smarty 3.x 没研究过它^_^) <hr/>'); } // 创建 SMARTY 对象 require_once $smartyFile; $this->_smarty = new Smarty(); // 左/右 标记定义 $this->_smarty->left_delimiter = self::LEFT_DELIMITER; $this->_smarty->right_delimiter = self::RIGHT_DELIMITER; // 编译文件目录 $this->_smarty->compile_dir = APPLICATION_PATH . '/Data/Temp'; // 是否强行编译 $this->_smarty->force_compile = $appCfg['debug']; // 赋于视图中全局的对象 $this->_smarty->assign('request', QP_Request::getInstance()); }
/** * 得到数据库操作对象 * * @param string $driverType 驱动类型,可选项有: 'mysql' | 'mysqli' | 'pdo' * @param string $configItem 数据库配置项,具体请看 Application/Configs/Database.php * @return object */ public static function factory($driverType = 'mysql', $configItem = 'default') { // 对象唯一KEY $key = $driverType . $configItem; // 判断驱动是否已经生成对应的对象了 $driverType = ucfirst(strtolower($driverType)); if (!isset(self::$_instance[$key])) { // 判断驱动文件是否存在 $driverFile = QUICKPHP_PATH . '/Db/' . $driverType . '.php'; if (!file_exists($driverFile)) { throw new QP_Exception('数据驱动文件不存在:' . $driverFile); } // 判断配置项是否定义 $dfCfgFile = APPLICATION_PATH . '/Configs/Database.php'; $dbConfig = (include $dfCfgFile); if (!isset($dbConfig[$configItem])) { throw new QP_Exception("数据库配置项:{$configItem} 在配置文件中找不到:{$dfCfgFile}"); } // 得到APP配置 $appCfg = QP_Sys::getAppCfg(); // 生成驱动对象 require $driverFile; $class = 'QP_Db_' . $driverType; self::$_instance[$key] = new $class($dbConfig[$configItem], $appCfg['debug'], $appCfg['display_error']); } return self::$_instance[$key]; }
/** * 检测用户是否对URI有访问权限的 * * @param unknown_type $userid * @return boolean */ public static function check($userid, $controller, $action) { // 非普通用户不查检权限 if (QP_Session_Session::get('login_priv') != 1) { return true; } // 得到配置 $privcfg = QP_Sys::config('privconfig'); // 如果不使用权限则永远返回 true if (!$privcfg['enable']) { return true; } // 判断是否在全局访问的资源中 $allRes = strtolower($controller . '_*'); $currentRes = strtolower($controller . '_' . $action); if (in_array($currentRes, $privcfg['allow']) || in_array($allRes, $privcfg['allow'])) { return true; } // 得到用户所在的组的所有权限 $userModel = new Model_User(); $userInfo = $userModel->userinfo($userid); $privModel = new Model_Priv(); $resourceArr = $privModel->getResource($userInfo['groupid']); // 判断是否在权限组中 return in_array($currentRes, $resourceArr) || in_array($allRes, $resourceArr); }
/** * 构造函数 * */ public function __construct() { // APP配置 $appCfg = QP_Sys::getAppCfg(); $this->_urlMethod = $appCfg['url_method']; $this->_request = QP_Request::getInstance(); }
/** * 语言切换 */ public function langAction() { $lang = $this->request->getGet('lang'); // 记录 session QP_Session_Session::set('lang', $lang); // 跳转到上一个页面 $url = $this->request->getGet('bgurl'); $url == '' && ($url = QP_Sys::url('setup')); $this->location($url); }
/** * 构造函数,但该类不能被实例化 */ private function __construct() { // 处理请求数据 $_POST = $this->_magicQuotes($_POST); $_GET = $this->_magicQuotes($_GET); $_REQUEST = $this->_magicQuotes($_REQUEST); // 如果 URL模式 不是 standard 则要解析 URI $appCfg = QP_Sys::getAppCfg(); if ($appCfg['url_method'] != 'standard') { $this->_parseUri(); } }
/** * 为用户组添加默认权限(所有的权限) * * @param unknown_type $groupid */ public function addDefaultPriv($groupid) { // 得到权限的配置 $privcfg = QP_Sys::config('privconfig'); $priv = array(); foreach ($privcfg['priv'] as $gid => $res) { foreach ($res['resgroup'] as $resid => $row) { $priv[] = $gid . '|' . $resid; } } return $this->submit($groupid, $priv); }
/** * 检测用户的权限 * */ private function _checkpriv() { $controller = isset($_GET['c']) ? $_GET['c'] : QP_Controller::DEFAULT_CONTROLLER; $action = isset($_GET['a']) ? $_GET['a'] : QP_Controller::DEFAULT_ACTION; // 如果有权限则返回 $ret = Priv::check(QP_Session_Session::get('login_userid'), $controller, $action); if ($ret) { return; } // 如果是异步访问则直接输出错误 if (QP_Request::getInstance()->isAJAX()) { die('Priv Access denied'); } else { // 其它方式则直接提示后跳转 QP_Sys::msgbox('Priv Access denied!', url('index', 'index'), 10); } }
/** * 首页 */ public function indexAction() { $get = $this->request->getGet(); Priv::check($this->userid, $get['controller'], $get['action']); // 所有的用户组 $groupList = $this->userModel->userGroupList(array('userid' => $this->userid)); // 得到组ID $groupid = $this->request->getGet('groupid', 0); if ($groupid < 1) { $groupid = isset($groupList[0]['groupid']) ? $groupList[0]['groupid'] : 0; } // 得到群对应的所有权限 $priv = $this->privModel->get($groupid); $allPriv = QP_Sys::config('privconfig.priv'); $this->view->groups = $groupList; $this->view->privList = $allPriv; $this->view->priv = $priv; }
/** * 发送邮件 * * @param string $address 收件人,多个用 ";" 分开 * @param string $subject 主题 * @param string $body 内容 * @param string $attachment 附件文件,多个用 ";" 分开,要用绝对路径,如 "/tmp/a.zip;/tmp/b.zip" * @return unknown */ public function send($address, $subject, $body, $attachment = '') { $mail = new PHPMailer(); $mail->CharSet = 'UTF-8'; $mail->SetLanguage('zh_cn'); $mail->IsSMTP(); $mail->SMTPDebug = 1; $mail->SMTPAuth = $this->_config['auth']; $mail->Host = $this->_config['host']; $mail->Port = $this->_config['port']; $mail->Username = $this->_config['username']; $mail->Password = $this->_config['password']; $mail->SetFrom($this->_config['from'], $this->_config['fromname']); $mail->Subject = $subject; $mail->MsgHTML($body); $mail->SMTPSecure = $this->_config['secure']; // 收件人 $addrArr = explode(';', $address); foreach ($addrArr as $addr) { $mail->AddAddress($addr); } // 附件 if ($attachment != '') { $attaArr = explode(';', $attachment); foreach ($attaArr as $file) { $mail->AddAttachment($file); } } if (!$this->debug) { // 发送 if (!$mail->Send()) { $ret = $mail->ErrorInfo; } else { $ret = true; } } else { // 写日志 $message = $subject . PHP_EOL . $body; QP_Sys::log($message, 'email'); $ret = true; } return $ret; }
/** * 发送通知 * * @param unknown_type $receiver 接收者ID,多个ID用","连接,如:"yuanwei1,guoyu" * @param unknown_type $msg 消息内容 * @param unknown_type $url 点击消息所打开的URL * @param unknown_type $title 标题 * @param unknown_type $delaytime 消息提醒框的停留时间(毫秒),0表示不自动消失。 */ public static function send($receiver, $msg, $url, $title = 'QuickBug Notify', $delaytime = 0) { $rtxCfg = QP_Sys::config('sysconfig.rtx'); // RTX网关只支持 GBK 的编码 $msg = sprintf("[%s|%s]", $msg, $url); $msg = mb_convert_encoding($msg, 'gbk', 'utf-8'); $title = mb_convert_encoding($title, 'gbk', 'utf-8'); // 组合参数 $params = implode('&', array('title=' . urlencode($title), 'receiver=' . $receiver, 'msg=' . urlencode($msg), 'delaytime=' . $delaytime)); $url = sprintf("http://%s:%d/sendnotify.cgi?%s", $rtxCfg['host'], $rtxCfg['port'], $params); if (!self::$debug) { // 发送请求 if (function_exists('curl_init')) { QP_Sys::load('curl')->set(array('port' => $rtxCfg['port'], 'timeOut' => 5))->get($url); } else { @file_get_contents($url); } } else { // 写日志 QP_Sys::log($url, 'rtx'); } }
/** * 自动运行 */ public function init() { // 得到用户信息 $this->userid = intval(QP_Session_Session::get('login_userid')); $this->username = QP_Session_Session::get('login_username'); $this->priv = QP_Session_Session::get('login_priv'); // 自动得到当前的控制器和方法,适应各种URL模式 $get = $this->request->getGet(); $parsm = $this->request->getParam(); $this->controller = strtolower($get['controller'] ? $get['controller'] : $parsm['controller']); $this->action = strtolower($get['action'] ? $get['action'] : $parsm['action']); // 判断是否登录了,除了可以直接访问的行为 $allowRes = QP_Sys::config('privconfig.allow'); $res = strtolower($this->controller . '_' . $this->action); $resAll = strtolower($this->controller . '_*'); if (!in_array($res, $allowRes) && !in_array($resAll, $allowRes)) { // 没有登录后台则跳转到登录去 if (!$this->userid) { $url = $this->request->currentUrl(); $this->gotoUri('index', 'login', array('bgurl' => $url)); } } }
/** * 视图助手,助手都定义在 Application/Views/Helpers * * @param string $name 助手名,默认情况下为当前控制器所对应的 * @return object */ public function helper($name = '') { // 默认为当前控制器所对应的助手 if ($name == '') { // 根据不同的URL模式得到当前的控制器 $request = QP_Request::getInstance(); $appConfig = QP_Sys::getAppCfg(); $param = $appConfig['url_method'] == 'standard' ? $request->getGet() : $request->getParam(); $name = $param['controller']; } // 得到助手文件名 $name = ucfirst(strtolower($name)); // 如果是新的助手对象已生成则要生成它 if (!isset(self::$_helper[$name]) || !is_object(self::$_helper[$name])) { // 助手文件不存在 $helperFile = APPLICATION_PATH . '/Views/Helpers/' . $name . '.php'; if (!file_exists($helperFile)) { throw new QP_Exception("助手文件不存在:{$helperFile}", QP_Exception::EXCEPTION_NO_HELPER); } // 包含助手生成对象 require_once $helperFile; $className = 'Helper_' . $name; // 类是否存在的 if (!class_exists($className, false)) { throw new QP_Exception("类:{$className} 未定义在:{$helperFile}"); } // 判断助手是否继承基类 self::$_helper[$name] = new $className(); if (!self::$_helper[$name] instanceof QP_View_Helper) { throw new QP_Exception("助手类 {$className} 必需继承 QP_View_Helper 基类"); } $obj = self::$_helper[$name]; self::$_helper[$name]->init(); } return self::$_helper[$name]; }
/** * 数据库连接或查询错误处理 * * @param string $errorMsg */ protected function _DBError($errorMsg) { // 如果显示错误则直接抛出异常 if ($this->_showError) { throw new QP_Exception($errorMsg, QP_Exception::EXCEPTION_DB_ERROR); } else { // 否则就写日志 QP_Sys::log($errorMsg, 'db'); } }
/** * 得到当前用户所选择的语言 * * @return unknown */ function getLang() { $lang = null; // 如果当前有用户登录了 if (QP_Session_Session::get('login_userid') > 0) { $userModel = new Model_User(); $lang = $userModel->getSet('lang'); } // 如果用户没有设置则检查 session if ($lang == null) { $lang = QP_Session_Session::get('lang'); } // 如果还是没有设置则取系统配置的默认值了 if ($lang == null) { $lang = QP_Sys::config('Sysconfig.lang'); } return ucfirst($lang); }
/** * 构造函数 * */ public function __construct() { $this->_request = QP_Request::getInstance(); $this->_appConfig = QP_Sys::getAppCfg(); }
/** * 显示提示消息页 * * 注意:子类可以重载这个方法以适合项目的使用 * * @param string $msg :消息文本 * @param string $url :将要跳转的URL "":自动返回到上一页 "close":则关闭窗口 * @param $time $time :页面显示停留的时间,单位:秒,过了时间后自动跳转 */ public function msgbox($msg, $url = '', $time = 10) { QP_Sys::msgbox($msg, $url, $time); }
所对应的文件: <span style="color:green;"> <?php // 根据类得到对应的文件名 $classFile = $spr = ''; $dirArr = array_map('ucfirst', explode('_', $class)); foreach ($dirArr as $k => $path) { $classFile .= $spr . $path; $spr = '/'; } echo $classFile . '.php'; ?> </span> <br/><br/> [提示]:请检查以下目录中是否有该文件的定义: <hr/> <?php QP_Sys::dump(explode(PATH_SEPARATOR, get_include_path())); ?> <hr/> <?php } ?> <!-- 类找不到的提示 --> <?php if (isset($classError)) { ?> 在文件:<span style="color:green;"><?php echo $includeFile; ?> </span> 中找不到类:<span style="color:red;"><b><?php echo $class;
/** * 更新BUG * * @param unknown_type $sets * @param unknown_type $id */ public function updateBug($sets, $bugid) { $info = $this->bugInfo($bugid); // 如果修改了标题或内容则要保存所修改的历史记录 if (isset($sets['subject']) || isset($sets['info'])) { if ($info) { $historySet = array('bugid' => $bugid, 'historydata' => serialize($info), 'dateline' => time()); $this->db->insert($this->bugHistoryTable, $historySet); } // 修改记录 $this->addOperate($bugid, $this->userid, L('bug.modifyed_bug_content')); } // 如果改变了状态则要记录操作记录 if (isset($sets['status']) && $sets['status'] != $info['status']) { $bugStatus = QP_Sys::config('bugconfig.status'); $this->addOperate($bugid, $this->userid, $bugStatus[$sets['status']] . L('bug.this_bug')); // 如果BUG的状态修改为了 "已接受" 则要把接受者改为当前的用户 if ($sets['status'] == 2) { $sets['touserid'] = $this->userid; } } // 记录最后更新的时间 if (!isset($sets['lastuptime'])) { $sets['lastuptime'] = time(); } return $this->db->update($this->bugTable, $sets, array('bugid' => $bugid)); }
/** * BUG列表字段设置 */ public function buglistsetAction() { $userModel = new Model_User(); //dump($userModel->getSet('bugListFields')); // 提交数据 if ($this->request->isPost()) { $userModel->saveSet('bugListFields', isset($_POST['fields']) ? $_POST['fields'] : array()); $this->outputJson(0); } $fields = QP_Sys::config('bugconfig.listfields'); $this->view->fields = $fields; $this->view->noSet = !fieldIsSet(); }
/** * 群发RTX/邮件通知 * */ public function notifyAction() { // 发通知提交 if ($this->request->isPost()) { switch ($_POST['toUser']) { // 所有人 case 0: $userList = $this->userModel->userinfoList(array('createuid' => $this->userid)); // 得到 UID $userids = $spr = ''; foreach ($userList as $row) { $userids .= $spr . $row['userid']; $spr = ','; } break; // 用户组 // 用户组 case 1: $userList = $this->userModel->userinfoList(array('groupid' => $_POST['userGroup'])); // 得到 UID $userids = $spr = ''; foreach ($userList as $row) { $userids .= $spr . $row['userid']; $spr = ','; } break; // 指定用户 // 指定用户 case 2: $userids = $_POST['sendUsers']; break; } // 发通知 if (isset($_POST['notifyEmail']) && $_POST['notifyEmail']) { User::notify($userids, $_POST['notMsg'], $_POST['notUrl'], 'mail', $_POST['notTitle']); } if (isset($_POST['notifyRtx']) && $_POST['notifyRtx']) { User::notify($userids, $_POST['notMsg'], $_POST['notUrl'], 'rtx', $_POST['notTitle']); } $this->outputJson(0); } // 得到所有的用户组 $this->view->groupList = $this->userModel->userGroupList(array('userid' => $this->userid)); // 域名配置 $conf = QP_Sys::config('sysconfig'); $this->view->domain = $conf['domain'] . $conf['path']; }
/** * 将内容保存后下载 * * @param string $content 内容 * @param string $filename 文件基本名 */ private function _downloadContent($content, $filename) { // 保存为临时文件 $file = 'files/export/' . $filename; file_put_contents(SITEWEB_PATH . '/' . $file, $content); // 下载 $sysCfg = QP_Sys::config('sysconfig'); $url = $sysCfg['domain'] . $sysCfg['path'] . $file; header("Location: {$url}"); exit; }
/** * 语言切换 */ public function langAction() { $lang = $this->request->getGet('lang'); // 记录 session QP_Session_Session::set('lang', $lang); // 如果登录了则记录用户的语言选择 if ($this->userid > 0) { $userModel = new Model_User(); $userModel->saveSet('lang', $lang); } $url = $this->request->getGet('bgurl'); $url == '' && ($url = QP_Sys::url('index')); $this->location($url); }
/** * 检测当前是否以 PHP CLI 方式运行,框架初始化时自动调用 */ public static function _checkSapi() { // 不是则直接闪人 if (PHP_SAPI != 'cli') { return; } // 根据框架的配置将 $_SERVER['argv'] 变量的值转成 REQUEST 的 GET 或 PARAM 值 $request = QP_Request::getInstance(); // APP的配置 $appCfg = QP_Sys::getAppCfg(); $argv = $request->server('argv'); // 主程序文件名 $prgName = $argv[0]; unset($argv[0]); // URL模式为 standard 时的 GET KEY名映射 $getKeyMap = array('controller' => QP_Request::C, 'action' => QP_Request::A); // 设置所有参数 foreach ($argv as $arg) { $arr = explode('=', $arg); if (count($arr) != 2) { echo <<<EOF \t\t\t\tsyntax: \t\t\t\tphp {$prgName} [controller=<controller> action=<action> param1=value1 param2=value2 ...] \t\t\t\texamples: \t\t\t\tphp {$prgName} controller=index action=test id=10 name=vg EOF; exit; } // 先设置控制器和动作 list($key, $val) = $arr; if ($appCfg['url_method'] == 'standard') { if (array_key_exists($key, $getKeyMap)) { $key = $getKeyMap[$key]; } $request->setGet($key, $val); } else { $request->setParam($key, $val); } } }