/** * 进行命令执行的异常拦截操作 * @static * @author 欧远宁 * @param string $cmd 命令名 * @param string $code 错误码 * @param string $msg 错误内容 */ public static function cmd_error($cmd, $code, $msg) { $cfg = fun::get_cfg('cfg'); if (key_exists('cmd_inject', $cfg)) { call_user_func(array($cfg['cmd_inject'], 'error'), $cmd, $code, $msg); } }
/** * 得到某个库某个表的分库分表信息 * @static * @author 欧远宁 * @param string $mdl 模块名 * @param string $tbl 表名 * @return array('db'=>'库后缀','tbl'=>'表后缀') */ public static function split($mdl, $tbl) { $cfg = fun::get_cfg('cfg'); if (!key_exists('dbsplit', $cfg)) { return array('', ''); } $db_suf = call_user_func($cfg['dbsplit'], $mdl); $tmp = $mdl . $tbl; $tbl_suf = call_user_func($cfg['dbsplit'], $mdl, $tbl); return array('db' => $db_suf, 'tbl' => $tbl_suf); }
/** * 进行单表的,基于等号筛选的查询 * @author 欧远宁 * @param array $filter 筛选条件如:array('uid'=>'outrace')。如果是字符串,则根据ID查找 * @param array $page 分页参数 array('cur'=>当前页数,'size'=>每页数据量, start=开始笔数,不予cur同时使用,<br/> * 'all'=>是否全部取回来y/n,'ttl'=是否返回总数) * @param string $order 排序信息如:'time desc,name asc',默认不排序 * @param array $get_one 所需要获取的一对一表如:array('base.user.user_id','forum.forum_stat') * @example * $dao = new dao('blog','article');<br/> * $filter = array('uid'=>'ouyuanning');<br/> * $page = array('cur'=>1,'size'=>40,'ttl'=>'y'); //取第一页。每页40笔,返回总量<br/> * $order = 'addtime Desc'; //根据时间降序排列<br/><br/> * * //获取1对1表内容,根据原则是article_id = articleStat_id<br/> * //多对1则根据最后一个的字段名的值 = 多对一表的ID值<br/> * $get_one = array('blog.article_stat','user.upoint.user_id');<br/><br/> * * //得到结果为:$res['article_list'] $res['article_stat_list'] $res['upoint_list']<br/> * $res = $dao->get($filter, $page, $order, $get_one); * @return array */ public function get($filter = '', $page = null, $order = null, $get_one = null) { $result[$this->key_list] = array(); if ($filter == '') { $filter = array(); } if (is_array($get_one)) { $one_arr = array(); foreach ($get_one as $obj) { $arr = explode('.', $obj); $one_arr[] = $arr; if (count($arr) == 4) { $result[$arr[3] . '_list'] = array(); } else { $result[$arr[1] . '_list'] = array(); } } } if (is_array($filter)) { //并非根据ID获取 $flen = count($filter); if ($flen == 1 && key_exists($this->key, $filter)) { return $this->get($filter[$this->key], null, null, $get_one); } //有值的普通数组 if ($flen > 0 && !fun::is_assoc($filter)) { foreach ($filter as $fkey) { $tmp = $this->get($fkey, null, null, $get_one); foreach ($result as $k => $v) { $result[$k] = array_merge($v, $tmp[$k]); } } return $result; } $klist = $this->get_rec($filter, $page, $order); if (!is_null($klist['ttl'])) { $result['ttl'] = $klist['ttl']; } foreach ($klist['list'] as $obj) { $tmp = $this->get($obj[$this->key], null, null, $get_one); //一次返回多笔记录的时候,不支持$getMany foreach ($tmp as $k => $v) { if (count($v) > 0) { $result[$k][] = $v[0]; } else { $result[$k][] = array(); } } } } else { //根据ID获取 $id = $filter; if ($id == '') { return array($this->key_list => array()); } $filter_para = array(); $filter_para[':' . $this->key] = $filter; $where = ' WHERE ' . $this->key . ' = :' . $this->key; if ($this->schema['cache'] > -1) { //说明有使用缓存 $cid = $this->ver . $this->mdl . $this->tbl . $id; $re = $this->cache->get($cid); if ($re == '') { //未命中缓存 $sql = 'SELECT ' . $this->schema['fields'] . ' FROM `' . $this->tbl . '`' . $where; $tmp = $this->db->query($sql, $filter_para); if (count($tmp) > 0) { $result[$this->key_list] = $tmp; $this->cache->set($cid, $tmp, $this->schema['cache']); } } else { $result[$this->key_list] = $re; } } else { //未使用缓存 $sql = 'SELECT ' . $this->schema['fields'] . ' FROM `' . $this->tbl . '`' . $where; $result[$this->key_list] = $this->db->query($sql, $filter_para); } $cLen = count($result[$this->key_list]); //得到1对1表数据 if ($cLen > 0 && is_array($get_one)) { foreach ($one_arr as $one) { $c = new dao($one[0], $one[1]); $len = count($one); if ($len < 3) { $tmp = $c->get($id); } else { $tmp = $c->get($result[$this->key_list][0][$one[2]]); } if ($len == 4) { $tmp[$one[3] . '_list'] = $tmp[$one[1] . '_list']; unset($tmp[$one[1] . '_list']); } $result = array_merge($result, $tmp); } } } return $result; }
/** * 检验对应的VO * @author 欧远宁 * @param string $mdl 模块名 * @param string $tbl 表名 * @param array $val 需要检验的数据 * @param bool $throw 是否直接抛出异常,默认为直接抛出异常,FALSE则在第一次出错的时候,返回FALSE。 * @throws err 抛出系统验证异常,我们一般认为只有黑客才会传入无法通过验证的异常 */ protected final function _valvo($mdl, $tbl, $val, $throw = TRUE) { $check = $GLOBALS['cfg'][$mdl]['schema'][$tbl]['check']; foreach ($val as $k => $v) { if (key_exists($k, $check) && $check[$k] != '') { if (!fun::val_str($v, $check[$k])) { if ($throw) { throw new err('The value of field [' . $k . '] , [' . $v . '] can not be verified'); } else { return FALSE; } } } } return TRUE; }
//设置mbstring的编码 date_default_timezone_set($now_cfg['timezone']); //设定时区 setlocale(LC_ALL, $now_cfg['locale']); //设置本地化区域及字符集 function _get_ver($arr) { $ip = $_SERVER['SERVER_ADDR']; foreach ($arr as $k => $v) { foreach ($v as $_ip) { if (strpos($_ip, $ip) !== FALSE) { return $k; } } } return 'dev'; } define('VER', _get_ver($now_cfg['ver_ip'])); //定义版本 if (VER == 'pro') { //正式版 error_reporting(0); //不返回错误 } else { //其他版 error_reporting(E_ALL); //返回所有错误 } fun::init_cfg(); use now\control; control::execute();
/** * 下载一份现有文件 * @static * @author 欧远宁 * @param string $name 下载时候显示的名字 * @param string $head 标题栏位 * @param array $data 返回的数据 */ public static function down($name, $path, $type = 'application/octet-stream') { header('Content-type: ' . $type); //处理中文文件名 $ua = $_SERVER["HTTP_USER_AGENT"]; if (preg_match('/MSIE/', $ua)) { $name = urlencode($name); $name = str_replace("+", "%20", $name); header('Content-Disposition: attachment; filename="' . $name . '"'); } else { if (preg_match("/Firefox/", $ua)) { header("Content-Disposition: attachment; filename*=\"utf8''" . $name . '"'); } else { header('Content-Disposition: attachment; filename="' . $name . '"'); } } //让Xsendfile发送文件 $cfg = fun::get_cfg('cfg', 'upload'); if ($cfg && key_exists('send_file', $cfg) && $cfg['send_file'] != '') { header($cfg['send_file'] . ': ' . $path); } else { header('Content-Length: ' . filesize($path)); readfile($path); } }