hackcal/cache/index.php
2026-01-20 00:32:39 +01:00

53 lines
No EOL
1.7 KiB
PHP

<?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.");
}