Subversion Repositories fastphp

Rev

Rev 16 | Rev 25 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
8 daniel-mar 1
unit BrowserMain;
2
 
3
interface
4
 
5
uses
6
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
9 daniel-mar 7
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.OleCtrls, SHDocVw_TLB, Vcl.ExtCtrls, StrUtils,
8
  Vcl.StdCtrls, activex, UrlMon;
8 daniel-mar 9
 
10
type
11
  TForm2 = class(TForm)
12
    WebBrowser1: TWebBrowser;
13
    Timer1: TTimer;
14
    procedure Timer1Timer(Sender: TObject);
15
    procedure WebBrowser1BeforeNavigate2(ASender: TObject;
16
      const pDisp: IDispatch; const URL, Flags, TargetFrameName, PostData,
17
      Headers: OleVariant; var Cancel: WordBool);
12 daniel-mar 18
  strict private
19
    function EmbeddedWBQueryService(const rsid, iid: TGUID; out Obj{: IInterface}): HRESULT;
8 daniel-mar 20
  end;
21
 
22
var
23
  Form2: TForm2;
24
 
25
implementation
26
 
27
{$R *.dfm}
28
 
29
uses
9 daniel-mar 30
  WebBrowserUtils, FastPHPUtils, Functions, ShellAPI;
8 daniel-mar 31
 
32
// TODO: Add a lot of nice stuff to let the PHP script communicate with this host application
33
//       For example, allow window resizing etc.  (See Microsoft HTA for inspiration)
34
// TODO: Ajax gives Access Denied error... Create own security manager?
35
// TODO: History doesn't work?
36
// (All these ToDos: Also fix in the Editor)
9 daniel-mar 37
// TODO: kann man eventuell auch php dateien aus einer DLL rausziehen? das wäre TOLL!!!!
38
// TODO: headers... cookies...
11 daniel-mar 39
// 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?)
40
// TODO: let the website decide if the window is maximized etc, as well as it's caption, size and icon
19 daniel-mar 41
// TODO: Pass parameters (argv) to PHP
8 daniel-mar 42
 
12 daniel-mar 43
type
44
  TEmbeddedSecurityManager = class(TInterfacedObject, IInternetSecurityManager)
45
  public
46
    function GetSecuritySite(out ppSite: IInternetSecurityMgrSite): HResult; stdcall;
47
    function MapUrlToZone(pwszUrl: LPCWSTR; out dwZone: DWORD; dwFlags: DWORD): HResult; stdcall;
48
    function GetSecurityId(pwszUrl: LPCWSTR; pbSecurityId: Pointer; var cbSecurityId: DWORD; dwReserved: DWORD): HResult; stdcall;
49
    function ProcessUrlAction(pwszUrl: LPCWSTR; dwAction: DWORD; pPolicy: Pointer; cbPolicy: DWORD; pContext: Pointer; cbContext: DWORD; dwFlags, dwReserved: DWORD): HResult; stdcall;
50
    function QueryCustomPolicy(pwszUrl: LPCWSTR; const guidKey: TGUID; out pPolicy: Pointer; out cbPolicy: DWORD; pContext: Pointer; cbContext: DWORD; dwReserved: DWORD): HResult; stdcall;
51
    function SetZoneMapping(dwZone: DWORD; lpszPattern: PWideChar; dwFlags: DWORD): HResult; stdcall;
52
    function GetZoneMappings(dwZone: DWORD;out ppenumString: IEnumString; dwFlags: DWORD): HResult; stdcall;
53
    function SetSecuritySite(pSite: IInternetSecurityMgrSite): HResult; stdcall;
54
  end;
55
 
56
function TEmbeddedSecurityManager.SetSecuritySite(pSite: IInternetSecurityMgrSite): HResult; stdcall;
57
begin
58
  Result := INET_E_DEFAULT_ACTION;
59
end;
60
function TEmbeddedSecurityManager.GetSecuritySite(
61
  out ppSite: IInternetSecurityMgrSite): HResult; stdcall;
62
begin
63
  Result := INET_E_DEFAULT_ACTION;
64
end;
65
function TEmbeddedSecurityManager.GetSecurityId(pwszUrl: LPCWSTR; pbSecurityId: Pointer;
66
  var cbSecurityId: DWORD; dwReserved: DWORD): HResult; stdcall;
