| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBooking\Package\CalDav\Clients; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use FluentBooking\Package\CalDav\ICal\XmlParser; |
| 7 |
use FluentBooking\Package\CalDav\Entities\Calendar; |
| 8 |
|
| 9 |
class ICloud extends Client |
| 10 |
{ |
| 11 |
public function getCalendars() |
| 12 |
{ |
| 13 |
$url = $this->getCalendarHomeset($this->getPrincipal()); |
| 14 |
|
| 15 |
return parent::getCalendarsInfo($url); |
| 16 |
} |
| 17 |
|
| 18 |
public function getCalendar($url) |
| 19 |
{ |
| 20 |
$url = $this->makeUrl($url); |
| 21 |
|
| 22 |
return parent::getCalendarInfo($url); |
| 23 |
} |
| 24 |
|
| 25 |
public function getEvents($url, $dateRange = []) |
| 26 |
{ |
| 27 |
$url = $this->makeUrl($url); |
| 28 |
|
| 29 |
return parent::getEventsFrom($url, $dateRange); |
| 30 |
} |
| 31 |
|
| 32 |
public function getEvent($uid) |
| 33 |
{ |
| 34 |
$url = $this->makeUrl($uid); |
| 35 |
|
| 36 |
return parent::getEventFrom($url); |
| 37 |
} |
| 38 |
|
| 39 |
public function addEvent($url, $payload) |
| 40 |
{ |
| 41 |
$url = $this->makeUrl($url); |
| 42 |
|
| 43 |
return parent::addEventTo($url, $payload); |
| 44 |
} |
| 45 |
|
| 46 |
public function deleteEvent($url) |
| 47 |
{ |
| 48 |
$url = $this->makeUrl($url); |
| 49 |
|
| 50 |
return parent::deleteEvent($url); |
| 51 |
} |
| 52 |
|
| 53 |
protected function getAuthCredential() |
| 54 |
{ |
| 55 |
return 'Basic ' . base64_encode( |
| 56 |
$this->username . ':' . $this->password |
| 57 |
); |
| 58 |
} |
| 59 |
|
| 60 |
public function getPrincipal() |
| 61 |
{ |
| 62 |
$payload = '<?xml version="1.0" encoding="utf-8"?> |
| 63 |
<propfind xmlns="DAV:"> |
| 64 |
<prop> |
| 65 |
<current-user-principal/> |
| 66 |
</prop> |
| 67 |
</propfind>'; |
| 68 |
|
| 69 |
$headers = array( |
| 70 |
'Authorization' => 'Basic ' . base64_encode($this->username . ':' . $this->password), |
| 71 |
'Content-Type' => 'application/xml', |
| 72 |
); |
| 73 |
|
| 74 |
$response = $this->propfind($this->baseUrl, [ |
| 75 |
'payload' => $payload |
| 76 |
]); |
| 77 |
|
| 78 |
if (!is_wp_error($response)) { |
| 79 |
if ($body = wp_remote_retrieve_body($response)) { |
| 80 |
$data = XmlParser::parse($body); |
| 81 |
$href = $data[0]['propstat'][0]['prop'][0]['current-user-principal'][0]['href'][0]; |
| 82 |
return $href; |
| 83 |
} elseif ($res = $response['response']) { |
| 84 |
return $res; |
| 85 |
} |
| 86 |
} else { |
| 87 |
return $response; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
public function getCalendarHomeset($url) |
| 92 |
{ |
| 93 |
$url = $this->makeUrl($url); |
| 94 |
|
| 95 |
$payload = '<?xml version="1.0" encoding="utf-8"?> |
| 96 |
<propfind xmlns="DAV:" xmlns:c="urn:ietf:params:xml:ns:caldav"> |
| 97 |
<prop><c:calendar-home-set/></prop> |
| 98 |
</propfind>'; |
| 99 |
|
| 100 |
$response = $this->propfind($url, [ |
| 101 |
'payload' => $payload |
| 102 |
]); |
| 103 |
|
| 104 |
if (!is_wp_error($response)) { |
| 105 |
if ($body = wp_remote_retrieve_body($response)) { |
| 106 |
$data = XmlParser::parse($body); |
| 107 |
$href = $data[0]['propstat'][0]['prop'][0]['calendar-home-set'][0]['href'][0]; |
| 108 |
return $href; |
| 109 |
} elseif ($res = $response['response']) { |
| 110 |
return $res; |
| 111 |
} |
| 112 |
} else { |
| 113 |
return $response; |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
|