XML.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. $this->xpath = new DOMXPath($this->doc);
  225. if ($this->xpath) {
  226. return true;
  227. } else {
  228. return false;
  229. }
  230. }
  231. function registerNamespace($prefix, $uri)
  232. {
  233. return $this->xpath->registerNamespace($prefix, $uri);
  234. }
  235. function &evalXPath($xpath, $node = null)
  236. {
  237. if ($node) {
  238. $result = @$this->xpath->query($xpath, $node);
  239. } else {
  240. $result = @$this->xpath->query($xpath);
  241. }
  242. $n = array();
  243. if (!$result) {
  244. return $n;
  245. }
  246. for ($i = 0; $i < $result->length; $i++) {
  247. $n[] = $result->item($i);
  248. }
  249. return $n;
  250. }
  251. function content($node)
  252. {
  253. if ($node) {
  254. return $node->textContent;
  255. }
  256. }
  257. function attributes($node)
  258. {
  259. if ($node) {
  260. $arr = $node->attributes;
  261. $result = array();
  262. if ($arr) {
  263. for ($i = 0; $i < $arr->length; $i++) {
  264. $node = $arr->item($i);
  265. $result[$node->nodeName] = $node->nodeValue;
  266. }
  267. }
  268. return $result;
  269. }
  270. }
  271. }
  272. global $__Auth_Yadis_defaultParser;
  273. $__Auth_Yadis_defaultParser = null;
  274. /**
  275. * Set a default parser to override the extension-driven selection of
  276. * available parser classes. This is helpful in a test environment or
  277. * one in which multiple parsers can be used but one is more
  278. * desirable.
  279. *
  280. * @param Auth_Yadis_XMLParser $parser An instance of a
  281. * Auth_Yadis_XMLParser subclass.
  282. */
  283. function Auth_Yadis_setDefaultParser($parser)
  284. {
  285. global $__Auth_Yadis_defaultParser;
  286. $__Auth_Yadis_defaultParser = $parser;
  287. }
  288. function Auth_Yadis_getSupportedExtensions()
  289. {
  290. return array('dom' => 'Auth_Yadis_dom',
  291. 'domxml' => 'Auth_Yadis_domxml');
  292. }
  293. /**
  294. * Returns an instance of a Auth_Yadis_XMLParser subclass based on
  295. * the availability of PHP extensions for XML parsing. If
  296. * Auth_Yadis_setDefaultParser has been called, the parser used in
  297. * that call will be returned instead.
  298. */
  299. function Auth_Yadis_getXMLParser()
  300. {
  301. global $__Auth_Yadis_defaultParser;
  302. if (isset($__Auth_Yadis_defaultParser)) {
  303. return $__Auth_Yadis_defaultParser;
  304. }
  305. foreach(Auth_Yadis_getSupportedExtensions() as $extension => $classname)
  306. {
  307. if (extension_loaded($extension))
  308. {
  309. $p = new $classname();
  310. Auth_Yadis_setDefaultParser($p);
  311. return $p;
  312. }
  313. }
  314. return false;
  315. }