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