-
Notifications
You must be signed in to change notification settings - Fork 0
/
internal.router.class.php
91 lines (76 loc) · 2.6 KB
/
internal.router.class.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
<?php
# # Used by router class to change HTTP status # #
namespace router_;
class Router_ {
/**
* Redirects the user on request, cannot process more code after execution.
*
* @author Rageous0
*
* @param http The HTTP status code for the page. 200 is default and is not necessary.
* @param url The URL to redirect to on request.
* @return Renders data from the callback function.
*/
public function redirect($url, $http=301) {
if(gettype($url) !== 'string') throw new Exception('Cannot resolve $url to type of string.');
if(gettype($http) !== 'string' and gettype($http) !== 'integer') throw new Exception('Cannot resolve $http to the type of string or integer.');
if(!in_array(intval($http), self::REDIRECT_STATUSES)) throw new Exception('Invalid HTTP status for redirection, valid statuses for redirection are 301 and 302.');
if(!preg_match('/((http(s)?:\/\/)?(([\w\d]+)\.([A-Z])))/i', $url)) throw new Exception('Invalid URL form. Valid URL form requires http://, https:// or no scheme. And needs to be in the same format as example.com.');
try {
if($http == 301) header("HTTP/1.1 301 Moved Permanently");
if($http == 302) header("HTTP/1.0 302 Noved temporarily");
header("location: ${url}");
exit;
} catch(Exception $e) {
throw new Exception($e);
}
}
/**
* Sets the content type to json.
*
* @author Rageous0
*
* @return The content type for json - application/json.
*/
public function json() {
try {
header('Content-Type: application/json');
return true;
} catch(Exception $e) {
return false;
}
}
/**
* Sets the content type to xml.
*
* @author Rageous0
*
* @return The content type for xml - application/xml.
*/
public function xml() {
try {
header('Content-Type: application/xml');
return true;
} catch(Exception $e) {
return false;
}
}
/**
* Sets the content type to plaintext.
*
* @author Rageous0
*
* @return The content type for plaintext - text/plain.
*/
public function plain() {
try {
header('Content-Type: text/plain');
return true;
} catch(Exception $e) {
return false;
}
}
// List of HTTP status codes supported, used by the status and redirect functions.
private const REDIRECT_STATUSES = array(301, 302);
}
?>