1<?php
2
3namespace Sabre\DAVACL\Xml\Property;
4
5use Sabre\DAV\Browser\HtmlOutput;
6use Sabre\DAV\Browser\HtmlOutputHelper;
7use Sabre\Xml\Writer;
8use Sabre\Xml\XmlSerializable;
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) 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 method 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 XmlSerializable 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, '{DAV:}all', ['aggregates' => $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($privName, $priv) use (&$traverse, $html) {
97            echo "<li>";
98            echo $html->xmlName($privName);
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 $subPrivName => $subPriv) {
108                    $traverse($subPrivName, $subPriv);
109                }
110                echo "</ul>";
111            }
112            echo "</li>\n";
113        };
114
115        ob_start();
116        echo "<ul class=\"tree\">";
117        $traverse('{DAV:}all', ['aggregates' => $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 string $privName
133     * @param array $privilege
134     * @return void
135     */
136    private function serializePriv(Writer $writer, $privName, $privilege) {
137
138        $writer->startElement('{DAV:}supported-privilege');
139
140        $writer->startElement('{DAV:}privilege');
141        $writer->writeElement($privName);
142        $writer->endElement(); // privilege
143
144        if (!empty($privilege['abstract'])) {
145            $writer->writeElement('{DAV:}abstract');
146        }
147        if (!empty($privilege['description'])) {
148            $writer->writeElement('{DAV:}description', $privilege['description']);
149        }
150        if (isset($privilege['aggregates'])) {
151            foreach ($privilege['aggregates'] as $subPrivName => $subPrivilege) {
152                $this->serializePriv($writer, $subPrivName, $subPrivilege);
153            }
154        }
155
156        $writer->endElement(); // supported-privilege
157
158    }
159
160}
161