MediaWiki master
CookieJar.php
Go to the documentation of this file.
1<?php
11class CookieJar {
12
14 private array $cookie = [];
15
23 public function setCookie( string $name, string $value, array $attr ): void {
24 /* cookies: case insensitive, so this should work.
25 * We'll still send the cookies back in the same case we got them, though.
26 */
27 $index = strtoupper( $name );
28
29 if ( isset( $this->cookie[$index] ) ) {
30 $this->cookie[$index]->set( $value, $attr );
31 } else {
32 $this->cookie[$index] = new Cookie( $name, $value, $attr );
33 }
34 }
35
39 public function serializeToHttpRequest( string $path, string $domain ): string {
40 $cookies = [];
41
42 foreach ( $this->cookie as $c ) {
43 $serialized = $c->serializeToHttpRequest( $path, $domain );
44 if ( $serialized ) {
45 $cookies[] = $serialized;
46 }
47 }
48
49 return implode( '; ', $cookies );
50 }
51
58 public function parseCookieResponseHeader( string $cookie, string $domain ): void {
59 $bit = array_map( 'trim', explode( ';', $cookie ) );
60
61 $parts = explode( '=', array_shift( $bit ), 2 );
62 $name = $parts[0];
63 $value = $parts[1] ?? '';
64
65 $attr = [];
66 foreach ( $bit as $piece ) {
67 $parts = explode( '=', $piece, 2 );
68 $attr[ strtolower( $parts[0] ) ] = $parts[1] ?? true;
69 }
70
71 if ( !isset( $attr['domain'] ) ) {
72 $attr['domain'] = $domain;
73 } elseif ( !Cookie::validateCookieDomain( $attr['domain'], $domain ) ) {
74 return;
75 }
76
77 $this->setCookie( $name, $value, $attr );
78 }
79
80}
Cookie jar to use with MWHttpRequest.
Definition CookieJar.php:11
setCookie(string $name, string $value, array $attr)
Set a cookie in the cookie jar.
Definition CookieJar.php:23
serializeToHttpRequest(string $path, string $domain)
Definition CookieJar.php:39
parseCookieResponseHeader(string $cookie, string $domain)
Parse the content of an Set-Cookie HTTP Response header.
Definition CookieJar.php:58
Cookie for HTTP requests.
Definition Cookie.php:12
static validateCookieDomain(string $domain, ?string $originDomain=null)
Return the true if the cookie is valid is valid.
Definition Cookie.php:71