summaryrefslogtreecommitdiff
path: root/public/index.php
diff options
context:
space:
mode:
Diffstat (limited to 'public/index.php')
-rw-r--r--public/index.php135
1 files changed, 135 insertions, 0 deletions
diff --git a/public/index.php b/public/index.php
new file mode 100644
index 0000000..dc421b9
--- /dev/null
+++ b/public/index.php
@@ -0,0 +1,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>&#9749;&#65039; 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">&#9654;&nbsp;Compile &amp; 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>