PageRenderTime 74ms CodeModel.GetById 40ms RepoModel.GetById 5ms app.codeStats 0ms

/inc/caldav-PROPFIND.php

https://gitlab.com/davical-project/davical
PHP | 286 lines | 227 code | 29 blank | 30 comment | 75 complexity | 25aca14e179998ecc26b0613985b07e1 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause
  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::principal/><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. if (! function_exists ('compare_val_with_re') ) {
  95. function compare_val_with_re($val, $re){ return preg_match($re, $val)===1 ? 0 : 1; }
  96. }
  97. $bound_from = $collection->bound_from();
  98. $bound_to = $collection->dav_name();
  99. if ( !isset($parent_path) ) $parent_path = $collection->dav_name();
  100. dbg_error_log('PROPFIND','Getting collection contents: Depth %d, Path: %s, Bound from: %s, Bound to: %s',
  101. $depth, $collection->dav_name(), $bound_from, $bound_to );
  102. $date_format = AwlDBDialect::HttpDateFormat;
  103. $responses = array();
  104. if ( ! $collection->IsCalendar() && ! $collection->IsAddressbook() ) {
  105. /**
  106. * Calendar/Addressbook collections may not contain collections, so we are only looking in the other ones
  107. */
  108. $params = array( ':session_principal' => $session->principal_id, ':scan_depth' => $c->permission_scan_depth );
  109. if ( $bound_from == '/' ) {
  110. $sql = "SELECT usr.*, '/' || username || '/' AS dav_name, md5(username || updated::text) AS dav_etag, ";
  111. $sql .= "to_char(joined at time zone 'GMT',$date_format) AS created, ";
  112. $sql .= "to_char(updated at time zone 'GMT',$date_format) AS modified, ";
  113. $sql .= 'FALSE AS is_calendar, TRUE AS is_principal, FALSE AS is_addressbook, \'principal\' AS type, ';
  114. $sql .= 'principal_id AS collection_id, ';
  115. $sql .= 'principal.* ';
  116. $sql .= 'FROM usr JOIN principal USING (user_no) ';
  117. $sql .= "WHERE (pprivs(:session_principal::int8,principal.principal_id,:scan_depth::int) & 1::BIT(24))::INT4::BOOLEAN ";
  118. $sql .= 'ORDER BY usr.user_no';
  119. }
  120. else {
  121. if ( !( isset($c->hide_bound) && (
  122. ((is_bool($c->hide_bound) || is_numeric($c->hide_bound)) && $c->hide_bound != false) ||
  123. (is_string($c->hide_bound) && preg_match($c->hide_bound, $_SERVER['HTTP_USER_AGENT'])) ||
  124. (is_array($c->hide_bound) && count(array_uintersect_assoc(
  125. array_change_key_case(apache_request_headers(), CASE_LOWER),
  126. array_change_key_case($c->hide_bound, CASE_LOWER),
  127. 'compare_val_with_re'))) ) ) ) {
  128. $qry = new AwlQuery('SELECT * FROM dav_binding WHERE dav_binding.parent_container = :this_dav_name ORDER BY bind_id',
  129. array(':this_dav_name' => $bound_from));
  130. if( $qry->Exec('PROPFIND',__LINE__,__FILE__) && $qry->rows() > 0 ) {
  131. while( $binding = $qry->Fetch() ) {
  132. $resource = new DAVResource($binding->dav_name);
  133. if ( $resource->IsExternal() ) {
  134. require_once("external-fetch.php");
  135. update_external ( $resource );
  136. }
  137. if ( $resource->HavePrivilegeTo('DAV::read', false) ) {
  138. $resource->set_bind_location( str_replace($bound_from,$bound_to,$binding->dav_name));
  139. $responses[] = $resource->RenderAsXML($property_list, $reply);
  140. if ( $depth > 0 ) {
  141. $responses = array_merge($responses, get_collection_contents( $depth - 1, $resource, $binding->dav_name ) );
  142. }
  143. }
  144. }
  145. }
  146. }
  147. $sql = 'SELECT principal.*, collection.*, \'collection\' AS type ';
  148. $sql .= 'FROM collection LEFT JOIN principal USING (user_no) ';
  149. $sql .= 'WHERE parent_container = :this_dav_name ';
  150. $sql .= ' ORDER BY collection_id';
  151. $params[':this_dav_name'] = $bound_from;
  152. unset($params[':session_principal']);
  153. unset($params[':scan_depth']);
  154. }
  155. $qry = new AwlQuery($sql, $params);
  156. if( $qry->Exec('PROPFIND',__LINE__,__FILE__) && $qry->rows() > 0 ) {
  157. while( $subcollection = $qry->Fetch() ) {
  158. $resource = new DAVResource($subcollection);
  159. if ( ! $resource->HavePrivilegeTo('DAV::read') ) continue;
  160. $resource->set_bind_location( str_replace($bound_from,$bound_to,$subcollection->dav_name));
  161. $responses[] = $resource->RenderAsXML($property_list, $reply);
  162. if ( $depth > 0 ) {
  163. $responses = array_merge($responses, get_collection_contents( $depth - 1, $resource,
  164. str_replace($resource->parent_path(), $parent_path, $resource->dav_name() ) ) );
  165. }
  166. }
  167. }
  168. if ( !( (isset($c->disable_caldav_proxy) && $c->disable_caldav_proxy != false) ||
  169. (isset($c->disable_caldav_proxy_propfind_collections) && (
  170. ((is_bool($c->disable_caldav_proxy_propfind_collections) || is_numeric($c->disable_caldav_proxy_propfind_collections)) && $c->disable_caldav_proxy_propfind_collections != false) ||
  171. (is_string($c->disable_caldav_proxy_propfind_collections) && preg_match($c->disable_caldav_proxy_propfind_collections, $_SERVER['HTTP_USER_AGENT'])) ||
  172. (is_array($c->disable_caldav_proxy_propfind_collections) && count(array_uintersect_assoc(
  173. array_change_key_case(apache_request_headers(), CASE_LOWER),
  174. array_change_key_case($c->disable_caldav_proxy_propfind_collections, CASE_LOWER),
  175. 'compare_val_with_re')))) ) ) && $collection->IsPrincipal() ) {
  176. // Caldav Proxy: 5.1 par. 2: Add child resources calendar-proxy-(read|write)
  177. dbg_error_log('PROPFIND','Adding calendar-proxy-read and write. Path: %s', $bound_from );
  178. $response = add_proxy_response('read', $bound_from );
  179. if ( isset($response) ) $responses[] = $response;
  180. $response = add_proxy_response('write', $bound_from );
  181. if ( isset($response) ) $responses[] = $response;
  182. }
  183. }
  184. /**
  185. * freebusy permission is not allowed to see the items in a collection. Must have at least read permission.
  186. */
  187. if ( $collection->HavePrivilegeTo('DAV::read', false) ) {
  188. dbg_error_log('PROPFIND','Getting collection items: Depth %d, Path: %s', $depth, $bound_from );
  189. $privacy_clause = ' ';
  190. $todo_clause = ' ';
  191. $time_limit_clause = ' ';
  192. if ( $collection->IsCalendar() ) {
  193. if ( ! $collection->HavePrivilegeTo('all', false) ) {
  194. $privacy_clause = " AND (calendar_item.class != 'PRIVATE' OR calendar_item.class IS NULL) ";
  195. }
  196. 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') ) {
  197. $todo_clause = " AND caldav_data.caldav_type NOT IN ('VTODO') ";
  198. }
  199. if ( isset($c->hide_older_than) && intval($c->hide_older_than > 0) ) {
  200. $time_limit_clause = " AND (CASE WHEN caldav_data.caldav_type<>'VEVENT' OR calendar_item.dtstart IS NULL OR calendar_item.rrule IS NOT NULL THEN true ELSE calendar_item.dtstart > (now() - interval '".intval($c->hide_older_than)." days') END) ";
  201. }
  202. }
  203. $sql = 'SELECT collection.*, principal.*, calendar_item.*, caldav_data.*, ';
  204. $sql .= "to_char(coalesce(calendar_item.created, caldav_data.created) at time zone 'GMT',$date_format) AS created, ";
  205. $sql .= "to_char(coalesce(calendar_item.last_modified, caldav_data.modified) at time zone 'GMT',$date_format) AS modified, ";
  206. $sql .= 'summary AS dav_displayname ';
  207. $sql .= 'FROM caldav_data LEFT JOIN calendar_item USING( dav_id, user_no, dav_name, collection_id) ';
  208. $sql .= 'LEFT JOIN collection USING(collection_id,user_no) LEFT JOIN principal USING(user_no) ';
  209. $sql .= 'WHERE collection.dav_name = :collection_dav_name '.$time_limit_clause.' '.$todo_clause.' '.$privacy_clause;
  210. if ( isset($c->strict_result_ordering) && $c->strict_result_ordering ) $sql .= " ORDER BY caldav_data.dav_id";
  211. $qry = new AwlQuery( $sql, array( ':collection_dav_name' => $bound_from) );
  212. if( $qry->Exec('PROPFIND',__LINE__,__FILE__) && $qry->rows() > 0 ) {
  213. while( $item = $qry->Fetch() ) {
  214. if ( $bound_from != $bound_to ) {
  215. $item->bound_from = $item->dav_name;
  216. $item->dav_name = str_replace($bound_from,$bound_to,$item->dav_name);
  217. }
  218. $resource = new DAVResource($item);
  219. $responses[] = $resource->RenderAsXML($property_list, $reply, $parent_path );
  220. }
  221. }
  222. }
  223. return $responses;
  224. }
  225. /**
  226. * Something that we can handle, at least roughly correctly.
  227. */
  228. $responses = array();
  229. if ( $request->IsProxyRequest() ) {
  230. $response = add_proxy_response($request->proxy_type, $request->principal->dav_name() );
  231. if ( isset($response) ) $responses[] = $response;
  232. }
  233. else {
  234. $resource = new DAVResource($request->path);
  235. if ( ! $resource->Exists() ) {
  236. $request->PreconditionFailed( 404, 'must-exist', translate('That resource is not present on this server.') );
  237. }
  238. $resource->NeedPrivilege('DAV::read');
  239. if ( $resource->IsExternal() ) {
  240. require_once("external-fetch.php");
  241. update_external ( $resource );
  242. }
  243. if ( $resource->IsCollection() ) {
  244. dbg_error_log('PROPFIND','Getting collection contents: Depth %d, Path: %s', $request->depth, $resource->dav_name() );
  245. $responses[] = $resource->RenderAsXML($property_list, $reply);
  246. if ( $request->depth > 0 ) {
  247. $responses = array_merge($responses, get_collection_contents( $request->depth - 1, $resource ) );
  248. }
  249. }
  250. elseif ( $request->HavePrivilegeTo('DAV::read',false) ) {
  251. $responses[] = $resource->RenderAsXML($property_list, $reply);
  252. }
  253. }
  254. $xmldoc = $reply->Render('multistatus', $responses);
  255. $etag = md5($xmldoc);
  256. header('ETag: "'.$etag.'"');
  257. $request->DoResponse( 207, $xmldoc, 'text/xml; charset="utf-8"' );