sabre/dav Documentation

repository·master·Indexed 23 days ago

https://github.com/sabre-io/dav

A robust PHP framework for building WebDAV, CalDAV, and CardDAV servers. It provides an extensible framework for managing file trees, handling WebDAV methods (PROPFIND, MOVE, COPY, etc.), and supporting extensions like locking and ACLs. The library includes a PDO CalDAV backend for storing calendar data in databases like MySQL or SQLite, supporting CRUD operations on calendar objects, scheduling, subscriptions, and sharing.

Tokens
9.4K
Snippets
12
Records
58
Agent score
82%

What's inside sabre/dav

  1. Understand the CalendarHome abstraction

    master

    In the CalDAV implementation, CalendarHome represents a node typically located within a user's calendar-homeset. It acts as a container for a user's personal resources, including:

    • Calendars: The user's primary calendar collections.
    • Special Nodes: Depending on backend support, it can contain an inbox and outbox (for Scheduling), a notifications collection, and calendar subscriptions.
    • ACLs: It manages Access Control Lists (ACLs) for the user's calendar space.

    CalendarHome delegates actual data storage and retrieval to a BackendackendInterface.

  2. Implement a DAVACL server plugin

    master

    To extend WebDAV functionality with Access Control Lists (ACL), you can implement or extend the DAVACL\Plugin base class. This class provides support for RFC3744 features, including various REPORT methods used for principal matching, property expansion, and principal searching.

    When implementing a plugin, you can use getPluginInfo() to provide metadata that can be displayed by browser interfaces, including a name and a description (which may contain unsanitized HTML).

    public function getPluginInfo(): array
    {
        return [
            'name' => $this->getPluginName(),
            'description' => 'Adds support for WebDAV ACL (rfc3744)',
            'link' => 'http://sabre.io/dav/acl/',
        ];
    }
  3. Initialize the SabreDAV Server

    master
    The Sabre\DAV\Server class is the main entry point for a WebDAV server. You can initialize it by providing a directory tree, a root node, or an array of top-level children. If no tree is provided, it defaults to a SimpleCollection named 'root'.
  4. Initialize the SabreDAV Client

    master

    To use the Sabre\DAV\Client, instantiate it with a $settings array. The baseUri is a required setting. You can also configure authentication, proxies, and content encoding.

    Supported Settings:

    • baseUri (string, required): The base URL of the WebDAV server.
    • userName (string, optional): Username for authentication.
    • password (string, optional): Password for authentication.
    • proxy (string, optional): Proxy server configuration.
    • authType (int, optional): A bitmap of authentication methods (see constants).
    • encoding (int, optional): A bitmap of supported encodings (see constants).
  5. Run the SabreDAV test server via CLI

    master

    The bin/sabredav.php script is a test server implementation designed to be run using the PHP built-in web server. It initializes a SabreDAV server instance with a root directory set to the current working directory (getcwd()) and includes the Sabre\DAV\Browser\Plugin to allow web-based browsing of the WebDAV filesystem.

    Note: This script is specifically intended to be executed via the PHP built-in web server (e.g., php -S localhost:8080 bin/sabredav.php) rather than as a standalone CLI command.

  6. Configure the PDO CalDAV backend

    master

    The Sabre\CalDAV\Backend\PDO class is a CalDAV backend implementation that uses a PDO database (such as MySQL or SQLite) to store calendar data.

    When initializing the backend, you can customize the database table names used for various components of the CalDAV system by setting the following public properties:

    • $calendarTableName: Table for calendars.
    • $calendarInstancesTableName: Table for calendar instances (e.g., when a calendar is shared).
    • $calendarObjectTableName: Table for individual calendar objects (events, todos, etc.).
    • $calendarChangesTableName: Table for tracking changes.
    • $schedulingObjectTableName: Table for inbox/scheduling items.
    • $calendarSubscriptionsTableName: Table for calendar subscriptions.

    You can also extend the $propertyMap or $subscriptionPropertyMap to map additional CalDAV/iCalendar properties to specific database field names. Note that only string-based properties are supported via this mapping.

  7. Configure the DAVACL Plugin

    master

    The Sabre\DAVACL\Plugin class can be customized via several public properties to change how permissions and principals are handled.

    Key configuration options include:

    • $principalCollectionSet: A list of URLs containing principal collections. Modify this if your principals are located elsewhere.
    • $hideNodesFromListings: If set to true, nodes that are inaccessible by the user will be hidden from directory listings (e.g., PROPFIND on a parent with Depth: 1). Defaults to false.
    • $adminPrincipals: A list of principal URIs that will automatically receive {DAV:}all privileges as protected privileges.
    • $allowUnauthenticatedAccess: If true, the plugin modifies the auth plugin to only require login when a privileged operation is denied. Set to false to harden security.
    • $principalSearchPropertySet: Defines which properties a client can search using the {DAV:}principal-property-search report. Keys are property names, values are descriptions.
  8. Configure Server Properties

    master

    The Server class provides several public properties and static settings to control its behavior:

    • $debugExceptions (bool): If true, the XML error response will include detailed information like file, line, code, and stack trace. Defaults to false.
    • $enablePropfindDepthInfinity (bool): If true, allows Depth: infinity on PROPFIND requests. This can be a DoS vector and is false by default.
    • $resourceTypeMapping (array): Maps node classes/interfaces to DAV resource types (e.g., ICollection::class => '{DAV:}collection').
    • $protectedProperties (array): A list of properties (e.g., {DAV:}getetag) that are server-controlled and cannot be modified via PROPPATCH.
    • static $exposeVersion (bool): If false, the SabreDAV version number is hidden from headers and XML responses. Defaults to true.
    • static $streamMultiStatus (bool): If true, PROPFIND multi-status responses are streamed to the output buffer to save memory. Defaults to false.
  9. Instantiate a Principal object

    master

    The Sabre\DAVACL\Principal class represents a user or group in the DAVACL system. To instantiate it, you must provide an implementation of PrincipalBackend\BackendInterface and an array of properties that includes at least the uri key.

    Note that the uri is used as the unique identifier for the principal.

  10. Create a directory with createDirectory()

    master

    The createDirectory($uri) method is a convenience wrapper used to create a new directory (collection) at the specified URI. It internally calls createCollection() using a standard MkCol object with the {DAV:}collection resource type.

    /**
         * @param string $uri
         */
        public function createDirectory($uri)
  11. Manage user subscriptions with createSubscription() and updateSubscription()

    master

    The PDO backend provides methods to manage CalDAV subscriptions for a principal.

    Create a subscription

    Use createSubscription($principalUri, $uri, array $properties) to create a new subscription. The $properties array must include the {http://calendarserver.org/ns/}source key, which contains the subscription source object.

    Update a subscription

    Use updateSubscription($subscriptionId, PropPatch $propPatch) to modify an existing subscription. You must pass a PropPatch object and call its handle() method, specifying the supported properties (including {http://calendarserver.org/ns/}source) to process the mutations.

    Delete a subscription

    Use deleteSubscription($subscriptionId) to remove a subscription.

    public function createSubscription($principalUri, $uri, array $properties)
    public function updateSubscription($subscriptionId, PropPatch $propPatch)
    public function deleteSubscription($subscriptionId)