Fix visibility on legacy functions
[civicrm-core.git] / CRM / Utils / ICalendar.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2017
32 */
33
34 /**
35 * @file
36 * API for event export in iCalendar format
37 * as outlined in Internet Calendaring and
38 * Scheduling Core Object Specification
39 */
40 class CRM_Utils_ICalendar {
41
42 /**
43 * Escape text elements for safe ICalendar use.
44 *
45 * @param string $text
46 * Text to escape.
47 *
48 * @return string
49 */
50 public static function formatText($text) {
51 $text = strip_tags($text);
52 $text = str_replace("\"", "DQUOTE", $text);
53 $text = str_replace("\\", "\\\\", $text);
54 $text = str_replace(',', '\,', $text);
55 $text = str_replace(';', '\;', $text);
56 $text = str_replace(array("\r\n", "\n", "\r"), "\\n ", $text);
57 $text = implode("\n ", str_split($text, 50));
58 return $text;
59 }
60
61 /**
62 * Restore iCal formatted text to normal.
63 *
64 * @param string $text
65 * Text to unescape.
66 *
67 * @return string
68 */
69 public static function unformatText($text) {
70 $text = str_replace('\n ', "\n", $text);
71 $text = str_replace('\;', ';', $text);
72 $text = str_replace('\,', ',', $text);
73 $text = str_replace("\\\\", "\\", $text);
74 $text = str_replace("DQUOTE", "\"", $text);
75 return $text;
76 }
77
78 /**
79 * Escape date elements for safe ICalendar use.
80 *
81 * @param $date
82 * Date to escape.
83 *
84 * @param bool $gdata
85 *
86 * @return string
87 * Escaped date
88 */
89 public static function formatDate($date, $gdata = FALSE) {
90
91 if ($gdata) {
92 return date("Y-m-d\TH:i:s.000",
93 strtotime($date)
94 );
95 }
96 else {
97 return date("Ymd\THis",
98 strtotime($date)
99 );
100 }
101 }
102
103 /**
104 * Send the ICalendar to the browser with the specified content type
105 * - 'text/calendar' : used for downloaded ics file
106 * - 'text/plain' : used for iCal formatted feed
107 * - 'text/xml' : used for gData or rss formatted feeds
108 *
109 *
110 * @param string $calendar
111 * The calendar data to be published.
112 * @param string $content_type
113 * @param string $charset
114 * The character set to use, defaults to 'us-ascii'.
115 * @param string $fileName
116 * The file name (for downloads).
117 * @param string $disposition
118 * How the file should be sent ('attachment' for downloads).
119 */
120 public static function send($calendar, $content_type = 'text/calendar', $charset = 'us-ascii', $fileName = NULL, $disposition = NULL) {
121 $config = CRM_Core_Config::singleton();
122 $lang = $config->lcMessages;
123 CRM_Utils_System::setHttpHeader("Content-Language", $lang);
124 CRM_Utils_System::setHttpHeader("Content-Type", "$content_type; charset=$charset");
125
126 if ($content_type == 'text/calendar') {
127 CRM_Utils_System::setHttpHeader('Content-Length', strlen($calendar));
128 CRM_Utils_System::setHttpHeader("Content-Disposition", "$disposition; filename=\"$fileName\"");
129 CRM_Utils_System::setHttpHeader("Pragma", "no-cache");
130 CRM_Utils_System::setHttpHeader("Expires", "0");
131 CRM_Utils_System::setHttpHeader("Cache-Control", "no-cache, must-revalidate");
132 }
133
134 echo $calendar;
135 }
136
137 }