#!%TCLSH%


#
# Script pour saisir les modifications demandes par un correspondant.
#
# Appel par : script modif (page lib/modif.htgt)
#
# Paramtres (formulaire ou URL) :
#   - dition des informations d'une machine
#	- nom : nom de la machine  ajouter
#	- domaine : domaine dans lequel elle doit tre ajoute
#
# Historique
#   2002/05/03 : pda/jean : cration
#   2002/07/09 : pda      : ajout de nologin
#   2002/07/09 : pda      : conversion des noms en minuscules
#   2003/05/13 : pda/jean : utilisation de la base d'authentification
#   2004/08/05 : pda/jean : ajout MAC
#   2005/04/08 : pda/jean : ajout dhcpprofil
#   2008/07/25 : pda/jean : ajout du droit d'mission SMTP
#   2010/10/26 : pda      : rendre le nom+domaine ditable
#   2010/10/31 : pda      : ajout ttl
#

set conf(homeurl)	%HOMEURL%

#
# Chemins utiliss par les scripts
#

set conf(pkg)		%PKGTCL%
set conf(lib)		%DESTDIR%/lib
set conf(libdns)	$conf(lib)/libdns.tcl

#
# Dfinition des noms des pages " trous"
#

set conf(page-edit-infos)	$conf(lib)/editmodif-infos.html
set conf(err)			$conf(lib)/erreur.html

#
# Quelques paramtres du script
#

set conf(auth)		%AUTH%
set conf(base)		%BASE%
set conf(nologin)	%NOLOGIN%
set conf(log)		%LOG%

# le champ discriminant pour la suite
set conf(form)		{
	{action		1 1}
}

# les champs utiliss pour chacune des actions
set conf(form-modif-infos)	{
	{nom		1 1}
	{domaine	1 1}
	{nextprog	0 1}
	{nextargs	0 1}
}

#
# Les outils du parfait concepteur de pages Web dynamiques...
#

lappend auto_path $conf(pkg)
package require webapp
package require pgsql

#
# On y va !
#

# ::webapp::cgidebug ; exit

source $conf(libdns)

##############################################################################
# Ajout d'un nom
##############################################################################

# Historique
#   2002/05/03 : pda/jean : conception
#
proc editmodif-infos {dbfd idcor ft} {
    global conf
    upvar $ft ftab

    #
    # Valider les champs du formulaire
    #

    set nom           [string trim [lindex $ftab(nom) 0]]
    set domaine       [string trim [lindex $ftab(domaine) 0]]
    set nextprog      [string trim [lindex $ftab(nextprog) 0]]
    set nextargs      [string trim [lindex $ftab(nextargs) 0]]

    #
    # Valider la syntaxe du nom au sens de la RFC ????
    #

    set m [syntaxe-nom $nom]
    if {[string length $m] > 0} then {
	::webapp::error-exit $conf(err) "Erreur sur le nom '$nom': $m"
    }
    set nom [string tolower $nom]

    #
    # Valider le domaine, le nom (qui ne doit pas tre un alias, un MX, etc)
    #

    set msg [valide-droit-nom $dbfd $idcor $nom $domaine trr "machine-existante"]
    if {[string length $msg] > 0} then {
	::webapp::error-exit $conf(err) $msg
    }

    #
    # Vrifier que le nom  modifier existe bien
    #

    if {[string equal $trr(idrr) ""]} then {
	::webapp::error-exit $conf(err) "Le nom '$nom' n'existe pas"
    }

    #
    # Pour l'affichage du nom original de la machine
    #

    set vnom $nom
    set vdomaine $domaine

    #
    # Pr-saisir les champs  diter
    #

    set nom [::webapp::form-text nom 1 20 64 $nom]
    set domaine [menu-domaine $dbfd $idcor domaine "" $domaine $conf(err)]

    set val [::webapp::html-string $trr(mac)]
    set mac [::webapp::form-text mac 1 20 17 $val]
    set dhcpprofil [menu-dhcpprofil $dbfd iddhcpprofil $idcor $trr(iddhcpprofil)]
    set dhcpprofillibelle [lindex $dhcpprofil 0]
    set dhcpprofilmenu    [lindex $dhcpprofil 1]

    set menuhinfo [menu-hinfo $dbfd hinfo $trr(hinfo)]

    set droitsmtp [menu-droitsmtp $dbfd "droitsmtp" $idcor $trr(droitsmtp)]
    set droitsmtplibelle [lindex $droitsmtp 0]
    set droitsmtpmenu    [lindex $droitsmtp 1]

    set ttl [menu-ttl $dbfd "ttl" $idcor $trr(ttl)]
    set ttllibelle [lindex $ttl 0]
    set ttlval     [lindex $ttl 1]

    set val [::webapp::html-string $trr(commentaire)]
    set commentaire [::webapp::form-text commentaire 1 50 50 $val]

    set val [::webapp::html-string $trr(respnom)]
    set respnom [::webapp::form-text respnom 1 50 50 $val]

    set val [::webapp::html-string $trr(respmel)]
    set respmel [::webapp::form-text respmel 1 50 50 $val]

    #
    # Sortie du rsultat
    #

    ::webapp::send html [::webapp::file-subst $conf(page-edit-infos) \
				[list \
					[list %IDRR% $trr(idrr)] \
					[list %VNOM% $vnom] \
					[list %VDOMAINE% $vdomaine] \
					[list %NOM% $nom] \
					[list %DOMAINE% $domaine] \
					[list %MAC% $mac] \
					[list %DHCPPROFILLIBELLE% $dhcpprofillibelle] \
					[list %DHCPPROFILMENU% $dhcpprofilmenu] \
					[list %MENUHINFO% $menuhinfo] \
					[list %DROITSMTPLIBELLE% $droitsmtplibelle] \
					[list %DROITSMTPMENU% $droitsmtpmenu] \
					[list %TTLLIBELLE% $ttllibelle] \
					[list %TTLVAL% $ttlval] \
					[list %COMMENTAIRE% $commentaire] \
					[list %RESPNOM% $respnom] \
					[list %RESPMEL% $respmel] \
					[list %NEXTPROG% $nextprog] \
					[list %NEXTARGS% $nextargs] \
				    ] \
			    ]
}

##############################################################################
# Programme principal
##############################################################################

proc main {} {
    global conf

    #
    # Initialisation
    #

    init-dns $conf(nologin) $conf(auth) $conf(base) $conf(err) "" \
			$conf(form) ftab dbfd login tabcor $conf(log)

    #
    # Valider l'action, lire les autres champs du formulaire,
    # et faire le branchement
    #

    set action [lindex $ftab(action) 0]

    if {! [info exists conf(form-$action)]} then {
	::webapp::error-exit $conf(err) "Champ 'action' non conforme : $action"
    }

    if {[llength [::webapp::get-data ftab $conf(form-$action)]] == 0} then {
	::webapp::error-exit $conf(err) "Formulaire non conforme aux spcifications"
    }

    edit$action $dbfd $tabcor(idcor) ftab

    #
    # Dconnexion de la base
    #

    fermer-base $dbfd
}

::webapp::cgi-exec main %DEBUG%
