Repository navigation
Expand file tree
/
Copy pathindex.php
More file actions
759 lines (724 loc) · 29.8 KB
/
Copy pathindex.php
File metadata and controls
759 lines (724 loc) · 29.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
<?php
declare(strict_types=1);
use SyntaxDevTeam\Cms\Core\Autoloader;
use SyntaxDevTeam\Cms\Core\AdminMenuRegistry;
use SyntaxDevTeam\Cms\Core\AdminSearchRegistry;
use SyntaxDevTeam\Cms\Core\Bootstrap;
use SyntaxDevTeam\Cms\Core\BrandIconGenerator;
use SyntaxDevTeam\Cms\Core\DashboardRegistry;
use SyntaxDevTeam\Cms\Core\DistributionAccessCredentialStore;
use SyntaxDevTeam\Cms\Core\CoreMigrationRunner;
use SyntaxDevTeam\Cms\Core\ErrorHandler;
use SyntaxDevTeam\Cms\Core\FilesystemPermissions;
use SyntaxDevTeam\Cms\Core\HookRegistry;
use SyntaxDevTeam\Cms\Core\GitHubReleaseDispatcher;
use SyntaxDevTeam\Cms\Core\InstallationState;
use SyntaxDevTeam\Cms\Core\ModuleBootstrapper;
use SyntaxDevTeam\Cms\Core\ModuleArchiveImporter;
use SyntaxDevTeam\Cms\Core\ModuleManifestValidator;
use SyntaxDevTeam\Cms\Core\ModuleInstaller;
use SyntaxDevTeam\Cms\Core\ModuleManagerService;
use SyntaxDevTeam\Cms\Core\ModulePackageSigner;
use SyntaxDevTeam\Cms\Core\ModuleRegistry;
use SyntaxDevTeam\Cms\Core\ModuleStateRepository;
use SyntaxDevTeam\Cms\Core\ModuleUpdatePublisher;
use SyntaxDevTeam\Cms\Core\ModuleUpdateRefreshQueue;
use SyntaxDevTeam\Cms\Core\ModuleUpdateRepository;
use SyntaxDevTeam\Cms\Core\PlatformReleaseRepository;
use SyntaxDevTeam\Cms\Core\PlatformReleasePublisher;
use SyntaxDevTeam\Cms\Core\PlatformUpdater;
use SyntaxDevTeam\Cms\Core\PublicNavigationRegistry;
use SyntaxDevTeam\Cms\Core\RateLimiter;
use SyntaxDevTeam\Cms\Core\Request;
use SyntaxDevTeam\Cms\Core\Router;
use SyntaxDevTeam\Cms\Core\SeoIndex;
use SyntaxDevTeam\Cms\Core\StorefrontHostResolverInterface;
use SyntaxDevTeam\Cms\Modules\CoreAuth\AdminAccessGate;
use SyntaxDevTeam\Cms\Modules\CoreAuth\AuditLogService;
use SyntaxDevTeam\Cms\Modules\CoreAuth\AuthService;
use SyntaxDevTeam\Cms\Modules\CoreAuth\AuthProviderConfigStore;
use SyntaxDevTeam\Cms\Modules\CoreAuth\AuthPolicyRuntime;
use SyntaxDevTeam\Cms\Modules\CoreAuth\AuthorizationService;
use SyntaxDevTeam\Cms\Modules\CoreAuth\CrudAppUserRepository;
use SyntaxDevTeam\Cms\Modules\CoreAuth\DiscordIdentityProvider;
use SyntaxDevTeam\Cms\Modules\CoreAuth\GitHubIdentityProvider;
use SyntaxDevTeam\Cms\Modules\CoreAuth\GoogleIdentityProvider;
use SyntaxDevTeam\Cms\Modules\CoreAuth\FirstAdminBootstrapper;
use SyntaxDevTeam\Cms\Modules\CoreAuth\IdentityProviderRegistry;
use SyntaxDevTeam\Cms\Modules\CoreAuth\InMemoryUserRepository;
use SyntaxDevTeam\Cms\Modules\CoreAuth\NativeHttpClient;
use SyntaxDevTeam\Cms\Modules\CoreAuth\MicrosoftIdentityProvider;
use SyntaxDevTeam\Cms\Modules\CoreAuth\UnavailableUserRepository;
use SyntaxDevTeam\Cms\Modules\CoreAuth\UserRepositoryInterface;
use SyntaxDevTeam\Cms\Modules\CorePages\HomepageSectionRepository;
use SyntaxDevTeam\Cms\Modules\CorePages\HomepageSectionItemRepository;
use SyntaxDevTeam\Cms\Modules\CorePages\PageRepository;
use SyntaxDevTeam\Cms\Modules\DistributionAccess\DistributionAccessRepository;
use SyntaxDevTeam\Cms\Modules\System\SystemSettingsRepository;
require_once __DIR__ . '/core/Autoloader.php';
Autoloader::register();
$requestStartedAt = hrtime(true);
$requestId = '';
$moduleBootstrapDurationMs = 0.0;
$config = [];
$application = null;
$theme = null;
ErrorHandler::register(static function () use (&$theme, &$application, &$config): array {
return [
'theme' => $theme,
'request' => $application?->request(),
'config' => $config,
];
});
if (!InstallationState::isInstalled(__DIR__)) {
$scriptDirectory = str_replace('\\', '/', dirname((string) ($_SERVER['SCRIPT_NAME'] ?? '/index.php')));
$installerUrl = rtrim($scriptDirectory, '/') . '/install.php';
header('Location: ' . ($installerUrl !== '' ? $installerUrl : '/install.php'), true, 302);
exit;
}
$permissionIssues = FilesystemPermissions::missing(__DIR__);
if ($permissionIssues !== []) {
http_response_code(503);
header('Content-Type: text/html; charset=UTF-8');
$escape = static fn (string $value): string => htmlspecialchars($value, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
$directories = implode(', ', array_map(
static fn (string $directory): string => $directory . '/',
$permissionIssues
));
$command = FilesystemPermissions::remediationCommand(__DIR__);
?>
<!doctype html>
<html lang="pl-PL"><head>
<meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="robots" content="noindex, nofollow">
<title>miniPORTAL — wymagane uprawnienia</title>
<style>
:root{color-scheme:dark;--bg:#07101d;--panel:#101b2b;--line:#29405c;--text:#edf6ff;--muted:#a9b8ca;--accent:#64c7ff}
*{box-sizing:border-box}body{display:grid;min-height:100vh;margin:0;place-items:center;padding:1rem;color:var(--text);background:var(--bg);font:16px/1.55 system-ui,sans-serif}
main{width:min(800px,100%);padding:1.5rem;background:var(--panel);border:1px solid var(--line);border-radius:1rem}
p{color:var(--muted)}code{color:var(--accent)}pre{overflow:auto;padding:1rem;background:#050b13;border:1px solid var(--line);border-radius:.55rem;white-space:pre-wrap}
</style>
</head><body><main>
<h1>miniPORTAL wymaga poprawienia uprawnień</h1>
<p>PHP nie może zapisywać katalogów: <code><?= $escape($directories) ?></code></p>
<p>Wykonaj poniższe polecenia, a następnie odśwież stronę:</p>
<pre><code><?= $escape($command) ?></code></pre>
</main></body></html>
<?php
exit;
}
try {
$config = require __DIR__ . '/config/config.php';
$application = Bootstrap::boot($config);
$requestId = bin2hex(random_bytes(8));
header('X-Request-ID: ' . $requestId);
$theme = $application->theme();
$security = $application->security();
$templateCache = $application->templateCache();
$router = new Router();
$adminMenu = new AdminMenuRegistry();
$adminSearch = new AdminSearchRegistry();
$dashboard = new DashboardRegistry();
$publicNavigation = new PublicNavigationRegistry();
$hooks = new HookRegistry();
$rateLimiter = new RateLimiter(__DIR__ . '/cache/rate-limits');
$seo = new SeoIndex((string) ($application->config()['app']['public_url'] ?? ''));
$modules = new ModuleRegistry();
$authConfig = $config['auth'] ?? [];
$authStorage = (string) ($authConfig['storage'] ?? 'database');
$authDemoEnabled = ($authConfig['demo_enabled'] ?? false) === true;
/** @var UserRepositoryInterface $userRepository */
$userRepository = match ($authStorage) {
'database' => $application->database() !== null
? new CrudAppUserRepository($application->database())
: new UnavailableUserRepository(),
'memory' => $authDemoEnabled
? new InMemoryUserRepository()
: new UnavailableUserRepository(),
default => throw new RuntimeException('Nieobsługiwany magazyn AUTH_STORAGE.'),
};
$firstOwnerBootstrapper = $application->database() !== null
? new FirstAdminBootstrapper($application->database())
: null;
$auth = new AuthService($userRepository, $security, $firstOwnerBootstrapper);
$previewAwarePath = $application->request()->path();
$auth->useRolePreview(
str_starts_with($previewAwarePath, '/admin')
|| str_starts_with($previewAwarePath, '/account')
);
$authorization = new AuthorizationService();
$access = new AdminAccessGate($auth, $authorization);
$audit = new AuditLogService(
$application->database(),
(string) ($authConfig['audit_hash_key'] ?? '')
);
$providerConfig = is_array($authConfig['providers'] ?? null) ? $authConfig['providers'] : [];
$githubConfig = is_array($providerConfig['github'] ?? null) ? $providerConfig['github'] : [];
$discordConfig = is_array($providerConfig['discord'] ?? null) ? $providerConfig['discord'] : [];
$googleConfig = is_array($providerConfig['google'] ?? null) ? $providerConfig['google'] : [];
$microsoftConfig = is_array($providerConfig['microsoft'] ?? null) ? $providerConfig['microsoft'] : [];
$providers = new IdentityProviderRegistry();
$httpClient = new NativeHttpClient();
$providerCredentials = static fn (array $provider): array => ($provider['enabled'] ?? false) === true
? [(string) ($provider['client_id'] ?? ''), (string) ($provider['client_secret'] ?? '')]
: ['', ''];
[$githubClientId, $githubClientSecret] = $providerCredentials($githubConfig);
[$discordClientId, $discordClientSecret] = $providerCredentials($discordConfig);
[$googleClientId, $googleClientSecret] = $providerCredentials($googleConfig);
[$microsoftClientId, $microsoftClientSecret] = $providerCredentials($microsoftConfig);
$providers->add(new GitHubIdentityProvider(
$httpClient,
$githubClientId,
$githubClientSecret,
(string) ($githubConfig['callback_url'] ?? '')
));
$providers->add(new DiscordIdentityProvider(
$httpClient,
$discordClientId,
$discordClientSecret,
(string) ($discordConfig['callback_url'] ?? '')
));
$providers->add(new GoogleIdentityProvider(
$httpClient,
$googleClientId,
$googleClientSecret,
(string) ($googleConfig['callback_url'] ?? '')
));
$providers->add(new MicrosoftIdentityProvider(
$httpClient,
$microsoftClientId,
$microsoftClientSecret,
(string) ($microsoftConfig['callback_url'] ?? '')
));
$authProviderConfigStore = new AuthProviderConfigStore(
(string) ($config['meta']['auth_providers_file'] ?? (__DIR__ . '/config/modules/auth-providers.env'))
);
$pageRepository = $application->database() !== null
? new PageRepository($application->database())
: null;
$homepageSectionRepository = $application->database() !== null
? new HomepageSectionRepository($application->database())
: null;
$homepageSectionItemRepository = $application->database() !== null
? new HomepageSectionItemRepository($application->database())
: null;
$moduleDefinitions = require __DIR__ . '/config/modules.php';
$trustedModulePublishers = require __DIR__ . '/config/module_publishers.php';
$modulesConfig = is_array($config['modules'] ?? null) ? $config['modules'] : [];
$moduleSigningKeyId = (string) ($modulesConfig['signing_key_id'] ?? '');
$moduleSigningPublicKeyFile = (string) ($modulesConfig['signing_public_key_file'] ?? '');
if (
$moduleSigningKeyId !== ''
&& $moduleSigningPublicKeyFile !== ''
&& is_readable($moduleSigningPublicKeyFile)
) {
$trustedModulePublishers[$moduleSigningKeyId] = [
'name' => 'Lokalny wydawca miniPORTAL',
'public_key' => (string) file_get_contents($moduleSigningPublicKeyFile),
'status' => 'active',
'valid_from' => null,
'valid_until' => null,
'replacement_key_id' => null,
];
}
$moduleSigningPrivateKeyFile = (string) ($modulesConfig['signing_private_key_file'] ?? '');
$modulePackageSigner = $moduleSigningKeyId !== '' && $moduleSigningPrivateKeyFile !== ''
? new ModulePackageSigner($moduleSigningPrivateKeyFile, $moduleSigningKeyId)
: null;
$manifestValidator = new ModuleManifestValidator(
(string) ($config['app']['version'] ?? '0.1.0'),
$trustedModulePublishers
);
$moduleStates = $application->database() !== null
? new ModuleStateRepository($application->database())
: null;
$moduleInstaller = $application->database() !== null && $moduleStates !== null
? new ModuleInstaller($application->database(), $moduleStates)
: null;
$moduleManager = $moduleStates !== null && $moduleInstaller !== null
? new ModuleManagerService(
__DIR__ . '/modules',
$manifestValidator,
$moduleStates,
$moduleInstaller,
array_values(array_map(
static fn (array $definition): string => (string) ($definition['directory'] ?? ''),
$moduleDefinitions
)),
$modulePackageSigner
)
: null;
$moduleArchiveImporter = new ModuleArchiveImporter(
__DIR__ . '/cache/module-quarantine',
$manifestValidator,
(int) ($config['modules']['archive_max_bytes'] ?? 10485760),
(int) ($config['modules']['archive_max_files'] ?? 2000),
(int) ($config['modules']['archive_max_file_bytes'] ?? 10485760),
(int) ($config['modules']['archive_max_unpacked_bytes'] ?? 52428800)
);
$distributionAccessCredentials = new DistributionAccessCredentialStore(
__DIR__ . '/config/modules/distribution-access.env',
__DIR__ . '/cache/module-updates/catalog.json'
);
$distributionAccessGateway = null;
if ($application->database() !== null && class_exists(DistributionAccessRepository::class)) {
try {
if ($moduleStates?->find('distribution_access')?->isActive() === true) {
$distributionAccessGateway = new DistributionAccessRepository(
$application->database(),
(string) ($modulesConfig['distribution_access_hash_key'] ?? '')
);
}
} catch (\Throwable) {
$distributionAccessGateway = null;
}
}
$moduleUpdates = new ModuleUpdateRepository(
(string) ($config['modules']['update_catalog_url'] ?? ''),
__DIR__ . '/cache/module-updates',
(int) ($config['modules']['archive_max_bytes'] ?? 10485760),
(int) ($config['modules']['update_catalog_ttl'] ?? 900),
$distributionAccessCredentials
);
$moduleUpdatePublisher = new ModuleUpdatePublisher(
$moduleManager,
__DIR__ . '/cache/module-repository'
);
$moduleUpdateRefreshQueue = new ModuleUpdateRefreshQueue(__DIR__ . '/cache/module-repository');
$updatesConfig = is_array($config['updates'] ?? null) ? $config['updates'] : [];
$platformReleases = new PlatformReleaseRepository(
__DIR__ . '/releases',
(string) ($updatesConfig['catalog_url'] ?? ''),
__DIR__ . '/cache/platform-updates/downloads',
(int) ($updatesConfig['archive_max_bytes'] ?? 52428800)
);
$platformUpdater = new PlatformUpdater(
__DIR__,
__DIR__ . '/cache/platform-updates',
(int) ($updatesConfig['archive_max_files'] ?? 10000),
(int) ($updatesConfig['archive_max_file_bytes'] ?? 52428800),
(int) ($updatesConfig['archive_max_unpacked_bytes'] ?? 209715200)
);
$platformReleasePublisher = new PlatformReleasePublisher(
__DIR__,
__DIR__ . '/cache/platform-updates/publisher'
);
$githubReleaseDispatcher = new GitHubReleaseDispatcher(
(string) ($updatesConfig['github_token'] ?? ''),
(string) ($updatesConfig['github_repository'] ?? 'SyntaxDevTeam/miniPORTAL-source'),
(string) ($updatesConfig['github_workflow'] ?? 'platform-release.yml'),
(string) ($updatesConfig['github_ref'] ?? 'main')
);
$coreMigrationRunner = $application->database() !== null
? new CoreMigrationRunner($application->database(), __DIR__ . '/core/migrations')
: null;
$brandIconGenerator = new BrandIconGenerator(__DIR__, __DIR__ . '/uploads/branding');
$moduleBootstrapper = new ModuleBootstrapper(
__DIR__ . '/modules',
$manifestValidator,
$moduleStates
);
$requestPath = $application->request()->path();
$pollingModuleDirectories = match (true) {
str_starts_with($requestPath, '/admin/remote-terminal/session/'),
str_starts_with($requestPath, '/admin/remote-terminal/assets/'),
str_starts_with($requestPath, '/admin/remote-terminal/output/'),
str_starts_with($requestPath, '/admin/remote-terminal/input/') => ['CoreAuth', 'RemoteTerminal'],
$requestPath === '/admin/minecraft-console/output',
$requestPath === '/admin/minecraft-console/command' => ['CoreAuth', 'MinecraftConsole'],
default => null,
};
$isPollingKernel = $pollingModuleDirectories !== null;
$moduleBootstrapStartedAt = hrtime(true);
$moduleBootstrapper->register($moduleDefinitions, [
'theme' => $theme,
'security' => $security,
'auth' => $auth,
'providers' => $providers,
'audit' => $audit,
'access' => $access,
'admin_menu' => $adminMenu,
'database' => $application->database(),
'auth_config' => $authConfig,
'auth_demo_enabled' => $authDemoEnabled,
'auth_provider_config_store' => $authProviderConfigStore,
'module_manager' => $moduleManager,
'module_archive_importer' => $moduleArchiveImporter,
'module_updates' => $moduleUpdates,
'module_update_publisher' => $moduleUpdatePublisher,
'module_update_refresh_queue' => $moduleUpdateRefreshQueue,
'distribution_access_credentials' => $distributionAccessCredentials,
'distribution_access_gateway' => $distributionAccessGateway,
'platform_releases' => $platformReleases,
'platform_updater' => $platformUpdater,
'platform_release_publisher' => $platformReleasePublisher,
'github_release_dispatcher' => $githubReleaseDispatcher,
'core_migration_runner' => $coreMigrationRunner,
'config' => $application->config(),
'diagnostics' => $application->diagnostics(),
'available_themes' => $application->availableThemes(),
'template_cache' => $templateCache,
'trusted_module_publishers' => $trustedModulePublishers,
'public_navigation' => $publicNavigation,
'dashboard' => $dashboard,
'brand_icon_generator' => $brandIconGenerator,
'http_client' => $httpClient,
'hooks' => $hooks,
'rate_limiter' => $rateLimiter,
], $modules, $pollingModuleDirectories);
$modules->boot(
$adminMenu,
$router,
$isPollingKernel ? null : $publicNavigation,
$isPollingKernel ? null : $adminSearch,
$isPollingKernel ? null : $dashboard,
$isPollingKernel ? null : $hooks,
);
$theme->set_auth_entry_locations(AuthPolicyRuntime::provider()->loginEntryLocations());
$moduleBootstrapDurationMs = (hrtime(true) - $moduleBootstrapStartedAt) / 1_000_000;
if (!$isPollingKernel) {
$theme->set_admin_search_items($adminSearch->visibleFor($auth->user()?->permissions ?? []));
$rolePreview = $auth->rolePreview();
$theme->set_admin_role_preview($rolePreview !== null ? [
'role' => $rolePreview['role'],
'label' => $rolePreview['label'],
'stop_action' => 'index.php?route=/admin/roles/preview/stop',
'csrf_token' => $security->csrfToken(),
] : null);
}
$publicNavigationItems = static function () use (
$pageRepository,
$publicNavigation,
$application,
$templateCache
): array {
static $resolved = null;
if (is_array($resolved)) {
return $resolved;
}
$loader = static function () use ($pageRepository, $publicNavigation, $application): array {
$navigation = [];
if ($pageRepository !== null) {
$navigation = array_map(
static fn ($page): array => [
'title' => $page->title,
'slug' => $page->slug,
'href' => '/p/' . rawurlencode($page->slug),
'summary' => $page->summary,
'type' => $page->pageType,
'navigation_area' => $page->navigationArea,
'navigation_label' => $page->navigationLabel,
'sort_order' => $page->sortOrder,
],
$pageRepository->published()
);
}
$settings = $application->database() !== null
? (new SystemSettingsRepository($application->database()))->publicNavigationSettings()
: [];
foreach ($publicNavigation->items($settings) as $item) {
foreach (['main' => $item['show_main'], 'footer' => $item['show_footer']] as $area => $enabled) {
if (!$enabled) {
continue;
}
$navigation[] = [
'title' => $item['label'],
'slug' => '',
'href' => $item['href'],
'summary' => '',
'type' => 'module',
'navigation_area' => $area,
'navigation_label' => $item['label'],
'sort_order' => $item['order'],
];
}
}
usort(
$navigation,
static fn (array $left, array $right): int => [$left['sort_order'], $left['title']]
<=> [$right['sort_order'], $right['title']]
);
return $navigation;
};
$json = $templateCache->remember(
'public-data',
'navigation-v1',
static fn (): string => json_encode(
$loader(),
JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES
),
['navigation', 'pages', 'theme']
);
try {
$navigation = json_decode($json, true, 32, JSON_THROW_ON_ERROR);
} catch (JsonException) {
$navigation = $loader();
}
return $resolved = is_array($navigation) ? $navigation : [];
};
$requestPath = $application->request()->path();
$requiresPublicNavigation = $requestPath !== '/'
&& !str_starts_with($requestPath, '/admin')
&& !str_starts_with($requestPath, '/api/')
&& !str_starts_with($requestPath, '/metrics/')
&& !in_array($requestPath, ['/robots.txt', '/sitemap.xml', '/ads.txt'], true);
if ($requiresPublicNavigation) {
$theme->set_public_navigation($publicNavigationItems(), $auth->user() !== null, $modules->ids());
}
$siteMaintenanceEnabled = static function () use ($application): bool {
$app = is_array($application->config()['app'] ?? null)
? $application->config()['app']
: [];
return filter_var($app['public_maintenance_enabled'] ?? '0', FILTER_VALIDATE_BOOL);
};
$renderStart = static function (string $title, string $lead) use ($theme): void {
$theme->start_page(
$title . ' - SyntaxDevTeam',
$lead
);
$theme->start_header($title, $lead, 'SyntaxDevTeam / System');
$theme->end_header();
$theme->start_section();
};
$renderEnd = static function () use ($theme): void {
$theme->end_section();
$theme->end_page();
};
$router->get('/', static function () use (
$pageRepository,
$homepageSectionRepository,
$homepageSectionItemRepository,
$theme,
$auth,
$templateCache,
$publicNavigationItems,
$hooks,
$application
): void {
$renderer = static function () use (
$homepageSectionRepository,
$homepageSectionItemRepository,
$theme,
$auth,
$publicNavigationItems,
$hooks,
$application
): string {
$navigation = $publicNavigationItems();
$sections = [];
if ($homepageSectionRepository !== null) {
$sections = array_map(
static fn ($section): array => $section->toThemeData(
$homepageSectionItemRepository?->forSection($section->id, true) ?? []
),
$homepageSectionRepository->visible()
);
}
$sections = $hooks->applyFilters('homepage.sections', $sections, [
'authenticated' => $auth->user() !== null,
'theme' => (string) ($application->config()['app']['theme'] ?? 'default'),
]);
if (!is_array($sections)) {
throw new UnexpectedValueException('Filtr homepage.sections musi zwrócić tablicę sekcji.');
}
ob_start();
$theme->render_homepage($sections, $navigation, $auth->user() !== null);
return (string) ob_get_clean();
};
echo $auth->user() === null
? $templateCache->remember('public', 'homepage', $renderer, ['homepage', 'pages', 'widgets', 'theme'])
: $renderer();
});
$router->get('/sitemap.xml', static function () use ($seo, $modules, $siteMaintenanceEnabled): void {
header('Content-Type: application/xml; charset=UTF-8');
if ($siteMaintenanceEnabled()) {
http_response_code(503);
header('Cache-Control: no-store');
header('Retry-After: 3600');
echo '<?xml version="1.0" encoding="UTF-8"?>'
. '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"></urlset>';
return;
}
$modules->registerSeo($seo);
header('Cache-Control: public, max-age=900');
echo $seo->sitemapXml();
});
$router->get('/robots.txt', static function () use ($seo, $application, $siteMaintenanceEnabled): void {
header('Content-Type: text/plain; charset=UTF-8');
if ($siteMaintenanceEnabled()) {
header('Cache-Control: no-store');
echo "User-agent: *\nDisallow: /\n";
return;
}
header('Cache-Control: public, max-age=900');
echo $seo->robotsTxt((string) ($application->config()['app']['public_meta_robots'] ?? 'index, follow'));
});
$router->get('/ads.txt', static function (): void {
header('Content-Type: text/plain; charset=UTF-8');
header('Cache-Control: public, max-age=3600');
echo "google.com, pub-3086258607996589, DIRECT, f08c47fec0942fa0\n";
});
$router->get('/security-demo', static function () use ($security, $theme, $renderStart, $renderEnd): void {
$renderStart(
'Test Security i Request',
'Wyślij formularz, aby sprawdzić filtrowanie wejścia, sesję i ochronę CSRF.'
);
$theme->render_form(
'index.php?route=/security-demo',
[
[
'name' => 'message',
'label' => 'Wiadomość testowa',
'type' => 'text',
'value' => 'miniPORTAL działa bezpiecznie',
],
[
'name' => 'level',
'label' => 'Poziom',
'type' => 'select',
'value' => 'core',
'options' => [
'core' => 'Core',
'module' => 'Module',
'template' => 'Template',
],
],
[
'name' => 'confirmed',
'label' => 'Potwierdzam wysłanie danych',
'type' => 'checkbox',
],
],
'Wyślij bezpiecznie',
$security->csrfToken()
);
$renderEnd();
});
$router->post('/security-demo', static function (Request $request) use ($security, $theme, $renderStart, $renderEnd): void {
$renderStart(
'Wynik bezpiecznego żądania',
'Dane zostały pobrane przez Request i zweryfikowane przed przekazaniem do widoku.'
);
if (!$security->validateCsrfToken($request->postString('_token'))) {
http_response_code(403);
$theme->render_alert('Token CSRF jest nieprawidłowy lub wygasł.', 'danger');
$renderEnd();
return;
}
$message = $request->postString('message');
$level = $request->postString('level');
$confirmed = $request->postBool('confirmed') ? 'Tak' : 'Nie';
$theme->render_alert('Token CSRF został poprawnie zweryfikowany.', 'success');
$theme->render_table(
['Pole', 'Wartość'],
[
['Wiadomość', $message],
['Poziom', $level],
['Potwierdzenie', $confirmed],
]
);
$renderEnd();
});
$request = $application->request();
$maintenanceTechnicalPath = str_starts_with($request->path(), '/admin')
|| str_starts_with($request->path(), '/api/')
|| str_starts_with($request->path(), '/metrics/')
|| in_array($request->path(), [
'/robots.txt',
'/sitemap.xml',
'/ads.txt',
'/account/registration',
'/p/miniportal-warunki-uzytkowania',
'/p/miniportal-prywatnosc',
'/p/regulamin',
'/p/polityka-prywatnosci',
], true);
$maintenanceVisitor = $auth->user();
$showMaintenance = $siteMaintenanceEnabled()
&& ($maintenanceVisitor === null || !$maintenanceVisitor->isActive())
&& in_array($request->method(), ['GET', 'HEAD'], true)
&& !$maintenanceTechnicalPath;
if ($showMaintenance) {
$app = is_array($application->config()['app'] ?? null) ? $application->config()['app'] : [];
$title = trim((string) ($app['public_maintenance_title'] ?? 'Site under construction'));
$message = trim((string) (
$app['public_maintenance_message']
?? 'We are preparing the content and configuration. Please check back soon.'
));
http_response_code(503);
header('Cache-Control: no-store, private');
header('Retry-After: 3600');
$theme->set_public_navigation([], false, $modules->ids());
$theme->render_public_error(
503,
$title !== '' ? $title : 'Site under construction',
$message !== '' ? $message : 'Please check back soon.',
'Sign in',
'/admin/login'
);
$status = 200;
} elseif ($auth->rolePreviewBlocks($request)) {
$actor = $auth->actualUser();
$audit->record(
$request,
'role_preview_write_blocked',
'forbidden',
'path=' . $request->path(),
$actor?->id
);
http_response_code(403);
$theme->render_admin_access_state(
403,
'Tryb podglądu jest tylko do odczytu',
'Zakończ podgląd roli, aby wykonać operację zmieniającą dane.',
'index.php?route=/admin',
'Wróć do dashboardu'
);
$status = 200;
} else {
if (
$modules->capabilities()->hasImplementation('itemshop.storefront_host', 'itemshop')
&& ($hostResolver = $modules->capabilities()->implementation('itemshop.storefront_host', 'itemshop')) instanceof StorefrontHostResolverInterface
&& ($hostRoute = $hostResolver->resolve($request)) !== null
) {
$request = $request->withPath($hostRoute);
header('Vary: Host', false);
}
$status = $router->dispatch($request);
}
if ($status === 404) {
http_response_code(404);
$theme->render_public_error(
404,
'Nie znaleziono strony',
'Adres nie pasuje do żadnej opublikowanej strony ani aktywnego modułu.',
'Wróć do strony głównej',
'/'
);
} elseif ($status === 405) {
http_response_code(405);
$theme->render_public_error(
405,
'Nie można wykonać tej akcji',
'Ten adres istnieje, ale nie obsługuje użytej metody HTTP.',
'Wróć do strony głównej',
'/'
);
}
} catch (Throwable $exception) {
ErrorHandler::handle($exception, $theme, $application?->request(), $config);
}
$requestDurationMs = (hrtime(true) - $requestStartedAt) / 1_000_000;
$slowRequestMs = max(50, (int) ($config['app']['performance_slow_request_ms'] ?? 250));
if ($requestDurationMs >= $slowRequestMs) {
$performanceEntry = json_encode([
'request_id' => $requestId,
'method' => $application?->request()->method() ?? (string) ($_SERVER['REQUEST_METHOD'] ?? ''),
'path' => $application?->request()->path() ?? '/',
'status' => http_response_code(),
'duration_ms' => round($requestDurationMs, 2),
'module_boot_ms' => round($moduleBootstrapDurationMs, 2),
'peak_memory_bytes' => memory_get_peak_usage(true),
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
error_log('[miniPORTAL performance] ' . (is_string($performanceEntry) ? $performanceEntry : '{}'));
}