BxDolInstallerUtils.php 5.9 KB

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