mirror of
https://github.com/RSS-Bridge/rss-bridge.git
synced 2025-10-20 02:56:06 +02:00
fix: introduce system env var, remove debug mode (#4658)
* fix: introduce system env var * docs * docs
This commit is contained in:
@@ -36,11 +36,10 @@ final class Configuration
|
||||
}
|
||||
|
||||
if (file_exists(__DIR__ . '/../DEBUG')) {
|
||||
// The debug mode has been moved to config. Preserve existing installs which has this DEBUG file.
|
||||
self::setConfig('system', 'enable_debug_mode', true);
|
||||
$debug = trim(file_get_contents(__DIR__ . '/../DEBUG'));
|
||||
if ($debug) {
|
||||
self::setConfig('system', 'debug_mode_whitelist', explode("\n", str_replace("\r", '', $debug)));
|
||||
if ($debug === '') {
|
||||
self::setConfig('system', 'env', 'dev');
|
||||
self::setConfig('cache', 'type', 'array');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -82,8 +81,8 @@ final class Configuration
|
||||
}
|
||||
}
|
||||
|
||||
if (Debug::isEnabled()) {
|
||||
self::setConfig('cache', 'type', 'array');
|
||||
if (!in_array(self::getConfig('system', 'env'), ['dev', 'prod'])) {
|
||||
self::throwConfigError('system', 'env', 'Must be dev or prod');
|
||||
}
|
||||
|
||||
if (!is_array(self::getConfig('system', 'enabled_bridges'))) {
|
||||
@@ -97,13 +96,6 @@ final class Configuration
|
||||
self::throwConfigError('system', 'timezone');
|
||||
}
|
||||
|
||||
if (!is_bool(self::getConfig('system', 'enable_debug_mode'))) {
|
||||
self::throwConfigError('system', 'enable_debug_mode', 'Is not a valid Boolean');
|
||||
}
|
||||
if (!is_array(self::getConfig('system', 'debug_mode_whitelist') ?: [])) {
|
||||
self::throwConfigError('system', 'debug_mode_whitelist', 'Is not a valid array');
|
||||
}
|
||||
|
||||
if (!is_string(self::getConfig('proxy', 'url'))) {
|
||||
self::throwConfigError('proxy', 'url', 'Is not a valid string');
|
||||
}
|
||||
@@ -199,6 +191,8 @@ final class Configuration
|
||||
|
||||
private static function throwConfigError($section, $key, $message = '')
|
||||
{
|
||||
throw new \Exception("Config [$section] => [$key] is invalid. $message");
|
||||
http_response_code(500);
|
||||
print ("Config [$section] => [$key] is invalid. $message");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
@@ -1,18 +0,0 @@
|
||||
<?php
|
||||
|
||||
class Debug
|
||||
{
|
||||
/**
|
||||
* Convenience function for Configuration::getConfig('system', 'enable_debug_mode')
|
||||
*/
|
||||
public static function isEnabled(): bool
|
||||
{
|
||||
$ip = $_SERVER['REMOTE_ADDR'] ?? 'x.y.z.1';
|
||||
$enableDebugMode = Configuration::getConfig('system', 'enable_debug_mode');
|
||||
$debugModeWhitelist = Configuration::getConfig('system', 'debug_mode_whitelist') ?: [];
|
||||
if ($enableDebugMode && ($debugModeWhitelist === [] || in_array($ip, $debugModeWhitelist))) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
@@ -47,16 +47,16 @@ $container['cache_factory'] = function ($c) {
|
||||
|
||||
$container['logger'] = function () {
|
||||
$logger = new SimpleLogger('rssbridge');
|
||||
if (Debug::isEnabled()) {
|
||||
if (Configuration::getConfig('system', 'env') === 'dev') {
|
||||
$logger->addHandler(new ErrorLogHandler(Logger::DEBUG));
|
||||
} else {
|
||||
$logger->addHandler(new ErrorLogHandler(Logger::INFO));
|
||||
}
|
||||
// Uncomment this for info logging to fs
|
||||
// $logger->addHandler(new StreamHandler('/tmp/rss-bridge.txt', Logger::INFO));
|
||||
// $logger->addHandler(new StreamHandler('/tmp/rss-bridge.log', Logger::INFO));
|
||||
|
||||
// Uncomment this for debug logging to fs
|
||||
// $logger->addHandler(new StreamHandler('/tmp/rss-bridge-debug.txt', Logger::DEBUG));
|
||||
// $logger->addHandler(new StreamHandler('/tmp/rss-bridge-debug.log', Logger::DEBUG));
|
||||
return $logger;
|
||||
};
|
||||
|
||||
|
22
lib/html.php
22
lib/html.php
@@ -15,19 +15,15 @@ function render(string $template, array $context = []): string
|
||||
'level' => 'info',
|
||||
];
|
||||
}
|
||||
if (Debug::isEnabled()) {
|
||||
$debugModeWhitelist = Configuration::getConfig('system', 'debug_mode_whitelist') ?: [];
|
||||
if ($debugModeWhitelist === []) {
|
||||
$context['messages'][] = [
|
||||
'body' => 'Warning : Debug mode is active from any location, make sure only you can access RSS-Bridge.',
|
||||
'level' => 'error'
|
||||
];
|
||||
} else {
|
||||
$context['messages'][] = [
|
||||
'body' => 'Warning : Debug mode is active from your IP address, your requests will bypass the cache.',
|
||||
'level' => 'warning'
|
||||
];
|
||||
}
|
||||
if (Configuration::getConfig('system', 'env') === 'dev') {
|
||||
$context['messages'][] = [
|
||||
'body' => 'System environment: dev',
|
||||
'level' => 'error'
|
||||
];
|
||||
$context['messages'][] = [
|
||||
'body' => sprintf('Cache type: %s', Configuration::getConfig('cache', 'type')),
|
||||
'level' => 'info'
|
||||
];
|
||||
}
|
||||
$context['page'] = render_template($template, $context);
|
||||
return render_template('base.html.php', $context);
|
||||
|
@@ -23,12 +23,10 @@ class HttpException extends \Exception
|
||||
public static function fromResponse(Response $response, string $url): HttpException
|
||||
{
|
||||
$message = sprintf(
|
||||
'%s resulted in %s %s %s',
|
||||
'%s resulted in %s %s',
|
||||
$url,
|
||||
$response->getCode(),
|
||||
$response->getStatusLine(),
|
||||
// If debug, include a part of the response body in the exception message
|
||||
Debug::isEnabled() ? mb_substr($response->getBody(), 0, 500) : '',
|
||||
$response->getStatusLine()
|
||||
);
|
||||
if (CloudFlareException::isCloudFlareResponse($response)) {
|
||||
return new CloudFlareException($message, $response->getCode(), $response);
|
||||
|
Reference in New Issue
Block a user