1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SimpleSAML\XML; |
6
|
|
|
|
7
|
|
|
use DateTimeImmutable; |
8
|
|
|
use DateTimeInterface; |
9
|
|
|
use DOMElement; |
10
|
|
|
use SimpleSAML\XML\Assert\Assert; |
11
|
|
|
|
12
|
|
|
use function trim; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Helper functions for the XML library. |
16
|
|
|
* |
17
|
|
|
* @package simplesamlphp/xml-common |
18
|
|
|
*/ |
19
|
|
|
class Utils |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @deprecated Create elements that extend AbstractElement and use it's interface to retrieve attributes instead. |
23
|
|
|
* |
24
|
|
|
* Extract localized strings from a set of nodes. |
25
|
|
|
* |
26
|
|
|
* @param \DOMElement $parent The element that contains the localized strings. |
27
|
|
|
* @param string $namespaceURI The namespace URI the localized strings should have. |
28
|
|
|
* @param string $localName The localName of the localized strings. |
29
|
|
|
* @return array<string, string> Localized strings. |
30
|
|
|
*/ |
31
|
|
|
public static function extractLocalizedStrings(DOMElement $parent, string $namespaceURI, string $localName): array |
32
|
|
|
{ |
33
|
|
|
$ret = []; |
34
|
|
|
foreach ($parent->childNodes as $node) { |
35
|
|
|
if ($node->namespaceURI !== $namespaceURI || $node->localName !== $localName) { |
36
|
|
|
continue; |
37
|
|
|
} elseif (!($node instanceof DOMElement)) { |
38
|
|
|
continue; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
if ($node->hasAttribute('xml:lang')) { |
42
|
|
|
$language = $node->getAttribute('xml:lang'); |
43
|
|
|
} else { |
44
|
|
|
$language = 'en'; |
45
|
|
|
} |
46
|
|
|
$ret[$language] = trim($node->textContent); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $ret; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @deprecated Create elements that extend AbstractElement and use <element>::getChildrenOfClass() instead. |
55
|
|
|
* |
56
|
|
|
* Extract strings from a set of nodes. |
57
|
|
|
* |
58
|
|
|
* @param \DOMElement $parent The element that contains the localized strings. |
59
|
|
|
* @param string $namespaceURI The namespace URI the string elements should have. |
60
|
|
|
* @param string $localName The localName of the string elements. |
61
|
|
|
* @return string[] The string values of the various nodes. |
62
|
|
|
*/ |
63
|
|
|
public static function extractStrings(DOMElement $parent, string $namespaceURI, string $localName): array |
64
|
|
|
{ |
65
|
|
|
$ret = []; |
66
|
|
|
foreach ($parent->childNodes as $node) { |
67
|
|
|
if ($node->namespaceURI !== $namespaceURI || $node->localName !== $localName) { |
68
|
|
|
continue; |
69
|
|
|
} |
70
|
|
|
$ret[] = trim($node->textContent); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $ret; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @deprecated Create elements that extend AbstractElement and use <element>::toXML() instead. |
79
|
|
|
* |
80
|
|
|
* Append string element. |
81
|
|
|
* |
82
|
|
|
* @param \DOMElement $parent The parent element we should append the new nodes to. |
83
|
|
|
* @param string $namespace The namespace of the created element. |
84
|
|
|
* @param string $name The name of the created element. |
85
|
|
|
* @param string $value The value of the element. |
86
|
|
|
* @return \DOMElement The generated element. |
87
|
|
|
*/ |
88
|
|
|
public static function addString( |
89
|
|
|
DOMElement $parent, |
90
|
|
|
string $namespace, |
91
|
|
|
string $name, |
92
|
|
|
string $value, |
93
|
|
|
): DOMElement { |
94
|
|
|
$doc = $parent->ownerDocument; |
95
|
|
|
Assert::notNull($doc); |
96
|
|
|
|
97
|
|
|
$n = $doc->createElementNS($namespace, $name); |
98
|
|
|
$n->appendChild($doc->createTextNode($value)); |
99
|
|
|
$parent->appendChild($n); |
100
|
|
|
|
101
|
|
|
return $n; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @deprecated Create elements that extend AbstractElement and use <element>::toXML() instead. |
107
|
|
|
* |
108
|
|
|
* Append string elements. |
109
|
|
|
* |
110
|
|
|
* @param \DOMElement $parent The parent element we should append the new nodes to. |
111
|
|
|
* @param string $namespace The namespace of the created elements |
112
|
|
|
* @param string $name The name of the created elements |
113
|
|
|
* @param bool $localized Whether the strings are localized, and should include the xml:lang attribute. |
114
|
|
|
* @param string[] $values The values we should create the elements from. |
115
|
|
|
*/ |
116
|
|
|
public static function addStrings( |
117
|
|
|
DOMElement $parent, |
118
|
|
|
string $namespace, |
119
|
|
|
string $name, |
120
|
|
|
bool $localized, |
121
|
|
|
array $values, |
122
|
|
|
): void { |
123
|
|
|
$doc = $parent->ownerDocument; |
124
|
|
|
Assert::notNull($doc); |
125
|
|
|
|
126
|
|
|
foreach ($values as $index => $value) { |
127
|
|
|
$n = $doc->createElementNS($namespace, $name); |
128
|
|
|
$n->appendChild($doc->createTextNode($value)); |
129
|
|
|
if ($localized) { |
130
|
|
|
$n->setAttribute('xml:lang', $index); |
131
|
|
|
} |
132
|
|
|
$parent->appendChild($n); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @deprecated Use DateTime objects instead |
139
|
|
|
* |
140
|
|
|
* This function converts a SAML2 timestamp on the form |
141
|
|
|
* yyyy-mm-ddThh:mm:ss(\.s+)?Z to a UNIX timestamp. The sub-second |
142
|
|
|
* part is ignored. |
143
|
|
|
* |
144
|
|
|
* Note that we always require a 'Z' timezone for the dateTime to be valid. |
145
|
|
|
* This is not in the SAML spec but that's considered to be a bug in the |
146
|
|
|
* spec. See https://github.com/simplesamlphp/saml2/pull/36 for some |
147
|
|
|
* background. |
148
|
|
|
* |
149
|
|
|
* @param string $time The time we should convert. |
150
|
|
|
* @throws \Exception |
151
|
|
|
* @return int Converted to a unix timestamp. |
152
|
|
|
*/ |
153
|
|
|
public static function xsDateTimeToTimestamp(string $time): int |
154
|
|
|
{ |
155
|
|
|
Assert::validDateTime($time); |
156
|
|
|
|
157
|
|
|
$dateTime1 = DateTimeImmutable::createFromFormat(DateTimeInterface::ISO8601, $time); |
158
|
|
|
$dateTime2 = DateTimeImmutable::createFromFormat(DateTimeInterface::RFC3339_EXTENDED, $time); |
159
|
|
|
|
160
|
|
|
$dateTime = $dateTime1 ?: $dateTime2; |
|
|
|
|
161
|
|
|
Assert::isInstanceOf($dateTime, DateTimeImmutable::class); |
162
|
|
|
Assert::same($dateTime->getTimeZone()->getName(), 'Z'); |
163
|
|
|
|
164
|
|
|
return $dateTime->getTimestamp(); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|