[LON-CAPA-cvs] cvs: lonline /cgi-bin penroll.pl

www lon-capa-cvs@mail.lon-capa.org
Wed, 01 Jan 2003 14:37:15 -0000


www		Wed Jan  1 09:37:15 2003 EDT

  Added files:                 
    /lonline/cgi-bin	penroll.pl 
  Log:
  This is the LectureOnline routine to automatically enroll students from
  SISinfo. I had to add "SS03" ... I wish this is the LAST semester.
  
  

Index: lonline/cgi-bin/penroll.pl
+++ lonline/cgi-bin/penroll.pl
#!/usr/bin/perl 

use IO::Socket;
use MIME::Base64;

$version='1.5 MSU';
$classhost="www.msu.edu";
$classport=80;
$rawpath="/service/clist/web/";
$rawfile="data.csv";
$home='http:/lecture/';
$enroll='http:/cgi-bin/penroll.pl';
$userdir='../users';
$classdir='../lecture';
$enrolldir='../classlst';

# ------------------------------------------------------ Print out what we need
print "Content-type: text/html\n\n";
print "<html>\n";
# --------------------------------------------------------------- Get the input
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

# Split the name-value pairs
@pairs = split(/&/, $buffer);

foreach $pair (@pairs)
{
    ($name, $value) = split(/=/, $pair);
    $value =~ tr/+/ /;
    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;

    # Stop people from using subshells to execute commands
    $value =~ s/~!/ ~!/g; 

    # Uncomment for debugging purposes
    # print "Setting $name to $value<P>";

    $FORM{$name} = $value;
}

$buffer=$ENV{'QUERY_STRING'};

# Split the name-value pairs
@pairs = split(/&/, $buffer);

foreach $pair (@pairs)
{
    ($name, $value) = split(/=/, $pair);
    $value =~ tr/+/ /;
    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;

    # Stop people from using subshells to execute commands
    $value =~ s/~!/ ~!/g; 

    # Uncomment for debugging purposes
    # print "Setting $name to $value<P>";

    $FORM{$name} = $value;
}
# ------------------------------------------------------------ Fast Lane Login?
if ($FORM{'uname'} eq "") { goto REJECTED; }
open(USERDATA,"$userdir/$FORM{'upass'}") || goto REJECTED;
$readinfo[0]=<USERDATA>;
$readinfo[1]=<USERDATA>;
$readinfo[2]=<USERDATA>;
close(USERDATA);
chop($readinfo[0]);
chop($readinfo[1]);
chop($readinfo[2]);
$ustatus=$readinfo[2];
if (($readinfo[0] ne $FORM{'uname'}) || ($readinfo[1] ne $FORM{'uclass'})) {
   goto REJECTED;
}

