BxDolStudioInstaller.php 37 KB

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