mirror of
https://github.com/Combodo/iTop.git
synced 2026-02-13 07:24:13 +01:00
N°2214 Add PHP check in CLI scripts
It is quite common that the PHP interpreter that is launched in CLI is different that the one used by the webserver. So iTop code launched by CLI could run in a context that doesn't meet iTop requirements !
This adds in the following scripts the same control that is done on the setup wizard first step :
* cron.php
* backup, check-backup
* export, exportv2
* bulk import
* synchro-exec, synchro-import
If the check throws at least one error then the script is stopped with an appropriate message, and a log is made (IssueLog, Error level, CLI channel)
(cherry picked from commit c768e18e2b : no risk taken for 2.7.1, so cherry picked for 2.8.0)
This commit is contained in:
@@ -29,8 +29,12 @@ require_once(APPROOT."/application/webpage.class.inc.php");
|
||||
|
||||
class CLIPage implements Page
|
||||
{
|
||||
/** @var string */
|
||||
public $s_title;
|
||||
|
||||
function __construct($s_title)
|
||||
{
|
||||
$this->s_title = $s_title;
|
||||
}
|
||||
|
||||
public function output()
|
||||
@@ -93,5 +97,3 @@ class CLIPage implements Page
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
@@ -134,6 +134,8 @@ set_time_limit(0);
|
||||
if (utils::IsModeCLI())
|
||||
{
|
||||
$oP = new CLIPage("iTop - Database Backup");
|
||||
|
||||
SetupUtils::CheckPhpAndExtensionsForCli($oP);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -250,6 +250,8 @@ catch(Exception $e)
|
||||
|
||||
if (utils::IsModeCLI())
|
||||
{
|
||||
SetupUtils::CheckPhpAndExtensionsForCli(new CLIPage('Check backup utility'));
|
||||
|
||||
echo date('Y-m-d H:i:s')." - running check-backup utility\n";
|
||||
try
|
||||
{
|
||||
|
||||
@@ -39,6 +39,49 @@ class CheckResult
|
||||
$this->sLabel = $sLabel;
|
||||
$this->sDescription = $sDescription;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
* @since 2.7.1 2.8.0 N°2214
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
$sPrintDesc = (empty($this->sDescription)) ? '' : " ({$this->sDescription})";
|
||||
return "{$this->sLabel}$sPrintDesc";
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \CheckResult[] $aResults
|
||||
*
|
||||
* @return \CheckResult[] only elements that are error (iSeverity===ERROR)
|
||||
*
|
||||
* @since 2.7.1 2.8.0 N°2214
|
||||
*/
|
||||
public static function KeepOnlyErrors($aResults)
|
||||
{
|
||||
return array_filter($aResults,
|
||||
static function ($v)
|
||||
{
|
||||
if ($v->iSeverity === CheckResult::ERROR) {
|
||||
return $v;
|
||||
}
|
||||
},
|
||||
ARRAY_FILTER_USE_BOTH);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \CheckResult[] $aResults
|
||||
* @return string[]
|
||||
* @uses \CheckResult::__toString
|
||||
*
|
||||
* @since 2.7.1 2.8.0 N°2214
|
||||
*/
|
||||
public static function FromObjetsToStrings($aResults)
|
||||
{
|
||||
return array_map(function($value) {
|
||||
return $value->__toString();
|
||||
}, $aResults);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -370,6 +413,37 @@ class SetupUtils
|
||||
return $aResult;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \CLIPage $oCliPage
|
||||
* @param int $iExitCode
|
||||
*
|
||||
* @since 2.7.1 2.8.0 N°2214
|
||||
*/
|
||||
public static function CheckPhpAndExtensionsForCli($oCliPage, $iExitCode = -1)
|
||||
{
|
||||
$aPhpCheckResults = self::CheckPhpAndExtensions();
|
||||
$aPhpCheckErrors = CheckResult::KeepOnlyErrors($aPhpCheckResults);
|
||||
if (empty($aPhpCheckErrors))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
$sMessageTitle = 'Error: PHP minimum requirements are not met !';
|
||||
$oCliPage->p($sMessageTitle);
|
||||
$aPhpCheckErrorsForPrint = CheckResult::FromObjetsToStrings($aPhpCheckErrors);
|
||||
foreach ($aPhpCheckErrorsForPrint as $sError)
|
||||
{
|
||||
$oCliPage->p(' * '.$sError);
|
||||
}
|
||||
$oCliPage->output();
|
||||
|
||||
// some CLI scripts are launched automatically
|
||||
// we need a log so that we don't miss errors after migration !
|
||||
IssueLog::Error($oCliPage->s_title.' '.$sMessageTitle, 'CLI', $aPhpCheckErrorsForPrint);
|
||||
|
||||
exit($iExitCode);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param CheckResult[] $aResult checks log
|
||||
*/
|
||||
|
||||
@@ -72,6 +72,7 @@ function ReadMandatoryParam($oP, $sParam, $sSanitizationFilter = 'parameter')
|
||||
if (utils::IsModeCLI())
|
||||
{
|
||||
$oP = new CLIPage(Dict::S("TitleSynchroExecution"));
|
||||
SetupUtils::CheckPhpAndExtensionsForCli($oP, -2);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -255,6 +255,7 @@ class CLILikeWebPage extends WebPage
|
||||
if (utils::IsModeCLI())
|
||||
{
|
||||
$oP = new CLIPage(Dict::S('TitleSynchroExecution'));
|
||||
SetupUtils::CheckPhpAndExtensionsForCli($oP, -2);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -60,7 +60,7 @@ function ReadMandatoryParam($oP, $sParam, $sSanitizationFilter = 'parameter')
|
||||
|
||||
function UsageAndExit($oP)
|
||||
{
|
||||
$bModeCLI = utils::IsModeCLI();
|
||||
$bModeCLI = ($oP instanceof CLIPage);
|
||||
|
||||
if ($bModeCLI)
|
||||
{
|
||||
@@ -477,9 +477,12 @@ function ReorderProcesses(&$aProcesses, $aTasks, $oNow, $bVerbose, &$oP)
|
||||
|
||||
set_time_limit(0); // Some background actions may really take long to finish (like backup)
|
||||
|
||||
if (utils::IsModeCLI())
|
||||
$bIsModeCLI = utils::IsModeCLI();
|
||||
if ($bIsModeCLI)
|
||||
{
|
||||
$oP = new CLIPage("iTop - cron");
|
||||
|
||||
SetupUtils::CheckPhpAndExtensionsForCli($oP, EXIT_CODE_FATAL);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -497,7 +500,7 @@ catch (Exception $e)
|
||||
exit(EXIT_CODE_FATAL);
|
||||
}
|
||||
|
||||
if (utils::IsModeCLI())
|
||||
if ($bIsModeCLI)
|
||||
{
|
||||
// Next steps:
|
||||
// specific arguments: 'csvfile'
|
||||
|
||||
@@ -32,6 +32,12 @@ require_once(APPROOT.'/core/bulkexport.class.inc.php');
|
||||
require_once(APPROOT.'/application/startup.inc.php');
|
||||
|
||||
|
||||
|
||||
const EXIT_CODE_ERROR = -1;
|
||||
const EXIT_CODE_FATAL = -2;
|
||||
|
||||
|
||||
|
||||
function ReportErrorAndExit($sErrorMessage)
|
||||
{
|
||||
if (utils::IsModeCLI())
|
||||
@@ -39,14 +45,14 @@ function ReportErrorAndExit($sErrorMessage)
|
||||
$oP = new CLIPage("iTop - Export");
|
||||
$oP->p('ERROR: '.$sErrorMessage);
|
||||
$oP->output();
|
||||
exit(-1);
|
||||
exit(EXIT_CODE_ERROR);
|
||||
}
|
||||
else
|
||||
{
|
||||
$oP = new WebPage("iTop - Export");
|
||||
$oP->p('ERROR: '.$sErrorMessage);
|
||||
$oP->output();
|
||||
exit(-1);
|
||||
exit(EXIT_CODE_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,7 +64,7 @@ function ReportErrorAndUsage($sErrorMessage)
|
||||
$oP->p('ERROR: '.$sErrorMessage);
|
||||
Usage($oP);
|
||||
$oP->output();
|
||||
exit(-1);
|
||||
exit(EXIT_CODE_ERROR);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -66,7 +72,7 @@ function ReportErrorAndUsage($sErrorMessage)
|
||||
$oP->p('ERROR: '.$sErrorMessage);
|
||||
Usage($oP);
|
||||
$oP->output();
|
||||
exit(-1);
|
||||
exit(EXIT_CODE_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -565,6 +571,8 @@ function DoExport(WebPage $oP, BulkExport $oExporter, $bInteractive = false)
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
if (utils::IsModeCLI())
|
||||
{
|
||||
SetupUtils::CheckPhpAndExtensionsForCli(new CLIPage('iTop - Export'));
|
||||
|
||||
try
|
||||
{
|
||||
// Do this before loging, in order to allow setting user credentials from within the file
|
||||
@@ -573,7 +581,7 @@ if (utils::IsModeCLI())
|
||||
catch(Exception $e)
|
||||
{
|
||||
echo "Error: ".$e->GetMessage()."<br/>\n";
|
||||
exit(-2);
|
||||
exit(EXIT_CODE_FATAL);
|
||||
}
|
||||
|
||||
$sAuthUser = utils::ReadParam('auth_user', null, true /* Allow CLI */, 'raw_data');
|
||||
|
||||
@@ -29,6 +29,11 @@ require_once(APPROOT.'/application/excelexporter.class.inc.php');
|
||||
|
||||
require_once(APPROOT.'/application/startup.inc.php');
|
||||
|
||||
|
||||
const EXIT_CODE_ERROR = -1;
|
||||
const EXIT_CODE_FATAL = -2;
|
||||
|
||||
|
||||
try
|
||||
{
|
||||
// Do this before loging, in order to allow setting user credentials from within the file
|
||||
@@ -37,11 +42,14 @@ try
|
||||
catch(Exception $e)
|
||||
{
|
||||
echo "Error: ".$e->GetMessage()."<br/>\n";
|
||||
exit -2;
|
||||
exit(EXIT_CODE_FATAL);
|
||||
}
|
||||
|
||||
if (utils::IsModeCLI())
|
||||
{
|
||||
$oP = new CLIPage("iTop - Export");
|
||||
SetupUtils::CheckPhpAndExtensionsForCli($oP, EXIT_CODE_FATAL);
|
||||
|
||||
$sAuthUser = utils::ReadParam('auth_user', null, true /* Allow CLI */, 'raw_data');
|
||||
$sAuthPwd = utils::ReadParam('auth_pwd', null, true /* Allow CLI */, 'raw_data');
|
||||
|
||||
@@ -51,10 +59,9 @@ if (utils::IsModeCLI())
|
||||
}
|
||||
else
|
||||
{
|
||||
$oP = new CLIPage("iTop - Export");
|
||||
$oP->p("Access restricted or wrong credentials ('$sAuthUser')");
|
||||
$oP->output();
|
||||
exit -1;
|
||||
exit(EXIT_CODE_ERROR);
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -74,7 +81,7 @@ if (utils::IsArchiveMode() && !UserRights::CanBrowseArchive())
|
||||
$oP = new CLIPage("iTop - Export");
|
||||
$oP->p("The user account is not authorized to access the archives");
|
||||
$oP->output();
|
||||
exit -1;
|
||||
exit(EXIT_CODE_ERROR);
|
||||
}
|
||||
|
||||
$bLocalize = (utils::ReadParam('no_localize', 0) != 1);
|
||||
|
||||
@@ -212,6 +212,7 @@ function ReadMandatoryParam($oP, $sParam, $sSanitizationFilter)
|
||||
if (utils::IsModeCLI())
|
||||
{
|
||||
$oP = new CLIPage("iTop - Bulk import");
|
||||
SetupUtils::CheckPhpAndExtensionsForCli($oP, -2);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user