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