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
|
<?php
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();
$code = $_POST["code"];
$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__);
if (is_string($resultStr)) {
$result = json_decode($resultStr, true);
}
}
?>
<!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="codemirror.js"></script>
<script src="clike.js"></script>
</head>
<body>
<h1>☕️ Java Compiler</h1>
<p>Powered by Eclipse Temurin</p>
<form method="POST">
<textarea maxlength="16383" id="editor" name="code" rows="20"><?= htmlentities($code ?? $defaultCode) ?></textarea>
<br>
<button type="submit">▶ Compile & run</button>
<?php if ($result !== null): ?>
<hr>
<strong>Compilation:</strong><br>
<blockquote>
<strong>Status:</strong> <span style="color:<?= match($result["compile"]["status"]) {
"0" => "green",
default => "red",
} ?>"><?= match($result["compile"]["status"]) {
"0" => "Successful",
"124" => "Compilation timed out",
default => "Failed (Status " . $result["compile"]["status"] . ")",
} ?></span><br>
<?php if ($result["compile"]["stdout"]): ?>
<strong>Output:</strong><br>
<pre><code><?= htmlentities($result["compile"]["stdout"]) ?></code></pre>
<?php endif; ?>
<?php if ($result["compile"]["stderr"]): ?>
<strong>Error output:</strong><br>
<pre><code><?= htmlentities($result["compile"]["stderr"]) ?></code></pre>
<?php endif; ?>
</blockquote>
<hr>
<strong>Execution:</strong><br>
<?php if ($result["run"]["status"] !== null): ?>
<blockquote>
<strong>Status:</strong> <span style="color:<?= match($result["run"]["status"]) {
"0" => "green",
default => "red",
} ?>"><?= match($result["run"]["status"]) {
"0" => "Successful",
"124" => "Execution timed out",
default => "Failed (Status " . $result["run"]["status"] . ")",
} ?></span><br>
<?php if ($result["run"]["stdout"]): ?>
<strong>Output:</strong><br>
<pre><code><?= htmlentities($result["run"]["stdout"]) ?></code></pre>
<?php endif; ?>
<?php if ($result["run"]["stderr"]): ?>
<strong>Error output:</strong><br>
<pre><code><?= htmlentities($result["run"]["stderr"]) ?></code></pre>
<?php endif; ?>
</blockquote>
<?php else: ?>
<em>Did not execute</em>
<?php endif; ?>
<hr>
<strong>Runner ID:</strong> <code><?= htmlentities($id) ?></code><br>
<?php endif; ?>
</form>
<script>
var editor = CodeMirror.fromTextArea(document.getElementById("editor"), {
lineNumbers: true,
mode: "text/x-java",
theme: "eclipse",
tabSize: 4,
indentWithTabs: false,
smartIndent: true,
indentUnit: 4
});
editor.setSize(900, 500);
</script>
</body>
</html>
|