1 /*
2 +----------------------------------------------------------------------+
3 | PHP Version 7 |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.01 of the PHP license, |
6 | that is bundled with this package in the file LICENSE, and is |
7 | available through the world-wide-web at the following url: |
8 | http://www.php.net/license/3_01.txt |
9 | If you did not receive a copy of the PHP license and are unable to |
10 | obtain it through the world-wide-web, please send a note to |
11 | license@php.net so we can mail you a copy immediately. |
12 +----------------------------------------------------------------------+
13 | Authors: Hans-Peter Oeri (University of St.Gallen) <hp@oeri.ch> |
14 +----------------------------------------------------------------------+
15 */
16
17 #include <stdlib.h>
18 #include <unicode/ures.h>
19 #include <unicode/uenum.h>
20
21 #include <zend.h>
22 #include <Zend/zend_exceptions.h>
23 #include <Zend/zend_interfaces.h>
24 #include <php.h>
25
26 #include "php_intl.h"
27 #include "intl_data.h"
28 #include "intl_common.h"
29
30 #include "resourcebundle/resourcebundle.h"
31 #include "resourcebundle/resourcebundle_iterator.h"
32 #include "resourcebundle/resourcebundle_class.h"
33
34 zend_class_entry *ResourceBundle_ce_ptr = NULL;
35
36 static zend_object_handlers ResourceBundle_object_handlers;
37
38 /* {{{ ResourceBundle_object_dtor */
ResourceBundle_object_destroy(zend_object * object)39 static void ResourceBundle_object_destroy( zend_object *object )
40 {
41 ResourceBundle_object *rb = php_intl_resourcebundle_fetch_object(object);
42
43 // only free local errors
44 intl_error_reset( INTL_DATA_ERROR_P(rb) );
45
46 if (rb->me) {
47 ures_close( rb->me );
48 }
49 if (rb->child) {
50 ures_close( rb->child );
51 }
52 }
53 /* }}} */
54
55 /* {{{ ResourceBundle_object_create */
ResourceBundle_object_create(zend_class_entry * ce)56 static zend_object *ResourceBundle_object_create( zend_class_entry *ce )
57 {
58 ResourceBundle_object *rb;
59
60 rb = ecalloc( 1, sizeof(ResourceBundle_object) + zend_object_properties_size(ce));
61
62 zend_object_std_init( &rb->zend, ce );
63 object_properties_init( &rb->zend, ce);
64
65 intl_error_init( INTL_DATA_ERROR_P(rb) );
66 rb->me = NULL;
67 rb->child = NULL;
68
69 rb->zend.handlers = &ResourceBundle_object_handlers;
70
71 return &rb->zend;
72 }
73 /* }}} */
74
75 /* {{{ ResourceBundle_ctor */
resourcebundle_ctor(INTERNAL_FUNCTION_PARAMETERS,zend_bool is_constructor)76 static int resourcebundle_ctor(INTERNAL_FUNCTION_PARAMETERS, zend_bool is_constructor)
77 {
78 const char *bundlename;
79 size_t bundlename_len = 0;
80 const char *locale;
81 size_t locale_len = 0;
82 zend_bool fallback = 1;
83 int zpp_flags = is_constructor ? ZEND_PARSE_PARAMS_THROW : 0;
84
85 zval *object = return_value;
86 ResourceBundle_object *rb = Z_INTL_RESOURCEBUNDLE_P( object );
87
88 intl_error_reset( NULL );
89
90 if( zend_parse_parameters_ex( zpp_flags, ZEND_NUM_ARGS(), "s!s!|b",
91 &locale, &locale_len, &bundlename, &bundlename_len, &fallback ) == FAILURE )
92 {
93 intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
94 "resourcebundle_ctor: unable to parse input parameters", 0 );
95 return FAILURE;
96 }
97
98 INTL_CHECK_LOCALE_LEN_OR_FAILURE(locale_len);
99
100 if (locale == NULL) {
101 locale = intl_locale_get_default();
102 }
103
104 if (bundlename_len >= MAXPATHLEN) {
105 intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "Bundle name too long", 0 );
106 zval_dtor(return_value);
107 ZVAL_NULL(return_value);
108 return FAILURE;
109 }
110
111 if (fallback) {
112 rb->me = ures_open(bundlename, locale, &INTL_DATA_ERROR_CODE(rb));
113 } else {
114 rb->me = ures_openDirect(bundlename, locale, &INTL_DATA_ERROR_CODE(rb));
115 }
116
117 INTL_CTOR_CHECK_STATUS(rb, "resourcebundle_ctor: Cannot load libICU resource bundle");
118
119 if (!fallback && (INTL_DATA_ERROR_CODE(rb) == U_USING_FALLBACK_WARNING ||
120 INTL_DATA_ERROR_CODE(rb) == U_USING_DEFAULT_WARNING)) {
121 char *pbuf;
122 intl_errors_set_code(NULL, INTL_DATA_ERROR_CODE(rb));
123 spprintf(&pbuf, 0, "resourcebundle_ctor: Cannot load libICU resource "
124 "'%s' without fallback from %s to %s",
125 bundlename ? bundlename : "(default data)", locale,
126 ures_getLocaleByType(
127 rb->me, ULOC_ACTUAL_LOCALE, &INTL_DATA_ERROR_CODE(rb)));
128 intl_errors_set_custom_msg(INTL_DATA_ERROR_P(rb), pbuf, 1);
129 efree(pbuf);
130 return FAILURE;
131 }
132
133 return SUCCESS;
134 }
135 /* }}} */
136
137 /* {{{ arginfo_resourcebundle__construct */
138 ZEND_BEGIN_ARG_INFO_EX( arginfo_resourcebundle___construct, 0, 0, 2 )
139 ZEND_ARG_INFO( 0, locale )
140 ZEND_ARG_INFO( 0, bundlename )
141 ZEND_ARG_INFO( 0, fallback )
ZEND_END_ARG_INFO()142 ZEND_END_ARG_INFO()
143 /* }}} */
144
145 /* {{{ proto void ResourceBundle::__construct( string $locale [, string $bundlename [, bool $fallback = true ]] )
146 * ResourceBundle object constructor
147 */
148 PHP_METHOD( ResourceBundle, __construct )
149 {
150 zend_error_handling error_handling;
151
152 zend_replace_error_handling(EH_THROW, IntlException_ce_ptr, &error_handling);
153 return_value = getThis();
154 if (resourcebundle_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1) == FAILURE) {
155 if (!EG(exception)) {
156 zend_throw_exception(IntlException_ce_ptr, "Constructor failed", 0);
157 }
158 }
159 zend_restore_error_handling(&error_handling);
160 }
161 /* }}} */
162
163 /* {{{ proto ResourceBundle ResourceBundle::create( string $locale [, string $bundlename [, bool $fallback = true ]] )
164 proto ResourceBundle resourcebundle_create( string $locale [, string $bundlename [, bool $fallback = true ]] )
165 */
PHP_FUNCTION(resourcebundle_create)166 PHP_FUNCTION( resourcebundle_create )
167 {
168 object_init_ex( return_value, ResourceBundle_ce_ptr );
169 if (resourcebundle_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0) == FAILURE) {
170 zval_ptr_dtor(return_value);
171 RETURN_NULL();
172 }
173 }
174 /* }}} */
175
176 /* {{{ resourcebundle_array_fetch */
resourcebundle_array_fetch(zval * object,zval * offset,zval * return_value,int fallback)177 static void resourcebundle_array_fetch(zval *object, zval *offset, zval *return_value, int fallback)
178 {
179 int32_t meindex = 0;
180 char * mekey = NULL;
181 zend_bool is_numeric = 0;
182 char *pbuf;
183 ResourceBundle_object *rb;
184
185 intl_error_reset( NULL );
186 RESOURCEBUNDLE_METHOD_FETCH_OBJECT;
187
188 if(Z_TYPE_P(offset) == IS_LONG) {
189 is_numeric = 1;
190 meindex = (int32_t)Z_LVAL_P(offset);
191 rb->child = ures_getByIndex( rb->me, meindex, rb->child, &INTL_DATA_ERROR_CODE(rb) );
192 } else if(Z_TYPE_P(offset) == IS_STRING) {
193 mekey = Z_STRVAL_P(offset);
194 rb->child = ures_getByKey(rb->me, mekey, rb->child, &INTL_DATA_ERROR_CODE(rb) );
195 } else {
196 intl_errors_set(INTL_DATA_ERROR_P(rb), U_ILLEGAL_ARGUMENT_ERROR,
197 "resourcebundle_get: index should be integer or string", 0);
198 RETURN_NULL();
199 }
200
201 intl_error_set_code( NULL, INTL_DATA_ERROR_CODE(rb) );
202 if (U_FAILURE(INTL_DATA_ERROR_CODE(rb))) {
203 if (is_numeric) {
204 spprintf( &pbuf, 0, "Cannot load resource element %d", meindex );
205 } else {
206 spprintf( &pbuf, 0, "Cannot load resource element '%s'", mekey );
207 }
208 intl_errors_set_custom_msg( INTL_DATA_ERROR_P(rb), pbuf, 1 );
209 efree(pbuf);
210 RETURN_NULL();
211 }
212
213 if (!fallback && (INTL_DATA_ERROR_CODE(rb) == U_USING_FALLBACK_WARNING || INTL_DATA_ERROR_CODE(rb) == U_USING_DEFAULT_WARNING)) {
214 UErrorCode icuerror;
215 const char * locale = ures_getLocaleByType( rb->me, ULOC_ACTUAL_LOCALE, &icuerror );
216 if (is_numeric) {
217 spprintf( &pbuf, 0, "Cannot load element %d without fallback from to %s", meindex, locale );
218 } else {
219 spprintf( &pbuf, 0, "Cannot load element '%s' without fallback from to %s", mekey, locale );
220 }
221 intl_errors_set_custom_msg( INTL_DATA_ERROR_P(rb), pbuf, 1 );
222 efree(pbuf);
223 RETURN_NULL();
224 }
225
226 resourcebundle_extract_value( return_value, rb );
227 }
228 /* }}} */
229
230 /* {{{ resourcebundle_array_get */
resourcebundle_array_get(zval * object,zval * offset,int type,zval * rv)231 zval *resourcebundle_array_get(zval *object, zval *offset, int type, zval *rv)
232 {
233 if(offset == NULL) {
234 php_error( E_ERROR, "Cannot apply [] to ResourceBundle object" );
235 }
236 ZVAL_NULL(rv);
237 resourcebundle_array_fetch(object, offset, rv, 1);
238 return rv;
239 }
240 /* }}} */
241
242 /* {{{ arginfo_resourcebundle_get */
243 ZEND_BEGIN_ARG_INFO_EX( arginfo_resourcebundle_get, 0, 0, 1 )
244 ZEND_ARG_INFO( 0, index )
245 ZEND_ARG_INFO( 0, fallback )
ZEND_END_ARG_INFO()246 ZEND_END_ARG_INFO()
247 /* }}} */
248
249 /* {{{ proto mixed ResourceBundle::get( integer|string $resindex [, bool $fallback = true ] )
250 * proto mixed resourcebundle_get( ResourceBundle $rb, integer|string $resindex [, bool $fallback = true ] )
251 * Get resource identified by numerical index or key name.
252 */
253 PHP_FUNCTION( resourcebundle_get )
254 {
255 zend_bool fallback = 1;
256 zval * offset;
257 zval * object;
258
259 if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Oz|b", &object, ResourceBundle_ce_ptr, &offset, &fallback ) == FAILURE) {
260 intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
261 "resourcebundle_get: unable to parse input params", 0);
262 RETURN_FALSE;
263 }
264
265 resourcebundle_array_fetch(object, offset, return_value, fallback);
266 }
267 /* }}} */
268
269 /* {{{ resourcebundle_array_count */
resourcebundle_array_count(zval * object,zend_long * count)270 int resourcebundle_array_count(zval *object, zend_long *count)
271 {
272 ResourceBundle_object *rb;
273 RESOURCEBUNDLE_METHOD_FETCH_OBJECT_NO_CHECK;
274
275 if (rb->me == NULL) {
276 intl_errors_set(&rb->error, U_ILLEGAL_ARGUMENT_ERROR,
277 "Found unconstructed ResourceBundle", 0);
278 return 0;
279 }
280
281 *count = ures_getSize( rb->me );
282
283 return SUCCESS;
284 }
285 /* }}} */
286
287 /* {{{ arginfo_resourcebundle_count */
288 ZEND_BEGIN_ARG_INFO_EX( arginfo_resourcebundle_count, 0, 0, 0 )
ZEND_END_ARG_INFO()289 ZEND_END_ARG_INFO()
290 /* }}} */
291
292 /* {{{ proto int ResourceBundle::count()
293 * proto int resourcebundle_count( ResourceBundle $bundle )
294 * Get resources count
295 */
296 PHP_FUNCTION( resourcebundle_count )
297 {
298 int32_t len;
299 RESOURCEBUNDLE_METHOD_INIT_VARS;
300
301 if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "O", &object, ResourceBundle_ce_ptr ) == FAILURE ) {
302 intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
303 "resourcebundle_count: unable to parse input params", 0);
304 RETURN_FALSE;
305 }
306
307 RESOURCEBUNDLE_METHOD_FETCH_OBJECT;
308
309 len = ures_getSize( rb->me );
310 RETURN_LONG( len );
311 }
312
313 /* {{{ arginfo_resourcebundle_getlocales */
314 ZEND_BEGIN_ARG_INFO_EX( arginfo_resourcebundle_getlocales, 0, 0, 1 )
315 ZEND_ARG_INFO( 0, bundlename )
ZEND_END_ARG_INFO()316 ZEND_END_ARG_INFO()
317 /* }}} */
318
319 /* {{{ proto array ResourceBundle::getLocales( string $bundlename )
320 * proto array resourcebundle_locales( string $bundlename )
321 * Get available locales from ResourceBundle name
322 */
323 PHP_FUNCTION( resourcebundle_locales )
324 {
325 char * bundlename;
326 size_t bundlename_len = 0;
327 const char * entry;
328 int entry_len;
329 UEnumeration *icuenum;
330 UErrorCode icuerror = U_ZERO_ERROR;
331
332 intl_errors_reset( NULL );
333
334 if( zend_parse_parameters(ZEND_NUM_ARGS(), "s", &bundlename, &bundlename_len ) == FAILURE )
335 {
336 intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
337 "resourcebundle_locales: unable to parse input params", 0);
338 RETURN_FALSE;
339 }
340
341 if (bundlename_len >= MAXPATHLEN) {
342 intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "resourcebundle_locales: bundle name too long", 0 );
343 RETURN_FALSE;
344 }
345
346 if(bundlename_len == 0) {
347 // fetch default locales list
348 bundlename = NULL;
349 }
350
351 icuenum = ures_openAvailableLocales( bundlename, &icuerror );
352 INTL_CHECK_STATUS(icuerror, "Cannot fetch locales list");
353
354 uenum_reset( icuenum, &icuerror );
355 INTL_CHECK_STATUS(icuerror, "Cannot iterate locales list");
356
357 array_init( return_value );
358 while ((entry = uenum_next( icuenum, &entry_len, &icuerror ))) {
359 add_next_index_stringl( return_value, (char *) entry, entry_len);
360 }
361 uenum_close( icuenum );
362 }
363 /* }}} */
364
365 /* {{{ arginfo_resourcebundle_get_error_code */
366 ZEND_BEGIN_ARG_INFO_EX( arginfo_resourcebundle_get_error_code, 0, 0, 0 )
ZEND_END_ARG_INFO()367 ZEND_END_ARG_INFO()
368 /* }}} */
369
370 /* {{{ proto string ResourceBundle::getErrorCode( )
371 * proto string resourcebundle_get_error_code( ResourceBundle $bundle )
372 * Get text description for ResourceBundle's last error code.
373 */
374 PHP_FUNCTION( resourcebundle_get_error_code )
375 {
376 RESOURCEBUNDLE_METHOD_INIT_VARS;
377
378 if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "O",
379 &object, ResourceBundle_ce_ptr ) == FAILURE )
380 {
381 intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
382 "resourcebundle_get_error_code: unable to parse input params", 0 );
383 RETURN_FALSE;
384 }
385
386 rb = Z_INTL_RESOURCEBUNDLE_P( object );
387
388 RETURN_LONG(INTL_DATA_ERROR_CODE(rb));
389 }
390 /* }}} */
391
392 /* {{{ arginfo_resourcebundle_get_error_message */
393 ZEND_BEGIN_ARG_INFO_EX( arginfo_resourcebundle_get_error_message, 0, 0, 0 )
ZEND_END_ARG_INFO()394 ZEND_END_ARG_INFO()
395 /* }}} */
396
397 /* {{{ proto string ResourceBundle::getErrorMessage( )
398 * proto string resourcebundle_get_error_message( ResourceBundle $bundle )
399 * Get text description for ResourceBundle's last error.
400 */
401 PHP_FUNCTION( resourcebundle_get_error_message )
402 {
403 zend_string* message = NULL;
404 RESOURCEBUNDLE_METHOD_INIT_VARS;
405
406 if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "O",
407 &object, ResourceBundle_ce_ptr ) == FAILURE )
408 {
409 intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
410 "resourcebundle_get_error_message: unable to parse input params", 0 );
411 RETURN_FALSE;
412 }
413
414 rb = Z_INTL_RESOURCEBUNDLE_P( object );
415 message = intl_error_get_message(INTL_DATA_ERROR_P(rb));
416 RETURN_STR(message);
417 }
418 /* }}} */
419
420 /* {{{ ResourceBundle_class_functions
421 * Every 'ResourceBundle' class method has an entry in this table
422 */
423 static zend_function_entry ResourceBundle_class_functions[] = {
424 PHP_ME( ResourceBundle, __construct, arginfo_resourcebundle___construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR )
425 ZEND_NAMED_ME( create, ZEND_FN( resourcebundle_create ), arginfo_resourcebundle___construct, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC )
426 ZEND_NAMED_ME( get, ZEND_FN(resourcebundle_get), arginfo_resourcebundle_get, ZEND_ACC_PUBLIC )
427 ZEND_NAMED_ME( count, ZEND_FN(resourcebundle_count), arginfo_resourcebundle_count, ZEND_ACC_PUBLIC )
428 ZEND_NAMED_ME( getLocales, ZEND_FN(resourcebundle_locales), arginfo_resourcebundle_getlocales, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC )
429 ZEND_NAMED_ME( getErrorCode, ZEND_FN(resourcebundle_get_error_code), arginfo_resourcebundle_get_error_code, ZEND_ACC_PUBLIC )
430 ZEND_NAMED_ME( getErrorMessage, ZEND_FN(resourcebundle_get_error_message), arginfo_resourcebundle_get_error_message, ZEND_ACC_PUBLIC )
431 PHP_FE_END
432 };
433 /* }}} */
434
435 /* {{{ resourcebundle_register_class
436 * Initialize 'ResourceBundle' class
437 */
resourcebundle_register_class(void)438 void resourcebundle_register_class( void )
439 {
440 zend_class_entry ce;
441
442 INIT_CLASS_ENTRY( ce, "ResourceBundle", ResourceBundle_class_functions );
443
444 ce.create_object = ResourceBundle_object_create;
445 ce.get_iterator = resourcebundle_get_iterator;
446
447 ResourceBundle_ce_ptr = zend_register_internal_class( &ce );
448
449 if( !ResourceBundle_ce_ptr )
450 {
451 zend_error(E_ERROR, "Failed to register ResourceBundle class");
452 return;
453 }
454
455 ResourceBundle_object_handlers = std_object_handlers;
456 ResourceBundle_object_handlers.offset = XtOffsetOf(ResourceBundle_object, zend);
457 ResourceBundle_object_handlers.clone_obj = NULL; /* ICU ResourceBundle has no clone implementation */
458 ResourceBundle_object_handlers.dtor_obj = ResourceBundle_object_destroy;
459 ResourceBundle_object_handlers.read_dimension = resourcebundle_array_get;
460 ResourceBundle_object_handlers.count_elements = resourcebundle_array_count;
461
462 zend_class_implements(ResourceBundle_ce_ptr, 1, zend_ce_traversable);
463 }
464 /* }}} */
465
466 /*
467 * Local variables:
468 * tab-width: 4
469 * c-basic-offset: 4
470 * End:
471 * vim600: noet sw=4 ts=4 fdm=marker
472 * vim<600: noet sw=4 ts=4
473 */
474