| Total Complexity | 117 |
| Total Lines | 680 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 0 |
Complex classes like MetaLoader 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 MetaLoader, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class MetaLoader |
||
| 19 | { |
||
| 20 | /** @var int|null */ |
||
| 21 | private ?int $expire; |
||
| 22 | |||
| 23 | /** @var array */ |
||
| 24 | private array $metadata = []; |
||
| 25 | |||
| 26 | /** @var object|null */ |
||
| 27 | private ?object $oldMetadataSrc; |
||
| 28 | |||
| 29 | /** @var string|null */ |
||
| 30 | private ?string $stateFile = null; |
||
| 31 | |||
| 32 | /** @var bool */ |
||
| 33 | private bool $changed = false; |
||
| 34 | |||
| 35 | /** @var array */ |
||
| 36 | private array $state = []; |
||
| 37 | |||
| 38 | /** @var array */ |
||
| 39 | private array $types = [ |
||
| 40 | 'saml20-idp-remote', |
||
| 41 | 'saml20-sp-remote', |
||
| 42 | 'attributeauthority-remote', |
||
| 43 | ]; |
||
| 44 | |||
| 45 | |||
| 46 | /** |
||
| 47 | * Constructor |
||
| 48 | * |
||
| 49 | * @param int|null $expire |
||
| 50 | * @param string|null $stateFile |
||
| 51 | * @param object|null $oldMetadataSrc |
||
| 52 | */ |
||
| 53 | public function __construct(?int $expire = null, ?string $stateFile = null, ?object $oldMetadataSrc = null) |
||
| 54 | { |
||
| 55 | $this->expire = $expire; |
||
| 56 | $this->oldMetadataSrc = $oldMetadataSrc; |
||
| 57 | $this->stateFile = $stateFile; |
||
| 58 | |||
| 59 | // Read file containing $state from disk |
||
| 60 | /** @psalm-var array|null */ |
||
| 61 | $state = null; |
||
| 62 | if (!is_null($stateFile) && is_readable($stateFile)) { |
||
| 63 | include($stateFile); |
||
| 64 | } |
||
| 65 | |||
| 66 | if (!empty($state)) { |
||
|
|
|||
| 67 | $this->state = $state; |
||
| 68 | } |
||
| 69 | } |
||
| 70 | |||
| 71 | |||
| 72 | /** |
||
| 73 | * Get the types of entities that will be loaded. |
||
| 74 | * |
||
| 75 | * @return array The entity types allowed. |
||
| 76 | */ |
||
| 77 | public function getTypes(): array |
||
| 78 | { |
||
| 79 | return $this->types; |
||
| 80 | } |
||
| 81 | |||
| 82 | |||
| 83 | /** |
||
| 84 | * Set the types of entities that will be loaded. |
||
| 85 | * |
||
| 86 | * @param string|array $types Either a string with the name of one single type allowed, or an array with a list of |
||
| 87 | * types. Pass an empty array to reset to all types of entities. |
||
| 88 | */ |
||
| 89 | public function setTypes($types): void |
||
| 90 | { |
||
| 91 | if (!is_array($types)) { |
||
| 92 | $types = [$types]; |
||
| 93 | } |
||
| 94 | $this->types = $types; |
||
| 95 | } |
||
| 96 | |||
| 97 | |||
| 98 | /** |
||
| 99 | * This function processes a SAML metadata file. |
||
| 100 | * |
||
| 101 | * @param array $source |
||
| 102 | */ |
||
| 103 | public function loadSource(array $source): void |
||
| 104 | { |
||
| 105 | if (preg_match('@^https?://@i', $source['src'])) { |
||
| 106 | // Build new HTTP context |
||
| 107 | $context = $this->createContext($source); |
||
| 108 | |||
| 109 | $httpUtils = new Utils\HTTP(); |
||
| 110 | $data = null; |
||
| 111 | // GET! |
||
| 112 | try { |
||
| 113 | /** @var array $response We know this because we set the third parameter to `true` */ |
||
| 114 | $response = $httpUtils->fetch($source['src'], $context, true); |
||
| 115 | list($data, $responseHeaders) = $response; |
||
| 116 | } catch (Exception $e) { |
||
| 117 | Logger::warning('metarefresh: ' . $e->getMessage()); |
||
| 118 | } |
||
| 119 | |||
| 120 | // We have response headers, so the request succeeded |
||
| 121 | if (!isset($responseHeaders)) { |
||
| 122 | // No response headers, this means the request failed in some way, so re-use old data |
||
| 123 | Logger::info('No response from ' . $source['src'] . ' - attempting to re-use cached metadata'); |
||
| 124 | $this->addCachedMetadata($source); |
||
| 125 | return; |
||
| 126 | } elseif (preg_match('@^HTTP/(2\.0|1\.[01])\s304\s@', $responseHeaders[0])) { |
||
| 127 | // 304 response |
||
| 128 | Logger::debug('Received HTTP 304 (Not Modified) - attempting to re-use cached metadata'); |
||
| 129 | $this->addCachedMetadata($source); |
||
| 130 | return; |
||
| 131 | } elseif (!preg_match('@^HTTP/(2\.0|1\.[01])\s200\s@', $responseHeaders[0])) { |
||
| 132 | // Other error |
||
| 133 | Logger::info('Error from ' . $source['src'] . ' - attempting to re-use cached metadata'); |
||
| 134 | $this->addCachedMetadata($source); |
||
| 135 | return; |
||
| 136 | } |
||
| 137 | } else { |
||
| 138 | // Local file. |
||
| 139 | $data = file_get_contents($source['src']); |
||
| 140 | $responseHeaders = null; |
||
| 141 | } |
||
| 142 | |||
| 143 | // Everything OK. Proceed. |
||
| 144 | if (isset($source['conditionalGET']) && $source['conditionalGET']) { |
||
| 145 | // Stale or no metadata, so a fresh copy |
||
| 146 | Logger::debug('Downloaded fresh copy'); |
||
| 147 | } |
||
| 148 | |||
| 149 | try { |
||
| 150 | $entities = $this->loadXML($data, $source); |
||
| 151 | } catch (Exception $e) { |
||
| 152 | Logger::notice( |
||
| 153 | 'XML parser error when parsing ' . $source['src'] . ' - attempting to re-use cached metadata', |
||
| 154 | ); |
||
| 155 | Logger::debug('XML parser returned: ' . $e->getMessage()); |
||
| 156 | $this->addCachedMetadata($source); |
||
| 157 | return; |
||
| 158 | } |
||
| 159 | |||
| 160 | foreach ($entities as $entity) { |
||
| 161 | if (!$this->processBlacklist($entity, $source)) { |
||
| 162 | continue; |
||
| 163 | } |
||
| 164 | if (!$this->processWhitelist($entity, $source)) { |
||
| 165 | continue; |
||
| 166 | } |
||
| 167 | if (!$this->processAttributeWhitelist($entity, $source)) { |
||
| 168 | continue; |
||
| 169 | } |
||
| 170 | if (!$this->processCertificates($entity, $source)) { |
||
| 171 | continue; |
||
| 172 | } |
||
| 173 | |||
| 174 | $template = null; |
||
| 175 | if (array_key_exists('template', $source)) { |
||
| 176 | $template = $source['template']; |
||
| 177 | } |
||
| 178 | |||
| 179 | if (array_key_exists('regex-template', $source)) { |
||
| 180 | foreach ($source['regex-template'] as $e => $t) { |
||
| 181 | if (preg_match($e, $entity->getEntityID())) { |
||
| 182 | if (is_array($template)) { |
||
| 183 | $template = array_merge($template, $t); |
||
| 184 | } else { |
||
| 185 | $template = $t; |
||
| 186 | } |
||
| 187 | } |
||
| 188 | } |
||
| 189 | } |
||
| 190 | |||
| 191 | if (in_array('saml20-sp-remote', $this->types, true)) { |
||
| 192 | $this->addMetadata($source['src'], $entity->getMetadata20SP(), 'saml20-sp-remote', $template); |
||
| 193 | } |
||
| 194 | if (in_array('saml20-idp-remote', $this->types, true)) { |
||
| 195 | $this->addMetadata($source['src'], $entity->getMetadata20IdP(), 'saml20-idp-remote', $template); |
||
| 196 | } |
||
| 197 | if (in_array('attributeauthority-remote', $this->types, true)) { |
||
| 198 | $attributeAuthorities = $entity->getAttributeAuthorities(); |
||
| 199 | if (count($attributeAuthorities) && !empty($attributeAuthorities[0])) { |
||
| 200 | $this->addMetadata( |
||
| 201 | $source['src'], |
||
| 202 | $attributeAuthorities, |
||
| 203 | 'attributeauthority-remote', |
||
| 204 | $template, |
||
| 205 | ); |
||
| 206 | } |
||
| 207 | } |
||
| 208 | } |
||
| 209 | |||
| 210 | Logger::debug(sprintf('Found %d entities', count($entities))); |
||
| 211 | $this->saveState($source, $responseHeaders); |
||
| 212 | } |
||
| 213 | |||
| 214 | |||
| 215 | /** |
||
| 216 | * @param \SimpleSAML\Metadata\SAMLParser $entity |
||
| 217 | * @param array $source |
||
| 218 | * @return bool |
||
| 219 | */ |
||
| 220 | private function processCertificates(Metadata\SAMLParser $entity, array $source): bool |
||
| 221 | { |
||
| 222 | if (array_key_exists('certificates', $source) && ($source['certificates'] !== null)) { |
||
| 223 | if (!$entity->validateSignature($source['certificates'])) { |
||
| 224 | $entityId = $entity->getEntityId(); |
||
| 225 | Logger::notice( |
||
| 226 | 'Skipping "' . $entityId . '" - could not verify signature using certificate.' . "\n", |
||
| 227 | ); |
||
| 228 | return false; |
||
| 229 | } |
||
| 230 | } |
||
| 231 | return true; |
||
| 232 | } |
||
| 233 | |||
| 234 | |||
| 235 | /** |
||
| 236 | * @param \SimpleSAML\Metadata\SAMLParser $entity |
||
| 237 | * @param array $source |
||
| 238 | * @return bool |
||
| 239 | */ |
||
| 240 | private function processBlacklist(Metadata\SAMLParser $entity, array $source): bool |
||
| 241 | { |
||
| 242 | if (isset($source['blacklist'])) { |
||
| 243 | if (!empty($source['blacklist']) && in_array($entity->getEntityId(), $source['blacklist'], true)) { |
||
| 244 | Logger::info('Skipping "' . $entity->getEntityId() . '" - blacklisted.' . "\n"); |
||
| 245 | return false; |
||
| 246 | } |
||
| 247 | } |
||
| 248 | return true; |
||
| 249 | } |
||
| 250 | |||
| 251 | |||
| 252 | /** |
||
| 253 | * @param \SimpleSAML\Metadata\SAMLParser $entity |
||
| 254 | * @param array $source |
||
| 255 | * @return bool |
||
| 256 | */ |
||
| 257 | private function processWhitelist(Metadata\SAMLParser $entity, array $source): bool |
||
| 258 | { |
||
| 259 | if (isset($source['whitelist'])) { |
||
| 260 | if (!empty($source['whitelist']) && !in_array($entity->getEntityId(), $source['whitelist'], true)) { |
||
| 261 | Logger::info('Skipping "' . $entity->getEntityId() . '" - not in the whitelist.' . "\n"); |
||
| 262 | return false; |
||
| 263 | } |
||
| 264 | } |
||
| 265 | return true; |
||
| 266 | } |
||
| 267 | |||
| 268 | |||
| 269 | /** |
||
| 270 | * @param \SimpleSAML\Metadata\SAMLParser $entity |
||
| 271 | * @param array $source |
||
| 272 | * @return bool |
||
| 273 | */ |
||
| 274 | private function processAttributeWhitelist(Metadata\SAMLParser $entity, array $source): bool |
||
| 275 | { |
||
| 276 | /* Do we have an attribute whitelist? */ |
||
| 277 | if (isset($source['attributewhitelist']) && !empty($source['attributewhitelist'])) { |
||
| 278 | $idpMetadata = $entity->getMetadata20IdP(); |
||
| 279 | if (!isset($idpMetadata)) { |
||
| 280 | /* Skip non-IdPs */ |
||
| 281 | return false; |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Do a recursive comparison for each whitelist of the attributewhitelist with the idpMetadata for this |
||
| 286 | * IdP. At least one of these whitelists should match |
||
| 287 | */ |
||
| 288 | $match = false; |
||
| 289 | foreach ($source['attributewhitelist'] as $whitelist) { |
||
| 290 | if ($this->containsArray($whitelist, $idpMetadata)) { |
||
| 291 | $match = true; |
||
| 292 | break; |
||
| 293 | } |
||
| 294 | } |
||
| 295 | |||
| 296 | if (!$match) { |
||
| 297 | /* No match found -> next IdP */ |
||
| 298 | return false; |
||
| 299 | } |
||
| 300 | Logger::debug('Whitelisted entityID: ' . $entity->getEntityID()); |
||
| 301 | } |
||
| 302 | return true; |
||
| 303 | } |
||
| 304 | |||
| 305 | |||
| 306 | /** |
||
| 307 | * @param array|string $src |
||
| 308 | * @param array|string $dst |
||
| 309 | * @return bool |
||
| 310 | * |
||
| 311 | * Recursively checks whether array $dst contains array $src. If $src |
||
| 312 | * is not an array, a literal comparison is being performed. |
||
| 313 | */ |
||
| 314 | private function containsArray($src, $dst): bool |
||
| 315 | { |
||
| 316 | if (is_array($src)) { |
||
| 317 | if (!is_array($dst)) { |
||
| 318 | return false; |
||
| 319 | } |
||
| 320 | $dstKeys = array_keys($dst); |
||
| 321 | |||
| 322 | /* Loop over all src keys */ |
||
| 323 | foreach ($src as $srcKey => $srcval) { |
||
| 324 | if (is_int($srcKey)) { |
||
| 325 | /* key is number, check that the key appears as one |
||
| 326 | * of the destination keys: if not, then src has |
||
| 327 | * more keys than dst */ |
||
| 328 | if (!array_key_exists($srcKey, $dst)) { |
||
| 329 | return false; |
||
| 330 | } |
||
| 331 | |||
| 332 | /* loop over dest keys, to find value: we don't know |
||
| 333 | * whether they are in the same order */ |
||
| 334 | $submatch = false; |
||
| 335 | foreach ($dstKeys as $dstKey) { |
||
| 336 | if ($this->containsArray($srcval, $dst[$dstKey])) { |
||
| 337 | $submatch = true; |
||
| 338 | break; |
||
| 339 | } |
||
| 340 | } |
||
| 341 | if (!$submatch) { |
||
| 342 | return false; |
||
| 343 | } |
||
| 344 | } else { |
||
| 345 | /* key is regexp: find matching keys */ |
||
| 346 | /** @var array|false $matchingDstKeys */ |
||
| 347 | $matchingDstKeys = preg_grep($srcKey, $dstKeys); |
||
| 348 | if (!is_array($matchingDstKeys)) { |
||
| 349 | return false; |
||
| 350 | } |
||
| 351 | |||
| 352 | $match = false; |
||
| 353 | foreach ($matchingDstKeys as $dstKey) { |
||
| 354 | if ($this->containsArray($srcval, $dst[$dstKey])) { |
||
| 355 | /* Found a match */ |
||
| 356 | $match = true; |
||
| 357 | break; |
||
| 358 | } |
||
| 359 | } |
||
| 360 | if (!$match) { |
||
| 361 | /* none of the keys has a matching value */ |
||
| 362 | return false; |
||
| 363 | } |
||
| 364 | } |
||
| 365 | } |
||
| 366 | /* each src key/value matches */ |
||
| 367 | return true; |
||
| 368 | } else { |
||
| 369 | /* src is not an array, do a regexp match against dst */ |
||
| 370 | return (preg_match($src, strval($dst)) === 1); |
||
| 371 | } |
||
| 372 | } |
||
| 373 | |||
| 374 | |||
| 375 | /** |
||
| 376 | * Create HTTP context, with any available caches taken into account |
||
| 377 | * |
||
| 378 | * @param array $source |
||
| 379 | * @return array |
||
| 380 | */ |
||
| 381 | private function createContext(array $source): array |
||
| 382 | { |
||
| 383 | $config = Configuration::getInstance(); |
||
| 384 | $name = $config->getOptionalString('technicalcontact_name', null); |
||
| 385 | $mail = $config->getOptionalString('technicalcontact_email', null); |
||
| 386 | |||
| 387 | $rawheader = "User-Agent: SimpleSAMLphp metarefresh, run by $name <$mail>\r\n"; |
||
| 388 | |||
| 389 | if (isset($source['conditionalGET']) && $source['conditionalGET']) { |
||
| 390 | if (array_key_exists($source['src'], $this->state)) { |
||
| 391 | $sourceState = $this->state[$source['src']]; |
||
| 392 | |||
| 393 | if (isset($sourceState['last-modified'])) { |
||
| 394 | $rawheader .= 'If-Modified-Since: ' . $sourceState['last-modified'] . "\r\n"; |
||
| 395 | } |
||
| 396 | |||
| 397 | if (isset($sourceState['etag'])) { |
||
| 398 | $rawheader .= 'If-None-Match: ' . $sourceState['etag'] . "\r\n"; |
||
| 399 | } |
||
| 400 | } |
||
| 401 | } |
||
| 402 | |||
| 403 | return ['http' => ['header' => $rawheader]]; |
||
| 404 | } |
||
| 405 | |||
| 406 | |||
| 407 | private function addCachedMetadata(array $source): void |
||
| 408 | { |
||
| 409 | if (!isset($this->oldMetadataSrc)) { |
||
| 410 | Logger::info('No oldMetadataSrc, cannot re-use cached metadata'); |
||
| 411 | return; |
||
| 412 | } |
||
| 413 | |||
| 414 | foreach ($this->types as $type) { |
||
| 415 | foreach ($this->oldMetadataSrc->getMetadataSet($type) as $entity) { |
||
| 416 | if (array_key_exists('metarefresh:src', $entity)) { |
||
| 417 | if ($entity['metarefresh:src'] == $source['src']) { |
||
| 418 | $this->addMetadata($source['src'], $entity, $type); |
||
| 419 | } |
||
| 420 | } |
||
| 421 | } |
||
| 422 | } |
||
| 423 | } |
||
| 424 | |||
| 425 | |||
| 426 | /** |
||
| 427 | * Store caching state data for a source |
||
| 428 | * |
||
| 429 | * @param array $source |
||
| 430 | * @param array|null $responseHeaders |
||
| 431 | */ |
||
| 432 | private function saveState(array $source, ?array $responseHeaders): void |
||
| 433 | { |
||
| 434 | if (isset($source['conditionalGET']) && $source['conditionalGET']) { |
||
| 435 | // Headers section |
||
| 436 | if ($responseHeaders !== null) { |
||
| 437 | $candidates = ['last-modified', 'etag']; |
||
| 438 | |||
| 439 | foreach ($candidates as $candidate) { |
||
| 440 | if (array_key_exists($candidate, $responseHeaders)) { |
||
| 441 | $this->state[$source['src']][$candidate] = $responseHeaders[$candidate]; |
||
| 442 | } |
||
| 443 | } |
||
| 444 | } |
||
| 445 | |||
| 446 | if (!empty($this->state[$source['src']])) { |
||
| 447 | // Timestamp when this src was requested. |
||
| 448 | $this->state[$source['src']]['requested_at'] = $this->getTime(); |
||
| 449 | $this->changed = true; |
||
| 450 | } |
||
| 451 | } |
||
| 452 | } |
||
| 453 | |||
| 454 | |||
| 455 | /** |
||
| 456 | * Parse XML metadata and return entities |
||
| 457 | * |
||
| 458 | * @param string $data |
||
| 459 | * @param array $source |
||
| 460 | * @return \SimpleSAML\Metadata\SAMLParser[] |
||
| 461 | * @throws \Exception |
||
| 462 | */ |
||
| 463 | private function loadXML(string $data, array $source): array |
||
| 464 | { |
||
| 465 | try { |
||
| 466 | $doc = DOMDocumentFactory::fromString($data, DOMDocumentFactory::DEFAULT_OPTIONS | LIBXML_PARSEHUGE); |
||
| 467 | } catch (Exception $e) { |
||
| 468 | throw new Exception('Failed to read XML from ' . $source['src']); |
||
| 469 | } |
||
| 470 | return Metadata\SAMLParser::parseDescriptorsElement($doc->documentElement); |
||
| 471 | } |
||
| 472 | |||
| 473 | |||
| 474 | /** |
||
| 475 | * This function writes the state array back to disk |
||
| 476 | * |
||
| 477 | */ |
||
| 478 | public function writeState(): void |
||
| 479 | { |
||
| 480 | if ($this->changed && !is_null($this->stateFile)) { |
||
| 481 | Logger::debug('Writing: ' . $this->stateFile); |
||
| 482 | $sysUtils = new Utils\System(); |
||
| 483 | $sysUtils->writeFile( |
||
| 484 | $this->stateFile, |
||
| 485 | "<?php\n/* This file was generated by the metarefresh module at " . $this->getTime() . ".\n" . |
||
| 486 | " Do not update it manually as it will get overwritten. */\n" . |
||
| 487 | '$state = ' . var_export($this->state, true) . ";\n", |
||
| 488 | 0644, |
||
| 489 | ); |
||
| 490 | } |
||
| 491 | } |
||
| 492 | |||
| 493 | |||
| 494 | /** |
||
| 495 | * This function writes the metadata to stdout. |
||
| 496 | * |
||
| 497 | */ |
||
| 498 | public function dumpMetadataStdOut(): void |
||
| 499 | { |
||
| 500 | foreach ($this->metadata as $category => $elements) { |
||
| 501 | echo '/* The following data should be added to metadata/' . $category . '.php. */' . "\n"; |
||
| 502 | |||
| 503 | foreach ($elements as $m) { |
||
| 504 | $filename = $m['filename']; |
||
| 505 | $entityID = $m['metadata']['entityid']; |
||
| 506 | $time = $this->getTime(); |
||
| 507 | echo "\n"; |
||
| 508 | echo '/* The following metadata was generated from ' . $filename . ' on ' . $time . '. */' . "\n"; |
||
| 509 | echo '$metadata[\'' . addslashes($entityID) . '\'] = ' . var_export($m['metadata'], true) . ';' . "\n"; |
||
| 510 | } |
||
| 511 | |||
| 512 | echo "\n"; |
||
| 513 | echo '/* End of data which should be added to metadata/' . $category . '.php. */' . "\n"; |
||
| 514 | echo "\n"; |
||
| 515 | } |
||
| 516 | } |
||
| 517 | |||
| 518 | |||
| 519 | /** |
||
| 520 | * This function adds metadata from the specified file to the list of metadata. |
||
| 521 | * This function will return without making any changes if $metadata is NULL. |
||
| 522 | * |
||
| 523 | * @param string $filename The filename the metadata comes from. |
||
| 524 | * @param \SAML2\XML\md\AttributeAuthorityDescriptor[]|null $metadata The metadata. |
||
| 525 | * @param string $type The metadata type. |
||
| 526 | * @param array|null $template The template. |
||
| 527 | */ |
||
| 528 | private function addMetadata(string $filename, ?array $metadata, string $type, ?array $template = null): void |
||
| 529 | { |
||
| 530 | if ($metadata === null) { |
||
| 531 | return; |
||
| 532 | } |
||
| 533 | |||
| 534 | if (isset($template)) { |
||
| 535 | $metadata = array_merge($metadata, $template); |
||
| 536 | } |
||
| 537 | |||
| 538 | $metadata['metarefresh:src'] = $filename; |
||
| 539 | if (!array_key_exists($type, $this->metadata)) { |
||
| 540 | $this->metadata[$type] = []; |
||
| 541 | } |
||
| 542 | |||
| 543 | // If expire is defined in constructor... |
||
| 544 | if (!empty($this->expire)) { |
||
| 545 | // If expire is already in metadata |
||
| 546 | if (array_key_exists('expire', $metadata)) { |
||
| 547 | // Override metadata expire with more restrictive global config |
||
| 548 | if ($this->expire < $metadata['expire']) { |
||
| 549 | $metadata['expire'] = $this->expire; |
||
| 550 | } |
||
| 551 | |||
| 552 | // If expire is not already in metadata use global config |
||
| 553 | } else { |
||
| 554 | $metadata['expire'] = $this->expire; |
||
| 555 | } |
||
| 556 | } |
||
| 557 | $this->metadata[$type][] = ['filename' => $filename, 'metadata' => $metadata]; |
||
| 558 | } |
||
| 559 | |||
| 560 | |||
| 561 | /** |
||
| 562 | * This function writes the metadata to an ARP file |
||
| 563 | * |
||
| 564 | * @param \SimpleSAML\Configuration $config |
||
| 565 | */ |
||
| 566 | public function writeARPfile(Configuration $config): void |
||
| 567 | { |
||
| 568 | $arpfile = $config->getString('arpfile'); |
||
| 569 | $types = ['saml20-sp-remote']; |
||
| 570 | |||
| 571 | $md = []; |
||
| 572 | foreach ($this->metadata as $category => $elements) { |
||
| 573 | if (!in_array($category, $types, true)) { |
||
| 574 | continue; |
||
| 575 | } |
||
| 576 | $md = array_merge($md, $elements); |
||
| 577 | } |
||
| 578 | |||
| 579 | // $metadata, $attributemap, $prefix, $suffix |
||
| 580 | $arp = new ARP( |
||
| 581 | $md, |
||
| 582 | $config->getOptionalString('attributemap', ''), |
||
| 583 | $config->getOptionalString('prefix', ''), |
||
| 584 | $config->getOptionalString('suffix', ''), |
||
| 585 | ); |
||
| 586 | |||
| 587 | |||
| 588 | $arpxml = $arp->getXML(); |
||
| 589 | |||
| 590 | Logger::info('Writing ARP file: ' . $arpfile . "\n"); |
||
| 591 | file_put_contents($arpfile, $arpxml); |
||
| 592 | } |
||
| 593 | |||
| 594 | |||
| 595 | /** |
||
| 596 | * This function writes the metadata to to separate files in the output directory. |
||
| 597 | * |
||
| 598 | * @param string $outputDir |
||
| 599 | */ |
||
| 600 | public function writeMetadataFiles(string $outputDir): void |
||
| 601 | { |
||
| 602 | while (strlen($outputDir) > 0 && $outputDir[strlen($outputDir) - 1] === '/') { |
||
| 603 | $outputDir = substr($outputDir, 0, strlen($outputDir) - 1); |
||
| 604 | } |
||
| 605 | |||
| 606 | if (!file_exists($outputDir)) { |
||
| 607 | Logger::info('Creating directory: ' . $outputDir . "\n"); |
||
| 608 | $res = @mkdir($outputDir, 0777, true); |
||
| 609 | if ($res === false) { |
||
| 610 | throw new Exception('Error creating directory: ' . $outputDir); |
||
| 611 | } |
||
| 612 | } |
||
| 613 | |||
| 614 | foreach ($this->types as $type) { |
||
| 615 | $filename = $outputDir . '/' . $type . '.php'; |
||
| 616 | |||
| 617 | if (array_key_exists($type, $this->metadata)) { |
||
| 618 | $elements = $this->metadata[$type]; |
||
| 619 | Logger::debug('Writing: ' . $filename); |
||
| 620 | |||
| 621 | $content = '<?php' . "\n" . '/* This file was generated by the metarefresh module at '; |
||
| 622 | $content .= $this->getTime() . "\nDo not update it manually as it will get overwritten\n" . '*/' . "\n"; |
||
| 623 | |||
| 624 | foreach ($elements as $m) { |
||
| 625 | $entityID = $m['metadata']['entityid']; |
||
| 626 | $content .= "\n" . '$metadata[\''; |
||
| 627 | $content .= addslashes($entityID) . '\'] = ' . VarExporter::export($m['metadata']) . ';' . "\n"; |
||
| 628 | } |
||
| 629 | |||
| 630 | $sysUtils = new Utils\System(); |
||
| 631 | $sysUtils->writeFile($filename, $content, 0644); |
||
| 632 | } elseif (is_file($filename)) { |
||
| 633 | if (unlink($filename)) { |
||
| 634 | Logger::debug('Deleting stale metadata file: ' . $filename); |
||
| 635 | } else { |
||
| 636 | Logger::warning('Could not delete stale metadata file: ' . $filename); |
||
| 637 | } |
||
| 638 | } |
||
| 639 | } |
||
| 640 | } |
||
| 641 | |||
| 642 | |||
| 643 | /** |
||
| 644 | * Save metadata for loading with the 'serialize' metadata loader. |
||
| 645 | * |
||
| 646 | * @param string $outputDir The directory we should save the metadata to. |
||
| 647 | */ |
||
| 648 | public function writeMetadataSerialize(string $outputDir): void |
||
| 649 | { |
||
| 650 | $metaHandler = new Metadata\MetaDataStorageHandlerSerialize(['directory' => $outputDir]); |
||
| 651 | |||
| 652 | // First we add all the metadata entries to the metadata handler |
||
| 653 | foreach ($this->metadata as $set => $elements) { |
||
| 654 | foreach ($elements as $m) { |
||
| 655 | $entityId = $m['metadata']['entityid']; |
||
| 656 | |||
| 657 | Logger::debug(sprintf( |
||
| 658 | 'metarefresh: Add metadata entry %s in set %s.', |
||
| 659 | var_export($entityId, true), |
||
| 660 | var_export($set, true), |
||
| 661 | )); |
||
| 662 | $metaHandler->saveMetadata($entityId, $set, $m['metadata']); |
||
| 663 | } |
||
| 664 | } |
||
| 665 | } |
||
| 666 | |||
| 667 | |||
| 668 | /** |
||
| 669 | * This function uses the `PDO` metadata handler to upsert metadata in database. |
||
| 670 | * |
||
| 671 | * @param \SimpleSAML\Configuration $globalConfig |
||
| 672 | * @param array $config An associative array with the configuration for `PDO` handler. |
||
| 673 | * |
||
| 674 | * @return void |
||
| 675 | */ |
||
| 676 | public function writeMetadataPdo(Configuration $globalConfig, array $config = []): void |
||
| 677 | { |
||
| 678 | $metaHandler = new Metadata\MetaDataStorageHandlerPdo($config); |
||
| 679 | |||
| 680 | foreach ($this->metadata as $set => $elements) { |
||
| 681 | foreach ($elements as $m) { |
||
| 682 | $entityId = $m['metadata']['entityid']; |
||
| 683 | |||
| 684 | Logger::debug("PDO Metarefresh: Upsert metadata entry `{$entityId}` in set `{$set}`."); |
||
| 685 | $metaHandler->addEntry($entityId, $set, $m['metadata']); |
||
| 686 | } |
||
| 687 | } |
||
| 688 | } |
||
| 689 | |||
| 690 | |||
| 691 | /** |
||
| 692 | * @return string |
||
| 693 | */ |
||
| 694 | private function getTime(): string |
||
| 698 | } |
||
| 699 | } |
||
| 700 |