summaryrefslogtreecommitdiff
path: root/public/index.php
blob: 4f0f0ff1bb72e89feaafe5135a7bd37010eb7e77 (plain)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php

session_name("a3e0f0ee-9f5a-4be2-88cb-3b7ceb626d00");
session_start();

function get(string $id, string $name): ?string {
    $file = __DIR__ . "/../_runners/$id/results/" . $name;
    if (!is_file($file))
        return null;
    return file_get_contents($file);
}

function csrf_token(): string {
    return $_SESSION["csrf-token"] = base64_encode(random_bytes(30));
}

function getid(): string {
    return str_pad(bin2hex(random_bytes(16)), 32, "0", STR_PAD_LEFT);
}

function delTree($dir) { 
    foreach (scandir($dir) as $ent) {
        if ($ent === "." || $ent === "..")
            continue;
        $path = $dir . DIRECTORY_SEPARATOR . $ent;
        if (is_dir($path))
            delTree($path);
        else
            unlink($path);
    }
    return rmdir($dir);
}

$defaultCode = <<<java
public class Program {
    public static void main(String[] args) {
        // Your code here
        System.out.println("Hello, world!");
    }
}
java;

$code = null;
$result = null;

if (isset($_POST["code"]) && strlen($_POST["code"]) <= 16383) {
    $id = getid();

    header("Content-Type: application/json");

    $code = $_POST["code"];
    $csrf = $_POST["csrf"] ?? null;
    
    if ($csrf === null || $csrf !== $_SESSION["csrf-token"]) {
        echo json_encode([
            "ok" => false,
            "message" => "CSRF token mismatch",
            "csrf" => csrf_token(),
        ]);
        exit;
    }

    $runnerDir = __DIR__ . "/../_runners/$id";
    $srcDir = $runnerDir . "/src";

    mkdir($srcDir, recursive: true);

    file_put_contents($srcDir . "/Program.java", $code);

    $isInDocker = getenv("IS_IN_DOCKER") === "1";

    if ($isInDocker)
        file_put_contents("php://stderr", "[debug] In Docker\n");
    chdir(__DIR__ . "/..");

    $stderrFile = sys_get_temp_dir() . "/tmp{$id}.2";
    exec(
        ($isInDocker ? "su-exec root " : "") .
        "./compile-and-run.sh '$id' Program 2>'$stderrFile'", $resultStr, $code);
    $resultStr = implode("\n", $resultStr);
    if (is_file($stderrFile)) {
        $stderrContents = file_get_contents($stderrFile);
        unlink($stderrFile);
        if (!empty($stderrContents)) {
            file_put_contents("php://stderr", "compile-and-run.sh failed: $stderrContents\n");
            if ($code != 0) {
                echo json_encode([
                    "ok" => false,
                    "message" => "Execution failed:\n$stderrContents",
                    "csrf" => csrf_token(),
                ]);
                delTree($runnerDir);
                exit;
            }
        }
    }
    chdir(__DIR__);
    file_put_contents("php://stderr", "result: $result\n");

    $csrf = csrf_token();


    $result = [
        "compile" => [
            "status" => get($id, "c.s"),
            "stdout" => get($id, "c.0"),
            "stderr" => get($id, "c.1"),
        ],
        "run" => [
            "status" => get($id, "r.s"),
            "stdout" => get($id, "r.0"),
            "stderr" => get($id, "r.1"),
        ],
        "runner" => $id,
        "ok" => true,
        "csrf" => $csrf,
    ];

    delTree($runnerDir);

    echo json_encode($result);
    exit;
}

$csrf = csrf_token();

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=916, initial-scale=1.0">
    <title>Java</title>
    <link rel="stylesheet" href="codemirror.css">
    <link rel="stylesheet" href="eclipse.css">
    <link rel="stylesheet" href="site.css">
    <script src="jquery-1.12.4.min.js"></script>
    <script src="codemirror.js"></script>
    <script src="clike.js"></script>
    <script src="site.js"></script>
</head>
<body>
    <div id="loader" style="display: none;">
        <div id="spinner"></div>
    </div>
    <h1>&#9749;&#65039; Java Compiler</h1>
    <p>Powered by Eclipse Temurin</p>
    <form method="POST">
        <input name="csrf" id="csrf-token" type="hidden" value="<?= htmlentities($csrf) ?>">
        <textarea maxlength="16383" id="editor" name="code" rows="20"><?= htmlentities($code ?? $defaultCode) ?></textarea>
        <div id="toolbar">
            <button type="submit">&#9654;&nbsp;Compile &amp; run</button>
        </div>
    </form>
    <div id="output" style="display: none;"></div>
</body>
</html>