diff --git a/src/domain/Bouquet/BouquetService.php b/src/domain/Bouquet/BouquetService.php index 4d12781e..fecf266c 100644 --- a/src/domain/Bouquet/BouquetService.php +++ b/src/domain/Bouquet/BouquetService.php @@ -11,8 +11,20 @@ */ class BouquetService { + private static $db = null; + + public static function setDb($db): void { + self::$db = $db; + } + + private static function db(): object { + if (self::$db === null) { + throw new \RuntimeException(static::class . '::setDb() must be called before use.'); + } + return self::$db; + } public static function process($rData) { - global $db; + $db = self::db(); if (isset($rData['edit'])) { if (!Authorization::check('adv', 'edit_bouquet')) { exit(); @@ -84,7 +96,7 @@ class BouquetService { } public static function reorder($rData) { - global $db; + $db = self::db(); $rOrder = json_decode($rData['stream_order_array'], true); $rOrder['stream'] = AdminHelpers::confirmIDs($rOrder['stream']); $rOrder['series'] = AdminHelpers::confirmIDs($rOrder['series']); @@ -96,7 +108,7 @@ class BouquetService { } public static function sort($rData) { - global $db; + $db = self::db(); set_time_limit(0); ini_set('mysql.connect_timeout', 0); ini_set('max_execution_time', 0); @@ -137,7 +149,7 @@ class BouquetService { } public static function scanOne($rID) { - global $db; + $db = self::db(); $rBouquet = BouquetService::getById($rID); if (!$rBouquet) { return; @@ -198,7 +210,7 @@ class BouquetService { } public static function getAll($rForce = false) { - global $db; + $db = self::db(); if (!$rForce) { $rCache = FileCache::getCache('bouquets', 60); if (!empty($rCache)) { @@ -232,7 +244,7 @@ class BouquetService { } public static function getUserBouquets() { - global $db; + $db = self::db(); $rReturn = array(); $db->query('SELECT `id`, `bouquet` FROM `lines` ORDER BY `id` ASC;'); @@ -246,7 +258,7 @@ class BouquetService { } public static function getAllSimple() { - global $db; + $db = self::db(); $rReturn = array(); $db->query('SELECT * FROM `bouquets` ORDER BY `bouquet_order` ASC;'); @@ -264,7 +276,7 @@ class BouquetService { } public static function getById($rID) { - global $db; + $db = self::db(); $db->query('SELECT * FROM `bouquets` WHERE `id` = ?;', $rID); if ($db->num_rows() != 1) { @@ -275,7 +287,7 @@ class BouquetService { } public static function deleteById($rID) { - global $db; + $db = self::db(); $rBouquet = self::getById($rID); if (!$rBouquet) { @@ -333,7 +345,7 @@ class BouquetService { } public static function addItems($rType, $rBouquetID, $rIDs) { - global $db; + $db = self::db(); if (!is_array($rIDs)) { $rIDs = array($rIDs); @@ -372,7 +384,7 @@ class BouquetService { } public static function removeItems($rType, $rBouquetID, $rIDs) { - global $db; + $db = self::db(); if (!is_array($rIDs)) { $rIDs = array($rIDs); diff --git a/src/domain/Device/EnigmaService.php b/src/domain/Device/EnigmaService.php index e376e129..a3673d19 100644 --- a/src/domain/Device/EnigmaService.php +++ b/src/domain/Device/EnigmaService.php @@ -11,6 +11,18 @@ */ class EnigmaService { + private static $db = null; + + public static function setDb($db): void { + self::$db = $db; + } + + private static function db(): object { + if (self::$db === null) { + throw new \RuntimeException(static::class . '::setDb() must be called before use.'); + } + return self::$db; + } public static function massDelete($rData) { set_time_limit(0); ini_set('mysql.connect_timeout', 0); @@ -24,7 +36,7 @@ class EnigmaService { } public static function massEdit($rData) { - global $db; + $db = self::db(); if (InputValidator::validate('massEditEnigmas', $rData)) { $rArray = array(); $rUserArray = array(); @@ -163,7 +175,7 @@ class EnigmaService { } public static function process($rData) { - global $db; + $db = self::db(); if (InputValidator::validate('processEnigma', $rData)) { if (isset($rData['edit'])) { if (Authorization::check('adv', 'edit_e2')) { @@ -344,7 +356,7 @@ class EnigmaService { } public static function getById($rID) { - global $db; + $db = self::db(); $db->query('SELECT * FROM `enigma2_devices` WHERE `device_id` = ?;', $rID); if ($db->num_rows() != 1) { @@ -365,7 +377,7 @@ class EnigmaService { } public static function getByUserId($rID) { - global $db; + $db = self::db(); $db->query('SELECT * FROM `enigma2_devices` WHERE `user_id` = ?;', $rID); if ($db->num_rows() != 1) { @@ -376,7 +388,7 @@ class EnigmaService { } public static function deleteDevice($rID, $rDeletePaired = false, $rCloseCons = true, $rConvert = false) { - global $db; + $db = self::db(); $rEnigma = self::getById($rID); if (!$rEnigma) { @@ -409,7 +421,7 @@ class EnigmaService { } public static function deleteDevices($rIDs) { - global $db; + $db = self::db(); $rIDs = AdminHelpers::confirmIDs($rIDs); if (0 >= count($rIDs)) { diff --git a/src/domain/Device/MagService.php b/src/domain/Device/MagService.php index 22305ced..4115288f 100644 --- a/src/domain/Device/MagService.php +++ b/src/domain/Device/MagService.php @@ -11,6 +11,18 @@ */ class MagService { + private static $db = null; + + public static function setDb($db): void { + self::$db = $db; + } + + private static function db(): object { + if (self::$db === null) { + throw new \RuntimeException(static::class . '::setDb() must be called before use.'); + } + return self::$db; + } public static function massDelete($rData) { set_time_limit(0); ini_set('mysql.connect_timeout', 0); @@ -24,7 +36,7 @@ class MagService { } public static function massEdit($rData) { - global $db; + $db = self::db(); if (InputValidator::validate('massEditMags', $rData)) { $rArray = array(); $rUserArray = array(); @@ -189,7 +201,7 @@ class MagService { } public static function process($rData) { - global $db; + $db = self::db(); if (InputValidator::validate('processMAG', $rData)) { if (isset($rData['edit'])) { if (Authorization::check('adv', 'edit_mag')) { @@ -361,7 +373,7 @@ class MagService { } public static function syncLineDevices($rUserID, $rDeviceID = null) { - global $db; + $db = self::db(); $rUser = UserRepository::getLineById($rUserID); if ($rUser) { @@ -393,7 +405,7 @@ class MagService { } public static function getById($rID) { - global $db; + $db = self::db(); $db->query('SELECT * FROM `mag_devices` WHERE `mag_id` = ?;', $rID); if ($db->num_rows() != 1) { @@ -413,7 +425,7 @@ class MagService { } public static function deleteDevice($rID, $rDeletePaired = false, $rCloseCons = true, $rConvert = false) { - global $db; + $db = self::db(); $rMag = self::getById($rID); if (!$rMag) { @@ -448,7 +460,7 @@ class MagService { } public static function deleteDevices($rIDs) { - global $db; + $db = self::db(); $rIDs = AdminHelpers::confirmIDs($rIDs); if (0 >= count($rIDs)) { @@ -475,7 +487,7 @@ class MagService { } public static function resetSTB($rID) { - global $db; + $db = self::db(); $db->query("UPDATE `mag_devices` SET `ip` = '', `ver` = '', `image_version` = '', `stb_type` = '', `sn` = '', `device_id` = '', `device_id2` = '', `hw_version` = '', `token` = '' WHERE `mag_id` = ?;", $rID); } } diff --git a/src/domain/Epg/EPG.php b/src/domain/Epg/EPG.php index 92f4e72e..bb176bd9 100644 --- a/src/domain/Epg/EPG.php +++ b/src/domain/Epg/EPG.php @@ -11,6 +11,18 @@ */ class EPG { + private static $db = null; + + public static function setDb($db): void { + self::$db = $db; + } + + private static function db(): object { + if (self::$db === null) { + throw new \RuntimeException(static::class . '::setDb() must be called before use.'); + } + return self::$db; + } public $rValid = false; public $rEPGSource; public $rFilename; @@ -77,7 +89,7 @@ class EPG { } public function parseEPG($rEPGID, $rChannelInfo, $rOffset = 0) { - global $db; + $db = self::db(); $rInsertQuery = []; $programCount = 0; diff --git a/src/domain/Epg/EpgService.php b/src/domain/Epg/EpgService.php index 484f5cba..30b5e43a 100644 --- a/src/domain/Epg/EpgService.php +++ b/src/domain/Epg/EpgService.php @@ -11,8 +11,20 @@ */ class EpgService { + private static $db = null; + + public static function setDb($db): void { + self::$db = $db; + } + + private static function db(): object { + if (self::$db === null) { + throw new \RuntimeException(static::class . '::setDb() must be called before use.'); + } + return self::$db; + } public static function process($rData) { - global $db; + $db = self::db(); if (isset($rData['edit'])) { if (!Authorization::check('adv', 'epg_edit')) { exit(); @@ -52,7 +64,7 @@ class EpgService { // ──────────── Из EpgRepository ──────────── public static function findByName($rEPGName) { - global $db; + $db = self::db(); $db->query('SELECT `id`, `data` FROM `epg`;'); if ($db->num_rows() > 0) { @@ -73,7 +85,7 @@ class EpgService { } public static function getById($rID) { - global $db; + $db = self::db(); $db->query('SELECT * FROM `epg` WHERE `id` = ?;', $rID); if ($db->num_rows() == 1) { @@ -131,7 +143,7 @@ class EpgService { } public static function getAll() { - global $db; + $db = self::db(); $rReturn = array(); $db->query('SELECT * FROM `epg` ORDER BY `id` ASC;'); @@ -145,7 +157,7 @@ class EpgService { } public static function deleteEpgById($rID) { - global $db; + $db = self::db(); $rEPG = self::getById($rID); if (!$rEPG) { diff --git a/src/domain/Line/LineRepository.php b/src/domain/Line/LineRepository.php index d55f12c7..20fb925b 100644 --- a/src/domain/Line/LineRepository.php +++ b/src/domain/Line/LineRepository.php @@ -11,8 +11,20 @@ */ class LineRepository { + private static $db = null; + + public static function setDb($db): void { + self::$db = $db; + } + + private static function db(): object { + if (self::$db === null) { + throw new \RuntimeException(static::class . '::setDb() must be called before use.'); + } + return self::$db; + } public static function deleteMany($rIDs) { - global $db; + $db = self::db(); $rIDs = AdminHelpers::confirmIDs($rIDs); if (0 >= count($rIDs)) { @@ -41,7 +53,7 @@ class LineRepository { } public static function getOutputFormats() { - global $db; + $db = self::db(); $db->query('SELECT * FROM `output_formats` ORDER BY `access_output_id` ASC;'); return (0 < $db->num_rows() ? $db->get_rows() : array()); } diff --git a/src/domain/Line/LineService.php b/src/domain/Line/LineService.php index 12cfe80e..548d0078 100644 --- a/src/domain/Line/LineService.php +++ b/src/domain/Line/LineService.php @@ -11,6 +11,18 @@ */ class LineService { + private static $db = null; + + public static function setDb($db): void { + self::$db = $db; + } + + private static function db(): object { + if (self::$db === null) { + throw new \RuntimeException(static::class . '::setDb() must be called before use.'); + } + return self::$db; + } public static function massDelete($rData) { set_time_limit(0); ini_set('mysql.connect_timeout', 0); @@ -24,7 +36,7 @@ class LineService { } public static function massEdit($rData) { - global $db; + $db = self::db(); if (InputValidator::validate('massEditLines', $rData)) { $rArray = array(); @@ -139,7 +151,7 @@ class LineService { } public static function process($rData) { - global $db; + $db = self::db(); if (InputValidator::validate('processLine', $rData)) { if (isset($rData['edit'])) { if (Authorization::check('adv', 'edit_user')) { @@ -277,7 +289,7 @@ class LineService { } public static function updateLineSignal($rUserID, $rForce = false) { - global $db; + $db = self::db(); $rCached = SettingsManager::getAll()['enable_cache']; $rMainID = ConnectionTracker::getMainID(); if ($rCached) { @@ -292,7 +304,7 @@ class LineService { } public static function updateLinesSignal($rUserIDs) { - global $db; + $db = self::db(); $rCached = SettingsManager::getAll()['enable_cache']; $rMainID = ConnectionTracker::getMainID(); if ($rCached) { @@ -307,7 +319,7 @@ class LineService { } public static function deleteLineById($rID, $rDeletePaired = false, $rCloseCons = true) { - global $db; + $db = self::db(); $rLine = UserRepository::getLineById($rID); if (!$rLine) { @@ -349,7 +361,7 @@ class LineService { } public static function getExpiring($rLimit = 2419200) { - global $db; + $db = self::db(); global $rUserInfo; global $rPermissions; $rReturn = array(); @@ -368,7 +380,7 @@ class LineService { } public static function canGenerateTrials($rUserID) { - global $db; + $db = self::db(); global $rSettings; $rUser = UserRepository::getRegisteredUserById($rUserID); $rPermissions = AuthRepository::getPermissions($rUser['member_group_id']); diff --git a/src/domain/Line/PackageService.php b/src/domain/Line/PackageService.php index ce963715..bf19af62 100644 --- a/src/domain/Line/PackageService.php +++ b/src/domain/Line/PackageService.php @@ -11,8 +11,20 @@ */ class PackageService { + private static $db = null; + + public static function setDb($db): void { + self::$db = $db; + } + + private static function db(): object { + if (self::$db === null) { + throw new \RuntimeException(static::class . '::setDb() must be called before use.'); + } + return self::$db; + } public static function process($rData) { - global $db; + $db = self::db(); if (isset($rData['edit'])) { if (!Authorization::check('adv', 'edit_package')) { exit(); @@ -62,7 +74,7 @@ class PackageService { } public static function deleteById($rID) { - global $db; + $db = self::db(); $rPackage = self::getById($rID); if (!$rPackage) { @@ -76,7 +88,7 @@ class PackageService { } public static function getAll($rGroup = null, $rType = null) { - global $db; + $db = self::db(); $rReturn = array(); $db->query('SELECT * FROM `users_packages` ORDER BY `id` ASC;'); @@ -96,7 +108,7 @@ class PackageService { } public static function getById($rID) { - global $db; + $db = self::db(); $db->query('SELECT * FROM `users_packages` WHERE `id` = ?;', $rID); if ($db->num_rows() != 1) { diff --git a/src/domain/Security/BlocklistService.php b/src/domain/Security/BlocklistService.php index 11aafffa..9da14a27 100644 --- a/src/domain/Security/BlocklistService.php +++ b/src/domain/Security/BlocklistService.php @@ -11,8 +11,20 @@ */ class BlocklistService { + private static $db = null; + + public static function setDb($db): void { + self::$db = $db; + } + + private static function db(): object { + if (self::$db === null) { + throw new \RuntimeException(static::class . '::setDb() must be called before use.'); + } + return self::$db; + } public static function blockIP($rData) { - global $db; + $db = self::db(); if (!AdminHelpers::validateCIDR($rData['ip'])) { return array('status' => STATUS_INVALID_IP, 'data' => $rData); } @@ -31,7 +43,7 @@ class BlocklistService { } public static function processISP($rData) { - global $db; + $db = self::db(); if (isset($rData['edit'])) { if (!Authorization::check('adv', 'block_isps')) { exit(); @@ -67,7 +79,7 @@ class BlocklistService { } public static function processRTMPIP($rData) { - global $db; + $db = self::db(); if (isset($rData['edit'])) { $rArray = AdminHelpers::overwriteData(BlocklistService::getRTMPIPById($rData['edit']), $rData); } else { @@ -107,7 +119,7 @@ class BlocklistService { } public static function processUA($rData) { - global $db; + $db = self::db(); if (isset($rData['edit'])) { $rArray = AdminHelpers::overwriteData(BlocklistService::getUserAgentById($rData['edit']), $rData); } else { @@ -149,7 +161,7 @@ class BlocklistService { } public static function checkAndBlockUA($rBlockedUA, $rUserAgent, $rReturn = false) { - global $db; + $db = self::db(); $rUserAgent = strtolower($rUserAgent); $rFoundID = false; foreach ($rBlockedUA as $rKey => $rBlocked) { @@ -223,7 +235,7 @@ class BlocklistService { } public static function getBlockedUA($rForce = false) { - global $db; + $db = self::db(); if (!$rForce) { $rCache = FileCache::getCache('blocked_ua', 20); if ($rCache !== false) { @@ -240,7 +252,7 @@ class BlocklistService { } public static function getBlockedIPs($rForce = false) { - global $db; + $db = self::db(); if (!$rForce) { $rCache = FileCache::getCache('blocked_ips', 20); if ($rCache !== false) { @@ -260,7 +272,7 @@ class BlocklistService { } public static function getBlockedISP($rForce = false) { - global $db; + $db = self::db(); if (!$rForce) { $rCache = FileCache::getCache('blocked_isp', 20); if ($rCache !== false) { @@ -277,7 +289,7 @@ class BlocklistService { } public static function getBlockedServers($rForce = false) { - global $db; + $db = self::db(); if (!$rForce) { $rCache = FileCache::getCache('blocked_servers', 20); if ($rCache !== false) { @@ -297,7 +309,7 @@ class BlocklistService { } public static function getBlockedIPsSimple() { - global $db; + $db = self::db(); $rReturn = array(); $db->query('SELECT * FROM `blocked_ips` ORDER BY `id` ASC;'); @@ -311,7 +323,7 @@ class BlocklistService { } public static function getRTMPIPsSimple() { - global $db; + $db = self::db(); $rReturn = array(); $db->query('SELECT * FROM `rtmp_ips` ORDER BY `id` ASC;'); @@ -325,7 +337,7 @@ class BlocklistService { } public static function getAllowedRTMP() { - global $db; + $db = self::db(); $rReturn = array(); $db->query('SELECT `ip`, `password`, `push`, `pull` FROM `rtmp_ips`'); foreach ($db->get_rows() as $rRow) { @@ -335,7 +347,7 @@ class BlocklistService { } public static function deleteBlockedIP($rID) { - global $db; + $db = self::db(); $db->query('SELECT `id`, `ip` FROM `blocked_ips` WHERE `id` = ?;', $rID); if (0 >= $db->num_rows()) { @@ -354,7 +366,7 @@ class BlocklistService { } public static function deleteBlockedISP($rID) { - global $db; + $db = self::db(); $db->query('SELECT `id` FROM `blocked_isps` WHERE `id` = ?;', $rID); if (0 >= $db->num_rows()) { @@ -367,7 +379,7 @@ class BlocklistService { } public static function deleteBlockedUA($rID) { - global $db; + $db = self::db(); $db->query('SELECT `id` FROM `blocked_uas` WHERE `id` = ?;', $rID); if (0 >= $db->num_rows()) { @@ -380,7 +392,7 @@ class BlocklistService { } public static function flushIPs() { - global $db; + $db = self::db(); global $rServers; global $rProxyServers; $db->query('TRUNCATE `blocked_ips`;'); @@ -398,7 +410,7 @@ class BlocklistService { } public static function getAllUserAgents() { - global $db; + $db = self::db(); $rReturn = array(); $db->query('SELECT * FROM `blocked_uas` ORDER BY `id` ASC;'); @@ -413,7 +425,7 @@ class BlocklistService { } public static function getAllISPs() { - global $db; + $db = self::db(); $rReturn = array(); $db->query('SELECT * FROM `blocked_isps` ORDER BY `id` ASC;'); @@ -428,7 +440,7 @@ class BlocklistService { } public static function getUserAgentById($rID) { - global $db; + $db = self::db(); $db->query('SELECT * FROM `blocked_uas` WHERE `id` = ?;', $rID); if ($db->num_rows() != 1) { @@ -438,7 +450,7 @@ class BlocklistService { } public static function getISPById($rID) { - global $db; + $db = self::db(); $db->query('SELECT * FROM `blocked_isps` WHERE `id` = ?;', $rID); if ($db->num_rows() != 1) { @@ -448,7 +460,7 @@ class BlocklistService { } public static function deleteRTMPIP($rID) { - global $db; + $db = self::db(); $db->query('SELECT `id` FROM `rtmp_ips` WHERE `id` = ?;', $rID); if (0 >= $db->num_rows()) { @@ -461,7 +473,7 @@ class BlocklistService { } public static function getRTMPIPById($rID) { - global $db; + $db = self::db(); $db->query('SELECT * FROM `rtmp_ips` WHERE `id` = ?;', $rID); if ($db->num_rows() != 1) { diff --git a/src/domain/Server/ServerRepository.php b/src/domain/Server/ServerRepository.php index 0ad349dc..c1b94826 100644 --- a/src/domain/Server/ServerRepository.php +++ b/src/domain/Server/ServerRepository.php @@ -11,8 +11,21 @@ */ class ServerRepository { + private static $db = null; + + public static function setDb($db): void { + self::$db = $db; + } + + private static function db(): object { + if (self::$db === null) { + throw new \RuntimeException(static::class . '::setDb() must be called before use.'); + } + return self::$db; + } public static function getAll($rForce = false) { - global $db, $rSettings; + global $rSettings; + $db = self::db(); if (!$rSettings) { $rSettings = SettingsManager::getAll(); } @@ -94,7 +107,7 @@ class ServerRepository { } public static function getAllSimple() { - global $db; + $db = self::db(); $rReturn = array(); $db->query('SELECT * FROM `servers` ORDER BY `id` ASC;'); @@ -109,7 +122,7 @@ class ServerRepository { } public static function getStreamingSimple($rPermissions, $type = 'online') { - global $db; + $db = self::db(); $rReturn = array(); $db->query('SELECT * FROM `servers` WHERE `server_type` = 0 ORDER BY `id` ASC;'); @@ -133,7 +146,7 @@ class ServerRepository { } public static function getProxySimple($rPermissions, $rOnline = false) { - global $db; + $db = self::db(); $rReturn = array(); $db->query('SELECT * FROM `servers` WHERE `server_type` = 1 ORDER BY `id` ASC;'); @@ -232,7 +245,8 @@ class ServerRepository { } public static function deleteById($rID, $rReplaceWith = null) { - global $db, $rSettings; + global $rSettings; + $db = self::db(); $rServer = ServerRepository::getById($rID); if (!$rServer || $rServer['is_main']) { @@ -265,7 +279,7 @@ class ServerRepository { } public static function getAllowedDomains($rForce = false) { - global $db; + $db = self::db(); if (!$rForce) { $rCache = FileCache::getCache('allowed_domains', 20); if ($rCache !== false) { @@ -393,7 +407,7 @@ class ServerRepository { } public static function getById($rID) { - global $db; + $db = self::db(); $db->query('SELECT * FROM `servers` WHERE `id` = ?;', $rID); if ($db->num_rows() != 1) { diff --git a/src/domain/Server/ServerService.php b/src/domain/Server/ServerService.php index 8507597e..be76812f 100644 --- a/src/domain/Server/ServerService.php +++ b/src/domain/Server/ServerService.php @@ -11,8 +11,20 @@ */ class ServerService { + private static $db = null; + + public static function setDb($db): void { + self::$db = $db; + } + + private static function db(): object { + if (self::$db === null) { + throw new \RuntimeException(static::class . '::setDb() must be called before use.'); + } + return self::$db; + } public static function process($rData) { - global $db; + $db = self::db(); if (!Authorization::check('adv', 'edit_server')) { exit(); } @@ -164,7 +176,7 @@ class ServerService { } public static function processProxy($rData) { - global $db; + $db = self::db(); if (!Authorization::check('adv', 'edit_server')) { exit(); } @@ -215,7 +227,7 @@ class ServerService { } public static function install($rData, $rServers, $rProxyServers) { - global $db; + $db = self::db(); if (!Authorization::check('adv', 'add_server')) { exit(); } @@ -291,7 +303,7 @@ class ServerService { } public static function reorder($rData) { - global $db; + $db = self::db(); $rPostServers = json_decode($rData['server_order'], true); if (count($rPostServers) > 0) { foreach ($rPostServers as $rOrder => $rPostServer) { @@ -303,27 +315,27 @@ class ServerService { } public static function changePort($rServerID, $rType, $rPorts, $rReload = false) { - global $db; + $db = self::db(); $db->query('INSERT INTO `signals`(`server_id`, `time`, `custom_data`) VALUES(?, ?, ?);', $rServerID, time(), json_encode(array('action' => 'set_port', 'type' => intval($rType), 'ports' => $rPorts, 'reload' => $rReload))); } public static function setServices($rServerID, $rNumServices, $rReload = true) { - global $db; + $db = self::db(); $db->query('INSERT INTO `signals`(`server_id`, `time`, `custom_data`) VALUES(?, ?, ?);', $rServerID, time(), json_encode(array('action' => 'set_services', 'count' => intval($rNumServices), 'reload' => $rReload))); } public static function setGovernor($rServerID, $rGovernor) { - global $db; + $db = self::db(); $db->query('INSERT INTO `signals`(`server_id`, `time`, `custom_data`) VALUES(?, ?, ?);', $rServerID, time(), json_encode(array('action' => 'set_governor', 'data' => $rGovernor))); } public static function setSysctl($rServerID, $rSysCtl) { - global $db; + $db = self::db(); $db->query('INSERT INTO `signals`(`server_id`, `time`, `custom_data`) VALUES(?, ?, ?);', $rServerID, time(), json_encode(array('action' => 'set_sysctl', 'data' => $rSysCtl))); } public static function restoreImages() { - global $db; + $db = self::db(); global $rServers; foreach (array_keys($rServers) as $rServerID) { if (!$rServers[$rServerID]['server_online']) { @@ -336,7 +348,7 @@ class ServerService { } public static function killPlexSync() { - global $db; + $db = self::db(); $db->query("SELECT DISTINCT(`server_id`) AS `server_id` FROM `watch_folders` WHERE `active` = 1 AND `type` = 'plex';"); global $rServers; diff --git a/src/domain/Server/SettingsService.php b/src/domain/Server/SettingsService.php index bfc091c1..948a849d 100644 --- a/src/domain/Server/SettingsService.php +++ b/src/domain/Server/SettingsService.php @@ -11,8 +11,20 @@ */ class SettingsService { + private static $db = null; + + public static function setDb($db): void { + self::$db = $db; + } + + private static function db(): object { + if (self::$db === null) { + throw new \RuntimeException(static::class . '::setDb() must be called before use.'); + } + return self::$db; + } public static function edit($rData) { - global $db; + $db = self::db(); foreach (array('user_agent', 'http_proxy', 'cookie', 'headers') as $rKey) { $db->query('UPDATE `streams_arguments` SET `argument_default_value` = ? WHERE `argument_key` = ?;', ($rData[$rKey] ?: null), $rKey); unset($rData[$rKey]); @@ -73,7 +85,7 @@ class SettingsService { } public static function editBackup($rData) { - global $db; + $db = self::db(); $rArray = QueryHelper::verifyPostTable('settings', $rData, true); foreach (array('dropbox_remote') as $rSetting) { @@ -107,7 +119,7 @@ class SettingsService { } public static function editCacheCron($rData) { - global $db; + $db = self::db(); $rCheck = array(false, false); $rCron = array('*', '*', '*', '*', '*'); $rPattern = '/^[0-9\/*,-]+$/'; diff --git a/src/domain/Stream/CategoryService.php b/src/domain/Stream/CategoryService.php index 8c1ff6fa..fe71f063 100644 --- a/src/domain/Stream/CategoryService.php +++ b/src/domain/Stream/CategoryService.php @@ -11,8 +11,20 @@ */ class CategoryService { + private static $db = null; + + public static function setDb($db): void { + self::$db = $db; + } + + private static function db(): object { + if (self::$db === null) { + throw new \RuntimeException(static::class . '::setDb() must be called before use.'); + } + return self::$db; + } public static function reorder($rData) { - global $db; + $db = self::db(); $rPostCategories = json_decode($rData['categories'], true); if (0 >= count($rPostCategories)) { @@ -26,7 +38,7 @@ class CategoryService { } public static function process($rData) { - global $db; + $db = self::db(); if (isset($rData['edit'])) { $rArray = AdminHelpers::overwriteData(CategoryService::getById($rData['edit']), $rData); } else { @@ -71,7 +83,7 @@ class CategoryService { // ──────────── Из CategoryRepository ──────────── public static function getFromDatabase($rType = null, $rForce = false) { - global $db; + $db = self::db(); if (is_string($rType)) { $db->query('SELECT t1.* FROM `streams_categories` t1 WHERE t1.category_type = ? GROUP BY t1.id ORDER BY t1.cat_order ASC', $rType); return (0 < $db->num_rows() ? $db->get_rows(true, 'id') : array()); @@ -120,7 +132,7 @@ class CategoryService { } public static function getById($rID) { - global $db; + $db = self::db(); $db->query('SELECT * FROM `streams_categories` WHERE `id` = ?;', $rID); if ($db->num_rows() != 1) { @@ -131,7 +143,7 @@ class CategoryService { } public static function deleteById($rID) { - global $db; + $db = self::db(); $rCategory = self::getById($rID); if (!$rCategory) { diff --git a/src/domain/Stream/ChannelService.php b/src/domain/Stream/ChannelService.php index 18ff79e9..b6560849 100644 --- a/src/domain/Stream/ChannelService.php +++ b/src/domain/Stream/ChannelService.php @@ -11,8 +11,21 @@ */ class ChannelService { + private static $db = null; + + public static function setDb($db): void { + self::$db = $db; + } + + private static function db(): object { + if (self::$db === null) { + throw new \RuntimeException(static::class . '::setDb() must be called before use.'); + } + return self::$db; + } public static function process($rData) { - global $db, $rSettings; + global $rSettings; + $db = self::db(); if (isset($rData['edit'])) { if (Authorization::check('adv', 'edit_cchannel')) { $rArray = AdminHelpers::overwriteData(StreamRepository::getById($rData['edit']), $rData); @@ -213,7 +226,7 @@ class ChannelService { } public static function massEdit($rData) { - global $db; + $db = self::db(); set_time_limit(0); ini_set('mysql.connect_timeout', 0); ini_set('max_execution_time', 0); @@ -439,7 +452,7 @@ class ChannelService { } public static function setOrder($rData) { - global $db; + $db = self::db(); set_time_limit(0); ini_set('mysql.connect_timeout', 0); ini_set('max_execution_time', 0); diff --git a/src/domain/Stream/ConnectionTracker.php b/src/domain/Stream/ConnectionTracker.php index a9639ff0..2f8de570 100644 --- a/src/domain/Stream/ConnectionTracker.php +++ b/src/domain/Stream/ConnectionTracker.php @@ -16,6 +16,18 @@ */ class ConnectionTracker { + private static $db = null; + + public static function setDb($db): void { + self::$db = $db; + } + + private static function db(): object { + if (self::$db === null) { + throw new \RuntimeException(static::class . '::setDb() must be called before use.'); + } + return self::$db; + } /** * Calculate server/proxy load capacity. * @@ -27,7 +39,8 @@ class ConnectionTracker { * @return array Map of serverID => load data. */ public static function getCapacity(bool $rProxy = false): array { - global $rSettings, $rServers, $db; + global $rSettings, $rServers; + $db = self::db(); $rRedis = RedisManager::instance(); $rFile = ($rProxy ? 'proxy_capacity' : 'servers_capacity'); if ($rSettings['redis_handler'] && $rProxy && $rSettings['split_by'] == 'maxclients') { @@ -118,7 +131,8 @@ class ConnectionTracker { * @return array Connections: [keys[], data[]] for Redis or rows for MySQL. */ public static function getConnections(?int $rServerID = null, ?int $rUserID = null, ?int $rStreamID = null): array { - global $rSettings, $db; + global $rSettings; + $db = self::db(); $rRedis = RedisManager::instance(); if ($rSettings['redis_handler'] && $rRedis) { if ($rServerID) { @@ -669,7 +683,8 @@ class ConnectionTracker { */ public static function closeConnection($rActivityInfo, bool $rRemove = true, bool $rEnd = true): bool { if (!empty($rActivityInfo)) { - global $rSettings, $rServers, $db; + global $rSettings, $rServers; + $db = self::db(); if (!$rSettings['redis_handler'] || is_object(RedisManager::instance())) { } else { RedisManager::ensureConnected(); @@ -810,7 +825,7 @@ class ConnectionTracker { * @return int Number of active connections. */ public static function getLiveConnections(int $rServerID, bool $rProxy = false): int { - global $db; + $db = self::db(); if (SettingsManager::getAll()['redis_handler']) { $rCount = 0; diff --git a/src/domain/Stream/PlaylistGenerator.php b/src/domain/Stream/PlaylistGenerator.php index 930faeee..f4e2fd99 100644 --- a/src/domain/Stream/PlaylistGenerator.php +++ b/src/domain/Stream/PlaylistGenerator.php @@ -11,8 +11,21 @@ */ class PlaylistGenerator { + private static $db = null; + + public static function setDb($db): void { + self::$db = $db; + } + + private static function db(): object { + if (self::$db === null) { + throw new \RuntimeException(static::class . '::setDb() must be called before use.'); + } + return self::$db; + } public static function generate($rUserInfo, $rDeviceKey, $rOutputKey = 'ts', $rTypeKey = null, $rNoCache = false, $rProxy = false) { - global $db, $rSettings, $rServers; + global $rSettings, $rServers; + $db = self::db(); $rCategories = CategoryService::getFromDatabase(); $rCached = $rSettings['enable_cache']; if (empty($rDeviceKey)) { diff --git a/src/domain/Stream/ProfileService.php b/src/domain/Stream/ProfileService.php index 38caf643..becc4bc8 100644 --- a/src/domain/Stream/ProfileService.php +++ b/src/domain/Stream/ProfileService.php @@ -11,8 +11,20 @@ */ class ProfileService { + private static $db = null; + + public static function setDb($db): void { + self::$db = $db; + } + + private static function db(): object { + if (self::$db === null) { + throw new \RuntimeException(static::class . '::setDb() must be called before use.'); + } + return self::$db; + } public static function process($rData) { - global $db; + $db = self::db(); if (InputValidator::validate('processProfile', $rData)) { $rArray = array('profile_name' => $rData['profile_name'], 'profile_options' => null); $rProfileOptions = array(); diff --git a/src/domain/Stream/ProviderService.php b/src/domain/Stream/ProviderService.php index 71707125..931111e7 100644 --- a/src/domain/Stream/ProviderService.php +++ b/src/domain/Stream/ProviderService.php @@ -11,8 +11,20 @@ */ class ProviderService { + private static $db = null; + + public static function setDb($db): void { + self::$db = $db; + } + + private static function db(): object { + if (self::$db === null) { + throw new \RuntimeException(static::class . '::setDb() must be called before use.'); + } + return self::$db; + } public static function process($rData) { - global $db; + $db = self::db(); if (InputValidator::validate('processProvider', $rData)) { if (isset($rData['edit'])) { if (Authorization::check('adv', 'streams')) { @@ -62,7 +74,7 @@ class ProviderService { } public static function getById($rID) { - global $db; + $db = self::db(); $db->query('SELECT * FROM `providers` WHERE `id` = ?;', $rID); if ($db->num_rows() != 1) { @@ -73,7 +85,7 @@ class ProviderService { } public static function getAll() { - global $db; + $db = self::db(); $rReturn = array(); $db->query('SELECT * FROM `providers` ORDER BY `last_changed` DESC;'); @@ -87,7 +99,7 @@ class ProviderService { } public static function deleteById($rID) { - global $db; + $db = self::db(); $rProvider = self::getById($rID); if (!$rProvider) { diff --git a/src/domain/Stream/RadioService.php b/src/domain/Stream/RadioService.php index 14fb53b1..58947790 100644 --- a/src/domain/Stream/RadioService.php +++ b/src/domain/Stream/RadioService.php @@ -11,8 +11,20 @@ */ class RadioService { + private static $db = null; + + public static function setDb($db): void { + self::$db = $db; + } + + private static function db(): object { + if (self::$db === null) { + throw new \RuntimeException(static::class . '::setDb() must be called before use.'); + } + return self::$db; + } public static function process($rData) { - global $db; + $db = self::db(); if (InputValidator::validate('processRadio', $rData)) { if (isset($rData['edit'])) { if (Authorization::check('adv', 'edit_radio')) { @@ -256,7 +268,7 @@ class RadioService { } public static function massEdit($rData) { - global $db; + $db = self::db(); set_time_limit(0); ini_set('mysql.connect_timeout', 0); ini_set('max_execution_time', 0); diff --git a/src/domain/Stream/StreamConfigRepository.php b/src/domain/Stream/StreamConfigRepository.php index b3b4fdfc..a7e8bc5d 100644 --- a/src/domain/Stream/StreamConfigRepository.php +++ b/src/domain/Stream/StreamConfigRepository.php @@ -11,11 +11,23 @@ */ class StreamConfigRepository { + private static $db = null; + + public static function setDb($db): void { + self::$db = $db; + } + + private static function db(): object { + if (self::$db === null) { + throw new \RuntimeException(static::class . '::setDb() must be called before use.'); + } + return self::$db; + } /** * Получить все аргументы потоков (streams_arguments), индексированные по argument_key. */ public static function getStreamArguments() { - global $db; + $db = self::db(); $rReturn = array(); $db->query('SELECT * FROM `streams_arguments` ORDER BY `id` ASC;'); @@ -32,7 +44,7 @@ class StreamConfigRepository { * Получить все профили транскодирования (profiles). */ public static function getTranscodeProfiles() { - global $db; + $db = self::db(); $rReturn = array(); $db->query('SELECT * FROM `profiles` ORDER BY `profile_id` ASC;'); @@ -46,7 +58,7 @@ class StreamConfigRepository { } public static function getTranscodeProfile($rID) { - global $db; + $db = self::db(); $db->query('SELECT * FROM `profiles` WHERE `profile_id` = ?;', $rID); if ($db->num_rows() != 1) { @@ -57,7 +69,7 @@ class StreamConfigRepository { } public static function deleteProfile($rID) { - global $db; + $db = self::db(); $rProfile = self::getTranscodeProfile($rID); if (!$rProfile) { diff --git a/src/domain/Stream/StreamProcess.php b/src/domain/Stream/StreamProcess.php index e087bab1..d8b48576 100644 --- a/src/domain/Stream/StreamProcess.php +++ b/src/domain/Stream/StreamProcess.php @@ -11,6 +11,18 @@ */ class StreamProcess { + private static $db = null; + + public static function setDb($db): void { + self::$db = $db; + } + + private static function db(): object { + if (self::$db === null) { + throw new \RuntimeException(static::class . '::setDb() must be called before use.'); + } + return self::$db; + } /** * Write stream action log to file * @@ -42,7 +54,7 @@ class StreamProcess { } public static function queueChannel($rStreamID, $rServerID = null) { - global $db; + $db = self::db(); if ($rServerID) { } else { $rServerID = SERVER_ID; @@ -75,7 +87,7 @@ class StreamProcess { } public static function updateStream($rStreamID, $rForce = false) { - global $db; + $db = self::db(); $rCached = SettingsManager::getAll()['enable_cache']; $rMainID = ConnectionTracker::getMainID(); if ($rCached) { @@ -90,7 +102,7 @@ class StreamProcess { } public static function updateStreams($rStreamIDs) { - global $db; + $db = self::db(); $rCached = SettingsManager::getAll()['enable_cache']; $rMainID = ConnectionTracker::getMainID(); if ($rCached) { @@ -105,7 +117,8 @@ class StreamProcess { } public static function createChannelItem($rStreamID, $rSource) { - global $db, $rSettings, $rServers, $rFFMPEG_CPU, $rFFMPEG_GPU; + global $rSettings, $rServers, $rFFMPEG_CPU, $rFFMPEG_GPU; + $db = self::db(); $rStream = array(); $rLoopback = false; $db->query('SELECT * FROM `streams` t1 INNER JOIN `streams_types` t2 ON t2.type_id = t1.type AND t1.type = 3 LEFT JOIN `profiles` t4 ON t1.transcode_profile_id = t4.profile_id WHERE t1.direct_source = 0 AND t1.id = ?', $rStreamID); @@ -206,7 +219,7 @@ class StreamProcess { } public static function stopStream($rStreamID, $rStop = false) { - global $db; + $db = self::db(); if (file_exists(STREAMS_PATH . $rStreamID . '_.monitor')) { $rMonitor = intval(file_get_contents(STREAMS_PATH . $rStreamID . '_.monitor')); } else { @@ -246,7 +259,7 @@ class StreamProcess { } public static function stopMovie($rStreamID, $rForce = false) { - global $db; + $db = self::db(); shell_exec("kill -9 `ps -ef | grep '/" . intval($rStreamID) . ".' | grep -v grep | awk '{print \$2}'`;"); if ($rForce) { exec('rm ' . MAIN_HOME . 'content/vod/' . intval($rStreamID) . '.*'); @@ -258,7 +271,7 @@ class StreamProcess { } public static function queueMovie($rStreamID, $rServerID = null) { - global $db; + $db = self::db(); if ($rServerID) { } else { $rServerID = SERVER_ID; @@ -268,7 +281,7 @@ class StreamProcess { } public static function queueMovies($rStreamIDs, $rServerID = null) { - global $db; + $db = self::db(); if ($rServerID) { } else { $rServerID = SERVER_ID; @@ -292,7 +305,7 @@ class StreamProcess { } public static function refreshMovies($rIDs, $rType = 1) { - global $db; + $db = self::db(); if (0 >= count($rIDs)) { } else { $db->query('DELETE FROM `watch_refresh` WHERE `type` = ? AND `stream_id` IN (' . implode(',', array_map('intval', $rIDs)) . ');', $rType); @@ -312,7 +325,8 @@ class StreamProcess { } public static function startMovie($rStreamID) { - global $db, $rSettings, $rServers, $rFFMPEG_CPU, $rFFMPEG_GPU; + global $rSettings, $rServers, $rFFMPEG_CPU, $rFFMPEG_GPU; + $db = self::db(); $rStream = array(); $rLoopback = false; $db->query('SELECT * FROM `streams` t1 INNER JOIN `streams_types` t2 ON t2.type_id = t1.type AND t2.live = 0 LEFT JOIN `profiles` t4 ON t1.transcode_profile_id = t4.profile_id WHERE t1.direct_source = 0 AND t1.id = ?', $rStreamID); @@ -460,7 +474,8 @@ class StreamProcess { } public static function startLoopback($rStreamID) { - global $db, $rSettings, $rServers; + global $rSettings, $rServers; + $db = self::db(); shell_exec('rm -f ' . STREAMS_PATH . intval($rStreamID) . '_*.ts'); if (!file_exists(STREAMS_PATH . $rStreamID . '_.pid')) { } else { @@ -495,7 +510,7 @@ class StreamProcess { } public static function startLLOD($rStreamID, $rStreamInfo, $rStreamArguments, $rForceSource = null) { - global $db; + $db = self::db(); shell_exec('rm -f ' . STREAMS_PATH . intval($rStreamID) . '_*.ts'); if (file_exists(STREAMS_PATH . $rStreamID . '_.pid')) { unlink(STREAMS_PATH . $rStreamID . '_.pid'); @@ -518,7 +533,8 @@ class StreamProcess { } public static function startStream($rStreamID, $rFromCache = false, $rForceSource = null, $rLLOD = false, $rStartPos = 0) { - global $db, $rSettings, $rServers, $rFFMPEG_CPU, $rFFMPEG_GPU, $rFFPROBE; + global $rSettings, $rServers, $rFFMPEG_CPU, $rFFMPEG_GPU, $rFFPROBE; + $db = self::db(); $rSegmentSettings = array('seg_time' => intval($rSettings['seg_time']), 'seg_list_size' => intval($rSettings['seg_list_size']), 'seg_delete_threshold' => intval($rSettings['seg_delete_threshold'])); if (file_exists(STREAMS_PATH . $rStreamID . '_.pid')) { unlink(STREAMS_PATH . $rStreamID . '_.pid'); diff --git a/src/domain/Stream/StreamRepository.php b/src/domain/Stream/StreamRepository.php index ac9f457b..f6ca28d6 100644 --- a/src/domain/Stream/StreamRepository.php +++ b/src/domain/Stream/StreamRepository.php @@ -11,14 +11,26 @@ */ class StreamRepository { + private static $db = null; + + public static function setDb($db): void { + self::$db = $db; + } + + private static function db(): object { + if (self::$db === null) { + throw new \RuntimeException(static::class . '::setDb() must be called before use.'); + } + return self::$db; + } public static function getErrors($rStreamID, $rAmount = 250) { - global $db; + $db = self::db(); $db->query('SELECT * FROM (SELECT MAX(`date`) AS `date`, `error` FROM `streams_errors` WHERE `stream_id` = ? GROUP BY `error`) AS `output` ORDER BY `date` DESC LIMIT ' . intval($rAmount) . ';', $rStreamID); return $db->get_rows(); } public static function getById($rID) { - global $db; + $db = self::db(); $db->query('SELECT * FROM `streams` WHERE `id` = ?;', $rID); if ($db->num_rows() == 1) { @@ -27,7 +39,7 @@ class StreamRepository { } public static function getStats($rStreamID) { - global $db; + $db = self::db(); $rReturn = array(); $db->query('SELECT * FROM `streams_stats` WHERE `stream_id` = ?;', $rStreamID); @@ -47,7 +59,8 @@ class StreamRepository { } public static function getPIDs($rServerID) { - global $db, $rSettings; + global $rSettings; + $db = self::db(); $rReturn = array(); $db->query('SELECT `streams`.`id`, `streams`.`stream_display_name`, `streams`.`type`, `streams_servers`.`pid`, `streams_servers`.`monitor_pid`, `streams_servers`.`delay_pid` FROM `streams_servers` LEFT JOIN `streams` ON `streams`.`id` = `streams_servers`.`stream_id` WHERE `streams_servers`.`server_id` = ?;', $rServerID); @@ -112,7 +125,7 @@ class StreamRepository { } public static function getOptions($rID) { - global $db; + $db = self::db(); $rReturn = array(); $db->query('SELECT * FROM `streams_options` WHERE `stream_id` = ?;', $rID); @@ -126,7 +139,7 @@ class StreamRepository { } public static function getSystemRows($rID) { - global $db; + $db = self::db(); $rReturn = array(); $db->query('SELECT * FROM `streams_servers` WHERE `stream_id` = ?;', $rID); @@ -140,7 +153,7 @@ class StreamRepository { } public static function getNextOrder() { - global $db; + $db = self::db(); $db->query('SELECT MAX(`order`) AS `order` FROM `streams`;'); if ($db->num_rows() != 1) { @@ -152,7 +165,7 @@ class StreamRepository { } public static function getEncodeErrors($rID) { - global $db; + $db = self::db(); $rErrors = array(); $db->query('SELECT `server_id`, `error` FROM `streams_errors` WHERE `stream_id` = ?;', $rID); @@ -164,7 +177,7 @@ class StreamRepository { } public static function getSelections($rSources) { - global $db; + $db = self::db(); $rReturn = array(); foreach ($rSources as $rSource) { @@ -180,7 +193,7 @@ class StreamRepository { } public static function deleteStream($rID, $rServerID = -1, $rDeleteFiles = true, $f2d619cb38696890 = true) { - global $db; + $db = self::db(); $db->query('SELECT `id`, `type` FROM `streams` WHERE `id` = ?;', $rID); if (0 >= $db->num_rows()) { @@ -240,7 +253,7 @@ class StreamRepository { } public static function deleteStreams($rIDs, $rDeleteFiles = false) { - global $db; + $db = self::db(); $rIDs = AdminHelpers::confirmIDs($rIDs); if (0 >= count($rIDs)) { @@ -276,7 +289,7 @@ class StreamRepository { } public static function deleteStreamsByServer($rIDs, $rServerID, $rDeleteFiles = false) { - global $db; + $db = self::db(); $rIDs = AdminHelpers::confirmIDs($rIDs); if (0 >= count($rIDs)) { @@ -293,7 +306,7 @@ class StreamRepository { } public static function getWatchFolder($rID) { - global $db; + $db = self::db(); $db->query('SELECT * FROM `watch_folders` WHERE `id` = ?;', $rID); if ($db->num_rows() != 1) { @@ -303,7 +316,7 @@ class StreamRepository { } public static function deleteWatchFolder($rID) { - global $db; + $db = self::db(); $db->query('SELECT `id` FROM `watch_folders` WHERE `id` = ?;', $rID); if (0 >= $db->num_rows()) { diff --git a/src/domain/Stream/StreamService.php b/src/domain/Stream/StreamService.php index 9fe36ab1..b36ba49b 100644 --- a/src/domain/Stream/StreamService.php +++ b/src/domain/Stream/StreamService.php @@ -11,8 +11,21 @@ */ class StreamService { + private static $db = null; + + public static function setDb($db): void { + self::$db = $db; + } + + private static function db(): object { + if (self::$db === null) { + throw new \RuntimeException(static::class . '::setDb() must be called before use.'); + } + return self::$db; + } public static function process($rData) { - global $db, $rSettings; + global $rSettings; + $db = self::db(); set_time_limit(0); ini_set('mysql.connect_timeout', 0); ini_set('max_execution_time', 0); @@ -437,7 +450,7 @@ class StreamService { } public static function massEdit($rData) { - global $db; + $db = self::db(); set_time_limit(0); ini_set('mysql.connect_timeout', 0); ini_set('max_execution_time', 0); @@ -698,7 +711,7 @@ class StreamService { } public static function move($rData) { - global $db; + $db = self::db(); $rType = intval($rData['content_type']); $rSource = intval($rData['source_server']); $rReplacement = intval($rData['replacement_server']); @@ -739,7 +752,7 @@ class StreamService { } public static function replaceDNS($rData) { - global $db; + $db = self::db(); $rOldDNS = str_replace('/', '\\/', $rData['old_dns']); $rNewDNS = str_replace('/', '\\/', $rData['new_dns']); $db->query('UPDATE `streams` SET `stream_source` = REPLACE(`stream_source`, ?, ?);', $rOldDNS, $rNewDNS); @@ -776,7 +789,7 @@ class StreamService { } public static function getArchive($rStreamID) { - global $db; + $db = self::db(); $rReturn = array(); $rStream = StreamRepository::getById($rStreamID); $rEPG = EpgService::getChannelEpg($rStream, true); diff --git a/src/domain/User/GroupService.php b/src/domain/User/GroupService.php index 1d248e4b..5b51baca 100644 --- a/src/domain/User/GroupService.php +++ b/src/domain/User/GroupService.php @@ -11,8 +11,20 @@ */ class GroupService { + private static $db = null; + + public static function setDb($db): void { + self::$db = $db; + } + + private static function db(): object { + if (self::$db === null) { + throw new \RuntimeException(static::class . '::setDb() must be called before use.'); + } + return self::$db; + } public static function process($rData) { - global $db; + $db = self::db(); if (InputValidator::validate('processGroup', $rData)) { if (isset($rData['edit'])) { if (Authorization::check('adv', 'edit_group')) { @@ -100,7 +112,7 @@ class GroupService { // ──────────── Из GroupRepository ──────────── public static function getAll() { - global $db; + $db = self::db(); $rReturn = array(); $db->query('SELECT * FROM `users_groups` ORDER BY `group_id` ASC;'); @@ -115,7 +127,7 @@ class GroupService { } public static function getById($rID) { - global $db; + $db = self::db(); $db->query('SELECT * FROM `users_groups` WHERE `group_id` = ?;', $rID); if ($db->num_rows() != 1) { @@ -125,7 +137,7 @@ class GroupService { } public static function deleteById($rID) { - global $db; + $db = self::db(); $rGroup = self::getById($rID); if (!($rGroup && $rGroup['can_delete'])) { diff --git a/src/domain/User/ResellerAPI.php b/src/domain/User/ResellerAPI.php index affafa32..56cd2abb 100644 --- a/src/domain/User/ResellerAPI.php +++ b/src/domain/User/ResellerAPI.php @@ -11,6 +11,18 @@ */ class ResellerAPI { + private static $db = null; + + public static function setDb($db): void { + self::$db = $db; + } + + private static function db(): object { + if (self::$db === null) { + throw new \RuntimeException(static::class . '::setDb() must be called before use.'); + } + return self::$db; + } public static $rSettings = array(); public static $rServers = array(); public static $rProxyServers = array(); @@ -31,7 +43,7 @@ class ResellerAPI { } public static function init($rUserID = null) { - global $db; + $db = self::db(); global $rPermissions; self::$rSettings = SettingsManager::getAll(); self::$rServers = ServerRepository::getStreamingSimple($rPermissions); @@ -50,7 +62,7 @@ class ResellerAPI { } public static function editResellerProfile($rData) { - global $db; + $db = self::db(); global $rHues; global $allowedLangs; $rData = self::processData('profile', $rData); @@ -98,7 +110,7 @@ class ResellerAPI { } public static function processMAG($rData) { - global $db; + $db = self::db(); $rData = self::processData('mag', $rData); if (self::$rPermissions['create_mag']) { @@ -379,7 +391,7 @@ class ResellerAPI { } public static function processEnigma($rData) { - global $db; + $db = self::db(); $rData = self::processData('enigma', $rData); if (self::$rPermissions['create_enigma']) { @@ -658,7 +670,7 @@ class ResellerAPI { } public static function processUser($rData) { - global $db; + $db = self::db(); $rData = self::processData('user', $rData); if (self::$rPermissions['create_sub_resellers']) { @@ -780,7 +792,7 @@ class ResellerAPI { } public static function submitTicket($rData) { - global $db; + $db = self::db(); $rData = self::processData('ticket', $rData); if (isset($rData['edit'])) { @@ -840,7 +852,7 @@ class ResellerAPI { } public static function processLine($rData) { - global $db; + $db = self::db(); $rData = self::processData('line', $rData); if (self::$rPermissions['create_line']) { diff --git a/src/domain/User/TicketRepository.php b/src/domain/User/TicketRepository.php index b3d743e1..a96c2b53 100644 --- a/src/domain/User/TicketRepository.php +++ b/src/domain/User/TicketRepository.php @@ -11,8 +11,20 @@ */ class TicketRepository { + private static $db = null; + + public static function setDb($db): void { + self::$db = $db; + } + + private static function db(): object { + if (self::$db === null) { + throw new \RuntimeException(static::class . '::setDb() must be called before use.'); + } + return self::$db; + } public static function getById($rID) { - global $db; + $db = self::db(); $db->query('SELECT * FROM `tickets` WHERE `id` = ?;', $rID); if (0 >= $db->num_rows()) { @@ -39,7 +51,7 @@ class TicketRepository { } public static function getAll($rID = null, $rAdmin = false) { - global $db; + $db = self::db(); global $rUserInfo; global $rPermissions; $rReturn = array(); @@ -113,7 +125,7 @@ class TicketRepository { } public static function deleteById($rID) { - global $db; + $db = self::db(); $db->query('SELECT `id` FROM `tickets` WHERE `id` = ?;', $rID); if (0 >= $db->num_rows()) { diff --git a/src/domain/User/UserRepository.php b/src/domain/User/UserRepository.php index 0278c7b3..e368084f 100644 --- a/src/domain/User/UserRepository.php +++ b/src/domain/User/UserRepository.php @@ -11,8 +11,20 @@ */ class UserRepository { + private static $db = null; + + public static function setDb($db): void { + self::$db = $db; + } + + private static function db(): object { + if (self::$db === null) { + throw new \RuntimeException(static::class . '::setDb() must be called before use.'); + } + return self::$db; + } public static function getAuthUserByCredentials($rUsername, $rPassword) { - global $db; + $db = self::db(); $db->query('SELECT `id`, `username`, `password`, `member_group_id`, `status` FROM `users` WHERE `username` = ? LIMIT 1;', $rUsername); if ($db->num_rows() == 1) { @@ -25,7 +37,7 @@ class UserRepository { } public static function getResellers($rOwner, $rIncludeSelf = true) { - global $db; + $db = self::db(); if ($rIncludeSelf) { $db->query('SELECT `id`, `username` FROM `users` WHERE `owner_id` = ? OR `id` = ? ORDER BY `username` ASC;', $rOwner, $rOwner); } else { @@ -36,7 +48,7 @@ class UserRepository { } public static function getDirectReports($rPermissions, $rUserInfo, $rIncludeSelf = true) { - global $db; + $db = self::db(); $rUserIDs = $rPermissions['direct_reports']; if (!$rIncludeSelf) { @@ -70,7 +82,7 @@ class UserRepository { } public static function getSubUsers($rUser) { - global $db; + $db = self::db(); $rReturn = array(); $db->query('SELECT `id`, `username` FROM `users` WHERE `owner_id` = ?;', $rUser); @@ -86,7 +98,7 @@ class UserRepository { } public static function getLineById($rID) { - global $db; + $db = self::db(); $db->query('SELECT * FROM `lines` WHERE `id` = ?;', $rID); if ($db->num_rows() != 1) { @@ -96,7 +108,7 @@ class UserRepository { } public static function getRegisteredUserById($rID) { - global $db; + $db = self::db(); $db->query('SELECT * FROM `users` WHERE `id` = ?;', $rID); if ($db->num_rows() != 1) { @@ -106,7 +118,7 @@ class UserRepository { } public static function getRegisteredUsers($rOwner = null, $rIncludeSelf = true) { - global $db; + $db = self::db(); $rReturn = array(); $db->query('SELECT * FROM `users` ORDER BY `username` ASC;'); @@ -128,7 +140,8 @@ class UserRepository { } public static function getStreamingUserInfo($rSettings, $rCached, $rBouquets, $rUserID = null, $rUsername = null, $rPassword = null, $rGetChannelIDs = false, $rGetConnections = false, $rIP = '') { - global $db, $rBlockedISP, $rBlockedServers; + global $rBlockedISP, $rBlockedServers; + $db = self::db(); $rUserInfo = null; if ($rCached) { @@ -311,7 +324,8 @@ class UserRepository { } public static function getUserInfo($rUserID = null, $rUsername = null, $rPassword = null, $rGetChannelIDs = false, $rGetConnections = false, $rIP = '') { - global $db, $rSettings; + global $rSettings; + $db = self::db(); $rCached = $rSettings['enable_cache']; $rBouquets = BouquetService::getAll(); $rUserInfo = null; @@ -491,7 +505,7 @@ class UserRepository { } public static function getE2Info($rDevice, $rGetChannelIDs = false, $rGetBouquetInfo = false, $rGetConnections = false) { - global $db; + $db = self::db(); if (empty($rDevice['device_id'])) { $db->query('SELECT * FROM `enigma2_devices` WHERE `mac` = ?', $rDevice['mac']); } else { diff --git a/src/domain/User/UserService.php b/src/domain/User/UserService.php index 01517af2..ef772a5e 100644 --- a/src/domain/User/UserService.php +++ b/src/domain/User/UserService.php @@ -11,6 +11,18 @@ */ class UserService { + private static $db = null; + + public static function setDb($db): void { + self::$db = $db; + } + + private static function db(): object { + if (self::$db === null) { + throw new \RuntimeException(static::class . '::setDb() must be called before use.'); + } + return self::$db; + } public static function massDelete($rData) { set_time_limit(0); ini_set('mysql.connect_timeout', 0); @@ -24,7 +36,7 @@ class UserService { } public static function massEdit($rData) { - global $db; + $db = self::db(); if (InputValidator::validate('massEditUsers', $rData)) { $rArray = array(); @@ -93,7 +105,7 @@ class UserService { } public static function process($rData, $rBypassAuth = false) { - global $db; + $db = self::db(); if (InputValidator::validate('processUser', $rData)) { if (isset($rData['edit'])) { if (Authorization::check('adv', 'edit_reguser') || $rBypassAuth) { @@ -177,7 +189,7 @@ class UserService { } public static function editAdminProfile($rData, $rUserInfo, $allowedLangs) { - global $db; + $db = self::db(); if (!(0 >= strlen($rData['email']) || filter_var($rData['email'], FILTER_VALIDATE_EMAIL))) { return array('status' => STATUS_INVALID_EMAIL); } @@ -202,7 +214,7 @@ class UserService { } public static function submitTicket($rData, $rUserInfo) { - global $db; + $db = self::db(); if (isset($rData['edit'])) { $rArray = AdminHelpers::overwriteData(TicketRepository::getById($rData['edit']), $rData); } else { @@ -244,7 +256,7 @@ class UserService { } public static function deleteRegisteredUser($rID, $rDeleteSubUsers = false, $rDeleteLines = false, $rReplaceWith = null) { - global $db; + $db = self::db(); $rUser = UserRepository::getRegisteredUserById($rID); if (!$rUser) { @@ -281,7 +293,7 @@ class UserService { } public static function deleteRegisteredUsers($rIDs) { - global $db; + $db = self::db(); $rIDs = AdminHelpers::confirmIDs($rIDs); if (0 >= count($rIDs)) { diff --git a/src/domain/Vod/EpisodeService.php b/src/domain/Vod/EpisodeService.php index 7d3e480e..457c5b98 100644 --- a/src/domain/Vod/EpisodeService.php +++ b/src/domain/Vod/EpisodeService.php @@ -11,8 +11,21 @@ */ class EpisodeService { + private static $db = null; + + public static function setDb($db): void { + self::$db = $db; + } + + private static function db(): object { + if (self::$db === null) { + throw new \RuntimeException(static::class . '::setDb() must be called before use.'); + } + return self::$db; + } public static function process($rData) { - global $db, $rSettings; + global $rSettings; + $db = self::db(); if (isset($rData['edit'])) { if (Authorization::check('adv', 'edit_episode')) { $rArray = AdminHelpers::overwriteData(StreamRepository::getById($rData['edit']), $rData); @@ -259,7 +272,7 @@ class EpisodeService { } public static function massEdit($rData) { - global $db; + $db = self::db(); set_time_limit(0); ini_set('mysql.connect_timeout', 0); ini_set('max_execution_time', 0); diff --git a/src/domain/Vod/MovieService.php b/src/domain/Vod/MovieService.php index 652f56db..4161da4a 100644 --- a/src/domain/Vod/MovieService.php +++ b/src/domain/Vod/MovieService.php @@ -11,8 +11,20 @@ */ class MovieService { + private static $db = null; + + public static function setDb($db): void { + self::$db = $db; + } + + private static function db(): object { + if (self::$db === null) { + throw new \RuntimeException(static::class . '::setDb() must be called before use.'); + } + return self::$db; + } public static function process($rData) { - global $db; + $db = self::db(); if (InputValidator::validate('processMovie', $rData)) { set_time_limit(0); ini_set('mysql.connect_timeout', 0); @@ -475,7 +487,7 @@ class MovieService { } public static function import($rData) { - global $db; + $db = self::db(); if (Authorization::check('adv', 'import_movies')) { if (InputValidator::validate('importMovies', $rData)) { $rPostData = $rData; @@ -703,7 +715,7 @@ class MovieService { } public static function massEdit($rData) { - global $db; + $db = self::db(); set_time_limit(0); ini_set('mysql.connect_timeout', 0); ini_set('max_execution_time', 0); @@ -937,7 +949,7 @@ class MovieService { * Send delete signal for movie files on specified servers. */ public static function deleteFile($rServerIDs, $rID) { - global $db; + $db = self::db(); if (is_array($rServerIDs)) { } else { $rServerIDs = array($rServerIDs); diff --git a/src/domain/Vod/SeriesService.php b/src/domain/Vod/SeriesService.php index 056a896e..f8411f5d 100644 --- a/src/domain/Vod/SeriesService.php +++ b/src/domain/Vod/SeriesService.php @@ -11,8 +11,20 @@ */ class SeriesService { + private static $db = null; + + public static function setDb($db): void { + self::$db = $db; + } + + private static function db(): object { + if (self::$db === null) { + throw new \RuntimeException(static::class . '::setDb() must be called before use.'); + } + return self::$db; + } public static function process($rData) { - global $db; + $db = self::db(); if (InputValidator::validate('processSeries', $rData)) { if (isset($rData['edit'])) { if (Authorization::check('adv', 'edit_series')) { @@ -129,7 +141,7 @@ class SeriesService { } public static function import($rData) { - global $db; + $db = self::db(); if (Authorization::check('adv', 'import_movies')) { if (InputValidator::validate('importSeries', $rData)) { $rPostData = $rData; @@ -326,7 +338,7 @@ class SeriesService { } public static function massEdit($rData) { - global $db; + $db = self::db(); set_time_limit(0); ini_set('mysql.connect_timeout', 0); ini_set('max_execution_time', 0); @@ -442,7 +454,7 @@ class SeriesService { * Get all series as id => row array, ordered by title. */ public static function getList() { - global $db; + $db = self::db(); $rReturn = array(); $db->query('SELECT `id`, `title` FROM `streams_series` ORDER BY `title` ASC;'); @@ -459,7 +471,7 @@ class SeriesService { * Update series seasons from TMDB. */ public static function updateFromTMDB($rID) { - global $db; + $db = self::db(); require_once MAIN_HOME . 'modules/tmdb/lib/TmdbClient.php'; $db->query('SELECT `tmdb_id`, `tmdb_language` FROM `streams_series` WHERE `id` = ?;', $rID); @@ -505,7 +517,7 @@ class SeriesService { * Queue async series refresh via watch_refresh table. */ public static function queueRefresh($rID) { - global $db; + $db = self::db(); $db->query('INSERT INTO `watch_refresh`(`type`, `stream_id`, `status`) VALUES(4, ?, 0);', $rID); } @@ -513,7 +525,7 @@ class SeriesService { * Generate playlist of episode sources for a series. */ public static function generatePlaylist($rSeriesNo) { - global $db; + $db = self::db(); $rReturn = array(); $db->query('SELECT `stream_id` FROM `streams_episodes` WHERE `series_id` = ? ORDER BY `season_num` ASC, `episode_num` ASC;', $rSeriesNo); @@ -534,7 +546,7 @@ class SeriesService { } public static function getById($rID) { - global $db; + $db = self::db(); $db->query('SELECT * FROM `streams_series` WHERE `id` = ?;', $rID); if ($db->num_rows() != 1) { @@ -545,7 +557,7 @@ class SeriesService { } public static function getAll() { - global $db; + $db = self::db(); $rReturn = array(); $db->query('SELECT * FROM `streams_series` ORDER BY `title` ASC;'); @@ -559,7 +571,7 @@ class SeriesService { } public static function getByTMDBId($rID) { - global $db; + $db = self::db(); $db->query('SELECT * FROM `streams_series` WHERE `tmdb_id` = ?;', $rID); if ($db->num_rows() != 1) { @@ -570,7 +582,7 @@ class SeriesService { } public static function deleteSeriesById($rID, $rDeleteFiles = true) { - global $db; + $db = self::db(); $rSeries = self::getById($rID); if (!$rSeries) { @@ -590,7 +602,7 @@ class SeriesService { } public static function deleteSeriesByIds($rIDs) { - global $db; + $db = self::db(); $rIDs = AdminHelpers::confirmIDs($rIDs); if (0 >= count($rIDs)) { diff --git a/src/domain/Vod/TMDbService.php b/src/domain/Vod/TMDbService.php index 765226ba..f0e21949 100644 --- a/src/domain/Vod/TMDbService.php +++ b/src/domain/Vod/TMDbService.php @@ -11,6 +11,18 @@ */ class TMDbService { + private static $db = null; + + public static function setDb($db): void { + self::$db = $db; + } + + private static function db(): object { + if (self::$db === null) { + throw new \RuntimeException(static::class . '::setDb() must be called before use.'); + } + return self::$db; + } public static function getMovie($rID) { require_once MAIN_HOME . 'modules/tmdb/lib/TmdbClient.php'; @@ -83,7 +95,7 @@ class TMDbService { } public static function addCategories() { - global $db; + $db = self::db(); require_once MAIN_HOME . 'modules/tmdb/lib/TmdbClient.php'; if (0 < strlen(SettingsManager::getAll()['tmdb_language'])) { @@ -124,7 +136,7 @@ class TMDbService { } public static function updateCategories() { - global $db; + $db = self::db(); require_once MAIN_HOME . 'modules/tmdb/lib/TmdbClient.php'; if (0 < strlen(SettingsManager::getAll()['tmdb_language'])) {