#include "stdafx.h"
#include "WebUtil.h"
#include "Wininet.h"
#include "atlenc.h"
#include "../CommLib/commlib.h"

#pragma comment( lib, "Wininet.lib" )
#import <msxml.dll>
#include <comutil.h>
#include ".\webutil.h"

CWebUtil::CWebUtil()
{
}

CWebUtil::~CWebUtil()
{
}


int CWebUtil::kp_process_errors(int nError,MODIFY_TYPE Type)
{
	switch(nError)
	{
	case UNDEFINED_ERROR:			// 알수 없는 오류
		AfxMessageBox(_T("메타서버 오류 : 알수없는 오류"));
		break;
	case NOADMIT_SET_FAIL:			// 무인증 요청 실패
		AfxMessageBox(_T("메타서버 오류 : 무인증 요청 실패"));
		break;
	case ADMIT_SET_FAIL:			// 무인증 해제 실패
		AfxMessageBox(_T("메타서버 오류 : 무인증 해제 실패"));
		break;
	case LOGIN_FAIL_NOID:			// 로그인 실패 : 아이디 없음
		AfxMessageBox(_T("메타서버 오류 : 해당 아이디가 존재하지 않습니다."));
		break;
	case LOGIN_FAIL_NOPASSWD:		// 로그인 실패 : 패스워드 틀림
		AfxMessageBox(_T("메타서버 오류 : 비밀번호가 잘못 되었습니다."));
		break;
	case CANNOT_WRITE:				// 파일 업로드 권한 없음
		AfxMessageBox(_T("메타서버 오류 : 파일 업로드 권한 없음"));
		break;
	case NO_QUOTA:					// 파일 업로드 용량 없음
		AfxMessageBox(_T("메타서버 오류 : 파일 업로드 용량 없음"));
		break;
	case TARGET_FILE_EXIST:			// 파일 수정 불가 : 대상 파일이 이미 존재함
		AfxMessageBox(_T("메타서버 오류 : 파일 수정 불가 : 대상 파일이 이미 존재함"));
		break;
	default:
		break;
	}

	return nError;
}


