mirror of
https://github.com/danog/tgseclib.git
synced 2025-01-22 05:51:20 +01:00
SFTP: make it so files can be downloaded into resources or upload
from resources
This commit is contained in:
parent
0d7e9199ef
commit
e48ee12940
@ -1757,6 +1757,8 @@ class Net_SFTP extends Net_SSH2
|
|||||||
* contain as many bytes as filename.ext does on your local filesystem. If your filename.ext is 1MB then that is how
|
* contain as many bytes as filename.ext does on your local filesystem. If your filename.ext is 1MB then that is how
|
||||||
* large $remote_file will be, as well.
|
* large $remote_file will be, as well.
|
||||||
*
|
*
|
||||||
|
* If $data is a resource then it'll be used a resource instead.
|
||||||
|
*
|
||||||
* Currently, only binary mode is supported. As such, if the line endings need to be adjusted, you will need to take
|
* Currently, only binary mode is supported. As such, if the line endings need to be adjusted, you will need to take
|
||||||
* care of that, yourself.
|
* care of that, yourself.
|
||||||
*
|
*
|
||||||
@ -1834,16 +1836,25 @@ class Net_SFTP extends Net_SSH2
|
|||||||
}
|
}
|
||||||
|
|
||||||
// http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-8.2.3
|
// http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-8.2.3
|
||||||
if ($mode & NET_SFTP_LOCAL_FILE) {
|
switch (true) {
|
||||||
if (!is_file($data)) {
|
case is_resource($data):
|
||||||
user_error("$data is not a valid file");
|
$mode = $mode & ~NET_SFTP_LOCAL_FILE;
|
||||||
return false;
|
$fp = $data;
|
||||||
}
|
break;
|
||||||
$fp = @fopen($data, 'rb');
|
case $mode & NET_SFTP_LOCAL_FILE:
|
||||||
if (!$fp) {
|
if (!is_file($data)) {
|
||||||
return false;
|
user_error("$data is not a valid file");
|
||||||
}
|
return false;
|
||||||
$size = filesize($data);
|
}
|
||||||
|
$fp = @fopen($data, 'rb');
|
||||||
|
if (!$fp) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isset($fp)) {
|
||||||
|
$stat = fstat($fp);
|
||||||
|
$size = $stat['size'];
|
||||||
|
|
||||||
if ($local_start >= 0) {
|
if ($local_start >= 0) {
|
||||||
fseek($fp, $local_start);
|
fseek($fp, $local_start);
|
||||||
@ -2005,13 +2016,20 @@ class Net_SFTP extends Net_SSH2
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($local_file !== false) {
|
if (is_resource($local_file)) {
|
||||||
$fp = fopen($local_file, 'wb');
|
$fp = $local_file;
|
||||||
if (!$fp) {
|
$stat = fstat($fp);
|
||||||
return false;
|
$res_offset = $stat['size'];
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
$content = '';
|
$res_offset = 0;
|
||||||
|
if ($local_file !== false) {
|
||||||
|
$fp = fopen($local_file, 'wb');
|
||||||
|
if (!$fp) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$content = '';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$start = $offset;
|
$start = $offset;
|
||||||
@ -2019,7 +2037,7 @@ class Net_SFTP extends Net_SSH2
|
|||||||
while (true) {
|
while (true) {
|
||||||
$packet = pack('Na*N3', strlen($handle), $handle, $offset / 4294967296, $offset, $size);
|
$packet = pack('Na*N3', strlen($handle), $handle, $offset / 4294967296, $offset, $size);
|
||||||
if (!$this->_send_sftp_packet(NET_SFTP_READ, $packet)) {
|
if (!$this->_send_sftp_packet(NET_SFTP_READ, $packet)) {
|
||||||
if ($local_file !== false) {
|
if ($local_file !== false && !is_resource($local_file)) {
|
||||||
fclose($fp);
|
fclose($fp);
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
@ -2042,7 +2060,7 @@ class Net_SFTP extends Net_SSH2
|
|||||||
break 2;
|
break 2;
|
||||||
default:
|
default:
|
||||||
user_error('Expected SSH_FXP_DATA or SSH_FXP_STATUS');
|
user_error('Expected SSH_FXP_DATA or SSH_FXP_STATUS');
|
||||||
if ($local_file !== false) {
|
if ($local_file !== false && !is_resource($local_file)) {
|
||||||
fclose($fp);
|
fclose($fp);
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
@ -2057,11 +2075,11 @@ class Net_SFTP extends Net_SSH2
|
|||||||
if ($local_file === false) {
|
if ($local_file === false) {
|
||||||
$content = substr($content, 0, $length);
|
$content = substr($content, 0, $length);
|
||||||
} else {
|
} else {
|
||||||
ftruncate($fp, $length);
|
ftruncate($fp, $length + $res_offset);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($local_file !== false) {
|
if ($local_file !== false && !is_resource($local_file)) {
|
||||||
fclose($fp);
|
fclose($fp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -319,6 +319,26 @@ class Functional_Net_SFTPUserStoryTest extends PhpseclibFunctionalTestCase
|
|||||||
/**
|
/**
|
||||||
* @depends testSortOrder
|
* @depends testSortOrder
|
||||||
*/
|
*/
|
||||||
|
public function testResourceXfer($sftp)
|
||||||
|
{
|
||||||
|
$fp = fopen('res.txt', 'w+');
|
||||||
|
$sftp->get('file1.txt', $fp);
|
||||||
|
rewind($fp);
|
||||||
|
$sftp->put('file4.txt', $fp);
|
||||||
|
fclose($fp);
|
||||||
|
|
||||||
|
$this->assertSame(
|
||||||
|
self::$exampleData,
|
||||||
|
$sftp->get('file4.txt'),
|
||||||
|
'Failed asserting that a file downloaded into a resource and reuploaded from a resource has the correct data'
|
||||||
|
);
|
||||||
|
|
||||||
|
return $sftp;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @depends testResourceXfer
|
||||||
|
*/
|
||||||
public function testSymlink($sftp)
|
public function testSymlink($sftp)
|
||||||
{
|
{
|
||||||
$this->assertTrue(
|
$this->assertTrue(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user