mirror of
https://gitlab.com/davical-project/davical.git
synced 2026-08-29 18:26:05 +00:00
Implemented some further methods, including DoPUTRequest(), DoGETRequest()
and GetEventRange().
This commit is contained in:
parent
f838b4c553
commit
762803b273
@ -24,7 +24,6 @@ function __curl_init_callback( $data ) {
|
||||
global $__curl_read_callback_pos, $__curl_read_callback_data;
|
||||
$__curl_read_callback_pos = 0;
|
||||
$__curl_read_callback_data = $data;
|
||||
error_log( "CLIENT: Initialising callback data to: ". substr($__curl_read_callback_data,0,40)." ..." );
|
||||
}
|
||||
/**
|
||||
* As documented in the comments on this page(!)
|
||||
@ -33,7 +32,6 @@ function __curl_init_callback( $data ) {
|
||||
function __curl_read_callback( $ch, $fd, $length) {
|
||||
global $__curl_read_callback_pos, $__curl_read_callback_data;
|
||||
|
||||
error_log( "CLIENT: ANSWER: Pos=$__curl_read_callback_pos Len=$length" );
|
||||
if ( $__curl_read_callback_pos < 0 ) {
|
||||
unset($fd);
|
||||
return "";
|
||||
@ -43,7 +41,6 @@ function __curl_read_callback( $ch, $fd, $length) {
|
||||
if ( strlen($answer) < $length ) $__curl_read_callback_pos = -1;
|
||||
else $__curl_read_callback_pos += $length;
|
||||
|
||||
error_log( "CLIENT: ANSWER: POS=$__curl_read_callback_pos: ".$answer );
|
||||
return $answer;
|
||||
}
|
||||
|
||||
@ -130,6 +127,15 @@ class CalDAVClient {
|
||||
$this->user_agent = $user_agent;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a Content-type: header.
|
||||
*
|
||||
* @param int $type The content type
|
||||
*/
|
||||
function SetContentType( $type ) {
|
||||
$this->headers[] = "Content-type: $type";
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Send a request to the server
|
||||
@ -163,9 +169,9 @@ class CalDAVClient {
|
||||
curl_setopt($this->curl, CURLOPT_READFUNCTION, '__curl_read_callback' );
|
||||
}
|
||||
|
||||
$content = curl_exec($this->curl);
|
||||
$this->response = curl_exec($this->curl);
|
||||
|
||||
return $content;
|
||||
return $this->response;
|
||||
}
|
||||
|
||||
|
||||
@ -174,13 +180,16 @@ class CalDAVClient {
|
||||
*
|
||||
* @param string $relative_url The URL to make the request to, relative to $base_url
|
||||
*
|
||||
* @return string The OPTIONS response
|
||||
* @return array The allowed options
|
||||
*/
|
||||
function DoOptionsRequest( $relative_url = "" ) {
|
||||
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, "OPTIONS" );
|
||||
$this->body = "";
|
||||
curl_setopt($this->curl, CURLOPT_HEADER, true);
|
||||
return $this->DoRequest($relative_url);
|
||||
$headers = $this->DoRequest($relative_url);
|
||||
$options_header = preg_replace( '/^.*Allow: ([a-z, ]+)\r?\n.*/is', '$1', $headers );
|
||||
$options = array_flip( preg_split( '/[, ]+/', $options_header ));
|
||||
return $options;
|
||||
}
|
||||
|
||||
|
||||
@ -188,31 +197,142 @@ class CalDAVClient {
|
||||
/**
|
||||
* Send an XML request to the server (e.g. PROPFIND, REPORT, MKCALENDAR)
|
||||
*
|
||||
* @param string $relative_url The URL to make the request to, relative to $base_url
|
||||
* @param string $xml The XML to send along with the request
|
||||
* @param string $method The method (PROPFIND, REPORT, etc) to use with the request
|
||||
* @param string $xml The XML to send along with the request
|
||||
* @param string $relative_url The URL to make the request to, relative to $base_url
|
||||
*
|
||||
* @return string The content of the response from the server
|
||||
* @return array An array of the allowed methods
|
||||
*/
|
||||
function DoXMLRequest( $request_method, $xml, $relative_url = '' ) {
|
||||
$this->body = $xml;
|
||||
|
||||
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, $request_method );
|
||||
curl_setopt($this->curl, CURLOPT_HEADER, true);
|
||||
$this->headers[] = "Content-type: text/xml";
|
||||
curl_setopt($this->curl, CURLOPT_HEADER, false);
|
||||
$this->SetContentType("text/xml");
|
||||
return $this->DoRequest($relative_url);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Get a single item from the server.
|
||||
*
|
||||
* @param string $relative_url The part of the URL after the calendar
|
||||
*/
|
||||
function DoGETRequest( $relative_url ) {
|
||||
$this->body = "";
|
||||
curl_setopt($this->curl, CURLOPT_HTTPGET, true);
|
||||
curl_setopt($this->curl, CURLOPT_HEADER, false);
|
||||
$response = $this->DoRequest( $relative_url );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* PUT a text/icalendar resource, returning the etag
|
||||
*
|
||||
* @param string $relative_url The URL to make the request to, relative to $base_url
|
||||
* @param string $icalendar The iCalendar resource to send to the server
|
||||
* @param string $etag The etag of an existing resource to be overwritten, or '*' for a new resource.
|
||||
*
|
||||
* @return string The content of the response from the server
|
||||
*/
|
||||
function DoPUTRequest( $relative_url, $icalendar, $etag = null ) {
|
||||
$this->body = $icalendar;
|
||||
|
||||
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, "PUT" );
|
||||
curl_setopt($this->curl, CURLOPT_HEADER, true);
|
||||
if ( $etag != null ) {
|
||||
$this->SetMatch( ($etag == '*'), $etag );
|
||||
}
|
||||
$this->SetContentType("text/icalendar");
|
||||
$headers = $this->DoRequest($relative_url);
|
||||
|
||||
/**
|
||||
* RSCDS will always return the real etag on PUT. Other CalDAV servers may need
|
||||
* more work, but we are assuming we are running against RSCDS in this case.
|
||||
*/
|
||||
$etag = preg_replace( '/^.*Etag: "?([^"\r\n]+)"?\r?\n.*/is', '$1', $headers );
|
||||
return $etag;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the events in a range from $start to $finish. The dates should be in the
|
||||
* format yyyymmddThhmmssZ and should be in GMT. The events are returned as an
|
||||
* array of event arrays. Each event array will have a 'href', 'etag' and 'event'
|
||||
* part, where the 'href' is relative to the calendar and the event contains the
|
||||
* definition of the event in iCalendar format.
|
||||
*
|
||||
* @param timestamp $start The start time for the period
|
||||
* @param timestamp $finish The finish time for the period
|
||||
*
|
||||
* @return array An array of the relative URLs, etags, and events from the server
|
||||
*/
|
||||
function GetEventRange( $start, $finish ) {
|
||||
$xml = <<<EOXML
|
||||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<calendar-query xmlns:D="DAV:" xmlns="urn:ietf:params:xml:ns:caldav">
|
||||
<D:prop>
|
||||
<calendar-data/>
|
||||
<D:getetag/>
|
||||
</D:prop>
|
||||
<filter>
|
||||
<comp-filter name="VCALENDAR">
|
||||
<comp-filter name="VEVENT">
|
||||
<time-range start="$start" end="$finish"/>
|
||||
</comp-filter>
|
||||
</comp-filter>
|
||||
</filter>
|
||||
</calendar-query>
|
||||
EOXML;
|
||||
$this->SetDepth("1");
|
||||
$this->DoXMLRequest( 'REPORT', $xml );
|
||||
$xml_parser = xml_parser_create_ns('UTF-8');
|
||||
$this->xml_tags = array();
|
||||
xml_parser_set_option ( $xml_parser, XML_OPTION_SKIP_WHITE, 1 );
|
||||
xml_parse_into_struct( $xml_parser, $this->response, $this->xml_tags );
|
||||
xml_parser_free($xml_parser);
|
||||
|
||||
$events = array();
|
||||
foreach( $this->xml_tags AS $k => $v ) {
|
||||
switch( $v['tag'] ) {
|
||||
case 'DAV::RESPONSE':
|
||||
if ( $v['type'] == 'open' ) {
|
||||
$response = array();
|
||||
}
|
||||
elseif ( $v['type'] == 'close' ) {
|
||||
$events[] = $response;
|
||||
}
|
||||
break;
|
||||
case 'DAV::HREF':
|
||||
$response['href'] = preg_replace( "#^.*/$this->user/$this->calendar/#", "", $v['value'] );
|
||||
break;
|
||||
case 'DAV::GETETAG':
|
||||
$response['etag'] = preg_replace('/^"?([^"]+)"?/', '$1', $v['value']);
|
||||
break;
|
||||
case 'URN:IETF:PARAMS:XML:NS:CALDAV:CALENDAR-DATA':
|
||||
$response['event'] = $v['value'];
|
||||
break;
|
||||
}
|
||||
}
|
||||
return $events;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Usage example
|
||||
|
||||
$cal = new CalDAVClient( "http://calendar.example.com/caldav.php/username/calendar/", "username", "password", "calendar" );
|
||||
$options_headers = $cal->DoOptionsRequest();
|
||||
$cal->SetDepth(1);
|
||||
$folder_xml = $cal->DoXMLRequest("PROPFIND", '<?xml version="1.0" encoding="utf-8" ?><propfind xmlns="DAV:"><prop><getcontentlength/><getcontenttype/><resourcetype/><getetag/></prop></propfind>' );
|
||||
|
||||
$options = $cal->DoOptionsRequest();
|
||||
if ( isset($options["PROPFIND"] ) {
|
||||
// Fetch some information about the events in that calendar
|
||||
$cal->SetDepth(1);
|
||||
$folder_xml = $cal->DoXMLRequest("PROPFIND", '<?xml version="1.0" encoding="utf-8" ?><propfind xmlns="DAV:"><prop><getcontentlength/><getcontenttype/><resourcetype/><getetag/></prop></propfind>' );
|
||||
}
|
||||
// Fetch all events for February
|
||||
$events = $cal->GetEventRange("20070101T000000Z","20070201T000000Z");
|
||||
*/
|
||||
|
||||
?>
|
||||
Loading…
x
Reference in New Issue
Block a user