/libraries/openid/Auth/Yadis/XML.php

https://bitbucket.org/stager94/skmz-joomla · PHP · 377 lines · 205 code · 51 blank · 121 comment · 29 complexity · 5f88095f13c110a56894647bf201de1e MD5 · raw file

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