familyHub/tabs/calendar.php

68 lines
2.7 KiB
PHP

<?php
$rawEmbed = defined('GOOGLE_CALENDAR_EMBED_CODE') ? trim((string) GOOGLE_CALENDAR_EMBED_CODE) : '';
$calId = defined('GOOGLE_CALENDAR_ID') ? trim((string) GOOGLE_CALENDAR_ID) : '';
/**
* @return string|null HTTPS embed URL restricted to Google Calendar hosts
*/
function familyHub_google_calendar_embed_url(string $raw): ?string {
$raw = trim($raw);
if ($raw === '') {
return null;
}
$allowed = static function (string $u): bool {
$p = parse_url($u);
if ($p === false || empty($p['scheme']) || empty($p['host'])) {
return false;
}
if (strtolower($p['scheme']) !== 'https') {
return false;
}
$h = strtolower($p['host']);
if ($h === 'calendar.google.com') {
return true;
}
if ($h === 'www.google.com' && isset($p['path']) && str_starts_with($p['path'], '/calendar')) {
return true;
}
return false;
};
if ($allowed($raw)) {
return $raw;
}
if (preg_match('#<iframe\s[^>]*\ssrc\s*=\s*["\']([^"\']+)["\']#i', $raw, $m)) {
$u = html_entity_decode($m[1], ENT_QUOTES | ENT_HTML5, 'UTF-8');
return $allowed($u) ? $u : null;
}
return null;
}
$embedUrl = familyHub_google_calendar_embed_url($rawEmbed);
$fromIdOnly = false;
if ($embedUrl === null && $calId !== '' && $calId !== 'your_calendar_id_here') {
$embedUrl = 'https://calendar.google.com/calendar/embed?src=' . rawurlencode($calId);
$fromIdOnly = true;
}
?>
<div id="calendar" class="tab-content">
<h2 class="mb-2">Calendar</h2>
<?php if ($embedUrl === null): ?>
<p class="text-muted">Connect a shared family calendar by setting <strong>GOOGLE_CALENDAR_EMBED_CODE</strong> (embed URL or full <code>&lt;iframe&gt;</code> from Google Calendar) or <strong>GOOGLE_CALENDAR_ID</strong> in <code>.env</code>. See <a href="https://calendar.google.com/">Google Calendar</a> → calendar menu → <strong>Settings and sharing</strong> → <strong>Integrate calendar</strong>.</p>
<?php else: ?>
<?php if ($fromIdOnly): ?>
<p class="small text-muted mb-2">Showing calendar from <code>GOOGLE_CALENDAR_ID</code>. For more control (view, height), paste the embed URL or iframe into <code>GOOGLE_CALENDAR_EMBED_CODE</code> instead.</p>
<?php endif; ?>
<div class="calendar-embed rounded border overflow-hidden bg-white">
<iframe
class="w-100 border-0 d-block"
style="height: 70vh; min-height: 560px;"
src="<?= htmlspecialchars($embedUrl, ENT_QUOTES, 'UTF-8') ?>"
loading="lazy"
title="Family calendar"
allowfullscreen
></iframe>
</div>
<?php endif; ?>
</div>