Make WordPress Core

Ticket #7059: 7059.3.diff

File 7059.3.diff, 13.5 KB (added by DD32, 18 years ago)
  • 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

     
    44        var $permission = null;
    55        var $errors = array();
    66        function WP_Filesystem_Direct($arg) {
     7                $this->method = 'direct';
    78                $this->errors = new WP_Error();
    89                $this->permission = umask();
    910        }
     
    1920        function get_contents_array($file) {
    2021                return @file($file);
    2122        }
    22         function put_contents($file,$contents,$mode=false,$type='') {
    23                 if ( ! ($fp = @fopen($file,'w' . $type)) )
     23        function put_contents($file, $contents, $mode = false, $type = '') {
     24                if ( ! ($fp = @fopen($file, 'w' . $type)) )
    2425                        return false;
    25                 @fwrite($fp,$contents);
     26                @fwrite($fp, $contents);
    2627                @fclose($fp);
    2728                $this->chmod($file,$mode);
    2829                return true;
     
    3334        function chdir($dir) {
    3435                return @chdir($dir);
    3536        }
    36         function chgrp($file,$group,$recursive=false) {
     37        function chgrp($file, $group, $recursive = false) {
    3738                if( ! $this->exists($file) )
    3839                        return false;
    3940                if( ! $recursive )
     
    4849
    4950                return true;
    5051        }
    51         function chmod($file,$mode=false,$recursive=false) {
     52        function chmod($file, $mode = false, $recursive = false) {
    5253                if( ! $mode )
    5354                        $mode = $this->permission;
    5455                if( ! $this->exists($file) )
  • wp-admin/includes/class-wp-filesystem-ftpsockets.php

     
    11<?php
    2 class WP_Filesystem_ftpsockets{
     2class WP_Filesystem_ftpsockets extends WP_Filesystem_Base {
    33        var $ftp = false;
    44        var $timeout = 5;
    55        var $errors;
     
    8686                $this->permission = $perm;
    8787        }
    8888
    89         function get_contents($file,$type='',$resumepos=0){
     89        function get_contents($file, $type = '', $resumepos = 0){
    9090                if( ! $this->exists($file) )
    9191                        return false;
    9292
    9393                if( empty($type) ){
    94                         $extension = substr(strrchr($file, "."), 1);
     94                        $extension = substr(strrchr($file, '.'), 1);
    9595                        $type = isset($this->filetypes[ $extension ]) ? $this->filetypes[ $extension ] : FTP_AUTOASCII;
    9696                }
    9797                $this->ftp->SetType($type);
    98                 $temp = tmpfile();
    99                 if ( ! $temp )
     98                $temp = wp_tempnam( $file );
     99                if ( ! $temphandle = fopen($temp, 'w+') )
    100100                        return false;
    101                 if ( ! $this->ftp->fget($temp, $file) ) {
    102                         fclose($temp);
     101                if ( ! $this->ftp->fget($temphandle, $file) ) {
     102                        fclose($temphandle);
     103                        unlink($temp);
    103104                        return ''; //Blank document, File does exist, Its just blank.
    104105                }
    105                 fseek($temp, 0); //Skip back to the start of the file being written to
     106                fseek($temphandle, 0); //Skip back to the start of the file being written to
    106107                $contents = '';
    107                 while ( !feof($temp) )
    108                         $contents .= fread($temp, 8192);
    109                 fclose($temp);
     108                while ( ! feof($temphandle) )
     109                        $contents .= fread($temphandle, 8192);
     110                fclose($temphandle);
     111                unlink($temp);
    110112                return $contents;
    111113        }
    112114
    113115        function get_contents_array($file){
    114                 return explode("\n",$this->get_contents($file));
     116                return explode("\n", $this->get_contents($file) );
    115117        }
    116118
    117         function put_contents($file,$contents,$type=''){
     119        function put_contents($file, $contents, $type = '' ) {
    118120                if( empty($type) ){
    119                         $extension = substr(strrchr($file, "."), 1);
    120                         $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;
    121123                }
    122124                $this->ftp->SetType($type);
    123125
    124                 $temp = tmpfile();
    125                 if ( ! $temp )
     126                $temp = wp_tempnam( $file );
     127                if ( ! $temphandle = fopen($temp, 'w+') ){
     128                        unlink($temp);         
    126129                        return false;
    127                 fwrite($temp,$contents);
    128                 fseek($temp, 0); //Skip back to the start of the file being written to
    129                 $ret = $this->ftp->fput($file, $temp);
    130                 fclose($temp);
     130                }
     131                fwrite($temphandle, $contents);
     132                fseek($temphandle, 0); //Skip back to the start of the file being written to
     133                $ret = $this->ftp->fput($file, $temphandle);
     134                fclose($temphandle);
     135                unlink($temp);
    131136                return $ret;
    132137        }
    133138
    134         function cwd(){
     139        function cwd() {
    135140                $cwd = $this->ftp->pwd();
    136141                if( $cwd )
    137142                        $cwd = trailingslashit($cwd);
    138143                return $cwd;
    139144        }
    140145
    141         function chdir($file){
     146        function chdir($file) {
    142147                return $this->ftp->chdir($file);
    143148        }
    144149       
    145         function chgrp($file,$group,$recursive=false){
     150        function chgrp($file, $group, $recursive = false ) {
    146151                return false;
    147152        }
    148153
    149         function chmod($file,$mode=false,$recursive=false){
     154        function chmod($file, $mode = false, $recursive = false ){
    150155                if( ! $mode )
    151156                        $mode = $this->permission;
    152157                if( ! $mode )
    153158                        return false;
    154159                //if( ! $this->exists($file) )
    155160                //      return false;
    156                 if( ! $recursive || ! $this->is_dir($file) ){
     161                if( ! $recursive || ! $this->is_dir($file) ) {
    157162                        return $this->ftp->chmod($file,$mode);
    158163                }
    159164                //Is a directory, and we want recursive
    160165                $filelist = $this->dirlist($file);
    161166                foreach($filelist as $filename){
    162                         $this->chmod($file.'/'.$filename,$mode,$recursive);
     167                        $this->chmod($file . '/' . $filename, $mode, $recursive);
    163168                }
    164169                return true;
    165170        }
    166171
    167         function chown($file,$owner,$recursive=false){
     172        function chown($file, $owner, $recursive = false ) {
    168173                return false;
    169174        }
    170175
    171         function owner($file){
     176        function owner($file) {
    172177                $dir = $this->dirlist($file);
    173178                return $dir[$file]['owner'];
    174179        }
    175180
    176         function getchmod($file){
     181        function getchmod($file) {
    177182                $dir = $this->dirlist($file);
    178183                return $dir[$file]['permsn'];
    179184        }
    180185
    181         function gethchmod($file){
    182                 //From the PHP.net page for ...?
    183                 $perms = $this->getchmod($file);
    184                 if (($perms & 0xC000) == 0xC000) {
    185                         // Socket
    186                         $info = 's';
    187                 } elseif (($perms & 0xA000) == 0xA000) {
    188                         // Symbolic Link
    189                         $info = 'l';
    190                 } elseif (($perms & 0x8000) == 0x8000) {
    191                         // Regular
    192                         $info = '-';
    193                 } elseif (($perms & 0x6000) == 0x6000) {
    194                         // Block special
    195                         $info = 'b';
    196                 } elseif (($perms & 0x4000) == 0x4000) {
    197                         // Directory
    198                         $info = 'd';
    199                 } elseif (($perms & 0x2000) == 0x2000) {
    200                         // Character special
    201                         $info = 'c';
    202                 } elseif (($perms & 0x1000) == 0x1000) {
    203                         // FIFO pipe
    204                         $info = 'p';
    205                 } else {
    206                         // Unknown
    207                         $info = 'u';
    208                 }
    209 
    210                 // Owner
    211                 $info .= (($perms & 0x0100) ? 'r' : '-');
    212                 $info .= (($perms & 0x0080) ? 'w' : '-');
    213                 $info .= (($perms & 0x0040) ?
    214                                         (($perms & 0x0800) ? 's' : 'x' ) :
    215                                         (($perms & 0x0800) ? 'S' : '-'));
    216 
    217                 // Group
    218                 $info .= (($perms & 0x0020) ? 'r' : '-');
    219                 $info .= (($perms & 0x0010) ? 'w' : '-');
    220                 $info .= (($perms & 0x0008) ?
    221                                         (($perms & 0x0400) ? 's' : 'x' ) :
    222                                         (($perms & 0x0400) ? 'S' : '-'));
    223 
    224                 // World
    225                 $info .= (($perms & 0x0004) ? 'r' : '-');
    226                 $info .= (($perms & 0x0002) ? 'w' : '-');
    227                 $info .= (($perms & 0x0001) ?
    228                                         (($perms & 0x0200) ? 't' : 'x' ) :
    229                                         (($perms & 0x0200) ? 'T' : '-'));
    230                 return $info;
    231         }
    232 
    233         function getnumchmodfromh($mode) {
    234                 $realmode = "";
    235                 $legal =  array("","w","r","x","-");
    236                 $attarray = preg_split("//",$mode);
    237                 for($i=0;$i<count($attarray);$i++){
    238                    if($key = array_search($attarray[$i],$legal)){
    239                            $realmode .= $legal[$key];
    240                    }
    241                 }
    242                 $mode = str_pad($realmode,9,'-');
    243                 $trans = array('-'=>'0','r'=>'4','w'=>'2','x'=>'1');
    244                 $mode = strtr($mode,$trans);
    245                 $newmode = '';
    246                 $newmode .= $mode[0]+$mode[1]+$mode[2];
    247                 $newmode .= $mode[3]+$mode[4]+$mode[5];
    248                 $newmode .= $mode[6]+$mode[7]+$mode[8];
    249                 return $newmode;
    250         }
    251 
    252         function group($file){
     186        function group($file) {
    253187                $dir = $this->dirlist($file);
    254188                return $dir[$file]['group'];
    255189        }
    256190
    257         function copy($source,$destination,$overwrite=false){
     191        function copy($source, $destination, $overwrite = false ) {
    258192                if( ! $overwrite && $this->exists($destination) )
    259193                        return false;
    260194