PCLFILE_STRUCTEX CWebUtil::kp_parse_file_list_struct(CString sData, unsigned int *count)
{
	PCLFILE_STRUCTEX pclf = NULL;

	MSXML::IXMLDOMDocumentPtr				pDoc;

	try
	{
		pDoc.CreateInstance(__uuidof(DOMDocument));
		HRESULT hr = pDoc->loadXML(_bstr_t(sData));

		if(VARIANT_TRUE == hr)
		{
			MSXML::IXMLDOMElementPtr				pRecordSetInfo;
			MSXML::IXMLDOMNodeListPtr				pRecordSet;
			MSXML::IXMLDOMElementPtr				pRecordInfo;
			MSXML::IXMLDOMNodeListPtr				pFieldList;
			MSXML::IXMLDOMElementPtr				pFieldInfo;

			CString szRecordCount = _T("0");

			pRecordSetInfo = pDoc->selectSingleNode("//filelist");
			if(pRecordSetInfo)
				pRecordSet = pRecordSetInfo->childNodes;

			szRecordCount = pRecordSetInfo->getAttribute("count");
			int nCount = atol(kp_get_unbase64_data(szRecordCount));

			if (nCount ==0)
				return pclf;

			pclf = new CLFILE_STRUCTEX[nCount];

			*count = nCount;

			for(int i = 0; i <= pRecordSet->length; i++)
			{
				pRecordInfo = pRecordSet->item[i];
				if(NULL == pRecordInfo) continue;

				pFieldList = pRecordInfo->childNodes;
				if(NULL == pFieldList) continue;

				CString szName = kp_get_decoded_data(kp_get_unbase64_data(pRecordInfo->getAttribute("name")));
				CString szFlag = kp_get_decoded_data(kp_get_unbase64_data(pRecordInfo->getAttribute("flag")));

				if(szFlag[0] == 'D')
					pclf[i].attr = CLFILEATTR_FOLDER;
				else
					pclf[i].attr = 0;

				strcpy(pclf[i].path,szName);

				for(int j = 0; j < pFieldList->length; j++)
				{
					pFieldInfo = pFieldList->item[j];

					if(NULL == pFieldInfo) break;

					CString szNodeName = pFieldInfo->nodeName;
					CString szTemp = pFieldInfo->text;
					CString szValue = kp_get_decoded_data(kp_get_unbase64_data(szTemp));

					if (szNodeName == "size")
						pclf[i].size = _atoi64(szValue);

					else if (szNodeName == "updateip")
						strcpy(pclf[i].ip,szValue);
					else if (szNodeName == "updatetime")
					{
						CTime fTime((time_t)atol(szValue));
						struct tm* osTime;

						osTime = fTime.GetLocalTm(NULL);

						sprintf(pclf[i].lastModified,"%04d-%02d-%02d %s %02d:%02d:%02d",
							osTime->tm_year+1900,
							osTime->tm_mon +1,
							osTime->tm_mday,
							osTime->tm_hour > 12 ? "오후" : "오전",
							osTime->tm_hour > 12 ? osTime->tm_hour - 12 : osTime->tm_hour,
							osTime->tm_min,
							osTime->tm_sec);
					}
					else if (szNodeName == "hit_today")
						pclf[i].hit_today = atol(szValue);
					else if (szNodeName == "hit_yesterday")
						pclf[i].hit_yesterday = atol(szValue);
					else if (szNodeName == "hit_avr")
						pclf[i].hit_avr = atol(szValue);
					else if (szNodeName == "per_success")
						pclf[i].per_success = atol(szValue);
					else if (szNodeName == "total_size")
						pclf[i].total_size = _atoi64(szValue);
					else if (szNodeName == "used_size")
						pclf[i].used_size = _atoi64(szValue);
					else if (szNodeName == "traffic")
						pclf[i].traffic = _atoi64(szValue);
					else if (szNodeName == "today_traffic")
						pclf[i].today_traffic = _atoi64(szValue);
					else if (szNodeName == "is_service")
						strncpy(pclf[i].service_status, szValue , 2);
					else if (szNodeName == "backup")
						strcpy(pclf[i].backup_path, szValue);
					//					strcpy(pclf[i].backup_path, "/TEMP/szValue/");

				}
			}
		}
	}
	catch(CException* )
	{
		return NULL;
	}

	return pclf;
}

PCLFILE_STRUCTEX CWebUtil::kp_get_file_list(const char *path, unsigned int depth, unsigned int *count)
{
	CString strTemp;
	CString sModNum;
	CString strPath;

	if (path[0] == 0x00)
		strPath.Format(_T("/"));
	else
		strPath.Format(_T("%s"), path);

	switch(depth)
	{
	case 2: // 서비스정보
		sModNum = _T("10");
		break;
	case 1: // 현재 레벨의 파일 리스트
		sModNum = _T("3");
		break;
	case 0: // 자기 자신 정보
		sModNum = _T("9");
		break;
	}

	strTemp = kp_get_crypted_data(sModNum) + DELIM + 
		kp_get_crypted_data(m_cpid) + DELIM +
		kp_get_crypted_data(m_loginid) + DELIM + 
		kp_get_crypted_data(strPath);


	TRACE(_T("\nkp_get_file_list : %s | %s | %s \n"),m_cpid,m_loginid,strPath);

	strTemp = kp_recv_data_http(_T("p=") + kp_get_base64_data(strTemp),true);

	strTemp.Trim();

	//	strTemp = kp_get_unbase64_data(strTemp);

	return kp_parse_file_list_struct(strTemp,count);
}

void CWebUtil::kp_free_file_list(PCLFILE_STRUCTEX pclf)
{
	if (!pclf)
		return;

	delete []pclf;
}

int	CWebUtil::kp_init(CString cp_id,CString cp_user_id, CString pass)
{
	m_cpid = cp_id;
	m_loginid = cp_user_id;
	m_pass = pass;

	return KP_ERROR_SUCCESS;
}

