PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/Auth/Yadis/XML.php

https://gitlab.com/x33n/ampache
PHP | 364 lines | 194 code | 49 blank | 121 comment | 27 complexity | 2f7007834b3e8373f5d99201114349c1 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. /**
  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. }