From d83f2f285bc5b92531cf91f1b500cce501bf98f3 Mon Sep 17 00:00:00 2001 From: LogMANOriginal Date: Sat, 21 Jul 2018 18:15:07 +0200 Subject: [PATCH] Separate index and bridge card generating code into a separate classes (#734) [html] Generate index and bridge cards using separate clases Move HTML generating code from 'index.php' to 'Index.php', separating components into static functions. Move HTML generation code for bridge cards from 'html.php' to 'BridgeCard.php', separating components into static functions. --- index.php | 83 +------------ lib/BridgeCard.php | 259 ++++++++++++++++++++++++++++++++++++++ lib/BridgeList.php | 126 +++++++++++++++++++ lib/RssBridge.php | 2 + lib/html.php | 300 --------------------------------------------- 5 files changed, 390 insertions(+), 380 deletions(-) create mode 100644 lib/BridgeCard.php create mode 100644 lib/BridgeList.php diff --git a/index.php b/index.php index 4a1e2aa2..e281b018 100644 --- a/index.php +++ b/index.php @@ -95,6 +95,7 @@ try { $whitelist_selection = array_map('strtolower', $whitelist_selection); } + $showInactive = filter_input(INPUT_GET, 'show_inactive', FILTER_VALIDATE_BOOLEAN); $action = array_key_exists('action', $params) ? $params['action'] : null; $bridge = array_key_exists('bridge', $params) ? $params['bridge'] : null; @@ -180,8 +181,8 @@ try { header('Content-Type: text/html'); die(buildBridgeException($e, $bridge)); } - - die; + } else { + echo BridgeList::create($whitelist_selection, $showInactive); } } catch(HttpException $e) { http_response_code($e->getCode()); @@ -190,81 +191,3 @@ try { } catch(\Exception $e) { die($e->getMessage()); } - -$formats = Format::searchInformation(); - -?> - - - - - - - RSS-Bridge - - - - - - - - -

RSS-Bridge

-

·Reconnecting the Web·

-

{$status}

- - - -EOD; - - $activeFoundBridgeCount = 0; - $showInactive = filter_input(INPUT_GET, 'show_inactive', FILTER_VALIDATE_BOOLEAN); - $inactiveBridges = ''; - $bridgeList = Bridge::listBridges(); - foreach($bridgeList as $bridgeName) { - if(Bridge::isWhitelisted($whitelist_selection, strtolower($bridgeName))) { - echo displayBridgeCard($bridgeName, $formats); - $activeFoundBridgeCount++; - } elseif($showInactive) { - // inactive bridges - $inactiveBridges .= displayBridgeCard($bridgeName, $formats, false) . PHP_EOL; - } - } - echo $inactiveBridges; - ?> - - - diff --git a/lib/BridgeCard.php b/lib/BridgeCard.php new file mode 100644 index 00000000..d68e49c0 --- /dev/null +++ b/lib/BridgeCard.php @@ -0,0 +1,259 @@ +' + . $name + . '' + . PHP_EOL; + } + + return $buttons; + } + + private static function getFormHeader($bridgeName, $isHttps = false) { + $form = << + + +EOD; + + if(!$isHttps) { + $form .= '
Warning : +This bridge is not fetching its content through a secure connection
'; + } + + return $form; + } + + private static function getForm($bridgeName, + $formats, + $isActive = false, + $isHttps = false, + $parameterName = '', + $parameters = array()) { + $form = BridgeCard::getFormHeader($bridgeName, $isHttps); + + foreach($parameters as $id => $inputEntry) { + if(!isset($inputEntry['exampleValue'])) + $inputEntry['exampleValue'] = ''; + + if(!isset($inputEntry['defaultValue'])) + $inputEntry['defaultValue'] = ''; + + $idArg = 'arg-' + . urlencode($bridgeName) + . '-' + . urlencode($parameterName) + . '-' + . urlencode($id); + + $form .= '' + . PHP_EOL; + + if(!isset($inputEntry['type']) || $inputEntry['type'] === 'text') { + $form .= BridgeCard::getTextInput($inputEntry, $idArg, $id); + } elseif($inputEntry['type'] === 'number') { + $form .= BridgeCard::getNumberInput($inputEntry, $idArg, $id); + } else if($inputEntry['type'] === 'list') { + $form .= BridgeCard::getListInput($inputEntry, $idArg, $id); + } elseif($inputEntry['type'] === 'checkbox') { + $form .= BridgeCard::getCheckboxInput($inputEntry, $idArg, $id); + } + } + + if($isActive) { + $form .= BridgeCard::buildFormatButtons($formats); + } else { + $form .= 'Inactive'; + } + + return $form . '' . PHP_EOL; + } + + private static function getInputAttributes($entry) { + $retVal = ''; + + if(isset($entry['required']) && $entry['required'] === true) + $retVal .= ' required'; + + if(isset($entry['pattern'])) + $retVal .= ' pattern="' . $entry['pattern'] . '"'; + + if(isset($entry['title'])) + $retVal .= ' title="' . filter_var($entry['title'], FILTER_SANITIZE_STRING) . '"'; + + return $retVal; + } + + private static function getTextInput($entry, $id, $name) { + return '
' + . PHP_EOL; + } + + private static function getNumberInput($entry, $id, $name) { + return '
' + . PHP_EOL; + } + + private static function getListInput($entry, $id, $name) { + $list = '
'; + + return $list; + } + + private static function getCheckboxInput($entry, $id, $name) { + return '
' + . PHP_EOL; + } + + static function displayBridgeCard($bridgeName, $formats, $isActive = true){ + + $bridge = Bridge::create($bridgeName); + + if($bridge == false) + return ''; + + $isHttps = strpos($bridge->getURI(), 'https') === 0; + + $uri = $bridge->getURI(); + $name = $bridge->getName(); + $description = $bridge->getDescription(); + $parameters = $bridge->getParameters(); + + if(defined('PROXY_URL') && PROXY_BYBRIDGE) { + $parameters['global']['_noproxy'] = array( + 'name' => 'Disable proxy (' . ((defined('PROXY_NAME') && PROXY_NAME) ? PROXY_NAME : PROXY_URL) . ')', + 'type' => 'checkbox' + ); + } + + if(CUSTOM_CACHE_TIMEOUT) { + $parameters['global']['_cache_timeout'] = array( + 'name' => 'Cache timeout in seconds', + 'type' => 'number', + 'defaultValue' => $bridge->getCacheTimeout() + ); + } + + $card = << +

{$name}

+

{$description}

+ + +CARD; + + // If we don't have any parameter for the bridge, we print a generic form to load it. + if(count($parameters) === 0 + || count($parameters) === 1 && array_key_exists('global', $parameters)) { + + $card .= BridgeCard::getForm($bridgeName, $formats, $isActive, $isHttps); + + } else { + + foreach($parameters as $parameterName => $parameter) { + if(!is_numeric($parameterName) && $parameterName === 'global') + continue; + + if(array_key_exists('global', $parameters)) + $parameter = array_merge($parameter, $parameters['global']); + + if(!is_numeric($parameterName)) + $card .= '
' . $parameterName . '
' . PHP_EOL; + + $card .= BridgeCard::getForm($bridgeName, $formats, $isActive, $isHttps, $parameterName, $parameter); + } + + } + + $card .= ''; + $card .= '

' . $bridge->getMaintainer() . '

'; + $card .= ''; + + return $card; + } +} diff --git a/lib/BridgeList.php b/lib/BridgeList.php new file mode 100644 index 00000000..e3f55cc2 --- /dev/null +++ b/lib/BridgeList.php @@ -0,0 +1,126 @@ + + + + + RSS-Bridge + + + + + +EOD; + } + + private static function getBridges($whitelist, $showInactive, &$totalBridges, &$totalActiveBridges) { + + $body = ''; + $totalActiveBridges = 0; + $inactiveBridges = ''; + + $bridgeList = Bridge::listBridges(); + $formats = Format::searchInformation(); + + $totalBridges = count($bridgeList); + + foreach($bridgeList as $bridgeName) { + + if(Bridge::isWhitelisted($whitelist, strtolower($bridgeName))) { + + $body .= BridgeCard::displayBridgeCard($bridgeName, $formats); + $totalActiveBridges++; + + } elseif($showInactive) { + + // inactive bridges + $inactiveBridges .= BridgeCard::displayBridgeCard($bridgeName, $formats, false) . PHP_EOL; + + } + + } + + $body .= $inactiveBridges; + + return $body; + } + + private static function getHeader() { + $status = ''; + + if(defined('DEBUG') && DEBUG === true) { + $status .= 'debug mode active'; + } + + return << +

RSS-Bridge

+

·Reconnecting the Web·

+

{$status}

+ +EOD; + } + + private static function getSearchbar() { + $query = filter_input(INPUT_GET, 'q'); + + return << +

Search

+ + +EOD; + } + + private static function getFooter($totalBridges, $totalActiveBridges, $showInactive) { + $version = Configuration::getVersion(); + + $inactive = ''; + + if($totalActiveBridges !== $totalBridges) { + + if(!$showInactive) { + $inactive = '
'; + } else { + $inactive = '
'; + } + + } + + return << + RSS-Bridge ~ Public Domain
+

{$version}

+ {$totalActiveBridges}/{$totalBridges} active bridges.
+ {$inactive} + +EOD; + } + + static function create($whitelist, $showInactive = true) { + + $totalBridges = 0; + $totalActiveBridges = 0; + + return '' + . BridgeList::getHead() + . '' + . BridgeList::getHeader() + . BridgeList::getSearchbar() + . BridgeList::getBridges($whitelist, $showInactive, $totalBridges, $totalActiveBridges) + . BridgeList::getFooter($totalBridges, $totalActiveBridges, $showInactive) + . ''; + + } +} diff --git a/lib/RssBridge.php b/lib/RssBridge.php index 8d0ef902..b570076d 100644 --- a/lib/RssBridge.php +++ b/lib/RssBridge.php @@ -16,6 +16,8 @@ require __DIR__ . '/FeedExpander.php'; require __DIR__ . '/Cache.php'; require __DIR__ . '/Authentication.php'; require __DIR__ . '/Configuration.php'; +require __DIR__ . '/BridgeCard.php'; +require __DIR__ . '/BridgeList.php'; require __DIR__ . '/validation.php'; require __DIR__ . '/html.php'; diff --git a/lib/html.php b/lib/html.php index 297ab801..3214eef8 100644 --- a/lib/html.php +++ b/lib/html.php @@ -1,304 +1,4 @@ ' - . $name - . '' - . PHP_EOL; - } - - return $buttons; - }; - - $getFormHeader = function($bridgeName){ - return << - - -EOD; - }; - - $bridge = Bridge::create($bridgeName); - - if($bridge == false) - return ''; - - $HTTPSWarning = ''; - if(strpos($bridge->getURI(), 'https') !== 0) { - - $HTTPSWarning = '
Warning : - This bridge is not fetching its content through a secure connection
'; - - } - - $name = '' . $bridge->getName() . ''; - $description = $bridge->getDescription(); - - $card = << -

{$name}

-

- {$description} -

- - -CARD; - - // If we don't have any parameter for the bridge, we print a generic form to load it. - if(count($bridge->getParameters()) == 0) { - - $card .= $getFormHeader($bridgeName); - $card .= $HTTPSWarning; - - if($isActive) { - if(defined('PROXY_URL') && PROXY_BYBRIDGE) { - $idArg = 'arg-' - . urlencode($bridgeName) - . '-' - . urlencode('proxyoff') - . '-' - . urlencode('_noproxy'); - - $card .= '' - . PHP_EOL; - - $card .= '
' - . PHP_EOL; - } if(CUSTOM_CACHE_TIMEOUT) { - $idArg = 'arg-' - . urlencode($bridgeName) - . '-' - . urlencode('_cache_timeout'); - - $card .= '' - . PHP_EOL; - - $card .= '
' - . PHP_EOL; - } - $card .= $getHelperButtonsFormat($formats); - } else { - $card .= 'Inactive'; - } - - $card .= '' . PHP_EOL; - } - - $hasGlobalParameter = array_key_exists('global', $bridge->getParameters()); - - if($hasGlobalParameter) { - $globalParameters = $bridge->getParameters()['global']; - } - - foreach($bridge->getParameters() as $parameterName => $parameter) { - if(!is_numeric($parameterName) && $parameterName == 'global') - continue; - - if($hasGlobalParameter) - $parameter = array_merge($parameter, $globalParameters); - - if(!is_numeric($parameterName)) - $card .= '
' . $parameterName . '
' . PHP_EOL; - - $card .= $getFormHeader($bridgeName); - $card .= $HTTPSWarning; - - foreach($parameter as $id => $inputEntry) { - $additionalInfoString = ''; - - if(isset($inputEntry['required']) && $inputEntry['required'] === true) - $additionalInfoString .= ' required'; - - if(isset($inputEntry['pattern'])) - $additionalInfoString .= ' pattern="' . $inputEntry['pattern'] . '"'; - - if(isset($inputEntry['title'])) - $additionalInfoString .= ' title="' . $inputEntry['title'] . '"'; - - if(!isset($inputEntry['exampleValue'])) - $inputEntry['exampleValue'] = ''; - - if(!isset($inputEntry['defaultValue'])) - $inputEntry['defaultValue'] = ''; - - $idArg = 'arg-' - . urlencode($bridgeName) - . '-' - . urlencode($parameterName) - . '-' - . urlencode($id); - - $card .= '' - . PHP_EOL; - - if(!isset($inputEntry['type']) || $inputEntry['type'] == 'text') { - $card .= '
' - . PHP_EOL; - } elseif($inputEntry['type'] == 'number') { - $card .= '
' - . PHP_EOL; - } else if($inputEntry['type'] == 'list') { - $card .= '
'; - } elseif($inputEntry['type'] == 'checkbox') { - if($inputEntry['defaultValue'] === 'checked') - $card .= '
' - . PHP_EOL; - else - $card .= '
' - . PHP_EOL; - } - } - - if($isActive) { - if(defined('PROXY_URL') && PROXY_BYBRIDGE) { - $idArg = 'arg-' - . urlencode($bridgeName) - . '-' - . urlencode('proxyoff') - . '-' - . urlencode('_noproxy'); - - $card .= '' - . PHP_EOL; - - $card .= '
' - . PHP_EOL; - } if(CUSTOM_CACHE_TIMEOUT) { - $idArg = 'arg-' - . urlencode($bridgeName) - . '-' - . urlencode('_cache_timeout'); - - $card .= '' - . PHP_EOL; - - $card .= '
' - . PHP_EOL; - } - $card .= $getHelperButtonsFormat($formats); - } else { - $card .= 'Inactive'; - } - $card .= '' . PHP_EOL; - } - - $card .= ''; - $card .= '

' . $bridge->getMaintainer() . '

'; - $card .= ''; - - return $card; -} - function sanitize($textToSanitize, $removedTags = array('script', 'iframe', 'input', 'form'), $keptAttributes = array('title', 'href', 'src'),