From 87bf09995deb3b9e9988d317f2e5097a72e0cfad Mon Sep 17 00:00:00 2001 From: Romain Quetiez Date: Thu, 12 Jan 2012 14:46:49 +0000 Subject: [PATCH] Updated session mechanisms: itop_env defaults to 'production', and adapted the setup to create the configuration SVN:trunk[1762] --- application/startup.inc.php | 6 ++-- application/utils.inc.php | 27 +++++++++++++-- approot.inc.php | 4 +++ core/action.class.inc.php | 2 +- core/config.class.inc.php | 21 ++++++++---- core/email.class.inc.php | 2 +- core/metamodel.class.php | 19 ++++++----- index.php | 2 +- pages/logoff.php | 2 -- setup/ajax.dataloader.php | 8 ++--- setup/index.php | 65 +++++++++++++++++++------------------ 11 files changed, 98 insertions(+), 60 deletions(-) diff --git a/application/startup.inc.php b/application/startup.inc.php index 307b0cb5a..51ae8658b 100644 --- a/application/startup.inc.php +++ b/application/startup.inc.php @@ -29,12 +29,14 @@ session_name('itop-'.md5(APPROOT)); session_start(); if (isset($_SESSION['itop_env'])) { - $sConfigFile = $_SESSION['itop_env']; + $sEnv = $_SESSION['itop_env']; } else { - $sConfigFile = ITOP_CONFIG_FILE; + $sEnv = ITOP_DEFAULT_ENV; + $_SESSION['itop_env'] = ITOP_DEFAULT_ENV; } +$sConfigFile = APPCONF.$sEnv.'/'.ITOP_CONFIG_FILE; MetaModel::Startup($sConfigFile); ?> diff --git a/application/utils.inc.php b/application/utils.inc.php index a91996b3f..754697f24 100644 --- a/application/utils.inc.php +++ b/application/utils.inc.php @@ -26,7 +26,9 @@ require_once(APPROOT.'/core/config.class.inc.php'); require_once(APPROOT.'/application/transaction.class.inc.php'); -define('ITOP_CONFIG_FILE', APPROOT.'/config-itop.php'); +define('ITOP_CONFIG_FILE', 'config-itop.php'); +define('ITOP_DEFAULT_CONFIG_FILE', APPCONF.ITOP_DEFAULT_ENV.'/'.ITOP_CONFIG_FILE); + define('SERVER_NAME_PLACEHOLDER', '$SERVER_NAME$'); class FileUploadException extends Exception @@ -40,7 +42,6 @@ class FileUploadException extends Exception */ class utils { - private static $m_sConfigFile = ITOP_CONFIG_FILE; private static $m_oConfig = null; private static $m_bCASClient = false; @@ -710,6 +711,28 @@ class utils return array($iRes, $aOutput); } + /** + * Get the current environment + */ + public static function GetCurrentEnvironment() + { + if (isset($_SESSION['itop_env'])) + { + return $_SESSION['itop_env']; + } + else + { + return ITOP_DEFAULT_ENV; + } + } + + /** + * Get target configuration file name (including full path) + */ + public static function GetConfigFilePath() + { + return APPCONF.self::GetCurrentEnvironment().'/'.ITOP_CONFIG_FILE; + } } ?> diff --git a/approot.inc.php b/approot.inc.php index 86d523b0b..e5cd6d0f2 100644 --- a/approot.inc.php +++ b/approot.inc.php @@ -1,5 +1,9 @@ Get('body'), $aContextArgs); $oObj = $aContextArgs['this->object()']; - $sMessageId = sprintf('iTop_%s_%d_%f@%s.openitop.org', get_class($oObj), $oObj->GetKey(), microtime(true /* get as float*/), MetaModel::GetConfig()->Get('session_name')); + $sMessageId = sprintf('iTop_%s_%d_%f@%s.openitop.org', get_class($oObj), $oObj->GetKey(), microtime(true /* get as float*/), MetaModel::GetEnvironmentId()); $sReference = '<'.$sMessageId.'>'; } catch(Exception $e) diff --git a/core/config.class.inc.php b/core/config.class.inc.php index ad326c45e..bf0786d8f 100644 --- a/core/config.class.inc.php +++ b/core/config.class.inc.php @@ -609,9 +609,14 @@ class Config */ protected $m_aCharsets; - public function __construct($sConfigFile, $bLoadConfig = true) + public function __construct($sConfigFile = null, $bLoadConfig = true) { $this->m_sFile = $sConfigFile; + if (is_null($sConfigFile)) + { + $bLoadConfig = false; + } + $this->m_aAppModules = array( // Some default modules, always present can be move to an official iTop Module later if needed 'application/transaction.class.inc.php', @@ -1108,13 +1113,17 @@ class Config { $this->m_aCharsets[$sIconvCode] = $sDisplayName; } - public function FileIsWritable() - { - return is_writable($this->m_sFile); - } + public function GetLoadedFile() { - return $this->m_sFile; + if (is_null($this->m_sFile)) + { + return ''; + } + else + { + return $this->m_sFile; + } } /** diff --git a/core/email.class.inc.php b/core/email.class.inc.php index 811b33ef4..f3e446081 100644 --- a/core/email.class.inc.php +++ b/core/email.class.inc.php @@ -37,7 +37,7 @@ class EMail { protected static $m_oConfig = null; - public function LoadConfig($sConfigFile = ITOP_CONFIG_FILE) + public function LoadConfig($sConfigFile = ITOP_DEFAULT_CONFIG_FILE) { if (is_null(self::$m_oConfig)) { diff --git a/core/metamodel.class.php b/core/metamodel.class.php index ad670092b..c855a6a87 100644 --- a/core/metamodel.class.php +++ b/core/metamodel.class.php @@ -1973,8 +1973,7 @@ if (!array_key_exists($sAttCode, self::$m_aAttribDefs[$sClass])) { // Note: For versions of APC older than 3.0.17, fetch() accepts only one parameter // - $sAppIdentity = self::GetConfig()->Get('session_name'); - $sOqlAPCCacheId = $sAppIdentity.'-query-cache-'.$sOqlId; + $sOqlAPCCacheId = 'itop-'.MetaModel::GetEnvironmentId().'-query-cache-'.$sOqlId; $oKPI = new ExecutionKPI(); $result = apc_fetch($sOqlAPCCacheId); $oKPI->ComputeStats('Query APC (fetch)', $sOqlQuery); @@ -4126,7 +4125,7 @@ if (!array_key_exists($sAttCode, self::$m_aAttribDefs[$sClass])) // Note: load the dictionary as soon as possible, because it might be // needed when some error occur - $sAppIdentity = self::GetConfig()->Get('session_name'); + $sAppIdentity = 'itop-'.MetaModel::GetEnvironmentId(); if (!self::$m_bUseAPCCache || !Dict::InCache($sAppIdentity)) { foreach (self::$m_oConfig->GetDictionaries() as $sModule => $sToInclude) @@ -4175,8 +4174,7 @@ if (!array_key_exists($sAttCode, self::$m_aAttribDefs[$sClass])) $oKPI = new ExecutionKPI(); // Note: For versions of APC older than 3.0.17, fetch() accepts only one parameter // - $sAppIdentity = self::GetConfig()->Get('session_name'); - $sOqlAPCCacheId = $sAppIdentity.'-metamodel'; + $sOqlAPCCacheId = 'itop-'.MetaModel::GetEnvironmentId().'-metamodel'; $result = apc_fetch($sOqlAPCCacheId); if (is_array($result)) @@ -4259,6 +4257,11 @@ if (!array_key_exists($sAttCode, self::$m_aAttribDefs[$sClass])) return self::$m_oConfig; } + public static function GetEnvironmentId() + { + return md5(APPROOT).'-'.utils::GetCurrentEnvironment(); + } + protected static $m_aExtensionClasses = array(); protected static function IncludeModule($sModuleType, $sToInclude) @@ -4663,10 +4666,8 @@ if (!array_key_exists($sAttCode, self::$m_aAttribDefs[$sClass])) { $oConfig = self::GetConfig(); } - $sAppIdentity = $oConfig->Get('session_name'); - $aCacheUserData = apc_cache_info('user'); - $sPrefix = $sAppIdentity.'-'; + $sPrefix = 'itop-'.MetaModel::GetEnvironmentId().'-'; $aEntries = array(); foreach($aCacheUserData['cache_list'] as $i => $aEntry) @@ -4688,8 +4689,8 @@ if (!array_key_exists($sAttCode, self::$m_aAttribDefs[$sClass])) { $oConfig = self::GetConfig(); } - $sAppIdentity = $oConfig->Get('session_name'); + $sAppIdentity = 'itop-'.MetaModel::GetEnvironmentId(); Dict::ResetCache($sAppIdentity); foreach(self::GetCacheEntries($oConfig) as $sKey => $aAPCInfo) diff --git a/index.php b/index.php index 21ce9f3d3..d0077b885 100644 --- a/index.php +++ b/index.php @@ -1,5 +1,5 @@ Get('session_name')); -session_start(); $bPortal = utils::ReadParam('portal', false); $sUrl = utils::GetAbsoluteUrlAppRoot(); if ($bPortal) diff --git a/setup/ajax.dataloader.php b/setup/ajax.dataloader.php index 0d13659d9..5e17547cd 100644 --- a/setup/ajax.dataloader.php +++ b/setup/ajax.dataloader.php @@ -121,8 +121,6 @@ require_once(APPROOT.'/core/kpi.class.inc.php'); require_once(APPROOT.'/core/cmdbsource.class.inc.php'); require_once('./xmldataloader.class.inc.php'); -define('TMP_CONFIG_FILE', APPROOT.'/tmp-config-itop.php'); -//define('FINAL_CONFIG_FILE', APPROOT.'/config-itop.php'); // Never cache this page header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1 @@ -202,7 +200,7 @@ try // SetupPage::log_info("Update Database Schema."); - $oConfig = new Config(TMP_CONFIG_FILE, false /* Don't try to load it */); + $oConfig = new Config(); $aParamValues = array( 'db_server' => utils::ReadParam('db_server', '', false, 'raw_data'), @@ -231,7 +229,7 @@ try // SetupPage::log_info('After Database Creation'); - $oConfig = new Config(TMP_CONFIG_FILE, false /* Don't try to load it */); + $oConfig = new Config(); $aParamValues = array( 'db_server' => utils::ReadParam('db_server', '', false, 'raw_data'), @@ -301,7 +299,7 @@ try throw(new Exception("File $sFileName does not exist")); } - $oConfig = new Config(TMP_CONFIG_FILE, false /* Don't try to load it */); + $oConfig = new Config(); $aParamValues = array( 'db_server' => utils::ReadParam('db_server', '', false, 'raw_data'), diff --git a/setup/index.php b/setup/index.php index 0d9bf84af..4c72b8962 100644 --- a/setup/index.php +++ b/setup/index.php @@ -32,8 +32,6 @@ require_once(APPROOT.'/core/cmdbsource.class.inc.php'); require_once(APPROOT.'/setup/setuppage.class.inc.php'); require_once(APPROOT.'/setup/moduleinstaller.class.inc.php'); -define('TMP_CONFIG_FILE', APPROOT.'/tmp-config-itop.php'); -define('FINAL_CONFIG_FILE', APPROOT.'/config-itop.php'); define('PHP_MIN_VERSION', '5.2.0'); define('MYSQL_MIN_VERSION', '5.0.0'); define('MIN_MEMORY_LIMIT', 32*1024*1024); @@ -589,10 +587,12 @@ function WelcomeAndCheckPrerequisites(SetupPage $oP, $aParamValues, $iCurrentSte $oP->add("
\n"); $sMode = 'install'; // Fresh install + $sConfigFile = utils::GetConfigFilePath(); + // Check for a previous version - if (file_exists(FINAL_CONFIG_FILE)) + if (file_exists($sConfigFile)) { - $oConfig = new Config(FINAL_CONFIG_FILE); + $oConfig = new Config($sConfigFile); $aVersion = AnalyzeInstallation($oConfig, $aParamValues['source_dir']); if (!empty($aVersion[ROOT_MODULE]['version_db'])) @@ -641,7 +641,7 @@ function WelcomeAndCheckPrerequisites(SetupPage $oP, $aParamValues, $iCurrentSte //AddHiddenParam($oP, 'source_dir', 'datamodel'); AddHiddenParam($oP, 'source_dir', 'modules'); // not ready for the big bang ? - AddHiddenParam($oP, 'target_dir', 'env-production'); + AddHiddenParam($oP, 'target_dir', 'env-'.utils::GetCurrentEnvironment()); $aPreviousParams = array('mode', 'source_dir', 'target_dir'); AddParamsToForm($oP, $aParamValues, $aPreviousParams); @@ -667,7 +667,8 @@ function LicenceAcknowledgement($oP, $aParamValues, $iCurrentStep) $sChecked = $aParamValues['licence_ok'] == 1 ? 'checked' : ''; $oP->add("

\n"); - if (file_exists(FINAL_CONFIG_FILE)) + $sConfigFile = utils::GetConfigFilePath(); + if (file_exists($sConfigFile)) { $oP->add("

Next: Modules selection

\n"); $sNextOperation = 'step4'; @@ -826,10 +827,11 @@ function ModulesSelection(SetupPage $oP, $aParamValues, $iCurrentStep, $oConfig) $aAvailableModules = AnalyzeInstallation($oConfig, $aParamValues['source_dir']); + $sConfigFile = utils::GetConfigFilePath(); // Form goes here if ($aParamValues['mode'] == 'upgrade') { - if (file_exists(FINAL_CONFIG_FILE)) + if (file_exists($sConfigFile)) { $iPrevStep = 1; // depends on where we came from } @@ -1018,7 +1020,7 @@ function AdminAccountDefinition(SetupPage $oP, $aParamValues, $iCurrentStep, Con $aForm = array(); $aAvailableLanguages = GetAvailableLanguages(APPROOT.'dictionaries'); $sLanguages = ''; - $sDefaultCode = $oConfig->GetDefaultLanguage(); + $sDefaultCode = 'EN US'; foreach($aAvailableLanguages as $sLangCode => $aInfo) { $sSelected = ($sLangCode == $sDefaultCode ) ? 'selected' : ''; @@ -1353,14 +1355,12 @@ function SetupFinished(SetupPage $oP, $aParamValues, $iCurrentStep, Config $oCon try { - $sSessionName = $oConfig->Get('session_name'); - if ($sSessionName != '') - { - $sSessionName = sprintf('iTop-%x', rand()); - $oConfig->Set('session_name', $sSessionName); - } - session_name($sSessionName); + session_name('itop-'.md5(APPROOT)); session_start(); + if (!isset($_SESSION['itop_env'])) + { + $_SESSION['itop_env'] = ITOP_DEFAULT_ENV; + } // Migration: force utf8_unicode_ci as the collation to make the global search // NON case sensitive @@ -1370,7 +1370,12 @@ function SetupFinished(SetupPage $oP, $aParamValues, $iCurrentStep, Config $oCon UpdateConfigSettings($oConfig, $aParamValues, $aParamValues['target_dir']); // Write the final configuration file - $oConfig->WriteToFile(FINAL_CONFIG_FILE); + $sConfigFile = utils::GetConfigFilePath(); + $sConfigDir = dirname($sConfigFile); + @mkdir($sConfigDir); + @chmod($sConfigDir, 0770); // RWX for owner and group, nothing for others + + $oConfig->WriteToFile($sConfigFile); // Start the application InitDataModel($oConfig, false, true); // Load model, startup DB and load the cache @@ -1387,7 +1392,7 @@ function SetupFinished(SetupPage $oP, $aParamValues, $iCurrentStep, Config $oCon $oP->add("

iTop configuration wizard

\n"); $oP->add("

Step 5: Configuration completed

\n"); - @unlink(FINAL_CONFIG_FILE); // remove the aborted config + @unlink($sConfigFile); // remove the aborted config $oP->error("Error: Failed to login for user: '$sAuthUser'\n"); $oP->add("\n"); @@ -1399,10 +1404,8 @@ function SetupFinished(SetupPage $oP, $aParamValues, $iCurrentStep, Config $oCon } } - // remove the tmp config file - @unlink(TMP_CONFIG_FILE); // try to make the final config file read-only - @chmod(FINAL_CONFIG_FILE, 0440); // Read-only for owner and group, nothing for others + @chmod($sConfigFile, 0440); // Read-only for owner and group, nothing for others $oP->set_title("Setup complete"); $oP->add("\n"); @@ -1471,16 +1474,16 @@ foreach($aParams as $sName) { $aParamValues[$sName] = utils::ReadParam($sName, '', false, 'raw_data'); } - -if (file_exists(FINAL_CONFIG_FILE)) +$sConfigFile = utils::GetConfigFilePath(); +if (file_exists($sConfigFile)) { // The configuration file already exists - if (!is_writable(FINAL_CONFIG_FILE)) + if (!is_writable($sConfigFile)) { $oP->add("

iTop configuration wizard

\n"); $oP->add("

Fatal error

\n"); - $oP->error("Error: the configuration file '".FINAL_CONFIG_FILE."' already exists and cannot be overwritten."); - $oP->p("The wizard cannot modify the configuration file for you. If you want to upgrade iTop, please make sure that the file '".realpath(FINAL_CONFIG_FILE)."' can be modified by the web server."); + $oP->error("Error: the configuration file '".$sConfigFile."' already exists and cannot be overwritten."); + $oP->p("The wizard cannot modify the configuration file for you. If you want to upgrade iTop, please make sure that the file '".realpath($sConfigFile)."' can be modified by the web server."); $oP->output(); exit; } @@ -1488,29 +1491,29 @@ if (file_exists(FINAL_CONFIG_FILE)) else { // No configuration file yet - // Check that the wizard can write into the root dir to create the configuration file - if (!is_writable(dirname(TMP_CONFIG_FILE))) + // Check that the wizard can write into the conf dir to create the configuration file + if (!is_writable(APPCONF)) { $oP->add("

iTop configuration wizard

\n"); $oP->add("

Fatal error

\n"); $oP->error("Error: the directory where to store the configuration file is not writable."); - $oP->p("The wizard cannot create the configuration file for you. Please make sure that the directory '".realpath(dirname(TMP_CONFIG_FILE))."' is writable for the web server."); + $oP->p("The wizard cannot create the configuration file for you. Please make sure that the directory '".realpath(APPCONF)."' is writable for the web server."); $oP->output(); exit; } - if (!is_writable(dirname(TMP_CONFIG_FILE).'/setup')) + if (!is_writable(APPROOT.'setup')) { $oP->add("

iTop configuration wizard

\n"); $oP->add("

Fatal error

\n"); $oP->error("Error: the directory where to store temporary setup files is not writable."); - $oP->p("The wizard cannot create operate. Please make sure that the directory '".realpath(dirname(TMP_CONFIG_FILE))."/setup' is writable for the web server."); + $oP->p("The wizard cannot create operate. Please make sure that the directory '".realpath(APPROOT.'setup')."' is writable for the web server."); $oP->output(); exit; } } -$oConfig = new Config(TMP_CONFIG_FILE, false /* Don't try to load it */); +$oConfig = new Config(); UpdateConfigSettings($oConfig, $aParamValues); try