Cheetah
ChWsbFtp.php
Go to the documentation of this file.
1 <?php
2 
8 class ChWsbFtp
9 {
10  var $_sHost;
11  var $_sLogin;
13  var $_sPath;
14  var $_rStream;
15 
16  function __construct($sHost, $sLogin, $sPassword, $sPath = '/')
17  {
18  $this->_sHost = $sHost;
19  $this->_sLogin = $sLogin;
20  $this->_sPassword = $sPassword;
21  $this->_sPath = $sPath . ('/' == substr($sPath, -1) ? '' : '/');
22  }
23  function connect()
24  {
25  $this->_rStream = ftp_connect($this->_sHost);
26  if($this->_rStream === false)
27  return false;
28 
29  return @ftp_login($this->_rStream, $this->_sLogin, $this->_sPassword);
30  }
31  function isCheetah()
32  {
33  return @ftp_size($this->_rStream, $this->_sPath . 'inc/header.inc.php') > 0;
34  }
35  function copy($sFilePathFrom, $sFilePathTo)
36  {
37  $sFilePathTo = $this->_sPath . $sFilePathTo;
38  return $this->_copyFile($sFilePathFrom, $sFilePathTo);
39  }
40  function delete($sPath)
41  {
42  $sPath = $this->_sPath . $sPath;
43  return $this->_deleteDirectory($sPath);
44  }
45  function setPermissions($sPath, $sMode)
46  {
47  $sPath = $this->_sPath . $sPath;
48  return $this->_setPermissions($sPath, $sMode);
49  }
50 
51  function _copyFile($sFilePathFrom, $sFilePathTo)
52  {
53  if(substr($sFilePathFrom, -1) == '*')
54  $sFilePathFrom = substr($sFilePathFrom, 0, -1);
55  $bResult = false;
56  if (is_file($sFilePathFrom)) {
57  if ($this->_isFile($sFilePathTo)) {
58  $aFileParts = $this->_parseFile($sFilePathTo);
59  if (isset($aFileParts[0])) {
60  $bRet = $this->_ftpMkDirR($aFileParts[0]);
61  }
62  $bResult = @ftp_put($this->_rStream, $sFilePathTo, $sFilePathFrom, FTP_BINARY);
63  } else if($this->_isDirectory($sFilePathTo)) {
64  $bRet = $this->_ftpMkDirR($sFilePathTo);
65  $aFileParts = $this->_parseFile($sFilePathFrom);
66  if (isset($aFileParts[1])) {
67  $bResult = @ftp_put($this->_rStream, $this->_validatePath($sFilePathTo) . $aFileParts[1], $sFilePathFrom, FTP_BINARY);
68  }
69  }
70  } else if(is_dir($sFilePathFrom) && $this->_isDirectory($sFilePathTo)) {
71  $bRet = $this->_ftpMkDirR($sFilePathTo);
72  $aInnerFiles = $this->_readDirectory($sFilePathFrom);
73  foreach($aInnerFiles as $sFile)
74  $bResult = $this->_copyFile($this->_validatePath($sFilePathFrom) . $sFile, $this->_validatePath($sFilePathTo) . $sFile);
75  } else {
76  $bResult = false;
77  }
78 
79  return $bResult;
80  }
81  function _readDirectory($sFilePath)
82  {
83  if(!is_dir($sFilePath) || !($rSource = opendir($sFilePath))) return false;
84 
85  $aResult = array();
86  while(($sFile = readdir($rSource)) !== false) {
87  if($sFile == '.' || $sFile =='..' || $sFile[0] == '.') continue;
88  $aResult[] = $sFile;
89  }
90  closedir($rSource);
91 
92  return $aResult;
93  }
94  function _deleteDirectory($sPath)
95  {
96  if ($this->_isDirectory($sPath)) {
97  if (substr($sPath, -1) != '/')
98  $sPath .= '/';
99 
100  if (($aFiles = @ftp_nlist($this->_rStream, $sPath)) !== false)
101  foreach ($aFiles as $sFile)
102  if ($sFile != '.' && $sFile != '..')
103  $this->_deleteDirectory(false === strpos($sFile, '/') ? $sPath . $sFile : $sFile);
104 
105  if (!@ftp_rmdir($this->_rStream, $sPath))
106  return false;
107 
108  } else if (!@ftp_delete($this->_rStream, $sPath)) {
109  return false;
110  }
111  return true;
112  }
113  function _validatePath($sPath)
114  {
115  if($sPath && substr($sPath, -1) != '/' && $this->_isDirectory($sPath))
116  $sPath .= '/';
117 
118  return $sPath;
119  }
120  function _parseFile($sFilePath)
121  {
122  $aParts = array();
123  preg_match("/^([a-zA-Z0-9@~_\.\\\\\/:-]+[\\\\\/])([a-zA-Z0-9~_-]+\.[a-zA-Z]{2,8})$/", $sFilePath, $aParts) ? true : false;
124  return count($aParts) > 1 ? array_slice($aParts, 1) : false;
125  }
126  function _isFile($sFilePath)
127  {
128  return preg_match("/^([a-zA-Z0-9@~_\.\\\\\/:-]+)\.([a-zA-Z]){2,8}$/", $sFilePath) ? true : false;
129  }
130  function _isDirectory($sFilePath)
131  {
132  return preg_match("/^([a-zA-Z0-9@~_\.\\\\\/:-]+)[\\\\\/]([a-zA-Z0-9~_-]+)[\\\\\/]?$/", $sFilePath) ? true : false;
133  }
134  protected function _setPermissions($sPath, $sMode)
135  {
136  $aConvert = array(
137  'writable' => $this->_isDirectory($sPath) ? 0777 : 0666,
138  'executable' => 0777
139  );
140 
141  if(@ftp_chmod($this->_rStream, $aConvert[$sMode], $sPath) === false)
142  return false;
143 
144  return true;
145  }
146  function _ftpMkDirR($sPath)
147  {
148  $sPwd = ftp_pwd ($this->_rStream);
149  $aParts = explode("/", $sPath);
150  $sPathFull = '';
151  if ('/' == $sPath[0]) {
152  $sPathFull = '/';
153  ftp_chdir($this->_rStream, '/');
154  }
155  foreach ($aParts as $sPart) {
156  if (!$sPart)
157  continue;
158  $sPathFull .= $sPart;
159  if ('..' == $sPart) {
160  @ftp_cdup($this->_rStream);
161  } elseif (!@ftp_chdir($this->_rStream, $sPart)) {
162  if (!@ftp_mkdir($this->_rStream, $sPart)) {
163  ftp_chdir($this->_rStream, $sPwd);
164  return false;
165  }
166  @ftp_chdir($this->_rStream, $sPart);
167  }
168  $sPathFull .= '/';
169  }
170  ftp_chdir($this->_rStream, $sPwd);
171  return true;
172  }
173 
174 }
true
if(!defined("TRUE_VAL")) define("TRUE_VAL" true
Definition: constants.inc.php:8
$sMode
else $sMode
Definition: antispam.php:362
ChWsbFtp\connect
connect()
Definition: ChWsbFtp.php:23
ChWsbFtp\__construct
__construct($sHost, $sLogin, $sPassword, $sPath='/')
Definition: ChWsbFtp.php:16
$aResult
$aResult
Definition: index.php:19
ChWsbFtp\setPermissions
setPermissions($sPath, $sMode)
Definition: ChWsbFtp.php:45
php
ChWsbFtp\_validatePath
_validatePath($sPath)
Definition: ChWsbFtp.php:113
$sPwd
$sPwd
Definition: r.php:14
ChWsbFtp\$_sLogin
$_sLogin
Definition: ChWsbFtp.php:11
$sPassword
$sPassword
Definition: actions.inc.php:10
ChWsbFtp\_ftpMkDirR
_ftpMkDirR($sPath)
Definition: ChWsbFtp.php:146
$sFile
$sFile
Definition: index.php:20
ChWsbFtp\$_sHost
$_sHost
Definition: ChWsbFtp.php:10
ChWsbFtp\_copyFile
_copyFile($sFilePathFrom, $sFilePathTo)
Definition: ChWsbFtp.php:51
ChWsbFtp\$_sPassword
$_sPassword
Definition: ChWsbFtp.php:12
ChWsbFtp\_parseFile
_parseFile($sFilePath)
Definition: ChWsbFtp.php:120
ChWsbFtp\_setPermissions
_setPermissions($sPath, $sMode)
Definition: ChWsbFtp.php:134
$bResult
$bResult
Definition: get_file.php:11
ChWsbFtp\copy
copy($sFilePathFrom, $sFilePathTo)
Definition: ChWsbFtp.php:35
ChWsbFtp\_readDirectory
_readDirectory($sFilePath)
Definition: ChWsbFtp.php:81
ChWsbFtp\_deleteDirectory
_deleteDirectory($sPath)
Definition: ChWsbFtp.php:94
ChWsbFtp\$_sPath
$_sPath
Definition: ChWsbFtp.php:13
ChWsbFtp\_isFile
_isFile($sFilePath)
Definition: ChWsbFtp.php:126
as
as
Definition: Filter.ExtractStyleBlocks.Escaping.txt:10
ChWsbFtp\$_rStream
$_rStream
Definition: ChWsbFtp.php:14
ChWsbFtp\isCheetah
isCheetah()
Definition: ChWsbFtp.php:31
ChWsbFtp
Definition: ChWsbFtp.php:9
ChWsbFtp\_isDirectory
_isDirectory($sFilePath)
Definition: ChWsbFtp.php:130