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 | http://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: Vadim Savchuk <vsavchuk@productengine.com> |
12 | Dmitry Lakhtyuk <dlakhtyuk@productengine.com> |
13 | Stanislav Malyshev <stas@zend.com> |
14 +----------------------------------------------------------------------+
15 */
16
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20
21 #include <php.h>
22 #include <zend_exceptions.h>
23
24 #include "php_intl.h"
25 #include "intl_error.h"
26 #include "intl_convert.h"
27
ZEND_EXTERN_MODULE_GLOBALS(intl)28 ZEND_EXTERN_MODULE_GLOBALS( intl )
29
30 zend_class_entry *IntlException_ce_ptr;
31
32 /* {{{ Return global error structure. */
33 static intl_error* intl_g_error_get( void )
34 {
35 return &INTL_G( g_error );
36 }
37 /* }}} */
38
39 /* {{{ Free mem. */
intl_free_custom_error_msg(intl_error * err)40 static void intl_free_custom_error_msg( intl_error* err )
41 {
42 if( !err && !( err = intl_g_error_get( ) ) )
43 return;
44
45 if(err->free_custom_error_message ) {
46 efree( err->custom_error_message );
47 }
48
49 err->custom_error_message = NULL;
50 err->free_custom_error_message = 0;
51 }
52 /* }}} */
53
54 /* {{{ Create and initialize internals of 'intl_error'. */
intl_error_create(void)55 intl_error* intl_error_create( void )
56 {
57 intl_error* err = ecalloc( 1, sizeof( intl_error ) );
58
59 intl_error_init( err );
60
61 return err;
62 }
63 /* }}} */
64
65 /* {{{ Initialize internals of 'intl_error'. */
intl_error_init(intl_error * err)66 void intl_error_init( intl_error* err )
67 {
68 if( !err && !( err = intl_g_error_get( ) ) )
69 return;
70
71 err->code = U_ZERO_ERROR;
72 err->custom_error_message = NULL;
73 err->free_custom_error_message = 0;
74 }
75 /* }}} */
76
77 /* {{{ Set last error code to 0 and unset last error message */
intl_error_reset(intl_error * err)78 void intl_error_reset( intl_error* err )
79 {
80 if( !err && !( err = intl_g_error_get( ) ) )
81 return;
82
83 err->code = U_ZERO_ERROR;
84
85 intl_free_custom_error_msg( err );
86 }
87 /* }}} */
88
89 /* {{{ Set last error message to msg copying it if needed. */
intl_error_set_custom_msg(intl_error * err,const char * msg,int copyMsg)90 void intl_error_set_custom_msg( intl_error* err, const char* msg, int copyMsg )
91 {
92 if( !msg )
93 return;
94
95 if( !err ) {
96 if( INTL_G( error_level ) )
97 php_error_docref( NULL, INTL_G( error_level ), "%s", msg );
98 if( INTL_G( use_exceptions ) )
99 zend_throw_exception_ex( IntlException_ce_ptr, 0, "%s", msg );
100 }
101 if( !err && !( err = intl_g_error_get( ) ) )
102 return;
103
104 /* Free previous message if any */
105 intl_free_custom_error_msg( err );
106
107 /* Mark message copied if any */
108 err->free_custom_error_message = copyMsg;
109
110 /* Set user's error text message */
111 err->custom_error_message = copyMsg ? estrdup( msg ) : (char *) msg;
112 }
113 /* }}} */
114
115 /* {{{ Create output message in format "<intl_error_text>: <extra_user_error_text>". */
intl_error_get_message(intl_error * err)116 zend_string * intl_error_get_message( intl_error* err )
117 {
118 const char *uErrorName = NULL;
119 zend_string *errMessage = 0;
120
121 if( !err && !( err = intl_g_error_get( ) ) )
122 return ZSTR_EMPTY_ALLOC();
123
124 uErrorName = u_errorName( err->code );
125
126 /* Format output string */
127 if( err->custom_error_message )
128 {
129 errMessage = strpprintf(0, "%s: %s", err->custom_error_message, uErrorName );
130 }
131 else
132 {
133 errMessage = strpprintf(0, "%s", uErrorName );
134 }
135
136 return errMessage;
137 }
138 /* }}} */
139
140 /* {{{ Set last error code. */
intl_error_set_code(intl_error * err,UErrorCode err_code)141 void intl_error_set_code( intl_error* err, UErrorCode err_code )
142 {
143 if( !err && !( err = intl_g_error_get( ) ) )
144 return;
145
146 err->code = err_code;
147 }
148 /* }}} */
149
150 /* {{{ Return last error code. */
intl_error_get_code(intl_error * err)151 UErrorCode intl_error_get_code( intl_error* err )
152 {
153 if( !err && !( err = intl_g_error_get( ) ) )
154 return U_ZERO_ERROR;
155
156 return err->code;
157 }
158 /* }}} */
159
160 /* {{{ Set error code and message. */
intl_error_set(intl_error * err,UErrorCode code,const char * msg,int copyMsg)161 void intl_error_set( intl_error* err, UErrorCode code, const char* msg, int copyMsg )
162 {
163 intl_error_set_code( err, code );
164 intl_error_set_custom_msg( err, msg, copyMsg );
165 }
166 /* }}} */
167
168 /* {{{ Set error code and message. */
intl_errors_set(intl_error * err,UErrorCode code,const char * msg,int copyMsg)169 void intl_errors_set( intl_error* err, UErrorCode code, const char* msg, int copyMsg )
170 {
171 intl_errors_set_code( err, code );
172 intl_errors_set_custom_msg( err, msg, copyMsg );
173 }
174 /* }}} */
175
176 /* {{{ */
intl_errors_reset(intl_error * err)177 void intl_errors_reset( intl_error* err )
178 {
179 if(err) {
180 intl_error_reset( err );
181 }
182 intl_error_reset( NULL );
183 }
184 /* }}} */
185
186 /* {{{ */
intl_errors_set_custom_msg(intl_error * err,const char * msg,int copyMsg)187 void intl_errors_set_custom_msg( intl_error* err, const char* msg, int copyMsg )
188 {
189 if(err) {
190 intl_error_set_custom_msg( err, msg, copyMsg );
191 }
192 intl_error_set_custom_msg( NULL, msg, copyMsg );
193 }
194 /* }}} */
195
196 /* {{{ */
intl_errors_set_code(intl_error * err,UErrorCode err_code)197 void intl_errors_set_code( intl_error* err, UErrorCode err_code )
198 {
199 if(err) {
200 intl_error_set_code( err, err_code );
201 }
202 intl_error_set_code( NULL, err_code );
203 }
204 /* }}} */
205
intl_register_IntlException_class(void)206 void intl_register_IntlException_class( void )
207 {
208 zend_class_entry ce;
209
210 /* Create and register 'IntlException' class. */
211 INIT_CLASS_ENTRY_EX( ce, "IntlException", sizeof( "IntlException" ) - 1, NULL );
212 IntlException_ce_ptr = zend_register_internal_class_ex( &ce,
213 zend_ce_exception );
214 IntlException_ce_ptr->create_object = zend_ce_exception->create_object;
215 }
216
intl_parse_error_to_string(UParseError * pe)217 smart_str intl_parse_error_to_string( UParseError* pe )
218 {
219 smart_str ret = {0};
220 zend_string *u8str;
221 UErrorCode status;
222 int any = 0;
223
224 assert( pe != NULL );
225
226 smart_str_appends( &ret, "parse error " );
227 if( pe->line > 0 )
228 {
229 smart_str_appends( &ret, "on line " );
230 smart_str_append_long( &ret, (zend_long ) pe->line );
231 any = 1;
232 }
233 if( pe->offset >= 0 ) {
234 if( any )
235 smart_str_appends( &ret, ", " );
236 else
237 smart_str_appends( &ret, "at " );
238
239 smart_str_appends( &ret, "offset " );
240 smart_str_append_long( &ret, (zend_long ) pe->offset );
241 any = 1;
242 }
243
244 if (pe->preContext[0] != 0 ) {
245 if( any )
246 smart_str_appends( &ret, ", " );
247
248 smart_str_appends( &ret, "after \"" );
249 u8str = intl_convert_utf16_to_utf8(pe->preContext, -1, &status );
250 if( !u8str )
251 {
252 smart_str_appends( &ret, "(could not convert parser error pre-context to UTF-8)" );
253 }
254 else {
255 smart_str_append( &ret, u8str );
256 zend_string_release_ex( u8str, 0 );
257 }
258 smart_str_appends( &ret, "\"" );
259 any = 1;
260 }
261
262 if( pe->postContext[0] != 0 )
263 {
264 if( any )
265 smart_str_appends( &ret, ", " );
266
267 smart_str_appends( &ret, "before or at \"" );
268 u8str = intl_convert_utf16_to_utf8(pe->postContext, -1, &status );
269 if( !u8str )
270 {
271 smart_str_appends( &ret, "(could not convert parser error post-context to UTF-8)" );
272 }
273 else
274 {
275 smart_str_append( &ret, u8str );
276 zend_string_release_ex( u8str, 0 );
277 }
278 smart_str_appends( &ret, "\"" );
279 any = 1;
280 }
281
282 if( !any )
283 {
284 smart_str_free( &ret );
285 smart_str_appends( &ret, "no parse error" );
286 }
287
288 smart_str_0( &ret );
289 return ret;
290 }
291