BxDolInstallerUtils.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 UnaCore UNA Core
  7. * @{
  8. */
  9. if (!defined('BX_FORCE_AUTOUPDATE_MAX_CHANGED_FILES_PERCENT'))
  10. define('BX_FORCE_AUTOUPDATE_MAX_CHANGED_FILES_PERCENT', 0.05);
  11. define('BX_FORCE_USE_FTP_FILE_TRANSFER', false);
  12. class BxDolInstallerUtils extends BxDolIO
  13. {
  14. protected $_aNonHashableFiles = array();
  15. public function __construct()
  16. {
  17. parent::__construct();
  18. }
  19. static public function isAllowUrlInclude()
  20. {
  21. if (version_compare(phpversion(), "5.2", ">") == 1 && version_compare(phpversion(), "8.0", "<") == 1) {
  22. $sAllowUrlInclude = ini_get('allow_url_include');
  23. return !($sAllowUrlInclude == 0);
  24. };
  25. return false;
  26. }
  27. static public function getModuleConfig($mixed)
  28. {
  29. $sConfig = '';
  30. if(is_array($mixed) && !empty($mixed['path']))
  31. $sConfig = BX_DIRECTORY_PATH_MODULES . $mixed['path'] . 'install/config.php';
  32. else if(is_string($mixed))
  33. $sConfig = $mixed;
  34. else
  35. return array();
  36. if(!file_exists($sConfig))
  37. return array();
  38. ob_start();
  39. include($sConfig);
  40. ob_end_clean();
  41. return isset($aConfig) ? $aConfig : array();
  42. }
  43. static public function isModuleInstalled($sUri)
  44. {
  45. return BxDolModuleQuery::getInstance()->isModule($sUri);
  46. }
  47. /**
  48. * Set module for delayed uninstall
  49. */
  50. static public function setModulePendingUninstall($sUri, $bPendingUninstall = true)
  51. {
  52. return BxDolModuleQuery::getInstance()->setModulePendingUninstall($sUri, $bPendingUninstall);
  53. }
  54. /**
  55. * Check if module is pending for uninstall
  56. */
  57. static public function isModulePendingUninstall($sUri)
  58. {
  59. $a = BxDolModuleQuery::getInstance()->getModuleByUri($sUri);
  60. return $a['pending_uninstall'];
  61. }
  62. /**
  63. * Check all pending for uninstallation modules and uninstall them if no pending for deletion files are found
  64. */
  65. static public function checkModulesPendingUninstall()
  66. {
  67. $a = BxDolModuleQuery::getInstance()->getModules();
  68. foreach ($a as $aModule) {
  69. // after we make sure that all pending for deletion files are deleted
  70. if (!$aModule['pending_uninstall'] || BxDolStorage::isQueuedFilesForDeletion($aModule['name']))
  71. continue;
  72. // remove pending uninstall flag
  73. self::setModulePendingUninstall($aModule['uri'], false);
  74. // perform uninstallation
  75. $aResult = BxDolStudioInstallerUtils::getInstance()->perform($aModule['path'], 'uninstall');
  76. // send email nofitication
  77. $aTemplateKeys = array(
  78. 'Module' => $aModule['title'],
  79. 'Result' => _t('_Success'),
  80. 'Message' => '',
  81. );
  82. if ($aResult['code'] > 0) {
  83. $aTemplateKeys['Result'] = _t('_Failed');
  84. $aTemplateKeys['Message'] = $aResult['message'];
  85. }
  86. $aMessage = BxDolEmailTemplates::getInstance()->parseTemplate('t_DelayedModuleUninstall', $aTemplateKeys);
  87. sendMail (getParam('site_email'), $aMessage['Subject'], $aMessage['Body'], 0, array(), BX_EMAIL_SYSTEM);
  88. }
  89. }
  90. public function getSubtypes($sModule)
  91. {
  92. $iResult = 0;
  93. $oModule = BxDolModule::getInstance($sModule);
  94. if(!$oModule)
  95. return $iResult;
  96. $sMethod = 'getSubtypes';
  97. if(method_exists($oModule, $sMethod))
  98. return $oModule->$sMethod();
  99. $sSrvMethod = 'act_as_profile';
  100. if($oModule instanceof iBxDolProfileService && bx_is_srv_ii($sModule, $sSrvMethod)) {
  101. $iResult |= pow(2, BX_DOL_MODULE_SUBTYPE_CONTEXT);
  102. if(bx_srv_ii($sModule, $sSrvMethod))
  103. $iResult |= pow(2, BX_DOL_MODULE_SUBTYPE_PROFILE);
  104. return $iResult;
  105. }
  106. $sSrvMethod = 'is_allowed_post_in_context';
  107. if($oModule instanceof iBxDolContentInfoService && !($oModule instanceof iBxDolProfileService) && bx_is_srv_ii($sModule, $sSrvMethod) && bx_srv_ii($sModule, $sSrvMethod)) {
  108. $iResult |= pow(2, BX_DOL_MODULE_SUBTYPE_TEXT);
  109. if($oModule instanceof BxBaseModFilesModule)
  110. $iResult |= pow(2, BX_DOL_MODULE_SUBTYPE_FILE);
  111. return $iResult;
  112. }
  113. if($oModule instanceof BxBaseModPaymentModule) {
  114. $iResult |= pow(2, BX_DOL_MODULE_SUBTYPE_PAYMENT);
  115. return $iResult;
  116. }
  117. if($oModule instanceof BxBaseModNotificationsModule) {
  118. $iResult |= pow(2, BX_DOL_MODULE_SUBTYPE_NOTIFICATION);
  119. return $iResult;
  120. }
  121. if($oModule instanceof BxBaseModConnectModule) {
  122. $iResult |= pow(2, BX_DOL_MODULE_SUBTYPE_CONNECT);
  123. return $iResult;
  124. }
  125. return $iResult;
  126. }
  127. /**
  128. * Generate hash for module files.
  129. * @param $sPath module's root folder
  130. * @param $aFiles array to fill with files hashes
  131. */
  132. public function hashFiles($sPath, &$aFiles)
  133. {
  134. $aExcludes = array('.', '..', 'error_log', 'php.ini', '.DS_Store', 'Thumbs.db');
  135. if (file_exists($sPath) && is_dir($sPath) && ($rSource = opendir($sPath))) {
  136. while (($sFile = readdir($rSource)) !== false) {
  137. if ('.' == $sFile[0] || in_array($sFile, $aExcludes))
  138. continue;
  139. if (in_array($this->filePathWithoutBase($sPath . $sFile), $this->_aNonHashableFiles))
  140. continue;
  141. if (is_dir($sPath . $sFile))
  142. $this->hashFiles($sPath . $sFile . '/', $aFiles);
  143. else
  144. $aFiles[] = $this->hashInfo($sPath . $sFile);
  145. }
  146. closedir($rSource);
  147. } elseif (file_exists($sPath) && is_file($sPath)) {
  148. $aFiles[] = $this->hashInfo($sPath);
  149. }
  150. }
  151. /**
  152. * Check module's files hashes. For system files use @see BxDolInstallerHasher class.
  153. * @param $aFiles current files checksums
  154. * @param $iModuleId module id
  155. * @return empty array on success, or array of files which checksum was changed
  156. */
  157. public function hashCheck($aFiles, $iModuleId)
  158. {
  159. $oDb = bx_instance('BxDolStudioInstallerQuery');
  160. $aFilesOrig = $oDb->getModuleTrackFiles($iModuleId);
  161. $aFailesChanged = array();
  162. foreach ($aFiles as $aFile)
  163. if(!isset($aFilesOrig[$aFile['file']]) || $aFilesOrig[$aFile['file']]['hash'] != $aFile['hash'])
  164. $aFailesChanged[] = $aFile['file'];
  165. $fChangedPercent = 0;
  166. if(count($aFilesOrig) != 0)
  167. $fChangedPercent = count($aFailesChanged) / count($aFilesOrig);
  168. return array($aFailesChanged, $fChangedPercent);
  169. }
  170. protected function hashInfo($sPath)
  171. {
  172. return array(
  173. 'file' => $this->filePathWithoutBase($sPath),
  174. 'hash' => md5_file($sPath)
  175. );
  176. }
  177. protected function filePathWithoutBase($sPath)
  178. {
  179. return bx_ltrim_str($sPath, BX_DIRECTORY_PATH_ROOT);
  180. }
  181. }
  182. /** @} */