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