LockDiscovery.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sabre\DAV\Xml\Property;
  4. use Sabre\DAV;
  5. use Sabre\DAV\Locks\LockInfo;
  6. use Sabre\Xml\Element\XmlFragment;
  7. use Sabre\Xml\Writer;
  8. use Sabre\Xml\XmlSerializable;
  9. /**
  10. * Represents {DAV:}lockdiscovery property.
  11. *
  12. * This property is defined here:
  13. * http://tools.ietf.org/html/rfc4918#section-15.8
  14. *
  15. * This property contains all the open locks on a given resource
  16. *
  17. * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
  18. * @author Evert Pot (http://www.rooftopsolutions.nl/)
  19. * @license http://sabre.io/license/ Modified BSD License
  20. */
  21. class LockDiscovery implements XmlSerializable
  22. {
  23. /**
  24. * locks.
  25. *
  26. * @var LockInfo[]
  27. */
  28. public $locks;
  29. /**
  30. * Hides the {DAV:}lockroot element from the response.
  31. *
  32. * It was reported that showing the lockroot in the response can break
  33. * Office 2000 compatibility.
  34. *
  35. * @var bool
  36. */
  37. public static $hideLockRoot = false;
  38. /**
  39. * __construct.
  40. *
  41. * @param LockInfo[] $locks
  42. */
  43. public function __construct($locks)
  44. {
  45. $this->locks = $locks;
  46. }
  47. /**
  48. * The serialize method is called during xml writing.
  49. *
  50. * It should use the $writer argument to encode this object into XML.
  51. *
  52. * Important note: it is not needed to create the parent element. The
  53. * parent element is already created, and we only have to worry about
  54. * attributes, child elements and text (if any).
  55. *
  56. * Important note 2: If you are writing any new elements, you are also
  57. * responsible for closing them.
  58. */
  59. public function xmlSerialize(Writer $writer)
  60. {
  61. foreach ($this->locks as $lock) {
  62. $writer->startElement('{DAV:}activelock');
  63. $writer->startElement('{DAV:}lockscope');
  64. if (LockInfo::SHARED === $lock->scope) {
  65. $writer->writeElement('{DAV:}shared');
  66. } else {
  67. $writer->writeElement('{DAV:}exclusive');
  68. }
  69. $writer->endElement(); // {DAV:}lockscope
  70. $writer->startElement('{DAV:}locktype');
  71. $writer->writeElement('{DAV:}write');
  72. $writer->endElement(); // {DAV:}locktype
  73. if (!self::$hideLockRoot) {
  74. $writer->startElement('{DAV:}lockroot');
  75. $writer->writeElement('{DAV:}href', $writer->contextUri.$lock->uri);
  76. $writer->endElement(); // {DAV:}lockroot
  77. }
  78. $writer->writeElement('{DAV:}depth', (DAV\Server::DEPTH_INFINITY == $lock->depth ? 'infinity' : $lock->depth));
  79. $writer->writeElement('{DAV:}timeout', (LockInfo::TIMEOUT_INFINITE === $lock->timeout ? 'Infinite' : 'Second-'.$lock->timeout));
  80. // optional according to https://tools.ietf.org/html/rfc4918#section-6.5
  81. if (null !== $lock->token && '' !== $lock->token) {
  82. $writer->startElement('{DAV:}locktoken');
  83. $writer->writeElement('{DAV:}href', 'opaquelocktoken:'.$lock->token);
  84. $writer->endElement(); // {DAV:}locktoken
  85. }
  86. if ($lock->owner) {
  87. $writer->writeElement('{DAV:}owner', new XmlFragment($lock->owner));
  88. }
  89. $writer->endElement(); // {DAV:}activelock
  90. }
  91. }
  92. }