xref: /PHP-8.1/ext/xsl/php_xsl.c (revision 68aa7931)
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_LONG_CONSTANT("XSL_CLONE_AUTO",      0,     CONST_CS | CONST_PERSISTENT);
136 	REGISTER_LONG_CONSTANT("XSL_CLONE_NEVER",    -1,     CONST_CS | CONST_PERSISTENT);
137 	REGISTER_LONG_CONSTANT("XSL_CLONE_ALWAYS",    1,     CONST_CS | CONST_PERSISTENT);
138 
139 	REGISTER_LONG_CONSTANT("XSL_SECPREF_NONE",             XSL_SECPREF_NONE,             CONST_CS | CONST_PERSISTENT);
140 	REGISTER_LONG_CONSTANT("XSL_SECPREF_READ_FILE",        XSL_SECPREF_READ_FILE,        CONST_CS | CONST_PERSISTENT);
141 	REGISTER_LONG_CONSTANT("XSL_SECPREF_WRITE_FILE",       XSL_SECPREF_WRITE_FILE,       CONST_CS | CONST_PERSISTENT);
142 	REGISTER_LONG_CONSTANT("XSL_SECPREF_CREATE_DIRECTORY", XSL_SECPREF_CREATE_DIRECTORY, CONST_CS | CONST_PERSISTENT);
143 	REGISTER_LONG_CONSTANT("XSL_SECPREF_READ_NETWORK",     XSL_SECPREF_READ_NETWORK,     CONST_CS | CONST_PERSISTENT);
144 	REGISTER_LONG_CONSTANT("XSL_SECPREF_WRITE_NETWORK",    XSL_SECPREF_WRITE_NETWORK,    CONST_CS | CONST_PERSISTENT);
145 	REGISTER_LONG_CONSTANT("XSL_SECPREF_DEFAULT",          XSL_SECPREF_DEFAULT,          CONST_CS | CONST_PERSISTENT);
146 
147 	REGISTER_LONG_CONSTANT("LIBXSLT_VERSION",           LIBXSLT_VERSION,            CONST_CS | CONST_PERSISTENT);
148 	REGISTER_STRING_CONSTANT("LIBXSLT_DOTTED_VERSION",  LIBXSLT_DOTTED_VERSION,     CONST_CS | CONST_PERSISTENT);
149 
150 #ifdef HAVE_XSL_EXSLT
151 	REGISTER_LONG_CONSTANT("LIBEXSLT_VERSION",           LIBEXSLT_VERSION,            CONST_CS | CONST_PERSISTENT);
152 	REGISTER_STRING_CONSTANT("LIBEXSLT_DOTTED_VERSION",  LIBEXSLT_DOTTED_VERSION,     CONST_CS | CONST_PERSISTENT);
153 #endif
154 
155 	return SUCCESS;
156 }
157 /* }}} */
158 
159 /* {{{ xsl_object_get_data */
xsl_object_get_data(void * obj)160 zval *xsl_object_get_data(void *obj)
161 {
162 	zval *dom_wrapper;
163 	dom_wrapper = ((xsltStylesheetPtr) obj)->_private;
164 	return dom_wrapper;
165 }
166 /* }}} */
167 
168 /* {{{ xsl_object_set_data */
xsl_object_set_data(void * obj,zval * wrapper)169 static void xsl_object_set_data(void *obj, zval *wrapper)
170 {
171 	((xsltStylesheetPtr) obj)->_private = wrapper;
172 }
173 /* }}} */
174 
175 /* {{{ php_xsl_set_object */
php_xsl_set_object(zval * wrapper,void * obj)176 void php_xsl_set_object(zval *wrapper, void *obj)
177 {
178 	xsl_object *object;
179 
180 	object = Z_XSL_P(wrapper);
181 	object->ptr = obj;
182 	xsl_object_set_data(obj, wrapper);
183 }
184 /* }}} */
185 
186 /* {{{ php_xsl_create_object */
php_xsl_create_object(xsltStylesheetPtr obj,zval * wrapper_in,zval * return_value)187 void php_xsl_create_object(xsltStylesheetPtr obj, zval *wrapper_in, zval *return_value )
188 {
189 	zval *wrapper;
190 	zend_class_entry *ce;
191 
192 	if (!obj) {
193 		wrapper = wrapper_in;
194 		ZVAL_NULL(wrapper);
195 		return;
196 	}
197 
198 	if ((wrapper = xsl_object_get_data((void *) obj))) {
199 		ZVAL_COPY(wrapper, wrapper_in);
200 		return;
201 	}
202 
203 	if (!wrapper_in) {
204 		wrapper = return_value;
205 	} else {
206 		wrapper = wrapper_in;
207 	}
208 
209 
210 	ce = xsl_xsltprocessor_class_entry;
211 
212 	if (!wrapper_in) {
213 		object_init_ex(wrapper, ce);
214 	}
215 	php_xsl_set_object(wrapper, (void *) obj);
216 
217 	return;
218 }
219 /* }}} */
220 
221 /* {{{ PHP_MSHUTDOWN_FUNCTION */
PHP_MSHUTDOWN_FUNCTION(xsl)222 PHP_MSHUTDOWN_FUNCTION(xsl)
223 {
224 	xsltUnregisterExtModuleFunction ((const xmlChar *) "functionString",
225 				   (const xmlChar *) "http://php.net/xsl");
226 	xsltUnregisterExtModuleFunction ((const xmlChar *) "function",
227 				   (const xmlChar *) "http://php.net/xsl");
228 	xsltSetGenericErrorFunc(NULL, NULL);
229 	xsltCleanupGlobals();
230 
231 	return SUCCESS;
232 }
233 /* }}} */
234 
235 /* {{{ PHP_MINFO_FUNCTION */
PHP_MINFO_FUNCTION(xsl)236 PHP_MINFO_FUNCTION(xsl)
237 {
238 	php_info_print_table_start();
239 	{
240 		char buffer[128];
241 		int major, minor, subminor;
242 
243 		php_info_print_table_row(2, "XSL", "enabled");
244 		major = xsltLibxsltVersion/10000;
245 		minor = (xsltLibxsltVersion - major * 10000) / 100;
246 		subminor = (xsltLibxsltVersion - major * 10000 - minor * 100);
247 		snprintf(buffer, 128, "%d.%d.%d", major, minor, subminor);
248 		php_info_print_table_row(2, "libxslt Version", buffer);
249 		major = xsltLibxmlVersion/10000;
250 		minor = (xsltLibxmlVersion - major * 10000) / 100;
251 		subminor = (xsltLibxmlVersion - major * 10000 - minor * 100);
252 		snprintf(buffer, 128, "%d.%d.%d", major, minor, subminor);
253 		php_info_print_table_row(2, "libxslt compiled against libxml Version", buffer);
254 	}
255 #ifdef HAVE_XSL_EXSLT
256 	php_info_print_table_row(2, "EXSLT", "enabled");
257 	php_info_print_table_row(2, "libexslt Version", LIBEXSLT_DOTTED_VERSION);
258 #endif
259 	php_info_print_table_end();
260 }
261 /* }}} */
262