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

📄 usermanager.cpp

📁 It can also accept a number of ftp connection (multithreaded), and with most of the commercial ftp s
💻 CPP
📖 第 1 页 / 共 2 页
字号:
			strChangeTo = strCurrentdir + "/" + strChangeTo;
		}
	}
	
	// goto parent directory
	if (strChangeTo.Right(2) == "..")
	{
		strChangeTo.TrimRight("..");
		strChangeTo.TrimRight("/");
		int nPos = strChangeTo.ReverseFind('/');
		if (nPos != -1)
			strChangeTo = strChangeTo.Left(nPos);

		if (strChangeTo == "")
		{
			// goto root
			strChangeTo = "/";
		}
	}

	// get local path
	CDirectory directory;
	BOOL bIsRootDir;
	int nResult = ConvertPathToLocal(strChangeTo, lpszUser, directory, bIsRootDir);
	
	if (nResult)
		return nResult;

	if (!directory.m_bAllowDownload)
		return 1;
	
	// is it really a directory ?
	if (!FileExists(directory.m_strDir, TRUE))
		return 2;

	// everything went successfully
	strCurrentdir = strChangeTo; 
	return 0;
}


/********************************************************************/
/*																	*/
/* Function name : GetDirectoryList									*/
/* Description   : Get directory listing for specfied directory		*/
/*																	*/
/********************************************************************/
int CUserManager::GetDirectoryList(LPCTSTR lpszUser, LPCTSTR lpszDirectory, CString &strResult)
{
	CUser user;
	
	// clear list
	strResult = "";

	if (!GetUser(lpszUser, user))
		return 1;

	CDirectory directory;
	BOOL bIsRootDir;
	int nResult = ConvertPathToLocal(lpszDirectory, lpszUser, directory, bIsRootDir);
	if (nResult)
		return nResult;

	CFileFind find;
	BOOL bFound = FALSE;

	// quick way to check if it's a directory
	if ((GetFileAttributes(directory.m_strDir) & FILE_ATTRIBUTE_DIRECTORY) == 
		FILE_ATTRIBUTE_DIRECTORY)
	{
		bFound = find.FindFile(directory.m_strDir + "\\*.*");
	}
	else
	{
		// it's a file
		bFound = find.FindFile(directory.m_strDir);
	}

	while (bFound)
	{
		bFound = find.FindNextFile();
		
		// skip "." and ".." 
		if (find.IsDots())
			continue;

		// permissions
		if (find.IsDirectory())
			strResult += "drwx------";
		else
			strResult += "-rwx------";

		// groups
		strResult += " 1 user group ";
		// file size
		CString strLength;
		strLength.Format("%d", find.GetLength());
		CString strFiller = "              ";
		strResult += strFiller.Left(strFiller.GetLength() - strLength.GetLength());
		strResult += strLength;
		// file date
		strResult += GetFileDate(find);
		// file name
		strResult += find.GetFileName();
		// end of line
		strResult += "\r\n";
	}
	
	// if it's the root dir also show virtual directories
	if (directory.m_bIsHomeDir)
	{
		for (int i=0; i<user.m_DirectoryArray.GetSize(); i++)
		{
			if (!user.m_DirectoryArray.GetAt(i).m_bIsHomeDir && user.m_DirectoryArray.GetAt(i).m_strAlias != "")
			{
				bFound = find.FindFile(user.m_DirectoryArray.GetAt(i).m_strDir);
				if (bFound)
				{
					find.FindNextFile();

					strResult += "drwx------";
					// groups
					strResult += " 1 user group ";
					strResult += "             0";
					// file date
					strResult += GetFileDate(find);
					// file name
					strResult += user.m_DirectoryArray.GetAt(i).m_strAlias;
					// end of line
					strResult += "\r\n"; 
				}
			}
		}
	}

	return 0;
}


