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
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Trait grouping common functionality for simple elements with base64 textContent |
13
|
|
|
* |
14
|
|
|
* @package simplesamlphp/xml-common |
15
|
|
|
*/ |
16
|
|
|
trait XMLBase64ElementTrait |
17
|
|
|
{ |
18
|
|
|
use XMLStringElementTrait; |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Get the content of the element. |
23
|
|
|
* |
24
|
|
|
* @return string |
25
|
|
|
*/ |
26
|
|
|
public function getContent(): string |
27
|
|
|
{ |
28
|
|
|
return $this->sanitizeContent($this->getRawContent()); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Get the raw and unsanitized content of the element. |
34
|
|
|
* |
35
|
|
|
* @return string |
36
|
|
|
*/ |
37
|
|
|
public function getRawContent(): string |
38
|
|
|
{ |
39
|
|
|
return $this->content; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Sanitize the content of the element. |
45
|
|
|
* |
46
|
|
|
* @param string $content The unsanitized textContent |
47
|
|
|
* @throws \Exception on failure |
48
|
|
|
* @return string |
49
|
|
|
*/ |
50
|
|
|
protected function sanitizeContent(string $content): string |
51
|
|
|
{ |
52
|
|
|
return str_replace(["\r", "\n", "\t", ' '], '', $content); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Validate the content of the element. |
58
|
|
|
* |
59
|
|
|
* @param string $content The value to go in the XML textContent |
60
|
|
|
* @throws \Exception on failure |
61
|
|
|
* @return void |
62
|
|
|
*/ |
63
|
|
|
protected function validateContent(string $content): void |
64
|
|
|
{ |
65
|
|
|
// Note: content must already be sanitized before validating |
66
|
|
|
Assert::stringPlausibleBase64($this->sanitizeContent($content)); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Convert this element to XML. |
72
|
|
|
* |
73
|
|
|
* @param \DOMElement|null $parent The element we should append this element to. |
74
|
|
|
* @return \DOMElement |
75
|
|
|
*/ |
76
|
|
|
public function toXML(DOMElement $parent = null): DOMElement |
77
|
|
|
{ |
78
|
|
|
$e = $this->instantiateParentElement($parent); |
79
|
|
|
$e->textContent = $this->getRawContent(); |
80
|
|
|
|
81
|
|
|
return $e; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|