]> git.mxchange.org Git - friendica-addons.git/blob - cal/cal.php
5c2f102191c51afb1bc6cff2a8c6dc756fe9e8f2
[friendica-addons.git] / cal / cal.php
1 <?php
2 /********************************************************************
3  * Name: Calendar Export
4  * Description: This addon exports the public events of your users as calendar files
5  * Version: 0.1
6  * Author: Tobias Diekershoff <https://f.diekershoff.de/profile/tobias>
7  * License: MIT
8  * Status: Unsupported
9  * ******************************************************************/
10
11
12 function cal_install()
13 {
14     register_hook('plugin_settings', 'addon/cal/cal.php', 'cal_addon_settings');
15     register_hook('plugin_settings_post', 'addon/cal/cal.php', 'cal_addon_settings_post');
16 }
17 function cal_uninstall()
18 {
19     unregister_hook('plugin_settings', 'addon/cal/cal.php', 'cal_addon_settings');
20     unregister_hook('plugin_settings_post', 'addon/cal/cal.php', 'cal_addon_settings_post');
21 }
22 function cal_module()
23 {
24 }
25 /*  pathes
26  *  /cal/$user/export/$format
27  *  currently supported formats are ical (iCalendar) and CSV
28  */
29 function cal_content()
30 {
31     $a = get_app();
32     $o = "";
33     if ($a->argc == 1) {
34     $o .= "<h3>".t('Event Export')."</h3><p>".t('You can download public events from: ').$a->get_baseurl()."/cal/username/export/ical</p>";
35     } elseif ($a->argc==4) {
36         //  get the parameters from the request we just received
37         $username = $a->argv[1];
38         $do = $a->argv[2];
39         $format = $a->argv[3];
40         //  check that there is a user matching the requested profile
41         $r = q("SELECT uid FROM user WHERE nickname='".$username."' LIMIT 1;");
42         if (count($r)) 
43         {
44             $uid = $r[0]['uid'];
45         } else {
46             killme();
47         }
48         //  if we shall do anything other then export, end here
49         if (! $do == 'export' )
50             killme();
51         //  check if the user allows us to share the profile
52         $enable = get_pconfig( $uid, 'cal', 'enable');
53         if (! $enable == 1) {
54             info(t('The user does not export the calendar.'));
55             return;
56         }
57         //  we are allowed to show events
58         //  get the timezone the user is in
59         $r = q("SELECT timezone FROM user WHERE uid=".$uid." LIMIT 1;");
60         if (count($r))
61             $timezone = $r[0]['timezone'];
62         //  does the user who requests happen to be the owner of the events 
63         //  requested? then show all of your events, otherwise only those that 
64         //  don't have limitations set in allow_cid and allow_gid
65         if (local_user() == $uid) {
66             $r = q("SELECT `start`, `finish`, `adjust`, `summary`, `desc`, `location` FROM `event` WHERE `uid`=".$uid." and `cid`=0;");
67         } else {
68             $r = q("SELECT `start`, `finish`, `adjust`, `summary`, `desc`, `location` FROM `event` WHERE `allow_cid`='' and `allow_gid`='' and `uid`='".$uid."' and `cid`='0';");
69         }
70         //  we have the events that are available for the requestor
71         //  now format the output according to the requested format
72         $res = cal_format_output($r, $format, $timezone);
73         if (! $res=='')
74             info($res);
75     } else {
76         //  wrong number of parameters
77         killme();
78     }
79     return $o;
80 }
81
82 function cal_format_output ($r, $f, $tz)
83 {
84     $res = t('This calendar format is not supported');
85     switch ($f)
86     {
87         //  format the exported data as a CSV file
88         case "csv":
89             header("Content-type: text/csv");
90             $o = '"Subject", "Start Date", "Start Time", "Description", "End Date", "End Time", "Location"' . PHP_EOL;
91             foreach ($r as $rr) {
92 //  TODO the time / date entries don't include any information about the 
93 //  timezone the event is scheduled in :-/
94                 $tmp1 = strtotime($rr['start']);
95                 $tmp2 = strtotime($rr['finish']);
96                 $time_format = "%H:%M:%S";
97                 $date_format = "%Y-%m-%d";
98                 $o .= '"'.$rr['summary'].'", "'.strftime($date_format, $tmp1) .
99                     '", "'.strftime($time_format, $tmp1).'", "'.$rr['desc'] .
100                     '", "'.strftime($date_format, $tmp2) .
101                     '", "'.strftime($time_format, $tmp2) . 
102                     '", "'.$rr['location'].'"' . PHP_EOL;
103             }
104             echo $o;
105             killme();
106
107         case "ical":
108             header("Content-type: text/ics");
109             $o = 'BEGIN:VCALENDAR'. PHP_EOL
110                 . 'VERSION:2.0' . PHP_EOL
111                 . 'PRODID:-//friendica calendar export//0.1//EN' . PHP_EOL;
112 //  TODO include timezone informations in cases were the time is not in UTC
113 //  see http://tools.ietf.org/html/rfc2445#section-4.8.3
114 //              . 'BEGIN:VTIMEZONE' . PHP_EOL
115 //              . 'TZID:' . $tz . PHP_EOL
116 //              . 'END:VTIMEZONE' . PHP_EOL;
117 //  TODO instead of PHP_EOL CRLF should be used for long entries
118 //       but test your solution against http://icalvalid.cloudapp.net/
119 //       also long lines SHOULD be split at 75 characters length
120             foreach ($r as $rr) {
121                 if ($rr['adjust'] == 1) {
122                     $UTC = 'Z';
123                 } else { 
124                    $UTC = '';
125                 }
126                 $o .= 'BEGIN:VEVENT' . PHP_EOL;
127                 if ($rr[start]) {
128                     $tmp = strtotime($rr['start']);
129                     $dtformat = "%Y%m%dT%H%M%S".$UTC;
130                     $o .= 'DTSTART:'.strftime($dtformat, $tmp).PHP_EOL;
131                 }
132                 if ($rr['finish']) {
133                     $tmp = strtotime($rr['finish']);
134                     $dtformat = "%Y%m%dT%H%M%S".$UTC;
135                     $o .= 'DTEND:'.strftime($dtformat, $tmp).PHP_EOL;
136                 }
137                 if ($rr['summary'])
138                     $tmp = $rr['summary'];
139                     $tmp = str_replace(PHP_EOL, PHP_EOL.' ',$tmp);
140                     $tmp = addcslashes($tmp, ',;');
141                     $o .= 'SUMMARY:' . $tmp . PHP_EOL;
142                 if ($rr['desc'])
143                     $tmp = $rr['desc'];
144                     $tmp = str_replace(PHP_EOL, PHP_EOL.' ',$tmp);
145                     $tmp = addcslashes($tmp, ',;');
146                     $o .= 'DESCRIPTION:' . $tmp . PHP_EOL;
147                 if ($rr['location']) {
148                     $tmp = $rr['location'];
149                     $tmp = str_replace(PHP_EOL, PHP_EOL.' ',$tmp);
150                     $tmp = addcslashes($tmp, ',;');
151                     $o .= 'LOCATION:' . $tmp . PHP_EOL;
152                 }
153                 $o .= 'END:VEVENT' . PHP_EOL;
154             }
155             $o .= 'END:VCALENDAR' . PHP_EOL;
156             echo $o;
157             killme();
158     }
159     return $res;
160 }
161
162 function cal_addon_settings_post ( &$a, &$b  )
163 {
164     if (! local_user())
165         return;
166
167     if (!x($_POST,'cal-submit'))
168         return;
169
170     set_pconfig(local_user(),'cal','enable',intval($_POST['cal-enable']));
171 }
172 function cal_addon_settings ( &$a, &$s  )
173 {
174     if (! local_user())
175         return;
176
177     $enabled = get_pconfig(local_user(), 'cal', 'enable');
178     $checked = (($enabled) ? ' checked="checked" ' : '');
179     $url = $a->get_baseurl().'/cal/'.$a->user['nickname'].'/export/<em>format</em>';
180
181     $s .= '<span id="settings_cal_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_cal_expanded\'); openClose(\'settings_cal_inflated\');">';
182     $s .= '<h3>'.t('Export Events').'</h3>';
183     $s .= '</span>';
184     $s .= '<div id="settings_cal_expanded" class="settings-block" style="display: none;">';
185     $s .= '<span class="fakelink" onclick="openClose(\'settings_cal_expanded\'); openClose(\'settings_cal_inflated\');">';
186     $s .= '<h3>'.t('Export Events').'</h3>';
187     $s .= '</span>';
188
189     $s .= '<div id="cal-wrapper">';
190     $s .= '<p>'.t('If this is enabled, your public events will be available at').' <strong>'.$url.'</strong></p>';
191     $s .= '<p>'.t('Currently supported formats are ical and csv.').'</p>';
192     $s .= '<label id="cal-enable-label" for="cal-checkbox">'. t('Enable calendar export') .'</label>';
193     $s .= '<input id="cal-checkbox" type="checkbox" name="cal-enable" value="1" ' . $checked . '/>';
194     $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="cal-submit" class="settings-submit" value="' . t('Save Settings') . '" /></div>'; 
195     $s .= '<div class="clear"></div>';
196     $s .= '</div>';
197     $s .= '</div>';
198 }
199
200 ?>