DAViCal
external-fetch.php
1 <?php
13 function create_external ( $path,$is_calendar,$is_addressbook )
14 {
15  global $request;
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') );
19  return ;
20  }
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 )',
28  array(
29  ':user_no' => $request->user_no,
30  ':parent_container' => '/.external/',
31  ':dav_name' => $path,
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
37  ) ) ) {
38  $request->DoResponse( 500, translate('Error writing calendar details to database.') );
39  }
40 }
41 
42 function fetch_external ( $bind_id, $min_age, $ua_string )
43 {
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') );
47  return ;
48  }
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;
54  }
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'] . ')');
74  curl_close ( $curl );
75  // BUGlet: should track server-time instead of local-time
76  $qry = new AwlQuery( 'UPDATE collection SET modified=NOW() WHERE collection_id = :cid', array ( ':cid' => $row->collection_id ) );
77  $qry->Exec('DAVResource');
78  return true;
79  }
80  dbg_error_log("external", "external resource changed, re importing" . $info['filetime'] );
81  }
82  else {
83  dbg_error_log("external", "fetching external resource for the first time %s ", $row->external_url );
84  }
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 ) {
89  // BUGlet: should track server-time instead of local-time
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 ) ;
95  return true;
96  } else {
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));
99  } else {
100  dbg_error_log('ERROR', 'external:Not importing unusual ics data: "%s"', $ics);
101  }
102  }
103  curl_close ( $curl );
104  }
105  else {
106  dbg_error_log("external", "external resource up to date or not found id(%s)", $bind_id );
107  }
108  return false;
109 }
110 
111 function update_external ( $request )
112 {
113  global $c;
114  if ( $c->external_refresh < 1 )
115  return ;
116  if ( ! function_exists ( "curl_init" ) ) {
117  dbg_error_log("external", "external resource cannot be fetched without curl, please install curl");
118  return ;
119  }
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 );
127  }
128  }
129 }