Bladeren bron

#73 - Modules autoupdate, manage files using BxDolFile.

AntonLV 12 jaren geleden
bovenliggende
commit
ad998b4b67

+ 44 - 20
inc/classes/BxDolFile.php

@@ -11,7 +11,6 @@ class BxDolFile extends BxDol implements iBxDolSingleton
 {
     protected $_sPathFrom;
     protected $_sPathTo;
-    protected $_bForceOverwrite = false;
 
     protected function __construct()
     {
@@ -43,8 +42,12 @@ class BxDolFile extends BxDol implements iBxDolSingleton
 
     function copy($sFilePathFrom, $sFilePathTo)
     {
-        $sFilePathFrom = $this->_sPathFrom . $sFilePathFrom;
-        $sFilePathTo = $this->_sPathTo . $sFilePathTo;
+    	if(substr($sFilePathFrom, 0, strlen($this->_sPathFrom)) != $this->_sPathFrom)
+        	$sFilePathFrom = $this->_sPathFrom . $sFilePathFrom;
+
+        if(substr($sFilePathTo, 0, strlen($this->_sPathTo)) != $this->_sPathTo)
+        	$sFilePathTo = $this->_sPathTo . $sFilePathTo;
+
         return $this->_copyFile($sFilePathFrom, $sFilePathTo);
     }
 
@@ -66,21 +69,8 @@ class BxDolFile extends BxDol implements iBxDolSingleton
         return $this->_setPermissions($sPath, $sMode);
     }
 
-    function setForceOverwrite($b)
-    {
-        $this->_bForceOverwrite = $b;
-    }
-
-    function getForceOverwrite()
-    {
-        return $this->_bForceOverwrite;
-    }
-
     protected function _copyFile($sFilePathFrom, $sFilePathTo)
     {
-        if(!$this->getForceOverwrite() && file_exists($sFilePathTo))
-            return true;
-
         if(substr($sFilePathFrom, -1) == '*')
             $sFilePathFrom = substr($sFilePathFrom, 0, -1);
 
@@ -89,12 +79,12 @@ class BxDolFile extends BxDol implements iBxDolSingleton
             if($this->_isFile($sFilePathTo)) {
                 $aFileParts = $this->_parseFile($sFilePathTo);
                 if(isset($aFileParts[0]))
-                    @mkdir($aFileParts[0]);
+                    $this->_mkDirR($aFileParts[0]);
 
                 return @copy($sFilePathFrom, $sFilePathTo);
             }
             else {
-                @mkdir($sFilePathTo);
+                $this->_mkDirR($sFilePathTo);
 
                 $aFileParts = $this->_parseFile($sFilePathFrom);
 				return @copy($sFilePathFrom, $this->_validatePath($sFilePathTo) . $aFileParts[1]);
@@ -102,8 +92,8 @@ class BxDolFile extends BxDol implements iBxDolSingleton
         }
 
         //--- Copy directory to directory
-        if(is_dir($sFilePathFrom) && $this->_isDirectory($sFilePathTo)) {
-            @mkdir($sFilePathTo);
+        if(is_dir($sFilePathFrom) && $this->_isDirectory($sFilePathTo)) {      	
+            $this->_mkDirR($sFilePathTo);
 
             $bResult = true;
             $aInnerFiles = $this->_readDirectory($sFilePathFrom);
@@ -202,6 +192,40 @@ class BxDolFile extends BxDol implements iBxDolSingleton
 
         return chmod($sPath, $aConvert[$sMode]);
     }
+
+	protected function _mkDirR($sPath)
+    {
+		$sPathFull = '';
+        $sPathCurrent = getcwd();
+
+        $aParts = explode('/', $sPath);
+    	if($sPath[0] == '/') {
+            $sPathFull = '/';
+            @chdir($sPathFull);
+        }
+
+        foreach($aParts as $sPart) {
+            if(!$sPart)
+				continue;
+
+            $sPathFull .= $sPart;
+            if($sPart == '..')
+                @chdir($sPart);
+            else if(!@chdir($sPart)) {
+                if(!@mkdir($sPart)) {
+                    @chdir($sPathCurrent);
+                    return false;
+                }
+
+                @chdir($sPart);
+            }
+
+            $sPathFull .= '/';
+        }
+
+        @chdir($sPathCurrent);
+        return true;
+    }
 }
 
 /** @} */

+ 9 - 10
inc/classes/BxDolFtp.php

@@ -14,7 +14,6 @@ class BxDolFtp extends BxDolFile
     protected $_sHost;
     protected $_sLogin;
     protected $_sPassword;
-    protected $_sPathTo;
     protected $_rStream;
 
     function __construct($sHost, $sLogin, $sPassword, $sPath = '/')
@@ -23,22 +22,21 @@ class BxDolFtp extends BxDolFile
         $this->_sHost = $sHost;
         $this->_sLogin = $sLogin;
         $this->_sPassword = $sPassword;
+
+        $this->_sPathFrom = '';
         $this->_sPathTo = $sPath . ('/' == substr($sPath, -1) ? '' : '/');
     }
+
     function connect()
     {
         $this->_rStream = ftp_connect($this->_sHost);
         return @ftp_login($this->_rStream, $this->_sLogin, $this->_sPassword);
     }
+
     function isDolphin()
     {
         return @ftp_size($this->_rStream, $this->_sPathTo . 'inc/header.inc.php') > 0;
     }
-    function copy($sFilePathFrom, $sFilePathTo)
-    {
-        $sFilePathTo = $this->_sPathTo . $sFilePathTo;
-        return $this->_copyFile($sFilePathFrom, $sFilePathTo);
-    }
 
     protected function _copyFile($sFilePathFrom, $sFilePathTo)
     {
@@ -50,17 +48,18 @@ class BxDolFtp extends BxDolFile
             if($this->_isFile($sFilePathTo)) {
                 $aFileParts = $this->_parseFile($sFilePathTo);
                 if(isset($aFileParts[0]))
-                    $this->_ftpMkDirR($aFileParts[0]);
+                    $this->_mkDirR($aFileParts[0]);
 
                 $bResult = @ftp_put($this->_rStream, $sFilePathTo, $sFilePathFrom, FTP_BINARY);
             } else if($this->_isDirectory($sFilePathTo)) {
-                $this->_ftpMkDirR($sFilePathTo);
+                $this->_mkDirR($sFilePathTo);
+
                 $aFileParts = $this->_parseFile($sFilePathFrom);
                 if(isset($aFileParts[1]))
                     $bResult = @ftp_put($this->_rStream, $this->_validatePath($sFilePathTo) . $aFileParts[1], $sFilePathFrom, FTP_BINARY);
             }
         } else if(is_dir($sFilePathFrom) && $this->_isDirectory($sFilePathTo)) {
-            $this->_ftpMkDirR($sFilePathTo);
+            $this->_mkDirR($sFilePathTo);
 
             $aInnerFiles = $this->_readDirectory($sFilePathFrom);
             foreach($aInnerFiles as $sFile)
@@ -116,7 +115,7 @@ class BxDolFtp extends BxDolFile
 
         return true;
     }
-    protected function _ftpMkDirR($sPath)
+    protected function _mkDirR($sPath)
     {
         $sPwd = ftp_pwd ($this->_rStream);
         $aParts = explode("/", $sPath);

+ 1 - 0
inc/classes/BxDolInstallerUtils.php

@@ -9,6 +9,7 @@
 bx_import('BxDolIO');
 
 define('BX_FORCE_AUTOUPDATE_MAX_CHANGED_FILES_PERCENT', 0.05);
+define('BX_FORCE_USE_FTP_FILE_TRANSFER', false);
 
 class BxDolInstallerUtils extends BxDolIO
 {

+ 1 - 2
modules/boonex/english/data/langs/system.xml

@@ -570,7 +570,6 @@
 	<string name="_adm_err_modules_already_uninstalled"><![CDATA[The module was already uninstalled.]]></string>
 	<string name="_adm_err_modules_cannot_add_to_cart"><![CDATA[Cannot add the product to shopping cart.]]></string>
 	<string name="_adm_err_modules_cannot_checkout_empty_vendor"><![CDATA[Cannot checkout. The vendor is unknown.]]></string>
-	<string name="_adm_err_modules_cannot_connect_to_ftp"><![CDATA[Cannot connect to FTP server]]></string>
 	<string name="_adm_err_modules_cannot_delete_from_cart"><![CDATA[Cannot delete the product(s) from shopping cart.]]></string>
 	<string name="_adm_err_modules_cannot_remove_package"><![CDATA[Cannot remove package.]]></string>
 	<string name="_adm_err_modules_module_not_found"><![CDATA[Module not found]]></string>
@@ -1167,7 +1166,7 @@
 	<string name="_adm_str_err_cannot_write"><![CDATA[Cannot save downloaded file.]]></string>
 	<string name="_adm_str_err_destination_not_valid"><![CDATA[Destination FTP folder doesn't look like a valid Dolphin root folder.]]></string>
 	<string name="_adm_str_err_download_failed"><![CDATA[Download faild. Please report.]]></string>
-	<string name="_adm_str_err_ftp_copy_failed"><![CDATA[FTP transfer failed, please check write permission for this FTP user.]]></string>
+	<string name="_adm_str_err_files_copy_failed"><![CDATA[File transfer failed, please check write permissions.]]></string>
 	<string name="_adm_str_err_no_ftp_info"><![CDATA[FTP access info was not specified. You may enter it via Settings -> General.]]></string>
 	<string name="_adm_str_err_no_product_info"><![CDATA[Cannot load product info. Try again later.]]></string>
 	<string name="_adm_str_err_wrong_package_format"><![CDATA[Wrong package format.]]></string>

+ 0 - 1
modules/boonex/russian/data/langs/system.xml

@@ -2388,7 +2388,6 @@ Without doing that you won't be submitted to our database.</p>
 	<string name="_adm_msg_modules_success_disable"><![CDATA[The package was disabled successfully]]></string>
 	<string name="_adm_err_modules_wrong_package_format"><![CDATA[Wrong package format]]></string>
 	<string name="_adm_err_modules_cannot_unzip_package"><![CDATA[Cannot unarchive the package]]></string>
-	<string name="_adm_err_modules_cannot_connect_to_ftp"><![CDATA[Cannot connect to FTP server]]></string>
 	<string name="_adm_err_modules_cannot_upload_package"><![CDATA[Cannot upload the package]]></string>
 	<string name="_adm_err_modules_cannot_remove_package"><![CDATA[Cannot remove package.]]></string>
 	<string name="_adm_err_modules_process_action_failed"><![CDATA[An error occured. Contact with module's vendor]]></string>

+ 18 - 15
studio/classes/BxDolStudioInstaller.php

@@ -32,6 +32,7 @@ define("BX_DOL_STUDIO_INSTALLER_FAILED", 1);
  * no alerts available
  *
  */
+
 class BxDolStudioInstaller extends BxDolInstallerUtils
 {
     protected $oDb;
@@ -41,6 +42,7 @@ class BxDolStudioInstaller extends BxDolInstallerUtils
     protected $_sHomePath;
     protected $_sModulePath;
 
+    protected $_bUseFtp;
     protected $_aActions;
 
     protected $_bShowOnSuccess = false;
@@ -56,6 +58,8 @@ class BxDolStudioInstaller extends BxDolInstallerUtils
         $this->_sHomePath = $this->_sBasePath . $aConfig['home_dir'];
         $this->_sModulePath = $this->_sBasePath . $aConfig['home_dir'];
 
+        $this->_bUseFtp = BX_FORCE_USE_FTP_FILE_TRANSFER;
+
         $this->_aActions = array(
             'perform_install' => array(
                 'title' => '',
@@ -318,19 +322,17 @@ class BxDolStudioInstaller extends BxDolInstallerUtils
 
     public function delete()
     {
-        bx_import('BxDolFtp');
-        $oFtp = new BxDolFtp($_SERVER['HTTP_HOST'], getParam('sys_ftp_login'), getParam('sys_ftp_password'), getParam('sys_ftp_dir'));
-        if(!$oFtp->connect())
-            return array(
-                'message' => _t('_adm_err_modules_cannot_connect_to_ftp'),
-                'result' => false
-            );
+    	$aError = array(
+			'message' => _t('_adm_err_modules_cannot_remove_package'),
+			'result' => false
+		);
 
-        if(!$oFtp->delete('modules/' . $this->_aConfig['home_dir']))
-            return array(
-                'message' => _t('_adm_err_modules_cannot_remove_package'),
-                'result' => false
-            );
+    	$oFile = $this->_getFileManager();
+    	if(empty($oFile))
+    		return $aError;
+
+        if(!$oFile->delete('modules/' . $this->_aConfig['home_dir']))
+            return $aError;
 
         return array(
             'message' => '', //_t('_adm_msg_modules_success_delete'),
@@ -812,11 +814,11 @@ class BxDolStudioInstaller extends BxDolInstallerUtils
     }
 
     //--- Protected methods ---//
-    protected function _getFileManager($bUseFtp = true)
+    protected function _getFileManager()
     {
         $oFile = null;
 
-        if($bUseFtp) {
+        if($this->_bUseFtp) {
             bx_import('BxDolFtp');
             $oFile = new BxDolFtp(isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'localhost', getParam('sys_ftp_login'), getParam('sys_ftp_password'), getParam('sys_ftp_dir'));
 
@@ -825,7 +827,8 @@ class BxDolStudioInstaller extends BxDolInstallerUtils
 
             if(!$oFile->isDolphin())
                 return null;
-        } else {
+        }
+        else {
             bx_import('BxDolFile');
             $oFile = BxDolFile::getInstance();
         }

+ 25 - 20
studio/classes/BxDolStudioInstallerUtils.php

@@ -213,7 +213,7 @@ class BxDolStudioInstallerUtils extends BxDolInstallerUtils implements iBxDolSin
 		return $this->downloadFile($aItem);
     }
 
-    protected function downloadFile($aItem)
+    protected function downloadFile($aItem, $bUseFtp = BX_FORCE_USE_FTP_FILE_TRANSFER)
     {
         if(empty($aItem) || !is_array($aItem))
             return $aItem;
@@ -246,28 +246,33 @@ class BxDolStudioInstallerUtils extends BxDolInstallerUtils implements iBxDolSin
         $oZip->close();
 
         //--- Move unarchived package.
-        $sLogin = getParam('sys_ftp_login');
-        $sPassword = getParam('sys_ftp_password');
-        $sPath = getParam('sys_ftp_dir');
-        if(empty($sLogin) || empty($sPassword) || empty($sPath))
-            return _t('_adm_str_err_no_ftp_info');
-
-        bx_import('BxDolFtp');
-        $oFtp = new BxDolFtp($_SERVER['HTTP_HOST'], $sLogin, $sPassword, $sPath);
-
-        if(!$oFtp->connect())
-            return _t('_adm_str_err_cannot_connect_to_ftp');
-
-        if(!$oFtp->isDolphin())
-            return _t('_adm_str_err_destination_not_valid');
+        if($bUseFtp) {
+	        $sLogin = getParam('sys_ftp_login');
+	        $sPassword = getParam('sys_ftp_password');
+	        $sPath = getParam('sys_ftp_dir');
+	        if(empty($sLogin) || empty($sPassword) || empty($sPath))
+	            return _t('_adm_str_err_no_ftp_info');
+	
+	        bx_import('BxDolFtp');
+	        $oFile = new BxDolFtp($_SERVER['HTTP_HOST'], $sLogin, $sPassword, $sPath);
+	
+	        if(!$oFile->connect())
+	            return _t('_adm_str_err_cannot_connect_to_ftp');
+	
+	        if(!$oFile->isDolphin())
+	            return _t('_adm_str_err_destination_not_valid');
+        }
+        else {
+        	bx_import('BxDolFile');
+        	$oFile = BxDolFile::getInstance();
+        }
 
-        $sConfigPath = BX_DIRECTORY_PATH_TMP . $sPackageRootFolder . '/install/config.php';
-        if(!file_exists($sConfigPath))
+        $aConfig = self::getModuleConfig(BX_DIRECTORY_PATH_TMP . $sPackageRootFolder . 'install/config.php');
+        if(empty($aConfig) || !is_array($aConfig) || empty($aConfig['home_dir']))
             return _t('_adm_str_err_wrong_package_format');
 
-		$aConfig = self::getModuleConfig($sConfigPath);
-        if(empty($aConfig) || empty($aConfig['home_dir']) || !$oFtp->copy(BX_DIRECTORY_PATH_TMP . $sPackageRootFolder . '/', 'modules/' . $aConfig['home_dir']))
-            return _t('_adm_str_err_ftp_copy_failed');
+        if(!$oFile->copy(BX_DIRECTORY_PATH_TMP . $sPackageRootFolder, 'modules/' . $aConfig['home_dir']))
+            return _t('_adm_str_err_files_copy_failed');
 
         return true;
     }

+ 4 - 5
studio/classes/BxDolStudioUpdater.php

@@ -101,12 +101,11 @@ class BxDolStudioUpdater extends BxDolStudioInstaller
         if(!file_exists($sPath))
             return BX_DOL_STUDIO_INSTALLER_FAILED;
 
-        bx_import('BxDolFtp');
-        $oFtp = new BxDolFtp($_SERVER['HTTP_HOST'], getParam('sys_ftp_login'), getParam('sys_ftp_password'), getParam('sys_ftp_dir'));
-        if($oFtp->connect() == false)
-            return BX_DOL_STUDIO_INSTALLER_FAILED;
+		$oFile = $this->_getFileManager();
+		if(empty($oFile))
+			return BX_DOL_STUDIO_INSTALLER_FAILED;
 
-        return $oFtp->copy($sPath . '*', 'modules/' . $this->_aConfig['module_dir']) ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
+        return $oFile->copy($sPath . '*', 'modules/' . $this->_aConfig['module_dir']) ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
     }
 
     public function actionUpdateLanguages($bInstall = true)

+ 0 - 1
upgrade/classes/BxDolUpgradeUtil.php

@@ -310,7 +310,6 @@ class BxDolUpgradeUtil
         // TODO: use standalone copying 
         require_once(BX_DIRECTORY_PATH_CLASSES . 'BxDolFile.php');
         $o = BxDolFile::getInstance();
-        $o->setForceOverwrite(true);
 
         $sDirSource = bx_ltrim_str(BX_UPGRADE_DIR_UPGRADES . $this->sFolder . '/files/', BX_DIRECTORY_PATH_ROOT);
         if (!$o->copy($sDirSource, ''))