SetCookie.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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 Header
  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_Http_Header_Exception_InvalidArgumentException
  24. */
  25. #require_once "Zend/Http/Header/Exception/InvalidArgumentException.php";
  26. /**
  27. * @see Zend_Http_Header_Exception_RuntimeException
  28. */
  29. #require_once "Zend/Http/Header/Exception/RuntimeException.php";
  30. /**
  31. * @see Zend_Http_Header_HeaderValue
  32. */
  33. #require_once "Zend/Http/Header/HeaderValue.php";
  34. /**
  35. * Zend_Http_Client is an implementation of an HTTP client in PHP. The client
  36. * supports basic features like sending different HTTP requests and handling
  37. * redirections, as well as more advanced features like proxy settings, HTTP
  38. * authentication and cookie persistence (using a Zend_Http_CookieJar object)
  39. *
  40. * @todo Implement proxy settings
  41. * @category Zend
  42. * @package Zend_Http
  43. * @subpackage Header
  44. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  45. * @license http://framework.zend.com/license/new-bsd New BSD License
  46. */
  47. class Zend_Http_Header_SetCookie
  48. {
  49. /**
  50. * Cookie name
  51. *
  52. * @var string
  53. */
  54. protected $name = null;
  55. /**
  56. * Cookie value
  57. *
  58. * @var string
  59. */
  60. protected $value = null;
  61. /**
  62. * Version
  63. *
  64. * @var integer
  65. */
  66. protected $version = null;
  67. /**
  68. * Max Age
  69. *
  70. * @var integer
  71. */
  72. protected $maxAge = null;
  73. /**
  74. * Cookie expiry date
  75. *
  76. * @var int
  77. */
  78. protected $expires = null;
  79. /**
  80. * Cookie domain
  81. *
  82. * @var string
  83. */
  84. protected $domain = null;
  85. /**
  86. * Cookie path
  87. *
  88. * @var string
  89. */
  90. protected $path = null;
  91. /**
  92. * Whether the cookie is secure or not
  93. *
  94. * @var boolean
  95. */
  96. protected $secure = null;
  97. /**
  98. * @var true
  99. */
  100. protected $httponly = null;
  101. /**
  102. * Generate a new Cookie object from a cookie string
  103. * (for example the value of the Set-Cookie HTTP header)
  104. *
  105. * @static
  106. * @throws Zend_Http_Header_Exception_InvalidArgumentException
  107. * @param $headerLine
  108. * @param bool $bypassHeaderFieldName
  109. * @return array|SetCookie
  110. */
  111. public static function fromString($headerLine, $bypassHeaderFieldName = false)
  112. {
  113. list($name, $value) = explode(': ', $headerLine, 2);
  114. // check to ensure proper header type for this factory
  115. if (strtolower($name) !== 'set-cookie') {
  116. throw new Zend_Http_Header_Exception_InvalidArgumentException('Invalid header line for Set-Cookie string: "' . $name . '"');
  117. }
  118. $multipleHeaders = preg_split('#(?<!Sun|Mon|Tue|Wed|Thu|Fri|Sat),\s*#', $value);
  119. $headers = array();
  120. foreach ($multipleHeaders as $headerLine) {
  121. $header = new self();
  122. $keyValuePairs = preg_split('#;\s*#', $headerLine);
  123. foreach ($keyValuePairs as $keyValue) {
  124. if (strpos($keyValue, '=')) {
  125. list($headerKey, $headerValue) = preg_split('#=\s*#', $keyValue, 2);
  126. } else {
  127. $headerKey = $keyValue;
  128. $headerValue = null;
  129. }
  130. // First K=V pair is always the cookie name and value
  131. if ($header->getName() === NULL) {
  132. $header->setName($headerKey);
  133. $header->setValue($headerValue);
  134. continue;
  135. }
  136. // Process the remanining elements
  137. switch (str_replace(array('-', '_'), '', strtolower($headerKey))) {
  138. case 'expires' : $header->setExpires($headerValue); break;
  139. case 'domain' : $header->setDomain($headerValue); break;
  140. case 'path' : $header->setPath($headerValue); break;
  141. case 'secure' : $header->setSecure(true); break;
  142. case 'httponly': $header->setHttponly(true); break;
  143. case 'version' : $header->setVersion((int) $headerValue); break;
  144. case 'maxage' : $header->setMaxAge((int) $headerValue); break;
  145. default:
  146. // Intentionally omitted
  147. }
  148. }
  149. $headers[] = $header;
  150. }
  151. return count($headers) == 1 ? array_pop($headers) : $headers;
  152. }
  153. /**
  154. * Cookie object constructor
  155. *
  156. * @todo Add validation of each one of the parameters (legal domain, etc.)
  157. *
  158. * @param string $name
  159. * @param string $value
  160. * @param int $expires
  161. * @param string $path
  162. * @param string $domain
  163. * @param bool $secure
  164. * @param bool $httponly
  165. * @param string $maxAge
  166. * @param int $version
  167. * @return SetCookie
  168. */
  169. public function __construct($name = null, $value = null, $expires = null, $path = null, $domain = null, $secure = false, $httponly = false, $maxAge = null, $version = null)
  170. {
  171. $this->type = 'Cookie';
  172. if ($name) {
  173. $this->setName($name);
  174. }
  175. if ($value) {
  176. $this->setValue($value); // in parent
  177. }
  178. if ($version) {
  179. $this->setVersion($version);
  180. }
  181. if ($maxAge) {
  182. $this->setMaxAge($maxAge);
  183. }
  184. if ($domain) {
  185. $this->setDomain($domain);
  186. }
  187. if ($expires) {
  188. $this->setExpires($expires);
  189. }
  190. if ($path) {
  191. $this->setPath($path);
  192. }
  193. if ($secure) {
  194. $this->setSecure($secure);
  195. }
  196. if ($httponly) {
  197. $this->setHttponly($httponly);
  198. }
  199. }
  200. /**
  201. * @return string 'Set-Cookie'
  202. */
  203. public function getFieldName()
  204. {
  205. return 'Set-Cookie';
  206. }
  207. /**
  208. * @throws Zend_Http_Header_Exception_RuntimeException
  209. * @return string
  210. */
  211. public function getFieldValue()
  212. {
  213. if ($this->getName() == '') {
  214. throw new Zend_Http_Header_Exception_RuntimeException('A cookie name is required to generate a field value for this cookie');
  215. }
  216. $value = $this->getValue();
  217. if (strpos($value,'"')!==false) {
  218. $value = '"'.urlencode(str_replace('"', '', $value)).'"';
  219. } else {
  220. $value = urlencode($value);
  221. }
  222. $fieldValue = $this->getName() . '=' . $value;
  223. $version = $this->getVersion();
  224. if ($version!==null) {
  225. $fieldValue .= '; Version=' . $version;
  226. }
  227. $maxAge = $this->getMaxAge();
  228. if ($maxAge!==null) {
  229. $fieldValue .= '; Max-Age=' . $maxAge;
  230. }
  231. $expires = $this->getExpires();
  232. if ($expires) {
  233. $fieldValue .= '; Expires=' . $expires;
  234. }
  235. $domain = $this->getDomain();
  236. if ($domain) {
  237. $fieldValue .= '; Domain=' . $domain;
  238. }
  239. $path = $this->getPath();
  240. if ($path) {
  241. $fieldValue .= '; Path=' . $path;
  242. }
  243. if ($this->isSecure()) {
  244. $fieldValue .= '; Secure';
  245. }
  246. if ($this->isHttponly()) {
  247. $fieldValue .= '; HttpOnly';
  248. }
  249. return $fieldValue;
  250. }
  251. /**
  252. * @param string $name
  253. * @return SetCookie
  254. */
  255. public function setName($name)
  256. {
  257. if (preg_match("/[=,; \t\r\n\013\014]/", $name)) {
  258. throw new Zend_Http_Header_Exception_InvalidArgumentException("Cookie name cannot contain these characters: =,; \\t\\r\\n\\013\\014 ({$name})");
  259. }
  260. $this->name = $name;
  261. return $this;
  262. }
  263. /**
  264. * @return string
  265. */
  266. public function getName()
  267. {
  268. return $this->name;
  269. }
  270. /**
  271. * @param string $value
  272. */
  273. public function setValue($value)
  274. {
  275. Zend_Http_Header_HeaderValue::assertValid($value);
  276. $this->value = $value;
  277. return $this;
  278. }
  279. /**
  280. * @return string
  281. */
  282. public function getValue()
  283. {
  284. return $this->value;
  285. }
  286. /**
  287. * Set version
  288. *
  289. * @param integer $version
  290. */
  291. public function setVersion($version)
  292. {
  293. if (!is_int($version)) {
  294. throw new Zend_Http_Header_Exception_InvalidArgumentException('Invalid Version number specified');
  295. }
  296. $this->version = $version;
  297. }
  298. /**
  299. * Get version
  300. *
  301. * @return integer
  302. */
  303. public function getVersion()
  304. {
  305. return $this->version;
  306. }
  307. /**
  308. * Set Max-Age
  309. *
  310. * @param integer $maxAge
  311. */
  312. public function setMaxAge($maxAge)
  313. {
  314. if (!is_int($maxAge) || ($maxAge<0)) {
  315. throw new Zend_Http_Header_Exception_InvalidArgumentException('Invalid Max-Age number specified');
  316. }
  317. $this->maxAge = $maxAge;
  318. }
  319. /**
  320. * Get Max-Age
  321. *
  322. * @return integer
  323. */
  324. public function getMaxAge()
  325. {
  326. return $this->maxAge;
  327. }
  328. /**
  329. * @param int $expires
  330. * @return SetCookie
  331. */
  332. public function setExpires($expires)
  333. {
  334. if (!empty($expires)) {
  335. if (is_string($expires)) {
  336. $expires = strtotime($expires);
  337. } elseif (!is_int($expires)) {
  338. throw new Zend_Http_Header_Exception_InvalidArgumentException('Invalid expires time specified');
  339. }
  340. $this->expires = (int) $expires;
  341. }
  342. return $this;
  343. }
  344. /**
  345. * @return int
  346. */
  347. public function getExpires($inSeconds = false)
  348. {
  349. if ($this->expires == null) {
  350. return;
  351. }
  352. if ($inSeconds) {
  353. return $this->expires;
  354. }
  355. return gmdate('D, d-M-Y H:i:s', $this->expires) . ' GMT';
  356. }
  357. /**
  358. * @param string $domain
  359. */
  360. public function setDomain($domain)
  361. {
  362. Zend_Http_Header_HeaderValue::assertValid($domain);
  363. $this->domain = $domain;
  364. return $this;
  365. }
  366. /**
  367. * @return string
  368. */
  369. public function getDomain()
  370. {
  371. return $this->domain;
  372. }
  373. /**
  374. * @param string $path
  375. */
  376. public function setPath($path)
  377. {
  378. Zend_Http_Header_HeaderValue::assertValid($path);
  379. $this->path = $path;
  380. return $this;
  381. }
  382. /**
  383. * @return string
  384. */
  385. public function getPath()
  386. {
  387. return $this->path;
  388. }
  389. /**
  390. * @param boolean $secure
  391. */
  392. public function setSecure($secure)
  393. {
  394. $this->secure = $secure;
  395. return $this;
  396. }
  397. /**
  398. * @return boolean
  399. */
  400. public function isSecure()
  401. {
  402. return $this->secure;
  403. }
  404. /**
  405. * @param bool $httponly
  406. */
  407. public function setHttponly($httponly)
  408. {
  409. $this->httponly = $httponly;
  410. return $this;
  411. }
  412. /**
  413. * @return bool
  414. */
  415. public function isHttponly()
  416. {
  417. return $this->httponly;
  418. }
  419. /**
  420. * Check whether the cookie has expired
  421. *
  422. * Always returns false if the cookie is a session cookie (has no expiry time)
  423. *
  424. * @param int $now Timestamp to consider as "now"
  425. * @return boolean
  426. */
  427. public function isExpired($now = null)
  428. {
  429. if ($now === null) {
  430. $now = time();
  431. }
  432. if (is_int($this->expires) && $this->expires < $now) {
  433. return true;
  434. } else {
  435. return false;
  436. }
  437. }
  438. /**
  439. * Check whether the cookie is a session cookie (has no expiry time set)
  440. *
  441. * @return boolean
  442. */
  443. public function isSessionCookie()
  444. {
  445. return ($this->expires === null);
  446. }
  447. public function isValidForRequest($requestDomain, $path, $isSecure = false)
  448. {
  449. if ($this->getDomain() && (strrpos($requestDomain, $this->getDomain()) !== false)) {
  450. return false;
  451. }
  452. if ($this->getPath() && (strpos($path, $this->getPath()) !== 0)) {
  453. return false;
  454. }
  455. if ($this->secure && $this->isSecure()!==$isSecure) {
  456. return false;
  457. }
  458. return true;
  459. }
  460. public function toString()
  461. {
  462. return $this->getFieldName() . ': ' . $this->getFieldValue();
  463. }
  464. public function __toString()
  465. {
  466. return $this->toString();
  467. }
  468. public function toStringMultipleHeaders(array $headers)
  469. {
  470. $headerLine = $this->toString();
  471. /* @var $header SetCookie */
  472. foreach ($headers as $header) {
  473. if (!$header instanceof Zend_Http_Header_SetCookie) {
  474. throw new Zend_Http_Header_Exception_RuntimeException(
  475. 'The SetCookie multiple header implementation can only accept an array of SetCookie headers'
  476. );
  477. }
  478. $headerLine .= ', ' . $header->getFieldValue();
  479. }
  480. return $headerLine;
  481. }
  482. }