php_intl.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. +----------------------------------------------------------------------+
  3. | This source file is subject to version 3.01 of the PHP license, |
  4. | that is bundled with this package in the file LICENSE, and is |
  5. | available through the world-wide-web at the following url: |
  6. | https://www.php.net/license/3_01.txt |
  7. | If you did not receive a copy of the PHP license and are unable to |
  8. | obtain it through the world-wide-web, please send a note to |
  9. | license@php.net so we can mail you a copy immediately. |
  10. +----------------------------------------------------------------------+
  11. | Authors: Vadim Savchuk <vsavchuk@productengine.com> |
  12. | Dmitry Lakhtyuk <dlakhtyuk@productengine.com> |
  13. | Stanislav Malyshev <stas@zend.com> |
  14. | Kirti Velankar <kirtig@yahoo-inc.com> |
  15. +----------------------------------------------------------------------+
  16. */
  17. #ifdef HAVE_CONFIG_H
  18. #include "config.h"
  19. #endif
  20. #include "php_intl.h"
  21. #include "php_intl_arginfo.h"
  22. #include "intl_error.h"
  23. #include "collator/collator_class.h"
  24. #include "collator/collator.h"
  25. #include "collator/collator_sort.h"
  26. #include "collator/collator_convert.h"
  27. #include "converter/converter.h"
  28. #include "formatter/formatter.h"
  29. #include "formatter/formatter_class.h"
  30. #include "formatter/formatter_format.h"
  31. #include "grapheme/grapheme.h"
  32. #include "msgformat/msgformat_class.h"
  33. #include "normalizer/normalizer.h"
  34. #include "normalizer/normalizer_class.h"
  35. #include "locale/locale.h"
  36. #include "locale/locale_class.h"
  37. #include "dateformat/dateformat.h"
  38. #include "dateformat/dateformat_class.h"
  39. #include "dateformat/dateformat_data.h"
  40. #include "dateformat/datepatterngenerator_class.h"
  41. #include "resourcebundle/resourcebundle_class.h"
  42. #include "transliterator/transliterator.h"
  43. #include "transliterator/transliterator_class.h"
  44. #include "timezone/timezone_class.h"
  45. #include "calendar/calendar_class.h"
  46. #include "breakiterator/breakiterator_class.h"
  47. #include "breakiterator/breakiterator_iterators.h"
  48. #include "idn/idn.h"
  49. #include "uchar/uchar.h"
  50. # include "spoofchecker/spoofchecker_class.h"
  51. # include "spoofchecker/spoofchecker.h"
  52. #include "common/common_error.h"
  53. #include "common/common_enum.h"
  54. #include <unicode/uloc.h>
  55. #include <unicode/uclean.h>
  56. #include <ext/standard/info.h>
  57. #include "php_ini.h"
  58. /*
  59. * locale_get_default has a conflict since ICU also has
  60. * a function with the same name
  61. * in fact ICU appends the version no. to it also
  62. * Hence the following undef for ICU version
  63. * Same true for the locale_set_default function
  64. */
  65. #undef locale_get_default
  66. #undef locale_set_default
  67. ZEND_DECLARE_MODULE_GLOBALS( intl )
  68. const char *intl_locale_get_default( void )
  69. {
  70. if( INTL_G(default_locale) == NULL ) {
  71. return uloc_getDefault();
  72. }
  73. return INTL_G(default_locale);
  74. }
  75. /* {{{ INI Settings */
  76. PHP_INI_BEGIN()
  77. STD_PHP_INI_ENTRY(LOCALE_INI_NAME, NULL, PHP_INI_ALL, OnUpdateStringUnempty, default_locale, zend_intl_globals, intl_globals)
  78. STD_PHP_INI_ENTRY("intl.error_level", "0", PHP_INI_ALL, OnUpdateLong, error_level, zend_intl_globals, intl_globals)
  79. STD_PHP_INI_BOOLEAN("intl.use_exceptions", "0", PHP_INI_ALL, OnUpdateBool, use_exceptions, zend_intl_globals, intl_globals)
  80. PHP_INI_END()
  81. /* }}} */
  82. static PHP_GINIT_FUNCTION(intl);
  83. /* {{{ intl_module_entry */
  84. zend_module_entry intl_module_entry = {
  85. STANDARD_MODULE_HEADER,
  86. "intl",
  87. ext_functions,
  88. PHP_MINIT( intl ),
  89. PHP_MSHUTDOWN( intl ),
  90. PHP_RINIT( intl ),
  91. PHP_RSHUTDOWN( intl ),
  92. PHP_MINFO( intl ),
  93. PHP_INTL_VERSION,
  94. PHP_MODULE_GLOBALS(intl), /* globals descriptor */
  95. PHP_GINIT(intl), /* globals ctor */
  96. NULL, /* globals dtor */
  97. NULL, /* post deactivate */
  98. STANDARD_MODULE_PROPERTIES_EX
  99. };
  100. /* }}} */
  101. #ifdef COMPILE_DL_INTL
  102. #ifdef ZTS
  103. ZEND_TSRMLS_CACHE_DEFINE()
  104. #endif
  105. ZEND_GET_MODULE( intl )
  106. #endif
  107. /* {{{ intl_init_globals */
  108. static PHP_GINIT_FUNCTION(intl)
  109. {
  110. #if defined(COMPILE_DL_INTL) && defined(ZTS)
  111. ZEND_TSRMLS_CACHE_UPDATE();
  112. #endif
  113. memset( intl_globals, 0, sizeof(zend_intl_globals) );
  114. }
  115. /* }}} */
  116. /* {{{ PHP_MINIT_FUNCTION */
  117. PHP_MINIT_FUNCTION( intl )
  118. {
  119. /* For the default locale php.ini setting */
  120. REGISTER_INI_ENTRIES();
  121. REGISTER_LONG_CONSTANT("INTL_MAX_LOCALE_LEN", INTL_MAX_LOCALE_LEN, CONST_PERSISTENT | CONST_CS);
  122. REGISTER_STRING_CONSTANT("INTL_ICU_VERSION", U_ICU_VERSION, CONST_PERSISTENT | CONST_CS);
  123. #ifdef U_ICU_DATA_VERSION
  124. REGISTER_STRING_CONSTANT("INTL_ICU_DATA_VERSION", U_ICU_DATA_VERSION, CONST_PERSISTENT | CONST_CS);
  125. #endif
  126. /* Register 'Collator' PHP class */
  127. collator_register_Collator_class( );
  128. /* Expose Collator constants to PHP scripts */
  129. collator_register_constants( INIT_FUNC_ARGS_PASSTHRU );
  130. /* Register 'NumberFormatter' PHP class */
  131. formatter_register_class( );
  132. /* Expose NumberFormatter constants to PHP scripts */
  133. formatter_register_constants( INIT_FUNC_ARGS_PASSTHRU );
  134. /* Register 'Normalizer' PHP class */
  135. normalizer_register_Normalizer_class( );
  136. /* Expose Normalizer constants to PHP scripts */
  137. normalizer_register_constants( INIT_FUNC_ARGS_PASSTHRU );
  138. /* Register 'Locale' PHP class */
  139. locale_register_Locale_class( );
  140. /* Expose Locale constants to PHP scripts */
  141. locale_register_constants( INIT_FUNC_ARGS_PASSTHRU );
  142. msgformat_register_class();
  143. grapheme_register_constants( INIT_FUNC_ARGS_PASSTHRU );
  144. /* Register 'DateFormat' PHP class */
  145. dateformat_register_IntlDateFormatter_class( );
  146. /* Expose DateFormat constants to PHP scripts */
  147. dateformat_register_constants( INIT_FUNC_ARGS_PASSTHRU );
  148. /* Register 'IntlDateTimeFormatter' PHP class */
  149. dateformat_register_IntlDatePatternGenerator_class( );
  150. /* Register 'ResourceBundle' PHP class */
  151. resourcebundle_register_class( );
  152. /* Register 'Transliterator' PHP class */
  153. transliterator_register_Transliterator_class( );
  154. /* Register Transliterator constants */
  155. transliterator_register_constants( INIT_FUNC_ARGS_PASSTHRU );
  156. /* Register 'IntlTimeZone' PHP class */
  157. timezone_register_IntlTimeZone_class( );
  158. /* Register 'IntlCalendar' PHP class */
  159. calendar_register_IntlCalendar_class( );
  160. /* Expose ICU error codes to PHP scripts. */
  161. intl_expose_icu_error_codes( INIT_FUNC_ARGS_PASSTHRU );
  162. /* Expose IDN constants to PHP scripts. */
  163. idn_register_constants(INIT_FUNC_ARGS_PASSTHRU);
  164. /* Register 'Spoofchecker' PHP class */
  165. spoofchecker_register_Spoofchecker_class( );
  166. /* Expose Spoofchecker constants to PHP scripts */
  167. spoofchecker_register_constants( INIT_FUNC_ARGS_PASSTHRU );
  168. /* Register 'IntlException' PHP class */
  169. IntlException_ce_ptr = register_class_IntlException(zend_ce_exception);
  170. IntlException_ce_ptr->create_object = zend_ce_exception->create_object;
  171. /* Register 'IntlIterator' PHP class */
  172. intl_register_IntlIterator_class( );
  173. /* Register 'BreakIterator' class */
  174. breakiterator_register_BreakIterator_class( );
  175. /* Register 'IntlPartsIterator' class */
  176. breakiterator_register_IntlPartsIterator_class();
  177. /* Global error handling. */
  178. intl_error_init( NULL );
  179. /* 'Converter' class for codepage conversions */
  180. php_converter_minit(INIT_FUNC_ARGS_PASSTHRU);
  181. /* IntlChar class */
  182. php_uchar_minit(INIT_FUNC_ARGS_PASSTHRU);
  183. return SUCCESS;
  184. }
  185. /* }}} */
  186. #define EXPLICIT_CLEANUP_ENV_VAR "INTL_EXPLICIT_CLEANUP"
  187. /* {{{ PHP_MSHUTDOWN_FUNCTION */
  188. PHP_MSHUTDOWN_FUNCTION( intl )
  189. {
  190. const char *cleanup;
  191. /* For the default locale php.ini setting */
  192. UNREGISTER_INI_ENTRIES();
  193. cleanup = getenv(EXPLICIT_CLEANUP_ENV_VAR);
  194. if (cleanup != NULL && !(cleanup[0] == '0' && cleanup[1] == '\0')) {
  195. u_cleanup();
  196. }
  197. return SUCCESS;
  198. }
  199. /* }}} */
  200. /* {{{ PHP_RINIT_FUNCTION */
  201. PHP_RINIT_FUNCTION( intl )
  202. {
  203. return SUCCESS;
  204. }
  205. /* }}} */
  206. /* {{{ PHP_RSHUTDOWN_FUNCTION */
  207. PHP_RSHUTDOWN_FUNCTION( intl )
  208. {
  209. INTL_G(current_collator) = NULL;
  210. if (INTL_G(grapheme_iterator)) {
  211. grapheme_close_global_iterator( );
  212. INTL_G(grapheme_iterator) = NULL;
  213. }
  214. intl_error_reset( NULL);
  215. return SUCCESS;
  216. }
  217. /* }}} */
  218. /* {{{ PHP_MINFO_FUNCTION */
  219. PHP_MINFO_FUNCTION( intl )
  220. {
  221. #ifndef UCONFIG_NO_FORMATTING
  222. UErrorCode status = U_ZERO_ERROR;
  223. const char *tzdata_ver = NULL;
  224. #endif
  225. php_info_print_table_start();
  226. php_info_print_table_header( 2, "Internationalization support", "enabled" );
  227. php_info_print_table_row( 2, "ICU version", U_ICU_VERSION );
  228. #ifdef U_ICU_DATA_VERSION
  229. php_info_print_table_row( 2, "ICU Data version", U_ICU_DATA_VERSION );
  230. #endif
  231. #ifndef UCONFIG_NO_FORMATTING
  232. tzdata_ver = ucal_getTZDataVersion(&status);
  233. if (U_ZERO_ERROR == status) {
  234. php_info_print_table_row( 2, "ICU TZData version", tzdata_ver);
  235. }
  236. #endif
  237. php_info_print_table_row( 2, "ICU Unicode version", U_UNICODE_VERSION );
  238. php_info_print_table_end();
  239. /* For the default locale php.ini setting */
  240. DISPLAY_INI_ENTRIES() ;
  241. }
  242. /* }}} */