-
Notifications
You must be signed in to change notification settings - Fork 16
/
php4delphi.pas
1132 lines (994 loc) · 34.1 KB
/
php4delphi.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{*******************************************************}
{ PHP4Delphi }
{ PHP - Delphi interface }
{ }
{ Developers: }
{ Serhiy Perevoznyk }
{ serge_perevoznyk@hotmail.com }
{ }
{ Toby Allen (Documentation) }
{ tobyphp@toflidium.com }
{ }
{ http://users.chello.be/ws36637 }
{*******************************************************}
{$I PHP.INC}
{ $Id: php4delphi.pas,v 6.2 02/2006 delphi32 Exp $ }
// Important:
// Please check PHP version you are using and change php.inc file
// See php.inc for more details
{
You can download the latest version of PHP from
http://www.php.net/downloads.php
You have to download and install PHP separately.
It is not included in the package.
For more information on the PHP Group and the PHP project,
please see <http://www.php.net>.
}
unit php4delphi;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics,
PHPCommon,
ZendTypes, PHPTypes, zendAPI, PHPAPI, DelphiFunctions;
type
TPHPLogMessage = procedure (Sender : TObject; AText : string) of object;
TPHPErrorEvent = procedure (Sender : TObject; AText : string;
AType : TPHPErrorType; AFileName : string; ALineNo : integer) of object;
IPHPLibrary = interface (IUnknown)
['{484AE2CA-755A-437C-9B60-E3735973D0A9}']
procedure AddModule(AModule : Pointer);
procedure RemoveModule(AModule : Pointer);
{$IFDEF PHP510}
procedure HandleRequest(ht : integer; return_value : pzval; return_value_ptr : ppzval; this_ptr : pzval;
return_value_used : integer; TSRMLS_DC : pointer);
{$ELSE}
procedure HandleRequest(ht : integer; return_value : pzval; this_ptr : pzval;
return_value_used : integer; TSRMLS_DC : pointer);
{$ENDIF}
end;
TpsvCustomPHP = class(TPHPComponent, IPHPLibrary)
private
FHeaders : TPHPHeaders;
FSafeMode : boolean;
FSafeModeGid : boolean;
FMaxExecutionTime : integer;
FMaxInputTime : integer;
FRegisterGlobals : boolean;
FINIPath : string;
FExecuteMethod : TPHPExecuteMethod;
FKeepSession : boolean;
FModuleActive : boolean;
FSessionActive : boolean;
FOnModuleStartup : TNotifyEvent;
FOnModuleShutdown : TNotifyEvent;
FOnRequestStartup : TNotifyEvent;
FOnRequestShutdown : TNotifyEvent;
FAfterExecute : TNotifyEvent;
FBeforeExecute : TNotifyEvent;
FAdditionalModules : TList;
FTerminated : boolean;
FConstants : TphpConstants;
TSRMLS_D : pppointer;
FVariables : TPHPVariables;
FBuffer : string;
FOnLogMessage : TPHPLogMessage;
FOnScriptError : TPHPErrorEvent;
FHTMLErrors : boolean;
FHandleErrors : boolean;
FFileName : string;
FVirtualReadHandle : THandle;
FVirtualWriteHandle : THandle;
FUseDelimiters : boolean;
FUseMapping : boolean;
FDLLFolder : string;
FReportDLLError : boolean;
procedure SetVariables(Value : TPHPVariables);
procedure SetConstants(Value : TPHPConstants);
procedure SetHeaders(Value : TPHPHeaders);
function GetVariableCount: integer;
function GetConstantCount: integer;
protected
procedure ClearBuffer;
procedure ClearHeaders;
procedure PrepareModule; virtual;
procedure PrepareIniEntry; virtual;
procedure StartupRequest; virtual;
procedure ShutdownRequest; virtual;
procedure PrepareResult(TSRMLS_D : pointer); virtual;
procedure PrepareVariables(TSRMLS_D : pointer); virtual;
procedure RegisterInternalConstants(TSRMLS_DC : pointer); virtual;
procedure AddModule(AModule : Pointer); virtual;
procedure RemoveModule(AModule : Pointer); virtual;
{$IFDEF PHP510}
procedure HandleRequest(ht : integer; return_value : pzval; return_value_ptr : ppzval; this_ptr : pzval;
return_value_used : integer; TSRMLS_DC : pointer); virtual;
{$ELSE}
procedure HandleRequest(ht : integer; return_value : pzval; this_ptr : pzval;
return_value_used : integer; TSRMLS_DC : pointer); virtual;
{$ENDIF}
function RunTime : boolean;
function GetThreadSafeResourceManager : pointer;
procedure RegisterConstants; virtual;
function CreateVirtualFile(ACode : string) : boolean;
procedure CloseVirtualFile;
procedure StartupPHP; virtual;
public
constructor Create(AOwner : TComponent); override;
destructor Destroy; override;
function Execute : string; overload;
function Execute(AFileName : string) : string; overload;
function RunCode(ACode : string) : string; overload;
function RunCode(ACode : TStrings) : string; overload;
function VariableByName(AName : string) : TPHPVariable;
procedure StartupModule; virtual;
procedure ShutdownModule; virtual;
property ExecuteMethod : TPHPExecuteMethod read FExecuteMethod write FExecuteMethod default emServer;
property FileName : string read FFileName write FFileName;
property Constants : TPHPConstants read FConstants write SetConstants;
property ConstantCount : integer read GetConstantCount;
property Variables : TPHPVariables read FVariables write SetVariables;
property VariableCount : integer read GetVariableCount;
property HTMLErrors : boolean read FHTMLErrors write FHTMLErrors default false;
property KeepSession : boolean read FKeepSession write FKeepSession default false;
property HandleErrors : boolean read FHandleErrors write FHandleErrors default true;
property OnLogMessage : TPHPLogMessage read FOnLogMessage write FOnLogMessage;
property OnScriptError : TPHPErrorEvent read FOnScriptError write FOnScriptError;
property OnModuleStartup : TNotifyEvent read FOnModuleStartup write FOnModuleStartup;
property OnModuleShutdown : TNotifyEvent read FOnModuleShutdown write FOnModuleShutdown;
property OnRequestStartup : TNotifyEvent read FOnRequestStartup write FOnRequestStartup;
property OnRequestShutdown : TNotifyEvent read FOnRequestShutdown write FOnRequestShutdown;
property BeforeExecute : TNotifyEvent read FBeforeExecute write FBeforeExecute;
property AfterExecute : TNotifyEvent read FAfterExecute write FAfterExecute;
property ThreadSafeResourceManager : pointer read GetThreadSafeResourceManager;
property ModuleActive : boolean read FModuleActive;
property SessionActive : boolean read FSessionActive;
property IniPath : string read FIniPath write FIniPath;
property UseDelimiters : boolean read FUseDelimiters write FUseDelimiters default true;
property RegisterGlobals : boolean read FRegisterGlobals write FRegisterGlobals default true;
property MaxExecutionTime : integer read FMaxExecutionTime write FMaxExecutionTime default 0;
property MaxInputTime : integer read FMaxInputTime write FMaxInputTime default 0;
property SafeMode : boolean read FSafeMode write FSafeMode default false;
property SafeModeGid : boolean read FSafeModeGid write FSafeModeGid default false;
property DLLFolder : string read FDLLFolder write FDLLFolder;
property ReportDLLError : boolean read FReportDLLError write FReportDLLError;
property Headers : TPHPHeaders read FHeaders write SetHeaders;
end;
TpsvPHP = class(TpsvCustomPHP)
published
property About;
property FileName;
property Constants;
property Variables;
property HTMLErrors;
property HandleErrors;
property KeepSession;
property OnLogMessage;
property OnScriptError;
property OnModuleStartup;
property OnModuleShutdown;
property OnRequestStartup;
property OnRequestShutdown;
property BeforeExecute;
property AfterExecute;
property IniPath;
property UseDelimiters;
property RegisterGlobals;
property MaxExecutionTime;
property MaxInputTime;
property SafeMode;
property SafeModeGid;
property DLLFolder;
end;
implementation
uses
PHPFunctions,
phpCustomLibrary;
var
delphi_sapi_module : sapi_module_struct;
php_delphi_module : Tzend_module_entry;
module_active : boolean = false;
procedure php_info_delphi(zend_module : Pointer; TSRMLS_DC : pointer); cdecl;
begin
php_info_print_table_start();
php_info_print_table_row(2, PChar('SAPI module version'), PChar('PHP4Delphi 6.2 Feb 2006'));
php_info_print_table_row(2, PChar('Variables support'), PChar('enabled'));
php_info_print_table_row(2, PChar('Constants support'), PChar('enabled'));
php_info_print_table_row(2, PChar('Classes support'), PChar('enabled'));
php_info_print_table_row(2, PChar('Home page'), PChar('http://users.chello.be/ws36637'));
php_info_print_table_end();
end;
function php_delphi_startup(sapi_module : Psapi_module_struct) : integer; cdecl;
begin
result := php_module_startup(sapi_module, nil, 0);
end;
function php_delphi_deactivate(p : pointer) : integer; cdecl;
begin
result := 0;
end;
function php_delphi_ub_write(str : pchar; len : uint; p : pointer) : integer; cdecl;
var
s : string;
php : TpsvPHP;
gl : psapi_globals_struct;
begin
Result := 0;
gl := GetSAPIGlobals(p);
if Assigned(gl) then
begin
php := TpsvPHP(gl^.server_context);
if Assigned(php) then
begin
SetLength(s, len);
Move(str^, s[1], len);
try
php.FBuffer := php.FBuffer + s;
except
end;
result := len;
end;
end;
end;
procedure php_delphi_register_variables(val : pzval; p : pointer); cdecl;
var
php : TpsvPHP;
gl : psapi_globals_struct;
ts : pointer;
cnt : integer;
begin
ts := ts_resource_ex(0, nil);
gl := GetSAPIGlobals(ts);
php := TpsvPHP(gl^.server_context);
php_register_variable('PHP_SELF', '_', nil, p);
php_register_variable('SERVER_NAME','DELPHI', val, p);
php_register_variable('SERVER_SOFTWARE', 'Delphi', val, p);
php_register_variable('IsLibrary', 'False', val, p);
if Assigned(php) then
begin
for cnt := 0 to php.Variables.Count - 1 do
begin
php_register_variable(PChar(php.Variables[cnt].Name),
PChar(php.Variables[cnt].Value), val, p);
end;
end;
end;
function php_delphi_log_message(msg : Pchar) : integer; cdecl;
var
php : TpsvPHP;
gl : psapi_globals_struct;
p : pointer;
begin
p := ts_resource_ex(0, nil);
gl := GetSAPIGlobals(p);
php := TpsvPHP(gl^.server_context);
if Assigned(php) then
begin
if Assigned(php.OnLogMessage) then
php.FOnLogMessage(php, msg)
else
MessageBox(0, MSG, 'PHP4Delphi', MB_OK)
end
else
MessageBox(0, msg, 'PHP4Delphi', MB_OK);
result := 0;
end;
procedure php_delphi_send_header(p1, p2, p3 : pointer); cdecl;
var
php : TpsvPHP;
gl : psapi_globals_struct;
begin
gl := GetSAPIGlobals(p3);
php := TpsvPHP(gl^.server_context);
if Assigned(p1) and Assigned(php) then
begin
with php.Headers.Add do
Header := String(Psapi_header_struct(p1).header);
end;
end;
function php_delphi_header_handler(sapi_header : psapi_header_struct; sapi_headers : psapi_headers_struct; TSRMLS_DC : pointer) : integer; cdecl;
begin
Result := SAPI_HEADER_ADD;
end;
function php_delphi_read_cookies(p1 : pointer) : pointer; cdecl;
begin
result := nil;
end;
procedure delphi_error_cb(_type : integer; const error_filename : PChar;
const error_lineno : uint; const _format : PChar; args : PChar); cdecl;
var
buffer : array[0..1023] of char;
err_msg : PChar;
php : TpsvPHP;
gl : psapi_globals_struct;
p : pointer;
error_type_str : string;
err : TPHPErrorType;
begin
wvsprintf(buffer, _format, args);
err_msg := buffer;
p := ts_resource_ex(0, nil);
gl := GetSAPIGlobals(p);
php := TpsvPHP(gl^.server_context);
case _type of
E_ERROR : err := etError;
E_WARNING : err := etWarning;
E_PARSE : err := etParse;
E_NOTICE : err := etNotice;
E_CORE_ERROR : err := etCoreError;
E_CORE_WARNING : err := etCoreWarning;
E_COMPILE_ERROR : err := etCompileError;
E_COMPILE_WARNING : err := etCompileWarning;
E_USER_ERROR : err := etUserError;
E_USER_WARNING : err := etUserWarning;
E_USER_NOTICE : err := etUserNotice;
else
err := etUnknown;
end;
if assigned(php) then
begin
if Assigned(php.FOnScriptError) then
begin
php.FOnScriptError(php, Err_Msg, err, error_filename, error_lineno);
end
else
begin
case _type of
E_ERROR,
E_CORE_ERROR,
E_COMPILE_ERROR,
E_USER_ERROR:
error_type_str := 'Fatal error';
E_WARNING,
E_CORE_WARNING,
E_COMPILE_WARNING,
E_USER_WARNING :
error_type_str := 'Warning';
E_PARSE:
error_type_str := 'Parse error';
E_NOTICE,
E_USER_NOTICE:
error_type_str := 'Notice';
else
error_type_str := 'Unknown error';
end;
php_log_err(PChar(Format('PHP4DELPHI %s: %s in %s on line %d', [error_type_str, buffer, error_filename, error_lineno])), p);
end;
end;
_zend_bailout(error_filename, error_lineno);
end;
function minit (_type : integer; module_number : integer; TSRMLS_DC : pointer) : integer; cdecl;
begin
RegisterInternalClasses(TSRMLS_DC);
module_active := true;
RESULT := SUCCESS;
end;
function mshutdown (_type : integer; module_number : integer; TSRMLS_DC : pointer) : integer; cdecl;
begin
module_active := false;
RESULT := SUCCESS;
end;
{ TpsvCustomPHP }
constructor TpsvCustomPHP.Create(AOwner: TComponent);
begin
inherited;
FSafeMode := false;
FSafeModeGid := false;
FMaxExecutionTime := 0;
FMaxInputTime := 0;
FRegisterGlobals := true;
FExecuteMethod := emServer;
FModuleActive := false;
FSessionActive := false;
FAdditionalModules := TList.Create;
FVariables := TPHPVariables.Create(Self);
FConstants := TPHPConstants.Create(Self);
FHeaders := TPHPHeaders.Create(Self);
FHandleErrors := true;
FHTMLErrors := false;
FKeepSession := false;
FUseDelimiters := true;
end;
destructor TpsvCustomPHP.Destroy;
begin
ShutdownModule;
FVariables.Free;
FConstants.Free;
FHeaders.Free;
FAdditionalModules.Free;
FModuleActive := false;
FSessionActive := False;
inherited;
end;
procedure TpsvCustomPHP.ClearBuffer;
begin
FBuffer := '';
end;
procedure TpsvCustomPHP.ClearHeaders;
begin
FHeaders.Clear;
end;
procedure TpsvCustomPHP.StartupModule;
var
i : integer;
p : pointer;
begin
if php_delphi_module.module_started = 0 then
begin
StartupPHP;
PrepareModule;
if FModuleActive then
raise EDelphiErrorEx.Create('PHP engine already active');
if not PHPLoaded then //Peter Enz
begin
raise EDelphiErrorEx.Create('PHP engine is not active');
end;
try
//Start PHP thread safe resource manager
tsrm_startup(128, 1, TSRM_ERROR_LEVEL_CORE , nil);
sapi_startup(@delphi_sapi_module);
php_module_startup(@delphi_sapi_module, @php_delphi_module, 1);
TSRMLS_D := ts_resource_ex(0, nil);
PrepareIniEntry;
RegisterConstants;
if FKeepSession then
PG(TSRMLS_D)^.output_buffering := 0;
for i := 0 to FAdditionalModules.Count -1 do
begin
p := FAdditionalModules[i];
TCustomPHPLibrary(p).Refresh;
p := @TCustomPHPLibrary(p).LibraryEntry;
{$IFDEF PHP510}
php_register_extensions(@p, 1, TSRMLS_D);
{$ELSE}
php_startup_extensions(@p, 1);
{$ENDIF}
end;
if Assigned(FOnModuleStartup) then
FOnModuleStartup(Self);
FModuleActive := true;
except
FModuleActive := false;
end;
end
else
begin
FModuleActive := true;
TSRMLS_D := ts_resource_ex(0, nil);
end;
StartupRequest;
end;
function TpsvCustomPHP.Execute : string;
var
file_handle : zend_file_handle;
begin
ClearHeaders;
ClearBuffer;
if Assigned(FBeforeExecute) then
FBeforeExecute(Self);
FTerminated := false;
if not FUseMapping then
begin
if not FileExists(FFileName) then
raise Exception.CreateFmt('File %s does not exists', [FFileName]);
end;
if not FModuleActive then
StartupModule
else
StartupRequest;
if FKeepSession then
PrepareVariables(TSRMLS_D);
FillChar(file_handle, sizeof(zend_file_handle), 0);
if FUseMapping then
begin
file_handle._type := ZEND_HANDLE_FD;
file_handle.opened_path := nil;
file_handle.filename := '-';
file_handle.free_filename := 0;
file_handle.handle.fd := FVirtualReadHandle;
end
else
begin
file_handle._type := ZEND_HANDLE_FILENAME;
file_handle.filename := PChar(FFileName);
file_handle.opened_path := nil;
file_handle.free_filename := 0;
end;
try
php_execute_script(@file_handle, TSRMLS_D);
except
FBuffer := '';
end;
PrepareResult(TSRMLS_D);
if Assigned(FAfterExecute) then
FAfterExecute(Self);
if not FKeepSession then
ShutdownRequest;
Result := FBuffer;
end;
function TpsvCustomPHP.RunCode(ACode : string) : string;
begin
ClearHeaders;
ClearBuffer;
FUseMapping := true;
try
if FUseDelimiters then
begin
if Pos('<?', ACode) = 0 then
ACode := '<? ' + ACode;
if Pos('?>', ACode) = 0 then
ACode := ACode + ' ?>';
end;
if not CreateVirtualFile(ACode) then
begin
Result := '';
Exit;
end;
Result := Execute;
CloseVirtualFile;
finally
FUseMapping := false;
end;
end;
procedure TpsvCustomPHP.PrepareModule;
begin
if php_delphi_module.module_started = 1 then
Exit;
delphi_sapi_module.name := 'embed'; //to solve a problem with dl()
delphi_sapi_module.pretty_name := 'PHP for Delphi'; (* pretty name *)
delphi_sapi_module.startup := php_delphi_startup; (* startup *)
delphi_sapi_module.shutdown := php_module_shutdown_wrapper; (* shutdown *)
delphi_sapi_module.activate:= nil; (* activate *)
delphi_sapi_module.deactivate := @php_delphi_deactivate; (* deactivate *)
delphi_sapi_module.ub_write := @php_delphi_ub_write; (* unbuffered write *)
delphi_sapi_module.flush := nil;
delphi_sapi_module.stat:= nil;
delphi_sapi_module.getenv:= nil;
delphi_sapi_module.sapi_error := @zend_error; (* error handler *)
delphi_sapi_module.header_handler := @php_delphi_header_handler;
delphi_sapi_module.send_headers := nil;
delphi_sapi_module.send_header := @php_delphi_send_header;
delphi_sapi_module.read_post := nil;
delphi_sapi_module.read_cookies := @php_delphi_read_cookies;
delphi_sapi_module.register_server_variables := @php_delphi_register_variables; (* register server variables *)
delphi_sapi_module.log_message := @php_delphi_log_message; (* Log message *)
if FIniPath <> '' then
delphi_sapi_module.php_ini_path_override := PChar(FIniPath)
else
delphi_sapi_module.php_ini_path_override := nil;
delphi_sapi_module.block_interruptions := nil;
delphi_sapi_module.unblock_interruptions := nil;
delphi_sapi_module.default_post_reader := nil;
delphi_sapi_module.treat_data := nil;
delphi_sapi_module.executable_location := nil;
delphi_sapi_module.php_ini_ignore := 0;
InitDelphiFunctions;
php_delphi_module.size := sizeOf(Tzend_module_entry);
php_delphi_module.zend_api := ZEND_MODULE_API_NO;
php_delphi_module.zend_debug := 0;
php_delphi_module.zts := USING_ZTS;
php_delphi_module.name := 'php4delphi_support';
php_delphi_module.functions := @DelphiTable[0];
php_delphi_module.module_startup_func := @minit;
php_delphi_module.module_shutdown_func := @mshutdown;
php_delphi_module.info_func := @php_info_delphi;
php_delphi_module.version := '6.2';
{$IFDEF PHP4}
php_delphi_module.global_startup_func := nil;
{$ENDIF}
php_delphi_module.request_shutdown_func := nil;
php_delphi_module.global_id := 0;
php_delphi_module.module_started := 0;
php_delphi_module._type := 0;
php_delphi_module.handle := nil;
php_delphi_module.module_number := 0;
end;
function TpsvCustomPHP.RunCode(ACode: TStrings): string;
begin
if Assigned(ACode) then
Result := RunCode(ACode.Text);
end;
procedure TpsvCustomPHP.SetConstants(Value: TPHPConstants);
begin
FConstants.Assign(Value);
end;
procedure TpsvCustomPHP.SetVariables(Value: TPHPVariables);
begin
FVariables.Assign(Value);
end;
procedure TpsvCustomPHP.SetHeaders(Value : TPHPHeaders);
begin
FHeaders.Assign(Value);
end;
procedure TpsvCustomPHP.PrepareIniEntry;
var
p : integer;
TimeStr : string;
begin
if not PHPLoaded then
Exit;
if FHandleErrors then
begin
p := integer(GetProcAddress(PHPLib, 'zend_error_cb'));
asm
mov edx, dword ptr [p]
mov dword ptr [edx], offset delphi_error_cb
end;
end;
if FSafeMode then
zend_alter_ini_entry('safe_mode', 10, '1', 1, PHP_INI_SYSTEM, PHP_INI_STAGE_STARTUP)
else
zend_alter_ini_entry('safe_mode', 10, '0', 1, PHP_INI_SYSTEM, PHP_INI_STAGE_STARTUP);
if FSafeModeGID then
zend_alter_ini_entry('safe_mode_gid', 14, '1', 1, PHP_INI_SYSTEM, PHP_INI_STAGE_STARTUP)
else
zend_alter_ini_entry('safe_mode_gid', 14, '0', 1, PHP_INI_SYSTEM, PHP_INI_STAGE_STARTUP);
zend_alter_ini_entry('register_argc_argv', 19, '0', 1, ZEND_INI_SYSTEM, ZEND_INI_STAGE_ACTIVATE);
if FRegisterGlobals then
zend_alter_ini_entry('register_globals', 17, '1', 1, ZEND_INI_SYSTEM, ZEND_INI_STAGE_ACTIVATE)
else
zend_alter_ini_entry('register_globals', 17, '0', 1, ZEND_INI_SYSTEM, ZEND_INI_STAGE_ACTIVATE);
if FHTMLErrors then
zend_alter_ini_entry('html_errors', 12, '1', 1, ZEND_INI_SYSTEM, ZEND_INI_STAGE_ACTIVATE)
else
zend_alter_ini_entry('html_errors', 12, '0', 1, ZEND_INI_SYSTEM, ZEND_INI_STAGE_ACTIVATE);
zend_alter_ini_entry('implicit_flush', 15, '1', 1, ZEND_INI_SYSTEM, ZEND_INI_STAGE_ACTIVATE);
TimeStr := IntToStr(FMaxInputTime);
zend_alter_ini_entry('max_input_time', 15, PChar(TimeStr), Length(TimeStr), ZEND_INI_SYSTEM, ZEND_INI_STAGE_ACTIVATE);
end;
{$IFDEF REGISTER_COLORS}
const
Colors: array[0..41] of TIdentMapEntry = (
(Value: clBlack; Name: 'clBlack'),
(Value: clMaroon; Name: 'clMaroon'),
(Value: clGreen; Name: 'clGreen'),
(Value: clOlive; Name: 'clOlive'),
(Value: clNavy; Name: 'clNavy'),
(Value: clPurple; Name: 'clPurple'),
(Value: clTeal; Name: 'clTeal'),
(Value: clGray; Name: 'clGray'),
(Value: clSilver; Name: 'clSilver'),
(Value: clRed; Name: 'clRed'),
(Value: clLime; Name: 'clLime'),
(Value: clYellow; Name: 'clYellow'),
(Value: clBlue; Name: 'clBlue'),
(Value: clFuchsia; Name: 'clFuchsia'),
(Value: clAqua; Name: 'clAqua'),
(Value: clWhite; Name: 'clWhite'),
(Value: clScrollBar; Name: 'clScrollBar'),
(Value: clBackground; Name: 'clBackground'),
(Value: clActiveCaption; Name: 'clActiveCaption'),
(Value: clInactiveCaption; Name: 'clInactiveCaption'),
(Value: clMenu; Name: 'clMenu'),
(Value: clWindow; Name: 'clWindow'),
(Value: clWindowFrame; Name: 'clWindowFrame'),
(Value: clMenuText; Name: 'clMenuText'),
(Value: clWindowText; Name: 'clWindowText'),
(Value: clCaptionText; Name: 'clCaptionText'),
(Value: clActiveBorder; Name: 'clActiveBorder'),
(Value: clInactiveBorder; Name: 'clInactiveBorder'),
(Value: clAppWorkSpace; Name: 'clAppWorkSpace'),
(Value: clHighlight; Name: 'clHighlight'),
(Value: clHighlightText; Name: 'clHighlightText'),
(Value: clBtnFace; Name: 'clBtnFace'),
(Value: clBtnShadow; Name: 'clBtnShadow'),
(Value: clGrayText; Name: 'clGrayText'),
(Value: clBtnText; Name: 'clBtnText'),
(Value: clInactiveCaptionText; Name: 'clInactiveCaptionText'),
(Value: clBtnHighlight; Name: 'clBtnHighlight'),
(Value: cl3DDkShadow; Name: 'cl3DDkShadow'),
(Value: cl3DLight; Name: 'cl3DLight'),
(Value: clInfoText; Name: 'clInfoText'),
(Value: clInfoBk; Name: 'clInfoBk'),
(Value: clNone; Name: 'clNone'));
{$ENDIF}
procedure TpsvCustomPHP.RegisterInternalConstants(TSRMLS_DC : pointer);
{$IFDEF REGISTER_COLORS}
var
i : integer;
{$ENDIF}
begin
{$IFDEF REGISTER_COLORS}
for I := Low(Colors) to High(Colors) do
zend_register_long_constant( PChar(Colors[i].Name), strlen(PChar(Colors[i].Name)) + 1, Colors[i].Value,
CONST_PERSISTENT or CONST_CS, 0, TSRMLS_DC);
{$ENDIF}
end;
procedure TpsvCustomPHP.AddModule(AModule: Pointer);
begin
FAdditionalModules.Add(AModule);
end;
procedure TpsvCustomPHP.RemoveModule(AModule: Pointer);
begin
try
FAdditionalModules.Remove(AModule);
except
end;
end;
{$IFDEF PHP510}
procedure TpsvCustomPHP.HandleRequest(ht: integer; return_value : pzval; return_value_ptr : ppzval; this_ptr: pzval;
return_value_used: integer; TSRMLS_DC: pointer);
{$ELSE}
procedure TpsvCustomPHP.HandleRequest(ht: integer; return_value, this_ptr: pzval;
return_value_used: integer; TSRMLS_DC: pointer);
{$ENDIF}
var
cnt : integer;
Params : pzval_array;
AFunction : TPHPFunction;
i, j : integer;
FActiveFunctionName : string;
begin
try
if ht > 0 then
begin
if ( not (zend_get_parameters_ex(ht, Params) = SUCCESS )) then
begin
zend_wrong_param_count(TSRMLS_DC);
Exit;
end;
end;
FActiveFunctionName := get_active_function_name(TSRMLS_DC);
for i := 0 to FAdditionalModules.Count - 1 do
begin
for cnt := 0 to TCustomPHPLibrary(FAdditionalModules[i]).Functions.Count - 1 do
begin
if SameText(TCustomPHPLibrary(FAdditionalModules[i]).Functions[cnt].FunctionName, FActiveFunctionName) then
begin
TCustomPHPLibrary(FAdditionalModules[i]).ActiveFunctionName := FActiveFunctionName;
AFunction := TCustomPHPLibrary(FAdditionalModules[i]).Functions[cnt];
if Assigned(AFunction.OnExecute) then
begin
if AFunction.Parameters.Count <> ht then
begin
zend_wrong_param_count(TSRMLS_DC);
Exit;
end;
if ht > 0 then
begin
for j := 0 to ht - 1 do
begin
if not IsParamTypeCorrect(AFunction.Parameters[j].ParamType, Params[j]^) then
begin
zend_error(E_WARNING, PChar(Format('Wrong parameter type for %s()', [get_active_function_name(TSRMLS_DC)])));
Exit;
end;
AFunction.Parameters[j].ZendValue := (Params[j]^);
end;
end; // if ht > 0
AFunction.ZendVar.AsZendVariable := return_value; //direct access to zend variable
AFunction.OnExecute(Self, AFunction.Parameters, AFunction.ReturnValue, this_ptr, TSRMLS_DC);
if AFunction.ZendVar.ISNull then //perform variant conversion
variant2zval(AFunction.ReturnValue, return_value);
end; //if assigned AFunction.OnExecute
Exit;
end; //found function
end; //functions.count
end; //modules.count
finally
dispose_pzval_array(Params);
end;
end;
function TpsvCustomPHP.Execute(AFileName: string): string;
begin
FFileName := AFileName;
Result := Execute;
end;
procedure TpsvCustomPHP.PrepareResult(TSRMLS_D : pointer);
var
ht : PHashTable;
data: ^ppzval;
cnt : integer;
variable : pzval;
begin
if FExecuteMethod = emServer then
{$IFDEF PHP4}
ht := GetSymbolsTable(TSRMLS_D)
{$ELSE}
ht := @GetExecutorGlobals(TSRMLS_D).symbol_table
{$ENDIF}
else
ht := GetTrackHash('_GET', TSRMLS_D);
if Assigned(ht) then
begin
for cnt := 0 to FVariables.Count - 1 do
begin
new(data);
try
if zend_hash_find(ht, PChar(FVariables[cnt].Name),
strlen(PChar(FVariables[cnt].Name)) + 1, data) = SUCCESS then
begin
variable := data^^;
convert_to_string(variable);
FVariables[cnt].Value := variable^.value.str.val;
end;
finally
freemem(data);
end;
end;
end;
end;
function TpsvCustomPHP.VariableByName(AName: string): TPHPVariable;
begin
Result := FVariables.ByName(AName);
end;
procedure TpsvCustomPHP.ShutdownModule;
begin
if module_active then
begin
FModuleActive := false;
FSessionActive := false;
Exit;
end;
if not FModuleActive then
Exit;
if FSessionActive then
ShutdownRequest;
try
delphi_sapi_module.shutdown(@delphi_sapi_module);
sleep(10);
sapi_shutdown;
//Shutdown PHP thread safe resource manager
if Assigned(FOnModuleShutdown) then
FOnModuleShutdown(Self);
tsrm_shutdown();
sleep(10); //?
if PHPLoaded then
UnloadPHP;
finally
FModuleActive := false;
end;
end;
procedure TpsvCustomPHP.ShutdownRequest;
begin
if not FSessionActive then
Exit;
try
if not FTerminated then
begin
php_request_shutdown(nil);
end;
if Assigned(FOnRequestShutdown) then
FOnRequestShutdown(Self);
finally
FSessionActive := false;
end;
end;
procedure TpsvCustomPHP.StartupRequest;
var
gl : psapi_globals_struct;
TimeStr : string;
begin
if not FModuleActive then
raise EDelphiErrorEx.Create('PHP engine is not active ');
if FSessionActive then
Exit;
if FRegisterGlobals then
PG(TSRMLS_D)^.register_globals := true;
try
gl := GetSAPIGlobals(TSRMLS_D);
gl^.server_context := Self;
gl^.request_info.query_string := PChar(Variables.GetVariables);
gl^.sapi_headers.http_response_code := 200;
gl^.request_info.request_method := 'GET';
php_request_startup(TSRMLS_D);
if Assigned(FOnRequestStartup) then
FOnRequestStartup(Self);
TimeStr := IntToStr(FMaxExecutionTime);
zend_alter_ini_entry('max_execution_time', 19, PChar(TimeStr), Length(TimeStr), ZEND_INI_SYSTEM, ZEND_INI_STAGE_RUNTIME);
FSessionActive := true;
except