bug: complete code of CRON module

This commit is contained in:
root 2026-04-01 15:39:02 +01:00
parent b8c847e2e7
commit b78f5e3c16
2 changed files with 92 additions and 0 deletions

View File

@ -0,0 +1,53 @@
<?php
/**
* Generates PHP code for sandbox execution based on provided dynamic PHP code and row data.
*
* @param string $functionName The name of the PHP function to generate.
* @param string $PhpCode The PHP code to embed within the generated function.
*
* @return string The complete PHP code ready for sandbox execution.
*/
function generatePHPCronCode($functionName, $PhpCode) {
// Fix code if redundant opening PHP tag
if (substr($PhpCode,0,5) == "<"."?"."php") $PhpCode = substr($PhpCode,5);
// Prepare the constants definition from the database
$constants = getConstantsDefinition();
// Prepare the support functions definitions from the database
$supportFunctions = getSupportFunctionsDefinition();
// Assemble the complete PHP script
$wrappedPhpCode = <<<PHP
<?php
// Define constants
$constants
// Include support functions
$supportFunctions
// Include system-level support file
require_once 'sys/sys.php';
// Include composer packages
require 'vendor/autoload.php';
// Dynamically defined trigger function
function $functionName(&\$error) {
$PhpCode
}
// Execute the dynamically generated function
\$error = "";
\$retVal = $functionName(\$error);
// Handle errors
if (\$retVal === false || !empty(\$error)) {
file_put_contents('php://stderr', "Error: \$error\n");
exit(1);
}
PHP;
return $wrappedPhpCode;
}

View File

@ -0,0 +1,39 @@
<?php
/**
* Handles the result of cron job execution, including error handling.
*
* @param int $result Exit code from the sandbox execution (0 for success, non-zero for failure).
* @param string $stdout Captured standard output from the execution (JSON encoded data).
* @param string $stderr Captured standard error from the execution (error message if any).
* @param array $cronJobId The id of executed Cron Job.
*/
function handleCronJobExecutionResult($result, $stdout, $stderr, $cronJobId) {
// 1. HANDLE FAILURE (Exit Code is not 0)
if ($result !== 0) {
// Update table with the error message from stderr
updateCronError($cronJobId, $stderr);
// Notify via Telegram (assuming a helper function exists)
sendTelegramMessage("Cron Job #$cronJobId Failed: " . $stderr,"DatabaseGroup");
}
// 2. HANDLE SUCCESS
else {
// Decode the output
$outputData = json_decode($stdout, true);
// If stdout exists but isn't valid JSON, treat it as an error
if (!empty($stdout) && $outputData === null) {
updateCronError($cronJobId, 'Invalid JSON output from sandboxed execution.');
return;
}
// Unlike Triggers, Cron Jobs don't usually compare $originalRow vs $newRow.
// We simply mark the job as clean/successful.
clearCronError($cronJobId);
// Optional: If your Cron Jobs are supposed to save data returned in $stdout,
// you would call that save function here.
// saveCronOutput($cronJobId, $outputData);
}
}