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