123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323 |
- /*
- +----------------------------------------------------------------------+
- | Copyright (c) The PHP Group |
- +----------------------------------------------------------------------+
- | This source file is subject to version 3.01 of the PHP license, |
- | that is bundled with this package in the file LICENSE, and is |
- | available through the world-wide-web at the following url: |
- | https://www.php.net/license/3_01.txt |
- | If you did not receive a copy of the PHP license and are unable to |
- | obtain it through the world-wide-web, please send a note to |
- | license@php.net so we can mail you a copy immediately. |
- +----------------------------------------------------------------------+
- | Authors: Brad Lafountain <rodif_bl@yahoo.com> |
- | Shane Caraveo <shane@caraveo.com> |
- | Dmitry Stogov <dmitry@php.net> |
- +----------------------------------------------------------------------+
- */
- #include "php_soap.h"
- #include "ext/libxml/php_libxml.h"
- #include "libxml/parser.h"
- #include "libxml/parserInternals.h"
- /* Channel libxml file io layer through the PHP streams subsystem.
- * This allows use of ftps:// and https:// urls */
- static int is_blank(const xmlChar* str)
- {
- while (*str != '\0') {
- if (*str != ' ' && *str != 0x9 && *str != 0xa && *str != 0xd) {
- return 0;
- }
- str++;
- }
- return 1;
- }
- /* removes all empty text, comments and other insignoficant nodes */
- static void cleanup_xml_node(xmlNodePtr node)
- {
- xmlNodePtr trav;
- xmlNodePtr del = NULL;
- trav = node->children;
- while (trav != NULL) {
- if (del != NULL) {
- xmlUnlinkNode(del);
- xmlFreeNode(del);
- del = NULL;
- }
- if (trav->type == XML_TEXT_NODE) {
- if (is_blank(trav->content)) {
- del = trav;
- }
- } else if ((trav->type != XML_ELEMENT_NODE) &&
- (trav->type != XML_CDATA_SECTION_NODE)) {
- del = trav;
- } else if (trav->children != NULL) {
- cleanup_xml_node(trav);
- }
- trav = trav->next;
- }
- if (del != NULL) {
- xmlUnlinkNode(del);
- xmlFreeNode(del);
- }
- }
- static void soap_ignorableWhitespace(void *ctx, const xmlChar *ch, int len)
- {
- }
- static void soap_Comment(void *ctx, const xmlChar *value)
- {
- }
- xmlDocPtr soap_xmlParseFile(const char *filename)
- {
- xmlParserCtxtPtr ctxt = NULL;
- xmlDocPtr ret;
- bool old_allow_url_fopen;
- /*
- xmlInitParser();
- */
- old_allow_url_fopen = PG(allow_url_fopen);
- PG(allow_url_fopen) = 1;
- ctxt = xmlCreateFileParserCtxt(filename);
- PG(allow_url_fopen) = old_allow_url_fopen;
- if (ctxt) {
- bool old;
- ctxt->keepBlanks = 0;
- ctxt->sax->ignorableWhitespace = soap_ignorableWhitespace;
- ctxt->sax->comment = soap_Comment;
- ctxt->sax->warning = NULL;
- ctxt->sax->error = NULL;
- /*ctxt->sax->fatalError = NULL;*/
- ctxt->options |= XML_PARSE_HUGE;
- old = php_libxml_disable_entity_loader(1);
- xmlParseDocument(ctxt);
- php_libxml_disable_entity_loader(old);
- if (ctxt->wellFormed) {
- ret = ctxt->myDoc;
- if (ret->URL == NULL && ctxt->directory != NULL) {
- ret->URL = xmlCharStrdup(ctxt->directory);
- }
- } else {
- ret = NULL;
- xmlFreeDoc(ctxt->myDoc);
- ctxt->myDoc = NULL;
- }
- xmlFreeParserCtxt(ctxt);
- } else {
- ret = NULL;
- }
- /*
- xmlCleanupParser();
- */
- if (ret) {
- cleanup_xml_node((xmlNodePtr)ret);
- }
- return ret;
- }
- xmlDocPtr soap_xmlParseMemory(const void *buf, size_t buf_size)
- {
- xmlParserCtxtPtr ctxt = NULL;
- xmlDocPtr ret;
- /*
- xmlInitParser();
- */
- ctxt = xmlCreateMemoryParserCtxt(buf, buf_size);
- if (ctxt) {
- bool old;
- ctxt->sax->ignorableWhitespace = soap_ignorableWhitespace;
- ctxt->sax->comment = soap_Comment;
- ctxt->sax->warning = NULL;
- ctxt->sax->error = NULL;
- /*ctxt->sax->fatalError = NULL;*/
- ctxt->options |= XML_PARSE_HUGE;
- old = php_libxml_disable_entity_loader(1);
- xmlParseDocument(ctxt);
- php_libxml_disable_entity_loader(old);
- if (ctxt->wellFormed) {
- ret = ctxt->myDoc;
- if (ret->URL == NULL && ctxt->directory != NULL) {
- ret->URL = xmlCharStrdup(ctxt->directory);
- }
- } else {
- ret = NULL;
- xmlFreeDoc(ctxt->myDoc);
- ctxt->myDoc = NULL;
- }
- xmlFreeParserCtxt(ctxt);
- } else {
- ret = NULL;
- }
- /*
- xmlCleanupParser();
- */
- /*
- if (ret) {
- cleanup_xml_node((xmlNodePtr)ret);
- }
- */
- return ret;
- }
- xmlNsPtr attr_find_ns(xmlAttrPtr node)
- {
- if (node->ns) {
- return node->ns;
- } else if (node->parent->ns) {
- return node->parent->ns;
- } else {
- return xmlSearchNs(node->doc, node->parent, NULL);
- }
- }
- xmlNsPtr node_find_ns(xmlNodePtr node)
- {
- if (node->ns) {
- return node->ns;
- } else {
- return xmlSearchNs(node->doc, node, NULL);
- }
- }
- int attr_is_equal_ex(xmlAttrPtr node, char *name, char *ns)
- {
- if (name == NULL || ((node->name) && strcmp((char*)node->name, name) == 0)) {
- if (ns) {
- xmlNsPtr nsPtr = attr_find_ns(node);
- if (nsPtr) {
- return (strcmp((char*)nsPtr->href, ns) == 0);
- } else {
- return FALSE;
- }
- }
- return TRUE;
- }
- return FALSE;
- }
- int node_is_equal_ex(xmlNodePtr node, char *name, char *ns)
- {
- if (name == NULL || ((node->name) && strcmp((char*)node->name, name) == 0)) {
- if (ns) {
- xmlNsPtr nsPtr = node_find_ns(node);
- if (nsPtr) {
- return (strcmp((char*)nsPtr->href, ns) == 0);
- } else {
- return FALSE;
- }
- }
- return TRUE;
- }
- return FALSE;
- }
- xmlAttrPtr get_attribute_ex(xmlAttrPtr node, char *name, char *ns)
- {
- while (node!=NULL) {
- if (attr_is_equal_ex(node, name, ns)) {
- return node;
- }
- node = node->next;
- }
- return NULL;
- }
- xmlNodePtr get_node_ex(xmlNodePtr node, char *name, char *ns)
- {
- while (node!=NULL) {
- if (node_is_equal_ex(node, name, ns)) {
- return node;
- }
- node = node->next;
- }
- return NULL;
- }
- xmlNodePtr get_node_recurisve_ex(xmlNodePtr node, char *name, char *ns)
- {
- while (node != NULL) {
- if (node_is_equal_ex(node, name, ns)) {
- return node;
- } else if (node->children != NULL) {
- xmlNodePtr tmp = get_node_recurisve_ex(node->children, name, ns);
- if (tmp) {
- return tmp;
- }
- }
- node = node->next;
- }
- return NULL;
- }
- xmlNodePtr get_node_with_attribute_ex(xmlNodePtr node, char *name, char *name_ns, char *attribute, char *value, char *attr_ns)
- {
- xmlAttrPtr attr;
- while (node != NULL) {
- if (name != NULL) {
- node = get_node_ex(node, name, name_ns);
- if (node==NULL) {
- return NULL;
- }
- }
- attr = get_attribute_ex(node->properties, attribute, attr_ns);
- if (attr != NULL && strcmp((char*)attr->children->content, value) == 0) {
- return node;
- }
- node = node->next;
- }
- return NULL;
- }
- xmlNodePtr get_node_with_attribute_recursive_ex(xmlNodePtr node, char *name, char *name_ns, char *attribute, char *value, char *attr_ns)
- {
- while (node != NULL) {
- if (node_is_equal_ex(node, name, name_ns)) {
- xmlAttrPtr attr = get_attribute_ex(node->properties, attribute, attr_ns);
- if (attr != NULL && strcmp((char*)attr->children->content, value) == 0) {
- return node;
- }
- }
- if (node->children != NULL) {
- xmlNodePtr tmp = get_node_with_attribute_recursive_ex(node->children, name, name_ns, attribute, value, attr_ns);
- if (tmp) {
- return tmp;
- }
- }
- node = node->next;
- }
- return NULL;
- }
- int parse_namespace(const xmlChar *inval, char **value, char **namespace)
- {
- char *found = strrchr((char*)inval, ':');
- if (found != NULL && found != (char*)inval) {
- (*namespace) = estrndup((char*)inval, found - (char*)inval);
- (*value) = estrdup(++found);
- } else {
- (*value) = estrdup((char*)inval);
- (*namespace) = NULL;
- }
- return FALSE;
- }
|