1
0
mirror of https://github.com/danog/tgseclib.git synced 2024-12-02 17:48:00 +01:00
tgseclib/phpseclib/Net/SCP.php

341 lines
9.0 KiB
PHP
Raw Normal View History

2013-04-27 23:03:35 +02:00
<?php
/**
* Pure-PHP implementation of SCP.
*
2015-04-02 12:57:52 +02:00
* PHP version 5
2013-04-27 23:03:35 +02:00
*
* The API for this library is modeled after the API from PHP's {@link http://php.net/book.ftp FTP extension}.
*
* Here's a short example of how to use this library:
* <code>
* <?php
2014-12-10 02:31:41 +01:00
* include 'vendor/autoload.php';
2013-04-27 23:03:35 +02:00
*
2014-12-10 02:31:41 +01:00
* $ssh = new \phpseclib\Net\SSH2('www.domain.tld');
2013-04-27 23:06:05 +02:00
* if (!$ssh->login('username', 'password')) {
2013-04-27 23:03:35 +02:00
* exit('bad login');
* }
2014-12-10 02:31:41 +01:00
* $scp = new \phpseclib\Net\SCP($ssh);
2015-09-14 17:30:31 +02:00
*
2013-04-27 23:03:35 +02:00
* $scp->put('abcd', str_repeat('x', 1024*1024));
* ?>
* </code>
*
* @category Net
2014-12-10 02:31:41 +01:00
* @package SCP
* @author Jim Wigginton <terrafrost@php.net>
* @copyright 2010 Jim Wigginton
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @link http://phpseclib.sourceforge.net
2013-04-27 23:03:35 +02:00
*/
2014-12-10 02:31:41 +01:00
namespace phpseclib\Net;
use phpseclib\Net\SSH1;
use phpseclib\Net\SSH2;
use phpseclib\Exception\FileNotFoundException;
2014-12-10 02:31:41 +01:00
2013-04-27 23:03:35 +02:00
/**
* Pure-PHP implementations of SCP.
*
2014-12-10 02:31:41 +01:00
* @package SCP
2013-04-27 23:03:35 +02:00
* @author Jim Wigginton <terrafrost@php.net>
* @access public
*/
2014-12-10 02:31:41 +01:00
class SCP
{
/**#@+
* @access public
2014-12-10 02:31:41 +01:00
* @see \phpseclib\Net\SCP::put()
*/
/**
* Reads data from a local file.
*/
const SOURCE_LOCAL_FILE = 1;
/**
* Reads data from a string.
2015-09-14 17:30:31 +02:00
*/
const SOURCE_STRING = 2;
/**#@-*/
/**#@+
* @access private
2014-12-10 02:31:41 +01:00
* @see \phpseclib\Net\SCP::_send()
* @see \phpseclib\Net\SCP::_receive()
*/
/**
* SSH1 is being used.
2015-09-14 17:30:31 +02:00
*/
const MODE_SSH1 = 1;
/**
* SSH2 is being used.
2015-09-14 17:30:31 +02:00
*/
const MODE_SSH2 = 2;
/**#@-*/
2013-04-27 23:03:35 +02:00
/**
* SSH Object
*
2015-09-02 01:44:55 +02:00
* @var object
2013-04-27 23:03:35 +02:00
* @access private
*/
var $ssh;
/**
* Packet Size
*
2015-09-02 01:44:55 +02:00
* @var int
2013-04-27 23:03:35 +02:00
* @access private
*/
var $packet_size;
/**
* Mode
*
2015-09-02 01:44:55 +02:00
* @var int
2013-04-27 23:03:35 +02:00
* @access private
*/
var $mode;
/**
* Default Constructor.
*
* Connects to an SSH server
*
2015-09-02 01:44:55 +02:00
* @param string $host
* @param int $port
* @param int $timeout
2014-12-10 02:31:41 +01:00
* @return \phpseclib\Net\SCP
2013-04-27 23:03:35 +02:00
* @access public
*/
function __construct($ssh)
2013-04-27 23:03:35 +02:00
{
if ($ssh instanceof SSH2) {
$this->mode = self::MODE_SSH2;
} elseif ($ssh instanceof SSH1) {
$this->packet_size = 50000;
$this->mode = self::MODE_SSH1;
} else {
2013-04-27 23:03:35 +02:00
return;
}
$this->ssh = $ssh;
}
/**
* Uploads a file to the SCP server.
*
2014-12-10 02:31:41 +01:00
* By default, \phpseclib\Net\SCP::put() does not read from the local filesystem. $data is dumped directly into $remote_file.
* So, for example, if you set $data to 'filename.ext' and then do \phpseclib\Net\SCP::get(), you will get a file, twelve bytes
2013-04-27 23:03:35 +02:00
* long, containing 'filename.ext' as its contents.
*
* Setting $mode to self::SOURCE_LOCAL_FILE will change the above behavior. With self::SOURCE_LOCAL_FILE, $remote_file will
2013-04-27 23:03:35 +02:00
* 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.
*
* 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.
*
2015-09-02 01:44:55 +02:00
* @param string $remote_file
* @param string $data
* @param int $mode
* @param callable $callback
* @throws \phpseclib\Exception\FileNotFoundException if you're uploading via a file and the file doesn't exist
2015-09-02 01:44:55 +02:00
* @return bool
2013-04-27 23:03:35 +02:00
* @access public
*/
function put($remote_file, $data, $mode = self::SOURCE_STRING, $callback = null)
2013-04-27 23:03:35 +02:00
{
if (!isset($this->ssh)) {
return false;
}
if (!$this->ssh->exec('scp -t ' . escapeshellarg($remote_file), false)) { // -t = to
return false;
}
2013-04-27 23:03:35 +02:00
$temp = $this->_receive();
if ($temp !== chr(0)) {
return false;
}
if ($this->mode == self::MODE_SSH2) {
2014-12-10 02:31:41 +01:00
$this->packet_size = $this->ssh->packet_size_client_to_server[SSH2::CHANNEL_EXEC] - 4;
2013-04-27 23:03:35 +02:00
}
$remote_file = basename($remote_file);
if ($mode == self::SOURCE_STRING) {
$size = strlen($data);
2013-04-27 23:03:35 +02:00
} else {
if (!is_file($data)) {
throw new FileNotFoundException("$data is not a valid file");
2013-04-27 23:03:35 +02:00
}
2013-04-27 23:03:35 +02:00
$fp = @fopen($data, 'rb');
if (!$fp) {
return false;
}
$size = filesize($data);
}
$this->_send('C0644 ' . $size . ' ' . $remote_file . "\n");
$temp = $this->_receive();
if ($temp !== chr(0)) {
return false;
}
$sent = 0;
while ($sent < $size) {
$temp = $mode & self::SOURCE_STRING ? substr($data, $sent, $this->packet_size) : fread($fp, $this->packet_size);
$this->_send($temp);
$sent+= strlen($temp);
if (is_callable($callback)) {
2014-03-05 18:38:33 +01:00
call_user_func($callback, $sent);
}
2013-04-27 23:03:35 +02:00
}
$this->_close();
if ($mode != self::SOURCE_STRING) {
fclose($fp);
}
return true;
2013-04-27 23:03:35 +02:00
}
/**
* Downloads a file from the SCP server.
*
* Returns a string containing the contents of $remote_file if $local_file is left undefined or a boolean false if
* the operation was unsuccessful. If $local_file is defined, returns true or false depending on the success of the
* operation
*
2015-09-02 01:44:55 +02:00
* @param string $remote_file
* @param string $local_file
* @return mixed
2013-04-27 23:03:35 +02:00
* @access public
*/
function get($remote_file, $local_file = false)
{
if (!isset($this->ssh)) {
return false;
}
if (!$this->ssh->exec('scp -f ' . escapeshellarg($remote_file), false)) { // -f = from
return false;
}
2013-04-27 23:03:35 +02:00
$this->_send("\0");
if (!preg_match('#(?<perms>[^ ]+) (?<size>\d+) (?<name>.+)#', rtrim($this->_receive()), $info)) {
return false;
}
$this->_send("\0");
$size = 0;
if ($local_file !== false) {
$fp = @fopen($local_file, 'wb');
if (!$fp) {
return false;
}
}
$content = '';
while ($size < $info['size']) {
$data = $this->_receive();
// SCP usually seems to split stuff out into 16k chunks
$size+= strlen($data);
if ($local_file === false) {
$content.= $data;
} else {
fputs($fp, $data);
}
}
$this->_close();
if ($local_file !== false) {
fclose($fp);
return true;
}
return $content;
}
/**
* Sends a packet to an SSH server
*
2015-09-02 01:44:55 +02:00
* @param string $data
2013-04-27 23:03:35 +02:00
* @access private
*/
function _send($data)
{
switch ($this->mode) {
case self::MODE_SSH2:
2014-12-10 02:31:41 +01:00
$this->ssh->_send_channel_packet(SSH2::CHANNEL_EXEC, $data);
2013-04-27 23:03:35 +02:00
break;
case self::MODE_SSH1:
2013-04-27 23:03:35 +02:00
$data = pack('CNa*', NET_SSH1_CMSG_STDIN_DATA, strlen($data), $data);
$this->ssh->_send_binary_packet($data);
2015-07-15 03:52:31 +02:00
}
2013-04-27 23:03:35 +02:00
}
/**
* Receives a packet from an SSH server
*
2015-09-02 01:44:55 +02:00
* @return string
* @throws \UnexpectedValueException on receipt of an unexpected packet
2013-04-27 23:03:35 +02:00
* @access private
*/
function _receive()
{
switch ($this->mode) {
case self::MODE_SSH2:
2014-12-10 02:31:41 +01:00
return $this->ssh->_get_channel_packet(SSH2::CHANNEL_EXEC, true);
case self::MODE_SSH1:
2013-04-27 23:03:35 +02:00
if (!$this->ssh->bitmap) {
return false;
}
while (true) {
$response = $this->ssh->_get_binary_packet();
2014-12-10 02:31:41 +01:00
switch ($response[SSH1::RESPONSE_TYPE]) {
2013-04-27 23:03:35 +02:00
case NET_SSH1_SMSG_STDOUT_DATA:
2014-12-10 02:31:41 +01:00
extract(unpack('Nlength', $response[SSH1::RESPONSE_DATA]));
return $this->ssh->_string_shift($response[SSH1::RESPONSE_DATA], $length);
2013-04-27 23:03:35 +02:00
case NET_SSH1_SMSG_STDERR_DATA:
break;
case NET_SSH1_SMSG_EXITSTATUS:
$this->ssh->_send_binary_packet(chr(NET_SSH1_CMSG_EXIT_CONFIRMATION));
fclose($this->ssh->fsock);
$this->ssh->bitmap = 0;
return false;
default:
throw new \UnexpectedValueException('Unknown packet received');
2013-04-27 23:03:35 +02:00
}
}
2015-07-15 03:52:31 +02:00
}
2013-04-27 23:03:35 +02:00
}
/**
* Closes the connection to an SSH server
*
* @access private
*/
function _close()
{
switch ($this->mode) {
case self::MODE_SSH2:
2014-12-10 02:31:41 +01:00
$this->ssh->_close_channel(SSH2::CHANNEL_EXEC, true);
2013-04-27 23:03:35 +02:00
break;
case self::MODE_SSH1:
2013-04-27 23:03:35 +02:00
$this->ssh->disconnect();
2015-07-15 03:52:31 +02:00
}
2013-04-27 23:03:35 +02:00
}
}