00001
00002
00003
00004
00005
00006 #include "stdafx.h"
00007 #include "Iefdm.h"
00008 #include "WgBHO.h"
00009 #include "comdef.h"
00010
00011 _COM_SMARTPTR_TYPEDEF(IHTMLInputElement, __uuidof(IHTMLInputElement));
00012
00013 STDMETHODIMP CWgBHO::GetSite(REFIID riid, void **ppvSite)
00014 {
00015 if (m_spWebBrowser2 == NULL || ppvSite == NULL)
00016 return E_INVALIDARG;
00017
00018 *ppvSite = NULL;
00019 return m_spWebBrowser2.QueryInterface ((IWebBrowser2**) ppvSite);
00020 }
00021
00022 STDMETHODIMP CWgBHO::SetSite(IUnknown *pSite)
00023 {
00024 m_spWebBrowser2 = pSite;
00025 if (m_spWebBrowser2 == NULL)
00026 return S_OK;
00027
00028 m_spCPC = m_spWebBrowser2;
00029 if (m_spCPC == NULL)
00030 return E_POINTER;
00031
00032 return Connect ();
00033 }
00034
00035 STDMETHODIMP CWgBHO::Connect()
00036 {
00037 HRESULT hr;
00038 CComQIPtr <IConnectionPoint> spCP;
00039
00040 hr = m_spCPC->FindConnectionPoint (DIID_DWebBrowserEvents2, &spCP);
00041 if (FAILED (hr))
00042 return hr;
00043
00044 IWgBHO *pThis = (IWgBHO*) this;
00045
00046 hr = spCP->Advise (pThis, &m_dwCookie);
00047
00048 return S_OK;
00049 }
00050
00051 STDMETHODIMP CWgBHO::BeforeNavigate2(IDispatch *, VARIANT *url, VARIANT *flags, VARIANT *tfn, VARIANT *pd, VARIANT *headers, VARIANT_BOOL *bCancel)
00052 {
00053 USES_CONVERSION;
00054
00055 if (fsIsMonitoringTurnedOn () == FALSE)
00056 return S_OK;
00057
00058 _bstr_t bstrUrl = url->bstrVal;
00059
00060 LPCSTR pszUrl = W2A (bstrUrl);
00061
00062 if (fsIsOKUrl (pszUrl) == FALSE)
00063 return S_OK;
00064
00065 *bCancel = VARIANT_FALSE;
00066
00067 if (FALSE == fsOnNavigateUrl (pszUrl))
00068 return S_OK;
00069
00070 fsString strCookies, strPostData, strReferer;
00071 IDispatchPtr spdDoc;
00072 m_spWebBrowser2->get_Document (&spdDoc);
00073 IHTMLDocument2Ptr spDoc;
00074 if (spdDoc)
00075 spDoc = spdDoc;
00076 if (spDoc)
00077 {
00078 BSTR bstr = NULL;
00079 spDoc->get_cookie (&bstr);
00080 if (bstr)
00081 {
00082 strCookies = W2A (bstr);
00083 SysFreeString (bstr);
00084 }
00085
00086 bstr = NULL;
00087 spDoc->get_URL (&bstr);
00088 if (bstr)
00089 {
00090 strReferer = W2A (bstr);
00091 SysFreeString (bstr);
00092 }
00093
00094 IHTMLElementCollectionPtr spForms;
00095 spDoc->get_forms (&spForms);
00096
00097 long cForms = 0;
00098 if (spForms)
00099 spForms->get_length (&cForms);
00100
00101 bool bFound = false;
00102
00103 for (long i = 0; bFound == false && i < cForms; i++)
00104 {
00105 IDispatchPtr spd;
00106 spForms->item (CComVariant (i), CComVariant (i), &spd);
00107
00108 IHTMLFormElementPtr spForm (spd);
00109 if (spForm == NULL)
00110 continue;
00111
00112 BSTR bstr = NULL;
00113 spForm->get_action (&bstr);
00114 bFound = bstr != NULL && wcscmp (url->bstrVal, bstr) == 0;
00115 SysFreeString (bstr);
00116 if (bFound == false)
00117 continue;
00118
00119 bstr = NULL;
00120 spForm->get_method (&bstr);
00121 if (bstr == NULL || wcsicmp (bstr, L"post"))
00122 break;
00123 SysFreeString (bstr);
00124
00125 IHTMLElementPtr spFormElem (spForm);
00126 if (spFormElem == NULL)
00127 {
00128 bFound = false;
00129 continue;
00130 }
00131
00132 WalkThroughForm (spFormElem, strPostData);
00133
00134 if (strPostData != "" &&
00135 strPostData [strPostData.GetLength ()-1] == '&')
00136 strPostData [strPostData.GetLength ()-1] = 0;
00137 }
00138
00139 }
00140
00141 if (fsUrlToFdm (pszUrl, strReferer, strCookies != "" ? strCookies : NULL,
00142 strPostData != "" ? strPostData : NULL, FALSE))
00143 *bCancel = VARIANT_TRUE;
00144
00145 return S_OK;
00146 }
00147
00148 BOOL fsIsOKUrl (LPCSTR pszURL)
00149 {
00150 return _strnicmp (pszURL, "http://", 7) == 0 ||
00151 _strnicmp (pszURL, "ftp://", 6) == 0 ||
00152 _strnicmp (pszURL, "https://", 8) == 0;
00153 }
00154
00155 void CWgBHO::WalkThroughForm(IHTMLElement *pElement, fsString &str)
00156 {
00157 USES_CONVERSION;
00158
00159 IDispatchPtr spd;
00160 pElement->get_children (&spd);
00161 IHTMLElementCollectionPtr spels (spd);
00162
00163 long cElements = 0;
00164 if (spels != NULL)
00165 spels->get_length (&cElements);
00166
00167 for (int j = 0; j < cElements; j++)
00168 {
00169 spd = NULL;
00170 spels->item (CComVariant (j), CComVariant (j), &spd);
00171
00172 IHTMLInputElementPtr spInp (spd);
00173 if (spInp != NULL)
00174 {
00175 BSTR bstr = NULL, bstr2 = NULL;
00176 spInp->get_name (&bstr);
00177 spInp->get_value (&bstr2);
00178 if (bstr)
00179 {
00180 str += W2A (bstr);
00181 str += "=";
00182 SysFreeString (bstr);
00183 }
00184 if (bstr2)
00185 {
00186 str += W2A (bstr2);
00187 SysFreeString (bstr2);
00188 }
00189
00190 if (bstr || bstr2)
00191 str += "&";
00192 }
00193
00194 IHTMLElementPtr spel (spd);
00195 if (spel != NULL)
00196 WalkThroughForm (spel, str);
00197 }
00198 }