<?php
/**
* 常用函數庫
*
*/
class Core_Fun
{
? ? /**
? ? * 對變量進行反轉義到原始數據
? ? *
? ? * @param string|array $param 需要反轉義的原始數據
? ? * @return string|array
? ? * @author Icehu
? ? */
? ? public static function stripslashes($param)
? ? {
? ? ? ? if (is_array($param))
? ? ? ? {
? ? ? ? ? ? foreach ($param as $k => $v)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? $param[$k] = self::stripslashes($v);
? ? ? ? ? ? }
? ? ? ? ? ? return $param;
? ? ? ? }
? ? ? ? else
? ? ? ? {
? ? ? ? ? ? return stripslashes($param);
? ? ? ? }
? ? }
? ? /**
? ? * 字符轉碼方法
? ? * @param string $in_charst 輸入字符集
? ? * @param string $out_charset 輸出字符集
? ? * @param string|array $param 轉換數據
? ? * @return string|array
? ? * @author Icehu
? ? */
? ? public static function iconv($in_charst, $out_charset, $param)
? ? {
? ? ? ? if (is_array($param))
? ? ? ? {
? ? ? ? ? ? foreach ($param as $_key => $_var)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? $param[$_key] = self::iconv($in_charst, $out_charset, $_var);
? ? ? ? ? ? }
? ? ? ? ? ? return $param;
? ? ? ? }
? ? ? ? else
? ? ? ? {
? ? ? ? ? ? if (function_exists('iconv'))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? return @iconv($in_charst, $out_charset, $param);
? ? ? ? ? ? }
? ? ? ? ? ? elseif (function_exists('mb_convert_encoding'))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? return @mb_convert_encoding($param, $out_charset, $in_charst);
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? return $param;
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? /**
? ? * 獲取上一次訪問的地址
? ? *
? ? * @return string
? ? * @author Icehu
? ? * @todo 考慮登陸、退出地址、SEO轉換等問題
? ? */
? ? public static function getreffer()
? ? {
? ? ? ? return $_SERVER['HTTP_REFERER'];
? ? }
? ? /**
? ? * 設置Cookie
? ? *
? ? * @param string $name Cookie名稱
? ? * @param string $value Cookie值
? ? * @param number $kptime Cookie有效期
? ? * @param bool $httponly 是否httponly
? ? * @author Icehu
? ? */
? ? public static function setcookie($name, $value=null, $kptime=0, $httponly=false)
? ? {
? ? ? ? $cookie_pre = Core_Config::get('cookiepre', 'basic', 't_');
? ? ? ? $domain = Core_Config::get('cookiedomain', 'basic', null);
? ? ? ? if (null !== $value)
? ? ? ? {
? ? ? ? ? ? if ($kptime)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? setcookie($cookie_pre . $name, $value, self::time() + $kptime, '/', $domain, $_SERVER['SERVER_PORT'] == 443 ? 1 : 0, $httponly);
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? setcookie($cookie_pre . $name, $value, null, '/', $domain, $_SERVER['SERVER_PORT'] == 443 ? 1 : 0, $httponly);
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? else
? ? ? ? {
? ? ? ? ? ? //drop
? ? ? ? ? ? setcookie($cookie_pre . $name, '', self::time() - 3600, '/', $domain, $_SERVER['SERVER_PORT'] == 443 ? 1 : 0);
? ? ? ? }
? ? }
? ? /**
? ? * 獲取Cookie的值
? ? * @param string $name
? ? * @return string
? ? * @author Icehu
? ? */
? ? public static function getcookie($name)
? ? {
? ? ? ? $cookie_pre = Core_Config::get('cookiepre', 'basic', 't_');
? ? ? ? return @$_COOKIE[$cookie_pre . $name];
? ? }
? ? /**
? ? * 獲取腳本運行時間
? ? *
? ? * @return number
? ? * @author Icehu
? ? */
? ? public static function time()
? ? {
? ? ? ? if (!isset($_SERVER['REQUEST_TIME']))
? ? ? ? {
? ? ? ? ? ? $_SERVER['REQUEST_TIME'] = time();
? ? ? ? }
? ? ? ? $timemodify = Core_Config::get('timemodify', 'basic',0);
? ? ? ? return $_SERVER['REQUEST_TIME'] + intval($timemodify);
? ? }
? ? /**
? ? * 獲取當前腳本執行的微秒時間
? ? * @return float
? ? * @author Icehu
? ? */
? ? public static function microtime()
? ? {
? ? ? ? return microtime(true);
? ? }
? ? /**
? ? * 獲取客戶端IP
? ? * 目前是取帶來IP,是否取真實IP?如果取真實IP,可能被偽造
? ? * @return string
? ? * @author Icehu
? ? * @todo 取真實IP?
? ? */
? ? public static function ip()
? ? {
? ? ? ? if (isset($_SERVER['REMOTE_ADDR']))
? ? ? ? {
? ? ? ? ? ? return $_SERVER['REMOTE_ADDR'];
? ? ? ? }
? ? ? ? else if ($_tmp = getenv('REMOTE_ADDR'))
? ? ? ? {
? ? ? ? ? ? return $_tmp;
? ? ? ? }
? ? ? ? return 'unknow';
? ? }
? ? /**
? ? * 考慮系統配置時區的時間格式化方法
? ? * 默認 +8 時區
? ? *
? ? * @param string $format 格式化時間的字符串,同date方法
? ? * @param number $timestamp 時間戳
? ? * @return string
? ? * @author Icehu
? ? */
? ? public static function date($format, $timestamp=null)
? ? {
? ? ? ? $timestamp = (null == $timestamp) ? self::time() : $timestamp;
? ? ? ? $timezone = Core_Config::get('timezone', 'basic', 8);
? ? ? ? return gmdate($format, $timestamp + $timezone * 3600);
? ? }
? ? /**
? ? * 考慮了系統設置時區的時間戳生成方法
? ? *
? ? * @param number $hour 小時
? ? * @param number $min 分鐘
? ? * @param number $sec 秒鐘
? ? * @param number $mon 月份
? ? * @param number $year 年份
? ? * @param number $day 日期
? ? * @return timestamp
? ? * @author Icehu
? ? */
? ? public static function mktime($hour, $min, $sec, $mon, $year, $day)
? ? {
? ? ? ? $timezone = Core_Config::get('timezone', 'basic', 8);
? ? ? ? $t = gmmktime($hour, $min, $sec, $mon, $day, $year);
? ? ? ? return $t ? $t - $timezone * 3600 : 0;
? ? }
? ? /**
? ? * 將 yyyy-mm-dd H:i:s 格式的時間格式化成時間戳
? ? * @param string $str
? ? * @return timestamp
? ? * @author Icehu
? ? */
? ? public static function strtotime($str)
? ? {
? ? ? ? $str = str_replace(array(' ', ':'), '-', trim($str));
? ? ? ? $a = explode('-', $str);
? ? ? ? @list($year, $mon, $day, $hour, $min, $sec) = $a;
? ? ? ? return self::mktime($hour, $min, $sec, $mon, $year, $day);
? ? }
? ? /**
? ? * 將數組轉換為JSON格式
? ? * 所有格式的數組都將轉換為json對象,而不會轉換為js array
? ? *
? ? * @param array $array
? ? * @param bool $_s
? ? * @return string
? ? * @author Icehu
? ? */
? ? public static function array2json($array=array(), $_s = false)
? ? {
? ? ? ? $r = array();
? ? ? ? foreach ((array) $array as $key => $val)
? ? ? ? {
? ? ? ? ? ? if (is_array($val))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? $r[$key] = "\"$key\": " . self::array2json($val, $_s);
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if ($_s && $key == '_s')
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? $r[$key] = "\"$key\": " . $val;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? if (is_numeric($val))
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? $r[$key] = "\"$key\": " . $val;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? else if (is_bool($val))
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? $r[$key] = "\"$key\": " . ($val ? 'true' : 'false');
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? else if (is_null($val))
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? $r[$key] = "\"$key\": null";
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? $r[$key] = "\"$key\": \"" . str_replace(array("\r\n", "\n", "\""), array("\\n", "\\n", "\\\""), $val) . '"';
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return '{' . implode(',', $r) . '}';
? ? }
? ? /**
? ? *
? ? **/
? ? public static function error($msg, $code=-1, $params=array())
? ? {
? ? ? ? throw new Core_Exception($msg, $code, $params);
? ? }
? ? /**
? ? * 返回json對象
? ? */
? ? public static function returnJson( $code , $msg='' , $params=array(), $callback=NULL)
? ? {
? ? ? ? $msg = empty($msg)? Core_Comm_Modret::getMsg($code) :$msg;
? ? ? ? return Core_Comm_Modret::getRetJson($code, $msg, $params,$callback);
? ? }
? ? /**
? ? * 輸出json對象 并exit
? ? */
? ? public static function exitJson( $code , $msg='' , $params=array(), $callback=NULL)
? ? {
? ? ? ? exit( self::returnJson($code, $msg, $params,$callback));
? ? }
? ? /**
? ? * iframe的ajax方式輸出json對象 主要是有script標簽
? ? */
? ? public static function iFrameExitJson( $code , $msg='' , $params=array(), $callback=NULL)
? ? {
? ? ? ? if(Core_Comm_Validator::checkCallback($callback))? ? ? //有回調函數才需要<script>標簽
? ? ? ? {
? ? ? ? ? ? exit('<script>'.self::returnJson($code,$msg , $params, $callback).'</script>');
? ? ? ? }
? ? ? ? else
? ? ? ? {
? ? ? ? ? ? exit( self::returnJson($code, $msg, $params));
? ? ? ? }
? ? }
? ? public static function showmsg ( $msg , $gourl=-1 , $time = null , $button=false )
? ? {
? ? ? ? if ($time === null)
? ? ? ? {
? ? ? ? ? ? $time = Core_Config::get ('showtime' , 'basic' , 3);
? ? ? ? }
? ? ? ? $time > 1000 && $time = intval ($time / 1000);
? ? ? ? $seogourl = $gourl;
? ? ? ? if ($gourl == -1)
? ? ? ? {
? ? ? ? ? ? $seogourl = $gourl = empty($_SERVER['HTTP_REFERER'])?self::getUrlroot():$_SERVER['HTTP_REFERER'];
? ? ? ? ? ? //? ? ? ? ? ? $seogourl = $gourl = 'javascript:history.go(-1)';
? ? ? ? }
? ? ? ? elseif (!preg_match ('#^(https?|ftp)://#' , $gourl) && !preg_match ('/^javascript/' , $gourl))
? ? ? ? {
? ? ? ? ? ? $seogourl = Core_Template::seoChange ($gourl);
? ? ? ? ? ? if(!preg_match('/^\//', $gourl))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? $gourl = '/' . $gourl;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? if (!$time)
? ? ? ? { #立即跳轉
? ? ? ? ? ? if (!preg_match ('/^javascript/' , $gourl))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? header ('Location: ' . $seogourl);
? ? ? ? ? ? ? ? echo $meta = "<meta http-equiv=\"refresh\" content=\"{$time};url={$seogourl}\" />";
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? echo "<script>" ,? str_replace("javascript:","",$seogourl)? , "</script>";
? ? ? ? ? ? }
? ? ? ? } else {
? ? ? ? ? ? #延遲跳轉
? ? ? ? ? ? Core_Template::assignvar('gourl' , $gourl);
? ? ? ? ? ? Core_Template::assignvar('seogourl' , $seogourl);
? ? ? ? ? ? Core_Template::assignvar('__msg' , $msg);
? ? ? ? ? ? Core_Template::assignvar('msg' , $msg);
? ? ? ? ? ? Core_Template::assignvar('time' , $time);
? ? ? ? ? ? Core_Template::assignvar('button' , $button);
? ? ? ? ? ? Core_Template::assignvar('_resource',? Core_Config::get('resource_path','basic','/'));
? ? ? ? ? ? Core_Template::assignvar('site_url', Core_Config::get('site_url','basic',false));
? ? ? ? ? ? if (Core_Controller_Front::getInstance()->getModelName () == 'index')
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Core_Template::render ('index/showmsg.tpl');
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? //exit("need showmsg templates");
? ? ? ? ? ? ? ? Core_Template::render ('member/showmsg.tpl');
? ? ? ? ? ? }
? ? ? ? }
//? ? ? ? exit;
? ? }
? ? public static $data = null;
? ? /**
? ? * 獲得webroot
? ? * 以 / 結尾
? ? * @return string
? ? * @author Icehu
? ? */
? ? public static function getWebroot()
? ? {
? ? ? ? return Core_Controller_Front::getWebRoot();
? ? }
? ? /**
? ? * 獲取前端用的Url根目錄
? ? * 開啟SEO 返回 /
? ? * 否則 返回 /index.php/
? ? *
? ? * @return string
? ? * @author Icehu
? ? */
? ? public static function getPathroot()
? ? {
? ? ? ? return self::getUrlroot()? . self::getPathinfoPre();
? ? }
? ? /**
? ? * 獲得Urlroot
? ? * @return string
? ? * @author Icehu
? ? */
? ? public static function getUrlroot()
? ? {
? ? ? ? $webroot = self::getWebroot();
? ? ? ? $http = $_SERVER['SERVER_PORT'] == 443 ? 'https' : 'http';
? ? ? ? return $http . '://' . $_SERVER['HTTP_HOST'] . $webroot;
? ? }
? ? /**
? ? * 獲得用戶地址
? ? * @return string
? ? * @author echoyang
? ? */
? ? public static function getUserurl($uname='')
? ? {
? ? ? ? $usertUrl= self::getUrlroot();
? ? ? ? if($uname && Core_Comm_Validator::isUserAccount($uname))
? ? ? ? {
? ? ? ? ? ? $http = $_SERVER['SERVER_PORT'] == 443 ? 'https' : 'http';
? ? ? ? ? ? $port = ($_SERVER["SERVER_PORT"] == 80)? '':':80';
? ? ? ? ? ? $usertUrl = $http . '://' . $_SERVER['HTTP_HOST'] .$port. self::seoChange('u/'.$uname);
? ? ? ? }
? ? ? ? return $usertUrl;
? ? }
? ? /**
? ? * 獲得 參數是$para的模塊$preUrl下的 url,用于前臺分頁pathinfo數據生成
? ? *@$preUrl = /model/controllor/action
? ? *@$para = array('key1'=>$value1,'key2'=>$value2)
? ? * @return string
? ? * @author echoyang
? ? */
? ? public static function getParaUrl($preUrl,$para = array())
? ? {
? ? ? ? $returnUrl = '';
? ? ? ? if(!empty($preUrl))
? ? ? ? {
? ? ? ? ? ? if(is_array($para) && $para)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? $returnUrl = $preUrl;
? ? ? ? ? ? ? ? foreach($para AS $k=>$v)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? if($k &&$v)
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? if(strpos($k,'__')===0)//去掉__開頭的私有變量
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? ? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? ? ? ? ? ? ? $returnUrl .= '/'.$k.'/'.$v;
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return $returnUrl;
? ? }
? ? /**
? ? * 獲得用戶地址以及簡短url
? ? * @return array('origin','short')
? ? * @author echoyang
? ? */
? ? public static function getUserShorturl($uname='', $formatLength=0,$endStr='' )
? ? {
? ? ? ? empty($endStr) && $endStr = '...'; //末尾添加str
? ? ? ? $formatLength = intval($formatLength);
? ? ? ? if(empty($formatLength))
? ? ? ? {
? ? ? ? ? ? $formatLength = 26;? //長度默認取26
? ? ? ? }
? ? ? ? $userShortUrl = $usertUrl = self::getUserurl($uname);
? ? ? ? if( strlen($usertUrl) > $formatLength )
? ? ? ? {
? ? ? ? ? ? $userShortUrl =? substr($usertUrl, 0 ,$formatLength);
? ? ? ? ? ? $userShortUrl .= $endStr;
? ? ? ? }
? ? ? ? return array('origin'=>$usertUrl,'short'=>$userShortUrl);
? ? }
? ? /**
? ? * seo轉換方法
? ? * @param string $str
? ? * @return string
? ? * @author Icehu
? ? */
? ? public static function seoChange($str)
? ? {
? ? ? ? return Core_Template::seoChange($str);
? ? }
? ? public static function escape_special_chars($string)
? ? {
? ? ? ? if (!is_array($string))
? ? ? ? {
? ? ? ? ? ? $string = preg_replace('!&(#?\w+);!', '%%%TTT_START%%%\\1%%%TTT_END%%%', $string);
? ? ? ? ? ? $string = htmlspecialchars($string);
? ? ? ? ? ? $string = str_replace(array('%%%TTT_START%%%', '%%%TTT_END%%%'), array('&', ';'), $string);
? ? ? ? }
? ? ? ? return $string;
? ? }
? ? /**
? ? * 檢查文件是否可讀
? ? * @param string $filename
? ? * @return bool
? ? * @author Icehu
? ? */
? ? public static function isReadable($filename)
? ? {
? ? ? ? if (!$fh = @fopen($filename, 'r', true))
? ? ? ? {
? ? ? ? ? ? return false;
? ? ? ? }
? ? ? ? @fclose($fh);
? ? ? ? return true;
? ? }
? ? /**
? ? * 獲取pathinfo前綴,當nginx無配置時,使用'?',否則使用默認值'/'
? ? * @return string
? ? * @author maynardliu
? ? */
? ? public static function getPathinfoPre()
? ? {
? ? ? ? static $pathinfopre;
? ? ? ? if(isset($pathinfopre))
? ? ? ? {
? ? ? ? ? ? return $pathinfopre;
? ? ? ? }
? ? ? ? else
? ? ? ? {
? ? ? ? ? ? $pathinfopre = "/";
? ? ? ? ? ? if(false!==strpos($_SERVER['SERVER_SOFTWARE'],"nginx"))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if(!isset($_SERVER['ORIG_PATH_INFO']))
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? $pathinfopre = "?";
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? if(false!==strpos($_SERVER['SERVER_SOFTWARE'],"Microsoft-IIS"))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? $pathinfopre = "/";
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return $pathinfopre;
? ? }
? ? /**
? ? * 經過重寫的SESSION_START
? ? *
? ? * @author Icehu
? ? */
? ? public static function session_start()
? ? {
? ? ? ? if (!defined('SESSION_START'))
? ? ? ? {
? ? ? ? ? ? $cookie_pre = Core_Config::get('cookiepre', 'basic', 't_');
? ? ? ? ? ? $domain = Core_Config::get('cookiedomain', 'basic', null);
? ? ? ? ? ? ini_set('session.name', $cookie_pre . 'skey');
? ? ? ? ? ? session_set_cookie_params(0, '/', $domain, $_SERVER['SERVER_PORT'] == 443 ? 1 : 0, true);
? ? ? ? ? ? session_set_save_handler(
? ? ? ? ? ? ? ? array('Core_Lib_Session', 'open'),
? ? ? ? ? ? ? ? array('Core_Lib_Session', 'close'),
? ? ? ? ? ? ? ? array('Core_Lib_Session', 'read'),
? ? ? ? ? ? ? ? array('Core_Lib_Session', 'write'),
? ? ? ? ? ? ? ? array('Core_Lib_Session', 'destroy'),
? ? ? ? ? ? ? ? array('Core_Lib_Session', 'gc')
? ? ? ? ? ? );
? ? ? ? ? ? $cookieTime = Core_Config::get('cookietime', 'basic', 30);
? ? ? ? ? ? session_cache_expire($cookieTime > 0 ? $cookieTime : 30);
? ? ? ? ? ? session_start();
? ? ? ? ? ? define('SESSION_START', true);
? ? ? ? }
? ? }
? ? /**
? ? * UTF-8數據的中文截字
? ? *
? ? * @param string $content 需要截字的原文
? ? * @param number $length 截取的長度
? ? * @param string $add 末尾添加的字符串
? ? * @return string
? ? * @author Icehu
? ? */
? ? public static function cn_substr($content, $length, $add='')
? ? {
? ? ? ? if ($length && strlen($content) > $length)
? ? ? ? {
? ? ? ? ? ? $str = substr($content, 0, $length);
? ? ? ? ? ? $len = strlen($str);
? ? ? ? ? ? for ($i = strlen($str) - 1; $i >= 0; $i-=1)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? $hex .= ' ' . ord($str[$i]);
? ? ? ? ? ? ? ? $ch = ord($str[$i]);
? ? ? ? ? ? ? ? if (($ch & 128) == 0)
? ? ? ? ? ? ? ? ? ? return substr($str, 0, $i) . $add;
? ? ? ? ? ? ? ? if (($ch & 192) == 192)
? ? ? ? ? ? ? ? ? ? return substr($str, 0, $i) . $add;
? ? ? ? ? ? }
? ? ? ? ? ? return($str . $hex . $add);
? ? ? ? }
? ? ? ? return $content;
? ? }
? ? /**
? ? * 按照命名規則載入類
? ? *
? ? * @param string $class
? ? * @return Class
? ? * @author
? ? */
? ? public static function loadClass($class)
? ? {
? ? ? ? if (class_exists($class, false))
? ? ? ? {
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? $file = str_replace('_', DIRECTORY_SEPARATOR, $class) . '.php';
? ? ? ? //include_once($file);
? ? ? ? require_once(INCLUDE_PATH . $file);
? ? }
? ? /**
? ? * 特殊加密url,防止地址被解析不完全
? ? *
? ? * @param string $class
? ? * @return string
? ? * @author
? ? */
? ? public static function iurlencode($key)
? ? {
? ? ? ? if(preg_match('/^apache/i', $_SERVER['SERVER_SOFTWARE']))
? ? ? ? {
? ? ? ? ? ? return rawurlencode(str_replace(array('/', '?', '&', '#'), array('%2F', '%3F', '%26', '%23'), $key));
? ? ? ? }
? ? ? ? return $key;
? ? }
? ? /**
? ? * 特殊url解密,防止地址不是原地址
? ? *
? ? * @param string $class
? ? * @return string
? ? * @author icehu
? ? */
? ? public static function iurldecode($key)
? ? {
? ? ? ? $key = rawurldecode($key);
? ? ? ? if(preg_match('/^apache/i', $_SERVER['SERVER_SOFTWARE']))
? ? ? ? {
? ? ? ? ? ? //Apache 會自動解碼一次
? ? ? ? ? ? return str_replace(array('%2F') , array('/') , $key);
? ? ? ? }
? ? ? ? else
? ? ? ? {
? ? ? ? ? ? return str_replace(array('%2F', '%3F', '%26', '%23'), array('/', '?', '&', '#'), $key);
? ? ? ? }
? ? }
? ? /**
? ? * 格式化字節
? ? * @param $size - 大小(字節)
? ? * @return 返回格式化后的文本
? ? * @author Icehu
? ? */
? ? public static function formatBytes($size)
? ? {
? ? ? ? if ($size >= 1073741824)
? ? ? ? {
? ? ? ? ? ? $size = round($size / 1073741824 * 100) / 100 . ' GB';
? ? ? ? }
? ? ? ? elseif ($size >= 1048576)
? ? ? ? {
? ? ? ? ? ? $size = round($size / 1048576 * 100) / 100 . ' MB';
? ? ? ? }
? ? ? ? elseif ($size >= 1024)
? ? ? ? {
? ? ? ? ? ? $size = round($size / 1024 * 100) / 100 . ' KB';
? ? ? ? }
? ? ? ? else
? ? ? ? {
? ? ? ? ? ? $size = $size . ' Bytes';
? ? ? ? }
? ? ? ? return $size;
? ? }
? ? /**
? ? * 插件鉤子調用方法
? ? * @param string $hackName
? ? * @return string
? ? */
? ? public static function pluginHack($hackName)
? ? {
? ? ? ? $returnContents = '';
? ? ? ? $pluginModel = new Model_Mb_Plugin();
? ? ? ? $pluginList = $pluginModel->getPluginList(3);
? ? ? ? foreach($pluginList as $plugin)
? ? ? ? {
? ? ? ? ? ? $call = array('Plugin_'.ucfirst($plugin['foldername']).'_Index', $hackName.'Hack');
? ? ? ? ? ? if (class_exists('Plugin_'.ucfirst($plugin['foldername']).'_Index'))
? ? ? ? ? ? ? ? if(is_callable($call))
? ? ? ? ? ? ? ? ? ? $returnContents .= call_user_func($call);
? ? ? ? }
? ? ? ? return $returnContents;
? ? }
? ? public static function timeMark($tag)
? ? {
? ? ? ? $apiName = NULL;
? ? ? ? static $timeArr=null;
? ? ? ? if(isset($timeArr[$apiName]))
? ? ? ? {
? ? ? ? ? ? $timeArr[$apiName]['et']=self::microtime();
? ? ? ? ? ? $costTime = $timeArr[$apiName]['et'] - $timeArr[$apiName]['bt'];
? ? ? ? ? ? unset($timeArr[$apiName]);
? ? ? ? ? ? return sprintf("%d", $costTime*1000);
? ? ? ? }
? ? ? ? else
? ? ? ? {
? ? ? ? ? ? $timeArr[$apiName]['bt']=self::microtime();
? ? ? ? }
? ? }
? ? public static function apiLog($apiName, $apiReponse)
? ? {
? ? ? ? if(defined("API_LOG_LEVEL"))
? ? ? ? {
? ? ? ? ? ? if(in_array(API_LOG_LEVEL,array(1,2)))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? $costTime = self::timeMark($apiName);
? ? ? ? ? ? }
? ? ? ? ? ? if($costTime)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if(!is_array($apiReponse))
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? $apiReponse = array('ret'=>4,'msg'=>'api call failed','errcode'=>'-1');
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? $ret = $apiReponse['ret'];
? ? ? ? ? ? ? ? $msg= $apiReponse['msg'];
? ? ? ? ? ? ? ? $errCode= $apiReponse['errcode'];
? ? ? ? ? ? ? ? switch(API_LOG_LEVEL)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? case 1:
? ? ? ? ? ? ? ? ? ? self::apiLogLocal($apiName, $ret, $msg, $errCode, $costTime);
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case 2:
? ? ? ? ? ? ? ? ? ? self::apiLogBoss($apiName, $ret, $msg, $errCode, $costTime);
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? default:
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? public static function apiLogLocal($apiName,$ret=0,$msg="ok",$errCode="0",$costTime)
? ? {
? ? ? ? static $pageBegin=true;
? ? ? ? $fileName = 'apilog';
? ? ? ? $logTime = date("Y-m-d H:i:s");
? ? ? ? $logData = array_values(func_get_args());
? ? ? ? array_unshift($logData,$logTime);
? ? ? ? $logData = implode("#",$logData);
? ? ? ? if($pageBegin)
? ? ? ? {
? ? ? ? ? ? Core_Util_Log::file($fileName,"\r\n".$_SERVER['REQUEST_URI']);
? ? ? ? ? ? $pageBegin=false;
? ? ? ? }
? ? ? ? Core_Util_Log::file($fileName,$logData);
? ? }
? ? public static function apiLogBoss($apiName, $ret=0, $msg="ok", $errCode="0", $costTime)
? ? {
? ? ? ? $ip? ? =? self::ip();
? ? ? ? $qq? ? =? 12345;
? ? ? ? $biz? ? =? "weibo.open.iweibo";
? ? ? ? $op? ? =? "call_api";
? ? ? ? $status =? $ret;
? ? ? ? $logid? =? 1160;
? ? ? ? $flowid =? $errCode;
? ? ? ? $appKey =? Core_Config::get('appkey', 'basic');
? ? ? ? $serverIp=? $_SERVER['SERVER_ADDR'];
? ? ? ? $value1=0;
? ? ? ? $value2=0;
? ? ? ? $value3=substr(Core_Util_Log::$apiRequstUrl,0,254);
? ? ? ? $value4=substr(Core_Util_Log::$apiRequstUrl,254,503);
? ? ? ? //$ret = Core_Util_Log::bossLog("%s,%u,%s,%s,%d,%u,%u,%s,%d,%s,%d,%d,%s,%s,%d,%d,%s,%s",$ip,$qq,$biz,$op,$status,$logid,$flowid, $apiName, $ret,$msg,$errCode,$costTime,$appKey,$serverIp,$value1,$value2,$value3,$value4);
? ? ? ? //$ret = Core_Util_Log::bossLog("%s,%d,%s,%s,%d,%d,%d,%s,%s,%d,%s,%d,%d,%s,%s,%d,%d,%s,%s",$ip,$qq,$biz,$op,$status,$logid,$flowid, $apiName, $ret,$msg,$errCode,$costTime,$appKey,$serverIp,$value1,$value2,$value3,$value4);
? ? ? ? $ret = Core_Util_Log::httpBossLog("%s,%u,%s,%s,%d,%u,%u,%s,%d,%s,%d,%d,%s,%s,%d,%d,%s,%s",$ip,$qq,$biz,$op,$status,$logid,$flowid, $apiName, $ret,$msg,$errCode,$costTime,$appKey,$serverIp,$value1,$value2,$value3,$value4);
? ? ? ? return ;
? ? }
? ? /*
? ? *@param $arg1 string
? ? *@param $arg2 string
? ? */
? ? function formatToFuncName()
? ? {
? ? ? ? $args = func_get_args();
? ? ? ? $args = array_filter($args,create_function('$v','return !empty($v);'));
? ? ? ? $ret = strtolower(array_shift($args));
? ? ? ? $ret .=? array_reduce($args,create_function('$v,$w','return $v . ucfirst(strtolower($w));'));
? ? ? ? return $ret;
? ? }
}
/**
* Core_Excetpion 異常重寫
* 這兩個異常放到Fun 文件中,不符合命名規范。
* 但是Fun全局載入,這是為了減少文件數量,提高性能。
*
* @author Icehu
*/
class Core_Exception extends Exception
{
? ? public $params = array();
? ? public function __construct($msg, $code, $params=array())
? ? {
? ? ? ? parent::__construct($msg, $code);
? ? ? ? $this->params = (array) $params;
? ? }
? ? public function getParams()
? ? {
? ? ? ? return $this->params;
? ? }
}
/**
* Core_Excetpion 異常重寫
* open_client異常
* @author echoyang
*/
class Core_Api_Exception extends Core_Exception
{
? ? public $params = array();
? ? public function __construct($msg, $code, $params=array())
? ? {
? ? ? ? parent::__construct($msg, $code, $params);
? ? }
}
class Core_Db_Exception extends Core_Exception
{
}
class Error_Display
{
? ? public static function show($exception)
? ? {
? ? ? ? $exceptionType = get_class($exception);
? ? ? ? $class= $exceptionType."_Error_Show";
? ? ? ? if(!class_exists($class, false))
? ? ? ? {
? ? ? ? ? ? $class= "Exception_Error_Show";
? ? ? ? }
? ? ? ? $errorShow = new $class($exception);
? ? ? ? $errorShow->dispatchShow();
? ? }
}
class Exception_Error_Show
{
? ? static $e;
? ? function __construct($e)
? ? {
? ? ? ? self::$e = $e;
? ? }
? ? public function dispatchShow()
? ? {
? ? ? ? $front = Core_Controller_Front::getInstance();
? ? ? ? $inajax=$front->inAjax() ? "inajax" : "";
? ? ? ? $rundebug = Core_Config::get('rundebug','basic',false) ? "debug" : "";
? ? ? ? $showfunc = Core_Fun::formatToFuncName($rundebug,$inajax,"show");
? ? ? ? $this->$showfunc();
? ? }
? ? protected function roughShow()
? ? {
? ? ? ? header('Content-Type: text/html; charset=utf-8');
? ? ? ? echo '<pre>';
? ? ? ? var_dump(self::$e);
? ? }
? ? protected function show()
? ? {
? ? ? ? Core_Fun::showmsg('系統繁忙', -1 );
? ? }
? ? protected function inajaxShow()
? ? {
? ? ? ? Core_Fun::exitJson(self::$e->getCode(), self::$e->getMessage());
? ? }
? ? protected function debugShow()
? ? {
? ? ? ? self::roughShow();
? ? }
? ? protected function debugInajaxShow()
? ? {
? ? ? ? Core_Fun::exitJson(self::$e->getCode(), self::$e->getMessage(), self::$e->getParams());
? ? }
}
class Core_Db_Exception_Error_Show extends Exception_Error_Show
{
? ? protected function inajaxShow()
? ? {
? ? ? ? Core_Fun::exitJson(self::$e->getCode(), '系統繁忙' , self::$e->getParams());
? ? }
}
class Core_Exception_Error_Show extends Exception_Error_Show
{
? ? public function dispatchShow()
? ? {
? ? ? ? $front = Core_Controller_Front::getInstance();
? ? ? ? $inajax=$front->inAjax() ? "inajax" : "";
? ? ? ? $rundebug = Core_Config::get('rundebug','basic',false) ? "debug" : "";
? ? ? ? if($rundebug)
? ? ? ? {
? ? ? ? ? ? $showfunc = Core_Fun::formatToFuncName($rundebug,$inajax,"show");
? ? ? ? ? ? $this->$showfunc();
? ? ? ? }
? ? ? ? else
? ? ? ? {
? ? ? ? ? ? $code = self::$e->getCode();
? ? ? ? ? ? $msg = self::$e->getMessage();
? ? ? ? ? ? if($code == 404)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? header('HTTP/1.1 404 Not Found');
? ? ? ? ? ? ? ? exit;
? ? ? ? ? ? }
? ? ? ? ? ? if($code != 0)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? if($inajax)
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? self::inajaxShow();
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? if($code > 0 )
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? self::show('系統繁忙', -1 );
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? self::show($msg, -1 );
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? protected function show($msg,$code)
? ? {
? ? ? ? Core_Fun::showmsg($msg,$code);
? ? }
? ? protected function inajaxShow()
? ? {
? ? ? ? Core_Fun::exitJson($code, '系統繁忙' , $msg);
? ? }
}
class Core_Api_Exception_Error_Show extends Exception_Error_Show
{
? ? protected function show()
? ? {
? ? ? ? $code = self::$e->getCode();
? ? ? ? $msg = self::$e->getMessage();
? ? ? ? if(isset($code) && !empty($code))
? ? ? ? {
? ? ? ? ? ? $msg .= ' (errcode='.$code.')。';
? ? ? ? }
? ? ? ? Core_Fun::showmsg($msg, -1 );
? ? ? ? exit();
? ? }
}