filebackend: clean up some comments and remove unused FileBackendStoreOpHandle field
[lhc/web/wiklou.git] / includes / libs / CookieJar.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 * @ingroup HTTP
20 */
21
22 /**
23 * Cookie jar to use with MWHttpRequest. Does not handle cookie unsetting.
24 */
25 class CookieJar {
26 /** @var Cookie[] */
27 private $cookie = [];
28
29 /**
30 * Set a cookie in the cookie jar. Make sure only one cookie per-name exists.
31 * @see Cookie::set()
32 * @param string $name
33 * @param string $value
34 * @param array $attr
35 */
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
49 /**
50 * @see Cookie::serializeToHttpRequest
51 * @param string $path
52 * @param string $domain
53 * @return string
54 */
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
69 /**
70 * Parse the content of an Set-Cookie HTTP Response header.
71 *
72 * @param string $cookie
73 * @param string $domain Cookie's domain
74 * @return null
75 */
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 }