BxDolLanguages.php 10.0 KB

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