|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* EasyPeasyICS Simple ICS/vCal data generator. |
|
4
|
|
|
* @author Marcus Bointon <[email protected]> |
|
5
|
|
|
* @author Manuel Reinhard <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* Built with inspiration from |
|
8
|
|
|
* http://stackoverflow.com/questions/1463480/how-can-i-use-php-to-dynamically-publish-an-ical-file-to-be-read-by-google-calend/1464355#1464355 |
|
9
|
|
|
* History: |
|
10
|
|
|
* 2010/12/17 - Manuel Reinhard - when it all started |
|
11
|
|
|
* 2014 PHPMailer project becomes maintainer |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class EasyPeasyICS. |
|
16
|
|
|
* Simple ICS data generator |
|
17
|
|
|
* @package phpmailer |
|
18
|
|
|
* @subpackage easypeasyics |
|
19
|
|
|
*/ |
|
20
|
|
|
class EasyPeasyICS |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* The name of the calendar |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $calendarName; |
|
27
|
|
|
/** |
|
28
|
|
|
* The array of events to add to this calendar |
|
29
|
|
|
* @var array |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $events = []; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Constructor |
|
35
|
|
|
* @param string $calendarName |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct($calendarName = '') |
|
38
|
|
|
{ |
|
39
|
|
|
$this->calendarName = $calendarName; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Add an event to this calendar. |
|
44
|
|
|
* @param string $start The start date and time as a unix timestamp |
|
45
|
|
|
* @param string $end The end date and time as a unix timestamp |
|
46
|
|
|
* @param string $summary A summary or title for the event |
|
47
|
|
|
* @param string $description A description of the event |
|
48
|
|
|
* @param string $url A URL for the event |
|
49
|
|
|
* @param string $uid A unique identifier for the event - generated automatically if not provided |
|
50
|
|
|
* @return array An array of event details, including any generated UID |
|
51
|
|
|
*/ |
|
52
|
|
|
public function addEvent($start, $end, $summary = '', $description = '', $url = '', $uid = '') |
|
53
|
|
|
{ |
|
54
|
|
|
if (empty($uid)) { |
|
55
|
|
|
$uid = md5(uniqid(mt_rand(), true)) . '@EasyPeasyICS'; |
|
56
|
|
|
} |
|
57
|
|
|
$event = [ |
|
58
|
|
|
'start' => gmdate('Ymd', $start) . 'T' . gmdate('His', $start) . 'Z', |
|
59
|
|
|
'end' => gmdate('Ymd', $end) . 'T' . gmdate('His', $end) . 'Z', |
|
60
|
|
|
'summary' => $summary, |
|
61
|
|
|
'description' => $description, |
|
62
|
|
|
'url' => $url, |
|
63
|
|
|
'uid' => $uid, |
|
64
|
|
|
]; |
|
65
|
|
|
$this->events[] = $event; |
|
66
|
|
|
|
|
67
|
|
|
return $event; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @return array Get the array of events. |
|
72
|
|
|
*/ |
|
73
|
|
|
public function getEvents() |
|
74
|
|
|
{ |
|
75
|
|
|
return $this->events; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Clear all events. |
|
80
|
|
|
*/ |
|
81
|
|
|
public function clearEvents() |
|
82
|
|
|
{ |
|
83
|
|
|
$this->events = []; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Get the name of the calendar. |
|
88
|
|
|
* @return string |
|
89
|
|
|
*/ |
|
90
|
|
|
public function getName() |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->calendarName; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Set the name of the calendar. |
|
97
|
|
|
* @param $name |
|
98
|
|
|
*/ |
|
99
|
|
|
public function setName($name) |
|
100
|
|
|
{ |
|
101
|
|
|
$this->calendarName = $name; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Render and optionally output a vcal string. |
|
106
|
|
|
* @param bool $output Whether to output the calendar data directly (the default). |
|
107
|
|
|
* @return string The complete rendered vlal |
|
108
|
|
|
*/ |
|
109
|
|
|
public function render($output = true) |
|
110
|
|
|
{ |
|
111
|
|
|
//Add header |
|
112
|
|
|
$ics = 'BEGIN:VCALENDAR |
|
113
|
|
|
METHOD:PUBLISH |
|
114
|
|
|
VERSION:2.0 |
|
115
|
|
|
X-WR-CALNAME:' . $this->calendarName . ' |
|
116
|
|
|
PRODID:-//hacksw/handcal//NONSGML v1.0//EN'; |
|
117
|
|
|
|
|
118
|
|
|
//Add events |
|
119
|
|
|
foreach ($this->events as $event) { |
|
120
|
|
|
$ics .= ' |
|
121
|
|
|
BEGIN:VEVENT |
|
122
|
|
|
UID:' . $event['uid'] . ' |
|
123
|
|
|
DTSTAMP:' . gmdate('Ymd') . 'T' . gmdate('His') . 'Z |
|
124
|
|
|
DTSTART:' . $event['start'] . ' |
|
125
|
|
|
DTEND:' . $event['end'] . ' |
|
126
|
|
|
SUMMARY:' . str_replace("\n", '\\n', $event['summary']) . ' |
|
127
|
|
|
DESCRIPTION:' . str_replace("\n", '\\n', $event['description']) . ' |
|
128
|
|
|
URL;VALUE=URI:' . $event['url'] . ' |
|
129
|
|
|
END:VEVENT'; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
//Add footer |
|
133
|
|
|
$ics .= ' |
|
134
|
|
|
END:VCALENDAR'; |
|
135
|
|
|
|
|
136
|
|
|
if ($output) { |
|
137
|
|
|
//Output |
|
138
|
|
|
$filename = $this->calendarName; |
|
139
|
|
|
//Filename needs quoting if it contains spaces |
|
140
|
|
|
if (false !== mb_strpos($filename, ' ')) { |
|
141
|
|
|
$filename = '"' . $filename . '"'; |
|
142
|
|
|
} |
|
143
|
|
|
header('Content-type: text/calendar; charset=utf-8'); |
|
144
|
|
|
header('Content-Disposition: inline; filename=' . $filename . '.ics'); |
|
145
|
|
|
echo $ics; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
return $ics; |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|