N°2989 ajax.backup : refactor exit conditions

Adding a die() call so that we are sure to exit on errors !
This commit is contained in:
Pierre Goiffon
2020-05-07 10:13:46 +02:00
parent c5b1f02d2b
commit 222eb47bd2

View File

@@ -31,6 +31,26 @@ require_once(APPROOT.'/application/ajaxwebpage.class.inc.php');
require_once(APPROOT.'core/mutex.class.inc.php'); require_once(APPROOT.'core/mutex.class.inc.php');
/**
* @param WebPage $oPage
* @param string $sHtmlErrorMessage the whole HTML error, cinluding div/p/...
* @param int|string $exitCode
*
* @uses \die() https://www.php.net/manual/fr/function.die.php
*
* @since 2.6.5 2.7.1 N°2989
*/
function DisplayErrorAndDie($oPage, $sHtmlErrorMessage, $exitCode = null)
{
$oPage->add($sHtmlErrorMessage);
$oPage->output();
die($exitCode);
}
try try
{ {
$sOperation = utils::ReadParam('operation', ''); $sOperation = utils::ReadParam('operation', '');
@@ -48,10 +68,9 @@ try
if (utils::GetConfig()->Get('demo_mode')) if (utils::GetConfig()->Get('demo_mode'))
{ {
$oPage->add("<div data-error-stimulus=\"Error\">Sorry, iTop is in <b>demonstration mode</b>: the feature is disabled.</div>"); DisplayErrorAndDie($oPage, '<div data-error-stimulus="Error">Sorry, iTop is in <b>demonstration mode</b>: the feature is disabled.</div>');
} }
else
{
try try
{ {
set_time_limit(0); set_time_limit(0);
@@ -63,7 +82,7 @@ try
$oPage->p('Error: '.$e->getMessage()); $oPage->p('Error: '.$e->getMessage());
IssueLog::Error($sOperation.' - '.$e->getMessage()); IssueLog::Error($sOperation.' - '.$e->getMessage());
} }
}
$oPage->output(); $oPage->output();
break; break;
@@ -85,23 +104,22 @@ try
$sEnvironment = utils::ReadParam('environment', 'production', false, 'raw_data'); $sEnvironment = utils::ReadParam('environment', 'production', false, 'raw_data');
$oRestoreMutex = new iTopMutex('restore.'.$sEnvironment); $oRestoreMutex = new iTopMutex('restore.'.$sEnvironment);
if (!$oRestoreMutex->IsLocked()) if ($oRestoreMutex->IsLocked())
{ {
DisplayErrorAndDie($oPage, '<p>'.Dict::S('bkp-restore-running').'</p>');
}
$sFile = utils::ReadParam('file', '', false, 'raw_data'); $sFile = utils::ReadParam('file', '', false, 'raw_data');
$sToken = str_replace(' ', '', (string)microtime()); $sToken = str_replace(' ', '', (string)microtime());
$sTokenFile = APPROOT.'/data/restore.'.$sToken.'.tok'; $sTokenFile = APPROOT.'/data/restore.'.$sToken.'.tok';
file_put_contents($sTokenFile, $sFile); file_put_contents($sTokenFile, $sFile);
$oPage->add_ready_script( $oPage->add_ready_script(
<<<EOF <<<JS
$("#restore_token").val('$sToken'); $("#restore_token").val('$sToken');
EOF JS
); );
}
else
{
$oPage->p(Dict::S('bkp-restore-running'));
}
$oPage->output(); $oPage->output();
break; break;
@@ -123,10 +141,17 @@ EOF
if (utils::GetConfig()->Get('demo_mode')) if (utils::GetConfig()->Get('demo_mode'))
{ {
$oPage->add("<div data-error-stimulus=\"Error\">Sorry, iTop is in <b>demonstration mode</b>: the feature is disabled.</div>"); DisplayErrorAndDie($oPage, '<div data-error-stimulus="Error">Sorry, iTop is in <b>demonstration mode</b>: the feature is disabled.</div>');
} }
else
$sToken = utils::ReadParam('token', '', false, 'raw_data');
$sTokenFile = APPROOT.'/data/restore.'.$sToken.'.tok';
if (!is_file($sTokenFile))
{ {
IssueLog::Error("ajax.backup.php operation=$sOperation ERROR = inexisting token $sToken");
DisplayErrorAndDie($oPage, "<p>Error: missing token file: '$sTokenFile'</p>");
}
$sEnvironment = utils::ReadParam('environment', 'production', false, 'raw_data'); $sEnvironment = utils::ReadParam('environment', 'production', false, 'raw_data');
$oRestoreMutex = new iTopMutex('restore.'.$sEnvironment); $oRestoreMutex = new iTopMutex('restore.'.$sEnvironment);
IssueLog::Info("Backup Restore - Acquiring the LOCK 'restore.$sEnvironment'"); IssueLog::Info("Backup Restore - Acquiring the LOCK 'restore.$sEnvironment'");
@@ -137,12 +162,6 @@ EOF
set_time_limit(0); set_time_limit(0);
// Get the file and destroy the token (single usage) // Get the file and destroy the token (single usage)
$sToken = utils::ReadParam('token', '', false, 'raw_data');
$sTokenFile = APPROOT.'/data/restore.'.$sToken.'.tok';
if (!is_file($sTokenFile))
{
throw new Exception("Error: missing token file: '$sTokenFile'");
}
$sFile = file_get_contents($sTokenFile); $sFile = file_get_contents($sTokenFile);
unlink($sTokenFile); unlink($sTokenFile);
@@ -159,15 +178,17 @@ EOF
$sRes = $oDBRS->RestoreFromCompressedBackup($sBackupFile, $sEnvironment); $sRes = $oDBRS->RestoreFromCompressedBackup($sBackupFile, $sEnvironment);
IssueLog::Info('Backup Restore - Done, releasing the LOCK'); IssueLog::Info('Backup Restore - Done, releasing the LOCK');
$oRestoreMutex->Unlock();
} }
catch (Exception $e) catch (Exception $e)
{ {
$oRestoreMutex->Unlock();
$oPage->p('Error: '.$e->getMessage()); $oPage->p('Error: '.$e->getMessage());
IssueLog::Error($sOperation.' - '.$e->getMessage()); IssueLog::Error($sOperation.' - '.$e->getMessage());
} }
finally
{
$oRestoreMutex->Unlock();
} }
$oPage->output(); $oPage->output();
break; break;
@@ -185,14 +206,11 @@ EOF
$oBackup = new DBBackupScheduled(); $oBackup = new DBBackupScheduled();
$sBackupDir = APPROOT.'data/backups/'; $sBackupDir = APPROOT.'data/backups/';
$sPathNoDotDotPattern = "/^((?![\/\\\\]\.\.[\/\\\\]).)*$/"; $sPathNoDotDotPattern = "/^((?![\/\\\\]\.\.[\/\\\\]).)*$/";
if(preg_match($sPathNoDotDotPattern, $sBackupDir.$sFile) == 1) if(preg_match($sPathNoDotDotPattern, $sBackupDir.$sFile) != 1)
{
$oBackup->DownloadBackup($sBackupDir.$sFile);
}
else
{ {
throw new InvalidParameterException('Invalid file path'); throw new InvalidParameterException('Invalid file path');
} }
$oBackup->DownloadBackup($sBackupDir.$sFile);
break; break;
} }
} }