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 
21 #include "php_intl.h"
22 #include "msgformat_class.h"
23 #include "msgformat_data.h"
24 #include "msgformat_helpers.h"
25 #include "intl_convert.h"
26 
27 #ifndef Z_ADDREF_P
28 #define Z_ADDREF_P(z) ((z)->refcount++)
29 #endif
30 
31 /* {{{ */
msgfmt_do_format(MessageFormatter_object * mfo,zval * args,zval * return_value)32 static void msgfmt_do_format(MessageFormatter_object *mfo, zval *args, zval *return_value)
33 {
34 	UChar* formatted = NULL;
35 	int32_t formatted_len = 0;
36 
37 	umsg_format_helper(mfo, Z_ARRVAL_P(args), &formatted, &formatted_len);
38 
39 	if (U_FAILURE(INTL_DATA_ERROR_CODE(mfo))) {
40 		if (formatted) {
41 			efree(formatted);
42 		}
43 		RETURN_FALSE;
44 	} else {
45 		INTL_METHOD_RETVAL_UTF8(mfo, formatted, formatted_len, 1);
46 	}
47 }
48 /* }}} */
49 
50 /* {{{ Format a message. */
PHP_FUNCTION(msgfmt_format)51 PHP_FUNCTION( msgfmt_format )
52 {
53 	zval *args;
54 	MSG_FORMAT_METHOD_INIT_VARS;
55 
56 
57 	/* Parse parameters. */
58 	if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "Oa",
59 		&object, MessageFormatter_ce_ptr,  &args ) == FAILURE )
60 	{
61 		RETURN_THROWS();
62 	}
63 
64 	/* Fetch the object. */
65 	MSG_FORMAT_METHOD_FETCH_OBJECT;
66 
67 	msgfmt_do_format(mfo, args, return_value);
68 }
69 /* }}} */
70 
71 /* {{{ Format a message. */
PHP_FUNCTION(msgfmt_format_message)72 PHP_FUNCTION( msgfmt_format_message )
73 {
74 	zval       *args;
75 	UChar      *spattern = NULL;
76 	int         spattern_len = 0;
77 	char       *pattern = NULL;
78 	size_t      pattern_len = 0;
79 	const char *slocale = NULL;
80 	size_t      slocale_len = 0;
81 	MessageFormatter_object mf;
82 	MessageFormatter_object *mfo = &mf;
83 	UParseError parse_error;
84 
85 	/* Parse parameters. */
86 	if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "ssa",
87 		  &slocale, &slocale_len, &pattern, &pattern_len, &args ) == FAILURE )
88 	{
89 		RETURN_THROWS();
90 	}
91 
92 	INTL_CHECK_LOCALE_LEN(slocale_len);
93 
94 	memset(mfo, 0, sizeof(*mfo));
95 	msgformat_data_init(&mfo->mf_data);
96 
97 	if(pattern && pattern_len) {
98 		intl_convert_utf8_to_utf16(&spattern, &spattern_len, pattern, pattern_len, &INTL_DATA_ERROR_CODE(mfo));
99 		if( U_FAILURE(INTL_DATA_ERROR_CODE((mfo))) )
100 		{
101 			intl_error_set(/* intl_error* */ NULL, U_ILLEGAL_ARGUMENT_ERROR,
102 				"msgfmt_format_message: error converting pattern to UTF-16", 0 );
103 			RETURN_FALSE;
104 		}
105 	} else {
106 		spattern_len = 0;
107 		spattern = NULL;
108 	}
109 
110 	if(slocale_len == 0) {
111 		slocale = intl_locale_get_default();
112 	}
113 
114 #ifdef MSG_FORMAT_QUOTE_APOS
115 	if(msgformat_fix_quotes(&spattern, &spattern_len, &INTL_DATA_ERROR_CODE(mfo)) != SUCCESS) {
116 		intl_error_set(/* intl_error* */ NULL, U_INVALID_FORMAT_ERROR,
117 			"msgfmt_format_message: error converting pattern to quote-friendly format", 0 );
118 		RETURN_FALSE;
119 	}
120 #endif
121 
122 	/* Create an ICU message formatter. */
123 	MSG_FORMAT_OBJECT(mfo) = umsg_open(spattern, spattern_len, slocale, &parse_error, &INTL_DATA_ERROR_CODE(mfo));
124 	if(spattern && spattern_len) {
125 		efree(spattern);
126 	}
127 
128 	/* Cannot use INTL_METHOD_CHECK_STATUS() as we need to free the message object formatter */
129 	if (U_FAILURE(INTL_DATA_ERROR_CODE(mfo))) {
130 		if (INTL_DATA_ERROR_CODE( mfo ) == U_PATTERN_SYNTAX_ERROR) {
131 			char *msg = NULL;
132 			smart_str parse_error_str;
133 			parse_error_str = intl_parse_error_to_string( &parse_error );
134 			spprintf( &msg, 0, "pattern syntax error (%s)", parse_error_str.s? ZSTR_VAL(parse_error_str.s) : "unknown parser error" );
135 			smart_str_free( &parse_error_str );
136 
137 			/* Pass NULL to intl_error* parameter to store message in global Intl error msg stack */
138 			intl_error_set_code(/* intl_error* */ NULL, INTL_DATA_ERROR_CODE( mfo ) );
139 			intl_errors_set_custom_msg(/* intl_error* */ NULL, msg, 1 );
140 
141 			efree( msg );
142 		} else {
143 			intl_errors_set_custom_msg(/* intl_error* */ NULL, "Creating message formatter failed", 0 );
144 		}
145 		umsg_close(MSG_FORMAT_OBJECT(mfo));
146 		RETURN_FALSE;
147     }
148 
149 	msgfmt_do_format(mfo, args, return_value);
150 
151 	/* drop the temporary formatter */
152 	msgformat_data_free(&mfo->mf_data);
153 }
154 /* }}} */
155