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
| ///////////////////////////////////////////////////////////////////////////
// //
// Copyright (c) 2015 by Charta Software B.V. //
// All rights reserved //
// //
// Version: 1.7.0.83525 //
// Web site: https://pascal.chartasoftware.com/ //
// //
// This code and information are provided "as is" without warranty of //
// any kind. Dissemination of this information or reproduction of //
// this material is strictly forbidden unless prior written permission //
// is obtained from Charta Software B.V.. //
// //
///////////////////////////////////////////////////////////////////////////
unit WebSite.Request;
interface
uses
Authorization.Authorizer,
Collection.List,
Collection.Stack.List,
ContentManagement.Article.UriProducer,
Dictionary,
Dom.Element,
Html.Element,
Http.Request,
Http.Response.Html,
Integer._32,
Language.Translator,
Localizer,
Mail,
Parser.Problem.Handler,
Resource,
Sql.Database,
Sql.Database.Connector,
Sql.Transaction,
StructuredDocument.Compiler,
StructuredDocument.Node,
StructuredDocument.Parser,
Table.Model.Factory,
Text,
Uri,
Uri.Producer,
Uuid,
WebSite.InterfaceType,
WebSite.Menu.Item,
WebSite.Module.Interaction,
WebSite.Preferences,
WebSite.Request.Handler,
WebSite.Request.Handler.Factory,
WebSite.Session,
WebSite.User.Account.Connection;
type
TWebSiteRequest = class;
TWebSiteRequestHandlerPath = class;
TWebSiteTemplate = class(TObject)
public
function Apply(Request: TWebSiteRequest; ContentStatus: THtmlContentStatus; Title: TDocumentNode; Content: THtmlElement; InterfaceType: TWebSiteInterfaceType): THtmlElement; virtual; abstract;
function GetWebSiteName(): TText; virtual; abstract;
function GetSlogan(): TText; virtual; abstract;
function GetTitle(Request: TWebSiteRequest): TText; virtual; abstract;
function GetDomain(): TText; virtual; abstract;
function GetLogo(): TResource; virtual; abstract;
function GetPasswordEmailTemplate(): TText; virtual; abstract;
function GetNewUserEmailTemplate(): TText; virtual; abstract;
end;
TStructuredDocumentToHtmlCompilerFactory = class(TObject)
public
function CreateParser(ProblemHandler: TParseProblemHandler): TStructuredDocumentParser; virtual; abstract;
function CreateHtmlCompiler(Request: TWebSiteRequest; Contents: THtmlElementList): TDocumentCompiler; virtual; abstract;
procedure CompileToHtml(Request: TWebSiteRequest; Contents: THtmlElementList; Value: TText);
end;
TAlternativeWebSiteOptionsFactory = class(TObject)
public
procedure AddOptions(Request: TWebSiteRequest; Target: TMenuItem); virtual; abstract;
function CreateConnection(Request: TWebSiteRequest): TAccountConnection; virtual; abstract;
end;
TWebSiteRequest = class(TObject)
public
HttpRequest: THttpRequest;
Session: TWebSiteSession;
Template: TWebSiteTemplate;
Path: TUriPath;
HandlerPath: TWebSiteRequestHandlerPath;
GetParameters: TDictionary;
PostParameters: TDictionary;
Preferences: TWebSitePreferences;
SqlDatabase: TSqlDatabase;
SqlDatabaseConnector: TSqlDatabaseConnector;
MaintenanceDatabaseConnector: TSqlDatabaseConnector;
Transaction: TSqlTransaction;
Translator: TTranslator;
Localizer: TLocalizer;
UriProducer: TUriProducer;
ArticleUriProducer: TAbstractArticleUriProducer;
Authorizer: TAuthorizer;
Domain: TText;
FilterValues: TList<TText>; // TODO: We really should provide a better data structure to keep track of filter values
StructuredDocument: TStructuredDocumentToHtmlCompilerFactory;
AlternativeWebSiteOptionFactory: TAlternativeWebSiteOptionsFactory;
TableModelFactory: TTableModelFactory;
HandlerFactory: TWebSiteRequestHandlerFactory;
Interaction: TWebSiteModuleInteraction;
constructor Create(); virtual;
destructor Destroy(); override;
function Resource(): TUri;
function AcquireModuleStorage(): TDomElement;
procedure ReleaseModuleStorage();
function SendMail(Mail: TMail): TUuid; // TODO: We should probably introduce something like an abstract class TMailSender instead
end;
TWebSiteRequestHandlerPath = class(TListStack<TWebSiteRequestHandler>)
protected
function GetTop(): TWebSiteRequestHandler; override;
function GetElements(Index: TInteger32): TWebSiteRequestHandler; override;
public
function Pop(): TWebSiteRequestHandler; override;
end deprecated 'An ordinary stack should be sufficient';
threadvar
ActiveWebSiteRequest: TWebSiteRequest;
implementation
uses
Configuration,
Email.Table.Set_,
Html.Document,
Internet.MediaType.Text,
Mail.Address,
Mail.Entity.Html,
Mail.Protocol.Smtp.Client,
Mail.Writer.Database,
Object_.Reference.Counter,
Sql,
Sql.Result,
StructuredDocument.Compiler.Html.Factory.Default,
Text.Document.Problem,
Text.Document.Problem.Handler,
Text.List,
WebSite.Request.Handler.Factory.Default;
{ TStructuredDocumentToHtmlCompilerFactory }
procedure TStructuredDocumentToHtmlCompilerFactory.CompileToHtml(Request: TWebSiteRequest; Contents: THtmlElementList; Value: TText);
var
Parser: TStructuredDocumentParser;
Nodes: TList<TDocumentNode>;
Compiler: TDocumentCompiler;
Problems: TSortedTextDocumentProblemList;
ProblemHandler: TTextDocumentProblemHandler;
begin
Problems := TSortedTextDocumentProblemList.Create();
ProblemHandler := TTextDocumentProblemHandler.Create(nil, Problems); // TODO: We should provide a valid document here
Parser := Request.StructuredDocument.CreateParser(ProblemHandler);
Nodes := Parser.ParseText(Value);
Compiler := Request.StructuredDocument.CreateHtmlCompiler(Request, Contents);
Compiler.CompileNodes(Nodes);
Compiler.RenderProblems(Problems);
Compiler.Free();
Nodes.Free();
Parser.Free();
ProblemHandler.Free();
Problems.Free();
end;
{ TWebSiteRequest }
constructor TWebSiteRequest.Create();
begin
inherited Create();
HandlerPath := TWebSiteRequestHandlerPath.Create();
HandlerPath.ElementManager := TReferenceCounter<TWebSiteRequestHandler>.Create();
Translator := TTranslator.Create();
Preferences := TWebSitePreferences.Create();
UriProducer := TUriProducer.Create();
FilterValues := TTextList.Create();
StructuredDocument := TDefaultStructuredDocumentToHtmlCompilerFactory.Create();
HandlerFactory := TDefaultWebSiteRequestHandlerFactory.Create(Self);
Localizer := TLocalizer.Create();
Localizer.Text := Translator;
end;
destructor TWebSiteRequest.Destroy();
begin
Preferences.Free();
Translator.Free();
Localizer.Free();
HandlerPath.Free();
UriProducer.Free();
FilterValues.Free();
StructuredDocument.Free();
GetParameters.Free();
PostParameters.Free();
AlternativeWebSiteOptionFactory.Free();
HandlerFactory.Free();
inherited Destroy();
end;
function TWebSiteRequest.AcquireModuleStorage(): TDomElement;
var
Uri: TUri;
Key: TText;
begin
// TODO: We currently lock the entire session here to prevent the module storage to become corrupt, however, we still can
// encounter "the lost update" problem because module storage is locked only briefly now. Probably we should revisit
// this approach once we persist the session and its module storage to the database.
Session.Lock.Acquire();
Uri := UriProducer.RelativeUri([], TUriResourceType.Directory);
Key := Uri.ToText();
Result := Session.ModuleStorage[Key];
if Result = nil then
begin
Result := TDomElement.Create('ModuleStorage');
Result.Attributes.Add('Uri', Key);
Session.ModuleStorage[Key] := Result;
end;
end;
procedure TWebSiteRequest.ReleaseModuleStorage();
begin
Session.Lock.Release();
end;
function TWebSiteRequest.Resource(): TUri;
begin
Result := UriProducer.RelativeUri(Path);
end;
function TWebSiteRequest.SendMail(Mail: TMail): TUuid;
var
HtmlMailEntity: THtmlMailEntity;
Document: THtmlDocument;
Style: THtmlStyle;
SmtpClient: TSmtpClient;
Configuration: TDomElement;
SmtpElement: TDomElement;
MailWriter: TMailDatabaseWriter;
SqlResult: TSqlResult;
SmtpUserId: TInteger32;
Name: TText;
EmailAddress: TText;
begin
// TODO: This is an awful function and we should provide for a much better abstraction
if Mail.Entity is THtmlMailEntity then
begin
HtmlMailEntity := Mail.Entity as THtmlMailEntity;
Document := THtmlDocument.Create();
Document.DocumentType := THtmlDocumentType.Create('', TUri.New());
Style := THtmlStyle.Create();
Style.ContentType := TMediaTypeTextCss.Create();
Style.Contents.Add('html, body {background-color: #FFFFFF;}');
Style.Contents.Add('table {text-align: left;}');
Document.Html.Head.Contents.Add(Style);
Document.Html.Body.Contents.Add(HtmlMailEntity.Element);
HtmlMailEntity.Element := Document;
end;
SqlResult :=
SqlDatabase.Query(
Select(
[
EmailTableSet.SmtpUsersTable.Name,
EmailTableSet.SmtpUsersTable.EmailAddress
],
[ EmailTableSet.SmtpUsersTable ],
[ EmailTableSet.SmtpUsersTable.ID = 1 ]
)
);
if SqlResult.FetchRow() then
begin
Name := SqlResult.ReadText();
EmailAddress := SqlResult.ReadText();
end
else
begin
Configuration := LoadDefaultConfiguration();
SmtpElement := Configuration.ElementWithName('Smtp');
Name := SmtpElement.Attributes.Get('Name').AsText();
EmailAddress := SmtpElement.Attributes.Get('Address').AsText();
Configuration.Free();
end;
Mail.From.Add(TMailAddress.Create(Name, EmailAddress));
SqlResult.Free();
// The implementation does not consider multiple users can have the same SMTP server assigned.
// Instead, an arbitrary user with server id 1 is taken.
SqlResult := SqlDatabase.Query(
Select(
[ EmailTableSet.SmtpUsersTable.Id ],
[
EmailTableSet.SmtpServersTable,
EmailTableSet.SmtpUsersTable
],
[
EmailTableSet.SmtpServersTable.ID = 1,
EmailTableSet.SmtpServersTable.ID = EmailTableSet.SmtpUsersTable.Server
]
)
);
if SqlResult.FetchRow() then
begin
SmtpUserId := SqlResult.ReadInteger();
SmtpClient := TSmtpClient.LoadFromDatabase(SqlDatabase, SmtpUserId);
end
else
SmtpClient := nil;
SqlResult.Free();
if SmtpClient = nil then
begin
Configuration := LoadDefaultConfiguration();
SmtpClient := TSmtpClient.LoadFromElement(Configuration.ElementWithName('Smtp'));
Configuration.Free();
end;
SmtpClient.Send(Mail);
SmtpClient.Quit();
SmtpClient.Free();
MailWriter := TMailDatabaseWriter.Create();
Result := MailWriter.Write(SqlDatabase, Mail);
MailWriter.Free();
end;
{ TWebSiteRequestHandlerPath }
function TWebSiteRequestHandlerPath.GetElements(Index: TInteger32): TWebSiteRequestHandler;
begin
// TODO: This inverses the implementation of the base class, however, this should not be necessary
Result := inherited GetElements(Count - 1 - Index);
end;
function TWebSiteRequestHandlerPath.GetTop(): TWebSiteRequestHandler;
begin
// TODO: This implementation circumvents exceptions caused by an empty path, this should not happen and this implementation
// should be removed
if Count > 0 then
Result := inherited GetTop()
else
Result := nil;
end;
function TWebSiteRequestHandlerPath.Pop(): TWebSiteRequestHandler;
begin
// TODO: This implementation circumvents exceptions caused by popping too many items, this should not happen and this implementation
// should be removed
if Count > 0 then
Result := inherited Pop()
else
Result := nil;
end;
end.
|