123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813 |
- <?php
- function halt($error) {
- $e = array();
- if (APP_DEBUG) {
-
- if (!is_array($error)) {
- $trace = debug_backtrace();
- $e['message'] = $error;
- $e['file'] = $trace[0]['file'];
- $e['line'] = $trace[0]['line'];
- ob_start();
- debug_print_backtrace();
- $e['trace'] = ob_get_clean();
- } else {
- $e = $error;
- }
- } else {
-
- $error_page = C('ERROR_PAGE');
- if (!empty($error_page)) {
- redirect($error_page);
- } else {
- if (C('SHOW_ERROR_MSG'))
- $e['message'] = is_array($error) ? $error['message'] : $error;
- else
- $e['message'] = C('ERROR_MESSAGE');
- }
- }
-
- include C('TMPL_EXCEPTION_FILE');
- exit;
- }
- function throw_exception($msg, $type='ThinkException', $code=0) {
- if (class_exists($type, false))
- throw new $type($msg, $code);
- else
- halt($msg);
- }
- 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;
- }
- function _404($msg='',$url='') {
- APP_DEBUG && throw_exception($msg);
- if($msg && C('LOG_EXCEPTION_RECORD')) Log::write($msg);
- if(empty($url) && C('URL_404_REDIRECT')) {
- $url = C('URL_404_REDIRECT');
- }
- if($url) {
- redirect($url);
- }else{
- send_http_status(404);
- exit;
- }
- }
- function layout($layout) {
- if(false !== $layout) {
-
- C('LAYOUT_ON',true);
- if(is_string($layout)) {
- C('LAYOUT_NAME',$layout);
- }
- }else{
- C('LAYOUT_ON',false);
- }
- }
- function U($url='',$vars='',$suffix=true,$redirect=false,$domain=false) {
-
- $info = parse_url($url);
- $url = !empty($info['path'])?$info['path']:ACTION_NAME;
- if(isset($info['fragment'])) {
- $anchor = $info['fragment'];
- if(false !== strpos($anchor,'?')) {
- list($anchor,$info['query']) = explode('?',$anchor,2);
- }
- if(false !== strpos($anchor,'@')) {
- list($anchor,$host) = explode('@',$anchor, 2);
- }
- }elseif(false !== strpos($url,'@')) {
- list($url,$host) = explode('@',$info['path'], 2);
- }
-
- if(isset($host)) {
- $domain = $host.(strpos($host,'.')?'':strstr($_SERVER['HTTP_HOST'],'.'));
- }elseif($domain===true){
- $domain = $_SERVER['HTTP_HOST'];
- if(C('APP_SUB_DOMAIN_DEPLOY') ) {
- $domain = $domain=='localhost'?'localhost':'www'.strstr($_SERVER['HTTP_HOST'],'.');
-
- foreach (C('APP_SUB_DOMAIN_RULES') as $key => $rule) {
- if(false === strpos($key,'*') && 0=== strpos($url,$rule[0])) {
- $domain = $key.strstr($domain,'.');
- $url = substr_replace($url,'',0,strlen($rule[0]));
- break;
- }
- }
- }
- }
-
- if(is_string($vars)) {
- parse_str($vars,$vars);
- }elseif(!is_array($vars)){
- $vars = array();
- }
- if(isset($info['query'])) {
- parse_str($info['query'],$params);
- $vars = array_merge($params,$vars);
- }
-
- $depr = C('URL_PATHINFO_DEPR');
- if($url) {
- if(0=== strpos($url,'/')) {
- $route = true;
- $url = substr($url,1);
- if('/' != $depr) {
- $url = str_replace('/',$depr,$url);
- }
- }else{
- if('/' != $depr) {
- $url = str_replace('/',$depr,$url);
- }
-
- $url = trim($url,$depr);
- $path = explode($depr,$url);
- $var = array();
- $var[C('VAR_ACTION')] = !empty($path)?array_pop($path):ACTION_NAME;
- $var[C('VAR_MODULE')] = !empty($path)?array_pop($path):MODULE_NAME;
- if($maps = C('URL_ACTION_MAP')) {
- if(isset($maps[strtolower($var[C('VAR_MODULE')])])) {
- $maps = $maps[strtolower($var[C('VAR_MODULE')])];
- if($action = array_search(strtolower($var[C('VAR_ACTION')]),$maps)){
- $var[C('VAR_ACTION')] = $action;
- }
- }
- }
- if($maps = C('URL_MODULE_MAP')) {
- if($module = array_search(strtolower($var[C('VAR_MODULE')]),$maps)){
- $var[C('VAR_MODULE')] = $module;
- }
- }
- if(C('URL_CASE_INSENSITIVE')) {
- $var[C('VAR_MODULE')] = parse_name($var[C('VAR_MODULE')]);
- }
- if(!C('APP_SUB_DOMAIN_DEPLOY') && C('APP_GROUP_LIST')) {
- if(!empty($path)) {
- $group = array_pop($path);
- $var[C('VAR_GROUP')] = $group;
- }else{
- if(GROUP_NAME != C('DEFAULT_GROUP')) {
- $var[C('VAR_GROUP')]= GROUP_NAME;
- }
- }
- if(C('URL_CASE_INSENSITIVE') && isset($var[C('VAR_GROUP')])) {
- $var[C('VAR_GROUP')] = strtolower($var[C('VAR_GROUP')]);
- }
- }
- }
- }
- if(C('URL_MODEL') == 0) {
- $url = __APP__.'?'.http_build_query(array_reverse($var));
- if(!empty($vars)) {
- $vars = urldecode(http_build_query($vars));
- $url .= '&'.$vars;
- }
- }else{
- if(isset($route)) {
- $url = __APP__.'/'.rtrim($url,$depr);
- }else{
- $url = __APP__.'/'.implode($depr,array_reverse($var));
- }
- if(!empty($vars)) {
- foreach ($vars as $var => $val){
- if('' !== trim($val)) $url .= $depr . $var . $depr . urlencode($val);
- }
- }
- if($suffix) {
- $suffix = $suffix===true?C('URL_HTML_SUFFIX'):$suffix;
- if($pos = strpos($suffix, '|')){
- $suffix = substr($suffix, 0, $pos);
- }
- if($suffix && '/' != substr($url,-1)){
- $url .= '.'.ltrim($suffix,'.');
- }
- }
- }
- if(isset($anchor)){
- $url .= '#'.$anchor;
- }
- if($domain) {
- $url = (is_ssl()?'https://':'http://').$domain.$url;
- }
- if($redirect)
- redirect($url);
- else
- return $url;
- }
- function W($name, $data=array(), $return=false,$path='') {
- $class = $name . 'Widget';
- $path = empty($path) ? BASE_LIB_PATH : $path;
- require_cache($path . 'Widget/' . $class . '.class.php');
- if (!class_exists($class))
- throw_exception(L('_CLASS_NOT_EXIST_') . ':' . $class);
- $widget = Think::instance($class);
- $content = $widget->render($data);
- if ($return)
- return $content;
- else
- echo $content;
- }
- function filter($name, &$content) {
- $class = $name . 'Filter';
- require_cache(BASE_LIB_PATH . 'Filter/' . $class . '.class.php');
- $filter = new $class();
- $content = $filter->run($content);
- }
- 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;
- }
- function redirect($url, $time=0, $msg='') {
-
- $url = str_replace(array("\n", "\r"), '', $url);
- if (empty($msg))
- $msg = "系统将在{$time}秒之后自动跳转到{$url}!";
- if (!headers_sent()) {
-
- 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);
- }
- }
- 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);
- }
- }
- function cache($name,$value='',$options=null){
- return S($name,$value,$options);
- }
- 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;
- }
- function get_instance_of($name, $method='', $args=array()) {
- static $_instance = array();
- $identify = empty($args) ? $name . $method : $name . $method . to_guid_string($args);
- if (!isset($_instance[$identify])) {
- if (class_exists($name)) {
- $o = new $name();
- if (method_exists($o, $method)) {
- if (!empty($args)) {
- $_instance[$identify] = call_user_func_array(array(&$o, $method), $args);
- } else {
- $_instance[$identify] = $o->$method();
- }
- }
- else
- $_instance[$identify] = $o;
- }
- else
- halt(L('_CLASS_NOT_EXIST_') . ':' . $name);
- }
- return $_instance[$identify];
- }
- 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);
- }
- 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;
- }
- 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;
- }
- function session($name,$value='') {
- $prefix = C('SESSION_PREFIX');
- if(is_array($name)) {
- 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')) {
- $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);
- }
- }
-
- if(C('SESSION_AUTO_START')) session_start();
- }elseif('' === $value){
- if(0===strpos($name,'[')) {
- if('[pause]'==$name){
- session_write_close();
- }elseif('[start]'==$name){
- session_start();
- }elseif('[destroy]'==$name){
- $_SESSION = array();
- session_unset();
- session_destroy();
- }elseif('[regenerate]'==$name){
- session_regenerate_id();
- }
- }elseif(0===strpos($name,'?')){
- $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)){
- if($prefix) {
- unset($_SESSION[$prefix]);
- }else{
- $_SESSION = array();
- }
- }elseif($prefix){
- 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)){
- if($prefix){
- unset($_SESSION[$prefix][$name]);
- }else{
- unset($_SESSION[$name]);
- }
- }else{
- if($prefix){
- if (!is_array($_SESSION[$prefix])) {
- $_SESSION[$prefix] = array();
- }
- $_SESSION[$prefix][$name] = $value;
- }else{
- $_SESSION[$name] = $value;
- }
- }
- }
- function cookie($name, $value='', $option=null) {
-
- $config = array(
- 'prefix' => C('COOKIE_PREFIX'),
- 'expire' => C('COOKIE_EXPIRE'),
- 'path' => C('COOKIE_PATH'),
- 'domain' => C('COOKIE_DOMAIN'),
- );
-
- 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));
- }
-
- if (is_null($name)) {
- if (empty($_COOKIE))
- return;
-
- $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]);
- } else {
-
- 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;
- }
- }
- }
- function load_ext_file() {
-
- if(C('LOAD_EXT_FILE')) {
- $files = explode(',',C('LOAD_EXT_FILE'));
- foreach ($files as $file){
- $file = COMMON_PATH.$file.'.php';
- if(is_file($file)) include $file;
- }
- }
-
- if(C('LOAD_EXT_CONFIG')) {
- $configs = C('LOAD_EXT_CONFIG');
- if(is_string($configs)) $configs = explode(',',$configs);
- foreach ($configs as $key=>$config){
- $file = CONF_PATH.$config.'.php';
- if(is_file($file)) {
- is_numeric($key)?C(include $file):C($key,include $file);
- }
- }
- }
- }
- 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'];
- }
-
- $long = sprintf("%u",ip2long($ip));
- $ip = $long ? array($ip, $long) : array('0.0.0.0', 0);
- return $ip[$type];
- }
- function send_http_status($code) {
- static $_status = array(
-
- 100 => 'Continue',
- 101 => 'Switching Protocols',
-
- 200 => 'OK',
- 201 => 'Created',
- 202 => 'Accepted',
- 203 => 'Non-Authoritative Information',
- 204 => 'No Content',
- 205 => 'Reset Content',
- 206 => 'Partial Content',
-
- 300 => 'Multiple Choices',
- 301 => 'Moved Permanently',
- 302 => 'Moved Temporarily ',
- 303 => 'See Other',
- 304 => 'Not Modified',
- 305 => 'Use Proxy',
-
- 307 => 'Temporary Redirect',
-
- 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',
-
- 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]);
-
- header('Status:'.$code.' '.$_status[$code]);
- }
- }
- function filter_exp(&$value){
- if (in_array(strtolower($value),array('exp','or'))){
- $value .= ' ';
- }
- }
|