Repository navigation
Expand file tree
/
Copy pathindex.php
More file actions
592 lines (522 loc) · 20.9 KB
/
Copy pathindex.php
File metadata and controls
592 lines (522 loc) · 20.9 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
<?php
//phpinfo();
declare(strict_types=1);
require 'config.php';
if (session_status() !== PHP_SESSION_ACTIVE) {
$secureCookie = false;
if (function_exists('aiConfigBool')) {
$secureCookie = aiConfigBool($securityConfig['session_cookie_secure'] ?? true, true);
} else {
$secureCookie = (bool)($securityConfig['session_cookie_secure'] ?? true);
}
if ($secureCookie && empty($_SERVER['HTTPS'])) {
$secureCookie = false;
}
session_set_cookie_params([
'lifetime' => 0,
'path' => '/',
'secure' => $secureCookie,
'httponly' => true,
'samesite' => 'Lax',
]);
session_start();
}
require_once __DIR__ . '/includes/orm.php';
require_once __DIR__ . '/includes/schema.php';
if (!function_exists('aiCmsNormalizeFrontControllerPath')) {
/**
* Normalize pretty/front-controller URLs into the legacy request variables.
*
* Nginx should execute index.php for every PHP-looking request. That means
* older URLs such as /pages.php, /actions/login.php, /login, /areas/pittsburgh,
* and /privacy.php all arrive here and can be mapped without exposing random
* PHP files as executable entry points.
*/
function aiCmsNormalizeFrontControllerPath(): void
{
$requestUri = (string)($_SERVER['REQUEST_URI'] ?? '/');
$path = parse_url($requestUri, PHP_URL_PATH);
if (!is_string($path) || $path === '') {
return;
}
$path = rawurldecode($path);
$path = preg_replace('#/+#', '/', $path) ?: '/';
$trimmed = trim($path, '/');
if ($trimmed === '' || strcasecmp($trimmed, 'index.php') === 0) {
return;
}
$segments = array_values(array_filter(explode('/', $trimmed), static function ($segment): bool {
return trim((string)$segment) !== '';
}));
if (!$segments) {
return;
}
// Support either a dedicated cms.triodesktop.com vhost rooted at /cms or
// a shared triodesktop.com vhost where requests may be prefixed /cms/...
if (isset($segments[0]) && strcasecmp((string)$segments[0], 'cms') === 0) {
array_shift($segments);
}
if (!$segments) {
return;
}
$setRequestValue = static function (string $key, string $value): void {
if ($value === '') {
return;
}
if (!isset($_GET[$key]) || $_GET[$key] === '') {
$_GET[$key] = $value;
}
if (!isset($_REQUEST[$key]) || $_REQUEST[$key] === '') {
$_REQUEST[$key] = $value;
}
};
$first = (string)$segments[0];
$last = (string)end($segments);
$lastBase = preg_replace('/\.php$/i', '', $last) ?: $last;
$firstBase = preg_replace('/\.php$/i', '', $first) ?: $first;
$pageFile = static function (string $name): string {
$safe = preg_replace('/[^A-Za-z0-9_-]/', '', $name) ?: '';
return __DIR__ . '/pages/' . $safe . '.php';
};
$actionFile = static function (string $name): string {
$safe = preg_replace('/[^A-Za-z0-9_-]/', '', $name) ?: '';
return __DIR__ . '/actions/' . $safe . '.php';
};
if (strcasecmp($firstBase, 'actions') === 0 || strcasecmp($firstBase, 'action') === 0) {
if (is_file($actionFile($lastBase))) {
$setRequestValue('action', $lastBase);
}
return;
}
if (strcasecmp($firstBase, 'areas') === 0 && isset($segments[1])) {
$setRequestValue('slug', (string)$segments[1]);
return;
}
if (strcasecmp($firstBase, 'pages') === 0 && isset($segments[1])) {
$candidate = preg_replace('/\.php$/i', '', (string)$segments[1]) ?: (string)$segments[1];
if (is_file($pageFile($candidate))) {
$setRequestValue('page', $candidate);
return;
}
}
if (strcasecmp($firstBase, 'oauth') === 0 && strcasecmp((string)($segments[1] ?? ''), 'google') === 0) {
$oauthPart = strtolower((string)($segments[2] ?? ''));
$setRequestValue('action', $oauthPart === 'callback' ? 'oauthGoogleCallback' : 'oauthGoogleStart');
return;
}
if (strcasecmp($firstBase, 'admin') === 0) {
$candidate = isset($segments[1]) ? (preg_replace('/\.php$/i', '', (string)$segments[1]) ?: (string)$segments[1]) : '';
$adminRoutes = [
'' => 'admin',
'index' => 'admin',
'login' => 'admin',
'users' => 'admin_users',
'user' => 'admin_user',
'edit-user' => 'admin_user',
'apps' => 'admin_apps',
'app' => 'admin_app',
'edit-app' => 'admin_app',
'logout' => 'admin_logout',
];
if (array_key_exists($candidate, $adminRoutes)) {
$target = $adminRoutes[$candidate];
if ($target === 'admin_logout') {
$setRequestValue('action', 'adminLogout');
} else {
$setRequestValue('page', $target);
}
return;
}
$setRequestValue('page', 'admin');
return;
}
if (preg_match('/\.php$/i', $last) === 1) {
if (is_file($actionFile($lastBase))) {
$setRequestValue('action', $lastBase);
return;
}
if (is_file($pageFile($lastBase))) {
$setRequestValue('page', $lastBase);
return;
}
$setRequestValue('slug', $lastBase);
return;
}
$publicSpecialRoutes = [
'signup' => 'signup',
'register' => 'signup',
'login' => 'login',
'user' => 'user_area',
'account' => 'user_area',
];
if (array_key_exists(strtolower($firstBase), $publicSpecialRoutes)) {
$setRequestValue('page', $publicSpecialRoutes[strtolower($firstBase)]);
return;
}
if (is_file($pageFile($firstBase))) {
$setRequestValue('page', $firstBase);
return;
}
// Default public route: treat the pretty path as a content slug.
$slugPath = implode('/', array_map(static function ($segment): string {
return trim((string)$segment, '/');
}, $segments));
$setRequestValue('slug', $slugPath);
}
}
aiCmsNormalizeFrontControllerPath();
if (!function_exists('aiCmsForceGlobalAuthRoute')) {
/**
* Keep global account/admin routes out of the domain/site slug fallback.
*
* Some panel rewrite files can pre-populate slug/page variables before the
* front controller sees the request. When /login or /admin arrives on
* triodesktop.com, www.triodesktop.com, or cms.triodesktop.com, those paths
* must always be application routes, never site slugs. This guard runs
* after the normal pretty-URL mapper and deliberately overwrites stale
* slug/page values for these global routes.
*/
function aiCmsForceGlobalAuthRoute(): void
{
$requestUri = (string)($_SERVER['REQUEST_URI'] ?? '/');
$path = parse_url($requestUri, PHP_URL_PATH);
if (!is_string($path) || $path === '') {
return;
}
$path = rawurldecode($path);
$path = preg_replace('#/+#', '/', $path) ?: '/';
$segments = array_values(array_filter(explode('/', trim($path, '/')), static function ($segment): bool {
return trim((string)$segment) !== '';
}));
if (!$segments) {
return;
}
if (strcasecmp((string)$segments[0], 'cms') === 0) {
array_shift($segments);
}
if (!$segments) {
return;
}
$existingAction = trim((string)($_POST['action'] ?? $_REQUEST['action'] ?? ''));
if ($existingAction !== '' && in_array($existingAction, [
'login',
'logout',
'signup',
'adminLogin',
'adminLogout',
'smsSignup',
'oauthGoogleStart',
'oauthGoogleCallback',
], true)) {
// Do not erase real form actions just because the pretty URL is /admin or /login.
return;
}
$first = strtolower((string)$segments[0]);
$second = strtolower((string)($segments[1] ?? ''));
$third = strtolower((string)($segments[2] ?? ''));
$page = null;
$action = null;
if ($first === 'login') {
$page = 'login';
} elseif ($first === 'signup' || $first === 'register') {
$page = 'signup';
} elseif ($first === 'user' || $first === 'account') {
$page = 'user_area';
} elseif ($first === 'logout') {
$action = 'logout';
} elseif ($first === 'admin') {
$candidate = strtolower((string)($segments[1] ?? ''));
$adminRoutes = [
'' => 'admin',
'index' => 'admin',
'login' => 'admin',
'users' => 'admin_users',
'user' => 'admin_user',
'edit-user' => 'admin_user',
'apps' => 'admin_apps',
'app' => 'admin_app',
'edit-app' => 'admin_app',
'logout' => null,
];
if ($candidate === 'logout') {
$action = 'adminLogout';
} elseif (array_key_exists($candidate, $adminRoutes)) {
$page = (string)$adminRoutes[$candidate];
} else {
$page = 'admin';
}
} elseif ($first === 'oauth' && $second === 'google') {
$action = ($third === 'callback') ? 'oauthGoogleCallback' : 'oauthGoogleStart';
}
if ($page !== null || $action !== null) {
unset($_GET['slug'], $_POST['slug'], $_REQUEST['slug']);
if ($page !== null) {
$_GET['page'] = $page;
$_REQUEST['page'] = $page;
unset($_GET['action'], $_POST['action'], $_REQUEST['action']);
}
if ($action !== null) {
$_GET['action'] = $action;
$_REQUEST['action'] = $action;
unset($_GET['page'], $_POST['page'], $_REQUEST['page']);
}
}
}
}
aiCmsForceGlobalAuthRoute();
if (!function_exists('aiCmsRedirectWwwAuthRoutesToCanonicalHost')) {
/**
* OAuth redirect URIs must match Google Cloud exactly. Keep account/OAuth
* routes on the canonical root host so /www/login never generates a
* different OAuth origin or falls into site-host mapping.
*/
function aiCmsRedirectWwwAuthRoutesToCanonicalHost(): void
{
$host = strtolower(trim((string)($_SERVER['HTTP_HOST'] ?? '')));
if ($host !== 'www.triodesktop.com') {
return;
}
$requestUri = (string)($_SERVER['REQUEST_URI'] ?? '/');
$path = parse_url($requestUri, PHP_URL_PATH);
if (!is_string($path) || $path === '') {
return;
}
$normalized = '/' . trim((preg_replace('#/+#', '/', rawurldecode($path)) ?: '/'), '/');
$isAuthRoute = preg_match('#^/(login|signup|register|admin|user|account|logout)(/|$)#i', $normalized)
|| preg_match('#^/oauth/google(/callback)?/?$#i', $normalized);
if (!$isAuthRoute) {
return;
}
$query = parse_url($requestUri, PHP_URL_QUERY);
$target = 'https://triodesktop.com' . $normalized . (is_string($query) && $query !== '' ? '?' . $query : '');
if (!headers_sent()) {
header('Location: ' . $target, true, 302);
}
exit;
}
}
aiCmsRedirectWwwAuthRoutesToCanonicalHost();
$requestedActionEarly = trim((string)($_REQUEST['action'] ?? ''));
$db = null;
try {
$db = aiCreateDatabase($eloquent_config);
aiEnsureCoreSchema($db);
} catch (Throwable $e) {
if (function_exists('aiRuntimeException')) {
aiRuntimeException('bootstrap-database', $e);
} else {
error_log('[TrioDesktop CMS] Eloquent bootstrap failed: ' . $e->getMessage());
}
}
// Investor-kit requests must still be captured even if the database is temporarily unavailable.
if ($requestedActionEarly === 'smsSignup' && file_exists(__DIR__ . '/actions/smsSignup.php')) {
require __DIR__ . '/actions/smsSignup.php';
exit;
}
if (!$db instanceof \TrioDesktopCms\Orm\CmsDatabase) {
if (!headers_sent()) {
http_response_code(503);
}
echo '<!doctype html><html><head><meta charset="utf-8"><title>TrioDesktop CMS | Temporarily Unavailable</title></head><body style="font-family:Arial,sans-serif; padding:40px; line-height:1.6;"><h1>TrioDesktop CMS</h1><p>The website is temporarily unavailable because no CMS database backend could be opened.</p><p>Verify <code>config.ini</code>, PHP database extensions, and the writable <code>data/</code> fallback path.</p></body></html>';
exit;
}
$pageName = trim((string)($_REQUEST['page'] ?? ''));
$slug = trim((string)($_GET['slug'] ?? $_REQUEST['slug'] ?? ''));
if (!function_exists('aiFriendlyPageLabel')) {
function aiFriendlyPageLabel(string $value): string
{
$value = trim($value);
if ($value === '' || $value === 'view' || $value === 'headline-story' || $value === 'headline_story') {
return 'Home';
}
return ucwords(str_replace(['-', '_'], ' ', $value));
}
}
if (!function_exists('aiPagesHasColumn')) {
function aiPagesHasColumn(string $column): bool
{
global $db;
return aiTableHasColumn($db, 'pages', $column);
}
}
require_once __DIR__ . '/includes/url_helpers.php';
require_once __DIR__ . '/includes/site_settings.php';
require_once __DIR__ . '/includes/locations.php';
require_once __DIR__ . '/includes/service_pages.php';
require_once __DIR__ . '/includes/sitemap.php';
require_once __DIR__ . '/includes/language.php';
require_once __DIR__ . '/includes/flash.php';
require_once __DIR__ . '/includes/auth.php';
require_once __DIR__ . '/includes/oauth.php';
require_once __DIR__ . '/includes/assets.php';
aiEnsureLanguageSchema($db);
aiSeedSpanishContent($db);
$GLOBALS['aiLanguage'] = aiDetectLanguage($db);
$aiMetaTitle = null;
$aiMetaDescription = null;
$aiCanonicalUrl = null;
$aiMetaExtra = [];
$aiServiceAreaActive = null;
if ($slug !== '') {
$area = aiGetServiceAreaBySlug($slug);
if ($area !== null) {
$area['slug'] = $slug;
$aiServiceAreaActive = $area;
$meta = aiServiceAreaMeta($area, (string)($db->site[1]->baseURL ?? 'https://cms.triodesktop.com'));
$aiMetaTitle = $meta['title'] ?? null;
$aiMetaDescription = $meta['description'] ?? null;
$aiCanonicalUrl = $meta['canonical'] ?? null;
$aiMetaExtra = $meta['meta_tags'] ?? [];
}
}
$isAdminLoggedIn = function_exists('aiIsAdminLoggedIn') ? aiIsAdminLoggedIn() : !empty($_SESSION['is_admin']);
$isSuperAdminLoggedIn = function_exists('aiIsSuperAdminLoggedIn') ? aiIsSuperAdminLoggedIn() : !empty($_SESSION['is_super_admin']);
$isUserLoggedIn = function_exists('aiIsUserLoggedIn') ? aiIsUserLoggedIn() : (!empty($_SESSION['user_id']) && empty($_SESSION['is_admin']));
$isLoggedIn = $isAdminLoggedIn || $isUserLoggedIn || !empty($_SESSION['email']);
if (!$isLoggedIn && $pageName === 'page' && intval($_GET['id'] ?? 0) > 0) {
if (!headers_sent()) {
header('Location: ' . aiBaseUrl($db) . '/', true, 301);
}
exit;
}
$publicPages = ['view', 'display', 'login', 'signup', 'admin'];
$publicActions = ['login', 'logout', 'signup', 'adminLogin', 'adminLogout', 'smsSignup', 'oauthGoogleStart', 'oauthGoogleCallback'];
// -------------------------
// 1. ACTION HANDLER
// -------------------------
if ($delete = intval($_REQUEST['delete'] ?? 0)) {
if (($_REQUEST['page'] ?? '') == 'pages') $action = 'deletePage';
if (in_array(($_REQUEST['page'] ?? ''), ['users', 'admin_users'], true)) $action = 'deleteUser';
if (($_REQUEST['page'] ?? '') == 'locations') $action = 'deleteLocation';
}
if ($undelete = intval($_REQUEST['undelete'] ?? 0)) {
if (($_REQUEST['page'] ?? '') == 'pages') $action = 'deletePage';
if (in_array(($_REQUEST['page'] ?? ''), ['users', 'admin_users'], true)) $action = 'deleteUser';
if (($_REQUEST['page'] ?? '') == 'locations') $action = 'deleteLocation';
}
if (!isset($action)) {
$action = trim((string)($_REQUEST['action'] ?? ''));
}
if ($action !== '') {
$superAdminActions = ['updateUser', 'deleteUser', 'updateApp', 'deleteApp'];
$siteAdminActions = ['addPage', 'updatePage', 'deletePage', 'updatePosition', 'updateSite', 'addLocation', 'updateLocation', 'deleteLocation', 'syncServicePages'];
if (in_array($action, $superAdminActions, true) && !$isSuperAdminLoggedIn) {
if (function_exists('aiFlashError')) {
aiFlashError('Super admin login required.');
}
if (function_exists('aiRedirect')) {
aiRedirect('/admin');
}
$action = '';
} elseif (in_array($action, $siteAdminActions, true) && !$isAdminLoggedIn) {
if (function_exists('aiFlashError')) {
aiFlashError('Site admin login required.');
}
if (function_exists('aiRedirect')) {
aiRedirect('/admin');
}
$action = '';
}
if (!$isLoggedIn && !in_array($action, $publicActions, true)) {
$action = '';
}
if ($action !== '' && file_exists("actions/{$action}.php")) {
require "actions/{$action}.php";
}
}
// -------------------------
// 2. PAGE HANDLER
// -------------------------
$page = trim((string)($_REQUEST['page'] ?? ''));
$pageFile = '';
$showCustom404 = false;
/*
|--------------------------------------------------------------------------
| Priority:
| 1. ?page=whatever -> pages/whatever.php (admin/named pages)
| 2. ?slug=whatever -> pages/view.php (slug-based content page)
| 3. no page, no slug -> pages/view.php (all-pages scroll view)
|--------------------------------------------------------------------------
*/
if ($page !== '') {
$superAdminPages = ['admin_users', 'admin_user', 'admin_apps', 'admin_app'];
$siteAdminPages = ['pages', 'page', 'files', 'site', 'locations', 'location', 'browse'];
$userPages = ['user_area'];
if (in_array($page, $superAdminPages, true) && !$isSuperAdminLoggedIn) {
$page = 'admin';
} elseif (in_array($page, $siteAdminPages, true) && !$isAdminLoggedIn) {
$page = 'admin';
} elseif (in_array($page, $userPages, true) && !$isUserLoggedIn) {
$page = 'login';
if (function_exists('aiFlashError')) {
aiFlashError('Please log in first.');
}
} elseif (!$isLoggedIn && !in_array($page, $publicPages, true)) {
$page = 'view';
$pageFile = 'pages/view.php';
}
if ($pageFile === '') {
$candidate = "pages/{$page}.php";
if (file_exists($candidate)) {
$pageFile = $candidate;
} else {
$page = 'view';
$pageFile = 'pages/view.php';
$showCustom404 = true;
}
}
} else {
$page = 'view';
$pageFile = 'pages/view.php';
}
// Keep pageName in sync with the resolved $page (used in header title and menu guard)
$pageName = $page;
$smsStatus = trim((string)($_GET['sms'] ?? ''));
$kitVersion = time();
// Browse page renders bare (inside elFinder iframe) no template wrapper
if ($page === 'browse' && file_exists($pageFile)) {
$id = intval($_REQUEST['id'] ?? 0);
require $pageFile;
exit;
}
// LP section is hidden when logged in, on the login page, or when viewing a specific slug page.
$showLP = !$isLoggedIn && !in_array($page, ['login', 'signup', 'admin'], true) && $slug === '';
?>
<?php if (!$isLoggedIn && function_exists('aiIsSpanish') && aiIsSpanish()) { ob_start('aiLocalizeHtmlResponse'); } ?>
<!DOCTYPE html>
<html lang="<?php echo function_exists('aiCurrentLanguage') ? htmlspecialchars(aiCurrentLanguage(), ENT_QUOTES, 'UTF-8') : 'en'; ?>">
<?php require 'sections/header.php'; ?>
<body class="tdcms-page-<?= htmlspecialchars(preg_replace('/[^a-z0-9_-]+/i', '-', (string)$pageName) ?: 'view', ENT_QUOTES, 'UTF-8') ?>">
<main>
<?php if ($showLP): ?>
<section class="lp-wrap">
<?php require 'sections/right.php'; ?>
</section>
<?php endif; ?>
<?php
if ($isAdminLoggedIn) {
$menu = '2.php';
} elseif ($isUserLoggedIn) {
$menu = 'user.php';
}
if ($pageName != 'browse') {
if (!empty($menu) && file_exists("sections/{$menu}")) {
require "sections/{$menu}";
} else {
require 'sections/0.php';
}
}
if (function_exists('aiFlashRender')) {
aiFlashRender();
}
if (!$showCustom404 && file_exists($pageFile)) {
$id = intval($_REQUEST['id'] ?? 0);
require $pageFile;
}
?>
<?php require_once __DIR__ . '/includes/assets.php'; ?>
<script src="<?= aiCmsHtmlAssetUrl('js/menu.js') ?>"></script>
<?php require("sections/footer.php"); ?>
</main>
</body>
</html>