PlainSQLiteBlog/lib/PostRender.php

139 lines
4.9 KiB
PHP

<?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();
$intRenderedRows = 0;
$intList = Request::getParam("v") == "list";
$intJson = Request::getParam("v") == "json";
$intText = Request::getParam("v") == "md";
if ($intJson)
Respond::json(PostRender::processRows($varRows));
$strBuffer = "";
?>
<div class="post-container">
<?php foreach ($varRows as $r): ?>
<?php if (!UserAuth::visible($r["visibility"])) continue; ?>
<?php
$strText = $r["content"];
preg_match("/(^|\n).*?([A-Za-z0-9].*?(\!|\.|\,|\?|\n))/i", $strText, $varTitles);
$strTitle = $varTitles[2];
$strTitle = trim($strTitle);
$intUpdated = Util::diff($r["created"], $r["updated"]) >= 500;
$strDatePreposition = "on";
$strDateUsed = $r["created"];
if ($intUpdated)
{
$strDatePreposition = "updated";
$strDateUsed = $r["updated"];
}
$strDateUsed = "{$strDateUsed} UTC";
$intOwnership = UserAuth::has("is_admin") || $varUser["username"] == $r["username"];
$fncFooter = function()
{
?>
<?php
};
?>
<?php if ($intList): ?>
<div class="post-listing">
<div class="post-title">
<a href="/<?= $r["id"]; ?>"><?= $strTitle; ?></a>
&middot; by <?= $r["username"]; ?>
</div>
</div>
<?php
$intRenderedRows++;
continue;
?>
<?php endif; ?>
<?php if ($intRenderedRows > 0): ?>
<hr />
<?php
$strBuffer .= "\n\n---\n\n";
?>
<?php endif; ?>
<!-- <?= $strTitle; ?> -->
<div class="post">
<div class="post-body">
<?php PageRender::markdown($r["content"]); ?>
<?php
$strBuffer .= $r["content"];
?>
</div>
<div class="post-footer">
<div class="post-author">by <?= $r["username"]; ?></div>
<div class="post-date"><?= $strDatePreposition; ?> <?= $strDateUsed; ?></div>
<div class="post-links">
<a href="/<?= $r["id"]; ?>">Permalink</a> &middot;
<a href="<?= $r["location"]; ?>">Related</a>
<?php if ($intOwnership): ?>
&middot; <a href="/post/<?= $r["id"]; ?>">Edit</a>
<?php endif; ?>
</div>
<?php
$strBuffer .= "\n";
$strBuffer .= "\n* by {$r["username"]}";
$strBuffer .= "\n* {$strDatePreposition} {$strDateUsed}";
?>
</div>
</div>
<?php $intRenderedRows++; ?>
<?php endforeach; ?>
<?php if ($intRenderedRows < 1): ?>
<?php
$strMessage = "Sorry, there is nothing here to show.";
$strBuffer .= $strMessage;
?>
<div><?= $strMessage; ?></div>
<?php endif; ?>
</div>
<?php
if ($intText)
{
ob_clean();
header("Content-Type: text/plain");
$strBuffer = Settings::makeReplacements($strBuffer);
echo $strBuffer;
ob_end_flush();
exit;
}
}
}
?>