summaryrefslogtreecommitdiff
path: root/icalExport.php
blob: 5d6d5f32f68622d9f03a08551e29e21224d2168c (plain)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php

define("ICAL_DATE_FORMAT", "Ymd\THis");

class infocalendar_CalendarView {
	public $id;
	public $name;
	public $password;
	
	public function __construct($id, $name, $password) {
			$this->id = $id;
			$this->name = $name;
			$this->password = $password;
	}
}

/** creates a calendarView object for the entry with $name. Returns
 * null if not found */
function infocalendar_getViewByName($name) {
	global $wpdb;
	$result = $wpdb->get_results(
		$wpdb->prepare("SELECT * FROM {$wpdb->prefix}infocalendar_calendar 
			WHERE name = %s", $name),
		'ARRAY_A');
		
	/* if nothing has been found, return */
	if(count($result) != 1)
		return 0;
		
	return new infocalendar_CalendarView($result[0]['id'], $result[0]['name'], 
		$result[0]['password']);
}

function infocalendar_sendAuthHeaderAndExit() {
	header('WWW-Authenticate: Basic realm="My Realm"');
	header('HTTP/1.0 401 Unauthorized');
	echo 'Invalid credentials';
	exit;
}

function infocalendar_beAuthorizedOrBust($view) {
	if($view->password != null) {
		if (!isset($_SERVER['PHP_AUTH_USER'])) {
			infocalendar_sendAuthHeaderAndExit();
		} else if(password_verify($_SERVER['PHP_AUTH_PW'], $view->password)) {
			return;
		} else {
			infocalendar_sendAuthHeaderAndExit();
			die("Should not be here!");
		}
	}
}

function infocalendar_printICalTextFromView($view) {
	global $wpdb;
	
	/* print the calendar */
?>
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//zeldakatze.de//InfoCalendar 1.1//DD	
<?php
	
	/* print singular events */
	$result = $wpdb->get_results($wpdb->prepare("SELECT
		event.id, title, location, startDate, endDate, creator, creationDate,
		repetitionOption, repeatEndTime,
		postID, uid, repeatingEvent, icalImported
		FROM {$wpdb->prefix}infocalendar_event AS event
		
		LEFT JOIN {$wpdb->prefix}infocalendar_eventCalendarMap AS eventCalendarMap
		ON event.id = eventCalendarMap.eventID
		
		LEFT JOIN {$wpdb->prefix}infocalendar_calendar AS calendar
		ON calendar.id = eventCalendarMap.calendarID
		
		LEFT JOIN {$wpdb->prefix}infocalendar_repeatingEvent AS repeatingEvent
		ON repeatingEvent.id = event.id
		
		WHERE
		calendar.name = %s
		",
		$view->name), 'ARRAY_A');
	foreach($result as $row) {
		print("BEGIN:VEVENT\n");
		print("SUMMARY:".$row['title']."\n");
		print("UID:".sha1($row['id'].$row['title'].$row['startDate'])."\n");
		print("DTSTART:".
			date_format(date_create($row['startDate']), ICAL_DATE_FORMAT)."\n");
		print("DTEND:".
			date_format(date_create($row['endDate']), ICAL_DATE_FORMAT)."\n");
		print("DTSTAMP:".
			date_format(date_create($row['creationDate']), ICAL_DATE_FORMAT)
			."\n");
		print("LOCATION:".$row['location']."\n");
		if($row['postID'] != 0) {
			print("URL:".get_permalink($row['postID'])."\n");
		}
		
		/* print the repeat information */
		if($row['repeatingEvent']) {
			$frequency = 'DAILY';
			print("RRULE:FREQ=".$frequency.
			";INTERVAL=".$row['repetitionOption'].
			";UNTIL=".
			date_format(date_create($row['repeatEndTime']),ICAL_DATE_FORMAT).
			"\n");
		}
		
		print("END:VEVENT\n");
	}

	/* close the calendar */
	?>
END:VCALENDAR
	<?php
}

function doICalExport(WP_REST_Request $request) {
	global $wpdb;
	
	/* get the view information */
	// TODO sanitization!?
	$calendarView = infocalendar_getViewByName($request['slug']);
	if($calendarView == null)
		die("unknown view");
	
	/* check authentication */
	infocalendar_beAuthorizedOrBust($calendarView);
	
	/* print the plaintext header */
	header("Content-type: text/plain");
	
	infocalendar_printICalTextFromView($calendarView);
	
	/* we don't want any more wordpress handleing from here */
	exit(0);

}

?>