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

/api/vendor/guzzlehttp/guzzle/src/Cookie/SetCookie.php

https://gitlab.com/x33n/respond
PHP | 373 lines | 351 code | 6 blank | 16 comment | 2 complexity | 00db8c8eb2182fddf8afadaaaff20460 MD5 | raw file
  1. <?php
  2. namespace GuzzleHttp\Cookie;
  3. use GuzzleHttp\ToArrayInterface;
  4. /**
  5. * Set-Cookie object
  6. */
  7. class SetCookie implements ToArrayInterface
  8. {
  9. /** @var array */
  10. private static $defaults = [
  11. 'Name' => null,
  12. 'Value' => null,
  13. 'Domain' => null,
  14. 'Path' => '/',
  15. 'Max-Age' => null,
  16. 'Expires' => null,
  17. 'Secure' => false,
  18. 'Discard' => false,
  19. 'HttpOnly' => false
  20. ];
  21. /** @var array Cookie data */
  22. private $data;
  23. /**
  24. * Create a new SetCookie object from a string
  25. *
  26. * @param string $cookie Set-Cookie header string
  27. *
  28. * @return self
  29. */
  30. public static function fromString($cookie)
  31. {
  32. // Create the default return array
  33. $data = self::$defaults;
  34. // Explode the cookie string using a series of semicolons
  35. $pieces = array_filter(array_map('trim', explode(';', $cookie)));
  36. // The name of the cookie (first kvp) must include an equal sign.
  37. if (empty($pieces) || !strpos($pieces[0], '=')) {
  38. return new self($data);
  39. }
  40. // Add the cookie pieces into the parsed data array
  41. foreach ($pieces as $part) {
  42. $cookieParts = explode('=', $part, 2);
  43. $key = trim($cookieParts[0]);
  44. $value = isset($cookieParts[1])
  45. ? trim($cookieParts[1], " \n\r\t\0\x0B\"")
  46. : true;
  47. // Only check for non-cookies when cookies have been found
  48. if (empty($data['Name'])) {
  49. $data['Name'] = $key;
  50. $data['Value'] = $value;
  51. } else {
  52. foreach (array_keys(self::$defaults) as $search) {
  53. if (!strcasecmp($search, $key)) {
  54. $data[$search] = $value;
  55. continue 2;
  56. }
  57. }
  58. $data[$key] = $value;
  59. }
  60. }
  61. return new self($data);
  62. }
  63. /**
  64. * @param array $data Array of cookie data provided by a Cookie parser
  65. */
  66. public function __construct(array $data = [])
  67. {
  68. $this->data = array_replace(self::$defaults, $data);
  69. // Extract the Expires value and turn it into a UNIX timestamp if needed
  70. if (!$this->getExpires() && $this->getMaxAge()) {
  71. // Calculate the Expires date
  72. $this->setExpires(time() + $this->getMaxAge());
  73. } elseif ($this->getExpires() && !is_numeric($this->getExpires())) {
  74. $this->setExpires($this->getExpires());
  75. }
  76. }
  77. public function __toString()
  78. {
  79. $str = $this->data['Name'] . '=' . $this->data['Value'] . '; ';
  80. foreach ($this->data as $k => $v) {
  81. if ($k != 'Name' && $k != 'Value' && $v !== null && $v !== false) {
  82. if ($k == 'Expires') {
  83. $str .= 'Expires=' . gmdate('D, d M Y H:i:s \G\M\T', $v) . '; ';
  84. } else {
  85. $str .= ($v === true ? $k : "{$k}={$v}") . '; ';
  86. }
  87. }
  88. }
  89. return rtrim($str, '; ');
  90. }
  91. public function toArray()
  92. {
  93. return $this->data;
  94. }
  95. /**
  96. * Get the cookie name
  97. *
  98. * @return string
  99. */
  100. public function getName()
  101. {
  102. return $this->data['Name'];
  103. }
  104. /**
  105. * Set the cookie name
  106. *
  107. * @param string $name Cookie name
  108. */
  109. public function setName($name)
  110. {
  111. $this->data['Name'] = $name;
  112. }
  113. /**
  114. * Get the cookie value
  115. *
  116. * @return string
  117. */
  118. public function getValue()
  119. {
  120. return $this->data['Value'];
  121. }
  122. /**
  123. * Set the cookie value
  124. *
  125. * @param string $value Cookie value
  126. */
  127. public function setValue($value)
  128. {
  129. $this->data['Value'] = $value;
  130. }
  131. /**
  132. * Get the domain
  133. *
  134. * @return string|null
  135. */
  136. public function getDomain()
  137. {
  138. return $this->data['Domain'];
  139. }
  140. /**
  141. * Set the domain of the cookie
  142. *
  143. * @param string $domain
  144. */
  145. public function setDomain($domain)
  146. {
  147. $this->data['Domain'] = $domain;
  148. }
  149. /**
  150. * Get the path
  151. *
  152. * @return string
  153. */
  154. public function getPath()
  155. {
  156. return $this->data['Path'];
  157. }
  158. /**
  159. * Set the path of the cookie
  160. *
  161. * @param string $path Path of the cookie
  162. */
  163. public function setPath($path)
  164. {
  165. $this->data['Path'] = $path;
  166. }
  167. /**
  168. * Maximum lifetime of the cookie in seconds
  169. *
  170. * @return int|null
  171. */
  172. public function getMaxAge()
  173. {
  174. return $this->data['Max-Age'];
  175. }
  176. /**
  177. * Set the max-age of the cookie
  178. *
  179. * @param int $maxAge Max age of the cookie in seconds
  180. */
  181. public function setMaxAge($maxAge)
  182. {
  183. $this->data['Max-Age'] = $maxAge;
  184. }
  185. /**
  186. * The UNIX timestamp when the cookie Expires
  187. *
  188. * @return mixed
  189. */
  190. public function getExpires()
  191. {
  192. return $this->data['Expires'];
  193. }
  194. /**
  195. * Set the unix timestamp for which the cookie will expire
  196. *
  197. * @param int $timestamp Unix timestamp
  198. */
  199. public function setExpires($timestamp)
  200. {
  201. $this->data['Expires'] = is_numeric($timestamp)
  202. ? (int) $timestamp
  203. : strtotime($timestamp);
  204. }
  205. /**
  206. * Get whether or not this is a secure cookie
  207. *
  208. * @return null|bool
  209. */
  210. public function getSecure()
  211. {
  212. return $this->data['Secure'];
  213. }
  214. /**
  215. * Set whether or not the cookie is secure
  216. *
  217. * @param bool $secure Set to true or false if secure
  218. */
  219. public function setSecure($secure)
  220. {
  221. $this->data['Secure'] = $secure;
  222. }
  223. /**
  224. * Get whether or not this is a session cookie
  225. *
  226. * @return null|bool
  227. */
  228. public function getDiscard()
  229. {
  230. return $this->data['Discard'];
  231. }
  232. /**
  233. * Set whether or not this is a session cookie
  234. *
  235. * @param bool $discard Set to true or false if this is a session cookie
  236. */
  237. public function setDiscard($discard)
  238. {
  239. $this->data['Discard'] = $discard;
  240. }
  241. /**
  242. * Get whether or not this is an HTTP only cookie
  243. *
  244. * @return bool
  245. */
  246. public function getHttpOnly()
  247. {
  248. return $this->data['HttpOnly'];
  249. }
  250. /**
  251. * Set whether or not this is an HTTP only cookie
  252. *
  253. * @param bool $httpOnly Set to true or false if this is HTTP only
  254. */
  255. public function setHttpOnly($httpOnly)
  256. {
  257. $this->data['HttpOnly'] = $httpOnly;
  258. }
  259. /**
  260. * Check if the cookie matches a path value
  261. *
  262. * @param string $path Path to check against
  263. *
  264. * @return bool
  265. */
  266. public function matchesPath($path)
  267. {
  268. return !$this->getPath() || 0 === stripos($path, $this->getPath());
  269. }
  270. /**
  271. * Check if the cookie matches a domain value
  272. *
  273. * @param string $domain Domain to check against
  274. *
  275. * @return bool
  276. */
  277. public function matchesDomain($domain)
  278. {
  279. // Remove the leading '.' as per spec in RFC 6265.
  280. // http://tools.ietf.org/html/rfc6265#section-5.2.3
  281. $cookieDomain = ltrim($this->getDomain(), '.');
  282. // Domain not set or exact match.
  283. if (!$cookieDomain || !strcasecmp($domain, $cookieDomain)) {
  284. return true;
  285. }
  286. // Matching the subdomain according to RFC 6265.
  287. // http://tools.ietf.org/html/rfc6265#section-5.1.3
  288. if (filter_var($domain, FILTER_VALIDATE_IP)) {
  289. return false;
  290. }
  291. return (bool) preg_match('/\.' . preg_quote($cookieDomain) . '$/i', $domain);
  292. }
  293. /**
  294. * Check if the cookie is expired
  295. *
  296. * @return bool
  297. */
  298. public function isExpired()
  299. {
  300. return $this->getExpires() && time() > $this->getExpires();
  301. }
  302. /**
  303. * Check if the cookie is valid according to RFC 6265
  304. *
  305. * @return bool|string Returns true if valid or an error message if invalid
  306. */
  307. public function validate()
  308. {
  309. // Names must not be empty, but can be 0
  310. $name = $this->getName();
  311. if (empty($name) && !is_numeric($name)) {
  312. return 'The cookie name must not be empty';
  313. }
  314. // Check if any of the invalid characters are present in the cookie name
  315. if (preg_match("/[=,; \t\r\n\013\014]/", $name)) {
  316. return "Cookie name must not cannot invalid characters: =,; \\t\\r\\n\\013\\014";
  317. }
  318. // Value must not be empty, but can be 0
  319. $value = $this->getValue();
  320. if (empty($value) && !is_numeric($value)) {
  321. return 'The cookie value must not be empty';
  322. }
  323. // Domains must not be empty, but can be 0
  324. // A "0" is not a valid internet domain, but may be used as server name
  325. // in a private network.
  326. $domain = $this->getDomain();
  327. if (empty($domain) && !is_numeric($domain)) {
  328. return 'The cookie domain must not be empty';
  329. }
  330. return true;
  331. }
  332. }