Total Complexity | 51 |
Total Lines | 359 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
Complex classes like DeviceLinuxSh 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 DeviceLinuxSh, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class DeviceLinuxSh extends \core\DeviceConfig { |
||
32 | /** |
||
33 | * constructor. Sets supported EAP methods. |
||
34 | */ |
||
35 | final public function __construct() { |
||
40 | |||
41 | } |
||
42 | |||
43 | /** |
||
44 | * create the actual installer script |
||
45 | * |
||
46 | * @return string filename of the generated installer |
||
47 | * @throws Exception |
||
48 | * |
||
49 | */ |
||
50 | public function writeInstaller() { |
||
51 | $installerPath = $this->installerBasename.".sh"; |
||
52 | $this->copyFile("eduroam_linux_main.sh", $installerPath); |
||
53 | |||
54 | if ( !file_exists($installerPath) ) { |
||
55 | throw new Exception('File not found.'); |
||
56 | } |
||
57 | |||
58 | if (!$installer = fopen($installerPath, "a")) { |
||
59 | throw new Exception("Unable to open installer file for writing!"); |
||
60 | } |
||
61 | |||
62 | try { |
||
63 | fwrite($installer, "\n\n"); |
||
64 | fseek($installer, 0, SEEK_END); |
||
65 | $this->writeMessages($installer); |
||
66 | $this->writeConfigVars($installer); |
||
67 | fwrite($installer, 'printf -v INIT_INFO "$INIT_INFO_TMP" "$ORGANISATION" "$EMAIL" "$URL"'."\n"); |
||
68 | fwrite($installer, 'printf -v INIT_CONFIRMATION "$INIT_CONFIRMATION_TMP" "$ORGANISATION"'."\n\n"); |
||
69 | fwrite($installer, 'main "$@"; exit'."\n"); |
||
70 | } catch (Exception $e) { |
||
71 | echo 'Error message: ' .$e->getMessage(); |
||
72 | } finally { |
||
73 | fclose($installer); |
||
74 | return($installerPath); |
||
75 | } |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * produces the HTML text to be displayed when clicking on the "help" button |
||
80 | * besides the download button. |
||
81 | * |
||
82 | * @return string |
||
83 | */ |
||
84 | public function writeDeviceInfo() { |
||
85 | \core\common\Entity::intoThePotatoes(); |
||
86 | $out = sprintf(_("The installer is in the form of a Python script. It will try to configure %s under NetworkManager and if this is either not appropriate for your system or your version of NetworkManager is too old, a wpa_supplicant config file will be created instead."), \config\ConfAssistant::CONSORTIUM['display_name']); |
||
87 | $out .= "<p>"._("The installer will configure access to:")." <strong>"; |
||
88 | $out .= implode('</strong>, <strong>', array_keys($this->attributes['internal:networks'])); |
||
89 | $out .= '</strong><p>'; |
||
90 | |||
91 | $out .= _("The installer will create cat_installer sub-directory in your config directory (possubly the .config in your home directory) and will copy your server certificates there."); |
||
92 | if ($this->selectedEap == \core\common\EAP::EAPTYPE_TLS) { |
||
93 | $out .= _("In order to connect to the network you will need a personal certificate in the form of a p12 file. You should obtain this certificate from your organisation. Consult the support page to find out how this certificate can be obtained. Such certificate files are password protected. You should have both the file and the password available during the installation process. Your p12 file will also be copied to the cat_installer directory."); |
||
94 | } elseif ($this->selectedEap != \core\common\EAP::EAPTYPE_SILVERBULLET) { |
||
95 | $out .= _("In order to connect to the network you will need an account from your organisation. You should consult the support page to find out how this account can be obtained. It is very likely that your account is already activated."); |
||
96 | $out .= "<p>"; |
||
97 | $out .= _("You will be requested to enter your account credentials during the installation. This information will be saved so that you will reconnect to the network automatically each time you are in the range."); |
||
98 | } |
||
99 | // nothing to say if we are doing silverbullet. |
||
100 | $out .= "<p>"; |
||
101 | \core\common\Entity::outOfThePotatoes(); |
||
102 | return $out; |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * writes a line of Python code into the installer script |
||
107 | * |
||
108 | * @param resource $file the file handle |
||
109 | * @param string $name config item to write |
||
110 | * @param string $text text to write |
||
111 | * @return void |
||
112 | */ |
||
113 | private function writeConfigLine($file, $name, $text) { |
||
114 | $out = $name.'="'.$text.'"'."\n"; |
||
115 | // fwrite($file, wordwrap($out, 70, "\n")); |
||
116 | fwrite($file, $out); |
||
117 | |||
118 | } |
||
119 | |||
120 | /** |
||
121 | * localises the user messages and writes them into the file |
||
122 | * |
||
123 | * @param resource $file the file resource of the installer script |
||
124 | * @return void |
||
125 | */ |
||
126 | private function writeMessages($file) { |
||
127 | \core\common\Entity::intoThePotatoes(); |
||
128 | $messages = [ |
||
129 | 'QUIT'=> _("Really quit?"), |
||
130 | 'USERNAME_PROMPT'=> _("enter your userid"), |
||
131 | 'ENTER_PASSWORD' => _("enter password"), |
||
132 | 'ENTER_IMPORT_PASSWORD' => _("enter your import password"), |
||
133 | 'INCORRECT_PASSWORD' => _("incorrect password"), |
||
134 | 'REPEAT_PASSWORD' => _("repeat your password"), |
||
135 | 'PASSWORD_DIFFER'=> _("passwords do not match"), |
||
136 | 'INSTALLATION_FINISHED' => _("Installation successful"), |
||
137 | 'CAT_DIR_EXISTS' => _("Directory %s exists; some of its files may be overwritten."), |
||
138 | 'CONTINUE' => _("Continue?"), |
||
139 | 'NM_NOT_SUPPORTED' => _("This NetworkManager version is not supported"), |
||
140 | 'CERT_ERROR' => _("Certificate file not found, looks like a CAT error"), |
||
141 | 'UNKNOWN_VERSION' => _("Unknown version"), |
||
142 | 'DBUS_ERROR' => _("DBus connection problem, a sudo might help"), |
||
143 | 'YES' => _("Y"), |
||
144 | 'NO' => _("N"), |
||
145 | 'P12_FILTER' => _("personal certificate file (p12 or pfx)"), |
||
146 | 'ALL_FILTER' => _("All files"), |
||
147 | 'P12_TITLE' => _("personal certificate file (p12 or pfx)"), |
||
148 | '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."), |
||
149 | 'SAVE_WPA_CONFIRM' => _("Write the file"), |
||
150 | 'WRONG_USERNAME_FORMAT' =>_("Error: Your username must be of the form 'xxx@institutionID' e.g. '[email protected]'!"), |
||
151 | 'WRONG_REALM' => _("Error: your username must be in the form of 'xxx@%s'. Please enter the username in the correct format."), |
||
152 | 'WRONG_REALM_SUFFIX' => _("Error: your username must be in the form of 'xxx@institutionID' and end with '%s'. Please enter the username in the correct format."), |
||
153 | 'USER_CERT_MISSING' => _("personal certificate file not found"), |
||
154 | ]; |
||
155 | foreach ($messages as $name => $value) { |
||
156 | $this->writeConfigLine($file, $name, $value); |
||
157 | } |
||
158 | \core\common\Entity::outOfThePotatoes(); |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * writes configuration variables into the installer script |
||
163 | * |
||
164 | * @param resource $file the file handle |
||
165 | * @return void |
||
166 | */ |
||
167 | private function writeConfigVars($file) { |
||
235 | } |
||
236 | } |
||
237 | |||
238 | /** |
||
239 | * coerces the list of EAP server names into a single string |
||
240 | * |
||
241 | * @return string |
||
242 | */ |
||
243 | private function glueServerNames() { |
||
244 | $serverList = $this->attributes['eap:server_name']; |
||
245 | if (!$serverList) { |
||
246 | return ''; |
||
247 | } |
||
248 | $A0 = array_reverse(explode('.', array_shift($serverList))); |
||
249 | $B = $A0; |
||
250 | foreach ($serverList as $oneServer) { |
||
251 | $A = array_reverse(explode('.', $oneServer)); |
||
252 | $B = array_intersect_assoc($A0, $A); |
||
253 | $A0 = $B; |
||
254 | } |
||
255 | return implode('.', array_reverse($B)); |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * generates the list of support contacts |
||
260 | * |
||
261 | * @return array |
||
262 | */ |
||
263 | private function mkSupportContacts() { |
||
264 | $url = (!empty($this->attributes['support:url'][0])) ? $this->attributes['support:url'][0] : $this->support_url_substitute; |
||
265 | $email = (!empty($this->attributes['support:email'][0])) ? $this->attributes['support:email'][0] : $this->support_email_substitute; |
||
266 | return ['url'=>$url, 'email'=>$email]; |
||
267 | } |
||
268 | |||
269 | /** |
||
270 | * generates the list of subjectAltNames to configure |
||
271 | * |
||
272 | * @return string |
||
273 | */ |
||
274 | private function mkSubjectAltNameList() { |
||
275 | $serverList = $this->attributes['eap:server_name']; |
||
276 | if (!$serverList) { |
||
277 | return ''; |
||
278 | } |
||
279 | $out = ''; |
||
280 | foreach ($serverList as $oneServer) { |
||
281 | if ($out) { |
||
282 | $out .= ', '; |
||
283 | } |
||
284 | $out .= "'DNS:$oneServer'"; |
||
285 | } |
||
286 | return "(".$out. ")"; |
||
287 | } |
||
288 | |||
289 | /** |
||
290 | * generates the list of SSIDs to configure |
||
291 | * |
||
292 | * @return string |
||
293 | */ |
||
294 | private function mkSsidList() { |
||
295 | $networks = $this->attributes['internal:networks']; |
||
296 | $outArray = []; |
||
297 | foreach ($networks as $network => $networkDetails) { |
||
298 | if (!empty($networkDetails['ssid'])) { |
||
299 | $outArray = array_merge($outArray, $networkDetails['ssid']); |
||
300 | } |
||
301 | } |
||
302 | return "['".implode("', '", $outArray)."']"; |
||
303 | } |
||
304 | |||
305 | /** |
||
306 | * generates the list of SSIDs to delete from the system |
||
307 | * |
||
308 | * @return string |
||
309 | */ |
||
310 | private function mkDelSsidList() { |
||
311 | $outArray = []; |
||
312 | $delSSIDs = $this->attributes['internal:remove_SSID']; |
||
313 | foreach ($delSSIDs as $ssid => $cipher) { |
||
314 | if ($cipher == 'DEL') { |
||
315 | $outArray[] = "'$ssid'"; |
||
316 | } |
||
317 | } |
||
318 | return '('.implode(', ', $outArray).')'; |
||
319 | } |
||
320 | |||
321 | /** |
||
322 | * creates a blob containing all CA certificates |
||
323 | * |
||
324 | * @return string |
||
325 | */ |
||
326 | private function mkCAfile(){ |
||
327 | $out = ''; |
||
328 | $cAlist = $this->attributes['internal:CAs'][0]; |
||
329 | foreach ($cAlist as $oneCa) { |
||
330 | $out .= $oneCa['pem']; |
||
331 | } |
||
332 | return $out; |
||
333 | } |
||
334 | |||
335 | /** |
||
336 | * generates the welcome text |
||
337 | * |
||
338 | * @return string |
||
339 | */ |
||
340 | private function mkIntro() { |
||
341 | \core\common\Entity::intoThePotatoes(); |
||
342 | $out = _("This installer has been prepared for %s").'\n\n'._("More information and comments:").'\n\nE-Mail: %s\nWWW: %s\n\n' . |
||
343 | _("Installer created with software from the GEANT project."); |
||
344 | \core\common\Entity::outOfThePotatoes(); |
||
345 | return $out; |
||
346 | } |
||
347 | |||
348 | /** |
||
349 | * generates text for the user consent dialog box, if any |
||
350 | * |
||
351 | * @return string |
||
352 | */ |
||
353 | private function mkUserConsent() { |
||
354 | $out = ''; |
||
355 | if (isset($this->attributes['support:info_file'])) { |
||
356 | if ($this->attributes['internal:info_file'][0]['mime'] == 'txt') { |
||
357 | $out = $this->attributes['support:info_file'][0]; |
||
358 | } |
||
359 | } |
||
360 | return $out; |
||
361 | } |
||
362 | |||
363 | /** |
||
364 | * generates the warning that the account will only work for inst members |
||
365 | * |
||
366 | * @return string |
||
367 | */ |
||
368 | private function mkProfileConfirmation() { |
||
377 | } |
||
378 | |||
379 | |||
380 | /** |
||
381 | * generates the client certificate data for Silberbullet installers |
||
382 | * |
||
383 | * @return string |
||
384 | */ |
||
385 | private function mkSbUserFile() { |
||
390 | } |
||
391 | |||
392 | } |
||
393 |