/inc/external-fetch.php

https://gitlab.com/tiggerben/davical · PHP · 121 lines · 106 code · 3 blank · 12 comment · 28 complexity · c49c0be2adda6bdecfe2fbad2ac7bc54 MD5 · raw file

  1. <?php
  2. /**
  3. * Functions for managing external BIND resources
  4. *
  5. *
  6. * @package davical
  7. * @subpackage external-bind
  8. * @author Rob Ostensen <rob@boxacle.net>
  9. * @copyright Rob Ostensen
  10. * @license http://gnu.org/copyleft/gpl.html GNU GPL v3 or later
  11. */
  12. function create_external ( $path,$is_calendar,$is_addressbook )
  13. {
  14. global $request;
  15. if ( ! function_exists ( "curl_init" ) ) {
  16. dbg_error_log("external", "external resource cannot be fetched without curl, please install curl");
  17. $request->DoResponse( 503, translate('PHP5 curl support is required for external binds') );
  18. return ;
  19. }
  20. $resourcetypes = '<DAV::collection/>';
  21. if ($is_calendar) $resourcetypes .= '<urn:ietf:params:xml:ns:caldav:calendar/>';
  22. $qry = new AwlQuery();
  23. if ( ! $qry->QDo( 'INSERT INTO collection ( user_no, parent_container, dav_name, dav_etag, dav_displayname,
  24. is_calendar, is_addressbook, resourcetypes, created )
  25. VALUES( :user_no, :parent_container, :dav_name, :dav_etag, :dav_displayname,
  26. :is_calendar, :is_addressbook, :resourcetypes, current_timestamp )',
  27. array(
  28. ':user_no' => $request->user_no,
  29. ':parent_container' => '/.external/',
  30. ':dav_name' => $path,
  31. ':dav_etag' => md5($request->user_no. $path),
  32. ':dav_displayname' => $path,
  33. ':is_calendar' => ($is_calendar ? 't' : 'f'),
  34. ':is_addressbook' => ($is_addressbook ? 't' : 'f'),
  35. ':resourcetypes' => $resourcetypes
  36. ) ) ) {
  37. $request->DoResponse( 500, translate('Error writing calendar details to database.') );
  38. }
  39. }
  40. function fetch_external ( $bind_id, $min_age = '1 hour' )
  41. {
  42. if ( ! function_exists ( "curl_init" ) ) {
  43. dbg_error_log("external", "external resource cannot be fetched without curl, please install curl");
  44. $request->DoResponse( 503, translate('PHP5 curl support is required for external binds') );
  45. return ;
  46. }
  47. $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';
  48. $params = array( ':bind_id' => $bind_id );
  49. if ( strlen ( $min_age ) > 2 ) {
  50. $sql .= ' AND collection.modified + interval :interval < NOW()';
  51. $params[':interval'] = $min_age;
  52. }
  53. $sql .= ' ORDER BY modified DESC LIMIT 1';
  54. $qry = new AwlQuery( $sql, $params );
  55. if ( $qry->Exec('DAVResource') && $qry->rows() > 0 && $row = $qry->Fetch() ) {
  56. $curl = curl_init ( $row->external_url );
  57. curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, true );
  58. if ( $row->modified ) {
  59. $local_ts = new DateTime($row->modified);
  60. curl_setopt ( $curl, CURLOPT_HEADER, true );
  61. curl_setopt ( $curl, CURLOPT_FILETIME, true );
  62. curl_setopt ( $curl, CURLOPT_NOBODY, true );
  63. curl_setopt ( $curl, CURLOPT_TIMEVALUE, $local_ts->format("U") );
  64. curl_setopt ( $curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE );
  65. dbg_error_log("external", "checking external resource for remote changes " . $row->external_url );
  66. $ics = curl_exec ( $curl );
  67. $info = curl_getinfo ( $curl );
  68. if ( $info['http_code'] === 304 || (isset($info['filetime']) && $info['filetime'] != -1 && new DateTime("@" . $info['filetime']) <= $local_ts )) {
  69. dbg_error_log("external", "external resource unchanged " . $info['filetime'] . ' < ' . $local_ts->getTimestamp() . ' (' . $info['http_code'] . ')');
  70. curl_close ( $curl );
  71. // BUGlet: should track server-time instead of local-time
  72. $qry = new AwlQuery( 'UPDATE collection SET modified=NOW() WHERE collection_id = :cid', array ( ':cid' => $row->collection_id ) );
  73. $qry->Exec('DAVResource');
  74. return true;
  75. }
  76. dbg_error_log("external", "external resource changed, re importing" . $info['filetime'] );
  77. }
  78. else {
  79. dbg_error_log("external", "fetching external resource for the first time " . $row->external_url );
  80. }
  81. curl_setopt ( $curl, CURLOPT_NOBODY, false );
  82. curl_setopt ( $curl, CURLOPT_HEADER, false );
  83. $ics = curl_exec ( $curl );
  84. curl_close ( $curl );
  85. if ( is_string ( $ics ) && strlen ( $ics ) > 20 ) {
  86. // BUGlet: should track server-time instead of local-time
  87. $qry = new AwlQuery( 'UPDATE collection SET modified=NOW(), dav_etag=:etag WHERE collection_id = :cid',
  88. array ( ':cid' => $row->collection_id, ':etag' => md5($ics) ) );
  89. $qry->Exec('DAVResource');
  90. require_once ( 'caldav-PUT-functions.php');
  91. import_collection ( $ics , $row->user_no, $row->path, 'External Fetch' , false ) ;
  92. return true;
  93. }
  94. }
  95. else {
  96. dbg_error_log("external", "external resource up to date or not found id(%s)", $bind_id );
  97. }
  98. return false;
  99. }
  100. function update_external ( $request )
  101. {
  102. global $c;
  103. if ( $c->external_refresh < 1 )
  104. return ;
  105. if ( ! function_exists ( "curl_init" ) ) {
  106. dbg_error_log("external", "external resource cannot be fetched without curl, please install curl");
  107. return ;
  108. }
  109. $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()';
  110. $qry = new AwlQuery( $sql, array ( ':dav_name' => $request->dav_name(), ':interval' => $c->external_refresh . ' minutes' ) );
  111. dbg_error_log("external", "checking if external resource needs update");
  112. if ( $qry->Exec('DAVResource') && $qry->rows() > 0 && $row = $qry->Fetch() ) {
  113. if ( $row->bind_id != 0 ) {
  114. dbg_error_log("external", "external resource needs updating, this might take a minute : %s", $row->url );
  115. fetch_external ( $row->bind_id, $c->external_refresh . ' minutes' );
  116. }
  117. }
  118. }