152 lines
4.3 KiB
PHP
152 lines
4.3 KiB
PHP
<?php
|
|
|
|
function get_color_from_ip($ip) {
|
|
$ip_elem = explode(".", $ip);
|
|
$css = "background-color: hsl(";
|
|
$number = abs(crc32($ip));
|
|
$css .= ($number % 360).", ";
|
|
$css .= (50 + (($number >> 3) % 51))."%, ";
|
|
$css .= (50 + (($number >> 5) % 51))."%);";
|
|
|
|
return $css;
|
|
}
|
|
|
|
//header("refresh: 28");
|
|
header("Cache-Control: no-cache, no-store, must-revalidate");
|
|
|
|
if (array_key_exists("submitted", $_REQUEST)) {
|
|
$word = urldecode($_REQUEST["submitted"]);
|
|
//print("Jemand schreibt ".$word."<br>");
|
|
$sentences = fopen("./data/sentences.txt", "a") or die("unable to open sentences file");
|
|
$content = "<span style='";
|
|
$content .= get_color_from_ip($_SERVER['REMOTE_ADDR'])."'>";
|
|
|
|
$ends_of_sentence = ['.', '!', '?'];
|
|
if (preg_match("/\p{L}/", $word[0])) { // check if first character is letter (including ä, ê, ß stuff)
|
|
$content .= " ";
|
|
}
|
|
$content .= htmlentities($word);
|
|
|
|
$content .= "</span>";
|
|
|
|
if (in_array($word[-1], $ends_of_sentence)) {
|
|
$content .= "<br>\n";
|
|
}
|
|
|
|
fwrite($sentences, $content);
|
|
fclose($sentences);
|
|
header("location: ".htmlentities($_SERVER['PHP_SELF']));
|
|
}
|
|
|
|
?>
|
|
|
|
<html>
|
|
<head>
|
|
<title>One Word Each</title>
|
|
<link rel="stylesheet" href="style.css">
|
|
<link rel="icon" href="favicon.ico" type="image/x-icon">
|
|
</head>
|
|
<body>
|
|
<table>
|
|
<tr>
|
|
<td width="50%"><img src="OWE_Logo.png" width="90%"></td>
|
|
<td><h1>One Word Each Seite</h1></td>
|
|
</tr>
|
|
<table>
|
|
|
|
<p>
|
|
<a href="./archiv.php?file=sentences-archive-30-mar-20.txt">Archiveintrag 30. März 2020</a><br>
|
|
<a href="./archiv.php?file=sentences-archive-31-mar-9-apr-20.txt">Archiveintrag 31. März bis 9. April 2020</a><br>
|
|
<a href="./archiv.php?file=sentences-archive-9-28-apr-20.txt">Archiveintrag 9. bis 28. April 2020</a>
|
|
</p>
|
|
|
|
<p class="sentences">
|
|
<?php
|
|
//print(file_get_contents("./data/sentences.txt"));
|
|
?>
|
|
</p>
|
|
<button onclick="onReloadClicked()" id="but-reload">aktualisieren</button>
|
|
<br><br>
|
|
<form action="./#wordform" method="post" autocomplete="off" id="wordform">
|
|
<table>
|
|
<tr> <td>Nächstes Wort:</td> <td><input type="text" name="submitted"></td> </tr>
|
|
<tr> <td></td> <td><input type="submit" value="senden"></td> </tr>
|
|
</table>
|
|
</form>
|
|
|
|
<script>
|
|
var previousTextLength;
|
|
|
|
function loadSentences(alertIfNew = false) {
|
|
fetch("./data/sentences.txt", {headers: {"Cache-Control": "no-cache, no-store"}})
|
|
.then((response) => {
|
|
return response.text().then((text) => {
|
|
document.querySelector(".sentences").innerHTML = text;
|
|
if (alertIfNew && text.length > previousTextLength) {
|
|
var newtxt = text.substring(previousTextLength, text.length);
|
|
//var parser = new DOMParser();
|
|
//var xmltxt = parser.parseFromString(newtxt, "text/xml");
|
|
//var newword = xmltxt.getElementsByTagName("span")[0].innerHTML.trim();
|
|
var htmltxt = document.createElement("html");
|
|
htmltxt.innerHTML = newtxt;
|
|
var newword = htmltxt.getElementsByTagName("span")[0].innerHTML.trim();
|
|
if (Notification.permission == 'granted') {
|
|
displayNotification(newword);
|
|
}
|
|
else {
|
|
alert("Neues Wort: " + newword);
|
|
}
|
|
}
|
|
previousTextLength = text.length;
|
|
});
|
|
});
|
|
}
|
|
|
|
function onReloadClicked() {
|
|
loadSentences();
|
|
document.querySelector("#but-reload").style.visibility = "hidden";
|
|
setTimeout(function(){
|
|
document.querySelector("#but-reload").style.visibility = "initial";
|
|
}, 100);
|
|
}
|
|
|
|
function displayNotification(word) {
|
|
if (Notification.permission == 'granted') {
|
|
navigator.serviceWorker.getRegistration().then(function(reg) {
|
|
reg.showNotification("Neues Wort auf OWE", { body: word, icon: "OWE_Logo.png", requireInteraction: true });
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
//setTimeout(loadSentences, 500);
|
|
loadSentences();
|
|
setInterval(loadSentences, 5000, true);
|
|
|
|
if ('serviceWorker' in navigator) {
|
|
navigator.serviceWorker.register('./service-worker.js')
|
|
.then(function(registration) {
|
|
console.log('Registration successful, scope is:', registration.scope);
|
|
})
|
|
.catch(function(error) {
|
|
console.log('Service worker registration failed, error:', error);
|
|
});
|
|
}
|
|
|
|
navigator.serviceWorker.register('./service-worker.js', {
|
|
scope: './'
|
|
});
|
|
|
|
navigator.serviceWorker.register('./service-worker.js', {
|
|
scope: '.'
|
|
});
|
|
|
|
Notification.requestPermission(function(status) {
|
|
console.log('Notification permission status:', status);
|
|
});
|
|
|
|
|
|
</script>
|
|
</body>
|
|
</html>
|