wp/wp-admin/includes/class-wp-filesystem-ftpsockets.php
changeset 5 5e2f62d02dcd
parent 0 d970ebf37754
child 7 cf61fcea0001
equal deleted inserted replaced
4:346c88efed21 5:5e2f62d02dcd
     7  */
     7  */
     8 
     8 
     9 /**
     9 /**
    10  * WordPress Filesystem Class for implementing FTP Sockets.
    10  * WordPress Filesystem Class for implementing FTP Sockets.
    11  *
    11  *
    12  * @since 2.5
    12  * @since 2.5.0
    13  * @package WordPress
    13  * @package WordPress
    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 	/**
    19 	var $errors = null;
    19 	 * @var ftp
    20 	var $options = array();
    20 	 */
    21 
    21 	public $ftp;
    22 	function __construct($opt = '') {
    22 
       
    23 	public function __construct($opt = '') {
    23 		$this->method = 'ftpsockets';
    24 		$this->method = 'ftpsockets';
    24 		$this->errors = new WP_Error();
    25 		$this->errors = new WP_Error();
    25 
    26 
    26 		// Check if possible to use ftp functions.
    27 		// Check if possible to use ftp functions.
    27 		if ( ! @include_once ABSPATH . 'wp-admin/includes/class-ftp.php' )
    28 		if ( ! @include_once( ABSPATH . 'wp-admin/includes/class-ftp.php' ) ) {
    28 				return false;
    29 			return;
       
    30 		}
    29 		$this->ftp = new ftp();
    31 		$this->ftp = new ftp();
    30 
    32 
    31 		if ( empty($opt['port']) )
    33 		if ( empty($opt['port']) )
    32 			$this->options['port'] = 21;
    34 			$this->options['port'] = 21;
    33 		else
    35 		else
    35 
    37 
    36 		if ( empty($opt['hostname']) )
    38 		if ( empty($opt['hostname']) )
    37 			$this->errors->add('empty_hostname', __('FTP hostname is required'));
    39 			$this->errors->add('empty_hostname', __('FTP hostname is required'));
    38 		else
    40 		else
    39 			$this->options['hostname'] = $opt['hostname'];
    41 			$this->options['hostname'] = $opt['hostname'];
    40 
       
    41 		if ( ! empty($opt['base']) )
       
    42 			$this->wp_base = $opt['base'];
       
    43 
    42 
    44 		// Check if the options provided are OK.
    43 		// Check if the options provided are OK.
    45 		if ( empty ($opt['username']) )
    44 		if ( empty ($opt['username']) )
    46 			$this->errors->add('empty_username', __('FTP username is required'));
    45 			$this->errors->add('empty_username', __('FTP username is required'));
    47 		else
    46 		else
    51 			$this->errors->add('empty_password', __('FTP password is required'));
    50 			$this->errors->add('empty_password', __('FTP password is required'));
    52 		else
    51 		else
    53 			$this->options['password'] = $opt['password'];
    52 			$this->options['password'] = $opt['password'];
    54 	}
    53 	}
    55 
    54 
    56 	function connect() {
    55 	public function connect() {
    57 		if ( ! $this->ftp )
    56 		if ( ! $this->ftp )
    58 			return false;
    57 			return false;
    59 
    58 
    60 		$this->ftp->setTimeout(FS_CONNECT_TIMEOUT);
    59 		$this->ftp->setTimeout(FS_CONNECT_TIMEOUT);
    61 
    60 
    78 		$this->ftp->Passive( true );
    77 		$this->ftp->Passive( true );
    79 		$this->ftp->setTimeout( FS_TIMEOUT );
    78 		$this->ftp->setTimeout( FS_TIMEOUT );
    80 		return true;
    79 		return true;
    81 	}
    80 	}
    82 
    81 
    83 	function get_contents( $file ) {
    82 	/**
       
    83 	 * @param string $file
       
    84 	 * @return false|string
       
    85 	 */
       
    86 	public function get_contents( $file ) {
    84 		if ( ! $this->exists($file) )
    87 		if ( ! $this->exists($file) )
    85 			return false;
    88 			return false;
    86 
    89 
    87 		$temp = wp_tempnam( $file );
    90 		$temp = wp_tempnam( $file );
    88 
    91 
   110 
   113 
   111 		fclose($temphandle);
   114 		fclose($temphandle);
   112 		unlink($temp);
   115 		unlink($temp);
   113 		return $contents;
   116 		return $contents;
   114 	}
   117 	}
   115 
   118 	/**
   116 	function get_contents_array($file) {
   119 	 * @param string $file
       
   120 	 * @return array
       
   121 	 */
       
   122 	public function get_contents_array($file) {
   117 		return explode("\n", $this->get_contents($file) );
   123 		return explode("\n", $this->get_contents($file) );
   118 	}
   124 	}
   119 
   125 
   120 	function put_contents($file, $contents, $mode = false ) {
   126 	/**
       
   127 	 * @param string $file
       
   128 	 * @param string $contents
       
   129 	 * @param int|bool $mode
       
   130 	 * @return bool
       
   131 	 */
       
   132 	public function put_contents($file, $contents, $mode = false ) {
   121 		$temp = wp_tempnam( $file );
   133 		$temp = wp_tempnam( $file );
   122 		if ( ! $temphandle = @fopen($temp, 'w+') ) {
   134 		if ( ! $temphandle = @fopen($temp, 'w+') ) {
   123 			unlink($temp);
   135 			unlink($temp);
   124 			return false;
   136 			return false;
   125 		}
   137 		}
   149 		$this->chmod($file, $mode);
   161 		$this->chmod($file, $mode);
   150 
   162 
   151 		return $ret;
   163 		return $ret;
   152 	}
   164 	}
   153 
   165 
   154 	function cwd() {
   166 	public function cwd() {
   155 		$cwd = $this->ftp->pwd();
   167 		$cwd = $this->ftp->pwd();
   156 		if ( $cwd )
   168 		if ( $cwd )
   157 			$cwd = trailingslashit($cwd);
   169 			$cwd = trailingslashit($cwd);
   158 		return $cwd;
   170 		return $cwd;
   159 	}
   171 	}
   160 
   172 
   161 	function chdir($file) {
   173 	public function chdir($file) {
   162 		return $this->ftp->chdir($file);
   174 		return $this->ftp->chdir($file);
   163 	}
   175 	}
   164 
   176 
   165 	function chgrp($file, $group, $recursive = false ) {
   177 	/**
   166 		return false;
   178 	 * @param string $file
   167 	}
   179 	 * @param int|bool $mode
   168 
   180 	 * @param bool $recursive
   169 	function chmod($file, $mode = false, $recursive = false ) {
   181 	 * @return bool
       
   182 	 */
       
   183 	public function chmod($file, $mode = false, $recursive = false ) {
   170 		if ( ! $mode ) {
   184 		if ( ! $mode ) {
   171 			if ( $this->is_file($file) )
   185 			if ( $this->is_file($file) )
   172 				$mode = FS_CHMOD_FILE;
   186 				$mode = FS_CHMOD_FILE;
   173 			elseif ( $this->is_dir($file) )
   187 			elseif ( $this->is_dir($file) )
   174 				$mode = FS_CHMOD_DIR;
   188 				$mode = FS_CHMOD_DIR;
   185 
   199 
   186 		// chmod the file or directory
   200 		// chmod the file or directory
   187 		return $this->ftp->chmod($file, $mode);
   201 		return $this->ftp->chmod($file, $mode);
   188 	}
   202 	}
   189 
   203 
   190 	function owner($file) {
   204 	/**
       
   205 	 * @param string $file
       
   206 	 * @return string
       
   207 	 */
       
   208 	public function owner($file) {
   191 		$dir = $this->dirlist($file);
   209 		$dir = $this->dirlist($file);
   192 		return $dir[$file]['owner'];
   210 		return $dir[$file]['owner'];
   193 	}
   211 	}
   194 
   212 	/**
   195 	function getchmod($file) {
   213 	 * @param string $file
       
   214 	 * @return string
       
   215 	 */
       
   216 	public function getchmod($file) {
   196 		$dir = $this->dirlist($file);
   217 		$dir = $this->dirlist($file);
   197 		return $dir[$file]['permsn'];
   218 		return $dir[$file]['permsn'];
   198 	}
   219 	}
   199 
   220 	/**
   200 	function group($file) {
   221 	 * @param string $file
       
   222 	 * @return string
       
   223 	 */
       
   224 	public function group($file) {
   201 		$dir = $this->dirlist($file);
   225 		$dir = $this->dirlist($file);
   202 		return $dir[$file]['group'];
   226 		return $dir[$file]['group'];
   203 	}
   227 	}
   204 
   228 	/**
   205 	function copy($source, $destination, $overwrite = false, $mode = false) {
   229 	 * @param string $source
       
   230 	 * @param string $destination
       
   231 	 * @param bool $overwrite
       
   232 	 * @param int|bool $mode
       
   233 	 * @return bool
       
   234 	 */
       
   235 	public function copy($source, $destination, $overwrite = false, $mode = false) {
   206 		if ( ! $overwrite && $this->exists($destination) )
   236 		if ( ! $overwrite && $this->exists($destination) )
   207 			return false;
   237 			return false;
   208 
   238 
   209 		$content = $this->get_contents($source);
   239 		$content = $this->get_contents($source);
   210 		if ( false === $content )
   240 		if ( false === $content )
   211 			return false;
   241 			return false;
   212 
   242 
   213 		return $this->put_contents($destination, $content, $mode);
   243 		return $this->put_contents($destination, $content, $mode);
   214 	}
   244 	}
   215 
   245 	/**
   216 	function move($source, $destination, $overwrite = false ) {
   246 	 * @param string $source
       
   247 	 * @param string $destination
       
   248 	 * @param bool $overwrite
       
   249 	 * @return bool
       
   250 	 */
       
   251 	public function move($source, $destination, $overwrite = false ) {
   217 		return $this->ftp->rename($source, $destination);
   252 		return $this->ftp->rename($source, $destination);
   218 	}
   253 	}
   219 
   254 	/**
   220 	function delete($file, $recursive = false, $type = false) {
   255 	 * @param string $file
       
   256 	 * @param bool $recursive
       
   257 	 * @param string $type
       
   258 	 * @return bool
       
   259 	 */
       
   260 	public function delete($file, $recursive = false, $type = false) {
   221 		if ( empty($file) )
   261 		if ( empty($file) )
   222 			return false;
   262 			return false;
   223 		if ( 'f' == $type || $this->is_file($file) )
   263 		if ( 'f' == $type || $this->is_file($file) )
   224 			return $this->ftp->delete($file);
   264 			return $this->ftp->delete($file);
   225 		if ( !$recursive )
   265 		if ( !$recursive )
   226 			return $this->ftp->rmdir($file);
   266 			return $this->ftp->rmdir($file);
   227 
   267 
   228 		return $this->ftp->mdel($file);
   268 		return $this->ftp->mdel($file);
   229 	}
   269 	}
   230 
   270 
   231 	function exists( $file ) {
   271 	/**
       
   272 	 * @param string $file
       
   273 	 * @return bool
       
   274 	 */
       
   275 	public function exists( $file ) {
   232 		$list = $this->ftp->nlist( $file );
   276 		$list = $this->ftp->nlist( $file );
       
   277 
       
   278 		if ( empty( $list ) && $this->is_dir( $file ) ) {
       
   279 			return true; // File is an empty directory.
       
   280 		}
       
   281 
   233 		return !empty( $list ); //empty list = no file, so invert.
   282 		return !empty( $list ); //empty list = no file, so invert.
   234 		// return $this->ftp->is_exists($file); has issues with ABOR+426 responses on the ncFTPd server
   283 		// Return $this->ftp->is_exists($file); has issues with ABOR+426 responses on the ncFTPd server.
   235 	}
   284 	}
   236 
   285 
   237 	function is_file($file) {
   286 	/**
       
   287 	 * @param string $file
       
   288 	 * @return bool
       
   289 	 */
       
   290 	public function is_file($file) {
   238 		if ( $this->is_dir($file) )
   291 		if ( $this->is_dir($file) )
   239 			return false;
   292 			return false;
   240 		if ( $this->exists($file) )
   293 		if ( $this->exists($file) )
   241 			return true;
   294 			return true;
   242 		return false;
   295 		return false;
   243 	}
   296 	}
   244 
   297 
   245 	function is_dir($path) {
   298 	/**
       
   299 	 * @param string $path
       
   300 	 * @return bool
       
   301 	 */
       
   302 	public function is_dir($path) {
   246 		$cwd = $this->cwd();
   303 		$cwd = $this->cwd();
   247 		if ( $this->chdir($path) ) {
   304 		if ( $this->chdir($path) ) {
   248 			$this->chdir($cwd);
   305 			$this->chdir($cwd);
   249 			return true;
   306 			return true;
   250 		}
   307 		}
   251 		return false;
   308 		return false;
   252 	}
   309 	}
   253 
   310 
   254 	function is_readable($file) {
   311 	/**
       
   312 	 * @param string $file
       
   313 	 * @return bool
       
   314 	 */
       
   315 	public function is_readable($file) {
   255 		return true;
   316 		return true;
   256 	}
   317 	}
   257 
   318 
   258 	function is_writable($file) {
   319 	/**
       
   320 	 * @param string $file
       
   321 	 * @return bool
       
   322 	 */
       
   323 	public function is_writable($file) {
   259 		return true;
   324 		return true;
   260 	}
   325 	}
   261 
   326 
   262 	function atime($file) {
   327 	/**
       
   328 	 * @param string $file
       
   329 	 * @return bool
       
   330 	 */
       
   331 	public function atime($file) {
   263 		return false;
   332 		return false;
   264 	}
   333 	}
   265 
   334 
   266 	function mtime($file) {
   335 	/**
       
   336 	 * @param string $file
       
   337 	 * @return int
       
   338 	 */
       
   339 	public function mtime($file) {
   267 		return $this->ftp->mdtm($file);
   340 		return $this->ftp->mdtm($file);
   268 	}
   341 	}
   269 
   342 
   270 	function size($file) {
   343 	/**
       
   344 	 * @param string $file
       
   345 	 * @return int
       
   346 	 */
       
   347 	public function size($file) {
   271 		return $this->ftp->filesize($file);
   348 		return $this->ftp->filesize($file);
   272 	}
   349 	}
   273 
   350 	/**
   274 	function touch($file, $time = 0, $atime = 0 ) {
   351 	 * @param string $file
       
   352 	 * @param int $time
       
   353 	 * @param int $atime
       
   354 	 * @return bool
       
   355 	 */
       
   356 	public function touch($file, $time = 0, $atime = 0 ) {
   275 		return false;
   357 		return false;
   276 	}
   358 	}
   277 
   359 
   278 	function mkdir($path, $chmod = false, $chown = false, $chgrp = false ) {
   360 	/**
       
   361 	 * @param string $path
       
   362 	 * @param mixed $chmod
       
   363 	 * @param mixed $chown
       
   364 	 * @param mixed $chgrp
       
   365 	 * @return bool
       
   366 	 */
       
   367 	public function mkdir($path, $chmod = false, $chown = false, $chgrp = false ) {
   279 		$path = untrailingslashit($path);
   368 		$path = untrailingslashit($path);
   280 		if ( empty($path) )
   369 		if ( empty($path) )
   281 			return false;
   370 			return false;
   282 
   371 
   283 		if ( ! $this->ftp->mkdir($path) )
   372 		if ( ! $this->ftp->mkdir($path) )
   284 			return false;
   373 			return false;
   285 		if ( ! $chmod )
   374 		if ( ! $chmod )
   286 			$chmod = FS_CHMOD_DIR;
   375 			$chmod = FS_CHMOD_DIR;
   287 		$this->chmod($path, $chmod);
   376 		$this->chmod($path, $chmod);
   288 		if ( $chown )
       
   289 			$this->chown($path, $chown);
       
   290 		if ( $chgrp )
       
   291 			$this->chgrp($path, $chgrp);
       
   292 		return true;
   377 		return true;
   293 	}
   378 	}
   294 
   379 
   295 	function rmdir($path, $recursive = false ) {
   380 	/**
       
   381 	 * @param sting $path
       
   382 	 * @param bool $recursive
       
   383 	 */
       
   384 	public function rmdir($path, $recursive = false ) {
   296 		$this->delete($path, $recursive);
   385 		$this->delete($path, $recursive);
   297 	}
   386 	}
   298 
   387 
   299 	function dirlist($path = '.', $include_hidden = true, $recursive = false ) {
   388 	/**
       
   389 	 * @param string $path
       
   390 	 * @param bool $include_hidden
       
   391 	 * @param bool $recursive
       
   392 	 * @return bool|array
       
   393 	 */
       
   394 	public function dirlist($path = '.', $include_hidden = true, $recursive = false ) {
   300 		if ( $this->is_file($path) ) {
   395 		if ( $this->is_file($path) ) {
   301 			$limit_file = basename($path);
   396 			$limit_file = basename($path);
   302 			$path = dirname($path) . '/';
   397 			$path = dirname($path) . '/';
   303 		} else {
   398 		} else {
   304 			$limit_file = false;
   399 			$limit_file = false;
   343 		reset_mbstring_encoding();
   438 		reset_mbstring_encoding();
   344 
   439 
   345 		return $ret;
   440 		return $ret;
   346 	}
   441 	}
   347 
   442 
   348 	function __destruct() {
   443 	public function __destruct() {
   349 		$this->ftp->quit();
   444 		$this->ftp->quit();
   350 	}
   445 	}
   351 }
   446 }