From 96a5a3120393a322a4b2ab2e670230a532581d2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CPaul?= <“p.kallnbach@gorilla-computing.de”> Date: Mon, 23 Jun 2014 21:55:11 +0200 Subject: [PATCH] Fixed fetching new external resources on BIND --- inc/caldav-PUT-functions.php | 2 +- inc/external-fetch.php | 59 +++++++++++++++++++----------------- 2 files changed, 33 insertions(+), 28 deletions(-) diff --git a/inc/caldav-PUT-functions.php b/inc/caldav-PUT-functions.php index e30ee372..bd5d55cc 100644 --- a/inc/caldav-PUT-functions.php +++ b/inc/caldav-PUT-functions.php @@ -28,7 +28,7 @@ $bad_events = null; /** * A regex which will match most reasonable timezones acceptable to PostgreSQL. */ -$tz_regex = ':^(Africa|America|Antarctica|Arctic|Asia|Atlantic|Australia|Brazil|Canada|Chile|Etc|Europe|Indian|Mexico|Mideast|Pacific|US)/[a-z_]+$:i'; +$GLOBALS['tz_regex'] = ':^(Africa|America|Antarctica|Arctic|Asia|Atlantic|Australia|Brazil|Canada|Chile|Etc|Europe|Indian|Mexico|Mideast|Pacific|US)/[a-z_]+$:i'; /** * This function launches an error diff --git a/inc/external-fetch.php b/inc/external-fetch.php index 6417feb5..1c2937a2 100644 --- a/inc/external-fetch.php +++ b/inc/external-fetch.php @@ -22,9 +22,9 @@ function create_external ( $path,$is_calendar,$is_addressbook ) if ($is_calendar) $resourcetypes .= ''; $qry = new AwlQuery(); if ( ! $qry->QDo( 'INSERT INTO collection ( user_no, parent_container, dav_name, dav_etag, dav_displayname, - is_calendar, is_addressbook, resourcetypes, created, modified ) + is_calendar, is_addressbook, resourcetypes, created ) VALUES( :user_no, :parent_container, :dav_name, :dav_etag, :dav_displayname, - :is_calendar, :is_addressbook, :resourcetypes, current_timestamp, current_timestamp )', + :is_calendar, :is_addressbook, :resourcetypes, current_timestamp )', array( ':user_no' => $request->user_no, ':parent_container' => '/.external/', @@ -55,43 +55,48 @@ function fetch_external ( $bind_id, $min_age = '1 hour' ) $sql .= ' ORDER BY modified DESC LIMIT 1'; $qry = new AwlQuery( $sql, $params ); if ( $qry->Exec('DAVResource') && $qry->rows() > 0 && $row = $qry->Fetch() ) { - $local_ts = new DateTime($row->modified); $curl = curl_init ( $row->external_url ); curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, true ); - curl_setopt ( $curl, CURLOPT_HEADER, true ); - curl_setopt ( $curl, CURLOPT_FILETIME, true ); - curl_setopt ( $curl, CURLOPT_NOBODY, true ); - curl_setopt ( $curl, CURLOPT_TIMEVALUE, $local_ts->format("U") ); - curl_setopt ( $curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE ); - dbg_error_log("external", "checking external resource for remote changes " . $row->external_url ); - $ics = curl_exec ( $curl ); - $info = curl_getinfo ( $curl ); - if ( $info['http_code'] === 304 || isset($info['filetime']) && new DateTime("@" . $info['filetime']) <= $local_ts ) { - dbg_error_log("external", "external resource unchanged " . $info['filetime'] . ' < ' . $local_ts->getTimestamp() ); - curl_close ( $curl ); - // BUGlet: should track server-time instead of local-time - $qry = new AwlQuery( 'UPDATE collection SET modified=NOW() WHERE collection_id = :cid', array ( ':cid' => $row->collection_id ) ); - $qry->Exec('DAVResource'); - return true; + if ( $row->modified ) { + $local_ts = new DateTime($row->modified); + curl_setopt ( $curl, CURLOPT_HEADER, true ); + curl_setopt ( $curl, CURLOPT_FILETIME, true ); + curl_setopt ( $curl, CURLOPT_NOBODY, true ); + curl_setopt ( $curl, CURLOPT_TIMEVALUE, $local_ts->format("U") ); + curl_setopt ( $curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE ); + dbg_error_log("external", "checking external resource for remote changes " . $row->external_url ); + $ics = curl_exec ( $curl ); + $info = curl_getinfo ( $curl ); + if ( $info['http_code'] === 304 || (isset($info['filetime']) && $info['filetime'] != -1 && new DateTime("@" . $info['filetime']) <= $local_ts )) { + dbg_error_log("external", "external resource unchanged " . $info['filetime'] . ' < ' . $local_ts->getTimestamp() . ' (' . $info['http_code'] . ')'); + curl_close ( $curl ); + // BUGlet: should track server-time instead of local-time + $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("external", "external resource changed, re importing" . $info['filetime'] ); + } + else { + dbg_error_log("external", "fetching external resource for the first time " . $row->external_url ); } - dbg_error_log("external", "external resource changed, re importing" . $info['filetime'] ); curl_setopt ( $curl, CURLOPT_NOBODY, false ); curl_setopt ( $curl, CURLOPT_HEADER, false ); $ics = curl_exec ( $curl ); curl_close ( $curl ); if ( is_string ( $ics ) && strlen ( $ics ) > 20 ) { - // BUGlet: should track server-time instead of local-time - $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'); + // BUGlet: should track server-time instead of local-time + $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 up to date or not found id(%s)", $bind_id ); - } + } + else { + dbg_error_log("external", "external resource up to date or not found id(%s)", $bind_id ); + } return false; }