HtmGem/index.php

135 lines
4.1 KiB
PHP

<?php declare(strict_types=1);
require_once "lib-htmgem.inc.php";
require_once "lib-html.inc.php";
require_once "lib-io.inc.php";
define("DEFAULT_CSS", "/css/default/htmgem.css");
$documentRoot = $_SERVER['DOCUMENT_ROOT'];
$scheme = (@$_SERVER['REQUEST_SCHEME']??"http")."://";
$domain = $_SERVER['HTTP_HOST'];
$php_self = $_SERVER['PHP_SELF']; // by default: /htmgem/index.php
$php_self_dir = dirname($php_self);
$url = @$_REQUEST["url"];
$urlRewriting = @$_REQUEST["rw"]=="1";
/**
* Installation page
*
* Accessing directly /htmgem will make display the self-hosted documentation
* contained in "index.gmi". If it's removed, display an empty page with a
* comment
*/
if (empty($url)) {
if (!file_exists("index.gmi")) {
http_response_code(403);
} else {
$gt_html = new \htmgem\GemTextTranslate_html(file_get_contents("index.gmi"), true, "$php_self?url=", $php_self_dir);
if (empty($gt_html->getCss())) $gt_html->addCss($php_self_dir.DEFAULT_CSS);
// No URL Rewritting assumed
echo \htmgem\html\getHtmlWithMenu($gt_html, $scheme, $domain, $php_self, "$php_self?url=");
}
exit();
}
$url = \htmgem\resolve_path(
// Some webservers (Apache) don't add the slash
// while others (Nginx) do…
( $url[0] == "/" ? "" : "/" ) . $url
);
if (!preg_match("/\.gmi$/", $url)) {
if ($url[-1] == "/")
$url = $url."index.gmi";
else
$url = $url."/index.gmi";
}
# Removes the headling and trailling slashes, to be sure there's not any.
$filePath = rtrim($_SERVER['DOCUMENT_ROOT'], "/")."/".ltrim($url, "/");
switch(true) {
case !realPath($filePath):
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;
}
/* 404 page
*/
if ($go404) {
error_log("HtmGem: 404 $url $filePath");
http_response_code(404);
$page404 = \htmgem\html\get404GmiPage($url);
$gt_html = new \htmgem\GemTextTranslate_html($page404);
if (empty($gt_html->getCss())) $gt_html->addCss($php_self_dir."/css/htmgem.css");
if ($urlRewriting)
echo \htmgem\html\getHtmlWithMenu($gt_html, $scheme, $domain, $url);
else
echo \htmgem\html\getHtmlWithMenu($gt_html, $scheme, $domain, $url, "$php_self?url=");
exit();
}
# to false only if textDecoration=0 in the URL
$gt_htmlextDecoration = "0" != @$_REQUEST['textDecoration'];
$fileContents = @file_get_contents($filePath);
\htmgem\io\convertToUTF8($fileContents);
/* CSS and special style management
*/
$style = @$_REQUEST['style'];
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));
echo $fileContents;
exit();
} elseif ("src" == $style) {
# Gets the page title: the first occurrence with # at the line start
mb_ereg("#\s*([^\n]+)\n", $fileContents, $matches);
$page_title = @$matches[1];
$fileContents = htmlspecialchars($fileContents, ENT_HTML5|ENT_QUOTES, "UTF-8", true);
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;
exit();
}
if ($urlRewriting)
$gt_html = new \htmgem\GemTextTranslate_html($fileContents, $gt_htmlextDecoration);
else
$gt_html = new \htmgem\GemTextTranslate_html($fileContents, $gt_htmlextDecoration, "$php_self?url=", dirname($url));
if (empty($style)) {
$gt_html->addCss($php_self_dir.DEFAULT_CSS);
} else {
$style = preg_replace("/,/", "/", $style);
if ("/" == $php_self_dir) $php_self_dir = ""; # dirname() never use a final slash except for the root
$gt_html->addCss("$php_self_dir/css/$style");
}
if ($urlRewriting)
echo \htmgem\html\getHtmlWithMenu($gt_html, $scheme, $domain, $url);
else
echo \htmgem\html\getHtmlWithMenu($gt_html, $scheme, $domain, $url, "$php_self?url=");
?>