1999-04-21 17:26:16 +00:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
2019-01-30 09:03:12 +00:00
|
|
|
| Copyright (c) The PHP Group |
|
1999-04-21 17:26:16 +00:00
|
|
|
+----------------------------------------------------------------------+
|
2006-01-01 12:51:34 +00:00
|
|
|
| This source file is subject to version 3.01 of the PHP license, |
|
1999-07-16 13:13:16 +00:00
|
|
|
| that is bundled with this package in the file LICENSE, and is |
|
2003-06-10 20:04:29 +00:00
|
|
|
| available through the world-wide-web at the following url: |
|
2021-05-06 10:16:35 +00:00
|
|
|
| https://www.php.net/license/3_01.txt |
|
1999-07-16 13:13:16 +00:00
|
|
|
| If you did not receive a copy of the PHP license and are unable to |
|
|
|
|
| obtain it through the world-wide-web, please send a note to |
|
|
|
|
| license@php.net so we can mail you a copy immediately. |
|
1999-04-21 17:26:16 +00:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Author: Alex Plotnick <alex@wgate.com> |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
2001-05-24 10:07:29 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
1999-04-21 17:26:16 +00:00
|
|
|
#include "php.h"
|
|
|
|
|
2020-05-12 21:19:30 +00:00
|
|
|
#ifdef HAVE_LIBINTL
|
1999-04-21 17:26:16 +00:00
|
|
|
|
2003-09-24 02:07:04 +00:00
|
|
|
#include <stdio.h>
|
2000-04-05 22:30:19 +00:00
|
|
|
#include "ext/standard/info.h"
|
2003-09-24 02:07:04 +00:00
|
|
|
#include "php_gettext.h"
|
2019-08-10 14:39:25 +00:00
|
|
|
#include "gettext_arginfo.h"
|
2006-06-14 21:36:10 +00:00
|
|
|
|
2004-07-29 22:23:23 +00:00
|
|
|
#include <libintl.h>
|
|
|
|
|
1999-12-17 21:50:07 +00:00
|
|
|
zend_module_entry php_gettext_module_entry = {
|
2003-09-24 02:07:04 +00:00
|
|
|
STANDARD_MODULE_HEADER,
|
2001-08-27 22:26:49 +00:00
|
|
|
"gettext",
|
2020-04-04 21:51:10 +00:00
|
|
|
ext_functions,
|
2001-08-27 22:26:49 +00:00
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
2003-09-24 02:07:04 +00:00
|
|
|
PHP_MINFO(php_gettext),
|
2015-03-23 19:33:54 +00:00
|
|
|
PHP_GETTEXT_VERSION,
|
2001-08-27 22:26:49 +00:00
|
|
|
STANDARD_MODULE_PROPERTIES
|
1999-04-21 17:26:16 +00:00
|
|
|
};
|
|
|
|
|
2000-05-23 22:26:51 +00:00
|
|
|
#ifdef COMPILE_DL_GETTEXT
|
|
|
|
ZEND_GET_MODULE(php_gettext)
|
|
|
|
#endif
|
|
|
|
|
2008-10-29 20:29:12 +00:00
|
|
|
#define PHP_GETTEXT_MAX_DOMAIN_LENGTH 1024
|
2008-10-30 14:22:50 +00:00
|
|
|
#define PHP_GETTEXT_MAX_MSGID_LENGTH 4096
|
|
|
|
|
2020-08-02 22:55:02 +00:00
|
|
|
#define PHP_GETTEXT_DOMAIN_LENGTH_CHECK(_arg_num, domain_len) \
|
2015-05-15 14:57:17 +00:00
|
|
|
if (UNEXPECTED(domain_len > PHP_GETTEXT_MAX_DOMAIN_LENGTH)) { \
|
2020-08-02 22:55:02 +00:00
|
|
|
zend_argument_value_error(_arg_num, "is too long"); \
|
|
|
|
RETURN_THROWS(); \
|
2008-10-29 20:29:12 +00:00
|
|
|
}
|
|
|
|
|
2020-08-02 22:55:02 +00:00
|
|
|
#define PHP_GETTEXT_LENGTH_CHECK(_arg_num, check_len) \
|
2015-05-15 14:57:17 +00:00
|
|
|
if (UNEXPECTED(check_len > PHP_GETTEXT_MAX_MSGID_LENGTH)) { \
|
2020-08-02 22:55:02 +00:00
|
|
|
zend_argument_value_error(_arg_num, "is too long"); \
|
|
|
|
RETURN_THROWS(); \
|
2008-10-30 14:22:50 +00:00
|
|
|
}
|
|
|
|
|
2003-09-24 02:07:04 +00:00
|
|
|
PHP_MINFO_FUNCTION(php_gettext)
|
1999-04-21 17:26:16 +00:00
|
|
|
{
|
2000-04-05 22:30:19 +00:00
|
|
|
php_info_print_table_start();
|
2001-06-09 16:55:27 +00:00
|
|
|
php_info_print_table_row(2, "GetText Support", "enabled");
|
2000-04-05 22:30:19 +00:00
|
|
|
php_info_print_table_end();
|
1999-04-21 17:26:16 +00:00
|
|
|
}
|
|
|
|
|
2020-07-01 13:32:55 +00:00
|
|
|
/* {{{ Set the textdomain to "domain". Returns the current domain */
|
2020-04-04 21:51:10 +00:00
|
|
|
PHP_FUNCTION(textdomain)
|
1999-04-21 17:26:16 +00:00
|
|
|
{
|
2021-05-14 12:45:17 +00:00
|
|
|
char *domain_name = NULL, *retval;
|
|
|
|
zend_string *domain = NULL;
|
1999-04-21 17:26:16 +00:00
|
|
|
|
2021-05-14 12:45:17 +00:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS(), "S!", &domain) == FAILURE) {
|
2019-12-30 17:20:30 +00:00
|
|
|
RETURN_THROWS();
|
2000-12-08 16:25:38 +00:00
|
|
|
}
|
1999-04-21 17:26:16 +00:00
|
|
|
|
2021-05-14 12:45:17 +00:00
|
|
|
if (domain != NULL && ZSTR_LEN(domain) != 0 && !zend_string_equals_literal(domain, "0")) {
|
|
|
|
PHP_GETTEXT_DOMAIN_LENGTH_CHECK(1, ZSTR_LEN(domain))
|
|
|
|
domain_name = ZSTR_VAL(domain);
|
1999-04-21 17:26:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
retval = textdomain(domain_name);
|
|
|
|
|
2014-05-05 06:32:32 +00:00
|
|
|
RETURN_STRING(retval);
|
1999-04-21 17:26:16 +00:00
|
|
|
}
|
2000-03-19 23:39:05 +00:00
|
|
|
/* }}} */
|
1999-04-21 17:26:16 +00:00
|
|
|
|
2020-07-01 13:32:55 +00:00
|
|
|
/* {{{ Return the translation of msgid for the current domain, or msgid unaltered if a translation does not exist */
|
2020-04-04 21:51:10 +00:00
|
|
|
PHP_FUNCTION(gettext)
|
1999-04-21 17:26:16 +00:00
|
|
|
{
|
2015-05-06 06:29:19 +00:00
|
|
|
char *msgstr;
|
|
|
|
zend_string *msgid;
|
1999-04-21 17:26:16 +00:00
|
|
|
|
2015-05-06 06:29:19 +00:00
|
|
|
ZEND_PARSE_PARAMETERS_START(1, 1)
|
|
|
|
Z_PARAM_STR(msgid)
|
|
|
|
ZEND_PARSE_PARAMETERS_END();
|
1999-04-21 17:26:16 +00:00
|
|
|
|
2020-08-02 22:55:02 +00:00
|
|
|
PHP_GETTEXT_LENGTH_CHECK(1, ZSTR_LEN(msgid))
|
2015-06-30 01:05:24 +00:00
|
|
|
msgstr = gettext(ZSTR_VAL(msgid));
|
1999-04-21 17:26:16 +00:00
|
|
|
|
2018-02-26 06:52:45 +00:00
|
|
|
if (msgstr != ZSTR_VAL(msgid)) {
|
|
|
|
RETURN_STRING(msgstr);
|
|
|
|
} else {
|
|
|
|
RETURN_STR_COPY(msgid);
|
|
|
|
}
|
1999-04-21 17:26:16 +00:00
|
|
|
}
|
2000-03-19 23:39:05 +00:00
|
|
|
/* }}} */
|
1999-04-21 17:26:16 +00:00
|
|
|
|
2020-07-01 13:32:55 +00:00
|
|
|
/* {{{ Return the translation of msgid for domain_name, or msgid unaltered if a translation does not exist */
|
2020-04-04 21:51:10 +00:00
|
|
|
PHP_FUNCTION(dgettext)
|
1999-04-21 17:26:16 +00:00
|
|
|
{
|
2018-02-26 06:52:45 +00:00
|
|
|
char *msgstr;
|
|
|
|
zend_string *domain, *msgid;
|
1999-04-21 17:26:16 +00:00
|
|
|
|
2018-02-26 06:52:45 +00:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS(), "SS", &domain, &msgid) == FAILURE) {
|
2019-12-30 17:20:30 +00:00
|
|
|
RETURN_THROWS();
|
1999-04-21 17:26:16 +00:00
|
|
|
}
|
|
|
|
|
2020-08-02 22:55:02 +00:00
|
|
|
PHP_GETTEXT_DOMAIN_LENGTH_CHECK(1, ZSTR_LEN(domain))
|
|
|
|
PHP_GETTEXT_LENGTH_CHECK(2, ZSTR_LEN(msgid))
|
2008-10-29 20:29:12 +00:00
|
|
|
|
2018-02-26 06:52:45 +00:00
|
|
|
msgstr = dgettext(ZSTR_VAL(domain), ZSTR_VAL(msgid));
|
1999-04-21 17:26:16 +00:00
|
|
|
|
2018-02-26 06:52:45 +00:00
|
|
|
if (msgstr != ZSTR_VAL(msgid)) {
|
|
|
|
RETURN_STRING(msgstr);
|
|
|
|
} else {
|
|
|
|
RETURN_STR_COPY(msgid);
|
|
|
|
}
|
1999-04-21 17:26:16 +00:00
|
|
|
}
|
2000-03-19 23:39:05 +00:00
|
|
|
/* }}} */
|
1999-04-21 17:26:16 +00:00
|
|
|
|
2020-07-01 13:32:55 +00:00
|
|
|
/* {{{ Return the translation of msgid for domain_name and category, or msgid unaltered if a translation does not exist */
|
2020-04-04 21:51:10 +00:00
|
|
|
PHP_FUNCTION(dcgettext)
|
1999-04-21 17:26:16 +00:00
|
|
|
{
|
2018-02-26 06:52:45 +00:00
|
|
|
char *msgstr;
|
|
|
|
zend_string *domain, *msgid;
|
2014-08-25 17:24:55 +00:00
|
|
|
zend_long category;
|
1999-04-21 17:26:16 +00:00
|
|
|
|
2018-02-26 06:52:45 +00:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS(), "SSl", &domain, &msgid, &category) == FAILURE) {
|
2019-12-30 17:20:30 +00:00
|
|
|
RETURN_THROWS();
|
1999-04-21 17:26:16 +00:00
|
|
|
}
|
|
|
|
|
2020-08-02 22:55:02 +00:00
|
|
|
PHP_GETTEXT_DOMAIN_LENGTH_CHECK(1, ZSTR_LEN(domain))
|
|
|
|
PHP_GETTEXT_LENGTH_CHECK(2, ZSTR_LEN(msgid))
|
2008-10-29 20:29:12 +00:00
|
|
|
|
2018-02-26 06:52:45 +00:00
|
|
|
msgstr = dcgettext(ZSTR_VAL(domain), ZSTR_VAL(msgid), category);
|
1999-04-21 17:26:16 +00:00
|
|
|
|
2018-02-26 06:52:45 +00:00
|
|
|
if (msgstr != ZSTR_VAL(msgid)) {
|
|
|
|
RETURN_STRING(msgstr);
|
|
|
|
} else {
|
|
|
|
RETURN_STR_COPY(msgid);
|
|
|
|
}
|
1999-04-21 17:26:16 +00:00
|
|
|
}
|
2000-03-19 23:39:05 +00:00
|
|
|
/* }}} */
|
1999-04-21 17:26:16 +00:00
|
|
|
|
2020-07-01 13:32:55 +00:00
|
|
|
/* {{{ Bind to the text domain domain_name, looking for translations in dir. Returns the current domain */
|
2020-04-04 21:51:10 +00:00
|
|
|
PHP_FUNCTION(bindtextdomain)
|
1999-04-21 17:26:16 +00:00
|
|
|
{
|
2021-05-14 12:45:17 +00:00
|
|
|
char *domain;
|
|
|
|
size_t domain_len;
|
|
|
|
zend_string *dir = NULL;
|
2000-11-27 18:21:48 +00:00
|
|
|
char *retval, dir_name[MAXPATHLEN];
|
1999-04-21 17:26:16 +00:00
|
|
|
|
2021-05-14 12:45:17 +00:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS(), "sS!", &domain, &domain_len, &dir) == FAILURE) {
|
2019-12-30 17:20:30 +00:00
|
|
|
RETURN_THROWS();
|
1999-04-21 17:26:16 +00:00
|
|
|
}
|
|
|
|
|
2020-08-02 22:55:02 +00:00
|
|
|
PHP_GETTEXT_DOMAIN_LENGTH_CHECK(1, domain_len)
|
2008-10-29 20:29:12 +00:00
|
|
|
|
2008-06-20 18:00:10 +00:00
|
|
|
if (domain[0] == '\0') {
|
2020-08-02 22:55:02 +00:00
|
|
|
zend_argument_value_error(1, "cannot be empty");
|
|
|
|
RETURN_THROWS();
|
2003-02-08 18:58:34 +00:00
|
|
|
}
|
2008-10-30 14:22:50 +00:00
|
|
|
|
2021-01-22 12:08:51 +00:00
|
|
|
if (dir == NULL) {
|
|
|
|
RETURN_STRING(bindtextdomain(domain, NULL));
|
|
|
|
}
|
|
|
|
|
2021-05-14 12:45:17 +00:00
|
|
|
if (ZSTR_LEN(dir) != 0 && !zend_string_equals_literal(dir, "0")) {
|
|
|
|
if (!VCWD_REALPATH(ZSTR_VAL(dir), dir_name)) {
|
2007-01-16 14:42:38 +00:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
} else if (!VCWD_GETCWD(dir_name, MAXPATHLEN)) {
|
|
|
|
RETURN_FALSE;
|
1999-04-21 17:26:16 +00:00
|
|
|
}
|
|
|
|
|
2008-06-20 18:00:10 +00:00
|
|
|
retval = bindtextdomain(domain, dir_name);
|
1999-04-21 17:26:16 +00:00
|
|
|
|
2014-05-05 06:32:32 +00:00
|
|
|
RETURN_STRING(retval);
|
1999-04-21 17:26:16 +00:00
|
|
|
}
|
2000-03-19 23:39:05 +00:00
|
|
|
/* }}} */
|
1999-04-21 17:26:16 +00:00
|
|
|
|
2020-05-12 21:19:30 +00:00
|
|
|
#ifdef HAVE_NGETTEXT
|
2020-07-01 13:32:55 +00:00
|
|
|
/* {{{ Plural version of gettext() */
|
2020-04-04 21:51:10 +00:00
|
|
|
PHP_FUNCTION(ngettext)
|
2001-08-27 18:43:07 +00:00
|
|
|
{
|
2008-06-20 18:00:10 +00:00
|
|
|
char *msgid1, *msgid2, *msgstr;
|
2014-08-27 13:31:48 +00:00
|
|
|
size_t msgid1_len, msgid2_len;
|
2014-08-25 17:24:55 +00:00
|
|
|
zend_long count;
|
2001-08-27 18:43:07 +00:00
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS(), "ssl", &msgid1, &msgid1_len, &msgid2, &msgid2_len, &count) == FAILURE) {
|
2019-12-30 17:20:30 +00:00
|
|
|
RETURN_THROWS();
|
2008-06-20 18:00:10 +00:00
|
|
|
}
|
2001-08-27 18:43:07 +00:00
|
|
|
|
2020-08-02 22:55:02 +00:00
|
|
|
PHP_GETTEXT_LENGTH_CHECK(1, msgid1_len)
|
|
|
|
PHP_GETTEXT_LENGTH_CHECK(2, msgid2_len)
|
2008-10-30 14:22:50 +00:00
|
|
|
|
2008-06-20 18:00:10 +00:00
|
|
|
msgstr = ngettext(msgid1, msgid2, count);
|
2018-02-26 06:52:45 +00:00
|
|
|
|
|
|
|
ZEND_ASSERT(msgstr);
|
|
|
|
RETURN_STRING(msgstr);
|
2001-08-27 18:43:07 +00:00
|
|
|
}
|
|
|
|
/* }}} */
|
2001-08-29 13:13:24 +00:00
|
|
|
#endif
|
2001-08-27 18:43:07 +00:00
|
|
|
|
2020-05-12 21:19:30 +00:00
|
|
|
#ifdef HAVE_DNGETTEXT
|
2020-07-01 13:32:55 +00:00
|
|
|
/* {{{ Plural version of dgettext() */
|
2020-04-04 21:51:10 +00:00
|
|
|
PHP_FUNCTION(dngettext)
|
2001-08-27 18:43:07 +00:00
|
|
|
{
|
2008-06-20 18:00:10 +00:00
|
|
|
char *domain, *msgid1, *msgid2, *msgstr = NULL;
|
2014-08-27 17:25:28 +00:00
|
|
|
size_t domain_len, msgid1_len, msgid2_len;
|
2014-08-25 17:24:55 +00:00
|
|
|
zend_long count;
|
2008-10-30 14:22:50 +00:00
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS(), "sssl", &domain, &domain_len,
|
2008-06-20 18:00:10 +00:00
|
|
|
&msgid1, &msgid1_len, &msgid2, &msgid2_len, &count) == FAILURE) {
|
2019-12-30 17:20:30 +00:00
|
|
|
RETURN_THROWS();
|
2008-06-20 18:00:10 +00:00
|
|
|
}
|
2001-08-27 18:43:07 +00:00
|
|
|
|
2020-08-02 22:55:02 +00:00
|
|
|
PHP_GETTEXT_DOMAIN_LENGTH_CHECK(1, domain_len)
|
|
|
|
PHP_GETTEXT_LENGTH_CHECK(2, msgid1_len)
|
|
|
|
PHP_GETTEXT_LENGTH_CHECK(3, msgid2_len)
|
2008-10-29 20:29:12 +00:00
|
|
|
|
2008-06-20 18:00:10 +00:00
|
|
|
msgstr = dngettext(domain, msgid1, msgid2, count);
|
2018-02-26 06:52:45 +00:00
|
|
|
|
|
|
|
ZEND_ASSERT(msgstr);
|
|
|
|
RETURN_STRING(msgstr);
|
2001-08-27 18:43:07 +00:00
|
|
|
}
|
|
|
|
/* }}} */
|
2001-08-29 13:13:24 +00:00
|
|
|
#endif
|
2001-08-27 18:43:07 +00:00
|
|
|
|
2020-05-12 21:19:30 +00:00
|
|
|
#ifdef HAVE_DCNGETTEXT
|
2020-07-01 13:32:55 +00:00
|
|
|
/* {{{ Plural version of dcgettext() */
|
2020-04-04 21:51:10 +00:00
|
|
|
PHP_FUNCTION(dcngettext)
|
2001-08-27 18:43:07 +00:00
|
|
|
{
|
2008-06-20 18:00:10 +00:00
|
|
|
char *domain, *msgid1, *msgid2, *msgstr = NULL;
|
2014-08-27 17:25:28 +00:00
|
|
|
size_t domain_len, msgid1_len, msgid2_len;
|
2014-08-25 17:24:55 +00:00
|
|
|
zend_long count, category;
|
2001-08-27 18:43:07 +00:00
|
|
|
|
|
|
|
RETVAL_FALSE;
|
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS(), "sssll", &domain, &domain_len,
|
2008-06-20 18:00:10 +00:00
|
|
|
&msgid1, &msgid1_len, &msgid2, &msgid2_len, &count, &category) == FAILURE) {
|
2019-12-30 17:20:30 +00:00
|
|
|
RETURN_THROWS();
|
2008-06-20 18:00:10 +00:00
|
|
|
}
|
2001-08-27 18:43:07 +00:00
|
|
|
|
2020-08-02 22:55:02 +00:00
|
|
|
PHP_GETTEXT_DOMAIN_LENGTH_CHECK(1, domain_len)
|
|
|
|
PHP_GETTEXT_LENGTH_CHECK(2, msgid1_len)
|
|
|
|
PHP_GETTEXT_LENGTH_CHECK(3, msgid2_len)
|
2008-10-29 20:29:12 +00:00
|
|
|
|
2008-06-20 18:00:10 +00:00
|
|
|
msgstr = dcngettext(domain, msgid1, msgid2, count, category);
|
2001-08-27 18:43:07 +00:00
|
|
|
|
2018-02-26 06:52:45 +00:00
|
|
|
ZEND_ASSERT(msgstr);
|
|
|
|
RETURN_STRING(msgstr);
|
2001-08-27 18:43:07 +00:00
|
|
|
}
|
2001-08-29 13:13:24 +00:00
|
|
|
/* }}} */
|
|
|
|
#endif
|
2001-08-27 18:43:07 +00:00
|
|
|
|
2020-05-12 21:19:30 +00:00
|
|
|
#ifdef HAVE_BIND_TEXTDOMAIN_CODESET
|
2001-11-09 16:27:09 +00:00
|
|
|
|
2020-07-01 13:32:55 +00:00
|
|
|
/* {{{ Specify the character encoding in which the messages from the DOMAIN message catalog will be returned. */
|
2020-04-04 21:51:10 +00:00
|
|
|
PHP_FUNCTION(bind_textdomain_codeset)
|
2001-11-09 16:27:09 +00:00
|
|
|
{
|
2021-01-22 12:08:51 +00:00
|
|
|
char *domain, *codeset = NULL, *retval = NULL;
|
2014-08-27 13:31:48 +00:00
|
|
|
size_t domain_len, codeset_len;
|
2001-11-09 16:27:09 +00:00
|
|
|
|
2021-01-22 12:08:51 +00:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss!", &domain, &domain_len, &codeset, &codeset_len) == FAILURE) {
|
2019-12-30 17:20:30 +00:00
|
|
|
RETURN_THROWS();
|
2008-06-20 18:00:10 +00:00
|
|
|
}
|
|
|
|
|
2020-08-02 22:55:02 +00:00
|
|
|
PHP_GETTEXT_DOMAIN_LENGTH_CHECK(1, domain_len)
|
2008-10-29 20:29:12 +00:00
|
|
|
|
2008-06-20 18:00:10 +00:00
|
|
|
retval = bind_textdomain_codeset(domain, codeset);
|
|
|
|
|
|
|
|
if (!retval) {
|
|
|
|
RETURN_FALSE;
|
2002-08-07 11:50:49 +00:00
|
|
|
}
|
2014-05-05 06:32:32 +00:00
|
|
|
RETURN_STRING(retval);
|
2001-11-09 16:27:09 +00:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
1999-04-21 17:26:16 +00:00
|
|
|
#endif /* HAVE_LIBINTL */
|