Fix missing END:VCALENDAR.
[davical.git] / inc / WritableCollection.php
blob6d2bb91e59bcf5f1b1de51bd5dd8cc0cdbd46f22
1 <?php
2 include_once('DAVResource.php');
4 class WritableCollection extends DAVResource {
6 /**
7 * Get a TZID string from this VEVENT/VTODO/... component if we can
8 * @param vComponent $comp
9 * @return The TZID value we found, or null
11 private static function GetTZID( vComponent $comp ) {
12 $p = $comp->GetProperty('DTSTART');
13 if ( !isset($p) && $comp->GetType() == 'VTODO' ) {
14 $p = $comp->GetProperty('DUE');
16 if ( !isset($p) ) return null;
17 return $p->GetParameterValue('TZID');
20 /**
21 * Writes the data to a member in the collection and returns the segment_name of the
22 * resource in our internal namespace.
24 * @param vCalendar $vcal The resource to be written.
25 * @param boolean $create_resource True if this is a new resource.
26 * @param boolean $do_scheduling True if we should also do scheduling for this write. Default false.
27 * @param string $segment_name The name of the resource within the collection, or null if this
28 * call should invent one based on the UID of the vCalendar.
29 * @param boolean $log_action Whether to log this action. Defaults to false since this is normally called
30 * in situations where one is writing secondary data.
31 * @return string The segment_name of the resource within the collection, as written, or false on failure.
33 function WriteCalendarMember( vCalendar $vcal, $create_resource, $do_scheduling=false, $segment_name = null, $log_action=false ) {
34 if ( !$this->IsSchedulingCollection() && !$this->IsCalendar() ) {
35 dbg_error_log( 'PUT', '"%s" is not a calendar or scheduling collection!', $this->dav_name);
36 return false;
39 global $session, $caldav_context;
41 $resources = $vcal->GetComponents('VTIMEZONE',false); // Not matching VTIMEZONE
42 $user_no = $this->user_no();
43 $collection_id = $this->collection_id();
45 if ( !isset($resources[0]) ) {
46 dbg_error_log( 'PUT', 'No calendar content!');
47 rollback_on_error( $caldav_context, $user_no, $this->dav_name.'/'.$segment_name, translate('No calendar content'), 412 );
48 return false;
50 else {
51 $first = $resources[0];
52 $resource_type = $first->GetType();
55 $uid = $first->GetPValue('UID');
56 if ( empty($segment_name) ) {
57 $segment_name = $uid.'.ics';
59 $path = $this->dav_name() . $segment_name;
61 $caldav_data = $vcal->Render();
62 $etag = md5($caldav_data);
63 $weak_etag = null;
65 $qry = new AwlQuery();
66 $existing_transaction_state = $qry->TransactionState();
67 if ( $existing_transaction_state == 0 ) $qry->Begin();
70 if ( $create_resource ) {
71 $qry->QDo('SELECT nextval(\'dav_id_seq\') AS dav_id');
73 else {
74 $qry->QDo('SELECT dav_id FROM caldav_data WHERE dav_name = :dav_name ', array(':dav_name' => $path));
76 if ( $qry->rows() != 1 || !($row = $qry->Fetch()) ) {
77 if ( !$create_resource ) {
78 // Looks like we will have to create it, even if the caller thought we wouldn't
79 $qry->QDo('SELECT nextval(\'dav_id_seq\') AS dav_id');
80 if ( $qry->rows() != 1 || !($row = $qry->Fetch()) ) {
81 // No dav_id? => We're toast!
82 trace_bug( 'No dav_id for "%s" on %s!!!', $path, ($create_resource ? 'create': 'update'));
83 rollback_on_error( $caldav_context, $user_no, $path);
84 return false;
86 $create_resource = true;
87 dbg_error_log( 'PUT', 'Unexpected need to create resource at "%s"', $path);
90 $dav_id = $row->dav_id;
92 $calitem_params = array(
93 ':dav_name' => $path,
94 ':user_no' => $user_no,
95 ':etag' => $etag,
96 ':dav_id' => $dav_id
99 $dav_params = array_merge($calitem_params, array(
100 ':dav_data' => $caldav_data,
101 ':caldav_type' => $resource_type,
102 ':session_user' => $session->user_no,
103 ':weak_etag' => $weak_etag
104 ) );
106 if ( !$this->IsSchedulingCollection() && $do_scheduling ) {
107 if ( do_scheduling_requests($vcal, $create_resource ) ) {
108 $dav_params[':dav_data'] = $vcal->Render(null, true);
109 $etag = null;
113 if ( $create_resource ) {
114 $sql = 'INSERT INTO caldav_data ( dav_id, user_no, dav_name, dav_etag, caldav_data, caldav_type, logged_user, created, modified, collection_id, weak_etag )
115 VALUES( :dav_id, :user_no, :dav_name, :etag, :dav_data, :caldav_type, :session_user, current_timestamp, current_timestamp, :collection_id, :weak_etag )';
116 $dav_params[':collection_id'] = $collection_id;
118 else {
119 $sql = 'UPDATE caldav_data SET caldav_data=:dav_data, dav_etag=:etag, caldav_type=:caldav_type, logged_user=:session_user,
120 modified=current_timestamp, weak_etag=:weak_etag WHERE dav_id=:dav_id';
122 if ( !$qry->QDo($sql,$dav_params) ) {
123 rollback_on_error( $caldav_context, $user_no, $path);
124 return false;
127 $dtstart = $first->GetPValue('DTSTART');
128 $calitem_params[':dtstart'] = $dtstart;
129 if ( (!isset($dtstart) || $dtstart == '') && $first->GetPValue('DUE') != '' ) {
130 $dtstart = $first->GetPValue('DUE');
133 $dtend = $first->GetPValue('DTEND');
134 if ( isset($dtend) && $dtend != '' ) {
135 dbg_error_log( 'PUT', ' DTEND: "%s", DTSTART: "%s", DURATION: "%s"', $dtend, $dtstart, $first->GetPValue('DURATION') );
136 $calitem_params[':dtend'] = $dtend;
137 $dtend = ':dtend';
139 else {
140 $dtend = 'NULL';
141 if ( $first->GetPValue('DURATION') != '' AND $dtstart != '' ) {
142 $duration = preg_replace( '#[PT]#', ' ', $first->GetPValue('DURATION') );
143 $dtend = '(:dtstart::timestamp with time zone + :duration::interval)';
144 $calitem_params[':duration'] = $duration;
146 elseif ( $first->GetType() == 'VEVENT' ) {
148 * From RFC2445 4.6.1:
149 * For cases where a "VEVENT" calendar component specifies a "DTSTART"
150 * property with a DATE data type but no "DTEND" property, the events
151 * non-inclusive end is the end of the calendar date specified by the
152 * "DTSTART" property. For cases where a "VEVENT" calendar component specifies
153 * a "DTSTART" property with a DATE-TIME data type but no "DTEND" property,
154 * the event ends on the same calendar date and time of day specified by the
155 * "DTSTART" property.
157 * So we're looking for 'VALUE=DATE', to identify the duration, effectively.
160 $value_type = $first->GetPParamValue('DTSTART','VALUE');
161 dbg_error_log('PUT','DTSTART without DTEND. DTSTART value type is %s', $value_type );
162 if ( isset($value_type) && $value_type == 'DATE' )
163 $dtend = '(:dtstart::timestamp with time zone::date + \'1 day\'::interval)';
164 else
165 $dtend = ':dtstart';
169 $last_modified = $first->GetPValue('LAST-MODIFIED');
170 if ( !isset($last_modified) || $last_modified == '' ) {
171 $last_modified = gmdate( 'Ymd\THis\Z' );
173 $calitem_params[':modified'] = $last_modified;
175 $dtstamp = $first->GetPValue('DTSTAMP');
176 if ( !isset($dtstamp) || $dtstamp == '' ) {
177 $dtstamp = $last_modified;
179 $calitem_params[':dtstamp'] = $dtstamp;
181 $class = $first->GetPValue('CLASS');
183 * It seems that some calendar clients don't set a class...
184 * RFC2445, 4.8.1.3: Default is PUBLIC
186 if ( $this->IsPublicOnly() || !isset($class) || $class == '' ) {
187 $class = 'PUBLIC';
189 $calitem_params[':class'] = $class;
191 /** Calculate what timezone to set, first, if possible */
192 $last_olson = 'Turkmenikikamukau'; // I really hope this location doesn't exist!
193 $tzid = self::GetTZID($first);
194 if ( !empty($tzid) ) {
195 $tz = $vcal->GetTimeZone($tzid);
196 $olson = $vcal->GetOlsonName($tz);
198 if ( !empty($olson) && ($olson != $last_olson) ) {
199 dbg_error_log( 'PUT', ' Setting timezone to %s', $olson );
200 $qry->QDo('SET TIMEZONE TO \''.$olson."'" );
201 $last_olson = $olson;
205 $created = $first->GetPValue('CREATED');
206 if ( $created == '00001231T000000Z' ) $created = '20001231T000000Z';
207 $calitem_params[':created'] = $created;
209 $calitem_params[':tzid'] = $tzid;
210 $calitem_params[':uid'] = $uid;
211 $calitem_params[':summary'] = $first->GetPValue('SUMMARY');
212 $calitem_params[':location'] = $first->GetPValue('LOCATION');
213 $calitem_params[':transp'] = $first->GetPValue('TRANSP');
214 $calitem_params[':description'] = $first->GetPValue('DESCRIPTION');
215 $calitem_params[':rrule'] = $first->GetPValue('RRULE');
216 $calitem_params[':url'] = $first->GetPValue('URL');
217 $calitem_params[':priority'] = $first->GetPValue('PRIORITY');
218 $calitem_params[':due'] = $first->GetPValue('DUE');
219 $calitem_params[':percent_complete'] = $first->GetPValue('PERCENT-COMPLETE');
220 $calitem_params[':status'] = $first->GetPValue('STATUS');
221 if ( $create_resource ) {
222 $sql = <<<EOSQL
223 INSERT INTO calendar_item (user_no, dav_name, dav_id, dav_etag, uid, dtstamp,
224 dtstart, dtend, summary, location, class, transp,
225 description, rrule, tz_id, last_modified, url, priority,
226 created, due, percent_complete, status, collection_id )
227 VALUES ( :user_no, :dav_name, currval('dav_id_seq'), :etag, :uid, :dtstamp,
228 :dtstart, $dtend, :summary, :location, :class, :transp,
229 :description, :rrule, :tzid, :modified, :url, :priority,
230 :created, :due, :percent_complete, :status, $collection_id )
231 EOSQL;
232 $sync_change = 201;
234 else {
235 $sql = <<<EOSQL
236 UPDATE calendar_item SET dav_etag=:etag, uid=:uid, dtstamp=:dtstamp,
237 dtstart=:dtstart, dtend=$dtend, summary=:summary, location=:location, class=:class, transp=:transp,
238 description=:description, rrule=:rrule, tz_id=:tzid, last_modified=:modified, url=:url, priority=:priority,
239 created=:created, due=:due, percent_complete=:percent_complete, status=:status
240 WHERE user_no=:user_no AND dav_name=:dav_name
241 EOSQL;
242 $sync_change = 200;
245 if ( !$this->IsSchedulingCollection() ) {
246 $this->WriteCalendarAlarms($dav_id, $vcal);
247 $this->WriteCalendarAttendees($dav_id, $vcal);
248 if ( $log_action && function_exists('log_caldav_action') ) {
249 log_caldav_action( $put_action_type, $first->GetPValue('UID'), $user_no, $collection_id, $path );
251 else if ( $log_action ) {
252 dbg_error_log( 'PUT', 'No log_caldav_action( %s, %s, %s, %s, %s) can be called.',
253 $put_action_type, $first->GetPValue('UID'), $user_no, $collection_id, $path );
257 $qry = new AwlQuery( $sql, $calitem_params );
258 if ( !$qry->Exec('PUT',__LINE__,__FILE__) ) {
259 rollback_on_error( $caldav_context, $user_no, $path);
260 return false;
262 $qry->QDo("SELECT write_sync_change( $collection_id, $sync_change, :dav_name)", array(':dav_name' => $path ) );
263 if ( $existing_transaction_state == 0 ) $qry->Commit();
265 dbg_error_log( 'PUT', 'User: %d, ETag: %s, Path: %s', $session->user_no, $etag, $path);
268 return $segment_name;
272 * Writes the data to a member in the collection and returns the segment_name of the
273 * resource in our internal namespace.
275 * A caller who wants scheduling not to happen for this write must already
276 * know they are dealing with a calendar, so should be calling WriteCalendarMember
277 * directly.
279 * @param $resource mixed The resource to be written.
280 * @param $create_resource boolean True if this is a new resource.
281 * @param $segment_name The name of the resource within the collection, or false on failure.
282 * @param boolean $log_action Whether to log this action. Defaults to true since this is normally called
283 * in situations where one is writing primary data.
284 * @return string The segment_name that was given, or one that was assigned if null was given.
286 function WriteMember( $resource, $create_resource, $segment_name = null, $log_action=true ) {
287 if ( ! $this->IsCollection() ) {
288 dbg_error_log( 'PUT', '"%s" is not a collection path', $this->dav_name);
289 return false;
291 if ( ! is_object($resource) ) {
292 dbg_error_log( 'PUT', 'No data supplied!' );
293 return false;
296 if ( $resource instanceof vCalendar ) {
297 return $this->WriteCalendarMember($resource,$create_resource,true,$segment_name,$log_action);
299 else if ( $resource instanceof VCard )
300 return $this->WriteAddressbookMember($resource,$create_resource,$segment_name, $log_action);
302 return $segment_name;
307 * Given a dav_id and an original vCalendar, pull out each of the VALARMs
308 * and write the values into the calendar_alarm table.
310 * @return null
312 function WriteCalendarAlarms( $dav_id, vCalendar $vcal ) {
313 $qry = new AwlQuery('DELETE FROM calendar_alarm WHERE dav_id = '.$dav_id );
314 $qry->Exec('PUT',__LINE__,__FILE__);
316 $components = $vcal->GetComponents();
318 $qry->SetSql('INSERT INTO calendar_alarm ( dav_id, action, trigger, summary, description, component, next_trigger )
319 VALUES( '.$dav_id.', :action, :trigger, :summary, :description, :component,
320 :related::timestamp with time zone + :related_trigger::interval )' );
321 $qry->Prepare();
322 foreach( $components AS $component ) {
323 if ( $component->GetType() == 'VTIMEZONE' ) continue;
324 $alarms = $component->GetComponents('VALARM');
325 if ( count($alarms) < 1 ) return;
327 foreach( $alarms AS $v ) {
328 $trigger = array_merge($v->GetProperties('TRIGGER'));
329 if ( $trigger == null ) continue; // Bogus data.
330 $trigger = $trigger[0];
331 $related = null;
332 $related_trigger = '0M';
333 $trigger_type = $trigger->GetParameterValue('VALUE');
334 if ( !isset($trigger_type) || $trigger_type == 'DURATION' ) {
335 switch ( $trigger->GetParameterValue('RELATED') ) {
336 case 'DTEND': $related = $component->GetPValue('DTEND'); break;
337 case 'DUE': $related = $component->GetPValue('DUE'); break;
338 default: $related = $component->GetPValue('DTSTART');
340 $duration = $trigger->Value();
341 if ( !preg_match('{^-?P(:?\d+W)?(:?\d+D)?(:?T(:?\d+H)?(:?\d+M)?(:?\d+S)?)?$}', $duration ) ) continue;
342 $minus = (substr($duration,0,1) == '-');
343 $related_trigger = trim(preg_replace( '#[PT-]#', ' ', $duration ));
344 if ( $minus ) {
345 $related_trigger = preg_replace( '{(\d+[WDHMS])}', '-$1 ', $related_trigger );
347 else {
348 $related_trigger = preg_replace( '{(\d+[WDHMS])}', '$1 ', $related_trigger );
351 else {
352 if ( false === strtotime($trigger->Value()) ) continue; // Invalid date.
354 $qry->Bind(':action', $v->GetPValue('ACTION'));
355 $qry->Bind(':trigger', $trigger->Render());
356 $qry->Bind(':summary', $v->GetPValue('SUMMARY'));
357 $qry->Bind(':description', $v->GetPValue('DESCRIPTION'));
358 $qry->Bind(':component', $v->Render());
359 $qry->Bind(':related', $related );
360 $qry->Bind(':related_trigger', $related_trigger );
361 $qry->Exec('PUT',__LINE__,__FILE__);
368 * Parse out the attendee property and write a row to the
369 * calendar_attendee table for each one.
370 * @param int $dav_id The dav_id of the caldav_data we're processing
371 * @param vComponent The VEVENT or VTODO containing the ATTENDEEs
372 * @return null
374 function WriteCalendarAttendees( $dav_id, vCalendar $vcal ) {
375 $qry = new AwlQuery('DELETE FROM calendar_attendee WHERE dav_id = '.$dav_id );
376 $qry->Exec('PUT',__LINE__,__FILE__);
378 $attendees = $vcal->GetAttendees();
379 if ( count($attendees) < 1 ) return;
381 $qry->SetSql('INSERT INTO calendar_attendee ( dav_id, status, partstat, cn, attendee, role, rsvp, property )
382 VALUES( '.$dav_id.', :status, :partstat, :cn, :attendee, :role, :rsvp, :property )' );
383 $qry->Prepare();
384 $processed = array();
385 foreach( $attendees AS $v ) {
386 $attendee = $v->Value();
387 if ( isset($processed[$attendee]) ) {
388 dbg_error_log( 'LOG', 'Duplicate attendee "%s" in resource "%d"', $attendee, $dav_id );
389 dbg_error_log( 'LOG', 'Original: "%s"', $processed[$attendee] );
390 dbg_error_log( 'LOG', 'Duplicate: "%s"', $v->Render() );
391 continue; /** @todo work out why we get duplicate ATTENDEE on one VEVENT */
393 $qry->Bind(':attendee', $attendee );
394 $qry->Bind(':status', $v->GetParameterValue('STATUS') );
395 $qry->Bind(':partstat', $v->GetParameterValue('PARTSTAT') );
396 $qry->Bind(':cn', $v->GetParameterValue('CN') );
397 $qry->Bind(':role', $v->GetParameterValue('ROLE') );
398 $qry->Bind(':rsvp', $v->GetParameterValue('RSVP') );
399 $qry->Bind(':property', $v->Render() );
400 $qry->Exec('PUT',__LINE__,__FILE__);
401 $processed[$attendee] = $v->Render();
406 * Writes the data to a member in the collection and returns the segment_name of the
407 * resource in our internal namespace.
409 * @param vCalendar $member_dav_name The path to the resource to be deleted.
410 * @return boolean Success is true, or false on failure.
412 function actualDeleteCalendarMember( $member_dav_name ) {
413 global $session, $caldav_context;
415 // A quick sanity check...
416 $segment_name = str_replace( $this->dav_name(), '', $member_dav_name );
417 if ( strstr($segment_name, '/') !== false ) {
418 @dbg_error_log( "DELETE", "DELETE: Refused to delete member '%s' from calendar '%s'!", $member_dav_name, $this->dav_name() );
419 return false;
422 // We need to serialise access to this process just for this collection
423 $cache = getCacheInstance();
424 $myLock = $cache->acquireLock('collection-'.$this->dav_name());
426 $qry = new AwlQuery();
427 $params = array( ':dav_name' => $member_dav_name );
429 if ( $qry->QDo("SELECT write_sync_change(collection_id, 404, caldav_data.dav_name) FROM caldav_data WHERE dav_name = :dav_name", $params )
430 && $qry->QDo("DELETE FROM property WHERE dav_name = :dav_name", $params )
431 && $qry->QDo("DELETE FROM locks WHERE dav_name = :dav_name", $params )
432 && $qry->QDo("DELETE FROM caldav_data WHERE dav_name = :dav_name", $params ) ) {
433 @dbg_error_log( "DELETE", "DELETE: Calendar member %s deleted from calendar '%s'", $member_dav_name, $this->dav_name() );
435 $cache->releaseLock($myLock);
437 return true;
440 $cache->releaseLock($myLock);
441 return false;