Permalink
Browse files

Adding forwards compatibility to CookieComponent.

This allows 1.3 and 2.0 applications to interoperate easily.
Fixes #2131
  • Loading branch information...
markstory committed Nov 5, 2011
1 parent 6c92edb commit b75c414bbb175f6317d59fa7f8170597b10ec878
@@ -468,6 +468,10 @@ function __implode($array) {
* @access private
*/
function __explode($string) {
if (($string[0] === '{' || $string[0] === '[') && function_exists('json_decode')) {

This comment has been minimized.

Show comment
Hide comment
@bar

bar Jan 20, 2012

Contributor

should test for !empty($string) or $string!="" first

@bar

bar Jan 20, 2012

Contributor

should test for !empty($string) or $string!="" first

$ret = json_decode($string, true);
return ($ret != null) ? $ret : $string;

This comment has been minimized.

Show comment
Hide comment
@bar

bar Jan 20, 2012

Contributor

should return an array if $ret is null, I think

@bar

bar Jan 20, 2012

Contributor

should return an array if $ret is null, I think

}
$array = array();
foreach (explode(',', $string) as $pair) {
$key = explode('|', $pair);
@@ -478,6 +478,20 @@ function testDeleteChildrenNotExist() {
$this->assertNull($this->Controller->Cookie->delete('Not.Found'));
}
/**
* Test that 1.3 can read 2.0 format codes if json_encode exists.
*
* @return void
*/
function testForwardsCompatibility() {
if ($this->skipIf(!function_exists('json_decode'), 'no json_decode, skipping.')) {
return;
}
$_COOKIE['CakeTestCookie'] = array('Test' => '{"name":"value"}');
$this->Controller->Cookie->startup($this->Controller);
$this->assertEqual('value', $this->Controller->Cookie->read('Test.name'));
}
/**
* encrypt method
*

1 comment on commit b75c414

@bar

This comment has been minimized.

Show comment
Hide comment
@bar

bar Jan 20, 2012

Contributor

This produces a notice if the cookie value is an empty string, I think this should be tested before the first string character. And also, the function should always return an array, not a string if $ret is null. Something like this:

    function __explode($string) {
        if (!empty($string[0]) && ($string[0] === '{' || $string[0] === '[') && function_exists('json_decode')) {
            $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;
    }
Contributor

bar commented on b75c414 Jan 20, 2012

This produces a notice if the cookie value is an empty string, I think this should be tested before the first string character. And also, the function should always return an array, not a string if $ret is null. Something like this:

    function __explode($string) {
        if (!empty($string[0]) && ($string[0] === '{' || $string[0] === '[') && function_exists('json_decode')) {
            $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;
    }
Please sign in to comment.