This repository has been archived by the owner on Jun 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstart.php
406 lines (360 loc) · 9.62 KB
/
start.php
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
<?php
/**
* Provides support for URLs no longer used in Elgg for those who bookmarked or
* linked to them
*/
/**
* Initialize the plugin
*
* @return void
*/
function legacy_urls_init() {
elgg_register_page_handler('tag', 'legacy_urls_tag_handler');
elgg_register_page_handler('pg', 'legacy_urls_pg_handler');
elgg_register_plugin_hook_handler('route', 'blog', 'legacy_urls_blog_forward');
elgg_register_plugin_hook_handler('route', 'bookmarks', 'legacy_urls_bookmarks_forward');
elgg_register_plugin_hook_handler('route', 'file', 'legacy_urls_file_forward');
elgg_register_plugin_hook_handler('route', 'groups', 'legacy_urls_groups_forward');
elgg_register_plugin_hook_handler('route', 'settings', 'legacy_urls_settings_forward');
elgg_register_page_handler('forum', 'legacy_urls_forum_handler');
elgg_register_plugin_hook_handler('route', 'messageboard', 'legacy_urls_messageboard_forward');
}
/**
* Redirect the requestor to the new URL
* Checks the plugin setting to determine the course of action:
* a) Displays an error page with the new URL
* b) Forwards to the new URL and displays an error message
* c) Silently forwards to the new URL
*
* @param string $url Relative or absolute URL
*
* @return void|true
*/
function legacy_urls_redirect($url) {
$method = elgg_get_plugin_setting('redirect_method', 'legacy_urls');
// we only show landing page or queue warning if html generating page
$viewtype = elgg_get_viewtype();
if ($viewtype != 'default' && !elgg_does_viewtype_fallback($viewtype)) {
$method = 'immediate';
}
switch ($method) {
case 'landing':
$content = elgg_view('legacy_urls/message', ['url' => $url]);
$body = elgg_view_layout('error', ['content' => $content]);
echo elgg_view_page('', $body, 'error');
return true;
break;
case 'immediate_error':
// drop through after setting error message
register_error(elgg_echo('changebookmark'));
case 'immediate':
default:
$url = elgg_normalize_url($url);
header("HTTP/1.1 301 Moved Permanently");
header("Location: $url");
exit;
break;
}
}
/**
* Adds query parameters to URL for redirect
*
* @param string $url The URL
* @param array $query_vars Additional query parameters in associate array
*
* @return string
*/
function legacy_urls_prepare_url($url, array $query_vars = []) {
$params = [];
// Elgg munges the request in htaccess rules so cannot use $_GET
$query = parse_url(_elgg_services()->request->server->get('REQUEST_URI'), PHP_URL_QUERY);
if ($query) {
parse_str($query, $params);
}
$params = array_merge($params, $query_vars);
if ($params) {
return elgg_http_add_url_query_elements($url, $params);
}
return $url;
}
/**
* Handle requests for /tag/<tag string>
*
* @param array $segments URL segments
*
* @return void|true
*/
function legacy_urls_tag_handler($segments) {
$tag = $segments[0];
$url = legacy_urls_prepare_url('search', ['q' => $tag]);
return legacy_urls_redirect($url);
}
/**
* Handle requests for URLs that start with /pg/
*
* @param array $segments URL segments
*
* @return void|true
*/
function legacy_urls_pg_handler($segments) {
$url = implode('/', $segments);
return legacy_urls_redirect(legacy_urls_prepare_url($url));
}
/**
* Blog forwarder
*
* 1.0-1.7.5
* Group blogs page: /blog/group:<container_guid>/
* Group blog view: /blog/group:<container_guid>/read/<guid>/<title>
* 1.7.5-pre 1.8
* Group blogs page: /blog/owner/group:<container_guid>/
* Group blog view: /blog/read/<guid>
*
* @param string $hook "route"
* @param string $type "blog"
* @param array $result Old identifier and segments
*
* @return void|false
*/
function legacy_urls_blog_forward($hook, $type, $result) {
$page = $result['segments'];
// easier to work with and no notices
$page = array_pad($page, 4, "");
// group usernames
if (preg_match('~/group\:([0-9]+)/~', "/{$page[0]}/{$page[1]}/", $matches)) {
$guid = $matches[1];
$entity = get_entity($guid);
if (elgg_instanceof($entity, 'group')) {
if (!empty($page[2])) {
$url = "blog/view/$page[2]/";
} else {
$url = "blog/group/$guid/all";
}
// we drop query params because the old group URLs were invalid
legacy_urls_redirect(legacy_urls_prepare_url($url));
return false;
}
}
if (empty($page[0])) {
return;
}
if ($page[0] == "read") {
$url = "blog/view/{$page[1]}/";
legacy_urls_redirect(legacy_urls_prepare_url($url));
return false;
}
// user usernames
$user = get_user_by_username($page[0]);
if (!$user) {
return;
}
if (empty($page[1])) {
$page[1] = 'owner';
}
switch ($page[1]) {
case "read":
$url = "blog/view/{$page[2]}/{$page[3]}";
break;
case "archive":
$url = "blog/archive/{$page[0]}/{$page[2]}/{$page[3]}";
break;
case "friends":
$url = "blog/friends/{$page[0]}";
break;
case "new":
$url = "blog/add/$user->guid";
break;
case "owner":
$url = "blog/owner/{$page[0]}";
break;
}
if (isset($url)) {
legacy_urls_redirect(legacy_urls_prepare_url($url));
return false;
}
}
/**
* Bookmarks forwarder
* /bookmarks/group:<group_guid>/
* /bookmarks/gorup:<group_guid>/read/<guid>/
* /bookmarks/read/<guid>
* /bookmarks/<username>[/(items|read|inbox|friends|add|bookmarklet)/<guid>]
*
* @param string $hook "route"
* @param string $type "bookmarks"
* @param array $result Old identifier and segments
*
* @return void|false
*/
function legacy_urls_bookmarks_forward($hook, $type, $result) {
$page = $result['segments'];
// easier to work with and no notices
$page = array_pad($page, 4, "");
// old group usernames
if (substr_count($page[0], 'group:')) {
preg_match('/group\:([0-9]+)/i', $page[0], $matches);
$guid = $matches[1];
$entity = get_entity($guid);
if (elgg_instanceof($entity, 'group')) {
if (!empty($page[2])) {
$url = "bookmarks/view/$page[2]/";
} else {
$url = "bookmarks/group/$guid/all";
}
// we drop query params because the old group URLs were invalid
legacy_urls_redirect(legacy_urls_prepare_url($url));
}
}
if ($page[0] == "read") {
$url = "bookmarks/view/{$page[1]}/";
legacy_urls_redirect(legacy_urls_prepare_url($url));
return false;
}
$user = get_user_by_username($page[0]);
if (!$user) {
return;
}
if (empty($page[1])) {
$page[1] = 'items';
}
switch ($page[1]) {
case "read":
$url = "bookmarks/view/{$page[2]}/{$page[3]}";
break;
case "inbox":
$url = "bookmarks/inbox/{$page[0]}";
break;
case "friends":
$url = "bookmarks/friends/{$page[0]}";
break;
case "add":
$url = "bookmarks/add/{$page[0]}";
break;
case "items":
$url = "bookmarks/owner/{$page[0]}";
break;
case "bookmarklet":
$url = "bookmarks/bookmarklet/{$page[0]}";
break;
}
if (isset($url)) {
legacy_urls_redirect(legacy_urls_prepare_url($url));
return false;
}
}
/**
* File forwarder
* /file/read/<guid>
*
* @param string $hook "route"
* @param string $type "file"
* @param array $result Old identifier and segments
*
* @return void|false
*/
function legacy_urls_file_forward($hook, $type, $result) {
$page = $result['segments'];
// easier to work with and no notices
$page = array_pad($page, 4, "");
if ($page[0] == 'read') {
$url = "file/view/{$page[1]}";
legacy_urls_redirect(legacy_urls_prepare_url($url));
return false;
}
}
/**
* Groups forwarder
* /groups/<guid>
* /groups/forum/<guid>
*
* @param string $hook "route"
* @param string $type "groups"
* @param array $result Old identifier and segments
*
* @return void|false
*/
function legacy_urls_groups_forward($hook, $type, $result) {
$page = $result['segments'];
// easier to work with and no notices
$page = array_pad($page, 4, "");
if (is_numeric($page[0])) {
$group = get_entity($page[0]);
if ($group instanceof \ElggGroup) {
legacy_urls_redirect(legacy_urls_prepare_url($group->getURL()));
return false;
}
}
if ($page[0] == 'forum') {
$url = "discussion/owner/{$page[1]}";
legacy_urls_redirect(legacy_urls_prepare_url($url));
return false;
}
}
/**
* User settings forwarder
* /settings/plugins/
*
* @param string $hook "route"
* @param string $type "settings"
* @param array $result Old identifier and segments
*
* @return void|false
*/
function legacy_urls_settings_forward($hook, $type, $result) {
$page = $result['segments'];
// easier to work with and no notices
$page = array_pad($page, 4, "");
if ($page[0] == "plugins") {
if (empty($page[2])) {
$url = "settings";
if (!empty($page[1])) {
$url .= "/user/" . $page[1];
}
legacy_urls_redirect(legacy_urls_prepare_url($url));
return false;
}
}
}
/**
* Group forum forwarder
* /forum/.*
*
* @param array $page URL segments
*
* @return bool
*/
function legacy_urls_forum_handler($page) {
switch ($page[0]) {
case 'topic':
$url = "discussion/view/{$page[1]}/{$page[2]}";
legacy_urls_redirect(legacy_urls_prepare_url($url));
return true;
default:
return false;
}
}
/**
* Messageboard forwarder
* /messageboard/!(owner|add|group)
*
* @param string $hook "route"
* @param string $type "messageboard"
* @param array $result Old identifier and segments
*
* @return void|false
*/
function legacy_urls_messageboard_forward($hook, $type, $result) {
$page = $result['segments'];
// easier to work with and no notices
$page = array_pad($page, 4, "");
// if the first part is a username, forward to new format
$new_section_one = ['owner', 'add', 'group'];
if (isset($page[0]) && !in_array($page[0], $new_section_one) && get_user_by_username($page[0])) {
$url = "messageboard/owner/{$page[0]}";
legacy_urls_redirect(legacy_urls_prepare_url($url));
return false;
}
}
return function() {
elgg_register_event_handler('init', 'system', 'legacy_urls_init');
};