Plugin.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sabre\DAV\PropertyStorage;
  4. use Sabre\DAV\INode;
  5. use Sabre\DAV\PropFind;
  6. use Sabre\DAV\PropPatch;
  7. use Sabre\DAV\Server;
  8. use Sabre\DAV\ServerPlugin;
  9. /**
  10. * PropertyStorage Plugin.
  11. *
  12. * Adding this plugin to your server allows clients to store any arbitrary
  13. * WebDAV property.
  14. *
  15. * See:
  16. * http://sabre.io/dav/property-storage/
  17. *
  18. * for more information.
  19. *
  20. * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
  21. * @author Evert Pot (http://evertpot.com/)
  22. * @license http://sabre.io/license/ Modified BSD License
  23. */
  24. class Plugin extends ServerPlugin
  25. {
  26. /**
  27. * If you only want this plugin to store properties for a limited set of
  28. * paths, you can use a pathFilter to do this.
  29. *
  30. * The pathFilter should be a callable. The callable retrieves a path as
  31. * its argument, and should return true or false whether it allows
  32. * properties to be stored.
  33. *
  34. * @var callable
  35. */
  36. public $pathFilter;
  37. /**
  38. * @var Backend\BackendInterface
  39. */
  40. public $backend;
  41. /**
  42. * Creates the plugin.
  43. */
  44. public function __construct(Backend\BackendInterface $backend)
  45. {
  46. $this->backend = $backend;
  47. }
  48. /**
  49. * This initializes the plugin.
  50. *
  51. * This function is called by Sabre\DAV\Server, after
  52. * addPlugin is called.
  53. *
  54. * This method should set up the required event subscriptions.
  55. */
  56. public function initialize(Server $server)
  57. {
  58. $server->on('propFind', [$this, 'propFind'], 130);
  59. $server->on('propPatch', [$this, 'propPatch'], 300);
  60. $server->on('afterMove', [$this, 'afterMove']);
  61. $server->on('afterUnbind', [$this, 'afterUnbind']);
  62. }
  63. /**
  64. * Called during PROPFIND operations.
  65. *
  66. * If there's any requested properties that don't have a value yet, this
  67. * plugin will look in the property storage backend to find them.
  68. */
  69. public function propFind(PropFind $propFind, INode $node)
  70. {
  71. $path = $propFind->getPath();
  72. $pathFilter = $this->pathFilter;
  73. if ($pathFilter && !$pathFilter($path)) {
  74. return;
  75. }
  76. $this->backend->propFind($propFind->getPath(), $propFind);
  77. }
  78. /**
  79. * Called during PROPPATCH operations.
  80. *
  81. * If there's any updated properties that haven't been stored, the
  82. * propertystorage backend can handle it.
  83. *
  84. * @param string $path
  85. */
  86. public function propPatch($path, PropPatch $propPatch)
  87. {
  88. $pathFilter = $this->pathFilter;
  89. if ($pathFilter && !$pathFilter($path)) {
  90. return;
  91. }
  92. $this->backend->propPatch($path, $propPatch);
  93. }
  94. /**
  95. * Called after a node is deleted.
  96. *
  97. * This allows the backend to clean up any properties still in the
  98. * database.
  99. *
  100. * @param string $path
  101. */
  102. public function afterUnbind($path)
  103. {
  104. $pathFilter = $this->pathFilter;
  105. if ($pathFilter && !$pathFilter($path)) {
  106. return;
  107. }
  108. $this->backend->delete($path);
  109. }
  110. /**
  111. * Called after a node is moved.
  112. *
  113. * This allows the backend to move all the associated properties.
  114. *
  115. * @param string $source
  116. * @param string $destination
  117. */
  118. public function afterMove($source, $destination)
  119. {
  120. $pathFilter = $this->pathFilter;
  121. if ($pathFilter && !$pathFilter($source)) {
  122. return;
  123. }
  124. // If the destination is filtered, afterUnbind will handle cleaning up
  125. // the properties.
  126. if ($pathFilter && !$pathFilter($destination)) {
  127. return;
  128. }
  129. $this->backend->move($source, $destination);
  130. }
  131. /**
  132. * Returns a plugin name.
  133. *
  134. * Using this name other plugins will be able to access other plugins
  135. * using \Sabre\DAV\Server::getPlugin
  136. *
  137. * @return string
  138. */
  139. public function getPluginName()
  140. {
  141. return 'property-storage';
  142. }
  143. /**
  144. * Returns a bunch of meta-data about the plugin.
  145. *
  146. * Providing this information is optional, and is mainly displayed by the
  147. * Browser plugin.
  148. *
  149. * The description key in the returned array may contain html and will not
  150. * be sanitized.
  151. *
  152. * @return array
  153. */
  154. public function getPluginInfo()
  155. {
  156. return [
  157. 'name' => $this->getPluginName(),
  158. 'description' => 'This plugin allows any arbitrary WebDAV property to be set on any resource.',
  159. 'link' => 'http://sabre.io/dav/property-storage/',
  160. ];
  161. }
  162. }