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