Merge pull request #19554 from colemanw/searchFields
[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 iCal formatted feed
89 * - 'text/xml' : used for gData or rss formatted feeds
90 *
91 *
92 * @param string $calendar
93 * The calendar data to be published.
94 * @param string $content_type
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).
101 */
102 public static function send($calendar, $content_type = 'text/calendar', $charset = 'us-ascii', $fileName = NULL, $disposition = NULL) {
103 $config = CRM_Core_Config::singleton();
104 $lang = $config->lcMessages;
105 CRM_Utils_System::setHttpHeader("Content-Language", $lang);
106 CRM_Utils_System::setHttpHeader("Content-Type", "$content_type; charset=$charset");
107
108 if ($fileName) {
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");
114 }
115
116 echo $calendar;
117 }
118
119 }