Consolidated parsedown to static class function and added setting renderer for post content

This commit is contained in:
Conner Harkness 2025-08-12 14:50:58 -06:00
parent ecb5c59eb7
commit e9c764f8fe
7 changed files with 162 additions and 97 deletions

View File

@ -14,9 +14,6 @@
<?php if (strlen($strContent) > 0): ?>
<div class="footer-content">
<?php
$varParsedown = new Parsedown();
echo $varParsedown->text($strContent);
?>
<?php PageRender::markdown($strContent); ?>
</div>
<?php endif; ?>

View File

@ -12,9 +12,6 @@
<?php if (strlen($strContent) > 0): ?>
<div class="header-content">
<?php
$varParsedown = new Parsedown();
echo $varParsedown->text($strContent);
?>
<?php PageRender::markdown($strContent); ?>
</div>
<?php endif; ?>

View File

@ -1,6 +1,8 @@
<?php
class PageRender
{
public static $varParsedown = null;
public static function message()
{
if (func_num_args() > 0)
@ -33,6 +35,21 @@
Cookie::set("messageClass");
}
public static function markdown($strInput)
{
if (PageRender::$varParsedown == null)
PageRender::$varParsedown = new Parsedown();
$strText = PageRender::$varParsedown->text($strInput);
$strText = Settings::makeReplacements($strText);
?>
<div class="markdown">
<?php echo $strText; ?>
</div>
<?php
}
public static function uploads()
{
$varUploads = [];

View File

@ -1,24 +1,69 @@
<?php
class PostRender
{
public static function processRows($varRows)
{
for ($i = 0; $i < count($varRows); $i++)
{
$r = $varRows[$i];
$r["content"] = Settings::makeReplacements($r["content"]);
$varRows[$i] = $r;
}
return $varRows;
}
public static function rows($varRows)
{
$varUser = UserAuth::getUser();
$varParsedown = new Parsedown();
$intRenderedRows = 0;
$intList = Request::getParam("v") == "list";
$intJson = Request::getParam("v") == "json";
$intText = Request::getParam("v") == "text";
if ($intJson)
Respond::json(PostRender::processRows($varRows));
$strTextBuffer = "";
?>
<div class="post-container">
<?php foreach ($varRows as $r): ?>
<?php if (!UserAuth::visible($r["visibility"])) continue; ?>
<?php if ($intRenderedRows > 0): ?>
<hr />
<?php
$strText = $r["content"];
preg_match("/(^|\n).*?([A-Za-z0-9].*?(\!|\.|\,|\?|\n))/i", $strText, $varTitles);
$strTitle = $varTitles[2];
$strTitle = trim($strTitle);
?>
<?php if ($intList): ?>
<div class="post-title">
<a href="/<?= $r["id"]; ?>"><?= $strTitle; ?></a> &middot; by <?= $r["username"]; ?>
</div>
<?php
$intRenderedRows++;
continue;
?>
<?php endif; ?>
<?php if ($intRenderedRows > 0): ?>
<hr />
<?php
$strTextBuffer .= "\n\n---\n\n";
?>
<?php endif; ?>
<!-- <?= $strTitle; ?> -->
<div class="post">
<div class="post-body">
<?php echo $varParsedown->text($r["content"]); ?>
<?php PageRender::markdown($r["content"]); ?>
<?php
$strTextBuffer .= $r["content"];
?>
</div>
<div class="post-footer">
@ -39,6 +84,12 @@
&middot; <a href="/post/<?= $r["id"]; ?>">Edit</a>
<?php endif; ?>
</div>
<?php
$strTextBuffer .= "\n";
$strTextBuffer .= "\n* by {$r["username"]}";
$strTextBuffer .= "\n* on {$r["created"]}";
?>
</div>
</div>
@ -46,10 +97,26 @@
<?php endforeach; ?>
<?php if ($intRenderedRows < 1): ?>
<div>Sorry, there is nothing here to show.</div>
<?php
$strMessage = "Sorry, there is nothing here to show.";
$strTextBuffer .= $strMessage;
?>
<div><?= $strMessage; ?></div>
<?php endif; ?>
</div>
<?php
if ($intText)
{
ob_clean();
header("Content-Type: text/plain");
$strTextBuffer = Settings::makeReplacements($strTextBuffer);
echo $strTextBuffer;
ob_end_flush();
exit;
}
}
}
?>

View File

@ -6,6 +6,7 @@
public static function get($strSettingName=null, $strDefault="", $intSave=0)
{
global $c;
$strOutput = $strDefault;
if (Settings::$varValues == null)
{
@ -22,12 +23,12 @@
return Settings::$varValues;
if (array_key_exists($strSettingName, Settings::$varValues))
return Settings::$varValues[$strSettingName];
$strOutput = Settings::$varValues[$strSettingName];
if ($intSave)
Settings::set($strSettingName, $strDefault);
return $strDefault;
return trim($strOutput);
}
public static function set($strSettingName, $strValue)
@ -35,6 +36,7 @@
Settings::$varValues = null;
global $c;
$strValue = trim($strValue);
$varExisting = $c->query("
SELECT *
from settings
@ -58,5 +60,21 @@
$strValue,
$strSettingName);
}
public static function makeReplacements($strText)
{
// Handle nested setting value injection:
preg_match_all("/\{\{([A-Za-z0-9_]{1,})\}\}/i", $strText, $varMatches);
if (count($varMatches[1]) > 0)
foreach ($varMatches[1] as $varMatch)
{
$strKey = $varMatch;
$strValue = Settings::get($strKey);
$strText = preg_replace("/\{\{$strKey\}\}/i", $strValue, $strText);
}
return $strText;
}
}
?>

View File

@ -1,82 +0,0 @@
<?php
global $c;
$strPath = "/";
$strPath .= implode("/", Request::getPathParts());
$varPosts = [];
$strQuery = Request::getParam("q");
$strAuthor = Request::getParam("username");
$strId = Request::getParam("id");
$intHasQuery = $strQuery !== null && strlen($strQuery) > 0;
$intHasAuthor = $strAuthor !== null && strlen($strAuthor) > 0;
$intHasId = $strId !== null && strlen($strId) > 0;
if ($intHasQuery)
{
$strQuery = preg_replace("/[^A-Za-z0-9]/", "", $strQuery);
$varPosts = $c->query(
"SELECT *
from posts as p
where
content like concat('%', ?, '%')
order by
created desc",
$strQuery);
$i = 0;
for ($i = 0; $i < count($varPosts); $i++)
{
$varOld = $varPosts[$i];
$varOld["content"] = preg_replace("/({$strQuery})/i", "<mark>$1</mark>", $varOld["content"]);
$varPosts[$i] = $varOld;
}
}
if ($intHasAuthor)
{
$varPosts = $c->query(
"SELECT *
from posts as p
where
username like ?
order by
created desc",
$strAuthor);
}
if ($intHasId)
{
$varPosts = $c->query(
"SELECT *
from posts as p
where
id = ?
order by
created desc",
$strId);
}
?>
<?php if (!$intHasAuthor && !$intHasId): ?>
<form method="get">
<table>
<tbody>
<tr>
<td><label>Search</label></td>
<td><input type="text" name="q" value="<?= $strQuery; ?>" /></td>
<td>
<input type="submit" value="Go" />
</td>
</tr>
</tbody>
</table>
</form>
<?php endif; ?>
<?php if ($intHasQuery || $intHasAuthor || $intHasId): ?>
<?php PostRender::rows($varPosts); ?>
<?php endif; ?>

51
pages/search.php Normal file
View File

@ -0,0 +1,51 @@
<?php
global $c;
$strPath = "/";
$strPath .= implode("/", Request::getPathParts());
$varPosts = [];
$strQuery = Request::getParam("q");
$intHasQuery = $strQuery !== null && strlen($strQuery) > 0;
if ($intHasQuery)
{
$strQuery = preg_replace("/[^A-Za-z0-9]/", "", $strQuery);
$varPosts = $c->query(
"SELECT *
from posts as p
where
content like concat('%', ?, '%')
order by
created desc",
$strQuery);
$i = 0;
for ($i = 0; $i < count($varPosts); $i++)
{
$varOld = $varPosts[$i];
$varOld["content"] = preg_replace("/\b({$strQuery})\b/i", "<mark>$1</mark>", $varOld["content"]);
$varPosts[$i] = $varOld;
}
}
?>
<form method="get">
<table>
<tbody>
<tr>
<td><label>Search</label></td>
<td><input type="text" name="q" value="<?= $strQuery; ?>" /></td>
<td>
<input type="submit" value="Go" />
</td>
</tr>
</tbody>
</table>
</form>
<?php if ($intHasQuery): ?>
<?php PostRender::rows($varPosts); ?>
<?php endif; ?>