|
TYPO3 API
SVNRelease
|
00001 <?php 00002 00003 /** 00004 * XML-parsing classes to wrap the domxml and DOM extensions for PHP 4 00005 * and 5, respectively. 00006 * 00007 * @package OpenID 00008 */ 00009 00010 /** 00011 * The base class for wrappers for available PHP XML-parsing 00012 * extensions. To work with this Yadis library, subclasses of this 00013 * class MUST implement the API as defined in the remarks for this 00014 * class. Subclasses of Auth_Yadis_XMLParser are used to wrap 00015 * particular PHP XML extensions such as 'domxml'. These are used 00016 * internally by the library depending on the availability of 00017 * supported PHP XML extensions. 00018 * 00019 * @package OpenID 00020 */ 00021 class Auth_Yadis_XMLParser { 00022 /** 00023 * Initialize an instance of Auth_Yadis_XMLParser with some 00024 * XML and namespaces. This SHOULD NOT be overridden by 00025 * subclasses. 00026 * 00027 * @param string $xml_string A string of XML to be parsed. 00028 * @param array $namespace_map An array of ($ns_name => $ns_uri) 00029 * to be registered with the XML parser. May be empty. 00030 * @return boolean $result True if the initialization and 00031 * namespace registration(s) succeeded; false otherwise. 00032 */ 00033 function init($xml_string, $namespace_map) 00034 { 00035 if (!$this->setXML($xml_string)) { 00036 return false; 00037 } 00038 00039 foreach ($namespace_map as $prefix => $uri) { 00040 if (!$this->registerNamespace($prefix, $uri)) { 00041 return false; 00042 } 00043 } 00044 00045 return true; 00046 } 00047 00048 /** 00049 * Register a namespace with the XML parser. This should be 00050 * overridden by subclasses. 00051 * 00052 * @param string $prefix The namespace prefix to appear in XML tag 00053 * names. 00054 * 00055 * @param string $uri The namespace URI to be used to identify the 00056 * namespace in the XML. 00057 * 00058 * @return boolean $result True if the registration succeeded; 00059 * false otherwise. 00060 */ 00061 function registerNamespace($prefix, $uri) 00062 { 00063 // Not implemented. 00064 } 00065 00066 /** 00067 * Set this parser object's XML payload. This should be 00068 * overridden by subclasses. 00069 * 00070 * @param string $xml_string The XML string to pass to this 00071 * object's XML parser. 00072 * 00073 * @return boolean $result True if the initialization succeeded; 00074 * false otherwise. 00075 */ 00076 function setXML($xml_string) 00077 { 00078 // Not implemented. 00079 } 00080 00081 /** 00082 * Evaluate an XPath expression and return the resulting node 00083 * list. This should be overridden by subclasses. 00084 * 00085 * @param string $xpath The XPath expression to be evaluated. 00086 * 00087 * @param mixed $node A node object resulting from a previous 00088 * evalXPath call. This node, if specified, provides the context 00089 * for the evaluation of this xpath expression. 00090 * 00091 * @return array $node_list An array of matching opaque node 00092 * objects to be used with other methods of this parser class. 00093 */ 00094 function evalXPath($xpath, $node = null) 00095 { 00096 // Not implemented. 00097 } 00098 00099 /** 00100 * Return the textual content of a specified node. 00101 * 00102 * @param mixed $node A node object from a previous call to 00103 * $this->evalXPath(). 00104 * 00105 * @return string $content The content of this node. 00106 */ 00107 function content($node) 00108 { 00109 // Not implemented. 00110 } 00111 00112 /** 00113 * Return the attributes of a specified node. 00114 * 00115 * @param mixed $node A node object from a previous call to 00116 * $this->evalXPath(). 00117 * 00118 * @return array $attrs An array mapping attribute names to 00119 * values. 00120 */ 00121 function attributes($node) 00122 { 00123 // Not implemented. 00124 } 00125 } 00126 00127 /** 00128 * This concrete implementation of Auth_Yadis_XMLParser implements 00129 * the appropriate API for the 'domxml' extension which is typically 00130 * packaged with PHP 4. This class will be used whenever the 'domxml' 00131 * extension is detected. See the Auth_Yadis_XMLParser class for 00132 * details on this class's methods. 00133 * 00134 * @package OpenID 00135 */ 00136 class Auth_Yadis_domxml extends Auth_Yadis_XMLParser { 00137 function Auth_Yadis_domxml() 00138 { 00139 $this->xml = null; 00140 $this->doc = null; 00141 $this->xpath = null; 00142 $this->errors = array(); 00143 } 00144 00145 function setXML($xml_string) 00146 { 00147 $this->xml = $xml_string; 00148 $this->doc = @domxml_open_mem($xml_string, DOMXML_LOAD_PARSING, 00149 $this->errors); 00150 00151 if (!$this->doc) { 00152 return false; 00153 } 00154 00155 $this->xpath = $this->doc->xpath_new_context(); 00156 00157 return true; 00158 } 00159 00160 function registerNamespace($prefix, $uri) 00161 { 00162 return xpath_register_ns($this->xpath, $prefix, $uri); 00163 } 00164 00165 function &evalXPath($xpath, $node = null) 00166 { 00167 if ($node) { 00168 $result = @$this->xpath->xpath_eval($xpath, $node); 00169 } else { 00170 $result = @$this->xpath->xpath_eval($xpath); 00171 } 00172 00173 if (!$result) { 00174 $n = array(); 00175 return $n; 00176 } 00177 00178 if (!$result->nodeset) { 00179 $n = array(); 00180 return $n; 00181 } 00182 00183 return $result->nodeset; 00184 } 00185 00186 function content($node) 00187 { 00188 if ($node) { 00189 return $node->get_content(); 00190 } 00191 } 00192 00193 function attributes($node) 00194 { 00195 if ($node) { 00196 $arr = $node->attributes(); 00197 $result = array(); 00198 00199 if ($arr) { 00200 foreach ($arr as $attrnode) { 00201 $result[$attrnode->name] = $attrnode->value; 00202 } 00203 } 00204 00205 return $result; 00206 } 00207 } 00208 } 00209 00210 /** 00211 * This concrete implementation of Auth_Yadis_XMLParser implements 00212 * the appropriate API for the 'dom' extension which is typically 00213 * packaged with PHP 5. This class will be used whenever the 'dom' 00214 * extension is detected. See the Auth_Yadis_XMLParser class for 00215 * details on this class's methods. 00216 * 00217 * @package OpenID 00218 */ 00219 class Auth_Yadis_dom extends Auth_Yadis_XMLParser { 00220 function Auth_Yadis_dom() 00221 { 00222 $this->xml = null; 00223 $this->doc = null; 00224 $this->xpath = null; 00225 $this->errors = array(); 00226 } 00227 00228 function setXML($xml_string) 00229 { 00230 $this->xml = $xml_string; 00231 $this->doc = new DOMDocument; 00232 00233 if (!$this->doc) { 00234 return false; 00235 } 00236 00237 if (!@$this->doc->loadXML($xml_string)) { 00238 return false; 00239 } 00240 00241 $this->xpath = new DOMXPath($this->doc); 00242 00243 if ($this->xpath) { 00244 return true; 00245 } else { 00246 return false; 00247 } 00248 } 00249 00250 function registerNamespace($prefix, $uri) 00251 { 00252 return $this->xpath->registerNamespace($prefix, $uri); 00253 } 00254 00255 function &evalXPath($xpath, $node = null) 00256 { 00257 if ($node) { 00258 $result = @$this->xpath->query($xpath, $node); 00259 } else { 00260 $result = @$this->xpath->query($xpath); 00261 } 00262 00263 $n = array(); 00264 00265 if (!$result) { 00266 return $n; 00267 } 00268 00269 for ($i = 0; $i < $result->length; $i++) { 00270 $n[] = $result->item($i); 00271 } 00272 00273 return $n; 00274 } 00275 00276 function content($node) 00277 { 00278 if ($node) { 00279 return $node->textContent; 00280 } 00281 } 00282 00283 function attributes($node) 00284 { 00285 if ($node) { 00286 $arr = $node->attributes; 00287 $result = array(); 00288 00289 if ($arr) { 00290 for ($i = 0; $i < $arr->length; $i++) { 00291 $node = $arr->item($i); 00292 $result[$node->nodeName] = $node->nodeValue; 00293 } 00294 } 00295 00296 return $result; 00297 } 00298 } 00299 } 00300 00301 global $__Auth_Yadis_defaultParser; 00302 $__Auth_Yadis_defaultParser = null; 00303 00304 /** 00305 * Set a default parser to override the extension-driven selection of 00306 * available parser classes. This is helpful in a test environment or 00307 * one in which multiple parsers can be used but one is more 00308 * desirable. 00309 * 00310 * @param Auth_Yadis_XMLParser $parser An instance of a 00311 * Auth_Yadis_XMLParser subclass. 00312 */ 00313 function Auth_Yadis_setDefaultParser(&$parser) 00314 { 00315 global $__Auth_Yadis_defaultParser; 00316 $__Auth_Yadis_defaultParser =& $parser; 00317 } 00318 00319 function Auth_Yadis_getSupportedExtensions() 00320 { 00321 return array( 00322 'dom' => array('classname' => 'Auth_Yadis_dom', 00323 'libname' => array('dom.so', 'dom.dll')), 00324 'domxml' => array('classname' => 'Auth_Yadis_domxml', 00325 'libname' => array('domxml.so', 'php_domxml.dll')), 00326 ); 00327 } 00328 00329 /** 00330 * Returns an instance of a Auth_Yadis_XMLParser subclass based on 00331 * the availability of PHP extensions for XML parsing. If 00332 * Auth_Yadis_setDefaultParser has been called, the parser used in 00333 * that call will be returned instead. 00334 */ 00335 function &Auth_Yadis_getXMLParser() 00336 { 00337 global $__Auth_Yadis_defaultParser; 00338 00339 if (isset($__Auth_Yadis_defaultParser)) { 00340 return $__Auth_Yadis_defaultParser; 00341 } 00342 00343 $p = null; 00344 $classname = null; 00345 00346 $extensions = Auth_Yadis_getSupportedExtensions(); 00347 00348 // Return a wrapper for the resident implementation, if any. 00349 foreach ($extensions as $name => $params) { 00350 if (!extension_loaded($name)) { 00351 foreach ($params['libname'] as $libname) { 00352 if (@dl($libname)) { 00353 $classname = $params['classname']; 00354 } 00355 } 00356 } else { 00357 $classname = $params['classname']; 00358 } 00359 if (isset($classname)) { 00360 $p = new $classname(); 00361 return $p; 00362 } 00363 } 00364 00365 if (!isset($p)) { 00366 trigger_error('No XML parser was found', E_USER_ERROR); 00367 } else { 00368 Auth_Yadis_setDefaultParser($p); 00369 } 00370 00371 return $p; 00372 } 00373 00374 ?>
1.8.0