diff options
Diffstat (limited to 'ical.php')
-rw-r--r-- | ical.php | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/ical.php b/ical.php new file mode 100644 index 0000000..92c7eb8 --- /dev/null +++ b/ical.php @@ -0,0 +1,67 @@ +<?php +if (isset ($_GET['plain'])){ + header ('Content-Type: text/plain'); +} else { + header ('Content-Type: text/calendar'); + header ('Content-Disposition: attachment; filename=HebrewTools.ical'); +} + +require_once ('config.php'); +require_once ('db.php'); + +function icalspecialchars ($text) +{ + $text=str_replace( + ["\\", "\n", ";", ","], + ["\\\\","\\n","\\;","\\,"], + $text + ); + $text=preg_replace ('/[\x00-\x1f\x7f]/','',$text); + return $text; +} + +function icalline ($key,$val) +{ + $line=$key.':'; + $val=icalspecialchars ($val); + + if (strlen ($val) + strlen ($line) > 75){ + $split_at=75 - strlen ($line); + $line.=substr ($val,0,$split_at)."\r\n"; + $val=substr ($val,$split_at); + $val_parts=str_split ($val,74); + foreach ($val_parts as $val) + $line.=' '.$val."\r\n"; + } else { + $line.=$val."\r\n"; + } + + return $line; +} + +echo "BEGIN:VCALENDAR\r\n"; +echo icalline ('VERSION','2.0'); +echo icalline ('PRODID','-//HebrewTools//NONSGML '.ICAL_NAME.' '.ICAL_VERSION.'//EN'); +echo icalline ('METHOD','PUBLISH'); +echo icalline ('X-WR-CALNAME',ICAL_NAME); +echo icalline ('X-WR-CALDESC',ICAL_DESC); +echo icalline ('X-PUBLISHED-TTL','PT12H'); /* update every 12h */ +echo icalline ('URL','https://'.$_SERVER['HTTP_HOST']); + +$events=isset($_GET['keywords']) ? get_events (explode (',',$_GET['keywords'])) : get_events(); +foreach ($events as $event){ + echo "BEGIN:VEVENT\r\n"; + echo icalline ('UID','event-'.$event['id'].'@agenda.hebrewtools.org'); + echo icalline ('SUMMARY',$event['title']); + echo icalline ('LOCATION',$event['location']); + echo icalline ('DTSTART',date ('Ymd',strtotime ($event['start_date']))); + if ($event['end_date']!=$event['start_date']) + echo icalline ('DTEND',date ('Ymd',strtotime ($event['end_date'])+24*3600)); + $description=$event['description']; + $description.="\n\nKeywords: ".implode (', ',$event['keywords']); + $description.="\nSource: ".$event['source_name']; + echo icalline ("DESCRIPTION",$description); + echo "END:VEVENT\r\n"; +} + +echo "END:VCALENDAR\r\n"; |