1 <?php namespace BookStack\Providers;
4 use BookStack\Entities\Book;
5 use BookStack\Entities\Bookshelf;
6 use BookStack\Entities\BreadcrumbsViewComposer;
7 use BookStack\Entities\Chapter;
8 use BookStack\Entities\Page;
9 use BookStack\Settings\Setting;
10 use BookStack\Settings\SettingService;
11 use Illuminate\Database\Eloquent\Relations\Relation;
12 use Illuminate\Support\Facades\View;
13 use Illuminate\Support\ServiceProvider;
18 class AppServiceProvider extends ServiceProvider
21 * Bootstrap any application services.
25 public function boot()
28 $appUrl = config('app.url');
30 $isHttps = (strpos($appUrl, 'https://') === 0);
31 URL::forceRootUrl($appUrl);
32 URL::forceScheme($isHttps ? 'https' : 'http');
35 // Custom validation methods
36 Validator::extend('image_extension', function ($attribute, $value, $parameters, $validator) {
37 $validImageExtensions = ['png', 'jpg', 'jpeg', 'gif', 'webp'];
38 return in_array(strtolower($value->getClientOriginalExtension()), $validImageExtensions);
41 Validator::extend('no_double_extension', function ($attribute, $value, $parameters, $validator) {
42 $uploadName = $value->getClientOriginalName();
43 return substr_count($uploadName, '.') < 2;
46 Validator::extend('safe_url', function ($attribute, $value, $parameters, $validator) {
47 $cleanLinkName = strtolower(trim($value));
48 $isJs = strpos($cleanLinkName, 'javascript:') === 0;
49 $isData = strpos($cleanLinkName, 'data:') === 0;
50 return !$isJs && !$isData;
53 // Custom blade view directives
54 Blade::directive('icon', function ($expression) {
55 return "<?php echo icon($expression); ?>";
58 Blade::directive('exposeTranslations', function ($expression) {
59 return "<?php \$__env->startPush('translations'); ?>" .
60 "<?php foreach({$expression} as \$key): ?>" .
61 '<meta name="translation" key="<?php echo e($key); ?>" value="<?php echo e(trans($key)); ?>">' . "\n" .
62 "<?php endforeach; ?>" .
63 '<?php $__env->stopPush(); ?>';
66 // Allow longer string lengths after upgrade to utf8mb4
67 Schema::defaultStringLength(191);
69 // Set morph-map due to namespace changes
71 'BookStack\\Bookshelf' => Bookshelf::class,
72 'BookStack\\Book' => Book::class,
73 'BookStack\\Chapter' => Chapter::class,
74 'BookStack\\Page' => Page::class,
78 View::composer('partials.breadcrumbs', BreadcrumbsViewComposer::class);
82 * Register any application services.
86 public function register()
88 $this->app->singleton(SettingService::class, function ($app) {
89 return new SettingService($app->make(Setting::class), $app->make('Illuminate\Contracts\Cache\Repository'));