dolibarr  9.0.0
xcal.lib.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2008-2011 Laurent Destailleur <eldy@users.sourceforge.net>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 
35 function build_calfile($format,$title,$desc,$events_array,$outputfile)
36 {
37  global $conf,$langs;
38 
39  dol_syslog("xcal.lib.php::build_calfile Build cal file ".$outputfile." to format ".$format);
40 
41  if (empty($outputfile)) return -1;
42 
43  // Note: A cal file is an UTF8 encoded file
44  $calfileh=fopen($outputfile,'w');
45  if ($calfileh)
46  {
47  include_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
48  $now=dol_now();
49 
50  $encoding='';
51  if ($format == 'vcal') $encoding='ENCODING=QUOTED-PRINTABLE:';
52 
53  // Print header
54  fwrite($calfileh,"BEGIN:VCALENDAR\n");
55  fwrite($calfileh,"VERSION:2.0\n");
56  fwrite($calfileh,"METHOD:PUBLISH\n");
57  //fwrite($calfileh,"PRODID:-//DOLIBARR ".DOL_VERSION."//EN\n");
58  fwrite($calfileh,"PRODID:-//DOLIBARR ".DOL_VERSION."\n");
59  fwrite($calfileh,"CALSCALE:GREGORIAN\n");
60  fwrite($calfileh,"X-WR-CALNAME:".$encoding.format_cal($format,$title)."\n");
61  fwrite($calfileh,"X-WR-CALDESC:".$encoding.format_cal($format,$desc)."\n");
62  //fwrite($calfileh,"X-WR-TIMEZONE:Europe/Paris\n");
63  if (! empty($conf->global->MAIN_AGENDA_EXPORT_CACHE)
64  && $conf->global->MAIN_AGENDA_EXPORT_CACHE > 60){
65  $hh=convertSecondToTime($conf->global->MAIN_AGENDA_EXPORT_CACHE,'hour');
66  $mm=convertSecondToTime($conf->global->MAIN_AGENDA_EXPORT_CACHE,'min');
67  $ss=convertSecondToTime($conf->global->MAIN_AGENDA_EXPORT_CACHE,'sec');
68  fwrite($calfileh,"X-PUBLISHED-TTL: P".$hh."H".$mm."M".$ss."S\n");
69  }
70 
71  foreach ($events_array as $key => $event)
72  {
73  $eventqualified=true;
74  if ($eventqualified)
75  {
76  // See http://fr.wikipedia.org/wiki/ICalendar for format
77  // See http://www.ietf.org/rfc/rfc2445.txt for RFC
78  $uid = $event['uid'];
79  $type = $event['type'];
80  $startdate = $event['startdate'];
81  $duration = $event['duration'];
82  $enddate = $event['enddate'];
83  $summary = $event['summary'];
84  $category = $event['category'];
85  $priority = $event['priority'];
86  $fulldayevent = $event['fulldayevent'];
87  $location = $event['location'];
88  $email = $event['email'];
89  $url = $event['url'];
90  $transparency = $event['transparency']; // OPAQUE (busy) or TRANSPARENT (not busy)
91  $description=preg_replace('/<br[\s\/]?>/i',"\n",$event['desc']);
92  $description=dol_string_nohtmltag($description,0); // Remove html tags
93  $created = $event['created'];
94  $modified = $event['modified'];
95 
96  // Uncomment for tests
97  //$summary="Resume";
98  //$description="Description";
99  //$description="MemberValidatedInDolibarr gd gdf gd gdff\nNom: tgdf g dfgdf gfd r ter\nType: gdfgfdf dfg fd gfd gd gdf gdf gfd gdfg dfg ddf\nAuteur: AD01fg dgdgdfg df gdf gd";
100 
101  // Format
102  $summary=format_cal($format,$summary);
103  $description=format_cal($format,$description);
104  $category=format_cal($format,$category);
105  $location=format_cal($format,$location);
106 
107  // Output the vCard/iCal VEVENT object
108  /*
109  Example from Google ical export for a 1 hour event:
110  BEGIN:VEVENT
111  DTSTART:20101103T120000Z
112  DTEND:20101103T130000Z
113  DTSTAMP:20101121T144902Z
114  UID:4eilllcsq8r1p87ncg7vc8dbpk@google.com
115  CREATED:20101121T144657Z
116  DESCRIPTION:
117  LAST-MODIFIED:20101121T144707Z
118  LOCATION:
119  SEQUENCE:0
120  STATUS:CONFIRMED
121  SUMMARY:Tâche 1 heure
122  TRANSP:OPAQUE
123  END:VEVENT
124 
125  Example from Google ical export for a 1 day event:
126  BEGIN:VEVENT
127  DTSTART;VALUE=DATE:20101102
128  DTEND;VALUE=DATE:20101103
129  DTSTAMP:20101121T144902Z
130  UID:d09t43kcf1qgapu9efsmmo1m6k@google.com
131  CREATED:20101121T144607Z
132  DESCRIPTION:
133  LAST-MODIFIED:20101121T144607Z
134  LOCATION:
135  SEQUENCE:0
136  STATUS:CONFIRMED
137  SUMMARY:Tâche 1 jour
138  TRANSP:TRANSPARENT
139  END:VEVENT
140  */
141  if ($type == 'event')
142  {
143  fwrite($calfileh,"BEGIN:VEVENT\n");
144  fwrite($calfileh,"UID:".$uid."\n");
145  if (! empty($email))
146  {
147  fwrite($calfileh,"ORGANIZER:MAILTO:".$email."\n");
148  fwrite($calfileh,"CONTACT:MAILTO:".$email."\n");
149  }
150  if (! empty($url))
151  {
152  fwrite($calfileh,"URL:".$url."\n");
153  };
154 
155  if ($created) fwrite($calfileh,"CREATED:".dol_print_date($created,'dayhourxcard',true)."\n");
156  if ($modified) fwrite($calfileh,"LAST-MODIFIED:".dol_print_date($modified,'dayhourxcard',true)."\n");
157  fwrite($calfileh,"SUMMARY:".$encoding.$summary."\n");
158  fwrite($calfileh,"DESCRIPTION:".$encoding.$description."\n");
159 
160  /* Other keys:
161  // Status values for a "VEVENT"
162  statvalue = "TENTATIVE" ;Indicates event is
163  ;tentative.
164  / "CONFIRMED" ;Indicates event is
165  ;definite.
166  / "CANCELLED" ;Indicates event was
167  // Status values for "VTODO".
168  statvalue =/ "NEEDS-ACTION" ;Indicates to-do needs action.
169  / "COMPLETED" ;Indicates to-do completed.
170  / "IN-PROCESS" ;Indicates to-do in process of
171  / "CANCELLED" ;Indicates to-do was cancelled.
172  // Status values for "VJOURNAL".
173  statvalue =/ "DRAFT" ;Indicates journal is draft.
174  / "FINAL" ;Indicates journal is final.
175  / "CANCELLED" ;Indicates journal is removed.
176  */
177  //fwrite($calfileh,"CLASS:PUBLIC\n"); // PUBLIC, PRIVATE, CONFIDENTIAL
178  //fwrite($calfileh,"X-MICROSOFT-CDO-BUSYSTATUS:1\n");
179  //ATTENDEE;CUTYPE=INDIVIDUAL;ROLE=REQ-PARTICIPANT;PARTSTAT=ACCEPTED;CN=Laurent Destailleur;X-NUM-GUESTS=0:mailto:eldy10@gmail.com
180 
181  if (! empty($location)) fwrite($calfileh,"LOCATION:".$encoding.$location."\n");
182  if ($fulldayevent) fwrite($calfileh,"X-FUNAMBOL-ALLDAY:1\n");
183  if ($fulldayevent) fwrite($calfileh,"X-MICROSOFT-CDO-ALLDAYEVENT:1\n");
184 
185  // Date must be GMT dates
186  // Current date
187  fwrite($calfileh,"DTSTAMP:".dol_print_date($now,'dayhourxcard',true)."\n");
188  // Start date
189  $prefix='';
190  $startdatef = dol_print_date($startdate,'dayhourxcard',true);
191  if ($fulldayevent)
192  {
193  $prefix=';VALUE=DATE';
194  $startdatef = dol_print_date($startdate,'dayxcard',false); // Local time
195  }
196  fwrite($calfileh,"DTSTART".$prefix.":".$startdatef."\n");
197  // End date
198  if ($fulldayevent)
199  {
200  if (empty($enddate)) $enddate=dol_time_plus_duree($startdate,1,'d');
201  }
202  else
203  {
204  if (empty($enddate)) $enddate=$startdate+$duration;
205  }
206  $prefix='';
207  $enddatef = dol_print_date($enddate,'dayhourxcard',true);
208  if ($fulldayevent)
209  {
210  $prefix=';VALUE=DATE';
211  $enddatef = dol_print_date($enddate+1,'dayxcard',false);
212  //$enddatef .= dol_print_date($enddate+1,'dayhourxcard',false); // Local time
213  }
214  fwrite($calfileh,"DTEND".$prefix.":".$enddatef."\n");
215  fwrite($calfileh,'STATUS:CONFIRMED'."\n");
216  if (! empty($transparency)) fwrite($calfileh,"TRANSP:".$transparency."\n");
217  if (! empty($category)) fwrite($calfileh,"CATEGORIES:".$encoding.$category."\n");
218  fwrite($calfileh,"END:VEVENT\n");
219  }
220 
221  // Output the vCard/iCal VTODO object
222  // ...
223  //PERCENT-COMPLETE:39
224 
225  // Output the vCard/iCal VJOURNAL object
226  if ($type == 'journal')
227  {
228  fwrite($calfileh,"BEGIN:VJOURNAL\n");
229  fwrite($calfileh,"UID:".$uid."\n");
230  if (! empty($email))
231  {
232  fwrite($calfileh,"ORGANIZER:MAILTO:".$email."\n");
233  fwrite($calfileh,"CONTACT:MAILTO:".$email."\n");
234  }
235  if (! empty($url))
236  {
237  fwrite($calfileh,"URL:".$url."\n");
238  };
239 
240  if ($created) fwrite($calfileh,"CREATED:".dol_print_date($created,'dayhourxcard',true)."\n");
241  if ($modified) fwrite($calfileh,"LAST-MODIFIED:".dol_print_date($modified,'dayhourxcard',true)."\n");
242  fwrite($calfileh,"SUMMARY:".$encoding.$summary."\n");
243  fwrite($calfileh,"DESCRIPTION:".$encoding.$description."\n");
244  fwrite($calfileh,'STATUS:CONFIRMED'."\n");
245  fwrite($calfileh,"CATEGORIES:".$category."\n");
246  fwrite($calfileh,"LOCATION:".$location."\n");
247  fwrite($calfileh,"TRANSP:OPAQUE\n");
248  fwrite($calfileh,"CLASS:CONFIDENTIAL\n");
249  fwrite($calfileh,"DTSTAMP:".dol_print_date($startdatef,'dayhourxcard',true)."\n");
250 
251  fwrite($calfileh,"END:VJOURNAL\n");
252  }
253 
254 
255  // Put other info in comment
256  /*
257  $comment=array();
258  $comment ['eid'] = $eid;
259  $comment ['url'] = $linktoevent;
260  $comment ['date'] = dol_mktime($evttime,"Ymd");
261  $comment ['duration'] = $duration;
262  $comment ['startdate'] = $startdate;
263  $comment ['enddate'] = $enddate;
264  fwrite($calfileh,"COMMENT:" . serialize ($comment) . "\n");
265  */
266  }
267  }
268 
269  // Footer
270  fwrite($calfileh,"END:VCALENDAR");
271 
272  fclose($calfileh);
273  if (! empty($conf->global->MAIN_UMASK))
274  @chmod($outputfile, octdec($conf->global->MAIN_UMASK));
275  }
276  else
277  {
278  dol_syslog("xcal.lib.php::build_calfile Failed to open file ".$outputfile." for writing");
279  return -2;
280  }
281 }
282 
295 function build_rssfile($format,$title,$desc,$events_array,$outputfile,$filter='')
296 {
297  global $user,$conf,$langs;
298  global $dolibarr_main_url_root;
299 
300  dol_syslog("xcal.lib.php::build_rssfile Build rss file ".$outputfile." to format ".$format);
301 
302  if (empty($outputfile)) return -1;
303 
304  $fichier=fopen($outputfile,'w');
305  if ($fichier)
306  {
307  $date=date("r");
308 
309  // Print header
310  $form='<?xml version="1.0" encoding="'.$langs->charset_output.'"?>';
311  fwrite($fichier, $form);
312  fwrite($fichier, "\n");
313  $form='<rss version="2.0">';
314  fwrite($fichier, $form);
315  fwrite($fichier, "\n");
316 
317  $form="<channel>\n<title>".$title."</title>\n";
318  fwrite($fichier, $form);
319 
320  $form='<description><![CDATA['.$desc.'.]]></description>'."\n".
321 // '<language>fr</language>'."\n".
322  '<copyright>Dolibarr</copyright>'."\n".
323  '<lastBuildDate>'.$date.'</lastBuildDate>'."\n".
324  '<generator>Dolibarr</generator>'."\n";
325 
326  // Define $urlwithroot
327  $urlwithouturlroot=preg_replace('/'.preg_quote(DOL_URL_ROOT,'/').'$/i','',trim($dolibarr_main_url_root));
328  $urlwithroot=$urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file
329  //$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current
330  $url=$urlwithroot.'/public/agenda/agendaexport.php?format=rss&exportkey='.urlencode($conf->global->MAIN_AGENDA_XCAL_EXPORTKEY);
331  $form.='<link><![CDATA['.$url.']]></link>'."\n";
332 
333  //print $form;
334  fwrite($fichier, $form);
335 
336 
337  foreach ($events_array as $key => $event)
338  {
339  $eventqualified=true;
340  if ($filter)
341  {
342  // TODO Add a filter
343 
344  $eventqualified=false;
345  }
346 
347  if ($eventqualified)
348  {
349  $uid = $event['uid'];
350  $startdate = $event['startdate'];
351  $summary = $event['summary'];
352  $url = $event['url'];
353  $author = $event['author'];
354  $category = $event['category'];
355  /* No place inside a RSS
356  $priority = $event['priority'];
357  $fulldayevent = $event['fulldayevent'];
358  $location = $event['location'];
359  $email = $event['email'];
360  */
361  $description=preg_replace('/<br[\s\/]?>/i',"\n",$event['desc']);
362  $description=dol_string_nohtmltag($description,0); // Remove html tags
363 
364  fwrite($fichier, "<item>\n");
365  fwrite($fichier, "<title><![CDATA[".$summary."]]></title>\n");
366  fwrite($fichier, "<link><![CDATA[".$url."]]></link>\n");
367  fwrite($fichier, "<author><![CDATA[".$author."]]></author>\n");
368  fwrite($fichier, "<category><![CDATA[".$category."]]></category>\n");
369  fwrite($fichier, "<description><![CDATA[");
370  if ($description) fwrite($fichier, $description);
371  //else fwrite($fichier, 'NoDesc');
372  fwrite($fichier, "]]></description>\n");
373  fwrite($fichier, "<pubDate>".date("r", $startdate)."</pubDate>\n");
374  fwrite($fichier, "<guid isPermaLink=\"true\"><![CDATA[".$uid."]]></guid>\n");
375  fwrite($fichier, "<source><![CDATA[Dolibarr]]></source>\n");
376  fwrite($fichier, "</item>\n");
377  }
378  }
379 
380  fwrite($fichier, '</channel>');
381  fwrite($fichier, "\n");
382  fwrite($fichier, '</rss>');
383 
384  fclose($fichier);
385  if (! empty($conf->global->MAIN_UMASK))
386  @chmod($outputfile, octdec($conf->global->MAIN_UMASK));
387  }
388 }
389 
390 
398 function format_cal($format,$string)
399 {
400  global $conf;
401 
402  $newstring=$string;
403 
404  if ($format == 'vcal')
405  {
406  $newstring=quotedPrintEncode($newstring);
407  }
408  if ($format == 'ical')
409  {
410  // Replace new lines chars by '\n'
411  $newstring=preg_replace('/'."\r\n".'/i',"\n",$newstring);
412  $newstring=preg_replace('/'."\n\r".'/i',"\n",$newstring);
413  $newstring=preg_replace('/'."\n".'/i','\n',$newstring);
414  // Must not exceed 75 char. Cut with "\r\n"+Space
415  $newstring=calEncode($newstring);
416  }
417 
418  return $newstring;
419 }
420 
428 function calEncode($line)
429 {
430  $out = '';
431 
432  $newpara = '';
433 
434  // If mb_ functions exists, it's better to use them
435  if (function_exists('mb_strlen'))
436  {
437  $strlength=mb_strlen($line, 'UTF-8');
438  for ($j = 0; $j <= ($strlength - 1); $j++)
439  {
440  $char = mb_substr($line, $j, 1, 'UTF-8'); // Take char at position $j
441 
442  if ((mb_strlen($newpara, 'UTF-8') + mb_strlen($char, 'UTF-8')) >= 75)
443  {
444  $out .= $newpara . "\r\n "; // CRLF + Space for cal
445  $newpara = '';
446  }
447  $newpara .= $char;
448  }
449  $out .= $newpara;
450  }
451  else
452  {
453  $strlength=dol_strlen($line);
454  for ($j = 0; $j <= ($strlength - 1); $j++)
455  {
456  $char = substr($line, $j, 1); // Take char at position $j
457 
458  if ((dol_strlen($newpara) + dol_strlen($char)) >= 75 )
459  {
460  $out .= $newpara . "\r\n "; // CRLF + Space for cal
461  $newpara = '';
462  }
463  $newpara .= $char;
464  }
465  $out .= $newpara;
466  }
467 
468  return trim($out);
469 }
470 
471 
479 function quotedPrintEncode($str,$forcal=0)
480 {
481  $lines = preg_split("/\r\n/", $str);
482  $out = '';
483 
484  foreach ($lines as $line)
485  {
486  $newpara = '';
487 
488  $strlength=strlen($line); // Do not use dol_strlen here, we need number of bytes
489  for ($j = 0; $j <= ($strlength - 1); $j++)
490  {
491  $char = substr($line, $j, 1);
492  $ascii = ord($char);
493 
494  if ( $ascii < 32 || $ascii == 61 || $ascii > 126 )
495  $char = '=' . strtoupper(sprintf("%02X", $ascii));
496 
497  if ((strlen($newpara) + strlen($char)) >= 76 ) // Do not use dol_strlen here, we need number of bytes
498  {
499  $out .= $newpara . '=' . "\r\n"; // CRLF
500  if ($forcal) $out .= " "; // + Space for cal
501  $newpara = '';
502  }
503  $newpara .= $char;
504  }
505  $out .= $newpara;
506  }
507  return trim($out);
508 }
509 
516 function quotedPrintDecode($str)
517 {
518  $out = preg_replace('/=\r?\n/', '', $str);
519  $out = quoted_printable_decode($out); // Available with PHP 4+
520  return trim($out);
521 }
convertSecondToTime($iSecond, $format='all', $lengthOfDay=86400, $lengthOfWeek=7)
Return, in clear text, value of a number of seconds in days, hours and minutes.
Definition: date.lib.php:180
quotedPrintEncode($str, $forcal=0)
Encode into vcal format.
Definition: xcal.lib.php:479
build_rssfile($format, $title, $desc, $events_array, $outputfile, $filter='')
Build a file from an array of events.
Definition: xcal.lib.php:295
build_calfile($format, $title, $desc, $events_array, $outputfile)
Build a file from an array of events All input params and data must be encoded in $conf->charset_outp...
Definition: xcal.lib.php:35
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
quotedPrintDecode($str)
Decode vcal format.
Definition: xcal.lib.php:516
dol_now($mode='gmt')
Return date for now.
dol_string_nohtmltag($stringtoclean, $removelinefeed=1, $pagecodeto='UTF-8', $strip_tags=0)
Clean a string from all HTML tags and entities.
dol_print_date($time, $format='', $tzoutput='tzserver', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
calEncode($line)
Cut string after 75 chars.
Definition: xcal.lib.php:428
format_cal($format, $string)
Encode for cal export.
Definition: xcal.lib.php:398
dol_time_plus_duree($time, $duration_value, $duration_unit)
Add a delay to a date.
Definition: date.lib.php:116
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.