/*
+----------------------------------------------------------------------+
| Copyright © The PHP Group and Contributors. |
+----------------------------------------------------------------------+
| This source file is subject to the Modified BSD License that is |
| bundled with this package in the file LICENSE, and is available |
| through the World Wide Web at . |
| |
| SPDX-License-Identifier: BSD-3-Clause |
+----------------------------------------------------------------------+
| Authors: Brad Lafountain |
| Shane Caraveo |
| Dmitry Stogov |
+----------------------------------------------------------------------+
*/
#include "php_soap.h"
#include "ext/libxml/php_libxml.h"
#include "libxml/uri.h"
#include "zend_virtual_cwd.h"
#include "main/php_open_temporary_file.h"
#include
#include
#include
#ifndef O_BINARY
# define O_BINARY 0
#endif
#ifdef WORDS_BIGENDIAN
# define SOAP_BIG_ENDIAN 1
#else
# define SOAP_BIG_ENDIAN 0
#endif
static void delete_fault(zval *zv);
static void delete_fault_persistent(zval *zv);
static void delete_binding(zval *zv);
static void delete_binding_persistent(zval *zv);
static void delete_function(zval *zv);
static void delete_function_persistent(zval *zv);
static void delete_parameter(zval *zv);
static void delete_parameter_persistent(zval *zv);
static void delete_header(zval *header);
static void delete_header_int(sdlSoapBindingFunctionHeaderPtr hdr);
static void delete_header_persistent(zval *zv);
static void delete_document(zval *zv);
encodePtr get_encoder_from_prefix(sdlPtr sdl, xmlNodePtr node, const xmlChar *type)
{
encodePtr enc = NULL;
xmlNsPtr nsptr;
const char *cptype;
char *ns;
parse_namespace(type, &cptype, &ns);
nsptr = xmlSearchNs(node->doc, node, BAD_CAST(ns));
if (nsptr != NULL) {
enc = get_encoder(sdl, (char*)nsptr->href, cptype);
if (enc == NULL) {
enc = get_encoder_ex(sdl, cptype, strlen(cptype));
}
} else {
enc = get_encoder_ex(sdl, (char*)type, xmlStrlen(type));
}
if (ns) {efree(ns);}
return enc;
}
static sdlTypePtr get_element(sdlPtr sdl, xmlNodePtr node, const xmlChar *type)
{
sdlTypePtr ret = NULL;
if (sdl->elements) {
xmlNsPtr nsptr;
const char *cptype;
char *ns;
sdlTypePtr sdl_type;
parse_namespace(type, &cptype, &ns);
nsptr = xmlSearchNs(node->doc, node, BAD_CAST(ns));
if (nsptr != NULL) {
size_t ns_len = xmlStrlen(nsptr->href);
size_t type_len = strlen(cptype);
size_t len = ns_len + type_len + 1;
char *nscat = emalloc(len + 1);
memcpy(nscat, nsptr->href, ns_len);
nscat[ns_len] = ':';
memcpy(nscat+ns_len+1, cptype, type_len);
nscat[len] = '\0';
if ((sdl_type = zend_hash_str_find_ptr(sdl->elements, nscat, len)) != NULL) {
ret = sdl_type;
} else if ((sdl_type = zend_hash_str_find_ptr(sdl->elements, (char*)type, type_len)) != NULL) {
ret = sdl_type;
}
efree(nscat);
} else {
if ((sdl_type = zend_hash_str_find_ptr(sdl->elements, (char*)type, xmlStrlen(type))) != NULL) {
ret = sdl_type;
}
}
if (ns) {efree(ns);}
}
return ret;
}
encodePtr get_encoder(sdlPtr sdl, const char *ns, const char *type)
{
encodePtr enc = NULL;
char *nscat;
size_t ns_len = ns ? strlen(ns) : 0;
size_t type_len = strlen(type);
size_t len = ns_len + type_len + 1;
nscat = emalloc(len + 1);
if (ns) {
memcpy(nscat, ns, ns_len);
}
nscat[ns_len] = ':';
memcpy(nscat+ns_len+1, type, type_len);
nscat[len] = '\0';
enc = get_encoder_ex(sdl, nscat, len);
if (enc == NULL &&
((ns_len == sizeof(SOAP_1_1_ENC_NAMESPACE)-1 &&
memcmp(ns, SOAP_1_1_ENC_NAMESPACE, sizeof(SOAP_1_1_ENC_NAMESPACE)-1) == 0) ||
(ns_len == sizeof(SOAP_1_2_ENC_NAMESPACE)-1 &&
memcmp(ns, SOAP_1_2_ENC_NAMESPACE, sizeof(SOAP_1_2_ENC_NAMESPACE)-1) == 0))) {
char *enc_nscat;
size_t enc_ns_len;
size_t enc_len;
enc_ns_len = sizeof(XSD_NAMESPACE)-1;
enc_len = enc_ns_len + type_len + 1;
enc_nscat = emalloc(enc_len + 1);
memcpy(enc_nscat, XSD_NAMESPACE, sizeof(XSD_NAMESPACE)-1);
enc_nscat[enc_ns_len] = ':';
memcpy(enc_nscat+enc_ns_len+1, type, type_len);
enc_nscat[enc_len] = '\0';
enc = get_encoder_ex(NULL, enc_nscat, enc_len);
efree(enc_nscat);
if (enc && sdl) {
encodePtr new_enc = pemalloc(sizeof(encode), sdl->is_persistent);
memcpy(new_enc, enc, sizeof(encode));
if (sdl->is_persistent) {
new_enc->details.ns = zend_strndup(ns, ns_len);
new_enc->details.type_str = strdup(new_enc->details.type_str);
} else {
new_enc->details.ns = estrndup(ns, ns_len);
new_enc->details.type_str = estrdup(new_enc->details.type_str);
}
if (new_enc->details.clark_notation) {
/* If it was persistent or becomes persistent, we must dup. Otherwise we can copy. */
bool was_persistent = GC_FLAGS(new_enc->details.clark_notation) & IS_STR_PERSISTENT;
if (was_persistent || sdl->is_persistent) {
new_enc->details.clark_notation = zend_string_dup(new_enc->details.clark_notation, sdl->is_persistent);
} else {
zend_string_addref(new_enc->details.clark_notation);
}
}
if (sdl->encoders == NULL) {
sdl->encoders = pemalloc(sizeof(HashTable), sdl->is_persistent);
zend_hash_init(sdl->encoders, 0, NULL, sdl->is_persistent ? delete_encoder_persistent : delete_encoder, sdl->is_persistent);
}
zend_hash_str_update_ptr(sdl->encoders, nscat, len, new_enc);
enc = new_enc;
}
}
efree(nscat);
return enc;
}
encodePtr get_encoder_ex(sdlPtr sdl, const char *nscat, size_t len)
{
encodePtr enc;
if ((enc = zend_hash_str_find_ptr(&php_soap_defEnc, nscat, len)) != NULL) {
return enc;
} else if (sdl && sdl->encoders && (enc = zend_hash_str_find_ptr(sdl->encoders, nscat, len)) != NULL) {
return enc;
}
return NULL;
}
static int is_wsdl_element(xmlNodePtr node)
{
if (node->ns && strcmp((char*)node->ns->href, WSDL_NAMESPACE) != 0) {
xmlAttrPtr attr;
if ((attr = get_attribute_ex(node->properties, "required", WSDL_NAMESPACE)) != NULL &&
attr->children && attr->children->content &&
(strcmp((char*)attr->children->content, "1") == 0 ||
strcmp((char*)attr->children->content, "true") == 0)) {
soap_error1(E_ERROR, "Parsing WSDL: Unknown required WSDL extension '%s'", node->ns->href);
}
return 0;
}
return 1;
}
void sdl_set_uri_credentials(sdlCtx *ctx, char *uri)
{
char *s;
size_t l1, l2;
zval context;
zval *header = NULL;
/* check if we load xsd from the same server */
s = strstr(ctx->sdl->source, "://");
if (!s) return;
s = strchr(s+3, '/');
l1 = s ? (size_t)(s - ctx->sdl->source) : strlen(ctx->sdl->source);
s = strstr((char*)uri, "://");
if (!s) return;
s = strchr(s+3, '/');
l2 = s ? (size_t)(s - (char*)uri) : strlen((char*)uri);
if (l1 != l2) {
/* check for http://...:80/ */
if (l1 > 11 &&
ctx->sdl->source[4] == ':' &&
ctx->sdl->source[l1-3] == ':' &&
ctx->sdl->source[l1-2] == '8' &&
ctx->sdl->source[l1-1] == '0') {
l1 -= 3;
}
if (l2 > 11 &&
uri[4] == ':' &&
uri[l2-3] == ':' &&
uri[l2-2] == '8' &&
uri[l2-1] == '0') {
l2 -= 3;
}
/* check for https://...:443/ */
if (l1 > 13 &&
ctx->sdl->source[4] == 's' &&
ctx->sdl->source[l1-4] == ':' &&
ctx->sdl->source[l1-3] == '4' &&
ctx->sdl->source[l1-2] == '4' &&
ctx->sdl->source[l1-1] == '3') {
l1 -= 4;
}
if (l2 > 13 &&
uri[4] == 's' &&
uri[l2-4] == ':' &&
uri[l2-3] == '4' &&
uri[l2-2] == '4' &&
uri[l2-1] == '3') {
l2 -= 4;
}
}
if (l1 != l2 || memcmp(ctx->sdl->source, uri, l1) != 0) {
/* another server. clear authentication credentals */
php_libxml_switch_context(NULL, &context);
php_libxml_switch_context(&context, NULL);
if (Z_TYPE(context) != IS_UNDEF) {
zval *context_ptr = &context;
ctx->context = php_stream_context_from_zval(context_ptr, 1);
if (ctx->context &&
(header = php_stream_context_get_option(ctx->context, "http", "header")) != NULL &&
Z_TYPE_P(header) == IS_STRING) {
/* TODO: should support header as an array, but this code path is untested */
s = strstr(Z_STRVAL_P(header), "Authorization: Basic");
if (s && (s == Z_STRVAL_P(header) || *(s-1) == '\n' || *(s-1) == '\r')) {
char *rest = strstr(s, "\r\n");
if (rest) {
zval new_header;
rest += 2;
ZVAL_NEW_STR(&new_header, zend_string_alloc(Z_STRLEN_P(header) - (rest - s), 0));
memcpy(Z_STRVAL(new_header), Z_STRVAL_P(header), s - Z_STRVAL_P(header));
memcpy(Z_STRVAL(new_header) + (s - Z_STRVAL_P(header)), rest, Z_STRLEN_P(header) - (rest - Z_STRVAL_P(header)) + 1);
ZVAL_COPY(&ctx->old_header, header);
php_stream_context_set_option(ctx->context, "http", "header", &new_header);
zval_ptr_dtor(&new_header);
}
}
}
}
}
}
void sdl_restore_uri_credentials(sdlCtx *ctx)
{
if (Z_TYPE(ctx->old_header) != IS_UNDEF) {
php_stream_context_set_option(ctx->context, "http", "header", &ctx->old_header);
zval_ptr_dtor(&ctx->old_header);
ZVAL_UNDEF(&ctx->old_header);
}
ctx->context = NULL;
}
#define SAFE_STR(a) ((a)?((const char *)a):"")
static void load_wsdl_ex(zval *this_ptr, char *struri, sdlCtx *ctx, bool include)
{
sdlPtr tmpsdl = ctx->sdl;
xmlDocPtr wsdl;
xmlNodePtr root, definitions, trav;
if (zend_hash_str_exists(&ctx->docs, struri, strlen(struri))) {
return;
}
sdl_set_uri_credentials(ctx, struri);
wsdl = soap_xmlParseFile(struri);
sdl_restore_uri_credentials(ctx);
if (!wsdl) {
const xmlError *xmlErrorPtr = xmlGetLastError();
if (xmlErrorPtr) {
soap_error2(E_ERROR, "Parsing WSDL: Couldn't load from '%s' : %s", struri, xmlErrorPtr->message);
} else {
soap_error1(E_ERROR, "Parsing WSDL: Couldn't load from '%s'", struri);
}
}
zend_hash_str_add_ptr(&ctx->docs, struri, strlen(struri), wsdl);
root = wsdl->children;
definitions = get_node_ex(root, "definitions", WSDL_NAMESPACE);
if (!definitions) {
if (include) {
xmlNodePtr schema = get_node_ex(root, "schema", XSD_NAMESPACE);
if (schema) {
load_schema(ctx, schema);
return;
}
}
soap_error1(E_ERROR, "Parsing WSDL: Couldn't find in '%s'", struri);
}
if (!include) {
xmlAttrPtr targetNamespace = get_attribute(definitions->properties, "targetNamespace");
if (targetNamespace) {
tmpsdl->target_ns = estrdup((char*)targetNamespace->children->content);
}
}
trav = definitions->children;
while (trav != NULL) {
if (!is_wsdl_element(trav)) {
trav = trav->next;
continue;
}
if (node_is_equal(trav,"types")) {
/* TODO: Only one "types" is allowed */
xmlNodePtr trav2 = trav->children;
while (trav2 != NULL) {
if (node_is_equal_ex(trav2, "schema", XSD_NAMESPACE)) {
load_schema(ctx, trav2);
} else if (is_wsdl_element(trav2) && !node_is_equal(trav2,"documentation")) {
soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", SAFE_STR(trav2->name));
}
trav2 = trav2->next;
}
} else if (node_is_equal(trav,"import")) {
/* TODO: namespace ??? */
xmlAttrPtr tmp = get_attribute(trav->properties, "location");
if (tmp) {
xmlChar *uri = schema_location_construct_uri(tmp);
load_wsdl_ex(this_ptr, (char*)uri, ctx, true);
xmlFree(uri);
}
} else if (node_is_equal(trav,"message")) {
xmlAttrPtr name = get_attribute(trav->properties, "name");
if (name && name->children && name->children->content) {
if (zend_hash_str_add_ptr(&ctx->messages, (char*)name->children->content, xmlStrlen(name->children->content), trav) == NULL) {
soap_error1(E_ERROR, "Parsing WSDL: '%s' already defined", name->children->content);
}
} else {
soap_error0(E_ERROR, "Parsing WSDL: has no name attribute");
}
} else if (node_is_equal(trav,"portType")) {
xmlAttrPtr name = get_attribute(trav->properties, "name");
if (name && name->children && name->children->content) {
if (zend_hash_str_add_ptr(&ctx->portTypes, (char*)name->children->content, xmlStrlen(name->children->content), trav) == NULL) {
soap_error1(E_ERROR, "Parsing WSDL: '%s' already defined", name->children->content);
}
} else {
soap_error0(E_ERROR, "Parsing WSDL: has no name attribute");
}
} else if (node_is_equal(trav,"binding")) {
xmlAttrPtr name = get_attribute(trav->properties, "name");
if (name && name->children && name->children->content) {
if (zend_hash_str_add_ptr(&ctx->bindings, (char*)name->children->content, xmlStrlen(name->children->content), trav) == NULL) {
soap_error1(E_ERROR, "Parsing WSDL: '%s' already defined", name->children->content);
}
} else {
soap_error0(E_ERROR, "Parsing WSDL: has no name attribute");
}
} else if (node_is_equal(trav,"service")) {
xmlAttrPtr name = get_attribute(trav->properties, "name");
if (name && name->children && name->children->content) {
if (zend_hash_str_add_ptr(&ctx->services, (char*)name->children->content, xmlStrlen(name->children->content), trav) == NULL) {
soap_error1(E_ERROR, "Parsing WSDL: '%s' already defined", name->children->content);
}
} else {
soap_error0(E_ERROR, "Parsing WSDL: has no name attribute");
}
} else if (!node_is_equal(trav,"documentation")) {
soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", SAFE_STR(trav->name));
}
trav = trav->next;
}
}
static sdlSoapBindingFunctionHeaderPtr wsdl_soap_binding_header(sdlCtx* ctx, xmlNodePtr header, char* wsdl_soap_namespace, int fault)
{
xmlAttrPtr tmp;
xmlNodePtr message, part;
char *ctype;
sdlSoapBindingFunctionHeaderPtr h;
tmp = get_attribute(header->properties, "message");
if (!tmp) {
soap_error0(E_ERROR, "Parsing WSDL: Missing message attribute for ");
}
ctype = strrchr((char*)tmp->children->content,':');
if (ctype == NULL) {
ctype = (char*)tmp->children->content;
} else {
++ctype;
}
if ((message = zend_hash_str_find_ptr(&ctx->messages, ctype, strlen(ctype))) == NULL) {
soap_error1(E_ERROR, "Parsing WSDL: Missing with name '%s'", tmp->children->content);
}
tmp = get_attribute(header->properties, "part");
if (!tmp) {
soap_error0(E_ERROR, "Parsing WSDL: Missing part attribute for ");
}
part = get_node_with_attribute_ex(message->children, "part", WSDL_NAMESPACE, "name", (char*)tmp->children->content, NULL);
if (!part) {
soap_error1(E_ERROR, "Parsing WSDL: Missing part '%s' in ", tmp->children->content);
}
h = emalloc(sizeof(sdlSoapBindingFunctionHeader));
memset(h, 0, sizeof(sdlSoapBindingFunctionHeader));
h->name = estrdup((char*)tmp->children->content);
tmp = get_attribute(header->properties, "use");
if (tmp && !strncmp((char*)tmp->children->content, "encoded", sizeof("encoded"))) {
h->use = SOAP_ENCODED;
} else {
h->use = SOAP_LITERAL;
}
tmp = get_attribute(header->properties, "namespace");
if (tmp) {
h->ns = estrdup((char*)tmp->children->content);
}
if (h->use == SOAP_ENCODED) {
tmp = get_attribute(header->properties, "encodingStyle");
if (tmp) {
if (strncmp((char*)tmp->children->content, SOAP_1_1_ENC_NAMESPACE, sizeof(SOAP_1_1_ENC_NAMESPACE)) == 0) {
h->encodingStyle = SOAP_ENCODING_1_1;
} else if (strncmp((char*)tmp->children->content, SOAP_1_2_ENC_NAMESPACE, sizeof(SOAP_1_2_ENC_NAMESPACE)) == 0) {
h->encodingStyle = SOAP_ENCODING_1_2;
} else {
soap_error1(E_ERROR, "Parsing WSDL: Unknown encodingStyle '%s'", tmp->children->content);
}
} else {
soap_error0(E_ERROR, "Parsing WSDL: Unspecified encodingStyle");
}
}
tmp = get_attribute(part->properties, "type");
if (tmp != NULL) {
h->encode = get_encoder_from_prefix(ctx->sdl, part, tmp->children->content);
} else {
tmp = get_attribute(part->properties, "element");
if (tmp != NULL) {
h->element = get_element(ctx->sdl, part, tmp->children->content);
if (h->element) {
h->encode = h->element->encode;
if (!h->ns && h->element->namens) {
h->ns = estrdup(h->element->namens);
}
if (h->element->name) {
efree(h->name);
h->name = estrdup(h->element->name);
}
}
}
}
if (!fault) {
xmlNodePtr trav = header->children;
while (trav != NULL) {
if (node_is_equal_ex(trav, "headerfault", wsdl_soap_namespace)) {
sdlSoapBindingFunctionHeaderPtr hf = wsdl_soap_binding_header(ctx, trav, wsdl_soap_namespace, 1);
smart_str key = {0};
if (h->headerfaults == NULL) {
h->headerfaults = emalloc(sizeof(HashTable));
zend_hash_init(h->headerfaults, 0, NULL, delete_header, 0);
}
if (hf->ns) {
smart_str_appends(&key,hf->ns);
smart_str_appendc(&key,':');
}
smart_str_appends(&key,hf->name);
smart_str_0(&key);
if (zend_hash_add_ptr(h->headerfaults, key.s, hf) == NULL) {
delete_header_int(hf);
}
smart_str_free(&key);
} else if (is_wsdl_element(trav) && !node_is_equal(trav,"documentation")) {
soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", SAFE_STR(trav->name));
}
trav = trav->next;
}
}
return h;
}
static void wsdl_soap_binding_body(sdlCtx* ctx, xmlNodePtr node, char* wsdl_soap_namespace, sdlSoapBindingFunctionBody *binding, HashTable* params)
{
xmlNodePtr trav;
trav = node->children;
while (trav != NULL) {
if (node_is_equal_ex(trav, "body", wsdl_soap_namespace)) {
xmlNodePtr body = trav;
xmlAttrPtr useAttribute = get_attribute(body->properties, "use");
if (useAttribute && !strncmp((char*)useAttribute->children->content, "literal", sizeof("literal"))) {
binding->use = SOAP_LITERAL;
} else {
binding->use = SOAP_ENCODED;
}
xmlAttrPtr namespaceAttribute = get_attribute(body->properties, "namespace");
if (namespaceAttribute) {
binding->ns = estrdup((char*)namespaceAttribute->children->content);
}
xmlAttrPtr partsAttribute = get_attribute(body->properties, "parts");
if (partsAttribute) {
HashTable ht;
char *parts = (char*)partsAttribute->children->content;
/* Delete all parts those are not in the "parts" attribute */
zend_hash_init(&ht, 0, NULL, delete_parameter, 0);
while (*parts) {
sdlParamPtr param;
bool found = false;
char *end;
while (*parts == ' ') ++parts;
if (*parts == '\0') break;
end = strchr(parts, ' ');
if (end) *end = '\0';
ZEND_HASH_FOREACH_PTR(params, param) {
if (param->paramName && strcmp(parts, param->paramName) == 0) {
sdlParamPtr x_param;
x_param = emalloc(sizeof(sdlParam));
*x_param = *param;
param->paramName = NULL;
zend_hash_next_index_insert_ptr(&ht, x_param);
found = true;
break;
}
} ZEND_HASH_FOREACH_END();
if (!found) {
soap_error1(E_ERROR, "Parsing WSDL: Missing part '%s' in ", parts);
}
parts += strlen(parts);
if (end) *end = ' ';
}
zend_hash_destroy(params);
*params = ht;
}
if (binding->use == SOAP_ENCODED) {
xmlAttrPtr encodingStyleAttribute = get_attribute(body->properties, "encodingStyle");
if (encodingStyleAttribute) {
if (strncmp((char*)encodingStyleAttribute->children->content, SOAP_1_1_ENC_NAMESPACE, sizeof(SOAP_1_1_ENC_NAMESPACE)) == 0) {
binding->encodingStyle = SOAP_ENCODING_1_1;
} else if (strncmp((char*)encodingStyleAttribute->children->content, SOAP_1_2_ENC_NAMESPACE, sizeof(SOAP_1_2_ENC_NAMESPACE)) == 0) {
binding->encodingStyle = SOAP_ENCODING_1_2;
} else {
soap_error1(E_ERROR, "Parsing WSDL: Unknown encodingStyle '%s'", encodingStyleAttribute->children->content);
}
} else {
soap_error0(E_ERROR, "Parsing WSDL: Unspecified encodingStyle");
}
}
} else if (node_is_equal_ex(trav, "header", wsdl_soap_namespace)) {
sdlSoapBindingFunctionHeaderPtr h = wsdl_soap_binding_header(ctx, trav, wsdl_soap_namespace, 0);
smart_str key = {0};
if (binding->headers == NULL) {
binding->headers = emalloc(sizeof(HashTable));
zend_hash_init(binding->headers, 0, NULL, delete_header, 0);
}
if (h->ns) {
smart_str_appends(&key,h->ns);
smart_str_appendc(&key,':');
}
smart_str_appends(&key,h->name);
smart_str_0(&key);
if (zend_hash_add_ptr(binding->headers, key.s, h) == NULL) {
delete_header_int(h);
}
smart_str_free(&key);
} else if (is_wsdl_element(trav) && !node_is_equal(trav,"documentation")) {
soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", SAFE_STR(trav->name));
}
trav = trav->next;
}
}
static HashTable* wsdl_message(const sdlCtx *ctx, const xmlChar* message_name)
{
HashTable* parameters = NULL;
const char *ctype = strrchr((const char*)message_name,':');
if (ctype == NULL) {
ctype = (const char*)message_name;
} else {
++ctype;
}
xmlNodePtr message = zend_hash_str_find_ptr(&ctx->messages, ctype, strlen(ctype));
if (message == NULL) {
soap_error1(E_ERROR, "Parsing WSDL: Missing with name '%s'", (const char*)message_name);
}
parameters = emalloc(sizeof(HashTable));
zend_hash_init(parameters, 0, NULL, delete_parameter, 0);
xmlNodePtr trav = message->children;
while (trav != NULL) {
xmlAttrPtr type, name;
sdlParamPtr param;
if (trav->ns != NULL && strcmp((char*)trav->ns->href, WSDL_NAMESPACE) != 0) {
soap_error1(E_ERROR, "Parsing WSDL: Unexpected extensibility element <%s>", SAFE_STR(trav->name));
}
if (node_is_equal(trav,"documentation")) {
trav = trav->next;
continue;
}
if (!node_is_equal(trav,"part")) {
soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", SAFE_STR(trav->name));
}
xmlNodePtr part = trav;
param = emalloc(sizeof(sdlParam));
memset(param,0,sizeof(sdlParam));
param->order = 0;
name = get_attribute(part->properties, "name");
if (name == NULL) {
soap_error1(E_ERROR, "Parsing WSDL: No name associated with '%s'", SAFE_STR(message->name));
}
param->paramName = estrdup((char*)name->children->content);
type = get_attribute(part->properties, "type");
if (type != NULL) {
param->encode = get_encoder_from_prefix(ctx->sdl, part, type->children->content);
} else {
xmlAttrPtr element = get_attribute(part->properties, "element");
if (element != NULL) {
param->element = get_element(ctx->sdl, part, element->children->content);
if (param->element) {
param->encode = param->element->encode;
}
}
}
zend_hash_next_index_insert_ptr(parameters, param);
trav = trav->next;
}
return parameters;
}
static sdlPtr load_wsdl(zval *this_ptr, char *struri)
{
sdlCtx ctx = {0};
ctx.sdl = emalloc(sizeof(sdl));
memset(ctx.sdl, 0, sizeof(sdl));
ctx.sdl->source = estrdup(struri);
zend_hash_init(&ctx.sdl->functions, 0, NULL, delete_function, 0);
zend_hash_init(&ctx.docs, 0, NULL, delete_document, 0);
zend_hash_init(&ctx.messages, 0, NULL, NULL, 0);
zend_hash_init(&ctx.bindings, 0, NULL, NULL, 0);
zend_hash_init(&ctx.portTypes, 0, NULL, NULL, 0);
zend_hash_init(&ctx.services, 0, NULL, NULL, 0);
zend_try {
load_wsdl_ex(this_ptr, struri, &ctx, false);
schema_pass2(&ctx);
uint32_t n = zend_hash_num_elements(&ctx.services);
if (n == 0) {
soap_error0(E_ERROR, "Parsing WSDL: Couldn't bind to service");
}
zend_hash_internal_pointer_reset(&ctx.services);
for (uint32_t i = 0; i < n; i++) {
xmlNodePtr service, tmp;
xmlNodePtr trav, port;
bool has_soap_port = false;
service = tmp = zend_hash_get_current_data_ptr(&ctx.services);
trav = service->children;
while (trav != NULL) {
xmlAttrPtr type, name, bindingAttr, location;
xmlNodePtr portType, operation;
xmlNodePtr address, binding, trav2;
char *ctype;
sdlBindingPtr tmpbinding;
char *wsdl_soap_namespace = NULL;
if (!is_wsdl_element(trav) || node_is_equal(trav,"documentation")) {
trav = trav->next;
continue;
}
if (!node_is_equal(trav,"port")) {
soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", SAFE_STR(trav->name));
}
port = trav;
tmpbinding = emalloc(sizeof(sdlBinding));
memset(tmpbinding, 0, sizeof(sdlBinding));
bindingAttr = get_attribute(port->properties, "binding");
if (bindingAttr == NULL) {
soap_error0(E_ERROR, "Parsing WSDL: No binding associated with ");
}
/* find address and figure out binding type */
address = NULL;
trav2 = port->children;
while (trav2 != NULL) {
if (node_is_equal(trav2,"address") && trav2->ns) {
if (!strncmp((char*)trav2->ns->href, WSDL_SOAP11_NAMESPACE, sizeof(WSDL_SOAP11_NAMESPACE))) {
address = trav2;
wsdl_soap_namespace = WSDL_SOAP11_NAMESPACE;
tmpbinding->bindingType = BINDING_SOAP;
} else if (!strncmp((char*)trav2->ns->href, WSDL_SOAP12_NAMESPACE, sizeof(WSDL_SOAP12_NAMESPACE))) {
address = trav2;
wsdl_soap_namespace = WSDL_SOAP12_NAMESPACE;
tmpbinding->bindingType = BINDING_SOAP;
} else if (!strncmp((char*)trav2->ns->href, RPC_SOAP12_NAMESPACE, sizeof(RPC_SOAP12_NAMESPACE))) {
address = trav2;
wsdl_soap_namespace = RPC_SOAP12_NAMESPACE;
tmpbinding->bindingType = BINDING_SOAP;
} else if (!strncmp((char*)trav2->ns->href, WSDL_HTTP11_NAMESPACE, sizeof(WSDL_HTTP11_NAMESPACE))) {
address = trav2;
tmpbinding->bindingType = BINDING_HTTP;
} else if (!strncmp((char*)trav2->ns->href, WSDL_HTTP12_NAMESPACE, sizeof(WSDL_HTTP12_NAMESPACE))) {
address = trav2;
tmpbinding->bindingType = BINDING_HTTP;
}
}
if (trav2 != address && is_wsdl_element(trav2) && !node_is_equal(trav2,"documentation")) {
soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", SAFE_STR(trav2->name));
}
trav2 = trav2->next;
}
if (!address || tmpbinding->bindingType == BINDING_HTTP) {
if (has_soap_port || trav->next || i < n-1) {
efree(tmpbinding);
trav = trav->next;
continue;
} else if (!address) {
soap_error0(E_ERROR, "Parsing WSDL: No address associated with ");
}
}
has_soap_port = true;
location = get_attribute(address->properties, "location");
if (!location) {
soap_error0(E_ERROR, "Parsing WSDL: No location associated with ");
}
tmpbinding->location = estrdup((char*)location->children->content);
ctype = strrchr((char*)bindingAttr->children->content,':');
if (ctype == NULL) {
ctype = (char*)bindingAttr->children->content;
} else {
++ctype;
}
if ((tmp = zend_hash_str_find_ptr(&ctx.bindings, ctype, strlen(ctype))) == NULL) {
soap_error1(E_ERROR, "Parsing WSDL: No element with name '%s'", ctype);
}
binding = tmp;
if (tmpbinding->bindingType == BINDING_SOAP) {
sdlSoapBindingPtr soapBinding;
xmlNodePtr soapBindingNode;
soapBinding = emalloc(sizeof(sdlSoapBinding));
memset(soapBinding, 0, sizeof(sdlSoapBinding));
soapBinding->style = SOAP_DOCUMENT;
soapBindingNode = get_node_ex(binding->children, "binding", wsdl_soap_namespace);
if (soapBindingNode) {
xmlAttrPtr styleAttribute = get_attribute(soapBindingNode->properties, "style");
if (styleAttribute && !strncmp((char*)styleAttribute->children->content, "rpc", sizeof("rpc"))) {
soapBinding->style = SOAP_RPC;
}
xmlAttrPtr transportAttribute = get_attribute(soapBindingNode->properties, "transport");
if (transportAttribute) {
if (strncmp((char*)transportAttribute->children->content, WSDL_HTTP_TRANSPORT, sizeof(WSDL_HTTP_TRANSPORT)) == 0) {
soapBinding->transport = SOAP_TRANSPORT_HTTP;
} else {
/* try the next binding */
efree(soapBinding);
efree(tmpbinding->location);
efree(tmpbinding);
trav = trav->next;
continue;
}
}
}
tmpbinding->bindingAttributes = (void *)soapBinding;
}
name = get_attribute(binding->properties, "name");
if (name == NULL) {
soap_error0(E_ERROR, "Parsing WSDL: Missing 'name' attribute for ");
}
tmpbinding->name = estrdup((char*)name->children->content);
type = get_attribute(binding->properties, "type");
if (type == NULL) {
soap_error0(E_ERROR, "Parsing WSDL: Missing 'type' attribute for ");
}
ctype = strchr((char*)type->children->content,':');
if (ctype == NULL) {
ctype = (char*)type->children->content;
} else {
++ctype;
}
if ((tmp = zend_hash_str_find_ptr(&ctx.portTypes, ctype, strlen(ctype))) == NULL) {
soap_error1(E_ERROR, "Parsing WSDL: Missing with name '%s'", name->children->content);
}
portType = tmp;
trav2 = binding->children;
while (trav2 != NULL) {
sdlFunctionPtr function;
xmlNodePtr input, output, fault, portTypeOperation, trav3;
xmlAttrPtr op_name, paramOrder;
if ((tmpbinding->bindingType == BINDING_SOAP &&
node_is_equal_ex(trav2, "binding", wsdl_soap_namespace)) ||
!is_wsdl_element(trav2) ||
node_is_equal(trav2,"documentation")) {
trav2 = trav2->next;
continue;
}
if (!node_is_equal(trav2,"operation")) {
soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", SAFE_STR(trav2->name));
}
operation = trav2;
op_name = get_attribute(operation->properties, "name");
if (op_name == NULL) {
soap_error0(E_ERROR, "Parsing WSDL: Missing 'name' attribute for ");
}
trav3 = operation->children;
while (trav3 != NULL) {
if (tmpbinding->bindingType == BINDING_SOAP &&
node_is_equal_ex(trav3, "operation", wsdl_soap_namespace)) {
} else if (is_wsdl_element(trav3) &&
!node_is_equal(trav3,"input") &&
!node_is_equal(trav3,"output") &&
!node_is_equal(trav3,"fault") &&
!node_is_equal(trav3,"documentation")) {
soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", SAFE_STR(trav3->name));
}
trav3 = trav3->next;
}
portTypeOperation = get_node_with_attribute_ex(portType->children, "operation", WSDL_NAMESPACE, "name", (char*)op_name->children->content, NULL);
if (portTypeOperation == NULL) {
soap_error1(E_ERROR, "Parsing WSDL: Missing / with name '%s'", op_name->children->content);
}
function = emalloc(sizeof(sdlFunction));
memset(function, 0, sizeof(sdlFunction));
function->functionName = estrdup((char*)op_name->children->content);
if (tmpbinding->bindingType == BINDING_SOAP) {
sdlSoapBindingFunctionPtr soapFunctionBinding;
sdlSoapBindingPtr soapBinding;
xmlNodePtr soapOperation;
soapFunctionBinding = emalloc(sizeof(sdlSoapBindingFunction));
memset(soapFunctionBinding, 0, sizeof(sdlSoapBindingFunction));
soapBinding = (sdlSoapBindingPtr)tmpbinding->bindingAttributes;
soapFunctionBinding->style = soapBinding->style;
soapOperation = get_node_ex(operation->children, "operation", wsdl_soap_namespace);
if (soapOperation) {
xmlAttrPtr soapActionAttribute = get_attribute(soapOperation->properties, "soapAction");
if (soapActionAttribute) {
soapFunctionBinding->soapAction = estrdup((char*)soapActionAttribute->children->content);
}
xmlAttrPtr styleAttribute = get_attribute(soapOperation->properties, "style");
if (styleAttribute) {
if (!strncmp((char*)styleAttribute->children->content, "rpc", sizeof("rpc"))) {
soapFunctionBinding->style = SOAP_RPC;
} else {
soapFunctionBinding->style = SOAP_DOCUMENT;
}
} else {
soapFunctionBinding->style = soapBinding->style;
}
}
function->bindingAttributes = (void *)soapFunctionBinding;
}
input = get_node_ex(portTypeOperation->children, "input", WSDL_NAMESPACE);
if (input != NULL) {
xmlAttrPtr message;
message = get_attribute(input->properties, "message");
if (message == NULL) {
soap_error1(E_ERROR, "Parsing WSDL: Missing name for of '%s'", op_name->children->content);
}
function->requestParameters = wsdl_message(&ctx, message->children->content);
/* FIXME
xmlAttrPtr name = get_attribute(input->properties, "name");
if (name != NULL) {
function->requestName = estrdup(name->children->content);
} else {
*/
{
function->requestName = estrdup(function->functionName);
}
if (tmpbinding->bindingType == BINDING_SOAP) {
input = get_node_ex(operation->children, "input", WSDL_NAMESPACE);
if (input != NULL) {
sdlSoapBindingFunctionPtr soapFunctionBinding = function->bindingAttributes;
wsdl_soap_binding_body(&ctx, input, wsdl_soap_namespace, &soapFunctionBinding->input, function->requestParameters);
}
}
}
output = get_node_ex(portTypeOperation->children, "output", WSDL_NAMESPACE);
if (output != NULL) {
xmlAttrPtr message;
message = get_attribute(output->properties, "message");
if (message == NULL) {
soap_error1(E_ERROR, "Parsing WSDL: Missing name for