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