4 * Pure-PHP implementation of SCP.
8 * The API for this library is modeled after the API from PHP's {@link http://php.net/book.ftp FTP extension}.
10 * Here's a short example of how to use this library:
13 * include 'Net/SCP.php';
14 * include 'Net/SSH2.php';
16 * $ssh = new Net_SSH2('www.domain.tld');
17 * if (!$ssh->login('username', 'password')) {
21 * $scp = new Net_SCP($ssh);
22 * $scp->put('abcd', str_repeat('x', 1024*1024));
26 * LICENSE: Permission is hereby granted, free of charge, to any person obtaining a copy
27 * of this software and associated documentation files (the "Software"), to deal
28 * in the Software without restriction, including without limitation the rights
29 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
30 * copies of the Software, and to permit persons to whom the Software is
31 * furnished to do so, subject to the following conditions:
33 * The above copyright notice and this permission notice shall be included in
34 * all copies or substantial portions of the Software.
36 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
37 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
38 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
39 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
40 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
41 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
46 * @author Jim Wigginton <terrafrost@php.net>
47 * @copyright 2010 Jim Wigginton
48 * @license http://www.opensource.org/licenses/mit-license.html MIT License
49 * @link http://phpseclib.sourceforge.net
57 * Reads data from a local file.
59 define('NET_SCP_LOCAL_FILE', 1);
61 * Reads data from a string.
63 define('NET_SCP_STRING', 2);
68 * @see Net_SCP::_send()
69 * @see Net_SCP::_receive()
74 define('NET_SCP_SSH1', 1);
78 define('NET_SCP_SSH2', 2);
82 * Pure-PHP implementations of SCP.
85 * @author Jim Wigginton <terrafrost@php.net>
115 * Default Constructor.
117 * Connects to an SSH server
119 * @param String $host
120 * @param optional Integer $port
121 * @param optional Integer $timeout
125 function Net_SCP($ssh)
127 if (!is_object($ssh)) {
131 switch (strtolower(get_class($ssh))) {
133 $this->mode = NET_SCP_SSH2;
136 $this->packet_size = 50000;
137 $this->mode = NET_SCP_SSH1;
147 * Uploads a file to the SCP server.
149 * By default, Net_SCP::put() does not read from the local filesystem. $data is dumped directly into $remote_file.
150 * So, for example, if you set $data to 'filename.ext' and then do Net_SCP::get(), you will get a file, twelve bytes
151 * long, containing 'filename.ext' as its contents.
153 * Setting $mode to NET_SCP_LOCAL_FILE will change the above behavior. With NET_SCP_LOCAL_FILE, $remote_file will
154 * contain as many bytes as filename.ext does on your local filesystem. If your filename.ext is 1MB then that is how
155 * large $remote_file will be, as well.
157 * Currently, only binary mode is supported. As such, if the line endings need to be adjusted, you will need to take
158 * care of that, yourself.
160 * @param String $remote_file
161 * @param String $data
162 * @param optional Integer $mode
163 * @param optional Callable $callback
167 function put($remote_file, $data, $mode = NET_SCP_STRING, $callback = null)
169 if (!isset($this->ssh)) {
173 if (!$this->ssh->exec('scp -t ' . escapeshellarg($remote_file), false)) { // -t = to
177 $temp = $this->_receive();
178 if ($temp !== chr(0)) {
182 if ($this->mode == NET_SCP_SSH2) {
183 $this->packet_size = $this->ssh->packet_size_client_to_server[NET_SSH2_CHANNEL_EXEC] - 4;
186 $remote_file = basename($remote_file);
188 if ($mode == NET_SCP_STRING) {
189 $size = strlen($data);
191 if (!is_file($data)) {
192 user_error("$data is not a valid file", E_USER_NOTICE);
196 $fp = @fopen($data, 'rb');
200 $size = filesize($data);
203 $this->_send('C0644 ' . $size . ' ' . $remote_file . "\n");
205 $temp = $this->_receive();
206 if ($temp !== chr(0)) {
211 while ($sent < $size) {
212 $temp = $mode & NET_SCP_STRING ? substr($data, $sent, $this->packet_size) : fread($fp, $this->packet_size);
214 $sent+= strlen($temp);
216 if (is_callable($callback)) {
217 call_user_func($callback, $sent);
222 if ($mode != NET_SCP_STRING) {
230 * Downloads a file from the SCP server.
232 * Returns a string containing the contents of $remote_file if $local_file is left undefined or a boolean false if
233 * the operation was unsuccessful. If $local_file is defined, returns true or false depending on the success of the
236 * @param String $remote_file
237 * @param optional String $local_file
241 function get($remote_file, $local_file = false)
243 if (!isset($this->ssh)) {
247 if (!$this->ssh->exec('scp -f ' . escapeshellarg($remote_file), false)) { // -f = from
253 if (!preg_match('#(?<perms>[^ ]+) (?<size>\d+) (?<name>.+)#', rtrim($this->_receive()), $info)) {
261 if ($local_file !== false) {
262 $fp = @fopen($local_file, 'wb');
269 while ($size < $info['size']) {
270 $data = $this->_receive();
271 // SCP usually seems to split stuff out into 16k chunks
272 $size+= strlen($data);
274 if ($local_file === false) {
283 if ($local_file !== false) {
292 * Sends a packet to an SSH server
294 * @param String $data
297 function _send($data)
299 switch ($this->mode) {
301 $this->ssh->_send_channel_packet(NET_SSH2_CHANNEL_EXEC, $data);
304 $data = pack('CNa*', NET_SSH1_CMSG_STDIN_DATA, strlen($data), $data);
305 $this->ssh->_send_binary_packet($data);
310 * Receives a packet from an SSH server
317 switch ($this->mode) {
319 return $this->ssh->_get_channel_packet(NET_SSH2_CHANNEL_EXEC, true);
321 if (!$this->ssh->bitmap) {
325 $response = $this->ssh->_get_binary_packet();
326 switch ($response[NET_SSH1_RESPONSE_TYPE]) {
327 case NET_SSH1_SMSG_STDOUT_DATA:
328 extract(unpack('Nlength', $response[NET_SSH1_RESPONSE_DATA]));
329 return $this->ssh->_string_shift($response[NET_SSH1_RESPONSE_DATA], $length);
330 case NET_SSH1_SMSG_STDERR_DATA:
332 case NET_SSH1_SMSG_EXITSTATUS:
333 $this->ssh->_send_binary_packet(chr(NET_SSH1_CMSG_EXIT_CONFIRMATION));
334 fclose($this->ssh->fsock);
335 $this->ssh->bitmap = 0;
338 user_error('Unknown packet received', E_USER_NOTICE);
346 * Closes the connection to an SSH server
352 switch ($this->mode) {
354 $this->ssh->_close_channel(NET_SSH2_CHANNEL_EXEC, true);
357 $this->ssh->disconnect();