1<?php 2 3namespace Sabre\DAVACL\Xml\Property; 4 5use Sabre\DAV\Browser\HtmlOutput; 6use Sabre\DAV\Browser\HtmlOutputHelper; 7use Sabre\Xml\XmlSerializable; 8use Sabre\Xml\Writer; 9 10/** 11 * SupportedPrivilegeSet property 12 * 13 * This property encodes the {DAV:}supported-privilege-set property, as defined 14 * in rfc3744. Please consult the rfc for details about it's structure. 15 * 16 * This class expects a structure like the one given from 17 * Sabre\DAVACL\Plugin::getSupportedPrivilegeSet as the argument in its 18 * constructor. 19 * 20 * @copyright Copyright (C) 2007-2015 fruux GmbH (https://fruux.com/). 21 * @author Evert Pot (http://evertpot.com/) 22 * @license http://sabre.io/license/ Modified BSD License 23 */ 24class SupportedPrivilegeSet implements XmlSerializable, HtmlOutput { 25 26 /** 27 * privileges 28 * 29 * @var array 30 */ 31 protected $privileges; 32 33 /** 34 * Constructor 35 * 36 * @param array $privileges 37 */ 38 function __construct(array $privileges) { 39 40 $this->privileges = $privileges; 41 42 } 43 44 /** 45 * Returns the privilege value. 46 * 47 * @return array 48 */ 49 function getValue() { 50 51 return $this->privileges; 52 53 } 54 55 /** 56 * The xmlSerialize metod is called during xml writing. 57 * 58 * Use the $writer argument to write its own xml serialization. 59 * 60 * An important note: do _not_ create a parent element. Any element 61 * implementing XmlSerializble should only ever write what's considered 62 * its 'inner xml'. 63 * 64 * The parent of the current element is responsible for writing a 65 * containing element. 66 * 67 * This allows serializers to be re-used for different element names. 68 * 69 * If you are opening new elements, you must also close them again. 70 * 71 * @param Writer $writer 72 * @return void 73 */ 74 function xmlSerialize(Writer $writer) { 75 76 $this->serializePriv($writer, $this->privileges); 77 78 } 79 80 /** 81 * Generate html representation for this value. 82 * 83 * The html output is 100% trusted, and no effort is being made to sanitize 84 * it. It's up to the implementor to sanitize user provided values. 85 * 86 * The output must be in UTF-8. 87 * 88 * The baseUri parameter is a url to the root of the application, and can 89 * be used to construct local links. 90 * 91 * @param HtmlOutputHelper $html 92 * @return string 93 */ 94 function toHtml(HtmlOutputHelper $html) { 95 96 $traverse = function($priv) use (&$traverse, $html) { 97 echo "<li>"; 98 echo $html->xmlName($priv['privilege']); 99 if (isset($priv['abstract']) && $priv['abstract']) { 100 echo " <i>(abstract)</i>"; 101 } 102 if (isset($priv['description'])) { 103 echo " " . $html->h($priv['description']); 104 } 105 if (isset($priv['aggregates'])) { 106 echo "\n<ul>\n"; 107 foreach ($priv['aggregates'] as $subPriv) { 108 $traverse($subPriv); 109 } 110 echo "</ul>"; 111 } 112 echo "</li>\n"; 113 }; 114 115 ob_start(); 116 echo "<ul class=\"tree\">"; 117 $traverse($this->getValue(), ''); 118 echo "</ul>\n"; 119 120 return ob_get_clean(); 121 122 } 123 124 125 126 /** 127 * Serializes a property 128 * 129 * This is a recursive function. 130 * 131 * @param Writer $writer 132 * @param array $privilege 133 * @return void 134 */ 135 private function serializePriv(Writer $writer, $privilege) { 136 137 $writer->startElement('{DAV:}supported-privilege'); 138 139 $writer->startElement('{DAV:}privilege'); 140 $writer->writeElement($privilege['privilege']); 141 $writer->endElement(); // privilege 142 143 if (!empty($privilege['abstract'])) { 144 $writer->writeElement('{DAV:}abstract'); 145 } 146 if (!empty($privilege['description'])) { 147 $writer->writeElement('{DAV:}description', $privilege['description']); 148 } 149 if (isset($privilege['aggregates'])) { 150 foreach ($privilege['aggregates'] as $subPrivilege) { 151 $this->serializePriv($writer, $subPrivilege); 152 } 153 } 154 155 $writer->endElement(); // supported-privilege 156 157 } 158 159} 160