annotate plugins/libcalendaring/libvcalendar.php @ 4:888e774ee983

libcalendar plugin as distributed
author Charlie Root
date Sat, 13 Jan 2018 08:57:56 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1 <?php
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
2
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
3 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
4 * iCalendar functions for the libcalendaring plugin
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
5 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
6 * @author Thomas Bruederli <bruederli@kolabsys.com>
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
7 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
8 * Copyright (C) 2013-2015, Kolab Systems AG <contact@kolabsys.com>
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
9 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
10 * This program is free software: you can redistribute it and/or modify
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
11 * it under the terms of the GNU Affero General Public License as
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
12 * published by the Free Software Foundation, either version 3 of the
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
13 * License, or (at your option) any later version.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
14 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
15 * This program is distributed in the hope that it will be useful,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
18 * GNU Affero General Public License for more details.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
19 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
20 * You should have received a copy of the GNU Affero General Public License
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
22 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
23
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
24 use \Sabre\VObject;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
25 use \Sabre\VObject\DateTimeParser;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
26
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
27 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
28 * Class to parse and build vCalendar (iCalendar) files
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
29 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
30 * Uses the Sabre VObject library, version 3.x.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
31 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
32 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
33 class libvcalendar implements Iterator
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
34 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
35 private $timezone;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
36 private $attach_uri = null;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
37 private $prodid = '-//Roundcube libcalendaring//Sabre//Sabre VObject//EN';
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
38 private $type_component_map = array('event' => 'VEVENT', 'task' => 'VTODO');
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
39 private $attendee_keymap = array(
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
40 'name' => 'CN',
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
41 'status' => 'PARTSTAT',
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
42 'role' => 'ROLE',
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
43 'cutype' => 'CUTYPE',
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
44 'rsvp' => 'RSVP',
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
45 'delegated-from' => 'DELEGATED-FROM',
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
46 'delegated-to' => 'DELEGATED-TO',
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
47 'schedule-status' => 'SCHEDULE-STATUS',
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
48 'schedule-agent' => 'SCHEDULE-AGENT',
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
49 'sent-by' => 'SENT-BY',
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
50 );
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
51 private $organizer_keymap = array(
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
52 'name' => 'CN',
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
53 'schedule-status' => 'SCHEDULE-STATUS',
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
54 'schedule-agent' => 'SCHEDULE-AGENT',
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
55 'sent-by' => 'SENT-BY',
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
56 );
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
57 private $iteratorkey = 0;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
58 private $charset;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
59 private $forward_exceptions;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
60 private $vhead;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
61 private $fp;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
62 private $vtimezones = array();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
63
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
64 public $method;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
65 public $agent = '';
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
66 public $objects = array();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
67 public $freebusy = array();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
68
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
69
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
70 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
71 * Default constructor
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
72 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
73 function __construct($tz = null)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
74 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
75 $this->timezone = $tz;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
76 $this->prodid = '-//Roundcube libcalendaring ' . RCUBE_VERSION . '//Sabre//Sabre VObject ' . VObject\Version::VERSION . '//EN';
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
77 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
78
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
79 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
80 * Setter for timezone information
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
81 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
82 public function set_timezone($tz)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
83 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
84 $this->timezone = $tz;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
85 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
86
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
87 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
88 * Setter for URI template for attachment links
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
89 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
90 public function set_attach_uri($uri)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
91 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
92 $this->attach_uri = $uri;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
93 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
94
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
95 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
96 * Setter for a custom PRODID attribute
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
97 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
98 public function set_prodid($prodid)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
99 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
100 $this->prodid = $prodid;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
101 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
102
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
103 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
104 * Setter for a user-agent string to tweak input/output accordingly
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
105 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
106 public function set_agent($agent)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
107 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
108 $this->agent = $agent;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
109 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
110
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
111 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
112 * Free resources by clearing member vars
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
113 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
114 public function reset()
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
115 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
116 $this->vhead = '';
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
117 $this->method = '';
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
118 $this->objects = array();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
119 $this->freebusy = array();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
120 $this->vtimezones = array();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
121 $this->iteratorkey = 0;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
122
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
123 if ($this->fp) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
124 fclose($this->fp);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
125 $this->fp = null;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
126 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
127 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
128
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
129 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
130 * Import events from iCalendar format
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
131 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
132 * @param string vCalendar input
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
133 * @param string Input charset (from envelope)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
134 * @param boolean True if parsing exceptions should be forwarded to the caller
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
135 * @return array List of events extracted from the input
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
136 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
137 public function import($vcal, $charset = 'UTF-8', $forward_exceptions = false, $memcheck = true)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
138 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
139 // TODO: convert charset to UTF-8 if other
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
140
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
141 try {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
142 // estimate the memory usage and try to avoid fatal errors when allowed memory gets exhausted
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
143 if ($memcheck) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
144 $count = substr_count($vcal, 'BEGIN:VEVENT') + substr_count($vcal, 'BEGIN:VTODO');
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
145 $expected_memory = $count * 70*1024; // assume ~ 70K per event (empirically determined)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
146
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
147 if (!rcube_utils::mem_check($expected_memory)) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
148 throw new Exception("iCal file too big");
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
149 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
150 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
151
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
152 $vobject = VObject\Reader::read($vcal, VObject\Reader::OPTION_FORGIVING | VObject\Reader::OPTION_IGNORE_INVALID_LINES);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
153 if ($vobject)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
154 return $this->import_from_vobject($vobject);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
155 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
156 catch (Exception $e) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
157 if ($forward_exceptions) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
158 throw $e;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
159 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
160 else {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
161 rcube::raise_error(array(
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
162 'code' => 600, 'type' => 'php',
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
163 'file' => __FILE__, 'line' => __LINE__,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
164 'message' => "iCal data parse error: " . $e->getMessage()),
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
165 true, false);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
166 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
167 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
168
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
169 return array();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
170 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
171
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
172 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
173 * Read iCalendar events from a file
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
174 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
175 * @param string File path to read from
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
176 * @param string Input charset (from envelope)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
177 * @param boolean True if parsing exceptions should be forwarded to the caller
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
178 * @return array List of events extracted from the file
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
179 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
180 public function import_from_file($filepath, $charset = 'UTF-8', $forward_exceptions = false)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
181 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
182 if ($this->fopen($filepath, $charset, $forward_exceptions)) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
183 while ($this->_parse_next(false)) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
184 // nop
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
185 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
186
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
187 fclose($this->fp);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
188 $this->fp = null;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
189 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
190
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
191 return $this->objects;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
192 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
193
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
194 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
195 * Open a file to read iCalendar events sequentially
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
196 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
197 * @param string File path to read from
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
198 * @param string Input charset (from envelope)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
199 * @param boolean True if parsing exceptions should be forwarded to the caller
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
200 * @return boolean True if file contents are considered valid
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
201 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
202 public function fopen($filepath, $charset = 'UTF-8', $forward_exceptions = false)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
203 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
204 $this->reset();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
205
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
206 // just to be sure...
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
207 @ini_set('auto_detect_line_endings', true);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
208
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
209 $this->charset = $charset;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
210 $this->forward_exceptions = $forward_exceptions;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
211 $this->fp = fopen($filepath, 'r');
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
212
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
213 // check file content first
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
214 $begin = fread($this->fp, 1024);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
215 if (!preg_match('/BEGIN:VCALENDAR/i', $begin)) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
216 return false;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
217 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
218
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
219 fseek($this->fp, 0);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
220 return $this->_parse_next();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
221 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
222
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
223 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
224 * Parse the next event/todo/freebusy object from the input file
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
225 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
226 private function _parse_next($reset = true)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
227 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
228 if ($reset) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
229 $this->iteratorkey = 0;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
230 $this->objects = array();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
231 $this->freebusy = array();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
232 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
233
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
234 $next = $this->_next_component();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
235 $buffer = $next;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
236
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
237 // load the next component(s) too, as they could contain recurrence exceptions
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
238 while (preg_match('/(RRULE|RECURRENCE-ID)[:;]/i', $next)) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
239 $next = $this->_next_component();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
240 $buffer .= $next;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
241 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
242
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
243 // parse the vevent block surrounded with the vcalendar heading
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
244 if (strlen($buffer) && preg_match('/BEGIN:(VEVENT|VTODO|VFREEBUSY)/i', $buffer)) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
245 try {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
246 $this->import($this->vhead . $buffer . "END:VCALENDAR", $this->charset, true, false);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
247 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
248 catch (Exception $e) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
249 if ($this->forward_exceptions) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
250 throw new VObject\ParseException($e->getMessage() . " in\n" . $buffer);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
251 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
252 else {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
253 // write the failing section to error log
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
254 rcube::raise_error(array(
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
255 'code' => 600, 'type' => 'php',
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
256 'file' => __FILE__, 'line' => __LINE__,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
257 'message' => $e->getMessage() . " in\n" . $buffer),
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
258 true, false);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
259 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
260
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
261 // advance to next
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
262 return $this->_parse_next($reset);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
263 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
264
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
265 return count($this->objects) > 0;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
266 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
267
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
268 return false;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
269 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
270
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
271 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
272 * Helper method to read the next calendar component from the file
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
273 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
274 private function _next_component()
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
275 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
276 $buffer = '';
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
277 $vcalendar_head = false;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
278 while (($line = fgets($this->fp, 1024)) !== false) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
279 // ignore END:VCALENDAR lines
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
280 if (preg_match('/END:VCALENDAR/i', $line)) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
281 continue;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
282 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
283 // read vcalendar header (with timezone defintion)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
284 if (preg_match('/BEGIN:VCALENDAR/i', $line)) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
285 $this->vhead = '';
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
286 $vcalendar_head = true;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
287 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
288
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
289 // end of VCALENDAR header part
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
290 if ($vcalendar_head && preg_match('/BEGIN:(VEVENT|VTODO|VFREEBUSY)/i', $line)) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
291 $vcalendar_head = false;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
292 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
293
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
294 if ($vcalendar_head) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
295 $this->vhead .= $line;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
296 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
297 else {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
298 $buffer .= $line;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
299 if (preg_match('/END:(VEVENT|VTODO|VFREEBUSY)/i', $line)) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
300 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
301 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
302 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
303 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
304
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
305 return $buffer;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
306 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
307
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
308 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
309 * Import objects from an already parsed Sabre\VObject\Component object
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
310 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
311 * @param object Sabre\VObject\Component to read from
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
312 * @return array List of events extracted from the file
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
313 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
314 public function import_from_vobject($vobject)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
315 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
316 $seen = array();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
317 $exceptions = array();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
318
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
319 if ($vobject->name == 'VCALENDAR') {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
320 $this->method = strval($vobject->METHOD);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
321 $this->agent = strval($vobject->PRODID);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
322
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
323 foreach ($vobject->getComponents() as $ve) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
324 if ($ve->name == 'VEVENT' || $ve->name == 'VTODO') {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
325 // convert to hash array representation
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
326 $object = $this->_to_array($ve);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
327
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
328 // temporarily store this as exception
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
329 if ($object['recurrence_date']) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
330 $exceptions[] = $object;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
331 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
332 else if (!$seen[$object['uid']]++) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
333 $this->objects[] = $object;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
334 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
335 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
336 else if ($ve->name == 'VFREEBUSY') {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
337 $this->objects[] = $this->_parse_freebusy($ve);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
338 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
339 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
340
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
341 // add exceptions to the according master events
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
342 foreach ($exceptions as $exception) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
343 $uid = $exception['uid'];
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
344
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
345 // make this exception the master
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
346 if (!$seen[$uid]++) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
347 $this->objects[] = $exception;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
348 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
349 else {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
350 foreach ($this->objects as $i => $object) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
351 // add as exception to existing entry with a matching UID
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
352 if ($object['uid'] == $uid) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
353 $this->objects[$i]['exceptions'][] = $exception;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
354
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
355 if (!empty($object['recurrence'])) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
356 $this->objects[$i]['recurrence']['EXCEPTIONS'] = &$this->objects[$i]['exceptions'];
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
357 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
358 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
359 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
360 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
361 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
362 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
363 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
364
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
365 return $this->objects;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
366 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
367
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
368 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
369 * Getter for free-busy periods
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
370 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
371 public function get_busy_periods()
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
372 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
373 $out = array();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
374 foreach ((array)$this->freebusy['periods'] as $period) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
375 if ($period[2] != 'FREE') {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
376 $out[] = $period;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
377 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
378 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
379
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
380 return $out;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
381 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
382
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
383 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
384 * Helper method to determine whether the connected client is an Apple device
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
385 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
386 private function is_apple()
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
387 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
388 return stripos($this->agent, 'Apple') !== false
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
389 || stripos($this->agent, 'Mac OS X') !== false
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
390 || stripos($this->agent, 'iOS/') !== false;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
391 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
392
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
393 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
394 * Convert the given VEvent object to a libkolab compatible array representation
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
395 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
396 * @param object Vevent object to convert
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
397 * @return array Hash array with object properties
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
398 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
399 private function _to_array($ve)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
400 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
401 $event = array(
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
402 'uid' => self::convert_string($ve->UID),
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
403 'title' => self::convert_string($ve->SUMMARY),
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
404 '_type' => $ve->name == 'VTODO' ? 'task' : 'event',
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
405 // set defaults
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
406 'priority' => 0,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
407 'attendees' => array(),
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
408 'x-custom' => array(),
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
409 );
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
410
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
411 // Catch possible exceptions when date is invalid (Bug #2144)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
412 // We can skip these fields, they aren't critical
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
413 foreach (array('CREATED' => 'created', 'LAST-MODIFIED' => 'changed', 'DTSTAMP' => 'changed') as $attr => $field) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
414 try {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
415 if (!$event[$field] && $ve->{$attr}) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
416 $event[$field] = $ve->{$attr}->getDateTime();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
417 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
418 } catch (Exception $e) {}
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
419 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
420
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
421 // map other attributes to internal fields
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
422 foreach ($ve->children as $prop) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
423 if (!($prop instanceof VObject\Property))
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
424 continue;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
425
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
426 $value = strval($prop);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
427
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
428 switch ($prop->name) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
429 case 'DTSTART':
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
430 case 'DTEND':
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
431 case 'DUE':
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
432 $propmap = array('DTSTART' => 'start', 'DTEND' => 'end', 'DUE' => 'due');
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
433 $event[$propmap[$prop->name]] = self::convert_datetime($prop);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
434 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
435
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
436 case 'TRANSP':
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
437 $event['free_busy'] = strval($prop) == 'TRANSPARENT' ? 'free' : 'busy';
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
438 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
439
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
440 case 'STATUS':
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
441 if ($value == 'TENTATIVE')
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
442 $event['free_busy'] = 'tentative';
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
443 else if ($value == 'CANCELLED')
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
444 $event['cancelled'] = true;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
445 else if ($value == 'COMPLETED')
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
446 $event['complete'] = 100;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
447
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
448 $event['status'] = $value;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
449 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
450
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
451 case 'COMPLETED':
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
452 if (self::convert_datetime($prop)) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
453 $event['status'] = 'COMPLETED';
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
454 $event['complete'] = 100;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
455 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
456 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
457
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
458 case 'PRIORITY':
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
459 if (is_numeric($value))
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
460 $event['priority'] = $value;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
461 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
462
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
463 case 'RRULE':
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
464 $params = is_array($event['recurrence']) ? $event['recurrence'] : array();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
465 // parse recurrence rule attributes
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
466 foreach ($prop->getParts() as $k => $v) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
467 $params[strtoupper($k)] = is_array($v) ? implode(',', $v) : $v;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
468 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
469 if ($params['UNTIL'])
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
470 $params['UNTIL'] = date_create($params['UNTIL']);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
471 if (!$params['INTERVAL'])
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
472 $params['INTERVAL'] = 1;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
473
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
474 $event['recurrence'] = array_filter($params);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
475 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
476
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
477 case 'EXDATE':
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
478 if (!empty($value)) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
479 $exdates = array_map(function($_) { return is_array($_) ? $_[0] : $_; }, self::convert_datetime($prop, true));
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
480 $event['recurrence']['EXDATE'] = array_merge((array)$event['recurrence']['EXDATE'], $exdates);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
481 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
482 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
483
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
484 case 'RDATE':
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
485 if (!empty($value)) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
486 $rdates = array_map(function($_) { return is_array($_) ? $_[0] : $_; }, self::convert_datetime($prop, true));
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
487 $event['recurrence']['RDATE'] = array_merge((array)$event['recurrence']['RDATE'], $rdates);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
488 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
489 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
490
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
491 case 'RECURRENCE-ID':
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
492 $event['recurrence_date'] = self::convert_datetime($prop);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
493 if ($prop->offsetGet('RANGE') == 'THISANDFUTURE' || $prop->offsetGet('THISANDFUTURE') !== null) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
494 $event['thisandfuture'] = true;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
495 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
496 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
497
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
498 case 'RELATED-TO':
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
499 $reltype = $prop->offsetGet('RELTYPE');
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
500 if ($reltype == 'PARENT' || $reltype === null) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
501 $event['parent_id'] = $value;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
502 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
503 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
504
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
505 case 'SEQUENCE':
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
506 $event['sequence'] = intval($value);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
507 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
508
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
509 case 'PERCENT-COMPLETE':
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
510 $event['complete'] = intval($value);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
511 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
512
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
513 case 'LOCATION':
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
514 case 'DESCRIPTION':
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
515 case 'URL':
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
516 case 'COMMENT':
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
517 $event[strtolower($prop->name)] = self::convert_string($prop);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
518 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
519
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
520 case 'CATEGORY':
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
521 case 'CATEGORIES':
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
522 $event['categories'] = array_merge((array)$event['categories'], $prop->getParts());
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
523 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
524
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
525 case 'CLASS':
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
526 case 'X-CALENDARSERVER-ACCESS':
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
527 $event['sensitivity'] = strtolower($value);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
528 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
529
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
530 case 'X-MICROSOFT-CDO-BUSYSTATUS':
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
531 if ($value == 'OOF')
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
532 $event['free_busy'] = 'outofoffice';
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
533 else if (in_array($value, array('FREE', 'BUSY', 'TENTATIVE')))
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
534 $event['free_busy'] = strtolower($value);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
535 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
536
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
537 case 'ATTENDEE':
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
538 case 'ORGANIZER':
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
539 $params = array('RSVP' => false);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
540 foreach ($prop->parameters() as $pname => $pvalue) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
541 switch ($pname) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
542 case 'RSVP': $params[$pname] = strtolower($pvalue) == 'true'; break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
543 case 'CN': $params[$pname] = self::unescape($pvalue); break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
544 default: $params[$pname] = strval($pvalue); break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
545 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
546 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
547 $attendee = self::map_keys($params, array_flip($this->attendee_keymap));
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
548 $attendee['email'] = preg_replace('!^mailto:!i', '', $value);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
549
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
550 if ($prop->name == 'ORGANIZER') {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
551 $attendee['role'] = 'ORGANIZER';
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
552 $attendee['status'] = 'ACCEPTED';
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
553 $event['organizer'] = $attendee;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
554
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
555 if (array_key_exists('schedule-agent', $attendee)) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
556 $schedule_agent = $attendee['schedule-agent'];
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
557 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
558 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
559 else if ($attendee['email'] != $event['organizer']['email']) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
560 $event['attendees'][] = $attendee;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
561 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
562 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
563
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
564 case 'ATTACH':
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
565 $params = self::parameters_array($prop);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
566 if (substr($value, 0, 4) == 'http' && !strpos($value, ':attachment:')) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
567 $event['links'][] = $value;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
568 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
569 else if (strlen($value) && strtoupper($params['VALUE']) == 'BINARY') {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
570 $attachment = self::map_keys($params, array('FMTTYPE' => 'mimetype', 'X-LABEL' => 'name', 'X-APPLE-FILENAME' => 'name'));
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
571 $attachment['data'] = $value;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
572 $attachment['size'] = strlen($value);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
573 $event['attachments'][] = $attachment;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
574 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
575 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
576
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
577 default:
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
578 if (substr($prop->name, 0, 2) == 'X-')
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
579 $event['x-custom'][] = array($prop->name, strval($value));
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
580 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
581 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
582 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
583
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
584 // check DURATION property if no end date is set
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
585 if (empty($event['end']) && $ve->DURATION) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
586 try {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
587 $duration = new DateInterval(strval($ve->DURATION));
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
588 $end = clone $event['start'];
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
589 $end->add($duration);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
590 $event['end'] = $end;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
591 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
592 catch (\Exception $e) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
593 trigger_error(strval($e), E_USER_WARNING);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
594 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
595 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
596
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
597 // validate event dates
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
598 if ($event['_type'] == 'event') {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
599 $event['allday'] = false;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
600
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
601 // check for all-day dates
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
602 if ($event['start']->_dateonly) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
603 $event['allday'] = true;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
604 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
605
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
606 // events may lack the DTEND property, set it to DTSTART (RFC5545 3.6.1)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
607 if (empty($event['end'])) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
608 $event['end'] = clone $event['start'];
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
609 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
610 // shift end-date by one day (except Thunderbird)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
611 else if ($event['allday'] && is_object($event['end'])) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
612 $event['end']->sub(new \DateInterval('PT23H'));
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
613 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
614
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
615 // sanity-check and fix end date
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
616 if (!empty($event['end']) && $event['end'] < $event['start']) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
617 $event['end'] = clone $event['start'];
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
618 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
619 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
620
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
621 // make organizer part of the attendees list for compatibility reasons
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
622 if (!empty($event['organizer']) && is_array($event['attendees']) && $event['_type'] == 'event') {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
623 array_unshift($event['attendees'], $event['organizer']);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
624 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
625
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
626 // find alarms
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
627 foreach ($ve->select('VALARM') as $valarm) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
628 $action = 'DISPLAY';
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
629 $trigger = null;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
630 $alarm = array();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
631
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
632 foreach ($valarm->children as $prop) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
633 $value = strval($prop);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
634
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
635 switch ($prop->name) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
636 case 'TRIGGER':
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
637 foreach ($prop->parameters as $param) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
638 if ($param->name == 'VALUE' && $param->getValue() == 'DATE-TIME') {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
639 $trigger = '@' . $prop->getDateTime()->format('U');
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
640 $alarm['trigger'] = $prop->getDateTime();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
641 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
642 else if ($param->name == 'RELATED') {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
643 $alarm['related'] = $param->getValue();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
644 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
645 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
646 if (!$trigger && ($values = libcalendaring::parse_alarm_value($value))) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
647 $trigger = $values[2];
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
648 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
649
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
650 if (!$alarm['trigger']) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
651 $alarm['trigger'] = rtrim(preg_replace('/([A-Z])0[WDHMS]/', '\\1', $value), 'T');
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
652 // if all 0-values have been stripped, assume 'at time'
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
653 if ($alarm['trigger'] == 'P')
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
654 $alarm['trigger'] = 'PT0S';
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
655 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
656 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
657
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
658 case 'ACTION':
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
659 $action = $alarm['action'] = strtoupper($value);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
660 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
661
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
662 case 'SUMMARY':
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
663 case 'DESCRIPTION':
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
664 case 'DURATION':
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
665 $alarm[strtolower($prop->name)] = self::convert_string($prop);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
666 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
667
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
668 case 'REPEAT':
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
669 $alarm['repeat'] = intval($value);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
670 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
671
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
672 case 'ATTENDEE':
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
673 $alarm['attendees'][] = preg_replace('!^mailto:!i', '', $value);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
674 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
675
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
676 case 'ATTACH':
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
677 $params = self::parameters_array($prop);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
678 if (strlen($value) && (preg_match('/^[a-z]+:/', $value) || strtoupper($params['VALUE']) == 'URI')) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
679 // we only support URI-type of attachments here
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
680 $alarm['uri'] = $value;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
681 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
682 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
683 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
684 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
685
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
686 if ($action != 'NONE') {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
687 if ($trigger && !$event['alarms']) // store first alarm in legacy property
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
688 $event['alarms'] = $trigger . ':' . $action;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
689
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
690 if ($alarm['trigger'])
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
691 $event['valarms'][] = $alarm;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
692 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
693 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
694
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
695 // assign current timezone to event start/end
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
696 if ($event['start'] instanceof DateTime) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
697 if ($this->timezone)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
698 $event['start']->setTimezone($this->timezone);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
699 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
700 else {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
701 unset($event['start']);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
702 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
703
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
704 if ($event['end'] instanceof DateTime) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
705 if ($this->timezone)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
706 $event['end']->setTimezone($this->timezone);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
707 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
708 else {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
709 unset($event['end']);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
710 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
711
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
712 // some iTip CANCEL messages only contain the start date
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
713 if (!$event['end'] && $event['start'] && $this->method == 'CANCEL') {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
714 $event['end'] = clone $event['start'];
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
715 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
716
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
717 // T2531: Remember SCHEDULE-AGENT in custom property to properly
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
718 // support event updates via CalDAV when SCHEDULE-AGENT=CLIENT is used
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
719 if (isset($schedule_agent)) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
720 $event['x-custom'][] = array('SCHEDULE-AGENT', $schedule_agent);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
721 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
722
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
723 // minimal validation
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
724 if (empty($event['uid']) || ($event['_type'] == 'event' && empty($event['start']) != empty($event['end']))) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
725 throw new VObject\ParseException('Object validation failed: missing mandatory object properties');
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
726 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
727
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
728 return $event;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
729 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
730
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
731 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
732 * Parse the given vfreebusy component into an array representation
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
733 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
734 private function _parse_freebusy($ve)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
735 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
736 $this->freebusy = array('_type' => 'freebusy', 'periods' => array());
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
737 $seen = array();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
738
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
739 foreach ($ve->children as $prop) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
740 if (!($prop instanceof VObject\Property))
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
741 continue;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
742
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
743 $value = strval($prop);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
744
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
745 switch ($prop->name) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
746 case 'CREATED':
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
747 case 'LAST-MODIFIED':
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
748 case 'DTSTAMP':
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
749 case 'DTSTART':
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
750 case 'DTEND':
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
751 $propmap = array('DTSTART' => 'start', 'DTEND' => 'end', 'CREATED' => 'created', 'LAST-MODIFIED' => 'changed', 'DTSTAMP' => 'changed');
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
752 $this->freebusy[$propmap[$prop->name]] = self::convert_datetime($prop);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
753 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
754
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
755 case 'ORGANIZER':
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
756 $this->freebusy['organizer'] = preg_replace('!^mailto:!i', '', $value);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
757 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
758
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
759 case 'FREEBUSY':
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
760 // The freebusy component can hold more than 1 value, separated by commas.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
761 $periods = explode(',', $value);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
762 $fbtype = strval($prop['FBTYPE']) ?: 'BUSY';
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
763
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
764 // skip dupes
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
765 if ($seen[$value.':'.$fbtype]++)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
766 continue;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
767
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
768 foreach ($periods as $period) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
769 // Every period is formatted as [start]/[end]. The start is an
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
770 // absolute UTC time, the end may be an absolute UTC time, or
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
771 // duration (relative) value.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
772 list($busyStart, $busyEnd) = explode('/', $period);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
773
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
774 $busyStart = DateTimeParser::parse($busyStart);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
775 $busyEnd = DateTimeParser::parse($busyEnd);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
776 if ($busyEnd instanceof \DateInterval) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
777 $tmp = clone $busyStart;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
778 $tmp->add($busyEnd);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
779 $busyEnd = $tmp;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
780 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
781
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
782 if ($busyEnd && $busyEnd > $busyStart)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
783 $this->freebusy['periods'][] = array($busyStart, $busyEnd, $fbtype);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
784 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
785 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
786
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
787 case 'COMMENT':
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
788 $this->freebusy['comment'] = $value;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
789 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
790 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
791
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
792 return $this->freebusy;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
793 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
794
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
795 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
796 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
797 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
798 public static function convert_string($prop)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
799 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
800 return strval($prop);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
801 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
802
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
803 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
804 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
805 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
806 public static function unescape($prop)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
807 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
808 return str_replace('\,', ',', strval($prop));
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
809 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
810
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
811 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
812 * Helper method to correctly interpret an all-day date value
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
813 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
814 public static function convert_datetime($prop, $as_array = false)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
815 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
816 if (empty($prop)) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
817 return $as_array ? array() : null;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
818 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
819
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
820 else if ($prop instanceof VObject\Property\iCalendar\DateTime) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
821 if (count($prop->getDateTimes()) > 1) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
822 $dt = array();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
823 $dateonly = !$prop->hasTime();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
824 foreach ($prop->getDateTimes() as $item) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
825 $item->_dateonly = $dateonly;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
826 $dt[] = $item;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
827 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
828 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
829 else {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
830 $dt = $prop->getDateTime();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
831 if (!$prop->hasTime()) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
832 $dt->_dateonly = true;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
833 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
834 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
835 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
836 else if ($prop instanceof VObject\Property\iCalendar\Period) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
837 $dt = array();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
838 foreach ($prop->getParts() as $val) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
839 try {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
840 list($start, $end) = explode('/', $val);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
841 $start = DateTimeParser::parseDateTime($start);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
842
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
843 // This is a duration value.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
844 if ($end[0] === 'P') {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
845 $dur = DateTimeParser::parseDuration($end);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
846 $end = clone $start;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
847 $end->add($dur);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
848 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
849 else {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
850 $end = DateTimeParser::parseDateTime($end);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
851 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
852 $dt[] = array($start, $end);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
853 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
854 catch (Exception $e) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
855 // ignore single date parse errors
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
856 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
857 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
858 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
859 else if ($prop instanceof \DateTime) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
860 $dt = $prop;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
861 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
862
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
863 // force return value to array if requested
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
864 if ($as_array && !is_array($dt)) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
865 $dt = empty($dt) ? array() : array($dt);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
866 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
867
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
868 return $dt;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
869 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
870
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
871
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
872 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
873 * Create a Sabre\VObject\Property instance from a PHP DateTime object
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
874 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
875 * @param object VObject\Document parent node to create property for
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
876 * @param string Property name
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
877 * @param object DateTime
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
878 * @param boolean Set as UTC date
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
879 * @param boolean Set as VALUE=DATE property
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
880 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
881 public function datetime_prop($cal, $name, $dt, $utc = false, $dateonly = null, $set_type = false)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
882 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
883 if ($utc) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
884 $dt->setTimeZone(new \DateTimeZone('UTC'));
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
885 $is_utc = true;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
886 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
887 else {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
888 $is_utc = ($tz = $dt->getTimezone()) && in_array($tz->getName(), array('UTC','GMT','Z'));
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
889 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
890 $is_dateonly = $dateonly === null ? (bool)$dt->_dateonly : (bool)$dateonly;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
891 $vdt = $cal->createProperty($name, $dt, null, $is_dateonly ? 'DATE' : 'DATE-TIME');
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
892
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
893 if ($is_dateonly) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
894 $vdt['VALUE'] = 'DATE';
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
895 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
896 else if ($set_type) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
897 $vdt['VALUE'] = 'DATE-TIME';
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
898 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
899
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
900 // register timezone for VTIMEZONE block
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
901 if (!$is_utc && !$dateonly && $tz && ($tzname = $tz->getName())) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
902 $ts = $dt->format('U');
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
903 if (is_array($this->vtimezones[$tzname])) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
904 $this->vtimezones[$tzname][0] = min($this->vtimezones[$tzname][0], $ts);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
905 $this->vtimezones[$tzname][1] = max($this->vtimezones[$tzname][1], $ts);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
906 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
907 else {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
908 $this->vtimezones[$tzname] = array($ts, $ts);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
909 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
910 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
911
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
912 return $vdt;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
913 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
914
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
915 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
916 * Copy values from one hash array to another using a key-map
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
917 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
918 public static function map_keys($values, $map)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
919 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
920 $out = array();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
921 foreach ($map as $from => $to) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
922 if (isset($values[$from]))
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
923 $out[$to] = is_array($values[$from]) ? join(',', $values[$from]) : $values[$from];
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
924 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
925 return $out;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
926 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
927
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
928 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
929 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
930 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
931 private static function parameters_array($prop)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
932 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
933 $params = array();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
934 foreach ($prop->parameters() as $name => $value) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
935 $params[strtoupper($name)] = strval($value);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
936 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
937 return $params;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
938 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
939
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
940
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
941 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
942 * Export events to iCalendar format
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
943 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
944 * @param array Events as array
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
945 * @param string VCalendar method to advertise
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
946 * @param boolean Directly send data to stdout instead of returning
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
947 * @param callable Callback function to fetch attachment contents, false if no attachment export
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
948 * @param boolean Add VTIMEZONE block with timezone definitions for the included events
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
949 * @return string Events in iCalendar format (http://tools.ietf.org/html/rfc5545)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
950 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
951 public function export($objects, $method = null, $write = false, $get_attachment = false, $with_timezones = true)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
952 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
953 $this->method = $method;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
954
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
955 // encapsulate in VCALENDAR container
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
956 $vcal = new VObject\Component\VCalendar();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
957 $vcal->VERSION = '2.0';
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
958 $vcal->PRODID = $this->prodid;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
959 $vcal->CALSCALE = 'GREGORIAN';
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
960
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
961 if (!empty($method)) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
962 $vcal->METHOD = $method;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
963 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
964
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
965 // write vcalendar header
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
966 if ($write) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
967 echo preg_replace('/END:VCALENDAR[\r\n]*$/m', '', $vcal->serialize());
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
968 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
969
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
970 foreach ($objects as $object) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
971 $this->_to_ical($object, !$write?$vcal:false, $get_attachment);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
972 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
973
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
974 // include timezone information
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
975 if ($with_timezones || !empty($method)) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
976 foreach ($this->vtimezones as $tzid => $range) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
977 $vt = self::get_vtimezone($tzid, $range[0], $range[1], $vcal);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
978 if (empty($vt)) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
979 continue; // no timezone information found
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
980 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
981
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
982 if ($write) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
983 echo $vt->serialize();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
984 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
985 else {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
986 $vcal->add($vt);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
987 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
988 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
989 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
990
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
991 if ($write) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
992 echo "END:VCALENDAR\r\n";
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
993 return true;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
994 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
995 else {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
996 return $vcal->serialize();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
997 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
998 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
999
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1000 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1001 * Build a valid iCal format block from the given event
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1002 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1003 * @param array Hash array with event/task properties from libkolab
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1004 * @param object VCalendar object to append event to or false for directly sending data to stdout
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1005 * @param callable Callback function to fetch attachment contents, false if no attachment export
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1006 * @param object RECURRENCE-ID property when serializing a recurrence exception
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1007 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1008 private function _to_ical($event, $vcal, $get_attachment, $recurrence_id = null)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1009 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1010 $type = $event['_type'] ?: 'event';
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1011
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1012 $cal = $vcal ?: new VObject\Component\VCalendar();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1013 $ve = $cal->create($this->type_component_map[$type]);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1014 $ve->UID = $event['uid'];
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1015
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1016 // set DTSTAMP according to RFC 5545, 3.8.7.2.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1017 $dtstamp = !empty($event['changed']) && empty($this->method) ? $event['changed'] : new DateTime('now', new \DateTimeZone('UTC'));
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1018 $ve->DTSTAMP = $this->datetime_prop($cal, 'DTSTAMP', $dtstamp, true);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1019
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1020 // all-day events end the next day
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1021 if ($event['allday'] && !empty($event['end'])) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1022 $event['end'] = clone $event['end'];
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1023 $event['end']->add(new \DateInterval('P1D'));
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1024 $event['end']->_dateonly = true;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1025 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1026 if (!empty($event['created']))
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1027 $ve->add($this->datetime_prop($cal, 'CREATED', $event['created'], true));
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1028 if (!empty($event['changed']))
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1029 $ve->add($this->datetime_prop($cal, 'LAST-MODIFIED', $event['changed'], true));
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1030 if (!empty($event['start']))
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1031 $ve->add($this->datetime_prop($cal, 'DTSTART', $event['start'], false, (bool)$event['allday']));
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1032 if (!empty($event['end']))
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1033 $ve->add($this->datetime_prop($cal, 'DTEND', $event['end'], false, (bool)$event['allday']));
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1034 if (!empty($event['due']))
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1035 $ve->add($this->datetime_prop($cal, 'DUE', $event['due'], false));
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1036
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1037 // we're exporting a recurrence instance only
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1038 if (!$recurrence_id && $event['recurrence_date'] && $event['recurrence_date'] instanceof DateTime) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1039 $recurrence_id = $this->datetime_prop($cal, 'RECURRENCE-ID', $event['recurrence_date'], false, (bool)$event['allday']);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1040 if ($event['thisandfuture'])
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1041 $recurrence_id->add('RANGE', 'THISANDFUTURE');
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1042 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1043
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1044 if ($recurrence_id) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1045 $ve->add($recurrence_id);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1046 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1047
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1048 $ve->add('SUMMARY', $event['title']);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1049
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1050 if ($event['location'])
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1051 $ve->add($this->is_apple() ? new vobject_location_property($cal, 'LOCATION', $event['location']) : $cal->create('LOCATION', $event['location']));
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1052 if ($event['description'])
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1053 $ve->add('DESCRIPTION', strtr($event['description'], array("\r\n" => "\n", "\r" => "\n"))); // normalize line endings
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1054
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1055 if (isset($event['sequence']))
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1056 $ve->add('SEQUENCE', $event['sequence']);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1057
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1058 if ($event['recurrence'] && !$recurrence_id) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1059 $exdates = $rdates = null;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1060 if (isset($event['recurrence']['EXDATE'])) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1061 $exdates = $event['recurrence']['EXDATE'];
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1062 unset($event['recurrence']['EXDATE']); // don't serialize EXDATEs into RRULE value
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1063 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1064 if (isset($event['recurrence']['RDATE'])) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1065 $rdates = $event['recurrence']['RDATE'];
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1066 unset($event['recurrence']['RDATE']); // don't serialize RDATEs into RRULE value
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1067 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1068
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1069 if ($event['recurrence']['FREQ']) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1070 $ve->add('RRULE', libcalendaring::to_rrule($event['recurrence'], (bool)$event['allday']));
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1071 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1072
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1073 // add EXDATEs each one per line (for Thunderbird Lightning)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1074 if (is_array($exdates)) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1075 foreach ($exdates as $ex) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1076 if ($ex instanceof \DateTime) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1077 $exd = clone $event['start'];
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1078 $exd->setDate($ex->format('Y'), $ex->format('n'), $ex->format('j'));
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1079 $exd->setTimeZone(new \DateTimeZone('UTC'));
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1080 $ve->add($this->datetime_prop($cal, 'EXDATE', $exd, true));
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1081 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1082 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1083 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1084 // add RDATEs
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1085 if (!empty($rdates)) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1086 foreach ((array)$rdates as $rdate) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1087 $ve->add($this->datetime_prop($cal, 'RDATE', $rdate));
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1088 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1089 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1090 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1091
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1092 if ($event['categories']) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1093 $cat = $cal->create('CATEGORIES');
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1094 $cat->setParts((array)$event['categories']);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1095 $ve->add($cat);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1096 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1097
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1098 if (!empty($event['free_busy'])) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1099 $ve->add('TRANSP', $event['free_busy'] == 'free' ? 'TRANSPARENT' : 'OPAQUE');
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1100
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1101 // for Outlook clients we provide the X-MICROSOFT-CDO-BUSYSTATUS property
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1102 if (stripos($this->agent, 'outlook') !== false) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1103 $ve->add('X-MICROSOFT-CDO-BUSYSTATUS', $event['free_busy'] == 'outofoffice' ? 'OOF' : strtoupper($event['free_busy']));
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1104 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1105 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1106
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1107 if ($event['priority'])
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1108 $ve->add('PRIORITY', $event['priority']);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1109
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1110 if ($event['cancelled'])
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1111 $ve->add('STATUS', 'CANCELLED');
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1112 else if ($event['free_busy'] == 'tentative')
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1113 $ve->add('STATUS', 'TENTATIVE');
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1114 else if ($event['complete'] == 100)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1115 $ve->add('STATUS', 'COMPLETED');
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1116 else if (!empty($event['status']))
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1117 $ve->add('STATUS', $event['status']);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1118
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1119 if (!empty($event['sensitivity']))
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1120 $ve->add('CLASS', strtoupper($event['sensitivity']));
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1121
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1122 if (!empty($event['complete'])) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1123 $ve->add('PERCENT-COMPLETE', intval($event['complete']));
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1124 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1125
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1126 // Apple iCal and BusyCal required the COMPLETED date to be set in order to consider a task complete
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1127 if ($event['status'] == 'COMPLETED' || $event['complete'] == 100) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1128 $ve->add($this->datetime_prop($cal, 'COMPLETED', $event['changed'] ?: new DateTime('now - 1 hour'), true));
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1129 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1130
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1131 if ($event['valarms']) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1132 foreach ($event['valarms'] as $alarm) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1133 $va = $cal->createComponent('VALARM');
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1134 $va->action = $alarm['action'];
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1135 if ($alarm['trigger'] instanceof DateTime) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1136 $va->add($this->datetime_prop($cal, 'TRIGGER', $alarm['trigger'], true, null, true));
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1137 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1138 else {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1139 $alarm_props = array();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1140 if (strtoupper($alarm['related']) == 'END') {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1141 $alarm_props['RELATED'] = 'END';
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1142 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1143 $va->add('TRIGGER', $alarm['trigger'], $alarm_props);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1144 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1145
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1146 if ($alarm['action'] == 'EMAIL') {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1147 foreach ((array)$alarm['attendees'] as $attendee) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1148 $va->add('ATTENDEE', 'mailto:' . $attendee);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1149 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1150 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1151 if ($alarm['description']) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1152 $va->add('DESCRIPTION', $alarm['description'] ?: $event['title']);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1153 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1154 if ($alarm['summary']) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1155 $va->add('SUMMARY', $alarm['summary']);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1156 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1157 if ($alarm['duration']) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1158 $va->add('DURATION', $alarm['duration']);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1159 $va->add('REPEAT', intval($alarm['repeat']));
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1160 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1161 if ($alarm['uri']) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1162 $va->add('ATTACH', $alarm['uri'], array('VALUE' => 'URI'));
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1163 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1164 $ve->add($va);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1165 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1166 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1167 // legacy support
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1168 else if ($event['alarms']) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1169 $va = $cal->createComponent('VALARM');
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1170 list($trigger, $va->action) = explode(':', $event['alarms']);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1171 $val = libcalendaring::parse_alarm_value($trigger);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1172 if ($val[3])
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1173 $va->add('TRIGGER', $val[3]);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1174 else if ($val[0] instanceof DateTime)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1175 $va->add($this->datetime_prop($cal, 'TRIGGER', $val[0], true, null, true));
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1176 $ve->add($va);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1177 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1178
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1179 // Find SCHEDULE-AGENT
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1180 foreach ((array)$event['x-custom'] as $prop) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1181 if ($prop[0] === 'SCHEDULE-AGENT') {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1182 $schedule_agent = $prop[1];
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1183 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1184 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1185
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1186 foreach ((array)$event['attendees'] as $attendee) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1187 if ($attendee['role'] == 'ORGANIZER') {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1188 if (empty($event['organizer']))
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1189 $event['organizer'] = $attendee;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1190 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1191 else if (!empty($attendee['email'])) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1192 if (isset($attendee['rsvp']))
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1193 $attendee['rsvp'] = $attendee['rsvp'] ? 'TRUE' : null;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1194
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1195 $mailto = $attendee['email'];
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1196 $attendee = array_filter(self::map_keys($attendee, $this->attendee_keymap));
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1197
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1198 if ($schedule_agent !== null && !isset($attendee['SCHEDULE-AGENT'])) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1199 $attendee['SCHEDULE-AGENT'] = $schedule_agent;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1200 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1201
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1202 $ve->add('ATTENDEE', 'mailto:' . $mailto, $attendee);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1203 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1204 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1205
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1206 if ($event['organizer']) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1207 $organizer = array_filter(self::map_keys($event['organizer'], $this->organizer_keymap));
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1208
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1209 if ($schedule_agent !== null && !isset($organizer['SCHEDULE-AGENT'])) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1210 $organizer['SCHEDULE-AGENT'] = $schedule_agent;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1211 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1212
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1213 $ve->add('ORGANIZER', 'mailto:' . $event['organizer']['email'], $organizer);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1214 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1215
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1216 foreach ((array)$event['url'] as $url) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1217 if (!empty($url)) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1218 $ve->add('URL', $url);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1219 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1220 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1221
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1222 if (!empty($event['parent_id'])) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1223 $ve->add('RELATED-TO', $event['parent_id'], array('RELTYPE' => 'PARENT'));
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1224 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1225
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1226 if ($event['comment'])
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1227 $ve->add('COMMENT', $event['comment']);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1228
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1229 $memory_limit = parse_bytes(ini_get('memory_limit'));
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1230
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1231 // export attachments
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1232 if (!empty($event['attachments'])) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1233 foreach ((array)$event['attachments'] as $attach) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1234 // check available memory and skip attachment export if we can't buffer it
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1235 // @todo: use rcube_utils::mem_check()
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1236 if (is_callable($get_attachment) && $memory_limit > 0 && ($memory_used = function_exists('memory_get_usage') ? memory_get_usage() : 16*1024*1024)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1237 && $attach['size'] && $memory_used + $attach['size'] * 3 > $memory_limit) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1238 continue;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1239 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1240 // embed attachments using the given callback function
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1241 if (is_callable($get_attachment) && ($data = call_user_func($get_attachment, $attach['id'], $event))) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1242 // embed attachments for iCal
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1243 $ve->add('ATTACH',
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1244 $data,
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1245 array_filter(array('VALUE' => 'BINARY', 'ENCODING' => 'BASE64', 'FMTTYPE' => $attach['mimetype'], 'X-LABEL' => $attach['name'])));
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1246 unset($data); // attempt to free memory
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1247 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1248 // list attachments as absolute URIs
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1249 else if (!empty($this->attach_uri)) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1250 $ve->add('ATTACH',
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1251 strtr($this->attach_uri, array(
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1252 '{{id}}' => urlencode($attach['id']),
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1253 '{{name}}' => urlencode($attach['name']),
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1254 '{{mimetype}}' => urlencode($attach['mimetype']),
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1255 )),
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1256 array('FMTTYPE' => $attach['mimetype'], 'VALUE' => 'URI'));
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1257 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1258 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1259 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1260
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1261 foreach ((array)$event['links'] as $uri) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1262 $ve->add('ATTACH', $uri);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1263 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1264
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1265 // add custom properties
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1266 foreach ((array)$event['x-custom'] as $prop) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1267 $ve->add($prop[0], $prop[1]);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1268 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1269
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1270 // append to vcalendar container
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1271 if ($vcal) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1272 $vcal->add($ve);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1273 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1274 else { // serialize and send to stdout
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1275 echo $ve->serialize();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1276 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1277
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1278 // append recurrence exceptions
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1279 if (is_array($event['recurrence']) && $event['recurrence']['EXCEPTIONS']) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1280 foreach ($event['recurrence']['EXCEPTIONS'] as $ex) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1281 $exdate = $ex['recurrence_date'] ?: $ex['start'];
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1282 $recurrence_id = $this->datetime_prop($cal, 'RECURRENCE-ID', $exdate, false, (bool)$event['allday']);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1283 if ($ex['thisandfuture'])
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1284 $recurrence_id->add('RANGE', 'THISANDFUTURE');
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1285 $this->_to_ical($ex, $vcal, $get_attachment, $recurrence_id);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1286 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1287 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1288 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1289
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1290 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1291 * Returns a VTIMEZONE component for a Olson timezone identifier
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1292 * with daylight transitions covering the given date range.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1293 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1294 * @param string Timezone ID as used in PHP's Date functions
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1295 * @param integer Unix timestamp with first date/time in this timezone
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1296 * @param integer Unix timestap with last date/time in this timezone
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1297 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1298 * @return mixed A Sabre\VObject\Component object representing a VTIMEZONE definition
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1299 * or false if no timezone information is available
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1300 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1301 public static function get_vtimezone($tzid, $from = 0, $to = 0, $cal = null)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1302 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1303 if (!$from) $from = time();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1304 if (!$to) $to = $from;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1305 if (!$cal) $cal = new VObject\Component\VCalendar();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1306
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1307 if (is_string($tzid)) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1308 try {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1309 $tz = new \DateTimeZone($tzid);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1310 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1311 catch (\Exception $e) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1312 return false;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1313 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1314 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1315 else if (is_a($tzid, '\\DateTimeZone')) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1316 $tz = $tzid;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1317 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1318
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1319 if (!is_a($tz, '\\DateTimeZone')) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1320 return false;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1321 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1322
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1323 $year = 86400 * 360;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1324 $transitions = $tz->getTransitions($from - $year, $to + $year);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1325
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1326 $vt = $cal->createComponent('VTIMEZONE');
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1327 $vt->TZID = $tz->getName();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1328
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1329 $std = null; $dst = null;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1330 foreach ($transitions as $i => $trans) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1331 $cmp = null;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1332
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1333 if ($i == 0) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1334 $tzfrom = $trans['offset'] / 3600;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1335 continue;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1336 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1337
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1338 if ($trans['isdst']) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1339 $t_dst = $trans['ts'];
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1340 $dst = $cal->createComponent('DAYLIGHT');
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1341 $cmp = $dst;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1342 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1343 else {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1344 $t_std = $trans['ts'];
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1345 $std = $cal->createComponent('STANDARD');
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1346 $cmp = $std;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1347 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1348
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1349 if ($cmp) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1350 $dt = new DateTime($trans['time']);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1351 $offset = $trans['offset'] / 3600;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1352
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1353 $cmp->DTSTART = $dt->format('Ymd\THis');
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1354 $cmp->TZOFFSETFROM = sprintf('%+03d%02d', floor($tzfrom), ($tzfrom - floor($tzfrom)) * 60);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1355 $cmp->TZOFFSETTO = sprintf('%+03d%02d', floor($offset), ($offset - floor($offset)) * 60);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1356
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1357 if (!empty($trans['abbr'])) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1358 $cmp->TZNAME = $trans['abbr'];
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1359 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1360
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1361 $tzfrom = $offset;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1362 $vt->add($cmp);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1363 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1364
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1365 // we covered the entire date range
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1366 if ($std && $dst && min($t_std, $t_dst) < $from && max($t_std, $t_dst) > $to) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1367 break;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1368 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1369 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1370
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1371 // add X-MICROSOFT-CDO-TZID if available
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1372 $microsoftExchangeMap = array_flip(VObject\TimeZoneUtil::$microsoftExchangeMap);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1373 if (array_key_exists($tz->getName(), $microsoftExchangeMap)) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1374 $vt->add('X-MICROSOFT-CDO-TZID', $microsoftExchangeMap[$tz->getName()]);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1375 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1376
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1377 return $vt;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1378 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1379
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1380
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1381 /*** Implement PHP 5 Iterator interface to make foreach work ***/
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1382
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1383 function current()
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1384 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1385 return $this->objects[$this->iteratorkey];
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1386 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1387
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1388 function key()
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1389 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1390 return $this->iteratorkey;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1391 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1392
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1393 function next()
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1394 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1395 $this->iteratorkey++;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1396
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1397 // read next chunk if we're reading from a file
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1398 if (!$this->objects[$this->iteratorkey] && $this->fp) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1399 $this->_parse_next(true);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1400 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1401
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1402 return $this->valid();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1403 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1404
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1405 function rewind()
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1406 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1407 $this->iteratorkey = 0;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1408 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1409
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1410 function valid()
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1411 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1412 return !empty($this->objects[$this->iteratorkey]);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1413 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1414
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1415 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1416
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1417
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1418 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1419 * Override Sabre\VObject\Property\Text that quotes commas in the location property
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1420 * because Apple clients treat that property as list.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1421 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1422 class vobject_location_property extends VObject\Property\Text
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1423 {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1424 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1425 * List of properties that are considered 'structured'.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1426 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1427 * @var array
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1428 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1429 protected $structuredValues = array(
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1430 // vCard
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1431 'N',
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1432 'ADR',
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1433 'ORG',
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1434 'GENDER',
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1435 'LOCATION',
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1436 // iCalendar
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1437 'REQUEST-STATUS',
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1438 );
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1439 }