BxDolStudioLanguagesUtils.php 17 KB

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