2020-11-22 01:52:49 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Providers;
|
|
|
|
|
2021-11-01 08:24:42 +08:00
|
|
|
use BookStack\Uploads\ImageService;
|
2020-11-22 01:52:49 +08:00
|
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
|
use Illuminate\Support\ServiceProvider;
|
|
|
|
|
2022-09-27 09:48:05 +08:00
|
|
|
class ValidationRuleServiceProvider extends ServiceProvider
|
2020-11-22 01:52:49 +08:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Register our custom validation rules when the application boots.
|
|
|
|
*/
|
|
|
|
public function boot(): void
|
|
|
|
{
|
|
|
|
Validator::extend('image_extension', function ($attribute, $value, $parameters, $validator) {
|
2021-11-01 08:24:42 +08:00
|
|
|
$extension = strtolower($value->getClientOriginalExtension());
|
2021-11-01 21:26:02 +08:00
|
|
|
|
2021-11-01 08:24:42 +08:00
|
|
|
return ImageService::isExtensionSupported($extension);
|
2020-11-22 01:52:49 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
Validator::extend('safe_url', function ($attribute, $value, $parameters, $validator) {
|
|
|
|
$cleanLinkName = strtolower(trim($value));
|
|
|
|
$isJs = strpos($cleanLinkName, 'javascript:') === 0;
|
|
|
|
$isData = strpos($cleanLinkName, 'data:') === 0;
|
2021-06-26 23:23:15 +08:00
|
|
|
|
2020-11-22 01:52:49 +08:00
|
|
|
return !$isJs && !$isData;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|