class-wp-filesystem-direct.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. <?php
  2. /**
  3. * WordPress Direct Filesystem.
  4. *
  5. * @package WordPress
  6. * @subpackage Filesystem
  7. */
  8. /**
  9. * WordPress Filesystem Class for direct PHP file and folder manipulation.
  10. *
  11. * @since 2.5.0
  12. *
  13. * @see WP_Filesystem_Base
  14. */
  15. class WP_Filesystem_Direct extends WP_Filesystem_Base {
  16. /**
  17. * Constructor.
  18. *
  19. * @since 2.5.0
  20. *
  21. * @param mixed $arg Not used.
  22. */
  23. public function __construct( $arg ) {
  24. $this->method = 'direct';
  25. $this->errors = new WP_Error();
  26. }
  27. /**
  28. * Reads entire file into a string.
  29. *
  30. * @since 2.5.0
  31. *
  32. * @param string $file Name of the file to read.
  33. * @return string|false Read data on success, false on failure.
  34. */
  35. public function get_contents( $file ) {
  36. return @file_get_contents( $file );
  37. }
  38. /**
  39. * Reads entire file into an array.
  40. *
  41. * @since 2.5.0
  42. *
  43. * @param string $file Path to the file.
  44. * @return array|false File contents in an array on success, false on failure.
  45. */
  46. public function get_contents_array( $file ) {
  47. return @file( $file );
  48. }
  49. /**
  50. * Writes a string to a file.
  51. *
  52. * @since 2.5.0
  53. *
  54. * @param string $file Remote path to the file where to write the data.
  55. * @param string $contents The data to write.
  56. * @param int|false $mode Optional. The file permissions as octal number, usually 0644.
  57. * Default false.
  58. * @return bool True on success, false on failure.
  59. */
  60. public function put_contents( $file, $contents, $mode = false ) {
  61. $fp = @fopen( $file, 'wb' );
  62. if ( ! $fp ) {
  63. return false;
  64. }
  65. mbstring_binary_safe_encoding();
  66. $data_length = strlen( $contents );
  67. $bytes_written = fwrite( $fp, $contents );
  68. reset_mbstring_encoding();
  69. fclose( $fp );
  70. if ( $data_length !== $bytes_written ) {
  71. return false;
  72. }
  73. $this->chmod( $file, $mode );
  74. return true;
  75. }
  76. /**
  77. * Gets the current working directory.
  78. *
  79. * @since 2.5.0
  80. *
  81. * @return string|false The current working directory on success, false on failure.
  82. */
  83. public function cwd() {
  84. return getcwd();
  85. }
  86. /**
  87. * Changes current directory.
  88. *
  89. * @since 2.5.0
  90. *
  91. * @param string $dir The new current directory.
  92. * @return bool True on success, false on failure.
  93. */
  94. public function chdir( $dir ) {
  95. return @chdir( $dir );
  96. }
  97. /**
  98. * Changes the file group.
  99. *
  100. * @since 2.5.0
  101. *
  102. * @param string $file Path to the file.
  103. * @param string|int $group A group name or number.
  104. * @param bool $recursive Optional. If set to true, changes file group recursively.
  105. * Default false.
  106. * @return bool True on success, false on failure.
  107. */
  108. public function chgrp( $file, $group, $recursive = false ) {
  109. if ( ! $this->exists( $file ) ) {
  110. return false;
  111. }
  112. if ( ! $recursive ) {
  113. return chgrp( $file, $group );
  114. }
  115. if ( ! $this->is_dir( $file ) ) {
  116. return chgrp( $file, $group );
  117. }
  118. // Is a directory, and we want recursive.
  119. $file = trailingslashit( $file );
  120. $filelist = $this->dirlist( $file );
  121. foreach ( $filelist as $filename ) {
  122. $this->chgrp( $file . $filename, $group, $recursive );
  123. }
  124. return true;
  125. }
  126. /**
  127. * Changes filesystem permissions.
  128. *
  129. * @since 2.5.0
  130. *
  131. * @param string $file Path to the file.
  132. * @param int|false $mode Optional. The permissions as octal number, usually 0644 for files,
  133. * 0755 for directories. Default false.
  134. * @param bool $recursive Optional. If set to true, changes file permissions recursively.
  135. * Default false.
  136. * @return bool True on success, false on failure.
  137. */
  138. public function chmod( $file, $mode = false, $recursive = false ) {
  139. if ( ! $mode ) {
  140. if ( $this->is_file( $file ) ) {
  141. $mode = FS_CHMOD_FILE;
  142. } elseif ( $this->is_dir( $file ) ) {
  143. $mode = FS_CHMOD_DIR;
  144. } else {
  145. return false;
  146. }
  147. }
  148. if ( ! $recursive || ! $this->is_dir( $file ) ) {
  149. return chmod( $file, $mode );
  150. }
  151. // Is a directory, and we want recursive.
  152. $file = trailingslashit( $file );
  153. $filelist = $this->dirlist( $file );
  154. foreach ( (array) $filelist as $filename => $filemeta ) {
  155. $this->chmod( $file . $filename, $mode, $recursive );
  156. }
  157. return true;
  158. }
  159. /**
  160. * Changes the owner of a file or directory.
  161. *
  162. * @since 2.5.0
  163. *
  164. * @param string $file Path to the file or directory.
  165. * @param string|int $owner A user name or number.
  166. * @param bool $recursive Optional. If set to true, changes file owner recursively.
  167. * Default false.
  168. * @return bool True on success, false on failure.
  169. */
  170. public function chown( $file, $owner, $recursive = false ) {
  171. if ( ! $this->exists( $file ) ) {
  172. return false;
  173. }
  174. if ( ! $recursive ) {
  175. return chown( $file, $owner );
  176. }
  177. if ( ! $this->is_dir( $file ) ) {
  178. return chown( $file, $owner );
  179. }
  180. // Is a directory, and we want recursive.
  181. $filelist = $this->dirlist( $file );
  182. foreach ( $filelist as $filename ) {
  183. $this->chown( $file . '/' . $filename, $owner, $recursive );
  184. }
  185. return true;
  186. }
  187. /**
  188. * Gets the file owner.
  189. *
  190. * @since 2.5.0
  191. *
  192. * @param string $file Path to the file.
  193. * @return string|false Username of the owner on success, false on failure.
  194. */
  195. public function owner( $file ) {
  196. $owneruid = @fileowner( $file );
  197. if ( ! $owneruid ) {
  198. return false;
  199. }
  200. if ( ! function_exists( 'posix_getpwuid' ) ) {
  201. return $owneruid;
  202. }
  203. $ownerarray = posix_getpwuid( $owneruid );
  204. if ( ! $ownerarray ) {
  205. return false;
  206. }
  207. return $ownerarray['name'];
  208. }
  209. /**
  210. * Gets the permissions of the specified file or filepath in their octal format.
  211. *
  212. * FIXME does not handle errors in fileperms()
  213. *
  214. * @since 2.5.0
  215. *
  216. * @param string $file Path to the file.
  217. * @return string Mode of the file (the last 3 digits).
  218. */
  219. public function getchmod( $file ) {
  220. return substr( decoct( @fileperms( $file ) ), -3 );
  221. }
  222. /**
  223. * Gets the file's group.
  224. *
  225. * @since 2.5.0
  226. *
  227. * @param string $file Path to the file.
  228. * @return string|false The group on success, false on failure.
  229. */
  230. public function group( $file ) {
  231. $gid = @filegroup( $file );
  232. if ( ! $gid ) {
  233. return false;
  234. }
  235. if ( ! function_exists( 'posix_getgrgid' ) ) {
  236. return $gid;
  237. }
  238. $grouparray = posix_getgrgid( $gid );
  239. if ( ! $grouparray ) {
  240. return false;
  241. }
  242. return $grouparray['name'];
  243. }
  244. /**
  245. * Copies a file.
  246. *
  247. * @since 2.5.0
  248. *
  249. * @param string $source Path to the source file.
  250. * @param string $destination Path to the destination file.
  251. * @param bool $overwrite Optional. Whether to overwrite the destination file if it exists.
  252. * Default false.
  253. * @param int|false $mode Optional. The permissions as octal number, usually 0644 for files,
  254. * 0755 for dirs. Default false.
  255. * @return bool True on success, false on failure.
  256. */
  257. public function copy( $source, $destination, $overwrite = false, $mode = false ) {
  258. if ( ! $overwrite && $this->exists( $destination ) ) {
  259. return false;
  260. }
  261. $rtval = copy( $source, $destination );
  262. if ( $mode ) {
  263. $this->chmod( $destination, $mode );
  264. }
  265. return $rtval;
  266. }
  267. /**
  268. * Moves a file.
  269. *
  270. * @since 2.5.0
  271. *
  272. * @param string $source Path to the source file.
  273. * @param string $destination Path to the destination file.
  274. * @param bool $overwrite Optional. Whether to overwrite the destination file if it exists.
  275. * Default false.
  276. * @return bool True on success, false on failure.
  277. */
  278. public function move( $source, $destination, $overwrite = false ) {
  279. if ( ! $overwrite && $this->exists( $destination ) ) {
  280. return false;
  281. }
  282. // Try using rename first. if that fails (for example, source is read only) try copy.
  283. if ( @rename( $source, $destination ) ) {
  284. return true;
  285. }
  286. if ( $this->copy( $source, $destination, $overwrite ) && $this->exists( $destination ) ) {
  287. $this->delete( $source );
  288. return true;
  289. } else {
  290. return false;
  291. }
  292. }
  293. /**
  294. * Deletes a file or directory.
  295. *
  296. * @since 2.5.0
  297. *
  298. * @param string $file Path to the file or directory.
  299. * @param bool $recursive Optional. If set to true, deletes files and folders recursively.
  300. * Default false.
  301. * @param string|false $type Type of resource. 'f' for file, 'd' for directory.
  302. * Default false.
  303. * @return bool True on success, false on failure.
  304. */
  305. public function delete( $file, $recursive = false, $type = false ) {
  306. if ( empty( $file ) ) {
  307. // Some filesystems report this as /, which can cause non-expected recursive deletion of all files in the filesystem.
  308. return false;
  309. }
  310. $file = str_replace( '\\', '/', $file ); // For Win32, occasional problems deleting files otherwise.
  311. if ( 'f' === $type || $this->is_file( $file ) ) {
  312. return @unlink( $file );
  313. }
  314. if ( ! $recursive && $this->is_dir( $file ) ) {
  315. return @rmdir( $file );
  316. }
  317. // At this point it's a folder, and we're in recursive mode.
  318. $file = trailingslashit( $file );
  319. $filelist = $this->dirlist( $file, true );
  320. $retval = true;
  321. if ( is_array( $filelist ) ) {
  322. foreach ( $filelist as $filename => $fileinfo ) {
  323. if ( ! $this->delete( $file . $filename, $recursive, $fileinfo['type'] ) ) {
  324. $retval = false;
  325. }
  326. }
  327. }
  328. if ( file_exists( $file ) && ! @rmdir( $file ) ) {
  329. $retval = false;
  330. }
  331. return $retval;
  332. }
  333. /**
  334. * Checks if a file or directory exists.
  335. *
  336. * @since 2.5.0
  337. *
  338. * @param string $path Path to file or directory.
  339. * @return bool Whether $path exists or not.
  340. */
  341. public function exists( $path ) {
  342. return @file_exists( $path );
  343. }
  344. /**
  345. * Checks if resource is a file.
  346. *
  347. * @since 2.5.0
  348. *
  349. * @param string $file File path.
  350. * @return bool Whether $file is a file.
  351. */
  352. public function is_file( $file ) {
  353. return @is_file( $file );
  354. }
  355. /**
  356. * Checks if resource is a directory.
  357. *
  358. * @since 2.5.0
  359. *
  360. * @param string $path Directory path.
  361. * @return bool Whether $path is a directory.
  362. */
  363. public function is_dir( $path ) {
  364. return @is_dir( $path );
  365. }
  366. /**
  367. * Checks if a file is readable.
  368. *
  369. * @since 2.5.0
  370. *
  371. * @param string $file Path to file.
  372. * @return bool Whether $file is readable.
  373. */
  374. public function is_readable( $file ) {
  375. return @is_readable( $file );
  376. }
  377. /**
  378. * Checks if a file or directory is writable.
  379. *
  380. * @since 2.5.0
  381. *
  382. * @param string $path Path to file or directory.
  383. * @return bool Whether $path is writable.
  384. */
  385. public function is_writable( $path ) {
  386. return @is_writable( $path );
  387. }
  388. /**
  389. * Gets the file's last access time.
  390. *
  391. * @since 2.5.0
  392. *
  393. * @param string $file Path to file.
  394. * @return int|false Unix timestamp representing last access time, false on failure.
  395. */
  396. public function atime( $file ) {
  397. return @fileatime( $file );
  398. }
  399. /**
  400. * Gets the file modification time.
  401. *
  402. * @since 2.5.0
  403. *
  404. * @param string $file Path to file.
  405. * @return int|false Unix timestamp representing modification time, false on failure.
  406. */
  407. public function mtime( $file ) {
  408. return @filemtime( $file );
  409. }
  410. /**
  411. * Gets the file size (in bytes).
  412. *
  413. * @since 2.5.0
  414. *
  415. * @param string $file Path to file.
  416. * @return int|false Size of the file in bytes on success, false on failure.
  417. */
  418. public function size( $file ) {
  419. return @filesize( $file );
  420. }
  421. /**
  422. * Sets the access and modification times of a file.
  423. *
  424. * Note: If $file doesn't exist, it will be created.
  425. *
  426. * @since 2.5.0
  427. *
  428. * @param string $file Path to file.
  429. * @param int $time Optional. Modified time to set for file.
  430. * Default 0.
  431. * @param int $atime Optional. Access time to set for file.
  432. * Default 0.
  433. * @return bool True on success, false on failure.
  434. */
  435. public function touch( $file, $time = 0, $atime = 0 ) {
  436. if ( 0 === $time ) {
  437. $time = time();
  438. }
  439. if ( 0 === $atime ) {
  440. $atime = time();
  441. }
  442. return touch( $file, $time, $atime );
  443. }
  444. /**
  445. * Creates a directory.
  446. *
  447. * @since 2.5.0
  448. *
  449. * @param string $path Path for new directory.
  450. * @param int|false $chmod Optional. The permissions as octal number (or false to skip chmod).
  451. * Default false.
  452. * @param string|int|false $chown Optional. A user name or number (or false to skip chown).
  453. * Default false.
  454. * @param string|int|false $chgrp Optional. A group name or number (or false to skip chgrp).
  455. * Default false.
  456. * @return bool True on success, false on failure.
  457. */
  458. public function mkdir( $path, $chmod = false, $chown = false, $chgrp = false ) {
  459. // Safe mode fails with a trailing slash under certain PHP versions.
  460. $path = untrailingslashit( $path );
  461. if ( empty( $path ) ) {
  462. return false;
  463. }
  464. if ( ! $chmod ) {
  465. $chmod = FS_CHMOD_DIR;
  466. }
  467. if ( ! @mkdir( $path ) ) {
  468. return false;
  469. }
  470. $this->chmod( $path, $chmod );
  471. if ( $chown ) {
  472. $this->chown( $path, $chown );
  473. }
  474. if ( $chgrp ) {
  475. $this->chgrp( $path, $chgrp );
  476. }
  477. return true;
  478. }
  479. /**
  480. * Deletes a directory.
  481. *
  482. * @since 2.5.0
  483. *
  484. * @param string $path Path to directory.
  485. * @param bool $recursive Optional. Whether to recursively remove files/directories.
  486. * Default false.
  487. * @return bool True on success, false on failure.
  488. */
  489. public function rmdir( $path, $recursive = false ) {
  490. return $this->delete( $path, $recursive );
  491. }
  492. /**
  493. * Gets details for files in a directory or a specific file.
  494. *
  495. * @since 2.5.0
  496. *
  497. * @param string $path Path to directory or file.
  498. * @param bool $include_hidden Optional. Whether to include details of hidden ("." prefixed) files.
  499. * Default true.
  500. * @param bool $recursive Optional. Whether to recursively include file details in nested directories.
  501. * Default false.
  502. * @return array|false {
  503. * Array of files. False if unable to list directory contents.
  504. *
  505. * @type string $name Name of the file or directory.
  506. * @type string $perms *nix representation of permissions.
  507. * @type string $permsn Octal representation of permissions.
  508. * @type string $owner Owner name or ID.
  509. * @type int $size Size of file in bytes.
  510. * @type int $lastmodunix Last modified unix timestamp.
  511. * @type mixed $lastmod Last modified month (3 letter) and day (without leading 0).
  512. * @type int $time Last modified time.
  513. * @type string $type Type of resource. 'f' for file, 'd' for directory.
  514. * @type mixed $files If a directory and `$recursive` is true, contains another array of files.
  515. * }
  516. */
  517. public function dirlist( $path, $include_hidden = true, $recursive = false ) {
  518. if ( $this->is_file( $path ) ) {
  519. $limit_file = basename( $path );
  520. $path = dirname( $path );
  521. } else {
  522. $limit_file = false;
  523. }
  524. if ( ! $this->is_dir( $path ) || ! $this->is_readable( $path ) ) {
  525. return false;
  526. }
  527. $dir = dir( $path );
  528. if ( ! $dir ) {
  529. return false;
  530. }
  531. $ret = array();
  532. while ( false !== ( $entry = $dir->read() ) ) {
  533. $struc = array();
  534. $struc['name'] = $entry;
  535. if ( '.' === $struc['name'] || '..' === $struc['name'] ) {
  536. continue;
  537. }
  538. if ( ! $include_hidden && '.' === $struc['name'][0] ) {
  539. continue;
  540. }
  541. if ( $limit_file && $struc['name'] !== $limit_file ) {
  542. continue;
  543. }
  544. $struc['perms'] = $this->gethchmod( $path . '/' . $entry );
  545. $struc['permsn'] = $this->getnumchmodfromh( $struc['perms'] );
  546. $struc['number'] = false;
  547. $struc['owner'] = $this->owner( $path . '/' . $entry );
  548. $struc['group'] = $this->group( $path . '/' . $entry );
  549. $struc['size'] = $this->size( $path . '/' . $entry );
  550. $struc['lastmodunix'] = $this->mtime( $path . '/' . $entry );
  551. $struc['lastmod'] = gmdate( 'M j', $struc['lastmodunix'] );
  552. $struc['time'] = gmdate( 'h:i:s', $struc['lastmodunix'] );
  553. $struc['type'] = $this->is_dir( $path . '/' . $entry ) ? 'd' : 'f';
  554. if ( 'd' === $struc['type'] ) {
  555. if ( $recursive ) {
  556. $struc['files'] = $this->dirlist( $path . '/' . $struc['name'], $include_hidden, $recursive );
  557. } else {
  558. $struc['files'] = array();
  559. }
  560. }
  561. $ret[ $struc['name'] ] = $struc;
  562. }
  563. $dir->close();
  564. unset( $dir );
  565. return $ret;
  566. }
  567. }