html.php 7.2 KB

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