Merge pull request #15909 from eileenmcnaughton/payment
[civicrm-core.git] / CRM / Utils / ICalendar.php
CommitLineData
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 */
24class 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
TO
87 * Send the ICalendar to the browser with the specified content type
88 * - 'text/calendar' : used for downloaded ics file
89 * - 'text/plain' : used for iCal formatted feed
90 * - 'text/xml' : used for gData or rss formatted feeds
91 *
6a488035 92 *
77855840
TO
93 * @param string $calendar
94 * The calendar data to be published.
f4aaa82a 95 * @param string $content_type
77855840
TO
96 * @param string $charset
97 * The character set to use, defaults to 'us-ascii'.
98 * @param string $fileName
99 * The file name (for downloads).
100 * @param string $disposition
101 * How the file should be sent ('attachment' for downloads).
6a488035 102 */
00be9182 103 public static function send($calendar, $content_type = 'text/calendar', $charset = 'us-ascii', $fileName = NULL, $disposition = NULL) {
6a488035
TO
104 $config = CRM_Core_Config::singleton();
105 $lang = $config->lcMessages;
d42a224c
CW
106 CRM_Utils_System::setHttpHeader("Content-Language", $lang);
107 CRM_Utils_System::setHttpHeader("Content-Type", "$content_type; charset=$charset");
6a488035
TO
108
109 if ($content_type == 'text/calendar') {
d42a224c
CW
110 CRM_Utils_System::setHttpHeader('Content-Length', strlen($calendar));
111 CRM_Utils_System::setHttpHeader("Content-Disposition", "$disposition; filename=\"$fileName\"");
112 CRM_Utils_System::setHttpHeader("Pragma", "no-cache");
113 CRM_Utils_System::setHttpHeader("Expires", "0");
114 CRM_Utils_System::setHttpHeader("Cache-Control", "no-cache, must-revalidate");
6a488035
TO
115 }
116
117 echo $calendar;
118 }
96025800 119
6a488035 120}