2018-11-10 22:03:03 +03:00
|
|
|
<?php
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-11-15 22:27:32 +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
|
|
|
|
*/
|
2018-11-10 22:03:03 +03:00
|
|
|
|
2018-11-10 22:44:23 +03:00
|
|
|
/**
|
|
|
|
* Implements functions for debugging purposes. Debugging can be enabled by
|
2018-11-15 22:27:32 +03:00
|
|
|
* placing a file named DEBUG in {@see PATH_ROOT}.
|
2018-11-10 22:44:23 +03:00
|
|
|
*
|
|
|
|
* The file specifies a whitelist of IP addresses on which debug mode will be
|
|
|
|
* enabled. An empty file enables debug mode for everyone (highly discouraged
|
|
|
|
* for public servers!). Each line in the file specifies one client in the
|
|
|
|
* whitelist. For example:
|
|
|
|
*
|
2018-11-15 22:27:32 +03:00
|
|
|
* * `192.168.1.72`
|
|
|
|
* * `127.0.0.1`
|
|
|
|
* * `::1`
|
2018-11-10 22:44:23 +03:00
|
|
|
*
|
|
|
|
* Notice: If you are running RSS-Bridge on your local machine, you need to add
|
2018-11-15 22:27:32 +03:00
|
|
|
* localhost (either `127.0.0.1` for IPv4 or `::1` for IPv6) to your whitelist!
|
2018-11-10 22:44:23 +03:00
|
|
|
*
|
|
|
|
* Warning: In debug mode your server may display sensitive information! For
|
|
|
|
* security reasons it is recommended to whitelist only specific IP addresses.
|
|
|
|
*/
|
2018-11-10 22:03:03 +03:00
|
|
|
class Debug
|
|
|
|
{
|
2018-11-10 22:44:23 +03:00
|
|
|
/**
|
|
|
|
* Indicates if debug mode is enabled.
|
2018-11-15 22:27:32 +03:00
|
|
|
*
|
|
|
|
* Do not access this property directly!
|
|
|
|
* Use {@see Debug::isEnabled()} instead.
|
|
|
|
*
|
|
|
|
* @var bool
|
2018-11-10 22:44:23 +03:00
|
|
|
*/
|
|
|
|
private static $enabled = false;
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-11-10 22:44:23 +03:00
|
|
|
/**
|
2018-11-15 22:27:32 +03:00
|
|
|
* Indicates if debug mode is secure.
|
|
|
|
*
|
|
|
|
* Do not access this property directly!
|
|
|
|
* Use {@see Debug::isSecure()} instead.
|
|
|
|
*
|
|
|
|
* @var bool
|
2018-11-10 22:44:23 +03:00
|
|
|
*/
|
|
|
|
private static $secure = false;
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-11-10 22:44:23 +03:00
|
|
|
/**
|
2018-11-15 22:27:32 +03:00
|
|
|
* Returns true if debug mode is enabled
|
|
|
|
*
|
|
|
|
* If debug mode is enabled, sets `display_errors = 1` and `error_reporting = E_ALL`
|
|
|
|
*
|
|
|
|
* @return bool True if enabled.
|
2018-11-10 22:44:23 +03:00
|
|
|
*/
|
|
|
|
public static function isEnabled()
|
|
|
|
{
|
|
|
|
static $firstCall = true; // Initialized on first call
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-08-06 23:46:28 +03:00
|
|
|
if ($firstCall && file_exists(__DIR__ . '/../DEBUG')) {
|
|
|
|
$debug_whitelist = trim(file_get_contents(__DIR__ . '/../DEBUG'));
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-11-15 22:28:26 +03:00
|
|
|
self::$enabled = empty($debug_whitelist) || in_array(
|
|
|
|
$_SERVER['REMOTE_ADDR'],
|
2018-11-10 22:44:23 +03:00
|
|
|
explode("\n", str_replace("\r", '', $debug_whitelist))
|
|
|
|
);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-11-15 22:28:26 +03:00
|
|
|
if (self::$enabled) {
|
|
|
|
self::$secure = !empty($debug_whitelist);
|
2018-11-10 22:44:23 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-11-10 22:44:23 +03:00
|
|
|
$firstCall = false; // Skip check on next call
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-11-15 22:28:26 +03:00
|
|
|
return self::$enabled;
|
2018-11-10 22:44:23 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-11-10 22:44:23 +03:00
|
|
|
/**
|
2018-11-15 22:27:32 +03:00
|
|
|
* Returns true if debug mode is enabled only for specific IP addresses.
|
2018-11-10 22:44:23 +03:00
|
|
|
*
|
2018-11-15 22:27:32 +03:00
|
|
|
* Notice: The security flag is set by {@see Debug::isEnabled()}. If this
|
|
|
|
* function is called before {@see Debug::isEnabled()}, the default value is
|
|
|
|
* false!
|
2018-11-10 22:44:23 +03:00
|
|
|
*
|
2018-11-15 22:27:32 +03:00
|
|
|
* @return bool True if debug mode is secure
|
2018-11-10 22:44:23 +03:00
|
|
|
*/
|
|
|
|
public static function isSecure()
|
|
|
|
{
|
2018-11-15 22:28:26 +03:00
|
|
|
return self::$secure;
|
2018-11-10 22:44:23 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-09-08 20:07:57 +03:00
|
|
|
public static function log($message)
|
2018-11-10 22:03:03 +03:00
|
|
|
{
|
2018-11-15 22:28:26 +03:00
|
|
|
if (!self::isEnabled()) {
|
2018-11-10 22:03:03 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3);
|
2022-09-08 20:07:57 +03:00
|
|
|
$lastFrame = end($backtrace);
|
|
|
|
$file = trim_path_prefix($lastFrame['file']);
|
|
|
|
$line = $lastFrame['line'];
|
|
|
|
$class = $lastFrame['class'] ?? '';
|
|
|
|
$function = $lastFrame['function'];
|
|
|
|
$text = sprintf('%s:%s %s->%s() %s', $file, $line, $class, $function, $message);
|
|
|
|
Logger::info($text);
|
2018-11-10 22:03:03 +03:00
|
|
|
}
|
|
|
|
}
|