Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/zend/Zend/Http/CookieJar.php
Go to the documentation of this file.
00001 <?php
00026 require_once "Zend/Uri.php";
00030 require_once "Zend/Http/Cookie.php";
00034 require_once "Zend/Http/Response.php";
00035 
00060 class Zend_Http_CookieJar implements Countable, IteratorAggregate
00061 {
00066     const COOKIE_OBJECT = 0;
00067 
00072     const COOKIE_STRING_ARRAY = 1;
00073 
00078     const COOKIE_STRING_CONCAT = 2;
00079 
00097     protected $cookies = array();
00098 
00104     protected $_rawCookies = array();
00105 
00110     public function __construct()
00111     { }
00112 
00120     public function addCookie($cookie, $ref_uri = null)
00121     {
00122         if (is_string($cookie)) {
00123             $cookie = Zend_Http_Cookie::fromString($cookie, $ref_uri);
00124         }
00125 
00126         if ($cookie instanceof Zend_Http_Cookie) {
00127             $domain = $cookie->getDomain();
00128             $path = $cookie->getPath();
00129             if (! isset($this->cookies[$domain])) $this->cookies[$domain] = array();
00130             if (! isset($this->cookies[$domain][$path])) $this->cookies[$domain][$path] = array();
00131             $this->cookies[$domain][$path][$cookie->getName()] = $cookie;
00132             $this->_rawCookies[] = $cookie;
00133         } else {
00134             require_once 'Zend/Http/Exception.php';
00135             throw new Zend_Http_Exception('Supplient argument is not a valid cookie string or object');
00136         }
00137     }
00138 
00146     public function addCookiesFromResponse($response, $ref_uri)
00147     {
00148         if (! $response instanceof Zend_Http_Response) {
00149             require_once 'Zend/Http/Exception.php';
00150             throw new Zend_Http_Exception('$response is expected to be a Response object, ' .
00151                 gettype($response) . ' was passed');
00152         }
00153 
00154         $cookie_hdrs = $response->getHeader('Set-Cookie');
00155 
00156         if (is_array($cookie_hdrs)) {
00157             foreach ($cookie_hdrs as $cookie) {
00158                 $this->addCookie($cookie, $ref_uri);
00159             }
00160         } elseif (is_string($cookie_hdrs)) {
00161             $this->addCookie($cookie_hdrs, $ref_uri);
00162         }
00163     }
00164 
00171     public function getAllCookies($ret_as = self::COOKIE_OBJECT)
00172     {
00173         $cookies = $this->_flattenCookiesArray($this->cookies, $ret_as);
00174         return $cookies;
00175     }
00176 
00188     public function getMatchingCookies($uri, $matchSessionCookies = true,
00189         $ret_as = self::COOKIE_OBJECT, $now = null)
00190     {
00191         if (is_string($uri)) $uri = Zend_Uri::factory($uri);
00192         if (! $uri instanceof Zend_Uri_Http) {
00193             require_once 'Zend/Http/Exception.php';
00194             throw new Zend_Http_Exception("Invalid URI string or object passed");
00195         }
00196 
00197         // First, reduce the array of cookies to only those matching domain and path
00198         $cookies = $this->_matchDomain($uri->getHost());
00199         $cookies = $this->_matchPath($cookies, $uri->getPath());
00200         $cookies = $this->_flattenCookiesArray($cookies, self::COOKIE_OBJECT);
00201 
00202         // Next, run Cookie->match on all cookies to check secure, time and session mathcing
00203         $ret = array();
00204         foreach ($cookies as $cookie)
00205             if ($cookie->match($uri, $matchSessionCookies, $now))
00206                 $ret[] = $cookie;
00207 
00208         // Now, use self::_flattenCookiesArray again - only to convert to the return format ;)
00209         $ret = $this->_flattenCookiesArray($ret, $ret_as);
00210 
00211         return $ret;
00212     }
00213 
00222     public function getCookie($uri, $cookie_name, $ret_as = self::COOKIE_OBJECT)
00223     {
00224         if (is_string($uri)) {
00225             $uri = Zend_Uri::factory($uri);
00226         }
00227 
00228         if (! $uri instanceof Zend_Uri_Http) {
00229             require_once 'Zend/Http/Exception.php';
00230             throw new Zend_Http_Exception('Invalid URI specified');
00231         }
00232 
00233         // Get correct cookie path
00234         $path = $uri->getPath();
00235         $path = substr($path, 0, strrpos($path, '/'));
00236         if (! $path) $path = '/';
00237 
00238         if (isset($this->cookies[$uri->getHost()][$path][$cookie_name])) {
00239             $cookie = $this->cookies[$uri->getHost()][$path][$cookie_name];
00240 
00241             switch ($ret_as) {
00242                 case self::COOKIE_OBJECT:
00243                     return $cookie;
00244                     break;
00245 
00246                 case self::COOKIE_STRING_ARRAY:
00247                 case self::COOKIE_STRING_CONCAT:
00248                     return $cookie->__toString();
00249                     break;
00250 
00251                 default:
00252                     require_once 'Zend/Http/Exception.php';
00253                     throw new Zend_Http_Exception("Invalid value passed for \$ret_as: {$ret_as}");
00254                     break;
00255             }
00256         } else {
00257             return false;
00258         }
00259     }
00260 
00269     protected function _flattenCookiesArray($ptr, $ret_as = self::COOKIE_OBJECT) {
00270         if (is_array($ptr)) {
00271             $ret = ($ret_as == self::COOKIE_STRING_CONCAT ? '' : array());
00272             foreach ($ptr as $item) {
00273                 if ($ret_as == self::COOKIE_STRING_CONCAT) {
00274                     $ret .= $this->_flattenCookiesArray($item, $ret_as);
00275                 } else {
00276                     $ret = array_merge($ret, $this->_flattenCookiesArray($item, $ret_as));
00277                 }
00278             }
00279             return $ret;
00280         } elseif ($ptr instanceof Zend_Http_Cookie) {
00281             switch ($ret_as) {
00282                 case self::COOKIE_STRING_ARRAY:
00283                     return array($ptr->__toString());
00284                     break;
00285 
00286                 case self::COOKIE_STRING_CONCAT:
00287                     return $ptr->__toString();
00288                     break;
00289 
00290                 case self::COOKIE_OBJECT:
00291                 default:
00292                     return array($ptr);
00293                     break;
00294             }
00295         }
00296 
00297         return null;
00298     }
00299 
00306     protected function _matchDomain($domain)
00307     {
00308         $ret = array();
00309 
00310         foreach (array_keys($this->cookies) as $cdom) {
00311             if (Zend_Http_Cookie::matchCookieDomain($cdom, $domain)) {
00312                 $ret[$cdom] = $this->cookies[$cdom];
00313             }
00314         }
00315 
00316         return $ret;
00317     }
00318 
00326     protected function _matchPath($domains, $path)
00327     {
00328         $ret = array();
00329 
00330         foreach ($domains as $dom => $paths_array) {
00331             foreach (array_keys($paths_array) as $cpath) {
00332                 if (Zend_Http_Cookie::matchCookiePath($cpath, $path)) {
00333                     if (! isset($ret[$dom])) {
00334                         $ret[$dom] = array();
00335                     }
00336 
00337                     $ret[$dom][$cpath] = $paths_array[$cpath];
00338                 }
00339             }
00340         }
00341 
00342         return $ret;
00343     }
00344 
00356     public static function fromResponse(Zend_Http_Response $response, $ref_uri)
00357     {
00358         $jar = new self();
00359         $jar->addCookiesFromResponse($response, $ref_uri);
00360         return $jar;
00361     }
00362 
00368     public function count()
00369     {
00370         return count($this->_rawCookies);
00371     }
00372 
00378     public function getIterator()
00379     {
00380         return new ArrayIterator($this->_rawCookies);
00381     }
00382 
00388     public function isEmpty()
00389     {
00390         return count($this) == 0;
00391     }
00392 
00398     public function reset()
00399     {
00400         $this->cookies = $this->_rawCookies = array();
00401         return $this;
00402     }
00403 }
 All Data Structures Namespaces Files Functions Variables Enumerations