xref: /plugin/davcal/vendor/sabre/dav/lib/DAV/Xml/Element/Response.php (revision a1a3b6794e0e143a4a8b51d3185ce2d339be61ab)
1*a1a3b679SAndreas Boehler<?php
2*a1a3b679SAndreas Boehler
3*a1a3b679SAndreas Boehlernamespace Sabre\DAV\Xml\Element;
4*a1a3b679SAndreas Boehler
5*a1a3b679SAndreas Boehleruse Sabre\Xml\Element;
6*a1a3b679SAndreas Boehleruse Sabre\Xml\Reader;
7*a1a3b679SAndreas Boehleruse Sabre\Xml\Writer;
8*a1a3b679SAndreas Boehler
9*a1a3b679SAndreas Boehler/**
10*a1a3b679SAndreas Boehler * WebDAV {DAV:}response parser
11*a1a3b679SAndreas Boehler *
12*a1a3b679SAndreas Boehler * This class parses the {DAV:}response element, as defined in:
13*a1a3b679SAndreas Boehler *
14*a1a3b679SAndreas Boehler * https://tools.ietf.org/html/rfc4918#section-14.24
15*a1a3b679SAndreas Boehler *
16*a1a3b679SAndreas Boehler * @copyright Copyright (C) 2007-2015 fruux GmbH (https://fruux.com/).
17*a1a3b679SAndreas Boehler * @author Evert Pot (http://www.rooftopsolutions.nl/)
18*a1a3b679SAndreas Boehler * @license http://sabre.io/license/ Modified BSD License
19*a1a3b679SAndreas Boehler */
20*a1a3b679SAndreas Boehlerclass Response implements Element {
21*a1a3b679SAndreas Boehler
22*a1a3b679SAndreas Boehler    /**
23*a1a3b679SAndreas Boehler     * Url for the response
24*a1a3b679SAndreas Boehler     *
25*a1a3b679SAndreas Boehler     * @var string
26*a1a3b679SAndreas Boehler     */
27*a1a3b679SAndreas Boehler    protected $href;
28*a1a3b679SAndreas Boehler
29*a1a3b679SAndreas Boehler    /**
30*a1a3b679SAndreas Boehler     * Propertylist, ordered by HTTP status code
31*a1a3b679SAndreas Boehler     *
32*a1a3b679SAndreas Boehler     * @var array
33*a1a3b679SAndreas Boehler     */
34*a1a3b679SAndreas Boehler    protected $responseProperties;
35*a1a3b679SAndreas Boehler
36*a1a3b679SAndreas Boehler    /**
37*a1a3b679SAndreas Boehler     * The HTTP status for an entire response.
38*a1a3b679SAndreas Boehler     *
39*a1a3b679SAndreas Boehler     * This is currently only used in WebDAV-Sync
40*a1a3b679SAndreas Boehler     *
41*a1a3b679SAndreas Boehler     * @var string
42*a1a3b679SAndreas Boehler     */
43*a1a3b679SAndreas Boehler    protected $httpStatus;
44*a1a3b679SAndreas Boehler
45*a1a3b679SAndreas Boehler    /**
46*a1a3b679SAndreas Boehler     * The href argument is a url relative to the root of the server. This
47*a1a3b679SAndreas Boehler     * class will calculate the full path.
48*a1a3b679SAndreas Boehler     *
49*a1a3b679SAndreas Boehler     * The responseProperties argument is a list of properties
50*a1a3b679SAndreas Boehler     * within an array with keys representing HTTP status codes
51*a1a3b679SAndreas Boehler     *
52*a1a3b679SAndreas Boehler     * Besides specific properties, the entire {DAV:}response element may also
53*a1a3b679SAndreas Boehler     * have a http status code.
54*a1a3b679SAndreas Boehler     * In most cases you don't need it.
55*a1a3b679SAndreas Boehler     *
56*a1a3b679SAndreas Boehler     * This is currently used by the Sync extension to indicate that a node is
57*a1a3b679SAndreas Boehler     * deleted.
58*a1a3b679SAndreas Boehler     *
59*a1a3b679SAndreas Boehler     * @param string $href
60*a1a3b679SAndreas Boehler     * @param array $responseProperties
61*a1a3b679SAndreas Boehler     * @param string $httpStatus
62*a1a3b679SAndreas Boehler     */
63*a1a3b679SAndreas Boehler    function __construct($href, array $responseProperties, $httpStatus = null) {
64*a1a3b679SAndreas Boehler
65*a1a3b679SAndreas Boehler        $this->href = $href;
66*a1a3b679SAndreas Boehler        $this->responseProperties = $responseProperties;
67*a1a3b679SAndreas Boehler        $this->httpStatus = $httpStatus;
68*a1a3b679SAndreas Boehler
69*a1a3b679SAndreas Boehler    }
70*a1a3b679SAndreas Boehler
71*a1a3b679SAndreas Boehler    /**
72*a1a3b679SAndreas Boehler     * Returns the url
73*a1a3b679SAndreas Boehler     *
74*a1a3b679SAndreas Boehler     * @return string
75*a1a3b679SAndreas Boehler     */
76*a1a3b679SAndreas Boehler    function getHref() {
77*a1a3b679SAndreas Boehler
78*a1a3b679SAndreas Boehler        return $this->href;
79*a1a3b679SAndreas Boehler
80*a1a3b679SAndreas Boehler    }
81*a1a3b679SAndreas Boehler
82*a1a3b679SAndreas Boehler    /**
83*a1a3b679SAndreas Boehler     * Returns the httpStatus value
84*a1a3b679SAndreas Boehler     *
85*a1a3b679SAndreas Boehler     * @return string
86*a1a3b679SAndreas Boehler     */
87*a1a3b679SAndreas Boehler    function getHttpStatus() {
88*a1a3b679SAndreas Boehler
89*a1a3b679SAndreas Boehler        return $this->httpStatus;
90*a1a3b679SAndreas Boehler
91*a1a3b679SAndreas Boehler    }
92*a1a3b679SAndreas Boehler
93*a1a3b679SAndreas Boehler    /**
94*a1a3b679SAndreas Boehler     * Returns the property list
95*a1a3b679SAndreas Boehler     *
96*a1a3b679SAndreas Boehler     * @return array
97*a1a3b679SAndreas Boehler     */
98*a1a3b679SAndreas Boehler    function getResponseProperties() {
99*a1a3b679SAndreas Boehler
100*a1a3b679SAndreas Boehler        return $this->responseProperties;
101*a1a3b679SAndreas Boehler
102*a1a3b679SAndreas Boehler    }
103*a1a3b679SAndreas Boehler
104*a1a3b679SAndreas Boehler
105*a1a3b679SAndreas Boehler    /**
106*a1a3b679SAndreas Boehler     * The serialize method is called during xml writing.
107*a1a3b679SAndreas Boehler     *
108*a1a3b679SAndreas Boehler     * It should use the $writer argument to encode this object into XML.
109*a1a3b679SAndreas Boehler     *
110*a1a3b679SAndreas Boehler     * Important note: it is not needed to create the parent element. The
111*a1a3b679SAndreas Boehler     * parent element is already created, and we only have to worry about
112*a1a3b679SAndreas Boehler     * attributes, child elements and text (if any).
113*a1a3b679SAndreas Boehler     *
114*a1a3b679SAndreas Boehler     * Important note 2: If you are writing any new elements, you are also
115*a1a3b679SAndreas Boehler     * responsible for closing them.
116*a1a3b679SAndreas Boehler     *
117*a1a3b679SAndreas Boehler     * @param Writer $writer
118*a1a3b679SAndreas Boehler     * @return void
119*a1a3b679SAndreas Boehler     */
120*a1a3b679SAndreas Boehler    function xmlSerialize(Writer $writer) {
121*a1a3b679SAndreas Boehler
122*a1a3b679SAndreas Boehler        if ($status = $this->getHTTPStatus()) {
123*a1a3b679SAndreas Boehler            $writer->writeElement('{DAV:}status', 'HTTP/1.1 ' . $status . ' ' . \Sabre\HTTP\Response::$statusCodes[$status]);
124*a1a3b679SAndreas Boehler        }
125*a1a3b679SAndreas Boehler        $writer->writeElement('{DAV:}href', $writer->contextUri . $this->getHref());
126*a1a3b679SAndreas Boehler        foreach ($this->getResponseProperties() as $status => $properties) {
127*a1a3b679SAndreas Boehler
128*a1a3b679SAndreas Boehler            // Skipping empty lists
129*a1a3b679SAndreas Boehler            if (!$properties || (!ctype_digit($status) && !is_int($status))) {
130*a1a3b679SAndreas Boehler                continue;
131*a1a3b679SAndreas Boehler            }
132*a1a3b679SAndreas Boehler            $writer->startElement('{DAV:}propstat');
133*a1a3b679SAndreas Boehler            $writer->writeElement('{DAV:}prop', $properties);
134*a1a3b679SAndreas Boehler            $writer->writeElement('{DAV:}status', 'HTTP/1.1 ' . $status . ' ' . \Sabre\HTTP\Response::$statusCodes[$status]);
135*a1a3b679SAndreas Boehler            $writer->endElement(); // {DAV:}propstat
136*a1a3b679SAndreas Boehler
137*a1a3b679SAndreas Boehler        }
138*a1a3b679SAndreas Boehler
139*a1a3b679SAndreas Boehler    }
140*a1a3b679SAndreas Boehler
141*a1a3b679SAndreas Boehler    /**
142*a1a3b679SAndreas Boehler     * The deserialize method is called during xml parsing.
143*a1a3b679SAndreas Boehler     *
144*a1a3b679SAndreas Boehler     * This method is called statictly, this is because in theory this method
145*a1a3b679SAndreas Boehler     * may be used as a type of constructor, or factory method.
146*a1a3b679SAndreas Boehler     *
147*a1a3b679SAndreas Boehler     * Often you want to return an instance of the current class, but you are
148*a1a3b679SAndreas Boehler     * free to return other data as well.
149*a1a3b679SAndreas Boehler     *
150*a1a3b679SAndreas Boehler     * You are responsible for advancing the reader to the next element. Not
151*a1a3b679SAndreas Boehler     * doing anything will result in a never-ending loop.
152*a1a3b679SAndreas Boehler     *
153*a1a3b679SAndreas Boehler     * If you just want to skip parsing for this element altogether, you can
154*a1a3b679SAndreas Boehler     * just call $reader->next();
155*a1a3b679SAndreas Boehler     *
156*a1a3b679SAndreas Boehler     * $reader->parseInnerTree() will parse the entire sub-tree, and advance to
157*a1a3b679SAndreas Boehler     * the next element.
158*a1a3b679SAndreas Boehler     *
159*a1a3b679SAndreas Boehler     * @param Reader $reader
160*a1a3b679SAndreas Boehler     * @return mixed
161*a1a3b679SAndreas Boehler     */
162*a1a3b679SAndreas Boehler    static function xmlDeserialize(Reader $reader) {
163*a1a3b679SAndreas Boehler
164*a1a3b679SAndreas Boehler        $reader->pushContext();
165*a1a3b679SAndreas Boehler
166*a1a3b679SAndreas Boehler        $reader->elementMap['{DAV:}propstat'] = 'Sabre\\Xml\\Element\\KeyValue';
167*a1a3b679SAndreas Boehler
168*a1a3b679SAndreas Boehler        // We are overriding the parser for {DAV:}prop. This deserializer is
169*a1a3b679SAndreas Boehler        // almost identical to the one for Sabre\Xml\Element\KeyValue.
170*a1a3b679SAndreas Boehler        //
171*a1a3b679SAndreas Boehler        // The difference is that if there are any child-elements inside of
172*a1a3b679SAndreas Boehler        // {DAV:}prop, that have no value, normally any deserializers are
173*a1a3b679SAndreas Boehler        // called. But we don't want this, because a singlular element without
174*a1a3b679SAndreas Boehler        // child-elements implies 'no value' in {DAV:}prop, so we want to skip
175*a1a3b679SAndreas Boehler        // deserializers and just set null for those.
176*a1a3b679SAndreas Boehler        $reader->elementMap['{DAV:}prop'] = function(Reader $reader) {
177*a1a3b679SAndreas Boehler
178*a1a3b679SAndreas Boehler            if ($reader->isEmptyElement) {
179*a1a3b679SAndreas Boehler                $reader->next();
180*a1a3b679SAndreas Boehler                return [];
181*a1a3b679SAndreas Boehler            }
182*a1a3b679SAndreas Boehler            $values = [];
183*a1a3b679SAndreas Boehler            $reader->read();
184*a1a3b679SAndreas Boehler            do {
185*a1a3b679SAndreas Boehler                if ($reader->nodeType === Reader::ELEMENT) {
186*a1a3b679SAndreas Boehler                    $clark = $reader->getClark();
187*a1a3b679SAndreas Boehler
188*a1a3b679SAndreas Boehler                    if ($reader->isEmptyElement) {
189*a1a3b679SAndreas Boehler                        $values[$clark] = null;
190*a1a3b679SAndreas Boehler                        $reader->next();
191*a1a3b679SAndreas Boehler                    } else {
192*a1a3b679SAndreas Boehler                        $values[$clark] = $reader->parseCurrentElement()['value'];
193*a1a3b679SAndreas Boehler                    }
194*a1a3b679SAndreas Boehler                } else {
195*a1a3b679SAndreas Boehler                    $reader->read();
196*a1a3b679SAndreas Boehler                }
197*a1a3b679SAndreas Boehler            } while ($reader->nodeType !== Reader::END_ELEMENT);
198*a1a3b679SAndreas Boehler            $reader->read();
199*a1a3b679SAndreas Boehler            return $values;
200*a1a3b679SAndreas Boehler
201*a1a3b679SAndreas Boehler        };
202*a1a3b679SAndreas Boehler        $elems = $reader->parseInnerTree();
203*a1a3b679SAndreas Boehler        $reader->popContext();
204*a1a3b679SAndreas Boehler
205*a1a3b679SAndreas Boehler        $href = null;
206*a1a3b679SAndreas Boehler        $propertyLists = [];
207*a1a3b679SAndreas Boehler        $statusCode = null;
208*a1a3b679SAndreas Boehler
209*a1a3b679SAndreas Boehler        foreach ($elems as $elem) {
210*a1a3b679SAndreas Boehler
211*a1a3b679SAndreas Boehler            switch ($elem['name']) {
212*a1a3b679SAndreas Boehler
213*a1a3b679SAndreas Boehler                case '{DAV:}href' :
214*a1a3b679SAndreas Boehler                    $href = $elem['value'];
215*a1a3b679SAndreas Boehler                    break;
216*a1a3b679SAndreas Boehler                case '{DAV:}propstat' :
217*a1a3b679SAndreas Boehler                    $status = $elem['value']['{DAV:}status'];
218*a1a3b679SAndreas Boehler                    list(, $status, ) = explode(' ', $status, 3);
219*a1a3b679SAndreas Boehler                    $properties = isset($elem['value']['{DAV:}prop']) ? $elem['value']['{DAV:}prop'] : [];
220*a1a3b679SAndreas Boehler                    $propertyLists[$status] = $properties;
221*a1a3b679SAndreas Boehler                    break;
222*a1a3b679SAndreas Boehler                case '{DAV:}status' :
223*a1a3b679SAndreas Boehler                    list(, $statusCode, ) = explode(' ', $elem['value'], 3);
224*a1a3b679SAndreas Boehler                    break;
225*a1a3b679SAndreas Boehler
226*a1a3b679SAndreas Boehler            }
227*a1a3b679SAndreas Boehler
228*a1a3b679SAndreas Boehler        }
229*a1a3b679SAndreas Boehler
230*a1a3b679SAndreas Boehler        return new self($href, $propertyLists, $statusCode);
231*a1a3b679SAndreas Boehler
232*a1a3b679SAndreas Boehler    }
233*a1a3b679SAndreas Boehler
234*a1a3b679SAndreas Boehler}
235