PageRenderTime 25ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/calendar/export_execute.php

https://gitlab.com/unofficial-mirrors/moodle
PHP | 252 lines | 197 code | 28 blank | 27 comment | 45 complexity | 4006e3fb8bce639adcfa2a5961596892 MD5 | raw file
  1. <?php
  2. require_once('../config.php');
  3. //require_once($CFG->dirroot.'/course/lib.php');
  4. require_once($CFG->dirroot.'/calendar/lib.php');
  5. require_once($CFG->libdir.'/bennu/bennu.inc.php');
  6. $userid = optional_param('userid', 0, PARAM_INT);
  7. $username = optional_param('username', '', PARAM_TEXT);
  8. $authtoken = required_param('authtoken', PARAM_ALPHANUM);
  9. $generateurl = optional_param('generateurl', '', PARAM_TEXT);
  10. if (empty($CFG->enablecalendarexport)) {
  11. die('no export');
  12. }
  13. //Fetch user information
  14. $checkuserid = !empty($userid) && $user = $DB->get_record('user', array('id' => $userid), 'id,password');
  15. //allowing for fallback check of old url - MDL-27542
  16. $checkusername = !empty($username) && $user = $DB->get_record('user', array('username' => $username), 'id,password');
  17. if (!$checkuserid && !$checkusername) {
  18. //No such user
  19. die('Invalid authentication');
  20. }
  21. //Check authentication token
  22. $authuserid = !empty($userid) && $authtoken == sha1($userid . $user->password . $CFG->calendar_exportsalt);
  23. //allowing for fallback check of old url - MDL-27542
  24. $authusername = !empty($username) && $authtoken == sha1($username . $user->password . $CFG->calendar_exportsalt);
  25. if (!$authuserid && !$authusername) {
  26. die('Invalid authentication');
  27. }
  28. // Get the calendar type we are using.
  29. $calendartype = \core_calendar\type_factory::get_calendar_instance();
  30. $what = optional_param('preset_what', 'all', PARAM_ALPHA);
  31. $time = optional_param('preset_time', 'weeknow', PARAM_ALPHA);
  32. $now = $calendartype->timestamp_to_date_array(time());
  33. // Let's see if we have sufficient and correct data
  34. $allowed_what = array('all', 'user', 'groups', 'courses');
  35. $allowed_time = array('weeknow', 'weeknext', 'monthnow', 'monthnext', 'recentupcoming', 'custom');
  36. if (!empty($generateurl)) {
  37. $authtoken = sha1($user->id . $user->password . $CFG->calendar_exportsalt);
  38. $params = array();
  39. $params['preset_what'] = $what;
  40. $params['preset_time'] = $time;
  41. $params['userid'] = $userid;
  42. $params['authtoken'] = $authtoken;
  43. $params['generateurl'] = true;
  44. $link = new moodle_url('/calendar/export.php', $params);
  45. redirect($link->out());
  46. die;
  47. }
  48. if(!empty($what) && !empty($time)) {
  49. if(in_array($what, $allowed_what) && in_array($time, $allowed_time)) {
  50. $courses = enrol_get_users_courses($user->id, true, 'id, visible, shortname');
  51. // Array of courses that we will pass to calendar_get_legacy_events() which
  52. // is initially set to the list of the user's courses.
  53. $paramcourses = $courses;
  54. if ($what == 'all' || $what == 'groups') {
  55. $groups = array();
  56. foreach ($courses as $course) {
  57. $course_groups = groups_get_all_groups($course->id, $user->id);
  58. $groups = array_merge($groups, array_keys($course_groups));
  59. }
  60. if (empty($groups)) {
  61. $groups = false;
  62. }
  63. }
  64. if ($what == 'all') {
  65. $users = $user->id;
  66. $courses[SITEID] = new stdClass;
  67. $courses[SITEID]->shortname = get_string('globalevents', 'calendar');
  68. $paramcourses[SITEID] = $courses[SITEID];
  69. } else if ($what == 'groups') {
  70. $users = false;
  71. $paramcourses = array();
  72. } else if ($what == 'user') {
  73. $users = $user->id;
  74. $groups = false;
  75. $paramcourses = array();
  76. } else {
  77. $users = false;
  78. $groups = false;
  79. }
  80. // Store the number of days in the week.
  81. $numberofdaysinweek = $calendartype->get_num_weekdays();
  82. switch($time) {
  83. case 'weeknow':
  84. $startweekday = calendar_get_starting_weekday();
  85. $startmonthday = find_day_in_month($now['mday'] - ($numberofdaysinweek - 1), $startweekday, $now['mon'], $now['year']);
  86. $startmonth = $now['mon'];
  87. $startyear = $now['year'];
  88. if ($startmonthday > calendar_days_in_month($startmonth, $startyear)) {
  89. list($startmonth, $startyear) = calendar_add_month($startmonth, $startyear);
  90. $startmonthday = find_day_in_month(1, $startweekday, $startmonth, $startyear);
  91. }
  92. $gregoriandate = $calendartype->convert_to_gregorian($startyear, $startmonth, $startmonthday);
  93. $timestart = make_timestamp($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'],
  94. $gregoriandate['hour'], $gregoriandate['minute']);
  95. $endmonthday = $startmonthday + $numberofdaysinweek;
  96. $endmonth = $startmonth;
  97. $endyear = $startyear;
  98. if ($endmonthday > calendar_days_in_month($endmonth, $endyear)) {
  99. list($endmonth, $endyear) = calendar_add_month($endmonth, $endyear);
  100. $endmonthday = find_day_in_month(1, $startweekday, $endmonth, $endyear);
  101. }
  102. $gregoriandate = $calendartype->convert_to_gregorian($endyear, $endmonth, $endmonthday);
  103. $timeend = make_timestamp($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'],
  104. $gregoriandate['hour'], $gregoriandate['minute']);
  105. break;
  106. case 'weeknext':
  107. $startweekday = calendar_get_starting_weekday();
  108. $startmonthday = find_day_in_month($now['mday'] + 1, $startweekday, $now['mon'], $now['year']);
  109. $startmonth = $now['mon'];
  110. $startyear = $now['year'];
  111. if ($startmonthday > calendar_days_in_month($startmonth, $startyear)) {
  112. list($startmonth, $startyear) = calendar_add_month($startmonth, $startyear);
  113. $startmonthday = find_day_in_month(1, $startweekday, $startmonth, $startyear);
  114. }
  115. $gregoriandate = $calendartype->convert_to_gregorian($startyear, $startmonth, $startmonthday);
  116. $timestart = make_timestamp($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'],
  117. $gregoriandate['hour'], $gregoriandate['minute']);
  118. $endmonthday = $startmonthday + $numberofdaysinweek;
  119. $endmonth = $startmonth;
  120. $endyear = $startyear;
  121. if ($endmonthday > calendar_days_in_month($endmonth, $endyear)) {
  122. list($endmonth, $endyear) = calendar_add_month($endmonth, $endyear);
  123. $endmonthday = find_day_in_month(1, $startweekday, $endmonth, $endyear);
  124. }
  125. $gregoriandate = $calendartype->convert_to_gregorian($endyear, $endmonth, $endmonthday);
  126. $timeend = make_timestamp($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'],
  127. $gregoriandate['hour'], $gregoriandate['minute']);
  128. break;
  129. case 'monthnow':
  130. // Convert to gregorian.
  131. $gregoriandate = $calendartype->convert_to_gregorian($now['year'], $now['mon'], 1);
  132. $timestart = make_timestamp($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'],
  133. $gregoriandate['hour'], $gregoriandate['minute']);
  134. $timeend = $timestart + (calendar_days_in_month($now['mon'], $now['year']) * DAYSECS);
  135. break;
  136. case 'monthnext':
  137. // Get the next month for this calendar.
  138. list($nextmonth, $nextyear) = calendar_add_month($now['mon'], $now['year']);
  139. // Convert to gregorian.
  140. $gregoriandate = $calendartype->convert_to_gregorian($nextyear, $nextmonth, 1);
  141. // Create the timestamps.
  142. $timestart = make_timestamp($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'],
  143. $gregoriandate['hour'], $gregoriandate['minute']);
  144. $timeend = $timestart + (calendar_days_in_month($nextmonth, $nextyear) * DAYSECS);
  145. break;
  146. case 'recentupcoming':
  147. //Events in the last 5 or next 60 days
  148. $timestart = time() - 432000;
  149. $timeend = time() + 5184000;
  150. break;
  151. case 'custom':
  152. // Events based on custom date range.
  153. $timestart = time() - $CFG->calendar_exportlookback * DAYSECS;
  154. $timeend = time() + $CFG->calendar_exportlookahead * DAYSECS;
  155. break;
  156. }
  157. }
  158. else {
  159. // Parameters given but incorrect, redirect back to export page
  160. redirect($CFG->wwwroot.'/calendar/export.php');
  161. die();
  162. }
  163. }
  164. $events = calendar_get_legacy_events($timestart, $timeend, $users, $groups, array_keys($paramcourses), false);
  165. $ical = new iCalendar;
  166. $ical->add_property('method', 'PUBLISH');
  167. $ical->add_property('prodid', '-//Moodle Pty Ltd//NONSGML Moodle Version ' . $CFG->version . '//EN');
  168. foreach($events as $event) {
  169. if (!empty($event->modulename)) {
  170. $instances = get_fast_modinfo($event->courseid, $userid)->get_instances_of($event->modulename);
  171. if (empty($instances[$event->instance]->uservisible)) {
  172. continue;
  173. }
  174. }
  175. $hostaddress = str_replace('http://', '', $CFG->wwwroot);
  176. $hostaddress = str_replace('https://', '', $hostaddress);
  177. $me = new calendar_event($event); // To use moodle calendar event services.
  178. $ev = new iCalendar_event; // To export in ical format.
  179. $ev->add_property('uid', $event->id.'@'.$hostaddress);
  180. // Set iCal event summary from event name.
  181. $ev->add_property('summary', format_string($event->name, true, ['context' => $me->context]));
  182. // Format the description text.
  183. $description = format_text($me->description, $me->format, ['context' => $me->context]);
  184. // Then convert it to plain text, since it's the only format allowed for the event description property.
  185. // We use html_to_text in order to convert <br> and <p> tags to new line characters for descriptions in HTML format.
  186. $description = html_to_text($description, 0);
  187. $ev->add_property('description', $description);
  188. $ev->add_property('class', 'PUBLIC'); // PUBLIC / PRIVATE / CONFIDENTIAL
  189. $ev->add_property('last-modified', Bennu::timestamp_to_datetime($event->timemodified));
  190. $ev->add_property('dtstamp', Bennu::timestamp_to_datetime()); // now
  191. if ($event->timeduration > 0) {
  192. //dtend is better than duration, because it works in Microsoft Outlook and works better in Korganizer
  193. $ev->add_property('dtstart', Bennu::timestamp_to_datetime($event->timestart)); // when event starts.
  194. $ev->add_property('dtend', Bennu::timestamp_to_datetime($event->timestart + $event->timeduration));
  195. } else if ($event->timeduration == 0) {
  196. // When no duration is present, the event is instantaneous event, ex - Due date of a module.
  197. // Moodle doesn't support all day events yet. See MDL-56227.
  198. $ev->add_property('dtstart', Bennu::timestamp_to_datetime($event->timestart));
  199. $ev->add_property('dtend', Bennu::timestamp_to_datetime($event->timestart));
  200. } else {
  201. // This can be used to represent all day events in future.
  202. throw new coding_exception("Negative duration is not supported yet.");
  203. }
  204. if ($event->courseid != 0) {
  205. $coursecontext = context_course::instance($event->courseid);
  206. $ev->add_property('categories', format_string($courses[$event->courseid]->shortname, true, array('context' => $coursecontext)));
  207. }
  208. $ical->add_component($ev);
  209. }
  210. $serialized = $ical->serialize();
  211. if(empty($serialized)) {
  212. // TODO
  213. die('bad serialization');
  214. }
  215. $filename = 'icalexport.ics';
  216. header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
  217. header('Cache-Control: private, must-revalidate, pre-check=0, post-check=0, max-age=0');
  218. header('Expires: '. gmdate('D, d M Y H:i:s', 0) .'GMT');
  219. header('Pragma: no-cache');
  220. header('Accept-Ranges: none'); // Comment out if PDFs do not work...
  221. header('Content-disposition: attachment; filename='.$filename);
  222. header('Content-length: '.strlen($serialized));
  223. header('Content-type: text/calendar; charset=utf-8');
  224. echo $serialized;