Source code for file /joomla/client/ftp.php
Documentation is available at ftp.php
* @version $Id: ftp.php 9764 2007-12-30 07:48:11Z ircmaxell $
* @package Joomla.Framework
* @copyright Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
* @license GNU/GPL, see LICENSE.php
* Joomla! is free software and parts of it may contain or be derived from the
* GNU General Public License or other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
// Check to ensure this file is within the rest of the framework
* - 30 : Unable to connect to host
* - 32 : Unable to send command to server
* - 36 : Passive mode failed
* - 37 : Data transfer error
* - 38 : Local filesystem error
// Is FTP extension loaded? If not try to load it
define('FTP_NATIVE', (function_exists('ftp_connect'))?
1 :
0);
* @author Louis Landry <louis.landry@joomla.org>
* @package Joomla.Framework
* Server connection resource
* Data port connection resource
* Passive connection information
* Array to hold ascii format file extensions
var $_autoAscii =
array ("asp", "bat", "c", "cpp", "csv", "h", "htm", "html", "shtml", "ini", "inc", "log", "php", "php3", "pl", "perl", "sh", "sql", "txt", "xhtml", "xml");
* Array to hold native line ending characters
var $_lineEndings =
array ('UNIX' =>
"\n", 'MAC' =>
"\r", 'WIN' =>
"\r\n");
* JFTP object constructor
* @param array $options Associative array of options to set
// If default transfer type is no set, set it to autoascii detect
if (!isset
($options['type'])) {
// Import the generic buffer stream handler
jimport('joomla.utilities.buffer');
// Autoloading fails for JBuffer as the class is used as a stream handler
// Register faked "destructor" in PHP4 to close all connections we might have made
* Closes an existing connection, if we have one
* Returns a reference to the global FTP connector object, only creating it
* if it doesn't already exist.
* This method must be invoked as:
* <pre> $ftp = &JFTP::getInstance($host);</pre>
* You may optionally specify a username and password in the parameters. If you do so,
* you may not login() again with different credentials using the same object.
* If you do not use this option, you must quit() the current connection when you
* are done, to free it for use by others.
* @param string $host Host to connect to
* @param string $port Port to connect to
* @param array $options Array with any of these options: type=>[FTP_AUTOASCII|FTP_ASCII|FTP_BINARY], timeout=>(int)
* @param string $user Username to use for a connection
* @param string $pass Password to use for a connection
* @return JFTP The FTP Client object.
function &getInstance($host =
'127.0.0.1', $port =
'21', $options =
null, $user =
null, $pass =
null)
static $instances =
array();
$signature =
$user.
':'.
$pass.
'@'.
$host.
":".
$port;
// Create a new instance, or set the options of an existing one
if (!isset
($instances[$signature]) ||
!is_object($instances[$signature])) {
$instances[$signature] =
new JFTP($options);
$instances[$signature]->setOptions($options);
// Connect to the server, and login, if requested
$return =
$instances[$signature]->connect($host, $port);
if ($return &&
$user !==
null &&
$pass !==
null) {
$instances[$signature]->login($user, $pass);
return $instances[$signature];
* @param array $options Associative array of options to set
* @return boolean True if successful
if (isset
($options['type'])) {
$this->_type =
$options['type'];
if (isset
($options['timeout'])) {
$this->_timeout =
$options['timeout'];
* Method to connect to a FTP server
* @param string $host Host to connect to [Default: 127.0.0.1]
* @param string $port Port to connect on [Default: port 21]
* @return boolean True if successful
function connect($host =
'127.0.0.1', $port =
21) {
// If already connected, return
// If native FTP support is enabled lets use it...
$this->_conn =
@ftp_connect($host, $port, $this->_timeout);
if ($this->_conn ===
false) {
JError::raiseWarning('30', 'JFTP::connect: Could not connect to host "'.
$host.
'" on port '.
$port);
// Set the timeout for this connection
// Connect to the FTP server.
$this->_conn =
@ fsockopen($host, $port, $errno, $err, $this->_timeout);
JError::raiseWarning('30', 'JFTP::connect: Could not connect to host "'.
$host.
'" on port '.
$port, 'Socket error number '.
$errno.
' and error message: '.
$err);
// Set the timeout for this connection
// Check for welcome response code
if (!$this->_verifyResponse(220)) {
JError::raiseWarning('35', 'JFTP::connect: Bad response', 'Server response: '.
$this->_response.
' [Expected: 220]');
* Method to determine if the object is connected to an FTP server
* @return boolean True if connected
* Method to login to a server once connected
* @param string $user Username to login to the server
* @param string $pass Password to login to the server
* @return boolean True if successful
function login($user =
'anonymous', $pass =
'jftp@joomla.org') {
// If native FTP support is enabled lets use it...
if (@ftp_login($this->_conn, $user, $pass) ===
false) {
if (!$this->_putCmd('USER '.
$user, array(331, 503))) {
JError::raiseWarning('33', 'JFTP::login: Bad Username', 'Server response: '.
$this->_response.
' [Expected: 331] Username sent: '.
$user );
// If we are already logged in, continue :)
if ($this->_responseCode ==
503) {
if (!$this->_putCmd('PASS '.
$pass, 230)) {
* Method to quit and close the connection
* @return boolean True if successful
// If native FTP support is enabled lets use it...
// Logout and close connection
@fwrite($this->_conn, "QUIT\r\n");
* Method to retrieve the current working directory on the FTP server
* @return string Current working directory
// If native FTP support is enabled lets use it...
if (($ret =
@ftp_pwd($this->_conn)) ===
false) {
// Send print working directory command and verify success
if (!$this->_putCmd('PWD', 257)) {
JError::raiseWarning('35', 'JFTP::pwd: Bad response', 'Server response: '.
$this->_response.
' [Expected: 257]' );
preg_match('/"[^"\r\n]*"/', $this->_response, $match);
// Return the cleaned path
* Method to system string from the FTP server
* @return string System identifier string
// If native FTP support is enabled lets use it...
// Send print working directory command and verify success
if (!$this->_putCmd('SYST', 215)) {
JError::raiseWarning('35', 'JFTP::syst: Bad response', 'Server response: '.
$this->_response.
' [Expected: 215]' );
// Match the system string to an OS
* Method to change the current working directory on the FTP server
* @param string $path Path to change into on the server
* @return boolean True if successful
// If native FTP support is enabled lets use it...
if (@ftp_chdir($this->_conn, $path) ===
false) {
// Send change directory command and verify success
if (!$this->_putCmd('CWD '.
$path, 250)) {
JError::raiseWarning('35', 'JFTP::chdir: Bad response', 'Server response: '.
$this->_response.
' [Expected: 250] Path sent: '.
$path );
* Method to reinitialize the server, ie. need to login again
* NOTE: This command not available on all servers
* @return boolean True if successful
// If native FTP support is enabled lets use it...
if (@ftp_site($this->_conn, 'REIN') ===
false) {