BxDolStudioLanguagesUtils.php 15 KB

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