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 116
|
<?php
/**
* This file will help doing XPath queries in SAML 2 XML documents.
*
* @author Andreas Åkre Solberg, UNINETT AS. <andreas.solberg@uninett.no>
* @package SimpleSAMLphp
*/
declare(strict_types=1);
namespace SimpleSAML\XML;
class Parser
{
/** @var \SimpleXMLElement */
public $simplexml;
/**
* @param string $xml
*/
public function __construct($xml)
{
$this->simplexml = new \SimpleXMLElement($xml);
$this->simplexml->registerXPathNamespace('saml2', 'urn:oasis:names:tc:SAML:2.0:assertion');
$this->simplexml->registerXPathNamespace('saml2meta', 'urn:oasis:names:tc:SAML:2.0:metadata');
$this->simplexml->registerXPathNamespace('ds', 'http://www.w3.org/2000/09/xmldsig#');
}
/**
* @param \SimpleXMLElement $element
* @return \SimpleSAML\XML\Parser
* @psalm-return \SimpleSAML\XML\Parser
*/
public static function fromSimpleXMLElement(\SimpleXMLElement $element)
{
// Traverse all existing namespaces in element
$namespaces = $element->getNamespaces();
foreach ($namespaces as $prefix => $ns) {
$element[(($prefix === '') ? 'xmlns' : 'xmlns:' . $prefix)] = $ns;
}
/* Create a new parser with the xml document where the namespace definitions
* are added.
*/
$xml = $element->asXML();
if ($xml === false) {
throw new \Exception('Error converting SimpleXMLElement to well-formed XML string.');
}
return new Parser($xml);
}
/**
* @param string $xpath
* @param string $defvalue
* @throws \Exception
* @return string
*/
public function getValueDefault($xpath, $defvalue)
{
try {
/** @var string */
return $this->getValue($xpath, true);
} catch (\Exception $e) {
return $defvalue;
}
}
/**
* @param string $xpath
* @param bool $required
* @throws \Exception
* @return string|null
*/
public function getValue($xpath, $required = false)
{
$result = $this->simplexml->xpath($xpath);
if (!is_array($result) || empty($result)) {
if ($required) {
throw new \Exception(
'Could not get value from XML document using the following XPath expression: ' . $xpath
);
} else {
return null;
}
}
return (string) $result[0];
}
/**
* @param array $xpath
* @param bool $required
* @throws \Exception
* @return string|null
*/
public function getValueAlternatives(array $xpath, $required = false)
{
foreach ($xpath as $x) {
$seek = $this->getValue($x);
if ($seek) {
return $seek;
}
}
if ($required) {
throw new \Exception(
'Could not get value from XML document using multiple alternative XPath expressions.'
);
} else {
return null;
}
}
}
|