#include security.h
*!*	#Define WINDOWHIDE 0x80
*!*	#Define WINDOWSHOW 0x40
*!*	#define TBAR_AUTOHIDE	1
*!*	#define TBAR_KEEPONTOP	2
*!*	#define ABM_GETSTATE 0x00000004
*!*	#define ABM_SETSTATE 0x0000000A

Define Class settingsBaseController As Custom
	cNumeProgram = ''
	cAppName = ''
	cAppPath = ''
	cIniFile = ''
	cVersiuneProgram = ''
	cVersiuneDBTxt = ''
	cROAClientPath = ''
	cROAClientIniFile = ''
	cIniText = ''
	lLog = .F.
	cHostErrors = ''
	cCursorHost = [crshost]
	nTipCriptare = 0
	oOraSettingsController = Null

	*!*	0	=	fara
	*!*	1	=	vechi
	*!*	2	=	nou

	Procedure Init
		Lparameters tcNumeProgram
		Declare Long FindWindow In "user32" String lpClassName, String lpWindowName
		Declare Long SetWindowPos In "user32" Long HWnd, Long hWndInsertAfter, Long x, Long Y, Long cx, Long cy, Long wFlags
		Declare Long SHAppBarMessage In "shell32.dll" Long dwMessage,String @pData

		This.cNumeProgram = tcNumeProgram
		This.cAppPath = Addbs(ShortPath(GetAppStartPath()))
		This.cAppName = Allt(Uppe(Juststem(Sys(16,0))))
		This.cIniFile = This.cAppPath + 'settings.ini'
		This.cROAClientPath = Addbs(Left(This.cAppPath,Rat("\",This.cAppPath,2)-1))
		This.cROAClientIniFile = This.cROAClientPath + 'settings.ini'

		If !File(This.cIniFile)
			This.InitSettings()
		Endif

		This.ReadSettings()
		This.citesteVersiuneProgram()
		This.citesteVersiuneDBTxt()
	Endproc
	*********************************************************************
	Procedure setDBController
		Lparameters toDBController
		This.oOraSettingsController = Createobject('orasettingscontroller',toDBController)
	Endproc
	*********************************************************************
	Procedure verificaVersiuni
		Local lcMesaj,lcMesajVerificare
		lcMesajVerificare = This.oOraSettingsController.verificaVersiuni(This.cNumeProgram,This.cVersiuneProgram,This.cVersiuneDBTxt)
		If !Empty(Nvl(lcMesajVerificare,[]))
			Do Case
				Case This.cNumeProgram$Upper(lcMesajVerificare)
					lcMesaj = "Trebuie actualizat " + This.cNumeProgram + " la versiunea " + Getwordnum(lcMesajVerificare, 2, ":") + Chr(13) + Chr(10) + ;
						"Versiunea actuala este " + Getwordnum(lcMesajVerificare, 3, ":")
				Case "DB"$Upper(lcMesajVerificare)
					lcMesaj = "Trebuie actualizata baza de date la versiunea " + Getwordnum(lcMesajVerificare, 2, ":") + Chr(13) + Chr(10) + ;
						"Versiunea actuala este " + Getwordnum(lcMesajVerificare, 3, ":")
			Endcase
			AMESSAGEBOX(lcMesaj,0+48, "Actualizare")
		Endif
		Release lcMesaj,lcMesajVerificare
	Endproc
	*********************************************************************
	Procedure citesteVersiuneProgram
		Local lcFisierExe
		lcFisierExe = This.cAppPath + This.cAppName + [.exe]
		If Agetfileversion(aVersion, lcFisierExe) > 0
			This.cVersiuneProgram = Alltrim(aVersion(4))
		Else
			This.cVersiuneProgram = []
		Endif
		If Type('aVersion') <> 'U'
			Release aVersion
		Endif
	Endproc
	*********************************************************************
	Procedure citesteVersiuneDBTxt
		Local lcFisierVersiuneDB
		lcFisierVersiuneDB = This.cAppPath + 'versiune_db.txt'
		If File(lcFisierVersiuneDB)
			This.cVersiuneDBTxt = Alltrim(Filetostr(lcFisierVersiuneDB))
		Else
			This.cVersiuneDBTxt = []
		Endif
		Release lcFileVersiuneDB
	Endproc
	*********************************************************************
	Procedure InitSettings
		If Empty(This.cIniText)
			Return
		Endif
		Strtofile(This.cIniText, This.cIniFile)
	Endproc && InitSettings
	*!*		*********************************************************************
	*!*		Function HideTaskBar
	*!*			Local lnHandle,lcInfoBuffer
	*!*			lnHandle = FindWindow("Shell_TrayWnd", "")
	*!*	*!*			SetWindowPos(lnHandle, 0, 0, 0, 0, 0, WINDOWHIDE)
	*!*			DEBUG
	*!*			SUSPEND
	*!*			lcInfoBuffer = This.num2dword(0) + ;
	*!*				This.num2dword(lnHandle) + ;
	*!*				This.num2dword(0) + ;
	*!*				This.num2dword(0) + ;
	*!*				This.num2dword(0) + ;
	*!*				This.num2dword(0)
	*!*			SHAppBarMessage(ABM_GETSTATE,@lcInfoBuffer)
	*!*			lcInfoBuffer = This.num2dword(50000) + ;
	*!*				This.num2dword(lnHandle) + ;
	*!*				This.num2dword(0) + ;
	*!*				This.num2dword(0) + ;
	*!*				This.num2dword(0) + ;
	*!*				This.num2dword(BITNOT(TBAR_KEEPONTOP))
	*!*			SHAppBarMessage(ABM_SETSTATE,@lcInfoBuffer)
	*!*		Endfunc
	*!*		*********************************************************************
	*!*		Function ShowTaskBar
	*!*			Local lnHandle
	*!*			lnHandle = FindWindow("Shell_TrayWnd", "")
	*!*	*!*			SetWindowPos(lnHandle, 0, 0, 0, 0, 0, WINDOWSHOW)
	*!*		Endfunc
	*!*		*********************************************************************
	*!*		FUNCTION num2dword
	*!*		LPARAMETERS tnValue
	*!*		#DEFINE m0       256
	*!*	#DEFINE m1     65536
	*!*	#DEFINE m2  16777216
	*!*	    LOCAL b0, b1, b2, b3
	*!*	    b3 = Int(tnValue/m2)
	*!*	    b2 = Int((tnValue- b3*m2)/m1)
	*!*	    b1 = Int((tnValue- b3*m2 - b2*m1)/m0)
	*!*	    b0 = Mod(tnValue, m0)
	*!*		RETURN Chr(b0)+Chr(b1)+Chr(b2)+Chr(b3)
	*!*		endfunc
	*!*		*********************************************************************
	Procedure ReadSettings
		This.cHostErrors  = This.GetVariable([errors],[host])
		If Upper(Alltrim(This.GetVariable([errors],[log]))) = [TRUE]
			This.lLog = .T.
		Else
			This.lLog = .F.
		Endif

		*!*			This.citesteSecurity()
	Endproc
	*********************************************************************
	Function getHostErrors
		Return This.cHostErrors
	Endfunc
	*********************************************************************
	Function getlLog
		Return This.lLog
	Endfunc
	*********************************************************************
	Function getLogPath
		Return Iif(This.lLog,This.cAppPath + [log_]+ This.cNumeProgram + [_] + Dtos(Date()) + [.txt],[])
	Endfunc
	*********************************************************************
	Function getUpdateServer
		Return This.GetVariable([update],[server])
	Endfunc
	*********************************************************************
	Function getNumeProgram
		Return This.cNumeProgram
	Endfunc
	*********************************************************************
	Function getSchemaSecurity
		Lparameters tcNumeServer
		Local lcAliasVechi,lcSchema
		lcSchema = []
		lcAliasVechi = Alias()
		Select (This.cCursorHost)
		Locate For Upper(Alltrim(Host)) = Upper(Alltrim(tcNumeServer))
		If Found()
			lcSchema = Upper(Alltrim(schema))
		Endif
		If Used(lcAliasVechi)
			Select (lcAliasVechi)
		Endif
		Return lcSchema
	Endfunc
	*********************************************************************
	Function getParolaSecurity
		Lparameters tcNumeServer
		Local lcAliasVechi,lcParola
		lcParola = []
		lcAliasVechi = Alias()
		Select (This.cCursorHost)
		Locate For Upper(Alltrim(Host)) = Upper(Alltrim(tcNumeServer))
		If Found()
			lcParola = Alltrim(pwd)
			lcParola = Iif(Alltrim(isEncrypted) = [1],EncryptDecrypt(lcParola,ENCRYPTKEY,"decrypt",Iif(This.nTipCriptare=2,"blowfish","old")),lcParola)
		Endif
		If Used(lcAliasVechi)
			Select (lcAliasVechi)
		Endif
		Return lcParola
	Endfunc
	*********************************************************************
	Function citesteSecurity
		Lparameters tcFileExt
		Local lcSecurityFile,lcSecurityPath,lnValid,lcFileExt,lcSecurityText,llSucces
		llSucces = .T.
		lcFileExt = Iif(Empty(tcFileExt),[XML],tcFileExt)

		lcSecurityPath = This.cROAClientPath + [SECURITY\]
		lcSecurityFile = lcSecurityPath + [roa_security.] + lcFileExt
		lnValid = 0

		If !File(lcSecurityFile)
			AMESSAGEBOX('Nu exista fisierul ' + lcSecurityFile + ' !',0+16,'Atentie')
			llSucces = .F.
		Else
			If Used(This.cCursorHost)
				Use In (This.cCursorHost)
			Endif

			Create Cursor (This.cCursorHost)(Host c(100), schema c(100), pwd v(100), isEncrypted c(1))
			lcSecurityText = Filetostr(lcSecurityFile)
			If lcFileExt = [XML]
				Try
					Xmltocursor(lcSecurityFile, "cXML", 512)
					Select cXml
					Go Top
					Scatter Name lofirstrecord

					If lofirstrecord.Host="ENCRYPTION"
						This.nTipCriptare = 2
						Delete
					Else
						This.nTipCriptare = 1
					Endif

					Insert Into (This.cCursorHost) (Host, schema, pwd, isEncrypted) ;
						SELECT Host, schema, pwd, "1" As isEncrypted ;
						from cXml
					lnValid = Reccount('cXML')

					Use In cXml
				Catch To loex
					*!*				Local loEx As Exception
					AMESSAGEBOX('Mesaj: ' + loex.Message + CRLF + ;
						'Eroare nr: ' + Alltrim(Transform(loex.ErrorNo)) + CRLF + ;
						'Cod: ' + loex.LineContents + CRLF + 'Procedura: ' + loex.Procedure + CRLF + ;
						'Linia nr: ' + Alltrim(Transform(loex.Lineno)),0+16,'Eroare')
					llSucces = .F.
				Finally
					Use In (Select('cXML'))
				Endtry

			Else
				Local laHost
				Dimension laHost[1]

				lnLen = Alines(laHost, lcSecurityText)

				If lnLen > 0
					For i = 1 To lnLen
						lcLinie = laHost[i]

						lcHost = Getwordnum(lcLinie, 1, ';')
						lcSchema = Getwordnum(lcLinie, 2, ';')
						lcPassword = Getwordnum(lcLinie, 3, ';')
						lcMode = "0" && necriptat - security.txt

						If Empty(lcHost) Or Empty(lcSchema) Or Empty(lcPassword)
							Loop
						Endif

						lnValid = lnValid + 1
						Insert Into (This.cCursorHost) (Host, schema, pwd, isEncrypted) Values (lcHost, lcSchema, lcPassword, Iif(Empty(lcMode), '0', lcMode))
					Endfor
					This.nTipCriptare = 0
				Endif
			Endif
		Endif

		Return llSucces
	Endfunc
	*********************************************************************
	Procedure citesteSetariSchema
		Lparameters tnIdUtil
		This.oOraSettingsController.initializeaza_firma(This.nId,tnIdUtil)
	Endproc
	******************************************************************
	Function getoFirma
		Return This.oOraSettingsController.getoFirma()
	Endfunc
	******************************************************************
	Function getAn
		Return This.oOraSettingsController.getoCalendar().an
	Endfunc
	******************************************************************
	Function getLuna
		Return This.oOraSettingsController.getoCalendar().luna
	Endfunc
	*********************************************************************
	Function getIdSucursala
		Return This.oOraSettingsController.getIdSucursala()
	Endfunc
	*********************************************************************
	Function getIdMama
		Return This.oOraSettingsController.getIdMama()
	Endfunc
	*********************************************************************
	Function getEMama
		Return This.oOraSettingsController.getEMama()
	Endfunc
	*********************************************************************
	Procedure SetVariable
		Lparameters tcSection, tcVarname, tuValue
		Local lcSection, lcVarname, lcValue

		lcSection = Lower(Alltrim(tcSection))
		lcVarname = Lower(Alltrim(tcVarname))
		lcValue = Transform(tuValue)
		SetIni(This.cIniFile, lcSection, lcVarname, lcValue)

	Endproc && SetVariable
	*********************************************************************
	Procedure GetVariable
		Lparameters tcSection, tcVarname

		Local lcSection, lcVarname, lcValue

		lcSection = Lower(Alltrim(tcSection))
		lcVarname = Lower(Alltrim(tcVarname))
		lcValue = GetIni(This.cIniFile, lcSection, lcVarname)
		lcValue = Nvl(lcValue, '')
		If Empty(lcValue) And File(This.cROAClientIniFile)
			lcValue = GetIni(This.cROAClientIniFile, lcSection, lcVarname)
			lcValue = Nvl(lcValue, '')
		Endif

		Return lcValue
	Endproc && GetVariable
	*********************************************************************
	Function getIniFilePath
		Return This.cIniFile
	Endfunc
	*********************************************************************	
	*!*	  	FUNCTION GetValue
	*!*	  		LPARAMETERS tcSetting
	*!*
	*!*			LOCAL lcSetting, luValue
	*!*	  		lcSetting = "THIS." + tcSetting
	*!*	  		luValue = ''
	*!*	  		IF TYPE(lcSetting) # 'U'
	*!*		  		luValue = &lcSetting
	*!*		  	ENDIF
	*!*		  	RETURN luValue
	*!*
	*!*	  	ENDFUNC
	*!*		*********************************************************************
	*!*	  	PROCEDURE SetValue
	*!*	  		LPARAMETERS tcSetting, tuValue
	*!*
	*!*			LOCAL lcSetting, luValue
	*!*	  		lcSetting = "THIS." + tcSetting
	*!*	  		luValue = tuValue
	*!*	  		IF TYPE(lcSetting) # 'U'
	*!*		  		&lcSetting = luValue
	*!*		  	ENDIF
	*!*
	*!*	  	ENDPROC
	*!*	  	*********************************************************************
Enddefine && settingsBaseController
