dolibarr 23.0.3
langs-tool-interface.php
1<?php
2/*
3 * Copyright (C) ---Replace with your own copyright and developer email---
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
19//if (! defined('NOREQUIREDB')) define('NOREQUIREDB', '1'); // Do not create database handler $db
20//if (! defined('NOREQUIREUSER')) define('NOREQUIREUSER', '1'); // Do not load object $user
21if (! defined('NOREQUIRESOC')) define('NOREQUIRESOC', '1'); // Do not load object $mysoc
22//if (! defined('NOREQUIRETRAN')) define('NOREQUIRETRAN', '1'); // Do not load object $langs
23//if (! defined('NOSCANGETFORINJECTION')) define('NOSCANGETFORINJECTION', '1'); // Do not check injection attack on GET parameters
24//if (! defined('NOSCANPOSTFORINJECTION')) define('NOSCANPOSTFORINJECTION', '1'); // Do not check injection attack on POST parameters
25//if (! defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL', '1'); // Do not roll the Anti CSRF token (used if MAIN_SECURITY_CSRF_WITH_TOKEN is on)
26//if (! defined('NOSTYLECHECK')) define('NOSTYLECHECK', '1'); // Do not check style html tag into posted data
27if (! defined('NOREQUIREMENU')) define('NOREQUIREMENU', '1'); // If there is no need to load and show top and left menu
28//if (! defined('NOREQUIREHTML')) define('NOREQUIREHTML', '1'); // If we don't need to load the html.form.class.php
29//if (! defined('NOREQUIREAJAX')) define('NOREQUIREAJAX', '1'); // Do not load ajax.lib.php library
30//if (! defined("NOLOGIN")) define("NOLOGIN", '1'); // If this page is public (can be called outside logged session). This include the NOIPCHECK too.
31//if (! defined('NOIPCHECK')) define('NOIPCHECK', '1'); // Do not check IP defined into conf $dolibarr_main_restrict_ip
32//if (! defined("MAIN_LANG_DEFAULT")) define('MAIN_LANG_DEFAULT', 'auto'); // Force lang to a particular value
33//if (! defined("MAIN_AUTHENTICATION_MODE")) define('MAIN_AUTHENTICATION_MODE', 'aloginmodule'); // Force authentication handler
34//if (! defined('CSRFCHECK_WITH_TOKEN')) define('CSRFCHECK_WITH_TOKEN', '1'); // Force use of CSRF protection with tokens even for GET
35//if (! defined('NOBROWSERNOTIF')) define('NOBROWSERNOTIF', '1'); // Disable browser notification
36
37
38// Load Dolibarr environment
39require '../../../../../../main.inc.php';
40
41
55if (empty($dolibarr_nocache)) {
56 $delaycache = '86400';
57 header('Cache-Control: max-age=' . $delaycache . ', public, must-revalidate');
58 header('Pragma: cache'); // This is to avoid to have Pragma: no-cache set by proxy or web server
59 header('Expires: ' . gmdate('D, d M Y H:i:s', time() + (int) $delaycache) . ' GMT'); // This is to avoid to have Expires set by proxy or web server
60} else {
61 // If any cache on files were disable by config file (for test purpose)
62 header('Cache-Control: no-cache');
63}
64header('Content-Type: application/json; charset=utf-8');
65
66
67$local = GETPOST('local');
68if (empty($local)) {
69 $local = $langs->getDefaultLang();
70}
71
72$domain = GETPOST('domain');
73if (empty($domain)) {
74 echo json_encode(['error' => 'Missing domain']);
75 exit;
76}
77
78if (!preg_match('/^[A-Za-z0-9_-]+(?:@[A-Za-z0-9_-]+)?$/', $domain)) {
79 echo json_encode(['error' => 'Invalid domain']);
80 exit;
81}
82
83
84if (!preg_match('/^[a-z]{1,2}_[A-Z]{1,2}(?:,[a-z]{1,2}_[A-Z]{1,2})*$/', $local)) {
85 echo json_encode(['error' => 'Invalid langs codes']);
86 exit;
87}
88
89/*
90Format for JS:
91{
92 fr_FR : {KEY:"TEXT", ...},
93 en_US : {KEY:"TEXT", ...}
94}
95*/
96
97$locals = explode(',', $local);
98$json = new stdClass();
99
100foreach ($locals as $langCode) {
101 $json->$langCode = [];
102 $outputlangs = new Translate("", $conf);
103 $outputlangs->setDefaultLang($langCode);
104 $outputlangs->load($domain);
105 foreach ($outputlangs->tab_translate as $k => $v) {
106 $json->$langCode[$k] = dolPrintHTML($v); // to escape js and other stuff
107 }
108}
109
110print json_encode($json, JSON_PRETTY_PRINT);
Class to manage translations.
dolPrintHTML($s, $allowiframe=0)
Return a string (that can be on several lines) ready to be output on a HTML page.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.