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_must_not_be_empty_error(_arg_num); \
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 (dir == NULL) {
194 RETURN_STRING(bindtextdomain(ZSTR_VAL(domain), NULL));
195 }
196
197 if (ZSTR_LEN(dir) != 0 && !zend_string_equals_literal(dir, "0")) {
198 if (!VCWD_REALPATH(ZSTR_VAL(dir), dir_name)) {
199 RETURN_FALSE;
200 }
201 } else if (!VCWD_GETCWD(dir_name, MAXPATHLEN)) {
202 RETURN_FALSE;
203 }
204
205 retval = bindtextdomain(ZSTR_VAL(domain), dir_name);
206
207 RETURN_STRING(retval);
208 }
209 /* }}} */
210
211 #ifdef HAVE_NGETTEXT
212 /* {{{ Plural version of gettext() */
PHP_FUNCTION(ngettext)213 PHP_FUNCTION(ngettext)
214 {
215 char *msgstr = NULL;
216 zend_string *msgid1, *msgid2;
217 zend_long count;
218
219 ZEND_PARSE_PARAMETERS_START(3, 3)
220 Z_PARAM_STR(msgid1)
221 Z_PARAM_STR(msgid2)
222 Z_PARAM_LONG(count)
223 ZEND_PARSE_PARAMETERS_END();
224
225 PHP_GETTEXT_LENGTH_CHECK(1, ZSTR_LEN(msgid1))
226 PHP_GETTEXT_LENGTH_CHECK(2, ZSTR_LEN(msgid2))
227
228 msgstr = ngettext(ZSTR_VAL(msgid1), ZSTR_VAL(msgid2), count);
229
230 ZEND_ASSERT(msgstr);
231 RETURN_STRING(msgstr);
232 }
233 /* }}} */
234 #endif
235
236 #ifdef HAVE_DNGETTEXT
237 /* {{{ Plural version of dgettext() */
PHP_FUNCTION(dngettext)238 PHP_FUNCTION(dngettext)
239 {
240 char *msgstr = NULL;
241 zend_string *domain, *msgid1, *msgid2;
242 zend_long count;
243
244 ZEND_PARSE_PARAMETERS_START(4, 4)
245 Z_PARAM_STR(domain)
246 Z_PARAM_STR(msgid1)
247 Z_PARAM_STR(msgid2)
248 Z_PARAM_LONG(count)
249 ZEND_PARSE_PARAMETERS_END();
250
251 PHP_GETTEXT_DOMAIN_LENGTH_CHECK(1, ZSTR_LEN(domain))
252 PHP_GETTEXT_LENGTH_CHECK(2, ZSTR_LEN(msgid1))
253 PHP_GETTEXT_LENGTH_CHECK(3, ZSTR_LEN(msgid2))
254
255 msgstr = dngettext(ZSTR_VAL(domain), ZSTR_VAL(msgid1), ZSTR_VAL(msgid2), count);
256
257 ZEND_ASSERT(msgstr);
258 RETURN_STRING(msgstr);
259 }
260 /* }}} */
261 #endif
262
263 #ifdef HAVE_DCNGETTEXT
264 /* {{{ Plural version of dcgettext() */
PHP_FUNCTION(dcngettext)265 PHP_FUNCTION(dcngettext)
266 {
267 char *msgstr = NULL;
268 zend_string *domain, *msgid1, *msgid2;
269 zend_long count, category;
270
271 RETVAL_FALSE;
272
273 ZEND_PARSE_PARAMETERS_START(5, 5)
274 Z_PARAM_STR(domain)
275 Z_PARAM_STR(msgid1)
276 Z_PARAM_STR(msgid2)
277 Z_PARAM_LONG(count)
278 Z_PARAM_LONG(category)
279 ZEND_PARSE_PARAMETERS_END();
280
281 PHP_GETTEXT_DOMAIN_LENGTH_CHECK(1, ZSTR_LEN(domain))
282 PHP_GETTEXT_LENGTH_CHECK(2, ZSTR_LEN(msgid1))
283 PHP_GETTEXT_LENGTH_CHECK(3, ZSTR_LEN(msgid2))
284 PHP_DCGETTEXT_CATEGORY_CHECK(5, category)
285
286 msgstr = dcngettext(ZSTR_VAL(domain), ZSTR_VAL(msgid1), ZSTR_VAL(msgid2), count, category);
287
288 ZEND_ASSERT(msgstr);
289 RETURN_STRING(msgstr);
290 }
291 /* }}} */
292 #endif
293
294 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
295
296 /* {{{ Specify the character encoding in which the messages from the DOMAIN message catalog will be returned. */
PHP_FUNCTION(bind_textdomain_codeset)297 PHP_FUNCTION(bind_textdomain_codeset)
298 {
299 char *retval = NULL;
300 zend_string *domain, *codeset = NULL;
301
302 ZEND_PARSE_PARAMETERS_START(1, 2)
303 Z_PARAM_STR(domain)
304 Z_PARAM_OPTIONAL
305 Z_PARAM_STR_OR_NULL(codeset)
306 ZEND_PARSE_PARAMETERS_END();
307
308 PHP_GETTEXT_DOMAIN_LENGTH_CHECK(1, ZSTR_LEN(domain))
309
310 retval = bind_textdomain_codeset(ZSTR_VAL(domain), codeset ? ZSTR_VAL(codeset) : NULL);
311
312 if (!retval) {
313 RETURN_FALSE;
314 }
315 RETURN_STRING(retval);
316 }
317 /* }}} */
318 #endif
319
320
321 #endif /* HAVE_LIBINTL */
322