BxDolStudioInstaller.php 38 KB

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