1 /*
2 +----------------------------------------------------------------------+
3 | Copyright (c) The PHP Group |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.01 of the PHP license, |
6 | that is bundled with this package in the file LICENSE, and is |
7 | available through the world-wide-web at the following url: |
8 | https://www.php.net/license/3_01.txt |
9 | If you did not receive a copy of the PHP license and are unable to |
10 | obtain it through the world-wide-web, please send a note to |
11 | license@php.net so we can mail you a copy immediately. |
12 +----------------------------------------------------------------------+
13 | Authors: Christian Stocker <chregu@php.net> |
14 | Rob Richards <rrichards@php.net> |
15 +----------------------------------------------------------------------+
16 */
17
18 #ifdef HAVE_CONFIG_H
19 #include <config.h>
20 #endif
21
22 #include "php.h"
23 #include "php_xsl.h"
24 #include <libxslt/variables.h>
25 #include "ext/libxml/php_libxml.h"
26 #include "ext/dom/namespace_compat.h"
27
28
php_xsl_xslt_apply_params(xsltTransformContextPtr ctxt,HashTable * params)29 static zend_result php_xsl_xslt_apply_params(xsltTransformContextPtr ctxt, HashTable *params)
30 {
31 zend_string *string_key;
32 zval *value;
33
34 ZEND_HASH_MAP_FOREACH_STR_KEY_VAL(params, string_key, value) {
35 ZEND_ASSERT(string_key != NULL);
36 /* Already a string because of setParameter() */
37 ZEND_ASSERT(Z_TYPE_P(value) == IS_STRING);
38
39 int result = xsltQuoteOneUserParam(ctxt, (const xmlChar *) ZSTR_VAL(string_key), (const xmlChar *) Z_STRVAL_P(value));
40 if (result < 0) {
41 php_error_docref(NULL, E_WARNING, "Could not apply parameter \"%s\"", ZSTR_VAL(string_key));
42 return FAILURE;
43 }
44 } ZEND_HASH_FOREACH_END();
45
46 return SUCCESS;
47 }
48
xsl_proxy_factory(xmlNodePtr node,zval * child,dom_object * intern,xmlXPathParserContextPtr ctxt)49 static void xsl_proxy_factory(xmlNodePtr node, zval *child, dom_object *intern, xmlXPathParserContextPtr ctxt)
50 {
51 ZEND_ASSERT(node->type != XML_NAMESPACE_DECL);
52
53 /**
54 * Upon freeing libxslt's context, every document that is not the *main* document will be freed by libxslt.
55 * If a node of a document that is *not the main* document gets returned to userland, we'd free the node twice:
56 * first by the cleanup of the xslt context, and then by our own refcounting mechanism.
57 * To prevent this, we'll take a copy if the node is not from the main document.
58 * It is important that we do not copy the node unconditionally, because that means that:
59 * - modifications to the node will only modify the copy, and not the original
60 * - accesses to the parent, path, ... will not work
61 */
62 xsltTransformContextPtr transform_ctxt = (xsltTransformContextPtr) ctxt->context->extra;
63 if (node->doc != transform_ctxt->document->doc) {
64 node = xmlDocCopyNode(node, intern->document->ptr, 1);
65 }
66 php_dom_create_object(node, child, intern);
67 }
68
xsl_ext_fetch_intern(xmlXPathParserContextPtr ctxt)69 static xsl_object *xsl_ext_fetch_intern(xmlXPathParserContextPtr ctxt)
70 {
71 if (UNEXPECTED(!zend_is_executing())) {
72 xsltGenericError(xsltGenericErrorContext,
73 "xsltExtFunctionTest: Function called from outside of PHP\n");
74 return NULL;
75 }
76
77 xsltTransformContextPtr tctxt = xsltXPathGetTransformContext(ctxt);
78 if (UNEXPECTED(tctxt == NULL)) {
79 xsltGenericError(xsltGenericErrorContext,
80 "xsltExtFunctionTest: failed to get the transformation context\n");
81 return NULL;
82 }
83
84 xsl_object *intern = (xsl_object *) tctxt->_private;
85 if (UNEXPECTED(intern == NULL)) {
86 xsltGenericError(xsltGenericErrorContext,
87 "xsltExtFunctionTest: failed to get the internal object\n");
88 return NULL;
89 }
90 return intern;
91 }
92
xsl_ext_function_php(xmlXPathParserContextPtr ctxt,int nargs,php_dom_xpath_nodeset_evaluation_mode evaluation_mode)93 static void xsl_ext_function_php(xmlXPathParserContextPtr ctxt, int nargs, php_dom_xpath_nodeset_evaluation_mode evaluation_mode) /* {{{ */
94 {
95 xsl_object *intern = xsl_ext_fetch_intern(ctxt);
96 if (!intern) {
97 php_dom_xpath_callbacks_clean_argument_stack(ctxt, nargs);
98 } else {
99 php_dom_xpath_callbacks_call_php_ns(&intern->xpath_callbacks, ctxt, nargs, evaluation_mode, (dom_object *) intern->doc, xsl_proxy_factory);
100 }
101 }
102 /* }}} */
103
xsl_ext_function_string_php(xmlXPathParserContextPtr ctxt,int nargs)104 void xsl_ext_function_string_php(xmlXPathParserContextPtr ctxt, int nargs) /* {{{ */
105 {
106 xsl_ext_function_php(ctxt, nargs, PHP_DOM_XPATH_EVALUATE_NODESET_TO_STRING);
107 }
108 /* }}} */
109
xsl_ext_function_object_php(xmlXPathParserContextPtr ctxt,int nargs)110 void xsl_ext_function_object_php(xmlXPathParserContextPtr ctxt, int nargs) /* {{{ */
111 {
112 xsl_ext_function_php(ctxt, nargs, PHP_DOM_XPATH_EVALUATE_NODESET_TO_NODESET);
113 }
114 /* }}} */
115
xsl_ext_function_trampoline(xmlXPathParserContextPtr ctxt,int nargs)116 static void xsl_ext_function_trampoline(xmlXPathParserContextPtr ctxt, int nargs)
117 {
118 xsl_object *intern = xsl_ext_fetch_intern(ctxt);
119 if (!intern) {
120 php_dom_xpath_callbacks_clean_argument_stack(ctxt, nargs);
121 } else {
122 php_dom_xpath_callbacks_call_custom_ns(&intern->xpath_callbacks, ctxt, nargs, PHP_DOM_XPATH_EVALUATE_NODESET_TO_NODESET, (dom_object *) intern->doc, xsl_proxy_factory);
123 }
124 }
125
xsl_add_ns_to_map(xmlHashTablePtr table,xsltStylesheetPtr sheet,const xmlNode * cur,const xmlChar * prefix,const xmlChar * uri)126 static void xsl_add_ns_to_map(xmlHashTablePtr table, xsltStylesheetPtr sheet, const xmlNode *cur, const xmlChar *prefix, const xmlChar *uri)
127 {
128 const xmlChar *existing_url = xmlHashLookup(table, prefix);
129 if (existing_url != NULL && !xmlStrEqual(existing_url, uri)) {
130 xsltTransformError(NULL, sheet, (xmlNodePtr) cur, "Namespaces prefix %s used for multiple namespaces\n", prefix);
131 sheet->warnings++;
132 } else if (existing_url == NULL) {
133 xmlHashUpdateEntry(table, prefix, (void *) uri, NULL);
134 }
135 }
136
137 /* Adds all namespace declaration (not using nsDef) into a hash map that maps prefix to uri. Warns on conflicting declarations. */
xsl_build_ns_map(xmlHashTablePtr table,xsltStylesheetPtr sheet,php_dom_libxml_ns_mapper * ns_mapper,const xmlDoc * doc)138 static void xsl_build_ns_map(xmlHashTablePtr table, xsltStylesheetPtr sheet, php_dom_libxml_ns_mapper *ns_mapper, const xmlDoc *doc)
139 {
140 const xmlNode *cur = xmlDocGetRootElement(doc);
141
142 while (cur != NULL) {
143 if (cur->type == XML_ELEMENT_NODE) {
144 if (cur->ns != NULL && cur->ns->prefix != NULL) {
145 xsl_add_ns_to_map(table, sheet, cur, cur->ns->prefix, cur->ns->href);
146 }
147
148 for (const xmlAttr *attr = cur->properties; attr != NULL; attr = attr->next) {
149 if (attr->ns != NULL && attr->ns->prefix != NULL && php_dom_ns_is_fast_ex(attr->ns, php_dom_ns_is_xmlns_magic_token)
150 && attr->children != NULL && attr->children->content != NULL) {
151 /* This attribute declares a namespace, get the relevant instance.
152 * The declared namespace is not the same as the namespace of this attribute (which is xmlns). */
153 const xmlChar *prefix = attr->name;
154 xmlNsPtr ns = php_dom_libxml_ns_mapper_get_ns_raw_strings_nullsafe(ns_mapper, (const char *) prefix, (const char *) attr->children->content);
155 xsl_add_ns_to_map(table, sheet, cur, prefix, ns->href);
156 }
157 }
158 }
159
160 cur = php_dom_next_in_tree_order(cur, (const xmlNode *) doc);
161 }
162 }
163
164 /* Apply namespace corrections for new DOM */
165 typedef enum {
166 XSL_NS_HASH_CORRECTION_NONE = 0,
167 XSL_NS_HASH_CORRECTION_APPLIED = 1,
168 XSL_NS_HASH_CORRECTION_FAILED = 2
169 } xsl_ns_hash_correction_status;
170
xsl_apply_ns_hash_corrections(xsltStylesheetPtr sheetp,xmlNodePtr nodep,xmlDocPtr doc)171 static zend_always_inline xsl_ns_hash_correction_status xsl_apply_ns_hash_corrections(xsltStylesheetPtr sheetp, xmlNodePtr nodep, xmlDocPtr doc)
172 {
173 if (sheetp->nsHash == NULL) {
174 dom_object *node_intern = php_dom_object_get_data(nodep);
175 if (node_intern != NULL && php_dom_follow_spec_intern(node_intern)) {
176 sheetp->nsHash = xmlHashCreate(10);
177 if (UNEXPECTED(!sheetp->nsHash)) {
178 return XSL_NS_HASH_CORRECTION_FAILED;
179 }
180 xsl_build_ns_map(sheetp->nsHash, sheetp, php_dom_get_ns_mapper(node_intern), doc);
181 return XSL_NS_HASH_CORRECTION_APPLIED;
182 }
183 }
184 return XSL_NS_HASH_CORRECTION_NONE;
185 }
186
187 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#
188 Since:
189 */
PHP_METHOD(XSLTProcessor,importStylesheet)190 PHP_METHOD(XSLTProcessor, importStylesheet)
191 {
192 zval *id, *docp = NULL;
193 xmlDoc *doc = NULL, *newdoc = NULL;
194 xsltStylesheetPtr sheetp;
195 bool clone_docu = false;
196 xmlNode *nodep = NULL;
197 zval *cloneDocu, rv;
198 zend_string *member;
199
200 id = ZEND_THIS;
201 if (zend_parse_parameters(ZEND_NUM_ARGS(), "o", &docp) == FAILURE) {
202 RETURN_THROWS();
203 }
204
205 nodep = php_libxml_import_node(docp);
206
207 if (nodep) {
208 doc = nodep->doc;
209 }
210 if (doc == NULL) {
211 zend_argument_type_error(1, "must be a valid XML node");
212 RETURN_THROWS();
213 }
214
215 /* libxslt uses _private, so we must copy the imported
216 stylesheet document otherwise the node proxies will be a mess */
217 newdoc = xmlCopyDoc(doc, 1);
218 xmlNodeSetBase((xmlNodePtr) newdoc, (xmlChar *)doc->URL);
219 PHP_LIBXML_SANITIZE_GLOBALS(parse);
220 ZEND_DIAGNOSTIC_IGNORED_START("-Wdeprecated-declarations")
221 xmlSubstituteEntitiesDefault(1);
222 xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS;
223 ZEND_DIAGNOSTIC_IGNORED_END
224
225 sheetp = xsltParseStylesheetDoc(newdoc);
226 PHP_LIBXML_RESTORE_GLOBALS(parse);
227
228 if (!sheetp) {
229 xmlFreeDoc(newdoc);
230 RETURN_FALSE;
231 }
232
233 xsl_object *intern = Z_XSL_P(id);
234
235 xsl_ns_hash_correction_status status = xsl_apply_ns_hash_corrections(sheetp, nodep, doc);
236 if (UNEXPECTED(status == XSL_NS_HASH_CORRECTION_FAILED)) {
237 xsltFreeStylesheet(sheetp);
238 xmlFreeDoc(newdoc);
239 RETURN_FALSE;
240 } else if (status == XSL_NS_HASH_CORRECTION_APPLIED) {
241 /* The namespace mappings need to be kept alive.
242 * This is stored in the ref obj outside of libxml2, but that means that the sheet won't keep it alive
243 * unlike with namespaces from old DOM. */
244 if (intern->sheet_ref_obj) {
245 php_libxml_decrement_doc_ref_directly(intern->sheet_ref_obj);
246 }
247 intern->sheet_ref_obj = Z_LIBXML_NODE_P(docp)->document;
248 intern->sheet_ref_obj->refcount++;
249 }
250
251 member = ZSTR_INIT_LITERAL("cloneDocument", 0);
252 cloneDocu = zend_std_read_property(Z_OBJ_P(id), member, BP_VAR_R, NULL, &rv);
253 clone_docu = zend_is_true(cloneDocu);
254 zend_string_release_ex(member, 0);
255 if (!clone_docu) {
256 /* Check if the stylesheet is using xsl:key, if yes, we have to clone the document _always_ before a transformation.
257 * xsl:key elements may only occur at the top level. Furthermore, all elements at the top level must be in a
258 * namespace (if not, then the stylesheet is not well-formed and this function will have returned false earlier). */
259 nodep = xmlDocGetRootElement(sheetp->doc);
260 if (nodep && (nodep = nodep->children)) {
261 while (nodep) {
262 ZEND_ASSERT(nodep->ns != NULL);
263 if (nodep->type == XML_ELEMENT_NODE && xmlStrEqual(nodep->name, (const xmlChar *) "key") && xmlStrEqual(nodep->ns->href, XSLT_NAMESPACE)) {
264 intern->hasKeys = true;
265 break;
266 }
267 nodep = nodep->next;
268 }
269 }
270 } else {
271 intern->hasKeys = true;
272 }
273
274 xsl_free_sheet(intern);
275
276 php_xsl_set_object(id, sheetp);
277 RETVAL_TRUE;
278 }
279 /* }}} end XSLTProcessor::importStylesheet */
280
php_xsl_delayed_lib_registration(void * ctxt,const zend_string * ns,const zend_string * name)281 static void php_xsl_delayed_lib_registration(void *ctxt, const zend_string *ns, const zend_string *name)
282 {
283 xsltTransformContextPtr xsl = (xsltTransformContextPtr) ctxt;
284 xsltRegisterExtFunction(xsl, (const xmlChar *) ZSTR_VAL(name), (const xmlChar *) ZSTR_VAL(ns), xsl_ext_function_trampoline);
285 }
286
php_xsl_apply_stylesheet(zval * id,xsl_object * intern,xsltStylesheetPtr style,zval * docp)287 static xmlDocPtr php_xsl_apply_stylesheet(zval *id, xsl_object *intern, xsltStylesheetPtr style, zval *docp) /* {{{ */
288 {
289 xmlDocPtr newdocp = NULL;
290 xmlDocPtr doc = NULL;
291 xmlNodePtr node = NULL;
292 xsltTransformContextPtr ctxt;
293 php_libxml_node_object *object;
294 zval *doXInclude, rv;
295 zend_string *member;
296 FILE *f;
297 int secPrefsError = 0;
298 xsltSecurityPrefsPtr secPrefs = NULL;
299
300 node = php_libxml_import_node(docp);
301
302 if (node) {
303 doc = node->doc;
304 }
305
306 if (doc == NULL) {
307 zend_argument_type_error(1, "must be a valid XML node");
308 return NULL;
309 }
310
311 if (style == NULL) {
312 zend_string *name = get_active_function_or_method_name();
313 zend_throw_error(NULL, "%s() can only be called after a stylesheet has been imported",
314 ZSTR_VAL(name));
315 zend_string_release(name);
316 return NULL;
317 }
318
319 if (intern->profiling) {
320 if (php_check_open_basedir(ZSTR_VAL(intern->profiling))) {
321 f = NULL;
322 } else {
323 f = VCWD_FOPEN(ZSTR_VAL(intern->profiling), "w");
324 }
325 } else {
326 f = NULL;
327 }
328
329 intern->doc = emalloc(sizeof(php_libxml_node_object));
330 memset(intern->doc, 0, sizeof(php_libxml_node_object));
331
332 if (intern->hasKeys) {
333 doc = xmlCopyDoc(doc, 1);
334 } else {
335 object = Z_LIBXML_NODE_P(docp);
336 intern->doc->document = object->document;
337 }
338
339 php_libxml_increment_doc_ref(intern->doc, doc);
340
341 ctxt = xsltNewTransformContext(style, doc);
342 ctxt->_private = (void *) intern;
343
344 if (intern->parameter) {
345 zend_result status = php_xsl_xslt_apply_params(ctxt, intern->parameter);
346 if (UNEXPECTED(status != SUCCESS) && EG(exception)) {
347 goto out;
348 }
349 }
350
351 member = ZSTR_INIT_LITERAL("doXInclude", 0);
352 doXInclude = zend_std_read_property(Z_OBJ_P(id), member, BP_VAR_R, NULL, &rv);
353 ctxt->xinclude = zend_is_true(doXInclude);
354 zend_string_release_ex(member, 0);
355
356 zval *max_template_depth = xsl_prop_max_template_depth(Z_OBJ_P(id));
357 ZEND_ASSERT(Z_TYPE_P(max_template_depth) == IS_LONG);
358 ctxt->maxTemplateDepth = Z_LVAL_P(max_template_depth);
359
360 zval *max_template_vars = xsl_prop_max_template_vars(Z_OBJ_P(id));
361 ZEND_ASSERT(Z_TYPE_P(max_template_vars) == IS_LONG);
362 ctxt->maxTemplateVars = Z_LVAL_P(max_template_vars);
363
364 zend_long secPrefsValue = intern->securityPrefs;
365
366 /* if securityPrefs is set to NONE, we don't have to do any checks, but otherwise... */
367 if (secPrefsValue != XSL_SECPREF_NONE) {
368 secPrefs = xsltNewSecurityPrefs();
369 if (secPrefsValue & XSL_SECPREF_READ_FILE ) {
370 if (0 != xsltSetSecurityPrefs(secPrefs, XSLT_SECPREF_READ_FILE, xsltSecurityForbid)) {
371 secPrefsError = 1;
372 }
373 }
374 if (secPrefsValue & XSL_SECPREF_WRITE_FILE ) {
375 if (0 != xsltSetSecurityPrefs(secPrefs, XSLT_SECPREF_WRITE_FILE, xsltSecurityForbid)) {
376 secPrefsError = 1;
377 }
378 }
379 if (secPrefsValue & XSL_SECPREF_CREATE_DIRECTORY ) {
380 if (0 != xsltSetSecurityPrefs(secPrefs, XSLT_SECPREF_CREATE_DIRECTORY, xsltSecurityForbid)) {
381 secPrefsError = 1;
382 }
383 }
384 if (secPrefsValue & XSL_SECPREF_READ_NETWORK) {
385 if (0 != xsltSetSecurityPrefs(secPrefs, XSLT_SECPREF_READ_NETWORK, xsltSecurityForbid)) {
386 secPrefsError = 1;
387 }
388 }
389 if (secPrefsValue & XSL_SECPREF_WRITE_NETWORK) {
390 if (0 != xsltSetSecurityPrefs(secPrefs, XSLT_SECPREF_WRITE_NETWORK, xsltSecurityForbid)) {
391 secPrefsError = 1;
392 }
393 }
394
395 if (0 != xsltSetCtxtSecurityPrefs(secPrefs, ctxt)) {
396 secPrefsError = 1;
397 }
398 }
399
400 php_dom_xpath_callbacks_delayed_lib_registration(&intern->xpath_callbacks, ctxt, php_xsl_delayed_lib_registration);
401
402 if (secPrefsError == 1) {
403 php_error_docref(NULL, E_WARNING, "Can't set libxslt security properties, not doing transformation for security reasons");
404 } else {
405 newdocp = xsltApplyStylesheetUser(style, doc, /* params (handled manually) */ NULL, /* output */ NULL, f, ctxt);
406 }
407
408 out:
409 if (f) {
410 fclose(f);
411 }
412
413 xsltFreeTransformContext(ctxt);
414 if (secPrefs) {
415 xsltFreeSecurityPrefs(secPrefs);
416 }
417
418 php_dom_xpath_callbacks_clean_node_list(&intern->xpath_callbacks);
419
420 php_libxml_decrement_doc_ref(intern->doc);
421 efree(intern->doc);
422 intern->doc = NULL;
423
424 return newdocp;
425
426 }
427 /* }}} */
428
429 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#
430 Since:
431 */
PHP_METHOD(XSLTProcessor,transformToDoc)432 PHP_METHOD(XSLTProcessor, transformToDoc)
433 {
434 zval *id, *docp = NULL;
435 xmlDoc *newdocp;
436 xsltStylesheetPtr sheetp;
437 zend_class_entry *ret_class = NULL;
438 xsl_object *intern;
439
440 id = ZEND_THIS;
441 intern = Z_XSL_P(id);
442 sheetp = (xsltStylesheetPtr) intern->ptr;
443
444 if (zend_parse_parameters(ZEND_NUM_ARGS(), "o|C!", &docp, &ret_class) == FAILURE) {
445 RETURN_THROWS();
446 }
447
448 newdocp = php_xsl_apply_stylesheet(id, intern, sheetp, docp);
449
450 if (newdocp) {
451 if (ret_class) {
452 zend_string *curclass_name;
453 zend_class_entry *curce;
454 php_libxml_node_object *interndoc;
455
456 curce = Z_OBJCE_P(docp);
457 curclass_name = curce->name;
458 while (curce->parent != NULL) {
459 curce = curce->parent;
460 }
461
462 if (!instanceof_function(ret_class, curce)) {
463 xmlFreeDoc(newdocp);
464 zend_argument_type_error(2, "must be a class name compatible with %s, %s given",
465 ZSTR_VAL(curclass_name), ZSTR_VAL(ret_class->name)
466 );
467 RETURN_THROWS();
468 }
469
470 object_init_ex(return_value, ret_class);
471
472 interndoc = Z_LIBXML_NODE_P(return_value);
473 php_libxml_increment_doc_ref(interndoc, newdocp);
474 php_libxml_increment_node_ptr(interndoc, (xmlNodePtr)newdocp, (void *)interndoc);
475 } else {
476 php_dom_create_object((xmlNodePtr) newdocp, return_value, NULL);
477 }
478 } else {
479 RETURN_FALSE;
480 }
481 }
482 /* }}} end XSLTProcessor::transformToDoc */
483
484 /* {{{ */
PHP_METHOD(XSLTProcessor,transformToUri)485 PHP_METHOD(XSLTProcessor, transformToUri)
486 {
487 zval *id, *docp = NULL;
488 xmlDoc *newdocp;
489 xsltStylesheetPtr sheetp;
490 int ret;
491 size_t uri_len;
492 char *uri;
493 xsl_object *intern;
494
495 id = ZEND_THIS;
496 intern = Z_XSL_P(id);
497 sheetp = (xsltStylesheetPtr) intern->ptr;
498
499 if (zend_parse_parameters(ZEND_NUM_ARGS(), "op", &docp, &uri, &uri_len) == FAILURE) {
500 RETURN_THROWS();
501 }
502
503 newdocp = php_xsl_apply_stylesheet(id, intern, sheetp, docp);
504
505 ret = -1;
506 if (newdocp) {
507 ret = xsltSaveResultToFilename(uri, newdocp, sheetp, 0);
508 xmlFreeDoc(newdocp);
509 }
510
511 RETVAL_LONG(ret);
512 }
513 /* }}} end XSLTProcessor::transformToUri */
514
515 /* {{{ */
PHP_METHOD(XSLTProcessor,transformToXml)516 PHP_METHOD(XSLTProcessor, transformToXml)
517 {
518 zval *id, *docp = NULL;
519 xmlDoc *newdocp;
520 xsltStylesheetPtr sheetp;
521 int ret;
522 xmlChar *doc_txt_ptr;
523 int doc_txt_len;
524 xsl_object *intern;
525
526 id = ZEND_THIS;
527 intern = Z_XSL_P(id);
528 sheetp = (xsltStylesheetPtr) intern->ptr;
529
530 if (zend_parse_parameters(ZEND_NUM_ARGS(), "o", &docp) == FAILURE) {
531 RETURN_THROWS();
532 }
533
534 newdocp = php_xsl_apply_stylesheet(id, intern, sheetp, docp);
535
536 ret = -1;
537 if (newdocp) {
538 ret = xsltSaveResultToString(&doc_txt_ptr, &doc_txt_len, newdocp, sheetp);
539 if (doc_txt_ptr && doc_txt_len) {
540 RETVAL_STRINGL((char *) doc_txt_ptr, doc_txt_len);
541 xmlFree(doc_txt_ptr);
542 }
543 xmlFreeDoc(newdocp);
544 }
545
546 if (ret < 0) {
547 RETURN_FALSE;
548 }
549 }
550 /* }}} end XSLTProcessor::transformToXml */
551
xsl_create_parameter_key(uint32_t arg_num,const zend_string * namespace,zend_string * name)552 static zend_string *xsl_create_parameter_key(uint32_t arg_num, const zend_string *namespace, zend_string *name)
553 {
554 if (ZSTR_LEN(namespace) == 0) {
555 return zend_string_copy(name);
556 }
557
558 /* Clark notation already sets the namespace and we cannot have a double namespace declaration. */
559 if (ZSTR_VAL(name)[0] == '{') {
560 zend_argument_value_error(arg_num, "must not use clark notation when argument #1 ($namespace) is not empty");
561 return NULL;
562 }
563
564 /* Cannot be a QName as that would cause a namespace lookup in the document. */
565 if (ZSTR_VAL(name)[0] != ':' && strchr(ZSTR_VAL(name), ':')) {
566 zend_argument_value_error(arg_num, "must not be a QName when argument #1 ($namespace) is not empty");
567 return NULL;
568 }
569
570 zend_string *clark_str = zend_string_safe_alloc(1, ZSTR_LEN(name), 2 + ZSTR_LEN(namespace), false);
571 ZSTR_VAL(clark_str)[0] = '{';
572 memcpy(ZSTR_VAL(clark_str) + 1, ZSTR_VAL(namespace), ZSTR_LEN(namespace));
573 ZSTR_VAL(clark_str)[ZSTR_LEN(namespace) + 1] = '}';
574 memcpy(ZSTR_VAL(clark_str) + 2 + ZSTR_LEN(namespace), ZSTR_VAL(name), ZSTR_LEN(name) + 1 /* include '\0' */);
575 return clark_str;
576 }
577
578 /* {{{ */
PHP_METHOD(XSLTProcessor,setParameter)579 PHP_METHOD(XSLTProcessor, setParameter)
580 {
581
582 zval *id = ZEND_THIS;
583 zval *entry, new_string;
584 HashTable *array_value;
585 xsl_object *intern;
586 zend_string *namespace, *string_key, *name, *value = NULL;
587
588 ZEND_PARSE_PARAMETERS_START(2, 3)
589 Z_PARAM_PATH_STR(namespace)
590 Z_PARAM_ARRAY_HT_OR_STR(array_value, name)
591 Z_PARAM_OPTIONAL
592 Z_PARAM_PATH_STR_OR_NULL(value)
593 ZEND_PARSE_PARAMETERS_END();
594
595 intern = Z_XSL_P(id);
596
597 if (array_value) {
598 if (value) {
599 zend_argument_value_error(3, "must be null when argument #2 ($name) is an array");
600 RETURN_THROWS();
601 }
602
603 ZEND_HASH_FOREACH_STR_KEY_VAL(array_value, string_key, entry) {
604 zval tmp;
605 zend_string *str;
606
607 if (string_key == NULL) {
608 zend_argument_type_error(2, "must contain only string keys");
609 RETURN_THROWS();
610 }
611
612 if (UNEXPECTED(CHECK_NULL_PATH(ZSTR_VAL(string_key), ZSTR_LEN(string_key)))) {
613 zend_argument_value_error(3, "must not contain keys with any null bytes");
614 RETURN_THROWS();
615 }
616
617 zend_string *ht_key = xsl_create_parameter_key(2, namespace, string_key);
618 if (!ht_key) {
619 RETURN_THROWS();
620 }
621
622 str = zval_try_get_string(entry);
623 if (UNEXPECTED(!str)) {
624 zend_string_release_ex(ht_key, false);
625 RETURN_THROWS();
626 }
627
628 if (UNEXPECTED(CHECK_NULL_PATH(ZSTR_VAL(str), ZSTR_LEN(str)))) {
629 zend_string_release(str);
630 zend_string_release_ex(ht_key, false);
631 zend_argument_value_error(3, "must not contain values with any null bytes");
632 RETURN_THROWS();
633 }
634
635 ZVAL_STR(&tmp, str);
636 zend_hash_update(intern->parameter, ht_key, &tmp);
637 zend_string_release_ex(ht_key, false);
638 } ZEND_HASH_FOREACH_END();
639 RETURN_TRUE;
640 } else {
641 if (!value) {
642 zend_argument_value_error(3, "cannot be null when argument #2 ($name) is a string");
643 RETURN_THROWS();
644 }
645
646 if (UNEXPECTED(CHECK_NULL_PATH(ZSTR_VAL(name), ZSTR_LEN(name)))) {
647 zend_argument_value_error(2, "must not contain any null bytes");
648 RETURN_THROWS();
649 }
650
651 zend_string *key = xsl_create_parameter_key(2, namespace, name);
652 if (!key) {
653 RETURN_THROWS();
654 }
655
656 ZVAL_STR_COPY(&new_string, value);
657
658 zend_hash_update(intern->parameter, key, &new_string);
659 zend_string_release_ex(key, false);
660 RETURN_TRUE;
661 }
662 }
663 /* }}} end XSLTProcessor::setParameter */
664
665 /* {{{ */
PHP_METHOD(XSLTProcessor,getParameter)666 PHP_METHOD(XSLTProcessor, getParameter)
667 {
668 zval *id = ZEND_THIS;
669 zval *value;
670 zend_string *namespace, *name;
671 xsl_object *intern;
672
673 if (zend_parse_parameters(ZEND_NUM_ARGS(), "PP", &namespace, &name) == FAILURE) {
674 RETURN_THROWS();
675 }
676 zend_string *key = xsl_create_parameter_key(2, namespace, name);
677 if (!key) {
678 RETURN_THROWS();
679 }
680 intern = Z_XSL_P(id);
681 value = zend_hash_find(intern->parameter, key);
682 zend_string_release_ex(key, false);
683 if (value != NULL) {
684 RETURN_STR_COPY(Z_STR_P(value));
685 } else {
686 RETURN_FALSE;
687 }
688 }
689 /* }}} end XSLTProcessor::getParameter */
690
691 /* {{{ */
PHP_METHOD(XSLTProcessor,removeParameter)692 PHP_METHOD(XSLTProcessor, removeParameter)
693 {
694 zval *id = ZEND_THIS;
695 zend_string *namespace, *name;
696 xsl_object *intern;
697
698 if (zend_parse_parameters(ZEND_NUM_ARGS(), "PP", &namespace, &name) == FAILURE) {
699 RETURN_THROWS();
700 }
701 zend_string *key = xsl_create_parameter_key(2, namespace, name);
702 if (!key) {
703 RETURN_THROWS();
704 }
705 intern = Z_XSL_P(id);
706 if (zend_hash_del(intern->parameter, key) == SUCCESS) {
707 RETVAL_TRUE;
708 } else {
709 RETVAL_FALSE;
710 }
711 zend_string_release_ex(key, false);
712 }
713 /* }}} end XSLTProcessor::removeParameter */
714
715 /* {{{ */
PHP_METHOD(XSLTProcessor,registerPHPFunctions)716 PHP_METHOD(XSLTProcessor, registerPHPFunctions)
717 {
718 xsl_object *intern = Z_XSL_P(ZEND_THIS);
719
720 zend_string *name = NULL;
721 HashTable *callable_ht = NULL;
722
723 ZEND_PARSE_PARAMETERS_START(0, 1)
724 Z_PARAM_OPTIONAL
725 Z_PARAM_ARRAY_HT_OR_STR_OR_NULL(callable_ht, name)
726 ZEND_PARSE_PARAMETERS_END();
727
728 php_dom_xpath_callbacks_update_method_handler(
729 &intern->xpath_callbacks,
730 NULL,
731 NULL,
732 name,
733 callable_ht,
734 PHP_DOM_XPATH_CALLBACK_NAME_VALIDATE_NULLS,
735 NULL
736 );
737 }
738 /* }}} end XSLTProcessor::registerPHPFunctions(); */
739
PHP_METHOD(XSLTProcessor,registerPHPFunctionNS)740 PHP_METHOD(XSLTProcessor, registerPHPFunctionNS)
741 {
742 xsl_object *intern = Z_XSL_P(ZEND_THIS);
743
744 zend_string *namespace, *name;
745 zend_fcall_info fci;
746 zend_fcall_info_cache fcc;
747
748 ZEND_PARSE_PARAMETERS_START(3, 3)
749 Z_PARAM_PATH_STR(namespace)
750 Z_PARAM_PATH_STR(name)
751 Z_PARAM_FUNC_NO_TRAMPOLINE_FREE(fci, fcc)
752 ZEND_PARSE_PARAMETERS_END();
753
754 if (zend_string_equals_literal(namespace, "http://php.net/xsl")) {
755 zend_release_fcall_info_cache(&fcc);
756 zend_argument_value_error(1, "must not be \"http://php.net/xsl\" because it is reserved by PHP");
757 RETURN_THROWS();
758 }
759
760 if (php_dom_xpath_callbacks_update_single_method_handler(
761 &intern->xpath_callbacks,
762 NULL,
763 namespace,
764 name,
765 &fcc,
766 PHP_DOM_XPATH_CALLBACK_NAME_VALIDATE_NCNAME,
767 NULL
768 ) != SUCCESS) {
769 zend_release_fcall_info_cache(&fcc);
770 }
771 }
772
773 /* {{{ */
PHP_METHOD(XSLTProcessor,setProfiling)774 PHP_METHOD(XSLTProcessor, setProfiling)
775 {
776 zval *id = ZEND_THIS;
777 xsl_object *intern;
778 zend_string *filename = NULL;
779
780 if (zend_parse_parameters(ZEND_NUM_ARGS(), "P!", &filename) == FAILURE) {
781 RETURN_THROWS();
782 }
783
784 intern = Z_XSL_P(id);
785 if (intern->profiling) {
786 zend_string_release(intern->profiling);
787 }
788 if (filename != NULL) {
789 intern->profiling = zend_string_copy(filename);
790 } else {
791 intern->profiling = NULL;
792 }
793
794 RETURN_TRUE;
795 }
796 /* }}} end XSLTProcessor::setProfiling */
797
798 /* {{{ */
PHP_METHOD(XSLTProcessor,setSecurityPrefs)799 PHP_METHOD(XSLTProcessor, setSecurityPrefs)
800 {
801 zval *id = ZEND_THIS;
802 xsl_object *intern;
803 zend_long securityPrefs, oldSecurityPrefs;
804
805 if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &securityPrefs) == FAILURE) {
806 RETURN_THROWS();
807 }
808 intern = Z_XSL_P(id);
809 oldSecurityPrefs = intern->securityPrefs;
810 intern->securityPrefs = securityPrefs;
811 RETURN_LONG(oldSecurityPrefs);
812 }
813 /* }}} end XSLTProcessor::setSecurityPrefs */
814
815 /* {{{ */
PHP_METHOD(XSLTProcessor,getSecurityPrefs)816 PHP_METHOD(XSLTProcessor, getSecurityPrefs)
817 {
818 zval *id = ZEND_THIS;
819 xsl_object *intern;
820
821 if (zend_parse_parameters_none() == FAILURE) {
822 RETURN_THROWS();
823 }
824
825 intern = Z_XSL_P(id);
826
827 RETURN_LONG(intern->securityPrefs);
828 }
829 /* }}} end XSLTProcessor::getSecurityPrefs */
830
831 /* {{{ */
PHP_METHOD(XSLTProcessor,hasExsltSupport)832 PHP_METHOD(XSLTProcessor, hasExsltSupport)
833 {
834 if (zend_parse_parameters_none() == FAILURE) {
835 RETURN_THROWS();
836 }
837
838 #ifdef HAVE_XSL_EXSLT
839 RETURN_TRUE;
840 #else
841 RETURN_FALSE;
842 #endif
843 }
844 /* }}} end XSLTProcessor::hasExsltSupport(); */
845