CalDavCalendarManager
đĻ CalDav\Client\CalDavCalendarManager
CalDAV Calendar Manager Client Handles all calendar-related operations through the CalDAV protocol. This class provides functionality for: - Listing available calendars - Creating new calendars - Updating existing calendars - Deleting calendars - Managing calendar sharing
function __construct($baseUrl, $username, $password, $config ) {
$this->baseUrl = rtrim($baseUrl, '/');
$this->username = $username;
$this->password = $password;
$this->calendarRepository = new \CalDav\Repository\CalendarRepository($config);
$this->setDefaultHeaders([
'Content-Type: application/xml; charset=utf-8',
'Depth: 0'
]);
}
đ§ __construct
function __construct($baseUrl, $username, $password, $config ) {
$this->baseUrl = rtrim($baseUrl, '/');
$this->username = $username;
$this->password = $password;
$this->calendarRepository = new \CalDav\Repository\CalendarRepository($config);
$this->setDefaultHeaders([
'Content-Type: application/xml; charset=utf-8',
'Depth: 0'
]);
}
đ§ __construct
function __construct($baseUrl, $username, $password, $config ) {
$this->baseUrl = rtrim($baseUrl, '/');
$this->username = $username;
$this->password = $password;
$this->calendarRepository = new \CalDav\Repository\CalendarRepository($config);
$this->setDefaultHeaders([
'Content-Type: application/xml; charset=utf-8',
'Depth: 0'
]);
}
đ§ __construct
function __construct($baseUrl, $username, $password, $config ) {
$this->baseUrl = rtrim($baseUrl, '/');
$this->username = $username;
$this->password = $password;
$this->calendarRepository = new \CalDav\Repository\CalendarRepository($config);
$this->setDefaultHeaders([
'Content-Type: application/xml; charset=utf-8',
'Depth: 0'
]);
}
đ§ __construct
function __construct($baseUrl, $username, $password, $config ) {
$this->baseUrl = rtrim($baseUrl, '/');
$this->username = $username;
$this->password = $password;
$this->calendarRepository = new \CalDav\Repository\CalendarRepository($config);
$this->setDefaultHeaders([
'Content-Type: application/xml; charset=utf-8',
'Depth: 0'
]);
}
đ§ __construct
Constructor Initializes the calendar manager with server credentials
function __construct($baseUrl, $username, $password, $config ) {
$this->baseUrl = rtrim($baseUrl, '/');
$this->username = $username;
$this->password = $password;
$this->calendarRepository = new \CalDav\Repository\CalendarRepository($config);
$this->setDefaultHeaders([
'Content-Type: application/xml; charset=utf-8',
'Depth: 0'
]);
}
âī¸ Parameters
$baseUrl (string) Base URL of the CalDAV server
$username (string) Username for authentication
$password (string) Password for authentication
đ§ listCalendars
List all calendars for a user Retrieves all calendars accessible to the specified user using PROPFIND request. The response includes calendar properties such as display name, description, and color.
function listCalendars($username): array {
try {
$calendarHomeUrl = "{$this->baseUrl}";
$this->initCurl($calendarHomeUrl, 'PROPFIND', ['Depth: 1']);
// Build XML request for calendar properties
$xmlRequest = $this->buildPropfindXml([
'{DAV:}displayname',
'{DAV:}resourcetype',
'{urn:ietf:params:xml:ns:caldav}calendar-description',
'{http://apple.com/ns/ical/}calendar-color',
'{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set'
// ... (truncated)
âī¸ Parameters
$username (string) The username to get calendars for
âŠī¸ Returns
(array) Array of calendar information
â ī¸ Throws
Exception : If there's an error communicating with the server
đ§ buildPropfindXml
Build PROPFIND XML request Creates an XML request for the PROPFIND method to retrieve calendar properties
function buildPropfindXml(array $properties): string {
$xml = '<?xml version="1.0" encoding="utf-8" ?>';
$xml .= '<D:propfind xmlns:D="DAV:">';
$xml .= '<D:prop>';
foreach ($properties as $property) {
$parts = explode('}', $property);
$namespace = trim($parts[0], '{');
$name = $parts[1];
if ($namespace === 'DAV:') {
$xml .= "<D:$name />";
// ... (truncated)
âī¸ Parameters
$properties (array) Array of property URIs to request
âŠī¸ Returns
(string) Formatted XML request
đ§ createCalendar
Create a new calendar Creates a new calendar for the specified user with the given properties
function createCalendar($username, array $calendarData): array {
try {
// Add default values for synctoken and components
$calendarData['synctoken'] = 1; // Default synctoken for new calendars
$calendarData['components'] = 'VEVENT'; // Default components support
$calendar = Calendar::fromArray($calendarData, $username);
// Save to database only
try {
$success = $this->calendarRepository->save($calendar);
if ($success) {
return ['success' => true, 'message' => 'Calendar created successfully', 'calendar' => $calendar->toArray()];
}
return ['success' => false, 'message' => 'Failed to save calendar to database'];
// ... (truncated)
âī¸ Parameters
$username (string) Owner of the calendar
$calendarData (array) Calendar properties (name, color, etc.)
âŠī¸ Returns
(array) Result of the operation
đ§ setDirectoryAcl
Set ACL permissions on a directory
function setDirectoryAcl($username, $url): array {
try {
// Build ACL XML with full permissions including bind privilege
$xml = '<?xml version="1.0" encoding="utf-8" ?>';
$xml .= '<acl xmlns="DAV:">';
// Owner privileges
$xml .= '<ace>';
$xml .= '<principal><href>/principals/' . $username . '</href></principal>';
$xml .= '<grant>';
$xml .= '<privilege><read/></privilege>';
$xml .= '<privilege><write/></privilege>';
$xml .= '<privilege><bind/></privilege>';
// ... (truncated)
âī¸ Parameters
$username (string) The username
$url (string) The directory URL
âŠī¸ Returns
(array) Result of the operation
đ§ checkAclSupport
Check if ACL support is enabled on the server
function checkAclSupport(): array {
try {
$this->initCurl($this->baseUrl, 'OPTIONS');
$result = $this->executeRequest();
if ($result['httpCode'] !== 200) {
return [
'success' => false,
'enabled' => false,
'message' => 'Failed to check ACL support. Server returned: ' . $result['httpCode']
];
// ... (truncated)
âŠī¸ Returns
(array) ['success' => bool, 'message' => string, 'enabled' => bool]
đ§ directUpdateCalendarColor
Update calendar color directly via PROPPATCH Uses a direct PROPPATCH request to update the calendar color. This is a more reliable approach when standard methods fail.
function directUpdateCalendarColor(string $caldav_url, string $color, ?string $override_username = null, ?string $override_password = null): array
{
try {
// Ensure URL ends with slash
$caldav_url = rtrim($caldav_url, '/') . '/';
// Use provided credentials or fall back to instance credentials
$username = $override_username ?? $this->username;
$password = $override_password ?? $this->password;
// Build XML for PROPPATCH request
$xml = '<?xml version="1.0" encoding="utf-8" ?>';
$xml .= '<propertyupdate xmlns="DAV:" xmlns:C="urn:ietf:params:xml:ns:caldav">';
$xml .= '<set><prop>';
$xml .= '<apple:calendar-color xmlns:apple="http://apple.com/ns/ical/">' .
htmlspecialchars($color) . '</apple:calendar-color>';
// ... (truncated)
âī¸ Parameters
$caldav_url (string) The full calendar URL
$color (string) Calendar color in hex format (e.g. '#FF0000')
$override_username (string|null) Optional username to override the instance username
$override_password (string|null) Optional password to override the instance password
âŠī¸ Returns
(array) Result of the operation with 'success' and 'message' keys