From 4f75591060d95208a301bc6bf460d875631b29cc Mon Sep 17 00:00:00 2001 From: Dag Date: Fri, 1 Jul 2022 15:10:30 +0200 Subject: Reformat codebase v4 (#2872) Reformat code base to PSR12 Co-authored-by: rssbridge --- lib/Debug.php | 184 +++++++++++++++++++++++++++++----------------------------- 1 file changed, 93 insertions(+), 91 deletions(-) (limited to 'lib/Debug.php') diff --git a/lib/Debug.php b/lib/Debug.php index f912fb3b..75bf5f33 100644 --- a/lib/Debug.php +++ b/lib/Debug.php @@ -1,4 +1,5 @@ ') . '->' - . $calling['function'] . ' - ' - . $text; - - error_log($message); - } +class Debug +{ + /** + * Indicates if debug mode is enabled. + * + * Do not access this property directly! + * Use {@see Debug::isEnabled()} instead. + * + * @var bool + */ + private static $enabled = false; + + /** + * Indicates if debug mode is secure. + * + * Do not access this property directly! + * Use {@see Debug::isSecure()} instead. + * + * @var bool + */ + private static $secure = false; + + /** + * 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. + */ + public static function isEnabled() + { + static $firstCall = true; // Initialized on first call + + if ($firstCall && file_exists(PATH_ROOT . 'DEBUG')) { + $debug_whitelist = trim(file_get_contents(PATH_ROOT . 'DEBUG')); + + self::$enabled = empty($debug_whitelist) || in_array( + $_SERVER['REMOTE_ADDR'], + explode("\n", str_replace("\r", '', $debug_whitelist)) + ); + + if (self::$enabled) { + ini_set('display_errors', '1'); + error_reporting(E_ALL); + + self::$secure = !empty($debug_whitelist); + } + + $firstCall = false; // Skip check on next call + } + + return self::$enabled; + } + + /** + * Returns true if debug mode is enabled only for specific IP addresses. + * + * Notice: The security flag is set by {@see Debug::isEnabled()}. If this + * function is called before {@see Debug::isEnabled()}, the default value is + * false! + * + * @return bool True if debug mode is secure + */ + public static function isSecure() + { + return self::$secure; + } + + /** + * Adds a debug message to error_log if debug mode is enabled + * + * @param string $text The message to add to error_log + */ + public static function log($text) + { + if (!self::isEnabled()) { + return; + } + + $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3); + $calling = end($backtrace); + $message = $calling['file'] . ':' + . $calling['line'] . ' class ' + . (isset($calling['class']) ? $calling['class'] : '') . '->' + . $calling['function'] . ' - ' + . $text; + + error_log($message); + } } -- cgit v1.2.3