diff --git a/app/Http/Controllers/Admin/NEditorController.php b/app/Http/Controllers/Admin/NEditorController.php index 680614f..f3de53b 100644 --- a/app/Http/Controllers/Admin/NEditorController.php +++ b/app/Http/Controllers/Admin/NEditorController.php @@ -12,6 +12,8 @@ use Illuminate\Http\Request; use Illuminate\Http\UploadedFile; use Illuminate\Support\Facades\Storage; +use Intervention\Image\Facades\Image; +use Intervention\Image\Exception\NotReadableException; class NEditorController extends Controller { @@ -78,13 +80,17 @@ public function catchImage(Request $request) return call_user_func([new $class, 'catchImage'], $request); } - $files = (array) $request->post('file'); + $files = array_unique((array) $request->post('file')); $urls = []; foreach ($files as $v) { - $extention = pathinfo(parse_url($v, PHP_URL_PATH), PATHINFO_EXTENSION); - $path = date('Ym') . '/' . md5($v) . '.' . ($extention == '' ? 'jpg' : $extention); + $image = $this->fetchImageFile($v); + if (!$image || !$image['extension'] || !$this->isAllowedImageType($image['extension'])) { + continue; + } + + $path = date('Ym') . '/' . md5($v) . '.' . $image['extension']; Storage::disk(config('light.neditor.disk')) - ->put($path, file_get_contents($v)); + ->put($path, $image['data']); $urls[] = [ 'url' => Storage::disk(config('light.neditor.disk'))->url($path), 'source' => $v, @@ -222,4 +228,52 @@ protected function isValidUploadedFile(UploadedFile $file, array $config) return true; } + + protected function fetchImageFile($url) + { + try { + if (!filter_var($url, FILTER_VALIDATE_URL)) { + return false; + } + + if (extension_loaded('curl')) { + $ch = curl_init(); + $options = [ + CURLOPT_URL => $url, + CURLOPT_RETURNTRANSFER => true, + CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.2 (KHTML, like Gecko) Chrome/22.0.1216.0 Safari/537.2' + ]; + curl_setopt_array($ch, $options); + $data = curl_exec($ch); + curl_close($ch); + if (!$data) { + return false; + } + + if (isWebp($data)) { + $image = Image::make(imagecreatefromwebp($url)); + $extension = 'webp'; + } else { + $image = Image::make($data); + } + } else { + $image = Image::make($url); + } + } catch (NotReadableException $e) { + return false; + } + + $mime = $image->mime(); + return [ + 'extension' => $extension ?? ($mime ? strtolower(explode('/', $mime)[1]) : ''), + 'data' => $data + ]; + } + + protected function isAllowedImageType($extension) + { + $c = config('light.neditor.upload'); + + return in_array('.' . $extension, $c['imageAllowFiles'], true); + } } diff --git a/app/functions.php b/app/functions.php index 4d2fdff..bebac63 100644 --- a/app/functions.php +++ b/app/functions.php @@ -188,3 +188,12 @@ function checkSensitiveWords(string $text, $type = 'join', $mode = null) } return array_merge($return, $result['exclusive']); } + +function isWebp($data) +{ + if (strncmp(substr($data, 8, 7), "WEBPVP8", 7) === 0) { + return true; + } + + return false; +} \ No newline at end of file