Add geo protocol handler

This commit is contained in:
~lucidiot 2023-03-28 12:55:12 +02:00
parent c94c3e4760
commit d3671039e7
1 changed files with 58 additions and 0 deletions

58
html_assets/geo.php Normal file
View File

@ -0,0 +1,58 @@
<?php
if (!isset($_GET['geo'])) {
http_response_code(200);
?>
<html>
<head>
<title>GeoHack Redirect</title>
</head>
<body>
<p>This is a quick and dirty PHP page that takes a <a href="https://geouri.org/">Geo URI</a> and redirects you to <a href="https://www.mediawiki.org/wiki/GeoHack">GeoHack</a>, on its WMF Toolforge page, to let you open it in other map services. I am no PHP developer, so expect this thing to be broken.</p>
<p>
<p>I made this because I wanted a better <code>geo:</code> protocol handler than those provided by Firefox. Specifically, the Google Maps provider no longer worked and I also wanted to use the French map service IGN and Google Earth.</p>
<h2>Try it</h2>
<form method="GET">
<label for="geo-input">Geo URI:</label>
<input type="url" name="geo" id="geo-input" placeholder="geo:[latitude],[longitude]" pattern="geo:.*" required />
<br />
<button type="submit">Submit</button>
</form>
<hr />
<p>If you have JavaScript enabled, <a href="javascript:navigator.registerProtocolHandler('geo', window.location.toString().split('?')[0].split('#')[0] + '?geo=%s', 'GeoHack Redirect')">click here</a> to register this page as a <code>geo:</code> protocol handler in your browser. <a href="https://caniuse.com/mdn-api_navigator_registerprotocolhandler_scheme_parameter_geo">Supported browsers</a></p>
</body>
</html>
<?php
die();
}
$parsed = parse_url($_GET['geo']);
if ($parsed["scheme"] != "geo") {
http_response_code(400);
echo "Unsupported URL scheme";
die();
}
$parts = explode(";", $parsed["path"]);
if (sizeof($parts) <= 0) {
http_response_code(400);
echo "Unrecognized geo: URL";
die();
};
list($lat, $lng) = explode(",", array_shift($parts));
if (!isset($lat) || !isset($lng)) {
http_response_code(400);
echo "Unrecognized geo: URL";
die();
}
$header = "Location: https://geohack.toolforge.org/geohack.php?params=$lat;$lng";
parse_str(implode("&", $parts), $params);
if (isset($params["u"])) {
$header .= "_dim:" . $params["u"];
}
http_response_code(302);
header($header);
?>