Skip to content

Commit

Permalink
Merge pull request #10792 from vpg/issue_10789
Browse files Browse the repository at this point in the history
Cookie fix #10789
  • Loading branch information
andresgutierrez committed Aug 14, 2015
2 parents 5ccecc2 + 2644966 commit d69a8e2
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 33 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ belongs to the uniqueId or the whole session data
- Added parameter the changing operator for conditions in method `Mvc\Model\Criteria::fromImput()` [#10749](https://github.com/phalcon/cphalcon/issues/10749)
- Added `\Phalcon\Queue\Beanstalk::listTubes()` to get list of a tubes
- Added a fix to avoid that a table present in many sub-queries causes invalid SQL generation
- Add CookieInterface, update Cookie and Cookies to use this interface - Decoupling Cookies and Cookie - Check Session state before using it in Cookie . [#10789](https://github.com/phalcon/cphalcon/issues/10789)

# [2.0.6](https://github.com/phalcon/cphalcon/releases/tag/phalcon-v2.0.6) (2015-07-21)
- Builds in TravisCI now uses Docker to perform faster builds
Expand Down
62 changes: 34 additions & 28 deletions phalcon/http/cookie.zep
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use Phalcon\Session\AdapterInterface as SessionInterface;
*
* Provide OO wrappers to manage a HTTP cookie
*/
class Cookie implements InjectionAwareInterface
class Cookie implements CookieInterface, InjectionAwareInterface
{

protected _readed = false;
Expand Down Expand Up @@ -117,7 +117,7 @@ class Cookie implements InjectionAwareInterface
* @param string value
* @return \Phalcon\Http\Cookie
*/
public function setValue(value) -> <Cookie>
public function setValue(value) -> <CookieInterface>
{
let this->_value = value,
this->_readed = true;
Expand Down Expand Up @@ -201,7 +201,7 @@ class Cookie implements InjectionAwareInterface
* Sends the cookie to the HTTP client
* Stores the cookie definition in session
*/
public function send() -> <Cookie>
public function send() -> <CookieInterface>
{
var name, value, expire, domain, path, secure, httpOnly,
dependencyInjector, definition, session, crypt, encryptValue;
Expand Down Expand Up @@ -247,7 +247,9 @@ class Cookie implements InjectionAwareInterface
*/
if count(definition) {
let session = <SessionInterface> dependencyInjector->getShared("session");
session->set("_PHCOOKIE_" . name, definition);
if session->isStarted() {
session->set("_PHCOOKIE_" . name, definition);
}
}

if this->_useEncryption {
Expand Down Expand Up @@ -285,7 +287,7 @@ class Cookie implements InjectionAwareInterface
* Reads the cookie-related info from the SESSION to restore the cookie as it was set
* This method is automatically called internally so normally you don't need to call it
*/
public function restore() -> <Cookie>
public function restore() -> <CookieInterface>
{
var dependencyInjector, expire, domain, path, secure,
httpOnly, session, definition;
Expand All @@ -297,27 +299,29 @@ class Cookie implements InjectionAwareInterface

let session = dependencyInjector->getShared("session");

let definition = session->get("_PHCOOKIE_" . this->_name);
if typeof definition == "array" {
if session->isStarted() {
let definition = session->get("_PHCOOKIE_" . this->_name);
if typeof definition == "array" {

if fetch expire, definition["expire"] {
let this->_expire = expire;
}
if fetch expire, definition["expire"] {
let this->_expire = expire;
}

if fetch domain, definition["domain"] {
let this->_domain = domain;
}
if fetch domain, definition["domain"] {
let this->_domain = domain;
}

if fetch path, definition["path"] {
let this->_path = path;
}
if fetch path, definition["path"] {
let this->_path = path;
}

if fetch secure, definition["secure"] {
let this->_secure = secure;
}
if fetch secure, definition["secure"] {
let this->_secure = secure;
}

if fetch httpOnly, definition["httpOnly"] {
let this->_httpOnly = httpOnly;
if fetch httpOnly, definition["httpOnly"] {
let this->_httpOnly = httpOnly;
}
}
}
}
Expand All @@ -344,7 +348,9 @@ class Cookie implements InjectionAwareInterface
let dependencyInjector = <DiInterface> this->_dependencyInjector;
if typeof dependencyInjector != "object" {
let session = <SessionInterface> dependencyInjector->getShared("session");
session->remove("_PHCOOKIE_" . name);
if session->isStarted() {
session->remove("_PHCOOKIE_" . name);
}
}

let this->_value = null;
Expand All @@ -354,7 +360,7 @@ class Cookie implements InjectionAwareInterface
/**
* Sets if the cookie must be encrypted/decrypted automatically
*/
public function useEncryption(boolean useEncryption) -> <Cookie>
public function useEncryption(boolean useEncryption) -> <CookieInterface>
{
let this->_useEncryption = useEncryption;
return this;
Expand All @@ -371,7 +377,7 @@ class Cookie implements InjectionAwareInterface
/**
* Sets the cookie's expiration time
*/
public function setExpiration(int expire) -> <Cookie>
public function setExpiration(int expire) -> <CookieInterface>
{
if !this->_restored {
this->restore();
Expand All @@ -394,7 +400,7 @@ class Cookie implements InjectionAwareInterface
/**
* Sets the cookie's expiration time
*/
public function setPath(string! path) -> <Cookie>
public function setPath(string! path) -> <CookieInterface>
{
if !this->_restored {
this->restore();
Expand Down Expand Up @@ -425,7 +431,7 @@ class Cookie implements InjectionAwareInterface
/**
* Sets the domain that the cookie is available to
*/
public function setDomain(string! domain) -> <Cookie>
public function setDomain(string! domain) -> <CookieInterface>
{
if !this->_restored {
this->restore();
Expand All @@ -448,7 +454,7 @@ class Cookie implements InjectionAwareInterface
/**
* Sets if the cookie must only be sent when the connection is secure (HTTPS)
*/
public function setSecure(boolean secure) -> <Cookie>
public function setSecure(boolean secure) -> <CookieInterface>
{
if !this->_restored {
this->restore();
Expand All @@ -471,7 +477,7 @@ class Cookie implements InjectionAwareInterface
/**
* Sets if the cookie is accessible only through the HTTP protocol
*/
public function setHttpOnly(boolean httpOnly) -> <Cookie>
public function setHttpOnly(boolean httpOnly) -> <CookieInterface>
{
if !this->_restored {
this->restore();
Expand Down
119 changes: 119 additions & 0 deletions phalcon/http/cookieinterface.zep
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@

/*
+------------------------------------------------------------------------+
| Phalcon Framework |
+------------------------------------------------------------------------+
| Copyright (c) 2011-2015 Phalcon Team (http://www.phalconphp.com) |
+------------------------------------------------------------------------+
| This source file is subject to the New BSD License that is bundled |
| with this package in the file docs/LICENSE.txt. |
| |
| If you did not receive a copy of the license and are unable to |
| obtain it through the world-wide-web, please send an email |
| to license@phalconphp.com so we can send you a copy immediately. |
+------------------------------------------------------------------------+
| Authors: Olivier Garbe <ogarbe@voyageprive.com |
+------------------------------------------------------------------------+
*/

namespace Phalcon\Http;

/**
* Phalcon\Http\CookieInterface
*
* Interface for Phalcon\Http\Cookie
*/
interface CookieInterface
{
/**
* Sets the cookie's value
*
* @param string value
* @return \Phalcon\Http\CookieInterface
*/
public function setValue(value) -> <CookieInterface>;

/**
* Returns the cookie's value
*
* @param string|array filters
* @param string defaultValue
* @return mixed
*/
public function getValue(filters = null, defaultValue = null);

/**
* Sends the cookie to the HTTP client
*/
public function send() -> <CookieInterface>;

/**
* Deletes the cookie
*/
public function delete();

/**
* Sets if the cookie must be encrypted/decrypted automatically
*/
public function useEncryption(boolean useEncryption) -> <CookieInterface>;

/**
* Check if the cookie is using implicit encryption
*/
public function isUsingEncryption() -> boolean;

/**
* Sets the cookie's expiration time
*/
public function setExpiration(int expire) -> <CookieInterface>;

/**
* Returns the current expiration time
*/
public function getExpiration() -> string;

/**
* Sets the cookie's expiration time
*/
public function setPath(string! path) -> <CookieInterface>;

/**
* Returns the current cookie's name
*/
public function getName() -> string;

/**
* Returns the current cookie's path
*/
public function getPath() -> string;

/**
* Sets the domain that the cookie is available to
*/
public function setDomain(string! domain) -> <CookieInterface>;

/**
* Returns the domain that the cookie is available to
*/
public function getDomain() -> string;

/**
* Sets if the cookie must only be sent when the connection is secure (HTTPS)
*/
public function setSecure(boolean secure) -> <CookieInterface>;

/**
* Returns whether the cookie must only be sent when the connection is secure (HTTPS)
*/
public function getSecure() -> boolean;

/**
* Sets if the cookie is accessible only through the HTTP protocol
*/
public function setHttpOnly(boolean httpOnly) -> <CookieInterface>;

/**
* Returns if the cookie is accessible only through the HTTP protocol
*/
public function getHttpOnly() -> boolean;
}
11 changes: 6 additions & 5 deletions phalcon/http/response/cookies.zep
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
namespace Phalcon\Http\Response;

use Phalcon\DiInterface;
use Phalcon\Http\Cookie;
use Phalcon\Http\CookieInterface;
use Phalcon\Http\Response\CookiesInterface;
use Phalcon\Di\InjectionAwareInterface;
use Phalcon\Http\Cookie\Exception;
Expand Down Expand Up @@ -89,8 +89,9 @@ class Cookies implements CookiesInterface, InjectionAwareInterface
* Check if the cookie needs to be updated or
*/
if !fetch cookie, this->_cookies[name] {

let cookie = new Cookie(name, value, expire, path, secure, domain, httpOnly);
let cookie =
<CookieInterface> this->_dependencyInjector->get("Phalcon\\Http\\Cookie",
[name, value, expire, path, secure, domain, httpOnly]);

/**
* Pass the DI to created cookies
Expand Down Expand Up @@ -143,7 +144,7 @@ class Cookies implements CookiesInterface, InjectionAwareInterface
/**
* Gets a cookie from the bag
*/
public function get(string! name) -> <Cookie>
public function get(string! name) -> <CookieInterface>
{
var dependencyInjector, encryption, cookie;

Expand All @@ -154,7 +155,7 @@ class Cookies implements CookiesInterface, InjectionAwareInterface
/**
* Create the cookie if the it does not exist
*/
let cookie = new Cookie(name),
let cookie = <CookieInterface> this->_dependencyInjector->get("Phalcon\\Http\\Cookie", [name]),
dependencyInjector = this->_dependencyInjector;

if typeof dependencyInjector == "object" {
Expand Down

0 comments on commit d69a8e2

Please sign in to comment.