Total Complexity | 65 |
Total Lines | 407 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 1 | Features | 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 |
||
44 | abstract class Device_XML extends \core\DeviceConfig { |
||
45 | |||
46 | /** |
||
47 | * construct the device |
||
48 | */ |
||
49 | public function __construct() { |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * $lang_scope can be 'global' wheb all lang and all lang-specific information |
||
55 | * is dumped or 'single' when only the selected lang (and defaults) are passed |
||
56 | * NOTICE: 'global' is not yet supported |
||
57 | */ |
||
58 | public $langScope; |
||
59 | public $allEaps = FALSE; |
||
60 | public $VendorSpecific; |
||
61 | /** |
||
62 | * A flag to penable correct handling if TTLS-MSCHAP |
||
63 | */ |
||
64 | public $eduroamCATcompatibility = TRUE; |
||
65 | /** |
||
66 | * create HTML code explaining the installer |
||
67 | * |
||
68 | * @return string |
||
69 | */ |
||
70 | public function writeDeviceInfo() { |
||
71 | \core\common\Entity::intoThePotatoes(); |
||
72 | $out = "<p>"; |
||
73 | $out .= sprintf(_("This is a generic configuration file in the IETF <a href='%s'>EAP Metadata -00</a> XML format."), "https://tools.ietf.org/html/draft-winter-opsawg-eap-metadata-00"); |
||
74 | \core\common\Entity::outOfThePotatoes(); |
||
75 | return $out; |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * create the actual XML file |
||
80 | * |
||
81 | * @return string filename of the generated installer |
||
82 | * |
||
83 | */ |
||
84 | public function writeInstaller() { |
||
85 | $attr = $this->attributes; |
||
86 | $NAMESPACE = 'urn:RFC4282:realm'; |
||
87 | //EAPIdentityProvider begin |
||
88 | $eapIdp = new EAPIdentityProvider(); |
||
89 | $eapIdp->setProperty('CredentialApplicability', $this->getCredentialApplicability()); |
||
90 | // $eap_idp->setProperty('ValidUntil',$this->getValidUntil()); |
||
91 | // ProviderInfo-> |
||
92 | $eapIdp->setProperty('ProviderInfo', $this->getProviderInfo()); |
||
93 | // TODO $eap_idp->setProperty('VendorSpecific',$this->getVendorSpecific()); |
||
94 | //AuthenticationMethods |
||
95 | // TODO |
||
96 | //ID attribute |
||
97 | //lang attribute |
||
98 | $methodList = []; |
||
99 | if ($this->allEaps) { |
||
100 | $eapmethods = []; |
||
101 | foreach ($attr['all_eaps'] as $eap) { |
||
102 | $eapRep = $eap->getArrayRep(); |
||
103 | if (in_array($eapRep, $this->supportedEapMethods)) { |
||
104 | $eapmethods[] = $eapRep; |
||
105 | } |
||
106 | } |
||
107 | } else { |
||
108 | $eapmethods = [$this->selectedEap]; |
||
109 | } |
||
110 | foreach ($eapmethods as $eap) { |
||
111 | $methodList[] = $this->getAuthMethod($eap); |
||
112 | } |
||
113 | $authMethods = new AuthenticationMethods(); |
||
114 | $authMethods->setProperty('AuthenticationMethods', $methodList); |
||
115 | $eapIdp->setProperty('AuthenticationMethods', $authMethods); |
||
116 | if (empty($attr['internal:realm'][0])) { |
||
117 | $eapIdp->setAttribute('ID', 'undefined'); |
||
118 | $eapIdp->setAttribute('namespace', 'urn:undefined'); |
||
119 | } else { |
||
120 | $eapIdp->setAttribute('ID', $attr['internal:realm'][0]); |
||
121 | $eapIdp->setAttribute('namespace', $NAMESPACE); |
||
122 | } |
||
123 | if ($this->langScope === 'single') { |
||
124 | $eapIdp->setAttribute('lang', $this->languageInstance->getLang()); |
||
125 | } |
||
126 | $eapIdp->setAttribute('version', '1'); |
||
127 | |||
128 | |||
129 | // EAPIdentityProvider end |
||
130 | // Generate XML |
||
131 | |||
132 | $rootname = 'EAPIdentityProviderList'; |
||
133 | $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}>"); |
||
134 | |||
135 | marshalObject($root, $eapIdp); |
||
136 | $dom = dom_import_simplexml($root)->ownerDocument; |
||
137 | //TODO schema validation makes sense so probably should be used |
||
138 | if ($dom->schemaValidate(ROOT . '/devices/xml/eap-metadata.xsd') === FALSE) { |
||
139 | throw new Exception("Schema validation failed for eap-metadata"); |
||
140 | } |
||
141 | file_put_contents($this->installerBasename . '.eap-config', $dom->saveXML()); |
||
142 | return($this->installerBasename . '.eap-config'); |
||
143 | } |
||
144 | |||
145 | private $AttributeNames = [ |
||
146 | 'support:email' => 'EmailAddress', |
||
147 | 'support:url' => 'WebAddress', |
||
148 | 'support:phone' => 'Phone', |
||
149 | 'profile:description' => 'Description', |
||
150 | 'support:info_file' => 'TermsOfUse', |
||
151 | 'general:logo_file' => 'ProviderLogo', |
||
152 | ]; |
||
153 | |||
154 | /** |
||
155 | * |
||
156 | * @param string $attrName |
||
157 | * @return array of values for this attribute |
||
158 | */ |
||
159 | private function getSimpleMLAttribute($attrName) { |
||
184 | } |
||
185 | |||
186 | private function getDisplayName() { |
||
187 | $attr = $this->attributes; |
||
188 | $objs = []; |
||
189 | if ($this->langScope === 'global') { |
||
190 | $instNameLangs = $attr['general:instname']['langs']; |
||
191 | if ($attr['internal:profile_count'][0] > 1) { |
||
192 | $profileNameLangs = $attr['profile:name']['langs']; |
||
193 | } |
||
194 | foreach ($instNameLangs as $language => $value) { |
||
195 | $language = ($language === 'C' ? 'any' : $language); |
||
196 | $displayname = new DisplayName(); |
||
197 | if (isset($profileNameLangs)) { |
||
198 | $langOrC = isset($profileNameLangs[$language]) ? $profileNameLangs[$language] : $profileNameLangs['C']; |
||
199 | $value .= ' - ' . $langOrC; |
||
200 | } |
||
201 | $displayname->setValue($value); |
||
202 | $displayname->setAttributes(['lang' => $language]); |
||
203 | $objs[] = $displayname; |
||
204 | } |
||
205 | } else { |
||
206 | $displayname = new DisplayName(); |
||
207 | $value = $attr['general:instname'][0]; |
||
208 | if ($attr['internal:profile_count'][0] > 1) { |
||
209 | $value .= ' - ' . $attr['profile:name'][0]; |
||
210 | } |
||
211 | $displayname->setValue($value); |
||
212 | $objs[] = $displayname; |
||
213 | } |
||
214 | return $objs; |
||
215 | } |
||
216 | |||
217 | private function getProviderLogo() { |
||
218 | $attr = $this->attributes; |
||
219 | if (isset($attr['general:logo_file'][0])) { |
||
220 | $logoString = base64_encode($attr['general:logo_file'][0]); |
||
221 | $logoMime = 'image/' . $attr['internal:logo_file'][0]['mime']; |
||
222 | $providerlogo = new ProviderLogo(); |
||
223 | $providerlogo->setAttributes(['mime' => $logoMime, 'encoding' => 'base64']); |
||
224 | $providerlogo->setValue($logoString); |
||
225 | return $providerlogo; |
||
226 | } |
||
227 | } |
||
228 | |||
229 | private function getProviderInfo() { |
||
230 | $providerinfo = new ProviderInfo(); |
||
231 | $providerinfo->setProperty('DisplayName', $this->getDisplayName()); |
||
232 | $providerinfo->setProperty('Description', $this->getSimpleMLAttribute('profile:description')); |
||
233 | $providerinfo->setProperty('ProviderLocation', $this->getProvideLocation()); |
||
234 | $providerinfo->setProperty('ProviderLogo', $this->getProviderLogo()); |
||
235 | $providerinfo->setProperty('TermsOfUse', $this->getSimpleMLAttribute('support:info_file')); |
||
236 | $providerinfo->setProperty('Helpdesk', $this->getHelpdesk()); |
||
237 | return $providerinfo; |
||
238 | } |
||
239 | |||
240 | private function getProvideLocation() { |
||
241 | $attr = $this->attributes; |
||
242 | if (isset($attr['general:geo_coordinates'])) { |
||
243 | $attrCoordinates = $attr['general:geo_coordinates']; |
||
244 | if (count($attrCoordinates) > 1) { |
||
245 | $location = []; |
||
246 | foreach ($attrCoordinates as $a) { |
||
247 | $providerlocation = new ProviderLocation(); |
||
248 | $b = json_decode($a, true); |
||
249 | $providerlocation->setProperty('Longitude', $b['lon']); |
||
250 | $providerlocation->setProperty('Latitude', $b['lat']); |
||
251 | $location[] = $providerlocation; |
||
252 | } |
||
253 | } else { |
||
254 | $providerlocation = new ProviderLocation(); |
||
255 | $b = json_decode($attrCoordinates[0], true); |
||
256 | $providerlocation->setProperty('Longitude', $b['lon']); |
||
257 | $providerlocation->setProperty('Latitude', $b['lat']); |
||
258 | $location = $providerlocation; |
||
259 | } |
||
260 | return $location; |
||
261 | } |
||
262 | } |
||
263 | |||
264 | private function getHelpdesk() { |
||
265 | $helpdesk = new Helpdesk(); |
||
266 | $helpdesk->setProperty('EmailAddress', $this->getSimpleMLAttribute('support:email')); |
||
267 | $helpdesk->setProperty('WebAddress', $this->getSimpleMLAttribute('support:url')); |
||
268 | $helpdesk->setProperty('Phone', $this->getSimpleMLAttribute('support:phone')); |
||
269 | return $helpdesk; |
||
270 | } |
||
271 | |||
272 | private function getCredentialApplicability() { |
||
273 | $setWired = isset($this->attributes['media:wired'][0]) && |
||
274 | $this->attributes['media:wired'][0] == 'on' ? 1 : 0; |
||
275 | $ssids = $this->attributes['internal:SSID']; |
||
276 | $oids = $this->attributes['internal:consortia']; |
||
277 | $credentialapplicability = new CredentialApplicability(); |
||
278 | $ieee80211s = []; |
||
279 | foreach ($ssids as $ssid => $ciph) { |
||
280 | $ieee80211 = new IEEE80211(); |
||
281 | $ieee80211->setProperty('SSID', $ssid); |
||
282 | $ieee80211->setProperty('MinRSNProto', $ciph == 'AES' ? 'CCMP' : 'TKIP'); |
||
283 | $ieee80211s[] = $ieee80211; |
||
284 | } |
||
285 | foreach ($oids as $oid) { |
||
286 | $ieee80211 = new IEEE80211(); |
||
287 | $ieee80211->setProperty('ConsortiumOID', $oid); |
||
288 | $ieee80211s[] = $ieee80211; |
||
289 | } |
||
290 | $credentialapplicability->setProperty('IEEE80211', $ieee80211s); |
||
291 | if ($setWired) { |
||
292 | $credentialapplicability->setProperty('IEEE8023', ''); |
||
293 | } |
||
294 | return($credentialapplicability); |
||
295 | } |
||
296 | |||
297 | /** |
||
298 | * determines the inner authentication. Is it EAP, and which mechanism is used to convey actual auth data |
||
299 | * @param array $eap the EAP type for which we want to get the inner auth |
||
300 | * @return array |
||
301 | */ |
||
302 | private function innerAuth($eap) |
||
303 | { |
||
304 | $out = []; |
||
305 | $out['EAP'] = 0; |
||
306 | |||
307 | if ($eap == \core\common\EAP::EAPTYPE_TTLS_MSCHAP2) { |
||
308 | if ($this->eduroamCATcompatibility === TRUE) { |
||
309 | $out['METHOD'] = \core\common\EAP::MSCHAP2; |
||
310 | $out['EAP'] = 1; |
||
311 | } else { |
||
312 | $out['METHOD'] = \core\common\EAP::NE_MSCHAP2; |
||
313 | } |
||
314 | return $out; |
||
315 | } |
||
316 | |||
317 | if ($eap == \core\common\EAP::EAPTYPE_SILVERBULLET) { |
||
318 | $out['METHOD'] = \core\common\EAP::NONE; |
||
319 | return $out; |
||
320 | } |
||
321 | |||
322 | $out['METHOD'] = $eap["INNER"]; |
||
323 | |||
324 | // override if there is an inner EAP |
||
325 | if ($eap["INNER"] != 0) { // there is an inner EAP method |
||
326 | $out['EAP'] = 1; |
||
327 | } |
||
328 | return $out; |
||
329 | } |
||
330 | |||
331 | private function getAuthenticationMethodParams($eap) { |
||
332 | $inner = $this->innerAuth($eap); |
||
333 | $outerMethod = $eap["OUTER"]; |
||
334 | |||
335 | if (isset($inner["METHOD"]) && $inner["METHOD"]) { |
||
336 | $innerauthmethod = new InnerAuthenticationMethod(); |
||
337 | $typeOfInner = "\devices\xml\\" . ($inner["EAP"] ? 'EAPMethod' : 'NonEAPAuthMethod'); |
||
338 | $eapmethod = new $typeOfInner(); |
||
339 | $eaptype = new Type(); |
||
340 | $eaptype->setValue($inner['METHOD']); |
||
341 | $eapmethod->setProperty('Type', $eaptype); |
||
342 | $innerauthmethod->setProperty($typeOfInner, $eapmethod); |
||
343 | return ['inner_method' => $innerauthmethod, 'methodID' => $outerMethod, 'inner_methodID' => $inner['METHOD']]; |
||
344 | } else { |
||
345 | return ['inner_method' => 0, 'methodID' => $outerMethod, 'inner_methodID' => 0]; |
||
346 | } |
||
347 | } |
||
348 | |||
349 | private function setServerSideCredentials($eaptype) { |
||
350 | $attr = $this->attributes; |
||
351 | $serversidecredential = new ServerSideCredential(); |
||
352 | // Certificates and server names |
||
353 | $cAlist = []; |
||
354 | $attrCaList = $attr['internal:CAs'][0]; |
||
355 | foreach ($attrCaList as $ca) { |
||
356 | $caObject = new CA(); |
||
357 | $caObject->setValue(base64_encode($ca['der'])); |
||
358 | $caObject->setAttributes(['format' => 'X.509', 'encoding' => 'base64']); |
||
359 | $cAlist[] = $caObject; |
||
360 | } |
||
361 | $serverids = []; |
||
362 | $servers = $attr['eap:server_name']; |
||
363 | foreach ($servers as $server) { |
||
364 | $serverid = new ServerID(); |
||
365 | $serverid->setValue($server); |
||
366 | $serverids[] = $serverid; |
||
367 | } |
||
368 | $serversidecredential->setProperty('EAPType', $eaptype->getValue()); |
||
369 | $serversidecredential->setProperty('CA', $cAlist); |
||
370 | $serversidecredential->setProperty('ServerID', $serverids); |
||
371 | return($serversidecredential); |
||
372 | } |
||
373 | |||
374 | private function setClientSideRealm ($clientsidecredential) { |
||
375 | $attr = $this->attributes; |
||
376 | $realm = \core\common\Entity::getAttributeValue($attr, 'internal:realm', 0); |
||
377 | if ($realm === NULL) { |
||
378 | return; |
||
379 | } |
||
380 | if (\core\common\Entity::getAttributeValue($attr, 'internal:verify_userinput_suffix', 0) !== 1) { |
||
381 | return; |
||
382 | } |
||
383 | $clientsidecredential->setProperty('InnerIdentitySuffix', $realm); |
||
384 | if (\core\common\Entity::getAttributeValue($attr, 'internal:hint_userinput_suffix', 0) === 1) { |
||
385 | $clientsidecredential->setProperty('InnerIdentityHint', 'true'); |
||
386 | } |
||
387 | } |
||
388 | |||
389 | private function setClientCetificate() { |
||
394 | } |
||
395 | |||
396 | private function setClientSideCredentials($eapParams) { |
||
413 | } |
||
414 | |||
415 | private function setEapMethod($eaptype) { |
||
430 | } |
||
431 | |||
432 | private function getAuthMethod($eap) { |
||
433 | // $attr = $this->attributes; |
||
434 | $authmethod = new AuthenticationMethod(); |
||
435 | $eapParams = $this->getAuthenticationMethodParams($eap); |
||
436 | $eaptype = new Type(); |
||
451 | |||
452 | |||
453 | } |
||
458 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.