CString CWebUtil::kp_get_crypted_data(CString strData)
{
	return kp_get_base64_data(kp_get_encoded_data(strData));
}

int CWebUtil::kp_set_annoy(CString sPath,BOOL bSet)
{
	CString strTemp;

	strTemp = kp_get_crypted_data(_T("1")) + DELIM +
		kp_get_crypted_data(sPath) + DELIM +
		kp_get_crypted_data(bSet ? _T("1") : _T("0"));
	/*
	strTemp.Format(_T("%s%s%s%s%s"),
	kp_get_crypted_data(_T("1")),DELIM,
	kp_get_crypted_data(sPath),DELIM,
	kp_get_crypted_data(bSet ? _T("1") : _T("0")));
	*/
	TRACE(_T("\nkp_set_annoy : %s | %s\n"),sPath,bSet ? _T("1") : _T("0"));

	strTemp = kp_recv_data_http(_T("p=") + kp_get_base64_data(strTemp),true);

	strTemp.Trim();

	return kp_process_errors(atol(strTemp));
}

int CWebUtil::kp_chk_login(BOOL bisNetdrive)
{
	CString strTemp;

	strTemp = kp_get_crypted_data(_T("2")) + DELIM + 
		kp_get_crypted_data(m_cpid) + DELIM +
		kp_get_crypted_data(m_loginid) + DELIM +
		kp_get_crypted_data(m_pass) + DELIM +
		kp_get_crypted_data(bisNetdrive ? _T("1") : _T(""));

	//	if(!bisNetdrive)
	//		strTemp += kp_get_crypted_data(_T("1");

	TRACE(_T("\nkp_chk_login : %s | %s | %s\n"),m_cpid,m_loginid,m_pass);

	strTemp = kp_recv_data_http(_T("p=") + kp_get_base64_data(strTemp),true);

	strTemp.Trim();

	return kp_process_errors(atol(strTemp));
}

int CWebUtil::kp_chk_upload(CString sPath,CString sFileName,LARGE_INTEGER nSize,BOOL bDir)
{
	CString strTemp;
	CString strSize;

	strSize.Format(_T("%I64d"),nSize.QuadPart);

	strTemp = kp_get_crypted_data(_T("4")) + DELIM + 
		kp_get_crypted_data(m_cpid) + DELIM +
		kp_get_crypted_data(m_loginid) + DELIM +
		kp_get_crypted_data(sPath) + DELIM +
		kp_get_crypted_data(sFileName) + DELIM +
		kp_get_crypted_data(bDir?_T("D"):_T("F")) + DELIM +
		kp_get_crypted_data(strSize);
	/*
	strTemp.Format(_T("%s%s%s%s%s%s%s%s%s%s%s%s%s"),
	kp_get_crypted_data(_T("4")),DELIM,
	kp_get_crypted_data(m_cpid),DELIM,
	kp_get_crypted_data(m_loginid),DELIM,
	kp_get_crypted_data(sPath),DELIM,
	kp_get_crypted_data(sFileName),DELIM,
	kp_get_crypted_data(bDir?_T("D"):_T("F")),DELIM,
	kp_get_crypted_data(strSize));
	*/	
	TRACE(_T("\nkp_chk_upload : %s | %s | %s | %s | %s\n"),m_cpid,m_loginid,sPath,sFileName,strSize);

	strTemp = kp_recv_data_http(_T("p=") + kp_get_base64_data(strTemp),true);

	strTemp.Trim();
	return kp_process_errors(atol(strTemp));
}

