Correct response code for PROPPATCH and add support for Brief header.
[davical.git] / inc / caldav-PROPPATCH.php
blobd52263e9d8fad89a32313405f80a27d999ccf818
1 <?php
2 /**
3 * CalDAV Server - handle PROPPATCH method
5 * @package davical
6 * @subpackage caldav
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' );
24 $position = 0;
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 &lt;propertyupdate&gt; tag' );
33 /**
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/*");
39 /**
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].
45 $failure = array();
46 $success = array();
49 /**
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) {
57 global $failure;
58 $propstat = array(
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 );
72 /**
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();
78 $qry->Begin();
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();
84 switch( $tag ) {
86 case 'DAV::displayname':
87 /**
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) );
99 else {
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()) );
103 $success[$tag] = 1;
105 else {
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');
109 break;
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 ) );
126 $success[$tag] = 1;
128 else {
129 add_failure('set', $tag, 'HTTP/1.1 403 Forbidden',
130 translate("Resources may not be changed to / from collections."), 'cannot-modify-protected-property');
132 break;
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 ) );
140 $success[$tag] = 1;
142 else {
143 add_failure('set', $tag, 'HTTP/1.1 409 Conflict',
144 translate("The CalDAV:schedule-calendar-transp property may only be set on calendars."));
146 break;
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.") );
151 break;
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()) );
173 else {
174 add_failure('set', $tag, 'HTTP/1.1 409 Conflict', translate("calendar-timezone property is only valid for a calendar."));
176 break;
179 * The following properties are read-only, so they will cause the request to fail
181 case 'http://calendarserver.org/ns/:getctag':
182 case 'DAV::owner':
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':
187 case 'DAV::getetag':
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'));
195 break;
198 * If we don't have any special processing for the property, we just store it verbatim (which will be an XML fragment).
200 default:
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) );
203 $success[$tag] = 1;
204 break;
208 foreach( $rmprops AS $k => $setting ) {
209 $tag = $setting->GetTag();
210 $content = $setting->RenderContent();
212 switch( $tag ) {
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');
217 break;
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()) );
223 else {
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');
227 break;
230 * The following properties are read-only, so they will cause the request to fail
232 case 'http://calendarserver.org/ns/:getctag':
233 case 'DAV::owner':
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':
238 case 'DAV::getetag':
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);
248 break;
251 * If we don't have any special processing then we must have to just delete it. Nonexistence is not failure.
253 default:
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) );
256 $success[$tag] = 1;
257 break;
263 * If we have encountered any instances of failure, the whole damn thing fails.
265 if ( count($failure) > 0 ) {
267 $qry->Rollback();
269 foreach( $success AS $tag => $v ) {
270 // Unfortunately although these succeeded, we failed overall, so they didn't happen...
271 $failure[] = new XMLElement( 'propstat', array(
272 new XMLElement( 'prop', new XMLElement($tag)),
273 new XMLElement( 'status', 'HTTP/1.1 424 Failed Dependency' ),
277 $url = ConstructURL($request->path);
278 array_unshift( $failure, new XMLElement('href', $url ) );
279 $failure[] = new XMLElement('responsedescription', translate("Some properties were not able to be changed.") );
281 $multistatus = new XMLElement( "multistatus", new XMLElement( 'response', $failure ), array('xmlns'=>'DAV:') );
282 $request->DoResponse( 207, $multistatus->Render(0,'<?xml version="1.0" encoding="utf-8" ?>'), 'text/xml; charset="utf-8"' );
287 * 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();
292 $cache_ns = null;
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 if ( $request->brief_response ) {
304 $request->DoResponse(200); // Does not return. See: http://msdn.microsoft.com/en-us/library/aa142976%28v=exchg.65%29.aspx
307 $url = ConstructURL($request->path);
308 $href = new XMLElement('href', $url );
309 $desc = new XMLElement('responsedescription', translate("All requested changes were made.") );
311 $propstat = array();
312 foreach( $success AS $tag => $v ) {
313 $propstat[] = new XMLElement( 'propstat', array(
314 new XMLElement( 'prop', new XMLElement($tag)),
315 new XMLElement( 'status', 'HTTP/1.1 200 OK' ),
319 $url = ConstructURL($request->path);
320 array_unshift( $failure, new XMLElement('href', $url ) );
322 $multistatus = new XMLElement( "multistatus", new XMLElement( 'response', array( $href, $propstat, $desc ) ), array('xmlns'=>'DAV:') );
323 $request->DoResponse( 207, $multistatus->Render(0,'<?xml version="1.0" encoding="utf-8" ?>'), 'text/xml; charset="utf-8"' );
327 * Or it was all crap.
329 $request->DoResponse( 500 );
330 exit(0); // unneccessary