forked from daveagp/websheets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
submit.php
66 lines (54 loc) · 1.97 KB
/
submit.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
<?php
include_once('auth.php');
global $WS_AUTHINFO;
if ($WS_AUTHINFO["error_span"] != "") {
echo json_encode($WS_AUTHINFO);
die();
}
if (!array_key_exists('stdin', $_REQUEST)
|| !array_key_exists('problem', $_REQUEST))
{
echo "Internal error, malformed request to submit.php";
die;
}
$client_request = json_decode($_REQUEST["stdin"], true);
if ($client_request == FALSE) {
echo "Internal error, request did not receive a json on stdin";
die;
}
$problem = $_REQUEST["problem"];
// only accept characters that cannot cause problems
if (!preg_match("@^[0-9a-zA-Z_/-]+$@", $problem)) {
echo "Internal error, malformed problem \"$problem\"";
die;
}
$descriptorspec = array(
0 => array("pipe", "r"), // stdin
1 => array("pipe", "w"), // stdout
2 => array("pipe", "w"), // stderr
);
$stdin = json_encode(array("client_request" => $client_request,
"authinfo" => $WS_AUTHINFO,
"php_data" => array("user"=>$WS_AUTHINFO['username'],
"problem"=>$problem,
"meta"=>array("authdomain"=>$WS_AUTHINFO['domain'],
"preview"=>$client_request['preview'],
"ip"=>$_SERVER['REMOTE_ADDR']))));
$process = proc_open("python3 ./submit.py", $descriptorspec, $pipes);
if (!is_resource($process)) {
echo "Internal error, could not run Websheet program";
die;
}
fwrite($pipes[0], $stdin);
fclose($pipes[0]);
$stdout = stream_get_contents($pipes[1]);
fclose($pipes[1]);
$stderr = stream_get_contents($pipes[2]);
fclose($pipes[2]);
$return_value = proc_close($process);
if ($stderr != "" || $return_value != 0) {
echo "Internal error: <pre>";
echo $stdout . $stderr . "\nReturned $return_value</pre>";
die;
}
echo $stdout;