Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
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 | */ | |
43 | class CRM_Utils_ICalendar { | |
44 | ||
45 | /** | |
46 | * Escape text elements for safe ICalendar use | |
47 | * | |
48 | * @param $text Text to escape | |
49 | * | |
50 | * @return Escaped text | |
51 | * | |
52 | */ | |
53 | static function formatText($text) { | |
54 | $text = strip_tags($text); | |
55 | $text = str_replace("\"", "DQUOTE", $text); | |
56 | $text = str_replace("\\", "\\\\", $text); | |
57 | $text = str_replace(",", "\,", $text); | |
58 | $text = str_replace(";", "\;", $text); | |
59 | $text = str_replace(array("\r\n", "\n", "\r"), "\\n ", $text); | |
60 | $text = implode("\n ", str_split($text, 50)); | |
61 | return $text; | |
62 | } | |
63 | ||
64 | /** | |
65 | * Escape date elements for safe ICalendar use | |
66 | * | |
67 | * @param $date Date to escape | |
68 | * | |
f4aaa82a | 69 | * @param bool $gdata |
6a488035 | 70 | * |
f4aaa82a | 71 | * @return Escaped date |
6a488035 TO |
72 | */ |
73 | static function formatDate($date, $gdata = FALSE) { | |
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 | /** | |
88 | * | |
89 | * Send the ICalendar to the browser with the specified content type | |
90 | * - 'text/calendar' : used for downloaded ics file | |
91 | * - 'text/plain' : used for iCal formatted feed | |
92 | * - 'text/xml' : used for gData or rss formatted feeds | |
93 | * | |
94 | * @access public | |
95 | * | |
f4aaa82a | 96 | * @param string $calendar The calendar data to be published. |
6a488035 | 97 | * |
f4aaa82a | 98 | * @param string $content_type |
6a488035 TO |
99 | * |
100 | * @param string $charset The character set to use, defaults to | |
101 | * 'us-ascii'. | |
102 | * | |
f4aaa82a EM |
103 | * @param null $fileName |
104 | * @param string $disposition How the file should be sent ('attachment' for downloads) | |
6a488035 | 105 | * |
f4aaa82a | 106 | * @internal param string $filename The file name (for downloads) |
6a488035 | 107 | * |
f4aaa82a | 108 | * @return void |
6a488035 | 109 | */ |
39eb89f4 | 110 | static function send($calendar, $content_type = 'text/calendar', $charset = 'us-ascii', $fileName = NULL, $disposition = NULL) { |
6a488035 TO |
111 | $config = CRM_Core_Config::singleton(); |
112 | $lang = $config->lcMessages; | |
113 | header("Content-Language: $lang"); | |
114 | // header( "Content-Type: $content_type; charset=$charset; profile=\"ICalendar\"" ); | |
115 | header("Content-Type: $content_type; charset=$charset"); | |
116 | ||
117 | if ($content_type == 'text/calendar') { | |
118 | header('Content-Length: ' . strlen($calendar)); | |
119 | header("Content-Disposition: $disposition; filename=\"$fileName\""); | |
120 | header("Pragma: no-cache"); | |
121 | header("Expires: 0"); | |
122 | header("Cache-Control: no-cache, must-revalidate"); | |
123 | } | |
124 | ||
125 | echo $calendar; | |
126 | } | |
127 | } | |
128 |