PageRenderTime 56ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/core/lib/xcal.lib.php

https://github.com/asterix14/dolibarr
PHP | 523 lines | 291 code | 61 blank | 171 comment | 56 complexity | 6f1a8d53d71c73a1fbd8dcf05825ad69 MD5 | raw file
Possible License(s): LGPL-2.0
  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 2 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:Tache 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:Tache 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. $urlwithouturlroot=preg_replace('/'.preg_quote(DOL_URL_ROOT,'/').'$/i','',$dolibarr_main_url_root);
  292. $url=$urlwithouturlroot.DOL_URL_ROOT.'/public/agenda/agendaexport.php?format=rss&exportkey='.urlencode($conf->global->MAIN_AGENDA_XCAL_EXPORTKEY);
  293. $form.='<link><![CDATA['.$url.']]></link>'."\n";
  294. //print $form;
  295. fwrite($fichier, $form);
  296. foreach ($events_array as $date => $event)
  297. {
  298. $eventqualified=true;
  299. if ($filter)
  300. {
  301. // TODO Add a filter
  302. $eventqualified=false;
  303. }
  304. if ($eventqualified)
  305. {
  306. $uid = $event['uid'];
  307. $startdate = $event['startdate'];
  308. $summary = $event['summary'];
  309. $url = $event['url'];
  310. $author = $event['author'];
  311. $category = $event['category'];
  312. /* No place inside a RSS
  313. $priority = $event['priority'];
  314. $fulldayevent = $event['fulldayevent'];
  315. $location = $event['location'];
  316. $email = $event['email'];
  317. */
  318. $description=preg_replace('/<br[\s\/]?>/i',"\n",$event['desc']);
  319. $description=dol_string_nohtmltag($description,0); // Remove html tags
  320. fwrite($fichier, "<item>\n");
  321. fwrite($fichier, "<title><![CDATA[".$summary."]]></title>\n");
  322. fwrite($fichier, "<link><![CDATA[".$url."]]></link>\n");
  323. fwrite($fichier, "<author><![CDATA[".$author."]]></author>\n");
  324. fwrite($fichier, "<category><![CDATA[".$category."]]></category>\n");
  325. fwrite($fichier, "<description><![CDATA[");
  326. if ($description) fwrite($fichier, $description);
  327. //else fwrite($fichier, 'NoDesc');
  328. fwrite($fichier, "]]></description>\n");
  329. fwrite($fichier, "<pubDate>".date("r", $startdate)."</pubDate>\n");
  330. fwrite($fichier, "<guid isPermaLink=\"true\"><![CDATA[".$uid."]]></guid>\n");
  331. fwrite($fichier, "<source><![CDATA[Dolibarr]]></source>\n");
  332. fwrite($fichier, "</item>\n");
  333. }
  334. }
  335. fwrite($fichier, '</channel>');
  336. fwrite($fichier, "\n");
  337. fwrite($fichier, '</rss>');
  338. fclose($fichier);
  339. if (! empty($conf->global->MAIN_UMASK))
  340. @chmod($outputfile, octdec($conf->global->MAIN_UMASK));
  341. }
  342. }
  343. /**
  344. * Encode for cal export
  345. * string must be encoded in conf->file->character_set_client
  346. *
  347. * @param string $format vcal or ical
  348. * @param string $string string to encode
  349. * @return string string encoded
  350. */
  351. function format_cal($format,$string)
  352. {
  353. global $conf;
  354. if ($conf->file->character_set_client == 'ISO-8859-1') $newstring=utf8_encode($string);
  355. else $newstring=$string;
  356. // Now newstring is always UTF8 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=dol_strlen($line);
  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 ((dol_strlen($newpara) + dol_strlen($char)) >= 76 )
  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 = preg_replace('/=([A-F0-9]{2})/e', chr(hexdec('\\1')), $out);
  459. return trim($out);
  460. }
  461. ?>