-
Notifications
You must be signed in to change notification settings - Fork 11
Enhance getEvents with logging and error handling #81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hismayilov
wants to merge
2
commits into
LJPc-solutions:main
Choose a base branch
from
hismayilov:patch-1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,17 +19,51 @@ public function __construct( $baseUri, $userName, $password ) { | |
| } | ||
|
|
||
| public function getEvents( $calendarUrl ) { | ||
| $response = $this->client->propFind( $calendarUrl, [ | ||
| '{DAV:}displayname', | ||
| '{urn:ietf:params:xml:ns:caldav}calendar-description', | ||
| '{urn:ietf:params:xml:ns:caldav}calendar-data', | ||
| ], 2 ); | ||
| \Log::debug( 'CalDAV getEvents called' ); | ||
|
|
||
| $xmlBody = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" . | ||
| '<C:calendar-query xmlns:C="urn:ietf:params:xml:ns:caldav" xmlns:D="DAV:">' . "\n" . | ||
| ' <D:prop>' . "\n" . | ||
| ' <D:getetag/>' . "\n" . | ||
| ' <C:calendar-data/>' . "\n" . | ||
| ' </D:prop>' . "\n" . | ||
| ' <C:filter>' . "\n" . | ||
| ' <C:comp-filter name="VCALENDAR">' . "\n" . | ||
| ' <C:comp-filter name="VEVENT">' . "\n" . | ||
| ' </C:comp-filter>' . "\n" . | ||
| ' </C:comp-filter>' . "\n" . | ||
| ' </C:filter>' . "\n" . | ||
| '</C:calendar-query>'; | ||
|
|
||
| try { | ||
| $response = $this->client->request( 'REPORT', $calendarUrl, $xmlBody, [ | ||
| 'Content-Type' => 'application/xml; charset=utf-8', | ||
| 'Depth' => '1', | ||
| ] ); | ||
| } catch ( \Exception $e ) { | ||
| \Log::debug( 'CalDAV REPORT query failed, returning empty events list', [ | ||
| 'error' => $e->getMessage(), | ||
| ] ); | ||
| return []; | ||
| } | ||
|
|
||
| \Log::debug( 'CalDAV REPORT response', [ | ||
| 'status' => $response['statusCode'] ?? 'no status', | ||
| 'body_length' => strlen( $response['body'] ?? '' ), | ||
| ] ); | ||
|
|
||
| $events = []; | ||
| foreach ( $response as $eventData ) { | ||
| if ( isset( $eventData['{urn:ietf:params:xml:ns:caldav}calendar-data'] ) ) { | ||
| $events[] = $eventData['{urn:ietf:params:xml:ns:caldav}calendar-data']; | ||
| if ( isset( $response['statusCode'] ) && $response['statusCode'] >= 200 && $response['statusCode'] < 300 ) { | ||
| if ( isset( $response['body'] ) && ! empty( $response['body'] ) ) { | ||
| $events = $this->extractCalendarDataFromMultistatus( $response['body'] ); | ||
| if ( count( $events ) === 0 ) { | ||
| $hrefs = $this->extractEventHrefsFromMultistatus( $response['body'], $calendarUrl ); | ||
| $events = $this->fetchEventsByHref( $hrefs ); | ||
| } | ||
| \Log::debug( 'CalDAV parsed events count', [ 'count' => count( $events ) ] ); | ||
| } | ||
| } else { | ||
| \Log::debug( 'CalDAV REPORT failed', [ 'status' => $response['statusCode'] ?? 'unknown' ] ); | ||
| } | ||
|
|
||
| return $events; | ||
|
|
@@ -100,7 +134,7 @@ public function deleteEvent( $calendarUrl, $uid ) { | |
| /** | ||
| * Get a single event by UID using CalDAV REPORT query | ||
| * This is much more efficient than fetching all events | ||
| * | ||
| * | ||
| * @param string $calendarUrl The calendar URL | ||
| * @param string $uid The event UID to fetch | ||
| * @return array|null The event data or null if not found | ||
|
|
@@ -157,25 +191,25 @@ public function getEventByUid( $calendarUrl, $uid ) { | |
|
|
||
| /** | ||
| * Check if the CalDAV server supports REPORT queries | ||
| * | ||
| * | ||
| * @param string $calendarUrl | ||
| * @return bool | ||
| */ | ||
| public function supportsReportQuery( $calendarUrl ) { | ||
| try { | ||
| $response = $this->client->options( $calendarUrl ); | ||
|
|
||
| if ( isset( $response['headers']['allow'] ) ) { | ||
| $allow = is_array( $response['headers']['allow'] ) | ||
| ? implode( ',', $response['headers']['allow'] ) | ||
| $allow = is_array( $response['headers']['allow'] ) | ||
| ? implode( ',', $response['headers']['allow'] ) | ||
| : $response['headers']['allow']; | ||
| return stripos( $allow, 'REPORT' ) !== false; | ||
| } | ||
|
|
||
| // Also check DAV header for calendar-access | ||
| if ( isset( $response['headers']['dav'] ) ) { | ||
| $dav = is_array( $response['headers']['dav'] ) | ||
| ? implode( ',', $response['headers']['dav'] ) | ||
| $dav = is_array( $response['headers']['dav'] ) | ||
| ? implode( ',', $response['headers']['dav'] ) | ||
| : $response['headers']['dav']; | ||
| return stripos( $dav, 'calendar-access' ) !== false; | ||
| } | ||
|
|
@@ -185,8 +219,115 @@ public function supportsReportQuery( $calendarUrl ) { | |
| 'calendar_url' => $calendarUrl | ||
| ] ); | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * Extract calendar-data nodes from a CalDAV multistatus XML response. | ||
| * Uses XML parsing for namespace-agnostic extraction and regex as fallback. | ||
| * | ||
| * @param string $responseBody | ||
| * @return string[] | ||
| */ | ||
| private function extractCalendarDataFromMultistatus( $responseBody ) { | ||
| $events = []; | ||
|
|
||
| libxml_use_internal_errors( true ); | ||
| $document = new \DOMDocument(); | ||
| if ( $document->loadXML( $responseBody ) ) { | ||
| $xpath = new \DOMXPath( $document ); | ||
| $nodes = $xpath->query( '//*[local-name()="calendar-data"]' ); | ||
| if ( $nodes !== false ) { | ||
| foreach ( $nodes as $node ) { | ||
| $data = trim( $node->nodeValue ); | ||
| if ( $data !== '' ) { | ||
| $events[] = html_entity_decode( $data, ENT_XML1, 'UTF-8' ); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't |
||
| } | ||
| } | ||
| } | ||
| } | ||
| libxml_clear_errors(); | ||
|
|
||
| if ( count( $events ) > 0 ) { | ||
| return $events; | ||
| } | ||
|
|
||
| $matches = []; | ||
| preg_match_all( '/<(?:[A-Za-z0-9_-]+:)?calendar-data[^>]*>(.*?)<\/(?:[A-Za-z0-9_-]+:)?calendar-data>/s', $responseBody, $matches ); | ||
| if ( isset( $matches[1] ) ) { | ||
| foreach ( $matches[1] as $data ) { | ||
| $decoded = html_entity_decode( trim( $data ), ENT_XML1, 'UTF-8' ); | ||
| if ( $decoded !== '' ) { | ||
| $events[] = $decoded; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return $events; | ||
| } | ||
|
|
||
| /** | ||
| * Extract event hrefs from a CalDAV multistatus XML response. | ||
| * | ||
| * @param string $responseBody | ||
| * @param string $calendarUrl | ||
| * @return string[] | ||
| */ | ||
| private function extractEventHrefsFromMultistatus( $responseBody, $calendarUrl ) { | ||
| $hrefs = []; | ||
|
|
||
| libxml_use_internal_errors( true ); | ||
| $document = new \DOMDocument(); | ||
| if ( $document->loadXML( $responseBody ) ) { | ||
| $xpath = new \DOMXPath( $document ); | ||
| $nodes = $xpath->query( '//*[local-name()="href"]' ); | ||
| if ( $nodes !== false ) { | ||
| foreach ( $nodes as $node ) { | ||
| $href = trim( $node->nodeValue ); | ||
| if ( $href === '' ) { | ||
| continue; | ||
| } | ||
| if ( substr( $href, -1 ) === '/' ) { | ||
| continue; | ||
| } | ||
| if ( stripos( $href, '.ics' ) === false ) { | ||
| continue; | ||
| } | ||
| $hrefs[] = $href; | ||
| } | ||
| } | ||
| } | ||
| libxml_clear_errors(); | ||
|
|
||
| return array_values( array_unique( $hrefs ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Fetch event bodies by href list. | ||
| * | ||
| * @param string[] $hrefs | ||
| * @return string[] | ||
| */ | ||
| private function fetchEventsByHref( $hrefs ) { | ||
| $events = []; | ||
| foreach ( $hrefs as $href ) { | ||
| try { | ||
| $response = $this->client->request( 'GET', $href, null, [ | ||
| 'Accept' => 'text/calendar, */*;q=0.1', | ||
| ] ); | ||
| if ( isset( $response['statusCode'] ) && $response['statusCode'] >= 200 && $response['statusCode'] < 300 ) { | ||
| $body = trim( $response['body'] ?? '' ); | ||
| if ( $body !== '' ) { | ||
| $events[] = $body; | ||
| } | ||
| } | ||
| } catch ( \Exception $e ) { | ||
| \Log::debug( 'CalDAV event GET failed', [ 'error' => $e->getMessage() ] ); | ||
| } | ||
| } | ||
|
|
||
| return $events; | ||
| } | ||
|
|
||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DOMDocument::loadXML()withoutLIBXML_NONETallows external entity loading on PHP < 8.0 (XXE risk). FreeScout supports PHP 7.x. Use:Same applies to
extractEventHrefsFromMultistatusbelow.