?? usermanager.cpp
字號(hào):
// 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
CString strLocalPath;
if (!ConvertPathToLocal(lpszUser, strChangeTo, strLocalPath))
{
// unable to convert to local path
return 2;
}
// check if user has access right for this directory
if (!CheckAccessRights(lpszUser, strLocalPath, FTP_DOWNLOAD))
{
// user has no permissions
return 1;
}
// 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)
{
CString strDirectory = lpszDirectory;
// make unix style
strDirectory.Replace("\\","/");
while(strDirectory.Replace("//","/"));
// clear list
strResult = "";
CUser user;
if (!GetUser(lpszUser, user))
{
// user not found -> no permissions
return 1;
}
CString strLocalPath;
if (!ConvertPathToLocal(lpszUser, strDirectory, strLocalPath))
{
// unable to convert to local path
return 2;
}
// check if user has access right for this directory
if (!CheckAccessRights(lpszUser, strLocalPath, FTP_DOWNLOAD))
{
// user has no permissions, to display this directory
return 1;
}
CFileFind find;
BOOL bFound = FALSE;
// check if it's a directory
if ((GetFileAttributes(strLocalPath) & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY)
{
bFound = find.FindFile(strLocalPath + "\\*.*");
}
else
{
// it's a file
bFound = find.FindFile(strLocalPath);
}
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
for (int i=0; i<user.m_DirectoryArray.GetSize(); i++)
{
if (user.m_DirectoryArray.GetAt(i).m_bIsHomeDir)
{
CString strPath = user.m_DirectoryArray.GetAt(i).m_strDir;
strPath.TrimRight('\\');
if (strLocalPath.CompareNoCase(strPath) == 0)
{
for (int j=0; j<user.m_DirectoryArray.GetSize(); j++)
{
if (i != j && user.m_DirectoryArray.GetAt(j).m_strAlias != "")
{
if (find.FindFile(user.m_DirectoryArray.GetAt(j).m_strDir))
{
find.FindNextFile();
strResult += "drwx------";
// groups
strResult += " 1 user group ";
strResult += " 0";
// file date
strResult += GetFileDate(find);
// file name
strResult += user.m_DirectoryArray.GetAt(j).m_strAlias;
// end of line
strResult += "\r\n";
}
}
}
}
break;
}
}
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;
}
}
/********************************************************************/
/* */
/* Function name : CheckFileName */
/* Description : Check if filename is valid and if user has */
/* access rights. */
/* */
/********************************************************************/
int CUserManager::CheckFileName(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
CString strLocalPath;
if (!ConvertPathToLocal(lpszUser, strDirectory, strLocalPath))
{
// directory does not exist
return 2;
}
// create the complete path
strResult = strLocalPath + "\\" + strFilename;
if ((nOption != FTP_UPLOAD) && !FileExists(strResult, FALSE))
{
// file does not exist
return 2;
}
// return relative path
if (nOption == FTP_LIST)
{
strResult = strCurrentdir;
strResult.TrimRight('/');
strResult += "/" + strFilename;
return 0;
}
// check file access rights
if (!CheckAccessRights(lpszUser, strLocalPath, nOption))
{
// user has no permissions, to execute specified action
return 1;
}
// everything is ok
return 0;
}
/********************************************************************/
/* */
/* Function name : CheckDirectory */
/* Description : Check if directory exists and if user has */
/* appropriate permissions. */
/* */
/********************************************************************/
int CUserManager::CheckDirectory(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);
CString strLocalPath;
do
{
// does parent directory exist ?
if ((!ConvertPathToLocal(lpszUser, strNode, strLocalPath)) && (nOption == FTP_CREATE_DIR))
{
// directory could not be found, maybe one level higher
int nPos = strNode.ReverseFind('/');
// no more levels
if (nPos == -1)
return 2;
strDirectory = strNode.Mid(nPos+1) + "/" + strDirectory;
strNode = strNode.Left(nPos);
continue;
}
// check directory access rights
if (!CheckAccessRights(lpszUser, strLocalPath, nOption))
{
// user has no permissions, to execute specified action
return 1;
}
strNode = strLocalPath;
break;
}
while (strNode != "/");
strDirectory.Replace("/","\\");
strResult = strNode + "\\" + strDirectory;
// check if directory exists
if (!FileExists(strResult))
return 2;
// function successfull
return 0;
}
/********************************************************************/
/* */
/* 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;
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -