2021-03-09 00:48:07 +01:00
|
|
|
|
<?php
|
|
|
|
|
|
2021-03-16 13:49:11 +01:00
|
|
|
|
require_once "lib-htmgem.php";
|
2021-03-09 01:21:50 +01:00
|
|
|
|
|
2021-03-16 20:54:10 +01:00
|
|
|
|
# The url argument is always absolute compared to the document root.
|
2021-03-09 11:26:31 +01:00
|
|
|
|
$url = @$_REQUEST["url"];
|
|
|
|
|
|
2021-03-16 20:54:10 +01:00
|
|
|
|
/* Installation page
|
|
|
|
|
*
|
|
|
|
|
* Accessing directly /htmgem will make display the self-hosted documentation
|
2021-03-25 22:39:52 +01:00
|
|
|
|
* contained in "index.gmi". If it's removed, display an empty page with a
|
2021-03-16 20:54:10 +01:00
|
|
|
|
* comment
|
|
|
|
|
*/
|
2021-03-09 00:48:07 +01:00
|
|
|
|
if (empty($url)) {
|
2021-03-09 02:03:24 +01:00
|
|
|
|
if (!file_exists("index.gmi")) {
|
|
|
|
|
http_response_code(403);
|
|
|
|
|
die("<!-- index.gmi missing -->");
|
|
|
|
|
}
|
2021-03-16 20:54:10 +01:00
|
|
|
|
$t = new \htmgem\GemTextTranslate_html(@file_get_contents("index.gmi"));
|
|
|
|
|
echo $t->getFullHtml();
|
|
|
|
|
exit();
|
2021-03-09 00:48:07 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-18 21:18:49 +01:00
|
|
|
|
$documentRoot = $_SERVER['DOCUMENT_ROOT'];
|
2021-03-09 00:48:07 +01:00
|
|
|
|
|
2021-03-16 20:54:10 +01:00
|
|
|
|
# Removes the headling and trailling slashes, to be sure there's not any.
|
|
|
|
|
$filePath = rtrim($_SERVER['DOCUMENT_ROOT'], "/")."/".ltrim($url, "/");
|
2021-03-09 00:48:07 +01:00
|
|
|
|
|
2021-03-18 21:18:49 +01:00
|
|
|
|
switch(true) {
|
|
|
|
|
case !realPath($filePath):
|
|
|
|
|
case !preg_match("/\.gmi$/", $url): # not finishing by .gmi
|
|
|
|
|
case strpos($filePath, $documentRoot)!==0: # not in web directory
|
|
|
|
|
$go404 = true;
|
|
|
|
|
// Says 404 even if the file exists to not give any information.
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
$go404 = false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-16 20:54:10 +01:00
|
|
|
|
/* 404 page
|
|
|
|
|
*/
|
2021-03-18 21:18:49 +01:00
|
|
|
|
if ($go404) {
|
2021-03-09 11:26:31 +01:00
|
|
|
|
error_log("HtmGem: 404 $url $filePath");
|
2021-03-16 20:54:10 +01:00
|
|
|
|
http_response_code(404);
|
|
|
|
|
$page404 = <<<EOF
|
2021-03-09 11:26:31 +01:00
|
|
|
|
# ⚠ Page non trouvée
|
2021-03-09 01:21:50 +01:00
|
|
|
|
|
2021-03-13 11:45:54 +01:00
|
|
|
|
**$url**
|
2021-03-09 01:21:50 +01:00
|
|
|
|
|
2021-03-18 21:16:30 +01:00
|
|
|
|
=> .. 🔄 🔄
|
2021-03-09 01:21:50 +01:00
|
|
|
|
EOF;
|
2021-03-16 20:54:10 +01:00
|
|
|
|
$t = new \htmgem\GemTextTranslate_html($page404);
|
|
|
|
|
echo $t->getFullHtml();
|
|
|
|
|
exit();
|
2021-03-09 00:48:07 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-18 21:18:49 +01:00
|
|
|
|
# to false only if textDecoration=0 in the URL
|
|
|
|
|
$textDecoration = "0" != @$_REQUEST['textDecoration'];
|
2021-03-16 20:54:10 +01:00
|
|
|
|
|
|
|
|
|
$fileContents = @file_get_contents($filePath);
|
2021-03-09 00:48:07 +01:00
|
|
|
|
# Removes the Byte Order Mark
|
|
|
|
|
$fileContents = preg_replace("/\xEF\xBB\xBF/", "", $fileContents);
|
|
|
|
|
|
|
|
|
|
|
2021-03-16 20:54:10 +01:00
|
|
|
|
/* CSS and special style management
|
|
|
|
|
*/
|
2021-03-09 11:26:31 +01:00
|
|
|
|
|
2021-03-16 20:54:10 +01:00
|
|
|
|
$style = @$_REQUEST['style'];
|
2021-03-09 11:26:31 +01:00
|
|
|
|
if ("source" == $style) {
|
2021-03-13 00:30:49 +01:00
|
|
|
|
$basename = basename($filePath);
|
|
|
|
|
header("Cache-Control: public");
|
|
|
|
|
header("Content-Disposition: attachment; filename=$basename");
|
|
|
|
|
header("Content-Type: text/plain");
|
|
|
|
|
header("Content-Transfer-Encoding: binary");
|
|
|
|
|
header('Content-Length: ' . filesize($filePath));
|
2021-03-16 20:54:10 +01:00
|
|
|
|
echo $fileContents;
|
2021-03-13 00:30:49 +01:00
|
|
|
|
exit();
|
2021-03-09 12:02:04 +01:00
|
|
|
|
} elseif ("pre" == $style) {
|
2021-03-16 20:54:10 +01:00
|
|
|
|
# Gets the page title: the first occurrence with # at the line start
|
|
|
|
|
mb_ereg("#\s*([^\n]+)\n", $fileContents, $matches);
|
|
|
|
|
$page_title = @$matches[1];
|
2021-03-20 15:00:24 +01:00
|
|
|
|
$fileContents = htmlspecialchars($fileContents, ENT_HTML5|ENT_QUOTES, "UTF-8", true);
|
2021-03-09 12:02:04 +01:00
|
|
|
|
echo <<<EOL
|
|
|
|
|
<!DOCTYPE html>
|
|
|
|
|
<html>
|
|
|
|
|
<head>
|
|
|
|
|
<title>$page_title</title>
|
|
|
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
|
|
|
|
</head>
|
2021-03-16 20:54:10 +01:00
|
|
|
|
<pre>
|
|
|
|
|
$fileContents</pre>
|
2021-03-09 12:02:04 +01:00
|
|
|
|
</body>
|
|
|
|
|
</html>
|
|
|
|
|
EOL;
|
2021-03-16 20:54:10 +01:00
|
|
|
|
exit();
|
2021-03-18 14:43:02 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$t = new \htmgem\GemTextTranslate_html($fileContents, $textDecoration);
|
|
|
|
|
if ("none" == $style) {
|
|
|
|
|
$t->addCss("");
|
|
|
|
|
} elseif ("/" == @$style[0]) {
|
|
|
|
|
$t->addCss($style);
|
|
|
|
|
} elseif (empty($style)) {
|
|
|
|
|
$parts = pathinfo($filePath);
|
|
|
|
|
$localCss = $parts["filename"].".css";
|
|
|
|
|
$localCssFilePath = $parts["dirname"]."/".$localCss;
|
|
|
|
|
if (file_exists($localCssFilePath)) {
|
|
|
|
|
# Warning, using htmhem.php?url=… will make $localCss not found
|
|
|
|
|
# as the path is relative to htmgem.php and not / !
|
|
|
|
|
$t->addCss($localCss);
|
2021-03-09 11:26:31 +01:00
|
|
|
|
}
|
2021-03-18 14:43:02 +01:00
|
|
|
|
} else { #TODO: regex check for $style
|
|
|
|
|
$t->addCss("/htmgem/css/$style.css");
|
2021-03-09 11:26:31 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-16 20:54:10 +01:00
|
|
|
|
echo $t->getFullHtml();
|
2021-03-09 00:48:07 +01:00
|
|
|
|
|
|
|
|
|
?>
|