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
|
|
|
|
{
|
2022-07-08 13:54:23 +03:00
|
|
|
private BridgeFactory $bridgeFactory;
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
2022-08-06 23:46:28 +03:00
|
|
|
$this->bridgeFactory = new BridgeFactory();
|
2022-07-08 13:54:23 +03:00
|
|
|
}
|
|
|
|
|
2022-07-08 22:06:14 +03:00
|
|
|
public function execute(array $request)
|
2019-11-01 00:02:38 +03:00
|
|
|
{
|
|
|
|
if (!Debug::isEnabled()) {
|
2022-08-06 23:46:28 +03:00
|
|
|
throw new \Exception('This action is only available in debug mode!');
|
2019-11-01 00:02:38 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-07-08 22:06:14 +03:00
|
|
|
if (!isset($request['bridge'])) {
|
2022-08-06 23:46:28 +03:00
|
|
|
print render_template('connectivity.html.php');
|
2019-11-01 00:02:38 +03:00
|
|
|
return;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-08-06 23:46:28 +03:00
|
|
|
$bridgeClassName = $this->bridgeFactory->sanitizeBridgeName($request['bridge']);
|
2022-07-08 13:54:23 +03:00
|
|
|
|
|
|
|
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
|
|
|
|
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)) {
|
2022-08-06 23:46:28 +03:00
|
|
|
throw new \Exception('Bridge is not whitelisted!');
|
2019-11-01 00:02:38 +03:00
|
|
|
}
|
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);
|
2019-11-01 00:02:38 +03:00
|
|
|
$curl_opts = [
|
|
|
|
CURLOPT_CONNECTTIMEOUT => 5
|
|
|
|
];
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2022-08-06 23:46:28 +03:00
|
|
|
} catch (\Exception $e) {
|
2019-11-01 00:02:38 +03:00
|
|
|
$retVal['successful'] = false;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-08-06 23:46:28 +03:00
|
|
|
header('Content-Type: text/json');
|
|
|
|
print Json::encode($retVal);
|
2019-11-01 00:02:38 +03:00
|
|
|
}
|
|
|
|
}
|