/libraries/vendor/joomla/uri/src/AbstractUri.php

https://gitlab.com/vitaliylukin91/idea-rating · PHP · 402 lines · 149 code · 42 blank · 211 comment · 27 complexity · b428b95c51043156b14b3c4802446820 MD5 · raw file

  1. <?php
  2. /**
  3. * Part of the Joomla Framework Uri Package
  4. *
  5. * @copyright Copyright (C) 2005 - 2015 Open Source Matters. All rights reserved.
  6. * @license GNU General Public License version 2 or later; see LICENSE
  7. */
  8. namespace Joomla\Uri;
  9. /**
  10. * Uri Class
  11. *
  12. * Abstract base for out uri classes.
  13. *
  14. * This class should be considered an implementation detail. Typehint against UriInterface.
  15. *
  16. * @since 1.0
  17. */
  18. abstract class AbstractUri implements UriInterface
  19. {
  20. /**
  21. * @var string Original URI
  22. * @since 1.0
  23. */
  24. protected $uri = null;
  25. /**
  26. * @var string Protocol
  27. * @since 1.0
  28. */
  29. protected $scheme = null;
  30. /**
  31. * @var string Host
  32. * @since 1.0
  33. */
  34. protected $host = null;
  35. /**
  36. * @var integer Port
  37. * @since 1.0
  38. */
  39. protected $port = null;
  40. /**
  41. * @var string Username
  42. * @since 1.0
  43. */
  44. protected $user = null;
  45. /**
  46. * @var string Password
  47. * @since 1.0
  48. */
  49. protected $pass = null;
  50. /**
  51. * @var string Path
  52. * @since 1.0
  53. */
  54. protected $path = null;
  55. /**
  56. * @var string Query
  57. * @since 1.0
  58. */
  59. protected $query = null;
  60. /**
  61. * @var string Anchor
  62. * @since 1.0
  63. */
  64. protected $fragment = null;
  65. /**
  66. * @var array Query variable hash
  67. * @since 1.0
  68. */
  69. protected $vars = array();
  70. /**
  71. * Constructor.
  72. * You can pass a URI string to the constructor to initialise a specific URI.
  73. *
  74. * @param string $uri The optional URI string
  75. *
  76. * @since 1.0
  77. */
  78. public function __construct($uri = null)
  79. {
  80. if (!is_null($uri))
  81. {
  82. $this->parse($uri);
  83. }
  84. }
  85. /**
  86. * Magic method to get the string representation of the URI object.
  87. *
  88. * @return string
  89. *
  90. * @since 1.0
  91. */
  92. public function __toString()
  93. {
  94. return $this->toString();
  95. }
  96. /**
  97. * Returns full uri string.
  98. *
  99. * @param array $parts An array specifying the parts to render.
  100. *
  101. * @return string The rendered URI string.
  102. *
  103. * @since 1.0
  104. */
  105. public function toString(array $parts = array('scheme', 'user', 'pass', 'host', 'port', 'path', 'query', 'fragment'))
  106. {
  107. // Make sure the query is created
  108. $query = $this->getQuery();
  109. $uri = '';
  110. $uri .= in_array('scheme', $parts) ? (!empty($this->scheme) ? $this->scheme . '://' : '') : '';
  111. $uri .= in_array('user', $parts) ? $this->user : '';
  112. $uri .= in_array('pass', $parts) ? (!empty($this->pass) ? ':' : '') . $this->pass . (!empty($this->user) ? '@' : '') : '';
  113. $uri .= in_array('host', $parts) ? $this->host : '';
  114. $uri .= in_array('port', $parts) ? (!empty($this->port) ? ':' : '') . $this->port : '';
  115. $uri .= in_array('path', $parts) ? $this->path : '';
  116. $uri .= in_array('query', $parts) ? (!empty($query) ? '?' . $query : '') : '';
  117. $uri .= in_array('fragment', $parts) ? (!empty($this->fragment) ? '#' . $this->fragment : '') : '';
  118. return $uri;
  119. }
  120. /**
  121. * Checks if variable exists.
  122. *
  123. * @param string $name Name of the query variable to check.
  124. *
  125. * @return boolean True if the variable exists.
  126. *
  127. * @since 1.0
  128. */
  129. public function hasVar($name)
  130. {
  131. return array_key_exists($name, $this->vars);
  132. }
  133. /**
  134. * Returns a query variable by name.
  135. *
  136. * @param string $name Name of the query variable to get.
  137. * @param string $default Default value to return if the variable is not set.
  138. *
  139. * @return array Query variables.
  140. *
  141. * @since 1.0
  142. */
  143. public function getVar($name, $default = null)
  144. {
  145. if (array_key_exists($name, $this->vars))
  146. {
  147. return $this->vars[$name];
  148. }
  149. return $default;
  150. }
  151. /**
  152. * Returns flat query string.
  153. *
  154. * @param boolean $toArray True to return the query as a key => value pair array.
  155. *
  156. * @return string Query string.
  157. *
  158. * @since 1.0
  159. */
  160. public function getQuery($toArray = false)
  161. {
  162. if ($toArray)
  163. {
  164. return $this->vars;
  165. }
  166. // If the query is empty build it first
  167. if (is_null($this->query))
  168. {
  169. $this->query = self::buildQuery($this->vars);
  170. }
  171. return $this->query;
  172. }
  173. /**
  174. * Get URI scheme (protocol)
  175. * ie. http, https, ftp, etc...
  176. *
  177. * @return string The URI scheme.
  178. *
  179. * @since 1.0
  180. */
  181. public function getScheme()
  182. {
  183. return $this->scheme;
  184. }
  185. /**
  186. * Get URI username
  187. * Returns the username, or null if no username was specified.
  188. *
  189. * @return string The URI username.
  190. *
  191. * @since 1.0
  192. */
  193. public function getUser()
  194. {
  195. return $this->user;
  196. }
  197. /**
  198. * Get URI password
  199. * Returns the password, or null if no password was specified.
  200. *
  201. * @return string The URI password.
  202. *
  203. * @since 1.0
  204. */
  205. public function getPass()
  206. {
  207. return $this->pass;
  208. }
  209. /**
  210. * Get URI host
  211. * Returns the hostname/ip or null if no hostname/ip was specified.
  212. *
  213. * @return string The URI host.
  214. *
  215. * @since 1.0
  216. */
  217. public function getHost()
  218. {
  219. return $this->host;
  220. }
  221. /**
  222. * Get URI port
  223. * Returns the port number, or null if no port was specified.
  224. *
  225. * @return integer The URI port number.
  226. *
  227. * @since 1.0
  228. */
  229. public function getPort()
  230. {
  231. return (isset($this->port)) ? $this->port : null;
  232. }
  233. /**
  234. * Gets the URI path string.
  235. *
  236. * @return string The URI path string.
  237. *
  238. * @since 1.0
  239. */
  240. public function getPath()
  241. {
  242. return $this->path;
  243. }
  244. /**
  245. * Get the URI archor string
  246. * Everything after the "#".
  247. *
  248. * @return string The URI anchor string.
  249. *
  250. * @since 1.0
  251. */
  252. public function getFragment()
  253. {
  254. return $this->fragment;
  255. }
  256. /**
  257. * Checks whether the current URI is using HTTPS.
  258. *
  259. * @return boolean True if using SSL via HTTPS.
  260. *
  261. * @since 1.0
  262. */
  263. public function isSSL()
  264. {
  265. return $this->getScheme() == 'https' ? true : false;
  266. }
  267. /**
  268. * Build a query from a array (reverse of the PHP parse_str()).
  269. *
  270. * @param array $params The array of key => value pairs to return as a query string.
  271. *
  272. * @return string The resulting query string.
  273. *
  274. * @see parse_str()
  275. * @since 1.0
  276. */
  277. protected static function buildQuery(array $params)
  278. {
  279. return urldecode(http_build_query($params, '', '&'));
  280. }
  281. /**
  282. * Parse a given URI and populate the class fields.
  283. *
  284. * @param string $uri The URI string to parse.
  285. *
  286. * @return boolean True on success.
  287. *
  288. * @since 1.0
  289. */
  290. protected function parse($uri)
  291. {
  292. // Set the original URI to fall back on
  293. $this->uri = $uri;
  294. /*
  295. * Parse the URI and populate the object fields. If URI is parsed properly,
  296. * set method return value to true.
  297. */
  298. $parts = UriHelper::parse_url($uri);
  299. $retval = ($parts) ? true : false;
  300. // We need to replace &amp; with & for parse_str to work right...
  301. if (isset($parts['query']) && strpos($parts['query'], '&amp;'))
  302. {
  303. $parts['query'] = str_replace('&amp;', '&', $parts['query']);
  304. }
  305. $this->scheme = isset($parts['scheme']) ? $parts['scheme'] : null;
  306. $this->user = isset($parts['user']) ? $parts['user'] : null;
  307. $this->pass = isset($parts['pass']) ? $parts['pass'] : null;
  308. $this->host = isset($parts['host']) ? $parts['host'] : null;
  309. $this->port = isset($parts['port']) ? $parts['port'] : null;
  310. $this->path = isset($parts['path']) ? $parts['path'] : null;
  311. $this->query = isset($parts['query']) ? $parts['query'] : null;
  312. $this->fragment = isset($parts['fragment']) ? $parts['fragment'] : null;
  313. // Parse the query
  314. if (isset($parts['query']))
  315. {
  316. parse_str($parts['query'], $this->vars);
  317. }
  318. return $retval;
  319. }
  320. /**
  321. * Resolves //, ../ and ./ from a path and returns
  322. * the result. Eg:
  323. *
  324. * /foo/bar/../boo.php => /foo/boo.php
  325. * /foo/bar/../../boo.php => /boo.php
  326. * /foo/bar/.././/boo.php => /foo/boo.php
  327. *
  328. * @param string $path The URI path to clean.
  329. *
  330. * @return string Cleaned and resolved URI path.
  331. *
  332. * @since 1.0
  333. */
  334. protected function cleanPath($path)
  335. {
  336. $path = explode('/', preg_replace('#(/+)#', '/', $path));
  337. for ($i = 0, $n = count($path); $i < $n; $i++)
  338. {
  339. if ($path[$i] == '.' || $path[$i] == '..')
  340. {
  341. if (($path[$i] == '.') || ($path[$i] == '..' && $i == 1 && $path[0] == ''))
  342. {
  343. unset($path[$i]);
  344. $path = array_values($path);
  345. $i--;
  346. $n--;
  347. }
  348. elseif ($path[$i] == '..' && ($i > 1 || ($i == 1 && $path[0] != '')))
  349. {
  350. unset($path[$i]);
  351. unset($path[$i - 1]);
  352. $path = array_values($path);
  353. $i -= 2;
  354. $n -= 2;
  355. }
  356. }
  357. }
  358. return implode('/', $path);
  359. }
  360. }