2015-09-05 02:24:56 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-11 11:58:15 +02:00
|
|
|
* PrivateBin
|
2015-09-05 02:24:56 +02:00
|
|
|
*
|
|
|
|
* a zero-knowledge paste bin
|
|
|
|
*
|
2016-07-11 11:58:15 +02:00
|
|
|
* @link https://github.com/PrivateBin/PrivateBin
|
2015-09-05 02:24:56 +02:00
|
|
|
* @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
|
2016-07-19 13:56:52 +02:00
|
|
|
* @license https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
|
2020-02-16 11:15:28 +01:00
|
|
|
* @version 1.3.3
|
2015-09-05 02:24:56 +02:00
|
|
|
*/
|
2016-12-12 18:43:23 +01:00
|
|
|
|
2016-12-12 18:50:00 +01:00
|
|
|
namespace PrivateBin;
|
2016-07-21 17:09:48 +02:00
|
|
|
|
2015-09-05 02:24:56 +02:00
|
|
|
/**
|
2016-08-09 11:54:42 +02:00
|
|
|
* I18n
|
2015-09-05 02:24:56 +02:00
|
|
|
*
|
|
|
|
* provides internationalization tools like translation, browser language detection, etc.
|
|
|
|
*/
|
2016-08-09 11:54:42 +02:00
|
|
|
class I18n
|
2015-09-05 02:24:56 +02:00
|
|
|
{
|
2015-09-06 19:21:17 +02:00
|
|
|
/**
|
|
|
|
* language
|
|
|
|
*
|
|
|
|
* @access protected
|
|
|
|
* @static
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected static $_language = 'en';
|
|
|
|
|
2015-10-18 20:38:07 +02:00
|
|
|
/**
|
|
|
|
* language fallback
|
|
|
|
*
|
|
|
|
* @access protected
|
|
|
|
* @static
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected static $_languageFallback = 'en';
|
|
|
|
|
2015-09-19 11:21:13 +02:00
|
|
|
/**
|
|
|
|
* language labels
|
|
|
|
*
|
|
|
|
* @access protected
|
|
|
|
* @static
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected static $_languageLabels = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* available languages
|
|
|
|
*
|
|
|
|
* @access protected
|
|
|
|
* @static
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected static $_availableLanguages = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* path to language files
|
|
|
|
*
|
|
|
|
* @access protected
|
|
|
|
* @static
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected static $_path = '';
|
|
|
|
|
2015-09-05 02:24:56 +02:00
|
|
|
/**
|
|
|
|
* translation cache
|
|
|
|
*
|
2015-09-06 19:21:17 +02:00
|
|
|
* @access protected
|
2015-09-05 02:24:56 +02:00
|
|
|
* @static
|
|
|
|
* @var array
|
|
|
|
*/
|
2015-09-06 19:21:17 +02:00
|
|
|
protected static $_translations = array();
|
2015-09-05 02:24:56 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* translate a string, alias for translate()
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
* @param string $messageId
|
|
|
|
* @param mixed $args one or multiple parameters injected into placeholders
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function _($messageId)
|
|
|
|
{
|
2016-08-09 11:54:42 +02:00
|
|
|
return forward_static_call_array('self::translate', func_get_args());
|
2015-09-05 02:24:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* translate a string
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
* @param string $messageId
|
|
|
|
* @param mixed $args one or multiple parameters injected into placeholders
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function translate($messageId)
|
|
|
|
{
|
2016-07-26 08:19:35 +02:00
|
|
|
if (empty($messageId)) {
|
|
|
|
return $messageId;
|
|
|
|
}
|
|
|
|
if (count(self::$_translations) === 0) {
|
|
|
|
self::loadTranslations();
|
|
|
|
}
|
2015-09-06 19:21:17 +02:00
|
|
|
$messages = $messageId;
|
2016-07-26 08:19:35 +02:00
|
|
|
if (is_array($messageId)) {
|
2015-09-06 19:21:17 +02:00
|
|
|
$messageId = count($messageId) > 1 ? $messageId[1] : $messageId[0];
|
|
|
|
}
|
2016-07-26 08:19:35 +02:00
|
|
|
if (!array_key_exists($messageId, self::$_translations)) {
|
2015-09-06 19:21:17 +02:00
|
|
|
self::$_translations[$messageId] = $messages;
|
2015-09-05 02:24:56 +02:00
|
|
|
}
|
|
|
|
$args = func_get_args();
|
2016-07-26 08:19:35 +02:00
|
|
|
if (is_array(self::$_translations[$messageId])) {
|
2015-09-06 19:21:17 +02:00
|
|
|
$number = (int) $args[1];
|
2016-08-15 16:45:47 +02:00
|
|
|
$key = self::_getPluralForm($number);
|
|
|
|
$max = count(self::$_translations[$messageId]) - 1;
|
2016-07-26 08:19:35 +02:00
|
|
|
if ($key > $max) {
|
|
|
|
$key = $max;
|
|
|
|
}
|
2015-09-06 19:21:17 +02:00
|
|
|
|
|
|
|
$args[0] = self::$_translations[$messageId][$key];
|
|
|
|
$args[1] = $number;
|
2016-07-26 08:19:35 +02:00
|
|
|
} else {
|
2015-09-06 19:21:17 +02:00
|
|
|
$args[0] = self::$_translations[$messageId];
|
|
|
|
}
|
2020-02-01 08:46:59 +01:00
|
|
|
// encode any non-integer arguments and the message ID, if it doesn't contain a link
|
|
|
|
$argsCount = count($args);
|
|
|
|
if ($argsCount > 1) {
|
|
|
|
for ($i = 0; $i < $argsCount; ++$i) {
|
|
|
|
if (($i > 0 && !is_int($args[$i])) || strpos($args[0], '<a') === false) {
|
2020-02-01 09:15:14 +01:00
|
|
|
$args[$i] = self::encode($args[$i]);
|
2020-02-01 08:46:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-05 02:24:56 +02:00
|
|
|
return call_user_func_array('sprintf', $args);
|
|
|
|
}
|
|
|
|
|
2020-02-01 09:15:14 +01:00
|
|
|
/**
|
|
|
|
* encode HTML entities for output into an HTML5 document
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
* @param string $string
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function encode($string)
|
|
|
|
{
|
|
|
|
return htmlspecialchars($string, ENT_QUOTES | ENT_HTML5 | ENT_DISALLOWED, 'UTF-8', false);
|
|
|
|
}
|
|
|
|
|
2015-09-05 02:24:56 +02:00
|
|
|
/**
|
|
|
|
* loads translations
|
|
|
|
*
|
2016-07-19 13:56:52 +02:00
|
|
|
* From: https://stackoverflow.com/questions/3770513/detect-browser-language-in-php#3771447
|
2015-09-05 02:24:56 +02:00
|
|
|
*
|
2015-09-19 11:21:13 +02:00
|
|
|
* @access public
|
2015-09-05 02:24:56 +02:00
|
|
|
* @static
|
|
|
|
*/
|
|
|
|
public static function loadTranslations()
|
|
|
|
{
|
2015-09-19 11:21:13 +02:00
|
|
|
$availableLanguages = self::getAvailableLanguages();
|
|
|
|
|
|
|
|
// check if the lang cookie was set and that language exists
|
2017-03-25 00:58:59 +01:00
|
|
|
if (
|
|
|
|
array_key_exists('lang', $_COOKIE) &&
|
|
|
|
($key = array_search($_COOKIE['lang'], $availableLanguages)) !== false
|
|
|
|
) {
|
|
|
|
$match = $availableLanguages[$key];
|
2015-09-19 11:21:13 +02:00
|
|
|
}
|
|
|
|
// find a translation file matching the browsers language preferences
|
2016-07-26 08:19:35 +02:00
|
|
|
else {
|
2015-09-19 11:21:13 +02:00
|
|
|
$match = self::_getMatchingLanguage(
|
|
|
|
self::getBrowserLanguages(), $availableLanguages
|
|
|
|
);
|
2015-09-05 02:24:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// load translations
|
2016-08-15 16:45:47 +02:00
|
|
|
self::$_language = $match;
|
2019-05-13 22:31:52 +02:00
|
|
|
self::$_translations = ($match == 'en') ? array() : Json::decode(
|
|
|
|
file_get_contents(self::_getPath($match . '.json'))
|
2015-09-19 11:21:13 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get list of available translations based on files found
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function getAvailableLanguages()
|
|
|
|
{
|
2016-07-26 08:19:35 +02:00
|
|
|
if (count(self::$_availableLanguages) == 0) {
|
2015-09-19 11:21:13 +02:00
|
|
|
$i18n = dir(self::_getPath());
|
2016-07-26 08:19:35 +02:00
|
|
|
while (false !== ($file = $i18n->read())) {
|
|
|
|
if (preg_match('/^([a-z]{2}).json$/', $file, $match) === 1) {
|
2015-09-19 11:21:13 +02:00
|
|
|
self::$_availableLanguages[] = $match[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self::$_availableLanguages[] = 'en';
|
2015-09-05 02:24:56 +02:00
|
|
|
}
|
2015-09-19 11:21:13 +02:00
|
|
|
return self::$_availableLanguages;
|
2015-09-05 02:24:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* detect the clients supported languages and return them ordered by preference
|
|
|
|
*
|
2016-07-19 13:56:52 +02:00
|
|
|
* From: https://stackoverflow.com/questions/3770513/detect-browser-language-in-php#3771447
|
2015-09-05 02:24:56 +02:00
|
|
|
*
|
2015-09-19 11:21:13 +02:00
|
|
|
* @access public
|
|
|
|
* @static
|
2015-09-05 02:24:56 +02:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function getBrowserLanguages()
|
|
|
|
{
|
|
|
|
$languages = array();
|
2016-07-26 08:19:35 +02:00
|
|
|
if (array_key_exists('HTTP_ACCEPT_LANGUAGE', $_SERVER)) {
|
2015-09-05 02:24:56 +02:00
|
|
|
$languageRanges = explode(',', trim($_SERVER['HTTP_ACCEPT_LANGUAGE']));
|
2016-07-26 08:19:35 +02:00
|
|
|
foreach ($languageRanges as $languageRange) {
|
2015-09-05 02:24:56 +02:00
|
|
|
if (preg_match(
|
|
|
|
'/(\*|[a-zA-Z0-9]{1,8}(?:-[a-zA-Z0-9]{1,8})*)(?:\s*;\s*q\s*=\s*(0(?:\.\d{0,3})|1(?:\.0{0,3})))?/',
|
|
|
|
trim($languageRange), $match
|
2016-07-26 08:19:35 +02:00
|
|
|
)) {
|
|
|
|
if (!isset($match[2])) {
|
2015-09-05 02:24:56 +02:00
|
|
|
$match[2] = '1.0';
|
2016-07-26 08:19:35 +02:00
|
|
|
} else {
|
2015-09-05 02:24:56 +02:00
|
|
|
$match[2] = (string) floatval($match[2]);
|
|
|
|
}
|
2016-07-26 08:19:35 +02:00
|
|
|
if (!isset($languages[$match[2]])) {
|
2015-09-05 02:24:56 +02:00
|
|
|
$languages[$match[2]] = array();
|
|
|
|
}
|
|
|
|
$languages[$match[2]][] = strtolower($match[1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
krsort($languages);
|
|
|
|
}
|
|
|
|
return $languages;
|
|
|
|
}
|
|
|
|
|
2015-09-19 11:21:13 +02:00
|
|
|
/**
|
|
|
|
* get currently loaded language
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function getLanguage()
|
|
|
|
{
|
|
|
|
return self::$_language;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get list of language labels
|
|
|
|
*
|
|
|
|
* Only for given language codes, otherwise all labels.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
* @param array $languages
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function getLanguageLabels($languages = array())
|
|
|
|
{
|
|
|
|
$file = self::_getPath('languages.json');
|
2016-07-26 08:19:35 +02:00
|
|
|
if (count(self::$_languageLabels) == 0 && is_readable($file)) {
|
2019-05-13 22:31:52 +02:00
|
|
|
self::$_languageLabels = Json::decode(file_get_contents($file));
|
2015-09-19 11:21:13 +02:00
|
|
|
}
|
2016-07-26 08:19:35 +02:00
|
|
|
if (count($languages) == 0) {
|
|
|
|
return self::$_languageLabels;
|
|
|
|
}
|
2015-09-19 11:21:13 +02:00
|
|
|
return array_intersect_key(self::$_languageLabels, array_flip($languages));
|
|
|
|
}
|
|
|
|
|
2015-10-18 20:38:07 +02:00
|
|
|
/**
|
|
|
|
* set the default language
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
* @param string $lang
|
|
|
|
*/
|
|
|
|
public static function setLanguageFallback($lang)
|
|
|
|
{
|
2016-07-26 08:19:35 +02:00
|
|
|
if (in_array($lang, self::getAvailableLanguages())) {
|
2015-10-18 20:38:07 +02:00
|
|
|
self::$_languageFallback = $lang;
|
2016-07-26 08:19:35 +02:00
|
|
|
}
|
2015-10-18 20:38:07 +02:00
|
|
|
}
|
|
|
|
|
2015-09-19 11:21:13 +02:00
|
|
|
/**
|
|
|
|
* get language file path
|
|
|
|
*
|
|
|
|
* @access protected
|
|
|
|
* @static
|
|
|
|
* @param string $file
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected static function _getPath($file = '')
|
|
|
|
{
|
2016-07-26 08:19:35 +02:00
|
|
|
if (strlen(self::$_path) == 0) {
|
2015-09-19 11:21:13 +02:00
|
|
|
self::$_path = PUBLIC_PATH . DIRECTORY_SEPARATOR . 'i18n';
|
|
|
|
}
|
|
|
|
return self::$_path . (strlen($file) ? DIRECTORY_SEPARATOR . $file : '');
|
|
|
|
}
|
|
|
|
|
2015-09-06 19:21:17 +02:00
|
|
|
/**
|
|
|
|
* determines the plural form to use based on current language and given number
|
|
|
|
*
|
|
|
|
* From: http://localization-guide.readthedocs.org/en/latest/l10n/pluralforms.html
|
|
|
|
*
|
2015-09-19 11:21:13 +02:00
|
|
|
* @access protected
|
|
|
|
* @static
|
|
|
|
* @param int $n
|
2015-09-06 19:21:17 +02:00
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
protected static function _getPluralForm($n)
|
|
|
|
{
|
|
|
|
switch (self::$_language) {
|
2019-06-23 12:06:36 +02:00
|
|
|
case 'cs':
|
2019-06-23 16:10:05 +02:00
|
|
|
return $n == 1 ? 0 : ($n >= 2 && $n <= 4 ? 1 : 2);
|
2015-09-06 19:21:17 +02:00
|
|
|
case 'fr':
|
2017-01-08 07:56:56 +01:00
|
|
|
case 'oc':
|
2016-04-26 20:21:30 +02:00
|
|
|
case 'zh':
|
2016-10-29 10:24:08 +02:00
|
|
|
return $n > 1 ? 1 : 0;
|
2015-09-06 19:21:17 +02:00
|
|
|
case 'pl':
|
2016-12-25 11:37:45 +01:00
|
|
|
return $n == 1 ? 0 : ($n % 10 >= 2 && $n % 10 <= 4 && ($n % 100 < 10 || $n % 100 >= 20) ? 1 : 2);
|
2016-12-16 10:15:37 +01:00
|
|
|
case 'ru':
|
2019-10-18 11:31:40 +02:00
|
|
|
case 'uk':
|
|
|
|
return $n % 10 == 1 && $n % 100 != 11 ? 0 : ($n % 10 >= 2 && $n % 10 <= 4 && ($n % 100 < 10 || $n % 100 >= 20) ? 1 : 2);
|
2017-01-01 14:35:39 +01:00
|
|
|
case 'sl':
|
|
|
|
return $n % 100 == 1 ? 1 : ($n % 100 == 2 ? 2 : ($n % 100 == 3 || $n % 100 == 4 ? 3 : 0));
|
2019-07-11 16:48:59 +02:00
|
|
|
// bg, de, en, es, hu, it, nl, no, pt
|
2015-09-06 19:21:17 +02:00
|
|
|
default:
|
2016-10-29 10:24:08 +02:00
|
|
|
return $n != 1 ? 1 : 0;
|
2015-09-06 19:21:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-05 02:24:56 +02:00
|
|
|
/**
|
|
|
|
* compares two language preference arrays and returns the preferred match
|
|
|
|
*
|
2016-07-19 13:56:52 +02:00
|
|
|
* From: https://stackoverflow.com/questions/3770513/detect-browser-language-in-php#3771447
|
2015-09-05 02:24:56 +02:00
|
|
|
*
|
2015-09-19 11:21:13 +02:00
|
|
|
* @access protected
|
|
|
|
* @static
|
|
|
|
* @param array $acceptedLanguages
|
|
|
|
* @param array $availableLanguages
|
2015-09-05 02:24:56 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
2016-07-26 08:19:35 +02:00
|
|
|
protected static function _getMatchingLanguage($acceptedLanguages, $availableLanguages)
|
|
|
|
{
|
2015-09-05 02:24:56 +02:00
|
|
|
$matches = array();
|
2016-08-15 16:45:47 +02:00
|
|
|
$any = false;
|
2016-07-26 08:19:35 +02:00
|
|
|
foreach ($acceptedLanguages as $acceptedQuality => $acceptedValues) {
|
2015-09-05 02:24:56 +02:00
|
|
|
$acceptedQuality = floatval($acceptedQuality);
|
2016-07-26 08:19:35 +02:00
|
|
|
if ($acceptedQuality === 0.0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
foreach ($availableLanguages as $availableValue) {
|
2015-09-05 02:24:56 +02:00
|
|
|
$availableQuality = 1.0;
|
2016-07-26 08:19:35 +02:00
|
|
|
foreach ($acceptedValues as $acceptedValue) {
|
|
|
|
if ($acceptedValue === '*') {
|
2015-09-05 02:24:56 +02:00
|
|
|
$any = true;
|
|
|
|
}
|
|
|
|
$matchingGrade = self::_matchLanguage($acceptedValue, $availableValue);
|
2016-07-26 08:19:35 +02:00
|
|
|
if ($matchingGrade > 0) {
|
2015-09-05 02:24:56 +02:00
|
|
|
$q = (string) ($acceptedQuality * $availableQuality * $matchingGrade);
|
2016-07-26 08:19:35 +02:00
|
|
|
if (!isset($matches[$q])) {
|
2015-09-05 02:24:56 +02:00
|
|
|
$matches[$q] = array();
|
|
|
|
}
|
2016-07-26 08:19:35 +02:00
|
|
|
if (!in_array($availableValue, $matches[$q])) {
|
2015-09-05 02:24:56 +02:00
|
|
|
$matches[$q][] = $availableValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-07-26 08:19:35 +02:00
|
|
|
if (count($matches) === 0 && $any) {
|
|
|
|
if (count($availableLanguages) > 0) {
|
2015-09-05 02:24:56 +02:00
|
|
|
$matches['1.0'] = $availableLanguages;
|
|
|
|
}
|
|
|
|
}
|
2016-07-26 08:19:35 +02:00
|
|
|
if (count($matches) === 0) {
|
2015-10-18 20:38:07 +02:00
|
|
|
return self::$_languageFallback;
|
2015-09-05 02:24:56 +02:00
|
|
|
}
|
|
|
|
krsort($matches);
|
|
|
|
$topmatches = current($matches);
|
|
|
|
return current($topmatches);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* compare two language IDs and return the degree they match
|
|
|
|
*
|
2016-07-19 13:56:52 +02:00
|
|
|
* From: https://stackoverflow.com/questions/3770513/detect-browser-language-in-php#3771447
|
2015-09-05 02:24:56 +02:00
|
|
|
*
|
2015-09-19 11:21:13 +02:00
|
|
|
* @access protected
|
|
|
|
* @static
|
|
|
|
* @param string $a
|
|
|
|
* @param string $b
|
2015-09-05 02:24:56 +02:00
|
|
|
* @return float
|
|
|
|
*/
|
2016-07-26 08:19:35 +02:00
|
|
|
protected static function _matchLanguage($a, $b)
|
|
|
|
{
|
2015-09-05 02:24:56 +02:00
|
|
|
$a = explode('-', $a);
|
|
|
|
$b = explode('-', $b);
|
2016-08-15 16:45:47 +02:00
|
|
|
for ($i = 0, $n = min(count($a), count($b)); $i < $n; ++$i) {
|
2016-07-26 08:19:35 +02:00
|
|
|
if ($a[$i] !== $b[$i]) {
|
|
|
|
break;
|
|
|
|
}
|
2015-09-05 02:24:56 +02:00
|
|
|
}
|
|
|
|
return $i === 0 ? 0 : (float) $i / count($a);
|
|
|
|
}
|
|
|
|
}
|