]> git.mxchange.org Git - friendica-addons.git/blob - dav/SabreDAV/lib/Sabre/DAVACL/Property/Principal.php
Initial Release of the calendar plugin
[friendica-addons.git] / dav / SabreDAV / lib / Sabre / DAVACL / Property / Principal.php
1 <?php
2
3 /**
4  * Principal property
5  *
6  * The principal property represents a principal from RFC3744 (ACL).
7  * The property can be used to specify a principal or pseudo principals.
8  *
9  * @package Sabre
10  * @subpackage DAVACL
11  * @copyright Copyright (C) 2007-2012 Rooftop Solutions. All rights reserved.
12  * @author Evert Pot (http://www.rooftopsolutions.nl/)
13  * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
14  */
15 class Sabre_DAVACL_Property_Principal extends Sabre_DAV_Property implements Sabre_DAV_Property_IHref {
16
17     /**
18      * To specify a not-logged-in user, use the UNAUTHENTICATED principal
19      */
20     const UNAUTHENTICATED = 1;
21
22     /**
23      * To specify any principal that is logged in, use AUTHENTICATED
24      */
25     const AUTHENTICATED = 2;
26
27     /**
28      * Specific principals can be specified with the HREF
29      */
30     const HREF = 3;
31
32     /**
33      * Everybody, basically
34      */
35     const ALL = 4;
36
37     /**
38      * Principal-type
39      *
40      * Must be one of the UNAUTHENTICATED, AUTHENTICATED or HREF constants.
41      *
42      * @var int
43      */
44     private $type;
45
46     /**
47      * Url to principal
48      *
49      * This value is only used for the HREF principal type.
50      *
51      * @var string
52      */
53     private $href;
54
55     /**
56      * Creates the property.
57      *
58      * The 'type' argument must be one of the type constants defined in this class.
59      *
60      * 'href' is only required for the HREF type.
61      *
62      * @param int $type
63      * @param string|null $href
64      */
65     public function __construct($type, $href = null) {
66
67         $this->type = $type;
68
69         if ($type===self::HREF && is_null($href)) {
70             throw new Sabre_DAV_Exception('The href argument must be specified for the HREF principal type.');
71         }
72         $this->href = $href;
73
74     }
75
76     /**
77      * Returns the principal type
78      *
79      * @return int
80      */
81     public function getType() {
82
83         return $this->type;
84
85     }
86
87     /**
88      * Returns the principal uri.
89      *
90      * @return string
91      */
92     public function getHref() {
93
94         return $this->href;
95
96     }
97
98     /**
99      * Serializes the property into a DOMElement.
100      *
101      * @param Sabre_DAV_Server $server
102      * @param DOMElement $node
103      * @return void
104      */
105     public function serialize(Sabre_DAV_Server $server, DOMElement $node) {
106
107         $prefix = $server->xmlNamespaces['DAV:'];
108         switch($this->type) {
109
110             case self::UNAUTHENTICATED :
111                 $node->appendChild(
112                     $node->ownerDocument->createElement($prefix . ':unauthenticated')
113                 );
114                 break;
115             case self::AUTHENTICATED :
116                 $node->appendChild(
117                     $node->ownerDocument->createElement($prefix . ':authenticated')
118                 );
119                 break;
120             case self::HREF :
121                 $href = $node->ownerDocument->createElement($prefix . ':href');
122                 $href->nodeValue = $server->getBaseUri() . $this->href;
123                 $node->appendChild($href);
124                 break;
125
126         }
127
128     }
129
130     /**
131      * Deserializes a DOM element into a property object.
132      *
133      * @param DOMElement $dom
134      * @return Sabre_DAVACL_Property_Principal
135      */
136     static public function unserialize(DOMElement $dom) {
137
138         $parent = $dom->firstChild;
139         while(!Sabre_DAV_XMLUtil::toClarkNotation($parent)) {
140             $parent = $parent->nextSibling;
141         }
142
143         switch(Sabre_DAV_XMLUtil::toClarkNotation($parent)) {
144
145             case '{DAV:}unauthenticated' :
146                 return new self(self::UNAUTHENTICATED);
147             case '{DAV:}authenticated' :
148                 return new self(self::AUTHENTICATED);
149             case '{DAV:}href':
150                 return new self(self::HREF, $parent->textContent);
151             case '{DAV:}all':
152                 return new self(self::ALL);
153             default :
154                 throw new Sabre_DAV_Exception_BadRequest('Unexpected element (' . Sabre_DAV_XMLUtil::toClarkNotation($parent) . '). Could not deserialize');
155
156         }
157
158     }
159
160 }