Ejemplo n.º 1
0
 private static function checkSession()
 {
     $system = systemInfo();
     $common_config = $system['session_handle']['common'];
     // set some important session vars
     ini_set('session.auto_start', 0);
     ini_set('session.gc_probability', 1);
     ini_set('session.gc_divisor', 100);
     ini_set('session.gc_maxlifetime', $common_config['lifetime']);
     ini_set('session.referer_check', '');
     ini_set('session.entropy_file', '/dev/urandom');
     ini_set('session.entropy_length', 16);
     ini_set('session.use_cookies', 1);
     ini_set('session.use_only_cookies', 1);
     ini_set('session.use_trans_sid', 0);
     ini_set('session.hash_function', 1);
     ini_set('session.hash_bits_per_character', 5);
     // disable client/proxy caching
     session_cache_limiter('nocache');
     // set the cookie parameters
     session_set_cookie_params($common_config['lifetime'], $common_config['cookie_path'], preg_match('/^[^\\.]+$/', MpInput::server('HTTP_HOST')) ? null : $common_config['cookie_domain']);
     // name the session
     session_name($common_config['session_name']);
     register_shutdown_function('session_write_close');
     //session自定义配置检测
     if (!empty($system['session_handle']['handle']) && isset($system['session_handle'][$system['session_handle']['handle']])) {
         $driver = $system['session_handle']['handle'];
         $config = $system['session_handle'];
         $handle = ucfirst($driver) . 'SessionHandle';
         if (class_exists($handle, FALSE)) {
             $session = new $handle();
             $session->start($config);
         }
     }
     // start it up
     if ($common_config['autostart']) {
         sessionStart();
     }
 }
