# Copyright for the URLibService (c) 1995 - 2019,
# by Gerald Banon. All rights reserved.
# Version 2.1
# forcenewpassword.tcl
# used to force a new password when the user has forgotten its password

if 0 {
# testing
http://vaio:1905/col/dpi.inpe.br/banon/1999/06.19.17.00/doc/forcenewpassword.cgi?languagebutton=pt-BR&returnbutton=yes&targetframe=display___dpi_inpe_br__banon__1999__06_19_17_00
http://vaio:1905/col/dpi.inpe.br/banon/1999/06.19.17.00/doc/forcenewpassword.cgi
}

# ----------------------------------------------------------------------
# ForcePassword

proc ForcePassword {} {
if [catch {
	global env
	global cgi
	global mirrorHomePageRep	;# defined in FindLanguage (utilities1.tcl)
	global homePath	;# used in SynchronizeRepository (sourceDisplayControl), StorePassword, deleteLogin, FindLanguage and FindCurrentHTMLFileName
	global htpasswdPath	;# used in StorePassword
	global administratorUserName	;# used by CreateReturnButton
	global localSite	;# used by Help
	global serverAddressWithIP	;# used by SynchronizeRepository (sourceDisplayControl)
	global loCoInRep	;# used by SynchronizeRepository (sourceDisplayControl), StorePassword and deleteLogin
	global loBiMiRep	;# used while executing SynchronizeRepository (sourceDisplayControl)
	global URLibServiceRepository	;# used while executing SynchronizeRepository (sourceDisplayControl)
	global language languageRep1 languageRep2	;# used in FindCurrentHTMLFileName
	global submissionFormLanguage submissionFormLanguageRep	;# used in FindCurrentHTMLFileName

# homePath (used in StorePassword and FindLanguage)
	set homePath $env(DOCUMENT_ROOT)
# htpasswdPath (used in StorePassword)
	set htpasswdPath $env(HTPASSWD_PATH)
# mirrorHomePageRepository
# used in sourceDisplayControl
	set mirrorHomePageRepository dpi.inpe.br/banon/2000/01.23.20.24

#	array set environment [array get env]	;# used in MultipleSubmit

	set col ../../../../..
# URLibServiceRepository
	set URLibServiceRepository $env(URLIB_SERVICE_REP)
	source ../$col/$URLibServiceRepository/doc/utilities1.tcl
	source ../$col/$URLibServiceRepository/doc/cgi/mirrorfind-.tcl

# serverAddress
	set serverAddress [list $env(SERVER_NAME) $env(URLIB_PORT)]
# serverAddressWithIP
	set serverAddressWithIP [list $env(IP_ADDR) $env(URLIB_PORT)]
# localSite
	set localSite $env(SERVER_NAME):$env(SERVER_PORT)
# loCoInRep
	set loCoInRep $env(LOCOINREP)
# loBiMiRep
	set loBiMiRep $env(LOBIMIREP)

# currentRep (local mirror repository - contains @siteList.txt)
	set uri [file split $env(REQUEST_URI)]
	regsub -all { } [lrange $uri 2 5] {/} currentRep	;# used in mirrorHomePage.html

# Find the language and the language repository
# use the same languages as used for the local bibliographic mirror
foreach {language languageRep1 languageRep2 firstLanguageRep \
submissionFormRep submissionFormLanguage submissionFormLanguageRep} \
[FindLanguage $currentRep] {break}
# Find the language and the language repository - end

	set currentFileName [FindCurrentHTMLFileName ForcePassword]
	set currentProcedureName ForcePassword
	set currentProcedureFileName $homePath/col/$URLibServiceRepository/doc/cgi/forcenewpassword.tcl


	puts {Content-Type: text/html}
	puts {}

# bgColor, background and bgProperties
	foreach {bgColor background bgProperties fontTag fontTag2} [GetBg $languageRep1 $language] {break}
	set background [subst $background]

# display
	set display [GetFrameName]

	if [info exists env(QUERY_STRING)] {
		foreach {name value} [split $env(QUERY_STRING) &=] {
			set cgi([DecodeURL $name]) [DecodeURL $value]
		}
	}

# Create cgi array
# usefull to recover the filled fields after a submit error
	CreateCGIArray
# Create cgi array - end

# puts [array get cgi]

# Source displayControl.tcl
	set enableOutput 0
	eval $sourceDisplayControl
# Source displayControl.tc - end

	set cellFont {<FONT class=cellFont>}

	source ../$col/$languageRep2/doc/mirror/${submissionFormLanguage}ForcePassword.tcl

	if {![info exists cgi(action)] || [string equal createpage $cgi(action)]} {
# create the page

		global "${languageRep2}::Please wait for the submission completion."
		global "${languageRep2}::Cancel"

		if ![info exists cgi(username)] {set cgi(username) {}}
		source ../$col/$languageRep2/doc/mirror/${language}ForcePassword.tcl
		if [file exists ../$col/$submissionFormLanguageRep/doc/${submissionFormLanguage}ForcePassword.html] {
			Load ../$col/$submissionFormLanguageRep/doc/${submissionFormLanguage}ForcePassword.html body
		} else {
			if [file exists ../$col/$languageRep1/doc/${language}ForcePassword.html] {
				Load ../$col/$languageRep1/doc/${language}ForcePassword.html body
			} else {
				Load ../$col/$languageRep2/doc/${language}ForcePassword.html body
			}
		}
# action
		set action ${Please wait for the submission completion.}
# seconds
		set seconds "<INPUT TYPE=HIDDEN NAME=\"seconds\" VALUE=\"[clock seconds]\">"
# sessionTime
# symmetricKey
#		regexp {(.*)-(.*)} [OpenSession $env(REMOTE_ADDR) symmetricKey] m sessionTime symmetricKey
		foreach {sessionTime symmetricKey} [OpenSession [list $env(REMOTE_ADDR) administrator] symmetricKey] {break}

		ConditionalSet returnButtonValue cgi(returnbutton) {no}
		ConditionalSet targetFrame cgi(targetframe) {}
		set returnButton [CreateReturnButton <HR> $col/$languageRep2/doc/mirror mirror.cgi/About?languagebutton=$language $targetFrame $Cancel]
#		puts $body
		puts [subst $body]
		return
	}

	global "${languageRep2}::header"
	global "${languageRep2}::footer"
	global "${languageRep2}::empty username"
	global "${languageRep2}::wrong username"
	global "${languageRep2}::unknown username"
#	global "${languageRep2}::empty passwords"
	global "${languageRep2}::assigned username"
	global "${languageRep2}::empty password1"
	global "${languageRep2}::empty password2"
	global "${languageRep2}::passwords are different"
	global "${languageRep2}::new password forced successfully"
	global "${languageRep2}::login deleted successfully"
	global "${languageRep2}::Copy"
	global "${languageRep2}::Return"
	global "${languageRep2}::Continue"

	set queryInfo ?languagebutton=$language	;# cannot be within POST - otherwise language is lost

# process Copy Button
	if {![info exists cgi(action)] || [string equal copy $cgi(action)]} {
# creat a confirmation copy
# the new password has been forced successfully
		set userName $cgi(username)
		catch {SetFont [subst [subst ${new password forced successfully}]]} output
		puts $output
		return
	}
# process Copy Button - end

# Make Return Button
	set returnButton "
<TABLE ALIGN=CENTER><TR><TD WIDTH=200><BR><HR></TD></TR></TABLE><TABLE ALIGN=CENTER><TR>
<TABLE ALIGN=CENTER><TR><TD>
<FORM NAME=error METHOD=POST ACTION=\"http://$localSite/col/$currentRep/doc/forcenewpassword.cgi$queryInfo\">
"
	CreateHiddenInput returnButton
	append returnButton "\
<INPUT class=button TYPE=submit VALUE=$Continue>
</FORM></TD></TR></TABLE>
" 
# Make Return Button - end

	set userName $cgi(username)
	ConditionalSet password1 cgi(codedpassword1) {}
	ConditionalSet password2 cgi(codedpassword2) {}

# Check form
	set return [Execute $serverAddress [list CheckUsernamePassword \
	$userName $password1 $password2 \
	1 read {} {} \
	{} 0]]	;# don't seek in other site 
	if [string equal {empty username} $return] {
		set return {empty username}
	} elseif {[string equal {wrong username} $return]} {
		set return {wrong username}
	} elseif {[string equal {unknown username} $return]} {
		set return {unknown username}
	} elseif {[string equal {} $password1] && [string equal {} $password2]} {
#		set return {empty passwords}
# DELETE
		Load $homePath/col/$loCoInRep/auxdoc/xxx data binary
		set data [UnShift $data]
		set codedPassword [lindex $data end]
		set query "usern $userName"
		if {[llength [FindMetadataRepositories $query 0 [list $serverAddress] $codedPassword]] > 0} {
			set return {assigned username}
		}
	} elseif {[string equal {} $password1]} {
		set return {empty password1}
	} elseif {[string equal {} $password2]} {
		set return {empty password2}
	} elseif {![string equal $password1 $password2]} {
		set return {passwords are different}
	} else {
		set return {}
	}

	if {![string equal {} $return] && ![string equal {empty password} $return]} {
# force new password error
		set output [subst [subst [subst $[list $return]]]]
		regsub {NAME="action" VALUE="forcepassword"} $output \
			{NAME="action" VALUE="createpage"} output
		puts $output
		return
	}

# Check form - end

# OK
# Waiting for the completion of other authentication
	while {[EnterQueue [pid] authentication]} {
		set x 0; after 100 {set x 1}; vwait x
	}
# Waiting for the completion of other authentication - end

# administratorUserName
	regsub {@.*$} $env(SERVER_ADMIN) {} administratorUserName

	if {[string equal {} $password1] && [string equal {} $password2]} {
# DELETE
		DeleteLogin $userName
		catch {SetFont [subst [subst ${login deleted successfully}]]} output
	} else {
# STORE
		StorePassword $userName $password1
		catch {SetFont [subst [subst ${new password forced successfully}]]} output
	}

	LeaveQueue [pid] authentication

	puts $output
# set xxx 3a-[pid]
# Store xxx C:/tmp/bbb auto 0 a 
} m] {puts $m; if 0 {global errorInfo; puts $errorInfo}}

}

# ForcePassword - end
# ----------------------------------------------------------------------
