HTMLUtils.php 7.7 KB

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