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

@@ -1,4 +1,6 @@
<?php
include_once "includes/config.php";
if (!isset($content)) {
$content = '<p>no content</p>';
}
@@ -12,19 +14,17 @@ if (!isset($content)) {
<meta charset="utf-8">
<?php
echo '<meta name="viewport" content="width=device-width, initial-scale=1">';
$dir = "";
if (isset($_GET["user"]) || isset($_GET["page"]) && $_GET["page"] == "edit") $dir = "../";
echo '<link rel="stylesheet" href="' . $dir . 'public/css/bootstrap.min.css">';
echo '<link rel="stylesheet" href="' . $BASE_DIR . 'public/css/bootstrap.min.css">';
echo '<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>';
echo '<script src="' . $dir . 'public/js/bootstrap.min.js"></script>';
echo '<script type="text/javascript" src="' . $dir . 'public/js/jquery.dynatable.js"></script>';
echo '<link href="' . $dir . 'public/css/jquery.dynatable.css" rel="stylesheet">';
echo '<script src="' . $BASE_DIR . 'public/js/bootstrap.min.js"></script>';
echo '<script type="text/javascript" src="' . $BASE_DIR . 'public/js/jquery.dynatable.js"></script>';
echo '<link href="' . $BASE_DIR . 'public/css/jquery.dynatable.css" rel="stylesheet">';
echo '<link href="' . $dir . 'public/css/chosen.css" rel="stylesheet">';
echo '<script src="' . $dir . 'public/js/chosen.jquery.js" type="text/javascript"></script>';
echo '<script src="' . $dir . 'public/js/chosen.proto.js" type="text/javascript"></script>';
echo '<link href="' . $BASE_DIR . 'public/css/chosen.css" rel="stylesheet">';
echo '<script src="' . $BASE_DIR . 'public/js/chosen.jquery.js" type="text/javascript"></script>';
echo '<script src="' . $BASE_DIR . 'public/js/chosen.proto.js" type="text/javascript"></script>';
echo "<script>$(document).ready(function(){
$('#tablepastes').dynatable();

View File

@@ -1,10 +1,10 @@
<?php include_once "includes/config.php"; ?>
<nav class="navbar navbar-inverse">
<div class="container">
<div class="navbar-header">
<?php
$dir = "";
if (isset($_GET["user"]) || isset($_GET["page"]) && $_GET["page"] == "edit") $dir = "../";
echo '<a class="navbar-brand" href="' . $dir . '.">Palm-Paste</a>';
echo '<a class="navbar-brand" href="' . $BASE_DIR . '.">Palm-Paste</a>';
?>
</div>
<ul class="nav navbar-nav">
@@ -15,13 +15,11 @@
<ul class="nav navbar-nav navbar-right">
<?php
include_once "includes/user.php";
$dir = "";
if (isset($_GET["user"]) || isset($_GET["page"]) && $_GET["page"] == "edit") $dir = "../";
$userID = -1;
if (isset($_COOKIE["pp_sid"]) && isset($_COOKIE["pp_skey"]))
$userID = GetUsersIDBySession($_COOKIE["pp_sid"], $_COOKIE["pp_skey"]);
if ($userID == -1) {
echo "<li><a href=\"" . $dir . "signup\"><span class=\"glyphicon glyphicon-user\"></span> Sign Up</a></li>";
echo "<li><a href=\"" . $BASE_DIR . "signup\"><span class=\"glyphicon glyphicon-user\"></span> Sign Up</a></li>";
echo "<li><a data-toggle=\"modal\" data-target=\"#LoginPopup\" href=\"#\"><span class=\"glyphicon glyphicon-log-in\"></span> Login</a></li>";
} else {
$user = GetUserByID($userID);
@@ -29,8 +27,8 @@
<li class="dropdown">
<a class="dropdown-toggle glyphicon glyphicon-user" data-toggle="dropdown" href="#"> ' . htmlspecialchars($user[1], ENT_QUOTES, 'UTF-8') . '<span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="' . $dir . 'mypastes">My pastes</a></li>
<li><a href="' . $dir . 'logout">Logout</a></li>
<li><a href="' . $BASE_DIR . 'mypastes">My pastes</a></li>
<li><a href="' . $BASE_DIR . 'logout">Logout</a></li>
</ul>
</li>
';
@@ -52,7 +50,7 @@
<div class="modal-body">
<!-- Login form -->
<?php
echo '<form role="form" method="POST" action="' . $dir . 'login">';
echo '<form role="form" method="POST" action="' . $BASE_DIR . 'login">';
?>
<div class="form-group">
<label for="user">Username:</label>

View File

@@ -12,7 +12,7 @@
<label for="title">Paste title:</label>
<?php
if ($edit_mode == true) {
printf('<input type="title" class="form-control" value="' . $row['title'] . '" id="title" name="title">');
printf('<input type="title" class="form-control" value="' . $pasteInfo['title'] . '" id="title" name="title">');
} else printf('<input type="title" class="form-control" id="title" name="title">');
?>
</div>
@@ -20,14 +20,14 @@
<label for="text">New paste:</label>
<?php
if ($edit_mode == true) {
echo '<textarea class="form-control" rows="5" id="text" name="text">' . $row['text'] . '</textarea>';
echo '<textarea class="form-control" rows="5" id="text" name="text">' . $pasteInfo['text'] . '</textarea>';
} else printf('<textarea class="form-control" rows="5" id="text" name="text"></textarea>');
?>
</div>
<?php
if ($edit_mode == true) {
printf("<input type='hidden' name='type' value='edit_paste'></input>");
printf("<input type='hidden' name='uid' value='" . $row['uid'] . "'></input>");
printf("<input type='hidden' name='uid' value='" . $pasteInfo['uid'] . "'></input>");
} else printf("<input type='hidden' name='type' value='paste'></input>");
?>
<div class="container-fluid">
@@ -58,7 +58,7 @@
<select data-placeholder="None" class="form-control chosen-select" id="syntax" name="syntax">
<?php
if ($edit_mode == true)
print '<option value="' . $row['highlight'] . '">Current (' . $row['highlight'] . ')</option>';
print '<option value="' . $pasteInfo['highlight'] . '">Current (' . $pasteInfo['highlight'] . ')</option>';
?>
<option value="plain">Plain</option>
<option value="applescript">AppleScript</option>
@@ -93,7 +93,7 @@
<select class="form-control" id="exposure" name="exposure">
<?php
print '<option value="0">Public</option>';
if ($edit_mode == true && $row['exposure'] == 1)
if ($edit_mode == true && $pasteInfo['exposure'] == 1)
print '<option selected="selected" value="1">Unlisted</option>';
else print '<option value="1">Unlisted</option>';
include_once "includes/user.php";
@@ -103,7 +103,7 @@
if ($userID == -1)
print '<option value="2" disabled>Private (Members only)</option>';
else {
if ($edit_mode === true && $row['exposure'] === 2)
if ($edit_mode === true && $pasteInfo['exposure'] === 2)
print '<option selected="selected" value="2" >Private</option>';
else print '<option value="2" >Private</option>';
}

View File

@@ -3,15 +3,15 @@
<h4>Newest pastes:</h4>
<div class="list-group">
<?php
include_once "includes/config.php";
$conn = GetConnectionToDB();
$stmt = $conn->query('SELECT * FROM pastes WHERE exposure=0 ORDER BY id DESC LIMIT 5');
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$title = htmlspecialchars($row['title'], ENT_QUOTES, 'UTF-8');
include_once "repositories/paste-repository.php";
$pasteRepo = new PasteRepository();
$pastes = $pasteRepo->getPastesWithExposure(0, 5);
foreach ($pastes as $paste) {
$title = htmlspecialchars($paste['title'], ENT_QUOTES, 'UTF-8');
if (strlen($title) > 25) $title = substr($title, 0, 25) . "...";
echo "<a href=\"" . htmlspecialchars($row['uid'], ENT_QUOTES, 'UTF-8') . "\" class=\"list-group-item\">" . $title . "</a>";
echo "<a href=\"" . htmlspecialchars($paste['uid'], ENT_QUOTES, 'UTF-8') . "\" class=\"list-group-item\">" . $title . "</a>";
}
$conn = null;
?>
</div>
</div>

View File

@@ -6,28 +6,25 @@
include_once "includes/user.php";
$uid = GetUsersIDBySession($_COOKIE["pp_sid"], $_COOKIE["pp_skey"]);
$paste = $_GET['id'];
//connect to db and get paste info
$conn = GetConnectionToDB();
$stmt = $conn->prepare('SELECT * FROM pastes WHERE uid=:uid');
$stmt->bindParam(':uid', $paste);
$stmt->execute();
if ($stmt->rowCount() > 0) {
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
if ($row['owner'] === $uid) {
include "views/_new-paste.php";
} else {
$conn = null;
echo '<center><h4>You are not the owner of the paste ' . $row["uid"] . '</h4></center>';
echo '<meta http-equiv="refresh" content="2;url=../index.php">';
die();
}
include_once "repositories/paste-repository.php";
$pasteRepo = new PasteRepository();
// Get paste info
$pasteInfo = $pasteRepo->readByUid($paste);
if ($pasteInfo) {
if ($pasteInfo['owner'] === $uid) {
include "views/_new-paste.php";
} else {
echo '<center><h4>You are not the owner of the paste ' . $pasteInfo["uid"] . '</h4></center>';
echo '<meta http-equiv="refresh" content="2;url=../index.php">';
die();
}
} else {
$conn = null;
echo '<center><h4>The paste ' . $row["uid"] . ' does not exist</h4></center>';
echo '<center><h4>The paste ' . $paste . ' does not exist</h4></center>';
echo '<meta http-equiv="refresh" content="2;url=../index.php">';
}
$conn = null;
}
?>
</div>

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>

View File

@@ -4,48 +4,57 @@
<?php
include_once "includes/config.php";
include_once "includes/user.php";
$conn = GetConnectionToDB();
$ownerID = GetUserIDByName($_GET["user"]);
include_once "repositories/paste-repository.php";
$pasteRepo = new PasteRepository();
$ownerName = $_GET["user"];
$ownerID = GetUserIDByName($ownerName);
if ($ownerID != -1) {
//== Print user info ==//
$owner = GetUserByID($ownerID);
printf('<h2>' . $owner["user"] . '\'s profile</h2>');
//== Print pastes ==//
$query = "SELECT * FROM pastes WHERE owner=:own AND exposure=0";
if (GetUsersIDBySession($_COOKIE["pp_sid"], $_COOKIE["pp_skey"]) == $ownerID) $query = "SELECT * FROM pastes WHERE owner=:own";
$stmt = $conn->prepare($query);
$stmt->bindParam(':own', $ownerID);
$stmt->execute();
if ($stmt->rowCount() > 0) {
$pastes = $pasteRepo->getPastesByOwner($ownerID, isset($_COOKIE["pp_sid"]) ? true : false);
if (!empty($pastes)) {
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></thead>');
<th style="text-align: left;">Added</th>
<th style="text-align: left;">Expires</th>
<th style="text-align: left;">ID</th></thead>');
printf('<tbody>');
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$title = $row['title'];
printf('<tr><td style="text-align: left;">' . htmlspecialchars($row["title"], ENT_QUOTES, 'UTF-8') . '</td>');
printf('<td style="text-align: left;">' . date('Y-m-d', $row["created"]) . '</td>');
if ($row["expire"] == 0) printf('<td style="text-align: left;">Never</td>');
else {
$expire = ($row["expire"] - time()) / 3600;
foreach ($pastes as $paste) {
$title = $paste['title'];
printf('<tr><td style="text-align: left;">' . htmlspecialchars($paste["title"], ENT_QUOTES, 'UTF-8') . '</td>');
printf('<td style="text-align: left;">' . date('Y-m-d', $paste["created"]) . '</td>');
if ($paste["expire"] == 0) {
printf('<td style="text-align: left;">Never</td>');
} else {
$expire = ($paste["expire"] - time()) / 3600;
if ($expire > 24) {
printf('<td style="text-align: left;">' . round($expire / 24) . ' days from now</td>');
} else if ($expire >= 1)
} 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>');
} else {
printf('<td style="text-align: left;">' . round($expire * 60) . ' minutes from now</td>');
}
}
printf('<td style="text-align: right;"><a href="../' . htmlspecialchars($row["uid"], ENT_QUOTES, 'UTF-8') . '">' . htmlspecialchars($row["uid"], ENT_QUOTES, 'UTF-8') . '</a></td></tr>');
printf('<td style="text-align: right;"><a href="../' . htmlspecialchars($paste["uid"], ENT_QUOTES, 'UTF-8') . '">' . htmlspecialchars($paste["uid"], ENT_QUOTES, 'UTF-8') . '</a></td></tr>');
}
printf('</tbody></talbe>');
printf('</tbody></table>');
} else {
printf('<h2>This user has no public pastes!</h2>');
}
} else printf('<h2>User does not exist!</h2>');
$conn = null;
} else {
printf('<h2>User does not exist!</h2>');
}
?>
</div>
</div>

View File

@@ -5,47 +5,63 @@
if (isset($uid)) {
include_once "includes/config.php";
include_once "includes/user.php";
$conn = GetConnectionToDB();
$stmt = $conn->query('SELECT * FROM pastes WHERE uid="' . $uid . '"');
if ($result = $stmt->fetch(PDO::FETCH_ASSOC)) {
$conn = null;
if ($result["expire"] != 0 && $result["expire"] < time()) {
//This paste is expired but not removed
include_once "repositories/paste-repository.php";
$pasteRepo = new PasteRepository();
$paste = $pasteRepo->readByUid($uid);
if ($paste) {
if ($paste["expire"] != 0 && $paste["expire"] < time()) {
// This paste is expired but not removed
echo "<h1>This paste just expired</h1>";
include_once "cronjob.php";
RemoveExpiredPastes();
$result = $pasteRepo->removeExpiredPastes();
if ($result === 'OK! 200') {
echo 'Expired pastes have been removed';
} else {
echo 'Error removing expired pastes';
}
die();
}
if ($result["exposure"] == 2 && $result["owner"] != 0 && isset($_COOKIE["pp_sid"]) && isset($_COOKIE["pp_skey"]) && $result["owner"] != GetUsersIDBySession($_COOKIE["pp_sid"], $_COOKIE["pp_skey"])) {
if ($paste["exposure"] == 2 && $paste["owner"] != 0 && isset($_COOKIE["pp_sid"]) && isset($_COOKIE["pp_skey"]) && $paste["owner"] != GetUsersIDBySession($_COOKIE["pp_sid"], $_COOKIE["pp_skey"])) {
echo "<h1>This paste is private</h1>";
die();
}
echo "<h1>" . htmlspecialchars($result["title"], ENT_QUOTES, 'UTF-8') . "</h1>";
//
$owner = GetUserByID($result["owner"]);
echo "<h1>" . htmlspecialchars($paste["title"], ENT_QUOTES, 'UTF-8') . "</h1>";
$owner = GetUserByID($paste["owner"]);
echo "<h5>";
if ($owner[1] == -1)
echo "Posted by: <b>Guest</b>";
else echo "Posted by: <b><a href=\"u/" . htmlspecialchars($owner[1]) . "\">" . htmlspecialchars($owner[1]) . "</a></b>";
echo ", at " . date('Y-m-d', $result["created"]) . ", it will expire <b>";
if ($result["expire"] == 0) printf('Never');
else {
$expire = ($result["expire"] - time()) / 3600;
else
echo "Posted by: <b><a href=\"u/" . htmlspecialchars($owner[1]) . "\">" . htmlspecialchars($owner[1]) . "</a></b>";
echo ", at " . date('Y-m-d', $paste["created"]) . ", it will expire <b>";
if ($paste["expire"] == 0) {
printf('Never');
} else {
$expire = ($paste["expire"] - time()) / 3600;
if ($expire > 24) {
printf(round($expire / 24) . ' days from now');
} else if ($expire >= 1)
} elseif ($expire >= 1) {
printf(round($expire) . ' hours from now');
else printf(round($expire * 60) . ' minutes from now');
} else {
printf(round($expire * 60) . ' minutes from now');
}
}
echo "</b></h5>";
//
echo "<pre class=\"brush: " . $_HL . "\">";
echo htmlspecialchars($result["text"], ENT_QUOTES, 'UTF-8') . "</pre><pb>";
echo htmlspecialchars($paste["text"], ENT_QUOTES, 'UTF-8') . "</pre><pb>";
echo "<label for=\"rawtext\">Raw text:</label>";
echo "<textarea id=\"rawtext\" class=\"form-control\" rows=\"10\">" . htmlspecialchars($result["text"], ENT_QUOTES, 'UTF-8') . "</textarea>";
} else echo "Paste does not exist";
echo "<textarea id=\"rawtext\" class=\"form-control\" rows=\"10\">" . htmlspecialchars($paste["text"], ENT_QUOTES, 'UTF-8') . "</textarea>";
} else {
echo "Paste does not exist";
}
$conn = null;
} else echo "Error: id not set";
} else {
echo "Error: id not set";
}
?>
<script type="text/javascript">
SyntaxHighlighter.all()