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