source: trunk/Application/WebSession.pas

Last change on this file was 151, checked in by chronos, 9 months ago
File size: 5.3 KB
Line 
1unit WebSession;
2
3interface
4
5uses
6 Classes, SysUtils, HTTPServer, SqlDatabase, HTTPSessionMySQL, User,
7 HtmlClasses, WebPage, Utils, XmlClasses, DateUtils, ModularSystem,
8 PageList, WebApp, Generics;
9
10type
11
12 { TWebSession }
13
14 TWebSession = class(THTTPHandlerData)
15 private
16 procedure GeneratePageDefault(ASession: TWebSession; Page: TWebPage);
17 procedure RegisterModules;
18 procedure CheckModules;
19 public
20 MainModule: TObject;
21 Database: TSqlDatabase;
22 SessionStorage: THTTPSessionStorageMySQL;
23 HtmlDocument: THtmlDocument;
24 GlobalTitle: string;
25 TimeStart: TDateTime;
26 BaseURL: string;
27 ModuleManager: TModuleManager;
28 procedure PageNotFound;
29 procedure InitDatabase;
30 constructor Create; override;
31 destructor Destroy; override;
32 procedure GeneratePage(Page: TWebPage);
33 procedure Run;
34 end;
35
36
37implementation
38
39uses
40 Core, ModuleSystem, ModuleZdechovNET, ModuleUser, PageAdmin, ModuleTV,
41 ModuleFinance, ModuleNetwork, ModulePortal, ModuleIS, ModuleBase,
42 ModuleNews, PageNotFound;
43
44resourcestring
45 SError = '<div>Error: %s</div>';
46
47{ TWebSession }
48
49constructor TWebSession.Create;
50begin
51 inherited;
52 ModuleManager := TModuleManager.Create(nil);
53 Database := TSqlDatabase.Create(nil);
54 SessionStorage := THTTPSessionStorageMySQL.Create(nil);
55 SessionStorage.Database := Database;
56 HtmlDocument := THtmlDocument.Create;
57end;
58
59destructor TWebSession.Destroy;
60begin
61 ModuleManager.StopAll;
62 FreeAndNil(ModuleManager);
63 FreeAndNil(HtmlDocument);
64 FreeAndNil(SessionStorage);
65 FreeAndNil(Database);
66 inherited;
67end;
68
69procedure TWebSession.GeneratePage(Page: TWebPage);
70begin
71 with TModuleBase(ModuleManager.FindModuleByName('Base')) do
72 if Assigned(OnGeneratePage) then OnGeneratePage(Self, Page)
73 else GeneratePageDefault(Self, Page);
74end;
75
76procedure TWebSession.Run;
77var
78 FileName: string;
79 PageName: string;
80 Page: TRegistredPage;
81 BaseUrlParts: TListString;
82 ModuleBase: TModuleBase;
83begin
84 RegisterModules;
85 CheckModules;
86 ModuleManager.StartInstalled;
87
88 try
89 BaseUrlParts := TListString.Create;
90 BaseUrlParts.Explode('/', BaseURL);
91 while (BaseUrlParts.Count > 0) and (Request.Path.Count > 0) and
92 (BaseUrlParts[0] = Request.Path[0]) do begin
93 BaseUrlParts.Delete(0);
94 Request.Path.Delete(0);
95 end;
96 finally
97 BaseUrlParts.Free;
98 end;
99 if Request.Path.Count > 0 then PageName := Request.Path[0]
100 else PageName := '';
101 ModuleBase := TModuleBase(ModuleManager.FindModuleByName('Base'));
102 Page := ModuleBase.Pages.FindByName(PageName);
103 if Assigned(Page) then begin
104 try
105 if Assigned(Page.Page) then Page.Page.OnProduce(Self);
106 except
107 on E: Exception do begin
108 Self.HtmlDocument.Body.SubItems.AddString.Text := Format(SError, [E.Message]);
109 GeneratePage(Page.Page);
110 end;
111 end;
112 end else begin
113 Core.Core.WebApp1.HTTPServer.FileResponse(Self)
114 end;
115end;
116
117procedure TWebSession.GeneratePageDefault(ASession: TWebSession; Page: TWebPage);
118var
119 I: Integer;
120begin
121 with ASession do begin
122 HtmlDocument.ContentLanguage := 'cs';
123 GlobalTitle := 'WebSystem';
124
125 //Page.Page.OnProduce(HandlerData);
126 HtmlDocument.Title := Page.Caption;
127 with HtmlDocument.AsXmlDocument do
128 try
129 Formated := TCore(MainModule).FormatHTML;
130 Response.Content.WriteString(AsString);
131 finally
132 Free;
133 end;
134 end;
135end;
136
137procedure TWebSession.InitDatabase;
138var
139 DbRows: TDbRows;
140begin
141 with Database do begin
142 Encoding := 'utf8';
143 Connect;
144 end;
145end;
146
147procedure TWebSession.RegisterModules;
148var
149 ModuleBase: TModuleBase;
150begin
151 with ModuleManager do begin
152 RegisterModule(TModuleSystem.Create(nil), True);
153 RegisterModule(TModuleBase.Create(nil), True);
154 RegisterModule(TModuleUser.Create(nil), True);
155 RegisterModule(TModuleZdechovNET.Create(nil), True);
156 RegisterModule(TModuleTV.Create(nil), True);
157 RegisterModule(TModuleFinance.Create(nil), True);
158 RegisterModule(TModuleNetwork.Create(nil), True);
159 //RegisterModule(TModulePortal.Create(nil), True);
160 RegisterModule(TModuleIS.Create(nil), True);
161 RegisterModule(TModuleNews.Create(nil), True);
162
163 ModuleBase := TModuleBase(ModuleManager.FindModuleByName('Base'));
164 if Assigned(ModuleBase) then ModuleBase.Session := Self;
165 if Assigned(ModuleBase) then ModuleBase.HandlerData := Self;
166 end;
167end;
168
169procedure TWebSession.CheckModules;
170var
171 DbRows: TDbRows;
172 Module: TModule;
173 I: Integer;
174begin
175 try
176 DbRows := TDbRows.Create;
177 Database.Query(DbRows, 'SELECT * FROM INFORMATION_SCHEMA.TABLES ' +
178 'WHERE TABLE_SCHEMA = "' + Database.Database + '" AND TABLE_NAME = "SystemModule"');
179 Module := ModuleManager.FindModuleByName('System');
180 if DbRows.Count > 0 then Module.SetInstalledState(True)
181 else Module.SetInstalledState(False);
182 finally
183 DbRows.Free;
184 end;
185
186 // Set all modules to already installed state
187 for I := 0 to ModuleManager.Modules.Count - 1 do
188 TModule(ModuleManager.Modules[I]).SetInstalledState(True);
189end;
190
191procedure TWebSession.PageNotFound;
192var
193 Page: TPageNotFound;
194begin
195 Page := TPageNotFound.Create(nil);
196 try
197 if Assigned(Page.OnProduce) then
198 Page.OnProduce(Self);
199 finally
200 Page.Free;
201 end;
202end;
203
204
205end.
206
Note: See TracBrowser for help on using the repository browser.