-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphpPage_final.php
109 lines (70 loc) · 1.99 KB
/
phpPage_final.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
<?php
function compile_code($cmd, &$stderr) {
$stderrFile = tempnam(".","temp0");
$descriptorspec = array(
2 => array("file", "$stderrFile", "w") // stderr is a file to write to
);
$process = proc_open($cmd, $descriptorspec, $pipes);
if (is_resource($process)) {
$return_value = proc_close($process);
$stderr = file($stderrFile);
unlink($stderrFile);
}
}
?>
<?php
function run_code($cmd, $inputs, &$stderr, &$stdout) {
$stdoutFile = tempnam(".","temp0");
$stderrFile = tempnam(".","temp0");
$descriptorspec = array(
0 => array("pipe", "r"), // stdin is a pipe that the child will read from
1 => array("file", "$stdoutFile", "w"), // stdout is a file that the child will write to
2 => array("file", "$stderrFile", "w") // stderr is a file to write to
);
$process = proc_open($cmd, $descriptorspec, $pipes);
if (is_resource($process)) {
fwrite($pipes[0], $inputs); //write stdinput to pipe[0]
fclose($pipes[0]);
$return_value = proc_close($process);
$stdout = file($stdoutFile);
$stderr = file($stderrFile);
unlink($stdoutFile);
unlink($stderrFile);
}
}
?>
<?php
$inputs = $_POST['inputs'];
//------------------------------
$string='abcdABCD1234';
while(true){
$filename=rand(1000,9999).str_shuffle($string).rand(1000,9999).'.s';
if(!file_exists($filename)){
break;
}
}
//--------------------------------------
$file = fopen($filename,'w');
fwrite($file, $_POST['formContent']);
function printResult($arrayName){
foreach($arrayName as &$value){
echo $value;
}
}
$string1='arm-linux-gnueabi-gcc -Wall -o '.substr($filename,0,-2).' '.$filename;
compile_code($string1, $stderr1);
if(count($stderr1)!=0){
echo "Compilation error!!\n\n";
printResult($stderr1);
}else{
$string2='qemu-arm -L /usr/arm-linux-gnueabi '.substr($filename,0,-2);
run_code($string2, $inputs, $stderr, $stdout);
if(count($stderr)!=0){
echo "Runtime error!!\n\n";
printResult($stderr);
}
printResult($stdout);
unlink($filename);
unlink(substr($filename,0,-2));
}
?>