BxDolStudioInstaller.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  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. {
  35. protected $oDb;
  36. protected $_aConfig;
  37. protected $_sBasePath;
  38. protected $_sHomePath;
  39. protected $_sModulePath;
  40. protected $_aActions;
  41. protected $_bShowOnSuccess = false;
  42. function __construct($aConfig)
  43. {
  44. parent::__construct();
  45. $this->oDb = new BxDolStudioInstallerQuery();
  46. $this->_aConfig = $aConfig;
  47. $this->_sBasePath = BX_DIRECTORY_PATH_MODULES;
  48. $this->_sHomePath = $this->_sBasePath . $aConfig['home_dir'];
  49. $this->_sModulePath = $this->_sBasePath . $aConfig['home_dir'];
  50. $this->_aActions = array(
  51. 'perform_install' => array(
  52. 'title' => '',
  53. 'success' => _t('_adm_msg_modules_success_install'),
  54. 'failed' => ''
  55. ),
  56. 'perform_uninstall' => array(
  57. 'title' => '',
  58. 'success' => _t('_adm_msg_modules_success_uninstall'),
  59. 'failed' => ''
  60. ),
  61. 'perform_enable' => array(
  62. 'title' => '',
  63. 'success' => _t('_adm_msg_modules_success_enable'),
  64. 'failed' => ''
  65. ),
  66. 'perform_disable' => array(
  67. 'title' => '',
  68. 'success' => _t('_adm_msg_modules_success_disable'),
  69. 'failed' => ''
  70. ),
  71. 'check_script_version' => array(
  72. 'title' => _t('_adm_txt_modules_check_script_version'),
  73. ),
  74. 'check_dependencies' => array(
  75. 'title' => _t('_adm_txt_modules_check_dependencies'),
  76. ),
  77. 'show_introduction' => array(
  78. 'title' => _t('_adm_txt_modules_show_introduction'),
  79. ),
  80. 'check_permissions' => array(
  81. 'title' => _t('_adm_txt_modules_check_permissions'),
  82. ),
  83. 'change_permissions' => array(
  84. 'title' => _t('_adm_txt_modules_change_permissions'),
  85. ),
  86. 'move_sources' => array(
  87. 'title' => _t('_adm_txt_modules_move_sources'),
  88. ),
  89. 'execute_sql' => array(
  90. 'title' => _t('_adm_txt_modules_execute_sql'),
  91. ),
  92. 'install_language' => array(
  93. 'title' => _t('_adm_txt_modules_install_language'),
  94. ),
  95. 'update_languages' => array(
  96. 'title' => _t('_adm_txt_modules_update_languages'),
  97. ),
  98. 'update_relations' => array(
  99. 'title' => _t('_adm_txt_modules_update_relations'),
  100. ),
  101. 'process_deleted_profiles' => array(
  102. 'title' => _t('_adm_txt_modules_process_deleted_profiles'),
  103. ),
  104. 'recompile_global_paramaters' => array(
  105. 'title' => _t('_adm_txt_modules_recompile_global_paramaters'),
  106. ),
  107. 'recompile_menus' => array(
  108. 'title' => _t('_adm_txt_modules_recompile_menus'),
  109. ),
  110. 'recompile_site_stats' => array(
  111. 'title' => _t('_adm_txt_modules_recompile_site_stats'),
  112. ),
  113. 'recompile_comments' => array(
  114. 'title' => _t('_adm_txt_modules_recompile_comments'),
  115. ),
  116. 'recompile_member_actions' => array(
  117. 'title' => _t('_adm_txt_modules_recompile_member_actions'),
  118. ),
  119. 'recompile_votes' => array(
  120. 'title' => _t('_adm_txt_modules_recompile_votes'),
  121. ),
  122. 'recompile_search' => array(
  123. 'title' => _t('_adm_txt_modules_recompile_search'),
  124. ),
  125. 'recompile_injections' => array(
  126. 'title' => _t('_adm_txt_modules_recompile_injections'),
  127. ),
  128. 'recompile_permalinks' => array(
  129. 'title' => _t('_adm_txt_modules_recompile_permalinks'),
  130. ),
  131. 'recompile_alerts' => array(
  132. 'title' => _t('_adm_txt_modules_recompile_alerts'),
  133. ),
  134. 'clear_db_cache' => array(
  135. 'title' => _t('_adm_txt_modules_clear_db_cache'),
  136. ),
  137. 'show_conclusion' => array(
  138. 'title' => _t('_adm_txt_modules_show_conclusion'),
  139. ),
  140. );
  141. $this->_aNonHashableFiles = array(
  142. 'install',
  143. 'updates'
  144. );
  145. }
  146. public function install($aParams, $bAutoEnable = false)
  147. {
  148. $bAutoEnable = $bAutoEnable || (isset($aParams['auto_enable']) && (bool)$aParams['auto_enable']);
  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. $aResult = array();
  156. bx_alert('system', 'before_install', 0, false, array ('config' => $this->_aConfig, 'result' => &$aResult));
  157. if ($aResult && !$aResult['result'])
  158. return $aResult;
  159. //--- Check mandatory settings ---//
  160. if($this->oDb->isModuleParamsUsed($this->_aConfig['home_uri'], $this->_aConfig['home_dir'], $this->_aConfig['db_prefix'], $this->_aConfig['class_prefix']))
  161. return array(
  162. 'message' => _t('_adm_txt_modules_params_used'),
  163. 'result' => false
  164. );
  165. //--- Check version compatibility ---//
  166. $bCompatible = false;
  167. if(isset($this->_aConfig['compatible_with']) && is_array($this->_aConfig['compatible_with']))
  168. foreach($this->_aConfig['compatible_with'] as $iKey => $sVersion) {
  169. $sVersion = '/^' . str_replace(array('.', 'x'), array('\.', '[A-Za-z0-9-]+'), $sVersion) . '$/is';
  170. $bCompatible = $bCompatible || (preg_match($sVersion, bx_get_ver()) > 0);
  171. }
  172. if(!$bCompatible)
  173. return array(
  174. 'message' => $this->_displayResult('check_script_version', false, '_adm_err_modules_wrong_version_script'),
  175. 'result' => false
  176. );
  177. //--- Check for available translations ---//
  178. $oFile = $this->_getFileManager();
  179. $sModuleUri = $this->_aConfig['home_uri'];
  180. $aLanguages = $this->oDb->getModulesBy(array('type' => 'languages'));
  181. foreach($aLanguages as $aLanguage) {
  182. if($aLanguage['uri'] == 'en')
  183. continue;
  184. $aLanguageConfig = self::getModuleConfig(BX_DIRECTORY_PATH_MODULES . $aLanguage['path'] . '/install/config.php');
  185. if(empty($aLanguageConfig))
  186. continue;
  187. if(!isset($aLanguageConfig['includes'][$sModuleUri]) || empty($aLanguageConfig['includes'][$sModuleUri]))
  188. continue;
  189. $sSrcPath = 'modules/' . $aLanguage['path'] . 'install/data/' . $aLanguageConfig['includes'][$sModuleUri];
  190. $sDstPath = $aLanguageConfig['includes'][$sModuleUri];
  191. $oFile->copy($sSrcPath, $sDstPath);
  192. }
  193. //--- Check actions ---//
  194. $aResult = $this->_perform('install');
  195. if($aResult['result']) {
  196. $sDependencies = "";
  197. if(isset($this->_aConfig['install']['check_dependencies']) && (int)$this->_aConfig['install']['check_dependencies'] == 1 && isset($this->_aConfig['dependencies']) && is_array($this->_aConfig['dependencies']))
  198. $this->_aConfig['dependencies'] = array_keys($this->_aConfig['dependencies']);
  199. $iModuleId = $this->oDb->insertModule($this->_aConfig);
  200. bx_import('BxDolModule');
  201. $sTitleKey = BxDolModule::getTitleKey($this->_aConfig['home_uri']);
  202. bx_import('BxDolStudioLanguagesUtils');
  203. $oLanguages = BxDolStudioLanguagesUtils::getInstance();
  204. $oLanguages->addLanguageString($sTitleKey, $this->_aConfig['title']);
  205. $oLanguages->compileLanguage();
  206. $aFiles = array();
  207. $this->hashFiles($this->_sModulePath, $aFiles);
  208. foreach($aFiles as $aFile)
  209. $this->oDb->insertModuleTrack($iModuleId, $aFile);
  210. $this->oDb->cleanMemory('sys_modules_' . $this->_aConfig['home_uri']);
  211. $this->oDb->cleanMemory('sys_modules_' . $iModuleId);
  212. $this->oDb->cleanMemory('sys_modules');
  213. }
  214. bx_alert('system', 'install', 0, false, array ('config' => $this->_aConfig, 'result' => &$aResult));
  215. if($aResult['result'] && $bAutoEnable) {
  216. $aResultEnable = $this->enable($aParams);
  217. $aResult['result'] = $aResult['result'] & $aResultEnable['result'];
  218. $aResult['message'] = $aResult['message'] . $aResultEnable['message'];
  219. }
  220. return $aResult;
  221. }
  222. public function uninstall($aParams, $bAutoDisable = false)
  223. {
  224. $bAutoDisable = $bAutoDisable || (isset($aParams['auto_disable']) && (bool)$aParams['auto_disable']);
  225. //--- Check whether the module was already uninstalled ---//
  226. if(!$this->oDb->isModule($this->_aConfig['home_uri']))
  227. return array(
  228. 'message' => _t('_adm_err_modules_already_uninstalled'),
  229. 'result' => false
  230. );
  231. if($bAutoDisable) {
  232. $aResultDisable = $this->disable($aParams);
  233. if(!$aResultDisable['result'])
  234. return $aResultDisable;
  235. }
  236. $aResult = array();
  237. bx_alert('system', 'before_uninstall', 0, false, array ('config' => $this->_aConfig, 'result' => &$aResult));
  238. if ($aResult && !$aResult['result'])
  239. return $aResult;
  240. //--- Check for dependent modules ---//
  241. $bDependent = false;
  242. $aDependents = $this->oDb->getDependent($this->_aConfig['home_uri']);
  243. if(is_array($aDependents) && !empty($aDependents)) {
  244. $bDependent = true;
  245. $sMessage = '<br />' . _t('_adm_err_modules_wrong_dependency_uninstall') . '<br />';
  246. foreach($aDependents as $aDependent)
  247. $sMessage .= $aDependent['title'] . '<br />';
  248. }
  249. if($bDependent)
  250. return array(
  251. 'message' => $this->_displayResult('check_dependencies', false, $sMessage),
  252. 'result' => false
  253. );
  254. $aResult = $this->_perform('uninstall');
  255. if($aResult['result']) {
  256. $iModuleId = $this->oDb->deleteModule($this->_aConfig);
  257. bx_import('BxDolModule');
  258. $sTitleKey = BxDolModule::getTitleKey($this->_aConfig['home_uri']);
  259. bx_import('BxDolStudioLanguagesUtils');
  260. $oLanguages = BxDolStudioLanguagesUtils::getInstance();
  261. $oLanguages->deleteLanguageString($sTitleKey);
  262. $oLanguages->compileLanguage();
  263. $this->oDb->cleanMemory ('sys_modules_' . $this->_aConfig['home_uri']);
  264. $this->oDb->cleanMemory ('sys_modules_' . $iModuleId);
  265. $this->oDb->cleanMemory ('sys_modules');
  266. }
  267. if($bAutoDisable) {
  268. $aResult['result'] = $aResultDisable['result'] & $aResult['result'];
  269. $aResult['message'] = $aResultDisable['message'] . $aResult['message'];
  270. }
  271. bx_alert('system', 'uninstall', 0, false, array ('config' => $this->_aConfig, 'result' => &$aResult));
  272. return $aResult;
  273. }
  274. public function delete()
  275. {
  276. bx_import('BxDolFtp');
  277. $oFtp = new BxDolFtp($_SERVER['HTTP_HOST'], getParam('sys_ftp_login'), getParam('sys_ftp_password'), getParam('sys_ftp_dir'));
  278. if(!$oFtp->connect())
  279. return array(
  280. 'message' => _t('_adm_err_modules_cannot_connect_to_ftp'),
  281. 'result' => false
  282. );
  283. if(!$oFtp->delete('modules/' . $this->_aConfig['home_dir']))
  284. return array(
  285. 'message' => _t('_adm_err_modules_cannot_remove_package'),
  286. 'result' => false
  287. );
  288. return array(
  289. 'message' => '', //_t('_adm_msg_modules_success_delete'),
  290. 'result' => true
  291. );
  292. }
  293. public function recompile($aParams)
  294. {
  295. bx_import('BxDolStudioLanguagesUtils');
  296. $oLanguages = BxDolStudioLanguagesUtils::getInstance();
  297. $aResult = array('message' => '', 'result' => false);
  298. $aLanguages = $this->oDb->getLanguages();
  299. if (isAdmin() && !empty($aLanguages)) {
  300. $this->_updateLanguage(false, current($aLanguages));
  301. $bResult = false;
  302. foreach($aLanguages as $aLanguage) {
  303. $bResult = $this->_updateLanguage(true, $aLanguage) && $oLanguages->compileLanguage($aLanguage['id']);
  304. $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 />';
  305. $aResult['result'] |= $bResult;
  306. }
  307. }
  308. return $aResult;
  309. }
  310. public function enable($aParams)
  311. {
  312. $aModule = $this->oDb->getModuleByUri($this->_aConfig['home_uri']);
  313. //--- Check whether the module is installed ---//
  314. if(empty($aModule) || !is_array($aModule))
  315. return array(
  316. 'message' => _t('_adm_err_modules_module_not_installed'),
  317. 'result' => false
  318. );
  319. //--- Check whether the module is already enabled ---//
  320. if((int)$aModule['enabled'] != 0)
  321. return array(
  322. 'message' => _t('_adm_err_modules_already_enabled'),
  323. 'result' => false
  324. );
  325. $aResult = array();
  326. bx_alert('system', 'before_enable', 0, false, array ('config' => $this->_aConfig, 'result' => &$aResult));
  327. if ($aResult && !$aResult['result'])
  328. return $aResult;
  329. $aResult = $this->_perform('enable');
  330. if($aResult['result']) {
  331. $this->oDb->enableModuleByUri($aModule['uri']);
  332. $this->oDb->cleanMemory('sys_modules_' . $aModule['uri']);
  333. $this->oDb->cleanMemory('sys_modules_' . $aModule['id']);
  334. $this->oDb->cleanMemory('sys_modules');
  335. }
  336. bx_alert('system', 'enable', 0, false, array ('config' => $this->_aConfig, 'result' => &$aResult));
  337. return $aResult;
  338. }
  339. public function disable($aParams)
  340. {
  341. $aModule = $this->oDb->getModuleByUri($this->_aConfig['home_uri']);
  342. //--- Check whether the module is installed ---//
  343. if(empty($aModule) || !is_array($aModule))
  344. return array(
  345. 'message' => _t('_adm_err_modules_module_not_installed'),
  346. 'result' => false
  347. );
  348. //--- Check whether the module is already disabled ---//
  349. if((int)$aModule['enabled'] == 0)
  350. return array(
  351. 'message' => _t('_adm_err_modules_already_disabled'),
  352. 'result' => false
  353. );
  354. $aResult = array();
  355. bx_alert('system', 'before_disable', 0, false, array ('config' => $this->_aConfig, 'result' => &$aResult));
  356. if ($aResult && !$aResult['result'])
  357. return $aResult;
  358. //--- Check for dependent modules ---//
  359. $bDependent = false;
  360. $aDependents = $this->oDb->getDependent($this->_aConfig['home_uri']);
  361. if(is_array($aDependents) && !empty($aDependents)) {
  362. $bDependent = true;
  363. $sMessage = '<br />' . _t('_adm_err_modules_wrong_dependency_disable') . '<br />';
  364. foreach($aDependents as $aDependent)
  365. $sMessage .= $aDependent['title'] . '<br />';
  366. }
  367. if($bDependent)
  368. return array(
  369. 'message' => $this->_displayResult('check_dependencies', false, $sMessage),
  370. 'result' => false
  371. );
  372. $aResult = $this->_perform('disable');
  373. if($aResult['result']) {
  374. $this->oDb->disableModuleByUri($aModule['uri']);
  375. $this->oDb->cleanMemory('sys_modules_' . $aModule['uri']);
  376. $this->oDb->cleanMemory('sys_modules_' . $aModule['id']);
  377. $this->oDb->cleanMemory('sys_modules');
  378. }
  379. bx_alert('system', 'disable', 0, false, array ('config' => $this->_aConfig, 'result' => &$aResult));
  380. return $aResult;
  381. }
  382. protected function filePathWithoutBase($sPath)
  383. {
  384. return bx_ltrim_str($sPath, $this->_sModulePath);
  385. }
  386. function _perform($sOperationName)
  387. {
  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. {
  411. $sMessage = $this->_aActions[$sAction]['title'] . ' ';
  412. if(!empty($sResult) && substr($sResult, 0, 1) == '_')
  413. $sResult = _t($sResult) . '<br />';
  414. if(!$bResult)
  415. return $sMessage . '<span style="color:red;">' . $sResult . '</span>';
  416. if($bResult && !in_array($sAction, array('show_introduction', 'show_conclusion')) && !$this->_bShowOnSuccess)
  417. return;
  418. return $sMessage . '<span style="color:green;">' . (!empty($sResult) ? $sResult : _t('_adm_txt_modules_process_action_success') . '<br />') . '</span>';
  419. }
  420. //--- Action Methods ---//
  421. function actionOperationFailed($mixedResult)
  422. {
  423. return _t('_adm_err_modules_process_action_failed');
  424. }
  425. function actionCheckDependencies($sOperation)
  426. {
  427. $sContent = '';
  428. if(in_array($sOperation, array('install', 'enable', 'update'))) {
  429. if(!isset($this->_aConfig['dependencies']) || !is_array($this->_aConfig['dependencies']))
  430. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  431. foreach($this->_aConfig['dependencies'] as $sModuleUri => $sModuleTitle)
  432. if(!$this->oDb->isModule($sModuleUri))
  433. $sContent .= $sModuleTitle . '<br />';
  434. if(!empty($sContent))
  435. $sContent = '<br />' . _t('_adm_err_modules_wrong_dependency_install') . '<br />' . $sContent;
  436. }
  437. return empty($sContent) ? BX_DOL_STUDIO_INSTALLER_SUCCESS : array('code' => BX_DOL_STUDIO_INSTALLER_FAILED, 'content' => $sContent);
  438. }
  439. function actionCheckDependenciesFailed($mixedResult)
  440. {
  441. return $mixedResult['content'];
  442. }
  443. function actionShowIntroduction($sOperation)
  444. {
  445. if(!isset($this->_aConfig[$sOperation . '_info']['introduction']))
  446. return BX_DOL_STUDIO_INSTALLER_FAILED;
  447. $sPath = $this->_sHomePath . 'install/info/' . $this->_aConfig[$sOperation . '_info']['introduction'];
  448. return file_exists($sPath) ? array("code" => BX_DOL_STUDIO_INSTALLER_SUCCESS, "content" => "<pre>" . file_get_contents($sPath) . "</pre>") : BX_DOL_STUDIO_INSTALLER_FAILED;
  449. }
  450. function actionShowConclusion($sOperation)
  451. {
  452. if(!isset($this->_aConfig[$sOperation . '_info']['conclusion']))
  453. return BX_DOL_STUDIO_INSTALLER_FAILED;
  454. $sPath = $this->_sHomePath . 'install/info/' . $this->_aConfig[$sOperation . '_info']['conclusion'];
  455. return file_exists($sPath) ? array("code" => BX_DOL_STUDIO_INSTALLER_SUCCESS, "content" => "<pre>" . file_get_contents($sPath) . "</pre>") : BX_DOL_STUDIO_INSTALLER_FAILED;
  456. }
  457. function actionCheckPermissions($sOperation)
  458. {
  459. if(!isset($this->_aConfig[$sOperation . '_permissions']) || !is_array($this->_aConfig[$sOperation . '_permissions']))
  460. return BX_DOL_STUDIO_INSTALLER_FAILED;
  461. $aResult = array();
  462. $aPermissions = $this->_aConfig[$sOperation . '_permissions'];
  463. foreach($aPermissions as $sPermissions => $aFiles) {
  464. $sCheckFunction = 'is' . ucfirst($sPermissions);
  465. $sCptPermissions = _t('_adm_txt_modules_' . $sPermissions);
  466. foreach($aFiles as $sFile)
  467. if(!BxDolInstallerUtils::$sCheckFunction(str_replace(BX_DIRECTORY_PATH_ROOT, '', $this->_sModulePath . $sFile)))
  468. $aResult[] = array('path' => $this->_sModulePath . $sFile, 'permissions' => $sCptPermissions);
  469. }
  470. return empty($aResult) ? BX_DOL_STUDIO_INSTALLER_SUCCESS : array('code' => BX_DOL_STUDIO_INSTALLER_FAILED, 'content' => $aResult);
  471. }
  472. function actionCheckPermissionsFailed($mixedResult)
  473. {
  474. $sResult = '<br />' . _t('_adm_err_modules_wrong_permissions_check') . '<br />';
  475. foreach($mixedResult['content'] as $aFile)
  476. $sResult .= _t('_adm_err_modules_wrong_permissions_msg', $aFile['path'], $aFile['permissions']) . '<br />';
  477. return $sResult;
  478. }
  479. function actionChangePermissions($sOperation)
  480. {
  481. if(!isset($this->_aConfig[$sOperation . '_permissions']) || !is_array($this->_aConfig[$sOperation . '_permissions']))
  482. return BX_DOL_STUDIO_INSTALLER_FAILED;
  483. $aChangeItems = array();
  484. $aPermissions = $this->_aConfig[$sOperation . '_permissions'];
  485. foreach($aPermissions as $sPermissions => $aFiles) {
  486. $sCheckFunction = 'is' . ucfirst($sPermissions);
  487. foreach($aFiles as $sFile) {
  488. $sPath = bx_ltrim_str($this->_sModulePath . $sFile, BX_DIRECTORY_PATH_ROOT);
  489. if(BxDolInstallerUtils::$sCheckFunction($sPath))
  490. continue;
  491. $aChangeItems[] = array('file' => $sFile, 'path' => $sPath, 'permissions' => $sPermissions);
  492. }
  493. }
  494. if(empty($aChangeItems))
  495. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  496. $oFile = $this->_getFileManager();
  497. if(empty($oFile))
  498. return array('code' => BX_DOL_STUDIO_INSTALLER_FAILED);
  499. $aResult = array();
  500. foreach($aChangeItems as $aChangeItem)
  501. if(!$oFile->setPermissions($aChangeItem['path'], $aChangeItem['permissions']))
  502. $aResult[] = array('path' => $this->_sModulePath . $aChangeItem['file'], 'permissions' => $aChangeItem['permissions']);
  503. return empty($aResult) ? BX_DOL_STUDIO_INSTALLER_SUCCESS : array('code' => BX_DOL_STUDIO_INSTALLER_FAILED, 'content' => $aResult);
  504. }
  505. function actionChangePermissionsFailed($mixedResult)
  506. {
  507. if(empty($mixedResult['content']))
  508. return $this->actionOperationFailed($mixedResult);
  509. $sResult = '<br />' . _t('_adm_err_modules_wrong_permissions_change') . '<br />';
  510. foreach($mixedResult['content'] as $aFile)
  511. $sResult .= _t('_adm_err_modules_wrong_permissions_msg', $aFile['path'], $aFile['permissions']) . '<br />';
  512. return $sResult;
  513. }
  514. function actionMoveSources($sOperation)
  515. {
  516. $oFile = $this->_getFileManager();
  517. $aInstalled = array_merge(array('system'), $this->oDb->getModulesUri());
  518. $bResult = true;
  519. foreach($this->_aConfig['includes'] as $sUri => $sPath) {
  520. if(!in_array($sUri, $aInstalled) || empty($sPath))
  521. continue;
  522. if($sOperation == 'install') {
  523. $sSrcPath = 'modules/' . $this->_aConfig['home_dir'] . 'install/data/' . $sPath;
  524. $sDstPath = $sPath;
  525. $bResult &= $oFile->copy($sSrcPath, $sDstPath);
  526. } else if($sOperation == 'uninstall')
  527. $bResult &= $oFile->delete($sPath);
  528. }
  529. return $bResult ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  530. }
  531. function actionExecuteSql($sOperation)
  532. {
  533. switch($sOperation) {
  534. case 'install':
  535. $this->actionExecuteSql('disable');
  536. $this->actionExecuteSql('uninstall');
  537. break;
  538. case 'enable':
  539. $this->actionExecuteSql('disable');
  540. break;
  541. }
  542. bx_import('BxDolDb');
  543. $mixedResult = BxDolDb::getInstance()->executeSQL($this->_sHomePath . 'install/sql/' . $sOperation . '.sql');
  544. return $mixedResult === true ? BX_DOL_STUDIO_INSTALLER_SUCCESS : array('code' => BX_DOL_STUDIO_INSTALLER_FAILED, 'content' => $mixedResult);
  545. }
  546. function actionExecuteSqlFailed($mixedResult)
  547. {
  548. $sResult = '<br />' . _t('_adm_err_modules_wrong_mysql_query') . '<br />';
  549. foreach($mixedResult['content'] as $aQuery) {
  550. $sResult .= _t('_adm_err_modules_wrong_mysql_query_msg', $aQuery['error']) . '<br />';
  551. $sResult .= '<pre>' . $aQuery['query'] . '</pre>';
  552. }
  553. return $sResult;
  554. }
  555. function actionInstallLanguage($sOperation)
  556. {
  557. bx_import('BxDolStudioLanguagesUtils');
  558. $oLanguages = BxDolStudioLanguagesUtils::getInstance();
  559. $sLanguage = isset($this->_aConfig['home_uri']) ? $this->_aConfig['home_uri'] : '';
  560. $bResult = true;
  561. if($sOperation == 'install')
  562. $bResult = $oLanguages->installLanguage(array('path' => $this->_aConfig['home_dir'], 'uri' => $this->_aConfig['home_uri'], 'lang_category' => $this->_aConfig['language_category']),false);
  563. return $bResult && $oLanguages->compileLanguage(0, true) ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  564. }
  565. function actionUpdateLanguages($sOperation)
  566. {
  567. if(!isset($this->_aConfig['language_category']) || empty($this->_aConfig['language_category']))
  568. return BX_DOL_STUDIO_INSTALLER_FAILED;
  569. bx_import('BxDolStudioLanguagesUtils');
  570. $oLanguages = BxDolStudioLanguagesUtils::getInstance();
  571. $bResult = true;
  572. $aResult = array();
  573. //--- Process Language Category ---//
  574. if($sOperation == 'install')
  575. $iCategoryId = $oLanguages->addLanguageCategory($this->_aConfig['language_category']);
  576. //--- Process languages' key=>value pears ---//
  577. $aModule = array(
  578. 'path' => $this->_aConfig['home_dir'],
  579. 'uri' => $this->_aConfig['home_uri'],
  580. 'lang_category' => $this->_aConfig['language_category']
  581. );
  582. if($sOperation == 'install')
  583. $bResult = $oLanguages->restoreLanguage(0, $aModule, false);
  584. else if($sOperation == 'uninstall')
  585. $bResult = $oLanguages->removeLanguageByModule($aModule, false);
  586. if($sOperation == 'uninstall' && $bResult)
  587. $oLanguages->deleteLanguageCategory($this->_aConfig['language_category']);
  588. return $bResult && $oLanguages->compileLanguage(0, true) ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  589. }
  590. function actionUpdateRelations($sOperation)
  591. {
  592. if(!in_array($sOperation, array('install', 'uninstall', 'enable', 'disable')))
  593. return BX_DOL_STUDIO_INSTALLER_FAILED;
  594. if(empty($this->_aConfig['relations']) || !is_array($this->_aConfig['relations']))
  595. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  596. foreach($this->_aConfig['relations'] as $sModule) {
  597. if(!$this->oDb->isModuleByName($sModule))
  598. continue;
  599. $aRelation = $this->oDb->getRelationsBy(array('type' => 'module', 'value' => $sModule));
  600. if(empty($aRelation) || empty($aRelation['on_' . $sOperation]) || !BxDolRequest::serviceExists($aRelation['module'], $aRelation['on_' . $sOperation]))
  601. continue;
  602. BxDolService::call($aRelation['module'], $aRelation['on_' . $sOperation], array($this->_aConfig['home_uri']));
  603. }
  604. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  605. }
  606. function actionProcessDeletedProfiles($sOperation)
  607. {
  608. if(!in_array($sOperation, array('install', 'uninstall', 'enable', 'disable')))
  609. return BX_DOL_STUDIO_INSTALLER_FAILED;
  610. bx_import('BxDolProfileQuery');
  611. $o = BxDolProfileQuery::getInstance();
  612. $o->processDeletedProfiles();
  613. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  614. }
  615. function actionRecompileGlobalParamaters($sOperation)
  616. {
  617. $bResult = $this->oDb->cacheParamsClear();
  618. return $bResult ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  619. }
  620. function actionRecompileMenus($sOperation)
  621. {
  622. $bResult = $this->oDb->cleanCache('sys_objects_menu');
  623. return $bResult ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  624. }
  625. function actionRecompileSiteStats($sOperation)
  626. {
  627. $bResult = $this->oDb->cleanCache('sys_stat_site');
  628. return $bResult ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  629. }
  630. function actionRecompileComments($sOperation)
  631. {
  632. $bResult = $this->oDb->cleanCache('sys_objects_cmts');
  633. return $bResult ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  634. }
  635. function actionRecompileMemberActions($sOperation)
  636. {
  637. $bResult = $this->oDb->cleanCache('sys_objects_actions');
  638. return $bResult ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  639. }
  640. function actionRecompileVotes($sOperation)
  641. {
  642. $bResult = $this->oDb->cleanCache('sys_objects_vote');
  643. return $bResult ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  644. }
  645. function actionRecompileInjections($sOperation)
  646. {
  647. $bResult = $this->oDb->cleanCache('sys_injections.inc');
  648. $bResult = $bResult && $this->oDb->cleanCache('sys_injections_admin.inc');
  649. return $bResult ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  650. }
  651. function actionRecompilePermalinks($sOperation)
  652. {
  653. $bResult = true;
  654. bx_import('BxDolPermalinks');
  655. $bResult = $bResult && BxDolPermalinks::getInstance()->cacheInvalidate();
  656. $bResult = $bResult && ($this->actionRecompileMenus($sOperation) == BX_DOL_STUDIO_INSTALLER_SUCCESS);
  657. return $bResult ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  658. }
  659. function actionRecompileAlerts($sOperation)
  660. {
  661. $bResult = $this->oDb->cleanCache('sys_alerts');
  662. return $bResult ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  663. }
  664. function actionClearDbCache($sOperation)
  665. {
  666. $oCache = $this->oDb->getDbCacheObject();
  667. $bResult = $oCache->removeAllByPrefix('db_');
  668. return $bResult ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  669. }
  670. //--- Get/Set Methods ---//
  671. function getVendor()
  672. {
  673. return $this->_aConfig['vendor'];
  674. }
  675. function getName()
  676. {
  677. return $this->_aConfig['name'];
  678. }
  679. function getTitle()
  680. {
  681. return $this->_aConfig['title'];
  682. }
  683. function getHomeDir()
  684. {
  685. return $this->_aConfig['home_dir'];
  686. }
  687. //--- Protected methods ---//
  688. protected function _getFileManager($bUseFtp = true)
  689. {
  690. $oFile = null;
  691. if($bUseFtp) {
  692. bx_import('BxDolFtp');
  693. $oFile = new BxDolFtp(isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'localhost', getParam('sys_ftp_login'), getParam('sys_ftp_password'), getParam('sys_ftp_dir'));
  694. if(!$oFile->connect())
  695. return null;
  696. if(!$oFile->isDolphin())
  697. return null;
  698. } else {
  699. bx_import('BxDolFile');
  700. $oFile = BxDolFile::getInstance();
  701. }
  702. return $oFile;
  703. }
  704. }
  705. /** @} */