Passed
Push — master ( c0a3a7...3b84a4 )
by Jeroen
58:51
created

mod/legacy_urls/start.php (11 issues)

1
<?php
2
/**
3
 * Provides support for URLs no longer used in Elgg for those who bookmarked or
4
 * linked to them
5
 */
6
7
/**
8
 * Initialize the plugin
9
 *
10
 * @return void
11
 */
12
function legacy_urls_init() {
13 31
	elgg_register_page_handler('tag', 'legacy_urls_tag_handler');
14 31
	elgg_register_page_handler('pg', 'legacy_urls_pg_handler');
15 31
	elgg_register_plugin_hook_handler('route', 'blog', 'legacy_urls_blog_forward');
16 31
	elgg_register_plugin_hook_handler('route', 'bookmarks', 'legacy_urls_bookmarks_forward');
17 31
	elgg_register_plugin_hook_handler('route', 'file', 'legacy_urls_file_forward');
18 31
	elgg_register_plugin_hook_handler('route', 'groups', 'legacy_urls_groups_forward');
19 31
	elgg_register_plugin_hook_handler('route', 'settings', 'legacy_urls_settings_forward');
20 31
	elgg_register_page_handler('forum', 'legacy_urls_forum_handler');
21 31
	elgg_register_plugin_hook_handler('route', 'messageboard', 'legacy_urls_messageboard_forward');
22 31
}
23
24
/**
25
 * Redirect the requestor to the new URL
26
 * Checks the plugin setting to determine the course of action:
27
 * a) Displays an error page with the new URL
28
 * b) Forwards to the new URL and displays an error message
29
 * c) Silently forwards to the new URL
30
 *
31
 * @param string $url Relative or absolute URL
32
 *
33
 * @return void|true
34
 */
35
function legacy_urls_redirect($url) {
36
	$method = elgg_get_plugin_setting('redirect_method', 'legacy_urls');
37
38
	// we only show landing page or queue warning if html generating page
39
	$viewtype = elgg_get_viewtype();
40
	if ($viewtype != 'default' && !elgg_does_viewtype_fallback($viewtype)) {
41
		$method = 'immediate';
42
	}
43
44
	switch ($method) {
45
		case 'landing':
46
			$content = elgg_view('legacy_urls/message', ['url' => $url]);
47
			$body = elgg_view_layout('error', ['content' => $content]);
48
			echo elgg_view_page('', $body, 'error');
49
			return true;
50
			break;
0 ignored issues
show
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
51
		case 'immediate_error':
52
			// drop through after setting error message
53
			register_error(elgg_echo('changebookmark'));
54
		case 'immediate':
55
		default:
56
			$url = elgg_normalize_url($url);
57
			header("HTTP/1.1 301 Moved Permanently");
58
			header("Location: $url");
59
			exit;
60
			break;
61
	}
62
}
63
64
/**
65
 * Adds query parameters to URL for redirect
66
 *
67
 * @param string $url        The URL
68
 * @param array  $query_vars Additional query parameters in associate array
69
 *
70
 * @return string
71
 */
72
function legacy_urls_prepare_url($url, array $query_vars = []) {
73
	$params = [];
74
	// Elgg munges the request in htaccess rules so cannot use $_GET
75
	$query = parse_url(_elgg_services()->request->server->get('REQUEST_URI'), PHP_URL_QUERY);
76
	if ($query) {
77
		parse_str($query, $params);
78
	}
79
	$params = array_merge($params, $query_vars);
80
	if ($params) {
81
		return elgg_http_add_url_query_elements($url, $params);
82
	}
83
	
84
	return $url;
85
}
86
87
/**
88
 * Handle requests for /tag/<tag string>
89
 *
90
 * @param array $segments URL segments
91
 *
92
 * @return void|true
93
 */
94
function legacy_urls_tag_handler($segments) {
95
	$tag = $segments[0];
96
	$url = legacy_urls_prepare_url('search', ['q' => $tag]);
97
	return legacy_urls_redirect($url);
98
}
99
100
/**
101
 * Handle requests for URLs that start with /pg/
102
 *
103
 * @param array $segments URL segments
104
 *
105
 * @return void|true
106
 */
107
function legacy_urls_pg_handler($segments) {
108
	$url = implode('/', $segments);
109
	return legacy_urls_redirect(legacy_urls_prepare_url($url));
110
}
111
112
/**
113
 * Blog forwarder
114
 *
115
 * 1.0-1.7.5
116
 * Group blogs page: /blog/group:<container_guid>/
117
 * Group blog view:  /blog/group:<container_guid>/read/<guid>/<title>
118
 * 1.7.5-pre 1.8
119
 * Group blogs page: /blog/owner/group:<container_guid>/
120
 * Group blog view:  /blog/read/<guid>
121
 *
122
 * @param string $hook   "route"
123
 * @param string $type   "blog"
124
 * @param array  $result Old identifier and segments
125
 *
126
 * @return void|false
127
 */
