diff --git a/inc/caldav-PROPFIND.php b/inc/caldav-PROPFIND.php index 957e166c..7964fbd2 100644 --- a/inc/caldav-PROPFIND.php +++ b/inc/caldav-PROPFIND.php @@ -135,6 +135,10 @@ function get_collection_contents( $depth, $collection, $parent_path = null ) { if( $qry->Exec('PROPFIND',__LINE__,__FILE__) && $qry->rows() > 0 ) { while( $binding = $qry->Fetch() ) { $resource = new DAVResource($binding->dav_name); + if ( $resource->IsExternal() ) { + require_once("external-fetch.php"); + update_external ( $resource ); + } if ( $resource->HavePrivilegeTo('DAV::read', false) ) { $resource->set_bind_location( str_replace($bound_from,$bound_to,$binding->dav_name)); $responses[] = $resource->RenderAsXML($property_list, $reply); @@ -233,6 +237,10 @@ else { $request->PreconditionFailed( 404, 'must-exist', translate('That resource is not present on this server.') ); } $resource->NeedPrivilege('DAV::read'); + if ( $resource->IsExternal() ) { + require_once("external-fetch.php"); + update_external ( $resource ); + } if ( $resource->IsCollection() ) { dbg_error_log('PROPFIND','Getting collection contents: Depth %d, Path: %s', $request->depth, $resource->dav_name() ); $responses[] = $resource->RenderAsXML($property_list, $reply); diff --git a/inc/caldav-REPORT.php b/inc/caldav-REPORT.php index c2d77b2f..ca1848a7 100644 --- a/inc/caldav-REPORT.php +++ b/inc/caldav-REPORT.php @@ -61,15 +61,17 @@ switch( $xmltree->GetTag() ) { include("caldav-REPORT-pps-set.php"); exit; // Not that it should return anyway. case 'DAV::sync-collection': - require_once("external-fetch.php"); - if ( $target->IsExternal() ) - update_external ( $target ); + if ( $target->IsExternal() ) { + require_once("external-fetch.php"); + update_external ( $target ); + } include("caldav-REPORT-sync-collection.php"); exit; // Not that it should return anyway. case 'DAV::expand-property': - require_once("external-fetch.php"); - if ( $target->IsExternal() ) - update_external ( $target ); + if ( $target->IsExternal() ) { + require_once("external-fetch.php"); + update_external ( $target ); + } include("caldav-REPORT-expand-property.php"); exit; // Not that it should return anyway. case 'DAV::principal-match': @@ -319,9 +321,10 @@ function component_to_xml( $properties, $item ) { return $response; } -require_once("external-fetch.php"); -if ( $target->IsExternal() ) - update_external ( $target ); +if ( $target->IsExternal() ) { + require_once("external-fetch.php"); + update_external ( $target ); +} if ( $xmltree->GetTag() == "urn:ietf:params:xml:ns:caldav:calendar-query" ) { $calquery = $xmltree->GetPath("/urn:ietf:params:xml:ns:caldav:calendar-query/*"); diff --git a/inc/external-fetch.php b/inc/external-fetch.php index 0dae0e7a..8f32bee2 100644 --- a/inc/external-fetch.php +++ b/inc/external-fetch.php @@ -36,7 +36,7 @@ function create_external ( $path,$is_calendar,$is_addressbook ) function fetch_external ( $bind_id, $min_age ) { - $sql = 'SELECT collection.*, collection.dav_name AS path, dav_binding.external_url AS external_url, EXTRACT(epoch FROM caldav_data.modified) AS updated FROM dav_binding LEFT JOIN collection ON (collection.collection_id=bound_source_id) JOIN caldav_data USING (collection_id) WHERE bind_id = :bind_id'; + $sql = 'SELECT collection.*, collection.dav_name AS path, dav_binding.external_url AS external_url FROM dav_binding LEFT JOIN collection ON (collection.collection_id=bound_source_id) WHERE bind_id = :bind_id'; $params = array( ':bind_id' => $bind_id ); if ( strlen ( $min_age ) > 2 ) { $sql .= ' AND collection.modified + interval :interval > NOW()'; @@ -53,36 +53,43 @@ function fetch_external ( $bind_id, $min_age ) $ics = curl_exec ( $curl ); $info = curl_getinfo ( $curl ); if ( $info['filetime'] <= $row->updated ) { - dbg_error_log("request", "external resource unchanged " . $info['filetime'] ); + dbg_error_log("external", "external resource unchanged " . $info['filetime'] ); curl_close ( $curl ); $qry = new AwlQuery( 'UPDATE collection SET modified=NOW() WHERE collection_id = :cid', array ( ':cid' => $row->collection_id ) ); $qry->Exec('DAVResource'); return true; } - dbg_error_log("request", "external resource changed, re importing" . $info['filetime'] ); + dbg_error_log("external", "external resource changed, re importing" . $info['filetime'] ); curl_setopt ( $curl, CURLOPT_NOBODY, false ); $ics = curl_exec ( $curl ); curl_close ( $curl ); if ( is_string ( $ics ) && strlen ( $ics ) > 20 ) { - $qry = new AwlQuery( 'UPDATE collection SET modified=NOW() WHERE collection_id = :cid', array ( ':cid' => $row->collection_id ) ); - $qry->Exec('DAVResource'); + $qry = new AwlQuery( 'UPDATE collection SET modified=NOW(), dav_etag=:etag WHERE collection_id = :cid', + array ( ':cid' => $row->collection_id, ':etag' => md5($ics) ) ); + $qry->Exec('DAVResource'); require_once ( 'caldav-PUT-functions.php'); import_collection ( $ics , $row->user_no, $row->path, 'External Fetch' , false ) ; return true; } - } + } + else { + dbg_error_log("external", "external resource not found"); + } return false; } function update_external ( $request ) { - global $c; + global $c; if ( $c->external_refresh < 1 ) return ; $sql = 'SELECT bind_id from dav_binding LEFT JOIN collection ON (collection.collection_id=bound_source_id) WHERE dav_binding.dav_name = :dav_name AND collection.modified + interval :interval < NOW()'; $qry = new AwlQuery( $sql, array ( ':dav_name' => $request->dav_name(), ':interval' => $c->external_refresh . ' minutes' ) ); + dbg_error_log("external", "checking if external resource needs update"); if ( $qry->Exec('DAVResource') && $qry->rows() > 0 && $row = $qry->Fetch() ) { - if ( $row->bind_id != 0 ) - fetch_external ( $row->bind_id ); + if ( $row->bind_id != 0 ) { + dbg_error_log("external", "external resource needs updating, this might take a minute"); + fetch_external ( $row->bind_id ); + } } }