BxDolStudioInstallerUtils.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  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(strtolower($aUpdate['module_version']), strtolower($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. if(empty($aParams['module_name']) && !empty($aConfig['name']))
  207. $aParams['module_name'] = $aConfig['name'];
  208. $sPathInstaller = BX_DIRECTORY_PATH_MODULES . $sDirectory . $sInstallerFile;
  209. if(empty($aConfig) || !file_exists($sPathInstaller)) {
  210. $sMessage = _t('_adm_mod_err_process_operation_failed', $sOperation, $sDirectory);
  211. $this->writeLog($sMessage, $aParams);
  212. if($bTransient)
  213. $this->emailNotify($sMessage, $aParams);
  214. return array('code' => BX_DOL_STUDIO_IU_RC_FAILED, 'message' => $sMessage);
  215. }
  216. require_once($sPathInstaller);
  217. $sClassName = $aConfig['class_prefix'] . $sInstallerClass;
  218. $oInstaller = new $sClassName($aConfig);
  219. $aResult = $oInstaller->$sOperation($aParams);
  220. if(!$aResult['result']) {
  221. $this->writeLog($aResult['message'], $aParams);
  222. if($bTransient)
  223. $this->emailNotify($aResult['message'], $aParams);
  224. }
  225. return array(
  226. 'code' => isset($aResult['code']) ? $aResult['code'] : ($aResult['result'] ? BX_DOL_STUDIO_IU_RC_SUCCESS : BX_DOL_STUDIO_IU_RC_FAILED),
  227. 'message' => $aResult['message']
  228. );
  229. }
  230. /**
  231. * Update modules using cron based auto updates.
  232. * @see BxDolCronUpgradeModulesCheck
  233. */
  234. public function performModulesUpgrade($aParams = array())
  235. {
  236. $bDirectly = true;
  237. if(isset($aParams['directly'])) {
  238. $bDirectly = (bool)$aParams['directly'];
  239. unset($aParams['directly']);
  240. }
  241. $bTransient = true;
  242. if(isset($aParams['transient'])) {
  243. $bTransient = (bool)$aParams['transient'];
  244. unset($aParams['transient']);
  245. }
  246. if(!defined('BX_DOL_CRON_EXECUTE') && !$bDirectly)
  247. return $this->addTransientJob(self::getNamePerformModulesUpgrade(), 'perform_modules_upgrade', array($aParams));
  248. $aFailed = array();
  249. $aUpdates = $this->checkUpdates();
  250. if(empty($aUpdates) || !is_array($aUpdates)) {
  251. $this->_oLog->write('Cannot get a list of modules which require to be updated.');
  252. return true;
  253. }
  254. foreach($aUpdates as $aUpdate) {
  255. $mixedResult = $this->downloadUpdatePublic($aUpdate['name']);
  256. if($mixedResult !== true) {
  257. $aFailed[$aUpdate['name']] = $mixedResult;
  258. continue;
  259. }
  260. }
  261. $aSuccess = array();
  262. $aUpdates = $this->getUpdates();
  263. if(empty($aUpdates) || !is_array($aUpdates)) {
  264. $this->_oLog->write('Cannot find update scripts for modules. They are damaged or were not downloaded.');
  265. return true;
  266. }
  267. foreach($aUpdates as $aUpdate) {
  268. $aParams['module_name'] = $aUpdate['module_name'];
  269. $aResult = $this->perform($aUpdate['dir'], 'update', $aParams);
  270. switch((int)$aResult['code']) {
  271. case BX_DOL_STUDIO_IU_RC_SUCCESS:
  272. $aSuccess[$aUpdate['module_name']] = $aUpdate['version_to'];
  273. break;
  274. case BX_DOL_STUDIO_IU_RCE_WSV_MU:
  275. if(isset($aParams['autoupdate']) && (bool)$aParams['autoupdate'] === true)
  276. break;
  277. default:
  278. $aFailed[$aUpdate['module_name']] = $aResult['message'];
  279. }
  280. }
  281. if(!empty($aFailed)) {
  282. $this->_oLog->write('Failed to update modules:', $aFailed);
  283. if($bTransient)
  284. $this->emailNotifyModulesUpgrade('failed', $aFailed);
  285. }
  286. if(!empty($aSuccess)) {
  287. $this->_oLog->write('Successfully updated modules:', $aSuccess);
  288. if($bTransient)
  289. $this->emailNotifyModulesUpgrade('success', $aSuccess);
  290. }
  291. return empty($aFailed);
  292. }
  293. public function checkModules($bAuthorizedAccess = false)
  294. {
  295. $iPerPage = 9999; //--- Note. It's essential to load all purchased products at the same time.
  296. if($bAuthorizedAccess)
  297. $aProducts = $this->getAccessObject(true)->loadItems(array(
  298. 'method' => 'browse_purchased',
  299. 'domain' => BX_DOL_URL_ROOT,
  300. 'products' => $this->getInstalledInfoShort(),
  301. 'per_page' => $iPerPage
  302. ));
  303. else
  304. $aProducts = $this->getAccessObject(false)->load($this->sStoreDataUrlPublic . 'json_browse_purchased', array('key' => getParam('sys_oauth_key'), 'per_page' => $iPerPage));
  305. if(empty($aProducts) || !is_array($aProducts))
  306. return $aProducts;
  307. $oModuleDb = BxDolModuleQuery::getInstance();
  308. $oModuleDb->updateModule(array('hash' => ''));
  309. if(!empty($aProducts) && is_array($aProducts))
  310. foreach ($aProducts as $aProduct)
  311. if(!empty($aProduct['hash']))
  312. $oModuleDb->updateModule(array('hash' => $aProduct['hash']), array('name' => $aProduct['name']));
  313. return $aProducts;
  314. }
  315. public function checkUpdates($bAuthorizedAccess = false)
  316. {
  317. $sVersion = bx_get_ver();
  318. $sProducts = $this->getInstalledInfoShort();
  319. if($bAuthorizedAccess)
  320. return $this->getAccessObject(true)->loadItems(array(
  321. 'method' => 'browse_updates',
  322. 'version' => $sVersion,
  323. 'products' => $sProducts
  324. ));
  325. return $this->getAccessObject(false)->load($this->sStoreDataUrlPublic . 'json_browse_updates', array(
  326. 'key' => getParam('sys_oauth_key'),
  327. 'version' => $sVersion,
  328. 'products' => $sProducts
  329. ));
  330. }
  331. public function downloadFileAuthorized($iFileId)
  332. {
  333. $aItem = $this->getAccessObject(true)->loadItems(array(
  334. 'method' => 'download_file',
  335. 'file_id' => $iFileId
  336. ));
  337. if(empty($aItem) || !is_array($aItem))
  338. return $aItem;
  339. return $this->downloadFileInit($aItem, array('module_name' => $aItem['module_name']));
  340. }
  341. public function downloadUpdatePublic($sModuleName, $bApplyUpdate = false)
  342. {
  343. $aModule = BxDolModuleQuery::getInstance()->getModuleByName($sModuleName);
  344. $aItem = $this->getAccessObject(false)->load($this->sStoreDataUrlPublic . 'json_download_update', array(
  345. 'key' => getParam('sys_oauth_key'),
  346. 'version' => bx_get_ver(),
  347. 'product' => base64_encode(serialize(array(
  348. 'name' => $aModule['name'],
  349. 'version' => $aModule['version'],
  350. 'hash' => $aModule['hash'],
  351. ))),
  352. ));
  353. return $this->downloadFileInit($aItem, array('module_name' => $sModuleName, 'auto_action' => $bApplyUpdate ? 'update' : ''));
  354. }
  355. protected function downloadFileInit($aItem, $aParams = array())
  356. {
  357. if(empty($aItem) || !is_array($aItem))
  358. return $aItem;
  359. //--- write ZIP archive.
  360. $sFilePath = '';
  361. $mixedResult = $this->performWrite($aItem, $sFilePath);
  362. if($mixedResult !== true)
  363. return $mixedResult;
  364. return $this->downloadFileComplete($sFilePath, $aParams);
  365. }
  366. protected function downloadFileComplete($sFilePath, $aParams = array())
  367. {
  368. $bTransient = false;
  369. if(isset($aParams['transient'])) {
  370. $bTransient = (bool)$aParams['transient'];
  371. unset($aParams['transient']);
  372. }
  373. $sAutoAction = '';
  374. $bAutoAction = false;
  375. if(isset($aParams['auto_action'])) {
  376. $sAutoAction = $aParams['auto_action'];
  377. $bAutoAction = !empty($sAutoAction);
  378. unset($aParams['auto_action']);
  379. }
  380. if(!defined('BX_DOL_CRON_EXECUTE') && !self::isRealOwner()) {
  381. if(!$this->addTransientJob(self::getNameDownloadFile($aParams['module_name']), 'download_file_complete', array($sFilePath, array_merge($aParams, array('auto_action' => $sAutoAction)))))
  382. return array('code' => BX_DOL_STUDIO_IU_RC_FAILED, 'message' => _t('_adm_str_err_download_failed'));
  383. return array('code' => BX_DOL_STUDIO_IU_RC_SCHEDULED, 'message' => _t('_adm_str_msg_download' . ($bAutoAction ? '_and_install' : '') . '_scheduled'));
  384. }
  385. //--- Unarchive package.
  386. $sPackagePath = '';
  387. $mixedResult = $this->performUnarchive($sFilePath, $sPackagePath);
  388. @unlink($sFilePath);
  389. if($mixedResult !== true) {
  390. $this->writeLog($mixedResult, $aParams);
  391. if($bTransient)
  392. $this->emailNotify($mixedResult, $aParams);
  393. return array('code' => BX_DOL_STUDIO_IU_RC_FAILED, 'message' => $mixedResult);
  394. }
  395. //--- Copy unarchived package.
  396. $sHomePath = '';
  397. $mixedResult = $this->performCopy($sPackagePath, $sHomePath);
  398. @bx_rrmdir($sPackagePath);
  399. if($mixedResult !== true) {
  400. $this->writeLog($mixedResult, $aParams);
  401. if($bTransient)
  402. $this->emailNotify($mixedResult, $aParams);
  403. return array('code' => BX_DOL_STUDIO_IU_RC_FAILED, 'message' => $mixedResult);
  404. }
  405. if(!$bAutoAction)
  406. return true;
  407. //--- Autoinstall the downloaded package if it's needed.
  408. $aResult = $this->perform($sHomePath, $sAutoAction, $aParams);
  409. if((int)$aResult['code'] == 0)
  410. return true;
  411. if($bTransient)
  412. $this->emailNotify($aResult['message'], $aParams);
  413. return array('code' => BX_DOL_STUDIO_IU_RC_FAILED, 'message' => $aResult['message']);
  414. }
  415. protected function performWrite($aItem, &$sFilePath)
  416. {
  417. $sFilePath = BX_DIRECTORY_PATH_TMP . $aItem['name'];
  418. if(file_exists($sFilePath))
  419. @unlink($sFilePath);
  420. $iUmaskSave = umask(0);
  421. if(!$rHandler = fopen($sFilePath, 'w')) {
  422. umask($iUmaskSave);
  423. return _t('_adm_str_err_cannot_write');
  424. }
  425. $sContent = urldecode($aItem['content']);
  426. if(!fwrite($rHandler, $sContent, strlen($sContent))) {
  427. umask($iUmaskSave);
  428. return _t('_adm_str_err_cannot_write');
  429. }
  430. fclose($rHandler);
  431. umask($iUmaskSave);
  432. return true;
  433. }
  434. protected function performUnarchive($sFilePath, &$sPackagePath)
  435. {
  436. if(!class_exists('ZipArchive'))
  437. return _t('_adm_str_err_zip_not_available');
  438. $iUmaskSave = umask(0);
  439. $oZip = new ZipArchive();
  440. if($oZip->open($sFilePath) !== true) {
  441. umask($iUmaskSave);
  442. return _t('_adm_str_err_cannot_unzip_package');
  443. }
  444. $sPackageFolder = '';
  445. if($oZip->numFiles > 0)
  446. $sPackageFolder = $oZip->getNameIndex(0);
  447. if(empty($sPackageFolder)) {
  448. umask($iUmaskSave);
  449. return _t('_adm_str_err_cannot_unzip_package');
  450. }
  451. $sPackagePath = BX_DIRECTORY_PATH_TMP . $sPackageFolder;
  452. if(file_exists($sPackagePath)) // remove existing tmp folder with the same name
  453. @bx_rrmdir($sPackagePath);
  454. if(!$oZip->extractTo(BX_DIRECTORY_PATH_TMP)) {
  455. umask($iUmaskSave);
  456. return _t('_adm_str_err_cannot_unzip_package');
  457. }
  458. $oZip->close();
  459. umask($iUmaskSave);
  460. return true;
  461. }
  462. protected function performCopy($sPackagePath, &$sHomePath)
  463. {
  464. if($this->bUseFtp) {
  465. $sLogin = getParam('sys_ftp_login');
  466. $sPassword = getParam('sys_ftp_password');
  467. $sPath = getParam('sys_ftp_dir');
  468. if(empty($sLogin) || empty($sPassword) || empty($sPath))
  469. return _t('_adm_str_err_no_ftp_info');
  470. $oFile = new BxDolFtp($_SERVER['HTTP_HOST'], $sLogin, $sPassword, $sPath);
  471. if(!$oFile->connect())
  472. return _t('_adm_str_err_cannot_connect_to_ftp');
  473. if(!$oFile->isUna())
  474. return _t('_adm_str_err_destination_not_valid');
  475. }
  476. else
  477. $oFile = BxDolFile::getInstance();
  478. $aConfig = self::getModuleConfig($sPackagePath . 'install/config.php');
  479. if(empty($aConfig) || !is_array($aConfig) || empty($aConfig['home_dir']))
  480. return _t('_adm_str_err_wrong_package_format');
  481. $sHomePath = $aConfig['home_dir'];
  482. if(!$oFile->copy($sPackagePath, 'modules/' . $sHomePath))
  483. return _t('_adm_str_err_files_copy_failed');
  484. return true;
  485. }
  486. protected function getConfigModule($sConfigPath, $aInstalled = array())
  487. {
  488. $aConfig = self::getModuleConfig($sConfigPath);
  489. if(empty($aConfig) || !is_array($aConfig))
  490. return array();
  491. $bInstalled = !empty($aInstalled);
  492. $bEnabled = $bInstalled && (int)$aInstalled['enabled'] == 1;
  493. $sVersion = $bInstalled ? $aInstalled['version'] : $aConfig['version'];
  494. return array(
  495. 'type' => $aConfig['type'],
  496. 'name' => isset($aConfig['name']) ? $aConfig['name'] : $aConfig['home_uri'],
  497. 'title' => bx_process_output($aConfig['title']),
  498. 'vendor' => $aConfig['vendor'],
  499. 'version' => $sVersion,
  500. 'uri' => $aConfig['home_uri'],
  501. 'dir' => $aConfig['home_dir'],
  502. 'note' => isset($aConfig['note']) ? bx_process_output($aConfig['note']) : '',
  503. 'installed' => $bInstalled,
  504. 'enabled' => $bEnabled
  505. );
  506. }
  507. protected function getConfigUpdate($sConfigPathModule, $sConfigPathUpdate, $aInstalledModule = array())
  508. {
  509. $aModule = $this->getConfigModule($sConfigPathModule, $aInstalledModule);
  510. if(empty($aModule) || !$aModule['installed'])
  511. return array();
  512. $aConfig = self::getModuleConfig($sConfigPathUpdate);
  513. if(empty($aConfig) || !is_array($aConfig))
  514. return array();
  515. return array(
  516. 'title' => bx_process_output($aConfig['title']),
  517. 'vendor' => $aConfig['vendor'],
  518. 'version_from' => $aConfig['version_from'],
  519. 'version_to' => $aConfig['version_to'],
  520. 'dir' => $aConfig['home_dir'],
  521. 'module_type' => $aModule['type'],
  522. 'module_name' => $aModule['name'],
  523. 'module_dir' => $aModule['dir'],
  524. 'module_version' => $aModule['version']
  525. );
  526. }
  527. private function getInstalledInfo()
  528. {
  529. $aModules = BxDolModuleQuery::getInstance()->getModules();
  530. $aInstalledInfo = array();
  531. foreach($aModules as $aModule)
  532. $aInstalledInfo[$aModule['path']] = $aModule;
  533. return $aInstalledInfo;
  534. }
  535. private function getInstalledInfoShort()
  536. {
  537. $aModules = BxDolModuleQuery::getInstance()->getModules();
  538. $aProducts = array();
  539. foreach($aModules as $aModule) {
  540. if(empty($aModule['name']))
  541. continue;
  542. $aProducts[$aModule['name']] = array(
  543. 'name' => $aModule['name'],
  544. 'version' => $aModule['version'],
  545. 'hash' => $aModule['hash'],
  546. );
  547. }
  548. return base64_encode(serialize($aProducts));
  549. }
  550. private function addTransientJob($sName, $sAction, $aParams)
  551. {
  552. if(BxDolCronQuery::getInstance()->addTransientJobService($sName, array('system', $sAction, $aParams, 'DolStudioInstallerUtils')))
  553. return true;
  554. return false;
  555. }
  556. private function writeLog($sMessage, $aParams = array())
  557. {
  558. $this->_oLog->write('Operation failed:', $this->getModuleTitle($aParams) . $sMessage);
  559. }
  560. private function emailNotify($sMessage, $aParams = array())
  561. {
  562. sendMailTemplateSystem('t_BgOperationFailed', array (
  563. 'conclusion' => strip_tags($this->getModuleTitle($aParams) . $sMessage),
  564. ));
  565. }
  566. private function getModuleTitle($aParams = array())
  567. {
  568. if(empty($aParams['module_name']))
  569. return '';
  570. $sTitleKey = '_' . $aParams['module_name'];
  571. $sTitleValue = _t($sTitleKey);
  572. if(strcmp($sTitleKey, $sTitleValue) == 0) {
  573. $aModule = BxDolModuleQuery::getInstance()->getModuleByName($aParams['module_name']);
  574. $sTitleValue = is_array($aModule) && !empty($aModule['title']) ? $aModule['title'] : $aParams['module_name'];
  575. }
  576. return $sTitleValue . ': ';
  577. }
  578. private function emailNotifyModulesUpgrade($sResult, $aData)
  579. {
  580. $oModuleQuery = BxDolModuleQuery::getInstance();
  581. $sConclusion = '';
  582. if(!empty($aData))
  583. foreach($aData as $sModule => $sMessage) {
  584. $aModule = $oModuleQuery->getModuleByName($sModule);
  585. $sConclusion .= _t('_sys_et_txt_body_modules_upgrade_' . $sResult, $aModule['title'], $sMessage);
  586. }
  587. sendMailTemplateSystem('t_UpgradeModules' . ucfirst($sResult), array (
  588. 'conclusion' => $sConclusion,
  589. ));
  590. }
  591. }
  592. /** @} */