| Total Complexity | 68 |
| Total Lines | 502 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like DeviceVista7 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 DeviceVista7, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 36 | class DeviceVista7 extends WindowsCommon { |
||
| 37 | |||
| 38 | /** |
||
| 39 | * constructor; tells the world about supported EAP types and device anomalies |
||
| 40 | */ |
||
| 41 | final public function __construct() { |
||
| 42 | parent::__construct(); |
||
| 43 | \core\common\Entity::intoThePotatoes(); |
||
| 44 | $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_SILVERBULLET]); |
||
| 45 | $this->loggerInstance->debug(4, "This device supports the following EAP methods: "); |
||
| 46 | $this->loggerInstance->debug(4, $this->supportedEapMethods); |
||
| 47 | $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."); |
||
| 48 | \core\common\Entity::outOfThePotatoes(); |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * create the actual installer executable |
||
| 53 | * |
||
| 54 | * @return string filename of the generated installer |
||
| 55 | * |
||
| 56 | */ |
||
| 57 | public function writeInstaller() { |
||
| 58 | $dom = textdomain(NULL); |
||
| 59 | textdomain("devices"); |
||
| 60 | // create certificate files and save their names in $caFiles arrary |
||
| 61 | $caFiles = $this->saveCertificateFiles('der'); |
||
| 62 | |||
| 63 | $allSSID = $this->attributes['internal:SSID']; |
||
| 64 | $delSSIDs = $this->attributes['internal:remove_SSID']; |
||
| 65 | $this->prepareInstallerLang(); |
||
| 66 | $setWired = isset($this->attributes['media:wired'][0]) && $this->attributes['media:wired'][0] == 'on' ? 1 : 0; |
||
| 67 | // create a list of profiles to be deleted after installation |
||
| 68 | $delProfiles = []; |
||
| 69 | foreach ($delSSIDs as $ssid => $cipher) { |
||
| 70 | if ($cipher == 'DEL') { |
||
| 71 | $delProfiles[] = $ssid; |
||
| 72 | } |
||
| 73 | if ($cipher == 'TKIP') { |
||
| 74 | $delProfiles[] = $ssid . ' (TKIP)'; |
||
| 75 | } |
||
| 76 | } |
||
| 77 | |||
| 78 | if ($this->selectedEap == \core\common\EAP::EAPTYPE_TLS || $this->selectedEap == \core\common\EAP::EAPTYPE_PEAP_MSCHAP2 || $this->selectedEap == \core\common\EAP::EAPTYPE_PWD || $this->selectedEap == \core\common\EAP::EAPTYPE_TTLS_PAP || $this->selectedEap == \core\common\EAP::EAPTYPE_TTLS_MSCHAP2 || $this->selectedEap == \core\common\EAP::EAPTYPE_SILVERBULLET) { |
||
| 79 | $windowsProfile = []; |
||
| 80 | $eapConfig = $this->prepareEapConfig($this->attributes); |
||
| 81 | $iterator = 0; |
||
| 82 | foreach ($allSSID as $ssid => $cipher) { |
||
| 83 | if ($cipher == 'TKIP') { |
||
| 84 | $windowsProfile[$iterator] = $this->writeWLANprofile($ssid . ' (TKIP)', $ssid, 'WPA', 'TKIP', $eapConfig, $iterator); |
||
| 85 | $iterator++; |
||
| 86 | } |
||
| 87 | $windowsProfile[$iterator] = $this->writeWLANprofile($ssid, $ssid, 'WPA2', 'AES', $eapConfig, $iterator); |
||
| 88 | $iterator++; |
||
| 89 | } |
||
| 90 | if ($setWired) { |
||
| 91 | $this->writeLANprofile($eapConfig); |
||
| 92 | } |
||
| 93 | } else { |
||
| 94 | print(" this EAP type is not handled yet.\n"); |
||
| 95 | return; |
||
| 96 | } |
||
| 97 | $this->loggerInstance->debug(4, "windowsProfile"); |
||
| 98 | $this->loggerInstance->debug(4, $windowsProfile); |
||
| 99 | |||
| 100 | $this->writeProfilesNSH($windowsProfile, $caFiles); |
||
| 101 | $this->writeAdditionalDeletes($delProfiles); |
||
| 102 | if ($this->selectedEap == \core\common\EAP::EAPTYPE_SILVERBULLET) { |
||
| 103 | $this->writeClientP12File(); |
||
| 104 | } |
||
| 105 | $this->copyFiles($this->selectedEap); |
||
| 106 | $fedLogo = $this->attributes['fed:logo_file'] ?? NULL; |
||
| 107 | $idpLogo = $this->attributes['internal:logo_file'] ?? NULL; |
||
| 108 | $this->combineLogo($idpLogo, $fedLogo); |
||
| 109 | $this->writeMainNSH($this->selectedEap, $this->attributes); |
||
| 110 | $this->compileNSIS(); |
||
| 111 | $installerPath = $this->signInstaller(); |
||
| 112 | |||
| 113 | textdomain($dom); |
||
| 114 | return($installerPath); |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * creates the XML snippet that describes the EAP configuration |
||
| 119 | * |
||
| 120 | * @param array $attr the attributes for the profile |
||
| 121 | * @return array two XML snippets describing the EAP configuration, for Vista and 7 respectively |
||
| 122 | */ |
||
| 123 | private function prepareEapConfig($attr) { |
||
| 328 | } |
||
| 329 | |||
| 330 | /** |
||
| 331 | * produce PEAP, TLS and TTLS configuration files for Vista and Windows 7. |
||
| 332 | * Writes XML snippet into file and returns some meta information |
||
| 333 | * |
||
| 334 | * @param string $wlanProfileName name of the WLAN profile |
||
| 335 | * @param string $ssid SSID that is being configured |
||
| 336 | * @param string $auth can be one of "WPA", "WPA2" |
||
| 337 | * @param string $encryption can be one of: "TKIP", "AES" |
||
| 338 | * @param array $eapConfig XML configuration block with EAP config data (two entries, one for Vista, one for 7) |
||
| 339 | * @param int $profileNumber counter, which profile number is this |
||
| 340 | * @return string meta info about generated XML snippet |
||
| 341 | */ |
||
| 342 | private function writeWLANprofile($wlanProfileName, $ssid, $auth, $encryption, $eapConfig, $profileNumber) { |
||
| 395 | } |
||
| 396 | |||
| 397 | /** |
||
| 398 | * writes LAN configuration profile into file |
||
| 399 | * |
||
| 400 | * @param array $eapConfig contains XML snippets for Vista and 7 with the EAP configuration |
||
| 401 | * @return void |
||
| 402 | */ |
||
| 403 | 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 | if (!is_dir('w7')) { |
||
| 421 | mkdir('w7'); |
||
| 422 | } |
||
| 423 | if (!is_dir('vista')) { |
||
| 424 | mkdir('vista'); |
||
| 425 | } |
||
| 426 | |||
| 427 | file_put_contents("vista/lan_prof.xml", $profileFileCont . $eapConfig['vista'] . $closing); |
||
| 428 | file_put_contents("w7/lan_prof.xml", $profileFileCont . $eapConfig['w7'] . $closing); |
||
| 429 | |||
| 430 | } |
||
| 431 | |||
| 432 | /** |
||
| 433 | * writes the main NSH file |
||
| 434 | * |
||
| 435 | * @param array $eap EAP type that is being configured in array representation |
||
| 436 | * @param array $attr list of attributes |
||
| 437 | * @return void |
||
| 438 | */ |
||
| 439 | private function writeMainNSH($eap, $attr) { |
||
| 440 | $this->loggerInstance->debug(4, "writeMainNSH"); |
||
| 441 | $this->loggerInstance->debug(4, $attr); |
||
| 442 | $this->loggerInstance->debug(4, "MYLANG=" . $this->lang . "\n"); |
||
| 443 | |||
| 444 | $eapOptions = [ |
||
| 445 | \core\common\EAP::PEAP => ['str' => 'PEAP', 'exec' => 'user'], |
||
| 446 | \core\common\EAP::TLS => ['str' => 'TLS', 'exec' => 'user'], |
||
| 447 | // TODO for TW: the following line doesn't work - that constant is an array, which can't be a key for another array |
||
| 448 | // generated a PHP Warning but doesn't seem to have any catastrophic effect? |
||
| 449 | // \core\common\EAP::EAPTYPE_SILVERBULLET => ['str' => 'TLS', 'exec' => 'user'], |
||
| 450 | \core\common\EAP::TTLS => ['str' => 'GEANTLink', 'exec' => 'user'], |
||
| 451 | \core\common\EAP::PWD => ['str' => 'PWD', 'exec' => 'user'], |
||
| 452 | ]; |
||
| 453 | $fcontents = ''; |
||
| 454 | if (CONFIG_CONFASSISTANT['NSIS_VERSION'] >= 3) { |
||
| 455 | $fcontents .= "Unicode true\n"; |
||
| 456 | } |
||
| 457 | |||
| 458 | // Uncomment the line below if you want this module to run under XP (only displaying a warning) |
||
| 459 | // $fcontents .= "!define ALLOW_XP\n"; |
||
| 460 | // Uncomment the line below if you want this module to produce debugging messages on the client |
||
| 461 | // $fcontents .= "!define DEBUG_CAT\n"; |
||
| 462 | if ($this->tlsOtherUsername == 1) { |
||
| 463 | $fcontents .= "!define PFX_USERNAME\n"; |
||
| 464 | } |
||
| 465 | $execLevel = $eapOptions[$eap["OUTER"]]['exec']; |
||
| 466 | $eapStr = $eapOptions[$eap["OUTER"]]['str']; |
||
| 467 | if ($eap == \core\common\EAP::EAPTYPE_SILVERBULLET) { |
||
| 468 | $fcontents .= "!define SILVERBULLET\n"; |
||
| 469 | } |
||
| 470 | $this->loggerInstance->debug(4, "EAP_STR=$eapStr\n"); |
||
| 471 | $this->loggerInstance->debug(4, $eap); |
||
| 472 | |||
| 473 | $fcontents .= '!define ' . $eapStr; |
||
| 474 | $fcontents .= "\n" . '!define EXECLEVEL "' . $execLevel . '"'; |
||
| 475 | $fcontents .= $this->writeNsisDefines($attr); |
||
| 476 | file_put_contents('main.nsh', $fcontents); |
||
| 477 | } |
||
| 478 | |||
| 479 | /** |
||
| 480 | * writes references to the individual WLAN profile files into master file |
||
| 481 | * @param array $wlanProfiles list of WLAN profiles |
||
| 482 | * @param array $caArray list of CA certificates |
||
| 483 | * @return void |
||
| 484 | * @throws Exception |
||
| 485 | */ |
||
| 486 | private function writeProfilesNSH($wlanProfiles, $caArray) { |
||
| 487 | $this->loggerInstance->debug(4, "writeProfilesNSH"); |
||
| 488 | $this->loggerInstance->debug(4, $wlanProfiles); |
||
| 489 | $contentWlan = ''; |
||
| 490 | foreach ($wlanProfiles as $wlanProfile) { |
||
| 491 | $contentWlan .= "!insertmacro define_wlan_profile $wlanProfile\n"; |
||
| 492 | } |
||
| 493 | |||
| 494 | file_put_contents('profiles.nsh', $contentWlan); |
||
| 495 | |||
| 496 | $contentCerts = ''; |
||
| 497 | $fileHandleCerts = fopen('certs.nsh', 'w'); |
||
| 498 | if ($fileHandleCerts === FALSE) { |
||
| 499 | throw new Exception("Unable to open new file certs.nsh to write CAs!"); |
||
| 500 | } |
||
| 501 | foreach ($caArray as $certAuthority) { |
||
| 502 | $store = $certAuthority['root'] ? "root" : "ca"; |
||
| 503 | $contentCerts .= '!insertmacro install_ca_cert "' . $certAuthority['file'] . '" "' . $certAuthority['sha1'] . '" "' . $store . "\"\n"; |
||
| 504 | } |
||
| 505 | fwrite($fileHandleCerts, $contentCerts); |
||
| 506 | fclose($fileHandleCerts); |
||
| 507 | } |
||
| 508 | |||
| 509 | /** |
||
| 510 | * copies various files into temp dir for inclusion into installer |
||
| 511 | * |
||
| 512 | * @param array $eap EAP type being configured, in array notation |
||
| 513 | * @return boolean TRUE if things worked (and throws an Exception if not) |
||
| 514 | * @throws Exception |
||
| 515 | */ |
||
| 516 | private function copyFiles($eap) { |
||
| 535 | } |
||
| 536 | |||
| 537 | private $tlsOtherUsername = 0; |
||
| 538 | |||
| 539 | } |
||
| 540 |