xref: /PHP-8.3/ext/intl/php_intl.c (revision c75facb5)
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 "php_intl_arginfo.h"
76 
77 /*
78  * locale_get_default has a conflict since ICU also has
79  * a function with the same  name
80  * in fact ICU appends the version no. to it also
81  * Hence the following undef for ICU version
82  * Same true for the locale_set_default function
83 */
84 #undef locale_get_default
85 #undef locale_set_default
86 
ZEND_DECLARE_MODULE_GLOBALS(intl)87 ZEND_DECLARE_MODULE_GLOBALS( intl )
88 
89 const char *intl_locale_get_default( void )
90 {
91 	if( INTL_G(default_locale) == NULL ) {
92 		return uloc_getDefault();
93 	}
94 	return INTL_G(default_locale);
95 }
96 
97 /* {{{ INI Settings */
98 PHP_INI_BEGIN()
99 	STD_PHP_INI_ENTRY(LOCALE_INI_NAME, NULL, PHP_INI_ALL, OnUpdateStringUnempty, default_locale, zend_intl_globals, intl_globals)
100 	STD_PHP_INI_ENTRY("intl.error_level", "0", PHP_INI_ALL, OnUpdateLong, error_level, zend_intl_globals, intl_globals)
101 	STD_PHP_INI_BOOLEAN("intl.use_exceptions", "0", PHP_INI_ALL, OnUpdateBool, use_exceptions, zend_intl_globals, intl_globals)
102 PHP_INI_END()
103 /* }}} */
104 
105 static PHP_GINIT_FUNCTION(intl);
106 
107 /* {{{ intl_module_entry */
108 zend_module_entry intl_module_entry = {
109 	STANDARD_MODULE_HEADER,
110 	"intl",
111 	ext_functions,
112 	PHP_MINIT( intl ),
113 	PHP_MSHUTDOWN( intl ),
114 	PHP_RINIT( intl ),
115 	PHP_RSHUTDOWN( intl ),
116 	PHP_MINFO( intl ),
117 	PHP_INTL_VERSION,
118 	PHP_MODULE_GLOBALS(intl),   /* globals descriptor */
119 	PHP_GINIT(intl),            /* globals ctor */
120 	NULL,                       /* globals dtor */
121 	NULL,                       /* post deactivate */
122 	STANDARD_MODULE_PROPERTIES_EX
123 };
124 /* }}} */
125 
126 #ifdef COMPILE_DL_INTL
127 #ifdef ZTS
128 ZEND_TSRMLS_CACHE_DEFINE()
129 #endif
ZEND_GET_MODULE(intl)130 ZEND_GET_MODULE( intl )
131 #endif
132 
133 /* {{{ intl_init_globals */
134 static PHP_GINIT_FUNCTION(intl)
135 {
136 #if defined(COMPILE_DL_INTL) && defined(ZTS)
137 	ZEND_TSRMLS_CACHE_UPDATE();
138 #endif
139 	memset( intl_globals, 0, sizeof(zend_intl_globals) );
140 }
141 /* }}} */
142 
143 /* {{{ PHP_MINIT_FUNCTION */
PHP_MINIT_FUNCTION(intl)144 PHP_MINIT_FUNCTION( intl )
145 {
146 	/* For the default locale php.ini setting */
147 	REGISTER_INI_ENTRIES();
148 
149 	register_php_intl_symbols(module_number);
150 
151 	/* Register collator symbols and classes */
152 	collator_register_Collator_symbols(module_number);
153 
154 	/* Register 'NumberFormatter' PHP class */
155 	formatter_register_class(  );
156 
157 	/* Register 'Normalizer' PHP class */
158 	normalizer_register_Normalizer_class(  );
159 
160 	/* Register 'Locale' PHP class */
161 	locale_register_Locale_class(  );
162 
163 	msgformat_register_class();
164 
165 	/* Register 'DateFormat' PHP class */
166 	dateformat_register_IntlDateFormatter_class(  );
167 
168 	/* Register 'IntlDateTimeFormatter' PHP class */
169 	dateformat_register_IntlDatePatternGenerator_class(  );
170 
171 	/* Register 'ResourceBundle' PHP class */
172 	resourcebundle_register_class( );
173 
174 	/* Register 'Transliterator' PHP class */
175 	transliterator_register_Transliterator_class(  );
176 
177 	/* Register 'IntlTimeZone' PHP class */
178 	timezone_register_IntlTimeZone_class(  );
179 
180 	/* Register 'IntlCalendar' PHP class */
181 	calendar_register_IntlCalendar_class(  );
182 
183 	/* Register 'Spoofchecker' PHP class */
184 	spoofchecker_register_Spoofchecker_class(  );
185 
186 	/* Register 'IntlException' PHP class */
187 	IntlException_ce_ptr = register_class_IntlException(zend_ce_exception);
188 	IntlException_ce_ptr->create_object = zend_ce_exception->create_object;
189 
190 	/* Register common symbols and classes */
191 	intl_register_common_symbols(module_number);
192 
193 	/* Register 'BreakIterator' class */
194 	breakiterator_register_BreakIterator_class(  );
195 
196 	/* Register 'IntlPartsIterator' class */
197 	breakiterator_register_IntlPartsIterator_class();
198 
199 	/* Global error handling. */
200 	intl_error_init( NULL );
201 
202 	/* 'Converter' class for codepage conversions */
203 	php_converter_minit(INIT_FUNC_ARGS_PASSTHRU);
204 
205 	/* IntlChar class */
206 	php_uchar_minit(INIT_FUNC_ARGS_PASSTHRU);
207 
208 	return SUCCESS;
209 }
210 /* }}} */
211 
212 #define EXPLICIT_CLEANUP_ENV_VAR "INTL_EXPLICIT_CLEANUP"
213 
214 /* {{{ PHP_MSHUTDOWN_FUNCTION */
PHP_MSHUTDOWN_FUNCTION(intl)215 PHP_MSHUTDOWN_FUNCTION( intl )
216 {
217 	const char *cleanup;
218 	/* For the default locale php.ini setting */
219 	UNREGISTER_INI_ENTRIES();
220 
221 	cleanup = getenv(EXPLICIT_CLEANUP_ENV_VAR);
222 	if (cleanup != NULL && !(cleanup[0] == '0' && cleanup[1] == '\0')) {
223 		u_cleanup();
224 	}
225 
226 	return SUCCESS;
227 }
228 /* }}} */
229 
230 /* {{{ PHP_RINIT_FUNCTION */
PHP_RINIT_FUNCTION(intl)231 PHP_RINIT_FUNCTION( intl )
232 {
233 	return SUCCESS;
234 }
235 /* }}} */
236 
237 /* {{{ PHP_RSHUTDOWN_FUNCTION */
PHP_RSHUTDOWN_FUNCTION(intl)238 PHP_RSHUTDOWN_FUNCTION( intl )
239 {
240 	INTL_G(current_collator) = NULL;
241 	if (INTL_G(grapheme_iterator)) {
242 		grapheme_close_global_iterator(  );
243 		INTL_G(grapheme_iterator) = NULL;
244 	}
245 
246 	intl_error_reset( NULL);
247 	return SUCCESS;
248 }
249 /* }}} */
250 
251 /* {{{ PHP_MINFO_FUNCTION */
PHP_MINFO_FUNCTION(intl)252 PHP_MINFO_FUNCTION( intl )
253 {
254 #if !UCONFIG_NO_FORMATTING
255 	UErrorCode status = U_ZERO_ERROR;
256 	const char *tzdata_ver = NULL;
257 #endif
258 
259 	php_info_print_table_start();
260 	php_info_print_table_row( 2, "Internationalization support", "enabled" );
261 	php_info_print_table_row( 2, "ICU version", U_ICU_VERSION );
262 #ifdef U_ICU_DATA_VERSION
263 	php_info_print_table_row( 2, "ICU Data version", U_ICU_DATA_VERSION );
264 #endif
265 #if !UCONFIG_NO_FORMATTING
266 	tzdata_ver = ucal_getTZDataVersion(&status);
267 	if (U_ZERO_ERROR == status) {
268 		php_info_print_table_row( 2, "ICU TZData version", tzdata_ver);
269 	}
270 #endif
271 	php_info_print_table_row( 2, "ICU Unicode version", U_UNICODE_VERSION );
272 	php_info_print_table_end();
273 
274 	/* For the default locale php.ini setting */
275 	DISPLAY_INI_ENTRIES() ;
276 }
277 /* }}} */
278