MediaWiki REL1_34
CookieJar.php
Go to the documentation of this file.
1<?php
25class CookieJar {
27 private $cookie = [];
28
36 public function setCookie( $name, $value, $attr ) {
37 /* cookies: case insensitive, so this should work.
38 * We'll still send the cookies back in the same case we got them, though.
39 */
40 $index = strtoupper( $name );
41
42 if ( isset( $this->cookie[$index] ) ) {
43 $this->cookie[$index]->set( $value, $attr );
44 } else {
45 $this->cookie[$index] = new Cookie( $name, $value, $attr );
46 }
47 }
48
55 public function serializeToHttpRequest( $path, $domain ) {
56 $cookies = [];
57
58 foreach ( $this->cookie as $c ) {
59 $serialized = $c->serializeToHttpRequest( $path, $domain );
60
61 if ( $serialized ) {
62 $cookies[] = $serialized;
63 }
64 }
65
66 return implode( '; ', $cookies );
67 }
68
76 public function parseCookieResponseHeader( $cookie, $domain ) {
77 $len = strlen( 'Set-Cookie:' );
78
79 if ( substr_compare( 'Set-Cookie:', $cookie, 0, $len, true ) === 0 ) {
80 $cookie = substr( $cookie, $len );
81 }
82
83 $bit = array_map( 'trim', explode( ';', $cookie ) );
84
85 if ( count( $bit ) >= 1 ) {
86 list( $name, $value ) = explode( '=', array_shift( $bit ), 2 );
87 $attr = [];
88
89 foreach ( $bit as $piece ) {
90 $parts = explode( '=', $piece );
91 if ( count( $parts ) > 1 ) {
92 $attr[strtolower( $parts[0] )] = $parts[1];
93 } else {
94 $attr[strtolower( $parts[0] )] = true;
95 }
96 }
97
98 if ( !isset( $attr['domain'] ) ) {
99 $attr['domain'] = $domain;
100 } elseif ( !Cookie::validateCookieDomain( $attr['domain'], $domain ) ) {
101 return null;
102 }
103
104 $this->setCookie( $name, $value, $attr );
105 }
106 }
107}
Cookie jar to use with MWHttpRequest.
Definition CookieJar.php:25
Cookie[] $cookie
Definition CookieJar.php:27
setCookie( $name, $value, $attr)
Set a cookie in the cookie jar.
Definition CookieJar.php:36
serializeToHttpRequest( $path, $domain)
Definition CookieJar.php:55
parseCookieResponseHeader( $cookie, $domain)
Parse the content of an Set-Cookie HTTP Response header.
Definition CookieJar.php:76
static validateCookieDomain( $domain, $originDomain=null)
Return the true if the cookie is valid is valid.
Definition Cookie.php:88
foreach( $res as $row) $serialized