Database improvements and make most of the site work to match

This commit is contained in:
Floorb 2021-07-13 13:32:28 -04:00
parent 2f60d48cd8
commit e163c40add
7 changed files with 254 additions and 253 deletions

View file

@ -1,9 +1,7 @@
<?php
session_start();
if (isset($_SESSION['login'])) {
// Do nothing
} else {
if (!isset($_SESSION['login'])) {
header("Location: .");
exit();
}
@ -18,6 +16,8 @@ if (isset($_GET['logout'])) {
}
require_once('../config.php');
// DB table to use
$table = 'pastes';

View file

@ -188,8 +188,9 @@ class SSP {
// Main query to actually get the data
$data = self::Ssql_exec($db, $bindings,
"SELECT SQL_CALC_FOUND_ROWS `" . implode("`, `", self::pluck($columns, 'db')) . "`
"SELECT SQL_CALC_FOUND_ROWS pastes.id AS id, users.name AS member
FROM `$table`
INNER JOIN users ON users.id = pastes.user_id
$where
$order
$limit"

View file

@ -214,8 +214,9 @@ class SSP {
// Main query to actually get the data
$data = self::Ssql_exec($db, $bindings,
"SELECT SQL_CALC_FOUND_ROWS `" . implode("`, `", self::pluck($columns, 'db')) . "`
FROM `$table` WHERE visible='0' AND tagsys IS NOT NULL AND NOT title LIKE ''
"SELECT SQL_CALC_FOUND_ROWS pastes.id AS id, users.username AS member, tagsys, title, visible
FROM `$table`
INNER JOIN users ON users.id = pastes.user_id WHERE visible='0' AND tagsys IS NOT NULL AND NOT title LIKE ''
$order
$limit"
);

View file

@ -19,9 +19,26 @@ require_once('includes/functions.php');
// UTF-8
header('Content-Type: text/html; charset=utf-8');
function transformPasteRow(array $row) : array {
return [
'id' => $row['id'],
'title' => $row['title'],
'member' => $row['member'],
'time' => $row['created_at'],
'friendly_time' => friendlyDateDifference(new DateTime($row['created_at']), new DateTime()),
'tags' => $row['tagsys']
];
}
$popular_pastes = array_map('transformPasteRow', getpopular($conn, 10));
$monthly_popular_pastes = array_map('transformPasteRow', monthpop($conn, 10));
$recent_pastes = array_map('transformPasteRow', getRecent($conn, 10));
$updated_pastes = array_map('transformPasteRow', recentupdate($conn, 10));
$random_pastes = array_map('transformPasteRow', getrandom($conn, 10));
// Theme
$p_title = $lang['archive']; // "Pastes Archive";
require_once('theme/' . $default_theme . '/header.php');
require_once('theme/' . $default_theme . '/discover.php');
require_once('theme/' . $default_theme . '/footer.php');
?>

View file

@ -6,7 +6,27 @@ if (!defined('IN_PONEPASTE')) {
require_once('config.php');
require_once('includes/functions.php');
/* View functions */
function urlForPaste($paste_id) : string {
global $mod_rewrite;
if ($mod_rewrite === '1') {
return "/${paste_id}";
}
return "/paste.php?id=${paste_id}";
}
function urlForMember(string $member_name) : string {
global $mod_rewrite;
if ($mod_rewrite === '1') {
return '/user/' . urlencode($member_name);
}
return '/user.php?name=' . urlencode($member_name);
}
/* Database functions */
function getSiteInfo() : array {
return require('config/site.php');
}

View file

@ -147,17 +147,23 @@ LIMIT 0 , 5");
}
function recentupdate($conn, $count) {
$query = $conn->prepare("SELECT id, visible, title, date, timeedit, now_time, member, tagsys FROM pastes WHERE visible='0' ORDER BY timeedit DESC
LIMIT 10 , ?");
$query = $conn->prepare(
"SELECT pastes.id AS id, visible, title, created_at, users.username AS member, tagsys
FROM pastes
INNER JOIN users ON users.id = pastes.user_id
WHERE visible = '0' ORDER BY timeedit DESC
LIMIT ?");
$query->execute([$count]);
return $query->fetchAll();
}
//Cannot get this to work.
function monthpop($conn, $count) {
$p_month = date('F');
$query = $conn->prepare("SELECT s_date, views, title, id, now_time, visible, tagsys, member FROM pastes WHERE s_date LIKE ? AND visible = '0' ORDER BY views + 0 DESC LIMIT 10, ?");
$query->execute([$p_month, $count]);
$query = $conn->prepare(
"SELECT pastes.id AS id, views, title, created_at, visible, tagsys, users.username AS member
FROM pastes
INNER JOIN users ON users.id = pastes.user_id
WHERE MONTH(created_at) = MONTH(NOW()) AND visible = '0' ORDER BY views DESC LIMIT ?");
$query->execute([$count]);
return $query->fetchAll();
}
@ -202,10 +208,13 @@ function deleteMyPaste($conn, $paste_id) {
function getRecent($conn, $count) {
$query = $conn->prepare("SELECT id, visible, title, date, now_time, member, tagsys
FROM pastes where visible='0'
ORDER BY id DESC
LIMIT ?");
$query = $conn->prepare("
SELECT pastes.id, visible, title, created_at, users.username AS member, tagsys
FROM pastes
INNER JOIN users ON pastes.user_id = users.id
WHERE visible = '0'
ORDER BY created_at DESC
LIMIT ?");
$query->execute([$count]);
return $query->fetchAll();
}
@ -218,19 +227,25 @@ function getRecentadmin($conn, $count = 5) {
}
function getpopular($conn, $count) {
$query = $conn->prepare("SELECT id, visible, title, date, now_time, views, member, tagsys
FROM pastes WHERE visible='0'
ORDER BY views + 0 DESC
LIMIT 0, ?");
$query = $conn->prepare("
SELECT pastes.id AS id, visible, title, pastes.created_at AS created_at, views, users.username AS member, tagsys
FROM pastes INNER JOIN users ON users.id = pastes.user_id
WHERE visible = '0'
ORDER BY views DESC
LIMIT ?
");
$query->execute([$count]);
return $query->fetchAll();
}
function getrandom($conn, $count) {
$query = $conn->prepare("SELECT id, visible, title, date, now_time, views, member, tagsys
FROM pastes where visible='0'
ORDER BY RAND()
LIMIT ?");
$query = $conn->prepare("
SELECT pastes.id, visible, title, created_at, views, users.username AS member, tagsys
FROM pastes
INNER JOIN users ON users.id = pastes.user_id
WHERE visible = '0'
ORDER BY RAND()
LIMIT ?");
$query->execute([$count]);
return $query->fetchAll();
}
@ -289,6 +304,30 @@ function updateMyView(PDO $conn, $paste_id) {
$query->execute([$paste_id]);
}
function friendlyDateDifference(DateTime $lesser, DateTime $greater) : string {
$delta = $greater->diff($lesser, true);
$parts = [
'year' => $delta->y,
'month' => $delta->m,
'day' => $delta->d,
'hour' => $delta->h,
'min' => $delta->i,
'sec' => $delta->s
];
$friendly = '';
foreach ($parts as $part => $value) {
if ($value !== 0) {
$pluralizer = ($value === 1 ? '' : 's');
$friendly .= "${value} ${part}${pluralizer} ";
}
}
return trim($friendly) . ' ago';
}
function conTime($secs) {
// round up to 1 seconnd
if ($secs == 0) {

View file

@ -37,102 +37,72 @@
<!-- Pop Pastes -->
<div class="tab-content" id="first-tab">
<div class="panel panel-default">
<h1 class="title is-4"><?php echo $lang['popular']; ?>
<h1>
<h1 class="title is-4"><?php echo $lang['popular']; ?></h1>
<div class="columns is-multiline">
<?php
$res = getpopular($conn, 10);
foreach ($res as $index => $row) {
$title = Trim($row['title']);
$p_member = Trim($row['member']);
$titlehov = ($row['title']);
$p_id = Trim($row['id']);
$p_date = Trim($row['date']);
$p_time = Trim($row['now_time']);
$nowtime = time();
$oldtime = $p_time;
$p_time = conTime($nowtime - $oldtime);
$p_tagsys = Trim($row['tagsys']);
$tags = htmlentities($p_tagsys, ENT_QUOTES, 'UTF-8');
$tagui = sandwitch($tags);
?>
<?php
if ($mod_rewrite == '1') {
echo '
<?php foreach ($popular_pastes as $paste): ?>
<div class="column is-half">
<div class="card">
<div class="card-content">
<div class="media">
<div class="media-content" style="overflow: hidden">
<p class="title is-5"><a href="' . $p_id . '" title="' . $titlehov . '">' . $title . ' </a></p>
<p class="subtitle is-6"><a href="/user/' . $p_member . '">' . $p_member . '</a><br><time datetime="' . $p_date . '">' . $p_date . '</time></p>';
if (strlen($p_tagsys) > 0) {
echo $tagui;
<p class="title is-5">
<a href="<?= urlForPaste($paste['id']) ?>" title="<?= $paste['title'] ?>"> <?= $paste['title'] ?> </a>
</p>
<p class="subtitle is-6">
<a href="<?= urlForMember($paste['member']) ?>"><?= $paste['member'] ?></a>
<br>
<time datetime="<?= $paste['time'] ?>"><?= $paste['friendly_time'] ?></time>
</p>
<?php
if (!empty($paste['tags'])) {
echo sandwitch($paste['tags']);
} else {
echo ' <span class="tag is-warning">No tags</span>';
}
echo '</div>
?>
</div>
</div>
</div>
</div><br>
</div> ';
} else {
echo '<a href="' . $p_id . '" title="' . $titlehov . '">' . ucfirst($title) . '</a>';
}
}
?>
</div>
<?php endforeach; ?>
</div>
</div>
</div>
<!-- mPop Pastes -->
<div class="tab-content" id="second-tab">
<div class="panel panel-default">
<h1 class="title is-4"><?php echo $lang['monthpopular']; ?>
<h1>
<h1 class="title is-4"><?php echo $lang['monthpopular']; ?></h1>
<div class="columns is-multiline">
<?php
$res = monthpop($conn, 10);
foreach ($res as $index => $row) {
$title = Trim($row['title']);
$p_member = Trim($row['member']);
$titlehov = ($row['title']);
$p_id = Trim($row['id']);
$p_date = Trim($row['date']);
$p_time = Trim($row['now_time']);
$nowtime = time();
$oldtime = $p_time;
$p_time = conTime($nowtime - $oldtime);
$p_tagsys = Trim($row['tagsys']);
$tags = htmlentities($p_tagsys, ENT_QUOTES, 'UTF-8');
$tagui = sandwitch($tags);
?>
<?php
if ($mod_rewrite == '1') {
echo '
<?php foreach ($monthly_popular_pastes as $paste): ?>
<div class="column is-half">
<div class="card">
<div class="card-content">
<div class="media">
<div class="media-content" style="overflow: hidden">
<p class="title is-5"><a href="' . $p_id . '" title="' . $titlehov . '">' . $title . ' </a></p>
<p class="subtitle is-6"><a href="/user/' . $p_member . '">' . $p_member . '</a><br><time datetime="' . $p_date . '">' . $p_date . '</time></p>';
if (strlen($p_tagsys) > 0) {
echo $tagui;
<p class="title is-5">
<a href="<?= urlForPaste($paste['id']) ?>" title="<?= $paste['title'] ?>"> <?= $paste['title'] ?> </a>
</p>
<p class="subtitle is-6">
<a href="<?= urlForMember($paste['member']) ?>"><?= $paste['member'] ?></a>
<br>
<time datetime="<?= $paste['time'] ?>"><?= $paste['friendly_time'] ?></time>
</p>
<?php
if (!empty($paste['tags'])) {
echo sandwitch($paste['tags']);
} else {
echo ' <span class="tag is-warning">No tags</span>';
}
echo '</div>
?>
</div>
</div>
</div>
</div><br>
</div> ';
} else {
echo '<a href="' . $p_id . '" title="' . $titlehov . '">' . ucfirst($title) . '</a>';
}
}
?>
</div>
<?php endforeach; ?>
</div>
</div>
</div>
@ -140,51 +110,36 @@
<!-- New Pastes -->
<div class="tab-content" id="third-tab">
<div class="panel panel-default">
<h1 class="title is-4"><?php echo $lang['recentpastes']; ?>
<h1>
<h1 class="title is-4"><?php echo $lang['recentpastes']; ?></h1>
<div class="columns is-multiline">
<?php
$res = getRecent($conn, 10);
foreach ($res as $index => $row) {
$title = Trim($row['title']);
$p_member = Trim($row['member']);
$titlehov = ($row['title']);
$p_id = Trim($row['id']);
$p_date = Trim($row['date']);
$p_time = Trim($row['now_time']);
$nowtime = time();
$oldtime = $p_time;
$p_time = conTime($nowtime - $oldtime);
$p_tagsys = Trim($row['tagsys']);
$tags = htmlentities($p_tagsys, ENT_QUOTES, 'UTF-8');
$tagui = sandwitch($tags);
?>
<?php
if ($mod_rewrite == '1') {
echo '
<?php foreach ($recent_pastes as $paste): ?>
<div class="column is-half">
<div class="card">
<div class="card-content">
<div class="media">
<div class="media-content" style="overflow: hidden">
<p class="title is-5"><a href="' . $p_id . '" title="' . $titlehov . '">' . $title . ' </a></p>
<p class="subtitle is-6"><a href="/user/' . $p_member . '">' . $p_member . '</a><br><time datetime="' . $p_date . '">' . $p_date . '</time></p>';
if (strlen($p_tagsys) > 0) {
echo $tagui;
<p class="title is-5">
<a href="<?= urlForPaste($paste['id']) ?>" title="<?= $paste['title'] ?>"> <?= $paste['title'] ?> </a>
</p>
<p class="subtitle is-6">
<a href="<?= urlForMember($paste['member']) ?>"><?= $paste['member'] ?></a>
<br>
<time datetime="<?= $paste['time'] ?>"><?= $paste['friendly_time'] ?></time>
</p>
<?php
if (!empty($paste['tags'])) {
echo sandwitch($paste['tags']);
} else {
echo ' <span class="tag is-warning">No tags</span>';
}
echo '</div>
?>
</div>
</div>
</div>
</div><br>
</div> ';
} else {
echo '<a href="' . $p_id . '" title="' . $titlehov . '">' . ucfirst($title) . '</a>';
}
}
?>
</div>
<?php endforeach; ?>
</div>
</div>
</div>
@ -192,112 +147,80 @@
<!-- Updated Pastes -->
<div class="tab-content" id="forth-tab">
<div class="panel panel-default">
<h1 class="title is-4"><?php echo $lang['updatedgreen']; ?>
<h1>
<h1 class="title is-4"><?php echo $lang['updatedgreen']; ?></h1>
<div class="columns is-multiline">
<?php
$res = recentupdate($conn, 10);
foreach ($res as $index => $row) {
$title = Trim($row['title']);
$p_member = Trim($row['member']);
$titlehov = ($row['title']);
$p_id = Trim($row['id']);
$p_date = Trim($row['date']);
$p_time = Trim($row['now_time']);
$nowtime = time();
$oldtime = $p_time;
$p_time = conTime($nowtime - $oldtime);
$p_tagsys = Trim($row['tagsys']);
$tags = htmlentities($p_tagsys, ENT_QUOTES, 'UTF-8');
$tagui = sandwitch($tags);
?>
<?php
if ($mod_rewrite == '1') {
echo '
<?php foreach ($updated_pastes as $paste): ?>
<div class="column is-half">
<div class="card">
<div class="card-content">
<div class="media">
<div class="media-content" style="overflow: hidden">
<p class="title is-5"><a href="' . $p_id . '" title="' . $titlehov . '">' . $title . ' </a></p>
<p class="subtitle is-6"><a href="/user/' . $p_member . '">' . $p_member . '</a><br><time datetime="' . $p_date . '">' . $p_date . '</time></p>';
if (strlen($p_tagsys) > 0) {
echo $tagui;
<p class="title is-5">
<a href="<?= urlForPaste($paste['id']) ?>" title="<?= $paste['title'] ?>"> <?= $paste['title'] ?> </a>
</p>
<p class="subtitle is-6">
<a href="<?= urlForMember($paste['member']) ?>"><?= $paste['member'] ?></a>
<br>
<time datetime="<?= $paste['time'] ?>"><?= $paste['friendly_time'] ?></time>
</p>
<?php
if (!empty($paste['tags'])) {
echo sandwitch($paste['tags']);
} else {
echo ' <span class="tag is-warning">No tags</span>';
}
echo '</div>
?>
</div>
</div>
</div>
</div><br>
</div> ';
</div>
<?php endforeach; ?>
</div>
</div>
</div>
} else {
echo '<a href="' . $p_id . '" title="' . $titlehov . '">' . ucfirst($title) . '</a>';
}
}
?>
</div>
</div>
</div>
<!-- Updated Pastes -->
<!-- Random Pastes -->
<div class="tab-content" id="fifth-tab">
<div class="panel panel-default">
<h1 class="title is-4"><?php echo $lang['random']; ?>
<h1>
<h1 class="title is-4"><?php echo $lang['random']; ?></h1>
<div class="columns is-multiline">
<?php
$res = getrandom($conn, 10);
foreach ($res as $index => $row) {
$title = Trim($row['title']);
$p_member = Trim($row['member']);
$titlehov = ($row['title']);
$p_id = Trim($row['id']);
$p_date = Trim($row['date']);
$p_time = Trim($row['now_time']);
$nowtime = time();
$oldtime = $p_time;
$p_time = conTime($nowtime - $oldtime);
$p_tagsys = Trim($row['tagsys']);
$tags = htmlentities($p_tagsys, ENT_QUOTES, 'UTF-8');
$tagui = sandwitch($tags);
?>
<?php
if ($mod_rewrite == '1') {
echo '
<?php foreach ($random_pastes as $paste): ?>
<div class="column is-half">
<div class="card">
<div class="card-content">
<div class="media">
<div class="media-content" style="overflow: hidden">
<p class="title is-5"><a href="' . $p_id . '" title="' . $titlehov . '">' . $title . ' </a></p>
<p class="subtitle is-6"><a href="/user/' . $p_member . '">' . $p_member . '</a><br><time datetime="' . $p_date . '">' . $p_date . '</time></p>';
if (strlen($p_tagsys) > 0) {
echo $tagui;
<p class="title is-5">
<a href="<?= urlForPaste($paste['id']) ?>" title="<?= $paste['title'] ?>"> <?= $paste['title'] ?> </a>
</p>
<p class="subtitle is-6">
<a href="<?= urlForMember($paste['member']) ?>"><?= $paste['member'] ?></a>
<br>
<time datetime="<?= $paste['time'] ?>"><?= $paste['friendly_time'] ?></time>
</p>
<?php
if (!empty($paste['tags'])) {
echo sandwitch($paste['tags']);
} else {
echo ' <span class="tag is-warning">No tags</span>';
}
echo '</div>
?>
</div>
</div>
</div>
</div><br>
</div> ';
} else {
echo '<a href="' . $p_id . '" title="' . $titlehov . '">' . ucfirst($title) . '</a>';
}
}
?>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</main>
<script>
const tabSystem = {