2024-04-11 10:36:40 +02:00
|
|
|
<?php
|
|
|
|
|
2024-04-25 22:50:35 +02:00
|
|
|
require_once "lib/upload.php";
|
2024-04-26 01:17:49 +02:00
|
|
|
require_once "lib/account.php";
|
2024-04-25 22:50:35 +02:00
|
|
|
|
2024-04-25 22:53:14 +02:00
|
|
|
function addMeme(string $title, string $memeText, int $imageID): array
|
2024-04-11 10:36:40 +02:00
|
|
|
{
|
|
|
|
global $mysqli;
|
2024-04-25 22:53:14 +02:00
|
|
|
$output = ["Status" => "Fail"];
|
2024-04-25 23:20:46 +02:00
|
|
|
if (isLoggedIn() && fileExists($imageID, false) && !empty($title) && !empty($memeText) && !empty($imageID) && $imageID > 0) {
|
2024-04-25 09:45:28 +02:00
|
|
|
$stmtMemeAdd = $mysqli->prepare('INSERT INTO Memes (AuthorID, Title, TextContent, FileID) VALUES (?, ?, ?, ?)');
|
2024-04-25 10:19:24 +02:00
|
|
|
$stmtMemeAdd->bind_param('issi', $_SESSION['ID'], htmlspecialchars($title), htmlspecialchars($memeText), $imageID);
|
2024-04-26 01:17:49 +02:00
|
|
|
if ($stmtMemeAdd->execute() && $stmtMemeAdd->affected_rows > 0) {
|
2024-04-25 22:55:00 +02:00
|
|
|
$output["Status"] = "Success";
|
2024-04-25 23:16:13 +02:00
|
|
|
$output["Meme"] = "Funny";
|
2024-04-25 22:55:00 +02:00
|
|
|
}
|
2024-04-11 10:36:40 +02:00
|
|
|
}
|
2024-04-25 22:53:14 +02:00
|
|
|
return $output;
|
2024-04-11 10:36:40 +02:00
|
|
|
}
|
|
|
|
|
2024-04-27 11:48:58 +02:00
|
|
|
function renderMeme(int $id, int $authorId, string $title, string $textContent, string $createdAt, string $filePath, int $imageWidth, int $imageHeight, string $userNickname, string $meme_template): string
|
2024-04-25 09:40:21 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
$meme_out = str_replace('__TEMPLATE_MEME_TITLE__', htmlspecialchars($title), $meme_template);
|
|
|
|
$meme_out = str_replace('__TEMPLATE_MEME_AUTHOR__', htmlspecialchars($userNickname), $meme_out);
|
|
|
|
$meme_out = str_replace('__TEMPLATE_MEME_DATE__', htmlspecialchars($createdAt), $meme_out);
|
2024-04-25 23:42:39 +02:00
|
|
|
$meme_out = str_replace('__TEMPLATE_MEME_IMAGE__', '/' . htmlspecialchars($filePath), $meme_out);
|
2024-04-27 11:51:01 +02:00
|
|
|
$meme_out = str_replace('__TEMPLATE_MEME_IMAGE_WIDTH__', strval($imageWidth), $meme_out);
|
|
|
|
$meme_out = str_replace('__TEMPLATE_MEME_IMAGE_HEIGHT__', strval($imageHeight), $meme_out);
|
2024-04-26 09:46:10 +02:00
|
|
|
$meme_out = str_replace('__TEMPLATE_MEME_DELETE_BUTTON__', (isModerator() || $_SESSION['ID'] == $authorId) ? "<button onclick=\"deleteMeme($id);\"><i class='ri-delete-bin-line'></i></button>" : '', $meme_out);
|
2024-04-26 01:17:49 +02:00
|
|
|
|
2024-04-27 10:30:01 +02:00
|
|
|
$meme_votes = calculateNetVotes($id);
|
2024-04-27 11:23:42 +02:00
|
|
|
$meme_net_votes = $meme_votes['NetVotes'];
|
2024-04-27 10:30:01 +02:00
|
|
|
|
2024-04-27 11:23:42 +02:00
|
|
|
if ($meme_votes['UserVote'] > 0){
|
|
|
|
$meme_upvote_active = 'fill';
|
|
|
|
$meme_downvote_active = 'line';
|
|
|
|
$meme_vote_counter_class = 'positive';
|
|
|
|
}
|
|
|
|
elseif (($meme_votes['UserVote'] < 0)) {
|
|
|
|
$meme_upvote_active = 'line';
|
|
|
|
$meme_downvote_active = 'fill';
|
|
|
|
$meme_vote_counter_class = 'negative';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$meme_downvote_active = 'line';
|
|
|
|
$meme_upvote_active = 'line';
|
|
|
|
$meme_vote_counter_class = 'neutral';
|
|
|
|
}
|
2024-04-27 12:17:20 +02:00
|
|
|
$meme_upvote = isLoggedIn() ? "<button id='meme_votes_upvote_button_$id' class='meme_upvote' onclick=\"voteMeme($id, 1);\"> <i id='meme_votes_upvote_$id' class=\"ri-arrow-up-circle-$meme_upvote_active\"></i></button>" : '';
|
|
|
|
$meme_downvote = isLoggedIn() ? "<button id='meme_votes_downvote_button_$id' class='meme_downvote' onclick=\"voteMeme($id, 0);\"> <i id='meme_votes_downvote_$id' class=\"ri-arrow-down-circle-$meme_downvote_active\"></i></button>" : '';
|
2024-04-27 11:23:42 +02:00
|
|
|
|
2024-04-27 11:48:58 +02:00
|
|
|
$meme_out = str_replace('__TEMPLATE_MEME_VOTES_NUMBER__', strval($meme_net_votes), $meme_out);
|
2024-04-27 10:30:01 +02:00
|
|
|
$meme_out = str_replace('__TEMPLATE_MEME_UPVOTE__', $meme_upvote, $meme_out);
|
|
|
|
$meme_out = str_replace('__TEMPLATE_MEME_DOWNVOTE__', $meme_downvote, $meme_out);
|
2024-04-27 11:48:58 +02:00
|
|
|
$meme_out = str_replace('__TEMPLATE_MEME_ID__', strval($id), $meme_out);
|
2024-04-27 11:23:42 +02:00
|
|
|
$meme_out = str_replace('__TEMPLATE_MEME_VOTE_COUNTER_CLASS__', $meme_vote_counter_class, $meme_out);
|
2024-04-25 09:40:21 +02:00
|
|
|
|
|
|
|
return str_replace('__TEMPLATE_MEME_TEXT__', htmlspecialchars($textContent), $meme_out);
|
|
|
|
}
|
|
|
|
|
2024-04-25 09:04:10 +02:00
|
|
|
function renderMemeGallery(): string
|
2024-04-11 10:36:40 +02:00
|
|
|
{
|
2024-04-25 09:04:10 +02:00
|
|
|
global $mysqli;
|
2024-04-25 09:40:21 +02:00
|
|
|
global $routerConfig;
|
2024-04-27 11:48:58 +02:00
|
|
|
$stmtlist = $mysqli->prepare('SELECT Memes.ID, Memes.Title, Memes.TextContent, Memes.CreatedAt, Memes.AuthorID, Files.Path, Files.Width, Files.Height, Files.Type, Users.Nickname FROM Memes INNER JOIN Users ON Memes.AuthorID = Users.ID INNER JOIN Files ON Memes.FileID = Files.ID');
|
2024-04-25 09:04:10 +02:00
|
|
|
|
|
|
|
// Execute the prepared statement
|
|
|
|
$memeID = 0;
|
2024-04-26 09:46:10 +02:00
|
|
|
$authorID = 0;
|
2024-04-25 09:04:10 +02:00
|
|
|
$title = "";
|
|
|
|
$textContent = "";
|
|
|
|
$filePath = "";
|
|
|
|
$fileType = "";
|
|
|
|
$userNickname = "";
|
|
|
|
$createdAt = "";
|
2024-04-27 11:48:58 +02:00
|
|
|
$imageWidth = 0;
|
|
|
|
$imageHeight = 0;
|
2024-04-25 09:04:10 +02:00
|
|
|
// Bind the result variables
|
2024-04-27 11:48:58 +02:00
|
|
|
$stmtlist->bind_result($memeID, $title, $textContent, $createdAt, $authorID, $filePath, $imageWidth, $imageHeight, $fileType, $userNickname);
|
2024-04-25 23:40:27 +02:00
|
|
|
$stmtlist->execute();
|
2024-04-25 09:04:10 +02:00
|
|
|
|
2024-04-25 09:45:28 +02:00
|
|
|
$meme_gallery_template = file_get_contents($routerConfig['template_dir'] . 'meme_gallery.html');
|
2024-04-25 09:04:10 +02:00
|
|
|
|
2024-04-25 09:40:21 +02:00
|
|
|
// Fetch the results
|
|
|
|
$memes_out = '';
|
2024-04-26 20:24:09 +02:00
|
|
|
$meme_template = file_get_contents($routerConfig['template_dir'] . "meme.html");
|
2024-04-27 10:32:07 +02:00
|
|
|
$stmtlist->store_result();
|
2024-04-25 09:40:21 +02:00
|
|
|
while ($stmtlist->fetch()) {
|
2024-04-25 23:42:07 +02:00
|
|
|
if (str_starts_with($fileType, 'image')) {
|
2024-04-27 11:48:58 +02:00
|
|
|
$memes_out .= renderMeme($memeID, $authorID, $title, $textContent, $createdAt, $filePath, $imageWidth, $imageHeight, $userNickname, $meme_template);
|
2024-04-25 09:40:21 +02:00
|
|
|
}
|
2024-04-25 09:04:10 +02:00
|
|
|
}
|
2024-04-26 20:24:09 +02:00
|
|
|
$meme_add = isLoggedIn() ? file_get_contents($routerConfig['template_dir'] . 'meme_add.html') : '';
|
2024-04-25 09:40:21 +02:00
|
|
|
$meme_gallery_out = str_replace('__TEMPLATE_MEMES_HERE__', $memes_out, $meme_gallery_template);
|
2024-04-26 20:24:09 +02:00
|
|
|
$meme_gallery_out = str_replace('__TEMPLATE_MEME_ADD__', $meme_add, $meme_gallery_out);
|
2024-04-25 09:04:10 +02:00
|
|
|
|
|
|
|
// Close the statement
|
|
|
|
$stmtlist->close();
|
2024-04-25 09:40:21 +02:00
|
|
|
return $meme_gallery_out;
|
2024-04-26 01:17:49 +02:00
|
|
|
}
|
|
|
|
|
2024-04-27 10:30:01 +02:00
|
|
|
function deleteMeme(int $memeID): array
|
2024-04-26 01:17:49 +02:00
|
|
|
{
|
|
|
|
global $mysqli;
|
|
|
|
$out = ["Status" => "Fail"];
|
|
|
|
if (isLoggedIn()) {
|
2024-04-26 10:52:11 +02:00
|
|
|
$query = !isModerator() ? 'DELETE FROM Memes WHERE ID = ? AND AuthorID = ?' : 'DELETE FROM Memes WHERE ID = ?';
|
2024-04-26 01:17:49 +02:00
|
|
|
$stmtDelete = $mysqli->prepare($query);
|
2024-04-26 10:52:11 +02:00
|
|
|
if (!isModerator()) {
|
2024-04-27 10:30:01 +02:00
|
|
|
$stmtDelete->bind_param('ii', $memeID, $_SESSION['ID']);
|
2024-04-26 01:17:49 +02:00
|
|
|
} else {
|
2024-04-27 10:30:01 +02:00
|
|
|
$stmtDelete->bind_param('i', $memeID);
|
2024-04-26 01:17:49 +02:00
|
|
|
}
|
|
|
|
$stmtDelete->execute();
|
|
|
|
if ($stmtDelete->affected_rows > 0) {
|
|
|
|
$out['Status'] = 'Success';
|
|
|
|
}
|
2024-04-27 10:30:01 +02:00
|
|
|
$stmtDelete->close();
|
|
|
|
}
|
|
|
|
return $out;
|
|
|
|
}
|
|
|
|
|
2024-04-27 10:43:58 +02:00
|
|
|
function voteMeme(int $memeID, int $isUpvote): array
|
2024-04-27 10:30:01 +02:00
|
|
|
{
|
|
|
|
global $mysqli;
|
|
|
|
$out = ["Status" => "Fail"];
|
2024-04-27 10:43:58 +02:00
|
|
|
if ($isUpvote != 1) {
|
|
|
|
$isUpvote = 0;
|
|
|
|
}
|
2024-04-27 10:30:01 +02:00
|
|
|
$memeVoteConn = $mysqli->prepare('INSERT INTO MemeVotes (MemeID, UserID, isUpvote) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE isUpvote = VALUES(isUpvote)');
|
2024-04-27 10:43:58 +02:00
|
|
|
$memeVoteConn->bind_param('iii', $memeID, $_SESSION['ID'], $isUpvote);
|
2024-04-27 10:30:01 +02:00
|
|
|
$memeVoteConn->execute();
|
|
|
|
if ($memeVoteConn->affected_rows > 0) {
|
|
|
|
$out['Status'] = 'Success';
|
2024-04-26 01:17:49 +02:00
|
|
|
}
|
2024-04-27 10:30:01 +02:00
|
|
|
$memeVoteConn->close();
|
2024-04-26 01:17:49 +02:00
|
|
|
return $out;
|
2024-04-27 10:30:01 +02:00
|
|
|
}
|
|
|
|
|
2024-04-27 11:23:42 +02:00
|
|
|
function deleteVoteMeme(int $memeID): array
|
|
|
|
{
|
|
|
|
global $mysqli;
|
|
|
|
$out = ["Status" => "Fail"];
|
|
|
|
$memeVoteConn = $mysqli->prepare('DELETE FROM MemeVotes WHERE MemeID = ? AND UserID = ?');
|
|
|
|
$memeVoteConn->bind_param('ii', $memeID, $_SESSION['ID']);
|
|
|
|
$memeVoteConn->execute();
|
|
|
|
if ($memeVoteConn->affected_rows > 0) {
|
|
|
|
$out['Status'] = 'Success';
|
|
|
|
}
|
|
|
|
$memeVoteConn->close();
|
|
|
|
return $out;
|
|
|
|
}
|
|
|
|
|
|
|
|
function calculateNetVotes(int $memeID): array
|
2024-04-27 10:30:01 +02:00
|
|
|
{
|
|
|
|
global $mysqli;
|
|
|
|
|
2024-04-27 11:23:42 +02:00
|
|
|
$query = 'SELECT isUpvote, UserID FROM MemeVotes WHERE MemeID = ?';
|
2024-04-27 10:30:01 +02:00
|
|
|
$stmt = $mysqli->prepare($query);
|
|
|
|
$stmt->bind_param('i', $memeID);
|
|
|
|
$stmt->execute();
|
|
|
|
$result = $stmt->get_result();
|
|
|
|
|
|
|
|
$netVotes = 0;
|
2024-04-27 11:23:42 +02:00
|
|
|
$userVote = 0;
|
2024-04-27 10:30:01 +02:00
|
|
|
while ($row = $result->fetch_assoc()) {
|
2024-04-27 11:23:42 +02:00
|
|
|
$vote = ($row['isUpvote'] == 1) ? 1 : -1;
|
|
|
|
$netVotes += $vote;
|
|
|
|
if ($row['UserID'] == $_SESSION['ID']) {
|
|
|
|
$userVote = $vote;
|
|
|
|
}
|
2024-04-27 10:30:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$stmt->close();
|
|
|
|
|
2024-04-27 11:23:42 +02:00
|
|
|
return [
|
|
|
|
"NetVotes" => $netVotes,
|
|
|
|
"UserVote" => $userVote
|
|
|
|
];
|
2024-04-27 10:30:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function getMemeVotes(int $memeID): array
|
|
|
|
{
|
2024-04-27 11:23:42 +02:00
|
|
|
$voteData = calculateNetVotes($memeID);
|
2024-04-27 10:30:01 +02:00
|
|
|
return [
|
|
|
|
"Status" => "Success",
|
2024-04-27 11:23:42 +02:00
|
|
|
"NetVotes" => $voteData['NetVotes'],
|
|
|
|
"UserVote" => $voteData['UserVote']
|
2024-04-27 10:30:01 +02:00
|
|
|
];
|
2024-04-11 10:36:40 +02:00
|
|
|
}
|