BxDolLanguages.php 9.8 KB

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