Merge pull request #15944 from magnolia61/Sort_CMS_tables_alphabetically
[civicrm-core.git] / CRM / Utils / ICalendar.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * @file
20 * API for event export in iCalendar format
21 * as outlined in Internet Calendaring and
22 * Scheduling Core Object Specification
23 */
24 class CRM_Utils_ICalendar {
25
26 /**
27 * Escape text elements for safe ICalendar use.
28 *
29 * @param string $text
30 * Text to escape.
31 *
32 * @return string
33 */
34 public static function formatText($text) {
35 $text = strip_tags($text);
36 $text = str_replace("\\", "\\\\", $text);
37 $text = str_replace(',', '\,', $text);
38 $text = str_replace(';', '\;', $text);
39 $text = str_replace(["\r\n", "\n", "\r"], "\\n ", $text);
40 $text = implode("\n ", str_split($text, 50));
41 return $text;
42 }
43
44 /**
45 * Restore iCal formatted text to normal.
46 *
47 * @param string $text
48 * Text to unescape.
49 *
50 * @return string
51 */
52 public static function unformatText($text) {
53 $text = str_replace('\n ', "\n", $text);
54 $text = str_replace('\;', ';', $text);
55 $text = str_replace('\,', ',', $text);
56 $text = str_replace("\\\\", "\\", $text);
57 $text = str_replace("DQUOTE", "\"", $text);
58 return $text;
59 }
60
61 /**
62 * Escape date elements for safe ICalendar use.
63 *
64 * @param $date
65 * Date to escape.
66 *
67 * @param bool $gdata
68 *
69 * @return string
70 * Escaped date
71 */
72 public static function formatDate($date, $gdata = FALSE) {
73
74 if ($gdata) {
75 return date("Y-m-d\TH:i:s.000",
76 strtotime($date)
77 );
78 }
79 else {
80 return date("Ymd\THis",
81 strtotime($date)
82 );
83 }
84 }
85
86 /**
87 * Send the ICalendar to the browser with the specified content type
88 * - 'text/calendar' : used for downloaded ics file
89 * - 'text/plain' : used for iCal formatted feed
90 * - 'text/xml' : used for gData or rss formatted feeds
91 *
92 *
93 * @param string $calendar
94 * The calendar data to be published.
95 * @param string $content_type
96 * @param string $charset
97 * The character set to use, defaults to 'us-ascii'.
98 * @param string $fileName
99 * The file name (for downloads).
100 * @param string $disposition
101 * How the file should be sent ('attachment' for downloads).
102 */
103 public static function send($calendar, $content_type = 'text/calendar', $charset = 'us-ascii', $fileName = NULL, $disposition = NULL) {
104 $config = CRM_Core_Config::singleton();
105 $lang = $config->lcMessages;
106 CRM_Utils_System::setHttpHeader("Content-Language", $lang);
107 CRM_Utils_System::setHttpHeader("Content-Type", "$content_type; charset=$charset");
108
109 if ($content_type == 'text/calendar') {
110 CRM_Utils_System::setHttpHeader('Content-Length', strlen($calendar));
111 CRM_Utils_System::setHttpHeader("Content-Disposition", "$disposition; filename=\"$fileName\"");
112 CRM_Utils_System::setHttpHeader("Pragma", "no-cache");
113 CRM_Utils_System::setHttpHeader("Expires", "0");
114 CRM_Utils_System::setHttpHeader("Cache-Control", "no-cache, must-revalidate");
115 }
116
117 echo $calendar;
118 }
119
120 }