BxDolStudioLanguagesUtils.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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 DolphinStudio Dolphin Studio
  7. * @{
  8. */
  9. bx_import('BxDolLanguages');
  10. bx_import('BxDolXmlParser');
  11. bx_import('BxDolStudioLanguagesUtilsQuery');
  12. class BxDolStudioLanguagesUtils extends BxDolLanguages implements iBxDolSingleton
  13. {
  14. function __construct()
  15. {
  16. if(isset($GLOBALS['bxDolClasses'][get_class($this)]))
  17. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  18. parent::__construct();
  19. $this->oDb = new BxDolStudioLanguagesUtilsQuery();
  20. }
  21. /**
  22. * Prevent cloning the instance
  23. */
  24. public function __clone()
  25. {
  26. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  27. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  28. }
  29. /**
  30. * Get singleton instance of the class
  31. */
  32. public static function getInstance()
  33. {
  34. if(!isset($GLOBALS['bxDolClasses'][__CLASS__]))
  35. $GLOBALS['bxDolClasses'][__CLASS__] = new BxDolStudioLanguagesUtils();
  36. return $GLOBALS['bxDolClasses'][__CLASS__];
  37. }
  38. function getLanguageInfo($sName)
  39. {
  40. $aLanguage = array();
  41. $this->oDb->getLanguagesBy(array('type' => 'by_name', 'value' => $sName), $aLanguage, false);
  42. bx_import('BxDolStudioTemplate');
  43. if(!empty($aLanguage))
  44. $aLanguage['icon'] = BxDolStudioTemplate::getInstance()->getIconUrl('sys_fl_' . $aLanguage['flag'] . '.gif');
  45. return $aLanguage;
  46. }
  47. function getLanguagesInfo($bIdAsKey = false, $bActiveOnly = false)
  48. {
  49. $aLanguages = array();
  50. $this->oDb->getLanguagesBy(array('type' => 'all'), $aLanguages, false);
  51. if(!is_array($aLanguages) || empty($aLanguages))
  52. return $aLanguages;
  53. bx_import('BxDolStudioTemplate');
  54. $oTemplate = BxDolStudioTemplate::getInstance();
  55. foreach($aLanguages as $iKey => $aLanguage)
  56. $aLanguages[$iKey]['icon'] = $oTemplate->getIconUrl('sys_fl_' . $aLanguage['flag'] . '.gif');
  57. return $aLanguages;
  58. }
  59. function readLanguage($sPath, $sType = 'restore')
  60. {
  61. if(!file_exists($sPath))
  62. return array();
  63. $oXmlParser = BxDolXmlParser::getInstance();
  64. $sXmlContent = file_get_contents($sPath);
  65. $aResult = array();
  66. $aResult['name'] = $oXmlParser->getAttribute($sXmlContent, 'resources', 'name');
  67. switch($sType) {
  68. case 'restore':
  69. $aResult['strings'] = $oXmlParser->getValues($sXmlContent, 'string');
  70. break;
  71. case 'update':
  72. $aResult['strings_del'] = $oXmlParser->getValues($sXmlContent, 'string_del');
  73. $aResult['strings_add'] = $oXmlParser->getValues($sXmlContent, 'string_add');
  74. $aResult['strings_upd'] = $oXmlParser->getValues($sXmlContent, 'string_upd');
  75. break;
  76. }
  77. return $aResult;
  78. }
  79. function compileLanguage($mixedLang = 0, $bForce = false)
  80. {
  81. $sType = 'all';
  82. if(!empty($mixedLang)) {
  83. if(is_string($mixedLang) && !is_numeric($mixedLang))
  84. $sType = 'all_by_name';
  85. else if(is_numeric($mixedLang) || is_int($mixedLang)) {
  86. $sType = 'all_by_id';
  87. $mixedLang = (int)$mixedLang;
  88. }
  89. }
  90. $aLanguages = array();
  91. $iLanguages = $this->oDb->getLanguagesBy(array('type' => $sType, 'value' => $mixedLang), $aLanguages);
  92. if($iLanguages == 0)
  93. return false;
  94. bx_import('BxDolFile');
  95. $oFile = BxDolFile::getInstance();
  96. $sNewLine = "\r\n";
  97. foreach($aLanguages as $aLanguage) {
  98. if(!$bForce && (int)$aLanguage['enabled'] != 1)
  99. continue;
  100. $aKeys = array();
  101. $this->oDb->getKeysBy(array('type' => 'by_language_id_key_key', 'value' => $aLanguage['id']), $aKeys);
  102. if($aLanguage['name'] != BX_DOL_LANGUAGE_DEFAULT && getParam('lang_subst_from_en') == 'on') {
  103. $aKeysAll = array();
  104. $this->oDb->getKeysBy(array('type' => 'by_language_name_key_key', 'value' => BX_DOL_LANGUAGE_DEFAULT), $aKeysAll);
  105. $aKeys = array_merge($aKeysAll, $aKeys);
  106. }
  107. $sLanguageFile = "lang-" . $aLanguage['name'] . ".php";
  108. $oFile->delete('cache/' . $sLanguageFile);
  109. $rHandle = fopen( BX_DIRECTORY_PATH_CACHE . $sLanguageFile, 'w');
  110. if($rHandle === false)
  111. return false;
  112. $sContent = "<?php{$sNewLine}\$GLOBALS['LANG'] = array(";
  113. foreach($aKeys as $aKey) {
  114. list($sKey, $sString) = str_replace(array("\\", "'"), array("\\\\", "\\'"), array($aKey['key'], $aKey['string']));
  115. $sContent .= "{$sNewLine}\t'$sKey' => '$sString',";
  116. }
  117. $sContent = trim($sContent, ',');
  118. if(fwrite($rHandle, $sContent."{$sNewLine});?>") === false)
  119. return false;
  120. if(fclose($rHandle) === false)
  121. return false;
  122. @chmod( BX_DIRECTORY_PATH_CACHE . "lang-{$aLanguage['name']}.php", 0666);
  123. }
  124. $this->init();
  125. return true;
  126. }
  127. function installLanguage($aLanguageModule, $bRecompile = true)
  128. {
  129. $aLanguage = array();
  130. $this->oDb->getLanguagesBy(array('type' => 'by_name', 'value' => $aLanguageModule['uri']), $aLanguage, false);
  131. if(empty($aLanguage) || !is_array($aLanguage))
  132. return false;
  133. $sPath = BX_DIRECTORY_PATH_MODULES . $aLanguageModule['path'] . 'data/langs/';
  134. if(!($rHandler = opendir($sPath)))
  135. return false;
  136. $bResult = false;
  137. while(($sFileName = readdir($rHandler)) !== false) {
  138. if(substr($sFileName, -3) != 'xml')
  139. continue;
  140. $sModule = substr($sFileName, 0, -4);
  141. $bResult |= $this->_restoreLanguageByModule($aLanguage, $sPath, $sModule);
  142. }
  143. closedir($rHandler);
  144. $bResult |= $this->_restoreLanguageByModule($aLanguage, $sPath, $aLanguageModule);
  145. return $bResult && ($bRecompile ? $this->compileLanguage($aLanguage['id']) : true);
  146. }
  147. function restoreLanguage($mixedLang = 0, $mixedModule = 0, $bRecompile = true)
  148. {
  149. $aLanguages = array();
  150. if(!empty($mixedLang)) {
  151. $sType = is_string($mixedLang) && !is_numeric($mixedLang) ? 'by_name' : 'by_id';
  152. $this->oDb->getLanguagesBy(array('type' => $sType, 'value' => $mixedLang), $aLanguages[0], false);
  153. }
  154. else
  155. $this->oDb->getLanguagesBy(array('type' => 'all'), $aLanguages, false);
  156. $bResult = true;
  157. foreach($aLanguages as $aLanguage) {
  158. if(empty($aLanguage) || !is_array($aLanguage))
  159. continue;
  160. bx_import('BxDolModuleQuery');
  161. $oModuleQuery = BxDolModuleQuery::getInstance();
  162. $aLanguageModule = $oModuleQuery->getModuleByUri($aLanguage['name']);
  163. if(empty($aLanguageModule) || !is_array($aLanguageModule))
  164. continue;
  165. $sPath = BX_DIRECTORY_PATH_MODULES . $aLanguageModule['path'] . 'data/langs/';
  166. if(!empty($mixedModule))
  167. $bResult &= $this->_restoreLanguageByModule($aLanguage, $sPath, $mixedModule);
  168. else {
  169. $aModules = $oModuleQuery->getModules();
  170. foreach($aModules as $aModule)
  171. $bResult &= $this->_restoreLanguageByModule($aLanguage, $sPath, $aModule);
  172. }
  173. if($bRecompile)
  174. $bResult &= $this->compileLanguage($aLanguage['id']);
  175. }
  176. return $bResult;
  177. }
  178. function removeLanguageById($mixedLang = 0, $bRecompile = true)
  179. {
  180. $aLanguages = array();
  181. if(!empty($mixedLang)) {
  182. $sType = is_string($mixedLang) && !is_numeric($mixedLang) ? 'by_name' : 'by_id';
  183. $this->oDb->getLanguagesBy(array('type' => $sType, 'value' => $mixedLang), $aLanguages[0], false);
  184. } else
  185. $this->oDb->getLanguagesBy(array('type' => 'all'), $aLanguages, false);
  186. $bResult = true;
  187. foreach($aLanguages as $aLanguage) {
  188. if(empty($aLanguage) || !is_array($aLanguage))
  189. continue;
  190. $bResult &= (int)$this->oDb->deleteStringsBy(array('type' => 'by_lang', 'language_id' => $aLanguage['id'])) >= 0;
  191. if($bRecompile)
  192. $bResult &= $this->compileLanguage($aLanguage['id']);
  193. }
  194. return $bResult;
  195. }
  196. function removeLanguageByModule($mixedModule, $bRecompile = true)
  197. {
  198. $aModule = $mixedModule;
  199. if(!is_array($mixedModule)) {
  200. $sMethod = is_string($mixedModule) && !is_numeric($mixedModule) ? 'getModuleByUri' : 'getModuleById';
  201. $aModule = BxDolModuleQuery::getInstance()->$sMethod($mixedModule);
  202. }
  203. if(empty($aModule) || !is_array($aModule) || !$aModule['uri'] || !$aModule['lang_category'])
  204. return false;
  205. $aCategory = array();
  206. $this->oDb->getCategoriesBy(array('type' => 'by_name', 'value' => $aModule['lang_category']), $aCategory, false);
  207. if(empty($aCategory) || !is_array($aCategory))
  208. return false;
  209. $bResult = (int)$this->oDb->deleteKeysBy(array('type' => 'by_cat_id', 'value' => $aCategory['id'])) >= 0;
  210. return $bResult && ($bRecompile ? $this->compileLanguage() : true);
  211. }
  212. function getLanguageString($sKey, $iLangId = 0)
  213. {
  214. if($iLangId == 0) {
  215. $aStrings = array();
  216. $this->oDb->getStringsBy(array('type' => 'all_by_key_key_language_id', 'value' => $sKey), $aStrings, false);
  217. return $aStrings;
  218. }
  219. $aString = array();
  220. $this->oDb->getStringsBy(array('type' => 'by_key_language_id', 'key' => $sKey, 'language_id' => $iLangId), $aString, false);
  221. return $aString;
  222. }
  223. function addLanguageString($sKey, $mixedString, $iLangId = 0, $iCategoryId = 0, $bRecompile = true)
  224. {
  225. $iLangId = (int)$iLangId;
  226. $iCategoryId = (int)$iCategoryId;
  227. if($iCategoryId == 0)
  228. $iCategoryId = BX_DOL_LANGUAGE_CATEGORY_CUSTOM;
  229. $sKey = bx_process_input($sKey);
  230. $mixedString = bx_process_input($mixedString);
  231. $aLanguages = array();
  232. $iLanguages = $this->oDb->getLanguagesBy(($iLangId == 0 ? array('type' => 'all') : array('type' => 'all_by_id', 'value' => $iLangId)), $aLanguages);
  233. if(($iKeyId = $this->oDb->addKey($iCategoryId, $sKey)) === false)
  234. return false;
  235. foreach($aLanguages as $aLanguage) {
  236. $sString = '';
  237. if(is_string($mixedString))
  238. $sString = $mixedString;
  239. else if(is_array($mixedString))
  240. $sString = $mixedString[$aLanguage['id']];
  241. if(!$this->oDb->addString($iKeyId, $aLanguage['id'], $sString))
  242. return false;
  243. if($bRecompile)
  244. $this->compileLanguage($aLanguage['id']);
  245. }
  246. return $iKeyId;
  247. }
  248. function updateLanguageString($sKey, $sString, $iLangId = 0, $bRecompile = true)
  249. {
  250. $sKey = bx_process_input($sKey);
  251. $sString = bx_process_input($sString);
  252. $iLangId = (int)$iLangId;
  253. $aKey = array();
  254. $this->oDb->getKeysBy(array('type' => 'by_name', 'value' => $sKey), $aKey);
  255. if(empty($aKey))
  256. return $this->addLanguageString($sKey, $sString, $iLangId, BX_DOL_LANGUAGE_CATEGORY_CUSTOM, $bRecompile);
  257. return $this->updateLanguageStringById($aKey['id'], $sString, $iLangId, $bRecompile);
  258. }
  259. function updateLanguageStringById($iKeyId, $mixedString, $iLangId = 0, $bRecompile = true)
  260. {
  261. $iKeyId = (int)$iKeyId;
  262. $mixedString = bx_process_input($mixedString);
  263. $iLangId = (int)$iLangId;
  264. $aLanguages = array();
  265. $iLanguages = $this->oDb->getLanguagesBy(($iLangId == 0 ? array('type' => 'all') : array('type' => 'all_by_id', 'value' => $iLangId)), $aLanguages);
  266. foreach($aLanguages as $aLanguage) {
  267. $sString = '';
  268. if(is_string($mixedString))
  269. $sString = $mixedString;
  270. else if(is_array($mixedString))
  271. $sString = $mixedString[$aLanguage['id']];
  272. if($this->oDb->updateString($iKeyId, $aLanguage['id'], $sString) && $bRecompile)
  273. $this->compileLanguage($aLanguage['id']);
  274. }
  275. return true;
  276. }
  277. function deleteLanguageString($sKey, $iLangId = 0, $bRecompile = true)
  278. {
  279. $iLangId = (int)$iLangId;
  280. $sKey = bx_process_input($sKey);
  281. $aKey = array();
  282. $this->oDb->getKeysBy(array('type' => 'by_name', 'value' => $sKey), $aKey);
  283. if(empty($aKey))
  284. return false;
  285. return $this->deleteLanguageStringById($aKey['id'], $iLangId, $bRecompile);
  286. }
  287. function deleteLanguageStringById($iKeyId, $iLangId = 0, $bRecompile = true)
  288. {
  289. $iKeyId = (int)$iKeyId;
  290. $iLangId = (int)$iLangId;
  291. $aLanguages = array();
  292. $iLanguages = $this->oDb->getLanguagesBy(($iLangId == 0 ? array('type' => 'all') : array('type' => 'all_by_id', 'value' => $iLangId)), $aLanguages);
  293. foreach($aLanguages as $aLanguage) {
  294. if(!$this->oDb->deleteString($iKeyId, $aLanguage['id']))
  295. continue;
  296. if($bRecompile)
  297. $this->compileLanguage($aLanguage['id']);
  298. }
  299. return $this->oDb->deleteKey($iKeyId);
  300. }
  301. function addLanguageKeys($iLanguageId, $iCategoryId, &$aKeys)
  302. {
  303. $this->oDb->addKeys($iLanguageId, $iCategoryId, $aKeys);
  304. }
  305. function deleteLanguageKeys($aKeys)
  306. {
  307. return $this->oDb->deleteKeys($aKeys);
  308. }
  309. function addLanguageCategory($sName)
  310. {
  311. return $this->oDb->addCategory($sName);
  312. }
  313. function deleteLanguageCategory($sName)
  314. {
  315. return $this->oDb->deleteCategory($sName);
  316. }
  317. protected function _restoreLanguageByModule($aLanguage, $sPath, $mixedModule)
  318. {
  319. $oXmlParser = BxDolXmlParser::getInstance();
  320. $aModule = $mixedModule;
  321. if(!is_array($mixedModule)) {
  322. $sMethod = is_string($mixedModule) && !is_numeric($mixedModule) ? 'getModuleByUri' : 'getModuleById';
  323. $aModule = BxDolModuleQuery::getInstance()->$sMethod($mixedModule);
  324. }
  325. if(empty($aModule) || !is_array($aModule) || !$aModule['uri'] || !$aModule['lang_category'])
  326. return true;
  327. $sPath = $sPath . $aModule['uri'] . '.xml';
  328. if(!file_exists($sPath) && isset($aModule['path'])) {
  329. $sPath = BX_DIRECTORY_PATH_MODULES . $aModule['path'] . 'install/langs/' . $aLanguage['name'] . '.xml';
  330. if(!file_exists($sPath))
  331. return true;
  332. }
  333. $aLanguageInfo = $this->readLanguage($sPath);
  334. if(empty($aLanguageInfo['name']) || empty($aLanguageInfo['strings']) || $aLanguageInfo['name'] != $aLanguage['name'])
  335. return false;
  336. $aCategory = array();
  337. $this->oDb->getCategoriesBy(array('type' => 'by_name', 'value' => $aModule['lang_category']), $aCategory, false);
  338. $iCategoryId = 0;
  339. if(empty($aCategory) || !is_array($aCategory)) {
  340. $iCategoryId = $this->addLanguageCategory($aModule['lang_category']);
  341. if(empty($iCategoryId))
  342. return false;
  343. }
  344. else
  345. $iCategoryId = $aCategory['id'];
  346. $bDeleteStringsByKey = !$this->oDb->deleteStringsBy(array('type' => 'by_cat_and_lang', 'category_id' => $iCategoryId, 'language_id' => $aLanguage['id']));
  347. $bResult = true;
  348. foreach($aLanguageInfo['strings'] as $sKey => $sValue)
  349. if($sKey != '') {
  350. if($bDeleteStringsByKey)
  351. $this->oDb->deleteStringsBy(array('type' => 'by_key_and_lang', 'key' => $sKey, 'language_id' => $aLanguage['id']));
  352. $bResult &= $this->addLanguageString($sKey, $sValue, $aLanguage['id'], $iCategoryId, false) > 0;
  353. }
  354. return $bResult;
  355. }
  356. }
  357. /** @} */