PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/inc/lib/Auth/Yadis/XML.php

https://bitbucket.org/yoander/mtrack
PHP | 374 lines | 204 code | 50 blank | 120 comment | 29 complexity | 759d6a481cd8aeabc7a264d3f278e483 MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0
  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. if (!@$this->doc->loadXML($xml_string)) {
  211. return false;
  212. }
  213. $this->xpath = new DOMXPath($this->doc);
  214. if ($this->xpath) {
  215. return true;
  216. } else {
  217. return false;
  218. }
  219. }
  220. function registerNamespace($prefix, $uri)
  221. {
  222. return $this->xpath->registerNamespace($prefix, $uri);
  223. }
  224. function &evalXPath($xpath, $node = null)
  225. {
  226. if ($node) {
  227. $result = @$this->xpath->query($xpath, $node);
  228. } else {
  229. $result = @$this->xpath->query($xpath);
  230. }
  231. $n = array();
  232. if (!$result) {
  233. return $n;
  234. }
  235. for ($i = 0; $i < $result->length; $i++) {
  236. $n[] = $result->item($i);
  237. }
  238. return $n;
  239. }
  240. function content($node)
  241. {
  242. if ($node) {
  243. return $node->textContent;
  244. }
  245. }
  246. function attributes($node)
  247. {
  248. if ($node) {
  249. $arr = $node->attributes;
  250. $result = array();
  251. if ($arr) {
  252. for ($i = 0; $i < $arr->length; $i++) {
  253. $node = $arr->item($i);
  254. $result[$node->nodeName] = $node->nodeValue;
  255. }
  256. }
  257. return $result;
  258. }
  259. }
  260. }
  261. global $__Auth_Yadis_defaultParser;
  262. $__Auth_Yadis_defaultParser = null;
  263. /**
  264. * Set a default parser to override the extension-driven selection of
  265. * available parser classes. This is helpful in a test environment or
  266. * one in which multiple parsers can be used but one is more
  267. * desirable.
  268. *
  269. * @param Auth_Yadis_XMLParser $parser An instance of a
  270. * Auth_Yadis_XMLParser subclass.
  271. */
  272. function Auth_Yadis_setDefaultParser(&$parser)
  273. {
  274. global $__Auth_Yadis_defaultParser;
  275. $__Auth_Yadis_defaultParser =& $parser;
  276. }
  277. function Auth_Yadis_getSupportedExtensions()
  278. {
  279. return array(
  280. 'dom' => array('classname' => 'Auth_Yadis_dom',
  281. 'libname' => array('dom.so', 'dom.dll')),
  282. 'domxml' => array('classname' => 'Auth_Yadis_domxml',
  283. 'libname' => array('domxml.so', 'php_domxml.dll')),
  284. );
  285. }
  286. /**
  287. * Returns an instance of a Auth_Yadis_XMLParser subclass based on
  288. * the availability of PHP extensions for XML parsing. If
  289. * Auth_Yadis_setDefaultParser has been called, the parser used in
  290. * that call will be returned instead.
  291. */
  292. function &Auth_Yadis_getXMLParser()
  293. {
  294. global $__Auth_Yadis_defaultParser;
  295. if (isset($__Auth_Yadis_defaultParser)) {
  296. return $__Auth_Yadis_defaultParser;
  297. }
  298. $p = null;
  299. $classname = null;
  300. $extensions = Auth_Yadis_getSupportedExtensions();
  301. // Return a wrapper for the resident implementation, if any.
  302. foreach ($extensions as $name => $params) {
  303. if (!extension_loaded($name)) {
  304. foreach ($params['libname'] as $libname) {
  305. if (@dl($libname)) {
  306. $classname = $params['classname'];
  307. }
  308. }
  309. } else {
  310. $classname = $params['classname'];
  311. }
  312. if (isset($classname)) {
  313. $p = new $classname();
  314. return $p;
  315. }
  316. }
  317. if (!isset($p)) {
  318. trigger_error('No XML parser was found', E_USER_ERROR);
  319. } else {
  320. Auth_Yadis_setDefaultParser($p);
  321. }
  322. return $p;
  323. }
  324. ?>