BxDolStudioLanguagesUtils.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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. return true;
  133. }
  134. function installLanguage($aLanguageModule, $bRecompile = true)
  135. {
  136. $aLanguage = array();
  137. $this->oDb->getLanguagesBy(array('type' => 'by_name', 'value' => $aLanguageModule['uri']), $aLanguage, false);
  138. if(empty($aLanguage) || !is_array($aLanguage))
  139. return false;
  140. $sPath = BX_DIRECTORY_PATH_MODULES . $aLanguageModule['path'] . 'data/langs/';
  141. if(!($rHandler = opendir($sPath)))
  142. return false;
  143. $oModuleQuery = BxDolModuleQuery::getInstance();
  144. $bResult = false;
  145. while(($sFileName = readdir($rHandler)) !== false) {
  146. if($sFileName == '.' || $sFileName == '..')
  147. continue;
  148. $aModule = $oModuleQuery->getModuleByName($sFileName);
  149. if(empty($aModule) || !is_array($aModule))
  150. continue;
  151. $bResult |= $this->_restoreLanguageByModule($aLanguage, $sPath, $aModule);
  152. }
  153. closedir($rHandler);
  154. $bResult |= $this->_restoreLanguageByModule($aLanguage, $sPath, $aLanguageModule);
  155. return $bResult && ($bRecompile ? $this->compileLanguage($aLanguage['id']) : true);
  156. }
  157. function restoreLanguage($mixedLang = 0, $mixedModule = 0, $bRecompile = true)
  158. {
  159. $aLanguages = array();
  160. if(!empty($mixedLang)) {
  161. $sType = is_string($mixedLang) && !is_numeric($mixedLang) ? 'by_name' : 'by_id';
  162. $this->oDb->getLanguagesBy(array('type' => $sType, 'value' => $mixedLang), $aLanguages[0], false);
  163. }
  164. else
  165. $this->oDb->getLanguagesBy(array('type' => 'all'), $aLanguages, false);
  166. $bResult = true;
  167. foreach($aLanguages as $aLanguage) {
  168. if(empty($aLanguage) || !is_array($aLanguage))
  169. continue;
  170. $oModuleQuery = BxDolModuleQuery::getInstance();
  171. $aLanguageModule = $oModuleQuery->getModuleByUri($aLanguage['name']);
  172. if(empty($aLanguageModule) || !is_array($aLanguageModule))
  173. continue;
  174. $sPath = BX_DIRECTORY_PATH_MODULES . $aLanguageModule['path'] . 'data/langs/';
  175. if(!empty($mixedModule))
  176. $bResult &= $this->_restoreLanguageByModule($aLanguage, $sPath, $mixedModule);
  177. else {
  178. $aModules = $oModuleQuery->getModules();
  179. foreach($aModules as $aModule)
  180. $bResult &= $this->_restoreLanguageByModule($aLanguage, $sPath, $aModule);
  181. }
  182. if($bRecompile)
  183. $bResult &= $this->compileLanguage($aLanguage['id']);
  184. }
  185. return $bResult;
  186. }
  187. function removeLanguageById($mixedLang = 0, $bRecompile = true)
  188. {
  189. $aLanguages = array();
  190. if(!empty($mixedLang)) {
  191. $sType = is_string($mixedLang) && !is_numeric($mixedLang) ? 'by_name' : 'by_id';
  192. $this->oDb->getLanguagesBy(array('type' => $sType, 'value' => $mixedLang), $aLanguages[0], false);
  193. } else
  194. $this->oDb->getLanguagesBy(array('type' => 'all'), $aLanguages, false);
  195. $bResult = true;
  196. foreach($aLanguages as $aLanguage) {
  197. if(empty($aLanguage) || !is_array($aLanguage))
  198. continue;
  199. $bResult &= (int)$this->oDb->deleteStringsBy(array('type' => 'by_lang', 'language_id' => $aLanguage['id'])) >= 0;
  200. if($bRecompile)
  201. $bResult &= $this->compileLanguage($aLanguage['id']);
  202. }
  203. return $bResult;
  204. }
  205. function removeLanguageByModule($mixedModule, $bRecompile = true)
  206. {
  207. $aModule = $mixedModule;
  208. if(!is_array($mixedModule)) {
  209. $sMethod = is_string($mixedModule) && !is_numeric($mixedModule) ? 'getModuleByUri' : 'getModuleById';
  210. $aModule = BxDolModuleQuery::getInstance()->$sMethod($mixedModule);
  211. }
  212. if(empty($aModule) || !is_array($aModule) || !$aModule['uri'] || !$aModule['lang_category'])
  213. return false;
  214. $aCategory = array();
  215. $this->oDb->getCategoriesBy(array('type' => 'by_name', 'value' => $aModule['lang_category']), $aCategory, false);
  216. if(empty($aCategory) || !is_array($aCategory))
  217. return false;
  218. $bResult = (int)$this->oDb->deleteKeysBy(array('type' => 'by_cat_id', 'value' => $aCategory['id'])) >= 0;
  219. return $bResult && ($bRecompile ? $this->compileLanguage() : true);
  220. }
  221. function getLanguageString($sKey, $iLangId = 0)
  222. {
  223. if($iLangId == 0) {
  224. $aStrings = array();
  225. $this->oDb->getStringsBy(array('type' => 'all_by_key_key_language_id', 'value' => $sKey), $aStrings, false);
  226. return $aStrings;
  227. }
  228. $aString = array();
  229. $this->oDb->getStringsBy(array('type' => 'by_key_language_id', 'key' => $sKey, 'language_id' => $iLangId), $aString, false);
  230. return $aString;
  231. }
  232. function addLanguageString($sKey, $mixedString, $iLangId = 0, $iCategoryId = 0, $bRecompile = true)
  233. {
  234. $iLangId = (int)$iLangId;
  235. $iCategoryId = (int)$iCategoryId;
  236. if($iCategoryId == 0)
  237. $iCategoryId = BX_DOL_LANGUAGE_CATEGORY_CUSTOM;
  238. $sKey = bx_process_input($sKey);
  239. $mixedString = bx_process_input($mixedString);
  240. $aLanguages = array();
  241. $iLanguages = $this->oDb->getLanguagesBy(($iLangId == 0 ? array('type' => 'all') : array('type' => 'all_by_id', 'value' => $iLangId)), $aLanguages);
  242. if(($iKeyId = $this->oDb->addKey($iCategoryId, $sKey)) === false)
  243. return false;
  244. foreach($aLanguages as $aLanguage) {
  245. $sString = '';
  246. if(is_string($mixedString))
  247. $sString = $mixedString;
  248. else if(is_array($mixedString))
  249. $sString = $mixedString[$aLanguage['id']];
  250. if(!$this->oDb->addString($iKeyId, $aLanguage['id'], $sString))
  251. return false;
  252. if($bRecompile)
  253. $this->compileLanguage($aLanguage['id']);
  254. }
  255. return $iKeyId;
  256. }
  257. function updateLanguageString($sKey, $mixedString, $iLangId = 0, $iCategoryId = 0, $bRecompile = true)
  258. {
  259. $sKey = bx_process_input($sKey);
  260. $mixedString = bx_process_input($mixedString);
  261. $iLangId = (int)$iLangId;
  262. $iCategoryId = (int)$iCategoryId;
  263. if(empty($iCategoryId))
  264. $iCategoryId = BX_DOL_LANGUAGE_CATEGORY_CUSTOM;
  265. $aKey = array();
  266. $this->oDb->getKeysBy(array('type' => 'by_name', 'value' => $sKey), $aKey);
  267. if(empty($aKey))
  268. return $this->addLanguageString($sKey, $mixedString, $iLangId, $iCategoryId, $bRecompile);
  269. if((int)$aKey['category_id'] != $iCategoryId)
  270. $this->oDb->updateKeys(array('IDCategory' => $iCategoryId), array('ID' => $aKey['id']));
  271. return $this->updateLanguageStringById($aKey['id'], $mixedString, $iLangId, $bRecompile);
  272. }
  273. function updateLanguageStringById($iKeyId, $mixedString, $iLangId = 0, $bRecompile = true)
  274. {
  275. $iKeyId = (int)$iKeyId;
  276. $mixedString = bx_process_input($mixedString);
  277. $iLangId = (int)$iLangId;
  278. $aLanguages = array();
  279. $iLanguages = $this->oDb->getLanguagesBy(($iLangId == 0 ? array('type' => 'all') : array('type' => 'all_by_id', 'value' => $iLangId)), $aLanguages);
  280. foreach($aLanguages as $aLanguage) {
  281. $sString = '';
  282. if(is_string($mixedString))
  283. $sString = $mixedString;
  284. else if(is_array($mixedString))
  285. $sString = $mixedString[$aLanguage['id']];
  286. if($this->oDb->updateString($iKeyId, $aLanguage['id'], $sString) && $bRecompile)
  287. $this->compileLanguage($aLanguage['id']);
  288. }
  289. return true;
  290. }
  291. function deleteLanguageString($sKey, $iLangId = 0, $bRecompile = true)
  292. {
  293. $iLangId = (int)$iLangId;
  294. $sKey = bx_process_input($sKey);
  295. $aKey = array();
  296. $this->oDb->getKeysBy(array('type' => 'by_name', 'value' => $sKey), $aKey);
  297. if(empty($aKey))
  298. return false;
  299. return $this->deleteLanguageStringById($aKey['id'], $iLangId, $bRecompile);
  300. }
  301. function deleteLanguageStringById($iKeyId, $iLangId = 0, $bRecompile = true)
  302. {
  303. $iKeyId = (int)$iKeyId;
  304. $iLangId = (int)$iLangId;
  305. $aLanguages = array();
  306. $iLanguages = $this->oDb->getLanguagesBy(($iLangId == 0 ? array('type' => 'all') : array('type' => 'all_by_id', 'value' => $iLangId)), $aLanguages);
  307. foreach($aLanguages as $aLanguage) {
  308. if(!$this->oDb->deleteString($iKeyId, $aLanguage['id']))
  309. continue;
  310. if($bRecompile)
  311. $this->compileLanguage($aLanguage['id']);
  312. }
  313. return $this->oDb->deleteKey($iKeyId);
  314. }
  315. function addLanguageCategory($sName)
  316. {
  317. return $this->oDb->addCategory($sName);
  318. }
  319. function deleteLanguageCategory($sName)
  320. {
  321. return $this->oDb->deleteCategory($sName);
  322. }
  323. protected function _restoreLanguageByModule($aLanguage, $sPath, $mixedModule)
  324. {
  325. $oXmlParser = BxDolXmlParser::getInstance();
  326. $aModule = $mixedModule;
  327. if(!is_array($mixedModule)) {
  328. $sMethod = is_string($mixedModule) && !is_numeric($mixedModule) ? 'getModuleByUri' : 'getModuleById';
  329. $aModule = BxDolModuleQuery::getInstance()->$sMethod($mixedModule);
  330. }
  331. if(empty($aModule) || !is_array($aModule) || !$aModule['name'] || !$aModule['lang_category'])
  332. return true;
  333. $sPath = $sPath . $aModule['name'] . '/' . $aLanguage['name'] . '.xml';
  334. if(!file_exists($sPath) && isset($aModule['path'])) {
  335. $sPath = BX_DIRECTORY_PATH_MODULES . $aModule['path'] . 'install/langs/' . $aLanguage['name'] . '.xml';
  336. if(!file_exists($sPath))
  337. return true;
  338. }
  339. $aLanguageInfo = $this->readLanguage($sPath);
  340. if(empty($aLanguageInfo['name']) || empty($aLanguageInfo['strings']) || $aLanguageInfo['name'] != $aLanguage['name'])
  341. return false;
  342. $aCategory = array();
  343. $this->oDb->getCategoriesBy(array('type' => 'by_name', 'value' => $aModule['lang_category']), $aCategory, false);
  344. $iCategoryId = 0;
  345. if(empty($aCategory) || !is_array($aCategory)) {
  346. $iCategoryId = $this->addLanguageCategory($aModule['lang_category']);
  347. if(empty($iCategoryId))
  348. return false;
  349. }
  350. else
  351. $iCategoryId = $aCategory['id'];
  352. $bDeleteStringsByKey = !$this->oDb->deleteStringsBy(array('type' => 'by_cat_and_lang', 'category_id' => $iCategoryId, 'language_id' => $aLanguage['id']));
  353. $bResult = true;
  354. foreach($aLanguageInfo['strings'] as $sKey => $sValue)
  355. if($sKey != '') {
  356. if($bDeleteStringsByKey)
  357. $this->oDb->deleteStringsBy(array('type' => 'by_key_and_lang', 'key' => $sKey, 'language_id' => $aLanguage['id']));
  358. $bResult &= $this->addLanguageString($sKey, $sValue, $aLanguage['id'], $iCategoryId, false) > 0;
  359. }
  360. return $bResult;
  361. }
  362. }
  363. /** @} */