Cookie.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. // Validate domain and path
  215. // Domain is validated using tail match, while path is validated using head match
  216. $domain_preg = preg_quote($this->getDomain(), "/");
  217. if (! preg_match("/{$domain_preg}$/", $uri->getHost())) return false;
  218. $path_preg = preg_quote($this->getPath(), "/");
  219. if (! preg_match("/^{$path_preg}/", $uri->getPath())) return false;
  220. // If we didn't die until now, return true.
  221. return true;
  222. }
  223. /**
  224. * Get the cookie as a string, suitable for sending as a "Cookie" header in an
  225. * HTTP request
  226. *
  227. * @return string
  228. */
  229. public function __toString()
  230. {
  231. return $this->name . '=' . urlencode($this->value) . ';';
  232. }
  233. /**
  234. * Generate a new Cookie object from a cookie string
  235. * (for example the value of the Set-Cookie HTTP header)
  236. *
  237. * @param string $cookieStr
  238. * @param Zend_Uri_Http|string $ref_uri Reference URI for default values (domain, path)
  239. * @return Zend_Http_Cookie A new Zend_Http_Cookie object or false on failure.
  240. */
  241. public static function fromString($cookieStr, $ref_uri = null)
  242. {
  243. // Set default values
  244. if (is_string($ref_uri)) {
  245. $ref_uri = Zend_Uri_Http::factory($ref_uri);
  246. }
  247. $name = '';
  248. $value = '';
  249. $domain = '';
  250. $path = '';
  251. $expires = null;
  252. $secure = false;
  253. $parts = explode(';', $cookieStr);
  254. // If first part does not include '=', fail
  255. if (strpos($parts[0], '=') === false) return false;
  256. // Get the name and value of the cookie
  257. list($name, $value) = explode('=', trim(array_shift($parts)), 2);
  258. $name = trim($name);
  259. $value = urldecode(trim($value));
  260. // Set default domain and path
  261. if ($ref_uri instanceof Zend_Uri_Http) {
  262. $domain = $ref_uri->getHost();
  263. $path = $ref_uri->getPath();
  264. $path = substr($path, 0, strrpos($path, '/'));
  265. }
  266. // Set other cookie parameters
  267. foreach ($parts as $part) {
  268. $part = trim($part);
  269. if (strtolower($part) == 'secure') {
  270. $secure = true;
  271. continue;
  272. }
  273. $keyValue = explode('=', $part, 2);
  274. if (count($keyValue) == 2) {
  275. list($k, $v) = $keyValue;
  276. switch (strtolower($k)) {
  277. case 'expires':
  278. if(($expires = strtotime($v)) === false) {
  279. /**
  280. * The expiration is past Tue, 19 Jan 2038 03:14:07 UTC
  281. * the maximum for 32-bit signed integer. Zend_Date
  282. * can get around that limit.
  283. *
  284. * @see Zend_Date
  285. */
  286. require_once 'Zend/Date.php';
  287. $expireDate = new Zend_Date($v);
  288. $expires = $expireDate->getTimestamp();
  289. }
  290. break;
  291. case 'path':
  292. $path = $v;
  293. break;
  294. case 'domain':
  295. $domain = $v;
  296. break;
  297. default:
  298. break;
  299. }
  300. }
  301. }
  302. if ($name !== '') {
  303. return new self($name, $value, $domain, $expires, $path, $secure);
  304. } else {
  305. return false;
  306. }
  307. }
  308. }