xref: /PHP-8.1/ext/intl/php_intl.c (revision 31e21f7d)
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 "php_intl_arginfo.h"
25 #include "intl_error.h"
26 #include "collator/collator_class.h"
27 #include "collator/collator.h"
28 #include "collator/collator_sort.h"
29 #include "collator/collator_convert.h"
30 
31 #include "converter/converter.h"
32 
33 #include "formatter/formatter.h"
34 #include "formatter/formatter_class.h"
35 #include "formatter/formatter_format.h"
36 
37 #include "grapheme/grapheme.h"
38 
39 #include "msgformat/msgformat_class.h"
40 
41 #include "normalizer/normalizer.h"
42 #include "normalizer/normalizer_class.h"
43 
44 #include "locale/locale.h"
45 #include "locale/locale_class.h"
46 
47 #include "dateformat/dateformat.h"
48 #include "dateformat/dateformat_class.h"
49 #include "dateformat/dateformat_data.h"
50 #include "dateformat/datepatterngenerator_class.h"
51 
52 #include "resourcebundle/resourcebundle_class.h"
53 
54 #include "transliterator/transliterator.h"
55 #include "transliterator/transliterator_class.h"
56 
57 #include "timezone/timezone_class.h"
58 
59 #include "calendar/calendar_class.h"
60 
61 #include "breakiterator/breakiterator_class.h"
62 #include "breakiterator/breakiterator_iterators.h"
63 
64 #include "idn/idn.h"
65 #include "uchar/uchar.h"
66 
67 # include "spoofchecker/spoofchecker_class.h"
68 # include "spoofchecker/spoofchecker.h"
69 
70 #include "common/common_error.h"
71 #include "common/common_enum.h"
72 
73 #include <unicode/uloc.h>
74 #include <unicode/uclean.h>
75 #include <ext/standard/info.h>
76 
77 #include "php_ini.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_LONG_CONSTANT("INTL_MAX_LOCALE_LEN", INTL_MAX_LOCALE_LEN, CONST_PERSISTENT | CONST_CS);
152 	REGISTER_STRING_CONSTANT("INTL_ICU_VERSION", U_ICU_VERSION, CONST_PERSISTENT | CONST_CS);
153 #ifdef U_ICU_DATA_VERSION
154 	REGISTER_STRING_CONSTANT("INTL_ICU_DATA_VERSION", U_ICU_DATA_VERSION, CONST_PERSISTENT | CONST_CS);
155 #endif
156 
157 	/* Register 'Collator' PHP class */
158 	collator_register_Collator_class(  );
159 
160 	/* Expose Collator constants to PHP scripts */
161 	collator_register_constants( INIT_FUNC_ARGS_PASSTHRU );
162 
163 	/* Register 'NumberFormatter' PHP class */
164 	formatter_register_class(  );
165 
166 	/* Expose NumberFormatter constants to PHP scripts */
167 	formatter_register_constants( INIT_FUNC_ARGS_PASSTHRU );
168 
169 	/* Register 'Normalizer' PHP class */
170 	normalizer_register_Normalizer_class(  );
171 
172 	/* Expose Normalizer constants to PHP scripts */
173 	normalizer_register_constants( INIT_FUNC_ARGS_PASSTHRU );
174 
175 	/* Register 'Locale' PHP class */
176 	locale_register_Locale_class(  );
177 
178 	/* Expose Locale constants to PHP scripts */
179 	locale_register_constants( INIT_FUNC_ARGS_PASSTHRU );
180 
181 	msgformat_register_class();
182 
183 	grapheme_register_constants( INIT_FUNC_ARGS_PASSTHRU );
184 
185 	/* Register 'DateFormat' PHP class */
186 	dateformat_register_IntlDateFormatter_class(  );
187 
188 	/* Expose DateFormat constants to PHP scripts */
189 	dateformat_register_constants( INIT_FUNC_ARGS_PASSTHRU );
190 
191 	/* Register 'IntlDateTimeFormatter' PHP class */
192 	dateformat_register_IntlDatePatternGenerator_class(  );
193 
194 	/* Register 'ResourceBundle' PHP class */
195 	resourcebundle_register_class( );
196 
197 	/* Register 'Transliterator' PHP class */
198 	transliterator_register_Transliterator_class(  );
199 
200 	/* Register Transliterator constants */
201 	transliterator_register_constants( INIT_FUNC_ARGS_PASSTHRU );
202 
203 	/* Register 'IntlTimeZone' PHP class */
204 	timezone_register_IntlTimeZone_class(  );
205 
206 	/* Register 'IntlCalendar' PHP class */
207 	calendar_register_IntlCalendar_class(  );
208 
209 	/* Expose ICU error codes to PHP scripts. */
210 	intl_expose_icu_error_codes( INIT_FUNC_ARGS_PASSTHRU );
211 
212 	/* Expose IDN constants to PHP scripts. */
213 	idn_register_constants(INIT_FUNC_ARGS_PASSTHRU);
214 
215 	/* Register 'Spoofchecker' PHP class */
216 	spoofchecker_register_Spoofchecker_class(  );
217 
218 	/* Expose Spoofchecker constants to PHP scripts */
219 	spoofchecker_register_constants( INIT_FUNC_ARGS_PASSTHRU );
220 
221 	/* Register 'IntlException' PHP class */
222 	IntlException_ce_ptr = register_class_IntlException(zend_ce_exception);
223 	IntlException_ce_ptr->create_object = zend_ce_exception->create_object;
224 
225 	/* Register 'IntlIterator' PHP class */
226 	intl_register_IntlIterator_class(  );
227 
228 	/* Register 'BreakIterator' class */
229 	breakiterator_register_BreakIterator_class(  );
230 
231 	/* Register 'IntlPartsIterator' class */
232 	breakiterator_register_IntlPartsIterator_class();
233 
234 	/* Global error handling. */
235 	intl_error_init( NULL );
236 
237 	/* 'Converter' class for codepage conversions */
238 	php_converter_minit(INIT_FUNC_ARGS_PASSTHRU);
239 
240 	/* IntlChar class */
241 	php_uchar_minit(INIT_FUNC_ARGS_PASSTHRU);
242 
243 	return SUCCESS;
244 }
245 /* }}} */
246 
247 #define EXPLICIT_CLEANUP_ENV_VAR "INTL_EXPLICIT_CLEANUP"
248 
249 /* {{{ PHP_MSHUTDOWN_FUNCTION */
PHP_MSHUTDOWN_FUNCTION(intl)250 PHP_MSHUTDOWN_FUNCTION( intl )
251 {
252 	const char *cleanup;
253 	/* For the default locale php.ini setting */
254 	UNREGISTER_INI_ENTRIES();
255 
256 	cleanup = getenv(EXPLICIT_CLEANUP_ENV_VAR);
257 	if (cleanup != NULL && !(cleanup[0] == '0' && cleanup[1] == '\0')) {
258 		u_cleanup();
259 	}
260 
261 	return SUCCESS;
262 }
263 /* }}} */
264 
265 /* {{{ PHP_RINIT_FUNCTION */
PHP_RINIT_FUNCTION(intl)266 PHP_RINIT_FUNCTION( intl )
267 {
268 	return SUCCESS;
269 }
270 /* }}} */
271 
272 /* {{{ PHP_RSHUTDOWN_FUNCTION */
PHP_RSHUTDOWN_FUNCTION(intl)273 PHP_RSHUTDOWN_FUNCTION( intl )
274 {
275 	INTL_G(current_collator) = NULL;
276 	if (INTL_G(grapheme_iterator)) {
277 		grapheme_close_global_iterator(  );
278 		INTL_G(grapheme_iterator) = NULL;
279 	}
280 
281 	intl_error_reset( NULL);
282 	return SUCCESS;
283 }
284 /* }}} */
285 
286 /* {{{ PHP_MINFO_FUNCTION */
PHP_MINFO_FUNCTION(intl)287 PHP_MINFO_FUNCTION( intl )
288 {
289 #if !UCONFIG_NO_FORMATTING
290 	UErrorCode status = U_ZERO_ERROR;
291 	const char *tzdata_ver = NULL;
292 #endif
293 
294 	php_info_print_table_start();
295 	php_info_print_table_header( 2, "Internationalization support", "enabled" );
296 	php_info_print_table_row( 2, "ICU version", U_ICU_VERSION );
297 #ifdef U_ICU_DATA_VERSION
298 	php_info_print_table_row( 2, "ICU Data version", U_ICU_DATA_VERSION );
299 #endif
300 #if !UCONFIG_NO_FORMATTING
301 	tzdata_ver = ucal_getTZDataVersion(&status);
302 	if (U_ZERO_ERROR == status) {
303 		php_info_print_table_row( 2, "ICU TZData version", tzdata_ver);
304 	}
305 #endif
306 	php_info_print_table_row( 2, "ICU Unicode version", U_UNICODE_VERSION );
307 	php_info_print_table_end();
308 
309 	/* For the default locale php.ini setting */
310 	DISPLAY_INI_ENTRIES() ;
311 }
312 /* }}} */
313