⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ftp.php

📁 Joomla!是一套获得过多个奖项的内容管理系统(Content Management System, CMS)。Joomla!采用PHP+MySQL数据库开发
💻 PHP
📖 第 1 页 / 共 3 页
字号:
<?php/*** @version		$Id: ftp.php 10707 2008-08-21 09:52:47Z eddieajau $* @package		Joomla.Framework* @subpackage	Client* @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 frameworkdefined('JPATH_BASE') or die();/** Error Codes: *  - 30 : Unable to connect to host *  - 31 : Not connected *  - 32 : Unable to send command to server *  - 33 : Bad username *  - 34 : Bad password *  - 35 : Bad response *  - 36 : Passive mode failed *  - 37 : Data transfer error *  - 38 : Local filesystem error */if (!defined('CRLF')) {	define('CRLF', "\r\n");}if (!defined("FTP_AUTOASCII")) {	define("FTP_AUTOASCII", -1);}if (!defined("FTP_BINARY")) {	define("FTP_BINARY", 1);}if (!defined("FTP_ASCII")) {	define("FTP_ASCII", 0);}// Is FTP extension loaded?  If not try to load itif (!extension_loaded('ftp')) {	if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {		@ dl('php_ftp.dll');	} else {		@ dl('ftp.so');	}}if (!defined('FTP_NATIVE')) {	define('FTP_NATIVE', (function_exists('ftp_connect'))? 1 : 0);}/** * FTP client class * * @package		Joomla.Framework * @subpackage	Client * @since		1.5 */class JFTP extends JObject{	/**	 * Server connection resource	 *	 * @access private	 * @var socket resource	 */	var $_conn = null;	/**	 * Data port connection resource	 *	 * @access private	 * @var socket resource	 */	var $_dataconn = null;	/**	 * Passive connection information	 *	 * @access private	 * @var array	 */	var $_pasv = null;	/**	 * Response Message	 *	 * @access private	 * @var string	 */	var $_response = null;	/**	 * Timeout limit	 *	 * @access private	 * @var int	 */	var $_timeout = 15;	/**	 * Transfer Type	 *	 * @access private	 * @var int	 */	var $_type = null;	/**	 * Native OS Type	 *	 * @access private	 * @var string	 */	var $_OS = null;	/**	 * Array to hold ascii format file extensions	 *	 * @final	 * @access private	 * @var array	 */	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	 *	 * @final	 * @access private	 * @var array	 */	var $_lineEndings = array ('UNIX' => "\n", 'MAC' => "\r", 'WIN' => "\r\n");	/**	 * JFTP object constructor	 *	 * @access protected	 * @param array $options Associative array of options to set	 * @since 1.5	 */	function __construct($options=array()) {		// If default transfer type is no set, set it to autoascii detect		if (!isset ($options['type'])) {			$options['type'] = FTP_BINARY;		}		$this->setOptions($options);		if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {			$this->_OS = 'WIN';		} elseif (strtoupper(substr(PHP_OS, 0, 3)) === 'MAC') {			$this->_OS = 'MAC';		} else {			$this->_OS = 'UNIX';		}		if (FTP_NATIVE) {			// Import the generic buffer stream handler			jimport('joomla.utilities.buffer');			// Autoloading fails for JBuffer as the class is used as a stream handler			JLoader::load('JBuffer');		}		// Register faked "destructor" in PHP4 to close all connections we might have made		if (version_compare(PHP_VERSION, '5') == -1) {			register_shutdown_function(array(&$this, '__destruct'));		}	}	/**	 * JFTP object destructor	 *	 * Closes an existing connection, if we have one	 *	 * @access protected	 * @since 1.5	 */	function __destruct() {		if (is_resource($this->_conn)) {			$this->quit();		}	}	/**	 * 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.	 * @since 1.5	 */	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);		} else {			$instances[$signature]->setOptions($options);		}		// Connect to the server, and login, if requested		if (!$instances[$signature]->isConnected()) {			$return = $instances[$signature]->connect($host, $port);			if ($return && $user !== null && $pass !== null) {				$instances[$signature]->login($user, $pass);			}		}		return $instances[$signature];	}	/**	 * Set client options	 *	 * @access public	 * @param array $options Associative array of options to set	 * @return boolean True if successful	 */	function setOptions($options) {		if (isset ($options['type'])) {			$this->_type = $options['type'];		}		if (isset ($options['timeout'])) {			$this->_timeout = $options['timeout'];		}		return true;	}	/**	 * Method to connect to a FTP server	 *	 * @access public	 * @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) {		// Initialize variables		$errno = null;		$err = null;		// If already connected, return		if (is_resource($this->_conn)) {			return true;		}		// If native FTP support is enabled lets use it...		if (FTP_NATIVE) {			$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);				return false;			}			// Set the timeout for this connection			ftp_set_option($this->_conn, FTP_TIMEOUT_SEC, $this->_timeout);			return true;		}		// Connect to the FTP server.		$this->_conn = @ fsockopen($host, $port, $errno, $err, $this->_timeout);		if (!$this->_conn) {			JError::raiseWarning('30', 'JFTP::connect: Could not connect to host "'.$host.'" on port '.$port, 'Socket error number '.$errno.' and error message: '.$err);			return false;		}		// Set the timeout for this connection		socket_set_timeout($this->_conn, $this->_timeout);		// Check for welcome response code		if (!$this->_verifyResponse(220)) {			JError::raiseWarning('35', 'JFTP::connect: Bad response', 'Server response: '.$this->_response.' [Expected: 220]');			return false;		}		return true;	}	/**	 * Method to determine if the object is connected to an FTP server	 *	 * @access	public	 * @return	boolean	True if connected	 * @since	1.5	 */	function isConnected()	{		return is_resource($this->_conn);	}	/**	 * Method to login to a server once connected	 *	 * @access public	 * @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_NATIVE) {			if (@ftp_login($this->_conn, $user, $pass) === false) {				JError::raiseWarning('30', 'JFTP::login: Unable to login' );				return false;			}			return true;		}		// Send the username		if (!$this->_putCmd('USER '.$user, array(331, 503))) {			JError::raiseWarning('33', 'JFTP::login: Bad Username', 'Server response: '.$this->_response.' [Expected: 331] Username sent: '.$user );			return false;		}		// If we are already logged in, continue :)		if ($this->_responseCode == 503) {			return true;		}		// Send the password		if (!$this->_putCmd('PASS '.$pass, 230)) {			JError::raiseWarning('34', 'JFTP::login: Bad Password', 'Server response: '.$this->_response.' [Expected: 230] Password sent: '.str_repeat('*', strlen($pass)));			return false;		}		return true;	}	/**	 * Method to quit and close the connection	 *	 * @access public	 * @return boolean True if successful	 */	function quit() {		// If native FTP support is enabled lets use it...		if (FTP_NATIVE) {			@ftp_close($this->_conn);			return true;		}		// Logout and close connection		@fwrite($this->_conn, "QUIT\r\n");		@fclose($this->_conn);		return true;	}	/**	 * Method to retrieve the current working directory on the FTP server	 *	 * @access public	 * @return string Current working directory	 */	function pwd() {		// If native FTP support is enabled lets use it...		if (FTP_NATIVE) {			if (($ret = @ftp_pwd($this->_conn)) === false) {				JError::raiseWarning('35', 'JFTP::pwd: Bad response' );				return false;			}			return $ret;		}		// Initialize variables		$match = array (null);		// 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]' );			return false;		}		// Match just the path		preg_match('/"[^"\r\n]*"/', $this->_response, $match);		// Return the cleaned path		return preg_replace("/\"/", "", $match[0]);	}	/**	 * Method to system string from the FTP server	 *	 * @access public	 * @return string System identifier string	 */	function syst() {		// If native FTP support is enabled lets use it...		if (FTP_NATIVE) {			if (($ret = @ftp_systype($this->_conn)) === false) {				JError::raiseWarning('35', 'JFTP::syst: Bad response' );				return false;			}		} else {			// 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]' );				return false;			}			$ret = $this->_response;		}		// Match the system string to an OS		if (strpos(strtoupper($ret), 'MAC') !== false) {			$ret = 'MAC';		} elseif (strpos(strtoupper($ret), 'WIN') !== false) {			$ret = 'WIN';		} else {			$ret = 'UNIX';		}		// Return the os type		return $ret;	}	/**	 * Method to change the current working directory on the FTP server	 *	 * @access public	 * @param string $path Path to change into on the server	 * @return boolean True if successful	 */	function chdir($path) {		// If native FTP support is enabled lets use it...		if (FTP_NATIVE) {			if (@ftp_chdir($this->_conn, $path) === false) {				JError::raiseWarning('35', 'JFTP::chdir: Bad response' );				return false;			}			return true;		}		// 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 );			return false;		}		return true;	}	/**	 * Method to reinitialize the server, ie. need to login again	 *	 * NOTE: This command not available on all servers	 *	 * @access public	 * @return boolean True if successful	 */	function reinit() {		// If native FTP support is enabled lets use it...		if (FTP_NATIVE) {			if (@ftp_site($this->_conn, 'REIN') === false) {				JError::raiseWarning('35', 'JFTP::reinit: Bad response' );				return false;			}			return true;		}		// Send reinitialize command to the server		if (!$this->_putCmd('REIN', 220)) {			JError::raiseWarning('35', 'JFTP::reinit: Bad response', 'Server response: '.$this->_response.' [Expected: 220]' );			return false;		}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -