| Total Complexity | 65 |
| Total Lines | 419 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| Bugs | 0 | Features | 0 |
Complex classes like System often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use System, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 44 | class System extends Di\Injectable |
||
| 45 | { |
||
| 46 | private MikoPBXConfig $mikoPBXConfig; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * System constructor - Instantiates MikoPBXConfig. |
||
| 50 | */ |
||
| 51 | public function __construct() |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Is the configuration default? |
||
| 58 | * @return bool |
||
| 59 | */ |
||
| 60 | public function isDefaultConf():bool |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Trying to restore the backup |
||
| 76 | * @return void |
||
| 77 | */ |
||
| 78 | public function tryRestoreConf():void |
||
| 79 | { |
||
| 80 | $di = Di::getDefault(); |
||
| 81 | if ($di === null) { |
||
| 82 | return; |
||
| 83 | } |
||
| 84 | $storage = new Storage(); |
||
| 85 | $storages = $storage->getStorageCandidate(); |
||
| 86 | $tmpMountDir = '/tmp/mnt'; |
||
| 87 | $backupDir = str_replace(['/storage/usbdisk1','/mountpoint'],['',''],$di->getConfig()->path('core.confBackupDir')); |
||
| 88 | $confFile = $di->getConfig()->path('database.dbfile'); |
||
| 89 | foreach ($storages as $dev => $fs){ |
||
| 90 | Util::teletypeEcho(" - mount $dev ..."."\n"); |
||
| 91 | Util::mwMkdir($tmpMountDir."/$dev"); |
||
| 92 | $res = Storage::mountDisk($dev, $fs, $tmpMountDir."/$dev"); |
||
| 93 | if(!$res){ |
||
| 94 | Util::teletypeEcho(" - fail mount $dev ..."."\n"); |
||
| 95 | } |
||
| 96 | } |
||
| 97 | $pathBusybox = Util::which('busybox'); |
||
| 98 | $pathFind = Util::which('find'); |
||
| 99 | $pathMount = Util::which('umount'); |
||
| 100 | $pathRm = Util::which('rm'); |
||
| 101 | $pathGzip = Util::which('gzip'); |
||
| 102 | $pathSqlite3 = Util::which('sqlite3'); |
||
| 103 | $lastBackUp = trim(shell_exec("$pathFind $tmpMountDir/dev/*$backupDir -type f -printf '%T@ %p\\n' | $pathBusybox sort -n | $pathBusybox tail -1 | $pathBusybox cut -f2- -d' '")); |
||
| 104 | if(empty($lastBackUp)){ |
||
| 105 | return; |
||
| 106 | } |
||
| 107 | Util::teletypeEcho(" - Restore $lastBackUp ..."."\n"); |
||
| 108 | shell_exec("$pathRm -rf {$confFile}*"); |
||
| 109 | shell_exec("$pathGzip -c -d $lastBackUp | sqlite3 $confFile"); |
||
| 110 | Processes::mwExec("$pathSqlite3 $confFile 'select * from m_Storage'", $out, $ret); |
||
| 111 | if($ret !== 0){ |
||
| 112 | Util::teletypeEcho(" - fail restore $lastBackUp ..."."\n"); |
||
| 113 | copy('/conf.default/mikopbx.db', $confFile); |
||
| 114 | }elseif(!$this->isDefaultConf()){ |
||
| 115 | self::rebootSync(); |
||
| 116 | } |
||
| 117 | foreach ($storages as $dev => $fs){ |
||
| 118 | shell_exec("$pathMount $dev"); |
||
| 119 | } |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Returns the directory where logs are stored. |
||
| 124 | * |
||
| 125 | * @return string - Directory path where logs are stored. |
||
| 126 | */ |
||
| 127 | public static function getLogDir(): string |
||
| 128 | { |
||
| 129 | $di = Di::getDefault(); |
||
| 130 | if ($di !== null) { |
||
| 131 | return $di->getConfig()->path('core.logsDir'); |
||
| 132 | } |
||
| 133 | |||
| 134 | // Default logs directory |
||
| 135 | return '/var/log'; |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Refreshes networks configs and restarts network daemon. |
||
| 140 | * |
||
| 141 | * @return void |
||
| 142 | */ |
||
| 143 | public static function networkReload(): void |
||
| 144 | { |
||
| 145 | // Create Network object and configure settings |
||
| 146 | $network = new Network(); |
||
| 147 | $network->hostnameConfigure(); |
||
| 148 | $network->resolvConfGenerate(); |
||
| 149 | $network->loConfigure(); |
||
| 150 | $network->lanConfigure(); |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Updates custom changes in config files |
||
| 155 | * |
||
| 156 | * @return void |
||
| 157 | */ |
||
| 158 | public static function updateCustomFiles():void |
||
| 159 | { |
||
| 160 | $actions = []; |
||
| 161 | |||
| 162 | // Find all custom files marked as changed |
||
| 163 | /** @var CustomFiles $res_data */ |
||
| 164 | $res_data = CustomFiles::find("changed = '1'"); |
||
| 165 | |||
| 166 | // Process each changed file |
||
| 167 | foreach ($res_data as $file_data) { |
||
| 168 | // Always restart asterisk after any custom file change |
||
| 169 | $actions['asterisk_core_reload'] = 100; |
||
| 170 | $filename = basename($file_data->filepath); |
||
| 171 | |||
| 172 | // Process based on file name |
||
| 173 | switch ($filename) { |
||
| 174 | // Set actions based on the name of the changed file |
||
| 175 | case 'manager.conf': |
||
| 176 | $actions['manager'] = 10; |
||
| 177 | break; |
||
| 178 | case 'musiconhold.conf': |
||
| 179 | $actions['musiconhold'] = 100; |
||
| 180 | break; |
||
| 181 | case 'modules.conf': |
||
| 182 | $actions['modules'] = 10; |
||
| 183 | break; |
||
| 184 | case 'http.conf': |
||
| 185 | $actions['manager'] = 10; // |
||
| 186 | break; |
||
| 187 | case 'hep.conf': |
||
| 188 | $actions['hep'] = 10; // |
||
| 189 | break; |
||
| 190 | case 'root': // crontabs |
||
| 191 | $actions['cron'] = 10; |
||
| 192 | break; |
||
| 193 | case 'queues.conf': |
||
| 194 | $actions['queues'] = 10; |
||
| 195 | break; |
||
| 196 | case 'features.conf': |
||
| 197 | $actions['features'] = 10; |
||
| 198 | break; |
||
| 199 | case 'ntp.conf': |
||
| 200 | $actions['ntp'] = 100; |
||
| 201 | break; |
||
| 202 | case 'ooh323.conf': |
||
| 203 | $actions['h323'] = 100; |
||
| 204 | break; |
||
| 205 | case 'rtp.conf': |
||
| 206 | $actions['rtp'] = 10; |
||
| 207 | break; |
||
| 208 | case 'static-routes': |
||
| 209 | case 'openvpn.ovpn': |
||
| 210 | $actions['network'] = 100; |
||
| 211 | break; |
||
| 212 | case 'firewall_additional': |
||
| 213 | case 'jail.local': |
||
| 214 | $actions['firewall'] = 100; |
||
| 215 | break; |
||
| 216 | default: |
||
| 217 | break; |
||
| 218 | } |
||
| 219 | } |
||
| 220 | |||
| 221 | // Sort actions and invoke them |
||
| 222 | asort($actions); |
||
| 223 | self::invokeActions($actions); |
||
| 224 | |||
| 225 | // After actions are invoked, reset the changed status and save the file data |
||
| 226 | foreach ($res_data as $file_data) { |
||
| 227 | /** @var CustomFiles $file_data */ |
||
| 228 | $file_data->writeAttribute("changed", '0'); |
||
| 229 | $file_data->save(); |
||
| 230 | } |
||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Restart modules or services based on the provided actions. |
||
| 235 | * |
||
| 236 | * @param array $actions - The actions to be performed. |
||
| 237 | * |
||
| 238 | * @return void |
||
| 239 | */ |
||
| 240 | public static function invokeActions(array $actions): void |
||
| 241 | { |
||
| 242 | |||
| 243 | // Process each action |
||
| 244 | foreach ($actions as $action => $value) { |
||
| 245 | // Restart modules or services based on action |
||
| 246 | switch ($action) { |
||
| 247 | case 'manager': |
||
| 248 | PBX::managerReload(); |
||
| 249 | break; |
||
| 250 | case 'musiconhold': |
||
| 251 | PBX::musicOnHoldReload(); |
||
| 252 | break; |
||
| 253 | case 'rtp': |
||
| 254 | PBX::rtpReload(); |
||
| 255 | break; |
||
| 256 | case 'modules': |
||
| 257 | PBX::modulesReload(); |
||
| 258 | break; |
||
| 259 | case 'cron': |
||
| 260 | $cron = new CronConf(); |
||
| 261 | $cron->reStart(); |
||
| 262 | break; |
||
| 263 | case 'queues': |
||
| 264 | QueueConf::queueReload(); |
||
| 265 | break; |
||
| 266 | case 'features': |
||
| 267 | PBX::managerReload(); // |
||
| 268 | break; |
||
| 269 | case 'ntp': |
||
| 270 | NTPConf::configure(); |
||
| 271 | break; |
||
| 272 | case 'firewall': |
||
| 273 | IptablesConf::reloadFirewall(); |
||
| 274 | break; |
||
| 275 | case 'hep': |
||
| 276 | HepConf::reload(); |
||
| 277 | break; |
||
| 278 | case 'h323': |
||
| 279 | H323Conf::reload(); |
||
| 280 | break; |
||
| 281 | case 'network': |
||
| 282 | self::networkReload(); |
||
| 283 | break; |
||
| 284 | case 'asterisk_core_reload': |
||
| 285 | PBX::sipReload(); |
||
| 286 | PBX::iaxReload(); |
||
| 287 | PBX::dialplanReload(); |
||
| 288 | PBX::coreReload(); |
||
| 289 | break; |
||
| 290 | default: |
||
| 291 | } |
||
| 292 | } |
||
| 293 | } |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Sets the system date and time based on timestamp and timezone. |
||
| 297 | * |
||
| 298 | * @param int $timeStamp - Unix timestamp. |
||
| 299 | * @param string $remote_tz - Timezone string. |
||
| 300 | * |
||
| 301 | * @return bool |
||
| 302 | * @throws \Exception |
||
| 303 | */ |
||
| 304 | public static function setDate(int $timeStamp, string $remote_tz): bool |
||
| 305 | { |
||
| 306 | $datePath = Util::which('date'); |
||
| 307 | |||
| 308 | // Fetch timezone from database |
||
| 309 | $db_tz = PbxSettings::getValueByKey('PBXTimezone'); |
||
| 310 | $origin_tz = ''; |
||
| 311 | |||
| 312 | // Read existing timezone from file if it exists |
||
| 313 | if (file_exists('/etc/TZ')) { |
||
| 314 | $origin_tz = file_get_contents("/etc/TZ"); |
||
| 315 | } |
||
| 316 | |||
| 317 | // If the timezones are different, configure the timezone |
||
| 318 | if ($origin_tz !== $db_tz){ |
||
| 319 | self::timezoneConfigure(); |
||
| 320 | } |
||
| 321 | |||
| 322 | // Calculate the time offset and set the system time |
||
| 323 | $origin_tz = $db_tz; |
||
| 324 | $origin_dtz = new DateTimeZone($origin_tz); |
||
| 325 | $remote_dtz = new DateTimeZone($remote_tz); |
||
| 326 | $origin_dt = new DateTime('now', $origin_dtz); |
||
| 327 | $remote_dt = new DateTime('now', $remote_dtz); |
||
| 328 | $offset = $origin_dtz->getOffset($origin_dt) - $remote_dtz->getOffset($remote_dt); |
||
| 329 | $timeStamp = $timeStamp - $offset; |
||
| 330 | |||
| 331 | // Execute date command to set system time |
||
| 332 | Processes::mwExec("{$datePath} +%s -s @{$timeStamp}"); |
||
| 333 | |||
| 334 | return true; |
||
| 335 | } |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Reboots the system after calling system_reboot_cleanup() |
||
| 339 | * |
||
| 340 | * @return void |
||
| 341 | */ |
||
| 342 | public static function rebootSync(): void |
||
| 343 | { |
||
| 344 | $pbx_rebootPath = Util::which('pbx_reboot'); |
||
| 345 | Processes::mwExec("{$pbx_rebootPath} > /dev/null 2>&1"); |
||
| 346 | } |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Reboots the system after calling system_reboot_cleanup() |
||
| 350 | */ |
||
| 351 | public static function rebootSyncBg(): void |
||
| 352 | { |
||
| 353 | $pbx_rebootPath = Util::which('pbx_reboot'); |
||
| 354 | Processes::mwExecBg("{$pbx_rebootPath} > /dev/null 2>&1"); |
||
| 355 | } |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Shutdown the system |
||
| 359 | */ |
||
| 360 | public static function shutdown(): void |
||
| 361 | { |
||
| 362 | $shutdownPath = Util::which('shutdown'); |
||
| 363 | Processes::mwExec("{$shutdownPath} > /dev/null 2>&1"); |
||
| 364 | } |
||
| 365 | |||
| 366 | |||
| 367 | /** |
||
| 368 | * Configures the system timezone according to the PBXTimezone setting. |
||
| 369 | * |
||
| 370 | * @return void |
||
| 371 | */ |
||
| 372 | public static function timezoneConfigure(): void |
||
| 373 | { |
||
| 374 | |||
| 375 | // Get the timezone setting from the database |
||
| 376 | $timezone = PbxSettings::getValueByKey('PBXTimezone'); |
||
| 377 | |||
| 378 | // If /etc/TZ or /etc/localtime exist, delete them |
||
| 379 | if (file_exists('/etc/TZ')) { |
||
| 380 | unlink("/etc/TZ"); |
||
| 381 | } |
||
| 382 | if (file_exists('/etc/localtime')) { |
||
| 383 | unlink("/etc/localtime"); |
||
| 384 | } |
||
| 385 | |||
| 386 | // If a timezone is set, configure it |
||
| 387 | if ($timezone) { |
||
| 388 | |||
| 389 | // The path to the zone file |
||
| 390 | $zone_file = "/usr/share/zoneinfo/{$timezone}"; |
||
| 391 | |||
| 392 | // If the zone file exists, copy it to /etc/localtime |
||
| 393 | if ( ! file_exists($zone_file)) { |
||
| 394 | return; |
||
| 395 | } |
||
| 396 | $cpPath = Util::which('cp'); |
||
| 397 | Processes::mwExec("{$cpPath} {$zone_file} /etc/localtime"); |
||
| 398 | |||
| 399 | // Write the timezone to /etc/TZ and set the TZ environment variable |
||
| 400 | file_put_contents('/etc/TZ', $timezone); |
||
| 401 | putenv("TZ={$timezone}"); |
||
| 402 | |||
| 403 | // Execute the export TZ command and configure PHP's timezone |
||
| 404 | Processes::mwExec("export TZ;"); |
||
| 405 | PHPConf::phpTimeZoneConfigure(); |
||
| 406 | } |
||
| 407 | |||
| 408 | } |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Loads additional kernel modules. |
||
| 412 | * |
||
| 413 | * @return bool - Returns true if modules are loaded successfully. |
||
| 414 | */ |
||
| 415 | public function loadKernelModules(): bool |
||
| 434 | } |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Calculate the hash of SSL certificates and extract them from ca-certificates.crt. |
||
| 438 | * |
||
| 439 | * @return void |
||
| 440 | */ |
||
| 441 | public static function sslRehash(): void |
||
| 463 | } |
||
| 464 | } |
||
| 465 | } |
||
| 466 |