summaryrefslogtreecommitdiff
path: root/public/index.php
blob: 0b3cabc2efeec4fe787323c047f624fc6a6b6e89 (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
<?php

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

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;
    }

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

    mkdir($srcDir, recursive: true);

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

    chdir(__DIR__ . "/..");
    $resultStr = shell_exec("./compile-and-run.sh '$id' Program 2>/dev/null");
    chdir(__DIR__);

    $csrf = csrf_token();

    if (is_string($resultStr)) {
        $result = json_decode($resultStr, true);
        $result["runner"] = $id;
        $result["ok"] = true;
        $result["csrf"] = $csrf;
    }

    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>