Subversion Repositories fastphp

Rev

Rev 11 | Rev 16 | 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
8 daniel-mar 41
 
12 daniel-mar 42
type
43
  TEmbeddedSecurityManager = class(TInterfacedObject, IInternetSecurityManager)
44
  public
45
    function GetSecuritySite(out ppSite: IInternetSecurityMgrSite): HResult; stdcall;
46
    function MapUrlToZone(pwszUrl: LPCWSTR; out dwZone: DWORD; dwFlags: DWORD): HResult; stdcall;
47
    function GetSecurityId(pwszUrl: LPCWSTR; pbSecurityId: Pointer; var cbSecurityId: DWORD; dwReserved: DWORD): HResult; stdcall;
48
    function ProcessUrlAction(pwszUrl: LPCWSTR; dwAction: DWORD; pPolicy: Pointer; cbPolicy: DWORD; pContext: Pointer; cbContext: DWORD; dwFlags, dwReserved: DWORD): HResult; stdcall;
49
    function QueryCustomPolicy(pwszUrl: LPCWSTR; const guidKey: TGUID; out pPolicy: Pointer; out cbPolicy: DWORD; pContext: Pointer; cbContext: DWORD; dwReserved: DWORD): HResult; stdcall;
50
    function SetZoneMapping(dwZone: DWORD; lpszPattern: PWideChar; dwFlags: DWORD): HResult; stdcall;
51
    function GetZoneMappings(dwZone: DWORD;out ppenumString: IEnumString; dwFlags: DWORD): HResult; stdcall;
52
    function SetSecuritySite(pSite: IInternetSecurityMgrSite): HResult; stdcall;
53
  end;
54
 
55
function TEmbeddedSecurityManager.SetSecuritySite(pSite: IInternetSecurityMgrSite): HResult; stdcall;
56
begin
57
  Result := INET_E_DEFAULT_ACTION;
58
end;
59
function TEmbeddedSecurityManager.GetSecuritySite(
60
  out ppSite: IInternetSecurityMgrSite): HResult; stdcall;
61
begin
62
  Result := INET_E_DEFAULT_ACTION;
63
end;
64
function TEmbeddedSecurityManager.GetSecurityId(pwszUrl: LPCWSTR; pbSecurityId: Pointer;
65
  var cbSecurityId: DWORD; dwReserved: DWORD): HResult; stdcall;
66
begin
67
  Result := INET_E_DEFAULT_ACTION;
68
end;
69
function TEmbeddedSecurityManager.ProcessUrlAction(pwszUrl: LPCWSTR; dwAction: DWORD;
70
  pPolicy: Pointer; cbPolicy: DWORD; pContext: Pointer; cbContext: DWORD;
71
  dwFlags, dwReserved: DWORD): HResult; stdcall;
72
begin
73
  // Result := INET_E_DEFAULT_ACTION;
74
 
75
  // TODO: Doesn't work... Cross-Domain is still not allowed.
76
  PDWORD(pPolicy)^ := URLPOLICY_ALLOW;
77
  Result := S_OK;
78
end;
79
function TEmbeddedSecurityManager.QueryCustomPolicy(pwszUrl: LPCWSTR; const guidKey: TGUID;
80
  out pPolicy: Pointer; out cbPolicy: DWORD; pContext: Pointer; cbContext: DWORD;
81
  dwReserved: DWORD): HResult; stdcall;
82
begin
83
  // Result := INET_E_DEFAULT_ACTION;
84
 
85
  // TODO: Doesn't work... Cross-Domain is still not allowed.
86
  PDWORD(pPolicy)^ := URLPOLICY_ALLOW;
87
  Result := S_OK;
88
end;
89
function TEmbeddedSecurityManager.SetZoneMapping(dwZone: DWORD; lpszPattern: PWideChar;
90
  dwFlags: DWORD): HResult; stdcall;
91
begin
92
  Result := INET_E_DEFAULT_ACTION;
93
end;
94
function TEmbeddedSecurityManager.GetZoneMappings(dwZone: DWORD;out ppenumString: IEnumString;
95
  dwFlags: DWORD): HResult; stdcall;
96
begin
97
  Result := INET_E_DEFAULT_ACTION;
98
end;
99
function TEmbeddedSecurityManager.MapUrlToZone(pwszUrl: LPCWSTR; out dwZone: DWORD; dwFlags: DWORD): HResult;
100
begin
101
     dwZone := URLZONE_INTERNET;
102
     Result := S_OK;
103
    end;
104
 
105
function TForm2.EmbeddedWBQueryService(const rsid, iid: TGUID; out Obj{: IInterface}): HRESULT;
106
var
107
    sam: IInternetSecurityManager;
108
begin
109
    Result := E_NOINTERFACE;
110
 
111
    //rsid ==> Service Identifier
112
    //iid ==> Interface identifier
113
    if IsEqualGUID(rsid, IInternetSecurityManager) and IsEqualGUID(iid, IInternetSecurityManager) then
114
    begin
115
        sam := TEmbeddedSecurityManager.Create;
116
        IInterface(Obj) := sam;
