-
Notifications
You must be signed in to change notification settings - Fork 8k
Expand file tree
/
Copy pathphp_xml.c
More file actions
294 lines (268 loc) · 7.53 KB
/
php_xml.c
File metadata and controls
294 lines (268 loc) · 7.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
+----------------------------------------------------------------------+
| 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 bool is_blank(const xmlChar* str)
{
while (*str != '\0') {
if (*str != ' ' && *str != 0x9 && *str != 0xa && *str != 0xd) {
return false;
}
str++;
}
return true;
}
/* removes all empty text, comments and other insignificant 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)
{
}
/* Consumes `ctxt` */
static xmlDocPtr soap_xmlParse_ex(xmlParserCtxtPtr ctxt)
{
xmlDocPtr ret;
if (ctxt) {
#if LIBXML_VERSION >= 21300
xmlCtxtSetOptions(ctxt, XML_PARSE_HUGE | XML_PARSE_NO_XXE | XML_PARSE_NONET | XML_PARSE_NOBLANKS);
#else
php_libxml_sanitize_parse_ctxt_options(ctxt);
ZEND_DIAGNOSTIC_IGNORED_START("-Wdeprecated-declarations")
ctxt->keepBlanks = 0;
ctxt->options |= XML_PARSE_HUGE;
ZEND_DIAGNOSTIC_IGNORED_END
#endif
ctxt->sax->ignorableWhitespace = soap_ignorableWhitespace;
ctxt->sax->comment = soap_Comment;
ctxt->sax->warning = NULL;
ctxt->sax->error = NULL;
/*ctxt->sax->fatalError = NULL;*/
bool old = php_libxml_disable_entity_loader(true);
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;
}
return ret;
}
xmlDocPtr soap_xmlParseFile(const char *filename)
{
bool old_allow_url_fopen = PG(allow_url_fopen);
PG(allow_url_fopen) = true;
xmlParserCtxtPtr ctxt = xmlCreateFileParserCtxt(filename);
PG(allow_url_fopen) = old_allow_url_fopen;
xmlDocPtr ret = soap_xmlParse_ex(ctxt);
if (ret) {
cleanup_xml_node((xmlNodePtr)ret);
}
return ret;
}
xmlDocPtr soap_xmlParseMemory(const void *buf, size_t buf_size)
{
xmlParserCtxtPtr ctxt = xmlCreateMemoryParserCtxt(buf, buf_size);
xmlDocPtr ret = soap_xmlParse_ex(ctxt);
/*
if (ret) {
cleanup_xml_node((xmlNodePtr)ret);
}
*/
return ret;
}
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, const char *name, const char *ns)
{
if (node->name && strcmp((const char *) node->name, name) == 0) {
xmlNsPtr nsPtr = node->ns;
if (ns) {
if (nsPtr) {
return (strcmp((const char *) nsPtr->href, ns) == 0);
} else {
return FALSE;
}
} else if (nsPtr) {
return FALSE;
}
return TRUE;
}
return FALSE;
}
int node_is_equal_ex(xmlNodePtr node, const char *name, const 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((const char *) nsPtr->href, ns) == 0;
} else {
return FALSE;
}
}
return TRUE;
}
return FALSE;
}
int node_is_equal_ex_one_of(xmlNodePtr node, const char *name, const char *const *namespaces)
{
if ((node->name) && strcmp((char*)node->name, name) == 0) {
xmlNsPtr nsPtr = node_find_ns(node);
if (nsPtr) {
do {
if (strcmp((const char *) nsPtr->href, *namespaces) == 0) {
return TRUE;
}
namespaces++;
} while (*namespaces != NULL);
}
return FALSE;
}
return FALSE;
}
xmlAttrPtr get_attribute_any_ns(xmlAttrPtr node, const char *name)
{
while (node!=NULL) {
if (node->name && strcmp((const char *) node->name, name) == 0) {
return node;
}
node = node->next;
}
return NULL;
}
/* Finds an attribute by name and namespace.
* If ns is NULL, the attribute must not be in any namespace.
* If ns is not NULL, the attribute must be in the specified namespace.
*/
xmlAttrPtr get_attribute_ex(xmlAttrPtr node, const char *name, const 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, const char *name, const char *ns)
{
while (node!=NULL) {
if (node_is_equal_ex(node, name, ns)) {
return node;
}
node = node->next;
}
return NULL;
}
xmlNodePtr get_node_with_attribute_ex(xmlNodePtr node, const char *name, const char *name_ns, const char *attribute, const char *value, const 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, const char *name, const char *name_ns, const char *attribute, const char *value, const 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;
}
/* namespace is either a copy or NULL, value is never NULL and never a copy. */
void parse_namespace(const xmlChar *inval, const char **value, char **namespace)
{
const char *found = strrchr((const char *) inval, ':');
if (found != NULL && found != (const char *) inval) {
(*namespace) = estrndup((const char *) inval, found - (const char *) inval);
(*value) = ++found;
} else {
(*value) = (const char *) inval;
(*namespace) = NULL;
}
}