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