BxDolStudioLanguagesUtils.php 17 KB

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