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

/wp-content/plugins/static-html-output-plugin/library/Github/php-http/message/src/Cookie.php

https://github.com/lostechies/wordpress
PHP | 526 lines | 216 code | 58 blank | 252 comment | 13 complexity | 2f12f85c45c5a8b3fa288ddbed969194 MD5 | raw file
  1. <?php
  2. namespace Http\Message;
  3. /**
  4. * Cookie Value Object.
  5. *
  6. * @author Márk Sági-Kazár <mark.sagikazar@gmail.com>
  7. *
  8. * @see http://tools.ietf.org/search/rfc6265
  9. */
  10. final class Cookie
  11. {
  12. /**
  13. * @var string
  14. */
  15. private $name;
  16. /**
  17. * @var string|null
  18. */
  19. private $value;
  20. /**
  21. * @var int|null
  22. */
  23. private $maxAge;
  24. /**
  25. * @var string|null
  26. */
  27. private $domain;
  28. /**
  29. * @var string
  30. */
  31. private $path;
  32. /**
  33. * @var bool
  34. */
  35. private $secure;
  36. /**
  37. * @var bool
  38. */
  39. private $httpOnly;
  40. /**
  41. * Expires attribute is HTTP 1.0 only and should be avoided.
  42. *
  43. * @var \DateTime|null
  44. */
  45. private $expires;
  46. /**
  47. * @param string $name
  48. * @param string|null $value
  49. * @param int $maxAge
  50. * @param string|null $domain
  51. * @param string|null $path
  52. * @param bool $secure
  53. * @param bool $httpOnly
  54. * @param \DateTime|null $expires Expires attribute is HTTP 1.0 only and should be avoided.
  55. *
  56. * @throws \InvalidArgumentException If name, value or max age is not valid.
  57. */
  58. public function __construct(
  59. $name,
  60. $value = null,
  61. $maxAge = null,
  62. $domain = null,
  63. $path = null,
  64. $secure = false,
  65. $httpOnly = false,
  66. \DateTime $expires = null
  67. ) {
  68. $this->validateName($name);
  69. $this->validateValue($value);
  70. $this->validateMaxAge($maxAge);
  71. $this->name = $name;
  72. $this->value = $value;
  73. $this->maxAge = $maxAge;
  74. $this->expires = $expires;
  75. $this->domain = $this->normalizeDomain($domain);
  76. $this->path = $this->normalizePath($path);
  77. $this->secure = (bool) $secure;
  78. $this->httpOnly = (bool) $httpOnly;
  79. }
  80. /**
  81. * Creates a new cookie without any attribute validation.
  82. *
  83. * @param string $name
  84. * @param string|null $value
  85. * @param int $maxAge
  86. * @param string|null $domain
  87. * @param string|null $path
  88. * @param bool $secure
  89. * @param bool $httpOnly
  90. * @param \DateTime|null $expires Expires attribute is HTTP 1.0 only and should be avoided.
  91. */
  92. public static function createWithoutValidation(
  93. $name,
  94. $value = null,
  95. $maxAge = null,
  96. $domain = null,
  97. $path = null,
  98. $secure = false,
  99. $httpOnly = false,
  100. \DateTime $expires = null
  101. ) {
  102. $cookie = new self('name', null, null, $domain, $path, $secure, $httpOnly, $expires);
  103. $cookie->name = $name;
  104. $cookie->value = $value;
  105. $cookie->maxAge = $maxAge;
  106. return $cookie;
  107. }
  108. /**
  109. * Returns the name.
  110. *
  111. * @return string
  112. */
  113. public function getName()
  114. {
  115. return $this->name;
  116. }
  117. /**
  118. * Returns the value.
  119. *
  120. * @return string|null
  121. */
  122. public function getValue()
  123. {
  124. return $this->value;
  125. }
  126. /**
  127. * Checks if there is a value.
  128. *
  129. * @return bool
  130. */
  131. public function hasValue()
  132. {
  133. return isset($this->value);
  134. }
  135. /**
  136. * Sets the value.
  137. *
  138. * @param string|null $value
  139. *
  140. * @return Cookie
  141. */
  142. public function withValue($value)
  143. {
  144. $this->validateValue($value);
  145. $new = clone $this;
  146. $new->value = $value;
  147. return $new;
  148. }
  149. /**
  150. * Returns the max age.
  151. *
  152. * @return int|null
  153. */
  154. public function getMaxAge()
  155. {
  156. return $this->maxAge;
  157. }
  158. /**
  159. * Checks if there is a max age.
  160. *
  161. * @return bool
  162. */
  163. public function hasMaxAge()
  164. {
  165. return isset($this->maxAge);
  166. }
  167. /**
  168. * Sets the max age.
  169. *
  170. * @param int|null $maxAge
  171. *
  172. * @return Cookie
  173. */
  174. public function withMaxAge($maxAge)
  175. {
  176. $this->validateMaxAge($maxAge);
  177. $new = clone $this;
  178. $new->maxAge = $maxAge;
  179. return $new;
  180. }
  181. /**
  182. * Returns the expiration time.
  183. *
  184. * @return \DateTime|null
  185. */
  186. public function getExpires()
  187. {
  188. return $this->expires;
  189. }
  190. /**
  191. * Checks if there is an expiration time.
  192. *
  193. * @return bool
  194. */
  195. public function hasExpires()
  196. {
  197. return isset($this->expires);
  198. }
  199. /**
  200. * Sets the expires.
  201. *
  202. * @param \DateTime|null $expires
  203. *
  204. * @return Cookie
  205. */
  206. public function withExpires(\DateTime $expires = null)
  207. {
  208. $new = clone $this;
  209. $new->expires = $expires;
  210. return $new;
  211. }
  212. /**
  213. * Checks if the cookie is expired.
  214. *
  215. * @return bool
  216. */
  217. public function isExpired()
  218. {
  219. return isset($this->expires) and $this->expires < new \DateTime();
  220. }
  221. /**
  222. * Returns the domain.
  223. *
  224. * @return string|null
  225. */
  226. public function getDomain()
  227. {
  228. return $this->domain;
  229. }
  230. /**
  231. * Checks if there is a domain.
  232. *
  233. * @return bool
  234. */
  235. public function hasDomain()
  236. {
  237. return isset($this->domain);
  238. }
  239. /**
  240. * Sets the domain.
  241. *
  242. * @param string|null $domain
  243. *
  244. * @return Cookie
  245. */
  246. public function withDomain($domain)
  247. {
  248. $new = clone $this;
  249. $new->domain = $this->normalizeDomain($domain);
  250. return $new;
  251. }
  252. /**
  253. * Checks whether this cookie is meant for this domain.
  254. *
  255. * @see http://tools.ietf.org/html/rfc6265#section-5.1.3
  256. *
  257. * @param string $domain
  258. *
  259. * @return bool
  260. */
  261. public function matchDomain($domain)
  262. {
  263. // Domain is not set or exact match
  264. if (!$this->hasDomain() || strcasecmp($domain, $this->domain) === 0) {
  265. return true;
  266. }
  267. // Domain is not an IP address
  268. if (filter_var($domain, FILTER_VALIDATE_IP)) {
  269. return false;
  270. }
  271. return (bool) preg_match(sprintf('/\b%s$/i', preg_quote($this->domain)), $domain);
  272. }
  273. /**
  274. * Returns the path.
  275. *
  276. * @return string
  277. */
  278. public function getPath()
  279. {
  280. return $this->path;
  281. }
  282. /**
  283. * Sets the path.
  284. *
  285. * @param string|null $path
  286. *
  287. * @return Cookie
  288. */
  289. public function withPath($path)
  290. {
  291. $new = clone $this;
  292. $new->path = $this->normalizePath($path);
  293. return $new;
  294. }
  295. /**
  296. * Checks whether this cookie is meant for this path.
  297. *
  298. * @see http://tools.ietf.org/html/rfc6265#section-5.1.4
  299. *
  300. * @param string $path
  301. *
  302. * @return bool
  303. */
  304. public function matchPath($path)
  305. {
  306. return $this->path === $path || (strpos($path, rtrim($this->path, '/').'/') === 0);
  307. }
  308. /**
  309. * Checks whether this cookie may only be sent over HTTPS.
  310. *
  311. * @return bool
  312. */
  313. public function isSecure()
  314. {
  315. return $this->secure;
  316. }
  317. /**
  318. * Sets whether this cookie should only be sent over HTTPS.
  319. *
  320. * @param bool $secure
  321. *
  322. * @return Cookie
  323. */
  324. public function withSecure($secure)
  325. {
  326. $new = clone $this;
  327. $new->secure = (bool) $secure;
  328. return $new;
  329. }
  330. /**
  331. * Check whether this cookie may not be accessed through Javascript.
  332. *
  333. * @return bool
  334. */
  335. public function isHttpOnly()
  336. {
  337. return $this->httpOnly;
  338. }
  339. /**
  340. * Sets whether this cookie may not be accessed through Javascript.
  341. *
  342. * @param bool $httpOnly
  343. *
  344. * @return Cookie
  345. */
  346. public function withHttpOnly($httpOnly)
  347. {
  348. $new = clone $this;
  349. $new->httpOnly = (bool) $httpOnly;
  350. return $new;
  351. }
  352. /**
  353. * Checks if this cookie represents the same cookie as $cookie.
  354. *
  355. * This does not compare the values, only name, domain and path.
  356. *
  357. * @param Cookie $cookie
  358. *
  359. * @return bool
  360. */
  361. public function match(Cookie $cookie)
  362. {
  363. return $this->name === $cookie->name && $this->domain === $cookie->domain and $this->path === $cookie->path;
  364. }
  365. /**
  366. * Validates cookie attributes.
  367. *
  368. * @return bool
  369. */
  370. public function isValid()
  371. {
  372. try {
  373. $this->validateName($this->name);
  374. $this->validateValue($this->value);
  375. $this->validateMaxAge($this->maxAge);
  376. } catch (\InvalidArgumentException $e) {
  377. return false;
  378. }
  379. return true;
  380. }
  381. /**
  382. * Validates the name attribute.
  383. *
  384. * @see http://tools.ietf.org/search/rfc2616#section-2.2
  385. *
  386. * @param string $name
  387. *
  388. * @throws \InvalidArgumentException If the name is empty or contains invalid characters.
  389. */
  390. private function validateName($name)
  391. {
  392. if (strlen($name) < 1) {
  393. throw new \InvalidArgumentException('The name cannot be empty');
  394. }
  395. // Name attribute is a token as per spec in RFC 2616
  396. if (preg_match('/[\x00-\x20\x22\x28-\x29\x2C\x2F\x3A-\x40\x5B-\x5D\x7B\x7D\x7F]/', $name)) {
  397. throw new \InvalidArgumentException(sprintf('The cookie name "%s" contains invalid characters.', $name));
  398. }
  399. }
  400. /**
  401. * Validates a value.
  402. *
  403. * @see http://tools.ietf.org/html/rfc6265#section-4.1.1
  404. *
  405. * @param string|null $value
  406. *
  407. * @throws \InvalidArgumentException If the value contains invalid characters.
  408. */
  409. private function validateValue($value)
  410. {
  411. if (isset($value)) {
  412. if (preg_match('/[^\x21\x23-\x2B\x2D-\x3A\x3C-\x5B\x5D-\x7E]/', $value)) {
  413. throw new \InvalidArgumentException(sprintf('The cookie value "%s" contains invalid characters.', $value));
  414. }
  415. }
  416. }
  417. /**
  418. * Validates a Max-Age attribute.
  419. *
  420. * @param int|null $maxAge
  421. *
  422. * @throws \InvalidArgumentException If the Max-Age is not an empty or integer value.
  423. */
  424. private function validateMaxAge($maxAge)
  425. {
  426. if (isset($maxAge)) {
  427. if (!is_int($maxAge)) {
  428. throw new \InvalidArgumentException('Max-Age must be integer');
  429. }
  430. }
  431. }
  432. /**
  433. * Remove the leading '.' and lowercase the domain as per spec in RFC 6265.
  434. *
  435. * @see http://tools.ietf.org/html/rfc6265#section-4.1.2.3
  436. * @see http://tools.ietf.org/html/rfc6265#section-5.1.3
  437. * @see http://tools.ietf.org/html/rfc6265#section-5.2.3
  438. *
  439. * @param string|null $domain
  440. *
  441. * @return string
  442. */
  443. private function normalizeDomain($domain)
  444. {
  445. if (isset($domain)) {
  446. $domain = ltrim(strtolower($domain), '.');
  447. }
  448. return $domain;
  449. }
  450. /**
  451. * Processes path as per spec in RFC 6265.
  452. *
  453. * @see http://tools.ietf.org/html/rfc6265#section-5.1.4
  454. * @see http://tools.ietf.org/html/rfc6265#section-5.2.4
  455. *
  456. * @param string|null $path
  457. *
  458. * @return string
  459. */
  460. private function normalizePath($path)
  461. {
  462. $path = rtrim($path, '/');
  463. if (empty($path) or substr($path, 0, 1) !== '/') {
  464. $path = '/';
  465. }
  466. return $path;
  467. }
  468. }