Switch babblebox and map to new render model. Add positioning dot to map.

This commit is contained in:
Sky Johnson 2024-12-13 17:28:51 -06:00
parent 93638f65d4
commit 2801550bdd
4 changed files with 133 additions and 87 deletions

View File

@ -168,7 +168,7 @@ function showchar()
if ($userrow["magiclist"] == "") $userrow["magiclist"] = "None"; if ($userrow["magiclist"] == "") $userrow["magiclist"] = "None";
$array = ["content" => parsetemplate(gettemplate("showchar"), $userrow), "title" => "Character Information"]; $array = ["content" => parsetemplate(gettemplate("showchar"), $userrow), "title" => "Character Information"];
echo parsetemplate("<html>\n" . gettemplate("minimal"), $array); echo render('minimal', $array);
} }
function onlinechar($id) function onlinechar($id)
@ -204,31 +204,39 @@ function onlinechar($id)
function showmap() function showmap()
{ {
$array = ["content" => "<center><img src=\"/img/map.gif\" alt=\"Map\" /></center>", "title" => "Map"]; global $userrow;
echo parsetemplate("<html>\n" . gettemplate("minimal"), $array);
$pos = sprintf(
'<div style="position: absolute; width: 5px; height: 5px; border-radius: 1000px; border: solid 1px black; background-color: red; left: %dpx; top: %dpx;"></div>',
round(258 + $userrow['longitude'] * (500 / 500) - 3),
round(258 - $userrow['latitude'] * (500 / 500) - 3)
);
echo render('minimal', [
'content' => '<img src="/img/map.gif" alt="Map">'.$pos,
'title' => 'Map'
]);
} }
/**
* Either render the latest 40 chats to the babblebox, or add a chat to it and redirect. This is used
* within an iframe.
*/
function babblebox() function babblebox()
{ {
global $userrow; global $userrow;
if (isset($_POST["babble"])) { if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$safecontent = makesafe($_POST["babble"]); $safecontent = make_safe($_POST["babble"]);
if (!empty($safecontent)) { if (!empty($safecontent)) {
db()->query('INSERT INTO babble (posttime, author, babble) VALUES (CURRENT_TIMESTAMP, ?, ?);', [$userrow['username'], $safecontent]); db()->query('INSERT INTO babble (posttime, author, babble) VALUES (CURRENT_TIMESTAMP, ?, ?);',
[$userrow['username'], $safecontent]);
} }
redirect('/babblebox'); redirect('/babblebox');
} }
$babblebox['content'] = '';
$query = db()->query('SELECT * FROM babble ORDER BY id DESC LIMIT 40;'); $query = db()->query('SELECT * FROM babble ORDER BY id DESC LIMIT 40;');
while ($babblerow = $query->fetchArray(SQLITE3_ASSOC)) { echo render('babblebox', ['messages' => $query]);
$new = "<div class=\"message\">[<b>{$babblerow["author"]}</b>] {$babblerow["babble"]}</div>\n";
$babblebox["content"] = $new . $babblebox["content"];
}
$babblebox["content"] .= '<form action="/babblebox" method="post" style="margin-top: 1rem;"><input type="text" name="babble" maxlength="255" style="width: 100%;"><br><input type="submit" name="submit" value="Babble"><input type="reset" name="reset" value="Clear"></form>';
echo parsetemplate("<html>\n" . gettemplate("babblebox"), $babblebox);
} }
/** /**

View File

@ -24,6 +24,26 @@ function redirect(string $location): void
exit; exit;
} }
/**
* Return the path to a view file.
*/
function template(string $name): string
{
return "../templates/$name.php";
}
/**
* Render a view with the given data. Looks for `$path_to_base_view` through `template()`. Can be used redundantly
* within the template.
*/
function render(string $path_to_base_view, array $data = []): string|false
{
ob_start();
extract($data);
require template($path_to_base_view);
return ob_get_clean();
}
function gettemplate($templatename) { // SQL query for the template. function gettemplate($templatename) { // SQL query for the template.
$filename = __DIR__ . "/../templates/" . $templatename . ".php"; $filename = __DIR__ . "/../templates/" . $templatename . ".php";
@ -66,8 +86,12 @@ function is_email($email) { // Thanks to "mail(at)philipp-louis.de" from php.net
} }
function makesafe($d) { /**
return htmlentities($d); * Use htmlentities with UTF-8 encoding to ensure we're only outputting healthy, safe and effective HTML.
*/
function make_safe(string $content): string
{
return htmlentities($content, ENT_QUOTES, 'UTF-8');
} }
/** /**
@ -559,3 +583,4 @@ function guest_only(): void
{ {
if (checkcookies()) redirect('/login'); if (checkcookies()) redirect('/login');
} }

View File

@ -1,10 +1,9 @@
<?php <html lang="en">
$template = <<<HTML
<head> <head>
<title>Babblebox</title> <title>Babblebox</title>
<style type="text/css"> <style type="text/css">
body { body {
background-image: url(/img/background.jpg); background-image: url('/img/background.jpg');
color: black; color: black;
font: 11px verdana; font: 11px verdana;
margin: 0px; margin: 0px;
@ -37,8 +36,23 @@ $template = <<<HTML
} }
</style> </style>
</head> </head>
<body onload="window.scrollTo(0,99999)">
{{content}} <body onload="window.scrollTo(0, 99999)">
<?php
$has_chats = false;
while ($row = $messages->fetchArray(SQLITE3_ASSOC)):
$has_chats = true;
?>
<div class="message">[<b><?= $row['author'] ?></b>] <?= $row['babble'] ?></div>
<?php
endwhile;
if (!$has_chats) echo 'There are no messages. :(';
?>
<form action="/babblebox" method="post" style="margin-top: 1rem;">
<input type="text" name="babble" maxlength="255" style="width: 100%;"><br>
<input type="submit" name="submit" value="Babble">
<input type="reset" name="reset" value="Clear">
</form>
</body> </body>
</html> </html>
HTML;

View File

@ -1,68 +1,67 @@
<?php <html lang="en">
$template = <<<THEVERYENDOFYOU
<head> <head>
<title>{{title}}</title> <title><?= $title ?></title>
<style type="text/css"> <style type="text/css">
body { body {
background-image: url(/img/background.jpg); background-image: url(/img/background.jpg);
color: black; color: black;
font: 11px verdana; font: 11px verdana;
} }
table { table {
border-style: none; border-style: none;
padding: 0px; padding: 0px;
font: 11px verdana; font: 11px verdana;
} }
td { td {
border-style: none; border-style: none;
padding: 3px; padding: 3px;
vertical-align: top; vertical-align: top;
} }
td.top { td.top {
border-bottom: solid 2px black; border-bottom: solid 2px black;
} }
td.left { td.left {
width: 150px; width: 150px;
border-right: solid 2px black; border-right: solid 2px black;
} }
td.right { td.right {
width: 150px; width: 150px;
border-left: solid 2px black; border-left: solid 2px black;
} }
a { a {
color: #663300; color: #663300;
text-decoration: none; text-decoration: none;
font-weight: bold; font-weight: bold;
} }
a:hover { a:hover {
color: #330000; color: #330000;
} }
.small { .small {
font: 10px verdana; font: 10px verdana;
} }
.highlight { .highlight {
color: red; color: red;
} }
.light { .light {
color: #999999; color: #999999;
} }
.title { .title {
border: solid 1px black; border: solid 1px black;
background-color: #eeeeee; background-color: #eeeeee;
font-weight: bold; font-weight: bold;
padding: 5px; padding: 5px;
margin: 3px; margin: 3px;
} }
.copyright { .copyright {
border: solid 1px black; border: solid 1px black;
background-color: #eeeeee; background-color: #eeeeee;
font: 10px verdana; font: 10px verdana;
} }
</style> </style>
</head> </head>
<body><center> <body>
{{content}} <center>
</center></body> <?= $content ?>
</center>
</body>
</html> </html>
THEVERYENDOFYOU;
?>