unit DataMod; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, Db, DBTables, inifiles; type TDM = class(TDataModule) dbPPdata: TDatabase; dbQuetzal: TDatabase; dbQuote: TDatabase; dbMQuote: TDatabase; procedure DBSaccLogin(Database: TDatabase; LoginParams: TStrings); procedure DMCreate(Sender: TObject); function EncodeString(const ClientCode: string): string; function DecodeString(const Key: string): string; procedure SetDatabaseSet(const TestSet: boolean); private { Private declarations } function StringCode(const CodedWord: String): String; function EncryptIt(const TheString: string): string; procedure OpenMainServerDB; public { Public declarations } end; var DM: TDM; implementation uses ConstantValues, LoginFrm, Main; {$R *.DFM} function TDM.StringCode(const CodedWord: String) : String; var CodedWordLength : Integer; CodedWordCharNumber : Integer; AsciiValue : Integer; DecodedWord : string; begin CodedWordLength := Length( CodedWord ); DecodedWord := ''; for CodedWordCharNumber := 1 to CodedWordLength do begin // Get the ASCII value of the next character in the passed string. AsciiValue := Ord( CodedWord[ CodedWordCharNumber ] ); // XOR the ASCII code. AsciiValue := (AsciiValue xor (-1)); // Convert the ASCII code to a character, add to the result string. DecodedWord := DecodedWord + Chr( AsciiValue ); end; Result := DecodedWord; end; function TDM.EncodeString(const ClientCode: string): string; var CryptBuf : String; HexBuf : String; HexChar : String; i : Integer; begin CryptBuf := EncryptIt(ClientCode); for i := 1 To Length(CryptBuf) do begin HexChar := IntToHex(Ord(CryptBuf[ i ]),2); if (Length(HexChar) = 1) then begin HexChar := '0' + HexChar; end; HexBuf := HexBuf + HexChar; end; Result := HexBuf; end; // EncodeString. function TDM.DecodeString(const Key: string): string; var Buf : String; i : integer; begin i := 1; while i < Length(Key) do begin Buf := Buf + Chr(StrToInt('$' + Copy(Key, i, 2))); // Hex to Char. Inc(i, 2); // effectively steps by 2. end; Result := EncryptIt(Buf); end; // DecodeString. function TDM.EncryptIt(const TheString: string): string; const SIMPLE_KEY = '©~1m2D3b4C5o6N7s8U9l0T1i2N3g4~®'; var i : integer; Key : string; Jumble : string; begin //Make the key longer than 'TheString' while (Length(Key) < Length(TheString)) do Key := Key + SIMPLE_KEY; for i := 1 To Length(TheString) do begin Jumble := Jumble + Chr(Ord(TheString[ i ]) xor Ord(Key[ i ])); end; EncryptIt := Jumble; end; // EncryptIt. procedure TDM.DBSaccLogin(Database: TDatabase; LoginParams: TStrings); var KeyString : string; begin KeyString := Chr(147)+Chr(141)+Chr(145)+Chr(146)+ Chr(143)+Chr(135); LoginParams.Values['USERNAME'] := 'ibmsys'; KeyString := StringCode(KEYSTRING); LoginParams.Values['PASSWORD'] := KeyString; end; // DBSaccLogin. procedure TDM.OpenMainServerDB; var NewPPIniFile : TIniFile; IniFileName : string; LoginParams : TStringList; sDSN : string; sUserName : string; sPassword : string; begin LoginParams := TStringList.Create; IniFileName := INIFILEDIRECTORY+ChangeFileExt(ExtractFileName(ParamStr(0)),'.ini'); NewPPIniFile := TIniFile.Create(IniFileName); // dbPPdata - SQLPP (most common) database. with NewPPIniFile do begin sDSN := ReadString('DBconnection','PPDSN', ''); sUserName := DecodeString(ReadString('DBconnection','ur', '')); sPassword := DecodeString(ReadString('DBconnection','pd', '')); end; // True in the following = Startup call only. if (Length(sDSN) = 0) or (Length(sUserName) = 0) or (Length(sPassword) = 0) then begin if (Length(sDSN) = 0) then sDSN := dbPPdata.AliasName; GetLoginParams('PreProcessor',True,sDSN,LoginParams); sUserName := LoginParams.Values['USER NAME']; sPassword := LoginParams.Values['PASSWORD']; with NewPPIniFile do begin if (Length(sDSN)>0) then WriteString('DBconnection','PPDSN',sDSN); if (Length(sUserName)>0) then WriteString('DBconnection','ur',EncodeString(sUserName)); if (Length(sPassword)>0) then WriteString('DBconnection','pd',EncodeString(sPassword)); end; // with NewPPIniFile do begin. end; // One or more parameters missing. if (Length(sUserName) > 0) and (Length(sPassword) > 0) then begin LoginParams.Add('USER NAME='+sUserName); LoginParams.Add('PASSWORD='+sPassword); if dbPPdata.Connected then dbPPdata.Connected := False; dbPPdata.AliasName := sDSN; dbPPdata.Params := LoginParams; dbPPdata.LoginPrompt := False; end; // Username and password provided. dbPPdata.Session.NetFileDir := INIFILEDIRECTORY; // dbPPdata.Session.PrivateDir := INIFILEDIRECTORY; // Must be unique per program. dbPPdata.Connected := True; // dbQuetzal - Quetzal database. sDSN := ''; sUserName := ''; sPassword := ''; with NewPPIniFile do begin sDSN := ReadString('DBconnection','QuetzalDSN', ''); sUserName := DecodeString(ReadString('DBconnection','urq', '')); sPassword := DecodeString(ReadString('DBconnection','pdq', '')); end; // True in the following = Startup call only. if (Length(sDSN) = 0) or (Length(sUserName) = 0) or (Length(sPassword) = 0) then begin if (Length(sDSN) = 0) then sDSN := dbQuetzal.AliasName; GetLoginParams('QuetzalSC',True,sDSN,LoginParams); sUserName := LoginParams.Values['USER NAME']; sPassword := LoginParams.Values['PASSWORD']; with NewPPIniFile do begin if (Length(sDSN)>0) then WriteString('DBconnection','QuetzalDSN',sDSN); if (Length(sUserName)>0) then WriteString('DBconnection','urq',EncodeString(sUserName)); if (Length(sPassword)>0) then WriteString('DBconnection','pdq',EncodeString(sPassword)); end; // with NewPPIniFile do begin. end; // One or more parameters missing. if (Length(sUserName) > 0) and (Length(sPassword) > 0) then begin LoginParams.Clear; LoginParams.Add('USER NAME='+sUserName); LoginParams.Add('PASSWORD='+sPassword); if dbQuetzal.Connected then dbQuetzal.Connected := False; dbQuetzal.AliasName := sDSN; dbQuetzal.Params := LoginParams; dbQuetzal.LoginPrompt := False; end; // One or more parameters missing. dbQuetzal.Connected := True; // dbQuote - Quote System database. sDSN := ''; sUserName := ''; sPassword := ''; with NewPPIniFile do begin sDSN := ReadString('DBconnection','QuoteDSN', ''); sUserName := DecodeString(ReadString('DBconnection','urs', '')); sPassword := DecodeString(ReadString('DBconnection','pds', '')); end; // True in the following = Startup call only. if (Length(sDSN) = 0) or (Length(sUserName) = 0) or (Length(sPassword) = 0) then begin if (Length(sDSN) = 0) then sDSN := dbQuote.AliasName; GetLoginParams('Quote System',True,sDSN,LoginParams); sUserName := LoginParams.Values['USER NAME']; sPassword := LoginParams.Values['PASSWORD']; with NewPPIniFile do begin if (Length(sDSN)>0) then WriteString('DBconnection','QuoteDSN',sDSN); if (Length(sUserName)>0) then WriteString('DBconnection','urs',EncodeString(sUserName)); if (Length(sPassword)>0) then WriteString('DBconnection','pds',EncodeString(sPassword)); end; // with NewPPIniFile do begin. end; // One or more parameters missing. if (Length(sUserName) > 0) and (Length(sPassword) > 0) then begin LoginParams.Clear; LoginParams.Add('USER NAME='+sUserName); LoginParams.Add('PASSWORD='+sPassword); if dbQuote.Connected then dbQuote.Connected := False; dbQuote.AliasName := sDSN; dbQuote.Params := LoginParams; dbQuote.LoginPrompt := False; end; // One or more parameters missing. dbQuote.Connected := True; // dbMQuote - Marval Quote System database. sDSN := ''; sUserName := ''; sPassword := ''; with NewPPIniFile do begin sDSN := ReadString('DBconnection','MQuoteDSN', ''); sUserName := DecodeString(ReadString('DBconnection','urs', '')); sPassword := DecodeString(ReadString('DBconnection','pds', '')); end; // True in the following = Startup call only. if (Length(sDSN) = 0) or (Length(sUserName) = 0) or (Length(sPassword) = 0) then begin if (Length(sDSN) = 0) then sDSN := dbMQuote.AliasName; GetLoginParams('Marval Quote System',True,sDSN,LoginParams); sUserName := LoginParams.Values['USER NAME']; sPassword := LoginParams.Values['PASSWORD']; with NewPPIniFile do begin if (Length(sDSN)>0) then WriteString('DBconnection','MQuoteDSN',sDSN); if (Length(sUserName)>0) then WriteString('DBconnection','urs',EncodeString(sUserName)); if (Length(sPassword)>0) then WriteString('DBconnection','pds',EncodeString(sPassword)); end; // with NewPPIniFile do begin. end; // One or more parameters missing. if (Length(sUserName) > 0) and (Length(sPassword) > 0) then begin LoginParams.Clear; LoginParams.Add('USER NAME='+sUserName); LoginParams.Add('PASSWORD='+sPassword); if dbMQuote.Connected then dbMQuote.Connected := False; dbMQuote.AliasName := sDSN; dbMQuote.Params := LoginParams; dbMQuote.LoginPrompt := False; end; // One or more parameters missing. dbMQuote.Connected := True; // Connected - Logged into All databases. LoginParams.Free; NewPPIniFile.UpdateFile; NewPPIniFile.Free; end; // OpenMainServerDB. procedure TDM.SetDatabaseSet(const TestSet: boolean); var NewPPIniFile : TIniFile; IniFileName : string; LoginParams : TStringList; sDSN : string; sUserName : string; sPassword : string; lTest : boolean; sDBConnection : string; begin lTest := TestSet; LoginParams := TStringList.Create; IniFileName := INIFILEDIRECTORY+ChangeFileExt(ExtractFileName(ParamStr(0)),'.ini'); NewPPIniFile := TIniFile.Create(IniFileName); if lTest then sDBConnection := 'TestCfg' else sDBConnection := 'MainCfg'; with NewPPIniFile do begin sDSN := DecodeString(ReadString(sDBConnection,'id1', '')); sUserName := DecodeString(ReadString(sDBConnection,'id2', '')); sPassword := DecodeString(ReadString(sDBConnection,'id3', '')); end; // True in the following = Startup call only. if (Length(sDSN) = 0) or (Length(sUserName) = 0) or (Length(sPassword) = 0) then begin if (Length(sDSN) = 0) then begin sDSN := dbPPdata.AliasName; if lTest then sDSN := sDSN + '_TESTDB'; end; GetLoginParams('PreProcessor',True,sDSN,LoginParams); sUserName := LoginParams.Values['USER NAME']; sPassword := LoginParams.Values['PASSWORD']; end; with NewPPIniFile do begin if (Length(sDSN)>0) then begin WriteString('DBconnection','PPDSN',sDSN); WriteString(sDBConnection,'id1',EncodeString(sDSN)); end; if (Length(sUserName)>0) then begin WriteString('DBconnection','ur',EncodeString(sUserName)); WriteString(sDBConnection,'id2',EncodeString(sUserName)); end; if (Length(sPassword)>0) then begin WriteString('DBconnection','pd',EncodeString(sPassword)); WriteString(sDBConnection,'id3',EncodeString(sPassword)); end; end; // with NewPPIniFile do begin. // dbQuetzal - Quetzal database with NewPPIniFile do begin sDSN := DecodeString(ReadString(sDBConnection,'id4', '')); sUserName := DecodeString(ReadString(sDBConnection,'id5', '')); sPassword := DecodeString(ReadString(sDBConnection,'id6', '')); end; // True in the following = Startup call only. if (Length(sDSN) = 0) or (Length(sUserName) = 0) or (Length(sPassword) = 0) then begin if (Length(sDSN) = 0) then begin sDSN := dbQuetzal.AliasName; if lTest then sDSN := sDSN + '_TESTDB'; end; GetLoginParams('PreProcessor',True,sDSN,LoginParams); sUserName := LoginParams.Values['USER NAME']; sPassword := LoginParams.Values['PASSWORD']; end; with NewPPIniFile do begin if (Length(sDSN)>0) then begin WriteString('DBconnection','QuetzalDSN',sDSN); WriteString(sDBConnection,'id4',EncodeString(sDSN)); end; if (Length(sUserName)>0) then begin WriteString('DBconnection','urq',EncodeString(sUserName)); WriteString(sDBConnection,'id5',EncodeString(sUserName)); end; if (Length(sPassword)>0) then begin WriteString('DBconnection','pdq',EncodeString(sPassword)); WriteString(sDBConnection,'id6',EncodeString(sPassword)); end; end; // with NewPPIniFile do begin. // dbQuote - Quote System database. with NewPPIniFile do begin sDSN := DecodeString(ReadString(sDBConnection,'id7', '')); sUserName := DecodeString(ReadString(sDBConnection,'id8', '')); sPassword := DecodeString(ReadString(sDBConnection,'id9', '')); end; // True in the following = Startup call only. if (Length(sDSN) = 0) or (Length(sUserName) = 0) or (Length(sPassword) = 0) then begin if (Length(sDSN) = 0) then begin sDSN := dbQuote.AliasName; if lTest then sDSN := sDSN + '_TESTDB'; end; GetLoginParams('PreProcessor',True,sDSN,LoginParams); sUserName := LoginParams.Values['USER NAME']; sPassword := LoginParams.Values['PASSWORD']; end; with NewPPIniFile do begin if (Length(sDSN)>0) then begin WriteString('DBconnection','QuoteDSN',sDSN); WriteString(sDBConnection,'id7',EncodeString(sDSN)); end; if (Length(sUserName)>0) then begin WriteString('DBconnection','urs',EncodeString(sUserName)); WriteString(sDBConnection,'id8',EncodeString(sUserName)); end; if (Length(sPassword)>0) then begin WriteString('DBconnection','pds',EncodeString(sPassword)); WriteString(sDBConnection,'id9',EncodeString(sPassword)); end; end; // with NewPPIniFile do begin. OpenMainServerDB; // Done - Passwords are set correctly. LoginParams.Free; NewPPIniFile.UpdateFile; NewPPIniFile.Free; // if not dbPPdata.Connected then MainForm.Close; end; // SetDatabaseSet. procedure TDM.DMCreate(Sender: TObject); begin OpenMainServerDB; end; // DMCreate. end.