UserManager.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  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. 函数名 : Serialize
  221. 描述 : 存储/加载用户数据
  222. 参数 :
  223. BOOL bStoring 是否存储TRUE=存储,FALSE=加载
  224. 返回 : TRUE 成功, FALSE失败
  225. */
  226. /********************************************************************/
  227. BOOL CUserManager::Serialize(BOOL bStoring)
  228. {
  229. static const TCHAR* lpszSignature = _T("LYFZ Software Solutions - StoreObject");
  230. CFile file;
  231. if (file.Open(m_strFilename, bStoring ? CFile::modeWrite|CFile::modeCreate : CFile::modeRead))
  232. {
  233. TRY
  234. {
  235. CString str;
  236. CArchive ar(&file, bStoring ? CArchive::store : CArchive::load);
  237. if (bStoring)
  238. {
  239. // 保存 signature
  240. ar << CString(lpszSignature);
  241. // 保存更改的用户信息
  242. for (int i=0; i < m_UserArray.GetSize(); i++)
  243. {
  244. m_UserArray[i].Serialize(ar);
  245. }
  246. ar.Flush();
  247. }
  248. else
  249. {
  250. // 加载 signature
  251. ar >> str;
  252. // if this the file we are looking for ?
  253. if (str.Compare(lpszSignature) == 0)
  254. {
  255. int nCount=0;
  256. while(!ar.IsBufferEmpty())
  257. {
  258. CUser user;
  259. // get user data
  260. user.Serialize(ar);
  261. // add user to array
  262. m_UserArray.Add(user);
  263. }
  264. }
  265. }
  266. ar.Close();
  267. file.Close();
  268. }
  269. CATCH_ALL(e)
  270. {
  271. // catch all exceptions that might happen ...
  272. return FALSE;
  273. }
  274. END_CATCH_ALL
  275. }
  276. return TRUE;
  277. }
  278. /********************************************************************/
  279. /*
  280. 函数名 : ConvertPathToLocal
  281. 描述 : 转换相对路径到本地路径
  282. 参数 :
  283. LPCTSTR lpszUser, 帐户
  284. CString &strDirectoryIn, 输入的目录串
  285. CString &strDirectoryOut 输入的目录
  286. 返回 : TRUE 成功, FALSE失败
  287. */
  288. /********************************************************************/
  289. BOOL CUserManager::ConvertPathToLocal(LPCTSTR lpszUser, CString &strDirectoryIn, CString &strDirectoryOut)
  290. {// AfxMessageBox(strDirectoryIn+"out"+strDirectoryOut);
  291. CUser user;
  292. if (!GetUser(lpszUser, user))
  293. {
  294. // user not valid
  295. return FALSE;
  296. }
  297. CStringList partList;
  298. CString strSub = _T("");
  299. int nCount=0;
  300. // 分割目录块
  301. while(AfxExtractSubString(strSub, strDirectoryIn, nCount++, '/'))
  302. {
  303. if (!strSub.IsEmpty())
  304. partList.AddTail(strSub);
  305. }
  306. // search for home directory
  307. for (int i=0; i<user.m_DirectoryArray.GetSize(); i++)
  308. {
  309. if (user.m_DirectoryArray[i].m_bIsHomeDir)
  310. {
  311. CString strHomeDir = user.m_DirectoryArray[i].m_strDir;
  312. while(!partList.IsEmpty())
  313. {
  314. CString strPart = partList.GetHead();
  315. partList.RemoveHead();
  316. CString strCheckDir = _T("");
  317. if (strPart == "..")
  318. {
  319. // 返回上一级目录
  320. int nPos = strHomeDir.ReverseFind('\\');
  321. if (nPos != -1)
  322. strCheckDir = strHomeDir.Left(nPos);
  323. }
  324. else
  325. {
  326. strCheckDir = strHomeDir + "\\" + strPart;
  327. }
  328. // 检测目录
  329. if (FileExists(strCheckDir, TRUE))
  330. {
  331. strHomeDir = strCheckDir;
  332. }
  333. else
  334. {
  335. // 检测文件
  336. if (FileExists(strCheckDir, FALSE))
  337. {
  338. strHomeDir = strCheckDir;
  339. }
  340. else
  341. {
  342. BOOL bFound = FALSE;
  343. // virtual directories exist only in the root
  344. if (strHomeDir == user.m_DirectoryArray[i].m_strDir)
  345. {
  346. // maybe it's a virtual directory
  347. for (int j=0; j<user.m_DirectoryArray.GetSize(); j++)
  348. {
  349. if (i != j && (user.m_DirectoryArray[j].m_strAlias.CompareNoCase(strPart) == 0))
  350. {
  351. bFound = TRUE;
  352. strHomeDir = user.m_DirectoryArray[j].m_strDir;
  353. break;
  354. }
  355. }
  356. }
  357. if (!bFound)
  358. {
  359. // directory not found
  360. return FALSE;
  361. }
  362. }
  363. }
  364. }
  365. // successfully converted directory
  366. strDirectoryOut = strHomeDir;// AfxMessageBox(strDirectoryIn+"out"+strDirectoryOut);
  367. return TRUE;
  368. }
  369. }
  370. // no home directory found
  371. return FALSE;
  372. }
  373. /********************************************************************/
  374. /*
  375. 函数名 : CheckAccessRights
  376. 描述 : 检查用户是否具有访问指定目录的访问
  377. 参数 :
  378. LPCTSTR lpszUser, 帐户
  379. LPCTSTR lpszDirectory, 目录
  380. int nOption 操作命令
  381. 返回 :
  382. */
  383. /********************************************************************/
  384. BOOL CUserManager::CheckAccessRights(LPCTSTR lpszUser, LPCTSTR lpszDirectory, int nOption)
  385. {
  386. CUser user;
  387. if (!GetUser(lpszUser, user))
  388. {
  389. // user not valid
  390. return FALSE;
  391. }
  392. // start with full path
  393. CString strCheckDir = lpszDirectory;
  394. while(!strCheckDir.IsEmpty())
  395. {
  396. // search for a matching part
  397. for (int i=0; i<user.m_DirectoryArray.GetSize(); i++)
  398. {
  399. CString strPath1 = strCheckDir;
  400. strPath1.TrimRight("\\");
  401. CString strPath2 = user.m_DirectoryArray[i].m_strDir;
  402. //////////////////////////////////////////////////////////////////////////
  403. // 禁用账号本身目录,使用NetShareInfo表里的目录; 2015-05-16
  404. #if 1
  405. strPath2 = strCheckDir;
  406. #endif
  407. strPath2.TrimRight("\\");
  408. // found a match ?
  409. if (strPath1.CompareNoCase(strPath2) == 0)
  410. {
  411. // check file access rights
  412. if (((!user.m_DirectoryArray[i].m_bAllowDownload) && (nOption == FTP_DOWNLOAD)) ||
  413. ((!user.m_DirectoryArray[i].m_bAllowUpload) && (nOption == FTP_UPLOAD)) ||
  414. ((!user.m_DirectoryArray[i].m_bAllowRename) && (nOption == FTP_RENAME)) ||
  415. ((!user.m_DirectoryArray[i].m_bAllowDelete) && (nOption == FTP_DELETE)) ||
  416. ((!user.m_DirectoryArray[i].m_bAllowCreateDirectory) && (nOption == FTP_CREATE_DIR)))
  417. {
  418. return FALSE;
  419. }
  420. return TRUE;
  421. }
  422. }
  423. int nPos = strCheckDir.ReverseFind('\\');
  424. if (nPos != -1)
  425. {
  426. // strip subdir
  427. strCheckDir = strCheckDir.Left(nPos);
  428. }
  429. else
  430. {
  431. // we're done
  432. strCheckDir.Empty();
  433. }
  434. }
  435. // users has no rights to this directory
  436. return FALSE;
  437. }
  438. /********************************************************************/
  439. /*
  440. 函数名 : ChangeDirectory
  441. 描述 : 更改指定目录
  442. 参数 :
  443. LPCTSTR lpszUser, 用户
  444. CString &strCurrentdir, 当前目录
  445. CString &strChangeTo 要更改的目录
  446. 返回 : 2=无法转换到本地路径, 1=用户没有权限, 0=成功
  447. */
  448. /********************************************************************/
  449. int CUserManager::ChangeDirectory(LPCTSTR lpszUser, CString &strCurrentdir, CString &strChangeTo)
  450. {
  451. // make unix style
  452. strChangeTo.Replace("\\","/");
  453. while(strChangeTo.Replace("//","/"));
  454. strChangeTo.TrimRight("/");
  455. // now looks something like this:
  456. // "" = root
  457. // "/mydir/apps" = absolute path
  458. // "mydir/apps" = relative path
  459. if (strChangeTo == "")
  460. {
  461. // goto root
  462. strChangeTo = "/";
  463. }
  464. else
  465. {
  466. // first character '/' ?
  467. if (strChangeTo.Left(1) != "/")
  468. {
  469. // 客户端指定一个相对于它们当前路径的路径
  470. strCurrentdir.TrimRight("/");
  471. strChangeTo = strCurrentdir + "/" + strChangeTo;
  472. }
  473. }
  474. // 转到根目录
  475. if (strChangeTo.Right(2) == "..")
  476. {
  477. strChangeTo.TrimRight("..");
  478. strChangeTo.TrimRight("/");
  479. int nPos = strChangeTo.ReverseFind('/');
  480. if (nPos != -1)
  481. strChangeTo = strChangeTo.Left(nPos);
  482. // goto root
  483. if (strChangeTo == "")
  484. strChangeTo = "/";
  485. }
  486. // 获取本地路径
  487. CString strLocalPath;
  488. if (!ConvertPathToLocal(lpszUser, strChangeTo, strLocalPath))
  489. {
  490. // 无法转换到本地路径
  491. return 2;
  492. }
  493. // check if user has access right for this directory
  494. if (!CheckAccessRights(lpszUser, strLocalPath, FTP_DOWNLOAD))
  495. {
  496. // 用户没有权限
  497. return 1;
  498. }
  499. // everything went successfully
  500. strCurrentdir = strChangeTo;
  501. return 0;
  502. }
  503. /********************************************************************/
  504. /*
  505. 函数名 : GetDirectoryList
  506. 描述 : 获取指定目录的目录列表
  507. 参数 :
  508. LPCTSTR lpszUser, 用户
  509. LPCTSTR lpszDirectory, 目录
  510. CString &strResult 返回权限设置
  511. 返回 : 2=无法转换到本地路径, 1=用户没有权限, 0=成功
  512. */
  513. /********************************************************************/
  514. int CUserManager::GetDirectoryList(LPCTSTR lpszUser, LPCTSTR lpszDirectory, CString &strResult)
  515. {
  516. CString strDirectory = lpszDirectory;
  517. // AfxMessageBox("dddddd");
  518. // make unix style
  519. strDirectory.Replace("\\","/");
  520. while(strDirectory.Replace("//","/"));
  521. // clear list
  522. strResult = "";
  523. CUser user;
  524. if (!GetUser(lpszUser, user))
  525. {
  526. // 用户没有权限
  527. return 1;
  528. }
  529. CString strLocalPath;
  530. if (!ConvertPathToLocal(lpszUser, strDirectory, strLocalPath))
  531. {
  532. // 无法转换到本地路径
  533. return 2;
  534. }
  535. // check if user has access right for this directory
  536. if (!CheckAccessRights(lpszUser, strLocalPath, FTP_DOWNLOAD))
  537. {
  538. // 用户没有权限
  539. return 1;
  540. }
  541. CFileFind find;
  542. BOOL bFound = FALSE;
  543. // 检测目录
  544. if ((GetFileAttributes(strLocalPath) & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY)
  545. {
  546. bFound = find.FindFile(strLocalPath + "\\*.*");
  547. }
  548. else
  549. {
  550. // it's a file
  551. bFound = find.FindFile(strLocalPath);
  552. }
  553. while (bFound)
  554. {
  555. bFound = find.FindNextFile();
  556. // skip "." and ".."
  557. if (find.IsDots())
  558. continue;
  559. // 权限设置
  560. if (find.IsDirectory())
  561. strResult += "drwx------";
  562. else
  563. strResult += "-rwx------";
  564. // 用户组
  565. strResult += " 1 user group ";
  566. // file size
  567. CString strLength;
  568. strLength.Format("%d", find.GetLength());
  569. CString strFiller = " ";
  570. strResult += strFiller.Left(strFiller.GetLength() - strLength.GetLength());
  571. strResult += strLength;
  572. // file date
  573. strResult += GetFileDate(find);
  574. // file name
  575. strResult += find.GetFileName();
  576. // end of line
  577. strResult += "\r\n";
  578. }
  579. // 如果它是根目录也显示虚拟目录
  580. for (int i=0; i<user.m_DirectoryArray.GetSize(); i++)
  581. {
  582. if (user.m_DirectoryArray.GetAt(i).m_bIsHomeDir)
  583. {
  584. CString strPath = user.m_DirectoryArray.GetAt(i).m_strDir;
  585. strPath.TrimRight('\\');
  586. if (strLocalPath.CompareNoCase(strPath) == 0)
  587. {
  588. for (int j=0; j<user.m_DirectoryArray.GetSize(); j++)
  589. {
  590. if (i != j && user.m_DirectoryArray.GetAt(j).m_strAlias != "")
  591. {
  592. if (find.FindFile(user.m_DirectoryArray.GetAt(j).m_strDir))
  593. {
  594. find.FindNextFile();
  595. strResult += "drwx------";
  596. // groups
  597. strResult += " 1 user group ";
  598. strResult += " 0";
  599. // file date
  600. strResult += GetFileDate(find);
  601. // file name
  602. strResult += user.m_DirectoryArray.GetAt(j).m_strAlias;
  603. // end of line
  604. strResult += "\r\n";
  605. }
  606. }
  607. }
  608. }
  609. break;
  610. }
  611. }
  612. return 0;
  613. }
  614. /********************************************************************/
  615. /*
  616. 函数名 : FileExists
  617. 描述 : 检测是目录或者是文件
  618. 参数 :
  619. LPCTSTR lpszFileName, 目录或文件名
  620. BOOL bIsDirCheck 检测(1=目录,0=文件)
  621. 返回 : TRUE存在, FALSE 不存在
  622. */
  623. /********************************************************************/
  624. BOOL CUserManager::FileExists(LPCTSTR lpszFileName, const int bIsDirCheck)
  625. {
  626. // A quick'n'easy way to see if a file exists.
  627. DWORD dwAttributes = GetFileAttributes(lpszFileName);
  628. if (dwAttributes == 0xFFFFFFFF)
  629. return FALSE;
  630. //是否为目录
  631. if ((dwAttributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY)
  632. {
  633. if (bIsDirCheck == 1)
  634. return TRUE;
  635. }
  636. else
  637. {
  638. if (bIsDirCheck == 0)
  639. return TRUE;
  640. }
  641. return FALSE;
  642. }
  643. /********************************************************************/
  644. /*
  645. 函数名 : CheckFileName
  646. 描述 : 检查文件名是否正确,如果用户有访问权限
  647. 参数 :
  648. LPCTSTR lpszUser, 用户
  649. CString strFilename, 文件名
  650. CString strCurrentdir, 当前目录
  651. int nOption, ftp操作命令
  652. CString &strResult 文件全路径
  653. 返回 : 2=目录或文件不存在, 1=用户没有权限, 0=成功
  654. */
  655. /********************************************************************/
  656. int CUserManager::CheckFileName(LPCTSTR lpszUser, CString strFilename, CString strCurrentdir, int nOption, CString &strResult)
  657. {
  658. // make unix style
  659. strFilename.Replace("\\", "/");
  660. while(strFilename.Replace("//", "/"));
  661. strFilename.TrimRight("/");
  662. if (strFilename == "")
  663. {
  664. // no file name
  665. return 2;
  666. }
  667. // append filename to directory
  668. CString strDirectory = strCurrentdir;
  669. // 客户端已指定完整路径
  670. int nPos = strFilename.ReverseFind('/');
  671. if (nPos != -1)
  672. {
  673. strDirectory = strFilename.Left(nPos);
  674. if (strDirectory == "")
  675. strDirectory = "/";
  676. strFilename = strFilename.Mid(nPos+1);
  677. }
  678. // 转换到本地路径
  679. CString strLocalPath = _T("");
  680. if (!ConvertPathToLocal(lpszUser, strDirectory, strLocalPath))
  681. {
  682. // directory does not exist
  683. return 2;
  684. }
  685. //////////////////////////////////////////////////////////////////////////
  686. // 替换掉strLocalPath;
  687. #if 1
  688. // 下载相片的目录由NetShareInfo决定;
  689. BYTE byPhotoType = 0;
  690. if ( _tcscmp(lpszUser,_T("photo1account")) == 0 )
  691. {
  692. byPhotoType = 1; // 客户原片;
  693. }
  694. else if ( _tcscmp(lpszUser,_T("photo2account")) == 0 )
  695. {
  696. byPhotoType = 2; // 初修片;
  697. }
  698. else if ( _tcscmp(lpszUser,_T("photo3account")) == 0 )
  699. {
  700. byPhotoType = 3; // 精修片;
  701. }
  702. else if ( _tcscmp(lpszUser,_T("photo4account")) == 0 )
  703. {
  704. byPhotoType = 4; // 设计片;
  705. }
  706. BOOL bExistDirectory = FALSE;
  707. double dFreeNum;
  708. double dTotalNum;
  709. CString strBranchId = _T("");
  710. CString strOrderNum = _T("");
  711. TCHAR szEnableDirectory[MAX_PATH] = _T("");
  712. int nIndex = -1;
  713. nIndex = strDirectory.Find(_T(".ly.com"));
  714. if ( nIndex == -1 )
  715. {
  716. if(-1 != strDirectory.Find(_T("."))) // test.dat之类的文件;
  717. {
  718. strOrderNum = strDirectory.Mid(1);
  719. bExistDirectory = CBranchInfo::GetInstance()->GetEnableShareDirectory(dFreeNum,byPhotoType,CBranchInfo::GetInstance()->GetBranchId(),strOrderNum,szEnableDirectory);
  720. }
  721. }
  722. else
  723. {
  724. strBranchId = strDirectory.Mid(1,nIndex+6);
  725. strOrderNum = strDirectory.Mid(nIndex+8);
  726. bExistDirectory = CBranchInfo::GetInstance()->GetEnableShareDirectory(dFreeNum,byPhotoType,strBranchId,strOrderNum,szEnableDirectory);
  727. }
  728. // 如果目录存在;
  729. if ( bExistDirectory )
  730. {
  731. strLocalPath = szEnableDirectory;
  732. }
  733. strLocalPath.TrimRight(_T('\\'));
  734. #endif
  735. // create the complete path
  736. strResult = strLocalPath + "\\" + strFilename;
  737. if ((nOption != FTP_UPLOAD) && !FileExists(strResult, FALSE))
  738. {
  739. // file does not exist
  740. return 2;
  741. }
  742. // 相对路径
  743. if (nOption == FTP_LIST)
  744. {
  745. strResult = strCurrentdir;
  746. strResult.TrimRight('/');
  747. strResult += "/" + strFilename;
  748. return 0;
  749. }
  750. // 检查文件访问权限
  751. if (!CheckAccessRights(lpszUser, strLocalPath, nOption))
  752. {
  753. // 用户没有权限
  754. return 1;
  755. }
  756. // everything is ok
  757. return 0;
  758. }
  759. /********************************************************************/
  760. /*
  761. 函数名 : CheckDirectory
  762. 描述 : 检查目录是否存在
  763. 参数 :
  764. LPCTSTR lpszUser, 用户
  765. CString strDirectory, 目录
  766. CString strCurrentdir, 当前目录
  767. int nOption, FTP操作命令
  768. CString &strResult 返回
  769. 返回 : 2=目录或文件不存在, 1=用户没有权限, 0=成功
  770. */
  771. /********************************************************************/
  772. int CUserManager::CheckDirectory(LPCTSTR lpszUser, CString strDirectory, CString strCurrentdir, int nOption, CString &strResult)
  773. {
  774. // make unix compatible
  775. strDirectory.Replace("\\","/");
  776. while(strDirectory.Replace("//","/"));
  777. strDirectory.TrimRight("/");
  778. if (strDirectory == "")
  779. {
  780. // no directory
  781. return 2;
  782. }
  783. else
  784. {
  785. // first character '/' ?
  786. if (strDirectory.Left(1) != "/")
  787. {
  788. // 指定一个相对于当前路径的路径
  789. strCurrentdir.TrimRight("/");
  790. strDirectory = strCurrentdir + "/" + strDirectory;
  791. }
  792. }
  793. // split part into 2 parts
  794. int nPos = strDirectory.ReverseFind('/');
  795. if (nPos == -1)
  796. return 2;
  797. // get left part of directory
  798. CString strNode = strDirectory.Left(nPos);
  799. // root ?
  800. if (strNode == "")
  801. strNode = "/";
  802. // get right part of directory
  803. strDirectory = strDirectory.Mid(nPos+1);
  804. CString strLocalPath;
  805. do
  806. {
  807. // does parent directory exist ?
  808. if ((!ConvertPathToLocal(lpszUser, strNode, strLocalPath)) && (nOption == FTP_CREATE_DIR))
  809. {
  810. // directory could not be found, maybe one level higher
  811. int nPos = strNode.ReverseFind('/');
  812. // no more levels
  813. if (nPos == -1)
  814. return 2;
  815. strDirectory = strNode.Mid(nPos+1) + "/" + strDirectory;
  816. strNode = strNode.Left(nPos);
  817. continue;
  818. }
  819. // check directory access rights
  820. if (!CheckAccessRights(lpszUser, strLocalPath, nOption))
  821. {
  822. // 没权限
  823. return 1;
  824. }
  825. strNode = strLocalPath;
  826. break;
  827. }
  828. while (strNode != "/");
  829. strDirectory.Replace("/","\\");
  830. strResult = strNode + "\\" + strDirectory;
  831. // check if directory exists
  832. if (!FileExists(strResult))
  833. return 2;
  834. // function successfull
  835. return 0;
  836. }
  837. /********************************************************************/
  838. /*
  839. 函数名 : GetUser
  840. 描述 : 获取指定用户名的用户对象
  841. 参数 :
  842. LPCTSTR lpszUser, 用户名
  843. CUser &user 返回用户对象
  844. 返回 : TRUE成功,FALSE失败
  845. */
  846. /********************************************************************/
  847. BOOL CUserManager::GetUser(LPCTSTR lpszUser, CUser &user)
  848. {
  849. m_CriticalSection.Lock();
  850. for (int i=0; i<m_UserArray.GetSize(); i++)
  851. {
  852. if (!m_UserArray[i].m_strName.CompareNoCase(lpszUser))
  853. {
  854. user = m_UserArray[i];
  855. m_CriticalSection.Unlock();
  856. return TRUE;
  857. }
  858. }
  859. m_CriticalSection.Unlock();
  860. return FALSE;
  861. }
  862. /********************************************************************/
  863. /*
  864. 函数名 : GetUserList
  865. 描述 : 获取用户集
  866. 参数 :
  867. CArray<CUser, CUser&>&array 返回用户集
  868. 返回 :
  869. */
  870. /********************************************************************/
  871. void CUserManager::GetUserList(CArray<CUser, CUser&>&array)
  872. {
  873. m_CriticalSection.Lock();
  874. for (int i=0; i<m_UserArray.GetSize();i++)
  875. {
  876. array.Add(m_UserArray[i]);
  877. }
  878. m_CriticalSection.Unlock();
  879. }
  880. /********************************************************************/
  881. /*
  882. 函数名 : UpdateUserList
  883. 描述 : 更新用户集
  884. 参数 :
  885. CArray<CUser, CUser&>&array 用户集
  886. 返回 :
  887. */
  888. /********************************************************************/
  889. void CUserManager::UpdateUserList(CArray<CUser, CUser&>&array)
  890. {
  891. m_CriticalSection.Lock();
  892. m_UserArray.RemoveAll();
  893. for (int i=0; i<array.GetSize();i++)
  894. {
  895. m_UserArray.Add(array[i]);
  896. }
  897. m_CriticalSection.Unlock();
  898. Serialize(TRUE);
  899. }
  900. /********************************************************************/
  901. /*
  902. 函数名 : GetFileDate
  903. 描述 : 获取unix格式文件日期
  904. 参数 :
  905. CFileFind &find 文件类
  906. 返回 :
  907. */
  908. /********************************************************************/
  909. CString CUserManager::GetFileDate(CFileFind &find)
  910. {
  911. CString strResult;
  912. CTime time = CTime::GetCurrentTime();
  913. find.GetLastWriteTime(time);
  914. CTimeSpan timeSpan = CTime::GetCurrentTime() - time;
  915. if (timeSpan.GetDays() > 356)
  916. {
  917. strResult = time.Format(" %b %d %Y ");
  918. }
  919. else
  920. {
  921. strResult.Format(" %s %02d:%02d ", time.Format("%b %d"), time.GetHour(), time.GetMinute());
  922. }
  923. return strResult;
  924. }