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