int CWebUtil::kp_set_start_upload(CString sPath, CString sFileName, LARGE_INTEGER nSize,BOOL bIsDir, BOOL bOverWrite)
{
	CString strTemp;
	CString strSize;

	strSize.Format(_T("%I64d"),nSize.QuadPart);

	strTemp = kp_get_crypted_data(_T("5")) + DELIM + 
		kp_get_crypted_data(m_cpid) + DELIM +
		kp_get_crypted_data(m_loginid) + DELIM +
		kp_get_crypted_data(sPath) + DELIM +
		kp_get_crypted_data(sFileName) + DELIM + 
		kp_get_crypted_data(strSize) + DELIM +
		kp_get_crypted_data(bIsDir?_T("D"):_T("F")) + DELIM +
		kp_get_crypted_data(bOverWrite?_T("1"):_T("0"));
	/*
	strTemp.Format(_T("%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s"),
	kp_get_crypted_data(_T("5")),DELIM,
	kp_get_crypted_data(m_cpid),DELIM,
	kp_get_crypted_data(m_loginid),DELIM,
	kp_get_crypted_data(sPath),DELIM,
	kp_get_crypted_data(sFileName),DELIM,
	kp_get_crypted_data(strSize),DELIM,
	kp_get_crypted_data(bIsDir?_T("D"):_T("F")),DELIM,
	kp_get_crypted_data(bOverWrite?_T("1"):_T("0"))
	);
	*/	
	TRACE(_T("\nkp_set_start_upload : %s | %s | %s | %s | %s | %s | %s\n"),
	m_cpid,m_loginid,sPath,sFileName,strSize,bIsDir?_T("D"):_T("F"),bOverWrite?_T("1"):_T("0"));

	strTemp = kp_recv_data_http(_T("p=") + kp_get_base64_data(strTemp),true);

	strTemp.Trim();
	return kp_process_errors(atol(strTemp));
}

int CWebUtil::kp_set_end_upload(CString sPath, CString sFileName, LARGE_INTEGER nSize,BOOL bSuccess)
{
	CString strTemp;
	CString strSize;

	strSize.Format(_T("%I64d"),nSize.QuadPart);

	strTemp = kp_get_crypted_data(_T("6")) + DELIM +
		kp_get_crypted_data(m_cpid) + DELIM +
		kp_get_crypted_data(m_loginid) + DELIM +
		kp_get_crypted_data(sPath) + DELIM +
		kp_get_crypted_data(sFileName) + DELIM +
		kp_get_crypted_data(strSize) + DELIM +
		kp_get_crypted_data(bSuccess?_T("1"):_T("0"));
	/*
	strTemp.Format(_T("%s%s%s%s%s%s%s%s%s%s%s%s%s"),
	kp_get_crypted_data(_T("6")),DELIM,
	kp_get_crypted_data(m_cpid),DELIM,
	kp_get_crypted_data(m_loginid),DELIM,
	kp_get_crypted_data(sPath),DELIM,
	kp_get_crypted_data(sFileName),DELIM,
	kp_get_crypted_data(strSize),DELIM,
	kp_get_crypted_data(bSuccess?_T("1"):_T("0")));
	*/	
	TRACE(_T("\nkp_set_end_upload : %s | %s | %s | %s | %s\n"),
	m_cpid,m_loginid,sPath,sFileName,strSize);

	strTemp = kp_recv_data_http(_T("p=") + kp_get_base64_data(strTemp),true);

	strTemp.Trim();
	return kp_process_errors(atol(strTemp));
}

