PageRenderTime 27ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/main/lib/web/uri.php

https://gitlab.com/alexprowars/bitrix
PHP | 343 lines | 197 code | 41 blank | 105 comment | 18 complexity | 0302f8314035aeb8e71507b42f37e8c8 MD5 | raw file
  1. <?php
  2. /**
  3. * Bitrix Framework
  4. * @package bitrix
  5. * @subpackage main
  6. * @copyright 2001-2014 Bitrix
  7. */
  8. namespace Bitrix\Main\Web;
  9. class Uri implements \JsonSerializable
  10. {
  11. protected $scheme;
  12. protected $host;
  13. protected $port;
  14. protected $user;
  15. protected $pass;
  16. protected $path;
  17. protected $query;
  18. protected $fragment;
  19. /**
  20. * @param string $url
  21. */
  22. public function __construct($url)
  23. {
  24. if(mb_strpos($url, "/") === 0)
  25. {
  26. //we don't support "current scheme" e.g. "//host/path"
  27. $url = "/".ltrim($url, "/");
  28. }
  29. $parsedUrl = parse_url($url);
  30. if($parsedUrl !== false)
  31. {
  32. $this->scheme = (isset($parsedUrl["scheme"])? mb_strtolower($parsedUrl["scheme"]) : "http");
  33. $this->host = (isset($parsedUrl["host"])? $parsedUrl["host"] : "");
  34. if(isset($parsedUrl["port"]))
  35. {
  36. $this->port = $parsedUrl["port"];
  37. }
  38. else
  39. {
  40. $this->port = ($this->scheme == "https"? 443 : 80);
  41. }
  42. $this->user = (isset($parsedUrl["user"])? $parsedUrl["user"] : "");
  43. $this->pass = (isset($parsedUrl["pass"])? $parsedUrl["pass"] : "");
  44. $this->path = (isset($parsedUrl["path"])? $parsedUrl["path"] : "/");
  45. $this->query = (isset($parsedUrl["query"])? $parsedUrl["query"] : "");
  46. $this->fragment = (isset($parsedUrl["fragment"])? $parsedUrl["fragment"] : "");
  47. }
  48. }
  49. /**
  50. * @deprecated Use getLocator() or getUri().
  51. */
  52. public function getUrl()
  53. {
  54. return $this->getLocator();
  55. }
  56. /**
  57. * Return the URI without a fragment.
  58. * @return string
  59. */
  60. public function getLocator()
  61. {
  62. $url = "";
  63. if($this->host <> '')
  64. {
  65. $url .= $this->scheme."://".$this->host;
  66. if(($this->scheme == "http" && $this->port <> 80) || ($this->scheme == "https" && $this->port <> 443))
  67. {
  68. $url .= ":".$this->port;
  69. }
  70. }
  71. $url .= $this->getPathQuery();
  72. return $url;
  73. }
  74. /**
  75. * Return the URI with a fragment, if any.
  76. * @return string
  77. */
  78. public function getUri()
  79. {
  80. $url = $this->getLocator();
  81. if($this->fragment <> '')
  82. {
  83. $url .= "#".$this->fragment;
  84. }
  85. return $url;
  86. }
  87. /**
  88. * Returns the fragment.
  89. * @return string
  90. */
  91. public function getFragment()
  92. {
  93. return $this->fragment;
  94. }
  95. /**
  96. * Returns the host.
  97. * @return string
  98. */
  99. public function getHost()
  100. {
  101. return $this->host;
  102. }
  103. /**
  104. * Sets the host
  105. * @param string $host Host name.
  106. * @return $this
  107. */
  108. public function setHost($host)
  109. {
  110. $this->host = $host;
  111. return $this;
  112. }
  113. /**
  114. * Returns the password.
  115. * @return string
  116. */
  117. public function getPass()
  118. {
  119. return $this->pass;
  120. }
  121. /**
  122. * Sets the password.
  123. * @param string $pass Password,
  124. * @return $this
  125. */
  126. public function setPass($pass)
  127. {
  128. $this->pass = $pass;
  129. return $this;
  130. }
  131. /**
  132. * Returns the path.
  133. * @return string
  134. */
  135. public function getPath()
  136. {
  137. return $this->path;
  138. }
  139. /**
  140. * Sets the path.
  141. * @param string $path
  142. * @return $this
  143. */
  144. public function setPath($path)
  145. {
  146. $this->path = $path;
  147. return $this;
  148. }
  149. /**
  150. * Returns the path with the query.
  151. * @return string
  152. */
  153. public function getPathQuery()
  154. {
  155. $pathQuery = $this->path;
  156. if($this->query <> "")
  157. {
  158. $pathQuery .= '?'.$this->query;
  159. }
  160. return $pathQuery;
  161. }
  162. /**
  163. * Returns the port number.
  164. * @return string
  165. */
  166. public function getPort()
  167. {
  168. return $this->port;
  169. }
  170. /**
  171. * Returns the query.
  172. * @return string
  173. */
  174. public function getQuery()
  175. {
  176. return $this->query;
  177. }
  178. /**
  179. * Returns the scheme.
  180. * @return string
  181. */
  182. public function getScheme()
  183. {
  184. return $this->scheme;
  185. }
  186. /**
  187. * Returns the user.
  188. * @return string
  189. */
  190. public function getUser()
  191. {
  192. return $this->user;
  193. }
  194. /**
  195. * Sets the user.
  196. * @param string $user User.
  197. * @return $this
  198. */
  199. public function setUser($user)
  200. {
  201. $this->user = $user;
  202. return $this;
  203. }
  204. /**
  205. * Extended parsing to allow dots and spaces in parameters names.
  206. * @param string $params
  207. * @return array
  208. */
  209. protected static function parseParams($params)
  210. {
  211. $data = preg_replace_callback(
  212. '/(?:^|(?<=&))[^=[]+/',
  213. function($match)
  214. {
  215. return bin2hex(urldecode($match[0]));
  216. },
  217. $params
  218. );
  219. parse_str($data, $values);
  220. return array_combine(array_map('hex2bin', array_keys($values)), $values);
  221. }
  222. /**
  223. * Deletes parameters from the query.
  224. * @param array $params Parameters to delete.
  225. * @param bool $preserveDots Special treatment of dots and spaces in the parameters names.
  226. * @return $this
  227. */
  228. public function deleteParams(array $params, $preserveDots = false)
  229. {
  230. if($this->query <> '')
  231. {
  232. if($preserveDots)
  233. {
  234. $currentParams = static::parseParams($this->query);
  235. }
  236. else
  237. {
  238. $currentParams = array();
  239. parse_str($this->query, $currentParams);
  240. }
  241. foreach($params as $param)
  242. {
  243. unset($currentParams[$param]);
  244. }
  245. $this->query = http_build_query($currentParams, "", "&");
  246. }
  247. return $this;
  248. }
  249. /**
  250. * Adds parameters to query or replaces existing ones.
  251. * @param array $params Parameters to add.
  252. * @param bool $preserveDots Special treatment of dots and spaces in the parameters names.
  253. * @return $this
  254. */
  255. public function addParams(array $params, $preserveDots = false)
  256. {
  257. $currentParams = array();
  258. if($this->query <> '')
  259. {
  260. if($preserveDots)
  261. {
  262. $currentParams = static::parseParams($this->query);
  263. }
  264. else
  265. {
  266. parse_str($this->query, $currentParams);
  267. }
  268. }
  269. $currentParams = array_replace($currentParams, $params);
  270. $this->query = http_build_query($currentParams, "", "&");
  271. return $this;
  272. }
  273. public function __toString()
  274. {
  275. return $this->getUri();
  276. }
  277. /**
  278. * Specify data which should be serialized to JSON
  279. * @link http://php.net/manual/en/jsonserializable.jsonserialize.php
  280. * @return mixed data which can be serialized by <b>json_encode</b>,
  281. * which is a value of any type other than a resource.
  282. * @since 5.4.0
  283. */
  284. public function jsonSerialize()
  285. {
  286. return $this->getUri();
  287. }
  288. /**
  289. * Converts the host to punycode.
  290. * @return string|\Bitrix\Main\Error
  291. */
  292. public function convertToPunycode()
  293. {
  294. $host = \CBXPunycode::ToASCII($this->getHost(), $encodingErrors);
  295. if(!empty($encodingErrors))
  296. {
  297. return new \Bitrix\Main\Error(implode("\n", $encodingErrors));
  298. }
  299. $this->setHost($host);
  300. return $host;
  301. }
  302. }