html.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. $HTTPSWarning = '';
  26. if(strpos($bridge->getURI(), 'https') !== 0) {
  27. $HTTPSWarning = '<div class="secure-warning">Warning :
  28. This bridge is not fetching its content through a secure connection</div>';
  29. }
  30. $name = '<a href="' . $bridge->getURI() . '">' . $bridge->getName() . '</a>';
  31. $description = $bridge->getDescription();
  32. $card = <<<CARD
  33. <section id="bridge-{$bridgeName}" data-ref="{$bridgeName}">
  34. <h2>{$name}</h2>
  35. <p class="description">
  36. {$description}
  37. </p>
  38. <input type="checkbox" class="showmore-box" id="showmore-{$bridgeName}" />
  39. <label class="showmore" for="showmore-{$bridgeName}">Show more</label>
  40. CARD;
  41. // If we don't have any parameter for the bridge, we print a generic form to load it.
  42. if(count($bridge->getParameters()) == 0) {
  43. $card .= $getFormHeader($bridgeName);
  44. $card .= $HTTPSWarning;
  45. if($isActive) {
  46. if(defined('PROXY_URL') && PROXY_BYBRIDGE) {
  47. $idArg = 'arg-'
  48. . urlencode($bridgeName)
  49. . '-'
  50. . urlencode('proxyoff')
  51. . '-'
  52. . urlencode('_noproxy');
  53. $card .= '<input id="'
  54. . $idArg
  55. . '" type="checkbox" name="_noproxy" />'
  56. . PHP_EOL;
  57. $card .= '<label for="'
  58. . $idArg
  59. . '">Disable proxy ('
  60. . ((defined('PROXY_NAME') && PROXY_NAME) ? PROXY_NAME : PROXY_URL)
  61. . ')</label><br />'
  62. . PHP_EOL;
  63. } if(CUSTOM_CACHE_TIMEOUT) {
  64. $idArg = 'arg-'
  65. . urlencode($bridgeName)
  66. . '-'
  67. . urlencode('_cache_timeout');
  68. $card .= '<label for="'
  69. . $idArg
  70. . '">Cache timeout in seconds : </label>'
  71. . PHP_EOL;
  72. $card .= '<input id="'
  73. . $idArg
  74. . '" type="number" value="'
  75. . $bridge->getCacheTimeout()
  76. . '" name="_cache_timeout" /><br />'
  77. . PHP_EOL;
  78. }
  79. $card .= $getHelperButtonsFormat($formats);
  80. } else {
  81. $card .= '<span style="font-weight: bold;">Inactive</span>';
  82. }
  83. $card .= '</form>' . PHP_EOL;
  84. }
  85. $hasGlobalParameter = array_key_exists('global', $bridge->getParameters());
  86. if($hasGlobalParameter) {
  87. $globalParameters = $bridge->getParameters()['global'];
  88. }
  89. foreach($bridge->getParameters() as $parameterName => $parameter) {
  90. if(!is_numeric($parameterName) && $parameterName == 'global')
  91. continue;
  92. if($hasGlobalParameter)
  93. $parameter = array_merge($parameter, $globalParameters);
  94. if(!is_numeric($parameterName))
  95. $card .= '<h5>' . $parameterName . '</h5>' . PHP_EOL;
  96. $card .= $getFormHeader($bridgeName);
  97. $card .= $HTTPSWarning;
  98. foreach($parameter as $id => $inputEntry) {
  99. $additionalInfoString = '';
  100. if(isset($inputEntry['required']) && $inputEntry['required'] === true)
  101. $additionalInfoString .= ' required';
  102. if(isset($inputEntry['pattern']))
  103. $additionalInfoString .= ' pattern="' . $inputEntry['pattern'] . '"';
  104. if(isset($inputEntry['title']))
  105. $additionalInfoString .= ' title="' . $inputEntry['title'] . '"';
  106. if(!isset($inputEntry['exampleValue']))
  107. $inputEntry['exampleValue'] = '';
  108. if(!isset($inputEntry['defaultValue']))
  109. $inputEntry['defaultValue'] = '';
  110. $idArg = 'arg-'
  111. . urlencode($bridgeName)
  112. . '-'
  113. . urlencode($parameterName)
  114. . '-'
  115. . urlencode($id);
  116. $card .= '<label for="'
  117. . $idArg
  118. . '">'
  119. . $inputEntry['name']
  120. . ' : </label>'
  121. . PHP_EOL;
  122. if(!isset($inputEntry['type']) || $inputEntry['type'] == 'text') {
  123. $card .= '<input '
  124. . $additionalInfoString
  125. . ' id="'
  126. . $idArg
  127. . '" type="text" value="'
  128. . $inputEntry['defaultValue']
  129. . '" placeholder="'
  130. . $inputEntry['exampleValue']
  131. . '" name="'
  132. . $id
  133. . '" /><br />'
  134. . PHP_EOL;
  135. } elseif($inputEntry['type'] == 'number') {
  136. $card .= '<input '
  137. . $additionalInfoString
  138. . ' id="'
  139. . $idArg
  140. . '" type="number" value="'
  141. . $inputEntry['defaultValue']
  142. . '" placeholder="'
  143. . $inputEntry['exampleValue']
  144. . '" name="'
  145. . $id
  146. . '" /><br />'
  147. . PHP_EOL;
  148. } else if($inputEntry['type'] == 'list') {
  149. $card .= '<select '
  150. . $additionalInfoString
  151. . ' id="'
  152. . $idArg
  153. . '" name="'
  154. . $id
  155. . '" >';
  156. foreach($inputEntry['values'] as $name => $value) {
  157. if(is_array($value)) {
  158. $card .= '<optgroup label="' . htmlentities($name) . '">';
  159. foreach($value as $subname => $subvalue) {
  160. if($inputEntry['defaultValue'] === $subname
  161. || $inputEntry['defaultValue'] === $subvalue) {
  162. $card .= '<option value="'
  163. . $subvalue
  164. . '" selected>'
  165. . $subname
  166. . '</option>';
  167. } else {
  168. $card .= '<option value="'
  169. . $subvalue
  170. . '">'
  171. . $subname
  172. . '</option>';
  173. }
  174. }
  175. $card .= '</optgroup>';
  176. } else {
  177. if($inputEntry['defaultValue'] === $name
  178. || $inputEntry['defaultValue'] === $value) {
  179. $card .= '<option value="'
  180. . $value
  181. . '" selected>'
  182. . $name
  183. . '</option>';
  184. } else {
  185. $card .= '<option value="'
  186. . $value
  187. . '">'
  188. . $name
  189. . '</option>';
  190. }
  191. }
  192. }
  193. $card .= '</select><br >';
  194. } elseif($inputEntry['type'] == 'checkbox') {
  195. if($inputEntry['defaultValue'] === 'checked')
  196. $card .= '<input '
  197. . $additionalInfoString
  198. . ' id="'
  199. . $idArg
  200. . '" type="checkbox" name="'
  201. . $id
  202. . '" checked /><br />'
  203. . PHP_EOL;
  204. else
  205. $card .= '<input '
  206. . $additionalInfoString
  207. . ' id="'
  208. . $idArg
  209. . '" type="checkbox" name="'
  210. . $id
  211. . '" /><br />'
  212. . PHP_EOL;
  213. }
  214. }
  215. if($isActive) {
  216. if(defined('PROXY_URL') && PROXY_BYBRIDGE) {
  217. $idArg = 'arg-'
  218. . urlencode($bridgeName)
  219. . '-'
  220. . urlencode('proxyoff')
  221. . '-'
  222. . urlencode('_noproxy');
  223. $card .= '<input id="'
  224. . $idArg
  225. . '" type="checkbox" name="_noproxy" />'
  226. . PHP_EOL;
  227. $card .= '<label for="'
  228. . $idArg
  229. . '">Disable proxy ('
  230. . ((defined('PROXY_NAME') && PROXY_NAME) ? PROXY_NAME : PROXY_URL)
  231. . ')</label><br />'
  232. . PHP_EOL;
  233. } if(CUSTOM_CACHE_TIMEOUT) {
  234. $idArg = 'arg-'
  235. . urlencode($bridgeName)
  236. . '-'
  237. . urlencode('_cache_timeout');
  238. $card .= '<label for="'
  239. . $idArg
  240. . '">Cache timeout in seconds : </label>'
  241. . PHP_EOL;
  242. $card .= '<input id="'
  243. . $idArg
  244. . '" type="number" value="'
  245. . $bridge->getCacheTimeout()
  246. . '" name="_cache_timeout" /><br />'
  247. . PHP_EOL;
  248. }
  249. $card .= $getHelperButtonsFormat($formats);
  250. } else {
  251. $card .= '<span style="font-weight: bold;">Inactive</span>';
  252. }
  253. $card .= '</form>' . PHP_EOL;
  254. }
  255. $card .= '<label class="showless" for="showmore-' . $bridgeName . '">Show less</label>';
  256. $card .= '<p class="maintainer">' . $bridge->getMaintainer() . '</p>';
  257. $card .= '</section>';
  258. return $card;
  259. }
  260. function sanitize($textToSanitize,
  261. $removedTags = array('script', 'iframe', 'input', 'form'),
  262. $keptAttributes = array('title', 'href', 'src'),
  263. $keptText = array()){
  264. $htmlContent = str_get_html($textToSanitize);
  265. foreach($htmlContent->find('*[!b38fd2b1fe7f4747d6b1c1254ccd055e]') as $element) {
  266. if(in_array($element->tag, $keptText)) {
  267. $element->outertext = $element->plaintext;
  268. } elseif(in_array($element->tag, $removedTags)) {
  269. $element->outertext = '';
  270. } else {
  271. foreach($element->getAllAttributes() as $attributeName => $attribute) {
  272. if(!in_array($attributeName, $keptAttributes))
  273. $element->removeAttribute($attributeName);
  274. }
  275. }
  276. }
  277. return $htmlContent;
  278. }
  279. function backgroundToImg($htmlContent) {
  280. $regex = '/background-image[ ]{0,}:[ ]{0,}url\([\'"]{0,}(.*?)[\'"]{0,}\)/';
  281. $htmlContent = str_get_html($htmlContent);
  282. foreach($htmlContent->find('*[!b38fd2b1fe7f4747d6b1c1254ccd055e]') as $element) {
  283. if(preg_match($regex, $element->style, $matches) > 0) {
  284. $element->outertext = '<img style="display:block;" src="' . $matches[1] . '" />';
  285. }
  286. }
  287. return $htmlContent;
  288. }
  289. function defaultLinkTo($content, $server){
  290. foreach($content->find('img') as $image) {
  291. if(strpos($image->src, 'http') === false
  292. && strpos($image->src, '//') === false
  293. && strpos($image->src, 'data:') === false)
  294. $image->src = $server . $image->src;
  295. }
  296. foreach($content->find('a') as $anchor) {
  297. if(strpos($anchor->href, 'http') === false
  298. && strpos($anchor->href, '//') === false
  299. && strpos($anchor->href, '#') !== 0
  300. && strpos($anchor->href, '?') !== 0)
  301. $anchor->href = $server . $anchor->href;
  302. }
  303. return $content;
  304. }