3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
20 * API for event export in iCalendar format
21 * as outlined in Internet Calendaring and
22 * Scheduling Core Object Specification
24 class CRM_Utils_ICalendar
{
27 * Escape text elements for safe ICalendar use.
31 * @param bool $keep_html
32 * Flag to retain HTML formatting
33 * @param int $position
34 * Column number of the start of the string in the ICal output - used to
35 * determine allowable length of the first line
39 public static function formatText($text, $keep_html = FALSE, int $position = 0) {
42 '{ <a [^>]+ \\b href=(?: "( [^"]+ )" | \'( [^\']+ )\' ) [^>]* > ( [^<]* ) </a> }xi',
47 '{ < / [^>]+ > \s* }',
52 '{ <(br|/tr|/div|/h[1-6]) (\s [^>]*)? > (\s* \n)? }xi',
57 '{ </p> (\s* \n)? }xi',
61 $text = strip_tags($text);
62 $text = html_entity_decode($text, ENT_QUOTES | ENT_HTML401
, 'UTF-8');
65 $text = str_replace("\\", "\\\\", $text);
66 $text = str_replace(',', '\,', $text);
67 $text = str_replace(';', '\;', $text);
68 $text = str_replace(["\r\n", "\n", "\r"], "\\n ", $text);
70 // Remove this check after PHP 7.4 becomes a minimum requirement
71 $str_split = function_exists('mb_str_split') ?
'mb_str_split' : 'str_split';
74 $text = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"><html><body>' . $text . '</body></html>';
78 $prefixlen = max(50 - $position, 0);
79 $prefix = mb_substr($text, 0, $prefixlen) . "\n ";
80 $text = mb_substr($text, $prefixlen);
82 $text = $prefix . implode("\n ", $str_split($text, 50));
87 * Restore iCal formatted text to normal.
94 public static function unformatText($text) {
95 $text = str_replace("\n ", "", $text);
96 $text = str_replace('\n ', "\n", $text);
97 $text = str_replace('\;', ';', $text);
98 $text = str_replace('\,', ',', $text);
99 $text = str_replace("\\\\", "\\", $text);
100 $text = str_replace("DQUOTE", "\"", $text);
105 * Escape date elements for safe ICalendar use.
107 * @param string $date
115 public static function formatDate($date, $gdata = FALSE) {
118 return date("Y-m-d\TH:i:s.000",
123 return date("Ymd\THis",
130 * Send the ICalendar to the browser with the specified content type
131 * - 'text/calendar' : used for iCal formatted feed
132 * - 'text/xml' : used for gData or rss formatted feeds
135 * @param string $calendar
136 * The calendar data to be published.
137 * @param string $content_type
138 * @param string $charset
139 * The character set to use, defaults to 'us-ascii'.
140 * @param string $fileName
141 * The file name (for downloads).
142 * @param string $disposition
143 * How the file should be sent ('attachment' for downloads).
145 public static function send($calendar, $content_type = 'text/calendar', $charset = 'us-ascii', $fileName = NULL, $disposition = NULL) {
146 $config = CRM_Core_Config
::singleton();
147 $lang = $config->lcMessages
;
148 CRM_Utils_System
::setHttpHeader("Content-Language", $lang);
149 CRM_Utils_System
::setHttpHeader("Content-Type", "$content_type; charset=$charset");
152 CRM_Utils_System
::setHttpHeader('Content-Length', strlen($calendar));
153 CRM_Utils_System
::setHttpHeader("Content-Disposition", "$disposition; filename=\"$fileName\"");
154 CRM_Utils_System
::setHttpHeader("Pragma", "no-cache");
155 CRM_Utils_System
::setHttpHeader("Expires", "0");
156 CRM_Utils_System
::setHttpHeader("Cache-Control", "no-cache, must-revalidate");
163 * @param array $timezones - Timezone strings
169 public static function generate_timezones(array $timezones, $date_min, $date_max) {
170 if (empty($timezones)) {
176 foreach ($timezones as $tzstr) {
177 $timezone = new DateTimeZone($tzstr);
179 $transitions = $timezone->getTransitions($date_min, $date_max);
181 if (count($transitions) === 1) {
182 $transitions[] = array_values($transitions)[0];
186 'id' => $timezone->getName(),
190 $last_transition = array_shift($transitions);
192 foreach ($transitions as $transition) {
193 $item['transitions'][] = [
194 'type' => $transition['isdst'] ?
'DAYLIGHT' : 'STANDARD',
195 'offset_from' => self
::format_tz_offset($last_transition['offset']),
196 'offset_to' => self
::format_tz_offset($transition['offset']),
197 'abbr' => $transition['abbr'],
198 'dtstart' => date_create($transition['time'], $timezone)->format("Ymd\THis"),
201 $last_transition = $transition;
210 protected static function format_tz_offset($offset) {
212 $hours = intval($offset / 60);
213 $minutes = abs(intval($offset %
60));
215 return sprintf('%+03d%02d', $hours, $minutes);