1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SimpleSAML\XML; |
6
|
|
|
|
7
|
|
|
use DOMElement; |
8
|
|
|
use SimpleSAML\Assert\Assert; |
9
|
|
|
use SimpleSAML\XML\Constants; |
10
|
|
|
use SimpleSAML\XML\Exception\InvalidDOMElementException; |
11
|
|
|
use SimpleSAML\XML\Exception\SchemaViolationException; |
12
|
|
|
|
13
|
|
|
use function str_replace; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Trait grouping common functionality for simple elements with base64 textContent |
17
|
|
|
* |
18
|
|
|
* @package simplesamlphp/xml-common |
19
|
|
|
*/ |
20
|
|
|
trait Base64ElementTrait |
21
|
|
|
{ |
22
|
|
|
/** @var string */ |
23
|
|
|
protected string $content; |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Set the content of the element. |
28
|
|
|
* |
29
|
|
|
* @param string $content The value to go in the XML textContent |
30
|
|
|
*/ |
31
|
|
|
protected function setContent(string $content): void |
32
|
|
|
{ |
33
|
|
|
$this->validateContent($content); |
34
|
|
|
$this->content = $content; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Get the content of the element. |
40
|
|
|
* |
41
|
|
|
* @return string |
42
|
|
|
*/ |
43
|
|
|
public function getContent(): string |
44
|
|
|
{ |
45
|
|
|
return $this->sanitizeContent($this->getRawContent()); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get the raw and unsanitized content of the element. |
51
|
|
|
* |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
|
|
public function getRawContent(): string |
55
|
|
|
{ |
56
|
|
|
return $this->content; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Sanitize the content of the element. |
62
|
|
|
* |
63
|
|
|
* @param string $content The unsanitized textContent |
64
|
|
|
* @throws \Exception on failure |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
|
|
protected function sanitizeContent(string $content): string |
68
|
|
|
{ |
69
|
|
|
return str_replace(["\r", "\n", "\t", ' '], '', $content); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Validate the content of the element. |
75
|
|
|
* |
76
|
|
|
* @param string $content The value to go in the XML textContent |
77
|
|
|
* @throws \Exception on failure |
78
|
|
|
* @return void |
79
|
|
|
*/ |
80
|
|
|
protected function validateContent(string $content): void |
81
|
|
|
{ |
82
|
|
|
// Note: content must already be sanitized before validating |
83
|
|
|
Assert::stringPlausibleBase64( |
84
|
|
|
$this->sanitizeContent($content), |
85
|
|
|
SchemaViolationException::class |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Convert XML into a class instance |
92
|
|
|
* |
93
|
|
|
* @param \DOMElement $xml The XML element we should load |
94
|
|
|
* @return static |
95
|
|
|
* |
96
|
|
|
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException |
97
|
|
|
* If the qualified name of the supplied element is wrong |
98
|
|
|
*/ |
99
|
|
|
public static function fromXML(DOMElement $xml): static |
100
|
|
|
{ |
101
|
|
|
Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class); |
102
|
|
|
Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class); |
|
|
|
|
103
|
|
|
|
104
|
|
|
return new static($xml->textContent); |
|
|
|
|
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Convert this element to XML. |
110
|
|
|
* |
111
|
|
|
* @param \DOMElement|null $parent The element we should append this element to. |
112
|
|
|
* @return \DOMElement |
113
|
|
|
*/ |
114
|
|
|
public function toXML(DOMElement $parent = null): DOMElement |
115
|
|
|
{ |
116
|
|
|
$e = $this->instantiateParentElement($parent); |
117
|
|
|
$e->textContent = $this->getRawContent(); |
118
|
|
|
|
119
|
|
|
return $e; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
|
123
|
|
|
/** @return string */ |
124
|
|
|
abstract public static function getLocalName(): string; |
125
|
|
|
|
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Create a document structure for this element |
129
|
|
|
* |
130
|
|
|
* @param \DOMElement|null $parent The element we should append to. |
131
|
|
|
* @return \DOMElement |
132
|
|
|
*/ |
133
|
|
|
abstract public function instantiateParentElement(DOMElement $parent = null): DOMElement; |
134
|
|
|
} |
135
|
|
|
|