HTMLUtils.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. <?php
  2. class HTMLUtils {
  3. public static function displayBridgeCard($bridgeName, $formats, $isActive = true){
  4. $bridgeElement = Bridge::create($bridgeName);
  5. $bridgeClass = $bridgeName . 'Bridge';
  6. if($bridgeElement == false)
  7. return "";
  8. $name = '<a href="' . $bridgeClass::URI . '">' . $bridgeClass::NAME . '</a>';
  9. $description = $bridgeClass::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($bridgeClass::PARAMETERS) == 0){
  21. $card .= HTMLUtils::getFormHeader($bridgeName);
  22. if($isActive){
  23. if(defined('PROXY_URL') && PROXY_BYBRIDGE){
  24. $idArg = 'arg-'
  25. . urlencode($bridgeName)
  26. . '-'
  27. . urlencode('proxyoff')
  28. . '-'
  29. . urlencode('_noproxy');
  30. $card .= '<input id="'
  31. . $idArg
  32. . '" type="checkbox" name="_noproxy" />'
  33. . PHP_EOL;
  34. $card .= '<label for="'
  35. . $idArg
  36. . '">Disable proxy ('
  37. . ((defined('PROXY_NAME') && PROXY_NAME) ? PROXY_NAME : PROXY_URL)
  38. . ')</label><br />'
  39. . PHP_EOL;
  40. }
  41. $card .= HTMLUtils::getHelperButtonsFormat($formats);
  42. } else {
  43. $card .= '<span style="font-weight: bold;">Inactive</span>';
  44. }
  45. $card .= '</form>' . PHP_EOL;
  46. }
  47. $hasGlobalParameter = array_key_exists('global', $bridgeClass::PARAMETERS);
  48. if($hasGlobalParameter){
  49. $globalParameters = $bridgeClass::PARAMETERS['global'];
  50. }
  51. foreach($bridgeClass::PARAMETERS as $parameterName => $parameter){
  52. if(!is_numeric($parameterName) && $parameterName == 'global')
  53. continue;
  54. if($hasGlobalParameter)
  55. $parameter = array_merge($parameter, $globalParameters);
  56. if(!is_numeric($parameterName))
  57. $card .= '<h5>' . $parameterName . '</h5>' . PHP_EOL;
  58. $card .= HTMLUtils::getFormHeader($bridgeName);
  59. foreach($parameter as $id=>$inputEntry){
  60. $additionalInfoString = '';
  61. if(isset($inputEntry['required']) && $inputEntry['required'] === true)
  62. $additionalInfoString .= ' required';
  63. if(isset($inputEntry['pattern']))
  64. $additionalInfoString .= ' pattern="' . $inputEntry['pattern'] . '"';
  65. if(isset($inputEntry['title']))
  66. $additionalInfoString .= ' title="' . $inputEntry['title'] . '"';
  67. if(!isset($inputEntry['exampleValue']))
  68. $inputEntry['exampleValue'] = '';
  69. if(!isset($inputEntry['defaultValue']))
  70. $inputEntry['defaultValue'] = '';
  71. $idArg = 'arg-'
  72. . urlencode($bridgeName)
  73. . '-'
  74. . urlencode($parameterName)
  75. . '-'
  76. . urlencode($id);
  77. $card .= '<label for="'
  78. . $idArg
  79. . '">'
  80. . $inputEntry['name']
  81. . ' : </label>'
  82. . PHP_EOL;
  83. if(!isset($inputEntry['type']) || $inputEntry['type'] == 'text'){
  84. $card .= '<input '
  85. . $additionalInfoString
  86. . ' id="'
  87. . $idArg
  88. . '" type="text" value="'
  89. . $inputEntry['defaultValue']
  90. . '" placeholder="'
  91. . $inputEntry['exampleValue']
  92. . '" name="'
  93. . $id
  94. . '" /><br />'
  95. . PHP_EOL;
  96. } elseif($inputEntry['type'] == 'number'){
  97. $card .= '<input '
  98. . $additionalInfoString
  99. . ' id="'
  100. . $idArg
  101. . '" type="number" value="'
  102. . $inputEntry['defaultValue']
  103. . '" placeholder="'
  104. . $inputEntry['exampleValue']
  105. . '" name="'
  106. . $id
  107. . '" /><br />'
  108. . PHP_EOL;
  109. } else if($inputEntry['type'] == 'list'){
  110. $card .= '<select '
  111. . $additionalInfoString
  112. . ' id="'
  113. . $idArg
  114. . '" name="'
  115. . $id
  116. . '" >';
  117. foreach($inputEntry['values'] as $name => $value){
  118. if(is_array($value)){
  119. $card .= '<optgroup label="' . htmlentities($name) . '">';
  120. foreach($value as $subname => $subvalue){
  121. if($inputEntry['defaultValue'] === $subname
  122. || $inputEntry['defaultValue'] === $subvalue){
  123. $card .= '<option value="'
  124. . $subvalue
  125. . '" selected>'
  126. . $subname
  127. . '</option>';
  128. } else {
  129. $card .= '<option value="'
  130. . $subvalue
  131. . '">'
  132. . $subname
  133. . '</option>';
  134. }
  135. }
  136. $card .= '</optgroup>';
  137. } else {
  138. if($inputEntry['defaultValue'] === $name
  139. || $inputEntry['defaultValue'] === $value){
  140. $card .= '<option value="'
  141. . $value
  142. . '" selected>'
  143. . $name
  144. . '</option>';
  145. } else {
  146. $card .= '<option value="'
  147. . $value
  148. . '">'
  149. . $name
  150. . '</option>';
  151. }
  152. }
  153. }
  154. $card .= '</select><br >';
  155. } elseif($inputEntry['type'] == 'checkbox'){
  156. if($inputEntry['defaultValue'] === 'checked')
  157. $card .= '<input '
  158. . $additionalInfoString
  159. . ' id="'
  160. . $idArg
  161. . '" type="checkbox" name="'
  162. . $id
  163. . '" checked /><br />'
  164. . PHP_EOL;
  165. else
  166. $card .= '<input '
  167. . $additionalInfoString
  168. . ' id="'
  169. . $idArg
  170. . '" type="checkbox" name="'
  171. . $id
  172. . '" /><br />'
  173. . PHP_EOL;
  174. }
  175. }
  176. if($isActive){
  177. if(defined('PROXY_URL') && PROXY_BYBRIDGE){
  178. $idArg = 'arg-'
  179. . urlencode($bridgeName)
  180. . '-'
  181. . urlencode('proxyoff')
  182. . '-'
  183. . urlencode('_noproxy');
  184. $card .= '<input id="'
  185. . $idArg
  186. . '" type="checkbox" name="_noproxy" />'
  187. . PHP_EOL;
  188. $card .= '<label for="'
  189. . $idArg
  190. . '">Disable proxy ('
  191. . ((defined('PROXY_NAME') && PROXY_NAME) ? PROXY_NAME : PROXY_URL)
  192. . ')</label><br />'
  193. . PHP_EOL;
  194. }
  195. $card .= HTMLUtils::getHelperButtonsFormat($formats);
  196. } else {
  197. $card .= '<span style="font-weight: bold;">Inactive</span>';
  198. }
  199. $card .= '</form>' . PHP_EOL;
  200. }
  201. $card .= '<label class="showless" for="showmore-' . $bridgeName . '">Show less</label>';
  202. $card .= '<p class="maintainer">' . $bridgeClass::MAINTAINER . '</p>';
  203. $card .= '</section>';
  204. return $card;
  205. }
  206. private static function getHelperButtonsFormat($formats){
  207. $buttons = '';
  208. foreach($formats as $name){
  209. $buttons .= '<button type="submit" name="format" value="'
  210. . $name
  211. . '">'
  212. . $name
  213. . '</button>'
  214. . PHP_EOL;
  215. }
  216. return $buttons;
  217. }
  218. private static function getFormHeader($bridge){
  219. return <<<EOD
  220. <form method="GET" action="?">
  221. <input type="hidden" name="action" value="display" />
  222. <input type="hidden" name="bridge" value="{$bridge}" />
  223. EOD;
  224. }
  225. }
  226. class HTMLSanitizer {
  227. var $tagsToRemove;
  228. var $keptAttributes;
  229. var $onlyKeepText;
  230. public static $DEFAULT_CLEAR_TAGS = ["script", "iframe", "input", "form"];
  231. public static $KEPT_ATTRIBUTES = ["title", "href", "src"];
  232. public static $ONLY_TEXT = [];
  233. public function __construct($tags_to_remove = null
  234. , $kept_attributes = null
  235. , $only_keep_text = null){
  236. $this->tagsToRemove = is_null($tags_to_remove) ? HTMLSanitizer::$DEFAULT_CLEAR_TAGS : $tags_to_remove;
  237. $this->keptAttributes = is_null($kept_attributes) ? HTMLSanitizer::$KEPT_ATTRIBUTES : $kept_attributes;
  238. $this->onlyKeepText = is_null($only_keep_text) ? HTMLSanitizer::$ONLY_TEXT : $only_keep_text;
  239. }
  240. public function sanitize($textToSanitize){
  241. $htmlContent = str_get_html($textToSanitize);
  242. foreach($htmlContent->find('*[!b38fd2b1fe7f4747d6b1c1254ccd055e]') as $element){
  243. if(in_array($element->tag, $this->onlyKeepText)){
  244. $element->outertext = $element->plaintext;
  245. } elseif(in_array($element->tag, $this->tagsToRemove)){
  246. $element->outertext = '';
  247. } else {
  248. foreach($element->getAllAttributes() as $attributeName => $attribute){
  249. if(!in_array($attributeName, $this->keptAttributes))
  250. $element->removeAttribute($attributeName);
  251. }
  252. }
  253. }
  254. return $htmlContent;
  255. }
  256. public static function defaultImageSrcTo($content, $server){
  257. foreach($content->find('img') as $image){
  258. if(is_null(strpos($image->src, "http"))
  259. && is_null(strpos($image->src, "//"))
  260. && is_null(strpos($image->src, "data:")))
  261. $image->src = $server . $image->src;
  262. }
  263. return $content;
  264. }
  265. }