}else{ $_SESSION[$name] = $value; } } } return null; } function cookie($name='', $value='', $option=null) { $config = array( 'prefix' => C('COOKIE_PREFIX'), 'expire' => C('COOKIE_EXPIRE'), 'path' => C('COOKIE_PATH'), 'domain' => C('COOKIE_DOMAIN'), 'secure' => C('COOKIE_SECURE'), 'httponly' => C('COOKIE_HTTPONLY'), ); 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(!empty($config['httponly'])){ ini_set("session.cookie_httponly", 1); } if (is_null($name)) { if (empty($_COOKIE)) return null; $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'],$config['secure'],$config['httponly']); unset($_COOKIE[$key]); } } } return null; }elseif('' === $name){ return $_COOKIE; } $name = $config['prefix'] . str_replace('.', '_', $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'],$config['secure'],$config['httponly']); 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'],$config['secure'],$config['httponly']); $_COOKIE[$name] = $value; } } return null; } function load_ext_file($path) { if($files = C('LOAD_EXT_FILE')) { $files = explode(',',$files); foreach ($files as $file){ $file = $path.'Common/'.$file.'.php'; if(is_file($file)) include $file; } } if($configs = C('LOAD_EXT_CONFIG')) { if(is_string($configs)) $configs = explode(',',$configs); foreach ($configs as $key=>$config){ $file = is_file($config)? $config : $path.'Conf/'.$config.CONF_EXT; if(is_file($file)) { is_numeric($key)?C(load_config($file)):C($key,load_config($file)); } } } } function get_client_ip($type = 0,$adv=false) { $type = $type ? 1 : 0; static $ip = NULL; if ($ip !== NULL) return $ip[$type]; if($adv){ 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']; } }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 think_filter(&$value){ if(preg_match('/^(EXP|NEQ|GT|EGT|LT|ELT|OR|XOR|LIKE|NOTLIKE|NOT BETWEEN|NOTBETWEEN|BETWEEN|NOTIN|NOT IN|IN)$/i',$value)){ $value .= ' '; } } function in_array_case($value,$array){ return in_array(strtolower($value),array_map('strtolower',$array)); }}namespace {require_once COMMON_PATH . "Common/extend.php"; function strexists($string, $find){ return !(strpos($string, $find) === false); } function htmlspecialchars_trans($string, $transway = 'encode'){ if (!verify_license()) { exit("this domain is not allowed!"); } if ($transway == "encode") { $newstring = str_replace(array("\r\n", "\r", "\n", " "), array("[line]", "[line]", "[line]", "[space]"), $string); } else { if ($transway == "decode") { $newstring = str_replace(array("[line]", "[space]"), array("\r\n", "&nbsp;"), htmlspecialchars($string)); } else { if ($transway == "pick") { $newstring = str_replace(array("&", "[space]"), array("&", " "), $string); } } } return $newstring; } function getRandChar($length){ $str = NULL; $strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz"; $max = strlen($strPol) - 1; $i = 0; while ($i < $length) { $str .= $strPol[rand(0, $max)]; $i = $i + 1; } return $str; } function encodekey($id){ return substr(md5("yg_" . $id), 5, 16); } function unique_array($array, $total, $unique = true){ $newArray = array(); if($unique){ $array = array_unique($array); } shuffle($array); $length = count($array); $i = 0; while ($i < $total) { if ($i < $length) { $newArray[] = $array[$i]; } $i = $i + 1; } return $newArray; } function is_HTTPS(){ if (!isset($_SERVER['HTTPS'])) { return false; } if ($_SERVER["HTTPS"] === 1) { return true; } if ($_SERVER["HTTPS"] === "on") { return true; } if ($_SERVER["SERVER_PORT"] == 443) { return true; } return false; } function g2u($str){ $charset = mb_detect_encoding($str, array("UTF-8", "GBK", "GB2312")); $charset = strtolower($charset); if ("cp936" == $charset) { $charset = "GBK"; } if ("utf-8" != $charset) { $str = iconv($charset, "UTF-8//IGNORE", $str); } return $str; } function toutf8($str){ $encode = mb_detect_encoding($str, array("ASCII", "UTF-8", "GB2312", "EUC-CN", "GBK", "BIG5")); if (in_array($encode, array("GB2312", "EUC-CN", "GBK"))) { $str = iconv($encode, "UTF-8", $str); } return $str; } function str_insert($str, $i, $substr){ $j = 0; while ($j < $i) { $startstr .= $str[$j]; $j = $j + 1; } $j = $i; while ($j < strlen($str)) { $laststr .= $str[$j]; $j = $j + 1; } $str = $startstr . $substr . $laststr; return $str; } function getip(){ if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown")) { $ip = getenv("HTTP_CLIENT_IP"); } else { if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")) { $ip = getenv("HTTP_X_FORWARDED_FOR"); } else { if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown")) { $ip = getenv("REMOTE_ADDR"); } else { if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER["REMOTE_ADDR"] && strcasecmp($_SERVER["REMOTE_ADDR"], "unknown")) { $ip = $_SERVER["REMOTE_ADDR"]; } else { $ip = "unknown"; } } } } return $ip; } function cleanHtml($str){ $str = trim($str); $str = preg_replace("/<(style.*?)>(.*?)<(\\/style.*?)>/si", "", $str); $str = preg_replace("/<(\\/?style.*?)>/si", "", $str); $str = preg_replace("/<(script.*?)>(.*?)<(\\/script.*?)>/si", "", $str); $str = preg_replace("/<(\\/?script.*?)>/si", "", $str); $str = strip_tags($str, ""); $str = str_replace("\t", "", $str); $str = str_replace("\r\n", "", $str); $str = str_replace("\r", "", $str); $str = str_replace("\n", "", $str); $str = str_replace(" ", "", $str); $str = str_replace(" ", "", $str); return trim($str); } function writefile($file, $str){ if (!is_dir(dirname($file))) { mkdir(dirname($file)