/********************************************************************/
/*																	*/
/* Function name : FileExists										*/
/* Description   : Check if file or directory exists				*/
/*																	*/
/********************************************************************/
BOOL CUserManager::FileExists(LPCTSTR lpszFileName, BOOL bIsDirCheck)
{
	// A quick'n'easy way to see if a file exists.
	DWORD dwAttributes = GetFileAttributes(lpszFileName);
    if (dwAttributes == 0xFFFFFFFF)
        return FALSE;

	if ((dwAttributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY)
	{
		if (bIsDirCheck)
			return TRUE;
		else
			return FALSE;
	}
	else
	{
		if (!bIsDirCheck)
			return TRUE;
		else
			return FALSE;
	}
/*
    HANDLE hFindFile;
    WIN32_FIND_DATA findData;

	if (strFileName.Right(1) == "\\")
	{
		strFileName += "*.*";
	}

    hFindFile = FindFirstFile(strFileName, &findData);
    if (hFindFile == INVALID_HANDLE_VALUE)
	{
		FindClose(hFindFile);
		return FALSE;
	}
     
    FindClose(hFindFile);
	return TRUE; */
}


/********************************************************************/
/*																	*/
/* Function name : GetFileName										*/
/* Description   : Get file name									*/
/*																	*/
/********************************************************************/
int CUserManager::GetFileName(LPCTSTR lpszUser, CString strFilename, CString strCurrentdir, int nOption, CString &strResult)
{
	// make unix style
	strFilename.Replace("\\", "/");
	while(strFilename.Replace("//", "/"));
	strFilename.TrimRight("/");
	
	if (strFilename == "")
	{
		// no file name
		return 2;
	}

	// append filename to directory
	CString strDirectory = strCurrentdir;

	// client has specified complete path 
	int nPos = strFilename.ReverseFind('/');
	if (nPos != -1)
	{
		strDirectory = strFilename.Left(nPos);
		if (strDirectory == "")
			strDirectory = "/";
		strFilename = strFilename.Mid(nPos+1);
	}

	// get local path
	CDirectory directory;
	BOOL bIsRootDir;
	int nResult = ConvertPathToLocal(strDirectory, lpszUser, directory, bIsRootDir);

	// we have a complete path !
	strResult = directory.m_strDir + "\\" + strFilename;

	if (nResult)
		return nResult;

	if ((nOption != FTP_UPLOAD) && !FileExists(strResult, FALSE))
	{
		// file does not exist
		return 2;
	}

	// check file access rights
	if (((!directory.m_bAllowDownload) && (nOption == FTP_DOWNLOAD)) ||
		((!directory.m_bAllowDelete) && (nOption == FTP_DELETE)) ||
		((!directory.m_bAllowUpload) && (nOption == FTP_UPLOAD)) ||
		((!directory.m_bAllowRename) && (nOption == FTP_RENAME)))
	{
		return 1;
	}
	
	// return relative path
	if (nOption == FTP_LIST)
	{
		strResult = strCurrentdir + "/" + strFilename;
	}
	return 0;
}


/********************************************************************/
/*																	*/
/* Function name : GetDirectory										*/
/* Description   :													*/
/*																	*/
/********************************************************************/
int CUserManager::GetDirectory(LPCTSTR lpszUser, CString strDirectory, CString strCurrentdir, int nOption, CString &strResult)
{
	// make unix compatible
	strDirectory.Replace("\\","/");
	while(strDirectory.Replace("//","/"));
	strDirectory.TrimRight("/");
	
	if (strDirectory == "")
	{
		// no directory
		return 2;
	}
	else
	{
		// first character '/' ?
		if (strDirectory.Left(1) != "/")
		{ 
			// client specified a path relative to their current path
			strCurrentdir.TrimRight("/");
			strDirectory = strCurrentdir + "/" + strDirectory;
		}
	}

	// split part into 2 parts
	int nPos = strDirectory.ReverseFind('/');
	if (nPos == -1)
		return 2;

	// get left part of directory
	CString strNode = strDirectory.Left(nPos);
	// root ?
	if (strNode == "")
		strNode = "/";
	// get right part of directory
	strDirectory = strDirectory.Mid(nPos+1);

	CDirectory directory;
	BOOL bIsRootDir;

	int nResult;
	do
	{
		// does parent directory exist ?
		nResult = ConvertPathToLocal(strNode, lpszUser, directory, bIsRootDir);
		if ((nResult == 2) && (nOption == FTP_CREATE_DIR)) 
		{ 
			// directory could not be found, maybe one level higher
			int nPos = strNode.ReverseFind('/');
			// no more levels
			if (nPos == -1) 
				return nResult;
			strDirectory = strNode.Mid(nPos+1) + "/" + strDirectory;
			strNode = strNode.Left(nPos);
			continue;
		}
		else 
		if (nResult) 
		{
			// not enough rights
			return nResult;
		}
		
		strNode = directory.m_strDir;
		
		// check directory access rights
		if ((!directory.m_bAllowDelete && nOption == FTP_DELETE) ||
			(!directory.m_bAllowRename && nOption == FTP_RENAME) ||
			(!directory.m_bAllowCreateDirectory && nOption == FTP_CREATE_DIR))
		{
			return 1;
		}
		break;
	} while (strNode != "/");
	
	strDirectory.Replace("/","\\");
	strResult = strNode + "\\" + strDirectory;
		
	// check if directory exists
	if (!FileExists(strResult))
		return 2;

	// function successfull
	return nResult;
}


/********************************************************************/
/*																	*/
/* Function name : GetUser											*/
/* Description   : Get user object for specified username			*/
/*																	*/
/********************************************************************/
BOOL CUserManager::GetUser(LPCTSTR lpszUser, CUser &user)
{
	m_CriticalSection.Lock();
	for (int i=0; i<m_UserArray.GetSize(); i++)
	{
		if (!m_UserArray[i].m_strName.CompareNoCase(lpszUser))
		{
			user = m_UserArray[i];
			m_CriticalSection.Unlock();
			return TRUE;
		}
	}
	m_CriticalSection.Unlock();
	return FALSE;
}


/********************************************************************/
/*																	*/
/* Function name : GetUserList										*/
/* Description   : Make copy of user array							*/
/*																	*/
/********************************************************************/
void CUserManager::GetUserList(CArray<CUser, CUser&>&array)
{
	m_CriticalSection.Lock();
	for (int i=0; i<m_UserArray.GetSize();i++)
	{
		array.Add(m_UserArray[i]);
	}
	m_CriticalSection.Unlock();
}


/********************************************************************/
/*																	*/
/* Function name : UpdateUserList									*/
/* Description   : Update user array								*/
/*																	*/
/********************************************************************/
void CUserManager::UpdateUserList(CArray<CUser, CUser&>&array)
{
	m_CriticalSection.Lock();
	m_UserArray.RemoveAll();
	for (int i=0; i<array.GetSize();i++)
	{
		m_UserArray.Add(array[i]);
	}
	m_CriticalSection.Unlock();
	Serialize(TRUE);
}


/********************************************************************/
/*																	*/
/* Function name : GetFileDate										*/
/* Description   : return file date in unix style					*/
/*																	*/
/********************************************************************/
CString CUserManager::GetFileDate(CFileFind &find)
{
	CString strResult;

	CTime time = CTime::GetCurrentTime();

	find.GetLastWriteTime(time);

	CTimeSpan timeSpan = CTime::GetCurrentTime() - time;

	if (timeSpan.GetDays() > 356)
	{
		strResult = time.Format(" %b %d %Y ");
	}
	else
	{
		strResult.Format(" %s %02d:%02d ", time.Format("%b %d"), time.GetHour(), time.GetMinute());
	}

	return strResult;
}

⌨️ 快捷键说明

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