-
Notifications
You must be signed in to change notification settings - Fork 8
/
load.php
60 lines (50 loc) · 1.62 KB
/
load.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
<?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 load.php";
die;
}
//$stdin = $_REQUEST["stdin"];
$problem = $_REQUEST["problem"];
$preview = $_REQUEST["preview"]=='True' ? 'True' : 'False';
$student = $WS_AUTHINFO['username'];
// for code review by instructor
if (array_key_exists('student', $_REQUEST) && $_REQUEST["student"]!=""){
$student = $_REQUEST["student"];
}
// only accept characters that cannot cause problems
$regex = "[_0-9a-zA-Z/-]+";
if (!preg_match("@^$regex\$@", $problem)) {
echo "Internal error, problem name \"$problem\" doesn't match $regex";
die;
}
$descriptorspec = array(
0 => array("pipe", "r"), // stdin
1 => array("pipe", "w"), // stdout
2 => array("pipe", "w"), // stderr
);
$process = proc_open("python3 ./load.py " . $problem . " " . $student . " " . $preview, $descriptorspec, $pipes);
if (!is_resource($process)) {
echo "Internal error, could not run Websheet program";
die;
}
fwrite($pipes[0], json_encode($WS_AUTHINFO));
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;