INFRA-132 - CRM/Upgrade - phpcbf (plus fixup)
[civicrm-core.git] / CRM / Utils / ICalendar.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
32 * $Id$
33 *
34 */
35
36 /**
37 * @file
38 * API for event export in iCalendar format
39 * as outlined in Internet Calendaring and
40 * Scheduling Core Object Specification
41 *
42 */
43 class CRM_Utils_ICalendar {
44
45 /**
46 * Escape text elements for safe ICalendar use
47 *
48 * @param $text
49 * Text to escape.
50 *
51 * @return Escaped text
52 *
53 */
54 public static function formatText($text) {
55 $text = strip_tags($text);
56 $text = str_replace("\"", "DQUOTE", $text);
57 $text = str_replace("\\", "\\\\", $text);
58 $text = str_replace(",", "\,", $text);
59 $text = str_replace(";", "\;", $text);
60 $text = str_replace(array("\r\n", "\n", "\r"), "\\n ", $text);
61 $text = implode("\n ", str_split($text, 50));
62 return $text;
63 }
64
65 /**
66 * Escape date elements for safe ICalendar use
67 *
68 * @param $date
69 * Date to escape.
70 *
71 * @param bool $gdata
72 *
73 * @return Escaped date
74 */
75 public static function formatDate($date, $gdata = FALSE) {
76
77 if ($gdata) {
78 return date("Y-m-d\TH:i:s.000",
79 strtotime($date)
80 );
81 }
82 else {
83 return date("Ymd\THis",
84 strtotime($date)
85 );
86 }
87 }
88
89 /**
90 * Send the ICalendar to the browser with the specified content type
91 * - 'text/calendar' : used for downloaded ics file
92 * - 'text/plain' : used for iCal formatted feed
93 * - 'text/xml' : used for gData or rss formatted feeds
94 *
95 *
96 * @param string $calendar
97 * The calendar data to be published.
98 * @param string $content_type
99 * @param string $charset
100 * The character set to use, defaults to 'us-ascii'.
101 * @param string $fileName
102 * The file name (for downloads).
103 * @param string $disposition
104 * How the file should be sent ('attachment' for downloads).
105 *
106 * @return void
107 */
108 public static function send($calendar, $content_type = 'text/calendar', $charset = 'us-ascii', $fileName = NULL, $disposition = NULL) {
109 $config = CRM_Core_Config::singleton();
110 $lang = $config->lcMessages;
111 header("Content-Language: $lang");
112 // header( "Content-Type: $content_type; charset=$charset; profile=\"ICalendar\"" );
113 header("Content-Type: $content_type; charset=$charset");
114
115 if ($content_type == 'text/calendar') {
116 header('Content-Length: ' . strlen($calendar));
117 header("Content-Disposition: $disposition; filename=\"$fileName\"");
118 header("Pragma: no-cache");
119 header("Expires: 0");
120 header("Cache-Control: no-cache, must-revalidate");
121 }
122
123 echo $calendar;
124 }
125 }