00001
00002
00003
00004
00005 #include "stdafx.h"
00006 #include "Iecooks.h"
00007 #include "FDMIECookiesBHO.h"
00008 #include <mshtml.h>
00009 #include <oleacc.h>
00010 #include "../../vmsVideoSiteHtmlCodeParser.h"
00011
00012 _COM_SMARTPTR_TYPEDEF(IHTMLInputElement, __uuidof(IHTMLInputElement));
00013
00014 extern BOOL _bIsWin9x;
00015
00016 #pragma data_seg("shdata")
00017
00018 long CFDMIECookiesBHO::_cIEDownloads = 0;
00019 #pragma data_seg()
00020
00021 #pragma comment(linker, "/section:shdata,rws")
00022
00023 IHTMLDocument2Ptr doc_from_hwnd (HWND hwnd)
00024 {
00025 HINSTANCE hInst = ::LoadLibrary( _T("OLEACC.DLL") );
00026 if (hInst == NULL)
00027 return NULL;
00028
00029 LPFNOBJECTFROMLRESULT pfObjectFromLresult = (LPFNOBJECTFROMLRESULT)::GetProcAddress( hInst, _T("ObjectFromLresult") );
00030
00031 UINT nMsg = RegisterWindowMessage ("WM_HTML_GETOBJECT");
00032
00033 LRESULT lRes;
00034 ::SendMessageTimeout (hwnd, nMsg, 0L, 0L, SMTO_ABORTIFHUNG, 1000, (DWORD*)&lRes );
00035
00036 IHTMLDocument2Ptr spDoc;
00037 pfObjectFromLresult (lRes, IID_IHTMLDocument2, 0, (void**)&spDoc);
00038
00039 FreeLibrary (hInst);
00040
00041 return spDoc;
00042 }
00043
00044 BOOL is_okpage (HWND hwnd)
00045 {
00046 IHTMLDocument2Ptr spDoc = doc_from_hwnd (hwnd);
00047 if (spDoc == NULL)
00048 return TRUE;
00049
00050 IHTMLLocationPtr spLoc;
00051 spDoc->get_location (&spLoc);
00052
00053 BSTR bstrHost = NULL;
00054 spLoc->get_hostname (&bstrHost);
00055
00056 if (bstrHost == NULL)
00057 return FALSE;
00058
00059 USES_CONVERSION;
00060
00061 if (vmsVideoSiteHtmlCodeParser::GetSupportedSiteIndex (W2A (bstrHost)) != -1)
00062 return TRUE;
00063
00064 return FALSE;
00065 }
00066
00067 WNDPROC _pfnIeServerOrigProc = NULL;
00068 BOOL _bUseUnicode;
00069 DWORD _dwIeMajorVersion;
00070
00071 BOOL GetIEVersion(DWORD *pMajor, DWORD *pMinor, DWORD *pBuild, DWORD *pSubBuild)
00072 {
00073 CRegKey rk;
00074 LONG lRet = rk.Open(HKEY_LOCAL_MACHINE, _T("Software\\Microsoft\\Internet Explorer"), KEY_READ);
00075 if(lRet == 0)
00076 {
00077 TCHAR szBuff[256];
00078 DWORD dwCount = sizeof(szBuff);
00079 lRet = rk.QueryValue(szBuff, _T("Version"), &dwCount);
00080 if(lRet == 0)
00081 {
00082 sscanf(szBuff, "%u.%u.%u.%u", pMajor, pMinor, pBuild, pSubBuild);
00083 return TRUE;
00084 }
00085 }
00086
00087 return FALSE;
00088 }
00089
00090 BOOL _bMenuWasRemoved = FALSE;
00091 UINT _nRemovedMenuPrevIndex;
00092 char _szRemovedMenuPrevString [100];
00093 UINT _nRemovedMenuID;
00094 char _szRemovedMenuString [100];
00095 BOOL _bSeparatorWasRemoved;
00096
00097 LRESULT CALLBACK _IeServerWndProc (HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
00098 {
00099 if (msg == WM_INITMENUPOPUP && is_okpage (hwnd) == FALSE)
00100 {
00101 HMENU menu = (HMENU)wp;
00102
00103 CRegKey key;
00104 key.Open (HKEY_CURRENT_USER,
00105 "Software\\FreeDownloadManager.ORG\\Free Download Manager\\Settings\\View\\Loc", KEY_READ);
00106 char szItem [1000] = ""; DWORD dw = sizeof (szItem);
00107 key.QueryValue (szItem, "dlfvideoiemenu", &dw);
00108
00109 int cItems = GetMenuItemCount (menu);
00110
00111 while (--cItems)
00112 {
00113 char sz [300] = "";
00114 GetMenuString (menu, cItems, sz, sizeof (sz), MF_BYPOSITION);
00115 if (lstrcmp (sz, szItem) == 0)
00116 {
00117 BOOL bRemoveSeparator = GetMenuState (menu, cItems+1, MF_BYPOSITION) &
00118 GetMenuState (menu, cItems-1, MF_BYPOSITION) & MF_SEPARATOR;
00119
00120 if (_dwIeMajorVersion != 7)
00121 {
00122 _nRemovedMenuPrevIndex = cItems - 1;
00123 _szRemovedMenuPrevString [0] = 0;
00124 GetMenuString (menu, _nRemovedMenuPrevIndex, _szRemovedMenuPrevString,
00125 sizeof (_szRemovedMenuPrevString), MF_BYPOSITION);
00126
00127 _nRemovedMenuID = GetMenuItemID (menu, cItems);
00128 GetMenuString (menu, cItems, _szRemovedMenuString,
00129 sizeof (_szRemovedMenuString), MF_BYPOSITION);
00130
00131 _bSeparatorWasRemoved = bRemoveSeparator;
00132
00133 _bMenuWasRemoved = TRUE;
00134 }
00135
00136 RemoveMenu (menu, cItems, MF_BYPOSITION);
00137 if (bRemoveSeparator)
00138 RemoveMenu (menu, cItems, MF_BYPOSITION);
00139 break;
00140 }
00141 }
00142 }
00143 else if (msg == WM_UNINITMENUPOPUP && _bMenuWasRemoved)
00144 {
00145 HMENU menu = (HMENU)wp;
00146
00147 char sz [100] = "";
00148 GetMenuString (menu, _nRemovedMenuPrevIndex, sz, sizeof (sz), MF_BYPOSITION);
00149
00150 if (lstrcmp (sz, _szRemovedMenuPrevString) == 0)
00151 {
00152 _bMenuWasRemoved = FALSE;
00153 InsertMenu (menu, _nRemovedMenuPrevIndex+1, MF_BYPOSITION, _nRemovedMenuID, _szRemovedMenuString);
00154 if (_bSeparatorWasRemoved)
00155 InsertMenu (menu, _nRemovedMenuPrevIndex+2, MF_BYPOSITION | MF_SEPARATOR, 0, NULL);
00156 }
00157 }
00158
00159 return _bUseUnicode ? CallWindowProcW (_pfnIeServerOrigProc, hwnd, msg, wp, lp) :
00160 CallWindowProcA (_pfnIeServerOrigProc, hwnd, msg, wp, lp);
00161 }
00162
00163 HWND find_ie_server (HWND hwnd)
00164 {
00165 HWND hwnd2 = NULL;
00166
00167 for (;;)
00168 {
00169 hwnd2 = FindWindowEx (hwnd, hwnd2, NULL, NULL);
00170 if (hwnd2 == NULL)
00171 return NULL;
00172
00173 char sz [1000];
00174 GetClassName (hwnd2, sz, sizeof (sz));
00175
00176 if (lstrcmpi (sz, "Internet Explorer_Server") == 0)
00177 {
00178 LONG lProc;
00179 if (_bUseUnicode)
00180 lProc = GetWindowLongW (hwnd2, GWL_WNDPROC);
00181 else
00182 lProc = GetWindowLongA (hwnd2, GWL_WNDPROC);
00183 if (_pfnIeServerOrigProc == NULL)
00184 _pfnIeServerOrigProc = (WNDPROC)lProc;
00185 if (lProc == (LONG)_pfnIeServerOrigProc)
00186 return hwnd2;
00187 }
00188
00189 HWND hwnd3 = find_ie_server (hwnd2);
00190 if (hwnd3 != NULL)
00191 return hwnd3;
00192 }
00193 }
00194
00195 DWORD WINAPI _threadSubclassIeServerWnd (LPVOID lp)
00196 {
00197 IWebBrowser2* wb = (IWebBrowser2*) lp;
00198
00199 _bUseUnicode = (GetVersion () & 0x80000000) == 0;
00200 DWORD dw;
00201 GetIEVersion (&_dwIeMajorVersion, &dw, &dw, &dw);
00202
00203 HWND hwnd;
00204 wb->get_HWND ((long*)&hwnd);
00205
00206 HWND hwnd2;
00207 do {
00208 Sleep (1000);
00209 hwnd2 = find_ie_server (hwnd);
00210 }
00211 while (hwnd2 == NULL);
00212
00213 if (_bUseUnicode)
00214 SetWindowLongW (hwnd2, GWL_WNDPROC, (LONG)_IeServerWndProc);
00215 else
00216 SetWindowLongA (hwnd2, GWL_WNDPROC, (LONG)_IeServerWndProc);
00217
00218 return 0;
00219 }
00220
00221 STDMETHODIMP CFDMIECookiesBHO::SetSite(IUnknown *pSite)
00222 {
00223 if (m_spWB != NULL)
00224 Disconnect ();
00225
00226 if (pSite == NULL)
00227 DeleteFile ();
00228
00229 m_spWB = pSite;
00230
00231 if (m_spWB == NULL)
00232 return pSite == NULL ? S_OK : E_INVALIDARG;
00233
00234
00235
00236 m_spWB_CPC = m_spWB;
00237 if (m_spWB_CPC == NULL)
00238 return E_INVALIDARG;
00239
00240 return Connect ();
00241 }
00242
00243 STDMETHODIMP CFDMIECookiesBHO::GetSite(REFIID riid, void **ppvSite)
00244 {
00245 if (m_spWB == NULL || ppvSite == NULL)
00246 return E_INVALIDARG;
00247
00248 *ppvSite = NULL;
00249 return m_spWB.QueryInterface (riid, (IWebBrowser2**)ppvSite);
00250 }
00251
00252 STDMETHODIMP CFDMIECookiesBHO::Connect()
00253 {
00254 HRESULT hr;
00255
00256 hr = Disconnect ();
00257 if (FAILED (hr))
00258 return hr;
00259
00260 hr = m_spWB_CPC->FindConnectionPoint (DIID_DWebBrowserEvents2, &m_spWB_CPC_CP);
00261 if (FAILED (hr))
00262 return hr;
00263
00264 IUnknown *pthis = (IUnknown*)(IFDMIECookiesBHO*) this;
00265
00266 hr = m_spWB_CPC_CP->Advise (pthis, &m_dwCookie);
00267
00268 return hr;
00269 }
00270
00271 STDMETHODIMP CFDMIECookiesBHO::Disconnect()
00272 {
00273 if (m_spWB_CPC_CP == NULL)
00274 return S_FALSE;
00275
00276 HRESULT hr = m_spWB_CPC_CP->Unadvise (m_dwCookie);
00277 if (FAILED (hr))
00278 return hr;
00279
00280 m_spWB_CPC_CP = NULL;
00281 m_spWB_CPC = NULL;
00282
00283 return S_OK;
00284 }
00285
00286 STDMETHODIMP CFDMIECookiesBHO::ProgressChange(long Progress, long ProgressMax)
00287 {
00288 USES_CONVERSION;
00289
00290
00291
00292 IDispatchPtr spdDoc;
00293 m_spWB->get_Document (&spdDoc);
00294 if (spdDoc == NULL)
00295 return S_OK;
00296
00297 IHTMLDocument2Ptr spDoc (spdDoc);
00298 if (spDoc == NULL)
00299 return S_OK;
00300
00301 BSTR bstr;
00302 spDoc->get_cookie (&bstr);
00303
00304 fsString strCookies, strUrl;
00305
00306 if (bstr)
00307 {
00308 strCookies = W2A (bstr);
00309 SysFreeString (bstr);
00310 }
00311
00312 spDoc->get_URL (&bstr);
00313
00314 if (bstr)
00315 {
00316 strUrl = W2A (bstr);
00317 SysFreeString (bstr);
00318 }
00319
00320 if (m_strUrl != strUrl || m_strCookies != strCookies ||
00321 m_strPostData != "")
00322 {
00323 m_strUrl = strUrl;
00324 m_strCookies = strCookies;
00325 SaveInfoToDisk ();
00326 }
00327
00328 return S_OK;
00329 }
00330
00331 void CFDMIECookiesBHO::SaveInfoToDisk()
00332 {
00333 WaitForSingleObject (m_mxFile, INFINITE);
00334
00335 if (m_hFile == INVALID_HANDLE_VALUE)
00336 {
00337 char szTmpPath [MAX_PATH], szFile [MAX_PATH];
00338 GetTempPath (MAX_PATH, szTmpPath);
00339 CreateDirectory (szTmpPath, NULL);
00340 lstrcat (szTmpPath, "Free Download Manager");
00341 CreateDirectory (szTmpPath, NULL);
00342 GetTempFileName (szTmpPath, "tic", 0, szFile);
00343
00344 if (_bIsWin9x == FALSE)
00345 {
00346 m_hFile = CreateFile (szFile, GENERIC_WRITE,
00347 FILE_SHARE_READ | FILE_SHARE_DELETE, NULL, CREATE_ALWAYS,
00348 FILE_ATTRIBUTE_HIDDEN | FILE_FLAG_DELETE_ON_CLOSE, NULL);
00349 }
00350 else
00351 {
00352 m_strTmpFile = szFile;
00353 m_hFile = CreateFile (szFile, GENERIC_WRITE,
00354 FILE_SHARE_READ, NULL, CREATE_ALWAYS,
00355 FILE_ATTRIBUTE_HIDDEN, NULL);
00356 }
00357
00358 if (m_hFile == INVALID_HANDLE_VALUE)
00359 {
00360 ReleaseMutex (m_mxFile);
00361 return;
00362 }
00363 }
00364
00365 SetFilePointer (m_hFile, 0, NULL, FILE_BEGIN);
00366
00367 DWORD dw;
00368
00369 WriteFile (m_hFile, (LPCSTR)m_strUrl, m_strUrl.GetLength (), &dw, NULL);
00370 WriteFile (m_hFile, "\r\n", 2, &dw, NULL);
00371 WriteFile (m_hFile, (LPCSTR)m_strCookies, m_strCookies.GetLength (), &dw, NULL);
00372 WriteFile (m_hFile, "\r\n", 2, &dw, NULL);
00373 WriteFile (m_hFile, (LPCSTR)m_strPostData, m_strPostData.GetLength (), &dw, NULL);
00374 WriteFile (m_hFile, "\r\n", 2, &dw, NULL);
00375 WriteFile (m_hFile, (LPCSTR)m_strBeforeNavUrl, m_strBeforeNavUrl.GetLength (), &dw, NULL);
00376
00377 SetEndOfFile (m_hFile);
00378
00379 ReleaseMutex (m_mxFile);
00380 }
00381
00382 STDMETHODIMP CFDMIECookiesBHO::BeforeNavigate2(IDispatch *pdWB, VARIANT *url, VARIANT *flags, VARIANT *tfn, VARIANT *pd, VARIANT *headers, VARIANT_BOOL *bCancel)
00383 {
00384 USES_CONVERSION;
00385
00386 m_strPostData = "";
00387 m_strBeforeNavUrl = W2A (url->bstrVal);
00388
00389 IWebBrowser2Ptr spWB (pdWB);
00390 if (spWB == NULL)
00391 return S_OK;
00392
00393 IDispatchPtr spdDoc;
00394 spWB->get_Document (&spdDoc);
00395 if (spdDoc == NULL)
00396 return S_OK;
00397
00398 IHTMLDocument2Ptr spDoc (spdDoc);
00399 if (spDoc == NULL)
00400 return S_OK;
00401
00402 IHTMLElementCollectionPtr spForms;
00403 spDoc->get_forms (&spForms);
00404
00405 long cForms = 0;
00406 if (spForms != NULL)
00407 spForms->get_length (&cForms);
00408
00409 bool bFound = false;
00410
00411
00412
00413 for (long i = 0; bFound == false && i < cForms; i++)
00414 {
00415 IDispatchPtr spd;
00416 spForms->item (CComVariant (i), CComVariant (i), &spd);
00417 if (spd == NULL)
00418 continue;
00419
00420 IHTMLFormElementPtr spForm (spd);
00421 if (spForm == NULL)
00422 continue;
00423
00424 BSTR bstr = NULL;
00425 spForm->get_action (&bstr);
00426
00427 bFound = bstr != NULL && wcscmp (url->bstrVal, bstr) == 0;
00428 SysFreeString (bstr);
00429 if (bFound == false)
00430 continue;
00431
00432 bstr = NULL;
00433 spForm->get_method (&bstr);
00434
00435 if (bstr == NULL || wcsicmp (bstr, L"post"))
00436 break;
00437 SysFreeString (bstr);
00438
00439 IHTMLElementPtr spFormElem (spForm);
00440 if (spFormElem == NULL)
00441 {
00442 bFound = false;
00443 continue;
00444 }
00445
00446
00447 WalkThroughForm (spFormElem, m_strPostData);
00448
00449
00450 if (m_strPostData != "" &&
00451 m_strPostData [m_strPostData.GetLength ()-1] == '&')
00452 m_strPostData [m_strPostData.GetLength ()-1] = 0;
00453 }
00454
00455 return S_OK;
00456 }
00457
00458 void CFDMIECookiesBHO::WalkThroughForm(IHTMLElement *pElement, fsString& str)
00459 {
00460 USES_CONVERSION;
00461
00462 IDispatchPtr spd;
00463 pElement->get_children (&spd);
00464 if (spd == NULL)
00465 return;
00466 IHTMLElementCollectionPtr spels (spd);
00467 if (spels == NULL)
00468 return;
00469
00470 long cElements = 0;
00471 spels->get_length (&cElements);
00472
00473 for (int j = 0; j < cElements; j++)
00474 {
00475 spd = NULL;
00476 spels->item (CComVariant (j), CComVariant (j), &spd);
00477 if (spd == NULL)
00478 continue;
00479
00480
00481 IHTMLInputElementPtr spInp (spd);
00482 if (spInp != NULL)
00483 {
00484
00485 BSTR bstr, bstr2;
00486 spInp->get_name (&bstr);
00487 spInp->get_value (&bstr2);
00488 if (bstr)
00489 {
00490 str += W2A (bstr);
00491 str += "=";
00492 SysFreeString (bstr);
00493 }
00494 if (bstr2)
00495 {
00496 str += W2A (bstr2);
00497 SysFreeString (bstr2);
00498 }
00499 if (bstr || bstr2)
00500 str += "&";
00501 }
00502
00503 IHTMLElementPtr spel (spd);
00504 if (spel != NULL)
00505 WalkThroughForm (spel, str);
00506 }
00507 }
00508
00509 STDMETHODIMP CFDMIECookiesBHO::DownloadBegin()
00510 {
00511 InterlockedIncrement (&_cIEDownloads);
00512 return S_OK;
00513 }
00514
00515 STDMETHODIMP CFDMIECookiesBHO::DownloadComplete()
00516 {
00517 InterlockedDecrement (&_cIEDownloads);
00518 return S_OK;
00519 }
00520
00521 long CFDMIECookiesBHO::get_IEDownloadsCount()
00522 {
00523 return _cIEDownloads;
00524 }
00525
00526 void CFDMIECookiesBHO::DeleteFile()
00527 {
00528 if (m_hFile != INVALID_HANDLE_VALUE)
00529 {
00530 CloseHandle (m_hFile);
00531 m_hFile = INVALID_HANDLE_VALUE;
00532 if (_bIsWin9x)
00533 ::DeleteFile (m_strTmpFile);
00534 }
00535
00536 WaitForSingleObject (m_mxFile, INFINITE);
00537 char szTmpPath [MAX_PATH];
00538 GetTempPath (MAX_PATH, szTmpPath);
00539 lstrcat (szTmpPath, "Free Download Manager");
00540 RemoveDirectory (szTmpPath);
00541 ReleaseMutex (m_mxFile);
00542 }
00543
00544 #include "vmsFlashHelper.h"
00545 #include <mflash.h>
00546
00547 _COM_SMARTPTR_TYPEDEF(IShockwaveFlash, __uuidof(IShockwaveFlash));
00548
00549 STDMETHODIMP CFDMIECookiesBHO::DocumentComplete(IDispatch *pDisp, VARIANT *URL)
00550 {
00551
00552
00553 return S_OK;
00554 }