BxDolFtp.php 5.5 KB

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