Blame view

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