aboutsummaryrefslogtreecommitdiff
path: root/ical.php
blob: 92c7eb8cb6375c2f07d69abcc8b17d58f349bd4d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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";