Plugin.php 2.1 KB

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