117
        Result := S_OK;
118
    end;
119
end;
120
 
8 daniel-mar 121
procedure TForm2.Timer1Timer(Sender: TObject);
122
var
123
  phpScript: string;
124
begin
125
  Timer1.Enabled := false;
126
  phpScript := ParamStr(1);
127
 
12 daniel-mar 128
  // Remove Security
129
  WebBrowser1.ServiceQuery := EmbeddedWBQueryService;
130
 
8 daniel-mar 131
  WebBrowser1.LoadHTML('<h1>FastPHP</h1>Running script... please wait...');
132
 
133
  // TODO: nice HTML error/intro pages (as resource?)
134
  if phpScript = '' then
135
  begin
136
    WebBrowser1.LoadHTML('<h1>FastPHP</h1>Please enter a PHP file to execute.');
137
    Abort;
138
  end;
139
 
140
  if not FileExists(phpScript) then
141
  begin
142
    WebBrowser1.LoadHTML(Format('<h1>FastPHP</h1>File %s does not exist.', [phpScript]));
143
    Abort;
144
  end;
145
 
146
  WebBrowser1.LoadHTML(RunPHPScript(phpScript), phpScript);
147
end;
148
 
149
procedure TForm2.WebBrowser1BeforeNavigate2(ASender: TObject;
150
  const pDisp: IDispatch; const URL, Flags, TargetFrameName, PostData,
151
  Headers: OleVariant; var Cancel: WordBool);
152
var
9 daniel-mar 153
  myURL, getData: string;
8 daniel-mar 154
  p: integer;
9 daniel-mar 155
  background: boolean;
156
  ArgGet, ArgPost, ArgHeader: string;
8 daniel-mar 157
begin
9 daniel-mar 158
  background := Pos('background|', URL) >= 1;
159
 
8 daniel-mar 160
  {$REGION 'Line number references (PHP errors and warnings)'}
161
  if Copy(URL, 1, length(FASTPHP_GOTO_URI_PREFIX)) = FASTPHP_GOTO_URI_PREFIX then
162
  begin
9 daniel-mar 163
    // TODO: maybe we could even open that file in the editor!
8 daniel-mar 164
    ShowMessage('This action only works in FastPHP editor.');
165
    Cancel := true;
166
    Exit;
167
  end;
168
  {$ENDREGION}
169
 
170
  {$REGION 'Intelligent browser (executes PHP scripts)'}
171
  if URL <> 'about:blank' then
172
  begin
173
    myUrl := URL;
174
 
9 daniel-mar 175
    myurl := StringReplace(myurl, 'background|', '', []);
176
 
8 daniel-mar 177
    p := Pos('?', myUrl);
9 daniel-mar 178
    if p >= 1 then
179
    begin
180
      getData := copy(myURL, p+1, Length(myURL)-p);
181
      myURL := copy(myURL, 1, p-1);
182
    end
183
    else
184
    begin
185
      getData := '';
186
    end;
8 daniel-mar 187
 
9 daniel-mar 188
    myURL := StringReplace(myURL, 'file:///', '', []);
189
    myURL := StringReplace(myURL, '/', '\', [rfReplaceAll]);
8 daniel-mar 190
 
9 daniel-mar 191
    // TODO: real myURL urldecode
192
    myURL := StringReplace(myURL, '+', ' ', []);
193
    myURL := StringReplace(myURL, '%20', ' ', []);
194
    myURL := StringReplace(myURL, '%%', '%', []);
195
 
196
    ArgHeader := '';
197
    ArgHeader := MyVarToStr(Headers);
198
    ArgHeader := StringReplace(ArgHeader, #13, '|CR|', [rfReplaceAll]);
199
    ArgHeader := StringReplace(ArgHeader, #10, '|LF|', [rfReplaceAll]);
200
 
11 daniel-mar 201
    // *.xphp is ViaThinkSoft's extension associated to FastPHPBrowser
202
    // This allows the "executable PHP scripts" to be executed via double click.--
203
    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 204
    begin
9 daniel-mar 205
      if background then
206
      begin
207
        // TODO: how to detach the process?
208
        ShellExecute(0, 'open', PChar(GetPHPExe), PChar('"'+myURL+'" "'+ArgGet+'" "'+ArgPost+'" "'+ArgHeader+'"'), PChar(ExtractFileDir(Application.ExeName)), SW_HIDE);
209
      end
210
      else
211
      begin
212
        // TODO: somehow prepend fastphp_server.inc.php (populates the $_GET and $_POST arrays)
213
        // TODO: is there a maximal length for the command line?
214
        ArgGet := MyVarToStr(getData);
215
        ArgPost := MyVarToStr(PostData);
216
        WebBrowser1.LoadHTML(GetDosOutput('"'+GetPHPExe+'" "'+myURL+'" "'+ArgGet+'" "'+ArgPost+'" "'+ArgHeader+'"', ExtractFileDir(Application.ExeName)), myUrl);
217
      end;
8 daniel-mar 218
      Cancel := true;
219
    end;
220
  end;
221
  {$ENDREGION}
222
end;
223
 
224
end.