Kaynağa Gözat

Updated session mechanisms: itop_env defaults to 'production', and adapted the setup to create the configuration

git-svn-id: http://svn.code.sf.net/p/itop/code/trunk@1762 a333f486-631f-4898-b8df-5754b55c2be0
romainq 13 yıl önce
ebeveyn
işleme
dfaea9c1fa

+ 4 - 2
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);
 
 ?>

+ 25 - 2
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;
+	}
 
 }
 ?>

+ 4 - 0
approot.inc.php

@@ -1,5 +1,9 @@
 <?php
+
 define('APPROOT', dirname(__FILE__).'/');
+define('APPCONF', APPROOT.'conf/');
+define('ITOP_DEFAULT_ENV', 'production');
+
 if (function_exists('microtime'))
 {
 	$fItopStarted = microtime(true); 

+ 1 - 1
core/action.class.inc.php

@@ -295,7 +295,7 @@ class ActionEmail extends ActionNotification
 			$sBody = MetaModel::ApplyParams($this->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)

+ 15 - 6
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;
+		}
 	}
 	
 	/**

+ 1 - 1
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))
 		{

+ 10 - 9
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)

+ 1 - 1
index.php

@@ -1,5 +1,5 @@
 <?php
-$sConfigFile = 'config-itop.php';
+$sConfigFile = 'conf/production/config-itop.php';
 $sStartPage = './pages/UI.php';
 $sSetupPage = './setup/index.php';
 

+ 0 - 2
pages/logoff.php

@@ -25,8 +25,6 @@ $currentOrganization = utils::ReadParam('org_id', '');
 $operation = utils::ReadParam('operation', '');
 
 require_once(APPROOT.'/application/loginwebpage.class.inc.php');
-session_name(MetaModel::GetConfig()->Get('session_name'));
-session_start();
 $bPortal = utils::ReadParam('portal', false);
 $sUrl = utils::GetAbsoluteUrlAppRoot();
 if ($bPortal)

+ 3 - 5
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'),

+ 33 - 30
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("<form id=\"theForm\" method=\"post\" onSubmit=\"return DoSubmit('', 0)\">\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("<h2><input id=\"licence_ok\" type=\"checkbox\" name=\"licence_ok\" value=\"1\" $sChecked><label for=\"licence_ok\">I accept the terms of this licence agreement</label></h2>\n");
 
-	if (file_exists(FINAL_CONFIG_FILE))
+	$sConfigFile = utils::GetConfigFilePath();
+	if (file_exists($sConfigFile))
 	{
 		$oP->add("<h2 class=\"next\">Next: Modules selection</h2>\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 != '')
+		session_name('itop-'.md5(APPROOT));
+		session_start();
+		if (!isset($_SESSION['itop_env']))
 		{
-			$sSessionName = sprintf('iTop-%x', rand());
-			$oConfig->Set('session_name', $sSessionName);
+			$_SESSION['itop_env'] = ITOP_DEFAULT_ENV;
 		}
-		session_name($sSessionName);
-		session_start();
 		
 		// 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("<h1>iTop configuration wizard</h1>\n");
 				$oP->add("<h2>Step 5: Configuration completed</h2>\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("<form id=\"theForm\" method=\"post\">\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("<form id=\"theForm\" method=\"get\" action=\"../index.php\">\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("<h1>iTop configuration wizard</h1>\n");
 		$oP->add("<h2>Fatal error</h2>\n");
-		$oP->error("<b>Error:</b> 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 '<b>".realpath(FINAL_CONFIG_FILE)."</b>' can be modified by the web server.");
+		$oP->error("<b>Error:</b> 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 '<b>".realpath($sConfigFile)."</b>' 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("<h1>iTop configuration wizard</h1>\n");
 		$oP->add("<h2>Fatal error</h2>\n");
 		$oP->error("<b>Error:</b> 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 '<b>".realpath(dirname(TMP_CONFIG_FILE))."</b>' is writable for the web server.");
+		$oP->p("The wizard cannot create the configuration file for you. Please make sure that the directory '<b>".realpath(APPCONF)."</b>' is writable for the web server.");
 		$oP->output();
 		exit;
 	}
-	if (!is_writable(dirname(TMP_CONFIG_FILE).'/setup'))
+	if (!is_writable(APPROOT.'setup'))
 	{
 		$oP->add("<h1>iTop configuration wizard</h1>\n");
 		$oP->add("<h2>Fatal error</h2>\n");
 		$oP->error("<b>Error:</b> the directory where to store temporary setup files is not writable.");
-		$oP->p("The wizard cannot create operate. Please make sure that the directory '<b>".realpath(dirname(TMP_CONFIG_FILE))."/setup</b>' is writable for the web server.");
+		$oP->p("The wizard cannot create operate. Please make sure that the directory '<b>".realpath(APPROOT.'setup')."</b>' 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