You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
807 lines
27 KiB
807 lines
27 KiB
<?php
|
|
|
|
/**
|
|
* ECTouch 基础函数库
|
|
*/
|
|
function getInstance()
|
|
{
|
|
static $obj = array();
|
|
if (empty($obj)) {
|
|
$obj = A('base/Frontend', 'controllers');
|
|
}
|
|
return $obj;
|
|
}
|
|
|
|
// 检测输入的验证码是否正确,$code为用户输入的验证码字符串
|
|
function check_verify($code, $id = '')
|
|
{
|
|
$verify = new vendor\Verify();
|
|
return $verify->check($code, $id);
|
|
}
|
|
|
|
/**
|
|
* 浏览器友好的变量输出
|
|
* @param mixed $var 变量
|
|
* @param boolean $echo 是否输出 默认为True 如果为false 则返回输出字符串
|
|
* @param string $label 标签 默认为空
|
|
* @param boolean $strict 是否严谨 默认为true
|
|
* @return void|string
|
|
*/
|
|
function dump($var, $echo=true, $label=null, $strict=true) {
|
|
$label = ($label === null) ? '' : rtrim($label) . ' ';
|
|
if (!$strict) {
|
|
if (ini_get('html_errors')) {
|
|
$output = print_r($var, true);
|
|
$output = '<pre>' . $label . htmlspecialchars($output, ENT_QUOTES) . '</pre>';
|
|
} else {
|
|
$output = $label . print_r($var, true);
|
|
}
|
|
} else {
|
|
ob_start();
|
|
var_dump($var);
|
|
$output = ob_get_clean();
|
|
if (!extension_loaded('xdebug')) {
|
|
$output = preg_replace('/\]\=\>\n(\s+)/m', '] => ', $output);
|
|
$output = '<pre>' . $label . htmlspecialchars($output, ENT_QUOTES) . '</pre>';
|
|
}
|
|
}
|
|
if ($echo) {
|
|
echo($output);
|
|
return null;
|
|
}else
|
|
return $output;
|
|
}
|
|
|
|
/**
|
|
* 过滤器方法 引用传值
|
|
* @param string $name 过滤器名称
|
|
* @param string $content 要过滤的内容
|
|
* @return void
|
|
*/
|
|
function filter($name, &$content) {
|
|
$class = $name . 'Filter';
|
|
require_cache(APP_PATH . 'filters/' . $class . '.php');
|
|
$filter = new $class();
|
|
$content = $filter->run($content);
|
|
}
|
|
|
|
/**
|
|
* 判断是否SSL协议
|
|
* @return boolean
|
|
*/
|
|
function is_ssl() {
|
|
if(isset($_SERVER['HTTPS']) && ('1' == $_SERVER['HTTPS'] || 'on' == strtolower($_SERVER['HTTPS']))){
|
|
return true;
|
|
}elseif(isset($_SERVER['SERVER_PORT']) && ('443' == $_SERVER['SERVER_PORT'] )) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* URL重定向
|
|
* @param string $url 重定向的URL地址
|
|
* @param integer $time 重定向的等待时间(秒)
|
|
* @param string $msg 重定向前的提示信息
|
|
* @return void
|
|
*/
|
|
function redirect($url, $time = 0, $msg = '')
|
|
{
|
|
//多行URL地址支持
|
|
$url = str_replace(array("\n", "\r"), '', $url);
|
|
if (empty($msg))
|
|
$msg = "系统将在{$time}秒之后自动跳转到{$url}!";
|
|
if (!headers_sent()) {
|
|
// redirect
|
|
if (0 === $time) {
|
|
header('Location: ' . $url);
|
|
} else {
|
|
header("refresh:{$time};url={$url}");
|
|
echo($msg);
|
|
}
|
|
exit();
|
|
} else {
|
|
$str = "<meta http-equiv='Refresh' content='{$time};URL={$url}'>";
|
|
if ($time != 0)
|
|
$str .= $msg;
|
|
exit($str);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 缓存管理
|
|
* @param mixed $name 缓存名称,如果为数组表示进行缓存设置
|
|
* @param mixed $value 缓存值
|
|
* @param mixed $options 缓存参数
|
|
* @return mixed
|
|
*/
|
|
function S($name,$value='',$options=null) {
|
|
static $cache = '';
|
|
if(is_array($options) && empty($cache)){
|
|
// 缓存操作的同时初始化
|
|
$type = isset($options['type'])?$options['type']:'';
|
|
$cache = Cache::getInstance($type,$options);
|
|
}elseif(is_array($name)) { // 缓存初始化
|
|
$type = isset($name['type'])?$name['type']:'';
|
|
$cache = Cache::getInstance($type,$name);
|
|
return $cache;
|
|
}elseif(empty($cache)) { // 自动初始化
|
|
$cache = Cache::getInstance();
|
|
}
|
|
if(''=== $value){ // 获取缓存
|
|
return $cache->get($name);
|
|
}elseif(is_null($value)) { // 删除缓存
|
|
return $cache->rm($name);
|
|
}else { // 缓存数据
|
|
if(is_array($options)) {
|
|
$expire = isset($options['expire'])?$options['expire']:NULL;
|
|
}else{
|
|
$expire = is_numeric($options)?$options:NULL;
|
|
}
|
|
return $cache->set($name, $value, $expire);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 快速文件数据读取和保存 针对简单类型数据 字符串、数组
|
|
* @param string $name 缓存名称
|
|
* @param mixed $value 缓存值
|
|
* @param string $path 缓存路径
|
|
* @return mixed
|
|
*/
|
|
function F($name, $value='', $path=DATA_PATH) {
|
|
static $_cache = array();
|
|
$filename = $path . $name . '.php';
|
|
if ('' !== $value) {
|
|
if (is_null($value)) {
|
|
// 删除缓存
|
|
return false !== strpos($name,'*')?array_map("unlink", glob($filename)):unlink($filename);
|
|
} else {
|
|
// 缓存数据
|
|
$dir = dirname($filename);
|
|
// 目录不存在则创建
|
|
if (!is_dir($dir))
|
|
mkdir($dir,0755,true);
|
|
$_cache[$name] = $value;
|
|
return file_put_contents($filename, strip_whitespace("<?php\treturn " . var_export($value, true) . ";?>"));
|
|
}
|
|
}
|
|
if (isset($_cache[$name]))
|
|
return $_cache[$name];
|
|
// 获取缓存数据
|
|
if (is_file($filename)) {
|
|
$value = include $filename;
|
|
$_cache[$name] = $value;
|
|
} else {
|
|
$value = false;
|
|
}
|
|
return $value;
|
|
}
|
|
|
|
/**
|
|
* 根据PHP各种类型变量生成唯一标识号
|
|
* @param mixed $mix 变量
|
|
* @return string
|
|
*/
|
|
function to_guid_string($mix) {
|
|
if (is_object($mix) && function_exists('spl_object_hash')) {
|
|
return spl_object_hash($mix);
|
|
} elseif (is_resource($mix)) {
|
|
$mix = get_resource_type($mix) . strval($mix);
|
|
} else {
|
|
$mix = serialize($mix);
|
|
}
|
|
return md5($mix);
|
|
}
|
|
|
|
|
|
/**
|
|
* XML编码
|
|
* @param mixed $data 数据
|
|
* @param string $root 根节点名
|
|
* @param string $item 数字索引的子节点名
|
|
* @param string $attr 根节点属性
|
|
* @param string $id 数字索引子节点key转换的属性名
|
|
* @param string $encoding 数据编码
|
|
* @return string
|
|
*/
|
|
function xml_encode($data, $root='think', $item='item', $attr='', $id='id', $encoding='utf-8') {
|
|
if(is_array($attr)){
|
|
$_attr = array();
|
|
foreach ($attr as $key => $value) {
|
|
$_attr[] = "{$key}=\"{$value}\"";
|
|
}
|
|
$attr = implode(' ', $_attr);
|
|
}
|
|
$attr = trim($attr);
|
|
$attr = empty($attr) ? '' : " {$attr}";
|
|
$xml = "<?xml version=\"1.0\" encoding=\"{$encoding}\"?>";
|
|
$xml .= "<{$root}{$attr}>";
|
|
$xml .= data_to_xml($data, $item, $id);
|
|
$xml .= "</{$root}>";
|
|
return $xml;
|
|
}
|
|
|
|
/**
|
|
* 数据XML编码
|
|
* @param mixed $data 数据
|
|
* @param string $item 数字索引时的节点名称
|
|
* @param string $id 数字索引key转换为的属性名
|
|
* @return string
|
|
*/
|
|
function data_to_xml($data, $item='item', $id='id') {
|
|
$xml = $attr = '';
|
|
foreach ($data as $key => $val) {
|
|
if(is_numeric($key)){
|
|
$id && $attr = " {$id}=\"{$key}\"";
|
|
$key = $item;
|
|
}
|
|
$xml .= "<{$key}{$attr}>";
|
|
$xml .= (is_array($val) || is_object($val)) ? data_to_xml($val, $item, $id) : $val;
|
|
$xml .= "</{$key}>";
|
|
}
|
|
return $xml;
|
|
}
|
|
|
|
/**
|
|
* session管理函数
|
|
* @param string|array $name session名称 如果为数组则表示进行session设置
|
|
* @param mixed $value session值
|
|
* @return mixed
|
|
*/
|
|
function session($name,$value='') {
|
|
$prefix = C('SESSION_PREFIX');
|
|
if(is_array($name)) { // session初始化 在session_start 之前调用
|
|
if(isset($name['prefix'])) C('SESSION_PREFIX',$name['prefix']);
|
|
if(C('VAR_SESSION_ID') && isset($_REQUEST[C('VAR_SESSION_ID')])){
|
|
session_id($_REQUEST[C('VAR_SESSION_ID')]);
|
|
}elseif(isset($name['id'])) {
|
|
session_id($name['id']);
|
|
}
|
|
ini_set('session.auto_start', 0);
|
|
if(isset($name['name'])) session_name($name['name']);
|
|
if(isset($name['path'])) session_save_path($name['path']);
|
|
if(isset($name['domain'])) ini_set('session.cookie_domain', $name['domain']);
|
|
if(isset($name['expire'])) ini_set('session.gc_maxlifetime', $name['expire']);
|
|
if(isset($name['use_trans_sid'])) ini_set('session.use_trans_sid', $name['use_trans_sid']?1:0);
|
|
if(isset($name['use_cookies'])) ini_set('session.use_cookies', $name['use_cookies']?1:0);
|
|
if(isset($name['cache_limiter'])) session_cache_limiter($name['cache_limiter']);
|
|
if(isset($name['cache_expire'])) session_cache_expire($name['cache_expire']);
|
|
if(isset($name['type'])) C('SESSION_TYPE',$name['type']);
|
|
if(C('SESSION_TYPE')) { // 读取session驱动
|
|
$class = 'Session'. ucwords(strtolower(C('SESSION_TYPE')));
|
|
// 检查驱动类
|
|
if(require_cache(EXTEND_PATH.'Driver/Session/'.$class.'.class.php')) {
|
|
$hander = new $class();
|
|
$hander->execute();
|
|
}else {
|
|
// 类没有定义
|
|
throw_exception(L('_CLASS_NOT_EXIST_').': ' . $class);
|
|
}
|
|
}
|
|
// 启动session
|
|
if(C('SESSION_AUTO_START')) session_start();
|
|
}elseif('' === $value){
|
|
if(0===strpos($name,'[')) { // session 操作
|
|
if('[pause]'==$name){ // 暂停session
|
|
session_write_close();
|
|
}elseif('[start]'==$name){ // 启动session
|
|
session_start();
|
|
}elseif('[destroy]'==$name){ // 销毁session
|
|
$_SESSION = array();
|
|
session_unset();
|
|
session_destroy();
|
|
}elseif('[regenerate]'==$name){ // 重新生成id
|
|
session_regenerate_id();
|
|
}
|
|
}elseif(0===strpos($name,'?')){ // 检查session
|
|
$name = substr($name,1);
|
|
if(strpos($name,'.')){ // 支持数组
|
|
list($name1,$name2) = explode('.',$name);
|
|
return $prefix?isset($_SESSION[$prefix][$name1][$name2]):isset($_SESSION[$name1][$name2]);
|
|
}else{
|
|
return $prefix?isset($_SESSION[$prefix][$name]):isset($_SESSION[$name]);
|
|
}
|
|
}elseif(is_null($name)){ // 清空session
|
|
if($prefix) {
|
|
unset($_SESSION[$prefix]);
|
|
}else{
|
|
$_SESSION = array();
|
|
}
|
|
}elseif($prefix){ // 获取session
|
|
if(strpos($name,'.')){
|
|
list($name1,$name2) = explode('.',$name);
|
|
return isset($_SESSION[$prefix][$name1][$name2])?$_SESSION[$prefix][$name1][$name2]:null;
|
|
}else{
|
|
return isset($_SESSION[$prefix][$name])?$_SESSION[$prefix][$name]:null;
|
|
}
|
|
}else{
|
|
if(strpos($name,'.')){
|
|
list($name1,$name2) = explode('.',$name);
|
|
return isset($_SESSION[$name1][$name2])?$_SESSION[$name1][$name2]:null;
|
|
}else{
|
|
return isset($_SESSION[$name])?$_SESSION[$name]:null;
|
|
}
|
|
}
|
|
}elseif(is_null($value)){ // 删除session
|
|
if($prefix){
|
|
unset($_SESSION[$prefix][$name]);
|
|
}else{
|
|
unset($_SESSION[$name]);
|
|
}
|
|
}else{ // 设置session
|
|
if($prefix){
|
|
if (!is_array($_SESSION[$prefix])) {
|
|
$_SESSION[$prefix] = array();
|
|
}
|
|
$_SESSION[$prefix][$name] = $value;
|
|
}else{
|
|
$_SESSION[$name] = $value;
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Cookie 设置、获取、删除
|
|
* @param string $name cookie名称
|
|
* @param mixed $value cookie值
|
|
* @param mixed $options cookie参数
|
|
* @return mixed
|
|
*/
|
|
function cookie($name, $value='', $option=null) {
|
|
// 默认设置
|
|
$config = array(
|
|
'prefix' => C('COOKIE_PREFIX'), // cookie 名称前缀
|
|
'expire' => C('COOKIE_EXPIRE'), // cookie 保存时间
|
|
'path' => C('COOKIE_PATH'), // cookie 保存路径
|
|
'domain' => C('COOKIE_DOMAIN'), // cookie 有效域名
|
|
);
|
|
// 参数设置(会覆盖黙认设置)
|
|
if (!is_null($option)) {
|
|
if (is_numeric($option))
|
|
$option = array('expire' => $option);
|
|
elseif (is_string($option))
|
|
parse_str($option, $option);
|
|
$config = array_merge($config, array_change_key_case($option));
|
|
}
|
|
// 清除指定前缀的所有cookie
|
|
if (is_null($name)) {
|
|
if (empty($_COOKIE))
|
|
return;
|
|
// 要删除的cookie前缀,不指定则删除config设置的指定前缀
|
|
$prefix = empty($value) ? $config['prefix'] : $value;
|
|
if (!empty($prefix)) {// 如果前缀为空字符串将不作处理直接返回
|
|
foreach ($_COOKIE as $key => $val) {
|
|
if (0 === stripos($key, $prefix)) {
|
|
setcookie($key, '', time() - 3600, $config['path'], $config['domain']);
|
|
unset($_COOKIE[$key]);
|
|
}
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
$name = $config['prefix'] . $name;
|
|
if ('' === $value) {
|
|
if(isset($_COOKIE[$name])){
|
|
$value = $_COOKIE[$name];
|
|
if(0===strpos($value,'think:')){
|
|
$value = substr($value,6);
|
|
return array_map('urldecode',json_decode(MAGIC_QUOTES_GPC?stripslashes($value):$value,true));
|
|
}else{
|
|
return $value;
|
|
}
|
|
}else{
|
|
return null;
|
|
}
|
|
} else {
|
|
if (is_null($value)) {
|
|
setcookie($name, '', time() - 3600, $config['path'], $config['domain']);
|
|
unset($_COOKIE[$name]); // 删除指定cookie
|
|
} else {
|
|
// 设置cookie
|
|
if(is_array($value)){
|
|
$value = 'think:'.json_encode(array_map('urlencode',$value));
|
|
}
|
|
$expire = !empty($config['expire']) ? time() + intval($config['expire']) : 0;
|
|
setcookie($name, $value, $expire, $config['path'], $config['domain']);
|
|
$_COOKIE[$name] = $value;
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取客户端IP地址
|
|
* @param integer $type 返回类型 0 返回IP地址 1 返回IPV4地址数字
|
|
* @return mixed
|
|
*/
|
|
function get_client_ip($type = 0) {
|
|
$type = $type ? 1 : 0;
|
|
static $ip = NULL;
|
|
if ($ip !== NULL) return $ip[$type];
|
|
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
|
|
$arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
|
|
$pos = array_search('unknown',$arr);
|
|
if(false !== $pos) unset($arr[$pos]);
|
|
$ip = trim($arr[0]);
|
|
}elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
|
|
$ip = $_SERVER['HTTP_CLIENT_IP'];
|
|
}elseif (isset($_SERVER['REMOTE_ADDR'])) {
|
|
$ip = $_SERVER['REMOTE_ADDR'];
|
|
}
|
|
// IP地址合法验证
|
|
$long = sprintf("%u",ip2long($ip));
|
|
$ip = $long ? array($ip, $long) : array('0.0.0.0', 0);
|
|
return $ip[$type];
|
|
}
|
|
|
|
/**
|
|
* 发送HTTP状态
|
|
* @param integer $code 状态码
|
|
* @return void
|
|
*/
|
|
function send_http_status($code) {
|
|
static $_status = array(
|
|
// Informational 1xx
|
|
100 => 'Continue',
|
|
101 => 'Switching Protocols',
|
|
// Success 2xx
|
|
200 => 'OK',
|
|
201 => 'Created',
|
|
202 => 'Accepted',
|
|
203 => 'Non-Authoritative Information',
|
|
204 => 'No Content',
|
|
205 => 'Reset Content',
|
|
206 => 'Partial Content',
|
|
// Redirection 3xx
|
|
300 => 'Multiple Choices',
|
|
301 => 'Moved Permanently',
|
|
302 => 'Moved Temporarily ', // 1.1
|
|
303 => 'See Other',
|
|
304 => 'Not Modified',
|
|
305 => 'Use Proxy',
|
|
// 306 is deprecated but reserved
|
|
307 => 'Temporary Redirect',
|
|
// Client Error 4xx
|
|
400 => 'Bad Request',
|
|
401 => 'Unauthorized',
|
|
402 => 'Payment Required',
|
|
403 => 'Forbidden',
|
|
404 => 'Not Found',
|
|
405 => 'Method Not Allowed',
|
|
406 => 'Not Acceptable',
|
|
407 => 'Proxy Authentication Required',
|
|
408 => 'Request Timeout',
|
|
409 => 'Conflict',
|
|
410 => 'Gone',
|
|
411 => 'Length Required',
|
|
412 => 'Precondition Failed',
|
|
413 => 'Request Entity Too Large',
|
|
414 => 'Request-URI Too Long',
|
|
415 => 'Unsupported Media Type',
|
|
416 => 'Requested Range Not Satisfiable',
|
|
417 => 'Expectation Failed',
|
|
// Server Error 5xx
|
|
500 => 'Internal Server Error',
|
|
501 => 'Not Implemented',
|
|
502 => 'Bad Gateway',
|
|
503 => 'Service Unavailable',
|
|
504 => 'Gateway Timeout',
|
|
505 => 'HTTP Version Not Supported',
|
|
509 => 'Bandwidth Limit Exceeded'
|
|
);
|
|
if(isset($_status[$code])) {
|
|
header('HTTP/1.1 '.$code.' '.$_status[$code]);
|
|
// 确保FastCGI模式下正常
|
|
header('Status:'.$code.' '.$_status[$code]);
|
|
}
|
|
}
|
|
|
|
// 过滤表单中的表达式
|
|
function touch_filter(&$value){
|
|
// TODO 其他安全过滤
|
|
|
|
// 过滤查询特殊字符
|
|
if(preg_match('/^(EXP|NEQ|GT|EGT|LT|ELT|OR|LIKE|NOTLIKE|NOTBETWEEN|NOT BETWEEN|BETWEEN|NOTIN|NOT IN|IN)$/i',$value)){
|
|
$value .= ' ';
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取输入参数 支持过滤和默认值
|
|
* 使用方法:
|
|
* <code>
|
|
* I('id',0); 获取id参数 自动判断get或者post
|
|
* I('post.name','','htmlspecialchars'); 获取$_POST['name']
|
|
* I('get.'); 获取$_GET
|
|
* </code>
|
|
* @param string $name 变量的名称 支持指定类型
|
|
* @param mixed $default 不存在的时候默认值
|
|
* @param mixed $filter 参数过滤方法
|
|
* @return mixed
|
|
*/
|
|
function I($name,$default='',$filter=null) {
|
|
if(strpos($name,'.')) { // 指定参数来源
|
|
list($method,$name) = explode('.',$name,2);
|
|
}else{ // 默认为自动判断
|
|
$method = 'param';
|
|
}
|
|
switch(strtolower($method)) {
|
|
case 'get' : $input =& $_GET;break;
|
|
case 'post' : $input =& $_POST;break;
|
|
case 'put' : parse_str(file_get_contents('php://input'), $input);break;
|
|
case 'param' :
|
|
switch($_SERVER['REQUEST_METHOD']) {
|
|
case 'POST':
|
|
$input = $_POST;
|
|
break;
|
|
case 'PUT':
|
|
parse_str(file_get_contents('php://input'), $input);
|
|
break;
|
|
default:
|
|
$input = $_GET;
|
|
}
|
|
if(C('VAR_URL_PARAMS') && isset($_GET[C('VAR_URL_PARAMS')])){
|
|
$input = array_merge($input,$_GET[C('VAR_URL_PARAMS')]);
|
|
}
|
|
break;
|
|
case 'request' : $input =& $_REQUEST; break;
|
|
case 'session' : $input =& $_SESSION; break;
|
|
case 'cookie' : $input =& $_COOKIE; break;
|
|
case 'server' : $input =& $_SERVER; break;
|
|
case 'globals' : $input =& $GLOBALS; break;
|
|
default:
|
|
return NULL;
|
|
}
|
|
// 全局过滤
|
|
if(C('VAR_FILTERS')) {
|
|
$_filters = explode(',',C('VAR_FILTERS'));
|
|
foreach($_filters as $_filter){
|
|
// 全局参数过滤
|
|
array_walk_recursive($input,$_filter);
|
|
}
|
|
}
|
|
if(empty($name)) { // 获取全部变量
|
|
$data = $input;
|
|
$filters = isset($filter)?$filter:C('DEFAULT_FILTER');
|
|
if($filters) {
|
|
$filters = explode(',',$filters);
|
|
foreach($filters as $filter){
|
|
$data = array_map($filter,$data); // 参数过滤
|
|
}
|
|
}
|
|
}elseif(isset($input[$name])) { // 取值操作
|
|
$data = $input[$name];
|
|
$filters = isset($filter)?$filter:C('DEFAULT_FILTER');
|
|
if($filters) {
|
|
$filters = explode(',',$filters);
|
|
foreach($filters as $filter){
|
|
if(function_exists($filter)) {
|
|
$data = is_array($data) ? array_map_recursive($filter,$data) : $filter($data); // 参数过滤
|
|
}else{
|
|
$data = filter_var($data,is_int($filter)?$filter:filter_id($filter));
|
|
if(false === $data) {
|
|
return isset($default)?$default:NULL;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}else{ // 变量默认值
|
|
$data = isset($default)?$default:NULL;
|
|
}
|
|
is_array($data) && array_walk_recursive($data,'touch_filter');
|
|
return $data;
|
|
}
|
|
|
|
function array_map_recursive($filter, $data) {
|
|
$result = array();
|
|
foreach ($data as $key => $val) {
|
|
$result[$key] = is_array($val)
|
|
? array_map_recursive($filter, $val)
|
|
: call_user_func($filter, $val);
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
|
|
/**
|
|
* 记录和统计时间(微秒)和内存使用情况
|
|
* 使用方法:
|
|
* <code>
|
|
* G('begin'); // 记录开始标记位
|
|
* // ... 区间运行代码
|
|
* G('end'); // 记录结束标签位
|
|
* echo G('begin','end',6); // 统计区间运行时间 精确到小数后6位
|
|
* echo G('begin','end','m'); // 统计区间内存使用情况
|
|
* 如果end标记位没有定义,则会自动以当前作为标记位
|
|
* 其中统计内存使用需要 MEMORY_LIMIT_ON 常量为true才有效
|
|
* </code>
|
|
* @param string $start 开始标签
|
|
* @param string $end 结束标签
|
|
* @param integer|string $dec 小数位或者m
|
|
* @return mixed
|
|
*/
|
|
function G($start,$end='',$dec=4) {
|
|
static $_info = array();
|
|
static $_mem = array();
|
|
if(is_float($end)) { // 记录时间
|
|
$_info[$start] = $end;
|
|
}elseif(!empty($end)){ // 统计时间和内存使用
|
|
if(!isset($_info[$end])) $_info[$end] = microtime(TRUE);
|
|
if(MEMORY_LIMIT_ON && $dec=='m'){
|
|
if(!isset($_mem[$end])) $_mem[$end] = memory_get_usage();
|
|
return number_format(($_mem[$end]-$_mem[$start])/1024);
|
|
}else{
|
|
return number_format(($_info[$end]-$_info[$start]),$dec);
|
|
}
|
|
|
|
}else{ // 记录时间和内存使用
|
|
$_info[$start] = microtime(TRUE);
|
|
if(MEMORY_LIMIT_ON) $_mem[$start] = memory_get_usage();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 设置和获取统计数据
|
|
* 使用方法:
|
|
* <code>
|
|
* N('db',1); // 记录数据库操作次数
|
|
* N('read',1); // 记录读取次数
|
|
* echo N('db'); // 获取当前页面数据库的所有操作次数
|
|
* echo N('read'); // 获取当前页面读取次数
|
|
* </code>
|
|
* @param string $key 标识位置
|
|
* @param integer $step 步进值
|
|
* @return mixed
|
|
*/
|
|
function N($key, $step=0,$save=false) {
|
|
static $_num = array();
|
|
if (!isset($_num[$key])) {
|
|
$_num[$key] = (false !== $save)? S('N_'.$key) : 0;
|
|
}
|
|
if (empty($step))
|
|
return $_num[$key];
|
|
else
|
|
$_num[$key] = $_num[$key] + (int) $step;
|
|
if(false !== $save){ // 保存结果
|
|
S('N_'.$key,$_num[$key],$save);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 字符串命名风格转换
|
|
* type 0 将Java风格转换为C的风格 1 将C风格转换为Java的风格
|
|
* @param string $name 字符串
|
|
* @param integer $type 转换类型
|
|
* @return string
|
|
*/
|
|
function parse_name($name, $type=0) {
|
|
if ($type) {
|
|
return ucfirst(preg_replace("/_([a-zA-Z])/e", "strtoupper('\\1')", $name));
|
|
} else {
|
|
return strtolower(trim(preg_replace("/[A-Z]/", "_\\0", $name), "_"));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 优化的require_once
|
|
* @param string $filename 文件地址
|
|
* @return boolean
|
|
*/
|
|
function require_cache($filename) {
|
|
static $_importFiles = array();
|
|
if (!isset($_importFiles[$filename])) {
|
|
if (file_exists_case($filename)) {
|
|
require $filename;
|
|
$_importFiles[$filename] = true;
|
|
} else {
|
|
$_importFiles[$filename] = false;
|
|
}
|
|
}
|
|
return $_importFiles[$filename];
|
|
}
|
|
|
|
/**
|
|
* 批量导入文件 成功则返回
|
|
* @param array $array 文件数组
|
|
* @param boolean $return 加载成功后是否返回
|
|
* @return boolean
|
|
*/
|
|
function require_array($array,$return=false){
|
|
foreach ($array as $file){
|
|
if (require_cache($file) && $return) return true;
|
|
}
|
|
if($return) return false;
|
|
}
|
|
|
|
/**
|
|
* 区分大小写的文件存在判断
|
|
* @param string $filename 文件地址
|
|
* @return boolean
|
|
*/
|
|
function file_exists_case($filename) {
|
|
if (is_file($filename)) {
|
|
if (IS_WIN && C('APP_FILE_CASE')) {
|
|
if (basename(realpath($filename)) != basename($filename))
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* M函数用于实例化一个没有模型文件的Model
|
|
* @param string $name Model名称 支持指定基础模型 例如 MongoModel:User
|
|
* @param string $tablePrefix 表前缀
|
|
* @param mixed $connection 数据库连接信息
|
|
* @return Model
|
|
*/
|
|
function M($name='', $tablePrefix='',$connection='') {
|
|
static $_model = array();
|
|
if(strpos($name,':')) {
|
|
list($class,$name) = explode(':',$name);
|
|
}else{
|
|
$class = 'Model';
|
|
}
|
|
$guid = $tablePrefix . $name . '_' . $class;
|
|
if (!isset($_model[$guid]))
|
|
$_model[$guid] = new $class($name,$tablePrefix,$connection);
|
|
return $_model[$guid];
|
|
}
|
|
|
|
/**
|
|
* 获取和设置语言定义(不区分大小写)
|
|
* @param string|array $name 语言变量
|
|
* @param string $value 语言值
|
|
* @return mixed
|
|
*/
|
|
function L($name=null, $value=null) {
|
|
static $_lang = array();
|
|
// 空参数返回所有定义
|
|
if (empty($name))
|
|
return $_lang;
|
|
// 判断语言获取(或设置)
|
|
// 若不存在,直接返回全大写$name
|
|
if (is_string($name)) {
|
|
$name = strtoupper($name);
|
|
if (is_null($value))
|
|
return isset($_lang[$name]) ? $_lang[$name] : $name;
|
|
$_lang[$name] = $value; // 语言定义
|
|
return;
|
|
}
|
|
// 批量定义
|
|
if (is_array($name))
|
|
$_lang = array_merge($_lang, array_change_key_case($name, CASE_UPPER));
|
|
return;
|
|
}
|
|
|
|
// 根据数组生成常量定义
|
|
function array_define($array,$check=true) {
|
|
$content = "\n";
|
|
foreach ($array as $key => $val) {
|
|
$key = strtoupper($key);
|
|
if($check) $content .= 'defined(\'' . $key . '\') or ';
|
|
if (is_int($val) || is_float($val)) {
|
|
$content .= "define('" . $key . "'," . $val . ');';
|
|
} elseif (is_bool($val)) {
|
|
$val = ($val) ? 'true' : 'false';
|
|
$content .= "define('" . $key . "'," . $val . ');';
|
|
} elseif (is_string($val)) {
|
|
$content .= "define('" . $key . "','" . addslashes($val) . "');";
|
|
}
|
|
$content .= "\n";
|
|
}
|
|
return $content;
|
|
}
|
|
|
|
/**
|
|
* 写入日志文件
|
|
* @param string $word
|
|
*/
|
|
function logResult($word='') {
|
|
$fp = fopen("log.txt","a");
|
|
flock($fp, LOCK_EX) ;
|
|
fwrite($fp,"执行日期:".strftime("%Y%m%d%H%M%S",time())."\n".$word."\n");
|
|
flock($fp, LOCK_UN);
|
|
fclose($fp);
|
|
}
|
|
|