diff --git a/application/utils.inc.php b/application/utils.inc.php index 84a4b5bbb..2d69bb697 100644 --- a/application/utils.inc.php +++ b/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]; diff --git a/synchro/synchro_exec.php b/synchro/synchro_exec.php index f48c3fd19..457a6452b 100644 --- a/synchro/synchro_exec.php +++ b/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) diff --git a/synchro/synchro_import.php b/synchro/synchro_import.php index b043a8454..52155938e 100644 --- a/synchro/synchro_import.php +++ b/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'); } diff --git a/webservices/cron.php b/webservices/cron.php index bdb967400..919d64e89 100644 --- a/webservices/cron.php +++ b/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; } diff --git a/webservices/import.php b/webservices/import.php index cc298b53f..e9ba408a6 100644 --- a/webservices/import.php +++ b/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'); }