Ejemplo n.º 2
0
 private static function checkRule($_rule, $val, $data, $db = null)
 {
     if (!$db) {
         $db = MpLoader::instance()->database();
     }
     $matches = self::getCheckRuleInfo($_rule);
     $_rule = $matches[1];
     $args = $matches[2];
     switch ($_rule) {
         case 'required':
             return !empty($val);
         case 'match':
             return isset($args[0]) && isset($data[$args[0]]) ? $val && $val == $data[$args[0]] : false;
         case 'equal':
             return isset($args[0]) ? $val && $val == $args[0] : false;
         case 'enum':
             return in_array($val, $args);
         case 'unique':
             #比如unique[user.name] , unique[user.name,id:1]
             if (!$val || !count($args)) {
                 return false;
             }
             $_info = explode('.', $args[0]);
             if (count($_info) != 2) {
                 return false;
             }
             $table = $_info[0];
             $col = $_info[1];
             if (isset($args[1])) {
                 $_id_info = explode(':', $args[1]);
                 if (count($_id_info) != 2) {
                     return false;
                 }
                 $id_col = $_id_info[0];
                 $id = $_id_info[1];
                 $id = stripos($id, '#') === 0 ? MpInput::get_post(substr($id, 1)) : $id;
                 $where = array($col => $val, "{$id_col} <>" => $id);
             } else {
                 $where = array($col => $val);
             }
             return !$db->where($where)->from($table)->count_all_results();
         case 'exists':
             #比如exists[user.name] , exists[user.name,type:1], exists[user.name,type:1,sex:#sex]
             if (!$val || !count($args)) {
                 return false;
             }
             $_info = explode('.', $args[0]);
             if (count($_info) != 2) {
                 return false;
             }
             $table = $_info[0];
             $col = $_info[1];
             $where = array($col => $val);
             if (count($args) > 1) {
                 foreach (array_slice($args, 1) as $v) {
                     $_id_info = explode(':', $v);
                     if (count($_id_info) != 2) {
                         continue;
                     }
                     $id_col = $_id_info[0];
                     $id = $_id_info[1];
                     $id = stripos($id, '#') === 0 ? MpInput::get_post(substr($id, 1)) : $id;
                     $where[$id_col] = $id;
                 }
             }
             return $db->where($where)->from($table)->count_all_results();
         case 'min_len':
             return isset($args[0]) ? mb_strlen($val, 'UTF-8') >= intval($args[0]) : false;
         case 'max_len':
             return isset($args[0]) ? mb_strlen($val, 'UTF-8') <= intval($args[0]) : false;
         case 'range_len':
             return count($args) == 2 ? mb_strlen($val, 'UTF-8') >= intval($args[0]) && mb_strlen($val, 'UTF-8') <= intval($args[1]) : false;
         case 'len':
             return isset($args[0]) ? mb_strlen($val, 'UTF-8') == intval($args[0]) : false;
         case 'min':
             return isset($args[0]) && is_numeric($val) ? $val >= $args[0] : false;
         case 'max':
             return isset($args[0]) && is_numeric($val) ? $val <= $args[0] : false;
         case 'range':
             return count($args) == 2 && is_numeric($val) ? $val >= $args[0] && $val <= $args[1] : false;
         case 'alpha':
             #纯字母
             return !preg_match('/[^A-Za-z]+/', $val);
         case 'alpha_num':
             #纯字母和数字
             return !preg_match('/[^A-Za-z0-9]+/', $val);
         case 'alpha_dash':
             #纯字母和数字和下划线和-
             return !preg_match('/[^A-Za-z0-9_-]+/', $val);
         case 'alpha_start':
             #以字母开头
             return preg_match('/^[A-Za-z]+/', $val);
         case 'num':
             #纯数字
             return !preg_match('/[^0-9]+/', $val);
         case 'int':
             #整数
             return preg_match('/^([-+]?[1-9]\\d*|0)$/', $val);
         case 'float':
             #小数
             return preg_match('/^([1-9]\\d*|0)\\.\\d+$/', $val);
         case 'numeric':
             #数字-1,1.2,+3,4e5
             return is_numeric($val);
         case 'natural':
             #自然数0,1,2,3,12,333
             return preg_match('/^([1-9]\\d*|0)$/', $val);
         case 'natural_no_zero':
             #自然数不包含0
             return preg_match('/^[1-9]\\d*$/', $val);
         case 'email':
             $args[0] = isset($args[0]) && $args[0] == 'true' ? TRUE : false;
             return !empty($val) ? preg_match('/^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$/', $val) : $args[0];
         case 'url':
             $args[0] = isset($args[0]) && $args[0] == 'true' ? TRUE : false;
             return !empty($val) ? preg_match('/^http[s]?:\\/\\/[A-Za-z0-9]+\\.[A-Za-z0-9]+[\\/=\\?%\\-&_~`@[\\]\':+!]*([^<>\\"])*$/', $val) : $args[0];
         case 'qq':
             $args[0] = isset($args[0]) && $args[0] == 'true' ? TRUE : false;
             return !empty($val) ? preg_match('/^[1-9][0-9]{4,}$/', $val) : $args[0];
         case 'phone':
             $args[0] = isset($args[0]) && $args[0] == 'true' ? TRUE : false;
             return !empty($val) ? preg_match('/^(?:\\d{3}-?\\d{8}|\\d{4}-?\\d{7})$/', $val) : $args[0];
         case 'mobile':
             $args[0] = isset($args[0]) && $args[0] == 'true' ? TRUE : false;
             return !empty($val) ? preg_match('/^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(14[0-9]{1}))+\\d{8})$/', $val) : $args[0];
         case 'zipcode':
             $args[0] = isset($args[0]) && $args[0] == 'true' ? TRUE : false;
             return !empty($val) ? preg_match('/^[1-9]\\d{5}(?!\\d)$/', $val) : $args[0];
         case 'idcard':
             $args[0] = isset($args[0]) && $args[0] == 'true' ? TRUE : false;
             return !empty($val) ? preg_match('/^\\d{14}(\\d{4}|(\\d{3}[xX])|\\d{1})$/', $val) : $args[0];
         case 'ip':
             $args[0] = isset($args[0]) && $args[0] == 'true' ? TRUE : false;
             return !empty($val) ? preg_match('/^((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)$/', $val) : $args[0];
         case 'chs':
             $count = implode(',', array_slice($args, 1, 2));
             $count = empty($count) ? '1,' : $count;
             $can_empty = isset($args[0]) && $args[0] == 'true';
             return !empty($val) ? preg_match('/^[\\x{4e00}-\\x{9fa5}]{' . $count . '}$/u', $val) : $can_empty;
         case 'date':
             $args[0] = isset($args[0]) && $args[0] == 'true' ? TRUE : false;
             return !empty($val) ? preg_match('/^[0-9]{4}-(((0[13578]|(10|12))-(0[1-9]|[1-2][0-9]|3[0-1]))|(02-(0[1-9]|[1-2][0-9]))|((0[469]|11)-(0[1-9]|[1-2][0-9]|30)))$/', $val) : $args[0];
         case 'time':
             $args[0] = isset($args[0]) && $args[0] == 'true' ? TRUE : false;
             return !empty($val) ? preg_match('/^(([0-1][0-9])|([2][0-3])):([0-5][0-9])(:([0-5][0-9]))$/', $val) : $args[0];
         case 'datetime':
             $args[0] = isset($args[0]) && $args[0] == 'true' ? TRUE : false;
             return !empty($val) ? preg_match('/^[0-9]{4}-(((0[13578]|(10|12))-(0[1-9]|[1-2][0-9]|3[0-1]))|(02-(0[1-9]|[1-2][0-9]))|((0[469]|11)-(0[1-9]|[1-2][0-9]|30))) (([0-1][0-9])|([2][0-3])):([0-5][0-9])(:([0-5][0-9]))$/', $val) : $args[0];
         case 'reg':
             #正则表达式验证,reg[/^[\]]$/i]
             /**
             * 模式修正符说明:
              i	表示在和模式进行匹配进不区分大小写
              m	将模式视为多行,使用^和$表示任何一行都可以以正则表达式开始或结束
              s	如果没有使用这个模式修正符号,元字符中的"."默认不能表示换行符号,将字符串视为单行
              x	表示模式中的空白忽略不计
              e	正则表达式必须使用在preg_replace替换字符串的函数中时才可以使用(讲这个函数时再说)
              A	以模式字符串开头,相当于元字符^
              Z	以模式字符串结尾,相当于元字符$
              U	正则表达式的特点:就是比较“贪婪”,使用该模式修正符可以取消贪婪模式
             */
             return !empty($args[0]) ? preg_match($args[0], $val) : false;
             /**
              * set set_post不参与验证,返回true跳过
              *
              * 说明:
              * set用于设置在验证数据前对数据进行处理的函数或者方法
              * set_post用于设置在验证数据后对数据进行处理的函数或者方法
              * 如果设置了set,数据在验证的时候验证的是处理过的数据
              * 如果设置了set_post,可以通过第三个参数$data接收数据:$this->checkData($rule, $_POST, $data),$data是验证通过并经过set_post处理后的数据
              * set和set_post后面是一个或者多个函数或者方法,多个逗号分割
              * 注意:
              * 1.无论是函数或者方法都必须有一个字符串返回
              * 2.如果是系统函数,系统会传递当前值给系统函数,因此系统函数必须是至少接受一个字符串参数,比如md5,trim
              * 3.如果是自定义的函数,系统会传递当前值和全部数据给自定义的函数,因此自定义函数可以接收两个参数第一个是值,第二个是全部数据$data
              * 4.如果是类的方法写法是:类名称::方法名 (方法静态动态都可以,public,private,都可以)
              */
         /**
          * set set_post不参与验证,返回true跳过
          *
          * 说明:
          * set用于设置在验证数据前对数据进行处理的函数或者方法
          * set_post用于设置在验证数据后对数据进行处理的函数或者方法
          * 如果设置了set,数据在验证的时候验证的是处理过的数据
          * 如果设置了set_post,可以通过第三个参数$data接收数据:$this->checkData($rule, $_POST, $data),$data是验证通过并经过set_post处理后的数据
          * set和set_post后面是一个或者多个函数或者方法,多个逗号分割
          * 注意:
          * 1.无论是函数或者方法都必须有一个字符串返回
          * 2.如果是系统函数,系统会传递当前值给系统函数,因此系统函数必须是至少接受一个字符串参数,比如md5,trim
          * 3.如果是自定义的函数,系统会传递当前值和全部数据给自定义的函数,因此自定义函数可以接收两个参数第一个是值,第二个是全部数据$data
          * 4.如果是类的方法写法是:类名称::方法名 (方法静态动态都可以,public,private,都可以)
          */
         case 'set':
         case 'set_post':
             return true;
         default:
             $_args = array_merge(array($val, $data), $args);
             $matches = self::getCheckRuleInfo($_rule);
             $func = $matches[1];
             $args = $matches[2];
             if (function_exists($func)) {
                 $reflection = new ReflectionFunction($func);
                 //如果是系统函数
                 if ($reflection->isInternal()) {
                     $_args = isset($_args[0]) ? array($_args[0]) : array();
                 }
             }
             return self::callFunc($_rule, $_args);
     }
     return false;
 }
