- added destructor for FroxlorSshTransport
- added a fix for close() operation to prevent missing file writing on remote disk - added a switch for scp/sftp to sendFile
This commit is contained in:
@@ -20,7 +20,7 @@
|
|||||||
* @author Froxlor Team <team@froxlor.org>
|
* @author Froxlor Team <team@froxlor.org>
|
||||||
* @copyright 2010 the authors
|
* @copyright 2010 the authors
|
||||||
* @license GPLv2 http://files.froxlor.org/misc/COPYING.txt
|
* @license GPLv2 http://files.froxlor.org/misc/COPYING.txt
|
||||||
* @version SVN: $Id: class.FroxlorModule.php 1167 2010-06-22 11:46:34Z d00p $
|
* @version SVN: $Id$
|
||||||
* @link http://www.froxlor.org/
|
* @link http://www.froxlor.org/
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -87,6 +87,15 @@ class FroxlorSshTransport
|
|||||||
$this->_shell = $this->_getShell();
|
$this->_shell = $this->_getShell();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Destructor.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public function __destruct()
|
||||||
|
{
|
||||||
|
$this->close();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor for publickey auth
|
* Constructor for publickey auth
|
||||||
*
|
*
|
||||||
@@ -156,6 +165,8 @@ class FroxlorSshTransport
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Sends a file to the remote server.
|
* Sends a file to the remote server.
|
||||||
|
* The function checks if fopen() is enabled to use a faster way
|
||||||
|
* to send the file. Otherwise ssh2_scp_send() is used.
|
||||||
*
|
*
|
||||||
* @param string $localFile path to the local file
|
* @param string $localFile path to the local file
|
||||||
* @param string $remoteFile remote file path
|
* @param string $remoteFile remote file path
|
||||||
@@ -170,12 +181,42 @@ class FroxlorSshTransport
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// send file
|
// send file with scp if fopen() is disabled
|
||||||
if (ssh2_scp_send($this->_connection, $localFile, $remoteFile, $chmod)) {
|
if (ssh2_scp_send($this->_connection, $localFile, $remoteFile, $chmod) && !function_exists("fopen")) {
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// open a sftp cnnection
|
||||||
|
$sftp = ssh2_sftp($this->_connection);
|
||||||
|
|
||||||
|
// connect to it
|
||||||
|
$sftpStream = @fopen('ssh2.sftp://'.$sftp.$remoteFile, 'w');
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (!$sftpStream) {
|
||||||
|
throw new Exception("Could not open remote file: $remoteFile");
|
||||||
|
}
|
||||||
|
|
||||||
|
$data_to_send = @file_get_contents($localFile);
|
||||||
|
|
||||||
|
if ($data_to_send === false) {
|
||||||
|
throw new Exception("Could not open local file: $localFile.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (@fwrite($sftpStream, $data_to_send) === false) {
|
||||||
|
throw new Exception("Could not send data from file: $localFile.");
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose($sftpStream);
|
||||||
|
return true;
|
||||||
|
|
||||||
|
} catch (Exception $e) {
|
||||||
|
// TODO log to error log?
|
||||||
|
fclose($sftpStream);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -203,6 +244,10 @@ class FroxlorSshTransport
|
|||||||
*/
|
*/
|
||||||
public function close()
|
public function close()
|
||||||
{
|
{
|
||||||
|
// close the session and force flushing file content to disk
|
||||||
|
ssh2_exec($this->_connection, 'exit');
|
||||||
|
|
||||||
|
// set null values
|
||||||
$this->_shell = null;
|
$this->_shell = null;
|
||||||
$this->_settings = null;
|
$this->_settings = null;
|
||||||
$this->_connection = null;
|
$this->_connection = null;
|
||||||
|
|||||||
Reference in New Issue
Block a user