xref: /PHP-8.4/ext/intl/php_intl.c (revision 29f98e74)
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 
18 #ifdef HAVE_CONFIG_H
19 #include <config.h>
20 #endif
21 
22 
23 #include "php_intl.h"
24 #include "intl_error.h"
25 #include "collator/collator_class.h"
26 #include "collator/collator.h"
27 #include "collator/collator_sort.h"
28 #include "collator/collator_convert.h"
29 
30 #include "converter/converter.h"
31 
32 #include "formatter/formatter_class.h"
33 #include "formatter/formatter_format.h"
34 
35 #include "grapheme/grapheme.h"
36 
37 #include "msgformat/msgformat_class.h"
38 
39 #include "normalizer/normalizer_class.h"
40 
41 #include "locale/locale.h"
42 #include "locale/locale_class.h"
43 
44 #include "dateformat/dateformat.h"
45 #include "dateformat/dateformat_class.h"
46 #include "dateformat/dateformat_data.h"
47 #include "dateformat/datepatterngenerator_class.h"
48 
49 #include "resourcebundle/resourcebundle_class.h"
50 
51 #include "transliterator/transliterator.h"
52 #include "transliterator/transliterator_class.h"
53 
54 #include "timezone/timezone_class.h"
55 
56 #include "calendar/calendar_class.h"
57 
58 #include "breakiterator/breakiterator_class.h"
59 #include "breakiterator/breakiterator_iterators.h"
60 
61 #include <unicode/uidna.h>
62 #include "idn/idn.h"
63 #include "uchar/uchar.h"
64 
65 # include "spoofchecker/spoofchecker_class.h"
66 
67 #include "common/common_enum.h"
68 
69 #include <unicode/uloc.h>
70 #include <unicode/uclean.h>
71 #include <ext/standard/info.h>
72 
73 #include "php_ini.h"
74 
75 #include "zend_attributes.h"
76 
77 #include "php_intl_arginfo.h"
78 
79 /*
80  * locale_get_default has a conflict since ICU also has
81  * a function with the same  name
82  * in fact ICU appends the version no. to it also
83  * Hence the following undef for ICU version
84  * Same true for the locale_set_default function
85 */
86 #undef locale_get_default
87 #undef locale_set_default
88 
ZEND_DECLARE_MODULE_GLOBALS(intl)89 ZEND_DECLARE_MODULE_GLOBALS( intl )
90 
91 const char *intl_locale_get_default( void )
92 {
93 	if( INTL_G(default_locale) == NULL ) {
94 		return uloc_getDefault();
95 	}
96 	return INTL_G(default_locale);
97 }
98 
99 /* {{{ INI Settings */
100 PHP_INI_BEGIN()
101 	STD_PHP_INI_ENTRY(LOCALE_INI_NAME, NULL, PHP_INI_ALL, OnUpdateStringUnempty, default_locale, zend_intl_globals, intl_globals)
102 	STD_PHP_INI_ENTRY("intl.error_level", "0", PHP_INI_ALL, OnUpdateLong, error_level, zend_intl_globals, intl_globals)
103 	STD_PHP_INI_BOOLEAN("intl.use_exceptions", "0", PHP_INI_ALL, OnUpdateBool, use_exceptions, zend_intl_globals, intl_globals)
104 PHP_INI_END()
105 /* }}} */
106 
107 static PHP_GINIT_FUNCTION(intl);
108 
109 /* {{{ intl_module_entry */
110 zend_module_entry intl_module_entry = {
111 	STANDARD_MODULE_HEADER,
112 	"intl",
113 	ext_functions,
114 	PHP_MINIT( intl ),
115 	PHP_MSHUTDOWN( intl ),
116 	PHP_RINIT( intl ),
117 	PHP_RSHUTDOWN( intl ),
118 	PHP_MINFO( intl ),
119 	PHP_INTL_VERSION,
120 	PHP_MODULE_GLOBALS(intl),   /* globals descriptor */
121 	PHP_GINIT(intl),            /* globals ctor */
122 	NULL,                       /* globals dtor */
123 	NULL,                       /* post deactivate */
124 	STANDARD_MODULE_PROPERTIES_EX
125 };
126 /* }}} */
127 
128 #ifdef COMPILE_DL_INTL
129 #ifdef ZTS
130 ZEND_TSRMLS_CACHE_DEFINE()
131 #endif
ZEND_GET_MODULE(intl)132 ZEND_GET_MODULE( intl )
133 #endif
134 
135 /* {{{ intl_init_globals */
136 static PHP_GINIT_FUNCTION(intl)
137 {
138 #if defined(COMPILE_DL_INTL) && defined(ZTS)
139 	ZEND_TSRMLS_CACHE_UPDATE();
140 #endif
141 	memset( intl_globals, 0, sizeof(zend_intl_globals) );
142 }
143 /* }}} */
144 
145 /* {{{ PHP_MINIT_FUNCTION */
PHP_MINIT_FUNCTION(intl)146 PHP_MINIT_FUNCTION( intl )
147 {
148 	/* For the default locale php.ini setting */
149 	REGISTER_INI_ENTRIES();
150 
151 	register_php_intl_symbols(module_number);
152 
153 	/* Register collator symbols and classes */
154 	collator_register_Collator_symbols(module_number);
155 
156 	/* Register 'NumberFormatter' PHP class */
157 	formatter_register_class(  );
158 
159 	/* Register 'Normalizer' PHP class */
160 	normalizer_register_Normalizer_class(  );
161 
162 	/* Register 'Locale' PHP class */
163 	locale_register_Locale_class(  );
164 
165 	msgformat_register_class();
166 
167 	/* Register 'DateFormat' PHP class */
168 	dateformat_register_IntlDateFormatter_class(  );
169 
170 	/* Register 'IntlDateTimeFormatter' PHP class */
171 	dateformat_register_IntlDatePatternGenerator_class(  );
172 
173 	/* Register 'ResourceBundle' PHP class */
174 	resourcebundle_register_class( );
175 
176 	/* Register 'Transliterator' PHP class */
177 	transliterator_register_Transliterator_class(  );
178 
179 	/* Register 'IntlTimeZone' PHP class */
180 	timezone_register_IntlTimeZone_class(  );
181 
182 	/* Register 'IntlCalendar' PHP class */
183 	calendar_register_IntlCalendar_class(  );
184 
185 	/* Register 'Spoofchecker' PHP class */
186 	spoofchecker_register_Spoofchecker_class(  );
187 
188 	/* Register 'IntlException' PHP class */
189 	IntlException_ce_ptr = register_class_IntlException(zend_ce_exception);
190 	IntlException_ce_ptr->create_object = zend_ce_exception->create_object;
191 
192 	/* Register common symbols and classes */
193 	intl_register_common_symbols(module_number);
194 
195 	/* Register 'BreakIterator' class */
196 	breakiterator_register_BreakIterator_class(  );
197 
198 	/* Register 'IntlPartsIterator' class */
199 	breakiterator_register_IntlPartsIterator_class();
200 
201 	/* Global error handling. */
202 	intl_error_init( NULL );
203 
204 	/* 'Converter' class for codepage conversions */
205 	php_converter_minit(INIT_FUNC_ARGS_PASSTHRU);
206 
207 	/* IntlChar class */
208 	php_uchar_minit(INIT_FUNC_ARGS_PASSTHRU);
209 
210 	return SUCCESS;
211 }
212 /* }}} */
213 
214 #define EXPLICIT_CLEANUP_ENV_VAR "INTL_EXPLICIT_CLEANUP"
215 
216 /* {{{ PHP_MSHUTDOWN_FUNCTION */
PHP_MSHUTDOWN_FUNCTION(intl)217 PHP_MSHUTDOWN_FUNCTION( intl )
218 {
219 	const char *cleanup;
220 	/* For the default locale php.ini setting */
221 	UNREGISTER_INI_ENTRIES();
222 
223 	cleanup = getenv(EXPLICIT_CLEANUP_ENV_VAR);
224 	if (cleanup != NULL && !(cleanup[0] == '0' && cleanup[1] == '\0')) {
225 		u_cleanup();
226 	}
227 
228 	return SUCCESS;
229 }
230 /* }}} */
231 
232 /* {{{ PHP_RINIT_FUNCTION */
PHP_RINIT_FUNCTION(intl)233 PHP_RINIT_FUNCTION( intl )
234 {
235 	return SUCCESS;
236 }
237 /* }}} */
238 
239 /* {{{ PHP_RSHUTDOWN_FUNCTION */
PHP_RSHUTDOWN_FUNCTION(intl)240 PHP_RSHUTDOWN_FUNCTION( intl )
241 {
242 	INTL_G(current_collator) = NULL;
243 	if (INTL_G(grapheme_iterator)) {
244 		grapheme_close_global_iterator(  );
245 		INTL_G(grapheme_iterator) = NULL;
246 	}
247 
248 	intl_error_reset( NULL);
249 	return SUCCESS;
250 }
251 /* }}} */
252 
253 /* {{{ PHP_MINFO_FUNCTION */
PHP_MINFO_FUNCTION(intl)254 PHP_MINFO_FUNCTION( intl )
255 {
256 #if !UCONFIG_NO_FORMATTING
257 	UErrorCode status = U_ZERO_ERROR;
258 	const char *tzdata_ver = NULL;
259 #endif
260 
261 	php_info_print_table_start();
262 	php_info_print_table_row( 2, "Internationalization support", "enabled" );
263 	php_info_print_table_row( 2, "ICU version", U_ICU_VERSION );
264 #ifdef U_ICU_DATA_VERSION
265 	php_info_print_table_row( 2, "ICU Data version", U_ICU_DATA_VERSION );
266 #endif
267 #if !UCONFIG_NO_FORMATTING
268 	tzdata_ver = ucal_getTZDataVersion(&status);
269 	if (U_ZERO_ERROR == status) {
270 		php_info_print_table_row( 2, "ICU TZData version", tzdata_ver);
271 	}
272 #endif
273 	php_info_print_table_row( 2, "ICU Unicode version", U_UNICODE_VERSION );
274 	php_info_print_table_end();
275 
276 	/* For the default locale php.ini setting */
277 	DISPLAY_INI_ENTRIES() ;
278 }
279 /* }}} */
280