unit LogIn;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, jpeg, ExtCtrls;

type
  TfrmLogIn = class(TForm)
    gbLogIn: TGroupBox;
    lb1Login: TLabel;
    lb2Login: TLabel;
    txtUname: TEdit;
    txtPass: TEdit;
    Image1: TImage;
    btnCancel: TBitBtn;
    btnLogin: TBitBtn;
    procedure logIn();
    procedure dsplayStatus();
    procedure Clear();
    procedure txtUnameKeyPress(Sender: TObject; var Key: Char);
    procedure txtPassKeyPress(Sender: TObject; var Key: Char);
    procedure btnCancelClick(Sender: TObject);
    procedure btnLoginClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);

  private
    { Private declarations }
  public
    { Public declarations }
    username:string;
    password:string;
    fullname:string;
    access:string;
    status:string;
    pk:string;
    useracces: string;
  end;

  type
   TWordTriple = Array[0..2] of Word;
    function FileEncrypt(InFile, OutFile: String; Key: TWordTriple): boolean;
    function FileDecrypt(InFile, OutFile: String; Key: TWordTriple): boolean;
    function TextEncrypt(const s: string; Key: TWordTriple): string;
    function TextDecrypt(const s: string; Key: TWordTriple): string;
    function MemoryEncrypt(Src: Pointer; SrcSize: Cardinal; Target: Pointer; TargetSize: Cardinal; Key:TWordTriple): boolean;
    function MemoryDecrypt(Src: Pointer; SrcSize: Cardinal; Target: Pointer; TargetSize: Cardinal; Key:TWordTriple): boolean;

var
  frmLogIn: TfrmLogIn;

implementation

uses Main, DM, AccountInfo, jvuiblib, AccountInformation, Particulars,
  TaxPayerDetails;

{$R *.dfm}

function MemoryEncrypt(Src: Pointer; SrcSize: Cardinal; Target: Pointer; TargetSize: Cardinal; Key: TWordTriple): boolean;
var
  pIn, pOut: ^byte;
  i : Cardinal;
begin
    if SrcSize = TargetSize then
    begin
        pIn := Src;
        pOut := Target;
        for i := 1 to SrcSize do
        begin
          pOut^ := pIn^ xor (Key[2] shr 8);
          Key[2] := Byte(pIn^ + Key[2]) * Key[0] + Key[1];
          inc(pIn);
          inc(pOut);
        end;
              Result := True;
    end else
              Result := False;
end;

function MemoryDecrypt(Src: Pointer; SrcSize: Cardinal; Target: Pointer; TargetSize: Cardinal; Key: TWordTriple): boolean;
var
  pIn, pOut: ^byte;
  i : Cardinal;
begin
  if SrcSize = TargetSize then
  begin
    pIn := Src;
    pOut := Target;
    for i := 1 to SrcSize do
    begin
      pOut^ := pIn^ xor (Key[2] shr 8);
      Key[2] := byte(pOut^ + Key[2]) * Key[0] + Key[1];
      inc(pIn);
      inc(pOut);
    end;
    Result := True;
  end else
    Result := False;
end;

function TextCrypt(const s: string; Key: TWordTriple; Encrypt: Boolean): string;
var
  bOK: Boolean;
begin
  SetLength(Result, Length(s));
  if Encrypt then
    bOK := MemoryEncrypt(PChar(s), Length(s), PChar(Result), Length(Result), Key)
  else
    bOK := MemoryDecrypt(PChar(s), Length(s), PChar(Result), Length(Result), Key);
  if not bOK then Result := '';
end;

function FileCrypt(InFile, OutFile: String; Key: TWordTriple; Encrypt: Boolean): boolean;
var
  MIn, MOut: TMemoryStream;
begin
  MIn := TMemoryStream.Create;
  MOut := TMemoryStream.Create;
  Try
    MIn.LoadFromFile(InFile);
    MOut.SetSize(MIn.Size);
    if Encrypt then
      Result := MemoryEncrypt(MIn.Memory, MIn.Size, MOut.Memory, MOut.Size, Key)
    else
      Result := MemoryDecrypt(MIn.Memory, MIn.Size, MOut.Memory, MOut.Size, Key);
    MOut.SaveToFile(OutFile);
  finally
    MOut.Free;
    MIn.Free;
  end;
end;
function TextEncrypt(const s: string; Key: TWordTriple): string;
begin
  Result := TextCrypt(s, Key, True);
end;

function TextDecrypt(const s: string; Key: TWordTriple): string;
begin
  Result := TextCrypt(s, Key, False);
end;

function FileEncrypt(InFile, OutFile: String; Key: TWordTriple): boolean;
begin
  Result := FileCrypt(InFile, OutFile, Key, True);
end;

