gemini-site/capsule/cgi-bin/nex/nex.php

134 lines
3.3 KiB
PHP
Executable File

#! /usr/bin/php
<?php declare(strict_types=1);
chdir("/home/lehmann/gemini-site/capsule/cgi-bin");
if(isset($_SERVER['QUERY_STRING'])) {
$query=urldecode($_SERVER['QUERY_STRING']);
} else {
$query="";
}
if($query=="") {
echo "20 text/gemini\r\n";
?>
Welcome to nex2gemini proxy
This page provides a gateway on the gemini network to access the nex network
Source is in git for my main capsule
=> https://tildegit.org/alexlehm/gemini-site/src/branch/main/capsule/cgi-bin/nex.php
It runs on Molly-Brown as a PHP CGI script
Please note that this tool currently cuts some corners regarding the format differences between Nex text ad Gemtext, so some formatting does not work and depending on the browser space formatting will not render correctly since spaces are normalized. E.g. in Lagrange the display looks better if the body display font is set to fixed font, when using a web proxy, it does not work well at all. Also some Gemtext features will show even though they are not intended in the Nex text format.
To get started, go to the main page of the Nex project
=> nex.php?nex%3A%2F%2fnex.nightfall.city%2F
<?php
exit;
}
$parts=explode("/", $query, 4);
$proto=$parts[0];
$host=$parts[2];
$path="/".$parts[3];
if(strtolower(substr($path, -4, 4))==".jpg" || strtolower(substr($path, -5, 5))==".jpeg") {
$fp=fsockopen($host, 1900, $errno, $errstr);
if(!$fp) {
echo "42 connection failed $errono $errstr\r\n";
exit;
}
fwrite($fp, $path);
fwrite($fp, "\n");
echo "20 image/jpeg\r\n";
$size=0;
while(!feof($fp)) {
$line=fread($fp, 4096);
echo($line);
$size+=strlen($fp);
if($size>10240) break;
}
fclose($fp);
} else {
$fp=fsockopen($host, 1900);
if(!$fp) {
echo "42 connection failed $errono $errstr\r\n";
exit;
}
fwrite($fp, $path);
fwrite($fp, "\n");
$line=fgets($fp);
if(preg_match("/^document not found/", $line)) {
echo "51 File not found: $line\r\n";
exit;
}
if($line=="") {
echo "42 server didn't return any content\r\n";
exit;
}
echo "20 text/gemini\r\n";
echo "Nex url: ",$proto."//".$host.$path,"\n";
echo "\n";
$size=0;
do {
if(substr($line, 0, 2)=="=>") {
$line=substr($line, 0, strlen($line)-1);
if(preg_match("/^=>[^ ]/", $line)) {
$line="=> ".substr($line, 2);
}
$parts=explode(" ", $line, 3);
$link=$parts[1];
$text=count($parts)>2 ? $parts[2] : "";
if(substr($link, 0, 4)=="nex:") {
$line="=> nex.php?".urlencode($link)." ".$link." ".$text."\n";
}
else if(preg_match("/^=> [a-z]+:/", $line)) {
// keep it like it is, e.g. https
$line=$line."\n";
}
else if(substr($link, 0, 1)=="/") {
$line="=> nex.php?".urlencode("nex://$host".$link)." ".$link." ".$text."\n";
}
else {
$line="=> nex.php?".urlencode("nex://$host".combine($path, $link))." ".$link." ".$text."\n";
}
}
echo $line;
$size+=strlen($line);
if($size>10240) break;
} while(($line=fgets($fp))!==false);
fclose($fp);
}
function combine(string $path, string $link) {
if(substr($path, -1, 1)!="/") {
$path=substr($path, 0, strrpos($path, "/", -1))."/";
}
while(substr($link, 0, 3)=="../") {
$path=substr($path, 0, strrpos($path, "/", -2))."/";
$link=substr($link, 3);
}
return $path.$link;
}