HtmGem/index.php

156 lines
4.1 KiB
PHP
Raw Normal View History

2021-03-09 00:48:07 +01:00
<?php
require_once "lib-htmgem.php";
use function htmgem\translateGemToHtml;
2021-03-09 01:21:50 +01:00
# to false only if textDecoration=0 in the URL
$textDecoration = "0" != @$_REQUEST['textDecoration'];
2021-03-09 01:21:50 +01:00
2021-03-09 11:26:31 +01:00
/* The url argument is always absolute compared to the document root
* The leading slash is removed. so url=/foo/bar and url=foo/bar ar the same.
*/
$url = @$_REQUEST["url"];
2021-03-09 01:21:50 +01:00
######################################## Installation page
2021-03-09 00:48:07 +01:00
if (empty($url)) {
if (!file_exists("index.gmi")) {
http_response_code(403);
die("<!-- index.gmi missing -->");
}
2021-03-09 00:48:07 +01:00
?>
<!DOCTYPE html>
<html lang="fr">
<head>
<title>Installation de HtmGem</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style>
2021-03-09 11:26:31 +01:00
<?php include("css/htmgem.css"); ?>
2021-03-09 00:48:07 +01:00
</style>
</head>
<body>
<?php
echo translateGemToHtml(@file_get_contents("index.gmi"));
echo "</body>\n</html>\n";
die();
}
2021-03-09 01:21:50 +01:00
######################################## /Installation page
2021-03-09 00:48:07 +01:00
2021-03-09 11:26:31 +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-09 01:21:50 +01:00
$fileContents = @file_get_contents($filePath);
2021-03-09 00:48:07 +01:00
2021-03-09 01:21:50 +01:00
######################################## 404 page
if (empty($fileContents)) {
2021-03-09 11:26:31 +01:00
error_log("HtmGem: 404 $url $filePath");
2021-03-09 01:21:50 +01:00
http_response_code(404); ?>
<!DOCTYPE html>
<html lang="fr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style>
2021-03-09 11:26:31 +01:00
<?php include("css/htmgem.css"); ?>
2021-03-09 01:21:50 +01:00
</style>
</head>
<body>
<?php
$text404 = <<<EOF
2021-03-09 11:26:31 +01:00
# ⚠ Page non trouvée
2021-03-09 01:21:50 +01:00
**$url**
2021-03-09 01:21:50 +01:00
=> $url Recharger 🔄
2021-03-09 11:26:31 +01:00
=> /
2021-03-09 01:21:50 +01:00
EOF;
echo translateGemToHtml($text404);
echo "</body>\n</html>";
die();
2021-03-09 00:48:07 +01:00
}
2021-03-09 01:21:50 +01:00
######################################## /404 page
2021-03-09 00:48:07 +01:00
# Removes the Byte Order Mark
$fileContents = preg_replace("/\xEF\xBB\xBF/", "", $fileContents);
# 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-09 11:26:31 +01:00
###################################### CSS Management
/**
* if &style=source displays the source directly and stops.
* if there's a filename.css besides filename.gmi, use the css and stops.
* if &style=<NOTHING> then embbed the default style, and stops.
* if &style=<word not beginngin by slash> then use htmgem/word.css
* if &style=/ then use the as as stylesheet.
**/
if ("source" == $style) {
$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));
readfile($filePath);
exit();
2021-03-09 12:02:04 +01:00
} elseif ("pre" == $style) {
$fileContents = htmlspecialchars($fileContents, ENT_HTML5|ENT_NOQUOTES, "UTF-8", false);
echo <<<EOL
<!DOCTYPE html>
<html>
<head>
<title>$page_title</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<pre>$fileContents</pre>
</body>
</html>
EOL;
2021-03-09 11:26:31 +01:00
} else {
$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 / !
$cssContent = "<link type='text/css' rel='StyleSheet' href='$localCss'>";
} else {
if (empty($style)) {
$cssContent =
"<style>\n"
.@file_get_contents("css/htmgem.css")
."</style>\n";
} else {
if ("none" == $style) {
$cssContent = "";
} else {
if ("/" == $style[0])
$href = $style;
else
$href = "/htmgem/css/$style.css";
$cssContent = "<link type='text/css' rel='StyleSheet' href='$href'>";
}
}
}
echo <<<EOL
2021-03-09 00:48:07 +01:00
<!DOCTYPE html>
<html lang="fr">
<head>
<title>$page_title</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
2021-03-09 11:26:31 +01:00
$cssContent
2021-03-09 00:48:07 +01:00
</head>
<body>
EOL;
2021-03-09 11:26:31 +01:00
echo "\n".translateGemToHtml($fileContents);
echo "</body>\n</html>\n";
}
2021-03-09 00:48:07 +01:00
ob_end_flush();
?>