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