xref: /PHP-8.1/ext/xsl/xsltprocessor.c (revision 20c9c4a3)
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 "ext/libxml/php_libxml.h"
25 
26 /* {{{ php_xsl_xslt_string_to_xpathexpr()
27    Translates a string to a XPath Expression */
php_xsl_xslt_string_to_xpathexpr(const char * str)28 static char *php_xsl_xslt_string_to_xpathexpr(const char *str)
29 {
30 	const xmlChar *string = (const xmlChar *)str;
31 
32 	xmlChar *value;
33 	int str_len;
34 
35 	str_len = xmlStrlen(string) + 3;
36 
37 	if (xmlStrchr(string, '"')) {
38 		if (xmlStrchr(string, '\'')) {
39 			php_error_docref(NULL, E_WARNING, "Cannot create XPath expression (string contains both quote and double-quotes)");
40 			return NULL;
41 		}
42 		value = (xmlChar*) safe_emalloc (str_len, sizeof(xmlChar), 0);
43 		snprintf((char*)value, str_len, "'%s'", string);
44 	} else {
45 		value = (xmlChar*) safe_emalloc (str_len, sizeof(xmlChar), 0);
46 		snprintf((char *)value, str_len, "\"%s\"", string);
47 	}
48 	return (char *) value;
49 }
50 /* }}} */
51 
52 /* {{{ php_xsl_xslt_make_params()
53    Translates a PHP array to a libxslt parameters array */
php_xsl_xslt_make_params(HashTable * parht,int xpath_params)54 static char **php_xsl_xslt_make_params(HashTable *parht, int xpath_params)
55 {
56 
57 	int parsize;
58 	zval *value;
59 	char *xpath_expr;
60 	zend_string *string_key;
61 	char **params = NULL;
62 	int i = 0;
63 
64 	parsize = (2 * zend_hash_num_elements(parht) + 1) * sizeof(char *);
65 	params = (char **)safe_emalloc((2 * zend_hash_num_elements(parht) + 1), sizeof(char *), 0);
66 	memset((char *)params, 0, parsize);
67 
68 	ZEND_HASH_FOREACH_STR_KEY_VAL(parht, string_key, value) {
69 		ZEND_ASSERT(string_key != NULL);
70 		if (Z_TYPE_P(value) != IS_STRING) {
71 			if (!try_convert_to_string(value)) {
72 				efree(params);
73 				return NULL;
74 			}
75 		}
76 
77 		if (!xpath_params) {
78 			xpath_expr = php_xsl_xslt_string_to_xpathexpr(Z_STRVAL_P(value));
79 		} else {
80 			xpath_expr = estrndup(Z_STRVAL_P(value), Z_STRLEN_P(value));
81 		}
82 		if (xpath_expr) {
83 			params[i++] = estrndup(ZSTR_VAL(string_key), ZSTR_LEN(string_key));
84 			params[i++] = xpath_expr;
85 		}
86 	} ZEND_HASH_FOREACH_END();
87 
88 	params[i++] = NULL;
89 
90 	return params;
91 }
92 /* }}} */
93 
xsl_ext_function_php(xmlXPathParserContextPtr ctxt,int nargs,int type)94 static void xsl_ext_function_php(xmlXPathParserContextPtr ctxt, int nargs, int type) /* {{{ */
95 {
96 	xsltTransformContextPtr tctxt;
97 	zval *args = NULL;
98 	zval retval;
99 	int result, i;
100 	int error = 0;
101 	zend_fcall_info fci;
102 	zval handler;
103 	xmlXPathObjectPtr obj;
104 	char *str;
105 	xsl_object *intern;
106 	zend_string *callable = NULL;
107 
108 
109 	if (! zend_is_executing()) {
110 		xsltGenericError(xsltGenericErrorContext,
111 		"xsltExtFunctionTest: Function called from outside of PHP\n");
112 		error = 1;
113 	} else {
114 		tctxt = xsltXPathGetTransformContext(ctxt);
115 		if (tctxt == NULL) {
116 			xsltGenericError(xsltGenericErrorContext,
117 			"xsltExtFunctionTest: failed to get the transformation context\n");
118 			error = 1;
119 		} else {
120 			intern = (xsl_object*)tctxt->_private;
121 			if (intern == NULL) {
122 				xsltGenericError(xsltGenericErrorContext,
123 				"xsltExtFunctionTest: failed to get the internal object\n");
124 				error = 1;
125 			}
126 			else if (intern->registerPhpFunctions == 0) {
127 				xsltGenericError(xsltGenericErrorContext,
128 				"xsltExtFunctionTest: PHP Object did not register PHP functions\n");
129 				error = 1;
130 			}
131 		}
132 	}
133 
134 	if (error == 1) {
135 		for (i = nargs - 1; i >= 0; i--) {
136 			obj = valuePop(ctxt);
137 			if (obj) {
138 				xmlXPathFreeObject(obj);
139 			}
140 		}
141 		return;
142 	}
143 
144 	if (UNEXPECTED(nargs == 0)) {
145 		zend_throw_error(NULL, "Function name must be passed as the first argument");
146 		return;
147 	}
148 
149 	fci.param_count = nargs - 1;
150 	if (fci.param_count > 0) {
151 		args = safe_emalloc(fci.param_count, sizeof(zval), 0);
152 	}
153 	/* Reverse order to pop values off ctxt stack */
154 	for (i = fci.param_count - 1; i >= 0; i--) {
155 		obj = valuePop(ctxt);
156 		if (obj == NULL) {
157 			ZVAL_NULL(&args[i]);
158 			continue;
159 		}
160 		switch (obj->type) {
161 			case XPATH_STRING:
162 				ZVAL_STRING(&args[i], (char *)obj->stringval);
163 				break;
164 			case XPATH_BOOLEAN:
165 				ZVAL_BOOL(&args[i],  obj->boolval);
166 				break;
167 			case XPATH_NUMBER:
168 				ZVAL_DOUBLE(&args[i], obj->floatval);
169 				break;
170 			case XPATH_NODESET:
171 				if (type == 1) {
172 					str = (char*)xmlXPathCastToString(obj);
173 					ZVAL_STRING(&args[i], str);
174 					xmlFree(str);
175 				} else if (type == 2) {
176 					int j;
177 					dom_object *domintern = (dom_object *)intern->doc;
178 					if (obj->nodesetval && obj->nodesetval->nodeNr > 0) {
179 						array_init(&args[i]);
180 						for (j = 0; j < obj->nodesetval->nodeNr; j++) {
181 							xmlNodePtr node = obj->nodesetval->nodeTab[j];
182 							zval child;
183 							/* not sure, if we need this... it's copied from xpath.c */
184 							if (node->type == XML_NAMESPACE_DECL) {
185 								xmlNsPtr curns;
186 								xmlNodePtr nsparent;
187 
188 								nsparent = node->_private;
189 								curns = xmlNewNs(NULL, node->name, NULL);
190 								if (node->children) {
191 									curns->prefix = xmlStrdup((xmlChar *)node->children);
192 								}
193 								if (node->children) {
194 									node = xmlNewDocNode(node->doc, NULL, (xmlChar *) node->children, node->name);
195 								} else {
196 									node = xmlNewDocNode(node->doc, NULL, (const xmlChar *) "xmlns", node->name);
197 								}
198 								node->type = XML_NAMESPACE_DECL;
199 								node->parent = nsparent;
200 								node->ns = curns;
201 							} else {
202 								node = xmlDocCopyNode(node, domintern->document->ptr, 1);
203 							}
204 
205 							php_dom_create_object(node, &child, domintern);
206 							add_next_index_zval(&args[i], &child);
207 						}
208 					} else {
209 						ZVAL_EMPTY_ARRAY(&args[i]);
210 					}
211 				}
212 				break;
213 			default:
214 				str = (char *) xmlXPathCastToString(obj);
215 				ZVAL_STRING(&args[i], str);
216 				xmlFree(str);
217 		}
218 		xmlXPathFreeObject(obj);
219 	}
220 
221 	fci.size = sizeof(fci);
222 	fci.named_params = NULL;
223 	if (fci.param_count > 0) {
224 		fci.params = args;
225 	} else {
226 		fci.params = NULL;
227 	}
228 
229 	/* Last element of the stack is the function name */
230 	obj = valuePop(ctxt);
231 	if (obj == NULL || obj->stringval == NULL) {
232 		php_error_docref(NULL, E_WARNING, "Handler name must be a string");
233 		xmlXPathFreeObject(obj);
234 		valuePush(ctxt, xmlXPathNewString((const xmlChar *) ""));
235 		if (fci.param_count > 0) {
236 			for (i = 0; i < nargs - 1; i++) {
237 				zval_ptr_dtor(&args[i]);
238 			}
239 			efree(args);
240 		}
241 		return;
242 	}
243 	ZVAL_STRING(&handler, (char *) obj->stringval);
244 	xmlXPathFreeObject(obj);
245 
246 	ZVAL_COPY_VALUE(&fci.function_name, &handler);
247 	fci.object = NULL;
248 	fci.retval = &retval;
249 	if (!zend_make_callable(&handler, &callable)) {
250 		if (!EG(exception)) {
251 			php_error_docref(NULL, E_WARNING, "Unable to call handler %s()", ZSTR_VAL(callable));
252 		}
253 		valuePush(ctxt, xmlXPathNewString((const xmlChar *) ""));
254 	} else if ( intern->registerPhpFunctions == 2 && zend_hash_exists(intern->registered_phpfunctions, callable) == 0) {
255 		php_error_docref(NULL, E_WARNING, "Not allowed to call handler '%s()'", ZSTR_VAL(callable));
256 		/* Push an empty string, so that we at least have an xslt result... */
257 		valuePush(ctxt, xmlXPathNewString((const xmlChar *) ""));
258 	} else {
259 		result = zend_call_function(&fci, NULL);
260 		if (result == FAILURE) {
261 			if (Z_TYPE(handler) == IS_STRING) {
262 				php_error_docref(NULL, E_WARNING, "Unable to call handler %s()", Z_STRVAL(handler));
263 				valuePush(ctxt, xmlXPathNewString((const xmlChar *) ""));
264 			}
265 		/* retval is == NULL, when an exception occurred, don't report anything, because PHP itself will handle that */
266 		} else if (Z_ISUNDEF(retval)) {
267 		} else {
268 			if (Z_TYPE(retval) == IS_OBJECT && instanceof_function(Z_OBJCE(retval), dom_node_class_entry)) {
269 				xmlNode *nodep;
270 				dom_object *obj;
271 				if (intern->node_list == NULL) {
272 					intern->node_list = zend_new_array(0);
273 				}
274 				Z_ADDREF(retval);
275 				zend_hash_next_index_insert(intern->node_list, &retval);
276 				obj = Z_DOMOBJ_P(&retval);
277 				nodep = dom_object_get_node(obj);
278 				valuePush(ctxt, xmlXPathNewNodeSet(nodep));
279 			} else if (Z_TYPE(retval) == IS_TRUE || Z_TYPE(retval) == IS_FALSE) {
280 				valuePush(ctxt, xmlXPathNewBoolean(Z_TYPE(retval) == IS_TRUE));
281 			} else if (Z_TYPE(retval) == IS_OBJECT) {
282 				php_error_docref(NULL, E_WARNING, "A PHP Object cannot be converted to a XPath-string");
283 				valuePush(ctxt, xmlXPathNewString((const xmlChar *) ""));
284 			} else {
285 				convert_to_string(&retval);
286 				valuePush(ctxt, xmlXPathNewString((xmlChar *) Z_STRVAL(retval)));
287 			}
288 			zval_ptr_dtor(&retval);
289 		}
290 	}
291 	zend_string_release_ex(callable, 0);
292 	zval_ptr_dtor(&handler);
293 	if (fci.param_count > 0) {
294 		for (i = 0; i < nargs - 1; i++) {
295 			zval_ptr_dtor(&args[i]);
296 		}
297 		efree(args);
298 	}
299 }
300 /* }}} */
301 
xsl_ext_function_string_php(xmlXPathParserContextPtr ctxt,int nargs)302 void xsl_ext_function_string_php(xmlXPathParserContextPtr ctxt, int nargs) /* {{{ */
303 {
304 	xsl_ext_function_php(ctxt, nargs, 1);
305 }
306 /* }}} */
307 
xsl_ext_function_object_php(xmlXPathParserContextPtr ctxt,int nargs)308 void xsl_ext_function_object_php(xmlXPathParserContextPtr ctxt, int nargs) /* {{{ */
309 {
310 	xsl_ext_function_php(ctxt, nargs, 2);
311 }
312 /* }}} */
313 
314 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#
315 Since:
316 */
PHP_METHOD(XSLTProcessor,importStylesheet)317 PHP_METHOD(XSLTProcessor, importStylesheet)
318 {
319 	zval *id, *docp = NULL;
320 	xmlDoc *doc = NULL, *newdoc = NULL;
321 	xsltStylesheetPtr sheetp, oldsheetp;
322 	xsl_object *intern;
323 	int clone_docu = 0;
324 	xmlNode *nodep = NULL;
325 	zval *cloneDocu, rv;
326 	zend_string *member;
327 
328 	id = ZEND_THIS;
329 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "o", &docp) == FAILURE) {
330 		RETURN_THROWS();
331 	}
332 
333 	nodep = php_libxml_import_node(docp);
334 
335 	if (nodep) {
336 		doc = nodep->doc;
337 	}
338 	if (doc == NULL) {
339 		zend_argument_value_error(1, "must be a valid XML node");
340 		RETURN_THROWS();
341 	}
342 
343 	/* libxslt uses _private, so we must copy the imported
344 	stylesheet document otherwise the node proxies will be a mess */
345 	newdoc = xmlCopyDoc(doc, 1);
346 	xmlNodeSetBase((xmlNodePtr) newdoc, (xmlChar *)doc->URL);
347 	PHP_LIBXML_SANITIZE_GLOBALS(parse);
348 	xmlSubstituteEntitiesDefault(1);
349 	xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS;
350 
351 	sheetp = xsltParseStylesheetDoc(newdoc);
352 	PHP_LIBXML_RESTORE_GLOBALS(parse);
353 
354 	if (!sheetp) {
355 		xmlFreeDoc(newdoc);
356 		RETURN_FALSE;
357 	}
358 
359 	intern = Z_XSL_P(id);
360 
361 	member = zend_string_init("cloneDocument", sizeof("cloneDocument")-1, 0);
362 	cloneDocu = zend_std_read_property(Z_OBJ_P(id), member, BP_VAR_IS, NULL, &rv);
363 	if (Z_TYPE_P(cloneDocu) != IS_NULL) {
364 		convert_to_long(cloneDocu);
365 		clone_docu = Z_LVAL_P(cloneDocu);
366 	}
367 	zend_string_release_ex(member, 0);
368 	if (clone_docu == 0) {
369 		/* check if the stylesheet is using xsl:key, if yes, we have to clone the document _always_ before a transformation */
370 		nodep = xmlDocGetRootElement(sheetp->doc);
371 		if (nodep && (nodep = nodep->children)) {
372 			while (nodep) {
373 				if (nodep->type == XML_ELEMENT_NODE && xmlStrEqual(nodep->name, (const xmlChar *) "key") && xmlStrEqual(nodep->ns->href, XSLT_NAMESPACE)) {
374 					intern->hasKeys = 1;
375 					break;
376 				}
377 				nodep = nodep->next;
378 			}
379 		}
380 	} else {
381 		intern->hasKeys = clone_docu;
382 	}
383 
384 	if ((oldsheetp = (xsltStylesheetPtr)intern->ptr)) {
385 		/* free wrapper */
386 		if (((xsltStylesheetPtr) intern->ptr)->_private != NULL) {
387 			((xsltStylesheetPtr) intern->ptr)->_private = NULL;
388 		}
389 		xsltFreeStylesheet((xsltStylesheetPtr) intern->ptr);
390 		intern->ptr = NULL;
391 	}
392 
393 	php_xsl_set_object(id, sheetp);
394 	RETVAL_TRUE;
395 }
396 /* }}} end XSLTProcessor::importStylesheet */
397 
php_xsl_apply_stylesheet(zval * id,xsl_object * intern,xsltStylesheetPtr style,zval * docp)398 static xmlDocPtr php_xsl_apply_stylesheet(zval *id, xsl_object *intern, xsltStylesheetPtr style, zval *docp) /* {{{ */
399 {
400 	xmlDocPtr newdocp = NULL;
401 	xmlDocPtr doc = NULL;
402 	xmlNodePtr node = NULL;
403 	xsltTransformContextPtr ctxt;
404 	php_libxml_node_object *object;
405 	char **params = NULL;
406 	int clone;
407 	zval *doXInclude, rv;
408 	zend_string *member;
409 	FILE *f;
410 	int secPrefsError = 0;
411 	int secPrefsValue;
412 	xsltSecurityPrefsPtr secPrefs = NULL;
413 
414 	node = php_libxml_import_node(docp);
415 
416 	if (node) {
417 		doc = node->doc;
418 	}
419 
420 	if (doc == NULL) {
421 		zend_argument_value_error(1, "must be a valid XML node");
422 		return NULL;
423 	}
424 
425 	if (style == NULL) {
426 		zend_string *name = get_active_function_or_method_name();
427 		zend_throw_error(NULL, "%s() can only be called after a stylesheet has been imported",
428 			ZSTR_VAL(name));
429 		zend_string_release(name);
430 		return NULL;
431 	}
432 
433 	if (intern->profiling) {
434 		if (php_check_open_basedir(intern->profiling)) {
435 			f = NULL;
436 		} else {
437 			f = VCWD_FOPEN(intern->profiling, "w");
438 		}
439 	} else {
440 		f = NULL;
441 	}
442 
443 	if (intern->parameter) {
444 		params = php_xsl_xslt_make_params(intern->parameter, 0);
445 	}
446 
447 	intern->doc = emalloc(sizeof(php_libxml_node_object));
448 	memset(intern->doc, 0, sizeof(php_libxml_node_object));
449 
450 	if (intern->hasKeys == 1) {
451 		doc = xmlCopyDoc(doc, 1);
452 	} else {
453 		object = Z_LIBXML_NODE_P(docp);
454 		intern->doc->document = object->document;
455 	}
456 
457 	php_libxml_increment_doc_ref(intern->doc, doc);
458 
459 	ctxt = xsltNewTransformContext(style, doc);
460 	ctxt->_private = (void *) intern;
461 
462 	member = zend_string_init("doXInclude", sizeof("doXInclude")-1, 0);
463 	doXInclude = zend_std_read_property(Z_OBJ_P(id), member, BP_VAR_IS, NULL, &rv);
464 	if (Z_TYPE_P(doXInclude) != IS_NULL) {
465 		convert_to_long(doXInclude);
466 		ctxt->xinclude = Z_LVAL_P(doXInclude);
467 	}
468 	zend_string_release_ex(member, 0);
469 
470 	secPrefsValue = intern->securityPrefs;
471 
472 	/* if securityPrefs is set to NONE, we don't have to do any checks, but otherwise... */
473 	if (secPrefsValue != XSL_SECPREF_NONE) {
474 		secPrefs = xsltNewSecurityPrefs();
475 		if (secPrefsValue & XSL_SECPREF_READ_FILE ) {
476 			if (0 != xsltSetSecurityPrefs(secPrefs, XSLT_SECPREF_READ_FILE, xsltSecurityForbid)) {
477 				secPrefsError = 1;
478 			}
479 		}
480 		if (secPrefsValue & XSL_SECPREF_WRITE_FILE ) {
481 			if (0 != xsltSetSecurityPrefs(secPrefs, XSLT_SECPREF_WRITE_FILE, xsltSecurityForbid)) {
482 				secPrefsError = 1;
483 			}
484 		}
485 		if (secPrefsValue & XSL_SECPREF_CREATE_DIRECTORY ) {
486 			if (0 != xsltSetSecurityPrefs(secPrefs, XSLT_SECPREF_CREATE_DIRECTORY, xsltSecurityForbid)) {
487 				secPrefsError = 1;
488 			}
489 		}
490 		if (secPrefsValue & XSL_SECPREF_READ_NETWORK) {
491 			if (0 != xsltSetSecurityPrefs(secPrefs, XSLT_SECPREF_READ_NETWORK, xsltSecurityForbid)) {
492 				secPrefsError = 1;
493 			}
494 		}
495 		if (secPrefsValue & XSL_SECPREF_WRITE_NETWORK) {
496 			if (0 != xsltSetSecurityPrefs(secPrefs, XSLT_SECPREF_WRITE_NETWORK, xsltSecurityForbid)) {
497 				secPrefsError = 1;
498 			}
499 		}
500 
501 		if (0 != xsltSetCtxtSecurityPrefs(secPrefs, ctxt)) {
502 			secPrefsError = 1;
503 		}
504 	}
505 
506 	if (secPrefsError == 1) {
507 		php_error_docref(NULL, E_WARNING, "Can't set libxslt security properties, not doing transformation for security reasons");
508 	} else {
509 		newdocp = xsltApplyStylesheetUser(style, doc, (const char**) params,  NULL, f, ctxt);
510 	}
511 	if (f) {
512 		fclose(f);
513 	}
514 
515 	xsltFreeTransformContext(ctxt);
516 	if (secPrefs) {
517 		xsltFreeSecurityPrefs(secPrefs);
518 	}
519 
520 	if (intern->node_list != NULL) {
521 		zend_hash_destroy(intern->node_list);
522 		FREE_HASHTABLE(intern->node_list);
523 		intern->node_list = NULL;
524 	}
525 
526 	php_libxml_decrement_doc_ref(intern->doc);
527 	efree(intern->doc);
528 	intern->doc = NULL;
529 
530 	if (params) {
531 		clone = 0;
532 		while(params[clone]) {
533 			efree(params[clone++]);
534 		}
535 		efree(params);
536 	}
537 
538 	return newdocp;
539 
540 }
541 /* }}} */
542 
543 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#
544 Since:
545 */
PHP_METHOD(XSLTProcessor,transformToDoc)546 PHP_METHOD(XSLTProcessor, transformToDoc)
547 {
548 	zval *id, *docp = NULL;
549 	xmlDoc *newdocp;
550 	xsltStylesheetPtr sheetp;
551 	zend_string *ret_class = NULL;
552 	xsl_object *intern;
553 
554 	id = ZEND_THIS;
555 	intern = Z_XSL_P(id);
556 	sheetp = (xsltStylesheetPtr) intern->ptr;
557 
558 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "o|S!", &docp, &ret_class) == FAILURE) {
559 		RETURN_THROWS();
560 	}
561 
562 	newdocp = php_xsl_apply_stylesheet(id, intern, sheetp, docp);
563 
564 	if (newdocp) {
565 		if (ret_class) {
566 			zend_string *curclass_name;
567 			zend_class_entry *curce, *ce;
568 			php_libxml_node_object *interndoc;
569 
570 			curce = Z_OBJCE_P(docp);
571 			curclass_name = curce->name;
572 			while (curce->parent != NULL) {
573 				curce = curce->parent;
574 			}
575 
576 			ce = zend_lookup_class(ret_class);
577 			if (ce == NULL || !instanceof_function(ce, curce)) {
578 				xmlFreeDoc(newdocp);
579 				zend_argument_type_error(2, "must be a class name compatible with %s, \"%s\" given",
580 					ZSTR_VAL(curclass_name), ZSTR_VAL(ret_class)
581 				);
582 				RETURN_THROWS();
583 			}
584 
585 			object_init_ex(return_value, ce);
586 
587 			interndoc = Z_LIBXML_NODE_P(return_value);
588 			php_libxml_increment_doc_ref(interndoc, newdocp);
589 			php_libxml_increment_node_ptr(interndoc, (xmlNodePtr)newdocp, (void *)interndoc);
590 		} else {
591 			php_dom_create_object((xmlNodePtr) newdocp, return_value, NULL);
592 		}
593 	} else {
594 		RETURN_FALSE;
595 	}
596 
597 }
598 /* }}} end XSLTProcessor::transformToDoc */
599 
600 /* {{{ */
PHP_METHOD(XSLTProcessor,transformToUri)601 PHP_METHOD(XSLTProcessor, transformToUri)
602 {
603 	zval *id, *docp = NULL;
604 	xmlDoc *newdocp;
605 	xsltStylesheetPtr sheetp;
606 	int ret;
607 	size_t uri_len;
608 	char *uri;
609 	xsl_object *intern;
610 
611 	id = ZEND_THIS;
612 	intern = Z_XSL_P(id);
613 	sheetp = (xsltStylesheetPtr) intern->ptr;
614 
615 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "op", &docp, &uri, &uri_len) == FAILURE) {
616 		RETURN_THROWS();
617 	}
618 
619 	newdocp = php_xsl_apply_stylesheet(id, intern, sheetp, docp);
620 
621 	ret = -1;
622 	if (newdocp) {
623 		ret = xsltSaveResultToFilename(uri, newdocp, sheetp, 0);
624 		xmlFreeDoc(newdocp);
625 	}
626 
627 	RETVAL_LONG(ret);
628 }
629 /* }}} end XSLTProcessor::transformToUri */
630 
631 /* {{{ */
PHP_METHOD(XSLTProcessor,transformToXml)632 PHP_METHOD(XSLTProcessor, transformToXml)
633 {
634 	zval *id, *docp = NULL;
635 	xmlDoc *newdocp;
636 	xsltStylesheetPtr sheetp;
637 	int ret;
638 	xmlChar *doc_txt_ptr;
639 	int doc_txt_len;
640 	xsl_object *intern;
641 
642 	id = ZEND_THIS;
643 	intern = Z_XSL_P(id);
644 	sheetp = (xsltStylesheetPtr) intern->ptr;
645 
646 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "o", &docp) == FAILURE) {
647 		RETURN_THROWS();
648 	}
649 
650 	newdocp = php_xsl_apply_stylesheet(id, intern, sheetp, docp);
651 
652 	ret = -1;
653 	if (newdocp) {
654 		ret = xsltSaveResultToString(&doc_txt_ptr, &doc_txt_len, newdocp, sheetp);
655 		if (doc_txt_ptr && doc_txt_len) {
656 			RETVAL_STRINGL((char *) doc_txt_ptr, doc_txt_len);
657 			xmlFree(doc_txt_ptr);
658 		}
659 		xmlFreeDoc(newdocp);
660 	}
661 
662 	if (ret < 0) {
663 		RETURN_FALSE;
664 	}
665 }
666 /* }}} end XSLTProcessor::transformToXml */
667 
668 /* {{{ */
PHP_METHOD(XSLTProcessor,setParameter)669 PHP_METHOD(XSLTProcessor, setParameter)
670 {
671 
672 	zval *id = ZEND_THIS;
673 	zval *entry, new_string;
674 	HashTable *array_value;
675 	xsl_object *intern;
676 	char *namespace;
677 	size_t namespace_len;
678 	zend_string *string_key, *name, *value = NULL;
679 
680 	ZEND_PARSE_PARAMETERS_START(2, 3)
681 		Z_PARAM_STRING(namespace, namespace_len)
682 		Z_PARAM_ARRAY_HT_OR_STR(array_value, name)
683 		Z_PARAM_OPTIONAL
684 		Z_PARAM_STR_OR_NULL(value)
685 	ZEND_PARSE_PARAMETERS_END();
686 
687 	intern = Z_XSL_P(id);
688 
689 	if (array_value) {
690 		if (value) {
691 			zend_argument_value_error(3, "must be null when argument #2 ($name) is an array");
692 			RETURN_THROWS();
693 		}
694 
695 		ZEND_HASH_FOREACH_STR_KEY_VAL(array_value, string_key, entry) {
696 			zval tmp;
697 			zend_string *str;
698 
699 			if (string_key == NULL) {
700 				zend_argument_type_error(2, "must contain only string keys");
701 				RETURN_THROWS();
702 			}
703 			str = zval_try_get_string(entry);
704 			if (UNEXPECTED(!str)) {
705 				RETURN_THROWS();
706 			}
707 			ZVAL_STR(&tmp, str);
708 			zend_hash_update(intern->parameter, string_key, &tmp);
709 		} ZEND_HASH_FOREACH_END();
710 		RETURN_TRUE;
711 	} else {
712 		if (!value) {
713 			zend_argument_value_error(3, "cannot be null when argument #2 ($name) is a string");
714 			RETURN_THROWS();
715 		}
716 
717 		ZVAL_STR_COPY(&new_string, value);
718 
719 		zend_hash_update(intern->parameter, name, &new_string);
720 		RETURN_TRUE;
721 	}
722 }
723 /* }}} end XSLTProcessor::setParameter */
724 
725 /* {{{ */
PHP_METHOD(XSLTProcessor,getParameter)726 PHP_METHOD(XSLTProcessor, getParameter)
727 {
728 	zval *id = ZEND_THIS;
729 	char *namespace;
730 	size_t namespace_len = 0;
731 	zval *value;
732 	zend_string *name;
733 	xsl_object *intern;
734 
735 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "sS", &namespace, &namespace_len, &name) == FAILURE) {
736 		RETURN_THROWS();
737 	}
738 	intern = Z_XSL_P(id);
739 	if ((value = zend_hash_find(intern->parameter, name)) != NULL) {
740 		RETURN_STR(zval_get_string(value));
741 	} else {
742 		RETURN_FALSE;
743 	}
744 }
745 /* }}} end XSLTProcessor::getParameter */
746 
747 /* {{{ */
PHP_METHOD(XSLTProcessor,removeParameter)748 PHP_METHOD(XSLTProcessor, removeParameter)
749 {
750 	zval *id = ZEND_THIS;
751 	size_t namespace_len = 0;
752 	char *namespace;
753 	zend_string *name;
754 	xsl_object *intern;
755 
756 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "sS", &namespace, &namespace_len, &name) == FAILURE) {
757 		RETURN_THROWS();
758 	}
759 	intern = Z_XSL_P(id);
760 	if (zend_hash_del(intern->parameter, name) == SUCCESS) {
761 		RETURN_TRUE;
762 	} else {
763 		RETURN_FALSE;
764 	}
765 }
766 /* }}} end XSLTProcessor::removeParameter */
767 
768 /* {{{ */
PHP_METHOD(XSLTProcessor,registerPHPFunctions)769 PHP_METHOD(XSLTProcessor, registerPHPFunctions)
770 {
771 	zval *id = ZEND_THIS;
772 	xsl_object *intern;
773 	zval *entry, new_string;
774 	zend_string *restrict_str = NULL;
775 	HashTable *restrict_ht = NULL;
776 
777 	ZEND_PARSE_PARAMETERS_START(0, 1)
778 		Z_PARAM_OPTIONAL
779 		Z_PARAM_ARRAY_HT_OR_STR_OR_NULL(restrict_ht, restrict_str)
780 	ZEND_PARSE_PARAMETERS_END();
781 
782 	intern = Z_XSL_P(id);
783 
784 	if (restrict_ht) {
785 		ZEND_HASH_FOREACH_VAL(restrict_ht, entry) {
786 			zend_string *str = zval_try_get_string(entry);
787 			if (UNEXPECTED(!str)) {
788 				return;
789 			}
790 			ZVAL_LONG(&new_string, 1);
791 			zend_hash_update(intern->registered_phpfunctions, str, &new_string);
792 			zend_string_release(str);
793 		} ZEND_HASH_FOREACH_END();
794 
795 		intern->registerPhpFunctions = 2;
796 	} else if (restrict_str) {
797 		ZVAL_LONG(&new_string, 1);
798 		zend_hash_update(intern->registered_phpfunctions, restrict_str, &new_string);
799 		intern->registerPhpFunctions = 2;
800 	} else {
801 		intern->registerPhpFunctions = 1;
802 	}
803 }
804 /* }}} end XSLTProcessor::registerPHPFunctions(); */
805 
806 /* {{{ */
PHP_METHOD(XSLTProcessor,setProfiling)807 PHP_METHOD(XSLTProcessor, setProfiling)
808 {
809 	zval *id = ZEND_THIS;
810 	xsl_object *intern;
811 	char *filename = NULL;
812 	size_t filename_len;
813 
814 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "p!", &filename, &filename_len) == FAILURE) {
815 		RETURN_THROWS();
816 	}
817 
818 	intern = Z_XSL_P(id);
819 	if (intern->profiling) {
820 		efree(intern->profiling);
821 	}
822 	if (filename != NULL) {
823 		intern->profiling = estrndup(filename, filename_len);
824 	} else {
825 		intern->profiling = NULL;
826 	}
827 
828 	RETURN_TRUE;
829 }
830 /* }}} end XSLTProcessor::setProfiling */
831 
832 /* {{{ */
PHP_METHOD(XSLTProcessor,setSecurityPrefs)833 PHP_METHOD(XSLTProcessor, setSecurityPrefs)
834 {
835 	zval *id = ZEND_THIS;
836 	xsl_object *intern;
837 	zend_long securityPrefs, oldSecurityPrefs;
838 
839 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &securityPrefs) == FAILURE) {
840 		RETURN_THROWS();
841 	}
842 	intern = Z_XSL_P(id);
843 	oldSecurityPrefs = intern->securityPrefs;
844 	intern->securityPrefs = securityPrefs;
845 	/* set this to 1 so that we know, it was set through this method. Can be removed, when we remove the ini setting */
846 	intern->securityPrefsSet = 1;
847 	RETURN_LONG(oldSecurityPrefs);
848 }
849 /* }}} end XSLTProcessor::setSecurityPrefs */
850 
851 /* {{{ */
PHP_METHOD(XSLTProcessor,getSecurityPrefs)852 PHP_METHOD(XSLTProcessor, getSecurityPrefs)
853 {
854 	zval *id = ZEND_THIS;
855 	xsl_object *intern;
856 
857 	if (zend_parse_parameters_none() == FAILURE) {
858 		RETURN_THROWS();
859 	}
860 
861 	intern = Z_XSL_P(id);
862 
863 	RETURN_LONG(intern->securityPrefs);
864 }
865 /* }}} end XSLTProcessor::getSecurityPrefs */
866 
867 /* {{{ */
PHP_METHOD(XSLTProcessor,hasExsltSupport)868 PHP_METHOD(XSLTProcessor, hasExsltSupport)
869 {
870 	if (zend_parse_parameters_none() == FAILURE) {
871 		RETURN_THROWS();
872 	}
873 
874 #ifdef HAVE_XSL_EXSLT
875 	RETURN_TRUE;
876 #else
877 	RETURN_FALSE;
878 #endif
879 }
880 /* }}} end XSLTProcessor::hasExsltSupport(); */
881