1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// | PHP version 4 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2003 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 3.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available through the world-wide-web at |
// | http://www.php.net/license/3_0.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | [email protected] so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Author: Alexey Borzov <[email protected]> |
// +----------------------------------------------------------------------+
//
// $Id: CookieManager.php,v 1.3 2004/04/10 10:04:52 avb Exp $
/**
* This class is used to store cookies and pass them between HTTP requests.
*
* @package HTTP_Client
* @author Alexey Borzov <[email protected]>
* @version $Revision: 1.3 $
*/
class HTTP_Client_CookieManager
{
/**
* An array containing cookie values
* @var array
*/
var $_cookies = array();
/**
* Constructor
*
* @access public
*/
function HTTP_Client_CookieManager()
{
// abstract
}
/**
* Adds cookies to the request
*
* @access public
* @param object An HTTP_Request object
*/
function passCookies(&$request)
{
if (!empty($this->_cookies)) {
$url =& $request->_url;
// We do not check cookie's "expires" field, as we do not store deleted
// cookies in the array and our client does not work long enough for other
// cookies to expire. If some kind of persistence is added to this object,
// then expiration should be checked upon loading and session cookies should
// be cleared on saving.
$cookies = array();
foreach ($this->_cookies as $cookie) {
if ($this->_domainMatch($url->host, $cookie['domain']) && (0 === strpos($url->path, $cookie['path']))
&& (empty($cookie['secure']) || $url->protocol == 'https')) {
$cookies[$cookie['name']][strlen($cookie['path'])] = $cookie['value'];
}
}
// cookies with longer paths go first
foreach ($cookies as $name => $values) {
krsort($values);
foreach ($values as $value) {
$request->addCookie($name, $value);
}
}
}
return true;
}
/**
* Explicitly adds cookie to the list
*
* @param array An array representing cookie, this function expects all of the array's
* fields to be set
* @access public
*/
function addCookie($cookie)
{
$hash = $this->_makeHash($cookie['name'], $cookie['domain'], $cookie['path']);
$this->_cookies[$hash] = $cookie;
}
/**
* Updates cookie list from HTTP server response
*
* @access public
* @param object An HTTP_Request object with sendRequest() already done
*/
function updateCookies(&$request)
{
if (false !== ($cookies = $request->getResponseCookies())) {
$url =& $request->_url;
foreach ($cookies as $cookie) {
// use the current domain by default
if (!isset($cookie['domain'])) {
$cookie['domain'] = $url->host;
}
// use the path to the current page by default
if (!isset($cookie['path'])) {
$cookie['path'] = DIRECTORY_SEPARATOR == dirname($url->path)? '/': dirname($url->path);
}
// check if the domains match
if ($this->_domainMatch($url->host, $cookie['domain'])) {
$hash = $this->_makeHash($cookie['name'], $cookie['domain'], $cookie['path']);
// if value is empty or the time is in the past the cookie is deleted, else added
if (strlen($cookie['value'])
&& (!isset($cookie['expires']) || (strtotime($cookie['expires']) > time()))) {
$this->_cookies[$hash] = $cookie;
} elseif (isset($this->_cookies[$hash])) {
unset($this->_cookies[$hash]);
}
}
}
}
}
/**
* Generates a key for the $_cookies array.
*
* The cookies is uniquely identified by its name, domain and path.
* Thus we cannot make f.e. an associative array with name as a key, we should
* generate a key from these 3 values.
*
* @access private
* @param string Cookie name
* @param string Cookie domain
* @param string Cookie path
* @return string a key
*/
function _makeHash($name, $domain, $path)
{
return md5($name . "\r\n" . $domain . "\r\n" . $path);
}
/**
* Checks whether a cookie domain matches a request host.
*
* Cookie domain can begin with a dot, it also must contain at least
* two dots.
*
* @access private
* @param string request host
* @param string cookie domain
* @return bool match success
*/
function _domainMatch($requestHost, $cookieDomain)
{
if ('.' != $cookieDomain{0}) {
return $requestHost == $cookieDomain;
} elseif (substr_count($cookieDomain, '.') < 2) {
return false;
} else {
return substr('.'. $requestHost, - strlen($cookieDomain)) == $cookieDomain;
}
}
/**
* Clears the $_cookies array
*
* @access public
*/
function reset()
{
$this->_cookies = array();
}
}
?>
|