#!/usr/bin/perl -w
##
## muquit, Jun-12-1999
## generate. <uid>:<crypted_password>:<directory> for upload.pl scritp
## URL: http://www.muquit.com/muquit/software/upload_pl/upload_pl.html
##
## Nov-29-2000, modified for new | separated db file
##
##-----------------------------------------------------

use strict;
use CGI qw/:standard :html3/;;

my $query=new CGI;
my $title="generate <uid>:<crypted_password>:<directory> for upload.pl";


##------
# printHTMLHeader()
##------
sub printHTMLHeader
{
    print $query->start_html(
            -title=>"$title",
            -bgcolor=>"#ffffff",
            -link=>"#000099",
            -vlink=>"#000099",
            -alink=>"#ffff00",
            -text=>"#000000");
}

print $query->header;
# print the HTML HEADER
&printHTMLHeader;

# print the HTML HEADER
&printHTMLHeader;

unless ($query->param)
{
    &printForm;
}
else
{
    &doWork;
}

#-------------
# print Form
#-------------
sub printForm
{
    print "<blockquote>\n";
    print<<EOF;
    <center><h2>Encription form per l\'
        <a
        href="http://copernico.dm.unipi.it/cgi-bin/simca/upload.pl">uplaoder</a></h2></center>
    Questo programma serve la stringa crittografata, contenente 
    <tt> Userid, Password </tt> ed <tt> Upload directory </tt> 
    per il programma di upload.
    Per esempio, se si mette
<p>
    "user" come Userid, <br>
    "/home/simca/public_html/incoming" come Upload directory e <br>
    "passwd" come Password, 
<p>
     dopo aver premuto il bottone Submit, 
     l\'output &egrave qualcosa del tipo
    <p><font color="#000099">
     <code> user|xYmEB.iTXAkJI|/home/simca/public_html/incoming </code>
     </font>
<p>
     
EOF
;
    print "<center>\n";
    print "<table border=0 bgcolor=\"#c0c0c0\" cellpadding=5 cellspacing=0>\n";

    print $query->start_form,"\n";

    #------------- userid
    print "<tr>\n";
    print "<td align=\"right\">\n";
    print "Userid:\n";
    print "</td>\n";

    print "<td>\n";
    print $query->textfield(-name=>'userid',
            -size=>30);
    print "</td>\n";
    print "</tr>\n";

    #------------- password
    print "<tr>\n";
    print "<td align=\"right\">\n";
    print "Password:\n";
    print "</td>\n";

    print "<td>\n";
    print $query->password_field(-name=>'password',
            -size=>20);
    print "</td>\n";
    print "</tr>\n";



    #------------- upload directory
    print "<tr>\n";
    print "<td align=\"right\">\n";
    print "Upload directory:\n";
    print "</td>\n";

    print "<td>\n";
    print $query->textfield(-name=>'upload_dir',
            -size=>30);
    print "</td>\n";
    print "</tr>\n";



    # submit
    print "<tr>\n";
    print "<td colspan=2 align=\"center\">\n";
    print "<hr noshade size=1>\n";
    print $query->submit(-label=>'Submit',
            -value=>'Submit'),"\n";
    print "</td>\n";
    print "</tr>\n";

    print $query->end_form,"\n";

    print "</table>\n";
    print "</center>\n";

    print "</blockquote>\n";
}




sub doWork
{
    my @saltchars=('a'..'z','A'..'Z','0'..'9',',','/');
    my $salt='';
    my $crypted_pass='';
    my $word='';
    my $error='';

    my $userid=$query->param('userid');
    my $upload_dir=$query->param('upload_dir');
    my $password=$query->param('password');

    if (! $userid)
    {
        $error="No user id supplied<br>";
    }
    if (! $upload_dir)
    {
        $error .= "No upload directory supplied<br>";
    }
    if (! $password)
    {
        $error .= "No password supplied";
    }

    if ($error)
    {
        &printForm;
        &printError($error);
        return;
    }



    if (! $salt)
    {
        # random seed
        srand(time() ^ ($$ + ($$ << 15)));
        $salt=$saltchars[int(rand(64))];
        $salt .= $saltchars[int(rand(64))];
    }

    $crypted_pass=crypt($password,$salt);
    if ($upload_dir =~ /\\/)
    {
        $upload_dir =~ s/\\/\\\\/g;
        $upload_dir =~ s/\\+/\\\\/g;
    }

    &printForm;
    print "<hr>\n";
    print<<EOF;
    <! center Please cut and paste the followin the line at the end of upload.db file: /center>
    <pre>
    <font color="#000099" size="+2">$userid\|$crypted_pass\|$upload_dir</font></code>
    </pre>
EOF
;

}

sub printError
{
    my $errmsg=shift;

    print "<hr noshade>\n";
    print "<center>\n";
    print "Error:<font color=\"#ff0000\"><b>$errmsg</b></font>";
    print "</center>\n";
}

