Update meme voting

This commit is contained in:
Bruno Rybársky 2024-04-27 11:48:58 +02:00
parent 287c2050e0
commit adb738c12f
24 changed files with 43 additions and 12 deletions

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
require_once "lib/account.php";

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
require_once "lib/meme.php";

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
require_once "lib/newsarticle.php";

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
require_once "lib/page.php";
require_once "lib/navigation.php";

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
require_once "lib/upload.php";

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/** @noinspection PhpIncludeInspection */
require_once 'secrets/config.php';
require_once 'lib/config.php';

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
use Random\RandomException;

@ -1,5 +1,6 @@
<?php
function loadRouterConfig(): array
declare(strict_types=1);
function loadRouterConfig(): array
{
return [

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
function doDynamicStyling() :string
{

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
function runEndpoint($endpoint_file): ?array
{

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
function inlineLocalStylesFromHref($inputString): string
{

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
require_once "lib/upload.php";
require_once "lib/account.php";
@ -18,13 +19,15 @@ function addMeme(string $title, string $memeText, int $imageID): array
return $output;
}
function renderMeme(int $id, int $authorId, string $title, string $textContent, string $createdAt, string $filePath, string $userNickname, string $meme_template): string
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) ? "<button onclick=\"deleteMeme($id);\"><i class='ri-delete-bin-line'></i></button>" : '', $meme_out);
$meme_votes = calculateNetVotes($id);
@ -48,10 +51,10 @@ function renderMeme(int $id, int $authorId, string $title, string $textContent,
$meme_upvote = isLoggedIn() ? "<button onclick=\"voteMeme($id, 1);\"> <i id='meme_votes_upvote_$id' class=\"ri-arrow-up-circle-$meme_upvote_active\"></i></button>" : '';
$meme_downvote = isLoggedIn() ? "<button onclick=\"voteMeme($id, 0);\"> <i id='meme_votes_downvote_$id' class=\"ri-arrow-down-circle-$meme_downvote_active\"></i></button>" : '';
$meme_out = str_replace('__TEMPLATE_MEME_VOTES_NUMBER__', $meme_net_votes, $meme_out);
$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__', $id, $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);
@ -61,7 +64,7 @@ function renderMemeGallery(): string
{
global $mysqli;
global $routerConfig;
$stmtlist = $mysqli->prepare('SELECT Memes.ID, Memes.Title, Memes.TextContent, Memes.CreatedAt, Memes.AuthorID, Files.Path, Files.Type, Users.Nickname FROM Memes INNER JOIN Users ON Memes.AuthorID = Users.ID INNER JOIN Files ON Memes.FileID = Files.ID');
$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;
@ -72,8 +75,10 @@ function renderMemeGallery(): string
$fileType = "";
$userNickname = "";
$createdAt = "";
$imageWidth = 0;
$imageHeight = 0;
// Bind the result variables
$stmtlist->bind_result($memeID, $title, $textContent, $createdAt, $authorID, $filePath, $fileType, $userNickname);
$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');
@ -84,7 +89,7 @@ function renderMemeGallery(): string
$stmtlist->store_result();
while ($stmtlist->fetch()) {
if (str_starts_with($fileType, 'image')) {
$memes_out .= renderMeme($memeID, $authorID, $title, $textContent, $createdAt, $filePath, $userNickname, $meme_template);
$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') : '';

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
function getDynamicMetadata($file): array{

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
function getNewsArticles() :array
{
global $mysqli;

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
require_once "lib/dynamic_style.php";
require_once "lib/script_data.php";
function renderDynamicPage($page_file): array

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
function initRouter(): array

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
function generateScriptData($phpArray):string {
// Check if the array is associative and single-level
if (is_array($phpArray) && count($phpArray) > 0 && count(array_filter(array_keys($phpArray), 'is_string')) === count($phpArray)) {

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
require_once "lib/account.php";

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
function makePathSafe($userInput): string
{
@ -68,11 +69,11 @@ function getIncomingFiles(): array
return $files3;
}
function saveUploadedFileInDatabase($filePath, $fileType): bool
function saveUploadedFileInDatabase(string $filePath, string $fileType, int $width, int $height): bool
{
global $mysqli;
$stmt = $mysqli->prepare("INSERT INTO Files (Path, Type, UploadedBy, UploadedAt) VALUES (?, ?, ?, NOW())");
$stmt->bind_param("ssi", $filePath, $fileType, $_SESSION["ID"]);
$stmt = $mysqli->prepare("INSERT INTO Files (Path, Type, UploadedBy, UploadedAt, Width, Height) VALUES (?, ?, ?, NOW(), ?, ?)");
$stmt->bind_param("ssiii", $filePath, $fileType, $_SESSION["ID"], $width, $height);
$stmt->execute();
$stat = $stmt->affected_rows > 0;
$stmt->close();
@ -82,19 +83,23 @@ function saveUploadedFileInDatabase($filePath, $fileType): bool
function doImageUpload($inFile, $outFile): bool
{
// Create Imagick object
$width = 0;
$height = 0;
try {
$imagick = new Imagick($inFile);
$imagick->setImageFormat('webp');
autoRotateImage($imagick);
$imagick->stripImage();
$imagick->writeImage($outFile);
$width = $imagick->getImageWidth();
$height = $imagick->getImageHeight();
$imagick->destroy();
} catch (ImagickException) {
}
// Check if the reencoding was successful
if (file_exists($outFile)) {
return saveUploadedFileInDatabase($outFile, 'image/webp');
return saveUploadedFileInDatabase($outFile, 'image/webp', $width, $height);
} else {
return false;
}

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
require_once "lib/router.php";
require_once "lib/account.php";

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
require_once "lib/router.php";
require_once "lib/account.php";

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
require_once "lib/router.php";
require_once "lib/meme.php";

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
require_once "lib/router.php";
require_once "lib/newsarticle.php";

@ -18,7 +18,7 @@
</div>
</div>
<div class="meme_body" id="meme_body___TEMPLATE_MEME_ID__">
<img id="meme_image___TEMPLATE_MEME_ID__" src="__TEMPLATE_MEME_IMAGE__" width="500" alt="meme image"
<img id="meme_image___TEMPLATE_MEME_ID__" src="__TEMPLATE_MEME_IMAGE__" width="__TEMPLATE_MEME_IMAGE_WIDTH__" height="__TEMPLATE_MEME_IMAGE_HEIGHT__" alt="meme image"
class="meme_image">
<p class="meme_text" id="meme_text___TEMPLATE_MEME_ID__">__TEMPLATE_MEME_TEXT__</p>
</div>