XML.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. <?php
  2. /**
  3. * XML-parsing classes to wrap the domxml and DOM extensions for PHP 4
  4. * and 5, respectively.
  5. *
  6. * @package OpenID
  7. */
  8. /**
  9. * The base class for wrappers for available PHP XML-parsing
  10. * extensions. To work with this Yadis library, subclasses of this
  11. * class MUST implement the API as defined in the remarks for this
  12. * class. Subclasses of Auth_Yadis_XMLParser are used to wrap
  13. * particular PHP XML extensions such as 'domxml'. These are used
  14. * internally by the library depending on the availability of
  15. * supported PHP XML extensions.
  16. *
  17. * @package OpenID
  18. */
  19. class Auth_Yadis_XMLParser {
  20. /**
  21. * Initialize an instance of Auth_Yadis_XMLParser with some
  22. * XML and namespaces. This SHOULD NOT be overridden by
  23. * subclasses.
  24. *
  25. * @param string $xml_string A string of XML to be parsed.
  26. * @param array $namespace_map An array of ($ns_name => $ns_uri)
  27. * to be registered with the XML parser. May be empty.
  28. * @return boolean $result True if the initialization and
  29. * namespace registration(s) succeeded; false otherwise.
  30. */
  31. function init($xml_string, $namespace_map)
  32. {
  33. if (!$this->setXML($xml_string)) {
  34. return false;
  35. }
  36. foreach ($namespace_map as $prefix => $uri) {
  37. if (!$this->registerNamespace($prefix, $uri)) {
  38. return false;
  39. }
  40. }
  41. return true;
  42. }
  43. /**
  44. * Register a namespace with the XML parser. This should be
  45. * overridden by subclasses.
  46. *
  47. * @param string $prefix The namespace prefix to appear in XML tag
  48. * names.
  49. *
  50. * @param string $uri The namespace URI to be used to identify the
  51. * namespace in the XML.
  52. *
  53. * @return boolean $result True if the registration succeeded;
  54. * false otherwise.
  55. */
  56. function registerNamespace($prefix, $uri)
  57. {
  58. // Not implemented.
  59. }
  60. /**
  61. * Set this parser object's XML payload. This should be
  62. * overridden by subclasses.
  63. *
  64. * @param string $xml_string The XML string to pass to this
  65. * object's XML parser.
  66. *
  67. * @return boolean $result True if the initialization succeeded;
  68. * false otherwise.
  69. */
  70. function setXML($xml_string)
  71. {
  72. // Not implemented.
  73. }
  74. /**
  75. * Evaluate an XPath expression and return the resulting node
  76. * list. This should be overridden by subclasses.
  77. *
  78. * @param string $xpath The XPath expression to be evaluated.
  79. *
  80. * @param mixed $node A node object resulting from a previous
  81. * evalXPath call. This node, if specified, provides the context
  82. * for the evaluation of this xpath expression.
  83. *
  84. * @return array $node_list An array of matching opaque node
  85. * objects to be used with other methods of this parser class.
  86. */
  87. function &evalXPath($xpath, $node = null)
  88. {
  89. // Not implemented.
  90. }
  91. /**
  92. * Return the textual content of a specified node.
  93. *
  94. * @param mixed $node A node object from a previous call to
  95. * $this->evalXPath().
  96. *
  97. * @return string $content The content of this node.
  98. */
  99. function content($node)
  100. {
  101. // Not implemented.
  102. }
  103. /**
  104. * Return the attributes of a specified node.
  105. *
  106. * @param mixed $node A node object from a previous call to
  107. * $this->evalXPath().
  108. *
  109. * @return array $attrs An array mapping attribute names to
  110. * values.
  111. */
  112. function attributes($node)
  113. {
  114. // Not implemented.
  115. }
  116. }
  117. /**
  118. * This concrete implementation of Auth_Yadis_XMLParser implements
  119. * the appropriate API for the 'domxml' extension which is typically
  120. * packaged with PHP 4. This class will be used whenever the 'domxml'
  121. * extension is detected. See the Auth_Yadis_XMLParser class for
  122. * details on this class's methods.
  123. *
  124. * @package OpenID
  125. */
  126. class Auth_Yadis_domxml extends Auth_Yadis_XMLParser {
  127. function Auth_Yadis_domxml()
  128. {
  129. $this->xml = null;
  130. $this->doc = null;
  131. $this->xpath = null;
  132. $this->errors = array();
  133. }
  134. function setXML($xml_string)
  135. {
  136. $this->xml = $xml_string;
  137. $this->doc = @domxml_open_mem($xml_string, DOMXML_LOAD_PARSING,
  138. $this->errors);
  139. if (!$this->doc) {
  140. return false;
  141. }
  142. $this->xpath = $this->doc->xpath_new_context();
  143. return true;
  144. }
  145. function registerNamespace($prefix, $uri)
  146. {
  147. return xpath_register_ns($this->xpath, $prefix, $uri);
  148. }
  149. function &evalXPath($xpath, $node = null)
  150. {
  151. if ($node) {
  152. $result = @$this->xpath->xpath_eval($xpath, $node);
  153. } else {
  154. $result = @$this->xpath->xpath_eval($xpath);
  155. }
  156. if (!$result) {
  157. $n = array();
  158. return $n;
  159. }
  160. if (!$result->nodeset) {
  161. $n = array();
  162. return $n;
  163. }
  164. return $result->nodeset;
  165. }
  166. function content($node)
  167. {
  168. if ($node) {
  169. return $node->get_content();
  170. }
  171. }
  172. function attributes($node)
  173. {
  174. if ($node) {
  175. $arr = $node->attributes();
  176. $result = array();
  177. if ($arr) {
  178. foreach ($arr as $attrnode) {
  179. $result[$attrnode->name] = $attrnode->value;
  180. }
  181. }
  182. return $result;
  183. }
  184. }
  185. }
  186. /**
  187. * This concrete implementation of Auth_Yadis_XMLParser implements
  188. * the appropriate API for the 'dom' extension which is typically
  189. * packaged with PHP 5. This class will be used whenever the 'dom'
  190. * extension is detected. See the Auth_Yadis_XMLParser class for
  191. * details on this class's methods.
  192. *
  193. * @package OpenID
  194. */
  195. class Auth_Yadis_dom extends Auth_Yadis_XMLParser {
  196. function Auth_Yadis_dom()
  197. {
  198. $this->xml = null;
  199. $this->doc = null;
  200. $this->xpath = null;
  201. $this->errors = array();
  202. }
  203. function setXML($xml_string)
  204. {
  205. $this->xml = $xml_string;
  206. $this->doc = new DOMDocument;
  207. if (!$this->doc) {
  208. return false;
  209. }
  210. // libxml_disable_entity_loader (PHP 5 >= 5.2.11)
  211. if (function_exists('libxml_disable_entity_loader') && function_exists('libxml_use_internal_errors')) {
  212. // disable external entities and libxml errors
  213. $loader = libxml_disable_entity_loader(true);
  214. $errors = libxml_use_internal_errors(true);
  215. $parse_result = @$this->doc->loadXML($xml_string);
  216. libxml_disable_entity_loader($loader);
  217. libxml_use_internal_errors($errors);
  218. } else {
  219. $parse_result = @$this->doc->loadXML($xml_string);
  220. }
  221. if (!$parse_result) {
  222. return false;
  223. }
  224. if (isset($this->doc->doctype)) {
  225. return false;
  226. }
  227. $this->xpath = new DOMXPath($this->doc);
  228. if ($this->xpath) {
  229. return true;
  230. } else {
  231. return false;
  232. }
  233. }
  234. function registerNamespace($prefix, $uri)
  235. {
  236. return $this->xpath->registerNamespace($prefix, $uri);
  237. }
  238. function &evalXPath($xpath, $node = null)
  239. {
  240. if ($node) {
  241. $result = @$this->xpath->query($xpath, $node);
  242. } else {
  243. $result = @$this->xpath->query($xpath);
  244. }
  245. $n = array();
  246. if (!$result) {
  247. return $n;
  248. }
  249. for ($i = 0; $i < $result->length; $i++) {
  250. $n[] = $result->item($i);
  251. }
  252. return $n;
  253. }
  254. function content($node)
  255. {
  256. if ($node) {
  257. return $node->textContent;
  258. }
  259. }
  260. function attributes($node)
  261. {
  262. if ($node) {
  263. $arr = $node->attributes;
  264. $result = array();
  265. if ($arr) {
  266. for ($i = 0; $i < $arr->length; $i++) {
  267. $node = $arr->item($i);
  268. $result[$node->nodeName] = $node->nodeValue;
  269. }
  270. }
  271. return $result;
  272. }
  273. }
  274. }
  275. global $__Auth_Yadis_defaultParser;
  276. $__Auth_Yadis_defaultParser = null;
  277. /**
  278. * Set a default parser to override the extension-driven selection of
  279. * available parser classes. This is helpful in a test environment or
  280. * one in which multiple parsers can be used but one is more
  281. * desirable.
  282. *
  283. * @param Auth_Yadis_XMLParser $parser An instance of a
  284. * Auth_Yadis_XMLParser subclass.
  285. */
  286. function Auth_Yadis_setDefaultParser($parser)
  287. {
  288. global $__Auth_Yadis_defaultParser;
  289. $__Auth_Yadis_defaultParser = $parser;
  290. }
  291. function Auth_Yadis_getSupportedExtensions()
  292. {
  293. return array('dom' => 'Auth_Yadis_dom',
  294. 'domxml' => 'Auth_Yadis_domxml');
  295. }
  296. /**
  297. * Returns an instance of a Auth_Yadis_XMLParser subclass based on
  298. * the availability of PHP extensions for XML parsing. If
  299. * Auth_Yadis_setDefaultParser has been called, the parser used in
  300. * that call will be returned instead.
  301. */
  302. function Auth_Yadis_getXMLParser()
  303. {
  304. global $__Auth_Yadis_defaultParser;
  305. if (isset($__Auth_Yadis_defaultParser)) {
  306. return $__Auth_Yadis_defaultParser;
  307. }
  308. foreach(Auth_Yadis_getSupportedExtensions() as $extension => $classname)
  309. {
  310. if (extension_loaded($extension))
  311. {
  312. $p = new $classname();
  313. Auth_Yadis_setDefaultParser($p);
  314. return $p;
  315. }
  316. }
  317. return false;
  318. }