PageRenderTime 93ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/inc/caldav-PROPPATCH.php

https://gitlab.com/tiggerben/davical
PHP | 351 lines | 246 code | 43 blank | 62 comment | 58 complexity | 9c98cabae7224ab478ea5b6516d614d0 MD5 | raw file
  1. <?php
  2. /**
  3. * CalDAV Server - handle PROPPATCH method
  4. *
  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
  10. */
  11. dbg_error_log("PROPPATCH", "method handler");
  12. require_once('vCalendar.php');
  13. require_once('DAVResource.php');
  14. $dav_resource = new DAVResource($request->path);
  15. if ( !$dav_resource->HavePrivilegeTo('DAV::write-properties') ) {
  16. $parent = $dav_resource->GetParentContainer();
  17. if ( !$dav_resource->IsBinding() || !$parent->HavePrivilegeTo('DAV::write') ) {
  18. $request->PreconditionFailed(403, 'DAV::write-properties', 'You do not have permission to write properties to that resource' );
  19. }
  20. }
  21. $position = 0;
  22. $xmltree = BuildXMLTree( $request->xml_tags, $position);
  23. // echo $xmltree->Render();
  24. if ( $xmltree->GetNSTag() != "DAV::propertyupdate" ) {
  25. $request->PreconditionFailed( 403, 'DAV::propertyupdate', 'XML request did not contain a &lt;propertyupdate&gt; tag' );
  26. }
  27. /**
  28. * Find the properties being set, and the properties being removed
  29. */
  30. $setprops = $xmltree->GetPath("/DAV::propertyupdate/DAV::set/DAV::prop/*");
  31. $rmprops = $xmltree->GetPath("/DAV::propertyupdate/DAV::remove/DAV::prop/*");
  32. /**
  33. * We build full status responses for failures. For success we just record
  34. * it, since the multistatus response only applies to failure. While it is
  35. * not explicitly stated in RFC2518, from reading between the lines (8.2.1)
  36. * a success will return 200 OK [with an empty response].
  37. */
  38. $failure = array();
  39. $success = array();
  40. $reply = new XMLDocument( array( 'DAV:' => '') );
  41. /**
  42. * Small utility function to add propstat for one failure
  43. * @param unknown_type $tag
  44. * @param unknown_type $status
  45. * @param unknown_type $description
  46. * @param unknown_type $error_tag
  47. */
  48. function add_failure( $type, $tag, $status, $description=null, $error_tag = null) {
  49. global $failure, $reply;
  50. $prop = new XMLElement('prop');
  51. $reply->NSElement($prop, $tag);
  52. $propstat = array($prop,new XMLElement( 'status', $status ));
  53. if ( isset($description))
  54. $propstat[] = new XMLElement( 'responsedescription', $description );
  55. if ( isset($error_tag) )
  56. $propstat[] = new XMLElement( 'error', new XMLElement( $error_tag ) );
  57. $failure[$type.'-'.$tag] = new XMLElement('propstat', $propstat );
  58. }
  59. /**
  60. * Not much for it but to process the incoming settings in a big loop, doing
  61. * the special-case stuff as needed and falling through to a default which
  62. * stuffs the property somewhere we will be able to retrieve it from later.
  63. */
  64. $qry = new AwlQuery();
  65. $qry->Begin();
  66. $setcalendar = count($xmltree->GetPath('/DAV::propertyupdate/DAV::set/DAV::prop/DAV::resourcetype/urn:ietf:params:xml:ns:caldav:calendar'));
  67. foreach( $setprops AS $k => $setting ) {
  68. $tag = $setting->GetNSTag();
  69. $content = $setting->RenderContent(0,null,true);
  70. switch( $tag ) {
  71. case 'DAV::displayname':
  72. /**
  73. * Can't set displayname on resources - only collections or principals
  74. */
  75. if ( $dav_resource->IsCollection() || $dav_resource->IsPrincipal() ) {
  76. if ( $dav_resource->IsBinding() ) {
  77. $qry->QDo('UPDATE dav_binding SET dav_displayname = :displayname WHERE dav_name = :dav_name',
  78. array( ':displayname' => $content, ':dav_name' => $dav_resource->dav_name()) );
  79. }
  80. else if ( $dav_resource->IsPrincipal() ) {
  81. $qry->QDo('UPDATE dav_principal SET fullname = :displayname, displayname = :displayname, modified = current_timestamp WHERE user_no = :user_no',
  82. array( ':displayname' => $content, ':user_no' => $request->user_no) );
  83. }
  84. else {
  85. $qry->QDo('UPDATE collection SET dav_displayname = :displayname, modified = current_timestamp WHERE dav_name = :dav_name',
  86. array( ':displayname' => $content, ':dav_name' => $dav_resource->dav_name()) );
  87. }
  88. $success[$tag] = 1;
  89. }
  90. else {
  91. add_failure('set', $tag, 'HTTP/1.1 403 Forbidden',
  92. translate("The displayname may only be set on collections, principals or bindings."), 'cannot-modify-protected-property');
  93. }
  94. break;
  95. case 'DAV::resourcetype':
  96. /**
  97. * We only allow resourcetype setting on a normal collection, and not on a resource, a principal or a bind.
  98. * Only collections may be CalDAV calendars or addressbooks, and they may not be both.
  99. */
  100. $resourcetypes = $setting->GetPath('DAV::resourcetype/*');
  101. $setcollection = false;
  102. $setcalendar = false;
  103. $setaddressbook = false;
  104. $setother = false;
  105. foreach( $resourcetypes AS $xnode ) {
  106. switch( $xnode->GetNSTag() ) {
  107. case 'urn:ietf:params:xml:ns:caldav:calendar': $setcalendar = true; break;
  108. case 'urn:ietf:params:xml:ns:carddav:addressbook': $setaddressbook = true; break;
  109. case 'DAV::collection': $setcollection = true; break;
  110. default:
  111. $setother = true;
  112. }
  113. }
  114. if ( $dav_resource->IsCollection() && $setcollection && ! $dav_resource->IsPrincipal() && ! $dav_resource->IsBinding()
  115. && !($setcalendar && $setaddressbook) && !$setother ) {
  116. $resourcetypes = '<collection xmlns="DAV:"/>';
  117. if ( $setcalendar ) $resourcetypes .= '<calendar xmlns="urn:ietf:params:xml:ns:caldav"/>';
  118. else if ( $setaddressbook ) $resourcetypes .= '<addressbook xmlns="urn:ietf:params:xml:ns:carddav"/>';
  119. $qry->QDo('UPDATE collection SET is_calendar = :is_calendar::boolean, is_addressbook = :is_addressbook::boolean,
  120. resourcetypes = :resourcetypes WHERE dav_name = :dav_name',
  121. array( ':dav_name' => $dav_resource->dav_name(), ':resourcetypes' => $resourcetypes,
  122. ':is_calendar' => $setcalendar, ':is_addressbook' => $setaddressbook ) );
  123. $success[$tag] = 1;
  124. }
  125. else if ( $setcalendar && $setaddressbook ) {
  126. add_failure('set', $tag, 'HTTP/1.1 409 Conflict',
  127. translate("A collection may not be both a calendar and an addressbook."));
  128. }
  129. else if ( $setother ) {
  130. add_failure('set', $tag, 'HTTP/1.1 403 Forbidden',
  131. translate("Unsupported resourcetype modification."), 'cannot-modify-protected-property');
  132. }
  133. else {
  134. add_failure('set', $tag, 'HTTP/1.1 403 Forbidden',
  135. translate("Resources may not be changed to / from collections."), 'cannot-modify-protected-property');
  136. }
  137. break;
  138. case 'urn:ietf:params:xml:ns:caldav:schedule-calendar-transp':
  139. if ( $dav_resource->IsCollection() && ( $dav_resource->IsCalendar() || $setcalendar ) && !$dav_resource->IsBinding() ) {
  140. $transparency = $setting->GetPath('urn:ietf:params:xml:ns:caldav:schedule-calendar-transp/*');
  141. $transparency = preg_replace( '{^.*:}', '', $transparency[0]->GetNSTag());
  142. $qry->QDo('UPDATE collection SET schedule_transp = :transparency WHERE dav_name = :dav_name',
  143. array( ':dav_name' => $dav_resource->dav_name(), ':transparency' => $transparency ) );
  144. $success[$tag] = 1;
  145. }
  146. else {
  147. add_failure('set', $tag, 'HTTP/1.1 409 Conflict',
  148. translate("The CalDAV:schedule-calendar-transp property may only be set on calendars."));
  149. }
  150. break;
  151. case 'urn:ietf:params:xml:ns:caldav:calendar-free-busy-set':
  152. add_failure('set', $tag, 'HTTP/1.1 409 Conflict',
  153. translate("The calendar-free-busy-set is superseded by the schedule-calendar-transp property of a calendar collection.") );
  154. break;
  155. case 'urn:ietf:params:xml:ns:caldav:calendar-timezone':
  156. if ( $dav_resource->IsCollection() && $dav_resource->IsCalendar() && ! $dav_resource->IsBinding() ) {
  157. $tzcomponent = $setting->GetPath('urn:ietf:params:xml:ns:caldav:calendar-timezone');
  158. $tzstring = $tzcomponent[0]->GetContent();
  159. $calendar = new vCalendar( $tzstring );
  160. $timezones = $calendar->GetComponents('VTIMEZONE');
  161. if ( count($timezones) == 0 ) break;
  162. $tz = $timezones[0]; // Backward compatibility
  163. $tzid = $tz->GetPValue('TZID');
  164. $params = array( ':tzid' => $tzid );
  165. $qry = new AwlQuery('SELECT 1 FROM timezones WHERE tzid = :tzid', $params );
  166. if ( $qry->Exec('PUT',__LINE__,__FILE__) && $qry->rows() == 0 ) {
  167. $params[':olson_name'] = $calendar->GetOlsonName($tz);
  168. $params[':vtimezone'] = (isset($tz) ? $tz->Render() : null );
  169. $qry->QDo('INSERT INTO timezones (tzid, olson_name, active, vtimezone) VALUES(:tzid,:olson_name,false,:vtimezone)', $params );
  170. }
  171. $qry->QDo('UPDATE collection SET timezone = :tzid WHERE dav_name = :dav_name',
  172. array( ':tzid' => $tzid, ':dav_name' => $dav_resource->dav_name()) );
  173. }
  174. else {
  175. add_failure('set', $tag, 'HTTP/1.1 409 Conflict', translate("calendar-timezone property is only valid for a calendar."));
  176. }
  177. break;
  178. /**
  179. * The following properties are read-only, so they will cause the request to fail
  180. */
  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;
  196. /**
  197. * If we don't have any special processing for the property, we just store it verbatim (which will be an XML fragment).
  198. */
  199. default:
  200. $qry->QDo('SELECT set_dav_property( :dav_name, :user_no::integer, :tag::text, :value::text)',
  201. array( ':dav_name' => $dav_resource->dav_name(), ':user_no' => $request->user_no, ':tag' => $tag, ':value' => $content) );
  202. $success[$tag] = 1;
  203. break;
  204. }
  205. }
  206. foreach( $rmprops AS $k => $setting ) {
  207. $tag = $setting->GetNSTag();
  208. $content = $setting->RenderContent();
  209. switch( $tag ) {
  210. case 'DAV::resourcetype':
  211. add_failure('rm', $tag, 'HTTP/1.1 409 Conflict',
  212. translate("DAV::resourcetype may only be set to a new value, it may not be removed."), 'cannot-modify-protected-property');
  213. break;
  214. case 'urn:ietf:params:xml:ns:caldav:calendar-timezone':
  215. if ( $dav_resource->IsCollection() && $dav_resource->IsCalendar() && ! $dav_resource->IsBinding() ) {
  216. $qry->QDo('UPDATE collection SET timezone = NULL WHERE dav_name = :dav_name', array( ':dav_name' => $dav_resource->dav_name()) );
  217. }
  218. else {
  219. add_failure('rm', $tag, 'HTTP/1.1 409 Conflict',
  220. translate("calendar-timezone property is only valid for a calendar."), 'cannot-modify-protected-property');
  221. }
  222. break;
  223. /**
  224. * The following properties are read-only, so they will cause the request to fail
  225. */
  226. case 'http://calendarserver.org/ns/:getctag':
  227. case 'DAV::owner':
  228. case 'DAV::principal-collection-set':
  229. case 'urn:ietf:params:xml:ns:caldav:CALENDAR-USER-ADDRESS-SET':
  230. case 'urn:ietf:params:xml:ns:caldav:schedule-inbox-URL':
  231. case 'urn:ietf:params:xml:ns:caldav:schedule-outbox-URL':
  232. case 'DAV::getetag':
  233. case 'DAV::getcontentlength':
  234. case 'DAV::getcontenttype':
  235. case 'DAV::getlastmodified':
  236. case 'DAV::creationdate':
  237. case 'DAV::displayname':
  238. case 'DAV::lockdiscovery':
  239. case 'DAV::supportedlock':
  240. add_failure('rm', $tag, 'HTTP/1.1 409 Conflict', translate("Property is read-only"));
  241. dbg_error_log( 'PROPPATCH', ' RMProperty %s is read only and cannot be removed', $tag);
  242. break;
  243. /**
  244. * If we don't have any special processing then we must have to just delete it. Nonexistence is not failure.
  245. */
  246. default:
  247. $qry->QDo('DELETE FROM property WHERE dav_name=:dav_name AND property_name=:property_name',
  248. array( ':dav_name' => $dav_resource->dav_name(), ':property_name' => $tag) );
  249. $success[$tag] = 1;
  250. break;
  251. }
  252. }
  253. /**
  254. * If we have encountered any instances of failure, the whole damn thing fails.
  255. */
  256. if ( count($failure) > 0 ) {
  257. $qry->Rollback();
  258. $url = ConstructURL($request->path);
  259. $multistatus = new XMLElement('multistatus');
  260. array_unshift($failure,new XMLElement('responsedescription', translate("Some properties were not able to be changed.") ));
  261. array_unshift($failure,new XMLElement('href', $url));
  262. $response = $reply->DAVElement($multistatus,'response', $failure);
  263. if ( !empty($success) ) {
  264. $prop = new XMLElement('prop');
  265. foreach( $success AS $tag => $v ) {
  266. $reply->NSElement($prop, $tag);
  267. }
  268. $reply->DAVElement($response, 'propstat', array( $prop, new XMLElement( 'status', 'HTTP/1.1 424 Failed Dependency' )) );
  269. }
  270. $request->DoResponse( 207, $reply->Render($multistatus), 'text/xml; charset="utf-8"' );
  271. }
  272. /**
  273. * Otherwise we will try and do the SQL. This is inside a transaction, so PostgreSQL guarantees the atomicity
  274. */
  275. if ( $qry->Commit() ) {
  276. $cache = getCacheInstance();
  277. $cache_ns = null;
  278. if ( $dav_resource->IsPrincipal() ) {
  279. $cache_ns = 'principal-'.$dav_resource->dav_name();
  280. }
  281. else if ( $dav_resource->IsCollection() ) {
  282. // Uncache anything to do with the collection
  283. $cache_ns = 'collection-'.$dav_resource->dav_name();
  284. }
  285. if ( isset($cache_ns) ) $cache->delete( $cache_ns, null );
  286. if ( $request->PreferMinimal() ) {
  287. $request->DoResponse(200); // Does not return.
  288. }
  289. $url = ConstructURL($request->path);
  290. $multistatus = new XMLElement('multistatus');
  291. $response = $multistatus->NewElement('response');
  292. $reply->DAVElement($response,'href', $url);
  293. $reply->DAVElement($response,'responsedescription', translate("All requested changes were made.") );
  294. $prop = new XMLElement('prop');
  295. foreach( $success AS $tag => $v ) {
  296. $reply->NSElement($prop, $tag);
  297. }
  298. $reply->DAVElement($response, 'propstat', array( $prop, new XMLElement( 'status', 'HTTP/1.1 200 OK' )) );
  299. $url = ConstructURL($request->path);
  300. array_unshift( $failure, new XMLElement('href', $url ) );
  301. $request->DoResponse( 207, $reply->Render($multistatus), 'text/xml; charset="utf-8"' );
  302. }
  303. /**
  304. * Or it was all crap.
  305. */
  306. $request->DoResponse( 500 );
  307. exit(0); // unneccessary