<?php
class Ftp {
private $hostname = '';
private $username = '';
private $password = '';
private $port = 21;
private $passive = 1;
private $debug = TRUE;
private $conn_id = FALSE;
private $ssl = 0;
private $timeout = 0;
private $rootdir = '';
public function __construct($config = array()) {
if(count($config) > 0) {
$this->_init($config);
}
}
public function connect($config = array()) {
if(count($config) > 0) {
$this->_init($config);
}
$funcname = $this->ssl && function_exists('ftp_ssl_connect') ? 'ftp_ssl_connect' : 'ftp_connect';
if(!function_exists($funcname)) {
return FALSE;
}
if(FALSE === ($this->conn_id = @$funcname($this->hostname, $this->port))) {
if($this->debug === TRUE) {
$this->_error("ftp_unable_to_connect");
}
return FALSE;
}
if (!empty($this->timeout)) {
$this->set_option(FTP_TIMEOUT_SEC, $this->timeout);
}
if( ! $this->_login()) {
if($this->debug === TRUE) {
$this->_error("ftp_unable_to_login");
}
return FALSE;
}
if(!empty($this->passive)) {
ftp_pasv($this->conn_id, TRUE);
}
if (!empty($this->rootdir)) {
$this->chgdir($this->rootdir);
}
return TRUE;
}
public function chgdir($path = '', $supress_debug = FALSE) {
if($path == '' OR ! $this->_isconn()) {
return FALSE;
}
$result = @ftp_chdir($this->conn_id, $path);
if($result === FALSE) {
if($this->debug === TRUE AND $supress_debug == FALSE) {
$this->_error("ftp_unable_to_chgdir:dir[".$path."]");
}
return FALSE;
}
return TRUE;
}
public function mkdir($path = '', $permissions = NULL) {
if($path == '' OR ! $this->_isconn()) {
return FALSE;
}
$result = @ftp_mkdir($this->conn_id, $path);
if($result === FALSE) {
if($this->debug === TRUE) {
$this->_error("ftp_unable_to_mkdir:dir[".$path."]");
}
return FALSE;
}
if( ! is_null($permissions)) {
$this->chmod($path,(int)$permissions);
}
return TRUE;
}
public function mkdirs($path = '', $permissions = NULL) {
$targetpaths = explode('/', $path);
$dir = ''; $comma = '';
foreach($targetpaths as $pathitem) {
$dir .= $comma.$pathitem;
$comma = '/';
$return = $this->mkdir($dir);
}
return $return;
}
public function upload($localpath, $remotepath, $mode = 'auto', $permissions = NULL) {
if( ! $this->_isconn()) {
return FALSE;
}
if( ! file_exists($localpath)) {
if($this->debug === TRUE) {
$this->_error("ftp_no_source_file:".$localpath);
}
return FALSE;
}
if($mode == 'auto') {
$ext = $this->_getext($localpath);
$mode = $this->_settype($ext);
}
$mode = ($mode == 'ascii') ? FTP_ASCII : FTP_BINARY;
$old_dir = $this->ftp_pwd();
$dirname = dirname($remotepath);
$filename = basename($remotepath);
if(!$this->chgdir($dirname)) {
if($this->mkdirs($dirname)) {
if(!$this->chgdir($dirname)) {
$this->_error("ftp_error_change_dir");
}
} else {
$this->_error("ftp_error_make_dir");
$this->set_error(FTP_ERR_MKDIR);
}
}
if($fp = @fopen($localpath, 'rb')) {
$result = ftp_fput($this->conn_id, $filename, $fp, $mode);
@fclose($fp);
} else {
$this->_error("ftp_error_read_file");
}
if($result === FALSE) {
if($this->debug === TRUE) {
$this->_error("ftp_unable_to_upload:localpath[".$localpath."]/remotepath[".$remotepath."]");
}
return FALSE;
}
if( ! is_null($permissions)) {
$this->chmod($remotepath,(int)$permissions);
}
$this->chgdir($old_dir);
return TRUE;
}
public function download($remotepath, $localpath, $mode = 'auto') {
if( ! $this->_isconn()) {
return FALSE;
}
if($mode == 'auto') {
$ext = $this->_getext($remotepath);
$mode = $this->_settype($ext);
}
$mode = ($mode == 'ascii') ? FTP_ASCII : FTP_BINARY;
$result = @ftp_get($this->conn_id, $localpath, $remotepath, $mode);
if($result === FALSE) {
if($this->debug === TRUE) {
$this->_error("ftp_unable_to_download:localpath[".$localpath."]-remotepath[".$remotepath."]");
}
return FALSE;
}
return TRUE;
}
public function rename($oldname, $newname, $move = FALSE) {
if( ! $this->_isconn()) {
return FALSE;
}
$result = @ftp_rename($this->conn_id, $oldname, $newname);
if($result === FALSE) {
if($this->debug === TRUE) {
$msg = ($move == FALSE) ? "ftp_unable_to_rename" : "ftp_unable_to_move";
$this->_error($msg);
}
return FALSE;
}
return TRUE;
}
public function delete_file($file) {
if( ! $this->_isconn()) {
return FALSE;
}
$result = @ftp_delete($this->conn_id, $file);
if($result === FALSE) {
if($this->debug === TRUE) {
$this->_error("ftp_unable_to_delete_file:file[".$file."]");
}
return FALSE;
}
return TRUE;
}
public function delete_dir($path) {
if( ! $this->_isconn()) {
return FALSE;
}
$path = preg_replace("/(.+?)\/*$/", "\\1/", $path);
$filelist = $this->filelist($path);
if($filelist !== FALSE AND count($filelist) > 0) {
foreach($filelist as $item) {
if( ! @delete_file($item)) {
$this->delete_dir($item);
}
}
}
$result = @ftp_rmdir($this->conn_id, $path);
if($result === FALSE) {
if($this->debug === TRUE) {
$this->_error("ftp_unable_to_delete_dir:dir[".$path."]");
}
return FALSE;
}
return TRUE;
}
public function chmod($path, $perm) {
if( ! $this->_isconn()) {
return FALSE;
}
if( ! function_exists('ftp_chmod')) {
if($this->debug === TRUE) {
$this->_error("ftp_unable_to_chmod(function)");
}
return FALSE;
}
$result = @ftp_chmod($this->conn_id, $perm, $path);
if($result === FALSE) {
if($this->debug === TRUE) {
$this->_error("ftp_unable_to_chmod:path[".$path."]-chmod[".$perm."]");
}
return FALSE;
}
return TRUE;
}
public function filelist($path = '.') {
if( ! $this->_isconn()) {
return FALSE;
}
return ftp_nlist($this->conn_id, $path);
}
public function ftp_pwd() {
if( ! $this->_isconn()) {
return FALSE;
}
return @ftp_pwd($this->conn_id);
}
public function close() {
if( ! $this->_isconn()) {
return FALSE;
}
return @ftp_close($this->conn_id);
}
public function set_option($cmd, $value) {
if(function_exists('ftp_set_option')) {
return @ftp_set_option($this->conn_id, $cmd, $value);
}
}
private function _init($config = array()) {
foreach($config as $key => $val) {
if(isset($this->$key)) {
$this->$key = $val;
}
}
$this->hostname = preg_replace('|.+?://|','',$this->hostname);
}
private function _login() {
return @ftp_login($this->conn_id, $this->username, $this->password);
}
private function _isconn() {
if( ! is_resource($this->conn_id)) {
if($this->debug === TRUE) {
$this->_error("ftp_no_connection");
}
return FALSE;
}
return TRUE;
}
private function _getext($filename) {
if(FALSE === strpos($filename, '.')) {
return 'txt';
}
$extarr = explode('.', $filename);
return end($extarr);
}
private function _settype($ext) {
$text_type = array (
'txt',
'text',
'php',
'phps',
'php4',
'js',
'css',
'htm',
'html',
'phtml',
'shtml',
'log',
'xml'
);
return (in_array($ext, $text_type)) ? 'ascii' : 'binary';
}
private function _error($msg) { }
}