13 function create_external ( $path,$is_calendar,$is_addressbook )
16 if ( ! function_exists (
"curl_init" ) ) {
17 dbg_error_log(
"external",
"external resource cannot be fetched without curl, please install curl");
18 $request->DoResponse( 503, translate(
'PHP curl support is required for external binds') );
21 $resourcetypes =
'<DAV::collection/>';
22 if ($is_calendar) $resourcetypes .=
'<urn:ietf:params:xml:ns:caldav:calendar/>';
23 $qry =
new AwlQuery();
24 if ( ! $qry->QDo(
'INSERT INTO collection ( user_no, parent_container, dav_name, dav_etag, dav_displayname, 25 is_calendar, is_addressbook, resourcetypes, created ) 26 VALUES( :user_no, :parent_container, :dav_name, :dav_etag, :dav_displayname, 27 :is_calendar, :is_addressbook, :resourcetypes, current_timestamp )',
29 ':user_no' => $request->user_no,
30 ':parent_container' =>
'/.external/',
32 ':dav_etag' => md5($request->user_no. $path),
33 ':dav_displayname' => $path,
34 ':is_calendar' => ($is_calendar ?
't' :
'f'),
35 ':is_addressbook' => ($is_addressbook ?
't' :
'f'),
36 ':resourcetypes' => $resourcetypes
38 $request->DoResponse( 500, translate(
'Error writing calendar details to database.') );
42 function fetch_external ( $bind_id, $min_age, $ua_string )
44 if ( ! function_exists (
"curl_init" ) ) {
45 dbg_error_log(
"external",
"external resource cannot be fetched without curl, please install curl");
46 $request->DoResponse( 503, translate(
'PHP curl support is required for external binds') );
49 $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';
50 $params = array(
':bind_id' => $bind_id );
51 if ( strlen ( $min_age ) > 2 ) {
52 $sql .=
' AND ( collection.modified + interval :interval < NOW() OR collection.modified is NULL )';
53 $params[
':interval'] = $min_age;
55 $sql .=
' ORDER BY modified DESC LIMIT 1';
56 $qry =
new AwlQuery( $sql, $params );
57 if ( $qry->Exec(
'DAVResource') && $qry->rows() > 0 && $row = $qry->Fetch() ) {
58 $curl = curl_init ( $row->external_url );
59 curl_setopt ( $curl, CURLOPT_RETURNTRANSFER,
true );
60 if ( $row->modified ) {
61 $local_ts =
new DateTime($row->modified);
62 curl_setopt ( $curl, CURLOPT_HEADER,
true );
63 curl_setopt ( $curl, CURLOPT_FILETIME,
true );
64 curl_setopt ( $curl, CURLOPT_NOBODY,
true );
65 curl_setopt ( $curl, CURLOPT_TIMEVALUE, $local_ts->format(
"U") );
66 curl_setopt ( $curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE );
67 if ( isset( $ua_string ) )
68 curl_setopt($curl, CURLOPT_USERAGENT, $ua_string);
69 dbg_error_log(
"external",
"checking external resource for remote changes %s ", $row->external_url );
70 $ics = curl_exec ( $curl );
71 $info = curl_getinfo ( $curl );
72 if ( $info[
'http_code'] === 304 || (isset($info[
'filetime']) && $info[
'filetime'] != -1 &&
new DateTime(
"@" . $info[
'filetime']) <= $local_ts )) {
73 dbg_error_log(
"external",
"external resource unchanged " . $info[
'filetime'] .
' < ' . $local_ts->getTimestamp() .
' (' . $info[
'http_code'] .
')');
76 $qry =
new AwlQuery(
'UPDATE collection SET modified=NOW() WHERE collection_id = :cid', array (
':cid' => $row->collection_id ) );
77 $qry->Exec(
'DAVResource');
80 dbg_error_log(
"external",
"external resource changed, re importing" . $info[
'filetime'] );
83 dbg_error_log(
"external",
"fetching external resource for the first time %s ", $row->external_url );
85 curl_setopt ( $curl, CURLOPT_NOBODY,
false );
86 curl_setopt ( $curl, CURLOPT_HEADER,
false );
87 $ics = curl_exec ( $curl );
88 if ( is_string ( $ics ) && strlen ( $ics ) > 20 ) {
90 $qry =
new AwlQuery(
'UPDATE collection SET modified=NOW(), dav_etag=:etag WHERE collection_id = :cid',
91 array (
':cid' => $row->collection_id,
':etag' => md5($ics) ) );
92 $qry->Exec(
'DAVResource');
93 require_once (
'caldav-PUT-functions.php');
94 import_collection ( $ics , $row->user_no, $row->path,
'External Fetch' ,
false ) ;
97 if ( curl_errno($curl) ) {
98 dbg_error_log(
'ERROR',
'external:Fetch of %s (%s) failed with curl error %s: %s', $row->collection_id, $row->external_url, curl_errno($curl), curl_error($curl));
100 dbg_error_log(
'ERROR',
'external:Not importing unusual ics data: "%s"', $ics);
103 curl_close ( $curl );
106 dbg_error_log(
"external",
"external resource up to date or not found id(%s)", $bind_id );
111 function update_external ( $request )
114 if ( $c->external_refresh < 1 )
116 if ( ! function_exists (
"curl_init" ) ) {
117 dbg_error_log(
"external",
"external resource cannot be fetched without curl, please install curl");
120 $sql =
'SELECT bind_id, external_url as url 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() OR collection.modified is NULL )';
121 $qry =
new AwlQuery( $sql, array (
':dav_name' => $request->dav_name(),
':interval' => $c->external_refresh .
' minutes' ) );
122 dbg_error_log(
"external",
"checking if external resource needs update");
123 if ( $qry->Exec(
'DAVResource') && $qry->rows() > 0 && $row = $qry->Fetch() ) {
124 if ( $row->bind_id != 0 ) {
125 dbg_error_log(
"external",
"external resource needs updating, this might take a minute : %s", $row->url );
126 fetch_external ( $row->bind_id, $c->external_refresh .
' minutes', @$c->external_ua_string );