2009-05-08 21:46:07 +02:00
|
|
|
<?php
|
2015-06-27 19:57:35 +02:00
|
|
|
/* Copyright (C) 2009-2015 Regis Houssin <regis.houssin@capnetworks.com>
|
2009-05-08 21:46:07 +02:00
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
2013-01-16 15:36:08 +01:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2009-05-08 21:46:07 +02:00
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2011-08-01 01:45:11 +02:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2009-05-08 21:46:07 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
2010-06-08 01:52:43 +02:00
|
|
|
* \file htdocs/core/class/cookie.class.php
|
2010-04-21 20:40:18 +02:00
|
|
|
* \ingroup core
|
|
|
|
|
* \brief File of class to manage cookies
|
2009-05-08 21:46:07 +02:00
|
|
|
*/
|
|
|
|
|
|
2010-07-21 13:57:52 +02:00
|
|
|
|
|
|
|
|
/**
|
2015-06-27 21:26:05 +02:00
|
|
|
* Class to manage cookies.
|
|
|
|
|
* This class is used by external module multicompany but will be removed soon only and must not be used by
|
|
|
|
|
*
|
|
|
|
|
* @deprecated PHP already provide function to read/store a cookie. No need to use a dedicated class. Also storing sensitive information into cookie is forbidden, so encryption is useless.
|
|
|
|
|
* If a data is sensitive, it must be stored into database (if we need a long term retention) or into session.
|
2010-07-21 13:57:52 +02:00
|
|
|
*/
|
2010-04-21 20:40:18 +02:00
|
|
|
class DolCookie
|
|
|
|
|
{
|
2015-06-27 20:32:19 +02:00
|
|
|
private $_myKey;
|
|
|
|
|
private $_iv;
|
2015-06-27 19:57:35 +02:00
|
|
|
|
2010-04-21 20:40:18 +02:00
|
|
|
var $myCookie;
|
|
|
|
|
var $myValue;
|
|
|
|
|
var $myExpire;
|
|
|
|
|
var $myPath;
|
|
|
|
|
var $myDomain;
|
2014-04-23 14:26:57 +02:00
|
|
|
var $mySecure;
|
2010-04-21 20:40:18 +02:00
|
|
|
var $cookie;
|
|
|
|
|
|
|
|
|
|
/**
|
2015-09-24 20:35:50 +02:00
|
|
|
* Constructor
|
2011-09-11 20:35:38 +02:00
|
|
|
*
|
2015-09-24 20:35:50 +02:00
|
|
|
* @param string $key Personnal key
|
|
|
|
|
* @deprecated
|
2010-04-21 20:40:18 +02:00
|
|
|
*/
|
2012-07-30 17:17:33 +02:00
|
|
|
function __construct($key = '')
|
2010-04-21 20:40:18 +02:00
|
|
|
{
|
2015-06-27 20:32:19 +02:00
|
|
|
$this->_myKey = hash('sha256', $key, TRUE);
|
|
|
|
|
$this->_iv = md5(md5($this->_myKey));
|
2010-04-21 20:40:18 +02:00
|
|
|
$this->cookie = "";
|
|
|
|
|
$this->myCookie = "";
|
|
|
|
|
$this->myValue = "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2011-09-21 15:05:32 +02:00
|
|
|
* Encrypt en create the cookie
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
2010-04-21 20:40:18 +02:00
|
|
|
*/
|
2015-06-27 20:37:22 +02:00
|
|
|
private function _cryptCookie()
|
2010-04-21 20:40:18 +02:00
|
|
|
{
|
2015-06-27 20:32:19 +02:00
|
|
|
if (!empty($this->_myKey) && !empty($this->_iv))
|
2010-04-21 20:40:18 +02:00
|
|
|
{
|
|
|
|
|
$valuecrypt = base64_encode($this->myValue);
|
2015-06-27 20:32:19 +02:00
|
|
|
$this->cookie = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $this->_myKey, $valuecrypt, MCRYPT_MODE_CBC, $this->_iv));
|
2010-04-21 20:40:18 +02:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
$this->cookie = $this->myValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setcookie($this->myCookie, $this->cookie, $this->myExpire, $this->myPath, $this->myDomain, $this->mySecure);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2011-09-21 15:05:32 +02:00
|
|
|
* Decrypt the cookie
|
|
|
|
|
*
|
2014-04-23 14:27:17 +02:00
|
|
|
* @return string
|
2010-04-21 20:40:18 +02:00
|
|
|
*/
|
2015-06-27 20:37:22 +02:00
|
|
|
private function _decryptCookie()
|
2010-04-21 20:40:18 +02:00
|
|
|
{
|
2015-06-27 20:32:19 +02:00
|
|
|
if (!empty($this->_myKey) && !empty($this->_iv))
|
2010-04-21 20:40:18 +02:00
|
|
|
{
|
2015-06-27 19:57:35 +02:00
|
|
|
$this->cookie = $_COOKIE[$this->myCookie];
|
2015-06-27 20:32:19 +02:00
|
|
|
$this->myValue = trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $this->_myKey, base64_decode($this->cookie), MCRYPT_MODE_CBC, $this->_iv));
|
2010-04-21 20:40:18 +02:00
|
|
|
|
2011-09-20 15:32:16 +02:00
|
|
|
return(base64_decode($this->myValue));
|
2010-04-21 20:40:18 +02:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return($_COOKIE[$this->myCookie]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2011-09-21 15:05:32 +02:00
|
|
|
* Set and create the cookie
|
2011-09-12 19:08:02 +02:00
|
|
|
*
|
2011-09-21 15:05:32 +02:00
|
|
|
* @param string $cookie Cookie name
|
|
|
|
|
* @param string $value Cookie value
|
2015-03-06 03:10:01 +01:00
|
|
|
* @param integer $expire Expiration
|
2011-09-21 15:05:32 +02:00
|
|
|
* @param string $path Path of cookie
|
2012-02-20 10:09:28 +01:00
|
|
|
* @param string $domain Domain name
|
2011-09-21 15:05:32 +02:00
|
|
|
* @param int $secure 0 or 1
|
|
|
|
|
* @return void
|
2010-04-21 20:40:18 +02:00
|
|
|
*/
|
2015-06-27 20:37:22 +02:00
|
|
|
public function setCookie($cookie, $value, $expire=0, $path="/", $domain="", $secure=0)
|
2010-04-21 20:40:18 +02:00
|
|
|
{
|
|
|
|
|
$this->myCookie = $cookie;
|
|
|
|
|
$this->myValue = $value;
|
|
|
|
|
$this->myExpire = $expire;
|
|
|
|
|
$this->myPath = $path;
|
|
|
|
|
$this->myDomain = $domain;
|
2010-09-26 11:53:42 +02:00
|
|
|
$this->mySecure = $secure;
|
2010-04-21 20:40:18 +02:00
|
|
|
|
|
|
|
|
//print 'key='.$this->myKey.' name='.$this->myCookie.' value='.$this->myValue.' expire='.$this->myExpire;
|
|
|
|
|
|
2015-06-27 20:37:22 +02:00
|
|
|
$this->_cryptCookie();
|
2010-04-21 20:40:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2011-09-12 19:08:02 +02:00
|
|
|
* Get the cookie
|
|
|
|
|
*
|
|
|
|
|
* @param string $cookie Cookie name
|
|
|
|
|
* @return string Decrypted value
|
2010-04-21 20:40:18 +02:00
|
|
|
*/
|
2015-06-27 20:37:22 +02:00
|
|
|
public function getCookie($cookie)
|
2010-04-21 20:40:18 +02:00
|
|
|
{
|
|
|
|
|
$this->myCookie = $cookie;
|
|
|
|
|
|
2015-06-27 20:37:22 +02:00
|
|
|
$decryptValue = $this->_decryptCookie();
|
2010-04-21 20:40:18 +02:00
|
|
|
|
|
|
|
|
return $decryptValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2009-05-08 21:46:07 +02:00
|
|
|
|