PageRenderTime 86ms CodeModel.GetById 35ms RepoModel.GetById 2ms app.codeStats 0ms

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

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