class-wp-filesystem-ssh2.php 22 KB

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