HtmlOutputHelper.php 2.8 KB

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