2015-11-05 21:26:48 +01:00
|
|
|
<?php
|
|
|
|
class HTMLUtils {
|
2016-08-15 02:03:53 +02:00
|
|
|
public static function displayBridgeCard($bridgeName, $formats, $isActive = true){
|
2015-11-05 21:26:48 +01:00
|
|
|
$bridgeElement = Bridge::create($bridgeName);
|
2016-09-10 20:41:11 +02:00
|
|
|
$bridgeClass = $bridgeName . 'Bridge';
|
2016-08-15 02:03:53 +02:00
|
|
|
|
|
|
|
if($bridgeElement == false)
|
2015-11-05 21:26:48 +01:00
|
|
|
return "";
|
2016-08-15 02:03:53 +02:00
|
|
|
|
2016-08-30 11:23:15 +02:00
|
|
|
$name = '<a href="' . $bridgeClass::URI . '">' . $bridgeClass::NAME . '</a>';
|
|
|
|
$description = $bridgeClass::DESCRIPTION;
|
2015-11-05 21:26:48 +01:00
|
|
|
|
|
|
|
$card = <<<CARD
|
|
|
|
<section id="bridge-{$bridgeName}" data-ref="{$bridgeName}">
|
|
|
|
<h2>{$name}</h2>
|
|
|
|
<p class="description">
|
|
|
|
{$description}
|
|
|
|
</p>
|
2016-08-02 22:16:43 +02:00
|
|
|
<input type="checkbox" class="showmore-box" id="showmore-{$bridgeName}" />
|
|
|
|
<label class="showmore" for="showmore-{$bridgeName}">Show more</label>
|
2015-11-05 21:26:48 +01:00
|
|
|
CARD;
|
|
|
|
|
|
|
|
// If we don't have any parameter for the bridge, we print a generic form to load it.
|
2016-09-10 20:41:11 +02:00
|
|
|
if(count($bridgeClass::PARAMETERS) == 0){
|
2015-11-05 21:26:48 +01:00
|
|
|
|
2016-08-15 02:21:07 +02:00
|
|
|
$card .= HTMLUtils::getFormHeader($bridgeName);
|
2015-11-05 21:26:48 +01:00
|
|
|
|
2016-09-10 20:41:11 +02:00
|
|
|
if($isActive){
|
2016-07-03 23:49:43 +02:00
|
|
|
if(defined('PROXY_URL') && PROXY_BYBRIDGE){
|
2016-09-10 20:41:11 +02:00
|
|
|
$idArg = 'arg-'
|
|
|
|
. urlencode($bridgeName)
|
|
|
|
. '-'
|
|
|
|
. urlencode('proxyoff')
|
|
|
|
. '-'
|
|
|
|
. urlencode('_noproxy');
|
|
|
|
|
|
|
|
$card .= '<input id="'
|
|
|
|
. $idArg
|
|
|
|
. '" type="checkbox" name="_noproxy" />'
|
|
|
|
. PHP_EOL;
|
|
|
|
|
|
|
|
$card .= '<label for="'
|
|
|
|
. $idArg
|
|
|
|
. '">Disable proxy ('
|
|
|
|
. ((defined('PROXY_NAME') && PROXY_NAME) ? PROXY_NAME : PROXY_URL)
|
|
|
|
. ')</label><br />'
|
|
|
|
. PHP_EOL;
|
2016-07-03 23:11:28 +02:00
|
|
|
}
|
|
|
|
|
2015-11-05 21:26:48 +01:00
|
|
|
$card .= HTMLUtils::getHelperButtonsFormat($formats);
|
2016-08-15 02:03:53 +02:00
|
|
|
} else {
|
2015-11-05 21:26:48 +01:00
|
|
|
$card .= '<span style="font-weight: bold;">Inactive</span>';
|
|
|
|
}
|
|
|
|
|
2016-08-15 02:03:53 +02:00
|
|
|
$card .= '</form>' . PHP_EOL;
|
2015-11-05 21:26:48 +01:00
|
|
|
}
|
|
|
|
|
2016-08-30 11:23:15 +02:00
|
|
|
$hasGlobalParameter = array_key_exists('global', $bridgeClass::PARAMETERS);
|
2016-08-15 02:03:53 +02:00
|
|
|
|
2016-08-22 01:16:06 +02:00
|
|
|
if($hasGlobalParameter){
|
2016-08-30 11:23:15 +02:00
|
|
|
$globalParameters = $bridgeClass::PARAMETERS['global'];
|
2016-08-22 01:16:06 +02:00
|
|
|
}
|
2016-07-03 23:11:28 +02:00
|
|
|
|
2016-08-30 11:23:15 +02:00
|
|
|
foreach($bridgeClass::PARAMETERS as $parameterName => $parameter){
|
2016-08-15 02:03:53 +02:00
|
|
|
if(!is_numeric($parameterName) && $parameterName == 'global')
|
2016-02-11 16:24:40 +01:00
|
|
|
continue;
|
2016-07-03 23:11:28 +02:00
|
|
|
|
2016-08-15 02:03:53 +02:00
|
|
|
if($hasGlobalParameter)
|
2016-02-11 16:24:40 +01:00
|
|
|
$parameter = array_merge($parameter, $globalParameters);
|
|
|
|
|
2016-08-15 02:03:53 +02:00
|
|
|
if(!is_numeric($parameterName))
|
|
|
|
$card .= '<h5>' . $parameterName . '</h5>' . PHP_EOL;
|
2015-11-05 21:26:48 +01:00
|
|
|
|
2016-08-15 02:21:07 +02:00
|
|
|
$card .= HTMLUtils::getFormHeader($bridgeName);
|
2015-11-05 21:26:48 +01:00
|
|
|
|
2016-09-10 21:01:02 +02:00
|
|
|
foreach($parameter as $id => $inputEntry){
|
2016-08-15 02:03:53 +02:00
|
|
|
$additionalInfoString = '';
|
2015-11-05 21:26:48 +01:00
|
|
|
|
2016-08-15 02:03:53 +02:00
|
|
|
if(isset($inputEntry['required']) && $inputEntry['required'] === true)
|
|
|
|
$additionalInfoString .= ' required';
|
2015-11-11 22:45:24 +01:00
|
|
|
|
2016-08-15 02:03:53 +02:00
|
|
|
if(isset($inputEntry['pattern']))
|
|
|
|
$additionalInfoString .= ' pattern="' . $inputEntry['pattern'] . '"';
|
2015-11-11 22:45:24 +01:00
|
|
|
|
2016-08-15 02:03:53 +02:00
|
|
|
if(isset($inputEntry['title']))
|
|
|
|
$additionalInfoString .= ' title="' . $inputEntry['title'] . '"';
|
2015-11-11 22:45:24 +01:00
|
|
|
|
2016-08-15 02:03:53 +02:00
|
|
|
if(!isset($inputEntry['exampleValue']))
|
|
|
|
$inputEntry['exampleValue'] = '';
|
2015-11-05 21:26:48 +01:00
|
|
|
|
2016-08-15 02:47:22 +02:00
|
|
|
if(!isset($inputEntry['defaultValue']))
|
|
|
|
$inputEntry['defaultValue'] = '';
|
|
|
|
|
2016-09-10 20:41:11 +02:00
|
|
|
$idArg = 'arg-'
|
|
|
|
. urlencode($bridgeName)
|
|
|
|
. '-'
|
|
|
|
. urlencode($parameterName)
|
|
|
|
. '-'
|
|
|
|
. urlencode($id);
|
|
|
|
|
|
|
|
$card .= '<label for="'
|
|
|
|
. $idArg
|
|
|
|
. '">'
|
|
|
|
. $inputEntry['name']
|
|
|
|
. ' : </label>'
|
|
|
|
. PHP_EOL;
|
|
|
|
|
|
|
|
if(!isset($inputEntry['type']) || $inputEntry['type'] == 'text'){
|
|
|
|
$card .= '<input '
|
|
|
|
. $additionalInfoString
|
|
|
|
. ' id="'
|
|
|
|
. $idArg
|
|
|
|
. '" type="text" value="'
|
|
|
|
. $inputEntry['defaultValue']
|
|
|
|
. '" placeholder="'
|
|
|
|
. $inputEntry['exampleValue']
|
|
|
|
. '" name="'
|
|
|
|
. $id
|
|
|
|
. '" /><br />'
|
|
|
|
. PHP_EOL;
|
|
|
|
} elseif($inputEntry['type'] == 'number'){
|
|
|
|
$card .= '<input '
|
|
|
|
. $additionalInfoString
|
|
|
|
. ' id="'
|
|
|
|
. $idArg
|
|
|
|
. '" type="number" value="'
|
|
|
|
. $inputEntry['defaultValue']
|
|
|
|
. '" placeholder="'
|
|
|
|
. $inputEntry['exampleValue']
|
|
|
|
. '" name="'
|
|
|
|
. $id
|
|
|
|
. '" /><br />'
|
|
|
|
. PHP_EOL;
|
|
|
|
} else if($inputEntry['type'] == 'list'){
|
|
|
|
$card .= '<select '
|
|
|
|
. $additionalInfoString
|
|
|
|
. ' id="'
|
|
|
|
. $idArg
|
|
|
|
. '" name="'
|
|
|
|
. $id
|
|
|
|
. '" >';
|
|
|
|
|
|
|
|
foreach($inputEntry['values'] as $name => $value){
|
|
|
|
if(is_array($value)){
|
|
|
|
$card .= '<optgroup label="' . htmlentities($name) . '">';
|
|
|
|
foreach($value as $subname => $subvalue){
|
|
|
|
if($inputEntry['defaultValue'] === $subname
|
|
|
|
|| $inputEntry['defaultValue'] === $subvalue){
|
|
|
|
$card .= '<option value="'
|
|
|
|
. $subvalue
|
|
|
|
. '" selected>'
|
|
|
|
. $subname
|
|
|
|
. '</option>';
|
|
|
|
} else {
|
|
|
|
$card .= '<option value="'
|
|
|
|
. $subvalue
|
|
|
|
. '">'
|
|
|
|
. $subname
|
|
|
|
. '</option>';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$card .= '</optgroup>';
|
|
|
|
} else {
|
|
|
|
if($inputEntry['defaultValue'] === $name
|
|
|
|
|| $inputEntry['defaultValue'] === $value){
|
|
|
|
$card .= '<option value="'
|
|
|
|
. $value
|
|
|
|
. '" selected>'
|
|
|
|
. $name
|
|
|
|
. '</option>';
|
|
|
|
} else {
|
|
|
|
$card .= '<option value="'
|
|
|
|
. $value
|
|
|
|
. '">'
|
|
|
|
. $name
|
|
|
|
. '</option>';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-11-05 21:26:48 +01:00
|
|
|
$card .= '</select><br >';
|
2016-09-10 20:41:11 +02:00
|
|
|
} elseif($inputEntry['type'] == 'checkbox'){
|
2016-08-15 02:47:22 +02:00
|
|
|
if($inputEntry['defaultValue'] === 'checked')
|
2016-09-10 20:41:11 +02:00
|
|
|
$card .= '<input '
|
|
|
|
. $additionalInfoString
|
|
|
|
. ' id="'
|
|
|
|
. $idArg
|
|
|
|
. '" type="checkbox" name="'
|
|
|
|
. $id
|
|
|
|
. '" checked /><br />'
|
|
|
|
. PHP_EOL;
|
2016-08-15 02:47:22 +02:00
|
|
|
else
|
2016-09-10 20:41:11 +02:00
|
|
|
$card .= '<input '
|
|
|
|
. $additionalInfoString
|
|
|
|
. ' id="'
|
|
|
|
. $idArg
|
|
|
|
. '" type="checkbox" name="'
|
|
|
|
. $id
|
|
|
|
. '" /><br />'
|
|
|
|
. PHP_EOL;
|
2015-11-05 21:26:48 +01:00
|
|
|
}
|
|
|
|
}
|
2016-08-15 02:03:53 +02:00
|
|
|
|
2016-09-10 20:41:11 +02:00
|
|
|
if($isActive){
|
2016-07-03 23:49:43 +02:00
|
|
|
if(defined('PROXY_URL') && PROXY_BYBRIDGE){
|
2016-09-10 20:41:11 +02:00
|
|
|
$idArg = 'arg-'
|
|
|
|
. urlencode($bridgeName)
|
|
|
|
. '-'
|
|
|
|
. urlencode('proxyoff')
|
|
|
|
. '-'
|
|
|
|
. urlencode('_noproxy');
|
|
|
|
|
|
|
|
$card .= '<input id="'
|
|
|
|
. $idArg
|
|
|
|
. '" type="checkbox" name="_noproxy" />'
|
|
|
|
. PHP_EOL;
|
|
|
|
|
|
|
|
$card .= '<label for="'
|
|
|
|
. $idArg
|
|
|
|
. '">Disable proxy ('
|
|
|
|
. ((defined('PROXY_NAME') && PROXY_NAME) ? PROXY_NAME : PROXY_URL)
|
|
|
|
. ')</label><br />'
|
|
|
|
. PHP_EOL;
|
2016-07-03 23:11:28 +02:00
|
|
|
}
|
2015-11-05 21:26:48 +01:00
|
|
|
$card .= HTMLUtils::getHelperButtonsFormat($formats);
|
2016-08-15 02:03:53 +02:00
|
|
|
} else {
|
2015-11-05 21:26:48 +01:00
|
|
|
$card .= '<span style="font-weight: bold;">Inactive</span>';
|
|
|
|
}
|
|
|
|
$card .= '</form>' . PHP_EOL;
|
|
|
|
}
|
|
|
|
|
2016-08-02 22:16:43 +02:00
|
|
|
$card .= '<label class="showless" for="showmore-' . $bridgeName . '">Show less</label>';
|
2016-08-30 11:23:15 +02:00
|
|
|
$card .= '<p class="maintainer">' . $bridgeClass::MAINTAINER . '</p>';
|
2015-11-05 21:26:48 +01:00
|
|
|
$card .= '</section>';
|
|
|
|
|
|
|
|
return $card;
|
|
|
|
}
|
2016-08-15 02:21:07 +02:00
|
|
|
|
2016-08-15 08:13:11 +02:00
|
|
|
private static function getHelperButtonsFormat($formats){
|
|
|
|
$buttons = '';
|
2016-09-10 20:41:11 +02:00
|
|
|
foreach($formats as $name){
|
|
|
|
$buttons .= '<button type="submit" name="format" value="'
|
|
|
|
. $name
|
|
|
|
. '">'
|
|
|
|
. $name
|
|
|
|
. '</button>'
|
|
|
|
. PHP_EOL;
|
2016-08-15 08:13:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $buttons;
|
|
|
|
}
|
|
|
|
|
2016-08-15 02:21:07 +02:00
|
|
|
private static function getFormHeader($bridge){
|
|
|
|
return <<<EOD
|
|
|
|
<form method="GET" action="?">
|
|
|
|
<input type="hidden" name="action" value="display" />
|
|
|
|
<input type="hidden" name="bridge" value="{$bridge}" />
|
|
|
|
EOD;
|
|
|
|
}
|
2015-11-11 22:45:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class HTMLSanitizer {
|
|
|
|
|
|
|
|
var $tagsToRemove;
|
|
|
|
var $keptAttributes;
|
|
|
|
var $onlyKeepText;
|
|
|
|
|
2016-02-26 19:17:48 +01:00
|
|
|
public static $DEFAULT_CLEAR_TAGS = ["script", "iframe", "input", "form"];
|
2015-12-06 18:47:20 +01:00
|
|
|
public static $KEPT_ATTRIBUTES = ["title", "href", "src"];
|
2016-02-26 19:17:48 +01:00
|
|
|
public static $ONLY_TEXT = [];
|
2015-11-11 22:45:24 +01:00
|
|
|
|
2016-09-10 20:41:11 +02:00
|
|
|
public function __construct($tags_to_remove = null
|
|
|
|
, $kept_attributes = null
|
|
|
|
, $only_keep_text = null){
|
|
|
|
$this->tagsToRemove = is_null($tags_to_remove) ? HTMLSanitizer::$DEFAULT_CLEAR_TAGS : $tags_to_remove;
|
|
|
|
$this->keptAttributes = is_null($kept_attributes) ? HTMLSanitizer::$KEPT_ATTRIBUTES : $kept_attributes;
|
|
|
|
$this->onlyKeepText = is_null($only_keep_text) ? HTMLSanitizer::$ONLY_TEXT : $only_keep_text;
|
2015-11-11 22:45:24 +01:00
|
|
|
}
|
|
|
|
|
2016-09-10 20:41:11 +02:00
|
|
|
public function sanitize($textToSanitize){
|
2015-11-11 22:45:24 +01:00
|
|
|
$htmlContent = str_get_html($textToSanitize);
|
|
|
|
|
2016-09-10 20:41:11 +02:00
|
|
|
foreach($htmlContent->find('*[!b38fd2b1fe7f4747d6b1c1254ccd055e]') as $element){
|
|
|
|
if(in_array($element->tag, $this->onlyKeepText)){
|
2015-11-11 22:45:24 +01:00
|
|
|
$element->outertext = $element->plaintext;
|
2016-09-10 20:41:11 +02:00
|
|
|
} elseif(in_array($element->tag, $this->tagsToRemove)){
|
2015-11-11 22:45:24 +01:00
|
|
|
$element->outertext = '';
|
|
|
|
} else {
|
2016-09-10 20:41:11 +02:00
|
|
|
foreach($element->getAllAttributes() as $attributeName => $attribute){
|
2016-07-03 23:11:28 +02:00
|
|
|
if(!in_array($attributeName, $this->keptAttributes))
|
2016-08-15 02:03:53 +02:00
|
|
|
$element->removeAttribute($attributeName);
|
2015-11-11 22:45:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $htmlContent;
|
|
|
|
}
|
2016-02-26 19:17:48 +01:00
|
|
|
|
2016-09-10 20:41:11 +02:00
|
|
|
public static function defaultImageSrcTo($content, $server){
|
|
|
|
foreach($content->find('img') as $image){
|
|
|
|
if(is_null(strpos($image->src, "http"))
|
|
|
|
&& is_null(strpos($image->src, "//"))
|
|
|
|
&& is_null(strpos($image->src, "data:")))
|
|
|
|
$image->src = $server . $image->src;
|
2016-08-15 02:03:53 +02:00
|
|
|
}
|
2016-02-26 19:17:48 +01:00
|
|
|
return $content;
|
2016-08-15 02:03:53 +02:00
|
|
|
}
|
2015-11-05 21:26:48 +01:00
|
|
|
}
|