2019-11-01 00:02:38 +03:00
|
|
|
<?php
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-11-01 00:02:38 +03:00
|
|
|
/**
|
|
|
|
* This file is part of RSS-Bridge, a PHP project capable of generating RSS and
|
|
|
|
* Atom feeds for websites that don't have one.
|
|
|
|
*
|
|
|
|
* For the full license information, please view the UNLICENSE file distributed
|
|
|
|
* with this source code.
|
|
|
|
*
|
|
|
|
* @package Core
|
|
|
|
* @license http://unlicense.org/ UNLICENSE
|
|
|
|
* @link https://github.com/rss-bridge/rss-bridge
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the website for a given bridge is reachable.
|
|
|
|
*
|
|
|
|
* **Remarks**
|
|
|
|
* - This action is only available in debug mode.
|
|
|
|
* - Returns the bridge status as Json-formatted string.
|
|
|
|
* - Returns an error if the bridge is not whitelisted.
|
|
|
|
* - Returns a responsive web page that automatically checks all whitelisted
|
|
|
|
* bridges (using JavaScript) if no bridge is specified.
|
|
|
|
*/
|
2022-06-22 19:30:37 +03:00
|
|
|
class ConnectivityAction implements ActionInterface
|
|
|
|
{
|
|
|
|
public $userData = [];
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-07-08 13:54:23 +03:00
|
|
|
private BridgeFactory $bridgeFactory;
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->bridgeFactory = new \BridgeFactory();
|
|
|
|
}
|
|
|
|
|
2019-11-01 00:02:38 +03:00
|
|
|
public function execute()
|
|
|
|
{
|
|
|
|
if (!Debug::isEnabled()) {
|
2022-05-01 22:35:52 +03:00
|
|
|
returnError('This action is only available in debug mode!', 400);
|
2019-11-01 00:02:38 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-11-01 00:02:38 +03:00
|
|
|
if (!isset($this->userData['bridge'])) {
|
|
|
|
$this->returnEntryPage();
|
|
|
|
return;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-11-01 00:02:38 +03:00
|
|
|
$bridgeName = $this->userData['bridge'];
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-07-08 13:54:23 +03:00
|
|
|
$bridgeClassName = $this->bridgeFactory->sanitizeBridgeName($bridgeName);
|
|
|
|
|
|
|
|
if ($bridgeClassName === null) {
|
|
|
|
throw new \InvalidArgumentException('Bridge name invalid!');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->reportBridgeConnectivity($bridgeClassName);
|
2019-11-01 00:02:38 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-11-01 00:02:38 +03:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
* }
|
|
|
|
*
|
2022-07-08 13:54:23 +03:00
|
|
|
* @param class-string<BridgeInterface> $bridgeClassName Name of the bridge to generate the report for
|
2019-11-01 00:02:38 +03:00
|
|
|
* @return void
|
|
|
|
*/
|
2022-07-08 13:54:23 +03:00
|
|
|
private function reportBridgeConnectivity($bridgeClassName)
|
2019-11-01 00:02:38 +03:00
|
|
|
{
|
2022-07-08 13:54:23 +03:00
|
|
|
if (!$this->bridgeFactory->isWhitelisted($bridgeClassName)) {
|
2019-11-01 00:02:38 +03:00
|
|
|
header('Content-Type: text/html');
|
|
|
|
returnServerError('Bridge is not whitelisted!');
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-11-01 00:02:38 +03:00
|
|
|
header('Content-Type: text/json');
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-11-01 00:02:38 +03:00
|
|
|
$retVal = [
|
2022-07-08 13:54:23 +03:00
|
|
|
'bridge' => $bridgeClassName,
|
2019-11-01 00:02:38 +03:00
|
|
|
'successful' => false,
|
|
|
|
'http_code' => 200,
|
|
|
|
];
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-07-08 13:54:23 +03:00
|
|
|
$bridge = $this->bridgeFactory->create($bridgeClassName);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-11-01 00:02:38 +03:00
|
|
|
if ($bridge === false) {
|
|
|
|
echo json_encode($retVal);
|
|
|
|
return;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-11-01 00:02:38 +03:00
|
|
|
$curl_opts = [
|
|
|
|
CURLOPT_CONNECTTIMEOUT => 5
|
|
|
|
];
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-11-01 00:02:38 +03:00
|
|
|
try {
|
|
|
|
$reply = getContents($bridge::URI, [], $curl_opts, true);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-05-18 00:46:37 +03:00
|
|
|
if ($reply['code'] === 200) {
|
2019-11-01 00:02:38 +03:00
|
|
|
$retVal['successful'] = true;
|
2022-05-18 00:46:37 +03:00
|
|
|
if (strpos(implode('', $reply['status_lines']), '301 Moved Permanently')) {
|
|
|
|
$retVal['http_code'] = 301;
|
2019-11-01 00:02:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$retVal['successful'] = false;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-11-01 00:02:38 +03:00
|
|
|
echo json_encode($retVal);
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-11-01 00:02:38 +03:00
|
|
|
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">×</span>
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
<input type="text" class="form-control" id="search" onkeyup="search()" placeholder="Search for bridge..">
|
|
|
|
</div>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
EOD;
|
|
|
|
}
|
|
|
|
}
|