PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Cake/Controller/Component/CookieComponent.php

https://bitbucket.org/gowthami_gk/inventory_management
PHP | 534 lines | 224 code | 39 blank | 271 comment | 39 complexity | 1b2777b1d5fa8b073999c39bbe70f4e4 MD5 | raw file
  1. <?php
  2. /**
  3. * Cookie Component
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice.
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  13. * @link https://cakephp.org CakePHP(tm) Project
  14. * @package Cake.Controller.Component
  15. * @since CakePHP(tm) v 1.2.0.4213
  16. * @license https://opensource.org/licenses/mit-license.php MIT License
  17. */
  18. App::uses('Component', 'Controller');
  19. App::uses('Security', 'Utility');
  20. App::uses('Hash', 'Utility');
  21. /**
  22. * Cookie Component.
  23. *
  24. * Cookie handling for the controller.
  25. *
  26. * @package Cake.Controller.Component
  27. * @link https://book.cakephp.org/2.0/en/core-libraries/components/cookie.html
  28. */
  29. class CookieComponent extends Component {
  30. /**
  31. * The name of the cookie.
  32. *
  33. * Overridden with the controller beforeFilter();
  34. * $this->Cookie->name = 'CookieName';
  35. *
  36. * @var string
  37. */
  38. public $name = 'CakeCookie';
  39. /**
  40. * The time a cookie will remain valid.
  41. *
  42. * Can be either integer Unix timestamp or a date string.
  43. *
  44. * Overridden with the controller beforeFilter();
  45. * $this->Cookie->time = '5 Days';
  46. *
  47. * @var mixed
  48. */
  49. public $time = null;
  50. /**
  51. * Cookie path.
  52. *
  53. * Overridden with the controller beforeFilter();
  54. * $this->Cookie->path = '/';
  55. *
  56. * The path on the server in which the cookie will be available on.
  57. * If public $cookiePath is set to '/foo/', the cookie will only be available
  58. * within the /foo/ directory and all sub-directories such as /foo/bar/ of domain.
  59. * The default value is the entire domain.
  60. *
  61. * @var string
  62. */
  63. public $path = '/';
  64. /**
  65. * Domain path.
  66. *
  67. * The domain that the cookie is available.
  68. *
  69. * Overridden with the controller beforeFilter();
  70. * $this->Cookie->domain = '.example.com';
  71. *
  72. * To make the cookie available on all subdomains of example.com.
  73. * Set $this->Cookie->domain = '.example.com'; in your controller beforeFilter
  74. *
  75. * @var string
  76. */
  77. public $domain = '';
  78. /**
  79. * Secure HTTPS only cookie.
  80. *
  81. * Overridden with the controller beforeFilter();
  82. * $this->Cookie->secure = true;
  83. *
  84. * Indicates that the cookie should only be transmitted over a secure HTTPS connection.
  85. * When set to true, the cookie will only be set if a secure connection exists.
  86. *
  87. * @var bool
  88. */
  89. public $secure = false;
  90. /**
  91. * Encryption key.
  92. *
  93. * Overridden with the controller beforeFilter();
  94. * $this->Cookie->key = 'SomeRandomString';
  95. *
  96. * @var string
  97. */
  98. public $key = null;
  99. /**
  100. * HTTP only cookie
  101. *
  102. * Set to true to make HTTP only cookies. Cookies that are HTTP only
  103. * are not accessible in JavaScript.
  104. *
  105. * @var bool
  106. */
  107. public $httpOnly = false;
  108. /**
  109. * Values stored in the cookie.
  110. *
  111. * Accessed in the controller using $this->Cookie->read('Name.key');
  112. *
  113. * @see CookieComponent::read();
  114. * @var string
  115. */
  116. protected $_values = array();
  117. /**
  118. * Type of encryption to use.
  119. *
  120. * Currently two methods are available: cipher and rijndael
  121. * Defaults to Security::cipher(). Cipher is horribly insecure and only
  122. * the default because of backwards compatibility. In new applications you should
  123. * always change this to 'aes' or 'rijndael'.
  124. *
  125. * @var string
  126. */
  127. protected $_type = 'cipher';
  128. /**
  129. * Used to reset cookie time if $expire is passed to CookieComponent::write()
  130. *
  131. * @var string
  132. */
  133. protected $_reset = null;
  134. /**
  135. * Expire time of the cookie
  136. *
  137. * This is controlled by CookieComponent::time;
  138. *
  139. * @var string
  140. */
  141. protected $_expires = 0;
  142. /**
  143. * A reference to the Controller's CakeResponse object
  144. *
  145. * @var CakeResponse
  146. */
  147. protected $_response = null;
  148. /**
  149. * Constructor
  150. *
  151. * @param ComponentCollection $collection A ComponentCollection for this component
  152. * @param array $settings Array of settings.
  153. */
  154. public function __construct(ComponentCollection $collection, $settings = array()) {
  155. $this->key = Configure::read('Security.salt');
  156. parent::__construct($collection, $settings);
  157. if (isset($this->time)) {
  158. $this->_expire($this->time);
  159. }
  160. $controller = $collection->getController();
  161. if ($controller && isset($controller->response)) {
  162. $this->_response = $controller->response;
  163. } else {
  164. $this->_response = new CakeResponse();
  165. }
  166. }
  167. /**
  168. * Start CookieComponent for use in the controller
  169. *
  170. * @param Controller $controller Controller instance.
  171. * @return void
  172. */
  173. public function startup(Controller $controller) {
  174. $this->_expire($this->time);
  175. $this->_values[$this->name] = array();
  176. }
  177. /**
  178. * Write a value to the $_COOKIE[$key];
  179. *
  180. * Optional [Name.], required key, optional $value, optional $encrypt, optional $expires
  181. * $this->Cookie->write('[Name.]key, $value);
  182. *
  183. * By default all values are encrypted.
  184. * You must pass $encrypt false to store values in clear test
  185. *
  186. * You must use this method before any output is sent to the browser.
  187. * Failure to do so will result in header already sent errors.
  188. *
  189. * @param string|array $key Key for the value
  190. * @param mixed $value Value
  191. * @param bool $encrypt Set to true to encrypt value, false otherwise
  192. * @param int|string $expires Can be either the number of seconds until a cookie
  193. * expires, or a strtotime compatible time offset.
  194. * @return void
  195. * @link https://book.cakephp.org/2.0/en/core-libraries/components/cookie.html#CookieComponent::write
  196. */
  197. public function write($key, $value = null, $encrypt = true, $expires = null) {
  198. if (empty($this->_values[$this->name])) {
  199. $this->read();
  200. }
  201. if ($encrypt === null) {
  202. $encrypt = true;
  203. }
  204. $this->_encrypted = $encrypt;
  205. $this->_expire($expires);
  206. if (!is_array($key)) {
  207. $key = array($key => $value);
  208. }
  209. foreach ($key as $name => $value) {
  210. if (strpos($name, '.') !== false) {
  211. $this->_values[$this->name] = Hash::insert($this->_values[$this->name], $name, $value);
  212. list($name) = explode('.', $name, 2);
  213. $value = $this->_values[$this->name][$name];
  214. } else {
  215. $this->_values[$this->name][$name] = $value;
  216. }
  217. $this->_write('[' . $name . ']', $value);
  218. }
  219. $this->_encrypted = true;
  220. }
  221. /**
  222. * Read the value of the $_COOKIE[$key];
  223. *
  224. * Optional [Name.], required key
  225. * $this->Cookie->read(Name.key);
  226. *
  227. * @param string $key Key of the value to be obtained. If none specified, obtain map key => values
  228. * @return string|null Value for specified key
  229. * @link https://book.cakephp.org/2.0/en/core-libraries/components/cookie.html#CookieComponent::read
  230. */
  231. public function read($key = null) {
  232. if (empty($this->_values[$this->name]) && isset($_COOKIE[$this->name])) {
  233. $this->_values[$this->name] = $this->_decrypt($_COOKIE[$this->name]);
  234. }
  235. if (empty($this->_values[$this->name])) {
  236. $this->_values[$this->name] = array();
  237. }
  238. if ($key === null) {
  239. return $this->_values[$this->name];
  240. }
  241. return Hash::get($this->_values[$this->name], $key);
  242. }
  243. /**
  244. * Returns true if given variable is set in cookie.
  245. *
  246. * @param string $key Variable name to check for
  247. * @return bool True if variable is there
  248. */
  249. public function check($key = null) {
  250. if (empty($key)) {
  251. return false;
  252. }
  253. return $this->read($key) !== null;
  254. }
  255. /**
  256. * Delete a cookie value
  257. *
  258. * Optional [Name.], required key
  259. * $this->Cookie->delete('Name.key);
  260. *
  261. * You must use this method before any output is sent to the browser.
  262. * Failure to do so will result in header already sent errors.
  263. *
  264. * This method will delete both the top level and 2nd level cookies set.
  265. * For example assuming that $name = App, deleting `User` will delete
  266. * both `App[User]` and any other cookie values like `App[User][email]`
  267. * This is done to clean up cookie storage from before 2.4.3, where cookies
  268. * were stored inconsistently.
  269. *
  270. * @param string $key Key of the value to be deleted
  271. * @return void
  272. * @link https://book.cakephp.org/2.0/en/core-libraries/components/cookie.html#CookieComponent::delete
  273. */
  274. public function delete($key) {
  275. if (empty($this->_values[$this->name])) {
  276. $this->read();
  277. }
  278. if (strpos($key, '.') === false) {
  279. unset($this->_values[$this->name][$key]);
  280. $this->_delete('[' . $key . ']');
  281. } else {
  282. $this->_values[$this->name] = Hash::remove((array)$this->_values[$this->name], $key);
  283. list($key) = explode('.', $key, 2);
  284. if (isset($this->_values[$this->name][$key])) {
  285. $value = $this->_values[$this->name][$key];
  286. $this->_write('[' . $key . ']', $value);
  287. }
  288. }
  289. }
  290. /**
  291. * Destroy current cookie
  292. *
  293. * You must use this method before any output is sent to the browser.
  294. * Failure to do so will result in header already sent errors.
  295. *
  296. * @return void
  297. * @link https://book.cakephp.org/2.0/en/core-libraries/components/cookie.html#CookieComponent::destroy
  298. */
  299. public function destroy() {
  300. if (isset($_COOKIE[$this->name])) {
  301. $this->_values[$this->name] = $this->_decrypt($_COOKIE[$this->name]);
  302. }
  303. foreach ($this->_values[$this->name] as $name => $value) {
  304. $this->delete($name);
  305. }
  306. }
  307. /**
  308. * Will allow overriding default encryption method. Use this method
  309. * in ex: AppController::beforeFilter() before you have read or
  310. * written any cookies.
  311. *
  312. * @param string $type Encryption method
  313. * @return void
  314. */
  315. public function type($type = 'cipher') {
  316. $availableTypes = array(
  317. 'cipher',
  318. 'rijndael',
  319. 'aes'
  320. );
  321. if (!in_array($type, $availableTypes)) {
  322. trigger_error(__d('cake_dev', 'You must use cipher, rijndael or aes for cookie encryption type'), E_USER_WARNING);
  323. $type = 'cipher';
  324. }
  325. $this->_type = $type;
  326. }
  327. /**
  328. * Set the expire time for a session variable.
  329. *
  330. * Creates a new expire time for a session variable.
  331. * $expire can be either integer Unix timestamp or a date string.
  332. *
  333. * Used by write()
  334. * CookieComponent::write(string, string, boolean, 8400);
  335. * CookieComponent::write(string, string, boolean, '5 Days');
  336. *
  337. * @param int|string $expires Can be either Unix timestamp, or date string
  338. * @return int Unix timestamp
  339. */
  340. protected function _expire($expires = null) {
  341. if ($expires === null) {
  342. return $this->_expires;
  343. }
  344. $this->_reset = $this->_expires;
  345. if (!$expires) {
  346. return $this->_expires = 0;
  347. }
  348. $now = new DateTime();
  349. if (is_int($expires) || is_numeric($expires)) {
  350. return $this->_expires = $now->format('U') + (int)$expires;
  351. }
  352. $now->modify($expires);
  353. return $this->_expires = $now->format('U');
  354. }
  355. /**
  356. * Set cookie
  357. *
  358. * @param string $name Name for cookie
  359. * @param string $value Value for cookie
  360. * @return void
  361. */
  362. protected function _write($name, $value) {
  363. $this->_response->cookie(array(
  364. 'name' => $this->name . $name,
  365. 'value' => $this->_encrypt($value),
  366. 'expire' => $this->_expires,
  367. 'path' => $this->path,
  368. 'domain' => $this->domain,
  369. 'secure' => $this->secure,
  370. 'httpOnly' => $this->httpOnly
  371. ));
  372. if (!empty($this->_reset)) {
  373. $this->_expires = $this->_reset;
  374. $this->_reset = null;
  375. }
  376. }
  377. /**
  378. * Sets a cookie expire time to remove cookie value
  379. *
  380. * @param string $name Name of cookie
  381. * @return void
  382. */
  383. protected function _delete($name) {
  384. $this->_response->cookie(array(
  385. 'name' => $this->name . $name,
  386. 'value' => '',
  387. 'expire' => time() - 42000,
  388. 'path' => $this->path,
  389. 'domain' => $this->domain,
  390. 'secure' => $this->secure,
  391. 'httpOnly' => $this->httpOnly
  392. ));
  393. }
  394. /**
  395. * Encrypts $value using public $type method in Security class
  396. *
  397. * @param string $value Value to encrypt
  398. * @return string Encoded values
  399. */
  400. protected function _encrypt($value) {
  401. if (is_array($value)) {
  402. $value = $this->_implode($value);
  403. }
  404. if (!$this->_encrypted) {
  405. return $value;
  406. }
  407. $prefix = "Q2FrZQ==.";
  408. if ($this->_type === 'rijndael') {
  409. $cipher = Security::rijndael($value, $this->key, 'encrypt');
  410. }
  411. if ($this->_type === 'cipher') {
  412. $cipher = Security::cipher($value, $this->key);
  413. }
  414. if ($this->_type === 'aes') {
  415. $cipher = Security::encrypt($value, $this->key);
  416. }
  417. return $prefix . base64_encode($cipher);
  418. }
  419. /**
  420. * Decrypts $value using public $type method in Security class
  421. *
  422. * @param array $values Values to decrypt
  423. * @return array decrypted string
  424. */
  425. protected function _decrypt($values) {
  426. $decrypted = array();
  427. $type = $this->_type;
  428. foreach ((array)$values as $name => $value) {
  429. if (is_array($value)) {
  430. foreach ($value as $key => $val) {
  431. $decrypted[$name][$key] = $this->_decode($val);
  432. }
  433. } else {
  434. $decrypted[$name] = $this->_decode($value);
  435. }
  436. }
  437. return $decrypted;
  438. }
  439. /**
  440. * Decodes and decrypts a single value.
  441. *
  442. * @param string $value The value to decode & decrypt.
  443. * @return string|array Decoded value.
  444. */
  445. protected function _decode($value) {
  446. $prefix = 'Q2FrZQ==.';
  447. $pos = strpos($value, $prefix);
  448. if ($pos === false) {
  449. return $this->_explode($value);
  450. }
  451. $value = base64_decode(substr($value, strlen($prefix)));
  452. if ($this->_type === 'rijndael') {
  453. $plain = Security::rijndael($value, $this->key, 'decrypt');
  454. }
  455. if ($this->_type === 'cipher') {
  456. $plain = Security::cipher($value, $this->key);
  457. }
  458. if ($this->_type === 'aes') {
  459. $plain = Security::decrypt($value, $this->key);
  460. }
  461. return $this->_explode($plain);
  462. }
  463. /**
  464. * Implode method to keep keys are multidimensional arrays
  465. *
  466. * @param array $array Map of key and values
  467. * @return string A json encoded string.
  468. */
  469. protected function _implode(array $array) {
  470. return json_encode($array);
  471. }
  472. /**
  473. * Explode method to return array from string set in CookieComponent::_implode()
  474. * Maintains reading backwards compatibility with 1.x CookieComponent::_implode().
  475. *
  476. * @param string $string A string containing JSON encoded data, or a bare string.
  477. * @return string|array Map of key and values
  478. */
  479. protected function _explode($string) {
  480. $first = substr($string, 0, 1);
  481. if ($first === '{' || $first === '[') {
  482. $ret = json_decode($string, true);
  483. return ($ret !== null) ? $ret : $string;
  484. }
  485. $array = array();
  486. foreach (explode(',', $string) as $pair) {
  487. $key = explode('|', $pair);
  488. if (!isset($key[1])) {
  489. return $key[0];
  490. }
  491. $array[$key[0]] = $key[1];
  492. }
  493. return $array;
  494. }
  495. }