]> git.mxchange.org Git - friendica-addons.git/blob - dav/SabreDAV/lib/Sabre/DAVACL/Property/Acl.php
Set notifications of events; the actual notification routine is not yet implemented
[friendica-addons.git] / dav / SabreDAV / lib / Sabre / DAVACL / Property / Acl.php
1 <?php
2
3 /**
4  * This class represents the {DAV:}acl property
5  *
6  * @package Sabre
7  * @subpackage DAVACL
8  * @copyright Copyright (C) 2007-2012 Rooftop Solutions. All rights reserved.
9  * @author Evert Pot (http://www.rooftopsolutions.nl/)
10  * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
11  */
12 class Sabre_DAVACL_Property_Acl extends Sabre_DAV_Property {
13
14     /**
15      * List of privileges
16      *
17      * @var array
18      */
19     private $privileges;
20
21     /**
22      * Whether or not the server base url is required to be prefixed when
23      * serializing the property.
24      *
25      * @var boolean
26      */
27     private $prefixBaseUrl;
28
29     /**
30      * Constructor
31      *
32      * This object requires a structure similar to the return value from
33      * Sabre_DAVACL_Plugin::getACL().
34      *
35      * Each privilege is a an array with at least a 'privilege' property, and a
36      * 'principal' property. A privilege may have a 'protected' property as
37      * well.
38      *
39      * The prefixBaseUrl should be set to false, if the supplied principal urls
40      * are already full urls. If this is kept to true, the servers base url
41      * will automatically be prefixed.
42      *
43      * @param bool $prefixBaseUrl
44      * @param array $privileges
45      */
46     public function __construct(array $privileges, $prefixBaseUrl = true) {
47
48         $this->privileges = $privileges;
49         $this->prefixBaseUrl = $prefixBaseUrl;
50
51     }
52
53     /**
54      * Returns the list of privileges for this property
55      *
56      * @return array
57      */
58     public function getPrivileges() {
59
60         return $this->privileges;
61
62     }
63
64     /**
65      * Serializes the property into a DOMElement
66      *
67      * @param Sabre_DAV_Server $server
68      * @param DOMElement $node
69      * @return void
70      */
71     public function serialize(Sabre_DAV_Server $server,DOMElement $node) {
72
73         $doc = $node->ownerDocument;
74         foreach($this->privileges as $ace) {
75
76             $this->serializeAce($doc, $node, $ace, $server);
77
78         }
79
80     }
81
82     /**
83      * Unserializes the {DAV:}acl xml element.
84      *
85      * @param DOMElement $dom
86      * @return Sabre_DAVACL_Property_Acl
87      */
88     static public function unserialize(DOMElement $dom) {
89
90         $privileges = array();
91         $xaces = $dom->getElementsByTagNameNS('DAV:','ace');
92         for($ii=0; $ii < $xaces->length; $ii++) {
93
94             $xace = $xaces->item($ii);
95             $principal = $xace->getElementsByTagNameNS('DAV:','principal');
96             if ($principal->length !== 1) {
97                 throw new Sabre_DAV_Exception_BadRequest('Each {DAV:}ace element must have one {DAV:}principal element');
98             }
99             $principal = Sabre_DAVACL_Property_Principal::unserialize($principal->item(0));
100
101             switch($principal->getType()) {
102                 case Sabre_DAVACL_Property_Principal::HREF :
103                     $principal = $principal->getHref();
104                     break;
105                 case Sabre_DAVACL_Property_Principal::AUTHENTICATED :
106                     $principal = '{DAV:}authenticated';
107                     break;
108                 case Sabre_DAVACL_Property_Principal::UNAUTHENTICATED :
109                     $principal = '{DAV:}unauthenticated';
110                     break;
111                 case Sabre_DAVACL_Property_Principal::ALL :
112                     $principal = '{DAV:}all';
113                     break;
114
115             }
116
117             $protected = false;
118
119             if ($xace->getElementsByTagNameNS('DAV:','protected')->length > 0) {
120                 $protected = true;
121             }
122
123             $grants = $xace->getElementsByTagNameNS('DAV:','grant');
124             if ($grants->length < 1) {
125                 throw new Sabre_DAV_Exception_NotImplemented('Every {DAV:}ace element must have a {DAV:}grant element. {DAV:}deny is not yet supported');
126             }
127             $grant = $grants->item(0);
128
129             $xprivs = $grant->getElementsByTagNameNS('DAV:','privilege');
130             for($jj=0; $jj<$xprivs->length; $jj++) {
131
132                 $xpriv = $xprivs->item($jj);
133
134                 $privilegeName = null;
135
136                 for ($kk=0;$kk<$xpriv->childNodes->length;$kk++) {
137
138                     $childNode = $xpriv->childNodes->item($kk);
139                     if ($t = Sabre_DAV_XMLUtil::toClarkNotation($childNode)) {
140                         $privilegeName = $t;
141                         break;
142                     }
143                 }
144                 if (is_null($privilegeName)) {
145                     throw new Sabre_DAV_Exception_BadRequest('{DAV:}privilege elements must have a privilege element contained within them.');
146                 }
147
148                 $privileges[] = array(
149                     'principal' => $principal,
150                     'protected' => $protected,
151                     'privilege' => $privilegeName,
152                 );
153
154             }
155
156         }
157
158         return new self($privileges);
159
160     }
161
162     /**
163      * Serializes a single access control entry.
164      *
165      * @param DOMDocument $doc
166      * @param DOMElement $node
167      * @param array $ace
168      * @param Sabre_DAV_Server $server
169      * @return void
170      */
171     private function serializeAce($doc,$node,$ace, $server) {
172
173         $xace  = $doc->createElementNS('DAV:','d:ace');
174         $node->appendChild($xace);
175
176         $principal = $doc->createElementNS('DAV:','d:principal');
177         $xace->appendChild($principal);
178         switch($ace['principal']) {
179             case '{DAV:}authenticated' :
180                 $principal->appendChild($doc->createElementNS('DAV:','d:authenticated'));
181                 break;
182             case '{DAV:}unauthenticated' :
183                 $principal->appendChild($doc->createElementNS('DAV:','d:unauthenticated'));
184                 break;
185             case '{DAV:}all' :
186                 $principal->appendChild($doc->createElementNS('DAV:','d:all'));
187                 break;
188             default:
189                 $principal->appendChild($doc->createElementNS('DAV:','d:href',($this->prefixBaseUrl?$server->getBaseUri():'') . $ace['principal'] . '/'));
190         }
191
192         $grant = $doc->createElementNS('DAV:','d:grant');
193         $xace->appendChild($grant);
194
195         $privParts = null;
196
197         preg_match('/^{([^}]*)}(.*)$/',$ace['privilege'],$privParts);
198
199         $xprivilege = $doc->createElementNS('DAV:','d:privilege');
200         $grant->appendChild($xprivilege);
201
202         $xprivilege->appendChild($doc->createElementNS($privParts[1],'d:'.$privParts[2]));
203
204         if (isset($ace['protected']) && $ace['protected'])
205             $xace->appendChild($doc->createElement('d:protected'));
206
207     }
208
209 }