Subversion Repositories fastphp

Rev

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

  1. unit FastPHPUtils;
  2.  
  3. interface
  4.  
  5. uses
  6.   SysUtils, StrUtils, Dialogs, IniFiles, Classes, Forms;
  7.  
  8. const
  9.   FASTPHP_GOTO_URI_PREFIX = 'fastphp://editor/gotoline/';
  10.  
  11. function FastPHPConfig: TMemIniFile;
  12. function GetPHPExe: string;
  13. function RunPHPScript(APHPFileName: string): string;
  14. function ParseCHM(const chmFile: TFileName): boolean;
  15. function IsValidPHPExe(const exeFile: TFileName): boolean;
  16.  
  17. implementation
  18.  
  19. uses
  20.   Functions;
  21.  
  22. var
  23.   __FastPHPConfig: TMemIniFile;
  24.  
  25. function FastPHPConfig: TMemIniFile;
  26. begin
  27.   if not Assigned(__FastPHPConfig) then
  28.   begin
  29.     __FastPHPConfig := TMemIniFile.Create(IncludeTrailingPathDelimiter(ExtractFilePath(ParamStr(0))) + 'FastPHP.ini');
  30.   end;
  31.   result := __FastPHPConfig;
  32. end;
  33.  
  34. function GetPHPExe: string;
  35. var
  36.   od: TOpenDialog;
  37. begin
  38.   result := FastPHPConfig.ReadString('Paths', 'PHPInterpreter', '');
  39.   if not FileExists(result) then
  40.   begin
  41.     od := TOpenDialog.Create(nil);
  42.     try
  43.       od.DefaultExt := '.exe';
  44.       od.FileName := 'php.exe';
  45.       od.Filter := 'Executable file (*.exe)|*.exe';
  46.       od.Options := [ofReadOnly, ofHideReadOnly, ofPathMustExist, ofFileMustExist, ofEnableSizing];
  47.       od.Title := 'Please chose your PHP interpreter (php.exe)';
  48.  
  49.       if not od.Execute then exit;
  50.       if not FileExists(od.FileName) then exit;
  51.       result := od.FileName;
  52.     finally
  53.       od.Free;
  54.     end;
  55.  
  56.     if not IsValidPHPExe(result) then
  57.     begin
  58.       ShowMessage('This is not a valid PHP executable.');
  59.       exit;
  60.     end;
  61.  
  62.     FastPHPConfig.WriteString('Paths', 'PHPInterpreter', result);
  63.     FastPHPConfig.UpdateFile;
  64.   end;
  65. end;
  66.  
  67. function RunPHPScript(APHPFileName: string): string;
  68. var
  69.   phpExe: string;
  70. begin
  71.   phpExe := GetPHPExe;
  72.   if phpExe = '' then Abort;
  73.   result := GetDosOutput('"'+phpExe+'" "'+APHPFileName+'"', ExtractFileDir(ParamStr(0)));
  74. end;
  75.  
  76. function ParseCHM(const chmFile: TFileName): boolean;
  77. var
  78.   test, candidate, candidate2: string;
  79.   p, p2, q: integer;
  80.   i: integer;
  81.   good: Boolean;
  82.   ini: TMemIniFile;
  83.   domain: string;
  84.   sl: TStringList;
  85.   symbolCount: Integer;
  86.   sl2: TStrings;
  87.   outFile: string;
  88. begin
  89.   // TODO: problem:  mysqli::commit has /res/mysqli.commit.html -> keyword is NOT commit alone
  90.  
  91.   outFile := ChangeFileExt(chmFile, '.ini');
  92.   DeleteFile(outFile);
  93.   test := LoadFileToStr(chmFile);
  94.   if Pos('/php_manual_', test) = -1 then
  95.   begin
  96.     result := false;
  97.     exit;
  98.   end;
  99.   p := 0;
  100.   ini := TMemIniFile.Create(outFile);
  101.   try
  102.     ini.WriteString('_Info_', 'Source', chmFile);
  103.     ini.WriteString('_Info_', 'Generated', DateTimeToStr(Now));
  104.     ini.WriteString('_Info_', 'GeneratorVer', '1.0');
  105.     ini.WriteString('_Info_', 'Signature', '$ViaThinkSoft$');
  106.     {$REGION 'Excludes'}
  107.     // TODO: more excludes
  108.     ini.WriteBool('_HelpExclude_', 'about', true);
  109.     ini.WriteBool('_HelpExclude_', 'apache', true);
  110.     {$ENDREGION}
  111.     while true do
  112.     begin
  113.       if Assigned(Application) then Application.ProcessMessages;
  114.  
  115.       p := PosEx('/res/', Test, p+1);
  116.       if p = 0 then break;
  117.       p2 := PosEx('.html', Test, p);
  118.       if p = 0 then break;
  119.       candidate := copy(Test, p+5, p2-p-5);
  120.       if candidate = '' then continue;
  121.       if Length(candidate) > 50 then continue;
  122.       good := true;
  123.       for i := p+5 to p2-1 do
  124.       begin
  125.         if ord(test[i]) < 32 then
  126.         begin
  127.           good := false;
  128.           break;
  129.         end;
  130.         if not CharInSet(test[i], ['a'..'z', 'A'..'Z', '.', '-', '_', '0'..'9']) then
  131.         begin
  132.           ini.WriteInteger('_Errors_', 'Contains unexpected character! ' + candidate, ini.ReadInteger('_Errors_', 'Contains unexpected character! ' + candidate, 0)+1);
  133.           good := false;
  134.           break;
  135.         end;
  136.       end;
  137.       if good then
  138.       begin
  139.         candidate2 := LowerCase(StringReplace(candidate, '-', '_', [rfReplaceAll]));
  140.         q := LastPos('.', candidate2);
  141.         domain := copy(candidate2, 1, q-1);
  142.         if domain = '' then continue;
  143.         candidate2 := copy(candidate2, q+1, Length(candidate2)-q);
  144.         ini.WriteInteger('_Category_', domain, ini.ReadInteger('_Category_', domain, 0)+1);
  145.         ini.WriteString(domain, candidate2, '/res/'+candidate+'.html');
  146.         if not ini.ReadBool('_HelpExclude_', domain, false)
  147.            and (candidate2 <> 'configuration')
  148.            and (candidate2 <> 'constants')
  149.            and (candidate2 <> 'installation')
  150.            and (candidate2 <> 'requirements')
  151.            and (candidate2 <> 'resources')
  152.            and (candidate2 <> 'setup') then
  153.         begin
  154.           if ini.ReadString('_HelpWords_', candidate2, '') <> '' then
  155.           begin
  156.             ini.WriteInteger('_Conflicts_', candidate2, ini.ReadInteger('_Conflicts_', candidate2, 0)+1);
  157.           end;
  158.  
  159.           ini.WriteString('_HelpWords_', candidate2, '/res/'+candidate+'.html');
  160.         end;
  161.       end;
  162.     end;
  163.  
  164.     sl := TStringList.Create;
  165.     sl2 := TStringList.Create;
  166.     try
  167.       ini.ReadSections(sl);
  168.       ini.WriteInteger('_Info_', 'TotalDomains', sl.Count);
  169.       symbolCount := 0;
  170.       for domain in sl do
  171.       begin
  172.         ini.ReadSection(domain, sl2);
  173.         Inc(symbolCount, sl2.Count)
  174.       end;
  175.       ini.WriteInteger('_Info_', 'TotalSymbols', symbolCount);
  176.     finally
  177.       sl.Free;
  178.       sl2.Free;
  179.     end;
  180.  
  181.     ini.UpdateFile;
  182.     result := true;
  183.   finally
  184.     ini.Free;
  185.   end;
  186. end;
  187.  
  188. function IsValidPHPExe(const exeFile: TFileName): boolean;
  189. var
  190.   cont: string;
  191. begin
  192.   cont := LoadFileToStr(exeFile);
  193.   result := (Pos('php://stdout', cont) >= 0) or
  194.             (Pos('PHP_SELF', cont) >= 0);
  195. end;
  196.  
  197. initialization
  198. finalization
  199.   if Assigned(__FastPHPConfig) then
  200.   begin
  201.     __FastPHPConfig.UpdateFile;
  202.     FreeAndNil(__FastPHPConfig);
  203.   end;
  204. end.
  205.