BxDolStudioInstallerUtils.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaStudio UNA Studio
  7. * @{
  8. */
  9. bx_import('BxDolLanguages');
  10. define('BX_DOL_MARKET_URL_ROOT', 'https://una.io/');
  11. define('BX_DOL_MARKET_URL_INTEGRATION', BX_DOL_MARKET_URL_ROOT . 'm/market_api/');
  12. define('BX_DOL_STUDIO_IU_RC_SUCCESS', 0);
  13. define('BX_DOL_STUDIO_IU_RC_FAILED', 1);
  14. define('BX_DOL_STUDIO_IU_RC_SCHEDULED', 2);
  15. /*
  16. * Errors list.
  17. * It can be broaden later.
  18. */
  19. define('BX_DOL_STUDIO_IU_RCE_NOT_FOUND', 100);
  20. define('BX_DOL_STUDIO_IU_RCE_NOT_AVAILABLE', 101);
  21. define('BX_DOL_STUDIO_IU_RCE_NOT_AUTHORIZED', 102);
  22. define('BX_DOL_STUDIO_IU_RCE_ALREADY_PERFORMED', 103);
  23. define('BX_DOL_STUDIO_IU_RCE_UNIQUE_PARAMS_USED', 104);
  24. define('BX_DOL_STUDIO_IU_RCE_WSV_MI', 105); //--- Wrong script version during module install
  25. define('BX_DOL_STUDIO_IU_RCE_WSV_MU', 106); //--- Wrong script version during module update
  26. define('BX_DOL_STUDIO_IU_RCE_WMV', 107); //--- Wrong module version during module update
  27. define('BX_DOL_STUDIO_IU_RCE_MODIFIED', 108);
  28. define('BX_DOL_STUDIO_IU_RCE_CHECKSUM_FAILED', 109);
  29. define('BX_DOL_STUDIO_IU_RCE_SUBACTION_FAILED', 200); //--- Error appeared in some subaction
  30. class BxDolStudioInstallerUtils extends BxDolInstallerUtils implements iBxDolSingleton
  31. {
  32. protected $bUseFtp;
  33. protected $sAuthorizedAccessClass;
  34. protected $sStoreDataUrlPublic;
  35. protected $_oLog;
  36. public function __construct()
  37. {
  38. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  39. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  40. parent::__construct();
  41. $this->bUseFtp = BX_FORCE_USE_FTP_FILE_TRANSFER;
  42. $this->sAuthorizedAccessClass = 'BxDolStudioOAuthOAuth2';
  43. $this->sStoreDataUrlPublic = BX_DOL_MARKET_URL_INTEGRATION;
  44. $this->_oLog = BxDolLog::getInstance();
  45. $this->_oLog->setName('upgrade_modules');
  46. }
  47. /**
  48. * Prevent cloning the instance
  49. */
  50. public function __clone()
  51. {
  52. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  53. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  54. }
  55. /**
  56. * Get singleton instance of the class
  57. */
  58. public static function getInstance()
  59. {
  60. if (!isset($GLOBALS['bxDolClasses'][__CLASS__]))
  61. $GLOBALS['bxDolClasses'][__CLASS__] = new BxDolStudioInstallerUtils();
  62. return $GLOBALS['bxDolClasses'][__CLASS__];
  63. }
  64. public static function getNamePerformAction($sParam)
  65. {
  66. return 'sys_perform_action_' . md5($sParam);
  67. }
  68. public static function getNameDownloadFile($sParam)
  69. {
  70. return 'sys_download_file_complete_' . $sParam;
  71. }
  72. public static function getNamePerformModulesUpgrade()
  73. {
  74. return 'sys_upgrade_modules_transient';
  75. }
  76. /*
  77. * Is used to complete Downloading inside Transient Cron Job.
  78. */
  79. public function serviceDownloadFileComplete($sFilePath, $aParams = array())
  80. {
  81. return $this->downloadFileComplete($sFilePath, array_merge($aParams, array('transient' => true)));
  82. }
  83. /*
  84. * Is used to perform action inside Transient Cron Job.
  85. */
  86. public function servicePerformAction($sDirectory, $sOperation, $aParams)
  87. {
  88. return $this->perform($sDirectory, $sOperation, array_merge($aParams, array('transient' => true)));
  89. }
  90. /*
  91. * Is used to perform modules upgrade inside Transient Cron Job.
  92. */
  93. public function servicePerformModulesUpgrade($aParams = array())
  94. {
  95. return $this->performModulesUpgrade(array_merge($aParams, array('directly' => true, 'transient' => true)));
  96. }
  97. public function getStoreDataUrl($sType = 'public')
  98. {
  99. return $sType == 'public' ? $this->sStoreDataUrlPublic : '';
  100. }
  101. public function getAccessObject($bAuthorizedAccess)
  102. {
  103. $sClass = $bAuthorizedAccess ? $this->sAuthorizedAccessClass : 'BxDolStudioJson';
  104. bx_import($sClass);
  105. return $sClass::getInstance();
  106. }
  107. public function getModules($bTitleAsKey = true)
  108. {
  109. $aModules = array();
  110. $oTemplate = BxDolStudioTemplate::getInstance();
  111. $aInstalledInfo = $this->getInstalledInfo();
  112. $sPath = BX_DIRECTORY_PATH_MODULES;
  113. if(($rHandleVendor = opendir($sPath)) !== false) {
  114. while(($sVendor = readdir($rHandleVendor)) !== false) {
  115. if(substr($sVendor, 0, 1) == '.' || !is_dir($sPath . $sVendor))
  116. continue;
  117. if(($rHandleModule = opendir($sPath . $sVendor)) !== false) {
  118. while(($sModule = readdir($rHandleModule)) !== false) {
  119. if(!is_dir($sPath . $sVendor . '/' . $sModule) || substr($sModule, 0, 1) == '.')
  120. continue;
  121. $sModulePath = $sVendor . '/' . $sModule . '/';
  122. $aModule = $this->getConfigModule($sPath . $sModulePath . 'install/config.php', !empty($aInstalledInfo[$sModulePath]) ? $aInstalledInfo[$sModulePath] : array());
  123. if(empty($aModule))
  124. continue;
  125. $aModules[$bTitleAsKey ? $aModule['title'] : $aModule['name']] = $aModule;
  126. }
  127. closedir($rHandleModule);
  128. }
  129. }
  130. closedir($rHandleVendor);
  131. }
  132. ksort($aModules);
  133. return $aModules;
  134. }
  135. public function getUpdates()
  136. {
  137. $aUpdates = array();
  138. $oTemplate = BxDolStudioTemplate::getInstance();
  139. $aInstalledInfo = $this->getInstalledInfo();
  140. $sPath = BX_DIRECTORY_PATH_MODULES;
  141. if(($rHandleVendor = opendir($sPath)) !== false) {
  142. while(($sVendor = readdir($rHandleVendor)) !== false) {
  143. if(substr($sVendor, 0, 1) == '.' || !is_dir($sPath . $sVendor))
  144. continue;
  145. if(($rHandleModule = opendir($sPath . $sVendor . '/')) !== false) {
  146. while(($sModule = readdir($rHandleModule)) !== false) {
  147. if(!is_dir($sPath . $sVendor . '/' . $sModule) || substr($sModule, 0, 1) == '.')
  148. continue;
  149. $sModulePath = $sVendor . '/' . $sModule . '/';
  150. if(empty($aInstalledInfo[$sModulePath]))
  151. continue;
  152. $aUpdate = $this->getUpdate($aInstalledInfo[$sModulePath]);
  153. if(!empty($aUpdate))
  154. $aUpdates[$aUpdate['title']] = $aUpdate;
  155. }
  156. closedir($rHandleModule);
  157. }
  158. }
  159. closedir($rHandleVendor);
  160. }
  161. ksort($aUpdates);
  162. return $aUpdates;
  163. }
  164. public function getUpdate($mixedModule)
  165. {
  166. $aResult = array();
  167. if(!is_array($mixedModule))
  168. $mixedModule = BxDolModuleQuery::getInstance()->{is_numeric($mixedModule) ? 'getModuleById' : 'getModuleByName'}($mixedModule);
  169. if(empty($mixedModule) || !is_array($mixedModule))
  170. return $aResult;
  171. $sPathModule = BX_DIRECTORY_PATH_MODULES . $mixedModule['path'];
  172. $sPathUpdates = $sPathModule . 'updates/';
  173. $rHandleUpdate = @opendir($sPathUpdates);
  174. if($rHandleUpdate === false)
  175. return $aResult;
  176. while(($sUpdate = readdir($rHandleUpdate)) !== false) {
  177. $sPathUpdate = $sPathUpdates . $sUpdate . '/';
  178. if(substr($sUpdate, 0, 1) == '.' || !is_dir($sPathUpdate))
  179. continue;
  180. $aUpdate = $this->getConfigUpdate($sPathModule . 'install/config.php', $sPathUpdate . 'install/config.php', $mixedModule);
  181. if(empty($aUpdate) || !is_array($aUpdate) || version_compare($aUpdate['module_version'], $aUpdate['version_from']) != 0)
  182. continue;
  183. $aResult = $aUpdate;
  184. break;
  185. }
  186. closedir($rHandleUpdate);
  187. return $aResult;
  188. }
  189. public function perform($sDirectory, $sOperation, $aParams = array())
  190. {
  191. $bTransient = false;
  192. if(isset($aParams['transient'])) {
  193. $bTransient = (bool)$aParams['transient'];
  194. unset($aParams['transient']);
  195. }
  196. if(!defined('BX_DOL_CRON_EXECUTE') && !self::isRealOwner() && !in_array($sOperation, array('install', 'uninstall', 'enable', 'disable'))) {
  197. if($this->addTransientJob(self::getNamePerformAction($sDirectory), 'perform_action', array($sDirectory, $sOperation, $aParams)))
  198. return array('code' => BX_DOL_STUDIO_IU_RC_SCHEDULED, 'message' => _t('_adm_mod_msg_process_operation_scheduled'));
  199. else
  200. return array('code' => BX_DOL_STUDIO_IU_RC_FAILED, 'message' => _t('_adm_mod_err_process_operation_failed', $sOperation, $sDirectory));
  201. }
  202. $sConfigFile = 'install/config.php';
  203. $sInstallerFile = 'install/installer.php';
  204. $sInstallerClass = $sOperation == 'update' ? 'Updater' : 'Installer';
  205. $aConfig = self::getModuleConfig(BX_DIRECTORY_PATH_MODULES . $sDirectory . $sConfigFile);
  206. $sPathInstaller = BX_DIRECTORY_PATH_MODULES . $sDirectory . $sInstallerFile;
  207. if(empty($aConfig) || !file_exists($sPathInstaller)) {
  208. $sMessage = _t('_adm_mod_err_process_operation_failed', $sOperation, $sDirectory);
  209. $this->_oLog->write('Operation failed:', $sMessage);
  210. if($bTransient)
  211. $this->emailNotify($sMessage);
  212. return array('code' => BX_DOL_STUDIO_IU_RC_FAILED, 'message' => $sMessage);
  213. }
  214. require_once($sPathInstaller);
  215. $sClassName = $aConfig['class_prefix'] . $sInstallerClass;
  216. $oInstaller = new $sClassName($aConfig);
  217. $aResult = $oInstaller->$sOperation($aParams);
  218. if(!$aResult['result']) {
  219. $this->_oLog->write('Operation failed:', $aResult['message']);
  220. if($bTransient)
  221. $this->emailNotify($aResult['message']);
  222. }
  223. return array(
  224. 'code' => isset($aResult['code']) ? $aResult['code'] : ($aResult['result'] ? BX_DOL_STUDIO_IU_RC_SUCCESS : BX_DOL_STUDIO_IU_RC_FAILED),
  225. 'message' => $aResult['message']
  226. );
  227. }
  228. /**
  229. * Update modules using cron based auto updates.
  230. * @see BxDolCronUpgradeModulesCheck
  231. */
  232. public function performModulesUpgrade($aParams = array())
  233. {
  234. $bDirectly = true;
  235. if(isset($aParams['directly'])) {
  236. $bDirectly = (bool)$aParams['directly'];
  237. unset($aParams['directly']);
  238. }
  239. $bTransient = true;
  240. if(isset($aParams['transient'])) {
  241. $bTransient = (bool)$aParams['transient'];
  242. unset($aParams['transient']);
  243. }
  244. if(!defined('BX_DOL_CRON_EXECUTE') && !$bDirectly)
  245. return $this->addTransientJob(self::getNamePerformModulesUpgrade(), 'perform_modules_upgrade', array($aParams));
  246. $aFailed = array();
  247. $aUpdates = $this->checkUpdates();
  248. if(empty($aUpdates) || !is_array($aUpdates)) {
  249. $this->_oLog->write('Cannot get a list of modules which require to be updated.');
  250. return true;
  251. }
  252. foreach($aUpdates as $aUpdate) {
  253. $mixedResult = $this->downloadUpdatePublic($aUpdate['name']);
  254. if($mixedResult !== true) {
  255. $aFailed[$aUpdate['name']] = $mixedResult;
  256. continue;
  257. }
  258. }
  259. $aSuccess = array();
  260. $aUpdates = $this->getUpdates();
  261. if(empty($aUpdates) || !is_array($aUpdates)) {
  262. $this->_oLog->write('Cannot find update scripts for modules. They are damaged or were not downloaded.');
  263. return true;
  264. }
  265. foreach($aUpdates as $aUpdate) {
  266. $aResult = $this->perform($aUpdate['dir'], 'update', $aParams);
  267. switch((int)$aResult['code']) {
  268. case BX_DOL_STUDIO_IU_RC_SUCCESS:
  269. $aSuccess[$aUpdate['module_name']] = $aUpdate['version_to'];
  270. break;
  271. case BX_DOL_STUDIO_IU_RCE_WSV_MU:
  272. if(isset($aParams['autoupdate']) && (bool)$aParams['autoupdate'] === true)
  273. break;
  274. default:
  275. $aFailed[$aUpdate['module_name']] = $aResult['message'];
  276. }
  277. }
  278. if(!empty($aFailed)) {
  279. $this->_oLog->write('Failed to update modules:', $aFailed);
  280. if($bTransient)
  281. $this->emailNotifyModulesUpgrade('failed', $aFailed);
  282. }
  283. if(!empty($aSuccess)) {
  284. $this->_oLog->write('Successfully updated modules:', $aSuccess);
  285. if($bTransient)
  286. $this->emailNotifyModulesUpgrade('success', $aSuccess);
  287. }
  288. return empty($aFailed);
  289. }
  290. public function checkModules($bAuthorizedAccess = false)
  291. {
  292. $iPerPage = 9999; //--- Note. It's essential to load all purchased products at the same time.
  293. if($bAuthorizedAccess)
  294. $aProducts = $this->getAccessObject(true)->loadItems(array(
  295. 'method' => 'browse_purchased',
  296. 'domain' => BX_DOL_URL_ROOT,
  297. 'products' => $this->getInstalledInfoShort(),
  298. 'per_page' => $iPerPage
  299. ));
  300. else
  301. $aProducts = $this->getAccessObject(false)->load($this->sStoreDataUrlPublic . 'json_browse_purchased', array('key' => getParam('sys_oauth_key'), 'per_page' => $iPerPage));
  302. if(empty($aProducts) || !is_array($aProducts))
  303. return $aProducts;
  304. $oModuleDb = BxDolModuleQuery::getInstance();
  305. $oModuleDb->updateModule(array('hash' => ''));
  306. if(!empty($aProducts) && is_array($aProducts))
  307. foreach ($aProducts as $aProduct)
  308. if(!empty($aProduct['hash']))
  309. $oModuleDb->updateModule(array('hash' => $aProduct['hash']), array('name' => $aProduct['name']));
  310. return $aProducts;
  311. }
  312. public function checkUpdates($bAuthorizedAccess = false)
  313. {
  314. $sProducts = $this->getInstalledInfoShort();
  315. if($bAuthorizedAccess)
  316. return $this->getAccessObject(true)->loadItems(array(
  317. 'method' => 'browse_updates',
  318. 'products' => $sProducts
  319. ));
  320. return $this->getAccessObject(false)->load($this->sStoreDataUrlPublic . 'json_browse_updates', array(
  321. 'key' => getParam('sys_oauth_key'),
  322. 'products' => $sProducts
  323. ));
  324. }
  325. public function downloadFileAuthorized($iFileId)
  326. {
  327. $aItem = $this->getAccessObject(true)->loadItems(array(
  328. 'method' => 'download_file',
  329. 'file_id' => $iFileId
  330. ));
  331. if(empty($aItem) || !is_array($aItem))
  332. return $aItem;
  333. return $this->downloadFileInit($aItem, array('module_name' => $aItem['module_name']));
  334. }
  335. public function downloadUpdatePublic($sModuleName, $bApplyUpdate = false)
  336. {
  337. $aModule = BxDolModuleQuery::getInstance()->getModuleByName($sModuleName);
  338. $aItem = $this->getAccessObject(false)->load($this->sStoreDataUrlPublic . 'json_download_update', array(
  339. 'key' => getParam('sys_oauth_key'),
  340. 'product' => base64_encode(serialize(array(
  341. 'name' => $aModule['name'],
  342. 'version' => $aModule['version'],
  343. 'hash' => $aModule['hash'],
  344. ))),
  345. ));
  346. return $this->downloadFileInit($aItem, array('module_name' => $sModuleName, 'auto_action' => $bApplyUpdate ? 'update' : ''));
  347. }
  348. protected function downloadFileInit($aItem, $aParams = array())
  349. {
  350. if(empty($aItem) || !is_array($aItem))
  351. return $aItem;
  352. //--- write ZIP archive.
  353. $sFilePath = '';
  354. $mixedResult = $this->performWrite($aItem, $sFilePath);
  355. if($mixedResult !== true)
  356. return $mixedResult;
  357. return $this->downloadFileComplete($sFilePath, $aParams);
  358. }
  359. protected function downloadFileComplete($sFilePath, $aParams = array())
  360. {
  361. $bTransient = false;
  362. if(isset($aParams['transient'])) {
  363. $bTransient = (bool)$aParams['transient'];
  364. unset($aParams['transient']);
  365. }
  366. $sAutoAction = '';
  367. $bAutoAction = false;
  368. if(isset($aParams['auto_action'])) {
  369. $sAutoAction = $aParams['auto_action'];
  370. $bAutoAction = !empty($sAutoAction);
  371. unset($aParams['auto_action']);
  372. }
  373. if(!defined('BX_DOL_CRON_EXECUTE') && !self::isRealOwner()) {
  374. if(!$this->addTransientJob(self::getNameDownloadFile($aParams['module_name']), 'download_file_complete', array($sFilePath, array_merge($aParams, array('auto_action' => $sAutoAction)))))
  375. return _t('_adm_str_err_download_failed');
  376. return array('code' => BX_DOL_STUDIO_IU_RC_SCHEDULED, 'message' => _t('_adm_str_msg_download' . ($bAutoAction ? '_and_install' : '') . '_scheduled'));
  377. }
  378. //--- Unarchive package.
  379. $sPackagePath = '';
  380. $mixedResult = $this->performUnarchive($sFilePath, $sPackagePath);
  381. @unlink($sFilePath);
  382. if($mixedResult !== true) {
  383. $this->_oLog->write('Operation failed:', $mixedResult);
  384. if($bTransient)
  385. $this->emailNotify($mixedResult);
  386. return $mixedResult;
  387. }
  388. //--- Copy unarchived package.
  389. $sHomePath = '';
  390. $mixedResult = $this->performCopy($sPackagePath, $sHomePath);
  391. @bx_rrmdir($sPackagePath);
  392. if($mixedResult !== true) {
  393. $this->_oLog->write('Operation failed:', $mixedResult);
  394. if($bTransient)
  395. $this->emailNotify($mixedResult);
  396. return $mixedResult;
  397. }
  398. if(!$bAutoAction)
  399. return true;
  400. //--- Autoinstall the downloaded package if it's needed.
  401. $aResult = $this->perform($sHomePath, $sAutoAction, $aParams);
  402. if((int)$aResult['code'] != 0) {
  403. if($bTransient)
  404. $this->emailNotify($aResult['message']);
  405. return $aResult['message'];
  406. }
  407. return true;
  408. }
  409. protected function performWrite($aItem, &$sFilePath)
  410. {
  411. $sFilePath = BX_DIRECTORY_PATH_TMP . $aItem['name'];
  412. if(file_exists($sFilePath))
  413. @unlink($sFilePath);
  414. $iUmaskSave = umask(0);
  415. if(!$rHandler = fopen($sFilePath, 'w')) {
  416. umask($iUmaskSave);
  417. return _t('_adm_str_err_cannot_write');
  418. }
  419. $sContent = urldecode($aItem['content']);
  420. if(!fwrite($rHandler, $sContent, strlen($sContent))) {
  421. umask($iUmaskSave);
  422. return _t('_adm_str_err_cannot_write');
  423. }
  424. fclose($rHandler);
  425. umask($iUmaskSave);
  426. return true;
  427. }
  428. protected function performUnarchive($sFilePath, &$sPackagePath)
  429. {
  430. if(!class_exists('ZipArchive'))
  431. return _t('_adm_str_err_zip_not_available');
  432. $iUmaskSave = umask(0);
  433. $oZip = new ZipArchive();
  434. if($oZip->open($sFilePath) !== true) {
  435. umask($iUmaskSave);
  436. return _t('_adm_str_err_cannot_unzip_package');
  437. }
  438. $sPackageFolder = '';
  439. if($oZip->numFiles > 0)
  440. $sPackageFolder = $oZip->getNameIndex(0);
  441. if(empty($sPackageFolder)) {
  442. umask($iUmaskSave);
  443. return _t('_adm_str_err_cannot_unzip_package');
  444. }
  445. $sPackagePath = BX_DIRECTORY_PATH_TMP . $sPackageFolder;
  446. if(file_exists($sPackagePath)) // remove existing tmp folder with the same name
  447. @bx_rrmdir($sPackagePath);
  448. if(!$oZip->extractTo(BX_DIRECTORY_PATH_TMP)) {
  449. umask($iUmaskSave);
  450. return _t('_adm_str_err_cannot_unzip_package');
  451. }
  452. $oZip->close();
  453. umask($iUmaskSave);
  454. return true;
  455. }
  456. protected function performCopy($sPackagePath, &$sHomePath)
  457. {
  458. if($this->bUseFtp) {
  459. $sLogin = getParam('sys_ftp_login');
  460. $sPassword = getParam('sys_ftp_password');
  461. $sPath = getParam('sys_ftp_dir');
  462. if(empty($sLogin) || empty($sPassword) || empty($sPath))
  463. return _t('_adm_str_err_no_ftp_info');
  464. $oFile = new BxDolFtp($_SERVER['HTTP_HOST'], $sLogin, $sPassword, $sPath);
  465. if(!$oFile->connect())
  466. return _t('_adm_str_err_cannot_connect_to_ftp');
  467. if(!$oFile->isUna())
  468. return _t('_adm_str_err_destination_not_valid');
  469. }
  470. else
  471. $oFile = BxDolFile::getInstance();
  472. $aConfig = self::getModuleConfig($sPackagePath . 'install/config.php');
  473. if(empty($aConfig) || !is_array($aConfig) || empty($aConfig['home_dir']))
  474. return _t('_adm_str_err_wrong_package_format');
  475. $sHomePath = $aConfig['home_dir'];
  476. if(!$oFile->copy($sPackagePath, 'modules/' . $sHomePath))
  477. return _t('_adm_str_err_files_copy_failed');
  478. return true;
  479. }
  480. protected function getConfigModule($sConfigPath, $aInstalled = array())
  481. {
  482. $aConfig = self::getModuleConfig($sConfigPath);
  483. if(empty($aConfig) || !is_array($aConfig))
  484. return array();
  485. $bInstalled = !empty($aInstalled);
  486. $bEnabled = $bInstalled && (int)$aInstalled['enabled'] == 1;
  487. $sVersion = $bInstalled ? $aInstalled['version'] : $aConfig['version'];
  488. return array(
  489. 'type' => $aConfig['type'],
  490. 'name' => isset($aConfig['name']) ? $aConfig['name'] : $aConfig['home_uri'],
  491. 'title' => bx_process_output($aConfig['title']),
  492. 'vendor' => $aConfig['vendor'],
  493. 'version' => $sVersion,
  494. 'uri' => $aConfig['home_uri'],
  495. 'dir' => $aConfig['home_dir'],
  496. 'note' => isset($aConfig['note']) ? bx_process_output($aConfig['note']) : '',
  497. 'installed' => $bInstalled,
  498. 'enabled' => $bEnabled
  499. );
  500. }
  501. protected function getConfigUpdate($sConfigPathModule, $sConfigPathUpdate, $aInstalledModule = array())
  502. {
  503. $aModule = $this->getConfigModule($sConfigPathModule, $aInstalledModule);
  504. if(empty($aModule) || !$aModule['installed'])
  505. return array();
  506. $aConfig = self::getModuleConfig($sConfigPathUpdate);
  507. if(empty($aConfig) || !is_array($aConfig))
  508. return array();
  509. return array(
  510. 'title' => bx_process_output($aConfig['title']),
  511. 'vendor' => $aConfig['vendor'],
  512. 'version_from' => $aConfig['version_from'],
  513. 'version_to' => $aConfig['version_to'],
  514. 'dir' => $aConfig['home_dir'],
  515. 'module_type' => $aModule['type'],
  516. 'module_name' => $aModule['name'],
  517. 'module_dir' => $aModule['dir'],
  518. 'module_version' => $aModule['version']
  519. );
  520. }
  521. private function getInstalledInfo()
  522. {
  523. $aModules = BxDolModuleQuery::getInstance()->getModules();
  524. $aInstalledInfo = array();
  525. foreach($aModules as $aModule)
  526. $aInstalledInfo[$aModule['path']] = $aModule;
  527. return $aInstalledInfo;
  528. }
  529. private function getInstalledInfoShort()
  530. {
  531. $aModules = BxDolModuleQuery::getInstance()->getModules();
  532. $aProducts = array();
  533. foreach($aModules as $aModule) {
  534. if(empty($aModule['name']))
  535. continue;
  536. $aProducts[$aModule['name']] = array(
  537. 'name' => $aModule['name'],
  538. 'version' => $aModule['version'],
  539. 'hash' => $aModule['hash'],
  540. );
  541. }
  542. return base64_encode(serialize($aProducts));
  543. }
  544. private function addTransientJob($sName, $sAction, $aParams)
  545. {
  546. if(BxDolCronQuery::getInstance()->addTransientJobService($sName, array('system', $sAction, $aParams, 'DolStudioInstallerUtils')))
  547. return true;
  548. return false;
  549. }
  550. private function emailNotify($sMessage)
  551. {
  552. sendMailTemplateSystem('t_BgOperationFailed', array (
  553. 'conclusion' => strip_tags($sMessage),
  554. ));
  555. }
  556. private function emailNotifyModulesUpgrade($sResult, $aData)
  557. {
  558. $oModuleQuery = BxDolModuleQuery::getInstance();
  559. $sConclusion = '';
  560. if(!empty($aData))
  561. foreach($aData as $sModule => $sMessage) {
  562. $aModule = $oModuleQuery->getModuleByName($sModule);
  563. $sConclusion .= _t('_sys_et_txt_body_modules_upgrade_' . $sResult, $aModule['title'], $sMessage);
  564. }
  565. sendMailTemplateSystem('t_UpgradeModules' . ucfirst($sResult), array (
  566. 'conclusion' => $sConclusion,
  567. ));
  568. }
  569. }
  570. /** @} */