summaryrefslogtreecommitdiff
path: root/ext/soap/php_xml.c
blob: 786ebc97a54568552ff7a3ca4a6388e5038633a1 (plain)
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
#include "php_soap.h"

/* Channel libxml file io layer through the PHP streams subsystem.
 * This allows use of ftps:// and https:// urls */

int php_stream_xmlIO_match_wrapper(const char *filename)
{
	TSRMLS_FETCH();
	return php_stream_locate_url_wrapper(filename, NULL, STREAM_LOCATE_WRAPPERS_ONLY TSRMLS_CC) ? 1 : 0;

}

void *php_stream_xmlIO_open_wrapper(const char *filename)
{
	TSRMLS_FETCH();
	return php_stream_open_wrapper(filename, "rb", ENFORCE_SAFE_MODE|REPORT_ERRORS, NULL);
}

int php_stream_xmlIO_read(void *context, char *buffer, int len)
{
	TSRMLS_FETCH();
	return php_stream_read((php_stream*)context, buffer, len);
}

int php_stream_xmlIO_close(void *context)
{
	TSRMLS_FETCH();
	return php_stream_close((php_stream*)context);
}

xmlNsPtr attr_find_ns(xmlAttrPtr node)
{
	if(node->ns)
		return node->ns;
	else if(node->parent->ns)
		return node->parent->ns;
	else
		return xmlSearchNs(node->doc, node->parent, NULL);
}

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, char *name, char *ns)
{
	if(!strcmp(node->name, name))
	{
		if(ns)
		{
			xmlNsPtr nsPtr;
			if(node->ns)
				nsPtr = node->ns;
			else if(node->parent->ns)
				nsPtr = node->parent->ns;
			else
				nsPtr = xmlSearchNs(node->doc, node->parent, NULL);
			if(!strcmp(nsPtr->href, ns))
				return TRUE;
			return FALSE;
		}
		return TRUE;
	}
	return FALSE;
}

int node_is_equal_ex(xmlNodePtr node, char *name, char *ns)
{
	if(!strcmp(node->name, name))
	{
		if(ns)
		{
			xmlNsPtr nsPtr;
			if(node->ns)
				nsPtr = node->ns;
			else
				nsPtr = xmlSearchNs(node->doc, node, NULL);
			if(!strcmp(nsPtr->href, ns))
				return TRUE;
			return FALSE;
		}
		return TRUE;
	}
	return FALSE;
}

xmlAttrPtr get_attribute_ex(xmlAttrPtr node, char *name, char *ns)
{
	xmlAttrPtr trav = node;
	if(node == NULL) return NULL;
	do {
		if(attr_is_equal_ex(trav, name, ns))
			return trav;
	} while(trav = trav->next);
	return NULL;
}

xmlNodePtr get_node_ex(xmlNodePtr node, char *name, char *ns)
{
	xmlNodePtr trav = node;
	if(node == NULL) return NULL;
	do {
		if(node_is_equal_ex(trav, name, ns))
			return trav;
	} while(trav = trav->next);
	return NULL;
}

xmlNodePtr get_node_recurisve_ex(xmlNodePtr node, char *name, char *ns)
{
	xmlNodePtr trav = node;
	if(node == NULL) return NULL;
	do
	{
		if(node_is_equal_ex(trav, name, ns))
			return trav;
		else
		{
			if(node->children != NULL)
			{
				xmlNodePtr tmp;
				tmp = get_node_recurisve_ex(node->children, name, ns);
				if(tmp)
					return tmp;
			}
		}
	} while(trav = trav->next);
	return NULL;
}

xmlNodePtr get_node_with_attribute_ex(xmlNodePtr node, char *name, char *name_ns, char *attribute, char *value, char *attr_ns)
{
	xmlNodePtr trav = node, cur = NULL;
	xmlAttrPtr attr;

	if(node == NULL) return NULL;
	do
	{
		if(name != NULL)
		{
			cur = get_node_ex(trav, name, name_ns);
			if(!cur)
				return cur;
		}
		else
			cur = trav;

		attr = get_attribute_ex(cur->properties, attribute, attr_ns);
		if(attr != NULL && strcmp(attr->children->content, value) == 0)
			return cur;
		else
		{
			if(cur->children != NULL)
			{
				xmlNodePtr tmp;
				tmp = get_node_with_attribute_ex(cur->children, name, name_ns, attribute, value, attr_ns);
				if(tmp)
					return tmp;
			}
		}
	}while(trav = trav->next);
	return NULL;
}

xmlNodePtr get_node_with_attribute_recursive_ex(xmlNodePtr node, char *name, char *name_ns, char *attribute, char *value, char *attr_ns)
{
	xmlNodePtr trav = node, cur;
	xmlAttrPtr attr;

	if(node == NULL) return NULL;
	do
	{
		if(name != NULL)
		{
			cur = get_node_recurisve_ex(trav, name, name_ns);
			if(!cur)
				return cur;
		}
		else
			cur = trav;

		attr = get_attribute_ex(cur->properties, attribute, attr_ns);
		if(attr != NULL && strcmp(attr->children->content, value) == 0)
			return cur;
		else
		{
			if(cur->children != NULL)
			{
				xmlNodePtr tmp;
				tmp = get_node_with_attribute_recursive_ex(cur->children, name, name_ns, attribute, value, attr_ns);
				if(tmp)
					return tmp;
			}
		}
	}while(trav = trav->next);
	return NULL;
}

xmlNodePtr check_and_resolve_href(xmlNodePtr data)
{
	xmlAttrPtr href;
	xmlNodePtr ret = data;

	if(!data || !data->properties)
		return ret;

	href = get_attribute(data->properties, "href");
	if(href)
	{
		/*  Internal href try and find node */
		if(href->children->content[0] == '#')
		{
			ret = get_node_with_attribute_recursive(data->doc->children, NULL, "id", &href->children->content[1]);
		}
		/*  External href....? */
	}

	return ret;
}

int parse_namespace(char *inval, char **value, char **namespace)
{
	char *found = strchr(inval, ':');

	if(found != NULL)
	{
		(*namespace) = estrndup(inval, found - inval);
		(*value) = estrdup(++found);
	}
	else
	{
		(*value) = estrdup(inval);
		(*namespace) = NULL;
	}

	return FALSE;
}