1
0

BxDolStudioInstallerUtils.php 15 KB

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