1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
|
<?php
/**
* Pure-PHP implementation of SCP.
*
* PHP version 5
*
* 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
* include 'vendor/autoload.php';
*
* $scp = new \phpseclib3\Net\SCP('www.domain.tld');
* if (!$scp->login('username', 'password')) {
* exit('Login Failed');
* }
*
* echo $scp->exec('pwd') . "\r\n";
* $scp->put('filename.ext', 'hello, world!');
* echo $scp->exec('ls -latr');
* ?>
* </code>
*
* @author Jim Wigginton <terrafrost@php.net>
* @copyright 2009 Jim Wigginton
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @link http://phpseclib.sourceforge.net
*/
namespace phpseclib3\Net;
use phpseclib3\Common\Functions\Strings;
use phpseclib3\Exception\FileNotFoundException;
/**
* Pure-PHP implementations of SCP.
*
* @author Jim Wigginton <terrafrost@php.net>
*/
class SCP extends SSH2
{
/**
* Reads data from a local file.
*
* @see \phpseclib3\Net\SCP::put()
*/
const SOURCE_LOCAL_FILE = 1;
/**
* Reads data from a string.
*
* @see \phpseclib3\Net\SCP::put()
*/
// this value isn't really used anymore but i'm keeping it reserved for historical reasons
const SOURCE_STRING = 2;
/**
* SCP.php doesn't support SOURCE_CALLBACK because, with that one, we don't know the size, in advance
*/
//const SOURCE_CALLBACK = 16;
/**
* Error information
*
* @see self::getSCPErrors()
* @see self::getLastSCPError()
* @var array
*/
private $scp_errors = [];
/**
* Uploads a file to the SCP server.
*
* 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
* 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
* 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.
*
* @param string $remote_file
* @param string $data
* @param int $mode
* @param callable $callback
* @return bool
* @access public
*/
public function put($remote_file, $data, $mode = self::SOURCE_STRING, $callback = null)
{
if (!($this->bitmap & self::MASK_LOGIN)) {
return false;
}
if (empty($remote_file)) {
// remote file cannot be blank
return false;
}
if (!$this->exec('scp -t ' . escapeshellarg($remote_file), false)) { // -t = to
return false;
}
$temp = $this->get_channel_packet(self::CHANNEL_EXEC, true);
if ($temp !== chr(0)) {
$this->close_channel(self::CHANNEL_EXEC, true);
return false;
}
$packet_size = $this->packet_size_client_to_server[self::CHANNEL_EXEC] - 4;
$remote_file = basename($remote_file);
$dataCallback = false;
switch (true) {
case is_resource($data):
$mode = $mode & ~self::SOURCE_LOCAL_FILE;
$info = stream_get_meta_data($data);
if (isset($info['wrapper_type']) && $info['wrapper_type'] == 'PHP' && $info['stream_type'] == 'Input') {
$fp = fopen('php://memory', 'w+');
stream_copy_to_stream($data, $fp);
rewind($fp);
} else {
$fp = $data;
}
break;
case $mode & self::SOURCE_LOCAL_FILE:
if (!is_file($data)) {
throw new FileNotFoundException("$data is not a valid file");
}
$fp = @fopen($data, 'rb');
if (!$fp) {
$this->close_channel(self::CHANNEL_EXEC, true);
return false;
}
}
if (isset($fp)) {
$stat = fstat($fp);
$size = !empty($stat) ? $stat['size'] : 0;
} else {
$size = strlen($data);
}
$sent = 0;
$size = $size < 0 ? ($size & 0x7FFFFFFF) + 0x80000000 : $size;
$temp = 'C0644 ' . $size . ' ' . $remote_file . "\n";
$this->send_channel_packet(self::CHANNEL_EXEC, $temp);
$temp = $this->get_channel_packet(self::CHANNEL_EXEC, true);
if ($temp !== chr(0)) {
$this->close_channel(self::CHANNEL_EXEC, true);
return false;
}
$sent = 0;
while ($sent < $size) {
$temp = $mode & self::SOURCE_STRING ? substr($data, $sent, $packet_size) : fread($fp, $packet_size);
$this->send_channel_packet(self::CHANNEL_EXEC, $temp);
$sent += strlen($temp);
if (is_callable($callback)) {
call_user_func($callback, $sent);
}
}
$this->close_channel(self::CHANNEL_EXEC, true);
if ($mode != self::SOURCE_STRING) {
fclose($fp);
}
return true;
}
/**
* 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
*
* @param string $remote_file
* @param string $local_file
* @return mixed
* @access public
*/
public function get($remote_file, $local_file = null, $progressCallback = null)
{
if (!($this->bitmap & self::MASK_LOGIN)) {
return false;
}
if (!$this->exec('scp -f ' . escapeshellarg($remote_file), false)) { // -f = from
return false;
}
$this->send_channel_packet(self::CHANNEL_EXEC, chr(0));
$info = $this->get_channel_packet(self::CHANNEL_EXEC, true);
// per https://goteleport.com/blog/scp-familiar-simple-insecure-slow/ non-zero responses mean there are errors
if ($info[0] === chr(1) || $info[0] == chr(2)) {
$type = $info[0] === chr(1) ? 'warning' : 'error';
$this->scp_errors[] = "$type: " . substr($info, 1);
$this->close_channel(self::CHANNEL_EXEC, true);
return false;
}
$this->send_channel_packet(self::CHANNEL_EXEC, chr(0));
if (!preg_match('#(?<perms>[^ ]+) (?<size>\d+) (?<name>.+)#', rtrim($info), $info)) {
$this->close_channel(self::CHANNEL_EXEC, true);
return false;
}
$fclose_check = false;
if (is_resource($local_file)) {
$fp = $local_file;
} elseif (!is_null($local_file)) {
$fp = @fopen($local_file, 'wb');
if (!$fp) {
$this->close_channel(self::CHANNEL_EXEC, true);
return false;
}
$fclose_check = true;
} else {
$content = '';
}
$size = 0;
while (true) {
$data = $this->get_channel_packet(self::CHANNEL_EXEC, true);
// Terminate the loop in case the server repeatedly sends an empty response
if ($data === false) {
$this->close_channel(self::CHANNEL_EXEC, true);
// no data received from server
return false;
}
// SCP usually seems to split stuff out into 16k chunks
$length = strlen($data);
$size += $length;
$end = $size > $info['size'];
if ($end) {
$diff = $size - $info['size'];
$offset = $length - $diff;
if ($data[$offset] === chr(0)) {
$data = substr($data, 0, -$diff);
} else {
$type = $data[$offset] === chr(1) ? 'warning' : 'error';
$this->scp_errors[] = "$type: " . substr($data, 1);
$this->close_channel(self::CHANNEL_EXEC, true);
return false;
}
}
if (is_null($local_file)) {
$content .= $data;
} else {
fputs($fp, $data);
}
if (is_callable($progressCallback)) {
call_user_func($progressCallback, $size);
}
if ($end) {
break;
}
}
$this->close_channel(self::CHANNEL_EXEC, true);
if ($fclose_check) {
fclose($fp);
}
// if $content isn't set that means a file was written to
return isset($content) ? $content : true;
}
/**
* Returns all errors on the SCP layer
*
* @return array
*/
public function getSCPErrors()
{
return $this->scp_errors;
}
/**
* Returns the last error on the SCP layer
*
* @return string
*/
public function getLastSCPError()
{
return count($this->scp_errors) ? $this->scp_errors[count($this->scp_errors) - 1] : '';
}
}
|