258 lines
		
	
	
		
			10 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			258 lines
		
	
	
		
			10 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
require_once 'vendor/autoload.php';
 | 
						|
use ICal\ICal;
 | 
						|
static $DATE_FORMAT = 'd.m.Y H:i';
 | 
						|
static $VISITORS_FILE = 'C:\\Users\\Christopher Teutsch\\dorf_visitors.txt';
 | 
						|
static $DORF_IN_LOCKDOWN = false;
 | 
						|
static $DORF_VIRTUAL_EVENTS = true;
 | 
						|
static $ICAL_URL = 'https://chaosdorf.de/~derf/cccd_all.ics';
 | 
						|
static $ICAL_STALE_SECONDS = 60 * 15;
 | 
						|
static $ICAL_CACHE_FILE = 'C:\\Users\\Christopher Teutsch\\cccd_all.ics';
 | 
						|
//static $STATE_FILE = '/media/sg/dorf.jetzt_state';
 | 
						|
static $STATE_FILE = 'C:\\Users\\Christopher Teutsch\\dorf_state.txt';
 | 
						|
static $STATE_STALE_SECONDS = 60;
 | 
						|
if ($DORF_VIRTUAL_EVENTS || ! $DORF_IN_LOCKDOWN){
 | 
						|
    $ical = new ICal(false, array(
 | 
						|
            'defaultSpan' => 2,
 | 
						|
            'defaultTimeZone' => 'Europe/Berlin',
 | 
						|
            'defaultWeekStart' => 'MO',
 | 
						|
            'filterDaysBefore' => '1',
 | 
						|
        ));
 | 
						|
    if (is_file($ICAL_CACHE_FILE)){
 | 
						|
        error_log('is file');
 | 
						|
        $mtime = filemtime($ICAL_CACHE_FILE);
 | 
						|
        $time_diff = time() - $mtime;
 | 
						|
        error_log(sprintf('%d',$time_diff));
 | 
						|
        if ($time_diff >= $ICAL_STALE_SECONDS){
 | 
						|
            $data = file_get_contents($ICAL_URL);
 | 
						|
            file_put_contents($ICAL_CACHE_FILE);
 | 
						|
            $ical->initString($data);
 | 
						|
        }
 | 
						|
        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{
 | 
						|
	global $DATE_FORMAT, $ical;
 | 
						|
	$startdate_loop = $ical->iCalDateToDateTime($event->dtstart_array[3]);
 | 
						|
	$startdate_str = $startdate_loop->format($DATE_FORMAT);
 | 
						|
	$interval_loop = new DateInterval($event->duration);
 | 
						|
	if ($interval_loop->d != 0 || $interval_loop->h >= 24){
 | 
						|
		$enddate_str = $startdate_loop->add($interval_loop)->format($DATE_FORMAT);
 | 
						|
	}else{
 | 
						|
		$enddate_str = $startdate_loop->add($interval_loop)->format('H:i');
 | 
						|
	}
 | 
						|
	return $startdate_str . ' – ' . $enddate_str; 
 | 
						|
}
 | 
						|
$v = file_get_contents($VISITORS_FILE);
 | 
						|
?>
 | 
						|
<!DOCTYPE html>
 | 
						|
<html lang="de">
 | 
						|
<head>
 | 
						|
	<title>Was geht im Dorf.jetzt?</title>
 | 
						|
	<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
 | 
						|
	<link rel="stylesheet" href="assets/css/bootstrap.min.css" crossorigin="anonymous">
 | 
						|
	<meta charset="UTF-8" />
 | 
						|
</head>
 | 
						|
<body>
 | 
						|
	<div class="container">
 | 
						|
		<nav class="navbar">	
 | 
						|
			<h1 class="text-center">Was geht im Dorf.jetzt?</h1><hr />
 | 
						|
		</nav>
 | 
						|
		<?php if($DORF_IN_LOCKDOWN): ?>
 | 
						|
		<div class="card mb-3 text-center">
 | 
						|
			<div class="card-body">
 | 
						|
				<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;" />
 | 
						|
				<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">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>
 | 
						|
 | 
						|
		<?php else: ?>
 | 
						|
		<div class="card mb-3 text-center">
 | 
						|
			<div class="card-body">
 | 
						|
				<h2 class="card-title">Türstatus</h2>
 | 
						|
				<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"><?= $fmt->state_string ?></p>
 | 
						|
					<div class="btn-wrapper">
 | 
						|
						<a href="https://wiki.chaosdorf.de/Raumstatus" class="btn btn-lg btn-block btn-primary">What's this?</a>
 | 
						|
					</div>
 | 
						|
			</div>
 | 
						|
		</div>
 | 
						|
		<?php endif ?>
 | 
						|
		<?php if($DORF_VIRTUAL_EVENTS || !$DORF_IN_LOCKDOWN): ?>
 | 
						|
		<div class="card mb-3 text-center">
 | 
						|
			<div class="card-body">
 | 
						|
				<h2 class="card-title">Events</h2>
 | 
						|
				<?php if (!empty($events)) : ?>
 | 
						|
				<h3 class="card-subtitle mb-2 text-muted">Nächste Veranstaltung</h3>
 | 
						|
				<p class="card-text">
 | 
						|
					<h4 class="font-weight-bold"><?= htmlspecialchars($first_event->summary, ENT_QUOTES, 'UTF-8') ?></h4>
 | 
						|
					<div class="font-weight-normal"><?= format_event($first_event) ?></div>
 | 
						|
					<div class="font-weight-normal"><a href="<?= $first_event->url ?>">Details zur Veranstaltung</a></div>
 | 
						|
				</p>
 | 
						|
				<h3 class="card-title">Danach:</h3>
 | 
						|
				<table class="table">
 | 
						|
					<tbody>
 | 
						|
					<?php foreach ($events as $event) : ?>
 | 
						|
						<tr>
 | 
						|
							<th scope="row"><?=format_event($event)?></th>
 | 
						|
							<td><?= htmlspecialchars($event->summary) ?></td>
 | 
						|
							<td><a href="<?= $first_event->url ?>">Details</a></td>
 | 
						|
						</tr>
 | 
						|
					<?php endforeach ?>
 | 
						|
					</tbody>
 | 
						|
				</table>
 | 
						|
				<a href="https://wiki.chaosdorf.de/Chaosdorf_Wiki:Current_events" class="btn btn-lg btn-block btn-primary">Event-Kalender</a>
 | 
						|
				<?php else: ?>
 | 
						|
				<h4 class="card-subtitle mb-2 text-muted">Aktuell keine Veranstaltungen.</h4>
 | 
						|
				<p class="card-text">
 | 
						|
					<span class="font-weight-normal">
 | 
						|
						Es stehen aktuell keine Veranstaltungen an.
 | 
						|
					</span>
 | 
						|
				</p>
 | 
						|
				<?php endif ?>
 | 
						|
			</div>
 | 
						|
		</div>
 | 
						|
		<?php endif ?>
 | 
						|
	</div>
 | 
						|
	<footer class="page-footer font-small text-center">
 | 
						|
    	<div>
 | 
						|
        Du bist Besucher #<?= $v ?>
 | 
						|
        </div>
 | 
						|
		<div>
 | 
						|
		<a href="https://git.iwonder.name/iwonder/dorf.jetzt">Sieh dir den Code hier an</a>
 | 
						|
		</div>
 | 
						|
		<div>
 | 
						|
		Melde Fehler bitte an <a href="mailto:help@dorf.jetzt">help@dorf.jetzt</a>
 | 
						|
		</div>
 | 
						|
        <div>
 | 
						|
        Dies ist kein offizieller Auftritt des <a href="https://chaosdorf.de">Chaos Computer Club Düsseldorf / Chaosdorf e.V.</a>.
 | 
						|
        </div>
 | 
						|
	</footer>
 | 
						|
    <?php
 | 
						|
    // These are actually not needed, see https://getbootstrap.com/docs/4.0/getting-started/introduction/#js
 | 
						|
	// <script src="assets/js/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
 | 
						|
	// <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
 | 
						|
	// <script src="assets/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
 | 
						|
    ?>
 | 
						|
</body>
 | 
						|
</html>
 | 
						|
<?php
 | 
						|
if(!$v) $v=0;
 | 
						|
$v++;
 | 
						|
file_put_contents($VISITORS_FILE, $v);
 |