1
0

BxDolStudioInstaller.php 33 KB

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