-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.php
More file actions
122 lines (103 loc) · 3.68 KB
/
Copy pathindex.php
File metadata and controls
122 lines (103 loc) · 3.68 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
<?php
/**
* CodeVault — Main Router
*
* Every request goes through this file. It loads config,
* starts the session, reads the URL, and includes the right page.
*/
// Load environment variables and database config
require_once __DIR__ . '/config/env.php';
require_once __DIR__ . '/config/database.php';
require_once __DIR__ . '/includes/functions.php';
require_once __DIR__ . '/includes/auth.php';
// Start session with secure settings
session_start([
'cookie_httponly' => true,
'cookie_samesite' => 'Lax',
'use_strict_mode' => true,
]);
// Get the requested URL path, default to empty string (home)
$uri = trim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/');
$base = trim($_ENV['BASE_URL'] ?? 'codevault', '/');
$url = $_GET['url'] ?? (str_starts_with($uri, $base) ? trim(substr($uri, strlen($base)), '/') : $uri);
// Split the URL into segments: "edit/abc-123" → ["edit", "abc-123"]
$segments = $url ? explode('/', $url) : [];
$page = $segments[0] ?? '';
$param = $segments[1] ?? null;
// Base path for includes (so pages can reference project root)
define('BASE_PATH', __DIR__);
define('BASE_URL', $_ENV['BASE_URL'] ?? '');
// Route to the correct page
switch ($page) {
case '':
require BASE_PATH . '/pages/home.php';
break;
case 'register':
if (isLoggedIn()) { redirect(BASE_URL . '/dashboard'); }
require BASE_PATH . '/pages/register.php';
break;
case 'login':
if (isLoggedIn()) { redirect(BASE_URL . '/dashboard'); }
require BASE_PATH . '/pages/login.php';
break;
case 'logout':
logoutUser();
redirect(BASE_URL . '/');
break;
case 'dashboard':
requireLogin();
require BASE_PATH . '/pages/dashboard.php';
break;
case 'new':
requireLogin();
require BASE_PATH . '/pages/new-snippet.php';
break;
case 'edit':
requireLogin();
if (!$param) { redirect(BASE_URL . '/dashboard'); }
$snippetId = $param;
require BASE_PATH . '/pages/edit-snippet.php';
break;
case 'snippet':
if (!$param) { redirect(BASE_URL . '/explore'); }
$snippetId = $param;
require BASE_PATH . '/pages/snippet.php';
break;
case 'u':
if (!$param) { redirect(BASE_URL . '/explore'); }
$profileUsername = $param;
require BASE_PATH . '/pages/profile.php';
break;
case 'explore':
require BASE_PATH . '/pages/explore.php';
break;
case 'docs':
require BASE_PATH . '/pages/api-docs.php';
break;
case 'settings':
requireLogin();
require BASE_PATH . '/pages/settings.php';
break;
case 'api':
// Route API requests: /api/v1/snippets or /api/v1/snippets/{id}
if (($segments[1] ?? '') === 'v1' && ($segments[2] ?? '') === 'snippets') {
$apiSnippetId = $segments[3] ?? null;
require BASE_PATH . '/api/v1/snippets.php';
} else {
http_response_code(404);
echo json_encode(['error' => 'API endpoint not found']);
}
break;
default:
// 404 page
http_response_code(404);
$pageTitle = 'Page Not Found';
require BASE_PATH . '/includes/header.php';
echo '<div class="container" style="text-align:center; padding: 4rem 1rem;">';
echo '<h1 style="font-size: 3rem; margin-bottom: 1rem;">404</h1>';
echo '<p class="text-secondary" style="margin-bottom: 2rem;">The page you\'re looking for doesn\'t exist.</p>';
echo '<a href="' . BASE_URL . '/" class="btn btn-primary">Go Home</a>';
echo '</div>';
require BASE_PATH . '/includes/footer.php';
break;
}