1 | <?php
|
---|
2 | /*
|
---|
3 | $Id: icalendar_download.php 2299 2009-08-24 09:46:33Z andrewsimpson $
|
---|
4 |
|
---|
5 | (c) 2005 - 2008 Andrew Simpson <andrew.simpson at paradise.net.nz>
|
---|
6 |
|
---|
7 | WebCollab
|
---|
8 | ---------------------------------------
|
---|
9 |
|
---|
10 | This program is free software; you can redistribute it and/or modify it under the
|
---|
11 | terms of the GNU General Public License as published by the Free Software Foundation;
|
---|
12 | either version 2 of the License, or (at your option) any later version.
|
---|
13 |
|
---|
14 | This program is distributed in the hope that it will be useful, but WITHOUT ANY
|
---|
15 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
---|
16 | PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
---|
17 |
|
---|
18 | You should have received a copy of the GNU General Public License along with this
|
---|
19 | program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave,
|
---|
20 | Cambridge, MA 02139, USA.
|
---|
21 |
|
---|
22 | Function:
|
---|
23 | ---------
|
---|
24 |
|
---|
25 | Creates a download file in the iCalendar format to RFC 2445
|
---|
26 |
|
---|
27 | */
|
---|
28 |
|
---|
29 | //security check
|
---|
30 | if(! defined('UID' ) ) {
|
---|
31 | die('Direct file access not permitted' );
|
---|
32 | }
|
---|
33 |
|
---|
34 | //
|
---|
35 | // Send out iCalendar header
|
---|
36 | //
|
---|
37 |
|
---|
38 | function icalendar_header($id ) {
|
---|
39 |
|
---|
40 | global $dtstamp;
|
---|
41 |
|
---|
42 | //get rid of some problematic system settings
|
---|
43 | @ob_end_clean();
|
---|
44 | @ini_set('zlib.output_compression', 'Off');
|
---|
45 |
|
---|
46 | //cache the generated timestamp (date functions are slow)
|
---|
47 | $dtstamp = gmdate('Ymd\THis\Z');
|
---|
48 |
|
---|
49 | //these headers are for IE 6
|
---|
50 | header('Pragma: public');
|
---|
51 | header('Cache-Control: no-store, max-age=0, no-cache, must-revalidate');
|
---|
52 | header('Cache-Control: post-check=0, pre-check=0', false);
|
---|
53 | header('Cache-control: private');
|
---|
54 |
|
---|
55 | //send the headers describing the file type
|
---|
56 | header('Content-Type: text/calendar; charset=UTF-8' );
|
---|
57 | header('Content-Disposition: attachment; filename="'.ABBR_MANAGER_NAME.'-'.$id.'-'.date('Ymd').'-1.ics"');
|
---|
58 |
|
---|
59 | echo "BEGIN:VCALENDAR\r\n".
|
---|
60 | "VERSION:2.0\r\n".
|
---|
61 | "PRODID:-//WebCollab iCalendar V2.0//EN\r\n".
|
---|
62 | "CALSCALE:GREGORIAN\r\n".
|
---|
63 | "METHOD:PUBLISH\r\n";
|
---|
64 |
|
---|
65 | return;
|
---|
66 | }
|
---|
67 |
|
---|
68 | //
|
---|
69 | // Send out a VTODO or VEVENT
|
---|
70 | //
|
---|
71 |
|
---|
72 | function icalendar_body($row) {
|
---|
73 |
|
---|
74 | global $icalendar_id, $dtstamp;
|
---|
75 |
|
---|
76 | if(VEVENT == 'Y' ) {
|
---|
77 | $content = "BEGIN:VEVENT\r\n";
|
---|
78 | }
|
---|
79 | else {
|
---|
80 | $content = "BEGIN:VTODO\r\n";
|
---|
81 | }
|
---|
82 |
|
---|
83 | $content .= "UID:".$row['taskid']."-".$icalendar_id."\r\n".
|
---|
84 | "SUMMARY:".icalendar_text_format($row['name'] )."\r\n".
|
---|
85 | "DESCRIPTION:".icalendar_text_format($row['text'] )."\r\n".
|
---|
86 | "CREATED:".icalendar_datetime($row['created_utc'])."Z\r\n".
|
---|
87 | "LAST-MODIFIED:".icalendar_datetime($row['edited_utc'])."Z\r\n".
|
---|
88 | "DTSTAMP:".$dtstamp."\r\n".
|
---|
89 | "ORGANIZER;CN=\"".$row['fullname']."\":MAILTO:".$row['email']."\r\n".
|
---|
90 | "SEQUENCE:".$row['sequence']."\r\n";
|
---|
91 |
|
---|
92 | //private
|
---|
93 | if($row['globalaccess'] == 'f' && $row['usergroupid'] != 0 ) {
|
---|
94 | $content.= "CLASS:PRIVATE\r\n";
|
---|
95 | }
|
---|
96 |
|
---|
97 | //priority
|
---|
98 | switch($row['priority'] ) {
|
---|
99 | case 0:
|
---|
100 | $case = '9';
|
---|
101 | break;
|
---|
102 |
|
---|
103 | case 1:
|
---|
104 | $case = '7';
|
---|
105 | break;
|
---|
106 |
|
---|
107 | case 3:
|
---|
108 | $case = '3';
|
---|
109 | break;
|
---|
110 |
|
---|
111 | case 4:
|
---|
112 | $case = '1';
|
---|
113 | break;
|
---|
114 |
|
---|
115 | case 2:
|
---|
116 | default:
|
---|
117 | $case = '5';
|
---|
118 | break;
|
---|
119 | }
|
---|
120 |
|
---|
121 | $content .= "PRIORITY:".$case."\r\n";
|
---|
122 |
|
---|
123 | //get the specific parts for a VTODO or VEVENT
|
---|
124 | if(VEVENT == 'Y' ) {
|
---|
125 | $content .= icalendar_vevent($row );
|
---|
126 | }
|
---|
127 | else {
|
---|
128 | $content .= icalendar_vtodo($row );
|
---|
129 | }
|
---|
130 |
|
---|
131 | //echo $content;
|
---|
132 |
|
---|
133 | return $content;
|
---|
134 | }
|
---|
135 |
|
---|
136 | //
|
---|
137 | // Send out a vtodo set
|
---|
138 | //
|
---|
139 |
|
---|
140 | function icalendar_vtodo($row) {
|
---|
141 |
|
---|
142 | global $icalendar_id;
|
---|
143 |
|
---|
144 | //deadline
|
---|
145 | $content = "DUE;VALUE=DATE:".icalendar_date($row['deadline_date'])."\r\n".
|
---|
146 | "DTSTART:".icalendar_datetime($row['created_utc'])."Z\r\n";
|
---|
147 |
|
---|
148 | //status
|
---|
149 | switch($row['status'] ) {
|
---|
150 | case 'done':
|
---|
151 | $case = 'COMPLETED';
|
---|
152 | break;
|
---|
153 |
|
---|
154 | case 'active':
|
---|
155 | $case = 'IN-PROCESS';
|
---|
156 | break;
|
---|
157 |
|
---|
158 | case 'cantcomplete':
|
---|
159 | case 'notactive':
|
---|
160 | $case = 'CANCELLED';
|
---|
161 | break;
|
---|
162 |
|
---|
163 | case 'created':
|
---|
164 | default:
|
---|
165 | $case = 'NEEDS-ACTION';
|
---|
166 | break;
|
---|
167 | }
|
---|
168 |
|
---|
169 | $content .= "STATUS:".$case."\r\n";
|
---|
170 |
|
---|
171 | if($row['parent'] == 0 ) {
|
---|
172 | //project
|
---|
173 | $content .= "CATEGORIES:Project\r\n";
|
---|
174 |
|
---|
175 | $content .= "PERCENT-COMPLETE:".intval($row['completed'])."\r\n";
|
---|
176 | }
|
---|
177 | else {
|
---|
178 | //task ==> show relationships
|
---|
179 | $content .= "CATEGORIES:Task\r\n";
|
---|
180 | if($row['parent'] == $row['projectid'] ) {
|
---|
181 | //task under project
|
---|
182 | $content .= "RELATED-TO;RELTYPE=CHILD:".$row['projectid']."-".$icalendar_id."\r\n";
|
---|
183 | }
|
---|
184 | else {
|
---|
185 | //sub task
|
---|
186 | $content .= "RELATED-TO;RELTYPE=CHILD:".$row['parent']."-".$icalendar_id."\r\n";
|
---|
187 | }
|
---|
188 |
|
---|
189 | $task_complete = ($row['status'] == 'done' ) ? 100 : 0;
|
---|
190 | $content .= "PERCENT-COMPLETE:".$task_complete."\r\n";
|
---|
191 | }
|
---|
192 |
|
---|
193 | //private
|
---|
194 | if($row['globalaccess'] == 'f' && $row['usergroupid'] != 0 ) {
|
---|
195 | $content .= "CLASS:PRIVATE\r\n";
|
---|
196 | }
|
---|
197 |
|
---|
198 | $content .= "URL:".BASE_URL."\r\n".
|
---|
199 | "END:VTODO\r\n";
|
---|
200 |
|
---|
201 | return $content;
|
---|
202 | }
|
---|
203 |
|
---|
204 |
|
---|
205 | //
|
---|
206 | // Send out a vevent set
|
---|
207 | //
|
---|
208 |
|
---|
209 | function icalendar_vevent($row) {
|
---|
210 |
|
---|
211 | global $icalendar_id;
|
---|
212 |
|
---|
213 | //deadline
|
---|
214 | $content = "DTSTART;VALUE=DATE:".icalendar_date($row['deadline_date'])."\r\n".
|
---|
215 | "DTEND;VALUE=DATE:".icalendar_date($row['deadline_date_end'])."\r\n";
|
---|
216 |
|
---|
217 | //status
|
---|
218 | switch($row['status'] ) {
|
---|
219 | case 'done':
|
---|
220 | $content = "STATUS:FINAL\r\n";
|
---|
221 | break;
|
---|
222 |
|
---|
223 | case 'cantcomplete':
|
---|
224 | case 'notactive':
|
---|
225 | $content .= "STATUS:CANCELLED\r\n";
|
---|
226 | break;
|
---|
227 | }
|
---|
228 |
|
---|
229 | if($row['parent'] == 0 ) {
|
---|
230 | //project
|
---|
231 | $content .= "CATEGORIES:Project\r\n";
|
---|
232 | }
|
---|
233 | else {
|
---|
234 | //task ==> show relationships
|
---|
235 | $content .= "CATEGORIES:Task\r\n";
|
---|
236 | if($row['parent'] == $row['projectid'] ) {
|
---|
237 | //task under project
|
---|
238 | $content .= "RELATED-TO;RELTYPE=CHILD:".$row['projectid']."-".$icalendar_id."\r\n";
|
---|
239 | }
|
---|
240 | else {
|
---|
241 | //sub task
|
---|
242 | $content .= "RELATED-TO;RELTYPE=CHILD:".$row['parent']."-".$icalendar_id."\r\n";
|
---|
243 | }
|
---|
244 | }
|
---|
245 |
|
---|
246 | $content .= "TRANSP:TRANSPARENT\r\n".
|
---|
247 | "URL:".BASE_URL."\r\n".
|
---|
248 | "END:VEVENT\r\n";
|
---|
249 |
|
---|
250 | return $content;
|
---|
251 | }
|
---|
252 |
|
---|
253 |
|
---|
254 | //
|
---|
255 | // End of file
|
---|
256 | //
|
---|
257 |
|
---|
258 | function icalendar_end(){
|
---|
259 |
|
---|
260 | echo "END:VCALENDAR\r\n";
|
---|
261 |
|
---|
262 | return;
|
---|
263 | }
|
---|
264 |
|
---|
265 | //
|
---|
266 | // Fold long lines to RFC 2445 & set to UTF-8 character set
|
---|
267 | //
|
---|
268 |
|
---|
269 | function icalendar_text_format($string ) {
|
---|
270 |
|
---|
271 | //convert line breaks
|
---|
272 | $string = strtr($string, array("\n"=>'\n' ) );
|
---|
273 |
|
---|
274 | //word wrap at 75 octets with '\r\n[space]'
|
---|
275 | $string = wordwrap($string, 75, "\r\n ", 1 );
|
---|
276 |
|
---|
277 | return $string;
|
---|
278 | }
|
---|
279 |
|
---|
280 | //
|
---|
281 | // Convert database timestamp to iCalendar timestamp
|
---|
282 | //
|
---|
283 |
|
---|
284 | function icalendar_date($timestamp ) {
|
---|
285 |
|
---|
286 | //format is 20040803 for 3 August 2002 input as '2002-08-03 12:35:00'
|
---|
287 | return strtr(substr($timestamp, 0, 10), array('-'=>'' ) );
|
---|
288 | }
|
---|
289 |
|
---|
290 |
|
---|
291 | function icalendar_datetime($timestamp ) {
|
---|
292 |
|
---|
293 | //format is 20040803T123500 for 3 August 2002 12:35am input as '2002-08-03 12:35:00'
|
---|
294 | return strtr(substr($timestamp, 0, 19 ), array('-'=>'', ' '=>'T', ':'=>'' ) );
|
---|
295 | }
|
---|
296 |
|
---|
297 | ?>
|
---|