mirror of
https://github.com/Combodo/iTop.git
synced 2026-02-13 07:24:13 +01:00
cron endpoints behind login mechanism via auth_user and auth_pwd + deal with status_only different output
This commit is contained in:
@@ -1,39 +1,82 @@
|
||||
<?php
|
||||
|
||||
require_once(__DIR__.'/../approot.inc.php');
|
||||
require_once(APPROOT.'/application/application.inc.php');
|
||||
require_once(APPROOT.'/application/startup.inc.php');
|
||||
|
||||
const ERROR_ALREADY_RUNNING = "error_already_running";
|
||||
const RUNNING = "running";
|
||||
const STOPPED = "stopped";
|
||||
const ERROR = "error";
|
||||
|
||||
$sLogFilename = utils::ReadParam("cron_log_file", null, true, utils::ENUM_SANITIZATION_FILTER_RAW_DATA);
|
||||
if (is_null($sLogFilename)) {
|
||||
$sLogFilename = utils::ReadPostedParam("cron_log_file", null, utils::ENUM_SANITIZATION_FILTER_RAW_DATA) ?? "cron.log";
|
||||
}
|
||||
$sLogFile = APPROOT . "log/$sLogFilename";
|
||||
if (is_file($sLogFile)){
|
||||
$sContent = exec("tail -n 1 $sLogFile");
|
||||
if (0 === strpos($sContent, 'Exiting: ')){
|
||||
exec("tail -n 2 $sLogFile", $aContent);
|
||||
//var_dump($aContent);
|
||||
$sContent = implode("\n", $aContent);
|
||||
if (false !== strpos($sContent, 'Already running')){
|
||||
echo ERROR . " (already running)";
|
||||
exit;
|
||||
}
|
||||
$sAuthUser = ReadParam("auth_user");
|
||||
$sAuthPwd = ReadParam("auth_pwd");
|
||||
|
||||
if (preg_match('/ERROR: (.*)\\n/', $sContent, $aMatches)){
|
||||
echo ERROR . " ($aMatches[1])";
|
||||
exit;
|
||||
}
|
||||
try {
|
||||
$sAuthUser = ReadParam("auth_user");
|
||||
$sAuthPwd = ReadParam("auth_pwd");
|
||||
|
||||
echo "$sContent\n";
|
||||
echo STOPPED;
|
||||
exit;
|
||||
if (is_null($sAuthUser) || is_null($sAuthPwd)) {
|
||||
throw new \Exception("Missing credentials");
|
||||
}
|
||||
|
||||
echo RUNNING;
|
||||
return;
|
||||
if (UserRights::CheckCredentials($sAuthUser, $sAuthPwd)) {
|
||||
UserRights::Login($sAuthUser); // Login & set the user's language
|
||||
} else {
|
||||
throw new \Exception("Invalid credentials");
|
||||
}
|
||||
|
||||
$sLogFilename = ReadParam("cron_log_file", "cron.log");
|
||||
|
||||
$sStatus = STOPPED;
|
||||
$sMsg = "";
|
||||
$sLogFile = APPROOT."log/$sLogFilename";
|
||||
if (is_file($sLogFile)) {
|
||||
$sContent = exec("tail -n 1 $sLogFile");
|
||||
if (0 === strpos($sContent, 'Exiting: ')) {
|
||||
exec("tail -n 2 $sLogFile", $aContent);
|
||||
//var_dump($aContent);
|
||||
$sContent = implode("\n", $aContent);
|
||||
if (false !== strpos($sContent, 'Already running')) {
|
||||
$sStatus = ERROR_ALREADY_RUNNING;
|
||||
} else if (preg_match('/ERROR: (.*)\\n/', $sContent, $aMatches)) {
|
||||
$sMsg = "$aMatches[1]";
|
||||
$sStatus = ERROR;
|
||||
} else {
|
||||
$sMsg = "$sContent";
|
||||
$sStatus = STOPPED;
|
||||
}
|
||||
} else {
|
||||
$sStatus = RUNNING;
|
||||
}
|
||||
} else {
|
||||
$sMsg = "missing $sLogFile";
|
||||
$sStatus = ERROR;
|
||||
}
|
||||
|
||||
http_response_code(200);
|
||||
$oP = new JsonPage();
|
||||
$oP->add_header('Access-Control-Allow-Origin: *');
|
||||
$oP->SetData(["status" => $sStatus, 'message' => $sMsg]);
|
||||
$oP->SetOutputDataOnly(true);
|
||||
$oP->Output();
|
||||
}
|
||||
catch (Exception $e) {
|
||||
\IssueLog::Error("Cannot cron status", 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();
|
||||
}
|
||||
|
||||
echo ERROR . "(missing $sLogFile)";
|
||||
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);
|
||||
}
|
||||
@@ -1,24 +1,100 @@
|
||||
<?php
|
||||
|
||||
require_once(__DIR__.'/../approot.inc.php');
|
||||
require_once(APPROOT.'/application/application.inc.php');
|
||||
require_once(APPROOT.'/application/startup.inc.php');
|
||||
|
||||
$sLogFilename = utils::ReadParam("cron_log_file", null, true, utils::ENUM_SANITIZATION_FILTER_RAW_DATA);
|
||||
if (is_null($sLogFilename)) {
|
||||
$sLogFilename = utils::ReadPostedParam("cron_log_file", null, utils::ENUM_SANITIZATION_FILTER_RAW_DATA) ?? "cron.log";
|
||||
try {
|
||||
$sAuthUser = ReadParam("auth_user");
|
||||
$sAuthPwd = ReadParam("auth_pwd");
|
||||
|
||||
if (is_null($sAuthUser) || is_null($sAuthPwd)) {
|
||||
throw new \Exception("Missing credentials");
|
||||
}
|
||||
|
||||
if (UserRights::CheckCredentials($sAuthUser, $sAuthPwd)) {
|
||||
UserRights::Login($sAuthUser); // Login & set the user's language
|
||||
} else {
|
||||
throw new \Exception("Invalid credentials");
|
||||
}
|
||||
|
||||
$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));
|
||||
|
||||
if (false === strpos($sCliParams, '--auth_user=')) {
|
||||
$sCliParams = "--auth_user=$sAuthUser ".$sCliParams;
|
||||
}
|
||||
|
||||
if (false === strpos($sCliParams, '--auth_pwd=')) {
|
||||
$sCliParams = "--auth_pwd=$sAuthPwd ".$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();
|
||||
}
|
||||
$sLogFile = APPROOT . "log/$sLogFilename";
|
||||
|
||||
$sCliParams = utils::ReadParam("cron_cli_parameters", null, true, utils::ENUM_SANITIZATION_FILTER_RAW_DATA);
|
||||
if (is_null($sCliParams)) {
|
||||
$sCliParams = utils::ReadPostedParam("cron_cli_parameters", null, utils::ENUM_SANITIZATION_FILTER_RAW_DATA) ?? "--help";
|
||||
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();
|
||||
}
|
||||
|
||||
if (is_null($sCliParams)){
|
||||
$sCliParams = "--help";
|
||||
} else {
|
||||
$sCliParams = base64_decode($sCliParams, true);
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
$sCli = sprintf("php %s/cron.php $sCliParams 2>&1 >>$sLogFile &", __DIR__);
|
||||
exec("echo $sCli>>$sLogFile");
|
||||
$process=popen($sCli, 'r');
|
||||
return trim($sValue);
|
||||
}
|
||||
Reference in New Issue
Block a user