Created
April 3, 2018 15:45
-
-
Save 20esaua/1db6f8af5ef77468df3dee6599e3acd3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
# This is the location where program will reside | |
if (isset($_SERVER["HTTP_ORIGIN"]) || isset($_SERVER['HTTP_REFERER']) ) { | |
$valid_site = false; | |
$origin_site = isset($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN']: $_SERVER['HTTP_REFERER']; | |
$origin_site = trim($origin_site); | |
$pattern = "/(.*)tutorialspoint.com(.*)/"; | |
if( preg_match($pattern, $origin_site, $matches )){ | |
$valid_site = true; | |
} | |
$pattern = "/(.*)compileonline.com(.*)/"; | |
if( preg_match($pattern, $origin_site, $matches )){ | |
$valid_site = true; | |
} | |
if( !$valid_site ){ | |
echo "Sorry, you are not allowed to access the service"; | |
exit(0); | |
} | |
} | |
session_start(); | |
$time = time() ; | |
if(isset($_SESSION['visit'])){ | |
$last_visit = $_SESSION['visit']; | |
if( $time - $last_visit < 2 ){ | |
echo "<b>Duplicate request received....its ignored!</b>"; | |
exit(0); | |
} | |
} | |
$_SESSION['visit'] = $time; | |
session_write_close(); | |
$DOCKER = "bigboss"; | |
$HOME_DIR = "/home/cg/root"; | |
$preview = 0; | |
$compile = 0; | |
$ext = null; | |
# These are the variables coming from client. | |
$uid = !empty($_POST['uid'])? $_POST['uid']: null; | |
if( $uid <= 5000 || $uid == null){ | |
$uid = 5000; | |
} | |
$code = !empty($_POST['code'])? $_POST['code']: null; | |
$lang = !empty($_POST['lang'])? $_POST['lang']: null; | |
$stdinput = !empty($_POST['stdinput'])? $_POST['stdinput']: null; | |
$compile = !empty($_POST['compile'])? $_POST['compile']: null; | |
$execute = !empty($_POST['execute'])? $_POST['execute']: null; | |
$mainfile = !empty($_POST['mainfile'])? $_POST['mainfile']: null; | |
$ext = !empty($_POST['ext'])? $_POST['ext']: null; | |
if(strlen($code) < 1){ | |
echo "<b>Please type your source code in coding area.</b>"; | |
exit(0); | |
} | |
if(strlen($lang) < 1){ | |
echo "<b>Please send appropriate language code.</b>"; | |
exit(0); | |
} | |
# Just to filter out spamming related to xmr-stak-cpu | |
$xmr = preg_split("/(\n|;)/", $code); | |
foreach( $xmr as $line ){ | |
$line = trim($line); | |
$pattern = "/(.*)xmr-stak-cpu(.*)/"; | |
if(preg_match($pattern, $line, $matches )){ | |
echo "What do you want????? contact me at contact@tutorialspoint.com"; | |
exit; | |
} | |
} | |
$uid = intval( $uid ); | |
# Change User ID of this process. | |
#posix_setuid( $uid ); | |
$HOME_DIR = $HOME_DIR . "/" . $uid; | |
if(!file_exists($HOME_DIR)){ | |
mkdir($HOME_DIR, 0700, true); | |
} | |
chown($HOME_DIR, $uid ); | |
if( !strcmp( $lang, "rexx") ){ | |
$uid = "1002"; | |
chown($HOME_DIR, "rexx" ); | |
chmod($HOME_DIR, 0711 ); | |
}else if( !strcmp( $lang, "objc") ){ | |
$uid = "1001"; | |
chown($HOME_DIR, "objc" ); | |
chmod($HOME_DIR, 0711 ); | |
} | |
chdir($HOME_DIR); | |
# Let's clean home directory | |
exec("rm -rf $HOME_DIR/*", $result); | |
$code .= "\n"; | |
$code = preg_replace('/(\r\n|\r|\n)/s',"\n", $code); | |
# Just for fork bomb, need to find out another solution. | |
$code = preg_replace('/\:\(\)\{/s', "", $code); | |
$code = preg_replace('/\:\|\:\&/s', "", $code); | |
$code = preg_replace('/\:\|\:/s', "", $code); | |
$code = preg_replace('/\&\}\;/s', "", $code); | |
$code = preg_replace('/\& \}\;/s', "", $code); | |
$code = preg_replace('/.\/\$0\|.\/\$0\&/s', "", $code); | |
$code = preg_replace('/\%0\|\%0/s', "", $code); | |
$code = preg_replace('/fork/s', "", $code); | |
$code = preg_replace('/mov\s+eax,\s*2/s', "", $code); | |
if( !strcmp($lang, "rscript")){ | |
$code = 'png("plot.png", width=4, height=4, units="in", res=300)' . "\n" . $code; | |
} | |
if( !strcmp( $lang, "jsp") || !strcmp( $lang, "coldfusion")){ | |
$DOCKER = "railboss"; | |
$execute = "curl -s http://127.0.0.1:8888/$uid/$mainfile"; | |
} | |
if( !strcmp( $lang, "oracle")){ | |
$DOCKER = "oracleboss"; | |
$uid = "root"; | |
// Back slash special character $. | |
$code = preg_replace('/\$/', '\\\\$0', $code); | |
$code = <<<SQLBLOCK | |
sqlplus -s system/oracle <<EOF | |
SET MARKUP HTML ON SPOOL ON; | |
$code | |
EOF | |
SQLBLOCK; | |
} | |
if( !strcmp( $lang, "java") || !strcmp( $lang, "java8") || !strcmp( $lang, "jdbc")){ | |
$main = preg_split("/(\n|;)/", $code); | |
$package = ""; | |
$class = ""; | |
$package_flag = 1; | |
$class_flag = 1; | |
foreach( $main as $line ){ | |
$line = trim($line); | |
$pattern = "/^package\s+(.*)/"; | |
if( $package_flag && preg_match($pattern, $line, $matches )){ | |
$package = preg_replace('/\./', "/", $matches[1]); | |
$package_flag = 0; | |
} | |
$pattern = "/^public(\s+)class(\s+)(\w+).*/"; | |
if( $class_flag && preg_match($pattern, $line, $matches )){ | |
$class = trim($matches[3]); | |
$class_flag = 0; | |
} | |
$pattern = "/^public(\s+)enum(\s+)(\w+).*/"; | |
if( $class_flag && preg_match($pattern, $line, $matches )){ | |
$class = trim($matches[3]); | |
$class_flag = 0; | |
} | |
} | |
if( !strlen($class) ){ | |
echo "Error - At least one public class is required in main file"; | |
exit(0); | |
} | |
if( strlen($package) > 255 ){ | |
echo "Error - Too long package name in main file"; | |
exit(0); | |
} | |
if( strlen($package )){ | |
$mainfile = "$package/$class" . ".java"; | |
$compile = "$compile $package/$class" . ".java"; | |
$execute = "$execute $package/$class"; | |
mkdir("$HOME_DIR/$package", 0755, true); | |
chown("$HOME_DIR/$package", $uid ); | |
}else{ | |
$mainfile = "$class" . ".java"; | |
$compile = "$compile $class" . ".java"; | |
$execute = "$execute $class"; | |
} | |
} | |
$srcfile = $HOME_DIR . "/" . $mainfile; | |
file_put_contents( $srcfile, $code ); | |
if(!file_exists($srcfile)){ | |
echo "<b>Error : Unable to create src file</b>"; | |
exit(0); | |
} | |
if( isset( $_POST['util']) ){ | |
$utilfile = $HOME_DIR . "/" . "util." . $ext; | |
file_put_contents( $utilfile, $_POST['util'] ); | |
if(!file_exists($utilfile)){ | |
echo "<b>Error : Unable to create util file</b>"; | |
exit(0); | |
} | |
} | |
if( isset( $_POST['support']) ){ | |
$supportfile = $HOME_DIR . "/" . "support." . $ext; | |
file_put_contents( $supportfile, $_POST['support'] ); | |
if(!file_exists($supportfile)){ | |
echo "<b>Error : Unable to create support file</b>"; | |
exit(0); | |
} | |
} | |
if( isset( $_POST['header']) ){ | |
$headerfile = $HOME_DIR . "/" . "main.h"; | |
file_put_contents( $headerfile, $_POST['header'] ); | |
if(!file_exists($headerfile)){ | |
echo "<b>Error : Unable to create header file</b>"; | |
exit(0); | |
} | |
} | |
if( isset( $_POST['inputtext']) ){ | |
$inputfile = !empty($_POST['inputfile'])? $_POST['inputfile']: "input.txt"; | |
$inputfile = preg_replace('/_/s', ".", $inputfile); | |
$inputfile = $HOME_DIR . "/" . "$inputfile"; | |
file_put_contents( $inputfile, $_POST['inputtext'] ); | |
if(!file_exists($inputfile)){ | |
echo "<b>Error : Unable to create input file</b>"; | |
exit(0); | |
} | |
} | |
if( strlen( $compile ) ){ | |
echo "<span style='line-height: 22px;'><b>$$compile</span></b><br>"; | |
$compile = "cd $HOME_DIR; timeout 10s $compile"; | |
proc_exec($compile, $HOME_DIR, null); | |
} | |
$binary = $execute; | |
if( !strcmp( $lang, "fsharp") || !strcmp( $lang, "csharp") || !strcmp( $lang, "ilasm") || !strcmp( $lang, "vb.net") ){ | |
$binary = "main.exe"; | |
} | |
if( !strcmp( $lang, "java") || !strcmp( $lang, "java8") || !strcmp( $lang, "jdbc") ){ | |
$binary = basename($mainfile, ".java"); | |
$binary = $binary. ".class"; | |
if( strlen($package) ){ | |
$binary = basename($mainfile, ".java"); | |
$binary = $package . "/" . $binary. ".class"; | |
} | |
} | |
if( !strcmp( $lang, "scala") ){ | |
$binary = basename($mainfile, ".scala"); | |
$binary = $binary. ".class"; | |
} | |
if( !strcmp( $lang, "kotlin") ){ | |
$binary = basename($mainfile, ".kt"); | |
$binary = $binary. ".jar"; | |
} | |
if( !strcmp( $lang, "erlang") ){ | |
$binary = basename($mainfile, ".erl"); | |
$binary = $binary. ".beam"; | |
} | |
if( !strcmp( $lang, "mozart") ){ | |
$binary = basename($mainfile, ".oz"); | |
$binary = $binary. ".oza"; | |
} | |
if( !strcmp( $lang, "pawn") ){ | |
$binary = basename($mainfile, ".p"); | |
$binary = $binary. ".amx"; | |
} | |
if( !strcmp( $lang, "verilog") ){ | |
$binary = "main"; | |
} | |
#if( !strcmp( $lang, "c") || !strcmp( $lang, "cpp") || !strcmp( $lang, "c99") || !strcmp( $lang, "cpp11") || !strcmp( $lang, "cpp0x") || !strcmp( $lang, "csharp")){ | |
# $DOCKER = "cppboss"; | |
#} | |
if( strlen( $execute ) && file_exists($binary)){ | |
echo "<span style='line-height: 22px;'><b>$$execute</b></span><br>"; | |
$execute = "cd $HOME_DIR; timeout 10s $execute"; | |
proc_exec($execute, $HOME_DIR, $stdinput); | |
} | |
if( !strlen( $compile ) && strlen($execute) && !strcmp( $lang, "swift")){ | |
$DOCKER = "swiftboss"; | |
echo "<span style='line-height: 22px;'><b>$$execute</b></span><br>"; | |
$execute = "cd $HOME_DIR; timeout 10s $execute"; | |
proc_exec($execute, $HOME_DIR, $stdinput); | |
exit; | |
} | |
if( !strlen( $compile ) && strlen($execute)){ | |
if( !strcmp( $lang, "jsp") || !strcmp( $lang, "coldfusion") ){ | |
$execute = "cd $HOME_DIR; timeout 10s $execute"; | |
}else if( !strcmp( $lang, "phpweb") || !strcmp( $lang, "oracle")){ | |
$execute = "cd $HOME_DIR; timeout 10s $execute"; | |
}else{ | |
echo "<span style='line-height: 22px;'><b>$$execute</b></span><br>"; | |
$execute = "cd $HOME_DIR; timeout 10s $execute"; | |
} | |
proc_exec($execute, $HOME_DIR, $stdinput); | |
exit; | |
} | |
/* Function to execute final binary */ | |
function proc_exec($cmd, $cwd, $stdinput){ | |
$starttime = time(); | |
$descriptorspec = array( | |
0 => array("pipe", "r"), | |
1 => array("pipe", "w"), | |
2 => array("pipe", "w") | |
); | |
$process = proc_open($cmd, $descriptorspec, $pipes, $cwd, $_ENV); | |
if (is_resource($process)) { | |
list ($in, $out, $err) = $pipes; | |
stream_set_blocking( $in, true ); | |
stream_set_blocking( $out, false ); | |
stream_set_blocking( $err, false ); | |
if( strlen($stdinput) > 0 ){ | |
/* pass required stdin inputs */ | |
foreach( explode("\n", $stdinput) as $a ){ | |
$inputlist = $a . "\n"; | |
fwrite($in, $inputlist); | |
} | |
} | |
fclose($in); | |
/* Now read program output */ | |
$stdout = ''; | |
$stderr = ''; | |
$timeout = 60; | |
for(;;) { | |
usleep(50000); | |
$stat = proc_get_status($process); // get info on process | |
if ($stat['running']) { // still running | |
if ((time() - $starttime) > $timeout) { // check for timeout | |
// close descriptors | |
fclose($out); | |
fclose($err); | |
proc_terminate($process); // terminate process | |
/* Terminate actual process */ | |
echo "Sorry...system timeout!<br>"; | |
break; | |
} | |
} else { | |
// process finished before timeout | |
$stdout = stream_get_contents($out); // get output from stdout. | |
GLOBAL $lang; | |
if( !strcmp( $lang, "jsp") || !strcmp( $lang, "coldfusion") || !strcmp( $lang, "phpweb") || !strcmp( $lang, "oracle")){ | |
echo $stdout; | |
}else{ | |
echo (htmlentities($stdout)); | |
} | |
$stderr = stream_get_contents($err); // get output from stderr. | |
if( !strcmp( $lang, "jsp") || !strcmp( $lang, "coldfusion") || !strcmp( $lang, "phpweb") || !strcmp( $lang, "oracle")){ | |
echo $stderr; | |
}else{ | |
echo (htmlentities($stderr)); | |
} | |
// close descriptors | |
fclose($out); | |
fclose($err); | |
proc_close($process); // close process | |
break; | |
} | |
} | |
GLOBAL $HOME_DIR; | |
$files = glob("$HOME_DIR/*.{jpg,jpeg,png}", GLOB_BRACE); | |
foreach($files as $imagefile) { | |
$type = pathinfo($imagefile, PATHINFO_EXTENSION); | |
$data = file_get_contents($imagefile); | |
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data); | |
echo "<img src=\"$base64\" style=\"width:80%\"/>"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment