web/lib/Zend/Dom/Query.php
changeset 807 877f952ae2bd
parent 207 621fa6caec0c
child 1230 68c69c656a2c
equal deleted inserted replaced
805:5e7a0fedabdf 807:877f952ae2bd
    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-2010 Zend Technologies USA Inc. (http://www.zend.com)
    17  * @copyright  Copyright (c) 2005-2012 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 23062 2010-10-08 14:05:45Z matthew $
    19  * @version    $Id: Query.php 25033 2012-08-17 19:50:08Z matthew $
    20  */
    20  */
    21 
    21 
    22 /**
    22 /**
    23  * @see Zend_Dom_Query_Css2Xpath
    23  * @see Zend_Dom_Query_Css2Xpath
    24  */
    24  */
    32 /**
    32 /**
    33  * Query DOM structures based on CSS selectors and/or XPath
    33  * Query DOM structures based on CSS selectors and/or XPath
    34  *
    34  *
    35  * @package    Zend_Dom
    35  * @package    Zend_Dom
    36  * @subpackage Query
    36  * @subpackage Query
    37  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    37  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    38  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    38  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    39  */
    39  */
    40 class Zend_Dom_Query
    40 class Zend_Dom_Query
    41 {
    41 {
    42     /**#@+
    42     /**#@+
    88         $this->setDocument($document);
    88         $this->setDocument($document);
    89     }
    89     }
    90 
    90 
    91     /**
    91     /**
    92      * Set document encoding
    92      * Set document encoding
    93      * 
    93      *
    94      * @param  string $encoding 
    94      * @param  string $encoding
    95      * @return Zend_Dom_Query
    95      * @return Zend_Dom_Query
    96      */
    96      */
    97     public function setEncoding($encoding)
    97     public function setEncoding($encoding)
    98     {
    98     {
    99         $this->_encoding = (null === $encoding) ? null : (string) $encoding;
    99         $this->_encoding = (null === $encoding) ? null : (string) $encoding;
   100         return $this;
   100         return $this;
   101     }
   101     }
   102 
   102 
   103     /**
   103     /**
   104      * Get document encoding
   104      * Get document encoding
   105      * 
   105      *
   106      * @return null|string
   106      * @return null|string
   107      */
   107      */
   108     public function getEncoding()
   108     public function getEncoding()
   109     {
   109     {
   110         return $this->_encoding;
   110         return $this->_encoding;
   122         if (0 === strlen($document)) {
   122         if (0 === strlen($document)) {
   123             return $this;
   123             return $this;
   124         }
   124         }
   125         // breaking XML declaration to make syntax highlighting work
   125         // breaking XML declaration to make syntax highlighting work
   126         if ('<' . '?xml' == substr(trim($document), 0, 5)) {
   126         if ('<' . '?xml' == substr(trim($document), 0, 5)) {
       
   127             if (preg_match('/<html[^>]*xmlns="([^"]+)"[^>]*>/i', $document, $matches)) {
       
   128                 $this->_xpathNamespaces[] = $matches[1];
       
   129                 return $this->setDocumentXhtml($document, $encoding);
       
   130             }
   127             return $this->setDocumentXml($document, $encoding);
   131             return $this->setDocumentXml($document, $encoding);
   128         }
   132         }
   129         if (strstr($document, 'DTD XHTML')) {
   133         if (strstr($document, 'DTD XHTML')) {
   130             return $this->setDocumentXhtml($document, $encoding);
   134             return $this->setDocumentXhtml($document, $encoding);
   131         }
   135         }
   203         return $this->_docType;
   207         return $this->_docType;
   204     }
   208     }
   205 
   209 
   206     /**
   210     /**
   207      * Get any DOMDocument errors found
   211      * Get any DOMDocument errors found
   208      * 
   212      *
   209      * @return false|array
   213      * @return false|array
   210      */
   214      */
   211     public function getDocumentErrors()
   215     public function getDocumentErrors()
   212     {
   216     {
   213         return $this->_documentErrors;
   217         return $this->_documentErrors;
   239             throw new Zend_Dom_Exception('Cannot query; no document registered');
   243             throw new Zend_Dom_Exception('Cannot query; no document registered');
   240         }
   244         }
   241 
   245 
   242         $encoding = $this->getEncoding();
   246         $encoding = $this->getEncoding();
   243         libxml_use_internal_errors(true);
   247         libxml_use_internal_errors(true);
       
   248         libxml_disable_entity_loader(true);
   244         if (null === $encoding) {
   249         if (null === $encoding) {
   245             $domDoc = new DOMDocument('1.0');
   250             $domDoc = new DOMDocument('1.0');
   246         } else {
   251         } else {
   247             $domDoc = new DOMDocument('1.0', $encoding);
   252             $domDoc = new DOMDocument('1.0', $encoding);
   248         }
   253         }
   249         $type   = $this->getDocumentType();
   254         $type   = $this->getDocumentType();
   250         switch ($type) {
   255         switch ($type) {
   251             case self::DOC_XML:
   256             case self::DOC_XML:
   252                 $success = $domDoc->loadXML($document);
   257                 $success = $domDoc->loadXML($document);
       
   258                 foreach ($domDoc->childNodes as $child) {
       
   259                     if ($child->nodeType === XML_DOCUMENT_TYPE_NODE) {
       
   260                         require_once 'Zend/Dom/Exception.php';
       
   261                         throw new Zend_Dom_Exception(
       
   262                             'Invalid XML: Detected use of illegal DOCTYPE'
       
   263                         );
       
   264                     }
       
   265                 }
   253                 break;
   266                 break;
   254             case self::DOC_HTML:
   267             case self::DOC_HTML:
   255             case self::DOC_XHTML:
   268             case self::DOC_XHTML:
   256             default:
   269             default:
   257                 $success = $domDoc->loadHTML($document);
   270                 $success = $domDoc->loadHTML($document);
   260         $errors = libxml_get_errors();
   273         $errors = libxml_get_errors();
   261         if (!empty($errors)) {
   274         if (!empty($errors)) {
   262             $this->_documentErrors = $errors;
   275             $this->_documentErrors = $errors;
   263             libxml_clear_errors();
   276             libxml_clear_errors();
   264         }
   277         }
       
   278         libxml_disable_entity_loader(false);
   265         libxml_use_internal_errors(false);
   279         libxml_use_internal_errors(false);
   266 
   280 
   267         if (!$success) {
   281         if (!$success) {
   268             require_once 'Zend/Dom/Exception.php';
   282             require_once 'Zend/Dom/Exception.php';
   269             throw new Zend_Dom_Exception(sprintf('Error parsing document (type == %s)', $type));
   283             throw new Zend_Dom_Exception(sprintf('Error parsing document (type == %s)', $type));