UserManager.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002
  1. /****************************************************************/
  2. /* */
  3. /* UserManager.cpp */
  4. /* */
  5. /* Implementation of the CUserManager class. */
  6. /* */
  7. /* Programmed by LYFZ van der Meer */
  8. /* Based partially on and inspired by FileZilla Server. */
  9. /* */
  10. /* Copyright LYFZ Software Solutions 2002 */
  11. /* http://www.LYFZvandermeer.nl */
  12. /* */
  13. /* Last updated: 21 july 2002 */
  14. /* */
  15. /****************************************************************/
  16. #include "stdafx.h"
  17. #include "CPhotoFTPReceiveApp.h"
  18. #include "UserManager.h"
  19. #include "Shlwapi.h"
  20. #pragma comment(lib, "Shlwapi.lib")
  21. #include "BranchInfo.h"
  22. extern CCPhotoFTPReceiveApp theApp;
  23. IMPLEMENT_SERIAL(CDirectory, CObject, 1)
  24. CDirectory::CDirectory()
  25. {
  26. }
  27. CDirectory::~CDirectory()
  28. {
  29. }
  30. void CDirectory::Serialize(CArchive& ar)
  31. {
  32. if (ar.IsStoring())
  33. {
  34. // 'store' data
  35. ar << m_strDir;
  36. ar << m_strAlias;
  37. ar << m_bAllowDownload;
  38. ar << m_bAllowUpload;
  39. ar << m_bAllowRename;
  40. ar << m_bAllowDelete;
  41. ar << m_bAllowCreateDirectory;
  42. ar << m_bIsHomeDir;
  43. }
  44. else
  45. {
  46. // 'load' data
  47. ar >> m_strDir;
  48. ar >> m_strAlias;
  49. ar >> m_bAllowDownload;
  50. ar >> m_bAllowUpload;
  51. ar >> m_bAllowRename;
  52. ar >> m_bAllowDelete;
  53. ar >> m_bAllowCreateDirectory;
  54. ar >> m_bIsHomeDir;
  55. if(m_strDir.Find ("PC-201005152209")!=-1)
  56. {
  57. TCHAR nName[256];
  58. DWORD len=256;
  59. memset(nName, 0, 256*sizeof(TCHAR));
  60. GetComputerName((LPTSTR)nName,&len);
  61. CString computername=nName;
  62. m_strDir.Replace ("PC-201005152209", computername);
  63. }
  64. }
  65. }
  66. template <> void AFXAPI SerializeElements <CDirectory> (CArchive& ar, CDirectory* pNewDirectories, int nCount)
  67. {
  68. for (int i = 0; i < nCount; i++, pNewDirectories++)
  69. {
  70. // Serialize each CDirectory object
  71. pNewDirectories->Serialize(ar);
  72. }
  73. }
  74. /* Copy-constructor */
  75. CDirectory::CDirectory(const CDirectory &dir)
  76. {
  77. m_strDir = dir.m_strDir;
  78. m_strAlias = dir.m_strAlias;
  79. m_bAllowDownload = dir.m_bAllowDownload;
  80. m_bAllowUpload = dir.m_bAllowUpload;
  81. m_bAllowRename = dir.m_bAllowRename;
  82. m_bAllowDelete = dir.m_bAllowDelete;
  83. m_bAllowCreateDirectory = dir.m_bAllowCreateDirectory;
  84. m_bIsHomeDir = dir.m_bIsHomeDir;
  85. }
  86. /* = operator definition */
  87. CDirectory& CDirectory::operator=(const CDirectory &dir)
  88. {
  89. if (&dir != this)
  90. {
  91. m_strDir = dir.m_strDir;
  92. m_strAlias = dir.m_strAlias;
  93. m_bAllowDownload = dir.m_bAllowDownload;
  94. m_bAllowUpload = dir.m_bAllowUpload;
  95. m_bAllowRename = dir.m_bAllowRename;
  96. m_bAllowDelete = dir.m_bAllowDelete;
  97. m_bAllowCreateDirectory = dir.m_bAllowCreateDirectory;
  98. m_bIsHomeDir = dir.m_bIsHomeDir;
  99. }
  100. return *this;
  101. }
  102. IMPLEMENT_SERIAL(CUser, CObject, 1)
  103. CUser::CUser()
  104. {
  105. m_bAccountDisabled = FALSE;
  106. }
  107. CUser::~CUser()
  108. {
  109. }
  110. void CUser::Serialize(CArchive& ar)
  111. {
  112. if (ar.IsStoring())
  113. {
  114. if(m_strPassword.Find ("lyfzphoto")!=-1 && m_strPassword.Find ("account")!=-1)
  115. {
  116. int leng=m_strPassword.GetLength ();
  117. char *pData=new char[leng+1];
  118. memset(pData, 0, leng+1);
  119. strcpy(pData, m_strPassword);
  120. for(long j=0; j<leng; j++)
  121. pData[j]^=5194435218323;
  122. m_strPassword=pData;delete[]pData;
  123. }
  124. // 'store' data
  125. ar << m_strName;
  126. ar << m_strPassword;
  127. ar << m_bAccountDisabled;
  128. }
  129. else
  130. {
  131. // 'load' data
  132. ar >> m_strName;
  133. ar >> m_strPassword;
  134. ar >> m_bAccountDisabled;
  135. if(m_strName.Find ("photo")!=-1 && m_strName.Find ("account")!=-1)
  136. {
  137. int leng=m_strPassword.GetLength ();
  138. char *pData=new char[leng+1];
  139. memset(pData, 0, leng+1);
  140. strcpy(pData, m_strPassword);
  141. for(long j=0; j<leng; j++)
  142. pData[j]^=5194435218323;
  143. m_strPassword=pData;delete[]pData;
  144. }
  145. }
  146. // serialize directories
  147. m_DirectoryArray.Serialize(ar);
  148. }
  149. /* Copy-constructor */
  150. CUser::CUser(const CUser &user)
  151. {
  152. m_strName = user.m_strName;
  153. m_strPassword = user.m_strPassword;
  154. m_bAccountDisabled = user.m_bAccountDisabled;
  155. for (int i=0; i < user.m_DirectoryArray.GetSize(); i++)
  156. m_DirectoryArray.Add((CDirectory&)user.m_DirectoryArray[i]);
  157. }
  158. /* = operator definition */
  159. CUser& CUser::operator=(const CUser &user)
  160. {
  161. if (&user != this)
  162. {
  163. m_strName = user.m_strName;
  164. m_strPassword = user.m_strPassword;
  165. m_bAccountDisabled = user.m_bAccountDisabled;
  166. for (int i=0; i < user.m_DirectoryArray.GetSize(); i++)
  167. m_DirectoryArray.Add((CDirectory&)user.m_DirectoryArray[i]);
  168. }
  169. return *this;
  170. }
  171. CUserManager::CUserManager()
  172. {
  173. // GetAppDir(m_strFilename);
  174. TCHAR szDir[MAX_PATH];
  175. ::GetSystemDirectory (szDir, MAX_PATH);
  176. m_strFilename=szDir;
  177. m_strFilename += "\\lyfzftpusers.dat";
  178. if(::PathFileExists (m_strFilename)==0)
  179. {
  180. HGLOBAL hGlobal = NULL;
  181. HRSRC hSource = NULL;
  182. LPVOID lpVoid = NULL;
  183. int nSize = 0;
  184. BOOL bResult=FALSE;
  185. hSource = FindResource(NULL, MAKEINTRESOURCE(IDR_BIN1), "BIN");
  186. if(hSource == NULL)
  187. {
  188. return;
  189. }
  190. hGlobal = LoadResource(NULL, hSource);
  191. if(hGlobal == NULL)
  192. {
  193. return;
  194. }
  195. lpVoid = LockResource(hGlobal);
  196. if(lpVoid == NULL)
  197. {
  198. return;
  199. }
  200. nSize = (UINT)SizeofResource(NULL, hSource);
  201. BYTE *pData=new BYTE[nSize];
  202. memcpy(pData, (BYTE*)hGlobal, nSize);
  203. UnlockResource(hGlobal); // 16Bit Windows Needs This
  204. FreeResource(hGlobal); // 16Bit Windows Needs This (32Bit - Automatic Release)
  205. CFile fp;
  206. if(fp.Open (m_strFilename, CFile::modeCreate|CFile::modeWrite)==0)
  207. {
  208. return;
  209. }
  210. fp.Write (pData, nSize);
  211. fp.Close ();
  212. delete []pData;
  213. }
  214. }
  215. CUserManager::~CUserManager()
  216. {
  217. }
  218. /********************************************************************/
  219. /* */
  220. /* Function name : Serialize */
  221. /* Description : Call this function to store/load the user data */
  222. /* */
  223. /********************************************************************/
  224. BOOL CUserManager::Serialize(BOOL bStoring)
  225. {
  226. static const TCHAR* lpszSignature = _T("LYFZ Software Solutions - StoreObject");
  227. CFile file;
  228. if (file.Open(m_strFilename, bStoring ? CFile::modeWrite|CFile::modeCreate : CFile::modeRead))
  229. {
  230. TRY
  231. {
  232. CString str;
  233. CArchive ar(&file, bStoring ? CArchive::store : CArchive::load);
  234. if (bStoring)
  235. {
  236. // save signature
  237. ar << CString(lpszSignature);
  238. // Save the changed user details
  239. for (int i=0; i < m_UserArray.GetSize(); i++)
  240. {
  241. m_UserArray[i].Serialize(ar);
  242. }
  243. ar.Flush();
  244. }
  245. else
  246. {
  247. // load signature
  248. ar >> str;
  249. // if this the file we are looking for ?
  250. if (str.Compare(lpszSignature) == 0)
  251. {
  252. int nCount=0;
  253. while(!ar.IsBufferEmpty())
  254. {
  255. CUser user;
  256. // get user data
  257. user.Serialize(ar);
  258. // add user to array
  259. m_UserArray.Add(user);
  260. }
  261. }
  262. }
  263. ar.Close();
  264. file.Close();
  265. }
  266. CATCH_ALL(e)
  267. {
  268. // catch all exceptions that might happen ...
  269. return FALSE;
  270. }
  271. END_CATCH_ALL
  272. }
  273. return TRUE;
  274. }
  275. /********************************************************************/
  276. /* */
  277. /* Function name : ConvertPathToLocal */
  278. /* Description : Convert relative path to local path */
  279. /* */
  280. /********************************************************************/
  281. BOOL CUserManager::ConvertPathToLocal(LPCTSTR lpszUser, CString &strDirectoryIn, CString &strDirectoryOut)
  282. {// AfxMessageBox(strDirectoryIn+"out"+strDirectoryOut);
  283. CUser user;
  284. if (!GetUser(lpszUser, user))
  285. {
  286. // user not valid
  287. return FALSE;
  288. }
  289. CStringList partList;
  290. CString strSub;
  291. int nCount=0;
  292. // split path in parts
  293. while(AfxExtractSubString(strSub, strDirectoryIn, nCount++, '/'))
  294. {
  295. if (!strSub.IsEmpty())
  296. partList.AddTail(strSub);
  297. }
  298. // search for home directory
  299. for (int i=0; i<user.m_DirectoryArray.GetSize(); i++)
  300. {
  301. if (user.m_DirectoryArray[i].m_bIsHomeDir)
  302. {
  303. CString strHomeDir = user.m_DirectoryArray[i].m_strDir;
  304. while(!partList.IsEmpty())
  305. {
  306. CString strPart = partList.GetHead();
  307. partList.RemoveHead();
  308. CString strCheckDir;
  309. if (strPart == "..")
  310. {
  311. // go back one level
  312. int nPos = strHomeDir.ReverseFind('\\');
  313. if (nPos != -1)
  314. {
  315. strCheckDir = strHomeDir.Left(nPos);
  316. }
  317. }
  318. else
  319. {
  320. strCheckDir = strHomeDir + "\\" + strPart;
  321. }
  322. // does directory exist ?
  323. if (FileExists(strCheckDir, TRUE))
  324. {
  325. strHomeDir = strCheckDir;
  326. }
  327. else
  328. // does file exist ?
  329. if (FileExists(strCheckDir, FALSE))
  330. {
  331. strHomeDir = strCheckDir;
  332. }
  333. else
  334. {
  335. BOOL bFound = FALSE;
  336. // virtual directories exist only in the root
  337. if (strHomeDir == user.m_DirectoryArray[i].m_strDir)
  338. {
  339. // maybe it's a virtual directory
  340. for (int j=0; j<user.m_DirectoryArray.GetSize(); j++)
  341. {
  342. if (i != j && (user.m_DirectoryArray[j].m_strAlias.CompareNoCase(strPart) == 0))
  343. {
  344. bFound = TRUE;
  345. strHomeDir = user.m_DirectoryArray[j].m_strDir;
  346. break;
  347. }
  348. }
  349. }
  350. if (!bFound)
  351. {
  352. // directory not found
  353. return FALSE;
  354. }
  355. }
  356. }
  357. // successfully converted directory
  358. strDirectoryOut = strHomeDir;// AfxMessageBox(strDirectoryIn+"out"+strDirectoryOut);
  359. return TRUE;
  360. }
  361. }
  362. // no home directory found
  363. return FALSE;
  364. }
  365. /********************************************************************/
  366. /* */
  367. /* Function name : CheckAccessRights */
  368. /* Description : Check if user has access to specified directory. */
  369. /* */
  370. /********************************************************************/
  371. BOOL CUserManager::CheckAccessRights(LPCTSTR lpszUser, LPCTSTR lpszDirectory, int nOption)
  372. {
  373. CUser user;
  374. if (!GetUser(lpszUser, user))
  375. {
  376. // user not valid
  377. return FALSE;
  378. }
  379. // start with full path
  380. CString strCheckDir = lpszDirectory;
  381. while(!strCheckDir.IsEmpty())
  382. {
  383. // search for a matching part
  384. for (int i=0; i<user.m_DirectoryArray.GetSize(); i++)
  385. {
  386. CString strPath1 = strCheckDir;
  387. strPath1.TrimRight("\\");
  388. CString strPath2 = user.m_DirectoryArray[i].m_strDir;
  389. //////////////////////////////////////////////////////////////////////////
  390. // 禁用账号本身目录,使用NetShareInfo表里的目录; 2015-05-16
  391. #if 1
  392. strPath2 = strCheckDir;
  393. #endif
  394. strPath2.TrimRight("\\");
  395. // found a match ?
  396. if (strPath1.CompareNoCase(strPath2) == 0)
  397. {
  398. // check file access rights
  399. if (((!user.m_DirectoryArray[i].m_bAllowDownload) && (nOption == FTP_DOWNLOAD)) ||
  400. ((!user.m_DirectoryArray[i].m_bAllowUpload) && (nOption == FTP_UPLOAD)) ||
  401. ((!user.m_DirectoryArray[i].m_bAllowRename) && (nOption == FTP_RENAME)) ||
  402. ((!user.m_DirectoryArray[i].m_bAllowDelete) && (nOption == FTP_DELETE)) ||
  403. ((!user.m_DirectoryArray[i].m_bAllowCreateDirectory) && (nOption == FTP_CREATE_DIR)))
  404. {
  405. return FALSE;
  406. }
  407. return TRUE;
  408. }
  409. }
  410. int nPos = strCheckDir.ReverseFind('\\');
  411. if (nPos != -1)
  412. {
  413. // strip subdir
  414. strCheckDir = strCheckDir.Left(nPos);
  415. }
  416. else
  417. {
  418. // we're done
  419. strCheckDir.Empty();
  420. }
  421. }
  422. // users has no rights to this directory
  423. return FALSE;
  424. }
  425. /********************************************************************/
  426. /* */
  427. /* Function name : ChangeDirectory */
  428. /* Description : Change to specified directory */
  429. /* */
  430. /********************************************************************/
  431. int CUserManager::ChangeDirectory(LPCTSTR lpszUser, CString &strCurrentdir, CString &strChangeTo)
  432. {
  433. // make unix style
  434. strChangeTo.Replace("\\","/");
  435. while(strChangeTo.Replace("//","/"));
  436. strChangeTo.TrimRight("/");
  437. // now looks something like this:
  438. // "" = root
  439. // "/mydir/apps" = absolute path
  440. // "mydir/apps" = relative path
  441. if (strChangeTo == "")
  442. {
  443. // goto root
  444. strChangeTo = "/";
  445. }
  446. else
  447. {
  448. // first character '/' ?
  449. if (strChangeTo.Left(1) != "/")
  450. {
  451. // client specified a path relative to their current path
  452. strCurrentdir.TrimRight("/");
  453. strChangeTo = strCurrentdir + "/" + strChangeTo;
  454. }
  455. }
  456. // goto parent directory
  457. if (strChangeTo.Right(2) == "..")
  458. {
  459. strChangeTo.TrimRight("..");
  460. strChangeTo.TrimRight("/");
  461. int nPos = strChangeTo.ReverseFind('/');
  462. if (nPos != -1)
  463. {
  464. strChangeTo = strChangeTo.Left(nPos);
  465. }
  466. if (strChangeTo == "")
  467. {
  468. // goto root
  469. strChangeTo = "/";
  470. }
  471. }
  472. // get local path
  473. CString strLocalPath;
  474. if (!ConvertPathToLocal(lpszUser, strChangeTo, strLocalPath))
  475. {
  476. // unable to convert to local path
  477. return 2;
  478. }
  479. // check if user has access right for this directory
  480. if (!CheckAccessRights(lpszUser, strLocalPath, FTP_DOWNLOAD))
  481. {
  482. // user has no permissions
  483. return 1;
  484. }
  485. // everything went successfully
  486. strCurrentdir = strChangeTo;
  487. return 0;
  488. }
  489. /********************************************************************/
  490. /* */
  491. /* Function name : GetDirectoryList */
  492. /* Description : Get directory listing for specfied directory */
  493. /* */
  494. /********************************************************************/
  495. int CUserManager::GetDirectoryList(LPCTSTR lpszUser, LPCTSTR lpszDirectory, CString &strResult)
  496. {
  497. CString strDirectory = lpszDirectory;
  498. // AfxMessageBox("dddddd");
  499. // make unix style
  500. strDirectory.Replace("\\","/");
  501. while(strDirectory.Replace("//","/"));
  502. // clear list
  503. strResult = "";
  504. CUser user;
  505. if (!GetUser(lpszUser, user))
  506. {
  507. // user not found -> no permissions
  508. return 1;
  509. }
  510. CString strLocalPath;
  511. if (!ConvertPathToLocal(lpszUser, strDirectory, strLocalPath))
  512. {
  513. // unable to convert to local path
  514. return 2;
  515. }
  516. // check if user has access right for this directory
  517. if (!CheckAccessRights(lpszUser, strLocalPath, FTP_DOWNLOAD))
  518. {
  519. // user has no permissions, to display this directory
  520. return 1;
  521. }
  522. CFileFind find;
  523. BOOL bFound = FALSE;
  524. // check if it's a directory
  525. if ((GetFileAttributes(strLocalPath) & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY)
  526. {
  527. bFound = find.FindFile(strLocalPath + "\\*.*");
  528. }
  529. else
  530. {
  531. // it's a file
  532. bFound = find.FindFile(strLocalPath);
  533. }
  534. while (bFound)
  535. {
  536. bFound = find.FindNextFile();
  537. // skip "." and ".."
  538. if (find.IsDots())
  539. continue;
  540. // permissions
  541. if (find.IsDirectory())
  542. strResult += "drwx------";
  543. else
  544. strResult += "-rwx------";
  545. // groups
  546. strResult += " 1 user group ";
  547. // file size
  548. CString strLength;
  549. strLength.Format("%d", find.GetLength());
  550. CString strFiller = " ";
  551. strResult += strFiller.Left(strFiller.GetLength() - strLength.GetLength());
  552. strResult += strLength;
  553. // file date
  554. strResult += GetFileDate(find);
  555. // file name
  556. strResult += find.GetFileName();
  557. // end of line
  558. strResult += "\r\n";
  559. }
  560. // if it's the root dir also show virtual directories
  561. for (int i=0; i<user.m_DirectoryArray.GetSize(); i++)
  562. {
  563. if (user.m_DirectoryArray.GetAt(i).m_bIsHomeDir)
  564. {
  565. CString strPath = user.m_DirectoryArray.GetAt(i).m_strDir;
  566. strPath.TrimRight('\\');
  567. if (strLocalPath.CompareNoCase(strPath) == 0)
  568. {
  569. for (int j=0; j<user.m_DirectoryArray.GetSize(); j++)
  570. {
  571. if (i != j && user.m_DirectoryArray.GetAt(j).m_strAlias != "")
  572. {
  573. if (find.FindFile(user.m_DirectoryArray.GetAt(j).m_strDir))
  574. {
  575. find.FindNextFile();
  576. strResult += "drwx------";
  577. // groups
  578. strResult += " 1 user group ";
  579. strResult += " 0";
  580. // file date
  581. strResult += GetFileDate(find);
  582. // file name
  583. strResult += user.m_DirectoryArray.GetAt(j).m_strAlias;
  584. // end of line
  585. strResult += "\r\n";
  586. }
  587. }
  588. }
  589. }
  590. break;
  591. }
  592. }
  593. return 0;
  594. }
  595. /********************************************************************/
  596. /* */
  597. /* Function name : FileExists */
  598. /* Description : Check if file or directory exists */
  599. /* */
  600. /********************************************************************/
  601. BOOL CUserManager::FileExists(LPCTSTR lpszFileName, BOOL bIsDirCheck)
  602. {
  603. // A quick'n'easy way to see if a file exists.
  604. DWORD dwAttributes = GetFileAttributes(lpszFileName);
  605. if (dwAttributes == 0xFFFFFFFF)
  606. return FALSE;
  607. if ((dwAttributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY)
  608. {
  609. if (bIsDirCheck)
  610. return TRUE;
  611. else
  612. return FALSE;
  613. }
  614. else
  615. {
  616. if (!bIsDirCheck)
  617. return TRUE;
  618. else
  619. return FALSE;
  620. }
  621. }
  622. /********************************************************************/
  623. /* */
  624. /* Function name : CheckFileName */
  625. /* Description : Check if filename is valid and if user has */
  626. /* access rights. */
  627. /* */
  628. /********************************************************************/
  629. int CUserManager::CheckFileName(LPCTSTR lpszUser, CString strFilename, CString strCurrentdir, int nOption, CString &strResult)
  630. {
  631. // make unix style
  632. strFilename.Replace("\\", "/");
  633. while(strFilename.Replace("//", "/"));
  634. strFilename.TrimRight("/");
  635. if (strFilename == "")
  636. {
  637. // no file name
  638. return 2;
  639. }
  640. // append filename to directory
  641. CString strDirectory = strCurrentdir;
  642. // client has specified complete path
  643. int nPos = strFilename.ReverseFind('/');
  644. if (nPos != -1)
  645. {
  646. strDirectory = strFilename.Left(nPos);
  647. if (strDirectory == "")
  648. strDirectory = "/";
  649. strFilename = strFilename.Mid(nPos+1);
  650. }
  651. // get local path
  652. CString strLocalPath;
  653. if (!ConvertPathToLocal(lpszUser, strDirectory, strLocalPath))
  654. {
  655. // directory does not exist
  656. return 2;
  657. }
  658. //////////////////////////////////////////////////////////////////////////
  659. // 替换掉strLocalPath;
  660. #if 1
  661. // 下载相片的目录由NetShareInfo决定;
  662. BYTE byPhotoType = 0;
  663. if ( _tcscmp(lpszUser,_T("photo1account")) == 0 )
  664. {
  665. byPhotoType = 1; // 客户原片;
  666. }
  667. else if ( _tcscmp(lpszUser,_T("photo2account")) == 0 )
  668. {
  669. byPhotoType = 2; // 初修片;
  670. }
  671. else if ( _tcscmp(lpszUser,_T("photo3account")) == 0 )
  672. {
  673. byPhotoType = 3; // 精修片;
  674. }
  675. else if ( _tcscmp(lpszUser,_T("photo4account")) == 0 )
  676. {
  677. byPhotoType = 4; // 设计片;
  678. }
  679. BOOL bExistDirectory = FALSE;
  680. double dFreeNum;
  681. double dTotalNum;
  682. CString strBranchId = _T("");
  683. CString strOrderNum = _T("");
  684. TCHAR szEnableDirectory[MAX_PATH] = _T("");
  685. int nIndex = -1;
  686. nIndex = strDirectory.Find(_T(".ly.com"));
  687. if ( nIndex == -1 )
  688. {
  689. if(-1 != strDirectory.Find(_T("."))) // test.dat之类的文件;
  690. {
  691. strOrderNum = strDirectory.Mid(1);
  692. bExistDirectory = CBranchInfo::GetInstance()->GetEnableShareDirectory(dFreeNum,byPhotoType,CBranchInfo::GetInstance()->GetBranchId(),strOrderNum,szEnableDirectory);
  693. }
  694. }
  695. else
  696. {
  697. strBranchId = strDirectory.Mid(1,nIndex+6);
  698. strOrderNum = strDirectory.Mid(nIndex+8);
  699. bExistDirectory = CBranchInfo::GetInstance()->GetEnableShareDirectory(dFreeNum,byPhotoType,strBranchId,strOrderNum,szEnableDirectory);
  700. }
  701. // 如果目录存在;
  702. if ( bExistDirectory )
  703. {
  704. strLocalPath = szEnableDirectory;
  705. }
  706. strLocalPath.TrimRight(_T('\\'));
  707. #endif
  708. // create the complete path
  709. strResult = strLocalPath + "\\" + strFilename;
  710. if ((nOption != FTP_UPLOAD) && !FileExists(strResult, FALSE))
  711. {
  712. // file does not exist
  713. return 2;
  714. }
  715. // return relative path
  716. if (nOption == FTP_LIST)
  717. {
  718. strResult = strCurrentdir;
  719. strResult.TrimRight('/');
  720. strResult += "/" + strFilename;
  721. return 0;
  722. }
  723. // check file access rights
  724. if (!CheckAccessRights(lpszUser, strLocalPath, nOption))
  725. {
  726. // user has no permissions, to execute specified action
  727. return 1;
  728. }
  729. // everything is ok
  730. return 0;
  731. }
  732. /********************************************************************/
  733. /* */
  734. /* Function name : CheckDirectory */
  735. /* Description : Check if directory exists and if user has */
  736. /* appropriate permissions. */
  737. /* */
  738. /********************************************************************/
  739. int CUserManager::CheckDirectory(LPCTSTR lpszUser, CString strDirectory, CString strCurrentdir, int nOption, CString &strResult)
  740. {
  741. // make unix compatible
  742. strDirectory.Replace("\\","/");
  743. while(strDirectory.Replace("//","/"));
  744. strDirectory.TrimRight("/");
  745. if (strDirectory == "")
  746. {
  747. // no directory
  748. return 2;
  749. }
  750. else
  751. {
  752. // first character '/' ?
  753. if (strDirectory.Left(1) != "/")
  754. {
  755. // client specified a path relative to their current path
  756. strCurrentdir.TrimRight("/");
  757. strDirectory = strCurrentdir + "/" + strDirectory;
  758. }
  759. }
  760. // split part into 2 parts
  761. int nPos = strDirectory.ReverseFind('/');
  762. if (nPos == -1)
  763. return 2;
  764. // get left part of directory
  765. CString strNode = strDirectory.Left(nPos);
  766. // root ?
  767. if (strNode == "")
  768. strNode = "/";
  769. // get right part of directory
  770. strDirectory = strDirectory.Mid(nPos+1);
  771. CString strLocalPath;
  772. do
  773. {
  774. // does parent directory exist ?
  775. if ((!ConvertPathToLocal(lpszUser, strNode, strLocalPath)) && (nOption == FTP_CREATE_DIR))
  776. {
  777. // directory could not be found, maybe one level higher
  778. int nPos = strNode.ReverseFind('/');
  779. // no more levels
  780. if (nPos == -1)
  781. return 2;
  782. strDirectory = strNode.Mid(nPos+1) + "/" + strDirectory;
  783. strNode = strNode.Left(nPos);
  784. continue;
  785. }
  786. // check directory access rights
  787. if (!CheckAccessRights(lpszUser, strLocalPath, nOption))
  788. {
  789. // user has no permissions, to execute specified action
  790. return 1;
  791. }
  792. strNode = strLocalPath;
  793. break;
  794. }
  795. while (strNode != "/");
  796. strDirectory.Replace("/","\\");
  797. strResult = strNode + "\\" + strDirectory;
  798. // check if directory exists
  799. if (!FileExists(strResult))
  800. return 2;
  801. // function successfull
  802. return 0;
  803. }
  804. /********************************************************************/
  805. /* */
  806. /* Function name : GetUser */
  807. /* Description : Get user object for specified username */
  808. /* */
  809. /********************************************************************/
  810. BOOL CUserManager::GetUser(LPCTSTR lpszUser, CUser &user)
  811. {
  812. m_CriticalSection.Lock();
  813. for (int i=0; i<m_UserArray.GetSize(); i++)
  814. {
  815. if (!m_UserArray[i].m_strName.CompareNoCase(lpszUser))
  816. {
  817. user = m_UserArray[i];
  818. m_CriticalSection.Unlock();
  819. return TRUE;
  820. }
  821. }
  822. m_CriticalSection.Unlock();
  823. return FALSE;
  824. }
  825. /********************************************************************/
  826. /* */
  827. /* Function name : GetUserList */
  828. /* Description : Make copy of user array */
  829. /* */
  830. /********************************************************************/
  831. void CUserManager::GetUserList(CArray<CUser, CUser&>&array)
  832. {
  833. m_CriticalSection.Lock();
  834. for (int i=0; i<m_UserArray.GetSize();i++)
  835. {
  836. array.Add(m_UserArray[i]);
  837. }
  838. m_CriticalSection.Unlock();
  839. }
  840. /********************************************************************/
  841. /* */
  842. /* Function name : UpdateUserList */
  843. /* Description : Update user array */
  844. /* */
  845. /********************************************************************/
  846. void CUserManager::UpdateUserList(CArray<CUser, CUser&>&array)
  847. {
  848. m_CriticalSection.Lock();
  849. m_UserArray.RemoveAll();
  850. for (int i=0; i<array.GetSize();i++)
  851. {
  852. m_UserArray.Add(array[i]);
  853. }
  854. m_CriticalSection.Unlock();
  855. Serialize(TRUE);
  856. }
  857. /********************************************************************/
  858. /* */
  859. /* Function name : GetFileDate */
  860. /* Description : return file date in unix style */
  861. /* */
  862. /********************************************************************/
  863. CString CUserManager::GetFileDate(CFileFind &find)
  864. {
  865. CString strResult;
  866. CTime time = CTime::GetCurrentTime();
  867. find.GetLastWriteTime(time);
  868. CTimeSpan timeSpan = CTime::GetCurrentTime() - time;
  869. if (timeSpan.GetDays() > 356)
  870. {
  871. strResult = time.Format(" %b %d %Y ");
  872. }
  873. else
  874. {
  875. strResult.Format(" %s %02d:%02d ", time.Format("%b %d"), time.GetHour(), time.GetMinute());
  876. }
  877. return strResult;
  878. }