Skip to content

Instantly share code, notes, and snippets.

@serpro
Created May 31, 2013 11:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save serpro/5684502 to your computer and use it in GitHub Desktop.
Save serpro/5684502 to your computer and use it in GitHub Desktop.
Allow you to test PHP Code on the browser
<?php
register_shutdown_function( "fatal_handler" );
function fatal_handler() {
$errfile = "unknown file";
$errstr = "shutdown";
$errno = E_CORE_ERROR;
$errline = 0;
$error = error_get_last();
if( $error !== NULL) {
$errno = $error["type"];
$errfile = $error["file"];
$errline = $error["line"];
$errstr = $error["message"];
}
if($errno == E_ERROR)
echo $errstr . '<br /><button onclick="history.go(-1);">Try again</button>';
}
$name = basename(__FILE__, ".php");
$tool = ucwords(str_replace("_", " ", $name));
$extraJs = array();
if(file_exists("js/_" . $name . ".js"))
$extraJs[] = '<script type="text/javascript" language="javascript" src="js/_' . $name . '.js"></script>';
//REMOVE EXEC METHODS
$removeMethods = array('exec','system','passthru','escapeshellcmd','pcntl_exec','backtick_operator','call_user_func','call_user_func_array','create_function');
$regex = '#(\$\w+|\b($\w+|'.implode('|',$removeMethods).'))\s*\([^\)]*\);?#';
$codeOriginal = isset($_REQUEST['code'])? $_REQUEST['code'] : '';
$code = preg_replace($regex, '"";', $codeOriginal);
ob_start();
$return = eval($code);
$output = ob_get_contents();
ob_end_clean();
if ( $return === false && ( $error = error_get_last() ) ) {
$output = $error['message'];
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>TOOLS / <?=$tool?></title>
<link href="favicon.ico" rel="shortcut icon" />
<style type="text/css" media="all"> @import url("css/styles.css");</style>
<script type="text/javascript" language="javascript" src="js/jquery-1.3.2.min.js"></script>
<?=implode("\n ", $extraJs)?>
</head>
<body>
<div id="container">
<?php if($code !== $codeOriginal):?>
<strong><a href="http://cdn.memegenerator.net/instances/400x/36383077.jpg">Yo dawg, I heard you write naughty code, so I disabled it!</a></strong>
<?php endif;?>
<form method="post" action="">
<fieldset>PHP Code<br/>
<textarea id="code-area" name="code" style="width:800px;height:400px"><?=htmlentities($code)?></textarea><br/>
<input type="submit" value="Execute"/>
</fieldset>
</form>
<h2>Result</h2>
<div><?=$output?></div>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment