From f853f62270a62fa6b7207dc4c7dcf0af7ed49228 Mon Sep 17 00:00:00 2001 From: logmanoriginal Date: Mon, 15 Aug 2016 02:03:53 +0200 Subject: [PATCH] [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 --- lib/HTMLUtils.php | 158 ++++++++++++++++++---------------------------- 1 file changed, 63 insertions(+), 95 deletions(-) diff --git a/lib/HTMLUtils.php b/lib/HTMLUtils.php index c025e85f..57421177 100644 --- a/lib/HTMLUtils.php +++ b/lib/HTMLUtils.php @@ -7,26 +7,24 @@ class HTMLUtils { public static function getHelperButtonsFormat($formats){ $buttons = ''; - foreach( $formats as $name => $infos ) - { - if ( isset($infos['name']) ) - { - $buttons .= HTMLUtils::getHelperButtonFormat($name, $infos['name']) . PHP_EOL; - } - } + + foreach( $formats as $name => $infos ){ + if ( isset($infos['name']) ) + $buttons .= HTMLUtils::getHelperButtonFormat($name, $infos['name']) . PHP_EOL; + } + return $buttons; } - public static function displayBridgeCard($bridgeName, $formats, $isActive = true) - { - + public static function displayBridgeCard($bridgeName, $formats, $isActive = true){ $bridgeElement = Bridge::create($bridgeName); - if($bridgeElement == false) { + + if($bridgeElement == false) return ""; - } + $bridgeElement->loadMetadatas(); - $name = ''.$bridgeElement->name.''; + $name = '' . $bridgeElement->name . ''; $description = $bridgeElement->description; $card = <<parameters) == 0) { - $card .= '
- - ' . PHP_EOL; + $card .= << + + +CARD; - if ($isActive) - { + if ($isActive){ $card .= HTMLUtils::getHelperButtonsFormat($formats); - } - else - { + } else { $card .= 'Inactive'; } + $card .= '' . PHP_EOL; - } - $hasGlobalParameter = array_key_exists("global", $bridgeElement->parameters); - if($hasGlobalParameter) { + $hasGlobalParameter = array_key_exists('global', $bridgeElement->parameters); + + if($hasGlobalParameter) $globalParameters = json_decode($bridgeElement->parameters['global'], true); - } - foreach($bridgeElement->parameters as $parameterName => $parameter) - { - + foreach($bridgeElement->parameters as $parameterName => $parameter){ $parameter = json_decode($parameter, true); - if(!is_numeric($parameterName) && $parameterName == "global") { - + if(!is_numeric($parameterName) && $parameterName == 'global') continue; - - } - if($hasGlobalParameter) { - + if($hasGlobalParameter) $parameter = array_merge($parameter, $globalParameters); - } - - if(!is_numeric($parameterName)) { - $card .= '
'.$parameterName.'
' . PHP_EOL; - } - $card .= '
- - ' . PHP_EOL; + if(!is_numeric($parameterName)) + $card .= '
' . $parameterName . '
' . PHP_EOL; + $card .= << + + +CARD; 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['pattern'])) { + if(isset($inputEntry['title'])) + $additionalInfoString .= ' title="' . $inputEntry['title'] . '"'; - $additionalInfoString .= " pattern=\"".$inputEntry['pattern']."\""; - - } - if(isset($inputEntry['title'])) { - - $additionalInfoString .= " title=\"" .$inputEntry['title']."\""; - - } - if(!isset($inputEntry['exampleValue'])) $inputEntry['exampleValue'] = ""; + if(!isset($inputEntry['exampleValue'])) + $inputEntry['exampleValue'] = ''; $idArg = 'arg-' . urlencode($bridgeName) . '-' . urlencode($parameterName) . '-' . urlencode($inputEntry['identifier']); - - $card .= '' . PHP_EOL; + $card .= '' . PHP_EOL; if(!isset($inputEntry['type']) || $inputEntry['type'] == 'text') { - $card .= '
' . PHP_EOL; + $card .= '
' . PHP_EOL; } else if($inputEntry['type'] == 'number') { - $card .= '
' . PHP_EOL; + $card .= '
' . PHP_EOL; } else if($inputEntry['type'] == 'list') { - $card .= ''; + foreach($inputEntry['values'] as $listValues) { - - $card .= ""; - + $card .= ''; } + $card .= '
'; } else if($inputEntry['type'] == 'checkbox') { - $card .= '
' . PHP_EOL; - } + } - } - if ($isActive) - { + if ($isActive){ $card .= HTMLUtils::getHelperButtonsFormat($formats); - } - else - { + } else { $card .= 'Inactive'; } + $card .= '' . PHP_EOL; - } $card .= ''; - $card .= '

'.$bridgeElement->maintainer.'

'; + $card .= '

' . $bridgeElement->maintainer . '

'; $card .= ''; return $card; } - - } class HTMLSanitizer { - var $tagsToRemove; var $keptAttributes; var $onlyKeepText; - public static $DEFAULT_CLEAR_TAGS = ["script", "iframe", "input", "form"]; public static $KEPT_ATTRIBUTES = ["title", "href", "src"]; - 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->keptAttributes = $kept_attributes == null ? HTMLSanitizer::$KEPT_ATTRIBUTES : $kept_attributes; $this->onlyKeepText = $only_keep_text == null ? HTMLSanitizer::$ONLY_TEXT : $only_keep_text; - } - function sanitize($textToSanitize) { - + public function sanitize($textToSanitize) { $htmlContent = str_get_html($textToSanitize); foreach($htmlContent->find('*[!b38fd2b1fe7f4747d6b1c1254ccd055e]') as $element) { @@ -185,23 +156,20 @@ class HTMLSanitizer { $element->outertext = ''; } else { 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; - } + public static function defaultImageSrcTo($content, $server) { - foreach($content->find('img') as $image) { - - if(strpos($image->src, "http") == NULL && strpos($image->src, "//") == NULL && strpos($image->src, "data:") == NULL) { - $image->src = $server.$image->src; - } - } + foreach($content->find('img') as $image) { + if(strpos($image->src, "http") == NULL && strpos($image->src, "//") == NULL && strpos($image->src, "data:") == NULL) + $image->src = $server.$image->src; + } return $content; - } - + } } -?>