'删除', '0' => '禁用', '1' => '正常'], $arg = 0, $default = '未知') { if (array_key_exists($arg, $list)) { $value = $list[$arg]; } return isset($value) ? $value : $default; } /** * 格式化字节大小 * @param number $size 字节数 * @param string $delimiter 数字和单位分隔符 * @return string 格式化后的带单位的大小 */ function format_bytes($size, $delimiter = '', $decimal = -1) { $units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB']; for ($i = 0; $size >= 1024 && $i < 5; $i++) { $size /= 1024; } return ($decimal > -1 ? round($size, 2) : $size) . $delimiter . $units[$i]; } /** * 获取随机字符 * @param number $length 字符串长度 * @return string 返回随机字符 */ function getstr_random($length = 4) { $chars = array( "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" ); $charsLen = count($chars) - 1; shuffle($chars); $output = ""; for ($i = 0; $i < $length; $i++) { $output .= $chars[mt_rand(0, $charsLen)]; } return $output; } function getImage($filename, $width = -1, $height = -1, $type = 1, $savedir = 'allimg', $baseurl = '') { $docDir = request()->server('DOCUMENT_ROOT'); $rootDir = request()->root(); if ($width < 0 && $height < 0) { return $rootDir . $filename; } $directory = $docDir . $rootDir; $path_parts = pathinfo($filename); $cleanPath = ''; if ( !empty($path_parts) ) { if ( !empty($path_parts['dirname']) ) { $cleanPath = str_replace(array('/', '\\'), '', $path_parts['dirname']); } } if (!isset($path_parts['extension']) || empty($filename)) { $newfilename = $baseurl . '/uploads/' . $savedir . '/nopic' . $type . $cleanPath . $width . 'x' . $height . '.jpg'; if (is_file($directory . $newfilename) && is_file($directory . $filename) && filemtime($directory . $filename) < filemtime($directory . $newfilename)) { return $newfilename; } else { $imageObj = Image::open($directory . '/uploads/nopic.jpg'); if (!is_dir(dirname($directory . $newfilename))) { mkdir(dirname($directory . $newfilename), 0777, true); } $imageObj->thumb($width, $height, $type)->save($directory . $newfilename); if (is_file($directory . $newfilename)) { return $newfilename; } } } $newfilename = $baseurl . '/uploads/' . $savedir . '/' . $path_parts['filename'] . $type . $cleanPath . $width . 'x' . $height . '.' . $path_parts['extension']; if (is_file($directory . $newfilename) && is_file($directory . $filename) && filemtime($directory . $filename) < filemtime($directory . $newfilename)) { return $newfilename; } if (is_file($directory . $filename)) { $imageObj = Image::open($directory . $filename); if (!is_dir(dirname($directory . $newfilename))) { mkdir(dirname($directory . $newfilename), 0777, true); } $imageObj->thumb($width, $height, $type)->save($directory . $newfilename); if (is_file($directory . $newfilename)) { return $newfilename; } } return $baseurl . '/uploads/nopic.jpg'; } /* base64格式编码转换为图片并保存对应文件夹 */ function base64_image_content($base64_image_content,$path){ //匹配出图片的格式 if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)){ $type = $result[2]; $new_file = $path."/".date('Ymd',time())."/"; if(!file_exists($new_file)){ //检查是否有该文件夹,如果没有就创建,并给予最高权限 mkdir($new_file, 0700); } $new_file = $new_file.time().".{$type}"; if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_image_content)))){ return '/'.$new_file; }else{ return false; } }else{ return false; } } /* * 判断数组是关联数组还是数值数组 */ function checkAssocArray($arr) { $index = 0; foreach (array_keys($arr) as $key) { if ($index++ != $key) { return false; } } return true; } /** * 节点遍历 * @param $list * @param string $pk * @param string $pid * @param string $child * @param int $root * return array */ function list_to_tree($list, $pk = 'id', $pid = 'pid', $child = '_child', $root = 0) { // 创建Tree $tree = []; if (is_array($list)) { // 创建基于主键的数组引用 $refer = []; foreach ($list as $key => $data) { $refer[$data[$pk]] = & $list[$key]; } foreach ($list as $key => $data) { // 判断是否存在parent $parentId = $data[$pid]; if ($root == $parentId) { $tree[] = & $list[$key]; } else { if (isset($refer[$parentId])) { $parent = & $refer[$parentId]; $parent[$child][] = & $list[$key]; } } } } return $tree; } function getcposition($ip){ try { $res1 = file_get_contents("http://ip.taobao.com/service/getIpInfo.php?ip=$ip"); $res1 = json_decode($res1,true); if ($res1[ "code"]==0){ return $res1['data']["country"].$res1['data'][ "region"].$res1['data']["city"]."_".$res1['data'][ "isp"]; }else{ return "未能获取"; } }catch (Exception $e){ return "未能获取"; } } function tiaoshi($obj) { header("content-type: text/html; charset=utf8"); dump(json_decode(json_encode($obj), true)); } function get_ip() { if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) { $ip = getenv('HTTP_CLIENT_IP'); } elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) { $ip = getenv('HTTP_X_FORWARDED_FOR'); } elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) { $ip = getenv('REMOTE_ADDR'); } elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) { $ip = $_SERVER['REMOTE_ADDR']; } return preg_match( '/[\d\.]{7,15}/', $ip, $matches ) ? $matches [0] : ''; } //CURL POST请求 function https_request($url, $data = null) { $apiUrl = "$url";//print_r($apiUrl);die; $oCurl = curl_init(); if(stripos($apiUrl,"https://")!==FALSE){ curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false); } curl_setopt($oCurl,CURLOPT_TIMEOUT,5); curl_setopt ($oCurl, CURLOPT_HEADER, 0); curl_setopt($oCurl, CURLOPT_HTTPHEADER, []); curl_setopt($oCurl, CURLOPT_URL, $apiUrl); curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt($oCurl, CURLOPT_FOLLOWLOCATION, 3); $sContent = curl_exec($oCurl); $aStatus = curl_getinfo($oCurl); curl_close($oCurl); if(intval($aStatus["http_code"])==200){ return $sContent; }else{ return false; } } function CurlRequest($url,$data=null,$header=null){ //初始化浏览器 $ch = curl_init(); //设置浏览器,把参数url传到浏览器的设置当中 curl_setopt($ch, CURLOPT_URL, $url); //以字符串形式返回到浏览器当中 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //禁止https协议验证域名,0就是禁止验证域名且兼容php5.6 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); //禁止https协议验证ssl安全认证证书 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //判断data是否有数据,如果有data数据传入那么就把curl的请求方式设置为POST请求方式 if ( !empty($data) ) { //设置POST请求方式 @curl_setopt($ch, CURLOPT_POST, true); //设置POST的数据包 @curl_setopt($ch, CURLOPT_POSTFIELDS, $data); } //设置header头 if ( !empty($header) ) { curl_setopt($ch, CURLOPT_HTTPHEADER, $header); } //让curl发起请求 $str = curl_exec($ch); //关闭curl浏览器 curl_close($ch); //把请求回来的数据返回 return $str; } function upload_headimg($form_name, $allow_size=1048576, $allow_type=['jpg', 'png'], $path = '/uploads/customer/headimg/') { $image_info = $_FILES[$form_name]; if ($image_info['size'] > $allow_size) return ['code' => -1, 'msg' => '上传文件大小不能超过' . $allow_size / (1024 * 1024) . 'M']; $image_detail = @getimagesize($image_info['tmp_name']); if (!$image_detail) return ['code' => -2, 'msg' => '上传文件不是有效的图片类型']; switch ($image_detail[2]) { case 2: $image_ext = '.jpg'; break; case 3: $image_ext = '.png'; break; default: $image_ext = ''; break; } if ($image_ext == '') { $allow_type_str = ''; foreach ($allow_type as $key => $value) { $allow_type_str .= $value . ','; } return ['code' => -3, 'msg' => '请上传' . rtrim($allow_type_str, ',') . '类型的图片']; } $new_image_name = get_unique_img_name() . $image_ext; $path1 = dirname(__DIR__) . '/public' . $path; if (!@move_uploaded_file($image_info['tmp_name'], $path1 . $new_image_name)) return ['code' => -4, '上传图像失败']; return ['code' => 200, 'msg' => 'ok', 'data' => $path . $new_image_name]; } function get_unique_img_name() { $arr1 = range('a', 'z'); $arr2 = range('A', 'Z'); $arr3 = range('0', '9'); $arr = array_merge($arr1, $arr2, $arr3); shuffle($arr); $img_name = ''; for ($i=0; $i<15; $i++) { $img_name .= $arr[$i]; } $img_name .= time(); return $img_name; } if (!function_exists('mb_str_split')) { function mb_str_split($str){ return preg_split('/(? 0) { preg_match("/Firefox\/([^;)]+)+/i", $sys, $b); $exp[0] = "Firefox"; $exp[1] = $b[1]; //获取火狐浏览器的版本号 } elseif (stripos($sys, "Maxthon") > 0) { preg_match("/Maxthon\/([\d\.]+)/", $sys, $aoyou); $exp[0] = "傲游"; $exp[1] = $aoyou[1]; } elseif (stripos($sys, "MSIE") > 0) { preg_match("/MSIE\s+([^;)]+)+/i", $sys, $ie); $exp[0] = "IE"; $exp[1] = $ie[1]; //获取IE的版本号 } elseif (stripos($sys, "OPR") > 0) { preg_match("/OPR\/([\d\.]+)/", $sys, $opera); $exp[0] = "Opera"; $exp[1] = $opera[1]; } elseif(stripos($sys, "Edge") > 0) { //win10 Edge浏览器 添加了chrome内核标记 在判断Chrome之前匹配 preg_match("/Edge\/([\d\.]+)/", $sys, $Edge); $exp[0] = "Edge"; $exp[1] = $Edge[1]; } elseif (stripos($sys, "Chrome") > 0) { preg_match("/Chrome\/([\d\.]+)/", $sys, $google); $exp[0] = "Chrome"; $exp[1] = $google[1]; //获取google chrome的版本号 } elseif(stripos($sys,'rv:')>0 && stripos($sys,'Gecko')>0){ preg_match("/rv:([\d\.]+)/", $sys, $IE); $exp[0] = "IE"; $exp[1] = $IE[1]; }else { $exp[0] = "未知浏览器"; $exp[1] = ""; } return $exp[0].'('.$exp[1].')'; } //获取 客户通过来源渠道搜索关键词 function getKeywords($url){ $spier=array( 'baidu.'=>'百度', 'google.'=>'谷歌', 'soso.'=>'搜搜', 'sogou.'=>'搜狗', 'bing'=>'必应', 'www.so.com'=>'360' ); $q=array( '百度'=>'/wd=([^&]*)/i', '谷歌'=>'/q=([^&]*)/i', '360'=>'/q=(.*)/i', '搜狗'=>'/query=([^&]*)/i', '搜狗'=>'/q=([^&]*)/i', '搜搜'=>'/w=([^&]*)/i' ); $feed = array(); $feed['channel'] = ''; $feed['search'] = ''; foreach($spier as $k=>$v){ if(strpos($url,$k)){ preg_match("{$q[$v]}",$url,$b); if($v=='搜搜'||$v=='搜狗'){ $keywords=iconv('GBK','UTF-8',urldecode($b[1])); }else{ $keywords=urldecode($b[1]); } $feed['channel'] = $v; $feed['search'] = $keywords; } } return $feed; } // 生成唯一标识码 function createGuid() { if (function_exists('com_create_guid') === true){ return trim(com_create_guid(), '{}'); } $data = openssl_random_pseudo_bytes(16); $data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0100 $data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10 return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4)); } //获取 IP地址解析生成国家城市 function GetIpLookup($ip = ''){ if(empty($ip)){ $ip = GetIp(); } $content = @file_get_contents('http://ip-api.com/json/'. $ip.'?lang=en'); if(empty($content)){ return ''; } $json = json_decode($content); if(!property_exists($json, 'country')){ return ''; } $address = $json->{'country'}; $return['country'] = $address = $json->{'country'}; $return['province'] = $address = $json->{'regionName'}; $return['city'] = $address = $json->{'city'}; $return['isp'] = $address = $json->{'isp'}; return $return; } // 判断是否手机端官网 function isMobile() { // 如果有HTTP_X_WAP_PROFILE则一定是移动设备 if (isset($_SERVER['HTTP_X_WAP_PROFILE'])) { return true; } // 如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息 if (isset($_SERVER['HTTP_VIA'])) { // 找不到为flase,否则为true return stristr($_SERVER['HTTP_VIA'], "wap") ? true : false; } // 脑残法,判断手机发送的客户端标志,兼容性有待提高。其中'MicroMessenger'是电脑微信 if (isset($_SERVER['HTTP_USER_AGENT'])) { $clientkeywords = array('nokia','sony','ericsson','mot','samsung','htc','sgh','lg','sharp','sie-','philips','panasonic','alcatel','lenovo','iphone','ipod','blackberry','meizu','android','netfront','symbian','ucweb','windowsce','palm','operamini','operamobi','openwave','nexusone','cldc','midp','wap','mobile','MicroMessenger'); // 从HTTP_USER_AGENT中查找手机浏览器的关键字 if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))) { return true; } } // 协议法,因为有可能不准确,放到最后判断 if (isset ($_SERVER['HTTP_ACCEPT'])) { // 如果只支持wml并且不支持html那一定是移动设备 // 如果支持wml和html但是wml在html之前则是移动设备 if ((strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === false || (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html')))) { return true; } } return false; } //提交 访问日志 function visitLog(){ $useragent = $_SERVER['HTTP_USER_AGENT']; $client_info = getOs($useragent) . "---" . getBroswer($useragent); $ip = get_ip(); $address = GetIpLookup($ip); $data = array( //"items" => 'ORICO Official', //"forum" => $this->request->controller(), "forum_code" => createGuid(), //"url" => $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'], //"refer" => $referer, //"channel" => $channel, //"keyword" => $keyword, // "start_time" => date("y-m-d H:i:s"), "ip" => $ip, "country" => isset($address['country']) ? $address['country'] : '', "province" => isset($address['province']) ? $address['province'] : '', "city" => isset($address['city']) ? $address['city'] : '', "drive" => isMobile() ? "移动端": "PC", "system" => getOs($useragent), "brower" => getBroswer($useragent), ); return $data; } function pregReplaceImg($content,$prefix) { $contentAlter = preg_replace_callback("/(src)=([\"|']?)([^ \"'>]+\.(gif|jpg|jpeg|bmp|png))\\2/i", function($match)use($prefix){ if(strstr($match[1], 'http://')==false && strstr($match[1], 'https://')==false) { return $str = 'src="'.$prefix.substr($match[3], 6, strlen($match[3])).'"'; } else { return 'src="'.$match[3].'"'; } } , $content); return $contentAlter; } //监控采集访客浏览历史数据 function setMomitorFeed($controller,$uuid){ $useragent = $_SERVER['HTTP_USER_AGENT']; $ip = get_ip(); $address = GetIpLookup($ip); //echo $ip."
=="; print_r($address);die;
$url = getpageurl();
$keywords =getKeywords($url);
$curUrl = parse_url($url);
$host = $curUrl['host'];
$data = array(
"items" => 'ORICO Official',
"forum" => $controller,
"forum_code" => $controller,
"uuid" => $uuid,
"url" => $url,
"refer" => isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : $url,
"channel" => isset($keywords['channel']) ? $keywords['channel'] : $host,
"channel_type" => isset($keywords['channel']) ?$keywords['channel'] :'1',
"keyword" => isset($keywords['search']) ?$keywords['search'] :'',
"start_time" => date("y-m-d H:i:s"),
"ip" => $ip,
"country" => isset($address['country']) ? $address['country'] :'',
"province" => isset($address['province']) ? $address['province'] :'',
"city" => isset($address['city']) ? $address['city'] :'',
"drive" => isMobile() ? "H5": "PC",
"system" => getOs($useragent),
"brower" => getBroswer($useragent),
);
$data = http_build_query($data);
$result = CurlRequest('https://producer.datamaster.cc/api/v1/browser_logs/send', $data);
$res = json_decode($result, true);
return $res;
}
function qu($url){
$query = parse_url($url);
$var = array_values(array_unique(explode("&",$query['query'])));
$shu = count($var);
$qian = array();
for($i=0; $i<$shu; $i++){
$qian[] = substr($var[$i],0,strpos($var[$i], '='));
}
$hou = array();
for($i=0; $i<$shu; $i++){
$hou[] = trim(strrchr($var[$i], '='),'=');
}
for($i=0; $i<$shu; $i++){
$arr[] = array($qian[$i]=>$hou[$i]);
}
$arr = array_reduce($arr, 'array_merge', array());
$arr = http_build_query($arr);
$url = $query['path'] .'?'. $arr;
return $url;
}
?>