SCP.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. <?php
  2. /**
  3. * Pure-PHP implementation of SCP.
  4. *
  5. * PHP version 5
  6. *
  7. * The API for this library is modeled after the API from PHP's {@link http://php.net/book.ftp FTP extension}.
  8. *
  9. * Here's a short example of how to use this library:
  10. * <code>
  11. * <?php
  12. * include 'vendor/autoload.php';
  13. *
  14. * $ssh = new \phpseclib\Net\SSH2('www.domain.tld');
  15. * if (!$ssh->login('username', 'password')) {
  16. * exit('bad login');
  17. * }
  18. * $scp = new \phpseclib\Net\SCP($ssh);
  19. *
  20. * $scp->put('abcd', str_repeat('x', 1024*1024));
  21. * ?>
  22. * </code>
  23. *
  24. * @category Net
  25. * @package SCP
  26. * @author Jim Wigginton <terrafrost@php.net>
  27. * @copyright 2010 Jim Wigginton
  28. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  29. * @link http://phpseclib.sourceforge.net
  30. */
  31. namespace phpseclib\Net;
  32. use phpseclib\Exception\FileNotFoundException;
  33. /**
  34. * Pure-PHP implementations of SCP.
  35. *
  36. * @package SCP
  37. * @author Jim Wigginton <terrafrost@php.net>
  38. * @access public
  39. */
  40. class SCP
  41. {
  42. /**#@+
  43. * @access public
  44. * @see \phpseclib\Net\SCP::put()
  45. */
  46. /**
  47. * Reads data from a local file.
  48. */
  49. const SOURCE_LOCAL_FILE = 1;
  50. /**
  51. * Reads data from a string.
  52. */
  53. const SOURCE_STRING = 2;
  54. /**#@-*/
  55. /**#@+
  56. * @access private
  57. * @see \phpseclib\Net\SCP::_send()
  58. * @see \phpseclib\Net\SCP::_receive()
  59. */
  60. /**
  61. * SSH1 is being used.
  62. */
  63. const MODE_SSH1 = 1;
  64. /**
  65. * SSH2 is being used.
  66. */
  67. const MODE_SSH2 = 2;
  68. /**#@-*/
  69. /**
  70. * SSH Object
  71. *
  72. * @var object
  73. * @access private
  74. */
  75. var $ssh;
  76. /**
  77. * Packet Size
  78. *
  79. * @var int
  80. * @access private
  81. */
  82. var $packet_size;
  83. /**
  84. * Mode
  85. *
  86. * @var int
  87. * @access private
  88. */
  89. var $mode;
  90. /**
  91. * Default Constructor.
  92. *
  93. * Connects to an SSH server
  94. *
  95. * @param string $host
  96. * @param int $port
  97. * @param int $timeout
  98. * @return \phpseclib\Net\SCP
  99. * @access public
  100. */
  101. function __construct($ssh)
  102. {
  103. if ($ssh instanceof SSH2) {
  104. $this->mode = self::MODE_SSH2;
  105. } elseif ($ssh instanceof SSH1) {
  106. $this->packet_size = 50000;
  107. $this->mode = self::MODE_SSH1;
  108. } else {
  109. return;
  110. }
  111. $this->ssh = $ssh;
  112. }
  113. /**
  114. * Uploads a file to the SCP server.
  115. *
  116. * By default, \phpseclib\Net\SCP::put() does not read from the local filesystem. $data is dumped directly into $remote_file.
  117. * So, for example, if you set $data to 'filename.ext' and then do \phpseclib\Net\SCP::get(), you will get a file, twelve bytes
  118. * long, containing 'filename.ext' as its contents.
  119. *
  120. * Setting $mode to self::SOURCE_LOCAL_FILE will change the above behavior. With self::SOURCE_LOCAL_FILE, $remote_file will
  121. * contain as many bytes as filename.ext does on your local filesystem. If your filename.ext is 1MB then that is how
  122. * large $remote_file will be, as well.
  123. *
  124. * Currently, only binary mode is supported. As such, if the line endings need to be adjusted, you will need to take
  125. * care of that, yourself.
  126. *
  127. * @param string $remote_file
  128. * @param string $data
  129. * @param int $mode
  130. * @param callable $callback
  131. * @throws \phpseclib\Exception\FileNotFoundException if you're uploading via a file and the file doesn't exist
  132. * @return bool
  133. * @access public
  134. */
  135. function put($remote_file, $data, $mode = self::SOURCE_STRING, $callback = null)
  136. {
  137. if (!isset($this->ssh)) {
  138. return false;
  139. }
  140. if (!$this->ssh->exec('scp -t ' . escapeshellarg($remote_file), false)) { // -t = to
  141. return false;
  142. }
  143. $temp = $this->_receive();
  144. if ($temp !== chr(0)) {
  145. return false;
  146. }
  147. if ($this->mode == self::MODE_SSH2) {
  148. $this->packet_size = $this->ssh->packet_size_client_to_server[SSH2::CHANNEL_EXEC] - 4;
  149. }
  150. $remote_file = basename($remote_file);
  151. if ($mode == self::SOURCE_STRING) {
  152. $size = strlen($data);
  153. } else {
  154. if (!is_file($data)) {
  155. throw new FileNotFoundException("$data is not a valid file");
  156. }
  157. $fp = @fopen($data, 'rb');
  158. if (!$fp) {
  159. return false;
  160. }
  161. $size = filesize($data);
  162. }
  163. $this->_send('C0644 ' . $size . ' ' . $remote_file . "\n");
  164. $temp = $this->_receive();
  165. if ($temp !== chr(0)) {
  166. return false;
  167. }
  168. $sent = 0;
  169. while ($sent < $size) {
  170. $temp = $mode & self::SOURCE_STRING ? substr($data, $sent, $this->packet_size) : fread($fp, $this->packet_size);
  171. $this->_send($temp);
  172. $sent+= strlen($temp);
  173. if (is_callable($callback)) {
  174. call_user_func($callback, $sent);
  175. }
  176. }
  177. $this->_close();
  178. if ($mode != self::SOURCE_STRING) {
  179. fclose($fp);
  180. }
  181. return true;
  182. }
  183. /**
  184. * Downloads a file from the SCP server.
  185. *
  186. * Returns a string containing the contents of $remote_file if $local_file is left undefined or a boolean false if
  187. * the operation was unsuccessful. If $local_file is defined, returns true or false depending on the success of the
  188. * operation
  189. *
  190. * @param string $remote_file
  191. * @param string $local_file
  192. * @return mixed
  193. * @access public
  194. */
  195. function get($remote_file, $local_file = false)
  196. {
  197. if (!isset($this->ssh)) {
  198. return false;
  199. }
  200. if (!$this->ssh->exec('scp -f ' . escapeshellarg($remote_file), false)) { // -f = from
  201. return false;
  202. }
  203. $this->_send("\0");
  204. if (!preg_match('#(?<perms>[^ ]+) (?<size>\d+) (?<name>.+)#', rtrim($this->_receive()), $info)) {
  205. return false;
  206. }
  207. $this->_send("\0");
  208. $size = 0;
  209. if ($local_file !== false) {
  210. $fp = @fopen($local_file, 'wb');
  211. if (!$fp) {
  212. return false;
  213. }
  214. }
  215. $content = '';
  216. while ($size < $info['size']) {
  217. $data = $this->_receive();
  218. // SCP usually seems to split stuff out into 16k chunks
  219. $size+= strlen($data);
  220. if ($local_file === false) {
  221. $content.= $data;
  222. } else {
  223. fputs($fp, $data);
  224. }
  225. }
  226. $this->_close();
  227. if ($local_file !== false) {
  228. fclose($fp);
  229. return true;
  230. }
  231. return $content;
  232. }
  233. /**
  234. * Sends a packet to an SSH server
  235. *
  236. * @param string $data
  237. * @access private
  238. */
  239. function _send($data)
  240. {
  241. switch ($this->mode) {
  242. case self::MODE_SSH2:
  243. $this->ssh->_send_channel_packet(SSH2::CHANNEL_EXEC, $data);
  244. break;
  245. case self::MODE_SSH1:
  246. $data = pack('CNa*', NET_SSH1_CMSG_STDIN_DATA, strlen($data), $data);
  247. $this->ssh->_send_binary_packet($data);
  248. }
  249. }
  250. /**
  251. * Receives a packet from an SSH server
  252. *
  253. * @return string
  254. * @throws \UnexpectedValueException on receipt of an unexpected packet
  255. * @access private
  256. */
  257. function _receive()
  258. {
  259. switch ($this->mode) {
  260. case self::MODE_SSH2:
  261. return $this->ssh->_get_channel_packet(SSH2::CHANNEL_EXEC, true);
  262. case self::MODE_SSH1:
  263. if (!$this->ssh->bitmap) {
  264. return false;
  265. }
  266. while (true) {
  267. $response = $this->ssh->_get_binary_packet();
  268. switch ($response[SSH1::RESPONSE_TYPE]) {
  269. case NET_SSH1_SMSG_STDOUT_DATA:
  270. extract(unpack('Nlength', $response[SSH1::RESPONSE_DATA]));
  271. return $this->ssh->_string_shift($response[SSH1::RESPONSE_DATA], $length);
  272. case NET_SSH1_SMSG_STDERR_DATA:
  273. break;
  274. case NET_SSH1_SMSG_EXITSTATUS:
  275. $this->ssh->_send_binary_packet(chr(NET_SSH1_CMSG_EXIT_CONFIRMATION));
  276. fclose($this->ssh->fsock);
  277. $this->ssh->bitmap = 0;
  278. return false;
  279. default:
  280. throw new \UnexpectedValueException('Unknown packet received');
  281. }
  282. }
  283. }
  284. }
  285. /**
  286. * Closes the connection to an SSH server
  287. *
  288. * @access private
  289. */
  290. function _close()
  291. {
  292. switch ($this->mode) {
  293. case self::MODE_SSH2:
  294. $this->ssh->_close_channel(SSH2::CHANNEL_EXEC, true);
  295. break;
  296. case self::MODE_SSH1:
  297. $this->ssh->disconnect();
  298. }
  299. }
  300. }