1<?php
2/*
3** Zabbix
4** Copyright (C) 2001-2021 Zabbix SIA
5**
6** This program is free software; you can redistribute it and/or modify
7** it under the terms of the GNU General Public License as published by
8** the Free Software Foundation; either version 2 of the License, or
9** (at your option) any later version.
10**
11** This program is distributed in the hope that it will be useful,
12** but WITHOUT ANY WARRANTY; without even the implied warranty of
13** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14** GNU General Public License for more details.
15**
16** You should have received a copy of the GNU General Public License
17** along with this program; if not, write to the Free Software
18** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19**/
20
21
22function init_mbstrings() {
23	$res = true;
24	$res &= extension_loaded('mbstring');
25
26	if (version_compare(PHP_VERSION, '5.6', '<')) {
27		ini_set('mbstring.internal_encoding', 'UTF-8');
28		$res &= (ini_get('mbstring.internal_encoding') === 'UTF-8');
29	}
30	else {
31		ini_set('default_charset', 'UTF-8');
32		$res &= (ini_get('default_charset') === 'UTF-8');
33	}
34
35	ini_set('mbstring.detect_order', 'UTF-8, ISO-8859-1, JIS, SJIS');
36	$res &= (ini_get('mbstring.detect_order') === 'UTF-8, ISO-8859-1, JIS, SJIS');
37
38	return $res;
39}
40
41/**
42 * Returns a list of all used locales.
43 *
44 * Each locale has the following properties:
45 * - name       - the full name of the locale
46 * - display    - whether to display the locale in the frontend
47 *
48 * @return array    an array of locales with locale codes as keys and arrays as values
49 */
50function getLocales() {
51	return [
52		'en_GB' => ['name' => _('English (en_GB)'),	'display' => true],
53		'en_US' => ['name' => _('English (en_US)'),	'display' => true],
54		'bg_BG' => ['name' => _('Bulgarian (bg_BG)'),	'display' => false],
55		'zh_CN' => ['name' => _('Chinese (zh_CN)'),	'display' => true],
56		'zh_TW' => ['name' => _('Chinese (zh_TW)'),	'display' => false],
57		'cs_CZ' => ['name' => _('Czech (cs_CZ)'),	'display' => true],
58		'nl_NL' => ['name' => _('Dutch (nl_NL)'),	'display' => false],
59		'fi_FI' => ['name' => _('Finnish (fi_FI)'),	'display' => false],
60		'fr_FR' => ['name' => _('French (fr_FR)'),	'display' => true],
61		'ka_GE' => ['name' => _('Georgian (ka_GE)'),	'display' => false],
62		'de_DE' => ['name' => _('German (de_DE)'),	'display' => false],
63		'el_GR' => ['name' => _('Greek (el_GR)'),	'display' => false],
64		'hu_HU' => ['name' => _('Hungarian (hu_HU)'),	'display' => false],
65		'id_ID' => ['name' => _('Indonesian (id_ID)'),	'display' => false],
66		'it_IT' => ['name' => _('Italian (it_IT)'),	'display' => true],
67		'ko_KR' => ['name' => _('Korean (ko_KR)'),	'display' => true],
68		'ja_JP' => ['name' => _('Japanese (ja_JP)'),	'display' => true],
69		'lv_LV' => ['name' => _('Latvian (lv_LV)'),	'display' => false],
70		'lt_LT' => ['name' => _('Lithuanian (lt_LT)'),	'display' => false],
71		'fa_IR' => ['name' => _('Persian (fa_IR)'),	'display' => false],
72		'pl_PL' => ['name' => _('Polish (pl_PL)'),	'display' => true],
73		'pt_BR' => ['name' => _('Portuguese (pt_BR)'),	'display' => true],
74		'pt_PT' => ['name' => _('Portuguese (pt_PT)'),	'display' => false],
75		'ro_RO' => ['name' => _('Romanian (ro_RO)'),	'display' => false],
76		'ru_RU' => ['name' => _('Russian (ru_RU)'),	'display' => true],
77		'sk_SK' => ['name' => _('Slovak (sk_SK)'),	'display' => true],
78		'es_ES' => ['name' => _('Spanish (es_ES)'),	'display' => false],
79		'sv_SE' => ['name' => _('Swedish (sv_SE)'),	'display' => false],
80		'tr_TR' => ['name' => _('Turkish (tr_TR)'),	'display' => true],
81		'uk_UA' => ['name' => _('Ukrainian (uk_UA)'),	'display' => true],
82		'vi_VN' => ['name' => _('Vietnamese (vi_VN)'),	'display' => false]
83	];
84}
85
86/**
87 * Return an array of locale name variants based on language.
88 *
89 * @param string $language in format 'ru_RU', 'en_EN' and so on
90 * @return array a list of possible locale names
91 */
92function zbx_locale_variants($language) {
93	if (strtolower(substr(PHP_OS, 0, 3)) === 'win') {
94		return zbx_locale_variants_win($language);
95	}
96	else {
97		return zbx_locale_variants_unix($language);
98	}
99}
100
101function zbx_locale_variants_unix($language) {
102	$postfixes = [
103		'',
104		'.utf8',
105		'.UTF-8',
106		'.iso885915',
107		'.ISO8859-1',
108		'.ISO8859-2',
109		'.ISO8859-4',
110		'.ISO8859-5',
111		'.ISO8859-15',
112		'.ISO8859-13',
113		'.CP1131',
114		'.CP1251',
115		'.CP1251',
116		'.CP949',
117		'.KOI8-U',
118		'.US-ASCII',
119		'.eucKR',
120		'.eucJP',
121		'.SJIS',
122		'.GB18030',
123		'.GB2312',
124		'.GBK',
125		'.eucCN',
126		'.Big5HKSCS',
127		'.Big5',
128		'.armscii8',
129		'.cp1251',
130		'.eucjp',
131		'.euckr',
132		'.euctw',
133		'.gb18030',
134		'.gbk',
135		'.koi8r',
136		'.tcvn'
137	];
138	$result = [];
139	foreach ($postfixes as $postfix) {
140		$result[] = $language.$postfix;
141	}
142	return $result;
143}
144
145function zbx_locale_variants_win($language) {
146	// windows locales are written like language[_country[.charset]]
147	// for a list of supported languages see:
148	// http://msdn.microsoft.com/en-us/library/39cwe7zf(vs.71).aspx
149	// http://docs.moodle.org/dev/Table_of_locales#Table
150	$winLanguageName = [
151		'en_gb' => ['English_United Kingdom.1252', 'english-uk'],
152		'en_us' => ['English_United States.1252', 'english-usa'],
153		'bg_bg' => ['Bulgarian_Bulgaria.1251'],
154		'zh_cn' => ['Chinese (Simplified)_People\'s Republic of China.936', 'chinese'],
155		'zh_tw' => ['Chinese_Taiwan.950', 'chinese'],
156		'cs_cz' => ['Czech_Czech Republic.1250', 'czech'],
157		'nl_nl' => ['Dutch_Netherlands.1252', 'dutch'],
158		'fi_fi' => ['Finnish_Finland.1252', 'finnish'],
159		'fr_fr' => ['French_France.1252', 'french'],
160		'ka_ge' => ['Georgian_Georgia.65001', 'georgian'],
161		'de_de' => ['German_Germany.1252', 'german'],
162		'el_gr' => ['Greek_Greece.1253', 'greek'],
163		'hu_hu' => ['Hungarian_Hungary.1250', 'hungarian'],
164		'id_id' => ['Indonesian_indonesia.1252', 'indonesian'],
165		'it_it' => ['Italian_Italy.1252', 'italian'],
166		'ko_kr' => ['Korean_Korea.949', 'korean'],
167		'ja_jp' => ['Japanese_Japan.932', 'japanese'],
168		'lv_lv' => ['Latvian_Latvia.1257', 'latvian'],
169		'lt_lt' => ['Lithuanian_Lithuania.1257', 'lithuanian'],
170		'fa_ir' => ['Farsi_Iran.1256', 'farsi'],
171		'pl_pl' => ['Polish_Poland.1250', 'polish'],
172		'pt_br' => ['Portuguese_Brazil.1252', 'portuguese-brazil'],
173		'pt_pt' => ['Portuguese_Portugal.1252', 'portuguese'],
174		'ro_ro' => ['Romanian_Romania.1250', 'romanian'],
175		'ru_ru' => ['Russian_Russia.1251', 'russian'],
176		'sk_sk' => ['Slovak_Slovakia.1250', 'slovak'],
177		'es_es' => ['Spanish_Spain.1252', 'spanish'],
178		'sv_se' => ['Swedish_Sweden.1252', 'swedish'],
179		'tr_tr' => ['Turkish_Turkey.1254', 'turkish'],
180		'uk_ua' => ['Ukrainian_Ukraine.1251', 'ukrainian'],
181		'vi_vn' => ['Vietnamese_Viet Nam.1258', 'vietnamese']
182	];
183	return $winLanguageName[strtolower($language)];
184}
185