' ScriptBasic / IUP - Login - JRS
IMPORT iup.bas
IMPORT sqlite.bas
DECLARE SUB MD5 ALIAS "md5fun" LIB "t"
initdb = FILELEN("user.db")
usrdb = sqlite::open("user.db")
IF initdb = 0 THEN
sqlite::execute(usrdb,"CREATE TABLE user (" & _
"UserID VARCHAR NOT NULL, " & _
"Password VARCHAR NOT NULL, " & _
"CONSTRAINT user_pk PRIMARY KEY (UserID));")
END IF
SUB login_clicked
uid = Iup::GetAttribute(uidtxt,"VALUE")
IF Iup::GetAttribute(showpwd, "VALUE") = "OFF" THEN
pwd = Iup::GetAttribute(pwdtxt1,"VALUE")
ELSE
pwd = Iup::GetAttribute(pwdtxt2,"VALUE")
END IF
' *** NEW USER ***
IF newusr_cb = "ON" THEN
IF uid <> "" AND pwd <> "" THEN
rtnmsg = sqlite::execute(usrdb,"INSERT INTO user VALUES ('" & uid & "', '" & MD5STR(pwd) & "');")
IF rtnmsg = 19 THEN
Iup::SetAttribute errlbl, "TITLE", "Duplicate User ID"
Iup::Show errlbl
Iup::SetFocus uidtxt
EXIT SUB
END IF
Iup::Message "Login", "User Added"
GOSUB Init_Form
EXIT SUB
ELSE
Iup::Show errlbl
Iup::SetFocus uidtxt
EXIT SUB
END IF
END IF
' *** LOGIN VERIFY ***
Iup::SetAttribute errlbl, "TITLE", "User ID / Password Incorrect"
stmt = sqlite::query(usrdb,"SELECT Password FROM user WHERE UserID = '" & uid & "';")
IF sqlite::row(stmt) = sqlite::SQLITE3_ROW THEN
sqlite::fetchhash(stmt, col)
IF col{"Password"} = MD5STR(pwd) THEN
Iup::Hide errlbl
Iup::Message "Login", "Login Successful"
GOSUB Init_Form
EXIT SUB
END IF
END IF
Iup::Show errlbl
Iup::SetFocus uidtxt
EXIT SUB
Init_Form:
Iup::Hide errlbl
Iup::SetAttribute uidtxt, "VALUE", ""
Iup::SetAttribute pwdtxt1, "VALUE", ""
Iup::SetAttribute pwdtxt2, "VALUE", ""
Iup::SetAttribute showpwd, "VALUE", "OFF"
Iup::SetAttribute newusr, "VALUE", "OFF"
Iup::SetAttribute zbtxt, "VALUE", "pwdtxt1"
Iup::SetAttribute loginbut, "TITLE", "Login"
newusr_cb = "OFF"
Iup::SetFocus uidtxt
RETURN
END SUB
FUNCTION MD5STR(txtstr)
LOCAL tmpstr, hexstr, x
tmpstr = MD5(txtstr)
FOR x = 1 TO 16
hexstr &= RIGHT("0" & HEX(ASC(MID(tmpstr, x, 1))), 2)
NEXT
MD5STR = hexstr
END FUNCTION
SUB show_password
IF Iup::GetAttribute(showpwd, "VALUE") = "OFF" THEN
pwd2 = Iup::GetAttribute(pwdtxt2,"VALUE")
Iup::SetAttribute pwdtxt1, "VALUE", pwd2
Iup::SetAttribute zbtxt, "VALUE", "pwdtxt1"
ELSE
pwd1 = Iup::GetAttribute(pwdtxt1,"VALUE")
Iup::SetAttribute pwdtxt2, "VALUE", pwd1
Iup::SetAttribute zbtxt, "VALUE", "pwdtxt2"
END IF
END SUB
SUB new_user
newusr_cb =Iup::GetAttribute(newusr, "VALUE")
IF newusr_cb = "ON" THEN
Iup::SetAttribute loginbut, "TITLE", "Add User"
ELSE
Iup::SetAttribute loginbut, "TITLE", "Login"
END IF
END SUB
SUB Win_exit
Iup::ExitLoop = TRUE
END SUB
' *** DIALOG ***
Iup::Open
dlg = Iup::Create("dialog")
Iup::SetAttributes dlg, _
"TITLE=\"Login\", " & _
"SIZE=200x200, " & _
"MAXBOX=NO, " & _
"MINBOX=NO, " & _
"RESIZE=NO, " & _
"DEFAULTENTER=\"loginbut\""
' *** CONTAINER ***
vb = Iup::Create("vbox")
' *** IMAGE ***
hb1 = Iup::Create("hbox")
piclbl = Iup::Create("label")
Iup::SetAttributes piclbl, _
"IMAGE=\"./login.png\", " & _
"EXPAND=HORIZONTAL, " & _
"ALIGNMENT=ACENTER:ATOP"
Iup::Append hb1, piclbl
Iup::Append vb, hb1
' *** ERROR ***
hb2 = Iup::Create("hbox")
errlbl = Iup::Create("label")
Iup::SetAttributes errlbl, _
"TITLE=\"User ID / Password Incorrect\", " & _
"FGCOLOR=\"#ff0000\", " & _
"EXPAND=HORIZONTAL, " & _
"ALIGNMENT=ACENTER"
Iup::Hide errlbl
Iup::Append hb2, errlbl
Iup::Append vb, hb2
' *** ENTRY ***
hb3 = Iup::Create("hbox")
Iup::SetAttributes hb3, _
"MARGIN=20x10, " & _
"GAP=5"
vb1 = Iup::Create("vbox")
Iup::SetAttribute vb1, "GAP", "15"
uidlbl = Iup::Create("label")
Iup::SetAttribute uidlbl, "TITLE", "User ID"
Iup::Append vb1, uidlbl
pwdlbl = Iup::Create("label")
Iup::SetAttribute pwdlbl, "TITLE", "Password"
Iup::Append vb1, pwdlbl
Iup::Append hb3, vb1
vb2 = Iup::Create("vbox")
uidtxt = Iup::Create("text")
Iup::SetAttribute uidtxt, "SIZE", "85x"
Iup::Append vb2, uidtxt
pwdtxt1 = Iup::Create("text")
Iup::SetAttributes pwdtxt1, _
"PASSWORD=YES, " & _
"SIZE=85x"
pwdtxt2 = Iup::Create("text")
Iup::SetAttribute pwdtxt2, "SIZE", "85x"
Iup::SetHandle "pwdtxt1", pwdtxt1
Iup::SetHandle "pwdtxt2", pwdtxt2
zbtxt = Iup::Zbox(pwdtxt1, pwdtxt2)
Iup::SetHandle "zbtxt", zbtxt
Iup::Append vb2, zbtxt
Iup::Append hb3, vb2
Iup::Append vb, hb3
' *** SHOW/HIDE ***
hb4 = Iup::Create("hbox")
Iup::SetAttributes hb4, _
"MARGIN=35x, " & _
"GAP=40"
showpwd = Iup::Create("toggle")
Iup::SetAttributes showpwd, _
"TITLE=\"Show Password\", " & _
"CANFOCUS=NO"
newusr = Iup::Create("toggle")
Iup::SetAttributes newusr, _
"TITLE=\"New User\", " & _
"CANFOCUS=NO"
Iup::Append hb4, showpwd
Iup::Append hb4, newusr
Iup::Append vb, hb4
' *** LOGIN ***
hb5 = Iup::Create("hbox")
Iup::SetAttribute hb5, "MARGIN", "40x10"
loginbut = Iup::Create("button")
Iup::SetAttributes loginbut, _
"TITLE=\"Login\", " & _
"EXPAND=HORIZONTAL"
Iup::SetHandle "loginbut", loginbut
iup::Append hb5, loginbut
iup::Append vb, hb5
' *** ATTACH CONTAINER ***
iup::Append dlg, vb
' *** CALLBACKS ***
Iup::SetCallback dlg,"CLOSE_CB",ADDRESS(Win_exit())
Iup::SetCallback loginbut, "ACTION", ADDRESS(login_clicked())
Iup::SetCallback showpwd, "ACTION", ADDRESS(show_password())
Iup::SetCallback newusr, "ACTION", ADDRESS(new_user())
' *** PROCESS ***
' Iup::Show(Iup::LayoutDialog(dlg))
Iup::Show(dlg)
Iup::MainLoop
Iup::Close
sqlite::Close(usrdb)