2015-11-05 21:26:48 +01:00
|
|
|
<?php
|
|
|
|
class HTMLUtils {
|
|
|
|
|
|
|
|
public static function getHelperButtonFormat($value, $name){
|
|
|
|
return '<button type="submit" name="format" value="' . $value . '">' . $name . '</button>';
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getHelperButtonsFormat($formats){
|
|
|
|
$buttons = '';
|
2016-08-15 02:03:53 +02:00
|
|
|
|
|
|
|
foreach( $formats as $name => $infos ){
|
|
|
|
if ( isset($infos['name']) )
|
|
|
|
$buttons .= HTMLUtils::getHelperButtonFormat($name, $infos['name']) . PHP_EOL;
|
|
|
|
}
|
|
|
|
|
2015-11-05 21:26:48 +01:00
|
|
|
return $buttons;
|
|
|
|
}
|
|
|
|
|
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-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
|
|
|
|
2015-11-05 21:26:48 +01:00
|
|
|
$bridgeElement->loadMetadatas();
|
|
|
|
|
2016-08-15 02:03:53 +02:00
|
|
|
$name = '<a href="' . $bridgeElement->uri . '">' . $bridgeElement->name . '</a>';
|
2015-11-05 21:26:48 +01:00
|
|
|
$description = $bridgeElement->description;
|
|
|
|
|
|
|
|
$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.
|
|
|
|
if(count($bridgeElement->parameters) == 0) {
|
|
|
|
|
2016-08-15 02:03:53 +02:00
|
|
|
$card .= <<<CARD
|
|
|
|
<form method="GET" action="?">
|
|
|
|
<input type="hidden" name="action" value="display" />
|
|
|
|
<input type="hidden" name="bridge" value="{$bridgeName}" />
|
|
|
|
CARD;
|
2015-11-05 21:26:48 +01:00
|
|
|
|
2016-08-15 02:03:53 +02:00
|
|
|
if ($isActive){
|
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-15 02:03:53 +02:00
|
|
|
$hasGlobalParameter = array_key_exists('global', $bridgeElement->parameters);
|
|
|
|
|
|
|
|
if($hasGlobalParameter)
|
2016-02-11 16:24:40 +01:00
|
|
|
$globalParameters = json_decode($bridgeElement->parameters['global'], true);
|
|
|
|
|
2016-08-15 02:03:53 +02:00
|
|
|
foreach($bridgeElement->parameters as $parameterName => $parameter){
|
2016-02-11 16:24:40 +01:00
|
|
|
$parameter = json_decode($parameter, true);
|
|
|
|
|
2016-08-15 02:03:53 +02:00
|
|
|
if(!is_numeric($parameterName) && $parameterName == 'global')
|
2016-02-11 16:24:40 +01:00
|
|
|
continue;
|
|
|
|
|
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:03:53 +02:00
|
|
|
$card .= <<<CARD
|
|
|
|
<form method="GET" action="?">
|
|
|
|
<input type="hidden" name="action" value="display" />
|
|
|
|
<input type="hidden" name="bridge" value="{$bridgeName}" />
|
|
|
|
CARD;
|
2015-11-05 21:26:48 +01:00
|
|
|
|
|
|
|
foreach($parameter as $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
|
|
|
|
2015-11-11 22:45:24 +01:00
|
|
|
$idArg = 'arg-' . urlencode($bridgeName) . '-' . urlencode($parameterName) . '-' . urlencode($inputEntry['identifier']);
|
2016-08-15 02:03:53 +02:00
|
|
|
$card .= '<label for="' . $idArg . '">' . $inputEntry['name'] . ' : </label>' . PHP_EOL;
|
2015-11-05 21:26:48 +01:00
|
|
|
|
|
|
|
if(!isset($inputEntry['type']) || $inputEntry['type'] == 'text') {
|
2016-08-15 02:03:53 +02:00
|
|
|
$card .= '<input ' . $additionalInfoString . ' id="' . $idArg . '" type="text" value="" placeholder="' . $inputEntry['exampleValue'] . '" name="' . $inputEntry['identifier'] . '" /><br />' . PHP_EOL;
|
2015-11-05 21:26:48 +01:00
|
|
|
} else if($inputEntry['type'] == 'number') {
|
2016-08-15 02:03:53 +02:00
|
|
|
$card .= '<input ' . $additionalInfoString . ' id="' . $idArg . '" type="number" value="" placeholder="' . $inputEntry['exampleValue'] . '" name="' . $inputEntry['identifier'] . '" /><br />' . PHP_EOL;
|
2015-11-05 21:26:48 +01:00
|
|
|
} else if($inputEntry['type'] == 'list') {
|
2016-08-15 02:03:53 +02:00
|
|
|
$card .= '<select ' . $additionalInfoString . ' id="' . $idArg . '" name="' . $inputEntry['identifier'] . '" >';
|
|
|
|
|
2015-11-05 21:26:48 +01:00
|
|
|
foreach($inputEntry['values'] as $listValues) {
|
2016-08-15 02:03:53 +02:00
|
|
|
$card .= '<option ' . $additionalInfoString . ' value="' . $listValues['value'] . '">' . $listValues['name'] . '</option>';
|
2015-11-05 21:26:48 +01:00
|
|
|
}
|
2016-08-15 02:03:53 +02:00
|
|
|
|
2015-11-05 21:26:48 +01:00
|
|
|
$card .= '</select><br >';
|
|
|
|
} else if($inputEntry['type'] == 'checkbox') {
|
|
|
|
$card .= '<input id="' . $idArg . '" type="checkbox" name="' . $inputEntry['identifier'] . '" /><br />' . PHP_EOL;
|
|
|
|
}
|
|
|
|
}
|
2016-08-15 02:03:53 +02:00
|
|
|
|
|
|
|
if ($isActive){
|
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
|
|
|
|
2015-11-05 21:26:48 +01:00
|
|
|
$card .= '</form>' . PHP_EOL;
|
|
|
|
}
|
|
|
|
|
2016-08-02 22:16:43 +02:00
|
|
|
$card .= '<label class="showless" for="showmore-' . $bridgeName . '">Show less</label>';
|
2016-08-15 02:03:53 +02:00
|
|
|
$card .= '<p class="maintainer">' . $bridgeElement->maintainer . '</p>';
|
2015-11-05 21:26:48 +01:00
|
|
|
$card .= '</section>';
|
|
|
|
|
|
|
|
return $card;
|
|
|
|
}
|
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-08-15 02:03:53 +02:00
|
|
|
public function __construct($tags_to_remove = null, $kept_attributes = null, $only_keep_text = null) {
|
2016-02-26 19:17:48 +01:00
|
|
|
$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;
|
2015-11-11 22:45:24 +01:00
|
|
|
}
|
|
|
|
|
2016-08-15 02:03:53 +02:00
|
|
|
public function sanitize($textToSanitize) {
|
2015-11-11 22:45:24 +01:00
|
|
|
$htmlContent = str_get_html($textToSanitize);
|
|
|
|
|
2016-02-26 19:31:53 +01:00
|
|
|
foreach($htmlContent->find('*[!b38fd2b1fe7f4747d6b1c1254ccd055e]') as $element) {
|
2015-11-11 22:45:24 +01:00
|
|
|
if(in_array($element->tag, $this->onlyKeepText)) {
|
|
|
|
$element->outertext = $element->plaintext;
|
|
|
|
} else if(in_array($element->tag, $this->tagsToRemove)) {
|
|
|
|
$element->outertext = '';
|
|
|
|
} else {
|
|
|
|
foreach($element->getAllAttributes() as $attributeName => $attribute) {
|
2016-08-15 02:03:53 +02:00
|
|
|
if(!in_array($attributeName, $this->keptAttributes))
|
|
|
|
$element->removeAttribute($attributeName);
|
2015-11-11 22:45:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $htmlContent;
|
|
|
|
}
|
2016-02-26 19:17:48 +01:00
|
|
|
|
2016-08-15 02:03:53 +02:00
|
|
|
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;
|
|
|
|
}
|
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
|
|
|
}
|