67
begin
68
  Result := INET_E_DEFAULT_ACTION;
69
end;
70
function TEmbeddedSecurityManager.ProcessUrlAction(pwszUrl: LPCWSTR; dwAction: DWORD;
71
  pPolicy: Pointer; cbPolicy: DWORD; pContext: Pointer; cbContext: DWORD;
72
  dwFlags, dwReserved: DWORD): HResult; stdcall;
73
begin
74
  // Result := INET_E_DEFAULT_ACTION;
75
 
76
  // TODO: Doesn't work... Cross-Domain is still not allowed.
77
  PDWORD(pPolicy)^ := URLPOLICY_ALLOW;
78
  Result := S_OK;
79
end;
80
function TEmbeddedSecurityManager.QueryCustomPolicy(pwszUrl: LPCWSTR; const guidKey: TGUID;
81
  out pPolicy: Pointer; out cbPolicy: DWORD; pContext: Pointer; cbContext: DWORD;
82
  dwReserved: DWORD): HResult; stdcall;
83
begin
84
  // Result := INET_E_DEFAULT_ACTION;
85
 
86
  // TODO: Doesn't work... Cross-Domain is still not allowed.
87
  PDWORD(pPolicy)^ := URLPOLICY_ALLOW;
88
  Result := S_OK;
89
end;
90
function TEmbeddedSecurityManager.SetZoneMapping(dwZone: DWORD; lpszPattern: PWideChar;
91
  dwFlags: DWORD): HResult; stdcall;
92
begin
93
  Result := INET_E_DEFAULT_ACTION;
94
end;
95
function TEmbeddedSecurityManager.GetZoneMappings(dwZone: DWORD;out ppenumString: IEnumString;
96
  dwFlags: DWORD): HResult; stdcall;
97
begin
98
  Result := INET_E_DEFAULT_ACTION;
99
end;
100
function TEmbeddedSecurityManager.MapUrlToZone(pwszUrl: LPCWSTR; out dwZone: DWORD; dwFlags: DWORD): HResult;
101
begin
16 daniel-mar 102
  dwZone := URLZONE_TRUSTED;
103
  Result := S_OK;
104
end;
12 daniel-mar 105
 
106
function TForm2.EmbeddedWBQueryService(const rsid, iid: TGUID; out Obj{: IInterface}): HRESULT;
107
var
108
    sam: IInternetSecurityManager;
109
begin
110
    Result := E_NOINTERFACE;
111
 
112
    //rsid ==> Service Identifier
113
    //iid ==> Interface identifier
114
    if IsEqualGUID(rsid, IInternetSecurityManager) and IsEqualGUID(iid, IInternetSecurityManager) then
115
    begin
116
        sam := TEmbeddedSecurityManager.Create;
117
        IInterface(Obj) := sam;
118
        Result := S_OK;
119
    end;
120
end;
121
 
8 daniel-mar 122
procedure TForm2.Timer1Timer(Sender: TObject);
123
var
124
  phpScript: string;
125
begin
126
  Timer1.Enabled := false;
127
  phpScript := ParamStr(1);
128
 
12 daniel-mar 129
  // Remove Security
130
  WebBrowser1.ServiceQuery := EmbeddedWBQueryService;
131
 
8 daniel-mar 132
  WebBrowser1.LoadHTML('<h1>FastPHP</h1>Running script... please wait...');
133
 
134
  // TODO: nice HTML error/intro pages (as resource?)
135
  if phpScript = '' then
136
  begin
137
    WebBrowser1.LoadHTML('<h1>FastPHP</h1>Please enter a PHP file to execute.');
138
    Abort;
139
  end;
140
 
141
  if not FileExists(phpScript) then
142
  begin
143
    WebBrowser1.LoadHTML(Format('<h1>FastPHP</h1>File %s does not exist.', [phpScript]));
144
    Abort;
145
  end;
146
 
147
  WebBrowser1.LoadHTML(RunPHPScript(phpScript), phpScript);
148
end;
149
 
150
procedure TForm2.WebBrowser1BeforeNavigate2(ASender: TObject;
151
  const pDisp: IDispatch; const URL, Flags, TargetFrameName, PostData,
152
  Headers: OleVariant; var Cancel: WordBool);
153
var
16 daniel-mar 154
  myURL, myUrl2, getData: string;
8 daniel-mar 155
  p: integer;
