10 defined(
'JPATH_PLATFORM') or die;
29 protected $uri = null;
35 protected $scheme = null;
41 protected $host = null;
47 protected $port = null;
53 protected $user = null;
59 protected $pass = null;
65 protected $path = null;
71 protected $query = null;
77 protected $fragment = null;
83 protected $vars = array();
89 protected static $instances = array();
95 protected static $base = array();
101 protected static $root = array();
117 public function __construct($uri = null)
132 public function __toString()
134 return $this->toString();
147 public static function getInstance($uri =
'SERVER')
149 if (empty(self::$instances[$uri]))
152 if ($uri ==
'SERVER')
155 if (isset($_SERVER[
'HTTPS']) && !empty($_SERVER[
'HTTPS']) && (strtolower($_SERVER[
'HTTPS']) !=
'off'))
170 if (!empty($_SERVER[
'PHP_SELF']) && !empty($_SERVER[
'REQUEST_URI']))
174 $theURI =
'http' . $https . $_SERVER[
'HTTP_HOST'] . $_SERVER[
'REQUEST_URI'];
185 $theURI =
'http' . $https . $_SERVER[
'HTTP_HOST'] . $_SERVER[
'SCRIPT_NAME'];
188 if (isset($_SERVER[
'QUERY_STRING']) && !empty($_SERVER[
'QUERY_STRING']))
190 $theURI .=
'?' . $_SERVER[
'QUERY_STRING'];
195 $theURI = str_replace(array(
"'",
'"',
'<',
'>'), array(
"%27",
"%22",
"%3C",
"%3E"), $theURI);
203 self::$instances[$uri] =
new JUri($theURI);
206 return self::$instances[$uri];
218 public static function base($pathonly =
false)
221 if (empty(self::$base))
224 $live_site = $config->get(
'live_site');
226 if (trim($live_site) !=
'')
228 $uri = self::getInstance($live_site);
229 self::$base[
'prefix'] = $uri->toString(array(
'scheme',
'host',
'port'));
230 self::$base[
'path'] = rtrim($uri->toString(array(
'path')),
'/\\');
232 if (defined(
'JPATH_BASE') && defined(
'JPATH_ADMINISTRATOR'))
234 if (JPATH_BASE == JPATH_ADMINISTRATOR)
236 self::$base[
'path'] .=
'/administrator';
242 $uri = self::getInstance();
243 self::$base[
'prefix'] = $uri->toString(array(
'scheme',
'host',
'port'));
245 if (strpos(php_sapi_name(),
'cgi') !==
false && !ini_get(
'cgi.fix_pathinfo') && !empty($_SERVER[
'REQUEST_URI']))
251 $script_name = $_SERVER[
'PHP_SELF'];
256 $script_name = $_SERVER[
'SCRIPT_NAME'];
259 self::$base[
'path'] = rtrim(dirname($script_name),
'/\\');
263 return $pathonly ===
false ? self::$base[
'prefix'] . self::$base[
'path'] .
'/' : self::$base[
'path'];
276 public static function root($pathonly =
false, $path = null)
279 if (empty(self::$root))
281 $uri = self::getInstance(self::base());
282 self::$root[
'prefix'] = $uri->toString(array(
'scheme',
'host',
'port'));
283 self::$root[
'path'] = rtrim($uri->toString(array(
'path')),
'/\\');
289 self::$root[
'path'] = $path;
292 return $pathonly ===
false ? self::$root[
'prefix'] . self::$root[
'path'] .
'/' : self::$root[
'path'];
302 public static function current()
305 if (empty(self::$current))
307 $uri = self::getInstance();
308 self::$current = $uri->toString(array(
'scheme',
'host',
'port',
'path'));
311 return self::$current;
321 public static function reset()
323 self::$instances = array();
324 self::$base = array();
325 self::$root = array();
338 public function parse($uri)
349 $retval = ($parts) ?
true :
false;
352 if (isset($parts[
'query']) && strpos($parts[
'query'],
'&'))
354 $parts[
'query'] = str_replace(
'&',
'&', $parts[
'query']);
357 $this->scheme = isset($parts[
'scheme']) ? $parts[
'scheme'] : null;
358 $this->user = isset($parts[
'user']) ? $parts[
'user'] : null;
359 $this->pass = isset($parts[
'pass']) ? $parts[
'pass'] : null;
360 $this->host = isset($parts[
'host']) ? $parts[
'host'] : null;
361 $this->port = isset($parts[
'port']) ? $parts[
'port'] : null;
362 $this->path = isset($parts[
'path']) ? $parts[
'path'] : null;
363 $this->query = isset($parts[
'query']) ? $parts[
'query'] : null;
364 $this->fragment = isset($parts[
'fragment']) ? $parts[
'fragment'] : null;
367 if (isset($parts[
'query']))
369 parse_str($parts[
'query'], $this->vars);
384 public function toString(array $parts = array(
'scheme',
'user',
'pass',
'host',
'port',
'path',
'query',
'fragment'))
387 $query = $this->getQuery();
390 $uri .= in_array(
'scheme', $parts) ? (!empty($this->scheme) ? $this->scheme .
'://' :
'') :
'';
391 $uri .= in_array(
'user', $parts) ? $this->user :
'';
392 $uri .= in_array(
'pass', $parts) ? (!empty($this->pass) ?
':' :
'') . $this->pass . (!empty($this->user) ?
'@' :
'') :
'';
393 $uri .= in_array(
'host', $parts) ? $this->host :
'';
394 $uri .= in_array(
'port', $parts) ? (!empty($this->port) ?
':' :
'') . $this->port :
'';
395 $uri .= in_array(
'path', $parts) ? $this->path :
'';
396 $uri .= in_array(
'query', $parts) ? (!empty($query) ?
'?' . $query :
'') :
'';
397 $uri .= in_array(
'fragment', $parts) ? (!empty($this->fragment) ?
'#' . $this->fragment :
'') :
'';
413 public function setVar($name, $value)
415 $tmp = isset($this->vars[$name]) ? $this->vars[$name] : null;
417 $this->vars[$name] = $value;
434 public function hasVar($name)
436 return array_key_exists($name, $this->vars);
449 public function getVar($name, $default = null)
451 if (array_key_exists($name, $this->vars))
453 return $this->vars[$name];
468 public function delVar($name)
470 if (array_key_exists($name, $this->vars))
472 unset($this->vars[$name]);
489 public function setQuery($query)
491 if (is_array($query))
493 $this->vars = $query;
497 if (strpos($query,
'&') !==
false)
499 $query = str_replace(
'&',
'&', $query);
502 parse_str($query, $this->vars);
518 public function getQuery($toArray =
false)
526 if (is_null($this->query))
528 $this->query = self::buildQuery($this->vars);
544 public static function buildQuery(array $params)
546 if (count($params) == 0)
551 return urldecode(http_build_query($params,
'',
'&'));
562 public function getScheme()
564 return $this->scheme;
577 public function setScheme($scheme)
579 $this->scheme = $scheme;
590 public function getUser()
604 public function setUser($user)
617 public function getPass()
631 public function setPass($pass)
644 public function getHost()
658 public function setHost($host)
671 public function getPort()
673 return (isset($this->port)) ? $this->port : null;
685 public function setPort($port)
697 public function getPath()
711 public function setPath($path)
713 $this->path = $this->_cleanPath($path);
724 public function getFragment()
726 return $this->fragment;
739 public function setFragment($anchor)
741 $this->fragment = $anchor;
751 public function isSSL()
753 return $this->getScheme() ==
'https' ?
true :
false;
765 public static function isInternal($url)
767 $uri = self::getInstance($url);
768 $base = $uri->toString(array(
'scheme',
'host',
'port',
'path'));
769 $host = $uri->toString(array(
'scheme',
'host',
'port'));
771 if (stripos($base, self::base()) !== 0 && !empty($host))
793 protected function _cleanPath($path)
795 $path = explode(
'/', preg_replace(
'#(/+)#',
'/', $path));
797 for ($i = 0, $n = count($path); $i < $n; $i++)
799 if ($path[$i] ==
'.' || $path[$i] ==
'..')
801 if (($path[$i] ==
'.') || ($path[$i] ==
'..' && $i == 1 && $path[0] ==
''))
804 $path = array_values($path);
808 elseif ($path[$i] ==
'..' && ($i > 1 || ($i == 1 && $path[0] !=
'')))
811 unset($path[$i - 1]);
812 $path = array_values($path);
819 return implode(
'/', $path);