BxDolStudioInstallerUtils.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  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://unacms.com/');
  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. }
  45. /**
  46. * Prevent cloning the instance
  47. */
  48. public function __clone()
  49. {
  50. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  51. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  52. }
  53. /**
  54. * Get singleton instance of the class
  55. */
  56. public static function getInstance()
  57. {
  58. if (!isset($GLOBALS['bxDolClasses'][__CLASS__]))
  59. $GLOBALS['bxDolClasses'][__CLASS__] = new BxDolStudioInstallerUtils();
  60. return $GLOBALS['bxDolClasses'][__CLASS__];
  61. }
  62. public static function getNamePerformAction($sParam)
  63. {
  64. return 'sys_perform_action_' . md5($sParam);
  65. }
  66. public static function getNameDownloadFile($sParam)
  67. {
  68. return 'sys_download_file_complete_' . $sParam;
  69. }
  70. public static function getNamePerformModulesUpgrade()
  71. {
  72. return 'sys_upgrade_modules_transient';
  73. }
  74. /*
  75. * Is used to complete Downloading inside Transient Cron Job.
  76. */
  77. public function serviceDownloadFileComplete($sFilePath, $aParams = array())
  78. {
  79. return $this->downloadFileComplete($sFilePath, array_merge($aParams, array('transient' => true)));
  80. }
  81. /*
  82. * Is used to perform action inside Transient Cron Job.
  83. */
  84. public function servicePerformAction($sDirectory, $sOperation, $aParams)
  85. {
  86. return $this->perform($sDirectory, $sOperation, array_merge($aParams, array('transient' => true)));
  87. }
  88. /*
  89. * Is used to perform modules upgrade inside Transient Cron Job.
  90. */
  91. public function servicePerformModulesUpgrade($aParams = array())
  92. {
  93. return $this->performModulesUpgrade(array_merge($aParams, array('directly' => true, 'transient' => true)));
  94. }
  95. public function getStoreDataUrl($sType = 'public')
  96. {
  97. return $sType == 'public' ? $this->sStoreDataUrlPublic : '';
  98. }
  99. public function getAccessObject($bAuthorizedAccess)
  100. {
  101. $sClass = $bAuthorizedAccess ? $this->sAuthorizedAccessClass : 'BxDolStudioJson';
  102. bx_import($sClass);
  103. return $sClass::getInstance();
  104. }
  105. public function getModules($bTitleAsKey = true)
  106. {
  107. $aModules = array();
  108. $oTemplate = BxDolStudioTemplate::getInstance();
  109. $aInstalledInfo = $this->getInstalledInfo();
  110. $sPath = BX_DIRECTORY_PATH_MODULES;
  111. if(($rHandleVendor = opendir($sPath)) !== false) {
  112. while(($sVendor = readdir($rHandleVendor)) !== false) {
  113. if(substr($sVendor, 0, 1) == '.' || !is_dir($sPath . $sVendor))
  114. continue;
  115. if(($rHandleModule = opendir($sPath . $sVendor)) !== false) {
  116. while(($sModule = readdir($rHandleModule)) !== false) {
  117. if(!is_dir($sPath . $sVendor . '/' . $sModule) || substr($sModule, 0, 1) == '.')
  118. continue;
  119. $sModulePath = $sVendor . '/' . $sModule . '/';
  120. $aModule = $this->getConfigModule($sPath . $sModulePath . 'install/config.php', !empty($aInstalledInfo[$sModulePath]) ? $aInstalledInfo[$sModulePath] : array());
  121. if(empty($aModule))
  122. continue;
  123. $aModules[$bTitleAsKey ? $aModule['title'] : $aModule['name']] = $aModule;
  124. }
  125. closedir($rHandleModule);
  126. }
  127. }
  128. closedir($rHandleVendor);
  129. }
  130. ksort($aModules);
  131. return $aModules;
  132. }
  133. public function getUpdates()
  134. {
  135. $aUpdates = array();
  136. $oTemplate = BxDolStudioTemplate::getInstance();
  137. $aInstalledInfo = $this->getInstalledInfo();
  138. $sPath = BX_DIRECTORY_PATH_MODULES;
  139. if(($rHandleVendor = opendir($sPath)) !== false) {
  140. while(($sVendor = readdir($rHandleVendor)) !== false) {
  141. if(substr($sVendor, 0, 1) == '.' || !is_dir($sPath . $sVendor))
  142. continue;
  143. if(($rHandleModule = opendir($sPath . $sVendor . '/')) !== false) {
  144. while(($sModule = readdir($rHandleModule)) !== false) {
  145. if(!is_dir($sPath . $sVendor . '/' . $sModule) || substr($sModule, 0, 1) == '.')
  146. continue;
  147. $sModulePath = $sVendor . '/' . $sModule . '/';
  148. if(empty($aInstalledInfo[$sModulePath]))
  149. continue;
  150. $aUpdate = $this->getUpdate($aInstalledInfo[$sModulePath]);
  151. if(!empty($aUpdate))
  152. $aUpdates[$aUpdate['title']] = $aUpdate;
  153. }
  154. closedir($rHandleModule);
  155. }
  156. }
  157. closedir($rHandleVendor);
  158. }
  159. ksort($aUpdates);
  160. return $aUpdates;
  161. }
  162. public function getUpdate($mixedModule)
  163. {
  164. $aResult = array();
  165. if(!is_array($mixedModule))
  166. $mixedModule = BxDolModuleQuery::getInstance()->{is_numeric($mixedModule) ? 'getModuleById' : 'getModuleByName'}($mixedModule);
  167. if(empty($mixedModule) || !is_array($mixedModule))
  168. return $aResult;
  169. $sPathModule = BX_DIRECTORY_PATH_MODULES . $mixedModule['path'];
  170. $sPathUpdates = $sPathModule . 'updates/';
  171. $rHandleUpdate = @opendir($sPathUpdates);
  172. if($rHandleUpdate === false)
  173. return $aResult;
  174. while(($sUpdate = readdir($rHandleUpdate)) !== false) {
  175. $sPathUpdate = $sPathUpdates . $sUpdate . '/';
  176. if(substr($sUpdate, 0, 1) == '.' || !is_dir($sPathUpdate))
  177. continue;
  178. $aUpdate = $this->getConfigUpdate($sPathModule . 'install/config.php', $sPathUpdate . 'install/config.php', $mixedModule);
  179. if(empty($aUpdate) || !is_array($aUpdate) || version_compare(strtolower($aUpdate['module_version']), strtolower($aUpdate['version_from'])) != 0)
  180. continue;
  181. $aResult = $aUpdate;
  182. break;
  183. }
  184. closedir($rHandleUpdate);
  185. return $aResult;
  186. }
  187. public function perform($sDirectory, $sOperation, $aParams = array())
  188. {
  189. $bTransient = false;
  190. if(isset($aParams['transient'])) {
  191. $bTransient = (bool)$aParams['transient'];
  192. unset($aParams['transient']);
  193. }
  194. if(!defined('BX_DOL_CRON_EXECUTE') && !self::isRealOwner() && !in_array($sOperation, array('install', 'uninstall', 'enable', 'disable'))) {
  195. if($this->addTransientJob(self::getNamePerformAction($sDirectory), 'perform_action', array($sDirectory, $sOperation, $aParams)))
  196. return array('code' => BX_DOL_STUDIO_IU_RC_SCHEDULED, 'message' => _t('_adm_mod_msg_process_operation_scheduled'));
  197. else
  198. return array('code' => BX_DOL_STUDIO_IU_RC_FAILED, 'message' => _t('_adm_mod_err_process_operation_failed', $sOperation, $sDirectory));
  199. }
  200. $sConfigFile = 'install/config.php';
  201. $sInstallerFile = 'install/installer.php';
  202. $sInstallerClass = $sOperation == 'update' ? 'Updater' : 'Installer';
  203. $aConfig = self::getModuleConfig(BX_DIRECTORY_PATH_MODULES . $sDirectory . $sConfigFile);
  204. if(empty($aParams['module_name']) && !empty($aConfig['name']))
  205. $aParams['module_name'] = $aConfig['name'];
  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. bx_log('sys_modules', ":\n[" . $sOperation . "] Operation failed: " . $this->getModuleTitle($aParams) . "\n" . strip_tags($sMessage), BX_LOG_ERR);
  210. if($bTransient)
  211. $this->emailNotify($sMessage, $aParams);
  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. bx_log('sys_modules', ":\n[" . $sOperation . "] Operation failed: " . $this->getModuleTitle($aParams) . "\n" . strip_tags($aResult['message']), BX_LOG_ERR);
  220. if($bTransient)
  221. $this->emailNotify($aResult['message'], $aParams);
  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. bx_log('sys_modules', ":\n[upgrade] Cannot get a list of modules which require to be updated.", BX_LOG_ERR);
  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. bx_log('sys_modules', ":\n[upgrade] Cannot find update scripts for modules. They are damaged or were not downloaded.", BX_LOG_ERR);
  263. return true;
  264. }
  265. foreach($aUpdates as $aUpdate) {
  266. $aParams['module_name'] = $aUpdate['module_name'];
  267. $aResult = $this->perform($aUpdate['dir'], 'update', $aParams);
  268. switch((int)$aResult['code']) {
  269. case BX_DOL_STUDIO_IU_RC_SUCCESS:
  270. $aSuccess[$aUpdate['module_name']] = $aUpdate['version_to'];
  271. break;
  272. case BX_DOL_STUDIO_IU_RCE_WSV_MU:
  273. if(isset($aParams['autoupdate']) && (bool)$aParams['autoupdate'] === true)
  274. break;
  275. default:
  276. $aFailed[$aUpdate['module_name']] = $aResult['message'];
  277. }
  278. }
  279. if(!empty($aFailed)) {
  280. bx_log('sys_modules', [
  281. ":\n[upgrade] Failed to update modules:",
  282. $aFailed
  283. ], BX_LOG_ERR);
  284. if($bTransient)
  285. $this->emailNotifyModulesUpgrade('failed', $aFailed);
  286. }
  287. if(!empty($aSuccess)) {
  288. bx_log('sys_modules', [
  289. ":\n[upgrade] Successfully updated modules:",
  290. $aSuccess
  291. ], BX_LOG_INFO);
  292. if($bTransient)
  293. $this->emailNotifyModulesUpgrade('success', $aSuccess);
  294. }
  295. return empty($aFailed);
  296. }
  297. public function checkModules($bAuthorizedAccess = false)
  298. {
  299. $iPerPage = 9999; //--- Note. It's essential to load all purchased products at the same time.
  300. $sVersion = bx_get_ver();
  301. if($bAuthorizedAccess)
  302. $aProducts = $this->getAccessObject(true)->loadItems(array(
  303. 'method' => 'browse_purchased',
  304. 'domain' => BX_DOL_URL_ROOT,
  305. 'version' => $sVersion,
  306. 'products' => $this->getInstalledInfoShort(),
  307. 'per_page' => $iPerPage
  308. ));
  309. else
  310. $aProducts = $this->getAccessObject(false)->load($this->sStoreDataUrlPublic . 'json_browse_purchased', array('key' => getParam('sys_oauth_key'), 'per_page' => $iPerPage));
  311. if(empty($aProducts) || !is_array($aProducts))
  312. return $aProducts;
  313. $oModuleDb = BxDolModuleQuery::getInstance();
  314. $oModuleDb->updateModule(array('hash' => ''));
  315. if(!empty($aProducts) && is_array($aProducts))
  316. foreach ($aProducts as $aProduct)
  317. if(!empty($aProduct['hash']))
  318. $oModuleDb->updateModule(array('hash' => $aProduct['hash']), array('name' => $aProduct['name']));
  319. return $aProducts;
  320. }
  321. public function checkUpdates($bAuthorizedAccess = false)
  322. {
  323. return $this->getUpdatesInfo('', $bAuthorizedAccess);
  324. }
  325. public function checkUpdatesByModule($sModule, $bAuthorizedAccess = false)
  326. {
  327. return $this->getUpdatesInfo($sModule, $bAuthorizedAccess);
  328. }
  329. public function downloadFileAuthorized($iFileId)
  330. {
  331. $aItem = $this->getAccessObject(true)->loadItems(array(
  332. 'method' => 'download_file',
  333. 'version' => bx_get_ver(),
  334. 'file_id' => $iFileId
  335. ));
  336. if(empty($aItem) || !is_array($aItem))
  337. return $aItem;
  338. return $this->downloadFileInit($aItem, array('module_name' => $aItem['module_name']));
  339. }
  340. public function downloadUpdatePublic($sModuleName, $bApplyUpdate = false)
  341. {
  342. $aModule = BxDolModuleQuery::getInstance()->getModuleByName($sModuleName);
  343. $aItem = $this->getAccessObject(false)->load($this->sStoreDataUrlPublic . 'json_download_update', array(
  344. 'key' => getParam('sys_oauth_key'),
  345. 'version' => bx_get_ver(),
  346. 'product' => base64_encode(json_encode(array(
  347. 'name' => $aModule['name'],
  348. 'version' => $aModule['version'],
  349. 'hash' => $aModule['hash'],
  350. ))),
  351. ));
  352. return $this->downloadFileInit($aItem, array('module_name' => $sModuleName, 'auto_action' => $bApplyUpdate ? 'update' : ''));
  353. }
  354. protected function downloadFileInit($aItem, $aParams = array())
  355. {
  356. if(empty($aItem) || !is_array($aItem))
  357. return $aItem;
  358. //--- write ZIP archive.
  359. $sFilePath = '';
  360. $mixedResult = $this->performWrite($aItem, $sFilePath);
  361. if($mixedResult !== true)
  362. return $mixedResult;
  363. return $this->downloadFileComplete($sFilePath, $aParams);
  364. }
  365. protected function downloadFileComplete($sFilePath, $aParams = array())
  366. {
  367. $bTransient = false;
  368. if(isset($aParams['transient'])) {
  369. $bTransient = (bool)$aParams['transient'];
  370. unset($aParams['transient']);
  371. }
  372. $sAutoAction = '';
  373. $bAutoAction = false;
  374. if(isset($aParams['auto_action'])) {
  375. $sAutoAction = $aParams['auto_action'];
  376. $bAutoAction = !empty($sAutoAction);
  377. unset($aParams['auto_action']);
  378. }
  379. if(!defined('BX_DOL_CRON_EXECUTE') && !self::isRealOwner()) {
  380. if(!$this->addTransientJob(self::getNameDownloadFile($aParams['module_name']), 'download_file_complete', array($sFilePath, array_merge($aParams, array('auto_action' => $sAutoAction)))))
  381. return array('code' => BX_DOL_STUDIO_IU_RC_FAILED, 'message' => _t('_adm_str_err_download_failed'));
  382. return array('code' => BX_DOL_STUDIO_IU_RC_SCHEDULED, 'message' => _t('_adm_str_msg_download' . ($bAutoAction ? '_and_install' : '') . '_scheduled'));
  383. }
  384. //--- Unarchive package.
  385. $sPackagePath = '';
  386. $mixedResult = $this->performUnarchive($sFilePath, $sPackagePath);
  387. @unlink($sFilePath);
  388. if($mixedResult !== true) {
  389. bx_log('sys_modules', ":\n[download] Operation failed: " . $this->getModuleTitle($aParams) . "\n" . $mixedResult, BX_LOG_ERR);
  390. if($bTransient)
  391. $this->emailNotify($mixedResult, $aParams);
  392. return array('code' => BX_DOL_STUDIO_IU_RC_FAILED, 'message' => $mixedResult);
  393. }
  394. //--- Copy unarchived package.
  395. $sHomePath = '';
  396. $mixedResult = $this->performCopy($sPackagePath, $sHomePath);
  397. @bx_rrmdir($sPackagePath);
  398. if($mixedResult !== true) {
  399. bx_log('sys_modules', ":\n[download] Operation failed: " . $this->getModuleTitle($aParams) . "\n" . $mixedResult, BX_LOG_ERR);
  400. if($bTransient)
  401. $this->emailNotify($mixedResult, $aParams);
  402. return array('code' => BX_DOL_STUDIO_IU_RC_FAILED, 'message' => $mixedResult);
  403. }
  404. if(!$bAutoAction)
  405. return true;
  406. //--- Autoinstall the downloaded package if it's needed.
  407. $aResult = $this->perform($sHomePath, $sAutoAction, $aParams);
  408. if((int)$aResult['code'] == 0)
  409. return true;
  410. if($bTransient)
  411. $this->emailNotify($aResult['message'], $aParams);
  412. return array('code' => BX_DOL_STUDIO_IU_RC_FAILED, 'message' => $aResult['message']);
  413. }
  414. protected function performWrite($aItem, &$sFilePath)
  415. {
  416. $sFilePath = BX_DIRECTORY_PATH_TMP . $aItem['name'];
  417. if(file_exists($sFilePath))
  418. @unlink($sFilePath);
  419. $iUmaskSave = umask(0);
  420. if(!$rHandler = fopen($sFilePath, 'w')) {
  421. umask($iUmaskSave);
  422. return _t('_adm_str_err_cannot_write');
  423. }
  424. $sContent = base64_decode(urldecode($aItem['content']));
  425. if(!fwrite($rHandler, $sContent, strlen($sContent))) {
  426. umask($iUmaskSave);
  427. return _t('_adm_str_err_cannot_write');
  428. }
  429. fclose($rHandler);
  430. umask($iUmaskSave);
  431. return true;
  432. }
  433. protected function performUnarchive($sFilePath, &$sPackagePath)
  434. {
  435. if(!class_exists('ZipArchive'))
  436. return _t('_adm_str_err_zip_not_available');
  437. $iUmaskSave = umask(0);
  438. $oZip = new ZipArchive();
  439. if($oZip->open($sFilePath) !== true) {
  440. umask($iUmaskSave);
  441. return _t('_adm_str_err_cannot_unzip_package');
  442. }
  443. $sPackageFolder = '';
  444. if($oZip->numFiles > 0)
  445. $sPackageFolder = $oZip->getNameIndex(0);
  446. if(empty($sPackageFolder)) {
  447. umask($iUmaskSave);
  448. return _t('_adm_str_err_cannot_unzip_package');
  449. }
  450. $sPackagePath = BX_DIRECTORY_PATH_TMP . $sPackageFolder;
  451. if(file_exists($sPackagePath)) // remove existing tmp folder with the same name
  452. @bx_rrmdir($sPackagePath);
  453. if(!$oZip->extractTo(BX_DIRECTORY_PATH_TMP)) {
  454. umask($iUmaskSave);
  455. return _t('_adm_str_err_cannot_unzip_package');
  456. }
  457. $oZip->close();
  458. umask($iUmaskSave);
  459. return true;
  460. }
  461. protected function performCopy($sPackagePath, &$sHomePath)
  462. {
  463. if($this->bUseFtp) {
  464. $sLogin = getParam('sys_ftp_login');
  465. $sPassword = getParam('sys_ftp_password');
  466. $sPath = getParam('sys_ftp_dir');
  467. if(empty($sLogin) || empty($sPassword) || empty($sPath))
  468. return _t('_adm_str_err_no_ftp_info');
  469. $oFile = new BxDolFtp($_SERVER['HTTP_HOST'], $sLogin, $sPassword, $sPath);
  470. if(!$oFile->connect())
  471. return _t('_adm_str_err_cannot_connect_to_ftp');
  472. if(!$oFile->isUna())
  473. return _t('_adm_str_err_destination_not_valid');
  474. }
  475. else
  476. $oFile = BxDolFile::getInstance();
  477. $aConfig = self::getModuleConfig($sPackagePath . 'install/config.php');
  478. if(empty($aConfig) || !is_array($aConfig) || empty($aConfig['home_dir']))
  479. return _t('_adm_str_err_wrong_package_format');
  480. $sHomePath = $aConfig['home_dir'];
  481. if(!$oFile->copy($sPackagePath, 'modules/' . $sHomePath))
  482. return _t('_adm_str_err_files_copy_failed');
  483. return true;
  484. }
  485. protected function getConfigModule($sConfigPath, $aInstalled = array())
  486. {
  487. $aConfig = self::getModuleConfig($sConfigPath);
  488. if(empty($aConfig) || !is_array($aConfig))
  489. return array();
  490. $bInstalled = !empty($aInstalled);
  491. $bEnabled = $bInstalled && (int)$aInstalled['enabled'] == 1;
  492. $sVersion = $bInstalled ? $aInstalled['version'] : $aConfig['version'];
  493. return array(
  494. 'type' => $aConfig['type'],
  495. 'name' => isset($aConfig['name']) ? $aConfig['name'] : $aConfig['home_uri'],
  496. 'title' => bx_process_output($aConfig['title']),
  497. 'vendor' => $aConfig['vendor'],
  498. 'version' => $sVersion,
  499. 'uri' => $aConfig['home_uri'],
  500. 'dir' => $aConfig['home_dir'],
  501. 'note' => isset($aConfig['note']) ? bx_process_output($aConfig['note']) : '',
  502. 'installed' => $bInstalled,
  503. 'enabled' => $bEnabled
  504. );
  505. }
  506. protected function getConfigUpdate($sConfigPathModule, $sConfigPathUpdate, $aInstalledModule = array())
  507. {
  508. $aModule = $this->getConfigModule($sConfigPathModule, $aInstalledModule);
  509. if(empty($aModule) || !$aModule['installed'])
  510. return array();
  511. $aConfig = self::getModuleConfig($sConfigPathUpdate);
  512. if(empty($aConfig) || !is_array($aConfig))
  513. return array();
  514. return array(
  515. 'title' => bx_process_output($aConfig['title']),
  516. 'vendor' => $aConfig['vendor'],
  517. 'version_from' => $aConfig['version_from'],
  518. 'version_to' => $aConfig['version_to'],
  519. 'dir' => $aConfig['home_dir'],
  520. 'module_type' => $aModule['type'],
  521. 'module_name' => $aModule['name'],
  522. 'module_dir' => $aModule['dir'],
  523. 'module_version' => $aModule['version']
  524. );
  525. }
  526. private function getInstalledInfo()
  527. {
  528. $aModules = BxDolModuleQuery::getInstance()->getModules();
  529. $aInstalledInfo = array();
  530. foreach($aModules as $aModule)
  531. $aInstalledInfo[$aModule['path']] = $aModule;
  532. return $aInstalledInfo;
  533. }
  534. private function getInstalledInfoShort($sModule = '')
  535. {
  536. if(!empty($sModule))
  537. $aModules = array(BxDolModuleQuery::getInstance()->getModuleByName($sModule));
  538. else
  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(json_encode($aProducts));
  551. }
  552. protected function getUpdatesInfo($sModule = '', $bAuthorizedAccess = false)
  553. {
  554. $sVersion = bx_get_ver();
  555. $sProducts = $this->getInstalledInfoShort($sModule);
  556. if($bAuthorizedAccess)
  557. return $this->getAccessObject(true)->loadItems(array(
  558. 'method' => 'browse_updates',
  559. 'version' => $sVersion,
  560. 'products' => $sProducts
  561. ));
  562. return $this->getAccessObject(false)->load($this->sStoreDataUrlPublic . 'json_browse_updates', array(
  563. 'key' => getParam('sys_oauth_key'),
  564. 'version' => $sVersion,
  565. 'products' => $sProducts
  566. ));
  567. }
  568. private function addTransientJob($sName, $sAction, $aParams)
  569. {
  570. if(BxDolCronQuery::getInstance()->addTransientJobService($sName, array('system', $sAction, $aParams, 'DolStudioInstallerUtils')))
  571. return true;
  572. return false;
  573. }
  574. private function emailNotify($sMessage, $aParams = array())
  575. {
  576. sendMailTemplateSystem('t_BgOperationFailed', array (
  577. 'conclusion' => strip_tags($this->getModuleTitle($aParams) . $sMessage),
  578. ));
  579. }
  580. private function getModuleTitle($aParams = array())
  581. {
  582. if(empty($aParams['module_name']))
  583. return '';
  584. $sTitleKey = '_' . $aParams['module_name'];
  585. $sTitleValue = _t($sTitleKey);
  586. if(strcmp($sTitleKey, $sTitleValue) == 0) {
  587. $aModule = BxDolModuleQuery::getInstance()->getModuleByName($aParams['module_name']);
  588. $sTitleValue = is_array($aModule) && !empty($aModule['title']) ? $aModule['title'] : $aParams['module_name'];
  589. }
  590. return $sTitleValue . ': ';
  591. }
  592. private function emailNotifyModulesUpgrade($sResult, $aData)
  593. {
  594. $oModuleQuery = BxDolModuleQuery::getInstance();
  595. $sConclusion = '';
  596. if(!empty($aData))
  597. foreach($aData as $sModule => $sMessage) {
  598. $aModule = $oModuleQuery->getModuleByName($sModule);
  599. $sConclusion .= _t('_sys_et_txt_body_modules_upgrade_' . $sResult, $aModule['title'], $sMessage);
  600. }
  601. sendMailTemplateSystem('t_UpgradeModules' . ucfirst($sResult), array (
  602. 'conclusion' => $sConclusion,
  603. ));
  604. }
  605. }
  606. /** @} */