PageRenderTime 46ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/api/vendor/guzzle/guzzle/src/Guzzle/Plugin/Cookie/Cookie.php

https://gitlab.com/x33n/respond
PHP | 513 lines | 211 code | 53 blank | 249 comment | 18 complexity | 547a2a85d45e3d7537d73e5c92d52ac8 MD5 | raw file
  1. <?php
  2. namespace Guzzle\Plugin\Cookie;
  3. use Guzzle\Common\ToArrayInterface;
  4. /**
  5. * Set-Cookie object
  6. */
  7. class Cookie implements ToArrayInterface
  8. {
  9. /** @var array Cookie data */
  10. protected $data;
  11. /**
  12. * @var string ASCII codes not valid for for use in a cookie name
  13. *
  14. * Cookie names are defined as 'token', according to RFC 2616, Section 2.2
  15. * A valid token may contain any CHAR except CTLs (ASCII 0 - 31 or 127)
  16. * or any of the following separators
  17. */
  18. protected static $invalidCharString;
  19. /**
  20. * Gets an array of invalid cookie characters
  21. *
  22. * @return array
  23. */
  24. protected static function getInvalidCharacters()
  25. {
  26. if (!self::$invalidCharString) {
  27. self::$invalidCharString = implode('', array_map('chr', array_merge(
  28. range(0, 32),
  29. array(34, 40, 41, 44, 47),
  30. array(58, 59, 60, 61, 62, 63, 64, 91, 92, 93, 123, 125, 127)
  31. )));
  32. }
  33. return self::$invalidCharString;
  34. }
  35. /**
  36. * @param array $data Array of cookie data provided by a Cookie parser
  37. */
  38. public function __construct(array $data = array())
  39. {
  40. static $defaults = array(
  41. 'name' => '',
  42. 'value' => '',
  43. 'domain' => '',
  44. 'path' => '/',
  45. 'expires' => null,
  46. 'max_age' => 0,
  47. 'comment' => null,
  48. 'comment_url' => null,
  49. 'port' => array(),
  50. 'version' => null,
  51. 'secure' => false,
  52. 'discard' => false,
  53. 'http_only' => false
  54. );
  55. $this->data = array_merge($defaults, $data);
  56. // Extract the expires value and turn it into a UNIX timestamp if needed
  57. if (!$this->getExpires() && $this->getMaxAge()) {
  58. // Calculate the expires date
  59. $this->setExpires(time() + (int) $this->getMaxAge());
  60. } elseif ($this->getExpires() && !is_numeric($this->getExpires())) {
  61. $this->setExpires(strtotime($this->getExpires()));
  62. }
  63. }
  64. /**
  65. * Get the cookie as an array
  66. *
  67. * @return array
  68. */
  69. public function toArray()
  70. {
  71. return $this->data;
  72. }
  73. /**
  74. * Get the cookie name
  75. *
  76. * @return string
  77. */
  78. public function getName()
  79. {
  80. return $this->data['name'];
  81. }
  82. /**
  83. * Set the cookie name
  84. *
  85. * @param string $name Cookie name
  86. *
  87. * @return Cookie
  88. */
  89. public function setName($name)
  90. {
  91. return $this->setData('name', $name);
  92. }
  93. /**
  94. * Get the cookie value
  95. *
  96. * @return string
  97. */
  98. public function getValue()
  99. {
  100. return $this->data['value'];
  101. }
  102. /**
  103. * Set the cookie value
  104. *
  105. * @param string $value Cookie value
  106. *
  107. * @return Cookie
  108. */
  109. public function setValue($value)
  110. {
  111. return $this->setData('value', $value);
  112. }
  113. /**
  114. * Get the domain
  115. *
  116. * @return string|null
  117. */
  118. public function getDomain()
  119. {
  120. return $this->data['domain'];
  121. }
  122. /**
  123. * Set the domain of the cookie
  124. *
  125. * @param string $domain
  126. *
  127. * @return Cookie
  128. */
  129. public function setDomain($domain)
  130. {
  131. return $this->setData('domain', $domain);
  132. }
  133. /**
  134. * Get the path
  135. *
  136. * @return string
  137. */
  138. public function getPath()
  139. {
  140. return $this->data['path'];
  141. }
  142. /**
  143. * Set the path of the cookie
  144. *
  145. * @param string $path Path of the cookie
  146. *
  147. * @return Cookie
  148. */
  149. public function setPath($path)
  150. {
  151. return $this->setData('path', $path);
  152. }
  153. /**
  154. * Maximum lifetime of the cookie in seconds
  155. *
  156. * @return int|null
  157. */
  158. public function getMaxAge()
  159. {
  160. return $this->data['max_age'];
  161. }
  162. /**
  163. * Set the max-age of the cookie
  164. *
  165. * @param int $maxAge Max age of the cookie in seconds
  166. *
  167. * @return Cookie
  168. */
  169. public function setMaxAge($maxAge)
  170. {
  171. return $this->setData('max_age', $maxAge);
  172. }
  173. /**
  174. * The UNIX timestamp when the cookie expires
  175. *
  176. * @return mixed
  177. */
  178. public function getExpires()
  179. {
  180. return $this->data['expires'];
  181. }
  182. /**
  183. * Set the unix timestamp for which the cookie will expire
  184. *
  185. * @param int $timestamp Unix timestamp
  186. *
  187. * @return Cookie
  188. */
  189. public function setExpires($timestamp)
  190. {
  191. return $this->setData('expires', $timestamp);
  192. }
  193. /**
  194. * Version of the cookie specification. RFC 2965 is 1
  195. *
  196. * @return mixed
  197. */
  198. public function getVersion()
  199. {
  200. return $this->data['version'];
  201. }
  202. /**
  203. * Set the cookie version
  204. *
  205. * @param string|int $version Version to set
  206. *
  207. * @return Cookie
  208. */
  209. public function setVersion($version)
  210. {
  211. return $this->setData('version', $version);
  212. }
  213. /**
  214. * Get whether or not this is a secure cookie
  215. *
  216. * @return null|bool
  217. */
  218. public function getSecure()
  219. {
  220. return $this->data['secure'];
  221. }
  222. /**
  223. * Set whether or not the cookie is secure
  224. *
  225. * @param bool $secure Set to true or false if secure
  226. *
  227. * @return Cookie
  228. */
  229. public function setSecure($secure)
  230. {
  231. return $this->setData('secure', (bool) $secure);
  232. }
  233. /**
  234. * Get whether or not this is a session cookie
  235. *
  236. * @return null|bool
  237. */
  238. public function getDiscard()
  239. {
  240. return $this->data['discard'];
  241. }
  242. /**
  243. * Set whether or not this is a session cookie
  244. *
  245. * @param bool $discard Set to true or false if this is a session cookie
  246. *
  247. * @return Cookie
  248. */
  249. public function setDiscard($discard)
  250. {
  251. return $this->setData('discard', $discard);
  252. }
  253. /**
  254. * Get the comment
  255. *
  256. * @return string|null
  257. */
  258. public function getComment()
  259. {
  260. return $this->data['comment'];
  261. }
  262. /**
  263. * Set the comment of the cookie
  264. *
  265. * @param string $comment Cookie comment
  266. *
  267. * @return Cookie
  268. */
  269. public function setComment($comment)
  270. {
  271. return $this->setData('comment', $comment);
  272. }
  273. /**
  274. * Get the comment URL of the cookie
  275. *
  276. * @return string|null
  277. */
  278. public function getCommentUrl()
  279. {
  280. return $this->data['comment_url'];
  281. }
  282. /**
  283. * Set the comment URL of the cookie
  284. *
  285. * @param string $commentUrl Cookie comment URL for more information
  286. *
  287. * @return Cookie
  288. */
  289. public function setCommentUrl($commentUrl)
  290. {
  291. return $this->setData('comment_url', $commentUrl);
  292. }
  293. /**
  294. * Get an array of acceptable ports this cookie can be used with
  295. *
  296. * @return array
  297. */
  298. public function getPorts()
  299. {
  300. return $this->data['port'];
  301. }
  302. /**
  303. * Set a list of acceptable ports this cookie can be used with
  304. *
  305. * @param array $ports Array of acceptable ports
  306. *
  307. * @return Cookie
  308. */
  309. public function setPorts(array $ports)
  310. {
  311. return $this->setData('port', $ports);
  312. }
  313. /**
  314. * Get whether or not this is an HTTP only cookie
  315. *
  316. * @return bool
  317. */
  318. public function getHttpOnly()
  319. {
  320. return $this->data['http_only'];
  321. }
  322. /**
  323. * Set whether or not this is an HTTP only cookie
  324. *
  325. * @param bool $httpOnly Set to true or false if this is HTTP only
  326. *
  327. * @return Cookie
  328. */
  329. public function setHttpOnly($httpOnly)
  330. {
  331. return $this->setData('http_only', $httpOnly);
  332. }
  333. /**
  334. * Get an array of extra cookie data
  335. *
  336. * @return array
  337. */
  338. public function getAttributes()
  339. {
  340. return $this->data['data'];
  341. }
  342. /**
  343. * Get a specific data point from the extra cookie data
  344. *
  345. * @param string $name Name of the data point to retrieve
  346. *
  347. * @return null|string
  348. */
  349. public function getAttribute($name)
  350. {
  351. return array_key_exists($name, $this->data['data']) ? $this->data['data'][$name] : null;
  352. }
  353. /**
  354. * Set a cookie data attribute
  355. *
  356. * @param string $name Name of the attribute to set
  357. * @param string $value Value to set
  358. *
  359. * @return Cookie
  360. */
  361. public function setAttribute($name, $value)
  362. {
  363. $this->data['data'][$name] = $value;
  364. return $this;
  365. }
  366. /**
  367. * Check if the cookie matches a path value
  368. *
  369. * @param string $path Path to check against
  370. *
  371. * @return bool
  372. */
  373. public function matchesPath($path)
  374. {
  375. return !$this->getPath() || 0 === stripos($path, $this->getPath());
  376. }
  377. /**
  378. * Check if the cookie matches a domain value
  379. *
  380. * @param string $domain Domain to check against
  381. *
  382. * @return bool
  383. */
  384. public function matchesDomain($domain)
  385. {
  386. // Remove the leading '.' as per spec in RFC 6265: http://tools.ietf.org/html/rfc6265#section-5.2.3
  387. $cookieDomain = ltrim($this->getDomain(), '.');
  388. // Domain not set or exact match.
  389. if (!$cookieDomain || !strcasecmp($domain, $cookieDomain)) {
  390. return true;
  391. }
  392. // Matching the subdomain according to RFC 6265: http://tools.ietf.org/html/rfc6265#section-5.1.3
  393. if (filter_var($domain, FILTER_VALIDATE_IP)) {
  394. return false;
  395. }
  396. return (bool) preg_match('/\.' . preg_quote($cookieDomain) . '$/i', $domain);
  397. }
  398. /**
  399. * Check if the cookie is compatible with a specific port
  400. *
  401. * @param int $port Port to check
  402. *
  403. * @return bool
  404. */
  405. public function matchesPort($port)
  406. {
  407. return count($this->getPorts()) == 0 || in_array($port, $this->getPorts());
  408. }
  409. /**
  410. * Check if the cookie is expired
  411. *
  412. * @return bool
  413. */
  414. public function isExpired()
  415. {
  416. return $this->getExpires() && time() > $this->getExpires();
  417. }
  418. /**
  419. * Check if the cookie is valid according to RFC 6265
  420. *
  421. * @return bool|string Returns true if valid or an error message if invalid
  422. */
  423. public function validate()
  424. {
  425. // Names must not be empty, but can be 0
  426. $name = $this->getName();
  427. if (empty($name) && !is_numeric($name)) {
  428. return 'The cookie name must not be empty';
  429. }
  430. // Check if any of the invalid characters are present in the cookie name
  431. if (strpbrk($name, self::getInvalidCharacters()) !== false) {
  432. return 'The cookie name must not contain invalid characters: ' . $name;
  433. }
  434. // Value must not be empty, but can be 0
  435. $value = $this->getValue();
  436. if (empty($value) && !is_numeric($value)) {
  437. return 'The cookie value must not be empty';
  438. }
  439. // Domains must not be empty, but can be 0
  440. // A "0" is not a valid internet domain, but may be used as server name in a private network
  441. $domain = $this->getDomain();
  442. if (empty($domain) && !is_numeric($domain)) {
  443. return 'The cookie domain must not be empty';
  444. }
  445. return true;
  446. }
  447. /**
  448. * Set a value and return the cookie object
  449. *
  450. * @param string $key Key to set
  451. * @param string $value Value to set
  452. *
  453. * @return Cookie
  454. */
  455. private function setData($key, $value)
  456. {
  457. $this->data[$key] = $value;
  458. return $this;
  459. }
  460. }