2 /********************************************************************
3 * Name: Calendar Export
4 * Description: This addon exports the public events of your users as calendar files
6 * Author: Tobias Diekershoff <https://f.diekershoff.de/profile/tobias>
8 * ******************************************************************/
11 function cal_install()
13 register_hook('plugin_settings', 'addon/cal/cal.php', 'cal_addon_settings');
14 register_hook('plugin_settings_post', 'addon/cal/cal.php', 'cal_addon_settings_post');
16 function cal_uninstall()
18 unregister_hook('plugin_settings', 'addon/cal/cal.php', 'cal_addon_settings');
19 unregister_hook('plugin_settings_post', 'addon/cal/cal.php', 'cal_addon_settings_post');
26 * currently supported format is ical (iCalendar
28 function cal_content()
33 $o .= "<h3>".t('Event Export')."</h3><p>".t('You can download public events from: ').$a->get_baseurl()."/cal/username/export/ical</p>";
34 } elseif ($a->argc==4) {
35 // get the parameters from the request we just received
36 $username = $a->argv[1];
38 $format = $a->argv[3];
39 // check that there is a user matching the requested profile
40 $r = q("SELECT uid FROM user WHERE nickname='".$username."' LIMIT 1;");
47 // if we shall do anything other then export, end here
48 if (! $do == 'export' )
50 // check if the user allows us to share the profile
51 $enable = get_pconfig( $uid, 'cal', 'enable');
53 info(t('The user does not export the calendar.'));
56 // we are allowed to show events
57 // get the timezone the user is in
58 $r = q("SELECT timezone FROM user WHERE uid=".$uid." LIMIT 1;");
60 $timezone = $r[0]['timezone'];
61 // does the user who requests happen to be the owner of the events
62 // requested? then show all of your events, otherwise only those that
63 // don't have limitations set in allow_cid and allow_gid
64 if (local_user() == $uid) {
65 $r = q("SELECT `start`, `finish`, `adjust`, `summary`, `desc`, `location` FROM `event` WHERE `uid`=".$uid.";");
67 $r = q("SELECT `start`, `finish`, `adjust`, `summary`, `desc`, `location` FROM `event` WHERE `allow_cid`='' and `allow_gid`='' and `uid`='".$uid."';");
69 // we have the events that are available for the requestor
70 // now format the output according to the requested format
71 $res = cal_format_output($r, $format, $timezone);
75 // wrong number of parameters
81 function cal_format_output ($r, $f, $tz)
83 $res = t('This calendar format is not supported');;
87 header("Content-type: text/icon");
89 $o = 'BEGIN:VCALENDAR'. PHP_EOL
90 . 'PRODID:-//friendica calendar export//0.1//EN' . PHP_EOL
91 . 'VERSION:2.0' . PHP_EOL;
92 // TODO include timezone informations in cases were the time is not in UTC
93 // . 'BEGIN:VTIMEZONE' . PHP_EOL
94 // . 'TZID:' . $tz . PHP_EOL
95 // . 'END:VTIMEZONE' . PHP_EOL;
97 if ($rr['adjust'] == 1) {
102 $o .= 'BEGIN:VEVENT' . PHP_EOL;
104 $tmp = strtotime($rr['start']);
105 $dtformat = "%Y%m%dT%H%M%S".$UTC;
106 $o .= 'DTSTART:'.strftime($dtformat, $tmp).PHP_EOL;
109 $tmp = strtotime($rr['finish']);
110 $dtformat = "%Y%m%dT%H%M%S".$UTC;
111 $o .= 'DTEND:'.strftime($dtformat, $tmp).PHP_EOL;
114 $tmp = $rr['summary'];
115 $tmp = str_replace(PHP_EOL, PHP_EOL.' ',$tmp);
116 $o .= 'SUMMARY:' . $tmp . PHP_EOL;
119 $tmp = str_replace(PHP_EOL, PHP_EOL.' ',$tmp);
120 $o .= 'DESCRIPTION:' . $tmp . PHP_EOL;
121 if ($rr['location']) {
122 $tmp = $rr['location'];
123 $tmp = str_replace(PHP_EOL, PHP_EOL.' ',$tmp);
124 $o .= 'LOCATION:' . $tmp . PHP_EOL;
126 $o .= 'END:VEVENT' . PHP_EOL;
128 $o .= 'END:VCALENDAR' . PHP_EOL;
135 function cal_addon_settings_post ( &$a, &$b )
140 if (!x($_POST,'cal-submit'))
143 set_pconfig(local_user(),'cal','enable',intval($_POST['cal-enable']));
145 function cal_addon_settings ( &$a, &$s )
150 $enabled = get_pconfig(local_user(), 'cal', 'enable');
151 $checked = (($enabled) ? ' checked="checked" ' : '');
152 $url = $a->get_baseurl().'/cal/'.$a->user['nickname'].'/export/ical';
154 $s .= '<h3>'.t('Export Events').'</h3>';
155 $s .= '<p>'.t('If this is enabled, you public events will be available at').' <strong>'.$url.'</strong></p>';
156 $s .= '<div id="cal-enable-wrapper">';
157 $s .= '<label id="cal-enable-label" for="cal-checkbox">'. t('Enable calendar export') .'</label>';
158 $s .= '<input id="cal-checkbox" type="checkbox" name="cal-enable" value="1" ' . $checked . '/>';
159 $s .= '</div><div class="clear"></div>';
160 $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="cal-submit" class="settings-submit" value="' . t('Submit') . '" /></div>';
161 $s .= '</div><div class="clear"></div>';