[ Index ]

PHP Cross Reference of Wordpress 2.7.1

title

Body

[close]

/wp-admin/includes/ -> class-wp-filesystem-ftpext.php (source)

   1  <?php
   2  /**
   3   * WordPress FTP Filesystem.
   4   *
   5   * @package WordPress
   6   * @subpackage Filesystem
   7   */
   8  
   9  /**
  10   * WordPress Filesystem Class for implementing FTP.
  11   *
  12   * @since 2.5
  13   * @package WordPress
  14   * @subpackage Filesystem
  15   * @uses WP_Filesystem_Base Extends class
  16   */
  17  class WP_Filesystem_FTPext extends WP_Filesystem_Base {
  18      var $link;
  19      var $timeout = 5;
  20      var $errors = array();
  21      var $options = array();
  22  
  23      var $permission = null;
  24  
  25  	function WP_Filesystem_FTPext($opt='') {
  26          $this->method = 'ftpext';
  27          $this->errors = new WP_Error();
  28  
  29          //Check if possible to use ftp functions.
  30          if ( ! extension_loaded('ftp') ) {
  31              $this->errors->add('no_ftp_ext', __('The ftp PHP extension is not available'));
  32              return false;
  33          }
  34  
  35          // Set defaults:
  36          if ( empty($opt['port']) )
  37              $this->options['port'] = 21;
  38          else
  39              $this->options['port'] = $opt['port'];
  40  
  41          if ( empty($opt['hostname']) )
  42              $this->errors->add('empty_hostname', __('FTP hostname is required'));
  43          else
  44              $this->options['hostname'] = $opt['hostname'];
  45  
  46          if ( isset($opt['base']) && ! empty($opt['base']) )
  47              $this->wp_base = $opt['base'];
  48  
  49          // Check if the options provided are OK.
  50          if ( empty ($opt['username']) )
  51              $this->errors->add('empty_username', __('FTP username is required'));
  52          else
  53              $this->options['username'] = $opt['username'];
  54  
  55          if ( empty ($opt['password']) )
  56              $this->errors->add('empty_password', __('FTP password is required'));
  57          else
  58              $this->options['password'] = $opt['password'];
  59  
  60          $this->options['ssl'] = false;
  61          if ( isset($opt['ssl']) )
  62              $this->options['ssl'] = ( !empty($opt['ssl']) );
  63          elseif ( isset( $opt['connection_type']) )
  64              $this->options['ssl'] = ( 'ftps' == $opt['connection_type'] );
  65      }
  66  
  67  	function connect() {
  68          if ( $this->options['ssl'] && function_exists('ftp_ssl_connect') )
  69              $this->link = @ftp_ssl_connect($this->options['hostname'], $this->options['port'],$this->timeout);
  70          else
  71              $this->link = @ftp_connect($this->options['hostname'], $this->options['port'],$this->timeout);
  72  
  73          if ( ! $this->link ) {
  74              $this->errors->add('connect', sprintf(__('Failed to connect to FTP Server %1$s:%2$s'), $this->options['hostname'], $this->options['port']));
  75              return false;
  76          }
  77  
  78          if ( ! @ftp_login($this->link,$this->options['username'], $this->options['password']) ) {
  79              $this->errors->add('auth', sprintf(__('Username/Password incorrect for %s'), $this->options['username']));
  80              return false;
  81          }
  82  
  83          //Set the Connection to use Passive FTP
  84          @ftp_pasv( $this->link, true );
  85  
  86          return true;
  87      }
  88  
  89  	function setDefaultPermissions($perm) {
  90          $this->permission = $perm;
  91      }
  92  
  93  	function get_contents($file, $type = '', $resumepos = 0 ){
  94          if( empty($type) )
  95              $type = FTP_BINARY;
  96  
  97          $temp = tmpfile();
  98          if ( ! $temp )
  99              return false;
 100  
 101          if( ! @ftp_fget($this->link, $temp, $file, $type, $resumepos) )
 102              return false;
 103  
 104          fseek($temp, 0); //Skip back to the start of the file being written to
 105          $contents = '';
 106  
 107          while ( ! feof($temp) )
 108              $contents .= fread($temp, 8192);
 109  
 110          fclose($temp);
 111          return $contents;
 112      }
 113  	function get_contents_array($file) {
 114          return explode("\n", $this->get_contents($file));
 115      }
 116  	function put_contents($file, $contents, $type = '' ) {
 117          if( empty($type) )
 118              $type = $this->is_binary($contents) ? FTP_BINARY : FTP_ASCII;
 119  
 120          $temp = tmpfile();
 121          if ( ! $temp )
 122              return false;
 123  
 124          fwrite($temp, $contents);
 125          fseek($temp, 0); //Skip back to the start of the file being written to
 126  
 127          $ret = @ftp_fput($this->link, $file, $temp, $type);
 128  
 129          fclose($temp);
 130          return $ret;
 131      }
 132  	function cwd() {
 133          $cwd = @ftp_pwd($this->link);
 134          if( $cwd )
 135              $cwd = trailingslashit($cwd);
 136          return $cwd;
 137      }
 138  	function chdir($dir) {
 139          return @ftp_chdir($dir);
 140      }
 141  	function chgrp($file, $group, $recursive = false ) {
 142          return false;
 143      }
 144  	function chmod($file, $mode = false, $recursive = false) {
 145          if( ! $mode )
 146              $mode = $this->permission;
 147          if( ! $mode )
 148              return false;
 149          if ( ! $this->exists($file) )
 150              return false;
 151          if ( ! $recursive || ! $this->is_dir($file) ) {
 152              if ( ! function_exists('ftp_chmod') )
 153                  return @ftp_site($this->link, sprintf('CHMOD %o %s', $mode, $file));
 154              return @ftp_chmod($this->link, $mode, $file);
 155          }
 156          //Is a directory, and we want recursive
 157          $filelist = $this->dirlist($file);
 158          foreach($filelist as $filename){
 159              $this->chmod($file . '/' . $filename, $mode, $recursive);
 160          }
 161          return true;
 162      }
 163  	function chown($file, $owner, $recursive = false ) {
 164          return false;
 165      }
 166  	function owner($file) {
 167          $dir = $this->dirlist($file);
 168          return $dir[$file]['owner'];
 169      }
 170  	function getchmod($file) {
 171          $dir = $this->dirlist($file);
 172          return $dir[$file]['permsn'];
 173      }
 174  	function group($file) {
 175          $dir = $this->dirlist($file);
 176          return $dir[$file]['group'];
 177      }
 178  	function copy($source, $destination, $overwrite = false ) {
 179          if( ! $overwrite && $this->exists($destination) )
 180              return false;
 181          $content = $this->get_contents($source);
 182          if( false === $content)
 183              return false;
 184          return $this->put_contents($destination, $content);
 185      }
 186  	function move($source, $destination, $overwrite = false) {
 187          return ftp_rename($this->link, $source, $destination);
 188      }
 189  
 190  	function delete($file,$recursive=false) {
 191          if ( $this->is_file($file) )
 192              return @ftp_delete($this->link, $file);
 193          if ( !$recursive )
 194              return @ftp_rmdir($this->link, $file);
 195          $filelist = $this->dirlist($file);
 196          foreach ((array) $filelist as $filename => $fileinfo) {
 197              $this->delete($file . '/' . $filename, $recursive);
 198          }
 199          return @ftp_rmdir($this->link, $file);
 200      }
 201  
 202  	function exists($file) {
 203          $list = ftp_rawlist($this->link, $file, false);
 204          if( ! $list )
 205              return false;
 206          return count($list) == 1 ? true : false;
 207      }
 208  	function is_file($file) {
 209          return $this->is_dir($file) ? false : true;
 210      }
 211  	function is_dir($path) {
 212          $cwd = $this->cwd();
 213          $result = @ftp_chdir($this->link, $path);
 214          if( $result && $path == $this->cwd() || $this->cwd() != $cwd ) {
 215              @ftp_chdir($this->link, $cwd);
 216              return true;
 217          }
 218          return false;
 219      }
 220  	function is_readable($file) {
 221          //Get dir list, Check if the file is writable by the current user??
 222          return true;
 223      }
 224  	function is_writable($file) {
 225          //Get dir list, Check if the file is writable by the current user??
 226          return true;
 227      }
 228  	function atime($file) {
 229          return false;
 230      }
 231  	function mtime($file) {
 232          return ftp_mdtm($this->link, $file);
 233      }
 234  	function size($file) {
 235          return ftp_size($this->link, $file);
 236      }
 237  	function touch($file, $time = 0, $atime = 0) {
 238          return false;
 239      }
 240  	function mkdir($path, $chmod = false, $chown = false, $chgrp = false) {
 241          if( !@ftp_mkdir($this->link, $path) )
 242              return false;
 243          if( $chmod )
 244              $this->chmod($path, $chmod);
 245          if( $chown )
 246              $this->chown($path, $chown);
 247          if( $chgrp )
 248              $this->chgrp($path, $chgrp);
 249          return true;
 250      }
 251  	function rmdir($path, $recursive = false) {
 252          if( ! $recursive )
 253              return @ftp_rmdir($this->link, $path);
 254  
 255          //TODO: Recursive Directory delete, Have to delete files from the folder first.
 256          //$dir = $this->dirlist($path);
 257          //foreach($dir as $file)
 258  
 259      }
 260  
 261  	function parselisting($line) {
 262          $is_windows = ($this->OS_remote == FTP_OS_Windows);
 263          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)) {
 264              $b = array();
 265              if ($lucifer[3]<70) { $lucifer[3] +=2000; } else { $lucifer[3]+=1900; } // 4digit year fix
 266              $b['isdir'] = ($lucifer[7]=="<DIR>");
 267              if ( $b['isdir'] )
 268                  $b['type'] = 'd';
 269              else
 270                  $b['type'] = 'f';
 271              $b['size'] = $lucifer[7];
 272              $b['month'] = $lucifer[1];
 273              $b['day'] = $lucifer[2];
 274              $b['year'] = $lucifer[3];
 275              $b['hour'] = $lucifer[4];
 276              $b['minute'] = $lucifer[5];
 277              $b['time'] = @mktime($lucifer[4]+(strcasecmp($lucifer[6],"PM")==0?12:0),$lucifer[5],0,$lucifer[1],$lucifer[2],$lucifer[3]);
 278              $b['am/pm'] = $lucifer[6];
 279              $b['name'] = $lucifer[8];
 280          } else if (!$is_windows && $lucifer=preg_split("/[ ]/",$line,9,PREG_SPLIT_NO_EMPTY)) {
 281              //echo $line."\n";
 282              $lcount=count($lucifer);
 283              if ($lcount<8) return '';
 284              $b = array();
 285              $b['isdir'] = $lucifer[0]{0} === "d";
 286              $b['islink'] = $lucifer[0]{0} === "l";
 287              if ( $b['isdir'] )
 288                  $b['type'] = 'd';
 289              elseif ( $b['islink'] )
 290                  $b['type'] = 'l';
 291              else
 292                  $b['type'] = 'f';
 293              $b['perms'] = $lucifer[0];
 294              $b['number'] = $lucifer[1];
 295              $b['owner'] = $lucifer[2];
 296              $b['group'] = $lucifer[3];
 297              $b['size'] = $lucifer[4];
 298              if ($lcount==8) {
 299                  sscanf($lucifer[5],"%d-%d-%d",$b['year'],$b['month'],$b['day']);
 300                  sscanf($lucifer[6],"%d:%d",$b['hour'],$b['minute']);
 301                  $b['time'] = @mktime($b['hour'],$b['minute'],0,$b['month'],$b['day'],$b['year']);
 302                  $b['name'] = $lucifer[7];
 303              } else {
 304                  $b['month'] = $lucifer[5];
 305                  $b['day'] = $lucifer[6];
 306                  if (preg_match("/([0-9]{2}):([0-9]{2})/",$lucifer[7],$l2)) {
 307                      $b['year'] = date("Y");
 308                      $b['hour'] = $l2[1];
 309                      $b['minute'] = $l2[2];
 310                  } else {
 311                      $b['year'] = $lucifer[7];
 312                      $b['hour'] = 0;
 313                      $b['minute'] = 0;
 314                  }
 315                  $b['time'] = strtotime(sprintf("%d %s %d %02d:%02d",$b['day'],$b['month'],$b['year'],$b['hour'],$b['minute']));
 316                  $b['name'] = $lucifer[8];
 317              }
 318          }
 319  
 320          return $b;
 321      }
 322  
 323  	function dirlist($path = '.', $incdot = false, $recursive = false) {
 324          if( $this->is_file($path) ) {
 325              $limitFile = basename($path);
 326              $path = dirname($path) . '/';
 327          } else {
 328              $limitFile = false;
 329          }
 330  
 331          $list = @ftp_rawlist($this->link, '-a ' . $path, false);
 332  
 333          if ( $list === false )
 334              return false;
 335  
 336          $dirlist = array();
 337          foreach ( $list as $k => $v ) {
 338              $entry = $this->parselisting($v);
 339              if ( empty($entry) )
 340                  continue;
 341  
 342              if ( '.' == $entry["name"] || '..' == $entry["name"] )
 343                  continue;
 344  
 345              $dirlist[ $entry['name'] ] = $entry;
 346          }
 347  
 348          if ( ! $dirlist )
 349              return false;
 350          if ( empty($dirlist) )
 351              return array();
 352  
 353          $ret = array();
 354          foreach ( $dirlist as $struc ) {
 355  
 356              if ( 'd' == $struc['type'] ) {
 357                  $struc['files'] = array();
 358  
 359                  if ( $incdot ){
 360                      //We're including the doted starts
 361                      if( '.' != $struc['name'] && '..' != $struc['name'] ){ //Ok, It isnt a special folder
 362                          if ($recursive)
 363                              $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $incdot, $recursive);
 364                      }
 365                  } else { //No dots
 366                      if ($recursive)
 367                          $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $incdot, $recursive);
 368                  }
 369              }
 370              //File
 371              $ret[$struc['name']] = $struc;
 372          }
 373          return $ret;
 374      }
 375  
 376  	function __destruct(){
 377          if( $this->link )
 378              ftp_close($this->link);
 379      }
 380  }
 381  
 382  ?>


Generated: Mon Mar 23 16:23:02 2009 Cross-referenced by PHPXref 0.7