PageRenderTime 66ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/Requests/Cookie.php

https://gitlab.com/campus-academy/krowkaramel
PHP | 505 lines | 240 code | 63 blank | 202 comment | 49 complexity | 749b5685e38bd71c73554ec096f364ad MD5 | raw file
  1. <?php
  2. /**
  3. * Cookie storage object
  4. *
  5. * @package Requests
  6. * @subpackage Cookies
  7. */
  8. /**
  9. * Cookie storage object
  10. *
  11. * @package Requests
  12. * @subpackage Cookies
  13. */
  14. class Requests_Cookie {
  15. /**
  16. * Cookie name.
  17. *
  18. * @var string
  19. */
  20. public $name;
  21. /**
  22. * Cookie value.
  23. *
  24. * @var string
  25. */
  26. public $value;
  27. /**
  28. * Cookie attributes
  29. *
  30. * Valid keys are (currently) path, domain, expires, max-age, secure and
  31. * httponly.
  32. *
  33. * @var Requests_Utility_CaseInsensitiveDictionary|array Array-like object
  34. */
  35. public $attributes = array();
  36. /**
  37. * Cookie flags
  38. *
  39. * Valid keys are (currently) creation, last-access, persistent and
  40. * host-only.
  41. *
  42. * @var array
  43. */
  44. public $flags = array();
  45. /**
  46. * Reference time for relative calculations
  47. *
  48. * This is used in place of `time()` when calculating Max-Age expiration and
  49. * checking time validity.
  50. *
  51. * @var int
  52. */
  53. public $reference_time = 0;
  54. /**
  55. * Create a new cookie object
  56. *
  57. * @param string $name
  58. * @param string $value
  59. * @param array|Requests_Utility_CaseInsensitiveDictionary $attributes Associative array of attribute data
  60. */
  61. public function __construct($name, $value, $attributes = array(), $flags = array(), $reference_time = null) {
  62. $this->name = $name;
  63. $this->value = $value;
  64. $this->attributes = $attributes;
  65. $default_flags = array(
  66. 'creation' => time(),
  67. 'last-access' => time(),
  68. 'persistent' => false,
  69. 'host-only' => true,
  70. );
  71. $this->flags = array_merge($default_flags, $flags);
  72. $this->reference_time = time();
  73. if ($reference_time !== null) {
  74. $this->reference_time = $reference_time;
  75. }
  76. $this->normalize();
  77. }
  78. /**
  79. * Check if a cookie is expired.
  80. *
  81. * Checks the age against $this->reference_time to determine if the cookie
  82. * is expired.
  83. *
  84. * @return boolean True if expired, false if time is valid.
  85. */
  86. public function is_expired() {
  87. // RFC6265, s. 4.1.2.2:
  88. // If a cookie has both the Max-Age and the Expires attribute, the Max-
  89. // Age attribute has precedence and controls the expiration date of the
  90. // cookie.
  91. if (isset($this->attributes['max-age'])) {
  92. $max_age = $this->attributes['max-age'];
  93. return $max_age < $this->reference_time;
  94. }
  95. if (isset($this->attributes['expires'])) {
  96. $expires = $this->attributes['expires'];
  97. return $expires < $this->reference_time;
  98. }
  99. return false;
  100. }
  101. /**
  102. * Check if a cookie is valid for a given URI
  103. *
  104. * @param Requests_IRI $uri URI to check
  105. * @return boolean Whether the cookie is valid for the given URI
  106. */
  107. public function uri_matches(Requests_IRI $uri) {
  108. if (!$this->domain_matches($uri->host)) {
  109. return false;
  110. }
  111. if (!$this->path_matches($uri->path)) {
  112. return false;
  113. }
  114. return empty($this->attributes['secure']) || $uri->scheme === 'https';
  115. }
  116. /**
  117. * Check if a cookie is valid for a given domain
  118. *
  119. * @param string $string Domain to check
  120. * @return boolean Whether the cookie is valid for the given domain
  121. */
  122. public function domain_matches($string) {
  123. if (!isset($this->attributes['domain'])) {
  124. // Cookies created manually; cookies created by Requests will set
  125. // the domain to the requested domain
  126. return true;
  127. }
  128. $domain_string = $this->attributes['domain'];
  129. if ($domain_string === $string) {
  130. // The domain string and the string are identical.
  131. return true;
  132. }
  133. // If the cookie is marked as host-only and we don't have an exact
  134. // match, reject the cookie
  135. if ($this->flags['host-only'] === true) {
  136. return false;
  137. }
  138. if (strlen($string) <= strlen($domain_string)) {
  139. // For obvious reasons, the string cannot be a suffix if the domain
  140. // is shorter than the domain string
  141. return false;
  142. }
  143. if (substr($string, -1 * strlen($domain_string)) !== $domain_string) {
  144. // The domain string should be a suffix of the string.
  145. return false;
  146. }
  147. $prefix = substr($string, 0, strlen($string) - strlen($domain_string));
  148. if (substr($prefix, -1) !== '.') {
  149. // The last character of the string that is not included in the
  150. // domain string should be a %x2E (".") character.
  151. return false;
  152. }
  153. // The string should be a host name (i.e., not an IP address).
  154. return !preg_match('#^(.+\.)\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$#', $string);
  155. }
  156. /**
  157. * Check if a cookie is valid for a given path
  158. *
  159. * From the path-match check in RFC 6265 section 5.1.4
  160. *
  161. * @param string $request_path Path to check
  162. * @return boolean Whether the cookie is valid for the given path
  163. */
  164. public function path_matches($request_path) {
  165. if (empty($request_path)) {
  166. // Normalize empty path to root
  167. $request_path = '/';
  168. }
  169. if (!isset($this->attributes['path'])) {
  170. // Cookies created manually; cookies created by Requests will set
  171. // the path to the requested path
  172. return true;
  173. }
  174. $cookie_path = $this->attributes['path'];
  175. if ($cookie_path === $request_path) {
  176. // The cookie-path and the request-path are identical.
  177. return true;
  178. }
  179. if (strlen($request_path) > strlen($cookie_path) && substr($request_path, 0, strlen($cookie_path)) === $cookie_path) {
  180. if (substr($cookie_path, -1) === '/') {
  181. // The cookie-path is a prefix of the request-path, and the last
  182. // character of the cookie-path is %x2F ("/").
  183. return true;
  184. }
  185. if (substr($request_path, strlen($cookie_path), 1) === '/') {
  186. // The cookie-path is a prefix of the request-path, and the
  187. // first character of the request-path that is not included in
  188. // the cookie-path is a %x2F ("/") character.
  189. return true;
  190. }
  191. }
  192. return false;
  193. }
  194. /**
  195. * Normalize cookie and attributes
  196. *
  197. * @return boolean Whether the cookie was successfully normalized
  198. */
  199. public function normalize() {
  200. foreach ($this->attributes as $key => $value) {
  201. $orig_value = $value;
  202. $value = $this->normalize_attribute($key, $value);
  203. if ($value === null) {
  204. unset($this->attributes[$key]);
  205. continue;
  206. }
  207. if ($value !== $orig_value) {
  208. $this->attributes[$key] = $value;
  209. }
  210. }
  211. return true;
  212. }
  213. /**
  214. * Parse an individual cookie attribute
  215. *
  216. * Handles parsing individual attributes from the cookie values.
  217. *
  218. * @param string $name Attribute name
  219. * @param string|boolean $value Attribute value (string value, or true if empty/flag)
  220. * @return mixed Value if available, or null if the attribute value is invalid (and should be skipped)
  221. */
  222. protected function normalize_attribute($name, $value) {
  223. switch (strtolower($name)) {
  224. case 'expires':
  225. // Expiration parsing, as per RFC 6265 section 5.2.1
  226. if (is_int($value)) {
  227. return $value;
  228. }
  229. $expiry_time = strtotime($value);
  230. if ($expiry_time === false) {
  231. return null;
  232. }
  233. return $expiry_time;
  234. case 'max-age':
  235. // Expiration parsing, as per RFC 6265 section 5.2.2
  236. if (is_int($value)) {
  237. return $value;
  238. }
  239. // Check that we have a valid age
  240. if (!preg_match('/^-?\d+$/', $value)) {
  241. return null;
  242. }
  243. $delta_seconds = (int) $value;
  244. if ($delta_seconds <= 0) {
  245. $expiry_time = 0;
  246. }
  247. else {
  248. $expiry_time = $this->reference_time + $delta_seconds;
  249. }
  250. return $expiry_time;
  251. case 'domain':
  252. // Domains are not required as per RFC 6265 section 5.2.3
  253. if (empty($value)) {
  254. return null;
  255. }
  256. // Domain normalization, as per RFC 6265 section 5.2.3
  257. if ($value[0] === '.') {
  258. $value = substr($value, 1);
  259. }
  260. return $value;
  261. default:
  262. return $value;
  263. }
  264. }
  265. /**
  266. * Format a cookie for a Cookie header
  267. *
  268. * This is used when sending cookies to a server.
  269. *
  270. * @return string Cookie formatted for Cookie header
  271. */
  272. public function format_for_header() {
  273. return sprintf('%s=%s', $this->name, $this->value);
  274. }
  275. /**
  276. * Format a cookie for a Cookie header
  277. *
  278. * @codeCoverageIgnore
  279. * @deprecated Use {@see Requests_Cookie::format_for_header}
  280. * @return string
  281. */
  282. public function formatForHeader() {
  283. return $this->format_for_header();
  284. }
  285. /**
  286. * Format a cookie for a Set-Cookie header
  287. *
  288. * This is used when sending cookies to clients. This isn't really
  289. * applicable to client-side usage, but might be handy for debugging.
  290. *
  291. * @return string Cookie formatted for Set-Cookie header
  292. */
  293. public function format_for_set_cookie() {
  294. $header_value = $this->format_for_header();
  295. if (!empty($this->attributes)) {
  296. $parts = array();
  297. foreach ($this->attributes as $key => $value) {
  298. // Ignore non-associative attributes
  299. if (is_numeric($key)) {
  300. $parts[] = $value;
  301. }
  302. else {
  303. $parts[] = sprintf('%s=%s', $key, $value);
  304. }
  305. }
  306. $header_value .= '; ' . implode('; ', $parts);
  307. }
  308. return $header_value;
  309. }
  310. /**
  311. * Format a cookie for a Set-Cookie header
  312. *
  313. * @codeCoverageIgnore
  314. * @deprecated Use {@see Requests_Cookie::format_for_set_cookie}
  315. * @return string
  316. */
  317. public function formatForSetCookie() {
  318. return $this->format_for_set_cookie();
  319. }
  320. /**
  321. * Get the cookie value
  322. *
  323. * Attributes and other data can be accessed via methods.
  324. */
  325. public function __toString() {
  326. return $this->value;
  327. }
  328. /**
  329. * Parse a cookie string into a cookie object
  330. *
  331. * Based on Mozilla's parsing code in Firefox and related projects, which
  332. * is an intentional deviation from RFC 2109 and RFC 2616. RFC 6265
  333. * specifies some of this handling, but not in a thorough manner.
  334. *
  335. * @param string Cookie header value (from a Set-Cookie header)
  336. * @return Requests_Cookie Parsed cookie object
  337. */
  338. public static function parse($string, $name = '', $reference_time = null) {
  339. $parts = explode(';', $string);
  340. $kvparts = array_shift($parts);
  341. if (!empty($name)) {
  342. $value = $string;
  343. }
  344. elseif (strpos($kvparts, '=') === false) {
  345. // Some sites might only have a value without the equals separator.
  346. // Deviate from RFC 6265 and pretend it was actually a blank name
  347. // (`=foo`)
  348. //
  349. // https://bugzilla.mozilla.org/show_bug.cgi?id=169091
  350. $name = '';
  351. $value = $kvparts;
  352. }
  353. else {
  354. list($name, $value) = explode('=', $kvparts, 2);
  355. }
  356. $name = trim($name);
  357. $value = trim($value);
  358. // Attribute key are handled case-insensitively
  359. $attributes = new Requests_Utility_CaseInsensitiveDictionary();
  360. if (!empty($parts)) {
  361. foreach ($parts as $part) {
  362. if (strpos($part, '=') === false) {
  363. $part_key = $part;
  364. $part_value = true;
  365. }
  366. else {
  367. list($part_key, $part_value) = explode('=', $part, 2);
  368. $part_value = trim($part_value);
  369. }
  370. $part_key = trim($part_key);
  371. $attributes[$part_key] = $part_value;
  372. }
  373. }
  374. return new Requests_Cookie($name, $value, $attributes, array(), $reference_time);
  375. }
  376. /**
  377. * Parse all Set-Cookie headers from request headers
  378. *
  379. * @param Requests_Response_Headers $headers Headers to parse from
  380. * @param Requests_IRI|null $origin URI for comparing cookie origins
  381. * @param int|null $time Reference time for expiration calculation
  382. * @return array
  383. */
  384. public static function parse_from_headers(Requests_Response_Headers $headers, Requests_IRI $origin = null, $time = null) {
  385. $cookie_headers = $headers->getValues('Set-Cookie');
  386. if (empty($cookie_headers)) {
  387. return array();
  388. }
  389. $cookies = array();
  390. foreach ($cookie_headers as $header) {
  391. $parsed = self::parse($header, '', $time);
  392. // Default domain/path attributes
  393. if (empty($parsed->attributes['domain']) && !empty($origin)) {
  394. $parsed->attributes['domain'] = $origin->host;
  395. $parsed->flags['host-only'] = true;
  396. }
  397. else {
  398. $parsed->flags['host-only'] = false;
  399. }
  400. $path_is_valid = (!empty($parsed->attributes['path']) && $parsed->attributes['path'][0] === '/');
  401. if (!$path_is_valid && !empty($origin)) {
  402. $path = $origin->path;
  403. // Default path normalization as per RFC 6265 section 5.1.4
  404. if (substr($path, 0, 1) !== '/') {
  405. // If the uri-path is empty or if the first character of
  406. // the uri-path is not a %x2F ("/") character, output
  407. // %x2F ("/") and skip the remaining steps.
  408. $path = '/';
  409. }
  410. elseif (substr_count($path, '/') === 1) {
  411. // If the uri-path contains no more than one %x2F ("/")
  412. // character, output %x2F ("/") and skip the remaining
  413. // step.
  414. $path = '/';
  415. }
  416. else {
  417. // Output the characters of the uri-path from the first
  418. // character up to, but not including, the right-most
  419. // %x2F ("/").
  420. $path = substr($path, 0, strrpos($path, '/'));
  421. }
  422. $parsed->attributes['path'] = $path;
  423. }
  424. // Reject invalid cookie domains
  425. if (!empty($origin) && !$parsed->domain_matches($origin->host)) {
  426. continue;
  427. }
  428. $cookies[$parsed->name] = $parsed;
  429. }
  430. return $cookies;
  431. }
  432. /**
  433. * Parse all Set-Cookie headers from request headers
  434. *
  435. * @codeCoverageIgnore
  436. * @deprecated Use {@see Requests_Cookie::parse_from_headers}
  437. * @return array
  438. */
  439. public static function parseFromHeaders(Requests_Response_Headers $headers) {
  440. return self::parse_from_headers($headers);
  441. }
  442. }