PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Sabre/DAV/Property/Response.php

https://github.com/KOLANICH/SabreDAV
PHP | 215 lines | 77 code | 50 blank | 88 comment | 9 complexity | 4f450e524df3398085352e8b9ede9f5b MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. namespace Sabre\DAV\Property;
  3. use Sabre\DAV;
  4. /**
  5. * Response property
  6. *
  7. * This class represents the {DAV:}response XML element.
  8. * This is used by the Server class to encode individual items within a multistatus
  9. * response.
  10. *
  11. * @copyright Copyright (C) 2007-2013 fruux GmbH (https://fruux.com/).
  12. * @author Evert Pot (http://evertpot.com/)
  13. * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
  14. */
  15. class Response extends DAV\Property implements IHref {
  16. /**
  17. * Url for the response
  18. *
  19. * @var string
  20. */
  21. protected $href;
  22. /**
  23. * Propertylist, ordered by HTTP status code
  24. *
  25. * @var array
  26. */
  27. protected $responseProperties;
  28. /**
  29. * The HTTP status for an entire response.
  30. *
  31. * This is currently only used in WebDAV-Sync
  32. *
  33. * @var string
  34. */
  35. protected $httpStatus;
  36. /**
  37. * The href argument is a url relative to the root of the server. This
  38. * class will calculate the full path.
  39. *
  40. * The responseProperties argument is a list of properties
  41. * within an array with keys representing HTTP status codes
  42. *
  43. * Besides specific properties, the entire {DAV:}response element may also
  44. * have a http status code.
  45. * In most cases you don't need it.
  46. *
  47. * This is currently used by the Sync extension to indicate that a node is
  48. * deleted.
  49. *
  50. * @param string $href
  51. * @param array $responseProperties
  52. * @param string $httpStatus
  53. */
  54. public function __construct($href, array $responseProperties, $httpStatus = null) {
  55. $this->href = $href;
  56. $this->responseProperties = $responseProperties;
  57. $this->httpStatus = $httpStatus;
  58. }
  59. /**
  60. * Returns the url
  61. *
  62. * @return string
  63. */
  64. public function getHref() {
  65. return $this->href;
  66. }
  67. /**
  68. * Returns the httpStatus value
  69. *
  70. * @return string
  71. */
  72. public function getHttpStatus() {
  73. return $this->httpStatus;
  74. }
  75. /**
  76. * Returns the property list
  77. *
  78. * @return array
  79. */
  80. public function getResponseProperties() {
  81. return $this->responseProperties;
  82. }
  83. /**
  84. * serialize
  85. *
  86. * @param DAV\Server $server
  87. * @param \DOMElement $dom
  88. * @return void
  89. */
  90. public function serialize(DAV\Server $server, \DOMElement $dom) {
  91. $document = $dom->ownerDocument;
  92. $properties = $this->responseProperties;
  93. $xresponse = $document->createElement('d:response');
  94. $dom->appendChild($xresponse);
  95. $uri = DAV\URLUtil::encodePath($this->href);
  96. // Adding the baseurl to the beginning of the url
  97. $uri = $server->getBaseUri() . $uri;
  98. $xresponse->appendChild($document->createElement('d:href',$uri));
  99. if ($this->httpStatus) {
  100. $statusString = $server->httpResponse->getStatusMessage($this->httpStatus);
  101. $xresponse->appendChild($document->createElement('d:status', $statusString));
  102. }
  103. // The properties variable is an array containing properties, grouped by
  104. // HTTP status
  105. foreach($properties as $httpStatus=>$propertyGroup) {
  106. // The 'href' is also in this array, and it's special cased.
  107. // We will ignore it
  108. if ($httpStatus=='href') continue;
  109. // If there are no properties in this group, we can also just carry on
  110. if (!count($propertyGroup)) continue;
  111. $xpropstat = $document->createElement('d:propstat');
  112. $xresponse->appendChild($xpropstat);
  113. $xprop = $document->createElement('d:prop');
  114. $xpropstat->appendChild($xprop);
  115. $nsList = $server->xmlNamespaces;
  116. foreach($propertyGroup as $propertyName=>$propertyValue) {
  117. $propName = null;
  118. preg_match('/^{([^}]*)}(.*)$/',$propertyName,$propName);
  119. // special case for empty namespaces
  120. if ($propName[1]=='') {
  121. $currentProperty = $document->createElement($propName[2]);
  122. $xprop->appendChild($currentProperty);
  123. $currentProperty->setAttribute('xmlns','');
  124. } else {
  125. if (!isset($nsList[$propName[1]])) {
  126. $nsList[$propName[1]] = 'x' . count($nsList);
  127. }
  128. // If the namespace was defined in the top-level xml namespaces, it means
  129. // there was already a namespace declaration, and we don't have to worry about it.
  130. if (isset($server->xmlNamespaces[$propName[1]])) {
  131. $currentProperty = $document->createElement($nsList[$propName[1]] . ':' . $propName[2]);
  132. } else {
  133. $currentProperty = $document->createElementNS($propName[1],$nsList[$propName[1]].':' . $propName[2]);
  134. }
  135. $xprop->appendChild($currentProperty);
  136. }
  137. if (is_scalar($propertyValue)) {
  138. $text = $document->createTextNode($propertyValue);
  139. $currentProperty->appendChild($text);
  140. } elseif ($propertyValue instanceof DAV\PropertyInterface) {
  141. $propertyValue->serialize($server,$currentProperty);
  142. } elseif (!is_null($propertyValue)) {
  143. throw new DAV\Exception('Unknown property value type: ' . gettype($propertyValue) . ' for property: ' . $propertyName);
  144. }
  145. }
  146. $xpropstat->appendChild($document->createElement('d:status',$server->httpResponse->getStatusMessage($httpStatus)));
  147. }
  148. }
  149. /**
  150. * Unserializes the property.
  151. *
  152. * This static method should return a an instance of this object.
  153. *
  154. * @param \DOMElement $prop
  155. * @param array $propertyMap
  156. * @return DAV\IProperty
  157. */
  158. public static function unserialize(\DOMElement $prop, array $propertyMap) {
  159. // Delegating this to the ResponseList property. It does make more
  160. // sense there.
  161. $result = ResponseList::unserialize($prop, $propertyMap);
  162. $result = $result->getResponses();
  163. return $result[0];
  164. }
  165. }