#!/usr/bin/perl -w 
#
# $Id: novell_verlaengern.pl,v 1.1 1997/06/04 21:43:44 webadm Exp $
#
# DESCRIPTION: novell_verlaengern.pl - display all users in a WWW-Formular and
#    forward choosen users for further processing to benutzerantrag_senden.pl
#
# URL: none yet
#
# AUTHOR: Cord Beermann (cord@Wunder-Nett.org)
#
# Thanks to: Ralf Begemann (begemann@cc.fh-lippe.de)
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
#
# A Perl script is "correct" if it gets the job done before your boss fires
# you.
# ('Programming Perl Second Edition' by Larry Wall, Tom Christiansen
#   & Randal. L. Schwartz)
#
# Bugs and shortcomings
# none yet
#
# Todo
# * write instructions
#
### Configuration ###
#
# where we should store that little user.n files ;-)
$DIR='/var/infosystems/www/security/login';
#
# where is the file with the known users
$FILE='/var/infosystems/www/security/benutzer.nt.txt';
#
# which chars do we want as indices
@index= qw(a b c d e f g h i j k l m n o pq r s t uv w xyz);
#
### End of Configuration ###

# include perl-modules
use CGI::Carp;
use CGI qw(:standard);
use CGI qw(:shortcuts);

# initialize the CGI-Module and load the form-input
$query = new CGI;
$query->autoEscape(undef);

# print HTTP and HTML-Header
print $query->header;
print $query->start_html(-"title"=>'Benutzerzugang f&uuml;r NT/Novell verl&auml;ngern',
                         -"author"=>'gnats@thalassa.cc.fh-lippe.de',
                         -"base"=>'true',
                         -"text"=>'#000000',
                         -"BGCOLOR"=>'#FFFFF7');
print "\n";

print(h1("Benutzerzugang f&uuml;r NT/Novell verl&auml;ngern"),
      hr
      );

# check if we got some input to process
if ($query->query_string =~ /=on/) {
    # loop on each parameter we've got.
    foreach $name ($query->param) {
	if ($query->param($name) eq 'on') {
	    # touch the file user.n
	    open(LOGIN, '>' . $DIR . '/' . $name . '.n') or
		die("open $DIR/$name.: $!");
	    close(LOGIN);
	    # give a receipt back
	    print "<p>Verl&auml;ngerung f&uuml;r <em>$name</em> weitergeleitet</p>\n";
	}
    }
    # at the end add a link back to the userlist
    print hr, "\n", $query->a({href=>$query->url},'Hauptseite');
} else {
    # start the WWW-Form
    print $query->startform();
    # print the leading index, to jump in the list
    foreach $name (@index) {
	print "[<A HREF=\"#$name\">$name</a>]";
    }
    # print a button, to submit the choosen users
    print p($query->submit(-name=>'Verl&auml;ngern')), hr;

    # get the list of the known users, sort it and loop over each line
    open(NTUSER, $FILE) or
	die("open $FILE: $!");
    foreach (sort { lc($a) cmp lc($b)} (<NTUSER>)) {
	($login,$realname) = (split(/\,/))[0,1];
	# skip uncomplete lines.
	next unless (defined($login) and defined($realname) and not ($login =~ / /));
	# translate A-Z to a-z
	$login =~ tr/A-Z/a-z/;
	# print login, realname and a checkbox
	push(@list, "$login:$realname",
	     $query->checkbox(-name=>$login,
			      -label=>''
			      ),
	     "<br>\n"
	     );
    }
    # close the file
    close(NTUSER);

    foreach $head (@index) {
	# print a jumpmark for the index
	print "<H2><A NAME=\"$head\">$head</a></H2>\n";
	while ($name = shift(@list)) {
	    if (substr($name,0,1) le substr($head,-1,1)) {
		print $name;
	    } else {
		unshift(@list,$name);
		last;
	    }
	}
    }

    # print another button for submitting
    print hr, $query->submit(-name=>'Verl&auml;ngern');
}

print(hr, address('Autor: ' . a({href=>'http://Cord.de/'}, 'Cord Beermann'
				)
		  )
      );

