PageRenderTime 54ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/phpseclib/phpseclib/phpseclib/Net/SFTP.php

https://bitbucket.org/Kamor/nexway
PHP | 2778 lines | 1538 code | 287 blank | 953 comment | 322 complexity | 6bc2a83cb408b8f5bf9d6cec971e3d45 MD5 | raw file
Possible License(s): MIT, BSD-3-Clause
  1. <?php
  2. /**
  3. * Pure-PHP implementation of SFTP.
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * Currently only supports SFTPv2 and v3, which, according to wikipedia.org, "is the most widely used version,
  8. * implemented by the popular OpenSSH SFTP server". If you want SFTPv4/5/6 support, provide me with access
  9. * to an SFTPv4/5/6 server.
  10. *
  11. * The API for this library is modeled after the API from PHP's {@link http://php.net/book.ftp FTP extension}.
  12. *
  13. * Here's a short example of how to use this library:
  14. * <code>
  15. * <?php
  16. * include 'Net/SFTP.php';
  17. *
  18. * $sftp = new Net_SFTP('www.domain.tld');
  19. * if (!$sftp->login('username', 'password')) {
  20. * exit('Login Failed');
  21. * }
  22. *
  23. * echo $sftp->pwd() . "\r\n";
  24. * $sftp->put('filename.ext', 'hello, world!');
  25. * print_r($sftp->nlist());
  26. * ?>
  27. * </code>
  28. *
  29. * LICENSE: Permission is hereby granted, free of charge, to any person obtaining a copy
  30. * of this software and associated documentation files (the "Software"), to deal
  31. * in the Software without restriction, including without limitation the rights
  32. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  33. * copies of the Software, and to permit persons to whom the Software is
  34. * furnished to do so, subject to the following conditions:
  35. *
  36. * The above copyright notice and this permission notice shall be included in
  37. * all copies or substantial portions of the Software.
  38. *
  39. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  40. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  41. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  42. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  43. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  44. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  45. * THE SOFTWARE.
  46. *
  47. * @category Net
  48. * @package Net_SFTP
  49. * @author Jim Wigginton <terrafrost@php.net>
  50. * @copyright MMIX Jim Wigginton
  51. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  52. * @link http://phpseclib.sourceforge.net
  53. */
  54. /**
  55. * Include Net_SSH2
  56. */
  57. if (!class_exists('Net_SSH2')) {
  58. include_once 'SSH2.php';
  59. }
  60. /**#@+
  61. * @access public
  62. * @see Net_SFTP::getLog()
  63. */
  64. /**
  65. * Returns the message numbers
  66. */
  67. define('NET_SFTP_LOG_SIMPLE', NET_SSH2_LOG_SIMPLE);
  68. /**
  69. * Returns the message content
  70. */
  71. define('NET_SFTP_LOG_COMPLEX', NET_SSH2_LOG_COMPLEX);
  72. /**
  73. * Outputs the message content in real-time.
  74. */
  75. define('NET_SFTP_LOG_REALTIME', 3);
  76. /**#@-*/
  77. /**
  78. * SFTP channel constant
  79. *
  80. * Net_SSH2::exec() uses 0 and Net_SSH2::read() / Net_SSH2::write() use 1.
  81. *
  82. * @see Net_SSH2::_send_channel_packet()
  83. * @see Net_SSH2::_get_channel_packet()
  84. * @access private
  85. */
  86. define('NET_SFTP_CHANNEL', 0x100);
  87. /**#@+
  88. * @access public
  89. * @see Net_SFTP::put()
  90. */
  91. /**
  92. * Reads data from a local file.
  93. */
  94. define('NET_SFTP_LOCAL_FILE', 1);
  95. /**
  96. * Reads data from a string.
  97. */
  98. // this value isn't really used anymore but i'm keeping it reserved for historical reasons
  99. define('NET_SFTP_STRING', 2);
  100. /**
  101. * Resumes an upload
  102. */
  103. define('NET_SFTP_RESUME', 4);
  104. /**
  105. * Append a local file to an already existing remote file
  106. */
  107. define('NET_SFTP_RESUME_START', 8);
  108. /**#@-*/
  109. /**
  110. * Pure-PHP implementations of SFTP.
  111. *
  112. * @package Net_SFTP
  113. * @author Jim Wigginton <terrafrost@php.net>
  114. * @access public
  115. */
  116. class Net_SFTP extends Net_SSH2
  117. {
  118. /**
  119. * Packet Types
  120. *
  121. * @see Net_SFTP::Net_SFTP()
  122. * @var Array
  123. * @access private
  124. */
  125. var $packet_types = array();
  126. /**
  127. * Status Codes
  128. *
  129. * @see Net_SFTP::Net_SFTP()
  130. * @var Array
  131. * @access private
  132. */
  133. var $status_codes = array();
  134. /**
  135. * The Request ID
  136. *
  137. * The request ID exists in the off chance that a packet is sent out-of-order. Of course, this library doesn't support
  138. * concurrent actions, so it's somewhat academic, here.
  139. *
  140. * @var Integer
  141. * @see Net_SFTP::_send_sftp_packet()
  142. * @access private
  143. */
  144. var $request_id = false;
  145. /**
  146. * The Packet Type
  147. *
  148. * The request ID exists in the off chance that a packet is sent out-of-order. Of course, this library doesn't support
  149. * concurrent actions, so it's somewhat academic, here.
  150. *
  151. * @var Integer
  152. * @see Net_SFTP::_get_sftp_packet()
  153. * @access private
  154. */
  155. var $packet_type = -1;
  156. /**
  157. * Packet Buffer
  158. *
  159. * @var String
  160. * @see Net_SFTP::_get_sftp_packet()
  161. * @access private
  162. */
  163. var $packet_buffer = '';
  164. /**
  165. * Extensions supported by the server
  166. *
  167. * @var Array
  168. * @see Net_SFTP::_initChannel()
  169. * @access private
  170. */
  171. var $extensions = array();
  172. /**
  173. * Server SFTP version
  174. *
  175. * @var Integer
  176. * @see Net_SFTP::_initChannel()
  177. * @access private
  178. */
  179. var $version;
  180. /**
  181. * Current working directory
  182. *
  183. * @var String
  184. * @see Net_SFTP::_realpath()
  185. * @see Net_SFTP::chdir()
  186. * @access private
  187. */
  188. var $pwd = false;
  189. /**
  190. * Packet Type Log
  191. *
  192. * @see Net_SFTP::getLog()
  193. * @var Array
  194. * @access private
  195. */
  196. var $packet_type_log = array();
  197. /**
  198. * Packet Log
  199. *
  200. * @see Net_SFTP::getLog()
  201. * @var Array
  202. * @access private
  203. */
  204. var $packet_log = array();
  205. /**
  206. * Error information
  207. *
  208. * @see Net_SFTP::getSFTPErrors()
  209. * @see Net_SFTP::getLastSFTPError()
  210. * @var String
  211. * @access private
  212. */
  213. var $sftp_errors = array();
  214. /**
  215. * Stat Cache
  216. *
  217. * Rather than always having to open a directory and close it immediately there after to see if a file is a directory
  218. * we'll cache the results.
  219. *
  220. * @see Net_SFTP::_update_stat_cache()
  221. * @see Net_SFTP::_remove_from_stat_cache()
  222. * @see Net_SFTP::_query_stat_cache()
  223. * @var Array
  224. * @access private
  225. */
  226. var $stat_cache = array();
  227. /**
  228. * Max SFTP Packet Size
  229. *
  230. * @see Net_SFTP::Net_SFTP()
  231. * @see Net_SFTP::get()
  232. * @var Array
  233. * @access private
  234. */
  235. var $max_sftp_packet;
  236. /**
  237. * Stat Cache Flag
  238. *
  239. * @see Net_SFTP::disableStatCache()
  240. * @see Net_SFTP::enableStatCache()
  241. * @var Boolean
  242. * @access private
  243. */
  244. var $use_stat_cache = true;
  245. /**
  246. * Sort Options
  247. *
  248. * @see Net_SFTP::_comparator()
  249. * @see Net_SFTP::setListOrder()
  250. * @var Array
  251. * @access private
  252. */
  253. var $sortOptions = array();
  254. /**
  255. * Default Constructor.
  256. *
  257. * Connects to an SFTP server
  258. *
  259. * @param String $host
  260. * @param optional Integer $port
  261. * @param optional Integer $timeout
  262. * @return Net_SFTP
  263. * @access public
  264. */
  265. function Net_SFTP($host, $port = 22, $timeout = 10)
  266. {
  267. parent::Net_SSH2($host, $port, $timeout);
  268. $this->max_sftp_packet = 1 << 15;
  269. $this->packet_types = array(
  270. 1 => 'NET_SFTP_INIT',
  271. 2 => 'NET_SFTP_VERSION',
  272. /* the format of SSH_FXP_OPEN changed between SFTPv4 and SFTPv5+:
  273. SFTPv5+: http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-8.1.1
  274. pre-SFTPv5 : http://tools.ietf.org/html/draft-ietf-secsh-filexfer-04#section-6.3 */
  275. 3 => 'NET_SFTP_OPEN',
  276. 4 => 'NET_SFTP_CLOSE',
  277. 5 => 'NET_SFTP_READ',
  278. 6 => 'NET_SFTP_WRITE',
  279. 7 => 'NET_SFTP_LSTAT',
  280. 9 => 'NET_SFTP_SETSTAT',
  281. 11 => 'NET_SFTP_OPENDIR',
  282. 12 => 'NET_SFTP_READDIR',
  283. 13 => 'NET_SFTP_REMOVE',
  284. 14 => 'NET_SFTP_MKDIR',
  285. 15 => 'NET_SFTP_RMDIR',
  286. 16 => 'NET_SFTP_REALPATH',
  287. 17 => 'NET_SFTP_STAT',
  288. /* the format of SSH_FXP_RENAME changed between SFTPv4 and SFTPv5+:
  289. SFTPv5+: http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-8.3
  290. pre-SFTPv5 : http://tools.ietf.org/html/draft-ietf-secsh-filexfer-04#section-6.5 */
  291. 18 => 'NET_SFTP_RENAME',
  292. 19 => 'NET_SFTP_READLINK',
  293. 20 => 'NET_SFTP_SYMLINK',
  294. 101=> 'NET_SFTP_STATUS',
  295. 102=> 'NET_SFTP_HANDLE',
  296. /* the format of SSH_FXP_NAME changed between SFTPv3 and SFTPv4+:
  297. SFTPv4+: http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-9.4
  298. pre-SFTPv4 : http://tools.ietf.org/html/draft-ietf-secsh-filexfer-02#section-7 */
  299. 103=> 'NET_SFTP_DATA',
  300. 104=> 'NET_SFTP_NAME',
  301. 105=> 'NET_SFTP_ATTRS',
  302. 200=> 'NET_SFTP_EXTENDED'
  303. );
  304. $this->status_codes = array(
  305. 0 => 'NET_SFTP_STATUS_OK',
  306. 1 => 'NET_SFTP_STATUS_EOF',
  307. 2 => 'NET_SFTP_STATUS_NO_SUCH_FILE',
  308. 3 => 'NET_SFTP_STATUS_PERMISSION_DENIED',
  309. 4 => 'NET_SFTP_STATUS_FAILURE',
  310. 5 => 'NET_SFTP_STATUS_BAD_MESSAGE',
  311. 6 => 'NET_SFTP_STATUS_NO_CONNECTION',
  312. 7 => 'NET_SFTP_STATUS_CONNECTION_LOST',
  313. 8 => 'NET_SFTP_STATUS_OP_UNSUPPORTED',
  314. 9 => 'NET_SFTP_STATUS_INVALID_HANDLE',
  315. 10 => 'NET_SFTP_STATUS_NO_SUCH_PATH',
  316. 11 => 'NET_SFTP_STATUS_FILE_ALREADY_EXISTS',
  317. 12 => 'NET_SFTP_STATUS_WRITE_PROTECT',
  318. 13 => 'NET_SFTP_STATUS_NO_MEDIA',
  319. 14 => 'NET_SFTP_STATUS_NO_SPACE_ON_FILESYSTEM',
  320. 15 => 'NET_SFTP_STATUS_QUOTA_EXCEEDED',
  321. 16 => 'NET_SFTP_STATUS_UNKNOWN_PRINCIPAL',
  322. 17 => 'NET_SFTP_STATUS_LOCK_CONFLICT',
  323. 18 => 'NET_SFTP_STATUS_DIR_NOT_EMPTY',
  324. 19 => 'NET_SFTP_STATUS_NOT_A_DIRECTORY',
  325. 20 => 'NET_SFTP_STATUS_INVALID_FILENAME',
  326. 21 => 'NET_SFTP_STATUS_LINK_LOOP',
  327. 22 => 'NET_SFTP_STATUS_CANNOT_DELETE',
  328. 23 => 'NET_SFTP_STATUS_INVALID_PARAMETER',
  329. 24 => 'NET_SFTP_STATUS_FILE_IS_A_DIRECTORY',
  330. 25 => 'NET_SFTP_STATUS_BYTE_RANGE_LOCK_CONFLICT',
  331. 26 => 'NET_SFTP_STATUS_BYTE_RANGE_LOCK_REFUSED',
  332. 27 => 'NET_SFTP_STATUS_DELETE_PENDING',
  333. 28 => 'NET_SFTP_STATUS_FILE_CORRUPT',
  334. 29 => 'NET_SFTP_STATUS_OWNER_INVALID',
  335. 30 => 'NET_SFTP_STATUS_GROUP_INVALID',
  336. 31 => 'NET_SFTP_STATUS_NO_MATCHING_BYTE_RANGE_LOCK'
  337. );
  338. // http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-7.1
  339. // the order, in this case, matters quite a lot - see Net_SFTP::_parseAttributes() to understand why
  340. $this->attributes = array(
  341. 0x00000001 => 'NET_SFTP_ATTR_SIZE',
  342. 0x00000002 => 'NET_SFTP_ATTR_UIDGID', // defined in SFTPv3, removed in SFTPv4+
  343. 0x00000004 => 'NET_SFTP_ATTR_PERMISSIONS',
  344. 0x00000008 => 'NET_SFTP_ATTR_ACCESSTIME',
  345. // 0x80000000 will yield a floating point on 32-bit systems and converting floating points to integers
  346. // yields inconsistent behavior depending on how php is compiled. so we left shift -1 (which, in
  347. // two's compliment, consists of all 1 bits) by 31. on 64-bit systems this'll yield 0xFFFFFFFF80000000.
  348. // that's not a problem, however, and 'anded' and a 32-bit number, as all the leading 1 bits are ignored.
  349. -1 << 31 => 'NET_SFTP_ATTR_EXTENDED'
  350. );
  351. // http://tools.ietf.org/html/draft-ietf-secsh-filexfer-04#section-6.3
  352. // the flag definitions change somewhat in SFTPv5+. if SFTPv5+ support is added to this library, maybe name
  353. // the array for that $this->open5_flags and similarily alter the constant names.
  354. $this->open_flags = array(
  355. 0x00000001 => 'NET_SFTP_OPEN_READ',
  356. 0x00000002 => 'NET_SFTP_OPEN_WRITE',
  357. 0x00000004 => 'NET_SFTP_OPEN_APPEND',
  358. 0x00000008 => 'NET_SFTP_OPEN_CREATE',
  359. 0x00000010 => 'NET_SFTP_OPEN_TRUNCATE',
  360. 0x00000020 => 'NET_SFTP_OPEN_EXCL'
  361. );
  362. // http://tools.ietf.org/html/draft-ietf-secsh-filexfer-04#section-5.2
  363. // see Net_SFTP::_parseLongname() for an explanation
  364. $this->file_types = array(
  365. 1 => 'NET_SFTP_TYPE_REGULAR',
  366. 2 => 'NET_SFTP_TYPE_DIRECTORY',
  367. 3 => 'NET_SFTP_TYPE_SYMLINK',
  368. 4 => 'NET_SFTP_TYPE_SPECIAL',
  369. 5 => 'NET_SFTP_TYPE_UNKNOWN',
  370. // the followin types were first defined for use in SFTPv5+
  371. // http://tools.ietf.org/html/draft-ietf-secsh-filexfer-05#section-5.2
  372. 6 => 'NET_SFTP_TYPE_SOCKET',
  373. 7 => 'NET_SFTP_TYPE_CHAR_DEVICE',
  374. 8 => 'NET_SFTP_TYPE_BLOCK_DEVICE',
  375. 9 => 'NET_SFTP_TYPE_FIFO'
  376. );
  377. $this->_define_array(
  378. $this->packet_types,
  379. $this->status_codes,
  380. $this->attributes,
  381. $this->open_flags,
  382. $this->file_types
  383. );
  384. if (!defined('NET_SFTP_QUEUE_SIZE')) {
  385. define('NET_SFTP_QUEUE_SIZE', 50);
  386. }
  387. }
  388. /**
  389. * Login
  390. *
  391. * @param String $username
  392. * @param optional String $password
  393. * @return Boolean
  394. * @access public
  395. */
  396. function login($username)
  397. {
  398. $args = func_get_args();
  399. if (!call_user_func_array(array(&$this, '_login'), $args)) {
  400. return false;
  401. }
  402. $this->window_size_server_to_client[NET_SFTP_CHANNEL] = $this->window_size;
  403. $packet = pack('CNa*N3',
  404. NET_SSH2_MSG_CHANNEL_OPEN, strlen('session'), 'session', NET_SFTP_CHANNEL, $this->window_size, 0x4000);
  405. if (!$this->_send_binary_packet($packet)) {
  406. return false;
  407. }
  408. $this->channel_status[NET_SFTP_CHANNEL] = NET_SSH2_MSG_CHANNEL_OPEN;
  409. $response = $this->_get_channel_packet(NET_SFTP_CHANNEL);
  410. if ($response === false) {
  411. return false;
  412. }
  413. $packet = pack('CNNa*CNa*',
  414. NET_SSH2_MSG_CHANNEL_REQUEST, $this->server_channels[NET_SFTP_CHANNEL], strlen('subsystem'), 'subsystem', 1, strlen('sftp'), 'sftp');
  415. if (!$this->_send_binary_packet($packet)) {
  416. return false;
  417. }
  418. $this->channel_status[NET_SFTP_CHANNEL] = NET_SSH2_MSG_CHANNEL_REQUEST;
  419. $response = $this->_get_channel_packet(NET_SFTP_CHANNEL);
  420. if ($response === false) {
  421. // from PuTTY's psftp.exe
  422. $command = "test -x /usr/lib/sftp-server && exec /usr/lib/sftp-server\n" .
  423. "test -x /usr/local/lib/sftp-server && exec /usr/local/lib/sftp-server\n" .
  424. "exec sftp-server";
  425. // we don't do $this->exec($command, false) because exec() operates on a different channel and plus the SSH_MSG_CHANNEL_OPEN that exec() does
  426. // is redundant
  427. $packet = pack('CNNa*CNa*',
  428. NET_SSH2_MSG_CHANNEL_REQUEST, $this->server_channels[NET_SFTP_CHANNEL], strlen('exec'), 'exec', 1, strlen($command), $command);
  429. if (!$this->_send_binary_packet($packet)) {
  430. return false;
  431. }
  432. $this->channel_status[NET_SFTP_CHANNEL] = NET_SSH2_MSG_CHANNEL_REQUEST;
  433. $response = $this->_get_channel_packet(NET_SFTP_CHANNEL);
  434. if ($response === false) {
  435. return false;
  436. }
  437. }
  438. $this->channel_status[NET_SFTP_CHANNEL] = NET_SSH2_MSG_CHANNEL_DATA;
  439. if (!$this->_send_sftp_packet(NET_SFTP_INIT, "\0\0\0\3")) {
  440. return false;
  441. }
  442. $response = $this->_get_sftp_packet();
  443. if ($this->packet_type != NET_SFTP_VERSION) {
  444. user_error('Expected SSH_FXP_VERSION');
  445. return false;
  446. }
  447. extract(unpack('Nversion', $this->_string_shift($response, 4)));
  448. $this->version = $version;
  449. while (!empty($response)) {
  450. extract(unpack('Nlength', $this->_string_shift($response, 4)));
  451. $key = $this->_string_shift($response, $length);
  452. extract(unpack('Nlength', $this->_string_shift($response, 4)));
  453. $value = $this->_string_shift($response, $length);
  454. $this->extensions[$key] = $value;
  455. }
  456. /*
  457. SFTPv4+ defines a 'newline' extension. SFTPv3 seems to have unofficial support for it via 'newline@vandyke.com',
  458. however, I'm not sure what 'newline@vandyke.com' is supposed to do (the fact that it's unofficial means that it's
  459. not in the official SFTPv3 specs) and 'newline@vandyke.com' / 'newline' are likely not drop-in substitutes for
  460. one another due to the fact that 'newline' comes with a SSH_FXF_TEXT bitmask whereas it seems unlikely that
  461. 'newline@vandyke.com' would.
  462. */
  463. /*
  464. if (isset($this->extensions['newline@vandyke.com'])) {
  465. $this->extensions['newline'] = $this->extensions['newline@vandyke.com'];
  466. unset($this->extensions['newline@vandyke.com']);
  467. }
  468. */
  469. $this->request_id = 1;
  470. /*
  471. A Note on SFTPv4/5/6 support:
  472. <http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-5.1> states the following:
  473. "If the client wishes to interoperate with servers that support noncontiguous version
  474. numbers it SHOULD send '3'"
  475. Given that the server only sends its version number after the client has already done so, the above
  476. seems to be suggesting that v3 should be the default version. This makes sense given that v3 is the
  477. most popular.
  478. <http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-5.5> states the following;
  479. "If the server did not send the "versions" extension, or the version-from-list was not included, the
  480. server MAY send a status response describing the failure, but MUST then close the channel without
  481. processing any further requests."
  482. So what do you do if you have a client whose initial SSH_FXP_INIT packet says it implements v3 and
  483. a server whose initial SSH_FXP_VERSION reply says it implements v4 and only v4? If it only implements
  484. v4, the "versions" extension is likely not going to have been sent so version re-negotiation as discussed
  485. in draft-ietf-secsh-filexfer-13 would be quite impossible. As such, what Net_SFTP would do is close the
  486. channel and reopen it with a new and updated SSH_FXP_INIT packet.
  487. */
  488. switch ($this->version) {
  489. case 2:
  490. case 3:
  491. break;
  492. default:
  493. return false;
  494. }
  495. $this->pwd = $this->_realpath('.');
  496. $this->_update_stat_cache($this->pwd, array());
  497. return true;
  498. }
  499. /**
  500. * Disable the stat cache
  501. *
  502. * @access public
  503. */
  504. function disableStatCache()
  505. {
  506. $this->use_stat_cache = false;
  507. }
  508. /**
  509. * Enable the stat cache
  510. *
  511. * @access public
  512. */
  513. function enableStatCache()
  514. {
  515. $this->use_stat_cache = true;
  516. }
  517. /**
  518. * Clear the stat cache
  519. *
  520. * @access public
  521. */
  522. function clearStatCache()
  523. {
  524. $this->stat_cache = array();
  525. }
  526. /**
  527. * Returns the current directory name
  528. *
  529. * @return Mixed
  530. * @access public
  531. */
  532. function pwd()
  533. {
  534. return $this->pwd;
  535. }
  536. /**
  537. * Logs errors
  538. *
  539. * @param String $response
  540. * @param optional Integer $status
  541. * @access public
  542. */
  543. function _logError($response, $status = -1)
  544. {
  545. if ($status == -1) {
  546. extract(unpack('Nstatus', $this->_string_shift($response, 4)));
  547. }
  548. $error = $this->status_codes[$status];
  549. if ($this->version > 2) {
  550. extract(unpack('Nlength', $this->_string_shift($response, 4)));
  551. $this->sftp_errors[] = $error . ': ' . $this->_string_shift($response, $length);
  552. } else {
  553. $this->sftp_errors[] = $error;
  554. }
  555. }
  556. /**
  557. * Canonicalize the Server-Side Path Name
  558. *
  559. * SFTP doesn't provide a mechanism by which the current working directory can be changed, so we'll emulate it. Returns
  560. * the absolute (canonicalized) path.
  561. *
  562. * @see Net_SFTP::chdir()
  563. * @param String $path
  564. * @return Mixed
  565. * @access private
  566. */
  567. function _realpath($path)
  568. {
  569. if ($this->pwd === false) {
  570. // http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-8.9
  571. if (!$this->_send_sftp_packet(NET_SFTP_REALPATH, pack('Na*', strlen($path), $path))) {
  572. return false;
  573. }
  574. $response = $this->_get_sftp_packet();
  575. switch ($this->packet_type) {
  576. case NET_SFTP_NAME:
  577. // although SSH_FXP_NAME is implemented differently in SFTPv3 than it is in SFTPv4+, the following
  578. // should work on all SFTP versions since the only part of the SSH_FXP_NAME packet the following looks
  579. // at is the first part and that part is defined the same in SFTP versions 3 through 6.
  580. $this->_string_shift($response, 4); // skip over the count - it should be 1, anyway
  581. extract(unpack('Nlength', $this->_string_shift($response, 4)));
  582. return $this->_string_shift($response, $length);
  583. case NET_SFTP_STATUS:
  584. $this->_logError($response);
  585. return false;
  586. default:
  587. user_error('Expected SSH_FXP_NAME or SSH_FXP_STATUS');
  588. return false;
  589. }
  590. }
  591. if ($path[0] != '/') {
  592. $path = $this->pwd . '/' . $path;
  593. }
  594. $path = explode('/', $path);
  595. $new = array();
  596. foreach ($path as $dir) {
  597. if (!strlen($dir)) {
  598. continue;
  599. }
  600. switch ($dir) {
  601. case '..':
  602. array_pop($new);
  603. case '.':
  604. break;
  605. default:
  606. $new[] = $dir;
  607. }
  608. }
  609. return '/' . implode('/', $new);
  610. }
  611. /**
  612. * Changes the current directory
  613. *
  614. * @param String $dir
  615. * @return Boolean
  616. * @access public
  617. */
  618. function chdir($dir)
  619. {
  620. if (!($this->bitmap & NET_SSH2_MASK_LOGIN)) {
  621. return false;
  622. }
  623. // assume current dir if $dir is empty
  624. if ($dir === '') {
  625. $dir = './';
  626. // suffix a slash if needed
  627. } elseif ($dir[strlen($dir) - 1] != '/') {
  628. $dir.= '/';
  629. }
  630. $dir = $this->_realpath($dir);
  631. // confirm that $dir is, in fact, a valid directory
  632. if ($this->use_stat_cache && is_array($this->_query_stat_cache($dir))) {
  633. $this->pwd = $dir;
  634. return true;
  635. }
  636. // we could do a stat on the alleged $dir to see if it's a directory but that doesn't tell us
  637. // the currently logged in user has the appropriate permissions or not. maybe you could see if
  638. // the file's uid / gid match the currently logged in user's uid / gid but how there's no easy
  639. // way to get those with SFTP
  640. if (!$this->_send_sftp_packet(NET_SFTP_OPENDIR, pack('Na*', strlen($dir), $dir))) {
  641. return false;
  642. }
  643. // see Net_SFTP::nlist() for a more thorough explanation of the following
  644. $response = $this->_get_sftp_packet();
  645. switch ($this->packet_type) {
  646. case NET_SFTP_HANDLE:
  647. $handle = substr($response, 4);
  648. break;
  649. case NET_SFTP_STATUS:
  650. $this->_logError($response);
  651. return false;
  652. default:
  653. user_error('Expected SSH_FXP_HANDLE or SSH_FXP_STATUS');
  654. return false;
  655. }
  656. if (!$this->_close_handle($handle)) {
  657. return false;
  658. }
  659. $this->_update_stat_cache($dir, array());
  660. $this->pwd = $dir;
  661. return true;
  662. }
  663. /**
  664. * Returns a list of files in the given directory
  665. *
  666. * @param optional String $dir
  667. * @param optional Boolean $recursive
  668. * @return Mixed
  669. * @access public
  670. */
  671. function nlist($dir = '.', $recursive = false)
  672. {
  673. return $this->_nlist_helper($dir, $recursive, '');
  674. }
  675. /**
  676. * Helper method for nlist
  677. *
  678. * @param String $dir
  679. * @param Boolean $recursive
  680. * @param String $relativeDir
  681. * @return Mixed
  682. * @access private
  683. */
  684. function _nlist_helper($dir, $recursive, $relativeDir)
  685. {
  686. $files = $this->_list($dir, false);
  687. if (!$recursive) {
  688. return $files;
  689. }
  690. $result = array();
  691. foreach ($files as $value) {
  692. if ($value == '.' || $value == '..') {
  693. if ($relativeDir == '') {
  694. $result[] = $value;
  695. }
  696. continue;
  697. }
  698. if (is_array($this->_query_stat_cache($this->_realpath($dir . '/' . $value)))) {
  699. $temp = $this->_nlist_helper($dir . '/' . $value, true, $relativeDir . $value . '/');
  700. $result = array_merge($result, $temp);
  701. } else {
  702. $result[] = $relativeDir . $value;
  703. }
  704. }
  705. return $result;
  706. }
  707. /**
  708. * Returns a detailed list of files in the given directory
  709. *
  710. * @param optional String $dir
  711. * @param optional Boolean $recursive
  712. * @return Mixed
  713. * @access public
  714. */
  715. function rawlist($dir = '.', $recursive = false)
  716. {
  717. $files = $this->_list($dir, true);
  718. if (!$recursive || $files === false) {
  719. return $files;
  720. }
  721. static $depth = 0;
  722. foreach ($files as $key=>$value) {
  723. if ($depth != 0 && $key == '..') {
  724. unset($files[$key]);
  725. continue;
  726. }
  727. if ($key != '.' && $key != '..' && is_array($this->_query_stat_cache($this->_realpath($dir . '/' . $key)))) {
  728. $depth++;
  729. $files[$key] = $this->rawlist($dir . '/' . $key, true);
  730. $depth--;
  731. } else {
  732. $files[$key] = (object) $value;
  733. }
  734. }
  735. return $files;
  736. }
  737. /**
  738. * Reads a list, be it detailed or not, of files in the given directory
  739. *
  740. * @param String $dir
  741. * @param optional Boolean $raw
  742. * @return Mixed
  743. * @access private
  744. */
  745. function _list($dir, $raw = true)
  746. {
  747. if (!($this->bitmap & NET_SSH2_MASK_LOGIN)) {
  748. return false;
  749. }
  750. $dir = $this->_realpath($dir . '/');
  751. if ($dir === false) {
  752. return false;
  753. }
  754. // http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-8.1.2
  755. if (!$this->_send_sftp_packet(NET_SFTP_OPENDIR, pack('Na*', strlen($dir), $dir))) {
  756. return false;
  757. }
  758. $response = $this->_get_sftp_packet();
  759. switch ($this->packet_type) {
  760. case NET_SFTP_HANDLE:
  761. // http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-9.2
  762. // since 'handle' is the last field in the SSH_FXP_HANDLE packet, we'll just remove the first four bytes that
  763. // represent the length of the string and leave it at that
  764. $handle = substr($response, 4);
  765. break;
  766. case NET_SFTP_STATUS:
  767. // presumably SSH_FX_NO_SUCH_FILE or SSH_FX_PERMISSION_DENIED
  768. $this->_logError($response);
  769. return false;
  770. default:
  771. user_error('Expected SSH_FXP_HANDLE or SSH_FXP_STATUS');
  772. return false;
  773. }
  774. $this->_update_stat_cache($dir, array());
  775. $contents = array();
  776. while (true) {
  777. // http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-8.2.2
  778. // why multiple SSH_FXP_READDIR packets would be sent when the response to a single one can span arbitrarily many
  779. // SSH_MSG_CHANNEL_DATA messages is not known to me.
  780. if (!$this->_send_sftp_packet(NET_SFTP_READDIR, pack('Na*', strlen($handle), $handle))) {
  781. return false;
  782. }
  783. $response = $this->_get_sftp_packet();
  784. switch ($this->packet_type) {
  785. case NET_SFTP_NAME:
  786. extract(unpack('Ncount', $this->_string_shift($response, 4)));
  787. for ($i = 0; $i < $count; $i++) {
  788. extract(unpack('Nlength', $this->_string_shift($response, 4)));
  789. $shortname = $this->_string_shift($response, $length);
  790. extract(unpack('Nlength', $this->_string_shift($response, 4)));
  791. $longname = $this->_string_shift($response, $length);
  792. $attributes = $this->_parseAttributes($response);
  793. if (!isset($attributes['type'])) {
  794. $fileType = $this->_parseLongname($longname);
  795. if ($fileType) {
  796. $attributes['type'] = $fileType;
  797. }
  798. }
  799. $contents[$shortname] = $attributes + array('filename' => $shortname);
  800. if (isset($attributes['type']) && $attributes['type'] == NET_SFTP_TYPE_DIRECTORY && ($shortname != '.' && $shortname != '..')) {
  801. $this->_update_stat_cache($dir . '/' . $shortname, array());
  802. } else {
  803. if ($shortname == '..') {
  804. $temp = $this->_realpath($dir . '/..') . '/.';
  805. } else {
  806. $temp = $dir . '/' . $shortname;
  807. }
  808. $this->_update_stat_cache($temp, (object) $attributes);
  809. }
  810. // SFTPv6 has an optional boolean end-of-list field, but we'll ignore that, since the
  811. // final SSH_FXP_STATUS packet should tell us that, already.
  812. }
  813. break;
  814. case NET_SFTP_STATUS:
  815. extract(unpack('Nstatus', $this->_string_shift($response, 4)));
  816. if ($status != NET_SFTP_STATUS_EOF) {
  817. $this->_logError($response, $status);
  818. return false;
  819. }
  820. break 2;
  821. default:
  822. user_error('Expected SSH_FXP_NAME or SSH_FXP_STATUS');
  823. return false;
  824. }
  825. }
  826. if (!$this->_close_handle($handle)) {
  827. return false;
  828. }
  829. if (count($this->sortOptions)) {
  830. uasort($contents, array(&$this, '_comparator'));
  831. }
  832. return $raw ? $contents : array_keys($contents);
  833. }
  834. /**
  835. * Compares two rawlist entries using parameters set by setListOrder()
  836. *
  837. * Intended for use with uasort()
  838. *
  839. * @param Array $a
  840. * @param Array $b
  841. * @return Integer
  842. * @access private
  843. */
  844. function _comparator($a, $b)
  845. {
  846. switch (true) {
  847. case $a['filename'] === '.' || $b['filename'] === '.':
  848. if ($a['filename'] === $b['filename']) {
  849. return 0;
  850. }
  851. return $a['filename'] === '.' ? -1 : 1;
  852. case $a['filename'] === '..' || $b['filename'] === '..':
  853. if ($a['filename'] === $b['filename']) {
  854. return 0;
  855. }
  856. return $a['filename'] === '..' ? -1 : 1;
  857. case isset($a['type']) && $a['type'] === NET_SFTP_TYPE_DIRECTORY:
  858. if (!isset($b['type'])) {
  859. return 1;
  860. }
  861. if ($b['type'] !== $a['type']) {
  862. return -1;
  863. }
  864. break;
  865. case isset($b['type']) && $b['type'] === NET_SFTP_TYPE_DIRECTORY:
  866. return 1;
  867. }
  868. foreach ($this->sortOptions as $sort => $order) {
  869. if (!isset($a[$sort]) || !isset($b[$sort])) {
  870. if (isset($a[$sort])) {
  871. return -1;
  872. }
  873. if (isset($b[$sort])) {
  874. return 1;
  875. }
  876. return 0;
  877. }
  878. switch ($sort) {
  879. case 'filename':
  880. $result = strcasecmp($a['filename'], $b['filename']);
  881. if ($result) {
  882. return $order === SORT_DESC ? -$result : $result;
  883. }
  884. break;
  885. case 'permissions':
  886. case 'mode':
  887. $a[$sort]&= 07777;
  888. $b[$sort]&= 07777;
  889. default:
  890. if ($a[$sort] === $b[$sort]) {
  891. break;
  892. }
  893. return $order === SORT_ASC ? $a[$sort] - $b[$sort] : $b[$sort] - $a[$sort];
  894. }
  895. }
  896. }
  897. /**
  898. * Defines how nlist() and rawlist() will be sorted - if at all.
  899. *
  900. * If sorting is enabled directories and files will be sorted independently with
  901. * directories appearing before files in the resultant array that is returned.
  902. *
  903. * Any parameter returned by stat is a valid sort parameter for this function.
  904. * Filename comparisons are case insensitive.
  905. *
  906. * Examples:
  907. *
  908. * $sftp->setListOrder('filename', SORT_ASC);
  909. * $sftp->setListOrder('size', SORT_DESC, 'filename', SORT_ASC);
  910. * $sftp->setListOrder(true);
  911. * Separates directories from files but doesn't do any sorting beyond that
  912. * $sftp->setListOrder();
  913. * Don't do any sort of sorting
  914. *
  915. * @access public
  916. */
  917. function setListOrder()
  918. {
  919. $this->sortOptions = array();
  920. $args = func_get_args();
  921. if (empty($args)) {
  922. return;
  923. }
  924. $len = count($args) & 0x7FFFFFFE;
  925. for ($i = 0; $i < $len; $i+=2) {
  926. $this->sortOptions[$args[$i]] = $args[$i + 1];
  927. }
  928. if (!count($this->sortOptions)) {
  929. $this->sortOptions = array('bogus' => true);
  930. }
  931. }
  932. /**
  933. * Returns the file size, in bytes, or false, on failure
  934. *
  935. * Files larger than 4GB will show up as being exactly 4GB.
  936. *
  937. * @param String $filename
  938. * @return Mixed
  939. * @access public
  940. */
  941. function size($filename)
  942. {
  943. if (!($this->bitmap & NET_SSH2_MASK_LOGIN)) {
  944. return false;
  945. }
  946. $result = $this->stat($filename);
  947. if ($result === false) {
  948. return false;
  949. }
  950. return isset($result['size']) ? $result['size'] : -1;
  951. }
  952. /**
  953. * Save files / directories to cache
  954. *
  955. * @param String $path
  956. * @param Mixed $value
  957. * @access private
  958. */
  959. function _update_stat_cache($path, $value)
  960. {
  961. // preg_replace('#^/|/(?=/)|/$#', '', $dir) == str_replace('//', '/', trim($path, '/'))
  962. $dirs = explode('/', preg_replace('#^/|/(?=/)|/$#', '', $path));
  963. $temp = &$this->stat_cache;
  964. $max = count($dirs) - 1;
  965. foreach ($dirs as $i=>$dir) {
  966. if (!isset($temp[$dir])) {
  967. $temp[$dir] = array();
  968. }
  969. if ($i === $max) {
  970. $temp[$dir] = $value;
  971. break;
  972. }
  973. $temp = &$temp[$dir];
  974. }
  975. }
  976. /**
  977. * Remove files / directories from cache
  978. *
  979. * @param String $path
  980. * @return Boolean
  981. * @access private
  982. */
  983. function _remove_from_stat_cache($path)
  984. {
  985. $dirs = explode('/', preg_replace('#^/|/(?=/)|/$#', '', $path));
  986. $temp = &$this->stat_cache;
  987. $max = count($dirs) - 1;
  988. foreach ($dirs as $i=>$dir) {
  989. if ($i === $max) {
  990. unset($temp[$dir]);
  991. return true;
  992. }
  993. if (!isset($temp[$dir])) {
  994. return false;
  995. }
  996. $temp = &$temp[$dir];
  997. }
  998. }
  999. /**
  1000. * Checks cache for path
  1001. *
  1002. * Mainly used by file_exists
  1003. *
  1004. * @param String $dir
  1005. * @return Mixed
  1006. * @access private
  1007. */
  1008. function _query_stat_cache($path)
  1009. {
  1010. $dirs = explode('/', preg_replace('#^/|/(?=/)|/$#', '', $path));
  1011. $temp = &$this->stat_cache;
  1012. foreach ($dirs as $dir) {
  1013. if (!isset($temp[$dir])) {
  1014. return null;
  1015. }
  1016. $temp = &$temp[$dir];
  1017. }
  1018. return $temp;
  1019. }
  1020. /**
  1021. * Returns general information about a file.
  1022. *
  1023. * Returns an array on success and false otherwise.
  1024. *
  1025. * @param String $filename
  1026. * @return Mixed
  1027. * @access public
  1028. */
  1029. function stat($filename)
  1030. {
  1031. if (!($this->bitmap & NET_SSH2_MASK_LOGIN)) {
  1032. return false;
  1033. }
  1034. $filename = $this->_realpath($filename);
  1035. if ($filename === false) {
  1036. return false;
  1037. }
  1038. if ($this->use_stat_cache) {
  1039. $result = $this->_query_stat_cache($filename);
  1040. if (is_array($result) && isset($result['.'])) {
  1041. return (array) $result['.'];
  1042. }
  1043. if (is_object($result)) {
  1044. return (array) $result;
  1045. }
  1046. }
  1047. $stat = $this->_stat($filename, NET_SFTP_STAT);
  1048. if ($stat === false) {
  1049. $this->_remove_from_stat_cache($filename);
  1050. return false;
  1051. }
  1052. if (isset($stat['type'])) {
  1053. if ($stat['type'] == NET_SFTP_TYPE_DIRECTORY) {
  1054. $filename.= '/.';
  1055. }
  1056. $this->_update_stat_cache($filename, (object) $stat);
  1057. return $stat;
  1058. }
  1059. $pwd = $this->pwd;
  1060. $stat['type'] = $this->chdir($filename) ?
  1061. NET_SFTP_TYPE_DIRECTORY :
  1062. NET_SFTP_TYPE_REGULAR;
  1063. $this->pwd = $pwd;
  1064. if ($stat['type'] == NET_SFTP_TYPE_DIRECTORY) {
  1065. $filename.= '/.';
  1066. }
  1067. $this->_update_stat_cache($filename, (object) $stat);
  1068. return $stat;
  1069. }
  1070. /**
  1071. * Returns general information about a file or symbolic link.
  1072. *
  1073. * Returns an array on success and false otherwise.
  1074. *
  1075. * @param String $filename
  1076. * @return Mixed
  1077. * @access public
  1078. */
  1079. function lstat($filename)
  1080. {
  1081. if (!($this->bitmap & NET_SSH2_MASK_LOGIN)) {
  1082. return false;
  1083. }
  1084. $filename = $this->_realpath($filename);
  1085. if ($filename === false) {
  1086. return false;
  1087. }
  1088. if ($this->use_stat_cache) {
  1089. $result = $this->_query_stat_cache($filename);
  1090. if (is_array($result) && isset($result['.'])) {
  1091. return (array) $result['.'];
  1092. }
  1093. if (is_object($result)) {
  1094. return (array) $result;
  1095. }
  1096. }
  1097. $lstat = $this->_stat($filename, NET_SFTP_LSTAT);
  1098. if ($lstat === false) {
  1099. $this->_remove_from_stat_cache($filename);
  1100. return false;
  1101. }
  1102. if (isset($lstat['type'])) {
  1103. if ($lstat['type'] == NET_SFTP_TYPE_DIRECTORY) {
  1104. $filename.= '/.';
  1105. }
  1106. $this->_update_stat_cache($filename, (object) $lstat);
  1107. return $lstat;
  1108. }
  1109. $stat = $this->_stat($filename, NET_SFTP_STAT);
  1110. if ($lstat != $stat) {
  1111. $lstat = array_merge($lstat, array('type' => NET_SFTP_TYPE_SYMLINK));
  1112. $this->_update_stat_cache($filename, (object) $lstat);
  1113. return $stat;
  1114. }
  1115. $pwd = $this->pwd;
  1116. $lstat['type'] = $this->chdir($filename) ?
  1117. NET_SFTP_TYPE_DIRECTORY :
  1118. NET_SFTP_TYPE_REGULAR;
  1119. $this->pwd = $pwd;
  1120. if ($lstat['type'] == NET_SFTP_TYPE_DIRECTORY) {
  1121. $filename.= '/.';
  1122. }
  1123. $this->_update_stat_cache($filename, (object) $lstat);
  1124. return $lstat;
  1125. }
  1126. /**
  1127. * Returns general information about a file or symbolic link
  1128. *
  1129. * Determines information without calling Net_SFTP::_realpath().
  1130. * The second parameter can be either NET_SFTP_STAT or NET_SFTP_LSTAT.
  1131. *
  1132. * @param String $filename
  1133. * @param Integer $type
  1134. * @return Mixed
  1135. * @access private
  1136. */
  1137. function _stat($filename, $type)
  1138. {
  1139. // SFTPv4+ adds an additional 32-bit integer field - flags - to the following:
  1140. $packet = pack('Na*', strlen($filename), $filename);
  1141. if (!$this->_send_sftp_packet($type, $packet)) {
  1142. return false;
  1143. }
  1144. $response = $this->_get_sftp_packet();
  1145. switch ($this->packet_type) {
  1146. case NET_SFTP_ATTRS:
  1147. return $this->_parseAttributes($response);
  1148. case NET_SFTP_STATUS:
  1149. $this->_logError($response);
  1150. return false;
  1151. }
  1152. user_error('Expected SSH_FXP_ATTRS or SSH_FXP_STATUS');
  1153. return false;
  1154. }
  1155. /**
  1156. * Truncates a file to a given length
  1157. *
  1158. * @param String $filename
  1159. * @param Integer $new_size
  1160. * @return Boolean
  1161. * @access public
  1162. */
  1163. function truncate($filename, $new_size)
  1164. {
  1165. $attr = pack('N3', NET_SFTP_ATTR_SIZE, $new_size / 4294967296, $new_size); // 4294967296 == 0x100000000 == 1<<32
  1166. return $this->_setstat($filename, $attr, false);
  1167. }
  1168. /**
  1169. * Sets access and modification time of file.
  1170. *
  1171. * If the file does not exist, it will be created.
  1172. *
  1173. * @param String $filename
  1174. * @param optional Integer $time
  1175. * @param optional Integer $atime
  1176. * @return Boolean
  1177. * @access public
  1178. */
  1179. function touch($filename, $time = null, $atime = null)
  1180. {
  1181. if (!($this->bitmap & NET_SSH2_MASK_LOGIN)) {
  1182. return false;
  1183. }
  1184. $filename = $this->_realpath($filename);
  1185. if ($filename === false) {
  1186. return false;
  1187. }
  1188. if (!isset($time)) {
  1189. $time = time();
  1190. }
  1191. if (!isset($atime)) {
  1192. $atime = $time;
  1193. }
  1194. $flags = NET_SFTP_OPEN_WRITE | NET_SFTP_OPEN_CREATE | NET_SFTP_OPEN_EXCL;
  1195. $attr = pack('N3', NET_SFTP_ATTR_ACCESSTIME, $time, $atime);
  1196. $packet = pack('Na*Na*', strlen($filename), $filename, $flags, $attr);
  1197. if (!$this->_send_sftp_packet(NET_SFTP_OPEN, $packet)) {
  1198. return false;
  1199. }
  1200. $response = $this->_get_sftp_packet();
  1201. switch ($this->packet_type) {
  1202. case NET_SFTP_HANDLE:
  1203. return $this->_close_handle(substr($response, 4));
  1204. case NET_SFTP_STATUS:
  1205. $this->_logError($response);
  1206. break;
  1207. default:
  1208. user_error('Expected SSH_FXP_HANDLE or SSH_FXP_STATUS');
  1209. return false;
  1210. }
  1211. return $this->_setstat($filename, $attr, false);
  1212. }
  1213. /**
  1214. * Changes file or directory owner
  1215. *
  1216. * Returns true on success or false on error.
  1217. *
  1218. * @param String $filename
  1219. * @param Integer $uid
  1220. * @param optional Boolean $recursive
  1221. * @return Boolean
  1222. * @access public
  1223. */
  1224. function chown($filename, $uid, $recursive = false)
  1225. {
  1226. // quoting from <http://www.kernel.org/doc/man-pages/online/pages/man2/chown.2.html>,
  1227. // "if the owner or group is specified as -1, then that ID is not changed"
  1228. $attr = pack('N3', NET_SFTP_ATTR_UIDGID, $uid, -1);
  1229. return $this->_setstat($filename, $attr, $recursive);
  1230. }
  1231. /**
  1232. * Changes file or directory group
  1233. *
  1234. * Returns true on success or false on error.
  1235. *
  1236. * @param String $filename
  1237. * @param Integer $gid
  1238. * @param optional Boolean $recursive
  1239. * @return Boolean
  1240. * @access public
  1241. */
  1242. function chgrp($filename, $gid, $recursive = false)
  1243. {
  1244. $attr = pack('N3', NET_SFTP_ATTR_UIDGID, -1, $gid);
  1245. return $this->_setstat($filename, $attr, $recursive);
  1246. }
  1247. /**
  1248. * Set permissions on a file.
  1249. *
  1250. * Returns the new file permissions on success or false on error.
  1251. * If $recursive is true than this just returns true or false.
  1252. *
  1253. * @param Integer $mode
  1254. * @param String $filename
  1255. * @param optional Boolean $recursive
  1256. * @return Mixed
  1257. * @access public
  1258. */
  1259. function chmod($mode, $filename, $recursive = false)
  1260. {
  1261. if (is_string($mode) && is_int($filename)) {
  1262. $temp = $mode;
  1263. $mode = $filename;
  1264. $filename = $temp;
  1265. }
  1266. $attr = pack('N2', NET_SFTP_ATTR_PERMISSIONS, $mode & 07777);
  1267. if (!$this->_setstat($filename, $attr, $recursive)) {
  1268. return false;
  1269. }
  1270. if ($recursive) {
  1271. return true;
  1272. }
  1273. // rather than return what the permissions *should* be, we'll return what they actually are. this will also
  1274. // tell us if the file actually exists.
  1275. // incidentally, SFTPv4+ adds an additional 32-bit integer field - flags - to the following:
  1276. $packet = pack('Na*', strlen($filename), $filename);
  1277. if (!$this->_send_sftp_packet(NET_SFTP_STAT, $packet)) {
  1278. return false;
  1279. }
  1280. $response = $this->_get_sftp_packet();
  1281. switch ($this->packet_type) {
  1282. case NET_SFTP_ATTRS:
  1283. $attrs = $this->_parseAttributes($response);
  1284. return $attrs['permissions'];
  1285. case NET_SFTP_STATUS:
  1286. $this->_logError($response);
  1287. return false;
  1288. }
  1289. user_error('Expected SSH_FXP_ATTRS or SSH_FXP_STATUS');
  1290. return false;
  1291. }
  1292. /**
  1293. * Sets information about a file
  1294. *
  1295. * @param String $filename
  1296. * @param String $attr
  1297. * @param Boolean $recursive
  1298. * @return Boolean
  1299. * @access private
  1300. */
  1301. function _setstat($filename, $attr, $recursive)
  1302. {
  1303. if (!($this->bitmap & NET_SSH2_MASK_LOGIN)) {
  1304. return false;
  1305. }
  1306. $filename = $this->_realpath($filename);
  1307. if ($filename === false) {
  1308. return false;
  1309. }
  1310. $this->_remove_from_stat_cache($filename);
  1311. if ($recursive) {
  1312. $i = 0;
  1313. $result = $this->_setstat_recursive($filename, $attr, $i);
  1314. $this->_read_put_responses($i);
  1315. return $result;
  1316. }
  1317. // SFTPv4+ has an additional byte field - type - that would need to be sent, as well. setting it to
  1318. // SSH_FILEXFER_TYPE_UNKNOWN might work. if not, we'd have to do an SSH_FXP_STAT before doing an SSH_FXP_SETSTAT.
  1319. if (!$this->_send_sftp_packet(NET_SFTP_SETSTAT, pack('Na*a*', strlen($filename), $filename, $attr))) {
  1320. return false;
  1321. }
  1322. /*
  1323. "Because some systems must use separate system calls to set various attributes, it is possible that a failure
  1324. response will be returned, but yet some of the attributes may be have been successfully modified. If possible,
  1325. servers SHOULD avoid this situation; however, clients MUST be aware that this is possible."
  1326. -- http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-8.6
  1327. */
  1328. $response = $this->_get_sftp_packet();
  1329. if ($this->packet_type != NET_SFTP_STATUS) {
  1330. user_error('Expected SSH_FXP_STATUS');
  1331. return false;
  1332. }
  1333. extract(unpack('Nstatus', $this->_string_shift($response, 4)));
  1334. if ($status != NET_SFTP_STATUS_OK) {
  1335. $this->_logError($response, $status);
  1336. return false;
  1337. }
  1338. return true;
  1339. }
  1340. /**
  1341. * Recursively sets information on directories on the SFTP server
  1342. *
  1343. * Minimizes directory lookups and SSH_FXP_STATUS requests for speed.
  1344. *
  1345. * @param String $path
  1346. * @param String $attr
  1347. * @param Integer $i
  1348. * @return Boolean
  1349. * @access private
  1350. */
  1351. function _setstat_recursive($path, $attr, &$i)
  1352. {
  1353. if (!$this->_read_put_responses($i)) {
  1354. return false;
  1355. }
  1356. $i = 0;
  1357. $entries = $this->_list($path, true);
  1358. if ($entries === false) {
  1359. return $this->_setstat($path, $attr, false);
  1360. }
  1361. // normally $entries would have at least . and .. but it might not if the directories
  1362. // permissions didn't allow reading
  1363. if (empty($entries)) {
  1364. return false;
  1365. }
  1366. unset($entries['.'], $entries['..']);
  1367. foreach ($entries as $filename=>$props) {
  1368. if (!isset($props['type'])) {
  1369. return false;
  1370. }
  1371. $temp = $path . '/' . $filename;
  1372. if ($props['type'] == NET_SFTP_TYPE_DIRECTORY) {
  1373. if (!$this->_setstat_recursive($temp, $attr, $i)) {
  1374. return false;
  1375. }
  1376. } else {
  1377. if (!$this->_send_sftp_packet(NET_SFTP_SETSTAT, pack('Na*a*', strlen($temp), $temp, $attr))) {
  1378. return false;
  1379. }
  1380. $i++;
  1381. if ($i >= NET_SFTP_QUEUE_SIZE) {
  1382. if (!$this->_read_put_responses($i)) {
  1383. return false;
  1384. }
  1385. $i = 0;
  1386. }
  1387. }
  1388. }
  1389. if (!$this->_send_sftp_packet(NET_SFTP_SETSTAT, pack('Na*a*', strlen($path), $path, $attr))) {
  1390. return false;
  1391. }
  1392. $i++;
  1393. if ($i >= NET_SFTP_QUEUE_SIZE) {
  1394. if (!$this->_read_put_responses($i)) {
  1395. return false;
  1396. }
  1397. $i = 0;
  1398. }
  1399. return true;
  1400. }
  1401. /**
  1402. * Return the target of a symbolic link
  1403. *
  1404. * @param String $link
  1405. * @return Mixed
  1406. * @access public
  1407. */
  1408. function readlink($link)
  1409. {
  1410. if (!($this->bitmap & NET_SSH2_MASK_LOGIN)) {
  1411. return false;
  1412. }
  1413. $link = $this->_realpath($link);
  1414. if (!$this->_send_sftp_packet(NET_SFTP_READLINK, pack('Na*', strlen($link), $link))) {
  1415. return false;
  1416. }
  1417. $response = $this->_get_sftp_packet();
  1418. switch ($this->packet_type) {
  1419. case NET_SFTP_NAME:
  1420. break;
  1421. case NET_SFTP_STATUS:
  1422. $this->_logError($response);
  1423. return false;
  1424. default:
  1425. user_error('Expected SSH_FXP_NAME or SSH_FXP_STATUS');
  1426. return false;
  1427. }
  1428. extract(unpack('Ncount', $this->_string_shift($response, 4)));
  1429. // the file isn't a symlink
  1430. if (!$count) {
  1431. return false;
  1432. }
  1433. extract(unpack('Nlength', $this->_string_shift($response, 4)));
  1434. return $this->_string_shift($response, $length);
  1435. }
  1436. /**
  1437. * Create a symlink
  1438. *
  1439. * symlink() creates a symbolic link to the existing target with the specified name link.
  1440. *
  1441. * @param String $target
  1442. * @param String $link
  1443. * @return Boolean
  1444. * @access public
  1445. */
  1446. function symlink($target, $link)
  1447. {
  1448. if (!($this->bitmap & NET_SSH2_MASK_LOGIN)) {
  1449. return false;
  1450. }
  1451. $target = $this->_realpath($target);
  1452. $link = $this->_realpath($link);
  1453. $packet = pack('Na*Na*', strlen($target), $target, strlen($link), $link);
  1454. if (!$this->_send_sftp_packet(NET_SFTP_SYMLINK, $packet)) {
  1455. return false;
  1456. }
  1457. $response = $this->_get_sftp_packet();
  1458. if ($this->packet_type != NET_SFTP_STATUS) {
  1459. user_error('Expected SSH_FXP_STATUS');
  1460. return false;
  1461. }
  1462. extract(unpack('Nstatus', $this->_string_shift($response, 4)));
  1463. if ($status != NET_SFTP_STATUS_OK) {
  1464. $this->_logError($response, $status);
  1465. return false;
  1466. }
  1467. return true;
  1468. }
  1469. /**
  1470. * Creates a directory.
  1471. *
  1472. * @param String $dir
  1473. * @return Boolean
  1474. * @access public
  1475. */
  1476. function mkdir($dir, $mode = -1, $recursive = false)
  1477. {
  1478. if (!($this->bitmap & NET_SSH2_MASK_LOGIN)) {
  1479. return false;
  1480. }
  1481. $dir = $this->_realpath($dir);
  1482. // by not providing any permissions, hopefully the server will use the logged in users umask - their
  1483. // default permissions.
  1484. $attr = $mode == -1 ? "\0\0\0\0" : pack('N2', NET_SFTP_ATTR_PERMISSIONS, $mode & 07777);
  1485. if ($recursive) {
  1486. $dirs = explode('/', preg_replace('#/(?=/)|/$#', '', $dir));
  1487. if (empty($dirs[0])) {
  1488. array_shift($dirs);
  1489. $dirs[0] = '/' . $dirs[0];
  1490. }
  1491. for ($i = 0; $i < count($dirs); $i++) {
  1492. $temp = array_slice($dirs, 0, $i + 1);
  1493. $temp = implode('/', $temp);
  1494. $result = $this->_mkdir_helper($temp, $attr);
  1495. }
  1496. return $result;
  1497. }
  1498. return $this->_mkdir_helper($dir, $attr);
  1499. }
  1500. /**
  1501. * Helper function for directory creation
  1502. *
  1503. * @param String $dir
  1504. * @return Boolean
  1505. * @access private
  1506. */
  1507. function _mkdir_helper($dir, $attr)
  1508. {
  1509. if (!$this->_send_sftp_packet(NET_SFTP_MKDIR, pack('Na*a*', strlen($dir), $dir, $attr))) {
  1510. return false;
  1511. }
  1512. $response = $this->_get_sftp_packet();
  1513. if ($this->packet_type != NET_SFTP_STATUS) {
  1514. user_error('Expected SSH_FXP_STATUS');
  1515. return false;
  1516. }
  1517. extract(unpack('Nstatus', $this->_string_shift($response, 4)));
  1518. if ($status != NET_SFTP_STATUS_OK) {
  1519. $this->_logError($response, $status);
  1520. return false;
  1521. }
  1522. return true;
  1523. }
  1524. /**
  1525. * Removes a directory.
  1526. *
  1527. * @param String $dir
  1528. * @return Boolean
  1529. * @access public
  1530. */
  1531. function rmdir($dir)
  1532. {
  1533. if (!($this->bitmap & NET_SSH2_MASK_LOGIN)) {
  1534. return false;
  1535. }
  1536. $dir = $this->_realpath($dir);
  1537. if ($dir === false) {
  1538. return false;
  1539. }
  1540. if (!$this->_send_sftp_packet(NET_SFTP_RMDIR, pack('Na*', strlen($dir), $dir))) {
  1541. return false;
  1542. }
  1543. $response = $this->_get_sftp_packet();
  1544. if ($this->packet_type != NET_SFTP_STATUS) {
  1545. user_error('Expected SSH_FXP_STATUS');
  1546. return false;
  1547. }
  1548. extract(unpack('Nstatus', $this->_string_shift($response, 4)));
  1549. if ($status != NET_SFTP_STATUS_OK) {
  1550. // presumably SSH_FX_NO_SUCH_FILE or SSH_FX_PERMISSION_DENIED?
  1551. $this->_logError($response, $status);
  1552. return false;
  1553. }
  1554. $this->_remove_from_stat_cache($dir);
  1555. // the following will do a soft delete, which would be useful if you deleted a file
  1556. // and then tried to do a stat on the deleted file. the above, in contrast, does
  1557. // a hard delete
  1558. //$this->_update_stat_cache($dir, false);
  1559. return true;
  1560. }
  1561. /**
  1562. * Uploads a file to the SFTP server.
  1563. *
  1564. * By default, Net_SFTP::put() does not read from the local filesystem. $data is dumped directly into $remote_file.
  1565. * So, for example, if you set $data to 'filename.ext' and then do Net_SFTP::get(), you will get a file, twelve bytes
  1566. * long, containing 'filename.ext' as its contents.
  1567. *
  1568. * Setting $mode to NET_SFTP_LOCAL_FILE will change the above behavior. With NET_SFTP_LOCAL_FILE, $remote_file will
  1569. * contain as many bytes as filename.ext does on your local filesystem. If your filename.ext is 1MB then that is how
  1570. * large $remote_file will be, as well.
  1571. *
  1572. * If $data is a resource then it'll be used as a resource instead.
  1573. *
  1574. * Currently, only binary mode is supported. As such, if the line endings need to be adjusted, you will need to take
  1575. * care of that, yourself.
  1576. *
  1577. * $mode can take an additional two parameters - NET_SFTP_RESUME and NET_SFTP_RESUME_START. These are bitwise AND'd with
  1578. * $mode. So if you want to resume upload of a 300mb file on the local file system you'd set $mode to the following:
  1579. *
  1580. * NET_SFTP_LOCAL_FILE | NET_SFTP_RESUME
  1581. *
  1582. * If you wanted to simply append the full contents of a local file to the full contents of a remote file you'd replace
  1583. * NET_SFTP_RESUME with NET_SFTP_RESUME_START.
  1584. *
  1585. * If $mode & (NET_SFTP_RESUME | NET_SFTP_RESUME_START) then NET_SFTP_RESUME_START will be assumed.
  1586. *
  1587. * $start and $local_start give you more fine grained control over this process and take precident over NET_SFTP_RESUME
  1588. * when they're non-negative. ie. $start could let you write at the end of a file (like NET_SFTP_RESUME) or in the middle
  1589. * of one. $local_start could let you start your reading from the end of a file (like NET_SFTP_RESUME_START) or in the
  1590. * middle of one.
  1591. *
  1592. * Setting $local_start to > 0 or $mode | NET_SFTP_RESUME_START doesn't do anything unless $mode | NET_SFTP_LOCAL_FILE.
  1593. *
  1594. * @param String $remote_file
  1595. * @param String|resource $data
  1596. * @param optional Integer $mode
  1597. * @param optional Integer $start
  1598. * @param optional Integer $local_start
  1599. * @return Boolean
  1600. * @access public
  1601. * @internal ASCII mode for SFTPv4/5/6 can be supported by adding a new function - Net_SFTP::setMode().
  1602. */
  1603. function put($remote_file, $data, $mode = NET_SFTP_STRING, $start = -1, $local_start = -1)
  1604. {
  1605. if (!($this->bitmap & NET_SSH2_MASK_LOGIN)) {
  1606. return false;
  1607. }
  1608. $remote_file = $this->_realpath($remote_file);
  1609. if ($remote_file === false) {
  1610. return false;
  1611. }
  1612. $this->_remove_from_stat_cache($remote_file);
  1613. $flags = NET_SFTP_OPEN_WRITE | NET_SFTP_OPEN_CREATE;
  1614. // according to the SFTP specs, NET_SFTP_OPEN_APPEND should "force all writes to append data at the end of the file."
  1615. // in practice, it doesn't seem to do that.
  1616. //$flags|= ($mode & NET_SFTP_RESUME) ? NET_SFTP_OPEN_APPEND : NET_SFTP_OPEN_TRUNCATE;
  1617. if ($start >= 0) {
  1618. $offset = $start;
  1619. } elseif ($mode & NET_SFTP_RESUME) {
  1620. // if NET_SFTP_OPEN_APPEND worked as it should _size() wouldn't need to be called
  1621. $size = $this->size($remote_file);
  1622. $offset = $size !== false ? $size : 0;
  1623. } else {
  1624. $offset = 0;
  1625. $flags|= NET_SFTP_OPEN_TRUNCATE;
  1626. }
  1627. $packet = pack('Na*N2', strlen($remote_file), $remote_file, $flags, 0);
  1628. if (!$this->_send_sftp_packet(NET_SFTP_OPEN, $packet)) {
  1629. return false;
  1630. }
  1631. $response = $this->_get_sftp_packet();
  1632. switch ($this->packet_type) {
  1633. case NET_SFTP_HANDLE:
  1634. $handle = substr($response, 4);
  1635. break;
  1636. case NET_SFTP_STATUS:
  1637. $this->_logError($response);
  1638. return false;
  1639. default:
  1640. user_error('Expected SSH_FXP_HANDLE or SSH_FXP_STATUS');
  1641. return false;
  1642. }
  1643. // http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-8.2.3
  1644. switch (true) {
  1645. case is_resource($data):
  1646. $mode = $mode & ~NET_SFTP_LOCAL_FILE;
  1647. $fp = $data;
  1648. break;
  1649. case $mode & NET_SFTP_LOCAL_FILE:
  1650. if (!is_file($data)) {
  1651. user_error("$data is not a valid file");
  1652. return false;
  1653. }
  1654. $fp = @fopen($data, 'rb');
  1655. if (!$fp) {
  1656. return false;
  1657. }
  1658. }
  1659. if (isset($fp)) {
  1660. $stat = fstat($fp);
  1661. $size = $stat['size'];
  1662. if ($local_start >= 0) {
  1663. fseek($fp, $local_start);
  1664. } elseif ($mode & NET_SFTP_RESUME_START) {
  1665. // do nothing
  1666. } else {
  1667. fseek($fp, $offset);
  1668. }
  1669. } else {
  1670. $size = strlen($data);
  1671. }
  1672. $sent = 0;
  1673. $size = $size < 0 ? ($size & 0x7FFFFFFF) + 0x80000000 : $size;
  1674. $sftp_packet_size = 4096; // PuTTY uses 4096
  1675. // make the SFTP packet be exactly 4096 bytes by including the bytes in the NET_SFTP_WRITE packets "header"
  1676. $sftp_packet_size-= strlen($handle) + 25;
  1677. $i = 0;
  1678. while ($sent < $size) {
  1679. $temp = isset($fp) ? fread($fp, $sftp_packet_size) : substr($data, $sent, $sftp_packet_size);
  1680. $subtemp = $offset + $sent;
  1681. $packet = pack('Na*N3a*', strlen($handle), $handle, $subtemp / 4294967296, $subtemp, strlen($temp), $temp);
  1682. if (!$this->_send_sftp_packet(NET_SFTP_WRITE, $packet)) {
  1683. if ($mode & NET_SFTP_LOCAL_FILE) {
  1684. fclose($fp);
  1685. }
  1686. return false;
  1687. }
  1688. $sent+= strlen($temp);
  1689. $i++;
  1690. if ($i == NET_SFTP_QUEUE_SIZE) {
  1691. if (!$this->_read_put_responses($i)) {
  1692. $i = 0;
  1693. break;
  1694. }
  1695. $i = 0;
  1696. }
  1697. }
  1698. if (!$this->_read_put_responses($i)) {
  1699. if ($mode & NET_SFTP_LOCAL_FILE) {
  1700. fclose($fp);
  1701. }
  1702. $this->_close_handle($handle);
  1703. return false;
  1704. }
  1705. if ($mode & NET_SFTP_LOCAL_FILE) {
  1706. fclose($fp);
  1707. }
  1708. return $this->_close_handle($handle);
  1709. }
  1710. /**
  1711. * Reads multiple successive SSH_FXP_WRITE responses
  1712. *
  1713. * Sending an SSH_FXP_WRITE packet and immediately reading its response isn't as efficient as blindly sending out $i
  1714. * SSH_FXP_WRITEs, in succession, and then reading $i responses.
  1715. *
  1716. * @param Integer $i
  1717. * @return Boolean
  1718. * @access private
  1719. */
  1720. function _read_put_responses($i)
  1721. {
  1722. while ($i--) {
  1723. $response = $this->_get_sftp_packet();
  1724. if ($this->packet_type != NET_SFTP_STATUS) {
  1725. user_error('Expected SSH_FXP_STATUS');
  1726. return false;
  1727. }
  1728. extract(unpack('Nstatus', $this->_string_shift($response, 4)));
  1729. if ($status != NET_SFTP_STATUS_OK) {
  1730. $this->_logError($response, $status);
  1731. break;
  1732. }
  1733. }
  1734. return $i < 0;
  1735. }
  1736. /**
  1737. * Close handle
  1738. *
  1739. * @param String $handle
  1740. * @return Boolean
  1741. * @access private
  1742. */
  1743. function _close_handle($handle)
  1744. {
  1745. if (!$this->_send_sftp_packet(NET_SFTP_CLOSE, pack('Na*', strlen($handle), $handle))) {
  1746. return false;
  1747. }
  1748. // "The client MUST release all resources associated with the handle regardless of the status."
  1749. // -- http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-8.1.3
  1750. $response = $this->_get_sftp_packet();
  1751. if ($this->packet_type != NET_SFTP_STATUS) {
  1752. user_error('Expected SSH_FXP_STATUS');
  1753. return false;
  1754. }
  1755. extract(unpack('Nstatus', $this->_string_shift($response, 4)));
  1756. if ($status != NET_SFTP_STATUS_OK) {
  1757. $this->_logError($response, $status);
  1758. return false;
  1759. }
  1760. return true;
  1761. }
  1762. /**
  1763. * Downloads a file from the SFTP server.
  1764. *
  1765. * Returns a string containing the contents of $remote_file if $local_file is left undefined or a boolean false if
  1766. * the operation was unsuccessful. If $local_file is defined, returns true or false depending on the success of the
  1767. * operation.
  1768. *
  1769. * $offset and $length can be used to download files in chunks.
  1770. *
  1771. * @param String $remote_file
  1772. * @param optional String $local_file
  1773. * @param optional Integer $offset
  1774. * @param optional Integer $length
  1775. * @return Mixed
  1776. * @access public
  1777. */
  1778. function get($remote_file, $local_file = false, $offset = 0, $length = -1)
  1779. {
  1780. if (!($this->bitmap & NET_SSH2_MASK_LOGIN)) {
  1781. return false;
  1782. }
  1783. $remote_file = $this->_realpath($remote_file);
  1784. if ($remote_file === false) {
  1785. return false;
  1786. }
  1787. $packet = pack('Na*N2', strlen($remote_file), $remote_file, NET_SFTP_OPEN_READ, 0);
  1788. if (!$this->_send_sftp_packet(NET_SFTP_OPEN, $packet)) {
  1789. return false;
  1790. }
  1791. $response = $this->_get_sftp_packet();
  1792. switch ($this->packet_type) {
  1793. case NET_SFTP_HANDLE:
  1794. $handle = substr($response, 4);
  1795. break;
  1796. case NET_SFTP_STATUS: // presumably SSH_FX_NO_SUCH_FILE or SSH_FX_PERMISSION_DENIED
  1797. $this->_logError($response);
  1798. return false;
  1799. default:
  1800. user_error('Expected SSH_FXP_HANDLE or SSH_FXP_STATUS');
  1801. return false;
  1802. }
  1803. if (is_resource($local_file)) {
  1804. $fp = $local_file;
  1805. $stat = fstat($fp);
  1806. $res_offset = $stat['size'];
  1807. } else {
  1808. $res_offset = 0;
  1809. if ($local_file !== false) {
  1810. $fp = fopen($local_file, 'wb');
  1811. if (!$fp) {
  1812. return false;
  1813. }
  1814. } else {
  1815. $content = '';
  1816. }
  1817. }
  1818. $fclose_check = $local_file !== false && !is_resource($local_file);
  1819. $start = $offset;
  1820. $size = $this->max_sftp_packet < $length || $length < 0 ? $this->max_sftp_packet : $length;
  1821. while (true) {
  1822. $packet = pack('Na*N3', strlen($handle), $handle, $offset / 4294967296, $offset, $size);
  1823. if (!$this->_send_sftp_packet(NET_SFTP_READ, $packet)) {
  1824. if ($fclose_check) {
  1825. fclose($fp);
  1826. }
  1827. return false;
  1828. }
  1829. $response = $this->_get_sftp_packet();
  1830. switch ($this->packet_type) {
  1831. case NET_SFTP_DATA:
  1832. $temp = substr($response, 4);
  1833. $offset+= strlen($temp);
  1834. if ($local_file === false) {
  1835. $content.= $temp;
  1836. } else {
  1837. fputs($fp, $temp);
  1838. }
  1839. break;
  1840. case NET_SFTP_STATUS:
  1841. // could, in theory, return false if !strlen($content) but we'll hold off for the time being
  1842. $this->_logError($response);
  1843. break 2;
  1844. default:
  1845. user_error('Expected SSH_FXP_DATA or SSH_FXP_STATUS');
  1846. if ($fclose_check) {
  1847. fclose($fp);
  1848. }
  1849. return false;
  1850. }
  1851. if ($length > 0 && $length <= $offset - $start) {
  1852. break;
  1853. }
  1854. }
  1855. if ($length > 0 && $length <= $offset - $start) {
  1856. if ($local_file === false) {
  1857. $content = substr($content, 0, $length);
  1858. } else {
  1859. ftruncate($fp, $length + $res_offset);
  1860. }
  1861. }
  1862. if ($fclose_check) {
  1863. fclose($fp);
  1864. }
  1865. if (!$this->_close_handle($handle)) {
  1866. return false;
  1867. }
  1868. // if $content isn't set that means a file was written to
  1869. return isset($content) ? $content : true;
  1870. }
  1871. /**
  1872. * Deletes a file on the SFTP server.
  1873. *
  1874. * @param String $path
  1875. * @param Boolean $recursive
  1876. * @return Boolean
  1877. * @access public
  1878. */
  1879. function delete($path, $recursive = true)
  1880. {
  1881. if (!($this->bitmap & NET_SSH2_MASK_LOGIN)) {
  1882. return false;
  1883. }
  1884. $path = $this->_realpath($path);
  1885. if ($path === false) {
  1886. return false;
  1887. }
  1888. // http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-8.3
  1889. if (!$this->_send_sftp_packet(NET_SFTP_REMOVE, pack('Na*', strlen($path), $path))) {
  1890. return false;
  1891. }
  1892. $response = $this->_get_sftp_packet();
  1893. if ($this->packet_type != NET_SFTP_STATUS) {
  1894. user_error('Expected SSH_FXP_STATUS');
  1895. return false;
  1896. }
  1897. // if $status isn't SSH_FX_OK it's probably SSH_FX_NO_SUCH_FILE or SSH_FX_PERMISSION_DENIED
  1898. extract(unpack('Nstatus', $this->_string_shift($response, 4)));
  1899. if ($status != NET_SFTP_STATUS_OK) {
  1900. $this->_logError($response, $status);
  1901. if (!$recursive) {
  1902. return false;
  1903. }
  1904. $i = 0;
  1905. $result = $this->_delete_recursive($path, $i);
  1906. $this->_read_put_responses($i);
  1907. return $result;
  1908. }
  1909. $this->_remove_from_stat_cache($path);
  1910. return true;
  1911. }
  1912. /**
  1913. * Recursively deletes directories on the SFTP server
  1914. *
  1915. * Minimizes directory lookups and SSH_FXP_STATUS requests for speed.
  1916. *
  1917. * @param String $path
  1918. * @param Integer $i
  1919. * @return Boolean
  1920. * @access private
  1921. */
  1922. function _delete_recursive($path, &$i)
  1923. {
  1924. if (!$this->_read_put_responses($i)) {
  1925. return false;
  1926. }
  1927. $i = 0;
  1928. $entries = $this->_list($path, true);
  1929. // normally $entries would have at least . and .. but it might not if the directories
  1930. // permissions didn't allow reading
  1931. if (empty($entries)) {
  1932. return false;
  1933. }
  1934. unset($entries['.'], $entries['..']);
  1935. foreach ($entries as $filename=>$props) {
  1936. if (!isset($props['type'])) {
  1937. return false;
  1938. }
  1939. $temp = $path . '/' . $filename;
  1940. if ($props['type'] == NET_SFTP_TYPE_DIRECTORY) {
  1941. if (!$this->_delete_recursive($temp, $i)) {
  1942. return false;
  1943. }
  1944. } else {
  1945. if (!$this->_send_sftp_packet(NET_SFTP_REMOVE, pack('Na*', strlen($temp), $temp))) {
  1946. return false;
  1947. }
  1948. $i++;
  1949. if ($i >= NET_SFTP_QUEUE_SIZE) {
  1950. if (!$this->_read_put_responses($i)) {
  1951. return false;
  1952. }
  1953. $i = 0;
  1954. }
  1955. }
  1956. $this->_remove_from_stat_cache($path);
  1957. }
  1958. if (!$this->_send_sftp_packet(NET_SFTP_RMDIR, pack('Na*', strlen($path), $path))) {
  1959. return false;
  1960. }
  1961. $i++;
  1962. if ($i >= NET_SFTP_QUEUE_SIZE) {
  1963. if (!$this->_read_put_responses($i)) {
  1964. return false;
  1965. }
  1966. $i = 0;
  1967. }
  1968. return true;
  1969. }
  1970. /**
  1971. * Checks whether a file or directory exists
  1972. *
  1973. * @param String $path
  1974. * @return Boolean
  1975. * @access public
  1976. */
  1977. function file_exists($path)
  1978. {
  1979. if ($this->use_stat_cache) {
  1980. $path = $this->_realpath($path);
  1981. $result = $this->_query_stat_cache($path);
  1982. if (isset($result)) {
  1983. // return true if $result is an array or if it's an stdClass object
  1984. return $result !== false;
  1985. }
  1986. }
  1987. return $this->stat($path) !== false;
  1988. }
  1989. /**
  1990. * Tells whether the filename is a directory
  1991. *
  1992. * @param String $path
  1993. * @return Boolean
  1994. * @access public
  1995. */
  1996. function is_dir($path)
  1997. {
  1998. $result = $this->_get_stat_cache_prop($path, 'type');
  1999. if ($result === false) {
  2000. return false;
  2001. }
  2002. return $result === NET_SFTP_TYPE_DIRECTORY;
  2003. }
  2004. /**
  2005. * Tells whether the filename is a regular file
  2006. *
  2007. * @param String $path
  2008. * @return Boolean
  2009. * @access public
  2010. */
  2011. function is_file($path)
  2012. {
  2013. $result = $this->_get_stat_cache_prop($path, 'type');
  2014. if ($result === false) {
  2015. return false;
  2016. }
  2017. return $result === NET_SFTP_TYPE_REGULAR;
  2018. }
  2019. /**
  2020. * Tells whether the filename is a symbolic link
  2021. *
  2022. * @param String $path
  2023. * @return Boolean
  2024. * @access public
  2025. */
  2026. function is_link($path)
  2027. {
  2028. $result = $this->_get_stat_cache_prop($path, 'type');
  2029. if ($result === false) {
  2030. return false;
  2031. }
  2032. return $result === NET_SFTP_TYPE_SYMLINK;
  2033. }
  2034. /**
  2035. * Gets last access time of file
  2036. *
  2037. * @param String $path
  2038. * @return Mixed
  2039. * @access public
  2040. */
  2041. function fileatime($path)
  2042. {
  2043. return $this->_get_stat_cache_prop($path, 'atime');
  2044. }
  2045. /**
  2046. * Gets file modification time
  2047. *
  2048. * @param String $path
  2049. * @return Mixed
  2050. * @access public
  2051. */
  2052. function filemtime($path)
  2053. {
  2054. return $this->_get_stat_cache_prop($path, 'mtime');
  2055. }
  2056. /**
  2057. * Gets file permissions
  2058. *
  2059. * @param String $path
  2060. * @return Mixed
  2061. * @access public
  2062. */
  2063. function fileperms($path)
  2064. {
  2065. return $this->_get_stat_cache_prop($path, 'permissions');
  2066. }
  2067. /**
  2068. * Gets file owner
  2069. *
  2070. * @param String $path
  2071. * @return Mixed
  2072. * @access public
  2073. */
  2074. function fileowner($path)
  2075. {
  2076. return $this->_get_stat_cache_prop($path, 'uid');
  2077. }
  2078. /**
  2079. * Gets file group
  2080. *
  2081. * @param String $path
  2082. * @return Mixed
  2083. * @access public
  2084. */
  2085. function filegroup($path)
  2086. {
  2087. return $this->_get_stat_cache_prop($path, 'gid');
  2088. }
  2089. /**
  2090. * Gets file size
  2091. *
  2092. * @param String $path
  2093. * @return Mixed
  2094. * @access public
  2095. */
  2096. function filesize($path)
  2097. {
  2098. return $this->_get_stat_cache_prop($path, 'size');
  2099. }
  2100. /**
  2101. * Gets file type
  2102. *
  2103. * @param String $path
  2104. * @return Mixed
  2105. * @access public
  2106. */
  2107. function filetype($path)
  2108. {
  2109. $type = $this->_get_stat_cache_prop($path, 'type');
  2110. if ($type === false) {
  2111. return false;
  2112. }
  2113. switch ($type) {
  2114. case NET_SFTP_TYPE_BLOCK_DEVICE: return 'block';
  2115. case NET_SFTP_TYPE_CHAR_DEVICE: return 'char';
  2116. case NET_SFTP_TYPE_DIRECTORY: return 'dir';
  2117. case NET_SFTP_TYPE_FIFO: return 'fifo';
  2118. case NET_SFTP_TYPE_REGULAR: return 'file';
  2119. case NET_SFTP_TYPE_SYMLINK: return 'link';
  2120. default: return false;
  2121. }
  2122. }
  2123. /**
  2124. * Return a stat properity
  2125. *
  2126. * Uses cache if appropriate.
  2127. *
  2128. * @param String $path
  2129. * @param String $prop
  2130. * @return Mixed
  2131. * @access private
  2132. */
  2133. function _get_stat_cache_prop($path, $prop)
  2134. {
  2135. if ($this->use_stat_cache) {
  2136. $path = $this->_realpath($path);
  2137. $result = $this->_query_stat_cache($path);
  2138. if (is_object($result) && isset($result->$prop)) {
  2139. return $result->$prop;
  2140. }
  2141. }
  2142. $result = $this->stat($path);
  2143. if ($result === false || !isset($result[$prop])) {
  2144. return false;
  2145. }
  2146. return $result[$prop];
  2147. }
  2148. /**
  2149. * Renames a file or a directory on the SFTP server
  2150. *
  2151. * @param String $oldname
  2152. * @param String $newname
  2153. * @return Boolean
  2154. * @access public
  2155. */
  2156. function rename($oldname, $newname)
  2157. {
  2158. if (!($this->bitmap & NET_SSH2_MASK_LOGIN)) {
  2159. return false;
  2160. }
  2161. $oldname = $this->_realpath($oldname);
  2162. $newname = $this->_realpath($newname);
  2163. if ($oldname === false || $newname === false) {
  2164. return false;
  2165. }
  2166. // http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-8.3
  2167. $packet = pack('Na*Na*', strlen($oldname), $oldname, strlen($newname), $newname);
  2168. if (!$this->_send_sftp_packet(NET_SFTP_RENAME, $packet)) {
  2169. return false;
  2170. }
  2171. $response = $this->_get_sftp_packet();
  2172. if ($this->packet_type != NET_SFTP_STATUS) {
  2173. user_error('Expected SSH_FXP_STATUS');
  2174. return false;
  2175. }
  2176. // if $status isn't SSH_FX_OK it's probably SSH_FX_NO_SUCH_FILE or SSH_FX_PERMISSION_DENIED
  2177. extract(unpack('Nstatus', $this->_string_shift($response, 4)));
  2178. if ($status != NET_SFTP_STATUS_OK) {
  2179. $this->_logError($response, $status);
  2180. return false;
  2181. }
  2182. // don't move the stat cache entry over since this operation could very well change the
  2183. // atime and mtime attributes
  2184. //$this->_update_stat_cache($newname, $this->_query_stat_cache($oldname));
  2185. $this->_remove_from_stat_cache($oldname);
  2186. $this->_remove_from_stat_cache($newname);
  2187. return true;
  2188. }
  2189. /**
  2190. * Parse Attributes
  2191. *
  2192. * See '7. File Attributes' of draft-ietf-secsh-filexfer-13 for more info.
  2193. *
  2194. * @param String $response
  2195. * @return Array
  2196. * @access private
  2197. */
  2198. function _parseAttributes(&$response)
  2199. {
  2200. $attr = array();
  2201. extract(unpack('Nflags', $this->_string_shift($response, 4)));
  2202. // SFTPv4+ have a type field (a byte) that follows the above flag field
  2203. foreach ($this->attributes as $key => $value) {
  2204. switch ($flags & $key) {
  2205. case NET_SFTP_ATTR_SIZE: // 0x00000001
  2206. // The size attribute is defined as an unsigned 64-bit integer.
  2207. // The following will use floats on 32-bit platforms, if necessary.
  2208. // As can be seen in the BigInteger class, floats are generally
  2209. // IEEE 754 binary64 "double precision" on such platforms and
  2210. // as such can represent integers of at least 2^50 without loss
  2211. // of precision. Interpreted in filesize, 2^50 bytes = 1024 TiB.
  2212. $attr['size'] = hexdec(bin2hex($this->_string_shift($response, 8)));
  2213. break;
  2214. case NET_SFTP_ATTR_UIDGID: // 0x00000002 (SFTPv3 only)
  2215. $attr+= unpack('Nuid/Ngid', $this->_string_shift($response, 8));
  2216. break;
  2217. case NET_SFTP_ATTR_PERMISSIONS: // 0x00000004
  2218. $attr+= unpack('Npermissions', $this->_string_shift($response, 4));
  2219. // mode == permissions; permissions was the original array key and is retained for bc purposes.
  2220. // mode was added because that's the more industry standard terminology
  2221. $attr+= array('mode' => $attr['permissions']);
  2222. $fileType = $this->_parseMode($attr['permissions']);
  2223. if ($fileType !== false) {
  2224. $attr+= array('type' => $fileType);
  2225. }
  2226. break;
  2227. case NET_SFTP_ATTR_ACCESSTIME: // 0x00000008
  2228. $attr+= unpack('Natime/Nmtime', $this->_string_shift($response, 8));
  2229. break;
  2230. case NET_SFTP_ATTR_EXTENDED: // 0x80000000
  2231. extract(unpack('Ncount', $this->_string_shift($response, 4)));
  2232. for ($i = 0; $i < $count; $i++) {
  2233. extract(unpack('Nlength', $this->_string_shift($response, 4)));
  2234. $key = $this->_string_shift($response, $length);
  2235. extract(unpack('Nlength', $this->_string_shift($response, 4)));
  2236. $attr[$key] = $this->_string_shift($response, $length);
  2237. }
  2238. }
  2239. }
  2240. return $attr;
  2241. }
  2242. /**
  2243. * Attempt to identify the file type
  2244. *
  2245. * Quoting the SFTP RFC, "Implementations MUST NOT send bits that are not defined" but they seem to anyway
  2246. *
  2247. * @param Integer $mode
  2248. * @return Integer
  2249. * @access private
  2250. */
  2251. function _parseMode($mode)
  2252. {
  2253. // values come from http://lxr.free-electrons.com/source/include/uapi/linux/stat.h#L12
  2254. // see, also, http://linux.die.net/man/2/stat
  2255. switch ($mode & 0170000) {// ie. 1111 0000 0000 0000
  2256. case 0000000: // no file type specified - figure out the file type using alternative means
  2257. return false;
  2258. case 0040000:
  2259. return NET_SFTP_TYPE_DIRECTORY;
  2260. case 0100000:
  2261. return NET_SFTP_TYPE_REGULAR;
  2262. case 0120000:
  2263. return NET_SFTP_TYPE_SYMLINK;
  2264. // new types introduced in SFTPv5+
  2265. // http://tools.ietf.org/html/draft-ietf-secsh-filexfer-05#section-5.2
  2266. case 0010000: // named pipe (fifo)
  2267. return NET_SFTP_TYPE_FIFO;
  2268. case 0020000: // character special
  2269. return NET_SFTP_TYPE_CHAR_DEVICE;
  2270. case 0060000: // block special
  2271. return NET_SFTP_TYPE_BLOCK_DEVICE;
  2272. case 0140000: // socket
  2273. return NET_SFTP_TYPE_SOCKET;
  2274. case 0160000: // whiteout
  2275. // "SPECIAL should be used for files that are of
  2276. // a known type which cannot be expressed in the protocol"
  2277. return NET_SFTP_TYPE_SPECIAL;
  2278. default:
  2279. return NET_SFTP_TYPE_UNKNOWN;
  2280. }
  2281. }
  2282. /**
  2283. * Parse Longname
  2284. *
  2285. * SFTPv3 doesn't provide any easy way of identifying a file type. You could try to open
  2286. * a file as a directory and see if an error is returned or you could try to parse the
  2287. * SFTPv3-specific longname field of the SSH_FXP_NAME packet. That's what this function does.
  2288. * The result is returned using the
  2289. * {@link http://tools.ietf.org/html/draft-ietf-secsh-filexfer-04#section-5.2 SFTPv4 type constants}.
  2290. *
  2291. * If the longname is in an unrecognized format bool(false) is returned.
  2292. *
  2293. * @param String $longname
  2294. * @return Mixed
  2295. * @access private
  2296. */
  2297. function _parseLongname($longname)
  2298. {
  2299. // http://en.wikipedia.org/wiki/Unix_file_types
  2300. // http://en.wikipedia.org/wiki/Filesystem_permissions#Notation_of_traditional_Unix_permissions
  2301. if (preg_match('#^[^/]([r-][w-][xstST-]){3}#', $longname)) {
  2302. switch ($longname[0]) {
  2303. case '-':
  2304. return NET_SFTP_TYPE_REGULAR;
  2305. case 'd':
  2306. return NET_SFTP_TYPE_DIRECTORY;
  2307. case 'l':
  2308. return NET_SFTP_TYPE_SYMLINK;
  2309. default:
  2310. return NET_SFTP_TYPE_SPECIAL;
  2311. }
  2312. }
  2313. return false;
  2314. }
  2315. /**
  2316. * Sends SFTP Packets
  2317. *
  2318. * See '6. General Packet Format' of draft-ietf-secsh-filexfer-13 for more info.
  2319. *
  2320. * @param Integer $type
  2321. * @param String $data
  2322. * @see Net_SFTP::_get_sftp_packet()
  2323. * @see Net_SSH2::_send_channel_packet()
  2324. * @return Boolean
  2325. * @access private
  2326. */
  2327. function _send_sftp_packet($type, $data)
  2328. {
  2329. $packet = $this->request_id !== false ?
  2330. pack('NCNa*', strlen($data) + 5, $type, $this->request_id, $data) :
  2331. pack('NCa*', strlen($data) + 1, $type, $data);
  2332. $start = strtok(microtime(), ' ') + strtok(''); // http://php.net/microtime#61838
  2333. $result = $this->_send_channel_packet(NET_SFTP_CHANNEL, $packet);
  2334. $stop = strtok(microtime(), ' ') + strtok('');
  2335. if (defined('NET_SFTP_LOGGING')) {
  2336. $packet_type = '-> ' . $this->packet_types[$type] .
  2337. ' (' . round($stop - $start, 4) . 's)';
  2338. if (NET_SFTP_LOGGING == NET_SFTP_LOG_REALTIME) {
  2339. echo "<pre>\r\n" . $this->_format_log(array($data), array($packet_type)) . "\r\n</pre>\r\n";
  2340. flush();
  2341. ob_flush();
  2342. } else {
  2343. $this->packet_type_log[] = $packet_type;
  2344. if (NET_SFTP_LOGGING == NET_SFTP_LOG_COMPLEX) {
  2345. $this->packet_log[] = $data;
  2346. }
  2347. }
  2348. }
  2349. return $result;
  2350. }
  2351. /**
  2352. * Receives SFTP Packets
  2353. *
  2354. * See '6. General Packet Format' of draft-ietf-secsh-filexfer-13 for more info.
  2355. *
  2356. * Incidentally, the number of SSH_MSG_CHANNEL_DATA messages has no bearing on the number of SFTP packets present.
  2357. * There can be one SSH_MSG_CHANNEL_DATA messages containing two SFTP packets or there can be two SSH_MSG_CHANNEL_DATA
  2358. * messages containing one SFTP packet.
  2359. *
  2360. * @see Net_SFTP::_send_sftp_packet()
  2361. * @return String
  2362. * @access private
  2363. */
  2364. function _get_sftp_packet()
  2365. {
  2366. $this->curTimeout = false;
  2367. $start = strtok(microtime(), ' ') + strtok(''); // http://php.net/microtime#61838
  2368. // SFTP packet length
  2369. while (strlen($this->packet_buffer) < 4) {
  2370. $temp = $this->_get_channel_packet(NET_SFTP_CHANNEL);
  2371. if (is_bool($temp)) {
  2372. $this->packet_type = false;
  2373. $this->packet_buffer = '';
  2374. return false;
  2375. }
  2376. $this->packet_buffer.= $temp;
  2377. }
  2378. extract(unpack('Nlength', $this->_string_shift($this->packet_buffer, 4)));
  2379. $tempLength = $length;
  2380. $tempLength-= strlen($this->packet_buffer);
  2381. // SFTP packet type and data payload
  2382. while ($tempLength > 0) {
  2383. $temp = $this->_get_channel_packet(NET_SFTP_CHANNEL);
  2384. if (is_bool($temp)) {
  2385. $this->packet_type = false;
  2386. $this->packet_buffer = '';
  2387. return false;
  2388. }
  2389. $this->packet_buffer.= $temp;
  2390. $tempLength-= strlen($temp);
  2391. }
  2392. $stop = strtok(microtime(), ' ') + strtok('');
  2393. $this->packet_type = ord($this->_string_shift($this->packet_buffer));
  2394. if ($this->request_id !== false) {
  2395. $this->_string_shift($this->packet_buffer, 4); // remove the request id
  2396. $length-= 5; // account for the request id and the packet type
  2397. } else {
  2398. $length-= 1; // account for the packet type
  2399. }
  2400. $packet = $this->_string_shift($this->packet_buffer, $length);
  2401. if (defined('NET_SFTP_LOGGING')) {
  2402. $packet_type = '<- ' . $this->packet_types[$this->packet_type] .
  2403. ' (' . round($stop - $start, 4) . 's)';
  2404. if (NET_SFTP_LOGGING == NET_SFTP_LOG_REALTIME) {
  2405. echo "<pre>\r\n" . $this->_format_log(array($packet), array($packet_type)) . "\r\n</pre>\r\n";
  2406. flush();
  2407. ob_flush();
  2408. } else {
  2409. $this->packet_type_log[] = $packet_type;
  2410. if (NET_SFTP_LOGGING == NET_SFTP_LOG_COMPLEX) {
  2411. $this->packet_log[] = $packet;
  2412. }
  2413. }
  2414. }
  2415. return $packet;
  2416. }
  2417. /**
  2418. * Returns a log of the packets that have been sent and received.
  2419. *
  2420. * Returns a string if NET_SFTP_LOGGING == NET_SFTP_LOG_COMPLEX, an array if NET_SFTP_LOGGING == NET_SFTP_LOG_SIMPLE and false if !defined('NET_SFTP_LOGGING')
  2421. *
  2422. * @access public
  2423. * @return String or Array
  2424. */
  2425. function getSFTPLog()
  2426. {
  2427. if (!defined('NET_SFTP_LOGGING')) {
  2428. return false;
  2429. }
  2430. switch (NET_SFTP_LOGGING) {
  2431. case NET_SFTP_LOG_COMPLEX:
  2432. return $this->_format_log($this->packet_log, $this->packet_type_log);
  2433. break;
  2434. //case NET_SFTP_LOG_SIMPLE:
  2435. default:
  2436. return $this->packet_type_log;
  2437. }
  2438. }
  2439. /**
  2440. * Returns all errors
  2441. *
  2442. * @return String
  2443. * @access public
  2444. */
  2445. function getSFTPErrors()
  2446. {
  2447. return $this->sftp_errors;
  2448. }
  2449. /**
  2450. * Returns the last error
  2451. *
  2452. * @return String
  2453. * @access public
  2454. */
  2455. function getLastSFTPError()
  2456. {
  2457. return count($this->sftp_errors) ? $this->sftp_errors[count($this->sftp_errors) - 1] : '';
  2458. }
  2459. /**
  2460. * Get supported SFTP versions
  2461. *
  2462. * @return Array
  2463. * @access public
  2464. */
  2465. function getSupportedVersions()
  2466. {
  2467. $temp = array('version' => $this->version);
  2468. if (isset($this->extensions['versions'])) {
  2469. $temp['extensions'] = $this->extensions['versions'];
  2470. }
  2471. return $temp;
  2472. }
  2473. /**
  2474. * Disconnect
  2475. *
  2476. * @param Integer $reason
  2477. * @return Boolean
  2478. * @access private
  2479. */
  2480. function _disconnect($reason)
  2481. {
  2482. $this->pwd = false;
  2483. parent::_disconnect($reason);
  2484. }
  2485. }