xref: /PHP-8.3/ext/xsl/php_xsl.c (revision 35d93d29)
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 	if (intern->parameter) {
64 		zend_hash_destroy(intern->parameter);
65 		FREE_HASHTABLE(intern->parameter);
66 	}
67 
68 	if (intern->registered_phpfunctions) {
69 		zend_hash_destroy(intern->registered_phpfunctions);
70 		FREE_HASHTABLE(intern->registered_phpfunctions);
71 	}
72 
73 	if (intern->node_list) {
74 		zend_hash_destroy(intern->node_list);
75 		FREE_HASHTABLE(intern->node_list);
76 	}
77 
78 	if (intern->doc) {
79 		php_libxml_decrement_doc_ref(intern->doc);
80 		efree(intern->doc);
81 	}
82 
83 	if (intern->ptr) {
84 		/* free wrapper */
85 		if (((xsltStylesheetPtr) intern->ptr)->_private != NULL) {
86 			((xsltStylesheetPtr) intern->ptr)->_private = NULL;
87 		}
88 
89 		xsltFreeStylesheet((xsltStylesheetPtr) intern->ptr);
90 		intern->ptr = NULL;
91 	}
92 	if (intern->profiling) {
93 		efree(intern->profiling);
94 	}
95 }
96 /* }}} */
97 
98 /* {{{ xsl_objects_new */
xsl_objects_new(zend_class_entry * class_type)99 zend_object *xsl_objects_new(zend_class_entry *class_type)
100 {
101 	xsl_object *intern;
102 
103 	intern = zend_object_alloc(sizeof(xsl_object), class_type);
104 	intern->securityPrefs = XSL_SECPREF_DEFAULT;
105 
106 	zend_object_std_init(&intern->std, class_type);
107 	object_properties_init(&intern->std, class_type);
108 	intern->parameter = zend_new_array(0);
109 	intern->registered_phpfunctions = zend_new_array(0);
110 
111 	return &intern->std;
112 }
113 /* }}} */
114 
115 /* {{{ PHP_MINIT_FUNCTION */
PHP_MINIT_FUNCTION(xsl)116 PHP_MINIT_FUNCTION(xsl)
117 {
118 	memcpy(&xsl_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
119 	xsl_object_handlers.offset = XtOffsetOf(xsl_object, std);
120 	xsl_object_handlers.clone_obj = NULL;
121 	xsl_object_handlers.free_obj = xsl_objects_free_storage;
122 
123 	xsl_xsltprocessor_class_entry = register_class_XSLTProcessor();
124 	xsl_xsltprocessor_class_entry->create_object = xsl_objects_new;
125 	xsl_xsltprocessor_class_entry->default_object_handlers = &xsl_object_handlers;
126 
127 #ifdef HAVE_XSL_EXSLT
128 	exsltRegisterAll();
129 #endif
130 
131 	xsltRegisterExtModuleFunction ((const xmlChar *) "functionString",
132 				   (const xmlChar *) "http://php.net/xsl",
133 				   xsl_ext_function_string_php);
134 	xsltRegisterExtModuleFunction ((const xmlChar *) "function",
135 				   (const xmlChar *) "http://php.net/xsl",
136 				   xsl_ext_function_object_php);
137 	xsltSetGenericErrorFunc(NULL, php_libxml_error_handler);
138 
139 	register_php_xsl_symbols(module_number);
140 
141 	return SUCCESS;
142 }
143 /* }}} */
144 
145 /* {{{ xsl_object_get_data */
xsl_object_get_data(void * obj)146 zval *xsl_object_get_data(void *obj)
147 {
148 	zval *dom_wrapper;
149 	dom_wrapper = ((xsltStylesheetPtr) obj)->_private;
150 	return dom_wrapper;
151 }
152 /* }}} */
153 
154 /* {{{ xsl_object_set_data */
xsl_object_set_data(void * obj,zval * wrapper)155 static void xsl_object_set_data(void *obj, zval *wrapper)
156 {
157 	((xsltStylesheetPtr) obj)->_private = wrapper;
158 }
159 /* }}} */
160 
161 /* {{{ php_xsl_set_object */
php_xsl_set_object(zval * wrapper,void * obj)162 void php_xsl_set_object(zval *wrapper, void *obj)
163 {
164 	xsl_object *object;
165 
166 	object = Z_XSL_P(wrapper);
167 	object->ptr = obj;
168 	xsl_object_set_data(obj, wrapper);
169 }
170 /* }}} */
171 
172 /* {{{ php_xsl_create_object */
php_xsl_create_object(xsltStylesheetPtr obj,zval * wrapper_in,zval * return_value)173 void php_xsl_create_object(xsltStylesheetPtr obj, zval *wrapper_in, zval *return_value )
174 {
175 	zval *wrapper;
176 	zend_class_entry *ce;
177 
178 	if (!obj) {
179 		wrapper = wrapper_in;
180 		ZVAL_NULL(wrapper);
181 		return;
182 	}
183 
184 	if ((wrapper = xsl_object_get_data((void *) obj))) {
185 		ZVAL_COPY(wrapper, wrapper_in);
186 		return;
187 	}
188 
189 	if (!wrapper_in) {
190 		wrapper = return_value;
191 	} else {
192 		wrapper = wrapper_in;
193 	}
194 
195 
196 	ce = xsl_xsltprocessor_class_entry;
197 
198 	if (!wrapper_in) {
199 		object_init_ex(wrapper, ce);
200 	}
201 	php_xsl_set_object(wrapper, (void *) obj);
202 
203 	return;
204 }
205 /* }}} */
206 
207 /* {{{ PHP_MSHUTDOWN_FUNCTION */
PHP_MSHUTDOWN_FUNCTION(xsl)208 PHP_MSHUTDOWN_FUNCTION(xsl)
209 {
210 	xsltUnregisterExtModuleFunction ((const xmlChar *) "functionString",
211 				   (const xmlChar *) "http://php.net/xsl");
212 	xsltUnregisterExtModuleFunction ((const xmlChar *) "function",
213 				   (const xmlChar *) "http://php.net/xsl");
214 	xsltSetGenericErrorFunc(NULL, NULL);
215 	xsltCleanupGlobals();
216 
217 	return SUCCESS;
218 }
219 /* }}} */
220 
221 /* {{{ PHP_MINFO_FUNCTION */
PHP_MINFO_FUNCTION(xsl)222 PHP_MINFO_FUNCTION(xsl)
223 {
224 	php_info_print_table_start();
225 	{
226 		char buffer[128];
227 		int major, minor, subminor;
228 
229 		php_info_print_table_row(2, "XSL", "enabled");
230 		major = xsltLibxsltVersion/10000;
231 		minor = (xsltLibxsltVersion - major * 10000) / 100;
232 		subminor = (xsltLibxsltVersion - major * 10000 - minor * 100);
233 		snprintf(buffer, 128, "%d.%d.%d", major, minor, subminor);
234 		php_info_print_table_row(2, "libxslt Version", buffer);
235 		major = xsltLibxmlVersion/10000;
236 		minor = (xsltLibxmlVersion - major * 10000) / 100;
237 		subminor = (xsltLibxmlVersion - major * 10000 - minor * 100);
238 		snprintf(buffer, 128, "%d.%d.%d", major, minor, subminor);
239 		php_info_print_table_row(2, "libxslt compiled against libxml Version", buffer);
240 	}
241 #ifdef HAVE_XSL_EXSLT
242 	php_info_print_table_row(2, "EXSLT", "enabled");
243 	php_info_print_table_row(2, "libexslt Version", LIBEXSLT_DOTTED_VERSION);
244 #endif
245 	php_info_print_table_end();
246 }
247 /* }}} */
248