54 lines
1.4 KiB
PHP
54 lines
1.4 KiB
PHP
<?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;
|
|
}
|