-
Notifications
You must be signed in to change notification settings - Fork 136
/
Copy pathDOMDocumentFactory.php
115 lines (91 loc) · 2.98 KB
/
DOMDocumentFactory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
declare(strict_types=1);
namespace SAML2;
use DOMDocument;
use SAML2\Exception\InvalidArgumentException;
use SAML2\Exception\RuntimeException;
use SAML2\Exception\UnparseableXmlException;
final class DOMDocumentFactory
{
/**
* Constructor for DOMDocumentFactory.
* This class should never be instantiated
*/
private function __construct()
{
}
/**
* @param string $xml
*
* @return \DOMDocument
*/
public static function fromString(string $xml) : DOMDocument
{
if (trim($xml) === '') {
throw InvalidArgumentException::invalidType('non-empty string', $xml);
} elseif (PHP_VERSION_ID < 80000) {
$entityLoader = libxml_disable_entity_loader(true);
}
$internalErrors = libxml_use_internal_errors(true);
libxml_clear_errors();
$domDocument = self::create();
$options = LIBXML_DTDLOAD | LIBXML_DTDATTR | LIBXML_NONET | LIBXML_PARSEHUGE;
if (defined('LIBXML_COMPACT')) {
$options |= LIBXML_COMPACT;
}
$loaded = $domDocument->loadXML($xml, $options);
libxml_use_internal_errors($internalErrors);
if (PHP_VERSION_ID < 80000) {
/** @psalm-suppress PossiblyUndefinedVariable */
libxml_disable_entity_loader($entityLoader);
}
if (!$loaded) {
$error = libxml_get_last_error();
libxml_clear_errors();
throw new UnparseableXmlException($error);
}
libxml_clear_errors();
foreach ($domDocument->childNodes as $child) {
if ($child->nodeType === XML_DOCUMENT_TYPE_NODE) {
throw new RuntimeException(
'Dangerous XML detected, DOCTYPE nodes are not allowed in the XML body'
);
}
}
return $domDocument;
}
/**
* @param string $file
*
* @return \DOMDocument
*/
public static function fromFile(string $file) : DOMDocument
{
if (!is_file($file)) {
throw new InvalidArgumentException(sprintf('Path "%s" is not a file', $file));
}
if (!is_readable($file)) {
throw new InvalidArgumentException(sprintf('File "%s" is not readable', $file));
}
// libxml_disable_entity_loader(true) disables \DOMDocument::load() method
// so we need to read the content and use \DOMDocument::loadXML()
$xml = file_get_contents($file);
if ($xml === false) {
throw new RuntimeException(sprintf(
'Contents of readable file "%s" could not be gotten',
$file
));
}
if (trim($xml) === '') {
throw new RuntimeException(sprintf('File "%s" does not have content', $file));
}
return static::fromString($xml);
}
/**
* @return \DOMDocument
*/
public static function create() : DOMDocument
{
return new DOMDocument('1.0', 'UTF-8');
}
}