9 daniel-mar 156
  background: boolean;
157
  ArgGet, ArgPost, ArgHeader: string;
8 daniel-mar 158
begin
9 daniel-mar 159
  background := Pos('background|', URL) >= 1;
160
 
8 daniel-mar 161
  {$REGION 'Line number references (PHP errors and warnings)'}
162
  if Copy(URL, 1, length(FASTPHP_GOTO_URI_PREFIX)) = FASTPHP_GOTO_URI_PREFIX then
163
  begin
9 daniel-mar 164
    // TODO: maybe we could even open that file in the editor!
8 daniel-mar 165
    ShowMessage('This action only works in FastPHP editor.');
166
    Cancel := true;
167
    Exit;
168
  end;
169
  {$ENDREGION}
170
 
171
  {$REGION 'Intelligent browser (executes PHP scripts)'}
172
  if URL <> 'about:blank' then
173
  begin
174
    myUrl := URL;
175
 
9 daniel-mar 176
    myurl := StringReplace(myurl, 'background|', '', []);
177
 
8 daniel-mar 178
    p := Pos('?', myUrl);
9 daniel-mar 179
    if p >= 1 then
180
    begin
181
      getData := copy(myURL, p+1, Length(myURL)-p);
182
      myURL := copy(myURL, 1, p-1);
183
    end
184
    else
185
    begin
186
      getData := '';
187
    end;
8 daniel-mar 188
 
16 daniel-mar 189
    myURL := StringReplace(myURL, 'http://wa.viathinksoft.de', '', []);
190
 
9 daniel-mar 191
    myURL := StringReplace(myURL, 'file:///', '', []);
192
    myURL := StringReplace(myURL, '/', '\', [rfReplaceAll]);
8 daniel-mar 193
 
9 daniel-mar 194
    // TODO: real myURL urldecode
195
    myURL := StringReplace(myURL, '+', ' ', []);
196
    myURL := StringReplace(myURL, '%20', ' ', []);
197
    myURL := StringReplace(myURL, '%%', '%', []);
198
 
199
    ArgHeader := '';
200
    ArgHeader := MyVarToStr(Headers);
201
    ArgHeader := StringReplace(ArgHeader, #13, '|CR|', [rfReplaceAll]);
202
    ArgHeader := StringReplace(ArgHeader, #10, '|LF|', [rfReplaceAll]);
203
 
11 daniel-mar 204
    // *.xphp is ViaThinkSoft's extension associated to FastPHPBrowser
205
    // This allows the "executable PHP scripts" to be executed via double click.--
206
    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
8 daniel-mar 207
    begin
9 daniel-mar 208
      if background then
209
      begin
210
        // TODO: how to detach the process?
211
        ShellExecute(0, 'open', PChar(GetPHPExe), PChar('"'+myURL+'" "'+ArgGet+'" "'+ArgPost+'" "'+ArgHeader+'"'), PChar(ExtractFileDir(Application.ExeName)), SW_HIDE);
212
      end
213
      else
214
      begin
215
        // TODO: somehow prepend fastphp_server.inc.php (populates the $_GET and $_POST arrays)
216
        // TODO: is there a maximal length for the command line?
217
        ArgGet := MyVarToStr(getData);
218
        ArgPost := MyVarToStr(PostData);
16 daniel-mar 219
 
220
        myUrl2 := myUrl;
221
        myUrl2 := StringReplace(myUrl2, '\', '/', [rfReplaceAll]);
222
        // TODO: real myURL urlencode
223
        myUrl2 := StringReplace(myUrl2, '%', '%%', []);
224
        //myUrl2 := StringReplace(myUrl2, ' ', '%20', []);
225
        myUrl2 := StringReplace(myUrl2, ' ', '+', []);
226
        myUrl2 := 'http://wa.viathinksoft.de/' + myUrl2;
227
 
228
        showmessage(myUrl2);
229
        WebBrowser1.LoadHTML(GetDosOutput('"'+GetPHPExe+'" "'+myURL+'" "'+ArgGet+'" "'+ArgPost+'" "'+ArgHeader+'"', ExtractFileDir(Application.ExeName)), myUrl2);
9 daniel-mar 230
      end;
8 daniel-mar 231
      Cancel := true;
232
    end;
233
  end;
234
  {$ENDREGION}
235
end;
236
 
237
end.