BxDolStudioLanguagesUtils.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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. $aModulesToProcess = array();
  143. $oModuleQuery = BxDolModuleQuery::getInstance();
  144. /*
  145. * Get modules to be processed from language module.
  146. */
  147. $sPath = BX_DIRECTORY_PATH_MODULES . $aLanguageModule['path'] . 'data/langs/';
  148. if(($rHandler = opendir($sPath)) !== false) {
  149. while(($sFileName = readdir($rHandler)) !== false) {
  150. if($sFileName == '.' || $sFileName == '..')
  151. continue;
  152. $aModule = $oModuleQuery->getModuleByName($sFileName);
  153. if(empty($aModule) || !is_array($aModule))
  154. continue;
  155. $aModulesToProcess[$aModule['name']] = $aModule;
  156. }
  157. closedir($rHandler);
  158. }
  159. /*
  160. * Get modules to be processed from a list of already installed modules.
  161. */
  162. $aModulesInstalled = $oModuleQuery->getModulesBy(array('type' => 'all_key_name'));
  163. if(!empty($aModulesInstalled) && is_array($aModulesInstalled))
  164. $aModulesToProcess = array_merge($aModulesToProcess, $aModulesInstalled);
  165. /*
  166. * Add language module itself.
  167. */
  168. $aModulesToProcess[$aLanguageModule['name']] = $aLanguageModule;
  169. $bResult = false;
  170. foreach($aModulesToProcess as $aModuleToProcess)
  171. $bResult |= $this->_restoreLanguageByModule($aLanguage, $sPath, $aModuleToProcess);
  172. return $bResult && ($bRecompile ? $this->compileLanguage($aLanguage['id']) : true);
  173. }
  174. function restoreLanguage($mixedLang = 0, $mixedModule = 0, $bRecompile = true)
  175. {
  176. $aLanguages = array();
  177. if(!empty($mixedLang)) {
  178. $sType = is_string($mixedLang) && !is_numeric($mixedLang) ? 'by_name' : 'by_id';
  179. $this->oDb->getLanguagesBy(array('type' => $sType, 'value' => $mixedLang), $aLanguages[0], false);
  180. }
  181. else
  182. $this->oDb->getLanguagesBy(array('type' => 'all'), $aLanguages, false);
  183. $bResult = true;
  184. foreach($aLanguages as $aLanguage) {
  185. if(empty($aLanguage) || !is_array($aLanguage))
  186. continue;
  187. $oModuleQuery = BxDolModuleQuery::getInstance();
  188. $aLanguageModule = $oModuleQuery->getModuleByUri($aLanguage['name']);
  189. if(empty($aLanguageModule) || !is_array($aLanguageModule))
  190. continue;
  191. $sPath = BX_DIRECTORY_PATH_MODULES . $aLanguageModule['path'] . 'data/langs/';
  192. if(!empty($mixedModule))
  193. $bResult &= $this->_restoreLanguageByModule($aLanguage, $sPath, $mixedModule);
  194. else {
  195. $aModules = $oModuleQuery->getModules();
  196. foreach($aModules as $aModule)
  197. $bResult &= $this->_restoreLanguageByModule($aLanguage, $sPath, $aModule);
  198. }
  199. if($bRecompile)
  200. $bResult &= $this->compileLanguage($aLanguage['id']);
  201. }
  202. return $bResult;
  203. }
  204. function fixLanguage($mixedLang = 0, $bRecompile = true)
  205. {
  206. $aLanguages = array();
  207. if(!empty($mixedLang)) {
  208. $sType = is_string($mixedLang) && !is_numeric($mixedLang) ? 'by_name' : 'by_id';
  209. $this->oDb->getLanguagesBy(array('type' => $sType, 'value' => $mixedLang), $aLanguages[0], false);
  210. }
  211. else
  212. $this->oDb->getLanguagesBy(array('type' => 'all'), $aLanguages, false);
  213. foreach($aLanguages as $aLanguage) {
  214. if(empty($aLanguage) || !is_array($aLanguage))
  215. continue;
  216. $iLanguageId = (int)$aLanguage['id'];
  217. $aKeys = array();
  218. $this->oDb->getKeysBy(array('type' => 'autogenerated', 'language_id' => $iLanguageId, 'empty' => true), $aKeys);
  219. if(empty($aKeys) || !is_array($aKeys))
  220. continue;
  221. foreach($aKeys as $aKey) {
  222. $sKey = preg_replace("/_[0-9]{8,10}$/i", '', $aKey['key']);
  223. if(empty($sKey))
  224. continue;
  225. $sString = '';
  226. $aString = array();
  227. $this->oDb->getStringsBy(array('type' => 'by_key_language_id', 'key' => $sKey, 'language_id' => $iLanguageId), $aString, false);
  228. if(!empty($aString) && is_array($aString) && isset($aString['string']))
  229. $sString = $aString['string'];
  230. $this->addLanguageString($aKey['key'], $sString, $iLanguageId, 0, false);
  231. }
  232. if($bRecompile)
  233. $this->compileLanguage($iLanguageId, true);
  234. }
  235. }
  236. function removeLanguageById($mixedLang = 0, $bRecompile = true)
  237. {
  238. $aLanguages = array();
  239. if(!empty($mixedLang)) {
  240. $sType = is_string($mixedLang) && !is_numeric($mixedLang) ? 'by_name' : 'by_id';
  241. $this->oDb->getLanguagesBy(array('type' => $sType, 'value' => $mixedLang), $aLanguages[0], false);
  242. } else
  243. $this->oDb->getLanguagesBy(array('type' => 'all'), $aLanguages, false);
  244. $bResult = true;
  245. foreach($aLanguages as $aLanguage) {
  246. if(empty($aLanguage) || !is_array($aLanguage))
  247. continue;
  248. $bResult &= (int)$this->oDb->deleteStringsBy(array('type' => 'by_lang', 'language_id' => $aLanguage['id'])) >= 0;
  249. if($bRecompile)
  250. $bResult &= $this->compileLanguage($aLanguage['id']);
  251. }
  252. return $bResult;
  253. }
  254. function removeLanguageByModule($mixedModule, $bRecompile = true)
  255. {
  256. $aModule = $mixedModule;
  257. if(!is_array($mixedModule)) {
  258. $sMethod = is_string($mixedModule) && !is_numeric($mixedModule) ? 'getModuleByUri' : 'getModuleById';
  259. $aModule = BxDolModuleQuery::getInstance()->$sMethod($mixedModule);
  260. }
  261. if(empty($aModule) || !is_array($aModule) || !$aModule['uri'] || !$aModule['lang_category'])
  262. return false;
  263. $aCategory = array();
  264. $this->oDb->getCategoriesBy(array('type' => 'by_name', 'value' => $aModule['lang_category']), $aCategory, false);
  265. if(empty($aCategory) || !is_array($aCategory))
  266. return false;
  267. $bResult = (int)$this->oDb->deleteKeysBy(array('type' => 'by_cat_id', 'value' => $aCategory['id'])) >= 0;
  268. return $bResult && ($bRecompile ? $this->compileLanguage() : true);
  269. }
  270. function getLanguageString($sKey, $iLangId = 0)
  271. {
  272. if($iLangId == 0) {
  273. $aStrings = array();
  274. $this->oDb->getStringsBy(array('type' => 'all_by_key_key_language_id', 'value' => $sKey), $aStrings, false);
  275. return $aStrings;
  276. }
  277. $aString = array();
  278. $this->oDb->getStringsBy(array('type' => 'by_key_language_id', 'key' => $sKey, 'language_id' => $iLangId), $aString, false);
  279. return $aString;
  280. }
  281. function addLanguageString($sKey, $mixedString, $iLangId = 0, $iCategoryId = 0, $bRecompile = true)
  282. {
  283. $iLangId = (int)$iLangId;
  284. $iCategoryId = (int)$iCategoryId;
  285. if($iCategoryId == 0)
  286. $iCategoryId = BX_DOL_LANGUAGE_CATEGORY_CUSTOM;
  287. $sKey = bx_process_input($sKey);
  288. $mixedString = bx_process_input($mixedString);
  289. $aLanguages = array();
  290. $iLanguages = $this->oDb->getLanguagesBy(($iLangId == 0 ? array('type' => 'all') : array('type' => 'all_by_id', 'value' => $iLangId)), $aLanguages);
  291. if(($iKeyId = $this->oDb->addKey($iCategoryId, $sKey)) === false)
  292. return false;
  293. foreach($aLanguages as $aLanguage) {
  294. $sString = '';
  295. if(is_string($mixedString))
  296. $sString = $mixedString;
  297. else if(is_array($mixedString))
  298. $sString = $mixedString[$aLanguage['id']];
  299. if(!$this->oDb->addString($iKeyId, $aLanguage['id'], $sString))
  300. return false;
  301. if($bRecompile)
  302. $this->compileLanguage($aLanguage['id']);
  303. }
  304. return $iKeyId;
  305. }
  306. function updateLanguageString($sKey, $mixedString, $iLangId = 0, $iCategoryId = 0, $bRecompile = true)
  307. {
  308. $sKey = bx_process_input($sKey);
  309. $mixedString = bx_process_input($mixedString);
  310. $iLangId = (int)$iLangId;
  311. $iCategoryId = (int)$iCategoryId;
  312. if(empty($iCategoryId))
  313. $iCategoryId = BX_DOL_LANGUAGE_CATEGORY_CUSTOM;
  314. $aKey = array();
  315. $this->oDb->getKeysBy(array('type' => 'by_name', 'value' => $sKey), $aKey);
  316. if(empty($aKey))
  317. return $this->addLanguageString($sKey, $mixedString, $iLangId, $iCategoryId, $bRecompile);
  318. if((int)$aKey['category_id'] != $iCategoryId)
  319. $this->oDb->updateKeys(array('IDCategory' => $iCategoryId), array('ID' => $aKey['id']));
  320. return $this->updateLanguageStringById($aKey['id'], $mixedString, $iLangId, $bRecompile);
  321. }
  322. function updateLanguageStringById($iKeyId, $mixedString, $iLangId = 0, $bRecompile = true)
  323. {
  324. $iKeyId = (int)$iKeyId;
  325. $mixedString = bx_process_input($mixedString);
  326. $iLangId = (int)$iLangId;
  327. $aLanguages = array();
  328. $iLanguages = $this->oDb->getLanguagesBy(($iLangId == 0 ? array('type' => 'all') : array('type' => 'all_by_id', 'value' => $iLangId)), $aLanguages);
  329. $bResult = false;
  330. foreach($aLanguages as $aLanguage) {
  331. $sString = '';
  332. if(is_string($mixedString))
  333. $sString = $mixedString;
  334. else if(is_array($mixedString))
  335. $sString = $mixedString[$aLanguage['id']];
  336. if(!$this->oDb->updateString($iKeyId, $aLanguage['id'], $sString))
  337. continue;
  338. if($bRecompile)
  339. $this->compileLanguage($aLanguage['id']);
  340. $bResult = true;
  341. }
  342. return $bResult;
  343. }
  344. function deleteLanguageString($sKey, $iLangId = 0, $bRecompile = true)
  345. {
  346. $iLangId = (int)$iLangId;
  347. $sKey = bx_process_input($sKey);
  348. $aKey = array();
  349. $this->oDb->getKeysBy(array('type' => 'by_name', 'value' => $sKey), $aKey);
  350. if(empty($aKey))
  351. return false;
  352. return $this->deleteLanguageStringById($aKey['id'], $iLangId, $bRecompile);
  353. }
  354. function deleteLanguageStringById($iKeyId, $iLangId = 0, $bRecompile = true)
  355. {
  356. $iKeyId = (int)$iKeyId;
  357. $iLangId = (int)$iLangId;
  358. $aLanguages = array();
  359. $iLanguages = $this->oDb->getLanguagesBy(($iLangId == 0 ? array('type' => 'all') : array('type' => 'all_by_id', 'value' => $iLangId)), $aLanguages);
  360. foreach($aLanguages as $aLanguage) {
  361. if(!$this->oDb->deleteString($iKeyId, $aLanguage['id']))
  362. continue;
  363. if($bRecompile)
  364. $this->compileLanguage($aLanguage['id']);
  365. }
  366. return $this->oDb->deleteKey($iKeyId);
  367. }
  368. function addLanguageCategory($sName)
  369. {
  370. return $this->oDb->addCategory($sName);
  371. }
  372. function deleteLanguageCategory($sName)
  373. {
  374. return $this->oDb->deleteCategory($sName);
  375. }
  376. protected function _restoreLanguageByModule($aLanguage, $sPath, $mixedModule)
  377. {
  378. $oXmlParser = BxDolXmlParser::getInstance();
  379. $aModule = $mixedModule;
  380. if(!is_array($mixedModule)) {
  381. $sMethod = is_string($mixedModule) && !is_numeric($mixedModule) ? 'getModuleByUri' : 'getModuleById';
  382. $aModule = BxDolModuleQuery::getInstance()->$sMethod($mixedModule);
  383. }
  384. if(empty($aModule) || !is_array($aModule) || !$aModule['name'] || !$aModule['lang_category'])
  385. return true;
  386. $sPath = $sPath . $aModule['name'] . '/' . $aLanguage['name'] . '.xml';
  387. if(!file_exists($sPath) && isset($aModule['path'])) {
  388. $sPath = BX_DIRECTORY_PATH_MODULES . $aModule['path'] . 'install/langs/' . $aLanguage['name'] . '.xml';
  389. if(!file_exists($sPath))
  390. return true;
  391. }
  392. $aLanguageInfo = $this->readLanguage($sPath);
  393. if(empty($aLanguageInfo['name']) || empty($aLanguageInfo['strings']) || $aLanguageInfo['name'] != $aLanguage['name'])
  394. return false;
  395. $aCategory = array();
  396. $this->oDb->getCategoriesBy(array('type' => 'by_name', 'value' => $aModule['lang_category']), $aCategory, false);
  397. $iCategoryId = 0;
  398. if(empty($aCategory) || !is_array($aCategory)) {
  399. $iCategoryId = $this->addLanguageCategory($aModule['lang_category']);
  400. if(empty($iCategoryId))
  401. return false;
  402. }
  403. else
  404. $iCategoryId = $aCategory['id'];
  405. $bDeleteStringsByKey = !$this->oDb->deleteStringsBy(array('type' => 'by_cat_and_lang', 'category_id' => $iCategoryId, 'language_id' => $aLanguage['id']));
  406. $bResult = true;
  407. foreach($aLanguageInfo['strings'] as $sKey => $sValue)
  408. if($sKey != '') {
  409. if($bDeleteStringsByKey)
  410. $this->oDb->deleteStringsBy(array('type' => 'by_key_and_lang', 'key' => $sKey, 'language_id' => $aLanguage['id']));
  411. $bResult &= $this->addLanguageString($sKey, $sValue, $aLanguage['id'], $iCategoryId, false) > 0;
  412. }
  413. return $bResult;
  414. }
  415. }
  416. /** @} */