PageRenderTime 23ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/magento/zendframework1/library/Zend/Http/CookieJar.php

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