BxDolStudioInstallerUtils.php 16 KB

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