--- loncom/lonnet/perl/lonnet.pm	2002/02/25 14:33:58	1.203
+++ loncom/lonnet/perl/lonnet.pm	2002/05/05 01:59:42	1.210
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # TCP networking package
 #
-# $Id: lonnet.pm,v 1.203 2002/02/25 14:33:58 www Exp $
+# $Id: lonnet.pm,v 1.210 2002/05/05 01:59:42 www Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -84,7 +84,7 @@ qw(%perlvar %hostname %homecache %hostip
 use IO::Socket;
 use GDBM_File;
 use Apache::Constants qw(:common :http);
-use HTML::TokeParser;
+use HTML::LCParser;
 use Fcntl qw(:flock);
 my $readit;
 
@@ -137,6 +137,7 @@ sub subreply {
 
 sub reply {
     my ($cmd,$server)=@_;
+    unless (defined($hostname{$server})) { return 'no_such_host'; }
     my $answer=subreply($cmd,$server);
     if ($answer eq 'con_lost') {
        sleep 5; 
@@ -715,6 +716,7 @@ sub flushcourselogs {
     &logthis('Flushing course log buffers');
     foreach (keys %courselogs) {
         my $crsid=$_;
+	&logthis(":$crsid:$coursehombuf{$crsid}");
         if (&reply('log:'.$coursedombuf{$crsid}.':'.
 		          &escape($courselogs{$crsid}),
 		          $coursehombuf{$crsid}) eq 'ok') {
@@ -912,10 +914,55 @@ sub devalidate {
     }
 }
 
+sub arrayref2str {
+  my ($arrayref) = @_;
+  my $result='_ARRAY_REF__';
+  foreach my $elem (@$arrayref) {
+    if (ref($elem) eq 'ARRAY') {
+      $result.=&escape(&arrayref2str($elem)).'&';
+    } elsif (ref($elem) eq 'HASH') {
+      $result.=&escape(&hashref2str($elem)).'&';
+    } elsif (ref($elem)) {
+      &logthis("Got a ref of ".(ref($elem))." skipping.");
+    } else {
+      $result.=&escape($elem).'&';
+    }
+  }
+  $result=~s/\&$//;
+  return $result;
+}
+
 sub hash2str {
-  my (%hash)=@_;
-  my $result='';
-  foreach (keys %hash) { $result.=escape($_).'='.escape($hash{$_}).'&'; }
+  my (%hash) = @_;
+  my $result=&hashref2str(\%hash);
+  $result=~s/^_HASH_REF__//;
+  return $result;
+}
+
+sub hashref2str {
+  my ($hashref)=@_;
+  my $result='_HASH_REF__';
+  foreach (keys(%$hashref)) {
+    if (ref($_) eq 'ARRAY') {
+      $result.=&escape(&arrayref2str($_)).'=';
+    } elsif (ref($_) eq 'HASH') {
+      $result.=&escape(&hashref2str($_)).'=';
+    } elsif (ref($_)) {
+      &logthis("Got a ref of ".(ref($_))." skipping.");
+    } else {
+      $result.=&escape($_).'=';
+    }
+
+    if (ref($$hashref{$_}) eq 'ARRAY') {
+      $result.=&escape(&arrayref2str($$hashref{$_})).'&';
+    } elsif (ref($$hashref{$_}) eq 'HASH') {
+      $result.=&escape(&hashref2str($$hashref{$_})).'&';
+    } elsif (ref($$hashref{$_})) {
+      &logthis("Got a ref of ".(ref($$hashref{$_}))." skipping.");
+    } else {
+      $result.=&escape($$hashref{$_}).'&';
+    }
+  }
   $result=~s/\&$//;
   return $result;
 }
@@ -925,9 +972,39 @@ sub str2hash {
   my %returnhash;
   foreach (split(/\&/,$string)) {
     my ($name,$value)=split(/\=/,$_);
-    $returnhash{&unescape($name)}=&unescape($value);
+    $name=&unescape($name);
+    $value=&unescape($value);
+    if ($value =~ /^_HASH_REF__/) {
+      $value =~ s/^_HASH_REF__//;
+      my %hash=&str2hash($value);
+      $value=\%hash;
+    } elsif ($value =~ /^_ARRAY_REF__/) {
+      $value =~ s/^_ARRAY_REF__//;
+      my @array=&str2array($value);
+      $value=\@array;
+    }
+    $returnhash{$name}=$value;
   }
-  return %returnhash;
+  return (%returnhash);
+}
+
+sub str2array {
+  my ($string) = @_;
+  my @returnarray;
+  foreach my $value (split(/\&/,$string)) {
+    $value=&unescape($value);
+    if ($value =~ /^_HASH_REF__/) {
+      $value =~ s/^_HASH_REF__//;
+      my %hash=&str2hash($value);
+      $value=\%hash;
+    } elsif ($value =~ /^_ARRAY_REF__/) {
+      $value =~ s/^_ARRAY_REF__//;
+      my @array=&str2array($value);
+      $value=\@array;
+    }
+    push(@returnarray,$value);
+  }
+  return (@returnarray);
 }
 
 # -------------------------------------------------------------------Temp Store
@@ -1783,23 +1860,28 @@ sub modifyuserauth {
 
 # --------------------------------------------------------------- Modify a user
 
-
 sub modifyuser {
-    my ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,
-        $forceid)=@_;
+    my ($udom,    $uname, $uid,
+        $umode,   $upass, $first,
+        $middle,  $last,  $gene,
+        $forceid, $desiredhome)=@_;
     $udom=~s/\W//g;
     $uname=~s/\W//g;
     &logthis('Call to modify user '.$udom.', '.$uname.', '.$uid.', '.
              $umode.', '.$first.', '.$middle.', '.
-	     $last.', '.$gene.'(forceid: '.$forceid.') by '.
-             $ENV{'user.name'}.' at '.$ENV{'user.domain'});  
+	     $last.', '.$gene.'(forceid: '.$forceid.')'.
+             (defined($desiredhome) ? ' desiredhome = '.$desiredhome :
+                                     ' desiredhome not specified'). 
+             ' by '.$ENV{'user.name'}.' at '.$ENV{'user.domain'});
     my $uhome=&homeserver($uname,$udom);
 # ----------------------------------------------------------------- Create User
     if (($uhome eq 'no_host') && ($umode) && ($upass)) {
         my $unhome='';
-	if ($ENV{'course.'.$ENV{'request.course.id'}.'.domain'} eq $udom) {
+        if (defined($desiredhome) && $hostdom{$desiredhome} eq $udom) { 
+            $unhome = $desiredhome;
+	} elsif($ENV{'course.'.$ENV{'request.course.id'}.'.domain'} eq $udom) {
 	    $unhome=$ENV{'course.'.$ENV{'request.course.id'}.'.home'};
-        } else {
+        } else { # load balancing routine for determining $unhome
             my $tryserver;
             my $loadm=10000000;
             foreach $tryserver (keys %libserv) {
@@ -1813,7 +1895,8 @@ sub modifyuser {
 	    }
         }
         if (($unhome eq '') || ($unhome eq 'no_host')) {
-	    return 'error: find home';
+	    return 'error: unable to find a home server for '.$uname.
+                   ' in domain '.$udom;
         }
         my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':'.$umode.':'.
                          &escape($upass),$unhome);
@@ -1824,7 +1907,7 @@ sub modifyuser {
         if (($uhome eq '') || ($uhome eq 'no_host') || ($uhome ne $unhome)) {
 	    return 'error: verify home';
         }
-    }
+    }   # End of creation of new user
 # ---------------------------------------------------------------------- Add ID
     if ($uid) {
        $uid=~tr/A-Z/a-z/;
@@ -1842,6 +1925,7 @@ sub modifyuser {
     my %names=&get('environment',
 		   ['firstname','middlename','lastname','generation'],
 		   $udom,$uname);
+    if ($names{'firstname'} =~ m/^error:.*/) { %names=(); }
     if ($first)  { $names{'firstname'}  = $first; }
     if ($middle) { $names{'middlename'} = $middle; }
     if ($last)   { $names{'lastname'}   = $last; }
@@ -1859,14 +1943,15 @@ sub modifyuser {
 
 sub modifystudent {
     my ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$usec,
-        $end,$start,$forceid)=@_;
+        $end,$start,$forceid,$desiredhome)=@_;
     my $cid='';
     unless ($cid=$ENV{'request.course.id'}) {
 	return 'not_in_class';
     }
 # --------------------------------------------------------------- Make the user
     my $reply=&modifyuser
-	($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$forceid);
+	($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$forceid,
+         $desiredhome);
     unless ($reply eq 'ok') { return $reply; }
     my $uhome=&homeserver($uname,$udom);
     if (($uhome eq '') || ($uhome eq 'no_host')) { 
@@ -2305,7 +2390,7 @@ sub metadata {
         my %metathesekeys=();
         unless ($filename=~/\.meta$/) { $filename.='.meta'; }
 	my $metastring=&getfile($perlvar{'lonDocRoot'}.'/res/'.$filename);
-        my $parser=HTML::TokeParser->new(\$metastring);
+        my $parser=HTML::LCParser->new(\$metastring);
         my $token;
         undef %metathesekeys;
         while ($token=$parser->get_token) {
@@ -2394,7 +2479,7 @@ sub metadata {
 		  $metacache{$uri.':'.$unikey.'.'.$_}=$token->[2]->{$_};
               }
               unless (
-                 $metacache{$uri.':'.$unikey}=$parser->get_text('/'.$entry)
+                 $metacache{$uri.':'.$unikey}=&HTML::Entities::decode($parser->get_text('/'.$entry))
 		      ) { $metacache{$uri.':'.$unikey}=
 			      $metacache{$uri.':'.$unikey.'.default'};
 		      }
@@ -2422,7 +2507,7 @@ sub symblist {
         if (tie(%hash,'GDBM_File',$ENV{'request.course.fn'}.'_symb.db',
                       &GDBM_WRCREAT,0640)) {
 	    foreach (keys %newhash) {
-                $hash{declutter($_)}=$mapname.'___'.$newhash{$_};
+                $hash{declutter($_)}=&symbclean($mapname.'___'.$newhash{$_});
             }
             if (untie(%hash)) {
 		return 'ok';
@@ -2432,12 +2517,23 @@ sub symblist {
     return 'error';
 }
 
+# --------------------------------------------------------------- Clean-up symb
+
+sub symbclean {
+    my $symb=shift;
+# remove version from map
+    $symb=~s/\.(\d+)\.(\w+)\_\_\_/\.$2\_\_\_/;
+# remove version from URL
+    $symb=~s/\.(\d+)\.(\w+)$/\.$2/;
+    return $symb;
+}
+
 # ------------------------------------------------------ Return symb list entry
 
 sub symbread {
     my $thisfn=shift;
     unless ($thisfn) {
-        if ($ENV{'request.symb'}) { return $ENV{'request.symb'}; }
+        if ($ENV{'request.symb'}) { return &symbclean($ENV{'request.symb'}); }
 	$thisfn=$ENV{'request.filename'};
     }
     $thisfn=declutter($thisfn);
@@ -2496,7 +2592,7 @@ sub symbread {
            } 
         }
         if ($syval) {
-           return $syval.'___'.$thisfn; 
+           return &symbclean($syval.'___'.$thisfn); 
         }
     }
     &appenv('request.ambiguous' => $thisfn);
@@ -2635,6 +2731,7 @@ sub unescape {
 # ================================================================ Main Program
 
 sub goodbye {
+   &logthis("Starting Shut down");
    &flushcourselogs();
    &logthis("Shutting down");
 }
@@ -2897,12 +2994,30 @@ devalidate($symb) : devalidate spreadshe
 =item *
 
 hash2str(%hash) : convert a hash into a string complete with escaping and '='
-and '&' separators
+and '&' separators, supports elements that are arrayrefs and hashrefs
+
+=item *
+
+hashref2str($hashref) : convert a hashref into a string complete with
+escaping and '=' and '&' separators, supports elements that are
+arrayrefs and hashrefs
+
+=item *
+
+arrayref2str($arrayref) : convert an arrayref into a string complete
+with escaping and '&' separators, supports elements that are arrayrefs
+and hashrefs
+
+=item *
+
+str2hash($string) : convert string to hash using unescaping and
+splitting on '=' and '&', supports elements that are arrayrefs and
+hashrefs
 
 =item *
 
-str2hash($string) : convert string to hash using unescaping and splitting on
-'=' and '&'
+str2array($string) : convert string to hash using unescaping and
+splitting on '&', supports elements that are arrayrefs and hashrefs
 
 =item *