123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044 |
- #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())
- {
-
- 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
- {
-
- 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 <CDirectory> (CArchive& ar, CDirectory* pNewDirectories, int nCount)
- {
- for (int i = 0; i < nCount; i++, pNewDirectories++)
- {
-
- pNewDirectories->Serialize(ar);
- }
- }
- 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;
- }
- 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<leng; j++)
- pData[j]^=5194435218323;
- m_strPassword=pData;delete[]pData;
- }
-
- ar << m_strName;
- ar << m_strPassword;
- ar << m_bAccountDisabled;
- }
- else
- {
-
- ar >> 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<leng; j++)
- pData[j]^=5194435218323;
- m_strPassword=pData;delete[]pData;
- }
- }
-
- m_DirectoryArray.Serialize(ar);
- }
- CUser::CUser(const CUser &user)
- {
- m_strName = user.m_strName;
- m_strPassword = user.m_strPassword;
- m_bAccountDisabled = user.m_bAccountDisabled;
- for (int i=0; i < user.m_DirectoryArray.GetSize(); i++)
- m_DirectoryArray.Add((CDirectory&)user.m_DirectoryArray[i]);
- }
- CUser& CUser::operator=(const CUser &user)
- {
- if (&user != this)
- {
- m_strName = user.m_strName;
- m_strPassword = user.m_strPassword;
- m_bAccountDisabled = user.m_bAccountDisabled;
- for (int i=0; i < user.m_DirectoryArray.GetSize(); i++)
- m_DirectoryArray.Add((CDirectory&)user.m_DirectoryArray[i]);
- }
- return *this;
- }
- CUserManager::CUserManager()
- {
-
- TCHAR szDir[MAX_PATH];
- ::GetSystemDirectory (szDir, MAX_PATH);
- m_strFilename=szDir;
- m_strFilename += "\\lyfzftpusers.dat";
- if(::PathFileExists (m_strFilename)==0)
- {
- HGLOBAL hGlobal = NULL;
- HRSRC hSource = NULL;
- LPVOID lpVoid = NULL;
- int nSize = 0;
- BOOL bResult=FALSE;
- hSource = FindResource(NULL, MAKEINTRESOURCE(IDR_BIN1), "BIN");
- if(hSource == NULL)
- {
- return;
- }
- hGlobal = LoadResource(NULL, hSource);
- if(hGlobal == NULL)
- {
- return;
- }
- lpVoid = LockResource(hGlobal);
- if(lpVoid == NULL)
- {
- return;
- }
- nSize = (UINT)SizeofResource(NULL, hSource);
- BYTE *pData=new BYTE[nSize];
- memcpy(pData, (BYTE*)hGlobal, nSize);
- UnlockResource(hGlobal);
- FreeResource(hGlobal);
- CFile fp;
- if(fp.Open (m_strFilename, CFile::modeCreate|CFile::modeWrite)==0)
- {
- return;
- }
- fp.Write (pData, nSize);
- fp.Close ();
- delete []pData;
- }
- }
- CUserManager::~CUserManager()
- {
- }
-
- BOOL CUserManager::Serialize(BOOL bStoring)
- {
- static const TCHAR* lpszSignature = _T("LYFZ Software Solutions - StoreObject");
- CFile file;
- if (file.Open(m_strFilename, bStoring ? CFile::modeWrite|CFile::modeCreate : CFile::modeRead))
- {
- TRY
- {
- CString str;
- CArchive ar(&file, bStoring ? CArchive::store : CArchive::load);
- if (bStoring)
- {
-
- ar << CString(lpszSignature);
-
- for (int i=0; i < m_UserArray.GetSize(); i++)
- {
- m_UserArray[i].Serialize(ar);
- }
- ar.Flush();
- }
- else
- {
-
- ar >> str;
-
- if (str.Compare(lpszSignature) == 0)
- {
- int nCount=0;
- while(!ar.IsBufferEmpty())
- {
- CUser user;
-
- user.Serialize(ar);
-
- m_UserArray.Add(user);
- }
- }
- }
- ar.Close();
- file.Close();
- }
- CATCH_ALL(e)
- {
-
- return FALSE;
- }
- END_CATCH_ALL
- }
- return TRUE;
- }
-
- BOOL CUserManager::ConvertPathToLocal(LPCTSTR lpszUser, CString &strDirectoryIn, CString &strDirectoryOut)
- {
- CUser user;
- if (!GetUser(lpszUser, user))
- {
-
- return FALSE;
- }
- CStringList partList;
- CString strSub = _T("");
- int nCount=0;
-
- while(AfxExtractSubString(strSub, strDirectoryIn, nCount++, '/'))
- {
- if (!strSub.IsEmpty())
- partList.AddTail(strSub);
- }
-
- for (int i=0; i<user.m_DirectoryArray.GetSize(); i++)
- {
- if (user.m_DirectoryArray[i].m_bIsHomeDir)
- {
- CString strHomeDir = user.m_DirectoryArray[i].m_strDir;
- while(!partList.IsEmpty())
- {
- CString strPart = partList.GetHead();
- partList.RemoveHead();
- CString strCheckDir = _T("");
- if (strPart == "..")
- {
-
- int nPos = strHomeDir.ReverseFind('\\');
- if (nPos != -1)
- strCheckDir = strHomeDir.Left(nPos);
- }
- else
- {
- strCheckDir = strHomeDir + "\\" + strPart;
- }
-
- if (FileExists(strCheckDir, TRUE))
- {
- strHomeDir = strCheckDir;
- }
- else
- {
-
- if (FileExists(strCheckDir, FALSE))
- {
- strHomeDir = strCheckDir;
- }
- else
- {
- BOOL bFound = FALSE;
-
- if (strHomeDir == user.m_DirectoryArray[i].m_strDir)
- {
-
- for (int j=0; j<user.m_DirectoryArray.GetSize(); j++)
- {
- if (i != j && (user.m_DirectoryArray[j].m_strAlias.CompareNoCase(strPart) == 0))
- {
- bFound = TRUE;
- strHomeDir = user.m_DirectoryArray[j].m_strDir;
- break;
- }
- }
- }
- if (!bFound)
- {
-
- return FALSE;
- }
- }
- }
- }
-
- strDirectoryOut = strHomeDir;
- return TRUE;
- }
- }
-
- return FALSE;
- }
-
- BOOL CUserManager::CheckAccessRights(LPCTSTR lpszUser, LPCTSTR lpszDirectory, int nOption)
- {
- CUser user;
- if (!GetUser(lpszUser, user))
- {
-
- return FALSE;
- }
-
- CString strCheckDir = lpszDirectory;
- while(!strCheckDir.IsEmpty())
- {
-
- for (int i=0; i<user.m_DirectoryArray.GetSize(); i++)
- {
- CString strPath1 = strCheckDir;
- strPath1.TrimRight("\\");
- CString strPath2 = user.m_DirectoryArray[i].m_strDir;
-
-
- #if 1
- strPath2 = strCheckDir;
- #endif
- strPath2.TrimRight("\\");
-
- if (strPath1.CompareNoCase(strPath2) == 0)
- {
-
- if (((!user.m_DirectoryArray[i].m_bAllowDownload) && (nOption == FTP_DOWNLOAD)) ||
- ((!user.m_DirectoryArray[i].m_bAllowUpload) && (nOption == FTP_UPLOAD)) ||
- ((!user.m_DirectoryArray[i].m_bAllowRename) && (nOption == FTP_RENAME)) ||
- ((!user.m_DirectoryArray[i].m_bAllowDelete) && (nOption == FTP_DELETE)) ||
- ((!user.m_DirectoryArray[i].m_bAllowCreateDirectory) && (nOption == FTP_CREATE_DIR)))
- {
- return FALSE;
- }
- return TRUE;
- }
- }
- int nPos = strCheckDir.ReverseFind('\\');
- if (nPos != -1)
- {
-
- strCheckDir = strCheckDir.Left(nPos);
- }
- else
- {
-
- strCheckDir.Empty();
- }
- }
-
- return FALSE;
- }
-
- int CUserManager::ChangeDirectory(LPCTSTR lpszUser, CString &strCurrentdir, CString &strChangeTo)
- {
-
- strChangeTo.Replace("\\","/");
- while(strChangeTo.Replace("//","/"));
- strChangeTo.TrimRight("/");
-
-
-
-
- if (strChangeTo == "")
- {
-
- strChangeTo = "/";
- }
- else
- {
-
- if (strChangeTo.Left(1) != "/")
- {
-
- strCurrentdir.TrimRight("/");
- strChangeTo = strCurrentdir + "/" + strChangeTo;
- }
- }
-
- if (strChangeTo.Right(2) == "..")
- {
- strChangeTo.TrimRight("..");
- strChangeTo.TrimRight("/");
- int nPos = strChangeTo.ReverseFind('/');
- if (nPos != -1)
- strChangeTo = strChangeTo.Left(nPos);
-
-
- if (strChangeTo == "")
- strChangeTo = "/";
- }
-
- CString strLocalPath;
- if (!ConvertPathToLocal(lpszUser, strChangeTo, strLocalPath))
- {
-
- return 2;
- }
-
- if (!CheckAccessRights(lpszUser, strLocalPath, FTP_DOWNLOAD))
- {
-
- return 1;
- }
-
- strCurrentdir = strChangeTo;
- return 0;
- }
-
- int CUserManager::GetDirectoryList(LPCTSTR lpszUser, LPCTSTR lpszDirectory, CString &strResult)
- {
- CString strDirectory = lpszDirectory;
-
-
- strDirectory.Replace("\\","/");
- while(strDirectory.Replace("//","/"));
-
- strResult = "";
- CUser user;
- if (!GetUser(lpszUser, user))
- {
-
- return 1;
- }
- CString strLocalPath;
- if (!ConvertPathToLocal(lpszUser, strDirectory, strLocalPath))
- {
-
- return 2;
- }
-
- if (!CheckAccessRights(lpszUser, strLocalPath, FTP_DOWNLOAD))
- {
-
- return 1;
- }
- CFileFind find;
- BOOL bFound = FALSE;
-
- if ((GetFileAttributes(strLocalPath) & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY)
- {
- bFound = find.FindFile(strLocalPath + "\\*.*");
- }
- else
- {
-
- bFound = find.FindFile(strLocalPath);
- }
- while (bFound)
- {
- bFound = find.FindNextFile();
-
- if (find.IsDots())
- continue;
-
- if (find.IsDirectory())
- strResult += "drwx------";
- else
- strResult += "-rwx------";
-
- strResult += " 1 user group ";
-
- CString strLength;
- strLength.Format("%d", find.GetLength());
- CString strFiller = " ";
- strResult += strFiller.Left(strFiller.GetLength() - strLength.GetLength());
- strResult += strLength;
-
- strResult += GetFileDate(find);
-
- strResult += find.GetFileName();
-
- strResult += "\r\n";
- }
-
- 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------";
-
- strResult += " 1 user group ";
- strResult += " 0";
-
- strResult += GetFileDate(find);
-
- strResult += user.m_DirectoryArray.GetAt(j).m_strAlias;
-
- strResult += "\r\n";
- }
- }
- }
- }
- break;
- }
- }
- return 0;
- }
-
- BOOL CUserManager::FileExists(LPCTSTR lpszFileName, const int bIsDirCheck)
- {
-
- DWORD dwAttributes = GetFileAttributes(lpszFileName);
- if (dwAttributes == 0xFFFFFFFF)
- return FALSE;
-
- if ((dwAttributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY)
- {
- if (bIsDirCheck == 1)
- return TRUE;
- }
- else
- {
- if (bIsDirCheck == 0)
- return TRUE;
- }
- return FALSE;
- }
-
- int CUserManager::CheckFileName(LPCTSTR lpszUser, CString strFilename, CString strCurrentdir, int nOption, CString &strResult)
- {
-
- strFilename.Replace("\\", "/");
- while(strFilename.Replace("//", "/"));
- strFilename.TrimRight("/");
- if (strFilename == "")
- {
-
- return 2;
- }
-
- CString strDirectory = strCurrentdir;
-
- int nPos = strFilename.ReverseFind('/');
- if (nPos != -1)
- {
- strDirectory = strFilename.Left(nPos);
- if (strDirectory == "")
- strDirectory = "/";
- strFilename = strFilename.Mid(nPos+1);
- }
-
- CString strLocalPath = _T("");
- if (!ConvertPathToLocal(lpszUser, strDirectory, strLocalPath))
- {
-
- return 2;
- }
-
- #if 1
-
- BYTE byPhotoType = 0;
- if ( _tcscmp(lpszUser,_T("photo1account")) == 0 )
- {
- byPhotoType = 1;
- }
- else if ( _tcscmp(lpszUser,_T("photo2account")) == 0 )
- {
- byPhotoType = 2;
- }
- else if ( _tcscmp(lpszUser,_T("photo3account")) == 0 )
- {
- byPhotoType = 3;
- }
- else if ( _tcscmp(lpszUser,_T("photo4account")) == 0 )
- {
- byPhotoType = 4;
- }
- BOOL bExistDirectory = FALSE;
- double dFreeNum;
- double dTotalNum;
- CString strBranchId = _T("");
- CString strOrderNum = _T("");
- TCHAR szEnableDirectory[MAX_PATH] = _T("");
- int nIndex = -1;
- nIndex = strDirectory.Find(_T(".ly.com"));
- if ( nIndex == -1 )
- {
- if(-1 != strDirectory.Find(_T(".")))
- {
- strOrderNum = strDirectory.Mid(1);
- bExistDirectory = CBranchInfo::GetInstance()->GetEnableShareDirectory(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
-
- strResult = strLocalPath + "\\" + strFilename;
- if ((nOption != FTP_UPLOAD) && !FileExists(strResult, FALSE))
- {
-
- return 2;
- }
-
- if (nOption == FTP_LIST)
- {
- strResult = strCurrentdir;
- strResult.TrimRight('/');
- strResult += "/" + strFilename;
- return 0;
- }
-
- if (!CheckAccessRights(lpszUser, strLocalPath, nOption))
- {
-
- return 1;
- }
-
- return 0;
- }
-
- int CUserManager::CheckDirectory(LPCTSTR lpszUser, CString strDirectory, CString strCurrentdir, int nOption, CString &strResult)
- {
-
- strDirectory.Replace("\\","/");
- while(strDirectory.Replace("//","/"));
- strDirectory.TrimRight("/");
- if (strDirectory == "")
- {
-
- return 2;
- }
- else
- {
-
- if (strDirectory.Left(1) != "/")
- {
-
- strCurrentdir.TrimRight("/");
- strDirectory = strCurrentdir + "/" + strDirectory;
- }
- }
-
- int nPos = strDirectory.ReverseFind('/');
- if (nPos == -1)
- return 2;
-
- CString strNode = strDirectory.Left(nPos);
-
- if (strNode == "")
- strNode = "/";
-
- strDirectory = strDirectory.Mid(nPos+1);
- CString strLocalPath;
- do
- {
-
- if ((!ConvertPathToLocal(lpszUser, strNode, strLocalPath)) && (nOption == FTP_CREATE_DIR))
- {
-
- int nPos = strNode.ReverseFind('/');
-
- if (nPos == -1)
- return 2;
- strDirectory = strNode.Mid(nPos+1) + "/" + strDirectory;
- strNode = strNode.Left(nPos);
- continue;
- }
-
- if (!CheckAccessRights(lpszUser, strLocalPath, nOption))
- {
-
- return 1;
- }
- strNode = strLocalPath;
- break;
- }
- while (strNode != "/");
- strDirectory.Replace("/","\\");
- strResult = strNode + "\\" + strDirectory;
-
- if (!FileExists(strResult))
- return 2;
-
- return 0;
- }
-
- 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;
- }
-
- 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();
- }
-
- 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);
- }
-
- 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;
- }
|