| Total Complexity | 69 |
| Total Lines | 417 |
| Duplicated Lines | 43.65 % |
| 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_W8 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_W8, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class Device_W8 extends WindowsCommon { |
||
| 29 | |||
| 30 | final public function __construct() { |
||
| 35 | } |
||
| 36 | |||
| 37 | public function writeInstaller() { |
||
| 95 | } |
||
| 96 | |||
| 97 | private function prepareEapConfig($attr) { |
||
| 98 | $outerUser = ''; |
||
| 99 | $eap = $this->selectedEap; |
||
| 100 | $w8Ext = ''; |
||
| 101 | if ($eap != \core\common\EAP::EAPTYPE_TLS && $eap != \core\common\EAP::EAPTYPE_PEAP_MSCHAP2 && $eap != \core\common\EAP::EAPTYPE_PWD && $eap != \core\common\EAP::EAPTYPE_TTLS_PAP && $eap != \core\common\EAP::EAPTYPE_TTLS_MSCHAP2 && $eap != \core\common\EAP::EAPTYPE_SILVERBULLET) { |
||
| 102 | $this->loggerInstance->debug(2, "this method only allows TLS, PEAP, TTLS-PAP, TTLS-MSCHAPv2 or EAP-pwd"); |
||
| 103 | throw new Exception("this method only allows TLS, PEAP, TTLS-PAP, TTLS-MSCHAPv2 or EAP-pwd"); |
||
| 104 | } |
||
| 105 | $useAnon = $attr['internal:use_anon_outer'] [0]; |
||
| 106 | View Code Duplication | if ($useAnon) { |
|
|
|
|||
| 107 | $outerUser = $attr['internal:anon_local_value'][0]; |
||
| 108 | $outerId = $outerUser . '@' . $attr['internal:realm'][0]; |
||
| 109 | } |
||
| 110 | // $servers = preg_quote(implode(';',$attr['eap:server_name'])); |
||
| 111 | $servers = implode(';', $attr['eap:server_name']); |
||
| 112 | |||
| 113 | $caArray = $attr['internal:CAs'][0]; |
||
| 114 | |||
| 115 | |||
| 116 | $profileFileCont = '<EAPConfig><EapHostConfig xmlns="http://www.microsoft.com/provisioning/EapHostConfig"> |
||
| 117 | <EapMethod> |
||
| 118 | '; |
||
| 119 | |||
| 120 | $profileFileCont .= '<Type xmlns="http://www.microsoft.com/provisioning/EapCommon">' . |
||
| 121 | $this->selectedEap["OUTER"] . '</Type> |
||
| 122 | <VendorId xmlns="http://www.microsoft.com/provisioning/EapCommon">0</VendorId> |
||
| 123 | <VendorType xmlns="http://www.microsoft.com/provisioning/EapCommon">0</VendorType> |
||
| 124 | '; |
||
| 125 | if ($eap == \core\common\EAP::EAPTYPE_TLS || $eap == \core\common\EAP::EAPTYPE_SILVERBULLET) { |
||
| 126 | $profileFileCont .= '<AuthorId xmlns="http://www.microsoft.com/provisioning/EapCommon">0</AuthorId> |
||
| 127 | </EapMethod> |
||
| 128 | '; |
||
| 129 | $profileFileCont .= ' |
||
| 130 | |||
| 131 | <Config xmlns:baseEap="http://www.microsoft.com/provisioning/BaseEapConnectionPropertiesV1" |
||
| 132 | xmlns:eapTls="http://www.microsoft.com/provisioning/EapTlsConnectionPropertiesV1"> |
||
| 133 | <baseEap:Eap> |
||
| 134 | <baseEap:Type>13</baseEap:Type> |
||
| 135 | <eapTls:EapType> |
||
| 136 | <eapTls:CredentialsSource> |
||
| 137 | <eapTls:CertificateStore /> |
||
| 138 | </eapTls:CredentialsSource> |
||
| 139 | <eapTls:ServerValidation> |
||
| 140 | <eapTls:DisableUserPromptForServerValidation>true</eapTls:DisableUserPromptForServerValidation> |
||
| 141 | <eapTls:ServerNames>' . $servers . '</eapTls:ServerNames>'; |
||
| 142 | View Code Duplication | if ($caArray) { |
|
| 143 | foreach ($caArray as $certAuthority) { |
||
| 144 | if ($certAuthority['root']) { |
||
| 145 | $profileFileCont .= "<eapTls:TrustedRootCA>" . $certAuthority['sha1'] . "</eapTls:TrustedRootCA>\n"; |
||
| 146 | } |
||
| 147 | } |
||
| 148 | } |
||
| 149 | $profileFileCont .= '</eapTls:ServerValidation> |
||
| 150 | '; |
||
| 151 | View Code Duplication | if (isset($attr['eap-specific:tls_use_other_id']) && $attr['eap-specific:tls_use_other_id'][0] == 'on') { |
|
| 152 | $profileFileCont .= '<eapTls:DifferentUsername>true</eapTls:DifferentUsername>'; |
||
| 153 | $this->tlsOtherUsername = 1; |
||
| 154 | } else { |
||
| 155 | $profileFileCont .= '<eapTls:DifferentUsername>false</eapTls:DifferentUsername>'; |
||
| 156 | } |
||
| 157 | $profileFileCont .= ' |
||
| 158 | </eapTls:EapType> |
||
| 159 | </baseEap:Eap> |
||
| 160 | </Config> |
||
| 161 | '; |
||
| 162 | View Code Duplication | } elseif ($eap == \core\common\EAP::EAPTYPE_PEAP_MSCHAP2) { |
|
| 163 | if (isset($attr['eap:enable_nea']) && $attr['eap:enable_nea'][0] == 'on') { |
||
| 164 | $nea = 'true'; |
||
| 165 | } else { |
||
| 166 | $nea = 'false'; |
||
| 167 | } |
||
| 168 | $profileFileCont .= '<AuthorId xmlns="http://www.microsoft.com/provisioning/EapCommon">0</AuthorId> |
||
| 169 | </EapMethod> |
||
| 170 | '; |
||
| 171 | $w8Ext = '<Config xmlns="http://www.microsoft.com/provisioning/EapHostConfig"> |
||
| 172 | <Eap xmlns="http://www.microsoft.com/provisioning/BaseEapConnectionPropertiesV1"> |
||
| 173 | <Type>25</Type> |
||
| 174 | <EapType xmlns="http://www.microsoft.com/provisioning/MsPeapConnectionPropertiesV1"> |
||
| 175 | <ServerValidation> |
||
| 176 | <DisableUserPromptForServerValidation>true</DisableUserPromptForServerValidation> |
||
| 177 | <ServerNames>' . $servers . '</ServerNames>'; |
||
| 178 | if ($caArray) { |
||
| 179 | foreach ($caArray as $certAuthority) { |
||
| 180 | if ($certAuthority['root']) { |
||
| 181 | $w8Ext .= "<TrustedRootCA>" . $certAuthority['sha1'] . "</TrustedRootCA>\n"; |
||
| 182 | } |
||
| 183 | } |
||
| 184 | } |
||
| 185 | $w8Ext .= '</ServerValidation> |
||
| 186 | <FastReconnect>true</FastReconnect> |
||
| 187 | <InnerEapOptional>false</InnerEapOptional> |
||
| 188 | <Eap xmlns="http://www.microsoft.com/provisioning/BaseEapConnectionPropertiesV1"> |
||
| 189 | <Type>26</Type> |
||
| 190 | <EapType xmlns="http://www.microsoft.com/provisioning/MsChapV2ConnectionPropertiesV1"> |
||
| 191 | <UseWinLogonCredentials>false</UseWinLogonCredentials> |
||
| 192 | </EapType> |
||
| 193 | </Eap> |
||
| 194 | <EnableQuarantineChecks>' . $nea . '</EnableQuarantineChecks> |
||
| 195 | <RequireCryptoBinding>false</RequireCryptoBinding> |
||
| 196 | '; |
||
| 197 | if ($useAnon == 1) { |
||
| 198 | $w8Ext .= '<PeapExtensions> |
||
| 199 | <IdentityPrivacy xmlns="http://www.microsoft.com/provisioning/MsPeapConnectionPropertiesV2"> |
||
| 200 | <EnableIdentityPrivacy>true</EnableIdentityPrivacy> |
||
| 201 | '; |
||
| 202 | if ($outerUser) { |
||
| 203 | $w8Ext .= '<AnonymousUserName>' . $outerUser . '</AnonymousUserName> |
||
| 204 | '; |
||
| 205 | } else { |
||
| 206 | $w8Ext .= '<AnonymousUserName/> |
||
| 207 | '; |
||
| 208 | } |
||
| 209 | $w8Ext .= '</IdentityPrivacy> |
||
| 210 | </PeapExtensions> |
||
| 211 | '; |
||
| 212 | } |
||
| 213 | $w8Ext .= '</EapType> |
||
| 214 | </Eap> |
||
| 215 | </Config> |
||
| 216 | '; |
||
| 217 | } elseif ($eap == \core\common\EAP::EAPTYPE_TTLS_PAP || $eap == \core\common\EAP::EAPTYPE_TTLS_MSCHAP2) { |
||
| 218 | $profileFileCont .= '<AuthorId xmlns="http://www.microsoft.com/provisioning/EapCommon">311</AuthorId> |
||
| 219 | </EapMethod> |
||
| 220 | '; |
||
| 221 | $w8Ext = '<Config xmlns="http://www.microsoft.com/provisioning/EapHostConfig"> |
||
| 222 | <EapTtls xmlns="http://www.microsoft.com/provisioning/EapTtlsConnectionPropertiesV1"> |
||
| 223 | <ServerValidation> |
||
| 224 | <ServerNames>' . $servers . '</ServerNames> '; |
||
| 225 | if ($caArray) { |
||
| 226 | foreach ($caArray as $certAuthority) { |
||
| 227 | if ($certAuthority['root']) { |
||
| 228 | $w8Ext .= "<TrustedRootCAHash>" . chunk_split($certAuthority['sha1'], 2, ' ') . "</TrustedRootCAHash>\n"; |
||
| 229 | } |
||
| 230 | } |
||
| 231 | } |
||
| 232 | $w8Ext .= '<DisablePrompt>true</DisablePrompt> |
||
| 233 | </ServerValidation> |
||
| 234 | <Phase2Authentication> |
||
| 235 | '; |
||
| 236 | if ($eap == \core\common\EAP::EAPTYPE_TTLS_PAP) { |
||
| 237 | $w8Ext .= '<PAPAuthentication /> '; |
||
| 238 | } |
||
| 239 | if ($eap == \core\common\EAP::EAPTYPE_TTLS_MSCHAP2) { |
||
| 240 | $w8Ext .= '<MSCHAPv2Authentication> |
||
| 241 | <UseWinlogonCredentials>false</UseWinlogonCredentials> |
||
| 242 | </MSCHAPv2Authentication> |
||
| 243 | '; |
||
| 244 | } |
||
| 245 | $w8Ext .= '</Phase2Authentication> |
||
| 246 | <Phase1Identity> |
||
| 247 | '; |
||
| 248 | if ($useAnon == 1) { |
||
| 249 | $w8Ext .= '<IdentityPrivacy>true</IdentityPrivacy> |
||
| 250 | '; |
||
| 251 | if (isset($outerId) && $outerId) { |
||
| 252 | $w8Ext .= '<AnonymousIdentity>' . $outerId . '</AnonymousIdentity> |
||
| 253 | '; |
||
| 254 | } else { |
||
| 255 | $w8Ext .= '<AnonymousIdentity/> |
||
| 256 | '; |
||
| 257 | } |
||
| 258 | } else { |
||
| 259 | $w8Ext .= '<IdentityPrivacy>false</IdentityPrivacy> |
||
| 260 | '; |
||
| 261 | } |
||
| 262 | $w8Ext .= '</Phase1Identity> |
||
| 263 | </EapTtls> |
||
| 264 | </Config> |
||
| 265 | '; |
||
| 266 | } elseif ($eap == \core\common\EAP::EAPTYPE_PWD) { |
||
| 267 | $profileFileCont .= '<AuthorId xmlns="http://www.microsoft.com/provisioning/EapCommon">0</AuthorId> |
||
| 268 | </EapMethod> |
||
| 269 | '; |
||
| 270 | $profileFileCont .= '<ConfigBlob></ConfigBlob>'; |
||
| 271 | } |
||
| 272 | |||
| 273 | $profileFileContEnd = '</EapHostConfig></EAPConfig>'; |
||
| 274 | $returnArray = []; |
||
| 275 | $returnArray['w8'] = $profileFileCont . $w8Ext . $profileFileContEnd; |
||
| 276 | return $returnArray; |
||
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * produce PEAP, TLS and TTLS configuration files for Windows 8 |
||
| 281 | * |
||
| 282 | * @param string $wlanProfileName |
||
| 283 | * @param string $ssid |
||
| 284 | * @param string $auth can be one of "WPA", "WPA2" |
||
| 285 | * @param string $encryption can be one of: "TKIP", "AES" |
||
| 286 | * @param array $eapConfig XML configuration block with EAP config data |
||
| 287 | * @param int $profileNumber counter, which profile number is this |
||
| 288 | * @return string |
||
| 289 | */ |
||
| 290 | View Code Duplication | private function writeWLANprofile($wlanProfileName, $ssid, $auth, $encryption, $eapConfig, $profileNumber) { |
|
| 291 | $profileFileCont = '<?xml version="1.0"?> |
||
| 292 | <WLANProfile xmlns="http://www.microsoft.com/networking/WLAN/profile/v1"> |
||
| 293 | <name>' . $wlanProfileName . '</name> |
||
| 294 | <SSIDConfig> |
||
| 295 | <SSID> |
||
| 296 | <name>' . $ssid . '</name> |
||
| 297 | </SSID> |
||
| 298 | <nonBroadcast>true</nonBroadcast> |
||
| 299 | </SSIDConfig> |
||
| 300 | <connectionType>ESS</connectionType> |
||
| 301 | <connectionMode>auto</connectionMode> |
||
| 302 | <autoSwitch>false</autoSwitch> |
||
| 303 | <MSM> |
||
| 304 | <security> |
||
| 305 | <authEncryption> |
||
| 306 | <authentication>' . $auth . '</authentication> |
||
| 307 | <encryption>' . $encryption . '</encryption> |
||
| 308 | <useOneX>true</useOneX> |
||
| 309 | </authEncryption> |
||
| 310 | '; |
||
| 311 | if ($auth == 'WPA2') { |
||
| 312 | $profileFileCont .= '<PMKCacheMode>enabled</PMKCacheMode> |
||
| 313 | <PMKCacheTTL>720</PMKCacheTTL> |
||
| 314 | <PMKCacheSize>128</PMKCacheSize> |
||
| 315 | <preAuthMode>disabled</preAuthMode> |
||
| 316 | '; |
||
| 317 | } |
||
| 318 | $profileFileCont .= '<OneX xmlns="http://www.microsoft.com/networking/OneX/v1"> |
||
| 319 | <cacheUserData>true</cacheUserData> |
||
| 320 | <authMode>user</authMode> |
||
| 321 | '; |
||
| 322 | |||
| 323 | $closing = ' |
||
| 324 | </OneX> |
||
| 325 | </security> |
||
| 326 | </MSM> |
||
| 327 | </WLANProfile> |
||
| 328 | '; |
||
| 329 | |||
| 330 | if (!is_dir('w8')) { |
||
| 331 | mkdir('w8'); |
||
| 332 | } |
||
| 333 | $xmlFname = "w8/wlan_prof-$profileNumber.xml"; |
||
| 334 | |||
| 335 | file_put_contents($xmlFname, $profileFileCont . $eapConfig['w8'] . $closing); |
||
| 336 | |||
| 337 | $this->loggerInstance->debug(2, "Installer has been written into directory $this->FPATH\n"); |
||
| 338 | $this->loggerInstance->debug(4, "WWWWLAN_Profile:$wlanProfileName:$encryption\n"); |
||
| 339 | return("\"$wlanProfileName\" \"$encryption\""); |
||
| 340 | } |
||
| 341 | |||
| 342 | View Code Duplication | private function writeLANprofile($eapConfig) { |
|
| 366 | } |
||
| 367 | |||
| 368 | private function writeMainNSH($eap, $attr) { |
||
| 399 | } |
||
| 400 | |||
| 401 | View Code Duplication | private function writeProfilesNSH($wlanProfiles, $caArray, $wired = 0) { |
|
| 402 | $this->loggerInstance->debug(4, "writeProfilesNSH"); |
||
| 403 | $this->loggerInstance->debug(4, $wlanProfiles); |
||
| 404 | $fcontentsProfile = ''; |
||
| 405 | foreach ($wlanProfiles as $wlanProfile) { |
||
| 406 | $fcontentsProfile .= "!insertmacro define_wlan_profile $wlanProfile\n"; |
||
| 407 | } |
||
| 408 | |||
| 409 | file_put_contents('profiles.nsh', $fcontentsProfile); |
||
| 410 | |||
| 411 | $fcontentsCerts = ''; |
||
| 412 | $fileHandleCerts = fopen('certs.nsh', 'w'); |
||
| 413 | if ($fileHandleCerts === FALSE) { |
||
| 414 | throw new Exception("Unable to open new certs.nsh file for writing CAs."); |
||
| 415 | } |
||
| 416 | if ($caArray) { |
||
| 417 | foreach ($caArray as $certAuthority) { |
||
| 418 | $store = $certAuthority['root'] ? "root" : "ca"; |
||
| 419 | $fcontentsCerts .= '!insertmacro install_ca_cert "' . $certAuthority['file'] . '" "' . $certAuthority['sha1'] . '" "' . $store . "\"\n"; |
||
| 420 | } |
||
| 421 | fwrite($fileHandleCerts, $fcontentsCerts); |
||
| 422 | } |
||
| 423 | fclose($fileHandleCerts); |
||
| 424 | } |
||
| 425 | |||
| 426 | //private function write |
||
| 427 | |||
| 428 | private function copyFiles($eap) { |
||
| 442 | } |
||
| 443 | |||
| 444 | private $tlsOtherUsername = 0; |
||
| 445 | |||
| 447 |
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.