3
* Base WordPress Filesystem
6
* @subpackage Filesystem
10
* Base WordPress Filesystem class for which Filesystem implementations extend
14
class WP_Filesystem_Base {
16
* Whether to display debug data for the connection.
22
public $verbose = false;
25
* Cached list of local filepaths to mapped remote filepaths.
31
private $cache = array();
34
* The Access method of the current connection, Set automatically.
43
* Make private properties readable for backwards compatibility.
48
* @param string $name Property to get.
49
* @return mixed Property.
51
public function __get( $name ) {
56
* Make private properties settable for backwards compatibility.
61
* @param string $name Property to set.
62
* @param mixed $value Property value.
63
* @return mixed Newly-set property.
65
public function __set( $name, $value ) {
66
return $this->$name = $value;
70
* Make private properties checkable for backwards compatibility.
75
* @param string $name Property to check if set.
76
* @return bool Whether the property is set.
78
public function __isset( $name ) {
79
return isset( $this->$name );
83
* Make private properties un-settable for backwards compatibility.
88
* @param string $name Property to unset.
90
public function __unset( $name ) {
91
unset( $this->$name );
95
* Return the path on the remote filesystem of ABSPATH.
100
* @return string The location of the remote path.
102
public function abspath() {
103
$folder = $this->find_folder(ABSPATH);
104
// Perhaps the FTP folder is rooted at the WordPress install, Check for wp-includes folder in root, Could have some false positives, but rare.
105
if ( ! $folder && $this->is_dir( '/' . WPINC ) )
111
* Return the path on the remote filesystem of WP_CONTENT_DIR.
116
* @return string The location of the remote path.
118
public function wp_content_dir() {
119
return $this->find_folder(WP_CONTENT_DIR);
123
* Return the path on the remote filesystem of WP_PLUGIN_DIR.
128
* @return string The location of the remote path.
130
public function wp_plugins_dir() {
131
return $this->find_folder(WP_PLUGIN_DIR);
135
* Return the path on the remote filesystem of the Themes Directory.
140
* @param string $theme The Theme stylesheet or template for the directory.
141
* @return string The location of the remote path.
143
public function wp_themes_dir( $theme = false ) {
144
$theme_root = get_theme_root( $theme );
146
// Account for relative theme roots
147
if ( '/themes' == $theme_root || ! is_dir( $theme_root ) )
148
$theme_root = WP_CONTENT_DIR . $theme_root;
150
return $this->find_folder( $theme_root );
154
* Return the path on the remote filesystem of WP_LANG_DIR.
159
* @return string The location of the remote path.
161
public function wp_lang_dir() {
162
return $this->find_folder(WP_LANG_DIR);
166
* Locate a folder on the remote filesystem.
170
* @deprecated 2.7.0 use WP_Filesystem::abspath() or WP_Filesystem::wp_*_dir() instead.
171
* @see WP_Filesystem::abspath()
172
* @see WP_Filesystem::wp_content_dir()
173
* @see WP_Filesystem::wp_plugins_dir()
174
* @see WP_Filesystem::wp_themes_dir()
175
* @see WP_Filesystem::wp_lang_dir()
177
* @param string $base The folder to start searching from.
178
* @param bool $echo True to display debug information.
180
* @return string The location of the remote path.
182
public function find_base_dir( $base = '.', $echo = false ) {
183
_deprecated_function(__FUNCTION__, '2.7', 'WP_Filesystem::abspath() or WP_Filesystem::wp_*_dir()' );
184
$this->verbose = $echo;
185
return $this->abspath();
189
* Locate a folder on the remote filesystem.
193
* @deprecated 2.7.0 use WP_Filesystem::abspath() or WP_Filesystem::wp_*_dir() methods instead.
194
* @see WP_Filesystem::abspath()
195
* @see WP_Filesystem::wp_content_dir()
196
* @see WP_Filesystem::wp_plugins_dir()
197
* @see WP_Filesystem::wp_themes_dir()
198
* @see WP_Filesystem::wp_lang_dir()
200
* @param string $base The folder to start searching from.
201
* @param bool $echo True to display debug information.
202
* @return string The location of the remote path.
204
public function get_base_dir( $base = '.', $echo = false ) {
205
_deprecated_function(__FUNCTION__, '2.7', 'WP_Filesystem::abspath() or WP_Filesystem::wp_*_dir()' );
206
$this->verbose = $echo;
207
return $this->abspath();
211
* Locate a folder on the remote filesystem.
213
* Assumes that on Windows systems, Stripping off the Drive
214
* letter is OK Sanitizes \\ to / in windows filepaths.
219
* @param string $folder the folder to locate.
220
* @return string The location of the remote path.
222
public function find_folder( $folder ) {
224
if ( isset( $this->cache[ $folder ] ) )
225
return $this->cache[ $folder ];
227
if ( stripos($this->method, 'ftp') !== false ) {
228
$constant_overrides = array(
229
'FTP_BASE' => ABSPATH,
230
'FTP_CONTENT_DIR' => WP_CONTENT_DIR,
231
'FTP_PLUGIN_DIR' => WP_PLUGIN_DIR,
232
'FTP_LANG_DIR' => WP_LANG_DIR
235
// Direct matches ( folder = CONSTANT/ )
236
foreach ( $constant_overrides as $constant => $dir ) {
237
if ( ! defined( $constant ) )
239
if ( $folder === $dir )
240
return trailingslashit( constant( $constant ) );
243
// Prefix Matches ( folder = CONSTANT/subdir )
244
foreach ( $constant_overrides as $constant => $dir ) {
245
if ( ! defined( $constant ) )
247
if ( 0 === stripos( $folder, $dir ) ) { // $folder starts with $dir
248
$potential_folder = preg_replace( '#^' . preg_quote( $dir, '#' ) . '/#i', trailingslashit( constant( $constant ) ), $folder );
249
$potential_folder = trailingslashit( $potential_folder );
251
if ( $this->is_dir( $potential_folder ) ) {
252
$this->cache[ $folder ] = $potential_folder;
253
return $potential_folder;
257
} elseif ( 'direct' == $this->method ) {
258
$folder = str_replace('\\', '/', $folder); // Windows path sanitisation
259
return trailingslashit($folder);
262
$folder = preg_replace('|^([a-z]{1}):|i', '', $folder); // Strip out windows drive letter if it's there.
263
$folder = str_replace('\\', '/', $folder); // Windows path sanitisation
265
if ( isset($this->cache[ $folder ] ) )
266
return $this->cache[ $folder ];
268
if ( $this->exists($folder) ) { // Folder exists at that absolute path.
269
$folder = trailingslashit($folder);
270
$this->cache[ $folder ] = $folder;
273
if ( $return = $this->search_for_folder($folder) )
274
$this->cache[ $folder ] = $return;
279
* Locate a folder on the remote filesystem.
281
* Expects Windows sanitized path.
286
* @param string $folder The folder to locate.
287
* @param string $base The folder to start searching from.
288
* @param bool $loop If the function has recursed, Internal use only.
289
* @return string The location of the remote path.
291
public function search_for_folder( $folder, $base = '.', $loop = false ) {
292
if ( empty( $base ) || '.' == $base )
293
$base = trailingslashit($this->cwd());
295
$folder = untrailingslashit($folder);
297
if ( $this->verbose )
298
printf( "\n" . __('Looking for %1$s in %2$s') . "<br/>\n", $folder, $base );
300
$folder_parts = explode('/', $folder);
301
$folder_part_keys = array_keys( $folder_parts );
302
$last_index = array_pop( $folder_part_keys );
303
$last_path = $folder_parts[ $last_index ];
305
$files = $this->dirlist( $base );
307
foreach ( $folder_parts as $index => $key ) {
308
if ( $index == $last_index )
309
continue; // We want this to be caught by the next code block.
312
* Working from /home/ to /user/ to /wordpress/ see if that file exists within
313
* the current folder, If it's found, change into it and follow through looking
314
* for it. If it cant find WordPress down that route, it'll continue onto the next
315
* folder level, and see if that matches, and so on. If it reaches the end, and still
316
* cant find it, it'll return false for the entire function.
318
if ( isset($files[ $key ]) ){
320
// Lets try that folder:
321
$newdir = trailingslashit(path_join($base, $key));
322
if ( $this->verbose )
323
printf( "\n" . __('Changing to %s') . "<br/>\n", $newdir );
325
// Only search for the remaining path tokens in the directory, not the full path again.
326
$newfolder = implode( '/', array_slice( $folder_parts, $index + 1 ) );
327
if ( $ret = $this->search_for_folder( $newfolder, $newdir, $loop) )
332
// Only check this as a last resort, to prevent locating the incorrect install. All above procedures will fail quickly if this is the right branch to take.
333
if (isset( $files[ $last_path ] ) ) {
334
if ( $this->verbose )
335
printf( "\n" . __('Found %s') . "<br/>\n", $base . $last_path );
336
return trailingslashit($base . $last_path);
339
// Prevent this function from looping again.
340
// No need to proceed if we've just searched in /
341
if ( $loop || '/' == $base )
344
// As an extra last resort, Change back to / if the folder wasn't found.
345
// This comes into effect when the CWD is /home/user/ but WP is at /var/www/....
346
return $this->search_for_folder( $folder, '/', true );
351
* Return the *nix-style file permissions for a file.
353
* From the PHP documentation page for fileperms().
355
* @link http://docs.php.net/fileperms
360
* @param string $file String filename.
361
* @return string The *nix-style representation of permissions.
363
public function gethchmod( $file ){
364
$perms = $this->getchmod($file);
365
if (($perms & 0xC000) == 0xC000) // Socket
367
elseif (($perms & 0xA000) == 0xA000) // Symbolic Link
369
elseif (($perms & 0x8000) == 0x8000) // Regular
371
elseif (($perms & 0x6000) == 0x6000) // Block special
373
elseif (($perms & 0x4000) == 0x4000) // Directory
375
elseif (($perms & 0x2000) == 0x2000) // Character special
377
elseif (($perms & 0x1000) == 0x1000) // FIFO pipe
383
$info .= (($perms & 0x0100) ? 'r' : '-');
384
$info .= (($perms & 0x0080) ? 'w' : '-');
385
$info .= (($perms & 0x0040) ?
386
(($perms & 0x0800) ? 's' : 'x' ) :
387
(($perms & 0x0800) ? 'S' : '-'));
390
$info .= (($perms & 0x0020) ? 'r' : '-');
391
$info .= (($perms & 0x0010) ? 'w' : '-');
392
$info .= (($perms & 0x0008) ?
393
(($perms & 0x0400) ? 's' : 'x' ) :
394
(($perms & 0x0400) ? 'S' : '-'));
397
$info .= (($perms & 0x0004) ? 'r' : '-');
398
$info .= (($perms & 0x0002) ? 'w' : '-');
399
$info .= (($perms & 0x0001) ?
400
(($perms & 0x0200) ? 't' : 'x' ) :
401
(($perms & 0x0200) ? 'T' : '-'));
406
* Convert *nix-style file permissions to a octal number.
408
* Converts '-rw-r--r--' to 0644
409
* From "info at rvgate dot nl"'s comment on the PHP documentation for chmod()
411
* @link http://docs.php.net/manual/en/function.chmod.php#49614
416
* @param string $mode string The *nix-style file permission.
417
* @return int octal representation
419
public function getnumchmodfromh( $mode ) {
421
$legal = array('', 'w', 'r', 'x', '-');
422
$attarray = preg_split('//', $mode);
424
for ($i=0; $i < count($attarray); $i++)
425
if ($key = array_search($attarray[$i], $legal))
426
$realmode .= $legal[$key];
428
$mode = str_pad($realmode, 10, '-', STR_PAD_LEFT);
429
$trans = array('-'=>'0', 'r'=>'4', 'w'=>'2', 'x'=>'1');
430
$mode = strtr($mode,$trans);
433
$newmode .= $mode[1] + $mode[2] + $mode[3];
434
$newmode .= $mode[4] + $mode[5] + $mode[6];
435
$newmode .= $mode[7] + $mode[8] + $mode[9];
440
* Determine if the string provided contains binary characters.
445
* @param string $text String to test against.
446
* @return bool true if string is binary, false otherwise.
448
public function is_binary( $text ) {
449
return (bool) preg_match( '|[^\x20-\x7E]|', $text ); // chr(32)..chr(127)
453
* Change the ownership of a file / folder.
455
* Default behavior is to do nothing, override this in your subclass, if desired.
459
* @param string $file Path to the file.
460
* @param mixed $owner A user name or number.
461
* @param bool $recursive Optional. If set True changes file owner recursivly. Defaults to False.
462
* @return bool Returns true on success or false on failure.
464
public function chown( $file, $owner, $recursive = false ) {
469
* Connect filesystem.
473
* @return bool True on success or false on failure (always true for WP_Filesystem_Direct).
475
public function connect() {
480
* Read entire file into a string.
484
* @param string $file Name of the file to read.
485
* @return string|bool Returns the read data or false on failure.
487
public function get_contents( $file ) {
492
* Read entire file into an array.
496
* @param string $file Path to the file.
497
* @return array|bool the file contents in an array or false on failure.
499
public function get_contents_array( $file ) {
504
* Write a string to a file.
508
* @param string $file Remote path to the file where to write the data.
509
* @param string $contents The data to write.
510
* @param int $mode Optional. The file permissions as octal number, usually 0644.
511
* @return bool False on failure.
513
public function put_contents( $file, $contents, $mode = false ) {
518
* Get the current working directory.
522
* @return string|bool The current working directory on success, or false on failure.
524
public function cwd() {
529
* Change current directory.
533
* @param string $dir The new current directory.
534
* @return bool Returns true on success or false on failure.
536
public function chdir( $dir ) {
541
* Change the file group.
545
* @param string $file Path to the file.
546
* @param mixed $group A group name or number.
547
* @param bool $recursive Optional. If set True changes file group recursively. Defaults to False.
548
* @return bool Returns true on success or false on failure.
550
public function chgrp( $file, $group, $recursive = false ) {
555
* Change filesystem permissions.
559
* @param string $file Path to the file.
560
* @param int $mode Optional. The permissions as octal number, usually 0644 for files, 0755 for dirs.
561
* @param bool $recursive Optional. If set True changes file group recursively. Defaults to False.
562
* @return bool Returns true on success or false on failure.
564
public function chmod( $file, $mode = false, $recursive = false ) {
569
* Get the file owner.
573
* @param string $file Path to the file.
574
* @return string|bool Username of the user or false on error.
576
public function owner( $file ) {
581
* Get the file's group.
585
* @param string $file Path to the file.
586
* @return string|bool The group or false on error.
588
public function group( $file ) {
597
* @param string $source Path to the source file.
598
* @param string $destination Path to the destination file.
599
* @param bool $overwrite Optional. Whether to overwrite the destination file if it exists.
601
* @param int $mode Optional. The permissions as octal number, usually 0644 for files, 0755 for dirs.
603
* @return bool True if file copied successfully, False otherwise.
605
public function copy( $source, $destination, $overwrite = false, $mode = false ) {
614
* @param string $source Path to the source file.
615
* @param string $destination Path to the destination file.
616
* @param bool $overwrite Optional. Whether to overwrite the destination file if it exists.
618
* @return bool True if file copied successfully, False otherwise.
620
public function move( $source, $destination, $overwrite = false ) {
625
* Delete a file or directory.
629
* @param string $file Path to the file.
630
* @param bool $recursive Optional. If set True changes file group recursively. Defaults to False.
632
* @param bool $type Type of resource. 'f' for file, 'd' for directory.
634
* @return bool True if the file or directory was deleted, false on failure.
636
public function delete( $file, $recursive = false, $type = false ) {
641
* Check if a file or directory exists.
645
* @param string $file Path to file/directory.
646
* @return bool Whether $file exists or not.
648
public function exists( $file ) {
653
* Check if resource is a file.
657
* @param string $file File path.
658
* @return bool Whether $file is a file.
660
public function is_file( $file ) {
665
* Check if resource is a directory.
669
* @param string $path Directory path.
670
* @return bool Whether $path is a directory.
672
public function is_dir( $path ) {
677
* Check if a file is readable.
681
* @param string $file Path to file.
682
* @return bool Whether $file is readable.
684
public function is_readable( $file ) {
689
* Check if a file or directory is writable.
693
* @param string $path Path to file/directory.
694
* @return bool Whether $file is writable.
696
public function is_writable( $file ) {
701
* Gets the file's last access time.
705
* @param string $file Path to file.
706
* @return int Unix timestamp representing last access time.
708
public function atime( $file ) {
713
* Gets the file modification time.
717
* @param string $file Path to file.
718
* @return int Unix timestamp representing modification time.
720
public function mtime( $file ) {
725
* Gets the file size (in bytes).
729
* @param string $file Path to file.
730
* @return int Size of the file in bytes.
732
public function size( $file ) {
737
* Set the access and modification times of a file.
739
* Note: If $file doesn't exist, it will be created.
743
* @param string $file Path to file.
744
* @param int $time Optional. Modified time to set for file.
746
* @param int $atime Optional. Access time to set for file.
748
* @return bool Whether operation was successful or not.
750
public function touch( $file, $time = 0, $atime = 0 ) {
755
* Create a directory.
759
* @param string $path Path for new directory.
760
* @param mixed $chmod Optional. The permissions as octal number, (or False to skip chmod)
762
* @param mixed $chown Optional. A user name or number (or False to skip chown)
764
* @param mixed $chgrp Optional. A group name or number (or False to skip chgrp).
766
* @return bool False if directory cannot be created, true otherwise.
768
public function mkdir( $path, $chmod = false, $chown = false, $chgrp = false ) {
773
* Delete a directory.
777
* @param string $path Path to directory.
778
* @param bool $recursive Optional. Whether to recursively remove files/directories.
780
* @return bool Whether directory is deleted successfully or not.
782
public function rmdir( $path, $recursive = false ) {
787
* Get details for files in a directory or a specific file.
791
* @param string $path Path to directory or file.
792
* @param bool $include_hidden Optional. Whether to include details of hidden ("." prefixed) files.
794
* @param bool $recursive Optional. Whether to recursively include file details in nested directories.
796
* @return array|bool {
797
* Array of files. False if unable to list directory contents.
799
* @type string 'name' Name of the file/directory.
800
* @type string 'perms' *nix representation of permissions.
801
* @type int 'permsn' Octal representation of permissions.
802
* @type string 'owner' Owner name or ID.
803
* @type int 'size' Size of file in bytes.
804
* @type int 'lastmodunix' Last modified unix timestamp.
805
* @type mixed 'lastmod' Last modified month (3 letter) and day (without leading 0).
806
* @type int 'time' Last modified time.
807
* @type string 'type' Type of resource. 'f' for file, 'd' for directory.
808
* @type mixed 'files' If a directory and $recursive is true, contains another array of files.
811
public function dirlist( $path, $include_hidden = true, $recursive = false ) {
815
} // WP_Filesystem_Base