PROCEDURE dbc_OpenData(cDatabaseName, lExclusive, lNoupdate, lValidate)
*Immediately after DBC is opened.

	RELEASE glValidUser
	PUBLIC glValidUser
	PRIVATE lcDbcPath
	lcDbcPath = SUBSTR(cDatabaseName,1,RAT('\',cDatabaseName))
	IF LEN(ALLTRIM(lcDbcPath)) <= 5
		MESSAGEBOX('INVALID PATH')
*		CLOSE ALL 
		CANCEL 
	ENDIF 
	lcDbcPath = LEFT(ALLTRIM(lcDbcPath),LEN(ALLTRIM(lcDbcPath)) - 9)
	IF ALIAS() = 'PROTECT'
		SELECT 0
	ENDIF
	IF ! USED('PROTECT')
		IF ! GET_DBF_PWD()
			MESSAGEBOX('INVALID PASSWORD ...')
*			CLOSE ALL
			CANCEL 
		ENDIF
		IF ! FILE(lcDbcPath + 'VFP.DLL')
			MESSAGEBOX('SUPPORTED FILE NOT FOUND')
*			CLOSE all
			CANCEL
		ELSE 
			USE lcDbcPath + 'VFP.DLL' IN 0 SHARED 
			SELECT 0
		ENDIF
		glValidUser = .t.
		RETURN .T.
	ENDIF 
	IF ! FILE(lcDbcPath + 'VFP7.DLL')
		MESSAGEBOX('SUPPORTING FILES FOR DBC PASSWORD NOT FOUND...')
		glValidUser = .F.
	ELSE
		RESTORE FROM lcDbcPath + 'VFP7.DLL' ADDITIVE 
		PROTECT_VALUE = decode(PROTECT_VALUE)
		IF GCPROTECT <> PROTECT_VALUE
			MESSAGEBOX('INVALID PASSWORD ...')
			glValidUser = .F.
		ELSE
			glValidUser = .T.
		ENDIF
	ENDIF
	RETURN .T.
ENDPROC


PROCEDURE dbc_BeforeOpenTable(cTableName)
*Just before a table or view is opened. Return .F. to prevent table or view being opened.

IF ! glValidUser
	MESSAGEBOX('YOU ARE NOT AUTHORISED TO OPEN THIS TABLE')
	CLOSE data all
	quit
ENDIF


FUNCTION GET_DBF_PWD

PRIVATE lcpwd
lcpwd = ' '
PRIVATE lctmp_wind
lctmp_wind = SYS(2015)
DEFINE WINDOW (lctmp_wind) from 0,0 to 10,50 PANE TITLE 'DATA SECURITY PASSWORD'
ACTIVATE WINDOW (lctmp_wind) 
MOVE WINDOW (lctmp_wind) cent 
@ 3,2 say 'PASSWORD : ' 
SET CONSOLE OFF 
ACCEPT TO LCPWD
SET CONSOLE ON
RELEASE WINDOWS (lctmp_wind)
IF EMPTY(lcpwd)
	MESSAGEBOX('INVALID PASSWORD')
	CLOSE DATABASES ALL 
	QUIT 
	RETURN .F.
ELSE
	RESTORE FROM lcDbcPath + 'VFP7.DLL' ADDITIVE 
	lcpwd = decode(PADR(lcpwd,LEN(PROTECT_VALUE)))
	IF lcpwd <> decode(PROTECT_VALUE)
		MESSAGEBOX('INVALID PASSWORD')
		CLOSE DATABASES ALL 
		QUIT 
		RETURN .F.
	ELSE 
		GCPROTECT = PROTECT_VALUE
		glValidUser = .T.
		RETURN .T.
	ENDIF
ENDIF


function decode
parameter mstr
retu(chrt(mstr,'ÜÝÞßàáâãäåæçèéêëìíîïðñòóôõ¥§©«­¯±³µ·¹»½¿ÁÃÅÇÉËÍÏÑÓÕ×','abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'))
