]> git.mxchange.org Git - friendica-addons.git/blob - dav/SabreDAV/lib/Sabre/DAV/Property/Response.php
Merge remote branch 'friendica/master'
[friendica-addons.git] / dav / SabreDAV / lib / Sabre / DAV / Property / Response.php
1 <?php
2
3 /**
4  * Response property
5  *
6  * This class represents the {DAV:}response XML element.
7  * This is used by the Server class to encode individual items within a multistatus
8  * response.
9  *
10  * @package Sabre
11  * @subpackage DAV
12  * @copyright Copyright (C) 2007-2012 Rooftop Solutions. All rights reserved.
13  * @author Evert Pot (http://www.rooftopsolutions.nl/)
14  * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
15  */
16 class Sabre_DAV_Property_Response extends Sabre_DAV_Property implements Sabre_DAV_Property_IHref {
17
18     /**
19      * Url for the response
20      *
21      * @var string
22      */
23     private $href;
24
25     /**
26      * Propertylist, ordered by HTTP status code
27      *
28      * @var array
29      */
30     private $responseProperties;
31
32     /**
33      * The responseProperties argument is a list of properties
34      * within an array with keys representing HTTP status codes
35      *
36      * @param string $href
37      * @param array $responseProperties
38      */
39     public function __construct($href, array $responseProperties) {
40
41         $this->href = $href;
42         $this->responseProperties = $responseProperties;
43
44     }
45
46     /**
47      * Returns the url
48      *
49      * @return string
50      */
51     public function getHref() {
52
53         return $this->href;
54
55     }
56
57     /**
58      * Returns the property list
59      *
60      * @return array
61      */
62     public function getResponseProperties() {
63
64         return $this->responseProperties;
65
66     }
67
68     /**
69      * serialize
70      *
71      * @param Sabre_DAV_Server $server
72      * @param DOMElement $dom
73      * @return void
74      */
75     public function serialize(Sabre_DAV_Server $server, DOMElement $dom) {
76
77         $document = $dom->ownerDocument;
78         $properties = $this->responseProperties;
79
80         $xresponse = $document->createElement('d:response');
81         $dom->appendChild($xresponse);
82
83         $uri = Sabre_DAV_URLUtil::encodePath($this->href);
84
85         // Adding the baseurl to the beginning of the url
86         $uri = $server->getBaseUri() . $uri;
87
88         $xresponse->appendChild($document->createElement('d:href',$uri));
89
90         // The properties variable is an array containing properties, grouped by
91         // HTTP status
92         foreach($properties as $httpStatus=>$propertyGroup) {
93
94             // The 'href' is also in this array, and it's special cased.
95             // We will ignore it
96             if ($httpStatus=='href') continue;
97
98             // If there are no properties in this group, we can also just carry on
99             if (!count($propertyGroup)) continue;
100
101             $xpropstat = $document->createElement('d:propstat');
102             $xresponse->appendChild($xpropstat);
103
104             $xprop = $document->createElement('d:prop');
105             $xpropstat->appendChild($xprop);
106
107             $nsList = $server->xmlNamespaces;
108
109             foreach($propertyGroup as $propertyName=>$propertyValue) {
110
111                 $propName = null;
112                 preg_match('/^{([^}]*)}(.*)$/',$propertyName,$propName);
113
114                 // special case for empty namespaces
115                 if ($propName[1]=='') {
116
117                     $currentProperty = $document->createElement($propName[2]);
118                     $xprop->appendChild($currentProperty);
119                     $currentProperty->setAttribute('xmlns','');
120
121                 } else {
122
123                     if (!isset($nsList[$propName[1]])) {
124                         $nsList[$propName[1]] = 'x' . count($nsList);
125                     }
126
127                     // If the namespace was defined in the top-level xml namespaces, it means
128                     // there was already a namespace declaration, and we don't have to worry about it.
129                     if (isset($server->xmlNamespaces[$propName[1]])) {
130                         $currentProperty = $document->createElement($nsList[$propName[1]] . ':' . $propName[2]);
131                     } else {
132                         $currentProperty = $document->createElementNS($propName[1],$nsList[$propName[1]].':' . $propName[2]);
133                     }
134                     $xprop->appendChild($currentProperty);
135
136                 }
137
138                 if (is_scalar($propertyValue)) {
139                     $text = $document->createTextNode($propertyValue);
140                     $currentProperty->appendChild($text);
141                 } elseif ($propertyValue instanceof Sabre_DAV_PropertyInterface) {
142                     $propertyValue->serialize($server,$currentProperty);
143                 } elseif (!is_null($propertyValue)) {
144                     throw new Sabre_DAV_Exception('Unknown property value type: ' . gettype($propertyValue) . ' for property: ' . $propertyName);
145                 }
146
147             }
148
149             $xpropstat->appendChild($document->createElement('d:status',$server->httpResponse->getStatusMessage($httpStatus)));
150
151         }
152
153     }
154
155 }