Файл: adultscript-2.0.3-pro/files/libraries/framework/ftp.php
Строк: 129
<?php
defined('_VALID') or die('Restricted Access!');
class VFtp
{
private $stream;
private $host;
private $username;
private $password;
private $root;
private $port;
private $ssl;
private $error = NULL;
private $errors = array(
'ftp_connect-failed' => 'Failed to connect to FTP!',
'ftp_login-failed' => 'Failed to login to FTP server!',
'ft_root-failed' => 'Failed to change directory to the root folder!',
'ftp_mkdir-failed' => 'Failed to create directory!',
'ftp_upload_failed' => 'Failed to upload file!',
'ftp_download_failed' => 'Failed to download file!',
'ftp_unknown' => 'Unexpected Error while performing FTP operation!';
);
private $debug = FALSE;
public function __construct($options=array())
{
$this->host = $options['host'];
$this->username = $options['username'];
$this->password = $options['password'];
$this->root = $options['root'];
$this->port = $options['port'];
$this->ssl = $options['ssl'];
$this->debug = (VF::cfg_item('debug') == '1') ? TRUE : FALSE;
}
public function __destruct()
{
$this->close();
}
public function connect()
{
$this->close();
$connect = ($this->ssl) ? 'ftp_ssl_connect' : 'ftp_connect';
$this->stream = $connect($this->host, $this->port);
if ($this->stream) {
if (ftp_login($this->stream, $this->username, $this->password)) {
if (ftp_chdir($this->stream, $this->root)) {
return TRUE;
}
$this->error = 'root-failed';
} else {
$this->error = 'login-failed';
}
} else {
$this->error = 'connect-failed';
}
return FALSE;
}
public function upload($local, $remote, $mode='auto', $perms='auto', $recursive=TRUE)
{
if (!file_exists($local)) {
return FALSE;
}
if (is_file($local)) {
if ($mode == 'auto') {
$mode = $this->type($local);
}
$mode = ($mode == 'ascii') ? FTP_ASCII : FTP_BINARY;
if ($perms == 'auto') {
$perms = substr(sprintf('%o', fileperms($local)), -4);
}
$result = @ftp_put($this->stream, $remote, $local, $mode);
if ($result === FALSE) {
if ($this->debug === TRUE) {
$this->error('ftp_upload_failed');
}
return FALSE;
}
if (is_int($perms)) {
@ftp_chmod($this->stream, (int) $perms, $remote);
}
return TRUE;
}
// we have a folder lets start recursive upload
if (is_dir($local) && $recursive === TRUE) {
if ($fp = @opendir($local)) {
if (!$this->is_dir($remote)) {
// remote folder does not exist, create and change directory
if (!$this->mkdir($remote) OR !$this->chdir($remote)) {
return FALSE;
}
}
while (FALSE !== ($file = readdir($fp)) {
if (@is_dir($local.$file) && substr($file, 0, 1) != '.') {
$this->upload($local.$file.'/', $remote.$file.'/', $mode, $perms, $recursive);
} elseif (substr($file, 0, 1) != '.')) {
$this->upload($local.$file, $remote.$file, $mode, $perms, $recursive);
}
}
return TRUE;
}
return FALSE;
}
}
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)
{
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;
}
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)
{
if ($this->is_dir($dir) OR @ftp_mkdir($this->stream, $dir)) {
return TRUE;
}
if (!$this->mkdir(dirname($dir)) {
$this->error = 'mkdir-failed';
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)
{
if ($mode === NULL) {
$mode = ($this->is_dir($path)) ? 0755 : 0644;
}
return ftp_chmod($this->stream, $mode, $path);
}
private function is_dir($dir)
{
// 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)
{
$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($file, strrpos($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 error()
{
if (isset($this->error) && !empty($this->error)) {
if (isset($this->errors[$this->error])) {
return $this->errors[$this->error];
} else {
return $this->errors['unknown'];
}
}
}
public function close()
{
if (isset($this->stream) && $this->stream && is_resource($this->stream)) {
ftp_close($this->stream);
$this->stream = NULL;
}
}
}