1
0

BxDolStudioInstaller.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  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 TridentStudio Trident Studio
  7. * @{
  8. */
  9. bx_import('BxDolInstallerUtils');
  10. define("BX_DOL_STUDIO_INSTALLER_SUCCESS", 0);
  11. define("BX_DOL_STUDIO_INSTALLER_FAILED", 1);
  12. /**
  13. * Base class for Installer classes in modules engine.
  14. *
  15. * The class contains different check functions which are used during the installation process.
  16. * An object of the class is created automatically with Dolphin's modules installer.
  17. * Installation/Uninstallation process can be controlled with config.php file located in [module]/install/ folder.
  18. *
  19. *
  20. * Example of usage:
  21. * @see any module included in the default Dolphin's package.
  22. *
  23. *
  24. * Memberships/ACL:
  25. * Doesn't depend on user's membership.
  26. *
  27. *
  28. * Alerts:
  29. * no alerts available
  30. *
  31. */
  32. class BxDolStudioInstaller extends BxDolInstallerUtils
  33. {
  34. protected $oDb;
  35. protected $_aConfig;
  36. protected $_sBasePath;
  37. protected $_sHomePath;
  38. protected $_sModulePath;
  39. protected $_bUseFtp;
  40. protected $_aActions;
  41. protected $_bShowOnSuccess = false;
  42. function __construct($aConfig)
  43. {
  44. parent::__construct();
  45. $this->oDb = bx_instance('BxDolStudioInstallerQuery');
  46. $this->_aConfig = $aConfig;
  47. $this->_sBasePath = BX_DIRECTORY_PATH_MODULES;
  48. $this->_sHomePath = $this->_sBasePath . $aConfig['home_dir'];
  49. $this->_sModulePath = $this->_sBasePath . $aConfig['home_dir'];
  50. $this->_bUseFtp = BX_FORCE_USE_FTP_FILE_TRANSFER;
  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. 'move_sources' => array(
  82. 'title' => _t('_adm_txt_modules_move_sources'),
  83. ),
  84. 'execute_sql' => array(
  85. 'title' => _t('_adm_txt_modules_execute_sql'),
  86. ),
  87. 'install_language' => array(
  88. 'title' => _t('_adm_txt_modules_install_language'),
  89. ),
  90. 'update_languages' => array(
  91. 'title' => _t('_adm_txt_modules_update_languages'),
  92. ),
  93. 'update_relations' => array(
  94. 'title' => _t('_adm_txt_modules_update_relations'),
  95. ),
  96. 'process_connections' => array(
  97. 'title' => _t('_adm_txt_modules_process_connections'),
  98. ),
  99. 'process_deleted_profiles' => array(
  100. 'title' => _t('_adm_txt_modules_process_deleted_profiles'),
  101. ),
  102. 'process_menu_triggers' => array(
  103. 'title' => _t('_adm_txt_modules_process_menu_triggers'),
  104. ),
  105. 'process_page_triggers' => array(
  106. 'title' => _t('_adm_txt_modules_process_page_triggers'),
  107. ),
  108. 'process_storages' => array(
  109. 'title' => _t('_adm_txt_modules_process_storages'),
  110. ),
  111. 'register_transcoders' => array(
  112. 'title' => _t('_adm_txt_modules_register_transcoders'),
  113. ),
  114. 'unregister_transcoders' => array(
  115. 'title' => _t('_adm_txt_modules_unregister_transcoders'),
  116. ),
  117. 'clear_db_cache' => array(
  118. 'title' => _t('_adm_txt_modules_clear_db_cache'),
  119. ),
  120. 'show_conclusion' => array(
  121. 'title' => _t('_adm_txt_modules_show_conclusion'),
  122. ),
  123. );
  124. $this->_aNonHashableFiles = array(
  125. 'install',
  126. 'updates'
  127. );
  128. }
  129. public function install($aParams, $bAutoEnable = false)
  130. {
  131. $bAutoEnable = $bAutoEnable || (isset($aParams['auto_enable']) && (bool)$aParams['auto_enable']);
  132. $bHtmlResponce = isset($aParams['html_response']) && (bool)$aParams['html_response'];
  133. //--- Check whether the module was already installed ---//
  134. if($this->oDb->isModule($this->_aConfig['home_uri']))
  135. return array(
  136. 'message' => _t('_adm_err_modules_already_installed'),
  137. 'result' => false
  138. );
  139. $aResult = array();
  140. bx_alert('system', 'before_install', 0, false, array ('config' => $this->_aConfig, 'result' => &$aResult));
  141. if ($aResult && !$aResult['result'])
  142. return $aResult;
  143. //--- Check mandatory settings ---//
  144. if($this->oDb->isModuleParamsUsed($this->_aConfig['home_uri'], $this->_aConfig['home_dir'], $this->_aConfig['db_prefix'], $this->_aConfig['class_prefix']))
  145. return array(
  146. 'message' => _t('_adm_txt_modules_params_used'),
  147. 'result' => false
  148. );
  149. //--- Check version compatibility ---//
  150. if(!$this->_isCompatibleWith())
  151. return array(
  152. 'message' => $this->_displayResult('check_script_version', false, '_adm_err_modules_wrong_version_script', $bHtmlResponce),
  153. 'result' => false
  154. );
  155. //--- Check for available translations ---//
  156. $oFile = $this->_getFileManager();
  157. $sModuleUri = $this->_aConfig['home_uri'];
  158. $aLanguages = $this->oDb->getModulesBy(array('type' => 'languages'));
  159. foreach($aLanguages as $aLanguage) {
  160. if($aLanguage['uri'] == 'en')
  161. continue;
  162. $aLanguageConfig = self::getModuleConfig(BX_DIRECTORY_PATH_MODULES . $aLanguage['path'] . '/install/config.php');
  163. if(empty($aLanguageConfig))
  164. continue;
  165. if(!isset($aLanguageConfig['includes'][$sModuleUri]) || empty($aLanguageConfig['includes'][$sModuleUri]))
  166. continue;
  167. $sSrcPath = 'modules/' . $aLanguage['path'] . 'install/data/' . $aLanguageConfig['includes'][$sModuleUri];
  168. $sDstPath = $aLanguageConfig['includes'][$sModuleUri];
  169. $oFile->copy($sSrcPath, $sDstPath);
  170. }
  171. //--- Check actions ---//
  172. $aResult = $this->_perform('install', $aParams);
  173. if($aResult['result']) {
  174. $sDependencies = "";
  175. if(isset($this->_aConfig['install']['check_dependencies']) && (int)$this->_aConfig['install']['check_dependencies'] == 1 && isset($this->_aConfig['dependencies']) && is_array($this->_aConfig['dependencies']))
  176. $this->_aConfig['dependencies'] = array_keys($this->_aConfig['dependencies']);
  177. $iModuleId = $this->oDb->insertModule($this->_aConfig);
  178. bx_import('BxDolModule');
  179. $sTitleKey = BxDolModule::getTitleKey($this->_aConfig['home_uri']);
  180. bx_import('BxDolStudioLanguagesUtils');
  181. $oLanguages = BxDolStudioLanguagesUtils::getInstance();
  182. $oLanguages->addLanguageString($sTitleKey, $this->_aConfig['title']);
  183. $oLanguages->compileLanguage();
  184. bx_import('BxDolStudioInstallerUtils');
  185. BxDolStudioInstallerUtils::getInstance()->checkModules();
  186. $aFiles = array();
  187. $this->hashFiles($this->_sModulePath, $aFiles);
  188. foreach($aFiles as $aFile)
  189. $this->oDb->insertModuleTrack($iModuleId, $aFile);
  190. $this->oDb->cleanMemory('sys_modules_' . $this->_aConfig['home_uri']);
  191. $this->oDb->cleanMemory('sys_modules_' . $iModuleId);
  192. $this->oDb->cleanMemory('sys_modules');
  193. if(!empty($this->_aConfig['install_success']))
  194. $this->_perform('install_success', $aParams);
  195. }
  196. else {
  197. if(!empty($this->_aConfig['install_failed']))
  198. $this->_perform('install_failed', $aParams);
  199. }
  200. bx_alert('system', 'install', 0, false, array ('config' => $this->_aConfig, 'result' => &$aResult));
  201. if($aResult['result'] && $bAutoEnable) {
  202. $aResultEnable = $this->enable($aParams);
  203. $aResult['result'] = $aResult['result'] & $aResultEnable['result'];
  204. $aResult['message'] = $aResult['message'] . $aResultEnable['message'];
  205. }
  206. return $aResult;
  207. }
  208. public function uninstall($aParams, $bAutoDisable = false)
  209. {
  210. $bAutoDisable = $bAutoDisable || (isset($aParams['auto_disable']) && (bool)$aParams['auto_disable']);
  211. $bHtmlResponce = isset($aParams['html_response']) && (bool)$aParams['html_response'];
  212. //--- Check whether the module was already uninstalled ---//
  213. if(!$this->oDb->isModule($this->_aConfig['home_uri']))
  214. return array(
  215. 'message' => _t('_adm_err_modules_already_uninstalled'),
  216. 'result' => false
  217. );
  218. if($bAutoDisable) {
  219. $aResultDisable = $this->disable($aParams);
  220. if(!$aResultDisable['result'])
  221. return $aResultDisable;
  222. }
  223. $aResult = array();
  224. bx_alert('system', 'before_uninstall', 0, false, array ('config' => $this->_aConfig, 'result' => &$aResult));
  225. if ($aResult && !$aResult['result'])
  226. return $aResult;
  227. //--- Check for dependent modules ---//
  228. $bDependent = false;
  229. $aDependents = $this->oDb->getDependent($this->_aConfig['home_uri']);
  230. if(is_array($aDependents) && !empty($aDependents)) {
  231. $bDependent = true;
  232. $sMessage = '<br />' . _t('_adm_err_modules_wrong_dependency_uninstall') . '<br />';
  233. foreach($aDependents as $aDependent)
  234. $sMessage .= $aDependent['title'] . '<br />';
  235. }
  236. if($bDependent)
  237. return array(
  238. 'message' => $this->_displayResult('check_dependencies', false, $sMessage, $bHtmlResponce),
  239. 'result' => false
  240. );
  241. $aResult = $this->_perform('uninstall', $aParams);
  242. if($aResult['result']) {
  243. $iModuleId = $this->oDb->deleteModule($this->_aConfig);
  244. bx_import('BxDolModule');
  245. $sTitleKey = BxDolModule::getTitleKey($this->_aConfig['home_uri']);
  246. bx_import('BxDolStudioLanguagesUtils');
  247. $oLanguages = BxDolStudioLanguagesUtils::getInstance();
  248. $oLanguages->deleteLanguageString($sTitleKey);
  249. $oLanguages->compileLanguage();
  250. $this->oDb->cleanMemory ('sys_modules_' . $this->_aConfig['home_uri']);
  251. $this->oDb->cleanMemory ('sys_modules_' . $iModuleId);
  252. $this->oDb->cleanMemory ('sys_modules');
  253. if(!empty($this->_aConfig['uninstall_success']))
  254. $this->_perform('uninstall_success', $aParams);
  255. }
  256. else {
  257. if(!empty($this->_aConfig['uninstall_failed']))
  258. $this->_perform('uninstall_failed', $aParams);
  259. }
  260. if($bAutoDisable) {
  261. $aResult['result'] = $aResultDisable['result'] & $aResult['result'];
  262. $aResult['message'] = $aResultDisable['message'] . $aResult['message'];
  263. }
  264. bx_alert('system', 'uninstall', 0, false, array ('config' => $this->_aConfig, 'result' => &$aResult));
  265. return $aResult;
  266. }
  267. public function delete($aParams)
  268. {
  269. $aError = array(
  270. 'message' => _t('_adm_err_modules_cannot_remove_package'),
  271. 'result' => false
  272. );
  273. $oFile = $this->_getFileManager();
  274. if(empty($oFile))
  275. return $aError;
  276. if(!$oFile->delete('modules/' . $this->_aConfig['home_dir']))
  277. return $aError;
  278. return array(
  279. 'message' => '', //_t('_adm_msg_modules_success_delete'),
  280. 'result' => true
  281. );
  282. }
  283. public function recompile($aParams)
  284. {
  285. bx_import('BxDolStudioLanguagesUtils');
  286. $oLanguages = BxDolStudioLanguagesUtils::getInstance();
  287. $aResult = array('message' => '', 'result' => false);
  288. $aLanguages = $this->oDb->getLanguages();
  289. if (isAdmin() && !empty($aLanguages)) {
  290. $this->_updateLanguage(false, current($aLanguages));
  291. $bResult = false;
  292. foreach($aLanguages as $aLanguage) {
  293. $bResult = $this->_updateLanguage(true, $aLanguage) && $oLanguages->compileLanguage($aLanguage['id']);
  294. $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 />';
  295. $aResult['result'] |= $bResult;
  296. }
  297. }
  298. return $aResult;
  299. }
  300. public function enable($aParams)
  301. {
  302. $aModule = $this->oDb->getModuleByUri($this->_aConfig['home_uri']);
  303. //--- Check whether the module is installed ---//
  304. if(empty($aModule) || !is_array($aModule))
  305. return array(
  306. 'message' => _t('_adm_err_modules_module_not_installed'),
  307. 'result' => false
  308. );
  309. //--- Check whether the module is already enabled ---//
  310. if((int)$aModule['enabled'] != 0)
  311. return array(
  312. 'message' => _t('_adm_err_modules_already_enabled'),
  313. 'result' => false
  314. );
  315. $aResult = array();
  316. bx_alert('system', 'before_enable', 0, false, array ('config' => $this->_aConfig, 'result' => &$aResult));
  317. if ($aResult && !$aResult['result'])
  318. return $aResult;
  319. $aResult = $this->_perform('enable', $aParams);
  320. if($aResult['result']) {
  321. $this->oDb->enableModuleByUri($aModule['uri']);
  322. $this->oDb->cleanMemory('sys_modules_' . $aModule['uri']);
  323. $this->oDb->cleanMemory('sys_modules_' . $aModule['id']);
  324. $this->oDb->cleanMemory('sys_modules');
  325. if(!empty($this->_aConfig['enable_success']))
  326. $this->_perform('enable_success', $aParams);
  327. }
  328. else {
  329. if(!empty($this->_aConfig['enable_failed']))
  330. $this->_perform('enable_failed', $aParams);
  331. }
  332. bx_alert('system', 'enable', 0, false, array ('config' => $this->_aConfig, 'result' => &$aResult));
  333. return $aResult;
  334. }
  335. public function disable($aParams)
  336. {
  337. $bHtmlResponce = isset($aParams['html_response']) && (bool)$aParams['html_response'];
  338. $aModule = $this->oDb->getModuleByUri($this->_aConfig['home_uri']);
  339. //--- Check whether the module is installed ---//
  340. if(empty($aModule) || !is_array($aModule))
  341. return array(
  342. 'message' => _t('_adm_err_modules_module_not_installed'),
  343. 'result' => false
  344. );
  345. //--- Check whether the module is already disabled ---//
  346. if((int)$aModule['enabled'] == 0)
  347. return array(
  348. 'message' => _t('_adm_err_modules_already_disabled'),
  349. 'result' => false
  350. );
  351. $aResult = array();
  352. bx_alert('system', 'before_disable', 0, false, array ('config' => $this->_aConfig, 'result' => &$aResult));
  353. if ($aResult && !$aResult['result'])
  354. return $aResult;
  355. //--- Check for dependent modules ---//
  356. $bDependent = false;
  357. $aDependents = $this->oDb->getDependent($this->_aConfig['home_uri']);
  358. if(is_array($aDependents) && !empty($aDependents)) {
  359. $bDependent = true;
  360. $sMessage = '<br />' . _t('_adm_err_modules_wrong_dependency_disable') . '<br />';
  361. foreach($aDependents as $aDependent)
  362. $sMessage .= $aDependent['title'] . '<br />';
  363. }
  364. if($bDependent)
  365. return array(
  366. 'message' => $this->_displayResult('check_dependencies', false, $sMessage, $bHtmlResponce),
  367. 'result' => false
  368. );
  369. $aResult = $this->_perform('disable', $aParams);
  370. if($aResult['result']) {
  371. $this->oDb->disableModuleByUri($aModule['uri']);
  372. $this->oDb->cleanMemory('sys_modules_' . $aModule['uri']);
  373. $this->oDb->cleanMemory('sys_modules_' . $aModule['id']);
  374. $this->oDb->cleanMemory('sys_modules');
  375. if(!empty($this->_aConfig['disable_success']))
  376. $this->_perform('disable_success', $aParams);
  377. }
  378. else {
  379. if(!empty($this->_aConfig['disable_failed']))
  380. $this->_perform('disable_failed', $aParams);
  381. }
  382. bx_alert('system', 'disable', 0, false, array ('config' => $this->_aConfig, 'result' => &$aResult));
  383. return $aResult;
  384. }
  385. //--- Get/Set Methods ---//
  386. public function getVendor()
  387. {
  388. return $this->_aConfig['vendor'];
  389. }
  390. public function getName()
  391. {
  392. return $this->_aConfig['name'];
  393. }
  394. public function getTitle()
  395. {
  396. return $this->_aConfig['title'];
  397. }
  398. public function getHomeDir()
  399. {
  400. return $this->_aConfig['home_dir'];
  401. }
  402. //--- Action Methods ---//
  403. protected function actionOperationFailed($mixedResult)
  404. {
  405. return _t('_adm_err_modules_process_action_failed');
  406. }
  407. protected function actionCheckDependencies($sOperation)
  408. {
  409. $sContent = '';
  410. if(in_array($sOperation, array('install', 'enable', 'update'))) {
  411. if(!isset($this->_aConfig['dependencies']) || !is_array($this->_aConfig['dependencies']))
  412. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  413. foreach($this->_aConfig['dependencies'] as $sModuleUri => $sModuleTitle)
  414. if(!$this->oDb->isModule($sModuleUri))
  415. $sContent .= $sModuleTitle . '<br />';
  416. if(!empty($sContent))
  417. $sContent = '<br />' . _t('_adm_err_modules_wrong_dependency_install') . '<br />' . $sContent;
  418. }
  419. return empty($sContent) ? BX_DOL_STUDIO_INSTALLER_SUCCESS : array('code' => BX_DOL_STUDIO_INSTALLER_FAILED, 'content' => $sContent);
  420. }
  421. protected function actionCheckDependenciesFailed($mixedResult)
  422. {
  423. return $mixedResult['content'];
  424. }
  425. protected function actionShowIntroduction($sOperation)
  426. {
  427. if(!isset($this->_aConfig[$sOperation . '_info']['introduction']))
  428. return BX_DOL_STUDIO_INSTALLER_FAILED;
  429. $sPath = $this->_sHomePath . 'install/info/' . $this->_aConfig[$sOperation . '_info']['introduction'];
  430. return file_exists($sPath) ? array("code" => BX_DOL_STUDIO_INSTALLER_SUCCESS, "content" => file_get_contents($sPath)) : BX_DOL_STUDIO_INSTALLER_FAILED;
  431. }
  432. protected function actionShowConclusion($sOperation)
  433. {
  434. if(!isset($this->_aConfig[$sOperation . '_info']['conclusion']))
  435. return BX_DOL_STUDIO_INSTALLER_FAILED;
  436. $sPath = $this->_sHomePath . 'install/info/' . $this->_aConfig[$sOperation . '_info']['conclusion'];
  437. return file_exists($sPath) ? array("code" => BX_DOL_STUDIO_INSTALLER_SUCCESS, "content" => file_get_contents($sPath)) : BX_DOL_STUDIO_INSTALLER_FAILED;
  438. }
  439. protected function actionMoveSources($sOperation)
  440. {
  441. $oFile = $this->_getFileManager();
  442. $aInstalled = array_merge(array('system'), $this->oDb->getModulesUri());
  443. $bResult = true;
  444. foreach($this->_aConfig['includes'] as $sUri => $sPath) {
  445. if(!in_array($sUri, $aInstalled) || empty($sPath))
  446. continue;
  447. if($sOperation == 'install') {
  448. $sSrcPath = 'modules/' . $this->_aConfig['home_dir'] . 'install/data/' . $sPath;
  449. $sDstPath = $sPath;
  450. $bResult &= $oFile->copy($sSrcPath, $sDstPath);
  451. } else if($sOperation == 'uninstall')
  452. $bResult &= $oFile->delete($sPath);
  453. }
  454. return $bResult ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  455. }
  456. protected function actionExecuteSql($sOperation)
  457. {
  458. switch($sOperation) {
  459. case 'install':
  460. $this->actionExecuteSql('disable');
  461. $this->actionExecuteSql('uninstall');
  462. break;
  463. case 'enable':
  464. $this->actionExecuteSql('disable');
  465. break;
  466. }
  467. $mixedResult = $this->oDb->executeSQL($this->_sHomePath . 'install/sql/' . $sOperation . '.sql', $this->getMarkersForDb());
  468. return $mixedResult === true ? BX_DOL_STUDIO_INSTALLER_SUCCESS : array('code' => BX_DOL_STUDIO_INSTALLER_FAILED, 'content' => $mixedResult);
  469. }
  470. protected function actionExecuteSqlFailed($mixedResult)
  471. {
  472. if(is_int($mixedResult))
  473. return $this->actionOperationFailed($mixedResult);
  474. $sResult = '<br />' . _t('_adm_err_modules_wrong_mysql_query') . '<br />';
  475. foreach($mixedResult['content'] as $aQuery) {
  476. $sResult .= _t('_adm_err_modules_wrong_mysql_query_msg', $aQuery['error']) . '<br />';
  477. $sResult .= '<pre>' . $aQuery['query'] . '</pre>';
  478. }
  479. return $sResult;
  480. }
  481. protected function actionInstallLanguage($sOperation)
  482. {
  483. bx_import('BxDolStudioLanguagesUtils');
  484. $oLanguages = BxDolStudioLanguagesUtils::getInstance();
  485. $sLanguage = isset($this->_aConfig['home_uri']) ? $this->_aConfig['home_uri'] : '';
  486. $bResult = true;
  487. if($sOperation == 'install')
  488. $bResult = $oLanguages->installLanguage(array('path' => $this->_aConfig['home_dir'], 'uri' => $this->_aConfig['home_uri'], 'lang_category' => $this->_aConfig['language_category']),false);
  489. return $bResult && $oLanguages->compileLanguage(0, true) ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  490. }
  491. protected function actionUpdateLanguages($sOperation)
  492. {
  493. if(!isset($this->_aConfig['language_category']) || empty($this->_aConfig['language_category']))
  494. return BX_DOL_STUDIO_INSTALLER_FAILED;
  495. bx_import('BxDolStudioLanguagesUtils');
  496. $oLanguages = BxDolStudioLanguagesUtils::getInstance();
  497. $bResult = true;
  498. $aResult = array();
  499. //--- Process Language Category ---//
  500. if($sOperation == 'install')
  501. $iCategoryId = $oLanguages->addLanguageCategory($this->_aConfig['language_category']);
  502. //--- Process languages' key=>value pears ---//
  503. $aModule = array(
  504. 'path' => $this->_aConfig['home_dir'],
  505. 'uri' => $this->_aConfig['home_uri'],
  506. 'lang_category' => $this->_aConfig['language_category']
  507. );
  508. if($sOperation == 'install')
  509. $bResult = $oLanguages->restoreLanguage(0, $aModule, false);
  510. else if($sOperation == 'uninstall')
  511. $bResult = $oLanguages->removeLanguageByModule($aModule, false);
  512. if($sOperation == 'uninstall' && $bResult)
  513. $oLanguages->deleteLanguageCategory($this->_aConfig['language_category']);
  514. return $bResult && $oLanguages->compileLanguage(0, true) ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  515. }
  516. protected function actionUpdateRelations($sOperation)
  517. {
  518. if(!in_array($sOperation, array('install', 'uninstall', 'enable', 'disable')))
  519. return BX_DOL_STUDIO_INSTALLER_FAILED;
  520. if(empty($this->_aConfig['relations']) || !is_array($this->_aConfig['relations']))
  521. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  522. foreach($this->_aConfig['relations'] as $sModule) {
  523. if(!$this->oDb->isModuleByName($sModule))
  524. continue;
  525. $aRelation = $this->oDb->getRelationsBy(array('type' => 'module', 'value' => $sModule));
  526. if(empty($aRelation) || empty($aRelation['on_' . $sOperation]) || !BxDolRequest::serviceExists($aRelation['module'], $aRelation['on_' . $sOperation]))
  527. continue;
  528. BxDolService::call($aRelation['module'], $aRelation['on_' . $sOperation], array($this->_aConfig['home_uri']));
  529. }
  530. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  531. }
  532. /**
  533. *
  534. * Connections objects associated with module data. It must be defined which content is associated with the connection.
  535. * The key is connection object name and value is array (possible array values: type, conn, table, field_id).
  536. * If 'type' == 'profiles', then it is considered profiles connection and other possible param is 'conn' ('initiator', 'content' or 'both')
  537. * If 'type' == 'custom' (or ommited), then other possible params are 'conn', 'table' and 'field_id'
  538. * @param string $sOperation - operation type.
  539. */
  540. protected function actionProcessConnections($sOperation)
  541. {
  542. if(!in_array($sOperation, array('uninstall')) || empty($this->_aConfig['connections']))
  543. return BX_DOL_STUDIO_INSTALLER_FAILED;
  544. bx_import('BxDolConnection');
  545. foreach($this->_aConfig['connections'] as $sObjectConnections => $a) {
  546. $o = BxDolConnection::getObjectInstance($sObjectConnections);
  547. if(!$o)
  548. continue;
  549. $sFuncSuffix = 'DeleteInitiatorAndContent';
  550. if (isset($a['conn']) && 'initiator' == $a['conn'])
  551. $sFuncSuffix = 'DeleteInitiator';
  552. elseif (isset($a['conn']) && 'content' == $a['conn'])
  553. $sFuncSuffix = 'DeleteContent';
  554. if (isset($a['type']) && 'profiles' == $a['type']) {
  555. $sFunc = 'onModuleProfile' . $sFuncSuffix;
  556. $o->$sFunc($this->_aConfig['name']);
  557. } else {
  558. $sFunc = 'onModule' . $sFuncSuffix;
  559. $o->$sFunc($a['table'], $a['field_id']);
  560. }
  561. }
  562. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  563. }
  564. /**
  565. *
  566. * Process the list of menu triggers provided in config array.
  567. * It must be specified in the module which adds menu item and in modules where menu items are added, @see BxDolMenu::processMenuTrigger
  568. * @param string $sOperation - operation type.
  569. */
  570. protected function actionProcessMenuTriggers($sOperation)
  571. {
  572. if(empty($this->_aConfig['menu_triggers']))
  573. return BX_DOL_STUDIO_INSTALLER_FAILED;
  574. bx_import('BxDolMenu');
  575. foreach($this->_aConfig['menu_triggers'] as $sMenuTriggerName)
  576. BxDolMenu::processMenuTrigger($sMenuTriggerName);
  577. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  578. }
  579. /**
  580. *
  581. * Process the list of page triggers provided in config array.
  582. * It must be specified in the module which adds page block and in modules where page blocks are added, @see BxDolPage::processPageTrigger
  583. * @param string $sOperation - operation type.
  584. */
  585. protected function actionProcessPageTriggers($sOperation)
  586. {
  587. if(empty($this->_aConfig['page_triggers']))
  588. return BX_DOL_STUDIO_INSTALLER_FAILED;
  589. bx_import('BxDolPage');
  590. foreach($this->_aConfig['page_triggers'] as $sPageTriggerName)
  591. BxDolPage::processPageTrigger($sPageTriggerName);
  592. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  593. }
  594. protected function actionProcessDeletedProfiles($sOperation)
  595. {
  596. if(!in_array($sOperation, array('install', 'uninstall', 'enable', 'disable')))
  597. return BX_DOL_STUDIO_INSTALLER_FAILED;
  598. bx_import('BxDolProfileQuery');
  599. $o = BxDolProfileQuery::getInstance();
  600. $o->processDeletedProfiles();
  601. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  602. }
  603. /**
  604. *
  605. * Storage objects to automatically delete files from upon module uninstallation.
  606. * Note. Don't add storage objects used in transcoder objects.
  607. * @param string $sOperation - operation type.
  608. */
  609. protected function actionProcessStorages($sOperation)
  610. {
  611. if(empty($this->_aConfig['storages']))
  612. return BX_DOL_STUDIO_INSTALLER_FAILED;
  613. // check if module is already waiting while files are deleting
  614. if(self::isModulePendingUninstall($this->_aConfig['home_uri']))
  615. return array(
  616. 'code' => BX_DOL_STUDIO_INSTALLER_FAILED,
  617. 'content' => _t('_adm_err_modules_pending_uninstall_already'),
  618. );
  619. $bSetModulePendingUninstall = false;
  620. // queue for deletion storage files
  621. bx_import('BxDolStorage');
  622. foreach($this->_aConfig['storages'] as $s)
  623. if(($o = BxDolStorage::getObjectInstance($s)) && $o->queueFilesForDeletionFromObject())
  624. $bSetModulePendingUninstall = true;
  625. // delete comments and queue for deletion comments attachments
  626. $iFiles = 0;
  627. bx_import('BxDolCmts');
  628. BxDolCmts::onModuleUninstall($this->_aConfig['name'], $iFiles);
  629. if($iFiles)
  630. $bSetModulePendingUninstall = true;
  631. // if some files were added to the queue, set module as pending uninstall
  632. if ($bSetModulePendingUninstall) {
  633. self::setModulePendingUninstall($this->_aConfig['home_uri']);
  634. return array(
  635. 'code' => BX_DOL_STUDIO_INSTALLER_FAILED,
  636. 'content' => _t('_adm_err_modules_pending_uninstall'),
  637. );
  638. }
  639. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  640. }
  641. protected function actionProcessStoragesFailed($mixedResult)
  642. {
  643. if(is_int($mixedResult))
  644. return $this->actionOperationFailed($mixedResult);
  645. return $mixedResult['content'];
  646. }
  647. /**
  648. *
  649. * Process the list of transcoders provided in config array.
  650. * Transcoder objects to automatically register/unregister necessary alerts for.
  651. * @param string $sOperation - operation type.
  652. */
  653. protected function actionRegisterTranscoders($sOperation)
  654. {
  655. if(empty($this->_aConfig['transcoders']))
  656. return BX_DOL_STUDIO_INSTALLER_FAILED;
  657. bx_import('BxDolTranscoderImage');
  658. BxDolTranscoderImage::registerHandlersArray($this->_aConfig['transcoders']);
  659. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  660. }
  661. /**
  662. *
  663. * Process the list of transcoders provided in config array.
  664. * Transcoder objects to automatically register/unregister necessary alerts for.
  665. * @param string $sOperation - operation type.
  666. */
  667. protected function actionUnregisterTranscoders($sOperation)
  668. {
  669. if(empty($this->_aConfig['transcoders']))
  670. return BX_DOL_STUDIO_INSTALLER_FAILED;
  671. bx_import('BxDolTranscoderImage');
  672. BxDolTranscoderImage::unregisterHandlersArray($this->_aConfig['transcoders']);
  673. BxDolTranscoderImage::cleanupObjectsArray($this->_aConfig['transcoders']);
  674. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  675. }
  676. protected function actionClearDbCache($sOperation)
  677. {
  678. $oCache = $this->oDb->getDbCacheObject();
  679. $bResult = $oCache->removeAllByPrefix('db_');
  680. return $bResult ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  681. }
  682. protected function _perform($sOperationName, $aParams = array())
  683. {
  684. $bHtmlResponce = isset($aParams['html_response']) && (bool)$aParams['html_response'];
  685. if(!defined('BX_SKIP_INSTALL_CHECK') && !defined('BX_DOL_CRON_EXECUTE') && !$GLOBALS['logged']['admin'])
  686. return array('message' => '_adm_mod_err_only_admin_can_perform_operations_with_modules', 'result' => false);
  687. $sMessage = '';
  688. foreach($this->_aConfig[$sOperationName] as $sAction => $iEnabled) {
  689. $sMethod = 'action' . bx_gen_method_name($sAction);
  690. if($iEnabled == 0 || !method_exists($this, $sMethod))
  691. continue;
  692. $mixedResult = $this->$sMethod($sOperationName);
  693. //--- On Success ---//
  694. if((is_int($mixedResult) && (int)$mixedResult == BX_DOL_STUDIO_INSTALLER_SUCCESS) || (isset($mixedResult['code']) && (int)$mixedResult['code'] == BX_DOL_STUDIO_INSTALLER_SUCCESS)) {
  695. $sMessage .= $this->_displayResult($sAction, true, isset($mixedResult['content']) ? $mixedResult['content'] : '', $bHtmlResponce);
  696. continue;
  697. }
  698. //--- On Failed ---//
  699. $sMethodFailed = $sMethod . 'Failed';
  700. return array('message' => $this->_displayResult($sAction, false, method_exists($this, $sMethodFailed) ? $this->$sMethodFailed($mixedResult) : $this->actionOperationFailed($mixedResult), $bHtmlResponce), 'result' => false);
  701. }
  702. if($this->_bShowOnSuccess)
  703. $sMessage = $this->_aActions['perform_' . $sOperationName]['success'] . $sMessage;
  704. return array('message' => $sMessage, 'result' => true);
  705. }
  706. protected function _displayResult($sAction, $bResult, $sResult = '', $bHtmlResponse = true)
  707. {
  708. if($bResult && !in_array($sAction, array('show_introduction', 'show_conclusion')) && !$this->_bShowOnSuccess)
  709. return '';
  710. $sTitle = $this->_aActions[$sAction]['title'] . ' ';
  711. if(!empty($sResult))
  712. $sResult = (substr($sResult, 0, 1) == '_' ? _t($sResult) : $sResult);
  713. $sContent = !empty($sResult) ? $sResult : _t($bResult ? '_adm_txt_modules_process_action_success' : '_adm_err_modules_process_action_failed');
  714. if(!$bHtmlResponse)
  715. return $sTitle . $sContent;
  716. bx_import('BxDolStudioTemplate');
  717. return BxDolStudioTemplate::getInstance()->parseHtmlByName('mod_action_result_step.html', array(
  718. 'color' => $bResult ? 'green' : 'red',
  719. 'title' => $sTitle,
  720. 'content' => $sContent
  721. ));
  722. }
  723. protected function _getFileManager()
  724. {
  725. $oFile = null;
  726. if($this->_bUseFtp) {
  727. bx_import('BxDolFtp');
  728. $oFile = new BxDolFtp(isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'localhost', getParam('sys_ftp_login'), getParam('sys_ftp_password'), getParam('sys_ftp_dir'));
  729. if(!$oFile->connect())
  730. return null;
  731. if(!$oFile->isDolphin())
  732. return null;
  733. }
  734. else {
  735. bx_import('BxDolFile');
  736. $oFile = BxDolFile::getInstance();
  737. }
  738. return $oFile;
  739. }
  740. protected function _isCompatibleWith()
  741. {
  742. $sVersionCur = bx_get_ver();
  743. $bCompatible = false;
  744. if(isset($this->_aConfig['compatible_with']) && is_array($this->_aConfig['compatible_with']))
  745. foreach($this->_aConfig['compatible_with'] as $iKey => $sVersionReq)
  746. $bCompatible = $bCompatible || (version_compare($sVersionCur, $sVersionReq, '>=') == 1);
  747. return $bCompatible;
  748. }
  749. protected function filePathWithoutBase($sPath)
  750. {
  751. return bx_ltrim_str($sPath, $this->_sModulePath);
  752. }
  753. protected function getMarkersForDb()
  754. {
  755. return array(
  756. 'from' => array('{db_name}'),
  757. 'to' => array(defined('BX_DATABASE_NAME') ? BX_DATABASE_NAME : ''),
  758. );
  759. }
  760. }
  761. /** @} */