# --------------------------------------------------------- End user validation
if ($ustatus eq 'teacher') {
   print "<Head><Title>MSU LectureOnline</Title>";
   print "</Head>\n";
   print '<Body bgcolor="#FFFFFF">';
   print "<basefont size=3>\n";
   print "<center><img src='$home";
   print "liteani.gif' align=right></center><p>\n"; 
   print "<center><h1>MSU Enrollment Update for $FORM{'uclass'}.</h1>";
   print "</center><hr>\n";
   if ($FORM{'uaction'} eq 'update') {


print "<h3>Connecting ...</h3>";
unless ( $socket=IO::Socket::INET->new(PeerAddr => $classhost,
                                      PeerPort => $classport,
                                      Type     => $SOCK_STREAM,
                                      Proto    => 'tcp') ) {
    print "<h1>Could not connect: $@</h1>\n";
    goto REJECTED;
}

print "<h3>Authenticating ...</h3>";
$fullpath=$rawpath.
          $FORM{'pilotSem'}.'/'.
          $FORM{'pilotSubj'}.'/'.
          $FORM{'pilotNum'}.'/'.$rawfile;

$encoded=encode_base64($FORM{'pilotID'}.':'.$FORM{'pilotPass'});

print $socket "GET $fullpath HTTP/1.0\n";
print $socket "Authorization: Basic $encoded\n\n";
$response=<$socket>;

unless ($response=~/OK/) {
    print "<h1>Error: $response</h1>\n";
    goto REJECTED;
}

print "<h3>Receiving Data ...</h3>\n";
@students=<$socket>;
close $socket;

print "<h3>Processing Data ...</h3>\n";

for ($i=0;$i<=$#students;$i++) {
    chomp($students[$i]);
    @contents=split(/"\s*,\s*"/,$students[$i]);
    if ($contents[6] ne '') {
     $contents[$#contents]=~s/"//;
     $newlist{$contents[$#contents]}=
     "$contents[8]:$contents[$#contents]:\@$contents[6]:*:*:*:*:*:*:*:*:*:*\n";
    }
}

if (-e "$enrolldir/$FORM{'uclass'}.dat") {
open(OLDDATA,"$enrolldir/$FORM{'uclass'}.dat");
@students=<OLDDATA>;
close OLDDATA;

for ($i=0;$i<=$#students;$i++) {
    @contents=split(/:/,$students[$i]);
    $oldlist{$contents[1]}=$students[$i];
}

foreach $key (keys %oldlist) {
    @contents=split(/:/,$oldlist{$key});
    if ($contents[2]=~/\@/) {
       if ((defined($newlist{$key})) ||
           ($FORM{'dropstudents'} ne 'on' )) {
          $newnewlist{$key}=$oldlist{$key};
       } else {
          print "Dropping $contents[1]<br>\n";
       }
       delete($newlist{$key});
    } else {
       $newnewlist{$key}=$oldlist{$key}; 
    }
}
}

foreach $key (keys %newlist) {
   @contents=split(/:/,$newlist{$key});
   $newnewlist{$key}=$newlist{$key};
   print "Adding $contents[1]<br>\n";
}

open(NEWDATA,">$enrolldir/$FORM{'uclass'}.dat");
foreach $key (keys %newnewlist) {
   print NEWDATA $newnewlist{$key};
}
close(NEWDATA);    

} elsif ($FORM{'uaction'} eq 'handadd') {
   print "Adding $FORM{'spilotID'}<br>";
   open(NEWDATA,">>$enrolldir/$FORM{'uclass'}.dat");
   print NEWDATA "$FORM{'spilotName'}:$FORM{'spilotID'}:\@$FORM{'spilotPID'}".
                 ":*:*:*:*:*:*:*:*:*:*\n";
   close(NEWDATA);
} else {
# ------------------------------------------------------- 
   print "<form action=$enroll method=post>\n";
   print "<p><h1>Update from Classlist Server</h1>";
   print "<input name='ures' type=hidden value=$FORM{'ures'}>";
   print "<input name='uname' type=hidden value=$FORM{'uname'}>\n";
   print "<input name='upass' type=hidden value=$FORM{'upass'}>\n";
   print "<input name='uclass' type=hidden value=$FORM{'uclass'}>\n";
   print "<input name='upage' type=hidden value=$FORM{'upage'}>\n";
   print "<input name='uaction' type=hidden value='update'>\n";
   print "<hr><h3>Instructor User Validation</h3>";
   print "<table><tr><td>Pilot ID:</td>";
   print "<td><input name='pilotID' type=text size=10></td></tr><tr><td>";
   print "Password:</td><td><input name='pilotPass' type=password size=10>";
   print "</td></tr></table><hr>";
   print "<h3>Class Information</h3>";
   print "<table><tr><td>Semester:</td>";
   print "<td><select name='pilotSem'>";
   print (<<ENDSELECT);
                 <option value="ss01">SS01</option>
                 <option value="us01">US01</option>
                 <option value="fs01">FS01</option>                 
                 <option value="ss02">SS02</option>
                 <option value="us02">US02</option>
                 <option value="fs02">FS02</option>
                 <option value="ss03">SS03</option>
</select>
ENDSELECT
   print "</td></tr><tr><td>Subject (for example <tt>phy</tt>):</td>";
   print "<td><input name='pilotSubj' type=text size=3></td></tr>";
   print "<tr><td>Number (for example <tt>231c</tt>):";
   print "</td><td><input name='pilotNum' type=text size=4>";
   print "</td></tr></table><p>";
   print "<input type=checkbox name=dropstudents>";
   print "Full update: also drop students<br>";
   print "<input value='Update Class Enrollment List' type=submit>";
   print "</form><p><hr><p>\n";
   print "<form action=$enroll method=post>\n";
   print "<p><h1>Add Pilot-Authenticated Student by Hand</h1>";
   print "<input name='ures' type=hidden value=$FORM{'ures'}>";
   print "<input name='uname' type=hidden value=$FORM{'uname'}>\n";
   print "<input name='upass' type=hidden value=$FORM{'upass'}>\n";
   print "<input name='uclass' type=hidden value=$FORM{'uclass'}>\n";
   print "<input name='upage' type=hidden value=$FORM{'upage'}>\n";
   print "<input name='uaction' type=hidden value='handadd'>\n";
   print "<table><tr><td>StudentPilot ID (case sensitive!)</td>";
   print "<td><input name='spilotID' size=10></td></tr>\n";
   print "<tr><td>Student Number</td>";
   print "<td><input name='spilotPID' size=12></td></tr>\n";
   print "<tr><td>Student Name (Last Name, First Names)</td>";
   print "<td><input name='spilotName' size=40></td></tr></table><p>";
   print "<input type=submit value='Enroll Student'></form>\n";
}
}
REJECTED:
print "</html>";
exit 0;