-
Notifications
You must be signed in to change notification settings - Fork 4
/
cpp.php
executable file
·127 lines (117 loc) · 3.55 KB
/
cpp.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
session_start();
#
# && isset( $_POST['checkbox'] )
#
#ini_set('memory_limit', '1M');
$prefix = $_SESSION['username'];
$main = "main.cpp";
$error_file = 'error.txt';
$output_file = 'output.txt';
$input_file = 'input.txt';
$exec_file = './a.out';
function make_files_and_read_data(){
global $main,$output_file,$prefix,$input_file;
// exec("mkdir $prefix");
if (!is_dir($prefix)) {
mkdir($prefix);
}
exec("chmod -R 777 $prefix");
chdir($prefix);
// echo getcwd();
if( isset( $_POST['code'] ) ){
$code = $_POST['code'];
$c = "code.cpp";
$file_to_save = fopen($c, "w+");
fwrite( $file_to_save, $code );
fclose( $file_to_save);
$f =fopen($main,"w+");
$d = <<<EOD
#include<cstdlib>
#include<unistd.h>
#define system <cstdlib>
#define exec <unistd.h>
\n
EOD;
fwrite($f,$d);
fclose($f);
shell_exec("cat $c>>$main");
shell_exec( "chmod 777 $main" );
shell_exec("touch $output_file");
shell_exec( "chmod 777 $output_file" );
shell_exec("touch $input_file");
shell_exec( "chmod 777 $input_file" );
exec("rm $c");
//echo getcwd();
}
}
function cpp_compile(){
global $main,$error_file,$exec_file,$prefix;
make_files_and_read_data();
$command = "g++ -lm main.cpp -std=c++11 2> ".$error_file;
shell_exec( $command );
exec("chmod 777 a.out");
if(filesize( $error_file)!=0 ){
echo nl2br(file_get_contents( $error_file ));
}
//echo getcwd();
}
function rrmdir($dir) {
if (is_dir($dir)) {
$objects = scandir($dir);
foreach ($objects as $object) {
if ($object != "." && $object != "..") {
if (is_dir($dir."/".$object))
rrmdir($dir."/".$object);
else
unlink($dir."/".$object);
}
}
rmdir($dir);
}
}
function cpp_run(){
global $input_file,$output_file,$prefix,$exec_file;
chdir($prefix);
$dir = realpath($prefix);
$p = $dir."./a.out";
// echo exec("ls -a");
$run = "timeout 2s $p";
$input = fopen( $input_file, "w+" );
if( isset($_POST['in']) ){
fwrite( $input, $_POST['in']);
$run = $run. "<"."$input_file";
}
fclose( $input );
$run = $run.">"."$output_file";
// echo $run;
// echo "<br>";
// echo getcwd();
shell_exec( $run );
//echo filesize($output_file)<=134217728;
if(filesize($output_file)<134217728){
$handle = @fopen($output_file, "r");
$content = fread($handle, 20000);
echo nl2br($content);
fclose($handle);
}
else{
echo '<p style="color:red;text-align:center;font-size:20px;">Time limit exceeded ! </p>'.'<br><br>';
$handle = @fopen($output_file, "r");
$content = fread($handle, 4096);
echo nl2br($content);
fclose($handle);
}
//echo $dir;
// exec('chmod 777 $prefix');
// rrmdir($dir);
// $path = "./".$prefix;
// exec('chmod 777 $prefix');
// exec('cd $prefix && rm *');
// exec('rmdir $prefix');
// echo $prefix;
// echo shell_exec("pwd");
// echo shell_exec("cd $prefix && pwd");
// exec("rm $input_file");
}
?>