BxDolInstallerUtils.php 5.4 KB

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