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