Compare commits

...

1 Commits

Author SHA1 Message Date
be765ed6b7 wip 2020-04-28 16:07:36 +02:00
3 changed files with 209 additions and 78 deletions

View File

@ -1,5 +1,6 @@
{ {
"require": { "require": {
"johngrogg/ics-parser": "^2.1.17" "johngrogg/ics-parser": "^2.1.17",
"twbs/bootstrap": "4.3.1"
} }
} }

60
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "1852786d46444b665586f9455a5cd888", "content-hash": "bdb9c2cf3bf1bc1e83193080c7d79b4b",
"packages": [ "packages": [
{ {
"name": "johngrogg/ics-parser", "name": "johngrogg/ics-parser",
@ -324,6 +324,61 @@
"standards" "standards"
], ],
"time": "2019-11-18T17:27:11+00:00" "time": "2019-11-18T17:27:11+00:00"
},
{
"name": "twbs/bootstrap",
"version": "v4.3.1",
"source": {
"type": "git",
"url": "https://github.com/twbs/bootstrap.git",
"reference": "8fa0d3010112dca5dd6dd501173415856001ba8b"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/twbs/bootstrap/zipball/8fa0d3010112dca5dd6dd501173415856001ba8b",
"reference": "8fa0d3010112dca5dd6dd501173415856001ba8b",
"shasum": ""
},
"replace": {
"twitter/bootstrap": "self.version"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "3.3.x-dev"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Jacob Thornton",
"email": "jacobthornton@gmail.com"
},
{
"name": "Mark Otto",
"email": "markdotto@gmail.com"
}
],
"description": "The most popular front-end framework for developing responsive, mobile first projects on the web.",
"homepage": "https://getbootstrap.com/",
"keywords": [
"JS",
"css",
"framework",
"front-end",
"mobile-first",
"responsive",
"sass",
"web"
],
"support": {
"issues": "https://github.com/twbs/bootstrap/issues",
"source": "https://github.com/twbs/bootstrap/tree/v4.3.1"
},
"time": "2019-02-13T16:01:40+00:00"
} }
], ],
"packages-dev": [], "packages-dev": [],
@ -333,5 +388,6 @@
"prefer-stable": false, "prefer-stable": false,
"prefer-lowest": false, "prefer-lowest": false,
"platform": [], "platform": [],
"platform-dev": [] "platform-dev": [],
"plugin-api-version": "2.0.0"
} }

224
index.php
View File

