Subversion Repositories fastphp

Rev

Rev 9 | Rev 12 | 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);
18
  end;
19
 
20
var
21
  Form2: TForm2;
22
 
23
implementation
24
 
25
{$R *.dfm}
26
 
27
uses
9 daniel-mar 28
  WebBrowserUtils, FastPHPUtils, Functions, ShellAPI;
8 daniel-mar 29
 
30
// TODO: Add a lot of nice stuff to let the PHP script communicate with this host application
31
//       For example, allow window resizing etc.  (See Microsoft HTA for inspiration)
32
// TODO: Ajax gives Access Denied error... Create own security manager?
33
// TODO: History doesn't work?
34
// (All these ToDos: Also fix in the Editor)
9 daniel-mar 35
// TODO: kann man eventuell auch php dateien aus einer DLL rausziehen? das wäre TOLL!!!!
36
// TODO: headers... cookies...
11 daniel-mar 37
// 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?)
38
// TODO: let the website decide if the window is maximized etc, as well as it's caption, size and icon
8 daniel-mar 39
 
40
procedure TForm2.Timer1Timer(Sender: TObject);
41
var
42
  phpScript: string;
43
begin
44
  Timer1.Enabled := false;
45
  phpScript := ParamStr(1);
46
 
47
  WebBrowser1.LoadHTML('<h1>FastPHP</h1>Running script... please wait...');
48
 
49
  // TODO: nice HTML error/intro pages (as resource?)
50
  if phpScript = '' then
51
  begin
52
    WebBrowser1.LoadHTML('<h1>FastPHP</h1>Please enter a PHP file to execute.');
53
    Abort;
54
  end;
55
 
56
  if not FileExists(phpScript) then
57
  begin
58
    WebBrowser1.LoadHTML(Format('<h1>FastPHP</h1>File %s does not exist.', [phpScript]));
59
    Abort;
60
  end;
61
 
62
  WebBrowser1.LoadHTML(RunPHPScript(phpScript), phpScript);
63
end;
64
 
65
procedure TForm2.WebBrowser1BeforeNavigate2(ASender: TObject;
66
  const pDisp: IDispatch; const URL, Flags, TargetFrameName, PostData,
67
  Headers: OleVariant; var Cancel: WordBool);
68
var
9 daniel-mar 69
  myURL, getData: string;
8 daniel-mar 70
  p: integer;
9 daniel-mar 71
  background: boolean;
72
  ArgGet, ArgPost, ArgHeader: string;
8 daniel-mar 73
begin
9 daniel-mar 74
  background := Pos('background|', URL) >= 1;
75
 
8 daniel-mar 76
  {$REGION 'Line number references (PHP errors and warnings)'}
77
  if Copy(URL, 1, length(FASTPHP_GOTO_URI_PREFIX)) = FASTPHP_GOTO_URI_PREFIX then
78
  begin
9 daniel-mar 79
    // TODO: maybe we could even open that file in the editor!
8 daniel-mar 80
    ShowMessage('This action only works in FastPHP editor.');
81
    Cancel := true;
82
    Exit;
83
  end;
84
  {$ENDREGION}
85
 
86
  {$REGION 'Intelligent browser (executes PHP scripts)'}
87
  if URL <> 'about:blank' then
88
  begin
89
    myUrl := URL;
90
 
9 daniel-mar 91
    myurl := StringReplace(myurl, 'background|', '', []);
92
 
8 daniel-mar 93
    p := Pos('?', myUrl);
9 daniel-mar 94
    if p >= 1 then
95
    begin
96
      getData := copy(myURL, p+1, Length(myURL)-p);
97
      myURL := copy(myURL, 1, p-1);
98
    end
99
    else
100
    begin
101
      getData := '';
102
    end;
8 daniel-mar 103
 
9 daniel-mar 104
    myURL := StringReplace(myURL, 'file:///', '', []);
105
    myURL := StringReplace(myURL, '/', '\', [rfReplaceAll]);
8 daniel-mar 106
 
9 daniel-mar 107
    // TODO: real myURL urldecode
108
    myURL := StringReplace(myURL, '+', ' ', []);
109
    myURL := StringReplace(myURL, '%20', ' ', []);
110
    myURL := StringReplace(myURL, '%%', '%', []);
111
 
112
    ArgHeader := '';
113
    ArgHeader := MyVarToStr(Headers);
114
    ArgHeader := StringReplace(ArgHeader, #13, '|CR|', [rfReplaceAll]);
115
    ArgHeader := StringReplace(ArgHeader, #10, '|LF|', [rfReplaceAll]);
116
 
11 daniel-mar 117
    // *.xphp is ViaThinkSoft's extension associated to FastPHPBrowser
118
    // This allows the "executable PHP scripts" to be executed via double click.--
119
    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 120
    begin
9 daniel-mar 121
      if background then
122
      begin
123
        // TODO: how to detach the process?
124
        ShellExecute(0, 'open', PChar(GetPHPExe), PChar('"'+myURL+'" "'+ArgGet+'" "'+ArgPost+'" "'+ArgHeader+'"'), PChar(ExtractFileDir(Application.ExeName)), SW_HIDE);
125
      end
126
      else
127
      begin
128
        // TODO: somehow prepend fastphp_server.inc.php (populates the $_GET and $_POST arrays)
129
        // TODO: is there a maximal length for the command line?
130
        ArgGet := MyVarToStr(getData);
131
        ArgPost := MyVarToStr(PostData);
132
        WebBrowser1.LoadHTML(GetDosOutput('"'+GetPHPExe+'" "'+myURL+'" "'+ArgGet+'" "'+ArgPost+'" "'+ArgHeader+'"', ExtractFileDir(Application.ExeName)), myUrl);
133
      end;
8 daniel-mar 134
      Cancel := true;
135
    end;
136
  end;
137
  {$ENDREGION}
138
end;
139
 
140
end.