#370 standard argument for CLI/REST services: param_file

SVN:trunk[1220]
This commit is contained in:
Romain Quetiez
2011-04-19 15:48:39 +00:00
parent 7e2982b848
commit 88a29fd8d1
5 changed files with 148 additions and 13 deletions

View File

@@ -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];

View File

@@ -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)

View File

@@ -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');
}

View File

@@ -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;
}

View File

@@ -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');
}