[ Index ]

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


Generated: Fri Jan 8 00:19:48 2010 Cross-referenced by PHPXref 0.7