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