BxDolStudioInstaller.php 31 KB

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