From 5b07a03be6117db627d47e7db258c2b590f0a1a8 Mon Sep 17 00:00:00 2001 From: kuaifan Date: Fri, 27 Aug 2021 14:00:31 +0800 Subject: [PATCH] no message --- app/Http/Middleware/WebApi.php | 17 +++ app/Module/Base.php | 214 +++++++++++++-------------------- cmd | 8 ++ 3 files changed, 111 insertions(+), 128 deletions(-) diff --git a/app/Http/Middleware/WebApi.php b/app/Http/Middleware/WebApi.php index 1c96b72f..07fb7f5e 100644 --- a/app/Http/Middleware/WebApi.php +++ b/app/Http/Middleware/WebApi.php @@ -5,6 +5,8 @@ namespace App\Http\Middleware; @error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING); use Closure; +use Request; +use URL; class WebApi { @@ -20,6 +22,21 @@ class WebApi global $_A; $_A = []; + if (Request::input('__Access-Control-Allow-Origin') || Request::header('__Access-Control-Allow-Origin')) { + header('Access-Control-Allow-Origin:*'); + header('Access-Control-Allow-Methods:GET,POST,PUT,DELETE,OPTIONS'); + header('Access-Control-Allow-Headers:Content-Type, platform, platform-channel, token, release, Access-Control-Allow-Origin'); + } + + $APP_FORCE_URL_SCHEME = env('APP_FORCE_URL_SCHEME', 'auto'); + if ($APP_FORCE_URL_SCHEME == 'https' || $APP_FORCE_URL_SCHEME === true) { + URL::forceScheme('https'); + } elseif ($APP_FORCE_URL_SCHEME == 'http' || $APP_FORCE_URL_SCHEME === false) { + URL::forceScheme('http'); + } elseif (Request::header('x-forwarded-server-port', 80) == 443) { + URL::forceScheme('https'); + } + return $next($request); } } diff --git a/app/Module/Base.php b/app/Module/Base.php index ea9b645b..a33bde3d 100755 --- a/app/Module/Base.php +++ b/app/Module/Base.php @@ -95,7 +95,7 @@ class Base */ public static function is_cidr($cidr) { - if (strpos($cidr, '/') !== false) { + if (str_contains($cidr, '/')) { list($cidr, $netmask) = explode('/', $cidr, 2); if ($netmask > 32 || $netmask < 0 || trim($netmask) == '') { return false; @@ -406,12 +406,12 @@ class Base } $data = trim($data); if ($data == '') return $default; - if (strpos(strtolower($data), 'array') === 0 && strtolower($data) !== 'array') { + if (str_starts_with(strtolower($data), 'array') && strtolower($data) !== 'array') { @ini_set('display_errors', 'on'); @eval("\$array = $data;"); @ini_set('display_errors', 'off'); } else { - if (strpos($data, '{\\') === 0) { + if (str_starts_with($data, '{\\')) { $data = stripslashes($data); } $array = json_decode($data, true); @@ -743,12 +743,12 @@ class Base if (empty($str)) { return $str; } - if (substr($str, 0, 2) == "//" || - substr($str, 0, 7) == "http://" || - substr($str, 0, 8) == "https://" || - substr($str, 0, 6) == "ftp://" || - substr($str, 0, 1) == "/" || - substr(str_replace(' ', '', $str), 0, 11) == "data:image/" + if (str_starts_with($str, "//") || + str_starts_with($str, "http://") || + str_starts_with($str, "https://") || + str_starts_with($str, "ftp://") || + str_starts_with($str, "/") || + str_starts_with(str_replace(' ', '', $str), "data:image/") ) { return $str; } else { @@ -802,14 +802,14 @@ class Base if (preg_match($pattern, $content)) { preg_match_all($pattern, $content, $matchs); foreach ($matchs[3] as $index => $value) { - if (!(substr($value, 0, 7) == "http://" || - substr($value, 0, 8) == "https://" || - substr($value, 0, 6) == "ftp://" || - substr(str_replace(' ', '', $value), 0, 11) == "data:image/" + if (!(str_starts_with($value, "http://") || + str_starts_with($value, "https://") || + str_starts_with($value, "ftp://") || + str_starts_with(str_replace(' ', '', $value), "data:image/") )) { - if (substr($value, 0, 2) == "//") { + if (str_starts_with($value, "//")) { $value = "http:" . $value; - } elseif (substr($value, 0, 1) == "/") { + } elseif (str_starts_with($value, "/")) { $value = substr($value, 1); } $newValue = " -1, //图片高度 "ext" => $extension, //文件后缀名 ]; - if (in_array($extension, ['png', 'jpg', 'jpeg', 'gif'])) { - //图片尺寸 - $paramet = getimagesize($array['file']); - $array['width'] = $paramet[0]; - $array['height'] = $paramet[1]; - //原图压缩 - if ($param['scale'] && is_array($param['scale'])) { - list($width, $height) = $param['scale']; - if (($width > 0 && $array['width'] > $width) || ($height > 0 && $array['height'] > $height)) { - $cut = ($width > 0 && $height > 0) ? 1 : -1; - $cut = $param['scale'][2] ?? $cut; - //图片压缩 - $tmpFile = $array['file'] . '_tmp.jpg'; - if (Base::imgThumb($array['file'], $tmpFile, $width, $height, $cut)) { - $tmpSize = filesize($tmpFile); - if ($tmpSize > $fileSize) { - @unlink($tmpFile); - } else { - @unlink($array['file']); - rename($tmpFile, $array['file']); - } + //图片尺寸 + $paramet = getimagesize($array['file']); + $array['width'] = $paramet[0]; + $array['height'] = $paramet[1]; + //原图压缩 + if ($param['scale'] && is_array($param['scale'])) { + list($width, $height) = $param['scale']; + if (($width > 0 && $array['width'] > $width) || ($height > 0 && $array['height'] > $height)) { + $cut = ($width > 0 && $height > 0) ? 1 : -1; + $cut = $param['scale'][2] ?? $cut; + //图片压缩 + $tmpFile = $array['file'] . '_tmp.jpg'; + if (Base::imgThumb($array['file'], $tmpFile, $width, $height, $cut)) { + $tmpSize = filesize($tmpFile); + if ($tmpSize > $fileSize) { + @unlink($tmpFile); + } else { + @unlink($array['file']); + rename($tmpFile, $array['file']); } - //图片尺寸 - $paramet = getimagesize($array['file']); - $array['width'] = $paramet[0]; - $array['height'] = $paramet[1]; - //重命名 - if ($scaleName) { - $scaleName = str_replace(['{WIDTH}', '{HEIGHT}'], [$array['width'], $array['height']], $scaleName); - if (rename($array['file'], Base::rightDelete($array['file'], $fileName) . $scaleName)) { - $array['file'] = Base::rightDelete($array['file'], $fileName) . $scaleName; - $array['path'] = Base::rightDelete($array['path'], $fileName) . $scaleName; - $array['url'] = Base::rightDelete($array['url'], $fileName) . $scaleName; - } + } + //图片尺寸 + $paramet = getimagesize($array['file']); + $array['width'] = $paramet[0]; + $array['height'] = $paramet[1]; + //重命名 + if ($scaleName) { + $scaleName = str_replace(['{WIDTH}', '{HEIGHT}'], [$array['width'], $array['height']], $scaleName); + if (rename($array['file'], Base::rightDelete($array['file'], $fileName) . $scaleName)) { + $array['file'] = Base::rightDelete($array['file'], $fileName) . $scaleName; + $array['path'] = Base::rightDelete($array['path'], $fileName) . $scaleName; + $array['url'] = Base::rightDelete($array['url'], $fileName) . $scaleName; } } } - //生成缩略图 - $array['thumb'] = $array['path']; - if (Base::imgThumb($array['file'], $array['file'] . "_thumb.jpg", 180, 0)) { - $array['thumb'] .= "_thumb.jpg"; - } - $array['thumb'] = Base::fillUrl($array['thumb']); } + //生成缩略图 + $array['thumb'] = $array['path']; + if (Base::imgThumb($array['file'], $array['file'] . "_thumb.jpg", 180, 0)) { + $array['thumb'] .= "_thumb.jpg"; + } + $array['thumb'] = Base::fillUrl($array['thumb']); return Base::retSuccess('success', $array); } } @@ -2245,7 +2243,7 @@ class Base return Base::retError('错误的类型参数'); } $extension = strtolower($file->getClientOriginalExtension()); - if ($type && is_array($type) && !in_array($extension, $type)) { + if ($type && !in_array($extension, $type)) { return Base::retError('文件格式错误,限制类型:' . implode(",", $type)); } try { @@ -2294,20 +2292,12 @@ class Base $data = imagecreatefromstring(file_get_contents($array['file'])); $exif = @exif_read_data($array['file']); if (!empty($exif['Orientation'])) { - switch ($exif['Orientation']) { - case 8: - $data = imagerotate($data, 90, 0); - break; - case 3: - $data = imagerotate($data, 180, 0); - break; - case 6: - $data = imagerotate($data, -90, 0); - break; - default: - $data = null; - break; - } + $data = match ($exif['Orientation']) { + 8 => imagerotate($data, 90, 0), + 3 => imagerotate($data, 180, 0), + 6 => imagerotate($data, -90, 0), + default => null, + }; if ($data !== null) { imagejpeg($data, $array['file']); } @@ -2506,41 +2496,13 @@ class Base */ public static function extIcon($ext) { - $value = 'images/ext/file.png'; - switch ($ext) { - case "docx": - $value = 'images/ext/doc.png'; - break; - case "xlsx": - $value = 'images/ext/xls.png'; - break; - case "pptx": - $value = 'images/ext/ppt.png'; - break; - case "ai": - case "avi": - case "bmp": - case "cdr": - case "doc": - case "eps": - case "gif": - case "mov": - case "mp3": - case "mp4": - case "pdf": - case "ppt": - case "pr": - case "psd": - case "rar": - case "svg": - case "tif": - case "txt": - case "xls": - case "zip": - $value = 'images/ext/' . $ext . '.png'; - break; - } - return $value; + return match ($ext) { + "docx" => 'images/ext/doc.png', + "xlsx" => 'images/ext/xls.png', + "pptx" => 'images/ext/ppt.png', + "ai", "avi", "bmp", "cdr", "doc", "eps", "gif", "mov", "mp3", "mp4", "pdf", "ppt", "pr", "psd", "rar", "svg", "tif", "txt", "xls", "zip" => 'images/ext/' . $ext . '.png', + default => 'images/ext/file.png', + }; } /** @@ -2602,14 +2564,10 @@ class Base $arr = []; foreach ($args as $k => $v) { if (isset($args[$k + 1]) && $args[$k + 1]) { - switch ($k) { - case 0: - $arr[$k] = $pailie($v, $args[$k + 1]); - break; - default: - $arr[$k] = $pailie($arr[$k - 1], $args[$k + 1]); - break; - } + $arr[$k] = match ($k) { + 0 => $pailie($v, $args[$k + 1]), + default => $pailie($arr[$k - 1], $args[$k + 1]), + }; } } $key = count($arr) - 1; @@ -2618,14 +2576,14 @@ class Base /** * 获取当前是本月第几个星期 - * @return false|float + * @return float */ public static function getMonthWeek() { $time = strtotime(date("Y-m-01")); $w = date('w', $time); $j = date("j"); - return ceil(($j + $w) / 7); + return ceil(($j . $w) / 7); } /** diff --git a/cmd b/cmd index dd84bbf2..1fe914e9 100755 --- a/cmd +++ b/cmd @@ -171,6 +171,14 @@ if [ $# -gt 0 ];then env_set APP_DEBUG "true" fi supervisorctl_restart php + elif [[ "$1" == "https" ]]; then + shift 1 + if [[ "$@" == "auto" ]];then + env_set APP_FORCE_URL_SCHEME "auto" + else + env_set APP_FORCE_URL_SCHEME "true" + fi + supervisorctl_restart php elif [[ "$1" == "artisan" ]]; then shift 1 e="php artisan $@" && $COMPOSE exec php /bin/bash -c "$e"