|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00012 require_once(dirname(__FILE__) . '/encoding.php'); 00025 class SimpleUrl { 00026 var $_scheme; 00027 var $_username; 00028 var $_password; 00029 var $_host; 00030 var $_port; 00031 var $_path; 00032 var $_request; 00033 var $_fragment; 00034 var $_x; 00035 var $_y; 00036 var $_target; 00037 var $_raw = false; 00038 00044 function SimpleUrl($url = '') { 00045 list($x, $y) = $this->_chompCoordinates($url); 00046 $this->setCoordinates($x, $y); 00047 $this->_scheme = $this->_chompScheme($url); 00048 list($this->_username, $this->_password) = $this->_chompLogin($url); 00049 $this->_host = $this->_chompHost($url); 00050 $this->_port = false; 00051 if (preg_match('/(.*?):(.*)/', $this->_host, $host_parts)) { 00052 $this->_host = $host_parts[1]; 00053 $this->_port = (integer)$host_parts[2]; 00054 } 00055 $this->_path = $this->_chompPath($url); 00056 $this->_request = $this->_parseRequest($this->_chompRequest($url)); 00057 $this->_fragment = (strncmp($url, "#", 1) == 0 ? substr($url, 1) : false); 00058 $this->_target = false; 00059 } 00060 00068 function _chompCoordinates(&$url) { 00069 if (preg_match('/(.*)\?(\d+),(\d+)$/', $url, $matches)) { 00070 $url = $matches[1]; 00071 return array((integer)$matches[2], (integer)$matches[3]); 00072 } 00073 return array(false, false); 00074 } 00075 00083 function _chompScheme(&$url) { 00084 if (preg_match('/^([^\/:]*):(\/\/)(.*)/', $url, $matches)) { 00085 $url = $matches[2] . $matches[3]; 00086 return $matches[1]; 00087 } 00088 return false; 00089 } 00090 00101 function _chompLogin(&$url) { 00102 $prefix = ''; 00103 if (preg_match('/^(\/\/)(.*)/', $url, $matches)) { 00104 $prefix = $matches[1]; 00105 $url = $matches[2]; 00106 } 00107 if (preg_match('/^([^\/]*)@(.*)/', $url, $matches)) { 00108 $url = $prefix . $matches[2]; 00109 $parts = explode(":", $matches[1]); 00110 return array( 00111 urldecode($parts[0]), 00112 isset($parts[1]) ? urldecode($parts[1]) : false); 00113 } 00114 $url = $prefix . $url; 00115 return array(false, false); 00116 } 00117 00129 function _chompHost(&$url) { 00130 if (preg_match('/^(\/\/)(.*?)(\/.*|\?.*|#.*|$)/', $url, $matches)) { 00131 $url = $matches[3]; 00132 return $matches[2]; 00133 } 00134 if (preg_match('/(.*?)(\.\.\/|\.\/|\/|\?|#|$)(.*)/', $url, $matches)) { 00135 $tlds = SimpleUrl::getAllTopLevelDomains(); 00136 if (preg_match('/[a-z0-9\-]+\.(' . $tlds . ')/i', $matches[1])) { 00137 $url = $matches[2] . $matches[3]; 00138 return $matches[1]; 00139 } elseif (preg_match('/[a-z0-9\-]+\.[a-z0-9\-]+\.[a-z0-9\-]+/i', $matches[1])) { 00140 $url = $matches[2] . $matches[3]; 00141 return $matches[1]; 00142 } 00143 } 00144 return false; 00145 } 00146 00155 function _chompPath(&$url) { 00156 if (preg_match('/(.*?)(\?|#|$)(.*)/', $url, $matches)) { 00157 $url = $matches[2] . $matches[3]; 00158 return ($matches[1] ? $matches[1] : ''); 00159 } 00160 return ''; 00161 } 00162 00170 function _chompRequest(&$url) { 00171 if (preg_match('/\?(.*?)(#|$)(.*)/', $url, $matches)) { 00172 $url = $matches[2] . $matches[3]; 00173 return $matches[1]; 00174 } 00175 return ''; 00176 } 00177 00184 function _parseRequest($raw) { 00185 $this->_raw = $raw; 00186 $request = new SimpleGetEncoding(); 00187 foreach (explode("&", $raw) as $pair) { 00188 if (preg_match('/(.*?)=(.*)/', $pair, $matches)) { 00189 $request->add($matches[1], urldecode($matches[2])); 00190 } elseif ($pair) { 00191 $request->add($pair, ''); 00192 } 00193 } 00194 return $request; 00195 } 00196 00203 function getScheme($default = false) { 00204 return $this->_scheme ? $this->_scheme : $default; 00205 } 00206 00212 function getUsername() { 00213 return $this->_username; 00214 } 00215 00221 function getPassword() { 00222 return $this->_password; 00223 } 00224 00231 function getHost($default = false) { 00232 return $this->_host ? $this->_host : $default; 00233 } 00234 00240 function getTld() { 00241 $path_parts = pathinfo($this->getHost()); 00242 return (isset($path_parts['extension']) ? $path_parts['extension'] : false); 00243 } 00244 00250 function getPort() { 00251 return $this->_port; 00252 } 00253 00259 function getPath() { 00260 if (! $this->_path && $this->_host) { 00261 return '/'; 00262 } 00263 return $this->_path; 00264 } 00265 00272 function getPage() { 00273 if (! preg_match('/([^\/]*?)$/', $this->getPath(), $matches)) { 00274 return false; 00275 } 00276 return $matches[1]; 00277 } 00278 00284 function getBasePath() { 00285 if (! preg_match('/(.*\/)[^\/]*?$/', $this->getPath(), $matches)) { 00286 return false; 00287 } 00288 return $matches[1]; 00289 } 00290 00296 function getFragment() { 00297 return $this->_fragment; 00298 } 00299 00307 function setCoordinates($x = false, $y = false) { 00308 if (($x === false) || ($y === false)) { 00309 $this->_x = $this->_y = false; 00310 return; 00311 } 00312 $this->_x = (integer)$x; 00313 $this->_y = (integer)$y; 00314 } 00315 00321 function getX() { 00322 return $this->_x; 00323 } 00324 00330 function getY() { 00331 return $this->_y; 00332 } 00333 00342 function getEncodedRequest() { 00343 if ($this->_raw) { 00344 $encoded = $this->_raw; 00345 } else { 00346 $encoded = $this->_request->asUrlRequest(); 00347 } 00348 if ($encoded) { 00349 return '?' . preg_replace('/^\?/', '', $encoded); 00350 } 00351 return ''; 00352 } 00353 00360 function addRequestParameter($key, $value) { 00361 $this->_raw = false; 00362 $this->_request->add($key, $value); 00363 } 00364 00371 function addRequestParameters($parameters) { 00372 $this->_raw = false; 00373 $this->_request->merge($parameters); 00374 } 00375 00380 function clearRequest() { 00381 $this->_raw = false; 00382 $this->_request = new SimpleGetEncoding(); 00383 } 00384 00392 function getTarget() { 00393 return $this->_target; 00394 } 00395 00401 function setTarget($frame) { 00402 $this->_raw = false; 00403 $this->_target = $frame; 00404 } 00405 00411 function asString() { 00412 $path = $this->_path; 00413 $scheme = $identity = $host = $encoded = $fragment = ''; 00414 if ($this->_username && $this->_password) { 00415 $identity = $this->_username . ':' . $this->_password . '@'; 00416 } 00417 if ($this->getHost()) { 00418 $scheme = $this->getScheme() ? $this->getScheme() : 'http'; 00419 $scheme .= "://"; 00420 $host = $this->getHost(); 00421 } 00422 if (substr($this->_path, 0, 1) == '/') { 00423 $path = $this->normalisePath($this->_path); 00424 } 00425 $encoded = $this->getEncodedRequest(); 00426 $fragment = $this->getFragment() ? '#'. $this->getFragment() : ''; 00427 $coords = $this->getX() === false ? '' : '?' . $this->getX() . ',' . $this->getY(); 00428 return "$scheme$identity$host$path$encoded$fragment$coords"; 00429 } 00430 00438 function makeAbsolute($base) { 00439 if (! is_object($base)) { 00440 $base = new SimpleUrl($base); 00441 } 00442 if ($this->getHost()) { 00443 $scheme = $this->getScheme(); 00444 $host = $this->getHost(); 00445 $port = $this->getPort() ? ':' . $this->getPort() : ''; 00446 $identity = $this->getIdentity() ? $this->getIdentity() . '@' : ''; 00447 if (! $identity) { 00448 $identity = $base->getIdentity() ? $base->getIdentity() . '@' : ''; 00449 } 00450 } else { 00451 $scheme = $base->getScheme(); 00452 $host = $base->getHost(); 00453 $port = $base->getPort() ? ':' . $base->getPort() : ''; 00454 $identity = $base->getIdentity() ? $base->getIdentity() . '@' : ''; 00455 } 00456 $path = $this->normalisePath($this->_extractAbsolutePath($base)); 00457 $encoded = $this->getEncodedRequest(); 00458 $fragment = $this->getFragment() ? '#'. $this->getFragment() : ''; 00459 $coords = $this->getX() === false ? '' : '?' . $this->getX() . ',' . $this->getY(); 00460 return new SimpleUrl("$scheme://$identity$host$port$path$encoded$fragment$coords"); 00461 } 00462 00470 function _extractAbsolutePath($base) { 00471 if ($this->getHost()) { 00472 return $this->_path; 00473 } 00474 if (! $this->_isRelativePath($this->_path)) { 00475 return $this->_path; 00476 } 00477 if ($this->_path) { 00478 return $base->getBasePath() . $this->_path; 00479 } 00480 return $base->getPath(); 00481 } 00482 00489 function _isRelativePath($path) { 00490 return (substr($path, 0, 1) != '/'); 00491 } 00492 00499 function getIdentity() { 00500 if ($this->_username && $this->_password) { 00501 return $this->_username . ':' . $this->_password; 00502 } 00503 return false; 00504 } 00505 00512 function normalisePath($path) { 00513 $path = preg_replace('|/\./|', '/', $path); 00514 return preg_replace('|/[^/]+/\.\./|', '/', $path); 00515 } 00516 00524 function getAllTopLevelDomains() { 00525 return 'com|edu|net|org|gov|mil|int|biz|info|name|pro|aero|coop|museum'; 00526 } 00527 } 00528 ?>