mirror of
https://github.com/Combodo/iTop.git
synced 2026-02-13 07:24:13 +01:00
100 lines
3.1 KiB
PHP
100 lines
3.1 KiB
PHP
<?php
|
|
|
|
use Hybridauth\Storage\Session;
|
|
|
|
require_once(__DIR__.'/../approot.inc.php');
|
|
require_once(APPROOT.'/application/application.inc.php');
|
|
require_once(APPROOT.'/application/startup.inc.php');
|
|
|
|
try {
|
|
$oCtx = new ContextTag(ContextTag::TAG_CRON);
|
|
LoginWebPage::ResetSession(true);
|
|
$iRet = LoginWebPage::DoLogin(false, false, LoginWebPage::EXIT_RETURN);
|
|
if ($iRet != LoginWebPage::EXIT_CODE_OK){
|
|
throw new Exception("Unknown authentication error (retCode=$iRet)", RestResult::UNAUTHORIZED);
|
|
}
|
|
|
|
$sCurrentLoginMode = \Combodo\iTop\Application\Helper\Session::Get('login_mode', '');
|
|
$oLoginFSMExtensionInstance = LoginWebPage::GetCurrentLoginPlugin($sCurrentLoginMode);
|
|
|
|
if ($oLoginFSMExtensionInstance instanceof iTokenLoginUIExtension){
|
|
/** @var iTokenLoginUIExtension $oLoginFSMExtensionInstance */
|
|
$aTokenInfo = $oLoginFSMExtensionInstance->GetTokenInfo();
|
|
$sTokenInfo = base64_encode(json_encode($aTokenInfo));
|
|
} else {
|
|
throw new \Exception("cannot call cron asynchronously via current login mode $sCurrentLoginMode");
|
|
}
|
|
|
|
$sLogFilename = ReadParam("cron_log_file", "cron.log");
|
|
$sLogFile = APPROOT."log/$sLogFilename";
|
|
|
|
$sCliParams = ReadParam("cron_cli_parameters");
|
|
|
|
$bAsynchronous = true;
|
|
if (is_null($sCliParams)) {
|
|
$sCliParams = "--help";
|
|
$bAsynchronous = false;
|
|
} else {
|
|
$sCliParams = trim(base64_decode($sCliParams, true));
|
|
|
|
$sCliParams = "--auth_token_info=$sTokenInfo ".$sCliParams;
|
|
$sCliParams = "--login_mode=$sCurrentLoginMode ".$sCliParams;
|
|
|
|
if (false !== strpos($sCliParams, '--status_only=1')) {
|
|
$bAsynchronous = false;
|
|
}
|
|
}
|
|
|
|
touch($sLogFile);
|
|
$sPHPExec = trim(\MetaModel::GetConfig()->Get('php_path'));
|
|
|
|
if ($bAsynchronous) {
|
|
$sCli = sprintf("$sPHPExec %s/cron.php $sCliParams 2>&1 >>$sLogFile &", __DIR__);
|
|
file_put_contents($sLogFile, $sCli);
|
|
$process = popen($sCli, 'r');
|
|
} else {
|
|
$sCli = sprintf("\n $sPHPExec %s/cron.php $sCliParams", __DIR__);
|
|
$fp = fopen($sLogFile, 'a+');
|
|
fwrite($fp, $sCli);
|
|
|
|
$aDescriptorSpec = [
|
|
0 => ["pipe", "r"], // stdin
|
|
1 => ["pipe", "w"], // stdout
|
|
];
|
|
$rProcess = proc_open($sCli, $aDescriptorSpec, $aPipes, __DIR__, null);
|
|
|
|
$sStdOut = stream_get_contents($aPipes[1]);
|
|
fclose($aPipes[1]);
|
|
$iCode = proc_close($rProcess);
|
|
|
|
fwrite($fp, $sStdOut);
|
|
fwrite($fp, "Exiting: ".time().' ('.date('Y-m-d H:i:s').')');
|
|
fclose($fp);
|
|
}
|
|
|
|
http_response_code(200);
|
|
$oP = new JsonPage();
|
|
$oP->add_header('Access-Control-Allow-Origin: *');
|
|
$oP->SetData(["message" => "OK"]);
|
|
$oP->SetOutputDataOnly(true);
|
|
$oP->Output();
|
|
}
|
|
catch (Exception $e) {
|
|
\IssueLog::Error("Cannot run cron", null, ['msg' => $e->getMessage(), 'stack' => $e->getTraceAsString()]);
|
|
http_response_code(500);
|
|
$oP = new JsonPage();
|
|
$oP->add_header('Access-Control-Allow-Origin: *');
|
|
$oP->SetData(["message" => $e->getMessage()]);
|
|
$oP->SetOutputDataOnly(true);
|
|
$oP->Output();
|
|
}
|
|
|
|
function ReadParam($sParam, $sDefaultValue = null, $sSanitizationFilter = utils::ENUM_SANITIZATION_FILTER_RAW_DATA)
|
|
{
|
|
$sValue = utils::ReadParam($sParam, null, true, $sSanitizationFilter);
|
|
if (is_null($sValue)) {
|
|
$sValue = utils::ReadPostedParam($sParam, $sDefaultValue, $sSanitizationFilter);
|
|
}
|
|
|
|
return trim($sValue);
|
|
} |