From 0a15de8247764997be10233ce091a8a0d5e0babf Mon Sep 17 00:00:00 2001 From: Dmitry Ivanov Date: Mon, 10 Jul 2023 15:55:13 +0200 Subject: [PATCH 1/3] Some improvements and bug fixes --- src/CalDAVClient.php | 16 ++++++++-------- src/SimpleCalDAVClient.php | 18 +++++++++++++++++- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/CalDAVClient.php b/src/CalDAVClient.php index 502a310..31fe53a 100644 --- a/src/CalDAVClient.php +++ b/src/CalDAVClient.php @@ -344,7 +344,7 @@ function GetHttpResultCode() { * * @return bool|string The content of the response from the server */ - function DoRequest( $url = null ) { + function DoRequest( $url = null, $forceBody = false ) { if (is_null($url)) { $url = $this->full_url; } @@ -357,7 +357,7 @@ function DoRequest( $url = null ) { curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, $this->requestMethod); // Empty body. If not used, cURL will spend ~5s on this request - if ($this->requestMethod == 'HEAD' || empty($this->body) ) { + if (!$forceBody && ($this->requestMethod == 'HEAD' || empty($this->body)) ) { curl_setopt($this->ch, CURLOPT_NOBODY, TRUE); } else { curl_setopt($this->ch, CURLOPT_NOBODY, FALSE); @@ -460,10 +460,10 @@ function DoXMLRequest( $request_method, $xml, $url = null ) { * * @param string $url The URL to GET */ - function DoGETRequest( $url ) { + function DoGETRequest( $url, $forceBody = false ) { $this->body = ""; $this->requestMethod = "GET"; - return $this->DoRequest( $url ); + return $this->DoRequest( $url, $forceBody ); } @@ -506,7 +506,7 @@ function DoPUTRequest( $url, $icalendar, $etag = null ) { $save_request = $this->httpRequest; $save_response_headers = $this->httpResponseHeaders; $save_http_result = $this->httpResultCode; - $this->DoHEADRequest( $url ); + $this->DoGETRequest( $url ); if ( preg_match( '{^Etag:\s+"([^"]*)"\s*$}im', $this->httpResponseHeaders, $matches ) ) $etag = $matches[1]; else if ( preg_match( '{^ETag:\s+([^\s]*)\s*$}im', $this->httpResponseHeaders, $matches ) ) $etag = $matches[1]; /* @@ -1106,9 +1106,9 @@ function GetEntryByUid( $uid, $relative_url = null ) { * * @return string The iCalendar of the calendar entry */ - function GetEntryByHref( $href ) { + function GetEntryByHref( $href, $forceBody = false ) { //$href = str_replace( rawurlencode('/'),'/',rawurlencode($href)); - $response = $this->DoGETRequest( $href ); + $response = $this->DoGETRequest( $href, $forceBody ); $report = array(); @@ -1136,7 +1136,7 @@ function GetEntryByHref( $href ) { $this->httpResultCode = $save_http_result; } - $report = array(array('etag'=>$etag)); + $report = array(array('etag' => $etag, 'body' => $this->httpResponseBody)); return $report; } diff --git a/src/SimpleCalDAVClient.php b/src/SimpleCalDAVClient.php index 68e86b6..a58efe8 100644 --- a/src/SimpleCalDAVClient.php +++ b/src/SimpleCalDAVClient.php @@ -230,7 +230,7 @@ function change ( $href, $new_data, $etag ) $newEtag = $this->client->DoPUTRequest( $href, $new_data, $etag ); // PUT-request successfull? - if ( $this->client->GetHttpResultCode() != '204' && $this->client->GetHttpResultCode() != '200' ) + if ( $this->client->GetHttpResultCode() != '204' && $this->client->GetHttpResultCode() != '200' && $this->client->GetHttpResultCode() != '201' ) { throw new CalDAVException('Recieved unknown HTTP status', $this->client); } @@ -413,6 +413,22 @@ function getCustomReport ( $filterXML ) return $report; } + + function getEntryByHref($href) + { + // Connection and calendar set? + if(!isset($this->client)) throw new Exception('No connection. Try connect().'); + if(!isset($this->client->calendar_url)) throw new Exception('No calendar selected. Try findCalendars() and setCalendar().'); + + // Does $href exist? + $result = $this->client->GetEntryByHref($href, true); + if ( $this->client->GetHttpResultCode() == '200' ); + else if ( $this->client->GetHttpResultCode() == '404' ) throw new CalDAVException('Can\'t find '.$href.' on the server', $this->client); + else throw new CalDAVException('Recieved unknown HTTP status', $this->client); + + $entry = array_shift($result); + return $entry; + } } ?> From ce2ce48a3e778f88b8abd9e7487f52bd0f9e7c63 Mon Sep 17 00:00:00 2001 From: Dmitry Ivanov Date: Mon, 17 Jul 2023 14:47:54 +0200 Subject: [PATCH 2/3] Fixing 'Received HTTP/0.9 when not allowed' issue --- src/CalDAVClient.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CalDAVClient.php b/src/CalDAVClient.php index 31fe53a..3dfbb0c 100644 --- a/src/CalDAVClient.php +++ b/src/CalDAVClient.php @@ -129,7 +129,7 @@ function __construct( $base_url, $user, $pass, $options = array()) { CURLOPT_MAXREDIRS => 2, CURLOPT_FORBID_REUSE => FALSE, CURLOPT_RETURNTRANSFER => TRUE, - CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, + CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_0, CURLOPT_HTTPAUTH => isset($options['auth']) ? $options['auth'] : (CURLAUTH_BASIC | CURLAUTH_DIGEST), From 122de4fe5c5e2ff976db6b182321fba412464bae Mon Sep 17 00:00:00 2001 From: Dmitry Ivanov Date: Fri, 21 Jul 2023 11:28:56 +0200 Subject: [PATCH 3/3] Added exception handling on curl errors. --- src/CalDAVClient.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/CalDAVClient.php b/src/CalDAVClient.php index 3dfbb0c..d15c3a3 100644 --- a/src/CalDAVClient.php +++ b/src/CalDAVClient.php @@ -383,10 +383,11 @@ function DoRequest( $url = null, $forceBody = false ) { $response = curl_exec($this->ch); if (FALSE === $response) { - // TODO better error handling - log_message('ERROR', 'Error requesting ' . $url . ': ' - . curl_error($this->ch)); - return false; + throw new CalDAVException( + 'curl error: ' . curl_error($this->ch) . + ', request method: ' . $this->requestMethod . + ', url: ' . $url, $this + ); } $info = curl_getinfo($this->ch);