int CWebUtil::kp_set_start_modify(CString sSPath, CString sSFileName, LARGE_INTEGER nSSize,
								  CString sDPath, CString sDFileName, LARGE_INTEGER nDSize, MODIFY_TYPE Type)
{
	CString strTemp;
	CString strSSize;
	CString strDSize;
	CString sFlag;

	strSSize.Format(_T("%I64d"),nSSize.QuadPart);
	strDSize.Format(_T("%I64d"),nDSize.QuadPart);

	strTemp = kp_get_crypted_data(_T("7")) + DELIM +
		kp_get_crypted_data(m_cpid) + DELIM +
		kp_get_crypted_data(m_loginid) + DELIM +
		kp_get_crypted_data(sSPath) + DELIM +
		kp_get_crypted_data(sSFileName) + DELIM +
		kp_get_crypted_data(strSSize) + DELIM +
		kp_get_crypted_data(sDPath) + DELIM +
		kp_get_crypted_data(sDFileName) + DELIM +
		kp_get_crypted_data(strDSize) + DELIM +
		kp_get_crypted_data(modify_types[Type]);
	/*
	strTemp.Format(_T("%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s"),
	kp_get_crypted_data(_T("7")),DELIM,
	kp_get_crypted_data(m_cpid),DELIM,
	kp_get_crypted_data(m_loginid),DELIM,
	kp_get_crypted_data(sSPath),DELIM,
	kp_get_crypted_data(sSFileName),DELIM,
	kp_get_crypted_data(strSSize),DELIM,
	kp_get_crypted_data(sDPath),DELIM,
	kp_get_crypted_data(sDFileName),DELIM,
	kp_get_crypted_data(strDSize),DELIM,
	kp_get_crypted_data(modify_types[Type]));
	*/	
	TRACE(_T("\nkp_set_start_modify : %s | %s | %s | %s | %s | %s | %s | %s | %s\n"),
	m_cpid,m_loginid,sSPath,sSFileName,strSSize,sDPath,sDFileName,strDSize,modify_types[Type]);

	strTemp = kp_recv_data_http(_T("p=") + kp_get_base64_data(strTemp),true);

	strTemp.Trim();
	return kp_process_errors(atol(strTemp),Type);
}

int CWebUtil::kp_set_end_modify(CString sSPath, CString sSFileName, LARGE_INTEGER nSSize, 
								CString sDPath, CString sDFileName, LARGE_INTEGER nDSize, MODIFY_TYPE Type)
{
	CString strTemp;
	CString strSSize;
	CString strDSize;
	CString sFlag;

	strSSize.Format(_T("%I64d"),nSSize.QuadPart);
	strDSize.Format(_T("%I64d"),nDSize.QuadPart);

	strTemp = kp_get_crypted_data(_T("8")) + DELIM +
		kp_get_crypted_data(m_cpid) + DELIM +
		kp_get_crypted_data(m_loginid) + DELIM +
		kp_get_crypted_data(sSPath) + DELIM +
		kp_get_crypted_data(sSFileName) + DELIM +
		kp_get_crypted_data(strSSize) + DELIM +
		kp_get_crypted_data(sDPath) + DELIM +
		kp_get_crypted_data(sDFileName) + DELIM +
		kp_get_crypted_data(strDSize) + DELIM +
		kp_get_crypted_data(modify_types[Type]);
	/*
	strTemp.Format(_T("%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s"),
	kp_get_crypted_data(_T("8")),DELIM,
	kp_get_crypted_data(m_cpid),DELIM,
	kp_get_crypted_data(m_loginid),DELIM,
	kp_get_crypted_data(sSPath),DELIM,
	kp_get_crypted_data(sSFileName),DELIM,
	kp_get_crypted_data(strSSize),DELIM,
	kp_get_crypted_data(sDPath),DELIM,
	kp_get_crypted_data(sDFileName),DELIM,
	kp_get_crypted_data(strDSize),DELIM,
	kp_get_crypted_data(modify_types[Type]));
	*/
	TRACE(_T("\nkp_set_end_modify : %s | %s | %s | %s | %s | %s | %s | %s | %s\n"),
	m_cpid,m_loginid,sSPath,sSFileName,strSSize,sDPath,sDFileName,strDSize,modify_types[Type]);

	strTemp = kp_recv_data_http(_T("p=") + kp_get_base64_data(strTemp),true);

	strTemp.Trim();
	return kp_process_errors(atol(strTemp),Type);
}

CString CWebUtil::kp_get_encoded_data(CString strData)
{
	char  szEncoded[MAX_PATH << 1];
	char* pszEncoded = szEncoded;
	CString strRet;

	LPCSTR pszURL = strData.GetBuffer();

	while (*pszURL != NULL) {
		if ((*pszURL >= 'a' && *pszURL <= 'z') || (*pszURL >= 'A' && *pszURL <= 'Z')
			|| (*pszURL >= '0' && *pszURL <= '9') || *pszURL == '/')
			*pszEncoded = (char)*pszURL;
		else {
			*pszEncoded++ = '%';

			char szBuf[10];
			sprintf(szBuf, _T("%02X"), *pszURL);
			int nBufLen = (int)strlen(szBuf);
			*pszEncoded++ = szBuf[nBufLen - 2];
			*pszEncoded   = szBuf[nBufLen - 1];
		}

		pszEncoded++;
		pszURL++;
	}
	*pszEncoded = NULL;

	strRet = szEncoded;

	return strData;
}

