BxDolStudioInstaller.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  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_tags' => array(
  123. 'title' => _t('_adm_txt_modules_recompile_tags'),
  124. ),
  125. 'recompile_votes' => array(
  126. 'title' => _t('_adm_txt_modules_recompile_votes'),
  127. ),
  128. 'recompile_categories' => array(
  129. 'title' => _t('_adm_txt_modules_recompile_categories'),
  130. ),
  131. 'recompile_search' => array(
  132. 'title' => _t('_adm_txt_modules_recompile_search'),
  133. ),
  134. 'recompile_injections' => array(
  135. 'title' => _t('_adm_txt_modules_recompile_injections'),
  136. ),
  137. 'recompile_permalinks' => array(
  138. 'title' => _t('_adm_txt_modules_recompile_permalinks'),
  139. ),
  140. 'recompile_alerts' => array(
  141. 'title' => _t('_adm_txt_modules_recompile_alerts'),
  142. ),
  143. 'clear_db_cache' => array(
  144. 'title' => _t('_adm_txt_modules_clear_db_cache'),
  145. ),
  146. 'show_conclusion' => array(
  147. 'title' => _t('_adm_txt_modules_show_conclusion'),
  148. ),
  149. );
  150. $this->_aNonHashable = array(
  151. 'install',
  152. 'updates'
  153. );
  154. }
  155. public function install($aParams, $bEnable = false) {
  156. //--- Check whether the module was already installed ---//
  157. if($this->oDb->isModule($this->_aConfig['home_uri']))
  158. return array(
  159. 'message' => _t('_adm_err_modules_already_installed'),
  160. 'result' => false
  161. );
  162. //--- Check mandatory settings ---//
  163. if($this->oDb->isModuleParamsUsed($this->_aConfig['home_uri'], $this->_aConfig['home_dir'], $this->_aConfig['db_prefix'], $this->_aConfig['class_prefix']))
  164. return array(
  165. 'message' => _t('_adm_txt_modules_params_used'),
  166. 'result' => false
  167. );
  168. //--- Check version compatibility ---//
  169. $bCompatible = false;
  170. if(isset($this->_aConfig['compatible_with']) && is_array($this->_aConfig['compatible_with']))
  171. foreach($this->_aConfig['compatible_with'] as $iKey => $sVersion) {
  172. $sVersion = '/^' . str_replace(array('.', 'x'), array('\.', '[0-9]+'), $sVersion) . '$/is';
  173. $bCompatible = $bCompatible || (preg_match($sVersion, getParam('sys_version')) > 0);
  174. }
  175. if(!$bCompatible)
  176. return array(
  177. 'message' => $this->_displayResult('check_script_version', false, '_adm_err_modules_wrong_version_script'),
  178. 'result' => false
  179. );
  180. //--- Check for available translations ---//
  181. $oFile = $this->_getFileManager();
  182. $sModuleUri = $this->_aConfig['home_uri'];
  183. $aLanguages = $this->oDb->getModulesBy(array('type' => 'languages'));
  184. foreach($aLanguages as $aLanguage) {
  185. if($aLanguage['uri'] == 'en')
  186. continue;
  187. $sLanguageConfig = BX_DIRECTORY_PATH_MODULES . $aLanguage['path'] . '/install/config.php';
  188. if(!file_exists($sLanguageConfig))
  189. continue;
  190. include_once($sLanguageConfig);
  191. $aLanguageConfig = &$aConfig;
  192. if(!isset($aLanguageConfig['includes'][$sModuleUri]) || empty($aLanguageConfig['includes'][$sModuleUri]))
  193. continue;
  194. $sSrcPath = 'modules/' . $aLanguage['path'] . 'install/data/' . $aLanguageConfig['includes'][$sModuleUri];
  195. $sDstPath = $aLanguageConfig['includes'][$sModuleUri];
  196. $oFile->copy($sSrcPath, $sDstPath);
  197. }
  198. //--- Check actions ---//
  199. $aResult = $this->_perform('install');
  200. if($aResult['result']) {
  201. $sDependencies = "";
  202. if(isset($this->_aConfig['install']['check_dependencies']) && (int)$this->_aConfig['install']['check_dependencies'] == 1 && isset($this->_aConfig['dependencies']) && is_array($this->_aConfig['dependencies']))
  203. $this->_aConfig['dependencies'] = array_keys($this->_aConfig['dependencies']);
  204. $iModuleId = $this->oDb->insertModule($this->_aConfig);
  205. bx_import('BxDolModule');
  206. $sTitleKey = BxDolModule::getTitleKey($this->_aConfig['home_uri']);
  207. bx_import('BxDolStudioLanguagesUtils');
  208. $oLanguages = BxDolStudioLanguagesUtils::getInstance();
  209. $oLanguages->addLanguageString($sTitleKey, $this->_aConfig['title']);
  210. $oLanguages->compileLanguage();
  211. $aFiles = array();
  212. $this->_hash($this->_sModulePath, $aFiles);
  213. foreach($aFiles as $aFile)
  214. $this->oDb->insertModuleTrack($iModuleId, $aFile);
  215. $this->oDb->cleanMemory('sys_modules_' . $this->_aConfig['home_uri']);
  216. $this->oDb->cleanMemory('sys_modules_' . $iModuleId);
  217. $this->oDb->cleanMemory('sys_modules');
  218. }
  219. if($bEnable) {
  220. $aResultEnable = $this->enable($aParams);
  221. $aResult['result'] = $aResult['result'] & $aResultEnable['result'];
  222. $aResult['message'] = $aResult['message'] . $aResultEnable['message'];
  223. }
  224. return $aResult;
  225. }
  226. public function uninstall($aParams, $bDisable = false) {
  227. //--- Check whether the module was already uninstalled ---//
  228. if(!$this->oDb->isModule($this->_aConfig['home_uri']))
  229. return array(
  230. 'message' => _t('_adm_err_modules_already_uninstalled'),
  231. 'result' => false
  232. );
  233. //--- Check for dependent modules ---//
  234. $bDependent = false;
  235. $aDependents = $this->oDb->getDependent($this->_aConfig['home_uri']);
  236. if(is_array($aDependents) && !empty($aDependents)) {
  237. $bDependent = true;
  238. $sMessage = '<br />' . _t('_adm_err_modules_wrong_dependency_uninstall') . '<br />';
  239. foreach($aDependents as $aDependent)
  240. $sMessage .= $aDependent['title'] . '<br />';
  241. }
  242. if($bDependent)
  243. return array(
  244. 'message' => $this->_displayResult('check_dependencies', false, $sMessage),
  245. 'result' => false
  246. );
  247. $aResult = $this->_perform('uninstall');
  248. if($aResult['result']) {
  249. $iModuleId = $this->oDb->deleteModule($this->_aConfig);
  250. bx_import('BxDolModule');
  251. $sTitleKey = BxDolModule::getTitleKey($this->_aConfig['home_uri']);
  252. bx_import('BxDolStudioLanguagesUtils');
  253. $oLanguages = BxDolStudioLanguagesUtils::getInstance();
  254. $oLanguages->deleteLanguageString($sTitleKey);
  255. $oLanguages->compileLanguage();
  256. $this->oDb->cleanMemory ('sys_modules_' . $this->_aConfig['home_uri']);
  257. $this->oDb->cleanMemory ('sys_modules_' . $iModuleId);
  258. $this->oDb->cleanMemory ('sys_modules');
  259. }
  260. if($bDisable) {
  261. $aResultEnable = $this->disable($aParams);
  262. $aResult['result'] = $aResult['result'] & $aResultEnable['result'];
  263. $aResult['message'] = $aResult['message'] . $aResultEnable['message'];
  264. }
  265. return $aResult;
  266. }
  267. public function delete() {
  268. bx_import('BxDolFtp');
  269. $oFtp = new BxDolFtp($_SERVER['HTTP_HOST'], getParam('sys_ftp_login'), getParam('sys_ftp_password'), getParam('sys_ftp_dir'));
  270. if(!$oFtp->connect())
  271. return array(
  272. 'message' => _t('_adm_err_modules_cannot_connect_to_ftp'),
  273. 'result' => false
  274. );
  275. if(!$oFtp->delete('modules/' . $this->_aConfig['home_dir']))
  276. return array(
  277. 'message' => _t('_adm_err_modules_cannot_remove_package'),
  278. 'result' => false
  279. );
  280. return array(
  281. 'message' => '', //_t('_adm_msg_modules_success_delete'),
  282. 'result' => true
  283. );
  284. }
  285. public function recompile($aParams) {
  286. bx_import('BxDolStudioLanguagesUtils');
  287. $oLanguages = BxDolStudioLanguagesUtils::getInstance();
  288. $aResult = array('message' => '', 'result' => false);
  289. $aLanguages = $this->oDb->getLanguages();
  290. if (isAdmin() && !empty($aLanguages)) {
  291. $this->_updateLanguage(false, current($aLanguages));
  292. $bResult = false;
  293. foreach($aLanguages as $aLanguage) {
  294. $bResult = $this->_updateLanguage(true, $aLanguage) && $oLanguages->compileLanguage($aLanguage['id']);
  295. $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 />';
  296. $aResult['result'] |= $bResult;
  297. }
  298. }
  299. return $aResult;
  300. }
  301. public function enable($aParams) {
  302. $aModule = $this->oDb->getModuleByUri($this->_aConfig['home_uri']);
  303. //--- Check whether the module is installed ---//
  304. if(empty($aModule) || !is_array($aModule))
  305. return array(
  306. 'message' => _t('_adm_err_modules_module_not_installed'),
  307. 'result' => false
  308. );
  309. //--- Check whether the module is already enabled ---//
  310. if((int)$aModule['enabled'] != 0)
  311. return array(
  312. 'message' => _t('_adm_err_modules_already_enabled'),
  313. 'result' => false
  314. );
  315. $aResult = $this->_perform('enable');
  316. if($aResult['result']) {
  317. $this->oDb->enableModuleByUri($aModule['uri']);
  318. $this->oDb->cleanMemory('sys_modules_' . $aModule['uri']);
  319. $this->oDb->cleanMemory('sys_modules_' . $aModule['id']);
  320. $this->oDb->cleanMemory('sys_modules');
  321. }
  322. return $aResult;
  323. }
  324. public function disable($aParams) {
  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. //--- Check for dependent modules ---//
  339. $bDependent = false;
  340. $aDependents = $this->oDb->getDependent($this->_aConfig['home_uri']);
  341. if(is_array($aDependents) && !empty($aDependents)) {
  342. $bDependent = true;
  343. $sMessage = '<br />' . _t('_adm_err_modules_wrong_dependency_disable') . '<br />';
  344. foreach($aDependents as $aDependent)
  345. $sMessage .= $aDependent['title'] . '<br />';
  346. }
  347. if($bDependent)
  348. return array(
  349. 'message' => $this->_displayResult('check_dependencies', false, $sMessage),
  350. 'result' => false
  351. );
  352. $aResult = $this->_perform('disable');
  353. if($aResult['result']) {
  354. $this->oDb->disableModuleByUri($aModule['uri']);
  355. $this->oDb->cleanMemory('sys_modules_' . $aModule['uri']);
  356. $this->oDb->cleanMemory('sys_modules_' . $aModule['id']);
  357. $this->oDb->cleanMemory('sys_modules');
  358. }
  359. return $aResult;
  360. }
  361. function _hash($sPath, &$aFiles) {
  362. if(file_exists($sPath) && is_dir($sPath) && ($rSource = opendir($sPath))) {
  363. while(($sFile = readdir($rSource)) !== false) {
  364. if($sFile == '.' || $sFile =='..' || $sFile[0] == '.' || in_array($sFile, $this->_aNonHashable))
  365. continue;
  366. if(is_dir($sPath . $sFile))
  367. $this->_hash($sPath . $sFile . '/', $aFiles);
  368. else
  369. $aFiles[] = $this->_info($sPath . $sFile);
  370. }
  371. closedir($rSource);
  372. }
  373. else
  374. $aFiles[] = $this->_info($sPath, $sFile);
  375. }
  376. function _info($sPath) {
  377. return array(
  378. 'file' => str_replace($this->_sModulePath, '', $sPath),
  379. 'hash' => md5(file_get_contents($sPath))
  380. );
  381. }
  382. function _perform($sOperationName) {
  383. if(!defined('BX_SKIP_INSTALL_CHECK') && !$GLOBALS['logged']['admin'])
  384. return array('message' => '', 'result' => false);
  385. $sMessage = '';
  386. foreach($this->_aConfig[$sOperationName] as $sAction => $iEnabled) {
  387. $sMethod = 'action' . str_replace (' ', '', ucwords(str_replace ('_', ' ', $sAction)));
  388. if($iEnabled == 0 || !method_exists($this, $sMethod))
  389. continue;
  390. $mixedResult = $this->$sMethod($sOperationName);
  391. //--- On Success ---//
  392. if((is_int($mixedResult) && (int)$mixedResult == BX_DOL_STUDIO_INSTALLER_SUCCESS) || (isset($mixedResult['code']) && (int)$mixedResult['code'] == BX_DOL_STUDIO_INSTALLER_SUCCESS)) {
  393. $sMessage .= $this->_displayResult($sAction, true, isset($mixedResult['content']) ? $mixedResult['content'] : '');
  394. continue;
  395. }
  396. //--- On Failed ---//
  397. $sMethodFailed = $sMethod . 'Failed';
  398. return array('message' => $this->_displayResult($sAction, false, method_exists($this, $sMethodFailed) ? $this->$sMethodFailed($mixedResult) : $this->actionOperationFailed($mixedResult)), 'result' => false);
  399. }
  400. if($this->_bShowOnSuccess)
  401. $sMessage = $this->_aActions['perform_' . $sOperationName]['success'] . $sMessage;
  402. return array('message' => $sMessage, 'result' => true);
  403. }
  404. function _displayResult($sAction, $bResult, $sResult = '') {
  405. $sMessage = $this->_aActions[$sAction]['title'] . ' ';
  406. if(!empty($sResult) && substr($sResult, 0, 1) == '_')
  407. $sResult = _t($sResult) . '<br />';
  408. if(!$bResult)
  409. return $sMessage . '<span style="color:red;">' . $sResult . '</span>';
  410. if($bResult && !in_array($sAction, array('show_introduction', 'show_conclusion')) && !$this->_bShowOnSuccess)
  411. return;
  412. return $sMessage . '<span style="color:green;">' . (!empty($sResult) ? $sResult : _t('_adm_txt_modules_process_action_success') . '<br />') . '</span>';
  413. }
  414. //--- Action Methods ---//
  415. function actionOperationFailed($mixedResult) {
  416. return _t('_adm_err_modules_process_action_failed');
  417. }
  418. function actionCheckDependencies($sOperation) {
  419. $sContent = '';
  420. if(in_array($sOperation, array('install', 'enable', 'update'))) {
  421. if(!isset($this->_aConfig['dependencies']) || !is_array($this->_aConfig['dependencies']))
  422. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  423. foreach($this->_aConfig['dependencies'] as $sModuleUri => $sModuleTitle)
  424. if(!$this->oDb->isModule($sModuleUri))
  425. $sContent .= $sModuleTitle . '<br />';
  426. if(!empty($sContent))
  427. $sContent = '<br />' . _t('_adm_err_modules_wrong_dependency_install') . '<br />' . $sContent;
  428. }
  429. return empty($sContent) ? BX_DOL_STUDIO_INSTALLER_SUCCESS : array('code' => BX_DOL_STUDIO_INSTALLER_FAILED, 'content' => $sContent);
  430. }
  431. function actionCheckDependenciesFailed($mixedResult) {
  432. return $mixedResult['content'];
  433. }
  434. function actionShowIntroduction($sOperation) {
  435. if(!isset($this->_aConfig[$sOperation . '_info']['introduction']))
  436. return BX_DOL_STUDIO_INSTALLER_FAILED;
  437. $sPath = $this->_sHomePath . 'install/info/' . $this->_aConfig[$sOperation . '_info']['introduction'];
  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 actionShowConclusion($sOperation) {
  441. if(!isset($this->_aConfig[$sOperation . '_info']['conclusion']))
  442. return BX_DOL_STUDIO_INSTALLER_FAILED;
  443. $sPath = $this->_sHomePath . 'install/info/' . $this->_aConfig[$sOperation . '_info']['conclusion'];
  444. return file_exists($sPath) ? array("code" => BX_DOL_STUDIO_INSTALLER_SUCCESS, "content" => "<pre>" . file_get_contents($sPath) . "</pre>") : BX_DOL_STUDIO_INSTALLER_FAILED;
  445. }
  446. function actionCheckPermissions($sOperation) {
  447. if(!isset($this->_aConfig[$sOperation . '_permissions']) || !is_array($this->_aConfig[$sOperation . '_permissions']))
  448. return BX_DOL_STUDIO_INSTALLER_FAILED;
  449. $aResult = array();
  450. $aPermissions = $this->_aConfig[$sOperation . '_permissions'];
  451. foreach($aPermissions as $sPermissions => $aFiles) {
  452. $sCheckFunction = 'is' . ucfirst($sPermissions);
  453. $sCptPermissions = _t('_adm_txt_modules_' . $sPermissions);
  454. foreach($aFiles as $sFile)
  455. if(!BxDolInstallerUtils::$sCheckFunction(str_replace(BX_DIRECTORY_PATH_ROOT, '', $this->_sModulePath . $sFile)))
  456. $aResult[] = array('path' => $this->_sModulePath . $sFile, 'permissions' => $sCptPermissions);
  457. }
  458. return empty($aResult) ? BX_DOL_STUDIO_INSTALLER_SUCCESS : array('code' => BX_DOL_STUDIO_INSTALLER_FAILED, 'content' => $aResult);
  459. }
  460. function actionCheckPermissionsFailed($mixedResult) {
  461. $sResult = '<br />' . _t('_adm_err_modules_wrong_permissions_check') . '<br />';
  462. foreach($mixedResult['content'] as $aFile)
  463. $sResult .= _t('_adm_err_modules_wrong_permissions_msg', $aFile['path'], $aFile['permissions']) . '<br />';
  464. return $sResult;
  465. }
  466. function actionChangePermissions($sOperation) {
  467. if(!isset($this->_aConfig[$sOperation . '_permissions']) || !is_array($this->_aConfig[$sOperation . '_permissions']))
  468. return BX_DOL_STUDIO_INSTALLER_FAILED;
  469. $oFile = $this->_getFileManager();
  470. if(empty($oFile))
  471. return array('code' => BX_DOL_STUDIO_INSTALLER_FAILED);
  472. $aResult = array();
  473. $aPermissions = $this->_aConfig[$sOperation . '_permissions'];
  474. foreach($aPermissions as $sPermissions => $aFiles) {
  475. $sCheckFunction = 'is' . ucfirst($sPermissions);
  476. foreach($aFiles as $sFile) {
  477. $sPath = bx_ltrim_str($this->_sModulePath . $sFile, BX_DIRECTORY_PATH_ROOT);
  478. if(BxDolInstallerUtils::$sCheckFunction($sPath))
  479. continue;
  480. if(!$oFile->setPermissions($sPath, $sPermissions))
  481. $aResult[] = array('path' => $this->_sModulePath . $sFile, 'permissions' => $sPermissions);
  482. }
  483. }
  484. return empty($aResult) ? BX_DOL_STUDIO_INSTALLER_SUCCESS : array('code' => BX_DOL_STUDIO_INSTALLER_FAILED, 'content' => $aResult);
  485. }
  486. function actionChangePermissionsFailed($mixedResult) {
  487. if(empty($mixedResult['content']))
  488. return $this->actionOperationFailed($mixedResult);
  489. $sResult = '<br />' . _t('_adm_err_modules_wrong_permissions_change') . '<br />';
  490. foreach($mixedResult['content'] as $aFile)
  491. $sResult .= _t('_adm_err_modules_wrong_permissions_msg', $aFile['path'], $aFile['permissions']) . '<br />';
  492. return $sResult;
  493. }
  494. function actionMoveSources($sOperation) {
  495. $oFile = $this->_getFileManager();
  496. $aInstalled = array_merge(array('system'), $this->oDb->getModulesUri());
  497. $bResult = true;
  498. foreach($this->_aConfig['includes'] as $sUri => $sPath) {
  499. if(!in_array($sUri, $aInstalled) || empty($sPath))
  500. continue;
  501. if($sOperation == 'install') {
  502. $sSrcPath = 'modules/' . $this->_aConfig['home_dir'] . 'install/data/' . $sPath;
  503. $sDstPath = $sPath;
  504. $bResult &= $oFile->copy($sSrcPath, $sDstPath);
  505. }
  506. else if($sOperation == 'uninstall')
  507. $bResult &= $oFile->delete($sPath);
  508. }
  509. return $bResult ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  510. }
  511. function actionExecuteSql($sOperation) {
  512. switch($sOperation) {
  513. case 'install':
  514. $this->actionExecuteSql('disable');
  515. $this->actionExecuteSql('uninstall');
  516. break;
  517. case 'enable':
  518. $this->actionExecuteSql('disable');
  519. break;
  520. }
  521. $sPath = $this->_sHomePath . 'install/sql/' . $sOperation . '.sql';
  522. if(!file_exists($sPath) || !($rHandler = fopen($sPath, "r")))
  523. return BX_DOL_STUDIO_INSTALLER_FAILED;
  524. $sQuery = "";
  525. $sDelimiter = ';';
  526. $aResult = array();
  527. while(!feof($rHandler)) {
  528. $sStr = trim(fgets($rHandler));
  529. if(empty($sStr) || $sStr[0] == "" || $sStr[0] == "#" || ($sStr[0] == "-" && $sStr[1] == "-"))
  530. continue;
  531. //--- Change delimiter ---//
  532. if(strpos($sStr, "DELIMITER //") !== false || strpos($sStr, "DELIMITER ;") !== false) {
  533. $sDelimiter = trim(str_replace('DELIMITER', '', $sStr));
  534. continue;
  535. }
  536. $sQuery .= $sStr;
  537. //--- Check for multiline query ---//
  538. if(substr($sStr, -strlen($sDelimiter)) != $sDelimiter)
  539. continue;
  540. //--- Execute query ---//
  541. if($sDelimiter != ';')
  542. $sQuery = str_replace($sDelimiter, "", $sQuery);
  543. $rResult = db_res(trim($sQuery), false);
  544. if(!$rResult)
  545. $aResult[] = array('query' => $sQuery, 'error' => BxDolDb::getInstance()->getErrorMessage());
  546. $sQuery = "";
  547. }
  548. fclose($rHandler);
  549. return empty($aResult) ? BX_DOL_STUDIO_INSTALLER_SUCCESS : array('code' => BX_DOL_STUDIO_INSTALLER_FAILED, 'content' => $aResult);
  550. }
  551. function actionExecuteSqlFailed($mixedResult) {
  552. $sResult = '<br />' . _t('_adm_err_modules_wrong_mysql_query') . '<br />';
  553. foreach($mixedResult['content'] as $aQuery) {
  554. $sResult .= _t('_adm_err_modules_wrong_mysql_query_msg', $aQuery['error']) . '<br />';
  555. $sResult .= '<pre>' . $aQuery['query'] . '</pre>';
  556. }
  557. return $sResult;
  558. }
  559. function actionInstallLanguage($sOperation) {
  560. bx_import('BxDolStudioLanguagesUtils');
  561. $oLanguages = BxDolStudioLanguagesUtils::getInstance();
  562. $sLanguage = isset($this->_aConfig['home_uri']) ? $this->_aConfig['home_uri'] : '';
  563. $bResult = true;
  564. if($sOperation == 'install')
  565. $bResult = $oLanguages->installLanguage(array('path' => $this->_aConfig['home_dir'], 'uri' => $this->_aConfig['home_uri'], 'lang_category' => $this->_aConfig['language_category']),false);
  566. return $bResult && $oLanguages->compileLanguage(0, true) ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  567. }
  568. function actionUpdateLanguages($sOperation) {
  569. if(!isset($this->_aConfig['language_category']) || empty($this->_aConfig['language_category']))
  570. return BX_DOL_STUDIO_INSTALLER_FAILED;
  571. bx_import('BxDolStudioLanguagesUtils');
  572. $oLanguages = BxDolStudioLanguagesUtils::getInstance();
  573. $bResult = true;
  574. $aResult = array();
  575. //--- Process Language Category ---//
  576. if($sOperation == 'install')
  577. $iCategoryId = $oLanguages->addLanguageCategory($this->_aConfig['language_category']);
  578. //--- Process languages' key=>value pears ---//
  579. $aModule = array(
  580. 'path' => $this->_aConfig['home_dir'],
  581. 'uri' => $this->_aConfig['home_uri'],
  582. 'lang_category' => $this->_aConfig['language_category']
  583. );
  584. if($sOperation == 'install')
  585. $bResult = $oLanguages->restoreLanguage(0, $aModule, false);
  586. else if($sOperation == 'uninstall')
  587. $bResult = $oLanguages->removeLanguageByModule($aModule, false);
  588. if($sOperation == 'uninstall' && $bResult)
  589. $oLanguages->deleteLanguageCategory($this->_aConfig['language_category']);
  590. return $bResult && $oLanguages->compileLanguage(0, true) ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  591. }
  592. function actionRecompileGlobalParamaters($sOperation) {
  593. $bResult = $this->oDb->oParams->cache();
  594. return $bResult ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  595. }
  596. function actionRecompileMainMenu($sOperation) {
  597. /*
  598. TODO: Update the code when menu engine is rewrited.
  599. bx_import('BxDolMenu');
  600. $bResult = BxDolMenu::getInstance()->_compile();
  601. */
  602. $bResult = true;
  603. return $bResult ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  604. }
  605. function actionRecompileMemberMenu($sOperation) {
  606. /*
  607. //TODO: Recreate if the feature will be available.
  608. bx_import('BxDolMemberMenu');
  609. $oMemberMenu = new BxDolMemberMenu();
  610. $bResult = $oMemberMenu->deleteMemberMenuCaches();
  611. */
  612. $bResult = true;
  613. return $bResult ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  614. }
  615. function actionRecompileSiteStats($sOperation) {
  616. $bResult = $this->oDb->cleanCache('sys_stat_site');
  617. return $bResult ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  618. }
  619. function actionRecompilePageBuilder($sOperation) {
  620. /*
  621. //TODO: Recreate when cache engine is available.
  622. bx_import('BxDolPageViewCacher');
  623. ob_start();
  624. $oPVCacher = new BxDolPageViewCacher('sys_page_compose', 'sys_page_compose.inc');
  625. $bResult = $oPVCacher->createCache();
  626. ob_get_clean();
  627. */
  628. $bResult = true;
  629. return $bResult ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  630. }
  631. function actionRecompileProfileFields($sOperation) {
  632. /*
  633. //TODO: Recreate when cache engine is available.
  634. bx_import('BxDolPFMCacher');
  635. ob_start();
  636. $oBxDolPFMCacher = new BxDolPFMCacher();
  637. $bResult = $oBxDolPFMCacher -> createCache();
  638. ob_get_clean();
  639. */
  640. $bResult = true;
  641. return $bResult ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  642. }
  643. function actionRecompileComments($sOperation) {
  644. $bResult = $this->oDb->cleanCache('sys_objects_cmts');
  645. return $bResult ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  646. }
  647. function actionRecompileMemberActions($sOperation) {
  648. $bResult = $this->oDb->cleanCache('sys_objects_actions');
  649. return $bResult ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  650. }
  651. function actionRecompileTags($sOperation) {
  652. $bResult = $this->oDb->cleanCache('sys_objects_tag');
  653. return $bResult ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  654. }
  655. function actionRecompileVotes($sOperation) {
  656. $bResult = $this->oDb->cleanCache('sys_objects_vote');
  657. return $bResult ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  658. }
  659. function actionRecompileCategories($sOperation) {
  660. $bResult = $this->oDb->cleanCache('sys_objects_categories');
  661. return $bResult ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  662. }
  663. function actionRecompileInjections($sOperation) {
  664. $bResult = $this->oDb->cleanCache('sys_injections.inc');
  665. $bResult = $bResult && $this->oDb->cleanCache('sys_injections_admin.inc');
  666. return $bResult ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  667. }
  668. function actionRecompilePermalinks($sOperation) {
  669. $bResult = true;
  670. ob_start();
  671. bx_import('BxDolPermalinks');
  672. $bResult = $bResult && BxDolPermalinks::getInstance()->cache();
  673. /*
  674. TODO: Update the code when menu engine is rewrited.
  675. bx_import('BxDolMenu');
  676. $bResult = $bResult && BxDolMenu::getInstance()->_compile();
  677. */
  678. $bResult = $bResult && $this->oDb->cleanCache('sys_menu_member');
  679. ob_get_clean();
  680. return $bResult ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  681. }
  682. function actionRecompileAlerts($sOperation) {
  683. $bResult = $this->oDb->cleanCache('sys_alerts');
  684. return $bResult ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  685. }
  686. function actionClearDbCache($sOperation) {
  687. $oCache = $this->oDb->getDbCacheObject();
  688. $bResult = $oCache->removeAllByPrefix('db_');
  689. return $bResult ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  690. }
  691. //--- Get/Set Methods ---//
  692. function getVendor() {
  693. return $this->_aConfig['vendor'];
  694. }
  695. function getName() {
  696. return $this->_aConfig['name'];
  697. }
  698. function getTitle() {
  699. return $this->_aConfig['title'];
  700. }
  701. function getHomeDir() {
  702. return $this->_aConfig['home_dir'];
  703. }
  704. //--- Protected methods ---//
  705. protected function _getFileManager($bUseFtp = true) {
  706. $oFile = null;
  707. if($bUseFtp) {
  708. bx_import('BxDolFtp');
  709. $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'));
  710. if(!$oFile->connect())
  711. return null;
  712. if(!$oFile->isDolphin())
  713. return null;
  714. }
  715. else {
  716. bx_import('BxDolFile');
  717. $oFile = BxDolFile::getInstance();
  718. }
  719. return $oFile;
  720. }
  721. }
  722. /** @} */