|
1 | 1 | <?php
|
2 | 2 |
|
3 |
| -$ip = $_SERVER['REMOTE_ADDR']; |
| 3 | +$ip = $_SERVER['REMOTE_ADDR'] ?? false; |
| 4 | +if ( ! is_string($ip) ) die('No REMOTE_ADDR'); |
4 | 5 |
|
5 | 6 | $public = filter_var(
|
6 | 7 | $ip,
|
|
12 | 13 | die('bad address');
|
13 | 14 | }
|
14 | 15 |
|
15 |
| -$content = isset($_POST['content']) ? $_POST['content'] : false; |
16 |
| -if ( ! is_string($content) ) die('No content'); |
| 16 | +$code = $_POST['code'] ?? false; |
| 17 | +$input = $_POST['input'] ?? false; |
| 18 | +if ( ! is_string($code) ) die('No code'); |
17 | 19 |
|
18 |
| -$json = json_decode($content); |
19 |
| -if ( ! is_object($json)) die ('Bad JSON'); |
20 |
| -die('Yada'); |
| 20 | +$folder = sys_get_temp_dir() . '/compile-' . md5(uniqid()); |
| 21 | +mkdir($folder); |
| 22 | + |
| 23 | +$retval = new \stdClass(); |
| 24 | +$retval->code = $code; |
| 25 | +$retval->input = $input; |
| 26 | +$retval->folder = $folder; |
| 27 | +$retval->compile_status = false; |
| 28 | +$retval->compile_out = false; |
| 29 | +$retval->compile_err = false; |
| 30 | +$retval->run_status = false; |
| 31 | +$retval->run_out = false; |
| 32 | +$retval->run_err = false; |
| 33 | + |
| 34 | + |
| 35 | +$descriptorspec = array( |
| 36 | + 0 => array("pipe", "r"), // stdin is a pipe that the child will read from |
| 37 | + 1 => array("pipe", "w"), // stdout is a pipe that the child will write to |
| 38 | + 2 => array("pipe", "w") // stderr is a file to write to |
| 39 | +); |
| 40 | + |
| 41 | +$cwd = $folder; |
| 42 | +$env = array( |
| 43 | + 'some_option' => 'aeiou', |
| 44 | + 'PATH' => '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin', |
| 45 | +); |
| 46 | + |
| 47 | +$command = 'gcc -ansi -x c -o a.out -'; |
| 48 | +$process = proc_open($command, $descriptorspec, $pipes, $cwd, $env); |
| 49 | + |
| 50 | +if (is_resource($process)) { |
| 51 | + // $pipes now looks like this: |
| 52 | + // 0 => writeable handle connected to child stdin |
| 53 | + // 1 => readable handle connected to child stdout |
| 54 | + // Any error output will be appended to /tmp/error-output.txt |
| 55 | + |
| 56 | + fwrite($pipes[0], $code); |
| 57 | + fclose($pipes[0]); |
| 58 | + |
| 59 | + $retval->compile_out = stream_get_contents($pipes[1]); |
| 60 | + fclose($pipes[1]); |
| 61 | + $retval->compile_err = stream_get_contents($pipes[2]); |
| 62 | + fclose($pipes[2]); |
| 63 | + |
| 64 | + // It is important that you close any pipes before calling |
| 65 | + // proc_close in order to avoid a deadlock |
| 66 | + $retval->compile_status = proc_close($process); |
| 67 | + |
| 68 | +} |
| 69 | + |
| 70 | +if ( $retval->compile_status === 0 ) { |
| 71 | + |
| 72 | +$command = './a.out'; |
| 73 | +$process = proc_open($command, $descriptorspec, $pipes, $cwd, $env); |
| 74 | + |
| 75 | +if (is_resource($process)) { |
| 76 | + // $pipes now looks like this: |
| 77 | + // 0 => writeable handle connected to child stdin |
| 78 | + // 1 => readable handle connected to child stdout |
| 79 | + // Any error output will be appended to /tmp/error-output.txt |
| 80 | + |
| 81 | + fwrite($pipes[0], $input); |
| 82 | + fclose($pipes[0]); |
| 83 | + |
| 84 | + $retval->run_out = stream_get_contents($pipes[1]); |
| 85 | + fclose($pipes[1]); |
| 86 | + $retval->run_err = stream_get_contents($pipes[2]); |
| 87 | + fclose($pipes[2]); |
| 88 | + |
| 89 | + // It is important that you close any pipes before calling |
| 90 | + // proc_close in order to avoid a deadlock |
| 91 | + $retval->run_status = proc_close($process); |
| 92 | + |
| 93 | +} |
| 94 | + |
| 95 | +header("Content-type: application/json; charset=utf-8"); |
| 96 | +echo(json_encode($retval, JSON_PRETTY_PRINT)); |
0 commit comments