ILIAS  trunk Revision v11.0_alpha-1843-g9e1fad99175
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilSamlIdpXmlMetadataParser.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
23 
25 {
26  private bool $xmlErrorState = false;
28  private array $errorStack = [];
29 
30  public function __construct(
31  private readonly DataTypeFactory $dataFactory,
32  private readonly ilSamlIdpXmlMetadataErrorFormatter $errorFormatter
33  ) {
34  }
35 
36  private function beginLogging(): void
37  {
38  if ([] === $this->errorStack) {
39  $this->xmlErrorState = libxml_use_internal_errors(true);
40  libxml_clear_errors();
41  } else {
42  $this->addErrors();
43  }
44 
45  $this->errorStack[] = [];
46  }
47 
48  private function addErrors(): void
49  {
50  $currentErrors = libxml_get_errors();
51  libxml_clear_errors();
52 
53  $level = count($this->errorStack) - 1;
54  $this->errorStack[$level] = array_merge($this->errorStack[$level], $currentErrors);
55  }
56 
60  private function endLogging(): array
61  {
62  $this->addErrors();
63 
64  $errors = array_pop($this->errorStack);
65 
66  if ([] === $this->errorStack) {
67  libxml_use_internal_errors($this->xmlErrorState);
68  }
69 
70  return $errors;
71  }
72 
76  public function parse(string $xmlString): Result
77  {
78  try {
79  $this->beginLogging();
80 
81  $xml = new SimpleXMLElement($xmlString);
82 
83  $xml->registerXPathNamespace('md', 'urn:oasis:names:tc:SAML:2.0:metadata');
84  $xml->registerXPathNamespace('mdui', 'urn:oasis:names:tc:SAML:metadata:ui');
85 
86  $idps = $xml->xpath('//md:EntityDescriptor[//md:IDPSSODescriptor]');
87 
88  $entity_id = ($idps[0] ?? null)?->attributes('', true)['entityID'][0] ?? '';
89  $entity_id = (string) $entity_id;
90 
91  if ($entity_id !== '') {
92  return $this->ok($entity_id);
93  }
94 
95  $error = new LibXMLError();
96  $error->level = LIBXML_ERR_FATAL;
97  $error->code = 0;
98  $error->message = 'No entityID found';
99  $error->line = 1;
100  $error->column = 0;
101 
102  return $this->error([$error]);
103  } catch (Exception) {
104  return $this->error();
105  }
106  }
107 
108  private function ok(string $entity_id): Result
109  {
110  $this->endLogging();
111 
112  return $this->dataFactory->ok($entity_id);
113  }
114 
118  private function error($additional_errors = []): Result
119  {
120  return $this->dataFactory->error($this->errorFormatter->formatErrors(...$this->endLogging(), ...$additional_errors));
121  }
122 }
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__construct(private readonly DataTypeFactory $dataFactory, private readonly ilSamlIdpXmlMetadataErrorFormatter $errorFormatter)