xref: /PHP-8.1/ext/xsl/xsltprocessor.c (revision dbde99d8)
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 	PHP_LIBXML_IGNORE_DEPRECATIONS_START
349 	xmlSubstituteEntitiesDefault(1);
350 	xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS;
351 	PHP_LIBXML_IGNORE_DEPRECATIONS_END
352 
353 	sheetp = xsltParseStylesheetDoc(newdoc);
354 	PHP_LIBXML_RESTORE_GLOBALS(parse);
355 
356 	if (!sheetp) {
357 		xmlFreeDoc(newdoc);
358 		RETURN_FALSE;
359 	}
360 
361 	intern = Z_XSL_P(id);
362 
363 	member = zend_string_init("cloneDocument", sizeof("cloneDocument")-1, 0);
364 	cloneDocu = zend_std_read_property(Z_OBJ_P(id), member, BP_VAR_IS, NULL, &rv);
365 	if (Z_TYPE_P(cloneDocu) != IS_NULL) {
366 		convert_to_long(cloneDocu);
367 		clone_docu = Z_LVAL_P(cloneDocu);
368 	}
369 	zend_string_release_ex(member, 0);
370 	if (clone_docu == 0) {
371 		/* check if the stylesheet is using xsl:key, if yes, we have to clone the document _always_ before a transformation */
372 		nodep = xmlDocGetRootElement(sheetp->doc);
373 		if (nodep && (nodep = nodep->children)) {
374 			while (nodep) {
375 				if (nodep->type == XML_ELEMENT_NODE && xmlStrEqual(nodep->name, (const xmlChar *) "key") && xmlStrEqual(nodep->ns->href, XSLT_NAMESPACE)) {
376 					intern->hasKeys = 1;
377 					break;
378 				}
379 				nodep = nodep->next;
380 			}
381 		}
382 	} else {
383 		intern->hasKeys = clone_docu;
384 	}
385 
386 	if ((oldsheetp = (xsltStylesheetPtr)intern->ptr)) {
387 		/* free wrapper */
388 		if (((xsltStylesheetPtr) intern->ptr)->_private != NULL) {
389 			((xsltStylesheetPtr) intern->ptr)->_private = NULL;
390 		}
391 		xsltFreeStylesheet((xsltStylesheetPtr) intern->ptr);
392 		intern->ptr = NULL;
393 	}
394 
395 	php_xsl_set_object(id, sheetp);
396 	RETVAL_TRUE;
397 }
398 /* }}} end XSLTProcessor::importStylesheet */
399 
php_xsl_apply_stylesheet(zval * id,xsl_object * intern,xsltStylesheetPtr style,zval * docp)400 static xmlDocPtr php_xsl_apply_stylesheet(zval *id, xsl_object *intern, xsltStylesheetPtr style, zval *docp) /* {{{ */
401 {
402 	xmlDocPtr newdocp = NULL;
403 	xmlDocPtr doc = NULL;
404 	xmlNodePtr node = NULL;
405 	xsltTransformContextPtr ctxt;
406 	php_libxml_node_object *object;
407 	char **params = NULL;
408 	int clone;
409 	zval *doXInclude, rv;
410 	zend_string *member;
411 	FILE *f;
412 	int secPrefsError = 0;
413 	int secPrefsValue;
414 	xsltSecurityPrefsPtr secPrefs = NULL;
415 
416 	node = php_libxml_import_node(docp);
417 
418 	if (node) {
419 		doc = node->doc;
420 	}
421 
422 	if (doc == NULL) {
423 		zend_argument_value_error(1, "must be a valid XML node");
424 		return NULL;
425 	}
426 
427 	if (style == NULL) {
428 		zend_string *name = get_active_function_or_method_name();
429 		zend_throw_error(NULL, "%s() can only be called after a stylesheet has been imported",
430 			ZSTR_VAL(name));
431 		zend_string_release(name);
432 		return NULL;
433 	}
434 
435 	if (intern->profiling) {
436 		if (php_check_open_basedir(intern->profiling)) {
437 			f = NULL;
438 		} else {
439 			f = VCWD_FOPEN(intern->profiling, "w");
440 		}
441 	} else {
442 		f = NULL;
443 	}
444 
445 	if (intern->parameter) {
446 		params = php_xsl_xslt_make_params(intern->parameter, 0);
447 	}
448 
449 	intern->doc = emalloc(sizeof(php_libxml_node_object));
450 	memset(intern->doc, 0, sizeof(php_libxml_node_object));
451 
452 	if (intern->hasKeys == 1) {
453 		doc = xmlCopyDoc(doc, 1);
454 	} else {
455 		object = Z_LIBXML_NODE_P(docp);
456 		intern->doc->document = object->document;
457 	}
458 
459 	php_libxml_increment_doc_ref(intern->doc, doc);
460 
461 	ctxt = xsltNewTransformContext(style, doc);
462 	ctxt->_private = (void *) intern;
463 
464 	member = zend_string_init("doXInclude", sizeof("doXInclude")-1, 0);
465 	doXInclude = zend_std_read_property(Z_OBJ_P(id), member, BP_VAR_IS, NULL, &rv);
466 	if (Z_TYPE_P(doXInclude) != IS_NULL) {
467 		convert_to_long(doXInclude);
468 		ctxt->xinclude = Z_LVAL_P(doXInclude);
469 	}
470 	zend_string_release_ex(member, 0);
471 
472 	secPrefsValue = intern->securityPrefs;
473 
474 	/* if securityPrefs is set to NONE, we don't have to do any checks, but otherwise... */
475 	if (secPrefsValue != XSL_SECPREF_NONE) {
476 		secPrefs = xsltNewSecurityPrefs();
477 		if (secPrefsValue & XSL_SECPREF_READ_FILE ) {
478 			if (0 != xsltSetSecurityPrefs(secPrefs, XSLT_SECPREF_READ_FILE, xsltSecurityForbid)) {
479 				secPrefsError = 1;
480 			}
481 		}
482 		if (secPrefsValue & XSL_SECPREF_WRITE_FILE ) {
483 			if (0 != xsltSetSecurityPrefs(secPrefs, XSLT_SECPREF_WRITE_FILE, xsltSecurityForbid)) {
484 				secPrefsError = 1;
485 			}
486 		}
487 		if (secPrefsValue & XSL_SECPREF_CREATE_DIRECTORY ) {
488 			if (0 != xsltSetSecurityPrefs(secPrefs, XSLT_SECPREF_CREATE_DIRECTORY, xsltSecurityForbid)) {
489 				secPrefsError = 1;
490 			}
491 		}
492 		if (secPrefsValue & XSL_SECPREF_READ_NETWORK) {
493 			if (0 != xsltSetSecurityPrefs(secPrefs, XSLT_SECPREF_READ_NETWORK, xsltSecurityForbid)) {
494 				secPrefsError = 1;
495 			}
496 		}
497 		if (secPrefsValue & XSL_SECPREF_WRITE_NETWORK) {
498 			if (0 != xsltSetSecurityPrefs(secPrefs, XSLT_SECPREF_WRITE_NETWORK, xsltSecurityForbid)) {
499 				secPrefsError = 1;
500 			}
501 		}
502 
503 		if (0 != xsltSetCtxtSecurityPrefs(secPrefs, ctxt)) {
504 			secPrefsError = 1;
505 		}
506 	}
507 
508 	if (secPrefsError == 1) {
509 		php_error_docref(NULL, E_WARNING, "Can't set libxslt security properties, not doing transformation for security reasons");
510 	} else {
511 		newdocp = xsltApplyStylesheetUser(style, doc, (const char**) params,  NULL, f, ctxt);
512 	}
513 	if (f) {
514 		fclose(f);
515 	}
516 
517 	xsltFreeTransformContext(ctxt);
518 	if (secPrefs) {
519 		xsltFreeSecurityPrefs(secPrefs);
520 	}
521 
522 	if (intern->node_list != NULL) {
523 		zend_hash_destroy(intern->node_list);
524 		FREE_HASHTABLE(intern->node_list);
525 		intern->node_list = NULL;
526 	}
527 
528 	php_libxml_decrement_doc_ref(intern->doc);
529 	efree(intern->doc);
530 	intern->doc = NULL;
531 
532 	if (params) {
533 		clone = 0;
534 		while(params[clone]) {
535 			efree(params[clone++]);
536 		}
537 		efree(params);
538 	}
539 
540 	return newdocp;
541 
542 }
543 /* }}} */
544 
545 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#
546 Since:
547 */
PHP_METHOD(XSLTProcessor,transformToDoc)548 PHP_METHOD(XSLTProcessor, transformToDoc)
549 {
550 	zval *id, *docp = NULL;
551 	xmlDoc *newdocp;
552 	xsltStylesheetPtr sheetp;
553 	zend_string *ret_class = NULL;
554 	xsl_object *intern;
555 
556 	id = ZEND_THIS;
557 	intern = Z_XSL_P(id);
558 	sheetp = (xsltStylesheetPtr) intern->ptr;
559 
560 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "o|S!", &docp, &ret_class) == FAILURE) {
561 		RETURN_THROWS();
562 	}
563 
564 	newdocp = php_xsl_apply_stylesheet(id, intern, sheetp, docp);
565 
566 	if (newdocp) {
567 		if (ret_class) {
568 			zend_string *curclass_name;
569 			zend_class_entry *curce, *ce;
570 			php_libxml_node_object *interndoc;
571 
572 			curce = Z_OBJCE_P(docp);
573 			curclass_name = curce->name;
574 			while (curce->parent != NULL) {
575 				curce = curce->parent;
576 			}
577 
578 			ce = zend_lookup_class(ret_class);
579 			if (ce == NULL || !instanceof_function(ce, curce)) {
580 				xmlFreeDoc(newdocp);
581 				zend_argument_type_error(2, "must be a class name compatible with %s, \"%s\" given",
582 					ZSTR_VAL(curclass_name), ZSTR_VAL(ret_class)
583 				);
584 				RETURN_THROWS();
585 			}
586 
587 			object_init_ex(return_value, ce);
588 
589 			interndoc = Z_LIBXML_NODE_P(return_value);
590 			php_libxml_increment_doc_ref(interndoc, newdocp);
591 			php_libxml_increment_node_ptr(interndoc, (xmlNodePtr)newdocp, (void *)interndoc);
592 		} else {
593 			php_dom_create_object((xmlNodePtr) newdocp, return_value, NULL);
594 		}
595 	} else {
596 		RETURN_FALSE;
597 	}
598 
599 }
600 /* }}} end XSLTProcessor::transformToDoc */
601 
602 /* {{{ */
PHP_METHOD(XSLTProcessor,transformToUri)603 PHP_METHOD(XSLTProcessor, transformToUri)
604 {
605 	zval *id, *docp = NULL;
606 	xmlDoc *newdocp;
607 	xsltStylesheetPtr sheetp;
608 	int ret;
609 	size_t uri_len;
610 	char *uri;
611 	xsl_object *intern;
612 
613 	id = ZEND_THIS;
614 	intern = Z_XSL_P(id);
615 	sheetp = (xsltStylesheetPtr) intern->ptr;
616 
617 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "op", &docp, &uri, &uri_len) == FAILURE) {
618 		RETURN_THROWS();
619 	}
620 
621 	newdocp = php_xsl_apply_stylesheet(id, intern, sheetp, docp);
622 
623 	ret = -1;
624 	if (newdocp) {
625 		ret = xsltSaveResultToFilename(uri, newdocp, sheetp, 0);
626 		xmlFreeDoc(newdocp);
627 	}
628 
629 	RETVAL_LONG(ret);
630 }
631 /* }}} end XSLTProcessor::transformToUri */
632 
633 /* {{{ */
PHP_METHOD(XSLTProcessor,transformToXml)634 PHP_METHOD(XSLTProcessor, transformToXml)
635 {
636 	zval *id, *docp = NULL;
637 	xmlDoc *newdocp;
638 	xsltStylesheetPtr sheetp;
639 	int ret;
640 	xmlChar *doc_txt_ptr;
641 	int doc_txt_len;
642 	xsl_object *intern;
643 
644 	id = ZEND_THIS;
645 	intern = Z_XSL_P(id);
646 	sheetp = (xsltStylesheetPtr) intern->ptr;
647 
648 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "o", &docp) == FAILURE) {
649 		RETURN_THROWS();
650 	}
651 
652 	newdocp = php_xsl_apply_stylesheet(id, intern, sheetp, docp);
653 
654 	ret = -1;
655 	if (newdocp) {
656 		ret = xsltSaveResultToString(&doc_txt_ptr, &doc_txt_len, newdocp, sheetp);
657 		if (doc_txt_ptr && doc_txt_len) {
658 			RETVAL_STRINGL((char *) doc_txt_ptr, doc_txt_len);
659 			xmlFree(doc_txt_ptr);
660 		}
661 		xmlFreeDoc(newdocp);
662 	}
663 
664 	if (ret < 0) {
665 		RETURN_FALSE;
666 	}
667 }
668 /* }}} end XSLTProcessor::transformToXml */
669 
670 /* {{{ */
PHP_METHOD(XSLTProcessor,setParameter)671 PHP_METHOD(XSLTProcessor, setParameter)
672 {
673 
674 	zval *id = ZEND_THIS;
675 	zval *entry, new_string;
676 	HashTable *array_value;
677 	xsl_object *intern;
678 	char *namespace;
679 	size_t namespace_len;
680 	zend_string *string_key, *name, *value = NULL;
681 
682 	ZEND_PARSE_PARAMETERS_START(2, 3)
683 		Z_PARAM_STRING(namespace, namespace_len)
684 		Z_PARAM_ARRAY_HT_OR_STR(array_value, name)
685 		Z_PARAM_OPTIONAL
686 		Z_PARAM_STR_OR_NULL(value)
687 	ZEND_PARSE_PARAMETERS_END();
688 
689 	intern = Z_XSL_P(id);
690 
691 	if (array_value) {
692 		if (value) {
693 			zend_argument_value_error(3, "must be null when argument #2 ($name) is an array");
694 			RETURN_THROWS();
695 		}
696 
697 		ZEND_HASH_FOREACH_STR_KEY_VAL(array_value, string_key, entry) {
698 			zval tmp;
699 			zend_string *str;
700 
701 			if (string_key == NULL) {
702 				zend_argument_type_error(2, "must contain only string keys");
703 				RETURN_THROWS();
704 			}
705 			str = zval_try_get_string(entry);
706 			if (UNEXPECTED(!str)) {
707 				RETURN_THROWS();
708 			}
709 			ZVAL_STR(&tmp, str);
710 			zend_hash_update(intern->parameter, string_key, &tmp);
711 		} ZEND_HASH_FOREACH_END();
712 		RETURN_TRUE;
713 	} else {
714 		if (!value) {
715 			zend_argument_value_error(3, "cannot be null when argument #2 ($name) is a string");
716 			RETURN_THROWS();
717 		}
718 
719 		ZVAL_STR_COPY(&new_string, value);
720 
721 		zend_hash_update(intern->parameter, name, &new_string);
722 		RETURN_TRUE;
723 	}
724 }
725 /* }}} end XSLTProcessor::setParameter */
726 
727 /* {{{ */
PHP_METHOD(XSLTProcessor,getParameter)728 PHP_METHOD(XSLTProcessor, getParameter)
729 {
730 	zval *id = ZEND_THIS;
731 	char *namespace;
732 	size_t namespace_len = 0;
733 	zval *value;
734 	zend_string *name;
735 	xsl_object *intern;
736 
737 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "sS", &namespace, &namespace_len, &name) == FAILURE) {
738 		RETURN_THROWS();
739 	}
740 	intern = Z_XSL_P(id);
741 	if ((value = zend_hash_find(intern->parameter, name)) != NULL) {
742 		RETURN_STR(zval_get_string(value));
743 	} else {
744 		RETURN_FALSE;
745 	}
746 }
747 /* }}} end XSLTProcessor::getParameter */
748 
749 /* {{{ */
PHP_METHOD(XSLTProcessor,removeParameter)750 PHP_METHOD(XSLTProcessor, removeParameter)
751 {
752 	zval *id = ZEND_THIS;
753 	size_t namespace_len = 0;
754 	char *namespace;
755 	zend_string *name;
756 	xsl_object *intern;
757 
758 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "sS", &namespace, &namespace_len, &name) == FAILURE) {
759 		RETURN_THROWS();
760 	}
761 	intern = Z_XSL_P(id);
762 	if (zend_hash_del(intern->parameter, name) == SUCCESS) {
763 		RETURN_TRUE;
764 	} else {
765 		RETURN_FALSE;
766 	}
767 }
768 /* }}} end XSLTProcessor::removeParameter */
769 
770 /* {{{ */
PHP_METHOD(XSLTProcessor,registerPHPFunctions)771 PHP_METHOD(XSLTProcessor, registerPHPFunctions)
772 {
773 	zval *id = ZEND_THIS;
774 	xsl_object *intern;
775 	zval *entry, new_string;
776 	zend_string *restrict_str = NULL;
777 	HashTable *restrict_ht = NULL;
778 
779 	ZEND_PARSE_PARAMETERS_START(0, 1)
780 		Z_PARAM_OPTIONAL
781 		Z_PARAM_ARRAY_HT_OR_STR_OR_NULL(restrict_ht, restrict_str)
782 	ZEND_PARSE_PARAMETERS_END();
783 
784 	intern = Z_XSL_P(id);
785 
786 	if (restrict_ht) {
787 		ZEND_HASH_FOREACH_VAL(restrict_ht, entry) {
788 			zend_string *str = zval_try_get_string(entry);
789 			if (UNEXPECTED(!str)) {
790 				return;
791 			}
792 			ZVAL_LONG(&new_string, 1);
793 			zend_hash_update(intern->registered_phpfunctions, str, &new_string);
794 			zend_string_release(str);
795 		} ZEND_HASH_FOREACH_END();
796 
797 		intern->registerPhpFunctions = 2;
798 	} else if (restrict_str) {
799 		ZVAL_LONG(&new_string, 1);
800 		zend_hash_update(intern->registered_phpfunctions, restrict_str, &new_string);
801 		intern->registerPhpFunctions = 2;
802 	} else {
803 		intern->registerPhpFunctions = 1;
804 	}
805 }
806 /* }}} end XSLTProcessor::registerPHPFunctions(); */
807 
808 /* {{{ */
PHP_METHOD(XSLTProcessor,setProfiling)809 PHP_METHOD(XSLTProcessor, setProfiling)
810 {
811 	zval *id = ZEND_THIS;
812 	xsl_object *intern;
813 	char *filename = NULL;
814 	size_t filename_len;
815 
816 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "p!", &filename, &filename_len) == FAILURE) {
817 		RETURN_THROWS();
818 	}
819 
820 	intern = Z_XSL_P(id);
821 	if (intern->profiling) {
822 		efree(intern->profiling);
823 	}
824 	if (filename != NULL) {
825 		intern->profiling = estrndup(filename, filename_len);
826 	} else {
827 		intern->profiling = NULL;
828 	}
829 
830 	RETURN_TRUE;
831 }
832 /* }}} end XSLTProcessor::setProfiling */
833 
834 /* {{{ */
PHP_METHOD(XSLTProcessor,setSecurityPrefs)835 PHP_METHOD(XSLTProcessor, setSecurityPrefs)
836 {
837 	zval *id = ZEND_THIS;
838 	xsl_object *intern;
839 	zend_long securityPrefs, oldSecurityPrefs;
840 
841 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &securityPrefs) == FAILURE) {
842 		RETURN_THROWS();
843 	}
844 	intern = Z_XSL_P(id);
845 	oldSecurityPrefs = intern->securityPrefs;
846 	intern->securityPrefs = securityPrefs;
847 	/* set this to 1 so that we know, it was set through this method. Can be removed, when we remove the ini setting */
848 	intern->securityPrefsSet = 1;
849 	RETURN_LONG(oldSecurityPrefs);
850 }
851 /* }}} end XSLTProcessor::setSecurityPrefs */
852 
853 /* {{{ */
PHP_METHOD(XSLTProcessor,getSecurityPrefs)854 PHP_METHOD(XSLTProcessor, getSecurityPrefs)
855 {
856 	zval *id = ZEND_THIS;
857 	xsl_object *intern;
858 
859 	if (zend_parse_parameters_none() == FAILURE) {
860 		RETURN_THROWS();
861 	}
862 
863 	intern = Z_XSL_P(id);
864 
865 	RETURN_LONG(intern->securityPrefs);
866 }
867 /* }}} end XSLTProcessor::getSecurityPrefs */
868 
869 /* {{{ */
PHP_METHOD(XSLTProcessor,hasExsltSupport)870 PHP_METHOD(XSLTProcessor, hasExsltSupport)
871 {
872 	if (zend_parse_parameters_none() == FAILURE) {
873 		RETURN_THROWS();
874 	}
875 
876 #ifdef HAVE_XSL_EXSLT
877 	RETURN_TRUE;
878 #else
879 	RETURN_FALSE;
880 #endif
881 }
882 /* }}} end XSLTProcessor::hasExsltSupport(); */
883