-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrouter.php
More file actions
147 lines (124 loc) · 3.34 KB
/
router.php
File metadata and controls
147 lines (124 loc) · 3.34 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
<?php
# Error routes
function pageNotFound() {
header("HTTP/1.0 404 Not Found");
include 'pages/errors/404.php';
exit();
}
# Routes
$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$parts = explode("/", $path);
$GLOBALS['path'] = $path;
# Home page route
if($path == '/') {
include 'pages/index.php';
exit();
}
# Login route
if($path == '/login') {
include 'pages/users/login.php';
exit();
}
# Signup route
if($path == '/signup') {
include 'pages/users/signup.php';
exit();
}
# Feed action route
if($path == '/feed') {
include 'actions/feed.php';
exit();
}
# Settings page
if($path == '/settings') {
include 'pages/users/settings.php';
exit();
}
# For multiple part routes
if(count($parts) < 2) {
pageNotFound();
}
# Notifications routes
if($path == '/notifications') {
include 'pages/users/notifications.php';
exit();
}
# Search routes
if ($path == '/search') {
include 'pages/search.php';
exit();
}
# User routes
if($parts[1] == 'user') {
if(count($parts) >= 3 AND $parts[2] != '') {
# User specific actions and pages
$GLOBALS['username'] = $parts[2];
if(isset($parts[3]) && $parts[3] == 'follow'){
# User follow action
include 'actions/users/follow.php';
exit();
}else if(isset($parts[3]) && $parts[3] == 'edit'){
# User edit action
include 'actions/users/edit.php';
exit();
}
# User view profile page
include 'pages/users/view.php';
exit();
}
pageNotFound();
}
# Post routes
if($parts[1] == 'post') {
if($parts[2] == 'upload'){
# Post upload page
include 'pages/posts/upload.php';
exit();
}
if(count($parts) >= 3 AND $parts[2] != '') {
# Post specific actions and pages
$GLOBALS['postShortId'] = $parts[2];
if(isset($parts[3]) && $parts[3] == 'like'){
# Post like action
include 'actions/posts/like.php';
exit();
}else if(isset($parts[3]) && $parts[3] == 'comment'){
# Comment action
include 'actions/posts/comment.php';
exit();
}
# View post
include 'pages/posts/view.php';
exit();
}
}
# Static routes and media routes
$mimeTypes = [
'static' => [
'css' => 'text/css',
'js' => 'application/javascript',
'svg' => 'image/svg+xml'
],
'media' => [
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'png' => 'image/png',
'gif' => 'image/gif'
]
];
# Check if the requested path starts with '/static/' or '/media/' and extract the directory and file extension
if (preg_match('/^\/(static|media)\/(.+)\.(css|js|svg|jpg|jpeg|png|gif)$/', $path, $matches)) {
$directory = $matches[1];
$extension = $matches[3];
# Check if the directory is supported and the file extension is valid
if (isset($mimeTypes[$directory]) && array_key_exists($extension, $mimeTypes[$directory])) {
$file = __DIR__ . "/{$directory}/{$matches[2]}.{$extension}";
if (file_exists($file)) {
# Set the appropriate MIME type and output the file content
header("Content-Type: {$mimeTypes[$directory][$extension]}");
readfile($file);
exit();
}
}
}
pageNotFound();