Make WordPress Core


Ignore:
Timestamp:
05/29/2008 05:29:32 PM (18 years ago)
Author:
ryan
Message:

Make WP_Filesystem work with new directory constants. Props DD32. fixes #7059

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/includes/class-wp-filesystem-ftpext.php

    r7999 r8009  
    11<?php
    2 class WP_Filesystem_FTPext{
     2class WP_Filesystem_FTPext extends WP_Filesystem_Base{
    33        var $link;
    44        var $timeout = 5;
    … …  
    66        var $options = array();
    77
    8         var $wp_base = '';
    98        var $permission = null;
    109
    … …  
    2524
    2625        function WP_Filesystem_FTPext($opt='') {
     26                $this->method = 'ftpext';
    2727                $this->errors = new WP_Error();
    2828
    … …  
    6161        }
    6262
    63         function connect(){
    64                 if ( $this->options['ssl'] && function_exists('ftp_ssl_connect') ) {
     63        function connect() {
     64                if ( $this->options['ssl'] && function_exists('ftp_ssl_connect') )
    6565                        $this->link = @ftp_ssl_connect($this->options['hostname'], $this->options['port'],$this->timeout);
    66                 } else {
     66                else
    6767                        $this->link = @ftp_connect($this->options['hostname'], $this->options['port'],$this->timeout);
    68                 }
    6968
    7069                if ( ! $this->link ) {
    … …  
    8180        }
    8281
    83         function setDefaultPermissions($perm){
     82        function setDefaultPermissions($perm) {
    8483                $this->permission = $perm;
    8584        }
    86 
    87         function find_base_dir($path = false, $base = '.',$echo = false, $loop = false) {
    88                 if (!$path)
    89                         $path = ABSPATH;
    90                
    91                 //Sanitize the Windows path formats, This allows easier conparison and aligns it to FTP output.
    92                 $path = str_replace('\\','/',$path); //windows: Straighten up the paths..
    93                 if( strpos($path, ':') ){ //Windows, Strip out the driveletter
    94                         if( preg_match("|.{1}\:(.+)|i", $path, $mat) )
    95                                 $path = $mat[1];
    96                 }
    9785       
    98                 //Set up the base directory (Which unless specified, is the current one)
    99                 if( empty( $base ) || '.' == $base ) $base = $this->cwd();
    100                 $base = trailingslashit($base);
    101 
    102                 //Can we see the Current directory as part of the ABSPATH?
    103                 $location = strpos($path, $base);
    104                 if( false !== $location ) {
    105                         $newbase = path_join($base, substr($path, $location + strlen($base)));
    106 
    107                         if( false !== $this->chdir($newbase) ){ //chdir sometimes returns null under certain circumstances, even when its changed correctly, FALSE will be returned if it doesnt change correctly.
    108                                 if($echo) printf( __('Changing to %s') . '<br/>', $newbase );
    109                                 //Check to see if it exists in that folder.
    110                                 if( $this->exists($newbase . 'wp-settings.php') ){
    111                                         if($echo) printf( __('Found %s'),  $newbase . 'wp-settings.php<br/>' );
    112                                         return $newbase;
    113                                 }       
    114                         }
    115                 }
    116        
    117                 //Ok, Couldnt do a magic location from that particular folder level
    118                
    119                 //Get a list of the files in the current directory, See if we can locate where we are in the folder stucture.
    120                 $files = $this->dirlist($base);
    121                
    122                 $arrPath = explode('/', $path);
    123                 foreach($arrPath as $key){
    124                         //Working from /home/ to /user/ to /wordpress/ see if that file exists within the current folder,
    125                         // If its found, change into it and follow through looking for it.
    126                         // If it cant find WordPress down that route, it'll continue onto the next folder level, and see if that matches, and so on.
    127                         // If it reaches the end, and still cant find it, it'll return false for the entire function.
    128                         if( isset($files[ $key ]) ){
    129                                 //Lets try that folder:
    130                                 $folder = path_join($base, $key);
    131                                 if($echo) printf( __('Changing to %s') . '<br/>', $folder );
    132                                 $ret = $this->find_base_dir( $path, $folder, $echo, $loop);
    133                                 if( $ret )
    134                                         return $ret;
    135                         }
    136                 }
    137                 //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.
    138                 if(isset( $files[ 'wp-settings.php' ]) ){
    139                         if($echo) printf( __('Found %s'),  $base . 'wp-settings.php<br/>' );
    140                         return $base;
    141                 }
    142                 if( $loop )
    143                         return false;//Prevent tihs function looping again.
    144                 //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.
    145                 return $this->find_base_dir($path, '/', $echo, true);
    146         }
    147 
    148         function get_base_dir($path = false, $base = '.', $echo = false){
    149                 if( defined('FTP_BASE') )
    150                         $this->wp_base = FTP_BASE;
    151                 if( empty($this->wp_base) )
    152                         $this->wp_base = $this->find_base_dir($path, $base, $echo);
    153                 return $this->wp_base;
    154         }
    155         function get_contents($file,$type='',$resumepos=0){
     86        function get_contents($file, $type = '', $resumepos = 0 ){
    15687                if( empty($type) ){
    15788                        $extension = substr(strrchr($file, "."), 1);
    … …  
    16192                if ( ! $temp )
    16293                        return false;
    163                 if( ! @ftp_fget($this->link,$temp,$file,$type,$resumepos) )
     94                if( ! @ftp_fget($this->link, $temp, $file, $type, $resumepos) )
    16495                        return false;
    16596                fseek($temp, 0); //Skip back to the start of the file being written to
    … …  
    171102                return $contents;
    172103        }
    173         function get_contents_array($file){
    174                 return explode("\n",$this->get_contents($file));
    175         }
    176         function put_contents($file,$contents,$type=''){
    177                 if( empty($type) ){
     104        function get_contents_array($file) {
     105                return explode("\n", $this->get_contents($file));
     106        }
     107        function put_contents($file, $contents, $type = '' ) {
     108                if( empty($type) ) {
    178109                        $extension = substr(strrchr($file, "."), 1);
    179110                        $type = isset($this->filetypes[ $extension ]) ? $this->filetypes[ $extension ] : FTP_ASCII;
    … …  
    182113                if ( ! $temp )
    183114                        return false;
    184                 fwrite($temp,$contents);
     115                fwrite($temp, $contents);
    185116                fseek($temp, 0); //Skip back to the start of the file being written to
    186                 $ret = @ftp_fput($this->link,$file,$temp,$type);
     117                $ret = @ftp_fput($this->link, $file, $temp, $type);
    187118                fclose($temp);
    188119                return $ret;
    189120        }
    190         function cwd(){
     121        function cwd() {
    191122                $cwd = ftp_pwd($this->link);
    192123                if( $cwd )
    … …  
    194125                return $cwd;
    195126        }
    196         function chdir($dir){
     127        function chdir($dir) {
    197128                return @ftp_chdir($dir);
    198129        }
    199         function chgrp($file,$group,$recursive=false){
    200                 return false;
    201         }
    202         function chmod($file,$mode=false,$recursive=false){
     130        function chgrp($file, $group, $recursive = false ) {
     131                return false;
     132        }
     133        function chmod($file, $mode = false, $recursive = false) {
    203134                if( ! $mode )
    204135                        $mode = $this->permission;
    … …  
    207138                if ( ! $this->exists($file) )
    208139                        return false;
    209                 if ( ! $recursive || ! $this->is_dir($file) ){
    210                         if (!function_exists('ftp_chmod'))
     140                if ( ! $recursive || ! $this->is_dir($file) ) {
     141                        if ( ! function_exists('ftp_chmod') )
    211142                                return @ftp_site($this->link, sprintf('CHMOD %o %s', $mode, $file));
    212                         return @ftp_chmod($this->link,$mode,$file);
     143                        return @ftp_chmod($this->link, $mode, $file);
    213144                }
    214145                //Is a directory, and we want recursive
    215146                $filelist = $this->dirlist($file);
    216147                foreach($filelist as $filename){
    217                         $this->chmod($file.'/'.$filename,$mode,$recursive);
    218                 }
    219                 return true;
    220         }
    221         function chown($file,$owner,$recursive=false){
    222                 return false;
    223         }
    224         function owner($file){
     148                        $this->chmod($file . '/' . $filename, $mode, $recursive);
     149                }
     150                return true;
     151        }
     152        function chown($file, $owner, $recursive = false ) {
     153                return false;
     154        }
     155        function owner($file) {
    225156                $dir = $this->dirlist($file);
    226157                return $dir[$file]['owner'];
    227158        }
    228         function getchmod($file){
     159        function getchmod($file) {
    229160                $dir = $this->dirlist($file);
    230161                return $dir[$file]['permsn'];
    231162        }
    232         function gethchmod($file){
    233                 //From the PHP.net page for ...?
    234                 $perms = $this->getchmod($file);
    235                 if (($perms & 0xC000) == 0xC000) {
    236                         // Socket
    237                         $info = 's';
    238                 } elseif (($perms & 0xA000) == 0xA000) {
    239                         // Symbolic Link
    240                         $info = 'l';
    241                 } elseif (($perms & 0x8000) == 0x8000) {
    242                         // Regular
    243                         $info = '-';
    244                 } elseif (($perms & 0x6000) == 0x6000) {
    245                         // Block special
    246                         $info = 'b';
    247                 } elseif (($perms & 0x4000) == 0x4000) {
    248                         // Directory
    249                         $info = 'd';
    250                 } elseif (($perms & 0x2000) == 0x2000) {
    251                         // Character special
    252                         $info = 'c';
    253                 } elseif (($perms & 0x1000) == 0x1000) {
    254                         // FIFO pipe
    255                         $info = 'p';
    256                 } else {
    257                         // Unknown
    258                         $info = 'u';
    259                 }
    260 
    261                 // Owner
    262                 $info .= (($perms & 0x0100) ? 'r' : '-');
    263                 $info .= (($perms & 0x0080) ? 'w' : '-');
    264                 $info .= (($perms & 0x0040) ?
    265                                         (($perms & 0x0800) ? 's' : 'x' ) :
    266                                         (($perms & 0x0800) ? 'S' : '-'));
    267 
    268                 // Group
    269                 $info .= (($perms & 0x0020) ? 'r' : '-');
    270                 $info .= (($perms & 0x0010) ? 'w' : '-');
    271                 $info .= (($perms & 0x0008) ?
    272                                         (($perms & 0x0400) ? 's' : 'x' ) :
    273                                         (($perms & 0x0400) ? 'S' : '-'));
    274 
    275                 // World
    276                 $info .= (($perms & 0x0004) ? 'r' : '-');
    277                 $info .= (($perms & 0x0002) ? 'w' : '-');
    278                 $info .= (($perms & 0x0001) ?
    279                                         (($perms & 0x0200) ? 't' : 'x' ) :
    280                                         (($perms & 0x0200) ? 'T' : '-'));
    281                 return $info;
    282         }
    283         function getnumchmodfromh($mode) {
    284                 $realmode = "";
    285                 $legal =  array("","w","r","x","-");
    286                 $attarray = preg_split("//",$mode);
    287                 for($i=0;$i<count($attarray);$i++){
    288                    if($key = array_search($attarray[$i],$legal)){
    289                            $realmode .= $legal[$key];
    290                    }
    291                 }
    292                 $mode = str_pad($realmode,9,'-');
    293                 $trans = array('-'=>'0','r'=>'4','w'=>'2','x'=>'1');
    294                 $mode = strtr($mode,$trans);
    295                 $newmode = '';
    296                 $newmode .= $mode[0]+$mode[1]+$mode[2];
    297                 $newmode .= $mode[3]+$mode[4]+$mode[5];
    298                 $newmode .= $mode[6]+$mode[7]+$mode[8];
    299                 return $newmode;
    300         }
    301         function group($file){
     163        function group($file) {
    302164                $dir = $this->dirlist($file);
    303165                return $dir[$file]['group'];
    304166        }
    305         function copy($source,$destination,$overwrite=false){
     167        function copy($source, $destination, $overwrite = false ) {
    306168                if( ! $overwrite && $this->exists($destination) )
    307169                        return false;
    … …  
    309171                if( false === $content)
    310172                        return false;
    311                 return $this->put_contents($destination,$content);
    312         }
    313         function move($source,$destination,$overwrite=false){
    314                 return ftp_rename($this->link,$source,$destination);
     173                return $this->put_contents($destination, $content);
     174        }
     175        function move($source, $destination, $overwrite = false) {
     176                return ftp_rename($this->link, $source, $destination);
    315177        }
    316178
    317179        function delete($file,$recursive=false) {
    318180                if ( $this->is_file($file) )
    319                         return @ftp_delete($this->link,$file);
     181                        return @ftp_delete($this->link, $file);
    320182                if ( !$recursive )
    321                         return @ftp_rmdir($this->link,$file);
     183                        return @ftp_rmdir($this->link, $file);
    322184                $filelist = $this->dirlist($file);
    323185                foreach ((array) $filelist as $filename => $fileinfo) {
    324                         $this->delete($file.'/'.$filename,$recursive);
    325                 }
    326                 return @ftp_rmdir($this->link,$file);
    327         }
    328 
    329         function exists($file){
    330                 $list = ftp_rawlist($this->link,$file,false);
     186                        $this->delete($file . '/' . $filename, $recursive);
     187                }
     188                return @ftp_rmdir($this->link, $file);
     189        }
     190
     191        function exists($file) {
     192                $list = ftp_rawlist($this->link, $file, false);
    331193                if( ! $list )
    332194                        return false;
    333195                return count($list) == 1 ? true : false;
    334196        }
    335         function is_file($file){
     197        function is_file($file) {
    336198                return $this->is_dir($file) ? false : true;
    337199        }
    338         function is_dir($path){
     200        function is_dir($path) {
    339201                $cwd = $this->cwd();
    340202                $result = @ftp_chdir($this->link, $path);
    341                 if( $result && $path == $this->cwd() ||
    342                         $this->cwd() != $cwd ) {
     203                if( $result && $path == $this->cwd() || $this->cwd() != $cwd ) {
    343204                        @ftp_chdir($this->link, $cwd);
    344205                        return true;
    … …  
    346207                return false;
    347208        }
    348         function is_readable($file){
     209        function is_readable($file) {
    349210                //Get dir list, Check if the file is writable by the current user??
    350211                return true;
    351212        }
    352         function is_writable($file){
     213        function is_writable($file) {
    353214                //Get dir list, Check if the file is writable by the current user??
    354215                return true;
    355216        }
    356         function atime($file){
    357                 return false;
    358         }
    359         function mtime($file){
     217        function atime($file) {
     218                return false;
     219        }
     220        function mtime($file) {
    360221                return ftp_mdtm($this->link, $file);
    361222        }
    362         function size($file){
     223        function size($file) {
    363224                return ftp_size($this->link, $file);
    364225        }
    365         function touch($file,$time=0,$atime=0){
    366                 return false;
    367         }
    368         function mkdir($path,$chmod=false,$chown=false,$chgrp=false){
     226        function touch($file, $time = 0, $atime = 0) {
     227                return false;
     228        }
     229        function mkdir($path, $chmod = false, $chown = false, $chgrp = false) {
    369230                if( !@ftp_mkdir($this->link, $path) )
    370231                        return false;
    … …  
    377238                return true;
    378239        }
    379         function rmdir($path,$recursive=false){
     240        function rmdir($path, $recursive = false) {
    380241                if( ! $recursive )
    381242                        return @ftp_rmdir($this->link, $path);
    … …  
    389250        function parselisting($line) {
    390251                $is_windows = ($this->OS_remote == FTP_OS_Windows);
    391                 if ($is_windows && preg_match("/([0-9]{2})-([0-9]{2})-([0-9]{2}) +([0-9]{2}):([0-9]{2})(AM|PM) +([0-9]+|<DIR>) +(.+)/",$line,$lucifer)) {
     252                if ($is_windows && preg_match("/([0-9]{2})-([0-9]{2})-([0-9]{2}) +([0-9]{2}):([0-9]{2})(AM|PM) +([0-9]+|<DIR>) +(.+)/", $line, $lucifer)) {
    392253                        $b = array();
    393                         if ($lucifer[3]<70) { $lucifer[3]+=2000; } else { $lucifer[3]+=1900; } // 4digit year fix
     254                        if ($lucifer[3]<70) { $lucifer[3] +=2000; } else { $lucifer[3]+=1900; } // 4digit year fix
    394255                        $b['isdir'] = ($lucifer[7]=="<DIR>");
    395256                        if ( $b['isdir'] )
    … …  
    449310        }
    450311
    451         function dirlist($path='.',$incdot=false,$recursive=false){
    452                 if( $this->is_file($path) ){
     312        function dirlist($path = '.', $incdot = false, $recursive = false) {
     313                if( $this->is_file($path) ) {
    453314                        $limitFile = basename($path);
    454315                        $path = dirname($path) . '/';
    … …  
    457318                }
    458319
    459                 $list = @ftp_rawlist($this->link , '-a ' . $path, false);
     320                $list = @ftp_rawlist($this->link, '-a ' . $path, false);
    460321
    461322                if ( $list === false )
    … …  
    468329                                continue;
    469330
    470                         if ( $entry["name"]=="." or $entry["name"]==".." )
     331                        if ( '.' == $entry["name"] || '..' == $entry["name"] )
    471332                                continue;
    472333
    473                         $dirlist[$entry['name']] = $entry;
     334                        $dirlist[ $entry['name'] ] = $entry;
    474335                }
    475336
    … …  
    489350                                        if( '.' != $struc['name'] && '..' != $struc['name'] ){ //Ok, It isnt a special folder
    490351                                                if ($recursive)
    491                                                         $struc['files'] = $this->dirlist($path.'/'.$struc['name'],$incdot,$recursive);
     352                                                        $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $incdot, $recursive);
    492353                                        }
    493354                                } else { //No dots
    494355                                        if ($recursive)
    495                                                 $struc['files'] = $this->dirlist($path.'/'.$struc['name'],$incdot,$recursive);
     356                                                $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $incdot, $recursive);
    496357                                }
    497358                        }
Note: See TracChangeset for help on using the changeset viewer.