Merge pull request #12162 from seamuslee001/group_query_count
[civicrm-core.git] / CRM / Utils / ICalendar.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
8c9251b3 6 | Copyright CiviCRM LLC (c) 2004-2018 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
8c9251b3 31 * @copyright CiviCRM LLC (c) 2004-2018
6a488035
TO
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
6a488035
TO
39 */
40class CRM_Utils_ICalendar {
41
42 /**
fe482240 43 * Escape text elements for safe ICalendar use.
6a488035 44 *
046dd6c2 45 * @param string $text
77855840 46 * Text to escape.
6a488035 47 *
72b3a70c 48 * @return string
6a488035 49 */
00be9182 50 public static function formatText($text) {
6a488035 51 $text = strip_tags($text);
6a488035 52 $text = str_replace("\\", "\\\\", $text);
046dd6c2
CW
53 $text = str_replace(',', '\,', $text);
54 $text = str_replace(';', '\;', $text);
6a488035
TO
55 $text = str_replace(array("\r\n", "\n", "\r"), "\\n ", $text);
56 $text = implode("\n ", str_split($text, 50));
57 return $text;
58 }
59
046dd6c2
CW
60 /**
61 * Restore iCal formatted text to normal.
62 *
63 * @param string $text
64 * Text to unescape.
65 *
66 * @return string
67 */
68 public static function unformatText($text) {
69 $text = str_replace('\n ', "\n", $text);
70 $text = str_replace('\;', ';', $text);
71 $text = str_replace('\,', ',', $text);
72 $text = str_replace("\\\\", "\\", $text);
73 $text = str_replace("DQUOTE", "\"", $text);
74 return $text;
75 }
76
6a488035 77 /**
fe482240 78 * Escape date elements for safe ICalendar use.
6a488035 79 *
77855840
TO
80 * @param $date
81 * Date to escape.
6a488035 82 *
f4aaa82a 83 * @param bool $gdata
6a488035 84 *
72b3a70c
CW
85 * @return string
86 * Escaped date
6a488035 87 */
00be9182 88 public static function formatDate($date, $gdata = FALSE) {
6a488035
TO
89
90 if ($gdata) {
91 return date("Y-m-d\TH:i:s.000",
92 strtotime($date)
93 );
94 }
95 else {
96 return date("Ymd\THis",
97 strtotime($date)
98 );
99 }
100 }
101
102 /**
6a488035
TO
103 * Send the ICalendar to the browser with the specified content type
104 * - 'text/calendar' : used for downloaded ics file
105 * - 'text/plain' : used for iCal formatted feed
106 * - 'text/xml' : used for gData or rss formatted feeds
107 *
6a488035 108 *
77855840
TO
109 * @param string $calendar
110 * The calendar data to be published.
f4aaa82a 111 * @param string $content_type
77855840
TO
112 * @param string $charset
113 * The character set to use, defaults to 'us-ascii'.
114 * @param string $fileName
115 * The file name (for downloads).
116 * @param string $disposition
117 * How the file should be sent ('attachment' for downloads).
6a488035 118 */
00be9182 119 public static function send($calendar, $content_type = 'text/calendar', $charset = 'us-ascii', $fileName = NULL, $disposition = NULL) {
6a488035
TO
120 $config = CRM_Core_Config::singleton();
121 $lang = $config->lcMessages;
d42a224c
CW
122 CRM_Utils_System::setHttpHeader("Content-Language", $lang);
123 CRM_Utils_System::setHttpHeader("Content-Type", "$content_type; charset=$charset");
6a488035
TO
124
125 if ($content_type == 'text/calendar') {
d42a224c
CW
126 CRM_Utils_System::setHttpHeader('Content-Length', strlen($calendar));
127 CRM_Utils_System::setHttpHeader("Content-Disposition", "$disposition; filename=\"$fileName\"");
128 CRM_Utils_System::setHttpHeader("Pragma", "no-cache");
129 CRM_Utils_System::setHttpHeader("Expires", "0");
130 CRM_Utils_System::setHttpHeader("Cache-Control", "no-cache, must-revalidate");
6a488035
TO
131 }
132
133 echo $calendar;
134 }
96025800 135
6a488035 136}