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_free */
ResourceBundle_object_free(zend_object * object)39 static void ResourceBundle_object_free( 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 	zend_object_std_dtor( &rb->zend );
54 }
55 /* }}} */
56 
57 /* {{{ ResourceBundle_object_create */
ResourceBundle_object_create(zend_class_entry * ce)58 static zend_object *ResourceBundle_object_create( zend_class_entry *ce )
59 {
60 	ResourceBundle_object *rb;
61 
62 	rb = zend_object_alloc(sizeof(ResourceBundle_object), ce);
63 
64 	zend_object_std_init( &rb->zend, ce );
65 	object_properties_init( &rb->zend, ce);
66 
67 	intl_error_init( INTL_DATA_ERROR_P(rb) );
68 	rb->me = NULL;
69 	rb->child = NULL;
70 
71 	rb->zend.handlers = &ResourceBundle_object_handlers;
72 
73 	return &rb->zend;
74 }
75 /* }}} */
76 
77 /* {{{ ResourceBundle_ctor */
resourcebundle_ctor(INTERNAL_FUNCTION_PARAMETERS,zend_bool is_constructor)78 static int resourcebundle_ctor(INTERNAL_FUNCTION_PARAMETERS, zend_bool is_constructor)
79 {
80 	const char *bundlename;
81 	size_t		bundlename_len = 0;
82 	const char *locale;
83 	size_t		locale_len = 0;
84 	zend_bool	fallback = 1;
85 	int         zpp_flags = is_constructor ? ZEND_PARSE_PARAMS_THROW : 0;
86 
87 	zval                  *object = return_value;
88 	ResourceBundle_object *rb = Z_INTL_RESOURCEBUNDLE_P( object );
89 
90 	intl_error_reset( NULL );
91 
92 	if( zend_parse_parameters_ex( zpp_flags, ZEND_NUM_ARGS(), "s!s!|b",
93 		&locale, &locale_len, &bundlename, &bundlename_len, &fallback ) == FAILURE )
94 	{
95 		intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
96 			"resourcebundle_ctor: unable to parse input parameters", 0 );
97 		return FAILURE;
98 	}
99 
100 	INTL_CHECK_LOCALE_LEN_OR_FAILURE(locale_len);
101 
102 	if (locale == NULL) {
103 		locale = intl_locale_get_default();
104 	}
105 
106 	if (bundlename_len >= MAXPATHLEN) {
107 		intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,	"Bundle name too long", 0 );
108 		zval_ptr_dtor(return_value);
109 		ZVAL_NULL(return_value);
110 		return FAILURE;
111 	}
112 
113 	if (fallback) {
114 		rb->me = ures_open(bundlename, locale, &INTL_DATA_ERROR_CODE(rb));
115 	} else {
116 		rb->me = ures_openDirect(bundlename, locale, &INTL_DATA_ERROR_CODE(rb));
117 	}
118 
119 	INTL_CTOR_CHECK_STATUS(rb, "resourcebundle_ctor: Cannot load libICU resource bundle");
120 
121 	if (!fallback && (INTL_DATA_ERROR_CODE(rb) == U_USING_FALLBACK_WARNING ||
122 			INTL_DATA_ERROR_CODE(rb) == U_USING_DEFAULT_WARNING)) {
123 		char *pbuf;
124 		intl_errors_set_code(NULL, INTL_DATA_ERROR_CODE(rb));
125 		spprintf(&pbuf, 0, "resourcebundle_ctor: Cannot load libICU resource "
126 				"'%s' without fallback from %s to %s",
127 				bundlename ? bundlename : "(default data)", locale,
128 				ures_getLocaleByType(
129 					rb->me, ULOC_ACTUAL_LOCALE, &INTL_DATA_ERROR_CODE(rb)));
130 		intl_errors_set_custom_msg(INTL_DATA_ERROR_P(rb), pbuf, 1);
131 		efree(pbuf);
132 		return FAILURE;
133 	}
134 
135 	return SUCCESS;
136 }
137 /* }}} */
138 
139 /* {{{ arginfo_resourcebundle__construct */
140 ZEND_BEGIN_ARG_INFO_EX( arginfo_resourcebundle___construct, 0, 0, 2 )
141 	ZEND_ARG_INFO( 0, locale )
142 	ZEND_ARG_INFO( 0, bundlename )
143 	ZEND_ARG_INFO( 0, fallback )
ZEND_END_ARG_INFO()144 ZEND_END_ARG_INFO()
145 /* }}} */
146 
147 /* {{{ proto ResourceBundle::__construct( string $locale [, string $bundlename [, bool $fallback = true ]] )
148  * ResourceBundle object constructor
149  */
150 PHP_METHOD( ResourceBundle, __construct )
151 {
152 	zend_error_handling error_handling;
153 
154 	zend_replace_error_handling(EH_THROW, IntlException_ce_ptr, &error_handling);
155 	return_value = ZEND_THIS;
156 	if (resourcebundle_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1) == FAILURE) {
157 		if (!EG(exception)) {
158 			zend_throw_exception(IntlException_ce_ptr, "Constructor failed", 0);
159 		}
160 	}
161 	zend_restore_error_handling(&error_handling);
162 }
163 /* }}} */
164 
165 /* {{{ proto ResourceBundle ResourceBundle::create( string $locale [, string $bundlename [, bool $fallback = true ]] )
166 proto ResourceBundle resourcebundle_create( string $locale [, string $bundlename [, bool $fallback = true ]] )
167 */
PHP_FUNCTION(resourcebundle_create)168 PHP_FUNCTION( resourcebundle_create )
169 {
170 	object_init_ex( return_value, ResourceBundle_ce_ptr );
171 	if (resourcebundle_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0) == FAILURE) {
172 		zval_ptr_dtor(return_value);
173 		RETURN_NULL();
174 	}
175 }
176 /* }}} */
177 
178 /* {{{ resourcebundle_array_fetch */
resourcebundle_array_fetch(zval * object,zval * offset,zval * return_value,int fallback)179 static void resourcebundle_array_fetch(zval *object, zval *offset, zval *return_value, int fallback)
180 {
181 	int32_t     meindex = 0;
182 	char *      mekey = NULL;
183     zend_bool    is_numeric = 0;
184 	char         *pbuf;
185 	ResourceBundle_object *rb;
186 
187 	intl_error_reset( NULL );
188 	RESOURCEBUNDLE_METHOD_FETCH_OBJECT;
189 
190 	if(Z_TYPE_P(offset) == IS_LONG) {
191 		is_numeric = 1;
192 		meindex = (int32_t)Z_LVAL_P(offset);
193 		rb->child = ures_getByIndex( rb->me, meindex, rb->child, &INTL_DATA_ERROR_CODE(rb) );
194 	} else if(Z_TYPE_P(offset) == IS_STRING) {
195 		mekey = Z_STRVAL_P(offset);
196 		rb->child = ures_getByKey(rb->me, mekey, rb->child, &INTL_DATA_ERROR_CODE(rb) );
197 	} else {
198 		intl_errors_set(INTL_DATA_ERROR_P(rb), U_ILLEGAL_ARGUMENT_ERROR,
199 			"resourcebundle_get: index should be integer or string", 0);
200 		RETURN_NULL();
201 	}
202 
203 	intl_error_set_code( NULL, INTL_DATA_ERROR_CODE(rb) );
204 	if (U_FAILURE(INTL_DATA_ERROR_CODE(rb))) {
205 		if (is_numeric) {
206 			spprintf( &pbuf, 0, "Cannot load resource element %d", meindex );
207 		} else {
208 			spprintf( &pbuf, 0, "Cannot load resource element '%s'", mekey );
209 		}
210 		intl_errors_set_custom_msg( INTL_DATA_ERROR_P(rb), pbuf, 1 );
211 		efree(pbuf);
212 		RETURN_NULL();
213 	}
214 
215 	if (!fallback && (INTL_DATA_ERROR_CODE(rb) == U_USING_FALLBACK_WARNING || INTL_DATA_ERROR_CODE(rb) == U_USING_DEFAULT_WARNING)) {
216 		UErrorCode icuerror;
217 		const char * locale = ures_getLocaleByType( rb->me, ULOC_ACTUAL_LOCALE, &icuerror );
218 		if (is_numeric) {
219 			spprintf( &pbuf, 0, "Cannot load element %d without fallback from to %s", meindex, locale );
220 		} else {
221 			spprintf( &pbuf, 0, "Cannot load element '%s' without fallback from to %s", mekey, locale );
222 		}
223 		intl_errors_set_custom_msg( INTL_DATA_ERROR_P(rb), pbuf, 1 );
224 		efree(pbuf);
225 		RETURN_NULL();
226 	}
227 
228 	resourcebundle_extract_value( return_value, rb );
229 }
230 /* }}} */
231 
232 /* {{{ resourcebundle_array_get */
resourcebundle_array_get(zval * object,zval * offset,int type,zval * rv)233 zval *resourcebundle_array_get(zval *object, zval *offset, int type, zval *rv)
234 {
235 	if(offset == NULL) {
236 		php_error( E_ERROR, "Cannot apply [] to ResourceBundle object" );
237 	}
238 	ZVAL_NULL(rv);
239 	resourcebundle_array_fetch(object, offset, rv, 1);
240 	return rv;
241 }
242 /* }}} */
243 
244 /* {{{ arginfo_resourcebundle_get */
245 ZEND_BEGIN_ARG_INFO_EX( arginfo_resourcebundle_get, 0, 0, 1 )
246 	ZEND_ARG_INFO( 0, index )
247 	ZEND_ARG_INFO( 0, fallback )
ZEND_END_ARG_INFO()248 ZEND_END_ARG_INFO()
249 /* }}} */
250 
251 /* {{{ proto mixed ResourceBundle::get( int|string $resindex [, bool $fallback = true ] )
252  * proto mixed resourcebundle_get( ResourceBundle $rb, int|string $resindex [, bool $fallback = true ] )
253  * Get resource identified by numerical index or key name.
254  */
255 PHP_FUNCTION( resourcebundle_get )
256 {
257 	zend_bool   fallback = 1;
258 	zval *		offset;
259 	zval *      object;
260 
261 	if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Oz|b",	&object, ResourceBundle_ce_ptr, &offset, &fallback ) == FAILURE) {
262 		intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
263 			"resourcebundle_get: unable to parse input params", 0);
264 		RETURN_FALSE;
265 	}
266 
267 	resourcebundle_array_fetch(object, offset, return_value, fallback);
268 }
269 /* }}} */
270 
271 /* {{{ resourcebundle_array_count */
resourcebundle_array_count(zval * object,zend_long * count)272 int resourcebundle_array_count(zval *object, zend_long *count)
273 {
274 	ResourceBundle_object *rb;
275 	RESOURCEBUNDLE_METHOD_FETCH_OBJECT_NO_CHECK;
276 
277 	if (rb->me == NULL) {
278 		intl_errors_set(&rb->error, U_ILLEGAL_ARGUMENT_ERROR,
279 				"Found unconstructed ResourceBundle", 0);
280 		return 0;
281 	}
282 
283 	*count = ures_getSize( rb->me );
284 
285 	return SUCCESS;
286 }
287 /* }}} */
288 
289 /* {{{ arginfo_resourcebundle_count */
290 ZEND_BEGIN_ARG_INFO_EX( arginfo_resourcebundle_count, 0, 0, 0 )
ZEND_END_ARG_INFO()291 ZEND_END_ARG_INFO()
292 /* }}} */
293 
294 /* {{{ proto int ResourceBundle::count()
295  * proto int resourcebundle_count( ResourceBundle $bundle )
296  * Get resources count
297  */
298 PHP_FUNCTION( resourcebundle_count )
299 {
300 	int32_t                len;
301 	RESOURCEBUNDLE_METHOD_INIT_VARS;
302 
303 	if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "O", &object, ResourceBundle_ce_ptr ) == FAILURE ) {
304 		intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
305 			"resourcebundle_count: unable to parse input params", 0);
306 		RETURN_FALSE;
307 	}
308 
309 	RESOURCEBUNDLE_METHOD_FETCH_OBJECT;
310 
311 	len = ures_getSize( rb->me );
312 	RETURN_LONG( len );
313 }
314 
315 /* {{{ arginfo_resourcebundle_getlocales */
316 ZEND_BEGIN_ARG_INFO_EX( arginfo_resourcebundle_getlocales, 0, 0, 1 )
317 	ZEND_ARG_INFO( 0, bundlename )
ZEND_END_ARG_INFO()318 ZEND_END_ARG_INFO()
319 /* }}} */
320 
321 /* {{{ proto array ResourceBundle::getLocales( string $bundlename )
322  * proto array resourcebundle_locales( string $bundlename )
323  * Get available locales from ResourceBundle name
324  */
325 PHP_FUNCTION( resourcebundle_locales )
326 {
327 	char * bundlename;
328 	size_t    bundlename_len = 0;
329 	const char * entry;
330 	int entry_len;
331 	UEnumeration *icuenum;
332 	UErrorCode   icuerror = U_ZERO_ERROR;
333 
334 	intl_errors_reset( NULL );
335 
336 	if( zend_parse_parameters(ZEND_NUM_ARGS(), "s", &bundlename, &bundlename_len ) == FAILURE )
337 	{
338 		intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
339 			"resourcebundle_locales: unable to parse input params", 0);
340 		RETURN_FALSE;
341 	}
342 
343 	if (bundlename_len >= MAXPATHLEN) {
344 		intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,	"resourcebundle_locales: bundle name too long", 0 );
345 		RETURN_FALSE;
346 	}
347 
348 	if(bundlename_len == 0) {
349 		// fetch default locales list
350 		bundlename = NULL;
351 	}
352 
353 	icuenum = ures_openAvailableLocales( bundlename, &icuerror );
354 	INTL_CHECK_STATUS(icuerror, "Cannot fetch locales list");
355 
356 	uenum_reset( icuenum, &icuerror );
357 	INTL_CHECK_STATUS(icuerror, "Cannot iterate locales list");
358 
359 	array_init( return_value );
360 	while ((entry = uenum_next( icuenum, &entry_len, &icuerror ))) {
361 		add_next_index_stringl( return_value, (char *) entry, entry_len);
362 	}
363 	uenum_close( icuenum );
364 }
365 /* }}} */
366 
367 /* {{{ arginfo_resourcebundle_get_error_code */
368 ZEND_BEGIN_ARG_INFO_EX( arginfo_resourcebundle_get_error_code, 0, 0, 0 )
ZEND_END_ARG_INFO()369 ZEND_END_ARG_INFO()
370 /* }}} */
371 
372 /* {{{ proto string ResourceBundle::getErrorCode( )
373  * proto string resourcebundle_get_error_code( ResourceBundle $bundle )
374  * Get text description for ResourceBundle's last error code.
375  */
376 PHP_FUNCTION( resourcebundle_get_error_code )
377 {
378 	RESOURCEBUNDLE_METHOD_INIT_VARS;
379 
380 	if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "O",
381 		&object, ResourceBundle_ce_ptr ) == FAILURE )
382 	{
383 		intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
384 			"resourcebundle_get_error_code: unable to parse input params", 0 );
385 		RETURN_FALSE;
386 	}
387 
388 	rb = Z_INTL_RESOURCEBUNDLE_P( object );
389 
390 	RETURN_LONG(INTL_DATA_ERROR_CODE(rb));
391 }
392 /* }}} */
393 
394 /* {{{ arginfo_resourcebundle_get_error_message */
395 ZEND_BEGIN_ARG_INFO_EX( arginfo_resourcebundle_get_error_message, 0, 0, 0 )
ZEND_END_ARG_INFO()396 ZEND_END_ARG_INFO()
397 /* }}} */
398 
399 /* {{{ proto string ResourceBundle::getErrorMessage( )
400  * proto string resourcebundle_get_error_message( ResourceBundle $bundle )
401  * Get text description for ResourceBundle's last error.
402  */
403 PHP_FUNCTION( resourcebundle_get_error_message )
404 {
405 	zend_string* message = NULL;
406 	RESOURCEBUNDLE_METHOD_INIT_VARS;
407 
408 	if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "O",
409 		&object, ResourceBundle_ce_ptr ) == FAILURE )
410 	{
411 		intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
412 			"resourcebundle_get_error_message: unable to parse input params", 0 );
413 		RETURN_FALSE;
414 	}
415 
416 	rb = Z_INTL_RESOURCEBUNDLE_P( object );
417 	message = intl_error_get_message(INTL_DATA_ERROR_P(rb));
418 	RETURN_STR(message);
419 }
420 /* }}} */
421 
422 /* {{{ ResourceBundle_class_functions
423  * Every 'ResourceBundle' class method has an entry in this table
424  */
425 static const zend_function_entry ResourceBundle_class_functions[] = {
426 	PHP_ME( ResourceBundle, __construct, arginfo_resourcebundle___construct, ZEND_ACC_PUBLIC )
427 	ZEND_NAMED_ME( create, ZEND_FN( resourcebundle_create ), arginfo_resourcebundle___construct, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC )
428 	ZEND_NAMED_ME( get, ZEND_FN(resourcebundle_get), arginfo_resourcebundle_get, ZEND_ACC_PUBLIC )
429 	ZEND_NAMED_ME( count, ZEND_FN(resourcebundle_count), arginfo_resourcebundle_count, ZEND_ACC_PUBLIC )
430 	ZEND_NAMED_ME( getLocales, ZEND_FN(resourcebundle_locales), arginfo_resourcebundle_getlocales, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC )
431 	ZEND_NAMED_ME( getErrorCode, ZEND_FN(resourcebundle_get_error_code), arginfo_resourcebundle_get_error_code, ZEND_ACC_PUBLIC )
432 	ZEND_NAMED_ME( getErrorMessage, ZEND_FN(resourcebundle_get_error_message), arginfo_resourcebundle_get_error_message, ZEND_ACC_PUBLIC )
433 	PHP_FE_END
434 };
435 /* }}} */
436 
437 /* {{{ resourcebundle_register_class
438  * Initialize 'ResourceBundle' class
439  */
resourcebundle_register_class(void)440 void resourcebundle_register_class( void )
441 {
442 	zend_class_entry ce;
443 
444 	INIT_CLASS_ENTRY( ce, "ResourceBundle", ResourceBundle_class_functions );
445 
446 	ce.create_object = ResourceBundle_object_create;
447 	ce.get_iterator = resourcebundle_get_iterator;
448 
449 	ResourceBundle_ce_ptr = zend_register_internal_class( &ce );
450 
451 	ResourceBundle_object_handlers = std_object_handlers;
452 	ResourceBundle_object_handlers.offset = XtOffsetOf(ResourceBundle_object, zend);
453 	ResourceBundle_object_handlers.clone_obj	  = NULL; /* ICU ResourceBundle has no clone implementation */
454 	ResourceBundle_object_handlers.free_obj = ResourceBundle_object_free;
455 	ResourceBundle_object_handlers.read_dimension = resourcebundle_array_get;
456 	ResourceBundle_object_handlers.count_elements = resourcebundle_array_count;
457 
458 	zend_class_implements(ResourceBundle_ce_ptr, 2, zend_ce_traversable, zend_ce_countable);
459 }
460 /* }}} */
461