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:
Pierre Goiffon
2020-06-12 16:31:50 +02:00
parent ea94986247
commit b7136c0b7a
10 changed files with 123 additions and 22 deletions

View File

@@ -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,12 +42,15 @@ try
catch(Exception $e)
{
echo "Error: ".$e->GetMessage()."<br/>\n";
exit -2;
exit(EXIT_CODE_FATAL);
}
if (utils::IsModeCLI())
{
$sAuthUser = utils::ReadParam('auth_user', null, true /* Allow CLI */, 'raw_data');
$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');
if (UserRights::CheckCredentials($sAuthUser, $sAuthPwd))
@@ -51,10 +59,9 @@ if (utils::IsModeCLI())
}
else
{
$oP = new CLIPage("iTop - Export");
$oP->p("Access restricted or wrong credentials ('$sAuthUser')");
$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);