Add handler to serve filtered ics file
This commit is contained in:
parent
3fcbd8e16f
commit
9ac6a6f3b1
1 changed files with 53 additions and 0 deletions
53
cache/index.php
vendored
Normal file
53
cache/index.php
vendored
Normal file
|
|
@ -0,0 +1,53 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
# Requirements
|
||||||
|
require_once '../include/config.php';
|
||||||
|
|
||||||
|
# Caching
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (file_exists($cachefile)) {
|
||||||
|
$content = file_get_contents($cachefile);
|
||||||
|
|
||||||
|
// Replace X-WR-CALNAME
|
||||||
|
$content = preg_replace('/X-WR-CALNAME:.*(\r\n|\n|\r)/i', "X-WR-CALNAME:" . $ical_name . "\r\n", $content);
|
||||||
|
|
||||||
|
// Remove all ORGANIZER
|
||||||
|
$content = preg_replace('/^ORGANIZER[:;].*(?:\r?\n[\t ].*)*(\r?\n)?/mi', '', $content);
|
||||||
|
|
||||||
|
// Filter CATEGORIES
|
||||||
|
$exclude = $_GET['exclude'] ?? '';
|
||||||
|
|
||||||
|
if ($exclude && strlen($exclude) < 32 && preg_match('/^[a-zA-Z0-9_|-]+$/', $exclude)) {
|
||||||
|
|
||||||
|
$parts = explode('|', $exclude);
|
||||||
|
$safe_parts = array_map(function($part) {
|
||||||
|
return preg_quote(trim($part), '/');
|
||||||
|
}, $parts);
|
||||||
|
$safe_exclude = implode('|', $safe_parts);
|
||||||
|
|
||||||
|
$filtered_content = preg_replace('/BEGIN:VEVENT(?!(?:(?!END:VEVENT)[\s\S])*CATEGORIES:(?:(?!' . $safe_exclude . ')[\s\S])*?(?:\r?\n|;))(?:(?!END:VEVENT)[\s\S])*END:VEVENT(?:\r?\n)?/ms', '', $content);
|
||||||
|
|
||||||
|
if ($filtered_content !== null) {
|
||||||
|
$content = $filtered_content;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
header('Content-Type: text/calendar; charset=utf-8');
|
||||||
|
header('Content-Disposition: attachment; filename="' . $ical_file . '"');
|
||||||
|
header('Content-Description: File Transfer');
|
||||||
|
header('Cache-Control: public, max-age=120, must-revalidate');
|
||||||
|
header('Content-Length: ' . strlen($content));
|
||||||
|
|
||||||
|
echo $content;
|
||||||
|
exit;
|
||||||
|
} else {
|
||||||
|
die("File not found.");
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue