1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SimpleSAML\XML; |
6
|
|
|
|
7
|
|
|
use DOMDocument; |
8
|
|
|
use SimpleSAML\Assert\Assert; |
9
|
|
|
use SimpleSAML\XML\Exception\{IOException, RuntimeException, SchemaViolationException}; |
10
|
|
|
|
11
|
|
|
use function array_unique; |
12
|
|
|
use function defined; |
13
|
|
|
use function file_exists; |
14
|
|
|
use function implode; |
15
|
|
|
use function libxml_clear_errors; |
16
|
|
|
use function libxml_get_errors; |
17
|
|
|
use function libxml_use_internal_errors; |
18
|
|
|
use function sprintf; |
19
|
|
|
use function trim; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* trait class to be used by all the classes that implement the SchemaValidatableElementInterface |
23
|
|
|
* |
24
|
|
|
* @package simplesamlphp/xml-common |
25
|
|
|
*/ |
26
|
|
|
trait SchemaValidatableElementTrait |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* Validate the given DOMDocument against the schema set for this element |
30
|
|
|
* |
31
|
|
|
* @param \DOMDocument $document |
32
|
|
|
* @param string|null $schemaFile |
33
|
|
|
* @return \DOMDocument |
34
|
|
|
* |
35
|
|
|
* @throws \SimpleSAML\XML\Exception\IOException |
36
|
|
|
* @throws \SimpleSAML\XML\Exception\SchemaViolationException |
37
|
|
|
*/ |
38
|
|
|
public static function schemaValidate(DOMDocument $document, ?string $schemaFile = null): DOMDocument |
39
|
|
|
{ |
40
|
|
|
$internalErrors = libxml_use_internal_errors(true); |
41
|
|
|
libxml_clear_errors(); |
42
|
|
|
|
43
|
|
|
if ($schemaFile === null) { |
44
|
|
|
$schemaFile = self::getSchemaFile(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
// Must suppress the warnings here in order to throw them as an error below. |
48
|
|
|
$result = @$document->schemaValidate($schemaFile); |
49
|
|
|
|
50
|
|
|
if ($result === false) { |
51
|
|
|
$msgs = []; |
52
|
|
|
foreach (libxml_get_errors() as $err) { |
53
|
|
|
$msgs[] = trim($err->message) . ' on line ' . $err->line; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
throw new SchemaViolationException(sprintf( |
57
|
|
|
"XML schema validation errors:\n - %s", |
58
|
|
|
implode("\n - ", array_unique($msgs)), |
59
|
|
|
)); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
libxml_use_internal_errors($internalErrors); |
63
|
|
|
libxml_clear_errors(); |
64
|
|
|
|
65
|
|
|
return $document; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get the schema file that can validate this element. |
71
|
|
|
* The path must be relative to the project's base directory. |
72
|
|
|
* |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
|
|
public static function getSchemaFile(): string |
76
|
|
|
{ |
77
|
|
|
if (!defined('static::SCHEMA')) { |
78
|
|
|
throw new RuntimeException('A SCHEMA-constant was not set on this class.'); |
79
|
|
|
} |
80
|
|
|
$schemaFile = static::SCHEMA; |
81
|
|
|
|
82
|
|
|
Assert::true(file_exists($schemaFile), sprintf("File not found: %s", $schemaFile), IOException::class); |
83
|
|
|
return $schemaFile; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|