| Total Complexity | 53 |
| Total Lines | 356 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like DeviceLinux 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 DeviceLinux, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 40 | class DeviceLinux extends \core\DeviceConfig { |
||
| 41 | |||
| 42 | /** |
||
| 43 | * constructor. Sets supported EAP methods. |
||
| 44 | */ |
||
| 45 | final public function __construct() { |
||
| 46 | parent::__construct(); |
||
| 47 | $this->setSupportedEapMethods([\core\common\EAP::EAPTYPE_PEAP_MSCHAP2, \core\common\EAP::EAPTYPE_TTLS_PAP, \core\common\EAP::EAPTYPE_TTLS_MSCHAP2, \core\common\EAP::EAPTYPE_TLS, \core\common\EAP::EAPTYPE_SILVERBULLET]); |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * create the actual installer script |
||
| 52 | * |
||
| 53 | * @return string filename of the generated installer |
||
| 54 | * |
||
| 55 | */ |
||
| 56 | public function writeInstaller() { |
||
| 57 | $installerPath = $this->installerBasename . ".py"; |
||
| 58 | $this->copyFile("main.py", $installerPath); |
||
| 59 | $installer = fopen($installerPath,"a"); |
||
| 60 | if ($installer === FALSE) { |
||
| 61 | throw new Exception("Unable to open installer file for writing!"); |
||
| 62 | } |
||
| 63 | fwrite($installer, "\n\n"); |
||
| 64 | $this->writeMessages($installer); |
||
| 65 | $this->writeConfigVars($installer); |
||
| 66 | fwrite($installer, "run_installer()\n"); |
||
| 67 | fclose($installer); |
||
| 68 | return($installerPath); |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * produces the HTML text to be displayed when clicking on the "help" button |
||
| 73 | * besides the download button. |
||
| 74 | * |
||
| 75 | * @return string |
||
| 76 | */ |
||
| 77 | public function writeDeviceInfo() { |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * writes a line of Python code into the installer script |
||
| 118 | * |
||
| 119 | * @param resource $file the file handle |
||
| 120 | * @param string $prefix prefix to write |
||
| 121 | * @param string $name config item to write |
||
| 122 | * @param string $text text to write |
||
| 123 | * @return void |
||
| 124 | */ |
||
| 125 | private function writeConfigLine($file, $prefix, $name, $text) { |
||
| 126 | $out = $prefix . $name . ' = "' . $text; |
||
| 127 | fwrite($file, wordwrap($out, 70, " \" \\\n \"") . "\n"); |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * localises the user messages and writes them into the file |
||
| 132 | * |
||
| 133 | * @param resource $file the file resource of the installer script |
||
| 134 | * @return void |
||
| 135 | */ |
||
| 136 | private function writeMessages($file) { |
||
| 137 | \core\common\Entity::intoThePotatoes(); |
||
| 138 | $messages = [ |
||
| 139 | 'quit'=> _("Really quit?"), |
||
| 140 | 'username_prompt'=> _("enter your userid"), |
||
| 141 | 'enter_password' => _("enter password"), |
||
| 142 | 'enter_import_password' => _("enter your import password"), |
||
| 143 | 'incorrect_password' => _("incorrect password"), |
||
| 144 | 'repeat_password' => _("repeat your password"), |
||
| 145 | 'passwords_difffer'=> _("passwords do not match"), |
||
| 146 | 'installation_finished' => _("Installation successful"), |
||
| 147 | 'cat_dir_exisits' => _("Directory {} exists; some of its files may be overwritten."), |
||
| 148 | 'cont' => _("Continue?"), |
||
| 149 | 'nm_not_supported' => _("This NetworkManager version is not supported"), |
||
| 150 | 'cert_error' => _("Certificate file not found, looks like a CAT error"), |
||
| 151 | 'unknown_version' => _("Unknown version"), |
||
| 152 | 'dbus_error' => _("DBus connection problem, a sudo might help"), |
||
| 153 | 'yes' => _("Y"), |
||
| 154 | 'no' => _("N"), |
||
| 155 | 'p12_filter' => _("personal certificate file (p12 or pfx)"), |
||
| 156 | 'all_filter' => _("All files"), |
||
| 157 | 'p12_title' => _("personal certificate file (p12 or pfx)"), |
||
| 158 | 'save_wpa_conf' => _("NetworkManager configuration failed, but we may generate a wpa_supplicant configuration file if you wish. Be warned that your connection password will be saved in this file as clear text."), |
||
| 159 | 'save_wpa_confirm' => _("Write the file"), |
||
| 160 | 'wrongUsernameFormat' =>_("Error: Your username must be of the form 'xxx@institutionID' e.g. '[email protected]'!"), |
||
| 161 | 'wrong_realm' => _("Error: your username must be in the form of 'xxx@{}'. Please enter the username in the correct format."), |
||
| 162 | 'wrong_realm_suffix' => _("Error: your username must be in the form of 'xxx@institutionID' and end with '{}'. Please enter the username in the correct format."), |
||
| 163 | 'user_cert_missing' => _("personal certificate file not found"), |
||
| 164 | ]; |
||
| 165 | foreach ($messages as $name => $value) { |
||
| 166 | $this->writeConfigLine($file, 'Messages.', $name, $value . '"'); |
||
| 167 | } |
||
| 168 | \core\common\Entity::outOfThePotatoes(); |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * writes configuration variables into the installer script |
||
| 173 | * |
||
| 174 | * @param resource $file the file handle |
||
| 175 | * @return void |
||
| 176 | */ |
||
| 177 | private function writeConfigVars($file) { |
||
| 178 | $eapMethod = \core\common\EAP::eapDisplayName($this->selectedEap); |
||
| 179 | $contacts = $this->mkSupportContacts(); |
||
| 180 | $tou = $this->mkUserConsent(); |
||
| 181 | $outerId = $this->determineOuterIdString(); |
||
| 182 | $config = [ |
||
| 183 | 'instname' => $this->attributes['general:instname'][0], |
||
| 184 | 'profilename' => $this->attributes['profile:name'][0], |
||
| 185 | 'url' => $contacts['url'], |
||
| 186 | 'email' => $contacts['email'], |
||
| 187 | 'title' => "eduroam CAT", |
||
| 188 | 'server_match' => $this->glueServerNames(), |
||
| 189 | 'eap_outer' => $eapMethod['OUTER'], |
||
| 190 | 'eap_inner' => $eapMethod['INNER'], |
||
| 191 | 'init_info' => $this->mkIntro(), |
||
| 192 | 'init_confirmation' => $this->mkProfileConfirmation(), |
||
| 193 | // 'sb_user_file' => $this->mkSbUserFile(), |
||
| 194 | ]; |
||
| 195 | |||
| 196 | $configRaw = [ |
||
| 197 | 'ssids' => $this->mkSsidList(), |
||
| 198 | 'del_ssids' => $this->mkDelSsidList(), |
||
| 199 | 'servers' => $this->mkSubjectAltNameList(), |
||
| 200 | ]; |
||
| 201 | |||
| 202 | if ($this->selectedEap == \core\common\EAP::EAPTYPE_TLS && isset($this->attributes['eap-specific:tls_use_other_id']) && $this->attributes['eap-specific:tls_use_other_id'][0] == 'on') { |
||
| 203 | $configRaw['use_other_tls_id'] = "True"; |
||
| 204 | } |
||
| 205 | else { |
||
| 206 | $configRaw['use_other_tls_id'] = "False"; |
||
| 207 | } |
||
| 208 | |||
| 209 | if ($outerId !== NULL) { |
||
| 210 | $configRaw['anonymous_identity'] = '"' . $outerId . '"'; |
||
| 211 | } |
||
| 212 | |||
| 213 | if (!empty($this->attributes['internal:realm'][0])) { |
||
| 214 | $config['user_realm'] = $this->attributes['internal:realm'][0]; |
||
| 215 | } |
||
| 216 | |||
| 217 | if(!empty($this->attributes['internal:hint_userinput_suffix'][0]) && $this->attributes['internal:hint_userinput_suffix'][0] == 1) { |
||
| 218 | $configRaw['hint_user_input'] = "True"; |
||
| 219 | } |
||
| 220 | |||
| 221 | if(!empty($this->attributes['internal:verify_userinput_suffix'][0]) && $this->attributes['internal:verify_userinput_suffix'][0] == 1) { |
||
| 222 | $configRaw['verify_user_realm_input'] = "True"; |
||
| 223 | } |
||
| 224 | |||
| 225 | foreach ($config as $name => $value) { |
||
| 226 | $this->writeConfigLine($file, 'Config.', $name, $value . '"'); |
||
| 227 | } |
||
| 228 | |||
| 229 | foreach ($configRaw as $name => $value) { |
||
| 230 | fwrite($file, 'Config.' . $name . ' = ' . $value . "\n"); |
||
| 231 | } |
||
| 232 | |||
| 233 | if ($tou === '') { |
||
| 234 | fwrite($file, 'Config.tou = ""' . "\n"); |
||
| 235 | } else { |
||
| 236 | fwrite($file, 'Config.tou = """' . $tou . '"""' . "\n"); |
||
| 237 | } |
||
| 238 | |||
| 239 | fwrite($file, 'Config.CA = """' . $this->mkCAfile() . '"""' . "\n"); |
||
| 240 | $sbUserFile = $this->mkSbUserFile(); |
||
| 241 | if ($sbUserFile !== '') { |
||
| 242 | fwrite($file, 'Config.sb_user_file = """' . $sbUserFile . '"""' . "\n"); |
||
| 243 | } |
||
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * coerces the list of EAP server names into a single string |
||
| 248 | * |
||
| 249 | * @return string |
||
| 250 | */ |
||
| 251 | private function glueServerNames() { |
||
| 252 | $serverList = $this->attributes['eap:server_name']; |
||
| 253 | if (!$serverList) { |
||
| 254 | return ''; |
||
| 255 | } |
||
| 256 | $A0 = array_reverse(explode('.', array_shift($serverList))); |
||
| 257 | $B = $A0; |
||
| 258 | foreach ($serverList as $oneServer) { |
||
| 259 | $A = array_reverse(explode('.', $oneServer)); |
||
| 260 | $B = array_intersect_assoc($A0, $A); |
||
| 261 | $A0 = $B; |
||
| 262 | } |
||
| 263 | return implode('.', array_reverse($B)); |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * generates the list of support contacts |
||
| 268 | * |
||
| 269 | * @return array |
||
| 270 | */ |
||
| 271 | private function mkSupportContacts() { |
||
| 272 | $url = (!empty($this->attributes['support:url'][0])) ? $this->attributes['support:url'][0] : $this->support_url_substitute; |
||
| 273 | $email = (!empty($this->attributes['support:email'][0])) ? $this->attributes['support:email'][0] : $this->support_email_substitute; |
||
| 274 | return ['url'=>$url, 'email'=>$email]; |
||
| 275 | } |
||
| 276 | |||
| 277 | /** |
||
| 278 | * generates the list of subjectAltNames to configure |
||
| 279 | * |
||
| 280 | * @return string |
||
| 281 | */ |
||
| 282 | private function mkSubjectAltNameList() { |
||
| 283 | $serverList = $this->attributes['eap:server_name']; |
||
| 284 | if (!$serverList) { |
||
| 285 | return ''; |
||
| 286 | } |
||
| 287 | $out = ''; |
||
| 288 | foreach ($serverList as $oneServer) { |
||
| 289 | if ($out) { |
||
| 290 | $out .= ', '; |
||
| 291 | } |
||
| 292 | $out .= "'DNS:$oneServer'"; |
||
| 293 | } |
||
| 294 | return "[" . $out. "]"; |
||
| 295 | } |
||
| 296 | |||
| 297 | /** |
||
| 298 | * generates the list of SSIDs to configure |
||
| 299 | * |
||
| 300 | * @return string |
||
| 301 | */ |
||
| 302 | private function mkSsidList() { |
||
| 303 | $ssids = $this->attributes['internal:SSID']; |
||
| 304 | $outArray = []; |
||
| 305 | foreach ($ssids as $ssid => $cipher) { |
||
| 306 | $outArray[] = "'$ssid'"; |
||
| 307 | } |
||
| 308 | return '[' . implode(', ', $outArray) . ']'; |
||
| 309 | } |
||
| 310 | |||
| 311 | /** |
||
| 312 | * generates the list of SSIDs to delete from the system |
||
| 313 | * |
||
| 314 | * @return string |
||
| 315 | */ |
||
| 316 | private function mkDelSsidList() { |
||
| 317 | $outArray = []; |
||
| 318 | $delSSIDs = $this->attributes['internal:remove_SSID']; |
||
| 319 | foreach ($delSSIDs as $ssid => $cipher) { |
||
| 320 | if ($cipher == 'DEL') { |
||
| 321 | $outArray[] = "'$ssid'"; |
||
| 322 | } |
||
| 323 | } |
||
| 324 | return '[' . implode(', ', $outArray) . ']'; |
||
| 325 | } |
||
| 326 | |||
| 327 | /** |
||
| 328 | * creates a blob containing all CA certificates |
||
| 329 | * |
||
| 330 | * @return string |
||
| 331 | */ |
||
| 332 | private function mkCAfile(){ |
||
| 333 | $out = ''; |
||
| 334 | $cAlist = $this->attributes['internal:CAs'][0]; |
||
| 335 | foreach ($cAlist as $oneCa) { |
||
| 336 | $out .= $oneCa['pem']; |
||
| 337 | } |
||
| 338 | return $out; |
||
| 339 | } |
||
| 340 | |||
| 341 | /** |
||
| 342 | * generates the welcome text |
||
| 343 | * |
||
| 344 | * @return string |
||
| 345 | */ |
||
| 346 | private function mkIntro() { |
||
| 347 | \core\common\Entity::intoThePotatoes(); |
||
| 348 | $out = _("This installer has been prepared for {0}") . '\n\n' . _("More information and comments:") . '\n\nEMAIL: {1}\nWWW: {2}\n\n' . |
||
| 349 | _("Installer created with software from the GEANT project."); |
||
| 350 | \core\common\Entity::outOfThePotatoes(); |
||
| 351 | return $out; |
||
| 352 | } |
||
| 353 | |||
| 354 | /** |
||
| 355 | * generates text for the user consent dialog box, if any |
||
| 356 | * |
||
| 357 | * @return string |
||
| 358 | */ |
||
| 359 | private function mkUserConsent() { |
||
| 367 | } |
||
| 368 | |||
| 369 | /** |
||
| 370 | * generates the warning that the account will only work for inst members |
||
| 371 | * |
||
| 372 | * @return string |
||
| 373 | */ |
||
| 374 | private function mkProfileConfirmation() { |
||
| 375 | \core\common\Entity::intoThePotatoes(); |
||
| 376 | if ($this->attributes['internal:profile_count'][0] > 1) { |
||
| 377 | $out = _("This installer will only work properly if you are a member of {0} and the user group: {1}."); |
||
| 378 | } else { |
||
| 379 | $out = _("This installer will only work properly if you are a member of {0}."); |
||
| 380 | } |
||
| 381 | \core\common\Entity::outOfThePotatoes(); |
||
| 382 | return $out; |
||
| 383 | } |
||
| 384 | |||
| 385 | |||
| 386 | /** |
||
| 387 | * generates the client certificate data for Silberbullet installers |
||
| 388 | * |
||
| 389 | * @return string |
||
| 390 | */ |
||
| 391 | private function mkSbUserFile() { |
||
| 396 | } |
||
| 397 | |||
| 398 | } |
||
| 399 |