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