#define BUFFER_SIZE 1024

CString CWebUtil::kp_get_decoded_data(CString strData)
{
	TCHAR szOut[BUFFER_SIZE], szHex[3];

	ZeroMemory(szOut, sizeof(szOut));

	for (int i = 0, j = 0; strData[i]; i++, j++) {
		if (strData[i] == '%') {
			szHex[0] = strData[++i];
			szHex[1] = strData[++i];
			szHex[2] = 0;
			sscanf((char *)szHex, "%x", &szOut[j]);
		} else {
			szOut[j] = strData[i];
		}
	}

	CString sRet = szOut;

	return strData;
}


CString CWebUtil::kp_get_base64_data(CString strData)
{
	CString strRet;

	int nDstLen = BEncodeGetRequiredLength(strData.GetLength(),sizeof(char));

	Base64Encode((BYTE*)strData.GetBuffer(), strData.GetLength(), strRet.GetBuffer(nDstLen), &nDstLen);
	strRet.ReleaseBuffer(nDstLen);

	return strRet;
}

CString CWebUtil::kp_get_unbase64_data(CString strData)
{
	CString strRet;

	int nDstLen = Base64DecodeGetRequiredLength(strData.GetLength());

	Base64Decode(strData.GetBuffer(),strData.GetLength(),(BYTE*)strRet.GetBuffer(nDstLen),&nDstLen);
	strRet.ReleaseBuffer(nDstLen);

	return strRet;
}


CString CWebUtil::kp_recv_data_http(CString strParam,BOOL bssl)
{
	CString strRet;
	DWORD dwFlags = 0 ;
	HINTERNET hInternetConnect;
	HINTERNET hOpenRequest;
	HINTERNET hInternetRoot;

	TRACE(_T("\nPARAM : %s\n"),strParam);

	hInternetRoot = InternetOpen(_T("KPOST"),INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);

	if(bssl)
	{
		hInternetConnect = InternetConnect(hInternetRoot,KPOST_WEBIP,
			INTERNET_DEFAULT_HTTPS_PORT,"","",INTERNET_SERVICE_HTTP,0,0);
		dwFlags = INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_NO_CACHE_WRITE |INTERNET_FLAG_FORMS_SUBMIT |INTERNET_FLAG_SECURE;
	}
	else
	{
		hInternetConnect = InternetConnect(hInternetRoot,KPOST_WEBIP,
			_HTTP_PORT,"","",INTERNET_SERVICE_HTTP,0,0);
		dwFlags = INTERNET_FLAG_IGNORE_CERT_CN_INVALID|INTERNET_FLAG_IGNORE_CERT_DATE_INVALID;
	}
	hOpenRequest = HttpOpenRequest(hInternetConnect,_T("POST"),KPOST_WEBREF,HTTP_VERSION,"",NULL,dwFlags,0);

	dwFlags = SECURITY_FLAG_IGNORE_UNKNOWN_CA|
		SECURITY_FLAG_IGNORE_REVOCATION|
		SECURITY_FLAG_IGNORE_REDIRECT_TO_HTTP|
		SECURITY_FLAG_IGNORE_REDIRECT_TO_HTTPS|
		SECURITY_FLAG_IGNORE_CERT_DATE_INVALID|
		SECURITY_FLAG_IGNORE_CERT_CN_INVALID;

	InternetSetOption (hOpenRequest, INTERNET_OPTION_SECURITY_FLAGS, &dwFlags, sizeof(dwFlags));

	char szPostData[2048] = {0};
	lstrcpy(szPostData, strParam);

	// post header
	char szLen[MAX_PATH] = {0};
	char szHeader[2048] = {0};

	sprintf(szLen, _T("%d"), lstrlen(szPostData));
	lstrcpy(szHeader, _T("Accept: text/*\r\n"));
	lstrcat(szHeader, _T("User-Agent: Mozilla/4.0 (compatible; MSIE 5.0;* Windows NT)\r\n"));
	lstrcat(szHeader, _T("Content-type: application/x-www-form-urlencoded\r\n"));
	lstrcat(szHeader, _T("Content-length: "));
	lstrcat(szHeader, szLen);
	lstrcat(szHeader, _T("\r\n\n"));

	HttpAddRequestHeaders(hOpenRequest, szHeader, -1L, HTTP_ADDREQ_FLAG_ADD);

	BOOL bSendRequest = HttpSendRequest(hOpenRequest,NULL,0,szPostData,lstrlen(szPostData));


	char szBuf[1024+1] = {0};
	DWORD dwSize = 0;

	InternetReadFile( hOpenRequest,szBuf,sizeof(szBuf)-1,&dwSize);
	strRet += szBuf;

	memset(szBuf,0x00,sizeof(szBuf));

	while(dwSize)
	{
		dwSize = 0;
		InternetReadFile( hOpenRequest,szBuf,sizeof(szBuf)-1,&dwSize);
		if(dwSize)
		{
			szBuf[dwSize] = 0x00;
			strRet += szBuf;
		}

		memset(szBuf,0x00,sizeof(szBuf));
	}


	BOOL bRet = InternetCloseHandle(hOpenRequest);
	bRet = InternetCloseHandle(hInternetConnect);
	bRet = InternetCloseHandle(hInternetRoot);

	strRet.Trim();

	TRACE(_T("\nRECV : %s\n"),strRet);

	return strRet;
}

