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