Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
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 | |
6a488035 TO |
23 | */ |
24 | class CRM_Utils_ICalendar { | |
25 | ||
26 | /** | |
fe482240 | 27 | * Escape text elements for safe ICalendar use. |
6a488035 | 28 | * |
046dd6c2 | 29 | * @param string $text |
77855840 | 30 | * Text to escape. |
6a488035 | 31 | * |
72b3a70c | 32 | * @return string |
6a488035 | 33 | */ |
00be9182 | 34 | public static function formatText($text) { |
6a488035 | 35 | $text = strip_tags($text); |
6a488035 | 36 | $text = str_replace("\\", "\\\\", $text); |
046dd6c2 CW |
37 | $text = str_replace(',', '\,', $text); |
38 | $text = str_replace(';', '\;', $text); | |
be2fb01f | 39 | $text = str_replace(["\r\n", "\n", "\r"], "\\n ", $text); |
6a488035 TO |
40 | $text = implode("\n ", str_split($text, 50)); |
41 | return $text; | |
42 | } | |
43 | ||
046dd6c2 CW |
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 | ||
6a488035 | 61 | /** |
fe482240 | 62 | * Escape date elements for safe ICalendar use. |
6a488035 | 63 | * |
77855840 TO |
64 | * @param $date |
65 | * Date to escape. | |
6a488035 | 66 | * |
f4aaa82a | 67 | * @param bool $gdata |
6a488035 | 68 | * |
72b3a70c CW |
69 | * @return string |
70 | * Escaped date | |
6a488035 | 71 | */ |
00be9182 | 72 | public static function formatDate($date, $gdata = FALSE) { |
6a488035 TO |
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 | /** | |
6a488035 | 87 | * Send the ICalendar to the browser with the specified content type |
cb087737 | 88 | * - 'text/calendar' : used for iCal formatted feed |
6a488035 TO |
89 | * - 'text/xml' : used for gData or rss formatted feeds |
90 | * | |
6a488035 | 91 | * |
77855840 TO |
92 | * @param string $calendar |
93 | * The calendar data to be published. | |
f4aaa82a | 94 | * @param string $content_type |
77855840 TO |
95 | * @param string $charset |
96 | * The character set to use, defaults to 'us-ascii'. | |
97 | * @param string $fileName | |
98 | * The file name (for downloads). | |
99 | * @param string $disposition | |
100 | * How the file should be sent ('attachment' for downloads). | |
6a488035 | 101 | */ |
00be9182 | 102 | public static function send($calendar, $content_type = 'text/calendar', $charset = 'us-ascii', $fileName = NULL, $disposition = NULL) { |
6a488035 TO |
103 | $config = CRM_Core_Config::singleton(); |
104 | $lang = $config->lcMessages; | |
d42a224c CW |
105 | CRM_Utils_System::setHttpHeader("Content-Language", $lang); |
106 | CRM_Utils_System::setHttpHeader("Content-Type", "$content_type; charset=$charset"); | |
6a488035 | 107 | |
cb087737 | 108 | if ($fileName) { |
d42a224c CW |
109 | CRM_Utils_System::setHttpHeader('Content-Length', strlen($calendar)); |
110 | CRM_Utils_System::setHttpHeader("Content-Disposition", "$disposition; filename=\"$fileName\""); | |
111 | CRM_Utils_System::setHttpHeader("Pragma", "no-cache"); | |
112 | CRM_Utils_System::setHttpHeader("Expires", "0"); | |
113 | CRM_Utils_System::setHttpHeader("Cache-Control", "no-cache, must-revalidate"); | |
6a488035 TO |
114 | } |
115 | ||
116 | echo $calendar; | |
117 | } | |
96025800 | 118 | |
6a488035 | 119 | } |