PHP - 常用函数库

<?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();

    }

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,569评论 4 363
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,499评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,271评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,087评论 0 209
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,474评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,670评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,911评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,636评论 0 202
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,397评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,607评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,093评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,418评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,074评论 3 237
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,092评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,865评论 0 196
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,726评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,627评论 2 270

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,100评论 18 139
  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,662评论 0 33
  • 主要积累一些开发中比较 常用的工具类,部分借鉴于网络,主要来源于平时开发因需求而生的小工具类 13、ArithUt...
    大鸭梨leepear阅读 651评论 0 1
  • /@type {Number}@const/NaN = 0;/@type {Number}/Infinity = ...
    SmallTwo阅读 724评论 0 0
  • 寒风乍起夜微凉, 草色渐黄欲披霜。 唯喜伊人归来笑, 缘是昨宵梦里伤。
    夏树野云阅读 148评论 0 0