new-bigly-chat/libs/comment.php
2024-12-12 00:08:19 -05:00

92 lines
2.5 KiB
PHP

<?php
require("../libs/markdown.php");
function comments($typer, $idr) {
$ref = 1732684297;
global $username;
global $db;
$page = array_key_exists('page',$_GET) ? ($_GET['page']) : 0;
if ($username) {
form("Broadcast your thoughts...", '', array(
array('key' => 'Your message', 'type' => 'textarea', 'name' => 'post', 'default' => ''),
array('key' => '','type' => 'hidden','name' => 'type', 'default' => $typer),
array('key' => '','type' => 'hidden','name' => 'id', 'default' => $idr),
array('key' => 'Attachments', 'type' => 'file', 'name' => 'file', 'default' => '')
),'/index.php');
}
$qs = parse_url($_SERVER['REQUEST_URI']);
if (is_null($qs)) $qs = array('query' => '?');
$qsq = $qs['query'];
$qss = array();
parse_str($qsq,$qss);
$qss['page'] = $page + 1;
$qs_next = http_build_query($qss);
$qss['page'] = $page - 1;
$qs_prev = http_build_query($qss);
?>
<div>
<?php if ($page > 0) { ?>
<a class="form-button" href="?<?php echo $qs_prev ?>">Previous</a>
<?php } ?>
<a class="form-button" href="?<?php echo $qs_next ?>">Next</a>
</div>
<?php
$posts = array();
$theId = $idr;
while (true) {
$stmt = $db->prepare("SELECT * FROM comment WHERE id = ? ORDER BY date DESC");
$stmt->execute([
$theId
]);
$posts1 = $stmt->fetchAll(PDO::FETCH_DEFAULT);
if (is_null($posts1) || count($posts1) < 1) break;
$theId = $posts1[0]['targetid'];
$posts = array_merge($posts1,$posts);
}
$stmt = $db->prepare("SELECT * FROM comment WHERE targetType = ? AND targetId = ? ORDER BY date DESC LIMIT ? OFFSET ?");
$stmt->execute([
$typer,
$idr,
10,
$page * 10
]);
$posts2 = $stmt->fetchAll(PDO::FETCH_DEFAULT);
$edge = count($posts);
$posts = array_merge($posts,$posts2);
foreach ($posts as $key => $post) {
if ($key == $edge) { ?>
<h2>Replies</h2>
<?php } ?>
<div class='comment'>
<div class='avatar'>
<img src='/pfp/<?php echo $post['username'] ?>.png' class='avatar-img'>
<div>
<div><b>
<a class='link' href='/user.php?id=<?php echo $post['username'] ?>'>
<?php echo $post['username'] ?>
</a>
</b></div>
<div><b>
<a class='link' href='/comment.php?id=<?php echo $post['id'] ?>'>
<?php echo date(DATE_RFC2822,$post['date'] + $ref) ?>
</a>
</b></div>
</div>
</div>
<pre><?php echo markdown(htmlspecialchars($post['content'])) ?></pre>
</div>
<?php }
}
?>