WPFS cleanups. Props DD32. fixes #9525

git-svn-id: http://svn.automattic.com/wordpress/trunk@10919 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2009-04-13 16:11:02 +00:00
parent 3dd79f8587
commit 31a2bbad76
4 changed files with 84 additions and 68 deletions

View File

@ -46,8 +46,6 @@ class WP_Filesystem_Base {
* @return string The location of the remote path. * @return string The location of the remote path.
*/ */
function abspath() { function abspath() {
if ( defined('FTP_BASE') && strpos($this->method, 'ftp') !== false )
return FTP_BASE;
$folder = $this->find_folder(ABSPATH); $folder = $this->find_folder(ABSPATH);
//Perhaps the FTP folder is rooted at the WordPress install, Check for wp-includes folder in root, Could have some false positives, but rare. //Perhaps the FTP folder is rooted at the WordPress install, Check for wp-includes folder in root, Could have some false positives, but rare.
if ( ! $folder && $this->is_dir('/wp-includes') ) if ( ! $folder && $this->is_dir('/wp-includes') )
@ -62,8 +60,6 @@ class WP_Filesystem_Base {
* @return string The location of the remote path. * @return string The location of the remote path.
*/ */
function wp_content_dir() { function wp_content_dir() {
if ( defined('FTP_CONTENT_DIR') && strpos($this->method, 'ftp') !== false )
return FTP_CONTENT_DIR;
return $this->find_folder(WP_CONTENT_DIR); return $this->find_folder(WP_CONTENT_DIR);
} }
/** /**
@ -75,8 +71,6 @@ class WP_Filesystem_Base {
* @return string The location of the remote path. * @return string The location of the remote path.
*/ */
function wp_plugins_dir() { function wp_plugins_dir() {
if ( defined('FTP_PLUGIN_DIR') && strpos($this->method, 'ftp') !== false )
return FTP_PLUGIN_DIR;
return $this->find_folder(WP_PLUGIN_DIR); return $this->find_folder(WP_PLUGIN_DIR);
} }
/** /**
@ -142,6 +136,15 @@ class WP_Filesystem_Base {
*/ */
function find_folder($folder) { function find_folder($folder) {
if ( strpos($this->method, 'ftp') !== false ) {
$constant_overrides = array( 'FTP_BASE' => ABSPATH, 'FTP_CONTENT_DIR' => WP_CONTENT_DIR, 'FTP_PLUGIN_DIR' => WP_PLUGIN_DIR );
foreach ( $constant_overrides as $constant => $dir )
if ( defined($constant) && $folder === $dir )
return trailingslashit(constant($constant));
} elseif ( 'direct' == $this->method ) {
return trailingslashit($folder);
}
$folder = preg_replace('|^([a-z]{1}):|i', '', $folder); //Strip out windows driveletter if its there. $folder = preg_replace('|^([a-z]{1}):|i', '', $folder); //Strip out windows driveletter if its there.
$folder = str_replace('\\', '/', $folder); //Windows path sanitiation $folder = str_replace('\\', '/', $folder); //Windows path sanitiation
@ -149,6 +152,7 @@ class WP_Filesystem_Base {
return $this->cache[ $folder ]; return $this->cache[ $folder ];
if ( $this->exists($folder) ) { //Folder exists at that absolute path. if ( $this->exists($folder) ) { //Folder exists at that absolute path.
$folder = trailingslashit($folder);
$this->cache[ $folder ] = $folder; $this->cache[ $folder ] = $folder;
return $folder; return $folder;
} }
@ -189,23 +193,23 @@ class WP_Filesystem_Base {
// If its found, change into it and follow through looking for it. // If its found, change into it and follow through looking for it.
// If it cant find WordPress down that route, it'll continue onto the next folder level, and see if that matches, and so on. // If it cant find WordPress down that route, it'll continue onto the next folder level, and see if that matches, and so on.
// If it reaches the end, and still cant find it, it'll return false for the entire function. // If it reaches the end, and still cant find it, it'll return false for the entire function.
if( isset($files[ $key ]) ){ if ( isset($files[ $key ]) ){
//Lets try that folder: //Lets try that folder:
$newdir = trailingslashit(path_join($base, $key)); $newdir = trailingslashit(path_join($base, $key));
if( $this->verbose ) if ( $this->verbose )
printf( __('Changing to %s') . '<br/>', $newdir ); printf( __('Changing to %s') . '<br/>', $newdir );
if( $ret = $this->search_for_folder( $folder, $newdir, $loop) ) if ( $ret = $this->search_for_folder( $folder, $newdir, $loop) )
return $ret; return $ret;
} }
} }
//Only check this as a last resort, to prevent locating the incorrect install. All above proceeedures will fail quickly if this is the right branch to take. //Only check this as a last resort, to prevent locating the incorrect install. All above proceeedures will fail quickly if this is the right branch to take.
if(isset( $files[ $last_path ] ) ) { if (isset( $files[ $last_path ] ) ) {
if( $this->verbose ) if ( $this->verbose )
printf( __('Found %s') . '<br/>', $base . $last_path ); printf( __('Found %s') . '<br/>', $base . $last_path );
return $base . $last_path; return trailingslashit($base . $last_path);
} }
if( $loop ) if ( $loop )
return false;//Prevent tihs function looping again. return false;//Prevent tihs function looping again.
//As an extra last resort, Change back to / if the folder wasnt found. This comes into effect when the CWD is /home/user/ but WP is at /var/www/.... mainly dedicated setups. //As an extra last resort, Change back to / if the folder wasnt found. This comes into effect when the CWD is /home/user/ but WP is at /var/www/.... mainly dedicated setups.
return $this->search_for_folder($folder, '/', true); return $this->search_for_folder($folder, '/', true);

View File

@ -14,9 +14,9 @@
* @subpackage Filesystem * @subpackage Filesystem
* @uses WP_Filesystem_Base Extends class * @uses WP_Filesystem_Base Extends class
*/ */
class WP_Filesystem_Direct extends WP_Filesystem_Base { class WP_Filesystem_Direct extends WP_Filesystem_Base {
var $permission = null; var $permission = null;
var $errors = array(); var $errors = null;
function WP_Filesystem_Direct($arg) { function WP_Filesystem_Direct($arg) {
$this->method = 'direct'; $this->method = 'direct';
$this->errors = new WP_Error(); $this->errors = new WP_Error();
@ -49,82 +49,82 @@ class WP_Filesystem_Direct extends WP_Filesystem_Base {
return @chdir($dir); return @chdir($dir);
} }
function chgrp($file, $group, $recursive = false) { function chgrp($file, $group, $recursive = false) {
if( ! $this->exists($file) ) if ( ! $this->exists($file) )
return false; return false;
if( ! $recursive ) if ( ! $recursive )
return @chgrp($file, $group); return @chgrp($file, $group);
if( ! $this->is_dir($file) ) if ( ! $this->is_dir($file) )
return @chgrp($file, $group); return @chgrp($file, $group);
//Is a directory, and we want recursive //Is a directory, and we want recursive
$file = trailingslashit($file); $file = trailingslashit($file);
$filelist = $this->dirlist($file); $filelist = $this->dirlist($file);
foreach($filelist as $filename) foreach ($filelist as $filename)
$this->chgrp($file . $filename, $group, $recursive); $this->chgrp($file . $filename, $group, $recursive);
return true; return true;
} }
function chmod($file, $mode = false, $recursive = false) { function chmod($file, $mode = false, $recursive = false) {
if( ! $mode ) if ( ! $mode )
$mode = $this->permission; $mode = $this->permission;
if( ! $this->exists($file) ) if ( ! $this->exists($file) )
return false; return false;
if( ! $recursive ) if ( ! $recursive )
return @chmod($file,$mode); return @chmod($file,$mode);
if( ! $this->is_dir($file) ) if ( ! $this->is_dir($file) )
return @chmod($file, $mode); return @chmod($file, $mode);
//Is a directory, and we want recursive //Is a directory, and we want recursive
$file = trailingslashit($file); $file = trailingslashit($file);
$filelist = $this->dirlist($file); $filelist = $this->dirlist($file);
foreach($filelist as $filename) foreach ($filelist as $filename)
$this->chmod($file . $filename, $mode, $recursive); $this->chmod($file . $filename, $mode, $recursive);
return true; return true;
} }
function chown($file, $owner, $recursive = false) { function chown($file, $owner, $recursive = false) {
if( ! $this->exists($file) ) if ( ! $this->exists($file) )
return false; return false;
if( ! $recursive ) if ( ! $recursive )
return @chown($file, $owner); return @chown($file, $owner);
if( ! $this->is_dir($file) ) if ( ! $this->is_dir($file) )
return @chown($file, $owner); return @chown($file, $owner);
//Is a directory, and we want recursive //Is a directory, and we want recursive
$filelist = $this->dirlist($file); $filelist = $this->dirlist($file);
foreach($filelist as $filename){ foreach ($filelist as $filename){
$this->chown($file . '/' . $filename, $owner, $recursive); $this->chown($file . '/' . $filename, $owner, $recursive);
} }
return true; return true;
} }
function owner($file) { function owner($file) {
$owneruid = @fileowner($file); $owneruid = @fileowner($file);
if( ! $owneruid ) if ( ! $owneruid )
return false; return false;
if( ! function_exists('posix_getpwuid') ) if ( ! function_exists('posix_getpwuid') )
return $owneruid; return $owneruid;
$ownerarray = posix_getpwuid($owneruid); $ownerarray = posix_getpwuid($owneruid);
return $ownerarray['name']; return $ownerarray['name'];
} }
function getchmod($file) { function getchmod($file) {
return @fileperms($file); return substr(decoct(@fileperms($file)),3);
} }
function group($file) { function group($file) {
$gid = @filegroup($file); $gid = @filegroup($file);
if( ! $gid ) if ( ! $gid )
return false; return false;
if( ! function_exists('posix_getgrgid') ) if ( ! function_exists('posix_getgrgid') )
return $gid; return $gid;
$grouparray = posix_getgrgid($gid); $grouparray = posix_getgrgid($gid);
return $grouparray['name']; return $grouparray['name'];
} }
function copy($source, $destination, $overwrite = false) { function copy($source, $destination, $overwrite = false) {
if( ! $overwrite && $this->exists($destination) ) if ( ! $overwrite && $this->exists($destination) )
return false; return false;
return copy($source, $destination); return copy($source, $destination);
} }
function move($source, $destination, $overwrite = false) { function move($source, $destination, $overwrite = false) {
//Possible to use rename()? //Possible to use rename()?
if( $this->copy($source, $destination, $overwrite) && $this->exists($destination) ){ if ( $this->copy($source, $destination, $overwrite) && $this->exists($destination) ){
$this->delete($source); $this->delete($source);
return true; return true;
} else { } else {
@ -133,11 +133,13 @@ class WP_Filesystem_Direct extends WP_Filesystem_Base {
} }
function delete($file, $recursive = false) { function delete($file, $recursive = false) {
if ( empty($file) ) //Some filesystems report this as /, which can cause non-expected recursive deletion of all files in the filesystem.
return false;
$file = str_replace('\\', '/', $file); //for win32, occasional problems deleteing files otherwise $file = str_replace('\\', '/', $file); //for win32, occasional problems deleteing files otherwise
if( $this->is_file($file) ) if ( $this->is_file($file) )
return @unlink($file); return @unlink($file);
if( ! $recursive && $this->is_dir($file) ) if ( ! $recursive && $this->is_dir($file) )
return @rmdir($file); return @rmdir($file);
//At this point its a folder, and we're in recursive mode //At this point its a folder, and we're in recursive mode
@ -145,13 +147,13 @@ class WP_Filesystem_Direct extends WP_Filesystem_Base {
$filelist = $this->dirlist($file, true); $filelist = $this->dirlist($file, true);
$retval = true; $retval = true;
if( is_array($filelist) ) //false if no files, So check first. if ( is_array($filelist) ) //false if no files, So check first.
foreach($filelist as $filename => $fileinfo) foreach ($filelist as $filename => $fileinfo)
if( ! $this->delete($file . $filename, $recursive) ) if ( ! $this->delete($file . $filename, $recursive) )
$retval = false; $retval = false;
if( ! @rmdir($file) ) if ( file_exists($file) && ! @rmdir($file) )
return false; $retval = false;
return $retval; return $retval;
} }
@ -187,33 +189,33 @@ class WP_Filesystem_Direct extends WP_Filesystem_Base {
} }
function touch($file, $time = 0, $atime = 0){ function touch($file, $time = 0, $atime = 0){
if($time == 0) if ($time == 0)
$time = time(); $time = time();
if($atime == 0) if ($atime == 0)
$atime = time(); $atime = time();
return @touch($file, $time, $atime); return @touch($file, $time, $atime);
} }
function mkdir($path, $chmod = false, $chown = false, $chgrp = false){ function mkdir($path, $chmod = false, $chown = false, $chgrp = false){
if( ! $chmod) if ( ! $chmod)
$chmod = $this->permission; $chmod = $this->permission;
if( ! @mkdir($path, $chmod) ) if ( ! @mkdir($path, $chmod) )
return false; return false;
if( $chown ) if ( $chown )
$this->chown($path, $chown); $this->chown($path, $chown);
if( $chgrp ) if ( $chgrp )
$this->chgrp($path, $chgrp); $this->chgrp($path, $chgrp);
return true; return true;
} }
function rmdir($path, $recursive = false) { function rmdir($path, $recursive = false) {
//Currently unused and untested, Use delete() instead. //Currently unused and untested, Use delete() instead.
if( ! $recursive ) if ( ! $recursive )
return @rmdir($path); return @rmdir($path);
//recursive: //recursive:
$filelist = $this->dirlist($path); $filelist = $this->dirlist($path);
foreach($filelist as $filename => $det) { foreach ($filelist as $filename => $det) {
if ( '/' == substr($filename, -1, 1) ) if ( '/' == substr($filename, -1, 1) )
$this->rmdir($path . '/' . $filename, $recursive); $this->rmdir($path . '/' . $filename, $recursive);
@rmdir($filename); @rmdir($filename);
@ -222,13 +224,13 @@ class WP_Filesystem_Direct extends WP_Filesystem_Base {
} }
function dirlist($path, $incdot = false, $recursive = false) { function dirlist($path, $incdot = false, $recursive = false) {
if( $this->is_file($path) ) { if ( $this->is_file($path) ) {
$limitFile = basename($path); $limitFile = basename($path);
$path = dirname($path); $path = dirname($path);
} else { } else {
$limitFile = false; $limitFile = false;
} }
if( ! $this->is_dir($path) ) if ( ! $this->is_dir($path) )
return false; return false;
$ret = array(); $ret = array();
@ -239,11 +241,11 @@ class WP_Filesystem_Direct extends WP_Filesystem_Base {
$struc = array(); $struc = array();
$struc['name'] = $entry; $struc['name'] = $entry;
if( '.' == $struc['name'] || '..' == $struc['name'] ) if ( '.' == $struc['name'] || '..' == $struc['name'] )
continue; //Do not care about these folders. continue; //Do not care about these folders.
if( '.' == $struc['name'][0] && !$incdot) if ( '.' == $struc['name'][0] && !$incdot)
continue; continue;
if( $limitFile && $struc['name'] != $limitFile) if ( $limitFile && $struc['name'] != $limitFile)
continue; continue;
$struc['perms'] = $this->gethchmod($path.'/'.$entry); $struc['perms'] = $this->gethchmod($path.'/'.$entry);
@ -258,7 +260,7 @@ class WP_Filesystem_Direct extends WP_Filesystem_Base {
$struc['type'] = $this->is_dir($path.'/'.$entry) ? 'd' : 'f'; $struc['type'] = $this->is_dir($path.'/'.$entry) ? 'd' : 'f';
if ( 'd' == $struc['type'] ) { if ( 'd' == $struc['type'] ) {
if( $recursive ) if ( $recursive )
$struc['files'] = $this->dirlist($path . '/' . $struc['name'], $incdot, $recursive); $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $incdot, $recursive);
else else
$struc['files'] = array(); $struc['files'] = array();

View File

@ -169,7 +169,7 @@ class WP_Filesystem_FTPext extends WP_Filesystem_Base {
} }
function getchmod($file) { function getchmod($file) {
$dir = $this->dirlist($file); $dir = $this->dirlist($file);
return $dir[$file]['permsn']; return $this->getnumchmodfromh( $dir[basename($file)]['perms'] );
} }
function group($file) { function group($file) {
$dir = $this->dirlist($file); $dir = $this->dirlist($file);
@ -187,7 +187,9 @@ class WP_Filesystem_FTPext extends WP_Filesystem_Base {
return ftp_rename($this->link, $source, $destination); return ftp_rename($this->link, $source, $destination);
} }
function delete($file,$recursive=false) { function delete($file ,$recursive = false ) {
if ( empty($file) )
return false;
if ( $this->is_file($file) ) if ( $this->is_file($file) )
return @ftp_delete($this->link, $file); return @ftp_delete($this->link, $file);
if ( !$recursive ) if ( !$recursive )
@ -321,11 +323,12 @@ class WP_Filesystem_FTPext extends WP_Filesystem_Base {
} }
function dirlist($path = '.', $incdot = false, $recursive = false) { function dirlist($path = '.', $incdot = false, $recursive = false) {
if( $this->is_file($path) ) {
$limitFile = basename($path); if ( substr($path, -1) !== '/') {
$path = dirname($path) . '/'; $limit = basename($path);
$path = trailingslashit(dirname($path));
} else { } else {
$limitFile = false; $limit = false;
} }
$list = @ftp_rawlist($this->link, '-a ' . $path, false); $list = @ftp_rawlist($this->link, '-a ' . $path, false);
@ -339,7 +342,10 @@ class WP_Filesystem_FTPext extends WP_Filesystem_Base {
if ( empty($entry) ) if ( empty($entry) )
continue; continue;
if ( '.' == $entry["name"] || '..' == $entry["name"] ) if ( '.' == $entry['name'] || '..' == $entry['name'] )
continue;
if ( $limit && $entry['name'] != $limit )
continue; continue;
$dirlist[ $entry['name'] ] = $entry; $dirlist[ $entry['name'] ] = $entry;

View File

@ -186,7 +186,7 @@ class WP_Filesystem_ftpsockets extends WP_Filesystem_Base {
function getchmod($file) { function getchmod($file) {
$dir = $this->dirlist($file); $dir = $this->dirlist($file);
return $dir[$file]['permsn']; return $this->getnumchmodfromh( $dir[basename($file)]['perms'] );
} }
function group($file) { function group($file) {
@ -281,21 +281,25 @@ class WP_Filesystem_ftpsockets extends WP_Filesystem_Base {
} }
function dirlist($path = '.', $incdot = false, $recursive = false ) { function dirlist($path = '.', $incdot = false, $recursive = false ) {
if( $this->is_file($path) ) {
$limitFile = basename($path); if ( substr($path, -1) !== '/') {
$path = dirname($path) . '/'; $limit = basename($path);
$path = trailingslashit(dirname($path));
} else { } else {
$limitFile = false; $limit = false;
} }
$list = $this->ftp->dirlist($path); $list = $this->ftp->dirlist($path);
if( ! $list ) if( ! $list )
return false; return false;
if( empty($list) ) if( empty($list) )
return array(); return array();
$ret = array(); $ret = array();
foreach ( $list as $struc ) { foreach ( $list as $struc ) {
if ( $limit && $struc['name'] != $limit )
continue;
if ( 'd' == $struc['type'] ) { if ( 'd' == $struc['type'] ) {
$struc['files'] = array(); $struc['files'] = array();