From f71deed544b93d1a397d1cec41c1987ba5186e38 Mon Sep 17 00:00:00 2001 From: Roman Kelesidis Date: Sun, 8 Dec 2024 21:28:35 +0700 Subject: [PATCH] Bring back `DBG_USER` (old debug method), fixed bugsnag handler (#1701) * Rollback some changes * Updated * Updated * Updated * Update SqlDb.php * Updated * Update Ajax.php * Update Redis.php * Update Redis.php * Update Memcached.php * Update File.php * Updated * Updated * Update CHANGELOG.md * Update Sqlite.php * Update SqlDb.php * Update common.php * Update Dev.php * Update config.php * Update User.php * Update defines.php * Updated * Update User.php * Update User.php * Update User.php * Update User.php * Update config.php * Update Dev.php * Updated * Update Dev.php * Update Dev.php * Update SqlDb.php * Revert "Update SqlDb.php" This reverts commit d7c05d85ec84bd7c74c12ae905ec3a9c26399c69. * Update Dev.php * Update Dev.php * Update Dev.php * Update Dev.php * Update CHANGELOG.md --- .env.example | 1 - CHANGELOG.md | 1 + common.php | 3 +- library/config.php | 4 + library/defines.php | 3 + library/includes/page_footer.php | 4 +- library/includes/page_footer_dev.php | 2 +- src/Ajax.php | 13 +-- src/Dev.php | 152 ++++++++++++--------------- src/Legacy/Cache/APCu.php | 6 +- src/Legacy/Cache/Common.php | 6 +- src/Legacy/Cache/File.php | 6 +- src/Legacy/Cache/Memcached.php | 6 +- src/Legacy/Cache/Redis.php | 6 +- src/Legacy/Cache/Sqlite.php | 6 +- src/Legacy/Common/User.php | 32 +++++- src/Legacy/Datastore/APCu.php | 6 +- src/Legacy/Datastore/Common.php | 6 +- src/Legacy/Datastore/File.php | 6 +- src/Legacy/Datastore/Memcached.php | 6 +- src/Legacy/Datastore/Redis.php | 6 +- src/Legacy/Datastore/Sqlite.php | 6 +- src/Legacy/SqlDb.php | 14 ++- 23 files changed, 160 insertions(+), 141 deletions(-) diff --git a/.env.example b/.env.example index f47c87658..5c2011f70 100644 --- a/.env.example +++ b/.env.example @@ -1,5 +1,4 @@ # Common params -APP_ENV=local APP_CRON_ENABLED=true APP_DEMO_MODE=false diff --git a/CHANGELOG.md b/CHANGELOG.md index d7b0c0bfc..be4d4f09c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ - Show torrent unregister action in log actions [\#1696](https://github.com/torrentpier/torrentpier/pull/1696) ([belomaxorka](https://github.com/belomaxorka)) - Show torrent status changes in actions log [\#1688](https://github.com/torrentpier/torrentpier/pull/1688) ([belomaxorka](https://github.com/belomaxorka)) - Show torrent type (gold / silver) changes in actions log [\#1689](https://github.com/torrentpier/torrentpier/pull/1689) ([belomaxorka](https://github.com/belomaxorka)) +- Bring back `DBG_USER` (old debug method), fixed bugsnag handler [\#1701](https://github.com/torrentpier/torrentpier/pull/1701) ([belomaxorka](https://github.com/belomaxorka)) - Merged some fixes from `new-attachments` branch [\#1700](https://github.com/torrentpier/torrentpier/pull/1700) ([belomaxorka](https://github.com/belomaxorka)) - Changed database encoding to `utf8mb4_unicode_ci` [\#1684](https://github.com/torrentpier/torrentpier/pull/1684) ([belomaxorka](https://github.com/belomaxorka)) - Demo mode: Save user language in cookies [\#1584](https://github.com/torrentpier/torrentpier/pull/1584) ([belomaxorka](https://github.com/belomaxorka)) diff --git a/common.php b/common.php index 15db02974..30e818a17 100644 --- a/common.php +++ b/common.php @@ -89,7 +89,8 @@ if (is_file(BB_PATH . '/library/config.local.php')) { /** * Error reporting */ -$debug = new \TorrentPier\Dev(); +define('DBG_USER', isset($_COOKIE[COOKIE_DBG])); +(new \TorrentPier\Dev()); /** * Server variables initialize diff --git a/library/config.php b/library/config.php index 24f874383..7fb327b88 100644 --- a/library/config.php +++ b/library/config.php @@ -484,6 +484,10 @@ $bb_cfg['telegram_sender'] = [ ]; // Special users +$bb_cfg['dbg_users'] = [ + // Syntax: 'user_id' => 'username' + 2 => 'admin', +]; $bb_cfg['unlimited_users'] = [ // Syntax: 'user_id' => 'username' 2 => 'admin', diff --git a/library/defines.php b/library/defines.php index cad9a50d0..f087efa17 100644 --- a/library/defines.php +++ b/library/defines.php @@ -35,6 +35,7 @@ define('UPDATER_FILE', INT_DATA_DIR . '/updater.json'); define('API_IP_URL', 'https://freeipapi.com/api/json/'); define('CHECKSUMS_FILE', INT_DATA_DIR . '/checksums.md5'); define('RESTORE_CORRUPT_CONFIRM_FILE', INT_DATA_DIR . '/rescorrupt.integrity'); +define('COOKIE_DBG', 'bb_dbg'); // Templates define('ADMIN_TPL_DIR', TEMPLATES_DIR . '/admin/'); @@ -68,6 +69,8 @@ define('LOG_MAX_SIZE', 1048576); // bytes // Error reporting ini_set('error_reporting', E_ALL); // PHP error reporting mode | https://www.php.net/manual/en/errorfunc.constants.php +ini_set('display_errors', 1); +ini_set('display_startup_errors', 1); define('MYSQLI_ERROR_REPORTING', MYSQLI_REPORT_ERROR); // MySQL error reporting mode | https://www.php.net/manual/mysqli-driver.report-mode.php ini_set('log_errors', 1); // Enable logging (For native & Whoops) ini_set('error_log', LOG_DIR . '/php_errors.log'); // path to log file enabled only if log_errors == 1 (native) diff --git a/library/includes/page_footer.php b/library/includes/page_footer.php index d4a8cd0d4..6dcb2d01f 100644 --- a/library/includes/page_footer.php +++ b/library/includes/page_footer.php @@ -11,7 +11,7 @@ if (!defined('BB_ROOT')) { die(basename(__FILE__)); } -global $bb_cfg, $debug, $userdata, $template, $DBS, $lang; +global $bb_cfg, $userdata, $template, $DBS, $lang; if (!empty($template)) { $template->assign_vars([ @@ -25,7 +25,7 @@ if (!empty($template)) { $template->pparse('page_footer'); } -$show_dbg_info = (!$debug->isProduction && !(isset($_GET['pane']) && $_GET['pane'] == 'left')); +$show_dbg_info = (DBG_USER && !(isset($_GET['pane']) && $_GET['pane'] == 'left')); if (!$bb_cfg['gzip_compress']) { flush(); diff --git a/library/includes/page_footer_dev.php b/library/includes/page_footer_dev.php index c8dac36d8..54be9567d 100644 --- a/library/includes/page_footer_dev.php +++ b/library/includes/page_footer_dev.php @@ -71,7 +71,7 @@ if (!empty($_COOKIE['explain'])) { } } -$sql_log = !empty($_COOKIE['sql_log']) ? $debug->getSqlLog() : false; +$sql_log = !empty($_COOKIE['sql_log']) ? \TorrentPier\Dev::getSqlLog() : false; if ($sql_log) { echo '
' . $sql_log . '

'; diff --git a/src/Ajax.php b/src/Ajax.php index 771449b0e..22e1764df 100644 --- a/src/Ajax.php +++ b/src/Ajax.php @@ -174,11 +174,10 @@ class Ajax */ public function send(): void { - global $debug; $this->response['action'] = $this->action; - if ($debug->sqlDebugAllowed()) { - $this->response['sql_log'] = $debug->getSqlLog(); + if (Dev::sqlDebugAllowed()) { + $this->response['sql_log'] = Dev::getSqlLog(); } // sending output will be handled by $this->ob_handler() @@ -194,12 +193,8 @@ class Ajax */ public function ob_handler($contents): string { - global $debug; - - if (!$debug->isProduction) { - if ($contents) { - $this->response['raw_output'] = $contents; - } + if (DBG_USER && $contents) { + $this->response['raw_output'] = $contents; } $response_js = json_encode($this->response, JSON_THROW_ON_ERROR); diff --git a/src/Dev.php b/src/Dev.php index 4afb1c718..d5b77063b 100644 --- a/src/Dev.php +++ b/src/Dev.php @@ -10,7 +10,6 @@ namespace TorrentPier; use Bugsnag\Client; -use Bugsnag\Handler; use Monolog\Formatter\LineFormatter; use Monolog\Handler\BrowserConsoleHandler; @@ -32,20 +31,6 @@ use Exception; */ class Dev { - /** - * Environment type - * - * @var string - */ - private string $envType; - - /** - * In production mode - * - * @var bool - */ - public bool $isProduction = false; - /** * Whoops instance * @@ -58,33 +43,22 @@ class Dev */ public function __construct() { - $this->envType = strtolower(env('APP_ENV', 'production')); $this->whoops = new Run; - switch ($this->envType) { - case 'prod': - case 'production': - ini_set('display_errors', 0); - ini_set('display_startup_errors', 0); - $this->getWhoopsProduction(); - $this->isProduction = true; - break; - case 'dev': - case 'local': - case 'development': - ini_set('display_errors', 1); - ini_set('display_startup_errors', 1); - $this->getWhoops(); - break; + if (DBG_USER) { + $this->getWhoopsOnPage(); + } else { + $this->getWhoopsPlaceholder(); } - $this->getBugsnag(); + $this->getWhoopsLogger(); $this->getTelegramSender(); + $this->getBugsnag(); $this->whoops->register(); } /** - * Bugsnag debug driver + * [Whoops] Bugsnag handler * * @return void */ @@ -96,11 +70,14 @@ class Dev return; } - Handler::register(Client::make($bb_cfg['bugsnag']['api_key'])); + $bugsnag = Client::make($bb_cfg['bugsnag']['api_key']); + $this->whoops->pushHandler(function ($e) use ($bugsnag) { + $bugsnag->notifyException($e); + }); } /** - * Telegram debug driver + * [Whoops] Telegram handler * * @return void */ @@ -108,37 +85,26 @@ class Dev { global $bb_cfg; - if ($bb_cfg['telegram_sender']['enabled']) { - $telegramSender = new PlainTextHandler(); - $telegramSender->loggerOnly(true); - $telegramSender->setLogger((new Logger( - APP_NAME, - [(new TelegramHandler($bb_cfg['telegram_sender']['token'], (int)$bb_cfg['telegram_sender']['chat_id'], timeout: (int)$bb_cfg['telegram_sender']['timeout'])) - ->setFormatter(new TelegramFormatter())] - ))); - $this->whoops->pushHandler($telegramSender); + if (!$bb_cfg['telegram_sender']['enabled']) { + return; } + + $telegramSender = new PlainTextHandler(); + $telegramSender->loggerOnly(true); + $telegramSender->setLogger((new Logger( + APP_NAME, + [(new TelegramHandler($bb_cfg['telegram_sender']['token'], (int)$bb_cfg['telegram_sender']['chat_id'], timeout: (int)$bb_cfg['telegram_sender']['timeout'])) + ->setFormatter(new TelegramFormatter())] + ))); + $this->whoops->pushHandler($telegramSender); } /** - * Whoops production debug driver + * [Whoops] On page handler (in debug) * * @return void */ - private function getWhoopsProduction(): void - { - $this->whoops->pushHandler(function () { - global $bb_cfg; - echo $bb_cfg['whoops']['error_message']; - }); - } - - /** - * Whoops debug driver - * - * @return void - */ - private function getWhoops(): void + private function getWhoopsOnPage(): void { global $bb_cfg; @@ -164,20 +130,42 @@ class Dev ->setFormatter((new LineFormatter(null, null, true)))] ))); $this->whoops->pushHandler($loggingInConsole); + } - /** - * Log errors in file - */ - if ((int)ini_get('log_errors') === 1) { - $loggingInFile = new PlainTextHandler(); - $loggingInFile->loggerOnly(true); - $loggingInFile->setLogger((new Logger( - APP_NAME, - [(new StreamHandler(WHOOPS_LOG_FILE)) - ->setFormatter((new LineFormatter(null, null, true)))] - ))); - $this->whoops->pushHandler($loggingInFile); + /** + * [Whoops] Logger handler + * + * @return void + */ + private function getWhoopsLogger(): void + { + if ((int)ini_get('log_errors') !== 1) { + return; } + + $loggingInFile = new PlainTextHandler(); + $loggingInFile->loggerOnly(true); + $loggingInFile->setLogger((new Logger( + APP_NAME, + [(new StreamHandler(WHOOPS_LOG_FILE)) + ->setFormatter((new LineFormatter(null, null, true)))] + ))); + $this->whoops->pushHandler($loggingInFile); + } + + /** + * [Whoops] Placeholder handler (non debug) + * + * @return void + */ + private function getWhoopsPlaceholder(): void + { + global $bb_cfg; + + $this->whoops->pushHandler(function ($e) use ($bb_cfg) { + echo $bb_cfg['whoops']['error_message']; + echo "
Error: {$e->getMessage()}."; + }); } /** @@ -186,28 +174,28 @@ class Dev * @return string * @throws Exception */ - public function getSqlLog(): string + public static function getSqlLog(): string { global $DBS, $CACHES, $datastore; $log = ''; foreach ($DBS->srv as $srv_name => $db_obj) { - $log .= !empty($db_obj->dbg) ? $this->getSqlLogHtml($db_obj, "database: $srv_name [{$db_obj->engine}]") : ''; + $log .= !empty($db_obj->dbg) ? self::getSqlLogHtml($db_obj, "database: $srv_name [{$db_obj->engine}]") : ''; } foreach ($CACHES->obj as $cache_name => $cache_obj) { if (!empty($cache_obj->db->dbg)) { - $log .= $this->getSqlLogHtml($cache_obj->db, "cache: $cache_name [{$cache_obj->db->engine}]"); + $log .= self::getSqlLogHtml($cache_obj->db, "cache: $cache_name [{$cache_obj->db->engine}]"); } elseif (!empty($cache_obj->dbg)) { - $log .= $this->getSqlLogHtml($cache_obj, "cache: $cache_name [{$cache_obj->engine}]"); + $log .= self::getSqlLogHtml($cache_obj, "cache: $cache_name [{$cache_obj->engine}]"); } } if (!empty($datastore->db->dbg)) { - $log .= $this->getSqlLogHtml($datastore->db, "cache: datastore [{$datastore->db->engine}]"); + $log .= self::getSqlLogHtml($datastore->db, "cache: datastore [{$datastore->db->engine}]"); } elseif (!empty($datastore->dbg)) { - $log .= $this->getSqlLogHtml($datastore, "cache: datastore [{$datastore->engine}]"); + $log .= self::getSqlLogHtml($datastore, "cache: datastore [{$datastore->engine}]"); } return $log; @@ -218,9 +206,9 @@ class Dev * * @return bool */ - public function sqlDebugAllowed(): bool + public static function sqlDebugAllowed(): bool { - return (SQL_DEBUG && !$this->isProduction && !empty($_COOKIE['sql_log'])); + return (SQL_DEBUG && DBG_USER && !empty($_COOKIE['sql_log'])); } /** @@ -232,13 +220,13 @@ class Dev * @return string * @throws Exception */ - private function getSqlLogHtml(object $db_obj, string $log_name): string + private static function getSqlLogHtml(object $db_obj, string $log_name): string { $log = ''; foreach ($db_obj->dbg as $i => $dbg) { $id = "sql_{$i}_" . random_int(0, mt_getrandmax()); - $sql = $this->shortQuery($dbg['sql'], true); + $sql = self::shortQuery($dbg['sql'], true); $time = sprintf('%.4f', $dbg['time']); $perc = '[' . round($dbg['time'] * 100 / $db_obj->sql_timetotal) . '%]'; $info = !empty($dbg['info']) ? $dbg['info'] . ' [' . $dbg['src'] . ']' : $dbg['src']; @@ -261,7 +249,7 @@ class Dev * @param bool $esc_html * @return string */ - public function shortQuery(string $sql, bool $esc_html = false): string + public static function shortQuery(string $sql, bool $esc_html = false): string { $max_len = 100; $sql = str_compact($sql); diff --git a/src/Legacy/Cache/APCu.php b/src/Legacy/Cache/APCu.php index 3af2d321e..471c2b7ff 100644 --- a/src/Legacy/Cache/APCu.php +++ b/src/Legacy/Cache/APCu.php @@ -9,6 +9,8 @@ namespace TorrentPier\Legacy\Cache; +use TorrentPier\Dev; + use MatthiasMullie\Scrapbook\Adapters\Apc; /** @@ -52,11 +54,9 @@ class APCu extends Common */ public function __construct(string $prefix) { - global $debug; - $this->apcu = new Apc(); $this->prefix = $prefix; - $this->dbg_enabled = $debug->sqlDebugAllowed(); + $this->dbg_enabled = Dev::sqlDebugAllowed(); } /** diff --git a/src/Legacy/Cache/Common.php b/src/Legacy/Cache/Common.php index bf33b8f3f..ba073ad6f 100644 --- a/src/Legacy/Cache/Common.php +++ b/src/Legacy/Cache/Common.php @@ -9,6 +9,8 @@ namespace TorrentPier\Legacy\Cache; +use TorrentPier\Dev; + /** * Class Common * @package TorrentPier\Legacy\Cache @@ -70,8 +72,6 @@ class Common public function debug($mode, $cur_query = null) { - global $debug; - if (!$this->dbg_enabled) { return; } @@ -82,7 +82,7 @@ class Common switch ($mode) { case 'start': $this->sql_starttime = utime(); - $dbg['sql'] = $debug->shortQuery($cur_query ?? $this->cur_query); + $dbg['sql'] = Dev::shortQuery($cur_query ?? $this->cur_query); $dbg['src'] = $this->debug_find_source(); $dbg['file'] = $this->debug_find_source('file'); $dbg['line'] = $this->debug_find_source('line'); diff --git a/src/Legacy/Cache/File.php b/src/Legacy/Cache/File.php index c273620d5..5954e161e 100644 --- a/src/Legacy/Cache/File.php +++ b/src/Legacy/Cache/File.php @@ -9,6 +9,8 @@ namespace TorrentPier\Legacy\Cache; +use TorrentPier\Dev; + use League\Flysystem\Filesystem; use League\Flysystem\Local\LocalFilesystemAdapter; use MatthiasMullie\Scrapbook\Adapters\Flysystem; @@ -55,13 +57,11 @@ class File extends Common */ public function __construct(string $dir, string $prefix) { - global $debug; - $adapter = new LocalFilesystemAdapter($dir, null, LOCK_EX); $filesystem = new Filesystem($adapter); $this->file = new Flysystem($filesystem); $this->prefix = $prefix; - $this->dbg_enabled = $debug->sqlDebugAllowed(); + $this->dbg_enabled = Dev::sqlDebugAllowed(); } /** diff --git a/src/Legacy/Cache/Memcached.php b/src/Legacy/Cache/Memcached.php index 55aaf35ef..12ee93594 100644 --- a/src/Legacy/Cache/Memcached.php +++ b/src/Legacy/Cache/Memcached.php @@ -9,6 +9,8 @@ namespace TorrentPier\Legacy\Cache; +use TorrentPier\Dev; + use Memcached as MemcachedClient; use MatthiasMullie\Scrapbook\Adapters\Memcached as MemcachedCache; @@ -75,12 +77,10 @@ class Memcached extends Common */ public function __construct(array $cfg, string $prefix) { - global $debug; - $this->client = new MemcachedClient(); $this->cfg = $cfg; $this->prefix = $prefix; - $this->dbg_enabled = $debug->sqlDebugAllowed(); + $this->dbg_enabled = Dev::sqlDebugAllowed(); } /** diff --git a/src/Legacy/Cache/Redis.php b/src/Legacy/Cache/Redis.php index cfc819bfd..24e9b4e19 100644 --- a/src/Legacy/Cache/Redis.php +++ b/src/Legacy/Cache/Redis.php @@ -9,6 +9,8 @@ namespace TorrentPier\Legacy\Cache; +use TorrentPier\Dev; + use Redis as RedisClient; use MatthiasMullie\Scrapbook\Adapters\Redis as RedisCache; @@ -75,12 +77,10 @@ class Redis extends Common */ public function __construct(array $cfg, string $prefix) { - global $debug; - $this->client = new RedisClient(); $this->cfg = $cfg; $this->prefix = $prefix; - $this->dbg_enabled = $debug->sqlDebugAllowed(); + $this->dbg_enabled = Dev::sqlDebugAllowed(); } /** diff --git a/src/Legacy/Cache/Sqlite.php b/src/Legacy/Cache/Sqlite.php index 6d677888b..e345410e6 100644 --- a/src/Legacy/Cache/Sqlite.php +++ b/src/Legacy/Cache/Sqlite.php @@ -9,6 +9,8 @@ namespace TorrentPier\Legacy\Cache; +use TorrentPier\Dev; + use MatthiasMullie\Scrapbook\Adapters\SQLite as SQLiteCache; use PDO; @@ -54,12 +56,10 @@ class Sqlite extends Common */ public function __construct(string $dir, string $prefix) { - global $debug; - $client = new PDO("sqlite:$dir.db"); $this->sqlite = new SQLiteCache($client); $this->prefix = $prefix; - $this->dbg_enabled = $debug->sqlDebugAllowed(); + $this->dbg_enabled = Dev::sqlDebugAllowed(); } /** diff --git a/src/Legacy/Common/User.php b/src/Legacy/Common/User.php index 678a7f905..43ecd6a93 100644 --- a/src/Legacy/Common/User.php +++ b/src/Legacy/Common/User.php @@ -463,8 +463,22 @@ class User */ public function set_session_cookies($user_id) { + global $bb_cfg; + + $debug_cookies = [ + COOKIE_DBG, + 'explain', + 'sql_log', + 'sql_log_full' + ]; + if ($user_id == GUEST_UID) { - $delete_cookies = [COOKIE_DATA, 'torhelp', 'user_lang']; + $delete_cookies = [ + COOKIE_DATA, + 'torhelp', + 'user_lang' + ]; + $delete_cookies = array_merge($delete_cookies, $debug_cookies); foreach ($delete_cookies as $cookie) { if (isset($_COOKIE[$cookie])) { @@ -472,6 +486,22 @@ class User } } } else { + if (!isset($bb_cfg['dbg_users'][$this->data['user_id']]) && DBG_USER) { + bb_setcookie(COOKIE_DBG, null); + } elseif (isset($bb_cfg['dbg_users'][$this->data['user_id']]) && !DBG_USER) { + bb_setcookie(COOKIE_DBG, hash('xxh128', $bb_cfg['dbg_users'][$this->data['user_id']]), COOKIE_SESSION); + } + + // Unset sql debug cookies if SQL_DEBUG is disabled or DBG_USER cookie not present + if (!SQL_DEBUG || !DBG_USER) { + foreach ($debug_cookies as $cookie) { + if (isset($_COOKIE[$cookie])) { + bb_setcookie($cookie, null); + } + } + } + + // Set bb_data (session) cookie $c_sdata_resv = !empty($_COOKIE[COOKIE_DATA]) ? $_COOKIE[COOKIE_DATA] : null; $c_sdata_curr = ($this->sessiondata) ? json_encode($this->sessiondata) : ''; diff --git a/src/Legacy/Datastore/APCu.php b/src/Legacy/Datastore/APCu.php index cd25fa64f..46a0c0e44 100644 --- a/src/Legacy/Datastore/APCu.php +++ b/src/Legacy/Datastore/APCu.php @@ -9,6 +9,8 @@ namespace TorrentPier\Legacy\Datastore; +use TorrentPier\Dev; + use MatthiasMullie\Scrapbook\Adapters\Apc; /** @@ -45,11 +47,9 @@ class APCu extends Common */ public function __construct(string $prefix) { - global $debug; - $this->apcu = new Apc(); $this->prefix = $prefix; - $this->dbg_enabled = $debug->sqlDebugAllowed(); + $this->dbg_enabled = Dev::sqlDebugAllowed(); } /** diff --git a/src/Legacy/Datastore/Common.php b/src/Legacy/Datastore/Common.php index 58bd69653..8de3a7d92 100644 --- a/src/Legacy/Datastore/Common.php +++ b/src/Legacy/Datastore/Common.php @@ -9,6 +9,8 @@ namespace TorrentPier\Legacy\Datastore; +use TorrentPier\Dev; + /** * Class Common * @package TorrentPier\Legacy\Datastore @@ -155,8 +157,6 @@ class Common public function debug($mode, $cur_query = null) { - global $debug; - if (!$this->dbg_enabled) { return; } @@ -167,7 +167,7 @@ class Common switch ($mode) { case 'start': $this->sql_starttime = utime(); - $dbg['sql'] = $debug->shortQuery($cur_query ?? $this->cur_query); + $dbg['sql'] = Dev::shortQuery($cur_query ?? $this->cur_query); $dbg['src'] = $this->debug_find_source(); $dbg['file'] = $this->debug_find_source('file'); $dbg['line'] = $this->debug_find_source('line'); diff --git a/src/Legacy/Datastore/File.php b/src/Legacy/Datastore/File.php index 86dab53e7..8d24923cb 100644 --- a/src/Legacy/Datastore/File.php +++ b/src/Legacy/Datastore/File.php @@ -9,6 +9,8 @@ namespace TorrentPier\Legacy\Datastore; +use TorrentPier\Dev; + use League\Flysystem\Filesystem; use League\Flysystem\Local\LocalFilesystemAdapter; use MatthiasMullie\Scrapbook\Adapters\Flysystem; @@ -48,13 +50,11 @@ class File extends Common */ public function __construct(string $dir, string $prefix) { - global $debug; - $adapter = new LocalFilesystemAdapter($dir, null, LOCK_EX); $filesystem = new Filesystem($adapter); $this->file = new Flysystem($filesystem); $this->prefix = $prefix; - $this->dbg_enabled = $debug->sqlDebugAllowed(); + $this->dbg_enabled = Dev::sqlDebugAllowed(); } /** diff --git a/src/Legacy/Datastore/Memcached.php b/src/Legacy/Datastore/Memcached.php index 1cb3e517f..6660d2add 100644 --- a/src/Legacy/Datastore/Memcached.php +++ b/src/Legacy/Datastore/Memcached.php @@ -9,6 +9,8 @@ namespace TorrentPier\Legacy\Datastore; +use TorrentPier\Dev; + use Memcached as MemcachedClient; use MatthiasMullie\Scrapbook\Adapters\Memcached as MemcachedCache; @@ -68,12 +70,10 @@ class Memcached extends Common */ public function __construct(array $cfg, string $prefix) { - global $debug; - $this->client = new MemcachedClient(); $this->cfg = $cfg; $this->prefix = $prefix; - $this->dbg_enabled = $debug->sqlDebugAllowed(); + $this->dbg_enabled = Dev::sqlDebugAllowed(); } /** diff --git a/src/Legacy/Datastore/Redis.php b/src/Legacy/Datastore/Redis.php index 7cf193d44..96749bb7a 100644 --- a/src/Legacy/Datastore/Redis.php +++ b/src/Legacy/Datastore/Redis.php @@ -9,6 +9,8 @@ namespace TorrentPier\Legacy\Datastore; +use TorrentPier\Dev; + use Redis as RedisClient; use MatthiasMullie\Scrapbook\Adapters\Redis as RedisCache; @@ -68,12 +70,10 @@ class Redis extends Common */ public function __construct(array $cfg, string $prefix) { - global $debug; - $this->client = new RedisClient(); $this->cfg = $cfg; $this->prefix = $prefix; - $this->dbg_enabled = $debug->sqlDebugAllowed(); + $this->dbg_enabled = Dev::sqlDebugAllowed(); } /** diff --git a/src/Legacy/Datastore/Sqlite.php b/src/Legacy/Datastore/Sqlite.php index 855b98aa0..f6e766e01 100644 --- a/src/Legacy/Datastore/Sqlite.php +++ b/src/Legacy/Datastore/Sqlite.php @@ -9,6 +9,8 @@ namespace TorrentPier\Legacy\Datastore; +use TorrentPier\Dev; + use MatthiasMullie\Scrapbook\Adapters\SQLite as SQLiteCache; use PDO; @@ -47,12 +49,10 @@ class Sqlite extends Common */ public function __construct(string $dir, string $prefix) { - global $debug; - $client = new PDO("sqlite:$dir.db"); $this->sqlite = new SQLiteCache($client); $this->prefix = $prefix; - $this->dbg_enabled = $debug->sqlDebugAllowed(); + $this->dbg_enabled = Dev::sqlDebugAllowed(); } /** diff --git a/src/Legacy/SqlDb.php b/src/Legacy/SqlDb.php index 00362f4a6..fdbc08540 100644 --- a/src/Legacy/SqlDb.php +++ b/src/Legacy/SqlDb.php @@ -11,6 +11,8 @@ namespace TorrentPier\Legacy; use mysqli_result; +use TorrentPier\Dev; + /** * Class SqlDb * @package TorrentPier\Legacy @@ -55,10 +57,10 @@ class SqlDb */ public function __construct($cfg_values) { - global $DBS, $debug; + global $DBS; $this->cfg = array_combine($this->cfg_keys, $cfg_values); - $this->dbg_enabled = ($debug->sqlDebugAllowed() || !empty($_COOKIE['explain'])); + $this->dbg_enabled = (Dev::sqlDebugAllowed() || !empty($_COOKIE['explain'])); $this->do_explain = ($this->dbg_enabled && !empty($_COOKIE['explain'])); $this->slow_time = SQL_SLOW_QUERY_TIME; @@ -830,8 +832,6 @@ class SqlDb */ public function log_query($log_file = 'sql_queries') { - global $debug; - $q_time = ($this->cur_query_time >= 10) ? round($this->cur_query_time, 0) : sprintf('%.4f', $this->cur_query_time); $msg = []; $msg[] = round($this->sql_starttime); @@ -839,7 +839,7 @@ class SqlDb $msg[] = sprintf('%-6s', $q_time); $msg[] = sprintf('%05d', getmypid()); $msg[] = $this->db_server; - $msg[] = $debug->shortQuery($this->cur_query); + $msg[] = Dev::shortQuery($this->cur_query); $msg = implode(LOG_SEPR, $msg); $msg .= ($info = $this->query_info()) ? ' # ' . $info : ''; $msg .= ' # ' . $this->debug_find_source() . ' '; @@ -903,8 +903,6 @@ class SqlDb */ public function explain($mode, $html_table = '', array $row = []) { - global $debug; - $query = str_compact($this->cur_query); // remove comments $query = preg_replace('#(\s*)(/\*)(.*)(\*/)(\s*)#', '', $query); @@ -950,7 +948,7 @@ class SqlDb ' . $this->explain_hold . ' -
' . $debug->shortQuery($dbg['sql'], true) . '  
+
' . Dev::shortQuery($dbg['sql'], true) . '  

'; break;