-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclass.ical.php
308 lines (272 loc) · 8.14 KB
/
class.ical.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<?php
/**
* ICAL CLASS
* OUTPUT AN ICS FILE TO BROWSER OR HARDDRIVE
*
* Please refer to http://tools.ietf.org/html/rfc5545 if infos are needed
*
* @author ANDY KAYL
* @version 0.2
* @license http://opensource.org/licenses/BSD-3-Clause
*
* Copyright (c) 2017, ND.K
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this list of conditions and
* the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
* following disclaimer in the documentation and/or other materials provided with the distribution.
*
* Neither the name of the <ORGANIZATION> nor the names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
class iCal {
/**
* CLASS CONSTRUCTOR
*/
public function __construct() {
$this->prodid = "PHP iCal Generator";
$this->alarmdesc = "You gotta do what you gotta do! You have a job soon...";
$this->alarmtrigger = 60;
$this->refresh_ttl = 60;
$this->status = "CONFIRMED";
$this->events = array();
$this->evnum = null;
return true;
}
/**
* START A NEW EVENT
* @var NONE
* @return BOOL TRUE
*/
public function NewEvent() {
$num = count($this->events);
$this->evnum =& $num;
$this->events[$num] = array(
"data" => array(),
"alarms" => array(),
"custom" => array()
);
return true;
}
/**
* SET PRODUCT ID
* @var STRING PRODUCT ID STRING
* @return BOOL TRUE
*/
public function SetProductID($string) {
$this->prodid = $string;
return true;
}
/**
* SET THE REFRESH INTERVAL
* @var INT MINUTES
* @return BOOL TRUE
*/
public function SetRefresh($minutes) {
$this->refresh_ttl = (int) $minutes;
return true;
}
/**
* ADD EVENT SUMMARY TEXT
* @var STRING SUMMARY TEXT
* @return BOOL TRUE
*/
public function SetTitle($string) {
$this->events[$this->evnum]['data']['title'] = preg_replace("/(<br[ ]?\/?>|\n)/", "\\n", $string);
return true;
}
/**
* ADD DESCRIPTION TEXT
* @var STRING DESCRIPTION
* @return BOOL TRUE
*/
public function SetDescription($string) {
$this->events[$this->evnum]['data']['description'] = preg_replace("/(<br[ ]?\/?>|\n)/", "\\n", $string);
return true;
}
/**
* ADD DATES TO EVENT
* @var STRING BEGIN DATE (Y-M-D H:S)
* @var STRING END DATE (Y-M-D H:S)
* @return BOOL TRUE
*/
public function SetDates($begin, $end) {
$this->events[$this->evnum]['data']['begin_date'] = date("Ymd\THis", strtotime($begin));
$this->events[$this->evnum]['data']['end_date'] = date("Ymd\THis", strtotime($end));
return true;
}
/**
* SET LOCATION
* @var STRING LOCATION NAME
* @return BOOL TRUE
*/
public function SetLocation($string) {
$this->events[$this->evnum]['data']['location'] = $string;
return true;
}
/**
* ADD CUSTOM ICAL PROPERTY
* @var STRING PROPERTY NAME
* @var STRING PROPERTY VALUE
* @return BOOL TRUE
*/
public function AddCustom($property, $value) {
$property = mb_strtoupper($property);
$this->events[$this->evnum]['custom'][$property] = $value;
return true;
}
/**
* SET ALARM
* @var STRING TYPE => DISPLAY (DEFAULT), AUDIO (UNSUPPORTED), EMAIL (UNSUPPORTED)
* @return BOOL TRUE
*/
public function SetAlarm($type="display") {
$type = mb_strtoupper($type);
$num = count($this->events[$this->evnum]['alarms']);
$this->alnum =& $num;
$this->events[$this->evnum]['alarms'][$this->alnum]['type'] = $type;
return true;
}
/**
* SET ALARM TEXT TO DISPLAY
* @var STRING MESSAGE TEXT
* @return BOOL TRUE
*/
public function SetAlarmText($string) {
$this->events[$this->evnum]['alarms'][$this->alnum]['description'] = $string;
return true;
}
/**
* SET ALARM TRIGGER
* @var INT NUMBER OF MINUTES BEFORE EVENT
* @return BOOL TRUE
*/
public function SetAlarmTrigger($minutes) {
$minutes = abs((int) $minutes);
$this->events[$this->evnum]['alarms'][$this->alnum]['trigger'] = $minutes;
return true;
}
/**
* SET EVENT STATUS
* @var STRING STATUS => TENTATIVE, CONFIRMED, CANCELLED
* @return BOOL TRUE
*/
public function SetStatus($status) {
$status = mb_strtoupper($status);
$this->events[$this->evnum]['data']['status'] = $status;
return true;
}
/**
* STORE CONTENT TO FILE
* @var STRING CALENDAR
* @var STRING FILENAME
* @return BOOL TRUE/FALSE
*/
private function StoreFile($content, $destfile) {
try {
$fh = fopen($destfile, "w");
if (!$fh) {
throw new Exception("file could not be created");
}
$ret_write = fwrite($fh, $content);
if (!$ret_write) {
throw new Exception("Could not write content to file");
}
fclose($fh);
return true;
}
catch (Exception $e) {
print $e->getMessage();
return false;
}
}
/**
* CLEAN AND FORMAT EVENT ENTRIES
* @VAR NONE
* @return BOOL TRUE
*/
private function EncodeValues() {
foreach($this->events as $i => $event) {
foreach($event['data'] as $key => $data) {
$this->events[$i]['data'][$key] = html_entity_decode($data);
}
foreach ($event['custom'] as $key => $custom) {
$this->events[$i]['custom'][$key] = html_entity_decode($custom);
}
foreach ($event['alarms'] as $j => $alarms) {
foreach ($alarms as $key => $data) {
$this->events[$i]['alarms'][$j][$key] = html_entity_decode($data);
}
}
}
return true;
}
/**
* WRITE ICAL DATA TO BROWSER OR FILE
* @var STRING DESTINATION FILE
* @return BOOL TRUE OR FALSE
*/
public function Write($destfile=false) {
$this->EncodeValues();
$cal = "BEGIN:VCALENDAR\n";
$cal .= "PRODID:-//{$this->prodid}//EN\n";
$cal .= "VERSION:2.0\n";
$cal .= "METHOD:PUBLISH\n";
$cal .= "X-PUBLISHED-TTL:PT{$this->refresh_ttl}M\n";
foreach ($this->events as $event) {
$cal .= "BEGIN:VEVENT\n";
$cal .= "CLASS:PUBLIC\n";
$cal .= "CREATED:".date("Ymd\THis", time())."\n";
$cal .= "SUMMARY:{$event['data']['title']}\n";
$status = (!empty($event['status'])) ? $event['status'] : mb_strtoupper($this->status);
$cal .= "STATUS:{$status}\n";
if (!empty($event['data']['description'])) {
$cal .= "DESCRIPTION:{$event['data']['description']}\n";
}
if (!empty($event['data']['location'])) {
$cal .= "LOCATION:{$event['data']['location']}\n";
}
$cal .= "DTSTART:{$event['data']['begin_date']}\n";
$cal .= "DTEND:{$event['data']['end_date']}\n";
$cal .= "DTSTAMP:".date("Ymd\THis", time())."\n";
$cal .= "UID:".md5(uniqid("", true))."\n";
foreach($event['custom'] as $prop => $val) {
$cal .= "{$prop}:{$val}\n";
}
foreach ($event['alarms'] as $alarms) {
$cal .= "BEGIN:VALARM\n";
$cal .= "ACTION:{$alarms['type']}\n";
$alarmtrigger = (!empty($alarms['trigger'])) ? $alarms['trigger'] : $this->alarmtrigger;
$cal .= "TRIGGER:-PT{$alarmtrigger}M\n";
$alarmdesc = (!empty($alarms['description'])) ? $alarms['description'] : $this->alarmdesc;
$cal .= "DESCRIPTION:{$alarmdesc}\n";
$cal .= "END:VALARM\n";
}
$cal .= "END:VEVENT\n";
}
$cal .= "END:VCALENDAR\n";
if (!$destfile) {
header("Content-Type: text/Calendar");
header("Content-Disposition: attachment; filename=events.ics");
print $cal;
return true;
}
else {
return $this->StoreFile($cal, $destfile);
}
}
}
?>