1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
use html_go\exceptions\InternalException; |
4
|
|
|
use html_go\model\Config; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* The main entry point. Called from <code>index.php</code> in the application |
8
|
|
|
* root. The parameters are provided for testing thus they have default values. |
9
|
|
|
* @param string $uri |
10
|
|
|
* @param string $method |
11
|
|
|
* @return string The html to be rendered. |
12
|
|
|
*/ |
13
|
|
|
function dispatch(string $uri = null, string $method = HTTP_GET): string { |
14
|
|
|
if ($uri === null) { |
15
|
|
|
$uri = $_SERVER['REQUEST_URI']; // @codeCoverageIgnore |
16
|
|
|
$method = \strtoupper($_SERVER['REQUEST_METHOD']); // @codeCoverageIgnore |
17
|
|
|
} |
18
|
|
|
$uri = strip_url_parameters($uri); |
19
|
|
|
$uri = \trim($uri, FWD_SLASH); |
20
|
|
|
if (empty($uri)) { |
21
|
|
|
$uri = HOME_INDEX_KEY; |
22
|
|
|
} |
23
|
|
|
return route($uri, $method); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Route the given HTTP request. |
28
|
|
|
* @param string $uri The requested URI |
29
|
|
|
* @param string $method the HTTP method |
30
|
|
|
* @throws InternalException |
31
|
|
|
* @return string |
32
|
|
|
*/ |
33
|
|
|
function route(string $uri, string $method): string { |
34
|
|
|
if ($method === HTTP_POST) { |
35
|
|
|
return not_found(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$result = \preg_match(POST_REQ_REGEX, $uri); |
39
|
|
|
if ($result === false) { |
40
|
|
|
throw new InternalException("preg_match() failed checking [$uri]"); // @codeCoverageIgnore |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$adminCtx = get_config()->getString(Config::KEY_ADMIN_CONTEXT); |
44
|
|
|
if ($result === SINGLE_POST_REQUEST) { |
45
|
|
|
$content = get_content_object($uri); |
46
|
|
|
} elseif (\str_starts_with($uri, $adminCtx)) { |
47
|
|
|
$content = process_admin_request($uri, $method); |
|
|
|
|
48
|
|
|
} else { |
49
|
|
|
$content = process_request($uri, get_pagination_pagenumber(), get_config()->getInt(Config::KEY_POSTS_PERPAGE)); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
if ($content === null) { |
53
|
|
|
return not_found(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return render(vars: get_template_context($content)); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Process a request for a single blog-post. |
61
|
|
|
* @param string $uri |
62
|
|
|
* @param int $pageNum |
63
|
|
|
* @param int $perPage |
64
|
|
|
* @return \stdClass|NULL |
65
|
|
|
*/ |
66
|
|
|
function process_request(string $uri, int $pageNum, int $perPage): ?\stdClass { |
67
|
|
|
$template = LIST_TEMPLATE; |
68
|
|
|
|
69
|
|
|
if (HOME_INDEX_KEY === $uri) { |
70
|
|
|
return get_homepage($pageNum, $perPage); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$list = is_landing_page($uri, $pageNum, $perPage); |
74
|
|
|
if (empty($list)) { |
75
|
|
|
$list = is_list_page($uri, $pageNum, $perPage); |
76
|
|
|
if (empty($list)) { |
77
|
|
|
$template = SINGLE_TEMPLATE; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return get_content_object($uri, $list, $template); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Process a request for an admin page. |
86
|
|
|
* @param string $uri |
87
|
|
|
* @param string $method |
88
|
|
|
* @return \stdClass|NULL |
89
|
|
|
*/ |
90
|
|
|
function process_admin_request(string $uri, string $method): ?\stdClass { |
|
|
|
|
91
|
|
|
require_once ADMIN_SYS_ROOT.DS.'functions.php'; |
92
|
|
|
exit('admin page'); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Checks if the given URI is a landing page (excluding the home page), if so returns a list of the |
97
|
|
|
* appropriate content objects for that page. |
98
|
|
|
* @param string $uri |
99
|
|
|
* @return array<\stdClass> |
100
|
|
|
*/ |
101
|
|
|
function is_landing_page(string $uri, int $pageNum = 1, int $perPage = 0): array { |
102
|
|
|
$list = []; |
103
|
|
|
switch (true) { |
104
|
|
|
case $uri === CAT_INDEX_KEY: |
105
|
|
|
$list = get_categories($pageNum, $perPage); |
106
|
|
|
break; |
107
|
|
|
case $uri === POST_INDEX_KEY: |
108
|
|
|
$list = get_posts($pageNum, $perPage); |
109
|
|
|
break; |
110
|
|
|
case $uri === TAG_INDEX_KEY: |
111
|
|
|
$list = get_tags($pageNum, $perPage); |
112
|
|
|
break; |
113
|
|
|
default: |
114
|
|
|
// Do nothing |
115
|
|
|
} |
116
|
|
|
return $list; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Checks if the given URI is a list page (excluding the home page), if so returns a list of the |
121
|
|
|
* approriate content object for that page. |
122
|
|
|
* @param string $uri |
123
|
|
|
* @param int $pageNum |
124
|
|
|
* @param int $perPage |
125
|
|
|
* @return array<\stdClass> |
126
|
|
|
*/ |
127
|
|
|
function is_list_page(string $uri, int $pageNum = 1, int $perPage = 0): array { |
128
|
|
|
$list = []; |
129
|
|
|
switch (true) { |
130
|
|
|
case \str_starts_with($uri, POST_INDEX_KEY): |
131
|
|
|
$list = get_posts($pageNum, $perPage); |
132
|
|
|
break; |
133
|
|
|
case \str_starts_with($uri, CAT_INDEX_KEY.FWD_SLASH): |
134
|
|
|
$list = get_posts_for_category($uri, $pageNum, $perPage); |
135
|
|
|
break; |
136
|
|
|
case \str_starts_with($uri, TAG_INDEX_KEY.FWD_SLASH): |
137
|
|
|
$list = get_posts_for_tag($uri, $pageNum, $perPage); |
138
|
|
|
break; |
139
|
|
|
default: |
140
|
|
|
// Do nothing |
141
|
|
|
} |
142
|
|
|
return $list; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Returns the home page. |
147
|
|
|
* @param int $pageNum |
148
|
|
|
* @param int $perPage |
149
|
|
|
* @throws InternalException |
150
|
|
|
* @return \stdClass |
151
|
|
|
*/ |
152
|
|
|
function get_homepage(int $pageNum, int $perPage): \stdClass { |
153
|
|
|
if (get_config()->getBool(Config::KEY_STATIC_INDEX)) { |
154
|
|
|
if (($content = get_content_object(HOME_INDEX_KEY, /** @scrutinizer ignore-type */ template: SINGLE_TEMPLATE)) === null) { |
155
|
|
|
throw new InternalException("Unable to find the home index page!"); |
156
|
|
|
} |
157
|
|
|
} else { |
158
|
|
|
if (($content = get_content_object(HOME_INDEX_KEY, get_posts($pageNum, $perPage), LIST_TEMPLATE)) === null) { |
159
|
|
|
throw new InternalException("Unable to find the home index page!"); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
return $content; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Strips parameters from the given URL. |
167
|
|
|
* @param string $url |
168
|
|
|
* @return string returns the URL without the parameters. |
169
|
|
|
*/ |
170
|
|
|
function strip_url_parameters(string $url): string { |
171
|
|
|
if (($pos = \strpos($url, '?')) === false) { |
172
|
|
|
return $url; |
173
|
|
|
} |
174
|
|
|
parse_query(\substr($url, $pos + 1)); |
175
|
|
|
return \substr($url, 0, $pos); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param string $query (Optional). Calling with this parameter set will populate the returned data array. |
180
|
|
|
* @return array<string, string> |
181
|
|
|
*/ |
182
|
|
|
function parse_query(string $query = null): array { |
183
|
|
|
static $data = []; |
184
|
|
|
if (empty($data) && $query !== null) { |
185
|
|
|
\parse_str($query, $data); |
186
|
|
|
} |
187
|
|
|
return $data; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Return the query string value for the given key. |
192
|
|
|
* @param string $key |
193
|
|
|
* @return string|NULL <code>null</code> if the key is not found |
194
|
|
|
*/ |
195
|
|
|
function get_query_parameter(string $key): ?string { |
196
|
|
|
$params = parse_query(); |
197
|
|
|
if (isset($params[$key])) { |
198
|
|
|
return $params[$key]; |
199
|
|
|
} |
200
|
|
|
return null; |
201
|
|
|
} |
202
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()
can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.