瀏覽代碼

#370 standard argument for CLI/REST services: param_file

git-svn-id: http://svn.code.sf.net/p/itop/code/trunk@1220 a333f486-631f-4898-b8df-5754b55c2be0
romainq 14 年之前
父節點
當前提交
fe8ebc4d5e
共有 5 個文件被更改,包括 148 次插入13 次删除
  1. 63 0
      application/utils.inc.php
  2. 18 1
      synchro/synchro_exec.php
  3. 23 4
      synchro/synchro_import.php
  4. 22 5
      webservices/cron.php
  5. 22 3
      webservices/import.php

+ 63 - 0
application/utils.inc.php

@@ -42,6 +42,60 @@ class utils
 	private static $m_sConfigFile = ITOP_CONFIG_FILE;
 	private static $m_oConfig = null;
 
+	// Parameters loaded from a file, parameters of the page/command line still have precedence
+	private static $m_aParamsFromFile = null;
+
+	protected static function LoadParamFile($sParamFile)
+	{
+		if (!file_exists($sParamFile))
+		{
+			throw new Exception("Could not find the parameter file: '$sParamFile'");
+		}
+		if (!is_readable($sParamFile))
+		{
+			throw new Exception("Could not load parameter file: '$sParamFile'");
+		}
+		$sParams = file_get_contents($sParamFile);
+
+		if (is_null(self::$m_aParamsFromFile))
+		{
+			self::$m_aParamsFromFile = array();
+		}
+
+		$aParamLines = explode("\n", $sParams);
+		foreach ($aParamLines as $sLine)
+		{
+			$sLine = trim($sLine);
+
+			// Ignore the line after a '#'
+			if (($iCommentPos = strpos($sLine, '#')) !== false)
+			{
+				$sLine = substr($sLine, 0, $iCommentPos);
+				$sLine = trim($sLine);
+			}
+
+			// Note: the line is supposed to be already trimmed
+			if (preg_match('/^(\S*)\s*=(.*)$/', $sLine, $aMatches))
+			{
+				$sParam = $aMatches[1];
+				$value = trim($aMatches[2]);
+				self::$m_aParamsFromFile[$sParam] = $value;
+			}
+		}
+	}
+
+	public static function UseParamFile($sParamFileArgName = 'param_file', $bAllowCLI = true)
+	{
+		$sFileSpec = self::ReadParam($sParamFileArgName, '', $bAllowCLI);
+		foreach(explode(',', $sFileSpec) as $sFile)
+		{
+			$sFile = trim($sFile);
+			if (!empty($sFile))
+			{
+				self::LoadParamFile($sFile);
+			}
+		}
+	}
 
 	public static function IsModeCLI()
 	{
@@ -62,6 +116,15 @@ class utils
 	{
 		global $argv;
 		$retValue = $defaultValue;
+
+		if (!is_null(self::$m_aParamsFromFile))
+		{
+			if (isset(self::$m_aParamsFromFile[$sName]))
+			{
+				$retValue = self::$m_aParamsFromFile[$sName];
+			}
+		}
+
 		if (isset($_REQUEST[$sName]))
 		{
 			$retValue = $_REQUEST[$sName];

+ 18 - 1
synchro/synchro_exec.php

@@ -77,7 +77,25 @@ function ReadMandatoryParam($oP, $sParam)
 if (utils::IsModeCLI())
 {
 	$oP = new CLIPage(Dict::S("TitleSynchroExecution"));
+}
+else
+{
+	$oP = new WebPage(Dict::S("TitleSynchroExecution"));
+}
 
+try
+{
+	utils::UseParamFile();
+}
+catch(Exception $e)
+{
+	$oP->p("Error: ".$e->GetMessage());
+	$oP->output();
+	exit -2;
+}
+
+if (utils::IsModeCLI())
+{
 	// Next steps:
 	//   specific arguments: 'csvfile'
 	//   
@@ -100,7 +118,6 @@ else
 	require_once(APPROOT.'/application/loginwebpage.class.inc.php');
 	LoginWebPage::DoLogin(); // Check user rights and prompt if needed
 
-	$oP = new WebPage(Dict::S("TitleSynchroExecution"));
 	$sDataSourcesList = utils::ReadParam('data_sources', null, true);
 	
 	if ($sDataSourcesList == null)

+ 23 - 4
synchro/synchro_import.php

@@ -197,8 +197,26 @@ function ReadMandatoryParam($oP, $sParam)
 
 if (utils::IsModeCLI())
 {
-	$oP = new CLIPage("iTop - Data Exchange");
+	$oP = new CLIPage(Dict::S("TitleSynchroExecution"));
+}
+else
+{
+	$oP = new WebPage(Dict::S("TitleSynchroExecution"));
+}
+
+try
+{
+	utils::UseParamFile();
+}
+catch(Exception $e)
+{
+	$oP->p("Error: ".$e->GetMessage());
+	$oP->output();
+	exit -2;
+}
 
+if (utils::IsModeCLI())
+{
 	// Next steps:
 	//   specific arguments: 'csvfile'
 	//   
@@ -212,13 +230,15 @@ if (utils::IsModeCLI())
 	else
 	{
 		$oP->p("Access restricted or wrong credentials ('$sAuthUser')");
-		exit;
+		$oP->output();
+		exit -1;
 	}
 
 	if (!is_readable($sCsvFile))
 	{
 		$oP->p("Input file could not be found or could not be read: '$sCsvFile'");
-		exit;
+		$oP->output();
+		exit -1;
 	}
 	$sCSVData = file_get_contents($sCsvFile);
 
@@ -229,7 +249,6 @@ else
 	require_once(APPROOT.'/application/loginwebpage.class.inc.php');
 	LoginWebPage::DoLogin(); // Check user rights and prompt if needed
 
-	$oP = new CSVPage("iTop - Data Exchange");
 	$sCSVData = utils::ReadPostedParam('csvdata');
 }
 

+ 22 - 5
webservices/cron.php

@@ -107,8 +107,26 @@ function CronExec($oP, $aBackgroundProcesses, $bVerbose)
 
 if (utils::IsModeCLI())
 {
-	$oP = new CLIPage("iTop - Bulk import");
+	$oP = new CLIPage("iTop - CRON");
+}
+else
+{
+	$oP = new WebPage("iTop - CRON");
+}
+
+try
+{
+	utils::UseParamFile();
+}
+catch(Exception $e)
+{
+	$oP->p("Error: ".$e->GetMessage());
+	$oP->output();
+	exit -2;
+}
 
+if (utils::IsModeCLI())
+{
 	// Next steps:
 	//   specific arguments: 'csvfile'
 	//   
@@ -121,7 +139,8 @@ if (utils::IsModeCLI())
 	else
 	{
 		$oP->p("Access wrong credentials ('$sAuthUser')");
-		exit;
+		$oP->output();
+		exit -1;
 	}
 }
 else
@@ -129,15 +148,13 @@ else
 	$_SESSION['login_mode'] = 'basic';
 	require_once(APPROOT.'/application/loginwebpage.class.inc.php');
 	LoginWebPage::DoLogin(); // Check user rights and prompt if needed
-
-	$oP = new WebPage("iTop - CRON");
 }
 
 if (!UserRights::IsAdministrator())
 {
 	$oP->p("Access restricted to administrators");
 	$oP->Output();
-	exit;
+	exit -1;
 }
 
 

+ 22 - 3
webservices/import.php

@@ -203,7 +203,25 @@ function ReadMandatoryParam($oP, $sParam)
 if (utils::IsModeCLI())
 {
 	$oP = new CLIPage("iTop - Bulk import");
+}
+else
+{
+	$oP = new CSVPage("iTop - Bulk import");
+}
+
+try
+{
+	utils::UseParamFile();
+}
+catch(Exception $e)
+{
+	$oP->p("Error: ".$e->GetMessage());
+	$oP->output();
+	exit -2;
+}
 
+if (utils::IsModeCLI())
+{
 	// Next steps:
 	//   specific arguments: 'csvfile'
 	//   
@@ -217,13 +235,15 @@ if (utils::IsModeCLI())
 	else
 	{
 		$oP->p("Access restricted or wrong credentials ('$sAuthUser')");
-		exit;
+		$oP->output();
+		exit -1;
 	}
 
 	if (!is_readable($sCsvFile))
 	{
 		$oP->p("Input file could not be found or could not be read: '$sCsvFile'");
-		exit;
+		$oP->output();
+		exit -1;
 	}
 	$sCSVData = file_get_contents($sCsvFile);
 
@@ -234,7 +254,6 @@ else
 	require_once(APPROOT.'/application/loginwebpage.class.inc.php');
 	LoginWebPage::DoLogin(); // Check user rights and prompt if needed
 
-	$oP = new CSVPage("iTop - Bulk import");
 	$sCSVData = utils::ReadPostedParam('csvdata');
 }