BxDolLanguages.php 10 KB

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