1 <?php
2 /**
3 * EasyPeasyICS Simple ICS/vCal data generator.
4 * @author Marcus Bointon <phpmailer@synchromedia.co.uk>
5 * @author Manuel Reinhard <manu@sprain.ch>
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 = array();
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 = array(
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 return $event;
67 }
68
69 /**
70 * @return array Get the array of events.
71 */
72 public function getEvents()
73 {
74 return $this->events;
75 }
76
77 /**
78 * Clear all events.
79 */
80 public function clearEvents()
81 {
82 $this->events = array();
83 }
84
85 /**
86 * Get the name of the calendar.
87 * @return string
88 */
89 public function getName()
90 {
91 return $this->calendarName;
92 }
93
94 /**
95 * Set the name of the calendar.
96 * @param $name
97 */
98 public function setName($name)
99 {
100 $this->calendarName = $name;
101 }
102
103 /**
104 * Render and optionally output a vcal string.
105 * @param bool $output Whether to output the calendar data directly (the default).
106 * @return string The complete rendered vlal
107 */
108 public function render($output = true)
109 {
110 //Add header
111 $ics = 'BEGIN:VCALENDAR
112 METHOD:PUBLISH
113 VERSION:2.0
114 X-WR-CALNAME:' . $this->calendarName . '
115 PRODID:-//hacksw/handcal//NONSGML v1.0//EN';
116
117 //Add events
118 foreach ($this->events as $event) {
119 $ics .= '
120 BEGIN:VEVENT
121 UID:' . $event['uid'] . '
122 DTSTAMP:' . gmdate('Ymd') . 'T' . gmdate('His') . 'Z
123 DTSTART:' . $event['start'] . '
124 DTEND:' . $event['end'] . '
125 SUMMARY:' . str_replace("\n", "\\n", $event['summary']) . '
126 DESCRIPTION:' . str_replace("\n", "\\n", $event['description']) . '
127 URL;VALUE=URI:' . $event['url'] . '
128 END:VEVENT';
129 }
130
131 //Add footer
132 $ics .= '
133 END:VCALENDAR';
134
135 if ($output) {
136 //Output
137 $filename = $this->calendarName;
138 //Filename needs quoting if it contains spaces
139 if (strpos($filename, ' ') !== false) {
140 $filename = '"'.$filename.'"';
141 }
142 header('Content-type: text/calendar; charset=utf-8');
143 header('Content-Disposition: inline; filename=' . $filename . '.ics');
144 echo $ics;
145 }
146 return $ics;
147 }
148 }
149