-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathURL.class.php
176 lines (142 loc) · 4.63 KB
/
URL.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
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
<?php namespace EmPHyre;
/**
* URL is a class to handle all sorts of URL-related functions
* An object for the EmPHyre project
*
* PHP version 7
*
* ------
* These files are part of the empiresPHPframework;
* The original framework core (specifically the mysql.php
* the router.php and the errorlog) was started by Timo Ewalds,
* and rewritten to use APC and extended by Julian Haagsma,
* for use in Earth Empires (located at http://www.earthempires.com );
* it was spun out for use on other projects.
*
* The general.php contains content from Earth Empires
* written by Dave McVittie and Joe Obbish.
*
* The example website files were written by Julian Haagsma.
*
* @category Core
* @package EmPHyre
* @author Julian Haagsma <jhaagsma@gmail.com>
* @author Dave McVittie <dave.mcvittie@gmail.com>
* @license All files are licensed under the MIT License.
* @link https://github.com/jhaagsma/emPHyre
* @since Recombined from empiresPHPframework extensions on Nov 4 2016
*/
class URL
{
/**
* Protected construct so that URLHandler can't be instantiated
*
* @return null
*/
protected function __construct()
{
}//end __construct()
public static function redirect($loc, $options = [], $code = 303)
{
$ext = null;
if ($options) {
$ext = '?' . http_build_query($options);
}
$loc .= $ext;
header("Location: $loc", true, $code);
echo "Redirecting to: <a href='".htmlentities($loc)."'>$loc</a>";
exit;
}//end redirect()
public static function moved($loc, $options = [])
{
self::redirect($loc, $options, 301);
}//end moved()
public static function sendJsHeader()
{
header('Content-Type: application/javascript');
}//end sendJsHeader()
public static function sendJsonHeader()
{
header('Content-Type: application/json');
}//end sendJsonHeader()
public static function getUriPart($token_num)
{
$parts = explode('/', trim(getenv('REQUEST_URI'), '/'));
if ($token_num < 0) {
return $parts[count($parts) + $token_num] ?? false;
} else {
return $parts[$token_num] ?? false;
}
}//end getUriPart()
public static function getUrlPart($token_num)
{
$parts = explode('.', $_SERVER['SERVER_NAME']);
if ($token_num < 0) {
return $parts[count($parts) + $token_num] ?? false;
} else {
return $parts[$token_num] ?? false;
}
}//end getUrlPart()
public static function getCOOKIEval($name, $type = 'string', $default = null)
{
$var = $_COOKIE[$name] ?? $default;
settype($var, $type);
return $var;
}//end getCOOKIEval()
public static function getDomainName()
{
return self::getUrlPart(-2).'.'.self::getUrlPart(-1);
}//end getDomainName()
public static function oneLessSubdomain($domain_name = null)
{
$parts = explode('.', ($domain_name ? $domain_name : $_SERVER['SERVER_NAME']));
if (count($parts) < 2) {
return $_SERVER['SERVER_NAME'];
}
unset($parts[0]);
return implode('.', $parts);
}//end oneLessSubdomain()
public static function parent($url, $levels = 1)
{
$url = trim($url, '/');
$url = explode('/', $url);
$count = count($url);
if ($count > $levels - 1) {
$i = 1;
while ($i <= $levels) {
unset($url[$count - $i]);
$i++;
}
}
$url = '/'.implode('/', $url);
return $url;
}//end parent()
public static function gparent($url)
{
return self::parent($url, 2);
}//end gparent()
public static function returnGetData($data)
{
$get = null;
foreach ($data as $key => $val) {
if (is_array($val)) {
foreach ($val as $subkey => $subval) {
$get .= '&'.urlencode($key).'['.$subkey.']='.urlencode($subval);
}
continue;
}
$get .= '&'.urlencode($key).'='.urlencode($val);
}
return $get;
}//end returnGetData()
//http://php.net/manual/en/function.base64-encode.php#103849
public static function encode64($data)
{
return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
}//end encode64()
//http://php.net/manual/en/function.base64-encode.php#103849
public static function decode64($data)
{
return base64_decode(str_pad(strtr($data, '-_', '+/'), strlen($data) % 4, '=', STR_PAD_RIGHT));
}//end decode64()
}//end class