<?php

//扩展名权限判断 有权限则返回1 不是true
function checkExt($file){
if($GLOBALS['isRoot']) return 1;
if (strstr($file,'<') || strstr($file,'>') || $file=='') {
return 0;
}

//'php|phtml|phtm|pwml|asp|aspx|ascx|jsp|pl|htaccess|shtml|shtm'
$notAllow = strtolower($GLOBALS['auth']['extNotAllow']);
$extArr = explode('|',$notAllow);
if(in_array('asp',$extArr)){
$extArr = array_merge($extArr,array('aspx','ascx','pwml'));
}
if(in_array('php',$extArr)){
$extArr = array_merge($extArr,array('phtml','phtm','htaccess','pwml'));
}
if(in_array('htm',$extArr) || in_array('html',$extArr)){
$extArr = array_merge($extArr,array('html','shtml','shtm','html'));
}
foreach ($extArr as $current) {
if ($current !== '' && stristr($file,'.'.$current)){//含有扩展名
return 0;
}
}
return 1;
}

//-----解压缩跨平台编码转换;自动识别编码-----
//压缩前,文件名处理;
//ACT=zip——压缩到当前
//ACT=zipDownload---打包下载[判断浏览器&UA——得到地区自动转换为目标编码];
function zip_pre_name($fileName,$toCharset=false){
if(get_path_this($fileName) == '.DS_Store') return '';//过滤文件
if (!function_exists('iconv')){
return $fileName;
}
$charset = $GLOBALS['config']['systemCharset'];
if($toCharset == false){//默认从客户端和浏览器自动识别
$toCharset = 'utf-8';
$clientLanugage = I18n::defaultLang();
$langType = I18n::getType();
if( client_is_windows() && (
$clientLanugage =='zh-CN' ||
$clientLanugage =='zh-TW' ||
$langType =='zh-CN' ||
$langType =='zh-TW' )
){
$toCharset = "gbk";//压缩或者打包下载压缩时文件名采用的编码
}
}

$result = iconv_to($fileName,$charset,$toCharset);
if(!$result){
$result = $fileName;
}
//write_log("zip:".$charset.'=>'.$toCharset.';'.$fileName.'=>'.$result,'zip');
return $result;
}

function unzip_filter_ext($name){
$add = '.txt';
if( checkExt($name) &&
!stristr($name,'user.ini') &&
!stristr($name,'.htaccess')
){//允许
return $name;
}
return $name.$add;
}
//解压到kod,文件名处理;识别编码并转换到当前系统编码
function unzip_pre_name($fileName){
$fileName = str_replace(array('../','..\\',''),'',$fileName);
if (!function_exists('iconv')){
return unzip_filter_ext($fileName);
}
if(isset($GLOBALS['unzipFileCharsetGet'])){
$charset = $GLOBALS['unzipFileCharsetGet'];
}else{
$charset = get_charset($fileName);
}
$toCharset = $GLOBALS['config']['systemCharset'];
$result = iconv_to($fileName,$charset,$toCharset);
if(!$result){
$result = $fileName;
}
$result = unzip_filter_ext($result);
//echo $charset.'==>'.$toCharset.':'.$result.'==='.$fileName.'<br/>';
return $result;
}

// 获取压缩文件内编码
// $GLOBALS['unzipFileCharsetGet']
function unzip_charset_get($list){
if(count($list) == 0) return 'utf-8';
$charsetArr = array();
for ($i=0; $i < count($list); $i++) {
$charset = get_charset($list[$i]['filename']);
if(!isset($charsetArr[$charset])){
$charsetArr[$charset] = 1;
}else{
$charsetArr[$charset] += 1;
}
}
arsort($charsetArr);
$keys = array_keys($charsetArr);

if(in_array('gbk',$keys)){//含有gbk,则认为是gbk
$keys[0] = 'gbk';
}
$GLOBALS['unzipFileCharsetGet'] = $keys[0];
return $keys[0];
}

/**
* 服务器相关环境
* 检测环境是否支持升级版本
*/
function serverInfo(){
$lib = array(
"sqlit3"=>intval( class_exists('SQLite3') ),
"sqlit" =>intval( extension_loaded('sqlite') ),
"curl" =>intval( function_exists('curl_init') ),
"pdo" =>intval( class_exists('PDO') ),
"mysqli"=>intval( extension_loaded('mysqli') ),
"mysql" =>intval( extension_loaded('mysql') ),
);
$libStr = "";
foreach($lib as $key=>$val){
$libStr .= $key.'='.$val.';';
}
$system = explode(" ", php_uname());
$env = array(
"sys" => strtolower($system[0]),
"php" => floatval(PHP_VERSION),
"server"=> $_SERVER['SERVER_SOFTWARE'],
"lib" => $libStr,
"info" => php_uname().';php='.PHP_VERSION,
);
$result = str_replace("\/","@",json_encode($env));
return $result;
}

