Skip to content

Commit

Permalink
Added path based redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
noxilixon committed Oct 10, 2023
1 parent 79aa1e0 commit 9f0145b
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 10 deletions.
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>
76 changes: 66 additions & 10 deletions www/knoteninfo.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,66 @@
<?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"] ?? "");
/**
* getPathElements
* @param string $request_uri
* @param string $script_name
* @return array|bool
*/
function getPathElements($request_uri, $script_name) {

Check failure on line 46 in www/knoteninfo.php

View workflow job for this annotation

GitHub Actions / Lint Code Base

Function getPathElements() never returns bool so it can be removed from the return type.

Check failure on line 46 in www/knoteninfo.php

View workflow job for this annotation

GitHub Actions / Lint Code Base

Function getPathElements() never returns bool so it can be removed from the return type.

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, '/'));
}

/**
* getUrl
* @param string $url
* @return string
*/
function getUrl($url) {
$ctx = stream_context_create(["http" => ["method" => "GET"]]);
$fp = fopen($url, "r", false, $ctx);
Expand All @@ -18,7 +69,12 @@ function getUrl($url) {
return $res;
}

function getWikiLink($knoten) {
/**
* getWikiLink
* @param string $knoten
* @return bool|string
*/
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 Down Expand Up @@ -56,11 +112,11 @@ function getWikiLink($knoten) {

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>";

?>

0 comments on commit 9f0145b

Please sign in to comment.