This function validates your code and automatically whitelists it according to your specified configuration, then executes it.
public execute ( callable | string $callable = null, boolean $skip_validation = false ) : mixed | ||
$callable | callable | string | Callable or string of PHP code to prepare and execute within the sandbox |
$skip_validation | boolean | Boolean flag to indicate whether the sandbox should skip validation of the pass callable. Default is false. |
Результат | mixed | The output from the executed sandboxed code |
/** * Test whether sandbox disallows violating callbacks */ public function testWhitelistMagicConstants() { $this->sandbox->whitelistMagicConst('DIR'); $this->assertEquals(str_replace('tests', 'src', __DIR__), $this->sandbox->execute(function () { return __DIR__; })); }
public function evaluate($code) { $this->reset(); try { $this->output = $this->sandbox->execute($code); $this->executionTime = $this->sandbox->execution_time; $success = true; if (!is_null($this->sandbox->get_last_error())) { $this->addError($this->sandbox->get_last_error()); } } catch (\Exception $e) { $this->addError(array('type' => get_class($e), 'message' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine())); $success = false; } return $success; }
public function testStaticTypeOverwriting() { $class = 'B' . md5(time()); $this->sandbox->define_class('A', $class); $this->sandbox->allow_classes = true; $this->sandbox->allow_functions = true; $this->assertEquals("Yes", $this->sandbox->execute('<?php class ' . $class . ' { public $value = "Yes"; } function test' . $class . '(A $var){ return $var->value; } return test' . $class . '(new ' . $class . '); ?>')); }