BxDolStudioInstaller.php 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079
  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. 'message' => _t('_adm_err_modules_already_installed'),
  148. 'result' => false
  149. );
  150. $aResult = array();
  151. bx_alert('system', 'before_install', 0, false, array ('config' => $this->_aConfig, 'result' => &$aResult));
  152. if ($aResult && !$aResult['result'])
  153. return $aResult;
  154. //--- Check mandatory settings ---//
  155. if($this->oDb->isModuleParamsUsed($this->_aConfig['home_uri'], $this->_aConfig['home_dir'], $this->_aConfig['db_prefix'], $this->_aConfig['class_prefix']))
  156. return array(
  157. 'message' => _t('_adm_txt_modules_params_used'),
  158. 'result' => false
  159. );
  160. //--- Check version compatibility ---//
  161. if(!$this->_isCompatibleWith())
  162. return array(
  163. 'message' => $this->_displayResult('check_script_version', false, '_adm_err_modules_wrong_version_script', $bHtmlResponce),
  164. 'result' => false
  165. );
  166. //--- Check for available translations ---//
  167. $oFile = $this->_getFileManager();
  168. $sModuleUri = $this->_aConfig['home_uri'];
  169. $aLanguages = $this->oDb->getModulesBy(array('type' => 'languages'));
  170. foreach($aLanguages as $aLanguage) {
  171. if($aLanguage['uri'] == 'en')
  172. continue;
  173. $aLanguageConfig = self::getModuleConfig(BX_DIRECTORY_PATH_MODULES . $aLanguage['path'] . '/install/config.php');
  174. if(empty($aLanguageConfig))
  175. continue;
  176. if(!isset($aLanguageConfig['includes'][$sModuleUri]) || empty($aLanguageConfig['includes'][$sModuleUri]))
  177. continue;
  178. $sSrcPath = 'modules/' . $aLanguage['path'] . 'install/data/' . $aLanguageConfig['includes'][$sModuleUri];
  179. $sDstPath = $aLanguageConfig['includes'][$sModuleUri];
  180. $oFile->copy($sSrcPath, $sDstPath);
  181. }
  182. $aResultBefore = $this->_onInstallBefore();
  183. if($aResultBefore !== BX_DOL_STUDIO_INSTALLER_SUCCESS)
  184. return array(
  185. 'message' => $aResultBefore['content'],
  186. 'result' => false
  187. );
  188. //--- Check actions ---//
  189. $aResult = $this->_perform('install', $aParams);
  190. if($aResult['result']) {
  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['name'], $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. setParam('sys_revision', 1 + getParam('sys_revision'));
  359. bx_alert('system', 'enable', 0, false, array ('config' => $this->_aConfig, 'result' => &$aResult));
  360. return $aResult;
  361. }
  362. public function disable($aParams)
  363. {
  364. $bHtmlResponce = isset($aParams['html_response']) && (bool)$aParams['html_response'];
  365. $aModule = $this->oDb->getModuleByUri($this->_aConfig['home_uri']);
  366. //--- Check whether the module is installed ---//
  367. if(empty($aModule) || !is_array($aModule))
  368. return array(
  369. 'message' => _t('_adm_err_modules_module_not_installed'),
  370. 'result' => false
  371. );
  372. //--- Check whether the module is already disabled ---//
  373. if((int)$aModule['enabled'] == 0)
  374. return array(
  375. 'message' => _t('_adm_err_modules_already_disabled'),
  376. 'result' => false
  377. );
  378. $aResult = array();
  379. if(!empty($this->_aConfig['before_disable'])) {
  380. $aResult = $this->_perform('before_disable', $aParams);
  381. if($aResult && !$aResult['result'])
  382. return $aResult;
  383. }
  384. $aResult = array();
  385. bx_alert('system', 'before_disable', 0, false, array ('config' => $this->_aConfig, 'result' => &$aResult));
  386. if ($aResult && !$aResult['result'])
  387. return $aResult;
  388. //--- Check for dependent modules ---//
  389. $bDependent = false;
  390. $aDependents = $this->oDb->getDependent($this->_aConfig['name'], $this->_aConfig['home_uri']);
  391. if(is_array($aDependents) && !empty($aDependents)) {
  392. $bDependent = true;
  393. $sMessage = '<br />' . _t('_adm_err_modules_wrong_dependency_disable') . '<br />';
  394. foreach($aDependents as $aDependent)
  395. $sMessage .= $aDependent['title'] . '<br />';
  396. }
  397. if($bDependent)
  398. return array(
  399. 'message' => $this->_displayResult('check_dependencies', false, $sMessage, $bHtmlResponce),
  400. 'result' => false
  401. );
  402. $aResultBefore = $this->_onDisableBefore();
  403. if($aResultBefore !== BX_DOL_STUDIO_INSTALLER_SUCCESS)
  404. return array(
  405. 'message' => $aResultBefore['content'],
  406. 'result' => false
  407. );
  408. $aResult = $this->_perform('disable', $aParams);
  409. if($aResult['result']) {
  410. $this->oDb->disableModuleByUri($aModule['uri']);
  411. $this->cleanupMemoryAfterAction($aModule['uri'], $aModule['id']);
  412. $this->_onDisableAfter();
  413. if(!empty($this->_aConfig['disable_success']))
  414. $this->_perform('disable_success', $aParams);
  415. }
  416. else {
  417. if(!empty($this->_aConfig['disable_failed']))
  418. $this->_perform('disable_failed', $aParams);
  419. }
  420. bx_alert('system', 'disable', 0, false, array ('config' => $this->_aConfig, 'result' => &$aResult));
  421. return $aResult;
  422. }
  423. //--- Get/Set Methods ---//
  424. public function getVendor()
  425. {
  426. return $this->_aConfig['vendor'];
  427. }
  428. public function getName()
  429. {
  430. return $this->_aConfig['name'];
  431. }
  432. public function getTitle()
  433. {
  434. return $this->_aConfig['title'];
  435. }
  436. public function getHomeDir()
  437. {
  438. return $this->_aConfig['home_dir'];
  439. }
  440. //--- Action Methods ---//
  441. protected function actionOperationFailed($mixedResult)
  442. {
  443. return _t('_adm_err_modules_process_action_failed');
  444. }
  445. protected function actionCheckDependencies($sOperation)
  446. {
  447. $sContent = '';
  448. if(in_array($sOperation, array('install', 'enable', 'update'))) {
  449. if(!isset($this->_aConfig['dependencies']) || !is_array($this->_aConfig['dependencies']))
  450. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  451. foreach($this->_aConfig['dependencies'] as $sModule => $sModuleTitle) {
  452. if($this->oDb->isModule($sModule) && $this->oDb->isEnabled($sModule))
  453. continue;
  454. if($this->oDb->isModuleByName($sModule) && $this->oDb->isEnabledByName($sModule))
  455. continue;
  456. $sContent .= $sModuleTitle . '<br />';
  457. }
  458. if(!empty($sContent))
  459. $sContent = '<br />' . _t('_adm_err_modules_wrong_dependency_install') . '<br />' . $sContent;
  460. }
  461. return empty($sContent) ? BX_DOL_STUDIO_INSTALLER_SUCCESS : array('code' => BX_DOL_STUDIO_INSTALLER_FAILED, 'content' => $sContent);
  462. }
  463. protected function actionCheckDependenciesFailed($mixedResult)
  464. {
  465. return $mixedResult['content'];
  466. }
  467. protected function actionShowIntroduction($sOperation)
  468. {
  469. if(!isset($this->_aConfig[$sOperation . '_info']['introduction']))
  470. return BX_DOL_STUDIO_INSTALLER_FAILED;
  471. $sPath = $this->_sHomePath . 'install/info/' . $this->_aConfig[$sOperation . '_info']['introduction'];
  472. return file_exists($sPath) ? array("code" => BX_DOL_STUDIO_INSTALLER_SUCCESS, "content" => file_get_contents($sPath)) : BX_DOL_STUDIO_INSTALLER_FAILED;
  473. }
  474. protected function actionShowConclusion($sOperation)
  475. {
  476. if(!isset($this->_aConfig[$sOperation . '_info']['conclusion']))
  477. return BX_DOL_STUDIO_INSTALLER_FAILED;
  478. $sPath = $this->_sHomePath . 'install/info/' . $this->_aConfig[$sOperation . '_info']['conclusion'];
  479. return file_exists($sPath) ? array("code" => BX_DOL_STUDIO_INSTALLER_SUCCESS, "content" => file_get_contents($sPath)) : BX_DOL_STUDIO_INSTALLER_FAILED;
  480. }
  481. protected function actionMoveSources($sOperation)
  482. {
  483. $oFile = $this->_getFileManager();
  484. $aInstalled = array_merge(array('system'), $this->oDb->getModulesUri());
  485. $bResult = true;
  486. foreach($this->_aConfig['includes'] as $sUri => $sPath) {
  487. if(!in_array($sUri, $aInstalled) || empty($sPath))
  488. continue;
  489. if($sOperation == 'install') {
  490. $sSrcPath = 'modules/' . $this->_aConfig['home_dir'] . 'install/data/' . $sPath;
  491. $sDstPath = $sPath;
  492. $bResult &= $oFile->copy($sSrcPath, $sDstPath);
  493. } else if($sOperation == 'uninstall')
  494. $bResult &= $oFile->delete($sPath);
  495. }
  496. return $bResult ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  497. }
  498. protected function actionExecuteSql($sOperation)
  499. {
  500. switch($sOperation) {
  501. case 'install':
  502. $this->actionExecuteSql('disable');
  503. $this->actionExecuteSql('uninstall');
  504. break;
  505. case 'enable':
  506. $this->actionExecuteSql('disable');
  507. break;
  508. }
  509. $mixedResult = $this->oDb->executeSQL($this->_sHomePath . 'install/sql/' . $sOperation . '.sql', $this->getMarkersForDb());
  510. return $mixedResult === true ? BX_DOL_STUDIO_INSTALLER_SUCCESS : array('code' => BX_DOL_STUDIO_INSTALLER_FAILED, 'content' => $mixedResult);
  511. }
  512. protected function actionExecuteSqlFailed($mixedResult)
  513. {
  514. if(is_int($mixedResult))
  515. return $this->actionOperationFailed($mixedResult);
  516. $sResult = _t('_adm_err_modules_wrong_mysql_query') . '<br />';
  517. foreach($mixedResult['content'] as $aQuery) {
  518. $sResult .= _t('_adm_err_modules_wrong_mysql_query_msg', $aQuery['error']) . '<br />';
  519. $sResult .= '<pre>' . $aQuery['query'] . '</pre>';
  520. }
  521. return $sResult;
  522. }
  523. protected function actionInstallLanguage($sOperation)
  524. {
  525. $oLanguages = BxDolStudioLanguagesUtils::getInstance();
  526. $sLanguage = isset($this->_aConfig['home_uri']) ? $this->_aConfig['home_uri'] : '';
  527. $bResult = true;
  528. if($sOperation == 'install')
  529. $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);
  530. return $bResult && $oLanguages->compileLanguage(0, true) ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  531. }
  532. protected function actionUpdateLanguages($sOperation)
  533. {
  534. if(!isset($this->_aConfig['language_category']) || empty($this->_aConfig['language_category']))
  535. return BX_DOL_STUDIO_INSTALLER_FAILED;
  536. $oLanguages = BxDolStudioLanguagesUtils::getInstance();
  537. $bResult = true;
  538. $aResult = array();
  539. //--- Process Language Category ---//
  540. if($sOperation == 'install')
  541. $iCategoryId = $oLanguages->addLanguageCategory($this->_aConfig['language_category']);
  542. //--- Process languages' key=>value pears ---//
  543. $aModule = array(
  544. 'name' => $this->_aConfig['name'],
  545. 'path' => $this->_aConfig['home_dir'],
  546. 'uri' => $this->_aConfig['home_uri'],
  547. 'lang_category' => $this->_aConfig['language_category']
  548. );
  549. if($sOperation == 'install')
  550. $bResult = $oLanguages->restoreLanguage(0, $aModule, false);
  551. else if($sOperation == 'uninstall')
  552. $bResult = $oLanguages->removeLanguageByModule($aModule, false);
  553. if($sOperation == 'uninstall' && $bResult)
  554. $oLanguages->deleteLanguageCategory($this->_aConfig['language_category']);
  555. return $bResult && $oLanguages->compileLanguage(0, true) ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  556. }
  557. /**
  558. * NOTE. The action is ONLY needed for dependent module to let
  559. * Notifications based module(s) know that he(they) should
  560. * update (request and save) handlers from this dependent module.
  561. */
  562. protected function actionUpdateRelations($sOperation)
  563. {
  564. if(!in_array($sOperation, array('install', 'uninstall', 'enable', 'disable')))
  565. return BX_DOL_STUDIO_INSTALLER_FAILED;
  566. if(empty($this->_aConfig['relations']) || !is_array($this->_aConfig['relations']))
  567. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  568. foreach($this->_aConfig['relations'] as $sModule) {
  569. if(!$this->oDb->isModuleByName($sModule))
  570. continue;
  571. $aRelation = $this->oDb->getRelationsBy(array('type' => 'module', 'value' => $sModule));
  572. if(empty($aRelation) || empty($aRelation['on_' . $sOperation]) || !BxDolRequest::serviceExists($aRelation['module'], $aRelation['on_' . $sOperation]))
  573. continue;
  574. BxDolService::call($aRelation['module'], $aRelation['on_' . $sOperation], array($this->_aConfig['home_uri']));
  575. }
  576. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  577. }
  578. /**
  579. * NOTE. The action is ONLY needed for Notifications based modules
  580. * to update (request and save) handlers from all dependent modules.
  581. */
  582. protected function actionUpdateRelationsForAll($sOperation)
  583. {
  584. if(!in_array($sOperation, array('enable', 'disable')))
  585. return BX_DOL_STUDIO_INSTALLER_FAILED;
  586. if($sOperation == 'enable')
  587. $this->oDb->insertRelation($this->_aConfig['name'], $this->_aConfig['relation_handlers']);
  588. $aRelation = $this->oDb->getRelationsBy(array('type' => 'module', 'value' => $this->_aConfig['name']));
  589. if(empty($aRelation) || empty($aRelation['on_' . $sOperation]))
  590. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  591. $aModules = $this->oDb->getModulesBy(array('type' => 'all', 'active' => 1));
  592. foreach($aModules as $aModule) {
  593. $aConfig = self::getModuleConfig($aModule);
  594. if(!empty($aConfig['relations']) && is_array($aConfig['relations']) && in_array($this->_aConfig['name'], $aConfig['relations']))
  595. BxDolService::call($this->_aConfig['name'], $aRelation['on_' . $sOperation], array($aModule['uri']));
  596. }
  597. if($sOperation == 'disable')
  598. $this->oDb->deleteRelation($this->_aConfig['name']);
  599. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  600. }
  601. /**
  602. *
  603. * Connections objects associated with module data. It must be defined which content is associated with the connection.
  604. * The key is connection object name and value is array (possible array values: type, conn, table, field_id).
  605. * If 'type' == 'profiles', then it is considered profiles connection and other possible param is 'conn' ('initiator', 'content' or 'both')
  606. * If 'type' == 'custom' (or ommited), then other possible params are 'conn', 'table' and 'field_id'
  607. * @param string $sOperation - operation type.
  608. */
  609. protected function actionProcessConnections($sOperation)
  610. {
  611. if(!in_array($sOperation, array('uninstall')) || empty($this->_aConfig['connections']))
  612. return BX_DOL_STUDIO_INSTALLER_FAILED;
  613. foreach($this->_aConfig['connections'] as $sObjectConnections => $a) {
  614. $o = BxDolConnection::getObjectInstance($sObjectConnections);
  615. if(!$o)
  616. continue;
  617. $sFuncSuffix = 'DeleteInitiatorAndContent';
  618. if (isset($a['conn']) && 'initiator' == $a['conn'])
  619. $sFuncSuffix = 'DeleteInitiator';
  620. elseif (isset($a['conn']) && 'content' == $a['conn'])
  621. $sFuncSuffix = 'DeleteContent';
  622. if (isset($a['type']) && 'profiles' == $a['type']) {
  623. $sFunc = 'onModuleProfile' . $sFuncSuffix;
  624. $o->$sFunc($this->_aConfig['name']);
  625. } else {
  626. $sFunc = 'onModule' . $sFuncSuffix;
  627. $o->$sFunc($a['table'], $a['field_id']);
  628. }
  629. }
  630. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  631. }
  632. /**
  633. *
  634. * Process the list of menu triggers provided in config array.
  635. * It must be specified in the module which adds menu item and in modules where menu items are added, @see BxDolMenu::processMenuTrigger
  636. * @param string $sOperation - operation type.
  637. */
  638. protected function actionProcessMenuTriggers($sOperation)
  639. {
  640. if(empty($this->_aConfig['menu_triggers']))
  641. return BX_DOL_STUDIO_INSTALLER_FAILED;
  642. foreach($this->_aConfig['menu_triggers'] as $sMenuTriggerName)
  643. BxDolMenu::processMenuTrigger($sMenuTriggerName);
  644. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  645. }
  646. /**
  647. *
  648. * Process the list of page triggers provided in config array.
  649. * It must be specified in the module which adds page block and in modules where page blocks are added, @see BxDolPage::processPageTrigger
  650. * @param string $sOperation - operation type.
  651. */
  652. protected function actionProcessPageTriggers($sOperation)
  653. {
  654. if(empty($this->_aConfig['page_triggers']))
  655. return BX_DOL_STUDIO_INSTALLER_FAILED;
  656. foreach($this->_aConfig['page_triggers'] as $sPageTriggerName)
  657. BxDolPage::processPageTrigger($sPageTriggerName);
  658. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  659. }
  660. protected function actionProcessDeletedProfiles($sOperation)
  661. {
  662. if(!in_array($sOperation, array('install', 'uninstall', 'enable', 'disable')))
  663. return BX_DOL_STUDIO_INSTALLER_FAILED;
  664. $o = BxDolProfileQuery::getInstance();
  665. $o->processDeletedProfiles();
  666. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  667. }
  668. /**
  669. *
  670. * Process the list of extended search forms provided in config array.
  671. * @param string $sOperation - operation type.
  672. */
  673. protected function actionProcessEsearches($sOperation)
  674. {
  675. if(empty($this->_aConfig['esearches']))
  676. return BX_DOL_STUDIO_INSTALLER_FAILED;
  677. foreach($this->_aConfig['esearches'] as $sObject) {
  678. if(in_array($sOperation, array('install', 'enable', 'enable_success')))
  679. BxDolSearchExtended::getObjectInstance($sObject);
  680. else if(in_array($sOperation, array('uninstall', 'disable')))
  681. BxDolSearchExtended::getObjectInstance($sObject)->clean();
  682. }
  683. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  684. }
  685. /**
  686. *
  687. * Process the list of transcoders provided in config array.
  688. * Transcoder objects to automatically register/unregister necessary alerts for.
  689. * @param string $sOperation - operation type.
  690. */
  691. protected function actionRegisterTranscoders($sOperation)
  692. {
  693. if(empty($this->_aConfig['transcoders']))
  694. return BX_DOL_STUDIO_INSTALLER_FAILED;
  695. BxDolTranscoderImage::registerHandlersArray($this->_aConfig['transcoders']);
  696. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  697. }
  698. /**
  699. *
  700. * Process the list of transcoders provided in config array.
  701. * Transcoder objects to automatically register/unregister necessary alerts for.
  702. * @param string $sOperation - operation type.
  703. */
  704. protected function actionUnregisterTranscoders($sOperation)
  705. {
  706. if(empty($this->_aConfig['transcoders']))
  707. return BX_DOL_STUDIO_INSTALLER_FAILED;
  708. BxDolTranscoderImage::unregisterHandlersArray($this->_aConfig['transcoders']);
  709. BxDolTranscoderImage::cleanupObjectsArray($this->_aConfig['transcoders']);
  710. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  711. }
  712. protected function actionClearDbCache($sOperation)
  713. {
  714. $oCache = $this->oDb->getDbCacheObject();
  715. $bResult = $oCache->removeAllByPrefix('db_');
  716. $bResult &= $this->oDb->cleanMemoryAll();
  717. $aKeys = array_keys($GLOBALS);
  718. $aKeysPrefixes = array('bx_dol_cache_memory_');
  719. foreach($aKeysPrefixes as $sKeyPrefix)
  720. foreach($aKeys as $sKey)
  721. if(substr($sKey, 0, strlen($sKeyPrefix)) == $sKeyPrefix)
  722. unset($GLOBALS[$sKey]);
  723. return $bResult ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  724. }
  725. protected function actionClearTemplateCache($sOperation)
  726. {
  727. $aCaches = array('template', 'css', 'js');
  728. $oCacheUtilities = BxDolCacheUtilities::getInstance();
  729. foreach($aCaches as $sCache) {
  730. $aResult = $oCacheUtilities->clear($sCache);
  731. if($aResult === false)
  732. continue;
  733. if(isset($aResult['code']) && $aResult['code'] != 0)
  734. return BX_DOL_STUDIO_INSTALLER_FAILED;
  735. }
  736. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  737. }
  738. protected function _onInstallBefore()
  739. {
  740. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  741. }
  742. protected function _onInstallAfter()
  743. {
  744. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  745. }
  746. protected function _onEnableBefore()
  747. {
  748. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  749. }
  750. protected function _onEnableAfter()
  751. {
  752. BxDolCmts::onModuleEnable($this->_aConfig['name']);
  753. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  754. }
  755. protected function _onDisableBefore()
  756. {
  757. BxDolCmts::onModuleDisable($this->_aConfig['name']);
  758. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  759. }
  760. protected function _onDisableAfter()
  761. {
  762. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  763. }
  764. protected function _onUninstallBefore()
  765. {
  766. // check if module is already waiting while files are deleting
  767. if(self::isModulePendingUninstall($this->_aConfig['home_uri']))
  768. return array(
  769. 'code' => BX_DOL_STUDIO_INSTALLER_FAILED,
  770. 'content' => _t('_adm_err_modules_pending_uninstall_already'),
  771. );
  772. $bSetModulePendingUninstall = false;
  773. // delete comments and queue for deletion comments attachments
  774. $iFiles = 0;
  775. BxDolCmts::onModuleUninstall($this->_aConfig['name'], $iFiles);
  776. if($iFiles)
  777. $bSetModulePendingUninstall = true;
  778. // queue for deletion storage files
  779. if(!empty($this->_aConfig['storages']) && is_array($this->_aConfig['storages']))
  780. foreach($this->_aConfig['storages'] as $s)
  781. if(($o = BxDolStorage::getObjectInstance($s)) && $o->queueFilesForDeletionFromObject())
  782. $bSetModulePendingUninstall = true;
  783. // if some files were added to the queue, set module as pending uninstall
  784. if ($bSetModulePendingUninstall) {
  785. self::setModulePendingUninstall($this->_aConfig['home_uri']);
  786. return array(
  787. 'code' => BX_DOL_STUDIO_INSTALLER_FAILED,
  788. 'content' => _t('_adm_err_modules_pending_uninstall'),
  789. );
  790. }
  791. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  792. }
  793. protected function _onUninstallAfter()
  794. {
  795. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  796. }
  797. protected function _perform($sOperationName, $aParams = array())
  798. {
  799. $bHtmlResponce = isset($aParams['html_response']) && (bool)$aParams['html_response'];
  800. if(!defined('BX_SKIP_INSTALL_CHECK') && !defined('BX_DOL_CRON_EXECUTE') && !$GLOBALS['logged']['admin'])
  801. return array('message' => '_adm_mod_err_only_admin_can_perform_operations_with_modules', 'result' => false);
  802. $sMessage = '';
  803. foreach($this->_aConfig[$sOperationName] as $sAction => $iEnabled) {
  804. $sMethod = 'action' . bx_gen_method_name($sAction);
  805. if($iEnabled == 0 || !method_exists($this, $sMethod))
  806. continue;
  807. $mixedResult = $this->$sMethod($sOperationName);
  808. //--- On Success ---//
  809. if((is_int($mixedResult) && (int)$mixedResult == BX_DOL_STUDIO_INSTALLER_SUCCESS) || (isset($mixedResult['code']) && (int)$mixedResult['code'] == BX_DOL_STUDIO_INSTALLER_SUCCESS)) {
  810. $sMessage .= $this->_displayResult($sAction, true, isset($mixedResult['content']) ? $mixedResult['content'] : '', $bHtmlResponce);
  811. continue;
  812. }
  813. //--- On Failed ---//
  814. $sMethodFailed = $sMethod . 'Failed';
  815. return array('message' => $this->_displayResult($sAction, false, method_exists($this, $sMethodFailed) ? $this->$sMethodFailed($mixedResult) : $this->actionOperationFailed($mixedResult), $bHtmlResponce), 'result' => false);
  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($sVersionCur, $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. /** @} */