BxDolFtp.php 4.7 KB

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