xref: /PHP-8.2/ext/xsl/php_xsl.c (revision 428ecf48)
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   | Author: Christian Stocker <chregu@php.net>                           |
14   +----------------------------------------------------------------------+
15 */
16 
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20 
21 #include "php.h"
22 #include "php_ini.h"
23 #include "ext/standard/info.h"
24 #include "php_xsl.h"
25 #include "php_xsl_arginfo.h"
26 
27 zend_class_entry *xsl_xsltprocessor_class_entry;
28 static zend_object_handlers xsl_object_handlers;
29 
30 static const zend_module_dep xsl_deps[] = {
31 	ZEND_MOD_REQUIRED("libxml")
32 	ZEND_MOD_REQUIRED("dom")
33 	ZEND_MOD_END
34 };
35 
36 /* {{{ xsl_module_entry */
37 zend_module_entry xsl_module_entry = {
38 	STANDARD_MODULE_HEADER_EX, NULL,
39 	xsl_deps,
40 	"xsl",
41 	NULL,
42 	PHP_MINIT(xsl),
43 	PHP_MSHUTDOWN(xsl),
44 	NULL,
45 	NULL,
46 	PHP_MINFO(xsl),
47 	PHP_XSL_VERSION,
48 	STANDARD_MODULE_PROPERTIES
49 };
50 /* }}} */
51 
52 #ifdef COMPILE_DL_XSL
ZEND_GET_MODULE(xsl)53 ZEND_GET_MODULE(xsl)
54 #endif
55 
56 /* {{{ xsl_objects_free_storage */
57 void xsl_objects_free_storage(zend_object *object)
58 {
59 	xsl_object *intern = php_xsl_fetch_object(object);
60 
61 	zend_object_std_dtor(&intern->std);
62 
63 	zend_hash_destroy(intern->parameter);
64 	FREE_HASHTABLE(intern->parameter);
65 
66 	zend_hash_destroy(intern->registered_phpfunctions);
67 	FREE_HASHTABLE(intern->registered_phpfunctions);
68 
69 	if (intern->node_list) {
70 		zend_hash_destroy(intern->node_list);
71 		FREE_HASHTABLE(intern->node_list);
72 	}
73 
74 	if (intern->doc) {
75 		php_libxml_decrement_doc_ref(intern->doc);
76 		efree(intern->doc);
77 	}
78 
79 	if (intern->ptr) {
80 		/* free wrapper */
81 		if (((xsltStylesheetPtr) intern->ptr)->_private != NULL) {
82 			((xsltStylesheetPtr) intern->ptr)->_private = NULL;
83 		}
84 
85 		xsltFreeStylesheet((xsltStylesheetPtr) intern->ptr);
86 		intern->ptr = NULL;
87 	}
88 	if (intern->profiling) {
89 		efree(intern->profiling);
90 	}
91 }
92 /* }}} */
93 
94 /* {{{ xsl_objects_new */
xsl_objects_new(zend_class_entry * class_type)95 zend_object *xsl_objects_new(zend_class_entry *class_type)
96 {
97 	xsl_object *intern;
98 
99 	intern = zend_object_alloc(sizeof(xsl_object), class_type);
100 	intern->securityPrefs = XSL_SECPREF_DEFAULT;
101 
102 	zend_object_std_init(&intern->std, class_type);
103 	object_properties_init(&intern->std, class_type);
104 	intern->parameter = zend_new_array(0);
105 	intern->registered_phpfunctions = zend_new_array(0);
106 
107 	intern->std.handlers = &xsl_object_handlers;
108 	return &intern->std;
109 }
110 /* }}} */
111 
112 /* {{{ PHP_MINIT_FUNCTION */
PHP_MINIT_FUNCTION(xsl)113 PHP_MINIT_FUNCTION(xsl)
114 {
115 	memcpy(&xsl_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
116 	xsl_object_handlers.offset = XtOffsetOf(xsl_object, std);
117 	xsl_object_handlers.clone_obj = NULL;
118 	xsl_object_handlers.free_obj = xsl_objects_free_storage;
119 
120 	xsl_xsltprocessor_class_entry = register_class_XSLTProcessor();
121 	xsl_xsltprocessor_class_entry->create_object = xsl_objects_new;
122 
123 #ifdef HAVE_XSL_EXSLT
124 	exsltRegisterAll();
125 #endif
126 
127 	xsltRegisterExtModuleFunction ((const xmlChar *) "functionString",
128 				   (const xmlChar *) "http://php.net/xsl",
129 				   xsl_ext_function_string_php);
130 	xsltRegisterExtModuleFunction ((const xmlChar *) "function",
131 				   (const xmlChar *) "http://php.net/xsl",
132 				   xsl_ext_function_object_php);
133 	xsltSetGenericErrorFunc(NULL, php_libxml_error_handler);
134 
135 	register_php_xsl_symbols(module_number);
136 
137 	return SUCCESS;
138 }
139 /* }}} */
140 
141 /* {{{ xsl_object_get_data */
xsl_object_get_data(void * obj)142 zval *xsl_object_get_data(void *obj)
143 {
144 	zval *dom_wrapper;
145 	dom_wrapper = ((xsltStylesheetPtr) obj)->_private;
146 	return dom_wrapper;
147 }
148 /* }}} */
149 
150 /* {{{ xsl_object_set_data */
xsl_object_set_data(void * obj,zval * wrapper)151 static void xsl_object_set_data(void *obj, zval *wrapper)
152 {
153 	((xsltStylesheetPtr) obj)->_private = wrapper;
154 }
155 /* }}} */
156 
157 /* {{{ php_xsl_set_object */
php_xsl_set_object(zval * wrapper,void * obj)158 void php_xsl_set_object(zval *wrapper, void *obj)
159 {
160 	xsl_object *object;
161 
162 	object = Z_XSL_P(wrapper);
163 	object->ptr = obj;
164 	xsl_object_set_data(obj, wrapper);
165 }
166 /* }}} */
167 
168 /* {{{ php_xsl_create_object */
php_xsl_create_object(xsltStylesheetPtr obj,zval * wrapper_in,zval * return_value)169 void php_xsl_create_object(xsltStylesheetPtr obj, zval *wrapper_in, zval *return_value )
170 {
171 	zval *wrapper;
172 	zend_class_entry *ce;
173 
174 	if (!obj) {
175 		wrapper = wrapper_in;
176 		ZVAL_NULL(wrapper);
177 		return;
178 	}
179 
180 	if ((wrapper = xsl_object_get_data((void *) obj))) {
181 		ZVAL_COPY(wrapper, wrapper_in);
182 		return;
183 	}
184 
185 	if (!wrapper_in) {
186 		wrapper = return_value;
187 	} else {
188 		wrapper = wrapper_in;
189 	}
190 
191 
192 	ce = xsl_xsltprocessor_class_entry;
193 
194 	if (!wrapper_in) {
195 		object_init_ex(wrapper, ce);
196 	}
197 	php_xsl_set_object(wrapper, (void *) obj);
198 
199 	return;
200 }
201 /* }}} */
202 
203 /* {{{ PHP_MSHUTDOWN_FUNCTION */
PHP_MSHUTDOWN_FUNCTION(xsl)204 PHP_MSHUTDOWN_FUNCTION(xsl)
205 {
206 	xsltUnregisterExtModuleFunction ((const xmlChar *) "functionString",
207 				   (const xmlChar *) "http://php.net/xsl");
208 	xsltUnregisterExtModuleFunction ((const xmlChar *) "function",
209 				   (const xmlChar *) "http://php.net/xsl");
210 	xsltSetGenericErrorFunc(NULL, NULL);
211 	xsltCleanupGlobals();
212 
213 	return SUCCESS;
214 }
215 /* }}} */
216 
217 /* {{{ PHP_MINFO_FUNCTION */
PHP_MINFO_FUNCTION(xsl)218 PHP_MINFO_FUNCTION(xsl)
219 {
220 	php_info_print_table_start();
221 	{
222 		char buffer[128];
223 		int major, minor, subminor;
224 
225 		php_info_print_table_row(2, "XSL", "enabled");
226 		major = xsltLibxsltVersion/10000;
227 		minor = (xsltLibxsltVersion - major * 10000) / 100;
228 		subminor = (xsltLibxsltVersion - major * 10000 - minor * 100);
229 		snprintf(buffer, 128, "%d.%d.%d", major, minor, subminor);
230 		php_info_print_table_row(2, "libxslt Version", buffer);
231 		major = xsltLibxmlVersion/10000;
232 		minor = (xsltLibxmlVersion - major * 10000) / 100;
233 		subminor = (xsltLibxmlVersion - major * 10000 - minor * 100);
234 		snprintf(buffer, 128, "%d.%d.%d", major, minor, subminor);
235 		php_info_print_table_row(2, "libxslt compiled against libxml Version", buffer);
236 	}
237 #ifdef HAVE_XSL_EXSLT
238 	php_info_print_table_row(2, "EXSLT", "enabled");
239 	php_info_print_table_row(2, "libexslt Version", LIBEXSLT_DOTTED_VERSION);
240 #endif
241 	php_info_print_table_end();
242 }
243 /* }}} */
244