| 1 |
unit UserSessionUnit; |
| 2 |
|
| 3 |
{ |
| 4 |
This is a DataModule where you can add components or declare fields that are specific to |
| 5 |
ONE user. Instead of creating global variables, it is better to use this datamodule. You can then |
| 6 |
access the it using UserSession. |
| 7 |
} |
| 8 |
interface |
| 9 |
|
| 10 |
uses |
| 11 |
IWUserSessionBase, SysUtils, Classes, Unit3, graphics, Data.DB, jpeg; |
| 12 |
|
| 13 |
type |
| 14 |
TIWUserSession = class(TIWUserSessionBase) |
| 15 |
procedure IWUserSessionBaseDestroy(Sender: TObject); |
| 16 |
procedure IWUserSessionBaseCreate(Sender: TObject); |
| 17 |
private |
| 18 |
FDM: TDataModule3; |
| 19 |
function GetDM: TDataModule3; |
| 20 |
{ Private declarations } |
| 21 |
public |
| 22 |
{ Public declarations } |
| 23 |
FPage: Boolean; |
| 24 |
user_number: integer; |
| 25 |
FThumbnail: Boolean; |
| 26 |
Serial: integer; |
| 27 |
function hash(const pass: string): string; |
| 28 |
procedure ImgToField(img: TPicture; field: TField); |
| 29 |
procedure FieldToImg(img: TPicture; field: TField); |
| 30 |
property DM: TDataModule3 read GetDM; |
| 31 |
end; |
| 32 |
|
| 33 |
implementation |
| 34 |
|
| 35 |
{$R *.dfm} |
| 36 |
{ TIWUserSession } |
| 37 |
|
| 38 |
procedure TIWUserSession.FieldToImg(img: TPicture; field: TField); |
| 39 |
var |
| 40 |
s: TStream; |
| 41 |
jpg: TJpegImage; |
| 42 |
begin |
| 43 |
if field.IsNull = false then |
| 44 |
begin |
| 45 |
s := field.DataSet.CreateBlobStream(field, bmRead); |
| 46 |
jpg := TJpegImage.Create; |
| 47 |
try |
| 48 |
jpg.LoadFromStream(s); |
| 49 |
img.Assign(jpg); |
| 50 |
finally |
| 51 |
s.Free; |
| 52 |
jpg.Free; |
| 53 |
end; |
| 54 |
end |
| 55 |
else |
| 56 |
img.Assign(nil); |
| 57 |
end; |
| 58 |
|
| 59 |
function TIWUserSession.GetDM: TDataModule3; |
| 60 |
begin |
| 61 |
if Assigned(FDM) = false then |
| 62 |
FDM := TDataModule3.Create(nil); |
| 63 |
result := FDM; |
| 64 |
end; |
| 65 |
|
| 66 |
function TIWUserSession.hash(const pass: string): string; |
| 67 |
var |
| 68 |
i: integer; |
| 69 |
k: integer; |
| 70 |
begin |
| 71 |
result := ''; |
| 72 |
for i := Length(pass) downto 1 do |
| 73 |
begin |
| 74 |
k := Ord(pass[i]) * i; |
| 75 |
result := result + k.ToString; |
| 76 |
end; |
| 77 |
end; |
| 78 |
|
| 79 |
procedure TIWUserSession.ImgToField(img: TPicture; field: TField); |
| 80 |
var |
| 81 |
s: TStream; |
| 82 |
jpg: TJpegImage; |
| 83 |
bmp: TBitmap; |
| 84 |
begin |
| 85 |
s := field.DataSet.CreateBlobStream(field, bmWrite); |
| 86 |
jpg := TJpegImage.Create; |
| 87 |
bmp:=TBitmap.Create; |
| 88 |
try |
| 89 |
bmp.Assign(img.Graphic); |
| 90 |
jpg.Assign(bmp); |
| 91 |
jpg.SaveToStream(s); |
| 92 |
finally |
| 93 |
s.Free; |
| 94 |
bmp.Free; |
| 95 |
jpg.Free; |
| 96 |
end; |
| 97 |
end; |
| 98 |
|
| 99 |
procedure TIWUserSession.IWUserSessionBaseCreate(Sender: TObject); |
| 100 |
var |
| 101 |
s: string; |
| 102 |
begin |
| 103 |
FThumbnail := true; |
| 104 |
s := WebApplication.Request.CookieFields.Values['user_cookie']; |
| 105 |
if (s <> '') and (DM.FDTable1.Locate('NUMBER', s, []) = true) then |
| 106 |
begin |
| 107 |
user_number := s.ToInteger; |
| 108 |
if DM.FDTable1.FieldByName('EMAIL').AsString = '' then |
| 109 |
FPage := true |
| 110 |
else |
| 111 |
FPage := false; |
| 112 |
end |
| 113 |
else |
| 114 |
FPage := true; |
| 115 |
end; |
| 116 |
|
| 117 |
procedure TIWUserSession.IWUserSessionBaseDestroy(Sender: TObject); |
| 118 |
begin |
| 119 |
FreeAndNil(FDM); |
| 120 |
end; |
| 121 |
|
| 122 |
end. |