Make WordPress Core


Ignore:
Timestamp:
11/30/2017 11:09:33 PM (8 years ago)
Author:
pento
Message:

Code is Poetry.
WordPress' code just... wasn't.
This is now dealt with.

Props jrf, pento, netweb, GaryJ, jdgrimes, westonruter, Greg Sherwood from PHPCS, and everyone who's ever contributed to WPCS and PHPCS.
Fixes #41057.

File:
1 edited

Legend:

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

    r41161 r42343  
    2121
    2222    /**
    23      *
    2423     * @param array $opt
    2524     */
    26     public function __construct( $opt  = '' ) {
     25    public function __construct( $opt = '' ) {
    2726        $this->method = 'ftpsockets';
    2827        $this->errors = new WP_Error();
     
    3433        $this->ftp = new ftp();
    3534
    36         if ( empty($opt['port']) )
     35        if ( empty( $opt['port'] ) ) {
    3736            $this->options['port'] = 21;
    38         else
     37        } else {
    3938            $this->options['port'] = (int) $opt['port'];
    40 
    41         if ( empty($opt['hostname']) )
    42             $this->errors->add('empty_hostname', __('FTP hostname is required'));
    43         else
     39        }
     40
     41        if ( empty( $opt['hostname'] ) ) {
     42            $this->errors->add( 'empty_hostname', __( 'FTP hostname is required' ) );
     43        } else {
    4444            $this->options['hostname'] = $opt['hostname'];
     45        }
    4546
    4647        // Check if the options provided are OK.
    47         if ( empty ($opt['username']) )
    48             $this->errors->add('empty_username', __('FTP username is required'));
    49         else
     48        if ( empty( $opt['username'] ) ) {
     49            $this->errors->add( 'empty_username', __( 'FTP username is required' ) );
     50        } else {
    5051            $this->options['username'] = $opt['username'];
    51 
    52         if ( empty ($opt['password']) )
    53             $this->errors->add('empty_password', __('FTP password is required'));
    54         else
     52        }
     53
     54        if ( empty( $opt['password'] ) ) {
     55            $this->errors->add( 'empty_password', __( 'FTP password is required' ) );
     56        } else {
    5557            $this->options['password'] = $opt['password'];
    56     }
    57 
    58     /**
    59      *
     58        }
     59    }
     60
     61    /**
    6062     * @return bool
    6163     */
    6264    public function connect() {
    63         if ( ! $this->ftp )
    64             return false;
    65 
    66         $this->ftp->setTimeout(FS_CONNECT_TIMEOUT);
     65        if ( ! $this->ftp ) {
     66            return false;
     67        }
     68
     69        $this->ftp->setTimeout( FS_CONNECT_TIMEOUT );
    6770
    6871        if ( ! $this->ftp->SetServer( $this->options['hostname'], $this->options['port'] ) ) {
    69             $this->errors->add( 'connect',
     72            $this->errors->add(
     73                'connect',
    7074                /* translators: %s: hostname:port */
    71                 sprintf( __( 'Failed to connect to FTP Server %s' ),
     75                sprintf(
     76                    __( 'Failed to connect to FTP Server %s' ),
    7277                    $this->options['hostname'] . ':' . $this->options['port']
    7378                )
     
    7782
    7883        if ( ! $this->ftp->connect() ) {
    79             $this->errors->add( 'connect',
     84            $this->errors->add(
     85                'connect',
    8086                /* translators: %s: hostname:port */
    81                 sprintf( __( 'Failed to connect to FTP Server %s' ),
     87                sprintf(
     88                    __( 'Failed to connect to FTP Server %s' ),
    8289                    $this->options['hostname'] . ':' . $this->options['port']
    8390                )
     
    8794
    8895        if ( ! $this->ftp->login( $this->options['username'], $this->options['password'] ) ) {
    89             $this->errors->add( 'auth',
     96            $this->errors->add(
     97                'auth',
    9098                /* translators: %s: username */
    91                 sprintf( __( 'Username/Password incorrect for %s' ),
     99                sprintf(
     100                    __( 'Username/Password incorrect for %s' ),
    92101                    $this->options['username']
    93102                )
     
    112121     */
    113122    public function get_contents( $file ) {
    114         if ( ! $this->exists($file) )
    115             return false;
     123        if ( ! $this->exists( $file ) ) {
     124            return false;
     125        }
    116126
    117127        $temp = wp_tempnam( $file );
     
    124134        mbstring_binary_safe_encoding();
    125135
    126         if ( ! $this->ftp->fget($temphandle, $file) ) {
    127             fclose($temphandle);
    128             unlink($temp);
     136        if ( ! $this->ftp->fget( $temphandle, $file ) ) {
     137            fclose( $temphandle );
     138            unlink( $temp );
    129139
    130140            reset_mbstring_encoding();
     
    138148        $contents = '';
    139149
    140         while ( ! feof($temphandle) )
    141             $contents .= fread($temphandle, 8192);
    142 
    143         fclose($temphandle);
    144         unlink($temp);
     150        while ( ! feof( $temphandle ) ) {
     151            $contents .= fread( $temphandle, 8192 );
     152        }
     153
     154        fclose( $temphandle );
     155        unlink( $temp );
    145156        return $contents;
    146157    }
    147158
    148159    /**
    149      *
    150160     * @param string $file
    151161     * @return array
    152162     */
    153     public function get_contents_array($file) {
    154         return explode("\n", $this->get_contents($file) );
    155     }
    156 
    157     /**
    158      *
     163    public function get_contents_array( $file ) {
     164        return explode( "\n", $this->get_contents( $file ) );
     165    }
     166
     167    /**
    159168     * @param string $file
    160169     * @param string $contents
     
    162171     * @return bool
    163172     */
    164     public function put_contents($file, $contents, $mode = false ) {
     173    public function put_contents( $file, $contents, $mode = false ) {
    165174        $temp = wp_tempnam( $file );
    166         if ( ! $temphandle = @fopen($temp, 'w+') ) {
    167             unlink($temp);
     175        if ( ! $temphandle = @fopen( $temp, 'w+' ) ) {
     176            unlink( $temp );
    168177            return false;
    169178        }
     
    184193        fseek( $temphandle, 0 ); // Skip back to the start of the file being written to
    185194
    186         $ret = $this->ftp->fput($file, $temphandle);
     195        $ret = $this->ftp->fput( $file, $temphandle );
    187196
    188197        reset_mbstring_encoding();
    189198
    190         fclose($temphandle);
    191         unlink($temp);
    192 
    193         $this->chmod($file, $mode);
     199        fclose( $temphandle );
     200        unlink( $temp );
     201
     202        $this->chmod( $file, $mode );
    194203
    195204        return $ret;
     
    197206
    198207    /**
    199      *
    200208     * @return string
    201209     */
    202210    public function cwd() {
    203211        $cwd = $this->ftp->pwd();
    204         if ( $cwd )
    205             $cwd = trailingslashit($cwd);
     212        if ( $cwd ) {
     213            $cwd = trailingslashit( $cwd );
     214        }
    206215        return $cwd;
    207216    }
    208217
    209218    /**
    210      *
    211      * @param string $file
    212      * @return bool
    213      */
    214     public function chdir($file) {
    215         return $this->ftp->chdir($file);
    216     }
    217 
    218     /**
    219      *
     219     * @param string $file
     220     * @return bool
     221     */
     222    public function chdir( $file ) {
     223        return $this->ftp->chdir( $file );
     224    }
     225
     226    /**
    220227     * @param string $file
    221228     * @param int|bool $mode
     
    223230     * @return bool
    224231     */
    225     public function chmod($file, $mode = false, $recursive = false ) {
     232    public function chmod( $file, $mode = false, $recursive = false ) {
    226233        if ( ! $mode ) {
    227             if ( $this->is_file($file) )
     234            if ( $this->is_file( $file ) ) {
    228235                $mode = FS_CHMOD_FILE;
    229             elseif ( $this->is_dir($file) )
     236            } elseif ( $this->is_dir( $file ) ) {
    230237                $mode = FS_CHMOD_DIR;
    231             else
     238            } else {
    232239                return false;
     240            }
    233241        }
    234242
    235243        // chmod any sub-objects if recursive.
    236         if ( $recursive && $this->is_dir($file) ) {
    237             $filelist = $this->dirlist($file);
    238             foreach ( (array)$filelist as $filename => $filemeta )
    239                 $this->chmod($file . '/' . $filename, $mode, $recursive);
     244        if ( $recursive && $this->is_dir( $file ) ) {
     245            $filelist = $this->dirlist( $file );
     246            foreach ( (array) $filelist as $filename => $filemeta ) {
     247                $this->chmod( $file . '/' . $filename, $mode, $recursive );
     248            }
    240249        }
    241250
    242251        // chmod the file or directory
    243         return $this->ftp->chmod($file, $mode);
    244     }
    245 
    246     /**
    247      *
     252        return $this->ftp->chmod( $file, $mode );
     253    }
     254
     255    /**
    248256     * @param string $file
    249257     * @return string
    250258     */
    251     public function owner($file) {
    252         $dir = $this->dirlist($file);
    253         return $dir[$file]['owner'];
    254     }
    255 
    256     /**
    257      *
     259    public function owner( $file ) {
     260        $dir = $this->dirlist( $file );
     261        return $dir[ $file ]['owner'];
     262    }
     263
     264    /**
    258265     * @param string $file
    259266     * @return string
    260267     */
    261     public function getchmod($file) {
    262         $dir = $this->dirlist($file);
    263         return $dir[$file]['permsn'];
    264     }
    265 
    266     /**
    267      *
     268    public function getchmod( $file ) {
     269        $dir = $this->dirlist( $file );
     270        return $dir[ $file ]['permsn'];
     271    }
     272
     273    /**
    268274     * @param string $file
    269275     * @return string
    270276     */
    271     public function group($file) {
    272         $dir = $this->dirlist($file);
    273         return $dir[$file]['group'];
    274     }
    275 
    276     /**
    277      *
     277    public function group( $file ) {
     278        $dir = $this->dirlist( $file );
     279        return $dir[ $file ]['group'];
     280    }
     281
     282    /**
    278283     * @param string   $source
    279284     * @param string   $destination
     
    282287     * @return bool
    283288     */
    284     public function copy($source, $destination, $overwrite = false, $mode = false) {
    285         if ( ! $overwrite && $this->exists($destination) )
    286             return false;
    287 
    288         $content = $this->get_contents($source);
    289         if ( false === $content )
    290             return false;
    291 
    292         return $this->put_contents($destination, $content, $mode);
    293     }
    294 
    295     /**
    296      *
     289    public function copy( $source, $destination, $overwrite = false, $mode = false ) {
     290        if ( ! $overwrite && $this->exists( $destination ) ) {
     291            return false;
     292        }
     293
     294        $content = $this->get_contents( $source );
     295        if ( false === $content ) {
     296            return false;
     297        }
     298
     299        return $this->put_contents( $destination, $content, $mode );
     300    }
     301
     302    /**
    297303     * @param string $source
    298304     * @param string $destination
     
    300306     * @return bool
    301307     */
    302     public function move($source, $destination, $overwrite = false ) {
    303         return $this->ftp->rename($source, $destination);
    304     }
    305 
    306     /**
    307      *
     308    public function move( $source, $destination, $overwrite = false ) {
     309        return $this->ftp->rename( $source, $destination );
     310    }
     311
     312    /**
    308313     * @param string $file
    309314     * @param bool   $recursive
     
    311316     * @return bool
    312317     */
    313     public function delete($file, $recursive = false, $type = false) {
    314         if ( empty($file) )
    315             return false;
    316         if ( 'f' == $type || $this->is_file($file) )
    317             return $this->ftp->delete($file);
    318         if ( !$recursive )
    319             return $this->ftp->rmdir($file);
    320 
    321         return $this->ftp->mdel($file);
    322     }
    323 
    324     /**
    325      *
     318    public function delete( $file, $recursive = false, $type = false ) {
     319        if ( empty( $file ) ) {
     320            return false;
     321        }
     322        if ( 'f' == $type || $this->is_file( $file ) ) {
     323            return $this->ftp->delete( $file );
     324        }
     325        if ( ! $recursive ) {
     326            return $this->ftp->rmdir( $file );
     327        }
     328
     329        return $this->ftp->mdel( $file );
     330    }
     331
     332    /**
    326333     * @param string $file
    327334     * @return bool
     
    334341        }
    335342
    336         return !empty( $list ); //empty list = no file, so invert.
     343        return ! empty( $list ); //empty list = no file, so invert.
    337344        // Return $this->ftp->is_exists($file); has issues with ABOR+426 responses on the ncFTPd server.
    338345    }
    339346
    340347    /**
    341      *
    342      * @param string $file
    343      * @return bool
    344      */
    345     public function is_file($file) {
    346         if ( $this->is_dir($file) )
    347             return false;
    348         if ( $this->exists($file) )
     348     * @param string $file
     349     * @return bool
     350     */
     351    public function is_file( $file ) {
     352        if ( $this->is_dir( $file ) ) {
     353            return false;
     354        }
     355        if ( $this->exists( $file ) ) {
    349356            return true;
     357        }
    350358        return false;
    351359    }
    352360
    353361    /**
    354      *
    355362     * @param string $path
    356363     * @return bool
    357364     */
    358     public function is_dir($path) {
     365    public function is_dir( $path ) {
    359366        $cwd = $this->cwd();
    360         if ( $this->chdir($path) ) {
    361             $this->chdir($cwd);
     367        if ( $this->chdir( $path ) ) {
     368            $this->chdir( $cwd );
    362369            return true;
    363370        }
     
    366373
    367374    /**
    368      *
    369      * @param string $file
    370      * @return bool
    371      */
    372     public function is_readable($file) {
     375     * @param string $file
     376     * @return bool
     377     */
     378    public function is_readable( $file ) {
    373379        return true;
    374380    }
    375381
    376382    /**
    377      *
    378      * @param string $file
    379      * @return bool
    380      */
    381     public function is_writable($file) {
     383     * @param string $file
     384     * @return bool
     385     */
     386    public function is_writable( $file ) {
    382387        return true;
    383388    }
    384389
    385390    /**
    386      *
    387      * @param string $file
    388      * @return bool
    389      */
    390     public function atime($file) {
     391     * @param string $file
     392     * @return bool
     393     */
     394    public function atime( $file ) {
    391395        return false;
    392396    }
    393397
    394398    /**
    395      *
    396399     * @param string $file
    397400     * @return int
    398401     */
    399     public function mtime($file) {
    400         return $this->ftp->mdtm($file);
     402    public function mtime( $file ) {
     403        return $this->ftp->mdtm( $file );
    401404    }
    402405
     
    405408     * @return int
    406409     */
    407     public function size($file) {
    408         return $this->ftp->filesize($file);
    409     }
    410 
    411     /**
    412      *
     410    public function size( $file ) {
     411        return $this->ftp->filesize( $file );
     412    }
     413
     414    /**
    413415     * @param string $file
    414416     * @param int $time
     
    416418     * @return bool
    417419     */
    418     public function touch($file, $time = 0, $atime = 0 ) {
     420    public function touch( $file, $time = 0, $atime = 0 ) {
    419421        return false;
    420422    }
    421423
    422424    /**
    423      *
    424425     * @param string $path
    425426     * @param mixed  $chmod
     
    428429     * @return bool
    429430     */
    430     public function mkdir($path, $chmod = false, $chown = false, $chgrp = false ) {
    431         $path = untrailingslashit($path);
    432         if ( empty($path) )
    433             return false;
    434 
    435         if ( ! $this->ftp->mkdir($path) )
    436             return false;
    437         if ( ! $chmod )
     431    public function mkdir( $path, $chmod = false, $chown = false, $chgrp = false ) {
     432        $path = untrailingslashit( $path );
     433        if ( empty( $path ) ) {
     434            return false;
     435        }
     436
     437        if ( ! $this->ftp->mkdir( $path ) ) {
     438            return false;
     439        }
     440        if ( ! $chmod ) {
    438441            $chmod = FS_CHMOD_DIR;
    439         $this->chmod($path, $chmod);
     442        }
     443        $this->chmod( $path, $chmod );
    440444        return true;
    441445    }
    442446
    443447    /**
    444      *
    445448     * @param string $path
    446449     * @param bool $recursive
    447450     * @return bool
    448451     */
    449     public function rmdir($path, $recursive = false ) {
    450         return $this->delete($path, $recursive);
    451     }
    452 
    453     /**
    454      *
     452    public function rmdir( $path, $recursive = false ) {
     453        return $this->delete( $path, $recursive );
     454    }
     455
     456    /**
    455457     * @param string $path
    456458     * @param bool   $include_hidden
     
    458460     * @return bool|array
    459461     */
    460     public function dirlist($path = '.', $include_hidden = true, $recursive = false ) {
    461         if ( $this->is_file($path) ) {
    462             $limit_file = basename($path);
    463             $path = dirname($path) . '/';
     462    public function dirlist( $path = '.', $include_hidden = true, $recursive = false ) {
     463        if ( $this->is_file( $path ) ) {
     464            $limit_file = basename( $path );
     465            $path       = dirname( $path ) . '/';
    464466        } else {
    465467            $limit_file = false;
     
    468470        mbstring_binary_safe_encoding();
    469471
    470         $list = $this->ftp->dirlist($path);
     472        $list = $this->ftp->dirlist( $path );
    471473        if ( empty( $list ) && ! $this->exists( $path ) ) {
    472474
     
    479481        foreach ( $list as $struc ) {
    480482
    481             if ( '.' == $struc['name'] || '..' == $struc['name'] )
     483            if ( '.' == $struc['name'] || '..' == $struc['name'] ) {
    482484                continue;
    483 
    484             if ( ! $include_hidden && '.' == $struc['name'][0] )
     485            }
     486
     487            if ( ! $include_hidden && '.' == $struc['name'][0] ) {
    485488                continue;
    486 
    487             if ( $limit_file && $struc['name'] != $limit_file )
     489            }
     490
     491            if ( $limit_file && $struc['name'] != $limit_file ) {
    488492                continue;
     493            }
    489494
    490495            if ( 'd' == $struc['type'] ) {
    491                 if ( $recursive )
    492                     $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $include_hidden, $recursive);
    493                 else
     496                if ( $recursive ) {
     497                    $struc['files'] = $this->dirlist( $path . '/' . $struc['name'], $include_hidden, $recursive );
     498                } else {
    494499                    $struc['files'] = array();
     500                }
    495501            }
    496502
    497503            // Replace symlinks formatted as "source -> target" with just the source name
    498             if ( $struc['islink'] )
     504            if ( $struc['islink'] ) {
    499505                $struc['name'] = preg_replace( '/(\s*->\s*.*)$/', '', $struc['name'] );
     506            }
    500507
    501508            // Add the Octal representation of the file permissions
Note: See TracChangeset for help on using the changeset viewer.