BxDolStudioInstaller.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  1. <?php
  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 DolphinStudio Dolphin Studio
  7. * @{
  8. */
  9. defined('BX_DOL') or die('hack attempt');
  10. bx_import('BxDolInstallerUtils');
  11. bx_import('BxDolStudioInstallerQuery');
  12. define("BX_DOL_STUDIO_INSTALLER_SUCCESS", 0);
  13. define("BX_DOL_STUDIO_INSTALLER_FAILED", 1);
  14. /**
  15. * Base class for Installer classes in modules engine.
  16. *
  17. * The class contains different check functions which are used during the installation process.
  18. * An object of the class is created automatically with Dolphin's modules installer.
  19. * Installation/Uninstallation process can be controlled with config.php file located in [module]/install/ folder.
  20. *
  21. *
  22. * Example of usage:
  23. * @see any module included in the default Dolphin's package.
  24. *
  25. *
  26. * Memberships/ACL:
  27. * Doesn't depend on user's membership.
  28. *
  29. *
  30. * Alerts:
  31. * no alerts available
  32. *
  33. */
  34. class BxDolStudioInstaller extends BxDolInstallerUtils {
  35. var $oDb;
  36. var $_aConfig;
  37. var $_sBasePath;
  38. var $_sHomePath;
  39. var $_sModulePath;
  40. var $_aActions;
  41. var $_aNonHashable;
  42. var $_bShowOnSuccess = false;
  43. function BxDolStudioInstaller($aConfig) {
  44. parent::__construct();
  45. $this->oDb = new BxDolStudioInstallerQuery();
  46. $this->_aConfig = $aConfig;
  47. $this->_sBasePath = BX_DIRECTORY_PATH_MODULES;
  48. $this->_sHomePath = $this->_sBasePath . $aConfig['home_dir'];
  49. $this->_sModulePath = $this->_sBasePath . $aConfig['home_dir'];
  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. 'check_permissions' => array(
  81. 'title' => _t('_adm_txt_modules_check_permissions'),
  82. ),
  83. 'change_permissions' => array(
  84. 'title' => _t('_adm_txt_modules_change_permissions'),
  85. ),
  86. 'move_sources' => array(
  87. 'title' => _t('_adm_txt_modules_move_sources'),
  88. ),
  89. 'execute_sql' => array(
  90. 'title' => _t('_adm_txt_modules_execute_sql'),
  91. ),
  92. 'install_language' => array(
  93. 'title' => _t('_adm_txt_modules_install_language'),
  94. ),
  95. 'update_languages' => array(
  96. 'title' => _t('_adm_txt_modules_update_languages'),
  97. ),
  98. 'recompile_global_paramaters' => array(
  99. 'title' => _t('_adm_txt_modules_recompile_global_paramaters'),
  100. ),
  101. 'recompile_main_menu' => array(
  102. 'title' => _t('_adm_txt_modules_recompile_main_menu'),
  103. ),
  104. 'recompile_member_menu' => array(
  105. 'title' => _t('_adm_txt_modules_recompile_member_menu'),
  106. ),
  107. 'recompile_site_stats' => array(
  108. 'title' => _t('_adm_txt_modules_recompile_site_stats'),
  109. ),
  110. 'recompile_page_builder' => array(
  111. 'title' => _t('_adm_txt_modules_recompile_page_builder'),
  112. ),
  113. 'recompile_profile_fields' => array(
  114. 'title' => _t('_adm_txt_modules_recompile_profile_fields'),
  115. ),
  116. 'recompile_comments' => array(
  117. 'title' => _t('_adm_txt_modules_recompile_comments'),
  118. ),
  119. 'recompile_member_actions' => array(
  120. 'title' => _t('_adm_txt_modules_recompile_member_actions'),
  121. ),
  122. 'recompile_votes' => array(
  123. 'title' => _t('_adm_txt_modules_recompile_votes'),
  124. ),
  125. 'recompile_search' => array(
  126. 'title' => _t('_adm_txt_modules_recompile_search'),
  127. ),
  128. 'recompile_injections' => array(
  129. 'title' => _t('_adm_txt_modules_recompile_injections'),
  130. ),
  131. 'recompile_permalinks' => array(
  132. 'title' => _t('_adm_txt_modules_recompile_permalinks'),
  133. ),
  134. 'recompile_alerts' => array(
  135. 'title' => _t('_adm_txt_modules_recompile_alerts'),
  136. ),
  137. 'clear_db_cache' => array(
  138. 'title' => _t('_adm_txt_modules_clear_db_cache'),
  139. ),
  140. 'show_conclusion' => array(
  141. 'title' => _t('_adm_txt_modules_show_conclusion'),
  142. ),
  143. );
  144. $this->_aNonHashable = array(
  145. 'install',
  146. 'updates'
  147. );
  148. }
  149. public function install($aParams, $bEnable = false) {
  150. //--- Check whether the module was already installed ---//
  151. if($this->oDb->isModule($this->_aConfig['home_uri']))
  152. return array(
  153. 'message' => _t('_adm_err_modules_already_installed'),
  154. 'result' => false
  155. );
  156. //--- Check mandatory settings ---//
  157. if($this->oDb->isModuleParamsUsed($this->_aConfig['home_uri'], $this->_aConfig['home_dir'], $this->_aConfig['db_prefix'], $this->_aConfig['class_prefix']))
  158. return array(
  159. 'message' => _t('_adm_txt_modules_params_used'),
  160. 'result' => false
  161. );
  162. //--- Check version compatibility ---//
  163. $bCompatible = false;
  164. if(isset($this->_aConfig['compatible_with']) && is_array($this->_aConfig['compatible_with']))
  165. foreach($this->_aConfig['compatible_with'] as $iKey => $sVersion) {
  166. $sVersion = '/^' . str_replace(array('.', 'x'), array('\.', '[0-9]+'), $sVersion) . '$/is';
  167. $bCompatible = $bCompatible || (preg_match($sVersion, getParam('sys_version')) > 0);
  168. }
  169. if(!$bCompatible)
  170. return array(
  171. 'message' => $this->_displayResult('check_script_version', false, '_adm_err_modules_wrong_version_script'),
  172. 'result' => false
  173. );
  174. //--- Check for available translations ---//
  175. $oFile = $this->_getFileManager();
  176. $sModuleUri = $this->_aConfig['home_uri'];
  177. $aLanguages = $this->oDb->getModulesBy(array('type' => 'languages'));
  178. foreach($aLanguages as $aLanguage) {
  179. if($aLanguage['uri'] == 'en')
  180. continue;
  181. $sLanguageConfig = BX_DIRECTORY_PATH_MODULES . $aLanguage['path'] . '/install/config.php';
  182. if(!file_exists($sLanguageConfig))
  183. continue;
  184. include_once($sLanguageConfig);
  185. $aLanguageConfig = &$aConfig;
  186. if(!isset($aLanguageConfig['includes'][$sModuleUri]) || empty($aLanguageConfig['includes'][$sModuleUri]))
  187. continue;
  188. $sSrcPath = 'modules/' . $aLanguage['path'] . 'install/data/' . $aLanguageConfig['includes'][$sModuleUri];
  189. $sDstPath = $aLanguageConfig['includes'][$sModuleUri];
  190. $oFile->copy($sSrcPath, $sDstPath);
  191. }
  192. //--- Check actions ---//
  193. $aResult = $this->_perform('install');
  194. if($aResult['result']) {
  195. $sDependencies = "";
  196. if(isset($this->_aConfig['install']['check_dependencies']) && (int)$this->_aConfig['install']['check_dependencies'] == 1 && isset($this->_aConfig['dependencies']) && is_array($this->_aConfig['dependencies']))
  197. $this->_aConfig['dependencies'] = array_keys($this->_aConfig['dependencies']);
  198. $iModuleId = $this->oDb->insertModule($this->_aConfig);
  199. bx_import('BxDolModule');
  200. $sTitleKey = BxDolModule::getTitleKey($this->_aConfig['home_uri']);
  201. bx_import('BxDolStudioLanguagesUtils');
  202. $oLanguages = BxDolStudioLanguagesUtils::getInstance();
  203. $oLanguages->addLanguageString($sTitleKey, $this->_aConfig['title']);
  204. $oLanguages->compileLanguage();
  205. $aFiles = array();
  206. $this->_hash($this->_sModulePath, $aFiles);
  207. foreach($aFiles as $aFile)
  208. $this->oDb->insertModuleTrack($iModuleId, $aFile);
  209. $this->oDb->cleanMemory('sys_modules_' . $this->_aConfig['home_uri']);
  210. $this->oDb->cleanMemory('sys_modules_' . $iModuleId);
  211. $this->oDb->cleanMemory('sys_modules');
  212. }
  213. if($bEnable) {
  214. $aResultEnable = $this->enable($aParams);
  215. $aResult['result'] = $aResult['result'] & $aResultEnable['result'];
  216. $aResult['message'] = $aResult['message'] . $aResultEnable['message'];
  217. }
  218. return $aResult;
  219. }
  220. public function uninstall($aParams, $bDisable = false) {
  221. //--- Check whether the module was already uninstalled ---//
  222. if(!$this->oDb->isModule($this->_aConfig['home_uri']))
  223. return array(
  224. 'message' => _t('_adm_err_modules_already_uninstalled'),
  225. 'result' => false
  226. );
  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),
  239. 'result' => false
  240. );
  241. $aResult = $this->_perform('uninstall');
  242. if($aResult['result']) {
  243. $iModuleId = $this->oDb->deleteModule($this->_aConfig);
  244. bx_import('BxDolModule');
  245. $sTitleKey = BxDolModule::getTitleKey($this->_aConfig['home_uri']);
  246. bx_import('BxDolStudioLanguagesUtils');
  247. $oLanguages = BxDolStudioLanguagesUtils::getInstance();
  248. $oLanguages->deleteLanguageString($sTitleKey);
  249. $oLanguages->compileLanguage();
  250. $this->oDb->cleanMemory ('sys_modules_' . $this->_aConfig['home_uri']);
  251. $this->oDb->cleanMemory ('sys_modules_' . $iModuleId);
  252. $this->oDb->cleanMemory ('sys_modules');
  253. }
  254. if($bDisable) {
  255. $aResultEnable = $this->disable($aParams);
  256. $aResult['result'] = $aResult['result'] & $aResultEnable['result'];
  257. $aResult['message'] = $aResult['message'] . $aResultEnable['message'];
  258. }
  259. return $aResult;
  260. }
  261. public function delete() {
  262. bx_import('BxDolFtp');
  263. $oFtp = new BxDolFtp($_SERVER['HTTP_HOST'], getParam('sys_ftp_login'), getParam('sys_ftp_password'), getParam('sys_ftp_dir'));
  264. if(!$oFtp->connect())
  265. return array(
  266. 'message' => _t('_adm_err_modules_cannot_connect_to_ftp'),
  267. 'result' => false
  268. );
  269. if(!$oFtp->delete('modules/' . $this->_aConfig['home_dir']))
  270. return array(
  271. 'message' => _t('_adm_err_modules_cannot_remove_package'),
  272. 'result' => false
  273. );
  274. return array(
  275. 'message' => '', //_t('_adm_msg_modules_success_delete'),
  276. 'result' => true
  277. );
  278. }
  279. public function recompile($aParams) {
  280. bx_import('BxDolStudioLanguagesUtils');
  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. $aModule = $this->oDb->getModuleByUri($this->_aConfig['home_uri']);
  297. //--- Check whether the module is installed ---//
  298. if(empty($aModule) || !is_array($aModule))
  299. return array(
  300. 'message' => _t('_adm_err_modules_module_not_installed'),
  301. 'result' => false
  302. );
  303. //--- Check whether the module is already enabled ---//
  304. if((int)$aModule['enabled'] != 0)
  305. return array(
  306. 'message' => _t('_adm_err_modules_already_enabled'),
  307. 'result' => false
  308. );
  309. $aResult = $this->_perform('enable');
  310. if($aResult['result']) {
  311. $this->oDb->enableModuleByUri($aModule['uri']);
  312. $this->oDb->cleanMemory('sys_modules_' . $aModule['uri']);
  313. $this->oDb->cleanMemory('sys_modules_' . $aModule['id']);
  314. $this->oDb->cleanMemory('sys_modules');
  315. }
  316. return $aResult;
  317. }
  318. public function disable($aParams) {
  319. $aModule = $this->oDb->getModuleByUri($this->_aConfig['home_uri']);
  320. //--- Check whether the module is installed ---//
  321. if(empty($aModule) || !is_array($aModule))
  322. return array(
  323. 'message' => _t('_adm_err_modules_module_not_installed'),
  324. 'result' => false
  325. );
  326. //--- Check whether the module is already disabled ---//
  327. if((int)$aModule['enabled'] == 0)
  328. return array(
  329. 'message' => _t('_adm_err_modules_already_disabled'),
  330. 'result' => false
  331. );
  332. //--- Check for dependent modules ---//
  333. $bDependent = false;
  334. $aDependents = $this->oDb->getDependent($this->_aConfig['home_uri']);
  335. if(is_array($aDependents) && !empty($aDependents)) {
  336. $bDependent = true;
  337. $sMessage = '<br />' . _t('_adm_err_modules_wrong_dependency_disable') . '<br />';
  338. foreach($aDependents as $aDependent)
  339. $sMessage .= $aDependent['title'] . '<br />';
  340. }
  341. if($bDependent)
  342. return array(
  343. 'message' => $this->_displayResult('check_dependencies', false, $sMessage),
  344. 'result' => false
  345. );
  346. $aResult = $this->_perform('disable');
  347. if($aResult['result']) {
  348. $this->oDb->disableModuleByUri($aModule['uri']);
  349. $this->oDb->cleanMemory('sys_modules_' . $aModule['uri']);
  350. $this->oDb->cleanMemory('sys_modules_' . $aModule['id']);
  351. $this->oDb->cleanMemory('sys_modules');
  352. }
  353. return $aResult;
  354. }
  355. function _hash($sPath, &$aFiles) {
  356. if(file_exists($sPath) && is_dir($sPath) && ($rSource = opendir($sPath))) {
  357. while(($sFile = readdir($rSource)) !== false) {
  358. if($sFile == '.' || $sFile =='..' || $sFile[0] == '.' || in_array($sFile, $this->_aNonHashable))
  359. continue;
  360. if(is_dir($sPath . $sFile))
  361. $this->_hash($sPath . $sFile . '/', $aFiles);
  362. else
  363. $aFiles[] = $this->_info($sPath . $sFile);
  364. }
  365. closedir($rSource);
  366. }
  367. else
  368. $aFiles[] = $this->_info($sPath, $sFile);
  369. }
  370. function _info($sPath) {
  371. return array(
  372. 'file' => str_replace($this->_sModulePath, '', $sPath),
  373. 'hash' => md5(file_get_contents($sPath))
  374. );
  375. }
  376. function _perform($sOperationName) {
  377. if(!defined('BX_SKIP_INSTALL_CHECK') && !$GLOBALS['logged']['admin'])
  378. return array('message' => '', 'result' => false);
  379. $sMessage = '';
  380. foreach($this->_aConfig[$sOperationName] as $sAction => $iEnabled) {
  381. $sMethod = 'action' . bx_gen_method_name($sAction);
  382. if($iEnabled == 0 || !method_exists($this, $sMethod))
  383. continue;
  384. $mixedResult = $this->$sMethod($sOperationName);
  385. //--- On Success ---//
  386. if((is_int($mixedResult) && (int)$mixedResult == BX_DOL_STUDIO_INSTALLER_SUCCESS) || (isset($mixedResult['code']) && (int)$mixedResult['code'] == BX_DOL_STUDIO_INSTALLER_SUCCESS)) {
  387. $sMessage .= $this->_displayResult($sAction, true, isset($mixedResult['content']) ? $mixedResult['content'] : '');
  388. continue;
  389. }
  390. //--- On Failed ---//
  391. $sMethodFailed = $sMethod . 'Failed';
  392. return array('message' => $this->_displayResult($sAction, false, method_exists($this, $sMethodFailed) ? $this->$sMethodFailed($mixedResult) : $this->actionOperationFailed($mixedResult)), 'result' => false);
  393. }
  394. if($this->_bShowOnSuccess)
  395. $sMessage = $this->_aActions['perform_' . $sOperationName]['success'] . $sMessage;
  396. return array('message' => $sMessage, 'result' => true);
  397. }
  398. function _displayResult($sAction, $bResult, $sResult = '') {
  399. $sMessage = $this->_aActions[$sAction]['title'] . ' ';
  400. if(!empty($sResult) && substr($sResult, 0, 1) == '_')
  401. $sResult = _t($sResult) . '<br />';
  402. if(!$bResult)
  403. return $sMessage . '<span style="color:red;">' . $sResult . '</span>';
  404. if($bResult && !in_array($sAction, array('show_introduction', 'show_conclusion')) && !$this->_bShowOnSuccess)
  405. return;
  406. return $sMessage . '<span style="color:green;">' . (!empty($sResult) ? $sResult : _t('_adm_txt_modules_process_action_success') . '<br />') . '</span>';
  407. }
  408. //--- Action Methods ---//
  409. function actionOperationFailed($mixedResult) {
  410. return _t('_adm_err_modules_process_action_failed');
  411. }
  412. function actionCheckDependencies($sOperation) {
  413. $sContent = '';
  414. if(in_array($sOperation, array('install', 'enable', 'update'))) {
  415. if(!isset($this->_aConfig['dependencies']) || !is_array($this->_aConfig['dependencies']))
  416. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  417. foreach($this->_aConfig['dependencies'] as $sModuleUri => $sModuleTitle)
  418. if(!$this->oDb->isModule($sModuleUri))
  419. $sContent .= $sModuleTitle . '<br />';
  420. if(!empty($sContent))
  421. $sContent = '<br />' . _t('_adm_err_modules_wrong_dependency_install') . '<br />' . $sContent;
  422. }
  423. return empty($sContent) ? BX_DOL_STUDIO_INSTALLER_SUCCESS : array('code' => BX_DOL_STUDIO_INSTALLER_FAILED, 'content' => $sContent);
  424. }
  425. function actionCheckDependenciesFailed($mixedResult) {
  426. return $mixedResult['content'];
  427. }
  428. function actionShowIntroduction($sOperation) {
  429. if(!isset($this->_aConfig[$sOperation . '_info']['introduction']))
  430. return BX_DOL_STUDIO_INSTALLER_FAILED;
  431. $sPath = $this->_sHomePath . 'install/info/' . $this->_aConfig[$sOperation . '_info']['introduction'];
  432. return file_exists($sPath) ? array("code" => BX_DOL_STUDIO_INSTALLER_SUCCESS, "content" => "<pre>" . file_get_contents($sPath) . "</pre>") : BX_DOL_STUDIO_INSTALLER_FAILED;
  433. }
  434. function actionShowConclusion($sOperation) {
  435. if(!isset($this->_aConfig[$sOperation . '_info']['conclusion']))
  436. return BX_DOL_STUDIO_INSTALLER_FAILED;
  437. $sPath = $this->_sHomePath . 'install/info/' . $this->_aConfig[$sOperation . '_info']['conclusion'];
  438. return file_exists($sPath) ? array("code" => BX_DOL_STUDIO_INSTALLER_SUCCESS, "content" => "<pre>" . file_get_contents($sPath) . "</pre>") : BX_DOL_STUDIO_INSTALLER_FAILED;
  439. }
  440. function actionCheckPermissions($sOperation) {
  441. if(!isset($this->_aConfig[$sOperation . '_permissions']) || !is_array($this->_aConfig[$sOperation . '_permissions']))
  442. return BX_DOL_STUDIO_INSTALLER_FAILED;
  443. $aResult = array();
  444. $aPermissions = $this->_aConfig[$sOperation . '_permissions'];
  445. foreach($aPermissions as $sPermissions => $aFiles) {
  446. $sCheckFunction = 'is' . ucfirst($sPermissions);
  447. $sCptPermissions = _t('_adm_txt_modules_' . $sPermissions);
  448. foreach($aFiles as $sFile)
  449. if(!BxDolInstallerUtils::$sCheckFunction(str_replace(BX_DIRECTORY_PATH_ROOT, '', $this->_sModulePath . $sFile)))
  450. $aResult[] = array('path' => $this->_sModulePath . $sFile, 'permissions' => $sCptPermissions);
  451. }
  452. return empty($aResult) ? BX_DOL_STUDIO_INSTALLER_SUCCESS : array('code' => BX_DOL_STUDIO_INSTALLER_FAILED, 'content' => $aResult);
  453. }
  454. function actionCheckPermissionsFailed($mixedResult) {
  455. $sResult = '<br />' . _t('_adm_err_modules_wrong_permissions_check') . '<br />';
  456. foreach($mixedResult['content'] as $aFile)
  457. $sResult .= _t('_adm_err_modules_wrong_permissions_msg', $aFile['path'], $aFile['permissions']) . '<br />';
  458. return $sResult;
  459. }
  460. function actionChangePermissions($sOperation) {
  461. if(!isset($this->_aConfig[$sOperation . '_permissions']) || !is_array($this->_aConfig[$sOperation . '_permissions']))
  462. return BX_DOL_STUDIO_INSTALLER_FAILED;
  463. $oFile = $this->_getFileManager();
  464. if(empty($oFile))
  465. return array('code' => BX_DOL_STUDIO_INSTALLER_FAILED);
  466. $aResult = array();
  467. $aPermissions = $this->_aConfig[$sOperation . '_permissions'];
  468. foreach($aPermissions as $sPermissions => $aFiles) {
  469. $sCheckFunction = 'is' . ucfirst($sPermissions);
  470. foreach($aFiles as $sFile) {
  471. $sPath = bx_ltrim_str($this->_sModulePath . $sFile, BX_DIRECTORY_PATH_ROOT);
  472. if(BxDolInstallerUtils::$sCheckFunction($sPath))
  473. continue;
  474. if(!$oFile->setPermissions($sPath, $sPermissions))
  475. $aResult[] = array('path' => $this->_sModulePath . $sFile, 'permissions' => $sPermissions);
  476. }
  477. }
  478. return empty($aResult) ? BX_DOL_STUDIO_INSTALLER_SUCCESS : array('code' => BX_DOL_STUDIO_INSTALLER_FAILED, 'content' => $aResult);
  479. }
  480. function actionChangePermissionsFailed($mixedResult) {
  481. if(empty($mixedResult['content']))
  482. return $this->actionOperationFailed($mixedResult);
  483. $sResult = '<br />' . _t('_adm_err_modules_wrong_permissions_change') . '<br />';
  484. foreach($mixedResult['content'] as $aFile)
  485. $sResult .= _t('_adm_err_modules_wrong_permissions_msg', $aFile['path'], $aFile['permissions']) . '<br />';
  486. return $sResult;
  487. }
  488. function actionMoveSources($sOperation) {
  489. $oFile = $this->_getFileManager();
  490. $aInstalled = array_merge(array('system'), $this->oDb->getModulesUri());
  491. $bResult = true;
  492. foreach($this->_aConfig['includes'] as $sUri => $sPath) {
  493. if(!in_array($sUri, $aInstalled) || empty($sPath))
  494. continue;
  495. if($sOperation == 'install') {
  496. $sSrcPath = 'modules/' . $this->_aConfig['home_dir'] . 'install/data/' . $sPath;
  497. $sDstPath = $sPath;
  498. $bResult &= $oFile->copy($sSrcPath, $sDstPath);
  499. }
  500. else if($sOperation == 'uninstall')
  501. $bResult &= $oFile->delete($sPath);
  502. }
  503. return $bResult ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  504. }
  505. function actionExecuteSql($sOperation) {
  506. switch($sOperation) {
  507. case 'install':
  508. $this->actionExecuteSql('disable');
  509. $this->actionExecuteSql('uninstall');
  510. break;
  511. case 'enable':
  512. $this->actionExecuteSql('disable');
  513. break;
  514. }
  515. $sPath = $this->_sHomePath . 'install/sql/' . $sOperation . '.sql';
  516. if(!file_exists($sPath) || !($rHandler = fopen($sPath, "r")))
  517. return BX_DOL_STUDIO_INSTALLER_FAILED;
  518. $sQuery = "";
  519. $sDelimiter = ';';
  520. $aResult = array();
  521. while(!feof($rHandler)) {
  522. $sStr = trim(fgets($rHandler));
  523. if(empty($sStr) || $sStr[0] == "" || $sStr[0] == "#" || ($sStr[0] == "-" && $sStr[1] == "-"))
  524. continue;
  525. //--- Change delimiter ---//
  526. if(strpos($sStr, "DELIMITER //") !== false || strpos($sStr, "DELIMITER ;") !== false) {
  527. $sDelimiter = trim(str_replace('DELIMITER', '', $sStr));
  528. continue;
  529. }
  530. $sQuery .= $sStr;
  531. //--- Check for multiline query ---//
  532. if(substr($sStr, -strlen($sDelimiter)) != $sDelimiter)
  533. continue;
  534. //--- Execute query ---//
  535. if($sDelimiter != ';')
  536. $sQuery = str_replace($sDelimiter, "", $sQuery);
  537. $rResult = db_res(trim($sQuery), false);
  538. if(!$rResult)
  539. $aResult[] = array('query' => $sQuery, 'error' => BxDolDb::getInstance()->getErrorMessage());
  540. $sQuery = "";
  541. }
  542. fclose($rHandler);
  543. return empty($aResult) ? BX_DOL_STUDIO_INSTALLER_SUCCESS : array('code' => BX_DOL_STUDIO_INSTALLER_FAILED, 'content' => $aResult);
  544. }
  545. function actionExecuteSqlFailed($mixedResult) {
  546. $sResult = '<br />' . _t('_adm_err_modules_wrong_mysql_query') . '<br />';
  547. foreach($mixedResult['content'] as $aQuery) {
  548. $sResult .= _t('_adm_err_modules_wrong_mysql_query_msg', $aQuery['error']) . '<br />';
  549. $sResult .= '<pre>' . $aQuery['query'] . '</pre>';
  550. }
  551. return $sResult;
  552. }
  553. function actionInstallLanguage($sOperation) {
  554. bx_import('BxDolStudioLanguagesUtils');
  555. $oLanguages = BxDolStudioLanguagesUtils::getInstance();
  556. $sLanguage = isset($this->_aConfig['home_uri']) ? $this->_aConfig['home_uri'] : '';
  557. $bResult = true;
  558. if($sOperation == 'install')
  559. $bResult = $oLanguages->installLanguage(array('path' => $this->_aConfig['home_dir'], 'uri' => $this->_aConfig['home_uri'], 'lang_category' => $this->_aConfig['language_category']),false);
  560. return $bResult && $oLanguages->compileLanguage(0, true) ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  561. }
  562. function actionUpdateLanguages($sOperation) {
  563. if(!isset($this->_aConfig['language_category']) || empty($this->_aConfig['language_category']))
  564. return BX_DOL_STUDIO_INSTALLER_FAILED;
  565. bx_import('BxDolStudioLanguagesUtils');
  566. $oLanguages = BxDolStudioLanguagesUtils::getInstance();
  567. $bResult = true;
  568. $aResult = array();
  569. //--- Process Language Category ---//
  570. if($sOperation == 'install')
  571. $iCategoryId = $oLanguages->addLanguageCategory($this->_aConfig['language_category']);
  572. //--- Process languages' key=>value pears ---//
  573. $aModule = array(
  574. 'path' => $this->_aConfig['home_dir'],
  575. 'uri' => $this->_aConfig['home_uri'],
  576. 'lang_category' => $this->_aConfig['language_category']
  577. );
  578. if($sOperation == 'install')
  579. $bResult = $oLanguages->restoreLanguage(0, $aModule, false);
  580. else if($sOperation == 'uninstall')
  581. $bResult = $oLanguages->removeLanguageByModule($aModule, false);
  582. if($sOperation == 'uninstall' && $bResult)
  583. $oLanguages->deleteLanguageCategory($this->_aConfig['language_category']);
  584. return $bResult && $oLanguages->compileLanguage(0, true) ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  585. }
  586. function actionRecompileGlobalParamaters($sOperation) {
  587. $bResult = $this->oDb->oParams->cache();
  588. return $bResult ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  589. }
  590. function actionRecompileMainMenu($sOperation) {
  591. /*
  592. TODO: Update the code when menu engine is rewrited.
  593. bx_import('BxDolMenu');
  594. $bResult = BxDolMenu::getInstance()->_compile();
  595. */
  596. $bResult = true;
  597. return $bResult ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  598. }
  599. function actionRecompileMemberMenu($sOperation) {
  600. /*
  601. //TODO: Recreate if the feature will be available.
  602. bx_import('BxDolMemberMenu');
  603. $oMemberMenu = new BxDolMemberMenu();
  604. $bResult = $oMemberMenu->deleteMemberMenuCaches();
  605. */
  606. $bResult = true;
  607. return $bResult ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  608. }
  609. function actionRecompileSiteStats($sOperation) {
  610. $bResult = $this->oDb->cleanCache('sys_stat_site');
  611. return $bResult ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  612. }
  613. function actionRecompilePageBuilder($sOperation) {
  614. /*
  615. //TODO: Recreate when cache engine is available.
  616. bx_import('BxDolPageViewCacher');
  617. ob_start();
  618. $oPVCacher = new BxDolPageViewCacher('sys_page_compose', 'sys_page_compose.inc');
  619. $bResult = $oPVCacher->createCache();
  620. ob_get_clean();
  621. */
  622. $bResult = true;
  623. return $bResult ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  624. }
  625. function actionRecompileProfileFields($sOperation) {
  626. /*
  627. //TODO: Recreate when cache engine is available.
  628. bx_import('BxDolPFMCacher');
  629. ob_start();
  630. $oBxDolPFMCacher = new BxDolPFMCacher();
  631. $bResult = $oBxDolPFMCacher -> createCache();
  632. ob_get_clean();
  633. */
  634. $bResult = true;
  635. return $bResult ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  636. }
  637. function actionRecompileComments($sOperation) {
  638. $bResult = $this->oDb->cleanCache('sys_objects_cmts');
  639. return $bResult ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  640. }
  641. function actionRecompileMemberActions($sOperation) {
  642. $bResult = $this->oDb->cleanCache('sys_objects_actions');
  643. return $bResult ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  644. }
  645. function actionRecompileVotes($sOperation) {
  646. $bResult = $this->oDb->cleanCache('sys_objects_vote');
  647. return $bResult ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  648. }
  649. function actionRecompileInjections($sOperation) {
  650. $bResult = $this->oDb->cleanCache('sys_injections.inc');
  651. $bResult = $bResult && $this->oDb->cleanCache('sys_injections_admin.inc');
  652. return $bResult ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  653. }
  654. function actionRecompilePermalinks($sOperation) {
  655. $bResult = true;
  656. ob_start();
  657. bx_import('BxDolPermalinks');
  658. $bResult = $bResult && BxDolPermalinks::getInstance()->cache();
  659. /*
  660. TODO: Update the code when menu engine is rewrited.
  661. bx_import('BxDolMenu');
  662. $bResult = $bResult && BxDolMenu::getInstance()->_compile();
  663. */
  664. $bResult = $bResult && $this->oDb->cleanCache('sys_menu_member');
  665. ob_get_clean();
  666. return $bResult ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  667. }
  668. function actionRecompileAlerts($sOperation) {
  669. $bResult = $this->oDb->cleanCache('sys_alerts');
  670. return $bResult ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  671. }
  672. function actionClearDbCache($sOperation) {
  673. $oCache = $this->oDb->getDbCacheObject();
  674. $bResult = $oCache->removeAllByPrefix('db_');
  675. return $bResult ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  676. }
  677. //--- Get/Set Methods ---//
  678. function getVendor() {
  679. return $this->_aConfig['vendor'];
  680. }
  681. function getName() {
  682. return $this->_aConfig['name'];
  683. }
  684. function getTitle() {
  685. return $this->_aConfig['title'];
  686. }
  687. function getHomeDir() {
  688. return $this->_aConfig['home_dir'];
  689. }
  690. //--- Protected methods ---//
  691. protected function _getFileManager($bUseFtp = true) {
  692. $oFile = null;
  693. if($bUseFtp) {
  694. bx_import('BxDolFtp');
  695. $oFile = new BxDolFtp(isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'localhost', $this->oDb->getParam('sys_ftp_login'), $this->oDb->getParam('sys_ftp_password'), $this->oDb->getParam('sys_ftp_dir'));
  696. if(!$oFile->connect())
  697. return null;
  698. if(!$oFile->isDolphin())
  699. return null;
  700. }
  701. else {
  702. bx_import('BxDolFile');
  703. $oFile = BxDolFile::getInstance();
  704. }
  705. return $oFile;
  706. }
  707. }
  708. /** @} */