xcal.lib.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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. * \file htdocs/core/lib/xcal.lib.php
  19. * \brief Function to manage calendar files (vcal/ical/...)
  20. */
  21. /**
  22. * Build a file from an array of events
  23. * All input params and data must be encoded in $conf->charset_output
  24. *
  25. * @param string $format 'vcal' or 'ical'
  26. * @param string $title Title of export
  27. * @param string $desc Description of export
  28. * @param array $events_array Array of events ('eid','startdate','duration','enddate','title','summary','category','email','url','desc','author')
  29. * @param string $outputfile Output file
  30. * @return int <0 if ko, Nb of events in file if ok
  31. */
  32. function build_calfile($format,$title,$desc,$events_array,$outputfile)
  33. {
  34. global $conf,$langs;
  35. dol_syslog("xcal.lib.php::build_calfile Build cal file ".$outputfile." to format ".$format);
  36. if (empty($outputfile)) return -1;
  37. // Note: A cal file is an UTF8 encoded file
  38. $calfileh=fopen($outputfile,'w');
  39. if ($calfileh)
  40. {
  41. include_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  42. $now=dol_now();
  43. $encoding='';
  44. if ($format == 'vcal') $encoding='ENCODING=QUOTED-PRINTABLE:';
  45. // Print header
  46. fwrite($calfileh,"BEGIN:VCALENDAR\n");
  47. fwrite($calfileh,"VERSION:2.0\n");
  48. fwrite($calfileh,"METHOD:PUBLISH\n");
  49. //fwrite($calfileh,"PRODID:-//DOLIBARR ".DOL_VERSION."//EN\n");
  50. fwrite($calfileh,"PRODID:-//DOLIBARR ".DOL_VERSION."\n");
  51. fwrite($calfileh,"CALSCALE:GREGORIAN\n");
  52. fwrite($calfileh,"X-WR-CALNAME:".$encoding.format_cal($format,$title)."\n");
  53. fwrite($calfileh,"X-WR-CALDESC:".$encoding.format_cal($format,$desc)."\n");
  54. $hh=convertSecondToTime($conf->global->MAIN_AGENDA_EXPORT_CACHE,'hour');
  55. $mm=convertSecondToTime($conf->global->MAIN_AGENDA_EXPORT_CACHE,'min');
  56. $ss=convertSecondToTime($conf->global->MAIN_AGENDA_EXPORT_CACHE,'sec');
  57. //fwrite($calfileh,"X-WR-TIMEZONE:Europe/Paris\n");
  58. if (! empty($conf->global->MAIN_AGENDA_EXPORT_CACHE)
  59. && $conf->global->MAIN_AGENDA_EXPORT_CACHE > 60) fwrite($calfileh,"X-PUBLISHED-TTL: P".$hh."H".$mm."M".$ss."S\n");
  60. foreach ($events_array as $date => $event)
  61. {
  62. $eventqualified=true;
  63. if ($eventqualified)
  64. {
  65. // See http://fr.wikipedia.org/wiki/ICalendar for format
  66. // See http://www.ietf.org/rfc/rfc2445.txt for RFC
  67. $uid = $event['uid'];
  68. $type = $event['type'];
  69. $startdate = $event['startdate'];
  70. $duration = $event['duration'];
  71. $enddate = $event['enddate'];
  72. $summary = $event['summary'];
  73. $category = $event['category'];
  74. $priority = $event['priority'];
  75. $fulldayevent = $event['fulldayevent'];
  76. $location = $event['location'];
  77. $email = $event['email'];
  78. $url = $event['url'];
  79. $transparency = $event['transparency']; // OPAQUE (busy) or TRANSPARENT (not busy)
  80. $description=preg_replace('/<br[\s\/]?>/i',"\n",$event['desc']);
  81. $description=dol_string_nohtmltag($description,0); // Remove html tags
  82. $created = $event['created'];
  83. $modified = $event['modified'];
  84. // Uncomment for tests
  85. //$summary="Resume";
  86. //$description="Description";
  87. //$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";
  88. // Format
  89. $summary=format_cal($format,$summary);
  90. $description=format_cal($format,$description);
  91. $category=format_cal($format,$category);
  92. $location=format_cal($format,$location);
  93. // Output the vCard/iCal VEVENT object
  94. /*
  95. Example from Google ical export for a 1 hour event:
  96. BEGIN:VEVENT
  97. DTSTART:20101103T120000Z
  98. DTEND:20101103T130000Z
  99. DTSTAMP:20101121T144902Z
  100. UID:4eilllcsq8r1p87ncg7vc8dbpk@google.com
  101. CREATED:20101121T144657Z
  102. DESCRIPTION:
  103. LAST-MODIFIED:20101121T144707Z
  104. LOCATION:
  105. SEQUENCE:0
  106. STATUS:CONFIRMED
  107. SUMMARY:Tâche 1 heure
  108. TRANSP:OPAQUE
  109. END:VEVENT
  110. Example from Google ical export for a 1 day event:
  111. BEGIN:VEVENT
  112. DTSTART;VALUE=DATE:20101102
  113. DTEND;VALUE=DATE:20101103
  114. DTSTAMP:20101121T144902Z
  115. UID:d09t43kcf1qgapu9efsmmo1m6k@google.com
  116. CREATED:20101121T144607Z
  117. DESCRIPTION:
  118. LAST-MODIFIED:20101121T144607Z
  119. LOCATION:
  120. SEQUENCE:0
  121. STATUS:CONFIRMED
  122. SUMMARY:Tâche 1 jour
  123. TRANSP:TRANSPARENT
  124. END:VEVENT
  125. */
  126. if ($type == 'event')
  127. {
  128. fwrite($calfileh,"BEGIN:VEVENT\n");
  129. fwrite($calfileh,"UID:".$uid."\n");
  130. if (! empty($email))
  131. {
  132. fwrite($calfileh,"ORGANIZER:MAILTO:".$email."\n");
  133. fwrite($calfileh,"CONTACT:MAILTO:".$email."\n");
  134. }
  135. if (! empty($url))
  136. {
  137. fwrite($calfileh,"URL:".$url."\n");
  138. };
  139. if ($created) fwrite($calfileh,"CREATED:".dol_print_date($created,'dayhourxcard',true)."\n");
  140. if ($modified) fwrite($calfileh,"LAST-MODIFIED:".dol_print_date($modified,'dayhourxcard',true)."\n");
  141. fwrite($calfileh,"SUMMARY:".$encoding.$summary."\n");
  142. fwrite($calfileh,"DESCRIPTION:".$encoding.$description."\n");
  143. /* Other keys:
  144. // Status values for a "VEVENT"
  145. statvalue = "TENTATIVE" ;Indicates event is
  146. ;tentative.
  147. / "CONFIRMED" ;Indicates event is
  148. ;definite.
  149. / "CANCELLED" ;Indicates event was
  150. // Status values for "VTODO".
  151. statvalue =/ "NEEDS-ACTION" ;Indicates to-do needs action.
  152. / "COMPLETED" ;Indicates to-do completed.
  153. / "IN-PROCESS" ;Indicates to-do in process of
  154. / "CANCELLED" ;Indicates to-do was cancelled.
  155. // Status values for "VJOURNAL".
  156. statvalue =/ "DRAFT" ;Indicates journal is draft.
  157. / "FINAL" ;Indicates journal is final.
  158. / "CANCELLED" ;Indicates journal is removed.
  159. */
  160. //fwrite($calfileh,"CLASS:PUBLIC\n"); // PUBLIC, PRIVATE, CONFIDENTIAL
  161. //fwrite($calfileh,"X-MICROSOFT-CDO-BUSYSTATUS:1\n");
  162. //ATTENDEE;CUTYPE=INDIVIDUAL;ROLE=REQ-PARTICIPANT;PARTSTAT=ACCEPTED;CN=Laurent Destailleur;X-NUM-GUESTS=0:mailto:eldy10@gmail.com
  163. if (! empty($location)) fwrite($calfileh,"LOCATION:".$encoding.$location."\n");
  164. if ($fulldayevent) fwrite($calfileh,"X-FUNAMBOL-ALLDAY:1\n");
  165. if ($fulldayevent) fwrite($calfileh,"X-MICROSOFT-CDO-ALLDAYEVENT:1\n");
  166. // Date must be GMT dates
  167. // Current date
  168. fwrite($calfileh,"DTSTAMP:".dol_print_date($now,'dayhourxcard',true)."\n");
  169. // Start date
  170. $prefix='';
  171. $startdatef = dol_print_date($startdate,'dayhourxcard',true);
  172. if ($fulldayevent)
  173. {
  174. $prefix=';VALUE=DATE';
  175. $startdatef = dol_print_date($startdate,'dayxcard',false); // Local time
  176. }
  177. fwrite($calfileh,"DTSTART".$prefix.":".$startdatef."\n");
  178. // End date
  179. if ($fulldayevent)
  180. {
  181. if (empty($enddate)) $enddate=dol_time_plus_duree($startdate,1,'d');
  182. }
  183. else
  184. {
  185. if (empty($enddate)) $enddate=$startdate+$duration;
  186. }
  187. $prefix='';
  188. $enddatef = dol_print_date($enddate,'dayhourxcard',true);
  189. if ($fulldayevent)
  190. {
  191. $prefix=';VALUE=DATE';
  192. $enddatef = dol_print_date($enddate+1,'dayxcard',false);
  193. //$enddatef .= dol_print_date($enddate+1,'dayhourxcard',false); // Local time
  194. }
  195. fwrite($calfileh,"DTEND".$prefix.":".$enddatef."\n");
  196. fwrite($calfileh,'STATUS:CONFIRMED'."\n");
  197. if (! empty($transparency)) fwrite($calfileh,"TRANSP:".$transparency."\n");
  198. if (! empty($category)) fwrite($calfileh,"CATEGORIES:".$encoding.$category."\n");
  199. fwrite($calfileh,"END:VEVENT\n");
  200. }
  201. // Output the vCard/iCal VTODO object
  202. // ...
  203. //PERCENT-COMPLETE:39
  204. // Output the vCard/iCal VJOURNAL object
  205. if ($type == 'journal')
  206. {
  207. fwrite($calfileh,"BEGIN:VJOURNAL\n");
  208. fwrite($calfileh,"UID:".$uid."\n");
  209. if (! empty($email))
  210. {
  211. fwrite($calfileh,"ORGANIZER:MAILTO:".$email."\n");
  212. fwrite($calfileh,"CONTACT:MAILTO:".$email."\n");
  213. }
  214. if (! empty($url))
  215. {
  216. fwrite($calfileh,"URL:".$url."\n");
  217. };
  218. if ($created) fwrite($calfileh,"CREATED:".dol_print_date($created,'dayhourxcard',true)."\n");
  219. if ($modified) fwrite($calfileh,"LAST-MODIFIED:".dol_print_date($modified,'dayhourxcard',true)."\n");
  220. fwrite($calfileh,"SUMMARY:".$encoding.$summary."\n");
  221. fwrite($calfileh,"DESCRIPTION:".$encoding.$description."\n");
  222. fwrite($calfileh,'STATUS:CONFIRMED'."\n");
  223. fwrite($calfileh,"CATEGORIES:".$category."\n");
  224. fwrite($calfileh,"LOCATION:".$location."\n");
  225. fwrite($calfileh,"TRANSP:OPAQUE\n");
  226. fwrite($calfileh,"CLASS:CONFIDENTIAL\n");
  227. fwrite($calfileh,"DTSTAMP:".dol_print_date($startdatef,'dayhourxcard',true)."\n");
  228. fwrite($calfileh,"END:VJOURNAL\n");
  229. }
  230. // Put other info in comment
  231. /*
  232. $comment=array();
  233. $comment ['eid'] = $eid;
  234. $comment ['url'] = $linktoevent;
  235. $comment ['date'] = dol_mktime($evttime,"Ymd");
  236. $comment ['duration'] = $duration;
  237. $comment ['startdate'] = $startdate;
  238. $comment ['enddate'] = $enddate;
  239. fwrite($calfileh,"COMMENT:" . serialize ($comment) . "\n");
  240. */
  241. }
  242. }
  243. // Footer
  244. fwrite($calfileh,"END:VCALENDAR");
  245. fclose($calfileh);
  246. if (! empty($conf->global->MAIN_UMASK))
  247. @chmod($outputfile, octdec($conf->global->MAIN_UMASK));
  248. }
  249. else
  250. {
  251. dol_syslog("xcal.lib.php::build_calfile Failed to open file ".$outputfile." for writing");
  252. return -2;
  253. }
  254. }
  255. /**
  256. * Build a file from an array of events.
  257. * All input data must be encoded in $conf->charset_output
  258. *
  259. * @param string $format 'rss'
  260. * @param string $title Title of export
  261. * @param string $desc Description of export
  262. * @param array $events_array Array of events ('uid','startdate','summary','url','desc','author','category')
  263. * @param string $outputfile Output file
  264. * @param string $filter Filter
  265. * @return int <0 if ko, Nb of events in file if ok
  266. */
  267. function build_rssfile($format,$title,$desc,$events_array,$outputfile,$filter='')
  268. {
  269. global $user,$conf,$langs;
  270. global $dolibarr_main_url_root;
  271. dol_syslog("xcal.lib.php::build_rssfile Build rss file ".$outputfile." to format ".$format);
  272. if (empty($outputfile)) return -1;
  273. $fichier=fopen($outputfile,'w');
  274. if ($fichier)
  275. {
  276. $date=date("r");
  277. // Print header
  278. $form='<?xml version="1.0" encoding="'.$langs->charset_output.'"?>';
  279. fwrite($fichier, $form);
  280. fwrite($fichier, "\n");
  281. $form='<rss version="2.0">';
  282. fwrite($fichier, $form);
  283. fwrite($fichier, "\n");
  284. $form="<channel>\n<title>".$title."</title>\n";
  285. fwrite($fichier, $form);
  286. $form='<description><![CDATA['.$desc.'.]]></description>'."\n".
  287. // '<language>fr</language>'."\n".
  288. '<copyright>Dolibarr</copyright>'."\n".
  289. '<lastBuildDate>'.$date.'</lastBuildDate>'."\n".
  290. '<generator>Dolibarr</generator>'."\n";
  291. // Define $urlwithroot
  292. $urlwithouturlroot=preg_replace('/'.preg_quote(DOL_URL_ROOT,'/').'$/i','',trim($dolibarr_main_url_root));
  293. $urlwithroot=$urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file
  294. //$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current
  295. $url=$urlwithroot.'/public/agenda/agendaexport.php?format=rss&exportkey='.urlencode($conf->global->MAIN_AGENDA_XCAL_EXPORTKEY);
  296. $form.='<link><![CDATA['.$url.']]></link>'."\n";
  297. //print $form;
  298. fwrite($fichier, $form);
  299. foreach ($events_array as $date => $event)
  300. {
  301. $eventqualified=true;
  302. if ($filter)
  303. {
  304. // TODO Add a filter
  305. $eventqualified=false;
  306. }
  307. if ($eventqualified)
  308. {
  309. $uid = $event['uid'];
  310. $startdate = $event['startdate'];
  311. $summary = $event['summary'];
  312. $url = $event['url'];
  313. $author = $event['author'];
  314. $category = $event['category'];
  315. /* No place inside a RSS
  316. $priority = $event['priority'];
  317. $fulldayevent = $event['fulldayevent'];
  318. $location = $event['location'];
  319. $email = $event['email'];
  320. */
  321. $description=preg_replace('/<br[\s\/]?>/i',"\n",$event['desc']);
  322. $description=dol_string_nohtmltag($description,0); // Remove html tags
  323. fwrite($fichier, "<item>\n");
  324. fwrite($fichier, "<title><![CDATA[".$summary."]]></title>\n");
  325. fwrite($fichier, "<link><![CDATA[".$url."]]></link>\n");
  326. fwrite($fichier, "<author><![CDATA[".$author."]]></author>\n");
  327. fwrite($fichier, "<category><![CDATA[".$category."]]></category>\n");
  328. fwrite($fichier, "<description><![CDATA[");
  329. if ($description) fwrite($fichier, $description);
  330. //else fwrite($fichier, 'NoDesc');
  331. fwrite($fichier, "]]></description>\n");
  332. fwrite($fichier, "<pubDate>".date("r", $startdate)."</pubDate>\n");
  333. fwrite($fichier, "<guid isPermaLink=\"true\"><![CDATA[".$uid."]]></guid>\n");
  334. fwrite($fichier, "<source><![CDATA[Dolibarr]]></source>\n");
  335. fwrite($fichier, "</item>\n");
  336. }
  337. }
  338. fwrite($fichier, '</channel>');
  339. fwrite($fichier, "\n");
  340. fwrite($fichier, '</rss>');
  341. fclose($fichier);
  342. if (! empty($conf->global->MAIN_UMASK))
  343. @chmod($outputfile, octdec($conf->global->MAIN_UMASK));
  344. }
  345. }
  346. /**
  347. * Encode for cal export
  348. *
  349. * @param string $format vcal or ical
  350. * @param string $string string to encode
  351. * @return string string encoded
  352. */
  353. function format_cal($format,$string)
  354. {
  355. global $conf;
  356. $newstring=$string;
  357. if ($format == 'vcal')
  358. {
  359. $newstring=quotedPrintEncode($newstring);
  360. }
  361. if ($format == 'ical')
  362. {
  363. // Replace new lines chars by '\n'
  364. $newstring=preg_replace('/'."\r\n".'/i',"\n",$newstring);
  365. $newstring=preg_replace('/'."\n\r".'/i',"\n",$newstring);
  366. $newstring=preg_replace('/'."\n".'/i','\n',$newstring);
  367. // Must not exceed 75 char. Cut with "\r\n"+Space
  368. $newstring=calEncode($newstring);
  369. }
  370. return $newstring;
  371. }
  372. /**
  373. * Cut string after 75 chars. Add CRLF+Space.
  374. * line must be encoded in UTF-8
  375. *
  376. * @param string $line String to convert
  377. * @return string String converted
  378. */
  379. function calEncode($line)
  380. {
  381. $out = '';
  382. $newpara = '';
  383. // If mb_ functions exists, it's better to use them
  384. if (function_exists('mb_strlen'))
  385. {
  386. $strlength=mb_strlen($line, 'UTF-8');
  387. for ($j = 0; $j <= ($strlength - 1); $j++)
  388. {
  389. $char = mb_substr($line, $j, 1, 'UTF-8'); // Take char at position $j
  390. if ((mb_strlen($newpara, 'UTF-8') + mb_strlen($char, 'UTF-8')) >= 75)
  391. {
  392. $out .= $newpara . "\r\n "; // CRLF + Space for cal
  393. $newpara = '';
  394. }
  395. $newpara .= $char;
  396. }
  397. $out .= $newpara;
  398. }
  399. else
  400. {
  401. $strlength=dol_strlen($line);
  402. for ($j = 0; $j <= ($strlength - 1); $j++)
  403. {
  404. $char = substr($line, $j, 1); // Take char at position $j
  405. if ((dol_strlen($newpara) + dol_strlen($char)) >= 75 )
  406. {
  407. $out .= $newpara . "\r\n "; // CRLF + Space for cal
  408. $newpara = '';
  409. }
  410. $newpara .= $char;
  411. }
  412. $out .= $newpara;
  413. }
  414. return trim($out);
  415. }
  416. /**
  417. * Encode into vcal format
  418. *
  419. * @param string $str String to convert
  420. * @param int $forcal 1=For cal
  421. * @return string String converted
  422. */
  423. function quotedPrintEncode($str,$forcal=0)
  424. {
  425. $lines = preg_split("/\r\n/", $str);
  426. $out = '';
  427. foreach ($lines as $line)
  428. {
  429. $newpara = '';
  430. $strlength=strlen($line); // Do not use dol_strlen here, we need number of bytes
  431. for ($j = 0; $j <= ($strlength - 1); $j++)
  432. {
  433. $char = substr($line, $j, 1);
  434. $ascii = ord($char);
  435. if ( $ascii < 32 || $ascii == 61 || $ascii > 126 )
  436. $char = '=' . strtoupper(sprintf("%02X", $ascii));
  437. if ((strlen($newpara) + strlen($char)) >= 76 ) // Do not use dol_strlen here, we need number of bytes
  438. {
  439. $out .= $newpara . '=' . "\r\n"; // CRLF
  440. if ($forcal) $out .= " "; // + Space for cal
  441. $newpara = '';
  442. }
  443. $newpara .= $char;
  444. }
  445. $out .= $newpara;
  446. }
  447. return trim($out);
  448. }
  449. /**
  450. * Decode vcal format
  451. *
  452. * @param string $str String to convert
  453. * @return string String converted
  454. */
  455. function quotedPrintDecode($str)
  456. {
  457. $out = preg_replace('/=\r?\n/', '', $str);
  458. $out = quoted_printable_decode($out); // Available with PHP 4+
  459. return trim($out);
  460. }