HTMLUtils.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. class HTMLUtils {
  3. public static function getHelperButtonFormat($value, $name){
  4. return '<button type="submit" name="format" value="' . $value . '">' . $name . '</button>';
  5. }
  6. public static function getHelperButtonsFormat($formats){
  7. $buttons = '';
  8. foreach( $formats as $name => $infos )
  9. {
  10. if ( isset($infos['name']) )
  11. {
  12. $buttons .= HTMLUtils::getHelperButtonFormat($name, $infos['name']) . PHP_EOL;
  13. }
  14. }
  15. return $buttons;
  16. }
  17. public static function displayBridgeCard($bridgeName, $formats, $isActive = true)
  18. {
  19. $bridgeElement = Bridge::create($bridgeName);
  20. if($bridgeElement == false) {
  21. return "";
  22. }
  23. $bridgeElement->loadMetadatas();
  24. $name = '<a href="'.$bridgeElement->uri.'">'.$bridgeElement->name.'</a>';
  25. $description = $bridgeElement->description;
  26. $card = <<<CARD
  27. <section id="bridge-{$bridgeName}" data-ref="{$bridgeName}">
  28. <h2>{$name}</h2>
  29. <p class="description">
  30. {$description}
  31. </p>
  32. <input type="checkbox" class="showmore-box" id="showmore-{$bridgeName}" />
  33. <label class="showmore" for="showmore-{$bridgeName}">Show more</label>
  34. CARD;
  35. // If we don't have any parameter for the bridge, we print a generic form to load it.
  36. if(count($bridgeElement->parameters) == 0) {
  37. $card .= '<form method="GET" action="?">
  38. <input type="hidden" name="action" value="display" />
  39. <input type="hidden" name="bridge" value="' . $bridgeName . '" />' . PHP_EOL;
  40. if ($isActive)
  41. {
  42. $card .= HTMLUtils::getHelperButtonsFormat($formats);
  43. }
  44. else
  45. {
  46. $card .= '<span style="font-weight: bold;">Inactive</span>';
  47. }
  48. $card .= '</form>' . PHP_EOL;
  49. }
  50. $hasGlobalParameter = array_key_exists("global", $bridgeElement->parameters);
  51. if($hasGlobalParameter) {
  52. $globalParameters = json_decode($bridgeElement->parameters['global'], true);
  53. }
  54. foreach($bridgeElement->parameters as $parameterName => $parameter)
  55. {
  56. $parameter = json_decode($parameter, true);
  57. if(!is_numeric($parameterName) && $parameterName == "global") {
  58. continue;
  59. }
  60. if($hasGlobalParameter) {
  61. $parameter = array_merge($parameter, $globalParameters);
  62. }
  63. if(!is_numeric($parameterName)) {
  64. $card .= '<h5>'.$parameterName.'</h5>' . PHP_EOL;
  65. }
  66. $card .= '<form method="GET" action="?">
  67. <input type="hidden" name="action" value="display" />
  68. <input type="hidden" name="bridge" value="' . $bridgeName . '" />' . PHP_EOL;
  69. foreach($parameter as $inputEntry) {
  70. $additionalInfoString = "";
  71. if(isset($inputEntry['required'])) {
  72. $additionalInfoString .= " required=\"required\"";
  73. }
  74. if(isset($inputEntry['pattern'])) {
  75. $additionalInfoString .= " pattern=\"".$inputEntry['pattern']."\"";
  76. }
  77. if(isset($inputEntry['title'])) {
  78. $additionalInfoString .= " title=\"" .$inputEntry['title']."\"";
  79. }
  80. if(!isset($inputEntry['exampleValue'])) $inputEntry['exampleValue'] = "";
  81. $idArg = 'arg-' . urlencode($bridgeName) . '-' . urlencode($parameterName) . '-' . urlencode($inputEntry['identifier']);
  82. $card .= '<label for="' .$idArg. '">' .$inputEntry['name']. ' : </label>' . PHP_EOL;
  83. if(!isset($inputEntry['type']) || $inputEntry['type'] == 'text') {
  84. $card .= '<input '.$additionalInfoString.' id="' . $idArg . '" type="text" value="" placeholder="' . $inputEntry['exampleValue'] . '" name="' . $inputEntry['identifier'] . '" /><br />' . PHP_EOL;
  85. } else if($inputEntry['type'] == 'number') {
  86. $card .= '<input '.$additionalInfoString.' id="' . $idArg . '" type="number" value="" placeholder="' . $inputEntry['exampleValue'] . '" name="' . $inputEntry['identifier'] . '" /><br />' . PHP_EOL;
  87. } else if($inputEntry['type'] == 'list') {
  88. $card .= '<select '.$additionalInfoString.' id="' . $idArg . '" name="' . $inputEntry['identifier'] . '" >';
  89. foreach($inputEntry['values'] as $listValues) {
  90. $card .= "<option $additionalInfoString value='" . $listValues['value'] . "'>" . $listValues['name'] . "</option>";
  91. }
  92. $card .= '</select><br >';
  93. } else if($inputEntry['type'] == 'checkbox') {
  94. $card .= '<input id="' . $idArg . '" type="checkbox" name="' . $inputEntry['identifier'] . '" /><br />' . PHP_EOL;
  95. }
  96. }
  97. if ($isActive)
  98. {
  99. $card .= HTMLUtils::getHelperButtonsFormat($formats);
  100. }
  101. else
  102. {
  103. $card .= '<span style="font-weight: bold;">Inactive</span>';
  104. }
  105. $card .= '</form>' . PHP_EOL;
  106. }
  107. $card .= '<label class="showless" for="showmore-' . $bridgeName . '">Show less</label>';
  108. $card .= '<p class="maintainer">'.$bridgeElement->maintainer.'</p>';
  109. $card .= '</section>';
  110. return $card;
  111. }
  112. }
  113. class HTMLSanitizer {
  114. var $tagsToRemove;
  115. var $keptAttributes;
  116. var $onlyKeepText;
  117. public static $DEFAULT_CLEAR_TAGS = ["script", "iframe", "input", "form"];
  118. public static $KEPT_ATTRIBUTES = ["title", "href", "src"];
  119. public static $ONLY_TEXT = [];
  120. function __construct($tags_to_remove = null, $kept_attributes = null, $only_keep_text = null) {
  121. $this->tagsToRemove = $tags_to_remove == null ? HTMLSanitizer::$DEFAULT_CLEAR_TAGS : $tags_to_remove;
  122. $this->keptAttributes = $kept_attributes == null ? HTMLSanitizer::$KEPT_ATTRIBUTES : $kept_attributes;
  123. $this->onlyKeepText = $only_keep_text == null ? HTMLSanitizer::$ONLY_TEXT : $only_keep_text;
  124. }
  125. function sanitize($textToSanitize) {
  126. $htmlContent = str_get_html($textToSanitize);
  127. foreach($htmlContent->find('*[!b38fd2b1fe7f4747d6b1c1254ccd055e]') as $element) {
  128. if(in_array($element->tag, $this->onlyKeepText)) {
  129. $element->outertext = $element->plaintext;
  130. } else if(in_array($element->tag, $this->tagsToRemove)) {
  131. $element->outertext = '';
  132. } else {
  133. foreach($element->getAllAttributes() as $attributeName => $attribute) {
  134. if(!in_array($attributeName, $this->keptAttributes)) $element->removeAttribute($attributeName);
  135. }
  136. }
  137. }
  138. return $htmlContent;
  139. }
  140. public static function defaultImageSrcTo($content, $server) {
  141. foreach($content->find('img') as $image) {
  142. if(strpos($image->src, "http") == NULL && strpos($image->src, "//") == NULL && strpos($image->src, "data:") == NULL) {
  143. $image->src = $server.$image->src;
  144. }
  145. }
  146. return $content;
  147. }
  148. }
  149. ?>