1
0

BxDolStudioInstaller.php 36 KB

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