-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathgoRouter.php
More file actions
188 lines (169 loc) · 6.67 KB
/
goRouter.php
File metadata and controls
188 lines (169 loc) · 6.67 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
<?php
class GoRouter {
// Edit Modes
const MODE_CREATE = 'create';
const MODE_EDIT = 'edit';
// route names
const ROUTE_NAME_API = 'api';
const ROUTE_NAME_LINKS_API = 'link_api';
const ROUTE_NAME_EDIT = 'edit';
const ROUTE_NAME_GROUP = 'group';
const ROUTE_NAME_GROUP_USER_ADD = 'group-user-add';
const ROUTE_NAME_GROUP_USER_REMOVE = 'group-user-remove';
const ROUTE_NAME_GROUPS = 'groups';
const ROUTE_NAME_HOME = 'home';
const ROUTE_NAME_LINKS = 'links';
const ROUTE_NAME_LOGIN = 'login';
const ROUTE_NAME_LOGOUT = 'logout';
const ROUTE_NAME_LOOKUP = 'lookup';
const ROUTE_NAME_MANAGE = 'manage';
const ROUTE_NAME_QR_PNG = 'qr-png';
const ROUTE_NAME_QR_SVG = 'qr-svg';
const ROUTE_NAME_REDIRECT = 'redirect';
const ROUTE_NAME_RESET = 'reset';
// route paths
const ROUTE_PATH_A = 'a/';
const ROUTE_PATH_API = 'api/';
const ROUTE_PATH_LINKS_API = 'api/links';
const ROUTE_PATH_GROUP = 'a/group';
const ROUTE_PATH_GROUP_USER_ADD = 'a/group-user-add';
const ROUTE_PATH_GROUP_USER_REMOVE = 'a/group-user-remove';
const ROUTE_PATH_GROUPS = 'a/groups';
const ROUTE_PATH_HOME = '';
const ROUTE_PATH_LINKS = 'a/links';
const ROUTE_PATH_LOGIN = 'a/login';
const ROUTE_PATH_LOGOUT = 'a/logout';
const ROUTE_PATH_LOOKUP = 'a/lookup';
public static $corsAllowedDomains = array();
protected $viewTemplate;
protected $viewParams;
protected $route;
protected $groupId;
protected $groupMode;
protected $uid;
protected $goId;
protected $pathInfo;
protected function routeRequiresLogin(): bool
{
return in_array($this->route, array(
self::ROUTE_NAME_LINKS_API,
self::ROUTE_NAME_EDIT,
self::ROUTE_NAME_GROUP,
self::ROUTE_NAME_GROUP_USER_ADD,
self::ROUTE_NAME_GROUP_USER_REMOVE,
self::ROUTE_NAME_GROUPS,
self::ROUTE_NAME_LINKS,
self::ROUTE_NAME_LOOKUP,
self::ROUTE_NAME_MANAGE,
self::ROUTE_NAME_RESET
));
}
protected function routeNoRedirect(): bool
{
return in_array($this->route, array(
self::ROUTE_NAME_LINKS_API,
));
}
protected function routeRequiresCredentials(): bool
{
return in_array($this->route, array(
self::ROUTE_NAME_LINKS_API,
self::ROUTE_NAME_API,
));
}
public function getViewTemplate() {
return $this->viewTemplate;
}
public function getViewParams() {
return $this->viewParams;
}
public function route() {
$this->route = NULL;
if (empty($this->pathInfo)) {
$this->route = self::ROUTE_NAME_HOME;
} elseif ($this->pathInfo === self::ROUTE_PATH_API) {
$this->route = self::ROUTE_NAME_API;
} elseif ($this->pathInfo === self::ROUTE_PATH_LINKS_API) {
$this->route = self::ROUTE_NAME_LINKS_API;
}elseif (preg_match('#^([^/]+)\.qr$#', $this->pathInfo, $matches)) {
$this->route = self::ROUTE_NAME_QR_PNG;
$this->goId = $matches[1];
} elseif (preg_match('#^([^/]+)\.png$#', $this->pathInfo, $matches)) {
$this->route = self::ROUTE_NAME_QR_PNG;
$this->goId = $matches[1];
}elseif (preg_match('#^([^/]+)\.svg$#', $this->pathInfo, $matches)) {
$this->route = self::ROUTE_NAME_QR_SVG;
$this->goId = $matches[1];
}
$this->routeAdmin();
if (!$this->route && $this->pathInfo !== '') {
$this->route = self::ROUTE_NAME_REDIRECT;
}
}
private function routeAdmin() {
if (isset($_GET['manage']) || in_array($this->pathInfo, array(self::ROUTE_PATH_A, self::ROUTE_PATH_LINKS))) {
$this->route = self::ROUTE_NAME_MANAGE;
} elseif (isset($_GET['lookup']) || $this->pathInfo === self::ROUTE_PATH_LOOKUP) {
$this->route = self::ROUTE_NAME_LOOKUP;
} elseif (preg_match('/^a\/group\/(\d+)$/', $this->pathInfo, $matches)) {
$this->route = self::ROUTE_NAME_GROUP;
$this->groupId = $matches[1];
$this->groupMode = self::MODE_EDIT;
} elseif ($this->pathInfo === self::ROUTE_PATH_GROUP) {
$this->route = self::ROUTE_NAME_GROUP;
$this->groupId = NULL;
$this->groupMode = self::MODE_CREATE;
} elseif ($this->pathInfo === self::ROUTE_PATH_GROUPS) {
$this->route = self::ROUTE_NAME_GROUPS;
} elseif (preg_match('/^a\/group-user-add\/(\d+)$/', $this->pathInfo, $matches)) {
$this->route = self::ROUTE_NAME_GROUP_USER_ADD;
$this->groupId = $matches[1];
$this->uid = htmlspecialchars($_POST['uid'] ?? '');
} elseif (preg_match('/^a\/group-user-remove\/(\d+)-([\w\-\.]+)$/', $this->pathInfo, $matches)) {
$this->route = self::ROUTE_NAME_GROUP_USER_REMOVE;
$this->groupId = $matches[1];
$this->uid = urldecode($matches[2]);
} elseif (preg_match('#^([^/]+)\/edit$#', $this->pathInfo, $matches)) {
$this->route = self::ROUTE_NAME_EDIT;
$this->goId = $matches[1];
} elseif (preg_match('#^([^/]+)\/reset$#', $this->pathInfo, $matches)) {
$this->route = self::ROUTE_NAME_RESET;
$this->goId = $matches[1];
}
}
protected function redirect($location, $code = 303, $sendCORSHeaders = FALSE) {
header("LOCATION: ". htmlspecialchars($location ?? ''), TRUE, $code);
if ($sendCORSHeaders) {
$this->sendCORSHeaders();
}
exit();
}
public function allowedCORSDomain() {
if (!empty($_SERVER['HTTP_ORIGIN'])) {
foreach (static::$corsAllowedDomains as $allowedDomain) {
$domainCheck = "/https?:\/\/.*" . $allowedDomain . "$/";
if (preg_match($domainCheck, $_SERVER['HTTP_ORIGIN'])) {
return $_SERVER['HTTP_ORIGIN'];
}
}
}
return false;
}
protected function sendCORSHeaders() {
if ($allowedCORSDomain = $this->allowedCORSDomain()) {
header('Access-Control-Allow-Origin: ' . $allowedCORSDomain);
header('Access-Control-Allow-Methods: GET, POST');
header('Access-Control-Allow-Headers: X-Requested-With');
if ($this->routeRequiresCredentials()) {
header('Access-Control-Allow-Credentials: true');
}
}
}
protected function handle404($showCustom = TRUE) {
header('HTTP/1.1 404 Not Found');
if ($showCustom === TRUE) {
include __DIR__ . '/../www/templates/404.php';
}
exit;
}
}