Return to php_xml.c CVS log | Up to [ELWIX - Embedded LightWeight unIX -] / embedaddon / php / ext / soap |
1.1 misho 1: /*
2: +----------------------------------------------------------------------+
3: | PHP Version 5 |
4: +----------------------------------------------------------------------+
5: | Copyright (c) 1997-2012 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: Brad Lafountain <rodif_bl@yahoo.com> |
16: | Shane Caraveo <shane@caraveo.com> |
17: | Dmitry Stogov <dmitry@zend.com> |
18: +----------------------------------------------------------------------+
19: */
1.1.1.2 ! misho 20: /* $Id$ */
1.1 misho 21:
22: #include "php_soap.h"
23: #include "libxml/parser.h"
24: #include "libxml/parserInternals.h"
25:
26: /* Channel libxml file io layer through the PHP streams subsystem.
27: * This allows use of ftps:// and https:// urls */
28:
29: static int is_blank(const xmlChar* str)
30: {
31: while (*str != '\0') {
32: if (*str != ' ' && *str != 0x9 && *str != 0xa && *str != 0xd) {
33: return 0;
34: }
35: str++;
36: }
37: return 1;
38: }
39:
40: /* removes all empty text, comments and other insignoficant nodes */
41: static void cleanup_xml_node(xmlNodePtr node)
42: {
43: xmlNodePtr trav;
44: xmlNodePtr del = NULL;
45:
46: trav = node->children;
47: while (trav != NULL) {
48: if (del != NULL) {
49: xmlUnlinkNode(del);
50: xmlFreeNode(del);
51: del = NULL;
52: }
53: if (trav->type == XML_TEXT_NODE) {
54: if (is_blank(trav->content)) {
55: del = trav;
56: }
57: } else if ((trav->type != XML_ELEMENT_NODE) &&
58: (trav->type != XML_CDATA_SECTION_NODE)) {
59: del = trav;
60: } else if (trav->children != NULL) {
61: cleanup_xml_node(trav);
62: }
63: trav = trav->next;
64: }
65: if (del != NULL) {
66: xmlUnlinkNode(del);
67: xmlFreeNode(del);
68: }
69: }
70:
71: static void soap_ignorableWhitespace(void *ctx, const xmlChar *ch, int len)
72: {
73: }
74:
75: static void soap_Comment(void *ctx, const xmlChar *value)
76: {
77: }
78:
79: xmlDocPtr soap_xmlParseFile(const char *filename TSRMLS_DC)
80: {
81: xmlParserCtxtPtr ctxt = NULL;
82: xmlDocPtr ret;
83: zend_bool old_allow_url_fopen;
84:
85: /*
86: xmlInitParser();
87: */
88:
89: old_allow_url_fopen = PG(allow_url_fopen);
90: PG(allow_url_fopen) = 1;
91: ctxt = xmlCreateFileParserCtxt(filename);
92: PG(allow_url_fopen) = old_allow_url_fopen;
93: if (ctxt) {
94: ctxt->keepBlanks = 0;
95: ctxt->sax->ignorableWhitespace = soap_ignorableWhitespace;
96: ctxt->sax->comment = soap_Comment;
97: ctxt->sax->warning = NULL;
98: ctxt->sax->error = NULL;
99: /*ctxt->sax->fatalError = NULL;*/
100: xmlParseDocument(ctxt);
101: if (ctxt->wellFormed) {
102: ret = ctxt->myDoc;
103: if (ret->URL == NULL && ctxt->directory != NULL) {
104: ret->URL = xmlCharStrdup(ctxt->directory);
105: }
106: } else {
107: ret = NULL;
108: xmlFreeDoc(ctxt->myDoc);
109: ctxt->myDoc = NULL;
110: }
111: xmlFreeParserCtxt(ctxt);
112: } else {
113: ret = NULL;
114: }
115:
116: /*
117: xmlCleanupParser();
118: */
119:
120: if (ret) {
121: cleanup_xml_node((xmlNodePtr)ret);
122: }
123: return ret;
124: }
125:
126: xmlDocPtr soap_xmlParseMemory(const void *buf, size_t buf_size)
127: {
128: xmlParserCtxtPtr ctxt = NULL;
129: xmlDocPtr ret;
130:
131: /*
132: xmlInitParser();
133: */
134: ctxt = xmlCreateMemoryParserCtxt(buf, buf_size);
135: if (ctxt) {
136: ctxt->sax->ignorableWhitespace = soap_ignorableWhitespace;
137: ctxt->sax->comment = soap_Comment;
138: ctxt->sax->warning = NULL;
139: ctxt->sax->error = NULL;
140: /*ctxt->sax->fatalError = NULL;*/
141: #if LIBXML_VERSION >= 20703
142: ctxt->options |= XML_PARSE_HUGE;
143: #endif
144: xmlParseDocument(ctxt);
145: if (ctxt->wellFormed) {
146: ret = ctxt->myDoc;
147: if (ret->URL == NULL && ctxt->directory != NULL) {
148: ret->URL = xmlCharStrdup(ctxt->directory);
149: }
150: } else {
151: ret = NULL;
152: xmlFreeDoc(ctxt->myDoc);
153: ctxt->myDoc = NULL;
154: }
155: xmlFreeParserCtxt(ctxt);
156: } else {
157: ret = NULL;
158: }
159:
160: /*
161: xmlCleanupParser();
162: */
163:
164: /*
165: if (ret) {
166: cleanup_xml_node((xmlNodePtr)ret);
167: }
168: */
169: return ret;
170: }
171:
172: xmlNsPtr attr_find_ns(xmlAttrPtr node)
173: {
174: if (node->ns) {
175: return node->ns;
176: } else if (node->parent->ns) {
177: return node->parent->ns;
178: } else {
179: return xmlSearchNs(node->doc, node->parent, NULL);
180: }
181: }
182:
183: xmlNsPtr node_find_ns(xmlNodePtr node)
184: {
185: if (node->ns) {
186: return node->ns;
187: } else {
188: return xmlSearchNs(node->doc, node, NULL);
189: }
190: }
191:
192: int attr_is_equal_ex(xmlAttrPtr node, char *name, char *ns)
193: {
194: if (name == NULL || strcmp((char*)node->name, name) == 0) {
195: if (ns) {
196: xmlNsPtr nsPtr = attr_find_ns(node);
197: if (nsPtr) {
198: return (strcmp((char*)nsPtr->href, ns) == 0);
199: } else {
200: return FALSE;
201: }
202: }
203: return TRUE;
204: }
205: return FALSE;
206: }
207:
208: int node_is_equal_ex(xmlNodePtr node, char *name, char *ns)
209: {
210: if (name == NULL || strcmp((char*)node->name, name) == 0) {
211: if (ns) {
212: xmlNsPtr nsPtr = node_find_ns(node);
213: if (nsPtr) {
214: return (strcmp((char*)nsPtr->href, ns) == 0);
215: } else {
216: return FALSE;
217: }
218: }
219: return TRUE;
220: }
221: return FALSE;
222: }
223:
224:
225: xmlAttrPtr get_attribute_ex(xmlAttrPtr node, char *name, char *ns)
226: {
227: while (node!=NULL) {
228: if (attr_is_equal_ex(node, name, ns)) {
229: return node;
230: }
231: node = node->next;
232: }
233: return NULL;
234: }
235:
236: xmlNodePtr get_node_ex(xmlNodePtr node, char *name, char *ns)
237: {
238: while (node!=NULL) {
239: if (node_is_equal_ex(node, name, ns)) {
240: return node;
241: }
242: node = node->next;
243: }
244: return NULL;
245: }
246:
247: xmlNodePtr get_node_recurisve_ex(xmlNodePtr node, char *name, char *ns)
248: {
249: while (node != NULL) {
250: if (node_is_equal_ex(node, name, ns)) {
251: return node;
252: } else if (node->children != NULL) {
253: xmlNodePtr tmp = get_node_recurisve_ex(node->children, name, ns);
254: if (tmp) {
255: return tmp;
256: }
257: }
258: node = node->next;
259: }
260: return NULL;
261: }
262:
263: xmlNodePtr get_node_with_attribute_ex(xmlNodePtr node, char *name, char *name_ns, char *attribute, char *value, char *attr_ns)
264: {
265: xmlAttrPtr attr;
266:
267: while (node != NULL) {
268: if (name != NULL) {
269: node = get_node_ex(node, name, name_ns);
270: if (node==NULL) {
271: return NULL;
272: }
273: }
274:
275: attr = get_attribute_ex(node->properties, attribute, attr_ns);
276: if (attr != NULL && strcmp((char*)attr->children->content, value) == 0) {
277: return node;
278: }
279: node = node->next;
280: }
281: return NULL;
282: }
283:
284: xmlNodePtr get_node_with_attribute_recursive_ex(xmlNodePtr node, char *name, char *name_ns, char *attribute, char *value, char *attr_ns)
285: {
286: while (node != NULL) {
287: if (node_is_equal_ex(node, name, name_ns)) {
288: xmlAttrPtr attr = get_attribute_ex(node->properties, attribute, attr_ns);
289: if (attr != NULL && strcmp((char*)attr->children->content, value) == 0) {
290: return node;
291: }
292: }
293: if (node->children != NULL) {
294: xmlNodePtr tmp = get_node_with_attribute_recursive_ex(node->children, name, name_ns, attribute, value, attr_ns);
295: if (tmp) {
296: return tmp;
297: }
298: }
299: node = node->next;
300: }
301: return NULL;
302: }
303:
304: int parse_namespace(const xmlChar *inval, char **value, char **namespace)
305: {
306: char *found = strrchr((char*)inval, ':');
307:
308: if (found != NULL && found != (char*)inval) {
309: (*namespace) = estrndup((char*)inval, found - (char*)inval);
310: (*value) = estrdup(++found);
311: } else {
312: (*value) = estrdup((char*)inval);
313: (*namespace) = NULL;
314: }
315:
316: return FALSE;
317: }