class-wp-filesystem-direct.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  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 group 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. return $ownerarray['name'];
  205. }
  206. /**
  207. * Gets the permissions of the specified file or filepath in their octal format.
  208. *
  209. * FIXME does not handle errors in fileperms()
  210. *
  211. * @since 2.5.0
  212. *
  213. * @param string $file Path to the file.
  214. * @return string Mode of the file (the last 3 digits).
  215. */
  216. public function getchmod( $file ) {
  217. return substr( decoct( @fileperms( $file ) ), -3 );
  218. }
  219. /**
  220. * Gets the file's group.
  221. *
  222. * @since 2.5.0
  223. *
  224. * @param string $file Path to the file.
  225. * @return string|false The group on success, false on failure.
  226. */
  227. public function group( $file ) {
  228. $gid = @filegroup( $file );
  229. if ( ! $gid ) {
  230. return false;
  231. }
  232. if ( ! function_exists( 'posix_getgrgid' ) ) {
  233. return $gid;
  234. }
  235. $grouparray = posix_getgrgid( $gid );
  236. return $grouparray['name'];
  237. }
  238. /**
  239. * Copies a file.
  240. *
  241. * @since 2.5.0
  242. *
  243. * @param string $source Path to the source file.
  244. * @param string $destination Path to the destination file.
  245. * @param bool $overwrite Optional. Whether to overwrite the destination file if it exists.
  246. * Default false.
  247. * @param int|false $mode Optional. The permissions as octal number, usually 0644 for files,
  248. * 0755 for dirs. Default false.
  249. * @return bool True on success, false on failure.
  250. */
  251. public function copy( $source, $destination, $overwrite = false, $mode = false ) {
  252. if ( ! $overwrite && $this->exists( $destination ) ) {
  253. return false;
  254. }
  255. $rtval = copy( $source, $destination );
  256. if ( $mode ) {
  257. $this->chmod( $destination, $mode );
  258. }
  259. return $rtval;
  260. }
  261. /**
  262. * Moves a file.
  263. *
  264. * @since 2.5.0
  265. *
  266. * @param string $source Path to the source file.
  267. * @param string $destination Path to the destination file.
  268. * @param bool $overwrite Optional. Whether to overwrite the destination file if it exists.
  269. * Default false.
  270. * @return bool True on success, false on failure.
  271. */
  272. public function move( $source, $destination, $overwrite = false ) {
  273. if ( ! $overwrite && $this->exists( $destination ) ) {
  274. return false;
  275. }
  276. // Try using rename first. if that fails (for example, source is read only) try copy.
  277. if ( @rename( $source, $destination ) ) {
  278. return true;
  279. }
  280. if ( $this->copy( $source, $destination, $overwrite ) && $this->exists( $destination ) ) {
  281. $this->delete( $source );
  282. return true;
  283. } else {
  284. return false;
  285. }
  286. }
  287. /**
  288. * Deletes a file or directory.
  289. *
  290. * @since 2.5.0
  291. *
  292. * @param string $file Path to the file or directory.
  293. * @param bool $recursive Optional. If set to true, changes file group recursively.
  294. * Default false.
  295. * @param string|false $type Type of resource. 'f' for file, 'd' for directory.
  296. * Default false.
  297. * @return bool True on success, false on failure.
  298. */
  299. public function delete( $file, $recursive = false, $type = false ) {
  300. if ( empty( $file ) ) { // Some filesystems report this as /, which can cause non-expected recursive deletion of all files in the filesystem.
  301. return false;
  302. }
  303. $file = str_replace( '\\', '/', $file ); // for win32, occasional problems deleting files otherwise
  304. if ( 'f' == $type || $this->is_file( $file ) ) {
  305. return @unlink( $file );
  306. }
  307. if ( ! $recursive && $this->is_dir( $file ) ) {
  308. return @rmdir( $file );
  309. }
  310. // At this point it's a folder, and we're in recursive mode
  311. $file = trailingslashit( $file );
  312. $filelist = $this->dirlist( $file, true );
  313. $retval = true;
  314. if ( is_array( $filelist ) ) {
  315. foreach ( $filelist as $filename => $fileinfo ) {
  316. if ( ! $this->delete( $file . $filename, $recursive, $fileinfo['type'] ) ) {
  317. $retval = false;
  318. }
  319. }
  320. }
  321. if ( file_exists( $file ) && ! @rmdir( $file ) ) {
  322. $retval = false;
  323. }
  324. return $retval;
  325. }
  326. /**
  327. * Checks if a file or directory exists.
  328. *
  329. * @since 2.5.0
  330. *
  331. * @param string $file Path to file or directory.
  332. * @return bool Whether $file exists or not.
  333. */
  334. public function exists( $file ) {
  335. return @file_exists( $file );
  336. }
  337. /**
  338. * Checks if resource is a file.
  339. *
  340. * @since 2.5.0
  341. *
  342. * @param string $file File path.
  343. * @return bool Whether $file is a file.
  344. */
  345. public function is_file( $file ) {
  346. return @is_file( $file );
  347. }
  348. /**
  349. * Checks if resource is a directory.
  350. *
  351. * @since 2.5.0
  352. *
  353. * @param string $path Directory path.
  354. * @return bool Whether $path is a directory.
  355. */
  356. public function is_dir( $path ) {
  357. return @is_dir( $path );
  358. }
  359. /**
  360. * Checks if a file is readable.
  361. *
  362. * @since 2.5.0
  363. *
  364. * @param string $file Path to file.
  365. * @return bool Whether $file is readable.
  366. */
  367. public function is_readable( $file ) {
  368. return @is_readable( $file );
  369. }
  370. /**
  371. * Checks if a file or directory is writable.
  372. *
  373. * @since 2.5.0
  374. *
  375. * @param string $file Path to file or directory.
  376. * @return bool Whether $file is writable.
  377. */
  378. public function is_writable( $file ) {
  379. return @is_writable( $file );
  380. }
  381. /**
  382. * Gets the file's last access time.
  383. *
  384. * @since 2.5.0
  385. *
  386. * @param string $file Path to file.
  387. * @return int|false Unix timestamp representing last access time, false on failure.
  388. */
  389. public function atime( $file ) {
  390. return @fileatime( $file );
  391. }
  392. /**
  393. * Gets the file modification time.
  394. *
  395. * @since 2.5.0
  396. *
  397. * @param string $file Path to file.
  398. * @return int|false Unix timestamp representing modification time, false on failure.
  399. */
  400. public function mtime( $file ) {
  401. return @filemtime( $file );
  402. }
  403. /**
  404. * Gets the file size (in bytes).
  405. *
  406. * @since 2.5.0
  407. *
  408. * @param string $file Path to file.
  409. * @return int|false Size of the file in bytes on success, false on failure.
  410. */
  411. public function size( $file ) {
  412. return @filesize( $file );
  413. }
  414. /**
  415. * Sets the access and modification times of a file.
  416. *
  417. * Note: If $file doesn't exist, it will be created.
  418. *
  419. * @since 2.5.0
  420. *
  421. * @param string $file Path to file.
  422. * @param int $time Optional. Modified time to set for file.
  423. * Default 0.
  424. * @param int $atime Optional. Access time to set for file.
  425. * Default 0.
  426. * @return bool True on success, false on failure.
  427. */
  428. public function touch( $file, $time = 0, $atime = 0 ) {
  429. if ( $time == 0 ) {
  430. $time = time();
  431. }
  432. if ( $atime == 0 ) {
  433. $atime = time();
  434. }
  435. return touch( $file, $time, $atime );
  436. }
  437. /**
  438. * Creates a directory.
  439. *
  440. * @since 2.5.0
  441. *
  442. * @param string $path Path for new directory.
  443. * @param int|false $chmod Optional. The permissions as octal number (or false to skip chmod).
  444. * Default false.
  445. * @param string|int $chown Optional. A user name or number (or false to skip chown).
  446. * Default false.
  447. * @param string|int $chgrp Optional. A group name or number (or false to skip chgrp).
  448. * Default false.
  449. * @return bool True on success, false on failure.
  450. */
  451. public function mkdir( $path, $chmod = false, $chown = false, $chgrp = false ) {
  452. // Safe mode fails with a trailing slash under certain PHP versions.
  453. $path = untrailingslashit( $path );
  454. if ( empty( $path ) ) {
  455. return false;
  456. }
  457. if ( ! $chmod ) {
  458. $chmod = FS_CHMOD_DIR;
  459. }
  460. if ( ! @mkdir( $path ) ) {
  461. return false;
  462. }
  463. $this->chmod( $path, $chmod );
  464. if ( $chown ) {
  465. $this->chown( $path, $chown );
  466. }
  467. if ( $chgrp ) {
  468. $this->chgrp( $path, $chgrp );
  469. }
  470. return true;
  471. }
  472. /**
  473. * Deletes a directory.
  474. *
  475. * @since 2.5.0
  476. *
  477. * @param string $path Path to directory.
  478. * @param bool $recursive Optional. Whether to recursively remove files/directories.
  479. * Default false.
  480. * @return bool True on success, false on failure.
  481. */
  482. public function rmdir( $path, $recursive = false ) {
  483. return $this->delete( $path, $recursive );
  484. }
  485. /**
  486. * Gets details for files in a directory or a specific file.
  487. *
  488. * @since 2.5.0
  489. *
  490. * @param string $path Path to directory or file.
  491. * @param bool $include_hidden Optional. Whether to include details of hidden ("." prefixed) files.
  492. * Default true.
  493. * @param bool $recursive Optional. Whether to recursively include file details in nested directories.
  494. * Default false.
  495. * @return array|false {
  496. * Array of files. False if unable to list directory contents.
  497. *
  498. * @type string $name Name of the file or directory.
  499. * @type string $perms *nix representation of permissions.
  500. * @type int $permsn Octal representation of permissions.
  501. * @type string $owner Owner name or ID.
  502. * @type int $size Size of file in bytes.
  503. * @type int $lastmodunix Last modified unix timestamp.
  504. * @type mixed $lastmod Last modified month (3 letter) and day (without leading 0).
  505. * @type int $time Last modified time.
  506. * @type string $type Type of resource. 'f' for file, 'd' for directory.
  507. * @type mixed $files If a directory and $recursive is true, contains another array of files.
  508. * }
  509. */
  510. public function dirlist( $path, $include_hidden = true, $recursive = false ) {
  511. if ( $this->is_file( $path ) ) {
  512. $limit_file = basename( $path );
  513. $path = dirname( $path );
  514. } else {
  515. $limit_file = false;
  516. }
  517. if ( ! $this->is_dir( $path ) || ! $this->is_readable( $path ) ) {
  518. return false;
  519. }
  520. $dir = dir( $path );
  521. if ( ! $dir ) {
  522. return false;
  523. }
  524. $ret = array();
  525. while ( false !== ( $entry = $dir->read() ) ) {
  526. $struc = array();
  527. $struc['name'] = $entry;
  528. if ( '.' == $struc['name'] || '..' == $struc['name'] ) {
  529. continue;
  530. }
  531. if ( ! $include_hidden && '.' == $struc['name'][0] ) {
  532. continue;
  533. }
  534. if ( $limit_file && $struc['name'] != $limit_file ) {
  535. continue;
  536. }
  537. $struc['perms'] = $this->gethchmod( $path . '/' . $entry );
  538. $struc['permsn'] = $this->getnumchmodfromh( $struc['perms'] );
  539. $struc['number'] = false;
  540. $struc['owner'] = $this->owner( $path . '/' . $entry );
  541. $struc['group'] = $this->group( $path . '/' . $entry );
  542. $struc['size'] = $this->size( $path . '/' . $entry );
  543. $struc['lastmodunix'] = $this->mtime( $path . '/' . $entry );
  544. $struc['lastmod'] = gmdate( 'M j', $struc['lastmodunix'] );
  545. $struc['time'] = gmdate( 'h:i:s', $struc['lastmodunix'] );
  546. $struc['type'] = $this->is_dir( $path . '/' . $entry ) ? 'd' : 'f';
  547. if ( 'd' == $struc['type'] ) {
  548. if ( $recursive ) {
  549. $struc['files'] = $this->dirlist( $path . '/' . $struc['name'], $include_hidden, $recursive );
  550. } else {
  551. $struc['files'] = array();
  552. }
  553. }
  554. $ret[ $struc['name'] ] = $struc;
  555. }
  556. $dir->close();
  557. unset( $dir );
  558. return $ret;
  559. }
  560. }