| Total Complexity | 79 |
| Total Lines | 361 |
| Duplicated Lines | 4.16 % |
| 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 SimpleGUI 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 SimpleGUI, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 29 | class SimpleGUI extends \core\UserAPI { |
||
| 30 | /** |
||
| 31 | * create the SimpleGUI object calling CAT constructor first |
||
| 32 | * |
||
| 33 | * sets up all public prperties of the object |
||
| 34 | */ |
||
| 35 | public function __construct() { |
||
| 36 | parent::__construct(); |
||
| 37 | $validator = new \web\lib\common\InputValidation(); |
||
| 38 | $this->args = []; |
||
| 39 | $this->page = 0; |
||
| 40 | $this->languageInstance->setTextDomain('core'); |
||
| 41 | $this->args['lang'] = $this->languageInstance->getLang(); |
||
| 42 | |||
| 43 | /* |
||
| 44 | The request may contain identifiers of country, idp, profile, device |
||
| 45 | We require that if an identifiet of a lower level exists then all higher identifiers must also |
||
| 46 | be present and match. If a mismatch occures that the lower level identifiers are dropped |
||
| 47 | */ |
||
| 48 | |||
| 49 | if (isset($_REQUEST['reset_dev']) && $_REQUEST['reset_dev'] == 1) { |
||
| 50 | unset($_REQUEST['device']); |
||
| 51 | } |
||
| 52 | |||
| 53 | /* Start with checking if we have the country code if not then use geolocation.. |
||
| 54 | */ |
||
| 55 | $federations = array_keys($this->printCountryList(1)); |
||
| 56 | if (isset($_REQUEST['country']) && $_REQUEST['country']) { |
||
| 57 | $country = strtoupper($_REQUEST['country']); |
||
| 58 | } else { |
||
| 59 | $location = $this->locateUser(); |
||
| 60 | if ($location['status'] == 'ok') { |
||
| 61 | $country = strtoupper($location['country']); |
||
| 62 | } else { |
||
| 63 | $this->loggerInstance->debug(2, "No coutry provided and unable to locate the address\n"); |
||
| 64 | $country = 'NONE'; |
||
| 65 | } |
||
| 66 | } |
||
| 67 | if (!in_array($country, $federations)) { |
||
| 68 | $country = array_shift($federations); |
||
| 69 | } |
||
| 70 | $this->country = $validator->Federation($country); |
||
| 71 | $this->args['country'] = $this->country->identifier; |
||
| 72 | $this->page = 1; |
||
| 73 | |||
| 74 | // If we have IdP identifier then match country to this identifier |
||
| 75 | // if the request contians a country code and an IdP code that do nat match |
||
| 76 | // then drop the IdP code and just leave the country |
||
| 77 | // If we have Profile identifier then test if we also have IdP identifier, if we do |
||
| 78 | // and they do not match then drop the profile code and just leave the IdP |
||
| 79 | |||
| 80 | if (isset($_REQUEST['idp']) && $_REQUEST['idp']) { |
||
| 81 | $this->page = 2; |
||
| 82 | try { |
||
| 83 | $this->idp = $validator->IdP($_REQUEST['idp']); |
||
| 84 | } catch (Exception $fail) { |
||
| 85 | $this->page = 1; |
||
| 86 | $this->languageInstance->setTextDomain("web_user"); |
||
| 87 | return; |
||
| 88 | } |
||
| 89 | $countryTemp = new \core\Federation($this->idp->federation); |
||
| 90 | if (strtoupper($this->country->identifier) !== strtoupper($countryTemp->identifier)) { |
||
| 91 | unset($this->idp); |
||
| 92 | $this->page = 1; |
||
| 93 | $this->languageInstance->setTextDomain("web_user"); |
||
| 94 | return; |
||
| 95 | } |
||
| 96 | $this->args['idp'] = $this->idp->identifier; |
||
| 97 | $this->profileCount = $this->idp->profileCount(); |
||
| 98 | if (!isset($_REQUEST['profile'])) { |
||
| 99 | $this->languageInstance->setTextDomain("web_user"); |
||
| 100 | return; |
||
| 101 | } |
||
| 102 | $this->page = 3; |
||
| 103 | try { |
||
| 104 | $this->profile = $validator->Profile($_REQUEST['profile']); |
||
| 105 | } catch (Exception $fail) { |
||
| 106 | $this->page = 2; |
||
| 107 | $this->languageInstance->setTextDomain("web_user"); |
||
| 108 | return; |
||
| 109 | } |
||
| 110 | if ($this->profile->institution != $this->idp->identifier) { |
||
| 111 | unset($this->profile); |
||
| 112 | $this->page = 2; |
||
| 113 | $this->languageInstance->setTextDomain("web_user"); |
||
| 114 | return; |
||
| 115 | } |
||
| 116 | $this->args['profile'] = $this->profile->identifier; |
||
| 117 | if (isset($_REQUEST['device'])) { |
||
| 118 | $this->args['device'] = $validator->Device($_REQUEST['device']); |
||
| 119 | } |
||
| 120 | } |
||
| 121 | $this->languageInstance->setTextDomain("web_user"); |
||
| 122 | } |
||
| 123 | |||
| 124 | // print country selection |
||
| 125 | public function listCountries() { |
||
| 126 | $out = ''; |
||
| 127 | $federations = $this->printCountryList(1); |
||
| 128 | $out .= _('Select your country') . '<br>'; |
||
| 129 | $out .= '<select name="country" onchange="submit_form(this)">' . "\n"; |
||
| 130 | foreach ($federations as $fedId => $fedName) { |
||
| 131 | $out .= '<option value="' . $fedId . '"'; |
||
| 132 | if ($fedId === $this->country->identifier) { |
||
| 133 | $out .= ' selected'; |
||
| 134 | } |
||
| 135 | $out .= '>' . $fedName . '</option>' . "\n"; |
||
| 136 | } |
||
| 137 | $out .= '</select>'; |
||
| 138 | return $out; |
||
| 139 | } |
||
| 140 | |||
| 141 | public function listIdPs() { |
||
| 142 | $instList = $this->orderIdentityProviders($this->country->identifier); |
||
| 143 | $out = ''; |
||
| 144 | $out .= sprintf(_("Select your %s"), $this->nomenclature_inst ); |
||
| 145 | $out .= '<select name="idp" onchange="submit_form(this)">'; |
||
| 146 | if (!empty($instList)) { |
||
| 147 | if (!isset($this->idp)) { |
||
| 148 | $this->idp = new \core\IdP($instList[0]['idp']); |
||
| 149 | } |
||
| 150 | $idpId = $this->idp->identifier; |
||
| 151 | } |
||
| 152 | foreach ($instList as $oneInst) { |
||
| 153 | $out .= '<option value="' . $oneInst['idp'] . '"'; |
||
| 154 | if ($oneInst['idp'] == $idpId) { |
||
|
|
|||
| 155 | $out .= ' selected'; |
||
| 156 | } |
||
| 157 | $out .= '>' . $oneInst['title'] . '</option>'; |
||
| 158 | } |
||
| 159 | $out .= '</select>'; |
||
| 160 | return $out; |
||
| 161 | } |
||
| 162 | |||
| 163 | public function listProfiles() { |
||
| 164 | if (empty($this->idp)) { |
||
| 165 | return(''); |
||
| 166 | } |
||
| 167 | $profiles = $this->idp->listProfiles(TRUE); |
||
| 168 | if (!isset($this->profile)) { |
||
| 169 | $this->profile = $profiles[0]; |
||
| 170 | } |
||
| 171 | $profileId = $this->profile->identifier; |
||
| 172 | $this->args['profile'] = $profileId; |
||
| 173 | $out = ''; |
||
| 174 | if (count($profiles) > 1) { |
||
| 175 | $out .= _("Select the user group") . '<br>'; |
||
| 176 | $out .= '<select name="profile" onchange="submit_form(this)">'; |
||
| 177 | foreach ($profiles as $profile) { |
||
| 178 | $out .= '<option value="' . $profile->identifier . '"'; |
||
| 179 | if ($profile->identifier == $profileId) { |
||
| 180 | $out .= ' selected'; |
||
| 181 | } |
||
| 182 | $out .= '>' . $profile->name . '</option>'; |
||
| 183 | } |
||
| 184 | $out .= '</select>'; |
||
| 185 | } else { |
||
| 186 | $out .= $this->passArgument('profile'); |
||
| 187 | } |
||
| 188 | return $out; |
||
| 189 | } |
||
| 190 | |||
| 191 | public function listProfileDevices() { |
||
| 192 | if (!isset($this->profile)) { |
||
| 193 | return ''; |
||
| 194 | } |
||
| 195 | $detectedOs = $this->detectOS(); |
||
| 196 | $deviceName = $detectedOs['device']; |
||
| 197 | $this->args['device'] = $deviceName; |
||
| 198 | $profileRedirect = 0; |
||
| 199 | $redirectTarget = ''; |
||
| 200 | $deviceRedirects = ''; |
||
| 201 | $selectedOs = 0; |
||
| 202 | $unsupportedMessage = '<div id="unsupported_os">' . sprintf(_("Your operating system was not properly detected, is not supported yet or cannot be configured with settings provided by your %s"), $this->nomenclature_inst) . "</div><br>"; |
||
| 203 | |||
| 204 | $attributes = $this->profileAttributes($this->profile->identifier); |
||
| 205 | $thedevices = $attributes['devices']; |
||
| 206 | $message = ''; |
||
| 207 | if (!$deviceName) { |
||
| 208 | $message = $unsupportedMessage; |
||
| 209 | } |
||
| 210 | if ($attributes['silverbullet']) { |
||
| 211 | $out = _("You can download your eduroam installer via a personalised invitation link sent from your IT support. Please talk to the IT department to get this link."); |
||
| 212 | return $out; |
||
| 213 | } |
||
| 214 | $out = _("Choose an installer to download") . '<br>'; |
||
| 215 | $out .= '<select name="device" onchange="set_device(this)">'; |
||
| 216 | $iterator = 0; |
||
| 217 | foreach ($thedevices as $oneDevice) { |
||
| 218 | View Code Duplication | if ((isset($oneDevice['options']) && isset($oneDevice['options']['hidden']) && $oneDevice['options']['hidden']) || $oneDevice['status']) { |
|
| 219 | continue; |
||
| 220 | } |
||
| 221 | if (!$deviceName) { |
||
| 222 | $deviceName = $oneDevice['id']; |
||
| 223 | } |
||
| 224 | $disp = $oneDevice['display']; |
||
| 225 | if ($oneDevice['id'] === '0') { |
||
| 226 | $profileRedirect = 1; |
||
| 227 | $redirectTarget = $oneDevice['redirect']; |
||
| 228 | } |
||
| 229 | $out .= '<option value="' . $oneDevice['id'] . '"'; |
||
| 230 | if ($oneDevice['id'] == $deviceName) { |
||
| 231 | $out .= ' selected'; |
||
| 232 | $selectedOs = 1; |
||
| 233 | if ($oneDevice['redirect']) { |
||
| 234 | $redirectTarget = $oneDevice['redirect']; |
||
| 235 | } |
||
| 236 | } |
||
| 237 | $out .= '>' . $disp . '</option>'; |
||
| 238 | $deviceRedirects .= 'redirects[' . $iterator . '] = ' . ( $oneDevice['redirect'] ? 1 : 0 ) . ';'; |
||
| 239 | $iterator++; |
||
| 240 | } |
||
| 241 | $out .= '</select>'; |
||
| 242 | if ($selectedOs == 0) { |
||
| 243 | $message = $unsupportedMessage; |
||
| 244 | } |
||
| 245 | $out = $message . $out; |
||
| 246 | if ($profileRedirect) { |
||
| 247 | $out = ''; |
||
| 248 | } |
||
| 249 | if ($redirectTarget) { |
||
| 250 | $deviceRedirects .= 'is_redirected = 1;'; |
||
| 251 | $out .= _("Your local administrator has specified a redirect to a local support page.") . '<br>' . _("When you click <b>CONTINUE</b> this support page will be opened."); |
||
| 252 | $action = 'window.location.href=\'' . $redirectTarget . '\'; return(false);'; |
||
| 253 | $out .= "<p><button id='devices' name='devices' style='width:100%;' onclick=\"" . $action . '">' . _("CONTINUE to local support page") . "</button>"; |
||
| 254 | } else { |
||
| 255 | $deviceRedirects .= 'is_redirected = 0;'; |
||
| 256 | $action = 'submit_form(this)'; |
||
| 257 | $out .= "<p><button id='devices' name='devices' style='width:100%;' onclick=\"" . $action . '">' . sprintf(_("Do you have an account at this %s?"), $this->nomenclature_inst) . '<br>' . _("If so and if the other settings above are OK then click here to download...") . "</button>"; |
||
| 258 | } |
||
| 259 | $out .= '<script type="text/javascript">' . $deviceRedirects . '</script>'; |
||
| 260 | return $out; |
||
| 261 | } |
||
| 262 | |||
| 263 | public function displayDeviceDownload() { |
||
| 338 | } |
||
| 339 | |||
| 340 | public function langSelection() { |
||
| 341 | $out = _("View this page in") . " "; |
||
| 342 | $out .= '<select onchange="submit_form(this)" name="lang">'; |
||
| 343 | foreach (CONFIG['LANGUAGES'] as $lang => $value) { |
||
| 344 | $out .= '<option value="' . $lang . '"'; |
||
| 345 | if ($lang === $this->languageInstance->getLang()) { |
||
| 346 | $out .= ' selected'; |
||
| 347 | } |
||
| 348 | $out .= '>' . $value['display'] . '</option>'; |
||
| 349 | } |
||
| 350 | $out .= '</select>'; |
||
| 351 | return $out; |
||
| 352 | } |
||
| 353 | |||
| 354 | /** |
||
| 355 | * displays the navigation bar showing the current location of the page |
||
| 356 | */ |
||
| 357 | public function yourChoice() { |
||
| 358 | $out = ''; |
||
| 359 | $capitalisedCountry = strtoupper($this->country->identifier); |
||
| 360 | $name = isset($this->knownFederations[$capitalisedCountry]) ? $this->knownFederations[$capitalisedCountry] : $capitalisedCountry; |
||
| 361 | $name = preg_replace('/ +/', ' ', $name); |
||
| 362 | $out .= "$name; "; |
||
| 363 | $name = $this->idp->name; |
||
| 364 | $name = preg_replace('/ +/', ' ', $name); |
||
| 365 | $out .= "$name"; |
||
| 366 | if ($this->profileCount > 1) { |
||
| 367 | $name = '; ' . $this->profile->name; |
||
| 368 | $name = preg_replace('/ +/', ' ', $name); |
||
| 369 | $out .= "$name"; |
||
| 370 | } |
||
| 371 | return $out; |
||
| 372 | } |
||
| 373 | |||
| 374 | /** |
||
| 375 | * generates a hidden input field with the given argName |
||
| 376 | * |
||
| 377 | * @param string $argName name of the hidden input field |
||
| 378 | * @return string |
||
| 379 | */ |
||
| 380 | public function passArgument($argName) { |
||
| 382 | } |
||
| 383 | |||
| 384 | public $country; |
||
| 385 | public $idp; |
||
| 386 | public $profile; |
||
| 387 | public $args; |
||
| 388 | public $profileCount; |
||
| 389 | public $page; |
||
| 390 | |||
| 475 |