OK, so accessing other servers from your own via either ftp or sftp… I have written a small class to handle either.. It is obviously new and could easily be improved so thought i would throw it out here and see what other people think (stackoverflow gets a hell of a lot of views so hopefully this could help someone else), and how they can improve on it… so i guess the question is… how can this be improved?
class ftp_sftp{
//determine, if ssh, to use phpseclib or php's inbuilt ssh_sftp 'libssh'
public $ssh_type = 'phpseclib';
//set ths path to the directory containing the entire phpseclib files
public $phpseclib_path = 'scripts/phpseclib0.3.0';
//private vars generated by this class
public $host;
public $username;
public $password;
public $connection_type;
public $port_number;
public $connection = false;
//contruct method which will attempt to set the connection details and automatically attempt to establisha connection to the server
public function __construct( $host, $username, $password, $connection_type, $port_number = false ){
//add the webroot to the beginning of the $this->phpseclib_path (this is bespoke to my own configuration)
$this->phpseclib_path = WEBROOT_PRIVATE.$this->phpseclib_path;
//setting the classes vars
$this->host = $host;
$this->username = $username;
$this->password = $password;
$this->connection_type = $connection_type;
//set the port number to defaults based on connection type if none passed
if( $port_number === false ){
if( $connection_type == 'ftp' ){
$port_number = 21;
} else {
$port_number = 22;
}
}
$this->port_number = $port_number;
//now set the server connection into this classes connection var
$this->connection = $this->connect();
}
//tests the details passed and tries to establish a connection, returns false on fail.
function connect(){
br($this->connection_type);
switch( $this->connection_type )
{
case 'ftp':
$connection = ftp_connect($this->host);
$login = ftp_login($connection, $this->username, $this->password);
//if no connection was possible return false and leave $this-connection as false
if(!$connection || !$login){
return false;
} else {
// enabling passive mode
ftp_pasv( $connection, true );
return $connection;
}
break;
case 'sftp':
//decide which ssh type to use
switch( $this->ssh_type ){
case 'phpseclib':
//inlcude the phpseclib path in the include array and include the ssh2 class
set_include_path($this->phpseclib_path );
if(!include('Net/SSH2.php')){
echo 'Sorry failed to load SSH2 class';
br();
}
if(!include('Net/SFTP.php')){
echo 'Sorry failed to load SFTP class';
br();
}
$connection = new Net_SFTP($this->host, $this->port_number);
$login = $connection->login($this->username, $this->password);
break;
case 'libssh2':
$connection = ssh2_connect($this->host, $this->port_number);
$login = ssh2_auth_password($connection, 'username', 'secret');
break;
default:
echo 'No ssh method defined, please define one in: $ftp_sftp->ssh_type';
exit();
break;
}
//if no connection was possible return false and leave $this-connection as false
if (!$connection || !$login) {
return false;
} else {
return $connection;
}
break;
default: echo 'No connection type set cannot choose a method to connect';
break;
}
}
//acces the phpseclib errors
public function errors(){
if($this->connection_type == 'sftp' && $this->ssh_type == 'phpseclib'){
print_r($this->connection->getErrors());
} else {
echo 'no error logs available';
}
}
//function used by this class to check certain values are set
public function connection_check(){
if( $this->connection === false){
echo 'Sorry there seems to be a connection problem please try again';
br();
}
if( $this->connection_type === false){
echo 'Sorry there seems to be a no connection type set';
}
if( $this->connection === false || $this->connection_type === false ){
exit();
}
}
//transfers a file to the connected server
public function put($targetLocationToSendTo, $existingLocationToSendFrom){
//check the connection
$this->connection_check();
switch( $this->connection_type )
{
case 'ftp':
//ftp_put the file across
$put = ftp_put( $this->connection, $targetLocationToSendTo, $existingLocationToSendFrom, FTP_BINARY);
break;
case 'sftp':
//decide which ssh type to use
switch( $this->ssh_type ){
case 'phpseclib':
$put = $this->connection->put( $targetLocationToSendTo, $existingLocationToSendFrom, NET_SFTP_LOCAL_FILE );
break;
case 'libssh2':
$put = ssh2_scp_send($this->connection, $targetLocationToSendTo, $existingLocationToSendFrom, 0755);
break;
}
break;
}
return $put;
}
//list the contents of a remote directory
public function dir_list( $dirToList ){
//check the connection
$this->connection_check();
//run appropriate list
switch( $this->connection_type )
{
case 'ftp':
$list = $this->connection = ftp_nlist($this->connection, $dirToList);
break;
case 'sftp':
//decide which ssh type to use
switch( $this->ssh_type ){
case 'phpseclib':
$list = $this->connection->nlist( $dirToList );
break;
case 'libssh2':
echo 'Sorry there is no support for nlist with libssh2, however this link has a possible answer: http://randomdrake.com/2012/02/08/listing-and-downloading-files-over-sftp-with-php-and-ssh2/';
break;
}
break;
}
return $list;
}
//get the timestamp of the file on another server
public function remote_filemtime( $pathToFile ){
//check the connection
$this->connection_check();
//run appropriate list
switch( $this->connection_type )
{
case 'ftp':
$timeStamp = ftp_mdtm($this->connection, $pathToFile);
break;
case 'sftp':
//decide which ssh type to use
switch( $this->ssh_type ){
case 'phpseclib':
$statinfo = $this->connection->stat( $pathToFile );
break;
case 'libssh2':
$statinfo = ssh2_sftp_stat($this->connection, $pathToFile);
break;
}
if($statinfo['mtime']){
$timeStamp = $statinfo['mtime'];
} else {
$timeStamp = false;
}
break;
}
return $timeStamp;
}
//make a directory on the remote server
public function make_dir( $dirToMake ){
//check the connection
$this->connection_check();
//run appropriate list
switch( $this->connection_type )
{
case 'ftp':
$dir_made = ftp_mkdir($this->connection, $dirToMake);
break;
case 'sftp':
//decide which ssh type to use
switch( $this->ssh_type ){
case 'phpseclib':
$statinfo = $this->connection->mkdir( $dirToMake );
break;
case 'libssh2':
$statinfo = ssh2_sftp_mkdir($this->connection, $dirToMake, 0755);
break;
}
break;
}
return $dir_made;
}
//change directory
public function change_dir( $dirToMoveTo ){
//check the connection
$this->connection_check();
//run appropriate list
switch( $this->connection_type )
{
case 'ftp': $chdir = ftp_chdir($this->connection, $dirToMoveTo );
break;
case 'sftp':
//decide which ssh type to use
switch( $this->ssh_type ){
case 'phpseclib':
$chdir = $this->connection->chdir( $dirToMoveTo );
break;
case 'libssh2':
echo 'Sorry this feature does exist yet for when using libssh2 with the ftp_sftp class';
exit();
break;
}
break;
}
return $chdir;
}
//curent directory we are looking in
public function pwd(){
//check the connection
$this->connection_check();
//run appropriate list
switch( $this->connection_type )
{
case 'ftp': $pwd = ftp_pwd($this->connection);
break;
case 'sftp':
//decide which ssh type to use
switch( $this->ssh_type ){
case 'phpseclib':
$pwd = $this->connection->pwd();
break;
case 'libssh2':
echo 'Sorry this feature does exist yet for when using libssh2';
exit();
break;
}
break;
}
return $pwd;
}
//delete file
public function delete_file($fileToDelete){
//check the connection
$this->connection_check();
//run appropriate list
switch( $this->connection_type )
{
case 'ftp': $unlink = ftp_delete($this->connection, $fileToDelete);
break;
case 'sftp':
//decide which ssh type to use
switch( $this->ssh_type ){
case 'phpseclib':
$unlink = $this->connection->delete( $fileToDelete );
break;
case 'libssh2':
$unlink = ssh2_sftp_unlink($this->connection, $fileToDelete);
break;
}
break;
}
return $unlink;
} }//end of class
Using the class:
$ftp_sftp = new ftp_sftp( '92.21.627.163', 'ftpuser', 'yourpassword', '', 'ftp', '21' );
echo $ftp_sftp->pwd();
I am having a little trouble getting the phpseclib to connect on my win7 machine using easyPHP and have started a Q.. if anyone has any ideas i would be very grateful…
Cannot get phpseclib to connect – error 10060
The main problem with this code is it scales vertically. Suppose you decide to add another implementation, a local filesystem, introducing a third potential value for
$connection_typeof ‘file’ say. Now you have to go through the code addingcase 'file':conditions everywhere, makingftp_sftpgrow out of control. You’ve got the same problem again inside of the ‘sftp’ branch of code right now, dealing with$ssh_type.Original code
Becomes
Bear in mind this is for every switch statement in the code that switches on
$connection_type. Imagine how many lines of code we’d add to theftp_sftpclass to incorporate each additional behavior…In fact, it looks like you actually have 3 strategies right now, ftp, sftp & ssh.
What you want to do here is set the code up to expand horizontally rather than vertically and the way to do that is via the Strategy design pattern.
Essentially what you’re doing is pulling out the common interface in your code, then creating separate implementations for FTP & SFTP. There are lots of different ways to go on the actual implementation on this, so tweak to your heart’s content!
A common interface
This is the definition of what any ‘ftp class’ must be able to do.
A factory
Now there is a single switch statement throughout the code that checks
$connection_type. You might decide to handle thedefaultcase differently.A base class
Not strictly necessary, but very helpful. Also, we’re sneaking in another design pattern here, called Template Method.
BaseFtp::pwdis a template method, it controls the overall pwd algorithm, but delegates to concrete children for a portion of it.Concrete FTP implementation
Now we have a succinct FTP class that only does FTP operations, it knows nothing about SFTP.
Concrete SFTP class
Now we have a standalone SFTP class, however notice it switches on
$ssh_typejust as the originalftp_sftpclass did for$connection_type. Once you get a handle on Strategy Pattern, and have theFtp&Sftpclasses finished, you can go back in and implement Strategy Pattern within theSftpimplementation so that you have 2 classesPhpseclib&Libssh2, for instance.Putting the new code to use
With the new system in place its 10X easier to see what’s what, each class focuses on it’s specific domain and the code grows horizontally. What does that mean? Remember above, when we considered adding a third
$connection_type‘file’ in the original paradigm? In the new arrangement we update one switch statement, the one in the factory:And then of course you add the new concrete implementation
Since the new
Fileclass can go in it’s own space, we’re not expanding one central class, namely the originalftp_sftpclass.In order to use the new system, you hit up the factory for an instance and go from there
Either of these instances support all the
PhpFtpfunctions because they implement the interface! This also gives you the ability to write polymorphic code as well. Consider a function that type-hints against the interface