PageRenderTime 46ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/core/common/third_party/Zend/Http/Header/SetCookie.php

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