PageRenderTime 26ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/cgajardo/repositorium
PHP | 474 lines | 177 code | 41 blank | 256 comment | 30 complexity | 8df51a6f3f9868aea2fd1602064dbe58 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. if (isset($this->time)) {
  157. $this->__expire($this->time);
  158. }
  159. }
  160. /**
  161. * Start CookieComponent for use in the controller
  162. *
  163. * @access public
  164. */
  165. function startup() {
  166. $this->__expire($this->time);
  167. if (isset($_COOKIE[$this->name])) {
  168. $this->__values = $this->__decrypt($_COOKIE[$this->name]);
  169. }
  170. }
  171. /**
  172. * Write a value to the $_COOKIE[$key];
  173. *
  174. * Optional [Name.], required key, optional $value, optional $encrypt, optional $expires
  175. * $this->Cookie->write('[Name.]key, $value);
  176. *
  177. * By default all values are encrypted.
  178. * You must pass $encrypt false to store values in clear test
  179. *
  180. * You must use this method before any output is sent to the browser.
  181. * Failure to do so will result in header already sent errors.
  182. *
  183. * @param mixed $key Key for the value
  184. * @param mixed $value Value
  185. * @param boolean $encrypt Set to true to encrypt value, false otherwise
  186. * @param string $expires Can be either Unix timestamp, or date string
  187. * @access public
  188. */
  189. function write($key, $value = null, $encrypt = true, $expires = null) {
  190. if (is_null($encrypt)) {
  191. $encrypt = true;
  192. }
  193. $this->__encrypted = $encrypt;
  194. $this->__expire($expires);
  195. if (!is_array($key)) {
  196. $key = array($key => $value);
  197. }
  198. foreach ($key as $name => $value) {
  199. if (strpos($name, '.') === false) {
  200. $this->__values[$name] = $value;
  201. $this->__write("[$name]", $value);
  202. } else {
  203. $names = explode('.', $name, 2);
  204. if (!isset($this->__values[$names[0]])) {
  205. $this->__values[$names[0]] = array();
  206. }
  207. $this->__values[$names[0]] = Set::insert($this->__values[$names[0]], $names[1], $value);
  208. $this->__write('[' . implode('][', $names) . ']', $value);
  209. }
  210. }
  211. $this->__encrypted = true;
  212. }
  213. /**
  214. * Read the value of the $_COOKIE[$key];
  215. *
  216. * Optional [Name.], required key
  217. * $this->Cookie->read(Name.key);
  218. *
  219. * @param mixed $key Key of the value to be obtained. If none specified, obtain map key => values
  220. * @return string or null, value for specified key
  221. * @access public
  222. */
  223. function read($key = null) {
  224. if (empty($this->__values) && isset($_COOKIE[$this->name])) {
  225. $this->__values = $this->__decrypt($_COOKIE[$this->name]);
  226. }
  227. if (is_null($key)) {
  228. return $this->__values;
  229. }
  230. if (strpos($key, '.') !== false) {
  231. $names = explode('.', $key, 2);
  232. $key = $names[0];
  233. }
  234. if (!isset($this->__values[$key])) {
  235. return null;
  236. }
  237. if (!empty($names[1])) {
  238. return Set::extract($this->__values[$key], $names[1]);
  239. }
  240. return $this->__values[$key];
  241. }
  242. /**
  243. * Delete a cookie value
  244. *
  245. * Optional [Name.], required key
  246. * $this->Cookie->read('Name.key);
  247. *
  248. * You must use this method before any output is sent to the browser.
  249. * Failure to do so will result in header already sent errors.
  250. *
  251. * @param string $key Key of the value to be deleted
  252. * @return void
  253. * @access public
  254. */
  255. function delete($key) {
  256. if (empty($this->__values)) {
  257. $this->read();
  258. }
  259. if (strpos($key, '.') === false) {
  260. unset($this->__values[$key]);
  261. $this->__delete("[$key]");
  262. return;
  263. }
  264. $names = explode('.', $key, 2);
  265. $this->__values[$names[0]] = Set::remove($this->__values[$names[0]], $names[1]);
  266. $this->__delete('[' . implode('][', $names) . ']');
  267. }
  268. /**
  269. * Destroy current cookie
  270. *
  271. * You must use this method before any output is sent to the browser.
  272. * Failure to do so will result in header already sent errors.
  273. *
  274. * @return void
  275. * @access public
  276. */
  277. function destroy() {
  278. if (isset($_COOKIE[$this->name])) {
  279. $this->__values = $this->__decrypt($_COOKIE[$this->name]);
  280. }
  281. foreach ($this->__values as $name => $value) {
  282. if (is_array($value)) {
  283. foreach ($value as $key => $val) {
  284. unset($this->__values[$name][$key]);
  285. $this->__delete("[$name][$key]");
  286. }
  287. }
  288. unset($this->__values[$name]);
  289. $this->__delete("[$name]");
  290. }
  291. }
  292. /**
  293. * Will allow overriding default encryption method.
  294. *
  295. * @param string $type Encryption method
  296. * @access public
  297. * @todo NOT IMPLEMENTED
  298. */
  299. function type($type = 'cipher') {
  300. $this->__type = 'cipher';
  301. }
  302. /**
  303. * Set the expire time for a session variable.
  304. *
  305. * Creates a new expire time for a session variable.
  306. * $expire can be either integer Unix timestamp or a date string.
  307. *
  308. * Used by write()
  309. * CookieComponent::write(string, string, boolean, 8400);
  310. * CookieComponent::write(string, string, boolean, '5 Days');
  311. *
  312. * @param mixed $expires Can be either Unix timestamp, or date string
  313. * @return int Unix timestamp
  314. * @access private
  315. */
  316. function __expire($expires = null) {
  317. $now = time();
  318. if (is_null($expires)) {
  319. return $this->__expires;
  320. }
  321. $this->__reset = $this->__expires;
  322. if ($expires == 0) {
  323. return $this->__expires = 0;
  324. }
  325. if (is_integer($expires) || is_numeric($expires)) {
  326. return $this->__expires = $now + intval($expires);
  327. }
  328. return $this->__expires = strtotime($expires, $now);
  329. }
  330. /**
  331. * Set cookie
  332. *
  333. * @param string $name Name for cookie
  334. * @param string $value Value for cookie
  335. * @access private
  336. */
  337. function __write($name, $value) {
  338. setcookie($this->name . $name, $this->__encrypt($value), $this->__expires, $this->path, $this->domain, $this->secure);
  339. if (!is_null($this->__reset)) {
  340. $this->__expires = $this->__reset;
  341. $this->__reset = null;
  342. }
  343. }
  344. /**
  345. * Sets a cookie expire time to remove cookie value
  346. *
  347. * @param string $name Name of cookie
  348. * @access private
  349. */
  350. function __delete($name) {
  351. setcookie($this->name . $name, '', time() - 42000, $this->path, $this->domain, $this->secure);
  352. }
  353. /**
  354. * Encrypts $value using var $type method in Security class
  355. *
  356. * @param string $value Value to encrypt
  357. * @return string encrypted string
  358. * @access private
  359. */
  360. function __encrypt($value) {
  361. if (is_array($value)) {
  362. $value = $this->__implode($value);
  363. }
  364. if ($this->__encrypted === true) {
  365. $type = $this->__type;
  366. $value = "Q2FrZQ==." .base64_encode(Security::$type($value, $this->key));
  367. }
  368. return $value;
  369. }
  370. /**
  371. * Decrypts $value using var $type method in Security class
  372. *
  373. * @param array $values Values to decrypt
  374. * @return string decrypted string
  375. * @access private
  376. */
  377. function __decrypt($values) {
  378. $decrypted = array();
  379. $type = $this->__type;
  380. foreach ((array)$values as $name => $value) {
  381. if (is_array($value)) {
  382. foreach ($value as $key => $val) {
  383. $pos = strpos($val, 'Q2FrZQ==.');
  384. $decrypted[$name][$key] = $this->__explode($val);
  385. if ($pos !== false) {
  386. $val = substr($val, 8);
  387. $decrypted[$name][$key] = $this->__explode(Security::$type(base64_decode($val), $this->key));
  388. }
  389. }
  390. } else {
  391. $pos = strpos($value, 'Q2FrZQ==.');
  392. $decrypted[$name] = $this->__explode($value);
  393. if ($pos !== false) {
  394. $value = substr($value, 8);
  395. $decrypted[$name] = $this->__explode(Security::$type(base64_decode($value), $this->key));
  396. }
  397. }
  398. }
  399. return $decrypted;
  400. }
  401. /**
  402. * Implode method to keep keys are multidimensional arrays
  403. *
  404. * @param array $array Map of key and values
  405. * @return string String in the form key1|value1,key2|value2
  406. * @access private
  407. */
  408. function __implode($array) {
  409. $string = '';
  410. foreach ($array as $key => $value) {
  411. $string .= ',' . $key . '|' . $value;
  412. }
  413. return substr($string, 1);
  414. }
  415. /**
  416. * Explode method to return array from string set in CookieComponent::__implode()
  417. *
  418. * @param string $string String in the form key1|value1,key2|value2
  419. * @return array Map of key and values
  420. * @access private
  421. */
  422. function __explode($string) {
  423. $array = array();
  424. foreach (explode(',', $string) as $pair) {
  425. $key = explode('|', $pair);
  426. if (!isset($key[1])) {
  427. return $key[0];
  428. }
  429. $array[$key[0]] = $key[1];
  430. }
  431. return $array;
  432. }
  433. }