BxDolStudioInstallerUtils.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  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. return $this->getUpdatesInfo('', $bAuthorizedAccess);
  320. }
  321. public function checkUpdatesByModule($sModule, $bAuthorizedAccess = false)
  322. {
  323. return $this->getUpdatesInfo($sModule, $bAuthorizedAccess);
  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. 'version' => bx_get_ver(),
  341. 'product' => base64_encode(serialize(array(
  342. 'name' => $aModule['name'],
  343. 'version' => $aModule['version'],
  344. 'hash' => $aModule['hash'],
  345. ))),
  346. ));
  347. return $this->downloadFileInit($aItem, array('module_name' => $sModuleName, 'auto_action' => $bApplyUpdate ? 'update' : ''));
  348. }
  349. protected function downloadFileInit($aItem, $aParams = array())
  350. {
  351. if(empty($aItem) || !is_array($aItem))
  352. return $aItem;
  353. //--- write ZIP archive.
  354. $sFilePath = '';
  355. $mixedResult = $this->performWrite($aItem, $sFilePath);
  356. if($mixedResult !== true)
  357. return $mixedResult;
  358. return $this->downloadFileComplete($sFilePath, $aParams);
  359. }
  360. protected function downloadFileComplete($sFilePath, $aParams = array())
  361. {
  362. $bTransient = false;
  363. if(isset($aParams['transient'])) {
  364. $bTransient = (bool)$aParams['transient'];
  365. unset($aParams['transient']);
  366. }
  367. $sAutoAction = '';
  368. $bAutoAction = false;
  369. if(isset($aParams['auto_action'])) {
  370. $sAutoAction = $aParams['auto_action'];
  371. $bAutoAction = !empty($sAutoAction);
  372. unset($aParams['auto_action']);
  373. }
  374. if(!defined('BX_DOL_CRON_EXECUTE') && !self::isRealOwner()) {
  375. if(!$this->addTransientJob(self::getNameDownloadFile($aParams['module_name']), 'download_file_complete', array($sFilePath, array_merge($aParams, array('auto_action' => $sAutoAction)))))
  376. return array('code' => BX_DOL_STUDIO_IU_RC_FAILED, 'message' => _t('_adm_str_err_download_failed'));
  377. return array('code' => BX_DOL_STUDIO_IU_RC_SCHEDULED, 'message' => _t('_adm_str_msg_download' . ($bAutoAction ? '_and_install' : '') . '_scheduled'));
  378. }
  379. //--- Unarchive package.
  380. $sPackagePath = '';
  381. $mixedResult = $this->performUnarchive($sFilePath, $sPackagePath);
  382. @unlink($sFilePath);
  383. if($mixedResult !== true) {
  384. $this->writeLog($mixedResult, $aParams);
  385. if($bTransient)
  386. $this->emailNotify($mixedResult, $aParams);
  387. return array('code' => BX_DOL_STUDIO_IU_RC_FAILED, 'message' => $mixedResult);
  388. }
  389. //--- Copy unarchived package.
  390. $sHomePath = '';
  391. $mixedResult = $this->performCopy($sPackagePath, $sHomePath);
  392. @bx_rrmdir($sPackagePath);
  393. if($mixedResult !== true) {
  394. $this->writeLog($mixedResult, $aParams);
  395. if($bTransient)
  396. $this->emailNotify($mixedResult, $aParams);
  397. return array('code' => BX_DOL_STUDIO_IU_RC_FAILED, 'message' => $mixedResult);
  398. }
  399. if(!$bAutoAction)
  400. return true;
  401. //--- Autoinstall the downloaded package if it's needed.
  402. $aResult = $this->perform($sHomePath, $sAutoAction, $aParams);
  403. if((int)$aResult['code'] == 0)
  404. return true;
  405. if($bTransient)
  406. $this->emailNotify($aResult['message'], $aParams);
  407. return array('code' => BX_DOL_STUDIO_IU_RC_FAILED, 'message' => $aResult['message']);
  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($sModule = '')
  530. {
  531. if(!empty($sModule))
  532. $aModules = array(BxDolModuleQuery::getInstance()->getModuleByName($sModule));
  533. else
  534. $aModules = BxDolModuleQuery::getInstance()->getModules();
  535. $aProducts = array();
  536. foreach($aModules as $aModule) {
  537. if(empty($aModule['name']))
  538. continue;
  539. $aProducts[$aModule['name']] = array(
  540. 'name' => $aModule['name'],
  541. 'version' => $aModule['version'],
  542. 'hash' => $aModule['hash'],
  543. );
  544. }
  545. return base64_encode(serialize($aProducts));
  546. }
  547. protected function getUpdatesInfo($sModule = '', $bAuthorizedAccess = false)
  548. {
  549. $sVersion = bx_get_ver();
  550. $sProducts = $this->getInstalledInfoShort($sModule);
  551. if($bAuthorizedAccess)
  552. return $this->getAccessObject(true)->loadItems(array(
  553. 'method' => 'browse_updates',
  554. 'version' => $sVersion,
  555. 'products' => $sProducts
  556. ));
  557. return $this->getAccessObject(false)->load($this->sStoreDataUrlPublic . 'json_browse_updates', array(
  558. 'key' => getParam('sys_oauth_key'),
  559. 'version' => $sVersion,
  560. 'products' => $sProducts
  561. ));
  562. }
  563. private function addTransientJob($sName, $sAction, $aParams)
  564. {
  565. if(BxDolCronQuery::getInstance()->addTransientJobService($sName, array('system', $sAction, $aParams, 'DolStudioInstallerUtils')))
  566. return true;
  567. return false;
  568. }
  569. private function writeLog($sMessage, $aParams = array())
  570. {
  571. $this->_oLog->write('Operation failed:', $this->getModuleTitle($aParams) . $sMessage);
  572. }
  573. private function emailNotify($sMessage, $aParams = array())
  574. {
  575. sendMailTemplateSystem('t_BgOperationFailed', array (
  576. 'conclusion' => strip_tags($this->getModuleTitle($aParams) . $sMessage),
  577. ));
  578. }
  579. private function getModuleTitle($aParams = array())
  580. {
  581. if(empty($aParams['module_name']))
  582. return '';
  583. $sTitleKey = '_' . $aParams['module_name'];
  584. $sTitleValue = _t($sTitleKey);
  585. if(strcmp($sTitleKey, $sTitleValue) == 0) {
  586. $aModule = BxDolModuleQuery::getInstance()->getModuleByName($aParams['module_name']);
  587. $sTitleValue = is_array($aModule) && !empty($aModule['title']) ? $aModule['title'] : $aParams['module_name'];
  588. }
  589. return $sTitleValue . ': ';
  590. }
  591. private function emailNotifyModulesUpgrade($sResult, $aData)
  592. {
  593. $oModuleQuery = BxDolModuleQuery::getInstance();
  594. $sConclusion = '';
  595. if(!empty($aData))
  596. foreach($aData as $sModule => $sMessage) {
  597. $aModule = $oModuleQuery->getModuleByName($sModule);
  598. $sConclusion .= _t('_sys_et_txt_body_modules_upgrade_' . $sResult, $aModule['title'], $sMessage);
  599. }
  600. sendMailTemplateSystem('t_UpgradeModules' . ucfirst($sResult), array (
  601. 'conclusion' => $sConclusion,
  602. ));
  603. }
  604. }
  605. /** @} */