BxDolStudioInstaller.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup TridentStudio Trident 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. 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. 'process_connections' => array(
  96. 'title' => _t('_adm_txt_modules_process_connections'),
  97. ),
  98. 'process_deleted_profiles' => array(
  99. 'title' => _t('_adm_txt_modules_process_deleted_profiles'),
  100. ),
  101. 'process_menu_triggers' => array(
  102. 'title' => _t('_adm_txt_modules_process_menu_triggers'),
  103. ),
  104. 'process_page_triggers' => array(
  105. 'title' => _t('_adm_txt_modules_process_page_triggers'),
  106. ),
  107. 'process_storages' => array(
  108. 'title' => _t('_adm_txt_modules_process_storages'),
  109. ),
  110. 'register_transcoders' => array(
  111. 'title' => _t('_adm_txt_modules_register_transcoders'),
  112. ),
  113. 'unregister_transcoders' => array(
  114. 'title' => _t('_adm_txt_modules_unregister_transcoders'),
  115. ),
  116. 'clear_db_cache' => array(
  117. 'title' => _t('_adm_txt_modules_clear_db_cache'),
  118. ),
  119. 'show_conclusion' => array(
  120. 'title' => _t('_adm_txt_modules_show_conclusion'),
  121. ),
  122. );
  123. $this->_aNonHashableFiles = array(
  124. 'install',
  125. 'updates'
  126. );
  127. }
  128. public function install($aParams, $bAutoEnable = false)
  129. {
  130. $bAutoEnable = $bAutoEnable || (isset($aParams['auto_enable']) && (bool)$aParams['auto_enable']);
  131. $bHtmlResponce = isset($aParams['html_response']) && (bool)$aParams['html_response'];
  132. //--- Check whether the module was already installed ---//
  133. if($this->oDb->isModule($this->_aConfig['home_uri']))
  134. return array(
  135. 'message' => _t('_adm_err_modules_already_installed'),
  136. 'result' => false
  137. );
  138. $aResult = array();
  139. bx_alert('system', 'before_install', 0, false, array ('config' => $this->_aConfig, 'result' => &$aResult));
  140. if ($aResult && !$aResult['result'])
  141. return $aResult;
  142. //--- Check mandatory settings ---//
  143. if($this->oDb->isModuleParamsUsed($this->_aConfig['home_uri'], $this->_aConfig['home_dir'], $this->_aConfig['db_prefix'], $this->_aConfig['class_prefix']))
  144. return array(
  145. 'message' => _t('_adm_txt_modules_params_used'),
  146. 'result' => false
  147. );
  148. //--- Check version compatibility ---//
  149. if(!$this->_isCompatibleWith())
  150. return array(
  151. 'message' => $this->_displayResult('check_script_version', false, '_adm_err_modules_wrong_version_script', $bHtmlResponce),
  152. 'result' => false
  153. );
  154. //--- Check for available translations ---//
  155. $oFile = $this->_getFileManager();
  156. $sModuleUri = $this->_aConfig['home_uri'];
  157. $aLanguages = $this->oDb->getModulesBy(array('type' => 'languages'));
  158. foreach($aLanguages as $aLanguage) {
  159. if($aLanguage['uri'] == 'en')
  160. continue;
  161. $aLanguageConfig = self::getModuleConfig(BX_DIRECTORY_PATH_MODULES . $aLanguage['path'] . '/install/config.php');
  162. if(empty($aLanguageConfig))
  163. continue;
  164. if(!isset($aLanguageConfig['includes'][$sModuleUri]) || empty($aLanguageConfig['includes'][$sModuleUri]))
  165. continue;
  166. $sSrcPath = 'modules/' . $aLanguage['path'] . 'install/data/' . $aLanguageConfig['includes'][$sModuleUri];
  167. $sDstPath = $aLanguageConfig['includes'][$sModuleUri];
  168. $oFile->copy($sSrcPath, $sDstPath);
  169. }
  170. //--- Check actions ---//
  171. $aResult = $this->_perform('install', $aParams);
  172. if($aResult['result']) {
  173. $sDependencies = "";
  174. if(isset($this->_aConfig['install']['check_dependencies']) && (int)$this->_aConfig['install']['check_dependencies'] == 1 && isset($this->_aConfig['dependencies']) && is_array($this->_aConfig['dependencies']))
  175. $this->_aConfig['dependencies'] = array_keys($this->_aConfig['dependencies']);
  176. $iModuleId = $this->oDb->insertModule($this->_aConfig);
  177. $sTitleKey = BxDolModule::getTitleKey($this->_aConfig['home_uri']);
  178. $oLanguages = BxDolStudioLanguagesUtils::getInstance();
  179. $oLanguages->addLanguageString($sTitleKey, $this->_aConfig['title']);
  180. $oLanguages->compileLanguage();
  181. BxDolStudioInstallerUtils::getInstance()->checkModules();
  182. $aFiles = array();
  183. $this->hashFiles($this->_sModulePath, $aFiles);
  184. foreach($aFiles as $aFile)
  185. $this->oDb->insertModuleTrack($iModuleId, $aFile);
  186. $this->cleanupMemoryAfterAction($this->_aConfig['home_uri'], $iModuleId);
  187. if(!empty($this->_aConfig['install_success']))
  188. $this->_perform('install_success', $aParams);
  189. }
  190. else {
  191. if(!empty($this->_aConfig['install_failed']))
  192. $this->_perform('install_failed', $aParams);
  193. }
  194. bx_alert('system', 'install', 0, false, array ('config' => $this->_aConfig, 'result' => &$aResult));
  195. if($aResult['result'] && $bAutoEnable) {
  196. $aResultEnable = $this->enable($aParams);
  197. $aResult['result'] = $aResult['result'] & $aResultEnable['result'];
  198. $aResult['message'] = $aResult['message'] . $aResultEnable['message'];
  199. }
  200. return $aResult;
  201. }
  202. public function uninstall($aParams, $bAutoDisable = false)
  203. {
  204. $bAutoDisable = $bAutoDisable || (isset($aParams['auto_disable']) && (bool)$aParams['auto_disable']);
  205. $bHtmlResponce = isset($aParams['html_response']) && (bool)$aParams['html_response'];
  206. //--- Check whether the module was already uninstalled ---//
  207. if(!$this->oDb->isModule($this->_aConfig['home_uri']))
  208. return array(
  209. 'message' => _t('_adm_err_modules_already_uninstalled'),
  210. 'result' => false
  211. );
  212. if($bAutoDisable) {
  213. $aResultDisable = $this->disable($aParams);
  214. if(!$aResultDisable['result'])
  215. return $aResultDisable;
  216. }
  217. $aResult = array();
  218. bx_alert('system', 'before_uninstall', 0, false, array ('config' => $this->_aConfig, 'result' => &$aResult));
  219. if ($aResult && !$aResult['result'])
  220. return $aResult;
  221. //--- Check for dependent modules ---//
  222. $bDependent = false;
  223. $aDependents = $this->oDb->getDependent($this->_aConfig['home_uri']);
  224. if(is_array($aDependents) && !empty($aDependents)) {
  225. $bDependent = true;
  226. $sMessage = '<br />' . _t('_adm_err_modules_wrong_dependency_uninstall') . '<br />';
  227. foreach($aDependents as $aDependent)
  228. $sMessage .= $aDependent['title'] . '<br />';
  229. }
  230. if($bDependent)
  231. return array(
  232. 'message' => $this->_displayResult('check_dependencies', false, $sMessage, $bHtmlResponce),
  233. 'result' => false
  234. );
  235. $aResult = $this->_perform('uninstall', $aParams);
  236. if($aResult['result']) {
  237. $iModuleId = $this->oDb->deleteModule($this->_aConfig);
  238. $sTitleKey = BxDolModule::getTitleKey($this->_aConfig['home_uri']);
  239. $oLanguages = BxDolStudioLanguagesUtils::getInstance();
  240. $oLanguages->deleteLanguageString($sTitleKey);
  241. $oLanguages->compileLanguage();
  242. $this->cleanupMemoryAfterAction($this->_aConfig['home_uri'], $iModuleId);
  243. if(!empty($this->_aConfig['uninstall_success']))
  244. $this->_perform('uninstall_success', $aParams);
  245. }
  246. else {
  247. if(!empty($this->_aConfig['uninstall_failed']))
  248. $this->_perform('uninstall_failed', $aParams);
  249. }
  250. if($bAutoDisable) {
  251. $aResult['result'] = $aResultDisable['result'] & $aResult['result'];
  252. $aResult['message'] = $aResultDisable['message'] . $aResult['message'];
  253. }
  254. bx_alert('system', 'uninstall', 0, false, array ('config' => $this->_aConfig, 'result' => &$aResult));
  255. return $aResult;
  256. }
  257. public function delete($aParams)
  258. {
  259. $aError = array(
  260. 'message' => _t('_adm_err_modules_cannot_remove_package'),
  261. 'result' => false
  262. );
  263. $oFile = $this->_getFileManager();
  264. if(empty($oFile))
  265. return $aError;
  266. if(!$oFile->delete('modules/' . $this->_aConfig['home_dir']))
  267. return $aError;
  268. return array(
  269. 'message' => '', //_t('_adm_msg_modules_success_delete'),
  270. 'result' => true
  271. );
  272. }
  273. public function recompile($aParams)
  274. {
  275. $oLanguages = BxDolStudioLanguagesUtils::getInstance();
  276. $aResult = array('message' => '', 'result' => false);
  277. $aLanguages = $this->oDb->getLanguages();
  278. if (isAdmin() && !empty($aLanguages)) {
  279. $this->_updateLanguage(false, current($aLanguages));
  280. $bResult = false;
  281. foreach($aLanguages as $aLanguage) {
  282. $bResult = $this->_updateLanguage(true, $aLanguage) && $oLanguages->compileLanguage($aLanguage['id']);
  283. $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 />';
  284. $aResult['result'] |= $bResult;
  285. }
  286. }
  287. return $aResult;
  288. }
  289. public function enable($aParams)
  290. {
  291. $aModule = $this->oDb->getModuleByUri($this->_aConfig['home_uri']);
  292. //--- Check whether the module is installed ---//
  293. if(empty($aModule) || !is_array($aModule))
  294. return array(
  295. 'message' => _t('_adm_err_modules_module_not_installed'),
  296. 'result' => false
  297. );
  298. //--- Check whether the module is already enabled ---//
  299. if((int)$aModule['enabled'] != 0)
  300. return array(
  301. 'message' => _t('_adm_err_modules_already_enabled'),
  302. 'result' => false
  303. );
  304. $aResult = array();
  305. bx_alert('system', 'before_enable', 0, false, array ('config' => $this->_aConfig, 'result' => &$aResult));
  306. if ($aResult && !$aResult['result'])
  307. return $aResult;
  308. $aResult = $this->_perform('enable', $aParams);
  309. if($aResult['result']) {
  310. $this->oDb->enableModuleByUri($aModule['uri']);
  311. $this->cleanupMemoryAfterAction($aModule['uri'], $aModule['id']);
  312. if(!empty($this->_aConfig['enable_success']))
  313. $this->_perform('enable_success', $aParams);
  314. }
  315. else {
  316. if(!empty($this->_aConfig['enable_failed']))
  317. $this->_perform('enable_failed', $aParams);
  318. }
  319. bx_alert('system', 'enable', 0, false, array ('config' => $this->_aConfig, 'result' => &$aResult));
  320. return $aResult;
  321. }
  322. public function disable($aParams)
  323. {
  324. $bHtmlResponce = isset($aParams['html_response']) && (bool)$aParams['html_response'];
  325. $aModule = $this->oDb->getModuleByUri($this->_aConfig['home_uri']);
  326. //--- Check whether the module is installed ---//
  327. if(empty($aModule) || !is_array($aModule))
  328. return array(
  329. 'message' => _t('_adm_err_modules_module_not_installed'),
  330. 'result' => false
  331. );
  332. //--- Check whether the module is already disabled ---//
  333. if((int)$aModule['enabled'] == 0)
  334. return array(
  335. 'message' => _t('_adm_err_modules_already_disabled'),
  336. 'result' => false
  337. );
  338. $aResult = array();
  339. bx_alert('system', 'before_disable', 0, false, array ('config' => $this->_aConfig, 'result' => &$aResult));
  340. if ($aResult && !$aResult['result'])
  341. return $aResult;
  342. //--- Check for dependent modules ---//
  343. $bDependent = false;
  344. $aDependents = $this->oDb->getDependent($this->_aConfig['home_uri']);
  345. if(is_array($aDependents) && !empty($aDependents)) {
  346. $bDependent = true;
  347. $sMessage = '<br />' . _t('_adm_err_modules_wrong_dependency_disable') . '<br />';
  348. foreach($aDependents as $aDependent)
  349. $sMessage .= $aDependent['title'] . '<br />';
  350. }
  351. if($bDependent)
  352. return array(
  353. 'message' => $this->_displayResult('check_dependencies', false, $sMessage, $bHtmlResponce),
  354. 'result' => false
  355. );
  356. $aResult = $this->_perform('disable', $aParams);
  357. if($aResult['result']) {
  358. $this->oDb->disableModuleByUri($aModule['uri']);
  359. $this->cleanupMemoryAfterAction($aModule['uri'], $aModule['id']);
  360. if(!empty($this->_aConfig['disable_success']))
  361. $this->_perform('disable_success', $aParams);
  362. }
  363. else {
  364. if(!empty($this->_aConfig['disable_failed']))
  365. $this->_perform('disable_failed', $aParams);
  366. }
  367. bx_alert('system', 'disable', 0, false, array ('config' => $this->_aConfig, 'result' => &$aResult));
  368. return $aResult;
  369. }
  370. //--- Get/Set Methods ---//
  371. public function getVendor()
  372. {
  373. return $this->_aConfig['vendor'];
  374. }
  375. public function getName()
  376. {
  377. return $this->_aConfig['name'];
  378. }
  379. public function getTitle()
  380. {
  381. return $this->_aConfig['title'];
  382. }
  383. public function getHomeDir()
  384. {
  385. return $this->_aConfig['home_dir'];
  386. }
  387. //--- Action Methods ---//
  388. protected function actionOperationFailed($mixedResult)
  389. {
  390. return _t('_adm_err_modules_process_action_failed');
  391. }
  392. protected function actionCheckDependencies($sOperation)
  393. {
  394. $sContent = '';
  395. if(in_array($sOperation, array('install', 'enable', 'update'))) {
  396. if(!isset($this->_aConfig['dependencies']) || !is_array($this->_aConfig['dependencies']))
  397. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  398. foreach($this->_aConfig['dependencies'] as $sModuleUri => $sModuleTitle)
  399. if(!$this->oDb->isModule($sModuleUri))
  400. $sContent .= $sModuleTitle . '<br />';
  401. if(!empty($sContent))
  402. $sContent = '<br />' . _t('_adm_err_modules_wrong_dependency_install') . '<br />' . $sContent;
  403. }
  404. return empty($sContent) ? BX_DOL_STUDIO_INSTALLER_SUCCESS : array('code' => BX_DOL_STUDIO_INSTALLER_FAILED, 'content' => $sContent);
  405. }
  406. protected function actionCheckDependenciesFailed($mixedResult)
  407. {
  408. return $mixedResult['content'];
  409. }
  410. protected function actionShowIntroduction($sOperation)
  411. {
  412. if(!isset($this->_aConfig[$sOperation . '_info']['introduction']))
  413. return BX_DOL_STUDIO_INSTALLER_FAILED;
  414. $sPath = $this->_sHomePath . 'install/info/' . $this->_aConfig[$sOperation . '_info']['introduction'];
  415. return file_exists($sPath) ? array("code" => BX_DOL_STUDIO_INSTALLER_SUCCESS, "content" => file_get_contents($sPath)) : BX_DOL_STUDIO_INSTALLER_FAILED;
  416. }
  417. protected function actionShowConclusion($sOperation)
  418. {
  419. if(!isset($this->_aConfig[$sOperation . '_info']['conclusion']))
  420. return BX_DOL_STUDIO_INSTALLER_FAILED;
  421. $sPath = $this->_sHomePath . 'install/info/' . $this->_aConfig[$sOperation . '_info']['conclusion'];
  422. return file_exists($sPath) ? array("code" => BX_DOL_STUDIO_INSTALLER_SUCCESS, "content" => file_get_contents($sPath)) : BX_DOL_STUDIO_INSTALLER_FAILED;
  423. }
  424. protected function actionMoveSources($sOperation)
  425. {
  426. $oFile = $this->_getFileManager();
  427. $aInstalled = array_merge(array('system'), $this->oDb->getModulesUri());
  428. $bResult = true;
  429. foreach($this->_aConfig['includes'] as $sUri => $sPath) {
  430. if(!in_array($sUri, $aInstalled) || empty($sPath))
  431. continue;
  432. if($sOperation == 'install') {
  433. $sSrcPath = 'modules/' . $this->_aConfig['home_dir'] . 'install/data/' . $sPath;
  434. $sDstPath = $sPath;
  435. $bResult &= $oFile->copy($sSrcPath, $sDstPath);
  436. } else if($sOperation == 'uninstall')
  437. $bResult &= $oFile->delete($sPath);
  438. }
  439. return $bResult ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  440. }
  441. protected function actionExecuteSql($sOperation)
  442. {
  443. switch($sOperation) {
  444. case 'install':
  445. $this->actionExecuteSql('disable');
  446. $this->actionExecuteSql('uninstall');
  447. break;
  448. case 'enable':
  449. $this->actionExecuteSql('disable');
  450. break;
  451. }
  452. $mixedResult = $this->oDb->executeSQL($this->_sHomePath . 'install/sql/' . $sOperation . '.sql', $this->getMarkersForDb());
  453. return $mixedResult === true ? BX_DOL_STUDIO_INSTALLER_SUCCESS : array('code' => BX_DOL_STUDIO_INSTALLER_FAILED, 'content' => $mixedResult);
  454. }
  455. protected function actionExecuteSqlFailed($mixedResult)
  456. {
  457. if(is_int($mixedResult))
  458. return $this->actionOperationFailed($mixedResult);
  459. $sResult = '<br />' . _t('_adm_err_modules_wrong_mysql_query') . '<br />';
  460. foreach($mixedResult['content'] as $aQuery) {
  461. $sResult .= _t('_adm_err_modules_wrong_mysql_query_msg', $aQuery['error']) . '<br />';
  462. $sResult .= '<pre>' . $aQuery['query'] . '</pre>';
  463. }
  464. return $sResult;
  465. }
  466. protected function actionInstallLanguage($sOperation)
  467. {
  468. $oLanguages = BxDolStudioLanguagesUtils::getInstance();
  469. $sLanguage = isset($this->_aConfig['home_uri']) ? $this->_aConfig['home_uri'] : '';
  470. $bResult = true;
  471. if($sOperation == 'install')
  472. $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);
  473. return $bResult && $oLanguages->compileLanguage(0, true) ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  474. }
  475. protected function actionUpdateLanguages($sOperation)
  476. {
  477. if(!isset($this->_aConfig['language_category']) || empty($this->_aConfig['language_category']))
  478. return BX_DOL_STUDIO_INSTALLER_FAILED;
  479. $oLanguages = BxDolStudioLanguagesUtils::getInstance();
  480. $bResult = true;
  481. $aResult = array();
  482. //--- Process Language Category ---//
  483. if($sOperation == 'install')
  484. $iCategoryId = $oLanguages->addLanguageCategory($this->_aConfig['language_category']);
  485. //--- Process languages' key=>value pears ---//
  486. $aModule = array(
  487. 'name' => $this->_aConfig['name'],
  488. 'path' => $this->_aConfig['home_dir'],
  489. 'uri' => $this->_aConfig['home_uri'],
  490. 'lang_category' => $this->_aConfig['language_category']
  491. );
  492. if($sOperation == 'install')
  493. $bResult = $oLanguages->restoreLanguage(0, $aModule, false);
  494. else if($sOperation == 'uninstall')
  495. $bResult = $oLanguages->removeLanguageByModule($aModule, false);
  496. if($sOperation == 'uninstall' && $bResult)
  497. $oLanguages->deleteLanguageCategory($this->_aConfig['language_category']);
  498. return $bResult && $oLanguages->compileLanguage(0, true) ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  499. }
  500. protected function actionUpdateRelations($sOperation)
  501. {
  502. if(!in_array($sOperation, array('install', 'uninstall', 'enable', 'disable')))
  503. return BX_DOL_STUDIO_INSTALLER_FAILED;
  504. if(empty($this->_aConfig['relations']) || !is_array($this->_aConfig['relations']))
  505. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  506. foreach($this->_aConfig['relations'] as $sModule) {
  507. if(!$this->oDb->isModuleByName($sModule))
  508. continue;
  509. $aRelation = $this->oDb->getRelationsBy(array('type' => 'module', 'value' => $sModule));
  510. if(empty($aRelation) || empty($aRelation['on_' . $sOperation]) || !BxDolRequest::serviceExists($aRelation['module'], $aRelation['on_' . $sOperation]))
  511. continue;
  512. BxDolService::call($aRelation['module'], $aRelation['on_' . $sOperation], array($this->_aConfig['home_uri']));
  513. }
  514. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  515. }
  516. /**
  517. *
  518. * Connections objects associated with module data. It must be defined which content is associated with the connection.
  519. * The key is connection object name and value is array (possible array values: type, conn, table, field_id).
  520. * If 'type' == 'profiles', then it is considered profiles connection and other possible param is 'conn' ('initiator', 'content' or 'both')
  521. * If 'type' == 'custom' (or ommited), then other possible params are 'conn', 'table' and 'field_id'
  522. * @param string $sOperation - operation type.
  523. */
  524. protected function actionProcessConnections($sOperation)
  525. {
  526. if(!in_array($sOperation, array('uninstall')) || empty($this->_aConfig['connections']))
  527. return BX_DOL_STUDIO_INSTALLER_FAILED;
  528. foreach($this->_aConfig['connections'] as $sObjectConnections => $a) {
  529. $o = BxDolConnection::getObjectInstance($sObjectConnections);
  530. if(!$o)
  531. continue;
  532. $sFuncSuffix = 'DeleteInitiatorAndContent';
  533. if (isset($a['conn']) && 'initiator' == $a['conn'])
  534. $sFuncSuffix = 'DeleteInitiator';
  535. elseif (isset($a['conn']) && 'content' == $a['conn'])
  536. $sFuncSuffix = 'DeleteContent';
  537. if (isset($a['type']) && 'profiles' == $a['type']) {
  538. $sFunc = 'onModuleProfile' . $sFuncSuffix;
  539. $o->$sFunc($this->_aConfig['name']);
  540. } else {
  541. $sFunc = 'onModule' . $sFuncSuffix;
  542. $o->$sFunc($a['table'], $a['field_id']);
  543. }
  544. }
  545. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  546. }
  547. /**
  548. *
  549. * Process the list of menu triggers provided in config array.
  550. * It must be specified in the module which adds menu item and in modules where menu items are added, @see BxDolMenu::processMenuTrigger
  551. * @param string $sOperation - operation type.
  552. */
  553. protected function actionProcessMenuTriggers($sOperation)
  554. {
  555. if(empty($this->_aConfig['menu_triggers']))
  556. return BX_DOL_STUDIO_INSTALLER_FAILED;
  557. foreach($this->_aConfig['menu_triggers'] as $sMenuTriggerName)
  558. BxDolMenu::processMenuTrigger($sMenuTriggerName);
  559. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  560. }
  561. /**
  562. *
  563. * Process the list of page triggers provided in config array.
  564. * It must be specified in the module which adds page block and in modules where page blocks are added, @see BxDolPage::processPageTrigger
  565. * @param string $sOperation - operation type.
  566. */
  567. protected function actionProcessPageTriggers($sOperation)
  568. {
  569. if(empty($this->_aConfig['page_triggers']))
  570. return BX_DOL_STUDIO_INSTALLER_FAILED;
  571. foreach($this->_aConfig['page_triggers'] as $sPageTriggerName)
  572. BxDolPage::processPageTrigger($sPageTriggerName);
  573. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  574. }
  575. protected function actionProcessDeletedProfiles($sOperation)
  576. {
  577. if(!in_array($sOperation, array('install', 'uninstall', 'enable', 'disable')))
  578. return BX_DOL_STUDIO_INSTALLER_FAILED;
  579. $o = BxDolProfileQuery::getInstance();
  580. $o->processDeletedProfiles();
  581. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  582. }
  583. /**
  584. *
  585. * Storage objects to automatically delete files from upon module uninstallation.
  586. * Note. Don't add storage objects used in transcoder objects.
  587. * @param string $sOperation - operation type.
  588. */
  589. protected function actionProcessStorages($sOperation)
  590. {
  591. if(empty($this->_aConfig['storages']))
  592. return BX_DOL_STUDIO_INSTALLER_FAILED;
  593. // check if module is already waiting while files are deleting
  594. if(self::isModulePendingUninstall($this->_aConfig['home_uri']))
  595. return array(
  596. 'code' => BX_DOL_STUDIO_INSTALLER_FAILED,
  597. 'content' => _t('_adm_err_modules_pending_uninstall_already'),
  598. );
  599. $bSetModulePendingUninstall = false;
  600. // queue for deletion storage files
  601. foreach($this->_aConfig['storages'] as $s)
  602. if(($o = BxDolStorage::getObjectInstance($s)) && $o->queueFilesForDeletionFromObject())
  603. $bSetModulePendingUninstall = true;
  604. // delete comments and queue for deletion comments attachments
  605. $iFiles = 0;
  606. BxDolCmts::onModuleUninstall($this->_aConfig['name'], $iFiles);
  607. if($iFiles)
  608. $bSetModulePendingUninstall = true;
  609. // if some files were added to the queue, set module as pending uninstall
  610. if ($bSetModulePendingUninstall) {
  611. self::setModulePendingUninstall($this->_aConfig['home_uri']);
  612. return array(
  613. 'code' => BX_DOL_STUDIO_INSTALLER_FAILED,
  614. 'content' => _t('_adm_err_modules_pending_uninstall'),
  615. );
  616. }
  617. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  618. }
  619. protected function actionProcessStoragesFailed($mixedResult)
  620. {
  621. if(is_int($mixedResult))
  622. return $this->actionOperationFailed($mixedResult);
  623. return $mixedResult['content'];
  624. }
  625. /**
  626. *
  627. * Process the list of transcoders provided in config array.
  628. * Transcoder objects to automatically register/unregister necessary alerts for.
  629. * @param string $sOperation - operation type.
  630. */
  631. protected function actionRegisterTranscoders($sOperation)
  632. {
  633. if(empty($this->_aConfig['transcoders']))
  634. return BX_DOL_STUDIO_INSTALLER_FAILED;
  635. BxDolTranscoderImage::registerHandlersArray($this->_aConfig['transcoders']);
  636. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  637. }
  638. /**
  639. *
  640. * Process the list of transcoders provided in config array.
  641. * Transcoder objects to automatically register/unregister necessary alerts for.
  642. * @param string $sOperation - operation type.
  643. */
  644. protected function actionUnregisterTranscoders($sOperation)
  645. {
  646. if(empty($this->_aConfig['transcoders']))
  647. return BX_DOL_STUDIO_INSTALLER_FAILED;
  648. BxDolTranscoderImage::unregisterHandlersArray($this->_aConfig['transcoders']);
  649. BxDolTranscoderImage::cleanupObjectsArray($this->_aConfig['transcoders']);
  650. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  651. }
  652. protected function actionClearDbCache($sOperation)
  653. {
  654. $oCache = $this->oDb->getDbCacheObject();
  655. $bResult = $oCache->removeAllByPrefix('db_');
  656. return $bResult ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  657. }
  658. protected function _perform($sOperationName, $aParams = array())
  659. {
  660. $bHtmlResponce = isset($aParams['html_response']) && (bool)$aParams['html_response'];
  661. if(!defined('BX_SKIP_INSTALL_CHECK') && !defined('BX_DOL_CRON_EXECUTE') && !$GLOBALS['logged']['admin'])
  662. return array('message' => '_adm_mod_err_only_admin_can_perform_operations_with_modules', 'result' => false);
  663. $sMessage = '';
  664. foreach($this->_aConfig[$sOperationName] as $sAction => $iEnabled) {
  665. $sMethod = 'action' . bx_gen_method_name($sAction);
  666. if($iEnabled == 0 || !method_exists($this, $sMethod))
  667. continue;
  668. $mixedResult = $this->$sMethod($sOperationName);
  669. //--- On Success ---//
  670. if((is_int($mixedResult) && (int)$mixedResult == BX_DOL_STUDIO_INSTALLER_SUCCESS) || (isset($mixedResult['code']) && (int)$mixedResult['code'] == BX_DOL_STUDIO_INSTALLER_SUCCESS)) {
  671. $sMessage .= $this->_displayResult($sAction, true, isset($mixedResult['content']) ? $mixedResult['content'] : '', $bHtmlResponce);
  672. continue;
  673. }
  674. //--- On Failed ---//
  675. $sMethodFailed = $sMethod . 'Failed';
  676. return array('message' => $this->_displayResult($sAction, false, method_exists($this, $sMethodFailed) ? $this->$sMethodFailed($mixedResult) : $this->actionOperationFailed($mixedResult), $bHtmlResponce), 'result' => false);
  677. }
  678. if($this->_bShowOnSuccess)
  679. $sMessage = $this->_aActions['perform_' . $sOperationName]['success'] . $sMessage;
  680. return array('message' => $sMessage, 'result' => true);
  681. }
  682. protected function _displayResult($sAction, $bResult, $sResult = '', $bHtmlResponse = true)
  683. {
  684. if($bResult && !in_array($sAction, array('show_introduction', 'show_conclusion')) && !$this->_bShowOnSuccess)
  685. return '';
  686. $sTitle = $this->_aActions[$sAction]['title'] . ' ';
  687. if(!empty($sResult))
  688. $sResult = (substr($sResult, 0, 1) == '_' ? _t($sResult) : $sResult);
  689. $sContent = !empty($sResult) ? $sResult : _t($bResult ? '_adm_txt_modules_process_action_success' : '_adm_err_modules_process_action_failed');
  690. if(!$bHtmlResponse)
  691. return $sTitle . $sContent;
  692. return BxDolStudioTemplate::getInstance()->parseHtmlByName('mod_action_result_step.html', array(
  693. 'color' => $bResult ? 'green' : 'red',
  694. 'title' => $sTitle,
  695. 'content' => $sContent
  696. ));
  697. }
  698. protected function _getFileManager()
  699. {
  700. $oFile = null;
  701. if($this->_bUseFtp) {
  702. $oFile = new BxDolFtp(isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'localhost', getParam('sys_ftp_login'), getParam('sys_ftp_password'), getParam('sys_ftp_dir'));
  703. if(!$oFile->connect())
  704. return null;
  705. if(!$oFile->isTrident())
  706. return null;
  707. }
  708. else
  709. $oFile = BxDolFile::getInstance();
  710. return $oFile;
  711. }
  712. protected function _isCompatibleWith()
  713. {
  714. $sVersionCur = bx_get_ver();
  715. $bCompatible = false;
  716. if(isset($this->_aConfig['compatible_with']) && is_array($this->_aConfig['compatible_with']))
  717. foreach($this->_aConfig['compatible_with'] as $iKey => $sVersionReq)
  718. $bCompatible = $bCompatible || (version_compare($sVersionCur, $sVersionReq, '>=') == 1);
  719. return $bCompatible;
  720. }
  721. protected function filePathWithoutBase($sPath)
  722. {
  723. return bx_ltrim_str($sPath, $this->_sModulePath);
  724. }
  725. protected function getMarkersForDb()
  726. {
  727. return array(
  728. 'from' => array('{db_name}'),
  729. 'to' => array(defined('BX_DATABASE_NAME') ? BX_DATABASE_NAME : ''),
  730. );
  731. }
  732. protected function cleanupMemoryAfterAction($sModuleUri, $iModuleId)
  733. {
  734. $this->oDb->cleanMemory('sys_modules_' . $sModuleUri);
  735. $this->oDb->cleanMemory('sys_modules_' . $iModuleId);
  736. $this->oDb->cleanMemory('sys_modules');
  737. $this->oDb->cleanMemory('sys_modules_modules');
  738. $this->oDb->cleanMemory('sys_modules_modules_active');
  739. }
  740. }
  741. /** @} */