Online PHP editor for testing your code. There is no isolation, do not make the web interface public!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
php-sandbox/index.php

146 lines
4.0 KiB

14 years ago
<?php
/**
* PHP Sandbox: https://github.com/MightyPork/php-sandbox
*/
ini_set('log_errors', false);
ini_set('display_errors', true);
ini_set('display_startup_errors', true);
ini_set('display_not_found_reason', true);
ini_set('display_exceptions', true);
ini_set('html_errors', false);
9 years ago
error_reporting(E_ALL | E_STRICT);
// --- Handle config ---
14 years ago
$defaults = array(
// how many spaces to use for indention, 0 will make it use real tabs
'tabsize' => 4,
9 years ago
// whitelist of IPs
// use '*' to allow any IP
'ip_whitelist' => array('127.0.0.1', '::1'),
// bootstrap file, if defined this file will be included before
// the code entered by the user is evaluated. any variables and classes
// defined here will be accessible by the eval'd code
'bootstrap' => null,
);
if (file_exists(__DIR__.'/config.php')) {
$options = include __DIR__.'/config.php';
$options = array_merge($defaults, $options);
} else {
$options = $defaults;
}
9 years ago
// --- IP Whitelist ---
if (!in_array('*', $options['ip_whitelist'], true) &&
!in_array($_SERVER['REMOTE_ADDR'], $options['ip_whitelist'], true)
) {
9 years ago
header("HTTP/1.0 401 Unauthorized");
die('Access Denied');
14 years ago
}
$debugOutput = '';
if (isset($_POST['code'])) {
ini_set('html_errors', false);
$code = $_POST['code'];
9 years ago
// --- Evaluate the code ---
14 years ago
ob_start();
$memBefore = memory_get_usage(true);
11 years ago
$start = microtime(true);
9 years ago
// Remove the < ?php mark
// TODO remove also ? > if present
$code = preg_replace('{^\s*<\?(php)?\s*}i', '', $code);
/** Run code with bootstrap in separate scope */
function runCode($__source_code, $__bootstrap_file) {
if ($__bootstrap_file) {
require $__bootstrap_file;
}
eval($__source_code);
}
runCode($code, $options['bootstrap']);
11 years ago
$end = microtime(true);
11 years ago
$memAfter = memory_get_peak_usage(true);
$debugOutput .= ob_get_clean();
11 years ago
9 years ago
// ---------------------------
if (isset($_GET['js'])) {
9 years ago
// --- Send response with metadata in headers ---
header('Content-Type: text/plain');
11 years ago
$memory = sprintf('%.3f', ($memAfter - $memBefore) / 1024.0 / 1024.0); // in MB
$rendertime = sprintf('%.3f', (($end - $start) * 1000)); // in ms
11 years ago
header('X-Memory-Usage: '. $memory);
header('X-Rendertime: '. $rendertime);
11 years ago
echo $debugOutput;
9 years ago
die('#end-php-console-output#');
}
14 years ago
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>PHP Sandbox</title>
14 years ago
<link rel="stylesheet" type="text/css" href="styles.css" />
<script src="jquery-1.9.1.min.js"></script>
<script src="ace/ace.js"></script>
<script src="ace/theme-monokai.js"></script>
<script src="ace/mode-php.js"></script>
<script src="ace/ext-language_tools.js"></script>
13 years ago
<script src="php-console.js"></script>
13 years ago
<script>
$.console({
tabsize: <?= json_encode($options['tabsize']) ?>
});
14 years ago
</script>
</head>
<body>
<div class="console-wrapper">
<div class="input-wrapper">
<form method="POST" action="" id="mainform">
<div class="input">
<textarea class="editor" id="editor" name="code"><?=
(isset($_POST['code']) ? htmlentities($_POST['code'], ENT_QUOTES, 'UTF-8') : "&lt;?php\n\n")
?></textarea>
</div>
<div class="statusbar">
<span class="position">Line: 1, Column: 1</span>
<span class="runtime-info"></span>
<input type="submit" name="subm" value="Run!"/>
</div>
</form>
</div>
<div class="output-wrapper">
<div class="output"><?= htmlentities($debugOutput) ?></div>
</div>
14 years ago
</div>
</body>
</html>