xref: /plugin/davcal/vendor/sabre/dav/lib/DAVACL/Xml/Property/Principal.php (revision a1a3b6794e0e143a4a8b51d3185ce2d339be61ab)
1*a1a3b679SAndreas Boehler<?php
2*a1a3b679SAndreas Boehler
3*a1a3b679SAndreas Boehlernamespace Sabre\DAVACL\Xml\Property;
4*a1a3b679SAndreas Boehler
5*a1a3b679SAndreas Boehleruse Sabre\DAV;
6*a1a3b679SAndreas Boehleruse Sabre\DAV\Browser\HtmlOutputHelper;
7*a1a3b679SAndreas Boehleruse Sabre\DAV\Exception\BadRequest;
8*a1a3b679SAndreas Boehleruse Sabre\Xml\Reader;
9*a1a3b679SAndreas Boehleruse Sabre\Xml\Writer;
10*a1a3b679SAndreas Boehler
11*a1a3b679SAndreas Boehler/**
12*a1a3b679SAndreas Boehler * Principal property
13*a1a3b679SAndreas Boehler *
14*a1a3b679SAndreas Boehler * The principal property represents a principal from RFC3744 (ACL).
15*a1a3b679SAndreas Boehler * The property can be used to specify a principal or pseudo principals.
16*a1a3b679SAndreas Boehler *
17*a1a3b679SAndreas Boehler * @copyright Copyright (C) 2007-2015 fruux GmbH (https://fruux.com/).
18*a1a3b679SAndreas Boehler * @author Evert Pot (http://evertpot.com/)
19*a1a3b679SAndreas Boehler * @license http://sabre.io/license/ Modified BSD License
20*a1a3b679SAndreas Boehler */
21*a1a3b679SAndreas Boehlerclass Principal extends DAV\Xml\Property\Href {
22*a1a3b679SAndreas Boehler
23*a1a3b679SAndreas Boehler    /**
24*a1a3b679SAndreas Boehler     * To specify a not-logged-in user, use the UNAUTHENTICATED principal
25*a1a3b679SAndreas Boehler     */
26*a1a3b679SAndreas Boehler    const UNAUTHENTICATED = 1;
27*a1a3b679SAndreas Boehler
28*a1a3b679SAndreas Boehler    /**
29*a1a3b679SAndreas Boehler     * To specify any principal that is logged in, use AUTHENTICATED
30*a1a3b679SAndreas Boehler     */
31*a1a3b679SAndreas Boehler    const AUTHENTICATED = 2;
32*a1a3b679SAndreas Boehler
33*a1a3b679SAndreas Boehler    /**
34*a1a3b679SAndreas Boehler     * Specific principals can be specified with the HREF
35*a1a3b679SAndreas Boehler     */
36*a1a3b679SAndreas Boehler    const HREF = 3;
37*a1a3b679SAndreas Boehler
38*a1a3b679SAndreas Boehler    /**
39*a1a3b679SAndreas Boehler     * Everybody, basically
40*a1a3b679SAndreas Boehler     */
41*a1a3b679SAndreas Boehler    const ALL = 4;
42*a1a3b679SAndreas Boehler
43*a1a3b679SAndreas Boehler    /**
44*a1a3b679SAndreas Boehler     * Principal-type
45*a1a3b679SAndreas Boehler     *
46*a1a3b679SAndreas Boehler     * Must be one of the UNAUTHENTICATED, AUTHENTICATED or HREF constants.
47*a1a3b679SAndreas Boehler     *
48*a1a3b679SAndreas Boehler     * @var int
49*a1a3b679SAndreas Boehler     */
50*a1a3b679SAndreas Boehler    protected $type;
51*a1a3b679SAndreas Boehler
52*a1a3b679SAndreas Boehler    /**
53*a1a3b679SAndreas Boehler     * Creates the property.
54*a1a3b679SAndreas Boehler     *
55*a1a3b679SAndreas Boehler     * The 'type' argument must be one of the type constants defined in this class.
56*a1a3b679SAndreas Boehler     *
57*a1a3b679SAndreas Boehler     * 'href' is only required for the HREF type.
58*a1a3b679SAndreas Boehler     *
59*a1a3b679SAndreas Boehler     * @param int $type
60*a1a3b679SAndreas Boehler     * @param string|null $href
61*a1a3b679SAndreas Boehler     */
62*a1a3b679SAndreas Boehler    function __construct($type, $href = null) {
63*a1a3b679SAndreas Boehler
64*a1a3b679SAndreas Boehler        $this->type = $type;
65*a1a3b679SAndreas Boehler        if ($type === self::HREF && is_null($href)) {
66*a1a3b679SAndreas Boehler            throw new DAV\Exception('The href argument must be specified for the HREF principal type.');
67*a1a3b679SAndreas Boehler        }
68*a1a3b679SAndreas Boehler        if ($href) {
69*a1a3b679SAndreas Boehler            $href = rtrim($href, '/') . '/';
70*a1a3b679SAndreas Boehler            parent::__construct($href);
71*a1a3b679SAndreas Boehler        }
72*a1a3b679SAndreas Boehler
73*a1a3b679SAndreas Boehler    }
74*a1a3b679SAndreas Boehler
75*a1a3b679SAndreas Boehler    /**
76*a1a3b679SAndreas Boehler     * Returns the principal type
77*a1a3b679SAndreas Boehler     *
78*a1a3b679SAndreas Boehler     * @return int
79*a1a3b679SAndreas Boehler     */
80*a1a3b679SAndreas Boehler    function getType() {
81*a1a3b679SAndreas Boehler
82*a1a3b679SAndreas Boehler        return $this->type;
83*a1a3b679SAndreas Boehler
84*a1a3b679SAndreas Boehler    }
85*a1a3b679SAndreas Boehler
86*a1a3b679SAndreas Boehler
87*a1a3b679SAndreas Boehler    /**
88*a1a3b679SAndreas Boehler     * The xmlSerialize metod is called during xml writing.
89*a1a3b679SAndreas Boehler     *
90*a1a3b679SAndreas Boehler     * Use the $writer argument to write its own xml serialization.
91*a1a3b679SAndreas Boehler     *
92*a1a3b679SAndreas Boehler     * An important note: do _not_ create a parent element. Any element
93*a1a3b679SAndreas Boehler     * implementing XmlSerializble should only ever write what's considered
94*a1a3b679SAndreas Boehler     * its 'inner xml'.
95*a1a3b679SAndreas Boehler     *
96*a1a3b679SAndreas Boehler     * The parent of the current element is responsible for writing a
97*a1a3b679SAndreas Boehler     * containing element.
98*a1a3b679SAndreas Boehler     *
99*a1a3b679SAndreas Boehler     * This allows serializers to be re-used for different element names.
100*a1a3b679SAndreas Boehler     *
101*a1a3b679SAndreas Boehler     * If you are opening new elements, you must also close them again.
102*a1a3b679SAndreas Boehler     *
103*a1a3b679SAndreas Boehler     * @param Writer $writer
104*a1a3b679SAndreas Boehler     * @return void
105*a1a3b679SAndreas Boehler     */
106*a1a3b679SAndreas Boehler    function xmlSerialize(Writer $writer) {
107*a1a3b679SAndreas Boehler
108*a1a3b679SAndreas Boehler        switch ($this->type) {
109*a1a3b679SAndreas Boehler
110*a1a3b679SAndreas Boehler            case self::UNAUTHENTICATED :
111*a1a3b679SAndreas Boehler                $writer->writeElement('{DAV:}unauthenticated');
112*a1a3b679SAndreas Boehler                break;
113*a1a3b679SAndreas Boehler            case self::AUTHENTICATED :
114*a1a3b679SAndreas Boehler                $writer->writeElement('{DAV:}authenticated');
115*a1a3b679SAndreas Boehler                break;
116*a1a3b679SAndreas Boehler            case self::HREF :
117*a1a3b679SAndreas Boehler                parent::xmlSerialize($writer);
118*a1a3b679SAndreas Boehler                break;
119*a1a3b679SAndreas Boehler            case self::ALL :
120*a1a3b679SAndreas Boehler                $writer->writeElement('{DAV:}all');
121*a1a3b679SAndreas Boehler                break;
122*a1a3b679SAndreas Boehler        }
123*a1a3b679SAndreas Boehler
124*a1a3b679SAndreas Boehler    }
125*a1a3b679SAndreas Boehler
126*a1a3b679SAndreas Boehler    /**
127*a1a3b679SAndreas Boehler     * Generate html representation for this value.
128*a1a3b679SAndreas Boehler     *
129*a1a3b679SAndreas Boehler     * The html output is 100% trusted, and no effort is being made to sanitize
130*a1a3b679SAndreas Boehler     * it. It's up to the implementor to sanitize user provided values.
131*a1a3b679SAndreas Boehler     *
132*a1a3b679SAndreas Boehler     * The output must be in UTF-8.
133*a1a3b679SAndreas Boehler     *
134*a1a3b679SAndreas Boehler     * The baseUri parameter is a url to the root of the application, and can
135*a1a3b679SAndreas Boehler     * be used to construct local links.
136*a1a3b679SAndreas Boehler     *
137*a1a3b679SAndreas Boehler     * @param HtmlOutputHelper $html
138*a1a3b679SAndreas Boehler     * @return string
139*a1a3b679SAndreas Boehler     */
140*a1a3b679SAndreas Boehler    function toHtml(HtmlOutputHelper $html) {
141*a1a3b679SAndreas Boehler
142*a1a3b679SAndreas Boehler        switch ($this->type) {
143*a1a3b679SAndreas Boehler
144*a1a3b679SAndreas Boehler            case self::UNAUTHENTICATED :
145*a1a3b679SAndreas Boehler                return '<em>unauthenticated</em>';
146*a1a3b679SAndreas Boehler            case self::AUTHENTICATED :
147*a1a3b679SAndreas Boehler                return '<em>authenticated</em>';
148*a1a3b679SAndreas Boehler            case self::HREF :
149*a1a3b679SAndreas Boehler                return parent::toHtml($html);
150*a1a3b679SAndreas Boehler            case self::ALL :
151*a1a3b679SAndreas Boehler                return '<em>all</em>';
152*a1a3b679SAndreas Boehler        }
153*a1a3b679SAndreas Boehler
154*a1a3b679SAndreas Boehler    }
155*a1a3b679SAndreas Boehler
156*a1a3b679SAndreas Boehler    /**
157*a1a3b679SAndreas Boehler     * The deserialize method is called during xml parsing.
158*a1a3b679SAndreas Boehler     *
159*a1a3b679SAndreas Boehler     * This method is called staticly, this is because in theory this method
160*a1a3b679SAndreas Boehler     * may be used as a type of constructor, or factory method.
161*a1a3b679SAndreas Boehler     *
162*a1a3b679SAndreas Boehler     * Often you want to return an instance of the current class, but you are
163*a1a3b679SAndreas Boehler     * free to return other data as well.
164*a1a3b679SAndreas Boehler     *
165*a1a3b679SAndreas Boehler     * Important note 2: You are responsible for advancing the reader to the
166*a1a3b679SAndreas Boehler     * next element. Not doing anything will result in a never-ending loop.
167*a1a3b679SAndreas Boehler     *
168*a1a3b679SAndreas Boehler     * If you just want to skip parsing for this element altogether, you can
169*a1a3b679SAndreas Boehler     * just call $reader->next();
170*a1a3b679SAndreas Boehler     *
171*a1a3b679SAndreas Boehler     * $reader->parseInnerTree() will parse the entire sub-tree, and advance to
172*a1a3b679SAndreas Boehler     * the next element.
173*a1a3b679SAndreas Boehler     *
174*a1a3b679SAndreas Boehler     * @param Reader $reader
175*a1a3b679SAndreas Boehler     * @return mixed
176*a1a3b679SAndreas Boehler     */
177*a1a3b679SAndreas Boehler    static function xmlDeserialize(Reader $reader) {
178*a1a3b679SAndreas Boehler
179*a1a3b679SAndreas Boehler        $tree = $reader->parseInnerTree()[0];
180*a1a3b679SAndreas Boehler
181*a1a3b679SAndreas Boehler        switch ($tree['name']) {
182*a1a3b679SAndreas Boehler            case '{DAV:}unauthenticated' :
183*a1a3b679SAndreas Boehler                return new self(self::UNAUTHENTICATED);
184*a1a3b679SAndreas Boehler            case '{DAV:}authenticated' :
185*a1a3b679SAndreas Boehler                return new self(self::AUTHENTICATED);
186*a1a3b679SAndreas Boehler            case '{DAV:}href':
187*a1a3b679SAndreas Boehler                return new self(self::HREF, $tree['value']);
188*a1a3b679SAndreas Boehler            case '{DAV:}all':
189*a1a3b679SAndreas Boehler                return new self(self::ALL);
190*a1a3b679SAndreas Boehler            default :
191*a1a3b679SAndreas Boehler                throw new BadRequest('Unknown or unsupported principal type: ' . $tree['name']);
192*a1a3b679SAndreas Boehler        }
193*a1a3b679SAndreas Boehler
194*a1a3b679SAndreas Boehler    }
195*a1a3b679SAndreas Boehler
196*a1a3b679SAndreas Boehler}
197