| Total Complexity | 48 |
| Total Lines | 325 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Device_XML 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_XML, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 32 | abstract class Device_XML extends \core\DeviceConfig { |
||
| 33 | |||
| 34 | public function __construct() { |
||
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * $lang_scope can be 'global' wheb all lang and all lang-specific information |
||
| 40 | * is dumped or 'single' when only the selected lang (and defaults) are passed |
||
| 41 | * NOTICE: 'global' is not yet supported |
||
| 42 | */ |
||
| 43 | public $langScope; |
||
| 44 | public $allEaps = FALSE; |
||
| 45 | public $VendorSpecific; |
||
| 46 | |||
| 47 | public function writeDeviceInfo() { |
||
| 51 | } |
||
| 52 | |||
| 53 | public function writeInstaller() { |
||
| 54 | $attr = $this->attributes; |
||
| 55 | $NAMESPACE = 'urn:RFC4282:realm'; |
||
| 56 | //EAPIdentityProvider begin |
||
| 57 | $eapIdp = new EAPIdentityProvider(); |
||
| 58 | // $eap_idp->setProperty('ValidUntil',$this->getValidUntil()); |
||
|
|
|||
| 59 | // ProviderInfo-> |
||
| 60 | $eapIdp->setProperty('ProviderInfo', $this->getProviderInfo()); |
||
| 61 | // TODO $eap_idp->setProperty('VendorSpecific',$this->getVendorSpecific()); |
||
| 62 | //AuthenticationMethods |
||
| 63 | // TODO |
||
| 64 | //ID attribute |
||
| 65 | //lang attribute |
||
| 66 | $methodList = []; |
||
| 67 | $this->loggerInstance->debug(4, "ALLEAP\n"); |
||
| 68 | $this->loggerInstance->debug(4, $attr['all_eaps']); |
||
| 69 | if ($this->allEaps) { |
||
| 70 | $eapmethods = []; |
||
| 71 | foreach ($attr['all_eaps'] as $eap) { |
||
| 72 | if (in_array($eap, $this->supportedEapMethods)) { |
||
| 73 | $eapmethods[] = $eap; |
||
| 74 | } |
||
| 75 | } |
||
| 76 | } else { |
||
| 77 | $eapmethods = [$this->selectedEap]; |
||
| 78 | } |
||
| 79 | foreach ($eapmethods as $eap) { |
||
| 80 | $methodList[] = $this->getAuthMethod($eap); |
||
| 81 | } |
||
| 82 | $authMethods = new AuthenticationMethods(); |
||
| 83 | $authMethods->setProperty('AuthenticationMethods', $methodList); |
||
| 84 | $eapIdp->setProperty('AuthenticationMethods', $authMethods); |
||
| 85 | if (empty($attr['internal:realm'][0])) { |
||
| 86 | $eapIdp->setAttribute('ID', 'undefined'); |
||
| 87 | $eapIdp->setAttribute('namespace', 'urn:undefined'); |
||
| 88 | } else { |
||
| 89 | $eapIdp->setAttribute('ID', $attr['internal:realm'][0]); |
||
| 90 | $eapIdp->setAttribute('namespace', $NAMESPACE); |
||
| 91 | } |
||
| 92 | if ($this->langScope === 'single') { |
||
| 93 | $eapIdp->setAttribute('lang', $this->languageInstance->getLang()); |
||
| 94 | } |
||
| 95 | $eapIdp->setAttribute('version', '1'); |
||
| 96 | |||
| 97 | |||
| 98 | // EAPIdentityProvider end |
||
| 99 | // Generate XML |
||
| 100 | |||
| 101 | $rootname = 'EAPIdentityProviderList'; |
||
| 102 | $root = new \SimpleXMLElement("<?xml version=\"1.0\" encoding=\"utf-8\" ?><{$rootname} xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"eap-metadata.xsd\"></{$rootname}>"); |
||
| 103 | |||
| 104 | marshalObject($root, $eapIdp); |
||
| 105 | $dom = dom_import_simplexml($root)->ownerDocument; |
||
| 106 | //TODO schema validation makes sense so probably should be used |
||
| 107 | $res = $dom->schemaValidate(ROOT . '/devices/xml/eap-metadata.xsd'); |
||
| 108 | file_put_contents($this->installerBasename . '.eap-config', $dom->saveXML()); |
||
| 109 | return($this->installerBasename . '.eap-config'); |
||
| 110 | } |
||
| 111 | |||
| 112 | private $AttributeNames = [ |
||
| 113 | 'support:email' => 'EmailAddress', |
||
| 114 | 'support:url' => 'WebAddress', |
||
| 115 | 'support:phone' => 'Phone', |
||
| 116 | 'profile:description' => 'Description', |
||
| 117 | 'support:info_file' => 'TermsOfUse', |
||
| 118 | 'general:logo_file' => 'ProviderLogo', |
||
| 119 | ]; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * |
||
| 123 | * @param string $attrName |
||
| 124 | * @return array of values for this attribute |
||
| 125 | */ |
||
| 126 | private function getSimpleMLAttribute($attrName) { |
||
| 153 | } |
||
| 154 | |||
| 155 | private function getDisplayName() { |
||
| 156 | $attr = $this->attributes; |
||
| 157 | $objs = []; |
||
| 158 | if ($this->langScope === 'global') { |
||
| 159 | $instNameLangs = $attr['general:instname']['langs']; |
||
| 160 | if ($attr['internal:profile_count'][0] > 1) { |
||
| 161 | $profileNameLangs = $attr['profile:name']['langs']; |
||
| 162 | } |
||
| 163 | foreach ($instNameLangs as $language => $value) { |
||
| 164 | $language = ($language === 'C' ? 'any' : $language); |
||
| 165 | $displayname = new DisplayName(); |
||
| 166 | if (isset($profileNameLangs)) { |
||
| 167 | $langOrC = isset($profileNameLangs[$language]) ? $profileNameLangs[$language] : $profileNameLangs['C']; |
||
| 168 | $value .= ' - ' . $langOrC; |
||
| 169 | } |
||
| 170 | $displayname->setValue($value); |
||
| 171 | $displayname->setAttributes(['lang' => $language]); |
||
| 172 | $objs[] = $displayname; |
||
| 173 | } |
||
| 174 | } else { |
||
| 175 | $displayname = new DisplayName(); |
||
| 176 | $value = $attr['general:instname'][0]; |
||
| 177 | if ($attr['internal:profile_count'][0] > 1) { |
||
| 178 | $value .= ' - ' . $attr['profile:name'][0]; |
||
| 179 | } |
||
| 180 | $displayname->setValue($value); |
||
| 181 | $objs[] = $displayname; |
||
| 182 | } |
||
| 183 | return $objs; |
||
| 184 | } |
||
| 185 | |||
| 186 | private function getProviderLogo() { |
||
| 187 | $attr = $this->attributes; |
||
| 188 | if (isset($attr['general:logo_file'][0])) { |
||
| 189 | $logoString = base64_encode($attr['general:logo_file'][0]); |
||
| 190 | $logoMime = 'image/' . $attr['internal:logo_file'][0]['mime']; |
||
| 191 | $providerlogo = new ProviderLogo(); |
||
| 192 | $providerlogo->setAttributes(['mime' => $logoMime, 'encoding' => 'base64']); |
||
| 193 | $providerlogo->setValue($logoString); |
||
| 194 | return $providerlogo; |
||
| 195 | } |
||
| 196 | } |
||
| 197 | |||
| 198 | private function getProviderInfo() { |
||
| 199 | $providerinfo = new ProviderInfo(); |
||
| 200 | $providerinfo->setProperty('DisplayName', $this->getDisplayName()); |
||
| 201 | $providerinfo->setProperty('Description', $this->getSimpleMLAttribute('profile:description')); |
||
| 202 | $providerinfo->setProperty('ProviderLocation', $this->getProvideLocation()); |
||
| 203 | $providerinfo->setProperty('ProviderLogo', $this->getProviderLogo()); |
||
| 204 | $providerinfo->setProperty('TermsOfUse', $this->getSimpleMLAttribute('support:info_file')); |
||
| 205 | $providerinfo->setProperty('Helpdesk', $this->getHelpdesk()); |
||
| 206 | return $providerinfo; |
||
| 207 | } |
||
| 208 | |||
| 209 | private function getProvideLocation() { |
||
| 210 | $attr = $this->attributes; |
||
| 211 | if (isset($attr['general:geo_coordinates'])) { |
||
| 212 | $attrCoordinates = $attr['general:geo_coordinates']; |
||
| 213 | if (count($attrCoordinates) > 1) { |
||
| 214 | $location = []; |
||
| 215 | foreach ($attrCoordinates as $a) { |
||
| 216 | $providerlocation = new ProviderLocation(); |
||
| 217 | $b = json_decode($a, true); |
||
| 218 | $providerlocation->setProperty('Longitude', $b['lon']); |
||
| 219 | $providerlocation->setProperty('Latitude', $b['lat']); |
||
| 220 | $location[] = $providerlocation; |
||
| 221 | } |
||
| 222 | } else { |
||
| 223 | $providerlocation = new ProviderLocation(); |
||
| 224 | $b = json_decode($attrCoordinates[0], true); |
||
| 225 | $providerlocation->setProperty('Longitude', $b['lon']); |
||
| 226 | $providerlocation->setProperty('Latitude', $b['lat']); |
||
| 227 | $location = $providerlocation; |
||
| 228 | } |
||
| 229 | return $location; |
||
| 230 | } |
||
| 231 | } |
||
| 232 | |||
| 233 | private function getHelpdesk() { |
||
| 234 | $helpdesk = new Helpdesk(); |
||
| 235 | $helpdesk->setProperty('EmailAddress', $this->getSimpleMLAttribute('support:email')); |
||
| 236 | $helpdesk->setProperty('WebAddress', $this->getSimpleMLAttribute('support:url')); |
||
| 237 | $helpdesk->setProperty('Phone', $this->getSimpleMLAttribute('support:phone')); |
||
| 238 | return $helpdesk; |
||
| 239 | } |
||
| 240 | |||
| 241 | /* This function is not currently used. |
||
| 242 | * |
||
| 243 | * private function getCompatibleUses() { |
||
| 244 | $ssids = $this->attributes['internal:SSID']; |
||
| 245 | $compatibleuses = new CompatibleUses(); |
||
| 246 | $ieee80211s = []; |
||
| 247 | foreach ($ssids as $ssid => $ciph) { |
||
| 248 | $ieee80211 = new IEEE80211(); |
||
| 249 | $ieee80211->setProperty('SSID', $ssid); |
||
| 250 | $ieee80211->setProperty('MinRSNProto', $ciph == 'AES' ? 'CCMP' : 'TKIP'); |
||
| 251 | $ieee80211s[] = $ieee80211; |
||
| 252 | } |
||
| 253 | $compatibleuses->setProperty('IEEE80211', $ieee80211s); |
||
| 254 | return($compatibleuses); |
||
| 255 | } |
||
| 256 | |||
| 257 | */ |
||
| 258 | private function getAuthenticationMethodParams($eap) { |
||
| 259 | $inner = \core\common\EAP::innerAuth($eap); |
||
| 260 | $outerMethod = $eap["OUTER"]; |
||
| 261 | |||
| 262 | if (isset($inner["METHOD"]) && $inner["METHOD"]) { |
||
| 263 | $innerauthmethod = new InnerAuthenticationMethod(); |
||
| 264 | $typeOfInner = "\devices\xml\\" . ($inner["EAP"] ? 'EAPMethod' : 'NonEAPAuthMethod'); |
||
| 265 | $eapmethod = new $typeOfInner(); |
||
| 266 | $eaptype = new Type(); |
||
| 267 | $eaptype->setValue($inner['METHOD']); |
||
| 268 | $eapmethod->setProperty('Type', $eaptype); |
||
| 269 | $innerauthmethod->setProperty($typeOfInner, $eapmethod); |
||
| 270 | return ['inner_method' => $innerauthmethod, 'methodID' => $outerMethod, 'inner_methodID' => $inner['METHOD']]; |
||
| 271 | } else { |
||
| 272 | return ['inner_method' => 0, 'methodID' => $outerMethod, 'inner_methodID' => 0]; |
||
| 273 | } |
||
| 274 | } |
||
| 275 | |||
| 276 | private function setServerSideCredentials($eaptype) { |
||
| 277 | $attr = $this->attributes; |
||
| 278 | $serversidecredential = new ServerSideCredential(); |
||
| 279 | // Certificates and server names |
||
| 280 | $cAlist = []; |
||
| 281 | $attrCaList = $attr['internal:CAs'][0]; |
||
| 282 | foreach ($attrCaList as $ca) { |
||
| 283 | $caObject = new CA(); |
||
| 284 | $caObject->setValue(base64_encode($ca['der'])); |
||
| 285 | $caObject->setAttributes(['format' => 'X.509', 'encoding' => 'base64']); |
||
| 286 | $cAlist[] = $caObject; |
||
| 287 | } |
||
| 288 | $serverids = []; |
||
| 289 | $servers = $attr['eap:server_name']; |
||
| 290 | foreach ($servers as $server) { |
||
| 291 | $serverid = new ServerID(); |
||
| 292 | $serverid->setValue($server); |
||
| 293 | $serverids[] = $serverid; |
||
| 294 | } |
||
| 295 | $serversidecredential->setProperty('EAPType', $eaptype->getValue()); |
||
| 296 | $serversidecredential->setProperty('CA', $cAlist); |
||
| 297 | $serversidecredential->setProperty('ServerID', $serverids); |
||
| 298 | return($serversidecredential); |
||
| 299 | } |
||
| 300 | |||
| 301 | private function setClientSideCredentials($eapParams) { |
||
| 318 | } |
||
| 319 | |||
| 320 | private function setEapMethod($eaptype) { |
||
| 336 | } |
||
| 337 | |||
| 338 | private function getAuthMethod($eap) { |
||
| 339 | // $attr = $this->attributes; |
||
| 340 | $authmethod = new AuthenticationMethod(); |
||
| 341 | $eapParams = $this->getAuthenticationMethodParams($eap); |
||
| 342 | $eaptype = new Type(); |
||
| 357 | |||
| 358 | |||
| 364 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.