|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace SimpleSAML\SAML2\XML\mdui; |
|
6
|
|
|
|
|
7
|
|
|
use SimpleSAML\Assert\Assert; |
|
8
|
|
|
use SimpleSAML\SAML2\Assert\Assert as SAMLAssert; |
|
9
|
|
|
use SimpleSAML\SAML2\Exception\ProtocolViolationException; |
|
10
|
|
|
use SimpleSAML\SAML2\XML\StringElementTrait; |
|
11
|
|
|
use SimpleSAML\XML\SchemaValidatableElementInterface; |
|
12
|
|
|
use SimpleSAML\XML\SchemaValidatableElementTrait; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class implementing GeolocationHint. |
|
16
|
|
|
* |
|
17
|
|
|
* @package simplesamlphp/saml2 |
|
18
|
|
|
*/ |
|
19
|
|
|
final class GeolocationHint extends AbstractMduiElement implements SchemaValidatableElementInterface |
|
20
|
|
|
{ |
|
21
|
|
|
use SchemaValidatableElementTrait; |
|
22
|
|
|
use StringElementTrait; |
|
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param string $content |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct(string $content) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->setContent($content); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Set the content of the element. |
|
36
|
|
|
* |
|
37
|
|
|
* @param string $content The value to go in the XML textContent |
|
38
|
|
|
*/ |
|
39
|
|
|
protected function setContent(string $content): void |
|
40
|
|
|
{ |
|
41
|
|
|
$sanitized = $this->sanitizeContent($content); |
|
42
|
|
|
$this->validateContent($sanitized); |
|
43
|
|
|
|
|
44
|
|
|
// Store the email address with any whitespace removed |
|
45
|
|
|
$this->content = $sanitized; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Sanitize the content of the element. |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $content The unsanitized textContent |
|
53
|
|
|
* @throws \Exception on failure |
|
54
|
|
|
* @return string |
|
55
|
|
|
*/ |
|
56
|
|
|
protected function sanitizeContent(string $content): string |
|
57
|
|
|
{ |
|
58
|
|
|
return preg_replace('/\s+/', '', $content); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Validate the content of the element. |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $content The value to go in the XML textContent |
|
66
|
|
|
* @throws \Exception on failure |
|
67
|
|
|
* @return void |
|
68
|
|
|
*/ |
|
69
|
|
|
protected function validateContent(string $content): void |
|
70
|
|
|
{ |
|
71
|
|
|
Assert::notWhitespaceOnly($content, ProtocolViolationException::class); |
|
72
|
|
|
// Assert::regex( |
|
73
|
|
|
// $content, |
|
74
|
|
|
// '/^geo:([-+]?\d+(?:\.\d+)?),([-+]?\d+(?:\.\d+)?)(?:\?z=(\d{1,2}))?$/', |
|
75
|
|
|
// 'Content is not a valid geolocation: %s' |
|
76
|
|
|
// ); |
|
77
|
|
|
// The regex above is incomplete, so for now we only test for a valid URI |
|
78
|
|
|
SAMLAssert::validURI($content); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|