OWE/admin.php

219 lines
7.1 KiB
PHP

<?php
if (null !== $_POST['set_pwd']) {
$pwd = $_POST['set_pwd'];
file_put_contents('data/admin_pwd', password_hash($pwd, PASSWORD_DEFAULT));
setcookie('owe_admin', base64_encode($pwd), ['httponly' => true]);
header('location: admin.php');
} elseif (null !== $_POST['pwd']) {
$pwd = $_POST['pwd'];
if (password_verify($pwd, file_get_contents('data/admin_pwd'))) {
setcookie('owe_admin', base64_encode($pwd), ['httponly' => true]);
header('location: admin.php');
print("DER KEKS MÜSSTE GESETZT SEIN");
} else {
print("Nope, falsch");
}
}
?>
<!DOCTYPE html>
<?php
$raw_json_config = file_get_contents("data/config.json");
$config_data = json_decode($raw_json_config, false);
?>
<html>
<head>
<title><?php print("Admin ".($config_data->title ?? "One Word Each")); ?></title>
<link rel="stylesheet" href="style.css">
<link rel="icon" href="<?php print($config_data->logo->icon_path ?? "favicon.ico"); ?>" type="image/x-icon">
</head>
<body>
<?php
if (isset($config_data->style)) {
print("<style>\n");
print($config_data->style);
print("\n</style>");
}
?>
<h1><?php print($config_data->headline ?? "One Word Each"); ?> - Administration</h1>
<p><a href="./">Startseite</a></p>
<?php
if (file_exists('data/admin_pwd')):
$token = $_COOKIE['owe_admin'];
$pwd=base64_decode($token);
if (password_verify($pwd, file_get_contents('data/admin_pwd'))) {
$login = true;
} else {
// either cookie didn't exist or is wrong
}
else:
?>
Du kannst Admin werden.
<form action="./admin.php" method="post">
<label>Passwort: <input type="password" required name="set_pwd" style="font-size: xx-large; border: 2px solid white;"></label>
<button>Registrieren</button>
</form>
<?php
endif;
if ($login):
?>
<div>
<h3>Archiv erstellen</h3>
Bisherige Archive:
<p id="archive_links">
<?php
$content = file_get_contents("./data/archive-links.txt") or die("ungültiger Dateiname");
print($content);
?></p>
Neues Archiv:<br>
<label>Erster Tag: <input id="firstday" oninput="previewNewArchive();" type="date" style="font-size: xx-large; border: 2px solid white;"></label><br>
<label>Letzter Tag: <input id="lastday" oninput="previewNewArchive();" type="date" style="font-size: xx-large; border: 2px solid white;"></label><br>
Vorschau<br>
Dateiname: <span id="archive_filename_preview"></span><br>
Linktext: <span id="archive_link_preview"></span><br>
Inhalt: <span class="sentences" id="archive_sentences_preview"></span><br>
<button onclick="createArchive();">erstellen</button>
</div>
<div>
<h3>Text bearbeiten</h3>
<div class="sentences" id="edit_sentences_preview"></div>
<textarea id="edit_sentences_textarea" name="textarea" rows="25" style="width: 90%; font: inherit; background: transparent; border: 2px solid white; color: inherit;" oninput="refreshTextArea();"></textarea>
<button onclick="saveEditedSentences();">speichern</button>
</div>
<?php elseif (file_exists('data/admin_pwd')): ?>
Anmelden<br>
<form action="./admin.php" method="post">
<label>Passwort: <input type="password" required name="pwd" style="font-size: xx-large; border: 2px solid white;"></label>
<button>Anmelden</button>
</form>
<?php
endif;
?>
</p>
<script>
var sentences;
function fetchSentences() {
fetch("./data/sentences.txt", {headers: {"Cache-Control": "no-cache, no-store"}})
.then((response) => {
return response.text().then((text) => {
sentences = text;
});
});
}
function previewNewArchive() {
month_names = ["Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"];
month_abbr = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"];
fd = document.getElementById("firstday").valueAsDate;
ld = document.getElementById("lastday").valueAsDate;
filename_preview = document.getElementById("archive_filename_preview");
link_preview = document.getElementById("archive_link_preview");
sentences_preview = document.getElementById("archive_sentences_preview");
var display_first_month = (fd.getMonth() != ld.getMonth()) || (fd.getFullYear() != ld.getFullYear());
var display_first_year = fd.getFullYear() != ld.getFullYear();
filename_preview.innerHTML = `sentences-archive-${fd.getDate()}${display_first_month ? '-'+month_abbr[fd.getMonth()] : ''}${display_first_year ? '-'+fd.getFullYear()%100 : ''}-${ld.getDate()}-${month_abbr[ld.getMonth()]}-${ld.getFullYear()%100}.txt`;
link_preview.innerHTML = `Archiveintrag ${fd.getDate()}. ${display_first_month ? month_names[fd.getMonth()] : ''} ${display_first_year ? fd.getFullYear() : ''} \
bis ${ld.getDate()}. ${month_names[ld.getMonth()]} ${ld.getFullYear()}`;
fetchSentences();
var htmltxt = document.createElement("html");
htmltxt.innerHTML = sentences;
spans = htmltxt.getElementsByTagName("span");
sentences_preview.innerHTML = '';
for (i=0; i<3; i++) {
sentences_preview.appendChild(spans[i].cloneNode(true));
}
sentences_preview.innerHTML += "<span style='color: white'>...</span>";
for (i=3; i>0; i--) {
sentences_preview.appendChild(spans[spans.length - i].cloneNode(true));
}
}
function createArchive() {
var data = new FormData();
data.append('filename', encodeURI(document.getElementById("archive_filename_preview").innerText));
data.append('linktext', encodeURI(document.getElementById("archive_link_preview").innerText));
const Http = new XMLHttpRequest();
Http.open('POST', 'archive_creator.php');
Http.onreadystatechange=(e)=>{
location.reload();
}
Http.send(data);
}
function saveEditedSentences() {
var data = new FormData();
data.append('sentences', encodeURI(document.getElementById("edit_sentences_textarea").value));
const Http = new XMLHttpRequest();
Http.open('POST', 'sentences_saver.php');
Http.onreadystatechange=(e)=>{
location.reload();
}
Http.send(data);
}
function defaultNewArchive() {
try {
lastlink = document.getElementById("archive_links").lastElementChild.getAttribute("href");
} catch(err) {
// last link not found, perhaps there is no archive yet
lastlink = "sentences-archive-8-aug-00.txt";
}
var re = /(\d+)-(\w\w\w)-(\d+)\.txt/;
res = re.exec(lastlink);
month_abbr = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"];
d = parseInt(res[1]);
m = month_abbr.indexOf(res[2]);
y = 2000 + parseInt(res[3]);
date = new Date(Date.UTC(y, m, d));
document.getElementById("firstday").value = date.toISOString().split("T")[0];
document.getElementById("lastday").valueAsNumber = Date.now()
}
function refreshTextArea() {
document.getElementById("edit_sentences_preview").innerHTML = document.getElementById("edit_sentences_textarea").value;
}
fetchSentences();
setTimeout(previewNewArchive, 500);
defaultNewArchive();
setTimeout(function () {
document.getElementById("edit_sentences_preview").innerHTML = sentences;
document.getElementById("edit_sentences_textarea").value = sentences;
}, 500);
</script>
</body>
</html>