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