xref: /PHP-7.4/ext/xsl/php_xsl.c (revision 92ac598a)
1 /*
2   +----------------------------------------------------------------------+
3   | PHP Version 7                                                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) The PHP Group                                          |
6   +----------------------------------------------------------------------+
7   | This source file is subject to version 3.01 of the PHP license,      |
8   | that is bundled with this package in the file LICENSE, and is        |
9   | available through the world-wide-web at the following url:           |
10   | http://www.php.net/license/3_01.txt                                  |
11   | If you did not receive a copy of the PHP license and are unable to   |
12   | obtain it through the world-wide-web, please send a note to          |
13   | license@php.net so we can mail you a copy immediately.               |
14   +----------------------------------------------------------------------+
15   | Author: Christian Stocker <chregu@php.net>                           |
16   +----------------------------------------------------------------------+
17 */
18 
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22 
23 #include "php.h"
24 #include "php_ini.h"
25 #include "ext/standard/info.h"
26 #include "php_xsl.h"
27 
28 zend_class_entry *xsl_xsltprocessor_class_entry;
29 static zend_object_handlers xsl_object_handlers;
30 
31 /* {{{ xsl_functions[]
32  *
33  * Every user visible function must have an entry in xsl_functions[].
34  */
35 const zend_function_entry xsl_functions[] = {
36 	PHP_FE_END
37 };
38 /* }}} */
39 
40 static const zend_module_dep xsl_deps[] = {
41 	ZEND_MOD_REQUIRED("libxml")
42 	ZEND_MOD_END
43 };
44 
45 /* {{{ xsl_module_entry
46  */
47 zend_module_entry xsl_module_entry = {
48 	STANDARD_MODULE_HEADER_EX, NULL,
49 	xsl_deps,
50 	"xsl",
51 	xsl_functions,
52 	PHP_MINIT(xsl),
53 	PHP_MSHUTDOWN(xsl),
54 	NULL,
55 	NULL,
56 	PHP_MINFO(xsl),
57 	PHP_XSL_VERSION,
58 	STANDARD_MODULE_PROPERTIES
59 };
60 /* }}} */
61 
62 #ifdef COMPILE_DL_XSL
ZEND_GET_MODULE(xsl)63 ZEND_GET_MODULE(xsl)
64 #endif
65 
66 /* {{{ xsl_objects_free_storage */
67 void xsl_objects_free_storage(zend_object *object)
68 {
69 	xsl_object *intern = php_xsl_fetch_object(object);
70 
71 	zend_object_std_dtor(&intern->std);
72 
73 	zend_hash_destroy(intern->parameter);
74 	FREE_HASHTABLE(intern->parameter);
75 
76 	zend_hash_destroy(intern->registered_phpfunctions);
77 	FREE_HASHTABLE(intern->registered_phpfunctions);
78 
79 	if (intern->node_list) {
80 		zend_hash_destroy(intern->node_list);
81 		FREE_HASHTABLE(intern->node_list);
82 	}
83 
84 	if (intern->doc) {
85 		php_libxml_decrement_doc_ref(intern->doc);
86 		efree(intern->doc);
87 	}
88 
89 	if (intern->ptr) {
90 		/* free wrapper */
91 		if (((xsltStylesheetPtr) intern->ptr)->_private != NULL) {
92 			((xsltStylesheetPtr) intern->ptr)->_private = NULL;
93 		}
94 
95 		xsltFreeStylesheet((xsltStylesheetPtr) intern->ptr);
96 		intern->ptr = NULL;
97 	}
98 	if (intern->profiling) {
99 		efree(intern->profiling);
100 	}
101 }
102 /* }}} */
103 
104 /* {{{ xsl_objects_new */
xsl_objects_new(zend_class_entry * class_type)105 zend_object *xsl_objects_new(zend_class_entry *class_type)
106 {
107 	xsl_object *intern;
108 
109 	intern = zend_object_alloc(sizeof(xsl_object), class_type);
110 	intern->securityPrefs = XSL_SECPREF_DEFAULT;
111 
112 	zend_object_std_init(&intern->std, class_type);
113 	object_properties_init(&intern->std, class_type);
114 	intern->parameter = zend_new_array(0);
115 	intern->registered_phpfunctions = zend_new_array(0);
116 
117 	intern->std.handlers = &xsl_object_handlers;
118 	return &intern->std;
119 }
120 /* }}} */
121 
122 /* {{{ PHP_MINIT_FUNCTION
123  */
PHP_MINIT_FUNCTION(xsl)124 PHP_MINIT_FUNCTION(xsl)
125 {
126 
127 	zend_class_entry ce;
128 
129 	memcpy(&xsl_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
130 	xsl_object_handlers.offset = XtOffsetOf(xsl_object, std);
131 	xsl_object_handlers.clone_obj = NULL;
132 	xsl_object_handlers.free_obj = xsl_objects_free_storage;
133 
134 	REGISTER_XSL_CLASS(ce, "XSLTProcessor", NULL, php_xsl_xsltprocessor_class_functions, xsl_xsltprocessor_class_entry);
135 #if HAVE_XSL_EXSLT
136 	exsltRegisterAll();
137 #endif
138 
139 	xsltRegisterExtModuleFunction ((const xmlChar *) "functionString",
140 				   (const xmlChar *) "http://php.net/xsl",
141 				   xsl_ext_function_string_php);
142 	xsltRegisterExtModuleFunction ((const xmlChar *) "function",
143 				   (const xmlChar *) "http://php.net/xsl",
144 				   xsl_ext_function_object_php);
145 	xsltSetGenericErrorFunc(NULL, php_libxml_error_handler);
146 
147 	REGISTER_LONG_CONSTANT("XSL_CLONE_AUTO",      0,     CONST_CS | CONST_PERSISTENT);
148 	REGISTER_LONG_CONSTANT("XSL_CLONE_NEVER",    -1,     CONST_CS | CONST_PERSISTENT);
149 	REGISTER_LONG_CONSTANT("XSL_CLONE_ALWAYS",    1,     CONST_CS | CONST_PERSISTENT);
150 
151 	REGISTER_LONG_CONSTANT("XSL_SECPREF_NONE",             XSL_SECPREF_NONE,             CONST_CS | CONST_PERSISTENT);
152 	REGISTER_LONG_CONSTANT("XSL_SECPREF_READ_FILE",        XSL_SECPREF_READ_FILE,        CONST_CS | CONST_PERSISTENT);
153 	REGISTER_LONG_CONSTANT("XSL_SECPREF_WRITE_FILE",       XSL_SECPREF_WRITE_FILE,       CONST_CS | CONST_PERSISTENT);
154 	REGISTER_LONG_CONSTANT("XSL_SECPREF_CREATE_DIRECTORY", XSL_SECPREF_CREATE_DIRECTORY, CONST_CS | CONST_PERSISTENT);
155 	REGISTER_LONG_CONSTANT("XSL_SECPREF_READ_NETWORK",     XSL_SECPREF_READ_NETWORK,     CONST_CS | CONST_PERSISTENT);
156 	REGISTER_LONG_CONSTANT("XSL_SECPREF_WRITE_NETWORK",    XSL_SECPREF_WRITE_NETWORK,    CONST_CS | CONST_PERSISTENT);
157 	REGISTER_LONG_CONSTANT("XSL_SECPREF_DEFAULT",          XSL_SECPREF_DEFAULT,          CONST_CS | CONST_PERSISTENT);
158 
159 	REGISTER_LONG_CONSTANT("LIBXSLT_VERSION",           LIBXSLT_VERSION,            CONST_CS | CONST_PERSISTENT);
160 	REGISTER_STRING_CONSTANT("LIBXSLT_DOTTED_VERSION",  LIBXSLT_DOTTED_VERSION,     CONST_CS | CONST_PERSISTENT);
161 
162 #if HAVE_XSL_EXSLT
163 	REGISTER_LONG_CONSTANT("LIBEXSLT_VERSION",           LIBEXSLT_VERSION,            CONST_CS | CONST_PERSISTENT);
164 	REGISTER_STRING_CONSTANT("LIBEXSLT_DOTTED_VERSION",  LIBEXSLT_DOTTED_VERSION,     CONST_CS | CONST_PERSISTENT);
165 #endif
166 
167 	return SUCCESS;
168 }
169 /* }}} */
170 
171 /* {{{ xsl_object_get_data */
xsl_object_get_data(void * obj)172 zval *xsl_object_get_data(void *obj)
173 {
174 	zval *dom_wrapper;
175 	dom_wrapper = ((xsltStylesheetPtr) obj)->_private;
176 	return dom_wrapper;
177 }
178 /* }}} */
179 
180 /* {{{ xsl_object_set_data */
xsl_object_set_data(void * obj,zval * wrapper)181 static void xsl_object_set_data(void *obj, zval *wrapper)
182 {
183 	((xsltStylesheetPtr) obj)->_private = wrapper;
184 }
185 /* }}} */
186 
187 /* {{{ php_xsl_set_object */
php_xsl_set_object(zval * wrapper,void * obj)188 void php_xsl_set_object(zval *wrapper, void *obj)
189 {
190 	xsl_object *object;
191 
192 	object = Z_XSL_P(wrapper);
193 	object->ptr = obj;
194 	xsl_object_set_data(obj, wrapper);
195 }
196 /* }}} */
197 
198 /* {{{ php_xsl_create_object */
php_xsl_create_object(xsltStylesheetPtr obj,zval * wrapper_in,zval * return_value)199 void php_xsl_create_object(xsltStylesheetPtr obj, zval *wrapper_in, zval *return_value )
200 {
201 	zval *wrapper;
202 	zend_class_entry *ce;
203 
204 	if (!obj) {
205 		wrapper = wrapper_in;
206 		ZVAL_NULL(wrapper);
207 		return;
208 	}
209 
210 	if ((wrapper = xsl_object_get_data((void *) obj))) {
211 		ZVAL_COPY(wrapper, wrapper_in);
212 		return;
213 	}
214 
215 	if (!wrapper_in) {
216 		wrapper = return_value;
217 	} else {
218 		wrapper = wrapper_in;
219 	}
220 
221 
222 	ce = xsl_xsltprocessor_class_entry;
223 
224 	if (!wrapper_in) {
225 		object_init_ex(wrapper, ce);
226 	}
227 	php_xsl_set_object(wrapper, (void *) obj);
228 
229 	return;
230 }
231 /* }}} */
232 
233 /* {{{ PHP_MSHUTDOWN_FUNCTION
234  */
PHP_MSHUTDOWN_FUNCTION(xsl)235 PHP_MSHUTDOWN_FUNCTION(xsl)
236 {
237 	xsltUnregisterExtModuleFunction ((const xmlChar *) "functionString",
238 				   (const xmlChar *) "http://php.net/xsl");
239 	xsltUnregisterExtModuleFunction ((const xmlChar *) "function",
240 				   (const xmlChar *) "http://php.net/xsl");
241 	xsltSetGenericErrorFunc(NULL, NULL);
242 	xsltCleanupGlobals();
243 
244 	return SUCCESS;
245 }
246 /* }}} */
247 
248 /* {{{ PHP_MINFO_FUNCTION
249  */
PHP_MINFO_FUNCTION(xsl)250 PHP_MINFO_FUNCTION(xsl)
251 {
252 	php_info_print_table_start();
253 	{
254 		char buffer[128];
255 		int major, minor, subminor;
256 
257 		php_info_print_table_row(2, "XSL", "enabled");
258 		major = xsltLibxsltVersion/10000;
259 		minor = (xsltLibxsltVersion - major * 10000) / 100;
260 		subminor = (xsltLibxsltVersion - major * 10000 - minor * 100);
261 		snprintf(buffer, 128, "%d.%d.%d", major, minor, subminor);
262 		php_info_print_table_row(2, "libxslt Version", buffer);
263 		major = xsltLibxmlVersion/10000;
264 		minor = (xsltLibxmlVersion - major * 10000) / 100;
265 		subminor = (xsltLibxmlVersion - major * 10000 - minor * 100);
266 		snprintf(buffer, 128, "%d.%d.%d", major, minor, subminor);
267 		php_info_print_table_row(2, "libxslt compiled against libxml Version", buffer);
268 	}
269 #if HAVE_XSL_EXSLT
270 	php_info_print_table_row(2, "EXSLT", "enabled");
271 	php_info_print_table_row(2, "libexslt Version", LIBEXSLT_DOTTED_VERSION);
272 #endif
273 	php_info_print_table_end();
274 }
275 /* }}} */
276