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: Stanislav Malyshev <stas@zend.com> |
12 +----------------------------------------------------------------------+
13 */
14
15 #ifdef HAVE_CONFIG_H
16 #include <config.h>
17 #endif
18
19 #include <unicode/ustring.h>
20 #include <unicode/umsg.h>
21
22 #include "php_intl.h"
23 #include "msgformat_class.h"
24 #include "msgformat_data.h"
25 #include "intl_convert.h"
26
27 /* {{{ */
msgfmt_ctor(INTERNAL_FUNCTION_PARAMETERS,zend_error_handling * error_handling,bool * error_handling_replaced)28 static int msgfmt_ctor(INTERNAL_FUNCTION_PARAMETERS, zend_error_handling *error_handling, bool *error_handling_replaced)
29 {
30 char* locale;
31 char* pattern;
32 size_t locale_len = 0, pattern_len = 0;
33 UChar* spattern = NULL;
34 int spattern_len = 0;
35 zval* object;
36 MessageFormatter_object* mfo;
37 UParseError parse_error;
38 intl_error_reset( NULL );
39
40 object = return_value;
41 ZEND_PARSE_PARAMETERS_START(2, 2)
42 Z_PARAM_STRING(locale, locale_len)
43 Z_PARAM_STRING(pattern, pattern_len)
44 ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
45
46 if (error_handling != NULL) {
47 zend_replace_error_handling(EH_THROW, IntlException_ce_ptr, error_handling);
48 *error_handling_replaced = 1;
49 }
50
51 INTL_CHECK_LOCALE_LEN_OR_FAILURE(locale_len);
52 MSG_FORMAT_METHOD_FETCH_OBJECT_NO_CHECK;
53
54 /* Convert pattern (if specified) to UTF-16. */
55 if(pattern && pattern_len) {
56 intl_convert_utf8_to_utf16(&spattern, &spattern_len, pattern, pattern_len, &INTL_DATA_ERROR_CODE(mfo));
57 INTL_CTOR_CHECK_STATUS(mfo, "msgfmt_create: error converting pattern to UTF-16");
58 } else {
59 spattern_len = 0;
60 spattern = NULL;
61 }
62
63 if(locale_len == 0) {
64 locale = (char *)intl_locale_get_default();
65 }
66
67 #ifdef MSG_FORMAT_QUOTE_APOS
68 if(msgformat_fix_quotes(&spattern, &spattern_len, &INTL_DATA_ERROR_CODE(mfo)) != SUCCESS) {
69 INTL_CTOR_CHECK_STATUS(mfo, "msgfmt_create: error converting pattern to quote-friendly format");
70 }
71 #endif
72
73 if ((mfo)->mf_data.orig_format) {
74 msgformat_data_free(&mfo->mf_data);
75 }
76
77 (mfo)->mf_data.orig_format = estrndup(pattern, pattern_len);
78 (mfo)->mf_data.orig_format_len = pattern_len;
79
80 /* Create an ICU message formatter. */
81 MSG_FORMAT_OBJECT(mfo) = umsg_open(spattern, spattern_len, locale, &parse_error, &INTL_DATA_ERROR_CODE(mfo));
82
83 if(spattern) {
84 efree(spattern);
85 }
86
87 if (INTL_DATA_ERROR_CODE( mfo ) == U_PATTERN_SYNTAX_ERROR) {
88 char *msg = NULL;
89 smart_str parse_error_str;
90 parse_error_str = intl_parse_error_to_string( &parse_error );
91 spprintf( &msg, 0, "pattern syntax error (%s)", parse_error_str.s? ZSTR_VAL(parse_error_str.s) : "unknown parser error" );
92 smart_str_free( &parse_error_str );
93
94 intl_error_set_code( NULL, INTL_DATA_ERROR_CODE( mfo ) );
95 intl_errors_set_custom_msg( INTL_DATA_ERROR_P( mfo ), msg, 1 );
96
97 efree( msg );
98 return FAILURE;
99 }
100
101 INTL_CTOR_CHECK_STATUS(mfo, "msgfmt_create: message formatter creation failed");
102 return SUCCESS;
103 }
104 /* }}} */
105
106 /* {{{ Create formatter. */
PHP_FUNCTION(msgfmt_create)107 PHP_FUNCTION( msgfmt_create )
108 {
109 object_init_ex( return_value, MessageFormatter_ce_ptr );
110 if (msgfmt_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, NULL, NULL) == FAILURE) {
111 zval_ptr_dtor(return_value);
112 RETURN_NULL();
113 }
114 }
115 /* }}} */
116
117 /* {{{ MessageFormatter object constructor. */
PHP_METHOD(MessageFormatter,__construct)118 PHP_METHOD( MessageFormatter, __construct )
119 {
120 zend_error_handling error_handling;
121 bool error_handling_replaced = 0;
122
123 return_value = ZEND_THIS;
124 if (msgfmt_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, &error_handling, &error_handling_replaced) == FAILURE) {
125 if (!EG(exception)) {
126 zend_string *err = intl_error_get_message(NULL);
127 zend_throw_exception(IntlException_ce_ptr, ZSTR_VAL(err), intl_error_get_code(NULL));
128 zend_string_release_ex(err, 0);
129 }
130 }
131 if (error_handling_replaced) {
132 zend_restore_error_handling(&error_handling);
133 }
134 }
135 /* }}} */
136
137 /* {{{ Get formatter's last error code. */
PHP_FUNCTION(msgfmt_get_error_code)138 PHP_FUNCTION( msgfmt_get_error_code )
139 {
140 zval* object = NULL;
141 MessageFormatter_object* mfo = NULL;
142
143 /* Parse parameters. */
144 if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "O",
145 &object, MessageFormatter_ce_ptr ) == FAILURE )
146 {
147 RETURN_THROWS();
148 }
149
150 mfo = Z_INTL_MESSAGEFORMATTER_P( object );
151
152 /* Return formatter's last error code. */
153 RETURN_LONG( INTL_DATA_ERROR_CODE(mfo) );
154 }
155 /* }}} */
156
157 /* {{{ Get text description for formatter's last error code. */
PHP_FUNCTION(msgfmt_get_error_message)158 PHP_FUNCTION( msgfmt_get_error_message )
159 {
160 zend_string* message = NULL;
161 zval* object = NULL;
162 MessageFormatter_object* mfo = NULL;
163
164 /* Parse parameters. */
165 if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "O",
166 &object, MessageFormatter_ce_ptr ) == FAILURE )
167 {
168 RETURN_THROWS();
169 }
170
171 mfo = Z_INTL_MESSAGEFORMATTER_P( object );
172
173 /* Return last error message. */
174 message = intl_error_get_message( &mfo->mf_data.error );
175 RETURN_STR(message);
176 }
177 /* }}} */
178