adlerka.top/lib/meme.php

65 lines
2.5 KiB
PHP
Raw Normal View History

2024-04-11 10:36:40 +02:00
<?php
2024-04-25 22:50:35 +02:00
require_once "lib/upload.php";
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 09:04:10 +02:00
if (isLoggedIn() && fileExists($imageID)) {
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-11 10:36:40 +02:00
$stmtMemeAdd->execute();
2024-04-25 22:55:00 +02:00
if($stmtMemeAdd->affected_rows > 0) {
$output["Status"] = "Success";
}
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-25 09:40:21 +02:00
function renderMeme(string $title, string $textContent, string $createdAt, string $filePath, string $userNickname): string
{
global $routerConfig;
2024-04-25 09:45:40 +02:00
$meme_template = file_get_contents($routerConfig['template_dir'] . "meme.html");
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);
$meme_out = str_replace('__TEMPLATE_MEME_IMAGE__', htmlspecialchars($filePath), $meme_out);
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;
$stmtlist = $mysqli->prepare('SELECT Memes.ID, Memes.Title, Memes.TextContent, Memes.CreatedAt, Files.Path, 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
$stmtlist->execute();
$memeID = 0;
$title = "";
$textContent = "";
$filePath = "";
$fileType = "";
$userNickname = "";
$createdAt = "";
// Bind the result variables
$stmtlist->bind_result($memeID, $title, $textContent, $createdAt, $filePath, $fileType, $userNickname);
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 = '';
while ($stmtlist->fetch()) {
if ($fileType == 'image') {
$memes_out .= renderMeme($title, $textContent, $createdAt, $filePath, $userNickname);
}
2024-04-25 09:04:10 +02:00
}
2024-04-25 09:40:21 +02:00
$meme_gallery_out = str_replace('__TEMPLATE_MEMES_HERE__', $memes_out, $meme_gallery_template);
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-11 10:36:40 +02:00
}