unit unitLogin;

interface

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

type
  TfrmLogin = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Image1: TImage;
    txtUname: TEdit;
    txtPass: TEdit;
    cmbType: TComboBox;
    btnCancel: TBitBtn;
    btnLogin: TBitBtn;
    procedure btnLoginClick(Sender: TObject);
    procedure btnCancelClick(Sender: TObject);
  private
    { Private declarations }
  public
    USERID : Integer;
           fullname : String;
           useraccess : String;
           procedure login;
           procedure users;
    
  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 DM, unitMain, unitRegistration, unitBuilding, unitUpdateOcc,
  unitCreateSched, unitAddRoom, unitHouseparent, AssignRoom, unitPrevRoom,
  unitNewHp, unitChangeRoom, unitRMFacility;

{$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.login;
var
    endec:TWordTriple;
    username : String;
    userpass :String;
begin
     endec[0]:=1000;
   endec[1]:=9292;
   endec[2]:=3333;

    if DataModule1.tr.InTransaction then
      DataModule1.tr.Commit;

      DataModule1.query.SQL.Clear();
      DataModule1.query.SQL.Add('SELECT *');
      DataModule1.query.SQL.Add('FROM SP_USER_ACCOUNTS WHERE USERNAME = :A AND USERPASS = :B AND USERACCESS = :C');
      DataModule1.query.Params.AsString[0] := trim(txtUname.Text);
      DataModule1.query.Params.AsString[1] := trim(TextEncrypt(txtPass.Text,endec));
      DataModule1.query.Params.AsString[2] := trim(cmbType.Text);

      DataModule1.query.Prepare;
      DataModule1.query.Open();

      USERID := DataModule1.query.Fields.AsInteger[0];
      USERNAME := DataModule1.query.Fields.AsString[2];
      USERPASS := DataModule1.query.Fields.AsString[3];
      USERACCESS := DataModule1.query.Fields.AsString[5];

      if DataModule1.tr.InTransaction then
         DataModule1.tr.Commit;
         Users;

       if (TextEncrypt(txtPass.Text,endec) <> USERPASS) or (txtUname.Text <> USERNAME) or (cmbType.Text <> USERACCESS) or (txtUname.Text = '') or (txtPass.Text = '') or (cmbType.Text = '') then  begin

       MessageDlg('Incorrect username and/or password and/or user type!',mtError,[mbOK],0);
        txtUname.Text:='';
        txtPass.Text:='';
        cmbType.Text:='';

      end
    else
    begin
    frmlogin.Close;
    DORM.StatusBar1.Panels[1].Text:= 'Current User : ' + fullname;
    Registration.StatusBar2.Panels[0].Text := 'Added By : ' + fullname;
    frmNewHP.StatusBar1.Panels[0].Text := 'Added By : ' + fullname;
    frmChangeRoom.StatusBar1.Panels[0].Text := 'Added By : ' + fullname;
    frmHouseparent.StatusBar2.Panels[1].Text := 'Current User : ' + fullname;

   frmRmFac.StatusBar1.Panels[0].Text := 'Added By : ' + fullname;
    //.StatusBar1.Panels[0].Text := 'Added By : ' + fullname;
    //frmRMAss.StatusBar1.Panels[0].Text := 'Added By : ' + fullname;
    //frmBuilding.StatusBar1.Panels[0].Text := 'Current User : ' + fullname;
   // frmPreviousRoom.StatusBar.Panels[0].Text := 'Modified By : ' + fullname;
    //frmUpdate.StatusBar1.Panels[0].Text := 'Modified By : ' + fullname;
    //frmCreateSched.StatusBar1.Panels[1].Text:= 'Current User : ' + fullname;
  //  frmScheduling.StatusBar1.Panels[0].Text:= 'Current User : ' + fullname;
   // frmParentProf.StatusBar1.Panels[0].Text:= 'Current User : ' + fullname;
   // frmUpdateParent.StatusBar2.Panels[0].Text := 'Current User : ' + fullname;

   end
end;

procedure TfrmLogin.users;
begin
       if DataModule1.tr.InTransaction then
          DataModule1.tr.Commit;

          DataModule1.query.SQL.Clear();
          DataModule1.query.SQL.Add('SELECT *');
          DataModule1.query.SQL.Add('FROM SP_USER_ACCOUNTS a WHERE a.USERID = :A');

          DataModule1.query.Params.AsInteger[0] := USERID;
          DataModule1.query.Prepare;
          DataModule1.query.Open();

          fullname:= DataModule1.query.Fields.AsString[1];

          if DataModule1.tr.InTransaction then
             DataModule1 .tr.Commit;
end;

procedure TfrmLogin.btnLoginClick(Sender: TObject);
begin
    login;
    txtUname.Text := '';
    txtPass.Text := '';
    DORM.View1.Enabled := True;
    DORM.StudentLog1.Enabled := True;
    DORM.ManageRooms1.Enabled := True;
    DORM.ManageAccounts1.Enabled := True;
    DORM.Reports1.Enabled := True;
    DORM.Logout1.Enabled := True;
    DORM.Reports1.Enabled := True;
    DORM.Login1.Enabled := False;
    txtUname.Text := '';
    txtPass.Text := '';
end;

{procedure TfrmLogin.cmbTypeKeyPress(Sender: TObject; var Key: Char);
begin
     if key = #13 then begin
        SelectNext(Sender as tWinControl, True, True);
          if Sender = cmbType then
              login;
            key := #0;
end;
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
            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
            key := #0;
end;
end;
 }

procedure TfrmLogin.btnCancelClick(Sender: TObject);
begin
      Close;
      txtUname.Text := '';
      txtPass.Text := '';
end;

end.
