Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add path based redirect #19

Merged
merged 1 commit into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions apache/util.conf
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,34 @@
CustomLog /var/log/apache2/util.berlin.freifunk.net-access.log combined
ErrorLog /var/log/apache2/util.berlin.freifunk.net-error.log
</VirtualHost>

<VirtualHost *:443>
ServerName ff.berlin
ServerAdmin "info@berlin.freifunk.net"
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/ff.berlin/cert.pem
SSLCertificateChainFile /etc/letsencrypt/live/ff.berlin/chain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/ff.berlin/privkey.pem

DocumentRoot /var/www/util.berlin.freifunk.net/www

<Directory /var/www/util.berlin.freifunk.net/www>
Options +FollowSymLinks -Indexes
AllowOverride None
Require all granted
</Directory>

# Always call the knoteninfo.php script
DirectoryIndex knoteninfo.php

# Deny access to all other PHP files
<FilesMatch "\.php$">
Require all denied
</FilesMatch>
<Files "knoteninfo.php">
Require all granted
</Files>

CustomLog /var/log/apache2/ff.berlin-access.log combined
ErrorLog /var/log/apache2/ff.berlin-error.log
</VirtualHost>
63 changes: 53 additions & 10 deletions www/knoteninfo.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,56 @@
<?php

// https://util.berlin.freifunk.net/knoteninfo?knoten=Zwingli-Nord-2GHz&typ=wiki
// Query based redirect
// Example: https://util.berlin.freifunk.net/knoteninfo?knoten=Zwingli-Nord-2GHz&typ=wiki
// typ: wiki, monitor, owm, hopglass
//
// Path based redirect
// Example: https://ff.berlin/d/linie206-core
// /d/ -> documentation (wiki)
// /m/ -> map (hopglass)
// /s/ -> statistics (monitor)

$path_elements = getPathElements($_SERVER['REQUEST_URI'], $_SERVER['SCRIPT_NAME']);

if (count($_GET)) {
$knoten = ($_GET["knoten"] ?? "%");
$typ = ($_GET["typ"] ?? "");
} else if (count($path_elements) == 2) {
switch ($path_elements[0]) {
case "d":
$typ = "wiki";
break;
case "m":
$typ = "hopglass";
break;
case "s":
$typ = "monitor";
break;
}
;
$knoten = $path_elements[1];
} else {
die("Keine gültige Anfrage.");
}

$knoten = ($_GET["knoten"] ?? "%");
$knoten = preg_replace("/\.olsr$/", "", $knoten);

if(preg_match('/[^A-Za-z0-9\\.\\-\\_]/', $knoten)) die("Ungültiger Knotenname.");

$typ = ($_GET["typ"] ?? "");

function getPathElements($request_uri, $script_name) {

if (strpos($request_uri, $script_name) === 0) {
$request_path = substr($request_uri, strlen($script_name));
} else {
$request_path = $request_uri;
}

$parsed_url = parse_url($request_path);
$path = $parsed_url['path'];
return explode('/', trim($path, '/'));
}


function getUrl($url) {
$ctx = stream_context_create(["http" => ["method" => "GET"]]);
Expand All @@ -18,7 +60,8 @@
return $res;
}

function getWikiLink($knoten) {

function getWikiLink($knoten){
$url = "https://wiki.freifunk.net/api.php?action=ask&query=[[Hat_Knoten%3A%3A".$knoten."]]|%3FModification%20date|sort%3DModification%20date|order%3Ddesc&format=json";
$body = getUrl($url);
$json = json_decode($body);
Expand All @@ -32,7 +75,7 @@
$owmurl = "https://openwifimap.net/#detail?node=".$knoten.".olsr";
$hgurl = "https://hopglass.berlin.freifunk.net/#!v:m;n:".$knoten.".olsr";

if($typ === "wiki") {

Check failure on line 78 in www/knoteninfo.php

View workflow job for this annotation

GitHub Actions / Lint Code Base

Variable $typ might not be defined.

Check failure on line 78 in www/knoteninfo.php

View workflow job for this annotation

GitHub Actions / Lint Code Base

Variable $typ might not be defined.
$url = getWikiLink($knoten);
if($url === false) {
header("HTTP/1.0 404 Not Found");
Expand All @@ -56,11 +99,11 @@

echo "<i>$knoten</i> auf...";
echo "<ul>".
"<li><a href=\"$owmurl\">openwifimap.net</a></li>".
"<li><a href=\"$hgurl\">hopglass.berlin.freifunk.net</a></li>".
"<li><a href=\"/knoteninfo?knoten=$knoten&typ=wiki\">wiki.freifunk.net</a></li>".
"<li><a href=\"$monurl\">monitor.berlin.freifunk.net</a></li>".
"</ul>".
"zu <a href=\"https://berlin.freifunk.net/\">berlin.freifunk.net</a></body>";
"<li><a href=\"$owmurl\">openwifimap.net</a></li>".
"<li><a href=\"$hgurl\">hopglass.berlin.freifunk.net</a></li>".
"<li><a href=\"/knoteninfo?knoten=$knoten&typ=wiki\">wiki.freifunk.net</a></li>".
"<li><a href=\"$monurl\">monitor.berlin.freifunk.net</a></li>".
"</ul>".
"zu <a href=\"https://berlin.freifunk.net/\">berlin.freifunk.net</a></body>";

?>