Compare commits

...

6 Commits

14 changed files with 410 additions and 33 deletions

View File

@@ -0,0 +1,23 @@
<?php
/**
* Login page extensibility
*
* @api
* @package UIExtensibilityAPI
* @since 3.x.x
*/
interface iTokenLoginUIExtension
{
/**
* @return array
* @api
*/
public function GetTokenInfo() : array;
/**
* @return array
* @api
*/
public function GetUserLogin(array $aTokenInfo) : string;
}

View File

@@ -9,7 +9,7 @@ use Combodo\iTop\Application\Helper\Session;
* @license http://opensource.org/licenses/AGPL-3.0 * @license http://opensource.org/licenses/AGPL-3.0
*/ */
class LoginBasic extends AbstractLoginFSMExtension class LoginBasic extends AbstractLoginFSMExtension implements iTokenLoginUIExtension
{ {
/** /**
* Return the list of supported login modes for this plugin * Return the list of supported login modes for this plugin
@@ -120,4 +120,21 @@ class LoginBasic extends AbstractLoginFSMExtension
} }
return [$sAuthUser, $sAuthPwd]; return [$sAuthUser, $sAuthPwd];
} }
public function GetTokenInfo(): array
{
return $this->GetAuthUserAndPassword();
}
public function GetUserLogin(array $aTokenInfo): string
{
$sLogin = $aTokenInfo[0];
$sLoginMode = 'basic';
if (UserRights::CheckCredentials($sLogin, $aTokenInfo[1], $sLoginMode, 'internal'))
{
return $sLogin;
}
throw new Exception("Cannot CheckCredentials user login ($sLogin) with ($sLoginMode) mode");
}
} }

View File

@@ -12,7 +12,7 @@ use Combodo\iTop\Application\Helper\Session;
* *
* @since 2.7.0 * @since 2.7.0
*/ */
class LoginForm extends AbstractLoginFSMExtension implements iLoginUIExtension class LoginForm extends AbstractLoginFSMExtension implements iLoginUIExtension, iTokenLoginUIExtension
{ {
private $bForceFormOnError = false; private $bForceFormOnError = false;
@@ -32,10 +32,11 @@ class LoginForm extends AbstractLoginFSMExtension implements iLoginUIExtension
protected function OnReadCredentials(&$iErrorCode) protected function OnReadCredentials(&$iErrorCode)
{ {
if (!Session::IsSet('login_mode') || Session::Get('login_mode') == 'form') { if (!Session::IsSet('login_mode') || Session::Get('login_mode') == 'form') {
$sAuthUser = utils::ReadPostedParam('auth_user', '', 'raw_data'); list($sAuthUser, $sAuthPwd) = $this->GetTokenInfo();
$sAuthPwd = utils::ReadPostedParam('auth_pwd', null, 'raw_data'); if ($this->bForceFormOnError || empty($sAuthUser) || empty($sAuthPwd))
if ($this->bForceFormOnError || empty($sAuthUser) || empty($sAuthPwd)) { {
if (array_key_exists('HTTP_X_COMBODO_AJAX', $_SERVER)) { if (array_key_exists('HTTP_X_COMBODO_AJAX', $_SERVER))
{
// X-Combodo-Ajax is a special header automatically added to all ajax requests // X-Combodo-Ajax is a special header automatically added to all ajax requests
// Let's reply that we're currently logged-out // Let's reply that we're currently logged-out
header('HTTP/1.0 401 Unauthorized'); header('HTTP/1.0 401 Unauthorized');
@@ -64,10 +65,11 @@ class LoginForm extends AbstractLoginFSMExtension implements iLoginUIExtension
*/ */
protected function OnCheckCredentials(&$iErrorCode) protected function OnCheckCredentials(&$iErrorCode)
{ {
if (Session::Get('login_mode') == 'form') { if (Session::Get('login_mode') == 'form')
$sAuthUser = utils::ReadPostedParam('auth_user', '', 'raw_data'); {
$sAuthPwd = utils::ReadPostedParam('auth_pwd', null, 'raw_data'); list($sAuthUser, $sAuthPwd) = $this->GetTokenInfo();
if (!UserRights::CheckCredentials($sAuthUser, $sAuthPwd, Session::Get('login_mode'), 'internal')) { if (!UserRights::CheckCredentials($sAuthUser, $sAuthPwd, Session::Get('login_mode'), 'internal'))
{
$iErrorCode = LoginWebPage::EXIT_CODE_WRONGCREDENTIALS; $iErrorCode = LoginWebPage::EXIT_CODE_WRONGCREDENTIALS;
return LoginWebPage::LOGIN_FSM_ERROR; return LoginWebPage::LOGIN_FSM_ERROR;
} }
@@ -145,4 +147,23 @@ class LoginForm extends AbstractLoginFSMExtension implements iLoginUIExtension
return $oLoginContext; return $oLoginContext;
} }
public function GetTokenInfo(): array
{
$sAuthUser = utils::ReadPostedParam('auth_user', '', 'raw_data');
$sAuthPwd = utils::ReadPostedParam('auth_pwd', null, 'raw_data');
return [$sAuthUser, $sAuthPwd];
}
public function GetUserLogin(array $aTokenInfo): string
{
$sLogin = $aTokenInfo[0];
$sLoginMode = 'form';
if (UserRights::CheckCredentials($sLogin, $aTokenInfo[1], $sLoginMode, 'internal'))
{
return $sLogin;
}
throw new Exception("Cannot CheckCredentials user login ($sLogin) with ($sLoginMode) mode");
}
} }

View File

@@ -9,7 +9,7 @@ use Combodo\iTop\Application\Helper\Session;
* @license http://opensource.org/licenses/AGPL-3.0 * @license http://opensource.org/licenses/AGPL-3.0
*/ */
class LoginURL extends AbstractLoginFSMExtension class LoginURL extends AbstractLoginFSMExtension implements iTokenLoginUIExtension
{ {
/** /**
* @var bool * @var bool
@@ -28,10 +28,10 @@ class LoginURL extends AbstractLoginFSMExtension
protected function OnModeDetection(&$iErrorCode) protected function OnModeDetection(&$iErrorCode)
{ {
if (!Session::IsSet('login_mode') && !$this->bErrorOccurred) { if (!Session::IsSet('login_mode') && !$this->bErrorOccurred)
$sAuthUser = utils::ReadParam('auth_user', '', false, 'raw_data'); {
$sAuthPwd = utils::ReadParam('auth_pwd', null, false, 'raw_data'); list($sAuthUser, $sAuthPwd) = $this->GetTokenInfo();
if (!empty($sAuthUser) && !empty($sAuthPwd)) { {
Session::Set('login_mode', 'url'); Session::Set('login_mode', 'url');
} }
} }
@@ -48,10 +48,11 @@ class LoginURL extends AbstractLoginFSMExtension
protected function OnCheckCredentials(&$iErrorCode) protected function OnCheckCredentials(&$iErrorCode)
{ {
if (Session::Get('login_mode') == 'url') { if (Session::Get('login_mode') == 'url')
$sAuthUser = utils::ReadParam('auth_user', '', false, 'raw_data'); {
$sAuthPwd = utils::ReadParam('auth_pwd', null, false, 'raw_data'); list($sAuthUser, $sAuthPwd) = $this->GetTokenInfo();
if (!UserRights::CheckCredentials($sAuthUser, $sAuthPwd, Session::Get('login_mode'), 'internal')) { if (!UserRights::CheckCredentials($sAuthUser, $sAuthPwd, Session::Get('login_mode'), 'internal'))
{
$iErrorCode = LoginWebPage::EXIT_CODE_WRONGCREDENTIALS; $iErrorCode = LoginWebPage::EXIT_CODE_WRONGCREDENTIALS;
return LoginWebPage::LOGIN_FSM_ERROR; return LoginWebPage::LOGIN_FSM_ERROR;
} }
@@ -84,4 +85,23 @@ class LoginURL extends AbstractLoginFSMExtension
} }
return LoginWebPage::LOGIN_FSM_CONTINUE; return LoginWebPage::LOGIN_FSM_CONTINUE;
} }
public function GetTokenInfo(): array
{
$sAuthUser = utils::ReadParam('auth_user', '', false, 'raw_data');
$sAuthPwd = utils::ReadParam('auth_pwd', null, false, 'raw_data');
return [$sAuthUser, $sAuthPwd];
}
public function GetUserLogin(array $aTokenInfo): string
{
$sLogin = $aTokenInfo[0];
$sLoginMode = 'url';
if (UserRights::CheckCredentials($sLogin, $aTokenInfo[1], $sLoginMode, 'internal'))
{
return $sLogin;
}
throw new Exception("Cannot CheckCredentials user login ($sLogin) with ($sLoginMode) mode");
}
} }

View File

@@ -530,6 +530,26 @@ class LoginWebPage extends NiceWebPage
return $aPlugins; return $aPlugins;
} }
public static function GetCurrentLoginPlugin(string $sCurrentLoginMode) : iLoginExtension
{
/** @var iLoginExtension $oLoginExtensionInstance */
foreach (MetaModel::EnumPlugins('iLoginFSMExtension') as $oLoginExtensionInstance)
{
$aLoginModes = $oLoginExtensionInstance->ListSupportedLoginModes();
$aLoginModes = (is_array($aLoginModes) ? $aLoginModes : array());
foreach ($aLoginModes as $sLoginMode)
{
// Keep only the plugins for the current login mode + before + after
if ($sLoginMode == $sCurrentLoginMode)
{
return $oLoginExtensionInstance;
}
}
}
throw new \Exception("should not happen");
}
/** /**
* Advance Login Finite State Machine to the next step * Advance Login Finite State Machine to the next step
* *

View File

@@ -14,7 +14,10 @@ if (PHP_VERSION_ID < 50600) {
echo $err; echo $err;
} }
} }
throw new RuntimeException($err); trigger_error(
$err,
E_USER_ERROR
);
} }
require_once __DIR__ . '/composer/autoload_real.php'; require_once __DIR__ . '/composer/autoload_real.php';

View File

@@ -3150,6 +3150,7 @@ return array(
'iRestServiceProvider' => $baseDir . '/application/applicationextension.inc.php', 'iRestServiceProvider' => $baseDir . '/application/applicationextension.inc.php',
'iScheduledProcess' => $baseDir . '/core/backgroundprocess.inc.php', 'iScheduledProcess' => $baseDir . '/core/backgroundprocess.inc.php',
'iSelfRegister' => $baseDir . '/core/userrights.class.inc.php', 'iSelfRegister' => $baseDir . '/core/userrights.class.inc.php',
'iTokenLoginUIExtension' => $baseDir . '/application/applicationextension/login/iTokenLoginUIExtension.php',
'iTopConfigParser' => $baseDir . '/core/iTopConfigParser.php', 'iTopConfigParser' => $baseDir . '/core/iTopConfigParser.php',
'iTopMutex' => $baseDir . '/core/mutex.class.inc.php', 'iTopMutex' => $baseDir . '/core/mutex.class.inc.php',
'iTopOwnershipLock' => $baseDir . '/core/ownershiplock.class.inc.php', 'iTopOwnershipLock' => $baseDir . '/core/ownershiplock.class.inc.php',

View File

@@ -56,7 +56,7 @@ return array(
'Psr\\Cache\\' => array($vendorDir . '/psr/cache/src'), 'Psr\\Cache\\' => array($vendorDir . '/psr/cache/src'),
'PhpParser\\' => array($vendorDir . '/nikic/php-parser/lib/PhpParser'), 'PhpParser\\' => array($vendorDir . '/nikic/php-parser/lib/PhpParser'),
'Pelago\\Emogrifier\\' => array($vendorDir . '/pelago/emogrifier/src'), 'Pelago\\Emogrifier\\' => array($vendorDir . '/pelago/emogrifier/src'),
'League\\OAuth2\\Client\\' => array($vendorDir . '/league/oauth2-google/src', $vendorDir . '/league/oauth2-client/src'), 'League\\OAuth2\\Client\\' => array($vendorDir . '/league/oauth2-client/src', $vendorDir . '/league/oauth2-google/src'),
'GuzzleHttp\\Psr7\\' => array($vendorDir . '/guzzlehttp/psr7/src'), 'GuzzleHttp\\Psr7\\' => array($vendorDir . '/guzzlehttp/psr7/src'),
'GuzzleHttp\\Promise\\' => array($vendorDir . '/guzzlehttp/promises/src'), 'GuzzleHttp\\Promise\\' => array($vendorDir . '/guzzlehttp/promises/src'),
'GuzzleHttp\\' => array($vendorDir . '/guzzlehttp/guzzle/src'), 'GuzzleHttp\\' => array($vendorDir . '/guzzlehttp/guzzle/src'),

View File

@@ -314,8 +314,8 @@ class ComposerStaticInit7f81b4a2a468a061c306af5e447a9a9f
), ),
'League\\OAuth2\\Client\\' => 'League\\OAuth2\\Client\\' =>
array ( array (
0 => __DIR__ . '/..' . '/league/oauth2-google/src', 0 => __DIR__ . '/..' . '/league/oauth2-client/src',
1 => __DIR__ . '/..' . '/league/oauth2-client/src', 1 => __DIR__ . '/..' . '/league/oauth2-google/src',
), ),
'GuzzleHttp\\Psr7\\' => 'GuzzleHttp\\Psr7\\' =>
array ( array (
@@ -3509,6 +3509,7 @@ class ComposerStaticInit7f81b4a2a468a061c306af5e447a9a9f
'iRestServiceProvider' => __DIR__ . '/../..' . '/application/applicationextension.inc.php', 'iRestServiceProvider' => __DIR__ . '/../..' . '/application/applicationextension.inc.php',
'iScheduledProcess' => __DIR__ . '/../..' . '/core/backgroundprocess.inc.php', 'iScheduledProcess' => __DIR__ . '/../..' . '/core/backgroundprocess.inc.php',
'iSelfRegister' => __DIR__ . '/../..' . '/core/userrights.class.inc.php', 'iSelfRegister' => __DIR__ . '/../..' . '/core/userrights.class.inc.php',
'iTokenLoginUIExtension' => __DIR__ . '/../..' . '/application/applicationextension/login/iTokenLoginUIExtension.php',
'iTopConfigParser' => __DIR__ . '/../..' . '/core/iTopConfigParser.php', 'iTopConfigParser' => __DIR__ . '/../..' . '/core/iTopConfigParser.php',
'iTopMutex' => __DIR__ . '/../..' . '/core/mutex.class.inc.php', 'iTopMutex' => __DIR__ . '/../..' . '/core/mutex.class.inc.php',
'iTopOwnershipLock' => __DIR__ . '/../..' . '/core/ownershiplock.class.inc.php', 'iTopOwnershipLock' => __DIR__ . '/../..' . '/core/ownershiplock.class.inc.php',

View File

@@ -36,7 +36,8 @@ if ($issues) {
echo 'Composer detected issues in your platform:' . PHP_EOL.PHP_EOL . str_replace('You are running '.PHP_VERSION.'.', '', implode(PHP_EOL, $issues)) . PHP_EOL.PHP_EOL; echo 'Composer detected issues in your platform:' . PHP_EOL.PHP_EOL . str_replace('You are running '.PHP_VERSION.'.', '', implode(PHP_EOL, $issues)) . PHP_EOL.PHP_EOL;
} }
} }
throw new \RuntimeException( trigger_error(
'Composer detected issues in your platform: ' . implode(' ', $issues) 'Composer detected issues in your platform: ' . implode(' ', $issues),
E_USER_ERROR
); );
} }

View File

@@ -0,0 +1,84 @@
<?php
namespace Combodo\iTop\Test\UnitTest\Webservices;
use Combodo\iTop\Test\UnitTest\ItopDataTestCase;
use Exception;
use MetaModel;
use utils;
/**
* @group itopRequestMgmt
* @group restApi
* @group defaultProfiles
*/
class CronTest extends ItopDataTestCase
{
public const USE_TRANSACTION = false;
public const CREATE_TEST_ORG = false;
private static $sLogin;
private static $sPassword = "Iuytrez9876543ç_è-(";
/**
* This method is called before the first test of this test class is run (in the current process).
*/
public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();
}
/**
* This method is called after the last test of this test class is run (in the current process).
*/
public static function tearDownAfterClass(): void
{
parent::tearDownAfterClass();
}
/**
* @throws Exception
*/
protected function setUp(): void
{
parent::setUp();
static::$sLogin = "rest-user-".date('dmYHis');
$this->CreateTestOrganization();
}
public function testListOperationsAndJSONPCallback()
{
$this->CreateUserWithProfiles([self::$aURP_Profiles['Administrator'], self::$aURP_Profiles['REST Services User']]);
$aPostFields = [
'version' => '1.3',
'auth_user' => static::$sLogin,
'auth_pwd' => static::$sPassword,
'json_data' => '{"operation": "list_operations"}',
];
// Test regular JSON result
$sJSONResult = $this->CallItopUri("/webservices/rest.php", $aPostFields);
$sExpected = <<<JSON
{"code":0,"message":"Operations: 7","version":"1.3","operations":[{"verb":"core\/create","description":"Create an object","extension":"CoreServices"},{"verb":"core\/update","description":"Update an object","extension":"CoreServices"},{"verb":"core\/apply_stimulus","description":"Apply a stimulus to change the state of an object","extension":"CoreServices"},{"verb":"core\/get","description":"Search for objects","extension":"CoreServices"},{"verb":"core\/delete","description":"Delete objects","extension":"CoreServices"},{"verb":"core\/get_related","description":"Get related objects through the specified relation","extension":"CoreServices"},{"verb":"core\/check_credentials","description":"Check user credentials","extension":"CoreServices"}]}
JSON;
$this->assertEquals($sExpected, $sJSONResult);
}
private function CreateUserWithProfiles(array $aProfileIds): void
{
if (count($aProfileIds) > 0) {
$oUser = null;
foreach ($aProfileIds as $oProfileId) {
if (is_null($oUser)) {
$oUser = $this->CreateUser(static::$sLogin, $oProfileId, static::$sPassword);
} else {
$this->AddProfileToUser($oUser, $oProfileId);
}
}
}
}
}

View File

@@ -466,6 +466,9 @@ try {
// Next steps: // Next steps:
// specific arguments: 'csv file' // specific arguments: 'csv file'
// //
$sTokenInfo = utils::ReadParam('auth_info', null, true, 'raw_data');
$sLoginMode = utils::ReadParam('login_mode', null, true, 'raw_data');
if (is_null($sLoginMode) || is_null($sTokenInfo)) {
$sAuthUser = ReadMandatoryParam($oP, 'auth_user', 'raw_data'); $sAuthUser = ReadMandatoryParam($oP, 'auth_user', 'raw_data');
$sAuthPwd = ReadMandatoryParam($oP, 'auth_pwd', 'raw_data'); $sAuthPwd = ReadMandatoryParam($oP, 'auth_pwd', 'raw_data');
if (UserRights::CheckCredentials($sAuthUser, $sAuthPwd)) { if (UserRights::CheckCredentials($sAuthUser, $sAuthPwd)) {
@@ -475,6 +478,21 @@ try {
$oP->output(); $oP->output();
exit(EXIT_CODE_ERROR); exit(EXIT_CODE_ERROR);
} }
} else
{
$oLoginFSMExtensionInstance = LoginWebPage::GetCurrentLoginPlugin($sLoginMode);
if ($oLoginFSMExtensionInstance instanceof iTokenLoginUIExtension){
$aTokenInfo = json_decode(base64_decode($sTokenInfo), true);
/** @var iTokenLoginUIExtension $oLoginFSMExtensionInstance */
$sAuthUser = $oLoginFSMExtensionInstance->GetUserLogin($aTokenInfo);
UserRights::Login($sAuthUser); // Login & set the user's language
} else {
$oP->p("cannot call cron asynchronously via current login mode $sLoginMode");
$oP->output();
exit(EXIT_CODE_ERROR);
}
}
} else { } else {
require_once(APPROOT.'/application/loginwebpage.class.inc.php'); require_once(APPROOT.'/application/loginwebpage.class.inc.php');
LoginWebPage::DoLogin(); // Check user rights and prompt if needed LoginWebPage::DoLogin(); // Check user rights and prompt if needed

View File

@@ -0,0 +1,73 @@
<?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";
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);
}
$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();
}
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);
}

View File

@@ -0,0 +1,95 @@
<?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){
throw new \Exception("cannot call cron asynchronously via current login mode $sCurrentLoginMode");
}
$aCronValues = [];
foreach ([ 'status_only', 'verbose', 'debug'] as $sParam){
$value = ReadParam($sParam, false);
$aCronValues[] = "--$sParam=".escapeshellarg($value);
}
/** @var iTokenLoginUIExtension $oLoginFSMExtensionInstance */
$aTokenInfo = $oLoginFSMExtensionInstance->GetTokenInfo();
$sTokenInfo = base64_encode(json_encode($aTokenInfo));
$aCronValues[] = "--auth_info=".escapeshellarg($sTokenInfo);
$aCronValues[] = "--login_mode=".escapeshellarg($sCurrentLoginMode);
$sCliParams=implode(" ", $aCronValues);
$sLogFilename = ReadParam("cron_log_file", "cron.log");
$sLogFile = APPROOT."log/$sLogFilename";
touch($sLogFile);
$sPHPExec = trim(\MetaModel::GetConfig()->Get('php_path'));
if ($aCronValues['status_only']) {
//still synchronous
$sCli = sprintf("$sPHPExec %s/cron.php $sCliParams 2>&1 >>$sLogFile &", __DIR__);
file_put_contents($sLogFile, $sCli);
$process = popen($sCli, 'r');
} else {
//asynchronous
$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);
}