BxDolStudioLanguagesUtils.php 16 KB

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