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

📄 ftp.php

📁 Joomla!是一套获得过多个奖项的内容管理系统(Content Management System, CMS)。Joomla!采用PHP+MySQL数据库开发
💻 PHP
📖 第 1 页 / 共 3 页
字号:
				JError::raiseWarning('37', 'JFTP::write: Unable to write to data port socket' );				return false;			}			$buffer = substr($buffer, $result);		} while ($buffer != "");		// Close the data connection port [Data transfer complete]		fclose($this->_dataconn);		// Verify that the server recieved the transfer		if (!$this->_verifyResponse(226)) {			JError::raiseWarning('37', 'JFTP::write: Transfer Failed', 'Server response: '.$this->_response.' [Expected: 226] Path sent: '.$remote );			return false;		}		return true;	}	/**	 * Method to list the filenames of the contents of a directory on the FTP server	 *	 * Note: Some servers also return folder names. However, to be sure to list folders on all	 * servers, you should use listDetails() instead, if you also need to deal with folders	 *	 * @access public	 * @param string $path Path local file to store on the FTP server	 * @return string Directory listing	 */	function listNames($path = null) {		// Initialize variables		$data = null;		// If native FTP support is enabled lets use it...		if (FTP_NATIVE) {			// turn passive mode on			if (@ftp_pasv($this->_conn, true) === false) {				JError::raiseWarning('36', 'JFTP::listNames: Unable to use passive mode' );				return false;			}			if (($list = @ftp_nlist($this->_conn,$path)) === false) {				// Workaround for empty directories on some servers				if ($this->listDetails($path, 'files') === array()) {					return array();				}				JError::raiseWarning('35', 'JFTP::listNames: Bad response' );				return false;			}			$list = preg_replace('#^'.preg_quote($path, '#').'[/\\\\]?#', '', $list);			if ($keys = array_merge(array_keys($list, '.'), array_keys($list, '..'))) {				foreach ($keys as $key) {					unset($list[$key]);				}			}			return $list;		}		/*		 * If a path exists, prepend a space		 */		if ($path != null) {			$path = ' ' . $path;		}		// Start passive mode		if (!$this->_passive()) {			JError::raiseWarning('36', 'JFTP::listNames: Unable to use passive mode' );			return false;		}		if (!$this->_putCmd('NLST'.$path, array (150, 125))) {			@ fclose($this->_dataconn);			// Workaround for empty directories on some servers			if ($this->listDetails($path, 'files') === array()) {				return array();			}			JError::raiseWarning('35', 'JFTP::listNames: Bad response', 'Server response: '.$this->_response.' [Expected: 150 or 125] Path sent: '.$path );			return false;		}		// Read in the file listing.		while (!feof($this->_dataconn)) {			$data .= fread($this->_dataconn, 4096);		}		fclose($this->_dataconn);		// Everything go okay?		if (!$this->_verifyResponse(226)) {			JError::raiseWarning('37', 'JFTP::listNames: Transfer Failed', 'Server response: '.$this->_response.' [Expected: 226] Path sent: '.$path );			return false;		}		$data = preg_split("/[".CRLF."]+/", $data, -1, PREG_SPLIT_NO_EMPTY);		$data = preg_replace('#^'.preg_quote(substr($path, 1), '#').'[/\\\\]?#', '', $data);		if ($keys = array_merge(array_keys($data, '.'), array_keys($data, '..'))) {			foreach ($keys as $key) {				unset($data[$key]);			}		}		return $data;	}	/**	 * Method to list the contents of a directory on the FTP server	 *	 * @access public	 * @param string $path Path local file to store on the FTP server	 * @param string $type Return type [raw|all|folders|files]	 * @param boolean $search Recursively search subdirectories	 * @return mixed : if $type is raw: string Directory listing, otherwise array of string with file-names	 */	function listDetails($path = null, $type = 'all') {		// Initialize variables		$dir_list = array();		$data = null;		$regs = null;		// TODO: Deal with recurse -- nightmare		// For now we will just set it to false		$recurse = false;		// If native FTP support is enabled lets use it...		if (FTP_NATIVE) {			// turn passive mode on			if (@ftp_pasv($this->_conn, true) === false) {				JError::raiseWarning('36', 'JFTP::listDetails: Unable to use passive mode' );				return false;			}			if (($contents = @ftp_rawlist($this->_conn, $path)) === false) {				JError::raiseWarning('35', 'JFTP::listDetails: Bad response' );				return false;			}		} else {			// Non Native mode			// Start passive mode			if (!$this->_passive()) {				JError::raiseWarning('36', 'JFTP::listDetails: Unable to use passive mode' );				return false;			}			// If a path exists, prepend a space			if ($path != null) {				$path = ' ' . $path;			}			// Request the file listing			if (!$this->_putCmd(($recurse == true) ? 'LIST -R' : 'LIST'.$path, array (150, 125))) {				JError::raiseWarning('35', 'JFTP::listDetails: Bad response', 'Server response: '.$this->_response.' [Expected: 150 or 125] Path sent: '.$path );				@ fclose($this->_dataconn);				return false;			}			// Read in the file listing.			while (!feof($this->_dataconn)) {				$data .= fread($this->_dataconn, 4096);			}			fclose($this->_dataconn);			// Everything go okay?			if (!$this->_verifyResponse(226)) {				JError::raiseWarning('37', 'JFTP::listDetails: Transfer Failed', 'Server response: '.$this->_response.' [Expected: 226] Path sent: '.$path );				return false;			}			$contents = explode(CRLF, $data);		}		// If only raw output is requested we are done		if ($type == 'raw') {			return $data;		}		// If we received the listing of an emtpy directory, we are done as well		if (empty($contents[0])) {			return $dir_list;		}		// If the server returned the number of results in the first response, let's dump it		if (strtolower(substr($contents[0], 0, 6)) == 'total ') {			array_shift($contents);			if (!isset($contents[0]) || empty($contents[0])) {				return $dir_list;			}		}		// Regular expressions for the directory listing parsing		$regexps['UNIX'] = '([-dl][rwxstST-]+).* ([0-9]*) ([a-zA-Z0-9]+).* ([a-zA-Z0-9]+).* ([0-9]*) ([a-zA-Z]+[0-9: ]*[0-9])[ ]+(([0-9]{1,2}:[0-9]{2})|[0-9]{4}) (.+)';		$regexps['MAC'] = '([-dl][rwxstST-]+).* ?([0-9 ]* )?([a-zA-Z0-9]+).* ([a-zA-Z0-9]+).* ([0-9]*) ([a-zA-Z]+[0-9: ]*[0-9])[ ]+(([0-9]{2}:[0-9]{2})|[0-9]{4}) (.+)';		$regexps['WIN'] = '([0-9]{2})-([0-9]{2})-([0-9]{2}) +([0-9]{2}):([0-9]{2})(AM|PM) +([0-9]+|<DIR>) +(.+)';		// Find out the format of the directory listing by matching one of the regexps		$osType = null;		foreach ($regexps as $k=>$v) {			if (ereg($v, $contents[0])) {				$osType = $k;				$regexp = $v;				break;			}		}		if (!$osType) {			JError::raiseWarning('SOME_ERROR_CODE', 'JFTP::listDetails: Unrecognized directory listing format' );			return false;		}		/*		 * Here is where it is going to get dirty....		 */		if ($osType == 'UNIX') {			foreach ($contents as $file) {				$tmp_array = null;				if (ereg($regexp, $file, $regs)) {					$fType = (int) strpos("-dl", $regs[1] { 0 });					//$tmp_array['line'] = $regs[0];					$tmp_array['type'] = $fType;					$tmp_array['rights'] = $regs[1];					//$tmp_array['number'] = $regs[2];					$tmp_array['user'] = $regs[3];					$tmp_array['group'] = $regs[4];					$tmp_array['size'] = $regs[5];					$tmp_array['date'] = date("m-d", strtotime($regs[6]));					$tmp_array['time'] = $regs[7];					$tmp_array['name'] = $regs[9];				}				// If we just want files, do not add a folder				if ($type == 'files' && $tmp_array['type'] == 1) {					continue;				}				// If we just want folders, do not add a file				if ($type == 'folders' && $tmp_array['type'] == 0) {					continue;				}				if (is_array($tmp_array) && $tmp_array['name'] != '.' && $tmp_array['name'] != '..') {					$dir_list[] = $tmp_array;				}			}		}		elseif ($osType == 'MAC') {			foreach ($contents as $file) {				$tmp_array = null;				if (ereg($regexp, $file, $regs)) {					$fType = (int) strpos("-dl", $regs[1] { 0 });					//$tmp_array['line'] = $regs[0];					$tmp_array['type'] = $fType;					$tmp_array['rights'] = $regs[1];					//$tmp_array['number'] = $regs[2];					$tmp_array['user'] = $regs[3];					$tmp_array['group'] = $regs[4];					$tmp_array['size'] = $regs[5];					$tmp_array['date'] = date("m-d", strtotime($regs[6]));					$tmp_array['time'] = $regs[7];					$tmp_array['name'] = $regs[9];				}				// If we just want files, do not add a folder				if ($type == 'files' && $tmp_array['type'] == 1) {					continue;				}				// If we just want folders, do not add a file				if ($type == 'folders' && $tmp_array['type'] == 0) {					continue;				}				if (is_array($tmp_array) && $tmp_array['name'] != '.' && $tmp_array['name'] != '..') {					$dir_list[] = $tmp_array;				}			}		} else {			foreach ($contents as $file) {				$tmp_array = null;				if (ereg($regexp, $file, $regs)) {					$fType = (int) ($regs[7] == '<DIR>');					$timestamp = strtotime("$regs[3]-$regs[1]-$regs[2] $regs[4]:$regs[5]$regs[6]");					//$tmp_array['line'] = $regs[0];					$tmp_array['type'] = $fType;					$tmp_array['rights'] = '';					//$tmp_array['number'] = 0;					$tmp_array['user'] = '';					$tmp_array['group'] = '';					$tmp_array['size'] = (int) $regs[7];					$tmp_array['date'] = date('m-d', $timestamp);					$tmp_array['time'] = date('H:i', $timestamp);					$tmp_array['name'] = $regs[8];				}				// If we just want files, do not add a folder				if ($type == 'files' && $tmp_array['type'] == 1) {					continue;				}				// If we just want folders, do not add a file				if ($type == 'folders' && $tmp_array['type'] == 0) {					continue;				}				if (is_array($tmp_array) && $tmp_array['name'] != '.' && $tmp_array['name'] != '..') {					$dir_list[] = $tmp_array;				}			}		}		return $dir_list;	}	/**	 * Send command to the FTP server and validate an expected response code	 *	 * @access private	 * @param string $cmd Command to send to the FTP server	 * @param mixed $expected Integer response code or array of integer response codes	 * @return boolean True if command executed successfully	 */	function _putCmd($cmd, $expectedResponse) {		// Make sure we have a connection to the server		if (!is_resource($this->_conn)) {			JError::raiseWarning('31', 'JFTP::_putCmd: Not connected to the control port' );			return false;		}		// Send the command to the server		if (!fwrite($this->_conn, $cmd."\r\n")) {			JError::raiseWarning('32', 'JFTP::_putCmd: Unable to send command: '.$cmd );		}		return $this->_verifyResponse($expectedResponse);	}	/**	 * Verify the response code from the server and log response if flag is set	 *	 * @access private	 * @param mixed $expected Integer response code or array of integer response codes	 * @return boolean True if response code from the server is expected	 */	function _verifyResponse($expected) {		// Initialize variables		$parts = null;		// Wait for a response from the server, but timeout after the set time limit		$endTime = time() + $this->_timeout;		$this->_response = '';		do {			$this->_response .= fgets($this->_conn, 4096);		} while (!preg_match("/^([0-9]{3})(-(.*".CRLF.")+\\1)? [^".CRLF."]+".CRLF."$/", $this->_response, $parts) && time() < $endTime);		// Catch a timeout or bad response		if (!isset($parts[1])) {			JError::raiseWarning('SOME_ERROR_CODE', 'JFTP::_verifyResponse: Timeout or unrecognized response while waiting for a response from the server', 'Server response: '.$this->_response);			return false;		}		// Separate the code from the message		$this->_responseCode = $parts[1];		$this->_responseMsg = $parts[0];		// Did the server respond with the code we wanted?		if (is_array($expected)) {			if (in_array($this->_responseCode, $expected)) {				$retval = true;			} else {				$retval = false;			}		} else {			if ($this->_responseCode == $expected) {				$retval = true;			} else {				$retval = false;			}		}		return $retval;	}	/**	 * Set server to passive mode and open a data port connection	 *	 * @access private	 * @return boolean True if successful	 */	function _passive() {		//Initialize variables		$match = array();		$parts = array();		$errno = null;		$err = null;		// Make sure we have a connection to the server		if (!is_resource($this->_conn)) {			JError::raiseWarning('31', 'JFTP::_passive: Not connected to the control port' );			return false;		}		// Request a passive connection - this means, we'll talk to you, you don't talk to us.		@ fwrite($this->_conn, "PASV\r\n");		// Wait for a response from the server, but timeout after the set time limit		$endTime = time() + $this->_timeout;		$this->_response = '';		do {			$this->_response .= fgets($this->_conn, 4096);		} while (!preg_match("/^([0-9]{3})(-(.*".CRLF.")+\\1)? [^".CRLF."]+".CRLF."$/", $this->_response, $parts) && time() < $endTime);		// Catch a timeout or bad response		if (!isset($parts[1])) {			JError::raiseWarning('SOME_ERROR_CODE', 'JFTP::_passive: Timeout or unrecognized response while waiting for a response from the server', 'Server response: '.$this->_response);			return false;		}		// Separate the code from the message		$this->_responseCode = $parts[1];		$this->_responseMsg = $parts[0];		// If it's not 227, we weren't given an IP and port, which means it failed.		if ($this->_responseCode != '227') {			JError::raiseWarning('36', 'JFTP::_passive: Unable to obtain IP and port for data transfer', 'Server response: '.$this->_responseMsg);			return false;		}		// Snatch the IP and port information, or die horribly trying...		if (preg_match('~\((\d+),\s*(\d+),\s*(\d+),\s*(\d+),\s*(\d+)(?:,\s*(\d+))\)~', $this->_responseMsg, $match) == 0) {			JError::raiseWarning('36', 'JFTP::_passive: IP and port for data transfer not valid', 'Server response: '.$this->_responseMsg);			return false;		}		// This is pretty simple - store it for later use ;).		$this->_pasv = array ('ip' => $match[1].'.'.$match[2].'.'.$match[3].'.'.$match[4], 'port' => $match[5] * 256 + $match[6]);		// Connect, assuming we've got a connection.		$this->_dataconn =  @fsockopen($this->_pasv['ip'], $this->_pasv['port'], $errno, $err, $this->_timeout);		if (!$this->_dataconn) {			JError::raiseWarning('30', 'JFTP::_passive: Could not connect to host '.$this->_pasv['ip'].' on port '.$this->_pasv['port'].'.  Socket error number '.$errno.' and error message: '.$err );			return false;		}		// Set the timeout for this connection		socket_set_timeout($this->_conn, $this->_timeout);		return true;	}	/**	 * Method to find out the correct transfer mode for a specific file	 *	 * @access	private	 * @param	string	$fileName	Name of the file	 * @return	integer	Transfer-mode for this filetype [FTP_ASCII|FTP_BINARY]	 */	function _findMode($fileName) {		if ($this->_type == FTP_AUTOASCII) {			$dot = strrpos($fileName, '.') + 1;			$ext = substr($fileName, $dot);			if (in_array($ext, $this->_autoAscii)) {				$mode = FTP_ASCII;			} else {				$mode = FTP_BINARY;			}		} elseif ($this->_type == FTP_ASCII) {			$mode = FTP_ASCII;		} else {			$mode = FTP_BINARY;		}		return $mode;	}	/**	 * Set transfer mode	 *	 * @access private	 * @param int $mode Integer representation of data transfer mode [1:Binary|0:Ascii]	 *  Defined constants can also be used [FTP_BINARY|FTP_ASCII]	 * @return boolean True if successful	 */	function _mode($mode) {		if ($mode == FTP_BINARY) {			if (!$this->_putCmd("TYPE I", 200)) {				JError::raiseWarning('35', 'JFTP::_mode: Bad response', 'Server response: '.$this->_response.' [Expected: 200] Mode sent: Binary' );				return false;			}		} else {			if (!$this->_putCmd("TYPE A", 200)) {				JError::raiseWarning('35', 'JFTP::_mode: Bad response', 'Server response: '.$this->_response.' [Expected: 200] Mode sent: Ascii' );				return false;			}		}		return true;	}}

⌨️ 快捷键说明

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