50 protected $base_url, $user, $pass, $entry, $protocol, $server, $port;
104 $this->headers = array();
106 if ( preg_match(
'#^(https?)://([a-z0-9.-]+)(:([0-9]+))?(/.*)$#', $base_url, $matches ) ) {
107 $this->server = $matches[2];
108 $this->base_url = $matches[5];
109 if ( $matches[1] ==
'https' ) {
110 $this->protocol =
'ssl';
114 $this->protocol =
'tcp';
117 if ( $matches[4] !=
'' ) {
118 $this->port = intval($matches[4]);
122 trigger_error(
"Invalid URL: '".$base_url.
"'", E_USER_ERROR);
196 $pos = strpos($response,
'<?xml');
197 if ($pos !==
false) {
198 $this->xmlResponse = trim(substr($response, $pos));
199 $this->xmlResponse = preg_replace(
'{>[^>]*$}s',
'>',$this->xmlResponse );
200 $parser = xml_parser_create_ns(
'UTF-8');
201 xml_parser_set_option ( $parser, XML_OPTION_SKIP_WHITE, 1 );
202 xml_parser_set_option ( $parser, XML_OPTION_CASE_FOLDING, 0 );
204 if ( xml_parse_into_struct( $parser, $this->xmlResponse, $this->xmlnodes, $this->xmltags ) === 0 ) {
205 printf(
"XML parsing error: %s - %s\n", xml_get_error_code($parser), xml_error_string(xml_get_error_code($parser)) );
209 printf(
"\nXML Reponse:\n%s\n", $this->xmlResponse );
212 xml_parser_free($parser);
222 if ( empty($this->httpResponseHeaders) )
return array();
223 if ( !isset($this->httpParsedHeaders) ) {
224 $this->httpParsedHeaders = array();
225 $headers = str_replace(
"\r\n",
"\n", $this->httpResponseHeaders);
226 $ar_headers = explode(
"\n", $headers);
228 foreach ($ar_headers as $cur_headers) {
229 if( preg_match(
'{^\s*\S}', $cur_headers) ) $header_name = $last_header;
230 else if ( preg_match(
'{^(\S*):', $cur_headers, $matches) ) {
231 $header_name = $matches[1];
232 $last_header = $header_name;
233 if ( empty($this->httpParsedHeaders[$header_name]) ) $this->httpParsedHeaders[$header_name] = array();
235 $this->httpParsedHeaders[$header_name][] = $cur_headers;
238 return $this->httpParsedHeaders;
292 if ( !isset($url) ) $url = $this->base_url;
293 $this->request_url = $url;
294 $url = preg_replace(
'{^https?://[^/]+}',
'', $url);
296 if ( preg_match(
'{[^%?&=+,.-_/a-z0-9]}', $url ) ) {
297 $url = str_replace(rawurlencode(
'/'),
'/',rawurlencode($url));
298 $url = str_replace(rawurlencode(
'?'),
'?',$url);
299 $url = str_replace(rawurlencode(
'&'),
'&',$url);
300 $url = str_replace(rawurlencode(
'='),
'=',$url);
301 $url = str_replace(rawurlencode(
'+'),
'+',$url);
302 $url = str_replace(rawurlencode(
','),
',',$url);
304 $headers[] = $this->requestMethod.
" ". $url .
" HTTP/1.1";
305 $headers[] =
"Authorization: Basic ".base64_encode($this->user .
":". $this->pass );
306 $headers[] =
"Host: ".$this->server .
":".$this->port;
308 if ( !isset($this->headers[
'content-type']) ) $this->headers[
'content-type'] =
"Content-type: text/plain";
309 foreach( $this->headers as $ii => $head ) {
312 $headers[] =
"Content-Length: " . strlen($this->body);
313 $headers[] =
"User-Agent: " . $this->user_agent;
314 $headers[] =
'Connection: close';
315 $this->httpRequest = join(
"\r\n",$headers);
316 $this->xmlRequest = $this->body;
318 $this->xmlResponse =
'';
320 $fip = fsockopen( $this->protocol .
'://' . $this->server, $this->port, $errno, $errstr, _FSOCK_TIMEOUT);
321 if ( !(get_resource_type($fip) ==
'stream') )
return false;
322 if ( !fwrite($fip, $this->httpRequest.
"\r\n\r\n".$this->body) ) { fclose($fip);
return false; }
324 while( !feof($fip) ) { $response .= fgets($fip,8192); }
327 list( $this->httpResponseHeaders, $this->httpResponseBody ) = preg_split(
'{\r?\n\r?\n}s', $response, 2 );
328 if ( preg_match(
'{Transfer-Encoding: chunked}i', $this->httpResponseHeaders ) ) $this->
Unchunk();
329 if ( preg_match(
'/HTTP\/\d\.\d (\d{3})/', $this->httpResponseHeaders, $status) )
330 $this->httpResponseCode = intval($status[1]);
332 $this->httpResponseCode = 0;
334 $this->headers = array();
345 $chunks = $this->httpResponseBody;
349 if ( preg_match(
'{^((\r\n)?\s*([ 0-9a-fA-F]+)(;[^\n]*)?\r?\n)}', $chunks, $matches ) ) {
350 $octets = $matches[3];
351 $bytes = hexdec($octets);
352 $pos = strlen($matches[1]);
356 $content .= substr($chunks,$pos,$bytes);
357 $chunks = substr($chunks,$pos + $bytes + 2);
366 $this->httpResponseBody = $content;
445 $this->body = $icalendar;
447 $this->requestMethod =
"PUT";
448 if ( $etag !=
null ) {
449 $this->
SetMatch( ($etag !=
'*'), $etag );
455 if ( preg_match(
'{^ETag:\s+"([^"]*)"\s*$}im', $this->httpResponseHeaders, $matches ) ) $etag = $matches[1];
456 if ( !isset($etag) || $etag ==
'' ) {
457 if ( $this->debug ) printf(
"No etag in:\n%s\n", $this->httpResponseHeaders );
458 $save_request = $this->httpRequest;
459 $save_response_headers = $this->httpResponseHeaders;
461 if ( preg_match(
'{^Etag:\s+"([^"]*)"\s*$}im', $this->httpResponseHeaders, $matches ) ) $etag = $matches[1];
462 if ( !isset($etag) || $etag ==
'' ) {
463 if ( $this->debug ) printf(
"Still No etag in:\n%s\n", $this->httpResponseHeaders );
465 $this->httpRequest = $save_request;
466 $this->httpResponseHeaders = $save_response_headers;
578 if ( isset($this->xmltags[$tagname]) && isset($this->xmltags[$tagname][$i]) ) {
579 $j = $this->xmltags[$tagname][$i];
580 while( $j-- > 0 && $this->xmlnodes[$j][
'tag'] !=
'DAV::href' ) {
582 if ( $this->xmlnodes[$j][
'tag'] ==
'DAV::status' && $this->xmlnodes[$j][
'value'] !=
'HTTP/1.1 200 OK' )
return null;
585 if ( $j > 0 && isset($this->xmlnodes[$j][
'value']) ) {
587 return rawurldecode($this->xmlnodes[$j][
'value']);
591 if ( $this->debug ) printf(
"xmltags[$tagname] or xmltags[$tagname][$i] is not set\n");
604 if ( isset($this->xmltags[$tagname]) && isset($this->xmltags[$tagname][$i]) ) {
605 $j = $this->xmltags[$tagname][$i];
606 while( $j-- > 0 && $this->xmlnodes[$j][
'tag'] !=
'DAV::resourcetype' );
608 while( $j-- > 0 && $this->xmlnodes[$j][
'tag'] !=
'DAV::href' );
609 if ( $j > 0 && isset($this->xmlnodes[$j][
'value']) ) {
610 return rawurldecode($this->xmlnodes[$j][
'value']);
625 $level = $this->xmlnodes[$nodenum][
'level'];
627 while ( $this->xmlnodes[++$nodenum][
'level'] >= $level ) {
628 if ( $this->xmlnodes[$nodenum][
'tag'] ==
'DAV::propstat' ) {
629 if ( $this->xmlnodes[$nodenum][
'type'] ==
'open' ) {
634 if ( $status ==
'HTTP/1.1 200 OK' )
break;
637 elseif ( !isset($this->xmlnodes[$nodenum]) || !is_array($this->xmlnodes[$nodenum]) ) {
640 elseif ( $this->xmlnodes[$nodenum][
'tag'] ==
'DAV::status' ) {
641 $status = $this->xmlnodes[$nodenum][
'value'];
644 $props[] = $this->xmlnodes[$nodenum];
680 if ( !isset($this->principal_url) ) {
684 $this->
DoPROPFINDRequest( $this->principal_url, array(
'urn:ietf:params:xml:ns:caldav:calendar-home-set'), 0);
687 $calendar_home = array();
688 foreach( $this->xmltags[
'urn:ietf:params:xml:ns:caldav:calendar-home-set'] AS $k => $v ) {
689 if ( $this->xmlnodes[$v][
'type'] !=
'open' )
continue;
690 while( $this->xmlnodes[++$v][
'type'] !=
'close' && $this->xmlnodes[$v][
'tag'] !=
'urn:ietf:params:xml:ns:caldav:calendar-home-set' ) {
692 if ( $this->xmlnodes[$v][
'tag'] ==
'DAV::href' && isset($this->xmlnodes[$v][
'value']) )
693 $calendar_home[] = rawurldecode($this->xmlnodes[$v][
'value']);
697 if ( !$recursed && count($calendar_home) < 1 ) {
709 if ( !isset($this->calendar_home_set[0]) ) {
712 $this->
DoPROPFINDRequest( $this->calendar_home_set[0], array(
'resourcetype',
'displayname',
'http://calendarserver.org/ns/:getctag'), 1);
714 $calendars = array();
715 if ( isset($this->xmltags[
'urn:ietf:params:xml:ns:caldav:calendar']) ) {
717 foreach( $this->xmltags[
'urn:ietf:params:xml:ns:caldav:calendar'] AS $k => $v ) {
721 foreach( $this->xmltags[
'DAV::href'] AS $i => $hnode ) {
722 $href = rawurldecode($this->xmlnodes[$hnode][
'value']);
730 foreach( $ok_props AS $v ) {
732 switch( $v[
'tag'] ) {
733 case 'http://calendarserver.org/ns/:getctag':
734 $calendar->getctag = $v[
'value'];
736 case 'DAV::displayname':
737 $calendar->displayname = $v[
'value'];
741 $calendars[] = $calendar;
781 if ( isset($url) ) $this->SetCalendar($url);
783 $this->DoPROPFINDRequest( $this->calendar_url, array(
'getetag'), 1);
786 if ( isset($this->xmltags[
'DAV::getetag']) ) {
787 foreach( $this->xmltags[
'DAV::getetag'] AS $k => $v ) {
788 $href = $this->HrefForProp(
'DAV::getetag', $k);
789 if ( isset($href) && isset($this->xmlnodes[$v][
'value']) ) $etags[$href] = $this->xmlnodes[$v][
'value'];
802 if ( isset($url) ) $this->SetCalendar($url);
805 foreach( $event_hrefs AS $k => $href ) {
806 $href = str_replace( rawurlencode(
'/'),
'/',rawurlencode($href));
807 $hrefs .=
'<href>'.$href.
'</href>';
809 $this->body = <<<EOXML
810<?xml version=
"1.0" encoding=
"utf-8" ?>
811<C:calendar-multiget xmlns=
"DAV:" xmlns:C=
"urn:ietf:params:xml:ns:caldav">
812<prop><getetag/><C:calendar-data/></prop>
814</C:calendar-multiget>
817 $this->requestMethod =
"REPORT";
818 $this->SetContentType(
"text/xml");
819 $this->DoRequest( $this->calendar_url );
822 if ( isset($this->xmltags[
'urn:ietf:params:xml:ns:caldav:calendar-data']) ) {
823 foreach( $this->xmltags[
'urn:ietf:params:xml:ns:caldav:calendar-data'] AS $k => $v ) {
824 $href = $this->HrefForProp(
'urn:ietf:params:xml:ns:caldav:calendar-data', $k);
826 $events[$href] = $this->xmlnodes[$v][
'value'];
830 foreach( $event_hrefs AS $k => $href ) {
831 $this->DoGETRequest($href);
832 $events[$href] = $this->httpResponseBody;
855 if ( !empty($url) ) $this->SetCalendar($url);
857 $this->body = <<<EOXML
858<?xml version=
"1.0" encoding=
"utf-8" ?>
859<C:calendar-query xmlns:D=
"DAV:" xmlns:C=
"urn:ietf:params:xml:ns:caldav">
867 $this->requestMethod =
"REPORT";
868 $this->SetContentType(
"text/xml");
869 $this->DoRequest( $this->calendar_url );
872 foreach( $this->xmlnodes as $k => $v ) {
873 switch( $v[
'tag'] ) {
874 case 'DAV::response':
875 if ( $v[
'type'] ==
'open' ) {
878 elseif ( $v[
'type'] ==
'close' ) {
879 $report[] = $response;
883 $response[
'href'] = basename( rawurldecode($v[
'value']) );
886 $response[
'etag'] = preg_replace(
'/^"?([^"]+)"?/',
'$1', $v[
'value']);
888 case 'urn:ietf:params:xml:ns:caldav:calendar-data':
889 $response[
'data'] = $v[
'value'];
946 function GetTodos( $start, $finish, $completed =
false, $cancelled =
false, $relative_url =
"" ) {
948 if ( $start && $finish ) {
949$time_range = <<<EOTIME
950 <C:time-range start=
"$start" end=
"$finish"/>
955 $neg_cancelled = ( $cancelled ===
true ?
"no" :
"yes" );
956 $neg_completed = ( $cancelled ===
true ?
"no" :
"yes" );
958 $filter = <<<EOFILTER
960 <C:comp-filter name=
"VCALENDAR">
961 <C:comp-filter name=
"VTODO">
962 <C:prop-filter name=
"STATUS">
963 <C:text-match negate-condition=
"$neg_completed">COMPLETED</C:text-match>
965 <C:prop-filter name=
"STATUS">
966 <C:text-match negate-condition=
"$neg_cancelled">CANCELLED</C:text-match>
967 </C:prop-filter>$time_range
973 return $this->DoCalendarQuery($filter, $relative_url);