Citations_old/api.php

380 lines
13 KiB
PHP
Raw Normal View History

2023-08-06 18:26:25 +02:00
<?php
session_start();
$mysqllogin = json_decode(file_get_contents("secrets/MysqlLogin.json"), false);
$mysqli = new mysqli('localhost', $mysqllogin->DBUser, $mysqllogin->DBPassword, $mysqllogin->DBName);
2023-08-07 10:09:29 +02:00
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
2023-08-07 15:52:21 +02:00
$action = htmlspecialchars($_POST["action"]);
2023-08-07 10:09:29 +02:00
if($action == "login"){
2023-08-07 15:52:21 +02:00
$uname = htmlspecialchars($_POST["username"]);
$pwd = htmlspecialchars($_POST["password"]);
$stmt1 = $mysqli->prepare("SELECT PWD, ID, FullName, Nickname, CanSeeFullNames, CanSeeOthersComments, IsAdmin FROM Users WHERE Username = ?;");
2023-08-07 10:09:29 +02:00
$stmt1->bind_param('s', $uname);
$stmt1->bind_result($pwdhash, $uid, $fullname, $nick, $fullnamepriv, $otherscommentspriv, $adminpriv);
$stmt1->execute();
$stmt1->store_result();
if ($stmt1->num_rows > 0){
while ($stmt1->fetch()){
if(password_verify($pwd, $pwdhash)){
$_SESSION["username"] = $uname;
$_SESSION["loggedin"] = 1;
$_SESSION["userID"] = $uid;
$_SESSION["fullname"] = $fullname;
$_SESSION["nickname"] = $nick;
$_SESSION["canseefullnames"] = $fullnamepriv;
$_SESSION["canseeotherscomments"] = $otherscommentspriv;
$_SESSION["isadmin"] = $adminpriv;
}
2023-08-06 18:26:25 +02:00
}
}
2023-08-07 10:09:29 +02:00
echo "Logged in";
return;
2023-08-07 09:37:44 +02:00
}
2023-08-06 18:26:25 +02:00
2023-08-07 10:09:29 +02:00
if($action == "logout"){
session_destroy();
echo "Logged out";
return;
}
2023-08-06 18:26:25 +02:00
2023-08-07 10:09:29 +02:00
function processUserRelated($actionin, mysqli $mysqliconn){
2023-08-06 18:26:25 +02:00
2023-08-07 10:09:29 +02:00
if($actionin == "setnickname"){
2023-08-07 15:52:21 +02:00
$newNick = htmlspecialchars($_POST["newnick"]);
2023-08-07 10:09:29 +02:00
$_SESSION["nickname"] = $newNick;
2023-08-07 15:52:21 +02:00
$stmt2 = $mysqliconn->prepare("UPDATE Users SET Nickname=? WHERE ID = ?;");
2023-08-07 10:09:29 +02:00
$stmt2->bind_param('si', $newNick, $_SESSION["userID"]);
$stmt2->execute();
$stmt2->store_result();
}
2023-08-06 18:26:25 +02:00
2023-08-07 10:09:29 +02:00
if($actionin == "setfullname"){
2023-08-07 15:52:21 +02:00
$newFull = htmlspecialchars($_POST["newFull"]);
2023-08-07 10:09:29 +02:00
$_SESSION["fullname"] = $newNick;
2023-08-07 15:52:21 +02:00
$stmt3 = $mysqliconn->prepare("UPDATE Users SET FullName=? WHERE ID = ?;");
2023-08-07 10:09:29 +02:00
$stmt3->bind_param('si', $newFull, $_SESSION["userID"]);
$stmt3->execute();
$stmt3->store_result();
}
2023-08-06 18:26:25 +02:00
2023-08-07 10:09:29 +02:00
if($actionin == "setpassword"){
2023-08-07 15:52:21 +02:00
$oldPWD = htmlspecialchars($_POST["oldPWD"]);
$newPWD = htmlspecialchars($_POST["newPWD"]);
$stmt4 = $mysqliconn->prepare("SELECT PWD FROM Users WHERE ID = ? AND Username = ?;");
2023-08-07 10:09:29 +02:00
$stmt4->bind_param('is', $_SESSION["userID"], $_SESSION["username"]);
$stmt4->bind_result($pwdhash);
$stmt4->execute();
$stmt4->store_result();
if ($stmt4->num_rows > 0){
while ($stmt4->fetch()){
if(password_verify($oldPWD, $pwdhash)){
2023-08-07 15:52:21 +02:00
$stmt5 = $mysqliconn->prepare("UPDATE Users SET PWD=? WHERE ID = ?;");
2023-08-07 10:09:29 +02:00
$newPWDhash = password_hash($newPWD, PASSWORD_DEFAULT);
$stmt5->bind_param('si', $newPWDhash, $_SESSION["userID"]);
$stmt5->execute();
$stmt5->store_result();
return "Password changed";
}
2023-08-06 18:26:25 +02:00
}
}
}
2023-08-07 10:09:29 +02:00
if($actionin == "getallusers"){
2023-08-07 15:52:21 +02:00
$stmt6 = $mysqliconn->prepare("SELECT ID, Username, Nickname, FullName, IsAdmin FROM Users;");
2023-08-07 10:09:29 +02:00
$stmt6->execute();
2023-08-07 15:52:21 +02:00
$stmt6->bind_result($id, $uname, $nick, $fnamex, $isadmin);
2023-08-07 10:09:29 +02:00
$stmt6->store_result();
$outarr = array();
if ($stmt6->num_rows > 0){
while ($stmt6->fetch()){
if ($_SESSION["canseefullnames"] == 1){
$fname = $fnamex;
}
else{
$fname = "";
}
2023-08-07 15:52:21 +02:00
$tmparr["id"] = $id;
2023-08-07 10:09:29 +02:00
$tmparr["username"] = $uname;
$tmparr["nickname"] = $nick;
$tmparr["fullname"] = $fname;
$tmparr["isadmin"] = $isadmin;
array_push($outarr, $tmparr);
2023-08-07 09:37:44 +02:00
}
}
2023-08-07 10:09:29 +02:00
return json_encode($outarr);
2023-08-07 09:37:44 +02:00
}
2023-08-07 10:09:29 +02:00
if($actionin == "adduser"){
2023-08-07 15:52:21 +02:00
if($_SESSION["isadmin"] == 1){
$stmt7 = $mysqliconn->prepare("INSERT INTO Users (Username, Nickname, FullName, PWD, CanSeeFullNames, CanSeeOthersComments, IsAdmin) VALUES (?, ?, ?, ?, ?, ?, ?);");
$uname = htmlspecialchars($_POST["username"]);
$nick = htmlspecialchars($_POST["nickname"]);
$fname = htmlspecialchars($_POST["fullname"]);
$pwdx = htmlspecialchars($_POST["password"]);
$canfullnames = intval($_POST["canseefullnames"]);
$cancomments = intval($_POST["canseeotherscomments"]);
$pwd = password_hash($pwdx, PASSWORD_DEFAULT);
$admin = intval($_POST["isadmin"]);
$stmt7->bind_param("ssssiii", $uname, $nick, $fname, $pwd, $canfullnames, $cancomments, $admin);
$stmt7->execute();
$stmt7->store_result();
return "User added";
2023-08-07 09:37:44 +02:00
}
}
2023-08-06 18:26:25 +02:00
2023-08-07 10:09:29 +02:00
return 0;
}
2023-08-06 18:26:25 +02:00
2023-08-07 10:09:29 +02:00
function processCitationRelated($actionin, mysqli $mysqliconn){
2023-08-06 18:26:25 +02:00
2023-08-07 10:09:29 +02:00
if($actionin == "getcitations"){
}
2023-08-06 18:26:25 +02:00
2023-08-07 10:09:29 +02:00
if($actionin == "getrandomcitation"){
}
2023-08-06 18:26:25 +02:00
2023-08-07 10:09:29 +02:00
if($actionin == "getcitationfulltextsearch"){
}
2023-08-06 18:26:25 +02:00
2023-08-07 10:09:29 +02:00
if($actionin == "getcitationsbyuser"){
}
2023-08-06 18:26:25 +02:00
2023-08-07 10:09:29 +02:00
if($actionin == "getcitationsbysource"){
}
2023-08-06 18:26:25 +02:00
2023-08-07 10:09:29 +02:00
if($actionin == "getcitationsbytags"){
}
2023-08-06 18:26:25 +02:00
2023-08-07 10:09:29 +02:00
if($actionin == "getcitationbyverse"){
}
return 0;
2023-08-06 18:26:25 +02:00
}
2023-08-07 10:09:29 +02:00
function processTagsRelated($actionin, mysqli $mysqliconn){
2023-08-06 18:26:25 +02:00
2023-08-07 10:09:29 +02:00
if($actionin == "createtag"){
2023-08-06 18:26:25 +02:00
2023-08-07 15:52:21 +02:00
$stmt9 = $mysqliconn->prepare("INSERT INTO Tags (TagName, CreatorID) VALUES (?, ?);");
$tagname = htmlspecialchars($_POST["tagname"]);
$uid = $_SESSION["userID"];
$stmt9->bind_param("si", $tagname, $uid);
$stmt9->execute();
$stmt9->store_result();
2023-08-07 10:09:29 +02:00
}
2023-08-06 18:26:25 +02:00
2023-08-07 10:09:29 +02:00
if($actionin == "getalltags"){
2023-08-07 15:52:21 +02:00
$stmt10 = $mysqliconn->prepare("SELECT ID, TagName, CreatorID FROM Tags;");
$stmt10->execute();
$stmt10->bind_result($id, $tagname, $creatorid);
$stmt10->store_result();
$outarr = array();
if ($stmt10->num_rows > 0){
while ($stmt10->fetch()){
$tmparr["id"] = $id;
$tmparr["tagname"] = $tagname;
$tmparr["creatorid"] = $creatorid;
array_push($outarr, $tmparr);
}
}
return json_encode($outarr);
2023-08-07 10:09:29 +02:00
}
2023-08-06 18:26:25 +02:00
2023-08-07 15:52:21 +02:00
if($actionin == "gettagsbycreator"){
$stmt11 = $mysqliconn->prepare("SELECT ID, TagName, CreatorID FROM Tags WHERE CreatorID = ?;");
$cruid = intval($_POST['creatorid']);
$stmt11->bind_param("i", $cruid);
$stmt11->execute();
$stmt11->bind_result($id, $tagname, $creatorid);
$stmt11->store_result();
$outarr = array();
if ($stmt11->num_rows > 0){
while ($stmt11->fetch()){
$tmparr["id"] = $id;
$tmparr["tagname"] = $tagname;
$tmparr["creatorid"] = $creatorid;
array_push($outarr, $tmparr);
}
}
return json_encode($outarr);
2023-08-07 10:09:29 +02:00
}
2023-08-06 18:26:25 +02:00
2023-08-07 10:09:29 +02:00
return 0;
2023-08-06 18:26:25 +02:00
}
2023-08-07 10:09:29 +02:00
function processSourcesRelated($actionin, mysqli $mysqliconn){
2023-08-06 18:26:25 +02:00
2023-08-07 10:09:29 +02:00
if($actionin == "createsource"){
2023-08-07 15:52:21 +02:00
$stmt12 = $mysqliconn->prepare("INSERT INTO Sources (PublicationID, Place, CreatorID) VALUES (?, ?, ?);");
$publicationid = intval($_POST["publicationid"]);
$place = htmlspecialchars($_POST["place"]);
$cruid = $_SESSION["userID"];
$stmt12->bind_param("isi", $publicationid, $place, $cruid);
$stmt12->execute();
$stmt12->store_result();
2023-08-07 10:09:29 +02:00
}
2023-08-06 18:26:25 +02:00
2023-08-07 10:09:29 +02:00
if($actionin == "getallsources"){
2023-08-07 15:52:21 +02:00
$stmt13 = $mysqliconn->prepare("SELECT ID, PublicationID, Place, CreatorID FROM Sources;");
$stmt13->execute();
$stmt13->bind_result($id, $publicationid, $place, $creatorid);
$stmt13->store_result();
$outarr = array();
if ($stmt13->num_rows > 0){
while ($stmt13->fetch()){
$tmparr["id"] = $id;
$tmparr["publicationid"] = $publicationid;
$tmparr["place"] = $place;
$tmparr["creatorid"] = $creatorid;
array_push($outarr, $tmparr);
}
}
return json_encode($outarr);
2023-08-07 10:09:29 +02:00
}
2023-08-06 18:26:25 +02:00
2023-08-07 10:09:29 +02:00
if($actionin == "getallsourcesbycreator"){
2023-08-07 15:52:21 +02:00
$stmt13 = $mysqliconn->prepare("SELECT ID, PublicationID, Place, CreatorID FROM Sources WHERE CreatorID = ?;");
$cruid = intval($_POST['creatorid']);
$stmt13->bind_param("i", $cruid);
$stmt13->execute();
$stmt13->bind_result($id, $publicationid, $place, $creatorid);
$stmt13->store_result();
$outarr = array();
if ($stmt13->num_rows > 0){
while ($stmt13->fetch()){
$tmparr["id"] = $id;
$tmparr["publicationid"] = $publicationid;
$tmparr["place"] = $place;
$tmparr["creatorid"] = $creatorid;
array_push($outarr, $tmparr);
}
}
return json_encode($outarr);
2023-08-07 10:09:29 +02:00
}
2023-08-06 18:26:25 +02:00
2023-08-07 10:09:29 +02:00
return 0;
2023-08-06 18:26:25 +02:00
}
2023-08-07 10:09:29 +02:00
function processCommentRelated($actionin, mysqli $mysqliconn){
2023-08-06 18:26:25 +02:00
2023-08-07 10:09:29 +02:00
if($actionin == "createcomment"){
2023-08-07 15:52:21 +02:00
$stmt14 = $mysqliconn->prepare("INSERT INTO Comments (CreatorID, CitationID, CitationThreadID, Content) VALUES (?, ?, ?, ?);");
$creatorid = $_SESSION["userid"];
$citationid = intval($_POST["citationid"]);
$citationthreadid = intval($_POST["citationthreadid"]);
$content = htmlspecialchars($_POST["content"]);
$stmt14->bind_param("iiis", $creatorid, $citationid, $citationthreadid, $content);
$stmt14->execute();
$stmt14->store_result();
2023-08-07 10:09:29 +02:00
}
2023-08-06 18:26:25 +02:00
2023-08-07 10:09:29 +02:00
if($actionin == "getallcommentsbycreator"){
2023-08-07 15:52:21 +02:00
$stmt15 = $mysqliconn->prepare("SELECT ID, CreatorID, CitationID, CitationThreadID, Content FROM Comments WHERE CreatorID = ?;");
$cruid = intval($_POST['creatorid']);
$stmt15->bind_param("i", $cruid);
$stmt15->execute();
$stmt15->bind_result($id, $creatorid, $citationid, $citationthreadid, $content);
$stmt15->store_result();
$outarr = array();
if ($stmt15->num_rows > 0){
while ($stmt15->fetch()){
$tmparr["id"] = $id;
$tmparr["creatorid"] = $creatorid;
$tmparr["citationid"] = $citationid;
$tmparr["citationthreadid"] = $citationthreadid;
$tmparr["content"] = $content;
array_push($outarr, $tmparr);
}
}
return json_encode($outarr);
2023-08-07 10:09:29 +02:00
}
2023-08-06 18:26:25 +02:00
2023-08-07 10:09:29 +02:00
if($actionin == "getcommentsbycitationlatestversion"){
}
2023-08-06 18:26:25 +02:00
2023-08-07 15:52:21 +02:00
if($actionin == "getcommentsbycitationbyversion"){
}
2023-08-07 10:09:29 +02:00
if($actionin == "getcommentsbycitationallversions"){
}
2023-08-06 18:26:25 +02:00
2023-08-07 10:09:29 +02:00
return 0;
2023-08-06 18:26:25 +02:00
}
2023-08-07 15:52:21 +02:00
function processAuthorRelated($actionin, mysqli $mysqliconn){
}
function processPublicationRelated($actionin, mysqli $mysqliconn){
}
2023-08-07 10:09:29 +02:00
if($_SESSION["loggedin"] == 1){
2023-08-06 18:26:25 +02:00
2023-08-07 10:09:29 +02:00
$citationreturn = processCitationRelated($action, $mysqli);
if($citationreturn != 0){
echo $citationreturn;
return;
}
2023-08-06 18:26:25 +02:00
2023-08-07 10:09:29 +02:00
$commentreturn = processCommentRelated($action, $mysqli);
if($commentreturn != 0){
echo $commentreturn;
return;
}
$tagsreturn = processTagsRelated($action, $mysqli);
if($tagsreturn != 0){
echo $tagsreturn;
return;
}
2023-08-06 18:26:25 +02:00
2023-08-07 10:09:29 +02:00
$sourcereturn = processSourcesRelated($action, $mysqli);
if($sourcereturn != 0){
echo $sourcereturn;
return;
}
2023-08-06 18:26:25 +02:00
2023-08-07 15:52:21 +02:00
$authorreturn = processAuthorRelated($action, $mysqli);
if($authorreturn != 0){
echo $authorreturn;
return;
}
$publicationreturn = processPublicationRelated($action, $mysqli);
if($publicationreturn != 0){
echo $publicationreturn;
return;
}
2023-08-07 10:09:29 +02:00
$userreturn = processUserRelated($action, $mysqli);
if($userreturn != 0){
echo $userreturn;
return;
}
2023-08-06 18:26:25 +02:00
}
2023-08-07 10:09:29 +02:00
else{
echo "Not logged in";
2023-08-07 09:37:44 +02:00
return;
2023-08-06 18:26:25 +02:00
}
}
?>