summaryrefslogtreecommitdiff
path: root/index.php
diff options
context:
space:
mode:
Diffstat (limited to 'index.php')
-rw-r--r--index.php83
1 files changed, 83 insertions, 0 deletions
diff --git a/index.php b/index.php
new file mode 100644
index 0000000..23c314f
--- /dev/null
+++ b/index.php
@@ -0,0 +1,83 @@
+<?php
+
+use GeminiFoundation\Client;
+use GeminiFoundation\Gemtext;
+
+require __DIR__ . '/vendor/autoload.php';
+
+$geminiHost = $_ENV['GEMINI_HOST'];
+
+$url = parse_url(
+ "//$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"
+);
+$url['query'] ??= '';
+
+$client = new Client($geminiHost);
+$response = $client->request("$url[path]$url[query]");
+
+ob_start();
+$mime = explode(';', $response->getMeta())[0];
+if (strpos($mime, 'text/gemini') === 0) {
+ $parser = new Gemtext($response->getBody());
+ $lines = $parser->parse();
+
+ $wasListItem = false;
+ $currentListItems = [];
+ foreach ($lines as $line) {
+ if ($wasListItem && $line['type'] !== 'listitem') {
+ echo "<ul>";
+ foreach ($currentListItems as $listItem) {
+ echo "<li>$listItem[text]</li>";
+ }
+ echo "</ul>";
+ $wasListItem = false;
+ $currentListItems = [];
+ }
+
+ if ($line['type'] === 'preformatted_on') {
+ echo "<div>$line[alt]</div><pre>";
+ }
+ else if ($line['type'] === 'preformatted') {
+ echo htmlentities($line['raw']);
+ }
+ else if ($line['type'] === 'preformatted_off') {
+ echo "</pre>";
+ }
+ else if ($line['type'] === 'heading') {
+ echo "<h$line[size]>$line[text]</h$line[size]>";
+ }
+ else if ($line['type'] === 'listitem') {
+ $currentListItems[] = $line;
+ $wasListItem = true;
+ }
+ else if ($line['type'] === 'quote') {
+ echo "<blockquote>$line[text]</blockquote>";
+ }
+ else if ($line['type'] === 'link') {
+ $line['link'] = str_replace(["gemini://$geminiHost", $geminiHost], "https://$geminiHost", $line['link']);
+ $line['text'] = $line['text'] ?: $line['link'];
+ echo "<a href=\"$line[link]\">$line[text]</a>";
+ }
+ else if ($line['type'] === 'text') {
+ echo "<p>$line[text]</p>";
+ }
+ }
+}
+
+else if (strpos($mime, 'image') === 0) {
+ echo "<img src=\"data:{$mime};base64," . base64_encode($response->getBody()) . "\">";
+}
+
+echo "<hr>";
+echo "Proxied from <a href=\"gemini://$geminiHost$url[path]$url[query]\">gemini://$geminiHost$url[path]$url[query]</a>";
+$content = ob_get_clean();
+
+?>
+<!doctype html>
+<html>
+ <head>
+ </head>
+ <body>
+ <?php echo $content; ?>
+ </body>
+</html>