PageRenderTime 47ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/phpseclib/phpseclib/phpseclib/Net/SCP.php

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