OAuthCommonHttpUriUri

Standards-compliant URI class.

Defined (1)

The class is defined in the following location(s).

/lib/PHPoAuthLib/src/OAuth/Common/Http/Uri/Uri.php  
  1. class Uri implements UriInterface 
  2. /** 
  3. * @var string 
  4. */ 
  5. private $scheme = 'http'; 
  6.  
  7. /** 
  8. * @var string 
  9. */ 
  10. private $userInfo = ''; 
  11.  
  12. /** 
  13. * @var string 
  14. */ 
  15. private $rawUserInfo = ''; 
  16.  
  17. /** 
  18. * @var string 
  19. */ 
  20. private $host; 
  21.  
  22. /** 
  23. * @var int 
  24. */ 
  25. private $port = 80; 
  26.  
  27. /** 
  28. * @var string 
  29. */ 
  30. private $path = '/'; 
  31.  
  32. /** 
  33. * @var string 
  34. */ 
  35. private $query = ''; 
  36.  
  37. /** 
  38. * @var string 
  39. */ 
  40. private $fragment = ''; 
  41.  
  42. /** 
  43. * @var bool 
  44. */ 
  45. private $explicitPortSpecified = false; 
  46.  
  47. /** 
  48. * @var bool 
  49. */ 
  50. private $explicitTrailingHostSlash = false; 
  51.  
  52. /** 
  53. * @param string $uri 
  54. */ 
  55. public function __construct($uri = null) 
  56. if (null !== $uri) { 
  57. $this->parseUri($uri); 
  58.  
  59. /** 
  60. * @param string $uri 
  61. * @throws \InvalidArgumentException 
  62. */ 
  63. protected function parseUri($uri) 
  64. if (false === ($uriParts = parse_url($uri))) { 
  65. // congratulations if you've managed to get parse_url to fail,  
  66. // it seems to always return some semblance of a parsed url no matter what 
  67. throw new InvalidArgumentException("Invalid URI: $uri"); 
  68.  
  69. if (!isset($uriParts['scheme'])) { 
  70. throw new InvalidArgumentException('Invalid URI: http|https scheme required'); 
  71.  
  72. $this->scheme = $uriParts['scheme']; 
  73. $this->host = $uriParts['host']; 
  74.  
  75. if (isset($uriParts['port'])) { 
  76. $this->port = $uriParts['port']; 
  77. $this->explicitPortSpecified = true; 
  78. } else { 
  79. $this->port = strcmp('https', $uriParts['scheme']) ? 80 : 443; 
  80. $this->explicitPortSpecified = false; 
  81.  
  82. if (isset($uriParts['path'])) { 
  83. $this->path = $uriParts['path']; 
  84. if ('/' === $uriParts['path']) { 
  85. $this->explicitTrailingHostSlash = true; 
  86. } else { 
  87. $this->path = '/'; 
  88.  
  89. $this->query = isset($uriParts['query']) ? $uriParts['query'] : ''; 
  90. $this->fragment = isset($uriParts['fragment']) ? $uriParts['fragment'] : ''; 
  91.  
  92. $userInfo = ''; 
  93. if (!empty($uriParts['user'])) { 
  94. $userInfo .= $uriParts['user']; 
  95. if ($userInfo && !empty($uriParts['pass'])) { 
  96. $userInfo .= ':' . $uriParts['pass']; 
  97.  
  98. $this->setUserInfo($userInfo); 
  99.  
  100. /** 
  101. * @param string $rawUserInfo 
  102. * @return string 
  103. */ 
  104. protected function protectUserInfo($rawUserInfo) 
  105. $colonPos = strpos($rawUserInfo, ':'); 
  106.  
  107. // rfc3986-3.2.1 | http://tools.ietf.org/html/rfc3986#section-3.2 
  108. // "Applications should not render as clear text any data 
  109. // after the first colon (":") character found within a userinfo 
  110. // subcomponent unless the data after the colon is the empty string 
  111. // (indicating no password)" 
  112. if ($colonPos !== false && strlen($rawUserInfo)-1 > $colonPos) { 
  113. return substr($rawUserInfo, 0, $colonPos) . ':********'; 
  114. } else { 
  115. return $rawUserInfo; 
  116.  
  117. /** 
  118. * @return string 
  119. */ 
  120. public function getScheme() 
  121. return $this->scheme; 
  122.  
  123. /** 
  124. * @return string 
  125. */ 
  126. public function getUserInfo() 
  127. return $this->userInfo; 
  128.  
  129. /** 
  130. * @return string 
  131. */ 
  132. public function getRawUserInfo() 
  133. return $this->rawUserInfo; 
  134.  
  135. /** 
  136. * @return string 
  137. */ 
  138. public function getHost() 
  139. return $this->host; 
  140.  
  141. /** 
  142. * @return int 
  143. */ 
  144. public function getPort() 
  145. return $this->port; 
  146.  
  147. /** 
  148. * @return string 
  149. */ 
  150. public function getPath() 
  151. return $this->path; 
  152.  
  153. /** 
  154. * @return string 
  155. */ 
  156. public function getQuery() 
  157. return $this->query; 
  158.  
  159. /** 
  160. * @return string 
  161. */ 
  162. public function getFragment() 
  163. return $this->fragment; 
  164.  
  165. /** 
  166. * Uses protected user info by default as per rfc3986-3.2.1 
  167. * Uri::getRawAuthority() is available if plain-text password information is desirable. 
  168. * @return string 
  169. */ 
  170. public function getAuthority() 
  171. $authority = $this->userInfo ? $this->userInfo.'@' : ''; 
  172. $authority .= $this->host; 
  173.  
  174. if ($this->explicitPortSpecified) { 
  175. $authority .= ":{$this->port}"; 
  176.  
  177. return $authority; 
  178.  
  179. /** 
  180. * @return string 
  181. */ 
  182. public function getRawAuthority() 
  183. $authority = $this->rawUserInfo ? $this->rawUserInfo.'@' : ''; 
  184. $authority .= $this->host; 
  185.  
  186. if ($this->explicitPortSpecified) { 
  187. $authority .= ":{$this->port}"; 
  188.  
  189. return $authority; 
  190.  
  191. /** 
  192. * @return string 
  193. */ 
  194. public function getAbsoluteUri() 
  195. $uri = $this->scheme . '://' . $this->getRawAuthority(); 
  196.  
  197. if ('/' === $this->path) { 
  198. $uri .= $this->explicitTrailingHostSlash ? '/' : ''; 
  199. } else { 
  200. $uri .= $this->path; 
  201.  
  202. if (!empty($this->query)) { 
  203. $uri .= "?{$this->query}"; 
  204.  
  205. if (!empty($this->fragment)) { 
  206. $uri .= "#{$this->fragment}"; 
  207.  
  208. return $uri; 
  209.  
  210. /** 
  211. * @return string 
  212. */ 
  213. public function getRelativeUri() 
  214. $uri = ''; 
  215.  
  216. if ('/' === $this->path) { 
  217. $uri .= $this->explicitTrailingHostSlash ? '/' : ''; 
  218. } else { 
  219. $uri .= $this->path; 
  220.  
  221. return $uri; 
  222.  
  223. /** 
  224. * Uses protected user info by default as per rfc3986-3.2.1 
  225. * Uri::getAbsoluteUri() is available if plain-text password information is desirable. 
  226. * @return string 
  227. */ 
  228. public function __toString() 
  229. $uri = $this->scheme . '://' . $this->getAuthority(); 
  230.  
  231. if ('/' === $this->path) { 
  232. $uri .= $this->explicitTrailingHostSlash ? '/' : ''; 
  233. } else { 
  234. $uri .= $this->path; 
  235.  
  236. if (!empty($this->query)) { 
  237. $uri .= "?{$this->query}"; 
  238.  
  239. if (!empty($this->fragment)) { 
  240. $uri .= "#{$this->fragment}"; 
  241.  
  242. return $uri; 
  243.  
  244. /** 
  245. * @param $path 
  246. */ 
  247. public function setPath($path) 
  248. if (empty($path)) { 
  249. $this->path = '/'; 
  250. $this->explicitTrailingHostSlash = false; 
  251. } else { 
  252. $this->path = $path; 
  253. if ('/' === $this->path) { 
  254. $this->explicitTrailingHostSlash = true; 
  255.  
  256. /** 
  257. * @param string $query 
  258. */ 
  259. public function setQuery($query) 
  260. $this->query = $query; 
  261.  
  262. /** 
  263. * @param string $var 
  264. * @param string $val 
  265. */ 
  266. public function addToQuery($var, $val) 
  267. if (strlen($this->query) > 0) { 
  268. $this->query .= '&'; 
  269. $this->query .= http_build_query(array($var => $val), '', '&'); 
  270.  
  271. /** 
  272. * @param string $fragment 
  273. */ 
  274. public function setFragment($fragment) 
  275. $this->fragment = $fragment; 
  276.  
  277. /** 
  278. * @param string $scheme 
  279. */ 
  280. public function setScheme($scheme) 
  281. $this->scheme = $scheme; 
  282.  
  283.  
  284. /** 
  285. * @param string $userInfo 
  286. */ 
  287. public function setUserInfo($userInfo) 
  288. $this->userInfo = $userInfo ? $this->protectUserInfo($userInfo) : ''; 
  289. $this->rawUserInfo = $userInfo; 
  290.  
  291.  
  292. /** 
  293. * @param int $port 
  294. */ 
  295. public function setPort($port) 
  296. $this->port = intval($port); 
  297.  
  298. if (('https' === $this->scheme && $this->port === 443) || ('http' === $this->scheme && $this->port === 80)) { 
  299. $this->explicitPortSpecified = false; 
  300. } else { 
  301. $this->explicitPortSpecified = true; 
  302.  
  303. /** 
  304. * @param string $host 
  305. */ 
  306. public function setHost($host) 
  307. $this->host = $host; 
  308.  
  309. /** 
  310. * @return bool 
  311. */ 
  312. public function hasExplicitTrailingHostSlash() 
  313. return $this->explicitTrailingHostSlash; 
  314.  
  315. /** 
  316. * @return bool 
  317. */ 
  318. public function hasExplicitPortSpecified() 
  319. return $this->explicitPortSpecified;