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