xref: /PHP-7.0/ext/xsl/php_xsl.c (revision 478f119a)
1 /*
2   +----------------------------------------------------------------------+
3   | PHP Version 7                                                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 1997-2017 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 /* $Id$ */
20 
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24 
25 #include "php.h"
26 #include "php_ini.h"
27 #include "ext/standard/info.h"
28 #include "php_xsl.h"
29 
30 zend_class_entry *xsl_xsltprocessor_class_entry;
31 static zend_object_handlers xsl_object_handlers;
32 
33 /* {{{ xsl_functions[]
34  *
35  * Every user visible function must have an entry in xsl_functions[].
36  */
37 const zend_function_entry xsl_functions[] = {
38 	PHP_FE_END
39 };
40 /* }}} */
41 
42 static const zend_module_dep xsl_deps[] = {
43 	ZEND_MOD_REQUIRED("libxml")
44 	ZEND_MOD_END
45 };
46 
47 /* {{{ xsl_module_entry
48  */
49 zend_module_entry xsl_module_entry = {
50 	STANDARD_MODULE_HEADER_EX, NULL,
51 	xsl_deps,
52 	"xsl",
53 	xsl_functions,
54 	PHP_MINIT(xsl),
55 	PHP_MSHUTDOWN(xsl),
56 	NULL,
57 	NULL,
58 	PHP_MINFO(xsl),
59 	PHP_XSL_VERSION,
60 	STANDARD_MODULE_PROPERTIES
61 };
62 /* }}} */
63 
64 #ifdef COMPILE_DL_XSL
ZEND_GET_MODULE(xsl)65 ZEND_GET_MODULE(xsl)
66 #endif
67 
68 /* {{{ xsl_objects_free_storage */
69 void xsl_objects_free_storage(zend_object *object)
70 {
71 	xsl_object *intern = php_xsl_fetch_object(object);
72 
73 	zend_object_std_dtor(&intern->std);
74 
75 	zend_hash_destroy(intern->parameter);
76 	FREE_HASHTABLE(intern->parameter);
77 
78 	zend_hash_destroy(intern->registered_phpfunctions);
79 	FREE_HASHTABLE(intern->registered_phpfunctions);
80 
81 	if (intern->node_list) {
82 		zend_hash_destroy(intern->node_list);
83 		FREE_HASHTABLE(intern->node_list);
84 	}
85 
86 	if (intern->doc) {
87 		php_libxml_decrement_doc_ref(intern->doc);
88 		efree(intern->doc);
89 	}
90 
91 	if (intern->ptr) {
92 		/* free wrapper */
93 		if (((xsltStylesheetPtr) intern->ptr)->_private != NULL) {
94 			((xsltStylesheetPtr) intern->ptr)->_private = NULL;
95 		}
96 
97 		xsltFreeStylesheet((xsltStylesheetPtr) intern->ptr);
98 		intern->ptr = NULL;
99 	}
100 	if (intern->profiling) {
101 		efree(intern->profiling);
102 	}
103 }
104 /* }}} */
105 
106 /* {{{ xsl_objects_new */
xsl_objects_new(zend_class_entry * class_type)107 zend_object *xsl_objects_new(zend_class_entry *class_type)
108 {
109 	xsl_object *intern;
110 
111 	intern = ecalloc(1, sizeof(xsl_object) + zend_object_properties_size(class_type));
112 	intern->securityPrefs = XSL_SECPREF_DEFAULT;
113 
114 	zend_object_std_init(&intern->std, class_type);
115 	object_properties_init(&intern->std, class_type);
116 	ALLOC_HASHTABLE(intern->parameter);
117 	zend_hash_init(intern->parameter, 0, NULL, ZVAL_PTR_DTOR, 0);
118 	ALLOC_HASHTABLE(intern->registered_phpfunctions);
119 	zend_hash_init(intern->registered_phpfunctions, 0, NULL, ZVAL_PTR_DTOR, 0);
120 
121 	intern->std.handlers = &xsl_object_handlers;
122 	return &intern->std;
123 }
124 /* }}} */
125 
126 /* {{{ PHP_MINIT_FUNCTION
127  */
PHP_MINIT_FUNCTION(xsl)128 PHP_MINIT_FUNCTION(xsl)
129 {
130 
131 	zend_class_entry ce;
132 
133 	memcpy(&xsl_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
134 	xsl_object_handlers.offset = XtOffsetOf(xsl_object, std);
135 	xsl_object_handlers.clone_obj = NULL;
136 	xsl_object_handlers.free_obj = xsl_objects_free_storage;
137 
138 	REGISTER_XSL_CLASS(ce, "XSLTProcessor", NULL, php_xsl_xsltprocessor_class_functions, xsl_xsltprocessor_class_entry);
139 #if HAVE_XSL_EXSLT
140 	exsltRegisterAll();
141 #endif
142 
143 	xsltRegisterExtModuleFunction ((const xmlChar *) "functionString",
144 				   (const xmlChar *) "http://php.net/xsl",
145 				   xsl_ext_function_string_php);
146 	xsltRegisterExtModuleFunction ((const xmlChar *) "function",
147 				   (const xmlChar *) "http://php.net/xsl",
148 				   xsl_ext_function_object_php);
149 	xsltSetGenericErrorFunc(NULL, php_libxml_error_handler);
150 
151 	REGISTER_LONG_CONSTANT("XSL_CLONE_AUTO",      0,     CONST_CS | CONST_PERSISTENT);
152 	REGISTER_LONG_CONSTANT("XSL_CLONE_NEVER",    -1,     CONST_CS | CONST_PERSISTENT);
153 	REGISTER_LONG_CONSTANT("XSL_CLONE_ALWAYS",    1,     CONST_CS | CONST_PERSISTENT);
154 
155 	REGISTER_LONG_CONSTANT("XSL_SECPREF_NONE",             XSL_SECPREF_NONE,             CONST_CS | CONST_PERSISTENT);
156 	REGISTER_LONG_CONSTANT("XSL_SECPREF_READ_FILE",        XSL_SECPREF_READ_FILE,        CONST_CS | CONST_PERSISTENT);
157 	REGISTER_LONG_CONSTANT("XSL_SECPREF_WRITE_FILE",       XSL_SECPREF_WRITE_FILE,       CONST_CS | CONST_PERSISTENT);
158 	REGISTER_LONG_CONSTANT("XSL_SECPREF_CREATE_DIRECTORY", XSL_SECPREF_CREATE_DIRECTORY, CONST_CS | CONST_PERSISTENT);
159 	REGISTER_LONG_CONSTANT("XSL_SECPREF_READ_NETWORK",     XSL_SECPREF_READ_NETWORK,     CONST_CS | CONST_PERSISTENT);
160 	REGISTER_LONG_CONSTANT("XSL_SECPREF_WRITE_NETWORK",    XSL_SECPREF_WRITE_NETWORK,    CONST_CS | CONST_PERSISTENT);
161 	REGISTER_LONG_CONSTANT("XSL_SECPREF_DEFAULT",          XSL_SECPREF_DEFAULT,          CONST_CS | CONST_PERSISTENT);
162 
163 	REGISTER_LONG_CONSTANT("LIBXSLT_VERSION",           LIBXSLT_VERSION,            CONST_CS | CONST_PERSISTENT);
164 	REGISTER_STRING_CONSTANT("LIBXSLT_DOTTED_VERSION",  LIBXSLT_DOTTED_VERSION,     CONST_CS | CONST_PERSISTENT);
165 
166 #if HAVE_XSL_EXSLT
167 	REGISTER_LONG_CONSTANT("LIBEXSLT_VERSION",           LIBEXSLT_VERSION,            CONST_CS | CONST_PERSISTENT);
168 	REGISTER_STRING_CONSTANT("LIBEXSLT_DOTTED_VERSION",  LIBEXSLT_DOTTED_VERSION,     CONST_CS | CONST_PERSISTENT);
169 #endif
170 
171 	return SUCCESS;
172 }
173 /* }}} */
174 
175 /* {{{ xsl_object_get_data */
xsl_object_get_data(void * obj)176 zval *xsl_object_get_data(void *obj)
177 {
178 	zval *dom_wrapper;
179 	dom_wrapper = ((xsltStylesheetPtr) obj)->_private;
180 	return dom_wrapper;
181 }
182 /* }}} */
183 
184 /* {{{ xsl_object_set_data */
xsl_object_set_data(void * obj,zval * wrapper)185 static void xsl_object_set_data(void *obj, zval *wrapper)
186 {
187 	((xsltStylesheetPtr) obj)->_private = wrapper;
188 }
189 /* }}} */
190 
191 /* {{{ php_xsl_set_object */
php_xsl_set_object(zval * wrapper,void * obj)192 void php_xsl_set_object(zval *wrapper, void *obj)
193 {
194 	xsl_object *object;
195 
196 	object = Z_XSL_P(wrapper);
197 	object->ptr = obj;
198 	xsl_object_set_data(obj, wrapper);
199 }
200 /* }}} */
201 
202 /* {{{ php_xsl_create_object */
php_xsl_create_object(xsltStylesheetPtr obj,zval * wrapper_in,zval * return_value)203 void php_xsl_create_object(xsltStylesheetPtr obj, zval *wrapper_in, zval *return_value )
204 {
205 	zval *wrapper;
206 	zend_class_entry *ce;
207 
208 	if (!obj) {
209 		wrapper = wrapper_in;
210 		ZVAL_NULL(wrapper);
211 		return;
212 	}
213 
214 	if ((wrapper = xsl_object_get_data((void *) obj))) {
215 		ZVAL_COPY(wrapper, wrapper_in);
216 		return;
217 	}
218 
219 	if (!wrapper_in) {
220 		wrapper = return_value;
221 	} else {
222 		wrapper = wrapper_in;
223 	}
224 
225 
226 	ce = xsl_xsltprocessor_class_entry;
227 
228 	if (!wrapper_in) {
229 		object_init_ex(wrapper, ce);
230 	}
231 	php_xsl_set_object(wrapper, (void *) obj);
232 
233 	return;
234 }
235 /* }}} */
236 
237 /* {{{ PHP_MSHUTDOWN_FUNCTION
238  */
PHP_MSHUTDOWN_FUNCTION(xsl)239 PHP_MSHUTDOWN_FUNCTION(xsl)
240 {
241 	xsltUnregisterExtModuleFunction ((const xmlChar *) "functionString",
242 				   (const xmlChar *) "http://php.net/xsl");
243 	xsltUnregisterExtModuleFunction ((const xmlChar *) "function",
244 				   (const xmlChar *) "http://php.net/xsl");
245 	xsltSetGenericErrorFunc(NULL, NULL);
246 	xsltCleanupGlobals();
247 
248 	return SUCCESS;
249 }
250 /* }}} */
251 
252 /* {{{ PHP_MINFO_FUNCTION
253  */
PHP_MINFO_FUNCTION(xsl)254 PHP_MINFO_FUNCTION(xsl)
255 {
256 	php_info_print_table_start();
257 	{
258 		char buffer[128];
259 		int major, minor, subminor;
260 
261 		php_info_print_table_row(2, "XSL", "enabled");
262 		major = xsltLibxsltVersion/10000;
263 		minor = (xsltLibxsltVersion - major * 10000) / 100;
264 		subminor = (xsltLibxsltVersion - major * 10000 - minor * 100);
265 		snprintf(buffer, 128, "%d.%d.%d", major, minor, subminor);
266 		php_info_print_table_row(2, "libxslt Version", buffer);
267 		major = xsltLibxmlVersion/10000;
268 		minor = (xsltLibxmlVersion - major * 10000) / 100;
269 		subminor = (xsltLibxmlVersion - major * 10000 - minor * 100);
270 		snprintf(buffer, 128, "%d.%d.%d", major, minor, subminor);
271 		php_info_print_table_row(2, "libxslt compiled against libxml Version", buffer);
272 	}
273 #if HAVE_XSL_EXSLT
274 	php_info_print_table_row(2, "EXSLT", "enabled");
275 	php_info_print_table_row(2, "libexslt Version", LIBEXSLT_DOTTED_VERSION);
276 #endif
277 	php_info_print_table_end();
278 }
279 /* }}} */
280 
281 /*
282  * Local variables:
283  * tab-width: 4
284  * c-basic-offset: 4
285  * End:
286  * vim600: noet sw=4 ts=4 fdm=marker
287  * vim<600: noet sw=4 ts=4
288  */
289