web/wp-admin/includes/class-wp-filesystem-ftpsockets.php
branchwordpress
changeset 132 4d4862461b8d
parent 109 03b0d1493584
equal deleted inserted replaced
131:a4642baaf829 132:4d4862461b8d
    14  * @subpackage Filesystem
    14  * @subpackage Filesystem
    15  * @uses WP_Filesystem_Base Extends class
    15  * @uses WP_Filesystem_Base Extends class
    16  */
    16  */
    17 class WP_Filesystem_ftpsockets extends WP_Filesystem_Base {
    17 class WP_Filesystem_ftpsockets extends WP_Filesystem_Base {
    18 	var $ftp = false;
    18 	var $ftp = false;
    19 	var $timeout = 5;
       
    20 	var $errors = null;
    19 	var $errors = null;
    21 	var $options = array();
    20 	var $options = array();
    22 
       
    23 	var $permission = null;
       
    24 
    21 
    25 	function WP_Filesystem_ftpsockets($opt = '') {
    22 	function WP_Filesystem_ftpsockets($opt = '') {
    26 		$this->method = 'ftpsockets';
    23 		$this->method = 'ftpsockets';
    27 		$this->errors = new WP_Error();
    24 		$this->errors = new WP_Error();
    28 
    25 
    29 		//Check if possible to use ftp functions.
    26 		//Check if possible to use ftp functions.
    30 		if( ! @include_once ABSPATH . 'wp-admin/includes/class-ftp.php' )
    27 		if ( ! @include_once ABSPATH . 'wp-admin/includes/class-ftp.php' )
    31 				return false;
    28 				return false;
    32 		$this->ftp = new ftp();
    29 		$this->ftp = new ftp();
    33 
    30 
    34 		//Set defaults:
    31 		//Set defaults:
    35 		if ( empty($opt['port']) )
    32 		if ( empty($opt['port']) )
    59 
    56 
    60 	function connect() {
    57 	function connect() {
    61 		if ( ! $this->ftp )
    58 		if ( ! $this->ftp )
    62 			return false;
    59 			return false;
    63 
    60 
    64 		//$this->ftp->Verbose = true;
    61 		$this->ftp->setTimeout(FS_CONNECT_TIMEOUT);
    65 
    62 
    66 		if ( ! $this->ftp->SetServer($this->options['hostname'], $this->options['port']) ) {
    63 		if ( ! $this->ftp->SetServer($this->options['hostname'], $this->options['port']) ) {
    67 			$this->errors->add('connect', sprintf(__('Failed to connect to FTP Server %1$s:%2$s'), $this->options['hostname'], $this->options['port']));
    64 			$this->errors->add('connect', sprintf(__('Failed to connect to FTP Server %1$s:%2$s'), $this->options['hostname'], $this->options['port']));
    68 			return false;
    65 			return false;
    69 		}
    66 		}
       
    67 
    70 		if ( ! $this->ftp->connect() ) {
    68 		if ( ! $this->ftp->connect() ) {
    71 			$this->errors->add('connect', sprintf(__('Failed to connect to FTP Server %1$s:%2$s'), $this->options['hostname'], $this->options['port']));
    69 			$this->errors->add('connect', sprintf(__('Failed to connect to FTP Server %1$s:%2$s'), $this->options['hostname'], $this->options['port']));
    72 			return false;
    70 			return false;
    73 		}
    71 		}
    74 
    72 
    77 			return false;
    75 			return false;
    78 		}
    76 		}
    79 
    77 
    80 		$this->ftp->SetType(FTP_AUTOASCII);
    78 		$this->ftp->SetType(FTP_AUTOASCII);
    81 		$this->ftp->Passive(true);
    79 		$this->ftp->Passive(true);
    82 		return true;
    80 		$this->ftp->setTimeout(FS_TIMEOUT);
    83 	}
    81 		return true;
    84 
       
    85 	function setDefaultPermissions($perm) {
       
    86 		$this->permission = $perm;
       
    87 	}
    82 	}
    88 
    83 
    89 	function get_contents($file, $type = '', $resumepos = 0) {
    84 	function get_contents($file, $type = '', $resumepos = 0) {
    90 		if( ! $this->exists($file) )
    85 		if ( ! $this->exists($file) )
    91 			return false;
    86 			return false;
    92 
    87 
    93 		if( empty($type) )
    88 		if ( empty($type) )
    94 			$type = FTP_AUTOASCII;
    89 			$type = FTP_AUTOASCII;
    95 		$this->ftp->SetType($type);
    90 		$this->ftp->SetType($type);
    96 
    91 
    97 		$temp = wp_tempnam( $file );
    92 		$temp = wp_tempnam( $file );
    98 
    93 
   119 	function get_contents_array($file) {
   114 	function get_contents_array($file) {
   120 		return explode("\n", $this->get_contents($file) );
   115 		return explode("\n", $this->get_contents($file) );
   121 	}
   116 	}
   122 
   117 
   123 	function put_contents($file, $contents, $type = '' ) {
   118 	function put_contents($file, $contents, $type = '' ) {
   124 		if( empty($type) )
   119 		if ( empty($type) )
   125 			$type = $this->is_binary($contents) ? FTP_BINARY : FTP_ASCII;
   120 			$type = $this->is_binary($contents) ? FTP_BINARY : FTP_ASCII;
   126 
   121 
   127 		$this->ftp->SetType($type);
   122 		$this->ftp->SetType($type);
   128 
   123 
   129 		$temp = wp_tempnam( $file );
   124 		$temp = wp_tempnam( $file );
   130 		if ( ! $temphandle = fopen($temp, 'w+') ){
   125 		if ( ! $temphandle = fopen($temp, 'w+') ) {
   131 			unlink($temp);
   126 			unlink($temp);
   132 			return false;
   127 			return false;
   133 		}
   128 		}
   134 
   129 
   135 		fwrite($temphandle, $contents);
   130 		fwrite($temphandle, $contents);
   142 		return $ret;
   137 		return $ret;
   143 	}
   138 	}
   144 
   139 
   145 	function cwd() {
   140 	function cwd() {
   146 		$cwd = $this->ftp->pwd();
   141 		$cwd = $this->ftp->pwd();
   147 		if( $cwd )
   142 		if ( $cwd )
   148 			$cwd = trailingslashit($cwd);
   143 			$cwd = trailingslashit($cwd);
   149 		return $cwd;
   144 		return $cwd;
   150 	}
   145 	}
   151 
   146 
   152 	function chdir($file) {
   147 	function chdir($file) {
   156 	function chgrp($file, $group, $recursive = false ) {
   151 	function chgrp($file, $group, $recursive = false ) {
   157 		return false;
   152 		return false;
   158 	}
   153 	}
   159 
   154 
   160 	function chmod($file, $mode = false, $recursive = false ) {
   155 	function chmod($file, $mode = false, $recursive = false ) {
   161 		if( ! $mode )
   156 
   162 			$mode = $this->permission;
   157 		if ( ! $mode ) {
   163 		if( ! $mode )
   158 			if ( $this->is_file($file) )
   164 			return false;
   159 				$mode = FS_CHMOD_FILE;
   165 		//if( ! $this->exists($file) )
   160 			elseif ( $this->is_dir($file) )
   166 		//	return false;
   161 				$mode = FS_CHMOD_DIR;
   167 		if( ! $recursive || ! $this->is_dir($file) ) {
   162 			else
   168 			return $this->ftp->chmod($file,$mode);
   163 				return false;
   169 		}
   164 		}
       
   165 
       
   166 		if ( ! $recursive || ! $this->is_dir($file) ) {
       
   167 			return $this->ftp->chmod($file, $mode);
       
   168 		}
       
   169 
   170 		//Is a directory, and we want recursive
   170 		//Is a directory, and we want recursive
   171 		$filelist = $this->dirlist($file);
   171 		$filelist = $this->dirlist($file);
   172 		foreach($filelist as $filename){
   172 		foreach ( $filelist as $filename )
   173 			$this->chmod($file . '/' . $filename, $mode, $recursive);
   173 			$this->chmod($file . '/' . $filename, $mode, $recursive);
   174 		}
   174 
   175 		return true;
   175 		return true;
   176 	}
   176 	}
   177 
   177 
   178 	function chown($file, $owner, $recursive = false ) {
   178 	function chown($file, $owner, $recursive = false ) {
   179 		return false;
   179 		return false;
   193 		$dir = $this->dirlist($file);
   193 		$dir = $this->dirlist($file);
   194 		return $dir[$file]['group'];
   194 		return $dir[$file]['group'];
   195 	}
   195 	}
   196 
   196 
   197 	function copy($source, $destination, $overwrite = false ) {
   197 	function copy($source, $destination, $overwrite = false ) {
   198 		if( ! $overwrite && $this->exists($destination) )
   198 		if ( ! $overwrite && $this->exists($destination) )
   199 			return false;
   199 			return false;
   200 
   200 
   201 		$content = $this->get_contents($source);
   201 		$content = $this->get_contents($source);
   202 		if ( false === $content )
   202 		if ( false === $content )
   203 			return false;
   203 			return false;
   262 	function touch($file, $time = 0, $atime = 0 ) {
   262 	function touch($file, $time = 0, $atime = 0 ) {
   263 		return false;
   263 		return false;
   264 	}
   264 	}
   265 
   265 
   266 	function mkdir($path, $chmod = false, $chown = false, $chgrp = false ) {
   266 	function mkdir($path, $chmod = false, $chown = false, $chgrp = false ) {
   267 		if( ! $this->ftp->mkdir($path) )
   267 		if ( ! $this->ftp->mkdir($path) )
   268 			return false;
   268 			return false;
   269 		if( $chmod )
   269 		if ( ! $chmod )
   270 			$this->chmod($path, $chmod);
   270 			$chmod = FS_CHMOD_DIR;
   271 		if( $chown )
   271 		$this->chmod($path, $chmod);
       
   272 		if ( $chown )
   272 			$this->chown($path, $chown);
   273 			$this->chown($path, $chown);
   273 		if( $chgrp )
   274 		if ( $chgrp )
   274 			$this->chgrp($path, $chgrp);
   275 			$this->chgrp($path, $chgrp);
   275 		return true;
   276 		return true;
   276 	}
   277 	}
   277 
   278 
   278 	function rmdir($path, $recursive = false ) {
   279 	function rmdir($path, $recursive = false ) {
   279 		if( ! $recursive )
   280 		if ( ! $recursive )
   280 			return $this->ftp->rmdir($path);
   281 			return $this->ftp->rmdir($path);
   281 
   282 
   282 		return $this->ftp->mdel($path);
   283 		return $this->ftp->mdel($path);
   283 	}
   284 	}
   284 
   285 
   285 	function dirlist($path = '.', $incdot = false, $recursive = false ) {
   286 	function dirlist($path = '.', $include_hidden = true, $recursive = false ) {
   286 		if( $this->is_file($path) ) {
   287 		if ( $this->is_file($path) ) {
   287 			$limitFile = basename($path);
   288 			$limit_file = basename($path);
   288 			$path = dirname($path) . '/';
   289 			$path = dirname($path) . '/';
   289 		} else {
   290 		} else {
   290 			$limitFile = false;
   291 			$limit_file = false;
   291 		}
   292 		}
   292 
   293 
   293 		$list = $this->ftp->dirlist($path);
   294 		$list = $this->ftp->dirlist($path);
   294 		if( ! $list )
   295 		if ( ! $list )
   295 			return false;
   296 			return false;
   296 		if( empty($list) )
       
   297 			return array();
       
   298 
   297 
   299 		$ret = array();
   298 		$ret = array();
   300 		foreach ( $list as $struc ) {
   299 		foreach ( $list as $struc ) {
   301 
   300 
       
   301 			if ( '.' == $struc['name'] || '..' == $struc['name'] )
       
   302 				continue;
       
   303 
       
   304 			if ( ! $include_hidden && '.' == $struc['name'][0] )
       
   305 				continue;
       
   306 
       
   307 			if ( $limit_file && $struc['name'] != $limit_file )
       
   308 				continue;
       
   309 
   302 			if ( 'd' == $struc['type'] ) {
   310 			if ( 'd' == $struc['type'] ) {
   303 				$struc['files'] = array();
   311 				if ( $recursive )
   304 
   312 					$struc['files'] = $this->dirlist($path . '/' . $struc['name'], $include_hidden, $recursive);
   305 				if ( $incdot ){
   313 				else
   306 					//We're including the doted starts
   314 					$struc['files'] = array();
   307 					if( '.' != $struc['name'] && '..' != $struc['name'] ){ //Ok, It isnt a special folder
       
   308 						if ($recursive)
       
   309 							$struc['files'] = $this->dirlist($path . '/' . $struc['name'], $incdot, $recursive);
       
   310 					}
       
   311 				} else { //No dots
       
   312 					if ($recursive)
       
   313 						$struc['files'] = $this->dirlist($path . '/' . $struc['name'], $incdot, $recursive);
       
   314 				}
       
   315 			}
   315 			}
   316 			//File
   316 
   317 			$ret[$struc['name']] = $struc;
   317 			$ret[ $struc['name'] ] = $struc;
   318 		}
   318 		}
   319 		return $ret;
   319 		return $ret;
   320 	}
   320 	}
   321 
   321 
   322 	function __destruct() {
   322 	function __destruct() {