PageRenderTime 83ms CodeModel.GetById 36ms RepoModel.GetById 3ms app.codeStats 0ms

/cake/libs/controller/components/cookie.php

https://github.com/campagsi/bisad
PHP | 471 lines | 174 code | 41 blank | 256 comment | 29 complexity | 1ebf179d91cce1cae7483b7da41d030e MD5 | raw file
  1. <?php
  2. /**
  3. * Cookie Component
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @package cake
  16. * @subpackage cake.cake.libs.controller.components
  17. * @since CakePHP(tm) v 1.2.0.4213
  18. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  19. */
  20. /**
  21. * Load Security class
  22. */
  23. App::import('Core', 'Security');
  24. /**
  25. * Cookie Component.
  26. *
  27. * Cookie handling for the controller.
  28. *
  29. * @package cake
  30. * @subpackage cake.cake.libs.controller.components
  31. * @link http://book.cakephp.org/view/1280/Cookies
  32. *
  33. */
  34. class CookieComponent extends Object {
  35. /**
  36. * The name of the cookie.
  37. *
  38. * Overridden with the controller beforeFilter();
  39. * $this->Cookie->name = 'CookieName';
  40. *
  41. * @var string
  42. * @access public
  43. */
  44. var $name = 'CakeCookie';
  45. /**
  46. * The time a cookie will remain valid.
  47. *
  48. * Can be either integer Unix timestamp or a date string.
  49. *
  50. * Overridden with the controller beforeFilter();
  51. * $this->Cookie->time = '5 Days';
  52. *
  53. * @var mixed
  54. * @access public
  55. */
  56. var $time = null;
  57. /**
  58. * Cookie path.
  59. *
  60. * Overridden with the controller beforeFilter();
  61. * $this->Cookie->path = '/';
  62. *
  63. * The path on the server in which the cookie will be available on.
  64. * If var $cookiePath is set to '/foo/', the cookie will only be available
  65. * within the /foo/ directory and all sub-directories such as /foo/bar/ of domain.
  66. * The default value is the entire domain.
  67. *
  68. * @var string
  69. * @access public
  70. */
  71. var $path = '/';
  72. /**
  73. * Domain path.
  74. *
  75. * The domain that the cookie is available.
  76. *
  77. * Overridden with the controller beforeFilter();
  78. * $this->Cookie->domain = '.example.com';
  79. *
  80. * To make the cookie available on all subdomains of example.com.
  81. * Set $this->Cookie->domain = '.example.com'; in your controller beforeFilter
  82. *
  83. * @var string
  84. * @access public
  85. */
  86. var $domain = '';
  87. /**
  88. * Secure HTTPS only cookie.
  89. *
  90. * Overridden with the controller beforeFilter();
  91. * $this->Cookie->secure = true;
  92. *
  93. * Indicates that the cookie should only be transmitted over a secure HTTPS connection.
  94. * When set to true, the cookie will only be set if a secure connection exists.
  95. *
  96. * @var boolean
  97. * @access public
  98. */
  99. var $secure = false;
  100. /**
  101. * Encryption key.
  102. *
  103. * Overridden with the controller beforeFilter();
  104. * $this->Cookie->key = 'SomeRandomString';
  105. *
  106. * @var string
  107. * @access protected
  108. */
  109. var $key = null;
  110. /**
  111. * Values stored in the cookie.
  112. *
  113. * Accessed in the controller using $this->Cookie->read('Name.key');
  114. *
  115. * @see CookieComponent::read();
  116. * @var string
  117. * @access private
  118. */
  119. var $__values = array();
  120. /**
  121. * Type of encryption to use.
  122. *
  123. * Currently only one method is available
  124. * Defaults to Security::cipher();
  125. *
  126. * @var string
  127. * @access private
  128. * @todo add additional encryption methods
  129. */
  130. var $__type = 'cipher';
  131. /**
  132. * Used to reset cookie time if $expire is passed to CookieComponent::write()
  133. *
  134. * @var string
  135. * @access private
  136. */
  137. var $__reset = null;
  138. /**
  139. * Expire time of the cookie
  140. *
  141. * This is controlled by CookieComponent::time;
  142. *
  143. * @var string
  144. * @access private
  145. */
  146. var $__expires = 0;
  147. /**
  148. * Main execution method.
  149. *
  150. * @param object $controller A reference to the instantiating controller object
  151. * @access public
  152. */
  153. function initialize(&$controller, $settings) {
  154. $this->key = Configure::read('Security.salt');
  155. $this->_set($settings);
  156. }
  157. /**
  158. * Start CookieComponent for use in the controller
  159. *
  160. * @access public
  161. */
  162. function startup() {
  163. $this->__expire($this->time);
  164. if (isset($_COOKIE[$this->name])) {
  165. $this->__values = $this->__decrypt($_COOKIE[$this->name]);
  166. }
  167. }
  168. /**
  169. * Write a value to the $_COOKIE[$key];
  170. *
  171. * Optional [Name.], required key, optional $value, optional $encrypt, optional $expires
  172. * $this->Cookie->write('[Name.]key, $value);
  173. *
  174. * By default all values are encrypted.
  175. * You must pass $encrypt false to store values in clear test
  176. *
  177. * You must use this method before any output is sent to the browser.
  178. * Failure to do so will result in header already sent errors.
  179. *
  180. * @param mixed $key Key for the value
  181. * @param mixed $value Value
  182. * @param boolean $encrypt Set to true to encrypt value, false otherwise
  183. * @param string $expires Can be either Unix timestamp, or date string
  184. * @access public
  185. */
  186. function write($key, $value = null, $encrypt = true, $expires = null) {
  187. if (is_null($encrypt)) {
  188. $encrypt = true;
  189. }
  190. $this->__encrypted = $encrypt;
  191. $this->__expire($expires);
  192. if (!is_array($key)) {
  193. $key = array($key => $value);
  194. }
  195. foreach ($key as $name => $value) {
  196. if (strpos($name, '.') === false) {
  197. $this->__values[$name] = $value;
  198. $this->__write("[$name]", $value);
  199. } else {
  200. $names = explode('.', $name, 2);
  201. if (!isset($this->__values[$names[0]])) {
  202. $this->__values[$names[0]] = array();
  203. }
  204. $this->__values[$names[0]] = Set::insert($this->__values[$names[0]], $names[1], $value);
  205. $this->__write('[' . implode('][', $names) . ']', $value);
  206. }
  207. }
  208. $this->__encrypted = true;
  209. }
  210. /**
  211. * Read the value of the $_COOKIE[$key];
  212. *
  213. * Optional [Name.], required key
  214. * $this->Cookie->read(Name.key);
  215. *
  216. * @param mixed $key Key of the value to be obtained. If none specified, obtain map key => values
  217. * @return string or null, value for specified key
  218. * @access public
  219. */
  220. function read($key = null) {
  221. if (empty($this->__values) && isset($_COOKIE[$this->name])) {
  222. $this->__values = $this->__decrypt($_COOKIE[$this->name]);
  223. }
  224. if (is_null($key)) {
  225. return $this->__values;
  226. }
  227. if (strpos($key, '.') !== false) {
  228. $names = explode('.', $key, 2);
  229. $key = $names[0];
  230. }
  231. if (!isset($this->__values[$key])) {
  232. return null;
  233. }
  234. if (!empty($names[1])) {
  235. return Set::extract($this->__values[$key], $names[1]);
  236. }
  237. return $this->__values[$key];
  238. }
  239. /**
  240. * Delete a cookie value
  241. *
  242. * Optional [Name.], required key
  243. * $this->Cookie->read('Name.key);
  244. *
  245. * You must use this method before any output is sent to the browser.
  246. * Failure to do so will result in header already sent errors.
  247. *
  248. * @param string $key Key of the value to be deleted
  249. * @return void
  250. * @access public
  251. */
  252. function delete($key) {
  253. if (empty($this->__values)) {
  254. $this->read();
  255. }
  256. if (strpos($key, '.') === false) {
  257. unset($this->__values[$key]);
  258. $this->__delete("[$key]");
  259. return;
  260. }
  261. $names = explode('.', $key, 2);
  262. $this->__values[$names[0]] = Set::remove($this->__values[$names[0]], $names[1]);
  263. $this->__delete('[' . implode('][', $names) . ']');
  264. }
  265. /**
  266. * Destroy current cookie
  267. *
  268. * You must use this method before any output is sent to the browser.
  269. * Failure to do so will result in header already sent errors.
  270. *
  271. * @return void
  272. * @access public
  273. */
  274. function destroy() {
  275. if (isset($_COOKIE[$this->name])) {
  276. $this->__values = $this->__decrypt($_COOKIE[$this->name]);
  277. }
  278. foreach ($this->__values as $name => $value) {
  279. if (is_array($value)) {
  280. foreach ($value as $key => $val) {
  281. unset($this->__values[$name][$key]);
  282. $this->__delete("[$name][$key]");
  283. }
  284. }
  285. unset($this->__values[$name]);
  286. $this->__delete("[$name]");
  287. }
  288. }
  289. /**
  290. * Will allow overriding default encryption method.
  291. *
  292. * @param string $type Encryption method
  293. * @access public
  294. * @todo NOT IMPLEMENTED
  295. */
  296. function type($type = 'cipher') {
  297. $this->__type = 'cipher';
  298. }
  299. /**
  300. * Set the expire time for a session variable.
  301. *
  302. * Creates a new expire time for a session variable.
  303. * $expire can be either integer Unix timestamp or a date string.
  304. *
  305. * Used by write()
  306. * CookieComponent::write(string, string, boolean, 8400);
  307. * CookieComponent::write(string, string, boolean, '5 Days');
  308. *
  309. * @param mixed $expires Can be either Unix timestamp, or date string
  310. * @return int Unix timestamp
  311. * @access private
  312. */
  313. function __expire($expires = null) {
  314. $now = time();
  315. if (is_null($expires)) {
  316. return $this->__expires;
  317. }
  318. $this->__reset = $this->__expires;
  319. if ($expires == 0) {
  320. return $this->__expires = 0;
  321. }
  322. if (is_integer($expires) || is_numeric($expires)) {
  323. return $this->__expires = $now + intval($expires);
  324. }
  325. return $this->__expires = strtotime($expires, $now);
  326. }
  327. /**
  328. * Set cookie
  329. *
  330. * @param string $name Name for cookie
  331. * @param string $value Value for cookie
  332. * @access private
  333. */
  334. function __write($name, $value) {
  335. setcookie($this->name . $name, $this->__encrypt($value), $this->__expires, $this->path, $this->domain, $this->secure);
  336. if (!is_null($this->__reset)) {
  337. $this->__expires = $this->__reset;
  338. $this->__reset = null;
  339. }
  340. }
  341. /**
  342. * Sets a cookie expire time to remove cookie value
  343. *
  344. * @param string $name Name of cookie
  345. * @access private
  346. */
  347. function __delete($name) {
  348. setcookie($this->name . $name, '', time() - 42000, $this->path, $this->domain, $this->secure);
  349. }
  350. /**
  351. * Encrypts $value using var $type method in Security class
  352. *
  353. * @param string $value Value to encrypt
  354. * @return string encrypted string
  355. * @access private
  356. */
  357. function __encrypt($value) {
  358. if (is_array($value)) {
  359. $value = $this->__implode($value);
  360. }
  361. if ($this->__encrypted === true) {
  362. $type = $this->__type;
  363. $value = "Q2FrZQ==." .base64_encode(Security::$type($value, $this->key));
  364. }
  365. return $value;
  366. }
  367. /**
  368. * Decrypts $value using var $type method in Security class
  369. *
  370. * @param array $values Values to decrypt
  371. * @return string decrypted string
  372. * @access private
  373. */
  374. function __decrypt($values) {
  375. $decrypted = array();
  376. $type = $this->__type;
  377. foreach ((array)$values as $name => $value) {
  378. if (is_array($value)) {
  379. foreach ($value as $key => $val) {
  380. $pos = strpos($val, 'Q2FrZQ==.');
  381. $decrypted[$name][$key] = $this->__explode($val);
  382. if ($pos !== false) {
  383. $val = substr($val, 8);
  384. $decrypted[$name][$key] = $this->__explode(Security::$type(base64_decode($val), $this->key));
  385. }
  386. }
  387. } else {
  388. $pos = strpos($value, 'Q2FrZQ==.');
  389. $decrypted[$name] = $this->__explode($value);
  390. if ($pos !== false) {
  391. $value = substr($value, 8);
  392. $decrypted[$name] = $this->__explode(Security::$type(base64_decode($value), $this->key));
  393. }
  394. }
  395. }
  396. return $decrypted;
  397. }
  398. /**
  399. * Implode method to keep keys are multidimensional arrays
  400. *
  401. * @param array $array Map of key and values
  402. * @return string String in the form key1|value1,key2|value2
  403. * @access private
  404. */
  405. function __implode($array) {
  406. $string = '';
  407. foreach ($array as $key => $value) {
  408. $string .= ',' . $key . '|' . $value;
  409. }
  410. return substr($string, 1);
  411. }
  412. /**
  413. * Explode method to return array from string set in CookieComponent::__implode()
  414. *
  415. * @param string $string String in the form key1|value1,key2|value2
  416. * @return array Map of key and values
  417. * @access private
  418. */
  419. function __explode($string) {
  420. $array = array();
  421. foreach (explode(',', $string) as $pair) {
  422. $key = explode('|', $pair);
  423. if (!isset($key[1])) {
  424. return $key[0];
  425. }
  426. $array[$key[0]] = $key[1];
  427. }
  428. return $array;
  429. }
  430. }