xref: /php-src/ext/gettext/gettext.c (revision 5823a96f)
1 /*
2    +----------------------------------------------------------------------+
3    | Copyright (c) The PHP Group                                          |
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    | https://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    | Author: Alex Plotnick <alex@wgate.com>                               |
14    +----------------------------------------------------------------------+
15  */
16 
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20 
21 #include "php.h"
22 
23 #ifdef HAVE_LIBINTL
24 
25 #include <stdio.h>
26 #include <locale.h>
27 #include "ext/standard/info.h"
28 #include "php_gettext.h"
29 #include "gettext_arginfo.h"
30 
31 #include <libintl.h>
32 
33 zend_module_entry php_gettext_module_entry = {
34 	STANDARD_MODULE_HEADER,
35 	"gettext",
36 	ext_functions,
37 	NULL,
38 	NULL,
39 	NULL,
40 	NULL,
41 	PHP_MINFO(php_gettext),
42 	PHP_GETTEXT_VERSION,
43 	STANDARD_MODULE_PROPERTIES
44 };
45 
46 #ifdef COMPILE_DL_GETTEXT
47 ZEND_GET_MODULE(php_gettext)
48 #endif
49 
50 #define PHP_GETTEXT_MAX_DOMAIN_LENGTH 1024
51 #define PHP_GETTEXT_MAX_MSGID_LENGTH 4096
52 
53 #define PHP_GETTEXT_DOMAIN_LENGTH_CHECK(_arg_num, domain_len) \
54 	if (UNEXPECTED(domain_len > PHP_GETTEXT_MAX_DOMAIN_LENGTH)) { \
55 		zend_argument_value_error(_arg_num, "is too long"); \
56 		RETURN_THROWS(); \
57 	} else if (domain_len == 0) { \
58 		zend_argument_value_error(_arg_num, "cannot be empty"); \
59 		RETURN_THROWS(); \
60 	}
61 
62 #define PHP_GETTEXT_LENGTH_CHECK(_arg_num, check_len) \
63 	if (UNEXPECTED(check_len > PHP_GETTEXT_MAX_MSGID_LENGTH)) { \
64 		zend_argument_value_error(_arg_num, "is too long"); \
65 		RETURN_THROWS(); \
66 	}
67 
68 #define PHP_DCGETTEXT_CATEGORY_CHECK(_arg_num, category) \
69 	if (category == LC_ALL) { \
70 		zend_argument_value_error(_arg_num, "cannot be LC_ALL"); \
71 		RETURN_THROWS(); \
72 	}
73 
PHP_MINFO_FUNCTION(php_gettext)74 PHP_MINFO_FUNCTION(php_gettext)
75 {
76 	php_info_print_table_start();
77 	php_info_print_table_row(2, "GetText Support", "enabled");
78 	php_info_print_table_end();
79 }
80 
81 /* {{{ Set the textdomain to "domain". Returns the current domain */
PHP_FUNCTION(textdomain)82 PHP_FUNCTION(textdomain)
83 {
84 	char *domain_name = NULL, *retval = NULL;
85 	zend_string *domain = NULL;
86 
87 	ZEND_PARSE_PARAMETERS_START(0, 1)
88 		Z_PARAM_OPTIONAL
89 		Z_PARAM_STR_OR_NULL(domain)
90 	ZEND_PARSE_PARAMETERS_END();
91 
92 	if (domain != NULL) {
93 		PHP_GETTEXT_DOMAIN_LENGTH_CHECK(1, ZSTR_LEN(domain))
94 		if (zend_string_equals_literal(domain, "0")) {
95 			zend_argument_value_error(1, "cannot be zero");
96 			RETURN_THROWS();
97 		}
98 		domain_name = ZSTR_VAL(domain);
99 	}
100 
101 	retval = textdomain(domain_name);
102 
103 	RETURN_STRING(retval);
104 }
105 /* }}} */
106 
107 /* {{{ Return the translation of msgid for the current domain, or msgid unaltered if a translation does not exist */
PHP_FUNCTION(gettext)108 PHP_FUNCTION(gettext)
109 {
110 	char *msgstr = NULL;
111 	zend_string *msgid;
112 
113 	ZEND_PARSE_PARAMETERS_START(1, 1)
114 		Z_PARAM_STR(msgid)
115 	ZEND_PARSE_PARAMETERS_END();
116 
117 	PHP_GETTEXT_LENGTH_CHECK(1, ZSTR_LEN(msgid))
118 	msgstr = gettext(ZSTR_VAL(msgid));
119 
120 	if (msgstr != ZSTR_VAL(msgid)) {
121 		RETURN_STRING(msgstr);
122 	} else {
123 		RETURN_STR_COPY(msgid);
124 	}
125 }
126 /* }}} */
127 
128 /* {{{ Return the translation of msgid for domain_name, or msgid unaltered if a translation does not exist */
PHP_FUNCTION(dgettext)129 PHP_FUNCTION(dgettext)
130 {
131 	char *msgstr = NULL;
132 	zend_string *domain, *msgid;
133 
134 	ZEND_PARSE_PARAMETERS_START(2, 2)
135 		Z_PARAM_STR(domain)
136 		Z_PARAM_STR(msgid)
137 	ZEND_PARSE_PARAMETERS_END();
138 
139 	PHP_GETTEXT_DOMAIN_LENGTH_CHECK(1, ZSTR_LEN(domain))
140 	PHP_GETTEXT_LENGTH_CHECK(2, ZSTR_LEN(msgid))
141 
142 	msgstr = dgettext(ZSTR_VAL(domain), ZSTR_VAL(msgid));
143 
144 	if (msgstr != ZSTR_VAL(msgid)) {
145 		RETURN_STRING(msgstr);
146 	} else {
147 		RETURN_STR_COPY(msgid);
148 	}
149 }
150 /* }}} */
151 
152 /* {{{ Return the translation of msgid for domain_name and category, or msgid unaltered if a translation does not exist */
PHP_FUNCTION(dcgettext)153 PHP_FUNCTION(dcgettext)
154 {
155 	char *msgstr = NULL;
156 	zend_string *domain, *msgid;
157 	zend_long category;
158 
159 	ZEND_PARSE_PARAMETERS_START(3, 3)
160 		Z_PARAM_STR(domain)
161 		Z_PARAM_STR(msgid)
162 		Z_PARAM_LONG(category)
163 	ZEND_PARSE_PARAMETERS_END();
164 
165 	PHP_GETTEXT_DOMAIN_LENGTH_CHECK(1, ZSTR_LEN(domain))
166 	PHP_GETTEXT_LENGTH_CHECK(2, ZSTR_LEN(msgid))
167 	PHP_DCGETTEXT_CATEGORY_CHECK(3, category)
168 
169 	msgstr = dcgettext(ZSTR_VAL(domain), ZSTR_VAL(msgid), category);
170 
171 	if (msgstr != ZSTR_VAL(msgid)) {
172 		RETURN_STRING(msgstr);
173 	} else {
174 		RETURN_STR_COPY(msgid);
175 	}
176 }
177 /* }}} */
178 
179 /* {{{ Bind to the text domain domain_name, looking for translations in dir. Returns the current domain */
PHP_FUNCTION(bindtextdomain)180 PHP_FUNCTION(bindtextdomain)
181 {
182 	zend_string *domain, *dir = NULL;
183 	char *retval, dir_name[MAXPATHLEN];
184 
185 	ZEND_PARSE_PARAMETERS_START(1, 2)
186 		Z_PARAM_STR(domain)
187 		Z_PARAM_OPTIONAL
188 		Z_PARAM_STR_OR_NULL(dir)
189 	ZEND_PARSE_PARAMETERS_END();
190 
191 	PHP_GETTEXT_DOMAIN_LENGTH_CHECK(1, ZSTR_LEN(domain))
192 
193 	if (!ZSTR_LEN(domain)) {
194 		zend_argument_value_error(1, "cannot be empty");
195 		RETURN_THROWS();
196 	}
197 
198 	if (dir == NULL) {
199 		RETURN_STRING(bindtextdomain(ZSTR_VAL(domain), NULL));
200 	}
201 
202 	if (ZSTR_LEN(dir) != 0 && !zend_string_equals_literal(dir, "0")) {
203 		if (!VCWD_REALPATH(ZSTR_VAL(dir), dir_name)) {
204 			RETURN_FALSE;
205 		}
206 	} else if (!VCWD_GETCWD(dir_name, MAXPATHLEN)) {
207 		RETURN_FALSE;
208 	}
209 
210 	retval = bindtextdomain(ZSTR_VAL(domain), dir_name);
211 
212 	RETURN_STRING(retval);
213 }
214 /* }}} */
215 
216 #ifdef HAVE_NGETTEXT
217 /* {{{ Plural version of gettext() */
PHP_FUNCTION(ngettext)218 PHP_FUNCTION(ngettext)
219 {
220 	char *msgstr = NULL;
221 	zend_string *msgid1, *msgid2;
222 	zend_long count;
223 
224 	ZEND_PARSE_PARAMETERS_START(3, 3)
225 		Z_PARAM_STR(msgid1)
226 		Z_PARAM_STR(msgid2)
227 		Z_PARAM_LONG(count)
228 	ZEND_PARSE_PARAMETERS_END();
229 
230 	PHP_GETTEXT_LENGTH_CHECK(1, ZSTR_LEN(msgid1))
231 	PHP_GETTEXT_LENGTH_CHECK(2, ZSTR_LEN(msgid2))
232 
233 	msgstr = ngettext(ZSTR_VAL(msgid1), ZSTR_VAL(msgid2), count);
234 
235 	ZEND_ASSERT(msgstr);
236 	RETURN_STRING(msgstr);
237 }
238 /* }}} */
239 #endif
240 
241 #ifdef HAVE_DNGETTEXT
242 /* {{{ Plural version of dgettext() */
PHP_FUNCTION(dngettext)243 PHP_FUNCTION(dngettext)
244 {
245 	char *msgstr = NULL;
246 	zend_string *domain, *msgid1, *msgid2;
247 	zend_long count;
248 
249 	ZEND_PARSE_PARAMETERS_START(4, 4)
250 		Z_PARAM_STR(domain)
251 		Z_PARAM_STR(msgid1)
252 		Z_PARAM_STR(msgid2)
253 		Z_PARAM_LONG(count)
254 	ZEND_PARSE_PARAMETERS_END();
255 
256 	PHP_GETTEXT_DOMAIN_LENGTH_CHECK(1, ZSTR_LEN(domain))
257 	PHP_GETTEXT_LENGTH_CHECK(2, ZSTR_LEN(msgid1))
258 	PHP_GETTEXT_LENGTH_CHECK(3, ZSTR_LEN(msgid2))
259 
260 	msgstr = dngettext(ZSTR_VAL(domain), ZSTR_VAL(msgid1), ZSTR_VAL(msgid2), count);
261 
262 	ZEND_ASSERT(msgstr);
263 	RETURN_STRING(msgstr);
264 }
265 /* }}} */
266 #endif
267 
268 #ifdef HAVE_DCNGETTEXT
269 /* {{{ Plural version of dcgettext() */
PHP_FUNCTION(dcngettext)270 PHP_FUNCTION(dcngettext)
271 {
272 	char *msgstr = NULL;
273 	zend_string *domain, *msgid1, *msgid2;
274 	zend_long count, category;
275 
276 	RETVAL_FALSE;
277 
278 	ZEND_PARSE_PARAMETERS_START(5, 5)
279 		Z_PARAM_STR(domain)
280 		Z_PARAM_STR(msgid1)
281 		Z_PARAM_STR(msgid2)
282 		Z_PARAM_LONG(count)
283 		Z_PARAM_LONG(category)
284 	ZEND_PARSE_PARAMETERS_END();
285 
286 	PHP_GETTEXT_DOMAIN_LENGTH_CHECK(1, ZSTR_LEN(domain))
287 	PHP_GETTEXT_LENGTH_CHECK(2, ZSTR_LEN(msgid1))
288 	PHP_GETTEXT_LENGTH_CHECK(3, ZSTR_LEN(msgid2))
289 	PHP_DCGETTEXT_CATEGORY_CHECK(5, category)
290 
291 	msgstr = dcngettext(ZSTR_VAL(domain), ZSTR_VAL(msgid1), ZSTR_VAL(msgid2), count, category);
292 
293 	ZEND_ASSERT(msgstr);
294 	RETURN_STRING(msgstr);
295 }
296 /* }}} */
297 #endif
298 
299 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
300 
301 /* {{{ Specify the character encoding in which the messages from the DOMAIN message catalog will be returned. */
PHP_FUNCTION(bind_textdomain_codeset)302 PHP_FUNCTION(bind_textdomain_codeset)
303 {
304 	char *retval = NULL;
305 	zend_string *domain, *codeset = NULL;
306 
307 	ZEND_PARSE_PARAMETERS_START(1, 2)
308 		Z_PARAM_STR(domain)
309 		Z_PARAM_OPTIONAL
310 		Z_PARAM_STR_OR_NULL(codeset)
311 	ZEND_PARSE_PARAMETERS_END();
312 
313 	PHP_GETTEXT_DOMAIN_LENGTH_CHECK(1, ZSTR_LEN(domain))
314 
315 	if (!ZSTR_LEN(domain)) {
316 		zend_argument_value_error(1, "cannot be empty");
317 		RETURN_THROWS();
318 	}
319 
320 	retval = bind_textdomain_codeset(ZSTR_VAL(domain), codeset ? ZSTR_VAL(codeset) : NULL);
321 
322 	if (!retval) {
323 		RETURN_FALSE;
324 	}
325 	RETURN_STRING(retval);
326 }
327 /* }}} */
328 #endif
329 
330 
331 #endif /* HAVE_LIBINTL */
332