You can pass optional arrays of predefined functions, variables, etc. to the sandbox through the constructor
public static create ( array $options = [], array $functions = [], array $variables = [], array $constants = [], array $namespaces = [], array $aliases = [], array $superglobals = [], array $magic_constants = [], array $classes = [], array $interfaces = [], array $traits = [] ) | ||
$options | array | Optional array of options to set for the sandbox |
$functions | array | Optional array of functions to define for the sandbox |
$variables | array | Optional array of variables to define for the sandbox |
$constants | array | Optional array of constants to define for the sandbox |
$namespaces | array | Optional array of namespaces to define for the sandbox |
$aliases | array | Optional array of aliases to define for the sandbox |
$superglobals | array | Optional array of superglobals to define for the sandbox |
$magic_constants | array | Optional array of magic constants to define for the sandbox |
$classes | array | Optional array of classes to define for the sandbox |
$interfaces | array | Optional array of interfaces to define for the sandbox |
$traits | array | Optional array of traits to define for the sandbox |
die(json_encode(array('message' => 'The template "' . $template . '" was saved successfully!', 'name' => $cnt . ' - ' . $template, 'file' => $filename, 'success' => true))); } } header('Content-type: text/html'); die(json_encode(array('message' => 'An error occurred that prevented your template from being saved!', 'success' => false))); } if (isset($_POST['code'])) { $code = $_POST['code']; $setup_code = isset($_POST['setup_code']) ? $_POST['setup_code'] : null; $prepend_code = isset($_POST['prepend_code']) ? $_POST['prepend_code'] : null; $append_code = isset($_POST['append_code']) ? $_POST['append_code'] : null; $options = isset($_POST['options']) ? $_POST['options'] : array(); $whitelist = isset($_POST['whitelist']) ? $_POST['whitelist'] : null; $blacklist = isset($_POST['blacklist']) ? $_POST['blacklist'] : null; $definitions = isset($_POST['definitions']) ? $_POST['definitions'] : null; $sandbox = \PHPSandbox\PHPSandbox::create()->import(array('setup_code' => $setup_code, 'prepend_code' => $prepend_code, 'append_code' => $append_code, 'options' => $options, 'whitelist' => $whitelist, 'blacklist' => $blacklist, 'definitions' => $definitions)); $sandbox->setErrorHandler(function ($errno, $errmsg, $errfile, $errline) { die('<h2 style="color: red;">Error: ' . $errmsg . ' on line ' . $errline . '</h2>'); }); $sandbox->setExceptionHandler(function (\Exception $e) { die('<h2 style="color: red;">Exception: ' . $e->getMessage() . ' on line ' . $e->getLine() . '</h2>'); }); $sandbox->setValidationErrorHandler(function (\PHPSandbox\Error $e) { die('<h2 style="color: red;">Validation Error: ' . $e->getMessage() . '</h2>'); }); try { ob_start(); if ($setup_code) { @eval($setup_code); } $result = $sandbox->execute($code);