Rank aliases in search in order they appear in the messages file.
[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 class CookieJar {
23 private $cookie = [];
24
25 /**
26 * Set a cookie in the cookie jar. Make sure only one cookie per-name exists.
27 * @see Cookie::set()
28 * @param string $name
29 * @param string $value
30 * @param array $attr
31 */
32 public function setCookie( $name, $value, $attr ) {
33 /* cookies: case insensitive, so this should work.
34 * We'll still send the cookies back in the same case we got them, though.
35 */
36 $index = strtoupper( $name );
37
38 if ( isset( $this->cookie[$index] ) ) {
39 $this->cookie[$index]->set( $value, $attr );
40 } else {
41 $this->cookie[$index] = new Cookie( $name, $value, $attr );
42 }
43 }
44
45 /**
46 * @see Cookie::serializeToHttpRequest
47 * @param string $path
48 * @param string $domain
49 * @return string
50 */
51 public function serializeToHttpRequest( $path, $domain ) {
52 $cookies = [];
53
54 foreach ( $this->cookie as $c ) {
55 $serialized = $c->serializeToHttpRequest( $path, $domain );
56
57 if ( $serialized ) {
58 $cookies[] = $serialized;
59 }
60 }
61
62 return implode( '; ', $cookies );
63 }
64
65 /**
66 * Parse the content of an Set-Cookie HTTP Response header.
67 *
68 * @param string $cookie
69 * @param string $domain Cookie's domain
70 * @return null
71 */
72 public function parseCookieResponseHeader( $cookie, $domain ) {
73 $len = strlen( 'Set-Cookie:' );
74
75 if ( substr_compare( 'Set-Cookie:', $cookie, 0, $len, true ) === 0 ) {
76 $cookie = substr( $cookie, $len );
77 }
78
79 $bit = array_map( 'trim', explode( ';', $cookie ) );
80
81 if ( count( $bit ) >= 1 ) {
82 list( $name, $value ) = explode( '=', array_shift( $bit ), 2 );
83 $attr = [];
84
85 foreach ( $bit as $piece ) {
86 $parts = explode( '=', $piece );
87 if ( count( $parts ) > 1 ) {
88 $attr[strtolower( $parts[0] )] = $parts[1];
89 } else {
90 $attr[strtolower( $parts[0] )] = true;
91 }
92 }
93
94 if ( !isset( $attr['domain'] ) ) {
95 $attr['domain'] = $domain;
96 } elseif ( !Cookie::validateCookieDomain( $attr['domain'], $domain ) ) {
97 return null;
98 }
99
100 $this->setCookie( $name, $value, $attr );
101 }
102 }
103 }