web/lib/Zend/Dom/Query.php
changeset 1230 68c69c656a2c
parent 807 877f952ae2bd
equal deleted inserted replaced
1229:5a6b6e770365 1230:68c69c656a2c
    12  * obtain it through the world-wide-web, please send an email
    12  * obtain it through the world-wide-web, please send an email
    13  * to license@zend.com so we can send you a copy immediately.
    13  * to license@zend.com so we can send you a copy immediately.
    14  *
    14  *
    15  * @category   Zend
    15  * @category   Zend
    16  * @package    Zend_Dom
    16  * @package    Zend_Dom
    17  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    17  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
    18  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    18  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    19  * @version    $Id: Query.php 25033 2012-08-17 19:50:08Z matthew $
    19  * @version    $Id$
    20  */
    20  */
    21 
    21 
    22 /**
    22 /**
    23  * @see Zend_Dom_Query_Css2Xpath
    23  * @see Zend_Dom_Query_Css2Xpath
    24  */
    24  */
    26 
    26 
    27 /**
    27 /**
    28  * @see Zend_Dom_Query_Result
    28  * @see Zend_Dom_Query_Result
    29  */
    29  */
    30 require_once 'Zend/Dom/Query/Result.php';
    30 require_once 'Zend/Dom/Query/Result.php';
       
    31 
       
    32 /** @see Zend_Xml_Security */
       
    33 require_once 'Zend/Xml/Security.php';
       
    34 
       
    35 /** @see Zend_Xml_Exception */
       
    36 require_once 'Zend/Xml/Exception.php';
    31 
    37 
    32 /**
    38 /**
    33  * Query DOM structures based on CSS selectors and/or XPath
    39  * Query DOM structures based on CSS selectors and/or XPath
    34  *
    40  *
    35  * @package    Zend_Dom
    41  * @package    Zend_Dom
    36  * @subpackage Query
    42  * @subpackage Query
    37  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    43  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
    38  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    44  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    39  */
    45  */
    40 class Zend_Dom_Query
    46 class Zend_Dom_Query
    41 {
    47 {
    42     /**#@+
    48     /**#@+
    77     protected $_xpathNamespaces = array();
    83     protected $_xpathNamespaces = array();
    78 
    84 
    79     /**
    85     /**
    80      * Constructor
    86      * Constructor
    81      *
    87      *
    82      * @param  null|string $document
    88      * @param null|string $document
    83      * @return void
    89      * @param null|string $encoding
    84      */
    90      */
    85     public function __construct($document = null, $encoding = null)
    91     public function __construct($document = null, $encoding = null)
    86     {
    92     {
    87         $this->setEncoding($encoding);
    93         $this->setEncoding($encoding);
    88         $this->setDocument($document);
    94         $this->setDocument($document);
   231 
   237 
   232     /**
   238     /**
   233      * Perform an XPath query
   239      * Perform an XPath query
   234      *
   240      *
   235      * @param  string|array $xpathQuery
   241      * @param  string|array $xpathQuery
   236      * @param  string $query CSS selector query
   242      * @param  string       $query CSS selector query
       
   243      * @throws Zend_Dom_Exception
   237      * @return Zend_Dom_Query_Result
   244      * @return Zend_Dom_Query_Result
   238      */
   245      */
   239     public function queryXpath($xpathQuery, $query = null)
   246     public function queryXpath($xpathQuery, $query = null)
   240     {
   247     {
   241         if (null === ($document = $this->getDocument())) {
   248         if (null === ($document = $this->getDocument())) {
   243             throw new Zend_Dom_Exception('Cannot query; no document registered');
   250             throw new Zend_Dom_Exception('Cannot query; no document registered');
   244         }
   251         }
   245 
   252 
   246         $encoding = $this->getEncoding();
   253         $encoding = $this->getEncoding();
   247         libxml_use_internal_errors(true);
   254         libxml_use_internal_errors(true);
   248         libxml_disable_entity_loader(true);
       
   249         if (null === $encoding) {
   255         if (null === $encoding) {
   250             $domDoc = new DOMDocument('1.0');
   256             $domDoc = new DOMDocument('1.0');
   251         } else {
   257         } else {
   252             $domDoc = new DOMDocument('1.0', $encoding);
   258             $domDoc = new DOMDocument('1.0', $encoding);
   253         }
   259         }
   254         $type   = $this->getDocumentType();
   260         $type   = $this->getDocumentType();
   255         switch ($type) {
   261         switch ($type) {
   256             case self::DOC_XML:
   262             case self::DOC_XML:
   257                 $success = $domDoc->loadXML($document);
   263                 try {
   258                 foreach ($domDoc->childNodes as $child) {
   264                     $domDoc = Zend_Xml_Security::scan($document, $domDoc);
   259                     if ($child->nodeType === XML_DOCUMENT_TYPE_NODE) {
   265                     $success = ($domDoc !== false);
   260                         require_once 'Zend/Dom/Exception.php';
   266                 } catch (Zend_Xml_Exception $e) {
   261                         throw new Zend_Dom_Exception(
   267                     require_once 'Zend/Dom/Exception.php';
   262                             'Invalid XML: Detected use of illegal DOCTYPE'
   268                     throw new Zend_Dom_Exception(
   263                         );
   269                         $e->getMessage()
   264                     }
   270                     );
   265                 }
   271                 }
   266                 break;
   272                 break;
   267             case self::DOC_HTML:
   273             case self::DOC_HTML:
   268             case self::DOC_XHTML:
   274             case self::DOC_XHTML:
   269             default:
   275             default:
   273         $errors = libxml_get_errors();
   279         $errors = libxml_get_errors();
   274         if (!empty($errors)) {
   280         if (!empty($errors)) {
   275             $this->_documentErrors = $errors;
   281             $this->_documentErrors = $errors;
   276             libxml_clear_errors();
   282             libxml_clear_errors();
   277         }
   283         }
   278         libxml_disable_entity_loader(false);
       
   279         libxml_use_internal_errors(false);
   284         libxml_use_internal_errors(false);
   280 
   285 
   281         if (!$success) {
   286         if (!$success) {
   282             require_once 'Zend/Dom/Exception.php';
   287             require_once 'Zend/Dom/Exception.php';
   283             throw new Zend_Dom_Exception(sprintf('Error parsing document (type == %s)', $type));
   288             throw new Zend_Dom_Exception(sprintf('Error parsing document (type == %s)', $type));
   284         }
   289         }
   285 
   290 
   286         $nodeList   = $this->_getNodeList($domDoc, $xpathQuery);
   291         $nodeList = $this->_getNodeList($domDoc, $xpathQuery);
   287         return new Zend_Dom_Query_Result($query, $xpathQuery, $domDoc, $nodeList);
   292         return new Zend_Dom_Query_Result($query, $xpathQuery, $domDoc, $nodeList);
   288     }
   293     }
   289 
   294 
   290     /**
   295     /**
   291      * Register XPath namespaces
   296      * Register XPath namespaces