<?php
/*
* execute PHP code as a file
* This class is for execute and test simple PHP codes online
* 09/19/2007
* version 1.0
*/
class executeCode{
//work space directory
private $workDir;
//the folder for store generated PHP scripts
private $scriptDir;
private $scriptFilePath;
private $scriptFileName;
public function __construct($workDir, $scriptDir){
$this->workDir = $workDir;
$this->scriptDir = $scriptDir;
$this->scriptFilePath = $workDir.$scriptDir;
}
public function run($code, $scriptName=NULL){
$this->generateScriptFolder();
$this->generateScript($code, $scriptName);
$result = $this->executeCode();
return $result;
}
//mkdir a folder to store code
private function generateScriptFolder(){
if (!is_dir($this->scriptFilePath)){
mkdir($this->scriptFilePath);
//I think 0755 will also be OK.
chmod($this->scriptFilePath, 0777);
}
}
//generate a PHP file to execute
private function generateScript($code, $scriptName=NULL){
//if users did not type "<?", then add PHP start and end tag automaticly
if (preg_match("/^<\?/", $code)){
$string = stripslashes($code);
}
else {
$string = "<?php\n".stripslashes($code)."\n?>";
}
//if users did not appoint the file name, then assign a random name for the file
if (NULL == $scriptName){
$this->scriptFileName = $this->scriptFilePath.date("YmdHis").rand(1000,9999).'.php';
}
else {
//if users missed .php extension name, then add it automaticly
if (preg_match("/.php/isU", $scriptName)){
$this->scriptFileName = $this->scriptFilePath.$scriptName;
}
else {
$this->scriptFileName = $this->scriptFilePath.$scriptName.'.php';
}
}
file_put_contents($this->scriptFileName, $string);
}
//execute files
private function executeCode(){
$result = exec("php ".$this->scriptFileName);
return $result;
}
//show the history of the files which has been executed
public function showHistory(){
$history = "";
$historyTempArr = array();
//read script folder
if ($handle = opendir($this->scriptFilePath)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$historyTempArr[] = $file;
}
}
//Because I don not know how to list the files from a folder order by name,
//so I had to put the files into an array, and rsort the array after that.
//Can someone tell me how to list the files from a folder order by name ;-)?
rsort($historyTempArr);
foreach($historyTempArr as $_key => $_value){
$history .= "<option value='".$_value."'>".$_value."</option>";
}
closedir($handle);
}
return $history;
}
//read history files
public function readFile($fileName){
$code = file_get_contents($this->scriptFilePath.$fileName);
return $code;
}
function __destruct() {
//do what you want to do;
}
}
//define the constant
//work space directory
define('WORKDIR', '/home/htdocs/');
//the folder for store generated scripts
define('SCRIPTDIR', 'script/');
$ob = new executeCode(WORKDIR, SCRIPTDIR);
if ($_POST['code']){
$result = $ob->run($_POST['code'], $_POST['scriptName']);
}
?>
<form name='submitCode' action='<?=basename($_SERVER['PHP_SELF'])?>' method=post>
<textarea name='code' cols=80 rows=30>
<?
if ($_GET['historyFile']){
echo $ob->readFile($_GET['historyFile']);
}
?>
</textarea>
history:
<select name='history' multiple size='20' onchange='javascript:window.location.href="<?=basename($_SERVER['PHP_SELF'])?>?historyFile="+this.value'>
<?
$option = $ob->showHistory();
echo $option;
?>
</select>
<br>
<input type='text' name='scriptName' size='30'>
<br>
<input type=submit>
</form>
<textarea name='codeResult' cols=80 rows=10><?=$result?></textarea>