Skip to content

Commit

Permalink
Revert "Moved serialization of Cookie to Cookie. Added unit test …
Browse files Browse the repository at this point in the history
…for serialization / deserialization."

This reverts commit a2acd88.
  • Loading branch information
SamMousa committed Apr 3, 2017
1 parent e26bc50 commit 183ebc5
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 71 deletions.
33 changes: 0 additions & 33 deletions framework/web/Cookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

namespace yii\web;

use yii\base\InvalidConfigException;

/**
* Cookie represents information related with a cookie, such as [[name]], [[value]], [[domain]], etc.
*
Expand Down Expand Up @@ -67,35 +65,4 @@ public function __toString()
{
return (string) $this->value;
}

/**
* Unserializes a cookie received by the client.
* @param string $data
* @return self|null
*/
public static function fromDataString($data)
{
if (($unserialized = json_decode($data, true)) !== false) {
$result = new self();
$result->name = $unserialized[0];
$result->value = $unserialized[1];
$result->expire = 0;
return $result;
}
}

/**
* Unserializes a cookie received by the client.
* @return string
*/
public function toDataString()
{
if (!isset($this->name)) {
throw new InvalidConfigException("Cookie serialization requires a name.");
}
return json_encode([
$this->name,
$this->value
]);
}
}
10 changes: 7 additions & 3 deletions framework/web/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -1286,9 +1286,13 @@ protected function loadCookies()
if ($data === false) {
continue;
}

if (($cookie = Cookie::fromDataString($data)) !== null && $cookie->name === $name) {
$cookies[$name] = $cookie;
$data = @unserialize($data);
if (is_array($data) && isset($data[0], $data[1]) && $data[0] === $name) {
$cookies[$name] = new Cookie([
'name' => $name,
'value' => $data[1],
'expire' => null,
]);
}
}
} else {
Expand Down
6 changes: 2 additions & 4 deletions framework/web/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -394,12 +394,10 @@ protected function sendCookies()
}
$validationKey = $request->cookieValidationKey;
}
/** @var Cookie $cookie */
foreach ($this->getCookies() as $cookie) {
$value = $cookie->value;
if ($cookie->expire != 1 && isset($validationKey)) {
$value = Yii::$app->getSecurity()->hashData($cookie->toDataString(), $validationKey);
} else {
$value = $cookie->value;
$value = Yii::$app->getSecurity()->hashData(serialize([$cookie->name, $value]), $validationKey);
}
setcookie($cookie->name, $value, $cookie->expire, $cookie->path, $cookie->domain, $cookie->secure, $cookie->httpOnly);
}
Expand Down
31 changes: 0 additions & 31 deletions tests/framework/web/CookieTest.php

This file was deleted.

0 comments on commit 183ebc5

Please sign in to comment.