xref: /PHP-5.4/ext/soap/php_xml.c (revision e08bef44)
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: 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 #if LIBXML_VERSION >= 20703
104 		ctxt->options |= XML_PARSE_HUGE;
105 #endif
106 		old = php_libxml_disable_entity_loader(1 TSRMLS_CC);
107 		xmlParseDocument(ctxt);
108 		php_libxml_disable_entity_loader(old TSRMLS_CC);
109 		if (ctxt->wellFormed) {
110 			ret = ctxt->myDoc;
111 			if (ret->URL == NULL && ctxt->directory != NULL) {
112 				ret->URL = xmlCharStrdup(ctxt->directory);
113 			}
114 		} else {
115 			ret = NULL;
116 			xmlFreeDoc(ctxt->myDoc);
117 			ctxt->myDoc = NULL;
118 		}
119 		xmlFreeParserCtxt(ctxt);
120 	} else {
121 		ret = NULL;
122 	}
123 
124 /*
125 	xmlCleanupParser();
126 */
127 
128 	if (ret) {
129 		cleanup_xml_node((xmlNodePtr)ret);
130 	}
131 	return ret;
132 }
133 
soap_xmlParseMemory(const void * buf,size_t buf_size)134 xmlDocPtr soap_xmlParseMemory(const void *buf, size_t buf_size)
135 {
136 	xmlParserCtxtPtr ctxt = NULL;
137 	xmlDocPtr ret;
138 
139 	TSRMLS_FETCH();
140 
141 /*
142 	xmlInitParser();
143 */
144 	ctxt = xmlCreateMemoryParserCtxt(buf, buf_size);
145 	if (ctxt) {
146 		zend_bool old;
147 
148 		ctxt->sax->ignorableWhitespace = soap_ignorableWhitespace;
149 		ctxt->sax->comment = soap_Comment;
150 		ctxt->sax->warning = NULL;
151 		ctxt->sax->error = NULL;
152 		/*ctxt->sax->fatalError = NULL;*/
153 #if LIBXML_VERSION >= 20703
154 		ctxt->options |= XML_PARSE_HUGE;
155 #endif
156 		old = php_libxml_disable_entity_loader(1 TSRMLS_CC);
157 		xmlParseDocument(ctxt);
158 		php_libxml_disable_entity_loader(old TSRMLS_CC);
159 		if (ctxt->wellFormed) {
160 			ret = ctxt->myDoc;
161 			if (ret->URL == NULL && ctxt->directory != NULL) {
162 				ret->URL = xmlCharStrdup(ctxt->directory);
163 			}
164 		} else {
165 			ret = NULL;
166 			xmlFreeDoc(ctxt->myDoc);
167 			ctxt->myDoc = NULL;
168 		}
169 		xmlFreeParserCtxt(ctxt);
170 	} else {
171 		ret = NULL;
172 	}
173 
174 /*
175 	xmlCleanupParser();
176 */
177 
178 /*
179 	if (ret) {
180 		cleanup_xml_node((xmlNodePtr)ret);
181 	}
182 */
183 	return ret;
184 }
185 
attr_find_ns(xmlAttrPtr node)186 xmlNsPtr attr_find_ns(xmlAttrPtr node)
187 {
188 	if (node->ns) {
189 		return node->ns;
190 	} else if (node->parent->ns) {
191 		return node->parent->ns;
192 	} else {
193 		return xmlSearchNs(node->doc, node->parent, NULL);
194 	}
195 }
196 
node_find_ns(xmlNodePtr node)197 xmlNsPtr node_find_ns(xmlNodePtr node)
198 {
199 	if (node->ns) {
200 		return node->ns;
201 	} else {
202 		return xmlSearchNs(node->doc, node, NULL);
203 	}
204 }
205 
attr_is_equal_ex(xmlAttrPtr node,char * name,char * ns)206 int attr_is_equal_ex(xmlAttrPtr node, char *name, char *ns)
207 {
208 	if (name == NULL || strcmp((char*)node->name, name) == 0) {
209 		if (ns) {
210 			xmlNsPtr nsPtr = attr_find_ns(node);
211 			if (nsPtr) {
212 				return (strcmp((char*)nsPtr->href, ns) == 0);
213 			} else {
214 				return FALSE;
215 			}
216 		}
217 		return TRUE;
218 	}
219 	return FALSE;
220 }
221 
node_is_equal_ex(xmlNodePtr node,char * name,char * ns)222 int node_is_equal_ex(xmlNodePtr node, char *name, char *ns)
223 {
224 	if (name == NULL || strcmp((char*)node->name, name) == 0) {
225 		if (ns) {
226 			xmlNsPtr nsPtr = node_find_ns(node);
227 			if (nsPtr) {
228 				return (strcmp((char*)nsPtr->href, ns) == 0);
229 			} else {
230 				return FALSE;
231 			}
232 		}
233 		return TRUE;
234 	}
235 	return FALSE;
236 }
237 
238 
get_attribute_ex(xmlAttrPtr node,char * name,char * ns)239 xmlAttrPtr get_attribute_ex(xmlAttrPtr node, char *name, char *ns)
240 {
241 	while (node!=NULL) {
242 		if (attr_is_equal_ex(node, name, ns)) {
243 			return node;
244 		}
245 		node = node->next;
246 	}
247 	return NULL;
248 }
249 
get_node_ex(xmlNodePtr node,char * name,char * ns)250 xmlNodePtr get_node_ex(xmlNodePtr node, char *name, char *ns)
251 {
252 	while (node!=NULL) {
253 		if (node_is_equal_ex(node, name, ns)) {
254 			return node;
255 		}
256 		node = node->next;
257 	}
258 	return NULL;
259 }
260 
get_node_recurisve_ex(xmlNodePtr node,char * name,char * ns)261 xmlNodePtr get_node_recurisve_ex(xmlNodePtr node, char *name, char *ns)
262 {
263 	while (node != NULL) {
264 		if (node_is_equal_ex(node, name, ns)) {
265 			return node;
266 		} else if (node->children != NULL) {
267 			xmlNodePtr tmp = get_node_recurisve_ex(node->children, name, ns);
268 			if (tmp) {
269 				return tmp;
270 			}
271 		}
272 		node = node->next;
273 	}
274 	return NULL;
275 }
276 
get_node_with_attribute_ex(xmlNodePtr node,char * name,char * name_ns,char * attribute,char * value,char * attr_ns)277 xmlNodePtr get_node_with_attribute_ex(xmlNodePtr node, char *name, char *name_ns, char *attribute, char *value, char *attr_ns)
278 {
279 	xmlAttrPtr attr;
280 
281 	while (node != NULL) {
282 		if (name != NULL) {
283 			node = get_node_ex(node, name, name_ns);
284 			if (node==NULL) {
285 				return NULL;
286 			}
287 		}
288 
289 		attr = get_attribute_ex(node->properties, attribute, attr_ns);
290 		if (attr != NULL && strcmp((char*)attr->children->content, value) == 0) {
291 			return node;
292 		}
293 		node = node->next;
294 	}
295 	return NULL;
296 }
297 
get_node_with_attribute_recursive_ex(xmlNodePtr node,char * name,char * name_ns,char * attribute,char * value,char * attr_ns)298 xmlNodePtr get_node_with_attribute_recursive_ex(xmlNodePtr node, char *name, char *name_ns, char *attribute, char *value, char *attr_ns)
299 {
300 	while (node != NULL) {
301 		if (node_is_equal_ex(node, name, name_ns)) {
302 			xmlAttrPtr attr = get_attribute_ex(node->properties, attribute, attr_ns);
303 			if (attr != NULL && strcmp((char*)attr->children->content, value) == 0) {
304 				return node;
305 			}
306 		}
307 		if (node->children != NULL) {
308 			xmlNodePtr tmp = get_node_with_attribute_recursive_ex(node->children, name, name_ns, attribute, value, attr_ns);
309 			if (tmp) {
310 				return tmp;
311 			}
312 		}
313 		node = node->next;
314 	}
315 	return NULL;
316 }
317 
parse_namespace(const xmlChar * inval,char ** value,char ** namespace)318 int parse_namespace(const xmlChar *inval, char **value, char **namespace)
319 {
320 	char *found = strrchr((char*)inval, ':');
321 
322 	if (found != NULL && found != (char*)inval) {
323 		(*namespace) = estrndup((char*)inval, found - (char*)inval);
324 		(*value) = estrdup(++found);
325 	} else {
326 		(*value) = estrdup((char*)inval);
327 		(*namespace) = NULL;
328 	}
329 
330 	return FALSE;
331 }
332