-
Notifications
You must be signed in to change notification settings - Fork 1
/
BrowserMain.pas
278 lines (241 loc) · 10.3 KB
/
BrowserMain.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
unit BrowserMain;
{$Include 'FastPHP.inc'}
interface
uses
// TODO: "{$IFDEF USE_SHDOCVW_TLB}_TLB{$ENDIF}" does not work with Delphi 10.2
// so you have to change the reference SHDocVw / SHDocVw_TLB yourself
Windows, Messages, SysUtils, Variants, Classes, Graphics,
Controls, Forms, Dialogs, OleCtrls, SHDocVw, ExtCtrls, StrUtils,
StdCtrls, activex, UrlMon, WebBrowserUtils;
type
TForm2 = class(TForm)
WebBrowser1: TWebBrowser;
Timer1: TTimer;
OpenDialog3: TOpenDialog;
procedure Timer1Timer(Sender: TObject);
procedure WebBrowser1BeforeNavigate2(ASender: TObject;
const pDisp: IDispatch; const URL, Flags, TargetFrameName, PostData,
Headers: OleVariant; var Cancel: WordBool);
procedure WebBrowser1WindowClosing(ASender: TObject;
IsChildWindow: WordBool; var Cancel: WordBool);
strict private
function EmbeddedWBQueryService(const rsid, iid: TGUID; out Obj{: IInterface}): HRESULT;
end;
var
Form2: TForm2;
implementation
{$R *.dfm}
uses
FastPHPUtils, Functions, ShellAPI;
// TODO: Add a lot of nice stuff to let the PHP script communicate with this host application
// For example, allow window resizing etc. (See Microsoft HTA for inspiration)
// TODO: Ajax gives Access Denied error... Create own security manager?
// TODO: History doesn't work?
// (All these ToDos: Also fix in the Editor)
// TODO: kann man eventuell auch php dateien aus einer DLL rausziehen? das wäre TOLL!!!!
// TODO: headers... cookies...
// TODO: WebBrowser1BeforeNavigate2 mit einem DLL-callback, sodass entwickler ihre eigenen fastphp:// links machen können, z.B. um DLL-Funktionen aufzurufen! (auch in JavaScript ansteuerbar?)
// TODO: let the website decide if the window is maximized etc, as well as it's caption, size and icon
// TODO: Pass parameters (argv) to PHP
type
TEmbeddedSecurityManager = class(TInterfacedObject, IInternetSecurityManager)
public
function GetSecuritySite(out ppSite: IInternetSecurityMgrSite): HResult; stdcall;
function MapUrlToZone(pwszUrl: LPCWSTR; out dwZone: DWORD; dwFlags: DWORD): HResult; stdcall;
function GetSecurityId(pwszUrl: LPCWSTR; pbSecurityId: Pointer; var cbSecurityId: DWORD; dwReserved: DWORD): HResult; stdcall;
function ProcessUrlAction(pwszUrl: LPCWSTR; dwAction: DWORD; pPolicy: Pointer; cbPolicy: DWORD; pContext: Pointer; cbContext: DWORD; dwFlags, dwReserved: DWORD): HResult; stdcall;
function QueryCustomPolicy(pwszUrl: LPCWSTR; const guidKey: TGUID; out pPolicy: Pointer; out cbPolicy: DWORD; pContext: Pointer; cbContext: DWORD; dwReserved: DWORD): HResult; stdcall;
function SetZoneMapping(dwZone: DWORD; lpszPattern: PWideChar; dwFlags: DWORD): HResult; stdcall;
function GetZoneMappings(dwZone: DWORD;out ppenumString: IEnumString; dwFlags: DWORD): HResult; stdcall;
function SetSecuritySite(pSite: IInternetSecurityMgrSite): HResult; stdcall;
end;
function TEmbeddedSecurityManager.SetSecuritySite(pSite: IInternetSecurityMgrSite): HResult; stdcall;
begin
Result := INET_E_DEFAULT_ACTION;
end;
function TEmbeddedSecurityManager.GetSecuritySite(
out ppSite: IInternetSecurityMgrSite): HResult; stdcall;
begin
Result := INET_E_DEFAULT_ACTION;
end;
function TEmbeddedSecurityManager.GetSecurityId(pwszUrl: LPCWSTR; pbSecurityId: Pointer;
var cbSecurityId: DWORD; dwReserved: DWORD): HResult; stdcall;
begin
Result := INET_E_DEFAULT_ACTION;
end;
function TEmbeddedSecurityManager.ProcessUrlAction(pwszUrl: LPCWSTR; dwAction: DWORD;
pPolicy: Pointer; cbPolicy: DWORD; pContext: Pointer; cbContext: DWORD;
dwFlags, dwReserved: DWORD): HResult; stdcall;
begin
// Result := INET_E_DEFAULT_ACTION;
// TODO: Doesn't work... Cross-Domain is still not allowed.
PDWORD(pPolicy)^ := URLPOLICY_ALLOW;
Result := S_OK;
end;
function TEmbeddedSecurityManager.QueryCustomPolicy(pwszUrl: LPCWSTR; const guidKey: TGUID;
out pPolicy: Pointer; out cbPolicy: DWORD; pContext: Pointer; cbContext: DWORD;
dwReserved: DWORD): HResult; stdcall;
begin
// Result := INET_E_DEFAULT_ACTION;
// TODO: Doesn't work... Cross-Domain is still not allowed.
PDWORD(pPolicy)^ := URLPOLICY_ALLOW;
Result := S_OK;
end;
function TEmbeddedSecurityManager.SetZoneMapping(dwZone: DWORD; lpszPattern: PWideChar;
dwFlags: DWORD): HResult; stdcall;
begin
Result := INET_E_DEFAULT_ACTION;
end;
function TEmbeddedSecurityManager.GetZoneMappings(dwZone: DWORD;out ppenumString: IEnumString;
dwFlags: DWORD): HResult; stdcall;
begin
Result := INET_E_DEFAULT_ACTION;
end;
function TEmbeddedSecurityManager.MapUrlToZone(pwszUrl: LPCWSTR; out dwZone: DWORD; dwFlags: DWORD): HResult;
begin
dwZone := URLZONE_TRUSTED;
Result := S_OK;
end;
function TForm2.EmbeddedWBQueryService(const rsid, iid: TGUID; out Obj{: IInterface}): HRESULT;
var
sam: IInternetSecurityManager;
begin
Result := E_NOINTERFACE;
//rsid ==> Service Identifier
//iid ==> Interface identifier
if IsEqualGUID(rsid, IInternetSecurityManager) and IsEqualGUID(iid, IInternetSecurityManager) then
begin
sam := TEmbeddedSecurityManager.Create;
IInterface(Obj) := sam;
Result := S_OK;
end;
end;
procedure TForm2.Timer1Timer(Sender: TObject);
var
phpScript: string;
sl: TStringList;
resourcestring
SRunningScriptPleaseWait = 'Running script... please wait...';
SPleaseEnterPHPScript = 'Please enter a PHP file to execute.';
SFileDoesNotExist = 'File %s does not exist.';
SFastPHP = 'ViaThinkSoft FastPHP';
begin
Timer1.Enabled := false;
phpScript := ParamStr(1);
// Remove Security
WebBrowser1.ServiceQuery := EmbeddedWBQueryService;
WebBrowser1.LoadHTML('<h1>'+SFastPHP+'</h1>'+SRunningScriptPleaseWait);
// TODO: nice HTML error/intro pages (as resource?)
if phpScript = '' then
begin
WebBrowser1.LoadHTML('<h1>'+SFastPHP+'</h1>'+SPleaseEnterPHPScript);
if not OpenDialog3.Execute then
begin
Abort;
end;
phpScript := OpenDialog3.FileName;
end;
if not FileExists(phpScript) then
begin
WebBrowser1.LoadHTML(Format('<h1>'+SFastPHP+'</h1>'+SFileDoesNotExist, [phpScript]));
Abort;
end;
WebBrowser1.LoadHTML(RunPHPScript(phpScript), phpScript);
Application.ProcessMessages; // This is important, otherwise the metatags can't be read...
sl := TStringList.Create;
try
WebBrowser1.ReadMetaTags(sl);
// TODO: case insensitive
if sl.Values['fastphp_title'] <> '' then Caption := sl.Values['fastphp_title'];
if sl.Values['fastphp_width'] <> '' then ClientWidth := StrToInt(sl.Values['fastphp_width']);
if sl.Values['fastphp_height'] <> '' then ClientHeight := StrToInt(sl.Values['fastphp_height']);
// TODO: Add more attributes, like HTA applications had
// TODO: Additionally implement "HTA:APPLICATION" element, see https://docs.microsoft.com/en-us/previous-versions//ms536495%28v%3dvs.85%29
finally
FreeAndNil(sl);
end;
end;
procedure TForm2.WebBrowser1BeforeNavigate2(ASender: TObject;
const pDisp: IDispatch; const URL, Flags, TargetFrameName, PostData,
Headers: OleVariant; var Cancel: WordBool);
var
myURL, myUrl2, getData: string;
p: integer;
background: boolean;
ArgGet, ArgPost, ArgHeader: string;
resourcestring
SOnlyWorksInEditor = 'This action only works in FastPHP editor.';
begin
background := Pos('background|', URL) >= 1; // do not translate
{$REGION 'Line number references (PHP errors and warnings)'}
if Copy(URL, 1, length(FASTPHP_GOTO_URI_PREFIX)) = FASTPHP_GOTO_URI_PREFIX then
begin
// TODO: maybe we could even open that file in the editor!
ShowMessage(SOnlyWorksInEditor);
Cancel := true;
Exit;
end;
{$ENDREGION}
{$REGION 'Intelligent browser (executes PHP scripts)'}
if URL <> 'about:blank' then
begin
myUrl := URL;
myurl := StringReplace(myurl, 'background|', '', []); // do not translate
p := Pos('?', myUrl);
if p >= 1 then
begin
getData := copy(myURL, p+1, Length(myURL)-p);
myURL := copy(myURL, 1, p-1);
end
else
begin
getData := '';
end;
myURL := StringReplace(myURL, 'http://wa.viathinksoft.de', '', []);
myURL := StringReplace(myURL, 'file:///', '', []);
myURL := StringReplace(myURL, '/', '\', [rfReplaceAll]);
// TODO: real myURL urldecode
myURL := StringReplace(myURL, '+', ' ', []);
myURL := StringReplace(myURL, '%20', ' ', []);
myURL := StringReplace(myURL, '%%', '%', []);
ArgHeader := '';
ArgHeader := MyVarToStr(Headers);
ArgHeader := StringReplace(ArgHeader, #13, '|CR|', [rfReplaceAll]);
ArgHeader := StringReplace(ArgHeader, #10, '|LF|', [rfReplaceAll]);
// *.xphp is ViaThinkSoft's extension associated to FastPHPBrowser
// This allows the "executable PHP scripts" to be executed via double click.--
if FileExists(myURL) and (EndsText('.xphp', myURL) or EndsText('.php', myURL) or EndsText('.php3', myURL) or EndsText('.php4', myURL) or EndsText('.php5', myURL) or EndsText('.phps', myURL)) then
begin
if background then
begin
// TODO: how to detach the process?
ShellExecute(0, 'open', PChar(GetPHPExe), PChar('"'+myURL+'" "'+ArgGet+'" "'+ArgPost+'" "'+ArgHeader+'"'), PChar(ExtractFileDir(Application.ExeName)), SW_HIDE);
end
else
begin
// TODO: somehow prepend fastphp_server.inc.php (populates the $_GET and $_POST arrays)
// TODO: is there a maximal length for the command line?
ArgGet := MyVarToStr(getData);
ArgPost := MyVarToStr(PostData);
myUrl2 := myUrl;
myUrl2 := StringReplace(myUrl2, '\', '/', [rfReplaceAll]);
// TODO: real myURL urlencode
myUrl2 := StringReplace(myUrl2, '%', '%%', []);
//myUrl2 := StringReplace(myUrl2, ' ', '%20', []);
myUrl2 := StringReplace(myUrl2, ' ', '+', []);
myUrl2 := 'http://wa.viathinksoft.de/' + myUrl2;
// showmessage(myUrl2);
WebBrowser1.LoadHTML(GetDosOutput('"'+GetPHPExe+'" -f "'+myURL+'" -- "'+ArgGet+'" "'+ArgPost+'" "'+ArgHeader+'"', ExtractFileDir(Application.ExeName)), myUrl2);
end;
Cancel := true;
end;
end;
{$ENDREGION}
end;
procedure TForm2.WebBrowser1WindowClosing(ASender: TObject;
IsChildWindow: WordBool; var Cancel: WordBool);
begin
Close;
Cancel := true;
end;
end.