BxDolStudioInstaller.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  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('BxDolInstallerUtils');
  10. bx_import('BxDolStudioInstallerQuery');
  11. define("BX_DOL_STUDIO_INSTALLER_SUCCESS", 0);
  12. define("BX_DOL_STUDIO_INSTALLER_FAILED", 1);
  13. /**
  14. * Base class for Installer classes in modules engine.
  15. *
  16. * The class contains different check functions which are used during the installation process.
  17. * An object of the class is created automatically with Dolphin's modules installer.
  18. * Installation/Uninstallation process can be controlled with config.php file located in [module]/install/ folder.
  19. *
  20. *
  21. * Example of usage:
  22. * @see any module included in the default Dolphin's package.
  23. *
  24. *
  25. * Memberships/ACL:
  26. * Doesn't depend on user's membership.
  27. *
  28. *
  29. * Alerts:
  30. * no alerts available
  31. *
  32. */
  33. class BxDolStudioInstaller extends BxDolInstallerUtils {
  34. protected $oDb;
  35. protected $_aConfig;
  36. protected $_sBasePath;
  37. protected $_sHomePath;
  38. protected $_sModulePath;
  39. protected $_aActions;
  40. protected $_aNonHashable;
  41. protected $_bShowOnSuccess = false;
  42. function __construct($aConfig) {
  43. parent::__construct();
  44. $this->oDb = new BxDolStudioInstallerQuery();
  45. $this->_aConfig = $aConfig;
  46. $this->_sBasePath = BX_DIRECTORY_PATH_MODULES;
  47. $this->_sHomePath = $this->_sBasePath . $aConfig['home_dir'];
  48. $this->_sModulePath = $this->_sBasePath . $aConfig['home_dir'];
  49. $this->_aActions = array(
  50. 'perform_install' => array(
  51. 'title' => '',
  52. 'success' => _t('_adm_msg_modules_success_install'),
  53. 'failed' => ''
  54. ),
  55. 'perform_uninstall' => array(
  56. 'title' => '',
  57. 'success' => _t('_adm_msg_modules_success_uninstall'),
  58. 'failed' => ''
  59. ),
  60. 'perform_enable' => array(
  61. 'title' => '',
  62. 'success' => _t('_adm_msg_modules_success_enable'),
  63. 'failed' => ''
  64. ),
  65. 'perform_disable' => array(
  66. 'title' => '',
  67. 'success' => _t('_adm_msg_modules_success_disable'),
  68. 'failed' => ''
  69. ),
  70. 'check_script_version' => array(
  71. 'title' => _t('_adm_txt_modules_check_script_version'),
  72. ),
  73. 'check_dependencies' => array(
  74. 'title' => _t('_adm_txt_modules_check_dependencies'),
  75. ),
  76. 'show_introduction' => array(
  77. 'title' => _t('_adm_txt_modules_show_introduction'),
  78. ),
  79. 'check_permissions' => array(
  80. 'title' => _t('_adm_txt_modules_check_permissions'),
  81. ),
  82. 'change_permissions' => array(
  83. 'title' => _t('_adm_txt_modules_change_permissions'),
  84. ),
  85. 'move_sources' => array(
  86. 'title' => _t('_adm_txt_modules_move_sources'),
  87. ),
  88. 'execute_sql' => array(
  89. 'title' => _t('_adm_txt_modules_execute_sql'),
  90. ),
  91. 'install_language' => array(
  92. 'title' => _t('_adm_txt_modules_install_language'),
  93. ),
  94. 'update_languages' => array(
  95. 'title' => _t('_adm_txt_modules_update_languages'),
  96. ),
  97. 'recompile_global_paramaters' => array(
  98. 'title' => _t('_adm_txt_modules_recompile_global_paramaters'),
  99. ),
  100. 'recompile_menus' => array(
  101. 'title' => _t('_adm_txt_modules_recompile_menus'),
  102. ),
  103. 'recompile_site_stats' => array(
  104. 'title' => _t('_adm_txt_modules_recompile_site_stats'),
  105. ),
  106. 'recompile_comments' => array(
  107. 'title' => _t('_adm_txt_modules_recompile_comments'),
  108. ),
  109. 'recompile_member_actions' => array(
  110. 'title' => _t('_adm_txt_modules_recompile_member_actions'),
  111. ),
  112. 'recompile_votes' => array(
  113. 'title' => _t('_adm_txt_modules_recompile_votes'),
  114. ),
  115. 'recompile_search' => array(
  116. 'title' => _t('_adm_txt_modules_recompile_search'),
  117. ),
  118. 'recompile_injections' => array(
  119. 'title' => _t('_adm_txt_modules_recompile_injections'),
  120. ),
  121. 'recompile_permalinks' => array(
  122. 'title' => _t('_adm_txt_modules_recompile_permalinks'),
  123. ),
  124. 'recompile_alerts' => array(
  125. 'title' => _t('_adm_txt_modules_recompile_alerts'),
  126. ),
  127. 'clear_db_cache' => array(
  128. 'title' => _t('_adm_txt_modules_clear_db_cache'),
  129. ),
  130. 'show_conclusion' => array(
  131. 'title' => _t('_adm_txt_modules_show_conclusion'),
  132. ),
  133. );
  134. $this->_aNonHashable = array(
  135. 'install',
  136. 'updates'
  137. );
  138. }
  139. public function install($aParams, $bEnable = false) {
  140. //--- Check whether the module was already installed ---//
  141. if($this->oDb->isModule($this->_aConfig['home_uri']))
  142. return array(
  143. 'message' => _t('_adm_err_modules_already_installed'),
  144. 'result' => false
  145. );
  146. $sAction = $bEnable ? 'enable' : 'install';
  147. $aResult = array();
  148. bx_alert('system', 'before_' . $sAction, 0, false, array ('config' => $this->_aConfig, 'result' => &$aResult));
  149. if ($aResult && !$aResult['result'])
  150. return $aResult;
  151. //--- Check mandatory settings ---//
  152. if($this->oDb->isModuleParamsUsed($this->_aConfig['home_uri'], $this->_aConfig['home_dir'], $this->_aConfig['db_prefix'], $this->_aConfig['class_prefix']))
  153. return array(
  154. 'message' => _t('_adm_txt_modules_params_used'),
  155. 'result' => false
  156. );
  157. //--- Check version compatibility ---//
  158. $bCompatible = false;
  159. if(isset($this->_aConfig['compatible_with']) && is_array($this->_aConfig['compatible_with']))
  160. foreach($this->_aConfig['compatible_with'] as $iKey => $sVersion) {
  161. $sVersion = '/^' . str_replace(array('.', 'x'), array('\.', '[0-9]+'), $sVersion) . '$/is';
  162. $bCompatible = $bCompatible || (preg_match($sVersion, getParam('sys_version')) > 0);
  163. }
  164. if(!$bCompatible)
  165. return array(
  166. 'message' => $this->_displayResult('check_script_version', false, '_adm_err_modules_wrong_version_script'),
  167. 'result' => false
  168. );
  169. //--- Check for available translations ---//
  170. $oFile = $this->_getFileManager();
  171. $sModuleUri = $this->_aConfig['home_uri'];
  172. $aLanguages = $this->oDb->getModulesBy(array('type' => 'languages'));
  173. foreach($aLanguages as $aLanguage) {
  174. if($aLanguage['uri'] == 'en')
  175. continue;
  176. $sLanguageConfig = BX_DIRECTORY_PATH_MODULES . $aLanguage['path'] . '/install/config.php';
  177. if(!file_exists($sLanguageConfig))
  178. continue;
  179. include_once($sLanguageConfig);
  180. $aLanguageConfig = &$aConfig;
  181. if(!isset($aLanguageConfig['includes'][$sModuleUri]) || empty($aLanguageConfig['includes'][$sModuleUri]))
  182. continue;
  183. $sSrcPath = 'modules/' . $aLanguage['path'] . 'install/data/' . $aLanguageConfig['includes'][$sModuleUri];
  184. $sDstPath = $aLanguageConfig['includes'][$sModuleUri];
  185. $oFile->copy($sSrcPath, $sDstPath);
  186. }
  187. //--- Check actions ---//
  188. $aResult = $this->_perform('install');
  189. if($aResult['result']) {
  190. $sDependencies = "";
  191. if(isset($this->_aConfig['install']['check_dependencies']) && (int)$this->_aConfig['install']['check_dependencies'] == 1 && isset($this->_aConfig['dependencies']) && is_array($this->_aConfig['dependencies']))
  192. $this->_aConfig['dependencies'] = array_keys($this->_aConfig['dependencies']);
  193. $iModuleId = $this->oDb->insertModule($this->_aConfig);
  194. bx_import('BxDolModule');
  195. $sTitleKey = BxDolModule::getTitleKey($this->_aConfig['home_uri']);
  196. bx_import('BxDolStudioLanguagesUtils');
  197. $oLanguages = BxDolStudioLanguagesUtils::getInstance();
  198. $oLanguages->addLanguageString($sTitleKey, $this->_aConfig['title']);
  199. $oLanguages->compileLanguage();
  200. $aFiles = array();
  201. $this->_hash($this->_sModulePath, $aFiles);
  202. foreach($aFiles as $aFile)
  203. $this->oDb->insertModuleTrack($iModuleId, $aFile);
  204. $this->oDb->cleanMemory('sys_modules_' . $this->_aConfig['home_uri']);
  205. $this->oDb->cleanMemory('sys_modules_' . $iModuleId);
  206. $this->oDb->cleanMemory('sys_modules');
  207. }
  208. if($bEnable) {
  209. $aResultEnable = $this->enable($aParams);
  210. $aResult['result'] = $aResult['result'] & $aResultEnable['result'];
  211. $aResult['message'] = $aResult['message'] . $aResultEnable['message'];
  212. }
  213. bx_alert('system', $sAction, 0, false, array ('config' => $this->_aConfig, 'result' => &$aResult));
  214. return $aResult;
  215. }
  216. public function uninstall($aParams, $bDisable = false) {
  217. //--- Check whether the module was already uninstalled ---//
  218. if(!$this->oDb->isModule($this->_aConfig['home_uri']))
  219. return array(
  220. 'message' => _t('_adm_err_modules_already_uninstalled'),
  221. 'result' => false
  222. );
  223. $sAction = $bDisable ? 'disable' : 'uninstall';
  224. $aResult = array();
  225. bx_alert('system', 'before_' . $sAction, 0, false, array ('config' => $this->_aConfig, 'result' => &$aResult));
  226. if ($aResult && !$aResult['result'])
  227. return $aResult;
  228. //--- Check for dependent modules ---//
  229. $bDependent = false;
  230. $aDependents = $this->oDb->getDependent($this->_aConfig['home_uri']);
  231. if(is_array($aDependents) && !empty($aDependents)) {
  232. $bDependent = true;
  233. $sMessage = '<br />' . _t('_adm_err_modules_wrong_dependency_uninstall') . '<br />';
  234. foreach($aDependents as $aDependent)
  235. $sMessage .= $aDependent['title'] . '<br />';
  236. }
  237. if($bDependent)
  238. return array(
  239. 'message' => $this->_displayResult('check_dependencies', false, $sMessage),
  240. 'result' => false
  241. );
  242. $aResult = $this->_perform('uninstall');
  243. if($aResult['result']) {
  244. $iModuleId = $this->oDb->deleteModule($this->_aConfig);
  245. bx_import('BxDolModule');
  246. $sTitleKey = BxDolModule::getTitleKey($this->_aConfig['home_uri']);
  247. bx_import('BxDolStudioLanguagesUtils');
  248. $oLanguages = BxDolStudioLanguagesUtils::getInstance();
  249. $oLanguages->deleteLanguageString($sTitleKey);
  250. $oLanguages->compileLanguage();
  251. $this->oDb->cleanMemory ('sys_modules_' . $this->_aConfig['home_uri']);
  252. $this->oDb->cleanMemory ('sys_modules_' . $iModuleId);
  253. $this->oDb->cleanMemory ('sys_modules');
  254. }
  255. if($bDisable) {
  256. $aResultEnable = $this->disable($aParams);
  257. $aResult['result'] = $aResult['result'] & $aResultEnable['result'];
  258. $aResult['message'] = $aResult['message'] . $aResultEnable['message'];
  259. }
  260. bx_alert('system', $sAction, 0, false, array ('config' => $this->_aConfig, 'result' => &$aResult));
  261. return $aResult;
  262. }
  263. public function delete() {
  264. bx_import('BxDolFtp');
  265. $oFtp = new BxDolFtp($_SERVER['HTTP_HOST'], getParam('sys_ftp_login'), getParam('sys_ftp_password'), getParam('sys_ftp_dir'));
  266. if(!$oFtp->connect())
  267. return array(
  268. 'message' => _t('_adm_err_modules_cannot_connect_to_ftp'),
  269. 'result' => false
  270. );
  271. if(!$oFtp->delete('modules/' . $this->_aConfig['home_dir']))
  272. return array(
  273. 'message' => _t('_adm_err_modules_cannot_remove_package'),
  274. 'result' => false
  275. );
  276. return array(
  277. 'message' => '', //_t('_adm_msg_modules_success_delete'),
  278. 'result' => true
  279. );
  280. }
  281. public function recompile($aParams) {
  282. bx_import('BxDolStudioLanguagesUtils');
  283. $oLanguages = BxDolStudioLanguagesUtils::getInstance();
  284. $aResult = array('message' => '', 'result' => false);
  285. $aLanguages = $this->oDb->getLanguages();
  286. if (isAdmin() && !empty($aLanguages)) {
  287. $this->_updateLanguage(false, current($aLanguages));
  288. $bResult = false;
  289. foreach($aLanguages as $aLanguage) {
  290. $bResult = $this->_updateLanguage(true, $aLanguage) && $oLanguages->compileLanguage($aLanguage['id']);
  291. $aResult['message'] .= $aLanguage['title'] . ': <span style="color:' . ($bResult ? 'green' : 'red') . '">' . _t($bResult ? '_adm_txt_modules_process_action_success' : '_adm_txt_modules_process_action_failed') . '</span><br />';
  292. $aResult['result'] |= $bResult;
  293. }
  294. }
  295. return $aResult;
  296. }
  297. public function enable($aParams) {
  298. $aModule = $this->oDb->getModuleByUri($this->_aConfig['home_uri']);
  299. //--- Check whether the module is installed ---//
  300. if(empty($aModule) || !is_array($aModule))
  301. return array(
  302. 'message' => _t('_adm_err_modules_module_not_installed'),
  303. 'result' => false
  304. );
  305. //--- Check whether the module is already enabled ---//
  306. if((int)$aModule['enabled'] != 0)
  307. return array(
  308. 'message' => _t('_adm_err_modules_already_enabled'),
  309. 'result' => false
  310. );
  311. $aResult = $this->_perform('enable');
  312. if($aResult['result']) {
  313. $this->oDb->enableModuleByUri($aModule['uri']);
  314. $this->oDb->cleanMemory('sys_modules_' . $aModule['uri']);
  315. $this->oDb->cleanMemory('sys_modules_' . $aModule['id']);
  316. $this->oDb->cleanMemory('sys_modules');
  317. }
  318. return $aResult;
  319. }
  320. public function disable($aParams) {
  321. $aModule = $this->oDb->getModuleByUri($this->_aConfig['home_uri']);
  322. //--- Check whether the module is installed ---//
  323. if(empty($aModule) || !is_array($aModule))
  324. return array(
  325. 'message' => _t('_adm_err_modules_module_not_installed'),
  326. 'result' => false
  327. );
  328. //--- Check whether the module is already disabled ---//
  329. if((int)$aModule['enabled'] == 0)
  330. return array(
  331. 'message' => _t('_adm_err_modules_already_disabled'),
  332. 'result' => false
  333. );
  334. //--- Check for dependent modules ---//
  335. $bDependent = false;
  336. $aDependents = $this->oDb->getDependent($this->_aConfig['home_uri']);
  337. if(is_array($aDependents) && !empty($aDependents)) {
  338. $bDependent = true;
  339. $sMessage = '<br />' . _t('_adm_err_modules_wrong_dependency_disable') . '<br />';
  340. foreach($aDependents as $aDependent)
  341. $sMessage .= $aDependent['title'] . '<br />';
  342. }
  343. if($bDependent)
  344. return array(
  345. 'message' => $this->_displayResult('check_dependencies', false, $sMessage),
  346. 'result' => false
  347. );
  348. $aResult = $this->_perform('disable');
  349. if($aResult['result']) {
  350. $this->oDb->disableModuleByUri($aModule['uri']);
  351. $this->oDb->cleanMemory('sys_modules_' . $aModule['uri']);
  352. $this->oDb->cleanMemory('sys_modules_' . $aModule['id']);
  353. $this->oDb->cleanMemory('sys_modules');
  354. }
  355. return $aResult;
  356. }
  357. function _hash($sPath, &$aFiles) {
  358. if(file_exists($sPath) && is_dir($sPath) && ($rSource = opendir($sPath))) {
  359. while(($sFile = readdir($rSource)) !== false) {
  360. if($sFile == '.' || $sFile =='..' || $sFile[0] == '.' || in_array($sFile, $this->_aNonHashable))
  361. continue;
  362. if(is_dir($sPath . $sFile))
  363. $this->_hash($sPath . $sFile . '/', $aFiles);
  364. else
  365. $aFiles[] = $this->_info($sPath . $sFile);
  366. }
  367. closedir($rSource);
  368. }
  369. else
  370. $aFiles[] = $this->_info($sPath, $sFile);
  371. }
  372. function _info($sPath) {
  373. return array(
  374. 'file' => str_replace($this->_sModulePath, '', $sPath),
  375. 'hash' => md5(file_get_contents($sPath))
  376. );
  377. }
  378. function _perform($sOperationName) {
  379. if(!defined('BX_SKIP_INSTALL_CHECK') && !defined('BX_DOL_CRON_EXECUTE') && !$GLOBALS['logged']['admin'])
  380. return array('message' => '_adm_mod_err_only_admin_can_perform_operations_with_modules', 'result' => false);
  381. $sMessage = '';
  382. foreach($this->_aConfig[$sOperationName] as $sAction => $iEnabled) {
  383. $sMethod = 'action' . bx_gen_method_name($sAction);
  384. if($iEnabled == 0 || !method_exists($this, $sMethod))
  385. continue;
  386. $mixedResult = $this->$sMethod($sOperationName);
  387. //--- On Success ---//
  388. if((is_int($mixedResult) && (int)$mixedResult == BX_DOL_STUDIO_INSTALLER_SUCCESS) || (isset($mixedResult['code']) && (int)$mixedResult['code'] == BX_DOL_STUDIO_INSTALLER_SUCCESS)) {
  389. $sMessage .= $this->_displayResult($sAction, true, isset($mixedResult['content']) ? $mixedResult['content'] : '');
  390. continue;
  391. }
  392. //--- On Failed ---//
  393. $sMethodFailed = $sMethod . 'Failed';
  394. return array('message' => $this->_displayResult($sAction, false, method_exists($this, $sMethodFailed) ? $this->$sMethodFailed($mixedResult) : $this->actionOperationFailed($mixedResult)), 'result' => false);
  395. }
  396. if($this->_bShowOnSuccess)
  397. $sMessage = $this->_aActions['perform_' . $sOperationName]['success'] . $sMessage;
  398. return array('message' => $sMessage, 'result' => true);
  399. }
  400. function _displayResult($sAction, $bResult, $sResult = '') {
  401. $sMessage = $this->_aActions[$sAction]['title'] . ' ';
  402. if(!empty($sResult) && substr($sResult, 0, 1) == '_')
  403. $sResult = _t($sResult) . '<br />';
  404. if(!$bResult)
  405. return $sMessage . '<span style="color:red;">' . $sResult . '</span>';
  406. if($bResult && !in_array($sAction, array('show_introduction', 'show_conclusion')) && !$this->_bShowOnSuccess)
  407. return;
  408. return $sMessage . '<span style="color:green;">' . (!empty($sResult) ? $sResult : _t('_adm_txt_modules_process_action_success') . '<br />') . '</span>';
  409. }
  410. //--- Action Methods ---//
  411. function actionOperationFailed($mixedResult) {
  412. return _t('_adm_err_modules_process_action_failed');
  413. }
  414. function actionCheckDependencies($sOperation) {
  415. $sContent = '';
  416. if(in_array($sOperation, array('install', 'enable', 'update'))) {
  417. if(!isset($this->_aConfig['dependencies']) || !is_array($this->_aConfig['dependencies']))
  418. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  419. foreach($this->_aConfig['dependencies'] as $sModuleUri => $sModuleTitle)
  420. if(!$this->oDb->isModule($sModuleUri))
  421. $sContent .= $sModuleTitle . '<br />';
  422. if(!empty($sContent))
  423. $sContent = '<br />' . _t('_adm_err_modules_wrong_dependency_install') . '<br />' . $sContent;
  424. }
  425. return empty($sContent) ? BX_DOL_STUDIO_INSTALLER_SUCCESS : array('code' => BX_DOL_STUDIO_INSTALLER_FAILED, 'content' => $sContent);
  426. }
  427. function actionCheckDependenciesFailed($mixedResult) {
  428. return $mixedResult['content'];
  429. }
  430. function actionShowIntroduction($sOperation) {
  431. if(!isset($this->_aConfig[$sOperation . '_info']['introduction']))
  432. return BX_DOL_STUDIO_INSTALLER_FAILED;
  433. $sPath = $this->_sHomePath . 'install/info/' . $this->_aConfig[$sOperation . '_info']['introduction'];
  434. return file_exists($sPath) ? array("code" => BX_DOL_STUDIO_INSTALLER_SUCCESS, "content" => "<pre>" . file_get_contents($sPath) . "</pre>") : BX_DOL_STUDIO_INSTALLER_FAILED;
  435. }
  436. function actionShowConclusion($sOperation) {
  437. if(!isset($this->_aConfig[$sOperation . '_info']['conclusion']))
  438. return BX_DOL_STUDIO_INSTALLER_FAILED;
  439. $sPath = $this->_sHomePath . 'install/info/' . $this->_aConfig[$sOperation . '_info']['conclusion'];
  440. return file_exists($sPath) ? array("code" => BX_DOL_STUDIO_INSTALLER_SUCCESS, "content" => "<pre>" . file_get_contents($sPath) . "</pre>") : BX_DOL_STUDIO_INSTALLER_FAILED;
  441. }
  442. function actionCheckPermissions($sOperation) {
  443. if(!isset($this->_aConfig[$sOperation . '_permissions']) || !is_array($this->_aConfig[$sOperation . '_permissions']))
  444. return BX_DOL_STUDIO_INSTALLER_FAILED;
  445. $aResult = array();
  446. $aPermissions = $this->_aConfig[$sOperation . '_permissions'];
  447. foreach($aPermissions as $sPermissions => $aFiles) {
  448. $sCheckFunction = 'is' . ucfirst($sPermissions);
  449. $sCptPermissions = _t('_adm_txt_modules_' . $sPermissions);
  450. foreach($aFiles as $sFile)
  451. if(!BxDolInstallerUtils::$sCheckFunction(str_replace(BX_DIRECTORY_PATH_ROOT, '', $this->_sModulePath . $sFile)))
  452. $aResult[] = array('path' => $this->_sModulePath . $sFile, 'permissions' => $sCptPermissions);
  453. }
  454. return empty($aResult) ? BX_DOL_STUDIO_INSTALLER_SUCCESS : array('code' => BX_DOL_STUDIO_INSTALLER_FAILED, 'content' => $aResult);
  455. }
  456. function actionCheckPermissionsFailed($mixedResult) {
  457. $sResult = '<br />' . _t('_adm_err_modules_wrong_permissions_check') . '<br />';
  458. foreach($mixedResult['content'] as $aFile)
  459. $sResult .= _t('_adm_err_modules_wrong_permissions_msg', $aFile['path'], $aFile['permissions']) . '<br />';
  460. return $sResult;
  461. }
  462. function actionChangePermissions($sOperation) {
  463. if(!isset($this->_aConfig[$sOperation . '_permissions']) || !is_array($this->_aConfig[$sOperation . '_permissions']))
  464. return BX_DOL_STUDIO_INSTALLER_FAILED;
  465. $aChangeItems = array();
  466. $aPermissions = $this->_aConfig[$sOperation . '_permissions'];
  467. foreach($aPermissions as $sPermissions => $aFiles) {
  468. $sCheckFunction = 'is' . ucfirst($sPermissions);
  469. foreach($aFiles as $sFile) {
  470. $sPath = bx_ltrim_str($this->_sModulePath . $sFile, BX_DIRECTORY_PATH_ROOT);
  471. if(BxDolInstallerUtils::$sCheckFunction($sPath))
  472. continue;
  473. $aChangeItems[] = array('file' => $sFile, 'path' => $sPath, 'permissions' => $sPermissions);
  474. }
  475. }
  476. if(empty($aChangeItems))
  477. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  478. $oFile = $this->_getFileManager();
  479. if(empty($oFile))
  480. return array('code' => BX_DOL_STUDIO_INSTALLER_FAILED);
  481. $aResult = array();
  482. foreach($aChangeItems as $aChangeItem)
  483. if(!$oFile->setPermissions($aChangeItem['path'], $aChangeItem['permissions']))
  484. $aResult[] = array('path' => $this->_sModulePath . $aChangeItem['file'], 'permissions' => $aChangeItem['permissions']);
  485. return empty($aResult) ? BX_DOL_STUDIO_INSTALLER_SUCCESS : array('code' => BX_DOL_STUDIO_INSTALLER_FAILED, 'content' => $aResult);
  486. }
  487. function actionChangePermissionsFailed($mixedResult) {
  488. if(empty($mixedResult['content']))
  489. return $this->actionOperationFailed($mixedResult);
  490. $sResult = '<br />' . _t('_adm_err_modules_wrong_permissions_change') . '<br />';
  491. foreach($mixedResult['content'] as $aFile)
  492. $sResult .= _t('_adm_err_modules_wrong_permissions_msg', $aFile['path'], $aFile['permissions']) . '<br />';
  493. return $sResult;
  494. }
  495. function actionMoveSources($sOperation) {
  496. $oFile = $this->_getFileManager();
  497. $aInstalled = array_merge(array('system'), $this->oDb->getModulesUri());
  498. $bResult = true;
  499. foreach($this->_aConfig['includes'] as $sUri => $sPath) {
  500. if(!in_array($sUri, $aInstalled) || empty($sPath))
  501. continue;
  502. if($sOperation == 'install') {
  503. $sSrcPath = 'modules/' . $this->_aConfig['home_dir'] . 'install/data/' . $sPath;
  504. $sDstPath = $sPath;
  505. $bResult &= $oFile->copy($sSrcPath, $sDstPath);
  506. }
  507. else if($sOperation == 'uninstall')
  508. $bResult &= $oFile->delete($sPath);
  509. }
  510. return $bResult ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  511. }
  512. function actionExecuteSql($sOperation) {
  513. switch($sOperation) {
  514. case 'install':
  515. $this->actionExecuteSql('disable');
  516. $this->actionExecuteSql('uninstall');
  517. break;
  518. case 'enable':
  519. $this->actionExecuteSql('disable');
  520. break;
  521. }
  522. bx_import('BxDolDb');
  523. $mixedResult = BxDolDb::getInstance()->executeSQL($this->_sHomePath . 'install/sql/' . $sOperation . '.sql');
  524. return $mixedResult === true ? BX_DOL_STUDIO_INSTALLER_SUCCESS : array('code' => BX_DOL_STUDIO_INSTALLER_FAILED, 'content' => $mixedResult);
  525. }
  526. function actionExecuteSqlFailed($mixedResult) {
  527. $sResult = '<br />' . _t('_adm_err_modules_wrong_mysql_query') . '<br />';
  528. foreach($mixedResult['content'] as $aQuery) {
  529. $sResult .= _t('_adm_err_modules_wrong_mysql_query_msg', $aQuery['error']) . '<br />';
  530. $sResult .= '<pre>' . $aQuery['query'] . '</pre>';
  531. }
  532. return $sResult;
  533. }
  534. function actionInstallLanguage($sOperation) {
  535. bx_import('BxDolStudioLanguagesUtils');
  536. $oLanguages = BxDolStudioLanguagesUtils::getInstance();
  537. $sLanguage = isset($this->_aConfig['home_uri']) ? $this->_aConfig['home_uri'] : '';
  538. $bResult = true;
  539. if($sOperation == 'install')
  540. $bResult = $oLanguages->installLanguage(array('path' => $this->_aConfig['home_dir'], 'uri' => $this->_aConfig['home_uri'], 'lang_category' => $this->_aConfig['language_category']),false);
  541. return $bResult && $oLanguages->compileLanguage(0, true) ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  542. }
  543. function actionUpdateLanguages($sOperation) {
  544. if(!isset($this->_aConfig['language_category']) || empty($this->_aConfig['language_category']))
  545. return BX_DOL_STUDIO_INSTALLER_FAILED;
  546. bx_import('BxDolStudioLanguagesUtils');
  547. $oLanguages = BxDolStudioLanguagesUtils::getInstance();
  548. $bResult = true;
  549. $aResult = array();
  550. //--- Process Language Category ---//
  551. if($sOperation == 'install')
  552. $iCategoryId = $oLanguages->addLanguageCategory($this->_aConfig['language_category']);
  553. //--- Process languages' key=>value pears ---//
  554. $aModule = array(
  555. 'path' => $this->_aConfig['home_dir'],
  556. 'uri' => $this->_aConfig['home_uri'],
  557. 'lang_category' => $this->_aConfig['language_category']
  558. );
  559. if($sOperation == 'install')
  560. $bResult = $oLanguages->restoreLanguage(0, $aModule, false);
  561. else if($sOperation == 'uninstall')
  562. $bResult = $oLanguages->removeLanguageByModule($aModule, false);
  563. if($sOperation == 'uninstall' && $bResult)
  564. $oLanguages->deleteLanguageCategory($this->_aConfig['language_category']);
  565. return $bResult && $oLanguages->compileLanguage(0, true) ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  566. }
  567. function actionRecompileGlobalParamaters($sOperation) {
  568. $bResult = $this->oDb->cacheParamsClear();
  569. return $bResult ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  570. }
  571. function actionRecompileMenus($sOperation) {
  572. $bResult = $this->oDb->cleanCache('sys_objects_menu');
  573. return $bResult ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  574. }
  575. function actionRecompileSiteStats($sOperation) {
  576. $bResult = $this->oDb->cleanCache('sys_stat_site');
  577. return $bResult ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  578. }
  579. function actionRecompileComments($sOperation) {
  580. $bResult = $this->oDb->cleanCache('sys_objects_cmts');
  581. return $bResult ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  582. }
  583. function actionRecompileMemberActions($sOperation) {
  584. $bResult = $this->oDb->cleanCache('sys_objects_actions');
  585. return $bResult ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  586. }
  587. function actionRecompileVotes($sOperation) {
  588. $bResult = $this->oDb->cleanCache('sys_objects_vote');
  589. return $bResult ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  590. }
  591. function actionRecompileInjections($sOperation) {
  592. $bResult = $this->oDb->cleanCache('sys_injections.inc');
  593. $bResult = $bResult && $this->oDb->cleanCache('sys_injections_admin.inc');
  594. return $bResult ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  595. }
  596. function actionRecompilePermalinks($sOperation) {
  597. $bResult = true;
  598. bx_import('BxDolPermalinks');
  599. $bResult = $bResult && BxDolPermalinks::getInstance()->cacheInvalidate();
  600. $bResult = $bResult && ($this->actionRecompileMenus($sOperation) == BX_DOL_STUDIO_INSTALLER_SUCCESS);
  601. return $bResult ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  602. }
  603. function actionRecompileAlerts($sOperation) {
  604. $bResult = $this->oDb->cleanCache('sys_alerts');
  605. return $bResult ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  606. }
  607. function actionClearDbCache($sOperation) {
  608. $oCache = $this->oDb->getDbCacheObject();
  609. $bResult = $oCache->removeAllByPrefix('db_');
  610. return $bResult ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  611. }
  612. //--- Get/Set Methods ---//
  613. function getVendor() {
  614. return $this->_aConfig['vendor'];
  615. }
  616. function getName() {
  617. return $this->_aConfig['name'];
  618. }
  619. function getTitle() {
  620. return $this->_aConfig['title'];
  621. }
  622. function getHomeDir() {
  623. return $this->_aConfig['home_dir'];
  624. }
  625. //--- Protected methods ---//
  626. protected function _getFileManager($bUseFtp = true) {
  627. $oFile = null;
  628. if($bUseFtp) {
  629. bx_import('BxDolFtp');
  630. $oFile = new BxDolFtp(isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'localhost', getParam('sys_ftp_login'), getParam('sys_ftp_password'), getParam('sys_ftp_dir'));
  631. if(!$oFile->connect())
  632. return null;
  633. if(!$oFile->isDolphin())
  634. return null;
  635. }
  636. else {
  637. bx_import('BxDolFile');
  638. $oFile = BxDolFile::getInstance();
  639. }
  640. return $oFile;
  641. }
  642. }
  643. /** @} */