BxDolStudioInstaller.php 39 KB

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