BxDolStudioInstaller.php 31 KB

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