Files
2025-01-20 09:57:40 +08:00

860 lines
28 KiB
PHP
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
use think\Request;
use think\Loader;
use image\Image;
/**
* 字符串截取,支持中文和其他编码
* @param string $str 需要转换的字符串
* @param int|string $start 开始位置
* @param string $length 截取长度
* @param string $charset 编码格式
* @param bool|string $suffix 截断显示字符
* @return string
*/
function msubstr($str, $start = 0, $length = 0, $charset = "utf-8", $suffix = '...') {
if (function_exists("mb_substr")) {
$len = mb_strlen($str, $charset);
if ($len <= $length) {
return $str;
}
$slice = mb_substr($str, $start, $length, $charset);
} elseif (function_exists('iconv_substr')) {
$len = iconv_strlen($str, $charset);
if ($len <= $length) {
return $str;
}
$slice = iconv_substr($str, $start, $length, $charset);
if (false === $slice) {
$slice = '';
$len = 0;
}
} 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);
$len = count($match[0]);
if ($len <= $length) {
return $str;
}
$slice = join("", array_slice($match[0], $start, $length));
}
return $suffix ? $slice . $suffix : $slice;
}
if (!function_exists('_lang_')) {
/**
* 获取语言变量值
* @param string $name 语言变量名
* @param array $vars 动态变量值
* @param string $lang 语言
* @return mixed
*/
function _lang_($name, $vars = [], $lang = '') {
if (is_numeric($name) || !$name)
return $name;
if (!is_array($vars)) {
$vars = func_get_args();
array_shift($vars);
$lang = '';
}
return \think\Lang::get($name, $vars, $lang);
}
}
// 应用公共文件
function data_auth_sign($data, $signature = '') {
//数据类型检测
if (!is_array($data)) {
$data = (array) $data;
}
//$data['signature'] = $signature;
ksort($data); //排序
$code = http_build_query($data); //url编码并生成query字符串
$sign = md5($code); //生成签名
return $sign;
}
function is_session_login($key = 'session', $signature = '') {
$row = session($key . '_auth');
if (empty($row)) {
return 0;
} else {
return session($key . '_auth_sign') === data_auth_sign($row, $signature) ? $row['id'] : 0;
}
}
/**
* 从数组中取出索引项
* @param array|type $list 数组
* @param type $arg 参数
* @return string
*/
function array_switch($list = ['-1' => '删除', '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 = '') {
// ini_set('memory_limit',-1);
$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('/(?<!^)(?!$)/u', $str );
}
}
/**
*生成不重复的随机数
*@param int $start 需要生成的数字开始范围
*@param int $end 结束范围
*@param int $lengt 需要生成的随机数个数
*@return array 生成的随机数
*/
function get_rand_number($start=1,$end=10,$length=4){
$connt=0;
$temp=array();
while($connt<$length){
$temp[]=mt_rand($start,$end);
$data=array_unique($temp);
$connt=count($data);
}
sort($data);
return $data;
}
function GetPageUrl(){
// 判断是否https
$protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443) ? "https://": "http://"; //组合url
$url = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
return $url;
}
//base64 文件上传(图片、视频)
function SaveBase64($base64_image_content, $allow_size=1048576, $allow_type=['jpg', 'png'], $path = '/uploads/attachment/')
{
//匹配出图片的格式
if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)){
$type = $result[2];
$data['type']=$type;
$file_name=date('His').str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT).".".$type;
$file = $path."img/".date('Ymd',time());
$file_path = ROOT_PATH.$file;
$file_web_url = $file.'/'.$file_name;
$file_real_url = $file_path.'/'.$file_name;
if (!file_exists($file_path)){
mkdir($file_path, 0777, true);
//fopen($file_path.'\\'.$file_name, "w");
}
//解码
$decode=base64_decode(str_replace($result[1], '', $base64_image_content));
if (file_put_contents($file_real_url, $decode)){
$data['code']=200;
//$protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443) ?"https://": "http://";
//$data['attr_url']=$protocol .$_SERVER['HTTP_HOST'].'/'.$file_web_url;
$data['attr_url']=str_replace('public','',$file_web_url);
$data['msg']='附件保存成功!';
}else{
$data['code']=-1;
$data['attr_url']='';
$data['msg']='附件保存失败!';
}
}
elseif(preg_match('/^(data:\s*video\/(\w+);base64,)/', $base64_image_content, $result)){
$type = $result[2];
$file_name=date('His').str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT).".".$type;
$file = $path."video/".date('Ymd',time());
$file_path = ROOT_PATH.$file;
$file_web_url = $file.'/'.$file_name;
$file_real_url = $file_path.'/'.$file_name;
if (!file_exists($file_path)){
mkdir($file_path, 0777, true);
//fopen($file_path.'\\'.$file_name, "w");
}
//echo $file_path; die;
$data['type']=$type;
//解码
$decode=base64_decode(str_replace($result[1], '', $base64_image_content));
if (file_put_contents($file_real_url, $decode)){
$data['code']=200;
//$protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443) ?"https://": "http://";
$data['attr_url']=str_replace('public','',$file_web_url);
$data['msg']='附件保存成功!';
}else{
$data['code']=-1;
$data['attr_url']='';
$data['msg']='附件保存失败!';
}
}
else{
$data['type']='';
$data['code']=-3;
$data['url']='';
$data['msg']='base64附件格式有误';
}
return $data;
}
//获取客户端类型,手机还是电脑,以及相应的操作系统类型
function getOs($agent) {
$os = false;
if (preg_match ( '/win 9x/i', $agent ) && strpos ( $agent, '4.90' )) {
$os = 'Windows ME';
} else if (preg_match ( '/win/i', $agent ) && preg_match ( '/98/i', $agent )) {
$os = 'Windows 98';
} else if (preg_match ( '/win/i', $agent ) && preg_match ( '/nt 6.0/i', $agent )) {
$os = 'Windows Vista';
} else if (preg_match ( '/win/i', $agent ) && preg_match ( '/nt 6.1/i', $agent )) {
$os = 'Windows 7';
} else if (preg_match ( '/win/i', $agent ) && preg_match ( '/nt 6.2/i', $agent )) {
$os = 'Windows 8';
} else if (preg_match ( '/win/i', $agent ) && preg_match ( '/nt 10.0/i', $agent )) {
$os = 'Windows 10'; // 添加win10判断
} else if (preg_match ( '/win/i', $agent ) && preg_match ( '/nt 5.1/i', $agent )) {
$os = 'Windows XP';
} else if (preg_match ( '/win/i', $agent ) && preg_match ( '/nt 5/i', $agent )) {
$os = 'Windows 2000';
} else if (preg_match ( '/win/i', $agent ) && preg_match ( '/nt/i', $agent )) {
$os = 'Windows NT';
} else if (preg_match ( '/win/i', $agent ) && preg_match ( '/32/i', $agent )) {
$os = 'Windows 32';
} else if (preg_match ( '/win/i', $agent ) && strpos ( $agent, '95' )) {
$os = 'Windows 95';
} else if (preg_match ( '/linux/i', $agent )) {
if(preg_match("/Mobile/", $agent)){
if(preg_match("/QQ/i", $agent)){
$os = "Android QQ Browser";
}else{
$os = "Android Browser";
}
}else{
$os = 'PC-Linux';
}
} else if (preg_match ( '/Mac/i', $agent )) {
if(preg_match("/Mobile/", $agent)){
if(preg_match("/QQ/i", $agent)){
$os = "IPhone QQ Browser";
}else{
$os = "IPhone Browser";
}
}else{
$os = 'Mac OS X';
}
} else if (preg_match ( '/unix/i', $agent )) {
$os = 'Unix';
} else if (preg_match ( '/sun/i', $agent ) && preg_match ( '/os/i', $agent )) {
$os = 'SunOS';
} else if (preg_match ( '/ibm/i', $agent ) && preg_match ( '/os/i', $agent )) {
$os = 'IBM OS/2';
} else if (preg_match ( '/Mac/i', $agent ) && preg_match ( '/PC/i', $agent )) {
$os = 'Macintosh';
} else if (preg_match ( '/PowerPC/i', $agent )) {
$os = 'PowerPC';
} else if (preg_match ( '/AIX/i', $agent )) {
$os = 'AIX';
} else if (preg_match ( '/HPUX/i', $agent )) {
$os = 'HPUX';
} else if (preg_match ( '/NetBSD/i', $agent )) {
$os = 'NetBSD';
} else if (preg_match ( '/BSD/i', $agent )) {
$os = 'BSD';
} else if (preg_match ( '/OSF1/i', $agent )) {
$os = 'OSF1';
} else if (preg_match ( '/IRIX/i', $agent )) {
$os = 'IRIX';
} else if (preg_match ( '/FreeBSD/i', $agent )) {
$os = 'FreeBSD';
} else if (preg_match ( '/teleport/i', $agent )) {
$os = 'teleport';
} else if (preg_match ( '/flashget/i', $agent )) {
$os = 'flashget';
} else if (preg_match ( '/webzip/i', $agent )) {
$os = 'webzip';
} else if (preg_match ( '/offline/i', $agent )) {
$os = 'offline';
} else {
$os = '未知操作系统';
}
return $os;
}
//获取 客户端的浏览器类型
function getBroswer($sys){
if (stripos($sys, "Firefox/") > 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."<pre>=="; 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;
}
?>