includes/functions_transfer.php
changeset 0 ceef6d64c21d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/includes/functions_transfer.php	Wed Mar 07 16:41:10 2012 +0400
     1.3 @@ -0,0 +1,906 @@
     1.4 +<?php
     1.5 +/**
     1.6 +*
     1.7 +* @package phpBB3
     1.8 +* @version $Id$
     1.9 +* @copyright (c) 2005 phpBB Group
    1.10 +* @license http://opensource.org/licenses/gpl-license.php GNU Public License
    1.11 +*
    1.12 +*/
    1.13 +
    1.14 +/**
    1.15 +* @ignore
    1.16 +*/
    1.17 +if (!defined('IN_PHPBB'))
    1.18 +{
    1.19 +	exit;
    1.20 +}
    1.21 +
    1.22 +/**
    1.23 +* Transfer class, wrapper for ftp/sftp/ssh
    1.24 +* @package phpBB3
    1.25 +*/
    1.26 +class transfer
    1.27 +{
    1.28 +	var $connection;
    1.29 +	var $host;
    1.30 +	var $port;
    1.31 +	var $username;
    1.32 +	var $password;
    1.33 +	var $timeout;
    1.34 +	var $root_path;
    1.35 +	var $tmp_path;
    1.36 +	var $file_perms;
    1.37 +	var $dir_perms;
    1.38 +
    1.39 +	/**
    1.40 +	* Constructor - init some basic values
    1.41 +	*/
    1.42 +	function transfer()
    1.43 +	{
    1.44 +		global $phpbb_root_path;
    1.45 +
    1.46 +		$this->file_perms	= 0644;
    1.47 +		$this->dir_perms	= 0777;
    1.48 +
    1.49 +		// We use the store directory as temporary path to circumvent open basedir restrictions
    1.50 +		$this->tmp_path = $phpbb_root_path . 'store/';
    1.51 +	}
    1.52 +
    1.53 +	/**
    1.54 +	* Write file to location
    1.55 +	*/
    1.56 +	function write_file($destination_file = '', $contents = '')
    1.57 +	{
    1.58 +		global $phpbb_root_path;
    1.59 +
    1.60 +		$destination_file = $this->root_path . str_replace($phpbb_root_path, '', $destination_file);
    1.61 +
    1.62 +		// need to create a temp file and then move that temp file.
    1.63 +		// ftp functions can only move files around and can't create.
    1.64 +		// This means that the users will need to have access to write
    1.65 +		// temporary files or have write access on a folder within phpBB
    1.66 +		// like the cache folder. If the user can't do either, then
    1.67 +		// he/she needs to use the fsock ftp method
    1.68 +		$temp_name = tempnam($this->tmp_path, 'transfer_');
    1.69 +		@unlink($temp_name);
    1.70 +
    1.71 +		$fp = @fopen($temp_name, 'w');
    1.72 +
    1.73 +		if (!$fp)
    1.74 +		{
    1.75 +			trigger_error('Unable to create temporary file ' . $temp_name, E_USER_ERROR);
    1.76 +		}
    1.77 +
    1.78 +		@fwrite($fp, $contents);
    1.79 +		@fclose($fp);
    1.80 +
    1.81 +		$result = $this->overwrite_file($temp_name, $destination_file);
    1.82 +
    1.83 +		// remove temporary file now
    1.84 +		@unlink($temp_name);
    1.85 +
    1.86 +		return $result;
    1.87 +	}
    1.88 +
    1.89 +	/**
    1.90 +	* Moving file into location. If the destination file already exists it gets overwritten
    1.91 +	*/
    1.92 +	function overwrite_file($source_file, $destination_file)
    1.93 +	{
    1.94 +		/**
    1.95 +		* @todo generally think about overwriting files in another way, by creating a temporary file and then renaming it
    1.96 +		* @todo check for the destination file existance too
    1.97 +		*/
    1.98 +		$this->_delete($destination_file);
    1.99 +		$result = $this->_put($source_file, $destination_file);
   1.100 +		$this->_chmod($destination_file, $this->file_perms);
   1.101 +
   1.102 +		return $result;
   1.103 +	}
   1.104 +
   1.105 +	/**
   1.106 +	* Create directory structure
   1.107 +	*/
   1.108 +	function make_dir($dir)
   1.109 +	{
   1.110 +		global $phpbb_root_path;
   1.111 +
   1.112 +		$dir = str_replace($phpbb_root_path, '', $dir);
   1.113 +		$dir = explode('/', $dir);
   1.114 +		$dirs = '';
   1.115 +
   1.116 +		for ($i = 0, $total = sizeof($dir); $i < $total; $i++)
   1.117 +		{
   1.118 +			$result = true;
   1.119 +
   1.120 +			if (strpos($dir[$i], '.') === 0)
   1.121 +			{
   1.122 +				continue;
   1.123 +			}
   1.124 +			$cur_dir = $dir[$i] . '/';
   1.125 +
   1.126 +			if (!file_exists($phpbb_root_path . $dirs . $cur_dir))
   1.127 +			{
   1.128 +				// create the directory
   1.129 +				$result = $this->_mkdir($dir[$i]);
   1.130 +				$this->_chmod($dir[$i], $this->dir_perms);
   1.131 +			}
   1.132 +
   1.133 +			$this->_chdir($this->root_path . $dirs . $dir[$i]);
   1.134 +			$dirs .= $cur_dir;
   1.135 +		}
   1.136 +
   1.137 +		$this->_chdir($this->root_path);
   1.138 +
   1.139 +		/**
   1.140 +		* @todo stack result into array to make sure every path creation has been taken care of
   1.141 +		*/
   1.142 +		return $result;
   1.143 +	}
   1.144 +
   1.145 +	/**
   1.146 +	* Copy file from source location to destination location
   1.147 +	*/
   1.148 +	function copy_file($from_loc, $to_loc)
   1.149 +	{
   1.150 +		global $phpbb_root_path;
   1.151 +
   1.152 +		$from_loc = ((strpos($from_loc, $phpbb_root_path) !== 0) ? $phpbb_root_path : '') . $from_loc;
   1.153 +		$to_loc = $this->root_path . str_replace($phpbb_root_path, '', $to_loc);
   1.154 +
   1.155 +		if (!file_exists($from_loc))
   1.156 +		{
   1.157 +			return false;
   1.158 +		}
   1.159 +
   1.160 +		$result = $this->overwrite_file($from_loc, $to_loc);
   1.161 +
   1.162 +		return $result;
   1.163 +	}
   1.164 +
   1.165 +	/**
   1.166 +	* Remove file
   1.167 +	*/
   1.168 +	function delete_file($file)
   1.169 +	{
   1.170 +		global $phpbb_root_path;
   1.171 +
   1.172 +		$file = $this->root_path . str_replace($phpbb_root_path, '', $file);
   1.173 +
   1.174 +		return $this->_delete($file);
   1.175 +	}
   1.176 +
   1.177 +	/**
   1.178 +	* Remove directory
   1.179 +	* @todo remove child directories?
   1.180 +	*/
   1.181 +	function remove_dir($dir)
   1.182 +	{
   1.183 +		global $phpbb_root_path;
   1.184 +
   1.185 +		$dir = $this->root_path . str_replace($phpbb_root_path, '', $dir);
   1.186 +
   1.187 +		return $this->_rmdir($dir);
   1.188 +	}
   1.189 +
   1.190 +	/**
   1.191 +	* Rename a file or folder
   1.192 +	*/
   1.193 +	function rename($old_handle, $new_handle)
   1.194 +	{
   1.195 +		global $phpbb_root_path;
   1.196 +
   1.197 +		$old_handle = $this->root_path . str_replace($phpbb_root_path, '', $old_handle);
   1.198 +
   1.199 +		return $this->_rename($old_handle, $new_handle);
   1.200 +	}
   1.201 +
   1.202 +	/**
   1.203 +	* Check if a specified file exist...
   1.204 +	*/
   1.205 +	function file_exists($directory, $filename)
   1.206 +	{
   1.207 +		global $phpbb_root_path;
   1.208 +
   1.209 +		$directory = $this->root_path . str_replace($phpbb_root_path, '', $directory);
   1.210 +
   1.211 +		$this->_chdir($directory);
   1.212 +		$result = $this->_ls();
   1.213 +
   1.214 +		if ($result !== false && is_array($result))
   1.215 +		{
   1.216 +			return (in_array($filename, $result)) ? true : false;
   1.217 +		}
   1.218 +
   1.219 +		return false;
   1.220 +	}
   1.221 +
   1.222 +	/**
   1.223 +	* Open session
   1.224 +	*/
   1.225 +	function open_session()
   1.226 +	{
   1.227 +		return $this->_init();
   1.228 +	}
   1.229 +
   1.230 +	/**
   1.231 +	* Close current session
   1.232 +	*/
   1.233 +	function close_session()
   1.234 +	{
   1.235 +		return $this->_close();
   1.236 +	}
   1.237 +
   1.238 +	/**
   1.239 +	* Determine methods able to be used
   1.240 +	*/
   1.241 +	function methods()
   1.242 +	{
   1.243 +		$methods = array();
   1.244 +		$disabled_functions = explode(',', @ini_get('disable_functions'));
   1.245 +
   1.246 +		if (@extension_loaded('ftp'))
   1.247 +		{
   1.248 +			$methods[] = 'ftp';
   1.249 +		}
   1.250 +
   1.251 +		if (!in_array('fsockopen', $disabled_functions))
   1.252 +		{
   1.253 +			$methods[] = 'ftp_fsock';
   1.254 +		}
   1.255 +
   1.256 +		return $methods;
   1.257 +	}
   1.258 +}
   1.259 +
   1.260 +/**
   1.261 +* FTP transfer class
   1.262 +* @package phpBB3
   1.263 +*/
   1.264 +class ftp extends transfer
   1.265 +{
   1.266 +	/**
   1.267 +	* Standard parameters for FTP session
   1.268 +	*/
   1.269 +	function ftp($host, $username, $password, $root_path, $port = 21, $timeout = 10)
   1.270 +	{
   1.271 +		$this->host			= $host;
   1.272 +		$this->port			= $port;
   1.273 +		$this->username		= $username;
   1.274 +		$this->password		= $password;
   1.275 +		$this->timeout		= $timeout;
   1.276 +
   1.277 +		// Make sure $this->root_path is layed out the same way as the $user->page['root_script_path'] value (/ at the end)
   1.278 +		$this->root_path	= str_replace('\\', '/', $this->root_path);
   1.279 +
   1.280 +		if (!empty($root_path))
   1.281 +		{
   1.282 +			$this->root_path = (($root_path[0] != '/' ) ? '/' : '') . $root_path . ((substr($root_path, -1, 1) == '/') ? '' : '/');
   1.283 +		}
   1.284 +
   1.285 +		// Init some needed values
   1.286 +		transfer::transfer();
   1.287 +
   1.288 +		return;
   1.289 +	}
   1.290 +
   1.291 +	/**
   1.292 +	* Requests data
   1.293 +	*/
   1.294 +	function data()
   1.295 +	{
   1.296 +		global $user;
   1.297 +
   1.298 +		return array(
   1.299 +			'host'		=> 'localhost',
   1.300 +			'username'	=> 'anonymous',
   1.301 +			'password'	=> '',
   1.302 +			'root_path'	=> $user->page['root_script_path'],
   1.303 +			'port'		=> 21,
   1.304 +			'timeout'	=> 10
   1.305 +		);
   1.306 +	}
   1.307 +
   1.308 +	/**
   1.309 +	* Init FTP Session
   1.310 +	* @access private
   1.311 +	*/
   1.312 +	function _init()
   1.313 +	{
   1.314 +		// connect to the server
   1.315 +		$this->connection = @ftp_connect($this->host, $this->port, $this->timeout);
   1.316 +
   1.317 +		if (!$this->connection)
   1.318 +		{
   1.319 +			return 'ERR_CONNECTING_SERVER';
   1.320 +		}
   1.321 +
   1.322 +		// login to the server
   1.323 +		if (!@ftp_login($this->connection, $this->username, $this->password))
   1.324 +		{
   1.325 +			return 'ERR_UNABLE_TO_LOGIN';
   1.326 +		}
   1.327 +
   1.328 +		// attempt to turn pasv mode on
   1.329 +		@ftp_pasv($this->connection, true);
   1.330 +
   1.331 +		// change to the root directory
   1.332 +		if (!$this->_chdir($this->root_path))
   1.333 +		{
   1.334 +			return 'ERR_CHANGING_DIRECTORY';
   1.335 +		}
   1.336 +
   1.337 +		return true;
   1.338 +	}
   1.339 +
   1.340 +	/**
   1.341 +	* Create Directory (MKDIR)
   1.342 +	* @access private
   1.343 +	*/
   1.344 +	function _mkdir($dir)
   1.345 +	{
   1.346 +		return @ftp_mkdir($this->connection, $dir);
   1.347 +	}
   1.348 +
   1.349 +	/**
   1.350 +	* Remove directory (RMDIR)
   1.351 +	* @access private
   1.352 +	*/
   1.353 +	function _rmdir($dir)
   1.354 +	{
   1.355 +		return @ftp_rmdir($this->connection, $dir);
   1.356 +	}
   1.357 +
   1.358 +	/**
   1.359 +	* Rename file
   1.360 +	* @access private
   1.361 +	*/
   1.362 +	function _rename($old_handle, $new_handle)
   1.363 +	{
   1.364 +		return @ftp_rename($this->connection, $old_handle, $new_handle);
   1.365 +	}
   1.366 +
   1.367 +	/**
   1.368 +	* Change current working directory (CHDIR)
   1.369 +	* @access private
   1.370 +	*/
   1.371 +	function _chdir($dir = '')
   1.372 +	{
   1.373 +		if ($dir && $dir !== '/')
   1.374 +		{
   1.375 +			if (substr($dir, -1, 1) == '/')
   1.376 +			{
   1.377 +				$dir = substr($dir, 0, -1);
   1.378 +			}
   1.379 +		}
   1.380 +
   1.381 +		return @ftp_chdir($this->connection, $dir);
   1.382 +	}
   1.383 +
   1.384 +	/**
   1.385 +	* change file permissions (CHMOD)
   1.386 +	* @access private
   1.387 +	*/
   1.388 +	function _chmod($file, $perms)
   1.389 +	{
   1.390 +		if (function_exists('ftp_chmod'))
   1.391 +		{
   1.392 +			$err = @ftp_chmod($this->connection, $perms, $file);
   1.393 +		}
   1.394 +		else
   1.395 +		{
   1.396 +			// Unfortunatly CHMOD is not expecting an octal value...
   1.397 +			// We need to transform the integer (which was an octal) to an octal representation (to get the int) and then pass as is. ;)
   1.398 +			$chmod_cmd = 'CHMOD ' . base_convert($perms, 10, 8) . ' ' . $file;
   1.399 +			$err = $this->_site($chmod_cmd);
   1.400 +		}
   1.401 +
   1.402 +		return $err;
   1.403 +	}
   1.404 +
   1.405 +	/**
   1.406 +	* Upload file to location (PUT)
   1.407 +	* @access private
   1.408 +	*/
   1.409 +	function _put($from_file, $to_file)
   1.410 +	{
   1.411 +		// get the file extension
   1.412 +		$file_extension = strtolower(substr(strrchr($to_file, '.'), 1));
   1.413 +
   1.414 +		// We only use the BINARY file mode to cicumvent rewrite actions from ftp server (mostly linefeeds being replaced)
   1.415 +		$mode = FTP_BINARY;
   1.416 +
   1.417 +		$to_dir = dirname($to_file);
   1.418 +		$to_file = basename($to_file);
   1.419 +		$this->_chdir($to_dir);
   1.420 +
   1.421 +		$result = @ftp_put($this->connection, $to_file, $from_file, $mode);
   1.422 +		$this->_chdir($this->root_path);
   1.423 +
   1.424 +		return $result;
   1.425 +	}
   1.426 +
   1.427 +	/**
   1.428 +	* Delete file (DELETE)
   1.429 +	* @access private
   1.430 +	*/
   1.431 +	function _delete($file)
   1.432 +	{
   1.433 +		return @ftp_delete($this->connection, $file);
   1.434 +	}
   1.435 +
   1.436 +	/**
   1.437 +	* Close ftp session (CLOSE)
   1.438 +	* @access private
   1.439 +	*/
   1.440 +	function _close()
   1.441 +	{
   1.442 +		if (!$this->connection)
   1.443 +		{
   1.444 +			return false;
   1.445 +		}
   1.446 +
   1.447 +		return @ftp_quit($this->connection);
   1.448 +	}
   1.449 +
   1.450 +	/**
   1.451 +	* Return current working directory (CWD)
   1.452 +	* At the moment not used by parent class
   1.453 +	* @access private
   1.454 +	*/
   1.455 +	function _cwd()
   1.456 +	{
   1.457 +		return @ftp_pwd($this->connection);
   1.458 +	}
   1.459 +
   1.460 +	/**
   1.461 +	* Return list of files in a given directory (LS)
   1.462 +	* @access private
   1.463 +	*/
   1.464 +	function _ls($dir = './')
   1.465 +	{
   1.466 +		$list = @ftp_nlist($this->connection, $dir);
   1.467 +
   1.468 +		// See bug #46295 - Some FTP daemons don't like './'
   1.469 +		if ($dir === './')
   1.470 +		{
   1.471 +			// Let's try some alternatives
   1.472 +			$list = (empty($list)) ? @ftp_nlist($this->connection, '.') : $list;
   1.473 +			$list = (empty($list)) ? @ftp_nlist($this->connection, '') : $list;
   1.474 +		}
   1.475 +
   1.476 +		// Return on error
   1.477 +		if ($list === false)
   1.478 +		{
   1.479 +			return false;
   1.480 +		}
   1.481 +
   1.482 +		// Remove path if prepended
   1.483 +		foreach ($list as $key => $item)
   1.484 +		{
   1.485 +			// Use same separator for item and dir
   1.486 +			$item = str_replace('\\', '/', $item);
   1.487 +			$dir = str_replace('\\', '/', $dir);
   1.488 +
   1.489 +			if (!empty($dir) && strpos($item, $dir) === 0)
   1.490 +			{
   1.491 +				$item = substr($item, strlen($dir));
   1.492 +			}
   1.493 +
   1.494 +			$list[$key] = $item;
   1.495 +		}
   1.496 +
   1.497 +		return $list;
   1.498 +	}
   1.499 +
   1.500 +	/**
   1.501 +	* FTP SITE command (ftp-only function)
   1.502 +	* @access private
   1.503 +	*/
   1.504 +	function _site($command)
   1.505 +	{
   1.506 +		return @ftp_site($this->connection, $command);
   1.507 +	}
   1.508 +}
   1.509 +
   1.510 +/**
   1.511 +* FTP fsock transfer class
   1.512 +*
   1.513 +* @author wGEric
   1.514 +* @package phpBB3
   1.515 +*/
   1.516 +class ftp_fsock extends transfer
   1.517 +{
   1.518 +	var $data_connection;
   1.519 +
   1.520 +	/**
   1.521 +	* Standard parameters for FTP session
   1.522 +	*/
   1.523 +	function ftp_fsock($host, $username, $password, $root_path, $port = 21, $timeout = 10)
   1.524 +	{
   1.525 +		$this->host			= $host;
   1.526 +		$this->port			= $port;
   1.527 +		$this->username		= $username;
   1.528 +		$this->password		= $password;
   1.529 +		$this->timeout		= $timeout;
   1.530 +
   1.531 +		// Make sure $this->root_path is layed out the same way as the $user->page['root_script_path'] value (/ at the end)
   1.532 +		$this->root_path	= str_replace('\\', '/', $this->root_path);
   1.533 +
   1.534 +		if (!empty($root_path))
   1.535 +		{
   1.536 +			$this->root_path = (($root_path[0] != '/' ) ? '/' : '') . $root_path . ((substr($root_path, -1, 1) == '/') ? '' : '/');
   1.537 +		}
   1.538 +
   1.539 +		// Init some needed values
   1.540 +		transfer::transfer();
   1.541 +
   1.542 +		return;
   1.543 +	}
   1.544 +
   1.545 +	/**
   1.546 +	* Requests data
   1.547 +	*/
   1.548 +	function data()
   1.549 +	{
   1.550 +		global $user;
   1.551 +
   1.552 +		return array(
   1.553 +			'host'		=> 'localhost',
   1.554 +			'username'	=> 'anonymous',
   1.555 +			'password'	=> '',
   1.556 +			'root_path'	=> $user->page['root_script_path'],
   1.557 +			'port'		=> 21,
   1.558 +			'timeout'	=> 10
   1.559 +		);
   1.560 +	}
   1.561 +
   1.562 +	/**
   1.563 +	* Init FTP Session
   1.564 +	* @access private
   1.565 +	*/
   1.566 +	function _init()
   1.567 +	{
   1.568 +		$errno = 0;
   1.569 +		$errstr = '';
   1.570 +
   1.571 +		// connect to the server
   1.572 +		$this->connection = @fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout);
   1.573 +
   1.574 +		if (!$this->connection || !$this->_check_command())
   1.575 +		{
   1.576 +			return 'ERR_CONNECTING_SERVER';
   1.577 +		}
   1.578 +
   1.579 +		@stream_set_timeout($this->connection, $this->timeout);
   1.580 +
   1.581 +		// login
   1.582 +		if (!$this->_send_command('USER', $this->username))
   1.583 +		{
   1.584 +			return 'ERR_UNABLE_TO_LOGIN';
   1.585 +		}
   1.586 +
   1.587 +		if (!$this->_send_command('PASS', $this->password))
   1.588 +		{
   1.589 +			return 'ERR_UNABLE_TO_LOGIN';
   1.590 +		}
   1.591 +
   1.592 +		// change to the root directory
   1.593 +		if (!$this->_chdir($this->root_path))
   1.594 +		{
   1.595 +			return 'ERR_CHANGING_DIRECTORY';
   1.596 +		}
   1.597 +
   1.598 +		return true;
   1.599 +	}
   1.600 +
   1.601 +	/**
   1.602 +	* Create Directory (MKDIR)
   1.603 +	* @access private
   1.604 +	*/
   1.605 +	function _mkdir($dir)
   1.606 +	{
   1.607 +		return $this->_send_command('MKD', $dir);
   1.608 +	}
   1.609 +
   1.610 +	/**
   1.611 +	* Remove directory (RMDIR)
   1.612 +	* @access private
   1.613 +	*/
   1.614 +	function _rmdir($dir)
   1.615 +	{
   1.616 +		return $this->_send_command('RMD', $dir);
   1.617 +	}
   1.618 +
   1.619 +	/**
   1.620 +	* Rename File
   1.621 +	* @access private
   1.622 +	*/
   1.623 +	function _rename($old_handle, $new_handle)
   1.624 +	{
   1.625 +		$this->_send_command('RNFR', $old_handle);
   1.626 +		return $this->_send_command('RNTO', $new_handle);
   1.627 +	}
   1.628 +
   1.629 +	/**
   1.630 +	* Change current working directory (CHDIR)
   1.631 +	* @access private
   1.632 +	*/
   1.633 +	function _chdir($dir = '')
   1.634 +	{
   1.635 +		if ($dir && $dir !== '/')
   1.636 +		{
   1.637 +			if (substr($dir, -1, 1) == '/')
   1.638 +			{
   1.639 +				$dir = substr($dir, 0, -1);
   1.640 +			}
   1.641 +		}
   1.642 +
   1.643 +		return $this->_send_command('CWD', $dir);
   1.644 +	}
   1.645 +
   1.646 +	/**
   1.647 +	* change file permissions (CHMOD)
   1.648 +	* @access private
   1.649 +	*/
   1.650 +	function _chmod($file, $perms)
   1.651 +	{
   1.652 +		// Unfortunatly CHMOD is not expecting an octal value...
   1.653 +		// We need to transform the integer (which was an octal) to an octal representation (to get the int) and then pass as is. ;)
   1.654 +		return $this->_send_command('SITE CHMOD', base_convert($perms, 10, 8) . ' ' . $file);
   1.655 +	}
   1.656 +
   1.657 +	/**
   1.658 +	* Upload file to location (PUT)
   1.659 +	* @access private
   1.660 +	*/
   1.661 +	function _put($from_file, $to_file)
   1.662 +	{
   1.663 +		// We only use the BINARY file mode to cicumvent rewrite actions from ftp server (mostly linefeeds being replaced)
   1.664 +		// 'I' == BINARY
   1.665 +		// 'A' == ASCII
   1.666 +		if (!$this->_send_command('TYPE', 'I'))
   1.667 +		{
   1.668 +			return false;
   1.669 +		}
   1.670 +
   1.671 +		// open the connection to send file over
   1.672 +		if (!$this->_open_data_connection())
   1.673 +		{
   1.674 +			return false;
   1.675 +		}
   1.676 +
   1.677 +		$this->_send_command('STOR', $to_file, false);
   1.678 +
   1.679 +		// send the file
   1.680 +		$fp = @fopen($from_file, 'rb');
   1.681 +		while (!@feof($fp))
   1.682 +		{
   1.683 +			@fwrite($this->data_connection, @fread($fp, 4096));
   1.684 +		}
   1.685 +		@fclose($fp);
   1.686 +
   1.687 +		// close connection
   1.688 +		$this->_close_data_connection();
   1.689 +
   1.690 +		return $this->_check_command();
   1.691 +	}
   1.692 +
   1.693 +	/**
   1.694 +	* Delete file (DELETE)
   1.695 +	* @access private
   1.696 +	*/
   1.697 +	function _delete($file)
   1.698 +	{
   1.699 +		return $this->_send_command('DELE', $file);
   1.700 +	}
   1.701 +
   1.702 +	/**
   1.703 +	* Close ftp session (CLOSE)
   1.704 +	* @access private
   1.705 +	*/
   1.706 +	function _close()
   1.707 +	{
   1.708 +		if (!$this->connection)
   1.709 +		{
   1.710 +			return false;
   1.711 +		}
   1.712 +
   1.713 +		return $this->_send_command('QUIT');
   1.714 +	}
   1.715 +
   1.716 +	/**
   1.717 +	* Return current working directory (CWD)
   1.718 +	* At the moment not used by parent class
   1.719 +	* @access private
   1.720 +	*/
   1.721 +	function _cwd()
   1.722 +	{
   1.723 +		$this->_send_command('PWD', '', false);
   1.724 +		return preg_replace('#^[0-9]{3} "(.+)" .+\r\n#', '\\1', $this->_check_command(true));
   1.725 +	}
   1.726 +
   1.727 +	/**
   1.728 +	* Return list of files in a given directory (LS)
   1.729 +	* @access private
   1.730 +	*/
   1.731 +	function _ls($dir = './')
   1.732 +	{
   1.733 +		if (!$this->_open_data_connection())
   1.734 +		{
   1.735 +			return false;
   1.736 +		}
   1.737 +
   1.738 +		$this->_send_command('NLST', $dir);
   1.739 +
   1.740 +		$list = array();
   1.741 +		while (!@feof($this->data_connection))
   1.742 +		{
   1.743 +			$filename = preg_replace('#[\r\n]#', '', @fgets($this->data_connection, 512));
   1.744 +
   1.745 +			if ($filename !== '')
   1.746 +			{
   1.747 +				$list[] = $filename;
   1.748 +			}
   1.749 +		}
   1.750 +		$this->_close_data_connection();
   1.751 +
   1.752 +		// Clear buffer
   1.753 +		$this->_check_command();
   1.754 +
   1.755 +		// See bug #46295 - Some FTP daemons don't like './'
   1.756 +		if ($dir === './' && empty($list))
   1.757 +		{
   1.758 +			// Let's try some alternatives
   1.759 +			$list = $this->_ls('.');
   1.760 +
   1.761 +			if (empty($list))
   1.762 +			{
   1.763 +				$list = $this->_ls('');
   1.764 +			}
   1.765 +
   1.766 +			return $list;
   1.767 +		}
   1.768 +
   1.769 +		// Remove path if prepended
   1.770 +		foreach ($list as $key => $item)
   1.771 +		{
   1.772 +			// Use same separator for item and dir
   1.773 +			$item = str_replace('\\', '/', $item);
   1.774 +			$dir = str_replace('\\', '/', $dir);
   1.775 +
   1.776 +			if (!empty($dir) && strpos($item, $dir) === 0)
   1.777 +			{
   1.778 +				$item = substr($item, strlen($dir));
   1.779 +			}
   1.780 +
   1.781 +			$list[$key] = $item;
   1.782 +		}
   1.783 +
   1.784 +		return $list;
   1.785 +	}
   1.786 +
   1.787 +	/**
   1.788 +	* Send a command to server (FTP fsock only function)
   1.789 +	* @access private
   1.790 +	*/
   1.791 +	function _send_command($command, $args = '', $check = true)
   1.792 +	{
   1.793 +		if (!empty($args))
   1.794 +		{
   1.795 +			$command = "$command $args";
   1.796 +		}
   1.797 +
   1.798 +		fwrite($this->connection, $command . "\r\n");
   1.799 +
   1.800 +		if ($check === true && !$this->_check_command())
   1.801 +		{
   1.802 +			return false;
   1.803 +		}
   1.804 +
   1.805 +		return true;
   1.806 +	}
   1.807 +
   1.808 +	/**
   1.809 +	* Opens a connection to send data (FTP fosck only function)
   1.810 +	* @access private
   1.811 +	*/
   1.812 +	function _open_data_connection()
   1.813 +	{
   1.814 +		// Try to find out whether we have a IPv4 or IPv6 (control) connection
   1.815 +		if (function_exists('stream_socket_get_name'))
   1.816 +		{
   1.817 +			$socket_name = stream_socket_get_name($this->connection, true);
   1.818 +			$server_ip = substr($socket_name, 0, strrpos($socket_name, ':'));
   1.819 +		}
   1.820 +
   1.821 +		if (!isset($server_ip) || preg_match(get_preg_expression('ipv4'), $server_ip))
   1.822 +		{
   1.823 +			// Passive mode
   1.824 +			$this->_send_command('PASV', '', false);
   1.825 +
   1.826 +			if (!$ip_port = $this->_check_command(true))
   1.827 +			{
   1.828 +				return false;
   1.829 +			}
   1.830 +
   1.831 +			// open the connection to start sending the file
   1.832 +			if (!preg_match('#[0-9]{1,3},[0-9]{1,3},[0-9]{1,3},[0-9]{1,3},[0-9]+,[0-9]+#', $ip_port, $temp))
   1.833 +			{
   1.834 +				// bad ip and port
   1.835 +				return false;
   1.836 +			}
   1.837 +
   1.838 +			$temp = explode(',', $temp[0]);
   1.839 +			$server_ip = $temp[0] . '.' . $temp[1] . '.' . $temp[2] . '.' . $temp[3];
   1.840 +			$server_port = $temp[4] * 256 + $temp[5];
   1.841 +		}
   1.842 +		else
   1.843 +		{
   1.844 +			// Extended Passive Mode - RFC2428
   1.845 +			$this->_send_command('EPSV', '', false);
   1.846 +
   1.847 +			if (!$epsv_response = $this->_check_command(true))
   1.848 +			{
   1.849 +				return false;
   1.850 +			}
   1.851 +
   1.852 +			// Response looks like "229 Entering Extended Passive Mode (|||12345|)"
   1.853 +			// where 12345 is the tcp port for the data connection
   1.854 +			if (!preg_match('#\(\|\|\|([0-9]+)\|\)#', $epsv_response, $match))
   1.855 +			{
   1.856 +				return false;
   1.857 +			}
   1.858 +			$server_port = (int) $match[1];
   1.859 +
   1.860 +			// fsockopen expects IPv6 address in square brackets
   1.861 +			$server_ip = "[$server_ip]";
   1.862 +		}
   1.863 +
   1.864 +		$errno = 0;
   1.865 +		$errstr = '';
   1.866 +
   1.867 +		if (!$this->data_connection = @fsockopen($server_ip, $server_port, $errno, $errstr, $this->timeout))
   1.868 +		{
   1.869 +			return false;
   1.870 +		}
   1.871 +		@stream_set_timeout($this->data_connection, $this->timeout);
   1.872 +
   1.873 +		return true;
   1.874 +	}
   1.875 +
   1.876 +	/**
   1.877 +	* Closes a connection used to send data
   1.878 +	* @access private
   1.879 +	*/
   1.880 +	function _close_data_connection()
   1.881 +	{
   1.882 +		return @fclose($this->data_connection);
   1.883 +	}
   1.884 +
   1.885 +	/**
   1.886 +	* Check to make sure command was successful (FTP fsock only function)
   1.887 +	* @access private
   1.888 +	*/
   1.889 +	function _check_command($return = false)
   1.890 +	{
   1.891 +		$response = '';
   1.892 +
   1.893 +		do
   1.894 +		{
   1.895 +			$result = @fgets($this->connection, 512);
   1.896 +			$response .= $result;
   1.897 +		}
   1.898 +		while (substr($result, 3, 1) !== ' ');
   1.899 +
   1.900 +		if (!preg_match('#^[123]#', $response))
   1.901 +		{
   1.902 +			return false;
   1.903 +		}
   1.904 +
   1.905 +		return ($return) ? $response : true;
   1.906 +	}
   1.907 +}
   1.908 +
   1.909 +?>
   1.910 \ No newline at end of file