Blame Identity/Models/Html/phpBB/3.0.4/includes/functions_transfer.php

d6e8d8
d6e8d8
/**
d6e8d8
*
d6e8d8
* @package phpBB3
d6e8d8
* @version $Id: functions_transfer.php 8479 2008-03-29 00:22:48Z naderman $
d6e8d8
* @copyright (c) 2005 phpBB Group
d6e8d8
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
d6e8d8
*
d6e8d8
*/
d6e8d8
d6e8d8
/**
d6e8d8
* @ignore
d6e8d8
*/
d6e8d8
if (!defined('IN_PHPBB'))
d6e8d8
{
d6e8d8
	exit;
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* Transfer class, wrapper for ftp/sftp/ssh
d6e8d8
* @package phpBB3
d6e8d8
*/
d6e8d8
class transfer
d6e8d8
{
d6e8d8
	var $connection;
d6e8d8
	var $host;
d6e8d8
	var $port;
d6e8d8
	var $username;
d6e8d8
	var $password;
d6e8d8
	var $timeout;
d6e8d8
	var $root_path;
d6e8d8
	var $tmp_path;
d6e8d8
	var $file_perms;
d6e8d8
	var $dir_perms;
d6e8d8
d6e8d8
	/**
d6e8d8
	* Constructor - init some basic values
d6e8d8
	*/
d6e8d8
	function transfer()
d6e8d8
	{
d6e8d8
		global $phpbb_root_path;
d6e8d8
d6e8d8
		$this->file_perms	= 0644;
d6e8d8
		$this->dir_perms	= 0777;
d6e8d8
d6e8d8
		// We use the store directory as temporary path to circumvent open basedir restrictions
d6e8d8
		$this->tmp_path = $phpbb_root_path . 'store/';
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Write file to location
d6e8d8
	*/
d6e8d8
	function write_file($destination_file = '', $contents = '')
d6e8d8
	{
d6e8d8
		global $phpbb_root_path;
d6e8d8
d6e8d8
		$destination_file = $this->root_path . str_replace($phpbb_root_path, '', $destination_file);
d6e8d8
d6e8d8
		// need to create a temp file and then move that temp file.
d6e8d8
		// ftp functions can only move files around and can't create.
d6e8d8
		// This means that the users will need to have access to write
d6e8d8
		// temporary files or have write access on a folder within phpBB
d6e8d8
		// like the cache folder. If the user can't do either, then
d6e8d8
		// he/she needs to use the fsock ftp method
d6e8d8
		$temp_name = tempnam($this->tmp_path, 'transfer_');
d6e8d8
		@unlink($temp_name);
d6e8d8
d6e8d8
		$fp = @fopen($temp_name, 'w');
d6e8d8
d6e8d8
		if (!$fp)
d6e8d8
		{
d6e8d8
			trigger_error('Unable to create temporary file ' . $temp_name, E_USER_ERROR);
d6e8d8
		}
d6e8d8
d6e8d8
		@fwrite($fp, $contents);
d6e8d8
		@fclose($fp);
d6e8d8
d6e8d8
		$result = $this->overwrite_file($temp_name, $destination_file);
d6e8d8
d6e8d8
		// remove temporary file now
d6e8d8
		@unlink($temp_name);
d6e8d8
d6e8d8
		return $result;
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Moving file into location. If the destination file already exists it gets overwritten
d6e8d8
	*/
d6e8d8
	function overwrite_file($source_file, $destination_file)
d6e8d8
	{
d6e8d8
		/**
d6e8d8
		* @todo generally think about overwriting files in another way, by creating a temporary file and then renaming it
d6e8d8
		* @todo check for the destination file existance too
d6e8d8
		*/
d6e8d8
		$this->_delete($destination_file);
d6e8d8
		$result = $this->_put($source_file, $destination_file);
d6e8d8
		$this->_chmod($destination_file, $this->file_perms);
d6e8d8
d6e8d8
		return $result;
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Create directory structure
d6e8d8
	*/
d6e8d8
	function make_dir($dir)
d6e8d8
	{
d6e8d8
		global $phpbb_root_path;
d6e8d8
d6e8d8
		$dir = str_replace($phpbb_root_path, '', $dir);
d6e8d8
		$dir = explode('/', $dir);
d6e8d8
		$dirs = '';
d6e8d8
d6e8d8
		for ($i = 0, $total = sizeof($dir); $i < $total; $i++)
d6e8d8
		{
d6e8d8
			$result = true;
d6e8d8
d6e8d8
			if (strpos($dir[$i], '.') === 0)
d6e8d8
			{
d6e8d8
				continue;
d6e8d8
			}
d6e8d8
			$cur_dir = $dir[$i] . '/';
d6e8d8
d6e8d8
			if (!file_exists($phpbb_root_path . $dirs . $cur_dir))
d6e8d8
			{
d6e8d8
				// create the directory
d6e8d8
				$result = $this->_mkdir($dir[$i]);
d6e8d8
				$this->_chmod($dir[$i], $this->dir_perms);
d6e8d8
			}
d6e8d8
d6e8d8
			$this->_chdir($this->root_path . $dirs . $dir[$i]);
d6e8d8
			$dirs .= $cur_dir;
d6e8d8
		}
d6e8d8
d6e8d8
		$this->_chdir($this->root_path);
d6e8d8
d6e8d8
		/**
d6e8d8
		* @todo stack result into array to make sure every path creation has been taken care of
d6e8d8
		*/
d6e8d8
		return $result;
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Copy file from source location to destination location
d6e8d8
	*/
d6e8d8
	function copy_file($from_loc, $to_loc)
d6e8d8
	{
d6e8d8
		global $phpbb_root_path;
d6e8d8
d6e8d8
		$from_loc = ((strpos($from_loc, $phpbb_root_path) !== 0) ? $phpbb_root_path : '') . $from_loc;
d6e8d8
		$to_loc = $this->root_path . str_replace($phpbb_root_path, '', $to_loc);
d6e8d8
d6e8d8
		if (!file_exists($from_loc))
d6e8d8
		{
d6e8d8
			return false;
d6e8d8
		}
d6e8d8
d6e8d8
		$result = $this->overwrite_file($from_loc, $to_loc);
d6e8d8
d6e8d8
		return $result;
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Remove file
d6e8d8
	*/
d6e8d8
	function delete_file($file)
d6e8d8
	{
d6e8d8
		global $phpbb_root_path;
d6e8d8
d6e8d8
		$file = $this->root_path . str_replace($phpbb_root_path, '', $file);
d6e8d8
d6e8d8
		return $this->_delete($file);
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Remove directory
d6e8d8
	* @todo remove child directories?
d6e8d8
	*/
d6e8d8
	function remove_dir($dir)
d6e8d8
	{
d6e8d8
		global $phpbb_root_path;
d6e8d8
d6e8d8
		$dir = $this->root_path . str_replace($phpbb_root_path, '', $dir);
d6e8d8
d6e8d8
		return $this->_rmdir($dir);
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Rename a file or folder
d6e8d8
	*/
d6e8d8
	function rename($old_handle, $new_handle)
d6e8d8
	{
d6e8d8
		global $phpbb_root_path;
d6e8d8
d6e8d8
		$old_handle = $this->root_path . str_replace($phpbb_root_path, '', $old_handle);
d6e8d8
d6e8d8
		return $this->_rename($old_handle, $new_handle);
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Check if a specified file exist...
d6e8d8
	*/
d6e8d8
	function file_exists($directory, $filename)
d6e8d8
	{
d6e8d8
		global $phpbb_root_path;
d6e8d8
d6e8d8
		$directory = $this->root_path . str_replace($phpbb_root_path, '', $directory);
d6e8d8
d6e8d8
		$this->_chdir($directory);
d6e8d8
		$result = $this->_ls('');
d6e8d8
d6e8d8
		if ($result !== false && is_array($result))
d6e8d8
		{
d6e8d8
			return (in_array($filename, $result)) ? true : false;
d6e8d8
		}
d6e8d8
d6e8d8
		return false;
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Open session
d6e8d8
	*/
d6e8d8
	function open_session()
d6e8d8
	{
d6e8d8
		return $this->_init();
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Close current session
d6e8d8
	*/
d6e8d8
	function close_session()
d6e8d8
	{
d6e8d8
		return $this->_close();
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Determine methods able to be used
d6e8d8
	*/
d6e8d8
	function methods()
d6e8d8
	{
d6e8d8
		$methods = array();
d6e8d8
		$disabled_functions = explode(',', @ini_get('disable_functions'));
d6e8d8
d6e8d8
		if (@extension_loaded('ftp'))
d6e8d8
		{
d6e8d8
			$methods[] = 'ftp';
d6e8d8
		}
d6e8d8
d6e8d8
		if (!in_array('fsockopen', $disabled_functions))
d6e8d8
		{
d6e8d8
			$methods[] = 'ftp_fsock';
d6e8d8
		}
d6e8d8
d6e8d8
		return $methods;
d6e8d8
	}
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* FTP transfer class
d6e8d8
* @package phpBB3
d6e8d8
*/
d6e8d8
class ftp extends transfer
d6e8d8
{
d6e8d8
	/**
d6e8d8
	* Standard parameters for FTP session
d6e8d8
	*/
d6e8d8
	function ftp($host, $username, $password, $root_path, $port = 21, $timeout = 10)
d6e8d8
	{
d6e8d8
		$this->host			= $host;
d6e8d8
		$this->port			= $port;
d6e8d8
		$this->username		= $username;
d6e8d8
		$this->password		= $password;
d6e8d8
		$this->timeout		= $timeout;
d6e8d8
d6e8d8
		// Make sure $this->root_path is layed out the same way as the $user->page['root_script_path'] value (/ at the end)
d6e8d8
		$this->root_path	= str_replace('\\', '/', $this->root_path);
d6e8d8
d6e8d8
		if (!empty($root_path))
d6e8d8
		{
d6e8d8
			$this->root_path = (($root_path[0] != '/' ) ? '/' : '') . $root_path . ((substr($root_path, -1, 1) == '/') ? '' : '/');
d6e8d8
		}
d6e8d8
d6e8d8
		// Init some needed values
d6e8d8
		transfer::transfer();
d6e8d8
d6e8d8
		return;
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Requests data
d6e8d8
	*/
d6e8d8
	function data()
d6e8d8
	{
d6e8d8
		global $user;
d6e8d8
d6e8d8
		return array(
d6e8d8
			'host'		=> 'localhost',
d6e8d8
			'username'	=> 'anonymous',
d6e8d8
			'password'	=> '',
d6e8d8
			'root_path'	=> $user->page['root_script_path'],
d6e8d8
			'port'		=> 21,
d6e8d8
			'timeout'	=> 10
d6e8d8
		);
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Init FTP Session
d6e8d8
	* @access private
d6e8d8
	*/
d6e8d8
	function _init()
d6e8d8
	{
d6e8d8
		// connect to the server
d6e8d8
		$this->connection = @ftp_connect($this->host, $this->port, $this->timeout);
d6e8d8
d6e8d8
		if (!$this->connection)
d6e8d8
		{
d6e8d8
			return 'ERR_CONNECTING_SERVER';
d6e8d8
		}
d6e8d8
d6e8d8
		// attempt to turn pasv mode on
d6e8d8
		@ftp_pasv($this->connection, true);
d6e8d8
d6e8d8
		// login to the server
d6e8d8
		if (!@ftp_login($this->connection, $this->username, $this->password))
d6e8d8
		{
d6e8d8
			return 'ERR_UNABLE_TO_LOGIN';
d6e8d8
		}
d6e8d8
d6e8d8
		// change to the root directory
d6e8d8
		if (!$this->_chdir($this->root_path))
d6e8d8
		{
d6e8d8
			return 'ERR_CHANGING_DIRECTORY';
d6e8d8
		}
d6e8d8
d6e8d8
		return true;
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Create Directory (MKDIR)
d6e8d8
	* @access private
d6e8d8
	*/
d6e8d8
	function _mkdir($dir)
d6e8d8
	{
d6e8d8
		return @ftp_mkdir($this->connection, $dir);
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Remove directory (RMDIR)
d6e8d8
	* @access private
d6e8d8
	*/
d6e8d8
	function _rmdir($dir)
d6e8d8
	{
d6e8d8
		return @ftp_rmdir($this->connection, $dir);
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Rename file
d6e8d8
	* @access private
d6e8d8
	*/
d6e8d8
	function _rename($old_handle, $new_handle)
d6e8d8
	{
d6e8d8
		return @ftp_rename($this->connection, $old_handle, $new_handle);
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Change current working directory (CHDIR)
d6e8d8
	* @access private
d6e8d8
	*/
d6e8d8
	function _chdir($dir = '')
d6e8d8
	{
d6e8d8
		if ($dir && $dir !== '/')
d6e8d8
		{
d6e8d8
			if (substr($dir, -1, 1) == '/')
d6e8d8
			{
d6e8d8
				$dir = substr($dir, 0, -1);
d6e8d8
			}
d6e8d8
		}
d6e8d8
d6e8d8
		return @ftp_chdir($this->connection, $dir);
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* change file permissions (CHMOD)
d6e8d8
	* @access private
d6e8d8
	*/
d6e8d8
	function _chmod($file, $perms)
d6e8d8
	{
d6e8d8
		if (function_exists('ftp_chmod'))
d6e8d8
		{
d6e8d8
			$err = @ftp_chmod($this->connection, $perms, $file);
d6e8d8
		}
d6e8d8
		else
d6e8d8
		{
d6e8d8
			// Unfortunatly CHMOD is not expecting an octal value...
d6e8d8
			// We need to transform the integer (which was an octal) to an octal representation (to get the int) and then pass as is. ;)
d6e8d8
			$chmod_cmd = 'CHMOD ' . base_convert($perms, 10, 8) . ' ' . $file;
d6e8d8
			$err = $this->_site($chmod_cmd);
d6e8d8
		}
d6e8d8
d6e8d8
		return $err;
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Upload file to location (PUT)
d6e8d8
	* @access private
d6e8d8
	*/
d6e8d8
	function _put($from_file, $to_file)
d6e8d8
	{
d6e8d8
		// get the file extension
d6e8d8
		$file_extension = strtolower(substr(strrchr($to_file, '.'), 1));
d6e8d8
d6e8d8
		// We only use the BINARY file mode to cicumvent rewrite actions from ftp server (mostly linefeeds being replaced)
d6e8d8
		$mode = FTP_BINARY;
d6e8d8
d6e8d8
		$to_dir = dirname($to_file);
d6e8d8
		$to_file = basename($to_file);
d6e8d8
		$this->_chdir($to_dir);
d6e8d8
d6e8d8
		$result = @ftp_put($this->connection, $to_file, $from_file, $mode);
d6e8d8
		$this->_chdir($this->root_path);
d6e8d8
d6e8d8
		return $result;
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Delete file (DELETE)
d6e8d8
	* @access private
d6e8d8
	*/
d6e8d8
	function _delete($file)
d6e8d8
	{
d6e8d8
		return @ftp_delete($this->connection, $file);
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Close ftp session (CLOSE)
d6e8d8
	* @access private
d6e8d8
	*/
d6e8d8
	function _close()
d6e8d8
	{
d6e8d8
		if (!$this->connection)
d6e8d8
		{
d6e8d8
			return false;
d6e8d8
		}
d6e8d8
d6e8d8
		return @ftp_quit($this->connection);
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Return current working directory (CWD)
d6e8d8
	* At the moment not used by parent class
d6e8d8
	* @access private
d6e8d8
	*/
d6e8d8
	function _cwd()
d6e8d8
	{
d6e8d8
		return @ftp_pwd($this->connection);
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Return list of files in a given directory (LS)
d6e8d8
	* @access private
d6e8d8
	*/
d6e8d8
	function _ls($dir = './')
d6e8d8
	{
d6e8d8
		return @ftp_nlist($this->connection, $dir);
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* FTP SITE command (ftp-only function)
d6e8d8
	* @access private
d6e8d8
	*/
d6e8d8
	function _site($command)
d6e8d8
	{
d6e8d8
		return @ftp_site($this->connection, $command);
d6e8d8
	}
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* FTP fsock transfer class
d6e8d8
*
d6e8d8
* @author wGEric
d6e8d8
* @package phpBB3
d6e8d8
*/
d6e8d8
class ftp_fsock extends transfer
d6e8d8
{
d6e8d8
	var $data_connection;
d6e8d8
d6e8d8
	/**
d6e8d8
	* Standard parameters for FTP session
d6e8d8
	*/
d6e8d8
	function ftp_fsock($host, $username, $password, $root_path, $port = 21, $timeout = 10)
d6e8d8
	{
d6e8d8
		$this->host			= $host;
d6e8d8
		$this->port			= $port;
d6e8d8
		$this->username		= $username;
d6e8d8
		$this->password		= $password;
d6e8d8
		$this->timeout		= $timeout;
d6e8d8
d6e8d8
		// Make sure $this->root_path is layed out the same way as the $user->page['root_script_path'] value (/ at the end)
d6e8d8
		$this->root_path	= str_replace('\\', '/', $this->root_path);
d6e8d8
d6e8d8
		if (!empty($root_path))
d6e8d8
		{
d6e8d8
			$this->root_path = (($root_path[0] != '/' ) ? '/' : '') . $root_path . ((substr($root_path, -1, 1) == '/') ? '' : '/');
d6e8d8
		}
d6e8d8
d6e8d8
		// Init some needed values
d6e8d8
		transfer::transfer();
d6e8d8
d6e8d8
		return;
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Requests data
d6e8d8
	*/
d6e8d8
	function data()
d6e8d8
	{
d6e8d8
		global $user;
d6e8d8
d6e8d8
		return array(
d6e8d8
			'host'		=> 'localhost',
d6e8d8
			'username'	=> 'anonymous',
d6e8d8
			'password'	=> '',
d6e8d8
			'root_path'	=> $user->page['root_script_path'],
d6e8d8
			'port'		=> 21,
d6e8d8
			'timeout'	=> 10
d6e8d8
		);
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Init FTP Session
d6e8d8
	* @access private
d6e8d8
	*/
d6e8d8
	function _init()
d6e8d8
	{
d6e8d8
		$errno = 0;
d6e8d8
		$errstr = '';
d6e8d8
d6e8d8
		// connect to the server
d6e8d8
		$this->connection = @fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout);
d6e8d8
d6e8d8
		if (!$this->connection || !$this->_check_command())
d6e8d8
		{
d6e8d8
			return 'ERR_CONNECTING_SERVER';
d6e8d8
		}
d6e8d8
d6e8d8
		@stream_set_timeout($this->connection, $this->timeout);
d6e8d8
d6e8d8
		// login
d6e8d8
		if (!$this->_send_command('USER', $this->username))
d6e8d8
		{
d6e8d8
			return 'ERR_UNABLE_TO_LOGIN';
d6e8d8
		}
d6e8d8
d6e8d8
		if (!$this->_send_command('PASS', $this->password))
d6e8d8
		{
d6e8d8
			return 'ERR_UNABLE_TO_LOGIN';
d6e8d8
		}
d6e8d8
d6e8d8
		// change to the root directory
d6e8d8
		if (!$this->_chdir($this->root_path))
d6e8d8
		{
d6e8d8
			return 'ERR_CHANGING_DIRECTORY';
d6e8d8
		}
d6e8d8
d6e8d8
		return true;
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Create Directory (MKDIR)
d6e8d8
	* @access private
d6e8d8
	*/
d6e8d8
	function _mkdir($dir)
d6e8d8
	{
d6e8d8
		return $this->_send_command('MKD', $dir);
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Remove directory (RMDIR)
d6e8d8
	* @access private
d6e8d8
	*/
d6e8d8
	function _rmdir($dir)
d6e8d8
	{
d6e8d8
		return $this->_send_command('RMD', $dir);
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Rename File
d6e8d8
	* @access private
d6e8d8
	*/
d6e8d8
	function _rename($old_handle, $new_handle)
d6e8d8
	{
d6e8d8
		$this->_send_command('RNFR', $old_handle);
d6e8d8
		return $this->_send_command('RNTO', $new_handle);
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Change current working directory (CHDIR)
d6e8d8
	* @access private
d6e8d8
	*/
d6e8d8
	function _chdir($dir = '')
d6e8d8
	{
d6e8d8
		if ($dir && $dir !== '/')
d6e8d8
		{
d6e8d8
			if (substr($dir, -1, 1) == '/')
d6e8d8
			{
d6e8d8
				$dir = substr($dir, 0, -1);
d6e8d8
			}
d6e8d8
		}
d6e8d8
d6e8d8
		return $this->_send_command('CWD', $dir);
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* change file permissions (CHMOD)
d6e8d8
	* @access private
d6e8d8
	*/
d6e8d8
	function _chmod($file, $perms)
d6e8d8
	{
d6e8d8
		// Unfortunatly CHMOD is not expecting an octal value...
d6e8d8
		// We need to transform the integer (which was an octal) to an octal representation (to get the int) and then pass as is. ;)
d6e8d8
		return $this->_send_command('SITE CHMOD', base_convert($perms, 10, 8) . ' ' . $file);
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Upload file to location (PUT)
d6e8d8
	* @access private
d6e8d8
	*/
d6e8d8
	function _put($from_file, $to_file)
d6e8d8
	{
d6e8d8
		// We only use the BINARY file mode to cicumvent rewrite actions from ftp server (mostly linefeeds being replaced)
d6e8d8
		// 'I' == BINARY
d6e8d8
		// 'A' == ASCII
d6e8d8
		if (!$this->_send_command('TYPE', 'I'))
d6e8d8
		{
d6e8d8
			return false;
d6e8d8
		}
d6e8d8
d6e8d8
		// open the connection to send file over
d6e8d8
		if (!$this->_open_data_connection())
d6e8d8
		{
d6e8d8
			return false;
d6e8d8
		}
d6e8d8
d6e8d8
		$this->_send_command('STOR', $to_file, false);
d6e8d8
d6e8d8
		// send the file
d6e8d8
		$fp = @fopen($from_file, 'rb');
d6e8d8
		while (!@feof($fp))
d6e8d8
		{
d6e8d8
			@fwrite($this->data_connection, @fread($fp, 4096));
d6e8d8
		}
d6e8d8
		@fclose($fp);
d6e8d8
d6e8d8
		// close connection
d6e8d8
		$this->_close_data_connection();
d6e8d8
d6e8d8
		return $this->_check_command();
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Delete file (DELETE)
d6e8d8
	* @access private
d6e8d8
	*/
d6e8d8
	function _delete($file)
d6e8d8
	{
d6e8d8
		return $this->_send_command('DELE', $file);
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Close ftp session (CLOSE)
d6e8d8
	* @access private
d6e8d8
	*/
d6e8d8
	function _close()
d6e8d8
	{
d6e8d8
		if (!$this->connection)
d6e8d8
		{
d6e8d8
			return false;
d6e8d8
		}
d6e8d8
d6e8d8
		return $this->_send_command('QUIT');
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Return current working directory (CWD)
d6e8d8
	* At the moment not used by parent class
d6e8d8
	* @access private
d6e8d8
	*/
d6e8d8
	function _cwd()
d6e8d8
	{
d6e8d8
		$this->_send_command('PWD', '', false);
d6e8d8
		return preg_replace('#^[0-9]{3} "(.+)" .+\r\n#', '\\1', $this->_check_command(true));
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Return list of files in a given directory (LS)
d6e8d8
	* @access private
d6e8d8
	*/
d6e8d8
	function _ls($dir = './')
d6e8d8
	{
d6e8d8
		if (!$this->_open_data_connection())
d6e8d8
		{
d6e8d8
			return false;
d6e8d8
		}
d6e8d8
d6e8d8
		$this->_send_command('NLST', $dir);
d6e8d8
d6e8d8
		$list = array();
d6e8d8
		while (!@feof($this->data_connection))
d6e8d8
		{
d6e8d8
			$list[] = preg_replace('#[\r\n]#', '', @fgets($this->data_connection, 512));
d6e8d8
		}
d6e8d8
		$this->_close_data_connection();
d6e8d8
d6e8d8
		return $list;
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Send a command to server (FTP fsock only function)
d6e8d8
	* @access private
d6e8d8
	*/
d6e8d8
	function _send_command($command, $args = '', $check = true)
d6e8d8
	{
d6e8d8
		if (!empty($args))
d6e8d8
		{
d6e8d8
			$command = "$command $args";
d6e8d8
		}
d6e8d8
d6e8d8
		fwrite($this->connection, $command . "\r\n");
d6e8d8
d6e8d8
		if ($check === true && !$this->_check_command())
d6e8d8
		{
d6e8d8
			return false;
d6e8d8
		}
d6e8d8
d6e8d8
		return true;
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Opens a connection to send data (FTP fosck only function)
d6e8d8
	* @access private
d6e8d8
	*/
d6e8d8
	function _open_data_connection()
d6e8d8
	{
d6e8d8
		$this->_send_command('PASV', '', false);
d6e8d8
d6e8d8
		if (!$ip_port = $this->_check_command(true))
d6e8d8
		{
d6e8d8
			return false;
d6e8d8
		}
d6e8d8
d6e8d8
		// open the connection to start sending the file
d6e8d8
		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))
d6e8d8
		{
d6e8d8
			// bad ip and port
d6e8d8
			return false;
d6e8d8
		}
d6e8d8
d6e8d8
		$temp = explode(',', $temp[0]);
d6e8d8
		$server_ip = $temp[0] . '.' . $temp[1] . '.' . $temp[2] . '.' . $temp[3];
d6e8d8
		$server_port = $temp[4] * 256 + $temp[5];
d6e8d8
		$errno = 0;
d6e8d8
		$errstr = '';
d6e8d8
d6e8d8
		if (!$this->data_connection = @fsockopen($server_ip, $server_port, $errno, $errstr, $this->timeout))
d6e8d8
		{
d6e8d8
			return false;
d6e8d8
		}
d6e8d8
		@stream_set_timeout($this->data_connection, $this->timeout);
d6e8d8
d6e8d8
		return true;
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Closes a connection used to send data
d6e8d8
	* @access private
d6e8d8
	*/
d6e8d8
	function _close_data_connection()
d6e8d8
	{
d6e8d8
		return @fclose($this->data_connection);
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Check to make sure command was successful (FTP fsock only function)
d6e8d8
	* @access private
d6e8d8
	*/
d6e8d8
	function _check_command($return = false)
d6e8d8
	{
d6e8d8
		$response = '';
d6e8d8
d6e8d8
		do
d6e8d8
		{
d6e8d8
			$result = @fgets($this->connection, 512);
d6e8d8
			$response .= $result;
d6e8d8
		}
d6e8d8
		while (substr($response, 3, 1) != ' ');
d6e8d8
d6e8d8
		if (!preg_match('#^[123]#', $response))
d6e8d8
		{
d6e8d8
			return false;
d6e8d8
		}
d6e8d8
d6e8d8
		return ($return) ? $response : true;
d6e8d8
	}
d6e8d8
}
d6e8d8
d6e8d8
?>