web/wp-admin/includes/class-wp-filesystem-ftpsockets.php
changeset 194 32102edaa81b
parent 136 bde1974c263b
child 204 09a1c134465b
equal deleted inserted replaced
193:2f6f6f7551ca 194:32102edaa81b
    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 $errors = null;
    19 	var $errors = null;
    20 	var $options = array();
    20 	var $options = array();
    21 
    21 
    22 	function WP_Filesystem_ftpsockets($opt = '') {
    22 	function __construct($opt = '') {
    23 		$this->method = 'ftpsockets';
    23 		$this->method = 'ftpsockets';
    24 		$this->errors = new WP_Error();
    24 		$this->errors = new WP_Error();
    25 
    25 
    26 		//Check if possible to use ftp functions.
    26 		//Check if possible to use ftp functions.
    27 		if ( ! @include_once ABSPATH . 'wp-admin/includes/class-ftp.php' )
    27 		if ( ! @include_once ABSPATH . 'wp-admin/includes/class-ftp.php' )
    37 		if ( empty($opt['hostname']) )
    37 		if ( empty($opt['hostname']) )
    38 			$this->errors->add('empty_hostname', __('FTP hostname is required'));
    38 			$this->errors->add('empty_hostname', __('FTP hostname is required'));
    39 		else
    39 		else
    40 			$this->options['hostname'] = $opt['hostname'];
    40 			$this->options['hostname'] = $opt['hostname'];
    41 
    41 
    42 		if ( isset($opt['base']) && ! empty($opt['base']) )
    42 		if ( ! empty($opt['base']) )
    43 			$this->wp_base = $opt['base'];
    43 			$this->wp_base = $opt['base'];
    44 
    44 
    45 		// Check if the options provided are OK.
    45 		// Check if the options provided are OK.
    46 		if ( empty ($opt['username']) )
    46 		if ( empty ($opt['username']) )
    47 			$this->errors->add('empty_username', __('FTP username is required'));
    47 			$this->errors->add('empty_username', __('FTP username is required'));
   113 
   113 
   114 	function get_contents_array($file) {
   114 	function get_contents_array($file) {
   115 		return explode("\n", $this->get_contents($file) );
   115 		return explode("\n", $this->get_contents($file) );
   116 	}
   116 	}
   117 
   117 
   118 	function put_contents($file, $contents, $type = '' ) {
   118 	function put_contents($file, $contents, $mode = false ) {
   119 		if ( empty($type) )
       
   120 			$type = $this->is_binary($contents) ? FTP_BINARY : FTP_ASCII;
       
   121 
       
   122 		$this->ftp->SetType($type);
       
   123 
       
   124 		$temp = wp_tempnam( $file );
   119 		$temp = wp_tempnam( $file );
   125 		if ( ! $temphandle = fopen($temp, 'w+') ) {
   120 		if ( ! $temphandle = @fopen($temp, 'w+') ) {
   126 			unlink($temp);
   121 			unlink($temp);
   127 			return false;
   122 			return false;
   128 		}
   123 		}
   129 
   124 
   130 		fwrite($temphandle, $contents);
   125 		fwrite($temphandle, $contents);
   131 		fseek($temphandle, 0); //Skip back to the start of the file being written to
   126 		fseek($temphandle, 0); //Skip back to the start of the file being written to
   132 
   127 
       
   128 		$type = $this->is_binary($contents) ? FTP_BINARY : FTP_ASCII;
       
   129 		$this->ftp->SetType($type);
       
   130 
   133 		$ret = $this->ftp->fput($file, $temphandle);
   131 		$ret = $this->ftp->fput($file, $temphandle);
   134 
   132 
   135 		fclose($temphandle);
   133 		fclose($temphandle);
   136 		unlink($temp);
   134 		unlink($temp);
       
   135 
       
   136 		$this->chmod($file, $mode);
       
   137 
   137 		return $ret;
   138 		return $ret;
   138 	}
   139 	}
   139 
   140 
   140 	function cwd() {
   141 	function cwd() {
   141 		$cwd = $this->ftp->pwd();
   142 		$cwd = $this->ftp->pwd();
   151 	function chgrp($file, $group, $recursive = false ) {
   152 	function chgrp($file, $group, $recursive = false ) {
   152 		return false;
   153 		return false;
   153 	}
   154 	}
   154 
   155 
   155 	function chmod($file, $mode = false, $recursive = false ) {
   156 	function chmod($file, $mode = false, $recursive = false ) {
   156 
       
   157 		if ( ! $mode ) {
   157 		if ( ! $mode ) {
   158 			if ( $this->is_file($file) )
   158 			if ( $this->is_file($file) )
   159 				$mode = FS_CHMOD_FILE;
   159 				$mode = FS_CHMOD_FILE;
   160 			elseif ( $this->is_dir($file) )
   160 			elseif ( $this->is_dir($file) )
   161 				$mode = FS_CHMOD_DIR;
   161 				$mode = FS_CHMOD_DIR;
   162 			else
   162 			else
   163 				return false;
   163 				return false;
   164 		}
   164 		}
   165 
   165 
   166 		if ( ! $recursive || ! $this->is_dir($file) ) {
   166 		// chmod any sub-objects if recursive.
   167 			return $this->ftp->chmod($file, $mode);
   167 		if ( $recursive && $this->is_dir($file) ) {
   168 		}
   168 			$filelist = $this->dirlist($file);
   169 
   169 			foreach ( (array)$filelist as $filename => $filemeta )
   170 		//Is a directory, and we want recursive
   170 				$this->chmod($file . '/' . $filename, $mode, $recursive);
   171 		$filelist = $this->dirlist($file);
   171 		}
   172 		foreach ( $filelist as $filename )
   172 
   173 			$this->chmod($file . '/' . $filename, $mode, $recursive);
   173 		// chmod the file or directory
   174 
   174 		return $this->ftp->chmod($file, $mode);
   175 		return true;
       
   176 	}
   175 	}
   177 
   176 
   178 	function chown($file, $owner, $recursive = false ) {
   177 	function chown($file, $owner, $recursive = false ) {
   179 		return false;
   178 		return false;
   180 	}
   179 	}
   192 	function group($file) {
   191 	function group($file) {
   193 		$dir = $this->dirlist($file);
   192 		$dir = $this->dirlist($file);
   194 		return $dir[$file]['group'];
   193 		return $dir[$file]['group'];
   195 	}
   194 	}
   196 
   195 
   197 	function copy($source, $destination, $overwrite = false ) {
   196 	function copy($source, $destination, $overwrite = false, $mode = false) {
   198 		if ( ! $overwrite && $this->exists($destination) )
   197 		if ( ! $overwrite && $this->exists($destination) )
   199 			return false;
   198 			return false;
   200 
   199 
   201 		$content = $this->get_contents($source);
   200 		$content = $this->get_contents($source);
   202 		if ( false === $content )
   201 		if ( false === $content )
   203 			return false;
   202 			return false;
   204 
   203 
   205 		return $this->put_contents($destination, $content);
   204 		return $this->put_contents($destination, $content, $mode);
   206 	}
   205 	}
   207 
   206 
   208 	function move($source, $destination, $overwrite = false ) {
   207 	function move($source, $destination, $overwrite = false ) {
   209 		return $this->ftp->rename($source, $destination);
   208 		return $this->ftp->rename($source, $destination);
   210 	}
   209 	}
   211 
   210 
   212 	function delete($file, $recursive = false ) {
   211 	function delete($file, $recursive = false, $type = false) {
   213 		if ( empty($file) )
   212 		if ( empty($file) )
   214 			return false;
   213 			return false;
   215 		if ( $this->is_file($file) )
   214 		if ( 'f' == $type || $this->is_file($file) )
   216 			return $this->ftp->delete($file);
   215 			return $this->ftp->delete($file);
   217 		if ( !$recursive )
   216 		if ( !$recursive )
   218 			return $this->ftp->rmdir($file);
   217 			return $this->ftp->rmdir($file);
   219 
   218 
   220 		return $this->ftp->mdel($file);
   219 		return $this->ftp->mdel($file);
   223 	function exists($file) {
   222 	function exists($file) {
   224 		return $this->ftp->is_exists($file);
   223 		return $this->ftp->is_exists($file);
   225 	}
   224 	}
   226 
   225 
   227 	function is_file($file) {
   226 	function is_file($file) {
   228 		return $this->is_dir($file) ? false : true;
   227 		if ( $this->is_dir($file) )
       
   228 			return false;
       
   229 		if ( $this->exists($file) )
       
   230 			return true;
       
   231 		return false;
   229 	}
   232 	}
   230 
   233 
   231 	function is_dir($path) {
   234 	function is_dir($path) {
   232 		$cwd = $this->cwd();
   235 		$cwd = $this->cwd();
   233 		if ( $this->chdir($path) ) {
   236 		if ( $this->chdir($path) ) {
   262 	function touch($file, $time = 0, $atime = 0 ) {
   265 	function touch($file, $time = 0, $atime = 0 ) {
   263 		return false;
   266 		return false;
   264 	}
   267 	}
   265 
   268 
   266 	function mkdir($path, $chmod = false, $chown = false, $chgrp = false ) {
   269 	function mkdir($path, $chmod = false, $chown = false, $chgrp = false ) {
       
   270 		$path = untrailingslashit($path);
       
   271 		if ( empty($path) )
       
   272 			return false;
       
   273 
   267 		if ( ! $this->ftp->mkdir($path) )
   274 		if ( ! $this->ftp->mkdir($path) )
   268 			return false;
   275 			return false;
   269 		if ( ! $chmod )
   276 		if ( ! $chmod )
   270 			$chmod = FS_CHMOD_DIR;
   277 			$chmod = FS_CHMOD_DIR;
   271 		$this->chmod($path, $chmod);
   278 		$this->chmod($path, $chmod);
   275 			$this->chgrp($path, $chgrp);
   282 			$this->chgrp($path, $chgrp);
   276 		return true;
   283 		return true;
   277 	}
   284 	}
   278 
   285 
   279 	function rmdir($path, $recursive = false ) {
   286 	function rmdir($path, $recursive = false ) {
   280 		if ( ! $recursive )
   287 		$this->delete($path, $recursive);
   281 			return $this->ftp->rmdir($path);
       
   282 
       
   283 		return $this->ftp->mdel($path);
       
   284 	}
   288 	}
   285 
   289 
   286 	function dirlist($path = '.', $include_hidden = true, $recursive = false ) {
   290 	function dirlist($path = '.', $include_hidden = true, $recursive = false ) {
   287 		if ( $this->is_file($path) ) {
   291 		if ( $this->is_file($path) ) {
   288 			$limit_file = basename($path);
   292 			$limit_file = basename($path);
   290 		} else {
   294 		} else {
   291 			$limit_file = false;
   295 			$limit_file = false;
   292 		}
   296 		}
   293 
   297 
   294 		$list = $this->ftp->dirlist($path);
   298 		$list = $this->ftp->dirlist($path);
   295 		if ( ! $list )
   299 		if ( empty($list) && !$this->exists($path) )
   296 			return false;
   300 			return false;
   297 
   301 
   298 		$ret = array();
   302 		$ret = array();
   299 		foreach ( $list as $struc ) {
   303 		foreach ( $list as $struc ) {
   300 
   304 
   321 
   325 
   322 	function __destruct() {
   326 	function __destruct() {
   323 		$this->ftp->quit();
   327 		$this->ftp->quit();
   324 	}
   328 	}
   325 }
   329 }
   326 
       
   327 ?>