This repository has been archived by the owner on May 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontaoIcalExport.php
288 lines (252 loc) · 9.08 KB
/
contaoIcalExport.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<?php
namespace contao;
/**
* Run this file as script
* (procedural at the end will executed)
*/
if (!defined(runContaoIcalExportAsScript)) define('runContaoIcalExportAsScript', true);
/**
* ICAL-Event-Export for Contao 3.3
* Sends the given event-id (contaoIcalExport.php?eventId=XX) als iCal-File to the browser
* (other contao-versions may be compatible too)
*
* Installation
* - Download file and store it to htdocs/httpdocs (contao-home)
* - Test it with: http://$DOMAIN.TLD/contaoIcalExport.php?eventId=XX
* - Implement iCal-Download-Button to the templates/event_full.html5 or wherever u
* need it by adding
* <a href="/contaoIcalExport.php?eventId=<?=$this->id?>">Download as iCal-File</a>
*
* Thanks to
* - PhpStorm for the great ide
* - Jake Bellacera for his draft: PHPtoICS.php
* https://gist.github.com/jakebellacera/635416
* - Hugo Wetterberg for his function ical_split
* https://gist.github.com/hugowetterberg/81747
* - Steven N. Severinghaus for his iCalendar Validator
* http://severinghaus.org/projects/icv/
* - iCalendar.org for its iCalendar Validator
* http://icalendar.org/component/com_icalvalidator/
*
* @author Andreas Doebeling <[email protected]>
* @copyright 1601.production siegler&thuemmler ohg
* @license cc-by-sa https://creativecommons.org/licenses/by-sa/3.0/
*
* @link https://github.com/ADoebeling/contaoIcalExport
* @link https://xing.doebeling.de
*
* @version 0.1.160412_1ad
*/
class icalExport
{
/**
* @var integer the id of the event
*/
protected $eventId = 0;
/**
* @var string text title of the event
*/
protected $summary = '';
/**
* @var integer the starting date (in seconds since unix epoch)
*/
protected $dateStart = 0;
/**
* @var integer the ending date (in seconds since unix epoch)
*/
protected $dateEnd = 0;
/**
* @var string the event's address
*/
protected $address = '';
/**
* @var string the URL of the event (add http://)
*/
protected $uri = '';
/**
* @var string text description of the event
*/
protected $description = '';
/**
* @var string the name of this file for saving (e.g. my-event-name.ics)
*/
protected $filename = '';
/**
* @var \mysqli
*/
protected $mysqli;
/**
* icalExport constructor.
*
* @param $dbHost
* @param $dbUser
* @param $dbPass
* @param $dbDatabase
*/
public function __construct($dbHost, $dbUser, $dbPass, $dbDatabase)
{
$this->mysqli = new \mysqli($dbHost, $dbUser, $dbPass, $dbDatabase);
if ($this->mysqli->errno)
{
printf("Connect failed: %s\n", mysqli_connect_error());
die();
}
}
/**
* Get single event
*
* @param integer $eventId
* @return $this
* @throws \Exception
*/
public function get($eventId)
{
$eventId = intval($eventId);
$sql = "
SELECT
id,
title as summery,
concat(alias, '.ics') as filename,
location as address,
teaser as description,
-- dateStart
if(addTime = 1,
-- Time is given
DATE_FORMAT(from_unixtime(startTime), 'DTSTART;TZID=Europe/Berlin:%Y%m%dT%H%i%s'),
-- Only date is given
DATE_FORMAT(from_unixtime(startDate), 'DTSTART;VALUE=DATE:%Y%m%d')
) as dateStart,
-- dateEnd
if(addTime = 1,
-- Time is given
if(endTime = 0 || endTime <= startTime || DATE_FORMAT(from_unixtime(endTime), '%H%i') = '2323',
-- Time is given but 0, wrong OR 23:23 (what we use as identifier for unlimited events)
DATE_FORMAT(from_unixtime(startTime + 2 * 60 * 60), 'DTEND;TZID=Europe/Berlin:%Y%m%dT%H%i%s'),
-- Time is given and correct
DATE_FORMAT(from_unixtime(endTime), 'DTEND;TZID=Europe/Berlin:%Y%m%dT%H%i%s')
),
-- Only date is given
if(endDate != 0 && endDate > startDate,
-- EndDate is correkt
DATE_FORMAT(from_unixtime(endDate + 24 * 60 * 60), 'DTEND;VALUE=DATE:%Y%m%d'),
-- EndDate is not correkt, take the startDate
DATE_FORMAT(from_unixtime(startDate + 24 * 60 * 60), 'DTEND;VALUE=DATE:%Y%m%d')
)
) as dateEnd
FROM
tl_calendar_events
WHERE
id = $eventId AND
published = 1
LIMIT 1";
$query = $this->mysqli->query($sql);
if ($query->num_rows !== 1)
{
throw new \Exception("EventId not found\n$sql");
}
else
{
$result = $query->fetch_assoc();
$this->dateStart = $result['dateStart'];
$this->dateEnd = $result['dateEnd'];
$this->address = $result['address'];
$this->description = strip_tags($result['description']);
$this->filename = strip_tags($result['filename']);
$this->summary = strip_tags(html_entity_decode($result['summery']));
$this->uri = $_SERVER[HTTP_REFERER];
return $this;
}
}
/**
* Send ical-file as download
*/
public function sendDownload()
{
$ics ="BEGIN:VCALENDAR\r\n";
$ics .="VERSION:2.0\r\n";
$ics .="PRODID:-//hacksw/handcal//NONSGML v1.0//EN\r\n";
$ics .="CALSCALE:GREGORIAN\r\n";
// Timezone-settings borrowed from
// http://pcal.gedaechtniskirche.com/termine/index.php?cal=kwg-probenplan&ics
$ics .= "BEGIN:VTIMEZONE\r\n";
$ics .= "TZID:Europe/Berlin\r\n";
$ics .= "BEGIN:DAYLIGHT\r\n";
$ics .= "TZOFFSETFROM:+0100\r\n";
$ics .= "DTSTART:19810329T020000\r\n";
$ics .= "RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU\r\n";
$ics .= "TZNAME:MESZ\r\n";
$ics .= "END:DAYLIGHT\r\n";
$ics .= "BEGIN:STANDARD\r\n";
$ics .= "TZOFFSETFROM:+0200\r\n";
$ics .= "DTSTART:19961027T030000\r\n";
$ics .= "RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU\r\n";
$ics .= "TZNAME:MEZ\r\n";
$ics .= "END:STANDARD\r\n";
$ics .= "END:VTIMEZONE\r\n";
$ics .="BEGIN:VEVENT\r\n";
$ics .=$this->dateEnd."\r\n";
$ics .="UID:".$this->eventId."\r\n";
$ics .="DTSTAMP:".$this->dateStart."\r\n";
$ics .="LOCATION:".$this->address."\r\n";
$ics .= 'DESCRIPTION:'.$this->getIcalSplit('DESCRIPTION', $this->description)."\r\n";
$ics .="URL;VALUE=URI:".$this->uri."\r\n";
$ics .= 'SUMMARY:'.$this->getIcalSplit('SUMMARY', $this->summary)."\r\n";
$ics .=$this->dateStart."\r\n";
$ics .="END:VEVENT\r\n";
$ics .="END:VCALENDAR\r\n";
header('Content-type: text/calendar; charset=utf-8');
header('Content-Disposition: attachment; filename=' . $this->filename);
echo utf8_encode($ics);
}
/**
* @author Hugo Wetterberg <[email protected]>
* @link https://gist.github.com/hugowetterberg/81747
*
* @param $preamble
* @param $value
* @return string
*/
public function getIcalSplit($preamble, $value) {
return $value; // disabled
$value = trim($value);
$value = strip_tags($value);
$value = preg_replace('/\n+/', ' ', $value);
$value = preg_replace('/\s{2,}/', ' ', $value);
$preamble_len = strlen($preamble);
$lines = array();
while (strlen($value)>(75-$preamble_len)) {
$space = (75-$preamble_len);
$mbcc = $space;
while ($mbcc) {
$line = mb_substr($value, 0, $mbcc);
$oct = strlen($line);
if ($oct > $space) {
$mbcc -= $oct-$space;
}
else {
$lines[] = $line;
$preamble_len = 1; // Still take the tab into account
$value = mb_substr($value, $mbcc);
break;
}
}
}
if (!empty($value)) {
$lines[] = $value;
}
return join($lines, "\n\t");
}
}
/******************************************************************************
******************************************************************************/
if (runContaoIcalExportAsScript && isset($_GET['eventId']) && intval($_GET['eventId']))
{
require_once 'system/config/localconfig.php';
$cie = new icalExport(
$GLOBALS['TL_CONFIG']['dbHost'],
$GLOBALS['TL_CONFIG']['dbUser'],
$GLOBALS['TL_CONFIG']['dbPass'],
$GLOBALS['TL_CONFIG']['dbDatabase']
);
$cie->get($_GET['eventId'])->sendDownload();
}