Exemple #1
0
fsString fsUtf8HttpUrlEncode (LPCSTR psz)
{
	return fsHttpUrlEncode (fsUtf8Encode (psz));
}
fsInternetResult fsUploadMgr::ValidateCaptcha()
{
	if (m_bCaptchaValidated)
		return IR_S_FALSE;

	Event (UMGRE_VALIDATING_UPLOAD);

	fsString strCookie;
	CString strPngFile, strCaptchaValidateUrl, strCaptchaVal;
	fsUpload_Properties up;
	ZeroMemory (&up, sizeof (up));
	vmsInternetSession* pSession = NULL;

	

	fsInternetResult ir = fsUP_GetByUrl (&up, m_strCaptchaUrl);
	if (ir != IR_SUCCESS)
		goto _lErr;

	pSession = new vmsInternetSession;
	char szProxy [10000];
	vmsMakeWinInetProxy (up.pszProxyName, up.enProtocol, up.enProtocol, szProxy);
	ir  = pSession->Create (up.pszAgent, up.enAccType, szProxy, up.enProtocol);
	if (ir != IR_SUCCESS)
		goto _lErr;
	
	pSession->SetProxyAuth (up.pszProxyUserName, up.pszProxyPassword);
	pSession->SetTimeout (up.uTimeout);

	m_memDldr.Initialize (pSession);

	m_memDldr.Get_File ()->UseHttp11 (TRUE);
	m_memDldr.Get_File ()->UseCookie (TRUE);

	ir = m_memDldr.Download (m_strCaptchaUrl);

	if (ir != IR_SUCCESS)
		goto _lErr;

	if (m_bNeedStop)
	{
		ir = IR_S_FALSE;
		goto _lErr;
	}

	strCookie = m_memDldr.Get_File ()->GetCookiesFromResponse ();

	m_memDldr.Get_File ()->CloseHandle ();
	fsUP_GetByUrl_Free (&up);

	char szTmpPath [MAX_PATH];
	GetTempPath (MAX_PATH, szTmpPath);
	if (szTmpPath [lstrlen (szTmpPath) - 1] != '\\')
		lstrcat (szTmpPath, "\\");

	int i; i = 0;
	do {
		strPngFile.Format ("%s%u%d.png", szTmpPath, m_nFileId, i++);
	}
	while (GetFileAttributes (strPngFile) != DWORD (-1));

	HANDLE hFile; hFile = CreateFile (strPngFile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
		FILE_ATTRIBUTE_HIDDEN, NULL);
	DWORD dw;
	WriteFile (hFile, m_memDldr.Get_FileBuffer (), m_memDldr.Get_FileSize (), &dw, NULL);
	CloseHandle (hFile);

	

_lAskCaptchaValue:

	LPSTR pszCV; pszCV = strCaptchaVal.GetBuffer (100);
	*pszCV = 0;
	Event (UMGRE_NEED_ASK_CAPTCHA_VALUE, (DWORD)(LPCSTR)strPngFile, pszCV);
	strCaptchaVal.ReleaseBuffer ();

	if (strCaptchaVal.IsEmpty ())
	{
		DeleteFile (strPngFile);
		ir = IR_S_FALSE;
		goto _lErr;
	}

	

	strCaptchaVal = fsHttpUrlEncode (strCaptchaVal);

	strCaptchaValidateUrl = m_strCaptchaValidateUrl;
	strCaptchaValidateUrl.Replace ("jcaptchaCode", strCaptchaVal);
	ir = fsUP_GetByUrl (&up, strCaptchaValidateUrl);
	if (ir != IR_SUCCESS)
	{
		DeleteFile (strPngFile);
		goto _lErr;
	}

	m_networkOpFile.Initialize (pSession, FALSE);

	m_networkOpFile.SetCookies (strCookie);

	m_networkOpFile.set_EnableAutoRedirect (TRUE);

	m_networkOpFile.set_Charset ("");

	ir = m_networkOpFile.OpenEx (fsNPToScheme (up.enProtocol), up.pszServerName,
		up.pszUserName, up.pszPassword, up.uServerPort, 
		up.pszPathName, 0, FALSE, _UI64_MAX, _UI64_MAX);

	if (ir == IR_FILENOTFOUND)
		goto _lAskCaptchaValue;

	DeleteFile (strPngFile);
	
	if (ir != IR_SUCCESS)
		goto _lErr;

	m_bCaptchaValidated = TRUE;

	m_networkOpFile.CloseHandle ();

	delete pSession;

	Event (UMGRE_VALIDATING_UPLOAD_DONE);

	return IR_SUCCESS;

_lErr:
	if (pSession)
		delete pSession;
	Event (UMGRE_VALIDATING_UPLOAD_FAILED, ir);
	return ir;
}