This commit is contained in:
Laurent Destailleur 2010-04-21 18:40:18 +00:00
parent 5490f71326
commit ba479b95f0

View File

@ -17,196 +17,195 @@
*/ */
/** /**
\file htdocs/core/cookie.class.php * \file htdocs/core/cookie.class.php
\ingroup core * \ingroup core
\version $Id$ * \version $Id$
\brief File of class to manage cookies * \brief File of class to manage cookies
*/ */
class DolCookie
{
var $myKey;
var $myCookie;
var $myValue;
var $myExpire;
var $myPath;
var $myDomain;
var $mySsecure;
var $cookiearray;
var $cookie;
class DolCookie /**
{ * \brief Constructor
var $myKey; * \param key Personnal key
var $myCookie; */
var $myValue; function DolCookie($key = '')
var $myExpire; {
var $myPath; $this->myKey = $key;
var $myDomain; $this->cookiearray = array();
var $mySsecure; $this->cookie = "";
var $cookiearray; $this->myCookie = "";
var $cookie; $this->myValue = "";
}
/**
* \brief Constructor
* \param key Personnal key
*/
function DolCookie($key = '')
{
$this->myKey = $key;
$this->cookiearray = array();
$this->cookie = "";
$this->myCookie = "";
$this->myValue = "";
}
/** /**
* \brief Encrypt en create the cookie * \brief Encrypt en create the cookie
*/ */
function cryptCookie() function cryptCookie()
{ {
if (!empty($this->myKey)) if (!empty($this->myKey))
{ {
$valuecrypt = base64_encode($this->myValue); $valuecrypt = base64_encode($this->myValue);
for ($f=0 ; $f<=strlen($valuecrypt)-1; $f++) for ($f=0 ; $f<=strlen($valuecrypt)-1; $f++)
{ {
$this->cookie .= intval(ord($valuecrypt[$f]))*$this->myKey."|"; $this->cookie .= intval(ord($valuecrypt[$f]))*$this->myKey."|";
} }
} }
else else
{ {
$this->cookie = $this->myValue; $this->cookie = $this->myValue;
} }
setcookie($this->myCookie, $this->cookie, $this->myExpire, $this->myPath, $this->myDomain, $this->mySecure); setcookie($this->myCookie, $this->cookie, $this->myExpire, $this->myPath, $this->myDomain, $this->mySecure);
} }
/** /**
* \brief Decrypt the cookie * \brief Decrypt the cookie
*/ */
function decryptCookie() function decryptCookie()
{ {
if (!empty($this->myKey)) if (!empty($this->myKey))
{ {
$this->cookiearray = explode("|",$_COOKIE[$this->myCookie]); $this->cookiearray = explode("|",$_COOKIE[$this->myCookie]);
$this->myValue = "" ; $this->myValue = "" ;
for ($f=0 ; $f<=count($this->cookiearray)-2; $f++) for ($f=0 ; $f<=count($this->cookiearray)-2; $f++)
{ {
$this->myValue .= strval(chr($this->cookiearray[$f]/$this->myKey)); $this->myValue .= strval(chr($this->cookiearray[$f]/$this->myKey));
} }
return(base64_decode($this->myValue)) ; return(base64_decode($this->myValue)) ;
} }
else else
{ {
return($_COOKIE[$this->myCookie]); return($_COOKIE[$this->myCookie]);
} }
} }
/** /**
* \brief Set and create the cookie * \brief Set and create the cookie
* \param cookie Cookie name * \param cookie Cookie name
* \param value Cookie value * \param value Cookie value
*/ */
function _setCookie($cookie, $value, $expire=0, $path="/", $domain="", $secure=0) function _setCookie($cookie, $value, $expire=0, $path="/", $domain="", $secure=0)
{ {
$this->myCookie = $cookie; $this->myCookie = $cookie;
$this->myValue = $value; $this->myValue = $value;
$this->myExpire = $expire; $this->myExpire = $expire;
$this->myPath = $path; $this->myPath = $path;
$this->myDomain = $domain; $this->myDomain = $domain;
$this->mySsecure = $secure; $this->mySsecure = $secure;
//print 'key='.$this->myKey.' name='.$this->myCookie.' value='.$this->myValue.' expire='.$this->myExpire; //print 'key='.$this->myKey.' name='.$this->myCookie.' value='.$this->myValue.' expire='.$this->myExpire;
$this->cryptCookie(); $this->cryptCookie();
} }
/** /**
* \brief Get the cookie * \brief Get the cookie
* \param cookie Cookie name * \param cookie Cookie name
* \param value Cookie value * \param value Cookie value
* \return decryptValue Decrypted value * \return decryptValue Decrypted value
*/ */
function _getCookie($cookie) function _getCookie($cookie)
{ {
$this->myCookie = $cookie; $this->myCookie = $cookie;
$decryptValue = $this->decryptCookie(); $decryptValue = $this->decryptCookie();
return $decryptValue; return $decryptValue;
} }
/** /**
* \brief Add cookie cryptkey in config file * \brief Add cookie cryptkey in config file
* \return int <0 if KO, >0 if OK * \return int <0 if KO, >0 if OK
*/ */
function add_cookiecryptkeyconf() function add_cookiecryptkeyconf()
{ {
dol_syslog("cookie.class::add_cookiecryptkeyconf", LOG_DEBUG); dol_syslog("cookie.class::add_cookiecryptkeyconf", LOG_DEBUG);
$config = ''; $config = '';
$added=0; $added=0;
if ($fp = fopen(DOL_DOCUMENT_ROOT.'/conf/conf.php','r')) if ($fp = fopen(DOL_DOCUMENT_ROOT.'/conf/conf.php','r'))
{ {
while(!feof($fp)) while(!feof($fp))
{ {
$buffer = fgets($fp,4096); $buffer = fgets($fp,4096);
if (strstr($buffer,"\$dolibarr_main_cookie_cryptkey")) if (strstr($buffer,"\$dolibarr_main_cookie_cryptkey"))
{ {
$config .= "\$dolibarr_main_cookie_cryptkey=\"$this->myKey\";\n"; $config .= "\$dolibarr_main_cookie_cryptkey=\"$this->myKey\";\n";
$added++; $added++;
} }
else else
{ {
$config .= $buffer; $config .= $buffer;
} }
} }
fclose($fp); fclose($fp);
if (!$added) if (!$added)
{ {
$config = ''; $config = '';
if ($fp = fopen(DOL_DOCUMENT_ROOT.'/conf/conf.php','r')) if ($fp = fopen(DOL_DOCUMENT_ROOT.'/conf/conf.php','r'))
{ {
while(!feof($fp)) while(!feof($fp))
{ {
$buffer = fgets($fp,4096); $buffer = fgets($fp,4096);
if (strstr($buffer,"\$dolibarr_main_authentication")) if (strstr($buffer,"\$dolibarr_main_authentication"))
{ {
$config .= $buffer; $config .= $buffer;
$config .= "\$dolibarr_main_cookie_cryptkey=\"$this->myKey\";\n"; $config .= "\$dolibarr_main_cookie_cryptkey=\"$this->myKey\";\n";
} }
else else
{ {
$config .= $buffer; $config .= $buffer;
} }
} }
fclose($fp); fclose($fp);
} }
else else
{ {
dol_syslog("cookie.class::add_cookiecryptkeyconf Failed to read conf.php", LOG_ERR); dol_syslog("cookie.class::add_cookiecryptkeyconf Failed to read conf.php", LOG_ERR);
return -2; return -2;
} }
} }
$file=DOL_DOCUMENT_ROOT.'/conf/conf.php'; $file=DOL_DOCUMENT_ROOT.'/conf/conf.php';
if ($fp = @fopen($file,'w')) if ($fp = @fopen($file,'w'))
{ {
fputs($fp, $config, strlen($config)); fputs($fp, $config, strlen($config));
fclose($fp); fclose($fp);
// It's config file, so we set permission for creator only // It's config file, so we set permission for creator only
// @chmod($file, octdec('0600')); // @chmod($file, octdec('0600'));
return 1; return 1;
} }
else else
{ {
dol_syslog("cookie.class::add_cookiecryptkeyconf Failed to open conf.php file for writing", LOG_WARNING); dol_syslog("cookie.class::add_cookiecryptkeyconf Failed to open conf.php file for writing", LOG_WARNING);
return -1; return -1;
} }
} }
else else
{ {
dol_syslog("cookie.class::add_cookiecryptkeyconf Failed to read conf.php", LOG_ERR); dol_syslog("cookie.class::add_cookiecryptkeyconf Failed to read conf.php", LOG_ERR);
return -2; return -2;
} }
} }
} }
?> ?>