Edit File by line
/home/barbar84/www/wp-conte.../plugins/worker/src/PHPSecLi.../Net
File: SCP.php
<?php
[0] Fix | Delete
[1] Fix | Delete
/**
[2] Fix | Delete
* Pure-PHP implementation of SCP.
[3] Fix | Delete
*
[4] Fix | Delete
* PHP versions 4 and 5
[5] Fix | Delete
*
[6] Fix | Delete
* The API for this library is modeled after the API from PHP's {@link http://php.net/book.ftp FTP extension}.
[7] Fix | Delete
*
[8] Fix | Delete
* Here's a short example of how to use this library:
[9] Fix | Delete
* <code>
[10] Fix | Delete
* <?php
[11] Fix | Delete
* include 'Net/SCP.php';
[12] Fix | Delete
* include 'Net/SSH2.php';
[13] Fix | Delete
*
[14] Fix | Delete
* $ssh = new Net_SSH2('www.domain.tld');
[15] Fix | Delete
* if (!$ssh->login('username', 'password')) {
[16] Fix | Delete
* exit('bad login');
[17] Fix | Delete
* }
[18] Fix | Delete
* $scp = new Net_SCP($ssh);
[19] Fix | Delete
* $scp->put('abcd', str_repeat('x', 1024*1024));
[20] Fix | Delete
* ?>
[21] Fix | Delete
* </code>
[22] Fix | Delete
*
[23] Fix | Delete
* LICENSE: Permission is hereby granted, free of charge, to any person obtaining a copy
[24] Fix | Delete
* of this software and associated documentation files (the "Software"), to deal
[25] Fix | Delete
* in the Software without restriction, including without limitation the rights
[26] Fix | Delete
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
[27] Fix | Delete
* copies of the Software, and to permit persons to whom the Software is
[28] Fix | Delete
* furnished to do so, subject to the following conditions:
[29] Fix | Delete
*
[30] Fix | Delete
* The above copyright notice and this permission notice shall be included in
[31] Fix | Delete
* all copies or substantial portions of the Software.
[32] Fix | Delete
*
[33] Fix | Delete
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
[34] Fix | Delete
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
[35] Fix | Delete
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
[36] Fix | Delete
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
[37] Fix | Delete
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
[38] Fix | Delete
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
[39] Fix | Delete
* THE SOFTWARE.
[40] Fix | Delete
*
[41] Fix | Delete
* @category Net
[42] Fix | Delete
* @package Net_SCP
[43] Fix | Delete
* @author Jim Wigginton <terrafrost@php.net>
[44] Fix | Delete
* @copyright MMX Jim Wigginton
[45] Fix | Delete
* @license http://www.opensource.org/licenses/mit-license.html MIT License
[46] Fix | Delete
* @link http://phpseclib.sourceforge.net
[47] Fix | Delete
*/
[48] Fix | Delete
[49] Fix | Delete
/**#@+
[50] Fix | Delete
* @access public
[51] Fix | Delete
* @see Net_SCP::put()
[52] Fix | Delete
*/
[53] Fix | Delete
/**
[54] Fix | Delete
* Reads data from a local file.
[55] Fix | Delete
*/
[56] Fix | Delete
define('NET_SCP_LOCAL_FILE', 1);
[57] Fix | Delete
/**
[58] Fix | Delete
* Reads data from a string.
[59] Fix | Delete
*/
[60] Fix | Delete
define('NET_SCP_STRING', 2);
[61] Fix | Delete
/**#@-*/
[62] Fix | Delete
[63] Fix | Delete
/**#@+
[64] Fix | Delete
* @access private
[65] Fix | Delete
* @see Net_SCP::_send()
[66] Fix | Delete
* @see Net_SCP::_receive()
[67] Fix | Delete
*/
[68] Fix | Delete
/**
[69] Fix | Delete
* SSH1 is being used.
[70] Fix | Delete
*/
[71] Fix | Delete
define('NET_SCP_SSH1', 1);
[72] Fix | Delete
/**
[73] Fix | Delete
* SSH2 is being used.
[74] Fix | Delete
*/
[75] Fix | Delete
define('NET_SCP_SSH2', 2);
[76] Fix | Delete
/**#@-*/
[77] Fix | Delete
[78] Fix | Delete
/**
[79] Fix | Delete
* Pure-PHP implementations of SCP.
[80] Fix | Delete
*
[81] Fix | Delete
* @package Net_SCP
[82] Fix | Delete
* @author Jim Wigginton <terrafrost@php.net>
[83] Fix | Delete
* @access public
[84] Fix | Delete
*/
[85] Fix | Delete
class Net_SCP
[86] Fix | Delete
{
[87] Fix | Delete
/**
[88] Fix | Delete
* SSH Object
[89] Fix | Delete
*
[90] Fix | Delete
* @var Object
[91] Fix | Delete
* @access private
[92] Fix | Delete
*/
[93] Fix | Delete
public $ssh;
[94] Fix | Delete
[95] Fix | Delete
/**
[96] Fix | Delete
* Packet Size
[97] Fix | Delete
*
[98] Fix | Delete
* @var Integer
[99] Fix | Delete
* @access private
[100] Fix | Delete
*/
[101] Fix | Delete
public $packet_size;
[102] Fix | Delete
[103] Fix | Delete
/**
[104] Fix | Delete
* Mode
[105] Fix | Delete
*
[106] Fix | Delete
* @var Integer
[107] Fix | Delete
* @access private
[108] Fix | Delete
*/
[109] Fix | Delete
public $mode;
[110] Fix | Delete
[111] Fix | Delete
/**
[112] Fix | Delete
* Default Constructor.
[113] Fix | Delete
*
[114] Fix | Delete
* Connects to an SSH server
[115] Fix | Delete
*
[116] Fix | Delete
* @param String $host
[117] Fix | Delete
* @param optional Integer $port
[118] Fix | Delete
* @param optional Integer $timeout
[119] Fix | Delete
*
[120] Fix | Delete
* @return Net_SCP
[121] Fix | Delete
* @access public
[122] Fix | Delete
*/
[123] Fix | Delete
public function __construct($ssh)
[124] Fix | Delete
{
[125] Fix | Delete
if (!is_object($ssh)) {
[126] Fix | Delete
return;
[127] Fix | Delete
}
[128] Fix | Delete
[129] Fix | Delete
switch (strtolower(get_class($ssh))) {
[130] Fix | Delete
case'net_ssh2':
[131] Fix | Delete
$this->mode = NET_SCP_SSH2;
[132] Fix | Delete
break;
[133] Fix | Delete
case 'net_ssh1':
[134] Fix | Delete
$this->packet_size = 50000;
[135] Fix | Delete
$this->mode = NET_SCP_SSH1;
[136] Fix | Delete
break;
[137] Fix | Delete
default:
[138] Fix | Delete
return;
[139] Fix | Delete
}
[140] Fix | Delete
[141] Fix | Delete
$this->ssh = $ssh;
[142] Fix | Delete
}
[143] Fix | Delete
[144] Fix | Delete
/**
[145] Fix | Delete
* Uploads a file to the SCP server.
[146] Fix | Delete
*
[147] Fix | Delete
* By default, Net_SCP::put() does not read from the local filesystem. $data is dumped directly into $remote_file.
[148] Fix | Delete
* So, for example, if you set $data to 'filename.ext' and then do Net_SCP::get(), you will get a file, twelve bytes
[149] Fix | Delete
* long, containing 'filename.ext' as its contents.
[150] Fix | Delete
*
[151] Fix | Delete
* Setting $mode to NET_SCP_LOCAL_FILE will change the above behavior. With NET_SCP_LOCAL_FILE, $remote_file will
[152] Fix | Delete
* contain as many bytes as filename.ext does on your local filesystem. If your filename.ext is 1MB then that is how
[153] Fix | Delete
* large $remote_file will be, as well.
[154] Fix | Delete
*
[155] Fix | Delete
* Currently, only binary mode is supported. As such, if the line endings need to be adjusted, you will need to take
[156] Fix | Delete
* care of that, yourself.
[157] Fix | Delete
*
[158] Fix | Delete
* @param String $remote_file
[159] Fix | Delete
* @param String $data
[160] Fix | Delete
* @param optional Integer $mode
[161] Fix | Delete
* @param optional Callable $callback
[162] Fix | Delete
*
[163] Fix | Delete
* @return Boolean
[164] Fix | Delete
* @access public
[165] Fix | Delete
*/
[166] Fix | Delete
public function put($remote_file, $data, $mode = NET_SCP_STRING, $callback = null)
[167] Fix | Delete
{
[168] Fix | Delete
if (!isset($this->ssh)) {
[169] Fix | Delete
return false;
[170] Fix | Delete
}
[171] Fix | Delete
[172] Fix | Delete
if (!$this->ssh->exec('scp -t "'.$remote_file.'"', false)) { // -t = to
[173] Fix | Delete
return false;
[174] Fix | Delete
}
[175] Fix | Delete
[176] Fix | Delete
$temp = $this->_receive();
[177] Fix | Delete
if ($temp !== chr(0)) {
[178] Fix | Delete
return false;
[179] Fix | Delete
}
[180] Fix | Delete
[181] Fix | Delete
if ($this->mode == NET_SCP_SSH2) {
[182] Fix | Delete
$this->packet_size = $this->ssh->packet_size_client_to_server[NET_SSH2_CHANNEL_EXEC] - 4;
[183] Fix | Delete
}
[184] Fix | Delete
[185] Fix | Delete
$remote_file = basename($remote_file);
[186] Fix | Delete
[187] Fix | Delete
if ($mode == NET_SCP_STRING) {
[188] Fix | Delete
$size = strlen($data);
[189] Fix | Delete
} else {
[190] Fix | Delete
if (!is_file($data)) {
[191] Fix | Delete
user_error("$data is not a valid file", E_USER_NOTICE);
[192] Fix | Delete
[193] Fix | Delete
return false;
[194] Fix | Delete
}
[195] Fix | Delete
[196] Fix | Delete
$fp = @fopen($data, 'rb');
[197] Fix | Delete
if (!$fp) {
[198] Fix | Delete
fclose($fp);
[199] Fix | Delete
[200] Fix | Delete
return false;
[201] Fix | Delete
}
[202] Fix | Delete
$size = filesize($data);
[203] Fix | Delete
}
[204] Fix | Delete
[205] Fix | Delete
$this->_send('C0644 '.$size.' '.$remote_file."\n");
[206] Fix | Delete
[207] Fix | Delete
$temp = $this->_receive();
[208] Fix | Delete
if ($temp !== chr(0)) {
[209] Fix | Delete
return false;
[210] Fix | Delete
}
[211] Fix | Delete
[212] Fix | Delete
$sent = 0;
[213] Fix | Delete
while ($sent < $size) {
[214] Fix | Delete
$temp = $mode & NET_SCP_STRING ? substr($data, $sent, $this->packet_size) : fread($fp, $this->packet_size);
[215] Fix | Delete
$this->_send($temp);
[216] Fix | Delete
$sent += strlen($temp);
[217] Fix | Delete
[218] Fix | Delete
if (is_callable($callback)) {
[219] Fix | Delete
call_user_func($callback, $sent);
[220] Fix | Delete
}
[221] Fix | Delete
}
[222] Fix | Delete
$this->_close();
[223] Fix | Delete
[224] Fix | Delete
if ($mode != NET_SCP_STRING) {
[225] Fix | Delete
fclose($fp);
[226] Fix | Delete
}
[227] Fix | Delete
[228] Fix | Delete
return true;
[229] Fix | Delete
}
[230] Fix | Delete
[231] Fix | Delete
/**
[232] Fix | Delete
* Downloads a file from the SCP server.
[233] Fix | Delete
*
[234] Fix | Delete
* Returns a string containing the contents of $remote_file if $local_file is left undefined or a boolean false if
[235] Fix | Delete
* the operation was unsuccessful. If $local_file is defined, returns true or false depending on the success of the
[236] Fix | Delete
* operation
[237] Fix | Delete
*
[238] Fix | Delete
* @param String $remote_file
[239] Fix | Delete
* @param optional String $local_file
[240] Fix | Delete
*
[241] Fix | Delete
* @return Mixed
[242] Fix | Delete
* @access public
[243] Fix | Delete
*/
[244] Fix | Delete
public function get($remote_file, $local_file = false)
[245] Fix | Delete
{
[246] Fix | Delete
if (!isset($this->ssh)) {
[247] Fix | Delete
return false;
[248] Fix | Delete
}
[249] Fix | Delete
[250] Fix | Delete
if (!$this->ssh->exec('scp -f "'.$remote_file.'"', false)) { // -f = from
[251] Fix | Delete
return false;
[252] Fix | Delete
}
[253] Fix | Delete
[254] Fix | Delete
$this->_send("\0");
[255] Fix | Delete
[256] Fix | Delete
if (!preg_match('#(?<perms>[^ ]+) (?<size>\d+) (?<name>.+)#', rtrim($this->_receive()), $info)) {
[257] Fix | Delete
return false;
[258] Fix | Delete
}
[259] Fix | Delete
[260] Fix | Delete
$this->_send("\0");
[261] Fix | Delete
[262] Fix | Delete
$size = 0;
[263] Fix | Delete
[264] Fix | Delete
if ($local_file !== false) {
[265] Fix | Delete
$fp = @fopen($local_file, 'wb');
[266] Fix | Delete
if (!$fp) {
[267] Fix | Delete
return false;
[268] Fix | Delete
}
[269] Fix | Delete
}
[270] Fix | Delete
[271] Fix | Delete
$content = '';
[272] Fix | Delete
while ($size < $info['size']) {
[273] Fix | Delete
$data = $this->_receive();
[274] Fix | Delete
// SCP usually seems to split stuff out into 16k chunks
[275] Fix | Delete
$size += strlen($data);
[276] Fix | Delete
[277] Fix | Delete
if ($local_file === false) {
[278] Fix | Delete
$content .= $data;
[279] Fix | Delete
} else {
[280] Fix | Delete
fputs($fp, $data);
[281] Fix | Delete
}
[282] Fix | Delete
}
[283] Fix | Delete
[284] Fix | Delete
$this->_close();
[285] Fix | Delete
[286] Fix | Delete
if ($local_file !== false) {
[287] Fix | Delete
fclose($fp);
[288] Fix | Delete
[289] Fix | Delete
return true;
[290] Fix | Delete
}
[291] Fix | Delete
[292] Fix | Delete
return $content;
[293] Fix | Delete
}
[294] Fix | Delete
[295] Fix | Delete
/**
[296] Fix | Delete
* Sends a packet to an SSH server
[297] Fix | Delete
*
[298] Fix | Delete
* @param String $data
[299] Fix | Delete
*
[300] Fix | Delete
* @access private
[301] Fix | Delete
*/
[302] Fix | Delete
public function _send($data)
[303] Fix | Delete
{
[304] Fix | Delete
switch ($this->mode) {
[305] Fix | Delete
case NET_SCP_SSH2:
[306] Fix | Delete
$this->ssh->_send_channel_packet(NET_SSH2_CHANNEL_EXEC, $data);
[307] Fix | Delete
break;
[308] Fix | Delete
case NET_SCP_SSH1:
[309] Fix | Delete
$data = pack('CNa*', NET_SSH1_CMSG_STDIN_DATA, strlen($data), $data);
[310] Fix | Delete
$this->ssh->_send_binary_packet($data);
[311] Fix | Delete
}
[312] Fix | Delete
}
[313] Fix | Delete
[314] Fix | Delete
/**
[315] Fix | Delete
* Receives a packet from an SSH server
[316] Fix | Delete
*
[317] Fix | Delete
* @return String
[318] Fix | Delete
* @access private
[319] Fix | Delete
*/
[320] Fix | Delete
public function _receive()
[321] Fix | Delete
{
[322] Fix | Delete
switch ($this->mode) {
[323] Fix | Delete
case NET_SCP_SSH2:
[324] Fix | Delete
return $this->ssh->_get_channel_packet(NET_SSH2_CHANNEL_EXEC, true);
[325] Fix | Delete
case NET_SCP_SSH1:
[326] Fix | Delete
if (!$this->ssh->bitmap) {
[327] Fix | Delete
return false;
[328] Fix | Delete
}
[329] Fix | Delete
while (true) {
[330] Fix | Delete
$response = $this->ssh->_get_binary_packet();
[331] Fix | Delete
switch ($response[NET_SSH1_RESPONSE_TYPE]) {
[332] Fix | Delete
case NET_SSH1_SMSG_STDOUT_DATA:
[333] Fix | Delete
extract(unpack('Nlength', $response[NET_SSH1_RESPONSE_DATA]));
[334] Fix | Delete
[335] Fix | Delete
return $this->ssh->_string_shift($response[NET_SSH1_RESPONSE_DATA], $length);
[336] Fix | Delete
case NET_SSH1_SMSG_STDERR_DATA:
[337] Fix | Delete
break;
[338] Fix | Delete
case NET_SSH1_SMSG_EXITSTATUS:
[339] Fix | Delete
$this->ssh->_send_binary_packet(chr(NET_SSH1_CMSG_EXIT_CONFIRMATION));
[340] Fix | Delete
fclose($this->ssh->fsock);
[341] Fix | Delete
$this->ssh->bitmap = 0;
[342] Fix | Delete
[343] Fix | Delete
return false;
[344] Fix | Delete
default:
[345] Fix | Delete
user_error('Unknown packet received', E_USER_NOTICE);
[346] Fix | Delete
[347] Fix | Delete
return false;
[348] Fix | Delete
}
[349] Fix | Delete
}
[350] Fix | Delete
}
[351] Fix | Delete
}
[352] Fix | Delete
[353] Fix | Delete
/**
[354] Fix | Delete
* Closes the connection to an SSH server
[355] Fix | Delete
*
[356] Fix | Delete
* @access private
[357] Fix | Delete
*/
[358] Fix | Delete
public function _close()
[359] Fix | Delete
{
[360] Fix | Delete
switch ($this->mode) {
[361] Fix | Delete
case NET_SCP_SSH2:
[362] Fix | Delete
$this->ssh->_close_channel(NET_SSH2_CHANNEL_EXEC, true);
[363] Fix | Delete
break;
[364] Fix | Delete
case NET_SCP_SSH1:
[365] Fix | Delete
$this->ssh->disconnect();
[366] Fix | Delete
}
[367] Fix | Delete
}
[368] Fix | Delete
}
[369] Fix | Delete
[370] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function