php_xsl.c 7.7 KB

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