instance('path.common', base_path('common')); } public function boot(): void { Route::prefix('api') ->middleware('api') ->group(function () { $this->loadRoutesFrom(__DIR__ . '/routes/api.php'); }); Route::middleware('web')->group(function () { $this->loadRoutesFrom(__DIR__ . '/routes/web.php'); }); $this->loadRoutesFrom(__DIR__ . '/routes/webhooks.php'); $this->loadMigrationsFrom(__DIR__ . '/Database/migrations'); $this->loadViewsFrom(app('path.common') . '/resources/views', 'common'); $this->loadViewsFrom( storage_path('app/editable-views'), 'editable-views', ); $this->registerPolicies(); $this->registerCustomValidators(); $this->registerCommands(); $this->registerMiddleware(); $this->registerCollectionExtensions(); $this->registerEventListeners(); $this->registerCustomMailDrivers(); $this->setMorphMap(); $configs = collect(self::CONFIG_FILES) ->mapWithKeys(function ($file) { return [ app('path.common') . "/resources/config/$file.php" => config_path( "common/$file.php", ), ]; }) ->toArray(); $this->publishes($configs); Vite::useScriptTagAttributes([ 'data-keep' => 'true', ]); Vite::useStyleTagAttributes([ 'data-keep' => 'true', ]); Vite::usePreloadTagAttributes([ 'data-keep' => 'true', ]); // install/update page components Blade::component( 'common::install.components.install-layout', 'install-layout', ); Blade::component( 'common::install.components.install-button', 'install-button', ); } public function register() { $this->mergeConfig(); $request = $this->app->make(Request::class); $this->app->instance(AppUrl::class, (new AppUrl())->init()); $this->normalizeRequestUri($request); app('url')->forceRootUrl(config('app.url')); $loader = AliasLoader::getInstance(); // register socialite service provider and alias $this->app->register(SocialiteServiceProvider::class); $this->app->register(AnalyticsServiceProvider::class); $loader->alias('Socialite', Socialite::class); $this->app->register(TusServiceProvider::class); // server timing $this->app->singleton(ServerTiming::class, function ($app) { return new ServerTiming(new Stopwatch()); }); // active workspace if (config('common.site.workspaces_integrated')) { $this->app->singleton(ActiveWorkspace::class, function () { return new ActiveWorkspace(); }); } // need the same instance of settings for request lifecycle, so dynamically changed settings work correctly $this->app->singleton(Settings::class, fn() => new Settings()); $this->app->singleton( 'guestRole', fn() => Role::where('guests', true)->first(), ); // url generator for SEO $this->app->bind(AppUrlGenerator::class, BaseUrlGenerator::class); // bootstrap data $this->app->bind(BootstrapData::class, BaseBootstrapData::class); // pagination $this->app->bind( LengthAwarePaginator::class, CustomLengthAwarePaginator::class, ); $this->app->bind(Paginator::class, CustomSimplePaginator::class); $this->registerDevProviders(); // register flysystem providers $this->app->register(DynamicStorageDiskProvider::class); if ($this->storageDriverSelected('dropbox')) { $this->app->register(DropboxServiceProvider::class); } if ($this->storageDriverSelected('digitalocean_s3')) { $this->app->register(DigitalOceanServiceProvider::class); } if ($this->storageDriverSelected('backblaze_s3')) { $this->app->register(BackblazeServiceProvider::class); } // register scout drivers resolve(EngineManager::class)->extend('mysql', function () { return new MysqlSearchEngine(); }); if (config('scout.driver') === ElasticSearchEngine::class) { $this->app->register(ElasticSearchServiceProvider::class); } } private function mergeConfig() { $this->deepMergeDefaultSettings( app('path.common') . '/resources/config/default-settings.php', 'common.default-settings', ); $this->deepMergeConfigFrom( app('path.common') . '/resources/config/demo-blocked-routes.php', 'common.demo-blocked-routes', ); $this->mergeConfigFrom( app('path.common') . '/resources/config/site.php', 'common.site', ); $this->mergeConfigFrom( app('path.common') . '/resources/config/setting-validators.php', 'common.setting-validators', ); $this->mergeConfigFrom( app('path.common') . '/resources/config/menus.php', 'common.menus', ); $this->mergeConfigFrom( app('path.common') . '/resources/config/appearance.php', 'common.appearance', ); $this->mergeConfigFrom( app('path.common') . '/resources/config/services.php', 'services', ); $this->mergeConfigFrom( app('path.common') . '/resources/config/seo/common.php', 'seo.common', ); } /** * Remove sub-directory from request uri, so as far as laravel/symfony * is concerned request came from public directory, even if request * was redirected from root laravel folder to public via .htaccess * * This will solve issues where requests redirected from laravel root * folder to public via .htaccess (or other) redirects are not working * if laravel is inside a subdirectory. Mostly useful for shared hosting * or local dev where virtual hosts can't be set up properly. * * @param Request $request */ private function normalizeRequestUri(Request $request) { $parsedUrl = parse_url(config('app.url')); //if there's no subdirectory we can bail if (!isset($parsedUrl['path'])) { return; } $originalUri = $request->server->get('REQUEST_URI'); $subdirectory = preg_quote($parsedUrl['path'], '/'); $normalizedUri = preg_replace("/^$subdirectory/", '', $originalUri); //if uri starts with "/public" after normalizing, //we can bail as laravel will handle this uri properly if (str_starts_with(ltrim($normalizedUri, '/'), 'public')) { return; } $request->server->set('REQUEST_URI', $normalizedUri); } private function registerMiddleware(): void { if (!config('common.site.installed')) { $this->app['router']->pushMiddlewareToGroup( 'web', RedirectIfNotInstalledMiddleware::class, ); return; } $aliasMiddleware = [ 'isAdmin' => IsAdmin::class, 'verified' => EnsureEmailIsVerified::class, 'optionalAuth' => OptionalAuthenticate::class, 'customDomainsEnabled' => CustomDomainsEnabled::class, 'prerenderIfCrawler' => PrerenderIfCrawler::class, 'verifyApiAccess' => VerifyApiAccessMiddleware::class, ]; $apiMiddleware = [ EnableDebugIfLoggedInAsAdmin::class, SimulateSlowConnectionMiddleware::class, SetAppLocale::class, ForbidBannedUser::class, SetSentryUserMiddleware::class, ]; $webMiddleware = [ EnableDebugIfLoggedInAsAdmin::class, SimulateSlowConnectionMiddleware::class, ServerTimingMiddleware::class, SetAppLocale::class, ForbidBannedUser::class, SetSentryUserMiddleware::class, ]; if (config('common.site.demo')) { $apiMiddleware[] = RestrictDemoSiteFunctionality::class; $webMiddleware[] = RestrictDemoSiteFunctionality::class; } foreach ($apiMiddleware as $middleware) { $this->app['router']->pushMiddlewareToGroup('api', $middleware); } foreach ($webMiddleware as $middleware) { $this->app['router']->pushMiddlewareToGroup('web', $middleware); } foreach ($aliasMiddleware as $alias => $middleware) { $this->app['router']->aliasMiddleware($alias, $middleware); } } /** * Register custom validation rules with laravel. */ private function registerCustomValidators() { Validator::extend( 'email_verified', 'Common\Auth\Validators\EmailVerifiedValidator@validate', ); Validator::extend( 'multi_date_format', 'Common\Validation\Validators\MultiDateFormatValidator@validate', ); } /** * Deep merge the given configuration with the existing configuration. */ private function deepMergeConfigFrom(string $path, string $key): void { $config = $this->app['config']->get($key, []); $this->app['config']->set( $key, array_merge_recursive(require $path, $config), ); } private function registerPolicies() { Gate::policy('App\Model', 'App\Policies\ModelPolicy'); Gate::policy(FileEntry::class, FileEntryPolicy::class); Gate::policy(BaseUser::class, UserPolicy::class); Gate::policy(Role::class, RolePolicy::class); Gate::policy(CustomPage::class, PagePolicy::class); Gate::policy(Setting::class, SettingPolicy::class); Gate::policy(Localization::class, LocalizationPolicy::class); Gate::policy('AppearancePolicy', AppearancePolicy::class); Gate::policy('ReportPolicy', ReportPolicy::class); Gate::policy(CssTheme::class, CssThemePolicy::class); Gate::policy(CustomDomain::class, CustomDomainPolicy::class); Gate::policy(Permission::class, PermissionPolicy::class); Gate::policy(Tag::class, TagPolicy::class); Gate::policy(Comment::class, CommentPolicy::class); Gate::policy(Channel::class, ChannelPolicy::class); // billing Gate::policy(Subscription::class, SubscriptionPolicy::class); Gate::policy(Invoice::class, InvoicePolicy::class); Gate::policy(Product::class, ProductPolicy::class); // workspaces Gate::policy(Workspace::class, WorkspacePolicy::class); Gate::policy(WorkspaceMember::class, WorkspaceMemberPolicy::class); Gate::define('admin.access', function (BaseUser $user) { return $user->hasPermission('admin.access'); }); } private function registerCommands(): void { // register commands $commands = [ DeleteUploadArtifacts::class, SeedCommand::class, DeleteExpiredCsvExports::class, GenerateChecksums::class, AbortOldS3Uploads::class, DeleteExpiredTusUploads::class, UpdateSimplePaginateTables::class, DeleteExpiredBansCommand::class, DeleteExpiredOtpCodesCommand::class, StartSsr::class, StopSsr::class, UpdateActionsCommand::class, GenerateSitemap::class, ScheduleHealthCommand::class, CleanLogTables::class, ]; if ($this->app->environment() !== 'production') { $commands = array_merge($commands, [ ExportTranslations::class, GenerateFooTranslations::class, ]); } $this->commands($commands); // schedule commands $this->app->booted(function () { $schedule = $this->app->make(Schedule::class); $schedule->command(DeleteUploadArtifacts::class)->daily(); $schedule->command(DeleteExpiredCsvExports::class)->daily(); $schedule->command(AbortOldS3Uploads::class)->daily(); $schedule->command(DeleteExpiredTusUploads::class)->daily(); $schedule->command(UpdateSimplePaginateTables::class)->daily(); $schedule->command(DeleteExpiredBansCommand::class)->daily(); $schedule->command(DeleteExpiredOtpCodesCommand::class)->hourly(); $schedule->command(ScheduleHealthCommand::class)->everyMinute(); $schedule->command(CleanLogTables::class)->daily(); $this->monitorSchedule($schedule); }); } /** * Deep merge "default-settings" config values. */ private function deepMergeDefaultSettings( string $path, string $configKey, ): void { $defaultSettings = require $path; $userSettings = $this->app['config']->get($configKey, []); foreach ($userSettings as $userSetting) { //remove default setting, if it's overwritten by user setting foreach ($defaultSettings as $key => $defaultSetting) { if ($defaultSetting['name'] === $userSetting['name']) { unset($defaultSettings[$key]); } } //push user setting into default settings array $defaultSettings[] = $userSetting; } $this->app['config']->set($configKey, $defaultSettings); } private function registerDevProviders() { if (!config('app.debug')) { return; } if ($this->clockworkExists()) { $this->app->register(ClockworkServiceProvider::class); } } private function clockworkExists(): bool { return class_exists(ClockworkServiceProvider::class); } private function registerCollectionExtensions() { // convert all array items to lowercase Collection::macro('toLower', function ($key = null) { return $this->map(function ($value) use ($key) { // remove all whitespace and lowercase if (is_string($value)) { return slugify($value, ' '); } else { $value[$key] = slugify($value[$key], ' '); return $value; } }); }); } protected function storageDriverSelected(string $name): bool { return config('common.site.uploads_disk_driver') === $name || config('common.site.public_disk_driver') === $name; } private function registerEventListeners(): void { Event::listen(SettingsSaved::class, [ SyncPlansWhenBillingSettingsChange::class, UpdateAllUsersLanguageWhenDefaultLocaleChanges::class, ]); Event::subscribe(OutgoingEmailLogSubscriber::class); Event::listen( FileUploaded::class, CreateThumbnailForUploadedFile::class, ); Event::listen(Registered::class, function (Registered $event) { if ( app(Settings::class)->get('require_email_confirmation') && !$event->user->hasVerifiedEmail() ) { $event->user->sendEmailVerificationNotification(); } }); if (config('common.site.workspaces_integrated')) { Event::listen(UsersDeleted::class, function (UsersDeleted $e) { $e->users->each(function (User $user) { app(Workspace::class) ->forUser($user->id) ->get() ->each(function (Workspace $workspace) use ($user) { app(RemoveMemberFromWorkspace::class)->execute( $workspace, $user->id, ); }); app(DeleteEntries::class)->execute([ 'entryIds' => $user ->entries() ->pluck('file_entries.id'), ]); }); }); } } public function registerCustomMailDrivers() { $this->app->get('mail.manager'); $this->app ->get('mail.manager') ->extend('gmailApi', function (array $config) { return new GmailApiMailTransport(); }); $this->app->singleton(GmailClient::class); } private function setMorphMap() { Relation::enforceMorphMap([ 'post' => 'App\Models\Post', 'video' => 'App\Models\Video', ]); } }