128
function legacy_urls_blog_forward($hook, $type, $result) {
2 ignored issues
show
The parameter $type is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

128
function legacy_urls_blog_forward($hook, /** @scrutinizer ignore-unused */ $type, $result) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
The parameter $hook is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

128
function legacy_urls_blog_forward(/** @scrutinizer ignore-unused */ $hook, $type, $result) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
129
130
	$page = $result['segments'];
131
132
	// easier to work with and no notices
133
	$page = array_pad($page, 4, "");
134
135
	// group usernames
136
	if (preg_match('~/group\:([0-9]+)/~', "/{$page[0]}/{$page[1]}/", $matches)) {
137
		$guid = $matches[1];
138
		$entity = get_entity($guid);
139
		if (elgg_instanceof($entity, 'group')) {
140
			if (!empty($page[2])) {
141
				$url = "blog/view/$page[2]/";
142
			} else {
143
				$url = "blog/group/$guid/all";
144
			}
145
			// we drop query params because the old group URLs were invalid
146
			legacy_urls_redirect(legacy_urls_prepare_url($url));
147
			return false;
148
		}
149
	}
150
151
	if (empty($page[0])) {
152
		return;
153
	}
154
155
	if ($page[0] == "read") {
156
		$url = "blog/view/{$page[1]}/";
157
		legacy_urls_redirect(legacy_urls_prepare_url($url));
158
		return false;
159
	}
160
161
	// user usernames
162
	$user = get_user_by_username($page[0]);
163
	if (!$user) {
164
		return;
165
	}
166
167
	if (empty($page[1])) {
168
		$page[1] = 'owner';
169
	}
170
171
	switch ($page[1]) {
172
		case "read":
173
			$url = "blog/view/{$page[2]}/{$page[3]}";
174
			break;
175
		case "archive":
176
			$url = "blog/archive/{$page[0]}/{$page[2]}/{$page[3]}";
177
			break;
178
		case "friends":
179
			$url = "blog/friends/{$page[0]}";
180
			break;
181
		case "new":
182
			$url = "blog/add/$user->guid";
183
			break;
184
		case "owner":
185
			$url = "blog/owner/{$page[0]}";
186
			break;
187
	}
188
189
	if (isset($url)) {
190
		legacy_urls_redirect(legacy_urls_prepare_url($url));
191
		return false;
192
	}
193
}
194
195
/**
196
 * Bookmarks forwarder
197
 * /bookmarks/group:<group_guid>/
198
 * /bookmarks/gorup:<group_guid>/read/<guid>/
199
 * /bookmarks/read/<guid>
200
 * /bookmarks/<username>[/(items|read|inbox|friends|add|bookmarklet)/<guid>]
201
 *
202
 * @param string $hook   "route"
203
 * @param string $type   "bookmarks"
204
 * @param array  $result Old identifier and segments
205
 *
206
 * @return void|false
207
 */
208
function legacy_urls_bookmarks_forward($hook, $type, $result) {
2 ignored issues
show
The parameter $type is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

208
function legacy_urls_bookmarks_forward($hook, /** @scrutinizer ignore-unused */ $type, $result) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
The parameter $hook is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

208
function legacy_urls_bookmarks_forward(/** @scrutinizer ignore-unused */ $hook, $type, $result) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
209
210
	$page = $result['segments'];
211
212
	// easier to work with and no notices
213
	$page = array_pad($page, 4, "");
214
215
	// old group usernames
216
	if (substr_count($page[0], 'group:')) {
217
		preg_match('/group\:([0-9]+)/i', $page[0], $matches);
218
		$guid = $matches[1];
219
		$entity = get_entity($guid);
220
		if (elgg_instanceof($entity, 'group')) {
221
			if (!empty($page[2])) {
222
				$url = "bookmarks/view/$page[2]/";
223
			} else {
224
				$url = "bookmarks/group/$guid/all";
225
			}
226
			// we drop query params because the old group URLs were invalid
227
			legacy_urls_redirect(legacy_urls_prepare_url($url));
228
		}
229
	}
230
231
	if ($page[0] == "read") {
232
		$url = "bookmarks/view/{$page[1]}/";
233
		legacy_urls_redirect(legacy_urls_prepare_url($url));
234
		return false;
235
	}
236
237
	$user = get_user_by_username($page[0]);
238
	if (!$user) {
239
		return;
240
	}
241
242
	if (empty($page[1])) {
243
		$page[1] = 'items';
244
	}
245
246
	switch ($page[1]) {
247
		case "read":
248
			$url = "bookmarks/view/{$page[2]}/{$page[3]}";
249
			break;
250
		case "inbox":
251
			$url = "bookmarks/inbox/{$page[0]}";
252
			break;
253
		case "friends":
254
			$url = "bookmarks/friends/{$page[0]}";
255
			break;
256
		case "add":
257
			$url = "bookmarks/add/{$page[0]}";
258
			break;
259
		case "items":
260
			$url = "bookmarks/owner/{$page[0]}";
261
			break;
262
		case "bookmarklet":
263
			$url = "bookmarks/bookmarklet/{$page[0]}";
264
			break;
265
	}
266
267
	if (isset($url)) {
268
		legacy_urls_redirect(legacy_urls_prepare_url($url));
269
		return false;
270
	}
271
}
272
273
/**
274
 * File forwarder
275
 * /file/read/<guid>
276
 *
277
 * @param string $hook   "route"
278
 * @param string $type   "file"
279
 * @param array  $result Old identifier and segments
280
 *
281
 * @return void|false
282
 */
