[ Index ]

PHP Cross Reference of Joomla 1.5.26 DE

title

Body

[close]

/libraries/openid/Auth/Yadis/ -> XML.php (source)

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


Generated: Wed Mar 28 15:54:07 2012 Cross-referenced by PHPXref 0.7.1