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