PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/standard/tags/release-0.7.0/library/Zend/Http/Cookie.php

https://github.com/bhaumik25/zend-framework
PHP | 319 lines | 132 code | 33 blank | 154 comment | 30 complexity | ba7a3e187b5ed28d81ff82c9364745ed MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to version 1.0 of the Zend Framework
  8. * license, that is bundled with this package in the file LICENSE,
  9. * and is available through the world-wide-web at the following URL:
  10. * http://www.zend.com/license/framework/1_0.txt. If you did not
  11. * receive a copy of the Zend Framework license and are unable to
  12. * obtain it through the world-wide-web, please send a note to
  13. * license@zend.com so we can mail you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Http
  17. * @subpackage Cookie
  18. * @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com/)
  19. * @version $Id$
  20. * @license http://www.zend.com/license/framework/1_0.txt Zend Framework License version 1.0
  21. */
  22. require_once "Zend.php";
  23. require_once "Zend/Uri.php";
  24. /**
  25. * Zend_Http_Cookie is a class describing an HTTP cookie and all it's parameters.
  26. *
  27. * Zend_Http_Cookie is a class describing an HTTP cookie and all it's parameters. The
  28. * class also enables validating whether the cookie should be sent to the server in
  29. * a specified scenario according to the request URI, the expiry time and whether
  30. * session cookies should be used or not. Generally speaking cookies should be
  31. * contained in a Cookiejar object, or instantiated manually and added to an HTTP
  32. * request.
  33. *
  34. * See http://wp.netscape.com/newsref/std/cookie_spec.html for some specs.
  35. *
  36. * @category Zend
  37. * @package Zend_Http
  38. * @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com/)
  39. * @license http://www.zend.com/license/framework/1_0.txt Zend Framework License version 1.0
  40. */
  41. class Zend_Http_Cookie
  42. {
  43. /**
  44. * Cookie name
  45. *
  46. * @var string
  47. */
  48. protected $name;
  49. /**
  50. * Cookie value
  51. *
  52. * @var string
  53. */
  54. protected $value;
  55. /**
  56. * Cookie expiry date
  57. *
  58. * @var int
  59. */
  60. protected $expires;
  61. /**
  62. * Cookie domain
  63. *
  64. * @var string
  65. */
  66. protected $domain;
  67. /**
  68. * Cookie path
  69. *
  70. * @var string
  71. */
  72. protected $path;
  73. /**
  74. * Whether the cookie is secure or not
  75. *
  76. * @var boolean
  77. */
  78. protected $secure;
  79. /**
  80. * Cookie object constructor
  81. *
  82. * @todo Add validation of each one of the parameters (legal domain, etc.)
  83. *
  84. * @param string $name
  85. * @param string $value
  86. * @param int $expires
  87. * @param string $domain
  88. * @param string $path
  89. * @param bool $secure
  90. */
  91. public function __construct($name, $value, $domain, $expires = null, $path = null, $secure = false)
  92. {
  93. if (preg_match("/[=,; \t\r\n\013\014]/", $name))
  94. throw new Zend_Http_Exception("Cookie name cannot contain these characters: =,; \\t\\r\\n\\013\\014 ({$name})");
  95. if (! $this->name = (string) $name)
  96. throw new Zend_Http_Exception('Cookies must have a name');
  97. if (! $this->domain = (string) $domain)
  98. throw new Zend_Http_Exception('Cookies must have a domain');
  99. $this->value = (string) $value;
  100. $this->expires = ($expires === null ? null : (int) $expires);
  101. $this->path = ($path ? $path : '/');
  102. $this->secure = $secure;
  103. }
  104. /**
  105. * Get Cookie name
  106. *
  107. * @return string
  108. */
  109. public function getName()
  110. {
  111. return $this->name;
  112. }
  113. /**
  114. * Get cookie value
  115. *
  116. * @return string
  117. */
  118. public function getValue()
  119. {
  120. return $this->value;
  121. }
  122. /**
  123. * Get cookie domain
  124. *
  125. * @return string
  126. */
  127. public function getDomain()
  128. {
  129. return $this->domain;
  130. }
  131. /**
  132. * Get the cookie path
  133. *
  134. * @return string
  135. */
  136. public function getPath()
  137. {
  138. return $this->path;
  139. }
  140. /**
  141. * Get the expiry time of the cookie, or null if no expiry time is set
  142. *
  143. * @return int|null
  144. */
  145. public function getExpiryTime()
  146. {
  147. return $this->expires;
  148. }
  149. /**
  150. * Check whether the cookie should only be sent over secure connections
  151. *
  152. * @return boolean
  153. */
  154. public function isSecure()
  155. {
  156. return $this->secure;
  157. }
  158. /**
  159. * Check whether the cookie has expired
  160. *
  161. * Always returns false if the cookie is a session cookie (has no expiry time)
  162. *
  163. * @param int $now Timestamp to consider as "now"
  164. * @return boolean
  165. */
  166. public function isExpired($now = null)
  167. {
  168. if ($now === null) $now = time();
  169. if (is_int($this->expires) && $this->expires < $now) {
  170. return true;
  171. } else {
  172. return false;
  173. }
  174. }
  175. /**
  176. * Check whether the cookie is a session cookie (has no expiry time set)
  177. *
  178. * @return boolean
  179. */
  180. public function isSessionCookie()
  181. {
  182. return ($this->expires === null);
  183. }
  184. /**
  185. * Checks whether the cookie should be sent or not in a specific scenario
  186. *
  187. * @param string|Zend_Uri_Http $uri URI to check against (secure, domain, path)
  188. * @param boolean $matchSessionCookies Whether to send session cookies
  189. * @param int $now Override the current time when checking for expiry time
  190. * @return boolean
  191. */
  192. public function match($uri, $matchSessionCookies = true, $now = null)
  193. {
  194. if (is_string ($uri)) {
  195. $uri = Zend_Uri::factory($uri);
  196. }
  197. // Make sure we have a valid Zend_Uri_Http object
  198. if (! ($uri->valid() && ($uri->getScheme() == 'http' || $uri->getScheme() =='https')))
  199. throw new Zend_Http_Exception('Passed URI is not a valid HTTP or HTTPS URI');
  200. // Check that the cookie is secure (if required) and not expired
  201. if ($this->secure && $uri->getScheme() != 'https') return false;
  202. if ($this->isExpired($now)) return false;
  203. if ($this->isSessionCookie() && ! $matchSessionCookies) return false;
  204. // Validate domain and path
  205. // Domain is validated using tail match, while path is validated using head match
  206. $domain_preg = preg_quote($this->getDomain(), "/");
  207. if (! preg_match("/{$domain_preg}$/", $uri->getHost())) return false;
  208. $path_preg = preg_quote($this->getPath(), "/");
  209. if (! preg_match("/^{$path_preg}/", $uri->getPath())) return false;
  210. // If we didn't die until now, return true.
  211. return true;
  212. }
  213. /**
  214. * Get the cookie as a string, suitable for sending as a "Cookie" header in an
  215. * HTTP request
  216. *
  217. * @return string
  218. */
  219. public function __toString()
  220. {
  221. return $this->name . '=' . urlencode($this->value) . ';';
  222. }
  223. /**
  224. * Generate a new Cookie object from a cookie string
  225. * (for example the value of the Set-Cookie HTTP header)
  226. *
  227. * @param string $cookieStr
  228. * @param Zend_Uri_Http|string $ref_uri Reference URI for default values (domain, path)
  229. * @return Zend_Http_Cookie A new Zend_Http_Cookie object or false on failure.
  230. */
  231. static public function fromString($cookieStr, $ref_uri = null)
  232. {
  233. // Set default values
  234. if (is_string($ref_uri)) {
  235. $ref_uri = Zend_Uri::factory($ref_uri);
  236. }
  237. $name = '';
  238. $value = '';
  239. $expires = null;
  240. $domain = '';
  241. $path = '';
  242. $secure = false;
  243. $parts = explode(';', $cookieStr);
  244. // If first part does not include '=', fail
  245. if (strpos($parts[0], '=') === false) return false;
  246. // Get the name and value of the cookie
  247. list($name, $value) = explode('=', trim(array_shift($parts)), 2);
  248. $value = urldecode($value);
  249. // Set default domain and path
  250. if ($ref_uri instanceof Zend_Uri_Http) {
  251. $domain = $ref_uri->getHost();
  252. $path = $ref_uri->getPath();
  253. $path = substr($path, 0, strrpos($path, '/'));
  254. }
  255. // Set other cookie parameters
  256. foreach ($parts as $part) {
  257. $part = trim($part);
  258. if (strtolower($part) == 'secure') {
  259. $secure = true;
  260. continue;
  261. }
  262. $keyValue = explode('=', $part, 2);
  263. if (count($keyValue) == 2) {
  264. list($k, $v) = $keyValue;
  265. switch (strtolower($k)) {
  266. case 'expires':
  267. $expires = strtotime($v);
  268. break;
  269. case 'path':
  270. $path = $v;
  271. break;
  272. case 'domain':
  273. $domain = $v;
  274. break;
  275. default:
  276. break;
  277. }
  278. }
  279. }
  280. if ($name !== '') {
  281. return new Zend_Http_Cookie($name, $value, $domain, $expires, $path, $secure);
  282. } else {
  283. return false;
  284. }
  285. }
  286. }