create($bridgeClassName); $uri = $bridge->getURI(); $name = $bridge->getName(); $icon = $bridge->getIcon(); $description = $bridge->getDescription(); $contexts = $bridge->getParameters(); if (Configuration::getConfig('proxy', 'url') && Configuration::getConfig('proxy', 'by_bridge')) { $contexts['global']['_noproxy'] = [ 'name' => 'Disable proxy (' . (Configuration::getConfig('proxy', 'name') ?: Configuration::getConfig('proxy', 'url')) . ')', 'type' => 'checkbox' ]; } if (Configuration::getConfig('cache', 'custom_timeout')) { $contexts['global']['_cache_timeout'] = [ 'name' => 'Cache timeout in seconds', 'type' => 'number', 'defaultValue' => $bridge->getCacheTimeout() ]; } $shortName = $bridge->getShortName(); $card = <<

{$name}

{$description}

CARD; $token = $request->attribute('token'); if (count($contexts) === 0) { // The bridge has zero parameters $card .= self::renderForm($bridgeClassName, '', [], $token); } elseif (count($contexts) === 1 && array_key_exists('global', $contexts)) { // The bridge has a single context with key 'global' $card .= self::renderForm($bridgeClassName, '', $contexts['global'], $token); } else { // The bridge has one or more contexts (named or unnamed) foreach ($contexts as $contextName => $contextParameters) { if ($contextName === 'global') { continue; } if (array_key_exists('global', $contexts)) { // Merge the global parameters into current context $contextParameters = array_merge($contextParameters, $contexts['global']); } if (!is_numeric($contextName)) { // This is a named context $card .= '
' . $contextName . '
' . PHP_EOL; } $card .= self::renderForm($bridgeClassName, $contextName, $contextParameters, $token); } } $card .= sprintf('', $bridgeClassName); if (Configuration::getConfig('admin', 'donations') && $bridge->getDonationURI()) { $card .= sprintf( '

%s ~ Donate

', $bridge->getMaintainer(), $bridge->getDonationURI() ); } else { $card .= sprintf('

%s

', $bridge->getMaintainer()); } $card .= ''; return $card; } private static function renderForm( string $bridgeClassName, string $contextName, array $contextParameters, ?string $token ) { $form = << EOD; if (Configuration::getConfig('authentication', 'token') && $token) { $form .= sprintf('', e($token)); } if (!empty($contextName)) { $form .= sprintf('', $contextName); } if (count($contextParameters) > 0) { $form .= '
'; foreach ($contextParameters as $id => $inputEntry) { if (!isset($inputEntry['exampleValue'])) { $inputEntry['exampleValue'] = ''; } if (!isset($inputEntry['defaultValue'])) { $inputEntry['defaultValue'] = ''; } $idArg = 'arg-' . urlencode($bridgeClassName) . '-' . urlencode($contextName) . '-' . urlencode($id); $inputName = filter_var($inputEntry['name'], FILTER_SANITIZE_FULL_SPECIAL_CHARS); $form .= '' . PHP_EOL; if ( !isset($inputEntry['type']) || $inputEntry['type'] === 'text' ) { $form .= self::getTextInput($inputEntry, $idArg, $id) . "\n"; } elseif ($inputEntry['type'] === 'number') { $form .= self::getNumberInput($inputEntry, $idArg, $id); } elseif ($inputEntry['type'] === 'list') { $form .= self::getListInput($inputEntry, $idArg, $id) . "\n"; } elseif ($inputEntry['type'] === 'checkbox') { $form .= self::getCheckboxInput($inputEntry, $idArg, $id); } else { $foo = 2; // oops? } $infoText = []; $infoTextScript = ''; if (isset($inputEntry['title'])) { $infoText[] = filter_var($inputEntry['title'], FILTER_SANITIZE_FULL_SPECIAL_CHARS); } if ($inputEntry['exampleValue'] !== '') { $infoText[] = "Example (right click to use):\n" . filter_var($inputEntry['exampleValue'], FILTER_SANITIZE_FULL_SPECIAL_CHARS); $infoTextScript = 'rssbridge_use_placeholder_value(this);'; } if (count($infoText) > 0) { $form .= 'i'; } else { $form .= ''; } } $form .= '
'; } $form .= ''; return $form . '' . PHP_EOL; } public static function getTextInput(array $entry, string $id, string $name): string { $defaultValue = filter_var($entry['defaultValue'], FILTER_SANITIZE_FULL_SPECIAL_CHARS); $exampleValue = filter_var($entry['exampleValue'], FILTER_SANITIZE_FULL_SPECIAL_CHARS); $attributes = self::getInputAttributes($entry); return sprintf('', $attributes, $id, $defaultValue, $exampleValue, $name); } public static function getNumberInput(array $entry, string $id, string $name): string { $defaultValue = filter_var($entry['defaultValue'], FILTER_SANITIZE_NUMBER_INT); $exampleValue = filter_var($entry['exampleValue'], FILTER_SANITIZE_NUMBER_INT); $attributes = self::getInputAttributes($entry); return sprintf('' . "\n", $attributes, $id, $defaultValue, $exampleValue, $name); } public static function getListInput(array $entry, string $id, string $name): string { $required = $entry['required'] ?? null; if ($required) { trigger_error('The required attribute is not supported for lists'); unset($entry['required']); } $attributes = self::getInputAttributes($entry); $list = sprintf(''; return $list; } public static function getCheckboxInput(array $entry, string $id, string $name): string { $required = $entry['required'] ?? null; if ($required) { trigger_error('The required attribute is not supported for checkboxes'); unset($entry['required']); } $checked = $entry['defaultValue'] === 'checked' ? 'checked' : ''; $attributes = self::getInputAttributes($entry); return sprintf('' . "\n", $attributes, $id, $name, $checked); } public static function getInputAttributes(array $entry): string { $result = ''; $required = $entry['required'] ?? null; if ($required) { $result .= ' required'; } $pattern = $entry['pattern'] ?? null; if ($pattern) { $result .= ' pattern="' . $pattern . '"'; } return $result; } }