2
0

Cookie.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Http
  17. * @subpackage Cookie
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @version $Id$
  20. * @license http://framework.zend.com/license/new-bsd New BSD License
  21. */
  22. /**
  23. * @see Zend_Uri_Http
  24. */
  25. require_once 'Zend/Uri/Http.php';
  26. /**
  27. * Zend_Http_Cookie is a class describing an HTTP cookie and all it's parameters.
  28. *
  29. * Zend_Http_Cookie is a class describing an HTTP cookie and all it's parameters. The
  30. * class also enables validating whether the cookie should be sent to the server in
  31. * a specified scenario according to the request URI, the expiry time and whether
  32. * session cookies should be used or not. Generally speaking cookies should be
  33. * contained in a Cookiejar object, or instantiated manually and added to an HTTP
  34. * request.
  35. *
  36. * See http://wp.netscape.com/newsref/std/cookie_spec.html for some specs.
  37. *
  38. * @category Zend
  39. * @package Zend_Http
  40. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  41. * @license http://framework.zend.com/license/new-bsd New BSD License
  42. */
  43. class Zend_Http_Cookie
  44. {
  45. /**
  46. * Cookie name
  47. *
  48. * @var string
  49. */
  50. protected $name;
  51. /**
  52. * Cookie value
  53. *
  54. * @var string
  55. */
  56. protected $value;
  57. /**
  58. * Cookie expiry date
  59. *
  60. * @var int
  61. */
  62. protected $expires;
  63. /**
  64. * Cookie domain
  65. *
  66. * @var string
  67. */
  68. protected $domain;
  69. /**
  70. * Cookie path
  71. *
  72. * @var string
  73. */
  74. protected $path;
  75. /**
  76. * Whether the cookie is secure or not
  77. *
  78. * @var boolean
  79. */
  80. protected $secure;
  81. /**
  82. * Cookie object constructor
  83. *
  84. * @todo Add validation of each one of the parameters (legal domain, etc.)
  85. *
  86. * @param string $name
  87. * @param string $value
  88. * @param string $domain
  89. * @param int $expires
  90. * @param string $path
  91. * @param bool $secure
  92. */
  93. public function __construct($name, $value, $domain, $expires = null, $path = null, $secure = false)
  94. {
  95. if (preg_match("/[=,; \t\r\n\013\014]/", $name)) {
  96. require_once 'Zend/Http/Exception.php';
  97. throw new Zend_Http_Exception("Cookie name cannot contain these characters: =,; \\t\\r\\n\\013\\014 ({$name})");
  98. }
  99. if (! $this->name = (string) $name) {
  100. require_once 'Zend/Http/Exception.php';
  101. throw new Zend_Http_Exception('Cookies must have a name');
  102. }
  103. if (! $this->domain = (string) $domain) {
  104. require_once 'Zend/Http/Exception.php';
  105. throw new Zend_Http_Exception('Cookies must have a domain');
  106. }
  107. $this->value = (string) $value;
  108. $this->expires = ($expires === null ? null : (int) $expires);
  109. $this->path = ($path ? $path : '/');
  110. $this->secure = $secure;
  111. }
  112. /**
  113. * Get Cookie name
  114. *
  115. * @return string
  116. */
  117. public function getName()
  118. {
  119. return $this->name;
  120. }
  121. /**
  122. * Get cookie value
  123. *
  124. * @return string
  125. */
  126. public function getValue()
  127. {
  128. return $this->value;
  129. }
  130. /**
  131. * Get cookie domain
  132. *
  133. * @return string
  134. */
  135. public function getDomain()
  136. {
  137. return $this->domain;
  138. }
  139. /**
  140. * Get the cookie path
  141. *
  142. * @return string
  143. */
  144. public function getPath()
  145. {
  146. return $this->path;
  147. }
  148. /**
  149. * Get the expiry time of the cookie, or null if no expiry time is set
  150. *
  151. * @return int|null
  152. */
  153. public function getExpiryTime()
  154. {
  155. return $this->expires;
  156. }
  157. /**
  158. * Check whether the cookie should only be sent over secure connections
  159. *
  160. * @return boolean
  161. */
  162. public function isSecure()
  163. {
  164. return $this->secure;
  165. }
  166. /**
  167. * Check whether the cookie has expired
  168. *
  169. * Always returns false if the cookie is a session cookie (has no expiry time)
  170. *
  171. * @param int $now Timestamp to consider as "now"
  172. * @return boolean
  173. */
  174. public function isExpired($now = null)
  175. {
  176. if ($now === null) $now = time();
  177. if (is_int($this->expires) && $this->expires < $now) {
  178. return true;
  179. } else {
  180. return false;
  181. }
  182. }
  183. /**
  184. * Check whether the cookie is a session cookie (has no expiry time set)
  185. *
  186. * @return boolean
  187. */
  188. public function isSessionCookie()
  189. {
  190. return ($this->expires === null);
  191. }
  192. /**
  193. * Checks whether the cookie should be sent or not in a specific scenario
  194. *
  195. * @param string|Zend_Uri_Http $uri URI to check against (secure, domain, path)
  196. * @param boolean $matchSessionCookies Whether to send session cookies
  197. * @param int $now Override the current time when checking for expiry time
  198. * @return boolean
  199. */
  200. public function match($uri, $matchSessionCookies = true, $now = null)
  201. {
  202. if (is_string ($uri)) {
  203. $uri = Zend_Uri_Http::factory($uri);
  204. }
  205. // Make sure we have a valid Zend_Uri_Http object
  206. if (! ($uri->valid() && ($uri->getScheme() == 'http' || $uri->getScheme() =='https'))) {
  207. require_once 'Zend/Http/Exception.php';
  208. throw new Zend_Http_Exception('Passed URI is not a valid HTTP or HTTPS URI');
  209. }
  210. // Check that the cookie is secure (if required) and not expired
  211. if ($this->secure && $uri->getScheme() != 'https') return false;
  212. if ($this->isExpired($now)) return false;
  213. if ($this->isSessionCookie() && ! $matchSessionCookies) return false;
  214. // Check if the domain matches
  215. if (! self::matchCookieDomain($this->getDomain(), $uri->getHost())) {
  216. return false;
  217. }
  218. // Check that path matches using prefix match
  219. if (! self::matchCookiePath($this->getPath(), $uri->getPath())) {
  220. return false;
  221. }
  222. // If we didn't die until now, return true.
  223. return true;
  224. }
  225. /**
  226. * Get the cookie as a string, suitable for sending as a "Cookie" header in an
  227. * HTTP request
  228. *
  229. * @return string
  230. */
  231. public function __toString()
  232. {
  233. return $this->name . '=' . urlencode($this->value) . ';';
  234. }
  235. /**
  236. * Generate a new Cookie object from a cookie string
  237. * (for example the value of the Set-Cookie HTTP header)
  238. *
  239. * @param string $cookieStr
  240. * @param Zend_Uri_Http|string $ref_uri Reference URI for default values (domain, path)
  241. * @return Zend_Http_Cookie A new Zend_Http_Cookie object or false on failure.
  242. */
  243. public static function fromString($cookieStr, $ref_uri = null)
  244. {
  245. // Set default values
  246. if (is_string($ref_uri)) {
  247. $ref_uri = Zend_Uri_Http::factory($ref_uri);
  248. }
  249. $name = '';
  250. $value = '';
  251. $domain = '';
  252. $path = '';
  253. $expires = null;
  254. $secure = false;
  255. $parts = explode(';', $cookieStr);
  256. // If first part does not include '=', fail
  257. if (strpos($parts[0], '=') === false) return false;
  258. // Get the name and value of the cookie
  259. list($name, $value) = explode('=', trim(array_shift($parts)), 2);
  260. $name = trim($name);
  261. $value = urldecode(trim($value));
  262. // Set default domain and path
  263. if ($ref_uri instanceof Zend_Uri_Http) {
  264. $domain = $ref_uri->getHost();
  265. $path = $ref_uri->getPath();
  266. $path = substr($path, 0, strrpos($path, '/'));
  267. }
  268. // Set other cookie parameters
  269. foreach ($parts as $part) {
  270. $part = trim($part);
  271. if (strtolower($part) == 'secure') {
  272. $secure = true;
  273. continue;
  274. }
  275. $keyValue = explode('=', $part, 2);
  276. if (count($keyValue) == 2) {
  277. list($k, $v) = $keyValue;
  278. switch (strtolower($k)) {
  279. case 'expires':
  280. if(($expires = strtotime($v)) === false) {
  281. /**
  282. * The expiration is past Tue, 19 Jan 2038 03:14:07 UTC
  283. * the maximum for 32-bit signed integer. Zend_Date
  284. * can get around that limit.
  285. *
  286. * @see Zend_Date
  287. */
  288. require_once 'Zend/Date.php';
  289. $expireDate = new Zend_Date($v);
  290. $expires = $expireDate->getTimestamp();
  291. }
  292. break;
  293. case 'path':
  294. $path = $v;
  295. break;
  296. case 'domain':
  297. $domain = $v;
  298. break;
  299. default:
  300. break;
  301. }
  302. }
  303. }
  304. if ($name !== '') {
  305. return new self($name, $value, $domain, $expires, $path, $secure);
  306. } else {
  307. return false;
  308. }
  309. }
  310. /**
  311. * Check if a cookie's domain matches a host name.
  312. *
  313. * Used by Zend_Http_Cookie and Zend_Http_CookieJar for cookie matching
  314. *
  315. * @param string $cookieDomain
  316. * @param string $host
  317. *
  318. * @return boolean
  319. */
  320. public static function matchCookieDomain($cookieDomain, $host)
  321. {
  322. if (! $cookieDomain) {
  323. require_once 'Zend/Http/Exception.php';
  324. throw new Zend_Http_Exception("\$cookieDomain is expected to be a cookie domain");
  325. }
  326. if (! $host) {
  327. require_once 'Zend/Http/Exception.php';
  328. throw new Zend_Http_Exception("\$host is expected to be a host name");
  329. }
  330. $cookieDomain = strtolower($cookieDomain);
  331. $host = strtolower($host);
  332. if ($cookieDomain[0] == '.') {
  333. $cookieDomain = substr($cookieDomain, 1);
  334. }
  335. // Check for either exact match or suffix match
  336. return ($cookieDomain == $host ||
  337. preg_match("/\.$cookieDomain$/", $host));
  338. }
  339. /**
  340. * Check if a cookie's path matches a URL path
  341. *
  342. * Used by Zend_Http_Cookie and Zend_Http_CookieJar for cookie matching
  343. *
  344. * @param string $cookiePath
  345. * @param string $path
  346. * @return boolean
  347. */
  348. public static function matchCookiePath($cookiePath, $path)
  349. {
  350. if (! $cookiePath) {
  351. require_once 'Zend/Http/Exception.php';
  352. throw new Zend_Http_Exception("\$cookiePath is expected to be a cookie path");
  353. }
  354. if (! $path) {
  355. require_once 'Zend/Http/Exception.php';
  356. throw new Zend_Http_Exception("\$path is expected to be a host name");
  357. }
  358. return (strpos($path, $cookiePath) === 0);
  359. }
  360. }