BxDolStudioInstallerUtils.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup DolphinStudio Dolphin Studio
  7. * @{
  8. */
  9. bx_import('BxDolLanguages');
  10. bx_import('BxDolInstallerUtils');
  11. define('BX_DOL_UNITY_URL_ROOT', 'http://www.boonex.com/');
  12. define('BX_DOL_UNITY_URL_MARKET', BX_DOL_UNITY_URL_ROOT . 'market/');
  13. class BxDolStudioInstallerUtils extends BxDolInstallerUtils implements iBxDolSingleton
  14. {
  15. protected $bUseFtp;
  16. protected $sAuthorizedAccessClass;
  17. public function __construct()
  18. {
  19. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  20. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  21. parent::__construct();
  22. $this->bUseFtp = BX_FORCE_USE_FTP_FILE_TRANSFER;
  23. $this->sAuthorizedAccessClass = 'BxDolStudioOAuthPlugin';
  24. }
  25. /**
  26. * Prevent cloning the instance
  27. */
  28. public function __clone()
  29. {
  30. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  31. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  32. }
  33. /**
  34. * Get singleton instance of the class
  35. */
  36. public static function getInstance()
  37. {
  38. if (!isset($GLOBALS['bxDolClasses'][__CLASS__]))
  39. $GLOBALS['bxDolClasses'][__CLASS__] = new BxDolStudioInstallerUtils();
  40. return $GLOBALS['bxDolClasses'][__CLASS__];
  41. }
  42. /*
  43. * Is used to complete Downloading inside Transient Cron Job.
  44. */
  45. public function serviceDownloadFileComplete($sFilePath, $aParams = array())
  46. {
  47. return $this->downloadFileComplete($sFilePath, array_merge($aParams, array('transient' => true)));
  48. }
  49. /*
  50. * Is used to perform action inside Transient Cron Job.
  51. */
  52. public function servicePerformAction($sDirectory, $sOperation, $aParams)
  53. {
  54. return $this->perform($sDirectory, $sOperation, array_merge($aParams, array('transient' => true)));
  55. }
  56. public function getModules($bTitleAsKey = true)
  57. {
  58. $aModules = array();
  59. $oTemplate = BxDolStudioTemplate::getInstance();
  60. $aInstalledInfo = $this->getInstalledInfo();
  61. $sPath = BX_DIRECTORY_PATH_MODULES;
  62. if(($rHandleVendor = opendir($sPath)) !== false) {
  63. while(($sVendor = readdir($rHandleVendor)) !== false) {
  64. if(substr($sVendor, 0, 1) == '.' || !is_dir($sPath . $sVendor))
  65. continue;
  66. if(($rHandleModule = opendir($sPath . $sVendor)) !== false) {
  67. while(($sModule = readdir($rHandleModule)) !== false) {
  68. if(!is_dir($sPath . $sVendor . '/' . $sModule) || substr($sModule, 0, 1) == '.')
  69. continue;
  70. $sModulePath = $sVendor . '/' . $sModule . '/';
  71. $aModule = $this->getConfigModule($sPath . $sModulePath . 'install/config.php', !empty($aInstalledInfo[$sModulePath]) ? $aInstalledInfo[$sModulePath] : array());
  72. if(empty($aModule))
  73. continue;
  74. $aModules[$bTitleAsKey ? $aModule['title'] : $aModule['name']] = $aModule;
  75. }
  76. closedir($rHandleModule);
  77. }
  78. }
  79. closedir($rHandleVendor);
  80. }
  81. ksort($aModules);
  82. return $aModules;
  83. }
  84. public function getUpdates()
  85. {
  86. $aUpdates = array();
  87. bx_import('BxDolStudioTemplate');
  88. $oTemplate = BxDolStudioTemplate::getInstance();
  89. $aInstalledInfo = $this->getInstalledInfo();
  90. $sPath = BX_DIRECTORY_PATH_MODULES;
  91. if(($rHandleVendor = opendir($sPath)) !== false) {
  92. while(($sVendor = readdir($rHandleVendor)) !== false) {
  93. if(substr($sVendor, 0, 1) == '.' || !is_dir($sPath . $sVendor))
  94. continue;
  95. if(($rHandleModule = opendir($sPath . $sVendor . '/')) !== false) {
  96. while(($sModule = readdir($rHandleModule)) !== false) {
  97. if(!is_dir($sPath . $sVendor . '/' . $sModule) || substr($sModule, 0, 1) == '.')
  98. continue;
  99. $sModulePath = $sVendor . '/' . $sModule . '/';
  100. if(empty($aInstalledInfo[$sModulePath]))
  101. continue;
  102. $aUpdate = $this->getUpdate($aInstalledInfo[$sModulePath]);
  103. if(!empty($aUpdate))
  104. $aUpdates[$aUpdate['title']] = $aUpdate;
  105. }
  106. closedir($rHandleModule);
  107. }
  108. }
  109. closedir($rHandleVendor);
  110. }
  111. ksort($aUpdates);
  112. return $aUpdates;
  113. }
  114. public function getUpdate($mixedModule)
  115. {
  116. $aResult = array();
  117. if(!is_array($mixedModule)) {
  118. bx_import('BxDolModuleQuery');
  119. $mixedModule = BxDolModuleQuery::getInstance()->{is_numeric($mixedModule) ? 'getModuleById' : 'getModuleByName'}($mixedModule);
  120. }
  121. if(empty($mixedModule) || !is_array($mixedModule))
  122. return $aResult;
  123. $sPathModule = BX_DIRECTORY_PATH_MODULES . $mixedModule['path'];
  124. $sPathUpdates = $sPathModule . 'updates/';
  125. $rHandleUpdate = @opendir($sPathUpdates);
  126. if($rHandleUpdate === false)
  127. return $aResult;
  128. while(($sUpdate = readdir($rHandleUpdate)) !== false) {
  129. $sPathUpdate = $sPathUpdates . $sUpdate . '/';
  130. if(substr($sUpdate, 0, 1) == '.' || !is_dir($sPathUpdate))
  131. continue;
  132. $aUpdate = $this->getConfigUpdate($sPathModule . 'install/config.php', $sPathUpdate . 'install/config.php', $mixedModule);
  133. if(empty($aUpdate) || !is_array($aUpdate) || version_compare($aUpdate['module_version'], $aUpdate['version_from']) != 0)
  134. continue;
  135. $aResult = $aUpdate;
  136. break;
  137. }
  138. closedir($rHandleUpdate);
  139. return $aResult;
  140. }
  141. public function perform($sDirectory, $sOperation, $aParams = array())
  142. {
  143. $bTransient = false;
  144. if(isset($aParams['transient']) && $aParams['transient'] === true) {
  145. $bTransient = true;
  146. unset($aParams['transient']);
  147. }
  148. if(!defined('BX_DOL_CRON_EXECUTE') && !self::isRealOwner() && !in_array($sOperation, array('install', 'uninstall', 'enable', 'disable'))) {
  149. if($this->addTransientJob('perform_action', array($sDirectory, $sOperation, $aParams)))
  150. return array('code' => 1, 'message' => _t('_adm_mod_msg_process_operation_scheduled'));
  151. else
  152. return array('code' => 2, 'message' => _t('_adm_mod_err_process_operation_failed', $sOperation, $sDirectory));
  153. }
  154. $sConfigFile = 'install/config.php';
  155. $sInstallerFile = 'install/installer.php';
  156. $sInstallerClass = $sOperation == 'update' ? 'Updater' : 'Installer';
  157. $aConfig = self::getModuleConfig(BX_DIRECTORY_PATH_MODULES . $sDirectory . $sConfigFile);
  158. $sPathInstaller = BX_DIRECTORY_PATH_MODULES . $sDirectory . $sInstallerFile;
  159. if(empty($aConfig) || !file_exists($sPathInstaller)) {
  160. $sMessage = _t('_adm_mod_err_process_operation_failed', $sOperation, $sDirectory);
  161. if($bTransient)
  162. $this->emailNotify($sMessage);
  163. return array('code' => 2, 'message' => $sMessage);
  164. }
  165. require_once($sPathInstaller);
  166. $sClassName = $aConfig['class_prefix'] . $sInstallerClass;
  167. $oInstaller = new $sClassName($aConfig);
  168. $aResult = $oInstaller->$sOperation($aParams);
  169. if(!$aResult['result'] && $bTransient)
  170. $this->emailNotify($aResult['message']);
  171. return array('code' => $aResult['result'] ? 0 : 2, 'message' => $aResult['message']);
  172. }
  173. public function checkModules($bAuthorizedAccess = false)
  174. {
  175. if($bAuthorizedAccess)
  176. $aProducts = $this->getAccessObject(true)->loadItems(array('dol_type' => 'purchased_products', 'dol_domain' => BX_DOL_URL_ROOT));
  177. else
  178. $aProducts = $this->getAccessObject(false)->load(BX_DOL_UNITY_URL_MARKET . 'json_browse_purchased', array('key' => getParam('sys_oauth_key')));
  179. bx_import('BxDolModuleQuery');
  180. $oModuleDb = BxDolModuleQuery::getInstance();
  181. $oModuleDb->updateModule(array('hash' => ''));
  182. if(!empty($aProducts) && is_array($aProducts))
  183. foreach ($aProducts as $aProduct)
  184. $oModuleDb->updateModule(array('hash' => $aProduct['hash']), array('name' => $aProduct['name']));
  185. return $aProducts;
  186. }
  187. public function checkUpdates($bAuthorizedAccess = false)
  188. {
  189. bx_import('BxDolModuleQuery');
  190. $aModules = BxDolModuleQuery::getInstance()->getModules();
  191. $aProducts = array();
  192. foreach($aModules as $aModule) {
  193. if(empty($aModule['name']))
  194. continue;
  195. $aProducts[] = array(
  196. 'name' => $aModule['name'],
  197. 'version' => $aModule['version'],
  198. 'hash' => $aModule['hash'],
  199. );
  200. }
  201. $sProducts = base64_encode(serialize($aProducts));
  202. if($bAuthorizedAccess)
  203. return $this->getAccessObject(true)->loadItems(array('dol_type' => 'available_updates', 'dol_products' => $sProducts));
  204. return $this->getAccessObject(false)->load(BX_DOL_UNITY_URL_MARKET . 'json_browse_updates', array(
  205. 'key' => getParam('sys_oauth_key'),
  206. 'products' => $sProducts
  207. ));
  208. }
  209. public function downloadFileAuthorized($iFileId)
  210. {
  211. $aItem = $this->getAccessObject(true)->loadItems(array('dol_type' => 'product_file', 'dol_file_id' => $iFileId));
  212. return $this->downloadFileInit($aItem);
  213. }
  214. public function downloadUpdatePublic($sModuleName, $bAutoUpdate = false)
  215. {
  216. bx_import('BxDolModuleQuery');
  217. $aModule = BxDolModuleQuery::getInstance()->getModuleByName($sModuleName);
  218. $aItem = $this->getAccessObject(false)->load(BX_DOL_UNITY_URL_MARKET . 'json_download_update', array(
  219. 'key' => getParam('sys_oauth_key'),
  220. 'product' => base64_encode(serialize(array(
  221. 'name' => $aModule['name'],
  222. 'version' => $aModule['version'],
  223. 'hash' => $aModule['hash'],
  224. ))),
  225. ));
  226. return $this->downloadFileInit($aItem, array('updated_module_name' => $sModuleName, 'auto_action' => $bAutoUpdate ? 'update' : ''));
  227. }
  228. protected function downloadFileInit($aItem, $aParams = array())
  229. {
  230. if(empty($aItem) || !is_array($aItem))
  231. return $aItem;
  232. //--- write ZIP archive.
  233. $sFilePath = '';
  234. $mixedResult = $this->performWrite($aItem, $sFilePath);
  235. if($mixedResult !== true)
  236. return $mixedResult;
  237. return $this->downloadFileComplete($sFilePath, $aParams);
  238. }
  239. protected function downloadFileComplete($sFilePath, $aParams = array())
  240. {
  241. $bTransient = false;
  242. if(isset($aParams['transient']) && $aParams['transient'] === true) {
  243. $bTransient = true;
  244. unset($aParams['transient']);
  245. }
  246. $sAutoAction = isset($aParams['auto_action']) ? $aParams['auto_action'] : '';
  247. $bAutoAction = !empty($sAutoAction);
  248. if(!defined('BX_DOL_CRON_EXECUTE') && !self::isRealOwner())
  249. return _t($this->addTransientJob('download_file_complete', array($sFilePath, $aParams)) ? '_adm_str_msg_download' . ($bAutoAction ? '_and_install' : '') . '_scheduled' : '_adm_str_err_download_failed');
  250. //--- Unarchive package.
  251. $sPackagePath = '';
  252. $mixedResult = $this->performUnarchive($sFilePath, $sPackagePath);
  253. @unlink($sFilePath);
  254. if($mixedResult !== true) {
  255. if($bTransient)
  256. $this->emailNotify($mixedResult);
  257. return $mixedResult;
  258. }
  259. //--- Copy unarchived package.
  260. $sHomePath = '';
  261. $mixedResult = $this->performCopy($sPackagePath, $sHomePath);
  262. @bx_rrmdir($sPackagePath);
  263. if($mixedResult !== true) {
  264. if($bTransient)
  265. $this->emailNotify($mixedResult);
  266. return $mixedResult;
  267. }
  268. if(!$bAutoAction)
  269. return true;
  270. //--- Autoinstall the downloaded package if it's needed.
  271. $aResult = $this->perform($sHomePath, $sAutoAction, $aParams);
  272. if((int)$aResult['code'] != 0) {
  273. if($bTransient)
  274. $this->emailNotify($aResult['message']);
  275. return $aResult['message'];
  276. }
  277. return true;
  278. }
  279. protected function performWrite($aItem, &$sFilePath)
  280. {
  281. $iUmaskSave = umask(0);
  282. $sFilePath = BX_DIRECTORY_PATH_TMP . $aItem['name'];
  283. if(file_exists($sFilePath))
  284. @unlink($sFilePath);
  285. if(!$rHandler = fopen($sFilePath, 'w'))
  286. return _t('_adm_str_err_cannot_write');
  287. $sContent = urldecode($aItem['content']);
  288. if(!fwrite($rHandler, $sContent, strlen($sContent)))
  289. return _t('_adm_str_err_cannot_write');
  290. fclose($rHandler);
  291. umask($iUmaskSave);
  292. return true;
  293. }
  294. protected function performUnarchive($sFilePath, &$sPackagePath)
  295. {
  296. $iUmaskSave = umask(0);
  297. if(!class_exists('ZipArchive'))
  298. return _t('_adm_str_err_zip_not_available');
  299. $oZip = new ZipArchive();
  300. if($oZip->open($sFilePath) !== true)
  301. return _t('_adm_str_err_cannot_unzip_package');
  302. $sPackageFolder = '';
  303. if($oZip->numFiles > 0)
  304. $sPackageFolder = $oZip->getNameIndex(0);
  305. if(empty($sPackageFolder))
  306. return _t('_adm_str_err_cannot_unzip_package');
  307. $sPackagePath = BX_DIRECTORY_PATH_TMP . $sPackageFolder;
  308. if(file_exists($sPackagePath)) // remove existing tmp folder with the same name
  309. @bx_rrmdir($sPackagePath);
  310. if(!$oZip->extractTo(BX_DIRECTORY_PATH_TMP))
  311. return _t('_adm_str_err_cannot_unzip_package');
  312. $oZip->close();
  313. umask($iUmaskSave);
  314. return true;
  315. }
  316. protected function performCopy($sPackagePath, &$sHomePath)
  317. {
  318. if($this->bUseFtp) {
  319. $sLogin = getParam('sys_ftp_login');
  320. $sPassword = getParam('sys_ftp_password');
  321. $sPath = getParam('sys_ftp_dir');
  322. if(empty($sLogin) || empty($sPassword) || empty($sPath))
  323. return _t('_adm_str_err_no_ftp_info');
  324. bx_import('BxDolFtp');
  325. $oFile = new BxDolFtp($_SERVER['HTTP_HOST'], $sLogin, $sPassword, $sPath);
  326. if(!$oFile->connect())
  327. return _t('_adm_str_err_cannot_connect_to_ftp');
  328. if(!$oFile->isDolphin())
  329. return _t('_adm_str_err_destination_not_valid');
  330. }
  331. else {
  332. bx_import('BxDolFile');
  333. $oFile = BxDolFile::getInstance();
  334. }
  335. $aConfig = self::getModuleConfig($sPackagePath . 'install/config.php');
  336. if(empty($aConfig) || !is_array($aConfig) || empty($aConfig['home_dir']))
  337. return _t('_adm_str_err_wrong_package_format');
  338. $sHomePath = $aConfig['home_dir'];
  339. if(!$oFile->copy($sPackagePath, 'modules/' . $sHomePath))
  340. return _t('_adm_str_err_files_copy_failed');
  341. return true;
  342. }
  343. protected function getConfigModule($sConfigPath, $aInstalled = array())
  344. {
  345. $aConfig = self::getModuleConfig($sConfigPath);
  346. if(empty($aConfig) || !is_array($aConfig))
  347. return array();
  348. $bInstalled = !empty($aInstalled);
  349. $bEnabled = $bInstalled && (int)$aInstalled['enabled'] == 1;
  350. $sVersion = $bInstalled ? $aInstalled['version'] : $aConfig['version'];
  351. return array(
  352. 'type' => $aConfig['type'],
  353. 'name' => isset($aConfig['name']) ? $aConfig['name'] : $aConfig['home_uri'],
  354. 'title' => bx_process_output($aConfig['title']),
  355. 'vendor' => $aConfig['vendor'],
  356. 'version' => $sVersion,
  357. 'uri' => $aConfig['home_uri'],
  358. 'dir' => $aConfig['home_dir'],
  359. 'note' => isset($aConfig['note']) ? bx_process_output($aConfig['note']) : '',
  360. 'installed' => $bInstalled,
  361. 'enabled' => $bEnabled
  362. );
  363. }
  364. protected function getConfigUpdate($sConfigPathModule, $sConfigPathUpdate, $aInstalledModule = array())
  365. {
  366. $aModule = $this->getConfigModule($sConfigPathModule, $aInstalledModule);
  367. if(empty($aModule) || !$aModule['installed'])
  368. return array();
  369. $aConfig = self::getModuleConfig($sConfigPathUpdate);
  370. if(empty($aConfig) || !is_array($aConfig))
  371. return array();
  372. return array(
  373. 'title' => bx_process_output($aConfig['title']),
  374. 'vendor' => $aConfig['vendor'],
  375. 'version_from' => $aConfig['version_from'],
  376. 'version_to' => $aConfig['version_to'],
  377. 'dir' => $aConfig['home_dir'],
  378. 'module_type' => $aModule['type'],
  379. 'module_name' => $aModule['name'],
  380. 'module_dir' => $aModule['dir'],
  381. 'module_version' => $aModule['version']
  382. );
  383. }
  384. private function getInstalledInfo()
  385. {
  386. bx_import('BxDolModuleQuery');
  387. $aModules = BxDolModuleQuery::getInstance()->getModules();
  388. $aInstalledInfo = array();
  389. foreach($aModules as $aModule)
  390. $aInstalledInfo[$aModule['path']] = $aModule;
  391. return $aInstalledInfo;
  392. }
  393. private function addTransientJob($sAction, $aParams)
  394. {
  395. bx_import('BxDolCronQuery');
  396. if(BxDolCronQuery::getInstance()->addTransientJobService('sys_' . $sAction, array('system', $sAction, $aParams, 'DolStudioInstallerUtils')))
  397. return true;
  398. return false;
  399. }
  400. private function emailNotify($sMessage)
  401. {
  402. sendMailTemplateSystem('t_BgOperationFailed', array (
  403. 'conclusion' => strip_tags($sMessage),
  404. ));
  405. }
  406. private function getAccessObject($bAuthorizedAccess)
  407. {
  408. $sClass = $bAuthorizedAccess ? $this->sAuthorizedAccessClass : 'BxDolStudioJson';
  409. bx_import($sClass);
  410. return $sClass::getInstance();
  411. }
  412. }
  413. /** @} */