Merge branch '2.0'

* 2.0:
  callbacks - two PRs conflict sorted out. First one: e9b698fd65, fba6894474, 1cd286642e Second one: 16430d4d2e, 0cc6125f87
  documentation fix
  support for callback function for SFTP::put function - in order to pipe data directly to remote server without putting it into file or keeping in memory. This can be useful particularly for dumping big databases directly to remote server.
This commit is contained in:
Andreas Fischer 2015-05-03 01:11:09 +02:00
commit be8d7d3467

View File

@ -1752,12 +1752,12 @@ class SFTP extends SSH2
* @param optional Integer $mode * @param optional Integer $mode
* @param optional Integer $start * @param optional Integer $start
* @param optional Integer $local_start * @param optional Integer $local_start
* @param optional callable|null $callback * @param optional callable|null $progressCallback
* @return Boolean * @return Boolean
* @access public * @access public
* @internal ASCII mode for SFTPv4/5/6 can be supported by adding a new function - \phpseclib\Net\SFTP::setMode(). * @internal ASCII mode for SFTPv4/5/6 can be supported by adding a new function - \phpseclib\Net\SFTP::setMode().
*/ */
function put($remote_file, $data, $mode = self::SOURCE_STRING, $start = -1, $local_start = -1, $callback = null) function put($remote_file, $data, $mode = self::SOURCE_STRING, $start = -1, $local_start = -1, $progressCallback = null)
{ {
if (!($this->bitmap & SSH2::MASK_LOGIN)) { if (!($this->bitmap & SSH2::MASK_LOGIN)) {
return false; return false;
@ -1805,13 +1805,13 @@ class SFTP extends 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
$callback = false; $dataCallback = false;
switch (true) { switch (true) {
case $mode & self::SOURCE_CALLBACK; case $mode & self::SOURCE_CALLBACK;
if (!is_callable($data)) { if (!is_callable($data)) {
user_error("\$data should be is_callable() if you specify SOURCE_CALLBACK flag"); user_error("\$data should be is_callable() if you specify SOURCE_CALLBACK flag");
} }
$callback = $data; $dataCallback = $data;
// do nothing // do nothing
break; break;
case is_resource($data): case is_resource($data):
@ -1840,7 +1840,7 @@ class SFTP extends SSH2
} else { } else {
fseek($fp, $offset); fseek($fp, $offset);
} }
} elseif ($callback) { } elseif ($dataCallback) {
$size = 0; $size = 0;
} else { } else {
$size = strlen($data); $size = strlen($data);
@ -1853,9 +1853,9 @@ class SFTP extends SSH2
// make the SFTP packet be exactly 4096 bytes by including the bytes in the NET_SFTP_WRITE packets "header" // make the SFTP packet be exactly 4096 bytes by including the bytes in the NET_SFTP_WRITE packets "header"
$sftp_packet_size-= strlen($handle) + 25; $sftp_packet_size-= strlen($handle) + 25;
$i = 0; $i = 0;
while ($callback || $sent < $size) { while ($dataCallback || $sent < $size) {
if ($callback) { if ($dataCallback) {
$temp = call_user_func($callback, $sftp_packet_size); $temp = call_user_func($dataCallback, $sftp_packet_size);
if (is_null($temp)) { if (is_null($temp)) {
break; break;
} }
@ -1871,8 +1871,8 @@ class SFTP extends SSH2
return false; return false;
} }
$sent+= strlen($temp); $sent+= strlen($temp);
if (is_callable($callback)) { if (is_callable($progressCallback)) {
call_user_func($callback, $sent); call_user_func($progressCallback, $sent);
} }
$i++; $i++;