PageRenderTime 54ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/inc/caldav-PROPFIND.php

https://gitlab.com/tiggerben/davical
PHP | 284 lines | 225 code | 29 blank | 30 comment | 74 complexity | cfd14d74674773cf9f4759f2620a4e40 MD5 | raw file
  1. <?php
  2. /**
  3. * CalDAV Server - handle PROPFIND method
  4. *
  5. * @package davical
  6. * @subpackage propfind
  7. * @author Andrew McMillan <andrew@catalyst.net.nz>
  8. * @copyright Catalyst .Net Ltd, Andrew McMillan
  9. * @license http://gnu.org/copyleft/gpl.html GNU GPL v2 or later
  10. */
  11. dbg_error_log('PROPFIND', 'method handler');
  12. $request->NeedPrivilege( array('DAV::read', 'urn:ietf:params:xml:ns:caldav:read-free-busy','DAV::read-current-user-privilege-set') );
  13. require_once('iCalendar.php');
  14. require_once('XMLDocument.php');
  15. require_once('DAVResource.php');
  16. $reply = new XMLDocument( array( 'DAV:' => '' ) );
  17. if ( !isset($request->xml_tags) ) {
  18. // Empty body indicates DAV::allprop request according to RFC4918
  19. $property_list = array('DAV::allprop');
  20. }
  21. else {
  22. $position = 0;
  23. $xmltree = BuildXMLTree( $request->xml_tags, $position);
  24. if ( !is_object($xmltree) ) {
  25. $request->DoResponse( 403, translate("Request body is not valid XML data!") );
  26. }
  27. $allprop = $xmltree->GetPath('/DAV::propfind/*');
  28. $property_list = array();
  29. foreach( $allprop AS $k1 => $propwrap ) {
  30. switch ( $propwrap->GetNSTag() ) {
  31. case 'DAV::allprop':
  32. $property_list[] = 'DAV::allprop';
  33. break;
  34. case 'DAV::propname':
  35. $property_list[] = 'DAV::propname';
  36. break;
  37. default: // prop, include
  38. $subprop = $propwrap->GetElements();
  39. foreach( $subprop AS $k => $v ) {
  40. if ( is_object($v) && method_exists($v,'GetTag') ) $property_list[] = $v->GetNSTag();
  41. }
  42. }
  43. }
  44. }
  45. /**
  46. * Add the calendar-proxy-read/write pseudocollections
  47. * @param responses array of responses to which to add the collections
  48. */
  49. function add_proxy_response( $which, $parent_path ) {
  50. global $request, $reply, $c, $session, $property_list;
  51. if ($parent_path != $request->principal->dav_name()) {
  52. dbg_error_log( 'PROPFIND', 'Not returning proxy response since "%s" != "%s"', $parent_path, $request->principal->dav_name() );
  53. return null; // Nothing to proxy for
  54. }
  55. $collection = (object) '';
  56. if ( $which == 'read' ) {
  57. $proxy_group = $request->principal->ReadProxyGroup();
  58. } else if ( $which == 'write' ) {
  59. $proxy_group = $request->principal->WriteProxyGroup();
  60. }
  61. dbg_error_log( 'PROPFIND', 'Returning proxy response to "%s" for "%s"', $which, $parent_path );
  62. $collection->parent_container = $parent_path;
  63. $collection->dav_name = $parent_path.'calendar-proxy-'.$which.'/';
  64. $collection->is_calendar = 'f';
  65. $collection->is_addressbook = 'f';
  66. $collection->is_principal = 't';
  67. $collection->is_proxy = 't';
  68. $collection->proxy_type = $which;
  69. $collection->type = 'proxy';
  70. $collection->dav_displayname = $collection->dav_name;
  71. $collection->collection_id = 0;
  72. $collection->user_no = $session->user_no;
  73. $collection->username = $session->username;
  74. $collection->email = $session->email;
  75. $collection->created = date('Ymd\THis');
  76. $collection->dav_etag = md5($c->system_name . $collection->dav_name . implode($proxy_group) );
  77. $collection->proxy_for = $proxy_group;
  78. $collection->resourcetypes = sprintf('<DAV::collection/><http://calendarserver.org/ns/:calendar-proxy-%s/>', $which);
  79. $collection->in_freebusy_set = 'f';
  80. $collection->schedule_transp = 'transp';
  81. $collection->timezone = null;
  82. $collection->description = '';
  83. $resource = new DAVResource($collection);
  84. return $resource->RenderAsXML($property_list, $reply);
  85. }
  86. /**
  87. * Get XML response for items in the collection
  88. * If '/' is requested, a list of visible users is given, otherwise
  89. * a list of calendars for the user which are parented by this path.
  90. */
  91. function get_collection_contents( $depth, $collection, $parent_path = null ) {
  92. global $c, $session, $request, $reply, $property_list;
  93. // for http header comparison
  94. function compare_val_with_re($val, $re){ return preg_match($re, $val)===1 ? 0 : 1; }
  95. $bound_from = $collection->bound_from();
  96. $bound_to = $collection->dav_name();
  97. if ( !isset($parent_path) ) $parent_path = $collection->dav_name();
  98. dbg_error_log('PROPFIND','Getting collection contents: Depth %d, Path: %s, Bound from: %s, Bound to: %s',
  99. $depth, $collection->dav_name(), $bound_from, $bound_to );
  100. $date_format = AwlDBDialect::HttpDateFormat;
  101. $responses = array();
  102. if ( ! $collection->IsCalendar() && ! $collection->IsAddressbook() ) {
  103. /**
  104. * Calendar/Addressbook collections may not contain collections, so we are only looking in the other ones
  105. */
  106. $params = array( ':session_principal' => $session->principal_id, ':scan_depth' => $c->permission_scan_depth );
  107. if ( $bound_from == '/' ) {
  108. $sql = "SELECT usr.*, '/' || username || '/' AS dav_name, md5(username || updated::text) AS dav_etag, ";
  109. $sql .= "to_char(joined at time zone 'GMT',$date_format) AS created, ";
  110. $sql .= "to_char(updated at time zone 'GMT',$date_format) AS modified, ";
  111. $sql .= 'FALSE AS is_calendar, TRUE AS is_principal, FALSE AS is_addressbook, \'principal\' AS type, ';
  112. $sql .= 'principal_id AS collection_id, ';
  113. $sql .= 'principal.* ';
  114. $sql .= 'FROM usr JOIN principal USING (user_no) ';
  115. $sql .= "WHERE (pprivs(:session_principal::int8,principal.principal_id,:scan_depth::int) & 1::BIT(24))::INT4::BOOLEAN ";
  116. $sql .= 'ORDER BY usr.user_no';
  117. }
  118. else {
  119. if ( !( isset($c->hide_bound) && (
  120. ((is_bool($c->hide_bound) || is_numeric($c->hide_bound)) && $c->hide_bound != false) ||
  121. (is_string($c->hide_bound) && preg_match($c->hide_bound, $_SERVER['HTTP_USER_AGENT'])) ||
  122. (is_array($c->hide_bound) && count(array_uintersect_assoc(
  123. array_change_key_case(apache_request_headers(), CASE_LOWER),
  124. array_change_key_case($c->hide_bound, CASE_LOWER),
  125. 'compare_val_with_re'))) ) ) ) {
  126. $qry = new AwlQuery('SELECT * FROM dav_binding WHERE dav_binding.parent_container = :this_dav_name ORDER BY bind_id',
  127. array(':this_dav_name' => $bound_from));
  128. if( $qry->Exec('PROPFIND',__LINE__,__FILE__) && $qry->rows() > 0 ) {
  129. while( $binding = $qry->Fetch() ) {
  130. $resource = new DAVResource($binding->dav_name);
  131. if ( $resource->IsExternal() ) {
  132. require_once("external-fetch.php");
  133. update_external ( $resource );
  134. }
  135. if ( $resource->HavePrivilegeTo('DAV::read', false) ) {
  136. $resource->set_bind_location( str_replace($bound_from,$bound_to,$binding->dav_name));
  137. $responses[] = $resource->RenderAsXML($property_list, $reply);
  138. if ( $depth > 0 ) {
  139. $responses = array_merge($responses, get_collection_contents( $depth - 1, $resource, $binding->dav_name ) );
  140. }
  141. }
  142. }
  143. }
  144. }
  145. $sql = 'SELECT principal.*, collection.*, \'collection\' AS type ';
  146. $sql .= 'FROM collection LEFT JOIN principal USING (user_no) ';
  147. $sql .= 'WHERE parent_container = :this_dav_name ';
  148. $sql .= ' ORDER BY collection_id';
  149. $params[':this_dav_name'] = $bound_from;
  150. unset($params[':session_principal']);
  151. unset($params[':scan_depth']);
  152. }
  153. $qry = new AwlQuery($sql, $params);
  154. if( $qry->Exec('PROPFIND',__LINE__,__FILE__) && $qry->rows() > 0 ) {
  155. while( $subcollection = $qry->Fetch() ) {
  156. $resource = new DAVResource($subcollection);
  157. if ( ! $resource->HavePrivilegeTo('DAV::read') ) continue;
  158. $resource->set_bind_location( str_replace($bound_from,$bound_to,$subcollection->dav_name));
  159. $responses[] = $resource->RenderAsXML($property_list, $reply);
  160. if ( $depth > 0 ) {
  161. $responses = array_merge($responses, get_collection_contents( $depth - 1, $resource,
  162. str_replace($resource->parent_path(), $parent_path, $resource->dav_name() ) ) );
  163. }
  164. }
  165. }
  166. if ( !( (isset($c->disable_caldav_proxy) && $c->disable_caldav_proxy != false) ||
  167. (isset($c->disable_caldav_proxy_propfind_collections) && (
  168. ((is_bool($c->disable_caldav_proxy_propfind_collections) || is_numeric($c->disable_caldav_proxy_propfind_collections)) && $c->disable_caldav_proxy_propfind_collections != false) ||
  169. (is_string($c->disable_caldav_proxy_propfind_collections) && preg_match($c->disable_caldav_proxy_propfind_collections, $_SERVER['HTTP_USER_AGENT'])) ||
  170. (is_array($c->disable_caldav_proxy_propfind_collections) && count(array_uintersect_assoc(
  171. array_change_key_case(apache_request_headers(), CASE_LOWER),
  172. array_change_key_case($c->disable_caldav_proxy_propfind_collections, CASE_LOWER),
  173. 'compare_val_with_re')))) ) ) && $collection->IsPrincipal() ) {
  174. // Caldav Proxy: 5.1 par. 2: Add child resources calendar-proxy-(read|write)
  175. dbg_error_log('PROPFIND','Adding calendar-proxy-read and write. Path: %s', $bound_from );
  176. $response = add_proxy_response('read', $bound_from );
  177. if ( isset($response) ) $responses[] = $response;
  178. $response = add_proxy_response('write', $bound_from );
  179. if ( isset($response) ) $responses[] = $response;
  180. }
  181. }
  182. /**
  183. * freebusy permission is not allowed to see the items in a collection. Must have at least read permission.
  184. */
  185. if ( $collection->HavePrivilegeTo('DAV::read', false) ) {
  186. dbg_error_log('PROPFIND','Getting collection items: Depth %d, Path: %s', $depth, $bound_from );
  187. $privacy_clause = ' ';
  188. $todo_clause = ' ';
  189. $time_limit_clause = ' ';
  190. if ( $collection->IsCalendar() ) {
  191. if ( ! $collection->HavePrivilegeTo('all', false) ) {
  192. $privacy_clause = " AND (calendar_item.class != 'PRIVATE' OR calendar_item.class IS NULL) ";
  193. }
  194. if ( isset($c->hide_TODO) && ($c->hide_TODO === true || (is_string($c->hide_TODO) && preg_match($c->hide_TODO, $_SERVER['HTTP_USER_AGENT']))) && ! $collection->HavePrivilegeTo('all') ) {
  195. $todo_clause = " AND caldav_data.caldav_type NOT IN ('VTODO') ";
  196. }
  197. if ( isset($c->hide_older_than) && intval($c->hide_older_than > 0) ) {
  198. $time_limit_clause = " AND (CASE WHEN caldav_data.caldav_type<>'VEVENT' OR calendar_item.dtstart IS NULL THEN true ELSE calendar_item.dtstart > (now() - interval '".intval($c->hide_older_than)." days') END) ";
  199. }
  200. }
  201. $sql = 'SELECT collection.*, principal.*, calendar_item.*, caldav_data.*, ';
  202. $sql .= "to_char(coalesce(calendar_item.created, caldav_data.created) at time zone 'GMT',$date_format) AS created, ";
  203. $sql .= "to_char(coalesce(calendar_item.last_modified, caldav_data.modified) at time zone 'GMT',$date_format) AS modified, ";
  204. $sql .= 'summary AS dav_displayname ';
  205. $sql .= 'FROM caldav_data LEFT JOIN calendar_item USING( dav_id, user_no, dav_name, collection_id) ';
  206. $sql .= 'LEFT JOIN collection USING(collection_id,user_no) LEFT JOIN principal USING(user_no) ';
  207. $sql .= 'WHERE collection.dav_name = :collection_dav_name '.$time_limit_clause.' '.$todo_clause.' '.$privacy_clause;
  208. if ( isset($c->strict_result_ordering) && $c->strict_result_ordering ) $sql .= " ORDER BY caldav_data.dav_id";
  209. $qry = new AwlQuery( $sql, array( ':collection_dav_name' => $bound_from) );
  210. if( $qry->Exec('PROPFIND',__LINE__,__FILE__) && $qry->rows() > 0 ) {
  211. while( $item = $qry->Fetch() ) {
  212. if ( $bound_from != $bound_to ) {
  213. $item->bound_from = $item->dav_name;
  214. $item->dav_name = str_replace($bound_from,$bound_to,$item->dav_name);
  215. }
  216. $resource = new DAVResource($item);
  217. $responses[] = $resource->RenderAsXML($property_list, $reply, $parent_path );
  218. }
  219. }
  220. }
  221. return $responses;
  222. }
  223. /**
  224. * Something that we can handle, at least roughly correctly.
  225. */
  226. $responses = array();
  227. if ( $request->IsProxyRequest() ) {
  228. $response = add_proxy_response($request->proxy_type, $request->principal->dav_name() );
  229. if ( isset($response) ) $responses[] = $response;
  230. }
  231. else {
  232. $resource = new DAVResource($request->path);
  233. if ( ! $resource->Exists() ) {
  234. $request->PreconditionFailed( 404, 'must-exist', translate('That resource is not present on this server.') );
  235. }
  236. $resource->NeedPrivilege('DAV::read');
  237. if ( $resource->IsExternal() ) {
  238. require_once("external-fetch.php");
  239. update_external ( $resource );
  240. }
  241. if ( $resource->IsCollection() ) {
  242. dbg_error_log('PROPFIND','Getting collection contents: Depth %d, Path: %s', $request->depth, $resource->dav_name() );
  243. $responses[] = $resource->RenderAsXML($property_list, $reply);
  244. if ( $request->depth > 0 ) {
  245. $responses = array_merge($responses, get_collection_contents( $request->depth - 1, $resource ) );
  246. }
  247. }
  248. elseif ( $request->HavePrivilegeTo('DAV::read',false) ) {
  249. $responses[] = $resource->RenderAsXML($property_list, $reply);
  250. }
  251. }
  252. $xmldoc = $reply->Render('multistatus', $responses);
  253. $etag = md5($xmldoc);
  254. header('ETag: "'.$etag.'"');
  255. $request->DoResponse( 207, $xmldoc, 'text/xml; charset="utf-8"' );