|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the LightSAML-Core package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Milos Tomic <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the MIT license that is bundled |
|
9
|
|
|
* with this source code in the file LICENSE. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace LightSaml\Validator\Model\Xsd; |
|
13
|
|
|
|
|
14
|
|
|
use LightSaml\Error\LightSamlXmlException; |
|
15
|
|
|
|
|
16
|
|
|
class XsdValidator |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @param string $xml |
|
20
|
|
|
* |
|
21
|
|
|
* @return XsdError[] |
|
22
|
|
|
*/ |
|
23
|
7 |
|
public function validateProtocol($xml) |
|
24
|
|
|
{ |
|
25
|
7 |
|
return $this->validate($xml, 'saml-schema-protocol-2.0.xsd'); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param string $xml |
|
30
|
|
|
* |
|
31
|
|
|
* @return XsdError[] |
|
32
|
|
|
*/ |
|
33
|
2 |
|
public function validateMetadata($xml) |
|
34
|
|
|
{ |
|
35
|
2 |
|
return $this->validate($xml, 'saml-schema-metadata-2.0.xsd'); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param string $xml |
|
40
|
|
|
* @param string $schema |
|
41
|
|
|
* |
|
42
|
|
|
* @return XsdError[] |
|
43
|
|
|
*/ |
|
44
|
9 |
|
private function validate($xml, $schema) |
|
45
|
|
|
{ |
|
46
|
9 |
|
$result = []; |
|
47
|
9 |
|
libxml_clear_errors(); |
|
48
|
9 |
|
$doc = new \DOMDocument(); |
|
49
|
|
|
|
|
50
|
|
|
set_error_handler(function ($errno, $errstr, $errfile, $errline, array $errcontext) use (&$result) { |
|
|
|
|
|
|
51
|
|
|
$error = new XsdError(XsdError::FATAL, $errno, $errstr, 0, 0); |
|
52
|
|
|
$result[] = $error; |
|
53
|
9 |
|
}); |
|
54
|
|
|
|
|
55
|
9 |
|
$schemaFile = __DIR__.'/../../../../../xsd/'.$schema; |
|
56
|
9 |
|
if (!is_file($schemaFile)) { |
|
57
|
|
|
throw new LightSamlXmlException('Invalid schema specified'); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
9 |
|
$ok = @$doc->loadXML($xml); |
|
61
|
9 |
|
if (!$ok) { |
|
62
|
2 |
|
restore_error_handler(); |
|
63
|
|
|
|
|
64
|
|
|
return [ |
|
65
|
2 |
|
new XsdError(XsdError::FATAL, 0, 'Invalid XML', 0, 0), |
|
66
|
|
|
]; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
7 |
|
@$doc->schemaValidate($schemaFile); |
|
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
/** @var \LibXMLError[] $errors */ |
|
72
|
7 |
|
$errors = libxml_get_errors(); |
|
73
|
7 |
|
foreach ($errors as $error) { |
|
74
|
|
|
$err = XsdError::fromLibXMLError($error); |
|
75
|
|
|
$result[] = $err; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
7 |
|
restore_error_handler(); |
|
79
|
|
|
|
|
80
|
7 |
|
return $result; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.