MDL-25973 add missing charset
[moodle.git] / calendar / export_execute.php
1 <?php
3 require_once('../config.php');
4 //require_once($CFG->dirroot.'/course/lib.php');
5 require_once($CFG->dirroot.'/calendar/lib.php');
6 require_once($CFG->libdir.'/bennu/bennu.inc.php');
8 $username = required_param('username', PARAM_TEXT);
9 $authtoken = required_param('authtoken', PARAM_ALPHANUM);
11 if (empty($CFG->enablecalendarexport)) {
12     die('no export');
13 }
15 //Fetch user information
16 if (!$user = get_complete_user_data('username', $username)) {
17    //No such user
18     die('Invalid authentication');
19 }
21 //Check authentication token
22 if ($authtoken != sha1($username . $user->password . $CFG->calendar_exportsalt)) {
23     die('Invalid authentication');
24 }
26 $what = optional_param('preset_what', 'all', PARAM_ALPHA);
27 $time = optional_param('preset_time', 'weeknow', PARAM_ALPHA);
29 $now = usergetdate(time());
30 // Let's see if we have sufficient and correct data
31 $allowed_what = array('all', 'courses');
32 $allowed_time = array('weeknow', 'weeknext', 'monthnow', 'monthnext', 'recentupcoming');
34 if(!empty($what) && !empty($time)) {
35     if(in_array($what, $allowed_what) && in_array($time, $allowed_time)) {
36         $courses = enrol_get_users_courses($user->id, true, 'id, visible, shortname');
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);
43                 $groups = array_merge($groups, array_keys($course_groups));
44             }
45             if (empty($groups)) {
46                 $groups = false;
47             }
48             $courses[SITEID] = new stdClass;
49             $courses[SITEID]->shortname = get_string('globalevents', 'calendar');
50         } else {
51             $users = false;
52             $groups = false;
53         }
55         switch($time) {
56             case 'weeknow':
57                 $startweekday  = get_user_preferences('calendar_startwday', calendar_get_starting_weekday());
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':
76                 $startweekday  = get_user_preferences('calendar_startwday', calendar_get_starting_weekday());
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;
103             case 'recentupcoming':
104                 //Events in the last 5 or next 60 days
105                 $timestart = time() - 432000;
106                 $timeend = time() + 5184000;
107             break;
108         }
109     }
110     else {
111         // Parameters given but incorrect, redirect back to export page
112         redirect($CFG->wwwroot.'/calendar/export.php');
113         die();
114     }
116 $events = calendar_get_events($timestart, $timeend, $users, $groups, array_keys($courses), false);
118 $ical = new iCalendar;
119 $ical->add_property('method', 'PUBLISH');
120 foreach($events as $event) {
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     }
127     $hostaddress = str_replace('http://', '', $CFG->wwwroot);
128     $hostaddress = str_replace('https://', '', $hostaddress);
130     $ev = new iCalendar_event;
131     $ev->add_property('uid', $event->id.'@'.$hostaddress);
132     $ev->add_property('summary', $event->name);
133     $ev->add_property('description', $event->description);
134     $ev->add_property('class', 'PUBLIC'); // PUBLIC / PRIVATE / CONFIDENTIAL
135     $ev->add_property('last-modified', Bennu::timestamp_to_datetime($event->timemodified));
136     $ev->add_property('dtstamp', Bennu::timestamp_to_datetime()); // now
137     $ev->add_property('dtstart', Bennu::timestamp_to_datetime($event->timestart)); // when event starts
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     }
145     $ical->add_component($ev);
148 $serialized = $ical->serialize();
149 if(empty($serialized)) {
150     // TODO
151     die('bad serialization');
154 //IE compatibility HACK!
155 if (ini_get_bool('zlib.output_compression')) {
156     ini_set('zlib.output_compression', 'Off');
159 $filename = 'icalexport.ics';
161 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
162 header('Cache-Control: private, must-revalidate, pre-check=0, post-check=0, max-age=0');
163 header('Expires: '. gmdate('D, d M Y H:i:s', 0) .'GMT');
164 header('Pragma: no-cache');
165 header('Accept-Ranges: none'); // Comment out if PDFs do not work...
166 header('Content-disposition: attachment; filename='.$filename);
167 header('Content-length: '.strlen($serialized));
168 header('Content-type: text/calendar; charset=utf-8');
170 echo $serialized;