MDL-25973 add missing charset
[moodle.git] / calendar / export_execute.php
CommitLineData
e7521559 1<?php
b5f1b8b1 2
3require_once('../config.php');
4//require_once($CFG->dirroot.'/course/lib.php');
5require_once($CFG->dirroot.'/calendar/lib.php');
6require_once($CFG->libdir.'/bennu/bennu.inc.php');
7
ea185313 8$username = required_param('username', PARAM_TEXT);
9$authtoken = required_param('authtoken', PARAM_ALPHANUM);
10
dbf9d4cb 11if (empty($CFG->enablecalendarexport)) {
12 die('no export');
13}
14
ea185313 15//Fetch user information
16if (!$user = get_complete_user_data('username', $username)) {
17 //No such user
804656ff 18 die('Invalid authentication');
b5f1b8b1 19}
20
ea185313 21//Check authentication token
b20c6318 22if ($authtoken != sha1($username . $user->password . $CFG->calendar_exportsalt)) {
804656ff 23 die('Invalid authentication');
ea185313 24}
b5f1b8b1 25
cba168a6 26$what = optional_param('preset_what', 'all', PARAM_ALPHA);
27$time = optional_param('preset_time', 'weeknow', PARAM_ALPHA);
b5f1b8b1 28
29$now = usergetdate(time());
30// Let's see if we have sufficient and correct data
31$allowed_what = array('all', 'courses');
ea185313 32$allowed_time = array('weeknow', 'weeknext', 'monthnow', 'monthnext', 'recentupcoming');
b5f1b8b1 33
34if(!empty($what) && !empty($time)) {
35 if(in_array($what, $allowed_what) && in_array($time, $allowed_time)) {
df997f84 36 $courses = enrol_get_users_courses($user->id, true, 'id, visible, shortname');
941939ef 37
38 if ($what == 'all') {
39 $users = $user->id;
40 $groups = array();
41 foreach ($courses as $course) {
42 $course_groups = groups_get_all_groups($course->id, $user->id);
4a965824 43 $groups = array_merge($groups, array_keys($course_groups));
941939ef 44 }
e36cd5d3 45 if (empty($groups)) {
46 $groups = false;
47 }
ea185313 48 $courses[SITEID] = new stdClass;
49 $courses[SITEID]->shortname = get_string('globalevents', 'calendar');
941939ef 50 } else {
51 $users = false;
52 $groups = false;
ea185313 53 }
cba168a6 54
b5f1b8b1 55 switch($time) {
56 case 'weeknow':
76d9df3f 57 $startweekday = get_user_preferences('calendar_startwday', calendar_get_starting_weekday());
b5f1b8b1 58 $startmonthday = find_day_in_month($now['mday'] - 6, $startweekday, $now['mon'], $now['year']);
59 $startmonth = $now['mon'];
60 $startyear = $now['year'];
61 if($startmonthday > calendar_days_in_month($startmonth, $startyear)) {
62 list($startmonth, $startyear) = calendar_add_month($startmonth, $startyear);
63 $startmonthday = find_day_in_month(1, $startweekday, $startmonth, $startyear);
64 }
65 $timestart = make_timestamp($startyear, $startmonth, $startmonthday);
66 $endmonthday = $startmonthday + 7;
67 $endmonth = $startmonth;
68 $endyear = $startyear;
69 if($endmonthday > calendar_days_in_month($endmonth, $endyear)) {
70 list($endmonth, $endyear) = calendar_add_month($endmonth, $endyear);
71 $endmonthday = find_day_in_month(1, $startweekday, $endmonth, $endyear);
72 }
73 $timeend = make_timestamp($endyear, $endmonth, $endmonthday) - 1;
74 break;
75 case 'weeknext':
76d9df3f 76 $startweekday = get_user_preferences('calendar_startwday', calendar_get_starting_weekday());
b5f1b8b1 77 $startmonthday = find_day_in_month($now['mday'] + 1, $startweekday, $now['mon'], $now['year']);
78 $startmonth = $now['mon'];
79 $startyear = $now['year'];
80 if($startmonthday > calendar_days_in_month($startmonth, $startyear)) {
81 list($startmonth, $startyear) = calendar_add_month($startmonth, $startyear);
82 $startmonthday = find_day_in_month(1, $startweekday, $startmonth, $startyear);
83 }
84 $timestart = make_timestamp($startyear, $startmonth, $startmonthday);
85 $endmonthday = $startmonthday + 7;
86 $endmonth = $startmonth;
87 $endyear = $startyear;
88 if($endmonthday > calendar_days_in_month($endmonth, $endyear)) {
89 list($endmonth, $endyear) = calendar_add_month($endmonth, $endyear);
90 $endmonthday = find_day_in_month(1, $startweekday, $endmonth, $endyear);
91 }
92 $timeend = make_timestamp($endyear, $endmonth, $endmonthday) - 1;
93 break;
94 case 'monthnow':
95 $timestart = make_timestamp($now['year'], $now['mon'], 1);
96 $timeend = make_timestamp($now['year'], $now['mon'], calendar_days_in_month($now['mon'], $now['year']), 23, 59, 59);
97 break;
98 case 'monthnext':
99 list($nextmonth, $nextyear) = calendar_add_month($now['mon'], $now['year']);
100 $timestart = make_timestamp($nextyear, $nextmonth, 1);
101 $timeend = make_timestamp($nextyear, $nextmonth, calendar_days_in_month($nextmonth, $nextyear), 23, 59, 59);
102 break;
ea185313 103 case 'recentupcoming':
104 //Events in the last 5 or next 60 days
105 $timestart = time() - 432000;
106 $timeend = time() + 5184000;
107 break;
b5f1b8b1 108 }
b5f1b8b1 109 }
110 else {
111 // Parameters given but incorrect, redirect back to export page
112 redirect($CFG->wwwroot.'/calendar/export.php');
b5f1b8b1 113 die();
114 }
115}
941939ef 116$events = calendar_get_events($timestart, $timeend, $users, $groups, array_keys($courses), false);
b5f1b8b1 117
118$ical = new iCalendar;
119$ical->add_property('method', 'PUBLISH');
120foreach($events as $event) {
13534ef7
ML
121 if (!empty($event->modulename)) {
122 $cm = get_coursemodule_from_instance($event->modulename, $event->instance);
123 if (!groups_course_module_visible($cm)) {
124 continue;
125 }
126 }
04f1715e 127 $hostaddress = str_replace('http://', '', $CFG->wwwroot);
128 $hostaddress = str_replace('https://', '', $hostaddress);
129
b5f1b8b1 130 $ev = new iCalendar_event;
04f1715e 131 $ev->add_property('uid', $event->id.'@'.$hostaddress);
b5f1b8b1 132 $ev->add_property('summary', $event->name);
133 $ev->add_property('description', $event->description);
ea185313 134 $ev->add_property('class', 'PUBLIC'); // PUBLIC / PRIVATE / CONFIDENTIAL
135 $ev->add_property('last-modified', Bennu::timestamp_to_datetime($event->timemodified));
b5f1b8b1 136 $ev->add_property('dtstamp', Bennu::timestamp_to_datetime()); // now
137 $ev->add_property('dtstart', Bennu::timestamp_to_datetime($event->timestart)); // when event starts
ea185313 138 if ($event->timeduration > 0) {
139 //dtend is better than duration, because it works in Microsoft Outlook and works better in Korganizer
140 $ev->add_property('dtend', Bennu::timestamp_to_datetime($event->timestart + $event->timeduration));
141 }
142 if ($event->courseid != 0) {
143 $ev->add_property('categories', $courses[$event->courseid]->shortname);
144 }
b5f1b8b1 145 $ical->add_component($ev);
146}
147
148$serialized = $ical->serialize();
149if(empty($serialized)) {
150 // TODO
151 die('bad serialization');
152}
153
ea185313 154//IE compatibility HACK!
cbad562e 155if (ini_get_bool('zlib.output_compression')) {
b5f1b8b1 156 ini_set('zlib.output_compression', 'Off');
157}
158
159$filename = 'icalexport.ics';
160
161header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
162header('Cache-Control: private, must-revalidate, pre-check=0, post-check=0, max-age=0');
163header('Expires: '. gmdate('D, d M Y H:i:s', 0) .'GMT');
164header('Pragma: no-cache');
165header('Accept-Ranges: none'); // Comment out if PDFs do not work...
166header('Content-disposition: attachment; filename='.$filename);
167header('Content-length: '.strlen($serialized));
8a7703ce 168header('Content-type: text/calendar; charset=utf-8');
b5f1b8b1 169
170echo $serialized;