Loading...
Searching...
No Matches
caldav-client.php
<?php
class CalDAVClient {
var $base_url, $user, $pass, $calendar, $entry, $protocol, $server, $port;
var $user_agent = 'DAViCalClient';
var $headers = array();
var $body = "";
var $requestMethod = "GET";
var $httpRequest = ""; // for debugging http headers sent
var $xmlRequest = ""; // for debugging xml sent
var $httpResponse = ""; // for debugging http headers received
var $xmlResponse = ""; // for debugging xml received
function __construct( $base_url, $user, $pass, $calendar = '' ) {
$this->user = $user;
$this->pass = $pass;
$this->calendar = $calendar;
$this->headers = array();
if ( preg_match( '#^(https?)://([a-z0-9.-]+)(:([0-9]+))?(/.*)$#', $base_url, $matches ) ) {
$this->server = $matches[2];
$this->base_url = $matches[5];
if ( $matches[1] == 'https' ) {
$this->protocol = 'ssl';
$this->port = 443;
}
else {
$this->protocol = 'tcp';
$this->port = 80;
}
if ( $matches[4] != '' ) {
$this->port = intval($matches[4]);
}
}
else {
trigger_error("Invalid URL: '".$base_url."'", E_USER_ERROR);
}
}
function SetMatch( $match, $etag = '*' ) {
$this->headers[] = sprintf( "%s-Match: %s", ($match ? "If" : "If-None"), $etag);
}
/*
* Add a Depth: header. Valid values are 0, 1 or infinity
*
* @param int $depth The depth, default to infinity
*/
function SetDepth( $depth = '0' ) {
$this->headers[] = 'Depth: '. ($depth == '1' ? "1" : ($depth == 'infinity' ? $depth : "0") );
}
function SetUserAgent( $user_agent = null ) {
if ( !isset($user_agent) ) $user_agent = $this->user_agent;
$this->user_agent = $user_agent;
}
function SetContentType( $type ) {
$this->headers[] = "Content-type: $type";
}
function ParseResponse( $response ) {
$pos = strpos($response, '<?xml');
if ($pos === false) {
$this->httpResponse = trim($response);
}
else {
$this->httpResponse = trim(substr($response, 0, $pos));
$this->xmlResponse = trim(substr($response, $pos));
}
}
function GetHttpRequest() {
return $this->httpRequest;
}
function GetHttpResponse() {
return $this->httpResponse;
}
function GetXmlRequest() {
return $this->xmlRequest;
}
function GetXmlResponse() {
return $this->xmlResponse;
}
function DoRequest( $relative_url = "" ) {
if(!defined("_FSOCK_TIMEOUT")){ define("_FSOCK_TIMEOUT", 10); }
$headers = array();
$headers[] = $this->requestMethod." ". $this->base_url . $relative_url . " HTTP/1.1";
$headers[] = "Authorization: Basic ".base64_encode($this->user .":". $this->pass );
$headers[] = "Host: ".$this->server .":".$this->port;
foreach( $this->headers as $ii => $head ) {
$headers[] = $head;
}
$headers[] = "Content-Length: " . strlen($this->body);
$headers[] = "User-Agent: " . $this->user_agent;
$headers[] = 'Connection: close';
$this->httpRequest = join("\r\n",$headers);
$this->xmlRequest = $this->body;
$fip = fsockopen( $this->protocol . '://' . $this->server, $this->port, $errno, $errstr, _FSOCK_TIMEOUT); //error handling?
if ( !(get_resource_type($fip) == 'stream') ) return false;
if ( !fwrite($fip, $this->httpRequest."\r\n\r\n".$this->body) ) { fclose($fip); return false; }
$rsp = "";
while( !feof($fip) ) { $rsp .= fgets($fip,8192); }
fclose($fip);
$this->headers = array(); // reset the headers array for our next request
$this->ParseResponse($rsp);
return $rsp;
}
function DoOptionsRequest( $relative_url = "" ) {
$this->requestMethod = "OPTIONS";
$this->body = "";
$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;
}
function DoXMLRequest( $request_method, $xml, $relative_url = '' ) {
$this->body = $xml;
$this->requestMethod = $request_method;
$this->SetContentType("text/xml");
return $this->DoRequest($relative_url);
}
function DoGETRequest( $relative_url ) {
$this->body = "";
$this->requestMethod = "GET";
return $this->DoRequest( $relative_url );
}
function DoPUTRequest( $relative_url, $icalendar, $etag = null ) {
$this->body = $icalendar;
$this->requestMethod = "PUT";
if ( $etag != null ) {
$this->SetMatch( ($etag != '*'), $etag );
}
$this->SetContentType("text/icalendar");
$headers = $this->DoRequest($relative_url);
$etag = preg_replace( '/^.*Etag: "?([^"\r\n]+)"?\r?\n.*/is', '$1', $headers );
return $etag;
}
function DoDELETERequest( $relative_url, $etag = null ) {
$this->body = "";
$this->requestMethod = "DELETE";
if ( $etag != null ) {
$this->SetMatch( true, $etag );
}
$this->DoRequest($relative_url);
return $this->resultcode;
}
function DoCalendarQuery( $filter, $relative_url = '' ) {
$xml = <<<EOXML
<?xml version="1.0" encoding="utf-8" ?>
<C:calendar-query xmlns:D="DAV:" xmlns:C="urn:ietf:params:xml:ns:caldav">
<D:prop>
<C:calendar-data/>
<D:getetag/>
</D:prop>$filter
</C:calendar-query>
EOXML;
$this->DoXMLRequest( 'REPORT', $xml, $relative_url );
$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->xmlResponse, $this->xml_tags );
xml_parser_free($xml_parser);
$report = array();
foreach( $this->xml_tags as $k => $v ) {
switch( $v['tag'] ) {
case 'DAV::RESPONSE':
if ( $v['type'] == 'open' ) {
$response = array();
}
elseif ( $v['type'] == 'close' ) {
$report[] = $response;
}
break;
case 'DAV::HREF':
$response['href'] = basename( $v['value'] );
break;
case 'DAV::GETETAG':
$response['etag'] = preg_replace('/^"?([^"]+)"?/', '$1', $v['value']);
break;
case 'URN:IETF:PARAMS:XML:NS:CALDAV:CALENDAR-DATA':
$response['data'] = $v['value'];
break;
}
}
return $report;
}
function GetEvents( $start = null, $finish = null, $relative_url = '' ) {
$filter = "";
if ( isset($start) && isset($finish) )
$range = "<C:time-range start=\"$start\" end=\"$finish\"/>";
else
$range = '';
$filter = <<<EOFILTER
<C:filter>
<C:comp-filter name="VCALENDAR">
<C:comp-filter name="VEVENT">
$range
</C:comp-filter>
</C:comp-filter>
</C:filter>
EOFILTER;
return $this->DoCalendarQuery($filter, $relative_url);
}
function GetTodos( $start, $finish, $completed = false, $cancelled = false, $relative_url = "" ) {
if ( $start && $finish ) {
$time_range = <<<EOTIME
<C:time-range start="$start" end="$finish"/>
EOTIME;
}
// Warning! May contain traces of double negatives...
$neg_cancelled = ( $cancelled === true ? "no" : "yes" );
$neg_completed = ( $cancelled === true ? "no" : "yes" );
$filter = <<<EOFILTER
<C:filter>
<C:comp-filter name="VCALENDAR">
<C:comp-filter name="VTODO">
<C:prop-filter name="STATUS">
<C:text-match negate-condition="$neg_completed">COMPLETED</C:text-match>
</C:prop-filter>
<C:prop-filter name="STATUS">
<C:text-match negate-condition="$neg_cancelled">CANCELLED</C:text-match>
</C:prop-filter>$time_range
</C:comp-filter>
</C:comp-filter>
</C:filter>
EOFILTER;
return $this->DoCalendarQuery($filter, $relative_url);
}
function GetEntryByUid( $uid, $relative_url = '' ) {
$filter = "";
if ( $uid ) {
$filter = <<<EOFILTER
<C:filter>
<C:comp-filter name="VCALENDAR">
<C:comp-filter name="VEVENT">
<C:prop-filter name="UID">
<C:text-match icollation="i;octet">$uid</C:text-match>
</C:prop-filter>
</C:comp-filter>
</C:comp-filter>
</C:filter>
EOFILTER;
}
return $this->DoCalendarQuery($filter, $relative_url);
}
function GetEntryByHref( $href, $relative_url = '' ) {
return $this->DoGETRequest( $relative_url . $href );
}
}
CalDAVClient
Definition caldav-client-v2.php:44
CalDAVClient\__construct
__construct( $base_url, $user, $pass)
Definition caldav-client-v2.php:101