123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380 |
- <?php
- require_once(dirname(__FILE__) . '/url.php');
- class SimpleCookie {
- var $_host;
- var $_name;
- var $_value;
- var $_path;
- var $_expiry;
- var $_is_secure;
-
- function SimpleCookie($name, $value = false, $path = false, $expiry = false, $is_secure = false) {
- $this->_host = false;
- $this->_name = $name;
- $this->_value = $value;
- $this->_path = ($path ? $this->_fixPath($path) : "/");
- $this->_expiry = false;
- if (is_string($expiry)) {
- $this->_expiry = strtotime($expiry);
- } elseif (is_integer($expiry)) {
- $this->_expiry = $expiry;
- }
- $this->_is_secure = $is_secure;
- }
-
- function setHost($host) {
- if ($host = $this->_truncateHost($host)) {
- $this->_host = $host;
- return true;
- }
- return false;
- }
-
- function getHost() {
- return $this->_host;
- }
-
- function isValidHost($host) {
- return ($this->_truncateHost($host) === $this->getHost());
- }
-
- function _truncateHost($host) {
- $tlds = SimpleUrl::getAllTopLevelDomains();
- if (preg_match('/[a-z\-]+\.(' . $tlds . ')$/i', $host, $matches)) {
- return $matches[0];
- } elseif (preg_match('/[a-z\-]+\.[a-z\-]+\.[a-z\-]+$/i', $host, $matches)) {
- return $matches[0];
- }
- return false;
- }
-
- function getName() {
- return $this->_name;
- }
-
- function getValue() {
- return $this->_value;
- }
-
- function getPath() {
- return $this->_path;
- }
-
- function isValidPath($path) {
- return (strncmp(
- $this->_fixPath($path),
- $this->getPath(),
- strlen($this->getPath())) == 0);
- }
-
- function getExpiry() {
- if (! $this->_expiry) {
- return false;
- }
- return gmdate("D, d M Y H:i:s", $this->_expiry) . " GMT";
- }
-
- function isExpired($now) {
- if (! $this->_expiry) {
- return true;
- }
- if (is_string($now)) {
- $now = strtotime($now);
- }
- return ($this->_expiry < $now);
- }
-
- function agePrematurely($interval) {
- if ($this->_expiry) {
- $this->_expiry -= $interval;
- }
- }
-
- function isSecure() {
- return $this->_is_secure;
- }
-
- function _fixPath($path) {
- if (substr($path, 0, 1) != '/') {
- $path = '/' . $path;
- }
- if (substr($path, -1, 1) != '/') {
- $path .= '/';
- }
- return $path;
- }
- }
- class SimpleCookieJar {
- var $_cookies;
-
- function SimpleCookieJar() {
- $this->_cookies = array();
- }
-
- function restartSession($date = false) {
- $surviving_cookies = array();
- for ($i = 0; $i < count($this->_cookies); $i++) {
- if (! $this->_cookies[$i]->getValue()) {
- continue;
- }
- if (! $this->_cookies[$i]->getExpiry()) {
- continue;
- }
- if ($date && $this->_cookies[$i]->isExpired($date)) {
- continue;
- }
- $surviving_cookies[] = $this->_cookies[$i];
- }
- $this->_cookies = $surviving_cookies;
- }
-
- function agePrematurely($interval) {
- for ($i = 0; $i < count($this->_cookies); $i++) {
- $this->_cookies[$i]->agePrematurely($interval);
- }
- }
-
- function setCookie($name, $value, $host = false, $path = '/', $expiry = false) {
- $cookie = new SimpleCookie($name, $value, $path, $expiry);
- if ($host) {
- $cookie->setHost($host);
- }
- $this->_cookies[$this->_findFirstMatch($cookie)] = $cookie;
- }
-
- function _findFirstMatch($cookie) {
- for ($i = 0; $i < count($this->_cookies); $i++) {
- $is_match = $this->_isMatch(
- $cookie,
- $this->_cookies[$i]->getHost(),
- $this->_cookies[$i]->getPath(),
- $this->_cookies[$i]->getName());
- if ($is_match) {
- return $i;
- }
- }
- return count($this->_cookies);
- }
-
- function getCookieValue($host, $path, $name) {
- $longest_path = '';
- foreach ($this->_cookies as $cookie) {
- if ($this->_isMatch($cookie, $host, $path, $name)) {
- if (strlen($cookie->getPath()) > strlen($longest_path)) {
- $value = $cookie->getValue();
- $longest_path = $cookie->getPath();
- }
- }
- }
- return (isset($value) ? $value : false);
- }
-
- function _isMatch($cookie, $host, $path, $name) {
- if ($cookie->getName() != $name) {
- return false;
- }
- if ($host && $cookie->getHost() && ! $cookie->isValidHost($host)) {
- return false;
- }
- if (! $cookie->isValidPath($path)) {
- return false;
- }
- return true;
- }
-
- function selectAsPairs($url) {
- $pairs = array();
- foreach ($this->_cookies as $cookie) {
- if ($this->_isMatch($cookie, $url->getHost(), $url->getPath(), $cookie->getName())) {
- $pairs[] = $cookie->getName() . '=' . $cookie->getValue();
- }
- }
- return $pairs;
- }
- }
- ?>
|