BxDolStudioInstaller.php 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  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. bx_srv_ii($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. bx_srv_ii($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. if(!$bResult)
  716. return BX_DOL_STUDIO_INSTALLER_FAILED;
  717. bx_alert('system', 'clear_cache', 0, 0, array('type' => 'custom'));
  718. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  719. }
  720. protected function actionClearTemplateCache($sOperation)
  721. {
  722. $aCaches = array('template', 'css', 'js');
  723. $oCacheUtilities = BxDolCacheUtilities::getInstance();
  724. foreach($aCaches as $sCache) {
  725. $aResult = $oCacheUtilities->clear($sCache);
  726. if($aResult === false)
  727. continue;
  728. if(isset($aResult['code']) && $aResult['code'] != 0)
  729. return BX_DOL_STUDIO_INSTALLER_FAILED;
  730. }
  731. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  732. }
  733. protected function _onInstallBefore()
  734. {
  735. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  736. }
  737. protected function _onInstallAfter()
  738. {
  739. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  740. }
  741. protected function _onEnableBefore()
  742. {
  743. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  744. }
  745. protected function _onEnableAfter()
  746. {
  747. BxDolCmts::onModuleEnable($this->_aConfig['name']);
  748. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  749. }
  750. protected function _onDisableBefore()
  751. {
  752. BxDolCmts::onModuleDisable($this->_aConfig['name']);
  753. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  754. }
  755. protected function _onDisableAfter()
  756. {
  757. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  758. }
  759. protected function _onUninstallBefore()
  760. {
  761. // check if module is already waiting while files are deleting
  762. if(self::isModulePendingUninstall($this->_aConfig['home_uri']))
  763. return array(
  764. 'code' => BX_DOL_STUDIO_INSTALLER_FAILED,
  765. 'content' => _t('_adm_err_modules_pending_uninstall_already'),
  766. );
  767. $bSetModulePendingUninstall = false;
  768. // delete comments and queue for deletion comments attachments
  769. $iFiles = 0;
  770. BxDolCmts::onModuleUninstall($this->_aConfig['name'], $iFiles);
  771. if($iFiles)
  772. $bSetModulePendingUninstall = true;
  773. // delete forms common data
  774. BxDolForm::onModuleUninstall($this->_aConfig['name'], $iFiles);
  775. // queue for deletion storage files
  776. if(!empty($this->_aConfig['storages']) && is_array($this->_aConfig['storages']))
  777. foreach($this->_aConfig['storages'] as $s)
  778. if(($o = BxDolStorage::getObjectInstance($s)) && $o->queueFilesForDeletionFromObject())
  779. $bSetModulePendingUninstall = true;
  780. // if some files were added to the queue, set module as pending uninstall
  781. if ($bSetModulePendingUninstall) {
  782. self::setModulePendingUninstall($this->_aConfig['home_uri']);
  783. return array(
  784. 'code' => BX_DOL_STUDIO_INSTALLER_FAILED,
  785. 'content' => _t('_adm_err_modules_pending_uninstall'),
  786. );
  787. }
  788. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  789. }
  790. protected function _onUninstallAfter()
  791. {
  792. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  793. }
  794. protected function _perform($sOperationName, $aParams = array())
  795. {
  796. $bHtmlResponce = isset($aParams['html_response']) && (bool)$aParams['html_response'];
  797. if(!defined('BX_SKIP_INSTALL_CHECK') && !defined('BX_DOL_CRON_EXECUTE') && !$GLOBALS['logged']['admin'])
  798. return array(
  799. 'code' => BX_DOL_STUDIO_IU_RCE_NOT_AUTHORIZED,
  800. 'message' => '_adm_mod_err_only_admin_can_perform_operations_with_modules',
  801. 'result' => false
  802. );
  803. $sMessage = '';
  804. foreach($this->_aConfig[$sOperationName] as $sAction => $iEnabled) {
  805. $sMethod = 'action' . bx_gen_method_name($sAction);
  806. if($iEnabled == 0 || !method_exists($this, $sMethod))
  807. continue;
  808. $mixedResult = $this->$sMethod($sOperationName);
  809. //--- On Success ---//
  810. if((is_int($mixedResult) && (int)$mixedResult == BX_DOL_STUDIO_INSTALLER_SUCCESS) || (isset($mixedResult['code']) && (int)$mixedResult['code'] == BX_DOL_STUDIO_INSTALLER_SUCCESS)) {
  811. $sMessage .= $this->_displayResult($sAction, true, isset($mixedResult['content']) ? $mixedResult['content'] : '', $bHtmlResponce);
  812. continue;
  813. }
  814. //--- On Failed ---//
  815. $sMethodFailed = $sMethod . 'Failed';
  816. return array(
  817. 'code' => BX_DOL_STUDIO_IU_RCE_SUBACTION_FAILED,
  818. 'message' => $this->_displayResult($sAction, false, method_exists($this, $sMethodFailed) ? $this->$sMethodFailed($mixedResult) : $this->actionOperationFailed($mixedResult), $bHtmlResponce),
  819. 'result' => false
  820. );
  821. }
  822. if($this->_bShowOnSuccess)
  823. $sMessage = $this->_aActions['perform_' . $sOperationName]['success'] . $sMessage;
  824. return array('message' => $sMessage, 'result' => true);
  825. }
  826. protected function _displayResult($sAction, $bResult, $sResult = '', $bHtmlResponse = true)
  827. {
  828. if($bResult && !in_array($sAction, array('show_introduction', 'show_conclusion')) && !$this->_bShowOnSuccess)
  829. return '';
  830. $sTitle = $this->_aActions[$sAction]['title'] . ' ';
  831. if(!empty($sResult))
  832. $sResult = (substr($sResult, 0, 1) == '_' ? _t($sResult) : $sResult);
  833. $sContent = !empty($sResult) ? $sResult : _t($bResult ? '_adm_txt_modules_process_action_success' : '_adm_err_modules_process_action_failed');
  834. if(!$bHtmlResponse)
  835. return $sTitle . $sContent;
  836. return BxDolStudioTemplate::getInstance()->parseHtmlByName('mod_action_result_step.html', array(
  837. 'color' => $bResult ? 'green' : 'red',
  838. 'title' => $sTitle,
  839. 'content' => $sContent
  840. ));
  841. }
  842. protected function _getFileManager()
  843. {
  844. $oFile = null;
  845. if($this->_bUseFtp) {
  846. $oFile = new BxDolFtp(isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'localhost', getParam('sys_ftp_login'), getParam('sys_ftp_password'), getParam('sys_ftp_dir'));
  847. if(!$oFile->connect())
  848. return null;
  849. if(!$oFile->isUna())
  850. return null;
  851. }
  852. else
  853. $oFile = BxDolFile::getInstance();
  854. return $oFile;
  855. }
  856. protected function _isCompatibleWith()
  857. {
  858. $sVersionCur = bx_get_ver();
  859. $bCompatible = false;
  860. if(isset($this->_aConfig['compatible_with']) && is_array($this->_aConfig['compatible_with']))
  861. foreach($this->_aConfig['compatible_with'] as $iKey => $sVersionReq)
  862. $bCompatible = $bCompatible || (version_compare(strtolower($sVersionCur), strtolower($sVersionReq), '>=') == 1);
  863. return $bCompatible;
  864. }
  865. protected function filePathWithoutBase($sPath)
  866. {
  867. return bx_ltrim_str($sPath, $this->_sModulePath);
  868. }
  869. protected function getMarkersForDb()
  870. {
  871. return array(
  872. 'from' => array('{db_name}'),
  873. 'to' => array(defined('BX_DATABASE_NAME') ? BX_DATABASE_NAME : ''),
  874. );
  875. }
  876. protected function cleanupMemoryAfterAction($sModuleUri, $iModuleId)
  877. {
  878. $this->oDb->cleanMemory('sys_modules_' . $sModuleUri);
  879. $this->oDb->cleanMemory('sys_modules_' . $iModuleId);
  880. $this->oDb->cleanMemory('sys_modules');
  881. $this->oDb->cleanMemory('sys_modules_modules');
  882. $this->oDb->cleanMemory('sys_modules_modules_active');
  883. }
  884. }
  885. /** @} */