/****************************************************************/ /* */ /* UserManager.cpp */ /* */ /* Implementation of the CUserManager class. */ /* */ /* Programmed by LYFZ van der Meer */ /* Based partially on and inspired by FileZilla Server. */ /* */ /* Copyright LYFZ Software Solutions 2002 */ /* http://www.LYFZvandermeer.nl */ /* */ /* Last updated: 21 july 2002 */ /* */ /****************************************************************/ #include "stdafx.h" #include "CPhotoFTPReceiveApp.h" #include "UserManager.h" #include "Shlwapi.h" #pragma comment(lib, "Shlwapi.lib") #include "BranchInfo.h" extern CCPhotoFTPReceiveApp theApp; IMPLEMENT_SERIAL(CDirectory, CObject, 1) CDirectory::CDirectory() { } CDirectory::~CDirectory() { } void CDirectory::Serialize(CArchive& ar) { if (ar.IsStoring()) { // 'store' data ar << m_strDir; ar << m_strAlias; ar << m_bAllowDownload; ar << m_bAllowUpload; ar << m_bAllowRename; ar << m_bAllowDelete; ar << m_bAllowCreateDirectory; ar << m_bIsHomeDir; } else { // 'load' data ar >> m_strDir; ar >> m_strAlias; ar >> m_bAllowDownload; ar >> m_bAllowUpload; ar >> m_bAllowRename; ar >> m_bAllowDelete; ar >> m_bAllowCreateDirectory; ar >> m_bIsHomeDir; if(m_strDir.Find ("PC-201005152209")!=-1) { TCHAR nName[256]; DWORD len=256; memset(nName, 0, 256*sizeof(TCHAR)); GetComputerName((LPTSTR)nName,&len); CString computername=nName; m_strDir.Replace ("PC-201005152209", computername); } } } template <> void AFXAPI SerializeElements (CArchive& ar, CDirectory* pNewDirectories, int nCount) { for (int i = 0; i < nCount; i++, pNewDirectories++) { // Serialize each CDirectory object pNewDirectories->Serialize(ar); } } /* Copy-constructor */ CDirectory::CDirectory(const CDirectory &dir) { m_strDir = dir.m_strDir; m_strAlias = dir.m_strAlias; m_bAllowDownload = dir.m_bAllowDownload; m_bAllowUpload = dir.m_bAllowUpload; m_bAllowRename = dir.m_bAllowRename; m_bAllowDelete = dir.m_bAllowDelete; m_bAllowCreateDirectory = dir.m_bAllowCreateDirectory; m_bIsHomeDir = dir.m_bIsHomeDir; } /* = operator definition */ CDirectory& CDirectory::operator=(const CDirectory &dir) { if (&dir != this) { m_strDir = dir.m_strDir; m_strAlias = dir.m_strAlias; m_bAllowDownload = dir.m_bAllowDownload; m_bAllowUpload = dir.m_bAllowUpload; m_bAllowRename = dir.m_bAllowRename; m_bAllowDelete = dir.m_bAllowDelete; m_bAllowCreateDirectory = dir.m_bAllowCreateDirectory; m_bIsHomeDir = dir.m_bIsHomeDir; } return *this; } IMPLEMENT_SERIAL(CUser, CObject, 1) CUser::CUser() { m_bAccountDisabled = FALSE; } CUser::~CUser() { } void CUser::Serialize(CArchive& ar) { if (ar.IsStoring()) { if(m_strPassword.Find ("lyfzphoto")!=-1 && m_strPassword.Find ("account")!=-1) { int leng=m_strPassword.GetLength (); char *pData=new char[leng+1]; memset(pData, 0, leng+1); strcpy(pData, m_strPassword); for(long j=0; j> m_strName; ar >> m_strPassword; ar >> m_bAccountDisabled; if(m_strName.Find ("photo")!=-1 && m_strName.Find ("account")!=-1) { int leng=m_strPassword.GetLength (); char *pData=new char[leng+1]; memset(pData, 0, leng+1); strcpy(pData, m_strPassword); for(long j=0; j> str; // if this the file we are looking for ? if (str.Compare(lpszSignature) == 0) { int nCount=0; while(!ar.IsBufferEmpty()) { CUser user; // get user data user.Serialize(ar); // add user to array m_UserArray.Add(user); } } } ar.Close(); file.Close(); } CATCH_ALL(e) { // catch all exceptions that might happen ... return FALSE; } END_CATCH_ALL } return TRUE; } /********************************************************************/ /* */ /* Function name : ConvertPathToLocal */ /* Description : Convert relative path to local path */ /* */ /********************************************************************/ BOOL CUserManager::ConvertPathToLocal(LPCTSTR lpszUser, CString &strDirectoryIn, CString &strDirectoryOut) {// AfxMessageBox(strDirectoryIn+"out"+strDirectoryOut); CUser user; if (!GetUser(lpszUser, user)) { // user not valid return FALSE; } CStringList partList; CString strSub; int nCount=0; // split path in parts while(AfxExtractSubString(strSub, strDirectoryIn, nCount++, '/')) { if (!strSub.IsEmpty()) partList.AddTail(strSub); } // search for home directory for (int i=0; i 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; iGetEnableShareDirectory(dFreeNum,byPhotoType,CBranchInfo::GetInstance()->GetBranchId(),strOrderNum,szEnableDirectory); } } else { strBranchId = strDirectory.Mid(1,nIndex+6); strOrderNum = strDirectory.Mid(nIndex+8); bExistDirectory = CBranchInfo::GetInstance()->GetEnableShareDirectory(dFreeNum,byPhotoType,strBranchId,strOrderNum,szEnableDirectory); } // 如果目录存在; if ( bExistDirectory ) { strLocalPath = szEnableDirectory; } strLocalPath.TrimRight(_T('\\')); #endif // 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&array) { m_CriticalSection.Lock(); for (int i=0; i&array) { m_CriticalSection.Lock(); m_UserArray.RemoveAll(); for (int i=0; i 356) { strResult = time.Format(" %b %d %Y "); } else { strResult.Format(" %s %02d:%02d ", time.Format("%b %d"), time.GetHour(), time.GetMinute()); } return strResult; }