int CWebUtil::kp_set_backup(CString sSrcPath,CString sFlag)
{
	CString strTemp;

	strTemp = kp_get_crypted_data(_T("11")) + DELIM +
		kp_get_crypted_data(m_cpid) + DELIM +
		kp_get_crypted_data(sSrcPath) + DELIM +
		kp_get_crypted_data(sFlag);
	/*
	strTemp.Format(_T("%s%s%s%s%s%s%s"),
	kp_get_crypted_data(_T("11")),DELIM,
	kp_get_crypted_data(m_cpid),DELIM,
	kp_get_crypted_data(sSrcPath),DELIM,
	kp_get_crypted_data(sFlag));
	*/	
	TRACE(_T("\nkp_set_backup : %s | %s | %s\n"),m_cpid,sSrcPath,sFlag);

	strTemp = kp_recv_data_http(_T("p=") + kp_get_base64_data(strTemp),true);

	strTemp.Trim();

	return kp_process_errors(atol(strTemp));
}

int CWebUtil::kp_set_backup_path(CString sBackupPath)
{
	CString strTemp;

	strTemp.Format(_T("%s%s%s%s%s%s%s"),
		kp_get_crypted_data(_T("13")) + DELIM +
		kp_get_crypted_data(m_cpid) + DELIM +
		kp_get_crypted_data(m_loginid) + DELIM +
		kp_get_crypted_data(sBackupPath));
	/*
	strTemp.Format(_T("%s%s%s%s%s%s%s"),
	kp_get_crypted_data(_T("13")),DELIM,
	kp_get_crypted_data(m_cpid),DELIM,
	kp_get_crypted_data(m_loginid),DELIM,
	kp_get_crypted_data(sBackupPath));
	*/	
	TRACE(_T("\nkp_set_backup_path : %s | %s | %s\n"),m_cpid,m_loginid, sBackupPath);

	strTemp = kp_recv_data_http(_T("p=") + kp_get_base64_data(strTemp),true);

	strTemp.Trim();

	return kp_process_errors(atol(strTemp));
}

