| Total Complexity | 85 |
| Total Lines | 580 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| Bugs | 0 | Features | 0 |
Complex classes like UserAPI 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 UserAPI, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 43 | class UserAPI extends CAT |
||
| 44 | { |
||
| 45 | |||
| 46 | /** |
||
| 47 | * nothing special to be done here. |
||
| 48 | */ |
||
| 49 | public function __construct() |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Prepare the device module environment and send back the link |
||
| 56 | * This method creates a device module instance via the {@link DeviceFactory} call, |
||
| 57 | * then sets up the device module environment for the specific profile by calling |
||
| 58 | * {@link DeviceConfig::setup()} method and finally, called the devide writeInstaller meethod |
||
| 59 | * passing the returned path name. |
||
| 60 | * |
||
| 61 | * @param string $device identifier as in {@link devices.php} |
||
| 62 | * @param int $profileId profile identifier |
||
| 63 | * @param string $generatedFor which download area does this pertain to |
||
| 64 | * @param string $token for silverbullet: invitation token to consume |
||
| 65 | * @param string $password for silverbull: import PIN for the future certificate |
||
| 66 | * |
||
| 67 | * @return array|NULL array with the following fields: |
||
| 68 | * profile - the profile identifier; |
||
| 69 | * device - the device identifier; |
||
| 70 | * link - the path name of the resulting installer |
||
| 71 | * mime - the mimetype of the installer |
||
| 72 | */ |
||
| 73 | public function generateInstaller($device, $profileId, $generatedFor = "user", $openRoaming = 0, $token = NULL, $password = NULL) |
||
| 74 | { |
||
| 75 | $this->loggerInstance->debug(4, "generateInstaller arguments:$device:$profileId:$openRoaming\n"); |
||
| 76 | $validator = new \web\lib\common\InputValidation(); |
||
| 77 | $profile = $validator->existingProfile($profileId); |
||
| 78 | // test if the profile is production-ready and if not if the authenticated user is an owner |
||
| 79 | if ($this->verifyDownloadAccess($profile) === FALSE) { |
||
| 80 | return; |
||
| 81 | } |
||
| 82 | $installerProperties = []; |
||
| 83 | $installerProperties['profile'] = $profileId; |
||
| 84 | $installerProperties['device'] = $device; |
||
| 85 | $cache = $this->getCache($device, $profile, $openRoaming); |
||
| 86 | $this->installerPath = $cache['path']; |
||
| 87 | if ($this->installerPath !== NULL && $token === NULL && $password === NULL) { |
||
| 88 | $this->loggerInstance->debug(4, "Using cached installer for: $device\n"); |
||
| 89 | $installerProperties['link'] = "user/API.php?action=downloadInstaller&lang=".$this->languageInstance->getLang()."&profile=$profileId&device=$device&generatedfor=$generatedFor&openroaming=$openRoaming"; |
||
| 90 | $installerProperties['mime'] = $cache['mime']; |
||
| 91 | } else { |
||
| 92 | $myInstaller = $this->generateNewInstaller($device, $profile, $generatedFor, $openRoaming, $token, $password); |
||
| 93 | if ($myInstaller['link'] !== 0) { |
||
| 94 | $installerProperties['mime'] = $myInstaller['mime']; |
||
| 95 | } |
||
| 96 | $installerProperties['link'] = $myInstaller['link']; |
||
| 97 | } |
||
| 98 | return $installerProperties; |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * checks whether the requested profile data is public, XOR was requested by |
||
| 103 | * its own admin. |
||
| 104 | * @param \core\AbstractProfile $profile the profile in question |
||
| 105 | * @return boolean |
||
| 106 | */ |
||
| 107 | private function verifyDownloadAccess($profile) |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * This function tries to find a cached copy of an installer for a given |
||
| 132 | * combination of Profile and device |
||
| 133 | * |
||
| 134 | * @param string $device the device for which the installer is searched in cache |
||
| 135 | * @param AbstractProfile $profile the profile for which the installer is searched in cache |
||
| 136 | * @return array containing path to the installer and mime type of the file, the path is set to NULL if no cache can be returned |
||
| 137 | */ |
||
| 138 | private function getCache($device, $profile, $openRoaming) |
||
| 139 | { |
||
| 140 | $deviceConfig = \devices\Devices::listDevices()[$device]; |
||
| 141 | $noCache = (isset(\devices\Devices::$Options['no_cache']) && \devices\Devices::$Options['no_cache']) ? 1 : 0; |
||
| 142 | if (isset($deviceConfig['options']['no_cache'])) { |
||
| 143 | $noCache = $deviceConfig['options']['no_cache'] ? 1 : 0; |
||
| 144 | } |
||
| 145 | if ($noCache) { |
||
| 146 | $this->loggerInstance->debug(5, "getCache: the no_cache option set for this device\n"); |
||
| 147 | return ['path' => NULL, 'mime' => NULL]; |
||
| 148 | } |
||
| 149 | $this->loggerInstance->debug(5, "getCache: caching option set for this device\n"); |
||
| 150 | $cache = $profile->testCache($device, $openRoaming); |
||
| 151 | $iPath = $cache['cache']; |
||
| 152 | if ($iPath && is_file($iPath)) { |
||
| 153 | return ['path' => $iPath, 'mime' => $cache['mime']]; |
||
| 154 | } |
||
| 155 | return ['path' => NULL, 'mime' => NULL]; |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Generates a new installer for the given combination of device and Profile |
||
| 160 | * |
||
| 161 | * @param string $device the device for which we want an installer |
||
| 162 | * @param AbstractProfile $profile the profile for which we want an installer |
||
| 163 | * @param string $generatedFor type of download requested (admin/user/silverbullet) |
||
| 164 | * @param int $openRoaming values 0 o 1 to indicate support for open roaming in the installer |
||
| 165 | * @param string $token in case of silverbullet, the token that was used to trigger the generation |
||
| 166 | * @param string $password in case of silverbullet, the import PIN for the future client certificate |
||
| 167 | * @return array info about the new installer (mime and link) |
||
| 168 | */ |
||
| 169 | private function generateNewInstaller($device, $profile, $generatedFor, $openRoaming, $token, $password) |
||
| 170 | { |
||
| 171 | $this->loggerInstance->debug(5, "generateNewInstaller() - Enter"); |
||
| 172 | $this->loggerInstance->debug(5, "generateNewInstaller:openRoaming:$openRoaming\n"); |
||
| 173 | $factory = new DeviceFactory($device); |
||
| 174 | $this->loggerInstance->debug(5, "generateNewInstaller() - created Device"); |
||
| 175 | $dev = $factory->device; |
||
| 176 | $out = []; |
||
| 177 | if (isset($dev)) { |
||
| 178 | $dev->setup($profile, $token, $password, $openRoaming); |
||
| 179 | $this->loggerInstance->debug(5, "generateNewInstaller() - Device setup done"); |
||
| 180 | $installer = $dev->writeInstaller(); |
||
| 181 | $this->loggerInstance->debug(5, "generateNewInstaller() - writeInstaller complete"); |
||
| 182 | $iPath = $dev->FPATH.'/tmp/'.$installer; |
||
| 183 | if ($iPath && is_file($iPath)) { |
||
| 184 | if (isset($dev->options['mime'])) { |
||
| 185 | $out['mime'] = $dev->options['mime']; |
||
| 186 | } else { |
||
| 187 | $info = new \finfo(); |
||
| 188 | $out['mime'] = $info->file($iPath, FILEINFO_MIME_TYPE); |
||
| 189 | } |
||
| 190 | $this->installerPath = $dev->FPATH.'/'.$installer; |
||
| 191 | rename($iPath, $this->installerPath); |
||
| 192 | $integerEap = (new \core\common\EAP($dev->selectedEap))->getIntegerRep(); |
||
| 193 | $profile->updateCache($device, $this->installerPath, $out['mime'], $integerEap, $openRoaming); |
||
| 194 | if (\config\Master::DEBUG_LEVEL < 4) { |
||
| 195 | \core\common\Entity::rrmdir($dev->FPATH.'/tmp'); |
||
| 196 | } |
||
| 197 | $this->loggerInstance->debug(4, "Generated installer: ".$this->installerPath.": for: $device, EAP:".$integerEap.", openRoaming: $openRoaming\n"); |
||
| 198 | $out['link'] = "user/API.php?action=downloadInstaller&lang=".$this->languageInstance->getLang()."&profile=".$profile->identifier."&device=$device&generatedfor=$generatedFor&openroaming=$openRoaming"; |
||
| 199 | } else { |
||
| 200 | $this->loggerInstance->debug(2, "Installer generation failed for: ".$profile->identifier.":$device:".$this->languageInstance->getLang()."openRoaming: $openRoaming\n"); |
||
| 201 | $out['link'] = 0; |
||
| 202 | } |
||
| 203 | } |
||
| 204 | return $out; |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * interface to Devices::listDevices() |
||
| 209 | * |
||
| 210 | * @param int $showHidden whether or not hidden devices should be shown |
||
| 211 | * @return array the list of devices |
||
| 212 | * @throws Exception |
||
| 213 | */ |
||
| 214 | public function listDevices($showHidden = 0) |
||
| 215 | { |
||
| 216 | $returnList = []; |
||
| 217 | $count = 0; |
||
| 218 | if ($showHidden !== 0 && $showHidden != 1) { |
||
| 219 | throw new Exception("show_hidden is only be allowed to be 0 or 1, but it is $showHidden!"); |
||
| 220 | } |
||
| 221 | foreach (\devices\Devices::listDevices() as $device => $deviceProperties) { |
||
| 222 | if (\core\common\Entity::getAttributeValue($deviceProperties, 'options', 'hidden') === 1 && $showHidden === 0) { |
||
| 223 | continue; |
||
| 224 | } |
||
| 225 | $count++; |
||
| 226 | $deviceProperties['device'] = $device; |
||
| 227 | $group = isset($deviceProperties['group']) ? $deviceProperties['group'] : 'other'; |
||
| 228 | if (!isset($returnList[$group])) { |
||
| 229 | $returnList[$group] = []; |
||
| 230 | } |
||
| 231 | $returnList[$group][$device] = $deviceProperties; |
||
| 232 | } |
||
| 233 | return $returnList; |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * |
||
| 238 | * @param string $device identifier of the device |
||
| 239 | * @param int $profileId identifier of the profile |
||
| 240 | * @return void |
||
| 241 | */ |
||
| 242 | public function deviceInfo($device, $profileId) |
||
| 254 | } |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Prepare the support data for a given profile |
||
| 258 | * |
||
| 259 | * @param int $profId profile identifier |
||
| 260 | * @return array |
||
| 261 | * array with the following fields: |
||
| 262 | * - local_email |
||
| 263 | * - local_phone |
||
| 264 | * - local_url |
||
| 265 | * - description |
||
| 266 | * - devices - an array of device names and their statuses (for a given profile) |
||
| 267 | * - last_changed |
||
| 268 | */ |
||
| 269 | public function profileAttributes($profId) |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Generate and send the installer |
||
| 300 | * |
||
| 301 | * @param string $device identifier as in {@link devices.php} |
||
| 302 | * @param int $prof_id profile identifier |
||
| 303 | * @param string $generated_for which download area does this pertain to |
||
| 304 | * @param string $token for silverbullet: invitation token to consume |
||
| 305 | * @param string $password for silverbull: import PIN for the future certificate |
||
| 306 | * @return string binary stream: installerFile |
||
| 307 | */ |
||
| 308 | public function downloadInstaller($device, $prof_id, $generated_for = 'user', $openRoaming = 0, $token = NULL, $password = NULL) |
||
| 334 | } |
||
| 335 | |||
| 336 | /** |
||
| 337 | * resizes image files |
||
| 338 | * |
||
| 339 | * @param string $inputImage the image we want to process |
||
| 340 | * @param string $destFile the output file for the processed image |
||
| 341 | * @param int $width if resizing, the target width |
||
| 342 | * @param int $height if resizing, the target height |
||
| 343 | * @param bool $resize shall we do resizing? width and height are ignored otherwise |
||
| 344 | * @return array |
||
| 345 | */ |
||
| 346 | private function processImage($inputImage, $destFile, $width, $height, $resize) |
||
| 364 | } |
||
| 365 | |||
| 366 | protected function logoExpireTime() |
||
| 367 | { |
||
| 368 | $offset = 60 * 60 * 24 * 30; |
||
| 369 | // gmdate cannot fail here - time() is its default argument (and integer), and we are adding an integer to it |
||
| 370 | return("Expires: "./** @scrutinizer ignore-type */ gmdate("D, d M Y H:i:s", time() + $offset)." GMT"); |
||
| 371 | } |
||
| 372 | /** |
||
| 373 | * Get and prepare logo file |
||
| 374 | * |
||
| 375 | * When called for DiscoJuice, first check if file cache exists |
||
| 376 | * If not then generate the file and save it in the cache |
||
| 377 | * @param int|string $identifier IdP or Federation identifier |
||
| 378 | * @param string $type either 'idp' or 'federation' is allowed |
||
| 379 | * @param integer $widthIn maximum width of the generated image - if 0 then it is treated as no upper bound |
||
| 380 | * @param integer $heightIn maximum height of the generated image - if 0 then it is treated as no upper bound |
||
| 381 | * @return array|null array with image information or NULL if there is no logo |
||
| 382 | * @throws Exception |
||
| 383 | */ |
||
| 384 | protected function getLogo($identifier, $type, $widthIn, $heightIn) |
||
| 385 | { |
||
| 386 | $expiresString = ''; |
||
| 387 | $attributeName = [ |
||
| 388 | 'federation' => "fed:logo_file", |
||
| 389 | 'federation_from_idp' => "fed:logo_file", |
||
| 390 | 'idp' => "general:logo_file", |
||
| 391 | ]; |
||
| 392 | |||
| 393 | $logoFile = ""; |
||
| 394 | $validator = new \web\lib\common\InputValidation(); |
||
| 395 | switch ($type) { |
||
| 396 | case "federation": |
||
| 397 | $entity = $validator->existingFederation($identifier); |
||
| 398 | break; |
||
| 399 | case "idp": |
||
| 400 | $entity = $validator->existingIdP($identifier); |
||
| 401 | break; |
||
| 402 | case "federation_from_idp": |
||
| 403 | $idp = $validator->existingIdP($identifier); |
||
| 404 | $entity = $validator->existingFederation($idp->federation); |
||
| 405 | break; |
||
| 406 | default: |
||
| 407 | throw new Exception("Unknown type of logo requested!"); |
||
| 408 | } |
||
| 409 | $filetype = 'image/png'; // default, only one code path where it can become different |
||
| 410 | list($width, $height, $resize) = $this->testForResize($widthIn, $heightIn); |
||
| 411 | if ($resize) { |
||
| 412 | $logoFile = ROOT.'/web/downloads/logos/'.$identifier.'_'.$width.'_'.$height.'.png'; |
||
| 413 | } |
||
| 414 | if (is_file($logoFile)) { // $logoFile could be an empty string but then we will get a FALSE |
||
| 415 | $this->loggerInstance->debug(4, "Using cached logo $logoFile for: $identifier\n"); |
||
| 416 | $blob = file_get_contents($logoFile); |
||
| 417 | } else { |
||
| 418 | $logoAttribute = $entity->getAttributes($attributeName[$type]); |
||
| 419 | if (count($logoAttribute) == 0) { |
||
| 420 | $blob = file_get_contents(ROOT . '/web/resources/images/empty.png'); |
||
| 421 | $expiresString = $this->logoExpireTime(); |
||
| 422 | } else { |
||
| 423 | $this->loggerInstance->debug(4, "RESIZE:$width:$height\n"); |
||
| 424 | $meta = $this->processImage($logoAttribute[0]['value'], $logoFile, $width, $height, $resize); |
||
| 425 | $filetype = $meta['filetype']; |
||
| 426 | $expiresString = $meta['expires']; |
||
| 427 | $blob = $meta['blob']; |
||
| 428 | } |
||
| 429 | } |
||
| 430 | return ["filetype" => $filetype, "expires" => $expiresString, "blob" => $blob]; |
||
| 431 | } |
||
| 432 | |||
| 433 | /** |
||
| 434 | * see if we have to resize an image |
||
| 435 | * |
||
| 436 | * @param integer $width the desired max width (0 = unbounded) |
||
| 437 | * @param integer $height the desired max height (0 = unbounded) |
||
| 438 | * @return array |
||
| 439 | */ |
||
| 440 | private function testForResize($width, $height) |
||
| 441 | { |
||
| 442 | if (is_numeric($width) && is_numeric($height) && ($width > 0 || $height > 0)) { |
||
| 443 | if ($height == 0) { |
||
| 444 | $height = 10000; |
||
| 445 | } |
||
| 446 | if ($width == 0) { |
||
| 447 | $width = 10000; |
||
| 448 | } |
||
| 449 | return [$width, $height, TRUE]; |
||
| 450 | } |
||
| 451 | return [0, 0, FALSE]; |
||
| 452 | } |
||
| 453 | |||
| 454 | /** |
||
| 455 | * find out where the device is currently located |
||
| 456 | * @return array |
||
| 457 | */ |
||
| 458 | public function locateDevice() |
||
| 459 | { |
||
| 460 | return \core\DeviceLocation::locateDevice(); |
||
| 461 | } |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Lists all identity providers in the database |
||
| 465 | * adding information required by DiscoJuice. |
||
| 466 | * |
||
| 467 | * @param int $activeOnly if set to non-zero will cause listing of only those institutions which have some valid profiles defined. |
||
| 468 | * @param string $country if set, only list IdPs in a specific country |
||
| 469 | * @return array the list of identity providers |
||
| 470 | * |
||
| 471 | */ |
||
| 472 | public function listAllIdentityProviders($activeOnly = 0, $country = "") |
||
| 473 | { |
||
| 474 | return IdPlist::listAllIdentityProviders($activeOnly, $country); |
||
| 475 | } |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Order active identity providers according to their distance and name |
||
| 479 | * @param string $country NRO to work with |
||
| 480 | * @param array $currentLocation current location |
||
| 481 | * |
||
| 482 | * @return array $IdPs - list of arrays ('id', 'name'); |
||
| 483 | */ |
||
| 484 | public function orderIdentityProviders($country, $currentLocation) |
||
| 485 | { |
||
| 486 | return IdPlist::orderIdentityProviders($country, $currentLocation); |
||
| 487 | } |
||
| 488 | |||
| 489 | /** |
||
| 490 | * outputs a full list of IdPs containing the fllowing data: |
||
| 491 | * institution_is, institution name in all available languages, |
||
| 492 | * list of production profiles. |
||
| 493 | * For eache profile the profile identifier, profile name in all languages |
||
| 494 | * and redirect values (empty rediret value means that no redirect has been |
||
| 495 | * set). |
||
| 496 | * |
||
| 497 | * @return array of identity providers with attributes |
||
| 498 | */ |
||
| 499 | public function listIdentityProvidersWithProfiles() { |
||
| 500 | return IdPlist::listIdentityProvidersWithProfiles(); |
||
| 501 | } |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Detect the best device driver form the browser |
||
| 505 | * Detects the operating system and returns its id |
||
| 506 | * display name and group membership (as in devices.php) |
||
| 507 | * @return array|boolean OS information, indexed by 'id', 'display', 'group' |
||
| 508 | */ |
||
| 509 | public function detectOS() |
||
| 532 | } |
||
| 533 | |||
| 534 | /** |
||
| 535 | * test if devise is defined and is not hidden. If all is fine return extracted information. |
||
| 536 | * |
||
| 537 | * @param string $devId device id as defined as index in Devices.php |
||
| 538 | * @param array $device device info as defined in Devices.php |
||
| 539 | * @return array|FALSE if the device has not been correctly specified |
||
| 540 | */ |
||
| 541 | private function returnDevice($devId, $device) |
||
| 553 | } |
||
| 554 | |||
| 555 | /** |
||
| 556 | * This methods cheks if the devide has been specified as the HTTP parameters |
||
| 557 | * |
||
| 558 | * @return device id|NULL if correcty specified or FALSE otherwise |
||
| 559 | */ |
||
| 560 | private function deviceFromRequest() |
||
| 561 | { |
||
| 562 | $devId = filter_input(INPUT_GET, 'device', FILTER_SANITIZE_STRING) ?? filter_input(INPUT_POST, 'device', FILTER_SANITIZE_STRING); |
||
| 563 | if ($devId === NULL || $devId === FALSE) { |
||
| 564 | $this->loggerInstance->debug(2, "Invalid device id provided\n"); |
||
| 565 | return NULL; |
||
| 566 | } |
||
| 567 | if (!isset(\devices\Devices::listDevices()[$devId])) { |
||
| 568 | $this->loggerInstance->debug(2, "Unrecognised system: $devId\n"); |
||
| 569 | return NULL; |
||
| 570 | } |
||
| 571 | return $devId; |
||
| 572 | } |
||
| 573 | |||
| 574 | /** |
||
| 575 | * finds all the user certificates that originated in a given token |
||
| 576 | * |
||
| 577 | * @param string $token the token for which we are fetching all associated user certs |
||
| 578 | * @return array|boolean returns FALSE if a token is invalid, otherwise array of certs |
||
| 579 | */ |
||
| 580 | public function getUserCerts($token) |
||
| 598 | } |
||
| 599 | |||
| 600 | /** |
||
| 601 | * device name |
||
| 602 | * |
||
| 603 | * @var string |
||
| 604 | */ |
||
| 605 | public $device; |
||
| 606 | |||
| 607 | /** |
||
| 608 | * path to installer |
||
| 609 | * |
||
| 610 | * @var string |
||
| 611 | */ |
||
| 612 | private $installerPath; |
||
| 613 | |||
| 614 | /** |
||
| 615 | * helper function to sort profiles by their name |
||
| 616 | * @param \core\AbstractProfile $profile1 the first profile's information |
||
| 617 | * @param \core\AbstractProfile $profile2 the second profile's information |
||
| 618 | * @return int |
||
| 619 | */ |
||
| 620 | private static function profileSort($profile1, $profile2) |
||
| 623 | } |
||
| 624 | } |