BxDolStudioInstaller.php 39 KB

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