function charset_check(&$str,$check,$tempCharset='utf-8'){
if ($str === '' || !function_exists("mb_convert_encoding")){
return false;
}
$testStr1 = @mb_convert_encoding($str,$tempCharset,$check);
$testStr2 = @mb_convert_encoding($testStr1,$check,$tempCharset);
if($str == $testStr2){
return true;
}
return false;

//http://blog.sina.com.cn/s/blog_b97feef301019571.html
function get_charset(&$str) {
if($GLOBALS['config']['checkCharsetDefault']){//直接指定编码
return $GLOBALS['config']['checkCharsetDefault'];
}
if ($str === '' || !function_exists("mb_detect_encoding")){
return 'utf-8';
}
$bom_arr = array(
'utf-8' => chr(0xEF) . chr(0xBB) .chr(0xBF),
'utf-16le' => chr(0xFF) . chr(0xFE),
'utf-16be' => chr(0xFE) . chr(0xFF),
'utf-32le' => chr(0xFF) . chr(0xFE) . chr(0x00) . chr(0x00),
'utf-32be' => chr(0x00) . chr(0x00) . chr(0xFE) . chr(0xFF),
);
foreach ($bom_arr as $key => $value) {
if (substr($str,0,strlen($value)) === $value ){
return $key;
}
}

//前面检测成功则,自动忽略后面
$charset=strtolower(@mb_detect_encoding($str,$GLOBALS['config']['checkCharset']));
$charsetGet = $charset;
if ($charset == 'cp936'){
// 有交叉,部分文件无法识别
if(charset_check($str,'ISO-8859-4') && !charset_check($str,'gbk') && !charset_check($str,'big5')){
$charset = 'ISO-8859-4';
}elseif(charset_check($str,'gbk') && !charset_check($str,'big5')){
$charset = 'gbk';
}else if(charset_check($str,'big5')){
$charset = 'big5';
}
}else if ($charset == 'euc-cn'){
$charset = 'gbk';
}else if ($charset == 'ascii'){
$charset = 'utf-8';
}
if ($charset == 'iso-8859-1'){
//检测详细编码;value为用什么编码检测;为空则用utf-8
$check = array(
'utf-8' => $charset,
'utf-16' => 'gbk',
'cp1251' => 'utf-8',
'cp1252' => 'utf-8'
);
foreach($check as $key => $val){
if(charset_check($str,$key,$val)){
if($val == ''){
$val = 'utf-8';
}
$charset = $key;
break;
}
}
}
//show_json($charset,false,$charsetGet);
return $charset;
}


function file_upload_size(){
global $config;
$size = get_post_max();
if(isset($config['settings']['updloadChunkSize'])){
$chunk = $config['settings']['updloadChunkSize'];
if($size >= $chunk){
$size = $chunk;
}
}
return $size;
}

function check_list_dir(){
$url = APP_HOST.'app/core/';
$find = "Application.class.php";

@ini_set('default_socket_timeout',1);
$context = stream_context_create(array('http'=>array('method'=>"GET",'timeout'=>1)));
$str = @file_get_contents($url,false,$context);
if(stripos($str,$find) === false){//not find;ok success
return true;
}else{
return false;
}
}
function php_env_check(){
$error = '';
if(!function_exists('iconv')) $error.= '<li>'.LNG('php_env_error').' iconv</li>';
if(!function_exists('json_encode')) $error.= '<li>'.LNG('php_env_error').' json</li>';
if(!function_exists('curl_init')) $error.= '<li>'.LNG('php_env_error').' curl</li>';
if(!function_exists('mb_convert_encoding')) $error.= '<li>'.LNG('php_env_error').' mb_string</li>';
if(!function_exists('file_get_contents')) $error.='<li>'.LNG('php_env_error').' file_get_contents</li>';
if(!version_compare(PHP_VERSION,'5.0','>=')) $error.= '<li>'.LNG('php_env_error_version').'</li>';
if(!check_list_dir()) $error.='<li>'.LNG('php_env_error_list_dir').'</li>';

$parent = get_path_father(BASIC_PATH);
$arr_check = array(
BASIC_PATH,
DATA_PATH,
DATA_PATH.'system',
DATA_PATH.'User',
DATA_PATH.'Group',
DATA_PATH.'session'
);
foreach ($arr_check as $value) {
if(!path_writeable($value)){
$error.= '<li>'.str_replace($parent,'',$value).'/ '.LNG('php_env_error_path').'</li>';
}
}
if( !function_exists('imagecreatefromjpeg')||
!function_exists('imagecreatefromgif')||
!function_exists('imagecreatefrompng')||
!function_exists('imagecolorallocate')){
$error.= '<li>'.LNG('php_env_error_gd').'</li>';
}
return $error;
}

//提前判断版本是否一致;
function check_cache(){
//检查是否更新失效
$content = file_get_contents(BASIC_PATH.'config/version.php');
$result = match_text($content,"'KOD_VERSION','(.*)'");
if($result != KOD_VERSION){
show_tips("您服务器开启了php缓存,文件更新尚未生效;
请关闭缓存,或稍后1分钟刷新页面再试!
<a href='http://www.tuicool.com/articles/QVjeu2i' target='_blank'>了解详情</a>");
}
}

function init_common(){
$GLOBALS['in'] = parse_incoming();
if(!file_exists(DATA_PATH)){
show_tips("data 目录不存在!\n\n(检查 DATA_PATH);");
}
// session path create and check
$errorTips = "[Error Code:1002] 目录权限错误!请设置程序目录及所有子目录为读写状态,
linux 运行如下指令:
<pre>su -c 'setenforce 0'\nchmod -R 777 ".BASIC_PATH.'</pre>';
if( !defined('SESSION_PATH_DEFAULT') ){
//检查session是否存在
if( !file_exists(KOD_SESSION) ||
!file_exists(KOD_SESSION.'index.html')){
mk_dir(KOD_SESSION);
touch(KOD_SESSION.'index.html');
if(!file_exists(KOD_SESSION.'index.html') ){
show_tips($errorTips);
}
}
//检查目录权限
if( !is_writable(KOD_SESSION) ||
!is_writable(KOD_SESSION.'index.html') ||
!is_writable(DATA_PATH.'system/apps.php') ||
!is_writable(DATA_PATH)){
show_tips($errorTips);
}
}

//version check update
$file = LIB_DIR.'update.php';
if(file_exists($file)){
//覆盖安装文件删除不了重定向问题优化
if(!is_writable($file) ){
show_tips($errorTips);
}

//update;
include($file);
updateCheck($file);

//clear
del_file($file);
if(file_exists($file)){
show_tips($errorTips);
}
user_logout();
}
}

//登陆是否需要验证码
function need_check_code(){
$setting = $GLOBALS['config']['settingSystem'];
if( !$setting['needCheckCode'] ||
!function_exists('imagecreatefromjpeg')||
!function_exists('imagecreatefromgif')||
!function_exists('imagecreatefrompng')||
!function_exists('imagecolorallocate')
){
return false;
}else{
return true;
}
}

function make_path($str){
//return md5(rand_string(30).$str.time());
$replace = array('/','\\',':','*','?','"','<','>','|');
return str_replace($replace, "_", $str);
}

function init_setting(){
$settingFile = USER_SYSTEM.'system_setting.php';
$settingSystemDefault = $GLOBALS['config']['settingSystemDefault'];
if (!file_exists($settingFile)){
$setting = $settingSystemDefault;
FileCache::save($settingFile,$setting);
}else{
$setting = FileCache::load($settingFile);
}
//合并配置
foreach ($settingSystemDefault as $key => $value) {
if(!isset($setting[$key])){
$setting[$key] = $value;
}
}
$GLOBALS['app']->setDefaultController($setting['firstIn']);
$GLOBALS['app']->setDefaultAction('index');
$GLOBALS['config']['settingSystem'] = $setting;

//group_role
$roleGroupFile = USER_SYSTEM.'system_role_group.php';
$roleGroup = $GLOBALS['config']['pathRoleGroupDefault'];
if (!file_exists($roleGroupFile)){
FileCache::save($roleGroupFile,$roleGroup);
}else{
$roleGroup = FileCache::load($roleGroupFile);
}
$GLOBALS['config']['pathRoleGroup'] = $roleGroup;

if(is_array($GLOBALS['L'])){
I18n::set($GLOBALS['L']);
}
I18n::set(array(
'kod_name' => $GLOBALS['config']['settingSystem']['systemName'],
'kod_name_desc' => $GLOBALS['config']['settingSystem']['systemDesc'],
));
if(isset($GLOBALS['config']['setting_system']['system_name'])){
I18n::set(array(
'kod_name' => $GLOBALS['config']['setting_system']['system_name'],
'kod_name_desc' => $GLOBALS['config']['setting_system']['system_desc'],
));
}
define('STATIC_PATH',$GLOBALS['config']['settings']['staticPath']);
}

function user_logout(){
@session_destroy();
@session_name('KOD_SESSION_SSO');
@session_start();
@session_destroy();

setcookie(SESSION_ID, '', time()-3600,'/');
setcookie('kod_name', '', time()-3600);
setcookie('kodToken', '', time()-3600);
setcookie('X-CSRF-TOKEN','',time()-3600);

$url = './index.php?user/login';
//之前界面维持,不是主动退出则登陆后跳转到之前页面
if(ACT != 'logout' && count($_GET)!=0 ){
$url .= '&link='.rawurlencode(this_url());
}
//移动端;接口请求时退出
if(isset($_REQUEST['HTTP_X_PLATFORM'])){
show_json('login error!',10001);
}
header('Location:'.$url);
exit;
}

function hash_encode($str) {
return str_replace(
base64_encode($str),
array('+','/','='),
array('_a','_b','_c')
);
}
function hash_decode($str) {
return base64_decode(
str_replace($str,array('_a','_b','_c'),array('+','/','='))
);
}

// 目录hash;
function hash_path($path,$addExt=false){
$password = 'kodcloud';
if(isset($GLOBALS['config']['settingSystem']['systemPassword'])){
$password = $GLOBALS['config']['settingSystem']['systemPassword'];
}

$pre = substr(md5($path.$password),0,8);
$result = $pre.md5($path);
if(file_exists($path)){
$result = $pre.md5($path.filemtime($path));
if(filesize($path) < 50*1024*1024){
$fileMd5 = @md5_file($path);
if($fileMd5){
$result = $fileMd5;
}
}
}
if($addExt){
$result = $result.'.'.get_path_ext($path);
}
return $result;
}


function navbar_menu_add($array){
$menu = &$GLOBALS['config']['settingSystem']['menu'];
$exist = false;
foreach ($menu as $value) {
if($value['name'] == $array['name']){
return false;
}
}
$menu[] = $array;
}

/**
* 检测用户是否在用户选择数据中
* @param [type] $info 组合数据 "all:0;role:1;2;user:2;group:101,102;"
* @return [type] [description]
*/
function check_user_select($info){
if(!is_string($info) || !$info) return true;
$valueArr = array(
"all" => "0",
"user" => array(),
"group" => array(),
"role" => array()
);
$userTypeArr = explode(';',$info);
for($i = 0;$i< count($userTypeArr);$i++){
$splitArr = explode(':',$userTypeArr[$i]);
if(count($splitArr) == 2){
$valueArr[$splitArr[0]] = $splitArr[1];
if($splitArr[0] != 'all'){
$valueArr[$splitArr[0]] = explode(',',$splitArr[1]);
}
}
}
if(!$valueArr['user'] && !$valueArr['group'] && !$valueArr['role']){
$valueArr['all'] = '1';
}
if($valueArr['all'] == '1'){
return true;
}

$userInfo = $_SESSION['kodUser'];
if(!$userInfo){
return false;
}
if( $valueArr['all'] == '1' ||
in_array($userInfo['userID'],$valueArr['user']) ||
in_array($userInfo['role'],$valueArr['role']) ){
return true;
}
$groupArr = array_keys($userInfo['groupInfo']);
foreach ($groupArr as $id) {
if( in_array($id,$valueArr['group']) ){
return true;
}
}
return false;
}
<?php
/*
* @link http://kodcloud.com/
* @author warlee | e-mail:kodcloud@qq.com
* @copyright warlee 2014.(Shanghai)Co.,Ltd
* @license http://kodcloud.com/tools/license/license.txt
*/


/**
* client ip address
*
* @param boolean $s_type ip类型[ip|long]
* @return string $ip
*/
function get_client_ip($b_ip = true){
$arr_ip_header = array(
"HTTP_CLIENT_IP",
"HTTP_X_FORWARDED_FOR",
"REMOTE_ADDR",
"HTTP_CDN_SRC_IP",
"HTTP_PROXY_CLIENT_IP",
"HTTP_WL_PROXY_CLIENT_IP"
);
$client_ip = 'unknown';
foreach ($arr_ip_header as $key) {
if (!empty($_SERVER[$key]) && strtolower($_SERVER[$key]) != "unknown") {
$client_ip = $_SERVER[$key];
break;
}
}
if ($pos = strpos($client_ip,',')){
$client_ip = substr($client_ip,$pos+1);
}
return $client_ip;
}

function get_url_link($url){
if(!$url) return "";
$res = parse_url($url);
$port = (empty($res["port"]) || $res["port"] == '80')?'':':'.$res["port"];
return $res['scheme']."://".$res["host"].$port.$res['path'];
}
function get_url_root($url){
if(!$url) return "";
$res = parse_url($url);
$port = (empty($res["port"]) || $res["port"] == '80')?'':':'.$res["port"];
return $res['scheme']."://".$res["host"].$port.'/';
}
function get_url_domain($url){
if(!$url) return "";
$res = parse_url($url);
return $res["host"];
}
function get_url_scheme($url){
if(!$url) return "";
$res = parse_url($url);
return $res['scheme'];
}

function http_type(){
if( (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ||
(isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') ||
$_SERVER['SERVER_PORT'] === 443
){
return 'https';
}
return 'http';
}

function get_host() {
//兼容子目录反向代理:只能是前端js通过cookie传入到后端进行处理
if(defined('GLOBAL_DEBUG') && isset($_COOKIE['HOST']) && isset($_COOKIE['APP_HOST'])){
return $_COOKIE['HOST'];
}
$protocol = http_type().'://';
$url_host = $_SERVER['SERVER_NAME'].($_SERVER['SERVER_PORT']=='80' ? '' : ':'.$_SERVER['SERVER_PORT']);
$host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $url_host;
$host = isset($_SERVER['HTTP_X_FORWARDED_HOST']) ? $_SERVER['HTTP_X_FORWARDED_HOST'] : $host;//proxy
return $protocol.$host;
}
// current request url
function this_url(){
$url = rtrim(get_host(),'/').'/'.ltrim($_SERVER['REQUEST_URI'],'/');
return $url;
}

//解决部分主机不兼容问题
function webroot_path($basic_path){
$webRoot = str_replace($_SERVER['SCRIPT_NAME'],'',$_SERVER['SCRIPT_FILENAME']);
$webRoot = rtrim(str_replace(array('\\','\/\/','\\\\'),'/',$webRoot),'/').'/';
if( substr($basic_path,0,strlen($webRoot)) == $webRoot ){
return $webRoot;
}

$webRoot = $_SERVER['DOCUMENT_ROOT'];
$webRoot = rtrim(str_replace(array('\\','\/\/','\\\\'),'/',$webRoot),'/').'/';
if( substr($basic_path,0,strlen($webRoot)) == $webRoot ){
return $webRoot;
}
return $basic_path;
}

function ua_has($str){
if(!isset($_SERVER['HTTP_USER_AGENT'])){
return false;
}
if(strpos($_SERVER['HTTP_USER_AGENT'],$str) ){
return true;
}
return false;
}
function is_wap(){
if(!isset($_SERVER['HTTP_USER_AGENT'])){
return false;
}
if(preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|iphone|ipad|ipod|android|xoom|miui)/i',
strtolower($_SERVER['HTTP_USER_AGENT']))){
return true;
}
if((isset($_SERVER['HTTP_ACCEPT'])) &&
(strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml') !== false)){
return true;
}
return false;
}

/**
* 终止并完成http请求;客户端终止等待完成请求
* 后续代码可以继续运行;例如日志、统计等代码;后续输出将不再生效;
*/
function http_close(){
ignore_timeout(0);
if(function_exists('fastcgi_finish_request')) {
fastcgi_finish_request();
} else {
header("Connection: close");
header("Content-Length: ".ob_get_length());
ob_start();
echo str_pad('',1024*5);
ob_end_flush();
flush();
}
}

function parse_headers($raw_headers){
$headers = array();
$key = '';
foreach (explode("\n", $raw_headers) as $h) {
$h = explode(':', $h, 2);
if (isset($h[1])) {
if ( ! isset($headers[$h[0]])) {
$headers[$h[0]] = trim($h[1]);
} elseif (is_array($headers[$h[0]])) {
$headers[$h[0]] = array_merge($headers[$h[0]], array(trim($h[1])) );
} else {
$headers[$h[0]] = array_merge(array($headers[$h[0]]), array(trim($h[1])) );
}
$key = $h[0];
} else {
if (substr($h[0], 0, 1) === "\t") {
$headers[$key] .= "\r\n\t" . trim($h[0]);
} elseif ( ! $key) {
$headers[0] = trim($h[0]);
}
trim($h[0]);
}
}
return $headers;
}

//多人同时上传同一个文件;或上传到多个服务;
$curlCurrentFile = false;
function curl_progress_bind($file,$uuid='',$download=false){
if(!$GLOBALS['curlCurrentFile']){
$cacheFile = TEMP_PATH.'/curlProgress/'.md5($file.$uuid).'.log';
mk_dir(get_path_father($cacheFile));
@touch($cacheFile);
if(!file_exists($cacheFile)){
return;
}
$GLOBALS['curlCurrentFile'] = array(
'path' => $file,
'uuid' => $uuid,
'time' => 0,
'setNum' => 0,
'cacheFile' => $cacheFile,
'download' => $download
);
}
curl_progress_set(false,0,0,0,0);
}
function curl_progress_set(){
$fileInfo = $GLOBALS['curlCurrentFile'];
$file = $fileInfo['path'];
$cacheFile = $fileInfo['cacheFile'];
if( !is_array($fileInfo) ||
mtime() - $fileInfo['time'] <= 0.3){//每300ms做一次记录
return;
}
//进度文件被删除则终止传输;
clearstatcache();
if( !file_exists($cacheFile) ||
!file_exists($file) ){
exit;
}

$GLOBALS['curlCurrentFile']['time'] = mtime();
$GLOBALS['curlCurrentFile']['setNum'] += 1;
$args = func_get_args();
if (is_resource($args[0])) {// php 5.5
array_shift($args);
}
$downTotal = $args[0];
$downSize = $args[1];
$upTotal = $args[2];
$upSize = $args[3];

//默认上传
$size = @filesize($file);
$sizeSuccess = $upSize;
if($fileInfo['download']){
$size = $downTotal;
$sizeSuccess = $downSize;
}
$json = array(
'name' => substr(rawurlencode(get_path_this($file)),-10),
'taskUuid' => $fileInfo['uuid'],
'type' => $fileInfo['download']?'fileDownload':'fileUpload',
'timeStart' => time(),

'sizeTotal' => $size,
'sizeSuccess' => $sizeSuccess,
'progress' => 0,
'timeUse' => 0,
'timeNeed' => 0,
'speed' => 0,
'logList' => array()
);
//write_log(array($args,$size,$sizeSuccess),'ttt');
if(time() - filemtime($cacheFile) <= 10){//10s内才处理;同一个文件
$data = @json_decode(file_get_contents($cacheFile),true);
$json = $data?$data:$json;
}else{
del_file($cacheFile);
touch($cacheFile);
}

//更新数据
$logList = &$json['logList'];
if(count($logList) >=10 ){
$logList = array_slice($logList,-10);
}

$current = array('time'=>time(),'sizeSuccess'=>$sizeSuccess);
if(count($logList) == 0){
$logList[] = $current;
}else{
$last = $logList[count($logList)-1];
if(time() == $last['time']){
$logList[count($logList)-1] = $current;
}else{
$logList[] = $current;
}
}

//计算速度
$first = $logList[0];
$last = $logList[count($logList)-1];
$time = $last['time'] - $first['time'];
$speed = $time?($last['sizeSuccess'] - $first['sizeSuccess'])/$time : 0;
if($speed <0 || $speed>500*1024*1024){
$speed = 0;
}
$timeNeed = $speed ? ($size - $sizeSuccess)/$speed:0;
$progress = 0;
if($size != 0 ){
$progress = ($sizeSuccess>=$size)?1:$sizeSuccess/$size;
}
$json['sizeTotal'] = $size;
$json['sizeSuccess'] = $sizeSuccess;
$json['progress'] = $progress;
$json['timeUse'] = time() - $json['timeStart'];
$json['timeNeed'] = intval($timeNeed);
$json['speed'] = intval($speed);
file_put_contents($cacheFile,json_encode($json));
}
function curl_progress_get($file,$uuid=''){
$cacheFile = TEMP_PATH.'/curlProgress/'.md5($file.$uuid).'.log';
if(!file_exists($cacheFile) || $file == ''){
return -1;
}
$data = @json_decode(file_get_contents($cacheFile),true);
if(is_array($data)){
unset($data['logList']);
return $data;
}
return -3;
}

// php7.1 curl上传中文路径文件失败问题?【暂时通过重命名方式解决】
function url_request($url,$method='GET',$data=false,$headers=false,$options=false,$json=false,$timeout=3600){
if(!$url){
return array(
'data' => 'url error! url='.$url,
'code' => 0
);
}
ignore_timeout();
$ch = curl_init();
$upload = false;
if(is_array($data)){//上传检测并兼容
foreach($data as $key => $value){
if(!is_string($value) || substr($value,0,1) !== "@"){
continue;
}
$upload = true;
$path = ltrim($value,'@');
$filename = iconv_app(get_path_this($path));
$mime = get_file_mime(get_path_ext($filename));
if(isset($data['curlUploadName'])){//自定义上传文件名;临时参数
$filename = $data['curlUploadName'];
unset($data['curlUploadName']);
}
if (class_exists('\CURLFile')){
$data[$key] = new CURLFile(realpath($path),$mime,$filename);
}else{
$data[$key] = "@".realpath($path).";type=".$mime.";filename=".$filename;
}
//有update且method为PUT
if($method == 'PUT'){
curl_setopt($ch, CURLOPT_PUT,1);
curl_setopt($ch, CURLOPT_INFILE,@fopen($path,'r'));
curl_setopt($ch, CURLOPT_INFILESIZE,@filesize($path));
}

//上传进度记录并处理
curl_progress_bind($path);
curl_setopt($ch, CURLOPT_NOPROGRESS, false);
curl_setopt($ch, CURLOPT_PROGRESSFUNCTION,'curl_progress_set');
}
}
if($upload){
if (class_exists('\CURLFile')){
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
} else {
if (defined('CURLOPT_SAFE_UPLOAD')) {
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
}
}
}

// post数组或拼接的参数;不同方式服务器兼容性有所差异

// post默认用array发送;content-type为x-www-form-urlencoded时用key=1&key=2的形式
if (is_array($data) && is_array($headers) && $method != 'DOWNLOAD'){
foreach ($headers as $key) {
if(strstr($key,'x-www-form-urlencoded')){
$data = http_build_query($data);
break;
}
}
}
if($method == 'GET' && $data){
if(is_array($data)){
$data = http_build_query($data);
}
if(strstr($url,'?')){
$url = $url.'&'.$data;
}else{
$url = $url.'?'.$data;
}
}
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HEADER,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);
curl_setopt($ch, CURLINFO_HEADER_OUT, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
// curl_setopt($ch, CURLOPT_SSLVERSION,1);//1|5|6; http://t.cn/RZy5nXF
curl_setopt($ch, CURLOPT_TIMEOUT,$timeout);
curl_setopt($ch, CURLOPT_REFERER,get_url_link($url));
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.94 Safari/537.36');
if($headers){
if(is_string($headers)){
$headers = array($headers);
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}

switch ($method) {
case 'GET':
curl_setopt($ch,CURLOPT_HTTPGET,1);
break;
case 'DOWNLOAD':
//远程下载到指定文件;进度条
$downTemp = $data.'.'.rand_string(5);
$fp = fopen ($downTemp,'w+');
curl_progress_bind($downTemp,'',true);//下载进度
curl_setopt($ch, CURLOPT_NOPROGRESS, false);
curl_setopt($ch, CURLOPT_PROGRESSFUNCTION,'curl_progress_set');

curl_setopt($ch, CURLOPT_HTTPGET,1);
curl_setopt($ch, CURLOPT_HEADER,0);//不输出头
curl_setopt($ch, CURLOPT_FILE, $fp);
//CURLOPT_RETURNTRANSFER 必须放在CURLOPT_FILE前面;否则出问题
break;
case 'HEAD':
curl_setopt($ch, CURLOPT_NOBODY, true);
break;
case 'POST':
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
break;
case 'OPTIONS':
case 'PATCH':
case 'DELETE':
case 'PUT':
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,$method);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
break;
default:break;
}

if(!empty($options)){
curl_setopt_array($ch, $options);
}
$response = curl_exec($ch);
$header_size = curl_getinfo($ch,CURLINFO_HEADER_SIZE);
$response_info = curl_getinfo($ch);
$http_body = substr($response, $header_size);
$http_header = substr($response, 0, $header_size);
$http_header = parse_headers($http_header);
if(is_array($http_header)){
// $http_header['kod_add_request_url'] = $url;
}
//error
if($response_info['http_code'] == 0){
$error_message = curl_error($ch);
if (! empty($error_message)) {
$error_message = "API call to $url failed;$error_message";
} else {
$error_message = "API call to $url failed;maybe network error!";
}
return array(
'data' => $error_message,
'code' => 0,
'header' => $response_info,
);
}

curl_close($ch);
if(is_array($GLOBALS['curlCurrentFile'])){
@unlink($GLOBALS['curlCurrentFile']['cacheFile']);
}
$success = $response_info['http_code'] >= 200 && $response_info['http_code'] <= 299;
if( $json && $success){
$data = @json_decode($http_body,true);
if (json_last_error() == 0) { // string
$http_body = $data;
}
}
if($method == 'DOWNLOAD'){
@fclose($fp);
@clearstatcache();
if($success){
move_path($downTemp,$data);
}
@unlink($downTemp);
}

$return = array(
'data' => $http_body,
'status' => $success,
'code' => $response_info['http_code'],
'header' => $http_header,
);
return $return;
}
function curl_get_contents($url){
$data = url_request($url);
return $data['data'];
}

function get_headers_curl($url,$timeout=30,$depth=0,&$headers=array()){
if(!function_exists('curl_init')){
return false;
}
if ($depth >= 10) return false;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HEADER,true);
curl_setopt($ch, CURLOPT_NOBODY,true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT,$timeout);
curl_setopt($ch, CURLOPT_REFERER,get_url_link($url));
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.94 Safari/537.36');

$res = curl_exec($ch);
$res = explode("\r\n", $res);

$location = false;
foreach ($res as $line) {
list($key, $val) = explode(": ", $line, 2);
$the_key = trim($key);
if($the_key == 'Location' || $the_key == 'location'){
$the_key = 'Location';
$location = trim($val);
}
if( strlen($the_key) == 0 &&
strlen(trim($val)) == 0 ){
continue;
}
if( substr($the_key,0,4) == 'HTTP' &&
strlen(trim($val)) == 0 ){
$headers[] = $the_key;
continue;
}

if(!isset($headers[$the_key])){
$headers[$the_key] = trim($val);
}else{
if(is_string($headers[$the_key])){
$temp = $headers[$the_key];
$headers[$the_key] = array($temp);
}
$headers[$the_key][] = trim($val);
}
}
if($location !== false){
$depth++;
get_headers_curl($location,$timeout,$depth,$headers);
}
return count($headers)==0?false:$headers;
}

// 防止SSRF 攻击;curl,file_get_contents前检测url;
function request_url_safe($url){
$link = trim(strtolower($url));
$link = str_replace('\\','/',$link);
while (strstr($link,'../')) {
$link = str_replace('../', '/', $link);
}
if( substr($link,0,6) != "ftp://" &&
substr($link,0,7) != "http://" &&
substr($link,0,8) != "https://" ){
return false;
}
return true;
}

// url header data
function url_header($url){
$header = get_headers_curl($url);//curl优先
if(is_array($header)){
$header['ACTION_BY'] = 'get_headers_curl';
}else{
$header = @get_headers($url,true);
}
if (!$header) return false;

//加入小写header值;兼容各种不统一的情况
$header['———'] = '————————————';//分隔
foreach ($header as $key => $value) {
$header[strtolower($key)] = $value;
}
$checkArr = array(
'content-length' => 0,
'location' => $url,//301调整
'content-disposition' => '',
);
//处理多次跳转的情况
foreach ($checkArr as $key=>$val) {
if(isset($header[$key])){
$checkArr[$key] = $header[$key];
if(is_array($header[$key]) && count($header[$key])>0){
$checkArr[$key] = $header[$key][count($header[$key])-1];
}
}
}
$name = $checkArr['content-disposition'];
$length = $checkArr['content-length'];
$fileUrl= $checkArr['location'];
if($name){
preg_match('/filename\s*=\s*"*(.*)"*?/',$name,$match);
if(count($match) == 2){
$name = $match[1];
}else{
$name = '';
}
}
if(!$name){
$name = get_path_this($fileUrl);
if (strstr($name,'=')) $name = substr($name,strrpos($name,'=')+1);
if (!$name) $name = 'file.data';
}
if(!empty($header['x-outfilename'])){
$name = $header['x-outfilename'];
}
$name = rawurldecode(trim($name,'"'));
$name = str_replace(array('/','\\'),'-',$name);//safe;
$supportRange = isset($header["accept-ranges"])?true:false;
if(!request_url_safe($fileUrl)){
$fileUrl = "";
}
$result = array(
'url' => $fileUrl,
'length' => $length,
'name' => $name,
'supportRange' => $supportRange && ($length!=0),
'all' => $header,
);
if(!function_exists('curl_init')){
$result['supportRange'] = false;
}
//pr($url,$result);
return $result;
}


// check url if can use
function check_url($url){
$array = get_headers($url,true);
$error = array('/404/','/403/','/500/');
foreach ($error as $value) {
if (preg_match($value, $array[0])) {
return false;
}
}
return true;
}

// refer URL
function refer_url(){
return isset($_SERVER["HTTP_REFERER"]) ? $_SERVER["HTTP_REFERER"] : '';
}

function select_var($array){
if (!is_array($array)) return -1;
ksort($array);
$chosen = -1;
foreach ($array as $k => $v) {
if (isset($v)) {
$chosen = $v;
break;
}
}
return $chosen;
}

/**
* 解析url获得url参数
* @param $query
* @return array array
*/
function parse_url_query($url){
$arr = parse_url($url);
$queryParts = explode('&',$arr['query']);
$params = array();
foreach ($queryParts as $param) {
$item = explode('=', $param);
$key = $item[0]; unset($item[0]);
$params[$key] = implode('=', $item);
}
return $params;
}

function stripslashes_deep($value){
$value = is_array($value) ? array_map('stripslashes_deep', $value) : (isset($value) ? stripslashes($value) : null);
return $value;
}

function parse_url_route(){
$param = str_replace($_SERVER['SCRIPT_NAME'],"",$_SERVER['SCRIPT_NAME']);
if($param && substr($param,0,1) == '/'){
$arr = explode('&',$param);
$arr[0] = ltrim($arr[0],'/');
foreach ($arr as $cell) {
$cell = explode('=',$cell);
if(is_array($cell)){
if(!isset($cell[1])){
$cell[1] = '';
}
$_GET[$cell[0]] = $cell[1];
$_REQUEST[$cell[0]] = $cell[1];
}
}
}
}


/**
* GET/POST数据统一入口
* 将GET和POST的数据进行过滤,去掉非法字符以及hacker code,返回一个数组
* 注意如果GET和POST有相同的Key,POST优先
*
* @return array $_GET和$_POST数据过滤处理后的值
*/
function parse_incoming(){
parse_url_route();
global $_GET, $_POST,$_COOKIE;

$_COOKIE = stripslashes_deep($_COOKIE);
$_GET = stripslashes_deep($_GET);
$_POST = stripslashes_deep($_POST);
$return = array();
$return = array_merge($_GET,$_POST);
$remote = array_get_index($return,0);
$remote = explode('/',trim($remote[0],'/'));
$return['URLremote'] = $remote;
return $return;
}

function db_escape($str) {
$str = addslashes($str);
$str = str_replace(array('_', '%'),array('\\_', '\\%'), $str);
return $str;
}

/**
* 获取输入参数 支持过滤和默认值
* 使用方法:
* <code>
* in('id',0); 获取id参数 自动判断get或者post
* in('post.name','','htmlspecialchars'); 获取$_POST['name']
* in('get.'); 获取$_GET
* </code>
* @param string $name 变量的名称 支持指定类型
* @param mixed $default 不存在的时候默认值
* @param mixed $filter 参数过滤方法
* @return mixed
*/
function in($name,$default='',$filter=null) {
$default_filter = 'htmlspecialchars,db_escape';
if(strpos($name,'.')) { // 指定参数来源
list($method,$name) = explode('.',$name,2);
}else{ // 默认为自动判断
$method = 'request';
}
switch(strtolower($method)) {
case 'get' : $input =& $_GET;break;
case 'post' : $input =& $_POST;break;
case 'request' : $input =& $_REQUEST; break;

case 'put' : parse_str(file_get_contents('php://input'), $input);break;
case 'session' : $input =& $_SESSION; break;
case 'cookie' : $input =& $_COOKIE; break;
case 'server' : $input =& $_SERVER; break;
case 'globals' : $input =& $GLOBALS; break;
default:return NULL;
}
$filters = isset($filter)?$filter:$default_filter;
if($filters) {
$filters = explode(',',$filters);
}
if(empty($name)) { // 获取全部变量
$data = $input;
foreach($filters as $filter){
$data = array_map($filter,$data); // 参数过滤
}
}elseif(isset($input[$name])) { // 取值操作
$data = $input[$name];
foreach($filters as $filter){
if(function_exists($filter)) {
$data = is_array($data)?array_map($filter,$data):$filter($data); // 参数过滤
}else{
$data = filter_var($data,is_int($filter)?$filter:filter_id($filter));
if(false === $data) {
return isset($default)?$default:NULL;
}
}
}
}else{ // 变量默认值
$data = isset($default)?$default:NULL;
}
return $data;
}


function url2absolute($index_url, $preg_url){
if (preg_match('/[a-zA-Z]*\:\/\//', $preg_url)) return $preg_url;
preg_match('/([a-zA-Z]*\:\/\/.*)\//', $index_url, $match);
$index_url_temp = $match[1];

foreach(explode('/', $preg_url) as $key => $var) {
if ($key == 0 && $var == '') {
preg_match('/([a-zA-Z]*\:\/\/[^\/]*)\//', $index_url, $match);
$index_url_temp = $match[1] . $preg_url;
break;
}
if ($var == '..') {
preg_match('/([a-zA-Z]*\:\/\/.*)\//', $index_url_temp, $match);
$index_url_temp = $match[1];
} elseif ($var != '.') $index_url_temp .= '/' . $var;
}
return $index_url_temp;
}

// 输出js
function exec_js($js){
echo "<script language='JavaScript'>\n" . $js . "</script>\n";
}
// 禁止缓存
function no_cache(){
header("Pragma:no-cache\r\n");
header("Cache-Control:no-cache\r\n");
header("Expires:0\r\n");
}
// 生成javascript转向
function go_url($url, $msg = ''){
header("Content-type: text/html; charset=utf-8\r\n");
echo "<script type='text/javascript'>\n";
echo "window.location.href='$url';";
echo "</script>\n";
exit;
}

function send_http_status($i_status, $s_message = ''){
$a_status = array(
// Informational 1xx
100 => 'Continue',
101 => 'Switching Protocols',
// Success 2xx
200 => 'OK',
201 => 'Created',
202 => 'Accepted',
203 => 'Non-Authoritative Information',
204 => 'No Content',
205 => 'Reset Content',
206 => 'Partial Content',
// Redirection 3xx
300 => 'Multiple Choices',
301 => 'Moved Permanently',
302 => 'Found', // 1.1
303 => 'See Other',
304 => 'Not Modified',
305 => 'Use Proxy', // 306 is deprecated but reserved
307 => 'Temporary Redirect',
// Client Error 4xx
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',
// Server Error 5xx
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 (array_key_exists($i_status, $a_status)) {
header('HTTP/1.1 ' . $i_status . ' ' . $a_status[$i_status]);
}
if ($s_message) {
echo $s_message;
exit();
}
}

//是否是windows
function client_is_windows(){
static $is_windows;
if(!is_array($is_windows)){
$is_windows = array(0);
$os = get_os();
if(strstr($os,'Windows')){
$is_windows = array(1);
}
}
return $is_windows[0];
}

// 获取操作系统信息 TODO
function get_os (){
$agent = $_SERVER['HTTP_USER_AGENT'];
$preg_find = array(
"Windows 95" =>array('win','95'),
"Windows ME" =>array('win 9x','4.90'),
"Windows 98" =>array('win','98'),
"Windows 2000" =>array('win','nt 5.0',),
"Windows XP" =>array('win','nt 5.1'),
"Windows Vista" =>array('win','nt 6.0'),
"Windows 7" =>array('win','nt 6.1'),
"Windows 32" =>array('win','32'),
"Windows NT" =>array('win','nt'),
"Mac OS" =>array('Mac OS'),
"Linux" =>array('linux'),
"Unix" =>array('unix'),
"SunOS" =>array('sun','os'),
"IBM OS/2" =>array('ibm','os'),
"Macintosh" =>array('Mac','PC'),
"PowerPC" =>array('PowerPC'),
"AIX" =>array('AIX'),
"HPUX" =>array('HPUX'),
"NetBSD" =>array('NetBSD'),
"BSD" =>array('BSD'),
"OSF1" =>array('OSF1'),
"IRIX" =>array('IRIX'),
"FreeBSD" =>array('FreeBSD'),
);

$os='';
foreach ($preg_find as $key => $value) {
if(count($value)==1 && stripos($agent,$value[0])){
$os=$key;break;
}else if(count($value)==2
&& stripos($agent,$value[0])
&& stripos($agent,$value[1])
){
$os=$key;break;
}
}
if ($os=='') {$os = "Unknown"; }
return $os;
}

// 浏览器是否直接打开
function mime_support($mime){
$arr_start = array(
"text/",
"image/",
"audio/",
"video/",
"message/",
);
$arr_mime = array(
"application/hta",
"application/javascript",
"application/json",
"application/x-latex",
"application/pdf",
"application/x-shockwave-flash",
"application/x-tex",
"application/x-texinfo"
);
if(in_array($mime,$arr_mime)){
return true;
}
foreach ($arr_start as $val) {
if(substr($mime,0,strlen($val)) == $val){
return true;
}
}
return false;
}

//根据扩展名获取mime
function get_file_mime($ext){
$mimetypes = array(
"323" => "text/h323",
"acx" => "application/internet-property-stream",
"ai" => "application/postscript",
"aif" => "audio/x-aiff",
"aifc" => "audio/x-aiff",
"aiff" => "audio/x-aiff",
"asf" => "video/x-ms-asf",
"asr" => "video/x-ms-asf",
"asx" => "video/x-ms-asf",
"au" => "audio/basic",
"avi" => "video/x-msvideo",
"axs" => "application/olescript",
"bas" => "text/plain",
"bcpio" => "application/x-bcpio",
"bin" => "application/octet-stream",
"bmp" => "image/bmp",
"c" => "text/plain",
"cat" => "application/vnd.ms-pkiseccat",
"cdf" => "application/x-cdf",
"cer" => "application/x-x509-ca-cert",
"class" => "application/octet-stream",
"clp" => "application/x-msclip",
"cmx" => "image/x-cmx",
"cod" => "image/cis-cod",
"cpio" => "application/x-cpio",
"crd" => "application/x-mscardfile",
"crl" => "application/pkix-crl",
"crt" => "application/x-x509-ca-cert",
"csh" => "application/x-csh",
"css" => "text/css",
"dcr" => "application/x-director",
"der" => "application/x-x509-ca-cert",
"dir" => "application/x-director",
"dll" => "application/x-msdownload",
"dms" => "application/octet-stream",
"doc" => "application/msword",
"docx" => "application/msword",
"dot" => "application/msword",
"dvi" => "application/x-dvi",
"dxr" => "application/x-director",
"eps" => "application/postscript",
"etx" => "text/x-setext",
"evy" => "application/envoy",
"exe" => "application/octet-stream",
"fif" => "application/fractals",
"flr" => "x-world/x-vrml",
"flv" => "video/x-flv",
"f4v" => "application/octet-stream",
"gif" => "image/gif",
"gtar" => "application/x-gtar",
"gz" => "application/x-gzip",
"h" => "text/plain",
"hdf" => "application/x-hdf",
"hlp" => "application/winhlp",
"hqx" => "application/mac-binhex40",
"hta" => "application/hta",
"htc" => "text/x-component",
"htm" => "text/html",
"html" => "text/html",
"htt" => "text/webviewhtml",
"ico" => "image/x-icon",
"ief" => "image/ief",
"iii" => "application/x-iphone",
"ins" => "application/x-internet-signup",
"isp" => "application/x-internet-signup",
"jfif" => "image/pipeg",
"jpe" => "image/jpeg",
"jpeg" => "image/jpeg",
"jpg" => "image/jpeg",
"js" => "application/javascript",
"json" => "application/json",
"latex" => "application/x-latex",
"lha" => "application/octet-stream",
"lsf" => "video/x-la-asf",
"lsx" => "video/x-la-asf",
"lzh" => "application/octet-stream",
"m13" => "application/x-msmediaview",
"m14" => "application/x-msmediaview",
"m3u" => "audio/x-mpegurl",
'm4a' => "audio/mp4",
'm4v' => "audio/mp4",
"man" => "application/x-troff-man",
"mdb" => "application/x-msaccess",
"me" => "application/x-troff-me",
"mht" => "message/rfc822",
"mhtml" => "message/rfc822",
"mid" => "audio/mid",
"mny" => "application/x-msmoney",
"mov" => "video/quicktime",
"movie" => "video/x-sgi-movie",
"mp2" => "video/mpeg",
"mp3" => "audio/mpeg",
"mp4" => "video/mp4",
"mp4v" => "video/mp4",
"mpa" => "video/mpeg",
"mpe" => "video/mpeg",
"mpeg" => "video/mpeg",
"mpg" => "video/mpeg",
"mpp" => "application/vnd.ms-project",
"mpv2" => "video/mpeg",
"ms" => "application/x-troff-ms",
"mvb" => "application/x-msmediaview",
"nws" => "message/rfc822",
"oda" => "application/oda",
"ogg" => "audio/ogg",
"oga" => "audio/ogg",
"ogv" => "audio/ogg",
"p10" => "application/pkcs10",
"p12" => "application/x-pkcs12",
"p7b" => "application/x-pkcs7-certificates",
"p7c" => "application/x-pkcs7-mime",
"p7m" => "application/x-pkcs7-mime",
"p7r" => "application/x-pkcs7-certreqresp",
"p7s" => "application/x-pkcs7-signature",
"pbm" => "image/x-portable-bitmap",
"pdf" => "application/pdf",
"pfx" => "application/x-pkcs12",
"pgm" => "image/x-portable-graymap",
"pko" => "application/ynd.ms-pkipko",
"pma" => "application/x-perfmon",
"pmc" => "application/x-perfmon",
"pml" => "application/x-perfmon",
"pmr" => "application/x-perfmon",
"pmw" => "application/x-perfmon",
"png" => "image/png",
"pnm" => "image/x-portable-anymap",
"pot," => "application/vnd.ms-powerpoint",
"ppm" => "image/x-portable-pixmap",
"pps" => "application/vnd.ms-powerpoint",
"ppt" => "application/vnd.ms-powerpoint",
"pptx" => "application/vnd.ms-powerpoint",
"prf" => "application/pics-rules",
"ps" => "application/postscript",
"pub" => "application/x-mspublisher",
"qt" => "video/quicktime",
"ra" => "audio/x-pn-realaudio",
"ram" => "audio/x-pn-realaudio",
"ras" => "image/x-cmu-raster",
"rgb" => "image/x-rgb",
"rmi" => "audio/mid",
"roff" => "application/x-troff",
"rtf" => "application/rtf",
"rtx" => "text/richtext",
"scd" => "application/x-msschedule",
"sct" => "text/scriptlet",
"setpay" => "application/set-payment-initiation",
"setreg" => "application/set-registration-initiation",
"sh" => "application/x-sh",
"shar" => "application/x-shar",
"sit" => "application/x-stuffit",
"snd" => "audio/basic",
"spc" => "application/x-pkcs7-certificates",
"spl" => "application/futuresplash",
"src" => "application/x-wais-source",
"sst" => "application/vnd.ms-pkicertstore",
"stl" => "application/vnd.ms-pkistl",
"stm" => "text/html",
"svg" => "image/svg+xml",
"sv4cpio" => "application/x-sv4cpio",
"sv4crc" => "application/x-sv4crc",
"swf" => "application/x-shockwave-flash",
"t" => "application/x-troff",
"tar" => "application/x-tar",
"tcl" => "application/x-tcl",
"tex" => "application/x-tex",
"texi" => "application/x-texinfo",
"texinfo" => "application/x-texinfo",
"tgz" => "application/x-compressed",
"tif" => "image/tiff",
"tiff" => "image/tiff",
"tr" => "application/x-troff",
"trm" => "application/x-msterminal",
"tsv" => "text/tab-separated-values",
"txt" => "text/plain",
"uls" => "text/iuls",
"ustar" => "application/x-ustar",
"vcf" => "text/x-vcard",
"vrml" => "x-world/x-vrml",
"wav" => "audio/wav",
"wcm" => "application/vnd.ms-works",
"wdb" => "application/vnd.ms-works",
"webm" => "video/webm",
"webmv" => "video/webm",
"wks" => "application/vnd.ms-works",
"wmf" => "application/x-msmetafile",
"wps" => "application/vnd.ms-works",
"wri" => "application/x-mswrite",
"wrl" => "x-world/x-vrml",
"wrz" => "x-world/x-vrml",
"xaf" => "x-world/x-vrml",
"xbm" => "image/x-xbitmap",
"xla" => "application/vnd.ms-excel",
"xlc" => "application/vnd.ms-excel",
"xlm" => "application/vnd.ms-excel",
"xls" => "application/vnd.ms-excel",
"xlsx" => "application/vnd.ms-excel",
"xlt" => "application/vnd.ms-excel",
"xlw" => "application/vnd.ms-excel",
"xof" => "x-world/x-vrml",
"xpm" => "image/x-xpixmap",
"xwd" => "image/x-xwindowdump",
"z" => "application/x-compress",
"zip" => "application/zip"
);

//代码 或文本浏览器输出
$text = array('oexe','inc','inf','csv','log','asc','tsv');
$code = array("abap","abc","as","ada","adb","htgroups","htpasswd","conf","htaccess","htgroups",
"htpasswd","asciidoc","asm","ahk","bat","cmd","c9search_results","cpp","c","cc","cxx","h","hh","hpp",
"cirru","cr","clj","cljs","CBL","COB","coffee","cf","cson","Cakefile","cfm","cs","css","curly","d",
"di","dart","diff","patch","Dockerfile","dot","dummy","dummy","e","ejs","ex","exs","elm","erl",
"hrl","frt","fs","ldr","ftl","gcode","feature",".gitignore","glsl","frag","vert","go","groovy",
"haml","hbs","handlebars","tpl","mustache","hs","hx","html","htm","xhtml","erb","rhtml","ini",
"cfg","prefs","io","jack","jade","java","js","jsm","json","jq","jsp","jsx","jl","tex","latex",
"ltx","bib","lean","hlean","less","liquid","lisp","ls","logic","lql","lsl","lua","lp","lucene",
"Makefile","GNUmakefile","makefile","OCamlMakefile","make","md","markdown","mask","matlab",
"mel","mc","mush","mysql","nc","nix","m","mm","ml","mli","pas","p","pl","pm","pgsql","php","phtml",
"ps1","praat","praatscript","psc","proc","plg","prolog","properties","proto","py","r","Rd",
"Rhtml","rb","ru","gemspec","rake","Guardfile","Rakefile","Gemfile","rs","sass","scad","scala",
"scm","rkt","scss","sh","bash",".bashrc","sjs","smarty","tpl","snippets","soy","space","sql",
"styl","stylus","svg","tcl","tex","txt","textile","toml","twig","ts","typescript","str","vala",
"vbs","vb","vm","v","vh","sv","svh","vhd","vhdl","xml","rdf","rss","log",
"wsdl","xslt","atom","mathml","mml","xul","xbl","xaml","xq","yaml","yml","htm",
"xib","storyboard","plist","csproj");
if (array_key_exists($ext,$mimetypes)){
return $mimetypes[$ext];
}else{
if(in_array($ext,$text) || in_array($ext,$code)){
return "text/plain";
}
return 'application/octet-stream';
}
}