Skip to content

Commit

Permalink
Fix reading multiple keys in a single request.
Browse files Browse the repository at this point in the history
Fixes #2676
  • Loading branch information
markstory committed Mar 16, 2012
1 parent 5713cd3 commit 6a55749
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 24 deletions.
53 changes: 30 additions & 23 deletions lib/Cake/Controller/Component/CookieComponent.php
Expand Up @@ -190,8 +190,9 @@ public function __construct(ComponentCollection $collection, $settings = array()
public function startup(Controller $controller) {
$this->_expire($this->time);

$this->_values[$this->name] = array();
if (isset($_COOKIE[$this->name])) {
$this->_values = $this->_decrypt($_COOKIE[$this->name]);
$this->_values[$this->name] = $this->_decrypt($_COOKIE[$this->name]);
}
}

Expand All @@ -215,6 +216,10 @@ public function startup(Controller $controller) {
* @link http://book.cakephp.org/2.0/en/core-libraries/components/cookie.html#CookieComponent::write
*/
public function write($key, $value = null, $encrypt = true, $expires = null) {
if (empty($this->_values[$this->name])) {
$this->read();
}

if (is_null($encrypt)) {
$encrypt = true;
}
Expand All @@ -227,14 +232,14 @@ public function write($key, $value = null, $encrypt = true, $expires = null) {

foreach ($key as $name => $value) {
if (strpos($name, '.') === false) {
$this->_values[$name] = $value;
$this->_values[$this->name][$name] = $value;
$this->_write("[$name]", $value);
} else {
$names = explode('.', $name, 2);
if (!isset($this->_values[$names[0]])) {
$this->_values[$names[0]] = array();
if (!isset($this->_values[$this->name][$names[0]])) {
$this->_values[$this->name][$names[0]] = array();
}
$this->_values[$names[0]] = Set::insert($this->_values[$names[0]], $names[1], $value);
$this->_values[$this->name][$names[0]] = Set::insert($this->_values[$this->name][$names[0]], $names[1], $value);
$this->_write('[' . implode('][', $names) . ']', $value);
}
}
Expand All @@ -252,26 +257,28 @@ public function write($key, $value = null, $encrypt = true, $expires = null) {
* @link http://book.cakephp.org/2.0/en/core-libraries/components/cookie.html#CookieComponent::read
*/
public function read($key = null) {
if (empty($this->_values) && isset($_COOKIE[$this->name])) {
$this->_values = $this->_decrypt($_COOKIE[$this->name]);
if (empty($this->_values[$this->name]) && isset($_COOKIE[$this->name])) {
$this->_values[$this->name] = $this->_decrypt($_COOKIE[$this->name]);
}
if (empty($this->_values[$this->name])) {
$this->_values[$this->name] = array();
}

if (is_null($key)) {
return $this->_values;
return $this->_values[$this->name];
}

if (strpos($key, '.') !== false) {
$names = explode('.', $key, 2);
$key = $names[0];
}
if (!isset($this->_values[$key])) {
if (!isset($this->_values[$this->name][$key])) {
return null;
}

if (!empty($names[1])) {
return Set::extract($this->_values[$key], $names[1]);
return Set::extract($this->_values[$this->name][$key], $names[1]);
}
return $this->_values[$key];
return $this->_values[$this->name][$key];
}

/**
Expand All @@ -288,22 +295,22 @@ public function read($key = null) {
* @link http://book.cakephp.org/2.0/en/core-libraries/components/cookie.html#CookieComponent::delete
*/
public function delete($key) {
if (empty($this->_values)) {
if (empty($this->_values[$this->name])) {
$this->read();
}
if (strpos($key, '.') === false) {
if (isset($this->_values[$key]) && is_array($this->_values[$key])) {
foreach ($this->_values[$key] as $idx => $val) {
if (isset($this->_values[$this->name][$key]) && is_array($this->_values[$this->name][$key])) {
foreach ($this->_values[$this->name][$key] as $idx => $val) {
$this->_delete("[$key][$idx]");
}
}
$this->_delete("[$key]");
unset($this->_values[$key]);
unset($this->_values[$this->name][$key]);
return;
}
$names = explode('.', $key, 2);
if (isset($this->_values[$names[0]])) {
$this->_values[$names[0]] = Set::remove($this->_values[$names[0]], $names[1]);
if (isset($this->_values[$this->name][$names[0]])) {
$this->_values[$this->name][$names[0]] = Set::remove($this->_values[$this->name][$names[0]], $names[1]);
}
$this->_delete('[' . implode('][', $names) . ']');
}
Expand All @@ -319,17 +326,17 @@ public function delete($key) {
*/
public function destroy() {
if (isset($_COOKIE[$this->name])) {
$this->_values = $this->_decrypt($_COOKIE[$this->name]);
$this->_values[$this->name] = $this->_decrypt($_COOKIE[$this->name]);
}

foreach ($this->_values as $name => $value) {
foreach ($this->_values[$this->name] as $name => $value) {
if (is_array($value)) {
foreach ($value as $key => $val) {
unset($this->_values[$name][$key]);
unset($this->_values[$this->name][$name][$key]);
$this->_delete("[$name][$key]");
}
}
unset($this->_values[$name]);
unset($this->_values[$this->name][$name]);
$this->_delete("[$name]");
}
}
Expand Down Expand Up @@ -503,5 +510,5 @@ protected function _explode($string) {
}
return $array;
}

}

21 changes: 20 additions & 1 deletion lib/Cake/Test/Case/Controller/Component/CookieComponentTest.php
Expand Up @@ -160,7 +160,6 @@ public function testReadEncryptedCookieData() {
*/
public function testReadPlainCookieData() {
$this->_setCookieData();

$data = $this->Cookie->read('Plain_array');
$expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
$this->assertEquals($data, $expected);
Expand All @@ -170,6 +169,26 @@ public function testReadPlainCookieData() {
$this->assertEquals($data, $expected);
}

/**
* test read() after switching the cookie name.
*
* @return void
*/
public function testReadWithNameSwitch() {
$_COOKIE = array(
'CakeTestCookie' => array(
'key' => 'value'
),
'OtherTestCookie' => array(
'key' => 'other value'
)
);
$this->assertEquals('value', $this->Cookie->read('key'));

$this->Cookie->name = 'OtherTestCookie';
$this->assertEquals('other value', $this->Cookie->read('key'));
}

/**
* test a simple write()
*
Expand Down

0 comments on commit 6a55749

Please sign in to comment.