| Total Complexity | 78 |
| Total Lines | 482 |
| Duplicated Lines | 0 % |
| Changes | 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 |
||
| 32 | class UserAPI extends CAT { |
||
| 33 | |||
| 34 | /** |
||
| 35 | * nothing special to be done here. |
||
| 36 | */ |
||
| 37 | public function __construct() { |
||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Prepare the device module environment and send back the link |
||
| 43 | * This method creates a device module instance via the {@link DeviceFactory} call, |
||
| 44 | * then sets up the device module environment for the specific profile by calling |
||
| 45 | * {@link DeviceConfig::setup()} method and finally, called the devide writeInstaller meethod |
||
| 46 | * passing the returned path name. |
||
| 47 | * |
||
| 48 | * @param string $device identifier as in {@link devices.php} |
||
| 49 | * @param int $profileId profile identifier |
||
| 50 | * |
||
| 51 | * @return array |
||
| 52 | * array with the following fields: |
||
| 53 | * profile - the profile identifier; |
||
| 54 | * device - the device identifier; |
||
| 55 | * link - the path name of the resulting installer |
||
| 56 | * mime - the mimetype of the installer |
||
| 57 | */ |
||
| 58 | public function generateInstaller($device, $profileId, $generatedFor = "user", $token = NULL, $password = NULL) { |
||
| 59 | $this->languageInstance->setTextDomain("devices"); |
||
| 60 | $this->loggerInstance->debug(4, "installer:$device:$profileId\n"); |
||
| 61 | $validator = new \web\lib\common\InputValidation(); |
||
| 62 | $profile = $validator->Profile($profileId); |
||
| 63 | // test if the profile is production-ready and if not if the authenticated user is an owner |
||
| 64 | if ($this->verifyDownloadAccess($profile) === FALSE) { |
||
| 65 | return; |
||
| 66 | } |
||
| 67 | $installerProperties = []; |
||
| 68 | $installerProperties['profile'] = $profileId; |
||
| 69 | $installerProperties['device'] = $device; |
||
| 70 | $cache = $this->getCache($device, $profile); |
||
| 71 | $this->installerPath = $cache['path']; |
||
| 72 | if ($this->installerPath !== NULL && $token == NULL && $password == NULL) { |
||
| 73 | $this->loggerInstance->debug(4, "Using cached installer for: $device\n"); |
||
| 74 | $installerProperties['link'] = "API.php?action=downloadInstaller&lang=" . $this->languageInstance->getLang() . "&profile=$profileId&device=$device&generatedfor=$generatedFor"; |
||
| 75 | $installerProperties['mime'] = $cache['mime']; |
||
| 76 | } else { |
||
| 77 | $myInstaller = $this->generateNewInstaller($device, $profile, $generatedFor, $token, $password); |
||
| 78 | if ($myInstaller['link'] !== 0) { |
||
| 79 | $installerProperties['mime'] = $myInstaller['mime']; |
||
| 80 | } |
||
| 81 | $installerProperties['link'] = $myInstaller['link']; |
||
| 82 | } |
||
| 83 | $this->languageInstance->setTextDomain("web_user"); |
||
| 84 | return($installerProperties); |
||
| 85 | } |
||
| 86 | |||
| 87 | private function verifyDownloadAccess($profile) { |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * This function tries to find a cached copy of an installer for a given |
||
| 110 | * combination of Profile and device |
||
| 111 | * @param string $device |
||
| 112 | * @param AbstractProfile $profile |
||
| 113 | * @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 |
||
| 114 | */ |
||
| 115 | private function getCache($device, $profile) { |
||
| 116 | $deviceConfig = \devices\Devices::listDevices()[$device]; |
||
| 117 | $noCache = (isset(\devices\Devices::$Options['no_cache']) && \devices\Devices::$Options['no_cache']) ? 1 : 0; |
||
| 118 | if (isset($deviceConfig['options']['no_cache'])) { |
||
| 119 | $noCache = $deviceConfig['options']['no_cache'] ? 1 : 0; |
||
| 120 | } |
||
| 121 | if ($noCache) { |
||
| 122 | $this->loggerInstance->debug(5, "getCache: the no_cache option set for this device\n"); |
||
| 123 | return(['path' => NULL, 'mime' => NULL]); |
||
| 124 | } |
||
| 125 | $this->loggerInstance->debug(5, "getCache: caching option set for this device\n"); |
||
| 126 | $cache = $profile->testCache($device); |
||
| 127 | $iPath = $cache['cache']; |
||
| 128 | if ($iPath && is_file($iPath)) { |
||
| 129 | return(['path' => $iPath, 'mime' => $cache['mime']]); |
||
| 130 | } |
||
| 131 | return(['path' => NULL, 'mime' => NULL]); |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Generates a new installer for the given combination of device and Profile |
||
| 136 | * |
||
| 137 | * @param string $device |
||
| 138 | * @param AbstractProfile $profile |
||
| 139 | * @return array info about the new installer (mime and link) |
||
| 140 | */ |
||
| 141 | private function generateNewInstaller($device, $profile, $generatedFor, $token, $password) { |
||
| 142 | $this->loggerInstance->debug(5, "generateNewInstaller() - Enter"); |
||
| 143 | $factory = new DeviceFactory($device); |
||
| 144 | $this->loggerInstance->debug(5, "generateNewInstaller() - created Device"); |
||
| 145 | $dev = $factory->device; |
||
| 146 | $out = []; |
||
| 147 | if (isset($dev)) { |
||
| 148 | $dev->setup($profile, $token, $password); |
||
| 149 | $this->loggerInstance->debug(5, "generateNewInstaller() - Device setup done"); |
||
| 150 | $installer = $dev->writeInstaller(); |
||
| 151 | $this->loggerInstance->debug(5, "generateNewInstaller() - writeInstaller complete"); |
||
| 152 | $iPath = $dev->FPATH . '/tmp/' . $installer; |
||
| 153 | if ($iPath && is_file($iPath)) { |
||
| 154 | if (isset($dev->options['mime'])) { |
||
| 155 | $out['mime'] = $dev->options['mime']; |
||
| 156 | } else { |
||
| 157 | $info = new \finfo(); |
||
| 158 | $out['mime'] = $info->file($iPath, FILEINFO_MIME_TYPE); |
||
| 159 | } |
||
| 160 | $this->installerPath = $dev->FPATH . '/' . $installer; |
||
| 161 | rename($iPath, $this->installerPath); |
||
| 162 | $integerEap = (new \core\common\EAP($dev->selectedEap))->getIntegerRep(); |
||
| 163 | $profile->updateCache($device, $this->installerPath, $out['mime'], $integerEap); |
||
| 164 | if (CONFIG['DEBUG_LEVEL'] < 4) { |
||
| 165 | \core\common\Entity::rrmdir($dev->FPATH . '/tmp'); |
||
| 166 | } |
||
| 167 | $this->loggerInstance->debug(4, "Generated installer: " . $this->installerPath . ": for: $device, EAP:" . $integerEap . "\n"); |
||
| 168 | $out['link'] = "API.php?action=downloadInstaller&lang=" . $this->languageInstance->getLang() . "&profile=" . $profile->identifier . "&device=$device&generatedfor=$generatedFor"; |
||
| 169 | } else { |
||
| 170 | $this->loggerInstance->debug(2, "Installer generation failed for: " . $profile->identifier . ":$device:" . $this->languageInstance->getLang() . "\n"); |
||
| 171 | $out['link'] = 0; |
||
| 172 | } |
||
| 173 | } |
||
| 174 | return($out); |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * interface to Devices::listDevices() |
||
| 179 | */ |
||
| 180 | public function listDevices($showHidden = 0) { |
||
| 181 | $returnList = []; |
||
| 182 | $count = 0; |
||
| 183 | if ($showHidden !== 0 && $showHidden != 1) { |
||
| 184 | throw new Exception("show_hidden is only be allowed to be 0 or 1, but it is $showHidden!"); |
||
| 185 | } |
||
| 186 | foreach (\devices\Devices::listDevices() as $device => $deviceProperties) { |
||
| 187 | if (\core\common\Entity::getAttributeValue($deviceProperties, 'options', 'hidden') === 1 && $showHidden === 0) { |
||
| 188 | continue; |
||
| 189 | } |
||
| 190 | $count++; |
||
| 191 | $deviceProperties['device'] = $device; |
||
| 192 | $group = isset($deviceProperties['group']) ? $deviceProperties['group'] : 'other'; |
||
| 193 | if (!isset($returnList[$group])) { |
||
| 194 | $returnList[$group] = []; |
||
| 195 | } |
||
| 196 | $returnList[$group][$device] = $deviceProperties; |
||
| 197 | } |
||
| 198 | return $returnList; |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * |
||
| 203 | * @param string $device |
||
| 204 | * @param int $profileId |
||
| 205 | */ |
||
| 206 | public function deviceInfo($device, $profileId) { |
||
| 207 | $this->languageInstance->setTextDomain("devices"); |
||
| 208 | $validator = new \web\lib\common\InputValidation(); |
||
| 209 | $out = 0; |
||
| 210 | $profile = $validator->Profile($profileId); |
||
| 211 | $factory = new DeviceFactory($device); |
||
| 212 | $dev = $factory->device; |
||
| 213 | if (isset($dev)) { |
||
| 214 | $dev->setup($profile); |
||
| 215 | $out = $dev->writeDeviceInfo(); |
||
| 216 | } |
||
| 217 | $this->languageInstance->setTextDomain("web_user"); |
||
| 218 | echo $out; |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Prepare the support data for a given profile |
||
| 223 | * |
||
| 224 | * @param int $profId profile identifier |
||
| 225 | * @return array |
||
| 226 | * array with the following fields: |
||
| 227 | * - local_email |
||
| 228 | * - local_phone |
||
| 229 | * - local_url |
||
| 230 | * - description |
||
| 231 | * - devices - an array of device names and their statuses (for a given profile) |
||
| 232 | */ |
||
| 233 | public function profileAttributes($profId) { |
||
| 234 | $this->languageInstance->setTextDomain("devices"); |
||
| 235 | $validator = new \web\lib\common\InputValidation(); |
||
| 236 | $profile = $validator->Profile($profId); |
||
| 237 | $attribs = $profile->getCollapsedAttributes(); |
||
| 238 | $returnArray = []; |
||
| 239 | $returnArray['silverbullet'] = $profile instanceof ProfileSilverbullet ? 1 : 0; |
||
| 240 | if (isset($attribs['support:email'])) { |
||
| 241 | $returnArray['local_email'] = $attribs['support:email'][0]; |
||
| 242 | } |
||
| 243 | if (isset($attribs['support:phone'])) { |
||
| 244 | $returnArray['local_phone'] = $attribs['support:phone'][0]; |
||
| 245 | } |
||
| 246 | if (isset($attribs['support:url'])) { |
||
| 247 | $returnArray['local_url'] = $attribs['support:url'][0]; |
||
| 248 | } |
||
| 249 | if (isset($attribs['profile:description'])) { |
||
| 250 | $returnArray['description'] = $attribs['profile:description'][0]; |
||
| 251 | } |
||
| 252 | $returnArray['devices'] = $profile->listDevices(); |
||
| 253 | $this->languageInstance->setTextDomain("web_user"); |
||
| 254 | return($returnArray); |
||
| 255 | } |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Generate and send the installer |
||
| 259 | * |
||
| 260 | * @param string $device identifier as in {@link devices.php} |
||
| 261 | * @param int $prof_id profile identifier |
||
| 262 | * @return string binary stream: installerFile |
||
| 263 | */ |
||
| 264 | public function downloadInstaller($device, $prof_id, $generated_for = 'user', $token = NULL, $password = NULL) { |
||
| 265 | $this->loggerInstance->debug(4, "downloadInstaller arguments: $device,$prof_id,$generated_for\n"); |
||
| 266 | $output = $this->generateInstaller($device, $prof_id, $generated_for, $token, $password); |
||
| 267 | $this->loggerInstance->debug(4, "output from GUI::generateInstaller:"); |
||
| 268 | $this->loggerInstance->debug(4, print_r($output, true)); |
||
| 269 | if (empty($output['link']) || $output['link'] === 0) { |
||
| 270 | header("HTTP/1.0 404 Not Found"); |
||
| 271 | return; |
||
| 272 | } |
||
| 273 | $validator = new \web\lib\common\InputValidation(); |
||
| 274 | $profile = $validator->Profile($prof_id); |
||
| 275 | $profile->incrementDownloadStats($device, $generated_for); |
||
| 276 | $file = $this->installerPath; |
||
| 277 | $filetype = $output['mime']; |
||
| 278 | $this->loggerInstance->debug(4, "installer MIME type:$filetype\n"); |
||
| 279 | header("Content-type: " . $filetype); |
||
| 280 | header('Content-Disposition: inline; filename="' . basename($file) . '"'); |
||
| 281 | header('Content-Length: ' . filesize($file)); |
||
| 282 | ob_clean(); |
||
| 283 | flush(); |
||
| 284 | readfile($file); |
||
| 285 | } |
||
| 286 | |||
| 287 | /** |
||
| 288 | * resizes image files |
||
| 289 | * |
||
| 290 | * @param string $inputImage |
||
| 291 | * @param string $destFile |
||
| 292 | * @param int $width |
||
| 293 | * @param int $height |
||
| 294 | * @param bool $resize shall we do resizing? width and height are ignored otherwise |
||
| 295 | * @return array |
||
| 296 | */ |
||
| 297 | private function processImage($inputImage, $destFile, $width, $height, $resize) { |
||
| 316 | } |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Get and prepare logo file |
||
| 320 | * |
||
| 321 | * When called for DiscoJuice, first check if file cache exists |
||
| 322 | * If not then generate the file and save it in the cache |
||
| 323 | * @param int|string $identifier IdP of Federation identifier |
||
| 324 | * @param string $type either 'idp' or 'federation' is allowed |
||
| 325 | * @param int $width maximum width of the generated image - if 0 then it is treated as no upper bound |
||
| 326 | * @param int $height maximum height of the generated image - if 0 then it is treated as no upper bound |
||
| 327 | * @return array|null array with image information or NULL if there is no logo |
||
| 328 | */ |
||
| 329 | protected function getLogo($identifier, $type, $width = 0, $height = 0) { |
||
| 330 | $expiresString = ''; |
||
| 331 | $resize = FALSE; |
||
|
|
|||
| 332 | $attributeName = [ |
||
| 333 | 'federation' => "fed:logo_file", |
||
| 334 | 'idp' => "general:logo_file", |
||
| 335 | ]; |
||
| 336 | |||
| 337 | $logoFile = ""; |
||
| 338 | $validator = new \web\lib\common\InputValidation(); |
||
| 339 | switch ($type) { |
||
| 340 | case "federation": |
||
| 341 | $entity = $validator->Federation($identifier); |
||
| 342 | break; |
||
| 343 | case "idp": |
||
| 344 | $entity = $validator->IdP($identifier); |
||
| 345 | break; |
||
| 346 | default: |
||
| 347 | throw new Exception("Unknown type of logo requested!"); |
||
| 348 | } |
||
| 349 | $filetype = 'image/png'; // default, only one code path where it can become different |
||
| 350 | list($width, $height, $resize) = $this->testForResize($width, $height); |
||
| 351 | if ($resize) { |
||
| 352 | $logoFile = ROOT . '/web/downloads/logos/' . $identifier . '_' . $width . '_' . $height . '.png'; |
||
| 353 | } |
||
| 354 | if (is_file($logoFile)) { // $logoFile could be an empty string but then we will get a FALSE |
||
| 355 | $this->loggerInstance->debug(4, "Using cached logo $logoFile for: $identifier\n"); |
||
| 356 | $blob = file_get_contents($logoFile); |
||
| 357 | } else { |
||
| 358 | $logoAttribute = $entity->getAttributes($attributeName[$type]); |
||
| 359 | if (count($logoAttribute) == 0) { |
||
| 360 | return(NULL); |
||
| 361 | } |
||
| 362 | $this->loggerInstance->debug(4,"RESIZE:$width:$height\n"); |
||
| 363 | $meta = $this->processImage($logoAttribute[0]['value'], $logoFile, $width, $height, $resize); |
||
| 364 | $filetype = $meta['filetype']; |
||
| 365 | $expiresString = $meta['expires']; |
||
| 366 | $blob = $meta['blob']; |
||
| 367 | } |
||
| 368 | return ["filetype" => $filetype, "expires" => $expiresString, "blob" => $blob]; |
||
| 369 | } |
||
| 370 | |||
| 371 | private function testForResize($width, $height) { |
||
| 372 | if (is_numeric($width) && is_numeric($height) && ($width > 0 || $height > 0)) { |
||
| 373 | if ($height == 0) { |
||
| 374 | $height = 10000; |
||
| 375 | } |
||
| 376 | if ($width == 0) { |
||
| 377 | $width = 10000; |
||
| 378 | } |
||
| 379 | $resize = TRUE; |
||
| 380 | } else { |
||
| 381 | $width = 0; |
||
| 382 | $height = 0; |
||
| 383 | $resize = FALSE; |
||
| 384 | } |
||
| 385 | return ([$width, $height, $resize]); |
||
| 386 | } |
||
| 387 | |||
| 388 | /** |
||
| 389 | * find out where the device is currently located |
||
| 390 | * @return array |
||
| 391 | */ |
||
| 392 | public function locateDevice() { |
||
| 393 | return(\core\DeviceLocation::locateDevice()); |
||
| 394 | } |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Lists all identity providers in the database |
||
| 398 | * adding information required by DiscoJuice. |
||
| 399 | * |
||
| 400 | * @param int $activeOnly if set to non-zero will cause listing of only those institutions which have some valid profiles defined. |
||
| 401 | * @param string $country if set, only list IdPs in a specific country |
||
| 402 | * @return array the list of identity providers |
||
| 403 | * |
||
| 404 | */ |
||
| 405 | public function listAllIdentityProviders($activeOnly = 0, $country = "") { |
||
| 406 | return(IdPlist::listAllIdentityProviders($activeOnly, $country)); |
||
| 407 | } |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Order active identity providers according to their distance and name |
||
| 411 | * @param array $currentLocation - current location |
||
| 412 | * @return array $IdPs - list of arrays ('id', 'name'); |
||
| 413 | */ |
||
| 414 | public function orderIdentityProviders($country, $currentLocation = NULL) { |
||
| 415 | return(IdPlist::orderIdentityProviders($country, $currentLocation)); |
||
| 416 | } |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Detect the best device driver form the browser |
||
| 420 | * Detects the operating system and returns its id |
||
| 421 | * display name and group membership (as in devices.php) |
||
| 422 | * @return array|FALSE OS information, indexed by 'id', 'display', 'group' |
||
| 423 | */ |
||
| 424 | public function detectOS() { |
||
| 448 | } |
||
| 449 | |||
| 450 | /* |
||
| 451 | * test if devise is defined and is not hidden. If all is fine return extracted information. |
||
| 452 | * Return FALSE if the device has not been correctly specified |
||
| 453 | */ |
||
| 454 | private function returnDevice($devId, $device) { |
||
| 460 | } |
||
| 461 | |||
| 462 | /** |
||
| 463 | * This methods cheks if the devide has been specified as the HTTP parameters |
||
| 464 | * @return device id|NULL if correcty specified or FALSE otherwise |
||
| 465 | */ |
||
| 466 | private function deviceFromRequest() { |
||
| 467 | $devId = filter_input(INPUT_GET, 'device', FILTER_SANITIZE_STRING) ?? filter_input(INPUT_POST, 'device', FILTER_SANITIZE_STRING); |
||
| 468 | if ($devId === NULL || $devId === FALSE) { |
||
| 469 | $this->loggerInstance->debug(2, "Invalid device id provided\n"); |
||
| 470 | return(NULL); |
||
| 471 | } |
||
| 472 | if (!isset(\devices\Devices::listDevices()[$devId])) { |
||
| 473 | $this->loggerInstance->debug(2, "Unrecognised system: $devId\n"); |
||
| 474 | return(NULL); |
||
| 475 | } |
||
| 476 | return($devId); |
||
| 477 | } |
||
| 478 | |||
| 479 | /** |
||
| 480 | * finds all the user certificates that originated in a given token |
||
| 481 | * @param string $token |
||
| 482 | * @return array|false returns FALSE if a token is invalid, otherwise array of certs |
||
| 483 | */ |
||
| 484 | public function getUserCerts($token) { |
||
| 501 | } |
||
| 502 | |||
| 503 | public $device; |
||
| 504 | private $installerPath; |
||
| 505 | |||
| 506 | /** |
||
| 507 | * helper function to sort profiles by their name |
||
| 508 | * @param \core\AbstractProfile $profile1 the first profile's information |
||
| 509 | * @param \core\AbstractProfile $profile2 the second profile's information |
||
| 510 | * @return int |
||
| 511 | */ |
||
| 512 | private static function profileSort($profile1, $profile2) { |
||
| 514 | } |
||
| 515 | |||
| 516 | } |
||
| 517 |