| Total Complexity | 61 |
| Total Lines | 525 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like UIElements 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 UIElements, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 32 | class UIElements extends \core\common\Entity { |
||
| 33 | |||
| 34 | /** |
||
| 35 | * the custom displayable variant of the term 'federation' |
||
| 36 | * |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | public $nomenclatureFed; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * the custom displayable variant of the term 'institution' |
||
| 43 | * |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | public $nomenclatureInst; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * the custom displayable variant of the term 'hotspot' |
||
| 50 | * |
||
| 51 | * @var string |
||
| 52 | */ |
||
| 53 | public $nomenclatureHotspot; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * the custom displayable variant of the term 'hotspot' |
||
| 57 | * |
||
| 58 | * @var string |
||
| 59 | */ |
||
| 60 | public $nomenclatureParticipant; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Initialises the class. |
||
| 64 | * |
||
| 65 | * Mainly fetches various nomenclature from the config and attempts to translate those into local language. Needs pre-loading some terms. |
||
| 66 | */ |
||
| 67 | public function __construct() { |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * provides human-readable text for the various option names as stored in DB. |
||
| 79 | * |
||
| 80 | * @param string $input raw text in need of a human-readable display variant |
||
| 81 | * @return string the human-readable variant |
||
| 82 | * @throws \Exception |
||
| 83 | */ |
||
| 84 | public function displayName($input) { |
||
| 85 | \core\common\Entity::intoThePotatoes(); |
||
| 86 | $ssidText = _("SSID"); |
||
| 87 | $ssidLegacyText = _("SSID (with WPA/TKIP)"); |
||
| 88 | $passpointOiText = _("HS20 Consortium OI"); |
||
| 89 | |||
| 90 | if (count(\config\ConfAssistant::CONFIG['CONSORTIUM']['ssid']) > 0) { |
||
| 91 | $ssidText = _("Additional SSID"); |
||
| 92 | $ssidLegacyText = _("Additional SSID (with WPA/TKIP)"); |
||
| 93 | } |
||
| 94 | if (!empty(\config\ConfAssistant::CONFIG['CONSORTIUM']['interworking-consortium-oi']) && count(\config\ConfAssistant::CONFIG['CONSORTIUM']['interworking-consortium-oi']) > 0) { |
||
| 95 | $passpointOiText = _("Additional HS20 Consortium OI"); |
||
| 96 | } |
||
| 97 | |||
| 98 | $displayNames = [_("Support: Web") => "support:url", |
||
| 99 | _("Support: EAP Types") => "support:eap_types", |
||
| 100 | _("Support: Phone") => "support:phone", |
||
| 101 | _("Support: E-Mail") => "support:email", |
||
| 102 | sprintf(_("%s Name"), $this->nomenclatureParticipant) => "general:instname", |
||
| 103 | _("Location") => "general:geo_coordinates", |
||
| 104 | _("Logo URL") => "general:logo_url", |
||
| 105 | _("Logo image") => "general:logo_file", |
||
| 106 | _("Configure Wired Ethernet") => "media:wired", |
||
| 107 | _("Name (CN) of Authentication Server") => "eap:server_name", |
||
| 108 | _("Enable device assessment") => "eap:enable_nea", |
||
| 109 | _("Terms of Use") => "support:info_file", |
||
| 110 | _("CA Certificate URL") => "eap:ca_url", |
||
| 111 | _("CA Certificate File") => "eap:ca_file", |
||
| 112 | _("Profile Display Name") => "profile:name", |
||
| 113 | _("Production-Ready") => "profile:production", |
||
| 114 | _("Admin Accepted Terms of Use") => 'hiddenprofile:tou_accepted', |
||
| 115 | _("Extra text on downloadpage for device") => "device-specific:customtext", |
||
| 116 | _("Redirection Target") => "device-specific:redirect", |
||
| 117 | _("Extra text on downloadpage for EAP method") => "eap-specific:customtext", |
||
| 118 | _("Turn on selection of EAP-TLS User-Name") => "eap-specific:tls_use_other_id", |
||
| 119 | _("Use GEANTlink TTLS supplicant for W8") => "device-specific:geantlink", |
||
| 120 | _("Use builtin TTLS supplicant for Windows 10") => "device-specific:builtin_ttls", |
||
| 121 | _("Profile Description") => "profile:description", |
||
| 122 | _("Custom Installer Name Suffix") => "profile:customsuffix", |
||
| 123 | sprintf(_("%s Administrator"), $this->nomenclatureFed) => "user:fedadmin", |
||
| 124 | _("Real Name") => "user:realname", |
||
| 125 | _("E-Mail Address") => "user:email", |
||
| 126 | _("Remove/Disable SSID") => "media:remove_SSID", |
||
| 127 | _("Mandatory Content Filtering Proxy") => "media:force_proxy", |
||
| 128 | _("Custom CSS file for User Area") => "fed:css_file", |
||
| 129 | sprintf(_("%s Logo"), $this->nomenclatureFed) => "fed:logo_file", |
||
| 130 | _("Preferred Skin for User Area") => "fed:desired_skin", |
||
| 131 | sprintf(_("Include %s branding in installers"), $this->nomenclatureFed) => "fed:include_logo_installers", |
||
| 132 | sprintf(_("%s Name"), $this->nomenclatureFed) => "fed:realname", |
||
| 133 | sprintf(_("%s Homepage"), $this->nomenclatureFed) => "fed:url", |
||
| 134 | sprintf(_("Custom text in %s Invitations"), $this->nomenclatureInst) => "fed:custominvite", |
||
| 135 | sprintf(_("Enable %s"), \core\ProfileSilverbullet::PRODUCTNAME) => "fed:silverbullet", |
||
| 136 | sprintf(_("%s: Do not terminate EAP"), \core\ProfileSilverbullet::PRODUCTNAME) => "fed:silverbullet-noterm", |
||
| 137 | sprintf(_("%s: max users per profile"), \core\ProfileSilverbullet::PRODUCTNAME) => "fed:silverbullet-maxusers", |
||
| 138 | sprintf(_("Mint %s with CA on creation"), $this->nomenclatureInst) => "fed:minted_ca_file", |
||
| 139 | $ssidText => "media:SSID", |
||
| 140 | $ssidLegacyText => "media:SSID_with_legacy", |
||
| 141 | $passpointOiText => "media:consortium_OI", |
||
| 142 | _("VLAN for own users") => "managedsp:vlan", |
||
| 143 | _("Realm to be considered own users") => "managedsp:realmforvlan", |
||
| 144 | _("Custom Operator-Name attribute") => "managedsp:operatorname", |
||
| 145 | ]; |
||
| 146 | |||
| 147 | $find = array_keys($displayNames, $input, TRUE); |
||
| 148 | |||
| 149 | if (count($find) == 0) { // this is an error! throw an Exception |
||
| 150 | throw new \Exception("The translation of an option name was requested, but the option is not known to the system: " . htmlentities($input)); |
||
| 151 | } |
||
| 152 | \core\common\Entity::outOfThePotatoes(); |
||
| 153 | return $find[0]; |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * creates an HTML information block with a list of options from a given category and level |
||
| 158 | * @param array $optionlist list of options |
||
| 159 | * @param string $class option class of interest |
||
| 160 | * @param string $level option level of interest |
||
| 161 | * @return string HTML code |
||
| 162 | */ |
||
| 163 | public function infoblock(array $optionlist, string $class, string $level) { |
||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * creates HTML code to display all information boxes for an IdP |
||
| 235 | * |
||
| 236 | * @param \core\IdP $myInst the IdP in question |
||
| 237 | * @return string HTML code |
||
| 238 | */ |
||
| 239 | public function instLevelInfoBoxes(\core\IdP $myInst) { |
||
| 240 | \core\common\Entity::intoThePotatoes(); |
||
| 241 | $idpoptions = $myInst->getAttributes(); |
||
| 242 | $retval = "<div class='infobox'> |
||
| 243 | <h2>" . sprintf(_("General %s details"), $this->nomenclatureInst) . "</h2> |
||
| 244 | <table> |
||
| 245 | <tr> |
||
| 246 | <td> |
||
| 247 | " . _("Country:") . " |
||
| 248 | </td> |
||
| 249 | <td> |
||
| 250 | </td> |
||
| 251 | <td> |
||
| 252 | <strong>"; |
||
| 253 | $myFed = new \core\Federation($myInst->federation); |
||
| 254 | $retval .= $myFed->name; |
||
| 255 | $retval .= "</strong> |
||
| 256 | </td> |
||
| 257 | </tr>" . $this->infoblock($idpoptions, "general", "IdP") . " |
||
| 258 | </table> |
||
| 259 | </div>"; |
||
| 260 | |||
| 261 | $blocks = [["support", _("Global Helpdesk Details")], ["media", _("Media Properties")]]; |
||
| 262 | foreach ($blocks as $block) { |
||
| 263 | $retval .= "<div class='infobox'> |
||
| 264 | <h2>" . $block[1] . "</h2> |
||
| 265 | <table>" . |
||
| 266 | $this->infoblock($idpoptions, $block[0], "IdP") . |
||
| 267 | "</table> |
||
| 268 | </div>"; |
||
| 269 | } |
||
| 270 | \core\common\Entity::outOfThePotatoes(); |
||
| 271 | return $retval; |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * pretty-prints a file size number in SI "bi" units |
||
| 276 | * @param int $number the size of the file |
||
| 277 | * @return string the pretty-print representation of the file size |
||
| 278 | */ |
||
| 279 | private function displaySize(int $number) { |
||
| 287 | } |
||
| 288 | |||
| 289 | /** |
||
| 290 | * |
||
| 291 | * @param string $table the database table |
||
| 292 | * @param integer $rowindex the database row |
||
| 293 | * @param boolean $checkpublic should we check if the requested piece of data is public? |
||
| 294 | * @return string|boolean the requested data, or FALSE if something went wrong |
||
| 295 | */ |
||
| 296 | public static function getBlobFromDB($table, $rowindex, $checkpublic) { |
||
| 297 | $validator = new \web\lib\common\InputValidation(); |
||
|
|
|||
| 298 | // the data is either public (just give it away) or not; in this case, only |
||
| 299 | // release if the data belongs to admin himself |
||
| 300 | if ($checkpublic) { |
||
| 301 | |||
| 302 | $owners = \core\EntityWithDBProperties::isDataRestricted($table, $rowindex); |
||
| 303 | |||
| 304 | $ownersCondensed = []; |
||
| 305 | |||
| 306 | if ($owners !== FALSE) { // restricted data, see if we're authenticated and owners of the data |
||
| 307 | $auth = new \web\lib\admin\Authentication(); |
||
| 308 | if (!$auth->isAuthenticated()) { |
||
| 309 | return FALSE; // admin-only, but we are not an admin |
||
| 310 | } |
||
| 311 | // we might be called without session context (filepreview) so get the |
||
| 312 | // context if needed |
||
| 313 | \core\CAT::sessionStart(); |
||
| 314 | |||
| 315 | foreach ($owners as $oneowner) { |
||
| 316 | $ownersCondensed[] = $oneowner['ID']; |
||
| 317 | } |
||
| 318 | if (array_search($_SESSION['user'], $ownersCondensed) === FALSE) { |
||
| 319 | return FALSE; // wrong guy |
||
| 320 | } |
||
| 321 | // carry on and get the data |
||
| 322 | } |
||
| 323 | } |
||
| 324 | |||
| 325 | $blob = \core\EntityWithDBProperties::fetchRawDataByIndex($table, $rowindex); |
||
| 326 | return $blob; // this means we might return FALSE here if something was wrong with the original requested reference |
||
| 327 | } |
||
| 328 | |||
| 329 | /** |
||
| 330 | * creates HTML code to display a nice UI representation of a CA |
||
| 331 | * |
||
| 332 | * @param string $cAReference ROWID pointer to the CA to display |
||
| 333 | * @return string HTML code |
||
| 334 | */ |
||
| 335 | public function previewCAinHTML($cAReference) { |
||
| 336 | \core\common\Entity::intoThePotatoes(); |
||
| 337 | $validator = new \web\lib\common\InputValidation(); |
||
| 338 | $ref = $validator->databaseReference($cAReference); |
||
| 339 | $rawResult = UIElements::getBlobFromDB($ref['table'], $ref['rowindex'], FALSE); |
||
| 340 | if (is_bool($rawResult)) { // we didn't actually get a CA! |
||
| 341 | $retval = "<div class='ca-summary'>" . _("There was an error while retrieving the certificate from the database!") . "</div>"; |
||
| 342 | \core\common\Entity::outOfThePotatoes(); |
||
| 343 | return $retval; |
||
| 344 | } |
||
| 345 | $cAblob = base64_decode($rawResult); |
||
| 346 | |||
| 347 | $func = new \core\common\X509; |
||
| 348 | $details = $func->processCertificate($cAblob); |
||
| 349 | if ($details === FALSE) { |
||
| 350 | $retval = _("There was an error processing the certificate!"); |
||
| 351 | \core\common\Entity::outOfThePotatoes(); |
||
| 352 | return $retval; |
||
| 353 | } |
||
| 354 | |||
| 355 | $details['name'] = preg_replace('/(.)\/(.)/', "$1<br/>$2", $details['name']); |
||
| 356 | $details['name'] = preg_replace('/\//', "", $details['name']); |
||
| 357 | $certstatus = ( $details['root'] == 1 ? "R" : "I"); |
||
| 358 | if ($details['ca'] == 0 && $details['root'] != 1) { |
||
| 359 | $retval = "<div class='ca-summary' style='background-color:red'><div style='position:absolute; right: 0px; width:20px; height:20px; background-color:maroon; border-radius:10px; text-align: center;'><div style='padding-top:3px; font-weight:bold; color:#ffffff;'>S</div></div>" . _("This is a <strong>SERVER</strong> certificate!") . "<br/>" . $details['name'] . "</div>"; |
||
| 360 | \core\common\Entity::outOfThePotatoes(); |
||
| 361 | return $retval; |
||
| 362 | } |
||
| 363 | $retval = "<div class='ca-summary' ><div style='position:absolute; right: 0px; width:20px; height:20px; background-color:#0000ff; border-radius:10px; text-align: center;'><div style='padding-top:3px; font-weight:bold; color:#ffffff;'>$certstatus</div></div>" . $details['name'] . "</div>"; |
||
| 364 | \core\common\Entity::outOfThePotatoes(); |
||
| 365 | return $retval; |
||
| 366 | } |
||
| 367 | |||
| 368 | /** |
||
| 369 | * creates HTML code to display a nice UI representation of an image |
||
| 370 | * |
||
| 371 | * @param string $imageReference ROWID pointer to the image to display |
||
| 372 | * @return string HTML code |
||
| 373 | */ |
||
| 374 | public function previewImageinHTML($imageReference) { |
||
| 375 | \core\common\Entity::intoThePotatoes(); |
||
| 376 | $retval = "<img style='max-width:150px' src='inc/filepreview.php?id=" . $imageReference . "' alt='" . _("Preview of logo file") . "'/>"; |
||
| 377 | \core\common\Entity::outOfThePotatoes(); |
||
| 378 | return $retval; |
||
| 379 | } |
||
| 380 | |||
| 381 | /** |
||
| 382 | * creates HTML code to display a nice UI representation of a TermsOfUse file |
||
| 383 | * |
||
| 384 | * @param string $fileReference ROWID pointer to the file to display |
||
| 385 | * @return string HTML code |
||
| 386 | */ |
||
| 387 | public function previewInfoFileinHTML($fileReference) { |
||
| 388 | \core\common\Entity::intoThePotatoes(); |
||
| 389 | $validator = new \web\lib\common\InputValidation(); |
||
| 390 | $ref = $validator->databaseReference($fileReference); |
||
| 391 | $fileBlob = UIElements::getBlobFromDB($ref['table'], $ref['rowindex'], FALSE); |
||
| 392 | if (is_bool($fileBlob)) { // we didn't actually get a file! |
||
| 393 | $retval = "<div class='ca-summary'>" . _("There was an error while retrieving the file from the database!") . "</div>"; |
||
| 394 | \core\common\Entity::outOfThePotatoes(); |
||
| 395 | return $retval; |
||
| 396 | } |
||
| 397 | $decodedFileBlob = base64_decode($fileBlob); |
||
| 398 | $fileinfo = new \finfo(); |
||
| 399 | $retval = "<div class='ca-summary'>" . _("File exists") . " (" . $fileinfo->buffer($decodedFileBlob, FILEINFO_MIME_TYPE) . ", " . $this->displaySize(strlen($decodedFileBlob)) . ")<br/><a href='inc/filepreview.php?id=$fileReference'>" . _("Preview") . "</a></div>"; |
||
| 400 | \core\common\Entity::outOfThePotatoes(); |
||
| 401 | return $retval; |
||
| 402 | } |
||
| 403 | |||
| 404 | /** |
||
| 405 | * creates HTML code for a UI element which informs the user about something. |
||
| 406 | * |
||
| 407 | * @param int $level what kind of information is to be displayed? |
||
| 408 | * @param string $text the text to display |
||
| 409 | * @param string $caption the caption to display |
||
| 410 | * @param bool $omittabletags the output usually has tr/td table tags, this option suppresses them |
||
| 411 | * @return string |
||
| 412 | */ |
||
| 413 | public function boxFlexible(int $level, string $text = NULL, string $caption = NULL, bool $omittabletags = FALSE) { |
||
| 414 | \core\common\Entity::intoThePotatoes(); |
||
| 415 | $uiMessages = [ |
||
| 416 | \core\common\Entity::L_OK => ['icon' => '../resources/images/icons/Quetto/check-icon.png', 'text' => _("OK")], |
||
| 417 | \core\common\Entity::L_REMARK => ['icon' => '../resources/images/icons/Quetto/info-icon.png', 'text' => _("Remark")], |
||
| 418 | \core\common\Entity::L_WARN => ['icon' => '../resources/images/icons/Quetto/danger-icon.png', 'text' => _("Warning!")], |
||
| 419 | \core\common\Entity::L_ERROR => ['icon' => '../resources/images/icons/Quetto/no-icon.png', 'text' => _("Error!")], |
||
| 420 | ]; |
||
| 421 | |||
| 422 | $retval = ""; |
||
| 423 | if (!$omittabletags) { |
||
| 424 | $retval .= "<tr><td>"; |
||
| 425 | } |
||
| 426 | $finalCaption = ($caption !== NULL ? $caption : $uiMessages[$level]['text']); |
||
| 427 | $retval .= "<img class='icon' src='" . $uiMessages[$level]['icon'] . "' alt='" . $finalCaption . "' title='" . $finalCaption . "'/>"; |
||
| 428 | if (!$omittabletags) { |
||
| 429 | $retval .= "</td><td>"; |
||
| 430 | } |
||
| 431 | if ($text !== NULL) { |
||
| 432 | $retval .= $text; |
||
| 433 | } |
||
| 434 | if (!$omittabletags) { |
||
| 435 | $retval .= "</td></tr>"; |
||
| 436 | } |
||
| 437 | \core\common\Entity::outOfThePotatoes(); |
||
| 438 | return $retval; |
||
| 439 | } |
||
| 440 | |||
| 441 | /** |
||
| 442 | * creates HTML code to display an "all is okay" message |
||
| 443 | * |
||
| 444 | * @param string $text the text to display |
||
| 445 | * @param string $caption the caption to display |
||
| 446 | * @param bool $omittabletags the output usually has tr/td table tags, this option suppresses them |
||
| 447 | * @return string HTML: the box |
||
| 448 | */ |
||
| 449 | public function boxOkay(string $text = NULL, string $caption = NULL, bool $omittabletags = FALSE) { |
||
| 450 | return $this->boxFlexible(\core\common\Entity::L_OK, $text, $caption, $omittabletags); |
||
| 451 | } |
||
| 452 | |||
| 453 | /** |
||
| 454 | * creates HTML code to display a "smartass comment" message |
||
| 455 | * |
||
| 456 | * @param string $text the text to display |
||
| 457 | * @param string $caption the caption to display |
||
| 458 | * @param bool $omittabletags the output usually has tr/td table tags, this option suppresses them |
||
| 459 | * @return string HTML: the box |
||
| 460 | */ |
||
| 461 | public function boxRemark(string $text = NULL, string $caption = NULL, bool $omittabletags = FALSE) { |
||
| 462 | return $this->boxFlexible(\core\common\Entity::L_REMARK, $text, $caption, $omittabletags); |
||
| 463 | } |
||
| 464 | |||
| 465 | /** |
||
| 466 | * creates HTML code to display a "something's a bit wrong" message |
||
| 467 | * |
||
| 468 | * @param string $text the text to display |
||
| 469 | * @param string $caption the caption to display |
||
| 470 | * @param bool $omittabletags the output usually has tr/td table tags, this option suppresses them |
||
| 471 | * @return string HTML: the box |
||
| 472 | */ |
||
| 473 | public function boxWarning(string $text = NULL, string $caption = NULL, bool $omittabletags = FALSE) { |
||
| 474 | return $this->boxFlexible(\core\common\Entity::L_WARN, $text, $caption, $omittabletags); |
||
| 475 | } |
||
| 476 | |||
| 477 | /** |
||
| 478 | * creates HTML code to display a "Whoa! Danger, Will Robinson!" message |
||
| 479 | * |
||
| 480 | * @param string $text the text to display |
||
| 481 | * @param string $caption the caption to display |
||
| 482 | * @param bool $omittabletags the output usually has tr/td table tags, this option suppresses them |
||
| 483 | * @return string HTML: the box |
||
| 484 | */ |
||
| 485 | public function boxError(string $text = NULL, string $caption = NULL, bool $omittabletags = FALSE) { |
||
| 487 | } |
||
| 488 | |||
| 489 | const QRCODE_PIXELS_PER_SYMBOL = 12; |
||
| 490 | /** |
||
| 491 | * Injects the consortium logo in the middle of a given PNG. |
||
| 492 | * |
||
| 493 | * Usually used on QR code PNGs - the parameters inform about the structure of |
||
| 494 | * the QR code so that the logo does not prevent parsing of the QR code. |
||
| 495 | * |
||
| 496 | * @param string $inputpngstring the PNG to edit |
||
| 497 | * @param int $symbolsize size in pixels of one QR "pixel" |
||
| 498 | * @param int $marginsymbols size in pixels of border around the actual QR |
||
| 499 | * @return string the image with logo centered in the middle |
||
| 500 | */ |
||
| 501 | public function pngInjectConsortiumLogo(string $inputpngstring, int $symbolsize, int $marginsymbols = 4) { |
||
| 541 | } |
||
| 542 | |||
| 543 | /** |
||
| 544 | * Something went wrong. We display the error cause and then throw an Exception. |
||
| 545 | * |
||
| 546 | * @param string $headerDisplay error to put in the page header |
||
| 547 | * @param string $uiDisplay error string to display |
||
| 548 | * @return void direct output |
||
| 549 | * @throws Exception |
||
| 550 | */ |
||
| 551 | public function errorPage($headerDisplay, $uiDisplay) { |
||
| 557 | } |
||
| 558 | |||
| 559 | } |
||
| 560 |