2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2010-05-29 15:20:28 +00:00
|
|
|
* Cookie Component
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-10-03 16:38:58 +00:00
|
|
|
* PHP 5
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-11-06 06:46:59 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2010-01-26 19:18:20 +00:00
|
|
|
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2010-01-26 19:18:20 +00:00
|
|
|
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2009-11-06 06:00:11 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2008-10-30 17:30:26 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs.controller.components
|
|
|
|
* @since CakePHP(tm) v 1.2.0.4213
|
2009-11-06 06:51:51 +00:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Load Security class
|
|
|
|
*/
|
2008-06-11 15:46:31 +00:00
|
|
|
App::import('Core', 'Security');
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Cookie Component.
|
|
|
|
*
|
|
|
|
* Cookie handling for the controller.
|
|
|
|
*
|
2008-10-30 17:30:26 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs.controller.components
|
2010-04-05 01:12:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1280/Cookies
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
*/
|
2010-07-04 19:12:42 +00:00
|
|
|
class CookieComponent extends Component {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* The name of the cookie.
|
|
|
|
*
|
|
|
|
* Overridden with the controller beforeFilter();
|
|
|
|
* $this->Cookie->name = 'CookieName';
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $name = 'CakeCookie';
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* The time a cookie will remain valid.
|
|
|
|
*
|
|
|
|
* Can be either integer Unix timestamp or a date string.
|
|
|
|
*
|
|
|
|
* Overridden with the controller beforeFilter();
|
|
|
|
* $this->Cookie->time = '5 Days';
|
|
|
|
*
|
|
|
|
* @var mixed
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $time = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Cookie path.
|
|
|
|
*
|
|
|
|
* Overridden with the controller beforeFilter();
|
|
|
|
* $this->Cookie->path = '/';
|
|
|
|
*
|
|
|
|
* The path on the server in which the cookie will be available on.
|
2010-04-04 07:14:00 +00:00
|
|
|
* If public $cookiePath is set to '/foo/', the cookie will only be available
|
2008-05-30 11:40:08 +00:00
|
|
|
* within the /foo/ directory and all sub-directories such as /foo/bar/ of domain.
|
|
|
|
* The default value is the entire domain.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $path = '/';
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Domain path.
|
|
|
|
*
|
|
|
|
* The domain that the cookie is available.
|
|
|
|
*
|
|
|
|
* Overridden with the controller beforeFilter();
|
|
|
|
* $this->Cookie->domain = '.example.com';
|
|
|
|
*
|
|
|
|
* To make the cookie available on all subdomains of example.com.
|
|
|
|
* Set $this->Cookie->domain = '.example.com'; in your controller beforeFilter
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $domain = '';
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Secure HTTPS only cookie.
|
|
|
|
*
|
|
|
|
* Overridden with the controller beforeFilter();
|
|
|
|
* $this->Cookie->secure = true;
|
|
|
|
*
|
|
|
|
* Indicates that the cookie should only be transmitted over a secure HTTPS connection.
|
|
|
|
* When set to true, the cookie will only be set if a secure connection exists.
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $secure = false;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Encryption key.
|
|
|
|
*
|
|
|
|
* Overridden with the controller beforeFilter();
|
|
|
|
* $this->Cookie->key = 'SomeRandomString';
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access protected
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $key = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-09-26 03:24:37 +00:00
|
|
|
/**
|
|
|
|
* HTTP only cookie
|
|
|
|
*
|
|
|
|
* Set to true to make HTTP only cookies. Cookies that are HTTP only
|
|
|
|
* are not accessible in Javascript.
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
public $httpOnly = false;
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Values stored in the cookie.
|
|
|
|
*
|
|
|
|
* Accessed in the controller using $this->Cookie->read('Name.key');
|
|
|
|
*
|
|
|
|
* @see CookieComponent::read();
|
|
|
|
* @var string
|
|
|
|
* @access private
|
|
|
|
*/
|
2010-09-19 23:30:41 +00:00
|
|
|
protected $_values = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Type of encryption to use.
|
|
|
|
*
|
|
|
|
* Currently only one method is available
|
|
|
|
* Defaults to Security::cipher();
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access private
|
|
|
|
* @todo add additional encryption methods
|
|
|
|
*/
|
2010-09-19 23:30:41 +00:00
|
|
|
protected $_type = 'cipher';
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Used to reset cookie time if $expire is passed to CookieComponent::write()
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access private
|
|
|
|
*/
|
2010-09-19 23:30:41 +00:00
|
|
|
protected $_reset = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Expire time of the cookie
|
|
|
|
*
|
|
|
|
* This is controlled by CookieComponent::time;
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access private
|
|
|
|
*/
|
2010-09-19 23:30:41 +00:00
|
|
|
protected $_expires = 0;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-09-19 23:30:41 +00:00
|
|
|
* Constructor
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-09-19 23:30:41 +00:00
|
|
|
* @param ComponentCollection $collection A ComponentCollection for this component
|
|
|
|
* @param array $settings Array of settings.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-09-19 23:30:41 +00:00
|
|
|
public function __construct(ComponentCollection $collection, $settings = array()) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->key = Configure::read('Security.salt');
|
2010-09-19 23:30:41 +00:00
|
|
|
parent::__construct($collection, $settings);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Start CookieComponent for use in the controller
|
|
|
|
*
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function startup() {
|
2010-09-19 23:30:41 +00:00
|
|
|
$this->_expire($this->time);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if (isset($_COOKIE[$this->name])) {
|
2010-09-19 23:30:41 +00:00
|
|
|
$this->_values = $this->_decrypt($_COOKIE[$this->name]);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Write a value to the $_COOKIE[$key];
|
|
|
|
*
|
|
|
|
* Optional [Name.], reguired key, optional $value, optional $encrypt, optional $expires
|
|
|
|
* $this->Cookie->write('[Name.]key, $value);
|
|
|
|
*
|
|
|
|
* By default all values are encrypted.
|
|
|
|
* You must pass $encrypt false to store values in clear test
|
|
|
|
*
|
|
|
|
* You must use this method before any output is sent to the browser.
|
|
|
|
* Failure to do so will result in header already sent errors.
|
|
|
|
*
|
|
|
|
* @param mixed $key Key for the value
|
|
|
|
* @param mixed $value Value
|
|
|
|
* @param boolean $encrypt Set to true to encrypt value, false otherwise
|
|
|
|
* @param string $expires Can be either Unix timestamp, or date string
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function write($key, $value = null, $encrypt = true, $expires = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (is_null($encrypt)) {
|
|
|
|
$encrypt = true;
|
|
|
|
}
|
2010-09-19 23:30:41 +00:00
|
|
|
$this->_encrypted = $encrypt;
|
|
|
|
$this->_expire($expires);
|
2009-10-29 04:44:30 +00:00
|
|
|
|
|
|
|
if (!is_array($key)) {
|
|
|
|
$key = array($key => $value);
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2009-10-29 04:44:30 +00:00
|
|
|
foreach ($key as $name => $value) {
|
|
|
|
if (strpos($name, '.') === false) {
|
2010-09-19 23:30:41 +00:00
|
|
|
$this->_values[$name] = $value;
|
|
|
|
$this->_write("[$name]", $value);
|
2009-10-29 04:44:30 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2009-10-29 04:44:30 +00:00
|
|
|
$names = explode('.', $name, 2);
|
2010-09-19 23:30:41 +00:00
|
|
|
if (!isset($this->_values[$names[0]])) {
|
|
|
|
$this->_values[$names[0]] = array();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-09-19 23:30:41 +00:00
|
|
|
$this->_values[$names[0]] = Set::insert($this->_values[$names[0]], $names[1], $value);
|
|
|
|
$this->_write('[' . implode('][', $names) . ']', $value);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2010-09-19 23:30:41 +00:00
|
|
|
$this->_encrypted = true;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Read the value of the $_COOKIE[$key];
|
|
|
|
*
|
|
|
|
* Optional [Name.], reguired key
|
|
|
|
* $this->Cookie->read(Name.key);
|
|
|
|
*
|
|
|
|
* @param mixed $key Key of the value to be obtained. If none specified, obtain map key => values
|
|
|
|
* @return string or null, value for specified key
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function read($key = null) {
|
2010-09-19 23:30:41 +00:00
|
|
|
if (empty($this->_values) && isset($_COOKIE[$this->name])) {
|
|
|
|
$this->_values = $this->_decrypt($_COOKIE[$this->name]);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_null($key)) {
|
2010-09-19 23:30:41 +00:00
|
|
|
return $this->_values;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-10-29 04:44:30 +00:00
|
|
|
|
|
|
|
if (strpos($key, '.') !== false) {
|
|
|
|
$names = explode('.', $key, 2);
|
|
|
|
$key = $names[0];
|
|
|
|
}
|
2010-09-19 23:30:41 +00:00
|
|
|
if (!isset($this->_values[$key])) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return null;
|
|
|
|
}
|
2009-10-29 04:44:30 +00:00
|
|
|
|
|
|
|
if (!empty($names[1])) {
|
2010-09-19 23:30:41 +00:00
|
|
|
return Set::extract($this->_values[$key], $names[1]);
|
2009-10-29 04:44:30 +00:00
|
|
|
}
|
2010-09-19 23:30:41 +00:00
|
|
|
return $this->_values[$key];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Delete a cookie value
|
|
|
|
*
|
|
|
|
* Optional [Name.], reguired key
|
|
|
|
* $this->Cookie->read('Name.key);
|
|
|
|
*
|
|
|
|
* You must use this method before any output is sent to the browser.
|
|
|
|
* Failure to do so will result in header already sent errors.
|
|
|
|
*
|
|
|
|
* @param string $key Key of the value to be deleted
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function delete($key) {
|
2010-09-19 23:30:41 +00:00
|
|
|
if (empty($this->_values)) {
|
2008-10-10 17:24:10 +00:00
|
|
|
$this->read();
|
|
|
|
}
|
2009-10-29 04:44:30 +00:00
|
|
|
if (strpos($key, '.') === false) {
|
2010-09-19 23:30:41 +00:00
|
|
|
unset($this->_values[$key]);
|
|
|
|
$this->_delete("[$key]");
|
2009-10-29 04:44:30 +00:00
|
|
|
return;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-10-29 04:44:30 +00:00
|
|
|
$names = explode('.', $key, 2);
|
2010-09-19 23:30:41 +00:00
|
|
|
$this->_values[$names[0]] = Set::remove($this->_values[$names[0]], $names[1]);
|
|
|
|
$this->_delete('[' . implode('][', $names) . ']');
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Destroy current cookie
|
|
|
|
*
|
|
|
|
* You must use this method before any output is sent to the browser.
|
|
|
|
* Failure to do so will result in header already sent errors.
|
|
|
|
*
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function destroy() {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (isset($_COOKIE[$this->name])) {
|
2010-09-19 23:30:41 +00:00
|
|
|
$this->_values = $this->_decrypt($_COOKIE[$this->name]);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2010-09-19 23:30:41 +00:00
|
|
|
foreach ($this->_values as $name => $value) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (is_array($value)) {
|
|
|
|
foreach ($value as $key => $val) {
|
2010-09-19 23:30:41 +00:00
|
|
|
unset($this->_values[$name][$key]);
|
|
|
|
$this->_delete("[$name][$key]");
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2010-09-19 23:30:41 +00:00
|
|
|
unset($this->_values[$name]);
|
|
|
|
$this->_delete("[$name]");
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Will allow overriding default encryption method.
|
|
|
|
*
|
|
|
|
* @param string $type Encryption method
|
|
|
|
* @access public
|
|
|
|
* @todo NOT IMPLEMENTED
|
|
|
|
*/
|
2010-09-19 23:30:41 +00:00
|
|
|
public function type($type = 'cipher') {
|
|
|
|
$this->_type = 'cipher';
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Set the expire time for a session variable.
|
|
|
|
*
|
|
|
|
* Creates a new expire time for a session variable.
|
|
|
|
* $expire can be either integer Unix timestamp or a date string.
|
|
|
|
*
|
|
|
|
* Used by write()
|
|
|
|
* CookieComponent::write(string, string, boolean, 8400);
|
|
|
|
* CookieComponent::write(string, string, boolean, '5 Days');
|
|
|
|
*
|
|
|
|
* @param mixed $expires Can be either Unix timestamp, or date string
|
|
|
|
* @return int Unix timestamp
|
|
|
|
*/
|
2010-09-19 23:30:41 +00:00
|
|
|
protected function _expire($expires = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$now = time();
|
|
|
|
if (is_null($expires)) {
|
2010-09-19 23:30:41 +00:00
|
|
|
return $this->_expires;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-09-19 23:30:41 +00:00
|
|
|
$this->_reset = $this->_expires;
|
2009-11-04 04:02:26 +00:00
|
|
|
|
|
|
|
if ($expires == 0) {
|
2010-09-19 23:30:41 +00:00
|
|
|
return $this->_expires = 0;
|
2009-11-04 04:02:26 +00:00
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if (is_integer($expires) || is_numeric($expires)) {
|
2010-09-19 23:30:41 +00:00
|
|
|
return $this->_expires = $now + intval($expires);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-09-19 23:30:41 +00:00
|
|
|
return $this->_expires = strtotime($expires, $now);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Set cookie
|
|
|
|
*
|
|
|
|
* @param string $name Name for cookie
|
|
|
|
* @param string $value Value for cookie
|
|
|
|
*/
|
2010-09-19 23:30:41 +00:00
|
|
|
protected function _write($name, $value) {
|
2010-09-26 03:24:37 +00:00
|
|
|
$this->_setcookie(
|
|
|
|
$this->name . $name, $this->_encrypt($value),
|
|
|
|
$this->_expires, $this->path, $this->domain, $this->secure, $this->httpOnly
|
|
|
|
);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-09-19 23:30:41 +00:00
|
|
|
if (!is_null($this->_reset)) {
|
|
|
|
$this->_expires = $this->_reset;
|
|
|
|
$this->_reset = null;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Sets a cookie expire time to remove cookie value
|
|
|
|
*
|
|
|
|
* @param string $name Name of cookie
|
2010-09-19 23:42:06 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-09-19 23:42:06 +00:00
|
|
|
protected function _delete($name) {
|
2010-09-26 03:24:37 +00:00
|
|
|
$this->_setcookie(
|
|
|
|
$this->name . $name, '',
|
|
|
|
time() - 42000, $this->path, $this->domain, $this->secure, $this->httpOnly
|
|
|
|
);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-09-19 23:42:06 +00:00
|
|
|
/**
|
|
|
|
* Object wrapper for setcookie() so it can be mocked in unit tests.
|
|
|
|
*
|
|
|
|
* @param string $name Name of the cookie
|
|
|
|
* @param integer $expire Time the cookie expires in
|
|
|
|
* @param string $path Path the cookie applies to
|
|
|
|
* @param string $domain Domain the cookie is for.
|
|
|
|
* @param boolean $secure Is the cookie https?
|
|
|
|
* @param boolean $httpOnly Is the cookie available in the client?
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function _setcookie($name, $value, $expire, $path, $domain, $secure, $httpOnly = false) {
|
|
|
|
setcookie($name, $value, $expire, $path, $domain, $secure, $httpOnly);
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-04-04 07:14:00 +00:00
|
|
|
* Encrypts $value using public $type method in Security class
|
2008-06-20 20:17:23 +00:00
|
|
|
*
|
|
|
|
* @param string $value Value to encrypt
|
|
|
|
* @return string encrypted string
|
2010-09-19 23:42:06 +00:00
|
|
|
* @return string Encoded values
|
2008-06-20 20:17:23 +00:00
|
|
|
*/
|
2010-09-19 23:42:06 +00:00
|
|
|
protected function _encrypt($value) {
|
2008-06-20 20:17:23 +00:00
|
|
|
if (is_array($value)) {
|
2010-09-19 23:30:41 +00:00
|
|
|
$value = $this->_implode($value);
|
2008-06-20 20:17:23 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-09-19 23:30:41 +00:00
|
|
|
if ($this->_encrypted === true) {
|
|
|
|
$type = $this->_type;
|
2008-06-20 20:17:23 +00:00
|
|
|
$value = "Q2FrZQ==." .base64_encode(Security::$type($value, $this->key));
|
|
|
|
}
|
2009-10-29 04:44:30 +00:00
|
|
|
return $value;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-04-04 07:14:00 +00:00
|
|
|
* Decrypts $value using public $type method in Security class
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param array $values Values to decrypt
|
|
|
|
* @return string decrypted string
|
|
|
|
*/
|
2010-09-19 23:42:06 +00:00
|
|
|
protected function _decrypt($values) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$decrypted = array();
|
2010-09-19 23:30:41 +00:00
|
|
|
$type = $this->_type;
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
foreach ($values as $name => $value) {
|
|
|
|
if (is_array($value)) {
|
|
|
|
foreach ($value as $key => $val) {
|
|
|
|
$pos = strpos($val, 'Q2FrZQ==.');
|
2010-09-19 23:30:41 +00:00
|
|
|
$decrypted[$name][$key] = $this->_explode($val);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if ($pos !== false) {
|
|
|
|
$val = substr($val, 8);
|
2010-09-19 23:30:41 +00:00
|
|
|
$decrypted[$name][$key] = $this->_explode(Security::$type(base64_decode($val), $this->key));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2008-09-25 16:49:56 +00:00
|
|
|
$pos = strpos($value, 'Q2FrZQ==.');
|
2010-09-19 23:30:41 +00:00
|
|
|
$decrypted[$name] = $this->_explode($value);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2008-09-25 16:49:56 +00:00
|
|
|
if ($pos !== false) {
|
|
|
|
$value = substr($value, 8);
|
2010-09-19 23:30:41 +00:00
|
|
|
$decrypted[$name] = $this->_explode(Security::$type(base64_decode($value), $this->key));
|
2008-09-25 16:49:56 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-10-29 04:44:30 +00:00
|
|
|
return $decrypted;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Implode method to keep keys are multidimensional arrays
|
|
|
|
*
|
|
|
|
* @param array $array Map of key and values
|
|
|
|
* @return string String in the form key1|value1,key2|value2
|
|
|
|
*/
|
2010-09-19 23:42:06 +00:00
|
|
|
protected function _implode($array) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$string = '';
|
|
|
|
foreach ($array as $key => $value) {
|
|
|
|
$string .= ',' . $key . '|' . $value;
|
|
|
|
}
|
|
|
|
return substr($string, 1);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-09-19 23:30:41 +00:00
|
|
|
* Explode method to return array from string set in CookieComponent::_implode()
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param string $string String in the form key1|value1,key2|value2
|
|
|
|
* @return array Map of key and values
|
|
|
|
*/
|
2010-09-19 23:42:06 +00:00
|
|
|
protected function _explode($string) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$array = array();
|
|
|
|
foreach (explode(',', $string) as $pair) {
|
|
|
|
$key = explode('|', $pair);
|
|
|
|
if (!isset($key[1])) {
|
|
|
|
return $key[0];
|
|
|
|
}
|
|
|
|
$array[$key[0]] = $key[1];
|
|
|
|
}
|
|
|
|
return $array;
|
|
|
|
}
|
|
|
|
}
|