3 * CalDAV Server - handle sync-collection report (draft-daboo-webdav-sync-01)
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 or later
15 * Build the array of properties to include in the report output
17 $sync_level = $xmltree->GetPath('/DAV::sync-collection/DAV::sync-level');
18 if ( empty($sync_level) ) {
19 $sync_level = $request->depth
;
22 $sync_level = $sync_level[0]->GetContent();
23 if ( $sync_level == 'infinity' )
24 $sync_level = DEPTH_INFINITY
;
29 if ( $sync_level == DEPTH_INFINITY
) {
30 $request->PreconditionFailed(403, 'DAV::sync-traversal-supported','This server does not support sync-traversal');
33 $sync_tokens = $xmltree->GetPath('/DAV::sync-collection/DAV::sync-token');
34 if ( isset($sync_tokens[0]) ) $sync_token = $sync_tokens[0]->GetContent();
35 if ( !isset($sync_token) ) $sync_token = 0;
36 $sync_token = intval(str_ireplace('data:,', '', $sync_token ));
37 dbg_error_log( 'sync', " sync-token: %s", $sync_token );
40 $props = $xmltree->GetPath('/DAV::sync-collection/DAV::prop/*');
41 if ( !empty($props) ) {
42 foreach( $props AS $k => $v ) {
43 $proplist[] = $v->GetNSTag();
47 function display_status( $status_code ) {
48 return sprintf( 'HTTP/1.1 %03d %s', intval($status_code), getStatusMessage($status_code) );
51 $collection = new DAVResource( $request->path
);
52 if ( !$collection->Exists() ) {
53 $request->DoResponse( 404 );
55 $bound_from = $collection->bound_from();
56 $collection_path = $collection->dav_name();
57 $request_via_binding = ($bound_from != $collection_path);
59 $params = array( ':collection_id' => $collection->GetProperty('collection_id'), ':sync_token' => $sync_token );
60 $sql = "SELECT new_sync_token( :sync_token, :collection_id)";
61 $qry = new AwlQuery($sql, $params);
62 if ( !$qry->Exec("REPORT",__LINE__
,__FILE__
) ||
$qry->rows() <= 0 ) {
63 $request->DoResponse( 500, translate("Database error") );
67 if ( !isset($row->new_sync_token
) ) {
68 /** If we got a null back then they gave us a sync token we know not of, so provide a full sync */
70 $params[':sync_token'] = $sync_token;
71 if ( !$qry->QDo($sql, $params) ||
$qry->rows() <= 0 ) {
72 $request->DoResponse( 500, translate("Database error") );
76 $new_token = $row->new_sync_token
;
78 if ( $sync_token == $new_token ) {
79 // No change, so we just re-send the old token.
80 $responses[] = new XMLElement( 'sync-token', 'data:,'.$new_token );
84 if ( isset($c->hide_older_than
) && intval($c->hide_older_than
) > 0 )
85 $hide_older = " 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)";
87 if ( $sync_token == 0 ) {
89 SELECT collection.*, calendar_item.*, caldav_data.*, addressbook_resource.*, 201 AS sync_status FROM collection
90 LEFT JOIN caldav_data USING (collection_id)
91 LEFT JOIN calendar_item USING (dav_id)
92 LEFT JOIN addressbook_resource USING (dav_id)
93 WHERE collection.collection_id = :collection_id $hide_older
94 ORDER BY collection.collection_id, caldav_data.dav_id
96 unset($params[':sync_token']);
100 SELECT collection.*, calendar_item.*, caldav_data.*, addressbook_resource.*, sync_changes.*
101 FROM collection LEFT JOIN sync_changes USING(collection_id)
102 LEFT JOIN caldav_data USING (collection_id,dav_id)
103 LEFT JOIN calendar_item USING (collection_id,dav_id)
104 LEFT JOIN addressbook_resource USING (dav_id)
105 WHERE collection.collection_id = :collection_id $hide_older
106 AND sync_time >= (SELECT modification_time FROM sync_tokens WHERE sync_token = :sync_token)
108 if ( isset($c->strict_result_ordering
) && $c->strict_result_ordering
) {
109 $sql .= " ORDER BY collection.collection_id, lower(sync_changes.dav_name), sync_changes.sync_time";
112 $sql .= " ORDER BY collection.collection_id, sync_changes.dav_name, sync_changes.sync_time";
115 $qry = new AwlQuery($sql, $params );
120 if ( $qry->Exec("REPORT",__LINE__
,__FILE__
) ) {
121 if ( $qry->rows() > 50 ) {
122 // If there are more than 50 rows to send we should not send full data in response ...
123 $c->sync_resource_data_ok
= false;
125 while( $object = $qry->Fetch() ) {
126 if ( $request_via_binding )
127 $object->dav_name
= str_replace( $bound_from, $collection_path, $object->dav_name
);
129 if ( $object->dav_name
== $last_dav_name ) {
130 /** The complex case: this is the second or subsequent for this dav_id */
131 if ( $object->sync_status
== 404 ) {
132 array_pop($responses);
134 new XMLElement( 'href', ConstructURL($object->dav_name
) ),
135 new XMLElement( 'status', display_status($object->sync_status
) )
137 $responses[] = new XMLElement( 'response', $resultset );
140 else if ( $object->sync_status
== 201 && $first_status == 404 ) {
141 // ... Delete ... Create ... is indicated as a create, but don't forget we started with a delete
142 array_pop($responses);
143 $dav_resource = new DAVResource($object);
144 $resultset = $dav_resource->GetPropStat($proplist,$reply);
145 array_unshift($resultset, new XMLElement( 'href', ConstructURL($object->dav_name
)));
146 $responses[] = new XMLElement( 'response', $resultset );
149 * the object existed at start and we have multiple modifications,
151 * the object didn't exist at start and we have subsequent modifications,
153 * in either case we simply stick with our existing report.
157 /** The simple case: this is the first one for this dav_id */
158 if ( $object->sync_status
== 404 ) {
160 new XMLElement( 'href', ConstructURL($object->dav_name
) ),
161 new XMLElement( 'status', display_status($object->sync_status
) )
166 $dav_resource = new DAVResource($object);
167 $resultset = $dav_resource->GetPropStat($proplist,$reply);
168 array_unshift($resultset, new XMLElement( 'href', ConstructURL($object->dav_name
)));
169 $first_status = $object->sync_status
;
171 $responses[] = new XMLElement( 'response', $resultset );
172 $last_dav_name = $object->dav_name
;
175 $responses[] = new XMLElement( 'sync-token', 'data:,'.$new_token );
178 $request->DoResponse( 500, translate("Database error") );
182 $multistatus = new XMLElement( "multistatus", $responses, $reply->GetXmlNsArray() );
184 $request->XMLResponse( 207, $multistatus );