PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/libxml - libxml.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 0 589 0.0 %
Date: 2014-04-16 Functions: 0 50 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 5                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1997-2014 The PHP Group                                |
       6             :    +----------------------------------------------------------------------+
       7             :    | This source file is subject to version 3.01 of the PHP license,      |
       8             :    | that is bundled with this package in the file LICENSE, and is        |
       9             :    | available through the world-wide-web at the following url:           |
      10             :    | http://www.php.net/license/3_01.txt                                  |
      11             :    | If you did not receive a copy of the PHP license and are unable to   |
      12             :    | obtain it through the world-wide-web, please send a note to          |
      13             :    | license@php.net so we can mail you a copy immediately.               |
      14             :    +----------------------------------------------------------------------+
      15             :    | Authors: Shane Caraveo <shane@php.net>                               |
      16             :    |          Wez Furlong <wez@thebrainroom.com>                          |
      17             :    +----------------------------------------------------------------------+
      18             :  */
      19             : 
      20             : /* $Id$ */
      21             : 
      22             : #define IS_EXT_MODULE
      23             : 
      24             : #ifdef HAVE_CONFIG_H
      25             : #include "config.h"
      26             : #endif
      27             : 
      28             : #include "php.h"
      29             : #include "SAPI.h"
      30             : 
      31             : #define PHP_XML_INTERNAL
      32             : #include "zend_variables.h"
      33             : #include "ext/standard/php_string.h"
      34             : #include "ext/standard/info.h"
      35             : #include "ext/standard/file.h"
      36             : 
      37             : #if HAVE_LIBXML
      38             : 
      39             : #include <libxml/parser.h>
      40             : #include <libxml/parserInternals.h>
      41             : #include <libxml/tree.h>
      42             : #include <libxml/uri.h>
      43             : #include <libxml/xmlerror.h>
      44             : #include <libxml/xmlsave.h>
      45             : #ifdef LIBXML_SCHEMAS_ENABLED
      46             : #include <libxml/relaxng.h>
      47             : #include <libxml/xmlschemas.h>
      48             : #endif
      49             : 
      50             : #include "php_libxml.h"
      51             : 
      52             : #define PHP_LIBXML_ERROR 0
      53             : #define PHP_LIBXML_CTX_ERROR 1
      54             : #define PHP_LIBXML_CTX_WARNING 2
      55             : 
      56             : /* a true global for initialization */
      57             : static int _php_libxml_initialized = 0;
      58             : static int _php_libxml_per_request_initialization = 1;
      59             : static xmlExternalEntityLoader _php_libxml_default_entity_loader;
      60             : 
      61             : typedef struct _php_libxml_func_handler {
      62             :         php_libxml_export_node export_func;
      63             : } php_libxml_func_handler;
      64             : 
      65             : static HashTable php_libxml_exports;
      66             : 
      67             : static ZEND_DECLARE_MODULE_GLOBALS(libxml)
      68             : static PHP_GINIT_FUNCTION(libxml);
      69             : 
      70             : static PHP_FUNCTION(libxml_set_streams_context);
      71             : static PHP_FUNCTION(libxml_use_internal_errors);
      72             : static PHP_FUNCTION(libxml_get_last_error);
      73             : static PHP_FUNCTION(libxml_clear_errors);
      74             : static PHP_FUNCTION(libxml_get_errors);
      75             : static PHP_FUNCTION(libxml_set_external_entity_loader);
      76             : static PHP_FUNCTION(libxml_disable_entity_loader);
      77             : 
      78             : static zend_class_entry *libxmlerror_class_entry;
      79             : 
      80             : /* {{{ dynamically loadable module stuff */
      81             : #ifdef COMPILE_DL_LIBXML
      82             : ZEND_GET_MODULE(libxml)
      83             : #endif /* COMPILE_DL_LIBXML */
      84             : /* }}} */
      85             : 
      86             : /* {{{ function prototypes */
      87             : static PHP_MINIT_FUNCTION(libxml);
      88             : static PHP_RINIT_FUNCTION(libxml);
      89             : static PHP_MSHUTDOWN_FUNCTION(libxml);
      90             : static PHP_MINFO_FUNCTION(libxml);
      91             : static int php_libxml_post_deactivate(void);
      92             : 
      93             : /* }}} */
      94             : 
      95             : /* {{{ arginfo */
      96             : ZEND_BEGIN_ARG_INFO(arginfo_libxml_set_streams_context, 0)
      97             :         ZEND_ARG_INFO(0, context)
      98             : ZEND_END_ARG_INFO()
      99             : 
     100             : ZEND_BEGIN_ARG_INFO_EX(arginfo_libxml_use_internal_errors, 0, 0, 0)
     101             :         ZEND_ARG_INFO(0, use_errors)
     102             : ZEND_END_ARG_INFO()
     103             : 
     104             : ZEND_BEGIN_ARG_INFO(arginfo_libxml_get_last_error, 0)
     105             : ZEND_END_ARG_INFO()
     106             : 
     107             : ZEND_BEGIN_ARG_INFO(arginfo_libxml_get_errors, 0)
     108             : ZEND_END_ARG_INFO()
     109             : 
     110             : ZEND_BEGIN_ARG_INFO(arginfo_libxml_clear_errors, 0)
     111             : ZEND_END_ARG_INFO()
     112             : 
     113             : ZEND_BEGIN_ARG_INFO_EX(arginfo_libxml_disable_entity_loader, 0, 0, 0)
     114             :         ZEND_ARG_INFO(0, disable)
     115             : ZEND_END_ARG_INFO()
     116             : 
     117             : ZEND_BEGIN_ARG_INFO_EX(arginfo_libxml_set_external_entity_loader, 0, 0, 1)
     118             :         ZEND_ARG_INFO(0, resolver_function)
     119             : ZEND_END_ARG_INFO()
     120             : /* }}} */
     121             : 
     122             : /* {{{ extension definition structures */
     123             : static const zend_function_entry libxml_functions[] = {
     124             :         PHP_FE(libxml_set_streams_context, arginfo_libxml_set_streams_context)
     125             :         PHP_FE(libxml_use_internal_errors, arginfo_libxml_use_internal_errors)
     126             :         PHP_FE(libxml_get_last_error, arginfo_libxml_get_last_error)
     127             :         PHP_FE(libxml_clear_errors, arginfo_libxml_clear_errors)
     128             :         PHP_FE(libxml_get_errors, arginfo_libxml_get_errors)
     129             :         PHP_FE(libxml_disable_entity_loader, arginfo_libxml_disable_entity_loader)
     130             :         PHP_FE(libxml_set_external_entity_loader, arginfo_libxml_set_external_entity_loader)
     131             :         PHP_FE_END
     132             : };
     133             : 
     134             : zend_module_entry libxml_module_entry = {
     135             :         STANDARD_MODULE_HEADER,
     136             :         "libxml",                /* extension name */
     137             :         libxml_functions,        /* extension function list */
     138             :         PHP_MINIT(libxml),       /* extension-wide startup function */
     139             :         PHP_MSHUTDOWN(libxml),   /* extension-wide shutdown function */
     140             :         PHP_RINIT(libxml),       /* per-request startup function */
     141             :         NULL,                    /* per-request shutdown function */
     142             :         PHP_MINFO(libxml),       /* information function */
     143             :         NO_VERSION_YET,
     144             :         PHP_MODULE_GLOBALS(libxml), /* globals descriptor */
     145             :         PHP_GINIT(libxml),          /* globals ctor */
     146             :         NULL,                       /* globals dtor */
     147             :         php_libxml_post_deactivate, /* post deactivate */
     148             :         STANDARD_MODULE_PROPERTIES_EX
     149             : };
     150             : 
     151             : /* }}} */
     152             : 
     153             : /* {{{ internal functions for interoperability */
     154           0 : static int php_libxml_clear_object(php_libxml_node_object *object TSRMLS_DC)
     155             : {
     156           0 :         if (object->properties) {
     157           0 :                 object->properties = NULL;
     158             :         }
     159           0 :         php_libxml_decrement_node_ptr(object TSRMLS_CC);
     160           0 :         return php_libxml_decrement_doc_ref(object TSRMLS_CC);
     161             : }
     162             : 
     163           0 : static int php_libxml_unregister_node(xmlNodePtr nodep TSRMLS_DC)
     164             : {
     165             :         php_libxml_node_object *wrapper;
     166             : 
     167           0 :         php_libxml_node_ptr *nodeptr = nodep->_private;
     168             : 
     169           0 :         if (nodeptr != NULL) {
     170           0 :                 wrapper = nodeptr->_private;
     171           0 :                 if (wrapper) {
     172           0 :                         php_libxml_clear_object(wrapper TSRMLS_CC);
     173             :                 } else {
     174           0 :                         if (nodeptr->node != NULL && nodeptr->node->type != XML_DOCUMENT_NODE) {
     175           0 :                                 nodeptr->node->_private = NULL;
     176             :                         }
     177           0 :                         nodeptr->node = NULL;
     178             :                 }
     179             :         }
     180             : 
     181           0 :         return -1;
     182             : }
     183             : 
     184           0 : static void php_libxml_node_free(xmlNodePtr node)
     185             : {
     186           0 :         if(node) {
     187           0 :                 if (node->_private != NULL) {
     188           0 :                         ((php_libxml_node_ptr *) node->_private)->node = NULL;
     189             :                 }
     190           0 :                 switch (node->type) {
     191             :                         case XML_ATTRIBUTE_NODE:
     192           0 :                                 xmlFreeProp((xmlAttrPtr) node);
     193           0 :                                 break;
     194             :                         case XML_ENTITY_DECL:
     195             :                         case XML_ELEMENT_DECL:
     196             :                         case XML_ATTRIBUTE_DECL:
     197           0 :                                 break;
     198             :                         case XML_NOTATION_NODE:
     199             :                                 /* These require special handling */
     200           0 :                                 if (node->name != NULL) {
     201           0 :                                         xmlFree((char *) node->name);
     202             :                                 }
     203           0 :                                 if (((xmlEntityPtr) node)->ExternalID != NULL) {
     204           0 :                                         xmlFree((char *) ((xmlEntityPtr) node)->ExternalID);
     205             :                                 }
     206           0 :                                 if (((xmlEntityPtr) node)->SystemID != NULL) {
     207           0 :                                         xmlFree((char *) ((xmlEntityPtr) node)->SystemID);
     208             :                                 }
     209           0 :                                 xmlFree(node);
     210           0 :                                 break;
     211             :                         case XML_NAMESPACE_DECL:
     212           0 :                                 if (node->ns) {
     213           0 :                                         xmlFreeNs(node->ns);
     214           0 :                                         node->ns = NULL;
     215             :                                 }
     216           0 :                                 node->type = XML_ELEMENT_NODE;
     217             :                         default:
     218           0 :                                 xmlFreeNode(node);
     219             :                 }
     220             :         }
     221           0 : }
     222             : 
     223           0 : static void php_libxml_node_free_list(xmlNodePtr node TSRMLS_DC)
     224             : {
     225             :         xmlNodePtr curnode;
     226             : 
     227           0 :         if (node != NULL) {
     228           0 :                 curnode = node;
     229           0 :                 while (curnode != NULL) {
     230           0 :                         node = curnode;
     231           0 :                         switch (node->type) {
     232             :                                 /* Skip property freeing for the following types */
     233             :                                 case XML_NOTATION_NODE:
     234             :                                 case XML_ENTITY_DECL:
     235           0 :                                         break;
     236             :                                 case XML_ENTITY_REF_NODE:
     237           0 :                                         php_libxml_node_free_list((xmlNodePtr) node->properties TSRMLS_CC);
     238           0 :                                         break;
     239             :                                 case XML_ATTRIBUTE_NODE:
     240           0 :                                                 if ((node->doc != NULL) && (((xmlAttrPtr) node)->atype == XML_ATTRIBUTE_ID)) {
     241           0 :                                                         xmlRemoveID(node->doc, (xmlAttrPtr) node);
     242             :                                                 }
     243             :                                 case XML_ATTRIBUTE_DECL:
     244             :                                 case XML_DTD_NODE:
     245             :                                 case XML_DOCUMENT_TYPE_NODE:
     246             :                                 case XML_NAMESPACE_DECL:
     247             :                                 case XML_TEXT_NODE:
     248           0 :                                         php_libxml_node_free_list(node->children TSRMLS_CC);
     249           0 :                                         break;
     250             :                                 default:
     251           0 :                                         php_libxml_node_free_list(node->children TSRMLS_CC);
     252           0 :                                         php_libxml_node_free_list((xmlNodePtr) node->properties TSRMLS_CC);
     253             :                         }
     254             : 
     255           0 :                         curnode = node->next;
     256           0 :                         xmlUnlinkNode(node);
     257           0 :                         if (php_libxml_unregister_node(node TSRMLS_CC) == 0) {
     258           0 :                                 node->doc = NULL;
     259             :                         }
     260           0 :                         php_libxml_node_free(node);
     261             :                 }
     262             :         }
     263           0 : }
     264             : 
     265             : /* }}} */
     266             : 
     267             : /* {{{ startup, shutdown and info functions */
     268           0 : static PHP_GINIT_FUNCTION(libxml)
     269             : {
     270           0 :         libxml_globals->stream_context = NULL;
     271           0 :         libxml_globals->error_buffer.c = NULL;
     272           0 :         libxml_globals->error_list = NULL;
     273           0 :         libxml_globals->entity_loader.fci.size = 0;
     274           0 :         libxml_globals->entity_loader_disabled = 0;
     275           0 : }
     276             : 
     277           0 : static void _php_libxml_destroy_fci(zend_fcall_info *fci)
     278             : {
     279           0 :         if (fci->size > 0) {
     280           0 :                 zval_ptr_dtor(&fci->function_name);
     281           0 :                 if (fci->object_ptr != NULL) {
     282           0 :                         zval_ptr_dtor(&fci->object_ptr);
     283             :                 }
     284           0 :                 fci->size = 0;
     285             :         }
     286           0 : }
     287             : 
     288             : /* Channel libxml file io layer through the PHP streams subsystem.
     289             :  * This allows use of ftps:// and https:// urls */
     290             : 
     291           0 : static void *php_libxml_streams_IO_open_wrapper(const char *filename, const char *mode, const int read_only)
     292             : {
     293             :         php_stream_statbuf ssbuf;
     294           0 :         php_stream_context *context = NULL;
     295           0 :         php_stream_wrapper *wrapper = NULL;
     296             :         char *resolved_path;
     297           0 :         const char *path_to_open = NULL;
     298           0 :         void *ret_val = NULL;
     299           0 :         int isescaped=0;
     300             :         xmlURI *uri;
     301             : 
     302             :         TSRMLS_FETCH();
     303             : 
     304           0 :         uri = xmlParseURI(filename);
     305           0 :         if (uri && (uri->scheme == NULL ||
     306           0 :                         (xmlStrncmp(BAD_CAST uri->scheme, BAD_CAST "file", 4) == 0))) {
     307           0 :                 resolved_path = xmlURIUnescapeString(filename, 0, NULL);
     308           0 :                 isescaped = 1;
     309             :         } else {
     310           0 :                 resolved_path = (char *)filename;
     311             :         }
     312             : 
     313           0 :         if (uri) {
     314           0 :                 xmlFreeURI(uri);
     315             :         }
     316             : 
     317           0 :         if (resolved_path == NULL) {
     318           0 :                 return NULL;
     319             :         }
     320             : 
     321             :         /* logic copied from _php_stream_stat, but we only want to fail
     322             :            if the wrapper supports stat, otherwise, figure it out from
     323             :            the open.  This logic is only to support hiding warnings
     324             :            that the streams layer puts out at times, but for libxml we
     325             :            may try to open files that don't exist, but it is not a failure
     326             :            in xml processing (eg. DTD files)  */
     327           0 :         wrapper = php_stream_locate_url_wrapper(resolved_path, &path_to_open, 0 TSRMLS_CC);
     328           0 :         if (wrapper && read_only && wrapper->wops->url_stat) {
     329           0 :                 if (wrapper->wops->url_stat(wrapper, path_to_open, PHP_STREAM_URL_STAT_QUIET, &ssbuf, NULL TSRMLS_CC) == -1) {
     330           0 :                         if (isescaped) {
     331           0 :                                 xmlFree(resolved_path);
     332             :                         }
     333           0 :                         return NULL;
     334             :                 }
     335             :         }
     336             : 
     337           0 :         context = php_stream_context_from_zval(LIBXML(stream_context), 0);
     338             :         
     339           0 :         ret_val = php_stream_open_wrapper_ex(path_to_open, (char *)mode, REPORT_ERRORS, NULL, context);
     340           0 :         if (isescaped) {
     341           0 :                 xmlFree(resolved_path);
     342             :         }
     343           0 :         return ret_val;
     344             : }
     345             : 
     346           0 : static void *php_libxml_streams_IO_open_read_wrapper(const char *filename)
     347             : {
     348           0 :         return php_libxml_streams_IO_open_wrapper(filename, "rb", 1);
     349             : }
     350             : 
     351           0 : static void *php_libxml_streams_IO_open_write_wrapper(const char *filename)
     352             : {
     353           0 :         return php_libxml_streams_IO_open_wrapper(filename, "wb", 0);
     354             : }
     355             : 
     356           0 : static int php_libxml_streams_IO_read(void *context, char *buffer, int len)
     357             : {
     358             :         TSRMLS_FETCH();
     359           0 :         return php_stream_read((php_stream*)context, buffer, len);
     360             : }
     361             : 
     362           0 : static int php_libxml_streams_IO_write(void *context, const char *buffer, int len)
     363             : {
     364             :         TSRMLS_FETCH();
     365           0 :         return php_stream_write((php_stream*)context, buffer, len);
     366             : }
     367             : 
     368           0 : static int php_libxml_streams_IO_close(void *context)
     369             : {
     370             :         TSRMLS_FETCH();
     371           0 :         return php_stream_close((php_stream*)context);
     372             : }
     373             : 
     374             : static xmlParserInputBufferPtr
     375           0 : php_libxml_input_buffer_create_filename(const char *URI, xmlCharEncoding enc)
     376             : {
     377             :         xmlParserInputBufferPtr ret;
     378           0 :         void *context = NULL;
     379             :         TSRMLS_FETCH();
     380             : 
     381           0 :         if (LIBXML(entity_loader_disabled)) {
     382           0 :                 return NULL;
     383             :         }
     384             : 
     385           0 :         if (URI == NULL)
     386           0 :                 return(NULL);
     387             : 
     388           0 :         context = php_libxml_streams_IO_open_read_wrapper(URI);
     389             : 
     390           0 :         if (context == NULL) {
     391           0 :                 return(NULL);
     392             :         }
     393             : 
     394             :         /* Allocate the Input buffer front-end. */
     395           0 :         ret = xmlAllocParserInputBuffer(enc);
     396           0 :         if (ret != NULL) {
     397           0 :                 ret->context = context;
     398           0 :                 ret->readcallback = php_libxml_streams_IO_read;
     399           0 :                 ret->closecallback = php_libxml_streams_IO_close;
     400             :         } else
     401           0 :                 php_libxml_streams_IO_close(context);
     402             : 
     403           0 :         return(ret);
     404             : }
     405             : 
     406             : static xmlOutputBufferPtr
     407           0 : php_libxml_output_buffer_create_filename(const char *URI,
     408             :                               xmlCharEncodingHandlerPtr encoder,
     409             :                               int compression ATTRIBUTE_UNUSED)
     410             : {
     411             :         xmlOutputBufferPtr ret;
     412             :         xmlURIPtr puri;
     413           0 :         void *context = NULL;
     414           0 :         char *unescaped = NULL;
     415             : 
     416           0 :         if (URI == NULL)
     417           0 :                 return(NULL);
     418             : 
     419           0 :         puri = xmlParseURI(URI);
     420           0 :         if (puri != NULL) {
     421           0 :                 if (puri->scheme != NULL)
     422           0 :                         unescaped = xmlURIUnescapeString(URI, 0, NULL);
     423           0 :                 xmlFreeURI(puri);
     424             :         }
     425             : 
     426           0 :         if (unescaped != NULL) {
     427           0 :                 context = php_libxml_streams_IO_open_write_wrapper(unescaped);
     428           0 :                 xmlFree(unescaped);
     429             :         }
     430             : 
     431             :         /* try with a non-escaped URI this may be a strange filename */
     432           0 :         if (context == NULL) {
     433           0 :                 context = php_libxml_streams_IO_open_write_wrapper(URI);
     434             :         }
     435             : 
     436           0 :         if (context == NULL) {
     437           0 :                 return(NULL);
     438             :         }
     439             : 
     440             :         /* Allocate the Output buffer front-end. */
     441           0 :         ret = xmlAllocOutputBuffer(encoder);
     442           0 :         if (ret != NULL) {
     443           0 :                 ret->context = context;
     444           0 :                 ret->writecallback = php_libxml_streams_IO_write;
     445           0 :                 ret->closecallback = php_libxml_streams_IO_close;
     446             :         }
     447             : 
     448           0 :         return(ret);
     449             : }
     450             : 
     451           0 : static int _php_libxml_free_error(xmlErrorPtr error)
     452             : {
     453             :         /* This will free the libxml alloc'd memory */
     454           0 :         xmlResetError(error);
     455           0 :         return 1;
     456             : }
     457             : 
     458           0 : static void _php_list_set_error_structure(xmlErrorPtr error, const char *msg)
     459             : {
     460             :         xmlError error_copy;
     461             :         int ret;
     462             : 
     463             :         TSRMLS_FETCH();
     464             : 
     465           0 :         memset(&error_copy, 0, sizeof(xmlError));
     466             : 
     467           0 :         if (error) {
     468           0 :                 ret = xmlCopyError(error, &error_copy);
     469             :         } else {
     470           0 :                 error_copy.domain = 0;
     471           0 :                 error_copy.code = XML_ERR_INTERNAL_ERROR;
     472           0 :                 error_copy.level = XML_ERR_ERROR;
     473           0 :                 error_copy.line = 0;
     474           0 :                 error_copy.node = NULL;
     475           0 :                 error_copy.int1 = 0;
     476           0 :                 error_copy.int2 = 0;
     477           0 :                 error_copy.ctxt = NULL;
     478           0 :                 error_copy.message = xmlStrdup(msg);
     479           0 :                 error_copy.file = NULL;
     480           0 :                 error_copy.str1 = NULL;
     481           0 :                 error_copy.str2 = NULL;
     482           0 :                 error_copy.str3 = NULL;
     483           0 :                 ret = 0;
     484             :         }
     485             : 
     486           0 :         if (ret == 0) {
     487           0 :                 zend_llist_add_element(LIBXML(error_list), &error_copy);
     488             :         }
     489           0 : }
     490             : 
     491           0 : static void php_libxml_ctx_error_level(int level, void *ctx, const char *msg TSRMLS_DC)
     492             : {
     493             :         xmlParserCtxtPtr parser;
     494             : 
     495           0 :         parser = (xmlParserCtxtPtr) ctx;
     496             : 
     497           0 :         if (parser != NULL && parser->input != NULL) {
     498           0 :                 if (parser->input->filename) {
     499           0 :                         php_error_docref(NULL TSRMLS_CC, level, "%s in %s, line: %d", msg, parser->input->filename, parser->input->line);
     500             :                 } else {
     501           0 :                         php_error_docref(NULL TSRMLS_CC, level, "%s in Entity, line: %d", msg, parser->input->line);
     502             :                 }
     503             :         }
     504           0 : }
     505             : 
     506           0 : void php_libxml_issue_error(int level, const char *msg TSRMLS_DC)
     507             : {
     508           0 :         if (LIBXML(error_list)) {
     509           0 :                 _php_list_set_error_structure(NULL, msg);
     510             :         } else {
     511           0 :                 php_error_docref(NULL TSRMLS_CC, level, "%s", msg);
     512             :         }
     513           0 : }
     514             : 
     515           0 : static void php_libxml_internal_error_handler(int error_type, void *ctx, const char **msg, va_list ap)
     516             : {
     517             :         char *buf;
     518           0 :         int len, len_iter, output = 0;
     519             : 
     520             :         TSRMLS_FETCH();
     521             : 
     522           0 :         len = vspprintf(&buf, 0, *msg, ap);
     523           0 :         len_iter = len;
     524             : 
     525             :         /* remove any trailing \n */
     526           0 :         while (len_iter && buf[--len_iter] == '\n') {
     527           0 :                 buf[len_iter] = '\0';
     528           0 :                 output = 1;
     529             :         }
     530             : 
     531           0 :         smart_str_appendl(&LIBXML(error_buffer), buf, len);
     532             : 
     533           0 :         efree(buf);
     534             : 
     535           0 :         if (output == 1) {
     536           0 :                 if (LIBXML(error_list)) {
     537           0 :                         _php_list_set_error_structure(NULL, LIBXML(error_buffer).c);
     538             :                 } else {
     539           0 :                         switch (error_type) {
     540             :                                 case PHP_LIBXML_CTX_ERROR:
     541           0 :                                         php_libxml_ctx_error_level(E_WARNING, ctx, LIBXML(error_buffer).c TSRMLS_CC);
     542           0 :                                         break;
     543             :                                 case PHP_LIBXML_CTX_WARNING:
     544           0 :                                         php_libxml_ctx_error_level(E_NOTICE, ctx, LIBXML(error_buffer).c TSRMLS_CC);
     545           0 :                                         break;
     546             :                                 default:
     547           0 :                                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", LIBXML(error_buffer).c);
     548             :                         }
     549             :                 }
     550           0 :                 smart_str_free(&LIBXML(error_buffer));
     551             :         }
     552           0 : }
     553             : 
     554           0 : static xmlParserInputPtr _php_libxml_external_entity_loader(const char *URL,
     555             :                 const char *ID, xmlParserCtxtPtr context)
     556             : {
     557           0 :         xmlParserInputPtr       ret                     = NULL;
     558           0 :         const char                      *resource       = NULL;
     559           0 :         zval                            *public         = NULL,
     560           0 :                                                 *system         = NULL,
     561           0 :                                                 *ctxzv          = NULL,
     562           0 :                                                 **params[]      = {&public, &system, &ctxzv},
     563           0 :                                                 *retval_ptr     = NULL;
     564             :         int                                     retval;
     565             :         zend_fcall_info         *fci;
     566             :         TSRMLS_FETCH();
     567             : 
     568           0 :         fci = &LIBXML(entity_loader).fci;
     569             :         
     570           0 :         if (fci->size == 0) {
     571             :                 /* no custom user-land callback set up; delegate to original loader */
     572           0 :                 return _php_libxml_default_entity_loader(URL, ID, context);
     573             :         }
     574             : 
     575           0 :         ALLOC_INIT_ZVAL(public);
     576           0 :         if (ID != NULL) {
     577           0 :                 ZVAL_STRING(public, ID, 1);
     578             :         }
     579           0 :         ALLOC_INIT_ZVAL(system);
     580           0 :         if (URL != NULL) {
     581           0 :                 ZVAL_STRING(system, URL, 1);
     582             :         }
     583           0 :         MAKE_STD_ZVAL(ctxzv);
     584           0 :         array_init_size(ctxzv, 4);
     585             : 
     586             : #define ADD_NULL_OR_STRING_KEY(memb) \
     587             :         if (context->memb == NULL) { \
     588             :                 add_assoc_null_ex(ctxzv, #memb, sizeof(#memb)); \
     589             :         } else { \
     590             :                 add_assoc_string_ex(ctxzv, #memb, sizeof(#memb), \
     591             :                                 (char *)context->memb, 1); \
     592             :         }
     593             :         
     594           0 :         ADD_NULL_OR_STRING_KEY(directory)
     595           0 :         ADD_NULL_OR_STRING_KEY(intSubName)
     596           0 :         ADD_NULL_OR_STRING_KEY(extSubURI)
     597           0 :         ADD_NULL_OR_STRING_KEY(extSubSystem)
     598             :         
     599             : #undef ADD_NULL_OR_STRING_KEY
     600             :         
     601           0 :         fci->retval_ptr_ptr  = &retval_ptr;
     602           0 :         fci->params                  = params;
     603           0 :         fci->param_count     = sizeof(params)/sizeof(*params);
     604           0 :         fci->no_separation   = 1;
     605             :         
     606           0 :         retval = zend_call_function(fci, &LIBXML(entity_loader).fcc TSRMLS_CC);
     607           0 :         if (retval != SUCCESS || fci->retval_ptr_ptr == NULL) {
     608           0 :                 php_libxml_ctx_error(context,
     609             :                                 "Call to user entity loader callback '%s' has failed",
     610             :                                 fci->function_name);
     611             :         } else {
     612           0 :                 retval_ptr = *fci->retval_ptr_ptr;
     613           0 :                 if (retval_ptr == NULL) {
     614           0 :                         php_libxml_ctx_error(context,
     615             :                                         "Call to user entity loader callback '%s' has failed; "
     616             :                                         "probably it has thrown an exception",
     617             :                                         fci->function_name);
     618           0 :                 } else if (Z_TYPE_P(retval_ptr) == IS_STRING) {
     619             : is_string:
     620           0 :                         resource = Z_STRVAL_P(retval_ptr);
     621           0 :                 } else if (Z_TYPE_P(retval_ptr) == IS_RESOURCE) {
     622             :                         php_stream *stream;
     623           0 :                         php_stream_from_zval_no_verify(stream, &retval_ptr);
     624           0 :                         if (stream == NULL) {
     625           0 :                                 php_libxml_ctx_error(context,
     626             :                                                 "The user entity loader callback '%s' has returned a "
     627             :                                                 "resource, but it is not a stream",
     628             :                                                 fci->function_name);
     629             :                         } else {
     630             :                                 /* TODO: allow storing the encoding in the stream context? */
     631           0 :                                 xmlCharEncoding enc = XML_CHAR_ENCODING_NONE;
     632           0 :                                 xmlParserInputBufferPtr pib = xmlAllocParserInputBuffer(enc);
     633           0 :                                 if (pib == NULL) {
     634           0 :                                         php_libxml_ctx_error(context, "Could not allocate parser "
     635             :                                                         "input buffer");
     636             :                                 } else {
     637             :                                         /* make stream not being closed when the zval is freed */
     638           0 :                                         zend_list_addref(stream->rsrc_id);
     639           0 :                                         pib->context = stream;
     640           0 :                                         pib->readcallback = php_libxml_streams_IO_read;
     641           0 :                                         pib->closecallback = php_libxml_streams_IO_close;
     642             :                                         
     643           0 :                                         ret = xmlNewIOInputStream(context, pib, enc);
     644           0 :                                         if (ret == NULL) {
     645           0 :                                                 xmlFreeParserInputBuffer(pib);
     646             :                                         }
     647             :                                 }
     648             :                         }
     649           0 :                 } else if (Z_TYPE_P(retval_ptr) != IS_NULL) {
     650             :                         /* retval not string nor resource nor null; convert to string */
     651           0 :                         SEPARATE_ZVAL(&retval_ptr);
     652           0 :                         convert_to_string(retval_ptr);
     653           0 :                         goto is_string;
     654             :                 } /* else is null; don't try anything */
     655             :         }
     656             : 
     657           0 :         if (ret == NULL) {
     658           0 :                 if (resource == NULL) {
     659           0 :                         if (ID == NULL) {
     660           0 :                                 ID = "NULL";
     661             :                         }
     662           0 :                         php_libxml_ctx_error(context,
     663             :                                         "Failed to load external entity \"%s\"\n", ID);
     664             :                 } else {
     665             :                         /* we got the resource in the form of a string; open it */
     666           0 :                         ret = xmlNewInputFromFile(context, resource);
     667             :                 }
     668             :         }
     669             : 
     670           0 :         zval_ptr_dtor(&public);
     671           0 :         zval_ptr_dtor(&system);
     672           0 :         zval_ptr_dtor(&ctxzv);
     673           0 :         if (retval_ptr != NULL) {
     674           0 :                 zval_ptr_dtor(&retval_ptr);
     675             :         }
     676           0 :         return ret;
     677             : }
     678             : 
     679           0 : static xmlParserInputPtr _php_libxml_pre_ext_ent_loader(const char *URL,
     680             :                 const char *ID, xmlParserCtxtPtr context)
     681             : {
     682             :         TSRMLS_FETCH();
     683             : 
     684             :         /* Check whether we're running in a PHP context, since the entity loader
     685             :          * we've defined is an application level (true global) setting.
     686             :          * If we are, we also want to check whether we've finished activating
     687             :          * the modules (RINIT phase). Using our external entity loader during a
     688             :          * RINIT should not be problem per se (though during MINIT it is, because
     689             :          * we don't even have a resource list by then), but then whether one
     690             :          * extension would be using the custom external entity loader or not
     691             :          * could depend on extension loading order
     692             :          * (if _php_libxml_per_request_initialization */
     693           0 :         if (xmlGenericError == php_libxml_error_handler && PG(modules_activated)) {
     694           0 :                 return _php_libxml_external_entity_loader(URL, ID, context);
     695             :         } else {
     696           0 :                 return _php_libxml_default_entity_loader(URL, ID, context);
     697             :         }
     698             : }
     699             : 
     700           0 : PHP_LIBXML_API void php_libxml_ctx_error(void *ctx, const char *msg, ...)
     701             : {
     702             :         va_list args;
     703           0 :         va_start(args, msg);
     704           0 :         php_libxml_internal_error_handler(PHP_LIBXML_CTX_ERROR, ctx, &msg, args);
     705           0 :         va_end(args);
     706           0 : }
     707             : 
     708           0 : PHP_LIBXML_API void php_libxml_ctx_warning(void *ctx, const char *msg, ...)
     709             : {
     710             :         va_list args;
     711           0 :         va_start(args, msg);
     712           0 :         php_libxml_internal_error_handler(PHP_LIBXML_CTX_WARNING, ctx, &msg, args);
     713           0 :         va_end(args);
     714           0 : }
     715             : 
     716           0 : PHP_LIBXML_API void php_libxml_structured_error_handler(void *userData, xmlErrorPtr error)
     717             : {
     718           0 :         _php_list_set_error_structure(error, NULL);
     719             : 
     720           0 :         return;
     721             : }
     722             : 
     723           0 : PHP_LIBXML_API void php_libxml_error_handler(void *ctx, const char *msg, ...)
     724             : {
     725             :         va_list args;
     726           0 :         va_start(args, msg);
     727           0 :         php_libxml_internal_error_handler(PHP_LIBXML_ERROR, ctx, &msg, args);
     728           0 :         va_end(args);
     729           0 : }
     730             : 
     731             : 
     732           0 : PHP_LIBXML_API void php_libxml_initialize(void)
     733             : {
     734           0 :         if (!_php_libxml_initialized) {
     735             :                 /* we should be the only one's to ever init!! */
     736           0 :                 xmlInitParser();
     737             :                 
     738           0 :                 _php_libxml_default_entity_loader = xmlGetExternalEntityLoader();
     739           0 :                 xmlSetExternalEntityLoader(_php_libxml_pre_ext_ent_loader);
     740             : 
     741           0 :                 zend_hash_init(&php_libxml_exports, 0, NULL, NULL, 1);
     742             : 
     743           0 :                 _php_libxml_initialized = 1;
     744             :         }
     745           0 : }
     746             : 
     747           0 : PHP_LIBXML_API void php_libxml_shutdown(void)
     748             : {
     749           0 :         if (_php_libxml_initialized) {
     750             : #if defined(LIBXML_SCHEMAS_ENABLED)
     751           0 :                 xmlRelaxNGCleanupTypes();
     752             : #endif
     753           0 :                 xmlCleanupParser();
     754           0 :                 zend_hash_destroy(&php_libxml_exports);
     755             :                 
     756           0 :                 xmlSetExternalEntityLoader(_php_libxml_default_entity_loader);
     757           0 :                 _php_libxml_initialized = 0;
     758             :         }
     759           0 : }
     760             : 
     761           0 : PHP_LIBXML_API zval *php_libxml_switch_context(zval *context TSRMLS_DC)
     762             : {
     763             :         zval *oldcontext;
     764             : 
     765           0 :         oldcontext = LIBXML(stream_context);
     766           0 :         LIBXML(stream_context) = context;
     767           0 :         return oldcontext;
     768             : 
     769             : }
     770             : 
     771           0 : static PHP_MINIT_FUNCTION(libxml)
     772             : {
     773             :         zend_class_entry ce;
     774             : 
     775           0 :         php_libxml_initialize();
     776             : 
     777           0 :         REGISTER_LONG_CONSTANT("LIBXML_VERSION",                      LIBXML_VERSION,                 CONST_CS | CONST_PERSISTENT);
     778           0 :         REGISTER_STRING_CONSTANT("LIBXML_DOTTED_VERSION",     LIBXML_DOTTED_VERSION,  CONST_CS | CONST_PERSISTENT);
     779           0 :         REGISTER_STRING_CONSTANT("LIBXML_LOADED_VERSION",     (char *)xmlParserVersion,               CONST_CS | CONST_PERSISTENT);
     780             : 
     781             :         /* For use with loading xml */
     782           0 :         REGISTER_LONG_CONSTANT("LIBXML_NOENT",                XML_PARSE_NOENT,                CONST_CS | CONST_PERSISTENT);
     783           0 :         REGISTER_LONG_CONSTANT("LIBXML_DTDLOAD",      XML_PARSE_DTDLOAD,              CONST_CS | CONST_PERSISTENT);
     784           0 :         REGISTER_LONG_CONSTANT("LIBXML_DTDATTR",      XML_PARSE_DTDATTR,              CONST_CS | CONST_PERSISTENT);
     785           0 :         REGISTER_LONG_CONSTANT("LIBXML_DTDVALID",     XML_PARSE_DTDVALID,             CONST_CS | CONST_PERSISTENT);
     786           0 :         REGISTER_LONG_CONSTANT("LIBXML_NOERROR",      XML_PARSE_NOERROR,              CONST_CS | CONST_PERSISTENT);
     787           0 :         REGISTER_LONG_CONSTANT("LIBXML_NOWARNING",    XML_PARSE_NOWARNING,    CONST_CS | CONST_PERSISTENT);
     788           0 :         REGISTER_LONG_CONSTANT("LIBXML_NOBLANKS",     XML_PARSE_NOBLANKS,             CONST_CS | CONST_PERSISTENT);
     789           0 :         REGISTER_LONG_CONSTANT("LIBXML_XINCLUDE",     XML_PARSE_XINCLUDE,             CONST_CS | CONST_PERSISTENT);
     790           0 :         REGISTER_LONG_CONSTANT("LIBXML_NSCLEAN",      XML_PARSE_NSCLEAN,              CONST_CS | CONST_PERSISTENT);
     791           0 :         REGISTER_LONG_CONSTANT("LIBXML_NOCDATA",      XML_PARSE_NOCDATA,              CONST_CS | CONST_PERSISTENT);
     792           0 :         REGISTER_LONG_CONSTANT("LIBXML_NONET",                XML_PARSE_NONET,                CONST_CS | CONST_PERSISTENT);
     793           0 :         REGISTER_LONG_CONSTANT("LIBXML_PEDANTIC",     XML_PARSE_PEDANTIC,             CONST_CS | CONST_PERSISTENT);
     794             : #if LIBXML_VERSION >= 20621
     795           0 :         REGISTER_LONG_CONSTANT("LIBXML_COMPACT",      XML_PARSE_COMPACT,              CONST_CS | CONST_PERSISTENT);
     796           0 :         REGISTER_LONG_CONSTANT("LIBXML_NOXMLDECL",    XML_SAVE_NO_DECL,               CONST_CS | CONST_PERSISTENT);
     797             : #endif
     798             : #if LIBXML_VERSION >= 20703
     799           0 :         REGISTER_LONG_CONSTANT("LIBXML_PARSEHUGE",    XML_PARSE_HUGE,                 CONST_CS | CONST_PERSISTENT);
     800             : #endif
     801           0 :         REGISTER_LONG_CONSTANT("LIBXML_NOEMPTYTAG",   LIBXML_SAVE_NOEMPTYTAG, CONST_CS | CONST_PERSISTENT);
     802             : 
     803             :         /* Schema validation options */
     804             : #if defined(LIBXML_SCHEMAS_ENABLED) && LIBXML_VERSION >= 20614
     805           0 :         REGISTER_LONG_CONSTANT("LIBXML_SCHEMA_CREATE",        XML_SCHEMA_VAL_VC_I_CREATE,     CONST_CS | CONST_PERSISTENT);
     806             : #endif
     807             : 
     808             :         /* Additional constants for use with loading html */
     809             : #if LIBXML_VERSION >= 20707
     810             :         REGISTER_LONG_CONSTANT("LIBXML_HTML_NOIMPLIED",       HTML_PARSE_NOIMPLIED,           CONST_CS | CONST_PERSISTENT);
     811             : #endif
     812             : 
     813             : #if LIBXML_VERSION >= 20708
     814             :         REGISTER_LONG_CONSTANT("LIBXML_HTML_NODEFDTD",        HTML_PARSE_NODEFDTD,            CONST_CS | CONST_PERSISTENT);
     815             : #endif
     816             : 
     817             :         /* Error levels */
     818           0 :         REGISTER_LONG_CONSTANT("LIBXML_ERR_NONE",             XML_ERR_NONE,           CONST_CS | CONST_PERSISTENT);
     819           0 :         REGISTER_LONG_CONSTANT("LIBXML_ERR_WARNING",  XML_ERR_WARNING,        CONST_CS | CONST_PERSISTENT);
     820           0 :         REGISTER_LONG_CONSTANT("LIBXML_ERR_ERROR",            XML_ERR_ERROR,          CONST_CS | CONST_PERSISTENT);
     821           0 :         REGISTER_LONG_CONSTANT("LIBXML_ERR_FATAL",            XML_ERR_FATAL,          CONST_CS | CONST_PERSISTENT);
     822             : 
     823           0 :         INIT_CLASS_ENTRY(ce, "LibXMLError", NULL);
     824           0 :         libxmlerror_class_entry = zend_register_internal_class(&ce TSRMLS_CC);
     825             : 
     826           0 :         if (sapi_module.name) {
     827             :                 static const char * const supported_sapis[] = {
     828             :                         "cgi-fcgi",
     829             :                         "fpm-fcgi",
     830             :                         "litespeed",
     831             :                         NULL
     832             :                 };
     833             :                 const char * const *sapi_name;
     834             : 
     835           0 :                 for (sapi_name = supported_sapis; *sapi_name; sapi_name++) {
     836           0 :                         if (strcmp(sapi_module.name, *sapi_name) == 0) {
     837           0 :                                 _php_libxml_per_request_initialization = 0;
     838           0 :                                 break;
     839             :                         }
     840             :                 }
     841             :         }
     842             : 
     843           0 :         if (!_php_libxml_per_request_initialization) {
     844             :                 /* report errors via handler rather than stderr */
     845           0 :                 xmlSetGenericErrorFunc(NULL, php_libxml_error_handler);
     846           0 :                 xmlParserInputBufferCreateFilenameDefault(php_libxml_input_buffer_create_filename);
     847           0 :                 xmlOutputBufferCreateFilenameDefault(php_libxml_output_buffer_create_filename);
     848             :         }
     849             :         
     850           0 :         return SUCCESS;
     851             : }
     852             : 
     853             : 
     854           0 : static PHP_RINIT_FUNCTION(libxml)
     855             : {
     856           0 :         if (_php_libxml_per_request_initialization) {
     857             :                 /* report errors via handler rather than stderr */
     858           0 :                 xmlSetGenericErrorFunc(NULL, php_libxml_error_handler);
     859           0 :                 xmlParserInputBufferCreateFilenameDefault(php_libxml_input_buffer_create_filename);
     860           0 :                 xmlOutputBufferCreateFilenameDefault(php_libxml_output_buffer_create_filename);
     861             :         }
     862           0 :         return SUCCESS;
     863             : }
     864             : 
     865             : 
     866           0 : static PHP_MSHUTDOWN_FUNCTION(libxml)
     867             : {
     868           0 :         if (!_php_libxml_per_request_initialization) {
     869           0 :                 xmlSetGenericErrorFunc(NULL, NULL);
     870             : 
     871           0 :                 xmlParserInputBufferCreateFilenameDefault(NULL);
     872           0 :                 xmlOutputBufferCreateFilenameDefault(NULL);
     873             :         }
     874           0 :         php_libxml_shutdown();
     875             : 
     876           0 :         return SUCCESS;
     877             : }
     878             : 
     879           0 : static int php_libxml_post_deactivate(void)
     880             : {
     881             :         TSRMLS_FETCH();
     882             :         /* reset libxml generic error handling */
     883           0 :         if (_php_libxml_per_request_initialization) {
     884           0 :                 xmlSetGenericErrorFunc(NULL, NULL);
     885             : 
     886           0 :                 xmlParserInputBufferCreateFilenameDefault(NULL);
     887           0 :                 xmlOutputBufferCreateFilenameDefault(NULL);
     888             :         }
     889           0 :         xmlSetStructuredErrorFunc(NULL, NULL);
     890             : 
     891           0 :         if (LIBXML(stream_context)) {
     892             :                 /* the steam_context resource will be released by resource list destructor */
     893           0 :                 efree(LIBXML(stream_context));
     894           0 :                 LIBXML(stream_context) = NULL;
     895             :         }
     896           0 :         smart_str_free(&LIBXML(error_buffer));
     897           0 :         if (LIBXML(error_list)) {
     898           0 :                 zend_llist_destroy(LIBXML(error_list));
     899           0 :                 efree(LIBXML(error_list));
     900           0 :                 LIBXML(error_list) = NULL;
     901             :         }
     902           0 :         xmlResetLastError();
     903             :         
     904           0 :         _php_libxml_destroy_fci(&LIBXML(entity_loader).fci);
     905             : 
     906           0 :         return SUCCESS;
     907             : }
     908             : 
     909             : 
     910           0 : static PHP_MINFO_FUNCTION(libxml)
     911             : {
     912           0 :         php_info_print_table_start();
     913           0 :         php_info_print_table_row(2, "libXML support", "active");
     914           0 :         php_info_print_table_row(2, "libXML Compiled Version", LIBXML_DOTTED_VERSION);
     915           0 :         php_info_print_table_row(2, "libXML Loaded Version", (char *)xmlParserVersion);
     916           0 :         php_info_print_table_row(2, "libXML streams", "enabled");
     917           0 :         php_info_print_table_end();
     918           0 : }
     919             : /* }}} */
     920             : 
     921             : /* {{{ proto void libxml_set_streams_context(resource streams_context) 
     922             :    Set the streams context for the next libxml document load or write */
     923           0 : static PHP_FUNCTION(libxml_set_streams_context)
     924             : {
     925             :         zval *arg;
     926             : 
     927           0 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &arg) == FAILURE) {
     928           0 :                 return;
     929             :         }
     930           0 :         if (LIBXML(stream_context)) {
     931           0 :                 zval_ptr_dtor(&LIBXML(stream_context));
     932           0 :                 LIBXML(stream_context) = NULL;
     933             :         }
     934           0 :         Z_ADDREF_P(arg);
     935           0 :         LIBXML(stream_context) = arg;
     936             : }
     937             : /* }}} */
     938             : 
     939             : /* {{{ proto bool libxml_use_internal_errors([boolean use_errors]) 
     940             :    Disable libxml errors and allow user to fetch error information as needed */
     941           0 : static PHP_FUNCTION(libxml_use_internal_errors)
     942             : {
     943             :         xmlStructuredErrorFunc current_handler;
     944           0 :         zend_bool use_errors=0, retval;
     945             : 
     946           0 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &use_errors) == FAILURE) {
     947           0 :                 return;
     948             :         }
     949             : 
     950           0 :         current_handler = xmlStructuredError;
     951           0 :         if (current_handler && current_handler == php_libxml_structured_error_handler) {
     952           0 :                 retval = 1;
     953             :         } else {
     954           0 :                 retval = 0;
     955             :         }
     956             : 
     957           0 :         if (ZEND_NUM_ARGS() == 0) {
     958           0 :                 RETURN_BOOL(retval);
     959             :         }
     960             : 
     961           0 :         if (use_errors == 0) {
     962           0 :                 xmlSetStructuredErrorFunc(NULL, NULL);
     963           0 :                 if (LIBXML(error_list)) {
     964           0 :                         zend_llist_destroy(LIBXML(error_list));
     965           0 :                         efree(LIBXML(error_list));
     966           0 :                         LIBXML(error_list) = NULL;
     967             :                 }
     968             :         } else {
     969           0 :                 xmlSetStructuredErrorFunc(NULL, php_libxml_structured_error_handler);
     970           0 :                 if (LIBXML(error_list) == NULL) {
     971           0 :                         LIBXML(error_list) = (zend_llist *) emalloc(sizeof(zend_llist));
     972           0 :                         zend_llist_init(LIBXML(error_list), sizeof(xmlError), (llist_dtor_func_t) _php_libxml_free_error, 0);
     973             :                 }
     974             :         }
     975           0 :         RETURN_BOOL(retval);
     976             : }
     977             : /* }}} */
     978             : 
     979             : /* {{{ proto object libxml_get_last_error() 
     980             :    Retrieve last error from libxml */
     981           0 : static PHP_FUNCTION(libxml_get_last_error)
     982             : {
     983             :         xmlErrorPtr error;
     984             : 
     985           0 :         error = xmlGetLastError();
     986             :         
     987           0 :         if (error) {
     988           0 :                 object_init_ex(return_value, libxmlerror_class_entry);
     989           0 :                 add_property_long(return_value, "level", error->level);
     990           0 :                 add_property_long(return_value, "code", error->code);
     991           0 :                 add_property_long(return_value, "column", error->int2);
     992           0 :                 if (error->message) {
     993           0 :                         add_property_string(return_value, "message", error->message, 1);
     994             :                 } else {
     995           0 :                         add_property_stringl(return_value, "message", "", 0, 1);
     996             :                 }
     997           0 :                 if (error->file) {
     998           0 :                         add_property_string(return_value, "file", error->file, 1);
     999             :                 } else {
    1000           0 :                         add_property_stringl(return_value, "file", "", 0, 1);
    1001             :                 }
    1002           0 :                 add_property_long(return_value, "line", error->line);
    1003             :         } else {
    1004           0 :                 RETURN_FALSE;
    1005             :         }
    1006             : }
    1007             : /* }}} */
    1008             : 
    1009             : /* {{{ proto object libxml_get_errors()
    1010             :    Retrieve array of errors */
    1011           0 : static PHP_FUNCTION(libxml_get_errors)
    1012             : {
    1013             :         
    1014             :         xmlErrorPtr error;
    1015             : 
    1016           0 :         if (array_init(return_value) == FAILURE) {
    1017           0 :                 RETURN_FALSE;
    1018             :         }
    1019             : 
    1020           0 :         if (LIBXML(error_list)) {
    1021             : 
    1022           0 :                 error = zend_llist_get_first(LIBXML(error_list));
    1023             : 
    1024           0 :                 while (error != NULL) {
    1025             :                         zval *z_error;
    1026           0 :                         MAKE_STD_ZVAL(z_error);
    1027             : 
    1028           0 :                         object_init_ex(z_error, libxmlerror_class_entry);
    1029           0 :                         add_property_long(z_error, "level", error->level);
    1030           0 :                         add_property_long(z_error, "code", error->code);
    1031           0 :                         add_property_long(z_error, "column", error->int2);
    1032           0 :                         if (error->message) {
    1033           0 :                                 add_property_string(z_error, "message", error->message, 1);
    1034             :                         } else {
    1035           0 :                                 add_property_stringl(z_error, "message", "", 0, 1);
    1036             :                         }
    1037           0 :                         if (error->file) {
    1038           0 :                                 add_property_string(z_error, "file", error->file, 1);
    1039             :                         } else {
    1040           0 :                                 add_property_stringl(z_error, "file", "", 0, 1);
    1041             :                         }
    1042           0 :                         add_property_long(z_error, "line", error->line);
    1043           0 :                         add_next_index_zval(return_value, z_error);
    1044             : 
    1045           0 :                         error = zend_llist_get_next(LIBXML(error_list));
    1046             :                 }
    1047             :         }
    1048             : }
    1049             : /* }}} */
    1050             : 
    1051             : /* {{{ proto void libxml_clear_errors() 
    1052             :    Clear last error from libxml */
    1053           0 : static PHP_FUNCTION(libxml_clear_errors)
    1054             : {
    1055           0 :         xmlResetLastError();
    1056           0 :         if (LIBXML(error_list)) {
    1057           0 :                 zend_llist_clean(LIBXML(error_list));
    1058             :         }
    1059           0 : }
    1060             : /* }}} */
    1061             : 
    1062           0 : PHP_LIBXML_API zend_bool php_libxml_disable_entity_loader(zend_bool disable TSRMLS_DC)
    1063             : {
    1064           0 :         zend_bool old = LIBXML(entity_loader_disabled);
    1065             : 
    1066           0 :         LIBXML(entity_loader_disabled) = disable;
    1067           0 :         return old;
    1068             : }
    1069             : 
    1070             : /* {{{ proto bool libxml_disable_entity_loader([boolean disable]) 
    1071             :    Disable/Enable ability to load external entities */
    1072           0 : static PHP_FUNCTION(libxml_disable_entity_loader)
    1073             : {
    1074           0 :         zend_bool disable = 1;
    1075             : 
    1076           0 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &disable) == FAILURE) {
    1077           0 :                 return;
    1078             :         }
    1079             : 
    1080           0 :         RETURN_BOOL(php_libxml_disable_entity_loader(disable TSRMLS_CC));
    1081             : }
    1082             : /* }}} */
    1083             : 
    1084             : /* {{{ proto void libxml_set_external_entity_loader(callback resolver_function) 
    1085             :    Changes the default external entity loader */
    1086           0 : static PHP_FUNCTION(libxml_set_external_entity_loader)
    1087             : {
    1088             :         zend_fcall_info                 fci;
    1089             :         zend_fcall_info_cache   fcc;
    1090           0 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "f!", &fci, &fcc)
    1091             :                         == FAILURE) {
    1092           0 :                 return;
    1093             :         }
    1094             :         
    1095           0 :         _php_libxml_destroy_fci(&LIBXML(entity_loader).fci);
    1096             :         
    1097           0 :         if (fci.size > 0) { /* argument not null */
    1098           0 :                 LIBXML(entity_loader).fci = fci;
    1099           0 :                 Z_ADDREF_P(fci.function_name);
    1100           0 :                 if (fci.object_ptr != NULL) {
    1101           0 :                         Z_ADDREF_P(fci.object_ptr);
    1102             :                 }
    1103           0 :                 LIBXML(entity_loader).fcc = fcc;
    1104             :         }
    1105             :         
    1106           0 :         RETURN_TRUE;
    1107             : }
    1108             : /* }}} */
    1109             : 
    1110             : /* {{{ Common functions shared by extensions */
    1111           0 : int php_libxml_xmlCheckUTF8(const unsigned char *s)
    1112             : {
    1113             :         int i;
    1114             :         unsigned char c;
    1115             : 
    1116           0 :         for (i = 0; (c = s[i++]);) {
    1117           0 :                 if ((c & 0x80) == 0) {
    1118           0 :                 } else if ((c & 0xe0) == 0xc0) {
    1119           0 :                         if ((s[i++] & 0xc0) != 0x80) {
    1120           0 :                                 return 0;
    1121             :                         }
    1122           0 :                 } else if ((c & 0xf0) == 0xe0) {
    1123           0 :                         if ((s[i++] & 0xc0) != 0x80 || (s[i++] & 0xc0) != 0x80) {
    1124           0 :                                 return 0;
    1125             :                         }
    1126           0 :                 } else if ((c & 0xf8) == 0xf0) {
    1127           0 :                         if ((s[i++] & 0xc0) != 0x80 || (s[i++] & 0xc0) != 0x80 || (s[i++] & 0xc0) != 0x80) {
    1128           0 :                                 return 0;
    1129             :                         }
    1130             :                 } else {
    1131           0 :                         return 0;
    1132             :                 }
    1133             :         }
    1134           0 :         return 1;
    1135             : }
    1136             : 
    1137           0 : int php_libxml_register_export(zend_class_entry *ce, php_libxml_export_node export_function)
    1138             : {
    1139             :         php_libxml_func_handler export_hnd;
    1140             :         
    1141             :         /* Initialize in case this module hasnt been loaded yet */
    1142           0 :         php_libxml_initialize();
    1143           0 :         export_hnd.export_func = export_function;
    1144             : 
    1145           0 :         return zend_hash_add(&php_libxml_exports, ce->name, ce->name_length + 1, &export_hnd, sizeof(export_hnd), NULL);
    1146             : }
    1147             : 
    1148           0 : PHP_LIBXML_API xmlNodePtr php_libxml_import_node(zval *object TSRMLS_DC)
    1149             : {
    1150           0 :         zend_class_entry *ce = NULL;
    1151           0 :         xmlNodePtr node = NULL;
    1152             :         php_libxml_func_handler *export_hnd;
    1153             : 
    1154           0 :         if (object->type == IS_OBJECT) {
    1155           0 :                 ce = Z_OBJCE_P(object);
    1156           0 :                 while (ce->parent != NULL) {
    1157           0 :                         ce = ce->parent;
    1158             :                 }
    1159           0 :                 if (zend_hash_find(&php_libxml_exports, ce->name, ce->name_length + 1, (void **) &export_hnd)  == SUCCESS) {
    1160           0 :                         node = export_hnd->export_func(object TSRMLS_CC);
    1161             :                 }
    1162             :         }
    1163           0 :         return node;
    1164             : }
    1165             : 
    1166           0 : PHP_LIBXML_API int php_libxml_increment_node_ptr(php_libxml_node_object *object, xmlNodePtr node, void *private_data TSRMLS_DC)
    1167             : {
    1168           0 :         int ret_refcount = -1;
    1169             : 
    1170           0 :         if (object != NULL && node != NULL) {
    1171           0 :                 if (object->node != NULL) {
    1172           0 :                         if (object->node->node == node) {
    1173           0 :                                 return object->node->refcount;
    1174             :                         } else {
    1175           0 :                                 php_libxml_decrement_node_ptr(object TSRMLS_CC);
    1176             :                         }
    1177             :                 }
    1178           0 :                 if (node->_private != NULL) {
    1179           0 :                         object->node = node->_private;
    1180           0 :                         ret_refcount = ++object->node->refcount;
    1181             :                         /* Only dom uses _private */
    1182           0 :                         if (object->node->_private == NULL) {
    1183           0 :                                 object->node->_private = private_data;
    1184             :                         }
    1185             :                 } else {
    1186           0 :                         ret_refcount = 1;
    1187           0 :                         object->node = emalloc(sizeof(php_libxml_node_ptr));
    1188           0 :                         object->node->node = node;
    1189           0 :                         object->node->refcount = 1;
    1190           0 :                         object->node->_private = private_data;
    1191           0 :                         node->_private = object->node;
    1192             :                 }
    1193             :         }
    1194             : 
    1195           0 :         return ret_refcount;
    1196             : }
    1197             : 
    1198           0 : PHP_LIBXML_API int php_libxml_decrement_node_ptr(php_libxml_node_object *object TSRMLS_DC)
    1199             : {
    1200           0 :         int ret_refcount = -1;
    1201             :         php_libxml_node_ptr *obj_node;
    1202             : 
    1203           0 :         if (object != NULL && object->node != NULL) {
    1204           0 :                 obj_node = (php_libxml_node_ptr *) object->node;
    1205           0 :                 ret_refcount = --obj_node->refcount;
    1206           0 :                 if (ret_refcount == 0) {
    1207           0 :                         if (obj_node->node != NULL) {
    1208           0 :                                 obj_node->node->_private = NULL;
    1209             :                         }
    1210           0 :                         efree(obj_node);
    1211             :                 } 
    1212           0 :                 object->node = NULL;
    1213             :         }
    1214             : 
    1215           0 :         return ret_refcount;
    1216             : }
    1217             : 
    1218           0 : PHP_LIBXML_API int php_libxml_increment_doc_ref(php_libxml_node_object *object, xmlDocPtr docp TSRMLS_DC)
    1219             : {
    1220           0 :         int ret_refcount = -1;
    1221             : 
    1222           0 :         if (object->document != NULL) {
    1223           0 :                 object->document->refcount++;
    1224           0 :                 ret_refcount = object->document->refcount;
    1225           0 :         } else if (docp != NULL) {
    1226           0 :                 ret_refcount = 1;
    1227           0 :                 object->document = emalloc(sizeof(php_libxml_ref_obj));
    1228           0 :                 object->document->ptr = docp;
    1229           0 :                 object->document->refcount = ret_refcount;
    1230           0 :                 object->document->doc_props = NULL;
    1231             :         }
    1232             : 
    1233           0 :         return ret_refcount;
    1234             : }
    1235             : 
    1236           0 : PHP_LIBXML_API int php_libxml_decrement_doc_ref(php_libxml_node_object *object TSRMLS_DC)
    1237             : {
    1238           0 :         int ret_refcount = -1;
    1239             : 
    1240           0 :         if (object != NULL && object->document != NULL) {
    1241           0 :                 ret_refcount = --object->document->refcount;
    1242           0 :                 if (ret_refcount == 0) {
    1243           0 :                         if (object->document->ptr != NULL) {
    1244           0 :                                 xmlFreeDoc((xmlDoc *) object->document->ptr);
    1245             :                         }
    1246           0 :                         if (object->document->doc_props != NULL) {
    1247           0 :                                 if (object->document->doc_props->classmap) {
    1248           0 :                                         zend_hash_destroy(object->document->doc_props->classmap);
    1249           0 :                                         FREE_HASHTABLE(object->document->doc_props->classmap);
    1250             :                                 }
    1251           0 :                                 efree(object->document->doc_props);
    1252             :                         }
    1253           0 :                         efree(object->document);
    1254           0 :                         object->document = NULL;
    1255             :                 }
    1256             :         }
    1257             : 
    1258           0 :         return ret_refcount;
    1259             : }
    1260             : 
    1261           0 : PHP_LIBXML_API void php_libxml_node_free_resource(xmlNodePtr node TSRMLS_DC)
    1262             : {
    1263           0 :         if (!node) {
    1264           0 :                 return;
    1265             :         }
    1266             : 
    1267           0 :         switch (node->type) {
    1268             :                 case XML_DOCUMENT_NODE:
    1269             :                 case XML_HTML_DOCUMENT_NODE:
    1270           0 :                         break;
    1271             :                 default:
    1272           0 :                         if (node->parent == NULL || node->type == XML_NAMESPACE_DECL) {
    1273           0 :                                 php_libxml_node_free_list((xmlNodePtr) node->children TSRMLS_CC);
    1274           0 :                                 switch (node->type) {
    1275             :                                         /* Skip property freeing for the following types */
    1276             :                                         case XML_ATTRIBUTE_DECL:
    1277             :                                         case XML_DTD_NODE:
    1278             :                                         case XML_DOCUMENT_TYPE_NODE:
    1279             :                                         case XML_ENTITY_DECL:
    1280             :                                         case XML_ATTRIBUTE_NODE:
    1281             :                                         case XML_NAMESPACE_DECL:
    1282             :                                         case XML_TEXT_NODE:
    1283           0 :                                                 break;
    1284             :                                         default:
    1285           0 :                                                 php_libxml_node_free_list((xmlNodePtr) node->properties TSRMLS_CC);
    1286             :                                 }
    1287           0 :                                 if (php_libxml_unregister_node(node TSRMLS_CC) == 0) {
    1288           0 :                                         node->doc = NULL;
    1289             :                                 }
    1290           0 :                                 php_libxml_node_free(node);
    1291             :                         } else {
    1292           0 :                                 php_libxml_unregister_node(node TSRMLS_CC);
    1293             :                         }
    1294             :         }
    1295             : }
    1296             : 
    1297           0 : PHP_LIBXML_API void php_libxml_node_decrement_resource(php_libxml_node_object *object TSRMLS_DC)
    1298             : {
    1299           0 :         int ret_refcount = -1;
    1300             :         xmlNodePtr nodep;
    1301             :         php_libxml_node_ptr *obj_node;
    1302             : 
    1303           0 :         if (object != NULL && object->node != NULL) {
    1304           0 :                 obj_node = (php_libxml_node_ptr *) object->node;
    1305           0 :                 nodep = object->node->node;
    1306           0 :                 ret_refcount = php_libxml_decrement_node_ptr(object TSRMLS_CC);
    1307           0 :                 if (ret_refcount == 0) {
    1308           0 :                         php_libxml_node_free_resource(nodep TSRMLS_CC);
    1309             :                 } else {
    1310           0 :                         if (obj_node && object == obj_node->_private) {
    1311           0 :                                 obj_node->_private = NULL;
    1312             :                         }
    1313             :                 }
    1314             :         }
    1315           0 :         if (object != NULL && object->document != NULL) {
    1316             :                 /* Safe to call as if the resource were freed then doc pointer is NULL */
    1317           0 :                 php_libxml_decrement_doc_ref(object TSRMLS_CC);
    1318             :         }
    1319           0 : }
    1320             : /* }}} */
    1321             : 
    1322             : #ifdef PHP_WIN32
    1323             : PHP_LIBXML_API BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
    1324             : {
    1325             :         return xmlDllMain(hinstDLL, fdwReason, lpvReserved);
    1326             : }
    1327             : #endif
    1328             : 
    1329             : #endif
    1330             : 
    1331             : /*
    1332             :  * Local variables:
    1333             :  * tab-width: 4
    1334             :  * c-basic-offset: 4
    1335             :  * End:
    1336             :  * vim600: sw=4 ts=4 fdm=marker
    1337             :  * vim<600: sw=4 ts=4
    1338             :  */

Generated by: LCOV version 1.10

Generated at Wed, 16 Apr 2014 12:47:50 +0000 (4 days ago)

Copyright © 2005-2014 The PHP Group
All rights reserved.