| Total Complexity | 51 |
| Total Lines | 306 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 0 |
Complex classes like DeviceW8W10 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 DeviceW8W10, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 46 | class DeviceW8W10 extends \devices\ms\WindowsCommon |
||
| 47 | { |
||
| 48 | public function __construct() |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * create the actual installer executable |
||
| 68 | * |
||
| 69 | * @return string filename of the generated installer |
||
| 70 | * |
||
| 71 | */ |
||
| 72 | public function writeInstaller() |
||
| 73 | { |
||
| 74 | \core\common\Entity::intoThePotatoes(); |
||
| 75 | $this->prepareInstallerLang(); |
||
| 76 | $this->setupEapConfig(); |
||
| 77 | $setWired = isset($this->attributes['media:wired'][0]) && $this->attributes['media:wired'][0] == 'on' ? 1 : 0; |
||
| 78 | $this->iterator = 0; |
||
| 79 | $fcontentsProfile = ''; |
||
| 80 | $this->createProfileDir(); |
||
| 81 | foreach ($this->attributes['internal:networks'] as $networkName => $oneNetwork) { |
||
| 82 | if ($this::separateHS20profiles === true) { |
||
| 83 | $fcontentsProfile .= $this->saveNetworkProfileSeparateHS($networkName, $oneNetwork); |
||
| 84 | } else { |
||
| 85 | $fcontentsProfile .= $this->saveNetworkProfileJoinedHS($networkName, $oneNetwork); |
||
| 86 | } |
||
| 87 | } |
||
| 88 | file_put_contents('profiles.nsh', $fcontentsProfile); |
||
| 89 | $delSSIDs = $this->attributes['internal:remove_SSID']; |
||
| 90 | $delProfiles = []; |
||
| 91 | foreach ($delSSIDs as $ssid => $cipher) { |
||
| 92 | if ($cipher == 'DEL') { |
||
| 93 | $delProfiles[] = $ssid; |
||
| 94 | } |
||
| 95 | if ($cipher == 'TKIP') { |
||
| 96 | $delProfiles[] = $ssid.' (TKIP)'; |
||
| 97 | } |
||
| 98 | } |
||
| 99 | $this->writeAdditionalDeletes($delProfiles); |
||
| 100 | if ($setWired) { |
||
| 101 | $this->loggerInstance->debug(4, "Saving LAN profile\n"); |
||
| 102 | $windowsProfile = $this->generateLANprofile(); |
||
| 103 | $this->saveProfile($windowsProfile); |
||
| 104 | } |
||
| 105 | $this->saveCerts(); |
||
| 106 | if ($this->selectedEap == \core\common\EAP::EAPTYPE_SILVERBULLET) { |
||
| 107 | $this->writeClientP12File(); |
||
| 108 | } |
||
| 109 | $this->copyFiles($this->selectedEap); |
||
| 110 | $this->saveLogo(); |
||
| 111 | $this->writeMainNSH($this->selectedEap, $this->attributes); |
||
| 112 | $this->compileNSIS(); |
||
| 113 | $installerPath = $this->signInstaller(); |
||
| 114 | \core\common\Entity::outOfThePotatoes(); |
||
| 115 | return $installerPath; |
||
| 116 | } |
||
| 117 | |||
| 118 | private function createProfileDir() |
||
| 119 | { |
||
| 120 | if (!is_dir('profiles')) { |
||
| 121 | mkdir('profiles'); |
||
| 122 | } |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * If separateHS20profiles is true then we should be saving OID and SSID |
||
| 127 | * profiles separately. OID profiles should be considered optionl, i.e. |
||
| 128 | * the installer should not report installation failure (??). If we decide |
||
| 129 | * that such installation failures should be silent then it is enough if |
||
| 130 | * these profiles are marked as hs20 and no "nohs" profiles are created |
||
| 131 | */ |
||
| 132 | |||
| 133 | private function saveNetworkProfileSeparateHS($profileName, $network) |
||
| 134 | { |
||
| 135 | $out = ''; |
||
| 136 | if (!empty($network['ssid'])) { |
||
| 137 | $windowsProfileSSID = $this->generateWlanProfile($profileName, $network['ssid'], 'WPA2', 'AES', [], false); |
||
| 138 | $this->saveProfile($windowsProfileSSID, $this->iterator, true); |
||
| 139 | $out = "!insertmacro define_wlan_profile \"$profileName\" \"AES\" 0\n"; |
||
| 140 | $this->iterator ++; |
||
| 141 | $profileName .= " via partner"; |
||
| 142 | } |
||
| 143 | if (!empty($network['oi'])) { |
||
| 144 | $windowsProfileHS = $this->generateWlanProfile($profileName, ['cat-passpoint-profile'], 'WPA2', 'AES', $network['oi'], true); |
||
| 145 | $this->saveProfile($windowsProfileHS, $this->iterator, true); |
||
| 146 | $out .= "!insertmacro define_wlan_profile \"$profileName\" \"AES\" 1\n"; |
||
| 147 | $this->iterator ++; |
||
| 148 | } |
||
| 149 | return($out); |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * If separateHS20profiles is false then we should be saving a hs20 profile |
||
| 154 | * containing both OIDs and SSIDs. In addiotion we should also be saving |
||
| 155 | * a nohs_... profile. When the installer runs it first tries the normal |
||
| 156 | * profile and if this fails it will try the nohs (if one exists) |
||
| 157 | */ |
||
| 158 | |||
| 159 | private function saveNetworkProfileJoinedHS($profileName, $network) |
||
| 160 | { |
||
| 161 | $oiOnly = false; |
||
| 162 | if ($network['ssid'] == []) { |
||
| 163 | $oiOnly = true; |
||
| 164 | $network['ssid'] = ['cat-passpoint-profile']; |
||
| 165 | } |
||
| 166 | $windowsProfile = $this->generateWlanProfile($profileName, $network['ssid'], 'WPA2', 'AES', $network['oi'], true); |
||
| 167 | $this->saveProfile($windowsProfile, $this->iterator, true); |
||
| 168 | if (!$oiOnly) { |
||
| 169 | $windowsProfile = $this->generateWlanProfile($profileName, $network['ssid'], 'WPA2', 'AES', [], false); |
||
| 170 | $this->saveProfile($windowsProfile, $this->iterator, false); |
||
| 171 | } |
||
| 172 | $this->iterator ++; |
||
| 173 | return("!insertmacro define_wlan_profile \"$profileName\" \"AES\" 1\n"); |
||
| 174 | } |
||
| 175 | |||
| 176 | private function saveLogo() |
||
| 177 | { |
||
| 178 | $fedLogo = $this->attributes['fed:logo_file'] ?? NULL; |
||
| 179 | $idpLogo = $this->attributes['internal:logo_file'] ?? NULL; |
||
| 180 | $this->combineLogo($idpLogo, $fedLogo); |
||
| 181 | } |
||
| 182 | |||
| 183 | private function writeMainNSH($eap, $attr) |
||
| 184 | { |
||
| 185 | $this->loggerInstance->debug(4, "writeMainNSH"); |
||
| 186 | $this->loggerInstance->debug(4, $attr); |
||
| 187 | $this->loggerInstance->debug(4, "Device_id = ".$this->device_id."\n"); |
||
| 188 | $fcontents = "!define W8\n"; |
||
| 189 | if ($this->device_id == 'w10') { |
||
| 190 | $fcontents .= "!define W10\n"; |
||
| 191 | } |
||
| 192 | $fcontents .= "Unicode true\n"; |
||
| 193 | if ($this->useGeantLink) { |
||
| 194 | $eapStr = 'GEANTLink'; |
||
| 195 | } else { |
||
| 196 | $eapStr = \core\common\EAP::eapDisplayName($this->selectedEap)['OUTER']; |
||
| 197 | } |
||
| 198 | if (isset($this->tlsOtherUsername) && $this->tlsOtherUsername == 1) { |
||
| 199 | $fcontents .= "!define PFX_USERNAME\n"; |
||
| 200 | } |
||
| 201 | if ($eap == \core\common\EAP::EAPTYPE_SILVERBULLET) { |
||
| 202 | $fcontents .= "!define SILVERBULLET\n"; |
||
| 203 | } |
||
| 204 | $fcontents .= '!define '.$eapStr; |
||
| 205 | $fcontents .= "\n".'!define EXECLEVEL "user"'; |
||
| 206 | $fcontents .= $this->writeNsisDefines($attr); |
||
| 207 | file_put_contents('main.nsh', $fcontents); |
||
| 208 | } |
||
| 209 | |||
| 210 | |||
| 211 | private function copyFiles($eap) |
||
| 212 | { |
||
| 213 | $this->loggerInstance->debug(4, "copyFiles start\n"); |
||
| 214 | $this->copyBasicFiles(); |
||
| 215 | switch ($eap["OUTER"]) { |
||
| 216 | case \core\common\EAP::TTLS: |
||
| 217 | if ($this->useGeantLink) { |
||
| 218 | $this->copyGeantLinkFiles(); |
||
| 219 | } else { |
||
| 220 | $this->copyStandardNsi(); |
||
| 221 | } |
||
| 222 | break; |
||
| 223 | default: |
||
| 224 | $this->copyStandardNsi(); |
||
| 225 | } |
||
| 226 | $this->loggerInstance->debug(4, "copyFiles end\n"); |
||
| 227 | return true; |
||
| 228 | } |
||
| 229 | |||
| 230 | private function copyStandardNsi() |
||
| 231 | { |
||
| 232 | if (!$this->translateFile('eap_w8.inc', 'cat.NSI')) { |
||
| 233 | throw new Exception("Translating needed file eap_w8.inc failed!"); |
||
| 234 | } |
||
| 235 | } |
||
| 236 | |||
| 237 | private function saveCerts() |
||
| 238 | { |
||
| 239 | $caArray = $this->saveCertificateFiles('der'); |
||
| 240 | $fcontentsCerts = ''; |
||
| 241 | $fileHandleCerts = fopen('certs.nsh', 'w'); |
||
| 242 | if ($fileHandleCerts === false) { |
||
| 243 | throw new Exception("Unable to open new certs.nsh file for writing CAs."); |
||
| 244 | } |
||
| 245 | foreach ($caArray as $certAuthority) { |
||
| 246 | $store = $certAuthority['root'] ? "root" : "ca"; |
||
| 247 | $fcontentsCerts .= '!insertmacro install_ca_cert "'.$certAuthority['file'].'" "'.$certAuthority['sha1'].'" "'.$store."\"\n"; |
||
| 248 | } |
||
| 249 | fwrite($fileHandleCerts, $fcontentsCerts); |
||
| 250 | fclose($fileHandleCerts); |
||
| 251 | } |
||
| 252 | |||
| 253 | /* saveProvile writes a LAN or WLAN profile |
||
| 254 | * @param string $profile the XML content to be saved |
||
| 255 | * @param int $profileNumber the profile index or NULL to indicate a LAN profile |
||
| 256 | * @param boolean $hs20 for WLAN profiles indicates if use the nohs prefix |
||
| 257 | */ |
||
| 258 | private function saveProfile($profile, $profileNumber=NULL, $hs20=false) |
||
| 259 | { |
||
| 260 | if ($hs20) { |
||
| 261 | $prefix = 'w'; |
||
| 262 | } else { |
||
| 263 | $prefix = 'nohs_w'; |
||
| 264 | } |
||
| 265 | if (is_null($profileNumber)) { |
||
| 266 | $prefix = ''; |
||
| 267 | $suffix = ''; |
||
| 268 | } else { |
||
| 269 | $suffix = "-$profileNumber"; |
||
| 270 | } |
||
| 271 | $xmlFname = "profiles/".$prefix."lan_prof".$suffix.".xml"; |
||
| 272 | $this->loggerInstance->debug(4, "Saving profile to ".$xmlFname."\n"); |
||
| 273 | file_put_contents($xmlFname, $profile); |
||
| 274 | } |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Selects the approprate handler for a given EAP type and retirns |
||
| 278 | * an initiated object |
||
| 279 | * |
||
| 280 | * @return a profile object |
||
|
|
|||
| 281 | */ |
||
| 282 | |||
| 283 | private function setEapObject() |
||
| 284 | { |
||
| 285 | switch ($this->selectedEap['OUTER']) { |
||
| 286 | case \core\common\EAP::TTLS: |
||
| 287 | if ($this->useGeantLink) { |
||
| 288 | return(new GeantLinkTtlsProfile()); |
||
| 289 | } else { |
||
| 290 | return(new MsTtlsProfile()); |
||
| 291 | } |
||
| 292 | case \core\common\EAP::PEAP: |
||
| 293 | return(new MsPeapProfile()); |
||
| 294 | case \core\common\EAP::TLS: |
||
| 295 | return(new MsTlsProfile()); |
||
| 296 | default: |
||
| 297 | // use Exception here |
||
| 298 | break; |
||
| 299 | } |
||
| 300 | } |
||
| 301 | |||
| 302 | private function setupEapConfig() { |
||
| 303 | $servers = empty($this->attributes['eap:server_name']) ? '' : implode(';', $this->attributes['eap:server_name']); |
||
| 304 | $outerId = $this->determineOuterIdString(); |
||
| 305 | $nea = (\core\common\Entity::getAttributeValue($this->attributes, 'media:wired', 0) === 'on') ? 'true' : 'false'; |
||
| 306 | $otherTlsName = \core\common\Entity::getAttributeValue($this->attributes, 'eap-specific:tls_use_other_id', 0) === 'on' ? 'true' : 'false'; |
||
| 307 | $this->useGeantLink = \core\common\Entity::getAttributeValue($this->attributes, 'device-specific:geantlink', $this->device_id)[0] === 'on' ? true : false; |
||
| 308 | $eapConfig = $this->setEapObject(); |
||
| 309 | $eapConfig->setInnerType($this->selectedEap['INNER']); |
||
| 310 | $eapConfig->setInnerTypeDisplay(\core\common\EAP::eapDisplayName($this->selectedEap)['INNER']); |
||
| 311 | $eapConfig->setCAList($this->getAttribute('internal:CAs')[0]); |
||
| 312 | $eapConfig->setServerNames($servers); |
||
| 313 | $eapConfig->setOuterId($outerId); |
||
| 314 | $eapConfig->setNea($nea); |
||
| 315 | $eapConfig->setDisplayName($this->translateString($this->attributes['general:instname'][0])); |
||
| 316 | $eapConfig->setIdPId($this->deviceUUID); |
||
| 317 | $eapConfig->setOtherTlsName($otherTlsName); |
||
| 318 | $eapConfig->setConfig(); |
||
| 319 | $this->eapConfigObject = $eapConfig; |
||
| 320 | } |
||
| 321 | |||
| 322 | private function generateWlanProfile($networkName, $ssids, $authentication, $encryption, $ois, $hs20 = false) |
||
| 338 | } |
||
| 339 | |||
| 340 | private function generateLanProfile() |
||
| 341 | { |
||
| 342 | $lanProfile = new MsLanProfile(); |
||
| 343 | $lanProfile->setEapConfig($this->eapConfigObject); |
||
| 344 | return($lanProfile->writeLANprofile()); |
||
| 345 | } |
||
| 346 | |||
| 347 | private $eapTypeId; |
||
| 348 | private $eapAuthorId; |
||
| 349 | private $eapConfigObject; |
||
| 350 | private $profileNames; |
||
| 351 | private $iterator; |
||
| 352 | } |
||
| 356 |