BxDolStudioInstallerUtils.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  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. $sVersion = bx_get_ver();
  297. if($bAuthorizedAccess)
  298. $aProducts = $this->getAccessObject(true)->loadItems(array(
  299. 'method' => 'browse_purchased',
  300. 'domain' => BX_DOL_URL_ROOT,
  301. 'version' => $sVersion,
  302. 'products' => $this->getInstalledInfoShort(),
  303. 'per_page' => $iPerPage
  304. ));
  305. else
  306. $aProducts = $this->getAccessObject(false)->load($this->sStoreDataUrlPublic . 'json_browse_purchased', array('key' => getParam('sys_oauth_key'), 'per_page' => $iPerPage));
  307. if(empty($aProducts) || !is_array($aProducts))
  308. return $aProducts;
  309. $oModuleDb = BxDolModuleQuery::getInstance();
  310. $oModuleDb->updateModule(array('hash' => ''));
  311. if(!empty($aProducts) && is_array($aProducts))
  312. foreach ($aProducts as $aProduct)
  313. if(!empty($aProduct['hash']))
  314. $oModuleDb->updateModule(array('hash' => $aProduct['hash']), array('name' => $aProduct['name']));
  315. return $aProducts;
  316. }
  317. public function checkUpdates($bAuthorizedAccess = false)
  318. {
  319. $sVersion = bx_get_ver();
  320. $sProducts = $this->getInstalledInfoShort();
  321. if($bAuthorizedAccess)
  322. return $this->getAccessObject(true)->loadItems(array(
  323. 'method' => 'browse_updates',
  324. 'version' => $sVersion,
  325. 'products' => $sProducts
  326. ));
  327. return $this->getAccessObject(false)->load($this->sStoreDataUrlPublic . 'json_browse_updates', array(
  328. 'key' => getParam('sys_oauth_key'),
  329. 'version' => $sVersion,
  330. 'products' => $sProducts
  331. ));
  332. }
  333. public function downloadFileAuthorized($iFileId)
  334. {
  335. $aItem = $this->getAccessObject(true)->loadItems(array(
  336. 'method' => 'download_file',
  337. 'file_id' => $iFileId
  338. ));
  339. if(empty($aItem) || !is_array($aItem))
  340. return $aItem;
  341. return $this->downloadFileInit($aItem, array('module_name' => $aItem['module_name']));
  342. }
  343. public function downloadUpdatePublic($sModuleName, $bApplyUpdate = false)
  344. {
  345. $aModule = BxDolModuleQuery::getInstance()->getModuleByName($sModuleName);
  346. $aItem = $this->getAccessObject(false)->load($this->sStoreDataUrlPublic . 'json_download_update', array(
  347. 'key' => getParam('sys_oauth_key'),
  348. 'version' => bx_get_ver(),
  349. 'product' => base64_encode(serialize(array(
  350. 'name' => $aModule['name'],
  351. 'version' => $aModule['version'],
  352. 'hash' => $aModule['hash'],
  353. ))),
  354. ));
  355. return $this->downloadFileInit($aItem, array('module_name' => $sModuleName, 'auto_action' => $bApplyUpdate ? 'update' : ''));
  356. }
  357. protected function downloadFileInit($aItem, $aParams = array())
  358. {
  359. if(empty($aItem) || !is_array($aItem))
  360. return $aItem;
  361. //--- write ZIP archive.
  362. $sFilePath = '';
  363. $mixedResult = $this->performWrite($aItem, $sFilePath);
  364. if($mixedResult !== true)
  365. return $mixedResult;
  366. return $this->downloadFileComplete($sFilePath, $aParams);
  367. }
  368. protected function downloadFileComplete($sFilePath, $aParams = array())
  369. {
  370. $bTransient = false;
  371. if(isset($aParams['transient'])) {
  372. $bTransient = (bool)$aParams['transient'];
  373. unset($aParams['transient']);
  374. }
  375. $sAutoAction = '';
  376. $bAutoAction = false;
  377. if(isset($aParams['auto_action'])) {
  378. $sAutoAction = $aParams['auto_action'];
  379. $bAutoAction = !empty($sAutoAction);
  380. unset($aParams['auto_action']);
  381. }
  382. if(!defined('BX_DOL_CRON_EXECUTE') && !self::isRealOwner()) {
  383. if(!$this->addTransientJob(self::getNameDownloadFile($aParams['module_name']), 'download_file_complete', array($sFilePath, array_merge($aParams, array('auto_action' => $sAutoAction)))))
  384. return array('code' => BX_DOL_STUDIO_IU_RC_FAILED, 'message' => _t('_adm_str_err_download_failed'));
  385. return array('code' => BX_DOL_STUDIO_IU_RC_SCHEDULED, 'message' => _t('_adm_str_msg_download' . ($bAutoAction ? '_and_install' : '') . '_scheduled'));
  386. }
  387. //--- Unarchive package.
  388. $sPackagePath = '';
  389. $mixedResult = $this->performUnarchive($sFilePath, $sPackagePath);
  390. @unlink($sFilePath);
  391. if($mixedResult !== true) {
  392. $this->writeLog($mixedResult, $aParams);
  393. if($bTransient)
  394. $this->emailNotify($mixedResult, $aParams);
  395. return array('code' => BX_DOL_STUDIO_IU_RC_FAILED, 'message' => $mixedResult);
  396. }
  397. //--- Copy unarchived package.
  398. $sHomePath = '';
  399. $mixedResult = $this->performCopy($sPackagePath, $sHomePath);
  400. @bx_rrmdir($sPackagePath);
  401. if($mixedResult !== true) {
  402. $this->writeLog($mixedResult, $aParams);
  403. if($bTransient)
  404. $this->emailNotify($mixedResult, $aParams);
  405. return array('code' => BX_DOL_STUDIO_IU_RC_FAILED, 'message' => $mixedResult);
  406. }
  407. if(!$bAutoAction)
  408. return true;
  409. //--- Autoinstall the downloaded package if it's needed.
  410. $aResult = $this->perform($sHomePath, $sAutoAction, $aParams);
  411. if((int)$aResult['code'] == 0)
  412. return true;
  413. if($bTransient)
  414. $this->emailNotify($aResult['message'], $aParams);
  415. return array('code' => BX_DOL_STUDIO_IU_RC_FAILED, 'message' => $aResult['message']);
  416. }
  417. protected function performWrite($aItem, &$sFilePath)
  418. {
  419. $sFilePath = BX_DIRECTORY_PATH_TMP . $aItem['name'];
  420. if(file_exists($sFilePath))
  421. @unlink($sFilePath);
  422. $iUmaskSave = umask(0);
  423. if(!$rHandler = fopen($sFilePath, 'w')) {
  424. umask($iUmaskSave);
  425. return _t('_adm_str_err_cannot_write');
  426. }
  427. $sContent = urldecode($aItem['content']);
  428. if(!fwrite($rHandler, $sContent, strlen($sContent))) {
  429. umask($iUmaskSave);
  430. return _t('_adm_str_err_cannot_write');
  431. }
  432. fclose($rHandler);
  433. umask($iUmaskSave);
  434. return true;
  435. }
  436. protected function performUnarchive($sFilePath, &$sPackagePath)
  437. {
  438. if(!class_exists('ZipArchive'))
  439. return _t('_adm_str_err_zip_not_available');
  440. $iUmaskSave = umask(0);
  441. $oZip = new ZipArchive();
  442. if($oZip->open($sFilePath) !== true) {
  443. umask($iUmaskSave);
  444. return _t('_adm_str_err_cannot_unzip_package');
  445. }
  446. $sPackageFolder = '';
  447. if($oZip->numFiles > 0)
  448. $sPackageFolder = $oZip->getNameIndex(0);
  449. if(empty($sPackageFolder)) {
  450. umask($iUmaskSave);
  451. return _t('_adm_str_err_cannot_unzip_package');
  452. }
  453. $sPackagePath = BX_DIRECTORY_PATH_TMP . $sPackageFolder;
  454. if(file_exists($sPackagePath)) // remove existing tmp folder with the same name
  455. @bx_rrmdir($sPackagePath);
  456. if(!$oZip->extractTo(BX_DIRECTORY_PATH_TMP)) {
  457. umask($iUmaskSave);
  458. return _t('_adm_str_err_cannot_unzip_package');
  459. }
  460. $oZip->close();
  461. umask($iUmaskSave);
  462. return true;
  463. }
  464. protected function performCopy($sPackagePath, &$sHomePath)
  465. {
  466. if($this->bUseFtp) {
  467. $sLogin = getParam('sys_ftp_login');
  468. $sPassword = getParam('sys_ftp_password');
  469. $sPath = getParam('sys_ftp_dir');
  470. if(empty($sLogin) || empty($sPassword) || empty($sPath))
  471. return _t('_adm_str_err_no_ftp_info');
  472. $oFile = new BxDolFtp($_SERVER['HTTP_HOST'], $sLogin, $sPassword, $sPath);
  473. if(!$oFile->connect())
  474. return _t('_adm_str_err_cannot_connect_to_ftp');
  475. if(!$oFile->isUna())
  476. return _t('_adm_str_err_destination_not_valid');
  477. }
  478. else
  479. $oFile = BxDolFile::getInstance();
  480. $aConfig = self::getModuleConfig($sPackagePath . 'install/config.php');
  481. if(empty($aConfig) || !is_array($aConfig) || empty($aConfig['home_dir']))
  482. return _t('_adm_str_err_wrong_package_format');
  483. $sHomePath = $aConfig['home_dir'];
  484. if(!$oFile->copy($sPackagePath, 'modules/' . $sHomePath))
  485. return _t('_adm_str_err_files_copy_failed');
  486. return true;
  487. }
  488. protected function getConfigModule($sConfigPath, $aInstalled = array())
  489. {
  490. $aConfig = self::getModuleConfig($sConfigPath);
  491. if(empty($aConfig) || !is_array($aConfig))
  492. return array();
  493. $bInstalled = !empty($aInstalled);
  494. $bEnabled = $bInstalled && (int)$aInstalled['enabled'] == 1;
  495. $sVersion = $bInstalled ? $aInstalled['version'] : $aConfig['version'];
  496. return array(
  497. 'type' => $aConfig['type'],
  498. 'name' => isset($aConfig['name']) ? $aConfig['name'] : $aConfig['home_uri'],
  499. 'title' => bx_process_output($aConfig['title']),
  500. 'vendor' => $aConfig['vendor'],
  501. 'version' => $sVersion,
  502. 'uri' => $aConfig['home_uri'],
  503. 'dir' => $aConfig['home_dir'],
  504. 'note' => isset($aConfig['note']) ? bx_process_output($aConfig['note']) : '',
  505. 'installed' => $bInstalled,
  506. 'enabled' => $bEnabled
  507. );
  508. }
  509. protected function getConfigUpdate($sConfigPathModule, $sConfigPathUpdate, $aInstalledModule = array())
  510. {
  511. $aModule = $this->getConfigModule($sConfigPathModule, $aInstalledModule);
  512. if(empty($aModule) || !$aModule['installed'])
  513. return array();
  514. $aConfig = self::getModuleConfig($sConfigPathUpdate);
  515. if(empty($aConfig) || !is_array($aConfig))
  516. return array();
  517. return array(
  518. 'title' => bx_process_output($aConfig['title']),
  519. 'vendor' => $aConfig['vendor'],
  520. 'version_from' => $aConfig['version_from'],
  521. 'version_to' => $aConfig['version_to'],
  522. 'dir' => $aConfig['home_dir'],
  523. 'module_type' => $aModule['type'],
  524. 'module_name' => $aModule['name'],
  525. 'module_dir' => $aModule['dir'],
  526. 'module_version' => $aModule['version']
  527. );
  528. }
  529. private function getInstalledInfo()
  530. {
  531. $aModules = BxDolModuleQuery::getInstance()->getModules();
  532. $aInstalledInfo = array();
  533. foreach($aModules as $aModule)
  534. $aInstalledInfo[$aModule['path']] = $aModule;
  535. return $aInstalledInfo;
  536. }
  537. private function getInstalledInfoShort()
  538. {
  539. $aModules = BxDolModuleQuery::getInstance()->getModules();
  540. $aProducts = array();
  541. foreach($aModules as $aModule) {
  542. if(empty($aModule['name']))
  543. continue;
  544. $aProducts[$aModule['name']] = array(
  545. 'name' => $aModule['name'],
  546. 'version' => $aModule['version'],
  547. 'hash' => $aModule['hash'],
  548. );
  549. }
  550. return base64_encode(serialize($aProducts));
  551. }
  552. private function addTransientJob($sName, $sAction, $aParams)
  553. {
  554. if(BxDolCronQuery::getInstance()->addTransientJobService($sName, array('system', $sAction, $aParams, 'DolStudioInstallerUtils')))
  555. return true;
  556. return false;
  557. }
  558. private function writeLog($sMessage, $aParams = array())
  559. {
  560. $this->_oLog->write('Operation failed:', $this->getModuleTitle($aParams) . $sMessage);
  561. }
  562. private function emailNotify($sMessage, $aParams = array())
  563. {
  564. sendMailTemplateSystem('t_BgOperationFailed', array (
  565. 'conclusion' => strip_tags($this->getModuleTitle($aParams) . $sMessage),
  566. ));
  567. }
  568. private function getModuleTitle($aParams = array())
  569. {
  570. if(empty($aParams['module_name']))
  571. return '';
  572. $sTitleKey = '_' . $aParams['module_name'];
  573. $sTitleValue = _t($sTitleKey);
  574. if(strcmp($sTitleKey, $sTitleValue) == 0) {
  575. $aModule = BxDolModuleQuery::getInstance()->getModuleByName($aParams['module_name']);
  576. $sTitleValue = is_array($aModule) && !empty($aModule['title']) ? $aModule['title'] : $aParams['module_name'];
  577. }
  578. return $sTitleValue . ': ';
  579. }
  580. private function emailNotifyModulesUpgrade($sResult, $aData)
  581. {
  582. $oModuleQuery = BxDolModuleQuery::getInstance();
  583. $sConclusion = '';
  584. if(!empty($aData))
  585. foreach($aData as $sModule => $sMessage) {
  586. $aModule = $oModuleQuery->getModuleByName($sModule);
  587. $sConclusion .= _t('_sys_et_txt_body_modules_upgrade_' . $sResult, $aModule['title'], $sMessage);
  588. }
  589. sendMailTemplateSystem('t_UpgradeModules' . ucfirst($sResult), array (
  590. 'conclusion' => $sConclusion,
  591. ));
  592. }
  593. }
  594. /** @} */