xref: /php-src/ext/xsl/xsltprocessor.c (revision 2d029efd)
1 /*
2    +----------------------------------------------------------------------+
3    | Copyright (c) The PHP Group                                          |
4    +----------------------------------------------------------------------+
5    | This source file is subject to version 3.01 of the PHP license,      |
6    | that is bundled with this package in the file LICENSE, and is        |
7    | available through the world-wide-web at the following url:           |
8    | https://www.php.net/license/3_01.txt                                 |
9    | If you did not receive a copy of the PHP license and are unable to   |
10    | obtain it through the world-wide-web, please send a note to          |
11    | license@php.net so we can mail you a copy immediately.               |
12    +----------------------------------------------------------------------+
13    | Authors: Christian Stocker <chregu@php.net>                          |
14    |          Rob Richards <rrichards@php.net>                            |
15    +----------------------------------------------------------------------+
16 */
17 
18 #ifdef HAVE_CONFIG_H
19 #include <config.h>
20 #endif
21 
22 #include "php.h"
23 #include "php_xsl.h"
24 #include <libxslt/variables.h>
25 #include "ext/libxml/php_libxml.h"
26 #include "ext/dom/namespace_compat.h"
27 
28 
php_xsl_xslt_apply_params(xsltTransformContextPtr ctxt,HashTable * params)29 static zend_result php_xsl_xslt_apply_params(xsltTransformContextPtr ctxt, HashTable *params)
30 {
31 	zend_string *string_key;
32 	zval *value;
33 
34 	ZEND_HASH_MAP_FOREACH_STR_KEY_VAL(params, string_key, value) {
35 		ZEND_ASSERT(string_key != NULL);
36 		/* Already a string because of setParameter() */
37 		ZEND_ASSERT(Z_TYPE_P(value) == IS_STRING);
38 
39 		int result = xsltQuoteOneUserParam(ctxt, (const xmlChar *) ZSTR_VAL(string_key), (const xmlChar *) Z_STRVAL_P(value));
40 		if (result < 0) {
41 			php_error_docref(NULL, E_WARNING, "Could not apply parameter \"%s\"", ZSTR_VAL(string_key));
42 			return FAILURE;
43 		}
44 	} ZEND_HASH_FOREACH_END();
45 
46 	return SUCCESS;
47 }
48 
xsl_proxy_factory(xmlNodePtr node,zval * child,dom_object * intern,xmlXPathParserContextPtr ctxt)49 static void xsl_proxy_factory(xmlNodePtr node, zval *child, dom_object *intern, xmlXPathParserContextPtr ctxt)
50 {
51 	ZEND_ASSERT(node->type != XML_NAMESPACE_DECL);
52 
53 	/**
54 	 * Upon freeing libxslt's context, every document that is not the *main* document will be freed by libxslt.
55 	 * If a node of a document that is *not the main* document gets returned to userland, we'd free the node twice:
56 	 * first by the cleanup of the xslt context, and then by our own refcounting mechanism.
57 	 * To prevent this, we'll take a copy if the node is not from the main document.
58 	 * It is important that we do not copy the node unconditionally, because that means that:
59 	 *  - modifications to the node will only modify the copy, and not the original
60 	 *  - accesses to the parent, path, ... will not work
61 	 */
62 	xsltTransformContextPtr transform_ctxt = (xsltTransformContextPtr) ctxt->context->extra;
63 	if (node->doc != transform_ctxt->document->doc) {
64 		node = xmlDocCopyNode(node, intern->document->ptr, 1);
65 	}
66 	php_dom_create_object(node, child, intern);
67 }
68 
xsl_ext_fetch_intern(xmlXPathParserContextPtr ctxt)69 static xsl_object *xsl_ext_fetch_intern(xmlXPathParserContextPtr ctxt)
70 {
71 	if (UNEXPECTED(!zend_is_executing())) {
72 		xsltGenericError(xsltGenericErrorContext,
73 		"xsltExtFunctionTest: Function called from outside of PHP\n");
74 		return NULL;
75 	}
76 
77 	xsltTransformContextPtr tctxt = xsltXPathGetTransformContext(ctxt);
78 	if (UNEXPECTED(tctxt == NULL)) {
79 		xsltGenericError(xsltGenericErrorContext,
80 		"xsltExtFunctionTest: failed to get the transformation context\n");
81 		return NULL;
82 	}
83 
84 	xsl_object *intern = (xsl_object *) tctxt->_private;
85 	if (UNEXPECTED(intern == NULL)) {
86 		xsltGenericError(xsltGenericErrorContext,
87 		"xsltExtFunctionTest: failed to get the internal object\n");
88 		return NULL;
89 	}
90 	return intern;
91 }
92 
xsl_ext_function_php(xmlXPathParserContextPtr ctxt,int nargs,php_dom_xpath_nodeset_evaluation_mode evaluation_mode)93 static void xsl_ext_function_php(xmlXPathParserContextPtr ctxt, int nargs, php_dom_xpath_nodeset_evaluation_mode evaluation_mode) /* {{{ */
94 {
95 	xsl_object *intern = xsl_ext_fetch_intern(ctxt);
96 	if (!intern) {
97 		php_dom_xpath_callbacks_clean_argument_stack(ctxt, nargs);
98 	} else {
99 		php_dom_xpath_callbacks_call_php_ns(&intern->xpath_callbacks, ctxt, nargs, evaluation_mode, (dom_object *) intern->doc, xsl_proxy_factory);
100 	}
101 }
102 /* }}} */
103 
xsl_ext_function_string_php(xmlXPathParserContextPtr ctxt,int nargs)104 void xsl_ext_function_string_php(xmlXPathParserContextPtr ctxt, int nargs) /* {{{ */
105 {
106 	xsl_ext_function_php(ctxt, nargs, PHP_DOM_XPATH_EVALUATE_NODESET_TO_STRING);
107 }
108 /* }}} */
109 
xsl_ext_function_object_php(xmlXPathParserContextPtr ctxt,int nargs)110 void xsl_ext_function_object_php(xmlXPathParserContextPtr ctxt, int nargs) /* {{{ */
111 {
112 	xsl_ext_function_php(ctxt, nargs, PHP_DOM_XPATH_EVALUATE_NODESET_TO_NODESET);
113 }
114 /* }}} */
115 
xsl_ext_function_trampoline(xmlXPathParserContextPtr ctxt,int nargs)116 static void xsl_ext_function_trampoline(xmlXPathParserContextPtr ctxt, int nargs)
117 {
118 	xsl_object *intern = xsl_ext_fetch_intern(ctxt);
119 	if (!intern) {
120 		php_dom_xpath_callbacks_clean_argument_stack(ctxt, nargs);
121 	} else {
122 		php_dom_xpath_callbacks_call_custom_ns(&intern->xpath_callbacks, ctxt, nargs, PHP_DOM_XPATH_EVALUATE_NODESET_TO_NODESET, (dom_object *) intern->doc, xsl_proxy_factory);
123 	}
124 }
125 
xsl_add_ns_to_map(xmlHashTablePtr table,xsltStylesheetPtr sheet,const xmlNode * cur,const xmlChar * prefix,const xmlChar * uri)126 static void xsl_add_ns_to_map(xmlHashTablePtr table, xsltStylesheetPtr sheet, const xmlNode *cur, const xmlChar *prefix, const xmlChar *uri)
127 {
128 	const xmlChar *existing_url = xmlHashLookup(table, prefix);
129 	if (existing_url != NULL && !xmlStrEqual(existing_url, uri)) {
130 		xsltTransformError(NULL, sheet, (xmlNodePtr) cur, "Namespaces prefix %s used for multiple namespaces\n", prefix);
131 		sheet->warnings++;
132 	} else if (existing_url == NULL) {
133 		xmlHashUpdateEntry(table, prefix, (void *) uri, NULL);
134 	}
135 }
136 
137 /* Adds all namespace declaration (not using nsDef) into a hash map that maps prefix to uri. Warns on conflicting declarations. */
xsl_build_ns_map(xmlHashTablePtr table,xsltStylesheetPtr sheet,php_dom_libxml_ns_mapper * ns_mapper,const xmlDoc * doc)138 static void xsl_build_ns_map(xmlHashTablePtr table, xsltStylesheetPtr sheet, php_dom_libxml_ns_mapper *ns_mapper, const xmlDoc *doc)
139 {
140 	const xmlNode *cur = xmlDocGetRootElement(doc);
141 
142 	while (cur != NULL) {
143 		if (cur->type == XML_ELEMENT_NODE) {
144 			if (cur->ns != NULL && cur->ns->prefix != NULL) {
145 				xsl_add_ns_to_map(table, sheet, cur, cur->ns->prefix, cur->ns->href);
146 			}
147 
148 			for (const xmlAttr *attr = cur->properties; attr != NULL; attr = attr->next) {
149 				if (attr->ns != NULL && attr->ns->prefix != NULL && php_dom_ns_is_fast_ex(attr->ns, php_dom_ns_is_xmlns_magic_token)
150 					&& attr->children != NULL && attr->children->content != NULL) {
151 					/* This attribute declares a namespace, get the relevant instance.
152 					 * The declared namespace is not the same as the namespace of this attribute (which is xmlns). */
153 					const xmlChar *prefix = attr->name;
154 					xmlNsPtr ns = php_dom_libxml_ns_mapper_get_ns_raw_strings_nullsafe(ns_mapper, (const char *) prefix, (const char *) attr->children->content);
155 					xsl_add_ns_to_map(table, sheet, cur, prefix, ns->href);
156 				}
157 			}
158 		}
159 
160 		cur = php_dom_next_in_tree_order(cur, (const xmlNode *) doc);
161 	}
162 }
163 
164 /* Apply namespace corrections for new DOM */
165 typedef enum {
166 	XSL_NS_HASH_CORRECTION_NONE = 0,
167 	XSL_NS_HASH_CORRECTION_APPLIED = 1,
168 	XSL_NS_HASH_CORRECTION_FAILED = 2
169 } xsl_ns_hash_correction_status;
170 
xsl_apply_ns_hash_corrections(xsltStylesheetPtr sheetp,xmlNodePtr nodep,xmlDocPtr doc)171 static zend_always_inline xsl_ns_hash_correction_status xsl_apply_ns_hash_corrections(xsltStylesheetPtr sheetp, xmlNodePtr nodep, xmlDocPtr doc)
172 {
173 	if (sheetp->nsHash == NULL) {
174 		dom_object *node_intern = php_dom_object_get_data(nodep);
175 		if (node_intern != NULL && php_dom_follow_spec_intern(node_intern)) {
176 			sheetp->nsHash = xmlHashCreate(10);
177 			if (UNEXPECTED(!sheetp->nsHash)) {
178 				return XSL_NS_HASH_CORRECTION_FAILED;
179 			}
180 			xsl_build_ns_map(sheetp->nsHash, sheetp, php_dom_get_ns_mapper(node_intern), doc);
181 			return XSL_NS_HASH_CORRECTION_APPLIED;
182 		}
183 	}
184 	return XSL_NS_HASH_CORRECTION_NONE;
185 }
186 
187 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#
188 Since:
189 */
PHP_METHOD(XSLTProcessor,importStylesheet)190 PHP_METHOD(XSLTProcessor, importStylesheet)
191 {
192 	zval *id, *docp = NULL;
193 	xmlDoc *doc = NULL, *newdoc = NULL;
194 	xsltStylesheetPtr sheetp;
195 	bool clone_docu = false;
196 	xmlNode *nodep = NULL;
197 	zval *cloneDocu, rv;
198 	zend_string *member;
199 
200 	id = ZEND_THIS;
201 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "o", &docp) == FAILURE) {
202 		RETURN_THROWS();
203 	}
204 
205 	nodep = php_libxml_import_node(docp);
206 
207 	if (nodep) {
208 		doc = nodep->doc;
209 	}
210 	if (doc == NULL) {
211 		zend_argument_type_error(1, "must be a valid XML node");
212 		RETURN_THROWS();
213 	}
214 
215 	/* libxslt uses _private, so we must copy the imported
216 	stylesheet document otherwise the node proxies will be a mess */
217 	newdoc = xmlCopyDoc(doc, 1);
218 	xmlNodeSetBase((xmlNodePtr) newdoc, (xmlChar *)doc->URL);
219 	PHP_LIBXML_SANITIZE_GLOBALS(parse);
220 	ZEND_DIAGNOSTIC_IGNORED_START("-Wdeprecated-declarations")
221 	xmlSubstituteEntitiesDefault(1);
222 	xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS;
223 	ZEND_DIAGNOSTIC_IGNORED_END
224 
225 	sheetp = xsltParseStylesheetDoc(newdoc);
226 	PHP_LIBXML_RESTORE_GLOBALS(parse);
227 
228 	if (!sheetp) {
229 		xmlFreeDoc(newdoc);
230 		RETURN_FALSE;
231 	}
232 
233 	xsl_object *intern = Z_XSL_P(id);
234 
235 	xsl_ns_hash_correction_status status = xsl_apply_ns_hash_corrections(sheetp, nodep, doc);
236 	if (UNEXPECTED(status == XSL_NS_HASH_CORRECTION_FAILED)) {
237 		xsltFreeStylesheet(sheetp);
238 		xmlFreeDoc(newdoc);
239 		RETURN_FALSE;
240 	} else if (status == XSL_NS_HASH_CORRECTION_APPLIED) {
241 		/* The namespace mappings need to be kept alive.
242 		 * This is stored in the ref obj outside of libxml2, but that means that the sheet won't keep it alive
243 		 * unlike with namespaces from old DOM. */
244 		if (intern->sheet_ref_obj) {
245 			php_libxml_decrement_doc_ref_directly(intern->sheet_ref_obj);
246 		}
247 		intern->sheet_ref_obj = Z_LIBXML_NODE_P(docp)->document;
248 		intern->sheet_ref_obj->refcount++;
249 	}
250 
251 	member = ZSTR_INIT_LITERAL("cloneDocument", 0);
252 	cloneDocu = zend_std_read_property(Z_OBJ_P(id), member, BP_VAR_R, NULL, &rv);
253 	clone_docu = zend_is_true(cloneDocu);
254 	zend_string_release_ex(member, 0);
255 	if (!clone_docu) {
256 		/* Check if the stylesheet is using xsl:key, if yes, we have to clone the document _always_ before a transformation.
257 		 * xsl:key elements may only occur at the top level. Furthermore, all elements at the top level must be in a
258 		 * namespace (if not, then the stylesheet is not well-formed and this function will have returned false earlier). */
259 		nodep = xmlDocGetRootElement(sheetp->doc);
260 		if (nodep && (nodep = nodep->children)) {
261 			while (nodep) {
262 				ZEND_ASSERT(nodep->ns != NULL);
263 				if (nodep->type == XML_ELEMENT_NODE && xmlStrEqual(nodep->name, (const xmlChar *) "key") && xmlStrEqual(nodep->ns->href, XSLT_NAMESPACE)) {
264 					intern->hasKeys = true;
265 					break;
266 				}
267 				nodep = nodep->next;
268 			}
269 		}
270 	} else {
271 		intern->hasKeys = true;
272 	}
273 
274 	xsl_free_sheet(intern);
275 
276 	php_xsl_set_object(id, sheetp);
277 	RETVAL_TRUE;
278 }
279 /* }}} end XSLTProcessor::importStylesheet */
280 
php_xsl_delayed_lib_registration(void * ctxt,const zend_string * ns,const zend_string * name)281 static void php_xsl_delayed_lib_registration(void *ctxt, const zend_string *ns, const zend_string *name)
282 {
283 	xsltTransformContextPtr xsl = (xsltTransformContextPtr) ctxt;
284 	xsltRegisterExtFunction(xsl, (const xmlChar *) ZSTR_VAL(name), (const xmlChar *) ZSTR_VAL(ns), xsl_ext_function_trampoline);
285 }
286 
php_xsl_apply_stylesheet(zval * id,xsl_object * intern,xsltStylesheetPtr style,zval * docp)287 static xmlDocPtr php_xsl_apply_stylesheet(zval *id, xsl_object *intern, xsltStylesheetPtr style, zval *docp) /* {{{ */
288 {
289 	xmlDocPtr newdocp = NULL;
290 	xmlDocPtr doc = NULL;
291 	xmlNodePtr node = NULL;
292 	xsltTransformContextPtr ctxt;
293 	php_libxml_node_object *object;
294 	zval *doXInclude, rv;
295 	zend_string *member;
296 	FILE *f;
297 	int secPrefsError = 0;
298 	xsltSecurityPrefsPtr secPrefs = NULL;
299 
300 	node = php_libxml_import_node(docp);
301 
302 	if (node) {
303 		doc = node->doc;
304 	}
305 
306 	if (doc == NULL) {
307 		zend_argument_type_error(1, "must be a valid XML node");
308 		return NULL;
309 	}
310 
311 	if (style == NULL) {
312 		zend_string *name = get_active_function_or_method_name();
313 		zend_throw_error(NULL, "%s() can only be called after a stylesheet has been imported",
314 			ZSTR_VAL(name));
315 		zend_string_release(name);
316 		return NULL;
317 	}
318 
319 	if (intern->profiling) {
320 		if (php_check_open_basedir(ZSTR_VAL(intern->profiling))) {
321 			f = NULL;
322 		} else {
323 			f = VCWD_FOPEN(ZSTR_VAL(intern->profiling), "w");
324 		}
325 	} else {
326 		f = NULL;
327 	}
328 
329 	intern->doc = emalloc(sizeof(php_libxml_node_object));
330 	memset(intern->doc, 0, sizeof(php_libxml_node_object));
331 
332 	if (intern->hasKeys) {
333 		doc = xmlCopyDoc(doc, 1);
334 	} else {
335 		object = Z_LIBXML_NODE_P(docp);
336 		intern->doc->document = object->document;
337 	}
338 
339 	php_libxml_increment_doc_ref(intern->doc, doc);
340 
341 	ctxt = xsltNewTransformContext(style, doc);
342 	ctxt->_private = (void *) intern;
343 
344 	if (intern->parameter) {
345 		zend_result status = php_xsl_xslt_apply_params(ctxt, intern->parameter);
346 		if (UNEXPECTED(status != SUCCESS) && EG(exception)) {
347 			goto out;
348 		}
349 	}
350 
351 	member = ZSTR_INIT_LITERAL("doXInclude", 0);
352 	doXInclude = zend_std_read_property(Z_OBJ_P(id), member, BP_VAR_R, NULL, &rv);
353 	ctxt->xinclude = zend_is_true(doXInclude);
354 	zend_string_release_ex(member, 0);
355 
356 	zval *max_template_depth = xsl_prop_max_template_depth(Z_OBJ_P(id));
357 	ZEND_ASSERT(Z_TYPE_P(max_template_depth) == IS_LONG);
358 	ctxt->maxTemplateDepth = Z_LVAL_P(max_template_depth);
359 
360 	zval *max_template_vars = xsl_prop_max_template_vars(Z_OBJ_P(id));
361 	ZEND_ASSERT(Z_TYPE_P(max_template_vars) == IS_LONG);
362 	ctxt->maxTemplateVars = Z_LVAL_P(max_template_vars);
363 
364 	zend_long secPrefsValue = intern->securityPrefs;
365 
366 	/* if securityPrefs is set to NONE, we don't have to do any checks, but otherwise... */
367 	if (secPrefsValue != XSL_SECPREF_NONE) {
368 		secPrefs = xsltNewSecurityPrefs();
369 		if (secPrefsValue & XSL_SECPREF_READ_FILE ) {
370 			if (0 != xsltSetSecurityPrefs(secPrefs, XSLT_SECPREF_READ_FILE, xsltSecurityForbid)) {
371 				secPrefsError = 1;
372 			}
373 		}
374 		if (secPrefsValue & XSL_SECPREF_WRITE_FILE ) {
375 			if (0 != xsltSetSecurityPrefs(secPrefs, XSLT_SECPREF_WRITE_FILE, xsltSecurityForbid)) {
376 				secPrefsError = 1;
377 			}
378 		}
379 		if (secPrefsValue & XSL_SECPREF_CREATE_DIRECTORY ) {
380 			if (0 != xsltSetSecurityPrefs(secPrefs, XSLT_SECPREF_CREATE_DIRECTORY, xsltSecurityForbid)) {
381 				secPrefsError = 1;
382 			}
383 		}
384 		if (secPrefsValue & XSL_SECPREF_READ_NETWORK) {
385 			if (0 != xsltSetSecurityPrefs(secPrefs, XSLT_SECPREF_READ_NETWORK, xsltSecurityForbid)) {
386 				secPrefsError = 1;
387 			}
388 		}
389 		if (secPrefsValue & XSL_SECPREF_WRITE_NETWORK) {
390 			if (0 != xsltSetSecurityPrefs(secPrefs, XSLT_SECPREF_WRITE_NETWORK, xsltSecurityForbid)) {
391 				secPrefsError = 1;
392 			}
393 		}
394 
395 		if (0 != xsltSetCtxtSecurityPrefs(secPrefs, ctxt)) {
396 			secPrefsError = 1;
397 		}
398 	}
399 
400 	php_dom_xpath_callbacks_delayed_lib_registration(&intern->xpath_callbacks, ctxt, php_xsl_delayed_lib_registration);
401 
402 	if (secPrefsError == 1) {
403 		php_error_docref(NULL, E_WARNING, "Can't set libxslt security properties, not doing transformation for security reasons");
404 	} else {
405 		newdocp = xsltApplyStylesheetUser(style, doc, /* params (handled manually) */ NULL, /* output */ NULL, f, ctxt);
406 	}
407 
408 out:
409 	if (f) {
410 		fclose(f);
411 	}
412 
413 	xsltFreeTransformContext(ctxt);
414 	if (secPrefs) {
415 		xsltFreeSecurityPrefs(secPrefs);
416 	}
417 
418 	php_dom_xpath_callbacks_clean_node_list(&intern->xpath_callbacks);
419 
420 	php_libxml_decrement_doc_ref(intern->doc);
421 	efree(intern->doc);
422 	intern->doc = NULL;
423 
424 	return newdocp;
425 
426 }
427 /* }}} */
428 
429 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#
430 Since:
431 */
PHP_METHOD(XSLTProcessor,transformToDoc)432 PHP_METHOD(XSLTProcessor, transformToDoc)
433 {
434 	zval *id, *docp = NULL;
435 	xmlDoc *newdocp;
436 	xsltStylesheetPtr sheetp;
437 	zend_class_entry *ret_class = NULL;
438 	xsl_object *intern;
439 
440 	id = ZEND_THIS;
441 	intern = Z_XSL_P(id);
442 	sheetp = (xsltStylesheetPtr) intern->ptr;
443 
444 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "o|C!", &docp, &ret_class) == FAILURE) {
445 		RETURN_THROWS();
446 	}
447 
448 	newdocp = php_xsl_apply_stylesheet(id, intern, sheetp, docp);
449 
450 	if (newdocp) {
451 		if (ret_class) {
452 			zend_string *curclass_name;
453 			zend_class_entry *curce;
454 			php_libxml_node_object *interndoc;
455 
456 			curce = Z_OBJCE_P(docp);
457 			curclass_name = curce->name;
458 			while (curce->parent != NULL) {
459 				curce = curce->parent;
460 			}
461 
462 			if (!instanceof_function(ret_class, curce)) {
463 				xmlFreeDoc(newdocp);
464 				zend_argument_type_error(2, "must be a class name compatible with %s, %s given",
465 					ZSTR_VAL(curclass_name), ZSTR_VAL(ret_class->name)
466 				);
467 				RETURN_THROWS();
468 			}
469 
470 			object_init_ex(return_value, ret_class);
471 
472 			interndoc = Z_LIBXML_NODE_P(return_value);
473 			php_libxml_increment_doc_ref(interndoc, newdocp);
474 			php_libxml_increment_node_ptr(interndoc, (xmlNodePtr)newdocp, (void *)interndoc);
475 		} else {
476 			php_dom_create_object((xmlNodePtr) newdocp, return_value, NULL);
477 		}
478 	} else {
479 		RETURN_FALSE;
480 	}
481 }
482 /* }}} end XSLTProcessor::transformToDoc */
483 
484 /* {{{ */
PHP_METHOD(XSLTProcessor,transformToUri)485 PHP_METHOD(XSLTProcessor, transformToUri)
486 {
487 	zval *id, *docp = NULL;
488 	xmlDoc *newdocp;
489 	xsltStylesheetPtr sheetp;
490 	int ret;
491 	size_t uri_len;
492 	char *uri;
493 	xsl_object *intern;
494 
495 	id = ZEND_THIS;
496 	intern = Z_XSL_P(id);
497 	sheetp = (xsltStylesheetPtr) intern->ptr;
498 
499 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "op", &docp, &uri, &uri_len) == FAILURE) {
500 		RETURN_THROWS();
501 	}
502 
503 	newdocp = php_xsl_apply_stylesheet(id, intern, sheetp, docp);
504 
505 	ret = -1;
506 	if (newdocp) {
507 		ret = xsltSaveResultToFilename(uri, newdocp, sheetp, 0);
508 		xmlFreeDoc(newdocp);
509 	}
510 
511 	RETVAL_LONG(ret);
512 }
513 /* }}} end XSLTProcessor::transformToUri */
514 
515 /* {{{ */
PHP_METHOD(XSLTProcessor,transformToXml)516 PHP_METHOD(XSLTProcessor, transformToXml)
517 {
518 	zval *id, *docp = NULL;
519 	xmlDoc *newdocp;
520 	xsltStylesheetPtr sheetp;
521 	int ret;
522 	xmlChar *doc_txt_ptr;
523 	int doc_txt_len;
524 	xsl_object *intern;
525 
526 	id = ZEND_THIS;
527 	intern = Z_XSL_P(id);
528 	sheetp = (xsltStylesheetPtr) intern->ptr;
529 
530 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "o", &docp) == FAILURE) {
531 		RETURN_THROWS();
532 	}
533 
534 	newdocp = php_xsl_apply_stylesheet(id, intern, sheetp, docp);
535 
536 	ret = -1;
537 	if (newdocp) {
538 		ret = xsltSaveResultToString(&doc_txt_ptr, &doc_txt_len, newdocp, sheetp);
539 		if (doc_txt_ptr && doc_txt_len) {
540 			RETVAL_STRINGL((char *) doc_txt_ptr, doc_txt_len);
541 			xmlFree(doc_txt_ptr);
542 		}
543 		xmlFreeDoc(newdocp);
544 	}
545 
546 	if (ret < 0) {
547 		RETURN_FALSE;
548 	}
549 }
550 /* }}} end XSLTProcessor::transformToXml */
551 
552 /* {{{ */
PHP_METHOD(XSLTProcessor,setParameter)553 PHP_METHOD(XSLTProcessor, setParameter)
554 {
555 
556 	zval *id = ZEND_THIS;
557 	zval *entry, new_string;
558 	HashTable *array_value;
559 	xsl_object *intern;
560 	char *namespace;
561 	size_t namespace_len;
562 	zend_string *string_key, *name, *value = NULL;
563 
564 	ZEND_PARSE_PARAMETERS_START(2, 3)
565 		Z_PARAM_STRING(namespace, namespace_len)
566 		Z_PARAM_ARRAY_HT_OR_STR(array_value, name)
567 		Z_PARAM_OPTIONAL
568 		Z_PARAM_PATH_STR_OR_NULL(value)
569 	ZEND_PARSE_PARAMETERS_END();
570 
571 	intern = Z_XSL_P(id);
572 
573 	if (array_value) {
574 		if (value) {
575 			zend_argument_value_error(3, "must be null when argument #2 ($name) is an array");
576 			RETURN_THROWS();
577 		}
578 
579 		ZEND_HASH_FOREACH_STR_KEY_VAL(array_value, string_key, entry) {
580 			zval tmp;
581 			zend_string *str;
582 
583 			if (string_key == NULL) {
584 				zend_argument_type_error(2, "must contain only string keys");
585 				RETURN_THROWS();
586 			}
587 
588 			if (UNEXPECTED(CHECK_NULL_PATH(ZSTR_VAL(string_key), ZSTR_LEN(string_key)))) {
589 				zend_argument_value_error(3, "must not contain keys with any null bytes");
590 				RETURN_THROWS();
591 			}
592 
593 			str = zval_try_get_string(entry);
594 			if (UNEXPECTED(!str)) {
595 				RETURN_THROWS();
596 			}
597 
598 			if (UNEXPECTED(CHECK_NULL_PATH(ZSTR_VAL(str), ZSTR_LEN(str)))) {
599 				zend_string_release(str);
600 				zend_argument_value_error(3, "must not contain values with any null bytes");
601 				RETURN_THROWS();
602 			}
603 
604 			ZVAL_STR(&tmp, str);
605 			zend_hash_update(intern->parameter, string_key, &tmp);
606 		} ZEND_HASH_FOREACH_END();
607 		RETURN_TRUE;
608 	} else {
609 		if (!value) {
610 			zend_argument_value_error(3, "cannot be null when argument #2 ($name) is a string");
611 			RETURN_THROWS();
612 		}
613 
614 		if (UNEXPECTED(CHECK_NULL_PATH(ZSTR_VAL(name), ZSTR_LEN(name)))) {
615 			zend_argument_value_error(2, "must not contain any null bytes");
616 			RETURN_THROWS();
617 		}
618 
619 		ZVAL_STR_COPY(&new_string, value);
620 
621 		zend_hash_update(intern->parameter, name, &new_string);
622 		RETURN_TRUE;
623 	}
624 }
625 /* }}} end XSLTProcessor::setParameter */
626 
627 /* {{{ */
PHP_METHOD(XSLTProcessor,getParameter)628 PHP_METHOD(XSLTProcessor, getParameter)
629 {
630 	zval *id = ZEND_THIS;
631 	char *namespace;
632 	size_t namespace_len = 0;
633 	zval *value;
634 	zend_string *name;
635 	xsl_object *intern;
636 
637 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "sS", &namespace, &namespace_len, &name) == FAILURE) {
638 		RETURN_THROWS();
639 	}
640 	intern = Z_XSL_P(id);
641 	if ((value = zend_hash_find(intern->parameter, name)) != NULL) {
642 		RETURN_STR_COPY(Z_STR_P(value));
643 	} else {
644 		RETURN_FALSE;
645 	}
646 }
647 /* }}} end XSLTProcessor::getParameter */
648 
649 /* {{{ */
PHP_METHOD(XSLTProcessor,removeParameter)650 PHP_METHOD(XSLTProcessor, removeParameter)
651 {
652 	zval *id = ZEND_THIS;
653 	size_t namespace_len = 0;
654 	char *namespace;
655 	zend_string *name;
656 	xsl_object *intern;
657 
658 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "sS", &namespace, &namespace_len, &name) == FAILURE) {
659 		RETURN_THROWS();
660 	}
661 	intern = Z_XSL_P(id);
662 	if (zend_hash_del(intern->parameter, name) == SUCCESS) {
663 		RETURN_TRUE;
664 	} else {
665 		RETURN_FALSE;
666 	}
667 }
668 /* }}} end XSLTProcessor::removeParameter */
669 
670 /* {{{ */
PHP_METHOD(XSLTProcessor,registerPHPFunctions)671 PHP_METHOD(XSLTProcessor, registerPHPFunctions)
672 {
673 	xsl_object *intern = Z_XSL_P(ZEND_THIS);
674 
675 	zend_string *name = NULL;
676 	HashTable *callable_ht = NULL;
677 
678 	ZEND_PARSE_PARAMETERS_START(0, 1)
679 		Z_PARAM_OPTIONAL
680 		Z_PARAM_ARRAY_HT_OR_STR_OR_NULL(callable_ht, name)
681 	ZEND_PARSE_PARAMETERS_END();
682 
683 	php_dom_xpath_callbacks_update_method_handler(
684 		&intern->xpath_callbacks,
685 		NULL,
686 		NULL,
687 		name,
688 		callable_ht,
689 		PHP_DOM_XPATH_CALLBACK_NAME_VALIDATE_NULLS,
690 		NULL
691 	);
692 }
693 /* }}} end XSLTProcessor::registerPHPFunctions(); */
694 
PHP_METHOD(XSLTProcessor,registerPHPFunctionNS)695 PHP_METHOD(XSLTProcessor, registerPHPFunctionNS)
696 {
697 	xsl_object *intern = Z_XSL_P(ZEND_THIS);
698 
699 	zend_string *namespace, *name;
700 	zend_fcall_info fci;
701 	zend_fcall_info_cache fcc;
702 
703 	ZEND_PARSE_PARAMETERS_START(3, 3)
704 		Z_PARAM_PATH_STR(namespace)
705 		Z_PARAM_PATH_STR(name)
706 		Z_PARAM_FUNC_NO_TRAMPOLINE_FREE(fci, fcc)
707 	ZEND_PARSE_PARAMETERS_END();
708 
709 	if (zend_string_equals_literal(namespace, "http://php.net/xsl")) {
710 		zend_release_fcall_info_cache(&fcc);
711 		zend_argument_value_error(1, "must not be \"http://php.net/xsl\" because it is reserved by PHP");
712 		RETURN_THROWS();
713 	}
714 
715 	if (php_dom_xpath_callbacks_update_single_method_handler(
716 		&intern->xpath_callbacks,
717 		NULL,
718 		namespace,
719 		name,
720 		&fcc,
721 		PHP_DOM_XPATH_CALLBACK_NAME_VALIDATE_NCNAME,
722 		NULL
723 	) != SUCCESS) {
724 		zend_release_fcall_info_cache(&fcc);
725 	}
726 }
727 
728 /* {{{ */
PHP_METHOD(XSLTProcessor,setProfiling)729 PHP_METHOD(XSLTProcessor, setProfiling)
730 {
731 	zval *id = ZEND_THIS;
732 	xsl_object *intern;
733 	zend_string *filename = NULL;
734 
735 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "P!", &filename) == FAILURE) {
736 		RETURN_THROWS();
737 	}
738 
739 	intern = Z_XSL_P(id);
740 	if (intern->profiling) {
741 		zend_string_release(intern->profiling);
742 	}
743 	if (filename != NULL) {
744 		intern->profiling = zend_string_copy(filename);
745 	} else {
746 		intern->profiling = NULL;
747 	}
748 
749 	RETURN_TRUE;
750 }
751 /* }}} end XSLTProcessor::setProfiling */
752 
753 /* {{{ */
PHP_METHOD(XSLTProcessor,setSecurityPrefs)754 PHP_METHOD(XSLTProcessor, setSecurityPrefs)
755 {
756 	zval *id = ZEND_THIS;
757 	xsl_object *intern;
758 	zend_long securityPrefs, oldSecurityPrefs;
759 
760 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &securityPrefs) == FAILURE) {
761 		RETURN_THROWS();
762 	}
763 	intern = Z_XSL_P(id);
764 	oldSecurityPrefs = intern->securityPrefs;
765 	intern->securityPrefs = securityPrefs;
766 	RETURN_LONG(oldSecurityPrefs);
767 }
768 /* }}} end XSLTProcessor::setSecurityPrefs */
769 
770 /* {{{ */
PHP_METHOD(XSLTProcessor,getSecurityPrefs)771 PHP_METHOD(XSLTProcessor, getSecurityPrefs)
772 {
773 	zval *id = ZEND_THIS;
774 	xsl_object *intern;
775 
776 	if (zend_parse_parameters_none() == FAILURE) {
777 		RETURN_THROWS();
778 	}
779 
780 	intern = Z_XSL_P(id);
781 
782 	RETURN_LONG(intern->securityPrefs);
783 }
784 /* }}} end XSLTProcessor::getSecurityPrefs */
785 
786 /* {{{ */
PHP_METHOD(XSLTProcessor,hasExsltSupport)787 PHP_METHOD(XSLTProcessor, hasExsltSupport)
788 {
789 	if (zend_parse_parameters_none() == FAILURE) {
790 		RETURN_THROWS();
791 	}
792 
793 #ifdef HAVE_XSL_EXSLT
794 	RETURN_TRUE;
795 #else
796 	RETURN_FALSE;
797 #endif
798 }
799 /* }}} end XSLTProcessor::hasExsltSupport(); */
800