BxDolFtp.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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('BxDolFile');
  10. class BxDolFtp extends BxDolFile
  11. {
  12. protected $_sHost;
  13. protected $_sLogin;
  14. protected $_sPassword;
  15. protected $_sPathTo;
  16. protected $_rStream;
  17. function __construct($sHost, $sLogin, $sPassword, $sPath = '/')
  18. {
  19. parent::__construct();
  20. $this->_sHost = $sHost;
  21. $this->_sLogin = $sLogin;
  22. $this->_sPassword = $sPassword;
  23. $this->_sPathTo = $sPath . ('/' == substr($sPath, -1) ? '' : '/');
  24. }
  25. function connect()
  26. {
  27. $this->_rStream = ftp_connect($this->_sHost);
  28. return @ftp_login($this->_rStream, $this->_sLogin, $this->_sPassword);
  29. }
  30. function isDolphin()
  31. {
  32. return @ftp_size($this->_rStream, $this->_sPathTo . 'inc/header.inc.php') > 0;
  33. }
  34. function copy($sFilePathFrom, $sFilePathTo)
  35. {
  36. $sFilePathTo = $this->_sPathTo . $sFilePathTo;
  37. return $this->_copyFile($sFilePathFrom, $sFilePathTo);
  38. }
  39. function delete($sPath)
  40. {
  41. $sPath = $this->_sPathTo . $sPath;
  42. return $this->_deleteDirectory($sPath);
  43. }
  44. protected function _copyFile($sFilePathFrom, $sFilePathTo)
  45. {
  46. if(substr($sFilePathFrom, -1) == '*')
  47. $sFilePathFrom = substr($sFilePathFrom, 0, -1);
  48. $bResult = false;
  49. if(is_file($sFilePathFrom)) {
  50. if($this->_isFile($sFilePathTo)) {
  51. $aFileParts = $this->_parseFile($sFilePathTo);
  52. if(isset($aFileParts[0]))
  53. $this->_ftpMkDirR($aFileParts[0]);
  54. $bResult = @ftp_put($this->_rStream, $sFilePathTo, $sFilePathFrom, FTP_BINARY);
  55. } else if($this->_isDirectory($sFilePathTo)) {
  56. $this->_ftpMkDirR($sFilePathTo);
  57. $aFileParts = $this->_parseFile($sFilePathFrom);
  58. if(isset($aFileParts[1]))
  59. $bResult = @ftp_put($this->_rStream, $this->_validatePath($sFilePathTo) . $aFileParts[1], $sFilePathFrom, FTP_BINARY);
  60. }
  61. } else if(is_dir($sFilePathFrom) && $this->_isDirectory($sFilePathTo)) {
  62. $this->_ftpMkDirR($sFilePathTo);
  63. $aInnerFiles = $this->_readDirectory($sFilePathFrom);
  64. foreach($aInnerFiles as $sFile)
  65. $bResult = $this->_copyFile($this->_validatePath($sFilePathFrom) . $sFile, $this->_validatePath($sFilePathTo) . $sFile);
  66. } else
  67. $bResult = false;
  68. return $bResult;
  69. }
  70. protected function _deleteDirectory($sPath)
  71. {
  72. if($this->_isDirectory($sPath)) {
  73. if(substr($sPath, -1) != '/')
  74. $sPath .= '/';
  75. if(($aFiles = @ftp_nlist($this->_rStream, $sPath)) !== false)
  76. foreach($aFiles as $sFile)
  77. if($sFile != '.' && $sFile != '..')
  78. $this->_deleteDirectory(false === strpos($sFile, '/') ? $sPath . $sFile : $sFile);
  79. if(!@ftp_rmdir($this->_rStream, $sPath))
  80. return false;
  81. } else if(!@ftp_delete($this->_rStream, $sPath))
  82. return false;
  83. return true;
  84. }
  85. protected function _validatePath($sPath)
  86. {
  87. if($sPath && substr($sPath, -1) != '/' && $this->_isDirectory($sPath))
  88. $sPath .= '/';
  89. return $sPath;
  90. }
  91. protected function _parseFile($sFilePath)
  92. {
  93. $aParts = array();
  94. preg_match("/^([a-zA-Z0-9@~_\.\\\\\/:-]+[\\\\\/])([a-zA-Z0-9~_-]+\.[a-zA-Z]{2,8})$/", $sFilePath, $aParts) ? true : false;
  95. return count($aParts) > 1 ? array_slice($aParts, 1) : false;
  96. }
  97. protected function _isFile($sFilePath)
  98. {
  99. return preg_match("/^([a-zA-Z0-9@~_\.\\\\\/:-]+)\.([a-zA-Z]){2,8}$/", $sFilePath) ? true : false;
  100. }
  101. protected function _isDirectory($sFilePath)
  102. {
  103. return preg_match("/^([a-zA-Z0-9@~_\.\\\\\/:-]+)[\\\\\/]([a-zA-Z0-9~_-]+)[\\\\\/]?$/", $sFilePath) ? true : false;
  104. }
  105. protected function _setPermissions($sPath, $sMode)
  106. {
  107. $aConvert = array(
  108. 'writable' => $this->_isDirectory($sPath) ? 0777 : 0666,
  109. 'executable' => 0777
  110. );
  111. if(@ftp_chmod($this->_rStream, $aConvert[$sMode], $sPath) === false)
  112. return false;
  113. return true;
  114. }
  115. protected function _ftpMkDirR($sPath)
  116. {
  117. $sPwd = ftp_pwd ($this->_rStream);
  118. $aParts = explode("/", $sPath);
  119. $sPathFull = '';
  120. if ('/' == $sPath[0]) {
  121. $sPathFull = '/';
  122. ftp_chdir($this->_rStream, '/');
  123. }
  124. foreach ($aParts as $sPart) {
  125. if (!$sPart)
  126. continue;
  127. $sPathFull .= $sPart;
  128. if ('..' == $sPart) {
  129. @ftp_cdup($this->_rStream);
  130. } elseif (!@ftp_chdir($this->_rStream, $sPart)) {
  131. if (!@ftp_mkdir($this->_rStream, $sPart)) {
  132. ftp_chdir($this->_rStream, $sPwd);
  133. return false;
  134. }
  135. @ftp_chdir($this->_rStream, $sPart);
  136. }
  137. $sPathFull .= '/';
  138. }
  139. ftp_chdir($this->_rStream, $sPwd);
  140. return true;
  141. }
  142. }
  143. /** @} */