Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
56.58% |
43 / 76 |
|
30.77% |
4 / 13 |
CRAP | |
0.00% |
0 / 1 |
| tao_helpers_Uri | |
56.58% |
43 / 76 |
|
30.77% |
4 / 13 |
210.78 | |
0.00% |
0 / 1 |
| getBaseUrl | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
20 | |||
| getRootUrl | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
4.13 | |||
| url | |
73.33% |
11 / 15 |
|
0.00% |
0 / 1 |
10.54 | |||
| legacyUrl | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| encode | |
83.33% |
5 / 6 |
|
0.00% |
0 / 1 |
3.04 | |||
| encodeRelativeUrl | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| decode | |
83.33% |
5 / 6 |
|
0.00% |
0 / 1 |
3.04 | |||
| isUriEncoded | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| encodeArray | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
72 | |||
| getUniqueId | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| getPath | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
| getDomain | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| isValidAsCookieDomain | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2008-2010 (original work) Deutsche Institut für Internationale Pädagogische Forschung |
| 19 | * (under the project TAO-TRANSFER); |
| 20 | * 2009-2012 (update and modification) Public Research Centre Henri Tudor |
| 21 | * (under the project TAO-SUSTAIN & TAO-DEV); |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | /** |
| 26 | * Utilities on URL/URI |
| 27 | * |
| 28 | * @access public |
| 29 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 30 | * @package tao |
| 31 | */ |
| 32 | class tao_helpers_Uri |
| 33 | { |
| 34 | /** |
| 35 | * the base url |
| 36 | * |
| 37 | * @access private |
| 38 | * @var string |
| 39 | */ |
| 40 | private static $base = ''; |
| 41 | |
| 42 | /** |
| 43 | * Short description of attribute root |
| 44 | * |
| 45 | * @access private |
| 46 | * @var string |
| 47 | */ |
| 48 | private static $root = ''; |
| 49 | |
| 50 | /** |
| 51 | * Short description of attribute ENCODE_ARRAY_KEYS |
| 52 | * |
| 53 | * @access public |
| 54 | * @var int |
| 55 | */ |
| 56 | public const ENCODE_ARRAY_KEYS = 1; |
| 57 | |
| 58 | /** |
| 59 | * Short description of attribute ENCODE_ARRAY_VALUES |
| 60 | * |
| 61 | * @access public |
| 62 | * @var int |
| 63 | */ |
| 64 | public const ENCODE_ARRAY_VALUES = 2; |
| 65 | |
| 66 | /** |
| 67 | * Short description of attribute ENCODE_ARRAY_ALL |
| 68 | * |
| 69 | * @access public |
| 70 | * @var int |
| 71 | */ |
| 72 | public const ENCODE_ARRAY_ALL = 3; |
| 73 | |
| 74 | /** |
| 75 | * get the project base url |
| 76 | * |
| 77 | * @access public |
| 78 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 79 | * @return string |
| 80 | */ |
| 81 | public static function getBaseUrl() |
| 82 | { |
| 83 | if (empty(self::$base) && defined('BASE_URL')) { |
| 84 | self::$base = BASE_URL; |
| 85 | if (!preg_match("/\/$/", self::$base)) { |
| 86 | self::$base .= '/'; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | return self::$base; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Short description of method getRootUrl |
| 95 | * |
| 96 | * @access public |
| 97 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 98 | * @return string |
| 99 | */ |
| 100 | public static function getRootUrl() |
| 101 | { |
| 102 | if (empty(self::$root) && defined('ROOT_URL')) { |
| 103 | self::$root = ROOT_URL; |
| 104 | if (!preg_match("/\/$/", self::$root)) { |
| 105 | self::$root .= '/'; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | return self::$root; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * conveniance method to create urls based on the current MVC context and |
| 114 | * it for the used kind of url resolving |
| 115 | * |
| 116 | * @access public |
| 117 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 118 | * @param string action |
| 119 | * @param string module |
| 120 | * @param string extension |
| 121 | * @param array|string params |
| 122 | * @return string |
| 123 | */ |
| 124 | public static function url($action = null, $module = null, $extension = null, $params = []) |
| 125 | { |
| 126 | |
| 127 | if (is_null($module)) { |
| 128 | $module = Context::getInstance()->getModuleName(); |
| 129 | } |
| 130 | if (is_null($action)) { |
| 131 | $action = Context::getInstance()->getActionName(); |
| 132 | } |
| 133 | if (is_null($extension)) { |
| 134 | $extension = Context::getInstance()->getExtensionName(); |
| 135 | } |
| 136 | |
| 137 | $returnValue = self::getRootUrl() . $extension . '/' . $module . '/' . $action; |
| 138 | |
| 139 | if (is_string($params) && strlen($params) > 0) { |
| 140 | $returnValue .= '?' . $params; |
| 141 | } |
| 142 | |
| 143 | if (is_array($params) && count($params)) { |
| 144 | $returnValue .= '?'; |
| 145 | foreach ($params as $key => $value) { |
| 146 | $returnValue .= $key . '=' . rawurlencode($value) . '&'; |
| 147 | } |
| 148 | $returnValue = substr($returnValue, 0, -1); |
| 149 | } |
| 150 | |
| 151 | return $returnValue; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * format propertly an ol style url |
| 156 | * |
| 157 | * @access public |
| 158 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 159 | * @param string url |
| 160 | * @param array params |
| 161 | * @return string |
| 162 | */ |
| 163 | public static function legacyUrl($url, $params = []) |
| 164 | { |
| 165 | return ''; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * encode an URI |
| 170 | * |
| 171 | * @access public |
| 172 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 173 | * @param string uri |
| 174 | * @param boolean dotMode |
| 175 | * @return string |
| 176 | */ |
| 177 | public static function encode($uri, $dotMode = true) |
| 178 | { |
| 179 | if (0 === strpos($uri, 'http')) { |
| 180 | //return base64_encode($uri); |
| 181 | if ($dotMode) { |
| 182 | //order matters here don't change the _4_ position |
| 183 | $returnValue = str_replace(['#', '://', '/', '.', ':'], ['_3_', '_2_', '_1_', '_0_', '_4_'], $uri); |
| 184 | } else { |
| 185 | $returnValue = str_replace(['#', '://', '/', ':'], ['_3_', '_2_', '_1_', '_4_'], $uri); |
| 186 | } |
| 187 | } else { |
| 188 | $returnValue = $uri; |
| 189 | } |
| 190 | |
| 191 | return $returnValue; |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * encode a relative URL |
| 196 | * |
| 197 | * @access public |
| 198 | * @param string uri |
| 199 | * @param boolean dotMode |
| 200 | * @return string |
| 201 | */ |
| 202 | public static function encodeRelativeUrl($url) |
| 203 | { |
| 204 | $url = str_replace(DIRECTORY_SEPARATOR, '/', $url); |
| 205 | $parts = explode('/', $url); |
| 206 | foreach (array_keys($parts) as $key) { |
| 207 | $parts[$key] = rawurlencode($parts[$key]); |
| 208 | } |
| 209 | return implode('/', $parts); |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * decode an URI |
| 214 | * |
| 215 | * @access public |
| 216 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 217 | * @param string $uri |
| 218 | * @param boolean $dotMode |
| 219 | * @return string |
| 220 | */ |
| 221 | public static function decode($uri, $dotMode = true) |
| 222 | { |
| 223 | if (0 === strpos($uri, 'http')) { |
| 224 | //return base64_decode($uri); |
| 225 | if ($dotMode) { |
| 226 | //$returnValue = urldecode(str_replace('__', '.', $uri)); |
| 227 | //$returnValue = str_replace('w_org', 'w3.org', $returnValue); |
| 228 | $returnValue = str_replace(['_0_', '_1_', '_2_', '_3_', '_4_'], ['.', '/', '://', '#', ':'], $uri); |
| 229 | } else { |
| 230 | $returnValue = str_replace(['_1_', '_2_', '_3_', '_4_'], ['/', '://', '#', ':'], $uri); |
| 231 | } |
| 232 | } else { |
| 233 | $returnValue = $uri; |
| 234 | } |
| 235 | |
| 236 | |
| 237 | return (string)$returnValue; |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * @param string $decodedUri |
| 242 | * |
| 243 | * @return bool |
| 244 | */ |
| 245 | public static function isUriEncoded($decodedUri) |
| 246 | { |
| 247 | return preg_match('/^[a-z]*_2_/', $decodedUri) === 1; |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Encode the uris composing either the keys or the values of the array in |
| 252 | * |
| 253 | * @access public |
| 254 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 255 | * @param array uris |
| 256 | * @param int encodeMode |
| 257 | * @param boolean dotMode |
| 258 | * @param boolean uniqueMode |
| 259 | * @return array |
| 260 | */ |
| 261 | public static function encodeArray( |
| 262 | $uris, |
| 263 | $encodeMode = self::ENCODE_ARRAY_ALL, |
| 264 | $dotMode = true, |
| 265 | $uniqueMode = false |
| 266 | ) { |
| 267 | $returnValue = []; |
| 268 | |
| 269 | if (is_array($uris)) { |
| 270 | foreach ($uris as $key => $value) { |
| 271 | if ($encodeMode == self::ENCODE_ARRAY_KEYS || $encodeMode == self::ENCODE_ARRAY_ALL) { |
| 272 | $key = self::encode($key, $dotMode); |
| 273 | } |
| 274 | if ($encodeMode == self::ENCODE_ARRAY_VALUES || $encodeMode == self::ENCODE_ARRAY_ALL) { |
| 275 | $value = self::encode($value, $dotMode); |
| 276 | } |
| 277 | $returnValue[$key] = $value; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | if ($uniqueMode) { |
| 282 | $returnValue = array_unique($returnValue); |
| 283 | } |
| 284 | |
| 285 | return $returnValue; |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * generate a semi unique id, that is used |
| 290 | * in folder names. |
| 291 | * @todo Remove the need for this function |
| 292 | * |
| 293 | * @access public |
| 294 | * @author sam@taotesting.com |
| 295 | * @param string uriResource |
| 296 | * @return string |
| 297 | */ |
| 298 | public static function getUniqueId($uriResource) |
| 299 | { |
| 300 | $returnValue = ''; |
| 301 | |
| 302 | if (stripos($uriResource, "#") > 0) { |
| 303 | $returnValue = substr($uriResource, stripos($uriResource, "#") + 1); |
| 304 | } |
| 305 | |
| 306 | return $returnValue; |
| 307 | } |
| 308 | |
| 309 | /** |
| 310 | * Returns the path from a URI. In other words, it returns what comes after |
| 311 | * domain but before the query string. If |
| 312 | * is given as a parameter value, '/path/to/something' is returned. If an |
| 313 | * occurs, null will be returned. |
| 314 | * |
| 315 | * @access public |
| 316 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 317 | * @param string uri A Uniform Resource Identifier (URI). |
| 318 | * @return string|null |
| 319 | */ |
| 320 | public static function getPath($uri) |
| 321 | { |
| 322 | if (preg_match("/^[A-Za-z0-9]*$/", $uri)) { |
| 323 | // no '.', no '/', ... does not look good. |
| 324 | return null; |
| 325 | } |
| 326 | |
| 327 | $returnValue = parse_url($uri, PHP_URL_PATH); |
| 328 | if (empty($returnValue)) { |
| 329 | return null; |
| 330 | } |
| 331 | |
| 332 | return $returnValue; |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * Returns the domain extracted from a given URI. If the domain cannot be |
| 337 | * null is returned. |
| 338 | * |
| 339 | * @access public |
| 340 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 341 | * @param string uri A Uniform Resource Identifier (URI). |
| 342 | * @return string|null |
| 343 | */ |
| 344 | public static function getDomain($uri) |
| 345 | { |
| 346 | $returnValue = parse_url($uri, PHP_URL_HOST); |
| 347 | if (empty($returnValue)) { |
| 348 | return null; |
| 349 | } |
| 350 | |
| 351 | return $returnValue; |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * To be used to know if a given URI is valid as a cookie domain. Usually, |
| 356 | * domain such as 'mytaoplatform', 'localhost' make issues with |
| 357 | * |
| 358 | * @access public |
| 359 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 360 | * @param string uri |
| 361 | * @return boolean |
| 362 | */ |
| 363 | public static function isValidAsCookieDomain($uri) |
| 364 | { |
| 365 | $domain = self::getDomain($uri); |
| 366 | if (!empty($domain)) { |
| 367 | if (preg_match("/^[a-z0-9\-]+(?:[a-z0-9\-]\.)+/iu", $domain) > 0) { |
| 368 | $returnValue = true; |
| 369 | } else { |
| 370 | $returnValue = false; |
| 371 | } |
| 372 | } else { |
| 373 | $returnValue = false; |
| 374 | } |
| 375 | |
| 376 | return $returnValue; |
| 377 | } |
| 378 | } |