Ejemplo n.º 3
0
 private static function checkRule($_rule, $val, $data, $db = null)
 {
     if (!$db) {
         $db = MpLoader::instance()->database();
     }
     $matches = self::getCheckRuleInfo($_rule);
     $_rule = $matches[1];
     $args = $matches[2];
     switch ($_rule) {
         case 'required':
             return !empty($val);
         case 'match':
             return isset($args[0]) && isset($data[$args[0]]) ? $val && $val == $data[$args[0]] : false;
         case 'equal':
             return isset($args[0]) ? $val && $val == $args[0] : false;
         case 'enum':
             return in_array($val, $args);
         case 'unique':
             if (!$val || !count($args)) {
                 return false;
             }
             $_info = explode('.', $args[0]);
             if (count($_info) != 2) {
                 return false;
             }
             $table = $_info[0];
             $col = $_info[1];
             if (isset($args[1])) {
                 $_id_info = explode(':', $args[1]);
                 if (count($_id_info) != 2) {
                     return false;
                 }
                 $id_col = $_id_info[0];
                 $id = $_id_info[1];
                 $id = stripos($id, '#') === 0 ? MpInput::get_post(substr($id, 1)) : $id;
                 $where = array($col => $val, "{$id_col} <>" => $id);
             } else {
                 $where = array($col => $val);
             }
             return !$db->where($where)->from($table)->count_all_results();
         case 'exists':
             if (!$val || !count($args)) {
                 return false;
             }
             $_info = explode('.', $args[0]);
             if (count($_info) != 2) {
                 return false;
             }
             $table = $_info[0];
             $col = $_info[1];
             $where = array($col => $val);
             if (count($args) > 1) {
                 foreach (array_slice($args, 1) as $v) {
                     $_id_info = explode(':', $v);
                     if (count($_id_info) != 2) {
                         continue;
                     }
                     $id_col = $_id_info[0];
                     $id = $_id_info[1];
                     $id = stripos($id, '#') === 0 ? MpInput::get_post(substr($id, 1)) : $id;
                     $where[$id_col] = $id;
                 }
             }
             return $db->where($where)->from($table)->count_all_results();
         case 'min_len':
             return isset($args[0]) ? mb_strlen($val, 'UTF-8') >= intval($args[0]) : false;
         case 'max_len':
             return isset($args[0]) ? mb_strlen($val, 'UTF-8') <= intval($args[0]) : false;
         case 'range_len':
             return count($args) == 2 ? mb_strlen($val, 'UTF-8') >= intval($args[0]) && mb_strlen($val, 'UTF-8') <= intval($args[1]) : false;
         case 'len':
             return isset($args[0]) ? mb_strlen($val, 'UTF-8') == intval($args[0]) : false;
         case 'min':
             return isset($args[0]) && is_numeric($val) ? $val >= $args[0] : false;
         case 'max':
             return isset($args[0]) && is_numeric($val) ? $val <= $args[0] : false;
         case 'range':
             return count($args) == 2 && is_numeric($val) ? $val >= $args[0] && $val <= $args[1] : false;
         case 'alpha':
             return !preg_match('/[^A-Za-z]+/', $val);
         case 'alpha_num':
             return !preg_match('/[^A-Za-z0-9]+/', $val);
         case 'alpha_dash':
             return !preg_match('/[^A-Za-z0-9_-]+/', $val);
         case 'alpha_start':
             return preg_match('/^[A-Za-z]+/', $val);
         case 'num':
             return !preg_match('/[^0-9]+/', $val);
         case 'int':
             return preg_match('/^([-+]?[1-9]\\d*|0)$/', $val);
         case 'float':
             return preg_match('/^([1-9]\\d*|0)\\.\\d+$/', $val);
         case 'numeric':
             return is_numeric($val);
         case 'natural':
             return preg_match('/^([1-9]\\d*|0)$/', $val);
         case 'natural_no_zero':
             return preg_match('/^[1-9]\\d*$/', $val);
         case 'email':
             $args[0] = isset($args[0]) && $args[0] == 'true' ? TRUE : false;
             return !empty($val) ? preg_match('/^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$/', $val) : $args[0];
         case 'url':
             $args[0] = isset($args[0]) && $args[0] == 'true' ? TRUE : false;
             return !empty($val) ? preg_match('/^http[s]?:\\/\\/[A-Za-z0-9]+\\.[A-Za-z0-9]+[\\/=\\?%\\-&_~`@[\\]\':+!]*([^<>\\"])*$/', $val) : $args[0];
         case 'qq':
             $args[0] = isset($args[0]) && $args[0] == 'true' ? TRUE : false;
             return !empty($val) ? preg_match('/^[1-9][0-9]{4,}$/', $val) : $args[0];
         case 'phone':
             $args[0] = isset($args[0]) && $args[0] == 'true' ? TRUE : false;
             return !empty($val) ? preg_match('/^(?:\\d{3}-?\\d{8}|\\d{4}-?\\d{7})$/', $val) : $args[0];
         case 'mobile':
             $args[0] = isset($args[0]) && $args[0] == 'true' ? TRUE : false;
             return !empty($val) ? preg_match('/^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(14[0-9]{1}))+\\d{8})$/', $val) : $args[0];
         case 'zipcode':
             $args[0] = isset($args[0]) && $args[0] == 'true' ? TRUE : false;
             return !empty($val) ? preg_match('/^[1-9]\\d{5}(?!\\d)$/', $val) : $args[0];
         case 'idcard':
             $args[0] = isset($args[0]) && $args[0] == 'true' ? TRUE : false;
             return !empty($val) ? preg_match('/^\\d{14}(\\d{4}|(\\d{3}[xX])|\\d{1})$/', $val) : $args[0];
         case 'ip':
             $args[0] = isset($args[0]) && $args[0] == 'true' ? TRUE : false;
             return !empty($val) ? preg_match('/^((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)$/', $val) : $args[0];
         case 'chs':
             $count = implode(',', array_slice($args, 1, 2));
             $count = empty($count) ? '1,' : $count;
             $can_empty = isset($args[0]) && $args[0] == 'true';
             return !empty($val) ? preg_match('/^[\\x{4e00}-\\x{9fa5}]{' . $count . '}$/u', $val) : $can_empty;
         case 'date':
             $args[0] = isset($args[0]) && $args[0] == 'true' ? TRUE : false;
             return !empty($val) ? preg_match('/^[0-9]{4}-(((0[13578]|(10|12))-(0[1-9]|[1-2][0-9]|3[0-1]))|(02-(0[1-9]|[1-2][0-9]))|((0[469]|11)-(0[1-9]|[1-2][0-9]|30)))$/', $val) : $args[0];
         case 'time':
             $args[0] = isset($args[0]) && $args[0] == 'true' ? TRUE : false;
             return !empty($val) ? preg_match('/^(([0-1][0-9])|([2][0-3])):([0-5][0-9])(:([0-5][0-9]))$/', $val) : $args[0];
         case 'datetime':
             $args[0] = isset($args[0]) && $args[0] == 'true' ? TRUE : false;
             return !empty($val) ? preg_match('/^[0-9]{4}-(((0[13578]|(10|12))-(0[1-9]|[1-2][0-9]|3[0-1]))|(02-(0[1-9]|[1-2][0-9]))|((0[469]|11)-(0[1-9]|[1-2][0-9]|30))) (([0-1][0-9])|([2][0-3])):([0-5][0-9])(:([0-5][0-9]))$/', $val) : $args[0];
         case 'reg':
             return !empty($args[0]) ? preg_match($args[0], $val) : false;
         case 'set':
         case 'set_post':
             return true;
         default:
             $_args = array_merge(array($val, $data), $args);
             $matches = self::getCheckRuleInfo($_rule);
             $func = $matches[1];
             $args = $matches[2];
             if (function_exists($func)) {
                 $reflection = new ReflectionFunction($func);
                 if ($reflection->isInternal()) {
                     $_args = isset($_args[0]) ? array($_args[0]) : array();
                 }
             }
             return self::callFunc($_rule, $_args);
     }
     return false;
 }