<?php namespace App\Http\Controllers\Common; use QrCode; use Session; use App\AppsVersions; use App\Apps; class UtilController { /** * 保存icon 到对应的应用下面 * @param [type] $icon [description] * @return [type] [description] */ public static function saveAppIcon($imgfile,$appid) { $targetDir = "uploads/icons/".$appid; UtilController::createDir($targetDir); list($type, $imgfile) = explode(';', $imgfile); list(, $imgfile) = explode(',', $imgfile); $imgfile = base64_decode($imgfile); file_put_contents($targetDir.'/'.$appid.'.png', $imgfile); } /** * 获取 登录信息 * @return 1 pincode 2 账号登录 user_id 0 未登陆或者Session 过期 */ public static function getLoginInfo() { return (Session::has('type') && in_array(Session::get('type'),[1,2])) ? Session::get('type') : 0; } public static function getSessionPincode() { return Session::get('pincode'); } public static function getSessionUserId() { return Session::get('pincode'); } /** * 创建 appversion 二维码 * qrcodeurl 二维码地址 * appverssionid appversion 编号 */ public static function createQrcode($qrcodeurl,$appverssionid) { if(!file_exists(public_path('qrcodes'))){ mkdir(public_path('qrcodes')); } QrCode::format("png")->size(200)->generate($qrcodeurl, public_path('qrcodes/'.$appverssionid.'.png')); } public static function createDir($dir) { return is_dir($dir) or (UtilController::createDir(dirname($dir)) and mkdir($dir, 0777)); } /** * 删除指定文件 * filepath 文件路径 images/uploads/appversions/test.apk * @return 1 成功 0 失败 */ public static function deleteFile($filepath) { $rs = unlink($filepath); return empty($rs)?0:1; } /** * $type 1 pincode 组 icon] * $type 2 app icon * $type 3 app verson 版本上传 * $type 4 重新创建app version 备份 */ public static function filecrate($type,$param) { $dirfile=''; switch ($type) { case '1': $dirfile = 'pincodeGroup/'.date('Ymd'); break; case '2': # code... $dirfile = empty($param)?'icons':'icons/'.$param; break; case '3': # code... $dirfile = 'appsversions/'.date('Ymd'); break; } $targetDir = "uploads/".$dirfile; UtilController::createDir($targetDir); $cleanupTargetDir = true; // Remove old files $maxFileAge = 5 * 3600; // Temp file age in seconds $fileName = $_FILES["file"]["name"]; $filePath = $targetDir . DIRECTORY_SEPARATOR . $fileName; $chunk = isset($_REQUEST["chunk"]) ? intval($_REQUEST["chunk"]) : 0; $chunks = isset($_REQUEST["chunks"]) ? intval($_REQUEST["chunks"]) : 0; if ($cleanupTargetDir) { if (!is_dir($targetDir) || !$dir = opendir($targetDir)) { die('{"jsonrpc" : "2.0", "error" : {"code": 100, "message": "Failed to open temp directory."}, "id" : "id"}'); } while (($file = readdir($dir)) !== false) { $tmpfilePath = $targetDir . DIRECTORY_SEPARATOR . $file; if ($tmpfilePath == "{$filePath}.part") { continue; } if (preg_match('/\.part$/', $file) && (filemtime($tmpfilePath) < time() - $maxFileAge)) { @unlink($tmpfilePath); } } closedir($dir); } if (!$out = @fopen("{$filePath}.part", $chunks ? "ab" : "wb")) { die('{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}'); } if (!empty($_FILES)) { if ($_FILES["file"]["error"] || !is_uploaded_file($_FILES["file"]["tmp_name"])) { die('{"jsonrpc" : "2.0", "error" : {"code": 103, "message": "Failed to move uploaded file."}, "id" : "id"}'); } if (!$in = @fopen($_FILES["file"]["tmp_name"], "rb")) { die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}'); } } else { if (!$in = @fopen("php://input", "rb")) { die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}'); } } while ($buff = fread($in, 4096)) { fwrite($out, $buff); } @fclose($out); @fclose($in); if (!$chunks || $chunk == $chunks - 1) { rename("{$filePath}.part", $filePath); } $url = $dirfile.'/'.$fileName; return $url; } /** * 是否移动端访问访问 * * @return bool */ public static 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; } // 脑残法,判断手机发送的客户端标志,兼容性有待提高 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' ); // 从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; } }