获取客户端操作系统信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
if (!function_exists('get_os_info')) { /** * 获取客户端操作系统信息 * @param string $agent - 代理信息 * @param bool $type - 返回类型(true-数组|false字符串) * @return array|string|bool - 失败返回false,成功返回操作系统信息 */ function get_os_info($agent, $type = true) { $os = 'Unknown'; $version = ''; $ico = 'unknown'; if (preg_match('/win/i', $agent)) { // 判断是否为Windows $os = 'Windows'; $version = ''; $ico = 'windows'; if (preg_match('/Windows NT 6.1/i', $agent)) { $version = '7'; $ico = 'windows_win7'; } elseif (preg_match('/Windows NT 5.1/i', $agent)) { $version = 'XP'; } elseif (preg_match('/Windows NT 6.2/i', $agent)) { $version = '8'; $ico = 'windows_win8'; } elseif (preg_match('/Windows NT 6.3/i', $agent)) { $version = '8.1'; $ico = 'windows_win8'; } elseif (preg_match('/Windows NT 6.0/i', $agent)) { $version = 'Vista'; $ico = "windows_vista"; } elseif (preg_match('/Windows NT 5.2/i', $agent)) { if (preg_match('/Win64/i', $agent)) { $version = 'XP 64 bit'; } else { $version = 'Server 2003'; } } else if (preg_match('/nt 10.0/i', $agent)) { $version = '10'; } elseif (preg_match('/Windows Phone/i', $agent)) { $matches = explode(';', $agent); $version = $matches[2]; $ico = 'windows_phone'; } else if (preg_match('/nt/i', $agent)) { $version = 'NT'; } else if (preg_match('/32/i', $agent)) { $version = '32'; } else if (strpos($agent, '95')) { $version = '95'; } else if (strpos($agent, '4.90')) { $version = 'ME'; } else if (preg_match('/98/i', $agent)) { $version = '98'; } else if (preg_match('/nt 5/i', $agent)) { $version = '2000'; } } else if (preg_match('/Linux/i', $agent)) { // 判断是否 Linux $os = 'Linux'; $version = ''; $ico = 'linux'; if (preg_match('/Ubuntu/i', $agent)) { $version = 'Ubuntu'; $ico = 'ubuntu'; } elseif (preg_match('/Debian/i', $agent)) { $version = 'Debian'; $ico = 'debian'; } elseif (preg_match('/Fedora/i', $agent)) { $version = 'Fedora'; $ico = 'fedora'; } else if (preg_match("/(?<=Android )[\d\.]{1,}/", $agent, $versionInfo)) { $os = 'Android'; $version = $versionInfo[0]; $ico = 'android'; } } else if (preg_match('/iPod.*.CPU.([a-zA-Z0-9.( _)]+)/i', $agent, $matches)) { $os = 'iPod'; $version = $matches[1]; $ico = 'iphone'; } elseif (preg_match('/iPhone OS ([a-zA-Z0-9.( _)]*)/i', $agent, $matches)) { $os = 'iPhone'; $version = $matches[1]; $ico = 'iphone'; } elseif (preg_match('/iPad.*.CPU.([a-zA-Z0-9.( _)]+)/i', $agent, $matches)) { $os = 'iPad'; $version = $matches[1]; $ico = 'ipad'; } elseif (preg_match('/Mac OS X.([0-9. _]+)/i', $agent, $matches)) { if (count(explode(7, $matches[1])) > 1) { $matches[1] = 'Lion ' . $matches[1]; } else if (count(explode(8, $matches[1])) > 1) { $matches[1] = 'Mountain Lion ' . $matches[1]; } $os = 'Mac'; $version = 'OSX ' . $matches[1]; $ico = "macos"; } elseif (preg_match('/Macintosh/i', $agent)) { $os = 'Mac'; $version = 'OS'; $ico = 'macos'; } else if (preg_match('/CrOS/i', $agent)) { // 谷歌操作系统 $os = 'Google'; $version = 'Chrome OS'; $ico = 'chrome'; } if ($type === true) { return empty($os) ? false : ['os' => $os, 'version' => $version, 'ico' => $ico]; } else { return empty($os) ? false : $os . ' ' . $version; } } } |
获取客户端浏览器信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
if (!function_exists('get_browser_info')) { /** * 获取客户端浏览器信息 * @param string $agent - 代理信息 * @param bool $type - 返回类型(true-数组|false字符串) * @return array|string|bool - 失败返回false,成功返回浏览器信息 */ function get_browser_info($agent, $type = true) { $browser = 'Unknown'; $version = ''; $ico = 'unknown'; if (preg_match('/rv:(11.0)/i', $agent, $matches)) { $browser = 'Internet Explorer'; $version = $matches[1]; $ico = 'ie11'; } else if (preg_match('/MSIE ([a-zA-Z0-9.]+)/i', $agent, $matches)) { $browser = 'Internet Explorer'; $version = $matches[1]; if (strpos($matches[1], '7') !== false || strpos($matches[1], '8') !== false) { $ico = 'ie8'; } else if (strpos($matches[1], '9') !== false) { $ico = 'ie9'; } else if (strpos($matches[1], '10') !== false) { $ico = 'ie10'; } else { $ico = 'ie'; } } else if (preg_match('/Firefox\/([a-zA-Z0-9.]+)/i', $agent, $matches)) { $browser = 'Firefox'; $version = $matches[1]; $ico = 'firefox'; } else if (stripos($agent, "Edge") > 0) { //win10 Edge浏览器 添加了chrome内核标记 在判断Chrome之前匹配 preg_match("/Edge\/([\d\.]+)/", $agent, $reg); $browser = 'Edge'; $version = $reg[1]; $ico = 'ie11'; } else if (preg_match('/CriOS\/([a-zA-Z0-9.]+)/i', $agent, $matches)) { $browser = 'Chrome for iOS'; $version = $matches[1]; $ico = 'crios'; } else if (preg_match('/Opera.(.*)Version[ \/]([a-zA-Z0-9.]+)/i', $agent, $matches)) { $browser = 'Opera'; $ico = 'opera'; if (preg_match('/opera mini/i', $agent)) { $browser = 'Opera Mini'; $version = $matches[2]; } } else if (preg_match('/Maxthon( |\/)([a-zA-Z0-9.]+)/i', $agent, $matches)) { $browser = 'Maxthon'; $version = $matches[2]; $ico = 'maxthon'; } else if (preg_match('/360([a-zA-Z0-9.]+)/i', $agent, $matches)) { $browser = '360 Browser'; $version = $matches[1]; $ico = '360se'; } elseif (preg_match('/SE 2([a-zA-Z0-9.]+)/i', $agent, $matches)) { $browser = 'SouGou Browser 2'; $version = $matches[1]; $ico = 'sogou'; } elseif (preg_match('/UCWEB([a-zA-Z0-9.]+)/i', $agent, $matches)) { $browser = 'UCWEB'; $version = $matches[1]; $ico = 'ucweb'; } elseif (preg_match('/UBrowser\/([a-zA-Z0-9.]+)/i', $agent, $matches)) { $browser = 'UBrowser'; $version = $matches[1]; $ico = 'ucweb'; } elseif (preg_match('/Chrome\/([a-zA-Z0-9.]+)/i', $agent, $matches)) { $browser = 'Google Chrome'; $version = $matches[1]; $ico = 'chrome'; if (preg_match('/OPR\/([a-zA-Z0-9.]+)/i', $agent, $matches)) { $browser = 'Opera'; $version = $matches[1]; $ico = 'opera15'; if (preg_match('/opera mini/i', $agent)) { $browser = 'Opera Mini'; $version = $matches[1]; } } } elseif (preg_match('/Safari\/([a-zA-Z0-9.]+)/i', $agent, $matches)) { $browser = 'Safari ' . $matches[1]; $ico = 'safari'; } if ($type === true) { return empty($browser) ? false : ['browser' => $browser, 'version' => $version, 'ico' => $ico]; } else { return empty($browser) ? false : $browser . ' ' . $version; } } } |
根据QQ号获取昵称
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
if (!function_exists('get_qq_nickname')) { /** * 根据QQ号获取昵称 * @param $qq - QQ号 * @param string $default - 获取失败时的默认昵称 * @return string - 返回昵称 */ function get_qq_nickname($qq, $default = '') { $index = 6; // 验证是否为数字 if (!\think\facade\Validate::isNumber($qq)) { return $default; } // 获取昵称 $apiUrl = 'http://r.pengyou.com/fcg-bin/cgi_get_portrait.fcg?uins='; $data = curl_request($apiUrl . $qq); // 中文支持 $data = iconv('GBK', 'UTF-8', $data); if (!$data || !preg_match('/portraitCallBack\((.*?)\)/i', $data, $info) || !isset($info[1])) { return $default; } // 返回信息处理 $info = @json_decode($info[1], true); if (!is_array($info) || !isset($info[$qq][$index])) { return $default; } $nickname = $info[$qq][$index]; if (empty($nickname)) { return $default; } //过滤特殊字符(火星文等,QQ昵称很变态,你懂得) $nickname = preg_replace('/[^a-zA-Z0-9\x{4e00}-\x{9fa5}]/u', '', $nickname); return $nickname; } } |
CURL模拟发送GET/POST请求
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
if (!function_exists('curl_request')) { /** * CURL模拟发送GET/POST请求 * @param $url - 请求地址 * @param null $data - 请求数据(GET请求时为NULL) * @return bool|mixed */ function curl_request($url, $data = NULL) { /* 利用 Curl 完成 GET/POST 请求 */ $curl = curl_init(); /* curl 配置项 */ /* 请求 URL */ curl_setopt($curl, CURLOPT_URL, $url); $user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : ' Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0 FirePHP/0.7.4'; /* user_agent,请求代理信息 */ curl_setopt($curl, CURLOPT_USERAGENT, $user_agent); /* referer头,请求来源 */ curl_setopt($curl, CURLOPT_AUTOREFERER, true); /* post相关选项 */ if ($data !== NULL) { /* 是否为POST请求 */ curl_setopt($curl, CURLOPT_POST, true); /* 处理请求数据 */ curl_setopt($curl, CURLOPT_POSTFIELDS, $data); } /* 是否处理响应头 ->否 */ curl_setopt($curl, CURLOPT_HEADER, false); /* curl_exec()是否返回响应结果 ->是 */ curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); /* 发送请求 */ $response = curl_exec($curl); if (false === $response) { return false; } return $response; } } |
根据邮箱获取头像
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
if (!function_exists('get_gr_avatar')) { /** * 根据邮箱获取头像 * @param $email - Email * @param string $default - 获取失败时的默认图像 * @return string - 返回图像地址 */ function get_gr_avatar($email, $default = '') { // 验证是否为邮箱 if (!\think\facade\Validate::isEmail($email)) { return $default; } // 设置头像缓存路径 $DI = DIRECTORY_SEPARATOR; $path = BASE_ROOT . PUBLIC_NAME . $DI . 'uploads' . $DI . 'avatars' . $DI . 'gr' . $DI; $pathUrl = get_public_path() . 'uploads/avatars/gr/'; $imgName = md5($email) . '.png'; // 如果文件存在且未过期,直接返回返回文件路径 if (is_file($path . $imgName)) { return $pathUrl . $imgName; } // 文件不存在时获取远程图片 $avatarApi = 'https://cn.gravatar.com/avatar/'; $url = $avatarApi . md5($email) . '?s=100'; $api = new \files\Upload(); $img = $api->getRemoteFile($url, ['size' => 0, 'ext' => '']); if (!$img) { return $default; } // 判断目录是否存在,不存在时自动创建 if (!is_dir($path)) { if (!mkdir($path, 0777, true)) { return $default; } } // 保存文件的本地目录 if (!file_put_contents($path . $imgName, $img)) { return $default; } return $pathUrl . $imgName; } } |
根据QQ号获取头像
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
if (!function_exists('get_qq_avatar')) { /** * 根据QQ号获取头像 * @param $qq - QQ号 * @param string $default - 获取失败时的默认图像 * @return string - 返回图像地址 */ function get_qq_avatar($qq, $default = '') { // 验证是否为邮箱 if (!\think\facade\Validate::isNumber($qq)) { return $default; } // 设置头像缓存路径 $DI = DIRECTORY_SEPARATOR; $path = BASE_ROOT . PUBLIC_NAME . $DI . 'uploads' . $DI . 'avatars' . $DI . 'qq' . $DI; $pathUrl = get_public_path() . 'uploads/avatars/qq/'; $imgName = md5($qq) . '.png'; // 如果文件存在且未过期,直接返回返回文件路径 if (is_file($path . $imgName)) { return $pathUrl . $imgName; } // 文件不存在时获取远程图片 $avatarApi = 'https://q1.qlogo.cn/g?b=qq&nk='; $url = $avatarApi . $qq . '&s=100'; $api = new \files\Upload(); $img = $api->getRemoteFile($url, ['size' => 0, 'ext' => '']); if (!$img) { return $default; } // 判断目录是否存在,不存在时自动创建 if (!is_dir($path)) { if (!mkdir($path, 0777, true)) { return $default; } } // 保存文件的本地目录 if (!file_put_contents($path . $imgName, $img)) { return $default; } return $pathUrl . $imgName; } } |
远程图片抓取
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
<?php /** * ================================================================== * 文 件 名: Upload.php * 概 要: 文件上传处理 * 作 者: IT小强 * 创建时间: 2017/11/12 18:43 * 修改时间: * copyright (c) 2016 - 2017 www.xqitw.cn * ================================================================== */ namespace files; use think\Image; class Upload { /** * @var string - 返回信息 */ protected $message = ''; /** * 保存Base64图像 * @param $base64Img - 原始Base64图像 * @param $savePath - 保存路径 * @param $saveName -保存文件名 * @return bool */ public function saveBase64Img($base64Img, $savePath, $saveName) { if (empty($base64Img)) { $this->message = '请选择要上传的图片'; return false; } if (!preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64Img, $result)) { $this->message = '图片参数错误'; return false; } $search = $result[1]; $extension = '.' . $result[2]; /* 解码 base64 */ $img = base64_decode(str_replace($search, '', $base64Img)); /* 创建目录 */ if (!is_dir($savePath)) { if (!mkdir($savePath, 0700, true)) { $this->message = '创建文件目录失败'; return false; } } /* 保存图片 */ $input = file_put_contents($savePath . $saveName . $extension, $img); if (!$input) { $this->message = '图片保存失败'; return false; } $this->message = $extension; return true; } /** * 图像裁剪 * @param $tempDir - 原图目录 * @param $saveDir - 保存目录 * @param $saveName - 文件名 * @param $width - 宽 * @param $height - 高 * @return bool */ public function thumb($tempDir, $saveDir, $saveName, $width = 150, $height = 150) { if (!is_dir($saveDir)) { if (!mkdir($saveDir, 0700, true)) { $this->message = '创建文件保存目录失败'; return false; } } $thumb = Image::open($tempDir . $saveName) ->thumb($width, $height, Image::THUMB_SCALING) ->save($saveDir . $saveName); if (!$thumb) { $this->message = '图像裁剪失败'; return false; } $this->message = '图像裁剪成功'; return true; } /** * 抓取远程图片 * @param $url - 图片地址 * @param array $config - 上传配置['size'=>1023,'ext'=>'png,jpg'] * @return bool|string */ public function getRemoteFile($url, $config = []) { $imgUrl = str_replace("&", "&", htmlspecialchars($url)); //http开头验证 if (strpos($imgUrl, "http") !== 0) { $this->message = '远程图片路径错误'; return false; } preg_match('/(^https*:\/\/[^:\/]+)/', $imgUrl, $matches); $host_with_protocol = count($matches) > 1 ? $matches[1] : ''; // 判断是否是合法 url if (!filter_var($host_with_protocol, FILTER_VALIDATE_URL)) { $this->message = '远程图片路径不合法'; return false; } preg_match('/^https*:\/\/(.+)/', $host_with_protocol, $matches); $host_without_protocol = count($matches) > 1 ? $matches[1] : ''; // 此时提取出来的可能是 ip 也有可能是域名,先获取 ip $ip = gethostbyname($host_without_protocol); // 判断是否是私有 ip if (!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE)) { $this->message = '远程图片IP不正确'; return false; } //获取请求头并检测死链 $heads = get_headers($imgUrl, 1); if (!(stristr($heads[0], "200") && stristr($heads[0], "OK"))) { $this->message = '远程图片路径错误'; return false; } //格式验证(扩展名验证和Content-Type验证) $fileType = str_replace('.', '', strtolower(strrchr($imgUrl, '.'))); // ① 验证文件格式 $uploadFileType = get_array_data('ext', $config, config('upload_file_type')); if (!empty($uploadFileType)) { $uploadFileType = explode(',', $uploadFileType); if (!in_array($fileType, $uploadFileType)) { $this->message = '文件扩展名错误'; return false; } } //打开输出缓冲区并获取远程图片 ob_start(); $context = stream_context_create(['http' => ['follow_location' => false]]); readfile($imgUrl, false, $context); $img = ob_get_contents(); ob_end_clean(); $size = strlen($img); // ② 验证文件大小 $uploadFileSize = intval(get_array_data('size', $config, intval(config('upload_file_size')))); if ($uploadFileSize > 0) { $uploadFileSize = $uploadFileSize * 1024; if ($size > $uploadFileSize) { $this->message = '文件大小超出限制'; return false; } } return $img; } /** * 获取反馈信息 * @return string */ public function getMsg() { return $this->message; } } |
IP地址信息查询
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
if (!function_exists('ip_lookup')) { /** * IP地址信息查询 * @param string $ip - 待查询的IP,为空自动换取本地客户端IP * @return array|bool - 查询失败返回false,成功返回地址信息数组 */ function ip_lookup($ip = '') { $ip = empty($ip) ? get_client_ip() : $ip; $url = 'http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=' . $ip; $data = curl_request($url); if (!$data || !preg_match('/^({).*?(})$/', $data)) { return false; } $data = json_decode($data, true); $info = []; $info['country'] = get_array_data('country', $data, ''); $info['province'] = get_array_data('province', $data, ''); $info['city'] = get_array_data('city', $data, ''); $info['district'] = get_array_data('district', $data, ''); return $info; } } |
1 2 3 4 5 6 7 8 9 10 11 12 |
if (!function_exists('get_client_ip')) { /** * 获取客户端IP地址 * @param integer $type 返回类型 0 返回IP地址 1 返回IPV4地址数字 * @param boolean $adv 是否进行高级模式获取(有可能被伪装) * @return mixed */ function get_client_ip($type = 0, $adv = true) { return request()->ip($type, $adv); } } |
博客使用ThinkPHP框架,函数中借用部分框架已有的函数、方法(避免重复造轮子)。其他程序使用时请做一些必要的修改。
发布评论