html.php 7.4 KB

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