xref: /PHP-8.0/ext/intl/php_intl.c (revision 27ad19c3)
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    | http://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 
51 #include "resourcebundle/resourcebundle_class.h"
52 
53 #include "transliterator/transliterator.h"
54 #include "transliterator/transliterator_class.h"
55 
56 #include "timezone/timezone_class.h"
57 
58 #include "calendar/calendar_class.h"
59 
60 #include "breakiterator/breakiterator_class.h"
61 #include "breakiterator/breakiterator_iterators.h"
62 
63 #include "idn/idn.h"
64 #include "uchar/uchar.h"
65 
66 # include "spoofchecker/spoofchecker_class.h"
67 # include "spoofchecker/spoofchecker.h"
68 
69 #include "common/common_error.h"
70 #include "common/common_enum.h"
71 
72 #include <unicode/uloc.h>
73 #include <unicode/uclean.h>
74 #include <ext/standard/info.h>
75 
76 #include "php_ini.h"
77 
78 /*
79  * locale_get_default has a conflict since ICU also has
80  * a function with the same  name
81  * in fact ICU appends the version no. to it also
82  * Hence the following undef for ICU version
83  * Same true for the locale_set_default function
84 */
85 #undef locale_get_default
86 #undef locale_set_default
87 
ZEND_DECLARE_MODULE_GLOBALS(intl)88 ZEND_DECLARE_MODULE_GLOBALS( intl )
89 
90 const char *intl_locale_get_default( void )
91 {
92 	if( INTL_G(default_locale) == NULL ) {
93 		return uloc_getDefault();
94 	}
95 	return INTL_G(default_locale);
96 }
97 
98 /* {{{ INI Settings */
99 PHP_INI_BEGIN()
100     STD_PHP_INI_ENTRY(LOCALE_INI_NAME, NULL, PHP_INI_ALL, OnUpdateStringUnempty, default_locale, zend_intl_globals, intl_globals)
101     STD_PHP_INI_ENTRY("intl.error_level", "0", PHP_INI_ALL, OnUpdateLong, error_level, zend_intl_globals, intl_globals)
102 	STD_PHP_INI_BOOLEAN("intl.use_exceptions", "0", PHP_INI_ALL, OnUpdateBool, use_exceptions, zend_intl_globals, intl_globals)
103 PHP_INI_END()
104 /* }}} */
105 
106 static PHP_GINIT_FUNCTION(intl);
107 
108 /* {{{ intl_module_entry */
109 zend_module_entry intl_module_entry = {
110 	STANDARD_MODULE_HEADER,
111 	"intl",
112 	ext_functions,
113 	PHP_MINIT( intl ),
114 	PHP_MSHUTDOWN( intl ),
115 	PHP_RINIT( intl ),
116 	PHP_RSHUTDOWN( intl ),
117 	PHP_MINFO( intl ),
118 	PHP_INTL_VERSION,
119 	PHP_MODULE_GLOBALS(intl),   /* globals descriptor */
120 	PHP_GINIT(intl),            /* globals ctor */
121 	NULL,                       /* globals dtor */
122 	NULL,                       /* post deactivate */
123 	STANDARD_MODULE_PROPERTIES_EX
124 };
125 /* }}} */
126 
127 #ifdef COMPILE_DL_INTL
128 #ifdef ZTS
129 ZEND_TSRMLS_CACHE_DEFINE()
130 #endif
ZEND_GET_MODULE(intl)131 ZEND_GET_MODULE( intl )
132 #endif
133 
134 /* {{{ intl_init_globals */
135 static PHP_GINIT_FUNCTION(intl)
136 {
137 #if defined(COMPILE_DL_INTL) && defined(ZTS)
138 	ZEND_TSRMLS_CACHE_UPDATE();
139 #endif
140 	memset( intl_globals, 0, sizeof(zend_intl_globals) );
141 }
142 /* }}} */
143 
144 /* {{{ PHP_MINIT_FUNCTION */
PHP_MINIT_FUNCTION(intl)145 PHP_MINIT_FUNCTION( intl )
146 {
147 	/* For the default locale php.ini setting */
148 	REGISTER_INI_ENTRIES();
149 
150 	REGISTER_LONG_CONSTANT("INTL_MAX_LOCALE_LEN", INTL_MAX_LOCALE_LEN, CONST_PERSISTENT | CONST_CS);
151 	REGISTER_STRING_CONSTANT("INTL_ICU_VERSION", U_ICU_VERSION, CONST_PERSISTENT | CONST_CS);
152 #ifdef U_ICU_DATA_VERSION
153 	REGISTER_STRING_CONSTANT("INTL_ICU_DATA_VERSION", U_ICU_DATA_VERSION, CONST_PERSISTENT | CONST_CS);
154 #endif
155 
156 	/* Register 'Collator' PHP class */
157 	collator_register_Collator_class(  );
158 
159 	/* Expose Collator constants to PHP scripts */
160 	collator_register_constants( INIT_FUNC_ARGS_PASSTHRU );
161 
162 	/* Register 'NumberFormatter' PHP class */
163 	formatter_register_class(  );
164 
165 	/* Expose NumberFormatter constants to PHP scripts */
166 	formatter_register_constants( INIT_FUNC_ARGS_PASSTHRU );
167 
168 	/* Register 'Normalizer' PHP class */
169 	normalizer_register_Normalizer_class(  );
170 
171 	/* Expose Normalizer constants to PHP scripts */
172 	normalizer_register_constants( INIT_FUNC_ARGS_PASSTHRU );
173 
174 	/* Register 'Locale' PHP class */
175 	locale_register_Locale_class(  );
176 
177 	/* Expose Locale constants to PHP scripts */
178 	locale_register_constants( INIT_FUNC_ARGS_PASSTHRU );
179 
180 	msgformat_register_class();
181 
182 	grapheme_register_constants( INIT_FUNC_ARGS_PASSTHRU );
183 
184 	/* Register 'DateFormat' PHP class */
185 	dateformat_register_IntlDateFormatter_class(  );
186 
187 	/* Expose DateFormat constants to PHP scripts */
188 	dateformat_register_constants( INIT_FUNC_ARGS_PASSTHRU );
189 
190 	/* Register 'ResourceBundle' PHP class */
191 	resourcebundle_register_class( );
192 
193 	/* Register 'Transliterator' PHP class */
194 	transliterator_register_Transliterator_class(  );
195 
196 	/* Register Transliterator constants */
197 	transliterator_register_constants( INIT_FUNC_ARGS_PASSTHRU );
198 
199 	/* Register 'IntlTimeZone' PHP class */
200 	timezone_register_IntlTimeZone_class(  );
201 
202 	/* Register 'IntlCalendar' PHP class */
203 	calendar_register_IntlCalendar_class(  );
204 
205 	/* Expose ICU error codes to PHP scripts. */
206 	intl_expose_icu_error_codes( INIT_FUNC_ARGS_PASSTHRU );
207 
208 	/* Expose IDN constants to PHP scripts. */
209 	idn_register_constants(INIT_FUNC_ARGS_PASSTHRU);
210 
211 	/* Register 'Spoofchecker' PHP class */
212 	spoofchecker_register_Spoofchecker_class(  );
213 
214 	/* Expose Spoofchecker constants to PHP scripts */
215 	spoofchecker_register_constants( INIT_FUNC_ARGS_PASSTHRU );
216 
217 	/* Register 'IntlException' PHP class */
218 	intl_register_IntlException_class(  );
219 
220 	/* Register 'IntlIterator' PHP class */
221 	intl_register_IntlIterator_class(  );
222 
223 	/* Register 'BreakIterator' class */
224 	breakiterator_register_BreakIterator_class(  );
225 
226 	/* Register 'IntlPartsIterator' class */
227 	breakiterator_register_IntlPartsIterator_class(  );
228 
229 	/* Global error handling. */
230 	intl_error_init( NULL );
231 
232 	/* 'Converter' class for codepage conversions */
233 	php_converter_minit(INIT_FUNC_ARGS_PASSTHRU);
234 
235 	/* IntlChar class */
236 	php_uchar_minit(INIT_FUNC_ARGS_PASSTHRU);
237 
238 	return SUCCESS;
239 }
240 /* }}} */
241 
242 #define EXPLICIT_CLEANUP_ENV_VAR "INTL_EXPLICIT_CLEANUP"
243 
244 /* {{{ PHP_MSHUTDOWN_FUNCTION */
PHP_MSHUTDOWN_FUNCTION(intl)245 PHP_MSHUTDOWN_FUNCTION( intl )
246 {
247 	const char *cleanup;
248     /* For the default locale php.ini setting */
249     UNREGISTER_INI_ENTRIES();
250 
251 	cleanup = getenv(EXPLICIT_CLEANUP_ENV_VAR);
252     if (cleanup != NULL && !(cleanup[0] == '0' && cleanup[1] == '\0')) {
253 		u_cleanup();
254     }
255 
256     return SUCCESS;
257 }
258 /* }}} */
259 
260 /* {{{ PHP_RINIT_FUNCTION */
PHP_RINIT_FUNCTION(intl)261 PHP_RINIT_FUNCTION( intl )
262 {
263 	return SUCCESS;
264 }
265 /* }}} */
266 
267 /* {{{ PHP_RSHUTDOWN_FUNCTION */
PHP_RSHUTDOWN_FUNCTION(intl)268 PHP_RSHUTDOWN_FUNCTION( intl )
269 {
270 	INTL_G(current_collator) = NULL;
271 	if (INTL_G(grapheme_iterator)) {
272 		grapheme_close_global_iterator(  );
273 		INTL_G(grapheme_iterator) = NULL;
274 	}
275 
276 	intl_error_reset( NULL);
277 	return SUCCESS;
278 }
279 /* }}} */
280 
281 /* {{{ PHP_MINFO_FUNCTION */
PHP_MINFO_FUNCTION(intl)282 PHP_MINFO_FUNCTION( intl )
283 {
284 #ifndef UCONFIG_NO_FORMATTING
285 	UErrorCode status = U_ZERO_ERROR;
286 	const char *tzdata_ver = NULL;
287 #endif
288 
289 	php_info_print_table_start();
290 	php_info_print_table_header( 2, "Internationalization support", "enabled" );
291 	php_info_print_table_row( 2, "ICU version", U_ICU_VERSION );
292 #ifdef U_ICU_DATA_VERSION
293 	php_info_print_table_row( 2, "ICU Data version", U_ICU_DATA_VERSION );
294 #endif
295 #ifndef UCONFIG_NO_FORMATTING
296 	tzdata_ver = ucal_getTZDataVersion(&status);
297 	if (U_ZERO_ERROR == status) {
298 		php_info_print_table_row( 2, "ICU TZData version", tzdata_ver);
299 	}
300 #endif
301 	php_info_print_table_row( 2, "ICU Unicode version", U_UNICODE_VERSION );
302 	php_info_print_table_end();
303 
304     /* For the default locale php.ini setting */
305     DISPLAY_INI_ENTRIES() ;
306 }
307 /* }}} */
308