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