adlerka.top/lib/newsarticle.php

65 lines
2.2 KiB
PHP

<?php
declare(strict_types=1);
function getNewsArticles() :array
{
global $mysqli;
$output = ["Status" => "Fail"]; // Default Status is "Fail"
$articles = [];
$stmt = $mysqli->prepare("SELECT NewsArticles.ID, NewsArticles.WrittenAt, NewsArticles.WrittenBy, NewsArticles.Title, NewsArticles.Body, NewsArticles.FileList, Users.Nickname FROM NewsArticles INNER JOIN Users ON NewsArticles.WrittenBy = Users.ID WHERE NewsArticles.PrivilegeLevel <= ?;");
$id = 0;
$writtenAt = "";
$writtenBy = 0;
$title = "";
$body = "";
$filelist = 0;
$writtenByName = "";
$stmt->bind_param("i", $_SESSION["privilege_level"]);
$stmt->bind_result($id, $writtenAt, $writtenBy, $title, $body, $filelist, $writtenByName);
$stmt->execute();
while ($stmt->fetch()) {
$articles[] = [
'ID' => $id,
'WrittenAt' => $writtenAt,
'Title' => $title,
'Body' => $body,
'WrittenByName' =>$writtenByName
];
}
// Check if any results were fetched
if (!empty($articles)) {
$output["Status"] = "Success";
$output["Articles"] = $articles;
}
return $output;
}
function addNewsArticle($title="Nazov", $body="Obsah", $privilegeLevel=0) :array
{
global $mysqli;
global $routerConfig;
if ($privilegeLevel == 0){
$privilegeLevel = $routerConfig['newsarticle']['default_permissions'];
}
$output = ["Status" => "Fail"]; // Default Status is "Fail"
if (isLoggedIn() && $privilegeLevel <= $_SESSION["privilege_level"]) {
$query = $mysqli->prepare("INSERT INTO NewsArticles (WrittenBy, Title, Body, FileList, PrivilegeLevel) VALUES (?, ?, ?, 0, ?);");
$minpriv = intval($privilegeLevel);
$query->bind_param("issi", $_SESSION["ID"], htmlspecialchars($title), htmlspecialchars($body), $minpriv);
$query->execute();
if ($query->affected_rows > 0) {
$output["Status"] = "Success";
}
$query->close();
}
return $output;
}