BxDolStudioInstaller.php 32 KB

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