Skip to content

Commit

Permalink
refactor: frontpage and proxy setting (#4214)
Browse files Browse the repository at this point in the history
  • Loading branch information
dvikan authored Aug 18, 2024
1 parent e9d3a65 commit c0e37bc
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 17 deletions.
1 change: 1 addition & 0 deletions actions/DisplayAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public function __invoke(Request $request): Response
return new Response(render(__DIR__ . '/../templates/error.html.php', ['message' => 'This bridge is not whitelisted']), 400);
}

// Disable proxy (if enabled and per user's request)
if (
Configuration::getConfig('proxy', 'url')
&& Configuration::getConfig('proxy', 'by_bridge')
Expand Down
22 changes: 13 additions & 9 deletions actions/FrontpageAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ final class FrontpageAction implements ActionInterface
{
public function __invoke(Request $request): Response
{
$token = $request->attribute('token');

$messages = [];
$activeBridges = 0;

Expand All @@ -20,19 +22,21 @@ public function __invoke(Request $request): Response
$body = '';
foreach ($bridgeClassNames as $bridgeClassName) {
if ($bridgeFactory->isEnabled($bridgeClassName)) {
$body .= BridgeCard::render($bridgeClassName, $request);
$body .= BridgeCard::render($bridgeClassName, $token);
$activeBridges++;
}
}

// todo: cache this renderered template?
return new Response(render(__DIR__ . '/../templates/frontpage.html.php', [
'messages' => $messages,
'admin_email' => Configuration::getConfig('admin', 'email'),
'admin_telegram' => Configuration::getConfig('admin', 'telegram'),
'bridges' => $body,
'active_bridges' => $activeBridges,
'total_bridges' => count($bridgeClassNames),
$response = new Response(render(__DIR__ . '/../templates/frontpage.html.php', [
'messages' => $messages,
'admin_email' => Configuration::getConfig('admin', 'email'),
'admin_telegram' => Configuration::getConfig('admin', 'telegram'),
'bridges' => $body,
'active_bridges' => $activeBridges,
'total_bridges' => count($bridgeClassNames),
]));

// TODO: The rendered template could be cached, but beware config changes that changes the html
return $response;
}
}
4 changes: 2 additions & 2 deletions config.default.ini.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@
donations = true

[proxy]

; Sets the proxy url (i.e. "tcp://192.168.0.0:32")
; The HTTP proxy to tunnel requests through
; https://curl.se/libcurl/c/CURLOPT_PROXY.html
; "" = Proxy disabled (default)
url = ""

Expand Down
15 changes: 9 additions & 6 deletions lib/BridgeCard.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

final class BridgeCard
{
public static function render(string $bridgeClassName, Request $request): string
public static function render(string $bridgeClassName, ?string $token): string
{
$bridgeFactory = new BridgeFactory();

Expand All @@ -14,10 +14,15 @@ public static function render(string $bridgeClassName, Request $request): string
$description = $bridge->getDescription();
$contexts = $bridge->getParameters();

if (Configuration::getConfig('proxy', 'url') && Configuration::getConfig('proxy', 'by_bridge')) {
// Checkbox for disabling of proxy (if enabled)
if (
Configuration::getConfig('proxy', 'url')
&& Configuration::getConfig('proxy', 'by_bridge')
) {
$proxyName = Configuration::getConfig('proxy', 'name') ?: Configuration::getConfig('proxy', 'url');
$contexts['global']['_noproxy'] = [
'name' => 'Disable proxy (' . (Configuration::getConfig('proxy', 'name') ?: Configuration::getConfig('proxy', 'url')) . ')',
'type' => 'checkbox'
'name' => sprintf('Disable proxy (%s)', $proxyName),
'type' => 'checkbox',
];
}

Expand Down Expand Up @@ -47,8 +52,6 @@ class="bridge-card"
CARD;

$token = $request->attribute('token');

if (count($contexts) === 0) {
// The bridge has zero parameters
$card .= self::renderForm($bridgeClassName, '', [], $token);
Expand Down
1 change: 1 addition & 0 deletions lib/http.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public function request(string $url, array $config = []): Response
if ($config['proxy']) {
curl_setopt($ch, CURLOPT_PROXY, $config['proxy']);
}

if (curl_setopt_array($ch, $config['curl_options']) === false) {
throw new \Exception('Tried to set an illegal curl option');
}
Expand Down

0 comments on commit c0e37bc

Please sign in to comment.