Skip to content
This repository has been archived by the owner on Sep 5, 2018. It is now read-only.

Commit

Permalink
- Fixed define_namespaces bug
Browse files Browse the repository at this point in the history
  • Loading branch information
fieryprophet committed May 7, 2014
1 parent b04dff7 commit 99c2eaf
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 55 deletions.
65 changes: 18 additions & 47 deletions src/PHPSandbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* @author Elijah Horton <[email protected]>
* @version 1.3.6
*/
class PHPSandbox {
class PHPSandbox implements \IteratorAggregate {
/**
* @const string The prefix given to the obfuscated sandbox key passed to the generated code
*/
Expand Down Expand Up @@ -530,7 +530,7 @@ public static function create(array $options = array(),
* @return mixed The output of the executed sandboxed code
*/
public function __invoke($code){
return call_user_func_array(array($this, 'execute'), func_get_args());
return call_user_func(array($this, 'execute'), $code);
}
/** PHPSandbox __sleep magic method
*
Expand Down Expand Up @@ -574,7 +574,7 @@ public function import($template, $import_flag = 0){
switch($type){
case 'func':
foreach($data as $key => $value){
$function = null;
$function = function(){};
@eval('$function = ' . $value["fullcode"] .';');
if(!is_callable($function)){
$this->validation_error("Could not import function $key! Please check your code for errors!", Error::IMPORT_ERROR, null, $function);
Expand Down Expand Up @@ -856,109 +856,74 @@ public function get_option($option){
switch($option){
case 'error_level':
return $this->error_level;
break;
case 'restore_error_level':
return $this->restore_error_level;
break;
case 'convert_errors':
return $this->convert_errors;
break;
case 'capture_output':
return $this->capture_output;
break;
case 'auto_whitelist_trusted_code':
return $this->auto_whitelist_trusted_code;
break;
case 'auto_whitelist_functions':
return $this->auto_whitelist_functions;
break;
case 'auto_whitelist_constants':
return $this->auto_whitelist_constants;
break;
case 'auto_whitelist_globals':
return $this->auto_whitelist_globals;
break;
case 'auto_whitelist_classes':
return $this->auto_whitelist_classes;
break;
case 'auto_whitelist_interfaces':
return $this->auto_whitelist_interfaces;
break;
case 'auto_whitelist_traits':
return $this->auto_whitelist_traits;
break;
case 'auto_define_vars':
return $this->auto_define_vars;
break;
case 'overwrite_defined_funcs':
return $this->overwrite_defined_funcs;
break;
case 'overwrite_sandboxed_string_funcs':
return $this->overwrite_sandboxed_string_funcs;
break;
case 'overwrite_func_get_args':
return $this->overwrite_func_get_args;
break;
case 'overwrite_superglobals':
return $this->overwrite_superglobals;
break;
case 'allow_functions':
return $this->allow_functions;
break;
case 'allow_closures':
return $this->allow_closures;
break;
case 'allow_variables':
return $this->allow_variables;
break;
case 'allow_static_variables':
return $this->allow_static_variables;
break;
case 'allow_objects':
return $this->allow_objects;
break;
case 'allow_constants':
return $this->allow_constants;
break;
case 'allow_globals':
return $this->allow_globals;
break;
case 'allow_namespaces':
return $this->allow_namespaces;
break;
case 'allow_aliases':
return $this->allow_aliases;
break;
case 'allow_classes':
return $this->allow_classes;
break;
case 'allow_interfaces':
return $this->allow_interfaces;
break;
case 'allow_traits':
return $this->allow_traits;
break;
case 'allow_generators':
return $this->allow_generators;
break;
case 'allow_escaping':
return $this->allow_escaping;
break;
case 'allow_casting':
return $this->allow_casting;
break;
case 'allow_error_suppressing':
return $this->allow_error_suppressing;
break;
case 'allow_references':
return $this->allow_references;
break;
case 'allow_backticks':
return $this->allow_backticks;
break;
case 'allow_halting':
return $this->allow_halting;
break;
}
return null;
}
Expand Down Expand Up @@ -2009,7 +1974,7 @@ public function _func_num_args(array $arguments = array()){
* @return mixed|SandboxedString Returns the wrapped value
*/
public function _wrap($value){
if(is_object($value) && is_callable($value, '__toString')){
if(is_object($value) && method_exists($value, '__toString')){
return $this->_wrap(strval($value));
} else if(is_string($value) && is_callable($value)){
return new SandboxedString($value, $this);
Expand Down Expand Up @@ -2642,7 +2607,7 @@ public function undefine_superglobals(array $superglobals = array()){
if(count($superglobals)){
foreach($superglobals as $superglobal => $name){
$name = $this->normalize_superglobal($name);
$this->undefine_superglobal(is_int($superglobal) ? $name : $superglobal, is_int($superglobal) ? null : $name);
$this->undefine_superglobal(is_int($superglobal) ? $name : $superglobal, is_int($superglobal) || !is_string($name) ? null : $name);
}
} else {
$this->definitions['superglobals'] = array();
Expand Down Expand Up @@ -2896,8 +2861,8 @@ public function define_namespace($name){
* @return PHPSandbox Returns the PHPSandbox instance for chainability
*/
public function define_namespaces(array $namespaces = array()){
foreach($namespaces as $name => $alias){
$this->define_namespace($name, $alias);
foreach($namespaces as $name){
$this->define_namespace($name);
}
return $this;
}
Expand Down Expand Up @@ -6685,7 +6650,7 @@ public function prepend($code){
if(!$code){
return $this;
}
$code = $this->disassemble($code, false);
$code = $this->disassemble($code);
if($this->auto_whitelist_trusted_code){
$this->auto_whitelist($code);
}
Expand All @@ -6701,7 +6666,7 @@ public function append($code){
if(!$code){
return $this;
}
$code = $this->disassemble($code, false);
$code = $this->disassemble($code);
if($this->auto_whitelist_trusted_code){
$this->auto_whitelist($code, true);
}
Expand Down Expand Up @@ -6895,7 +6860,7 @@ public function execute($callable = null, $skip_validation = false){
$saved_error_level = null;
if($this->error_level !== null){
$saved_error_level = error_reporting();
error_reporting($this->error_level);
error_reporting(intval($this->error_level));
}
if(is_callable($this->error_handler) || $this->convert_errors){
set_error_handler(array($this, 'error'), $this->error_handler_types);
Expand All @@ -6911,7 +6876,9 @@ public function execute($callable = null, $skip_validation = false){
} else {
$result = eval($this->generated_code);
}
} catch(\Exception $exception){}
} catch(\Exception $exception){
//swallow any exceptions
}
if(is_callable($this->error_handler) || $this->convert_errors){
restore_error_handler();
}
Expand Down Expand Up @@ -6987,7 +6954,7 @@ public function error($errno, $errstr, $errfile, $errline, $errcontext){
if($this->convert_errors){
return $this->exception(new \ErrorException($errstr, 0, $errno, $errfile, $errline));
}
return call_user_func_array($this->error_handler, array($errno, $errstr, $errfile, $errline, $errcontext, $this));
return is_callable($this->error_handler) ? call_user_func_array($this->error_handler, array($errno, $errstr, $errfile, $errline, $errcontext, $this)) : null;
}
/** Set callable to handle thrown exceptions
*
Expand Down Expand Up @@ -7133,4 +7100,8 @@ public function validation_error($error, $code = 0, \PHPParser_Node $node = null
public static function getSandbox($name){
return isset(static::$sandboxes[$name]) ? static::$sandboxes[$name] : null;
}

public function getIterator(){
return new \ArrayIterator(get_object_vars($this));
}
}
12 changes: 6 additions & 6 deletions tests/PHPSandbox/Tests/Config/DefaultConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public function testDisallowsEscaping(){
*/
public function testDisallowsCasting(){
$this->setExpectedException('PHPSandbox\Error');
$this->sandbox->execute(function(){ $a = '1'; $b = (bool)$a; });
$this->sandbox->execute(function(){ return (bool)'1'; });
}

/**
Expand Down Expand Up @@ -274,7 +274,7 @@ public function testDisallowsNonwhitelistedType(){
*/
public function testCustomFunctionValidationSuccess(){
$this->expectOutputString('success');
$this->sandbox->set_func_validator(function($name, $sandbox){
$this->sandbox->set_func_validator(function($name){
return $name == 'phpsandbox\tests\config\test';
});
function test(){
Expand All @@ -288,7 +288,7 @@ function test(){
*/
public function testCustomFunctionValidationFailure(){
$this->setExpectedException('PHPSandbox\Error');
$this->sandbox->set_func_validator(function($name, $sandbox){
$this->sandbox->set_func_validator(function($name){
return $name == 'test';
});
$this->sandbox->execute(function(){ test2(); });
Expand All @@ -311,7 +311,7 @@ public function testCustomErrorHandler(){
public function testCustomExceptionHandler(){
$this->setExpectedException('Exception');
$this->sandbox->whitelist_type('Exception');
$this->sandbox->set_exception_handler(function($exception, $sandbox){
$this->sandbox->set_exception_handler(function($exception){
throw $exception;
});
$this->sandbox->execute(function(){ throw new \Exception; });
Expand All @@ -323,7 +323,7 @@ public function testCustomExceptionHandler(){
public function testConvertErrors(){
$this->setExpectedException('ErrorException');
$this->sandbox->convert_errors = true;
$this->sandbox->set_exception_handler(function($error, $sandbox){
$this->sandbox->set_exception_handler(function($error){
throw $error;
});
$this->sandbox->execute(function(){ $a[1]; });
Expand All @@ -334,7 +334,7 @@ public function testConvertErrors(){
*/
public function testCustomValidationErrorHandler(){
$this->setExpectedException('PHPSandbox\Error');
$this->sandbox->set_validation_error_handler(function($error, $sandbox){
$this->sandbox->set_validation_error_handler(function($error){
throw $error;
});
$this->sandbox->execute(function(){ test2(); });
Expand Down
4 changes: 2 additions & 2 deletions toolkit/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

if(isset($_POST['save']) || isset($_POST['download'])){
if(isset($_POST['download'])){
$data = json_decode($_POST['download'], 1);
$data = json_decode($_POST['download'], true);
if(!is_array($data)){
header('Content-type: text/html');
die('<html><body><script>alert("Template could not be saved!");</script></body></html>');
Expand Down Expand Up @@ -154,7 +154,7 @@
exit;
}

$data = json_decode(file_get_contents("templates/001 - Hello World.json"), 1);
$data = json_decode(file_get_contents("templates/001 - Hello World.json"), true);
?>
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/html" xmlns="http://www.w3.org/1999/html">
Expand Down

0 comments on commit 99c2eaf

Please sign in to comment.