Make WordPress Core


Ignore:
Timestamp:
11/30/2017 11:09:33 PM (7 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-direct.php

    r41161 r42343  
    1919     * constructor
    2020     *
    21      *
    2221     * @param mixed $arg ignored argument
    2322     */
    24     public function __construct($arg) {
     23    public function __construct( $arg ) {
    2524        $this->method = 'direct';
    2625        $this->errors = new WP_Error();
     
    3029     * Reads entire file into a string
    3130     *
    32      *
    3331     * @param string $file Name of the file to read.
    3432     * @return string|bool The function returns the read data or false on failure.
    3533     */
    36     public function get_contents($file) {
    37         return @file_get_contents($file);
     34    public function get_contents( $file ) {
     35        return @file_get_contents( $file );
    3836    }
    3937
    4038    /**
    4139     * Reads entire file into an array
    42      *
    4340     *
    4441     * @param string $file Path to the file.
    4542     * @return array|bool the file contents in an array or false on failure.
    4643     */
    47     public function get_contents_array($file) {
    48         return @file($file);
     44    public function get_contents_array( $file ) {
     45        return @file( $file );
    4946    }
    5047
    5148    /**
    5249     * Write a string to a file
    53      *
    5450     *
    5551     * @param string $file     Remote path to the file where to write the data.
     
    6157    public function put_contents( $file, $contents, $mode = false ) {
    6258        $fp = @fopen( $file, 'wb' );
    63         if ( ! $fp )
    64             return false;
     59        if ( ! $fp ) {
     60            return false;
     61        }
    6562
    6663        mbstring_binary_safe_encoding();
     
    7471        fclose( $fp );
    7572
    76         if ( $data_length !== $bytes_written )
    77             return false;
     73        if ( $data_length !== $bytes_written ) {
     74            return false;
     75        }
    7876
    7977        $this->chmod( $file, $mode );
     
    8482    /**
    8583     * Gets the current working directory
    86      *
    8784     *
    8885     * @return string|bool the current working directory on success, or false on failure.
     
    9592     * Change directory
    9693     *
    97      *
    9894     * @param string $dir The new current directory.
    9995     * @return bool Returns true on success or false on failure.
    10096     */
    101     public function chdir($dir) {
    102         return @chdir($dir);
     97    public function chdir( $dir ) {
     98        return @chdir( $dir );
    10399    }
    104100
    105101    /**
    106102     * Changes file group
    107      *
    108103     *
    109104     * @param string $file      Path to the file.
     
    112107     * @return bool Returns true on success or false on failure.
    113108     */
    114     public function chgrp($file, $group, $recursive = false) {
    115         if ( ! $this->exists($file) )
    116             return false;
    117         if ( ! $recursive )
    118             return @chgrp($file, $group);
    119         if ( ! $this->is_dir($file) )
    120             return @chgrp($file, $group);
     109    public function chgrp( $file, $group, $recursive = false ) {
     110        if ( ! $this->exists( $file ) ) {
     111            return false;
     112        }
     113        if ( ! $recursive ) {
     114            return @chgrp( $file, $group );
     115        }
     116        if ( ! $this->is_dir( $file ) ) {
     117            return @chgrp( $file, $group );
     118        }
    121119        // Is a directory, and we want recursive
    122         $file = trailingslashit($file);
    123         $filelist = $this->dirlist($file);
    124         foreach ($filelist as $filename)
    125             $this->chgrp($file . $filename, $group, $recursive);
     120        $file     = trailingslashit( $file );
     121        $filelist = $this->dirlist( $file );
     122        foreach ( $filelist as $filename ) {
     123            $this->chgrp( $file . $filename, $group, $recursive );
     124        }
    126125
    127126        return true;
     
    130129    /**
    131130     * Changes filesystem permissions
    132      *
    133131     *
    134132     * @param string $file      Path to the file.
     
    138136     * @return bool Returns true on success or false on failure.
    139137     */
    140     public function chmod($file, $mode = false, $recursive = false) {
     138    public function chmod( $file, $mode = false, $recursive = false ) {
    141139        if ( ! $mode ) {
    142             if ( $this->is_file($file) )
     140            if ( $this->is_file( $file ) ) {
    143141                $mode = FS_CHMOD_FILE;
    144             elseif ( $this->is_dir($file) )
     142            } elseif ( $this->is_dir( $file ) ) {
    145143                $mode = FS_CHMOD_DIR;
    146             else
     144            } else {
    147145                return false;
    148         }
    149 
    150         if ( ! $recursive || ! $this->is_dir($file) )
    151             return @chmod($file, $mode);
     146            }
     147        }
     148
     149        if ( ! $recursive || ! $this->is_dir( $file ) ) {
     150            return @chmod( $file, $mode );
     151        }
    152152        // Is a directory, and we want recursive
    153         $file = trailingslashit($file);
    154         $filelist = $this->dirlist($file);
    155         foreach ( (array)$filelist as $filename => $filemeta)
    156             $this->chmod($file . $filename, $mode, $recursive);
     153        $file     = trailingslashit( $file );
     154        $filelist = $this->dirlist( $file );
     155        foreach ( (array) $filelist as $filename => $filemeta ) {
     156            $this->chmod( $file . $filename, $mode, $recursive );
     157        }
    157158
    158159        return true;
     
    161162    /**
    162163     * Changes file owner
    163      *
    164164     *
    165165     * @param string $file      Path to the file.
     
    169169     * @return bool Returns true on success or false on failure.
    170170     */
    171     public function chown($file, $owner, $recursive = false) {
    172         if ( ! $this->exists($file) )
    173             return false;
    174         if ( ! $recursive )
    175             return @chown($file, $owner);
    176         if ( ! $this->is_dir($file) )
    177             return @chown($file, $owner);
     171    public function chown( $file, $owner, $recursive = false ) {
     172        if ( ! $this->exists( $file ) ) {
     173            return false;
     174        }
     175        if ( ! $recursive ) {
     176            return @chown( $file, $owner );
     177        }
     178        if ( ! $this->is_dir( $file ) ) {
     179            return @chown( $file, $owner );
     180        }
    178181        // Is a directory, and we want recursive
    179         $filelist = $this->dirlist($file);
    180         foreach ($filelist as $filename) {
    181             $this->chown($file . '/' . $filename, $owner, $recursive);
     182        $filelist = $this->dirlist( $file );
     183        foreach ( $filelist as $filename ) {
     184            $this->chown( $file . '/' . $filename, $owner, $recursive );
    182185        }
    183186        return true;
     
    186189    /**
    187190     * Gets file owner
    188      *
    189191     *
    190192     * @param string $file Path to the file.
    191193     * @return string|bool Username of the user or false on error.
    192194     */
    193     public function owner($file) {
    194         $owneruid = @fileowner($file);
    195         if ( ! $owneruid )
    196             return false;
    197         if ( ! function_exists('posix_getpwuid') )
     195    public function owner( $file ) {
     196        $owneruid = @fileowner( $file );
     197        if ( ! $owneruid ) {
     198            return false;
     199        }
     200        if ( ! function_exists( 'posix_getpwuid' ) ) {
    198201            return $owneruid;
    199         $ownerarray = posix_getpwuid($owneruid);
     202        }
     203        $ownerarray = posix_getpwuid( $owneruid );
    200204        return $ownerarray['name'];
    201205    }
     
    205209     *
    206210     * FIXME does not handle errors in fileperms()
    207      *
    208211     *
    209212     * @param string $file Path to the file.
    210213     * @return string Mode of the file (last 3 digits).
    211214     */
    212     public function getchmod($file) {
     215    public function getchmod( $file ) {
    213216        return substr( decoct( @fileperms( $file ) ), -3 );
    214217    }
    215218
    216219    /**
    217      *
    218220     * @param string $file
    219221     * @return string|false
    220222     */
    221     public function group($file) {
    222         $gid = @filegroup($file);
    223         if ( ! $gid )
    224             return false;
    225         if ( ! function_exists('posix_getgrgid') )
     223    public function group( $file ) {
     224        $gid = @filegroup( $file );
     225        if ( ! $gid ) {
     226            return false;
     227        }
     228        if ( ! function_exists( 'posix_getgrgid' ) ) {
    226229            return $gid;
    227         $grouparray = posix_getgrgid($gid);
     230        }
     231        $grouparray = posix_getgrgid( $gid );
    228232        return $grouparray['name'];
    229233    }
    230234
    231235    /**
    232      *
    233236     * @param string $source
    234237     * @param string $destination
     
    237240     * @return bool
    238241     */
    239     public function copy($source, $destination, $overwrite = false, $mode = false) {
    240         if ( ! $overwrite && $this->exists($destination) )
    241             return false;
    242 
    243         $rtval = copy($source, $destination);
    244         if ( $mode )
    245             $this->chmod($destination, $mode);
     242    public function copy( $source, $destination, $overwrite = false, $mode = false ) {
     243        if ( ! $overwrite && $this->exists( $destination ) ) {
     244            return false;
     245        }
     246
     247        $rtval = copy( $source, $destination );
     248        if ( $mode ) {
     249            $this->chmod( $destination, $mode );
     250        }
    246251        return $rtval;
    247252    }
    248253
    249254    /**
    250      *
    251255     * @param string $source
    252256     * @param string $destination
     
    254258     * @return bool
    255259     */
    256     public function move($source, $destination, $overwrite = false) {
    257         if ( ! $overwrite && $this->exists($destination) )
    258             return false;
     260    public function move( $source, $destination, $overwrite = false ) {
     261        if ( ! $overwrite && $this->exists( $destination ) ) {
     262            return false;
     263        }
    259264
    260265        // Try using rename first. if that fails (for example, source is read only) try copy.
    261         if ( @rename($source, $destination) )
     266        if ( @rename( $source, $destination ) ) {
    262267            return true;
    263 
    264         if ( $this->copy($source, $destination, $overwrite) && $this->exists($destination) ) {
    265             $this->delete($source);
     268        }
     269
     270        if ( $this->copy( $source, $destination, $overwrite ) && $this->exists( $destination ) ) {
     271            $this->delete( $source );
    266272            return true;
    267273        } else {
     
    271277
    272278    /**
    273      *
    274279     * @param string $file
    275280     * @param bool $recursive
     
    277282     * @return bool
    278283     */
    279     public function delete($file, $recursive = false, $type = false) {
    280         if ( empty( $file ) ) // Some filesystems report this as /, which can cause non-expected recursive deletion of all files in the filesystem.
    281             return false;
     284    public function delete( $file, $recursive = false, $type = false ) {
     285        if ( empty( $file ) ) { // Some filesystems report this as /, which can cause non-expected recursive deletion of all files in the filesystem.
     286            return false;
     287        }
    282288        $file = str_replace( '\\', '/', $file ); // for win32, occasional problems deleting files otherwise
    283289
    284         if ( 'f' == $type || $this->is_file($file) )
    285             return @unlink($file);
    286         if ( ! $recursive && $this->is_dir($file) )
    287             return @rmdir($file);
     290        if ( 'f' == $type || $this->is_file( $file ) ) {
     291            return @unlink( $file );
     292        }
     293        if ( ! $recursive && $this->is_dir( $file ) ) {
     294            return @rmdir( $file );
     295        }
    288296
    289297        // At this point it's a folder, and we're in recursive mode
    290         $file = trailingslashit($file);
    291         $filelist = $this->dirlist($file, true);
     298        $file     = trailingslashit( $file );
     299        $filelist = $this->dirlist( $file, true );
    292300
    293301        $retval = true;
    294302        if ( is_array( $filelist ) ) {
    295303            foreach ( $filelist as $filename => $fileinfo ) {
    296                 if ( ! $this->delete($file . $filename, $recursive, $fileinfo['type']) )
     304                if ( ! $this->delete( $file . $filename, $recursive, $fileinfo['type'] ) ) {
    297305                    $retval = false;
     306                }
    298307            }
    299308        }
    300309
    301         if ( file_exists($file) && ! @rmdir($file) )
     310        if ( file_exists( $file ) && ! @rmdir( $file ) ) {
    302311            $retval = false;
     312        }
    303313
    304314        return $retval;
    305315    }
    306316    /**
    307      *
    308      * @param string $file
    309      * @return bool
    310      */
    311     public function exists($file) {
    312         return @file_exists($file);
    313     }
    314     /**
    315      *
    316      * @param string $file
    317      * @return bool
    318      */
    319     public function is_file($file) {
    320         return @is_file($file);
    321     }
    322     /**
    323      *
     317     * @param string $file
     318     * @return bool
     319     */
     320    public function exists( $file ) {
     321        return @file_exists( $file );
     322    }
     323    /**
     324     * @param string $file
     325     * @return bool
     326     */
     327    public function is_file( $file ) {
     328        return @is_file( $file );
     329    }
     330    /**
    324331     * @param string $path
    325332     * @return bool
    326333     */
    327     public function is_dir($path) {
    328         return @is_dir($path);
    329     }
    330 
    331     /**
    332      *
    333      * @param string $file
    334      * @return bool
    335      */
    336     public function is_readable($file) {
    337         return @is_readable($file);
    338     }
    339 
    340     /**
    341      *
    342      * @param string $file
    343      * @return bool
    344      */
    345     public function is_writable($file) {
    346         return @is_writable($file);
    347     }
    348 
    349     /**
    350      *
     334    public function is_dir( $path ) {
     335        return @is_dir( $path );
     336    }
     337
     338    /**
     339     * @param string $file
     340     * @return bool
     341     */
     342    public function is_readable( $file ) {
     343        return @is_readable( $file );
     344    }
     345
     346    /**
     347     * @param string $file
     348     * @return bool
     349     */
     350    public function is_writable( $file ) {
     351        return @is_writable( $file );
     352    }
     353
     354    /**
    351355     * @param string $file
    352356     * @return int
    353357     */
    354     public function atime($file) {
    355         return @fileatime($file);
    356     }
    357 
    358     /**
    359      *
     358    public function atime( $file ) {
     359        return @fileatime( $file );
     360    }
     361
     362    /**
    360363     * @param string $file
    361364     * @return int
    362365     */
    363     public function mtime($file) {
    364         return @filemtime($file);
    365     }
    366 
    367     /**
    368      *
     366    public function mtime( $file ) {
     367        return @filemtime( $file );
     368    }
     369
     370    /**
    369371     * @param string $file
    370372     * @return int
    371373     */
    372     public function size($file) {
    373         return @filesize($file);
    374     }
    375 
    376     /**
    377      *
     374    public function size( $file ) {
     375        return @filesize( $file );
     376    }
     377
     378    /**
    378379     * @param string $file
    379380     * @param int $time
     
    381382     * @return bool
    382383     */
    383     public function touch($file, $time = 0, $atime = 0) {
    384         if ($time == 0)
     384    public function touch( $file, $time = 0, $atime = 0 ) {
     385        if ( $time == 0 ) {
    385386            $time = time();
    386         if ($atime == 0)
     387        }
     388        if ( $atime == 0 ) {
    387389            $atime = time();
    388         return @touch($file, $time, $atime);
    389     }
    390 
    391     /**
    392      *
     390        }
     391        return @touch( $file, $time, $atime );
     392    }
     393
     394    /**
    393395     * @param string $path
    394396     * @param mixed  $chmod
     
    397399     * @return bool
    398400     */
    399     public function mkdir($path, $chmod = false, $chown = false, $chgrp = false) {
     401    public function mkdir( $path, $chmod = false, $chown = false, $chgrp = false ) {
    400402        // Safe mode fails with a trailing slash under certain PHP versions.
    401         $path = untrailingslashit($path);
    402         if ( empty($path) )
    403             return false;
    404 
    405         if ( ! $chmod )
     403        $path = untrailingslashit( $path );
     404        if ( empty( $path ) ) {
     405            return false;
     406        }
     407
     408        if ( ! $chmod ) {
    406409            $chmod = FS_CHMOD_DIR;
    407 
    408         if ( ! @mkdir($path) )
    409             return false;
    410         $this->chmod($path, $chmod);
    411         if ( $chown )
    412             $this->chown($path, $chown);
    413         if ( $chgrp )
    414             $this->chgrp($path, $chgrp);
     410        }
     411
     412        if ( ! @mkdir( $path ) ) {
     413            return false;
     414        }
     415        $this->chmod( $path, $chmod );
     416        if ( $chown ) {
     417            $this->chown( $path, $chown );
     418        }
     419        if ( $chgrp ) {
     420            $this->chgrp( $path, $chgrp );
     421        }
    415422        return true;
    416423    }
    417424
    418425    /**
    419      *
    420426     * @param string $path
    421427     * @param bool $recursive
    422428     * @return bool
    423429     */
    424     public function rmdir($path, $recursive = false) {
    425         return $this->delete($path, $recursive);
    426     }
    427 
    428     /**
    429      *
     430    public function rmdir( $path, $recursive = false ) {
     431        return $this->delete( $path, $recursive );
     432    }
     433
     434    /**
    430435     * @param string $path
    431436     * @param bool $include_hidden
     
    433438     * @return bool|array
    434439     */
    435     public function dirlist($path, $include_hidden = true, $recursive = false) {
    436         if ( $this->is_file($path) ) {
    437             $limit_file = basename($path);
    438             $path = dirname($path);
     440    public function dirlist( $path, $include_hidden = true, $recursive = false ) {
     441        if ( $this->is_file( $path ) ) {
     442            $limit_file = basename( $path );
     443            $path       = dirname( $path );
    439444        } else {
    440445            $limit_file = false;
    441446        }
    442447
    443         if ( ! $this->is_dir($path) )
    444             return false;
    445 
    446         $dir = @dir($path);
    447         if ( ! $dir )
    448             return false;
     448        if ( ! $this->is_dir( $path ) ) {
     449            return false;
     450        }
     451
     452        $dir = @dir( $path );
     453        if ( ! $dir ) {
     454            return false;
     455        }
    449456
    450457        $ret = array();
    451458
    452         while (false !== ($entry = $dir->read()) ) {
    453             $struc = array();
     459        while ( false !== ( $entry = $dir->read() ) ) {
     460            $struc         = array();
    454461            $struc['name'] = $entry;
    455462
    456             if ( '.' == $struc['name'] || '..' == $struc['name'] )
     463            if ( '.' == $struc['name'] || '..' == $struc['name'] ) {
    457464                continue;
    458 
    459             if ( ! $include_hidden && '.' == $struc['name'][0] )
     465            }
     466
     467            if ( ! $include_hidden && '.' == $struc['name'][0] ) {
    460468                continue;
    461 
    462             if ( $limit_file && $struc['name'] != $limit_file)
     469            }
     470
     471            if ( $limit_file && $struc['name'] != $limit_file ) {
    463472                continue;
    464 
    465             $struc['perms']     = $this->gethchmod($path.'/'.$entry);
    466             $struc['permsn']    = $this->getnumchmodfromh($struc['perms']);
    467             $struc['number']    = false;
    468             $struc['owner']     = $this->owner($path.'/'.$entry);
    469             $struc['group']     = $this->group($path.'/'.$entry);
    470             $struc['size']      = $this->size($path.'/'.$entry);
    471             $struc['lastmodunix']= $this->mtime($path.'/'.$entry);
    472             $struc['lastmod']   = date('M j',$struc['lastmodunix']);
    473             $struc['time']      = date('h:i:s',$struc['lastmodunix']);
    474             $struc['type']      = $this->is_dir($path.'/'.$entry) ? 'd' : 'f';
     473            }
     474
     475            $struc['perms']       = $this->gethchmod( $path . '/' . $entry );
     476            $struc['permsn']      = $this->getnumchmodfromh( $struc['perms'] );
     477            $struc['number']      = false;
     478            $struc['owner']       = $this->owner( $path . '/' . $entry );
     479            $struc['group']       = $this->group( $path . '/' . $entry );
     480            $struc['size']        = $this->size( $path . '/' . $entry );
     481            $struc['lastmodunix'] = $this->mtime( $path . '/' . $entry );
     482            $struc['lastmod']     = date( 'M j', $struc['lastmodunix'] );
     483            $struc['time']        = date( 'h:i:s', $struc['lastmodunix'] );
     484            $struc['type']        = $this->is_dir( $path . '/' . $entry ) ? 'd' : 'f';
    475485
    476486            if ( 'd' == $struc['type'] ) {
    477                 if ( $recursive )
    478                     $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $include_hidden, $recursive);
    479                 else
     487                if ( $recursive ) {
     488                    $struc['files'] = $this->dirlist( $path . '/' . $struc['name'], $include_hidden, $recursive );
     489                } else {
    480490                    $struc['files'] = array();
     491                }
    481492            }
    482493
     
    484495        }
    485496        $dir->close();
    486         unset($dir);
     497        unset( $dir );
    487498        return $ret;
    488499    }
Note: See TracChangeset for help on using the changeset viewer.