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