|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace SimpleSAML\XML; |
|
6
|
|
|
|
|
7
|
|
|
use DOMDocument; |
|
8
|
|
|
use InvalidArgumentException; |
|
9
|
|
|
use RuntimeException; |
|
10
|
|
|
use SimpleSAML\Assert\Assert; |
|
11
|
|
|
use SimpleSAML\XML\Exception\IOException; |
|
12
|
|
|
use SimpleSAML\XML\Exception\UnparseableXMLException; |
|
13
|
|
|
|
|
14
|
|
|
use function defined; |
|
15
|
|
|
use function file_get_contents; |
|
16
|
|
|
use function is_file; |
|
17
|
|
|
use function is_readable; |
|
18
|
|
|
use function libxml_clear_errors; |
|
19
|
|
|
use function libxml_get_last_error; |
|
20
|
|
|
use function libxml_use_internal_errors; |
|
21
|
|
|
use function sprintf; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @package simplesamlphp/xml-common |
|
25
|
|
|
*/ |
|
26
|
|
|
final class DOMDocumentFactory |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @param string $xml |
|
30
|
|
|
* @psalm-param non-empty-string $xml |
|
31
|
|
|
* |
|
32
|
|
|
* @return \DOMDocument |
|
33
|
|
|
*/ |
|
34
|
|
|
public static function fromString(string $xml): DOMDocument |
|
35
|
|
|
{ |
|
36
|
|
|
Assert::notWhitespaceOnly($xml); |
|
37
|
|
|
|
|
38
|
|
|
$internalErrors = libxml_use_internal_errors(true); |
|
39
|
|
|
libxml_clear_errors(); |
|
40
|
|
|
|
|
41
|
|
|
$domDocument = self::create(); |
|
42
|
|
|
$options = LIBXML_DTDLOAD | LIBXML_DTDATTR | LIBXML_NONET | LIBXML_PARSEHUGE | LIBXML_NSCLEAN; |
|
43
|
|
|
if (defined('LIBXML_COMPACT')) { |
|
44
|
|
|
$options |= LIBXML_COMPACT; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$loaded = $domDocument->loadXML($xml, $options); |
|
48
|
|
|
|
|
49
|
|
|
libxml_use_internal_errors($internalErrors); |
|
50
|
|
|
|
|
51
|
|
|
if (!$loaded) { |
|
52
|
|
|
$error = libxml_get_last_error(); |
|
53
|
|
|
libxml_clear_errors(); |
|
54
|
|
|
|
|
55
|
|
|
throw new UnparseableXMLException($error); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
libxml_clear_errors(); |
|
59
|
|
|
|
|
60
|
|
|
foreach ($domDocument->childNodes as $child) { |
|
61
|
|
|
if ($child->nodeType === XML_DOCUMENT_TYPE_NODE) { |
|
62
|
|
|
throw new RuntimeException( |
|
63
|
|
|
'Dangerous XML detected, DOCTYPE nodes are not allowed in the XML body', |
|
64
|
|
|
); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return $domDocument; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param string $file |
|
74
|
|
|
* |
|
75
|
|
|
* @return \DOMDocument |
|
76
|
|
|
*/ |
|
77
|
|
|
public static function fromFile(string $file): DOMDocument |
|
78
|
|
|
{ |
|
79
|
|
|
error_clear_last(); |
|
80
|
|
|
$xml = @file_get_contents($file); |
|
81
|
|
|
if ($xml === false) { |
|
82
|
|
|
/** @psalm-var array $e */ |
|
83
|
|
|
$e = error_get_last(); |
|
84
|
|
|
$error = $e['message'] ?: "Check that the file exists and can be read."; |
|
85
|
|
|
|
|
86
|
|
|
throw new IOException("File '$file' was not loaded; $error"); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
Assert::notWhitespaceOnly($xml, sprintf('File "%s" does not have content', $file), RuntimeException::class); |
|
90
|
|
|
/** @psalm-var non-empty-string $xml */ |
|
91
|
|
|
return static::fromString($xml); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @param string $version |
|
97
|
|
|
* @param string $encoding |
|
98
|
|
|
* @return \DOMDocument |
|
99
|
|
|
*/ |
|
100
|
|
|
public static function create(string $version = '1.0', string $encoding = 'UTF-8'): DOMDocument |
|
101
|
|
|
{ |
|
102
|
|
|
return new DOMDocument($version, $encoding); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|