OTSLicMgr.cpp 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309
  1. #include "stdafx.h"
  2. #include "OTSLicMgr.h"
  3. #include "resource.h"
  4. #include "OTSFileSys.h"
  5. #include "OTSHelper.h"
  6. #include "OTSCrypt.h"
  7. #include "COTSUtilityDllFunExport.h"
  8. #include "MultiLang.h"
  9. namespace OTSMODEL {
  10. // COTSLicenseFile class
  11. // OTS license/license key file
  12. IMPLEMENT_SERIAL(COTSLicenseFile, CObject, 1)
  13. // constructor
  14. COTSLicenseFile::COTSLicenseFile()
  15. {
  16. Init();
  17. }
  18. // copy constructor
  19. COTSLicenseFile::COTSLicenseFile(COTSLicenseFile* a_poSource)
  20. {
  21. // input check
  22. ASSERT(a_poSource);
  23. if (!a_poSource)
  24. {
  25. return;
  26. }
  27. // can't copy itself
  28. if (a_poSource == this)
  29. {
  30. return;
  31. }
  32. // copy data over
  33. Duplicate(*a_poSource);
  34. }
  35. // copy constructor
  36. COTSLicenseFile::COTSLicenseFile(const COTSLicenseFile& a_oSource)
  37. {
  38. // can't copy itself
  39. if (&a_oSource == this)
  40. {
  41. return;
  42. }
  43. // copy data over
  44. Duplicate(a_oSource);
  45. }
  46. // =operator
  47. COTSLicenseFile& COTSLicenseFile::operator=(const COTSLicenseFile& a_oSource)
  48. {
  49. // cleanup
  50. Cleanup();
  51. // copy the class data over
  52. Duplicate(a_oSource);
  53. // return class
  54. return *this;
  55. }
  56. // detractor
  57. COTSLicenseFile::~COTSLicenseFile()
  58. {
  59. // cleanup
  60. Cleanup();
  61. }
  62. // COTSLicenseFile member functions
  63. void COTSLicenseFile::SetLicenseInfo(COTSLicenseInfoPtr a_pLicenseInfo)
  64. {
  65. ASSERT(a_pLicenseInfo);
  66. if (!a_pLicenseInfo)
  67. {
  68. return;
  69. }
  70. m_pLicenseInfo = COTSLicenseInfoPtr(new COTSLicenseInfo(a_pLicenseInfo.get()));
  71. }
  72. // Load/Save
  73. // NOTE: be sure a_strPathName is a valid file pathname
  74. BOOL COTSLicenseFile::Load(CString a_strPathName)
  75. {
  76. // check the file pathname
  77. a_strPathName.Trim();
  78. if (a_strPathName.IsEmpty())
  79. {
  80. LogErrorTrace(__FILE__, __LINE__, _T("Load: file pathname string is empty."));
  81. return FALSE;
  82. }
  83. // open the license file
  84. CFile hFile;
  85. CFileException ex;
  86. if (!hFile.Open(a_strPathName, CFile::modeRead, &ex))
  87. {
  88. // failed to open the file
  89. TCHAR szCause[255];
  90. ex.GetErrorMessage(szCause, 255);
  91. LogErrorTrace(__FILE__, __LINE__, _T("Load: can't open file %s. error: %s"), a_strPathName, szCause);
  92. return FALSE;
  93. }
  94. // create license info point
  95. m_pLicenseInfo = COTSLicenseInfoPtr(new COTSLicenseInfo());
  96. // create a loading archive
  97. CArchive ar(&hFile, CArchive::load);
  98. // file serialization (load)
  99. Serialize(ar);
  100. ar.Close();
  101. // file pathname
  102. m_strPathName = a_strPathName;
  103. // ok, return TRUE
  104. return TRUE;
  105. }
  106. BOOL COTSLicenseFile::Save(CString a_strPathName)
  107. {
  108. // check the file pathname
  109. a_strPathName.Trim();
  110. if (a_strPathName.IsEmpty())
  111. {
  112. LogErrorTrace(__FILE__, __LINE__, _T("Save: file pathname string is empty."));
  113. return FALSE;
  114. }
  115. // can do nothing if there is no license file info
  116. ASSERT(m_pLicenseInfo);
  117. if (!m_pLicenseInfo)
  118. {
  119. LogErrorTrace(__FILE__, __LINE__, _T("Save: invalid license file"));
  120. return FALSE;
  121. }
  122. // get path of the file
  123. CString strFilePath = COTSHelper::GetFolderName(a_strPathName);
  124. if (!strFilePath.IsEmpty())
  125. {
  126. // check if file folder exists
  127. if (!COTSFileSys::Exists(strFilePath))
  128. {
  129. // create the file folder
  130. if (!COTSFileSys::CreateFolder(strFilePath))
  131. {
  132. // failed to create the file folder
  133. LogErrorTrace(__FILE__, __LINE__, _T("Save: failed to create file folder. %s"), strFilePath);
  134. return FALSE;
  135. }
  136. }
  137. }
  138. // Open the file
  139. CFile hFile;
  140. CFileException ex;
  141. if (!hFile.Open(a_strPathName, CFile::modeCreate | CFile::modeWrite, &ex))
  142. {
  143. // failed to open the file
  144. TCHAR szCause[255];
  145. ex.GetErrorMessage(szCause, 255);
  146. LogErrorTrace(__FILE__, __LINE__, _T("Save: can't open file %s. error: %s"), a_strPathName, szCause);
  147. return FALSE;
  148. }
  149. // store to file
  150. CArchive ar(&hFile, CArchive::store);
  151. // file serialization (store)
  152. Serialize(ar);
  153. ar.Close();
  154. // file pathname
  155. m_strPathName = a_strPathName;
  156. // ok, return TRUE
  157. return TRUE;
  158. }
  159. // serialization
  160. CString strLicense;
  161. void COTSLicenseFile::Serialize(CArchive& ar)
  162. {
  163. // store?
  164. if (ar.IsStoring())
  165. {
  166. // store
  167. ar << LICENNSE_FILE_MARK;
  168. ar << LICENNSE_FILE_VERSION;
  169. // license string
  170. strLicense = COTSLicMgr::EncryptLicenseInfo(m_pLicenseInfo);
  171. ar << strLicense;
  172. }
  173. else
  174. {
  175. // load
  176. // check if file mark
  177. int nFileNark;
  178. ar >> nFileNark;
  179. if (nFileNark != LICENNSE_FILE_MARK)
  180. {
  181. // invalid license file
  182. LogErrorTrace(__FILE__, __LINE__, _T("Serialize: invalid license file."));
  183. return;
  184. }
  185. // file version string;
  186. CString strFileVersion;
  187. ar >> strFileVersion;
  188. // license string
  189. CString strLicense;
  190. ar >> strLicense;
  191. m_pLicenseInfo = COTSLicMgr::DecryptLicenseInfo(strLicense);
  192. }
  193. // base object serialization
  194. CObject::Serialize(ar);
  195. }
  196. void COTSLicenseFile::Serialize(bool isStoring, tinyxml2::XMLDocument * classDoc, tinyxml2::XMLElement * rootNode)
  197. {
  198. xmls::xInt xLicennseFileMark;
  199. xmls::xString xLicennseFileVersion;
  200. xmls::xString xstrLicense;
  201. this->Register("LicennseFileMark", &xLicennseFileMark);
  202. this->Register("LicennseFileVersion", &xLicennseFileVersion);
  203. this->Register("strLicense", &xstrLicense);
  204. if (isStoring)
  205. {
  206. xLicennseFileMark = LICENNSE_FILE_MARK;
  207. xLicennseFileVersion = LICENNSE_FILE_VERSION;
  208. xstrLicense = strLicense;
  209. Slo::Serialize(true, classDoc, rootNode);
  210. }
  211. else
  212. {
  213. xmls::Slo::Serialize(false, classDoc, rootNode);
  214. strLicense = xstrLicense.value().c_str();
  215. }
  216. }
  217. // cleanup
  218. void COTSLicenseFile::Cleanup()
  219. {
  220. }
  221. // initialization
  222. void COTSLicenseFile::Init()
  223. {
  224. m_strPathName = _T("");
  225. }
  226. // duplication
  227. void COTSLicenseFile::Duplicate(const COTSLicenseFile& a_oSource)
  228. {
  229. // initialization
  230. Init();
  231. // copy data over
  232. m_pLicenseInfo = COTSLicenseInfoPtr(new COTSLicenseInfo((a_oSource.m_pLicenseInfo).get()));
  233. m_strPathName = a_oSource.m_strPathName;
  234. }
  235. // COTSLicInfoFile class
  236. // license manager app file
  237. IMPLEMENT_SERIAL(COTSLicInfoFile, CObject, 1)
  238. // constructor
  239. COTSLicInfoFile::COTSLicInfoFile()
  240. {
  241. Init();
  242. }
  243. // copy constructor
  244. COTSLicInfoFile::COTSLicInfoFile(const COTSLicInfoFile& a_oSource)
  245. {
  246. // can't copy itself
  247. if (&a_oSource == this)
  248. {
  249. return;
  250. }
  251. // copy data over
  252. Duplicate(a_oSource);
  253. }
  254. // copy constructor
  255. COTSLicInfoFile::COTSLicInfoFile(COTSLicInfoFile* a_poSource)
  256. {
  257. // input check
  258. ASSERT(a_poSource);
  259. if (!a_poSource)
  260. {
  261. return;
  262. }
  263. // can't copy itself
  264. if (a_poSource == this)
  265. {
  266. return;
  267. }
  268. // copy data over
  269. Duplicate(*a_poSource);
  270. }
  271. // =operator
  272. COTSLicInfoFile& COTSLicInfoFile::operator=(const COTSLicInfoFile& a_oSource)
  273. {
  274. // cleanup
  275. Cleanup();
  276. // copy the class data over
  277. Duplicate(a_oSource);
  278. // return class
  279. return *this;
  280. }
  281. // detractor
  282. COTSLicInfoFile::~COTSLicInfoFile()
  283. {
  284. // cleanup
  285. Cleanup();
  286. }
  287. // COTSLicInfoFile member functions
  288. // Load/Save
  289. // NOTE: be sure a_strPathName is a valid file pathname
  290. BOOL COTSLicInfoFile::Load(CString a_strPathName)
  291. {
  292. // open the license file
  293. CFile hFile;
  294. CFileException ex;
  295. if (!hFile.Open(a_strPathName, CFile::modeRead, &ex))
  296. {
  297. // failed to open the file
  298. TCHAR szCause[255];
  299. ex.GetErrorMessage(szCause, 255);
  300. LogErrorTrace(__FILE__, __LINE__, _T("Load: can't open file %s. error: %s"), a_strPathName, szCause);
  301. return FALSE;
  302. }
  303. // create a loading archive
  304. CArchive ar(&hFile, CArchive::load);
  305. m_listLicenseInfo.clear();
  306. Serialize(ar);
  307. ar.Close();
  308. // file pathname
  309. m_strPathName = a_strPathName;
  310. // ok, return TRUE
  311. return TRUE;
  312. }
  313. BOOL COTSLicInfoFile::Save()
  314. {
  315. // check if file pathname is empty
  316. m_strPathName.Trim();
  317. if (m_strPathName.IsEmpty())
  318. {
  319. LogErrorTrace(__FILE__, __LINE__, _T("Save: file pathname is empty."));
  320. return FALSE;
  321. }
  322. // Open the file
  323. CFile hFile;
  324. CFileException ex;
  325. if (!hFile.Open(m_strPathName, CFile::modeCreate | CFile::modeWrite, &ex))
  326. {
  327. // failed to open the file
  328. TCHAR szCause[255];
  329. ex.GetErrorMessage(szCause, 255);
  330. LogErrorTrace(__FILE__, __LINE__, _T("Save: can't open file %s. error: %s"), m_strPathName, szCause);
  331. return FALSE;
  332. }
  333. // store to file
  334. CArchive ar(&hFile, CArchive::store);
  335. Serialize(ar);
  336. ar.Close();
  337. // ok, return TRUE
  338. return TRUE;
  339. }
  340. // serialization
  341. void COTSLicInfoFile::Serialize(CArchive& ar)
  342. {
  343. // store?
  344. if (ar.IsStoring())
  345. {
  346. // store
  347. ar << LICENSE_INFO_FILE_MARK;
  348. ar << LICENSE_InFO_FILE_VERSION;
  349. ar << (int)m_listLicenseInfo.size();
  350. for (auto pLicenseInfo : m_listLicenseInfo)
  351. {
  352. pLicenseInfo->Serialize(ar);
  353. }
  354. }
  355. else
  356. {
  357. // load
  358. // check if file mark
  359. int nFileNark;
  360. ar >> nFileNark;
  361. if (nFileNark != LICENSE_INFO_FILE_MARK)
  362. {
  363. // invalid license file
  364. LogErrorTrace(__FILE__, __LINE__, _T("Serialize: invalid license info file."));
  365. return;
  366. }
  367. // file version string;
  368. CString strFileVersion;
  369. ar >> strFileVersion;
  370. // license info class number
  371. int nSize = 0;
  372. ar >> nSize;
  373. for (int i = 0; i < nSize; ++i)
  374. {
  375. COTSLicenseInfoPtr pLicenseInfo(new COTSLicenseInfo());
  376. pLicenseInfo->Serialize(ar);
  377. m_listLicenseInfo.push_back(pLicenseInfo);
  378. }
  379. }
  380. // base object serialization
  381. CObject::Serialize(ar);
  382. }
  383. // cleanup
  384. void COTSLicInfoFile::Cleanup()
  385. {
  386. }
  387. // initialization
  388. void COTSLicInfoFile::Init()
  389. {
  390. m_listLicenseInfo.clear();
  391. m_bModify = FALSE;
  392. }
  393. // duplication
  394. void COTSLicInfoFile::Duplicate(const COTSLicInfoFile& a_oSource)
  395. {
  396. // initialization
  397. Init();
  398. // copy data over
  399. for (auto pLicenseInfoSource : a_oSource.m_listLicenseInfo)
  400. {
  401. COTSLicenseInfoPtr pLicenseInfoNew = COTSLicenseInfoPtr(new COTSLicenseInfo(pLicenseInfoSource.get()));
  402. m_listLicenseInfo.push_back(pLicenseInfoNew);
  403. }
  404. m_strPathName = a_oSource.m_strPathName;
  405. }
  406. // constructor
  407. COTSLicMgr::COTSLicMgr()
  408. : m_nPackId(OTS_SOFT_PACKAGE_ID::INVALID)
  409. , m_nLicStatus(OTS_LICENSE_STATUS::INVALID)
  410. , m_pOTSLicenseFile(nullptr)
  411. {
  412. MultiLang::GetInstance().LoadStringFromXml();
  413. }
  414. // destructor
  415. COTSLicMgr::~COTSLicMgr()
  416. {
  417. }
  418. // COTSLicMgr member functions
  419. // check if there is valid license of the given software package
  420. BOOL COTSLicMgr::IsThereValidPackLicense(OTS_SOFT_PACKAGE_ID a_nPackId)
  421. {
  422. // make sure software package id is valid
  423. if (a_nPackId == OTS_SOFT_PACKAGE_ID::INVALID)
  424. {
  425. LogErrorTrace(__FILE__, __LINE__, _T("IsThereValidPackLicense: invalid software package id. %d"), (int)a_nPackId);
  426. return FALSE;
  427. }
  428. // get software package license file pathname
  429. CString strFilePathName = GetPackLicenseFilePathName(a_nPackId);
  430. strFilePathName.Trim();
  431. if (strFilePathName.IsEmpty())
  432. {
  433. LogErrorTrace(__FILE__, __LINE__, _T("IsThereValidPackLicense: failed to find software package license file pathname. software package id: %d"), (int)a_nPackId);
  434. m_nLicStatus = OTS_LICENSE_STATUS::INVALID;
  435. return FALSE;
  436. }
  437. // create a license file pointer
  438. COTSLicenseFilePtr pOTSLicenseFilePtr = COTSLicenseFilePtr(new COTSLicenseFile());
  439. // load license file
  440. if (!pOTSLicenseFilePtr->Load(strFilePathName))
  441. {
  442. // failed to load the software package license file
  443. LogErrorTrace(__FILE__, __LINE__, _T("IsThereValidPackLicense: failed to load the software package license file. pathname: %s"), strFilePathName);
  444. m_nLicStatus = OTS_LICENSE_STATUS::NO_FILE;
  445. return FALSE;
  446. }
  447. m_pOTSLicenseFile = pOTSLicenseFilePtr;
  448. m_nPackId = a_nPackId;
  449. // return the license file validation result
  450. return COTSLicMgr::IsValidLicense(m_nPackId, m_pOTSLicenseFile->GetLicenseInfo(), m_nLicStatus);
  451. }
  452. // load license info file
  453. //
  454. BOOL COTSLicMgr::LoadLicenseInfoFile()
  455. {
  456. // create a license info file pointer
  457. COTSLicInfoFilePtr pOTSLicInfoFilePtr = COTSLicInfoFilePtr(new COTSLicInfoFile());
  458. // get company log path
  459. CString strCompanySysDataPathName = COTSFileSys::GetCompanySysDataPathName();
  460. if (strCompanySysDataPathName.IsEmpty())
  461. {
  462. LogErrorTrace(__FILE__, __LINE__, _T("LoadLicenseInfoFile: failed to company system data pathname."));
  463. return FALSE;
  464. }
  465. // get license info file pathname
  466. CString strLicenseInfoPathName = GetLicenseInfoFilePathName();
  467. if (strLicenseInfoPathName.IsEmpty())
  468. {
  469. LogErrorTrace(__FILE__, __LINE__, _T("LoadLicenseInfoFile: failed to find license info file pathname."));
  470. return FALSE;
  471. }
  472. // check if company system data path exist
  473. if (COTSFileSys::Exists(strCompanySysDataPathName))
  474. {
  475. // yes, try to open license info file pathname
  476. // check if the file is there
  477. if (!COTSFileSys::Exists(strLicenseInfoPathName))
  478. {
  479. // license info file is missing
  480. LogTrace(__FILE__, __LINE__, _T("LoadLicenseInfoFile: license info file is missing. pathname: %s"), strLicenseInfoPathName);
  481. pOTSLicInfoFilePtr->SetPathName(strLicenseInfoPathName);
  482. }
  483. // load license info file
  484. else if (!pOTSLicInfoFilePtr->Load(strLicenseInfoPathName))
  485. {
  486. // failed to load the license info file
  487. LogErrorTrace(__FILE__, __LINE__, _T("LoadLicenseInfoFile: failed to load license info file. pathname: %s"), strLicenseInfoPathName);
  488. return FALSE;
  489. }
  490. }
  491. // create company system data path
  492. else if (COTSFileSys::CreateFolder(strCompanySysDataPathName))
  493. {
  494. // company system data path is missing
  495. LogTrace(__FILE__, __LINE__, _T("LoadLicenseInfoFile: company system data path is missing."));
  496. pOTSLicInfoFilePtr->SetPathName(strLicenseInfoPathName);
  497. }
  498. else
  499. {
  500. // failed to create company system data path
  501. LogErrorTrace(__FILE__, __LINE__, _T("LoadLicenseInfoFile: failed to create company system data path."));
  502. return FALSE;
  503. }
  504. // assign license info file point
  505. m_pOTSLicenseInfoFile = pOTSLicInfoFilePtr;
  506. // ok, return TRUE
  507. return TRUE;
  508. }
  509. // get software package license file pathname
  510. CString COTSLicMgr::GetPackLicenseFilePathName(OTS_SOFT_PACKAGE_ID a_nPackId)
  511. {
  512. // get software package system data pathname
  513. CString strOTSPackSysDataPathName = COTSFileSys::GetOTSPackSysDataPathName(a_nPackId);
  514. // check if software package system data pathname is right
  515. if (strOTSPackSysDataPathName.IsEmpty())
  516. {
  517. // failed to get software package system data pathname
  518. LogErrorTrace(__FILE__, __LINE__, _T("GetPackLicenseFilePathName: failed to get software package system data path string."));
  519. return _T("");
  520. }
  521. // software package license file pathname
  522. // i.e. "c:\ProgramData\Config\SysData\OTSLicense.lic"
  523. CString strOTSPackLicFileNathName = strOTSPackSysDataPathName + LICENSE_FILENAME;
  524. // return software package license file pathname
  525. return strOTSPackLicFileNathName;
  526. }
  527. // get license info file pathname
  528. CString COTSLicMgr::GetLicenseInfoFilePathName()
  529. {
  530. // get company system data path
  531. CString strOTSSysDataPathName = COTSFileSys::GetCompanySysDataPathName();
  532. // check if software package system data pathname is right
  533. if (strOTSSysDataPathName.IsEmpty())
  534. {
  535. // failed to get company system data pathname
  536. LogErrorTrace(__FILE__, __LINE__, _T("GetLicenseInfoFilePathName: failed to get company system data pathname."));
  537. return _T("");
  538. }
  539. // license info file pathname
  540. // i.e. "c:\ProgramData\Config\OTSLicenseInfo.lcf"
  541. CString strLicenseInfoFileNathName = strOTSSysDataPathName + LICENSE_INFO_FILE_NAME;
  542. return strLicenseInfoFileNathName;
  543. }
  544. // check if COTSLicenseInfo is valid license info
  545. BOOL COTSLicMgr::IsValidLicense(OTS_SOFT_PACKAGE_ID a_nPackId,
  546. COTSLicenseInfoPtr a_poLicenseInfo,
  547. OTS_LICENSE_STATUS& a_nLicStatus,
  548. BOOL a_bCheckMachinId /*= TRUE*/,
  549. BOOL a_bTraceInfo /* = TRUE*/)
  550. {
  551. // make sure license file is valid
  552. ASSERT(a_poLicenseInfo);
  553. if (!a_poLicenseInfo)
  554. {
  555. // invalid license info pointer
  556. a_nLicStatus = OTS_LICENSE_STATUS::INVALID;
  557. LogErrorTrace(__FILE__, __LINE__, _T("IsValidLicense: invalid license info pointer."));
  558. return FALSE;
  559. }
  560. // computer nick name
  561. CString strComputerNickName = a_poLicenseInfo->GetComputerNickName();
  562. strComputerNickName.Trim();
  563. if (strComputerNickName.IsEmpty())
  564. {
  565. // nick name is empty
  566. if(a_bTraceInfo)
  567. {
  568. LogTrace(__FILE__, __LINE__, _T("IsValidLicense: computer nick name is empty."));
  569. }
  570. a_nLicStatus = OTS_LICENSE_STATUS::COMPUTER_NICK_NAME_EMPTY;
  571. return FALSE;
  572. }
  573. // machine id
  574. CString strMachineId = a_poLicenseInfo->GetMachineId();
  575. strMachineId.Trim();
  576. if (strMachineId.GetLength() != MACHINEID_STR_LEN)
  577. {
  578. // invalid machine id string
  579. if (a_bTraceInfo)
  580. {
  581. LogTrace(__FILE__, __LINE__, _T("IsValidLicense: invalid machine id string."));
  582. }
  583. a_nLicStatus = OTS_LICENSE_STATUS::INVALID_FILE;
  584. return FALSE;
  585. }
  586. else if (a_bCheckMachinId)
  587. {
  588. CString strLocalMachinId = COTSHelper::GetMachineId();
  589. if (strMachineId.Compare(strLocalMachinId) != 0)
  590. {
  591. // machine id not match
  592. if (a_bTraceInfo)
  593. {
  594. LogTrace(__FILE__, __LINE__, _T("IsValidLicense: machine id not match. license: %s, local machine: %s"), strMachineId, strLocalMachinId);
  595. }
  596. a_nLicStatus = OTS_LICENSE_STATUS::MACHINEID_NOT_MATCH;
  597. return FALSE;
  598. }
  599. }
  600. // software package id
  601. if (a_poLicenseInfo->GetPackId() < OTS_SOFT_PACKAGE_ID::MIN || a_poLicenseInfo->GetPackId() > OTS_SOFT_PACKAGE_ID::MAX)
  602. {
  603. if (a_bTraceInfo)
  604. {
  605. LogTrace(__FILE__, __LINE__, _T("IsValidLicense: invalid software pack id."));
  606. }
  607. a_nLicStatus = OTS_LICENSE_STATUS::INVALID_FILE;
  608. return FALSE;
  609. }
  610. else if (a_nPackId != a_poLicenseInfo->GetPackId())
  611. {
  612. // software package id not match
  613. if (a_bTraceInfo)
  614. {
  615. LogTrace(__FILE__, __LINE__, _T("IsValidLicense: software pack id doesn't match."));
  616. }
  617. a_nLicStatus = OTS_LICENSE_STATUS::SOFTWARE_PACKID_NOT_MATCH;
  618. return FALSE;
  619. }
  620. // license type
  621. if (a_poLicenseInfo->GetLicType() < OTS_LICENSE_TYPE::MIN || a_poLicenseInfo->GetLicType() > OTS_LICENSE_TYPE::MAX)
  622. {
  623. if (a_bTraceInfo)
  624. {
  625. LogTrace(__FILE__, __LINE__, _T("IsValidLicense: invalid license type id."));
  626. }
  627. a_nLicStatus = OTS_LICENSE_STATUS::INVALID_FILE;
  628. return FALSE;
  629. }
  630. // expire date
  631. COleDateTime timeCurrent(COleDateTime::GetCurrentTime());
  632. if (timeCurrent >= a_poLicenseInfo->GetExpireDate())
  633. {
  634. // expired
  635. if (a_bTraceInfo)
  636. {
  637. LogTrace(__FILE__, __LINE__, _T("IsValidLicense: expired."));
  638. }
  639. a_nLicStatus = OTS_LICENSE_STATUS::EXPIRED;
  640. return FALSE;
  641. }
  642. // is close to expire?
  643. COleDateTimeSpan oLeftTime = a_poLicenseInfo->GetExpireDate() - timeCurrent;
  644. int nLeftDays = oLeftTime.GetDays();
  645. if (nLeftDays <= CLOSE_OVERDUE_DAYS)
  646. {
  647. // close to overdue
  648. if (a_bTraceInfo)
  649. {
  650. LogTrace(__FILE__, __LINE__, _T("IsValidLicense: nearly expired."));
  651. }
  652. a_nLicStatus = OTS_LICENSE_STATUS::CLOSE_OVERDUE;
  653. return TRUE;
  654. }
  655. // Ok, return TRUE
  656. a_nLicStatus = OTS_LICENSE_STATUS::VALID;
  657. return TRUE;
  658. }
  659. // load a license file (license key file)
  660. COTSLicenseInfoPtr COTSLicMgr::LoadLicenseInfoFromFile()
  661. {
  662. // file pathname
  663. CString strFilePathName = _T("");
  664. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  665. // open file dialog
  666. CFileDialog dlg(TRUE, LICENSEKEY_FILE_EXT, NULL, OFN_FILEMUSTEXIST, LICENSEKEY_FILTER);
  667. if (dlg.DoModal() != IDOK)
  668. {
  669. return nullptr;
  670. }
  671. // get file pathname
  672. strFilePathName = dlg.GetPathName();
  673. // create a license file pointer
  674. COTSLicenseFilePtr pOTSLicenseFilePtr = COTSLicenseFilePtr(new COTSLicenseFile());
  675. if (!pOTSLicenseFilePtr->Load(strFilePathName))
  676. {
  677. // failed to load the file
  678. LogTrace(__FILE__, __LINE__, _T("LoadLicenseInfoFromFile: failed to load the file %s."), strFilePathName);
  679. return nullptr;
  680. }
  681. // return license info
  682. return pOTSLicenseFilePtr->GetLicenseInfo();
  683. }
  684. // create license file for the software package pointed by the license info
  685. BOOL COTSLicMgr::CreateLicenseFile(COTSLicenseInfoPtr a_pOTSLicenseInfo)
  686. {
  687. // check the license info
  688. ASSERT(a_pOTSLicenseInfo);
  689. if (!a_pOTSLicenseInfo)
  690. {
  691. // invalid license info pointer
  692. LogErrorTrace(__FILE__, __LINE__, _T("CreateLicenseFile: invalid license info pointer."));
  693. return FALSE;
  694. }
  695. // get software package system data folder name
  696. CString strOTSPackSysDataPathName = COTSFileSys::GetOTSPackSysDataPathName(a_pOTSLicenseInfo->GetPackId());
  697. // check if the folder name is right
  698. if (strOTSPackSysDataPathName.IsEmpty())
  699. {
  700. // failed to get software package system data folder name
  701. LogErrorTrace(__FILE__, __LINE__, _T("CreateLicenseFile: failed to get software package system data folder string."));
  702. return FALSE;
  703. }
  704. // check if the path exist
  705. if (!COTSFileSys::Exists(strOTSPackSysDataPathName))
  706. {
  707. // create the folder
  708. if (!COTSFileSys::CreateFolder(strOTSPackSysDataPathName))
  709. {
  710. // failed to create software package system data folder
  711. LogErrorTrace(__FILE__, __LINE__, _T("CreateLicenseFile: failed to create software package system data folder."));
  712. return FALSE;
  713. }
  714. }
  715. // get software package license file pathname
  716. CString strFilePathName = GetPackLicenseFilePathName(a_pOTSLicenseInfo->GetPackId());
  717. strFilePathName.Trim();
  718. if (strFilePathName.IsEmpty())
  719. {
  720. LogErrorTrace(__FILE__, __LINE__, _T("IsThereValidPackLicense: failed to find software package license file pathname. software package id: %d"), (int)a_pOTSLicenseInfo->GetPackId());
  721. return FALSE;
  722. }
  723. return CreateLicenseFile( strFilePathName, a_pOTSLicenseInfo);
  724. }
  725. // create license file (a license key)
  726. BOOL COTSLicMgr::CreateLicenseFile(CString a_strFilePathName, COTSLicenseInfoPtr a_pOTSLicenseInfo)
  727. {
  728. // check if the file pathname is empty
  729. a_strFilePathName.Trim();
  730. if (a_strFilePathName.IsEmpty())
  731. {
  732. LogErrorTrace(__FILE__, __LINE__, _T("CreateLicenseFile: file pathname is empty."));
  733. return FALSE;
  734. }
  735. // check the license info
  736. ASSERT(a_pOTSLicenseInfo);
  737. if (!a_pOTSLicenseInfo)
  738. {
  739. // invalid license info pointer
  740. LogErrorTrace(__FILE__, __LINE__, _T("CreateLicenseFile: invalid license info pointer."));
  741. return FALSE;
  742. }
  743. // create a license file pointer
  744. COTSLicenseFilePtr pOTSLicenseFilePtr = COTSLicenseFilePtr(new COTSLicenseFile());
  745. pOTSLicenseFilePtr->SetLicenseInfo(a_pOTSLicenseInfo);
  746. // return save license file result
  747. return pOTSLicenseFilePtr->Save(a_strFilePathName);
  748. }
  749. // create license info from a text file
  750. BOOL COTSLicMgr::CreateLicenseInfoFileText(COTSLicenseInfoPtr a_pOTSLicenseInfo)
  751. {
  752. // check the license info
  753. ASSERT(a_pOTSLicenseInfo);
  754. if (!a_pOTSLicenseInfo)
  755. {
  756. // invalid license info pointer
  757. LogErrorTrace(__FILE__, __LINE__, _T("CreateLicenseInfoFileText: invalid license info pointer."));
  758. return FALSE;
  759. }
  760. // file save as dialog
  761. CFileDialog dlg(FALSE, TEXTFILE_FILE_EXT, NULL, OFN_OVERWRITEPROMPT, TEXTFILE_FILTER);
  762. if (dlg.DoModal() != IDOK)
  763. {
  764. return FALSE;
  765. }
  766. // get file pathname
  767. CString strPathName = dlg.GetPathName();
  768. // create the file
  769. CStdioFile file;
  770. CFileException ex;
  771. if (!file.Open(strPathName, CFile::modeCreate | CFile::modeWrite, &ex))
  772. {
  773. // failed to open file
  774. LogErrorTrace(__FILE__, __LINE__, _T("CreateLicenseInfoFileText: failed to create file. %s"), strPathName);
  775. return FALSE;
  776. }
  777. // get license info text body
  778. CString strLicenseInfoTextBody = COTSLicMgr::GetLicenseInfoTextBody(a_pOTSLicenseInfo);
  779. file.WriteString(strLicenseInfoTextBody);
  780. file.Close();
  781. // Ok, return TRUE
  782. return TRUE;
  783. }
  784. // load license info from a text file
  785. COTSLicenseInfoPtr COTSLicMgr::LoadLicenseInfoFromTextFile(CString a_strFilePathName)
  786. {
  787. // license info
  788. COTSLicenseInfoPtr pOTSLicenseInfo = COTSLicenseInfoPtr(new COTSLicenseInfo());
  789. // check file name
  790. a_strFilePathName.Trim();
  791. if (a_strFilePathName.IsEmpty())
  792. {
  793. LogErrorTrace(__FILE__, __LINE__, _T("LoadLicenseInfoFromTextFile: file pathname is empty."));
  794. return nullptr;
  795. }
  796. // load string lines from the file
  797. std::vector<CString > listLineStr = COTSHelper::LoadTextFileToCStingList(a_strFilePathName);
  798. // process string line
  799. BOOL bRet = FALSE;
  800. for (auto strLine : listLineStr)
  801. {
  802. // split the string line with ":"
  803. std::vector<CString > listStr = COTSHelper::SplitString(strLine, FILE_TITLE_SPLIT);
  804. // jump over the string if it is invalid
  805. // it should have a title string and item string
  806. if ((int)listStr.size() != TEXTFILE_ITEM_COLUMN_NUMBER)
  807. {
  808. continue;
  809. }
  810. CString strTitle = listStr[0];
  811. CString strItem = listStr[1];
  812. // get license info item
  813. bRet = ( COTSLicMgr::GetLicnseInfoItem(pOTSLicenseInfo, strTitle, strItem) || bRet);
  814. }
  815. // check read result
  816. if (!bRet)
  817. {
  818. LogErrorTrace(__FILE__, __LINE__, _T("LoadLicenseInfoFromTextFile: %s contains no license info item."), a_strFilePathName);
  819. return nullptr;
  820. }
  821. // return the license info
  822. return pOTSLicenseInfo;
  823. }
  824. // get license info item
  825. BOOL COTSLicMgr::GetLicnseInfoItem(COTSLicenseInfoPtr a_pOTSLicenseInfo, CString a_strTitle, CString a_strValue)
  826. {
  827. // make sure license info handle is valid
  828. ASSERT(a_pOTSLicenseInfo);
  829. if (!a_pOTSLicenseInfo)
  830. {
  831. // invalid license info pointer
  832. LogErrorTrace(__FILE__, __LINE__, _T("GetLicnseInfoItem: invalid license info pointer."));
  833. return FALSE;
  834. }
  835. // check title, value strings
  836. a_strTitle.Trim();
  837. a_strValue.Trim();
  838. if (a_strTitle.IsEmpty() || a_strValue.IsEmpty())
  839. {
  840. LogErrorTrace(__FILE__, __LINE__, _T("GetLicnseInfoItem: title string or value string is empty."));
  841. return FALSE;
  842. }
  843. // license info item
  844. for (int i = 0; i <= (int)OTS_LICENSE_INFO_ITEMS::MAX; ++i)
  845. {
  846. CString strFileItemTitle;
  847. strFileItemTitle= MultiLang::GetInstance().GetCStringByKey(IDS_LICENSE_INFO_TITLE_FIRST + i);
  848. if (a_strTitle.CompareNoCase(strFileItemTitle) == 0)
  849. {
  850. switch ((OTS_LICENSE_INFO_ITEMS)i)
  851. {
  852. case OTS_LICENSE_INFO_ITEMS::COMPUTER_NICK_NAME:
  853. {
  854. a_pOTSLicenseInfo->SetComputerNickName(a_strValue);
  855. return TRUE;
  856. }
  857. break;
  858. case OTS_LICENSE_INFO_ITEMS::MACHINE_ID:
  859. {
  860. if (a_strValue.GetLength() == MACHINEID_STR_LEN)
  861. {
  862. a_pOTSLicenseInfo->SetMachineId(a_strValue);
  863. return TRUE;
  864. }
  865. }
  866. break;
  867. case OTS_LICENSE_INFO_ITEMS::SOFTWARE_PACK_ID:
  868. {
  869. int nPackId;
  870. if (COTSHelper::StringToInt(a_strValue, nPackId) &&
  871. (OTS_SOFT_PACKAGE_ID)nPackId >= OTS_SOFT_PACKAGE_ID::MIN &&
  872. (OTS_SOFT_PACKAGE_ID)nPackId <= OTS_SOFT_PACKAGE_ID::MAX)
  873. {
  874. a_pOTSLicenseInfo->SetPackId((OTS_SOFT_PACKAGE_ID)nPackId);
  875. return TRUE;
  876. }
  877. }
  878. break;
  879. case OTS_LICENSE_INFO_ITEMS::LICENSE_TYPE:
  880. {
  881. int nLicType;
  882. if (COTSHelper::StringToInt(a_strValue, nLicType) &&
  883. (OTS_LICENSE_TYPE)nLicType >= OTS_LICENSE_TYPE::MIN &&
  884. (OTS_LICENSE_TYPE)nLicType <= OTS_LICENSE_TYPE::MAX)
  885. {
  886. a_pOTSLicenseInfo->SetLicType((OTS_LICENSE_TYPE)nLicType);
  887. return TRUE;
  888. }
  889. }
  890. break;
  891. case OTS_LICENSE_INFO_ITEMS::EXPIRE_DATE:
  892. {
  893. // Expire data format "%d%m%Y"
  894. if (COTSHelper::IsDigitString(a_strValue) && a_strValue.GetLength() == EXPIRE_DATE_STR_LENGTH)
  895. {
  896. CString strDay = a_strValue.Left(2);
  897. int nDay = 0;
  898. COTSHelper::StringToInt(strDay, nDay);
  899. CString strMonth = a_strValue.Mid(2, 2);
  900. int nMonth = 0;
  901. COTSHelper::StringToInt(strMonth, nMonth);
  902. CString strYear = a_strValue.Right(4);
  903. int nYear = 0;
  904. COTSHelper::StringToInt(strYear, nYear);
  905. COleDateTime oDataTime;
  906. oDataTime.SetDate(nYear, nMonth, nDay);
  907. a_pOTSLicenseInfo->SetExpireDate(oDataTime);
  908. return TRUE;
  909. }
  910. }
  911. break;
  912. }
  913. // value string is invalid , return FALSE
  914. LogErrorTrace(__FILE__, __LINE__, _T("GetLicnseInfoItem: title string or value string is empty. title: %s value: %s"), a_strTitle, a_strValue);
  915. return FALSE;
  916. }
  917. }
  918. // can't find matching title, return FALSE
  919. LogErrorTrace(__FILE__, __LINE__, _T("GetLicnseInfoItem: can't find matching title. title: %s"), a_strTitle);
  920. return FALSE;
  921. }
  922. // get software pack id string
  923. CString COTSLicMgr::GetSoftwarePackIdString(OTS_SOFT_PACKAGE_ID a_nPackId)
  924. {
  925. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  926. CString strPackId = _T("");
  927. if (a_nPackId >= OTS_SOFT_PACKAGE_ID::MIN && a_nPackId <= OTS_SOFT_PACKAGE_ID::MAX)
  928. {
  929. strPackId=MultiLang::GetInstance().GetCStringByKey(IDS_SOFTWARETYPEID_FIRST + (int)a_nPackId);
  930. }
  931. return strPackId;
  932. }
  933. // get license type string
  934. CString COTSLicMgr::GetLicenseTypeIdString(OTS_LICENSE_TYPE a_nLicTypeId)
  935. {
  936. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  937. CString strLicType = _T("");
  938. if (a_nLicTypeId >= OTS_LICENSE_TYPE::MIN && a_nLicTypeId <= OTS_LICENSE_TYPE::MAX)
  939. {
  940. strLicType=MultiLang::GetInstance().GetCStringByKey(IDS_LICENSETYPEID_FIRST + (int)a_nLicTypeId);
  941. }
  942. return strLicType;
  943. }
  944. // create an encrypt string containing the given info
  945. CString COTSLicMgr::EncryptLicenseInfo(COTSLicenseInfoPtr a_pOTSLicenseInfo)
  946. {
  947. // make sure license info handle is valid
  948. ASSERT(a_pOTSLicenseInfo);
  949. if (!a_pOTSLicenseInfo)
  950. {
  951. // invalid license info pointer
  952. LogErrorTrace(__FILE__, __LINE__, _T("GetLicnseInfoItem: invalid license info pointer."));
  953. return _T("");
  954. }
  955. // license string
  956. // computer machine id string -- length MACHINEID_STR_LEN (19)
  957. // software package id string -- length 1
  958. // license type id string -- length 1
  959. // expired date string -- length 8
  960. // computer nick name
  961. CString strPackId;
  962. strPackId.Format(_T("%i"), (int)a_pOTSLicenseInfo->GetPackId());
  963. CString strLicType;
  964. strLicType.Format(_T("%i"), (int)a_pOTSLicenseInfo->GetLicType());
  965. CString strExpiredDate;
  966. strExpiredDate = a_pOTSLicenseInfo->GetExpireDate().Format(EXPIRE_DATA_FORMAT);
  967. CString strLicense;
  968. strLicense = a_pOTSLicenseInfo->GetMachineId() + strPackId + strLicType + strExpiredDate + a_pOTSLicenseInfo->GetComputerNickName();
  969. // encrypt license string
  970. COTSCrypt oCrypt;
  971. CString strKey = CRYTP_KEY_STRING;
  972. try
  973. {
  974. oCrypt.Encrypt(strKey, strLicense);
  975. }
  976. catch(...)
  977. {
  978. LogErrorTrace(__FILE__, __LINE__, _T("EncryptLicenseInfo: failed to encrypt license string."));
  979. }
  980. // return the encrypt license string
  981. return strLicense;
  982. }
  983. COTSLicenseInfoPtr COTSLicMgr::DecryptLicenseInfo(CString a_strLicense)
  984. {
  985. // make sure the license string is valid
  986. a_strLicense.Trim();
  987. if (a_strLicense.IsEmpty())
  988. {
  989. // invalid license string
  990. LogErrorTrace(__FILE__, __LINE__, _T("DecryptLicenseInfo: license string is empty."));
  991. return nullptr;
  992. }
  993. // decrypt the license string
  994. COTSCrypt oCrypt;
  995. CString strKey = CRYTP_KEY_STRING;
  996. try
  997. {
  998. oCrypt.Decrypt(strKey, a_strLicense);
  999. }
  1000. catch (...)
  1001. {
  1002. LogErrorTrace(__FILE__, __LINE__, _T("DecryptLicenseInfo: failed to decrypt license string."));
  1003. }
  1004. // make sure license string is longer enough
  1005. if (a_strLicense.GetLength() < LICENSE_STR_MIN_LENGTH)
  1006. {
  1007. // invalid license string
  1008. return nullptr;
  1009. }
  1010. // separate the license string
  1011. // computer machine id string -- length MACHINEID_STR_LEN (19)
  1012. // software package id string -- length 1
  1013. // license type id string -- length 1
  1014. // expired date string -- EXPIRED_DATE_STR_LENGTH (8)
  1015. // computer nick name
  1016. CString strMachineId = a_strLicense.Left(MACHINEID_STR_LEN);
  1017. CString strPackId = a_strLicense.Mid(MACHINEID_STR_LEN, 1);
  1018. CString strLicTypeId = a_strLicense.Mid(MACHINEID_STR_LEN + 1, 1);
  1019. CString strExpiredDate = a_strLicense.Mid(MACHINEID_STR_LEN + 1 + 1, EXPIRE_DATE_STR_LENGTH);
  1020. int nComputerNickNameStrLength = a_strLicense.GetLength() - MACHINEID_STR_LEN - 1 - 1 - EXPIRE_DATE_STR_LENGTH;
  1021. CString strComputerNickName = a_strLicense.Right(nComputerNickNameStrLength);
  1022. // create an license info
  1023. COTSLicenseInfoPtr pOTSLicenseInfo = COTSLicenseInfoPtr(new COTSLicenseInfo());
  1024. // assign machine id
  1025. strMachineId.Trim();
  1026. if (strMachineId.GetLength() != MACHINEID_STR_LEN)
  1027. {
  1028. // invalid machine id
  1029. return nullptr;
  1030. }
  1031. pOTSLicenseInfo->SetMachineId(strMachineId);
  1032. // assign software package id
  1033. int nPackId;
  1034. if (!COTSHelper::StringToInt(strPackId, nPackId) ||
  1035. (OTS_SOFT_PACKAGE_ID)nPackId < OTS_SOFT_PACKAGE_ID::MIN ||
  1036. (OTS_SOFT_PACKAGE_ID)nPackId > OTS_SOFT_PACKAGE_ID::MAX)
  1037. {
  1038. // invalid package id value
  1039. return nullptr;
  1040. }
  1041. pOTSLicenseInfo->SetPackId((OTS_SOFT_PACKAGE_ID)nPackId);
  1042. // assign license type
  1043. int nLicType;
  1044. if (!COTSHelper::StringToInt(strLicTypeId, nLicType) ||
  1045. (OTS_LICENSE_TYPE)nLicType < OTS_LICENSE_TYPE::MIN ||
  1046. (OTS_LICENSE_TYPE)nLicType > OTS_LICENSE_TYPE::MAX)
  1047. {
  1048. // invalid license type value
  1049. return nullptr;
  1050. }
  1051. pOTSLicenseInfo->SetLicType((OTS_LICENSE_TYPE)nLicType);
  1052. // assign expire data
  1053. // Expire data format "%d%m%Y"
  1054. if (!COTSHelper::IsDigitString(strExpiredDate))
  1055. {
  1056. // invalid expire data string
  1057. return nullptr;
  1058. }
  1059. CString strDay = strExpiredDate.Left(2);
  1060. int nDay = 0;
  1061. COTSHelper::StringToInt(strDay, nDay);
  1062. CString strMonth = strExpiredDate.Mid(2,2);
  1063. int nMonth = 0;
  1064. COTSHelper::StringToInt(strMonth, nMonth);
  1065. CString strYear = strExpiredDate.Right(4);
  1066. int nYear = 0;
  1067. COTSHelper::StringToInt(strYear, nYear);
  1068. COleDateTime oDataTime;
  1069. oDataTime.SetDate(nYear, nMonth, nDay);
  1070. pOTSLicenseInfo->SetExpireDate(oDataTime);
  1071. // assign computer nick name
  1072. strComputerNickName.Trim();
  1073. if (strComputerNickName.IsEmpty())
  1074. {
  1075. // invalid computer nick name
  1076. return nullptr;
  1077. }
  1078. pOTSLicenseInfo->SetComputerNickName(strComputerNickName);
  1079. // return the license info
  1080. return pOTSLicenseInfo;
  1081. }
  1082. // get license info text body
  1083. CString COTSLicMgr::GetLicenseInfoTextBody(COTSLicenseInfoPtr a_pOTSLicenseInfo)
  1084. {
  1085. // license info text body
  1086. CString strLicenseInfoTextBody = _T("");
  1087. // make sure license info handle is valid
  1088. ASSERT(a_pOTSLicenseInfo);
  1089. if (!a_pOTSLicenseInfo)
  1090. {
  1091. // invalid license info pointer
  1092. LogErrorTrace(__FILE__, __LINE__, _T("GetLicenseInfoTextBody: invalid license info pointer."));
  1093. return strLicenseInfoTextBody;
  1094. }
  1095. CString strTitle;
  1096. // nick name
  1097. strTitle=MultiLang::GetInstance().GetCStringByKey(IDS_LICENSE_INFO_TITLE_FIRST);
  1098. strLicenseInfoTextBody = strTitle + FILE_TITLE_SPLIT + _T(" ") + a_pOTSLicenseInfo->GetComputerNickName() + LINE_END;
  1099. // machine id
  1100. strTitle=MultiLang::GetInstance().GetCStringByKey(IDS_LICENSE_INFO_TITLE_FIRST + 1);
  1101. strLicenseInfoTextBody += strTitle + FILE_TITLE_SPLIT + _T(" ") + a_pOTSLicenseInfo->GetMachineId() + LINE_END;
  1102. // software package id
  1103. strTitle=MultiLang::GetInstance().GetCStringByKey(IDS_LICENSE_INFO_TITLE_FIRST + 2);
  1104. CString strPackId;
  1105. strPackId.Format(_T("%i"), (int)a_pOTSLicenseInfo->GetPackId());
  1106. strLicenseInfoTextBody += strTitle + FILE_TITLE_SPLIT + _T(" ") + strPackId + LINE_END;
  1107. // license type id
  1108. strTitle=MultiLang::GetInstance().GetCStringByKey(IDS_LICENSE_INFO_TITLE_FIRST + 3);
  1109. CString strLicType;
  1110. strLicType.Format(_T("%i"), (int)a_pOTSLicenseInfo->GetLicType());
  1111. strLicenseInfoTextBody += strTitle + FILE_TITLE_SPLIT + _T(" ") + strLicType + LINE_END;
  1112. // expire date
  1113. strTitle=MultiLang::GetInstance().GetCStringByKey(IDS_LICENSE_INFO_TITLE_FIRST + 4);
  1114. CString strExpiredDate;
  1115. strExpiredDate = a_pOTSLicenseInfo->GetExpireDate().Format(EXPIRE_DATA_FORMAT);
  1116. strLicenseInfoTextBody += strTitle + FILE_TITLE_SPLIT + _T(" ") + strExpiredDate + LINE_END;
  1117. // return license info text body
  1118. return strLicenseInfoTextBody;
  1119. }
  1120. // private:
  1121. // is valid machine id string
  1122. BOOL COTSLicMgr::IsValidMachineIdString(CString a_strMachineId)
  1123. {
  1124. a_strMachineId.Trim();
  1125. return (!a_strMachineId.IsEmpty() && a_strMachineId.GetLength() == MACHINEID_STR_LEN);
  1126. }
  1127. }