Make WordPress Core

Ticket #7059: 7059.2.diff

File 7059.2.diff, 52.9 KB (added by DD32, 18 years ago)

oops, a extra function sliped in :)

  • wp-admin/includes/class-wp-filesystem-base.php

     
     1<?php
     2class WP_Filesystem_Base{
     3        var $verbose = true;
     4        var $cache = array();
     5       
     6        var $method = '';
     7       
     8        function abspath() {
     9                if ( defined('FTP_BASE') && strpos($this->method, 'ftp') !== false )
     10                        return FTP_BASE;
     11                return $this->find_folder(ABSPATH);
     12        }
     13        function wp_content_dir() {
     14                if ( defined('FTP_CONTENT_DIR') && strpos($this->method, 'ftp') !== false )
     15                        return FTP_CONTENT_DIR;
     16                return $this->find_folder(WP_CONTENT_DIR);
     17        }
     18        function wp_plugins_dir() {
     19                if ( defined('FTP_PLUGIN_DIR') && strpos($this->method, 'ftp') !== false )
     20                        return FTP_PLUGIN_DIR;
     21                return $this->find_folder(WP_PLUGIN_DIR);
     22        }
     23        function wp_themes_dir() {
     24                return $this->wp_content_dir() . '/themes';
     25        }
     26        //Back compat: use abspath() or wp_*_dir
     27        function find_base_dir($base = '.', $echo = false) {
     28                $this->verbose = $echo;
     29                return $this->abspath();
     30        }
     31        //Back compat: use ::abspath() or ::wp_*_dir
     32        function get_base_dir($base = '.', $echo = false) {
     33                $this->verbose = $echo;
     34                return $this->abspath();
     35        }
     36       
     37        function find_folder($folder) {
     38                $folder = str_replace('\\', '/', $folder); //Windows Sanitiation
     39                if ( isset($this->cache[ $folder ] ) )
     40                        return $this->cache[ $folder ];
     41
     42                if ( $this->exists($folder) ) { //Folder exists at that absolute path.
     43                        $this->cache[ $folder ] = $folder;
     44                        return $folder;
     45                }
     46                if( $return = $this->search_for_folder($folder) )
     47                        $this->cache[ $folder ] = $return;
     48                return $return;
     49        }
     50       
     51        // Assumes $folder is windows sanitized;
     52        // Assumes that the drive letter is safe to be stripped off, Should not be a problem for windows servers.
     53        function search_for_folder($folder, $base = '.', $loop = false ) {
     54                if ( empty( $base ) || '.' == $base )
     55                        $base = trailingslashit($this->cwd());
     56               
     57                $folder = preg_replace('|^([a-z]{1}):|i', '', $folder); //Strip out windows driveletter if its there.
     58               
     59                $folder_parts = explode('/', $folder);
     60                $last_path = $folder_parts[ count($folder_parts) - 1 ];
     61               
     62                $files = $this->dirlist( $base );
     63               
     64                foreach ( $folder_parts as $key ) {
     65                        if ( $key == $last_path )
     66                                continue; //We want this to be caught by the next code block.
     67
     68                        //Working from /home/ to /user/ to /wordpress/ see if that file exists within the current folder,
     69                        // If its found, change into it and follow through looking for it.
     70                        // If it cant find WordPress down that route, it'll continue onto the next folder level, and see if that matches, and so on.
     71                        // If it reaches the end, and still cant find it, it'll return false for the entire function.
     72                        if( isset($files[ $key ]) ){
     73                                //Lets try that folder:
     74                                $newdir = trailingslashit(path_join($base, $key));
     75                                if( $this->verbose )
     76                                        printf( __('Changing to %s') . '<br/>', $newdir );
     77                                if( $ret = $this->search_for_folder( $folder, $newdir, $loop) )
     78                                        return $ret;
     79                        }
     80                }
     81               
     82                //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.
     83                if(isset( $files[ $last_path ] ) ) {
     84                        if( $this->verbose )
     85                                printf( __('Found %s') . '<br/>',  $base . $last_path );
     86                        return $base . $last_path;
     87                }
     88                if( $loop )
     89                        return false;//Prevent tihs function looping again.
     90                //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.
     91                return $this->search_for_folder($folder, '/', true);
     92               
     93        }
     94       
     95        //Common Helper functions.
     96        function gethchmod($file){
     97                //From the PHP.net page for ...?
     98                $perms = $this->getchmod($file);
     99                if (($perms & 0xC000) == 0xC000) // Socket
     100                        $info = 's';
     101                elseif (($perms & 0xA000) == 0xA000) // Symbolic Link
     102                        $info = 'l';
     103                elseif (($perms & 0x8000) == 0x8000) // Regular
     104                        $info = '-';
     105                elseif (($perms & 0x6000) == 0x6000) // Block special
     106                        $info = 'b';
     107                elseif (($perms & 0x4000) == 0x4000) // Directory
     108                        $info = 'd';
     109                elseif (($perms & 0x2000) == 0x2000) // Character special
     110                        $info = 'c';
     111                elseif (($perms & 0x1000) == 0x1000)// FIFO pipe
     112                        $info = 'p';
     113                else // Unknown
     114                        $info = 'u';
     115
     116                // Owner
     117                $info .= (($perms & 0x0100) ? 'r' : '-');
     118                $info .= (($perms & 0x0080) ? 'w' : '-');
     119                $info .= (($perms & 0x0040) ?
     120                                        (($perms & 0x0800) ? 's' : 'x' ) :
     121                                        (($perms & 0x0800) ? 'S' : '-'));
     122
     123                // Group
     124                $info .= (($perms & 0x0020) ? 'r' : '-');
     125                $info .= (($perms & 0x0010) ? 'w' : '-');
     126                $info .= (($perms & 0x0008) ?
     127                                        (($perms & 0x0400) ? 's' : 'x' ) :
     128                                        (($perms & 0x0400) ? 'S' : '-'));
     129
     130                // World
     131                $info .= (($perms & 0x0004) ? 'r' : '-');
     132                $info .= (($perms & 0x0002) ? 'w' : '-');
     133                $info .= (($perms & 0x0001) ?
     134                                        (($perms & 0x0200) ? 't' : 'x' ) :
     135                                        (($perms & 0x0200) ? 'T' : '-'));
     136                return $info;
     137        }
     138        function getnumchmodfromh($mode) {
     139                $realmode = "";
     140                $legal =  array("", "w", "r", "x", "-");
     141                $attarray = preg_split("//", $mode);
     142
     143                for($i=0; $i < count($attarray); $i++)
     144                   if($key = array_search($attarray[$i], $legal))
     145                           $realmode .= $legal[$key];
     146                           
     147                $mode = str_pad($realmode, 9, '-');
     148                $trans = array('-'=>'0', 'r'=>'4', 'w'=>'2', 'x'=>'1');
     149                $mode = strtr($mode,$trans);
     150               
     151                $newmode = '';
     152                $newmode .= $mode[0] + $mode[1] + $mode[2];
     153                $newmode .= $mode[3] + $mode[4] + $mode[5];
     154                $newmode .= $mode[6] + $mode[7] + $mode[8];
     155                return $newmode;
     156        }
     157}
     158?>
     159 No newline at end of file
  • wp-admin/includes/class-wp-filesystem-direct.php

     
    11<?php
    22
    3 class WP_Filesystem_Direct{
     3class WP_Filesystem_Direct extends WP_Filesystem_Base{
    44        var $permission = null;
    55        var $errors = array();
    6         function WP_Filesystem_Direct($arg){
     6        function WP_Filesystem_Direct($arg) {
     7                $this->method = 'direct';
    78                $this->errors = new WP_Error();
    89                $this->permission = umask();
    910        }
    10         function connect(){
     11        function connect() {
    1112                return true;
    1213        }
    13         function setDefaultPermissions($perm){
     14        function setDefaultPermissions($perm) {
    1415                $this->permission = $perm;
    1516        }
    16         function find_base_dir($base = '.', $echo = false){
    17                 return str_replace('\\','/',ABSPATH);
    18         }
    19         function get_base_dir($base = '.', $echo = false){
    20                 return $this->find_base_dir($base, $echo);
    21         }
    2217        function get_contents($file){
    2318                return @file_get_contents($file);
    2419        }
    25         function get_contents_array($file){
     20        function get_contents_array($file) {
    2621                return @file($file);
    2722        }
    28         function put_contents($file,$contents,$mode=false,$type=''){
    29                 if ( ! ($fp = @fopen($file,'w'.$type)) )
     23        function put_contents($file,$contents,$mode=false,$type='') {
     24                if ( ! ($fp = @fopen($file, 'w' . $type)) )
    3025                        return false;
    3126                @fwrite($fp,$contents);
    3227                @fclose($fp);
     
    3631        function cwd(){
    3732                return @getcwd();
    3833        }
    39         function chdir($dir){
     34        function chdir($dir) {
    4035                return @chdir($dir);
    4136        }
    42         function chgrp($file,$group,$recursive=false){
     37        function chgrp($file,$group,$recursive=false) {
    4338                if( ! $this->exists($file) )
    4439                        return false;
    4540                if( ! $recursive )
    46                         return @chgrp($file,$group);
     41                        return @chgrp($file, $group);
    4742                if( ! $this->is_dir($file) )
    48                         return @chgrp($file,$group);
     43                        return @chgrp($file, $group);
    4944                //Is a directory, and we want recursive
    5045                $file = trailingslashit($file);
    5146                $filelist = $this->dirlist($file);
     
    5449
    5550                return true;
    5651        }
    57         function chmod($file,$mode=false,$recursive=false){
     52        function chmod($file,$mode=false,$recursive=false) {
    5853                if( ! $mode )
    5954                        $mode = $this->permission;
    6055                if( ! $this->exists($file) )
     
    6257                if( ! $recursive )
    6358                        return @chmod($file,$mode);
    6459                if( ! $this->is_dir($file) )
    65                         return @chmod($file,$mode);
     60                        return @chmod($file, $mode);
    6661                //Is a directory, and we want recursive
    6762                $file = trailingslashit($file);
    6863                $filelist = $this->dirlist($file);
     
    7166
    7267                return true;
    7368        }
    74         function chown($file,$owner,$recursive=false){
     69        function chown($file, $owner, $recursive = false) {
    7570                if( ! $this->exists($file) )
    7671                        return false;
    7772                if( ! $recursive )
    78                         return @chown($file,$owner);
     73                        return @chown($file, $owner);
    7974                if( ! $this->is_dir($file) )
    80                         return @chown($file,$owner);
     75                        return @chown($file, $owner);
    8176                //Is a directory, and we want recursive
    8277                $filelist = $this->dirlist($file);
    8378                foreach($filelist as $filename){
    84                         $this->chown($file.'/'.$filename,$owner,$recursive);
     79                        $this->chown($file . '/' . $filename, $owner, $recursive);
    8580                }
    8681                return true;
    8782        }
    88         function owner($file){
     83        function owner($file) {
    8984                $owneruid = @fileowner($file);
    9085                if( ! $owneruid )
    9186                        return false;
    92                 if( !function_exists('posix_getpwuid') )
     87                if( ! function_exists('posix_getpwuid') )
    9388                        return $owneruid;
    9489                $ownerarray = posix_getpwuid($owneruid);
    9590                return $ownerarray['name'];
    9691        }
    97         function getchmod($file){
     92        function getchmod($file) {
    9893                return @fileperms($file);
    9994        }
    100         function gethchmod($file){
    101                 //From the PHP.net page for ...?
    102                 $perms = $this->getchmod($file);
    103                 if (($perms & 0xC000) == 0xC000) {
    104                         // Socket
    105                         $info = 's';
    106                 } elseif (($perms & 0xA000) == 0xA000) {
    107                         // Symbolic Link
    108                         $info = 'l';
    109                 } elseif (($perms & 0x8000) == 0x8000) {
    110                         // Regular
    111                         $info = '-';
    112                 } elseif (($perms & 0x6000) == 0x6000) {
    113                         // Block special
    114                         $info = 'b';
    115                 } elseif (($perms & 0x4000) == 0x4000) {
    116                         // Directory
    117                         $info = 'd';
    118                 } elseif (($perms & 0x2000) == 0x2000) {
    119                         // Character special
    120                         $info = 'c';
    121                 } elseif (($perms & 0x1000) == 0x1000) {
    122                         // FIFO pipe
    123                         $info = 'p';
    124                 } else {
    125                         // Unknown
    126                         $info = 'u';
    127                 }
    128 
    129                 // Owner
    130                 $info .= (($perms & 0x0100) ? 'r' : '-');
    131                 $info .= (($perms & 0x0080) ? 'w' : '-');
    132                 $info .= (($perms & 0x0040) ?
    133                                         (($perms & 0x0800) ? 's' : 'x' ) :
    134                                         (($perms & 0x0800) ? 'S' : '-'));
    135 
    136                 // Group
    137                 $info .= (($perms & 0x0020) ? 'r' : '-');
    138                 $info .= (($perms & 0x0010) ? 'w' : '-');
    139                 $info .= (($perms & 0x0008) ?
    140                                         (($perms & 0x0400) ? 's' : 'x' ) :
    141                                         (($perms & 0x0400) ? 'S' : '-'));
    142 
    143                 // World
    144                 $info .= (($perms & 0x0004) ? 'r' : '-');
    145                 $info .= (($perms & 0x0002) ? 'w' : '-');
    146                 $info .= (($perms & 0x0001) ?
    147                                         (($perms & 0x0200) ? 't' : 'x' ) :
    148                                         (($perms & 0x0200) ? 'T' : '-'));
    149                 return $info;
    150         }
    151         function getnumchmodfromh($mode) {
    152                 $realmode = "";
    153                 $legal =  array("","w","r","x","-");
    154                 $attarray = preg_split("//",$mode);
    155                 for($i=0;$i<count($attarray);$i++){
    156                    if($key = array_search($attarray[$i],$legal)){
    157                            $realmode .= $legal[$key];
    158                    }
    159                 }
    160                 $mode = str_pad($realmode,9,'-');
    161                 $trans = array('-'=>'0','r'=>'4','w'=>'2','x'=>'1');
    162                 $mode = strtr($mode,$trans);
    163                 $newmode = '';
    164                 $newmode .= $mode[0]+$mode[1]+$mode[2];
    165                 $newmode .= $mode[3]+$mode[4]+$mode[5];
    166                 $newmode .= $mode[6]+$mode[7]+$mode[8];
    167                 return $newmode;
    168         }
    169         function group($file){
     95        function group($file) {
    17096                $gid = @filegroup($file);
    17197                if( ! $gid )
    17298                        return false;
    173                 if( !function_exists('posix_getgrgid') )
     99                if( ! function_exists('posix_getgrgid') )
    174100                        return $gid;
    175101                $grouparray = posix_getgrgid($gid);
    176102                return $grouparray['name'];
    177103        }
    178104
    179         function copy($source,$destination,$overwrite=false){
     105        function copy($source, $destination, $overwrite = false) {
    180106                if( ! $overwrite && $this->exists($destination) )
    181107                        return false;
    182                 return copy($source,$destination);
     108                return copy($source, $destination);
    183109        }
    184110
    185         function move($source,$destination,$overwrite=false){
     111        function move($source, $destination, $overwrite = false) {
    186112                //Possible to use rename()?
    187                 if( $this->copy($source,$destination,$overwrite) && $this->exists($destination) ){
     113                if( $this->copy($source, $destination, $overwrite) && $this->exists($destination) ){
    188114                        $this->delete($source);
    189115                        return true;
    190116                } else {
     
    192118                }
    193119        }
    194120
    195         function delete($file, $recursive=false){
    196                 $file = str_replace('\\','/',$file); //for win32, occasional problems deleteing files otherwise
     121        function delete($file, $recursive = false) {
     122                $file = str_replace('\\', '/', $file); //for win32, occasional problems deleteing files otherwise
    197123
    198124                if( $this->is_file($file) )
    199125                        return @unlink($file);
    200                 if( !$recursive && $this->is_dir($file) )
     126                if( ! $recursive && $this->is_dir($file) )
    201127                        return @rmdir($file);
    202128
    203129                //At this point its a folder, and we're in recursive mode
     
    206132
    207133                $retval = true;
    208134                if( is_array($filelist) ) //false if no files, So check first.
    209                         foreach($filelist as $filename=>$fileinfo)
     135                        foreach($filelist as $filename => $fileinfo)
    210136                                if( ! $this->delete($file . $filename, $recursive) )
    211137                                        $retval = false;
    212138
     
    215141                return $retval;
    216142        }
    217143
    218         function exists($file){
     144        function exists($file) {
    219145                return @file_exists($file);
    220146        }
    221147
    222         function is_file($file){
     148        function is_file($file) {
    223149                return @is_file($file);
    224150        }
    225151
    226         function is_dir($path){
     152        function is_dir($path) {
    227153                return @is_dir($path);
    228154        }
    229155
    230         function is_readable($file){
     156        function is_readable($file) {
    231157                return @is_readable($file);
    232158        }
    233159
    234         function is_writable($file){
     160        function is_writable($file) {
    235161                return @is_writable($file);
    236162        }
    237163
    238         function atime($file){
     164        function atime($file) {
    239165                return @fileatime($file);
    240166        }
    241167
    242         function mtime($file){
     168        function mtime($file) {
    243169                return @filemtime($file);
    244170        }
    245         function size($file){
     171        function size($file) {
    246172                return @filesize($file);
    247173        }
    248174
     
    251177                        $time = time();
    252178                if($atime == 0)
    253179                        $atime = time();
    254                 return @touch($file,$time,$atime);
     180                return @touch($file, $time, $atime);
    255181        }
    256182
    257183        function mkdir($path, $chmod = false, $chown = false, $chgrp = false){
    258184                if( ! $chmod)
    259185                        $chmod = $this->permission;
    260186
    261                 if( !@mkdir($path,$chmod) )
     187                if( ! @mkdir($path, $chmod) )
    262188                        return false;
    263189                if( $chown )
    264                         $this->chown($path,$chown);
     190                        $this->chown($path, $chown);
    265191                if( $chgrp )
    266                         $this->chgrp($path,$chgrp);
     192                        $this->chgrp($path, $chgrp);
    267193                return true;
    268194        }
    269195
    270         function rmdir($path,$recursive=false){
     196        function rmdir($path, $recursive = false) {
    271197                //Currently unused and untested, Use delete() instead.
    272198                if( ! $recursive )
    273199                        return @rmdir($path);
    274200                //recursive:
    275201                $filelist = $this->dirlist($path);
    276                 foreach($filelist as $filename=>$det){
    277                         if ( '/' == substr($filename,-1,1) )
    278                                 $this->rmdir($path.'/'.$filename,$recursive);
     202                foreach($filelist as $filename => $det) {
     203                        if ( '/' == substr($filename, -1, 1) )
     204                                $this->rmdir($path . '/' . $filename, $recursive);
    279205                        @rmdir($filename);
    280206                }
    281207                return @rmdir($path);
    282208        }
    283209
    284         function dirlist($path,$incdot=false,$recursive=false){
    285                 if( $this->is_file($path) ){
     210        function dirlist($path, $incdot = false, $recursive = false) {
     211                if( $this->is_file($path) ) {
    286212                        $limitFile = basename($path);
    287213                        $path = dirname($path);
    288214                } else {
     
    293219
    294220                $ret = array();
    295221                $dir = dir($path);
    296                 while (false !== ($entry = $dir->read())) {
     222                while (false !== ($entry = $dir->read()) ) {
    297223                        $struc = array();
    298                         $struc['name']          = $entry;
     224                        $struc['name'] = $entry;
    299225
    300226                        if( '.' == $struc['name'] || '..' == $struc['name'] )
    301227                                continue; //Do not care about these folders.
     
    315241                        $struc['time']          = date('h:i:s',$struc['lastmodunix']);
    316242                        $struc['type']          = $this->is_dir($path.'/'.$entry) ? 'd' : 'f';
    317243
    318                         if ('d' == $struc['type'] ){
     244                        if ( 'd' == $struc['type'] ) {
    319245                                if( $recursive )
    320                                         $struc['files'] = $this->dirlist($path.'/'.$struc['name'], $incdot, $recursive);
     246                                        $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $incdot, $recursive);
    321247                                else
    322248                                        $struc['files'] = array();
    323249                        }
     
    328254                unset($dir);
    329255                return $ret;
    330256        }
    331 
    332         function __destruct(){
    333                 return;
    334         }
    335257}
    336258?>
  • wp-admin/includes/class-wp-filesystem-ftpext.php

     
    11<?php
    2 class WP_Filesystem_FTPext{
     2class WP_Filesystem_FTPext extends WP_Filesystem_Base{
    33        var $link;
    44        var $timeout = 5;
    55        var $errors = array();
    66        var $options = array();
    77
    8         var $wp_base = '';
    98        var $permission = null;
    109
    1110        var $filetypes = array(
     
    2423                                                        );
    2524
    2625        function WP_Filesystem_FTPext($opt='') {
     26                $this->method = 'ftpext';
    2727                $this->errors = new WP_Error();
    2828
    2929                //Check if possible to use ftp functions.
     
    6060                $this->options['ssl'] = ( !empty($opt['ssl']) );
    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 ) {
    7170                        $this->errors->add('connect', sprintf(__('Failed to connect to FTP Server %1$s:%2$s'), $this->options['hostname'], $this->options['port']));
     
    8079                return true;
    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);
    15889                        $type = isset($this->filetypes[ $extension ]) ? $this->filetypes[ $extension ] : FTP_ASCII;
     
    16091                $temp = tmpfile();
    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
    16697                $contents = '';
     
    170101                fclose($temp);
    171102                return $contents;
    172103        }
    173         function get_contents_array($file){
    174                 return explode("\n",$this->get_contents($file));
     104        function get_contents_array($file) {
     105                return explode("\n", $this->get_contents($file));
    175106        }
    176         function put_contents($file,$contents,$type=''){
    177                 if( empty($type) ){
     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;
    180111                }
    181112                $temp = tmpfile();
    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 )
    193124                        $cwd = trailingslashit($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){
     130        function chgrp($file, $group, $recursive = false ) {
    200131                return false;
    201132        }
    202         function chmod($file,$mode=false,$recursive=false){
     133        function chmod($file, $mode = false, $recursive = false) {
    203134                if( ! $mode )
    204135                        $mode = $this->permission;
    205136                if( ! $mode )
    206137                        return false;
    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);
     148                        $this->chmod($file . '/' . $filename, $mode, $recursive);
    218149                }
    219150                return true;
    220151        }
    221         function chown($file,$owner,$recursive=false){
     152        function chown($file, $owner, $recursive = false ) {
    222153                return false;
    223154        }
    224         function owner($file){
     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;
    308170                $content = $this->get_contents($source);
    309171                if( false === $content)
    310172                        return false;
    311                 return $this->put_contents($destination,$content);
     173                return $this->put_contents($destination, $content);
    312174        }
    313         function move($source,$destination,$overwrite=false){
    314                 return ftp_rename($this->link,$source,$destination);
     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);
     186                        $this->delete($file . '/' . $filename, $recursive);
    325187                }
    326                 return @ftp_rmdir($this->link,$file);
     188                return @ftp_rmdir($this->link, $file);
    327189        }
    328190
    329         function exists($file){
    330                 $list = ftp_rawlist($this->link,$file,false);
     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;
    345206                }
    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){
     217        function atime($file) {
    357218                return false;
    358219        }
    359         function mtime($file){
     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){
     226        function touch($file, $time = 0, $atime = 0) {
    366227                return false;
    367228        }
    368         function mkdir($path,$chmod=false,$chown=false,$chgrp=false){
     229        function mkdir($path, $chmod = false, $chown = false, $chgrp = false) {
    369230                if( !@ftp_mkdir($this->link, $path) )
    370231                        return false;
    371232                if( $chmod )
     
    376237                        $this->chgrp($path, $chgrp);
    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);
    382243
     
    388249
    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'] )
    396257                                $b['type'] = 'd';
     
    448309                return $b;
    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) . '/';
    455316                } else {
    456317                        $limitFile = false;
    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 )
    462323                        return false;
     
    467328                        if ( empty($entry) )
    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
    476337                if ( ! $dirlist )
     
    488349                                        //We're including the doted starts
    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                        }
    498359                        //File
  • wp-admin/includes/class-wp-filesystem-ftpsockets.php

     
    55        var $errors;
    66        var $options = array();
    77
    8         var $wp_base = '';
    98        var $permission = null;
    109
    1110        var $filetypes = array(
    12                                                         'php'=>FTP_ASCII,
    13                                                         'css'=>FTP_ASCII,
    14                                                         'txt'=>FTP_ASCII,
    15                                                         'js'=>FTP_ASCII,
    16                                                         'html'=>FTP_ASCII,
    17                                                         'htm'=>FTP_ASCII,
    18                                                         'xml'=>FTP_ASCII,
     11                                                        'php' => FTP_ASCII,
     12                                                        'css' => FTP_ASCII,
     13                                                        'txt' => FTP_ASCII,
     14                                                        'js'  => FTP_ASCII,
     15                                                        'html'=> FTP_ASCII,
     16                                                        'htm' => FTP_ASCII,
     17                                                        'xml' => FTP_ASCII,
    1918
    20                                                         'jpg'=>FTP_BINARY,
    21                                                         'png'=>FTP_BINARY,
    22                                                         'gif'=>FTP_BINARY,
    23                                                         'bmp'=>FTP_BINARY
     19                                                        'jpg' => FTP_BINARY,
     20                                                        'png' => FTP_BINARY,
     21                                                        'gif' => FTP_BINARY,
     22                                                        'bmp' => FTP_BINARY
    2423                                                        );
    2524
    2625        function WP_Filesystem_ftpsockets($opt='') {
     26                $this->method = 'ftpsockets';
    2727                $this->errors = new WP_Error();
    2828
    2929                //Check if possible to use ftp functions.
     
    8686                $this->permission = $perm;
    8787        }
    8888
    89         function find_base_dir($base = '.',$echo = false, $loop = false) {
    90                 //Sanitize the Windows path formats, This allows easier conparison and aligns it to FTP output.
    91                 $abspath = str_replace('\\','/',ABSPATH); //windows: Straighten up the paths..
    92                 if( strpos($abspath, ':') ){ //Windows, Strip out the driveletter
    93                         if( preg_match("|.{1}\:(.+)|i", $abspath, $mat) )
    94                                 $abspath = $mat[1];
    95                 }
    96        
    97                 //Set up the base directory (Which unless specified, is the current one)
    98                 if( empty( $base ) || '.' == $base ) $base = $this->cwd();
    99                 $base = trailingslashit($base);
    100 
    101                 //Can we see the Current directory as part of the ABSPATH?
    102                 $location = strpos($abspath, $base);
    103                 if( false !== $location ) {
    104                         $newbase = path_join($base, substr($abspath, $location + strlen($base)));
    105 
    106                         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.
    107                                 if($echo) printf( __('Changing to %s') . '<br/>', $newbase );
    108                                 //Check to see if it exists in that folder.
    109                                 if( $this->exists($newbase . 'wp-settings.php') ){
    110                                         if($echo) printf( __('Found %s'),  $newbase . 'wp-settings.php<br/>' );
    111                                         return $newbase;
    112                                 }       
    113                         }
    114                 }
    115        
    116                 //Ok, Couldnt do a magic location from that particular folder level
    117                
    118                 //Get a list of the files in the current directory, See if we can locate where we are in the folder stucture.
    119                 $files = $this->dirlist($base);
    120                
    121                 $arrPath = explode('/', $abspath);
    122                 foreach($arrPath as $key){
    123                         //Working from /home/ to /user/ to /wordpress/ see if that file exists within the current folder,
    124                         // If its found, change into it and follow through looking for it.
    125                         // If it cant find WordPress down that route, it'll continue onto the next folder level, and see if that matches, and so on.
    126                         // If it reaches the end, and still cant find it, it'll return false for the entire function.
    127                         if( isset($files[ $key ]) ){
    128                                 //Lets try that folder:
    129                                 $folder = path_join($base, $key);
    130                                 if($echo) printf( __('Changing to %s') . '<br/>', $folder );
    131                                 $ret = $this->find_base_dir( $folder, $echo, $loop);
    132                                 if( $ret )
    133                                         return $ret;
    134                         }
    135                 }
    136                 //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.
    137                 if(isset( $files[ 'wp-settings.php' ]) ){
    138                         if($echo) printf( __('Found %s'),  $base . 'wp-settings.php<br/>' );
    139                         return $base;
    140                 }
    141                 if( $loop )
    142                         return false;//Prevent tihs function looping again.
    143                 //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.
    144                 return $this->find_base_dir('/', $echo, true);
    145         }
    146 
    147         function get_base_dir($base = '.', $echo = false){
    148                 if( defined('FTP_BASE') )
    149                         $this->wp_base = FTP_BASE;
    150                 if( empty($this->wp_base) )
    151                         $this->wp_base = $this->find_base_dir($base, $echo);
    152                 return $this->wp_base;
    153         }
    154 
    15589        function get_contents($file,$type='',$resumepos=0){
    15690                if( ! $this->exists($file) )
    15791                        return false;
    15892
    15993                if( empty($type) ){
    160                         $extension = substr(strrchr($file, "."), 1);
     94                        $extension = substr(strrchr($file, '.'), 1);
    16195                        $type = isset($this->filetypes[ $extension ]) ? $this->filetypes[ $extension ] : FTP_AUTOASCII;
    16296                }
    16397                $this->ftp->SetType($type);
    164                 $temp = tmpfile();
    165                 if ( ! $temp )
     98                $temp = wp_tempnam( $file );
     99                if ( ! @fopen($temp) )
    166100                        return false;
    167101                if ( ! $this->ftp->fget($temp, $file) ) {
    168102                        fclose($temp);
     103                        unlink($temp);
    169104                        return ''; //Blank document, File does exist, Its just blank.
    170105                }
    171106                fseek($temp, 0); //Skip back to the start of the file being written to
    172107                $contents = '';
    173                 while ( !feof($temp) )
     108                while ( ! feof($temp) )
    174109                        $contents .= fread($temp, 8192);
    175110                fclose($temp);
     111                unlink($temp);
    176112                return $contents;
    177113        }
    178114
    179115        function get_contents_array($file){
    180                 return explode("\n",$this->get_contents($file));
     116                return explode("\n", $this->get_contents($file) );
    181117        }
    182118
    183         function put_contents($file,$contents,$type=''){
     119        function put_contents($file, $contents, $type = '' ) {
    184120                if( empty($type) ){
    185                         $extension = substr(strrchr($file, "."), 1);
    186                         $type = isset($this->filetypes[ $extension ]) ? $this->filetypes[ $extension ] : FTP_ASCII;
     121                        $extension = substr(strrchr($file, '.'), 1);
     122                        $type = isset($this->filetypes[ $extension ]) ? $this->filetypes[ $extension ] : FTP_AUTOASCII;
    187123                }
    188124                $this->ftp->SetType($type);
    189125
    190                 $temp = tmpfile();
    191                 if ( ! $temp )
     126                $temp = wp_tempnam( $file );
     127                if ( ! @fopen($temp) )
    192128                        return false;
    193                 fwrite($temp,$contents);
     129                fwrite($temp, $contents);
    194130                fseek($temp, 0); //Skip back to the start of the file being written to
    195131                $ret = $this->ftp->fput($file, $temp);
    196132                fclose($temp);
     133                unlink($temp);
    197134                return $ret;
    198135        }
    199136
    200         function cwd(){
     137        function cwd() {
    201138                $cwd = $this->ftp->pwd();
    202139                if( $cwd )
    203140                        $cwd = trailingslashit($cwd);
    204141                return $cwd;
    205142        }
    206143
    207         function chdir($file){
     144        function chdir($file) {
    208145                return $this->ftp->chdir($file);
    209146        }
    210147       
    211         function chgrp($file,$group,$recursive=false){
     148        function chgrp($file, $group, $recursive = false ) {
    212149                return false;
    213150        }
    214151
    215         function chmod($file,$mode=false,$recursive=false){
     152        function chmod($file, $mode = false, $recursive = false ){
    216153                if( ! $mode )
    217154                        $mode = $this->permission;
    218155                if( ! $mode )
    219156                        return false;
    220157                //if( ! $this->exists($file) )
    221158                //      return false;
    222                 if( ! $recursive || ! $this->is_dir($file) ){
     159                if( ! $recursive || ! $this->is_dir($file) ) {
    223160                        return $this->ftp->chmod($file,$mode);
    224161                }
    225162                //Is a directory, and we want recursive
    226163                $filelist = $this->dirlist($file);
    227164                foreach($filelist as $filename){
    228                         $this->chmod($file.'/'.$filename,$mode,$recursive);
     165                        $this->chmod($file . '/' . $filename, $mode, $recursive);
    229166                }
    230167                return true;
    231168        }
    232169
    233         function chown($file,$owner,$recursive=false){
     170        function chown($file, $owner, $recursive = false ) {
    234171                return false;
    235172        }
    236173
    237         function owner($file){
     174        function owner($file) {
    238175                $dir = $this->dirlist($file);
    239176                return $dir[$file]['owner'];
    240177        }
    241178
    242         function getchmod($file){
     179        function getchmod($file) {
    243180                $dir = $this->dirlist($file);
    244181                return $dir[$file]['permsn'];
    245182        }
    246183
    247         function gethchmod($file){
    248                 //From the PHP.net page for ...?
    249                 $perms = $this->getchmod($file);
    250                 if (($perms & 0xC000) == 0xC000) {
    251                         // Socket
    252                         $info = 's';
    253                 } elseif (($perms & 0xA000) == 0xA000) {
    254                         // Symbolic Link
    255                         $info = 'l';
    256                 } elseif (($perms & 0x8000) == 0x8000) {
    257                         // Regular
    258                         $info = '-';
    259                 } elseif (($perms & 0x6000) == 0x6000) {
    260                         // Block special
    261                         $info = 'b';
    262                 } elseif (($perms & 0x4000) == 0x4000) {
    263                         // Directory
    264                         $info = 'd';
    265                 } elseif (($perms & 0x2000) == 0x2000) {
    266                         // Character special
    267                         $info = 'c';
    268                 } elseif (($perms & 0x1000) == 0x1000) {
    269                         // FIFO pipe
    270                         $info = 'p';
    271                 } else {
    272                         // Unknown
    273                         $info = 'u';
    274                 }
    275 
    276                 // Owner
    277                 $info .= (($perms & 0x0100) ? 'r' : '-');
    278                 $info .= (($perms & 0x0080) ? 'w' : '-');
    279                 $info .= (($perms & 0x0040) ?
    280                                         (($perms & 0x0800) ? 's' : 'x' ) :
    281                                         (($perms & 0x0800) ? 'S' : '-'));
    282 
    283                 // Group
    284                 $info .= (($perms & 0x0020) ? 'r' : '-');
    285                 $info .= (($perms & 0x0010) ? 'w' : '-');
    286                 $info .= (($perms & 0x0008) ?
    287                                         (($perms & 0x0400) ? 's' : 'x' ) :
    288                                         (($perms & 0x0400) ? 'S' : '-'));
    289 
    290                 // World
    291                 $info .= (($perms & 0x0004) ? 'r' : '-');
    292                 $info .= (($perms & 0x0002) ? 'w' : '-');
    293                 $info .= (($perms & 0x0001) ?
    294                                         (($perms & 0x0200) ? 't' : 'x' ) :
    295                                         (($perms & 0x0200) ? 'T' : '-'));
    296                 return $info;
    297         }
    298 
    299         function getnumchmodfromh($mode) {
    300                 $realmode = "";
    301                 $legal =  array("","w","r","x","-");
    302                 $attarray = preg_split("//",$mode);
    303                 for($i=0;$i<count($attarray);$i++){
    304                    if($key = array_search($attarray[$i],$legal)){
    305                            $realmode .= $legal[$key];
    306                    }
    307                 }
    308                 $mode = str_pad($realmode,9,'-');
    309                 $trans = array('-'=>'0','r'=>'4','w'=>'2','x'=>'1');
    310                 $mode = strtr($mode,$trans);
    311                 $newmode = '';
    312                 $newmode .= $mode[0]+$mode[1]+$mode[2];
    313                 $newmode .= $mode[3]+$mode[4]+$mode[5];
    314                 $newmode .= $mode[6]+$mode[7]+$mode[8];
    315                 return $newmode;
    316         }
    317 
    318         function group($file){
     184        function group($file) {
    319185                $dir = $this->dirlist($file);
    320186                return $dir[$file]['group'];
    321187        }
    322188
    323         function copy($source,$destination,$overwrite=false){
     189        function copy($source, $destination, $overwrite = false ) {
    324190                if( ! $overwrite && $this->exists($destination) )
    325191                        return false;
    326192
     
    328194                if ( false === $content )
    329195                        return false;
    330196
    331                 return $this->put_contents($destination,$content);
     197                return $this->put_contents($destination, $content);
    332198        }
    333199
    334         function move($source,$destination,$overwrite=false){
    335                 return $this->ftp->rename($source,$destination);
     200        function move($source, $destination, $overwrite = false ) {
     201                return $this->ftp->rename($source, $destination);
    336202        }
    337203
    338         function delete($file,$recursive=false) {
     204        function delete($file, $recursive = false ) {
    339205                if ( $this->is_file($file) )
    340206                        return $this->ftp->delete($file);
    341207                if ( !$recursive )
     
    344210                return $this->ftp->mdel($file);
    345211        }
    346212
    347         function exists($file){
     213        function exists($file) {
    348214                return $this->ftp->is_exists($file);
    349215        }
    350216
    351         function is_file($file){
     217        function is_file($file) {
    352218                return $this->is_dir($file) ? false : true;
    353219        }
    354220
    355         function is_dir($path){
     221        function is_dir($path) {
    356222                $cwd = $this->cwd();
    357223                if ( $this->chdir($path) ) {
    358224                        $this->chdir($cwd);
     
    361227                return false;
    362228        }
    363229
    364         function is_readable($file){
     230        function is_readable($file) {
    365231                //Get dir list, Check if the file is writable by the current user??
    366232                return true;
    367233        }
    368234
    369         function is_writable($file){
     235        function is_writable($file) {
    370236                //Get dir list, Check if the file is writable by the current user??
    371237                return true;
    372238        }
    373239
    374         function atime($file){
     240        function atime($file) {
    375241                return false;
    376242        }
    377243
    378         function mtime($file){
     244        function mtime($file) {
    379245                return $this->ftp->mdtm($file);
    380246        }
    381247
    382         function size($file){
     248        function size($file) {
    383249                return $this->ftp->filesize($file);
    384250        }
    385251
    386         function touch($file,$time=0,$atime=0){
     252        function touch($file, $time = 0, $atime = 0 ){
    387253                return false;
    388254        }
    389255
    390         function mkdir($path,$chmod=false,$chown=false,$chgrp=false){
     256        function mkdir($path, $chmod = false, $chown = false, $chgrp = false ) {
    391257                if( ! $this->ftp->mkdir($path) )
    392258                        return false;
    393259                if( $chmod )
     
    399265                return true;
    400266        }
    401267
    402         function rmdir($path,$recursive=false){
     268        function rmdir($path, $recursive = false ) {
    403269                if( ! $recursive )
    404270                        return $this->ftp->rmdir($path);
    405271
    406272                return $this->ftp->mdel($path);
    407273        }
    408274
    409         function dirlist($path='.',$incdot=false,$recursive=false){
    410                 if( $this->is_file($path) ){
     275        function dirlist($path = '.', $incdot = false, $recursive = false ) {
     276                if( $this->is_file($path) ) {
    411277                        $limitFile = basename($path);
    412278                        $path = dirname($path) . '/';
    413279                } else {
     
    430296                                        //We're including the doted starts
    431297                                        if( '.' != $struc['name'] && '..' != $struc['name'] ){ //Ok, It isnt a special folder
    432298                                                if ($recursive)
    433                                                         $struc['files'] = $this->dirlist($path.'/'.$struc['name'],$incdot,$recursive);
     299                                                        $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $incdot, $recursive);
    434300                                        }
    435301                                } else { //No dots
    436302                                        if ($recursive)
    437                                                 $struc['files'] = $this->dirlist($path.'/'.$struc['name'],$incdot,$recursive);
     303                                                $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $incdot, $recursive);
    438304                                }
    439305                        }
    440306                        //File
  • wp-admin/includes/file.php

     
    3434
    3535function get_real_file_to_edit( $file ) {
    3636        if ('index.php' == $file || '.htaccess' == $file ) {
    37                 $real_file = get_home_path().$file;
     37                $real_file = get_home_path() . $file;
    3838        } else {
    39                 $real_file = ABSPATH.$file;
     39                $real_file = ABSPATH . $file;
    4040        }
    4141
    4242        return $real_file;
     
    256274                        $tmppath .= $path[$j] . '/';
    257275                        if ( ! $fs->is_dir($to . $tmppath) )
    258276                                if ( !$fs->mkdir($to . $tmppath, 0755) )
    259                                         return new WP_Error('mkdir_failed', __('Could not create directory'));
     277                                        return new WP_Error('mkdir_failed', __('Could not create directory'), $to . $tmppath);
    260278                }
    261279
    262280                // We've made sure the folders are there, so let's extract the file now:
    263281                if ( ! $file['folder'] )
    264282                        if ( !$fs->put_contents( $to . $file['filename'], $file['content']) )
    265                                 return new WP_Error('copy_failed', __('Could not copy file'));
     283                                return new WP_Error('copy_failed', __('Could not copy file'), $to . $file['filename']);
    266284                        $fs->chmod($to . $file['filename'], 0644);
    267285        }
    268286
     
    280298        foreach ( (array) $dirlist as $filename => $fileinfo ) {
    281299                if ( 'f' == $fileinfo['type'] ) {
    282300                        if ( ! $wp_filesystem->copy($from . $filename, $to . $filename, true) )
    283                                 return false;
     301                                return new WP_Error('copy_failed', __('Could not copy file'), $to . $filename);
    284302                        $wp_filesystem->chmod($to . $filename, 0644);
    285303                } elseif ( 'd' == $fileinfo['type'] ) {
    286304                        if ( !$wp_filesystem->mkdir($to . $filename, 0755) )
    287                                 return false;
    288                         if ( !copy_dir($from . $filename, $to . $filename) )
    289                                 return false;
     305                                return new WP_Error('mkdir_failed', __('Could not create directory'), $to . $filename);
     306                        $result = copy_dir($from . $filename, $to . $filename);
     307                        if ( is_wp_error($result) )
     308                                return $result;
    290309                }
    291310        }
    292 
    293         return true;
    294311}
    295312
    296313function WP_Filesystem( $args = false ) {
    297314        global $wp_filesystem;
    298315
     316        require_once(ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php');
     317
    299318        $method = get_filesystem_method();
    300319
    301320        if ( ! $method )
     
    333352        return apply_filters('filesystem_method', $method);
    334353}
    335354
    336 ?>
     355?>
     356 No newline at end of file
  • wp-admin/includes/update.php

     
    150150                return new WP_Error('up_to_date', __('The plugin is at the latest version.'));
    151151
    152152        // Is a filesystem accessor setup?
    153         if ( ! $wp_filesystem || !is_object($wp_filesystem) )
     153        if ( ! $wp_filesystem || ! is_object($wp_filesystem) )
    154154                WP_Filesystem();
    155155
    156156        if ( ! is_object($wp_filesystem) )
     
    160160                return new WP_Error('fs_error', __('Filesystem error'), $wp_filesystem->errors);
    161161
    162162        //Get the base plugin folder
    163         $base = $wp_filesystem->get_base_dir(WP_PLUGIN_DIR);
     163        $plugins_dir = $wp_filesystem->wp_plugins_dir();
     164        if ( empty($plugins_dir) )
     165                return new WP_Error('fs_no)plugins_dir', __('Unable to locate WordPress Plugin directory.'));
     166
     167        //And the same for the Content directory.
     168        $content_dir = $wp_filesystem->wp_content_dir();
     169        if( empty($content_dir) )
     170                return new WP_Error('fs_no_content_dor', __('Unable to locate WordPress Content directory (wp-content).'));
    164171       
    165         if ( empty($base) )
    166                 return new WP_Error('fs_nowordpress', __('Unable to locate WordPress directory.'));
     172        $plugins_dir = trailingslashit( $plugins_dir );
     173        $content_dir = trailingslashit( $content_dir );
    167174
    168175        // Get the URL to the zip file
    169176        $r = $current->response[ $plugin ];
     
    174181        // Download the package
    175182        $package = $r->package;
    176183        apply_filters('update_feedback', sprintf(__('Downloading update from %s'), $package));
    177         $file = download_url($package);
     184        $download_file = download_url($package);
    178185
    179         if ( is_wp_error($file) )
     186        if ( is_wp_error($download_file) )
    180187                return new WP_Error('download_failed', __('Download failed.'), $file->get_error_message());
    181188
    182         $working_dir = $wp_filesystem->get_base_dir(WP_CONTENT_DIR) . '/upgrade/' . basename($plugin, '.php');
     189        $working_dir = $content_dir . 'upgrade/' . basename($plugin, '.php');
    183190
    184191        // Clean up working directory
    185192        if ( $wp_filesystem->is_dir($working_dir) )
     
    187194
    188195        apply_filters('update_feedback', __('Unpacking the update'));
    189196        // Unzip package to working directory
    190         $result = unzip_file($file, $working_dir);
     197        $result = unzip_file($download_file, $working_dir);
     198       
     199        // Once extracted, delete the package
     200        unlink($download_file);
     201       
    191202        if ( is_wp_error($result) ) {
    192                 unlink($file);
    193203                $wp_filesystem->delete($working_dir, true);
    194204                return $result;
    195205        }
    196206
    197         // Once extracted, delete the package
    198         unlink($file);
    199 
    200207        if ( is_plugin_active($plugin) ) {
    201208                //Deactivate the plugin silently, Prevent deactivation hooks from running.
    202209                apply_filters('update_feedback', __('Deactivating the plugin'));
     
    205212
    206213        // Remove the existing plugin.
    207214        apply_filters('update_feedback', __('Removing the old version of the plugin'));
    208         $plugin_dir = dirname($base . "/$plugin");
    209         $plugin_dir = trailingslashit($plugin_dir);
     215        $this_plugin_dir = trailingslashit( dirname($plugins_dir . $plugin) );
    210216       
    211217        // If plugin is in its own directory, recursively delete the directory.
    212         if ( strpos($plugin, '/') && $plugin_dir != $base . '/' ) //base check on if plugin includes directory seperator AND that its not the root plugin folder
    213                 $deleted = $wp_filesystem->delete($plugin_dir, true);
     218        if ( strpos($plugin, '/') && $this_plugin_dir != $plugins_dir ) //base check on if plugin includes directory seperator AND that its not the root plugin folder
     219                $deleted = $wp_filesystem->delete($this_plugin_dir, true);
    214220        else
    215                 $deleted = $wp_filesystem->delete($base . '/' . $plugin);
     221                $deleted = $wp_filesystem->delete($plugins_dir . $plugin);
    216222
    217         if ( !$deleted ) {
     223        if ( ! $deleted ) {
    218224                $wp_filesystem->delete($working_dir, true);
    219225                return new WP_Error('delete_failed', __('Could not remove the old plugin'));
    220226        }
    221227
    222228        apply_filters('update_feedback', __('Installing the latest version'));
    223229        // Copy new version of plugin into place.
    224         if ( !copy_dir($working_dir, $base) ) {
     230        $result = copy_dir($working_dir, $plugins_dir);
     231        if ( is_wp_error($result) ) {
    225232                //$wp_filesystem->delete($working_dir, true); //TODO: Uncomment? This DOES mean that the new files are available in the upgrade folder if it fails.
    226                 return new WP_Error('install_failed', __('Installation failed'));
     233                return $result;
    227234        }
    228235
    229236        //Get a list of the directories in the working directory before we delete it, We need to know the new folder for the plugin
     
    236243        delete_option('update_plugins');
    237244       
    238245        if( empty($filelist) )
    239                 return false; //We couldnt find any files in the working dir
     246                return false; //We couldnt find any files in the working dir, therefor no plugin installed? Failsafe backup.
    240247       
    241248        $folder = $filelist[0];
    242         $plugin = get_plugins('/' . $folder); //Pass it with a leading slash, search out the plugins in the folder,
     249        $plugin = get_plugins('/' . $folder); //Ensure to pass with leading slash
    243250        $pluginfiles = array_keys($plugin); //Assume the requested plugin is the first in the list
    244251
    245         return  $folder . '/' . $pluginfiles[0]; //Pass it without a leading slash as WP requires
     252        return  $folder . '/' . $pluginfiles[0];
    246253}
    247254
    248255?>
  • wp-admin/update.php

     
    110110                return;
    111111        }
    112112
    113         $was_activated = is_plugin_active($plugin); //Check now, It'll be deactivated by the next line if it is,
     113        $was_activated = is_plugin_active($plugin); //Check now, It'll be deactivated by the next line if it is
    114114
    115115        $result = wp_update_plugin($plugin, 'show_message');
    116116
    117117        if ( is_wp_error($result) ) {
    118118                show_message($result);
     119                show_message( __('Installation Failed') );
    119120        } else {
    120121                //Result is the new plugin file relative to WP_PLUGIN_DIR
    121                 show_message(__('Plugin upgraded successfully'));       
     122                show_message( __('Plugin upgraded successfully') );     
    122123                if( $result && $was_activated ){
    123124                        show_message(__('Attempting reactivation of the plugin'));
    124125                        echo '<iframe style="border:0" width="100%" height="170px" src="' . wp_nonce_url('update.php?action=activate-plugin&plugin=' . $result, 'activate-plugin_' . $result) .'"></iframe>';