"Fail"]; if (isLoggedIn() && fileExists($imageID, false) && !empty($title) && !empty($memeText) && !empty($imageID) && $imageID > 0) { $stmtMemeAdd = $mysqli->prepare('INSERT INTO Memes (AuthorID, Title, TextContent, FileID) VALUES (?, ?, ?, ?)'); $stmtMemeAdd->bind_param('issi', $_SESSION['ID'], htmlspecialchars($title), htmlspecialchars($memeText), $imageID); if ($stmtMemeAdd->execute() && $stmtMemeAdd->affected_rows > 0) { $output["Status"] = "Success"; $output["Meme"] = "Funny"; } } return $output; } /** * Renders a meme into HTML based on provided data and a template. * * @param int $id The ID of the meme. * @param int $authorId The author's user ID. * @param string $title The title of the meme. * @param string $textContent The text content of the meme. * @param string $createdAt The creation timestamp of the meme. * @param string $filePath The file path of the associated image. * @param int $imageWidth The width of the image. * @param int $imageHeight The height of the image. * @param string $userNickname The nickname of the meme's author. * @param string $meme_template The HTML template for a meme. (used to not read the template over and over when rendering more memes) * @return string Returns the rendered HTML of the meme. */ function renderMeme(int $id, int $authorId, string $title, string $textContent, string $createdAt, string $filePath, int $imageWidth, int $imageHeight, string $userNickname, string $meme_template): string { $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); $meme_out = str_replace('__TEMPLATE_MEME_IMAGE__', '/' . htmlspecialchars($filePath), $meme_out); $meme_out = str_replace('__TEMPLATE_MEME_IMAGE_WIDTH__', strval($imageWidth), $meme_out); $meme_out = str_replace('__TEMPLATE_MEME_IMAGE_HEIGHT__', strval($imageHeight), $meme_out); $meme_out = str_replace('__TEMPLATE_MEME_DELETE_BUTTON__', (isModerator() || $_SESSION['ID'] == $authorId) ? "" : '', $meme_out); $meme_votes = calculateNetVotes($id); $meme_net_votes = $meme_votes['NetVotes']; if ($meme_votes['UserVote'] > 0){ $meme_upvote_active = 'fill'; $meme_downvote_active = 'line'; $meme_vote_counter_class = 'positive'; $meme_upvote_button_class = ' visual_hover'; $meme_downvote_button_class = ''; } elseif (($meme_votes['UserVote'] < 0)) { $meme_upvote_active = 'line'; $meme_downvote_active = 'fill'; $meme_vote_counter_class = 'negative'; $meme_upvote_button_class = ''; $meme_downvote_button_class = ' visual_hover'; } else { $meme_downvote_active = 'line'; $meme_upvote_active = 'line'; $meme_vote_counter_class = 'neutral'; $meme_upvote_button_class = ''; $meme_downvote_button_class = ''; } $meme_upvote = isLoggedIn() ? "" : ''; $meme_downvote = isLoggedIn() ? "" : ''; $meme_out = str_replace('__TEMPLATE_MEME_VOTES_NUMBER__', strval($meme_net_votes), $meme_out); $meme_out = str_replace('__TEMPLATE_MEME_UPVOTE__', $meme_upvote, $meme_out); $meme_out = str_replace('__TEMPLATE_MEME_DOWNVOTE__', $meme_downvote, $meme_out); $meme_out = str_replace('__TEMPLATE_MEME_ID__', strval($id), $meme_out); $meme_out = str_replace('__TEMPLATE_MEME_VOTE_COUNTER_CLASS__', $meme_vote_counter_class, $meme_out); return str_replace('__TEMPLATE_MEME_TEXT__', htmlspecialchars($textContent), $meme_out); } /** * Generates an HTML representation of a gallery of memes from the database. * Renders the memes using renderMeme. * * @global mysqli $mysqli The database connection object. * @global array $routerConfig Configuration settings including paths to templates. * @return string Returns the complete HTML content of the meme gallery. */ function renderMemeGallery(): string { global $mysqli; global $routerConfig; $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'); // Execute the prepared statement $memeID = 0; $authorID = 0; $title = ""; $textContent = ""; $filePath = ""; $fileType = ""; $userNickname = ""; $createdAt = ""; $imageWidth = 0; $imageHeight = 0; // Bind the result variables $stmtlist->bind_result($memeID, $title, $textContent, $createdAt, $authorID, $filePath, $imageWidth, $imageHeight, $fileType, $userNickname); $stmtlist->execute(); $meme_gallery_template = file_get_contents($routerConfig['template_dir'] . 'meme_gallery.html'); // Fetch the results $memes_out = ''; $meme_template = file_get_contents($routerConfig['template_dir'] . "meme.html"); $stmtlist->store_result(); while ($stmtlist->fetch()) { if (str_starts_with($fileType, 'image')) { $memes_out .= renderMeme($memeID, $authorID, $title, $textContent, $createdAt, $filePath, $imageWidth, $imageHeight, $userNickname, $meme_template); } } $meme_add = isLoggedIn() ? file_get_contents($routerConfig['template_dir'] . 'meme_add.html') : ''; $meme_gallery_out = str_replace('__TEMPLATE_MEMES_HERE__', $memes_out, $meme_gallery_template); $meme_gallery_out = str_replace('__TEMPLATE_MEME_ADD__', $meme_add, $meme_gallery_out); // Close the statement $stmtlist->close(); return $meme_gallery_out; } /** * Deletes a meme from the database if the current user has the right permissions. * * @param int $memeID The ID of the meme to delete. * @global mysqli $mysqli The database connection object. * @return array Returns an associative array with the status of the operation. */ function deleteMeme(int $memeID): array { global $mysqli; $out = ["Status" => "Fail"]; if (isLoggedIn()) { $query = !isModerator() ? 'DELETE FROM Memes WHERE ID = ? AND AuthorID = ?' : 'DELETE FROM Memes WHERE ID = ?'; $stmtDelete = $mysqli->prepare($query); if (!isModerator()) { $stmtDelete->bind_param('ii', $memeID, $_SESSION['ID']); } else { $stmtDelete->bind_param('i', $memeID); } $stmtDelete->execute(); if ($stmtDelete->affected_rows > 0) { $out['Status'] = 'Success'; } $stmtDelete->close(); } return $out; } /** * Records or updates a vote on a meme by the current user. * * @param int $memeID The ID of the meme to be voted on. * @param int $isUpvote Indicates whether the vote is an upvote (1) or downvote (0). * @global mysqli $mysqli The database connection object. * @return array Returns an associative array with the status of the vote operation. */ function voteMeme(int $memeID, int $isUpvote): array { global $mysqli; $out = ["Status" => "Fail"]; if ($isUpvote != 1) { $isUpvote = 0; } $memeVoteConn = $mysqli->prepare('INSERT INTO MemeVotes (MemeID, UserID, isUpvote) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE isUpvote = VALUES(isUpvote)'); $memeVoteConn->bind_param('iii', $memeID, $_SESSION['ID'], $isUpvote); $memeVoteConn->execute(); if ($memeVoteConn->affected_rows > 0) { $out['Status'] = 'Success'; } $memeVoteConn->close(); return $out; } /** * Deletes a vote previously made by the current user on a meme. * * @param int $memeID The ID of the meme whose vote is to be deleted. * @global mysqli $mysqli The database connection object. * @return array Returns an associative array with the status of the deletion. */ 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; } /** * Calculates the net votes for a meme and determines if the current user has voted on it. * The array has both the net votes and the user vote(0 when the user hasn't voted) * * @param int $memeID The ID of the meme for which votes are being calculated. * @global mysqli $mysqli The database connection object. * @return array Returns an array with net votes and the user's vote status. */ function calculateNetVotes(int $memeID): array { global $mysqli; // Adjusted query to calculate net votes and get the user's vote in one go $query = " SELECT SUM(CASE WHEN isUpvote = 1 THEN 1 ELSE -1 END) AS NetVotes, ( SELECT CASE WHEN isUpvote = 1 THEN 1 ELSE -1 END FROM MemeVotes WHERE MemeID = ? AND UserID = ? ) AS UserVote FROM MemeVotes WHERE MemeID = ?"; $stmt = $mysqli->prepare($query); $userID = $_SESSION['ID']; $stmt->bind_param('iii', $memeID, $userID, $memeID); $stmt->execute(); $result = $stmt->get_result(); $data = $result->fetch_assoc(); $netVotes = $data['NetVotes'] ?? 0; // Null coalescing operator in case no votes are found $userVote = $data['UserVote'] ?? 0; // Default to 0 if the user hasn't voted $stmt->close(); return [ "NetVotes" => $netVotes, "UserVote" => $userVote ]; } /** * Fetches the net votes and user's vote status for a specific meme. * Essentially just a wrapper of getMemeVotes for an API call * * @param int $memeID The ID of the meme to fetch votes for. * @return array Returns an array with the net votes and the user's vote status, along with operation status. */ function getMemeVotes(int $memeID): array { $voteData = calculateNetVotes($memeID); return [ "Status" => "Success", "NetVotes" => $voteData['NetVotes'], "UserVote" => $voteData['UserVote'] ]; }