Subversion Repositories fastphp

Rev

Rev 42 | Rev 61 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

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