xcal.lib.php 17 KB

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