CalDavEventManager
đĻ CalDav\Client\CalDavEventManager
CalDAV Event Manager Handles all event-related operations through the CalDAV protocol including: - Creating new events - Updating existing events - Deleting events - Listing events from calendars - Moving events between calendars
function __construct($baseUrl, $username, $password, $authMethod = 'digest') {
$this->baseUrl = rtrim($baseUrl, '/');
$this->username = $username;
$this->password = $password;
// Set authentication method
if ($authMethod === 'digest') {
$this->authMethod = CURLAUTH_DIGEST;
} else if ($authMethod === 'basic') {
$this->authMethod = CURLAUTH_BASIC;
} else {
$this->authMethod = CURLAUTH_ANY;
// ... (truncated)
đ§ __construct
function __construct($baseUrl, $username, $password, $authMethod = 'digest') {
$this->baseUrl = rtrim($baseUrl, '/');
$this->username = $username;
$this->password = $password;
// Set authentication method
if ($authMethod === 'digest') {
$this->authMethod = CURLAUTH_DIGEST;
} else if ($authMethod === 'basic') {
$this->authMethod = CURLAUTH_BASIC;
} else {
$this->authMethod = CURLAUTH_ANY;
// ... (truncated)
đ§ __construct
function __construct($baseUrl, $username, $password, $authMethod = 'digest') {
$this->baseUrl = rtrim($baseUrl, '/');
$this->username = $username;
$this->password = $password;
// Set authentication method
if ($authMethod === 'digest') {
$this->authMethod = CURLAUTH_DIGEST;
} else if ($authMethod === 'basic') {
$this->authMethod = CURLAUTH_BASIC;
} else {
$this->authMethod = CURLAUTH_ANY;
// ... (truncated)
đ§ __construct
function __construct($baseUrl, $username, $password, $authMethod = 'digest') {
$this->baseUrl = rtrim($baseUrl, '/');
$this->username = $username;
$this->password = $password;
// Set authentication method
if ($authMethod === 'digest') {
$this->authMethod = CURLAUTH_DIGEST;
} else if ($authMethod === 'basic') {
$this->authMethod = CURLAUTH_BASIC;
} else {
$this->authMethod = CURLAUTH_ANY;
// ... (truncated)
đ§ __construct
function __construct($baseUrl, $username, $password, $authMethod = 'digest') {
$this->baseUrl = rtrim($baseUrl, '/');
$this->username = $username;
$this->password = $password;
// Set authentication method
if ($authMethod === 'digest') {
$this->authMethod = CURLAUTH_DIGEST;
} else if ($authMethod === 'basic') {
$this->authMethod = CURLAUTH_BASIC;
} else {
$this->authMethod = CURLAUTH_ANY;
// ... (truncated)
đ§ __construct
function __construct($baseUrl, $username, $password, $authMethod = 'digest') {
$this->baseUrl = rtrim($baseUrl, '/');
$this->username = $username;
$this->password = $password;
// Set authentication method
if ($authMethod === 'digest') {
$this->authMethod = CURLAUTH_DIGEST;
} else if ($authMethod === 'basic') {
$this->authMethod = CURLAUTH_BASIC;
} else {
$this->authMethod = CURLAUTH_ANY;
// ... (truncated)
đ§ __construct
Constructor
function __construct($baseUrl, $username, $password, $authMethod = 'digest') {
$this->baseUrl = rtrim($baseUrl, '/');
$this->username = $username;
$this->password = $password;
// Set authentication method
if ($authMethod === 'digest') {
$this->authMethod = CURLAUTH_DIGEST;
} else if ($authMethod === 'basic') {
$this->authMethod = CURLAUTH_BASIC;
} else {
$this->authMethod = CURLAUTH_ANY;
// ... (truncated)
âī¸ Parameters
- $baseUrl (string) CalDAV server URL
- $username (string) Auth username
- $password (string) Auth password
- $authMethod (string) Authentication method (digest, basic, any)
đ§ setError
Set error information
function setError(string $message, $code = null, \Exception $exception = null): void {
$this->lastError = [
'message' => $message,
'code' => $code,
'exception' => $exception ? $exception->getMessage() : null
];
}
âī¸ Parameters
- $message (string) Error message
- $code (mixed) Error code
- $exception (\Exception|null) Exception if available
đ§ listEvents
List all events from a calendar
function listEvents(string $username, string $calendarId): array {
$debug = [];
try {
// Build the calendar URL
$calendarUrl = "{$this->baseUrl}/calendars/{$username}
âī¸ Parameters
- $username (string) The username who owns the calendar
- $calendarId (string) The calendar ID
âŠī¸ Returns
(array) Array of events
đ§ getEvent
Get a specific event by ID
function getEvent($eventUrl): ?array {
try {
// Initialize curl
$this->ch = curl_init();
curl_setopt_array($this->ch, [
CURLOPT_URL => $eventUrl,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPGET => true,
CURLOPT_USERPWD => $this->username . ':' . $this->password,
CURLOPT_HTTPAUTH => $this->authMethod ?? CURLAUTH_DIGEST,
CURLOPT_SSL_VERIFYPEER => false,
// ... (truncated)
âī¸ Parameters
- $username (string) The username who owns the calendar
- $calendarId (string) The calendar ID
- $eventId (string) The event ID to retrieve
âŠī¸ Returns
(array|null) Event data or null if not found
đ§ parseICalData
Parse iCal data into event array
function parseICalData(string $icalData): ?array {
try {
$icalData = preg_replace("/\r\n[ \t]/", " ", $icalData);
$lines = explode("\n", $icalData);
$event = [];
$inEvent = false;
foreach ($lines as $line) {
$line = trim($line);
if (strcasecmp($line, 'BEGIN:VEVENT') === 0) {
$inEvent = true;
// ... (truncated)
âī¸ Parameters
- $icalData (string) The iCal data to parse
âŠī¸ Returns
(array|null) Parsed event data or null if parsing fails
đ§ parseICalDate
Parse iCal date format to ISO 8601
function parseICalDate(string $line): string {
try {
preg_match('/(\d{8}T\d{6}Z?)/', $line, $matches);
if (!empty($matches[1])) {
$date = $matches[1];
return substr($date, 0, 4) . '-' .
substr($date, 4, 2) . '-' .
substr($date, 6, 2) . 'T' .
substr($date, 9, 2) . ':' .
substr($date, 11, 2) . ':' .
// ... (truncated)
âī¸ Parameters
- $line (string) The date line to parse
âŠī¸ Returns
(string) Formatted date string
đ§ createEvent
Create a new event in a calendar $result = $calDavEventManager->createEvent($username, $calendarUrl, $eventData, null, true); if ($result['success']) { Download the ICS file header('Content-Type: text/calendar; charset=utf-8'); header('Content-Disposition: attachment; filename="' . $result['filename'] . '"'); echo $result['ics_content']; exit; }
function createEvent(string $username, string $calendarUrl, array $eventData, ?string $password = null, bool $download = false): array {
$debug = [];
try {
// Use provided password or fall back to instance password
$password = $password ?? $this->password;
// First verify the calendar exists
$this->ch = curl_init();
curl_setopt_array($this->ch, [
CURLOPT_URL => $calendarUrl,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'PROPFIND',
CURLOPT_USERPWD => $username . ':' . $password,
// ... (truncated)
âī¸ Parameters
- $username (string) The username who owns the calendar
- $calendarId (string) The calendar ID or full path
- $eventData (array) The event data
- $password (string|null) Password for the calendar (optional, defaults to instance password)
- $download (bool) Whether to download the ICS file (default: false)
âŠī¸ Returns
(array) Result of the operation
đ§ sanitizeFilename
Sanitize a string to be used as a filename
function sanitizeFilename(string $filename): string {
// Remove any character that isn't a letter, digit, space, underscore or hyphen
$filename = preg_replace('/[^\w\s\-\.]/u', '', $filename);
// Replace spaces with underscores
$filename = str_replace(' ', '_', $filename);
// Limit length
if (strlen($filename) > 50) {
$filename = substr($filename, 0, 47) . '...';
}
return $filename ?: 'event';
}
âī¸ Parameters
- $filename (string) The filename to sanitize
âŠī¸ Returns
(string) Sanitized filename
đ§ escapeICalText
Escape special characters in iCal text and fold long lines
function escapeICalText(string $text): string {
return addcslashes($text, ',;\\');
}
âī¸ Parameters
- $text (string) Text to escape
âŠī¸ Returns
(string) Escaped and folded text
đ§ updateEvent
Update an existing event
function updateEvent(string $username, string $calendarId, string $eventId, array $eventData, ?string $password = null): array {
$debug = [
'process_steps' => [],
'event_data' => $eventData,
'calendar_id' => $calendarId
];
try {
// Use provided password or fall back to instance password
$password = $password ?? $this->password;
// Clean up the old event ID
$oldEventId = trim($eventId);
$oldEventId = str_replace('.ics', '', $oldEventId);
// ... (truncated)
âī¸ Parameters
- $username (string) The username who owns the calendar
- $calendarId (string) The calendar ID
- $eventId (string) The event ID to update
- $event (array) The updated event data
- $password (string|null) Password for the calendar (optional, defaults to instance password)
âŠī¸ Returns
(array) Response array with success status and message
đ§ deleteEvent
Delete an event from the calendar
function deleteEvent(string $username, string $calendarId, string $eventId, ?string $password = null): array {
$debug = [
'process_steps' => [],
'original_event_id' => $eventId,
'calendar_id' => $calendarId
];
try {
// Use provided password or fall back to instance password
$password = $password ?? $this->password;
// Clean up the event ID - remove any .ics extension and ensure proper format
$eventId = trim($eventId);
$eventId = str_replace('.ics', '', $eventId);
// ... (truncated)
âī¸ Parameters
- $username (string) The username who owns the calendar
- $calendarId (string) The calendar ID
- $eventId (string) The event ID to delete
- $password (string|null) Password for the calendar (optional, defaults to instance password)
âŠī¸ Returns
(array) Response array with success status and message
đ§ moveEvent
Move an event from one calendar to another
function moveEvent(string $username, string $sourceCalendarId, string $targetCalendarId, string $eventId, array $eventData, ?string $sourcePassword = null, ?string $targetPassword = null, ?string $targetUsername = null): array {
$debug = [];
try {
// Use provided passwords or fall back to instance password
$sourcePassword = $sourcePassword ?? $this->password;
$targetPassword = $targetPassword ?? $this->password;
$targetUsername = $targetUsername ?? $username;
// Create the event in the target calendar
$createResult = $this->createEvent($targetUsername, $targetCalendarId, $eventData, $targetPassword);
$debug['create_result'] = $createResult;
if (!$createResult['success']) {
return [
// ... (truncated)
âī¸ Parameters
- $username (string) The username who owns the calendars
- $sourceCalendarId (string) Source calendar ID
- $targetCalendarId (string) Target calendar ID
- $eventId (string) The event ID to move
- $eventData (array) The event data
- $sourcePassword (string|null) Password for the source calendar
- $targetPassword (string|null) Password for the target calendar
âŠī¸ Returns
(array) Response array with success status and message