Skip to content

Commit

Permalink
Adding backwards compatible cookie reading back into CookieComponent.
Browse files Browse the repository at this point in the history
Cookie values using the 1.x formatting will be read, and upon next write
be converted to json encoded values.
Fixes #1593
  • Loading branch information
markstory committed Apr 19, 2011
1 parent d4ff392 commit 6327562
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
16 changes: 14 additions & 2 deletions lib/Cake/Controller/Component/CookieComponent.php
Expand Up @@ -467,12 +467,24 @@ protected function _implode(array $array) {

/**
* Explode method to return array from string set in CookieComponent::_implode()
* Maintains reading backwards compatibility with 1.x CookieComponent::_implode().
*
* @param string $string A string containing JSON encoded data, or a bare string.
* @return array Map of key and values
*/
protected function _explode($string) {
$ret = json_decode($string, true);
return ($ret != null) ? $ret : $string;
if ($string[0] === '{' || $string[0] === '[') {
$ret = json_decode($string, true);
return ($ret != null) ? $ret : $string;
}
$array = array();
foreach (explode(',', $string) as $pair) {
$key = explode('|', $pair);
if (!isset($key[1])) {
return $key[0];
}
$array[$key[0]] = $key[1];
}
return $array;
}
}
32 changes: 26 additions & 6 deletions lib/Cake/tests/Case/Controller/Component/CookieComponentTest.php
Expand Up @@ -471,6 +471,19 @@ function testReadingCookieDataWithoutStartup() {
unset($_COOKIE['CakeTestCookie']);
}

/**
* Test Reading legacy cookie values.
*
* @return void
*/
function testReadLegacyCookieValue() {
$_COOKIE['CakeTestCookie'] = array(
'Legacy' => array('value' => $this->_oldImplode(array(1, 2, 3)))
);
$result = $this->Cookie->read('Legacy.value');
$expected = array(1, 2, 3);
$this->assertEquals($expected, $result);
}

/**
* test that no error is issued for non array data.
Expand All @@ -485,21 +498,28 @@ function testNoErrorOnNonArrayData() {
}

/**
* Implode method to keep keys are multidimensional arrays
* Helper method for generating old style encoded cookie values.
*
* @param array $array Map of key and values
* @return string String in the form key1|value1,key2|value2
* @return string.
*/
protected function _implode(array $array) {
return json_encode($array);

protected function _oldImplode(array $array) {
$string = '';
foreach ($array as $key => $value) {
$string .= ',' . $key . '|' . $value;
}
return substr($string, 1);
}

/**
* 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
*/
protected function _implode(array $array) {
return json_encode($array);
}

/**
* encrypt method
*
Expand Down

1 comment on commit 6327562

@ptica
Copy link
Contributor

@ptica ptica commented on 6327562 Jul 4, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the backward compatibility fix is causing me after-comma-trim even for newly stored strings containing commas
(json_encode is not called on strings -> no json_decode -> splititng occurs)

Please sign in to comment.