-
Notifications
You must be signed in to change notification settings - Fork 4
/
python2.php
executable file
·112 lines (103 loc) · 3.28 KB
/
python2.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
<?php
session_start();
#
# && isset( $_POST['checkbox'] )
#
#ini_set('memory_limit', '1M');
$prefix = $_SESSION['username'];
$main = "main.py";
$error_file = 'error.txt';
$output_file = 'output.txt';
$input_file = 'input.txt';
$exec_file = 'main.py';
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'];
$f =fopen($main,"w+");
fwrite($f,$code);
fclose($f);
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" );
//echo getcwd();
}
}
function python2_compile(){
global $main,$error_file,$exec_file,$prefix;
make_files_and_read_data();
$command = "python2 -m py_compile main.py 2> ".$error_file;
shell_exec( $command );
exec("chmod 777 main.py");
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 python2_run(){
global $input_file,$output_file,$prefix,$exec_file;
chdir($prefix);
// echo exec("ls -a");
$run = "timeout 5s python2 main.py";
$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);
}
$dir = realpath($prefix);
//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");
}
?>