BxDolStudioInstaller.php 39 KB

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