3 * CalDAV Server - handle PROPPATCH method
7 * @author Andrew McMillan <andrew@mcmillan.net.nz>
8 * @copyright Morphoss Ltd - http://www.morphoss.com/
9 * @license http://gnu.org/copyleft/gpl.html GNU GPL v2
11 dbg_error_log("PROPPATCH", "method handler");
13 require_once('vCalendar.php');
14 require_once('DAVResource.php');
16 $dav_resource = new DAVResource($request->path
);
17 if ( !$dav_resource->HavePrivilegeTo('DAV::write-properties') ) {
18 $parent = $dav_resource->GetParentContainer();
19 if ( !$dav_resource->IsBinding() ||
!$parent->HavePrivilegeTo('DAV::write') ) {
20 $request->PreconditionFailed(403, 'DAV::write-properties', 'You do not have permission to write properties to that resource' );
25 $xmltree = BuildXMLTree( $request->xml_tags
, $position);
27 // echo $xmltree->Render();
29 if ( $xmltree->GetTag() != "DAV::propertyupdate" ) {
30 $request->PreconditionFailed( 403, 'DAV::propertyupdate', 'XML request did not contain a <propertyupdate> tag' );
34 * Find the properties being set, and the properties being removed
36 $setprops = $xmltree->GetPath("/DAV::propertyupdate/DAV::set/DAV::prop/*");
37 $rmprops = $xmltree->GetPath("/DAV::propertyupdate/DAV::remove/DAV::prop/*");
40 * We build full status responses for failures. For success we just record
41 * it, since the multistatus response only applies to failure. While it is
42 * not explicitly stated in RFC2518, from reading between the lines (8.2.1)
43 * a success will return 200 OK [with an empty response].
50 * Small utility function to add propstat for one failure
51 * @param unknown_type $tag
52 * @param unknown_type $status
53 * @param unknown_type $description
54 * @param unknown_type $error_tag
56 function add_failure( $type, $tag, $status, $description=null, $error_tag = null) {
59 new XMLElement( 'prop', new XMLElement($tag)),
60 new XMLElement( 'status', $status )
63 if ( isset($description))
64 $propstat[] = new XMLElement( 'responsedescription', $description );
65 if ( isset($error_tag) )
66 $propstat[] = new XMLElement( 'error', new XMLElement( $error_tag ) );
68 $failure[$type.'-'.$tag] = new XMLElement('propstat', $propstat );
73 * Not much for it but to process the incoming settings in a big loop, doing
74 * the special-case stuff as needed and falling through to a default which
75 * stuffs the property somewhere we will be able to retrieve it from later.
77 $qry = new AwlQuery();
79 $setcalendar = count($xmltree->GetPath('/DAV::propertyupdate/DAV::set/DAV::prop/DAV::resourcetype/urn:ietf:params:xml:ns:caldav:calendar'));
80 foreach( $setprops AS $k => $setting ) {
81 $tag = $setting->GetTag();
82 $content = $setting->RenderContent();
86 case 'DAV::displayname':
88 * Can't set displayname on resources - only collections or principals
90 if ( $dav_resource->IsCollection() ||
$dav_resource->IsPrincipal() ) {
91 if ( $dav_resource->IsBinding() ) {
92 $qry->QDo('UPDATE dav_binding SET dav_displayname = :displayname WHERE dav_name = :dav_name',
93 array( ':displayname' => $content, ':dav_name' => $dav_resource->dav_name()) );
95 else if ( $dav_resource->IsPrincipal() ) {
96 $qry->QDo('UPDATE dav_principal SET fullname = :displayname, displayname = :displayname, modified = current_timestamp WHERE user_no = :user_no',
97 array( ':displayname' => $content, ':user_no' => $request->user_no
) );
100 $qry->QDo('UPDATE collection SET dav_displayname = :displayname, modified = current_timestamp WHERE dav_name = :dav_name',
101 array( ':displayname' => $content, ':dav_name' => $dav_resource->dav_name()) );
106 add_failure('set', $tag, 'HTTP/1.1 403 Forbidden',
107 translate("The displayname may only be set on collections, principals or bindings."), 'cannot-modify-protected-property');
111 case 'DAV::resourcetype':
113 * We only allow resourcetype setting on a normal collection, and not on a resource, a principal or a bind.
114 * Only collections may be CalDAV calendars or addressbooks, and they may not be both.
116 $setcollection = count($setting->GetPath('DAV::resourcetype/DAV::collection'));
117 $setaddressbook = count($setting->GetPath('DAV::resourcetype/urn:ietf:params:xml:ns:carddav:addressbook'));
118 if ( $dav_resource->IsCollection() && $setcollection && ! $dav_resource->IsPrincipal()
119 && ! $dav_resource->IsBinding() && ! ($setaddressbook && $setcalendar) ) {
120 $resourcetypes = $setting->GetPath('DAV::resourcetype/*');
121 $resourcetypes = str_replace( "\n", "", implode('',$resourcetypes));
122 $qry->QDo('UPDATE collection SET is_calendar = :is_calendar::boolean, is_addressbook = :is_addressbook::boolean,
123 resourcetypes = :resourcetypes WHERE dav_name = :dav_name',
124 array( ':dav_name' => $dav_resource->dav_name(), ':resourcetypes' => $resourcetypes,
125 ':is_calendar' => $setcalendar, ':is_addressbook' => $setaddressbook ) );
129 add_failure('set', $tag, 'HTTP/1.1 403 Forbidden',
130 translate("Resources may not be changed to / from collections."), 'cannot-modify-protected-property');
134 case 'urn:ietf:params:xml:ns:caldav:schedule-calendar-transp':
135 if ( $dav_resource->IsCollection() && ( $dav_resource->IsCalendar() ||
$setcalendar ) && !$dav_resource->IsBinding() ) {
136 $transparency = $setting->GetPath('urn:ietf:params:xml:ns:caldav:schedule-calendar-transp/*');
137 $transparency = preg_replace( '{^.*:}', '', $transparency[0]->GetTag());
138 $qry->QDo('UPDATE collection SET schedule_transp = :transparency WHERE dav_name = :dav_name',
139 array( ':dav_name' => $dav_resource->dav_name(), ':transparency' => $transparency ) );
143 add_failure('set', $tag, 'HTTP/1.1 409 Conflict',
144 translate("The CalDAV:schedule-calendar-transp property may only be set on calendars."));
148 case 'urn:ietf:params:xml:ns:caldav:calendar-free-busy-set':
149 add_failure('set', $tag, 'HTTP/1.1 409 Conflict',
150 translate("The calendar-free-busy-set is superseded by the schedule-calendar-transp property of a calendar collection.") );
153 case 'urn:ietf:params:xml:ns:caldav:calendar-timezone':
154 if ( $dav_resource->IsCollection() && $dav_resource->IsCalendar() && ! $dav_resource->IsBinding() ) {
155 $tzcomponent = $setting->GetPath('urn:ietf:params:xml:ns:caldav:calendar-timezone');
156 $tzstring = $tzcomponent[0]->GetContent();
157 $calendar = new vCalendar( $tzstring );
158 $timezones = $calendar->GetComponents('VTIMEZONE');
159 if ( count($timezones) == 0 ) break;
160 $tz = $timezones[0]; // Backward compatibility
161 $tzid = $tz->GetPValue('TZID');
162 $params = array( ':tzid' => $tzid );
163 $qry = new AwlQuery('SELECT 1 FROM timezones WHERE tzid = :tzid', $params );
164 if ( $qry->Exec('PUT',__LINE__
,__FILE__
) && $qry->rows() == 0 ) {
165 $params[':olson_name'] = $calendar->GetOlsonName($tz);
166 $params[':vtimezone'] = (isset($tz) ?
$tz->Render() : null );
167 $qry->QDo('INSERT INTO timezones (tzid, olson_name, active, vtimezone) VALUES(:tzid,:olson_name,false,:vtimezone)', $params );
170 $qry->QDo('UPDATE collection SET timezone = :tzid WHERE dav_name = :dav_name',
171 array( ':tzid' => $tzid, ':dav_name' => $dav_resource->dav_name()) );
174 add_failure('set', $tag, 'HTTP/1.1 409 Conflict', translate("calendar-timezone property is only valid for a calendar."));
179 * The following properties are read-only, so they will cause the request to fail
181 case 'http://calendarserver.org/ns/:getctag':
183 case 'DAV::principal-collection-set':
184 case 'urn:ietf:params:xml:ns:caldav:calendar-user-address-set':
185 case 'urn:ietf:params:xml:ns:caldav:schedule-inbox-URL':
186 case 'urn:ietf:params:xml:ns:caldav:schedule-outbox-URL':
188 case 'DAV::getcontentlength':
189 case 'DAV::getcontenttype':
190 case 'DAV::getlastmodified':
191 case 'DAV::creationdate':
192 case 'DAV::lockdiscovery':
193 case 'DAV::supportedlock':
194 add_failure('set', $tag, 'HTTP/1.1 409 Conflict', translate("Property is read-only"), new XMLElement( 'cannot-modify-protected-property'));
198 * If we don't have any special processing for the property, we just store it verbatim (which will be an XML fragment).
201 $qry->QDo('SELECT set_dav_property( :dav_name, :user_no::integer, :tag::text, :value::text)',
202 array( ':dav_name' => $dav_resource->dav_name(), ':user_no' => $request->user_no
, ':tag' => $tag, ':value' => $content) );
208 foreach( $rmprops AS $k => $setting ) {
209 $tag = $setting->GetTag();
210 $content = $setting->RenderContent();
214 case 'DAV::resourcetype':
215 add_failure('rm', $tag, 'HTTP/1.1 409 Conflict',
216 translate("DAV::resourcetype may only be set to a new value, it may not be removed."), 'cannot-modify-protected-property');
219 case 'urn:ietf:params:xml:ns:caldav:calendar-timezone':
220 if ( $dav_resource->IsCollection() && $dav_resource->IsCalendar() && ! $dav_resource->IsBinding() ) {
221 $qry->QDo('UPDATE collection SET timezone = NULL WHERE dav_name = :dav_name', array( ':dav_name' => $dav_resource->dav_name()) );
224 add_failure('rm', $tag, 'HTTP/1.1 409 Conflict',
225 translate("calendar-timezone property is only valid for a calendar."), 'cannot-modify-protected-property');
230 * The following properties are read-only, so they will cause the request to fail
232 case 'http://calendarserver.org/ns/:getctag':
234 case 'DAV::principal-collection-set':
235 case 'urn:ietf:params:xml:ns:caldav:CALENDAR-USER-ADDRESS-SET':
236 case 'urn:ietf:params:xml:ns:caldav:schedule-inbox-URL':
237 case 'urn:ietf:params:xml:ns:caldav:schedule-outbox-URL':
239 case 'DAV::getcontentlength':
240 case 'DAV::getcontenttype':
241 case 'DAV::getlastmodified':
242 case 'DAV::creationdate':
243 case 'DAV::displayname':
244 case 'DAV::lockdiscovery':
245 case 'DAV::supportedlock':
246 add_failure('rm', $tag, 'HTTP/1.1 409 Conflict', translate("Property is read-only"));
247 dbg_error_log( 'PROPPATCH', ' RMProperty %s is read only and cannot be removed', $tag);
251 * If we don't have any special processing then we must have to just delete it. Nonexistence is not failure.
254 $qry->QDo('DELETE FROM property WHERE dav_name=:dav_name AND property_name=:property_name',
255 array( ':dav_name' => $dav_resource->dav_name(), ':property_name' => $tag) );
263 * If we have encountered any instances of failure, the whole damn thing fails.
265 if ( count($failure) > 0 ) {
266 foreach( $success AS $tag => $v ) {
267 // Unfortunately although these succeeded, we failed overall, so they didn't happen...
268 $failure[] = new XMLElement( 'propstat', array(
269 new XMLElement( 'prop', new XMLElement($tag)),
270 new XMLElement( 'status', 'HTTP/1.1 424 Failed Dependency' ),
274 $url = ConstructURL($request->path
);
275 array_unshift( $failure, new XMLElement('href', $url ) );
276 $failure[] = new XMLElement('responsedescription', translate("Some properties were not able to be changed.") );
280 $multistatus = new XMLElement( "multistatus", new XMLElement( 'response', $failure ), array('xmlns'=>'DAV:') );
281 $request->DoResponse( 207, $multistatus->Render(0,'<?xml version="1.0" encoding="utf-8" ?>'), 'text/xml; charset="utf-8"' );
286 * Otherwise we will try and do the SQL. This is inside a transaction, so PostgreSQL guarantees the atomicity
289 if ( $qry->Commit() ) {
291 $cache = getCacheInstance();
293 if ( $dav_resource->IsPrincipal() ) {
294 $cache_ns = 'principal-'.$dav_resource->dav_name();
296 else if ( $dav_resource->IsCollection() ) {
297 // Uncache anything to do with the collection
298 $cache_ns = 'collection-'.$dav_resource->dav_name();
301 if ( isset($cache_ns) ) $cache->delete( $cache_ns, null );
303 $url = ConstructURL($request->path
);
304 $href = new XMLElement('href', $url );
305 $desc = new XMLElement('responsedescription', translate("All requested changes were made.") );
308 foreach( $success AS $tag => $v ) {
309 $propstat[] = new XMLElement( 'propstat', array(
310 new XMLElement( 'prop', new XMLElement($tag)),
311 new XMLElement( 'status', 'HTTP/1.1 200 OK' ),
315 $url = ConstructURL($request->path
);
316 array_unshift( $failure, new XMLElement('href', $url ) );
318 $multistatus = new XMLElement( "multistatus", new XMLElement( 'response', array( $href, $propstat, $desc ) ), array('xmlns'=>'DAV:') );
319 $request->DoResponse( 200, $multistatus->Render(0,'<?xml version="1.0" encoding="utf-8" ?>'), 'text/xml; charset="utf-8"' );
323 * Or it was all crap.
325 $request->DoResponse( 500 );
326 exit(0); // unneccessary