<?phpnamespace 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; } }