int CWebUtil::kp_chk_notice()
{
	CString strTemp;

	strTemp = kp_get_crypted_data(_T("12")) + DELIM +
		kp_get_crypted_data(m_cpid) + DELIM +
		kp_get_crypted_data(m_loginid);
	/*
	strTemp.Format(_T("%s%s%s%s%s"),
	kp_get_crypted_data(_T("12")),DELIM,
	kp_get_crypted_data(m_cpid),DELIM,
	kp_get_crypted_data(m_loginid));
	*/	
	TRACE(_T("\nkp_chk_notice : %s\n"),m_cpid);

	strTemp = kp_recv_data_http(_T("p=") + kp_get_base64_data(strTemp),true);

	strTemp.Trim();

	return kp_process_errors(atol(strTemp));
}

CString CWebUtil::kp_get_backup_path()
{
	CString strTemp;

	strTemp = kp_get_crypted_data(_T("14")) + DELIM +
		kp_get_crypted_data(m_cpid) + DELIM +
		kp_get_crypted_data(m_loginid);
	/*
	strTemp.Format(_T("%s%s%s%s%s"),
	kp_get_crypted_data(_T("14")),DELIM,
	kp_get_crypted_data(m_cpid),DELIM,
	kp_get_crypted_data(m_loginid));
	*/	
	TRACE(_T("\n kp_get_backup_path : %s | %s\n"),m_cpid,m_loginid);

	strTemp = kp_get_unbase64_data(kp_recv_data_http(_T("p=") + kp_get_base64_data(strTemp),true));

	strTemp.Trim();

	return strTemp;
}

int CWebUtil::kp_sync_data()
{
	CString strTemp;

	strTemp = kp_get_crypted_data(_T("15")) + DELIM + kp_get_crypted_data(m_cpid);

	TRACE(_T("\n kp_sync_data : %s\n"),m_cpid);

	strTemp = kp_get_unbase64_data(kp_recv_data_http(_T("p=") + kp_get_base64_data(strTemp),true));

	strTemp.Trim();

	return kp_process_errors(atol(strTemp));

}

int CWebUtil::kp_get_sync_time()
{
	CString strTemp;

	strTemp = kp_get_crypted_data(_T("16")) + DELIM + kp_get_crypted_data(m_cpid);

	TRACE(_T("\n kp_get_sync_time : %s\n"),m_cpid);

	strTemp = kp_recv_data_http(_T("p=") + kp_get_base64_data(strTemp),true);

	strTemp.Trim();

	return kp_process_errors(atol(strTemp));
}

int CWebUtil::kp_file_opt_log(PTF_LOG_STURCT ptflog_info)
{
	CString strTemp;
	CString str_action;
	CString str_file_flag;

//	if (0==ptflog_info->action) str_action="M";
//	if (0==ptflog_info->file_flag) str_file_flag = "F";

	strTemp = kp_get_crypted_data(_T("1")) + DELIM +
		kp_get_crypted_data(ptflog_info->sp_user_id) + DELIM +
		kp_get_crypted_data(ptflog_info->vol_name) + DELIM +
		kp_get_crypted_data(ptflog_info->s_file) + DELIM +
		kp_get_crypted_data(ptflog_info->t_file) + DELIM +
		kp_get_crypted_data(CString(ptflog_info->action)) + DELIM +
		kp_get_crypted_data(CString(ptflog_info->file_flag));

	TRACE(_T("\n kp_get_sync_time : %s\n"),m_cpid);

	strTemp = kp_recv_data_http(_T("p=") + kp_get_base64_data(strTemp),false);

	strTemp.Trim();

	return kp_process_errors(atol(strTemp));
}

int CWebUtil::kp_send_mail(CString mail1 , CString mail2 , CString mail3 , CString file_name)
{
	CString strTemp;

	strTemp = kp_get_crypted_data(_T("2")) + DELIM +
		kp_get_crypted_data(mail1) + DELIM +
		kp_get_crypted_data(mail2) + DELIM +
		kp_get_crypted_data(mail3) + DELIM +
		kp_get_crypted_data(file_name);

	TRACE(_T("\n kp_get_sync_time : %s\n"),m_cpid);

	strTemp = _T("p=") + kp_get_base64_data(strTemp);

	strTemp = kp_recv_data_http( strTemp ,false);

	strTemp.Trim();

	return kp_process_errors(atol(strTemp));
}

