Quellcode durchsuchen

#73 - hashing remake

Alex Trofimov vor 12 Jahren
Ursprung
Commit
979df11f73

+ 41 - 0
inc/classes/BxDolInstallerUtils.php

@@ -11,6 +11,8 @@ bx_import('BxDolIO');
 
 class BxDolInstallerUtils extends BxDolIO
 {
+    protected $_aNonHashableFiles = array();
+
     function __construct()
     {
         parent::__construct();
@@ -119,6 +121,45 @@ class BxDolInstallerUtils extends BxDolIO
             sendMail (getParam('site_email'), $aMessage['Subject'], $aMessage['Body'], 0, array(), BX_EMAIL_SYSTEM);
         }
     }
+
+    public function setNonHashableFiles($a)
+    {
+        $this->_aNonHashableFiles = $a;
+    }
+
+    public function hashFiles($sPath, &$aFiles)
+    {
+        if (file_exists($sPath) && is_dir($sPath) && ($rSource = opendir($sPath))) {
+            while (($sFile = readdir($rSource)) !== false) {
+                if ($sFile == '.' || $sFile =='..' || $sFile[0] == '.')
+                    continue;
+                
+                if (in_array($this->filePathWithoutBase($sPath . $sFile), $this->_aNonHashableFiles))
+                    continue;                
+
+                if (is_dir($sPath . $sFile))
+                    $this->hashFiles($sPath . $sFile . '/', $aFiles);
+                else
+                    $aFiles[] = $this->hashInfo($sPath . $sFile);
+            }
+            closedir($rSource);
+        } else {
+            $aFiles[] = $this->hashInfo($sPath, $sFile);
+        }
+    }
+
+    protected function hashInfo($sPath)
+    {
+        return array(
+            'file' => $this->filePathWithoutBase($sPath),
+            'hash' => md5(file_get_contents($sPath))
+        );
+    }
+
+    protected function filePathWithoutBase($sPath)
+    {
+        return bx_ltrim_str($sPath, BX_DIRECTORY_PATH_ROOT);
+    }
 }
 
 /** @} */

+ 7 - 25
studio/classes/BxDolStudioInstaller.php

@@ -43,7 +43,6 @@ class BxDolStudioInstaller extends BxDolInstallerUtils
     protected $_sModulePath;
 
     protected $_aActions;
-    protected $_aNonHashable;
 
     protected $_bShowOnSuccess = false;
 
@@ -149,10 +148,11 @@ class BxDolStudioInstaller extends BxDolInstallerUtils
                 'title' => _t('_adm_txt_modules_show_conclusion'),
             ),
         );
-        $this->_aNonHashable = array(
+
+        $this->setNonHashableFiles(array(
             'install',
             'updates'
-        );
+        ));
     }
 
     public function install($aParams, $bAutoEnable = false)
@@ -230,7 +230,7 @@ class BxDolStudioInstaller extends BxDolInstallerUtils
             $oLanguages->compileLanguage();
 
             $aFiles = array();
-            $this->_hash($this->_sModulePath, $aFiles);
+            $this->hashFiles($this->_sModulePath, $aFiles);
 
             foreach($aFiles as $aFile)
                 $this->oDb->insertModuleTrack($iModuleId, $aFile);
@@ -451,29 +451,11 @@ class BxDolStudioInstaller extends BxDolInstallerUtils
         return $aResult;
     }
 
-    function _hash($sPath, &$aFiles)
-    {
-        if(file_exists($sPath) && is_dir($sPath) && ($rSource = opendir($sPath))) {
-            while(($sFile = readdir($rSource)) !== false) {
-                if($sFile == '.' || $sFile =='..' || $sFile[0] == '.' || in_array($sFile, $this->_aNonHashable))
-                    continue;
-
-                if(is_dir($sPath . $sFile))
-                    $this->_hash($sPath . $sFile . '/', $aFiles);
-                else
-                    $aFiles[] = $this->_info($sPath . $sFile);
-            }
-            closedir($rSource);
-        } else
-            $aFiles[] = $this->_info($sPath, $sFile);
-    }
-    function _info($sPath)
+    protected function filePathWithoutBase($sPath)
     {
-        return array(
-            'file' => str_replace($this->_sModulePath, '', $sPath),
-            'hash' => md5(file_get_contents($sPath))
-        );
+        return bx_ltrim_str($sPath, $this->_sModulePath);
     }
+
     function _perform($sOperationName)
     {
         if(!defined('BX_SKIP_INSTALL_CHECK') && !defined('BX_DOL_CRON_EXECUTE') && !$GLOBALS['logged']['admin'])

+ 2 - 2
studio/classes/BxDolStudioUpdater.php

@@ -61,7 +61,7 @@ class BxDolStudioUpdater extends BxDolStudioInstaller
         $aFilesOrig = $oDb->getAllWithKey($sQuery, "file");
 
         $aFiles = array();
-        $this->_hash($this->_sModulePath, $aFiles);
+        $this->hashFiles($this->_sModulePath, $aFiles);
         foreach($aFiles as $aFile)
             if(!isset($aFilesOrig[$aFile['file']]) || $aFilesOrig[$aFile['file']]['hash'] != $aFile['hash'])
                 return array_merge($aResult, array(
@@ -78,7 +78,7 @@ class BxDolStudioUpdater extends BxDolStudioInstaller
             $oDb->query($sQuery);
 
             $aFiles = array();
-            $this->_hash(BX_DIRECTORY_PATH_ROOT . 'modules/' . $this->_aConfig['module_dir'], $aFiles);
+            $this->hashFiles(BX_DIRECTORY_PATH_ROOT . 'modules/' . $this->_aConfig['module_dir'], $aFiles);
             foreach($aFiles as $aFile) {
                 $sQuery = $oDb->prepare("INSERT IGNORE INTO `sys_modules_file_tracks`(`module_id`, `file`, `hash`) VALUES(?, ?, ?)", $aModuleInfo['id'], $aFile['file'], $aFile['hash']);
                 $oDb->query($sQuery);