BxDolStudioLanguagesUtils.php 17 KB

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