@ -2,72 +2,147 @@
require_once 'vendor/autoload.php'; require_once 'vendor/autoload.php';
use ICal\ICal; use ICal\ICal;
static $DATE_FORMAT = 'd.m.Y H:i'; static $DATE_FORMAT = 'd.m.Y H:i';
static $VISITORS_FILE = '/media/sg/dorf.jetzt_visitors'; static $VISITORS_FILE = 'C:\\Users\\Christopher Teutsch\\dorf_visitors.txt';
static $DORF_IN_LOCKDOWN = true; static $DORF_IN_LOCKDOWN = false;
static $DORF_VIRTUAL_EVENTS = true; static $DORF_VIRTUAL_EVENTS = true;
static $ICAL_URL = 'https://chaosdorf.de/~derf/cccd_all.ics'; static $ICAL_URL = 'https://chaosdorf.de/~derf/cccd_all.ics';
$state_map = array( static $ICAL_STALE_SECONDS = 60 * 15;
'closed' => (object) array( static $ICAL_CACHE_FILE = 'C:\\Users\\Christopher Teutsch\\cccd_all.ics';
'state_string' => 'Das Dorf ist gerade <em>geschlossen</em>.', //static $STATE_FILE = '/media/sg/dorf.jetzt_state';
'svg' => 'lock', static $STATE_FILE = 'C:\\Users\\Christopher Teutsch\\dorf_state.txt';
'color' => 'red', static $STATE_STALE_SECONDS = 60;
),
'maybe_open' => (object) array(
'state_string' => 'Das Dorf ist gerade <em>vielleicht geöffnet</em>: </p><p>Der Clubraum ist offen, aber es findet keine Veranstaltung statt.</p><p>
Der Status kann sich also kurzfristig ändern.',
'svg' => 'done',
'color' => 'brown',
),
'open' => (object) array(
'state_string' => 'Das Dorf ist gerade <em>geöffnet</em>.</p><p>
Komm gerne vorbei.',
'svg' => 'done',
'color' => 'green',
),
'private' => (object) array(
'state_string' => 'Das Dorf ist gerade <em>privat</em>: </p><p>Es sind Leute da, aber der Clubraum ist nicht geöffnet.</p><p>
Komm gerne vorbei (aber frag lieber vorher, wie lange noch Leute da sind).',
'svg' => 'lock',
'color' => 'fdd835',
),
'unknown' => (object) array(
'state_string' => 'Der Status vom Dorf ist gerade <em>unbekannt</em>',
'svg' => 'warning',
'color' => 'orange',
),
'error' => (object) array(
'state_string' => 'Der Server konnte den Status vom Dorf nicht abrufen.',
'svg' => 'error',
'color' => 'blue',
),
);
$hash_to_state = array(
'bff0167ed8aba031c49122ef4046cf1b' => 'closed',
'd8ec899c69283bc775952a767db9d5f5' => 'maybe_open',
'2c2672c641425e5b2acd6ee74f39ae60' => 'open',
'66aece8ae27ffd3a656d42005fa3efbd' => 'private',
'86c75c0ad413b06ff8291673162d0b64' => 'unknown',
'0' => 'error',
);
if ($DORF_VIRTUAL_EVENTS || ! $DORF_IN_LOCKDOWN){ if ($DORF_VIRTUAL_EVENTS || ! $DORF_IN_LOCKDOWN){
$img = file_get_contents('https://chaosdorf.de/raumstatus/status.png'); $ical = new ICal(false, array(
$hash = md5($img); 'defaultSpan' => 2,
$state = $hash_to_state[$hash]; 'defaultTimeZone' => 'Europe/Berlin',
$ical = new ICal(false, array( 'defaultWeekStart' => 'MO',
'defaultSpan' => 2, 'filterDaysBefore' => '1',
'defaultTimeZone' => 'Europe/Berlin', ));
'defaultWeekStart' => 'MO', if (is_file($ICAL_CACHE_FILE)){
'filterDaysBefore' => '1', error_log('is file');
)); $mtime = filemtime($ICAL_CACHE_FILE);
$ical->initUrl($ICAL_URL, $acceptLanguage = 'de'); $time_diff = time() - $mtime;
$events = $ical->eventsFromInterval('2 week'); error_log(sprintf('%d',$time_diff));
$first_event = $events[0]; if ($time_diff >= $ICAL_STALE_SECONDS){
$events = array_slice($events, 1); $data = file_get_contents($ICAL_URL);
$locale = locale_accept_from_http($_SERVER['HTTP_ACCEPT_LANGUAGE']); file_put_contents($ICAL_CACHE_FILE);
if ($locale !== null){ $ical->initString($data);
setlocale(LC_TIME, $locale); }
else {
$ical->initFile()
}
// Else get state from network every time (inefficient!)
} else {
$state = get_state('https://chaosdorf.de/raumstatus/status.png');
}
if ()
$ical->initUrl($ICAL_URL, $acceptLanguage = 'de');
$events = $ical->eventsFromInterval('2 week');
$first_event = $events[0];
$events = array_slice($events, 1);
} }
/**
* @param $url
* @return int
*/
function get_state($url): int
{
$hash = hash('crc32b', file_get_contents($url));
switch($hash){
case '1ac9394d':
return State::STATE_UNKNOWN;
case 'bf48d8e5':
return State::STATE_OPEN;
case 'beab4306':
return State::STATE_MAYBE_OPEN;
case '4e7e398a':
return State::STATE_CLOSED;
case '5503654a':
return State::STATE_PRIVATE;
default:
error_log('Default state called in get_state(): hash '. $hash);
return State::STATE_ERROR;
}
} }
abstract class State{ // php doesn't have enums
const STATE_CLOSED = 0;
const STATE_OPEN = 1;
const STATE_MAYBE_OPEN = 2;
const STATE_PRIVATE = 3;
const STATE_UNKNOWN = 4;
const STATE_ERROR = -1;
}
class FormattingInfo{
function __construct($state)
{
switch($state){
case State::STATE_CLOSED:
$this->color = 'red';
$this->svg = 'lock';
$this->state_string = 'Das Dorf ist gerade <em>geschlossen</em>.';
$this->img_alt = 'geschlossen';
break;
case State::STATE_MAYBE_OPEN:
$this->color = 'brown';
$this->svg = 'done';
$this->state_string = 'Das Dorf ist gerade <em>vielleicht geöffnet</em>: </p><p>Der Clubraum ist offen, aber es findet keine Veranstaltung statt.</p><p>
Der Status kann sich also kurzfristig ändern.';
$this->img_alt = 'vielleicht geöffnet';
break;
case State::STATE_OPEN:
$this->color = 'green';
$this->svg = 'done';
$this->state_string = 'Das Dorf ist gerade <em>geöffnet</em>.</p><p>
Komm gerne vorbei.';
$this->img_alt = 'geöffnet';
break;
case State::STATE_PRIVATE:
$this->color = 'fdd835';
$this->svg = 'lock';
$this->state_string = 'Das Dorf ist gerade <em>privat</em>: </p><p>Es sind Leute da, aber der Clubraum ist nicht geöffnet.</p><p>
Komm gerne vorbei (aber frag lieber vorher, wie lange noch Leute da sind).';
$this->img_alt = 'privat';
break;
case State::STATE_UNKNOWN:
$this->color = 'orange';
$this->svg = 'warning';
$this->state_string = 'Der Status vom Dorf ist gerade <em>unbekannt</em>';
$this->img_alt = 'unbekannt';
case State::STATE_ERROR:
$this->color = 'blue';
$this->svg = 'error';
$this->state_string = 'Der Server konnte den Status vom Dorf nicht ermitteln. Sorry.';
$this->img_alt = 'konnte nicht abgerufen werden';
break;
}
}
public $svg;
public $img_alt;
public $state_string;
public $color;
}
if (! $DORF_IN_LOCKDOWN){
// Try to acquire state locally, but *only* if STATE_FILE exists
if (is_file($STATE_FILE)){
error_log('is file');
$mtime = filemtime($STATE_FILE);
$time_diff = time() - $mtime;
error_log(sprintf('%d',$time_diff));
if ($time_diff >= $STATE_STALE_SECONDS){
$state = get_state('https://chaosdorf.de/raumstatus/status.png');
$result = file_put_contents($STATE_FILE, sprintf('%d',$state));
}
else {
$state = sscanf(file_get_contents($STATE_FILE),'%d')[0];
}
// Else get state from network every time (inefficient!)
} else {
$state = get_state('https://chaosdorf.de/raumstatus/status.png');
}
$fmt = new FormattingInfo($state);
}
function format_event($event): string{ function format_event($event): string{
global $DATE_FORMAT, $ical; global $DATE_FORMAT, $ical;
$startdate_loop = $ical->iCalDateToDateTime($event->dtstart_array[3]); $startdate_loop = $ical->iCalDateToDateTime($event->dtstart_array[3]);
@ -87,8 +162,7 @@ $v = file_get_contents($VISITORS_FILE);
<head> <head>
<title>Was geht im Dorf.jetzt?</title> <title>Was geht im Dorf.jetzt?</title>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<link rel="stylesheet" href="assets/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous"> <link rel="stylesheet" href="assets/css/bootstrap.min.css" crossorigin="anonymous">
<link href="assets/font/Material-Icons.css" rel="stylesheet">
<meta charset="UTF-8" /> <meta charset="UTF-8" />
</head> </head>
<body> <body>
@ -99,20 +173,20 @@ $v = file_get_contents($VISITORS_FILE);
<?php if($DORF_IN_LOCKDOWN): ?> <?php if($DORF_IN_LOCKDOWN): ?>
<div class="card mb-3 text-center"> <div class="card mb-3 text-center">
<div class="card-body"> <div class="card-body">
<h5 class="card-title">Das Chaosdorf ist geschlossen</h5> <h2 class="card-title">Das Chaosdorf ist geschlossen</h2>
<img src="assets/svg/ic_lock_48px.svg" style="filter:url(assets/css/filter.svg#red);height:16rem;width:auto;" /> <img src="assets/svg/ic_lock_48px.svg" style="filter:url(assets/css/filter.svg#red);height:16rem;width:auto;" />
<p class="card-text font-weight-normal">Das Chaosdorf ist aufgrund eines Beschlusses des Vereins geschlossen.</p> <p class="card-text font-weight-normal">Das Chaosdorf ist aufgrund eines Beschlusses des Vereins geschlossen.</p>
<p class="card-text font-weight-normal">Das gilt <span class="font-weight-bold">bis auf Weiteres</span>.</p> <p class="card-text font-weight-normal">Das gilt <span class="font-weight-bold">bis auf Weiteres</span>.</p>
<p class="card-text font-weight-normal">Natürlich hört das Chaosdorfleben nicht auf, <span class="font-weight-bold">kommt uns gerne im <a href="https://wiki.chaosdorf.de/VirtualSpace">VirtualSpace</a> besuchen.</span></p> <p class="card-text font-weight-normal">Natürlich hört das Chaosdorfleben nicht auf, <span class="font-weight-bold">kommt uns gerne im <a href="https://wiki.chaosdorf.de/VirtualSpace">VirtualSpace</a> besuchen.</span></p>
</div> </div>
</div> </div>
<?php else: ?> <?php else: ?>
<div class="card mb-3 text-center"> <div class="card mb-3 text-center">
<div class="card-body"> <div class="card-body">
<h5 class="card-title">Türstatus</h5> <h2 class="card-title">Türstatus</h2>
<img src="assets/svg/ic_<?= $state_map[$state]->svg ?>_48px.svg" style="filter:url(assets/css/filter.svg#<?= $state_map[$state]->color ?>);height:16rem;width:auto;" /> <img alt="<?= $fmt->img_alt ?>" src="assets/svg/ic_<?= $fmt->svg ?>_48px.svg" style="filter:url(assets/css/filter.svg#<?= $fmt->color ?>);height:16rem;width:auto;" />
<p class="card-text"><?= $state_map[$state]->state_string ?></p> <p class="card-text"><?= $fmt->state_string ?></p>
<div class="btn-wrapper"> <div class="btn-wrapper">
<a href="https://wiki.chaosdorf.de/Raumstatus" class="btn btn-lg btn-block btn-primary">What's this?</a> <a href="https://wiki.chaosdorf.de/Raumstatus" class="btn btn-lg btn-block btn-primary">What's this?</a>
</div> </div>
@ -122,15 +196,15 @@ $v = file_get_contents($VISITORS_FILE);
<?php if($DORF_VIRTUAL_EVENTS || !$DORF_IN_LOCKDOWN): ?> <?php if($DORF_VIRTUAL_EVENTS || !$DORF_IN_LOCKDOWN): ?>
<div class="card mb-3 text-center"> <div class="card mb-3 text-center">
<div class="card-body"> <div class="card-body">
<h5 class="card-title">Events</h5> <h2 class="card-title">Events</h2>
<?php if (!empty($events)) : ?> <?php if (!empty($events)) : ?>
<h6 class="card-subtitle mb-2 text-muted">Nächste Veranstaltung</h6> <h3 class="card-subtitle mb-2 text-muted">Nächste Veranstaltung</h3>
<p class="card-text"> <p class="card-text">
<h5 class="font-weight-bold"><?= htmlspecialchars($first_event->summary, ENT_QUOTES, 'UTF-8') ?></h5> <h4 class="font-weight-bold"><?= htmlspecialchars($first_event->summary, ENT_QUOTES, 'UTF-8') ?></h4>
<span class="font-weight-normal"><?= format_event($first_event) ?></span> <div class="font-weight-normal"><?= format_event($first_event) ?></div>
<span class="font-weight-normal"><a href="<?= $first_event->url ?>">Details zur Veranstaltung</a></span> <div class="font-weight-normal"><a href="<?= $first_event->url ?>">Details zur Veranstaltung</a></div>
</p> </p>
<h5 class="card-title">Danach:</h5> <h3 class="card-title">Danach:</h3>
<table class="table"> <table class="table">
<tbody> <tbody>
<?php foreach ($events as $event) : ?> <?php foreach ($events as $event) : ?>
@ -144,7 +218,7 @@ $v = file_get_contents($VISITORS_FILE);
</table> </table>
<a href="https://wiki.chaosdorf.de/Chaosdorf_Wiki:Current_events" class="btn btn-lg btn-block btn-primary">Event-Kalender</a> <a href="https://wiki.chaosdorf.de/Chaosdorf_Wiki:Current_events" class="btn btn-lg btn-block btn-primary">Event-Kalender</a>
<?php else: ?> <?php else: ?>
<h6 class="card-subtitle mb-2 text-muted">Aktuell keine Veranstaltungen.</h6> <h4 class="card-subtitle mb-2 text-muted">Aktuell keine Veranstaltungen.</h4>
<p class="card-text"> <p class="card-text">
<span class="font-weight-normal"> <span class="font-weight-normal">
Es stehen aktuell keine Veranstaltungen an. Es stehen aktuell keine Veranstaltungen an.