BxDolLanguages.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaCore UNA Core
  7. * @{
  8. */
  9. define('BX_DOL_LANGUAGE_DEFAULT', 'en');
  10. define('BX_DOL_LANGUAGE_DIRECTION_LTR', 'LTR');
  11. define('BX_DOL_LANGUAGE_DIRECTION_RTL', 'RTL');
  12. define('BX_DOL_LANGUAGE_CATEGORY_SYSTEM', 1);
  13. define('BX_DOL_LANGUAGE_CATEGORY_CUSTOM', 2);
  14. class BxDolLanguages extends BxDolFactory implements iBxDolSingleton
  15. {
  16. protected $oDb;
  17. protected $sCurrentLanguage;
  18. protected function __construct()
  19. {
  20. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  21. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  22. parent::__construct();
  23. $this->oDb = BxDolLanguagesQuery::getInstance();
  24. }
  25. /**
  26. * Prevent cloning the instance
  27. */
  28. public function __clone()
  29. {
  30. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  31. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  32. }
  33. /**
  34. * Get singleton instance of the class
  35. */
  36. static function getInstance()
  37. {
  38. if(!isset($GLOBALS['bxDolClasses'][__CLASS__])) {
  39. $GLOBALS['bxDolClasses'][__CLASS__] = new BxDolLanguages();
  40. $GLOBALS['bxDolClasses'][__CLASS__]->init();
  41. }
  42. return $GLOBALS['bxDolClasses'][__CLASS__];
  43. }
  44. function getCurrentLangName($bSetCookie = true)
  45. {
  46. $sLang = '';
  47. if(!$sLang && !empty($_GET['lang']))
  48. $sLang = $this->tryToGetLang($_GET['lang'], $bSetCookie);
  49. if(!$sLang && !empty($_POST['lang']))
  50. $sLang = $this->tryToGetLang($_POST['lang'], $bSetCookie);
  51. if(!$sLang && !empty($_COOKIE['lang']))
  52. $sLang = $this->tryToGetLang($_COOKIE['lang']);
  53. if(!$sLang && !empty($_SERVER['HTTP_ACCEPT_LANGUAGE']))
  54. $sLang = $this->tryToGetLang($_SERVER['HTTP_ACCEPT_LANGUAGE']);
  55. if(!$sLang)
  56. $sLang = $this->tryToGetLang(getParam('lang_default'));
  57. if(!$sLang)
  58. $sLang = $this->tryToGetLang(BX_DOL_LANGUAGE_DEFAULT);
  59. setlocale(LC_TIME, $sLang . '_' . strtoupper($sLang) . '.utf-8', $sLang . '_' . strtoupper($sLang) . '.utf8', $sLang . '.utf-8', $sLang . '.utf8', $sLang);
  60. return $sLang;
  61. }
  62. function getCurrentLangId()
  63. {
  64. return $this->getLangId($this->getCurrentLanguage());
  65. }
  66. function getLangId($sLang)
  67. {
  68. return $this->oDb->getLanguageId($sLang);
  69. }
  70. function getLangFlag($sLang = '')
  71. {
  72. if (!$sLang)
  73. $sLang = $this->getCurrentLanguage();
  74. return $this->oDb->getLanguageFlag($sLang);
  75. }
  76. function getLangDirection($sLang = '')
  77. {
  78. if (!$sLang)
  79. $sLang = $this->getCurrentLanguage();
  80. return strtoupper($this->oDb->getLanguageDirection($sLang));
  81. }
  82. function getDefaultLangName()
  83. {
  84. return getParam('lang_default');
  85. }
  86. function getLanguages($bIdAsKey = false, $bActiveOnly = false)
  87. {
  88. return $this->oDb->getLanguages($bIdAsKey, $bActiveOnly);
  89. }
  90. function getLanguageCategory($sName)
  91. {
  92. $iId = 0;
  93. $this->oDb->getCategoriesBy(array('type' => 'id_by_name', 'value' => $sName), $iId);
  94. return (int)$iId;
  95. }
  96. /**
  97. * It tries to match current language with provided languages array, if nothing is found $sFallbackLanguage is returned.
  98. * @param $aLangs - array of languages, example: array('ru' => 1, 'kg' => 1);
  99. * @param $sFallbackLanguage - language to return of nothis is found
  100. * @return language code string
  101. */
  102. function detectLanguageFromArray($aLangs, $sFallbackLanguage = 'en')
  103. {
  104. return isset($aLangs[$GLOBALS['sCurrentLanguage']]) ? $GLOBALS['sCurrentLanguage'] : $sFallbackLanguage;
  105. }
  106. /**
  107. * Get current language.
  108. */
  109. function getCurrentLanguage()
  110. {
  111. return $GLOBALS['sCurrentLanguage'];
  112. }
  113. function _t()
  114. {
  115. global $LANG;
  116. $key = func_get_arg(0);
  117. if(isset($LANG[$key])) {
  118. $str = $LANG[$key];
  119. if(($iNumArgs = func_num_args()) > 1)
  120. for($i = 1; $i < $iNumArgs; $i++)
  121. $str = str_replace('{' . ($i - 1) . '}', func_get_arg($i), $str);
  122. return $str;
  123. } else
  124. return $key;
  125. }
  126. function _t_err()
  127. {
  128. return MsgBox(call_user_func_array(array($this, '_t'), func_get_args()));
  129. }
  130. function _t_action()
  131. {
  132. return MsgBox(call_user_func_array(array($this, '_t'), func_get_args()));
  133. }
  134. function _t_ext($key, $args)
  135. {
  136. global $LANG;
  137. if(isset($LANG[$key])) {
  138. $str = $LANG[$key];
  139. if(!is_array($args))
  140. return str_replace('{0}', $args, $str);
  141. foreach ($args as $key => $val)
  142. $str = str_replace('{'.$key.'}', $val, $str);
  143. return $str;
  144. } else
  145. return $key;
  146. }
  147. function _t_format_size ($iSize)
  148. {
  149. $a = array (
  150. 1024 => '_sys_format_size_b',
  151. 1024*1024 => '_sys_format_size_kb',
  152. 1024*1024*1024 => '_sys_format_size_mb',
  153. 1024*1024*1024*1024 => '_sys_format_size_gb',
  154. 1024*1024*1024*1024*1024 => '_sys_format_size_tb',
  155. );
  156. foreach($a as $i => $sKey)
  157. if($iSize < $i)
  158. return $this->_t($sKey, round($iSize / ($i / 1024), 1));
  159. return $this->_t('_sys_format_size_b', 0);
  160. }
  161. function _t_format_duration ($iTime)
  162. {
  163. $iTime = (int)$iTime;
  164. $sFormat = '%M:%S';
  165. if($iTime > 3600)
  166. $sFormat = '%H:' . $sFormat;
  167. return strftime($sFormat, $iTime);
  168. }
  169. function _t_format_currency ($fPrice)
  170. {
  171. return $this->_t('_sys_currency', getParam('currency_sign'), sprintf("%.2f", (float)$fPrice));
  172. }
  173. function _t_format_extensions ($mixedExtensions)
  174. {
  175. if (!is_array($mixedExtensions))
  176. $a = explode(',', $mixedExtensions);
  177. else
  178. $a = $mixedExtensions;
  179. if (!$a)
  180. return '';
  181. return '.' . implode(', .', $a);
  182. }
  183. protected function init()
  184. {
  185. /**
  186. * Trying to initialize default language.
  187. */
  188. $GLOBALS['sCurrentLanguage'] = $GLOBALS['bxDolClasses'][__CLASS__]->getCurrentLangName(false);
  189. if($GLOBALS['sCurrentLanguage'] != '') {
  190. $sPath = BX_DIRECTORY_PATH_CACHE . 'lang-' . $GLOBALS['sCurrentLanguage'] . '.php';
  191. if(!file_exists($sPath))
  192. BxDolStudioLanguagesUtils::getInstance()->compileLanguage();
  193. require($sPath);
  194. if($this->getLangDirection($GLOBALS['sCurrentLanguage']) == BX_DOL_LANGUAGE_DIRECTION_RTL)
  195. BxDolTemplate::getInstance()->addCss('rtl.css');
  196. }
  197. $GLOBALS['bxDolClasses'][__CLASS__]->getCurrentLangName(true);
  198. if(isset($_GET['lang'])) {
  199. if(BxDolPermalinks::getInstance()->redirectIfNecessary(array('lang')))
  200. exit;
  201. }
  202. }
  203. protected function tryToGetLang($sLangs, $bSetCookie = false)
  204. {
  205. $sLangs = trim($sLangs);
  206. if(!$sLangs)
  207. return '';
  208. $sLangs = preg_replace( '/[^a-zA-Z0-9,;-]/m', '', $sLangs ); // we do not need 'q=0.3'. we are using live queue :)
  209. $sLangs = strtolower($sLangs);
  210. if(!$sLangs)
  211. return '';
  212. $aLangs = explode(',', $sLangs); // ru,en-us;q=0.7,en;q=0.3 => array( 'ru' , 'en-us;q=0.7' , 'en;q=0.3' );
  213. foreach($aLangs as $sLang) {
  214. if(!$sLang)
  215. continue;
  216. list($sLang) = explode(';', $sLang, 2); // en-us;q=0.7 => en-us
  217. if(!$sLang)
  218. continue;
  219. // check with country
  220. if($this->checkLangExists($sLang)) {
  221. if( $bSetCookie && (!isset($_COOKIE['lang']) || $_COOKIE['lang'] != $sLang) && (!isset($GLOBALS['glLangSet']) || $GLOBALS['glLangSet'] != $sLang)) {
  222. $this->setLangCookie( $sLang );
  223. $GLOBALS['glLangSet'] = $sLang;
  224. }
  225. return $sLang;
  226. }
  227. //drop country
  228. if(strpos($sLang, '-') === false)
  229. continue;
  230. list($sLang, $sCntr) = explode('-', $sLang, 2); // en-us => en
  231. if(!$sLang or !$sCntr)
  232. continue; //no lang or nothing changed
  233. //check again. without country
  234. if($this->checkLangExists($sLang)) {
  235. if($bSetCookie)
  236. $this->setLangCookie($sLang);
  237. return $sLang;
  238. }
  239. }
  240. return '';
  241. }
  242. protected function checkLangExists( $sLang )
  243. {
  244. if(!preg_match('/^[A-Za-z0-9_]+$/', $sLang))
  245. return false;
  246. $iLangId = $this->oDb->getLanguageId($sLang, false);
  247. if(!$iLangId)
  248. return false;
  249. if(file_exists( BX_DIRECTORY_PATH_CACHE . "lang-{$sLang}.php"))
  250. return true;
  251. if(BxDolStudioLanguagesUtils::getInstance()->compileLanguage($iLangId))
  252. return true;
  253. return false;
  254. }
  255. protected function setLangCookie( $sLang )
  256. {
  257. $sLang = bx_process_input($sLang);
  258. if(isLogged()) {
  259. $iLangId = $this->oDb->getLanguageId($sLang, false);
  260. if(!$iLangId)
  261. $iLangId = 0;
  262. $iAccountId = getLoggedId();
  263. $oAccountQuery = BxDolAccountQuery::getInstance();
  264. $oAccountQuery->updateLanguage($iAccountId, $iLangId);
  265. }
  266. setcookie( 'lang', '', time() - 60*60*24, '/' );
  267. setcookie( 'lang', $sLang, time() + 60*60*24*365, '/' );
  268. }
  269. }
  270. if (!function_exists('_t')) {
  271. function _t()
  272. {
  273. return call_user_func_array(array(BxDolLanguages::getInstance(), '_t'), func_get_args());
  274. }
  275. }
  276. function _t_err()
  277. {
  278. return call_user_func_array(array(BxDolLanguages::getInstance(), '_t_err'), func_get_args());
  279. }
  280. function _t_action()
  281. {
  282. return call_user_func_array(array(BxDolLanguages::getInstance(), '_t_action'), func_get_args());
  283. }
  284. function _t_ext($key, $args)
  285. {
  286. return BxDolLanguages::getInstance()->_t_ext($key, $args);
  287. }
  288. function _t_format_size($iSize)
  289. {
  290. return BxDolLanguages::getInstance()->_t_format_size($iSize);
  291. }
  292. function _t_format_duration($iTime)
  293. {
  294. return BxDolLanguages::getInstance()->_t_format_duration($iTime);
  295. }
  296. function _t_format_currency($fPrice)
  297. {
  298. return BxDolLanguages::getInstance()->_t_format_currency($fPrice);
  299. }
  300. function _t_format_extensions($mixedExtensions)
  301. {
  302. return BxDolLanguages::getInstance()->_t_format_extensions($mixedExtensions);
  303. }
  304. function bx_lang_name()
  305. {
  306. return BxDolLanguages::getInstance()->getCurrentLanguage();
  307. }
  308. function bx_lang_direction($sLanguage = '')
  309. {
  310. return BxDolLanguages::getInstance()->getLangDirection($sLanguage);
  311. }
  312. /** @} */