PageRenderTime 24ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/standard/tags/release-0.9.3/library/Zend/Http/CookieJar.php

https://github.com/bhaumik25/zend-framework
PHP | 341 lines | 149 code | 36 blank | 156 comment | 27 complexity | 3caf174ccddeda3520a95fb7192f15a9 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 CookieJar
  18. * @version $Id$
  19. * @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com/)
  20. * @license http://www.zend.com/license/framework/1_0.txt Zend Framework License version 1.0
  21. */
  22. require_once "Zend/Uri.php";
  23. require_once "Zend/Http/Cookie.php";
  24. require_once "Zend/Http/Response.php";
  25. /**
  26. * A Zend_Http_CookieJar object is designed to contain and maintain HTTP cookies, and should
  27. * be used along with Zend_Http_Client in order to manage cookies across HTTP requests and
  28. * responses.
  29. *
  30. * The class contains an array of Zend_Http_Cookie objects. Cookies can be added to the jar
  31. * automatically from a request or manually. Then, the jar can find and return the cookies
  32. * needed for a specific HTTP request.
  33. *
  34. * A special parameter can be passed to all methods of this class that return cookies: Cookies
  35. * can be returned either in their native form (as Zend_Http_Cookie objects) or as strings -
  36. * the later is suitable for sending as the value of the "Cookie" header in an HTTP request.
  37. * You can also choose, when returning more than one cookie, whether to get an array of strings
  38. * (by passing Zend_Http_CookieJar::COOKIE_STRING_ARRAY) or one unified string for all cookies
  39. * (by passing Zend_Http_CookieJar::COOKIE_STRING_CONCAT).
  40. *
  41. * @see http://wp.netscape.com/newsref/std/cookie_spec.html for some specs.
  42. * @category Zend
  43. * @package Zend_Http
  44. * @subpackage CookieJar
  45. * @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com/)
  46. * @license http://www.zend.com/license/framework/1_0.txt Zend Framework License version 1.0
  47. */
  48. class Zend_Http_CookieJar
  49. {
  50. /**
  51. * Return cookie(s) as a Zend_Http_Cookie object
  52. *
  53. */
  54. const COOKIE_OBJECT = 0;
  55. /**
  56. * Return cookie(s) as a string (suitable for sending in an HTTP request)
  57. *
  58. */
  59. const COOKIE_STRING_ARRAY = 1;
  60. /**
  61. * Return all cookies as one long string (suitable for sending in an HTTP request)
  62. *
  63. */
  64. const COOKIE_STRING_CONCAT = 2;
  65. /**
  66. * Array storing cookies
  67. *
  68. * Cookies are stored according to domain and path:
  69. * $cookies
  70. * + www.mydomain.com
  71. * + /
  72. * - cookie1
  73. * - cookie2
  74. * + /somepath
  75. * - othercookie
  76. * + www.otherdomain.net
  77. * + /
  78. * - alsocookie
  79. *
  80. * @var array
  81. */
  82. protected $cookies = array();
  83. /**
  84. * Construct a new CookieJar object
  85. *
  86. */
  87. public function __construct()
  88. { }
  89. /**
  90. * Add a cookie to the jar. Cookie should be passed either as a Zend_Http_Cookie object
  91. * or as a string - in which case an object is created from the string.
  92. *
  93. * @param Zend_Http_Cookie|string $cookie
  94. * @param Zend_Uri_Http|string $red_uri Optional reference URI (for domain, path, secure)
  95. */
  96. public function addCookie($cookie, $ref_uri = null)
  97. {
  98. if (is_string($cookie)) {
  99. $cookie = Zend_Http_Cookie::fromString($cookie, $ref_uri);
  100. }
  101. if ($cookie instanceof Zend_Http_Cookie) {
  102. $domain = $cookie->getDomain();
  103. $path = $cookie->getPath();
  104. if (! isset($this->cookies[$domain])) $this->cookies[$domain] = array();
  105. if (! isset($this->cookies[$domain][$path])) $this->cookies[$domain][$path] = array();
  106. $this->cookies[$domain][$path][$cookie->getName()] = $cookie;
  107. } else {
  108. throw new Zend_Http_Exception('Supplient argument is not a valid cookie string or object');
  109. }
  110. }
  111. /**
  112. * Parse an HTTP response, adding all the cookies set in that response
  113. * to the cookie jar.
  114. *
  115. * @param Zend_Http_Response $response
  116. * @param Zend_Uri_Http|string $ref_uri Requested URI
  117. */
  118. public function addCookiesFromResponse($response, $ref_uri)
  119. {
  120. if (! $response instanceof Zend_Http_Response)
  121. throw new Zend_Http_Exception('$response is expected to be a Response object, ' .
  122. gettype($response) . ' was passed');
  123. $cookie_hdrs = $response->getHeader('Set-Cookie');
  124. if (is_array($cookie_hdrs)) {
  125. foreach ($cookie_hdrs as $cookie) {
  126. $this->addCookie($cookie, $ref_uri);
  127. }
  128. } elseif (is_string($cookie_hdrs)) {
  129. $this->addCookie($cookie_hdrs, $ref_uri);
  130. }
  131. }
  132. /**
  133. * Get all cookies in the cookie jar as an array
  134. *
  135. * @param int $ret_as Whether to return cookies as objects of Zend_Http_Cookie or as strings
  136. * @return array|string
  137. */
  138. public function getAllCookies($ret_as = self::COOKIE_OBJECT)
  139. {
  140. $cookies = $this->_flattenCookiesArray($this->cookies, $ret_as);
  141. return $cookies;
  142. }
  143. /**
  144. * Return an array of all cookies matching a specific request according to the request URI,
  145. * whether session cookies should be sent or not, and the time to consider as "now" when
  146. * checking cookie expiry time.
  147. *
  148. * @param string|Zend_Uri_Http $uri URI to check against (secure, domain, path)
  149. * @param boolean $matchSessionCookies Whether to send session cookies
  150. * @param int $ret_as Whether to return cookies as objects of Zend_Http_Cookie or as strings
  151. * @param int $now Override the current time when checking for expiry time
  152. * @return array|string
  153. */
  154. public function getMatchingCookies($uri, $matchSessionCookies = true,
  155. $ret_as = self::COOKIE_OBJECT, $now = null)
  156. {
  157. if (is_string($uri)) $uri = Zend_Uri::factory($uri);
  158. if (! $uri instanceof Zend_Uri_Http)
  159. throw new Zend_Http_Exception("Invalid URI: {$uri}");
  160. // Set path
  161. $path = $uri->getPath();
  162. $path = substr($path, 0, strrpos($path, '/'));
  163. if (! $path) $path = '/';
  164. // First, reduce the array of cookies to only those matching domain and path
  165. $cookies = $this->_matchDomain($uri->getHost());
  166. $cookies = $this->_matchPath($cookies, $path);
  167. $cookies = $this->_flattenCookiesArray($cookies, self::COOKIE_OBJECT);
  168. // Next, run Cookie->match on all cookies to check secure, time and session mathcing
  169. $ret = array();
  170. foreach ($cookies as $cookie)
  171. if ($cookie->match($uri, $matchSessionCookies, $now))
  172. $ret[] = $cookie;
  173. // Now, use self::_flattenCookiesArray again - only to convert to the return format ;)
  174. $ret = $this->_flattenCookiesArray($ret, $ret_as);
  175. return $ret;
  176. }
  177. /**
  178. * Get a specific cookie according to a URI and name
  179. *
  180. * @param Zend_Uri_Http|string $uri The uri (domain and path) to match
  181. * @param string $cookie_name The cookie's name
  182. * @param int $ret_as Whether to return cookies as objects of Zend_Http_Cookie or as strings
  183. * @return Zend_Http_Cookie|string
  184. */
  185. public function getCookie($uri, $cookie_name, $ret_as = self::COOKIE_OBJECT)
  186. {
  187. if (is_string($uri)) {
  188. $uri = Zend_Uri::factory($uri);
  189. }
  190. if (! $uri instanceof Zend_Uri_Http) {
  191. throw new Zend_Http_Exception('Invalid URI specified');
  192. }
  193. // Get correct cookie path
  194. $path = $uri->getPath();
  195. $path = substr($path, 0, strrpos($path, '/'));
  196. if (! $path) $path = '/';
  197. if (isset($this->cookies[$uri->getHost()][$path][$cookie_name])) {
  198. $cookie = $this->cookies[$uri->getHost()][$path][$cookie_name];
  199. switch ($ret_as) {
  200. case self::COOKIE_OBJECT:
  201. return $cookie;
  202. break;
  203. case self::COOKIE_STRING_ARRAY:
  204. case self::COOKIE_STRING_CONCAT:
  205. return $cookie->__toString();
  206. break;
  207. default:
  208. throw new Zend_Http_Exception("Invalid value passed for \$ret_as: {$ret_as}");
  209. break;
  210. }
  211. } else {
  212. return false;
  213. }
  214. }
  215. /**
  216. * Helper function to recursivly flatten an array. Shoud be used when exporting the
  217. * cookies array (or parts of it)
  218. *
  219. * @param Zend_Http_Cookie|array $ptr
  220. * @param int $ret_as What value to return
  221. * @return array|string
  222. */
  223. protected function _flattenCookiesArray($ptr, $ret_as = self::COOKIE_OBJECT) {
  224. if (is_array($ptr)) {
  225. $ret = ($ret_as == self::COOKIE_STRING_CONCAT ? '' : array());
  226. foreach ($ptr as $item) {
  227. if ($ret_as == self::COOKIE_STRING_CONCAT) {
  228. $ret .= $this->_flattenCookiesArray($item, $ret_as);
  229. } else {
  230. $ret = array_merge($ret, $this->_flattenCookiesArray($item, $ret_as));
  231. }
  232. }
  233. return $ret;
  234. } elseif ($ptr instanceof Zend_Http_Cookie) {
  235. switch ($ret_as) {
  236. case self::COOKIE_STRING_ARRAY:
  237. return array($ptr->__toString());
  238. break;
  239. case self::COOKIE_STRING_CONCAT:
  240. return $ptr->__toString();
  241. break;
  242. case self::COOKIE_OBJECT:
  243. default:
  244. return array($ptr);
  245. break;
  246. }
  247. }
  248. return null;
  249. }
  250. /**
  251. * Return a subset of the cookies array matching a specific domain
  252. *
  253. * Returned array is actually an array of pointers to items in the $this->cookies array.
  254. *
  255. * @param string $domain
  256. * @return array
  257. */
  258. protected function _matchDomain($domain) {
  259. $ret = array();
  260. foreach (array_keys($this->cookies) as $cdom) {
  261. $regex = "/" . preg_quote($cdom, "/") . "$/i";
  262. if (preg_match($regex, $domain)) $ret[$cdom] = &$this->cookies[$cdom];
  263. }
  264. return $ret;
  265. }
  266. /**
  267. * Return a subset of a domain-matching cookies that also match a specified path
  268. *
  269. * Returned array is actually an array of pointers to items in the $passed array.
  270. *
  271. * @param array $dom_array
  272. * @param string $path
  273. * @return array
  274. */
  275. protected function _matchPath($domains, $path) {
  276. $ret = array();
  277. foreach ($domains as $dom => $paths_array) {
  278. foreach (array_keys($paths_array) as $cpath) {
  279. $regex = "|^" . preg_quote($cpath, "|") . "|i";
  280. if (preg_match($regex, $path)) {
  281. if (! isset($ret[$dom])) $ret[$dom] = array();
  282. $ret[$dom][$cpath] = &$paths_array[$cpath];
  283. }
  284. }
  285. }
  286. return $ret;
  287. }
  288. /**
  289. * Create a new CookieJar object and automatically load into it all the
  290. * cookies set in an Http_Response object. If $uri is set, it will be
  291. * considered as the requested URI for setting default domain and path
  292. * of the cookie.
  293. *
  294. * @param Zend_Http_Response $response HTTP Response object
  295. * @param Zend_Uri_Http|string $uri The requested URI
  296. * @return Zend_Http_CookieJar
  297. * @todo Add the $uri functionality.
  298. */
  299. public static function fromResponse(Zend_Http_Response $response, $ref_uri)
  300. {
  301. $jar = new Zend_Http_CookieJar();
  302. $jar->addCookiesFromResponse($response, $ref_uri);
  303. return $jar;
  304. }
  305. }