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