PageRenderTime 38ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/openid/Services/Yadis/XML.php

https://gitlab.com/endomorphosis/greenrenaissancejoomla
PHP | 365 lines | 197 code | 48 blank | 120 comment | 27 complexity | e1ae895a580bbabd525d789e42e36faf 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 Yadis
  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 Services_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 Yadis
  18. */
  19. class Services_Yadis_XMLParser {
  20. /**
  21. * Initialize an instance of Services_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 Services_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 Services_Yadis_XMLParser class for
  122. * details on this class's methods.
  123. *
  124. * @package Yadis
  125. */
  126. class Services_Yadis_domxml extends Services_Yadis_XMLParser {
  127. function Services_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->nodeset) {
  157. $n = array();
  158. return $n;
  159. }
  160. return $result->nodeset;
  161. }
  162. function content($node)
  163. {
  164. if ($node) {
  165. return $node->get_content();
  166. }
  167. }
  168. function attributes($node)
  169. {
  170. if ($node) {
  171. $arr = $node->attributes();
  172. $result = array();
  173. if ($arr) {
  174. foreach ($arr as $attrnode) {
  175. $result[$attrnode->name] = $attrnode->value;
  176. }
  177. }
  178. return $result;
  179. }
  180. }
  181. }
  182. /**
  183. * This concrete implementation of Services_Yadis_XMLParser implements
  184. * the appropriate API for the 'dom' extension which is typically
  185. * packaged with PHP 5. This class will be used whenever the 'dom'
  186. * extension is detected. See the Services_Yadis_XMLParser class for
  187. * details on this class's methods.
  188. *
  189. * @package Yadis
  190. */
  191. class Services_Yadis_dom extends Services_Yadis_XMLParser {
  192. function Services_Yadis_dom()
  193. {
  194. $this->xml = null;
  195. $this->doc = null;
  196. $this->xpath = null;
  197. $this->errors = array();
  198. }
  199. function setXML($xml_string)
  200. {
  201. $this->xml = $xml_string;
  202. $this->doc = new DOMDocument;
  203. if (!$this->doc) {
  204. return false;
  205. }
  206. if (!@$this->doc->loadXML($xml_string)) {
  207. return false;
  208. }
  209. $this->xpath = new DOMXPath($this->doc);
  210. if ($this->xpath) {
  211. return true;
  212. } else {
  213. return false;
  214. }
  215. }
  216. function registerNamespace($prefix, $uri)
  217. {
  218. return $this->xpath->registerNamespace($prefix, $uri);
  219. }
  220. function &evalXPath($xpath, $node = null)
  221. {
  222. if ($node) {
  223. $result = @$this->xpath->query($xpath, $node);
  224. } else {
  225. $result = @$this->xpath->query($xpath);
  226. }
  227. $n = array();
  228. for ($i = 0; $i < $result->length; $i++) {
  229. $n[] = $result->item($i);
  230. }
  231. return $n;
  232. }
  233. function content($node)
  234. {
  235. if ($node) {
  236. return $node->textContent;
  237. }
  238. }
  239. function attributes($node)
  240. {
  241. if ($node) {
  242. $arr = $node->attributes;
  243. $result = array();
  244. if ($arr) {
  245. for ($i = 0; $i < $arr->length; $i++) {
  246. $node = $arr->item($i);
  247. $result[$node->nodeName] = $node->nodeValue;
  248. }
  249. }
  250. return $result;
  251. }
  252. }
  253. }
  254. global $__Services_Yadis_defaultParser;
  255. $__Services_Yadis_defaultParser = null;
  256. /**
  257. * Set a default parser to override the extension-driven selection of
  258. * available parser classes. This is helpful in a test environment or
  259. * one in which multiple parsers can be used but one is more
  260. * desirable.
  261. *
  262. * @param Services_Yadis_XMLParser $parser An instance of a
  263. * Services_Yadis_XMLParser subclass.
  264. */
  265. function Services_Yadis_setDefaultParser(&$parser)
  266. {
  267. global $__Services_Yadis_defaultParser;
  268. $__Services_Yadis_defaultParser =& $parser;
  269. }
  270. function Services_Yadis_getSupportedExtensions()
  271. {
  272. return array(
  273. 'dom' => array('classname' => 'Services_Yadis_dom',
  274. 'libname' => array('dom.so', 'dom.dll')),
  275. 'domxml' => array('classname' => 'Services_Yadis_domxml',
  276. 'libname' => array('domxml.so', 'php_domxml.dll')),
  277. );
  278. }
  279. /**
  280. * Returns an instance of a Services_Yadis_XMLParser subclass based on
  281. * the availability of PHP extensions for XML parsing. If
  282. * Services_Yadis_setDefaultParser has been called, the parser used in
  283. * that call will be returned instead.
  284. */
  285. function &Services_Yadis_getXMLParser()
  286. {
  287. global $__Services_Yadis_defaultParser;
  288. if (isset($__Services_Yadis_defaultParser)) {
  289. return $__Services_Yadis_defaultParser;
  290. }
  291. $p = null;
  292. $classname = null;
  293. $extensions = Services_Yadis_getSupportedExtensions();
  294. // Return a wrapper for the resident implementation, if any.
  295. foreach ($extensions as $name => $params) {
  296. if (!extension_loaded($name)) {
  297. foreach ($params['libname'] as $libname) {
  298. if (@dl($libname)) {
  299. $classname = $params['classname'];
  300. }
  301. }
  302. } else {
  303. $classname = $params['classname'];
  304. }
  305. if (isset($classname)) {
  306. $p = new $classname();
  307. return $p;
  308. }
  309. }
  310. if (!isset($p)) {
  311. trigger_error('No XML parser was found', E_USER_ERROR);
  312. } else {
  313. Services_Yadis_setDefaultParser($p);
  314. }
  315. return $p;
  316. }
  317. ?>