class-wp-filesystem-ssh2.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  1. <?php
  2. /**
  3. * WordPress Filesystem Class for implementing SSH2
  4. *
  5. * To use this class you must follow these steps for PHP 5.2.6+
  6. *
  7. * @contrib http://kevin.vanzonneveld.net/techblog/article/make_ssh_connections_with_php/ - Installation Notes
  8. *
  9. * Complie libssh2 (Note: Only 0.14 is officaly working with PHP 5.2.6+ right now, But many users have found the latest versions work)
  10. *
  11. * cd /usr/src
  12. * wget http://surfnet.dl.sourceforge.net/sourceforge/libssh2/libssh2-0.14.tar.gz
  13. * tar -zxvf libssh2-0.14.tar.gz
  14. * cd libssh2-0.14/
  15. * ./configure
  16. * make all install
  17. *
  18. * Note: Do not leave the directory yet!
  19. *
  20. * Enter: pecl install -f ssh2
  21. *
  22. * Copy the ssh.so file it creates to your PHP Module Directory.
  23. * Open up your PHP.INI file and look for where extensions are placed.
  24. * Add in your PHP.ini file: extension=ssh2.so
  25. *
  26. * Restart Apache!
  27. * Check phpinfo() streams to confirm that: ssh2.shell, ssh2.exec, ssh2.tunnel, ssh2.scp, ssh2.sftp exist.
  28. *
  29. * Note: as of WordPress 2.8, This utilises the PHP5+ function 'stream_get_contents'
  30. *
  31. * @since 2.7.0
  32. *
  33. * @package WordPress
  34. * @subpackage Filesystem
  35. */
  36. class WP_Filesystem_SSH2 extends WP_Filesystem_Base {
  37. /**
  38. * @since 2.7.0
  39. * @var resource
  40. */
  41. public $link = false;
  42. /**
  43. * @since 2.7.0
  44. * @var resource
  45. */
  46. public $sftp_link;
  47. /**
  48. * @since 2.7.0
  49. * @var bool
  50. */
  51. public $keys = false;
  52. /**
  53. * Constructor.
  54. *
  55. * @since 2.7.0
  56. *
  57. * @param array $opt
  58. */
  59. public function __construct( $opt = '' ) {
  60. $this->method = 'ssh2';
  61. $this->errors = new WP_Error();
  62. //Check if possible to use ssh2 functions.
  63. if ( ! extension_loaded( 'ssh2' ) ) {
  64. $this->errors->add( 'no_ssh2_ext', __( 'The ssh2 PHP extension is not available' ) );
  65. return;
  66. }
  67. if ( ! function_exists( 'stream_get_contents' ) ) {
  68. $this->errors->add(
  69. 'ssh2_php_requirement',
  70. sprintf(
  71. /* translators: %s: stream_get_contents() */
  72. __( 'The ssh2 PHP extension is available, however, we require the PHP5 function %s' ),
  73. '<code>stream_get_contents()</code>'
  74. )
  75. );
  76. return;
  77. }
  78. // Set defaults:
  79. if ( empty( $opt['port'] ) ) {
  80. $this->options['port'] = 22;
  81. } else {
  82. $this->options['port'] = $opt['port'];
  83. }
  84. if ( empty( $opt['hostname'] ) ) {
  85. $this->errors->add( 'empty_hostname', __( 'SSH2 hostname is required' ) );
  86. } else {
  87. $this->options['hostname'] = $opt['hostname'];
  88. }
  89. // Check if the options provided are OK.
  90. if ( ! empty( $opt['public_key'] ) && ! empty( $opt['private_key'] ) ) {
  91. $this->options['public_key'] = $opt['public_key'];
  92. $this->options['private_key'] = $opt['private_key'];
  93. $this->options['hostkey'] = array( 'hostkey' => 'ssh-rsa' );
  94. $this->keys = true;
  95. } elseif ( empty( $opt['username'] ) ) {
  96. $this->errors->add( 'empty_username', __( 'SSH2 username is required' ) );
  97. }
  98. if ( ! empty( $opt['username'] ) ) {
  99. $this->options['username'] = $opt['username'];
  100. }
  101. if ( empty( $opt['password'] ) ) {
  102. // Password can be blank if we are using keys.
  103. if ( ! $this->keys ) {
  104. $this->errors->add( 'empty_password', __( 'SSH2 password is required' ) );
  105. }
  106. } else {
  107. $this->options['password'] = $opt['password'];
  108. }
  109. }
  110. /**
  111. * Connects filesystem.
  112. *
  113. * @since 2.7.0
  114. *
  115. * @return bool True on success, false on failure.
  116. */
  117. public function connect() {
  118. if ( ! $this->keys ) {
  119. $this->link = @ssh2_connect( $this->options['hostname'], $this->options['port'] );
  120. } else {
  121. $this->link = @ssh2_connect( $this->options['hostname'], $this->options['port'], $this->options['hostkey'] );
  122. }
  123. if ( ! $this->link ) {
  124. $this->errors->add(
  125. 'connect',
  126. sprintf(
  127. /* translators: %s: hostname:port */
  128. __( 'Failed to connect to SSH2 Server %s' ),
  129. $this->options['hostname'] . ':' . $this->options['port']
  130. )
  131. );
  132. return false;
  133. }
  134. if ( ! $this->keys ) {
  135. if ( ! @ssh2_auth_password( $this->link, $this->options['username'], $this->options['password'] ) ) {
  136. $this->errors->add(
  137. 'auth',
  138. sprintf(
  139. /* translators: %s: Username. */
  140. __( 'Username/Password incorrect for %s' ),
  141. $this->options['username']
  142. )
  143. );
  144. return false;
  145. }
  146. } else {
  147. if ( ! @ssh2_auth_pubkey_file( $this->link, $this->options['username'], $this->options['public_key'], $this->options['private_key'], $this->options['password'] ) ) {
  148. $this->errors->add(
  149. 'auth',
  150. sprintf(
  151. /* translators: %s: Username. */
  152. __( 'Public and Private keys incorrect for %s' ),
  153. $this->options['username']
  154. )
  155. );
  156. return false;
  157. }
  158. }
  159. $this->sftp_link = ssh2_sftp( $this->link );
  160. if ( ! $this->sftp_link ) {
  161. $this->errors->add(
  162. 'connect',
  163. sprintf(
  164. /* translators: %s: hostname:port */
  165. __( 'Failed to initialize a SFTP subsystem session with the SSH2 Server %s' ),
  166. $this->options['hostname'] . ':' . $this->options['port']
  167. )
  168. );
  169. return false;
  170. }
  171. return true;
  172. }
  173. /**
  174. * Gets the ssh2.sftp PHP stream wrapper path to open for the given file.
  175. *
  176. * This method also works around a PHP bug where the root directory (/) cannot
  177. * be opened by PHP functions, causing a false failure. In order to work around
  178. * this, the path is converted to /./ which is semantically the same as /
  179. * See https://bugs.php.net/bug.php?id=64169 for more details.
  180. *
  181. * @since 4.4.0
  182. *
  183. * @param string $path The File/Directory path on the remote server to return
  184. * @return string The ssh2.sftp:// wrapped path to use.
  185. */
  186. public function sftp_path( $path ) {
  187. if ( '/' === $path ) {
  188. $path = '/./';
  189. }
  190. return 'ssh2.sftp://' . $this->sftp_link . '/' . ltrim( $path, '/' );
  191. }
  192. /**
  193. * @since 2.7.0
  194. *
  195. * @param string $command
  196. * @param bool $returnbool
  197. * @return bool|string True on success, false on failure. String if the command was executed, `$returnbool`
  198. * is false (default), and data from the resulting stream was retrieved.
  199. */
  200. public function run_command( $command, $returnbool = false ) {
  201. if ( ! $this->link ) {
  202. return false;
  203. }
  204. $stream = ssh2_exec( $this->link, $command );
  205. if ( ! $stream ) {
  206. $this->errors->add(
  207. 'command',
  208. sprintf(
  209. /* translators: %s: Command. */
  210. __( 'Unable to perform command: %s' ),
  211. $command
  212. )
  213. );
  214. } else {
  215. stream_set_blocking( $stream, true );
  216. stream_set_timeout( $stream, FS_TIMEOUT );
  217. $data = stream_get_contents( $stream );
  218. fclose( $stream );
  219. if ( $returnbool ) {
  220. return ( $data === false ) ? false : '' != trim( $data );
  221. } else {
  222. return $data;
  223. }
  224. }
  225. return false;
  226. }
  227. /**
  228. * Reads entire file into a string.
  229. *
  230. * @since 2.7.0
  231. *
  232. * @param string $file Name of the file to read.
  233. * @return string|false Read data on success, false if no temporary file could be opened,
  234. * or if the file couldn't be retrieved.
  235. */
  236. public function get_contents( $file ) {
  237. return file_get_contents( $this->sftp_path( $file ) );
  238. }
  239. /**
  240. * Reads entire file into an array.
  241. *
  242. * @since 2.7.0
  243. *
  244. * @param string $file Path to the file.
  245. * @return array|false File contents in an array on success, false on failure.
  246. */
  247. public function get_contents_array( $file ) {
  248. return file( $this->sftp_path( $file ) );
  249. }
  250. /**
  251. * Writes a string to a file.
  252. *
  253. * @since 2.7.0
  254. *
  255. * @param string $file Remote path to the file where to write the data.
  256. * @param string $contents The data to write.
  257. * @param int|false $mode Optional. The file permissions as octal number, usually 0644.
  258. * Default false.
  259. * @return bool True on success, false on failure.
  260. */
  261. public function put_contents( $file, $contents, $mode = false ) {
  262. $ret = file_put_contents( $this->sftp_path( $file ), $contents );
  263. if ( $ret !== strlen( $contents ) ) {
  264. return false;
  265. }
  266. $this->chmod( $file, $mode );
  267. return true;
  268. }
  269. /**
  270. * Gets the current working directory.
  271. *
  272. * @since 2.7.0
  273. *
  274. * @return string|false The current working directory on success, false on failure.
  275. */
  276. public function cwd() {
  277. $cwd = ssh2_sftp_realpath( $this->sftp_link, '.' );
  278. if ( $cwd ) {
  279. $cwd = trailingslashit( trim( $cwd ) );
  280. }
  281. return $cwd;
  282. }
  283. /**
  284. * Changes current directory.
  285. *
  286. * @since 2.7.0
  287. *
  288. * @param string $dir The new current directory.
  289. * @return bool True on success, false on failure.
  290. */
  291. public function chdir( $dir ) {
  292. return $this->run_command( 'cd ' . $dir, true );
  293. }
  294. /**
  295. * Changes the file group.
  296. *
  297. * @since 2.7.0
  298. *
  299. * @param string $file Path to the file.
  300. * @param string|int $group A group name or number.
  301. * @param bool $recursive Optional. If set to true, changes file group recursively.
  302. * Default false.
  303. * @return bool True on success, false on failure.
  304. */
  305. public function chgrp( $file, $group, $recursive = false ) {
  306. if ( ! $this->exists( $file ) ) {
  307. return false;
  308. }
  309. if ( ! $recursive || ! $this->is_dir( $file ) ) {
  310. return $this->run_command( sprintf( 'chgrp %s %s', escapeshellarg( $group ), escapeshellarg( $file ) ), true );
  311. }
  312. return $this->run_command( sprintf( 'chgrp -R %s %s', escapeshellarg( $group ), escapeshellarg( $file ) ), true );
  313. }
  314. /**
  315. * Changes filesystem permissions.
  316. *
  317. * @since 2.7.0
  318. *
  319. * @param string $file Path to the file.
  320. * @param int|false $mode Optional. The permissions as octal number, usually 0644 for files,
  321. * 0755 for directories. Default false.
  322. * @param bool $recursive Optional. If set to true, changes file group recursively.
  323. * Default false.
  324. * @return bool True on success, false on failure.
  325. */
  326. public function chmod( $file, $mode = false, $recursive = false ) {
  327. if ( ! $this->exists( $file ) ) {
  328. return false;
  329. }
  330. if ( ! $mode ) {
  331. if ( $this->is_file( $file ) ) {
  332. $mode = FS_CHMOD_FILE;
  333. } elseif ( $this->is_dir( $file ) ) {
  334. $mode = FS_CHMOD_DIR;
  335. } else {
  336. return false;
  337. }
  338. }
  339. if ( ! $recursive || ! $this->is_dir( $file ) ) {
  340. return $this->run_command( sprintf( 'chmod %o %s', $mode, escapeshellarg( $file ) ), true );
  341. }
  342. return $this->run_command( sprintf( 'chmod -R %o %s', $mode, escapeshellarg( $file ) ), true );
  343. }
  344. /**
  345. * Changes the owner of a file or directory.
  346. *
  347. * @since 2.7.0
  348. *
  349. * @param string $file Path to the file or directory.
  350. * @param string|int $owner A user name or number.
  351. * @param bool $recursive Optional. If set to true, changes file owner recursively.
  352. * Default false.
  353. * @return bool True on success, false on failure.
  354. */
  355. public function chown( $file, $owner, $recursive = false ) {
  356. if ( ! $this->exists( $file ) ) {
  357. return false;
  358. }
  359. if ( ! $recursive || ! $this->is_dir( $file ) ) {
  360. return $this->run_command( sprintf( 'chown %s %s', escapeshellarg( $owner ), escapeshellarg( $file ) ), true );
  361. }
  362. return $this->run_command( sprintf( 'chown -R %s %s', escapeshellarg( $owner ), escapeshellarg( $file ) ), true );
  363. }
  364. /**
  365. * Gets the file owner.
  366. *
  367. * @since 2.7.0
  368. *
  369. * @param string $file Path to the file.
  370. * @return string|false Username of the owner on success, false on failure.
  371. */
  372. public function owner( $file ) {
  373. $owneruid = @fileowner( $this->sftp_path( $file ) );
  374. if ( ! $owneruid ) {
  375. return false;
  376. }
  377. if ( ! function_exists( 'posix_getpwuid' ) ) {
  378. return $owneruid;
  379. }
  380. $ownerarray = posix_getpwuid( $owneruid );
  381. return $ownerarray['name'];
  382. }
  383. /**
  384. * Gets the permissions of the specified file or filepath in their octal format.
  385. *
  386. * @since 2.7.0
  387. *
  388. * @param string $file Path to the file.
  389. * @return string Mode of the file (the last 3 digits).
  390. */
  391. public function getchmod( $file ) {
  392. return substr( decoct( @fileperms( $this->sftp_path( $file ) ) ), -3 );
  393. }
  394. /**
  395. * Gets the file's group.
  396. *
  397. * @since 2.7.0
  398. *
  399. * @param string $file Path to the file.
  400. * @return string|false The group on success, false on failure.
  401. */
  402. public function group( $file ) {
  403. $gid = @filegroup( $this->sftp_path( $file ) );
  404. if ( ! $gid ) {
  405. return false;
  406. }
  407. if ( ! function_exists( 'posix_getgrgid' ) ) {
  408. return $gid;
  409. }
  410. $grouparray = posix_getgrgid( $gid );
  411. return $grouparray['name'];
  412. }
  413. /**
  414. * Copies a file.
  415. *
  416. * @since 2.7.0
  417. *
  418. * @param string $source Path to the source file.
  419. * @param string $destination Path to the destination file.
  420. * @param bool $overwrite Optional. Whether to overwrite the destination file if it exists.
  421. * Default false.
  422. * @param int|false $mode Optional. The permissions as octal number, usually 0644 for files,
  423. * 0755 for dirs. Default false.
  424. * @return bool True on success, false on failure.
  425. */
  426. public function copy( $source, $destination, $overwrite = false, $mode = false ) {
  427. if ( ! $overwrite && $this->exists( $destination ) ) {
  428. return false;
  429. }
  430. $content = $this->get_contents( $source );
  431. if ( false === $content ) {
  432. return false;
  433. }
  434. return $this->put_contents( $destination, $content, $mode );
  435. }
  436. /**
  437. * Moves a file.
  438. *
  439. * @since 2.7.0
  440. *
  441. * @param string $source Path to the source file.
  442. * @param string $destination Path to the destination file.
  443. * @param bool $overwrite Optional. Whether to overwrite the destination file if it exists.
  444. * Default false.
  445. * @return bool True on success, false on failure.
  446. */
  447. public function move( $source, $destination, $overwrite = false ) {
  448. if ( $this->exists( $destination ) ) {
  449. if ( $overwrite ) {
  450. // We need to remove the destination file before we can rename the source.
  451. $this->delete( $destination, false, 'f' );
  452. } else {
  453. // If we're not overwriting, the rename will fail, so return early.
  454. return false;
  455. }
  456. }
  457. return ssh2_sftp_rename( $this->sftp_link, $source, $destination );
  458. }
  459. /**
  460. * Deletes a file or directory.
  461. *
  462. * @since 2.7.0
  463. *
  464. * @param string $file Path to the file or directory.
  465. * @param bool $recursive Optional. If set to true, changes file group recursively.
  466. * Default false.
  467. * @param string|false $type Type of resource. 'f' for file, 'd' for directory.
  468. * Default false.
  469. * @return bool True on success, false on failure.
  470. */
  471. public function delete( $file, $recursive = false, $type = false ) {
  472. if ( 'f' == $type || $this->is_file( $file ) ) {
  473. return ssh2_sftp_unlink( $this->sftp_link, $file );
  474. }
  475. if ( ! $recursive ) {
  476. return ssh2_sftp_rmdir( $this->sftp_link, $file );
  477. }
  478. $filelist = $this->dirlist( $file );
  479. if ( is_array( $filelist ) ) {
  480. foreach ( $filelist as $filename => $fileinfo ) {
  481. $this->delete( $file . '/' . $filename, $recursive, $fileinfo['type'] );
  482. }
  483. }
  484. return ssh2_sftp_rmdir( $this->sftp_link, $file );
  485. }
  486. /**
  487. * Checks if a file or directory exists.
  488. *
  489. * @since 2.7.0
  490. *
  491. * @param string $file Path to file or directory.
  492. * @return bool Whether $file exists or not.
  493. */
  494. public function exists( $file ) {
  495. return file_exists( $this->sftp_path( $file ) );
  496. }
  497. /**
  498. * Checks if resource is a file.
  499. *
  500. * @since 2.7.0
  501. *
  502. * @param string $file File path.
  503. * @return bool Whether $file is a file.
  504. */
  505. public function is_file( $file ) {
  506. return is_file( $this->sftp_path( $file ) );
  507. }
  508. /**
  509. * Checks if resource is a directory.
  510. *
  511. * @since 2.7.0
  512. *
  513. * @param string $path Directory path.
  514. * @return bool Whether $path is a directory.
  515. */
  516. public function is_dir( $path ) {
  517. return is_dir( $this->sftp_path( $path ) );
  518. }
  519. /**
  520. * Checks if a file is readable.
  521. *
  522. * @since 2.7.0
  523. *
  524. * @param string $file Path to file.
  525. * @return bool Whether $file is readable.
  526. */
  527. public function is_readable( $file ) {
  528. return is_readable( $this->sftp_path( $file ) );
  529. }
  530. /**
  531. * Checks if a file or directory is writable.
  532. *
  533. * @since 2.7.0
  534. *
  535. * @param string $file Path to file or directory.
  536. * @return bool Whether $file is writable.
  537. */
  538. public function is_writable( $file ) {
  539. // PHP will base it's writable checks on system_user === file_owner, not ssh_user === file_owner
  540. return true;
  541. }
  542. /**
  543. * Gets the file's last access time.
  544. *
  545. * @since 2.7.0
  546. *
  547. * @param string $file Path to file.
  548. * @return int|false Unix timestamp representing last access time, false on failure.
  549. */
  550. public function atime( $file ) {
  551. return fileatime( $this->sftp_path( $file ) );
  552. }
  553. /**
  554. * Gets the file modification time.
  555. *
  556. * @since 2.7.0
  557. *
  558. * @param string $file Path to file.
  559. * @return int|false Unix timestamp representing modification time, false on failure.
  560. */
  561. public function mtime( $file ) {
  562. return filemtime( $this->sftp_path( $file ) );
  563. }
  564. /**
  565. * Gets the file size (in bytes).
  566. *
  567. * @since 2.7.0
  568. *
  569. * @param string $file Path to file.
  570. * @return int|false Size of the file in bytes on success, false on failure.
  571. */
  572. public function size( $file ) {
  573. return filesize( $this->sftp_path( $file ) );
  574. }
  575. /**
  576. * Sets the access and modification times of a file.
  577. *
  578. * Note: Not implemented.
  579. *
  580. * @since 2.7.0
  581. *
  582. * @param string $file Path to file.
  583. * @param int $time Optional. Modified time to set for file.
  584. * Default 0.
  585. * @param int $atime Optional. Access time to set for file.
  586. * Default 0.
  587. */
  588. public function touch( $file, $time = 0, $atime = 0 ) {
  589. // Not implemented.
  590. }
  591. /**
  592. * Creates a directory.
  593. *
  594. * @since 2.7.0
  595. *
  596. * @param string $path Path for new directory.
  597. * @param int|false $chmod Optional. The permissions as octal number (or false to skip chmod).
  598. * Default false.
  599. * @param string|int $chown Optional. A user name or number (or false to skip chown).
  600. * Default false.
  601. * @param string|int $chgrp Optional. A group name or number (or false to skip chgrp).
  602. * Default false.
  603. * @return bool True on success, false on failure.
  604. */
  605. public function mkdir( $path, $chmod = false, $chown = false, $chgrp = false ) {
  606. $path = untrailingslashit( $path );
  607. if ( empty( $path ) ) {
  608. return false;
  609. }
  610. if ( ! $chmod ) {
  611. $chmod = FS_CHMOD_DIR;
  612. }
  613. if ( ! ssh2_sftp_mkdir( $this->sftp_link, $path, $chmod, true ) ) {
  614. return false;
  615. }
  616. if ( $chown ) {
  617. $this->chown( $path, $chown );
  618. }
  619. if ( $chgrp ) {
  620. $this->chgrp( $path, $chgrp );
  621. }
  622. return true;
  623. }
  624. /**
  625. * Deletes a directory.
  626. *
  627. * @since 2.7.0
  628. *
  629. * @param string $path Path to directory.
  630. * @param bool $recursive Optional. Whether to recursively remove files/directories.
  631. * Default false.
  632. * @return bool True on success, false on failure.
  633. */
  634. public function rmdir( $path, $recursive = false ) {
  635. return $this->delete( $path, $recursive );
  636. }
  637. /**
  638. * Gets details for files in a directory or a specific file.
  639. *
  640. * @since 2.7.0
  641. *
  642. * @param string $path Path to directory or file.
  643. * @param bool $include_hidden Optional. Whether to include details of hidden ("." prefixed) files.
  644. * Default true.
  645. * @param bool $recursive Optional. Whether to recursively include file details in nested directories.
  646. * Default false.
  647. * @return array|false {
  648. * Array of files. False if unable to list directory contents.
  649. *
  650. * @type string $name Name of the file or directory.
  651. * @type string $perms *nix representation of permissions.
  652. * @type int $permsn Octal representation of permissions.
  653. * @type string $owner Owner name or ID.
  654. * @type int $size Size of file in bytes.
  655. * @type int $lastmodunix Last modified unix timestamp.
  656. * @type mixed $lastmod Last modified month (3 letter) and day (without leading 0).
  657. * @type int $time Last modified time.
  658. * @type string $type Type of resource. 'f' for file, 'd' for directory.
  659. * @type mixed $files If a directory and $recursive is true, contains another array of files.
  660. * }
  661. */
  662. public function dirlist( $path, $include_hidden = true, $recursive = false ) {
  663. if ( $this->is_file( $path ) ) {
  664. $limit_file = basename( $path );
  665. $path = dirname( $path );
  666. } else {
  667. $limit_file = false;
  668. }
  669. if ( ! $this->is_dir( $path ) || ! $this->is_readable( $path ) ) {
  670. return false;
  671. }
  672. $ret = array();
  673. $dir = dir( $this->sftp_path( $path ) );
  674. if ( ! $dir ) {
  675. return false;
  676. }
  677. while ( false !== ( $entry = $dir->read() ) ) {
  678. $struc = array();
  679. $struc['name'] = $entry;
  680. if ( '.' == $struc['name'] || '..' == $struc['name'] ) {
  681. continue; //Do not care about these folders.
  682. }
  683. if ( ! $include_hidden && '.' == $struc['name'][0] ) {
  684. continue;
  685. }
  686. if ( $limit_file && $struc['name'] != $limit_file ) {
  687. continue;
  688. }
  689. $struc['perms'] = $this->gethchmod( $path . '/' . $entry );
  690. $struc['permsn'] = $this->getnumchmodfromh( $struc['perms'] );
  691. $struc['number'] = false;
  692. $struc['owner'] = $this->owner( $path . '/' . $entry );
  693. $struc['group'] = $this->group( $path . '/' . $entry );
  694. $struc['size'] = $this->size( $path . '/' . $entry );
  695. $struc['lastmodunix'] = $this->mtime( $path . '/' . $entry );
  696. $struc['lastmod'] = gmdate( 'M j', $struc['lastmodunix'] );
  697. $struc['time'] = gmdate( 'h:i:s', $struc['lastmodunix'] );
  698. $struc['type'] = $this->is_dir( $path . '/' . $entry ) ? 'd' : 'f';
  699. if ( 'd' == $struc['type'] ) {
  700. if ( $recursive ) {
  701. $struc['files'] = $this->dirlist( $path . '/' . $struc['name'], $include_hidden, $recursive );
  702. } else {
  703. $struc['files'] = array();
  704. }
  705. }
  706. $ret[ $struc['name'] ] = $struc;
  707. }
  708. $dir->close();
  709. unset( $dir );
  710. return $ret;
  711. }
  712. }