From f45caf216f93132cba4e961089ab5737e037893a Mon Sep 17 00:00:00 2001 From: Regis Houssin Date: Sat, 23 May 2009 17:44:36 +0000 Subject: [PATCH] Add: just used cookie for remind last user and last entity Look: use cookie for view logo of last used entity --- htdocs/core/cookie.class.php | 82 +++++++++++++++++++++++++++++++++++- htdocs/lib/security.lib.php | 2 +- htdocs/main.inc.php | 2 +- htdocs/master.inc.php | 20 +++++++++ 4 files changed, 103 insertions(+), 3 deletions(-) diff --git a/htdocs/core/cookie.class.php b/htdocs/core/cookie.class.php index 846d8bf3576..118fe649c3b 100644 --- a/htdocs/core/cookie.class.php +++ b/htdocs/core/cookie.class.php @@ -126,7 +126,87 @@ return $decryptValue; } + + /** + * \brief Add cookie cryptkey in config file + * \return int <0 if KO, >0 if OK + */ + function add_cookiecryptkeyconf() + { + dol_syslog("cookie.class::add_cookiecryptkeyconf", LOG_DEBUG); + $config = ''; + $added=0; + + if ($fp = fopen(DOL_DOCUMENT_ROOT.'/conf/conf.php','r')) + { + while(!feof($fp)) + { + $buffer = fgets($fp,4096); + + if (strstr($buffer,"\$dolibarr_main_cookie_cryptkey")) + { + $config .= "\$dolibarr_main_cookie_cryptkey=\"$this->myKey\";\n"; + $added++; + } + else + { + $config .= $buffer; + } + } + fclose($fp); + + if (!$added) + { + $config = ''; + + if ($fp = fopen(DOL_DOCUMENT_ROOT.'/conf/conf.php','r')) + { + while(!feof($fp)) + { + $buffer = fgets($fp,4096); + + if (strstr($buffer,"\$dolibarr_main_authentication")) + { + $config .= $buffer; + $config .= "\$dolibarr_main_cookie_cryptkey=\"$this->myKey\";\n"; + } + else + { + $config .= $buffer; + } + } + fclose($fp); + } + else + { + dol_syslog("cookie.class::add_cookiecryptkeyconf Failed to read conf.php", LOG_ERR); + return -2; + } + } + + $file=DOL_DOCUMENT_ROOT.'/conf/conf.php'; + if ($fp = @fopen($file,'w')) + { + fputs($fp, $config, strlen($config)); + fclose($fp); + // It's config file, so we set permission for creator only + // @chmod($file, octdec('0600')); + + return 1; + } + else + { + dol_syslog("cookie.class::add_cookiecryptkeyconf Failed to open conf.php file for writing", LOG_WARNING); + return -1; + } + } + else + { + dol_syslog("cookie.class::add_cookiecryptkeyconf Failed to read conf.php", LOG_ERR); + return -2; + } + } } -?> +?> \ No newline at end of file diff --git a/htdocs/lib/security.lib.php b/htdocs/lib/security.lib.php index fb41ad002de..8907cb23d09 100644 --- a/htdocs/lib/security.lib.php +++ b/htdocs/lib/security.lib.php @@ -141,7 +141,7 @@ function dol_loginfunction($langs,$conf,$mysoc) { include_once(DOL_DOCUMENT_ROOT . "/core/cookie.class.php"); - $cryptkey = (! empty($conf->global->MAIN_MULTICOMPANY_COOKIE_CRYPTKEY) ? $conf->global->MAIN_MULTICOMPANY_COOKIE_CRYPTKEY : '' ); + $cryptkey = ( isset($conf->cookie->cryptkey) ? $conf->cookie->cryptkey : '' ); $entityCookie = new DolCookie($cryptkey); $cookieValue = $entityCookie->_getCookie($entityCookieName); diff --git a/htdocs/main.inc.php b/htdocs/main.inc.php index 535373c7571..2133c80a7b0 100644 --- a/htdocs/main.inc.php +++ b/htdocs/main.inc.php @@ -450,7 +450,7 @@ if (! isset($_SESSION["dol_login"])) // TTL : sera defini dans la page de config multicompany $ttl = (! empty($conf->global->MAIN_MULTICOMPANY_COOKIE_TTL) ? $conf->global->MAIN_MULTICOMPANY_COOKIE_TTL : time()+60*60*8 ); // Cryptkey : sera cree aleatoirement dans la page de config multicompany - $cryptkey = (! empty($conf->global->MAIN_MULTICOMPANY_COOKIE_CRYPTKEY) ? $conf->global->MAIN_MULTICOMPANY_COOKIE_CRYPTKEY : '' ); + $cryptkey = ( isset($conf->cookie->cryptkey) ? $conf->cookie->cryptkey : '' ); $entityCookie = new DolCookie($cryptkey); $entityCookie->_setCookie($entityCookieName, $entity, $ttl); diff --git a/htdocs/master.inc.php b/htdocs/master.inc.php index 5601a24735a..e87cd764ccc 100644 --- a/htdocs/master.inc.php +++ b/htdocs/master.inc.php @@ -136,6 +136,11 @@ $conf->file->main_force_https = empty($dolibarr_main_force_https)?'':$dolibarr_m // Define charset for HTML Output (can set hidden value force_charset in conf.php file) if (empty($force_charset_do_notuse)) $force_charset_do_notuse='UTF-8'; $conf->file->character_set_client=strtoupper($force_charset_do_notuse); +// Cookie cryptkey +if (! empty($dolibarr_main_cookie_cryptkey)) +{ + $conf->cookie->cryptkey = $dolibarr_main_cookie_cryptkey; +} // Define array of document root directories $conf->file->dol_document_root=array(DOL_DOCUMENT_ROOT); @@ -205,6 +210,8 @@ if (! defined('NOREQUIREUSER')) */ if (! defined('NOREQUIREDB')) { + $entityCookieName = 'DOLENTITYID_'.md5($_SERVER["SERVER_NAME"].$_SERVER["DOCUMENT_ROOT"]); + if (session_id() && isset($_SESSION["dol_entity"])) // Entity inside an opened session { $conf->entity = $_SESSION["dol_entity"]; @@ -217,6 +224,19 @@ if (! defined('NOREQUIREDB')) { $conf->entity = $_POST["entity"]; } + elseif (isset($_COOKIE[$entityCookieName]) && isset($conf->cookie->cryptkey)) // Just for view specific login page + { + include_once(DOL_DOCUMENT_ROOT."/core/cookie.class.php"); + + $lastuser = ''; + $lastentity = ''; + + $entityCookie = new DolCookie($conf->cookie->cryptkey); + $cookieValue = $entityCookie->_getCookie($entityCookieName); + list($lastuser, $lastentity) = split('\|', $cookieValue); + $conf->entity = $lastentity; + } + $conf->setValues($db); }