|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00012 require_once(dirname(__FILE__) . '/url.php'); 00023 class SimpleCookie { 00024 var $_host; 00025 var $_name; 00026 var $_value; 00027 var $_path; 00028 var $_expiry; 00029 var $_is_secure; 00030 00039 function SimpleCookie($name, $value = false, $path = false, $expiry = false, $is_secure = false) { 00040 $this->_host = false; 00041 $this->_name = $name; 00042 $this->_value = $value; 00043 $this->_path = ($path ? $this->_fixPath($path) : "/"); 00044 $this->_expiry = false; 00045 if (is_string($expiry)) { 00046 $this->_expiry = strtotime($expiry); 00047 } elseif (is_integer($expiry)) { 00048 $this->_expiry = $expiry; 00049 } 00050 $this->_is_secure = $is_secure; 00051 } 00052 00063 function setHost($host) { 00064 if ($host = $this->_truncateHost($host)) { 00065 $this->_host = $host; 00066 return true; 00067 } 00068 return false; 00069 } 00070 00077 function getHost() { 00078 return $this->_host; 00079 } 00080 00087 function isValidHost($host) { 00088 return ($this->_truncateHost($host) === $this->getHost()); 00089 } 00090 00098 function _truncateHost($host) { 00099 $tlds = SimpleUrl::getAllTopLevelDomains(); 00100 if (preg_match('/[a-z\-]+\.(' . $tlds . ')$/i', $host, $matches)) { 00101 return $matches[0]; 00102 } elseif (preg_match('/[a-z\-]+\.[a-z\-]+\.[a-z\-]+$/i', $host, $matches)) { 00103 return $matches[0]; 00104 } 00105 return false; 00106 } 00107 00113 function getName() { 00114 return $this->_name; 00115 } 00116 00123 function getValue() { 00124 return $this->_value; 00125 } 00126 00132 function getPath() { 00133 return $this->_path; 00134 } 00135 00144 function isValidPath($path) { 00145 return (strncmp( 00146 $this->_fixPath($path), 00147 $this->getPath(), 00148 strlen($this->getPath())) == 0); 00149 } 00150 00156 function getExpiry() { 00157 if (! $this->_expiry) { 00158 return false; 00159 } 00160 return gmdate("D, d M Y H:i:s", $this->_expiry) . " GMT"; 00161 } 00162 00174 function isExpired($now) { 00175 if (! $this->_expiry) { 00176 return true; 00177 } 00178 if (is_string($now)) { 00179 $now = strtotime($now); 00180 } 00181 return ($this->_expiry < $now); 00182 } 00183 00190 function agePrematurely($interval) { 00191 if ($this->_expiry) { 00192 $this->_expiry -= $interval; 00193 } 00194 } 00195 00201 function isSecure() { 00202 return $this->_is_secure; 00203 } 00204 00211 function _fixPath($path) { 00212 if (substr($path, 0, 1) != '/') { 00213 $path = '/' . $path; 00214 } 00215 if (substr($path, -1, 1) != '/') { 00216 $path .= '/'; 00217 } 00218 return $path; 00219 } 00220 } 00221 00228 class SimpleCookieJar { 00229 var $_cookies; 00230 00235 function SimpleCookieJar() { 00236 $this->_cookies = array(); 00237 } 00238 00245 function restartSession($date = false) { 00246 $surviving_cookies = array(); 00247 for ($i = 0; $i < count($this->_cookies); $i++) { 00248 if (! $this->_cookies[$i]->getValue()) { 00249 continue; 00250 } 00251 if (! $this->_cookies[$i]->getExpiry()) { 00252 continue; 00253 } 00254 if ($date && $this->_cookies[$i]->isExpired($date)) { 00255 continue; 00256 } 00257 $surviving_cookies[] = $this->_cookies[$i]; 00258 } 00259 $this->_cookies = $surviving_cookies; 00260 } 00261 00270 function agePrematurely($interval) { 00271 for ($i = 0; $i < count($this->_cookies); $i++) { 00272 $this->_cookies[$i]->agePrematurely($interval); 00273 } 00274 } 00275 00286 function setCookie($name, $value, $host = false, $path = '/', $expiry = false) { 00287 $cookie = new SimpleCookie($name, $value, $path, $expiry); 00288 if ($host) { 00289 $cookie->setHost($host); 00290 } 00291 $this->_cookies[$this->_findFirstMatch($cookie)] = $cookie; 00292 } 00293 00301 function _findFirstMatch($cookie) { 00302 for ($i = 0; $i < count($this->_cookies); $i++) { 00303 $is_match = $this->_isMatch( 00304 $cookie, 00305 $this->_cookies[$i]->getHost(), 00306 $this->_cookies[$i]->getPath(), 00307 $this->_cookies[$i]->getName()); 00308 if ($is_match) { 00309 return $i; 00310 } 00311 } 00312 return count($this->_cookies); 00313 } 00314 00326 function getCookieValue($host, $path, $name) { 00327 $longest_path = ''; 00328 foreach ($this->_cookies as $cookie) { 00329 if ($this->_isMatch($cookie, $host, $path, $name)) { 00330 if (strlen($cookie->getPath()) > strlen($longest_path)) { 00331 $value = $cookie->getValue(); 00332 $longest_path = $cookie->getPath(); 00333 } 00334 } 00335 } 00336 return (isset($value) ? $value : false); 00337 } 00338 00350 function _isMatch($cookie, $host, $path, $name) { 00351 if ($cookie->getName() != $name) { 00352 return false; 00353 } 00354 if ($host && $cookie->getHost() && ! $cookie->isValidHost($host)) { 00355 return false; 00356 } 00357 if (! $cookie->isValidPath($path)) { 00358 return false; 00359 } 00360 return true; 00361 } 00362 00370 function selectAsPairs($url) { 00371 $pairs = array(); 00372 foreach ($this->_cookies as $cookie) { 00373 if ($this->_isMatch($cookie, $url->getHost(), $url->getPath(), $cookie->getName())) { 00374 $pairs[] = $cookie->getName() . '=' . $cookie->getValue(); 00375 } 00376 } 00377 return $pairs; 00378 } 00379 } 00380 ?>