PageRenderTime 52ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-admin/includes/class-wp-filesystem-direct.php

http://github.com/wordpress/wordpress
PHP | 620 lines | 329 code | 48 blank | 243 comment | 56 complexity | 8cd2c6e9574201e479791c9cdf027e44 MD5 | raw file
Possible License(s): 0BSD
  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. 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, deletes files and folders 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 ) ) {
  301. // Some filesystems report this as /, which can cause non-expected recursive deletion of all files in the filesystem.
  302. return false;
  303. }
  304. $file = str_replace( '\\', '/', $file ); // For Win32, occasional problems deleting files otherwise.
  305. if ( 'f' == $type || $this->is_file( $file ) ) {
  306. return @unlink( $file );
  307. }
  308. if ( ! $recursive && $this->is_dir( $file ) ) {
  309. return @rmdir( $file );
  310. }
  311. // At this point it's a folder, and we're in recursive mode.
  312. $file = trailingslashit( $file );
  313. $filelist = $this->dirlist( $file, true );
  314. $retval = true;
  315. if ( is_array( $filelist ) ) {
  316. foreach ( $filelist as $filename => $fileinfo ) {
  317. if ( ! $this->delete( $file . $filename, $recursive, $fileinfo['type'] ) ) {
  318. $retval = false;
  319. }
  320. }
  321. }
  322. if ( file_exists( $file ) && ! @rmdir( $file ) ) {
  323. $retval = false;
  324. }
  325. return $retval;
  326. }
  327. /**
  328. * Checks if a file or directory exists.
  329. *
  330. * @since 2.5.0
  331. *
  332. * @param string $file Path to file or directory.
  333. * @return bool Whether $file exists or not.
  334. */
  335. public function exists( $file ) {
  336. return @file_exists( $file );
  337. }
  338. /**
  339. * Checks if resource is a file.
  340. *
  341. * @since 2.5.0
  342. *
  343. * @param string $file File path.
  344. * @return bool Whether $file is a file.
  345. */
  346. public function is_file( $file ) {
  347. return @is_file( $file );
  348. }
  349. /**
  350. * Checks if resource is a directory.
  351. *
  352. * @since 2.5.0
  353. *
  354. * @param string $path Directory path.
  355. * @return bool Whether $path is a directory.
  356. */
  357. public function is_dir( $path ) {
  358. return @is_dir( $path );
  359. }
  360. /**
  361. * Checks if a file is readable.
  362. *
  363. * @since 2.5.0
  364. *
  365. * @param string $file Path to file.
  366. * @return bool Whether $file is readable.
  367. */
  368. public function is_readable( $file ) {
  369. return @is_readable( $file );
  370. }
  371. /**
  372. * Checks if a file or directory is writable.
  373. *
  374. * @since 2.5.0
  375. *
  376. * @param string $file Path to file or directory.
  377. * @return bool Whether $file is writable.
  378. */
  379. public function is_writable( $file ) {
  380. return @is_writable( $file );
  381. }
  382. /**
  383. * Gets the file's last access time.
  384. *
  385. * @since 2.5.0
  386. *
  387. * @param string $file Path to file.
  388. * @return int|false Unix timestamp representing last access time, false on failure.
  389. */
  390. public function atime( $file ) {
  391. return @fileatime( $file );
  392. }
  393. /**
  394. * Gets the file modification time.
  395. *
  396. * @since 2.5.0
  397. *
  398. * @param string $file Path to file.
  399. * @return int|false Unix timestamp representing modification time, false on failure.
  400. */
  401. public function mtime( $file ) {
  402. return @filemtime( $file );
  403. }
  404. /**
  405. * Gets the file size (in bytes).
  406. *
  407. * @since 2.5.0
  408. *
  409. * @param string $file Path to file.
  410. * @return int|false Size of the file in bytes on success, false on failure.
  411. */
  412. public function size( $file ) {
  413. return @filesize( $file );
  414. }
  415. /**
  416. * Sets the access and modification times of a file.
  417. *
  418. * Note: If $file doesn't exist, it will be created.
  419. *
  420. * @since 2.5.0
  421. *
  422. * @param string $file Path to file.
  423. * @param int $time Optional. Modified time to set for file.
  424. * Default 0.
  425. * @param int $atime Optional. Access time to set for file.
  426. * Default 0.
  427. * @return bool True on success, false on failure.
  428. */
  429. public function touch( $file, $time = 0, $atime = 0 ) {
  430. if ( 0 == $time ) {
  431. $time = time();
  432. }
  433. if ( 0 == $atime ) {
  434. $atime = time();
  435. }
  436. return touch( $file, $time, $atime );
  437. }
  438. /**
  439. * Creates a directory.
  440. *
  441. * @since 2.5.0
  442. *
  443. * @param string $path Path for new directory.
  444. * @param int|false $chmod Optional. The permissions as octal number (or false to skip chmod).
  445. * Default false.
  446. * @param string|int $chown Optional. A user name or number (or false to skip chown).
  447. * Default false.
  448. * @param string|int $chgrp Optional. A group name or number (or false to skip chgrp).
  449. * Default false.
  450. * @return bool True on success, false on failure.
  451. */
  452. public function mkdir( $path, $chmod = false, $chown = false, $chgrp = false ) {
  453. // Safe mode fails with a trailing slash under certain PHP versions.
  454. $path = untrailingslashit( $path );
  455. if ( empty( $path ) ) {
  456. return false;
  457. }
  458. if ( ! $chmod ) {
  459. $chmod = FS_CHMOD_DIR;
  460. }
  461. if ( ! @mkdir( $path ) ) {
  462. return false;
  463. }
  464. $this->chmod( $path, $chmod );
  465. if ( $chown ) {
  466. $this->chown( $path, $chown );
  467. }
  468. if ( $chgrp ) {
  469. $this->chgrp( $path, $chgrp );
  470. }
  471. return true;
  472. }
  473. /**
  474. * Deletes a directory.
  475. *
  476. * @since 2.5.0
  477. *
  478. * @param string $path Path to directory.
  479. * @param bool $recursive Optional. Whether to recursively remove files/directories.
  480. * Default false.
  481. * @return bool True on success, false on failure.
  482. */
  483. public function rmdir( $path, $recursive = false ) {
  484. return $this->delete( $path, $recursive );
  485. }
  486. /**
  487. * Gets details for files in a directory or a specific file.
  488. *
  489. * @since 2.5.0
  490. *
  491. * @param string $path Path to directory or file.
  492. * @param bool $include_hidden Optional. Whether to include details of hidden ("." prefixed) files.
  493. * Default true.
  494. * @param bool $recursive Optional. Whether to recursively include file details in nested directories.
  495. * Default false.
  496. * @return array|false {
  497. * Array of files. False if unable to list directory contents.
  498. *
  499. * @type string $name Name of the file or directory.
  500. * @type string $perms *nix representation of permissions.
  501. * @type int $permsn Octal representation of permissions.
  502. * @type string $owner Owner name or ID.
  503. * @type int $size Size of file in bytes.
  504. * @type int $lastmodunix Last modified unix timestamp.
  505. * @type mixed $lastmod Last modified month (3 letter) and day (without leading 0).
  506. * @type int $time Last modified time.
  507. * @type string $type Type of resource. 'f' for file, 'd' for directory.
  508. * @type mixed $files If a directory and $recursive is true, contains another array of files.
  509. * }
  510. */
  511. public function dirlist( $path, $include_hidden = true, $recursive = false ) {
  512. if ( $this->is_file( $path ) ) {
  513. $limit_file = basename( $path );
  514. $path = dirname( $path );
  515. } else {
  516. $limit_file = false;
  517. }
  518. if ( ! $this->is_dir( $path ) || ! $this->is_readable( $path ) ) {
  519. return false;
  520. }
  521. $dir = dir( $path );
  522. if ( ! $dir ) {
  523. return false;
  524. }
  525. $ret = array();
  526. while ( false !== ( $entry = $dir->read() ) ) {
  527. $struc = array();
  528. $struc['name'] = $entry;
  529. if ( '.' == $struc['name'] || '..' == $struc['name'] ) {
  530. continue;
  531. }
  532. if ( ! $include_hidden && '.' == $struc['name'][0] ) {
  533. continue;
  534. }
  535. if ( $limit_file && $struc['name'] != $limit_file ) {
  536. continue;
  537. }
  538. $struc['perms'] = $this->gethchmod( $path . '/' . $entry );
  539. $struc['permsn'] = $this->getnumchmodfromh( $struc['perms'] );
  540. $struc['number'] = false;
  541. $struc['owner'] = $this->owner( $path . '/' . $entry );
  542. $struc['group'] = $this->group( $path . '/' . $entry );
  543. $struc['size'] = $this->size( $path . '/' . $entry );
  544. $struc['lastmodunix'] = $this->mtime( $path . '/' . $entry );
  545. $struc['lastmod'] = gmdate( 'M j', $struc['lastmodunix'] );
  546. $struc['time'] = gmdate( 'h:i:s', $struc['lastmodunix'] );
  547. $struc['type'] = $this->is_dir( $path . '/' . $entry ) ? 'd' : 'f';
  548. if ( 'd' == $struc['type'] ) {
  549. if ( $recursive ) {
  550. $struc['files'] = $this->dirlist( $path . '/' . $struc['name'], $include_hidden, $recursive );
  551. } else {
  552. $struc['files'] = array();
  553. }
  554. }
  555. $ret[ $struc['name'] ] = $struc;
  556. }
  557. $dir->close();
  558. unset( $dir );
  559. return $ret;
  560. }
  561. }