2015-04-22 16:53:55 +00:00
|
|
|
/* PROJECT: ReactOS Applications Manager
|
|
|
|
* LICENSE: GPL - See COPYING in the top level directory
|
2017-04-02 14:46:54 +00:00
|
|
|
* FILE: base/applications/rapps/loaddlg.cpp
|
2015-04-22 16:53:55 +00:00
|
|
|
* PURPOSE: Displaying a download dialog
|
2017-07-10 21:02:24 +00:00
|
|
|
* COPYRIGHT: Copyright 2001 John R. Sheets (for CodeWeavers)
|
|
|
|
* Copyright 2004 Mike McCormack (for CodeWeavers)
|
|
|
|
* Copyright 2005 Ge van Geldorp (gvg@reactos.org)
|
|
|
|
* Copyright 2009 Dmitry Chapyshev (dmitry@reactos.org)
|
2016-03-25 02:10:08 +00:00
|
|
|
* Copyright 2015 Ismael Ferreras Morezuelas (swyterzone+ros@gmail.com)
|
2017-07-10 21:02:24 +00:00
|
|
|
* Copyright 2017 Alexander Shaposhnikov (chaez.san@gmail.com)
|
2015-04-22 16:53:55 +00:00
|
|
|
*/
|
2017-07-10 21:02:24 +00:00
|
|
|
|
2015-04-22 16:53:55 +00:00
|
|
|
/*
|
|
|
|
* Based on Wine dlls/shdocvw/shdocvw_main.c
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "rapps.h"
|
2015-04-23 23:08:21 +00:00
|
|
|
|
|
|
|
#include <shlobj_undoc.h>
|
|
|
|
#include <shlguid_undoc.h>
|
|
|
|
|
|
|
|
#include <atlbase.h>
|
|
|
|
#include <atlcom.h>
|
2015-04-22 16:53:55 +00:00
|
|
|
#include <wininet.h>
|
2015-04-23 23:08:21 +00:00
|
|
|
#include <shellutils.h>
|
2016-03-25 02:10:08 +00:00
|
|
|
#include <windowsx.h>
|
2015-04-22 16:53:55 +00:00
|
|
|
|
|
|
|
static PAPPLICATION_INFO AppInfo;
|
|
|
|
|
2015-04-23 23:08:21 +00:00
|
|
|
class CDownloadDialog :
|
|
|
|
public CComObjectRootEx<CComMultiThreadModelNoCS>,
|
|
|
|
public IBindStatusCallback
|
2015-04-22 16:53:55 +00:00
|
|
|
{
|
2015-04-23 23:08:21 +00:00
|
|
|
HWND m_hDialog;
|
|
|
|
PBOOL m_pbCancelled;
|
2016-03-25 02:10:08 +00:00
|
|
|
BOOL m_UrlHasBeenCopied;
|
2015-04-22 16:53:55 +00:00
|
|
|
|
2015-04-23 23:08:21 +00:00
|
|
|
public:
|
|
|
|
~CDownloadDialog()
|
2015-04-22 16:53:55 +00:00
|
|
|
{
|
2015-04-23 23:08:21 +00:00
|
|
|
DestroyWindow(m_hDialog);
|
|
|
|
}
|
2017-07-10 21:02:24 +00:00
|
|
|
|
2015-04-23 23:08:21 +00:00
|
|
|
HRESULT Initialize(HWND Dlg, BOOL *pbCancelled)
|
|
|
|
{
|
|
|
|
m_hDialog = Dlg;
|
|
|
|
m_pbCancelled = pbCancelled;
|
2016-03-25 02:10:08 +00:00
|
|
|
m_UrlHasBeenCopied = FALSE;
|
2015-04-22 16:53:55 +00:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2015-04-23 23:08:21 +00:00
|
|
|
virtual HRESULT STDMETHODCALLTYPE OnStartBinding(
|
|
|
|
DWORD dwReserved,
|
|
|
|
IBinding *pib)
|
|
|
|
{
|
|
|
|
return S_OK;
|
|
|
|
}
|
2015-04-22 16:53:55 +00:00
|
|
|
|
2015-04-23 23:08:21 +00:00
|
|
|
virtual HRESULT STDMETHODCALLTYPE GetPriority(
|
|
|
|
LONG *pnPriority)
|
|
|
|
{
|
|
|
|
return S_OK;
|
|
|
|
}
|
2015-04-22 16:53:55 +00:00
|
|
|
|
2015-04-23 23:08:21 +00:00
|
|
|
virtual HRESULT STDMETHODCALLTYPE OnLowResource(
|
|
|
|
DWORD reserved)
|
2015-04-22 16:53:55 +00:00
|
|
|
{
|
2015-04-23 23:08:21 +00:00
|
|
|
return S_OK;
|
2015-04-22 16:53:55 +00:00
|
|
|
}
|
|
|
|
|
2015-04-23 23:08:21 +00:00
|
|
|
virtual HRESULT STDMETHODCALLTYPE OnProgress(
|
|
|
|
ULONG ulProgress,
|
|
|
|
ULONG ulProgressMax,
|
|
|
|
ULONG ulStatusCode,
|
|
|
|
LPCWSTR szStatusText)
|
|
|
|
{
|
|
|
|
HWND Item;
|
|
|
|
LONG r;
|
2015-04-22 16:53:55 +00:00
|
|
|
|
2015-04-23 23:08:21 +00:00
|
|
|
Item = GetDlgItem(m_hDialog, IDC_DOWNLOAD_PROGRESS);
|
|
|
|
if (Item && ulProgressMax)
|
|
|
|
{
|
2016-03-25 02:10:08 +00:00
|
|
|
WCHAR szProgress[100];
|
|
|
|
WCHAR szProgressMax[100];
|
2017-07-10 21:02:24 +00:00
|
|
|
UINT uiPercentage = ((ULONGLONG) ulProgress * 100) / ulProgressMax;
|
2016-03-25 02:10:08 +00:00
|
|
|
|
|
|
|
/* send the current progress to the progress bar */
|
|
|
|
SendMessageW(Item, PBM_SETPOS, uiPercentage, 0);
|
|
|
|
|
2016-11-05 14:55:55 +00:00
|
|
|
/* format the bits and bytes into pretty and accessible units... */
|
2016-03-25 02:10:08 +00:00
|
|
|
StrFormatByteSizeW(ulProgress, szProgress, _countof(szProgress));
|
|
|
|
StrFormatByteSizeW(ulProgressMax, szProgressMax, _countof(szProgressMax));
|
|
|
|
|
|
|
|
/* ...and post all of it to our subclassed progress bar text subroutine */
|
2017-07-10 21:02:24 +00:00
|
|
|
ATL::CStringW m_ProgressText;
|
|
|
|
m_ProgressText.Format(L"%u%% \x2014 %ls / %ls",
|
|
|
|
uiPercentage,
|
|
|
|
szProgress,
|
|
|
|
szProgressMax);
|
|
|
|
SendMessageW(Item, WM_SETTEXT, 0, (LPARAM) m_ProgressText.GetBuffer());
|
|
|
|
m_ProgressText.ReleaseBuffer();
|
2015-04-23 23:08:21 +00:00
|
|
|
}
|
2015-04-22 16:53:55 +00:00
|
|
|
|
2015-04-23 23:08:21 +00:00
|
|
|
Item = GetDlgItem(m_hDialog, IDC_DOWNLOAD_STATUS);
|
2016-03-25 02:10:08 +00:00
|
|
|
if (Item && szStatusText && wcslen(szStatusText) > 0 && m_UrlHasBeenCopied == FALSE)
|
2015-04-23 23:08:21 +00:00
|
|
|
{
|
2016-03-25 02:10:08 +00:00
|
|
|
DWORD len = wcslen(szStatusText) + 1;
|
2017-07-10 21:02:24 +00:00
|
|
|
ATL::CStringW buf;
|
2016-03-25 02:10:08 +00:00
|
|
|
|
2017-07-10 21:02:24 +00:00
|
|
|
/* beautify our url for display purposes */
|
|
|
|
if (!InternetCanonicalizeUrlW(szStatusText, buf.GetBuffer(len), &len, ICU_DECODE | ICU_NO_ENCODE))
|
2016-03-25 02:10:08 +00:00
|
|
|
{
|
2017-07-10 21:02:24 +00:00
|
|
|
/* just use the original */
|
|
|
|
buf.ReleaseBuffer();
|
|
|
|
buf = szStatusText;
|
2016-03-25 02:10:08 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-07-10 21:02:24 +00:00
|
|
|
buf.ReleaseBuffer();
|
2016-03-25 02:10:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* paste it into our dialog and don't do it again in this instance */
|
2017-07-10 21:02:24 +00:00
|
|
|
SendMessageW(Item, WM_SETTEXT, 0, (LPARAM) buf.GetBuffer());
|
|
|
|
buf.ReleaseBuffer();
|
2016-03-25 02:10:08 +00:00
|
|
|
m_UrlHasBeenCopied = TRUE;
|
2015-04-23 23:08:21 +00:00
|
|
|
}
|
2015-04-22 16:53:55 +00:00
|
|
|
|
2015-04-23 23:08:21 +00:00
|
|
|
SetLastError(0);
|
|
|
|
r = GetWindowLongPtrW(m_hDialog, GWLP_USERDATA);
|
|
|
|
if (0 != r || 0 != GetLastError())
|
|
|
|
{
|
|
|
|
*m_pbCancelled = TRUE;
|
|
|
|
return E_ABORT;
|
|
|
|
}
|
2015-04-22 16:53:55 +00:00
|
|
|
|
2015-04-23 23:08:21 +00:00
|
|
|
return S_OK;
|
|
|
|
}
|
2015-04-22 16:53:55 +00:00
|
|
|
|
2015-04-23 23:08:21 +00:00
|
|
|
virtual HRESULT STDMETHODCALLTYPE OnStopBinding(
|
|
|
|
HRESULT hresult,
|
|
|
|
LPCWSTR szError)
|
2015-04-22 16:53:55 +00:00
|
|
|
{
|
2015-04-23 23:08:21 +00:00
|
|
|
return S_OK;
|
2015-04-22 16:53:55 +00:00
|
|
|
}
|
|
|
|
|
2015-04-23 23:08:21 +00:00
|
|
|
virtual HRESULT STDMETHODCALLTYPE GetBindInfo(
|
|
|
|
DWORD *grfBINDF,
|
|
|
|
BINDINFO *pbindinfo)
|
2015-04-22 16:53:55 +00:00
|
|
|
{
|
2015-04-23 23:08:21 +00:00
|
|
|
return S_OK;
|
2015-04-22 16:53:55 +00:00
|
|
|
}
|
|
|
|
|
2015-04-23 23:08:21 +00:00
|
|
|
virtual HRESULT STDMETHODCALLTYPE OnDataAvailable(
|
|
|
|
DWORD grfBSCF,
|
|
|
|
DWORD dwSize,
|
|
|
|
FORMATETC *pformatetc,
|
|
|
|
STGMEDIUM *pstgmed)
|
2015-04-22 16:53:55 +00:00
|
|
|
{
|
2015-04-23 23:08:21 +00:00
|
|
|
return S_OK;
|
2015-04-22 16:53:55 +00:00
|
|
|
}
|
|
|
|
|
2015-04-23 23:08:21 +00:00
|
|
|
virtual HRESULT STDMETHODCALLTYPE OnObjectAvailable(
|
|
|
|
REFIID riid,
|
|
|
|
IUnknown *punk)
|
|
|
|
{
|
|
|
|
return S_OK;
|
|
|
|
}
|
2015-04-22 16:53:55 +00:00
|
|
|
|
2015-04-23 23:08:21 +00:00
|
|
|
BEGIN_COM_MAP(CDownloadDialog)
|
|
|
|
COM_INTERFACE_ENTRY_IID(IID_IBindStatusCallback, IBindStatusCallback)
|
|
|
|
END_COM_MAP()
|
2015-04-22 16:53:55 +00:00
|
|
|
};
|
|
|
|
|
2015-04-23 23:08:21 +00:00
|
|
|
extern "C"
|
|
|
|
HRESULT WINAPI CDownloadDialog_Constructor(HWND Dlg, BOOL *pbCancelled, REFIID riid, LPVOID *ppv)
|
2015-04-22 16:53:55 +00:00
|
|
|
{
|
2015-04-23 23:08:21 +00:00
|
|
|
return ShellObjectCreatorInit<CDownloadDialog>(Dlg, pbCancelled, riid, ppv);
|
2015-04-22 16:53:55 +00:00
|
|
|
}
|
|
|
|
|
2016-03-25 02:10:08 +00:00
|
|
|
#ifdef USE_CERT_PINNING
|
|
|
|
static BOOL CertIsValid(HINTERNET hInternet, LPWSTR lpszHostName)
|
|
|
|
{
|
|
|
|
HINTERNET hConnect;
|
|
|
|
HINTERNET hRequest;
|
|
|
|
DWORD certInfoLength;
|
|
|
|
BOOL Ret = FALSE;
|
|
|
|
INTERNET_CERTIFICATE_INFOW certInfo;
|
|
|
|
|
|
|
|
hConnect = InternetConnectW(hInternet, lpszHostName, INTERNET_DEFAULT_HTTPS_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, INTERNET_FLAG_SECURE, 0);
|
|
|
|
if (hConnect)
|
|
|
|
{
|
|
|
|
hRequest = HttpOpenRequestW(hConnect, L"HEAD", NULL, NULL, NULL, NULL, INTERNET_FLAG_SECURE, 0);
|
|
|
|
if (hRequest != NULL)
|
|
|
|
{
|
|
|
|
Ret = HttpSendRequestW(hRequest, L"", 0, NULL, 0);
|
|
|
|
if (Ret)
|
|
|
|
{
|
2016-10-06 12:22:32 +00:00
|
|
|
certInfoLength = sizeof(certInfo);
|
2016-03-25 02:10:08 +00:00
|
|
|
Ret = InternetQueryOptionW(hRequest,
|
|
|
|
INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
|
|
|
|
&certInfo,
|
|
|
|
&certInfoLength);
|
|
|
|
if (Ret)
|
|
|
|
{
|
|
|
|
if (certInfo.lpszEncryptionAlgName)
|
|
|
|
LocalFree(certInfo.lpszEncryptionAlgName);
|
|
|
|
if (certInfo.lpszIssuerInfo)
|
|
|
|
{
|
2017-07-10 21:02:24 +00:00
|
|
|
if (strcmp((LPSTR) certInfo.lpszIssuerInfo, CERT_ISSUER_INFO) != 0)
|
2016-03-25 02:10:08 +00:00
|
|
|
Ret = FALSE;
|
|
|
|
LocalFree(certInfo.lpszIssuerInfo);
|
|
|
|
}
|
|
|
|
if (certInfo.lpszProtocolName)
|
|
|
|
LocalFree(certInfo.lpszProtocolName);
|
|
|
|
if (certInfo.lpszSignatureAlgName)
|
|
|
|
LocalFree(certInfo.lpszSignatureAlgName);
|
|
|
|
if (certInfo.lpszSubjectInfo)
|
|
|
|
{
|
2017-07-10 21:02:24 +00:00
|
|
|
if (strcmp((LPSTR) certInfo.lpszSubjectInfo, CERT_SUBJECT_INFO) != 0)
|
2016-03-25 02:10:08 +00:00
|
|
|
Ret = FALSE;
|
|
|
|
LocalFree(certInfo.lpszSubjectInfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
InternetCloseHandle(hRequest);
|
|
|
|
}
|
|
|
|
InternetCloseHandle(hConnect);
|
|
|
|
}
|
|
|
|
return Ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-07-10 21:02:24 +00:00
|
|
|
inline VOID
|
|
|
|
MessageBox_LoadString(HWND hMainWnd, INT StringID)
|
|
|
|
{
|
|
|
|
ATL::CString szMsgText;
|
|
|
|
if (szMsgText.LoadStringW(hInst, StringID))
|
|
|
|
MessageBoxW(hMainWnd, szMsgText.GetString(), NULL, MB_OK | MB_ICONERROR);
|
|
|
|
}
|
2016-03-25 02:10:08 +00:00
|
|
|
|
2015-04-22 16:53:55 +00:00
|
|
|
static
|
|
|
|
DWORD WINAPI
|
|
|
|
ThreadFunc(LPVOID Context)
|
|
|
|
{
|
2015-04-23 23:08:21 +00:00
|
|
|
CComPtr<IBindStatusCallback> dl;
|
2017-07-10 21:02:24 +00:00
|
|
|
ATL::CStringW Path;
|
2016-03-25 02:10:08 +00:00
|
|
|
PWSTR p, q;
|
2015-04-22 16:53:55 +00:00
|
|
|
HWND Dlg = (HWND) Context;
|
2015-04-23 23:08:21 +00:00
|
|
|
ULONG dwContentLen, dwBytesWritten, dwBytesRead, dwStatus;
|
|
|
|
ULONG dwCurrentBytesRead = 0;
|
|
|
|
ULONG dwStatusLen = sizeof(dwStatus);
|
2015-04-22 16:53:55 +00:00
|
|
|
BOOL bCancelled = FALSE;
|
|
|
|
BOOL bTempfile = FALSE;
|
|
|
|
BOOL bCab = FALSE;
|
|
|
|
HINTERNET hOpen = NULL;
|
|
|
|
HINTERNET hFile = NULL;
|
|
|
|
HANDLE hOut = INVALID_HANDLE_VALUE;
|
|
|
|
unsigned char lpBuffer[4096];
|
2017-07-10 21:02:24 +00:00
|
|
|
LPCWSTR lpszAgent = L"RApps/1.0";
|
2015-04-22 16:53:55 +00:00
|
|
|
URL_COMPONENTS urlComponents;
|
2016-03-25 02:10:08 +00:00
|
|
|
size_t urlLength, filenameLength;
|
2015-04-22 16:53:55 +00:00
|
|
|
|
2016-03-25 02:10:08 +00:00
|
|
|
/* build the path for the download */
|
2015-04-22 16:53:55 +00:00
|
|
|
p = wcsrchr(AppInfo->szUrlDownload, L'/');
|
2016-03-25 02:10:08 +00:00
|
|
|
q = wcsrchr(AppInfo->szUrlDownload, L'?');
|
2017-07-10 21:02:24 +00:00
|
|
|
|
2016-03-25 02:10:08 +00:00
|
|
|
/* do we have a final slash separator? */
|
2015-04-22 16:53:55 +00:00
|
|
|
if (!p)
|
|
|
|
goto end;
|
|
|
|
|
2016-03-25 02:10:08 +00:00
|
|
|
/* prepare the tentative length of the filename, maybe we've to remove part of it later on */
|
|
|
|
filenameLength = wcslen(p) * sizeof(WCHAR);
|
|
|
|
|
|
|
|
/* do we have query arguments in the target URL after the filename? account for them
|
|
|
|
(e.g. https://example.org/myfile.exe?no_adware_plz) */
|
|
|
|
if (q && q > p && (q - p) > 0)
|
|
|
|
filenameLength -= wcslen(q - 1) * sizeof(WCHAR);
|
2015-04-22 16:53:55 +00:00
|
|
|
|
2016-03-25 02:10:08 +00:00
|
|
|
/* is this URL an update package for RAPPS? if so store it in a different place */
|
2017-07-10 21:02:24 +00:00
|
|
|
if (AppInfo->szUrlDownload == APPLICATION_DATABASE_URL)
|
2016-03-25 02:10:08 +00:00
|
|
|
{
|
|
|
|
bCab = TRUE;
|
2017-07-10 21:02:24 +00:00
|
|
|
if (!GetStorageDirectory(Path))
|
2016-03-25 02:10:08 +00:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-07-10 21:02:24 +00:00
|
|
|
Path = SettingsInfo.szDownloadDir;
|
2016-03-25 02:10:08 +00:00
|
|
|
}
|
2015-04-22 16:53:55 +00:00
|
|
|
|
2016-03-25 02:10:08 +00:00
|
|
|
/* is the path valid? can we access it? */
|
2017-07-10 21:02:24 +00:00
|
|
|
if (GetFileAttributesW(Path.GetString()) == INVALID_FILE_ATTRIBUTES)
|
2015-04-22 16:53:55 +00:00
|
|
|
{
|
2017-07-10 21:02:24 +00:00
|
|
|
if (!CreateDirectoryW(Path.GetString(), NULL))
|
2015-04-22 16:53:55 +00:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2016-03-25 02:10:08 +00:00
|
|
|
/* append a \ to the provided file system path, and the filename portion from the URL after that */
|
2017-07-10 21:02:24 +00:00
|
|
|
Path.Format(L"\\%ls", (p + 1));
|
2015-04-22 16:53:55 +00:00
|
|
|
|
2017-07-10 21:02:24 +00:00
|
|
|
if (!bCab && AppInfo->szSHA1[0] && GetFileAttributesW(Path.GetString()) != INVALID_FILE_ATTRIBUTES)
|
2016-03-18 06:40:39 +00:00
|
|
|
{
|
|
|
|
/* only open it in case of total correctness */
|
2017-07-10 21:02:24 +00:00
|
|
|
if (VerifyInteg(AppInfo->szSHA1, Path))
|
2016-03-18 06:40:39 +00:00
|
|
|
goto run;
|
|
|
|
}
|
|
|
|
|
2015-04-22 16:53:55 +00:00
|
|
|
/* download it */
|
|
|
|
bTempfile = TRUE;
|
2015-04-23 23:08:21 +00:00
|
|
|
CDownloadDialog_Constructor(Dlg, &bCancelled, IID_PPV_ARG(IBindStatusCallback, &dl));
|
2015-04-22 16:53:55 +00:00
|
|
|
|
|
|
|
if (dl == NULL)
|
|
|
|
goto end;
|
|
|
|
|
2016-03-25 02:10:08 +00:00
|
|
|
/* FIXME: this should just be using the system-wide proxy settings */
|
2017-07-10 21:02:24 +00:00
|
|
|
switch (SettingsInfo.Proxy)
|
2015-04-22 16:53:55 +00:00
|
|
|
{
|
2017-07-10 21:02:24 +00:00
|
|
|
case 0: /* preconfig */
|
|
|
|
hOpen = InternetOpenW(lpszAgent, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
|
|
|
|
break;
|
|
|
|
case 1: /* direct (no proxy) */
|
|
|
|
hOpen = InternetOpenW(lpszAgent, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
|
|
|
|
break;
|
|
|
|
case 2: /* use proxy */
|
|
|
|
hOpen = InternetOpenW(lpszAgent, INTERNET_OPEN_TYPE_PROXY, SettingsInfo.szProxyServer, SettingsInfo.szNoProxyFor, 0);
|
|
|
|
break;
|
|
|
|
default: /* preconfig */
|
|
|
|
hOpen = InternetOpenW(lpszAgent, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
|
|
|
|
break;
|
2015-04-22 16:53:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!hOpen)
|
|
|
|
goto end;
|
|
|
|
|
2017-07-10 21:02:24 +00:00
|
|
|
hFile = InternetOpenUrlW(hOpen, AppInfo->szUrlDownload, NULL, 0, INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_KEEP_CONNECTION, 0);
|
2015-04-22 16:53:55 +00:00
|
|
|
if (!hFile)
|
2016-09-29 21:31:11 +00:00
|
|
|
{
|
2017-07-10 21:02:24 +00:00
|
|
|
MessageBox_LoadString(hMainWnd, IDS_UNABLE_TO_DOWNLOAD2);
|
2015-04-22 16:53:55 +00:00
|
|
|
goto end;
|
2016-09-29 21:31:11 +00:00
|
|
|
}
|
2015-04-22 16:53:55 +00:00
|
|
|
|
|
|
|
if (!HttpQueryInfoW(hFile, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &dwStatus, &dwStatusLen, NULL))
|
|
|
|
goto end;
|
|
|
|
|
2017-07-10 21:02:24 +00:00
|
|
|
if (dwStatus != HTTP_STATUS_OK)
|
2015-04-22 16:53:55 +00:00
|
|
|
{
|
2017-07-10 21:02:24 +00:00
|
|
|
MessageBox_LoadString(hMainWnd, IDS_UNABLE_TO_DOWNLOAD);
|
2015-04-22 16:53:55 +00:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
dwStatusLen = sizeof(dwStatus);
|
|
|
|
|
|
|
|
memset(&urlComponents, 0, sizeof(urlComponents));
|
|
|
|
urlComponents.dwStructSize = sizeof(urlComponents);
|
2016-03-25 02:10:08 +00:00
|
|
|
|
2017-07-10 22:45:53 +00:00
|
|
|
urlLength = AppInfo->szUrlDownload.GetLength();
|
2016-03-25 02:10:08 +00:00
|
|
|
urlComponents.dwSchemeLength = urlLength + 1;
|
2017-07-10 21:02:24 +00:00
|
|
|
urlComponents.lpszScheme = (LPWSTR) malloc(urlComponents.dwSchemeLength * sizeof(WCHAR));
|
2016-03-25 02:10:08 +00:00
|
|
|
urlComponents.dwHostNameLength = urlLength + 1;
|
2017-07-10 21:02:24 +00:00
|
|
|
urlComponents.lpszHostName = (LPWSTR) malloc(urlComponents.dwHostNameLength * sizeof(WCHAR));
|
2016-03-25 02:10:08 +00:00
|
|
|
|
2017-07-10 21:02:24 +00:00
|
|
|
if (!InternetCrackUrlW(AppInfo->szUrlDownload, urlLength + 1, ICU_DECODE | ICU_ESCAPE, &urlComponents))
|
2015-04-22 16:53:55 +00:00
|
|
|
goto end;
|
2017-07-10 21:02:24 +00:00
|
|
|
|
|
|
|
if (urlComponents.nScheme == INTERNET_SCHEME_HTTP || urlComponents.nScheme == INTERNET_SCHEME_HTTPS)
|
2015-04-22 16:53:55 +00:00
|
|
|
HttpQueryInfo(hFile, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER, &dwContentLen, &dwStatus, 0);
|
|
|
|
|
2017-07-10 21:02:24 +00:00
|
|
|
if (urlComponents.nScheme == INTERNET_SCHEME_FTP)
|
2015-04-22 16:53:55 +00:00
|
|
|
dwContentLen = FtpGetFileSize(hFile, &dwStatus);
|
|
|
|
|
2016-03-25 02:10:08 +00:00
|
|
|
#ifdef USE_CERT_PINNING
|
|
|
|
/* are we using HTTPS to download the RAPPS update package? check if the certificate is original */
|
|
|
|
if ((urlComponents.nScheme == INTERNET_SCHEME_HTTPS) &&
|
|
|
|
(wcscmp(AppInfo->szUrlDownload, APPLICATION_DATABASE_URL) == 0) &&
|
|
|
|
(!CertIsValid(hOpen, urlComponents.lpszHostName)))
|
|
|
|
{
|
2017-07-10 21:02:24 +00:00
|
|
|
MessageBox_LoadString(hMainWnd, IDS_CERT_DOES_NOT_MATCH);
|
2016-03-25 02:10:08 +00:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-04-22 16:53:55 +00:00
|
|
|
free(urlComponents.lpszScheme);
|
2016-03-25 02:10:08 +00:00
|
|
|
free(urlComponents.lpszHostName);
|
2015-04-22 16:53:55 +00:00
|
|
|
|
2017-07-10 21:02:24 +00:00
|
|
|
hOut = CreateFileW(Path.GetString(), GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, 0, NULL);
|
2015-04-22 16:53:55 +00:00
|
|
|
|
|
|
|
if (hOut == INVALID_HANDLE_VALUE)
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2016-03-25 02:14:36 +00:00
|
|
|
if (!InternetReadFile(hFile, lpBuffer, _countof(lpBuffer), &dwBytesRead))
|
|
|
|
{
|
2017-07-10 21:02:24 +00:00
|
|
|
MessageBox_LoadString(hMainWnd, IDS_INTERRUPTED_DOWNLOAD);
|
2016-03-25 02:14:36 +00:00
|
|
|
goto end;
|
|
|
|
}
|
2017-07-10 21:02:24 +00:00
|
|
|
|
2016-03-25 02:14:36 +00:00
|
|
|
if (!WriteFile(hOut, &lpBuffer[0], dwBytesRead, &dwBytesWritten, NULL))
|
|
|
|
{
|
2017-07-10 21:02:24 +00:00
|
|
|
MessageBox_LoadString(hMainWnd, IDS_UNABLE_TO_WRITE);
|
2016-03-25 02:14:36 +00:00
|
|
|
goto end;
|
|
|
|
}
|
2017-07-10 21:02:24 +00:00
|
|
|
|
2015-04-22 16:53:55 +00:00
|
|
|
dwCurrentBytesRead += dwBytesRead;
|
2015-04-23 23:08:21 +00:00
|
|
|
dl->OnProgress(dwCurrentBytesRead, dwContentLen, 0, AppInfo->szUrlDownload);
|
2017-07-10 21:02:24 +00:00
|
|
|
} while (dwBytesRead && !bCancelled);
|
2015-04-22 16:53:55 +00:00
|
|
|
|
|
|
|
CloseHandle(hOut);
|
|
|
|
hOut = INVALID_HANDLE_VALUE;
|
|
|
|
|
|
|
|
if (bCancelled)
|
|
|
|
goto end;
|
|
|
|
|
2016-03-18 06:40:39 +00:00
|
|
|
/* if this thing isn't a RAPPS update and it has a SHA-1 checksum
|
|
|
|
verify its integrity by using the native advapi32.A_SHA1 functions */
|
|
|
|
if (!bCab && AppInfo->szSHA1[0] != 0)
|
|
|
|
{
|
2017-07-10 21:02:24 +00:00
|
|
|
ATL::CStringW szMsgText;
|
2016-03-18 06:40:39 +00:00
|
|
|
|
|
|
|
/* change a few strings in the download dialog to reflect the verification process */
|
2017-07-10 21:02:24 +00:00
|
|
|
if (!szMsgText.LoadStringW(hInst, IDS_INTEG_CHECK_TITLE))
|
|
|
|
goto end;
|
2016-03-18 06:40:39 +00:00
|
|
|
|
2017-07-10 21:02:24 +00:00
|
|
|
SetWindowText(Dlg, szMsgText.GetString());
|
|
|
|
SendMessageW(GetDlgItem(Dlg, IDC_DOWNLOAD_STATUS), WM_SETTEXT, 0, (LPARAM) Path.GetBuffer());
|
|
|
|
Path.ReleaseBuffer();
|
2016-03-18 06:40:39 +00:00
|
|
|
|
|
|
|
/* this may take a while, depending on the file size */
|
2017-07-10 21:02:24 +00:00
|
|
|
if (!VerifyInteg(AppInfo->szSHA1, Path.GetString()))
|
2016-03-18 06:40:39 +00:00
|
|
|
{
|
2017-07-10 21:02:24 +00:00
|
|
|
if (!szMsgText.LoadStringW(hInst, IDS_INTEG_CHECK_FAIL))
|
2016-03-18 06:40:39 +00:00
|
|
|
goto end;
|
|
|
|
|
2017-07-10 21:02:24 +00:00
|
|
|
MessageBoxW(Dlg, szMsgText.GetString(), NULL, MB_OK | MB_ICONERROR);
|
2016-03-18 06:40:39 +00:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-22 16:53:55 +00:00
|
|
|
ShowWindow(Dlg, SW_HIDE);
|
|
|
|
|
2016-03-18 06:40:39 +00:00
|
|
|
run:
|
2015-04-22 16:53:55 +00:00
|
|
|
/* run it */
|
|
|
|
if (!bCab)
|
2017-07-10 21:02:24 +00:00
|
|
|
ShellExecuteW(NULL, L"open", Path.GetString(), NULL, NULL, SW_SHOWNORMAL);
|
2015-04-22 16:53:55 +00:00
|
|
|
|
|
|
|
end:
|
|
|
|
if (hOut != INVALID_HANDLE_VALUE)
|
|
|
|
CloseHandle(hOut);
|
|
|
|
|
|
|
|
InternetCloseHandle(hFile);
|
|
|
|
InternetCloseHandle(hOpen);
|
|
|
|
|
|
|
|
if (bTempfile)
|
|
|
|
{
|
|
|
|
if (bCancelled || (SettingsInfo.bDelInstaller && !bCab))
|
2017-07-10 21:02:24 +00:00
|
|
|
DeleteFileW(Path.GetString());
|
2015-04-22 16:53:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
EndDialog(Dlg, 0);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-03-25 02:10:08 +00:00
|
|
|
|
|
|
|
LRESULT CALLBACK
|
|
|
|
DownloadProgressProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
|
|
|
|
{
|
2017-07-10 21:02:24 +00:00
|
|
|
static ATL::CStringW szProgressText;
|
2016-03-25 02:10:08 +00:00
|
|
|
|
|
|
|
switch (uMsg)
|
|
|
|
{
|
2017-07-10 21:02:24 +00:00
|
|
|
case WM_SETTEXT:
|
|
|
|
{
|
|
|
|
if (lParam)
|
2016-03-25 02:10:08 +00:00
|
|
|
{
|
2017-07-10 21:02:24 +00:00
|
|
|
szProgressText = (PCWSTR) lParam;
|
2016-03-25 02:10:08 +00:00
|
|
|
}
|
2017-07-10 21:02:24 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
2016-03-25 02:10:08 +00:00
|
|
|
|
2017-07-10 21:02:24 +00:00
|
|
|
case WM_ERASEBKGND:
|
|
|
|
case WM_PAINT:
|
|
|
|
{
|
|
|
|
PAINTSTRUCT ps;
|
|
|
|
HDC hDC = BeginPaint(hWnd, &ps), hdcMem;
|
|
|
|
HBITMAP hbmMem;
|
|
|
|
HANDLE hOld;
|
|
|
|
RECT myRect;
|
|
|
|
UINT win_width, win_height;
|
|
|
|
|
|
|
|
GetClientRect(hWnd, &myRect);
|
|
|
|
|
|
|
|
/* grab the progress bar rect size */
|
|
|
|
win_width = myRect.right - myRect.left;
|
|
|
|
win_height = myRect.bottom - myRect.top;
|
|
|
|
|
|
|
|
/* create an off-screen DC for double-buffering */
|
|
|
|
hdcMem = CreateCompatibleDC(hDC);
|
|
|
|
hbmMem = CreateCompatibleBitmap(hDC, win_width, win_height);
|
|
|
|
|
|
|
|
hOld = SelectObject(hdcMem, hbmMem);
|
|
|
|
|
|
|
|
/* call the original draw code and redirect it to our memory buffer */
|
|
|
|
DefSubclassProc(hWnd, uMsg, (WPARAM) hdcMem, lParam);
|
|
|
|
|
|
|
|
/* draw our nifty progress text over it */
|
|
|
|
SelectFont(hdcMem, GetStockFont(DEFAULT_GUI_FONT));
|
|
|
|
DrawShadowText(hdcMem, szProgressText.GetString(), szProgressText.GetLength(),
|
|
|
|
&myRect,
|
|
|
|
DT_CENTER | DT_VCENTER | DT_NOPREFIX | DT_SINGLELINE,
|
|
|
|
GetSysColor(COLOR_CAPTIONTEXT),
|
|
|
|
GetSysColor(COLOR_3DSHADOW),
|
|
|
|
1, 1);
|
|
|
|
|
|
|
|
/* transfer the off-screen DC to the screen */
|
|
|
|
BitBlt(hDC, 0, 0, win_width, win_height, hdcMem, 0, 0, SRCCOPY);
|
|
|
|
|
|
|
|
/* free the off-screen DC */
|
|
|
|
SelectObject(hdcMem, hOld);
|
|
|
|
DeleteObject(hbmMem);
|
|
|
|
DeleteDC(hdcMem);
|
|
|
|
|
|
|
|
EndPaint(hWnd, &ps);
|
|
|
|
return 0;
|
|
|
|
}
|
2016-03-25 02:10:08 +00:00
|
|
|
|
2017-07-10 21:02:24 +00:00
|
|
|
/* Raymond Chen says that we should safely unsubclass all the things!
|
|
|
|
(http://blogs.msdn.com/b/oldnewthing/archive/2003/11/11/55653.aspx) */
|
|
|
|
case WM_NCDESTROY:
|
|
|
|
{
|
|
|
|
szProgressText.Empty();
|
|
|
|
RemoveWindowSubclass(hWnd, DownloadProgressProc, uIdSubclass);
|
|
|
|
}
|
|
|
|
/* Fall-through */
|
|
|
|
default:
|
|
|
|
return DefSubclassProc(hWnd, uMsg, wParam, lParam);
|
2016-03-25 02:10:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-22 16:53:55 +00:00
|
|
|
static
|
|
|
|
INT_PTR CALLBACK
|
2016-03-25 02:10:08 +00:00
|
|
|
DownloadDlgProc(HWND Dlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
|
2015-04-22 16:53:55 +00:00
|
|
|
{
|
|
|
|
HANDLE Thread;
|
|
|
|
DWORD ThreadId;
|
|
|
|
HWND Item;
|
|
|
|
|
2016-03-25 02:10:08 +00:00
|
|
|
switch (uMsg)
|
2015-04-22 16:53:55 +00:00
|
|
|
{
|
2017-07-10 21:02:24 +00:00
|
|
|
case WM_INITDIALOG:
|
|
|
|
{
|
|
|
|
HICON hIconSm = NULL, hIconBg = NULL;
|
2016-03-24 23:03:35 +00:00
|
|
|
|
2017-07-10 21:02:24 +00:00
|
|
|
hIconBg = (HICON) GetClassLongPtr(hMainWnd, GCLP_HICON);
|
|
|
|
hIconSm = (HICON) GetClassLongPtr(hMainWnd, GCLP_HICONSM);
|
2015-04-22 16:53:55 +00:00
|
|
|
|
2017-07-10 21:02:24 +00:00
|
|
|
if (hIconBg && hIconSm)
|
|
|
|
{
|
|
|
|
SendMessageW(Dlg, WM_SETICON, ICON_BIG, (LPARAM) hIconBg);
|
|
|
|
SendMessageW(Dlg, WM_SETICON, ICON_SMALL, (LPARAM) hIconSm);
|
|
|
|
}
|
2015-04-22 16:53:55 +00:00
|
|
|
|
2017-07-10 21:02:24 +00:00
|
|
|
SetWindowLongPtrW(Dlg, GWLP_USERDATA, 0);
|
|
|
|
Item = GetDlgItem(Dlg, IDC_DOWNLOAD_PROGRESS);
|
|
|
|
if (Item)
|
|
|
|
{
|
|
|
|
/* initialize the default values for our nifty progress bar
|
|
|
|
and subclass it so that it learns to print a status text */
|
|
|
|
SendMessageW(Item, PBM_SETRANGE, 0, MAKELPARAM(0, 100));
|
|
|
|
SendMessageW(Item, PBM_SETPOS, 0, 0);
|
2016-03-25 02:10:08 +00:00
|
|
|
|
2017-07-10 21:02:24 +00:00
|
|
|
SetWindowSubclass(Item, DownloadProgressProc, 0, 0);
|
|
|
|
}
|
2015-04-22 16:53:55 +00:00
|
|
|
|
2017-07-10 21:02:24 +00:00
|
|
|
/* add a neat placeholder until the download URL is retrieved */
|
|
|
|
Item = GetDlgItem(Dlg, IDC_DOWNLOAD_STATUS);
|
|
|
|
SendMessageW(Item, WM_SETTEXT, 0, (LPARAM) L"\x2022 \x2022 \x2022");
|
2016-03-25 02:10:08 +00:00
|
|
|
|
2017-07-10 21:02:24 +00:00
|
|
|
Thread = CreateThread(NULL, 0, ThreadFunc, Dlg, 0, &ThreadId);
|
|
|
|
if (!Thread)
|
2015-04-22 16:53:55 +00:00
|
|
|
return FALSE;
|
2017-07-10 21:02:24 +00:00
|
|
|
CloseHandle(Thread);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
case WM_COMMAND:
|
|
|
|
if (wParam == IDCANCEL)
|
|
|
|
{
|
|
|
|
SetWindowLongPtrW(Dlg, GWLP_USERDATA, 1);
|
|
|
|
PostMessageW(Dlg, WM_CLOSE, 0, 0);
|
|
|
|
}
|
|
|
|
return FALSE;
|
2015-04-22 16:53:55 +00:00
|
|
|
|
2017-07-10 21:02:24 +00:00
|
|
|
case WM_CLOSE:
|
|
|
|
EndDialog(Dlg, 0);
|
|
|
|
return TRUE;
|
2015-04-22 16:53:55 +00:00
|
|
|
|
2017-07-10 21:02:24 +00:00
|
|
|
default:
|
|
|
|
return FALSE;
|
2015-04-22 16:53:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL
|
|
|
|
DownloadApplication(INT Index)
|
|
|
|
{
|
|
|
|
if (!IS_AVAILABLE_ENUM(SelectedEnumType))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
AppInfo = (PAPPLICATION_INFO) ListViewGetlParam(Index);
|
2017-07-10 21:02:24 +00:00
|
|
|
if (!AppInfo)
|
|
|
|
return FALSE;
|
2015-04-22 16:53:55 +00:00
|
|
|
|
2017-07-10 21:02:24 +00:00
|
|
|
WriteLogMessage(EVENTLOG_SUCCESS, MSG_SUCCESS_INSTALL, AppInfo->szName.GetString());
|
2015-04-22 16:53:55 +00:00
|
|
|
|
|
|
|
DialogBoxW(hInst,
|
|
|
|
MAKEINTRESOURCEW(IDD_DOWNLOAD_DIALOG),
|
|
|
|
hMainWnd,
|
|
|
|
DownloadDlgProc);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
VOID
|
2015-04-23 23:08:21 +00:00
|
|
|
DownloadApplicationsDB(LPCWSTR lpUrl)
|
2015-04-22 16:53:55 +00:00
|
|
|
{
|
|
|
|
APPLICATION_INFO IntInfo;
|
2017-07-10 21:02:24 +00:00
|
|
|
IntInfo.szUrlDownload = lpUrl;
|
2015-04-22 16:53:55 +00:00
|
|
|
|
|
|
|
AppInfo = &IntInfo;
|
|
|
|
|
|
|
|
DialogBoxW(hInst,
|
|
|
|
MAKEINTRESOURCEW(IDD_DOWNLOAD_DIALOG),
|
|
|
|
hMainWnd,
|
|
|
|
DownloadDlgProc);
|
|
|
|
}
|