xcal.lib.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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 <https://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 ("uid","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)) {
  37. // -1 = error
  38. return -1;
  39. }
  40. $nbevents = 0;
  41. // Note: A cal file is an UTF8 encoded file
  42. $calfileh = fopen($outputfile, "w");
  43. if ($calfileh) {
  44. include_once DOL_DOCUMENT_ROOT."/core/lib/date.lib.php";
  45. $now = dol_now();
  46. $encoding = "";
  47. if ($format === "vcal") {
  48. $encoding = "ENCODING=QUOTED-PRINTABLE:";
  49. }
  50. // Print header
  51. fwrite($calfileh, "BEGIN:VCALENDAR\n");
  52. // version is always "2.0"
  53. fwrite($calfileh, "VERSION:2.0\n");
  54. fwrite($calfileh, "METHOD:PUBLISH\n");
  55. fwrite($calfileh, "PRODID:-//DOLIBARR ".DOL_VERSION."\n");
  56. fwrite($calfileh, "CALSCALE:GREGORIAN\n");
  57. fwrite($calfileh, "X-WR-CALNAME:".$encoding.format_cal($format, $title)."\n");
  58. fwrite($calfileh, "X-WR-CALDESC:".$encoding.format_cal($format, $desc)."\n");
  59. //fwrite($calfileh,"X-WR-TIMEZONE:Europe/Paris\n");
  60. if (!empty($conf->global->MAIN_AGENDA_EXPORT_CACHE) && $conf->global->MAIN_AGENDA_EXPORT_CACHE > 60) {
  61. $hh = convertSecondToTime($conf->global->MAIN_AGENDA_EXPORT_CACHE, "hour");
  62. $mm = convertSecondToTime($conf->global->MAIN_AGENDA_EXPORT_CACHE, "min");
  63. $ss = convertSecondToTime($conf->global->MAIN_AGENDA_EXPORT_CACHE, "sec");
  64. fwrite($calfileh, "X-PUBLISHED-TTL: P".$hh."H".$mm."M".$ss."S\n");
  65. }
  66. foreach ($events_array as $key => $event) {
  67. // See http://fr.wikipedia.org/wiki/ICalendar for format
  68. // See http://www.ietf.org/rfc/rfc2445.txt for RFC
  69. // TODO: avoid use extra event array, use objects direct thahtwas created before
  70. $uid = $event["uid"];
  71. $type = $event["type"];
  72. $startdate = $event["startdate"];
  73. $duration = $event["duration"];
  74. $enddate = $event["enddate"];
  75. $summary = $event["summary"];
  76. $category = $event["category"];
  77. $priority = $event["priority"];
  78. $fulldayevent = $event["fulldayevent"];
  79. $location = $event["location"];
  80. $email = $event["email"];
  81. $url = $event["url"];
  82. $transparency = $event["transparency"];
  83. $description = dol_string_nohtmltag(preg_replace("/<br[\s\/]?>/i", "\n", $event["desc"]), 0);
  84. $created = $event["created"];
  85. $modified = $event["modified"];
  86. $assignedUsers = $event["assignedUsers"];
  87. //print $fulldayevent.' '.dol_print_date($startdate, 'dayhour', 'gmt');
  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. $nbevents++;
  128. fwrite($calfileh, "BEGIN:VEVENT\n");
  129. fwrite($calfileh, "UID:".$uid."\n");
  130. if (!empty($email)) {
  131. fwrite($calfileh, "ORGANIZER:MAILTO:".$email."\n");
  132. fwrite($calfileh, "CONTACT:MAILTO:".$email."\n");
  133. }
  134. if (!empty($url)) {
  135. fwrite($calfileh, "URL:".$url."\n");
  136. }
  137. if (is_array($assignedUsers)) {
  138. foreach ($assignedUsers as $assignedUser) {
  139. if ($assignedUser->email === $email) {
  140. continue;
  141. }
  142. fwrite($calfileh, "ATTENDEE;RSVP=TRUE:mailto:".$assignedUser->email."\n");
  143. }
  144. }
  145. if ($created) {
  146. fwrite($calfileh, "CREATED:".dol_print_date($created, "dayhourxcard", true)."\n");
  147. }
  148. if ($modified) {
  149. fwrite($calfileh, "LAST-MODIFIED:".dol_print_date($modified, "dayhourxcard", true)."\n");
  150. }
  151. fwrite($calfileh, "SUMMARY:".$encoding.$summary."\n");
  152. fwrite($calfileh, "DESCRIPTION:".$encoding.$description."\n");
  153. if (!empty($location)) {
  154. fwrite($calfileh, "LOCATION:".$encoding.$location."\n");
  155. }
  156. if ($fulldayevent) {
  157. fwrite($calfileh, "X-FUNAMBOL-ALLDAY:1\n");
  158. }
  159. // see https://docs.microsoft.com/en-us/openspecs/exchange_server_protocols/ms-oxcical/0f262da6-c5fd-459e-9f18-145eba86b5d2
  160. if ($fulldayevent) {
  161. fwrite($calfileh, "X-MICROSOFT-CDO-ALLDAYEVENT:TRUE\n");
  162. }
  163. // Date must be GMT dates
  164. // Current date
  165. fwrite($calfileh, "DTSTAMP:".dol_print_date($now, "dayhourxcard", 'gmt')."\n");
  166. // Start date
  167. $prefix = "";
  168. $startdatef = dol_print_date($startdate, "dayhourxcard", 'gmt');
  169. if ($fulldayevent) {
  170. // Local time should be used to prevent users in time zones earlier than GMT from being one day earlier
  171. $prefix = ";VALUE=DATE";
  172. $startdatef = dol_print_date($startdate, "dayxcard", 'tzserver');
  173. }
  174. fwrite($calfileh, "DTSTART".$prefix.":".$startdatef."\n");
  175. // End date
  176. if ($fulldayevent) {
  177. if (empty($enddate)) {
  178. // We add 1 day needed for full day event (DTEND must be next day after event).
  179. // This is mention in https://datatracker.ietf.org/doc/html/rfc5545:
  180. // "The "DTEND" property for a "VEVENT" calendar component specifies the non-inclusive end of the event."
  181. $enddate = dol_time_plus_duree($startdate, 1, "d");
  182. }
  183. } else {
  184. if (empty($enddate)) {
  185. $enddate = $startdate + $duration;
  186. }
  187. }
  188. $prefix = "";
  189. $enddatef = dol_print_date($enddate, "dayhourxcard", 'gmt');
  190. if ($fulldayevent) {
  191. $prefix = ";VALUE=DATE";
  192. // We add 1 second so we reach the +1 day needed for full day event (DTEND must be next day after event)
  193. // This is mention in https://datatracker.ietf.org/doc/html/rfc5545:
  194. // "The "DTEND" property for a "VEVENT" calendar component specifies the non-inclusive end of the event."
  195. $enddatef = dol_print_date($enddate + 1, "dayxcard", 'tzserver');
  196. }
  197. fwrite($calfileh, "DTEND".$prefix.":".$enddatef."\n");
  198. fwrite($calfileh, "STATUS:CONFIRMED\n");
  199. if (!empty($transparency)) {
  200. fwrite($calfileh, "TRANSP:".$transparency."\n");
  201. }
  202. if (!empty($category)) {
  203. fwrite($calfileh, "CATEGORIES:".$encoding.$category."\n");
  204. }
  205. fwrite($calfileh, "END:VEVENT\n");
  206. }
  207. // Output the vCard/iCal VJOURNAL object
  208. if ($type === "journal") {
  209. $nbevents++;
  210. fwrite($calfileh, "BEGIN:VJOURNAL\n");
  211. fwrite($calfileh, "UID:".$uid."\n");
  212. if (!empty($email)) {
  213. fwrite($calfileh, "ORGANIZER:MAILTO:".$email."\n");
  214. fwrite($calfileh, "CONTACT:MAILTO:".$email."\n");
  215. }
  216. if (!empty($url)) {
  217. fwrite($calfileh, "URL:".$url."\n");
  218. }
  219. if ($created) {
  220. fwrite($calfileh, "CREATED:".dol_print_date($created, "dayhourxcard", 'gmt')."\n");
  221. }
  222. if ($modified) {
  223. fwrite($calfileh, "LAST-MODIFIED:".dol_print_date($modified, "dayhourxcard", 'gmt')."\n");
  224. }
  225. fwrite($calfileh, "SUMMARY:".$encoding.$summary."\n");
  226. fwrite($calfileh, "DESCRIPTION:".$encoding.$description."\n");
  227. fwrite($calfileh, "STATUS:CONFIRMED\n");
  228. fwrite($calfileh, "CATEGORIES:".$category."\n");
  229. fwrite($calfileh, "LOCATION:".$location."\n");
  230. fwrite($calfileh, "TRANSP:OPAQUE\n");
  231. fwrite($calfileh, "CLASS:CONFIDENTIAL\n");
  232. fwrite($calfileh, "DTSTAMP:".dol_print_date($startdatef, "dayhourxcard", 'gmt')."\n");
  233. fwrite($calfileh, "END:VJOURNAL\n");
  234. }
  235. }
  236. // Footer
  237. fwrite($calfileh, "END:VCALENDAR");
  238. fclose($calfileh);
  239. dolChmod($outputfile);
  240. } else {
  241. dol_syslog("xcal.lib.php::build_calfile Failed to open file ".$outputfile." for writing");
  242. return -2;
  243. }
  244. return $nbevents;
  245. }
  246. /**
  247. * Build a file from an array of events.
  248. * All input data must be encoded in $conf->charset_output
  249. *
  250. * @param string $format "rss"
  251. * @param string $title Title of export
  252. * @param string $desc Description of export
  253. * @param array $events_array Array of events ("uid","startdate","summary","url","desc","author","category","image") or Array of WebsitePage
  254. * @param string $outputfile Output file
  255. * @param string $filter (optional) Filter
  256. * @param string $url Url (If empty, forge URL for agenda RSS export)
  257. * @param string $langcode Language code to show in header
  258. * @return int < 0 if KO, Nb of events in file if OK
  259. */
  260. function build_rssfile($format, $title, $desc, $events_array, $outputfile, $filter = '', $url = '', $langcode = '')
  261. {
  262. global $user, $conf, $langs, $mysoc;
  263. global $dolibarr_main_url_root;
  264. dol_syslog("xcal.lib.php::build_rssfile Build rss file ".$outputfile." to format ".$format);
  265. if (empty($outputfile)) {
  266. // -1 = error
  267. return -1;
  268. }
  269. $nbevents = 0;
  270. $fichier = fopen($outputfile, "w");
  271. if ($fichier) {
  272. // Print header
  273. fwrite($fichier, '<?xml version="1.0" encoding="'.$langs->charset_output.'"?>');
  274. fwrite($fichier, "\n");
  275. fwrite($fichier, '<rss version="2.0">');
  276. fwrite($fichier, "\n");
  277. fwrite($fichier, "<channel>\n");
  278. fwrite($fichier, "<title>".$title."</title>\n");
  279. if ($langcode) {
  280. fwrite($fichier, "<language>".$langcode."</language>\n");
  281. }
  282. // Define $urlwithroot
  283. $urlwithouturlroot = preg_replace("/".preg_quote(DOL_URL_ROOT, "/")."$/i", "", trim($dolibarr_main_url_root));
  284. $urlwithroot = $urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file
  285. //$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current
  286. // Url
  287. if (empty($url)) {
  288. $url = $urlwithroot."/public/agenda/agendaexport.php?format=rss&exportkey=".urlencode($conf->global->MAIN_AGENDA_XCAL_EXPORTKEY);
  289. }
  290. fwrite($fichier, "<link><![CDATA[".$url."]]></link>\n");
  291. // Image
  292. if (!empty($mysoc->logo_squarred_small)) {
  293. $urlimage = $urlwithroot.'/viewimage.php?cache=1&amp;modulepart=mycompany&amp;file='.urlencode($mysoc->logo_squarred_small);
  294. if ($urlimage) {
  295. fwrite($fichier, "<image><url><![CDATA[".$urlimage."]]></url><title>'.$title.</title></image>\n");
  296. }
  297. }
  298. foreach ($events_array as $key => $event) {
  299. $eventqualified = true;
  300. if ($filter) {
  301. // TODO Add a filter
  302. $eventqualified = false;
  303. }
  304. if ($eventqualified) {
  305. $nbevents++;
  306. if (is_object($event) && get_class($event) == 'WebsitePage') {
  307. // Convert object into an array
  308. $tmpevent = array();
  309. $tmpevent['uid'] = $event->id;
  310. $tmpevent['startdate'] = $event->date_creation;
  311. $tmpevent['summary'] = $event->title;
  312. $tmpevent['url'] = $event->fullpageurl ? $event->fullpageurl : $event->pageurl.'.php';
  313. $tmpevent['author'] = $event->author_alias ? $event->author_alias : 'unknown';
  314. //$tmpevent['category'] = '';
  315. $tmpevent['desc'] = $event->description;
  316. $tmpevent['image'] = $GLOBALS['website']->virtualhost.'/medias/'.$event->image;
  317. $event = $tmpevent;
  318. }
  319. $uid = $event["uid"];
  320. $startdate = $event["startdate"];
  321. $summary = $event["summary"];
  322. $url = $event["url"];
  323. $author = $event["author"];
  324. $category = $event["category"];
  325. if (!empty($event["image"])) {
  326. $image = $event["image"];
  327. }
  328. /* No place inside a RSS
  329. $priority = $event["priority"];
  330. $fulldayevent = $event["fulldayevent"];
  331. $location = $event["location"];
  332. $email = $event["email"];
  333. */
  334. $description = dol_string_nohtmltag(preg_replace("/<br[\s\/]?>/i", "\n", $event["desc"]), 0);
  335. fwrite($fichier, "<item>\n");
  336. fwrite($fichier, "<title><![CDATA[".$summary."]]></title>\n");
  337. fwrite($fichier, "<link><![CDATA[".$url."]]></link>\n");
  338. fwrite($fichier, "<author><![CDATA[".$author."]]></author>\n");
  339. fwrite($fichier, "<category><![CDATA[".$category."]]></category>\n");
  340. fwrite($fichier, "<description><![CDATA[");
  341. if (!empty($image)) {
  342. fwrite($fichier, '<p><img class="center" src="'.$image.'"/></p>');
  343. }
  344. if ($description) {
  345. fwrite($fichier, $description);
  346. }
  347. // else
  348. // fwrite($fichier, "NoDesc");
  349. fwrite($fichier, "]]></description>\n");
  350. fwrite($fichier, "<pubDate>".date("r", $startdate)."</pubDate>\n");
  351. fwrite($fichier, "<guid isPermaLink=\"true\"><![CDATA[".$uid."]]></guid>\n");
  352. fwrite($fichier, "<source><![CDATA[Dolibarr]]></source>\n");
  353. fwrite($fichier, "</item>\n");
  354. }
  355. }
  356. fwrite($fichier, "</channel>");
  357. fwrite($fichier, "\n");
  358. fwrite($fichier, "</rss>");
  359. fclose($fichier);
  360. dolChmod($outputfile);
  361. }
  362. return $nbevents;
  363. }
  364. /**
  365. * Encode for cal export
  366. *
  367. * @param string $format "vcal" or "ical"
  368. * @param string $string String to encode
  369. * @return string String encoded
  370. */
  371. function format_cal($format, $string)
  372. {
  373. $newstring = $string;
  374. if ($format === "vcal") {
  375. $newstring = quotedPrintEncode($newstring);
  376. }
  377. if ($format === "ical") {
  378. // Replace new lines chars by "\n"
  379. $newstring = preg_replace("/\r\n/i", "\\n", $newstring);
  380. $newstring = preg_replace("/\n\r/i", "\\n", $newstring);
  381. $newstring = preg_replace("/\n/i", "\\n", $newstring);
  382. // Must not exceed 75 char. Cut with "\r\n"+Space
  383. $newstring = calEncode($newstring);
  384. }
  385. return $newstring;
  386. }
  387. /**
  388. * Cut string after 75 chars. Add CRLF+Space.
  389. * line must be encoded in UTF-8
  390. *
  391. * @param string $line String to convert
  392. * @return string String converted
  393. */
  394. function calEncode($line)
  395. {
  396. $out = "";
  397. $newpara = "";
  398. // If mb_ functions exists, it"s better to use them
  399. if (function_exists("mb_strlen")) {
  400. $strlength = mb_strlen($line, "UTF-8");
  401. for ($j = 0; $j < $strlength; $j++) {
  402. // Take char at position $j
  403. $char = mb_substr($line, $j, 1, "UTF-8");
  404. if ((mb_strlen($newpara, "UTF-8") + mb_strlen($char, "UTF-8")) >= 75) {
  405. // CRLF + Space for cal
  406. $out .= $newpara."\r\n ";
  407. $newpara = "";
  408. }
  409. $newpara .= $char;
  410. }
  411. $out .= $newpara;
  412. } else {
  413. $strlength = dol_strlen($line);
  414. for ($j = 0; $j < $strlength; $j++) {
  415. // Take char at position $j
  416. $char = substr($line, $j, 1);
  417. if ((dol_strlen($newpara) + dol_strlen($char)) >= 75) {
  418. // CRLF + Space for cal
  419. $out .= $newpara."\r\n ";
  420. $newpara = "";
  421. }
  422. $newpara .= $char;
  423. }
  424. $out .= $newpara;
  425. }
  426. return trim($out);
  427. }
  428. /**
  429. * Encode into vcal format
  430. *
  431. * @param string $str String to convert
  432. * @param int $forcal (optional) 1 = For cal
  433. * @return string String converted
  434. */
  435. function quotedPrintEncode($str, $forcal = 0)
  436. {
  437. $lines = preg_split("/\r\n/", $str);
  438. $out = "";
  439. foreach ($lines as $line) {
  440. $newpara = "";
  441. // Do not use dol_strlen here, we need number of bytes
  442. $strlength = strlen($line);
  443. for ($j = 0; $j < $strlength; $j++) {
  444. $char = substr($line, $j, 1);
  445. $ascii = ord($char);
  446. if ($ascii < 32 || $ascii === 61 || $ascii > 126) {
  447. $char = "=".strtoupper(sprintf("%02X", $ascii));
  448. }
  449. // Do not use dol_strlen here, we need number of bytes
  450. if ((strlen($newpara) + strlen($char)) >= 76) {
  451. // New line with carray-return (CR) and line-feed (LF)
  452. $out .= $newpara."=\r\n";
  453. // extra space for cal
  454. if ($forcal) {
  455. $out .= " ";
  456. }
  457. $newpara = "";
  458. }
  459. $newpara .= $char;
  460. }
  461. $out .= $newpara;
  462. }
  463. return trim($out);
  464. }
  465. /**
  466. * Decode vcal format
  467. *
  468. * @param string $str String to convert
  469. * @return string String converted
  470. */
  471. function quotedPrintDecode($str)
  472. {
  473. return trim(quoted_printable_decode(preg_replace("/=\r?\n/", "", $str)));
  474. }