]> git.mxchange.org Git - friendica-addons.git/blob - securemail/vendor/phpseclib/phpseclib/phpseclib/Net/SCP.php
securemail: update pgp library
[friendica-addons.git] / securemail / vendor / phpseclib / phpseclib / phpseclib / Net / SCP.php
1 <?php
2
3 /**
4  * Pure-PHP implementation of SCP.
5  *
6  * PHP versions 4 and 5
7  *
8  * The API for this library is modeled after the API from PHP's {@link http://php.net/book.ftp FTP extension}.
9  *
10  * Here's a short example of how to use this library:
11  * <code>
12  * <?php
13  *    include 'Net/SCP.php';
14  *    include 'Net/SSH2.php';
15  *
16  *    $ssh = new Net_SSH2('www.domain.tld');
17  *    if (!$ssh->login('username', 'password')) {
18  *        exit('bad login');
19  *    }
20
21  *    $scp = new Net_SCP($ssh);
22  *    $scp->put('abcd', str_repeat('x', 1024*1024));
23  * ?>
24  * </code>
25  *
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:
32  *
33  * The above copyright notice and this permission notice shall be included in
34  * all copies or substantial portions of the Software.
35  *
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
42  * THE SOFTWARE.
43  *
44  * @category  Net
45  * @package   Net_SCP
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
50  */
51
52 /**#@+
53  * @access public
54  * @see Net_SCP::put()
55  */
56 /**
57  * Reads data from a local file.
58  */
59 define('NET_SCP_LOCAL_FILE', 1);
60 /**
61  * Reads data from a string.
62  */
63 define('NET_SCP_STRING',  2);
64 /**#@-*/
65
66 /**#@+
67  * @access private
68  * @see Net_SCP::_send()
69  * @see Net_SCP::_receive()
70  */
71 /**
72  * SSH1 is being used.
73  */
74 define('NET_SCP_SSH1', 1);
75 /**
76  * SSH2 is being used.
77  */
78 define('NET_SCP_SSH2',  2);
79 /**#@-*/
80
81 /**
82  * Pure-PHP implementations of SCP.
83  *
84  * @package Net_SCP
85  * @author  Jim Wigginton <terrafrost@php.net>
86  * @access  public
87  */
88 class Net_SCP
89 {
90     /**
91      * SSH Object
92      *
93      * @var Object
94      * @access private
95      */
96     var $ssh;
97
98     /**
99      * Packet Size
100      *
101      * @var Integer
102      * @access private
103      */
104     var $packet_size;
105
106     /**
107      * Mode
108      *
109      * @var Integer
110      * @access private
111      */
112     var $mode;
113
114     /**
115      * Default Constructor.
116      *
117      * Connects to an SSH server
118      *
119      * @param String $host
120      * @param optional Integer $port
121      * @param optional Integer $timeout
122      * @return Net_SCP
123      * @access public
124      */
125     function Net_SCP($ssh)
126     {
127         if (!is_object($ssh)) {
128             return;
129         }
130
131         switch (strtolower(get_class($ssh))) {
132             case 'net_ssh2':
133                 $this->mode = NET_SCP_SSH2;
134                 break;
135             case 'net_ssh1':
136                 $this->packet_size = 50000;
137                 $this->mode = NET_SCP_SSH1;
138                 break;
139             default:
140                 return;
141         }
142
143         $this->ssh = $ssh;
144     }
145
146     /**
147      * Uploads a file to the SCP server.
148      *
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.
152      *
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.
156      *
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.
159      *
160      * @param String $remote_file
161      * @param String $data
162      * @param optional Integer $mode
163      * @param optional Callable $callback
164      * @return Boolean
165      * @access public
166      */
167     function put($remote_file, $data, $mode = NET_SCP_STRING, $callback = null)
168     {
169         if (!isset($this->ssh)) {
170             return false;
171         }
172
173         if (!$this->ssh->exec('scp -t ' . escapeshellarg($remote_file), false)) { // -t = to
174             return false;
175         }
176
177         $temp = $this->_receive();
178         if ($temp !== chr(0)) {
179             return false;
180         }
181
182         if ($this->mode == NET_SCP_SSH2) {
183             $this->packet_size = $this->ssh->packet_size_client_to_server[NET_SSH2_CHANNEL_EXEC] - 4;
184         }
185
186         $remote_file = basename($remote_file);
187
188         if ($mode == NET_SCP_STRING) {
189             $size = strlen($data);
190         } else {
191             if (!is_file($data)) {
192                 user_error("$data is not a valid file", E_USER_NOTICE);
193                 return false;
194             }
195
196             $fp = @fopen($data, 'rb');
197             if (!$fp) {
198                 return false;
199             }
200             $size = filesize($data);
201         }
202
203         $this->_send('C0644 ' . $size . ' ' . $remote_file . "\n");
204
205         $temp = $this->_receive();
206         if ($temp !== chr(0)) {
207             return false;
208         }
209
210         $sent = 0;
211         while ($sent < $size) {
212             $temp = $mode & NET_SCP_STRING ? substr($data, $sent, $this->packet_size) : fread($fp, $this->packet_size);
213             $this->_send($temp);
214             $sent+= strlen($temp);
215
216             if (is_callable($callback)) {
217                 call_user_func($callback, $sent);
218             }
219         }
220         $this->_close();
221
222         if ($mode != NET_SCP_STRING) {
223             fclose($fp);
224         }
225
226         return true;
227     }
228
229     /**
230      * Downloads a file from the SCP server.
231      *
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
234      * operation
235      *
236      * @param String $remote_file
237      * @param optional String $local_file
238      * @return Mixed
239      * @access public
240      */
241     function get($remote_file, $local_file = false)
242     {
243         if (!isset($this->ssh)) {
244             return false;
245         }
246
247         if (!$this->ssh->exec('scp -f ' . escapeshellarg($remote_file), false)) { // -f = from
248             return false;
249         }
250
251         $this->_send("\0");
252
253         if (!preg_match('#(?<perms>[^ ]+) (?<size>\d+) (?<name>.+)#', rtrim($this->_receive()), $info)) {
254             return false;
255         }
256
257         $this->_send("\0");
258
259         $size = 0;
260
261         if ($local_file !== false) {
262             $fp = @fopen($local_file, 'wb');
263             if (!$fp) {
264                 return false;
265             }
266         }
267
268         $content = '';
269         while ($size < $info['size']) {
270             $data = $this->_receive();
271             // SCP usually seems to split stuff out into 16k chunks
272             $size+= strlen($data);
273
274             if ($local_file === false) {
275                 $content.= $data;
276             } else {
277                 fputs($fp, $data);
278             }
279         }
280
281         $this->_close();
282
283         if ($local_file !== false) {
284             fclose($fp);
285             return true;
286         }
287
288         return $content;
289     }
290
291     /**
292      * Sends a packet to an SSH server
293      *
294      * @param String $data
295      * @access private
296      */
297     function _send($data)
298     {
299         switch ($this->mode) {
300             case NET_SCP_SSH2:
301                 $this->ssh->_send_channel_packet(NET_SSH2_CHANNEL_EXEC, $data);
302                 break;
303             case NET_SCP_SSH1:
304                 $data = pack('CNa*', NET_SSH1_CMSG_STDIN_DATA, strlen($data), $data);
305                 $this->ssh->_send_binary_packet($data);
306          }
307     }
308
309     /**
310      * Receives a packet from an SSH server
311      *
312      * @return String
313      * @access private
314      */
315     function _receive()
316     {
317         switch ($this->mode) {
318             case NET_SCP_SSH2:
319                 return $this->ssh->_get_channel_packet(NET_SSH2_CHANNEL_EXEC, true);
320             case NET_SCP_SSH1:
321                 if (!$this->ssh->bitmap) {
322                     return false;
323                 }
324                 while (true) {
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:
331                             break;
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;
336                             return false;
337                         default:
338                             user_error('Unknown packet received', E_USER_NOTICE);
339                             return false;
340                     }
341                 }
342          }
343     }
344
345     /**
346      * Closes the connection to an SSH server
347      *
348      * @access private
349      */
350     function _close()
351     {
352         switch ($this->mode) {
353             case NET_SCP_SSH2:
354                 $this->ssh->_close_channel(NET_SSH2_CHANNEL_EXEC, true);
355                 break;
356             case NET_SCP_SSH1:
357                 $this->ssh->disconnect();
358          }
359     }
360 }