Move ICal module to include folder, export config to separate config file
This commit is contained in:
parent
e1fe24e6af
commit
3fcbd8e16f
5 changed files with 121 additions and 103 deletions
94
index.php
94
index.php
|
|
@ -1,72 +1,66 @@
|
|||
<?php
|
||||
|
||||
#ini_set('display_errors', 1);
|
||||
#ini_set('display_startup_errors', 1);
|
||||
#error_reporting(E_ALL);
|
||||
|
||||
|
||||
# config
|
||||
$ical = 'https://cloud.hacknang.de/remote.php/dav/public-calendars/nZTMSHpd29ZRpAr6/?export';
|
||||
$cachefile = 'cache/hacknang.ics';
|
||||
$cachetime = 120; // 2 min
|
||||
|
||||
# requirements
|
||||
require_once 'ICal/ICal.php';
|
||||
require_once 'ICal/Event.php';
|
||||
require_once 'include/config.php';
|
||||
require_once 'include/ICal/ICal.php';
|
||||
require_once 'include/ICal/Event.php';
|
||||
|
||||
# Fix relative path
|
||||
$cachefile = 'cache/'.$cachefile;
|
||||
|
||||
# Init Calendar
|
||||
$iCal = new \ICal\ICal();
|
||||
$ical = new \ICal\ICal();
|
||||
|
||||
# Caching and load calendar
|
||||
if(@filemtime($cachefile) + $cachetime < time()) {
|
||||
$ical_str = file_get_contents($ical);
|
||||
file_put_contents($cachefile, $ical_str);
|
||||
$iCal->initString($ical_str);
|
||||
if (!file_exists($cachefile) || (filemtime($cachefile) + $cachetime < time())) {
|
||||
$context = stream_context_create(['http' => ['timeout' => 10]]);
|
||||
$ical_str = @file_get_contents($ical_url, false, $context);
|
||||
|
||||
if ($ical_str !== false && strpos($ical_str, 'BEGIN:VCALENDAR') !== false) {
|
||||
file_put_contents($cachefile, $ical_str);
|
||||
$ical->initString($ical_str);
|
||||
} else {
|
||||
if (file_exists($cachefile)) $ical->initFile($cachefile);
|
||||
}
|
||||
} else {
|
||||
$iCal->initFile($cachefile);
|
||||
$ical->initFile($cachefile);
|
||||
}
|
||||
//$iCal->initURL($ical);
|
||||
|
||||
# Load calendar entries
|
||||
$months = max(filter_input(INPUT_GET, 'period', FILTER_VALIDATE_INT, array('options' => array('min_range' => 1, 'max_range' => 12))), 1);
|
||||
$events = $iCal->sortEventsWithOrder($iCal->eventsFromInterval($months.' month'));
|
||||
|
||||
$filter = filter_input(INPUT_GET, 'filter', FILTER_SANITIZE_SPECIAL_CHARS);
|
||||
$period_val = filter_input(INPUT_GET, 'period', FILTER_VALIDATE_INT, ['options' => ['min_range' => 1, 'max_range' => 12]]) ?: 1;
|
||||
$filter = filter_input(INPUT_GET, 'filter', FILTER_SANITIZE_SPECIAL_CHARS); // FILTER_UNSAFE_RAW
|
||||
|
||||
$events = $ical->sortEventsWithOrder($ical->eventsFromInterval($period_val . ' month'));
|
||||
$result = [];
|
||||
|
||||
foreach ($events as $event) {
|
||||
$cat = $event->categories ?? '';
|
||||
|
||||
if ($filter && strpos($event->categories, $filter) === false) {
|
||||
continue;
|
||||
}
|
||||
if ($filter && stripos($cat, $filter) === false) continue;
|
||||
if (stripos($cat, "hidden") !== false) continue;
|
||||
|
||||
if (isset($event->categories) && strpos($event->categories, "hidden")) {
|
||||
continue;
|
||||
}
|
||||
$start = new DateTime($event->dtstart);
|
||||
$end = new DateTime($event->dtend);
|
||||
$uid = $event->uid;
|
||||
|
||||
$start = new DateTime($event->dtstart);
|
||||
$end = new DateTime($event->dtend);
|
||||
$uid = $event->uid;
|
||||
|
||||
$interval = DateInterval::createFromDateString('1 day');
|
||||
$period = new DatePeriod($start, $interval, $end);
|
||||
|
||||
foreach ($period as $dt) {
|
||||
$date = $dt->format("Y-m-d");
|
||||
|
||||
$result[$date][$uid]["dtstart"] = $iCal->iCalDateToDateTime($event->dtstart_array[3])->format(DateTime::ATOM);
|
||||
$result[$date][$uid]["dtend"] = $iCal->iCalDateToDateTime($event->dtend_array[3])->format(DateTime::ATOM);
|
||||
$result[$date][$uid]["datestr"] = (isset($event->dtstart_array[0]["VALUE"]) && $event->dtstart_array[0]["VALUE"] == 'DATE')?'':$start->format('H:i');
|
||||
$result[$date][$uid]["summary"] = mb_strimwidth($event->summary, 0, 255, "...");
|
||||
$result[$date][$uid]["location"] = mb_strimwidth($event->location, 0, 255, "...");
|
||||
$result[$date][$uid]["description"] = mb_strimwidth($event->description, 0, 255, "...");
|
||||
if(isset($event->categories)) $result[$date][$uid]["categories"] = $event->categories;
|
||||
}
|
||||
$interval = new DateInterval('P1D');
|
||||
$period = new DatePeriod($start, $interval, $end);
|
||||
|
||||
foreach ($period as $dt) {
|
||||
$date = $dt->format("Y-m-d");
|
||||
$result[$date][$uid] = [
|
||||
"dtstart" => $start->format(DateTime::ATOM), //$ical->iCalDateToDateTime($event->dtstart_array[3])->format(DateTime::ATOM);
|
||||
"dtend" => $end->format(DateTime::ATOM), //$ical->iCalDateToDateTime($event->dtend_array[3])->format(DateTime::ATOM);
|
||||
"datestr" => (isset($event->dtstart_array[0]["VALUE"]) && $event->dtstart_array[0]["VALUE"] == 'DATE') ? '' : $start->format('H:i'),
|
||||
"summary" => mb_strimwidth($event->summary, 0, 255, "..."),
|
||||
"location" => mb_strimwidth($event->location ?? '', 0, 255, "..."),
|
||||
"description" => mb_strimwidth($event->description ?? '', 0, 255, "...")
|
||||
];
|
||||
if(!empty($cat)) $result[$date][$uid]["categories"] = $cat;
|
||||
}
|
||||
}
|
||||
|
||||
# Allow every page to load this json
|
||||
header("Access-Control-Allow-Origin: *");
|
||||
header("Content-Type: application/json");
|
||||
|
||||
echo json_encode($result, JSON_UNESCAPED_SLASHES);
|
||||
header("Content-Type: application/json; charset=utf-8");
|
||||
echo json_encode($result, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
|
||||
Loading…
Add table
Add a link
Reference in a new issue