update to mysql

This commit is contained in:
Bruno Rybársky 2023-06-10 09:10:10 +02:00
parent 40be40db92
commit 92cdabb7ae
No known key found for this signature in database
GPG Key ID: DFE2C061EF985CD4
2 changed files with 88 additions and 113 deletions

@ -1,62 +1,52 @@
<?php <?php
$action = $_POST['action']; $action = $_POST['action'];
if (!empty($action)){
$config = json_decode(file_get_contents("../data/config.json"), true); $config = json_decode(file_get_contents("../data/config.json"), true);
if (!empty($action) && $_POST["password"] == $config["admin_pwd"]){
$mysqli = new mysqli($config["mysqlhost"], $config["mysqluser"], $config["mysqlpass"], $config["mysqldb"]);
if ($action == "verify"){ if ($action == "verify"){
if ($_POST["password"] == $config["admin_pwd"]){
echo "OK"; echo "OK";
} }
else{
echo "ERROR";
}
}
if ($action == "get"){ if ($action == "get"){
if ($_POST["password"] == $config["admin_pwd"]){ $stmt1 = $mysqli->prepare("SELECT `LinkID`, `ContentID`, `Nickname` FROM Links");
if(!file_exists("../data/pageindex.json")){ $stmt1->bind_result($linkid, $contentid, $nickname);
file_put_contents("../data/pageindex.json", "{}"); $stmt1->execute();
} $stmt1->store_result();
$stranky = json_decode(file_get_contents("../data/pageindex.json"), true);
$out = "<table><tbody><tr><th>ID</th><th>Content ID</th><th>Nickname</th><th>Button</th></tr>"; $out = "<table><tbody><tr><th>ID</th><th>Content ID</th><th>Nickname</th><th>Button</th></tr>";
foreach($stranky as $idx => $arr) { while ($stmt1->fetch()) {
$id = htmlspecialchars($idx); $id = htmlspecialchars($linkid);
$nickname = htmlspecialchars($arr["nickname"]); $nickname = htmlspecialchars($nickname);
$contentid = htmlspecialchars($arr["contentid"]); $contentid = htmlspecialchars($contentid);
$out = $out . "<tr><td>$id</td><td>$contentid</td><td>$nickname</td><td><button onclick=\"delthis('$id')\">Delete</button></td></tr>"; $out = $out . "<tr><td>$id</td><td>$contentid</td><td>$nickname</td><td><button onclick=\"delthis('$id')\">Delete</button></td></tr>";
} }
$out = $out . "<tr><td><input id=\"addid\" placeholder=\"ID\"></td><td><input id=\"addcontentid\" placeholder=\"Content ID\"></td><td><input id=\"addnickname\" placeholder=\"Nickname\"></td><td><button id=\"linkadder\" onclick=\"justadd()\">Add</button></td></tr>"; $out = $out . "<tr><td><input id=\"addid\" placeholder=\"ID\"></td><td><input id=\"addcontentid\" placeholder=\"Content ID\"></td><td><input id=\"addnickname\" placeholder=\"Nickname\"></td><td><button id=\"linkadder\" onclick=\"justadd()\">Add</button></td></tr>";
$out = $out . "</tbody></table>"; $out = $out . "</tbody></table>";
echo $out; echo $out;
} }
}
if ($action == "delete" && !empty($_POST["id"])){ if ($action == "delete" && !empty($_POST["id"])){
if ($_POST["password"] == $config["admin_pwd"]){ $stmt2 = $mysqli->prepare("DELETE FROM Links WHERE `LinkID` = ?");
$stranky = json_decode(file_get_contents("../data/pageindex.json"), true); $stmt2->bind_param('s', $_POST["id"]);
unset($stranky[$_POST["id"]]); $stmt2->execute();
file_put_contents("../data/pageindex.json", json_encode($stranky, JSON_PRETTY_PRINT)); $stmt2->store_result();
}
} }
if ($action == "set" && !empty($_POST["id"]) && !empty($_POST["contentid"]) && !empty($_POST["nickname"])){ if ($action == "set" && !empty($_POST["id"]) && !empty($_POST["contentid"]) && !empty($_POST["nickname"])){
if ($_POST["password"] == $config["admin_pwd"]){ $stmt3 = $mysqli->prepare("INSERT INTO Links (`LinkID`, `ContentID`, `Nickname`) VALUES (?, ?, ?);");
$stranky = json_decode(file_get_contents("../data/pageindex.json"), true); $stmt3->bind_param('sss', $_POST["id"], $_POST["contentid"], $_POST["nickname"]);
$tmp = array("contentid"=>$_POST["contentid"], "nickname"=>$_POST["nickname"]); $stmt3->execute();
$stranky[$_POST["id"]] = $tmp; $stmt3->store_result();
file_put_contents("../data/pageindex.json", json_encode($stranky, JSON_PRETTY_PRINT));
}
} }
if ($action == "getcontent"){ if ($action == "getcontent"){
if(!file_exists("../data/contents.json")){ $stmt4 = $mysqli->prepare("SELECT `ContentID`, `Content` FROM Content");
file_put_contents("../data/contents.json", "{}"); $stmt4->bind_result ($contentid, $content);
} $stmt4->execute();
if ($_POST["password"] == $config["admin_pwd"]){ $stmt4->store_result();
$contents = json_decode(file_get_contents("../data/contents.json"), true);
$out = "<table><tbody><tr><th>Content ID</th><th>Content</th><th>Button</th></tr>"; $out = "<table><tbody><tr><th>Content ID</th><th>Content</th><th>Button</th></tr>";
foreach($contents as $idx => $content) { while ($stmt4->fetch()) {
$id = htmlspecialchars($idx); $id = htmlspecialchars($contentid);
$content = htmlspecialchars($content); $content = htmlspecialchars($content);
$out = $out . "<tr><td>$id</td><td>$content</td><td><button onclick=\"delcthis('$id')\">Delete</button></td></tr>"; $out = $out . "<tr><td>$id</td><td>$content</td><td><button onclick=\"delcthis('$id')\">Delete</button></td></tr>";
} }
@ -64,43 +54,42 @@ if (!empty($action)){
$out = $out . "</tbody></table>"; $out = $out . "</tbody></table>";
echo $out; echo $out;
} }
}
if ($action == "deletecontent" && !empty($_POST["id"])){ if ($action == "deletecontent" && !empty($_POST["id"])){
if ($_POST["password"] == $config["admin_pwd"]){ $stmt5 = $mysqli->prepare("DELETE FROM Content WHERE `ContentID` = ?");
$contents = json_decode(file_get_contents("../data/contents.json"), true); $stmt5->bind_param('s', $_POST["id"]);
unset($contents[$_POST["id"]]); $stmt5->execute();
file_put_contents("../data/contents.json", json_encode($contents, JSON_PRETTY_PRINT)); $stmt5->store_result();
}
} }
if ($action == "setcontent" && !empty($_POST["id"]) && !empty($_POST["content"])){ if ($action == "setcontent" && !empty($_POST["id"]) && !empty($_POST["content"])){
if ($_POST["password"] == $config["admin_pwd"]){ $stmt6 = $mysqli->prepare("INSERT INTO Content (`ContentID`, `Content`) VALUES (?, ?);");
$contents = json_decode(file_get_contents("../data/contents.json"), true); $stmt6->bind_param('ss', $_POST["id"], $_POST["content"]);
$contents[$_POST["id"]] = $_POST["content"]; $stmt6->execute();
file_put_contents("../data/contents.json", json_encode($contents, JSON_PRETTY_PRINT)); $stmt6->store_result();
}
} }
if ($action == "getlog"&& !empty($_POST["id"])){ if ($action == "getlog"&& !empty($_POST["id"])){
if(!file_exists("../data/logs.json")){ $stmt7 = $mysqli->prepare("SELECT `ID`, `Action`, `LinkID`, `ContentID`, `Datetime`, `Timestamp`, `Nickname`, `IP` FROM Logs");
file_put_contents("../data/logs.json", "{}"); $stmt7->bind_result ($rowidx, $actionx, $linkidx, $contentidx, $datetimex, $timestampx, $nicknamex, $ipx);
} $stmt7->execute();
if ($_POST["password"] == $config["admin_pwd"]){ $stmt7->store_result();
$logy = json_decode(file_get_contents("../data/logs.json"), true)[$_POST["id"]]; $out = "<table><tbody><tr><th>Action</th><th>Date and Time</th><th>Nickname</th><th>Link</th><th>Content</th><th>IP</th></tr>";
$out = "<table><tbody><tr><th>Action</th><th>Date and Time</th><th>Nickname</th><th>Link</th><th>IP</th></tr>"; while ($stmt7->fetch()) {
foreach($logy as $arr) { $action = htmlspecialchars($actionx);
$action = htmlspecialchars($arr["action"]); $link = htmlspecialchars($linkidx);
$link = htmlspecialchars($arr["link"]); $content = htmlspecialchars($contentidx);
$nickname = htmlspecialchars($arr["nickname"]); $nickname = htmlspecialchars($nicknamex);
$ip = htmlspecialchars($arr["ip"]); $ip = htmlspecialchars($ipx);
$datetime = htmlspecialchars($arr["datetime"]); $datetime = htmlspecialchars($datetimex);
$out = $out . "<tr><td>$action</td><td>$datetime</td><td>$nickname</td><td>$link</td><td>$ip</td></tr>"; $out = $out . "<tr><td>$action</td><td>$datetime</td><td>$nickname</td><td>$link</td><td>$content</td><td>$ip</td></tr>";
} }
$out = $out . "</tbody></table>"; $out = $out . "</tbody></table>";
echo $out; echo $out;
} }
}
} }
else{
echo "ERROR";
}
?> ?>

@ -4,52 +4,38 @@ $idcko = $_GET['id'];
if (!empty($idcko)){ if (!empty($idcko)){
$config = json_decode(file_get_contents("data/config.json"), true);
$mysqli = new mysqli($config["mysqlhost"], $config["mysqluser"], $config["mysqlpass"], $config["mysqldb"]);
$ipcka = $_SERVER['REMOTE_ADDR']; $ipcka = $_SERVER['REMOTE_ADDR'];
$curdate = date("H:i:s d.m.Y");
if(!file_exists("data/logs.json")){ $stmt1 = $mysqli->prepare("SELECT `LinkID`, `ContentID`, `Nickname` FROM `Links` WHERE `LinkID` = ?");
file_put_contents("data/logs.json", "{}"); $stmt1->bind_param("s", $idcko);
$stmt1->bind_result($linkid, $contentid, $nickname);
$stmt1->execute();
$stmt1->store_result();
if ($stmt1->num_rows() < 1) {
die("Nonexistent link");
} }
while ($stmt1->fetch()) {
if(!file_exists("data/pageindex.json")){ $stmt2 = $mysqli->prepare("SELECT `Content` FROM Content WHERE `ContentID` = ?");
file_put_contents("data/pageindex.json", "{}"); $stmt2->bind_param("s", $contentid);
} $stmt2->bind_result ($content);
$stmt2->execute();
if(!file_exists("data/contents.json")){ $stmt2->store_result();
file_put_contents("data/contents.json", "{}"); if ($stmt2->num_rows() < 1) {
}
$stranky = json_decode(file_get_contents("data/pageindex.json"), true);
$contents = json_decode(file_get_contents("data/contents.json"), true);
$logs = json_decode(file_get_contents("data/logs.json"), true);
$alllog = "data/all.log";
if(array_key_exists($idcko, $stranky)){
$entry = $stranky[$idcko];
if (!array_key_exists($entry["contentid"], $contents)){
die("Faulty link"); die("Faulty link");
} }
while ($stmt2->fetch()) {
$content = $contents[$entry["contentid"]]; $action = "Success";
$nickname = $entry["nickname"]; $stmt3 = $mysqli->prepare("INSERT INTO Logs (`Action`, `LinkID`, `ContentID`, `Datetime`, `Nickname`, `IP`) VALUES (?, ?, ?, ?, ?, ?);");
$logmessage = "Success from $nickname($idcko), IP: $ipcka\n"; $stmt3->bind_param('ssssss', $action, $idcko, $contentid, $curdate, $nickname, $ipcka);
if (!array_key_exists($idcko, $logs)){ $stmt3->execute();
$logs[$idcko] = []; $stmt3->store_result();
}
$logs[$idcko][] = array("action"=>"Success", "datetime"=>date("H:i:s d.m.Y"), "link"=>$idcko, "nickname"=>$nickname, "ip"=>$ipcka);
file_put_contents("data/logs.json", json_encode($logs, JSON_PRETTY_PRINT));
file_put_contents($alllog, $logmessage, FILE_APPEND);
echo $content; echo $content;
} }
else{
die("Nonexistent link");
} }
} }
else{ else{