| Total Complexity | 58 |
| Total Lines | 476 |
| 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 |
||
| 20 | class UIElements { |
||
| 21 | |||
| 22 | /** |
||
| 23 | * the custom displayable variant of the term 'federation' |
||
| 24 | * |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | public $nomenclature_fed; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * the custom displayable variant of the term 'institution' |
||
| 31 | * |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | public $nomenclature_inst; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Initialises the class. |
||
| 38 | * |
||
| 39 | * Mainly fetches various nomenclature from the config and attempts to translate those into local language. Needs pre-loading some terms. |
||
| 40 | */ |
||
| 41 | public function __construct() { |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * provides human-readable text for the various option names as stored in DB. |
||
| 60 | * |
||
| 61 | * @param string $input raw text in need of a human-readable display variant |
||
| 62 | * @return string the human-readable variant |
||
| 63 | * @throws Exception |
||
| 64 | */ |
||
| 65 | public function displayName($input) { |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * creates an HTML information block with a list of options from a given category and level |
||
| 133 | * @param array $optionlist list of options |
||
| 134 | * @param string $class option class of interest |
||
| 135 | * @param string $level option level of interest |
||
| 136 | * @return string HTML code |
||
| 137 | */ |
||
| 138 | public function infoblock(array $optionlist, string $class, string $level) { |
||
| 139 | $googleMarkers = []; |
||
| 140 | $retval = ""; |
||
| 141 | $optioninfo = \core\Options::instance(); |
||
| 142 | |||
| 143 | foreach ($optionlist as $option) { |
||
| 144 | $type = $optioninfo->optionType($option['name']); |
||
| 145 | if (preg_match('/^' . $class . '/', $option['name']) && $option['level'] == "$level") { |
||
| 146 | // all non-multilang attribs get this assignment ... |
||
| 147 | $language = ""; |
||
| 148 | $content = $option['value']; |
||
| 149 | // ... override them with multilang tags if needed |
||
| 150 | if ($type["flag"] == "ML") { |
||
| 151 | $language = _("default/other languages"); |
||
| 152 | if ($option['lang'] != 'C') { |
||
| 153 | $language = CONFIG['LANGUAGES'][$option['lang']]['display'] ?? "(unsupported language)"; |
||
| 154 | } |
||
| 155 | } |
||
| 156 | |||
| 157 | switch ($type["type"]) { |
||
| 158 | case "coordinates": |
||
| 159 | $coords = json_decode($option['value'], true); |
||
| 160 | $googleMarkers[] = $coords; |
||
| 161 | break; |
||
| 162 | case "file": |
||
| 163 | $retval .= "<tr><td>" . $this->displayName($option['name']) . "</td><td>$language</td><td>"; |
||
| 164 | switch ($option['name']) { |
||
| 165 | case "general:logo_file": |
||
| 166 | case "fed:logo_file": |
||
| 167 | $retval .= $this->previewImageinHTML('ROWID-' . $option['level'] . '-' . $option['row']); |
||
| 168 | break; |
||
| 169 | case "eap:ca_file": |
||
| 170 | // fall-through intended: display both the same way |
||
| 171 | case "fed:minted_ca_file": |
||
| 172 | $retval .= $this->previewCAinHTML('ROWID-' . $option['level'] . '-' . $option['row']); |
||
| 173 | break; |
||
| 174 | case "support:info_file": |
||
| 175 | $retval .= $this->previewInfoFileinHTML('ROWID-' . $option['level'] . '-' . $option['row']); |
||
| 176 | break; |
||
| 177 | default: |
||
| 178 | } |
||
| 179 | break; |
||
| 180 | case "boolean": |
||
| 181 | $retval .= "<tr><td>" . $this->displayName($option['name']) . "</td><td>$language</td><td><strong>" . ($content == "on" ? _("on") : _("off") ) . "</strong></td></tr>"; |
||
| 182 | break; |
||
| 183 | default: |
||
| 184 | $retval .= "<tr><td>" . $this->displayName($option['name']) . "</td><td>$language</td><td><strong>$content</strong></td></tr>"; |
||
| 185 | } |
||
| 186 | } |
||
| 187 | } |
||
| 188 | if (count($googleMarkers)) { |
||
| 189 | $marker = '<markers>'; |
||
| 190 | $locationCount = 0; |
||
| 191 | foreach ($googleMarkers as $g) { |
||
| 192 | $locationCount++; |
||
| 193 | $marker .= '<marker name="' . $locationCount . '" lat="' . $g['lat'] . '" lng="' . $g['lon'] . '" />'; |
||
| 194 | } |
||
| 195 | $marker .= '</markers>'; |
||
| 196 | $retval .= '<tr><td><script>markers=\'' . $marker . '\';</script></td><td></td><td></td></tr>'; |
||
| 197 | } |
||
| 198 | return $retval; |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * creates HTML code to display all information boxes for an IdP |
||
| 203 | * |
||
| 204 | * @param \core\IdP $myInst the IdP in question |
||
| 205 | * @return string HTML code |
||
| 206 | */ |
||
| 207 | public function instLevelInfoBoxes(\core\IdP $myInst) { |
||
| 208 | $idpoptions = $myInst->getAttributes(); |
||
| 209 | $retval = "<div class='infobox'> |
||
| 210 | <h2>" . sprintf(_("General %s details"), $this->nomenclature_inst) . "</h2> |
||
| 211 | <table> |
||
| 212 | <tr> |
||
| 213 | <td> |
||
| 214 | " . _("Country:") . " |
||
| 215 | </td> |
||
| 216 | <td> |
||
| 217 | </td> |
||
| 218 | <td> |
||
| 219 | <strong>"; |
||
| 220 | $myFed = new \core\Federation($myInst->federation); |
||
| 221 | $retval .= $myFed->name; |
||
| 222 | $retval .= "</strong> |
||
| 223 | </td> |
||
| 224 | </tr>" . $this->infoblock($idpoptions, "general", "IdP") . " |
||
| 225 | </table> |
||
| 226 | </div>"; |
||
| 227 | |||
| 228 | $blocks = [["support", _("Global Helpdesk Details")], ["media", _("Media Properties")]]; |
||
| 229 | foreach ($blocks as $block) { |
||
| 230 | $retval .= "<div class='infobox'> |
||
| 231 | <h2>" . $block[1] . "</h2> |
||
| 232 | <table>" . |
||
| 233 | $this->infoblock($idpoptions, $block[0], "IdP") . |
||
| 234 | "</table> |
||
| 235 | </div>"; |
||
| 236 | } |
||
| 237 | return $retval; |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * pretty-prints a file size number in SI "bi" units |
||
| 242 | * @param int $number the size of the file |
||
| 243 | * @return string the pretty-print representation of the file size |
||
| 244 | */ |
||
| 245 | private function displaySize(int $number) { |
||
| 246 | if ($number > 1024 * 1024) { |
||
| 247 | return round($number / 1024 / 1024, 2) . " MiB"; |
||
| 248 | } |
||
| 249 | if ($number > 1024) { |
||
| 250 | return round($number / 1024, 2) . " KiB"; |
||
| 251 | } |
||
| 252 | return $number . " B"; |
||
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * |
||
| 257 | * @param string $ref the database reference string |
||
| 258 | * @param boolean $checkpublic should we check if the requested piece of data is public? |
||
| 259 | * @return string|FALSE the requested data, or FALSE if something went wrong |
||
| 260 | */ |
||
| 261 | public static function getBlobFromDB($ref, $checkpublic) { |
||
| 262 | $validator = new \web\lib\common\InputValidation(); |
||
| 263 | $reference = $validator->databaseReference($ref); |
||
| 264 | |||
| 265 | if ($reference === FALSE) { |
||
|
1 ignored issue
–
show
|
|||
| 266 | return FALSE; |
||
| 267 | } |
||
| 268 | |||
| 269 | // the data is either public (just give it away) or not; in this case, only |
||
| 270 | // release if the data belongs to admin himself |
||
| 271 | if ($checkpublic) { |
||
| 272 | // we might be called without session context (filepreview) so get the |
||
| 273 | // context if needed |
||
| 274 | CAT_session_start(); |
||
| 275 | |||
| 276 | $owners = \core\EntityWithDBProperties::isDataRestricted($reference["table"], $reference["rowindex"]); |
||
| 277 | |||
| 278 | $ownersCondensed = []; |
||
| 279 | |||
| 280 | if ($owners !== FALSE) { // restricted datam see if we're authenticated and owners of the data |
||
| 281 | $auth = new \web\lib\admin\Authentication(); |
||
| 282 | if (!$auth->isAuthenticated()) { |
||
| 283 | return FALSE; // admin-only, but we are not an admin |
||
| 284 | } |
||
| 285 | foreach ($owners as $oneowner) { |
||
| 286 | $ownersCondensed[] = $oneowner['ID']; |
||
| 287 | } |
||
| 288 | if (array_search($_SESSION['user'], $ownersCondensed) === FALSE) { |
||
| 289 | return FALSE; // wrong guy |
||
| 290 | } |
||
| 291 | // carry on and get the data |
||
| 292 | } |
||
| 293 | } |
||
| 294 | |||
| 295 | $blob = \core\EntityWithDBProperties::fetchRawDataByIndex($reference["table"], $reference["rowindex"]); |
||
| 296 | return $blob; // this means we might return FALSE here if something was wrong with the original requested reference |
||
| 297 | } |
||
| 298 | |||
| 299 | /** |
||
| 300 | * |
||
| 301 | * @param string $reference a reference pointer to a database entry |
||
| 302 | * @throws Exception |
||
| 303 | */ |
||
| 304 | |||
| 305 | private function checkROWIDpresence($reference) { |
||
| 309 | } |
||
| 310 | } |
||
| 311 | |||
| 312 | /** |
||
| 313 | * creates HTML code to display a nice UI representation of a CA |
||
| 314 | * |
||
| 315 | * @param string $cAReference ROWID pointer to the CA to display |
||
| 316 | * @return string HTML code |
||
| 317 | */ |
||
| 318 | public function previewCAinHTML($cAReference) { |
||
| 319 | $this->checkROWIDpresence($cAReference); |
||
| 320 | $rawResult = UIElements::getBlobFromDB($cAReference, FALSE); |
||
| 321 | if ($rawResult === FALSE) { // we didn't actually get a CA! |
||
|
1 ignored issue
–
show
|
|||
| 322 | return "<div class='ca-summary'>"._("There was an error while retrieving the certificate from the database!")."</div>"; |
||
| 323 | } |
||
| 324 | $cAblob = base64_decode($rawResult); |
||
| 325 | |||
| 326 | $func = new \core\common\X509; |
||
| 327 | $details = $func->processCertificate($cAblob); |
||
| 328 | if ($details === FALSE) { |
||
| 329 | return _("There was an error processing the certificate!"); |
||
| 330 | } |
||
| 331 | |||
| 332 | $details['name'] = preg_replace('/(.)\/(.)/', "$1<br/>$2", $details['name']); |
||
| 333 | $details['name'] = preg_replace('/\//', "", $details['name']); |
||
| 334 | $certstatus = ( $details['root'] == 1 ? "R" : "I"); |
||
| 335 | if ($details['ca'] == 0 && $details['root'] != 1) { |
||
| 336 | return "<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>"; |
||
| 337 | } |
||
| 338 | return "<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>"; |
||
| 339 | } |
||
| 340 | |||
| 341 | /** |
||
| 342 | * creates HTML code to display a nice UI representation of an image |
||
| 343 | * |
||
| 344 | * @param string $imageReference ROWID pointer to the image to display |
||
| 345 | * @return string HTML code |
||
| 346 | */ |
||
| 347 | public function previewImageinHTML($imageReference) { |
||
| 348 | $this->checkROWIDpresence($imageReference); |
||
| 349 | return "<img style='max-width:150px' src='inc/filepreview.php?id=" . $imageReference . "' alt='" . _("Preview of logo file") . "'/>"; |
||
| 350 | } |
||
| 351 | |||
| 352 | /** |
||
| 353 | * creates HTML code to display a nice UI representation of a TermsOfUse file |
||
| 354 | * |
||
| 355 | * @param string $fileReference ROWID pointer to the file to display |
||
| 356 | * @return string HTML code |
||
| 357 | */ |
||
| 358 | public function previewInfoFileinHTML($fileReference) { |
||
| 359 | $this->checkROWIDpresence($fileReference); |
||
| 360 | $fileBlob = UIElements::getBlobFromDB($fileReference, FALSE); |
||
| 361 | if ($fileBlob === FALSE) { // we didn't actually get a file! |
||
|
1 ignored issue
–
show
|
|||
| 362 | return "<div class='ca-summary'>"._("There was an error while retrieving the file from the database!")."</div>"; |
||
| 363 | } |
||
| 364 | $decodedFileBlob = base64_decode($fileBlob); |
||
| 365 | $fileinfo = new \finfo(); |
||
| 366 | return "<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>"; |
||
| 367 | } |
||
| 368 | |||
| 369 | /** |
||
| 370 | * creates HTML code for a UI element which informs the user about something. |
||
| 371 | * |
||
| 372 | * @param int $level what kind of information is to be displayed? |
||
| 373 | * @param string $text the text to display |
||
| 374 | * @param string $caption the caption to display |
||
| 375 | * @param bool $omittabletags the output usually has tr/td table tags, this option suppresses them |
||
| 376 | * @return string |
||
| 377 | */ |
||
| 378 | public function boxFlexible(int $level, string $text = NULL, string $caption = NULL, bool $omittabletags = FALSE) { |
||
| 379 | |||
| 380 | $uiMessages = [ |
||
| 381 | \core\common\Entity::L_OK => ['icon' => '../resources/images/icons/Quetto/check-icon.png', 'text' => _("OK")], |
||
| 382 | \core\common\Entity::L_REMARK => ['icon' => '../resources/images/icons/Quetto/info-icon.png', 'text' => _("Remark")], |
||
| 383 | \core\common\Entity::L_WARN => ['icon' => '../resources/images/icons/Quetto/danger-icon.png', 'text' => _("Warning!")], |
||
| 384 | \core\common\Entity::L_ERROR => ['icon' => '../resources/images/icons/Quetto/no-icon.png', 'text' => _("Error!")], |
||
| 385 | ]; |
||
| 386 | |||
| 387 | $retval = ""; |
||
| 388 | if (!$omittabletags) { |
||
| 389 | $retval .= "<tr><td>"; |
||
| 390 | } |
||
| 391 | $finalCaption = ($caption !== NULL ? $caption : $uiMessages[$level]['text']); |
||
| 392 | $retval .= "<img class='icon' src='" . $uiMessages[$level]['icon'] . "' alt='" . $finalCaption . "' title='" . $finalCaption . "'/>"; |
||
| 393 | if (!$omittabletags) { |
||
|
1 ignored issue
–
show
|
|||
| 394 | $retval .= "</td><td>"; |
||
| 395 | } |
||
| 396 | if ($text !== NULL) { |
||
| 397 | $retval .= $text; |
||
| 398 | } |
||
| 399 | if (!$omittabletags) { |
||
|
1 ignored issue
–
show
|
|||
| 400 | $retval .= "</td></tr>"; |
||
| 401 | } |
||
| 402 | return $retval; |
||
| 403 | } |
||
| 404 | |||
| 405 | /** |
||
| 406 | * creates HTML code to display an "all is okay" message |
||
| 407 | * |
||
| 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 HTML: the box |
||
| 412 | */ |
||
| 413 | public function boxOkay(string $text = NULL, string $caption = NULL, bool $omittabletags = FALSE) { |
||
| 414 | return $this->boxFlexible(\core\common\Entity::L_OK, $text, $caption, $omittabletags); |
||
| 415 | } |
||
| 416 | |||
| 417 | /** |
||
| 418 | * creates HTML code to display a "smartass comment" message |
||
| 419 | * |
||
| 420 | * @param string $text the text to display |
||
| 421 | * @param string $caption the caption to display |
||
| 422 | * @param bool $omittabletags the output usually has tr/td table tags, this option suppresses them |
||
| 423 | * @return string HTML: the box |
||
| 424 | */ |
||
| 425 | public function boxRemark(string $text = NULL, string $caption = NULL, bool $omittabletags = FALSE) { |
||
| 426 | return $this->boxFlexible(\core\common\Entity::L_REMARK, $text, $caption, $omittabletags); |
||
| 427 | } |
||
| 428 | |||
| 429 | /** |
||
| 430 | * creates HTML code to display a "something's a bit wrong" message |
||
| 431 | * |
||
| 432 | * @param string $text the text to display |
||
| 433 | * @param string $caption the caption to display |
||
| 434 | * @param bool $omittabletags the output usually has tr/td table tags, this option suppresses them |
||
| 435 | * @return string HTML: the box |
||
| 436 | */ |
||
| 437 | public function boxWarning(string $text = NULL, string $caption = NULL, bool $omittabletags = FALSE) { |
||
| 438 | return $this->boxFlexible(\core\common\Entity::L_WARN, $text, $caption, $omittabletags); |
||
| 439 | } |
||
| 440 | |||
| 441 | /** |
||
| 442 | * creates HTML code to display a "Whoa! Danger, Will Robinson!" 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 boxError(string $text = NULL, string $caption = NULL, bool $omittabletags = FALSE) { |
||
| 451 | } |
||
| 452 | |||
| 453 | /** |
||
| 454 | * Injects the consortium logo in the middle of a given PNG. |
||
| 455 | * |
||
| 456 | * Usually used on QR code PNGs - the parameters inform about the structure of |
||
| 457 | * the QR code so that the logo does not prevent parsing of the QR code. |
||
| 458 | * |
||
| 459 | * @param string $inputpngstring the PNG to edit |
||
| 460 | * @param int $symbolsize size in pixels of one QR "pixel" |
||
| 461 | * @param int $marginsymbols size in pixels of border around the actual QR |
||
| 462 | * @return string the image with logo centered in the middle |
||
| 463 | */ |
||
| 464 | public function pngInjectConsortiumLogo(string $inputpngstring, int $symbolsize, int $marginsymbols = 4) { |
||
| 496 | } |
||
| 497 | |||
| 498 | } |
||
| 499 |