2021-04-02 21:29:46 +02:00
|
|
|
<?php declare(strict_types=1);
|
2021-03-09 00:48:07 +01:00
|
|
|
|
2021-04-02 21:00:09 +02:00
|
|
|
require_once "lib-htmgem.inc.php";
|
|
|
|
require_once "lib-html.inc.php";
|
2021-04-04 21:16:01 +02:00
|
|
|
require_once "lib-io.inc.php";
|
2021-03-09 01:21:50 +01:00
|
|
|
|
2022-07-29 11:08:28 +02:00
|
|
|
define("DEFAULT_CSS", "/css/default/htmgem.css");
|
|
|
|
|
2021-04-11 21:35:55 +02:00
|
|
|
$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);
|
2021-03-09 11:26:31 +01:00
|
|
|
$url = @$_REQUEST["url"];
|
2021-03-28 21:50:36 +02:00
|
|
|
$urlRewriting = @$_REQUEST["rw"]=="1";
|
2021-03-09 11:26:31 +01:00
|
|
|
|
2021-04-02 21:00:09 +02:00
|
|
|
/**
|
|
|
|
* Installation page
|
2021-03-16 20:54:10 +01:00
|
|
|
*
|
|
|
|
* 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);
|
2021-04-11 21:35:55 +02:00
|
|
|
} else {
|
|
|
|
$gt_html = new \htmgem\GemTextTranslate_html(file_get_contents("index.gmi"), true, "$php_self?url=", $php_self_dir);
|
2022-07-29 11:08:28 +02:00
|
|
|
if (empty($gt_html->getCss)) $gt_html->addCss($php_self_dir.DEFAULT_CSS);
|
2021-04-11 21:35:55 +02:00
|
|
|
|
|
|
|
// No URL Rewritting assumed
|
|
|
|
echo \htmgem\html\getHtmlWithMenu($gt_html, $scheme, $domain, $php_self, "$php_self?url=");
|
2021-03-09 02:03:24 +01:00
|
|
|
}
|
2021-03-16 20:54:10 +01:00
|
|
|
exit();
|
2021-03-09 00:48:07 +01:00
|
|
|
}
|
|
|
|
|
2021-04-11 21:35:55 +02:00
|
|
|
$url = \htmgem\resolve_path(
|
|
|
|
// Some webservers (Apache) don't add the slash
|
|
|
|
// while others (Nginx) do…
|
|
|
|
( $url[0] == "/" ? "" : "/" ) . $url
|
|
|
|
);
|
2021-03-28 21:50:36 +02:00
|
|
|
if (!preg_match("/\.gmi$/", $url)) {
|
|
|
|
if ($url[-1] == "/")
|
|
|
|
$url = $url."index.gmi";
|
|
|
|
else
|
|
|
|
$url = $url."/index.gmi";
|
|
|
|
}
|
|
|
|
|
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 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);
|
2021-04-11 21:35:55 +02:00
|
|
|
$page404 = \htmgem\html\get404GmiPage($url);
|
2021-04-02 21:00:09 +02:00
|
|
|
$gt_html = new \htmgem\GemTextTranslate_html($page404);
|
2021-04-11 21:35:55 +02:00
|
|
|
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=");
|
2021-03-16 20:54:10 +01:00
|
|
|
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
|
2021-04-02 21:00:09 +02:00
|
|
|
$gt_htmlextDecoration = "0" != @$_REQUEST['textDecoration'];
|
2021-03-16 20:54:10 +01:00
|
|
|
|
|
|
|
$fileContents = @file_get_contents($filePath);
|
2021-04-04 21:16:01 +02:00
|
|
|
\htmgem\io\convertToUTF8($fileContents);
|
2021-03-09 00:48:07 +01:00
|
|
|
|
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();
|
2022-07-29 11:08:28 +02:00
|
|
|
} elseif ("src" == $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
|
|
|
}
|
|
|
|
|
2021-03-28 21:50:36 +02:00
|
|
|
if ($urlRewriting)
|
2021-04-11 21:35:55 +02:00
|
|
|
$gt_html = new \htmgem\GemTextTranslate_html($fileContents, $gt_htmlextDecoration);
|
2021-03-28 21:50:36 +02:00
|
|
|
else
|
2021-04-11 21:35:55 +02:00
|
|
|
$gt_html = new \htmgem\GemTextTranslate_html($fileContents, $gt_htmlextDecoration, "$php_self?url=", dirname($url));
|
|
|
|
|
2022-08-01 14:34:45 +02:00
|
|
|
if (empty($style)) {
|
|
|
|
$gt_html->addCss($php_self_dir.DEFAULT_CSS);
|
2022-07-29 11:08:28 +02:00
|
|
|
} else {
|
|
|
|
$style = preg_replace("/,/", "/", $style);
|
|
|
|
$gt_html->addCss("$php_self_dir/css/$style");
|
2021-03-09 11:26:31 +01:00
|
|
|
}
|
|
|
|
|
2021-04-11 21:35:55 +02:00
|
|
|
if ($urlRewriting)
|
|
|
|
echo \htmgem\html\getHtmlWithMenu($gt_html, $scheme, $domain, $url);
|
|
|
|
else
|
|
|
|
echo \htmgem\html\getHtmlWithMenu($gt_html, $scheme, $domain, $url, "$php_self?url=");
|
2021-03-09 00:48:07 +01:00
|
|
|
|
|
|
|
?>
|