)/m', '', $input); return $input; } /** * Assert That no Back Office url is present on txt * * @param string $field * @param string $text * @return string * @throws \Illuminate\Validation\ValidationException If url is a bo url. */ public static function assertNoInternalBoUrls(string $field, string $text): string { $routes = Route::getRoutes(); // * Url replace and email replace. $text = preg_replace_callback( // phpcs:ignore Generic.Files.LineLength.TooLong '/(?P(?:http[s]?:\/\/.)?(?:www\.)?[-a-zA-Z0-9@%._\+~#=]{2,256}\.[a-z]{2,6}\b(?:[-a-zA-Z0-9@:%_\+.~#?&\/\/=]*))/', function ($matches) use ($routes, $field) { $match = collect($matches)->first() ?? ''; $url = $match; try { $request = Request::create($url); // If a route is match then it do not throws exception. if ( \parse_url(config('app.url'), \PHP_URL_HOST) === \parse_url($url, \PHP_URL_HOST) && trim($routes->match($request)->getPrefix() ?? '', '/') === 'bo' ) { // Removes the matched url. throw ValidationException::withMessages([ $field => sprintf( '%s : Il est interdit de coller une url du back office dans ce champ', trans($field) ) ]); } return $match; } catch (NotFoundHttpException $e) { return $match; } }, $text ); return $text; } /** * Assert That only external url is present on txt * * @param string $field * @param string $text * @return string * @throws \Illuminate\Validation\ValidationException If url is a bo url. */ public static function assertNoInternalUrls(string $field, string $text): string { self::assertNoInternalBoUrls($field, $text); $routes = Route::getRoutes(); // * Url replace and email replace. $text = preg_replace_callback( // phpcs:ignore Generic.Files.LineLength.TooLong '/(?P(?:http[s]?:\/\/.)?(?:www\.)?[-a-zA-Z0-9@%._\+~#=]{2,256}\.[a-z]{2,6}\b(?:[-a-zA-Z0-9@:%_\+.~#?&\/\/=]*))/', function ($matches) use ($routes, $field) { $match = collect($matches)->first() ?? ''; $url = $match; try { $request = Request::create($url); // If a route is match then it do not throws exception. if ( \parse_url(config('app.url'), \PHP_URL_HOST) === \parse_url($url, \PHP_URL_HOST) && $routes->match($request) ) { // Removes the matched url. throw ValidationException::withMessages([ $field => sprintf( '%s : Il est interdit de coller une url de %s dans ce champ', trans($field), config('app.name') ) ]); } return $match; } catch (NotFoundHttpException $e) { return $match; } }, $text ); return $text; } /** * Generates an array from a dot separated string * * @param string $path * @param mixed $value * @param string $separator * @return array */ public static function dotStringToNestedArray(string $path, $value, string $separator = '.'): array { $arr = $value; $keys = array_reverse(explode($separator, $path)); foreach ($keys as $key) { $arr = [$key => $arr]; } return $arr; } /** * Destroy image * * @param \Illuminate\Database\Eloquent\Model $model * @param string $attribute * @return void */ public static function removeImage(Model $model, string $attribute) { $path = ltrim($model->{$attribute}, '/'); if (File::exists($path) and \strpos($path, 'storage') === 0) { File::delete($path); } } /** * Destroy image * * @param \Illuminate\Database\Eloquent\Model $model * @param string $attribute * @return void */ public static function removeOldImage(Model $model, string $attribute) { $originalPath = ltrim($model->getOriginal($attribute), '/'); if (File::exists($originalPath) and \strpos($originalPath, 'storage') === 0) { File::delete($originalPath); } } /** * Store an image of different types * * @param \Illuminate\Database\Eloquent\Model $model * @param \Illuminate\Http\UploadedFile|\SplFileInfo|string $image * @return string * @throws \ErrorException If file copy fails. */ public static function storeImage(Model $model, $image): string { $table = $model->getTable(); if (\is_object($image) and $image instanceof \Illuminate\Http\UploadedFile) { /** @var \Illuminate\Http\UploadedFile $image */ $newPath = self::prepareMoveFile($table, \filter_var( \strip_tags(\urldecode($image->getClientOriginalName())), \FILTER_SANITIZE_STRING )); $newPath = str_replace(storage_path('app'), '', $newPath); $image->storePubliclyAs( pathinfo($newPath, \PATHINFO_DIRNAME), pathinfo($newPath, \PATHINFO_BASENAME) ); return 'storage' . str_replace('/public', '', $newPath); } if (\is_object($image) and $image instanceof \SplFileInfo) { /** @var \SplFileInfo $image */ $newPath = self::prepareMoveFile($table, $image->getBaseName()); if (!File::copy($image->getRealPath(), $newPath)) { throw new \ErrorException(sprintf( 'Failed to copy file %s to %s', $image->getRealPath(), $newPath )); } return \ltrim(str_replace(\storage_path('app/public'), '/storage', $newPath), '/'); } return \ltrim(strval($image), '/'); } /** * Prepare storage path and return * the image full path * * @param string $table Model table name. * @param string $filename The basename for the file. * @return string */ private static function prepareMoveFile(string $table, string $filename): string { $pathFolder = \storage_path(sprintf('app/public/images/%s', $table)); if (!File::exists($pathFolder)) { File::makeDirectory($pathFolder, 0755, true); } $filename = sprintf('%s-%s', Str::uuid(), $filename); $fullPath = sprintf('%s/%s', $pathFolder, $filename); if (File::exists($fullPath)) { File::delete($fullPath); } return $fullPath; } }