root/ext/gettext/gettext.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. PHP_MINFO_FUNCTION
  2. PHP_NAMED_FUNCTION
  3. PHP_NAMED_FUNCTION
  4. PHP_NAMED_FUNCTION
  5. PHP_NAMED_FUNCTION
  6. PHP_NAMED_FUNCTION
  7. PHP_NAMED_FUNCTION
  8. PHP_NAMED_FUNCTION
  9. PHP_NAMED_FUNCTION
  10. PHP_NAMED_FUNCTION

   1 /*
   2    +----------------------------------------------------------------------+
   3    | PHP Version 7                                                        |
   4    +----------------------------------------------------------------------+
   5    | Copyright (c) 1997-2016 The PHP Group                                |
   6    +----------------------------------------------------------------------+
   7    | This source file is subject to version 3.01 of the PHP license,      |
   8    | that is bundled with this package in the file LICENSE, and is        |
   9    | available through the world-wide-web at the following url:           |
  10    | http://www.php.net/license/3_01.txt                                  |
  11    | If you did not receive a copy of the PHP license and are unable to   |
  12    | obtain it through the world-wide-web, please send a note to          |
  13    | license@php.net so we can mail you a copy immediately.               |
  14    +----------------------------------------------------------------------+
  15    | Author: Alex Plotnick <alex@wgate.com>                               |
  16    +----------------------------------------------------------------------+
  17  */
  18 
  19 /* $Id$ */
  20 
  21 #ifdef HAVE_CONFIG_H
  22 #include "config.h"
  23 #endif
  24 
  25 #include "php.h"
  26 
  27 #if HAVE_LIBINTL
  28 
  29 #include <stdio.h>
  30 #include "ext/standard/info.h"
  31 #include "php_gettext.h"
  32 
  33 /* {{{ arginfo */
  34 ZEND_BEGIN_ARG_INFO(arginfo_textdomain, 0)
  35         ZEND_ARG_INFO(0, domain)
  36 ZEND_END_ARG_INFO()
  37 
  38 ZEND_BEGIN_ARG_INFO(arginfo_gettext, 0)
  39         ZEND_ARG_INFO(0, msgid)
  40 ZEND_END_ARG_INFO()
  41 
  42 ZEND_BEGIN_ARG_INFO(arginfo_dgettext, 0)
  43         ZEND_ARG_INFO(0, domain_name)
  44         ZEND_ARG_INFO(0, msgid)
  45 ZEND_END_ARG_INFO()
  46 
  47 ZEND_BEGIN_ARG_INFO(arginfo_dcgettext, 0)
  48         ZEND_ARG_INFO(0, domain_name)
  49         ZEND_ARG_INFO(0, msgid)
  50         ZEND_ARG_INFO(0, category)
  51 ZEND_END_ARG_INFO()
  52 
  53 ZEND_BEGIN_ARG_INFO(arginfo_bindtextdomain, 0)
  54         ZEND_ARG_INFO(0, domain_name)
  55         ZEND_ARG_INFO(0, dir)
  56 ZEND_END_ARG_INFO()
  57 
  58 #if HAVE_NGETTEXT
  59 ZEND_BEGIN_ARG_INFO(arginfo_ngettext, 0)
  60         ZEND_ARG_INFO(0, msgid1)
  61         ZEND_ARG_INFO(0, msgid2)
  62         ZEND_ARG_INFO(0, count)
  63 ZEND_END_ARG_INFO()
  64 #endif
  65 
  66 #if HAVE_DNGETTEXT
  67 ZEND_BEGIN_ARG_INFO(arginfo_dngettext, 0)
  68         ZEND_ARG_INFO(0, domain)
  69         ZEND_ARG_INFO(0, msgid1)
  70         ZEND_ARG_INFO(0, msgid2)
  71         ZEND_ARG_INFO(0, count)
  72 ZEND_END_ARG_INFO()
  73 #endif
  74 
  75 #if HAVE_DCNGETTEXT
  76 ZEND_BEGIN_ARG_INFO(arginfo_dcngettext, 0)
  77         ZEND_ARG_INFO(0, domain)
  78         ZEND_ARG_INFO(0, msgid1)
  79         ZEND_ARG_INFO(0, msgid2)
  80         ZEND_ARG_INFO(0, count)
  81         ZEND_ARG_INFO(0, category)
  82 ZEND_END_ARG_INFO()
  83 #endif
  84 
  85 #if HAVE_BIND_TEXTDOMAIN_CODESET
  86 ZEND_BEGIN_ARG_INFO(arginfo_bind_textdomain_codeset, 0)
  87         ZEND_ARG_INFO(0, domain)
  88         ZEND_ARG_INFO(0, codeset)
  89 ZEND_END_ARG_INFO()
  90 #endif
  91 /* }}} */
  92 
  93 /* {{{ php_gettext_functions[]
  94  */
  95 const zend_function_entry php_gettext_functions[] = {
  96         PHP_NAMED_FE(textdomain,                zif_textdomain,         arginfo_textdomain)
  97         PHP_NAMED_FE(gettext,                   zif_gettext,            arginfo_gettext)
  98         /* Alias for gettext() */
  99         PHP_NAMED_FE(_,                                 zif_gettext,            arginfo_gettext)
 100         PHP_NAMED_FE(dgettext,                  zif_dgettext,           arginfo_dgettext)
 101         PHP_NAMED_FE(dcgettext,                 zif_dcgettext,          arginfo_dcgettext)
 102         PHP_NAMED_FE(bindtextdomain,    zif_bindtextdomain,     arginfo_bindtextdomain)
 103 #if HAVE_NGETTEXT
 104         PHP_NAMED_FE(ngettext,                  zif_ngettext,           arginfo_ngettext)
 105 #endif
 106 #if HAVE_DNGETTEXT
 107         PHP_NAMED_FE(dngettext,                 zif_dngettext,          arginfo_dngettext)
 108 #endif
 109 #if HAVE_DCNGETTEXT
 110         PHP_NAMED_FE(dcngettext,                zif_dcngettext,         arginfo_dcngettext)
 111 #endif
 112 #if HAVE_BIND_TEXTDOMAIN_CODESET
 113         PHP_NAMED_FE(bind_textdomain_codeset,   zif_bind_textdomain_codeset,    arginfo_bind_textdomain_codeset)
 114 #endif
 115     PHP_FE_END
 116 };
 117 /* }}} */
 118 
 119 #include <libintl.h>
 120 
 121 zend_module_entry php_gettext_module_entry = {
 122         STANDARD_MODULE_HEADER,
 123         "gettext",
 124         php_gettext_functions,
 125         NULL,
 126         NULL,
 127         NULL,
 128         NULL,
 129         PHP_MINFO(php_gettext),
 130         PHP_GETTEXT_VERSION,
 131         STANDARD_MODULE_PROPERTIES
 132 };
 133 
 134 #ifdef COMPILE_DL_GETTEXT
 135 ZEND_GET_MODULE(php_gettext)
 136 #endif
 137 
 138 #define PHP_GETTEXT_MAX_DOMAIN_LENGTH 1024
 139 #define PHP_GETTEXT_MAX_MSGID_LENGTH 4096
 140 
 141 #define PHP_GETTEXT_DOMAIN_LENGTH_CHECK \
 142         if (UNEXPECTED(domain_len > PHP_GETTEXT_MAX_DOMAIN_LENGTH)) { \
 143                 php_error_docref(NULL, E_WARNING, "domain passed too long"); \
 144                 RETURN_FALSE; \
 145         }
 146 
 147 #define PHP_GETTEXT_LENGTH_CHECK(check_name, check_len) \
 148         if (UNEXPECTED(check_len > PHP_GETTEXT_MAX_MSGID_LENGTH)) { \
 149                 php_error_docref(NULL, E_WARNING, "%s passed too long", check_name); \
 150                 RETURN_FALSE; \
 151         }
 152 
 153 PHP_MINFO_FUNCTION(php_gettext)
 154 {
 155         php_info_print_table_start();
 156         php_info_print_table_row(2, "GetText Support", "enabled");
 157         php_info_print_table_end();
 158 }
 159 
 160 /* {{{ proto string textdomain(string domain)
 161    Set the textdomain to "domain". Returns the current domain */
 162 PHP_NAMED_FUNCTION(zif_textdomain)
 163 {
 164         char *domain, *domain_name, *retval;
 165         size_t domain_len;
 166 
 167         if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &domain, &domain_len) == FAILURE) {
 168                 return;
 169         }
 170 
 171         PHP_GETTEXT_DOMAIN_LENGTH_CHECK
 172 
 173         if (strcmp(domain, "") && strcmp(domain, "0")) {
 174                 domain_name = domain;
 175         } else {
 176                 domain_name = NULL;
 177         }
 178 
 179         retval = textdomain(domain_name);
 180 
 181         RETURN_STRING(retval);
 182 }
 183 /* }}} */
 184 
 185 /* {{{ proto string gettext(string msgid)
 186    Return the translation of msgid for the current domain, or msgid unaltered if a translation does not exist */
 187 PHP_NAMED_FUNCTION(zif_gettext)
 188 {
 189         char *msgstr;
 190         zend_string *msgid;
 191 
 192 #ifndef FAST_ZPP
 193         if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &msgid) == FAILURE) {
 194                 return;
 195         }
 196 #else
 197         ZEND_PARSE_PARAMETERS_START(1, 1)
 198                 Z_PARAM_STR(msgid)
 199         ZEND_PARSE_PARAMETERS_END();
 200 #endif
 201 
 202         PHP_GETTEXT_LENGTH_CHECK("msgid", ZSTR_LEN(msgid))
 203         msgstr = gettext(ZSTR_VAL(msgid));
 204 
 205         RETURN_STRING(msgstr);
 206 }
 207 /* }}} */
 208 
 209 /* {{{ proto string dgettext(string domain_name, string msgid)
 210    Return the translation of msgid for domain_name, or msgid unaltered if a translation does not exist */
 211 PHP_NAMED_FUNCTION(zif_dgettext)
 212 {
 213         char *domain, *msgid, *msgstr;
 214         size_t domain_len, msgid_len;
 215 
 216         if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &domain, &domain_len, &msgid, &msgid_len) == FAILURE)  {
 217                 return;
 218         }
 219 
 220         PHP_GETTEXT_DOMAIN_LENGTH_CHECK
 221         PHP_GETTEXT_LENGTH_CHECK("msgid", msgid_len)
 222 
 223         msgstr = dgettext(domain, msgid);
 224 
 225         RETURN_STRING(msgstr);
 226 }
 227 /* }}} */
 228 
 229 /* {{{ proto string dcgettext(string domain_name, string msgid, long category)
 230    Return the translation of msgid for domain_name and category, or msgid unaltered if a translation does not exist */
 231 PHP_NAMED_FUNCTION(zif_dcgettext)
 232 {
 233         char *domain, *msgid, *msgstr;
 234         size_t domain_len, msgid_len;
 235         zend_long category;
 236 
 237         if (zend_parse_parameters(ZEND_NUM_ARGS(), "ssl", &domain, &domain_len, &msgid, &msgid_len, &category) == FAILURE) {
 238                 return;
 239         }
 240 
 241         PHP_GETTEXT_DOMAIN_LENGTH_CHECK
 242         PHP_GETTEXT_LENGTH_CHECK("msgid", msgid_len)
 243 
 244         msgstr = dcgettext(domain, msgid, category);
 245 
 246         RETURN_STRING(msgstr);
 247 }
 248 /* }}} */
 249 
 250 /* {{{ proto string bindtextdomain(string domain_name, string dir)
 251    Bind to the text domain domain_name, looking for translations in dir. Returns the current domain */
 252 PHP_NAMED_FUNCTION(zif_bindtextdomain)
 253 {
 254         char *domain, *dir;
 255         size_t domain_len, dir_len;
 256         char *retval, dir_name[MAXPATHLEN];
 257 
 258         if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &domain, &domain_len, &dir, &dir_len) == FAILURE) {
 259                 return;
 260         }
 261 
 262         PHP_GETTEXT_DOMAIN_LENGTH_CHECK
 263 
 264         if (domain[0] == '\0') {
 265                 php_error(E_WARNING, "The first parameter of bindtextdomain must not be empty");
 266                 RETURN_FALSE;
 267         }
 268 
 269         if (dir[0] != '\0' && strcmp(dir, "0")) {
 270                 if (!VCWD_REALPATH(dir, dir_name)) {
 271                         RETURN_FALSE;
 272                 }
 273         } else if (!VCWD_GETCWD(dir_name, MAXPATHLEN)) {
 274                 RETURN_FALSE;
 275         }
 276 
 277         retval = bindtextdomain(domain, dir_name);
 278 
 279         RETURN_STRING(retval);
 280 }
 281 /* }}} */
 282 
 283 #if HAVE_NGETTEXT
 284 /* {{{ proto string ngettext(string MSGID1, string MSGID2, int N)
 285    Plural version of gettext() */
 286 PHP_NAMED_FUNCTION(zif_ngettext)
 287 {
 288         char *msgid1, *msgid2, *msgstr;
 289         size_t msgid1_len, msgid2_len;
 290         zend_long count;
 291 
 292         if (zend_parse_parameters(ZEND_NUM_ARGS(), "ssl", &msgid1, &msgid1_len, &msgid2, &msgid2_len, &count) == FAILURE) {
 293                 return;
 294         }
 295 
 296         PHP_GETTEXT_LENGTH_CHECK("msgid1", msgid1_len)
 297         PHP_GETTEXT_LENGTH_CHECK("msgid2", msgid2_len)
 298 
 299         msgstr = ngettext(msgid1, msgid2, count);
 300         if (msgstr) {
 301                 RETVAL_STRING(msgstr);
 302         }
 303 }
 304 /* }}} */
 305 #endif
 306 
 307 #if HAVE_DNGETTEXT
 308 /* {{{ proto string dngettext (string domain, string msgid1, string msgid2, int count)
 309    Plural version of dgettext() */
 310 PHP_NAMED_FUNCTION(zif_dngettext)
 311 {
 312         char *domain, *msgid1, *msgid2, *msgstr = NULL;
 313         size_t domain_len, msgid1_len, msgid2_len;
 314         zend_long count;
 315 
 316         if (zend_parse_parameters(ZEND_NUM_ARGS(), "sssl", &domain, &domain_len,
 317                 &msgid1, &msgid1_len, &msgid2, &msgid2_len, &count) == FAILURE) {
 318                 return;
 319         }
 320 
 321         PHP_GETTEXT_DOMAIN_LENGTH_CHECK
 322         PHP_GETTEXT_LENGTH_CHECK("msgid1", msgid1_len)
 323         PHP_GETTEXT_LENGTH_CHECK("msgid2", msgid2_len)
 324 
 325         msgstr = dngettext(domain, msgid1, msgid2, count);
 326         if (msgstr) {
 327                 RETVAL_STRING(msgstr);
 328         }
 329 }
 330 /* }}} */
 331 #endif
 332 
 333 #if HAVE_DCNGETTEXT
 334 /* {{{ proto string dcngettext (string domain, string msgid1, string msgid2, int n, int category)
 335    Plural version of dcgettext() */
 336 PHP_NAMED_FUNCTION(zif_dcngettext)
 337 {
 338         char *domain, *msgid1, *msgid2, *msgstr = NULL;
 339         size_t domain_len, msgid1_len, msgid2_len;
 340         zend_long count, category;
 341 
 342         RETVAL_FALSE;
 343 
 344         if (zend_parse_parameters(ZEND_NUM_ARGS(), "sssll", &domain, &domain_len,
 345                 &msgid1, &msgid1_len, &msgid2, &msgid2_len, &count, &category) == FAILURE) {
 346                 return;
 347         }
 348 
 349         PHP_GETTEXT_DOMAIN_LENGTH_CHECK
 350         PHP_GETTEXT_LENGTH_CHECK("msgid1", msgid1_len)
 351         PHP_GETTEXT_LENGTH_CHECK("msgid2", msgid2_len)
 352 
 353         msgstr = dcngettext(domain, msgid1, msgid2, count, category);
 354 
 355         if (msgstr) {
 356                 RETVAL_STRING(msgstr);
 357         }
 358 }
 359 /* }}} */
 360 #endif
 361 
 362 #if HAVE_BIND_TEXTDOMAIN_CODESET
 363 
 364 /* {{{ proto string bind_textdomain_codeset (string domain, string codeset)
 365    Specify the character encoding in which the messages from the DOMAIN message catalog will be returned. */
 366 PHP_NAMED_FUNCTION(zif_bind_textdomain_codeset)
 367 {
 368         char *domain, *codeset, *retval = NULL;
 369         size_t domain_len, codeset_len;
 370 
 371         if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &domain, &domain_len, &codeset, &codeset_len) == FAILURE) {
 372                 return;
 373         }
 374 
 375         PHP_GETTEXT_DOMAIN_LENGTH_CHECK
 376 
 377         retval = bind_textdomain_codeset(domain, codeset);
 378 
 379         if (!retval) {
 380                 RETURN_FALSE;
 381         }
 382         RETURN_STRING(retval);
 383 }
 384 /* }}} */
 385 #endif
 386 
 387 
 388 #endif /* HAVE_LIBINTL */
 389 
 390 /*
 391  * Local variables:
 392  * tab-width: 4
 393  * c-basic-offset: 4
 394  * End:
 395  * vim600: sw=4 ts=4 fdm=marker
 396  * vim<600: sw=4 ts=4
 397  */
 398 

/* [<][>][^][v][top][bottom][index][help] */