[HTMLUtils] Cleanup code
- Use single quote instead of double quotes - Remove unnecessary empty lines - Use common format for indentation and blocks - Use HEREDOC for long strings - Remove braces from single line if-statements
This commit is contained in:
parent
fa59a25229
commit
f853f62270
1 changed files with 63 additions and 95 deletions
|
@ -7,23 +7,21 @@ class HTMLUtils {
|
||||||
|
|
||||||
public static function getHelperButtonsFormat($formats){
|
public static function getHelperButtonsFormat($formats){
|
||||||
$buttons = '';
|
$buttons = '';
|
||||||
foreach( $formats as $name => $infos )
|
|
||||||
{
|
foreach( $formats as $name => $infos ){
|
||||||
if ( isset($infos['name']) )
|
if ( isset($infos['name']) )
|
||||||
{
|
|
||||||
$buttons .= HTMLUtils::getHelperButtonFormat($name, $infos['name']) . PHP_EOL;
|
$buttons .= HTMLUtils::getHelperButtonFormat($name, $infos['name']) . PHP_EOL;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return $buttons;
|
return $buttons;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function displayBridgeCard($bridgeName, $formats, $isActive = true)
|
public static function displayBridgeCard($bridgeName, $formats, $isActive = true){
|
||||||
{
|
|
||||||
|
|
||||||
$bridgeElement = Bridge::create($bridgeName);
|
$bridgeElement = Bridge::create($bridgeName);
|
||||||
if($bridgeElement == false) {
|
|
||||||
|
if($bridgeElement == false)
|
||||||
return "";
|
return "";
|
||||||
}
|
|
||||||
$bridgeElement->loadMetadatas();
|
$bridgeElement->loadMetadatas();
|
||||||
|
|
||||||
$name = '<a href="' . $bridgeElement->uri . '">' . $bridgeElement->name . '</a>';
|
$name = '<a href="' . $bridgeElement->uri . '">' . $bridgeElement->name . '</a>';
|
||||||
|
@ -42,74 +40,60 @@ CARD;
|
||||||
// If we don't have any parameter for the bridge, we print a generic form to load it.
|
// If we don't have any parameter for the bridge, we print a generic form to load it.
|
||||||
if(count($bridgeElement->parameters) == 0) {
|
if(count($bridgeElement->parameters) == 0) {
|
||||||
|
|
||||||
$card .= '<form method="GET" action="?">
|
$card .= <<<CARD
|
||||||
|
<form method="GET" action="?">
|
||||||
<input type="hidden" name="action" value="display" />
|
<input type="hidden" name="action" value="display" />
|
||||||
<input type="hidden" name="bridge" value="' . $bridgeName . '" />' . PHP_EOL;
|
<input type="hidden" name="bridge" value="{$bridgeName}" />
|
||||||
|
CARD;
|
||||||
|
|
||||||
if ($isActive)
|
if ($isActive){
|
||||||
{
|
|
||||||
$card .= HTMLUtils::getHelperButtonsFormat($formats);
|
$card .= HTMLUtils::getHelperButtonsFormat($formats);
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
$card .= '<span style="font-weight: bold;">Inactive</span>';
|
$card .= '<span style="font-weight: bold;">Inactive</span>';
|
||||||
}
|
}
|
||||||
|
|
||||||
$card .= '</form>' . PHP_EOL;
|
$card .= '</form>' . PHP_EOL;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$hasGlobalParameter = array_key_exists("global", $bridgeElement->parameters);
|
$hasGlobalParameter = array_key_exists('global', $bridgeElement->parameters);
|
||||||
if($hasGlobalParameter) {
|
|
||||||
|
if($hasGlobalParameter)
|
||||||
$globalParameters = json_decode($bridgeElement->parameters['global'], true);
|
$globalParameters = json_decode($bridgeElement->parameters['global'], true);
|
||||||
}
|
|
||||||
|
|
||||||
foreach($bridgeElement->parameters as $parameterName => $parameter)
|
|
||||||
{
|
|
||||||
|
|
||||||
|
foreach($bridgeElement->parameters as $parameterName => $parameter){
|
||||||
$parameter = json_decode($parameter, true);
|
$parameter = json_decode($parameter, true);
|
||||||
|
|
||||||
if(!is_numeric($parameterName) && $parameterName == "global") {
|
if(!is_numeric($parameterName) && $parameterName == 'global')
|
||||||
|
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
}
|
if($hasGlobalParameter)
|
||||||
|
|
||||||
if($hasGlobalParameter) {
|
|
||||||
|
|
||||||
$parameter = array_merge($parameter, $globalParameters);
|
$parameter = array_merge($parameter, $globalParameters);
|
||||||
|
|
||||||
}
|
if(!is_numeric($parameterName))
|
||||||
|
|
||||||
if(!is_numeric($parameterName)) {
|
|
||||||
$card .= '<h5>' . $parameterName . '</h5>' . PHP_EOL;
|
$card .= '<h5>' . $parameterName . '</h5>' . PHP_EOL;
|
||||||
}
|
|
||||||
$card .= '<form method="GET" action="?">
|
|
||||||
<input type="hidden" name="action" value="display" />
|
|
||||||
<input type="hidden" name="bridge" value="' . $bridgeName . '" />' . PHP_EOL;
|
|
||||||
|
|
||||||
|
$card .= <<<CARD
|
||||||
|
<form method="GET" action="?">
|
||||||
|
<input type="hidden" name="action" value="display" />
|
||||||
|
<input type="hidden" name="bridge" value="{$bridgeName}" />
|
||||||
|
CARD;
|
||||||
|
|
||||||
foreach($parameter as $inputEntry) {
|
foreach($parameter as $inputEntry) {
|
||||||
|
$additionalInfoString = '';
|
||||||
|
|
||||||
$additionalInfoString = "";
|
if(isset($inputEntry['required']) && $inputEntry['required'] === true)
|
||||||
if(isset($inputEntry['required']) && $inputEntry['required'] === true) {
|
$additionalInfoString .= ' required';
|
||||||
|
|
||||||
$additionalInfoString .= " required";
|
if(isset($inputEntry['pattern']))
|
||||||
|
$additionalInfoString .= ' pattern="' . $inputEntry['pattern'] . '"';
|
||||||
|
|
||||||
}
|
if(isset($inputEntry['title']))
|
||||||
if(isset($inputEntry['pattern'])) {
|
$additionalInfoString .= ' title="' . $inputEntry['title'] . '"';
|
||||||
|
|
||||||
$additionalInfoString .= " pattern=\"".$inputEntry['pattern']."\"";
|
if(!isset($inputEntry['exampleValue']))
|
||||||
|
$inputEntry['exampleValue'] = '';
|
||||||
}
|
|
||||||
if(isset($inputEntry['title'])) {
|
|
||||||
|
|
||||||
$additionalInfoString .= " title=\"" .$inputEntry['title']."\"";
|
|
||||||
|
|
||||||
}
|
|
||||||
if(!isset($inputEntry['exampleValue'])) $inputEntry['exampleValue'] = "";
|
|
||||||
|
|
||||||
$idArg = 'arg-' . urlencode($bridgeName) . '-' . urlencode($parameterName) . '-' . urlencode($inputEntry['identifier']);
|
$idArg = 'arg-' . urlencode($bridgeName) . '-' . urlencode($parameterName) . '-' . urlencode($inputEntry['identifier']);
|
||||||
|
|
||||||
$card .= '<label for="' . $idArg . '">' . $inputEntry['name'] . ' : </label>' . PHP_EOL;
|
$card .= '<label for="' . $idArg . '">' . $inputEntry['name'] . ' : </label>' . PHP_EOL;
|
||||||
|
|
||||||
if(!isset($inputEntry['type']) || $inputEntry['type'] == 'text') {
|
if(!isset($inputEntry['type']) || $inputEntry['type'] == 'text') {
|
||||||
|
@ -118,29 +102,24 @@ CARD;
|
||||||
$card .= '<input ' . $additionalInfoString . ' id="' . $idArg . '" type="number" value="" placeholder="' . $inputEntry['exampleValue'] . '" name="' . $inputEntry['identifier'] . '" /><br />' . PHP_EOL;
|
$card .= '<input ' . $additionalInfoString . ' id="' . $idArg . '" type="number" value="" placeholder="' . $inputEntry['exampleValue'] . '" name="' . $inputEntry['identifier'] . '" /><br />' . PHP_EOL;
|
||||||
} else if($inputEntry['type'] == 'list') {
|
} else if($inputEntry['type'] == 'list') {
|
||||||
$card .= '<select ' . $additionalInfoString . ' id="' . $idArg . '" name="' . $inputEntry['identifier'] . '" >';
|
$card .= '<select ' . $additionalInfoString . ' id="' . $idArg . '" name="' . $inputEntry['identifier'] . '" >';
|
||||||
|
|
||||||
foreach($inputEntry['values'] as $listValues) {
|
foreach($inputEntry['values'] as $listValues) {
|
||||||
|
$card .= '<option ' . $additionalInfoString . ' value="' . $listValues['value'] . '">' . $listValues['name'] . '</option>';
|
||||||
$card .= "<option $additionalInfoString value='" . $listValues['value'] . "'>" . $listValues['name'] . "</option>";
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$card .= '</select><br >';
|
$card .= '</select><br >';
|
||||||
} else if($inputEntry['type'] == 'checkbox') {
|
} else if($inputEntry['type'] == 'checkbox') {
|
||||||
|
|
||||||
$card .= '<input id="' . $idArg . '" type="checkbox" name="' . $inputEntry['identifier'] . '" /><br />' . PHP_EOL;
|
$card .= '<input id="' . $idArg . '" type="checkbox" name="' . $inputEntry['identifier'] . '" /><br />' . PHP_EOL;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
if ($isActive){
|
||||||
if ($isActive)
|
|
||||||
{
|
|
||||||
$card .= HTMLUtils::getHelperButtonsFormat($formats);
|
$card .= HTMLUtils::getHelperButtonsFormat($formats);
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
$card .= '<span style="font-weight: bold;">Inactive</span>';
|
$card .= '<span style="font-weight: bold;">Inactive</span>';
|
||||||
}
|
}
|
||||||
$card .= '</form>' . PHP_EOL;
|
|
||||||
|
|
||||||
|
$card .= '</form>' . PHP_EOL;
|
||||||
}
|
}
|
||||||
|
|
||||||
$card .= '<label class="showless" for="showmore-' . $bridgeName . '">Show less</label>';
|
$card .= '<label class="showless" for="showmore-' . $bridgeName . '">Show less</label>';
|
||||||
|
@ -149,33 +128,25 @@ CARD;
|
||||||
|
|
||||||
return $card;
|
return $card;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class HTMLSanitizer {
|
class HTMLSanitizer {
|
||||||
|
|
||||||
|
|
||||||
var $tagsToRemove;
|
var $tagsToRemove;
|
||||||
var $keptAttributes;
|
var $keptAttributes;
|
||||||
var $onlyKeepText;
|
var $onlyKeepText;
|
||||||
|
|
||||||
|
|
||||||
public static $DEFAULT_CLEAR_TAGS = ["script", "iframe", "input", "form"];
|
public static $DEFAULT_CLEAR_TAGS = ["script", "iframe", "input", "form"];
|
||||||
public static $KEPT_ATTRIBUTES = ["title", "href", "src"];
|
public static $KEPT_ATTRIBUTES = ["title", "href", "src"];
|
||||||
|
|
||||||
public static $ONLY_TEXT = [];
|
public static $ONLY_TEXT = [];
|
||||||
|
|
||||||
function __construct($tags_to_remove = null, $kept_attributes = null, $only_keep_text = null) {
|
public function __construct($tags_to_remove = null, $kept_attributes = null, $only_keep_text = null) {
|
||||||
|
|
||||||
$this->tagsToRemove = $tags_to_remove == null ? HTMLSanitizer::$DEFAULT_CLEAR_TAGS : $tags_to_remove;
|
$this->tagsToRemove = $tags_to_remove == null ? HTMLSanitizer::$DEFAULT_CLEAR_TAGS : $tags_to_remove;
|
||||||
$this->keptAttributes = $kept_attributes == null ? HTMLSanitizer::$KEPT_ATTRIBUTES : $kept_attributes;
|
$this->keptAttributes = $kept_attributes == null ? HTMLSanitizer::$KEPT_ATTRIBUTES : $kept_attributes;
|
||||||
$this->onlyKeepText = $only_keep_text == null ? HTMLSanitizer::$ONLY_TEXT : $only_keep_text;
|
$this->onlyKeepText = $only_keep_text == null ? HTMLSanitizer::$ONLY_TEXT : $only_keep_text;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function sanitize($textToSanitize) {
|
public function sanitize($textToSanitize) {
|
||||||
|
|
||||||
$htmlContent = str_get_html($textToSanitize);
|
$htmlContent = str_get_html($textToSanitize);
|
||||||
|
|
||||||
foreach($htmlContent->find('*[!b38fd2b1fe7f4747d6b1c1254ccd055e]') as $element) {
|
foreach($htmlContent->find('*[!b38fd2b1fe7f4747d6b1c1254ccd055e]') as $element) {
|
||||||
|
@ -185,23 +156,20 @@ class HTMLSanitizer {
|
||||||
$element->outertext = '';
|
$element->outertext = '';
|
||||||
} else {
|
} else {
|
||||||
foreach($element->getAllAttributes() as $attributeName => $attribute) {
|
foreach($element->getAllAttributes() as $attributeName => $attribute) {
|
||||||
if(!in_array($attributeName, $this->keptAttributes)) $element->removeAttribute($attributeName);
|
if(!in_array($attributeName, $this->keptAttributes))
|
||||||
|
$element->removeAttribute($attributeName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $htmlContent;
|
return $htmlContent;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function defaultImageSrcTo($content, $server) {
|
public static function defaultImageSrcTo($content, $server) {
|
||||||
foreach($content->find('img') as $image) {
|
foreach($content->find('img') as $image) {
|
||||||
|
if(strpos($image->src, "http") == NULL && strpos($image->src, "//") == NULL && strpos($image->src, "data:") == NULL)
|
||||||
if(strpos($image->src, "http") == NULL && strpos($image->src, "//") == NULL && strpos($image->src, "data:") == NULL) {
|
|
||||||
$image->src = $server.$image->src;
|
$image->src = $server.$image->src;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return $content;
|
return $content;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
?>
|
|
||||||
|
|
Loading…
Reference in a new issue