283
function legacy_urls_file_forward($hook, $type, $result) {
2 ignored issues
show
The parameter $hook is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

283
function legacy_urls_file_forward(/** @scrutinizer ignore-unused */ $hook, $type, $result) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
The parameter $type is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

283
function legacy_urls_file_forward($hook, /** @scrutinizer ignore-unused */ $type, $result) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
284
285
	$page = $result['segments'];
286
287
	// easier to work with and no notices
288
	$page = array_pad($page, 4, "");
289
290
	if ($page[0] == 'read') {
291
		$url = "file/view/{$page[1]}";
292
		legacy_urls_redirect(legacy_urls_prepare_url($url));
293
		return false;
294
	}
295
}
296
297
/**
298
 * Groups forwarder
299
 * /groups/<guid>
300
 * /groups/forum/<guid>
301
 *
302
 * @param string $hook   "route"
303
 * @param string $type   "groups"
304
 * @param array  $result Old identifier and segments
305
 *
306
 * @return void|false
307
 */
308
function legacy_urls_groups_forward($hook, $type, $result) {
2 ignored issues
show
The parameter $hook is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

308
function legacy_urls_groups_forward(/** @scrutinizer ignore-unused */ $hook, $type, $result) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
The parameter $type is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

308
function legacy_urls_groups_forward($hook, /** @scrutinizer ignore-unused */ $type, $result) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
309
310
	$page = $result['segments'];
311
312
	// easier to work with and no notices
313
	$page = array_pad($page, 4, "");
314
315
	if (is_numeric($page[0])) {
316
		$group = get_entity($page[0]);
317
		if ($group instanceof \ElggGroup) {
318
			legacy_urls_redirect(legacy_urls_prepare_url($group->getURL()));
319
			return false;
320
		}
321
	}
322
323
	if ($page[0] == 'forum') {
324
		$url = "discussion/owner/{$page[1]}";
325
		legacy_urls_redirect(legacy_urls_prepare_url($url));
326
		return false;
327
	}
328
}
329
330
/**
331
 * User settings forwarder
332
 * /settings/plugins/
333
 *
334
 * @param string $hook   "route"
335
 * @param string $type   "settings"
336
 * @param array  $result Old identifier and segments
337
 *
338
 * @return void|false
339
 */
340
function legacy_urls_settings_forward($hook, $type, $result) {
341
342
	$page = $result['segments'];
343
344
	// easier to work with and no notices
345
	$page = array_pad($page, 4, "");
346
	
347
	if ($page[0] == "plugins") {
348
		if (empty($page[2])) {
349
			$url = "settings";
350
			if (!empty($page[1])) {
351
				$url .= "/user/" . $page[1];
352
			}
353
			legacy_urls_redirect(legacy_urls_prepare_url($url));
354
			return false;
355
		}
356
	}
357
}
358
359
/**
360
 * Group forum forwarder
361
 * /forum/.*
362
 *
363
 * @param array $page URL segments
364
 *
365
 * @return bool
366
 */
367
function legacy_urls_forum_handler($page) {
368
	switch ($page[0]) {
369
		case 'topic':
370
			$url = "discussion/view/{$page[1]}/{$page[2]}";
371
			legacy_urls_redirect(legacy_urls_prepare_url($url));
372
			return true;
373
		default:
374
			return false;
375
	}
376
}
377
378
/**
379
 * Messageboard forwarder
380
 * /messageboard/!(owner|add|group)
381
 *
382
 * @param string $hook   "route"
383
 * @param string $type   "messageboard"
384
 * @param array  $result Old identifier and segments
385
 *
386
 * @return void|false
387
 */
388
function legacy_urls_messageboard_forward($hook, $type, $result) {
2 ignored issues
show
The parameter $hook is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

388
function legacy_urls_messageboard_forward(/** @scrutinizer ignore-unused */ $hook, $type, $result) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
The parameter $type is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

388
function legacy_urls_messageboard_forward($hook, /** @scrutinizer ignore-unused */ $type, $result) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
389
390
	$page = $result['segments'];
391
392
	// easier to work with and no notices
393
	$page = array_pad($page, 4, "");
394
395
	// if the first part is a username, forward to new format
396
	$new_section_one = ['owner', 'add', 'group'];
397
	if (isset($page[0]) && !in_array($page[0], $new_section_one) && get_user_by_username($page[0])) {
398
		$url = "messageboard/owner/{$page[0]}";
399
		legacy_urls_redirect(legacy_urls_prepare_url($url));
400
		return false;
401
	}
402
}
403
404
return function() {
405 18
	elgg_register_event_handler('init', 'system', 'legacy_urls_init');
406
};
407