This commit is contained in:
Bruno Rybársky 2023-06-09 21:45:45 +02:00
parent ee3a64e0f6
commit a74835d2cb
No known key found for this signature in database
GPG Key ID: DFE2C061EF985CD4
7 changed files with 303 additions and 7 deletions

97
admin/api.php Normal file

@ -0,0 +1,97 @@
<?php
$action = $_POST['action'];
if (!empty($action)){
$config = json_decode(file_get_contents("../data/config.json"), true);
if ($action == "verify"){
if ($_POST["password"] == $config["admin_pwd"]){
echo "OK";
}
else{
echo "ERROR";
}
}
if ($action == "get"){
if ($_POST["password"] == $config["admin_pwd"]){
$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>";
foreach($stranky as $idx => $arr) {
$id = htmlspecialchars($idx);
$nickname = htmlspecialchars($arr["nickname"]);
$contentid = htmlspecialchars($arr["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));
}
}
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));
}
}
if ($action == "getcontent"){
if ($_POST["password"] == $config["admin_pwd"]){
$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>";
foreach($contents as $idx => $content) {
$id = htmlspecialchars($idx);
$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><input id=\"addidc\" placeholder=\"Content ID\"></td><td><textarea id=\"addcontentc\" placeholder=\"Content\"></textarea></td><td><button id=\"contentadder\" onclick=\"justaddc()\">Add</button></td></tr>";
$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));
}
}
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));
}
}
if ($action == "getlog"&& !empty($_POST["id"])){
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>";
}
$out = $out . "</tbody></table>";
echo $out;
}
}
}
?>

34
admin/index.html Normal file

@ -0,0 +1,34 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<link rel="stylesheet" href="pico.css" type="text/css">
<link rel="stylesheet" href="style.css" type="text/css">
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="script.js"></script>
<title>Content dispenser admin</title>
</head>
<body>
<div id="pwdentry">
<input type="password" id="pwdbox">
<button id="submitpwd">Login</button>
</div>
<div id="indexmanagement">
<h1>Link manager:</h1>
<div id="linkslist">
</div>
<h1>Content manager:</h1>
<div id="contentlist">
</div>
<h1>Log manager:</h1>
<input type="text" id="logid"><button id="logget">Get log</button><br>
<div id="loglist">
</div>
</div>
</body>
</html>

2
admin/jquery.js vendored Normal file

File diff suppressed because one or more lines are too long

4
admin/pico.css Normal file

File diff suppressed because one or more lines are too long

151
admin/script.js Normal file

@ -0,0 +1,151 @@
let password = "";
let datatable = {};
let content = "";
function justadd(){
let id = $("#addid").val();
let contentid = $("#addcontentid").val();
let nickname = $("#addnickname").val();
$.post("api.php",
{
action: "set",
password: password,
id: id,
contentid: contentid,
nickname: nickname,
}, function( data ) {
getlinks();
});
}
function justaddc(){
let id = $("#addidc").val();
let content = $("#addcontentc").val();
$.post("api.php",
{
action: "setcontent",
password: password,
id: id,
content: content,
}, function( data ) {
getcontents();
});
}
function delcthis(name){
$.post("api.php",
{
action: "deletecontent",
password: password,
id: name
}, function( data ) {
getcontents();
});
}
function getcontents(){
$.post("api.php",
{
action: "getcontent",
password: password
}, function( data ) {
$("#contentlist").html(data);
$("#addcontentc").keyup(function(event) {
if (event.keyCode === 13) {
$("#contentadder").click();
$("#addidc").focus();
}
});
$("#addidc").keyup(function(event) {
if (event.keyCode === 13) {
$("#addcontentc").focus();
}
});
});
}
function delthis(name){
$.post("api.php",
{
action: "delete",
password: password,
id: name
}, function( data ) {
getlinks();
});
}
function verifyPassword(){
password = $("#pwdbox").val();
$.post("api.php",
{
action: "verify",
password: password
}, function( data ) {
if (data == "OK"){
getlinks();
getcontents();
$("#pwdentry").fadeOut("slow", function(){
$("#indexmanagement").fadeIn("slow", function(){
});
});
}
});
}
function getlinks(){
$.post("api.php",
{
action: "get",
password: password
}, function( data ) {
$("#linkslist").html(data);
$("#addnickname").keyup(function(event) {
if (event.keyCode === 13) {
$("#linkadder").click();
$("#addid").focus();
}
});
$("#addid").keyup(function(event) {
if (event.keyCode === 13) {
$("#addcontentid").focus();
}
});
$("#addcontentid").keyup(function(event) {
if (event.keyCode === 13) {
$("#addnickname").focus();
}
});
});
}
function getlog(){
let id = $("#logid").val();
$.post("api.php",
{
action: "getlog",
password: password,
id: id
}, function( data ) {
$("#loglist").html(data);
});
}
$(function() {
$("#submitpwd").click(verifyPassword);
$("#logget").click(getlog);
$("#logid").keyup(function(event) {
if (event.keyCode === 13) {
$("#logget").click();
}
});
});

3
admin/style.css Normal file

@ -0,0 +1,3 @@
#indexmanagement{
display: none;
}

@ -5,28 +5,33 @@ $idcko = $_GET['id'];
if (!empty($idcko)){ if (!empty($idcko)){
$ipcka = $_SERVER['REMOTE_ADDR']; $ipcka = $_SERVER['REMOTE_ADDR'];
$stranky = json_decode(file_get_contents("data/pageindex.json"), true); $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"; $alllog = "data/all.log";
$pagepath = "data/pages/";
if(array_key_exists($idcko, $stranky)){ if(array_key_exists($idcko, $stranky)){
$entry = $stranky[$idcko]; $entry = $stranky[$idcko];
$logfile = $pagepath . $entry["logfile"]; $content = $contents[$entry["contentid"]];
$contentfile = $pagepath . $entry["contentfile"];
$nickname = $entry["nickname"]; $nickname = $entry["nickname"];
$logmessage = "Success from $nickname(\"$idcko\"), IP: $ipcka\n"; $logmessage = "Success from $linkname, 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($logfile, $logmessage, FILE_APPEND); file_put_contents("data/logs.json", json_encode($logs, JSON_PRETTY_PRINT));
file_put_contents($alllog, $logmessage, FILE_APPEND); file_put_contents($alllog, $logmessage, FILE_APPEND);
echo file_get_contents($contentfile); echo $content;
} }
else{ else{
die("Bad link"); die("Bad link");
file_put_contents($pagepath . $entry["logfile"], "Fail from \"$idcko\", IP: $ipcka\n", FILE_APPEND);
} }
} }
else{ else{