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
$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 ($_POST["password"] == $config["admin_pwd"]){
echo "OK";
}
else{
echo "ERROR";
}
}
if ($action == "get"){
if ($_POST["password"] == $config["admin_pwd"]){
if(!file_exists("../data/pageindex.json")){
file_put_contents("../data/pageindex.json", "{}");
}
$stranky = json_decode(file_get_contents("../data/pageindex.json"), true);
$stmt1 = $mysqli->prepare("SELECT `LinkID`, `ContentID`, `Nickname` FROM Links");
$stmt1->bind_result($linkid, $contentid, $nickname);
$stmt1->execute();
$stmt1->store_result();
$out = "<table><tbody><tr><th>ID</th><th>Content ID</th><th>Nickname</th><th>Button</th></tr>";
foreach($stranky as $idx => $arr) {
$id = htmlspecialchars($idx);
$nickname = htmlspecialchars($arr["nickname"]);
$contentid = htmlspecialchars($arr["contentid"]);
while ($stmt1->fetch()) {
$id = htmlspecialchars($linkid);
$nickname = htmlspecialchars($nickname);
$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><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>";
echo $out;
}
}
if ($action == "delete" && !empty($_POST["id"])){
if ($_POST["password"] == $config["admin_pwd"]){
$stranky = json_decode(file_get_contents("../data/pageindex.json"), true);
unset($stranky[$_POST["id"]]);
file_put_contents("../data/pageindex.json", json_encode($stranky, JSON_PRETTY_PRINT));
}
$stmt2 = $mysqli->prepare("DELETE FROM Links WHERE `LinkID` = ?");
$stmt2->bind_param('s', $_POST["id"]);
$stmt2->execute();
$stmt2->store_result();
}
if ($action == "set" && !empty($_POST["id"]) && !empty($_POST["contentid"]) && !empty($_POST["nickname"])){
if ($_POST["password"] == $config["admin_pwd"]){
$stranky = json_decode(file_get_contents("../data/pageindex.json"), true);
$tmp = array("contentid"=>$_POST["contentid"], "nickname"=>$_POST["nickname"]);
$stranky[$_POST["id"]] = $tmp;
file_put_contents("../data/pageindex.json", json_encode($stranky, JSON_PRETTY_PRINT));
}
$stmt3 = $mysqli->prepare("INSERT INTO Links (`LinkID`, `ContentID`, `Nickname`) VALUES (?, ?, ?);");
$stmt3->bind_param('sss', $_POST["id"], $_POST["contentid"], $_POST["nickname"]);
$stmt3->execute();
$stmt3->store_result();
}
if ($action == "getcontent"){
if(!file_exists("../data/contents.json")){
file_put_contents("../data/contents.json", "{}");
}
if ($_POST["password"] == $config["admin_pwd"]){
$contents = json_decode(file_get_contents("../data/contents.json"), true);
$stmt4 = $mysqli->prepare("SELECT `ContentID`, `Content` FROM Content");
$stmt4->bind_result ($contentid, $content);
$stmt4->execute();
$stmt4->store_result();
$out = "<table><tbody><tr><th>Content ID</th><th>Content</th><th>Button</th></tr>";
foreach($contents as $idx => $content) {
$id = htmlspecialchars($idx);
while ($stmt4->fetch()) {
$id = htmlspecialchars($contentid);
$content = htmlspecialchars($content);
$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>";
echo $out;
}
}
if ($action == "deletecontent" && !empty($_POST["id"])){
if ($_POST["password"] == $config["admin_pwd"]){
$contents = json_decode(file_get_contents("../data/contents.json"), true);
unset($contents[$_POST["id"]]);
file_put_contents("../data/contents.json", json_encode($contents, JSON_PRETTY_PRINT));
}
$stmt5 = $mysqli->prepare("DELETE FROM Content WHERE `ContentID` = ?");
$stmt5->bind_param('s', $_POST["id"]);
$stmt5->execute();
$stmt5->store_result();
}
if ($action == "setcontent" && !empty($_POST["id"]) && !empty($_POST["content"])){
if ($_POST["password"] == $config["admin_pwd"]){
$contents = json_decode(file_get_contents("../data/contents.json"), true);
$contents[$_POST["id"]] = $_POST["content"];
file_put_contents("../data/contents.json", json_encode($contents, JSON_PRETTY_PRINT));
}
$stmt6 = $mysqli->prepare("INSERT INTO Content (`ContentID`, `Content`) VALUES (?, ?);");
$stmt6->bind_param('ss', $_POST["id"], $_POST["content"]);
$stmt6->execute();
$stmt6->store_result();
}
if ($action == "getlog"&& !empty($_POST["id"])){
if(!file_exists("../data/logs.json")){
file_put_contents("../data/logs.json", "{}");
}
if ($_POST["password"] == $config["admin_pwd"]){
$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>IP</th></tr>";
foreach($logy as $arr) {
$action = htmlspecialchars($arr["action"]);
$link = htmlspecialchars($arr["link"]);
$nickname = htmlspecialchars($arr["nickname"]);
$ip = htmlspecialchars($arr["ip"]);
$datetime = htmlspecialchars($arr["datetime"]);
$out = $out . "<tr><td>$action</td><td>$datetime</td><td>$nickname</td><td>$link</td><td>$ip</td></tr>";
$stmt7 = $mysqli->prepare("SELECT `ID`, `Action`, `LinkID`, `ContentID`, `Datetime`, `Timestamp`, `Nickname`, `IP` FROM Logs");
$stmt7->bind_result ($rowidx, $actionx, $linkidx, $contentidx, $datetimex, $timestampx, $nicknamex, $ipx);
$stmt7->execute();
$stmt7->store_result();
$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>";
while ($stmt7->fetch()) {
$action = htmlspecialchars($actionx);
$link = htmlspecialchars($linkidx);
$content = htmlspecialchars($contentidx);
$nickname = htmlspecialchars($nicknamex);
$ip = htmlspecialchars($ipx);
$datetime = htmlspecialchars($datetimex);
$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>";
echo $out;
}
}
}
else{
echo "ERROR";
}
?>

@ -4,52 +4,38 @@ $idcko = $_GET['id'];
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'];
$curdate = date("H:i:s d.m.Y");
if(!file_exists("data/logs.json")){
file_put_contents("data/logs.json", "{}");
$stmt1 = $mysqli->prepare("SELECT `LinkID`, `ContentID`, `Nickname` FROM `Links` WHERE `LinkID` = ?");
$stmt1->bind_param("s", $idcko);
$stmt1->bind_result($linkid, $contentid, $nickname);
$stmt1->execute();
$stmt1->store_result();
if ($stmt1->num_rows() < 1) {
die("Nonexistent link");
}
if(!file_exists("data/pageindex.json")){
file_put_contents("data/pageindex.json", "{}");
}
if(!file_exists("data/contents.json")){
file_put_contents("data/contents.json", "{}");
}
$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)){
while ($stmt1->fetch()) {
$stmt2 = $mysqli->prepare("SELECT `Content` FROM Content WHERE `ContentID` = ?");
$stmt2->bind_param("s", $contentid);
$stmt2->bind_result ($content);
$stmt2->execute();
$stmt2->store_result();
if ($stmt2->num_rows() < 1) {
die("Faulty link");
}
$content = $contents[$entry["contentid"]];
$nickname = $entry["nickname"];
$logmessage = "Success from $nickname($idcko), IP: $ipcka\n";
if (!array_key_exists($idcko, $logs)){
$logs[$idcko] = [];
}
$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);
while ($stmt2->fetch()) {
$action = "Success";
$stmt3 = $mysqli->prepare("INSERT INTO Logs (`Action`, `LinkID`, `ContentID`, `Datetime`, `Nickname`, `IP`) VALUES (?, ?, ?, ?, ?, ?);");
$stmt3->bind_param('ssssss', $action, $idcko, $contentid, $curdate, $nickname, $ipcka);
$stmt3->execute();
$stmt3->store_result();
echo $content;
}
else{
die("Nonexistent link");
}
}
else{