1<?php
2
3namespace Sabre\DAV\Browser;
4
5use Sabre\Uri;
6use Sabre\Xml\Service as XmlService;
7
8/**
9 * This class provides a few utility functions for easily generating HTML for
10 * the browser plugin.
11 *
12 * @copyright Copyright (C) 2007-2015 fruux GmbH. (https://fruux.com/)
13 * @author Evert Pot (http://evertpot.com/)
14 * @license http://sabre.io/license/ Modified BSD License
15 */
16class HtmlOutputHelper {
17
18    /**
19     * Link to the root of the application.
20     *
21     * @var string
22     */
23    protected $baseUri;
24
25    /**
26     * List of xml namespaces.
27     *
28     * @var array
29     */
30    protected $namespaceMap;
31
32    /**
33     * Creates the object.
34     *
35     * baseUri must point to the root of the application. This will be used to
36     * easily generate links.
37     *
38     * The namespaceMap contains an array with the list of xml namespaces and
39     * their prefixes. WebDAV uses a lot of XML with complex namespaces, so
40     * that can be used to make output a lot shorter.
41     *
42     * @param string $baseUri
43     * @param array $namespaceMap
44     */
45    function __construct($baseUri, array $namespaceMap) {
46
47        $this->baseUri = $baseUri;
48        $this->namespaceMap = $namespaceMap;
49
50    }
51
52    /**
53     * Generates a 'full' url based on a relative one.
54     *
55     * For relative urls, the base of the application is taken as the reference
56     * url, not the 'current url of the current request'.
57     *
58     * Absolute urls are left alone.
59     *
60     * @param string $path
61     * @return string
62     */
63    function fullUrl($path) {
64
65        return Uri\resolve($this->baseUri, $path);
66
67    }
68
69    /**
70     * Escape string for HTML output.
71     *
72     * @param string $input
73     * @return string
74     */
75    function h($input) {
76
77        return htmlspecialchars($input, ENT_COMPAT, 'UTF-8');
78
79    }
80
81    /**
82     * Generates a full <a>-tag.
83     *
84     * Url is automatically expanded. If label is not specified, we re-use the
85     * url.
86     *
87     * @param string $url
88     * @param string $label
89     * @return string
90     */
91    function link($url, $label = null) {
92
93        $url = $this->h($this->fullUrl($url));
94        return '<a href="' . $url . '">' . ($label ? $this->h($label) : $url) . '</a>';
95
96    }
97
98    /**
99     * This method takes an xml element in clark-notation, and turns it into a
100     * shortened version with a prefix, if it was a known namespace.
101     *
102     * @param string $element
103     * @return string
104     */
105    function xmlName($element) {
106
107        list($ns, $localName) = XmlService::parseClarkNotation($element);
108        if (isset($this->namespaceMap[$ns])) {
109            $propName = $this->namespaceMap[$ns] . ':' . $localName;
110        } else {
111            $propName = $element;
112        }
113        return "<span title=\"" . $this->h($element) . "\">" . $this->h($propName) . "</span>";
114
115    }
116
117}
118