xref: /plugin/davcal/vendor/sabre/dav/lib/DAV/Mount/Plugin.php (revision a1a3b6794e0e143a4a8b51d3185ce2d339be61ab)
1*a1a3b679SAndreas Boehler<?php
2*a1a3b679SAndreas Boehler
3*a1a3b679SAndreas Boehlernamespace Sabre\DAV\Mount;
4*a1a3b679SAndreas Boehler
5*a1a3b679SAndreas Boehleruse Sabre\DAV;
6*a1a3b679SAndreas Boehleruse Sabre\HTTP\RequestInterface;
7*a1a3b679SAndreas Boehleruse Sabre\HTTP\ResponseInterface;
8*a1a3b679SAndreas Boehler
9*a1a3b679SAndreas Boehler/**
10*a1a3b679SAndreas Boehler * This plugin provides support for RFC4709: Mounting WebDAV servers
11*a1a3b679SAndreas Boehler *
12*a1a3b679SAndreas Boehler * Simply append ?mount to any collection to generate the davmount response.
13*a1a3b679SAndreas Boehler *
14*a1a3b679SAndreas Boehler * @copyright Copyright (C) 2007-2015 fruux GmbH (https://fruux.com/).
15*a1a3b679SAndreas Boehler * @author Evert Pot (http://evertpot.com/)
16*a1a3b679SAndreas Boehler * @license http://sabre.io/license/ Modified BSD License
17*a1a3b679SAndreas Boehler */
18*a1a3b679SAndreas Boehlerclass Plugin extends DAV\ServerPlugin {
19*a1a3b679SAndreas Boehler
20*a1a3b679SAndreas Boehler    /**
21*a1a3b679SAndreas Boehler     * Reference to Server class
22*a1a3b679SAndreas Boehler     *
23*a1a3b679SAndreas Boehler     * @var Sabre\DAV\Server
24*a1a3b679SAndreas Boehler     */
25*a1a3b679SAndreas Boehler    protected $server;
26*a1a3b679SAndreas Boehler
27*a1a3b679SAndreas Boehler    /**
28*a1a3b679SAndreas Boehler     * Initializes the plugin and registers event handles
29*a1a3b679SAndreas Boehler     *
30*a1a3b679SAndreas Boehler     * @param DAV\Server $server
31*a1a3b679SAndreas Boehler     * @return void
32*a1a3b679SAndreas Boehler     */
33*a1a3b679SAndreas Boehler    function initialize(DAV\Server $server) {
34*a1a3b679SAndreas Boehler
35*a1a3b679SAndreas Boehler        $this->server = $server;
36*a1a3b679SAndreas Boehler        $this->server->on('method:GET', [$this, 'httpGet'], 90);
37*a1a3b679SAndreas Boehler
38*a1a3b679SAndreas Boehler    }
39*a1a3b679SAndreas Boehler
40*a1a3b679SAndreas Boehler    /**
41*a1a3b679SAndreas Boehler     * 'beforeMethod' event handles. This event handles intercepts GET requests ending
42*a1a3b679SAndreas Boehler     * with ?mount
43*a1a3b679SAndreas Boehler     *
44*a1a3b679SAndreas Boehler     * @param RequestInterface $request
45*a1a3b679SAndreas Boehler     * @param ResponseInterface $response
46*a1a3b679SAndreas Boehler     * @return bool
47*a1a3b679SAndreas Boehler     */
48*a1a3b679SAndreas Boehler    function httpGet(RequestInterface $request, ResponseInterface $response) {
49*a1a3b679SAndreas Boehler
50*a1a3b679SAndreas Boehler        $queryParams = $request->getQueryParameters();
51*a1a3b679SAndreas Boehler        if (!array_key_exists('mount', $queryParams)) return;
52*a1a3b679SAndreas Boehler
53*a1a3b679SAndreas Boehler        $currentUri = $request->getAbsoluteUrl();
54*a1a3b679SAndreas Boehler
55*a1a3b679SAndreas Boehler        // Stripping off everything after the ?
56*a1a3b679SAndreas Boehler        list($currentUri) = explode('?', $currentUri);
57*a1a3b679SAndreas Boehler
58*a1a3b679SAndreas Boehler        $this->davMount($response, $currentUri);
59*a1a3b679SAndreas Boehler
60*a1a3b679SAndreas Boehler        // Returning false to break the event chain
61*a1a3b679SAndreas Boehler        return false;
62*a1a3b679SAndreas Boehler
63*a1a3b679SAndreas Boehler    }
64*a1a3b679SAndreas Boehler
65*a1a3b679SAndreas Boehler    /**
66*a1a3b679SAndreas Boehler     * Generates the davmount response
67*a1a3b679SAndreas Boehler     *
68*a1a3b679SAndreas Boehler     * @param ResponseInterface $response
69*a1a3b679SAndreas Boehler     * @param string $uri absolute uri
70*a1a3b679SAndreas Boehler     * @return void
71*a1a3b679SAndreas Boehler     */
72*a1a3b679SAndreas Boehler    function davMount(ResponseInterface $response, $uri) {
73*a1a3b679SAndreas Boehler
74*a1a3b679SAndreas Boehler        $response->setStatus(200);
75*a1a3b679SAndreas Boehler        $response->setHeader('Content-Type', 'application/davmount+xml');
76*a1a3b679SAndreas Boehler        ob_start();
77*a1a3b679SAndreas Boehler        echo '<?xml version="1.0"?>', "\n";
78*a1a3b679SAndreas Boehler        echo "<dm:mount xmlns:dm=\"http://purl.org/NET/webdav/mount\">\n";
79*a1a3b679SAndreas Boehler        echo "  <dm:url>", htmlspecialchars($uri, ENT_NOQUOTES, 'UTF-8'), "</dm:url>\n";
80*a1a3b679SAndreas Boehler        echo "</dm:mount>";
81*a1a3b679SAndreas Boehler        $response->setBody(ob_get_clean());
82*a1a3b679SAndreas Boehler
83*a1a3b679SAndreas Boehler    }
84*a1a3b679SAndreas Boehler
85*a1a3b679SAndreas Boehler
86*a1a3b679SAndreas Boehler}
87