Acl.php 6.4 KB

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