HTMLUtils.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. CARD;
  33. // If we don't have any parameter for the bridge, we print a generic form to load it.
  34. if(count($bridgeElement->parameters) == 0) {
  35. $card .= '<form method="GET" action="?">
  36. <input type="hidden" name="action" value="display" />
  37. <input type="hidden" name="bridge" value="' . $bridgeName . '" />' . PHP_EOL;
  38. if ($isActive)
  39. {
  40. $card .= HTMLUtils::getHelperButtonsFormat($formats);
  41. }
  42. else
  43. {
  44. $card .= '<span style="font-weight: bold;">Inactive</span>';
  45. }
  46. $card .= '</form>' . PHP_EOL;
  47. }
  48. foreach($bridgeElement->parameters as $parameterName => $parameter)
  49. {
  50. if(!is_numeric($parameterName)) {
  51. $card .= '<h5>'.$parameterName.'</h5>' . PHP_EOL;
  52. }
  53. $card .= '<form method="GET" action="?">
  54. <input type="hidden" name="action" value="display" />
  55. <input type="hidden" name="bridge" value="' . $bridgeName . '" />' . PHP_EOL;
  56. $parameter = json_decode($parameter, true);
  57. foreach($parameter as $inputEntry) {
  58. $additionalInfoString = "";
  59. if(isset($inputEntry['required'])) {
  60. $additionalInfoString .= " required=\"required\"";
  61. }
  62. if(isset($inputEntry['pattern'])) {
  63. $additionalInfoString .= " pattern=\"".$inputEntry['pattern']."\"";
  64. }
  65. if(!isset($inputEntry['exampleValue'])) $inputEntry['exampleValue'] = "";
  66. $idArg = 'arg-' . urlencode($bridgeName) . '-' . urlencode($parameterName) . '-' . urlencode($inputEntry['identifier']);
  67. $card .= '<label for="' .$idArg. '">' .$inputEntry['name']. ' : </label>' . PHP_EOL;
  68. if(!isset($inputEntry['type']) || $inputEntry['type'] == 'text') {
  69. $card .= '<input '.$additionalInfoString.' id="' . $idArg . '" type="text" value="" placeholder="' . $inputEntry['exampleValue'] . '" name="' . $inputEntry['identifier'] . '" /><br />' . PHP_EOL;
  70. } else if($inputEntry['type'] == 'number') {
  71. $card .= '<input '.$additionalInfoString.' id="' . $idArg . '" type="number" value="" placeholder="' . $inputEntry['exampleValue'] . '" name="' . $inputEntry['identifier'] . '" /><br />' . PHP_EOL;
  72. } else if($inputEntry['type'] == 'list') {
  73. $card .= '<select '.$additionalInfoString.' id="' . $idArg . '" name="' . $inputEntry['identifier'] . '" >';
  74. foreach($inputEntry['values'] as $listValues) {
  75. $card .= "<option $additionalInfoString value='" . $listValues['value'] . "'>" . $listValues['name'] . "</option>";
  76. }
  77. $card .= '</select><br >';
  78. } else if($inputEntry['type'] == 'checkbox') {
  79. $card .= '<input id="' . $idArg . '" type="checkbox" name="' . $inputEntry['identifier'] . '" /><br />' . PHP_EOL;
  80. }
  81. }
  82. if ($isActive)
  83. {
  84. $card .= HTMLUtils::getHelperButtonsFormat($formats);
  85. }
  86. else
  87. {
  88. $card .= '<span style="font-weight: bold;">Inactive</span>';
  89. }
  90. $card .= '</form>' . PHP_EOL;
  91. }
  92. $card .= '<span class="maintainer">'.$bridgeElement->maintainer.'</span>';
  93. $card .= '</section>';
  94. return $card;
  95. }
  96. }
  97. class HTMLSanitizer {
  98. var $tagsToRemove;
  99. var $keptAttributes;
  100. var $onlyKeepText;
  101. const DEFAULT_CLEAR_TAGS = ["script", "iframe"];
  102. const KEPT_ATTRIBUTES = ["title", "href", "src"];
  103. const ONLY_TEXT = null;
  104. function __construct($tags_to_remove = HTMLSanitizer::DEFAULT_CLEAR_TAGS, $kept_attributes = HTMLSanitizer::KEPT_ATTRIBUTES, $only_keep_text = HTMLSanitizer::ONLY_TEXT) {
  105. $this->tagsToRemove = $tags_to_remove;
  106. $this->keptAttributes = $kept_attributes;
  107. $this->onlyKeepText = $only_keep_text;
  108. }
  109. function sanitize($textToSanitize) {
  110. $htmlContent = str_get_html($textToSanitize);
  111. foreach($htmlContent->find('*[!j_ai_pas_trouve_comment_tout_demander]') as $element) {
  112. if(in_array($element->tag, $this->onlyKeepText)) {
  113. $element->outertext = $element->plaintext;
  114. } else if(in_array($element->tag, $this->tagsToRemove)) {
  115. $element->outertext = '';
  116. } else {
  117. foreach($element->getAllAttributes() as $attributeName => $attribute) {
  118. if(!in_array($attributeName, $this->keptAttributes)) $element->removeAttribute($attributeName);
  119. }
  120. }
  121. }
  122. return $htmlContent;
  123. }
  124. public static function defaultImageSrcTo($content, $server) {
  125. foreach($content->find('img') as $image) {
  126. if(strpos($image->src, '/')==0) {
  127. $image->src = $server.$image->src;
  128. }
  129. }
  130. }
  131. }
  132. ?>