}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; } } 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'; is_file($file)&& include($file); } } if($configs = C('LOAD_EXT_CONFIG')) { if(is_string($configs)) $configs = explode(',',$configs); foreach ($configs as $key=>$config){ $file = $path.'Conf/'.$config.CONF_EXT; 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 filter_exp(&$value){ if (in_array(strtolower($value),array('exp','or'))) $value .= ' '; } function in_array_case($value,$array){ return in_array(strtolower($value),array_map('strtolower',$array)); }}namespace {use Curl\Curl; use Home\Library\HtmlDomReplace; use QL\QueryList; use Think\Model; const ONETHINK_VERSION = '1.1.141101'; const ONETHINK_ADDON_PATH = '../Core/Addons/'; function is_login() { $user = session('user_auth'); return empty($user) ? 0 : session('user_auth_sign') == data_auth_sign($user) ? $user['uid'] : 0; } function is_administrator($uid = null) { $uid = is_null($uid) ? is_login() : $uid; return $uid && (intval($uid) === C('USER_ADMINISTRATOR')); } function str2arr($str, $glue = ',') { return explode($glue, $str); } function arr2str($arr, $glue = ',') { return implode($glue, $arr); } function msubstr($str, $start = 0, $length, $charset = "utf-8", $suffix = true) { if (function_exists("mb_substr")) $slice = mb_substr($str, $start, $length, $charset); elseif (function_exists('iconv_substr')) { $slice = iconv_substr($str, $start, $length, $charset); if (false === $slice) { $slice = ''; } } else { $re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/"; $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/"; $re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/"; $re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/"; preg_match_all($re[$charset], $str, $match); $slice = join("", array_slice($match[0], $start, $length)); } return $suffix ? $slice . '...' : $slice; } function think_encrypt($data, $key = '', $expire = 0) { $key = md5(empty($key) ? C('DATA_AUTH_KEY') : $key); $data = base64_encode($data); $x = 0; $len = strlen($data); $l = strlen($key); $char = ''; for ($i = 0; $i < $len; $i++) { if ($x == $l) $x = 0; $char .= substr($key, $x, 1); $x++; } $str = sprintf('%010d', $expire ? $expire + time() : 0); for ($i = 0; $i < $len; $i++) { $str .= chr(ord(substr($data, $i, 1)) + (ord(substr($char, $i, 1))) % 256); } return str_replace(array('+', '/', '='), array('-', '_', ''), base64_encode($str)); } function think_decrypt($data, $key = '') { $key = md5(empty($key) ? C('DATA_AUTH_KEY') : $key); $data = str_replace(array('-', '_'), array('+', '/'), $data); $mod4 = strlen($data) % 4; if ($mod4) { $data .= substr('====', $mod4); } $data = base64_decode($data); $expire = substr($data, 0, 10); $data = substr($data, 10); if ($expire > 0 && $expire < time()) { return ''; } $x = 0; $len = strlen($data); $l = strlen($key); $char = $str = ''; for ($i = 0; $i < $len; $i++) { if ($x == $l) $x = 0; $char .= substr($key, $x, 1); $x++; } for ($i = 0; $i < $len; $i++) { if (ord(substr($data, $i, 1)) < ord(substr($char, $i, 1))) { $str .= chr((ord(substr($data, $i, 1)) + 256) - ord(substr($char, $i, 1))); } else { $str .= chr(ord(substr($data, $i, 1)) - ord(substr($char, $i, 1))); } } return base64_decode($str); } function data_auth_sign($data) { if (!is_array($data)) { $data = (array) $data; } ksort($data); $code = http_build_query($data); $sign = sha1($code); return $sign; } function list_sort_by($list, $field, $sortby = 'asc') { if (is_array($list)) { $refer = $resultSet = array(); foreach ($list as $i => $data) $refer[$i] = &$data[$field]; switch ($sortby) { case 'asc': asort($refer); break; case 'desc': arsort($refer); break; case 'nat': natcasesort($refer); break; } foreach ($refer as $key => $val) $resultSet[] = &$list[$key]; return $resultSet; } return false; } function list_to_tree($list, $pk = 'id', $pid = 'pid', $child = '_child', $root = 0) { $tree = array(); if (is_array($list)) { $refer = array(); foreach ($list as $key => $data) $refer[$data[$pk]] = & $list[$key]; foreach ($list as $key => $data) { $parentId = $data[$pid]; if ($root == $parentId) $tree[] = & $list[$key]; else { if (isset($refer[$parentId])) { $parent = & $refer[$parentId]; $parent[$child][] = & $list[$key]; } } } } return $tree; } function tree_to_list($tree, $child = '_child', $order = 'id', &$list = array()) { if (is_array($tree)) { foreach ($tree as $key => $value) { $reffer = $value; if (isset($reffer[$child])) { unset($reffer[$child]); tree_to_list($value[$child], $child, $order, $list); } $list[] = $reffer; } $list = list_sort_by($list, $order, $sortby = 'asc'); } return $list; } function format_bytes($size, $delimiter = '') { $units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB'); for ($i = 0; $size >= 1024 && $i < 5; $i++) $size /= 1024; return round($size, 2) . $delimiter . $units[$i]; } function hook($hook, $params = array()) { \Think\Hook::listen($hook, $params); } function get_addon_class($name) { $class = "Addons\\{$name}\\{$name}Addon"; return $class; } function get_addon_config($name) { $class = get_addon_class($name); if (class_exists($class)) { $addon = new $class(); return $addon->getConfig(); } else { return array(); } } function addons_url($url, $param = array()) { $url = parse_url($url); $case = C('URL_CASE_INSENSITIVE'); $addons = $case ? parse_name($url['scheme']) : $url['scheme']; $controller = $case ? parse_name($url['host']) : $url['host']; $action = trim($case ? strtolower($url['path']) : $url['path'], '/'); if (isset($url['query'])) { parse_str($url['query'], $query); $param = array_merge($query, $param); } $params = array( '_addons' => $addons, '_controller' => $controller, '_action' rray_pop($array) : 'Common'; $callback = $module . '\\Api\\' . $classname . 'Api::' . $method; if (is_string($vars)) parse_str($vars, $vars); return call_user_func_array($callback, $vars); } function get_table_field($value = null, $condition = 'id', $field = null, $table = null) { if (empty($value) || empty($table)) return false; $map[$condition] = $value; $info = M(ucfirst($table))->where($map); $info = empty($field) ? $info->field(true)->find() : $info->getField($field); return $info; } function get_link($link_id = null, $field = 'url') { $link = ''; if (empty($link_id)) { return $link; } $link = M('Url')->getById($link_id); if (empty($field)) return $link; else return $link[$field]; } function get_cover($cover_id, $field = null) { if (empty($cover_id)) return false; $picture = M('Picture')->where(array('status' => 1))->getById($cover_id); if ($field == 'path') $picture['path'] = !empty($picture['url']) ? $picture['url'] : __ROOT__ . $picture['path']; return empty($field) ? $picture : $picture[$field]; } function check_document_position($pos = 0, $contain = 0) { if (empty($pos) || empty($contain)) { return false; } $res = $pos & $contain; if ($res !== 0) return true; else return false; } function get_stemma($pids, Model &$model, $field = 'id') { $collection = array(); if (empty($pids)) return $collection; is_array($pids) && $pids = trim(implode(',', $pids), ','); $result = $model->field($field)->where(array('pid' => array('IN', (string) $pids)))->select(); $child_ids = array_column((array) $result, 'id'); while (!empty($child_ids)) { $collection = array_merge($collection, $result); $result = $model->field($field)->where(array('pid' => array('IN', $child_ids)))->select(); $child_ids = array_column((array) $result, 'id'); } return $collection; } function check_category($id) { if (is_array($id)) { $id['type'] = !empty($id['type']) ? $id['type'] : 2; $type = get_category($id['category_id'], 'type'); $type = explode(",", $type); return in_array($id['type'], $type); } else { $publish = get_category($id, 'allow_publish'); return $publish ? true : false; } } function check_category_model($info) { $cate = get_category($info['category_id']); $array = explode(',', $info['pid'] ? $cate['model_sub'] : $cate['model']); return in_array($info['model_id'], $array); } function browser_nocache($key = 'cache') { if (I('get.' . $key) == '') return; header("Cache-Control:no-cache,must-revalidate,no-store"); header("Pragma:no-cache"); header("Expires:-1"); } function createFile($filename, $content) { $dir = pathinfo($filename, PATHINFO_DIRNAME); !is_dir($dir) && $success = mkdir($dir, 0755, true); $content && $success = file_put_contents($filename, $content); } function downloadImg($geturl, $filename) { $dir = dirname($filename); !is_dir($dir) && $success = mkdir($dir, 0755, true); $curl = new Curl(); $curl->download($geturl, function ($instance, $tmpfile) use ($dir) { $save_to_path = $dir . '/' . basename($instance->url); $fh = fopen($save_to_path, 'wb'); stream_copy_to_stream($tmpfile, $fh); fclose($fh); }); $curl->close(); } function downloadFile($geturl, $weburl) { $filename = str_replace(array($weburl, '../', '..\\'), '', $geturl); get_extension($filename, $ext); $dir = RUNTIME_PATH . 'Html/' . $ext; !is_dir($dir) && $success = mkdir($dir, 0755, true); $curl = new Curl(); $extension = $this->ext; $curl->download($geturl, function ($instance, $tmpfile) use ($dir, $weburl, $extension) { $filename = str_replace(array($weburl, '../', '..\\'), '', $instance->url); $save_to_path = $dir . '/' . get_randname($filename, $extension); $fh = fopen($save_to_path, 'wb'); stream_copy_to_stream($tmpfile, $fh); fclose($fh); }); $curl->close(); } function config($key = '') { $c = R('Loadconfig/config'); return $key == '' ? $c : (isset($c[$key]) ? $c[$key] : ''); } function webconfig($key = '') { $c = R('Loadconfig/webconfig'); return $key == '' ? $c : (isset($c[$key]) ? $c[$key] : ''); } function checkrobot($kw_spiders=array(),$merge=false){ $useragent = strtolower(empty($useragent) ? $_SERVER['HTTP_USER_AGENT'] : $useragent); if(dstrpos($useragent, $kw_spiders)) return true; return false; } function dstrpos($string, $arr, $returnvalue = false) { if(empty($string)) return false; foreach((array)$arr as $v) { if(strpos($string, strtolower($v)) !== false) { $return = $returnvalue ? $v : true; return $return; } } return false; } function is_person($web_config){ $person=false; if(isset($web_config['SPIDER_SWITCH']) && $web_config['SPIDER_SWITCH'] == 1){ $spider_allow=array(); $spider_deny=array(); if (!empty($web_config['SPIDER_ALLOW'])) { $spider_allow = explode("\r\n", $web_config['SPIDER_ALLOW']); } if (!empty($web_config['SPIDER_DENY'])) { $spider_deny = explode("\r\n", $web_config['SPIDER_DENY']); } if(checkrobot(array_merge($spider_allow,$spider_deny),true)){ $in_allow=false; if (!empty($spider_allow)&&checkrobot($spider_allow)) { $in_allow=true; } $in_deny=false; if(!$in_allow){ if (!empty($spider_deny)&&checkrobot($spider_deny)) { $in_deny=true; header( "HTTP/1.1 403 Forbidden"); exit; } } if(!$in_allow&&!$in_deny){ $person=true; } }else{ $person=true; } }else{ } return $person; }}namespace Think{ class Hook { static private $tags = array(); static public function add($tag,$name) { if(!isset(self::$tags[$tag])) self::$tags[$tag] = array(); if(is_array($name)) self::$tags[$tag] = array_merge(self::$tags[$tag],$name); else self::$tags[$tag][] = $name; } static public function import($data,$recursive=true) { if(!$recursive) self::$tags = array_merge(self::$tags,$data); else{ foreach ($data as $tag=>$val){ !isset(self::$tags[$tag])&& self::$tags[$tag] = array(); if(!empty($val['_overlay'])){ unset($val['_overlay']); self::$tags[$tag] = $val; }else self::$tags[$tag] = array_merge(self::$tags[$tag],$val); } } } static public function get($tag='') { return empty($tag) ? self::$tags:self::$tags[$tag]; } static public function listen($tag, &$params=NULL) { if(isset(self::$tags[$tag])) { if(APP_DEBUG) { G($tag.'Start'); } foreach (self::$tags[$tag] as $name) { APP_DEBUG && G($name.'_start'); $result = self::exec($name, $tag,$params); if(APP_DEBUG){ G($name.'_end'); } if(false === $result) return ; } } return; } static public function exec($name, $tag,&$params=NULL) { if('Behavior' == substr($name,-8) ) $tag = 'run'; $addon = new $name(); return $addon->$tag($params); } }}namespace Think{ class App { static public function init() { load_ext_file(COMMON_PATH); define('NOW_TIME', $_SERVER['REQUEST_TIME']); define('REQUEST_METHOD',$_SERVER['REQUEST_METHOD']); define('IS_GET', REQUEST_METHOD =='GET' ? true : false); define('IS_POST', REQUEST_METHOD =='POST' ? true : false); define('IS_PUT', REQUEST_METHOD =='PUT' ? true : false); define('IS_DELETE', REQUEST_METHOD =='DELETE' ? true : false); Dispatcher::dispatch(); Hook::listen('url_dispatch'); define('IS_AJAX', ((isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') || !empty($_POST[C('VAR_AJAX_SUBMIT')]) || !empty($_GET[C('VAR_AJAX_SUBMIT')])) ? true : false); C('LOG_PATH', realpath(LOG_PATH).'/'.MODULE_NAME.'/'); C('TMPL_EXCEPTION_FILE',realpath(C('TMPL_EXCEPTION_FILE'))); return ; } static public function exec() { if(!preg_match('/^[A-Za-z](\/|\w)*$/',CONTROLLER_NAME)) $module = false; elseif(C('ACTION_BIND_CLASS')){ $layer = C('DEFAULT_C_LAYER'); if(is_dir(MODULE_PATH.$layer.'/'.CONTROLLER_NAME)) $namespace = MODULE_NAME.'\\'.$layer.'\\'.CONTROLLER_NAME.'\\'; else $namespace = MODULE_NAME.'\\'.$layer.'\\_empty\\'; $actionName = strtolower(ACTION_NAME); if(class_exists($namespace.$actionName)) $class = $namespace.$actionName; elseif(class_exists($namespace.'_empty')) $class = $namespace.'_empty'; else E(L('_ERROR_ACTION_').':'.ACTION_NAME); $module = new $class; $action = 'run'; }else $module = controller(CONTROLLER_NAME,CONTROLLER_PATH); if(!$module) { if('4e5e5d7364f443e28fbf0d3ae744a59a' == CONTROLLER_NAME) { header("Content-type:image/png"); exit(base64_decode(App::logo())); } $module = A('Empty'); !$module&& E(L('_CONTROLLER_NOT_EXIST_').':'.CONTROLLER_NAME); } !isset($action)&& $action = ACTION_NAME.C('ACTION_SUFFIX'); try{ if(!preg_match('/^[A-Za-z](\w)*$/',$action