// Normalization
git-svn-id: http://dev.prestashop.com/svn/v1/branches/1.5.x@9576 b9a71923-0436-4b27-9f14-aed3839534dd
This commit is contained in:
1 parent
7151022505
commit
39fcb10404
1 file changed
+69
-69
+69
-69
@@ -25,7 +25,7 @@
|
|||||||
* International Registered Trademark & Property of PrestaShop SA
|
* International Registered Trademark & Property of PrestaShop SA
|
||||||
*/
|
*/
|
||||||
|
|
||||||
class CookieCore
|
class CookieCore
|
||||||
{
|
{
|
||||||
/** @var array Contain cookie content in a key => value format */
|
/** @var array Contain cookie content in a key => value format */
|
||||||
protected $_content;
|
protected $_content;
|
||||||
@@ -54,12 +54,12 @@ class CookieCore
|
|||||||
protected $_modified = false;
|
protected $_modified = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get data if the cookie exists and else initialize an new one
|
* Get data if the cookie exists and else initialize an new one
|
||||||
*
|
*
|
||||||
* @param $name Cookie name before encrypting
|
* @param $name Cookie name before encrypting
|
||||||
* @param $path
|
* @param $path
|
||||||
*/
|
*/
|
||||||
public function __construct($name, $path = '', $expire = NULL)
|
public function __construct($name, $path = '', $expire = null)
|
||||||
{
|
{
|
||||||
$this->_content = array();
|
$this->_content = array();
|
||||||
$this->_expire = isset($expire) ? (int)($expire) : (time() + 1728000);
|
$this->_expire = isset($expire) ? (int)($expire) : (time() + 1728000);
|
||||||
@@ -82,10 +82,10 @@ class CookieCore
|
|||||||
protected function getDomain()
|
protected function getDomain()
|
||||||
{
|
{
|
||||||
$r = '!(?:(\w+)://)?(?:(\w+)\:(\w+)@)?([^/:]+)?(?:\:(\d*))?([^#?]+)?(?:\?([^#]+))?(?:#(.+$))?!i';
|
$r = '!(?:(\w+)://)?(?:(\w+)\:(\w+)@)?([^/:]+)?(?:\:(\d*))?([^#?]+)?(?:\?([^#]+))?(?:#(.+$))?!i';
|
||||||
preg_match ($r, Tools::getHttpHost(false, false), $out);
|
preg_match ($r, Tools::getHttpHost(false, false), $out);
|
||||||
if (preg_match('/^(((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]{1}[0-9]|[1-9]).)'.
|
if (preg_match('/^(((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]{1}[0-9]|[1-9]).)'.
|
||||||
'{1}((25[0-5]|2[0-4][0-9]|[1]{1}[0-9]{2}|[1-9]{1}[0-9]|[0-9]).)'.
|
'{1}((25[0-5]|2[0-4][0-9]|[1]{1}[0-9]{2}|[1-9]{1}[0-9]|[0-9]).)'.
|
||||||
'{2}((25[0-5]|2[0-4][0-9]|[1]{1}[0-9]{2}|[1-9]{1}[0-9]|[0-9]){1}))$/', $out[4]))
|
'{2}((25[0-5]|2[0-4][0-9]|[1]{1}[0-9]{2}|[1-9]{1}[0-9]|[0-9]){1}))$/', $out[4]))
|
||||||
return false;
|
return false;
|
||||||
if (!strstr(Tools::getHttpHost(false, false), '.'))
|
if (!strstr(Tools::getHttpHost(false, false), '.'))
|
||||||
return false;
|
return false;
|
||||||
@@ -93,7 +93,7 @@ class CookieCore
|
|||||||
$subDomains = SubDomain::getSubDomains();
|
$subDomains = SubDomain::getSubDomains();
|
||||||
if ($subDomains === false)
|
if ($subDomains === false)
|
||||||
die(Tools::displayError('Bad SubDomain SQL query.'));
|
die(Tools::displayError('Bad SubDomain SQL query.'));
|
||||||
foreach ($subDomains AS $subDomain)
|
foreach ($subDomains as $subDomain)
|
||||||
{
|
{
|
||||||
$subDomainLength = strlen($subDomain) + 1;
|
$subDomainLength = strlen($subDomain) + 1;
|
||||||
if (strncmp($subDomain.'.', $domain, $subDomainLength) == 0)
|
if (strncmp($subDomain.'.', $domain, $subDomainLength) == 0)
|
||||||
@@ -103,60 +103,60 @@ class CookieCore
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set expiration date
|
* Set expiration date
|
||||||
*
|
*
|
||||||
* @param integer $expire Expiration time from now
|
* @param integer $expire Expiration time from now
|
||||||
*/
|
*/
|
||||||
function setExpire($expire)
|
public function setExpire($expire)
|
||||||
{
|
{
|
||||||
$this->_expire = (int)($expire);
|
$this->_expire = (int)($expire);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Magic method wich return cookie data from _content array
|
* Magic method wich return cookie data from _content array
|
||||||
*
|
*
|
||||||
* @param $key key wanted
|
* @param $key key wanted
|
||||||
* @return string value corresponding to the key
|
* @return string value corresponding to the key
|
||||||
*/
|
*/
|
||||||
public function __get($key)
|
public function __get($key)
|
||||||
{
|
{
|
||||||
return isset($this->_content[$key]) ? $this->_content[$key] : false;
|
return isset($this->_content[$key]) ? $this->_content[$key] : false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Magic method which check if key exists in the cookie
|
* Magic method which check if key exists in the cookie
|
||||||
*
|
*
|
||||||
* @param $key key wanted
|
* @param $key key wanted
|
||||||
* @return boolean key existence
|
* @return boolean key existence
|
||||||
*/
|
*/
|
||||||
public function __isset($key)
|
public function __isset($key)
|
||||||
{
|
{
|
||||||
return isset($this->_content[$key]);
|
return isset($this->_content[$key]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Magic method wich add data into _content array
|
* Magic method wich add data into _content array
|
||||||
*
|
*
|
||||||
* @param $key key desired
|
* @param $key key desired
|
||||||
* @param $value value corresponding to the key
|
* @param $value value corresponding to the key
|
||||||
*/
|
*/
|
||||||
public function __set($key, $value)
|
public function __set($key, $value)
|
||||||
{
|
{
|
||||||
if (is_array($value))
|
if (is_array($value))
|
||||||
die(Tools::displayError());
|
die(Tools::displayError());
|
||||||
if (preg_match('/¤|\|/', $key.$value))
|
if (preg_match('/¤|\|/', $key.$value))
|
||||||
throw new Exception('Forbidden chars in cookie');
|
throw new Exception('Forbidden chars in cookie');
|
||||||
if (!$this->_modified AND (!isset($this->_content[$key]) OR (isset($this->_content[$key]) AND $this->_content[$key] != $value)))
|
if (!$this->_modified && (!isset($this->_content[$key]) || (isset($this->_content[$key]) && $this->_content[$key] != $value)))
|
||||||
$this->_modified = true;
|
$this->_modified = true;
|
||||||
$this->_content[$key] = $value;
|
$this->_content[$key] = $value;
|
||||||
$this->write();
|
$this->write();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Magic method wich delete data into _content array
|
* Magic method wich delete data into _content array
|
||||||
*
|
*
|
||||||
* @param $key key wanted
|
* @param $key key wanted
|
||||||
*/
|
*/
|
||||||
public function __unset($key)
|
public function __unset($key)
|
||||||
{
|
{
|
||||||
if (isset($this->_content[$key]))
|
if (isset($this->_content[$key]))
|
||||||
@@ -174,36 +174,36 @@ class CookieCore
|
|||||||
public function isLogged($withGuest = false)
|
public function isLogged($withGuest = false)
|
||||||
{
|
{
|
||||||
Tools::displayAsDeprecated();
|
Tools::displayAsDeprecated();
|
||||||
if (!$withGuest AND $this->is_guest == 1)
|
if (!$withGuest && $this->is_guest == 1)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
/* Customer is valid only if it can be load and if cookie password is the same as database one */
|
/* Customer is valid only if it can be load and if cookie password is the same as database one */
|
||||||
if ($this->logged == 1 AND $this->id_customer AND Validate::isUnsignedId($this->id_customer) AND Customer::checkPassword((int)($this->id_customer), $this->passwd))
|
if ($this->logged == 1 && $this->id_customer && Validate::isUnsignedId($this->id_customer) && Customer::checkPassword((int)($this->id_customer), $this->passwd))
|
||||||
return true;
|
return true;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check employee informations saved into cookie and return employee validity
|
* Check employee informations saved into cookie and return employee validity
|
||||||
*
|
*
|
||||||
* @deprecated as of version 1.5 use Employee::isLoggedBack() instead
|
* @deprecated as of version 1.5 use Employee::isLoggedBack() instead
|
||||||
* @return boolean employee validity
|
* @return boolean employee validity
|
||||||
*/
|
*/
|
||||||
public function isLoggedBack()
|
public function isLoggedBack()
|
||||||
{
|
{
|
||||||
Tools::displayAsDeprecated();
|
Tools::displayAsDeprecated();
|
||||||
/* Employee is valid only if it can be load and if cookie password is the same as database one */
|
/* Employee is valid only if it can be load and if cookie password is the same as database one */
|
||||||
return ($this->id_employee
|
return ($this->id_employee
|
||||||
AND Validate::isUnsignedId($this->id_employee)
|
&& Validate::isUnsignedId($this->id_employee)
|
||||||
AND Employee::checkPassword((int)$this->id_employee, $this->passwd)
|
&& Employee::checkPassword((int)$this->id_employee, $this->passwd)
|
||||||
AND (!isset($this->_content['remote_addr']) OR $this->_content['remote_addr'] == ip2long(Tools::getRemoteAddr()) OR !Configuration::get('PS_COOKIE_CHECKIP'))
|
&& (!isset($this->_content['remote_addr']) || $this->_content['remote_addr'] == ip2long(Tools::getRemoteAddr()) || !Configuration::get('PS_COOKIE_CHECKIP'))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Delete cookie
|
* Delete cookie
|
||||||
* As of version 1.5 don't call this function, use Customer::logout() or Employee::logout() instead;
|
* As of version 1.5 don't call this function, use Customer::logout() or Employee::logout() instead;
|
||||||
*/
|
*/
|
||||||
public function logout()
|
public function logout()
|
||||||
{
|
{
|
||||||
$this->_content = array();
|
$this->_content = array();
|
||||||
@@ -214,10 +214,10 @@ class CookieCore
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Soft logout, delete everything links to the customer
|
* Soft logout, delete everything links to the customer
|
||||||
* but leave there affiliate's informations.
|
* but leave there affiliate's informations.
|
||||||
* As of version 1.5 don't call this function, use Customer::mylogout() instead;
|
* As of version 1.5 don't call this function, use Customer::mylogout() instead;
|
||||||
*/
|
*/
|
||||||
public function mylogout()
|
public function mylogout()
|
||||||
{
|
{
|
||||||
unset($this->_content['id_customer']);
|
unset($this->_content['id_customer']);
|
||||||
@@ -236,7 +236,7 @@ class CookieCore
|
|||||||
$this->write();
|
$this->write();
|
||||||
}
|
}
|
||||||
|
|
||||||
function makeNewLog()
|
public function makeNewLog()
|
||||||
{
|
{
|
||||||
unset($this->_content['id_customer']);
|
unset($this->_content['id_customer']);
|
||||||
unset($this->_content['id_guest']);
|
unset($this->_content['id_guest']);
|
||||||
@@ -245,9 +245,9 @@ class CookieCore
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get cookie content
|
* Get cookie content
|
||||||
*/
|
*/
|
||||||
function update($nullValues = false)
|
public function update($nullValues = false)
|
||||||
{
|
{
|
||||||
if (isset($_COOKIE[$this->_name]))
|
if (isset($_COOKIE[$this->_name]))
|
||||||
{
|
{
|
||||||
@@ -259,10 +259,10 @@ class CookieCore
|
|||||||
|
|
||||||
/* Unserialize cookie content */
|
/* Unserialize cookie content */
|
||||||
$tmpTab = explode('¤', $content);
|
$tmpTab = explode('¤', $content);
|
||||||
foreach ($tmpTab AS $keyAndValue)
|
foreach ($tmpTab as $keyAndValue)
|
||||||
{
|
{
|
||||||
$tmpTab2 = explode('|', $keyAndValue);
|
$tmpTab2 = explode('|', $keyAndValue);
|
||||||
if (sizeof($tmpTab2) == 2)
|
if (count($tmpTab2) == 2)
|
||||||
$this->_content[$tmpTab2[0]] = $tmpTab2[1];
|
$this->_content[$tmpTab2[0]] = $tmpTab2[1];
|
||||||
}
|
}
|
||||||
/* Blowfish fix */
|
/* Blowfish fix */
|
||||||
@@ -270,7 +270,7 @@ class CookieCore
|
|||||||
$this->_content['checksum'] = (int)($this->_content['checksum']);
|
$this->_content['checksum'] = (int)($this->_content['checksum']);
|
||||||
|
|
||||||
/* Check if cookie has not been modified */
|
/* Check if cookie has not been modified */
|
||||||
if (!isset($this->_content['checksum']) OR $this->_content['checksum'] != $checksum)
|
if (!isset($this->_content['checksum']) || $this->_content['checksum'] != $checksum)
|
||||||
$this->logout();
|
$this->logout();
|
||||||
|
|
||||||
if (!isset($this->_content['date_add']))
|
if (!isset($this->_content['date_add']))
|
||||||
@@ -286,9 +286,9 @@ class CookieCore
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Setcookie according to php version
|
* Setcookie according to php version
|
||||||
*/
|
*/
|
||||||
protected function _setcookie($cookie = NULL)
|
protected function _setcookie($cookie = null)
|
||||||
{
|
{
|
||||||
if ($cookie)
|
if ($cookie)
|
||||||
{
|
{
|
||||||
@@ -315,7 +315,7 @@ class CookieCore
|
|||||||
|
|
||||||
/* Serialize cookie content */
|
/* Serialize cookie content */
|
||||||
if (isset($this->_content['checksum'])) unset($this->_content['checksum']);
|
if (isset($this->_content['checksum'])) unset($this->_content['checksum']);
|
||||||
foreach ($this->_content AS $key => $value)
|
foreach ($this->_content as $key => $value)
|
||||||
$cookie .= $key.'|'.$value.'¤';
|
$cookie .= $key.'|'.$value.'¤';
|
||||||
|
|
||||||
/* Add checksum to cookie */
|
/* Add checksum to cookie */
|
||||||
@@ -333,7 +333,7 @@ class CookieCore
|
|||||||
$result = array();
|
$result = array();
|
||||||
if (count($this->_content) == 0)
|
if (count($this->_content) == 0)
|
||||||
return $result;
|
return $result;
|
||||||
foreach ($this->_content AS $key => $value)
|
foreach ($this->_content as $key => $value)
|
||||||
if (strncmp($key, $origin, strlen($origin)) == 0)
|
if (strncmp($key, $origin, strlen($origin)) == 0)
|
||||||
$result[$key] = $value;
|
$result[$key] = $value;
|
||||||
return $result;
|
return $result;
|
||||||
@@ -345,7 +345,7 @@ class CookieCore
|
|||||||
public function unsetFamily($origin)
|
public function unsetFamily($origin)
|
||||||
{
|
{
|
||||||
$family = $this->getFamily($origin);
|
$family = $this->getFamily($origin);
|
||||||
foreach (array_keys($family) AS $member)
|
foreach (array_keys($family) as $member)
|
||||||
unset($this->$member);
|
unset($this->$member);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user