diff --git a/DbContinuousIntegration.php b/DbContinuousIntegration.php index 3d3a239..09a198b 100755 --- a/DbContinuousIntegration.php +++ b/DbContinuousIntegration.php @@ -3,7 +3,7 @@ require_once __DIR__."/.env.php"; require __DIR__."/app.php"; -require __DIR__."/plt.php"; +require __DIR__."/plt/index.php"; require __DIR__."/sys.php"; diff --git a/plt/generatePHPTriggerCode.inc.php b/plt/generatePHPTriggerCode.inc.php new file mode 100644 index 0000000..1e905d4 --- /dev/null +++ b/plt/generatePHPTriggerCode.inc.php @@ -0,0 +1,50 @@ + ['pipe', 'w'], // stdout - 2 => ['pipe', 'w'], // stderr - ]; - - // Execute the PHP code using proc_open - $process = proc_open($command, $descriptorspec, $pipes); - - if (!is_resource($process)) { - unlink($tempFile); - throw new Exception('Failed to execute sandboxed PHP code.'); - } - - // Capture stdout and stderr - $stdout = stream_get_contents($pipes[1]); - fclose($pipes[1]); - - $stderr = stream_get_contents($pipes[2]); - fclose($pipes[2]); - - // Get the exit code - $exitCode = proc_close($process); - - // Cleanup temporary file - unlink($tempFile); - - return $exitCode; -} -/** - * Handles the result of trigger execution, including error handling and updating database rows if necessary. - * - * @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 new row data). - * @param string $stderr Captured standard error from the execution (error message if any). - * @param array $originalRow Original database row before trigger execution. - * @param array $activeTable The active table details (including Name). - */ -function handleTriggerExecutionResult($result, $stdout, $stderr, $originalRow, $activeTable) { - $tableName = $activeTable['Name']; - - if ($result !== 0) { - // Update table error status and notify via Telegram if execution failed - updateTriggerError($tableName, $stderr); - } else { - // Decode the output from JSON to array - $newRowValue = json_decode($stdout, true); - - if ($newRowValue === null) { - updateTriggerError($tableName, 'Invalid JSON output from sandboxed execution.'); - return; - } - - // Check if the new data differs from the original data - if ($newRowValue !== $originalRow) { - // Update the database row accordingly - updateDatabaseRow($tableName, $originalRow, $newRowValue); - } - - // Clear any previous error status - clearTriggerError($tableName); - } -} function greenText($string) { $green = "\033[0;32m"; $reset = "\033[0m"; return $green . $string . $reset ; } function blueText($string) { $green = "\033[0;34m"; $reset = "\033[0m"; return $green . $string . $reset ; } function redText($string) { $green = "\033[0;31m"; $reset = "\033[0m"; return $green . $string . $reset ; } diff --git a/plt/runSandboxedPHP.inc.php b/plt/runSandboxedPHP.inc.php new file mode 100644 index 0000000..a9a9755 --- /dev/null +++ b/plt/runSandboxedPHP.inc.php @@ -0,0 +1,50 @@ + ['pipe', 'w'], // stdout + 2 => ['pipe', 'w'], // stderr + ]; + + // Execute the PHP code using proc_open + $process = proc_open($command, $descriptorspec, $pipes); + + if (!is_resource($process)) { + unlink($tempFile); + throw new Exception('Failed to execute sandboxed PHP code.'); + } + + // Capture stdout and stderr + $stdout = stream_get_contents($pipes[1]); + fclose($pipes[1]); + + $stderr = stream_get_contents($pipes[2]); + fclose($pipes[2]); + + // Get the exit code + $exitCode = proc_close($process); + + // Cleanup temporary file + unlink($tempFile); + + return $exitCode; +} +