Вход Регистрация
Файл: adultscript-2.0.3-pro/files/libraries/framework/filesystem/ftp.php
Строк: 173
<?php
defined
('_VALID') or die('Restricted Access!');
class 
VFilesystem_Driver_ftp extends VFilesystem
{
    private 
$stream;
    private 
$host;
    private 
$username;
    private 
$password;
    private 
$basedir;
    private 
$port;
    private 
$ssl;
    private 
$passive;

    private 
$error  NULL;
    private 
$errors = array(
        
'connect-failed'    => 'Failed to connect to FTP!',
        
'login-failed'      => 'Failed to login to FTP server!',
        
'root-failed'       => 'Failed to change directory to the root folder!',
        
'mkdir-failed'      => 'Failed to create directory!',
        
'upload-failed'     => 'Failed to upload file (%s - %s)!',
        
'download-failed'   => 'Failed to download file (%s - %s)!',
        
'unknown'           => 'Unexpected Error while performing FTP operation!',
        
'local_missing'        => 'Local file/folder (%s) not found or not readable!'
    
);

    private 
$log;    
    private 
$debug    FALSE;

    public function 
__construct($options)
    {
        
$this->host        $options['host'];
        
$this->port        $options['port'];
        
$this->ssl        $options['ssl'];
        
$this->username    $options['username'];
        
$this->password    $options['password'];
        
$this->basedir    $options['basedir'];
        
$this->passive    = (isset($options['passive'])) ? $options['passive'] : TRUE;
        
        
$this->log        TMP_DIR.'/logs/filesystem.log';
        
        
$this->debug('Initializing FTP filesystem: '.http_build_query($options));
    }

    public function 
__destruct()
    {
        
$this->close();
    }

    public function 
connect()
    {
          
$this->debug('Connecting to '.$this->host.'...');
    
        
$this->close();

        
$connect        = ($this->ssl) ? 'ftp_ssl_connect' 'ftp_connect';
        
$this->stream   $connect($this->host$this->port);
        if (
$this->stream) {
              
$this->debug('Logging in...');
            if (@
ftp_login($this->stream$this->username$this->password)) {
                  if (
$this->basedir != '') {
                      
$this->debug('Changing directory to basedir...');
                      if (!@
ftp_chdir($this->stream$this->basedir)) {
                          
$this->error    $this->errors['root-failed'];
                      }
                  }
            
                  if (!
$this->error) {
                      if (
$this->passive) {
                          
$this->debug('Enabling passive mode...');
                          @
ftp_pasv($this->streamTRUE);
                      }
                      
                      return 
TRUE;
                }
            } else {
                
$this->error    'login-failed';
            }
        } else {
            
$this->error    'connect-failed';
        }
        
        return 
FALSE;
    }

    public function 
upload($local$remote$mode='auto'$perms='auto')
    {
          
$this->debug('Running upload: local='.$local.', remote='.$remote.', mode='.$mode.', perms='.$perms);
    
        if (!
file_exists($local) OR !is_readable($local)) {
              
$this->error    sprintf($this->errors['local_missing'], $local);
            return 
FALSE;
        }
                        
        if (
$mode == 'auto') {
              
$mode   $this->type($local);
        }
            
        
$mode   = ($mode == 'ascii') ? FTP_ASCII FTP_BINARY;
          
        if (
$perms == 'auto') {
              
$perms  substr(sprintf('%o'fileperms($local)), -4);
        }
        
        
// check if folder exists
        
$dir    dirname($remote);
        if (
$dir != '' && $dir != '/') {
              
$this->mkdir($dir);
        }
        
        
$this->debug('Uploading '.$local.' to '.$remote.' using '.$mode.' mode...');
        
        
$result = @ftp_put($this->stream$remote$local$mode);
        if (
$result === FALSE) {
              
$this->error    sprintf($this->errors['upload_failed'], $local$remote);
            return 
FALSE;
        }
        
        if (
is_int($perms)) {
              
$this->debug('Setting permissions for '.$remote.' to '.$perms);
              @
ftp_chmod($this->stream, (int) $perms$remote);
        }
            
        return 
TRUE;
    }
    
    public function 
mirror($local$remote)
    {
          if (!
file_exists($local) OR !is_readable($local)) {
              
$this->error    sprintf($this->errors['local_missing'], $local);
            return 
FALSE;
          }
          
          if (!
is_dir($local)) {
              
$this->error    sprintf($this->errors['local_not_a_folder'], $local);
              return 
FALSE;
          }
          
          
$dir    dir($local);
          while (
$file $dir->read()) {
              if (
$file == '.' OR $file == '..') {
                  continue;
              }
              
              if (
is_dir($local.'/'.$file)) {
                  
$this->mkdir($remote.$file);
                  
$this->mirror($local.'/'.$file$remote.$file.'/');
              } else {
                  
$this->upload($local.'/'.$file$remote.$file);
              }
          }
          
          
$dir->close();
          
          return 
TRUE;
    }
    
    public function 
download($remote$local$mode='auto')
    {
        if (!
$this->stream) {
            return 
FALSE;
        }
        
        if (
$mode == 'auto') {
            
$mode   $this->type($remote);
        }
        
        
$mode   = ($mode == 'ascii') ? FTP_ASCII FTP_BINARY;
        
$result = @ftp_get($this->stream$local$remote$mode);
        if (
$result == FALSE) {
            if (
$this->debug === TRUE) {
                
$this->error('ftp_download_failed');
            }
            
            return 
FALSE;
        }
        
        return 
TRUE;
    }

    public function 
delete($path)
    {
          
$this->debug('Deleting '.$path.'...');
    
        if (!
$this->stream) {
            return 
FALSE;
        }
        
        
$result = @ftp_delete($this->stream$path);
        if (
$result === FALSE) {
            if (
$this->debug === TRUE) {
                
$this->error('ftp_delete_failed');
            }
            
            return 
FALSE;
        }
        
        return 
TRUE;
    }

    
// recursive folder remove function
    
public function rmdir($path)
    {
        if (!
$this->stream) {
            return 
FALSE;
        }
        
        
$this->debug('Removing folder '.$path.'...');
        
        if (!(@
ftp_rmdir($this->stream$path) || @ftp_delete($this->stream$path))) {
            
$filelist   = @ftp_nlist($this->stream$path);
            foreach (
$filelist as $file) {
                
$this->rmdir($path.'/'.$file);
            }
            
            
$this->rmdir($path);
        }
        
        return 
TRUE;
    }
    
    
// recursive mkdir function
    
public function mkdir($dir)
    {
          
$this->debug('Creating folder: '.$dir.'...');
    
        if (
$this->is_dir($dir) OR @ftp_mkdir($this->stream$dir)) {
            return 
TRUE;
        }
        
        if (!
$this->mkdir(dirname($dir))) {
            
$this->error    sprintf($this->errors['mkdir_failed'], $dir);
            return 
FALSE;
        }
        
        return 
$this->mkdir($dir);
    }

    
// set permissions for file
    // if no mode is specified set 755 for folders and 644 for files
    
public function chmod($path$mode=NULL)
    {
          
$this->debug('Changing permissions of '.$path.' to '.$mode);
    
        if (
$mode === NULL) {
            
$mode   = ($this->is_dir($path)) ? 0755 0644;
        }
        
        return 
ftp_chmod($this->stream$mode$path);
    }

    private function 
is_dir($dir)
    {
          
$this->debug('Checking if '.$dir.' is a directory');
    
        
// get the current FTP folder
        
$current    ftp_pwd($this->stream);

        
// try to change the location to the folder
        
if (@ftp_chdir($this->stream$dir)) {
            
// if it is a folder change to original location
            
ftp_chdir($this->stream$current);

            return 
TRUE;
        }
        
        return 
FALSE;
    }

    private function 
chdir($dir)
    {
          
$this->debug('Changing directory to '.$dir);
    
        
$result = @ftp_chdir($this->stream$dir);
        if (
$result === FALSE) {
            if (
$this->debug === TRUE) {
                
$this->error('ftp_chdir_failed');
            }
            
            return 
FALSE;
        }
        
        return 
TRUE;
    }
    
    private function 
type($file)
    {
        
$ext    strtolower(substr($filestrrpos($file'.')+1));
        
$ascii  = array(
            
'txt',
            
'text',
            
'php',
            
'phps',
            
'php4',
            
'js',
            
'css',
            
'htm',
            
'html',
            
'phtml',
            
'shtml',
            
'log',
            
'xml'
        
);

        return (
in_array($ext$ascii)) ? 'ascii' 'binary';
    }

    public function 
get_error()
    {
        if (isset(
$this->errors[$this->error]) && !empty($this->errors[$this->error])) {
            return 
$this->errors[$this->error];
        }
        
        return 
$this->error;
    }
    
    public function 
close()
    {
        if (isset(
$this->stream) && $this->stream && is_resource($this->stream)) {
            
ftp_close($this->stream);
            
$this->stream NULL;
        }
    }
    
    private function 
debug($message)
    {
          if (
$this->debug === TRUE) {
              
VFile::write($this->log'['.date('Y-m-d h:i:s').']: '.$message."n"TRUE);
          }
    }
}
Онлайн: 0
Реклама