Refactor pastes to use repository

This commit is contained in:
Pijus Kamandulis
2023-09-14 23:33:31 +03:00
parent 6617d03a17
commit d3970c3116
14 changed files with 495 additions and 337 deletions

View File

@@ -4,56 +4,60 @@
<?php
include_once "includes/config.php";
include_once "includes/user.php";
$conn = GetConnectionToDB();
include_once "repositories/paste-repository.php";
$pasteRepo = new PasteRepository();
if (GetUsersIDBySession($_COOKIE["pp_sid"], $_COOKIE["pp_skey"]) == -1) {
printf('<h2>You must be loged in to see your pastes!</h2>');
$conn = null;
echo '<h2>You must be logged in to see your pastes!</h2>';
echo '</div></div></div>';
die();
}
$stmt = $conn->prepare('SELECT * FROM pastes WHERE owner=:own');
$own = GetUsersIDBySession($_COOKIE["pp_sid"], $_COOKIE["pp_skey"]);
$stmt->bindParam(':own', $own);
$stmt->execute();
if ($stmt->rowCount() > 0) {
echo "<table id=\"tablepastes\" class=\"table table-striped\" style=\"width:100%\">";
printf('<thead><th data-dynatable-column="name" style="text-align: left;">Title</th>
<th style="text-align: left;">Added</th>
<th style="text-align: left;">Expires</th>
<th style="text-align: left;">ID</th>
<th style="text-align: left;">Actions</th></thead>');
printf('<tbody>');
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$title = $row['title'];
//Paste title
printf('<tr><td style="text-align: left;">' . htmlspecialchars($row["title"], ENT_QUOTES, 'UTF-8') . '</td>');
//Creation date
printf('<td style="text-align: left;">' . date('Y-m-d', $row["created"]) . '</td>');
//Expire date
if ($row["expire"] == 0) printf('<td style="text-align: left;">Never</td>');
else {
$expire = ($row["expire"] - time()) / 3600;
if ($expire > 24) {
printf('<td style="text-align: left;">' . round($expire / 24) . ' days from now</td>');
} else if ($expire >= 1)
printf('<td style="text-align: left;">' . round($expire) . ' hours from now</td>');
else printf('<td style="text-align: left;">' . round($expire * 60) . ' minutes from now</td>');
$pastes = $pasteRepo->getPastesByOwner($own, true);
if (!empty($pastes)) {
echo '<table id="tablepastes" class="table table-striped" style="width:100%">';
echo '<thead><th data-dynatable-column="name" style="text-align: left;">Title</th>
<th style="text-align: left;">Added</th>
<th style="text-align: left;">Expires</th>
<th style="text-align: left;">ID</th>
<th style="text-align: left;">Actions</th></thead>';
echo '<tbody>';
foreach ($pastes as $row) {
$title = htmlspecialchars($row['title'], ENT_QUOTES, 'UTF-8');
$created = date('Y-m-d', $row["created"]);
if ($row["expire"] == 0) {
$expire = 'Never';
} else {
$expireInSeconds = $row["expire"] - time();
if ($expireInSeconds > 24 * 3600) {
$expire = round($expireInSeconds / (24 * 3600)) . ' days from now';
} elseif ($expireInSeconds >= 3600) {
$expire = round($expireInSeconds / 3600) . ' hours from now';
} else {
$expire = round($expireInSeconds / 60) . ' minutes from now';
}
}
//Paste url
printf('<td style="text-align: right;"><a href="' . htmlspecialchars($row["uid"], ENT_QUOTES, 'UTF-8') . '">' . htmlspecialchars($row["uid"], ENT_QUOTES, 'UTF-8') . '</a></td>');
//Actions
printf('<td style="text-align: right;">');
//delete paste
printf('<a href="delete/' . htmlspecialchars($row["uid"], ENT_QUOTES, 'UTF-8') . '"><span class="glyphicon glyphicon-trash" title="Delete paste" aria-hidden="true"></span>');
//edit paste
printf('<a href="edit/' . htmlspecialchars($row["uid"], ENT_QUOTES, 'UTF-8') . '"><span class="glyphicon glyphicon-edit" title="Edit paste" aria-hidden="true"></span>');
printf('</td></tr>');
echo '<tr>';
echo '<td style="text-align: left;">' . $title . '</td>';
echo '<td style="text-align: left;">' . $created . '</td>';
echo '<td style="text-align: left;">' . $expire . '</td>';
echo '<td style="text-align: right;"><a href="' . htmlspecialchars($row["uid"], ENT_QUOTES, 'UTF-8') . '">' . htmlspecialchars($row["uid"], ENT_QUOTES, 'UTF-8') . '</a></td>';
echo '<td style="text-align: right;">';
echo '<a href="delete/' . htmlspecialchars($row["uid"], ENT_QUOTES, 'UTF-8') . '"><span class="glyphicon glyphicon-trash" title="Delete paste" aria-hidden="true"></span></a>';
echo '<a href="edit/' . htmlspecialchars($row["uid"], ENT_QUOTES, 'UTF-8') . '"><span class="glyphicon glyphicon-edit" title="Edit paste" aria-hidden="true"></span></a>';
echo '</td></tr>';
}
printf('</tbody></talbe>');
echo '</tbody></table>';
} else {
printf('<h2>You havent made any pastes yet!</h2>');
echo '<h2>You haven\'t made any pastes yet!</h2>';
}
$conn = null;
?>
</div>
</div>