1
0
mirror of https://github.com/RSS-Bridge/rss-bridge.git synced 2025-09-04 21:45:36 +02:00

refactor: general code base refactor (#2950)

* refactor

* fix: bug in previous refactor

* chore: exclude phpcompat sniff due to bug in phpcompat

* fix: do not leak absolute paths

* refactor/fix: batch extensions checking, fix DOS issue
This commit is contained in:
Dag
2022-08-06 22:46:28 +02:00
committed by GitHub
parent b042412416
commit 2bbce8ebef
45 changed files with 679 additions and 827 deletions

View File

@@ -28,23 +28,21 @@ class ConnectivityAction implements ActionInterface
public function __construct()
{
$this->bridgeFactory = new \BridgeFactory();
$this->bridgeFactory = new BridgeFactory();
}
public function execute(array $request)
{
if (!Debug::isEnabled()) {
returnError('This action is only available in debug mode!', 400);
throw new \Exception('This action is only available in debug mode!');
}
if (!isset($request['bridge'])) {
$this->returnEntryPage();
print render_template('connectivity.html.php');
return;
}
$bridgeName = $request['bridge'];
$bridgeClassName = $this->bridgeFactory->sanitizeBridgeName($bridgeName);
$bridgeClassName = $this->bridgeFactory->sanitizeBridgeName($request['bridge']);
if ($bridgeClassName === null) {
throw new \InvalidArgumentException('Bridge name invalid!');
@@ -53,28 +51,12 @@ class ConnectivityAction implements ActionInterface
$this->reportBridgeConnectivity($bridgeClassName);
}
/**
* Generates a report about the bridge connectivity status and sends it back
* to the user.
*
* The report is generated as Json-formatted string in the format
* {
* "bridge": "<bridge-name>",
* "successful": true/false
* }
*
* @param class-string<BridgeInterface> $bridgeClassName Name of the bridge to generate the report for
* @return void
*/
private function reportBridgeConnectivity($bridgeClassName)
{
if (!$this->bridgeFactory->isWhitelisted($bridgeClassName)) {
header('Content-Type: text/html');
returnServerError('Bridge is not whitelisted!');
throw new \Exception('Bridge is not whitelisted!');
}
header('Content-Type: text/json');
$retVal = [
'bridge' => $bridgeClassName,
'successful' => false,
@@ -82,16 +64,9 @@ class ConnectivityAction implements ActionInterface
];
$bridge = $this->bridgeFactory->create($bridgeClassName);
if ($bridge === false) {
echo json_encode($retVal);
return;
}
$curl_opts = [
CURLOPT_CONNECTTIMEOUT => 5
];
try {
$reply = getContents($bridge::URI, [], $curl_opts, true);
@@ -101,45 +76,11 @@ class ConnectivityAction implements ActionInterface
$retVal['http_code'] = 301;
}
}
} catch (Exception $e) {
} catch (\Exception $e) {
$retVal['successful'] = false;
}
echo json_encode($retVal);
}
private function returnEntryPage()
{
echo <<<EOD
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="static/bootstrap.min.css">
<link
rel="stylesheet"
href="https://use.fontawesome.com/releases/v5.6.3/css/all.css"
integrity="sha384-UHRtZLI+pbxtHCWp1t77Bi1L4ZtiqrqD80Kn4Z8NTSRyMA2Fd33n5dQ8lWUE00s/"
crossorigin="anonymous">
<link rel="stylesheet" href="static/connectivity.css">
<script src="static/connectivity.js" type="text/javascript"></script>
</head>
<body>
<div id="main-content" class="container">
<div class="progress">
<div class="progress-bar" role="progressbar" aria-valuenow="75" aria-valuemin="0" aria-valuemax="100"></div>
</div>
<div id="status-message" class="sticky-top alert alert-primary alert-dismissible fade show" role="alert">
<i id="status-icon" class="fas fa-sync"></i>
<span>...</span>
<button type="button" class="close" data-dismiss="alert" aria-label="Close" onclick="stopConnectivityChecks()">
<span aria-hidden="true">&times;</span>
</button>
</div>
<input type="text" class="form-control" id="search" onkeyup="search()" placeholder="Search for bridge..">
</div>
</body>
</html>
EOD;
header('Content-Type: text/json');
print Json::encode($retVal);
}
}