| Total Complexity | 71 |
| Total Lines | 487 |
| Duplicated Lines | 35.73 % |
| Changes | 0 | ||
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Device_W10 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 Device_W10, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class Device_W10 extends WindowsCommon { |
||
| 29 | |||
| 30 | final public function __construct() { |
||
| 31 | parent::__construct(); |
||
| 32 | $this->setSupportedEapMethods([\core\common\EAP::EAPTYPE_TLS, \core\common\EAP::EAPTYPE_PEAP_MSCHAP2, \core\common\EAP::EAPTYPE_TTLS_PAP, \core\common\EAP::EAPTYPE_TTLS_MSCHAP2, \core\common\EAP::EAPTYPE_PWD, \core\common\EAP::EAPTYPE_SILVERBULLET]); |
||
| 33 | $this->specialities['internal:use_anon_outer'][serialize(\core\common\EAP::EAPTYPE_PEAP_MSCHAP2)] = _("Anonymous identities do not use the realm as specified in the profile - it is derived from the suffix of the user's username input instead."); |
||
| 34 | } |
||
| 35 | |||
| 36 | public function writeInstaller() { |
||
| 37 | $dom = textdomain(NULL); |
||
| 38 | textdomain("devices"); |
||
| 39 | // create certificate files and save their names in $caFiles arrary |
||
| 40 | $caFiles = $this->saveCertificateFiles('der'); |
||
| 41 | $allSSID = $this->attributes['internal:SSID']; |
||
| 42 | $delSSIDs = $this->attributes['internal:remove_SSID']; |
||
| 43 | $this->prepareInstallerLang(); |
||
| 44 | $setWired = isset($this->attributes['media:wired'][0]) && $this->attributes['media:wired'][0] == 'on' ? 1 : 0; |
||
| 45 | // create a list of profiles to be deleted after installation |
||
| 46 | $delProfiles = []; |
||
| 47 | foreach ($delSSIDs as $ssid => $cipher) { |
||
| 48 | if ($cipher == 'DEL') { |
||
| 49 | $delProfiles[] = $ssid; |
||
| 50 | } |
||
| 51 | if ($cipher == 'TKIP') { |
||
| 52 | $delProfiles[] = $ssid . ' (TKIP)'; |
||
| 53 | } |
||
| 54 | } |
||
| 55 | |||
| 56 | |||
| 57 | if (in_array($this->selectedEap, [\core\common\EAP::EAPTYPE_TLS, |
||
| 58 | \core\common\EAP::EAPTYPE_PEAP_MSCHAP2, |
||
| 59 | \core\common\EAP::EAPTYPE_TTLS_PAP, |
||
| 60 | \core\common\EAP::EAPTYPE_TTLS_MSCHAP2, |
||
| 61 | \core\common\EAP::EAPTYPE_PWD, |
||
| 62 | \core\common\EAP::EAPTYPE_SILVERBULLET])) { |
||
| 63 | $windowsProfile = []; |
||
| 64 | $eapConfig = $this->prepareEapConfig($this->attributes); |
||
| 65 | $iterator = 0; |
||
| 66 | foreach ($allSSID as $ssid => $cipher) { |
||
| 67 | if ($cipher == 'TKIP') { |
||
| 68 | $windowsProfile[$iterator] = $this->writeWLANprofile($ssid . ' (TKIP)', $ssid, 'WPA', 'TKIP', $eapConfig, $iterator); |
||
| 69 | $iterator++; |
||
| 70 | } |
||
| 71 | $windowsProfile[$iterator] = $this->writeWLANprofile($ssid, $ssid, 'WPA2', 'AES', $eapConfig, $iterator); |
||
| 72 | $iterator++; |
||
| 73 | } |
||
| 74 | if ($setWired) { |
||
| 75 | $this->writeLANprofile($eapConfig); |
||
| 76 | } |
||
| 77 | } else { |
||
| 78 | print(" this EAP type is not handled yet.\n"); |
||
| 79 | return; |
||
| 80 | } |
||
| 81 | $this->loggerInstance->debug(4, "windowsProfile"); |
||
| 82 | $this->loggerInstance->debug(4, print_r($windowsProfile, true)); |
||
| 83 | |||
| 84 | $this->writeProfilesNSH($windowsProfile, $caFiles, $setWired); |
||
| 85 | $this->writeAdditionalDeletes($delProfiles); |
||
| 86 | if ($this->selectedEap == \core\common\EAP::EAPTYPE_SILVERBULLET) { |
||
| 87 | $this->writeClientP12File(); |
||
| 88 | } |
||
| 89 | $this->copyFiles($this->selectedEap); |
||
| 90 | $fedLogo = $this->attributes['fed:logo_file'] ?? NULL; |
||
| 91 | $idpLogo = $this->attributes['internal:logo_file'] ?? NULL; |
||
| 92 | $this->combineLogo($idpLogo, $fedLogo); |
||
| 93 | $this->writeMainNSH($this->selectedEap, $this->attributes); |
||
| 94 | $this->compileNSIS(); |
||
| 95 | $installerPath = $this->signInstaller(); |
||
| 96 | |||
| 97 | textdomain($dom); |
||
| 98 | return($installerPath); |
||
| 99 | } |
||
| 100 | |||
| 101 | private function prepareEapConfig($attr) { |
||
| 340 | } |
||
| 341 | |||
| 342 | /** |
||
| 343 | * produce PEAP, TLS and TTLS configuration files for Windows 8 |
||
| 344 | * |
||
| 345 | * @param string $wlanProfileName |
||
| 346 | * @param string $ssid |
||
| 347 | * @param string $auth can be one of "WPA", "WPA2" |
||
| 348 | * @param string $encryption can be one of: "TKIP", "AES" |
||
| 349 | * @param array $eapConfig XML configuration block with EAP config data |
||
| 350 | * @param int $profileNumber counter, which profile number is this |
||
| 351 | * @return string |
||
| 352 | */ |
||
| 353 | View Code Duplication | private function writeWLANprofile($wlanProfileName, $ssid, $auth, $encryption, $eapConfig, $profileNumber) { |
|
| 354 | $profileFileCont = '<?xml version="1.0"?> |
||
| 355 | <WLANProfile xmlns="http://www.microsoft.com/networking/WLAN/profile/v1"> |
||
| 356 | <name>' . $wlanProfileName . '</name> |
||
| 357 | <SSIDConfig> |
||
| 358 | <SSID> |
||
| 359 | <name>' . $ssid . '</name> |
||
| 360 | </SSID> |
||
| 361 | <nonBroadcast>true</nonBroadcast> |
||
| 362 | </SSIDConfig> |
||
| 363 | <connectionType>ESS</connectionType> |
||
| 364 | <connectionMode>auto</connectionMode> |
||
| 365 | <autoSwitch>false</autoSwitch> |
||
| 366 | <MSM> |
||
| 367 | <security> |
||
| 368 | <authEncryption> |
||
| 369 | <authentication>' . $auth . '</authentication> |
||
| 370 | <encryption>' . $encryption . '</encryption> |
||
| 371 | <useOneX>true</useOneX> |
||
| 372 | </authEncryption> |
||
| 373 | '; |
||
| 374 | if ($auth == 'WPA2') { |
||
| 375 | $profileFileCont .= '<PMKCacheMode>enabled</PMKCacheMode> |
||
| 376 | <PMKCacheTTL>720</PMKCacheTTL> |
||
| 377 | <PMKCacheSize>128</PMKCacheSize> |
||
| 378 | <preAuthMode>disabled</preAuthMode> |
||
| 379 | '; |
||
| 380 | } |
||
| 381 | $profileFileCont .= '<OneX xmlns="http://www.microsoft.com/networking/OneX/v1"> |
||
| 382 | <cacheUserData>true</cacheUserData> |
||
| 383 | <authMode>user</authMode> |
||
| 384 | '; |
||
| 385 | |||
| 386 | $closing = ' |
||
| 387 | </OneX> |
||
| 388 | </security> |
||
| 389 | </MSM> |
||
| 390 | </WLANProfile> |
||
| 391 | '; |
||
| 392 | |||
| 393 | if (!is_dir('w8')) { |
||
| 394 | mkdir('w8'); |
||
| 395 | } |
||
| 396 | $xmlFname = "w8/wlan_prof-$profileNumber.xml"; |
||
| 397 | file_put_contents($xmlFname, $profileFileCont . $eapConfig['w10'] . $closing); |
||
| 398 | $this->loggerInstance->debug(2, "Installer has been written into directory $this->FPATH\n"); |
||
| 399 | $this->loggerInstance->debug(4, "WWWWLAN_Profile:$wlanProfileName:$encryption\n"); |
||
| 400 | return("\"$wlanProfileName\" \"$encryption\""); |
||
| 401 | } |
||
| 402 | |||
| 403 | View Code Duplication | private function writeLANprofile($eapConfig) { |
|
| 404 | $profileFileCont = '<?xml version="1.0"?> |
||
| 405 | <LANProfile xmlns="http://www.microsoft.com/networking/LAN/profile/v1"> |
||
| 406 | <MSM> |
||
| 407 | <security> |
||
| 408 | <OneXEnforced>false</OneXEnforced> |
||
| 409 | <OneXEnabled>true</OneXEnabled> |
||
| 410 | <OneX xmlns="http://www.microsoft.com/networking/OneX/v1"> |
||
| 411 | <cacheUserData>true</cacheUserData> |
||
| 412 | <authMode>user</authMode> |
||
| 413 | '; |
||
| 414 | $closing = ' |
||
| 415 | </OneX> |
||
| 416 | </security> |
||
| 417 | </MSM> |
||
| 418 | </LANProfile> |
||
| 419 | '; |
||
| 420 | |||
| 421 | if (!is_dir('w8')) { |
||
| 422 | mkdir('w8'); |
||
| 423 | } |
||
| 424 | $xmlFname = "w8/lan_prof.xml"; |
||
| 425 | file_put_contents($xmlFname, $profileFileCont . $eapConfig['w10'] . $closing); |
||
| 426 | $this->loggerInstance->debug(2, "Installer has been written into directory $this->FPATH\n"); |
||
| 427 | } |
||
| 428 | |||
| 429 | private function writeMainNSH($eap, $attr) { |
||
| 430 | $this->loggerInstance->debug(4, "writeMainNSH"); |
||
| 431 | $this->loggerInstance->debug(4, $attr); |
||
| 432 | $fcontents = "!define W10\n"; |
||
| 433 | $fcontents .= "!define W8\n"; |
||
| 434 | if (CONFIG_CONFASSISTANT['NSIS_VERSION'] >= 3) { |
||
| 435 | $fcontents .= "Unicode true\n"; |
||
| 436 | } |
||
| 437 | |||
| 438 | $eapOptions = [ |
||
| 439 | \core\common\EAP::PEAP => ['str' => 'PEAP', 'exec' => 'user'], |
||
| 440 | \core\common\EAP::TLS => ['str' => 'TLS', 'exec' => 'user'], |
||
| 441 | \core\common\EAP::TTLS => ['str' => 'TTLS', 'exec' => 'user'], |
||
| 442 | \core\common\EAP::PWD => ['str' => 'PWD', 'exec' => 'user'], |
||
| 443 | ]; |
||
| 444 | if (isset($this->options['args']) && $this->options['args'] == 'gl') { |
||
| 445 | $eapOptions[\core\common\EAP::TTLS]['str'] = 'GEANTLink'; |
||
| 446 | } |
||
| 447 | |||
| 448 | // Uncomment the line below if you want this module to run under XP (only displaying a warning) |
||
| 449 | // $fcontents .= "!define ALLOW_XP\n"; |
||
| 450 | // Uncomment the line below if you want this module to produce debugging messages on the client |
||
| 451 | // $fcontents .= "!define DEBUG_CAT\n"; |
||
| 452 | if ($this->tlsOtherUsername == 1) { |
||
| 453 | $fcontents .= "!define PFX_USERNAME\n"; |
||
| 454 | } |
||
| 455 | $execLevel = $eapOptions[$eap["OUTER"]]['exec']; |
||
| 456 | $eapStr = $eapOptions[$eap["OUTER"]]['str']; |
||
| 457 | if ($eap == \core\common\EAP::EAPTYPE_SILVERBULLET) { |
||
| 458 | $fcontents .= "!define SILVERBULLET\n"; |
||
| 459 | } |
||
| 460 | $fcontents .= '!define ' . $eapStr; |
||
| 461 | $fcontents .= "\n" . '!define EXECLEVEL "' . $execLevel . '"'; |
||
| 462 | $fcontents .= $this->writeNsisDefines($eap, $attr); |
||
| 463 | file_put_contents('main.nsh', $fcontents); |
||
| 464 | } |
||
| 465 | |||
| 466 | View Code Duplication | private function writeProfilesNSH($wlanProfiles, $caArray, $wired = 0) { |
|
| 467 | $this->loggerInstance->debug(4, "writeProfilesNSH"); |
||
| 468 | $this->loggerInstance->debug(4, $wlanProfiles); |
||
| 469 | $fcontentsProfile = ''; |
||
| 470 | foreach ($wlanProfiles as $wlanProfile) { |
||
| 471 | $fcontentsProfile .= "!insertmacro define_wlan_profile $wlanProfile\n"; |
||
| 472 | } |
||
| 473 | |||
| 474 | file_put_contents('profiles.nsh', $fcontentsProfile); |
||
| 475 | |||
| 476 | $fcontentsCerts = ''; |
||
| 477 | $fileHandleCerts = fopen('certs.nsh', 'w'); |
||
| 478 | if ($fileHandleCerts === FALSE) { |
||
| 479 | throw new Exception("Unable to open new file certs.nsh to write CAs."); |
||
| 480 | } |
||
| 481 | if ($caArray) { |
||
| 482 | foreach ($caArray as $certAuthority) { |
||
| 483 | $store = $certAuthority['root'] ? "root" : "ca"; |
||
| 484 | $fcontentsCerts .= '!insertmacro install_ca_cert "' . $certAuthority['file'] . '" "' . $certAuthority['sha1'] . '" "' . $store . "\"\n"; |
||
| 485 | } |
||
| 486 | fwrite($fileHandleCerts, $fcontentsCerts); |
||
| 487 | } |
||
| 488 | fclose($fileHandleCerts); |
||
| 489 | } |
||
| 490 | |||
| 491 | //private function write |
||
| 492 | |||
| 493 | private function copyFiles($eap) { |
||
| 512 | } |
||
| 513 | |||
| 514 | private $tlsOtherUsername = 0; |
||
| 515 | |||
| 516 | } |
||
| 517 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.