Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/zend/Zend/Http/Cookie.php
Go to the documentation of this file.
00001 <?php
00002 
00027 require_once 'Zend/Uri/Http.php';
00028 
00029 
00047 class Zend_Http_Cookie
00048 {
00054     protected $name;
00055 
00061     protected $value;
00062 
00068     protected $expires;
00069 
00075     protected $domain;
00076 
00082     protected $path;
00083 
00089     protected $secure;
00090 
00096     protected $encodeValue;
00097 
00110     public function __construct($name, $value, $domain, $expires = null, $path = null, $secure = false)
00111     {
00112         if (preg_match("/[=,; \t\r\n\013\014]/", $name)) {
00113             require_once 'Zend/Http/Exception.php';
00114             throw new Zend_Http_Exception("Cookie name cannot contain these characters: =,; \\t\\r\\n\\013\\014 ({$name})");
00115         }
00116 
00117         if (! $this->name = (string) $name) {
00118             require_once 'Zend/Http/Exception.php';
00119             throw new Zend_Http_Exception('Cookies must have a name');
00120         }
00121 
00122         if (! $this->domain = (string) $domain) {
00123             require_once 'Zend/Http/Exception.php';
00124             throw new Zend_Http_Exception('Cookies must have a domain');
00125         }
00126 
00127         $this->value = (string) $value;
00128         $this->expires = ($expires === null ? null : (int) $expires);
00129         $this->path = ($path ? $path : '/');
00130         $this->secure = $secure;
00131     }
00132 
00138     public function getName()
00139     {
00140         return $this->name;
00141     }
00142 
00148     public function getValue()
00149     {
00150         return $this->value;
00151     }
00152 
00158     public function getDomain()
00159     {
00160         return $this->domain;
00161     }
00162 
00168     public function getPath()
00169     {
00170         return $this->path;
00171     }
00172 
00178     public function getExpiryTime()
00179     {
00180         return $this->expires;
00181     }
00182 
00188     public function isSecure()
00189     {
00190         return $this->secure;
00191     }
00192 
00201     public function isExpired($now = null)
00202     {
00203         if ($now === null) $now = time();
00204         if (is_int($this->expires) && $this->expires < $now) {
00205             return true;
00206         } else {
00207             return false;
00208         }
00209     }
00210 
00216     public function isSessionCookie()
00217     {
00218         return ($this->expires === null);
00219     }
00220 
00229     public function match($uri, $matchSessionCookies = true, $now = null)
00230     {
00231         if (is_string ($uri)) {
00232             $uri = Zend_Uri_Http::factory($uri);
00233         }
00234 
00235         // Make sure we have a valid Zend_Uri_Http object
00236         if (! ($uri->valid() && ($uri->getScheme() == 'http' || $uri->getScheme() =='https'))) {
00237             require_once 'Zend/Http/Exception.php';
00238             throw new Zend_Http_Exception('Passed URI is not a valid HTTP or HTTPS URI');
00239         }
00240 
00241         // Check that the cookie is secure (if required) and not expired
00242         if ($this->secure && $uri->getScheme() != 'https') return false;
00243         if ($this->isExpired($now)) return false;
00244         if ($this->isSessionCookie() && ! $matchSessionCookies) return false;
00245 
00246         // Check if the domain matches
00247         if (! self::matchCookieDomain($this->getDomain(), $uri->getHost())) {
00248             return false;
00249         }
00250 
00251         // Check that path matches using prefix match
00252         if (! self::matchCookiePath($this->getPath(), $uri->getPath())) {
00253             return false;
00254         }
00255 
00256         // If we didn't die until now, return true.
00257         return true;
00258     }
00259 
00266     public function __toString()
00267     {
00268         if ($this->encodeValue) {
00269             return $this->name . '=' . urlencode($this->value) . ';';
00270         }
00271         return $this->name . '=' . $this->value . ';';
00272     }
00273 
00284     public static function fromString($cookieStr, $refUri = null, $encodeValue = true)
00285     {
00286         // Set default values
00287         if (is_string($refUri)) {
00288             $refUri = Zend_Uri_Http::factory($refUri);
00289         }
00290 
00291         $name    = '';
00292         $value   = '';
00293         $domain  = '';
00294         $path    = '';
00295         $expires = null;
00296         $secure  = false;
00297         $parts   = explode(';', $cookieStr);
00298 
00299         // If first part does not include '=', fail
00300         if (strpos($parts[0], '=') === false) return false;
00301 
00302         // Get the name and value of the cookie
00303         list($name, $value) = explode('=', trim(array_shift($parts)), 2);
00304         $name  = trim($name);
00305         if ($encodeValue) {
00306             $value = urldecode(trim($value));
00307         }
00308 
00309         // Set default domain and path
00310         if ($refUri instanceof Zend_Uri_Http) {
00311             $domain = $refUri->getHost();
00312             $path = $refUri->getPath();
00313             $path = substr($path, 0, strrpos($path, '/'));
00314         }
00315 
00316         // Set other cookie parameters
00317         foreach ($parts as $part) {
00318             $part = trim($part);
00319             if (strtolower($part) == 'secure') {
00320                 $secure = true;
00321                 continue;
00322             }
00323 
00324             $keyValue = explode('=', $part, 2);
00325             if (count($keyValue) == 2) {
00326                 list($k, $v) = $keyValue;
00327                 switch (strtolower($k))    {
00328                     case 'expires':
00329                         if(($expires = strtotime($v)) === false) {
00337                             require_once 'Zend/Date.php';
00338 
00339                             $expireDate = new Zend_Date($v);
00340                             $expires = $expireDate->getTimestamp();
00341                         }
00342                         break;
00343 
00344                     case 'path':
00345                         $path = $v;
00346                         break;
00347 
00348                     case 'domain':
00349                         $domain = $v;
00350                         break;
00351 
00352                     default:
00353                         break;
00354                 }
00355             }
00356         }
00357 
00358         if ($name !== '') {
00359             $ret = new self($name, $value, $domain, $expires, $path, $secure);
00360             $ret->encodeValue = ($encodeValue) ? true : false;
00361             return $ret;
00362         } else {
00363             return false;
00364         }
00365     }
00366 
00377     public static function matchCookieDomain($cookieDomain, $host)
00378     {
00379         if (! $cookieDomain) {
00380             require_once 'Zend/Http/Exception.php';
00381             throw new Zend_Http_Exception("\$cookieDomain is expected to be a cookie domain");
00382         }
00383 
00384         if (! $host) {
00385             require_once 'Zend/Http/Exception.php';
00386             throw new Zend_Http_Exception("\$host is expected to be a host name");
00387         }
00388 
00389         $cookieDomain = strtolower($cookieDomain);
00390         $host = strtolower($host);
00391 
00392         if ($cookieDomain[0] == '.') {
00393             $cookieDomain = substr($cookieDomain, 1);
00394         }
00395 
00396         // Check for either exact match or suffix match
00397         return ($cookieDomain == $host ||
00398                 preg_match("/\.$cookieDomain$/", $host));
00399     }
00400 
00410     public static function matchCookiePath($cookiePath, $path)
00411     {
00412         if (! $cookiePath) {
00413             require_once 'Zend/Http/Exception.php';
00414             throw new Zend_Http_Exception("\$cookiePath is expected to be a cookie path");
00415         }
00416 
00417         if (! $path) {
00418             require_once 'Zend/Http/Exception.php';
00419             throw new Zend_Http_Exception("\$path is expected to be a host name");
00420         }
00421 
00422         return (strpos($path, $cookiePath) === 0);
00423     }
00424 }
 All Data Structures Namespaces Files Functions Variables Enumerations