DAViCal
caldav-PROPFIND.php
1 <?php
11 dbg_error_log('PROPFIND', 'method handler');
12 
13 $request->NeedPrivilege( array('DAV::read', 'urn:ietf:params:xml:ns:caldav:read-free-busy','DAV::read-current-user-privilege-set') );
14 
15 require_once('iCalendar.php');
16 require_once('XMLDocument.php');
17 require_once('DAVResource.php');
18 
19 $reply = new XMLDocument( array( 'DAV:' => '' ) );
20 
21 if ( !isset($request->xml_tags) ) {
22  // Empty body indicates DAV::allprop request according to RFC4918
23  $property_list = array('DAV::allprop');
24 }
25 else {
26  $xmltree = BuildXMLTree( $request->xml_tags );
27  if ( !is_object($xmltree) ) {
28  $request->DoResponse( 403, translate("Request body is not valid XML data!") );
29  }
30  $allprop = $xmltree->GetPath('/DAV::propfind/*');
31  $property_list = array();
32  foreach( $allprop AS $k1 => $propwrap ) {
33  switch ( $propwrap->GetNSTag() ) {
34  case 'DAV::allprop':
35  $property_list[] = 'DAV::allprop';
36  break;
37  case 'DAV::propname':
38  $property_list[] = 'DAV::propname';
39  break;
40  default: // prop, include
41  $subprop = $propwrap->GetElements();
42  foreach( $subprop AS $k => $v ) {
43  if ( is_object($v) && method_exists($v,'GetTag') ) $property_list[] = $v->GetNSTag();
44  }
45  }
46  }
47 }
48 
53 function add_proxy_response( $which, $parent_path ) {
54  global $request, $reply, $c, $session, $property_list;
55 
56  if ($parent_path != $request->principal->dav_name()) {
57  dbg_error_log( 'PROPFIND', 'Not returning proxy response since "%s" != "%s"', $parent_path, $request->principal->dav_name() );
58  return null; // Nothing to proxy for
59  }
60 
61  $collection = (object) '';
62  if ( $which == 'read' ) {
63  $proxy_group = $request->principal->ReadProxyGroup();
64  } else if ( $which == 'write' ) {
65  $proxy_group = $request->principal->WriteProxyGroup();
66  }
67 
68  dbg_error_log( 'PROPFIND', 'Returning proxy response to "%s" for "%s"', $which, $parent_path );
69 
70  $collection->parent_container = $parent_path;
71  $collection->dav_name = $parent_path.'calendar-proxy-'.$which.'/';
72  $collection->is_calendar = 'f';
73  $collection->is_addressbook = 'f';
74  $collection->is_principal = 't';
75  $collection->is_proxy = 't';
76  $collection->proxy_type = $which;
77  $collection->type = 'proxy';
78  $collection->dav_displayname = $collection->dav_name;
79  $collection->collection_id = 0;
80  $collection->user_no = $session->user_no;
81  $collection->username = $session->username;
82  $collection->email = $session->email;
83  $collection->created = date('Ymd\THis');
84  $collection->dav_etag = md5($c->system_name . $collection->dav_name . implode($proxy_group) );
85  $collection->proxy_for = $proxy_group;
86  $collection->resourcetypes = sprintf('<DAV::principal/><DAV::collection/><http://calendarserver.org/ns/:calendar-proxy-%s/>', $which);
87  $collection->in_freebusy_set = 'f';
88  $collection->schedule_transp = 'transp';
89  $collection->timezone = null;
90  $collection->description = '';
91 
92  $resource = new DAVResource($collection);
93  return $resource->RenderAsXML($property_list, $reply);
94 
95 }
96 
97 
103 function get_collection_contents( $depth, $collection, $parent_path = null ) {
104  global $c, $session, $request, $reply, $property_list;
105 
106  // for http header comparison
107  if (! function_exists ('compare_val_with_re') ) {
108  function compare_val_with_re($val, $re){ return preg_match($re, $val)===1 ? 0 : 1; }
109  }
110 
111  $bound_from = $collection->bound_from();
112  $bound_to = $collection->dav_name();
113  if ( !isset($parent_path) ) $parent_path = $collection->dav_name();
114  dbg_error_log('PROPFIND','Getting collection contents: Depth %d, Path: %s, Bound from: %s, Bound to: %s',
115  $depth, $collection->dav_name(), $bound_from, $bound_to );
116 
117  $date_format = AwlDBDialect::HttpDateFormat;
118  $responses = array();
119  if ( ! $collection->IsCalendar() && ! $collection->IsAddressbook() ) {
123  $params = array( ':session_principal' => $session->principal_id, ':scan_depth' => $c->permission_scan_depth );
124  if ( $bound_from == '/' ) {
125  $sql = "SELECT usr.*, '/' || username || '/' AS dav_name, md5(username || updated::text) AS dav_etag, ";
126  $sql .= "to_char(joined at time zone 'GMT',$date_format) AS created, ";
127  $sql .= "to_char(updated at time zone 'GMT',$date_format) AS modified, ";
128  $sql .= 'FALSE AS is_calendar, TRUE AS is_principal, FALSE AS is_addressbook, \'principal\' AS type, ';
129  $sql .= 'principal_id AS collection_id, ';
130  $sql .= 'principal.* ';
131  $sql .= 'FROM usr JOIN principal USING (user_no) ';
132  $sql .= "WHERE (pprivs(:session_principal::int8,principal.principal_id,:scan_depth::int) & 1::BIT(24))::INT4::BOOLEAN ";
133  $sql .= 'ORDER BY usr.user_no';
134  }
135  else {
136  if ( !( isset($c->hide_bound) && (
137  ((is_bool($c->hide_bound) || is_numeric($c->hide_bound)) && $c->hide_bound != false) ||
138  (is_string($c->hide_bound) && preg_match($c->hide_bound, $_SERVER['HTTP_USER_AGENT'])) ||
139  (is_array($c->hide_bound) && count(array_uintersect_assoc(
140  array_change_key_case(apache_request_headers(), CASE_LOWER),
141  array_change_key_case($c->hide_bound, CASE_LOWER),
142  'compare_val_with_re'))) ) ) ) {
143  $qry = new AwlQuery('SELECT * FROM dav_binding WHERE dav_binding.parent_container = :this_dav_name ORDER BY bind_id',
144  array(':this_dav_name' => $bound_from));
145  if( $qry->Exec('PROPFIND',__LINE__,__FILE__) && $qry->rows() > 0 ) {
146  while( $binding = $qry->Fetch() ) {
147  $resource = new DAVResource($binding->dav_name);
148  if ( $resource->IsExternal() ) {
149  require_once("external-fetch.php");
150  update_external ( $resource );
151  }
152  if ( $resource->HavePrivilegeTo('DAV::read', false) ) {
153  $resource->set_bind_location( str_replace($bound_from,$bound_to,$binding->dav_name));
154  $responses[] = $resource->RenderAsXML($property_list, $reply);
155  if ( $depth > 0 ) {
156  $responses = array_merge($responses, get_collection_contents( $depth - 1, $resource, $binding->dav_name ) );
157  }
158  }
159  }
160  }
161  }
162 
163  $sql = 'SELECT principal.*, collection.*, \'collection\' AS type ';
164  $sql .= 'FROM collection LEFT JOIN principal USING (user_no) ';
165  $sql .= 'WHERE parent_container = :this_dav_name ';
166  $sql .= ' ORDER BY collection_id';
167  $params[':this_dav_name'] = $bound_from;
168  unset($params[':session_principal']);
169  unset($params[':scan_depth']);
170  }
171  $qry = new AwlQuery($sql, $params);
172 
173  if( $qry->Exec('PROPFIND',__LINE__,__FILE__) && $qry->rows() > 0 ) {
174  while( $subcollection = $qry->Fetch() ) {
175  $resource = new DAVResource($subcollection);
176  if ( ! $resource->HavePrivilegeTo('DAV::read') ) continue;
177  $resource->set_bind_location( str_replace($bound_from,$bound_to,$subcollection->dav_name));
178  $responses[] = $resource->RenderAsXML($property_list, $reply);
179  if ( $depth > 0 ) {
180  $responses = array_merge($responses, get_collection_contents( $depth - 1, $resource,
181  str_replace($resource->parent_path(), $parent_path, $resource->dav_name() ) ) );
182  }
183  }
184  }
185 
186  if ( !( (isset($c->disable_caldav_proxy) && $c->disable_caldav_proxy != false) ||
187  (isset($c->disable_caldav_proxy_propfind_collections) && (
188  ((is_bool($c->disable_caldav_proxy_propfind_collections) || is_numeric($c->disable_caldav_proxy_propfind_collections)) && $c->disable_caldav_proxy_propfind_collections != false) ||
189  (is_string($c->disable_caldav_proxy_propfind_collections) && preg_match($c->disable_caldav_proxy_propfind_collections, $_SERVER['HTTP_USER_AGENT'])) ||
190  (is_array($c->disable_caldav_proxy_propfind_collections) && count(array_uintersect_assoc(
191  array_change_key_case(apache_request_headers(), CASE_LOWER),
192  array_change_key_case($c->disable_caldav_proxy_propfind_collections, CASE_LOWER),
193  'compare_val_with_re')))) ) ) && $collection->IsPrincipal() ) {
194  // Caldav Proxy: 5.1 par. 2: Add child resources calendar-proxy-(read|write)
195  dbg_error_log('PROPFIND','Adding calendar-proxy-read and write. Path: %s', $bound_from );
196  $response = add_proxy_response('read', $bound_from );
197  if ( isset($response) ) $responses[] = $response;
198  $response = add_proxy_response('write', $bound_from );
199  if ( isset($response) ) $responses[] = $response;
200  }
201  }
202 
206  if ( $collection->HavePrivilegeTo('DAV::read', false) ) {
207  dbg_error_log('PROPFIND','Getting collection items: Depth %d, Path: %s', $depth, $bound_from );
208  $privacy_clause = ' ';
209  $todo_clause = ' ';
210  $time_limit_clause = ' ';
211  if ( $collection->IsCalendar() ) {
212  if ( ! $collection->HavePrivilegeTo('all', false) ) {
213  $privacy_clause = " AND (calendar_item.class != 'PRIVATE' OR calendar_item.class IS NULL) ";
214  }
215 
216  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') ) {
217  $todo_clause = " AND caldav_data.caldav_type NOT IN ('VTODO') ";
218  }
219 
220  if ( isset($c->hide_older_than) && intval($c->hide_older_than > 0) ) {
221  $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) ";
222  }
223  }
224 
225  $sql = 'SELECT collection.*, principal.*, calendar_item.*, caldav_data.*, ';
226  $sql .= "to_char(coalesce(calendar_item.created, caldav_data.created) at time zone 'GMT',$date_format) AS created, ";
227  $sql .= "to_char(coalesce(calendar_item.last_modified, caldav_data.modified) at time zone 'GMT',$date_format) AS modified, ";
228  $sql .= 'summary AS dav_displayname ';
229  $sql .= 'FROM caldav_data LEFT JOIN calendar_item USING( dav_id, user_no, dav_name, collection_id) ';
230  $sql .= 'LEFT JOIN collection USING(collection_id,user_no) LEFT JOIN principal USING(user_no) ';
231  $sql .= 'WHERE collection.dav_name = :collection_dav_name '.$time_limit_clause.' '.$todo_clause.' '.$privacy_clause;
232  if ( isset($c->strict_result_ordering) && $c->strict_result_ordering ) $sql .= " ORDER BY caldav_data.dav_id";
233  $qry = new AwlQuery( $sql, array( ':collection_dav_name' => $bound_from) );
234  if( $qry->Exec('PROPFIND',__LINE__,__FILE__) && $qry->rows() > 0 ) {
235  while( $item = $qry->Fetch() ) {
236  if ( $bound_from != $bound_to ) {
237  $item->bound_from = $item->dav_name;
238  $item->dav_name = str_replace($bound_from,$bound_to,$item->dav_name);
239  }
240  $resource = new DAVResource($item);
241  $responses[] = $resource->RenderAsXML($property_list, $reply, $parent_path );
242  }
243  }
244  }
245 
246  return $responses;
247 }
248 
249 
250 
254 $responses = array();
255 if ( $request->IsProxyRequest() ) {
256  $response = add_proxy_response($request->proxy_type, $request->principal->dav_name() );
257  if ( isset($response) ) $responses[] = $response;
258 }
259 else {
260  $resource = new DAVResource($request->path);
261  if ( ! $resource->Exists() ) {
262  $request->PreconditionFailed( 404, 'must-exist', translate('That resource is not present on this server.') );
263  }
264  $resource->NeedPrivilege('DAV::read');
265  if ( $resource->IsExternal() ) {
266  require_once("external-fetch.php");
267  update_external ( $resource );
268  }
269  if ( $resource->IsCollection() ) {
270  dbg_error_log('PROPFIND','Getting collection contents: Depth %d, Path: %s', $request->depth, $resource->dav_name() );
271  $responses[] = $resource->RenderAsXML($property_list, $reply);
272  if ( $request->depth > 0 ) {
273  $responses = array_merge($responses, get_collection_contents( $request->depth - 1, $resource ) );
274  }
275  }
276  elseif ( $request->HavePrivilegeTo('DAV::read',false) ) {
277  $responses[] = $resource->RenderAsXML($property_list, $reply);
278  }
279 }
280 
281 $xmldoc = $reply->Render('multistatus', $responses);
282 $etag = md5($xmldoc);
283 header('ETag: "'.$etag.'"');
284 $request->DoResponse( 207, $xmldoc, 'text/xml; charset="utf-8"' );
285