Go to the documentation of this file.00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00036 class CAS_CookieJar {
00037
00038 private $_cookies;
00039
00047 public function __construct (array &$storageArray) {
00048 $this->_cookies =& $storageArray;
00049 }
00050
00062 public function storeCookies ($request_url, $response_headers) {
00063 $urlParts = parse_url($request_url);
00064 $defaultDomain = $urlParts['host'];
00065
00066 $cookies = $this->parseCookieHeaders($response_headers, $defaultDomain);
00067
00068
00069 foreach ($cookies as $cookie) {
00070
00071
00072 if (!$this->cookieMatchesTarget($cookie, $urlParts))
00073 continue;
00074
00075
00076 $this->storeCookie($cookie);
00077
00078 phpCAS::trace($cookie['name'].' -> '.$cookie['value']);
00079 }
00080 }
00081
00092 public function getCookies ($request_url) {
00093 if (!count($this->_cookies))
00094 return array();
00095
00096
00097 $target = parse_url($request_url);
00098 if ($target === FALSE)
00099 return array();
00100
00101 $this->expireCookies();
00102
00103 $matching_cookies = array();
00104 foreach ($this->_cookies as $key => $cookie) {
00105 if ($this->cookieMatchesTarget($cookie, $target)) {
00106 $matching_cookies[$cookie['name']] = $cookie['value'];
00107 }
00108 }
00109 return $matching_cookies;
00110 }
00111
00112
00120 protected function parseCookieHeaders( $header, $defaultDomain ) {
00121 phpCAS::traceBegin();
00122 $cookies = array();
00123 foreach( $header as $line ) {
00124 if( preg_match( '/^Set-Cookie2?: /i', $line ) ) {
00125 $cookies[] = $this->parseCookieHeader($line, $defaultDomain);
00126 }
00127 }
00128
00129 phpCAS::traceEnd($cookies);
00130 return $cookies;
00131 }
00132
00142 protected function parseCookieHeader ($line, $defaultDomain) {
00143 if (!$defaultDomain)
00144 throw new InvalidArgumentException('$defaultDomain was not provided.');
00145
00146
00147 $cookie = array(
00148 'domain' => $defaultDomain,
00149 'path' => '/',
00150 'secure' => false,
00151 );
00152
00153 $line = preg_replace( '/^Set-Cookie2?: /i', '', trim( $line ) );
00154
00155
00156 $line = trim($line, ';');
00157
00158 phpCAS::trace("Cookie Line: $line");
00159
00160
00161
00162
00163
00164 $attributeStrings = explode( ';', $line );
00165
00166 foreach( $attributeStrings as $attributeString ) {
00167
00168
00169
00170
00171 $attributeParts = explode( '=', $attributeString );
00172
00173 $attributeName = trim($attributeParts[0]);
00174 $attributeNameLC = strtolower($attributeName);
00175
00176 if (isset($attributeParts[1])) {
00177 $attributeValue = trim($attributeParts[1]);
00178
00179 if (strpos($attributeValue, '"') === 0) {
00180 $attributeValue = trim($attributeValue, '"');
00181
00182 $attributeValue = str_replace('\"', '"', $attributeValue);
00183 }
00184 } else {
00185 $attributeValue = null;
00186 }
00187
00188 switch ($attributeNameLC) {
00189 case 'expires':
00190 $cookie['expires'] = strtotime($attributeValue);
00191 break;
00192 case 'max-age':
00193 $cookie['max-age'] = (int)$attributeValue;
00194
00195 if ($cookie['max-age'])
00196 $cookie['expires'] = time() + $cookie['max-age'];
00197
00198
00199 else
00200 $cookie['expires'] = time() - 1;
00201 break;
00202 case 'secure':
00203 $cookie['secure'] = true;
00204 break;
00205 case 'domain':
00206 case 'path':
00207 case 'port':
00208 case 'version':
00209 case 'comment':
00210 case 'commenturl':
00211 case 'discard':
00212 case 'httponly':
00213 $cookie[$attributeNameLC] = $attributeValue;
00214 break;
00215 default:
00216 $cookie['name'] = $attributeName;
00217 $cookie['value'] = $attributeValue;
00218 }
00219 }
00220
00221 return $cookie;
00222 }
00223
00233 protected function storeCookie ($cookie) {
00234
00235 $this->discardCookie($cookie);
00236 $this->_cookies[] = $cookie;
00237
00238 }
00239
00249 protected function discardCookie ($cookie) {
00250 if (!isset($cookie['domain']) || !isset($cookie['path']) || !isset($cookie['path']))
00251 throw new InvalidArgumentException('Invalid Cookie array passed.');
00252
00253 foreach ($this->_cookies as $key => $old_cookie) {
00254 if ($cookie['domain'] == $old_cookie['domain']
00255 && $cookie['path'] == $old_cookie['path']
00256 && $cookie['name'] == $old_cookie['name'])
00257 {
00258 unset($this->_cookies[$key]);
00259 }
00260 }
00261 }
00262
00270 protected function expireCookies () {
00271 foreach ($this->_cookies as $key => $cookie) {
00272 if (isset($cookie['expires']) && $cookie['expires'] < time()) {
00273 unset($this->_cookies[$key]);
00274 }
00275 }
00276 }
00277
00288 protected function cookieMatchesTarget ($cookie, $target) {
00289 if (!is_array($target))
00290 throw new InvalidArgumentException('$target must be an array of URL attributes as generated by parse_url().');
00291 if (!isset($target['host']))
00292 throw new InvalidArgumentException('$target must be an array of URL attributes as generated by parse_url().');
00293
00294
00295 if ($cookie['secure'] && $target['scheme'] != 'https')
00296 return false;
00297
00298
00299
00300 if (strpos($cookie['domain'], '.') === 0) {
00301
00302 $pos = strripos($target['host'], $cookie['domain']);
00303 if (!$pos)
00304 return false;
00305
00306 if ($pos + strlen($cookie['domain']) != strlen($target['host']))
00307 return false;
00308 }
00309
00310
00311 else {
00312 if (strcasecmp($target['host'], $cookie['domain']) !== 0)
00313 return false;
00314 }
00315
00316
00317 if (isset($cookie['ports']) && !in_array($target['port'], $cookie['ports']))
00318 return false;
00319
00320
00321 if (strpos($target['path'], $cookie['path']) !== 0)
00322 return false;
00323
00324 return true;
00325 }
00326
00327 }
00328
00329 ?>