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