| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
@copyright |
|---|
| 10 |
@author will@flourishlib.com |
|---|
| 11 |
@license http://flourishlib.com/license |
|---|
| 12 |
|
|---|
| 13 |
@package |
|---|
| 14 |
@link http://flourishlib.com/fURL |
|---|
| 15 |
|
|---|
| 16 |
@version |
|---|
| 17 |
@changes |
|---|
| 18 |
|
|---|
| 19 |
class fURL |
|---|
| 20 |
{ |
|---|
| 21 |
|
|---|
| 22 |
const get = 'fURL::get'; |
|---|
| 23 |
const getDomain = 'fURL::getDomain'; |
|---|
| 24 |
const getQueryString = 'fURL::getQueryString'; |
|---|
| 25 |
const getWithQueryString = 'fURL::getWithQueryString'; |
|---|
| 26 |
const makeFriendly = 'fURL::makeFriendly'; |
|---|
| 27 |
const redirect = 'fURL::redirect'; |
|---|
| 28 |
const removeFromQueryString = 'fURL::removeFromQueryString'; |
|---|
| 29 |
const replaceInQueryString = 'fURL::replaceInQueryString'; |
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
@return |
|---|
| 39 |
|
|---|
| 40 |
static public function get() |
|---|
| 41 |
{ |
|---|
| 42 |
return preg_replace('#\?.*$#', '', $_SERVER['REQUEST_URI']); |
|---|
| 43 |
} |
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
@return |
|---|
| 50 |
|
|---|
| 51 |
static public function getDomain() |
|---|
| 52 |
{ |
|---|
| 53 |
return ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https://' : 'http://') . $_SERVER['SERVER_NAME']; |
|---|
| 54 |
} |
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
@return |
|---|
| 61 |
|
|---|
| 62 |
static public function getQueryString() |
|---|
| 63 |
{ |
|---|
| 64 |
return preg_replace('#^[^?]*\??#', '', $_SERVER['REQUEST_URI']); |
|---|
| 65 |
} |
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
@return |
|---|
| 72 |
|
|---|
| 73 |
static public function getWithQueryString() |
|---|
| 74 |
{ |
|---|
| 75 |
return $_SERVER['REQUEST_URI']; |
|---|
| 76 |
} |
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
@param |
|---|
| 83 |
@return |
|---|
| 84 |
|
|---|
| 85 |
static public function makeFriendly($string) |
|---|
| 86 |
{ |
|---|
| 87 |
$string = fHTML::decode(fUTF8::ascii($string)); |
|---|
| 88 |
$string = strtolower(trim($string)); |
|---|
| 89 |
$string = str_replace("'", '', $string); |
|---|
| 90 |
$string = preg_replace('#[^a-z0-9\-]+#', '_', $string); |
|---|
| 91 |
return preg_replace('#_+$#', '', $string); |
|---|
| 92 |
} |
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 |
|
|---|
| 98 |
@param |
|---|
| 99 |
@return |
|---|
| 100 |
|
|---|
| 101 |
static public function redirect($url) |
|---|
| 102 |
{ |
|---|
| 103 |
if (strpos($url, '/') === 0) { |
|---|
| 104 |
$url = self::getDomain() . $url; |
|---|
| 105 |
} elseif (!preg_match('#^https?://#i', $url)) { |
|---|
| 106 |
$url = self::getDomain() . self::get() . $url; |
|---|
| 107 |
} |
|---|
| 108 |
|
|---|
| 109 |
|
|---|
| 110 |
if (substr($url, -1) == '?') { |
|---|
| 111 |
$url = substr($url, 0, -1); |
|---|
| 112 |
} |
|---|
| 113 |
|
|---|
| 114 |
header('Location: ' . $url); |
|---|
| 115 |
exit($url); |
|---|
| 116 |
} |
|---|
| 117 |
|
|---|
| 118 |
|
|---|
| 119 |
|
|---|
| 120 |
|
|---|
| 121 |
|
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 |
|
|---|
| 125 |
@param |
|---|
| 126 |
@param |
|---|
| 127 |
@return |
|---|
| 128 |
|
|---|
| 129 |
static public function removeFromQueryString() |
|---|
| 130 |
{ |
|---|
| 131 |
$parameters = func_get_args(); |
|---|
| 132 |
|
|---|
| 133 |
parse_str(self::getQueryString(), $qs_array); |
|---|
| 134 |
if (get_magic_quotes_gpc()) { |
|---|
| 135 |
$qs_array = array_map('stripslashes', $qs_array); |
|---|
| 136 |
} |
|---|
| 137 |
|
|---|
| 138 |
foreach ($parameters as $parameter) { |
|---|
| 139 |
unset($qs_array[$parameter]); |
|---|
| 140 |
} |
|---|
| 141 |
|
|---|
| 142 |
return '?' . http_build_query($qs_array, '', '&'); |
|---|
| 143 |
} |
|---|
| 144 |
|
|---|
| 145 |
|
|---|
| 146 |
|
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 |
|
|---|
| 151 |
|
|---|
| 152 |
@param |
|---|
| 153 |
@param |
|---|
| 154 |
@return |
|---|
| 155 |
|
|---|
| 156 |
static public function replaceInQueryString($parameter, $value) |
|---|
| 157 |
{ |
|---|
| 158 |
parse_str(self::getQueryString(), $qs_array); |
|---|
| 159 |
if (get_magic_quotes_gpc()) { |
|---|
| 160 |
$qs_array = array_map('stripslashes', $qs_array); |
|---|
| 161 |
} |
|---|
| 162 |
|
|---|
| 163 |
settype($parameter, 'array'); |
|---|
| 164 |
settype($value, 'array'); |
|---|
| 165 |
|
|---|
| 166 |
if (sizeof($parameter) != sizeof($value)) { |
|---|
| 167 |
fCore::toss( |
|---|
| 168 |
'fProgrammerException', |
|---|
| 169 |
fGrammar::compose( |
|---|
| 170 |
"There are a different number of parameters and values.\nParameters:\n%1\$s\nValues\n%2\$s", |
|---|
| 171 |
fCore::dump($parameter), |
|---|
| 172 |
fCore::dump($value) |
|---|
| 173 |
) |
|---|
| 174 |
); |
|---|
| 175 |
} |
|---|
| 176 |
|
|---|
| 177 |
for ($i=0; $i<sizeof($parameter); $i++) { |
|---|
| 178 |
$qs_array[$parameter[$i]] = $value[$i]; |
|---|
| 179 |
} |
|---|
| 180 |
|
|---|
| 181 |
return '?' . http_build_query($qs_array, '', '&'); |
|---|
| 182 |
} |
|---|
| 183 |
|
|---|
| 184 |
|
|---|
| 185 |
|
|---|
| 186 |
|
|---|
| 187 |
|
|---|
| 188 |
@return |
|---|
| 189 |
|
|---|
| 190 |
private function __construct() { } |
|---|
| 191 |
} |
|---|
| 192 |
|
|---|
| 193 |
|
|---|
| 194 |
|
|---|
| 195 |
|
|---|
| 196 |
will@flourishlib.com |
|---|
| 197 |
|
|---|
| 198 |
|
|---|
| 199 |
|
|---|
| 200 |
|
|---|
| 201 |
|
|---|
| 202 |
|
|---|
| 203 |
|
|---|
| 204 |
|
|---|
| 205 |
|
|---|
| 206 |
|
|---|
| 207 |
|
|---|
| 208 |
|
|---|
| 209 |
|
|---|
| 210 |
|
|---|
| 211 |
|
|---|
| 212 |
|
|---|
| 213 |
|
|---|
| 214 |
|
|---|
| 215 |
|
|---|