function FileDecrypt(InFile, OutFile: String; Key: TWordTriple): boolean;
begin
  Result := FileCrypt(InFile, OutFile, Key, False);
end;

procedure TfrmLogIn.btnCancelClick(Sender: TObject);
begin
    close;
    Clear();
end;

procedure TfrmLogIn.btnLoginClick(Sender: TObject);
begin
      dsplayStatus();
      logIn();
      frmMain.mnuLogOut.Enabled:=true;
      frmMain.mnuLogIn.Enabled:=False;
      if(access = 'Administrator')then begin
          frmMain.Services.Enabled:=true;
          frmMain.Manage.Enabled:=true;
          frmMain.particulars2.Enabled:=true;
          frmMain.View.Enabled:=true;
          frmTaxPayerDetails.gbTaxPayerDetails.Enabled:=True;
          frmTaxPayerDetails.btnEdit.Visible:=True;
      end
      else begin
          frmMain.Services.Enabled:=true;
          frmMain.Manage.Enabled:=true;
          frmMain.Particulars2.Enabled:=false;
          frmMain.View.Enabled:=true;
          frmTaxPayerDetails.gbTaxPayerDetails.Enabled:=False;
          frmTaxPayerDetails.btnEdit.Visible:= False;
          frmTaxPayerDetails.btnCancel.Visible:=False;
      end
end;

procedure TfrmLogIn.logIn();
var
    endec:TWordTriple;
    useraccess: string;
begin
    endec[0]:=1000;
    endec[1]:=9292;
    endec[2]:=3333;

    if(username <> trim(frmMain.txtUname.Text)) or (password <> Trim(TextEncrypt(frmMain.txtPass.Text, endec))) or (status='Deactivated') then
    begin
        MessageDlg('Invalid Username or Password. Or Account may be Deactivated',mtError,[mbOK],0);
    end
    else begin

    if DModule.TR.InTransaction then
       DModule.TR.Commit;

    DModule.QR.SQL.Clear();
    DModule.QR.SQL.Add('SELECT * ');
    DModule.QR.SQL.Add('FROM usersInfo WHERE Username =:a AND uPAssword=:b');
    DModule.QR.Prepare;
    DModule.QR.open();

   username := DModule.QR.Fields.AsString[5];
   password := DModule.QR.Fields.AsString[6];
   status   := DModule.QR.Fields.AsString[9];

    if DModule.TR.InTransaction then
        DModule.TR.Commit;

    close; //aron pg log in display ang main frm
    dsplayStatus();
    frmMain.StatusBar1.Panels.Items[0].Text := ''+ useraccess;
    frmMain.StatusBar1.Panels.Items[1].Text:= ''+ fullname ;
    Clear;
    end

end;

procedure TfrmLogIn.Clear();
begin
    frmMain.txtUname.Clear;
    frmMain.txtPass.Clear;
end;

procedure TfrmLogIn.dsplayStatus();
  var endec:TWordTriple;
begin
    endec[0]:=1000;
    endec[1]:=9292;
    endec[2]:=3333;

      if DModule.TR.InTransaction then //if naai open nga connection kanang wla na close ang connection
       DModule.TR.Commit;            //e close or e commit niya

    DModule.QR.SQL.Clear();
    DModule.QR.SQL.Add('SELECT * ');
    DModule.QR.SQL.Add('FROM usersInfo WHERE Username =:a AND uPAssword=:b');
      DModule.QR.Params.AsString[0]:= Trim(frmMain.txtUname.Text);
      DModule.QR.Params.AsString[1]:=Trim(TextEncrypt(frmMain.txtPass.Text, endec));
    DModule.QR.Prepare;
    DModule.QR.open();

   username := DModule.QR.Fields.AsString[5];
   password:= DModule.QR.Fields.AsString[6];
   fullname := DModule.QR.Fields.AsString[1];
   access:=DModule.QR.Fields.AsString[8];
   status   := DModule.QR.Fields.AsString[9];
   pk:= DModule.QR.Fields.AsString[0];

    if DModule.TR.InTransaction then
        DModule.TR.Commit;

end;

procedure TfrmLogIn.txtUnameKeyPress(Sender: TObject; var Key: Char);
begin
      if Key = #13 then begin
        SelectNext(Sender as tWinControl, True, True );
      if Sender = txtUname then
        btnLogin.Click; // let the button click itself
        Key := #0;
      end;
end;

procedure TfrmLogIn.txtPassKeyPress(Sender: TObject; var Key: Char);
begin
      if Key = #13 then begin
        SelectNext(Sender as tWinControl, True, True );
      if Sender = txtPass then
        btnLogin.Click; // let the button click itself
        Key := #0;
      end;
end;

procedure TfrmLogIn.FormClose(Sender: TObject; var Action: TCloseAction);
begin
      Clear;
end;

end.
