webmodule.pas 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. unit webmodule;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, Sysutils, HTTPDefs, fpHTTP, fpWeb, iniwebsession;
  6. type
  7. { TFPWebModule1 }
  8. TFPWebModule1 = class(TFPWebModule)
  9. procedure DataModuleCreate(Sender: TObject);
  10. procedure DataModuleNewSession(Sender: TObject);
  11. procedure DataModuleSessionExpired(Sender: TObject);
  12. procedure gotonextpageRequest(Sender: TObject; ARequest: TRequest;
  13. AResponse: TResponse; var Handled: Boolean);
  14. procedure DataModuleAfterResponse(Sender: TObject; AResponse: TResponse);
  15. private
  16. { private declarations }
  17. NewSessionCreated : Boolean;
  18. ASessionExpired : Boolean;
  19. MySessionDir : String;
  20. // procedure GetSessionEvent(Var ASession : TCustomSession);
  21. procedure AutoSessionTemplateReplaceTag(Sender: TObject; const TagString:String;
  22. TagParams: TStringList; Out ReplaceText: String);
  23. public
  24. { public declarations }
  25. end;
  26. var
  27. FPWebModule1: TFPWebModule1;
  28. implementation
  29. {$R *.lfm}
  30. { TFPWebModule1 }
  31. procedure TFPWebModule1.DataModuleCreate(Sender: TObject);
  32. begin
  33. NewSessionCreated := false;
  34. ASessionExpired := false;
  35. ModuleTemplate.AllowTagParams := true;
  36. ModuleTemplate.StartDelimiter := '{+';//The default is { and } which is usually not good if we use Javascript in our templates
  37. ModuleTemplate.EndDelimiter := '+}';
  38. CreateSession := true; //Turn on automatic session handling for this web module
  39. MySessionDir := '';//'/Path/To/A/Directory/';{Use this if you don't want the automatic Temp dir to store the sessionID files under "fpwebsessions" sub-directory}
  40. with (SessionFactory as TIniSessionFactory) do
  41. begin
  42. DefaultTimeoutMinutes := 2; //Session timeout in minutes
  43. SessionDir := MySessionDir;
  44. // SessionCookie:='ACustomCookieName'; {Use this to set the cookie name that will be used for the session management. Default is 'FPWebSession'}
  45. end;
  46. end;
  47. procedure TFPWebModule1.DataModuleNewSession(Sender: TObject);
  48. begin {Sender as TIniWebSession}
  49. NewSessionCreated := true;
  50. end;
  51. procedure TFPWebModule1.DataModuleSessionExpired(Sender: TObject);
  52. begin {Sender as TIniWebSession}
  53. ASessionExpired := true;
  54. end;
  55. procedure TFPWebModule1.gotonextpageRequest(Sender: TObject; ARequest: TRequest;
  56. AResponse: TResponse; var Handled: Boolean);
  57. begin //ModuleTemplate:TFPTemplate is a property of the web module
  58. ModuleTemplate.FileName := 'autosession-template.html';
  59. ModuleTemplate.OnReplaceTag := @AutoSessionTemplateReplaceTag;
  60. AResponse.Content := ModuleTemplate.GetContent;
  61. Handled := true;
  62. end;
  63. procedure TFPWebModule1.AutoSessionTemplateReplaceTag(Sender: TObject; const TagString:
  64. String; TagParams: TStringList; Out ReplaceText: String);
  65. begin
  66. if AnsiCompareText(TagString, 'DATETIME') = 0 then
  67. begin
  68. ReplaceText := FormatDateTime(TagParams.Values['FORMAT'], Now);
  69. end else
  70. if AnsiCompareText(TagString, 'SESSIONID') = 0 then
  71. begin
  72. if Assigned(Session) then
  73. ReplaceText := Session.SessionID;
  74. end else
  75. if AnsiCompareText(TagString, 'TIMEOUTMINUTES') = 0 then
  76. begin
  77. if Assigned(Session) then
  78. ReplaceText := IntToStr(Session.TimeOutMinutes);
  79. end else
  80. if AnsiCompareText(TagString, 'SESSIONFILE') = 0 then
  81. begin
  82. if Assigned(Session) then
  83. if MySessionDir = '' then
  84. ReplaceText := IncludeTrailingPathDelimiter(GetTempDir(True)) + IncludeTrailingPathDelimiter('fpwebsessions') + Session.SessionID
  85. else
  86. ReplaceText := IncludeTrailingPathDelimiter(MySessionDir) + Session.SessionID;
  87. {NOTE: GetTempDir
  88. used by the session manager returns the OS temporary directory if possible, or from the
  89. environment variable TEMP . For CGI programs you need to pass global environment
  90. variables, it is not automatic. For example in the Apache httpd.conf with a
  91. "PassEnv TEMP" or "SetEnv TEMP /pathtotmpdir" line so the web server passes this
  92. global environment variable to the CGI programs' local environment variables.
  93. }
  94. end else
  95. if AnsiCompareText(TagString, 'EXPIREDMESSAGE') = 0 then
  96. begin
  97. if Assigned(Session) and ASessionExpired then
  98. ReplaceText := TagParams.Values['MESSAGE'];
  99. end else
  100. if AnsiCompareText(TagString, 'NEWSESSIONMESSAGE') = 0 then
  101. begin
  102. if Assigned(Session) and NewSessionCreated then
  103. ReplaceText := TagParams.Values['MESSAGE'];
  104. end else
  105. begin
  106. //Not found value for tag -> TagString
  107. ReplaceText := '[Template tag {+' + TagString + '+} is not implemented yet.]';
  108. end;
  109. end;
  110. procedure TFPWebModule1.DataModuleAfterResponse(Sender: TObject;
  111. AResponse: TResponse);
  112. begin
  113. //reset global variables for apache modules and FCGI applications for the next incoming request
  114. NewSessionCreated := false;
  115. ASessionExpired := false;
  116. ModuleTemplate.FileName := '';
  117. //
  118. end;
  119. initialization
  120. RegisterHTTPModule('TFPWebModule1', TFPWebModule1);
  121. end.