Annotation of loncom/lonnet/perl/lonnet.pm, revision 1.289

1.1       albertel    1: # The LearningOnline Network
                      2: # TCP networking package
1.12      www         3: #
1.289   ! bowersj2    4: # $Id: lonnet.pm,v 1.288 2002/10/01 07:59:33 albertel Exp $
1.178     www         5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
1.169     harris41   28: # 6/1/99,6/2,6/10,6/11,6/12,6/14,6/26,6/28,6/29,6/30,
                     29: # 7/1,7/2,7/9,7/10,7/12,7/14,7/15,7/19,
                     30: # 11/8,11/16,11/18,11/22,11/23,12/22,
                     31: # 01/06,01/13,02/24,02/28,02/29,
                     32: # 03/01,03/02,03/06,03/07,03/13,
                     33: # 04/05,05/29,05/31,06/01,
                     34: # 06/05,06/26 Gerd Kortemeyer
                     35: # 06/26 Ben Tyszka
                     36: # 06/30,07/15,07/17,07/18,07/20,07/21,07/22,07/25 Gerd Kortemeyer
                     37: # 08/14 Ben Tyszka
                     38: # 08/22,08/28,08/31,09/01,09/02,09/04,09/05,09/25,09/28,09/30 Gerd Kortemeyer
                     39: # 10/04 Gerd Kortemeyer
                     40: # 10/04 Guy Albertelli
                     41: # 10/06,10/09,10/10,10/11,10/14,10/20,10/23,10/25,10/26,10/27,10/28,10/29, 
                     42: # 10/30,10/31,
                     43: # 11/2,11/14,11/15,11/16,11/20,11/21,11/22,11/25,11/27,
                     44: # 12/02,12/12,12/13,12/14,12/28,12/29 Gerd Kortemeyer
                     45: # 05/01/01 Guy Albertelli
                     46: # 05/01,06/01,09/01 Gerd Kortemeyer
                     47: # 09/01 Guy Albertelli
                     48: # 09/01,10/01,11/01 Gerd Kortemeyer
                     49: # YEAR=2001
                     50: # 02/27/01 Scott Harrison
                     51: # 3/2 Gerd Kortemeyer
                     52: # 3/15,3/19 Scott Harrison
                     53: # 3/19,3/20 Gerd Kortemeyer
                     54: # 3/22,3/27,4/2,4/16,4/17 Scott Harrison
                     55: # 5/26,5/28 Gerd Kortemeyer
                     56: # 5/30 H. K. Ng
                     57: # 6/1 Gerd Kortemeyer
                     58: # July Guy Albertelli
                     59: # 8/4,8/7,8/8,8/9,8/11,8/16,8/17,8/18,8/20,8/23,9/20,9/21,9/26,
                     60: # 10/2 Gerd Kortemeyer
                     61: # 10/5,10/10,11/13,11/15 Scott Harrison
1.179     www        62: # 11/17,11/20,11/22,11/29 Gerd Kortemeyer
1.182     matthew    63: # 12/5 Matthew Hall
1.184     www        64: # 12/5 Guy Albertelli
1.190     www        65: # 12/6,12/7,12/12 Gerd Kortemeyer
1.191     harris41   66: # 12/18 Scott Harrison
1.195     www        67: # 12/21,12/22,12/27,12/28 Gerd Kortemeyer
1.196     www        68: # YEAR=2002
1.200     www        69: # 1/4,2/4,2/7 Gerd Kortemeyer
1.171     www        70: #
1.169     harris41   71: ###
                     72: 
1.1       albertel   73: package Apache::lonnet;
                     74: 
                     75: use strict;
                     76: use Apache::File;
1.8       www        77: use LWP::UserAgent();
1.15      www        78: use HTTP::Headers;
1.11      www        79: use vars 
1.231     stredwic   80: qw(%perlvar %hostname %homecache %badServerCache %hostip %spareid %hostdom 
1.192     www        81:    %libserv %pr %prp %metacache %packagetab 
1.188     www        82:    %courselogs %accesshash $processmarker $dumpcount 
1.245     www        83:    %coursedombuf %coursehombuf %courseresdatacache %domaindescription);
1.1       albertel   84: use IO::Socket;
1.31      www        85: use GDBM_File;
1.8       www        86: use Apache::Constants qw(:common :http);
1.208     albertel   87: use HTML::LCParser;
1.88      www        88: use Fcntl qw(:flock);
1.195     www        89: my $readit;
1.1       albertel   90: 
                     91: # --------------------------------------------------------------------- Logging
                     92: 
1.163     harris41   93: sub logtouch {
                     94:     my $execdir=$perlvar{'lonDaemons'};
                     95:     unless (-e "$execdir/logs/lonnet.log") {
                     96: 	my $fh=Apache::File->new(">>$execdir/logs/lonnet.log");
                     97: 	close $fh;
                     98:     }
                     99:     my ($wwwuid,$wwwgid)=(getpwnam('www'))[2,3];
                    100:     chown($wwwuid,$wwwgid,$execdir.'/logs/lonnet.log');
                    101: }
                    102: 
1.1       albertel  103: sub logthis {
                    104:     my $message=shift;
                    105:     my $execdir=$perlvar{'lonDaemons'};
                    106:     my $now=time;
                    107:     my $local=localtime($now);
1.162     harris41  108:     my $fh=Apache::File->new(">>$execdir/logs/lonnet.log");
                    109:     print $fh "$local ($$): $message\n";
1.1       albertel  110:     return 1;
                    111: }
                    112: 
                    113: sub logperm {
                    114:     my $message=shift;
                    115:     my $execdir=$perlvar{'lonDaemons'};
                    116:     my $now=time;
                    117:     my $local=localtime($now);
1.162     harris41  118:     my $fh=Apache::File->new(">>$execdir/logs/lonnet.perm.log");
                    119:     print $fh "$now:$message:$local\n";
1.1       albertel  120:     return 1;
                    121: }
                    122: 
                    123: # -------------------------------------------------- Non-critical communication
                    124: sub subreply {
                    125:     my ($cmd,$server)=@_;
                    126:     my $peerfile="$perlvar{'lonSockDir'}/$server";
                    127:     my $client=IO::Socket::UNIX->new(Peer    =>"$peerfile",
                    128:                                      Type    => SOCK_STREAM,
                    129:                                      Timeout => 10)
                    130:        or return "con_lost";
                    131:     print $client "$cmd\n";
                    132:     my $answer=<$client>;
1.9       www       133:     if (!$answer) { $answer="con_lost"; }
1.1       albertel  134:     chomp($answer);
                    135:     return $answer;
                    136: }
                    137: 
                    138: sub reply {
                    139:     my ($cmd,$server)=@_;
1.205     www       140:     unless (defined($hostname{$server})) { return 'no_such_host'; }
1.1       albertel  141:     my $answer=subreply($cmd,$server);
1.203     www       142:     if ($answer eq 'con_lost') {
1.233     albertel  143:        #sleep 5; 
                    144:        #$answer=subreply($cmd,$server);
                    145:        #if ($answer eq 'con_lost') {
                    146: 	#   &logthis("Second attempt con_lost on $server");
                    147:         #   my $peerfile="$perlvar{'lonSockDir'}/$server";
                    148:         #   my $client=IO::Socket::UNIX->new(Peer    =>"$peerfile",
                    149:         #                                    Type    => SOCK_STREAM,
                    150:         #                                    Timeout => 10)
                    151:         #              or return "con_lost";
                    152:         #   &logthis("Killing socket");
                    153:         #   print $client "close_connection_exit\n";
                    154:            #sleep 5;
                    155:         #   $answer=subreply($cmd,$server);       
                    156:        #}   
1.203     www       157:     }
1.65      www       158:     if (($answer=~/^refused/) || ($answer=~/^rejected/)) {
1.12      www       159:        &logthis("<font color=blue>WARNING:".
                    160:                 " $cmd to $server returned $answer</font>");
                    161:     }
1.1       albertel  162:     return $answer;
                    163: }
                    164: 
                    165: # ----------------------------------------------------------- Send USR1 to lonc
                    166: 
                    167: sub reconlonc {
                    168:     my $peerfile=shift;
                    169:     &logthis("Trying to reconnect for $peerfile");
                    170:     my $loncfile="$perlvar{'lonDaemons'}/logs/lonc.pid";
                    171:     if (my $fh=Apache::File->new("$loncfile")) {
                    172: 	my $loncpid=<$fh>;
                    173:         chomp($loncpid);
                    174:         if (kill 0 => $loncpid) {
                    175: 	    &logthis("lonc at pid $loncpid responding, sending USR1");
                    176:             kill USR1 => $loncpid;
                    177:             sleep 1;
                    178:             if (-e "$peerfile") { return; }
                    179:             &logthis("$peerfile still not there, give it another try");
                    180:             sleep 5;
                    181:             if (-e "$peerfile") { return; }
1.12      www       182:             &logthis(
                    183:   "<font color=blue>WARNING: $peerfile still not there, giving up</font>");
1.1       albertel  184:         } else {
1.12      www       185: 	    &logthis(
                    186:                "<font color=blue>WARNING:".
                    187:                " lonc at pid $loncpid not responding, giving up</font>");
1.1       albertel  188:         }
                    189:     } else {
1.12      www       190:      &logthis('<font color=blue>WARNING: lonc not running, giving up</font>');
1.1       albertel  191:     }
                    192: }
                    193: 
                    194: # ------------------------------------------------------ Critical communication
1.12      www       195: 
1.1       albertel  196: sub critical {
                    197:     my ($cmd,$server)=@_;
1.89      www       198:     unless ($hostname{$server}) {
                    199:         &logthis("<font color=blue>WARNING:".
                    200:                " Critical message to unknown server ($server)</font>");
                    201:         return 'no_such_host';
                    202:     }
1.1       albertel  203:     my $answer=reply($cmd,$server);
                    204:     if ($answer eq 'con_lost') {
                    205:         my $pingreply=reply('ping',$server);
                    206: 	&reconlonc("$perlvar{'lonSockDir'}/$server");
                    207:         my $pongreply=reply('pong',$server);
                    208:         &logthis("Ping/Pong for $server: $pingreply/$pongreply");
                    209:         $answer=reply($cmd,$server);
                    210:         if ($answer eq 'con_lost') {
                    211:             my $now=time;
                    212:             my $middlename=$cmd;
1.5       www       213:             $middlename=substr($middlename,0,16);
1.1       albertel  214:             $middlename=~s/\W//g;
                    215:             my $dfilename=
                    216:              "$perlvar{'lonSockDir'}/delayed/$now.$middlename.$server";
                    217:             {
                    218:              my $dfh;
                    219:              if ($dfh=Apache::File->new(">$dfilename")) {
1.7       www       220:                 print $dfh "$cmd\n";
1.1       albertel  221: 	     }
                    222:             }
                    223:             sleep 2;
                    224:             my $wcmd='';
                    225:             {
                    226: 	     my $dfh;
                    227:              if ($dfh=Apache::File->new("$dfilename")) {
                    228:                 $wcmd=<$dfh>;
                    229: 	     }
                    230:             }
                    231:             chomp($wcmd);
1.7       www       232:             if ($wcmd eq $cmd) {
1.12      www       233: 		&logthis("<font color=blue>WARNING: ".
                    234:                          "Connection buffer $dfilename: $cmd</font>");
1.1       albertel  235:                 &logperm("D:$server:$cmd");
                    236: 	        return 'con_delayed';
                    237:             } else {
1.12      www       238:                 &logthis("<font color=red>CRITICAL:"
                    239:                         ." Critical connection failed: $server $cmd</font>");
1.1       albertel  240:                 &logperm("F:$server:$cmd");
                    241:                 return 'con_failed';
                    242:             }
                    243:         }
                    244:     }
                    245:     return $answer;
                    246: }
                    247: 
1.5       www       248: # ---------------------------------------------------------- Append Environment
                    249: 
                    250: sub appenv {
1.6       www       251:     my %newenv=@_;
1.191     harris41  252:     foreach (keys %newenv) {
1.35      www       253: 	if (($newenv{$_}=~/^user\.role/) || ($newenv{$_}=~/^user\.priv/)) {
                    254:             &logthis("<font color=blue>WARNING: ".
1.151     www       255:                 "Attempt to modify environment ".$_." to ".$newenv{$_}
                    256:                 .'</font>');
1.35      www       257: 	    delete($newenv{$_});
                    258:         } else {
                    259:             $ENV{$_}=$newenv{$_};
                    260:         }
1.191     harris41  261:     }
1.95      www       262: 
                    263:     my $lockfh;
                    264:     unless ($lockfh=Apache::File->new("$ENV{'user.environment'}")) {
1.97      www       265:        return 'error: '.$!;
1.95      www       266:     }
                    267:     unless (flock($lockfh,LOCK_EX)) {
                    268:          &logthis("<font color=blue>WARNING: ".
                    269:                   'Could not obtain exclusive lock in appenv: '.$!);
                    270:          $lockfh->close();
                    271:          return 'error: '.$!;
                    272:     }
                    273: 
1.6       www       274:     my @oldenv;
                    275:     {
                    276:      my $fh;
                    277:      unless ($fh=Apache::File->new("$ENV{'user.environment'}")) {
1.97      www       278: 	return 'error: '.$!;
1.6       www       279:      }
                    280:      @oldenv=<$fh>;
1.89      www       281:      $fh->close();
1.6       www       282:     }
                    283:     for (my $i=0; $i<=$#oldenv; $i++) {
                    284:         chomp($oldenv[$i]);
1.9       www       285:         if ($oldenv[$i] ne '') {
                    286:            my ($name,$value)=split(/=/,$oldenv[$i]);
1.24      www       287:            unless (defined($newenv{$name})) {
                    288: 	      $newenv{$name}=$value;
                    289: 	   }
1.9       www       290:         }
1.6       www       291:     }
                    292:     {
                    293:      my $fh;
                    294:      unless ($fh=Apache::File->new(">$ENV{'user.environment'}")) {
                    295: 	return 'error';
                    296:      }
                    297:      my $newname;
1.93      www       298:      foreach $newname (keys %newenv) {
1.6       www       299: 	 print $fh "$newname=$newenv{$newname}\n";
                    300:      }
1.86      albertel  301:      $fh->close();
1.56      www       302:     }
1.95      www       303: 
                    304:     $lockfh->close();
1.56      www       305:     return 'ok';
                    306: }
                    307: # ----------------------------------------------------- Delete from Environment
                    308: 
                    309: sub delenv {
                    310:     my $delthis=shift;
                    311:     my %newenv=();
                    312:     if (($delthis=~/user\.role/) || ($delthis=~/user\.priv/)) {
                    313:         &logthis("<font color=blue>WARNING: ".
                    314:                 "Attempt to delete from environment ".$delthis);
                    315:         return 'error';
                    316:     }
                    317:     my @oldenv;
                    318:     {
                    319:      my $fh;
                    320:      unless ($fh=Apache::File->new("$ENV{'user.environment'}")) {
                    321: 	return 'error';
                    322:      }
1.89      www       323:      unless (flock($fh,LOCK_SH)) {
                    324:          &logthis("<font color=blue>WARNING: ".
                    325:                   'Could not obtain shared lock in delenv: '.$!);
                    326:          $fh->close();
                    327:          return 'error: '.$!;
                    328:      }
1.56      www       329:      @oldenv=<$fh>;
1.89      www       330:      $fh->close();
1.56      www       331:     }
                    332:     {
                    333:      my $fh;
                    334:      unless ($fh=Apache::File->new(">$ENV{'user.environment'}")) {
                    335: 	return 'error';
                    336:      }
1.89      www       337:      unless (flock($fh,LOCK_EX)) {
                    338:          &logthis("<font color=blue>WARNING: ".
                    339:                   'Could not obtain exclusive lock in delenv: '.$!);
                    340:          $fh->close();
                    341:          return 'error: '.$!;
                    342:      }
1.191     harris41  343:      foreach (@oldenv) {
1.56      www       344: 	 unless ($_=~/^$delthis/) { print $fh $_; }
1.191     harris41  345:      }
1.87      www       346:      $fh->close();
1.5       www       347:     }
                    348:     return 'ok';
1.283     www       349: }
                    350: 
                    351: # ------------------------------------------ Fight off request when overloaded
                    352: 
                    353: sub overloaderror {
                    354:     my ($r,$checkserver)=@_;
                    355:     unless ($checkserver) { $checkserver=$perlvar{'lonHostID'}; }
                    356:     my $loadavg;
                    357:     if ($checkserver eq $perlvar{'lonHostID'}) {
                    358:        my $loadfile=Apache::File->new('/proc/loadavg');
                    359:        $loadavg=<$loadfile>;
                    360:        $loadavg =~ s/\s.*//g;
1.285     matthew   361:        $loadavg = 100*$loadavg/$perlvar{'lonLoadLim'};
1.283     www       362:     } else {
                    363:        $loadavg=&reply('load',$checkserver);
                    364:     }
1.285     matthew   365:     my $overload=$loadavg-100;
1.283     www       366:     if ($overload>0) {
1.285     matthew   367: 	$r->err_headers_out->{'Retry-After'}=$overload;
1.283     www       368:         $r->log_error('Overload of '.$overload.' on '.$checkserver);
                    369:         return 413;
                    370:     }    
                    371:     return '';
1.5       www       372: }
1.1       albertel  373: 
                    374: # ------------------------------ Find server with least workload from spare.tab
1.11      www       375: 
1.1       albertel  376: sub spareserver {
1.284     matthew   377:     my $loadpercent = shift;
1.1       albertel  378:     my $tryserver;
                    379:     my $spareserver='';
1.284     matthew   380:     my $lowestserver=$loadpercent; 
1.1       albertel  381:     foreach $tryserver (keys %spareid) {
                    382:        my $answer=reply('load',$tryserver);
                    383:        if (($answer =~ /\d/) && ($answer<$lowestserver)) {
                    384: 	   $spareserver="http://$hostname{$tryserver}";
                    385:            $lowestserver=$answer;
                    386:        }
                    387:     }    
                    388:     return $spareserver;
1.202     matthew   389: }
                    390: 
                    391: # --------------------------------------------- Try to change a user's password
                    392: 
                    393: sub changepass {
                    394:     my ($uname,$udom,$currentpass,$newpass,$server)=@_;
                    395:     $currentpass = &escape($currentpass);
                    396:     $newpass     = &escape($newpass);
                    397:     my $answer = reply("encrypt:passwd:$udom:$uname:$currentpass:$newpass",
                    398: 		       $server);
                    399:     if (! $answer) {
                    400: 	&logthis("No reply on password change request to $server ".
                    401: 		 "by $uname in domain $udom.");
                    402:     } elsif ($answer =~ "^ok") {
                    403:         &logthis("$uname in $udom successfully changed their password ".
                    404: 		 "on $server.");
                    405:     } elsif ($answer =~ "^pwchange_failure") {
                    406: 	&logthis("$uname in $udom was unable to change their password ".
                    407: 		 "on $server.  The action was blocked by either lcpasswd ".
                    408: 		 "or pwchange");
                    409:     } elsif ($answer =~ "^non_authorized") {
                    410:         &logthis("$uname in $udom did not get their password correct when ".
                    411: 		 "attempting to change it on $server.");
                    412:     } elsif ($answer =~ "^auth_mode_error") {
                    413:         &logthis("$uname in $udom attempted to change their password despite ".
                    414: 		 "not being locally or internally authenticated on $server.");
                    415:     } elsif ($answer =~ "^unknown_user") {
                    416:         &logthis("$uname in $udom attempted to change their password ".
                    417: 		 "on $server but were unable to because $server is not ".
                    418: 		 "their home server.");
                    419:     } elsif ($answer =~ "^refused") {
                    420: 	&logthis("$server refused to change $uname in $udom password because ".
                    421: 		 "it was sent an unencrypted request to change the password.");
                    422:     }
                    423:     return $answer;
1.1       albertel  424: }
                    425: 
1.169     harris41  426: # ----------------------- Try to determine user's current authentication scheme
                    427: 
                    428: sub queryauthenticate {
                    429:     my ($uname,$udom)=@_;
                    430:     if (($perlvar{'lonRole'} eq 'library') && 
                    431:         ($udom eq $perlvar{'lonDefDomain'})) {
                    432: 	my $answer=reply("encrypt:currentauth:$udom:$uname",
                    433: 			 $perlvar{'lonHostID'});
                    434: 	unless ($answer eq 'unknown_user' or $answer eq 'refused') {
                    435: 	    if (length($answer)) {
                    436: 		return $answer;
                    437: 	    }
                    438: 	    else {
                    439: 	&logthis("User $uname at $udom lacks an authentication mechanism");
                    440: 		return 'no_host';
                    441: 	    }
                    442: 	}
                    443:     }
                    444: 
                    445:     my $tryserver;
                    446:     foreach $tryserver (keys %libserv) {
                    447: 	if ($hostdom{$tryserver} eq $udom) {
                    448:            my $answer=reply("encrypt:currentauth:$udom:$uname",$tryserver);
                    449: 	   unless ($answer eq 'unknown_user' or $answer eq 'refused') {
                    450: 	       if (length($answer)) {
                    451: 		   return $answer;
                    452: 	       }
                    453: 	       else {
                    454: 	   &logthis("User $uname at $udom lacks an authentication mechanism");
                    455: 		   return 'no_host';
                    456: 	       }
                    457: 	   }
                    458:        }
                    459:     }
                    460:     &logthis("User $uname at $udom lacks an authentication mechanism");    
                    461:     return 'no_host';
                    462: }
                    463: 
1.1       albertel  464: # --------- Try to authenticate user from domain's lib servers (first this one)
1.11      www       465: 
1.1       albertel  466: sub authenticate {
                    467:     my ($uname,$upass,$udom)=@_;
1.12      www       468:     $upass=escape($upass);
1.199     www       469:     $uname=~s/\W//g;
1.1       albertel  470:     if (($perlvar{'lonRole'} eq 'library') && 
                    471:         ($udom eq $perlvar{'lonDefDomain'})) {
1.3       www       472:     my $answer=reply("encrypt:auth:$udom:$uname:$upass",$perlvar{'lonHostID'});
1.2       www       473:         if ($answer =~ /authorized/) {
1.9       www       474:               if ($answer eq 'authorized') {
                    475:                  &logthis("User $uname at $udom authorized by local server"); 
                    476:                  return $perlvar{'lonHostID'}; 
                    477:               }
                    478:               if ($answer eq 'non_authorized') {
                    479:                  &logthis("User $uname at $udom rejected by local server"); 
                    480:                  return 'no_host'; 
                    481:               }
1.2       www       482: 	}
1.1       albertel  483:     }
                    484: 
                    485:     my $tryserver;
                    486:     foreach $tryserver (keys %libserv) {
                    487: 	if ($hostdom{$tryserver} eq $udom) {
1.10      www       488:            my $answer=reply("encrypt:auth:$udom:$uname:$upass",$tryserver);
1.1       albertel  489:            if ($answer =~ /authorized/) {
1.9       www       490:               if ($answer eq 'authorized') {
                    491:                  &logthis("User $uname at $udom authorized by $tryserver"); 
                    492:                  return $tryserver; 
                    493:               }
                    494:               if ($answer eq 'non_authorized') {
                    495:                  &logthis("User $uname at $udom rejected by $tryserver");
                    496:                  return 'no_host';
                    497:               } 
1.1       albertel  498: 	   }
                    499:        }
1.9       www       500:     }
                    501:     &logthis("User $uname at $udom could not be authenticated");    
1.1       albertel  502:     return 'no_host';
                    503: }
                    504: 
                    505: # ---------------------- Find the homebase for a user from domain's lib servers
1.11      www       506: 
1.1       albertel  507: sub homeserver {
1.230     stredwic  508:     my ($uname,$udom,$ignoreBadCache)=@_;
1.1       albertel  509:     my $index="$uname:$udom";
1.221     matthew   510:     if ($homecache{$index}) { 
                    511:         return "$homecache{$index}"; 
                    512:     }
1.1       albertel  513:     my $tryserver;
                    514:     foreach $tryserver (keys %libserv) {
1.230     stredwic  515:         next if ($ignoreBadCache ne 'true' && 
1.231     stredwic  516: 		 exists($badServerCache{$tryserver}));
1.1       albertel  517: 	if ($hostdom{$tryserver} eq $udom) {
                    518:            my $answer=reply("home:$udom:$uname",$tryserver);
                    519:            if ($answer eq 'found') { 
1.221     matthew   520:               $homecache{$index}=$tryserver;
1.1       albertel  521:               return $tryserver; 
1.231     stredwic  522:            } elsif ($answer eq 'no_host') {
                    523: 	       $badServerCache{$tryserver}=1;
1.221     matthew   524:            }
1.1       albertel  525:        }
                    526:     }    
                    527:     return 'no_host';
1.70      www       528: }
                    529: 
                    530: # ------------------------------------- Find the usernames behind a list of IDs
                    531: 
                    532: sub idget {
                    533:     my ($udom,@ids)=@_;
                    534:     my %returnhash=();
                    535:     
                    536:     my $tryserver;
                    537:     foreach $tryserver (keys %libserv) {
                    538:        if ($hostdom{$tryserver} eq $udom) {
                    539: 	  my $idlist=join('&',@ids);
                    540:           $idlist=~tr/A-Z/a-z/; 
                    541: 	  my $reply=&reply("idget:$udom:".$idlist,$tryserver);
                    542:           my @answer=();
1.76      www       543:           if (($reply ne 'con_lost') && ($reply!~/^error\:/)) {
1.70      www       544: 	      @answer=split(/\&/,$reply);
                    545:           }                    ;
                    546:           my $i;
                    547:           for ($i=0;$i<=$#ids;$i++) {
                    548:               if ($answer[$i]) {
                    549: 		  $returnhash{$ids[$i]}=$answer[$i];
                    550:               } 
                    551:           }
                    552:        }
                    553:     }    
                    554:     return %returnhash;
                    555: }
                    556: 
                    557: # ------------------------------------- Find the IDs behind a list of usernames
                    558: 
                    559: sub idrget {
                    560:     my ($udom,@unames)=@_;
                    561:     my %returnhash=();
1.191     harris41  562:     foreach (@unames) {
1.70      www       563:         $returnhash{$_}=(&userenvironment($udom,$_,'id'))[1];
1.191     harris41  564:     }
1.70      www       565:     return %returnhash;
                    566: }
                    567: 
                    568: # ------------------------------- Store away a list of names and associated IDs
                    569: 
                    570: sub idput {
                    571:     my ($udom,%ids)=@_;
                    572:     my %servers=();
1.191     harris41  573:     foreach (keys %ids) {
1.70      www       574:         my $uhom=&homeserver($_,$udom);
                    575:         if ($uhom ne 'no_host') {
                    576:             my $id=&escape($ids{$_});
                    577:             $id=~tr/A-Z/a-z/;
                    578:             my $unam=&escape($_);
                    579: 	    if ($servers{$uhom}) {
                    580: 		$servers{$uhom}.='&'.$id.'='.$unam;
                    581:             } else {
                    582:                 $servers{$uhom}=$id.'='.$unam;
                    583:             }
                    584:             &critical('put:'.$udom.':'.$unam.':environment:id='.$id,$uhom);
                    585:         }
1.191     harris41  586:     }
                    587:     foreach (keys %servers) {
1.70      www       588:         &critical('idput:'.$udom.':'.$servers{$_},$_);
1.191     harris41  589:     }
1.70      www       590: }
                    591: 
                    592: # ------------------------------------- Find the section of student in a course
                    593: 
                    594: sub usection {
                    595:     my ($udom,$unam,$courseid)=@_;
                    596:     $courseid=~s/\_/\//g;
                    597:     $courseid=~s/^(\w)/\/$1/;
1.191     harris41  598:     foreach (split(/\&/,&reply('dump:'.$udom.':'.$unam.':roles',
                    599:                         &homeserver($unam,$udom)))) {
1.70      www       600:         my ($key,$value)=split(/\=/,$_);
                    601:         $key=&unescape($key);
                    602:         if ($key=~/^$courseid(?:\/)*(\w+)*\_st$/) {
                    603:             my $section=$1;
                    604:             if ($key eq $courseid.'_st') { $section=''; }
                    605: 	    my ($dummy,$end,$start)=split(/\_/,&unescape($value));
                    606:             my $now=time;
                    607:             my $notactive=0;
                    608:             if ($start) {
                    609: 		if ($now<$start) { $notactive=1; }
                    610:             }
                    611:             if ($end) {
                    612:                 if ($now>$end) { $notactive=1; }
                    613:             } 
                    614:             unless ($notactive) { return $section; }
                    615:         }
1.191     harris41  616:     }
1.70      www       617:     return '-1';
                    618: }
                    619: 
                    620: # ------------------------------------- Read an entry from a user's environment
                    621: 
                    622: sub userenvironment {
                    623:     my ($udom,$unam,@what)=@_;
                    624:     my %returnhash=();
                    625:     my @answer=split(/\&/,
                    626:                 &reply('get:'.$udom.':'.$unam.':environment:'.join('&',@what),
                    627:                       &homeserver($unam,$udom)));
                    628:     my $i;
                    629:     for ($i=0;$i<=$#what;$i++) {
                    630: 	$returnhash{$what[$i]}=&unescape($answer[$i]);
                    631:     }
                    632:     return %returnhash;
1.1       albertel  633: }
                    634: 
1.263     www       635: # -------------------------------------------------------------------- New chat
                    636: 
                    637: sub chatsend {
                    638:     my ($newentry,$anon)=@_;
                    639:     my $cnum=$ENV{'course.'.$ENV{'request.course.id'}.'.num'};
                    640:     my $cdom=$ENV{'course.'.$ENV{'request.course.id'}.'.domain'};
                    641:     my $chome=$ENV{'course.'.$ENV{'request.course.id'}.'.home'};
                    642:     &reply('chatsend:'.$cdom.':'.$cnum.':'.
                    643: 	   &escape($ENV{'user.domain'}.':'.$ENV{'user.name'}.':'.$anon.':'.
                    644: 		   &escape($newentry)),$chome);
                    645: }
                    646: 
1.1       albertel  647: # ----------------------------- Subscribe to a resource, return URL if possible
1.11      www       648: 
1.1       albertel  649: sub subscribe {
                    650:     my $fname=shift;
                    651:     my $author=$fname;
                    652:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
                    653:     my ($udom,$uname)=split(/\//,$author);
                    654:     my $home=homeserver($uname,$udom);
                    655:     if (($home eq 'no_host') || ($home eq $perlvar{'lonHostID'})) { 
                    656:         return 'not_found'; 
                    657:     }
                    658:     my $answer=reply("sub:$fname",$home);
1.64      www       659:     if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
                    660: 	$answer.=' by '.$home;
                    661:     }
1.1       albertel  662:     return $answer;
                    663: }
                    664:     
1.8       www       665: # -------------------------------------------------------------- Replicate file
                    666: 
                    667: sub repcopy {
                    668:     my $filename=shift;
1.23      www       669:     $filename=~s/\/+/\//g;
1.214     www       670:     if ($filename=~/^\/home\/httpd\/html\/adm\//) { return OK; }
1.8       www       671:     my $transname="$filename.in.transfer";
1.17      www       672:     if ((-e $filename) || (-e $transname)) { return OK; }
1.8       www       673:     my $remoteurl=subscribe($filename);
1.64      www       674:     if ($remoteurl =~ /^con_lost by/) {
                    675: 	   &logthis("Subscribe returned $remoteurl: $filename");
1.8       www       676:            return HTTP_SERVICE_UNAVAILABLE;
                    677:     } elsif ($remoteurl eq 'not_found') {
                    678: 	   &logthis("Subscribe returned not_found: $filename");
                    679: 	   return HTTP_NOT_FOUND;
1.64      www       680:     } elsif ($remoteurl =~ /^rejected by/) {
                    681: 	   &logthis("Subscribe returned $remoteurl: $filename");
1.8       www       682:            return FORBIDDEN;
1.20      www       683:     } elsif ($remoteurl eq 'directory') {
                    684:            return OK;
1.8       www       685:     } else {
                    686:            my @parts=split(/\//,$filename);
                    687:            my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
                    688:            if ($path ne "$perlvar{'lonDocRoot'}/res") {
                    689:                &logthis("Malconfiguration for replication: $filename");
                    690: 	       return HTTP_BAD_REQUEST;
                    691:            }
                    692:            my $count;
                    693:            for ($count=5;$count<$#parts;$count++) {
                    694:                $path.="/$parts[$count]";
                    695:                if ((-e $path)!=1) {
                    696: 		   mkdir($path,0777);
                    697:                }
                    698:            }
                    699:            my $ua=new LWP::UserAgent;
                    700:            my $request=new HTTP::Request('GET',"$remoteurl");
                    701:            my $response=$ua->request($request,$transname);
                    702:            if ($response->is_error()) {
                    703: 	       unlink($transname);
                    704:                my $message=$response->status_line;
1.12      www       705:                &logthis("<font color=blue>WARNING:"
                    706:                        ." LWP get: $message: $filename</font>");
1.8       www       707:                return HTTP_SERVICE_UNAVAILABLE;
                    708:            } else {
1.16      www       709: 	       if ($remoteurl!~/\.meta$/) {
                    710:                   my $mrequest=new HTTP::Request('GET',$remoteurl.'.meta');
                    711:                   my $mresponse=$ua->request($mrequest,$filename.'.meta');
                    712:                   if ($mresponse->is_error()) {
                    713: 		      unlink($filename.'.meta');
                    714:                       &logthis(
                    715:                      "<font color=yellow>INFO: No metadata: $filename</font>");
                    716:                   }
                    717: 	       }
1.8       www       718:                rename($transname,$filename);
                    719:                return OK;
                    720:            }
                    721:     }
                    722: }
                    723: 
1.15      www       724: # --------------------------------------------------------- Server Side Include
                    725: 
                    726: sub ssi {
                    727: 
1.23      www       728:     my ($fn,%form)=@_;
1.15      www       729: 
                    730:     my $ua=new LWP::UserAgent;
1.23      www       731:     
                    732:     my $request;
                    733:     
                    734:     if (%form) {
                    735:       $request=new HTTP::Request('POST',"http://".$ENV{'HTTP_HOST'}.$fn);
1.201     albertel  736:       $request->content(join('&',map { &escape($_).'='.&escape($form{$_}) } keys %form));
1.23      www       737:     } else {
                    738:       $request=new HTTP::Request('GET',"http://".$ENV{'HTTP_HOST'}.$fn);
                    739:     }
                    740: 
1.15      www       741:     $request->header(Cookie => $ENV{'HTTP_COOKIE'});
                    742:     my $response=$ua->request($request);
                    743: 
                    744:     return $response->content;
                    745: }
1.254     www       746: 
                    747: # ------- Add a token to a remote URI's query string to vouch for access rights
                    748: 
                    749: sub tokenwrapper {
                    750:     my $uri=shift;
1.259     www       751:     $uri=~s/^http\:\/\/([^\/]+)//;
                    752:     $uri=~s/^\///;
                    753:     $ENV{'user.environment'}=~/\/([^\/]+)\.id/;
                    754:     my $token=$1;
                    755:     if ($uri=~/^uploaded\/([^\/]+)\/([^\/]+)\/([^\/]+)(\?\.*)*$/) {
                    756: 	&appenv('userfile.'.$1.'/'.$2.'/'.$3 => $ENV{'request.course.id'});
                    757:         return 'http://'.$hostname{ &homeserver($2,$1)}.'/'.$uri.
                    758:                (($uri=~/\?/)?'&':'?').'token='.$token;
                    759:     } else {
                    760: 	return '/adm/notfound.html';
                    761:     }
1.254     www       762: }
                    763:     
1.257     www       764: # --------------- Take an uploaded file and put it into the userfiles directory
1.259     www       765: # input: name of form element, coursedoc=1 means this is for the course
1.257     www       766: # output: url of file in userspace
                    767: 
                    768: sub userfileupload {
1.259     www       769:     my ($formname,$coursedoc)=@_;
1.257     www       770:     my $fname=$ENV{'form.'.$formname.'.filename'};
                    771:     $fname=~s/\\/\//g;
                    772:     $fname=~s/^.*\/([^\/]+)$/$1/;
                    773:     unless ($fname) { return 'error: no uploaded file'; }
                    774:     chop($ENV{'form.'.$formname});
1.258     www       775: # Create the directory if not present
1.259     www       776:     my $docuname='';
                    777:     my $docudom='';
                    778:     my $docuhome='';
                    779:     if ($coursedoc) {
                    780: 	$docuname=$ENV{'course.'.$ENV{'request.course.id'}.'.num'};
                    781: 	$docudom=$ENV{'course.'.$ENV{'request.course.id'}.'.domain'};
                    782: 	$docuhome=$ENV{'course.'.$ENV{'request.course.id'}.'.home'};
                    783:     } else {
                    784:         $docuname=$ENV{'user.name'};
                    785:         $docudom=$ENV{'user.domain'};
                    786:         $docuhome=$ENV{'user.home'};
                    787:     }
1.271     www       788:     return 
                    789:         &finishuserfileupload($docuname,$docudom,$docuhome,$formname,$fname);
                    790: }
                    791: 
                    792: sub finishuserfileupload {
                    793:     my ($docuname,$docudom,$docuhome,$formname,$fname)=@_;
1.259     www       794:     my $path=$docudom.'/'.$docuname.'/';
1.258     www       795:     my $filepath=$perlvar{'lonDocRoot'};
1.259     www       796:     my @parts=split(/\//,$filepath.'/userfiles/'.$path);
1.258     www       797:     my $count;
                    798:     for ($count=4;$count<=$#parts;$count++) {
                    799:         $filepath.="/$parts[$count]";
                    800:         if ((-e $filepath)!=1) {
                    801: 	    mkdir($filepath,0777);
                    802:         }
                    803:     }
                    804: # Save the file
                    805:     {
                    806:        my $fh=Apache::File->new('>'.$filepath.'/'.$fname);
                    807:        print $fh $ENV{'form.'.$formname};
                    808:     }
1.259     www       809: # Notify homeserver to grep it
                    810: #
1.263     www       811:     if 
                    812: (&reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$fname,$docuhome) eq 'ok') 
                    813:     {
1.259     www       814: #
1.258     www       815: # Return the URL to it
1.263     www       816:         return '/uploaded/'.$path.$fname;
                    817:     } else {
                    818:         return '/adm/notfound.html';
                    819:     }    
1.257     www       820: }
1.15      www       821: 
1.14      www       822: # ------------------------------------------------------------------------- Log
                    823: 
                    824: sub log {
                    825:     my ($dom,$nam,$hom,$what)=@_;
1.47      www       826:     return critical("log:$dom:$nam:$what",$hom);
1.157     www       827: }
                    828: 
                    829: # ------------------------------------------------------------------ Course Log
                    830: 
                    831: sub flushcourselogs {
                    832:     &logthis('Flushing course log buffers');
1.191     harris41  833:     foreach (keys %courselogs) {
1.157     www       834:         my $crsid=$_;
1.188     www       835:         if (&reply('log:'.$coursedombuf{$crsid}.':'.
                    836: 		          &escape($courselogs{$crsid}),
                    837: 		          $coursehombuf{$crsid}) eq 'ok') {
1.157     www       838: 	    delete $courselogs{$crsid};
                    839:         } else {
                    840:             &logthis('Failed to flush log buffer for '.$crsid);
                    841:             if (length($courselogs{$crsid})>40000) {
                    842:                &logthis("<font color=blue>WARNING: Buffer for ".$crsid.
                    843:                         " exceeded maximum size, deleting.</font>");
                    844:                delete $courselogs{$crsid};
                    845:             }
                    846:         }        
1.191     harris41  847:     }
1.185     www       848:     &logthis('Flushing access logs');
1.191     harris41  849:     foreach (keys %accesshash) {
1.185     www       850:         my $entry=$_;
                    851:         $entry=~/\_\_\_(\w+)\/(\w+)\/(.*)\_\_\_(\w+)$/;
                    852:         my %temphash=($entry => $accesshash{$entry});
1.266     albertel  853:         if (&Apache::lonnet::put('nohist_resevaldata',\%temphash,$1,$2) eq 'ok') {
1.185     www       854: 	    delete $accesshash{$entry};
                    855:         }
1.191     harris41  856:     }
1.186     www       857:     $dumpcount++;
1.157     www       858: }
                    859: 
                    860: sub courselog {
                    861:     my $what=shift;
1.158     www       862:     $what=time.':'.$what;
1.157     www       863:     unless ($ENV{'request.course.id'}) { return ''; }
1.188     www       864:     $coursedombuf{$ENV{'request.course.id'}}=
                    865:        $ENV{'course.'.$ENV{'request.course.id'}.'.domain'}.':'.
                    866:        $ENV{'course.'.$ENV{'request.course.id'}.'.num'};
                    867:     $coursehombuf{$ENV{'request.course.id'}}=
                    868:        $ENV{'course.'.$ENV{'request.course.id'}.'.home'};
1.157     www       869:     if (defined $courselogs{$ENV{'request.course.id'}}) {
                    870: 	$courselogs{$ENV{'request.course.id'}}.='&'.$what;
                    871:     } else {
                    872: 	$courselogs{$ENV{'request.course.id'}}.=$what;
                    873:     }
                    874:     if (length($courselogs{$ENV{'request.course.id'}})>4048) {
                    875: 	&flushcourselogs();
                    876:     }
1.158     www       877: }
                    878: 
                    879: sub courseacclog {
                    880:     my $fnsymb=shift;
                    881:     unless ($ENV{'request.course.id'}) { return ''; }
                    882:     my $what=$fnsymb.':'.$ENV{'user.name'}.':'.$ENV{'user.domain'};
1.192     www       883:     if ($fnsymb=~/(problem|exam|quiz|assess|survey|form)$/) {
1.187     www       884:         $what.=':POST';
1.191     harris41  885: 	foreach (keys %ENV) {
1.158     www       886:             if ($_=~/^form\.(.*)/) {
                    887: 		$what.=':'.$1.'='.$ENV{$_};
                    888:             }
1.191     harris41  889:         }
1.158     www       890:     }
                    891:     &courselog($what);
1.149     www       892: }
                    893: 
1.185     www       894: sub countacc {
                    895:     my $url=&declutter(shift);
                    896:     unless ($ENV{'request.course.id'}) { return ''; }
                    897:     $accesshash{$ENV{'request.course.id'}.'___'.$url.'___course'}=1;
1.281     www       898:     my $key=$$.$processmarker.'_'.$dumpcount.'___'.$url.'___count';
1.185     www       899:     if (defined($accesshash{$key})) {
                    900: 	$accesshash{$key}++;
                    901:     } else {
                    902:         $accesshash{$key}=1;
                    903:     }
                    904: }
                    905:     
1.149     www       906: # ----------------------------------------------------------- Check out an item
                    907: 
                    908: sub checkout {
                    909:     my ($symb,$tuname,$tudom,$tcrsid)=@_;
                    910:     my $now=time;
                    911:     my $lonhost=$perlvar{'lonHostID'};
                    912:     my $infostr=&escape(
1.234     www       913:                  'CHECKOUTTOKEN&'.
1.149     www       914:                  $tuname.'&'.
                    915:                  $tudom.'&'.
                    916:                  $tcrsid.'&'.
                    917:                  $symb.'&'.
                    918: 		 $now.'&'.$ENV{'REMOTE_ADDR'});
                    919:     my $token=&reply('tmpput:'.$infostr,$lonhost);
1.151     www       920:     if ($token=~/^error\:/) { 
                    921:         &logthis("<font color=blue>WARNING: ".
                    922:                 "Checkout tmpput failed ".$tudom.' - '.$tuname.' - '.$symb.
                    923:                  "</font>");
                    924:         return ''; 
                    925:     }
                    926: 
1.149     www       927:     $token=~s/^(\d+)\_.*\_(\d+)$/$1\*$2\*$lonhost/;
                    928:     $token=~tr/a-z/A-Z/;
                    929: 
1.153     www       930:     my %infohash=('resource.0.outtoken' => $token,
                    931:                   'resource.0.checkouttime' => $now,
                    932:                   'resource.0.outremote' => $ENV{'REMOTE_ADDR'});
1.149     www       933: 
                    934:     unless (&cstore(\%infohash,$symb,$tcrsid,$tudom,$tuname) eq 'ok') {
                    935:        return '';
1.151     www       936:     } else {
                    937:         &logthis("<font color=blue>WARNING: ".
                    938:                 "Checkout cstore failed ".$tudom.' - '.$tuname.' - '.$symb.
                    939:                  "</font>");
1.149     www       940:     }    
                    941: 
                    942:     if (&log($tudom,$tuname,&homeserver($tuname,$tudom),
                    943:                          &escape('Checkout '.$infostr.' - '.
                    944:                                                  $token)) ne 'ok') {
                    945: 	return '';
1.151     www       946:     } else {
                    947:         &logthis("<font color=blue>WARNING: ".
                    948:                 "Checkout log failed ".$tudom.' - '.$tuname.' - '.$symb.
                    949:                  "</font>");
1.149     www       950:     }
1.151     www       951:     return $token;
1.149     www       952: }
                    953: 
                    954: # ------------------------------------------------------------ Check in an item
                    955: 
                    956: sub checkin {
                    957:     my $token=shift;
1.150     www       958:     my $now=time;
                    959:     my ($ta,$tb,$lonhost)=split(/\*/,$token);
                    960:     $lonhost=~tr/A-Z/a-z/;
                    961:     my $dtoken=$ta.'_'.$hostip{$lonhost}.'_'.$tb;
                    962:     $dtoken=~s/\W/\_/g;
1.234     www       963:     my ($dummy,$tuname,$tudom,$tcrsid,$symb,$chtim,$rmaddr)=
1.150     www       964:                  split(/\&/,&unescape(&reply('tmpget:'.$dtoken,$lonhost)));
                    965: 
1.154     www       966:     unless (($tuname) && ($tudom)) {
                    967:         &logthis('Check in '.$token.' ('.$dtoken.') failed');
                    968:         return '';
                    969:     }
                    970:     
                    971:     unless (&allowed('mgr',$tcrsid)) {
                    972:         &logthis('Check in '.$token.' ('.$dtoken.') unauthorized: '.
                    973:                  $ENV{'user.name'}.' - '.$ENV{'user.domain'});
                    974:         return '';
                    975:     }
                    976: 
1.153     www       977:     my %infohash=('resource.0.intoken' => $token,
                    978:                   'resource.0.checkintime' => $now,
                    979:                   'resource.0.inremote' => $ENV{'REMOTE_ADDR'});
1.150     www       980: 
                    981:     unless (&cstore(\%infohash,$symb,$tcrsid,$tudom,$tuname) eq 'ok') {
                    982:        return '';
                    983:     }    
                    984: 
                    985:     if (&log($tudom,$tuname,&homeserver($tuname,$tudom),
                    986:                          &escape('Checkin - '.$token)) ne 'ok') {
                    987: 	return '';
                    988:     }
                    989: 
                    990:     return ($symb,$tuname,$tudom,$tcrsid);    
1.110     www       991: }
                    992: 
                    993: # --------------------------------------------- Set Expire Date for Spreadsheet
                    994: 
                    995: sub expirespread {
                    996:     my ($uname,$udom,$stype,$usymb)=@_;
                    997:     my $cid=$ENV{'request.course.id'}; 
                    998:     if ($cid) {
                    999:        my $now=time;
                   1000:        my $key=$uname.':'.$udom.':'.$stype.':'.$usymb;
                   1001:        return &reply('put:'.$ENV{'course.'.$cid.'.domain'}.':'.
                   1002:                             $ENV{'course.'.$cid.'.num'}.
                   1003: 	        	    ':nohist_expirationdates:'.
                   1004:                             &escape($key).'='.$now,
                   1005:                             $ENV{'course.'.$cid.'.home'})
                   1006:     }
                   1007:     return 'ok';
1.14      www      1008: }
                   1009: 
1.109     www      1010: # ----------------------------------------------------- Devalidate Spreadsheets
                   1011: 
                   1012: sub devalidate {
                   1013:     my $symb=shift;
                   1014:     my $cid=$ENV{'request.course.id'}; 
                   1015:     if ($cid) {
                   1016: 	my $key=$ENV{'user.name'}.':'.$ENV{'user.domain'}.':';
                   1017:         my $status=
1.133     albertel 1018: 	    &del('nohist_calculatedsheet',
                   1019: 		 [$key.'studentcalc'],
                   1020: 		 $ENV{'course.'.$cid.'.domain'},
                   1021: 		 $ENV{'course.'.$cid.'.num'})
                   1022: 		.' '.
                   1023: 	    &del('nohist_calculatedsheets_'.$cid,
                   1024: 		 [$key.'assesscalc:'.$symb]);
1.109     www      1025:         unless ($status eq 'ok ok') {
                   1026:            &logthis('Could not devalidate spreadsheet '.
                   1027:                     $ENV{'user.name'}.' at '.$ENV{'user.domain'}.' for '.
                   1028: 		    $symb.': '.$status);
1.133     albertel 1029:         }
1.109     www      1030:     }
                   1031: }
                   1032: 
1.265     albertel 1033: sub get_scalar {
                   1034:     my ($string,$end) = @_;
                   1035:     my $value;
                   1036:     if ($$string =~ s/^([^&]*?)($end)/$2/) {
                   1037: 	$value = $1;
                   1038:     } elsif ($$string =~ s/^([^&]*?)&//) {
                   1039: 	$value = $1;
                   1040:     }
                   1041:     return &unescape($value);
                   1042: }
                   1043: 
                   1044: sub array2str {
                   1045:   my (@array) = @_;
                   1046:   my $result=&arrayref2str(\@array);
                   1047:   $result=~s/^__ARRAY_REF__//;
                   1048:   $result=~s/__END_ARRAY_REF__$//;
                   1049:   return $result;
                   1050: }
                   1051: 
1.204     albertel 1052: sub arrayref2str {
                   1053:   my ($arrayref) = @_;
1.265     albertel 1054:   my $result='__ARRAY_REF__';
1.204     albertel 1055:   foreach my $elem (@$arrayref) {
1.265     albertel 1056:     if(ref($elem) eq 'ARRAY') {
                   1057:       $result.=&arrayref2str($elem).'&';
                   1058:     } elsif(ref($elem) eq 'HASH') {
                   1059:       $result.=&hashref2str($elem).'&';
                   1060:     } elsif(ref($elem)) {
                   1061:       #print("Got a ref of ".(ref($elem))." skipping.");
1.204     albertel 1062:     } else {
                   1063:       $result.=&escape($elem).'&';
                   1064:     }
                   1065:   }
                   1066:   $result=~s/\&$//;
1.265     albertel 1067:   $result .= '__END_ARRAY_REF__';
1.204     albertel 1068:   return $result;
                   1069: }
                   1070: 
1.168     albertel 1071: sub hash2str {
1.204     albertel 1072:   my (%hash) = @_;
                   1073:   my $result=&hashref2str(\%hash);
1.265     albertel 1074:   $result=~s/^__HASH_REF__//;
                   1075:   $result=~s/__END_HASH_REF__$//;
1.204     albertel 1076:   return $result;
                   1077: }
                   1078: 
                   1079: sub hashref2str {
                   1080:   my ($hashref)=@_;
1.265     albertel 1081:   my $result='__HASH_REF__';
1.204     albertel 1082:   foreach (keys(%$hashref)) {
                   1083:     if (ref($_) eq 'ARRAY') {
1.265     albertel 1084:       $result.=&arrayref2str($_).'=';
1.204     albertel 1085:     } elsif (ref($_) eq 'HASH') {
1.265     albertel 1086:       $result.=&hashref2str($_).'=';
1.204     albertel 1087:     } elsif (ref($_)) {
1.265     albertel 1088:       $result.='=';
                   1089:       #print("Got a ref of ".(ref($_))." skipping.");
1.204     albertel 1090:     } else {
1.265     albertel 1091: 	if ($_) {$result.=&escape($_).'=';} else { last; }
1.204     albertel 1092:     }
                   1093: 
1.265     albertel 1094:     if(ref($hashref->{$_}) eq 'ARRAY') {
                   1095:       $result.=&arrayref2str($hashref->{$_}).'&';
                   1096:     } elsif(ref($hashref->{$_}) eq 'HASH') {
                   1097:       $result.=&hashref2str($hashref->{$_}).'&';
                   1098:     } elsif(ref($hashref->{$_})) {
                   1099:        $result.='&';
                   1100:       #print("Got a ref of ".(ref($hashref->{$_}))." skipping.");
1.204     albertel 1101:     } else {
1.265     albertel 1102:       $result.=&escape($hashref->{$_}).'&';
1.204     albertel 1103:     }
                   1104:   }
1.168     albertel 1105:   $result=~s/\&$//;
1.265     albertel 1106:   $result .= '__END_HASH_REF__';
1.168     albertel 1107:   return $result;
                   1108: }
                   1109: 
                   1110: sub str2hash {
1.265     albertel 1111:     my ($string)=@_;
                   1112:     my ($hash)=&str2hashref('__HASH_REF__'.$string.'__END_HASH_REF__');
                   1113:     return %$hash;
                   1114: }
                   1115: 
                   1116: sub str2hashref {
1.168     albertel 1117:   my ($string) = @_;
1.265     albertel 1118: 
                   1119:   my %hash;
                   1120: 
                   1121:   if($string !~ /^__HASH_REF__/) {
                   1122:       if (! ($string eq '' || !defined($string))) {
                   1123: 	  $hash{'error'}='Not hash reference';
                   1124:       }
                   1125:       return (\%hash, $string);
                   1126:   }
                   1127: 
                   1128:   $string =~ s/^__HASH_REF__//;
                   1129: 
                   1130:   while($string !~ /^__END_HASH_REF__/) {
                   1131:       #key
                   1132:       my $key='';
                   1133:       if($string =~ /^__HASH_REF__/) {
                   1134:           ($key, $string)=&str2hashref($string);
                   1135:           if(defined($key->{'error'})) {
                   1136:               $hash{'error'}='Bad data';
                   1137:               return (\%hash, $string);
                   1138:           }
                   1139:       } elsif($string =~ /^__ARRAY_REF__/) {
                   1140:           ($key, $string)=&str2arrayref($string);
                   1141:           if($key->[0] eq 'Array reference error') {
                   1142:               $hash{'error'}='Bad data';
                   1143:               return (\%hash, $string);
                   1144:           }
                   1145:       } else {
                   1146:           $string =~ s/^(.*?)=//;
1.267     albertel 1147: 	  $key=&unescape($1);
1.265     albertel 1148:       }
                   1149:       $string =~ s/^=//;
                   1150: 
                   1151:       #value
                   1152:       my $value='';
                   1153:       if($string =~ /^__HASH_REF__/) {
                   1154:           ($value, $string)=&str2hashref($string);
                   1155:           if(defined($value->{'error'})) {
                   1156:               $hash{'error'}='Bad data';
                   1157:               return (\%hash, $string);
                   1158:           }
                   1159:       } elsif($string =~ /^__ARRAY_REF__/) {
                   1160:           ($value, $string)=&str2arrayref($string);
                   1161:           if($value->[0] eq 'Array reference error') {
                   1162:               $hash{'error'}='Bad data';
                   1163:               return (\%hash, $string);
                   1164:           }
                   1165:       } else {
                   1166: 	  $value=&get_scalar(\$string,'__END_HASH_REF__');
                   1167:       }
                   1168:       $string =~ s/^&//;
                   1169: 
                   1170:       $hash{$key}=$value;
1.204     albertel 1171:   }
1.265     albertel 1172: 
                   1173:   $string =~ s/^__END_HASH_REF__//;
                   1174: 
                   1175:   return (\%hash, $string);
1.204     albertel 1176: }
                   1177: 
                   1178: sub str2array {
1.265     albertel 1179:     my ($string)=@_;
                   1180:     my ($array)=&str2arrayref('__ARRAY_REF__'.$string.'__END_ARRAY_REF__');
                   1181:     return @$array;
                   1182: }
                   1183: 
                   1184: sub str2arrayref {
1.204     albertel 1185:   my ($string) = @_;
1.265     albertel 1186:   my @array;
                   1187: 
                   1188:   if($string !~ /^__ARRAY_REF__/) {
                   1189:       if (! ($string eq '' || !defined($string))) {
                   1190: 	  $array[0]='Array reference error';
                   1191:       }
                   1192:       return (\@array, $string);
                   1193:   }
                   1194: 
                   1195:   $string =~ s/^__ARRAY_REF__//;
                   1196: 
                   1197:   while($string !~ /^__END_ARRAY_REF__/) {
                   1198:       my $value='';
                   1199:       if($string =~ /^__HASH_REF__/) {
                   1200:           ($value, $string)=&str2hashref($string);
                   1201:           if(defined($value->{'error'})) {
                   1202:               $array[0] ='Array reference error';
                   1203:               return (\@array, $string);
                   1204:           }
                   1205:       } elsif($string =~ /^__ARRAY_REF__/) {
                   1206:           ($value, $string)=&str2arrayref($string);
                   1207:           if($value->[0] eq 'Array reference error') {
                   1208:               $array[0] ='Array reference error';
                   1209:               return (\@array, $string);
                   1210:           }
                   1211:       } else {
                   1212: 	  $value=&get_scalar(\$string,'__END_ARRAY_REF__');
                   1213:       }
                   1214:       $string =~ s/^&//;
                   1215: 
                   1216:       push(@array, $value);
1.191     harris41 1217:   }
1.265     albertel 1218: 
                   1219:   $string =~ s/^__END_ARRAY_REF__//;
                   1220: 
                   1221:   return (\@array, $string);
1.168     albertel 1222: }
                   1223: 
1.167     albertel 1224: # -------------------------------------------------------------------Temp Store
                   1225: 
1.168     albertel 1226: sub tmpreset {
                   1227:   my ($symb,$namespace,$domain,$stuname) = @_;
                   1228:   if (!$symb) {
                   1229:     $symb=&symbread();
                   1230:     if (!$symb) { $symb= $ENV{'REQUEST_URI'}; }
                   1231:   }
                   1232:   $symb=escape($symb);
                   1233: 
                   1234:   if (!$namespace) { $namespace=$ENV{'request.state'}; }
                   1235:   $namespace=~s/\//\_/g;
                   1236:   $namespace=~s/\W//g;
                   1237: 
                   1238:   #FIXME needs to do something for /pub resources
                   1239:   if (!$domain) { $domain=$ENV{'user.domain'}; }
                   1240:   if (!$stuname) { $stuname=$ENV{'user.name'}; }
                   1241:   my $path=$perlvar{'lonDaemons'}.'/tmp';
                   1242:   my %hash;
                   1243:   if (tie(%hash,'GDBM_File',
                   1244: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256     albertel 1245: 	  &GDBM_WRCREAT(),0640)) {
1.168     albertel 1246:     foreach my $key (keys %hash) {
1.180     albertel 1247:       if ($key=~ /:$symb/) {
1.168     albertel 1248: 	delete($hash{$key});
                   1249:       }
                   1250:     }
                   1251:   }
                   1252: }
                   1253: 
1.167     albertel 1254: sub tmpstore {
1.168     albertel 1255:   my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
                   1256: 
                   1257:   if (!$symb) {
                   1258:     $symb=&symbread();
                   1259:     if (!$symb) { $symb= $ENV{'request.url'}; }
                   1260:   }
                   1261:   $symb=escape($symb);
                   1262: 
                   1263:   if (!$namespace) {
                   1264:     # I don't think we would ever want to store this for a course.
                   1265:     # it seems this will only be used if we don't have a course.
                   1266:     #$namespace=$ENV{'request.course.id'};
                   1267:     #if (!$namespace) {
                   1268:       $namespace=$ENV{'request.state'};
                   1269:     #}
                   1270:   }
                   1271:   $namespace=~s/\//\_/g;
                   1272:   $namespace=~s/\W//g;
                   1273: #FIXME needs to do something for /pub resources
                   1274:   if (!$domain) { $domain=$ENV{'user.domain'}; }
                   1275:   if (!$stuname) { $stuname=$ENV{'user.name'}; }
                   1276:   my $now=time;
                   1277:   my %hash;
                   1278:   my $path=$perlvar{'lonDaemons'}.'/tmp';
                   1279:   if (tie(%hash,'GDBM_File',
                   1280: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256     albertel 1281: 	  &GDBM_WRCREAT(),0640)) {
1.168     albertel 1282:     $hash{"version:$symb"}++;
                   1283:     my $version=$hash{"version:$symb"};
                   1284:     my $allkeys=''; 
                   1285:     foreach my $key (keys(%$storehash)) {
                   1286:       $allkeys.=$key.':';
                   1287:       $hash{"$version:$symb:$key"}=$$storehash{$key};
                   1288:     }
                   1289:     $hash{"$version:$symb:timestamp"}=$now;
                   1290:     $allkeys.='timestamp';
                   1291:     $hash{"$version:keys:$symb"}=$allkeys;
                   1292:     if (untie(%hash)) {
                   1293:       return 'ok';
                   1294:     } else {
                   1295:       return "error:$!";
                   1296:     }
                   1297:   } else {
                   1298:     return "error:$!";
                   1299:   }
                   1300: }
1.167     albertel 1301: 
1.168     albertel 1302: # -----------------------------------------------------------------Temp Restore
1.167     albertel 1303: 
1.168     albertel 1304: sub tmprestore {
                   1305:   my ($symb,$namespace,$domain,$stuname) = @_;
1.167     albertel 1306: 
1.168     albertel 1307:   if (!$symb) {
                   1308:     $symb=&symbread();
                   1309:     if (!$symb) { $symb= $ENV{'request.url'}; }
                   1310:   }
                   1311:   $symb=escape($symb);
                   1312: 
                   1313:   if (!$namespace) { $namespace=$ENV{'request.state'}; }
                   1314:   #FIXME needs to do something for /pub resources
                   1315:   if (!$domain) { $domain=$ENV{'user.domain'}; }
                   1316:   if (!$stuname) { $stuname=$ENV{'user.name'}; }
                   1317: 
                   1318:   my %returnhash;
                   1319:   $namespace=~s/\//\_/g;
                   1320:   $namespace=~s/\W//g;
                   1321:   my %hash;
                   1322:   my $path=$perlvar{'lonDaemons'}.'/tmp';
                   1323:   if (tie(%hash,'GDBM_File',
                   1324: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256     albertel 1325: 	  &GDBM_READER(),0640)) {
1.168     albertel 1326:     my $version=$hash{"version:$symb"};
                   1327:     $returnhash{'version'}=$version;
                   1328:     my $scope;
                   1329:     for ($scope=1;$scope<=$version;$scope++) {
                   1330:       my $vkeys=$hash{"$scope:keys:$symb"};
                   1331:       my @keys=split(/:/,$vkeys);
                   1332:       my $key;
                   1333:       $returnhash{"$scope:keys"}=$vkeys;
                   1334:       foreach $key (@keys) {
                   1335: 	$returnhash{"$scope:$key"}=$hash{"$scope:$symb:$key"};
                   1336: 	$returnhash{"$key"}=$hash{"$scope:$symb:$key"};
1.167     albertel 1337:       }
                   1338:     }
1.168     albertel 1339:     if (!(untie(%hash))) {
                   1340:       return "error:$!";
                   1341:     }
                   1342:   } else {
                   1343:     return "error:$!";
                   1344:   }
                   1345:   return %returnhash;
1.167     albertel 1346: }
                   1347: 
1.9       www      1348: # ----------------------------------------------------------------------- Store
                   1349: 
                   1350: sub store {
1.124     www      1351:     my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
                   1352:     my $home='';
                   1353: 
1.168     albertel 1354:     if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124     www      1355: 
1.213     www      1356:     $symb=&symbclean($symb);
1.122     albertel 1357:     if (!$symb) { unless ($symb=&symbread()) { return ''; } }
1.109     www      1358: 
                   1359:     &devalidate($symb);
                   1360: 
                   1361:     $symb=escape($symb);
1.187     www      1362:     if (!$namespace) { 
                   1363:        unless ($namespace=$ENV{'request.course.id'}) { 
                   1364:           return ''; 
                   1365:        } 
                   1366:     }
1.122     albertel 1367:     if (!$domain) { $domain=$ENV{'user.domain'}; }
                   1368:     if (!$stuname) { $stuname=$ENV{'user.name'}; }
                   1369:     if (!$home) { $home=$ENV{'user.home'}; }
1.12      www      1370:     my $namevalue='';
1.191     harris41 1371:     foreach (keys %$storehash) {
1.122     albertel 1372:         $namevalue.=escape($_).'='.escape($$storehash{$_}).'&';
1.191     harris41 1373:     }
1.12      www      1374:     $namevalue=~s/\&$//;
1.187     www      1375:     &courselog($symb.':'.$stuname.':'.$domain.':STORE:'.$namevalue);
1.124     www      1376:     return reply("store:$domain:$stuname:$namespace:$symb:$namevalue","$home");
1.9       www      1377: }
                   1378: 
1.47      www      1379: # -------------------------------------------------------------- Critical Store
                   1380: 
                   1381: sub cstore {
1.124     www      1382:     my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
                   1383:     my $home='';
                   1384: 
1.168     albertel 1385:     if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124     www      1386: 
1.213     www      1387:     $symb=&symbclean($symb);
1.122     albertel 1388:     if (!$symb) { unless ($symb=&symbread()) { return ''; } }
1.109     www      1389: 
                   1390:     &devalidate($symb);
                   1391: 
                   1392:     $symb=escape($symb);
1.187     www      1393:     if (!$namespace) { 
                   1394:        unless ($namespace=$ENV{'request.course.id'}) { 
                   1395:           return ''; 
                   1396:        } 
                   1397:     }
1.122     albertel 1398:     if (!$domain) { $domain=$ENV{'user.domain'}; }
                   1399:     if (!$stuname) { $stuname=$ENV{'user.name'}; }
                   1400:     if (!$home) { $home=$ENV{'user.home'}; }
                   1401: 
1.47      www      1402:     my $namevalue='';
1.191     harris41 1403:     foreach (keys %$storehash) {
1.122     albertel 1404:         $namevalue.=escape($_).'='.escape($$storehash{$_}).'&';
1.191     harris41 1405:     }
1.47      www      1406:     $namevalue=~s/\&$//;
1.187     www      1407:     &courselog($symb.':'.$stuname.':'.$domain.':CSTORE:'.$namevalue);
1.188     www      1408:     return critical
                   1409:                 ("store:$domain:$stuname:$namespace:$symb:$namevalue","$home");
1.47      www      1410: }
                   1411: 
1.9       www      1412: # --------------------------------------------------------------------- Restore
                   1413: 
                   1414: sub restore {
1.124     www      1415:     my ($symb,$namespace,$domain,$stuname) = @_;
                   1416:     my $home='';
                   1417: 
1.168     albertel 1418:     if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124     www      1419: 
1.122     albertel 1420:     if (!$symb) {
                   1421:       unless ($symb=escape(&symbread())) { return ''; }
                   1422:     } else {
1.213     www      1423:       $symb=&escape(&symbclean($symb));
1.122     albertel 1424:     }
1.188     www      1425:     if (!$namespace) { 
                   1426:        unless ($namespace=$ENV{'request.course.id'}) { 
                   1427:           return ''; 
                   1428:        } 
                   1429:     }
1.122     albertel 1430:     if (!$domain) { $domain=$ENV{'user.domain'}; }
                   1431:     if (!$stuname) { $stuname=$ENV{'user.name'}; }
                   1432:     if (!$home) { $home=$ENV{'user.home'}; }
                   1433:     my $answer=&reply("restore:$domain:$stuname:$namespace:$symb","$home");
                   1434: 
1.12      www      1435:     my %returnhash=();
1.191     harris41 1436:     foreach (split(/\&/,$answer)) {
1.12      www      1437: 	my ($name,$value)=split(/\=/,$_);
                   1438:         $returnhash{&unescape($name)}=&unescape($value);
1.191     harris41 1439:     }
1.75      www      1440:     my $version;
                   1441:     for ($version=1;$version<=$returnhash{'version'};$version++) {
1.191     harris41 1442:        foreach (split(/\:/,$returnhash{$version.':keys'})) {
1.75      www      1443:           $returnhash{$_}=$returnhash{$version.':'.$_};
1.191     harris41 1444:        }
1.75      www      1445:     }
1.13      www      1446:     return %returnhash;
1.34      www      1447: }
                   1448: 
                   1449: # ---------------------------------------------------------- Course Description
                   1450: 
                   1451: sub coursedescription {
                   1452:     my $courseid=shift;
                   1453:     $courseid=~s/^\///;
1.49      www      1454:     $courseid=~s/\_/\//g;
1.34      www      1455:     my ($cdomain,$cnum)=split(/\//,$courseid);
1.129     albertel 1456:     my $chome=&homeserver($cnum,$cdomain);
1.34      www      1457:     if ($chome ne 'no_host') {
1.129     albertel 1458:        my %returnhash=&dump('environment',$cdomain,$cnum);
                   1459:        if (!exists($returnhash{'con_lost'})) {
1.96      www      1460:            my $normalid=$cdomain.'_'.$cnum;
1.53      www      1461:            my %envhash=();
1.129     albertel 1462:            $returnhash{'home'}= $chome;
                   1463: 	   $returnhash{'domain'} = $cdomain;
                   1464: 	   $returnhash{'num'} = $cnum;
1.130     albertel 1465:            while (my ($name,$value) = each %returnhash) {
1.53      www      1466:                $envhash{'course.'.$normalid.'.'.$name}=$value;
1.129     albertel 1467:            }
1.270     www      1468:            $returnhash{'url'}=&clutter($returnhash{'url'});
1.34      www      1469:            $returnhash{'fn'}=$perlvar{'lonDaemons'}.'/tmp/'.
1.38      www      1470: 	       $ENV{'user.name'}.'_'.$cdomain.'_'.$cnum;
1.54      www      1471:            $envhash{'course.'.$normalid.'.last_cache'}=time;
1.60      www      1472:            $envhash{'course.'.$normalid.'.home'}=$chome;
                   1473:            $envhash{'course.'.$normalid.'.domain'}=$cdomain;
                   1474:            $envhash{'course.'.$normalid.'.num'}=$cnum;
1.53      www      1475:            &appenv(%envhash);
1.34      www      1476:            return %returnhash;
                   1477:        }
                   1478:     }
                   1479:     return ();
1.9       www      1480: }
1.1       albertel 1481: 
1.103     harris41 1482: # -------------------------------------------------------- Get user privileges
1.11      www      1483: 
                   1484: sub rolesinit {
                   1485:     my ($domain,$username,$authhost)=@_;
                   1486:     my $rolesdump=reply("dump:$domain:$username:roles",$authhost);
1.12      www      1487:     if (($rolesdump eq 'con_lost') || ($rolesdump eq '')) { return ''; }
1.11      www      1488:     my %allroles=();
                   1489:     my %thesepriv=();
                   1490:     my $now=time;
1.21      www      1491:     my $userroles="user.login.time=$now\n";
1.11      www      1492:     my $thesestr;
                   1493: 
                   1494:     if ($rolesdump ne '') {
1.191     harris41 1495:         foreach (split(/&/,$rolesdump)) {
1.21      www      1496: 	  if ($_!~/^rolesdef\&/) {
1.11      www      1497:             my ($area,$role)=split(/=/,$_);
1.21      www      1498:             $area=~s/\_\w\w$//;
1.11      www      1499:             my ($trole,$tend,$tstart)=split(/_/,$role);
1.21      www      1500:             $userroles.='user.role.'.$trole.'.'.$area.'='.
                   1501:                         $tstart.'.'.$tend."\n";
1.11      www      1502:             if ($tend!=0) {
                   1503: 	        if ($tend<$now) {
                   1504: 	            $trole='';
                   1505:                 } 
                   1506:             }
                   1507:             if ($tstart!=0) {
                   1508:                 if ($tstart>$now) {
                   1509:                    $trole='';        
                   1510:                 }
                   1511:             }
                   1512:             if (($area ne '') && ($trole ne '')) {
1.50      www      1513: 	       my $spec=$trole.'.'.$area;
1.12      www      1514:                my ($tdummy,$tdomain,$trest)=split(/\//,$area);
                   1515:                if ($trole =~ /^cr\//) {
                   1516: 		   my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$trole);
                   1517:                    my $homsvr=homeserver($rauthor,$rdomain);
                   1518:                    if ($hostname{$homsvr} ne '') {
                   1519:                       my $roledef=
1.21      www      1520: 			  reply("get:$rdomain:$rauthor:roles:rolesdef_$rrole",
1.12      www      1521:                                 $homsvr);
                   1522:                       if (($roledef ne 'con_lost') && ($roledef ne '')) {
                   1523:                          my ($syspriv,$dompriv,$coursepriv)=
1.21      www      1524: 			     split(/\_/,unescape($roledef));
1.50      www      1525:  	                 $allroles{'cm./'}.=':'.$syspriv;
                   1526:                          $allroles{$spec.'./'}.=':'.$syspriv;
1.12      www      1527:                          if ($tdomain ne '') {
1.50      www      1528:                              $allroles{'cm./'.$tdomain.'/'}.=':'.$dompriv;
                   1529:                              $allroles{$spec.'./'.$tdomain.'/'}.=':'.$dompriv;
1.12      www      1530:                              if ($trest ne '') {
1.50      www      1531: 		                $allroles{'cm.'.$area}.=':'.$coursepriv;
                   1532: 		                $allroles{$spec.'.'.$area}.=':'.$coursepriv;
1.12      www      1533:                              }
                   1534: 	                 }
                   1535:                       }
1.11      www      1536:                    }
1.12      www      1537:                } else {
1.50      www      1538: 	           $allroles{'cm./'}.=':'.$pr{$trole.':s'};
                   1539: 	           $allroles{$spec.'./'}.=':'.$pr{$trole.':s'};
1.12      www      1540:                    if ($tdomain ne '') {
1.50      www      1541:                      $allroles{'cm./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
                   1542:                      $allroles{$spec.'./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
1.12      www      1543:                       if ($trest ne '') {
1.50      www      1544: 		          $allroles{'cm.'.$area}.=':'.$pr{$trole.':c'};
                   1545: 		          $allroles{$spec.'.'.$area}.=':'.$pr{$trole.':c'};
1.12      www      1546:                       }
                   1547: 	           }
1.11      www      1548: 	       }
1.12      www      1549:             }
                   1550:           } 
1.191     harris41 1551:         }
1.125     www      1552:         my $adv=0;
1.128     www      1553:         my $author=0;
1.191     harris41 1554:         foreach (keys %allroles) {
1.11      www      1555:             %thesepriv=();
1.146     www      1556:             if (($_!~/^st/) && ($_!~/^ta/) && ($_!~/^cm/)) { $adv=1; }
1.128     www      1557:             if (($_=~/^au/) || ($_=~/^ca/)) { $author=1; }
1.191     harris41 1558:             foreach (split(/:/,$allroles{$_})) {
1.11      www      1559:                 if ($_ ne '') {
1.103     harris41 1560: 		    my ($privilege,$restrictions)=split(/&/,$_);
1.11      www      1561:                     if ($restrictions eq '') {
1.103     harris41 1562: 			$thesepriv{$privilege}='F';
1.11      www      1563:                     } else {
1.103     harris41 1564:                         if ($thesepriv{$privilege} ne 'F') {
                   1565: 			    $thesepriv{$privilege}.=$restrictions;
1.11      www      1566:                         }
                   1567:                     }
                   1568:                 }
1.191     harris41 1569:             }
1.11      www      1570:             $thesestr='';
1.191     harris41 1571:             foreach (keys %thesepriv) { $thesestr.=':'.$_.'&'.$thesepriv{$_}; }
1.11      www      1572:             $userroles.='user.priv.'.$_.'='.$thesestr."\n";
1.191     harris41 1573:         }
1.128     www      1574:         $userroles.='user.adv='.$adv."\n".
                   1575: 	            'user.author='.$author."\n";
1.126     www      1576:         $ENV{'user.adv'}=$adv;
1.11      www      1577:     }
                   1578:     return $userroles;  
                   1579: }
                   1580: 
1.12      www      1581: # --------------------------------------------------------------- get interface
                   1582: 
                   1583: sub get {
1.131     albertel 1584:    my ($namespace,$storearr,$udomain,$uname)=@_;
1.12      www      1585:    my $items='';
1.191     harris41 1586:    foreach (@$storearr) {
1.12      www      1587:        $items.=escape($_).'&';
1.191     harris41 1588:    }
1.12      www      1589:    $items=~s/\&$//;
1.131     albertel 1590:    if (!$udomain) { $udomain=$ENV{'user.domain'}; }
                   1591:    if (!$uname) { $uname=$ENV{'user.name'}; }
                   1592:    my $uhome=&homeserver($uname,$udomain);
                   1593: 
1.133     albertel 1594:    my $rep=&reply("get:$udomain:$uname:$namespace:$items",$uhome);
1.15      www      1595:    my @pairs=split(/\&/,$rep);
1.273     albertel 1596:    if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
                   1597:      return @pairs;
                   1598:    }
1.15      www      1599:    my %returnhash=();
1.42      www      1600:    my $i=0;
1.191     harris41 1601:    foreach (@$storearr) {
1.42      www      1602:       $returnhash{$_}=unescape($pairs[$i]);
                   1603:       $i++;
1.191     harris41 1604:    }
1.15      www      1605:    return %returnhash;
1.27      www      1606: }
                   1607: 
                   1608: # --------------------------------------------------------------- del interface
                   1609: 
                   1610: sub del {
1.133     albertel 1611:    my ($namespace,$storearr,$udomain,$uname)=@_;
1.27      www      1612:    my $items='';
1.191     harris41 1613:    foreach (@$storearr) {
1.27      www      1614:        $items.=escape($_).'&';
1.191     harris41 1615:    }
1.27      www      1616:    $items=~s/\&$//;
1.133     albertel 1617:    if (!$udomain) { $udomain=$ENV{'user.domain'}; }
                   1618:    if (!$uname) { $uname=$ENV{'user.name'}; }
                   1619:    my $uhome=&homeserver($uname,$udomain);
                   1620: 
                   1621:    return &reply("del:$udomain:$uname:$namespace:$items",$uhome);
1.15      www      1622: }
                   1623: 
                   1624: # -------------------------------------------------------------- dump interface
                   1625: 
                   1626: sub dump {
1.193     www      1627:    my ($namespace,$udomain,$uname,$regexp)=@_;
1.129     albertel 1628:    if (!$udomain) { $udomain=$ENV{'user.domain'}; }
                   1629:    if (!$uname) { $uname=$ENV{'user.name'}; }
                   1630:    my $uhome=&homeserver($uname,$udomain);
1.193     www      1631:    if ($regexp) {
                   1632:        $regexp=&escape($regexp);
                   1633:    } else {
                   1634:        $regexp='.';
                   1635:    }
                   1636:    my $rep=reply("dump:$udomain:$uname:$namespace:$regexp",$uhome);
1.12      www      1637:    my @pairs=split(/\&/,$rep);
                   1638:    my %returnhash=();
1.191     harris41 1639:    foreach (@pairs) {
1.12      www      1640:       my ($key,$value)=split(/=/,$_);
1.29      www      1641:       $returnhash{unescape($key)}=unescape($value);
1.191     harris41 1642:    }
1.12      www      1643:    return %returnhash;
                   1644: }
                   1645: 
                   1646: # --------------------------------------------------------------- put interface
                   1647: 
                   1648: sub put {
1.134     albertel 1649:    my ($namespace,$storehash,$udomain,$uname)=@_;
                   1650:    if (!$udomain) { $udomain=$ENV{'user.domain'}; }
                   1651:    if (!$uname) { $uname=$ENV{'user.name'}; }
                   1652:    my $uhome=&homeserver($uname,$udomain);
1.12      www      1653:    my $items='';
1.191     harris41 1654:    foreach (keys %$storehash) {
1.134     albertel 1655:        $items.=&escape($_).'='.&escape($$storehash{$_}).'&';
1.191     harris41 1656:    }
1.12      www      1657:    $items=~s/\&$//;
1.134     albertel 1658:    return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
1.47      www      1659: }
                   1660: 
                   1661: # ------------------------------------------------------ critical put interface
                   1662: 
                   1663: sub cput {
1.134     albertel 1664:    my ($namespace,$storehash,$udomain,$uname)=@_;
                   1665:    if (!$udomain) { $udomain=$ENV{'user.domain'}; }
                   1666:    if (!$uname) { $uname=$ENV{'user.name'}; }
                   1667:    my $uhome=&homeserver($uname,$udomain);
1.47      www      1668:    my $items='';
1.191     harris41 1669:    foreach (keys %$storehash) {
1.134     albertel 1670:        $items.=escape($_).'='.escape($$storehash{$_}).'&';
1.191     harris41 1671:    }
1.47      www      1672:    $items=~s/\&$//;
1.134     albertel 1673:    return &critical("put:$udomain:$uname:$namespace:$items",$uhome);
1.12      www      1674: }
                   1675: 
                   1676: # -------------------------------------------------------------- eget interface
                   1677: 
                   1678: sub eget {
1.133     albertel 1679:    my ($namespace,$storearr,$udomain,$uname)=@_;
1.12      www      1680:    my $items='';
1.191     harris41 1681:    foreach (@$storearr) {
1.12      www      1682:        $items.=escape($_).'&';
1.191     harris41 1683:    }
1.12      www      1684:    $items=~s/\&$//;
1.133     albertel 1685:    if (!$udomain) { $udomain=$ENV{'user.domain'}; }
                   1686:    if (!$uname) { $uname=$ENV{'user.name'}; }
                   1687:    my $uhome=&homeserver($uname,$udomain);
                   1688:    my $rep=&reply("eget:$udomain:$uname:$namespace:$items",$uhome);
1.12      www      1689:    my @pairs=split(/\&/,$rep);
                   1690:    my %returnhash=();
1.42      www      1691:    my $i=0;
1.191     harris41 1692:    foreach (@$storearr) {
1.42      www      1693:       $returnhash{$_}=unescape($pairs[$i]);
                   1694:       $i++;
1.191     harris41 1695:    }
1.12      www      1696:    return %returnhash;
                   1697: }
                   1698: 
1.103     harris41 1699: # ------------------------------------------------- Check for a user privilege
1.12      www      1700: 
                   1701: sub allowed {
                   1702:     my ($priv,$uri)=@_;
1.152     www      1703: 
                   1704:     my $orguri=$uri;
1.52      www      1705:     $uri=&declutter($uri);
1.29      www      1706: 
1.54      www      1707: # Free bre access to adm and meta resources
1.29      www      1708: 
1.54      www      1709:     if ((($uri=~/^adm\//) || ($uri=~/\.meta$/)) && ($priv eq 'bre')) {
1.14      www      1710: 	return 'F';
1.159     www      1711:     }
                   1712: 
                   1713: # Free bre to public access
                   1714: 
                   1715:     if ($priv eq 'bre') {
1.238     www      1716:         my $copyright=&metadata($uri,'copyright');
                   1717: 	if ($copyright eq 'public') { return 'F'; }
                   1718:         if ($copyright eq 'priv') {
                   1719:             $uri=~/([^\/]+)\/([^\/]+)\//;
                   1720: 	    unless (($ENV{'user.name'} eq $2) && ($ENV{'user.domain'} eq $1)) {
                   1721: 		return '';
                   1722:             }
                   1723:         }
                   1724:         if ($copyright eq 'domain') {
                   1725:             $uri=~/([^\/]+)\/([^\/]+)\//;
                   1726: 	    unless (($ENV{'user.domain'} eq $1) ||
                   1727:                  ($ENV{'course.'.$ENV{'request.course.id'}.'.domain'} eq $1)) {
                   1728: 		return '';
                   1729:             }
1.262     matthew  1730:         }
                   1731:         if ($ENV{'request.role'}=~ /li\.\//) {
                   1732:             # Library role, so allow browsing of resources in this domain.
                   1733:             return 'F';
1.238     www      1734:         }
1.14      www      1735:     }
1.264     matthew  1736:     # Domain coordinator is trying to create a course
                   1737:     if (($priv eq 'ccc') && ($ENV{'request.role'} =~ /^dc\./)) {
                   1738:         # uri is the requested domain in this case.
                   1739:         # comparison to 'request.role.domain' shows if the user has selected
                   1740:         # a role of dc for the domain in question. 
                   1741:         return 'F' if ($uri eq $ENV{'request.role.domain'});
                   1742:     }
1.29      www      1743: 
1.52      www      1744:     my $thisallowed='';
                   1745:     my $statecond=0;
                   1746:     my $courseprivid='';
                   1747: 
                   1748: # Course
                   1749: 
                   1750:     if ($ENV{'user.priv.'.$ENV{'request.role'}.'./'}=~/$priv\&([^\:]*)/) {
                   1751:        $thisallowed.=$1;
                   1752:     }
1.29      www      1753: 
1.52      www      1754: # Domain
                   1755: 
                   1756:     if ($ENV{'user.priv.'.$ENV{'request.role'}.'./'.(split(/\//,$uri))[0].'/'}
                   1757:        =~/$priv\&([^\:]*)/) {
1.12      www      1758:        $thisallowed.=$1;
                   1759:     }
1.52      www      1760: 
                   1761: # Course: uri itself is a course
1.66      www      1762:     my $courseuri=$uri;
                   1763:     $courseuri=~s/\_(\d)/\/$1/;
1.83      www      1764:     $courseuri=~s/^([^\/])/\/$1/;
1.81      www      1765: 
1.83      www      1766:     if ($ENV{'user.priv.'.$ENV{'request.role'}.'.'.$courseuri}
1.52      www      1767:        =~/$priv\&([^\:]*)/) {
1.12      www      1768:        $thisallowed.=$1;
                   1769:     }
1.29      www      1770: 
1.52      www      1771: # Full access at system, domain or course-wide level? Exit.
1.29      www      1772: 
                   1773:     if ($thisallowed=~/F/) {
                   1774: 	return 'F';
                   1775:     }
                   1776: 
1.52      www      1777: # If this is generating or modifying users, exit with special codes
1.29      www      1778: 
1.166     www      1779:     if (':csu:cdc:ccc:cin:cta:cep:ccr:cst:cad:cli:cau:cdg:cca:'=~/\:$priv\:/) {
1.52      www      1780: 	return $thisallowed;
                   1781:     }
                   1782: #
1.103     harris41 1783: # Gathered so far: system, domain and course wide privileges
1.52      www      1784: #
                   1785: # Course: See if uri or referer is an individual resource that is part of 
                   1786: # the course
                   1787: 
                   1788:     if ($ENV{'request.course.id'}) {
1.232     www      1789: 
1.52      www      1790:        $courseprivid=$ENV{'request.course.id'};
                   1791:        if ($ENV{'request.course.sec'}) {
                   1792:           $courseprivid.='/'.$ENV{'request.course.sec'};
                   1793:        }
                   1794:        $courseprivid=~s/\_/\//;
                   1795:        my $checkreferer=1;
1.232     www      1796:        my ($match,$cond)=&is_on_map($uri);
                   1797:        if ($match) {
                   1798:            $statecond=$cond;
1.52      www      1799:            if ($ENV{'user.priv.'.$ENV{'request.role'}.'./'.$courseprivid}
                   1800:                =~/$priv\&([^\:]*)/) {
                   1801:                $thisallowed.=$1;
                   1802:                $checkreferer=0;
                   1803:            }
1.29      www      1804:        }
1.83      www      1805:        
1.148     www      1806:        if ($checkreferer) {
1.152     www      1807: 	  my $refuri=$ENV{'httpref.'.$orguri};
1.148     www      1808:             unless ($refuri) {
1.191     harris41 1809:                 foreach (keys %ENV) {
1.148     www      1810: 		    if ($_=~/^httpref\..*\*/) {
                   1811: 			my $pattern=$_;
1.156     www      1812:                         $pattern=~s/^httpref\.\/res\///;
1.148     www      1813:                         $pattern=~s/\*/\[\^\/\]\+/g;
                   1814:                         $pattern=~s/\//\\\//g;
1.152     www      1815:                         if ($orguri=~/$pattern/) {
1.148     www      1816: 			    $refuri=$ENV{$_};
                   1817:                         }
                   1818:                     }
1.191     harris41 1819:                 }
1.148     www      1820:             }
1.232     www      1821: 
1.148     www      1822:          if ($refuri) { 
1.152     www      1823: 	  $refuri=&declutter($refuri);
1.232     www      1824:           my ($match,$cond)=&is_on_map($refuri);
                   1825:             if ($match) {
                   1826:               my $refstatecond=$cond;
1.52      www      1827:               if ($ENV{'user.priv.'.$ENV{'request.role'}.'./'.$courseprivid}
                   1828:                   =~/$priv\&([^\:]*)/) {
                   1829:                   $thisallowed.=$1;
1.53      www      1830:                   $uri=$refuri;
                   1831:                   $statecond=$refstatecond;
1.52      www      1832:               }
                   1833:           }
1.148     www      1834:         }
1.29      www      1835:        }
1.52      www      1836:    }
1.29      www      1837: 
1.52      www      1838: #
1.103     harris41 1839: # Gathered now: all privileges that could apply, and condition number
1.52      www      1840: # 
                   1841: #
                   1842: # Full or no access?
                   1843: #
1.29      www      1844: 
1.52      www      1845:     if ($thisallowed=~/F/) {
                   1846: 	return 'F';
                   1847:     }
1.29      www      1848: 
1.52      www      1849:     unless ($thisallowed) {
                   1850:         return '';
                   1851:     }
1.29      www      1852: 
1.52      www      1853: # Restrictions exist, deal with them
                   1854: #
                   1855: #   C:according to course preferences
                   1856: #   R:according to resource settings
                   1857: #   L:unless locked
                   1858: #   X:according to user session state
                   1859: #
                   1860: 
                   1861: # Possibly locked functionality, check all courses
1.54      www      1862: # Locks might take effect only after 10 minutes cache expiration for other
                   1863: # courses, and 2 minutes for current course
1.52      www      1864: 
                   1865:     my $envkey;
                   1866:     if ($thisallowed=~/L/) {
                   1867:         foreach $envkey (keys %ENV) {
1.54      www      1868:            if ($envkey=~/^user\.role\.(st|ta)\.([^\.]*)/) {
                   1869:                my $courseid=$2;
                   1870:                my $roleid=$1.'.'.$2;
1.92      www      1871:                $courseid=~s/^\///;
1.54      www      1872:                my $expiretime=600;
                   1873:                if ($ENV{'request.role'} eq $roleid) {
                   1874: 		  $expiretime=120;
                   1875:                }
                   1876: 	       my ($cdom,$cnum,$csec)=split(/\//,$courseid);
                   1877:                my $prefix='course.'.$cdom.'_'.$cnum.'.';
                   1878:                if ((time-$ENV{$prefix.'last_cache'})>$expiretime) {
                   1879: 		   &coursedescription($courseid);
                   1880:                }
                   1881:                if (($ENV{$prefix.'res.'.$uri.'.lock.sections'}=~/\,$csec\,/)
                   1882:                 || ($ENV{$prefix.'res.'.$uri.'.lock.sections'} eq 'all')) {
                   1883: 		   if ($ENV{$prefix.'res.'.$uri.'.lock.expire'}>time) {
1.57      www      1884:                        &log($ENV{'user.domain'},$ENV{'user.name'},
1.239     www      1885:                             $ENV{'user.home'},
1.57      www      1886:                             'Locked by res: '.$priv.' for '.$uri.' due to '.
1.52      www      1887:                             $cdom.'/'.$cnum.'/'.$csec.' expire '.
1.54      www      1888:                             $ENV{$prefix.'priv.'.$priv.'.lock.expire'});
1.52      www      1889: 		       return '';
                   1890:                    }
                   1891:                }
1.54      www      1892:                if (($ENV{$prefix.'priv.'.$priv.'.lock.sections'}=~/\,$csec\,/)
                   1893:                 || ($ENV{$prefix.'priv.'.$priv.'.lock.sections'} eq 'all')) {
                   1894: 		   if ($ENV{'priv.'.$priv.'.lock.expire'}>time) {
1.57      www      1895:                        &log($ENV{'user.domain'},$ENV{'user.name'},
1.239     www      1896:                             $ENV{'user.home'},
1.57      www      1897:                             'Locked by priv: '.$priv.' for '.$uri.' due to '.
1.52      www      1898:                             $cdom.'/'.$cnum.'/'.$csec.' expire '.
1.54      www      1899:                             $ENV{$prefix.'priv.'.$priv.'.lock.expire'});
1.52      www      1900: 		       return '';
                   1901:                    }
                   1902:                }
                   1903: 	   }
1.29      www      1904:        }
1.52      www      1905:     }
                   1906:    
                   1907: #
                   1908: # Rest of the restrictions depend on selected course
                   1909: #
                   1910: 
                   1911:     unless ($ENV{'request.course.id'}) {
                   1912:        return '1';
                   1913:     }
1.29      www      1914: 
1.52      www      1915: #
                   1916: # Now user is definitely in a course
                   1917: #
1.53      www      1918: 
                   1919: 
                   1920: # Course preferences
                   1921: 
                   1922:    if ($thisallowed=~/C/) {
1.54      www      1923:        my $rolecode=(split(/\./,$ENV{'request.role'}))[0];
1.237     www      1924:        my $unamedom=$ENV{'user.name'}.':'.$ENV{'user.domain'};
1.54      www      1925:        if ($ENV{'course.'.$ENV{'request.course.id'}.'.'.$priv.'.roles.denied'}
1.194     www      1926: 	   =~/$rolecode/) {
1.57      www      1927:            &log($ENV{'user.domain'},$ENV{'user.name'},$ENV{'user.host'},
                   1928:                 'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode.' in '.
1.237     www      1929:                 $ENV{'request.course.id'});
                   1930:            return '';
                   1931:        }
                   1932: 
                   1933:        if ($ENV{'course.'.$ENV{'request.course.id'}.'.'.$priv.'.users.denied'}
                   1934: 	   =~/$unamedom/) {
                   1935:            &log($ENV{'user.domain'},$ENV{'user.name'},$ENV{'user.host'},
                   1936:                 'Denied by user: '.$priv.' for '.$uri.' as '.$unamedom.' in '.
1.54      www      1937:                 $ENV{'request.course.id'});
                   1938:            return '';
                   1939:        }
1.53      www      1940:    }
                   1941: 
                   1942: # Resource preferences
                   1943: 
                   1944:    if ($thisallowed=~/R/) {
1.54      www      1945:        my $rolecode=(split(/\./,$ENV{'request.role'}))[0];
                   1946:        my $filename=$perlvar{'lonDocRoot'}.'/res/'.$uri.'.meta';
                   1947:        if (-e $filename) {
                   1948:            my @content;
                   1949:            {
                   1950: 	     my $fh=Apache::File->new($filename);
                   1951:              @content=<$fh>;
                   1952: 	   }
                   1953:            if (join('',@content)=~
                   1954:                     /\<roledeny[^\>]*\>[^\<]*$rolecode[^\<]*\<\/roledeny\>/) {
1.57      www      1955: 	       &log($ENV{'user.domain'},$ENV{'user.name'},$ENV{'user.host'},
                   1956:                     'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode);
1.54      www      1957:            return '';
                   1958: 
                   1959:            }
                   1960:        }
1.53      www      1961:    }
1.30      www      1962: 
1.246     www      1963: # Restricted by state or randomout?
1.30      www      1964: 
1.52      www      1965:    if ($thisallowed=~/X/) {
1.247     www      1966:       if ($ENV{'acc.randomout'}) {
1.249     www      1967:          my $symb=&symbread($uri,1);
1.248     www      1968:          if (($symb) && ($ENV{'acc.randomout'}=~/\&$symb\&/)) { 
                   1969:             return ''; 
                   1970:          }
1.247     www      1971:       }
                   1972:       if (&condval($statecond)) {
1.52      www      1973: 	 return '2';
                   1974:       } else {
                   1975:          return '';
                   1976:       }
                   1977:    }
1.30      www      1978: 
1.52      www      1979:    return 'F';
1.232     www      1980: }
                   1981: 
                   1982: # --------------------------------------------------- Is a resource on the map?
                   1983: 
                   1984: sub is_on_map {
                   1985:     my $uri=&declutter(shift);
                   1986:     my @uriparts=split(/\//,$uri);
                   1987:     my $filename=$uriparts[$#uriparts];
                   1988:     my $pathname=$uri;
1.289   ! bowersj2 1989:     $pathname=~s|/\Q$filename\E$||;
        !          1990:     #Trying to find the conditional for the file
1.232     www      1991:     my $match=($ENV{'acc.res.'.$ENV{'request.course.id'}.'.'.$pathname}=~
1.289   ! bowersj2 1992: 	       /\&\Q$filename\E\:([\d\|]+)\&/);
1.232     www      1993:     if ($match) {
1.289   ! bowersj2 1994: 	return (1,$1);
        !          1995:     } else {
        !          1996: 	return (0,0);
        !          1997:     }
1.12      www      1998: }
                   1999: 
                   2000: # ----------------------------------------------------------------- Define Role
                   2001: 
                   2002: sub definerole {
                   2003:   if (allowed('mcr','/')) {
                   2004:     my ($rolename,$sysrole,$domrole,$courole)=@_;
1.191     harris41 2005:     foreach (split('/',$sysrole)) {
1.21      www      2006: 	my ($crole,$cqual)=split(/\&/,$_);
                   2007:         if ($pr{'cr:s'}!~/$crole/) { return "refused:s:$crole"; }
                   2008:         if ($pr{'cr:s'}=~/$crole\&/) {
                   2009: 	    if ($pr{'cr:s'}!~/$crole\&\w*$cqual/) { 
                   2010:                return "refused:s:$crole&$cqual"; 
                   2011:             }
                   2012:         }
1.191     harris41 2013:     }
                   2014:     foreach (split('/',$domrole)) {
1.21      www      2015: 	my ($crole,$cqual)=split(/\&/,$_);
                   2016:         if ($pr{'cr:d'}!~/$crole/) { return "refused:d:$crole"; }
                   2017:         if ($pr{'cr:d'}=~/$crole\&/) {
                   2018: 	    if ($pr{'cr:d'}!~/$crole\&\w*$cqual/) { 
                   2019:                return "refused:d:$crole&$cqual"; 
                   2020:             }
                   2021:         }
1.191     harris41 2022:     }
                   2023:     foreach (split('/',$courole)) {
1.21      www      2024: 	my ($crole,$cqual)=split(/\&/,$_);
                   2025:         if ($pr{'cr:c'}!~/$crole/) { return "refused:c:$crole"; }
                   2026:         if ($pr{'cr:c'}=~/$crole\&/) {
                   2027: 	    if ($pr{'cr:c'}!~/$crole\&\w*$cqual/) { 
                   2028:                return "refused:c:$crole&$cqual"; 
                   2029:             }
                   2030:         }
1.191     harris41 2031:     }
1.12      www      2032:     my $command="encrypt:rolesput:$ENV{'user.domain'}:$ENV{'user.name'}:".
                   2033:                 "$ENV{'user.domain'}:$ENV{'user.name'}:".
1.21      www      2034: 	        "rolesdef_$rolename=".
                   2035:                 escape($sysrole.'_'.$domrole.'_'.$courole);
1.12      www      2036:     return reply($command,$ENV{'user.home'});
                   2037:   } else {
                   2038:     return 'refused';
                   2039:   }
1.105     harris41 2040: }
                   2041: 
                   2042: # ---------------- Make a metadata query against the network of library servers
                   2043: 
                   2044: sub metadata_query {
1.244     matthew  2045:     my ($query,$custom,$customshow,$server_array)=@_;
1.120     harris41 2046:     my %rhash;
1.244     matthew  2047:     my @server_list = (defined($server_array) ? @$server_array
                   2048:                                               : keys(%libserv) );
                   2049:     for my $server (@server_list) {
1.118     harris41 2050: 	unless ($custom or $customshow) {
                   2051: 	    my $reply=&reply("querysend:".&escape($query),$server);
                   2052: 	    $rhash{$server}=$reply;
                   2053: 	}
                   2054: 	else {
                   2055: 	    my $reply=&reply("querysend:".&escape($query).':'.
                   2056: 			     &escape($custom).':'.&escape($customshow),
                   2057: 			     $server);
                   2058: 	    $rhash{$server}=$reply;
                   2059: 	}
1.112     harris41 2060:     }
1.118     harris41 2061:     return \%rhash;
1.240     www      2062: }
                   2063: 
                   2064: # ----------------------------------------- Send log queries and wait for reply
                   2065: 
                   2066: sub log_query {
                   2067:     my ($uname,$udom,$query,%filters)=@_;
                   2068:     my $uhome=&homeserver($uname,$udom);
                   2069:     if ($uhome eq 'no_host') { return 'error: no_host'; }
                   2070:     my $uhost=$hostname{$uhome};
1.241     www      2071:     my $command=&escape(join(':',map{$_.'='.$filters{$_}} keys %filters));
1.240     www      2072:     my $queryid=&reply("querysend:".$query.':'.$udom.':'.$uname.':'.$command,
                   2073:                        $uhome);
                   2074:     unless ($queryid=~/^$uhost\_/) { return 'error: '.$queryid; }
1.242     www      2075:     return get_query_reply($queryid);
                   2076: }
                   2077: 
                   2078: sub get_query_reply {
                   2079:     my $queryid=shift;
1.240     www      2080:     my $replyfile=$perlvar{'lonDaemons'}.'/tmp/'.$queryid;
                   2081:     my $reply='';
                   2082:     for (1..100) {
                   2083: 	sleep 2;
                   2084:         if (-e $replyfile.'.end') {
                   2085: 	    if (my $fh=Apache::File->new($replyfile)) {
                   2086:                $reply.=<$fh>;
                   2087:                $fh->close;
                   2088: 	   } else { return 'error: reply_file_error'; }
1.242     www      2089:            return &unescape($reply);
                   2090: 	}
1.240     www      2091:     }
1.242     www      2092:     return 'timeout:'.$queryid;
1.240     www      2093: }
                   2094: 
                   2095: sub courselog_query {
1.241     www      2096: #
                   2097: # possible filters:
                   2098: # url: url or symb
                   2099: # username
                   2100: # domain
                   2101: # action: view, submit, grade
                   2102: # start: timestamp
                   2103: # end: timestamp
                   2104: #
1.240     www      2105:     my (%filters)=@_;
                   2106:     unless ($ENV{'request.course.id'}) { return 'no_course'; }
1.241     www      2107:     if ($filters{'url'}) {
                   2108: 	$filters{'url'}=&symbclean(&declutter($filters{'url'}));
                   2109:         $filters{'url'}=~s/\.(\w+)$/(\\.\\d+)*\\.$1/;
                   2110:         $filters{'url'}=~s/\.(\w+)\_\_\_/(\\.\\d+)*\\.$1/;
                   2111:     }
1.240     www      2112:     my $cname=$ENV{'course.'.$ENV{'request.course.id'}.'.num'};
                   2113:     my $cdom=$ENV{'course.'.$ENV{'request.course.id'}.'.domain'};
                   2114:     return &log_query($cname,$cdom,'courselog',%filters);
                   2115: }
                   2116: 
                   2117: sub userlog_query {
                   2118:     my ($uname,$udom,%filters)=@_;
                   2119:     return &log_query($uname,$udom,'userlog',%filters);
1.12      www      2120: }
                   2121: 
                   2122: # ------------------------------------------------------------------ Plain Text
                   2123: 
                   2124: sub plaintext {
1.22      www      2125:     my $short=shift;
                   2126:     return $prp{$short};
1.12      www      2127: }
                   2128: 
                   2129: # ----------------------------------------------------------------- Assign Role
                   2130: 
                   2131: sub assignrole {
1.21      www      2132:     my ($udom,$uname,$url,$role,$end,$start)=@_;
                   2133:     my $mrole;
                   2134:     if ($role =~ /^cr\//) {
1.104     www      2135: 	unless (&allowed('ccr',$url)) {
                   2136:            &logthis('Refused custom assignrole: '.
                   2137:              $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
                   2138: 		    $ENV{'user.name'}.' at '.$ENV{'user.domain'});
                   2139:            return 'refused'; 
                   2140:         }
1.21      www      2141:         $mrole='cr';
                   2142:     } else {
1.82      www      2143:         my $cwosec=$url;
1.83      www      2144:         $cwosec=~s/^\/(\w+)\/(\w+)\/.*/$1\/$2/;
1.104     www      2145:         unless (&allowed('c'.$role,$cwosec)) { 
                   2146:            &logthis('Refused assignrole: '.
                   2147:              $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
                   2148: 		    $ENV{'user.name'}.' at '.$ENV{'user.domain'});
                   2149:            return 'refused'; 
                   2150:         }
1.21      www      2151:         $mrole=$role;
                   2152:     }
                   2153:     my $command="encrypt:rolesput:$ENV{'user.domain'}:$ENV{'user.name'}:".
                   2154:                 "$udom:$uname:$url".'_'."$mrole=$role";
1.81      www      2155:     if ($end) { $command.='_'.$end; }
1.21      www      2156:     if ($start) {
                   2157: 	if ($end) { 
1.81      www      2158:            $command.='_'.$start; 
1.21      www      2159:         } else {
1.81      www      2160:            $command.='_0_'.$start;
1.21      www      2161:         }
                   2162:     }
                   2163:     return &reply($command,&homeserver($uname,$udom));
1.169     harris41 2164: }
                   2165: 
                   2166: # -------------------------------------------------- Modify user authentication
1.197     www      2167: # Overrides without validation
                   2168: 
1.169     harris41 2169: sub modifyuserauth {
                   2170:     my ($udom,$uname,$umode,$upass)=@_;
                   2171:     my $uhome=&homeserver($uname,$udom);
1.197     www      2172:     unless (&allowed('mau',$udom)) { return 'refused'; }
                   2173:     &logthis('Call to modify user authentication '.$udom.', '.$uname.', '.
1.272     matthew  2174:              $umode.' by '.$ENV{'user.name'}.' at '.$ENV{'user.domain'}.
                   2175:              ' in domain '.$ENV{'request.role.domain'});  
1.169     harris41 2176:     my $reply=&reply('encrypt:changeuserauth:'.$udom.':'.$uname.':'.$umode.':'.
                   2177: 		     &escape($upass),$uhome);
1.197     www      2178:     &log($ENV{'user.domain'},$ENV{'user.name'},$ENV{'user.home'},
                   2179:         'Authentication changed for '.$udom.', '.$uname.', '.$umode.
                   2180:          '(Remote '.$ENV{'REMOTE_ADDR'}.'): '.$reply);
                   2181:     &log($udom,,$uname,$uhome,
                   2182:         'Authentication changed by '.$ENV{'user.domain'}.', '.
                   2183:                                      $ENV{'user.name'}.', '.$umode.
                   2184:          '(Remote '.$ENV{'REMOTE_ADDR'}.'): '.$reply);
1.169     harris41 2185:     unless ($reply eq 'ok') {
1.197     www      2186:         &logthis('Authentication mode error: '.$reply);
1.169     harris41 2187: 	return 'error: '.$reply;
                   2188:     }   
1.170     harris41 2189:     return 'ok';
1.80      www      2190: }
                   2191: 
1.81      www      2192: # --------------------------------------------------------------- Modify a user
1.80      www      2193: 
1.81      www      2194: sub modifyuser {
1.206     matthew  2195:     my ($udom,    $uname, $uid,
                   2196:         $umode,   $upass, $first,
                   2197:         $middle,  $last,  $gene,
                   2198:         $forceid, $desiredhome)=@_;
1.198     www      2199:     $udom=~s/\W//g;
                   2200:     $uname=~s/\W//g;
1.81      www      2201:     &logthis('Call to modify user '.$udom.', '.$uname.', '.$uid.', '.
1.80      www      2202:              $umode.', '.$first.', '.$middle.', '.
1.206     matthew  2203: 	     $last.', '.$gene.'(forceid: '.$forceid.')'.
                   2204:              (defined($desiredhome) ? ' desiredhome = '.$desiredhome :
                   2205:                                      ' desiredhome not specified'). 
1.272     matthew  2206:              ' by '.$ENV{'user.name'}.' at '.$ENV{'user.domain'}.
                   2207:              ' in domain '.$ENV{'request.role.domain'});
1.230     stredwic 2208:     my $uhome=&homeserver($uname,$udom,'true');
1.80      www      2209: # ----------------------------------------------------------------- Create User
1.81      www      2210:     if (($uhome eq 'no_host') && ($umode) && ($upass)) {
1.80      www      2211:         my $unhome='';
1.209     matthew  2212:         if (defined($desiredhome) && $hostdom{$desiredhome} eq $udom) { 
                   2213:             $unhome = $desiredhome;
                   2214: 	} elsif($ENV{'course.'.$ENV{'request.course.id'}.'.domain'} eq $udom) {
1.80      www      2215: 	    $unhome=$ENV{'course.'.$ENV{'request.course.id'}.'.home'};
1.209     matthew  2216:         } else { # load balancing routine for determining $unhome
1.80      www      2217:             my $tryserver;
1.81      www      2218:             my $loadm=10000000;
1.80      www      2219:             foreach $tryserver (keys %libserv) {
                   2220: 	       if ($hostdom{$tryserver} eq $udom) {
                   2221:                   my $answer=reply('load',$tryserver);
                   2222:                   if (($answer=~/\d+/) && ($answer<$loadm)) {
                   2223: 		      $loadm=$answer;
                   2224:                       $unhome=$tryserver;
                   2225:                   }
                   2226: 	       }
                   2227: 	    }
                   2228:         }
                   2229:         if (($unhome eq '') || ($unhome eq 'no_host')) {
1.206     matthew  2230: 	    return 'error: unable to find a home server for '.$uname.
                   2231:                    ' in domain '.$udom;
1.80      www      2232:         }
                   2233:         my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':'.$umode.':'.
                   2234:                          &escape($upass),$unhome);
                   2235: 	unless ($reply eq 'ok') {
                   2236:             return 'error: '.$reply;
                   2237:         }   
1.230     stredwic 2238:         $uhome=&homeserver($uname,$udom,'true');
1.80      www      2239:         if (($uhome eq '') || ($uhome eq 'no_host') || ($uhome ne $unhome)) {
                   2240: 	    return 'error: verify home';
                   2241:         }
1.209     matthew  2242:     }   # End of creation of new user
1.80      www      2243: # ---------------------------------------------------------------------- Add ID
                   2244:     if ($uid) {
                   2245:        $uid=~tr/A-Z/a-z/;
                   2246:        my %uidhash=&idrget($udom,$uname);
1.196     www      2247:        if (($uidhash{$uname}) && ($uidhash{$uname}!~/error\:/) 
                   2248:          && (!$forceid)) {
1.80      www      2249: 	  unless ($uid eq $uidhash{$uname}) {
                   2250: 	      return 'error: mismatch '.$uidhash{$uname}.' versus '.$uid;
                   2251:           }
                   2252:        } else {
                   2253: 	  &idput($udom,($uname => $uid));
                   2254:        }
                   2255:     }
                   2256: # -------------------------------------------------------------- Add names, etc
1.134     albertel 2257:     my %names=&get('environment',
                   2258: 		   ['firstname','middlename','lastname','generation'],
                   2259: 		   $udom,$uname);
1.207     albertel 2260:     if ($names{'firstname'} =~ m/^error:.*/) { %names=(); }
1.134     albertel 2261:     if ($first)  { $names{'firstname'}  = $first; }
                   2262:     if ($middle) { $names{'middlename'} = $middle; }
                   2263:     if ($last)   { $names{'lastname'}   = $last; }
                   2264:     if ($gene)   { $names{'generation'} = $gene; }
                   2265:     my $reply = &put('environment', \%names, $udom,$uname);
                   2266:     if ($reply ne 'ok') { return 'error: '.$reply; }
1.81      www      2267:     &logthis('Success modifying user '.$udom.', '.$uname.', '.$uid.', '.
1.80      www      2268:              $umode.', '.$first.', '.$middle.', '.
                   2269: 	     $last.', '.$gene.' by '.
                   2270:              $ENV{'user.name'}.' at '.$ENV{'user.domain'});
1.134     albertel 2271:     return 'ok';
1.80      www      2272: }
                   2273: 
1.81      www      2274: # -------------------------------------------------------------- Modify student
1.80      www      2275: 
1.81      www      2276: sub modifystudent {
                   2277:     my ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$usec,
1.209     matthew  2278:         $end,$start,$forceid,$desiredhome)=@_;
1.81      www      2279:     my $cid='';
                   2280:     unless ($cid=$ENV{'request.course.id'}) {
1.80      www      2281: 	return 'not_in_class';
                   2282:     }
                   2283: # --------------------------------------------------------------- Make the user
1.81      www      2284:     my $reply=&modifyuser
1.209     matthew  2285: 	($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$forceid,
                   2286:          $desiredhome);
1.80      www      2287:     unless ($reply eq 'ok') { return $reply; }
1.81      www      2288:     my $uhome=&homeserver($uname,$udom);
                   2289:     if (($uhome eq '') || ($uhome eq 'no_host')) { 
                   2290: 	return 'error: no such user';
                   2291:     }
1.80      www      2292: # -------------------------------------------------- Add student to course list
1.160     www      2293:     $reply=critical('put:'.$ENV{'course.'.$cid.'.domain'}.':'.
1.81      www      2294: 	              $ENV{'course.'.$cid.'.num'}.':classlist:'.
                   2295:                       &escape($uname.':'.$udom).'='.
                   2296:                       &escape($end.':'.$start),
                   2297: 	              $ENV{'course.'.$cid.'.home'});
                   2298:     unless (($reply eq 'ok') || ($reply eq 'delayed')) {
                   2299: 	return 'error: '.$reply;
                   2300:     }
1.80      www      2301: # ---------------------------------------------------- Add student role to user
1.83      www      2302:     my $uurl='/'.$cid;
1.81      www      2303:     $uurl=~s/\_/\//g;
                   2304:     if ($usec) {
                   2305: 	$uurl.='/'.$usec;
                   2306:     }
                   2307:     return &assignrole($udom,$uname,$uurl,'st',$end,$start);
1.21      www      2308: }
                   2309: 
1.84      www      2310: # ------------------------------------------------- Write to course preferences
                   2311: 
                   2312: sub writecoursepref {
                   2313:     my ($courseid,%prefs)=@_;
                   2314:     $courseid=~s/^\///;
                   2315:     $courseid=~s/\_/\//g;
                   2316:     my ($cdomain,$cnum)=split(/\//,$courseid);
                   2317:     my $chome=homeserver($cnum,$cdomain);
                   2318:     if (($chome eq '') || ($chome eq 'no_host')) { 
                   2319: 	return 'error: no such course';
                   2320:     }
                   2321:     my $cstring='';
1.191     harris41 2322:     foreach (keys %prefs) {
1.84      www      2323: 	$cstring.=escape($_).'='.escape($prefs{$_}).'&';
1.191     harris41 2324:     }
1.84      www      2325:     $cstring=~s/\&$//;
                   2326:     return reply('put:'.$cdomain.':'.$cnum.':environment:'.$cstring,$chome);
                   2327: }
                   2328: 
                   2329: # ---------------------------------------------------------- Make/modify course
                   2330: 
                   2331: sub createcourse {
1.271     www      2332:     my ($udom,$description,$url,$course_server,$nonstandard)=@_;
1.84      www      2333:     $url=&declutter($url);
                   2334:     my $cid='';
1.264     matthew  2335:     unless (&allowed('ccc',$udom)) {
1.84      www      2336:         return 'refused';
                   2337:     }
                   2338: # ------------------------------------------------------------------- Create ID
                   2339:    my $uname=substr($$.time,0,5).unpack("H8",pack("I32",time)).
                   2340:        unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
                   2341: # ----------------------------------------------- Make sure that does not exist
1.230     stredwic 2342:    my $uhome=&homeserver($uname,$udom,'true');
1.84      www      2343:    unless (($uhome eq '') || ($uhome eq 'no_host')) {
                   2344:        $uname=substr($$.time,0,5).unpack("H8",pack("I32",time)).
                   2345:         unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
1.230     stredwic 2346:        $uhome=&homeserver($uname,$udom,'true');       
1.84      www      2347:        unless (($uhome eq '') || ($uhome eq 'no_host')) {
                   2348:            return 'error: unable to generate unique course-ID';
                   2349:        } 
                   2350:    }
1.264     matthew  2351: # ------------------------------------------------ Check supplied server name
                   2352:     $course_server = $ENV{'user.homeserver'} if (! defined($course_server));
                   2353:     if (! exists($libserv{$course_server})) {
                   2354:         return 'error:bad server name '.$course_server;
                   2355:     }
1.84      www      2356: # ------------------------------------------------------------- Make the course
                   2357:     my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':none::',
1.264     matthew  2358:                       $course_server);
1.84      www      2359:     unless ($reply eq 'ok') { return 'error: '.$reply; }
1.230     stredwic 2360:     $uhome=&homeserver($uname,$udom,'true');
1.84      www      2361:     if (($uhome eq '') || ($uhome eq 'no_host')) { 
                   2362: 	return 'error: no such course';
                   2363:     }
1.271     www      2364: # ----------------------------------------------------------------- Course made
                   2365:     my $topurl=$url;
                   2366:     unless ($nonstandard) {
                   2367: # ------------------------------------------ For standard courses, make top url
                   2368:         my $mapurl=&clutter($url);
1.278     www      2369:         if ($mapurl eq '/res/') { $mapurl=''; }
1.271     www      2370:         $ENV{'form.initmap'}=(<<ENDINITMAP);
                   2371: <map>
                   2372: <resource id="1" type="start"></resource>
                   2373: <resource id="2" src="$mapurl"></resource>
                   2374: <resource id="3" type="finish"></resource>
                   2375: <link index="1" from="1" to="2"></link>
                   2376: <link index="2" from="2" to="3"></link>
                   2377: </map>
                   2378: ENDINITMAP
                   2379:         $topurl=&declutter(
                   2380:         &finishuserfileupload($uname,$udom,$uhome,'initmap','default.sequence')
                   2381:                           );
                   2382:     }
                   2383: # ----------------------------------------------------------- Write preferences
1.84      www      2384:     &writecoursepref($udom.'_'.$uname,
                   2385:                      ('description' => $description,
1.271     www      2386:                       'url'         => $topurl));
1.84      www      2387:     return '/'.$udom.'/'.$uname;
                   2388: }
                   2389: 
1.21      www      2390: # ---------------------------------------------------------- Assign Custom Role
                   2391: 
                   2392: sub assigncustomrole {
                   2393:     my ($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start)=@_;
                   2394:     return &assignrole($udom,$uname,$url,'cr/'.$rdom.'/'.$rnam.'/'.$rolename,
                   2395:                        $end,$start);
                   2396: }
                   2397: 
                   2398: # ----------------------------------------------------------------- Revoke Role
                   2399: 
                   2400: sub revokerole {
                   2401:     my ($udom,$uname,$url,$role)=@_;
                   2402:     my $now=time;
                   2403:     return &assignrole($udom,$uname,$url,$role,$now);
                   2404: }
                   2405: 
                   2406: # ---------------------------------------------------------- Revoke Custom Role
                   2407: 
                   2408: sub revokecustomrole {
                   2409:     my ($udom,$uname,$url,$rdom,$rnam,$rolename)=@_;
                   2410:     my $now=time;
                   2411:     return &assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$now);
1.17      www      2412: }
                   2413: 
                   2414: # ------------------------------------------------------------ Directory lister
                   2415: 
                   2416: sub dirlist {
1.253     stredwic 2417:     my ($uri,$userdomain,$username,$alternateDirectoryRoot)=@_;
                   2418: 
1.18      www      2419:     $uri=~s/^\///;
                   2420:     $uri=~s/\/$//;
1.253     stredwic 2421:     my ($udom, $uname);
                   2422:     (undef,$udom,$uname)=split(/\//,$uri);
                   2423:     if(defined($userdomain)) {
                   2424:         $udom = $userdomain;
                   2425:     }
                   2426:     if(defined($username)) {
                   2427:         $uname = $username;
                   2428:     }
                   2429: 
                   2430:     my $dirRoot = $perlvar{'lonDocRoot'};
                   2431:     if(defined($alternateDirectoryRoot)) {
                   2432:         $dirRoot = $alternateDirectoryRoot;
                   2433:         $dirRoot =~ s/\/$//;
                   2434:     }
                   2435: 
                   2436:     if($udom) {
                   2437:         if($uname) {
                   2438:             my $listing=reply('ls:'.$dirRoot.'/'.$uri,
                   2439:                               homeserver($uname,$udom));
                   2440:             return split(/:/,$listing);
                   2441:         } elsif(!defined($alternateDirectoryRoot)) {
                   2442:             my $tryserver;
                   2443:             my %allusers=();
                   2444:             foreach $tryserver (keys %libserv) {
                   2445:                 if($hostdom{$tryserver} eq $udom) {
                   2446:                     my $listing=reply('ls:'.$perlvar{'lonDocRoot'}.'/res/'.
                   2447:                                       $udom, $tryserver);
                   2448:                     if (($listing ne 'no_such_dir') && ($listing ne 'empty')
                   2449:                         && ($listing ne 'con_lost')) {
                   2450:                         foreach (split(/:/,$listing)) {
                   2451:                             my ($entry,@stat)=split(/&/,$_);
                   2452:                             $allusers{$entry}=1;
                   2453:                         }
                   2454:                     }
1.191     harris41 2455:                 }
1.253     stredwic 2456:             }
                   2457:             my $alluserstr='';
                   2458:             foreach (sort keys %allusers) {
                   2459:                 $alluserstr.=$_.'&user:';
                   2460:             }
                   2461:             $alluserstr=~s/:$//;
                   2462:             return split(/:/,$alluserstr);
                   2463:         } else {
                   2464:             my @emptyResults = ();
                   2465:             push(@emptyResults, 'missing user name');
                   2466:             return split(':',@emptyResults);
                   2467:         }
                   2468:     } elsif(!defined($alternateDirectoryRoot)) {
                   2469:         my $tryserver;
                   2470:         my %alldom=();
                   2471:         foreach $tryserver (keys %libserv) {
                   2472:             $alldom{$hostdom{$tryserver}}=1;
                   2473:         }
                   2474:         my $alldomstr='';
                   2475:         foreach (sort keys %alldom) {
                   2476:             $alldomstr.=$perlvar{'lonDocRoot'}.'/res/'.$_.'&domain:';
                   2477:         }
                   2478:         $alldomstr=~s/:$//;
                   2479:         return split(/:/,$alldomstr);       
                   2480:     } else {
                   2481:         my @emptyResults = ();
                   2482:         push(@emptyResults, 'missing domain');
                   2483:         return split(':',@emptyResults);
1.275     stredwic 2484:     }
                   2485: }
                   2486: 
                   2487: # --------------------------------------------- GetFileTimestamp
                   2488: # This function utilizes dirlist and returns the date stamp for
                   2489: # when it was last modified.  It will also return an error of -1
                   2490: # if an error occurs
                   2491: 
                   2492: sub GetFileTimestamp {
                   2493:     my ($studentDomain,$studentName,$filename,$root)=@_;
                   2494:     $studentDomain=~s/\W//g;
                   2495:     $studentName=~s/\W//g;
                   2496:     my $subdir=$studentName.'__';
                   2497:     $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
                   2498:     my $proname="$studentDomain/$subdir/$studentName";
                   2499:     $proname .= '/'.$filename;
                   2500:     my @dir = &Apache::lonnet::dirlist($proname, $studentDomain, $studentName,
                   2501:                                        $root);
                   2502:     my $fileStat = $dir[0];
                   2503:     my @stats = split('&', $fileStat);
                   2504:     if($stats[0] ne 'empty' && $stats[0] ne 'no_such_dir') {
                   2505:         return $stats[9];
                   2506:     } else {
                   2507:         return -1;
1.253     stredwic 2508:     }
1.26      www      2509: }
                   2510: 
                   2511: # -------------------------------------------------------- Value of a Condition
                   2512: 
1.40      www      2513: sub directcondval {
                   2514:     my $number=shift;
                   2515:     if ($ENV{'user.state.'.$ENV{'request.course.id'}}) {
                   2516:        return substr($ENV{'user.state.'.$ENV{'request.course.id'}},$number,1);
                   2517:     } else {
                   2518:        return 2;
                   2519:     }
                   2520: }
                   2521: 
1.26      www      2522: sub condval {
                   2523:     my $condidx=shift;
                   2524:     my $result=0;
1.54      www      2525:     my $allpathcond='';
1.191     harris41 2526:     foreach (split(/\|/,$condidx)) {
1.54      www      2527:        if (defined($ENV{'acc.cond.'.$ENV{'request.course.id'}.'.'.$_})) {
                   2528: 	   $allpathcond.=
                   2529:                '('.$ENV{'acc.cond.'.$ENV{'request.course.id'}.'.'.$_}.')|';
                   2530:        }
1.191     harris41 2531:     }
1.54      www      2532:     $allpathcond=~s/\|$//;
1.33      www      2533:     if ($ENV{'request.course.id'}) {
1.54      www      2534:        if ($allpathcond) {
1.26      www      2535:           my $operand='|';
                   2536: 	  my @stack;
1.191     harris41 2537:            foreach ($allpathcond=~/(\d+|\(|\)|\&|\|)/g) {
1.26      www      2538:               if ($_ eq '(') {
                   2539:                  push @stack,($operand,$result)
                   2540:               } elsif ($_ eq ')') {
                   2541:                   my $before=pop @stack;
                   2542: 		  if (pop @stack eq '&') {
                   2543: 		      $result=$result>$before?$before:$result;
                   2544:                   } else {
                   2545:                       $result=$result>$before?$result:$before;
                   2546:                   }
                   2547:               } elsif (($_ eq '&') || ($_ eq '|')) {
                   2548:                   $operand=$_;
                   2549:               } else {
1.40      www      2550:                   my $new=directcondval($_);
1.26      www      2551:                   if ($operand eq '&') {
                   2552:                      $result=$result>$new?$new:$result;
                   2553:                   } else {
                   2554:                      $result=$result>$new?$result:$new;
1.191     harris41 2555:                   }
1.26      www      2556:               }
1.191     harris41 2557:           }
1.26      www      2558:        }
                   2559:     }
                   2560:     return $result;
1.279     www      2561: }
                   2562: 
                   2563: # ---------------------------------------------------- Devalidate courseresdata
                   2564: 
                   2565: sub devalidatecourseresdata {
                   2566:     my ($coursenum,$coursedomain)=@_;
                   2567:     my $hashid=$coursenum.':'.$coursedomain;
                   2568:     delete $courseresdatacache{$hashid.'.time'};
1.28      www      2569: }
                   2570: 
1.200     www      2571: # --------------------------------------------------- Course Resourcedata Query
                   2572: 
                   2573: sub courseresdata {
                   2574:     my ($coursenum,$coursedomain,@which)=@_;
                   2575:     my $coursehom=&homeserver($coursenum,$coursedomain);
                   2576:     my $hashid=$coursenum.':'.$coursedomain;
1.250     albertel 2577:     my $dodump=0;
                   2578:     if (!defined($courseresdatacache{$hashid.'.time'})) {
                   2579: 	$dodump=1;
                   2580:     } else {
                   2581: 	if (time-$courseresdatacache{$hashid.'.time'}>300) { $dodump=1; }
                   2582:     }
                   2583:     if ($dodump) {
1.251     albertel 2584: 	my %dumpreply=&dump('resourcedata',$coursedomain,$coursenum);
                   2585: 	my ($tmp) = keys(%dumpreply);
                   2586: 	if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
                   2587: 	    $courseresdatacache{$hashid.'.time'}=time;
                   2588: 	    $courseresdatacache{$hashid}=\%dumpreply;
1.250     albertel 2589: 	}
                   2590:     }
1.251     albertel 2591:     foreach my $item (@which) {
1.287     albertel 2592: 	if (defined($courseresdatacache{$hashid}->{$item})) {
1.251     albertel 2593: 	    return $courseresdatacache{$hashid}->{$item};
                   2594: 	}
1.250     albertel 2595:     }
                   2596:     return '';
1.200     www      2597: }
                   2598: 
1.28      www      2599: # --------------------------------------------------------- Value of a Variable
                   2600: 
1.58      www      2601: sub EXT {
1.282     albertel 2602:     my ($varname,$symbparm,$udom,$uname,)=@_;
1.218     albertel 2603: 
1.68      www      2604:     unless ($varname) { return ''; }
1.218     albertel 2605: 
                   2606:     #get real user name/domain, courseid and symb
                   2607:     my $courseid;
                   2608:     if (!($uname && $udom)) {
                   2609:       (my $cursymb,$courseid,$udom,$uname)=&Apache::lonxml::whichuser();
                   2610:       if (!$symbparm) {	$symbparm=$cursymb; }
                   2611:     } else {
                   2612: 	$courseid=$ENV{'request.course.id'};
                   2613:     }
                   2614: 
1.48      www      2615:     my ($realm,$space,$qualifier,@therest)=split(/\./,$varname);
                   2616:     my $rest;
                   2617:     if ($therest[0]) {
                   2618:        $rest=join('.',@therest);
                   2619:     } else {
                   2620:        $rest='';
                   2621:     }
1.57      www      2622:     my $qualifierrest=$qualifier;
                   2623:     if ($rest) { $qualifierrest.='.'.$rest; }
                   2624:     my $spacequalifierrest=$space;
                   2625:     if ($qualifierrest) { $spacequalifierrest.='.'.$qualifierrest; }
1.28      www      2626:     if ($realm eq 'user') {
1.48      www      2627: # --------------------------------------------------------------- user.resource
                   2628: 	if ($space eq 'resource') {
1.218     albertel 2629: 	    my %restored=&restore(undef,undef,$udom,$uname);
1.57      www      2630:             return $restored{$qualifierrest};
1.48      www      2631: # ----------------------------------------------------------------- user.access
                   2632:         } elsif ($space eq 'access') {
1.218     albertel 2633: 	    # FIXME - not supporting calls for a specific user
1.48      www      2634:             return &allowed($qualifier,$rest);
                   2635: # ------------------------------------------ user.preferences, user.environment
                   2636:         } elsif (($space eq 'preferences') || ($space eq 'environment')) {
1.218     albertel 2637: 	    if (($uname eq $ENV{'user.name'}) &&
                   2638: 		($udom eq $ENV{'user.domain'})) {
                   2639: 		return $ENV{join('.',('environment',$qualifierrest))};
                   2640: 	    } else {
                   2641: 		my %returnhash=&userenvironment($udom,$uname,$qualifierrest);
                   2642: 		return $returnhash{$qualifierrest};
                   2643: 	    }
1.48      www      2644: # ----------------------------------------------------------------- user.course
                   2645:         } elsif ($space eq 'course') {
1.218     albertel 2646: 	    # FIXME - not supporting calls for a specific user
1.48      www      2647:             return $ENV{join('.',('request.course',$qualifier))};
                   2648: # ------------------------------------------------------------------- user.role
                   2649:         } elsif ($space eq 'role') {
1.218     albertel 2650: 	    # FIXME - not supporting calls for a specific user
1.48      www      2651:             my ($role,$where)=split(/\./,$ENV{'request.role'});
                   2652:             if ($qualifier eq 'value') {
                   2653: 		return $role;
                   2654:             } elsif ($qualifier eq 'extent') {
                   2655:                 return $where;
                   2656:             }
                   2657: # ----------------------------------------------------------------- user.domain
                   2658:         } elsif ($space eq 'domain') {
1.218     albertel 2659:             return $udom;
1.48      www      2660: # ------------------------------------------------------------------- user.name
                   2661:         } elsif ($space eq 'name') {
1.218     albertel 2662:             return $uname;
1.48      www      2663: # ---------------------------------------------------- Any other user namespace
1.29      www      2664:         } else {
1.48      www      2665:             my $item=($rest)?$qualifier.'.'.$rest:$qualifier;
1.131     albertel 2666:             my %reply=&get($space,[$item]);
1.48      www      2667:             return $reply{$item};
                   2668:         }
1.236     www      2669:     } elsif ($realm eq 'query') {
                   2670: # ---------------------------------------------- pull stuff out of query string
                   2671:         &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},[$space]);
                   2672: 	return $ENV{'form.'.$space}; 
                   2673:    } elsif ($realm eq 'request') {
1.48      www      2674: # ------------------------------------------------------------- request.browser
                   2675:         if ($space eq 'browser') {
                   2676: 	    return $ENV{'browser.'.$qualifier};
1.57      www      2677: # ------------------------------------------------------------ request.filename
                   2678:         } else {
                   2679:             return $ENV{'request.'.$spacequalifierrest};
1.29      www      2680:         }
1.28      www      2681:     } elsif ($realm eq 'course') {
1.48      www      2682: # ---------------------------------------------------------- course.description
1.218     albertel 2683:         return $ENV{'course.'.$courseid.'.'.$spacequalifierrest};
1.57      www      2684:     } elsif ($realm eq 'resource') {
1.165     www      2685: 
1.218     albertel 2686: 	if ($courseid eq $ENV{'request.course.id'}) {
1.165     www      2687: 
1.218     albertel 2688: 	    #print '<br>'.$space.' - '.$qualifier.' - '.$spacequalifierrest;
1.165     www      2689: 
1.60      www      2690: # ----------------------------------------------------- Cascading lookup scheme
1.218     albertel 2691: 	    if (!$symbparm) { $symbparm=&symbread(); }
                   2692: 	    my $symbp=$symbparm;
                   2693: 	    my $mapp=(split(/\_\_\_/,$symbp))[0];
                   2694: 
                   2695: 	    my $symbparm=$symbp.'.'.$spacequalifierrest;
                   2696: 	    my $mapparm=$mapp.'___(all).'.$spacequalifierrest;
                   2697: 
                   2698: 	    my $section;
                   2699: 	    if (($ENV{'user.name'} eq $uname) &&
                   2700: 		($ENV{'user.domain'} eq $udom)) {
1.255     albertel 2701: 		$section=$ENV{'request.course.sec'};
1.218     albertel 2702: 	    } else {
                   2703: 		$section=&usection($udom,$uname,$courseid);
                   2704: 	    }
                   2705: 
                   2706: 	    my $seclevel=$courseid.'.['.$section.'].'.$spacequalifierrest;
                   2707: 	    my $seclevelr=$courseid.'.['.$section.'].'.$symbparm;
                   2708: 	    my $seclevelm=$courseid.'.['.$section.'].'.$mapparm;
                   2709: 
                   2710: 	    my $courselevel=$courseid.'.'.$spacequalifierrest;
                   2711: 	    my $courselevelr=$courseid.'.'.$symbparm;
                   2712: 	    my $courselevelm=$courseid.'.'.$mapparm;
1.69      www      2713: 
1.60      www      2714: # ----------------------------------------------------------- first, check user
1.218     albertel 2715: 	    my %resourcedata=&get('resourcedata',
                   2716: 				  [$courselevelr,$courselevelm,$courselevel],
                   2717: 				 $udom,$uname);
                   2718: 	    if (($resourcedata{$courselevelr}!~/^error\:/) &&
                   2719: 		($resourcedata{$courselevelr}!~/^con_lost/)) {
                   2720: 
                   2721: 		if ($resourcedata{$courselevelr}) {
                   2722: 		    return $resourcedata{$courselevelr}; }
                   2723: 		if ($resourcedata{$courselevelm}) {
                   2724: 		    return $resourcedata{$courselevelm}; }
                   2725: 		if ($resourcedata{$courselevel}) {
                   2726: 		    return $resourcedata{$courselevel}; }
                   2727: 	    } else {
                   2728: 		if ($resourcedata{$courselevelr}!~/No such file/) {
                   2729: 		    &logthis("<font color=blue>WARNING:".
                   2730: 			     " Trying to get resource data for ".
                   2731: 			     $uname." at ".$udom.": ".
                   2732: 			     $resourcedata{$courselevelr}."</font>");
                   2733: 		}
                   2734: 	    }
1.95      www      2735: 
1.60      www      2736: # -------------------------------------------------------- second, check course
1.96      www      2737: 
1.218     albertel 2738: 	    my $coursereply=&courseresdata($ENV{'course.'.$courseid.'.num'},
                   2739: 					  $ENV{'course.'.$courseid.'.domain'},
                   2740: 					  ($seclevelr,$seclevelm,$seclevel,
                   2741: 					   $courselevelr,$courselevelm,
                   2742: 					   $courselevel));
1.287     albertel 2743: 	    if (defined($coursereply)) { return $coursereply; }
1.200     www      2744: 
1.60      www      2745: # ------------------------------------------------------ third, check map parms
1.218     albertel 2746: 	    my %parmhash=();
                   2747: 	    my $thisparm='';
                   2748: 	    if (tie(%parmhash,'GDBM_File',
                   2749: 		    $ENV{'request.course.fn'}.'_parms.db',
1.256     albertel 2750: 		    &GDBM_READER(),0640)) {
1.218     albertel 2751: 		$thisparm=$parmhash{$symbparm};
                   2752: 		untie(%parmhash);
                   2753: 	    }
                   2754: 	    if ($thisparm) { return $thisparm; }
                   2755: 	}
1.60      www      2756: # --------------------------------------------- last, look in resource metadata
1.71      www      2757: 
1.218     albertel 2758: 	$spacequalifierrest=~s/\./\_/;
1.282     albertel 2759: 	my $filename;
                   2760: 	if (!$symbparm) { $symbparm=&symbread(); }
                   2761: 	if ($symbparm) {
                   2762: 	    $filename=(split(/\_\_\_/,$symbparm))[2];
                   2763: 	} else {
                   2764: 	    $filename=$ENV{'request.filename'};
                   2765: 	}
                   2766: 	my $metadata=&metadata($filename,$spacequalifierrest);
1.288     albertel 2767: 	if (defined($metadata)) { return $metadata; }
1.282     albertel 2768: 	$metadata=&metadata($filename,'parameter_'.$spacequalifierrest);
1.288     albertel 2769: 	if (defined($metadata)) { return $metadata; }
1.142     www      2770: 
1.145     www      2771: # ------------------------------------------------------------------ Cascade up
1.218     albertel 2772: 	unless ($space eq '0') {
                   2773: 	    my ($part,$id)=split(/\_/,$space);
                   2774: 	    if ($id) {
                   2775: 		my $partgeneral=&EXT('resource.'.$part.'.'.$qualifierrest,
                   2776: 				     $symbparm,$udom,$uname);
1.288     albertel 2777: 		if (defined($partgeneral)) { return $partgeneral; }
1.218     albertel 2778: 	    } else {
                   2779: 		my $resourcegeneral=&EXT('resource.0.'.$qualifierrest,
                   2780: 					 $symbparm,$udom,$uname);
1.288     albertel 2781: 		if (defined($resourcegeneral)) { return $resourcegeneral; }
1.218     albertel 2782: 	    }
                   2783: 	}
1.71      www      2784: 
1.48      www      2785: # ---------------------------------------------------- Any other user namespace
                   2786:     } elsif ($realm eq 'environment') {
                   2787: # ----------------------------------------------------------------- environment
1.219     albertel 2788: 	if (($uname eq $ENV{'user.name'})&&($udom eq $ENV{'user.domain'})) {
                   2789: 	    return $ENV{'environment.'.$spacequalifierrest};
                   2790: 	} else {
                   2791: 	    my %returnhash=&userenvironment($udom,$uname,
                   2792: 					    $spacequalifierrest);
                   2793: 	    return $returnhash{$spacequalifierrest};
                   2794: 	}
1.28      www      2795:     } elsif ($realm eq 'system') {
1.48      www      2796: # ----------------------------------------------------------------- system.time
                   2797: 	if ($space eq 'time') {
                   2798: 	    return time;
                   2799:         }
1.28      www      2800:     }
1.48      www      2801:     return '';
1.61      www      2802: }
                   2803: 
1.71      www      2804: # ---------------------------------------------------------------- Get metadata
                   2805: 
                   2806: sub metadata {
1.176     www      2807:     my ($uri,$what,$liburi,$prefix,$depthcount)=@_;
1.78      www      2808: 
1.71      www      2809:     $uri=&declutter($uri);
1.288     albertel 2810:     # if it is a non metadata possible uri return quickly
                   2811:     if (($uri eq '') || ($uri =~ m|^/*adm/|) || ($uri =~ m|/$|) ||
                   2812: 	($uri =~ m|/.meta$|)) {
                   2813: 	return '';
                   2814:     }
1.73      www      2815:     my $filename=$uri;
                   2816:     $uri=~s/\.meta$//;
1.172     www      2817: #
                   2818: # Is the metadata already cached?
1.177     www      2819: # Look at timestamp of caching
1.172     www      2820: # Everything is cached by the main uri, libraries are never directly cached
                   2821: #
1.277     albertel 2822:     unless (abs($metacache{$uri.':cachedtimestamp'}-time)<600 && !defined($liburi)) {
1.172     www      2823: #
                   2824: # Is this a recursive call for a library?
                   2825: #
1.171     www      2826:         if ($liburi) {
                   2827: 	    $liburi=&declutter($liburi);
                   2828:             $filename=$liburi;
                   2829:         }
1.140     www      2830:         my %metathesekeys=();
1.73      www      2831:         unless ($filename=~/\.meta$/) { $filename.='.meta'; }
                   2832: 	my $metastring=&getfile($perlvar{'lonDocRoot'}.'/res/'.$filename);
1.208     albertel 2833:         my $parser=HTML::LCParser->new(\$metastring);
1.71      www      2834:         my $token;
1.140     www      2835:         undef %metathesekeys;
1.71      www      2836:         while ($token=$parser->get_token) {
                   2837:            if ($token->[0] eq 'S') {
1.135     www      2838: 	     if (defined($token->[2]->{'package'})) {
1.172     www      2839: #
                   2840: # This is a package - get package info
                   2841: #
1.136     www      2842: 	      my $package=$token->[2]->{'package'};
                   2843: 	      my $keyroot='';
1.172     www      2844:               if ($prefix) {
1.276     albertel 2845: 		  $keyroot.=$prefix;
1.172     www      2846:               } else {
                   2847:                 if (defined($token->[2]->{'part'})) { 
                   2848:                    $keyroot.='_'.$token->[2]->{'part'}; 
                   2849: 	        }
1.136     www      2850: 	      }
                   2851:               if (defined($token->[2]->{'id'})) { 
1.165     www      2852:                  $keyroot.='_'.$token->[2]->{'id'}; 
1.136     www      2853: 	      }
                   2854:               if ($metacache{$uri.':packages'}) {
                   2855:                  $metacache{$uri.':packages'}.=','.$package.$keyroot;
                   2856:               } else {
                   2857:                  $metacache{$uri.':packages'}=$package.$keyroot;
                   2858: 	      }
1.191     harris41 2859:               foreach (keys %packagetab) {
1.137     www      2860: 		  if ($_=~/^$package\&/) {
                   2861: 		      my ($pack,$name,$subp)=split(/\&/,$_);
1.139     www      2862:                       my $value=$packagetab{$_};
1.144     www      2863: 		      my $part=$keyroot;
                   2864:                       $part=~s/^\_//;
1.139     www      2865:                       if ($subp eq 'display') {
                   2866: 			  $value.=' [Part: '.$part.']';
                   2867:                       }
                   2868:                       my $unikey='parameter'.$keyroot.'_'.$name;
1.140     www      2869:                       $metathesekeys{$unikey}=1;
1.144     www      2870:                       $metacache{$uri.':'.$unikey.'.part'}=$part;
1.141     www      2871:                       unless 
                   2872:                        (defined($metacache{$uri.':'.$unikey.'.'.$subp})) {
                   2873:                          $metacache{$uri.':'.$unikey.'.'.$subp}=$value;
                   2874: 		      }
1.137     www      2875:                   }
1.191     harris41 2876:               }
1.135     www      2877:              } else {
1.172     www      2878: #
                   2879: # This is not a package - some other kind of start tag
                   2880: # 
1.175     www      2881:               my $entry=$token->[1];
1.176     www      2882:               my $unikey;
                   2883:               if ($entry eq 'import') {
                   2884:                  $unikey='';
                   2885:               } else {
                   2886:                  $unikey=$entry;
                   2887: 	      }
1.172     www      2888:               if ($prefix) {
1.176     www      2889: 		  $unikey.=$prefix;
1.172     www      2890:               } else {
                   2891:                 if (defined($token->[2]->{'part'})) { 
                   2892:                    $unikey.='_'.$token->[2]->{'part'}; 
                   2893: 	        }
1.136     www      2894: 	      }
                   2895:               if (defined($token->[2]->{'id'})) { 
1.165     www      2896:                  $unikey.='_'.$token->[2]->{'id'}; 
1.71      www      2897: 	      }
1.175     www      2898: 
                   2899:              if ($entry eq 'import') {
                   2900: #
                   2901: # Importing a library here
1.176     www      2902: #                
                   2903:                  if ($depthcount<20) {
1.276     albertel 2904: 		     my $location=$parser->get_text('/import');
                   2905: 		     my $dir=$filename;
                   2906: 		     $dir=~s|[^/]*$||;
                   2907: 		     $location=&filelocation($dir,$location);
                   2908: 		     foreach (sort(split(/\,/,&metadata($uri,'keys',
                   2909: 							$location,$unikey,
                   2910: 							$depthcount+1)))) {
1.177     www      2911:                          $metathesekeys{$_}=1;
1.191     harris41 2912: 		     }
1.176     www      2913: 		 }
1.175     www      2914:              } else { 
                   2915: 
1.72      www      2916:               if (defined($token->[2]->{'name'})) { 
1.71      www      2917:                  $unikey.='_'.$token->[2]->{'name'}; 
                   2918: 	      }
1.140     www      2919:               $metathesekeys{$unikey}=1;
1.191     harris41 2920:               foreach (@{$token->[3]}) {
1.71      www      2921: 		  $metacache{$uri.':'.$unikey.'.'.$_}=$token->[2]->{$_};
1.191     harris41 2922:               }
1.78      www      2923:               unless (
1.208     albertel 2924:                  $metacache{$uri.':'.$unikey}=&HTML::Entities::decode($parser->get_text('/'.$entry))
1.78      www      2925: 		      ) { $metacache{$uri.':'.$unikey}=
                   2926: 			      $metacache{$uri.':'.$unikey.'.default'};
                   2927: 		      }
1.172     www      2928: # end of not-a-package not-a-library import
1.175     www      2929: 	   }
1.172     www      2930: # end of not-a-package start tag
                   2931: 	  }
                   2932: # the next is the end of "start tag"
1.140     www      2933: 	 }
1.71      www      2934:        }
1.274     albertel 2935:        $metacache{$uri.':keys'}=join(',',keys %metathesekeys);
1.261     albertel 2936: 	&metadata_generate_part0(\%metathesekeys,\%metacache,$uri);
1.274     albertel 2937:        $metacache{$uri.':allpossiblekeys'}=join(',',keys %metathesekeys);
1.174     www      2938:        $metacache{$uri.':cachedtimestamp'}=time;
1.177     www      2939: # this is the end of "was not already recently cached
1.71      www      2940:     }
                   2941:     return $metacache{$uri.':'.$what};
1.261     albertel 2942: }
                   2943: 
                   2944: sub metadata_generate_part0 {
                   2945:     my ($metadata,$metacache,$uri) = @_;
                   2946:     my %allnames;
                   2947:     foreach my $metakey (sort keys %$metadata) {
                   2948: 	if ($metakey=~/^parameter\_(.*)/) {
                   2949: 	  my $part=$$metacache{$uri.':'.$metakey.'.part'};
                   2950: 	  my $name=$$metacache{$uri.':'.$metakey.'.name'};
                   2951: 	  if (! exists($$metadata{'parameter_0_'.$name})) {
                   2952: 	    $allnames{$name}=$part;
                   2953: 	  }
                   2954: 	}
                   2955:     }
                   2956:     foreach my $name (keys(%allnames)) {
                   2957:       $$metadata{"parameter_0_$name"}=1;
                   2958:       my $key="$uri:parameter_0_$name";
                   2959:       $$metacache{"$key.part"}='0';
                   2960:       $$metacache{"$key.name"}=$name;
                   2961:       $$metacache{"$key.type"}=$$metacache{$uri.':parameter_'.
                   2962: 					   $allnames{$name}.'_'.$name.
                   2963: 					   '.type'};
                   2964:       my $olddis=$$metacache{$uri.':parameter_'.$allnames{$name}.'_'.$name.
                   2965: 			     '.display'};
                   2966:       my $expr='\\[Part: '.$allnames{$name}.'\\]';
                   2967:       $olddis=~s/$expr/\[Part: 0\]/;
                   2968:       $$metacache{"$key.display"}=$olddis;
                   2969:     }
1.71      www      2970: }
                   2971: 
1.31      www      2972: # ------------------------------------------------- Update symbolic store links
                   2973: 
                   2974: sub symblist {
                   2975:     my ($mapname,%newhash)=@_;
                   2976:     $mapname=declutter($mapname);
                   2977:     my %hash;
                   2978:     if (($ENV{'request.course.fn'}) && (%newhash)) {
                   2979:         if (tie(%hash,'GDBM_File',$ENV{'request.course.fn'}.'_symb.db',
1.256     albertel 2980:                       &GDBM_WRCREAT(),0640)) {
1.191     harris41 2981: 	    foreach (keys %newhash) {
1.211     www      2982:                 $hash{declutter($_)}=$mapname.'___'.$newhash{$_};
1.191     harris41 2983:             }
1.31      www      2984:             if (untie(%hash)) {
                   2985: 		return 'ok';
                   2986:             }
                   2987:         }
                   2988:     }
                   2989:     return 'error';
1.212     www      2990: }
                   2991: 
                   2992: # --------------------------------------------------------------- Verify a symb
                   2993: 
                   2994: sub symbverify {
                   2995:     my ($symb,$thisfn)=@_;
1.213     www      2996:     $thisfn=&declutter($thisfn);
1.215     www      2997: # direct jump to resource in page or to a sequence - will construct own symbs
                   2998:     if ($thisfn=~/\.(page|sequence)$/) { return 1; }
                   2999: # check URL part
1.213     www      3000:     my ($map,$resid,$url)=split(/\_\_\_/,$symb);
                   3001:     unless (&symbclean($url) eq &symbclean($thisfn)) { return 0; }
                   3002: 
1.216     www      3003:     $symb=&symbclean($symb);
1.213     www      3004: 
                   3005:     my %bighash;
                   3006:     my $okay=0;
                   3007:     if (tie(%bighash,'GDBM_File',$ENV{'request.course.fn'}.'.db',
1.256     albertel 3008:                             &GDBM_READER(),0640)) {
1.280     www      3009:         my $ids=$bighash{'ids_'.&clutter($thisfn)};
1.216     www      3010:         unless ($ids) { 
                   3011:            $ids=$bighash{'ids_/'.$thisfn};
                   3012:         }
                   3013:         if ($ids) {
                   3014: # ------------------------------------------------------------------- Has ID(s)
                   3015: 	    foreach (split(/\,/,$ids)) {
                   3016:                my ($mapid,$resid)=split(/\./,$_);
                   3017:                if (
                   3018:   &symbclean(&declutter($bighash{'map_id_'.$mapid}).'___'.$resid.'___'.$thisfn)
                   3019:    eq $symb) { 
                   3020:                   $okay=1; 
                   3021:                }
                   3022: 	   }
                   3023:         }
1.213     www      3024: 	untie(%bighash);
                   3025:     }
                   3026:     return $okay;
1.31      www      3027: }
                   3028: 
1.210     www      3029: # --------------------------------------------------------------- Clean-up symb
                   3030: 
                   3031: sub symbclean {
                   3032:     my $symb=shift;
1.213     www      3033: 
1.210     www      3034: # remove version from map
                   3035:     $symb=~s/\.(\d+)\.(\w+)\_\_\_/\.$2\_\_\_/;
1.215     www      3036: 
1.210     www      3037: # remove version from URL
                   3038:     $symb=~s/\.(\d+)\.(\w+)$/\.$2/;
1.213     www      3039: 
1.210     www      3040:     return $symb;
                   3041: }
                   3042: 
1.31      www      3043: # ------------------------------------------------------ Return symb list entry
                   3044: 
                   3045: sub symbread {
1.249     www      3046:     my ($thisfn,$donotrecurse)=@_;
1.242     www      3047: # no filename provided? try from environment
1.44      www      3048:     unless ($thisfn) {
1.210     www      3049:         if ($ENV{'request.symb'}) { return &symbclean($ENV{'request.symb'}); }
1.44      www      3050: 	$thisfn=$ENV{'request.filename'};
                   3051:     }
1.242     www      3052: # is that filename actually a symb? Verify, clean, and return
                   3053:     if ($thisfn=~/\_\_\_\d+\_\_\_(.*)$/) {
                   3054: 	if (&symbverify($thisfn,$1)) { return &symbclean($thisfn); }
                   3055:     }
1.44      www      3056:     $thisfn=declutter($thisfn);
1.31      www      3057:     my %hash;
1.37      www      3058:     my %bighash;
                   3059:     my $syval='';
1.45      www      3060:     if (($ENV{'request.course.fn'}) && ($thisfn)) {
1.31      www      3061:         if (tie(%hash,'GDBM_File',$ENV{'request.course.fn'}.'_symb.db',
1.256     albertel 3062:                       &GDBM_READER(),0640)) {
1.31      www      3063: 	    $syval=$hash{$thisfn};
1.37      www      3064:             untie(%hash);
                   3065:         }
                   3066: # ---------------------------------------------------------- There was an entry
                   3067:         if ($syval) {
                   3068:            unless ($syval=~/\_\d+$/) {
                   3069: 	       unless ($ENV{'form.request.prefix'}=~/\.(\d+)\_$/) {
1.44      www      3070:                   &appenv('request.ambiguous' => $thisfn);
1.37      www      3071:                   return '';
                   3072:                }    
                   3073:                $syval.=$1;
                   3074: 	   }
                   3075:         } else {
                   3076: # ------------------------------------------------------- Was not in symb table
                   3077:            if (tie(%bighash,'GDBM_File',$ENV{'request.course.fn'}.'.db',
1.256     albertel 3078:                             &GDBM_READER(),0640)) {
1.37      www      3079: # ---------------------------------------------- Get ID(s) for current resource
1.280     www      3080:               my $ids=$bighash{'ids_'.&clutter($thisfn)};
1.65      www      3081:               unless ($ids) { 
                   3082:                  $ids=$bighash{'ids_/'.$thisfn};
1.242     www      3083:               }
                   3084:               unless ($ids) {
                   3085: # alias?
                   3086: 		  $ids=$bighash{'mapalias_'.$thisfn};
1.65      www      3087:               }
1.37      www      3088:               if ($ids) {
                   3089: # ------------------------------------------------------------------- Has ID(s)
                   3090:                  my @possibilities=split(/\,/,$ids);
1.39      www      3091:                  if ($#possibilities==0) {
                   3092: # ----------------------------------------------- There is only one possibility
1.37      www      3093: 		     my ($mapid,$resid)=split(/\./,$ids);
                   3094:                      $syval=declutter($bighash{'map_id_'.$mapid}).'___'.$resid;
1.249     www      3095:                  } elsif (!$donotrecurse) {
1.39      www      3096: # ------------------------------------------ There is more than one possibility
                   3097:                      my $realpossible=0;
1.191     harris41 3098:                      foreach (@possibilities) {
1.39      www      3099: 			 my $file=$bighash{'src_'.$_};
                   3100:                          if (&allowed('bre',$file)) {
                   3101:          		    my ($mapid,$resid)=split(/\./,$_);
                   3102:                             if ($bighash{'map_type_'.$mapid} ne 'page') {
                   3103: 				$realpossible++;
                   3104:                                 $syval=declutter($bighash{'map_id_'.$mapid}).
                   3105:                                        '___'.$resid;
                   3106:                             }
                   3107: 			 }
1.191     harris41 3108:                      }
1.39      www      3109: 		     if ($realpossible!=1) { $syval=''; }
1.249     www      3110:                  } else {
                   3111:                      $syval='';
1.37      www      3112:                  }
                   3113: 	      }
                   3114:               untie(%bighash)
                   3115:            } 
1.31      www      3116:         }
1.62      www      3117:         if ($syval) {
1.210     www      3118:            return &symbclean($syval.'___'.$thisfn); 
1.62      www      3119:         }
1.31      www      3120:     }
1.44      www      3121:     &appenv('request.ambiguous' => $thisfn);
1.31      www      3122:     return '';
                   3123: }
                   3124: 
                   3125: # ---------------------------------------------------------- Return random seed
                   3126: 
1.32      www      3127: sub numval {
                   3128:     my $txt=shift;
                   3129:     $txt=~tr/A-J/0-9/;
                   3130:     $txt=~tr/a-j/0-9/;
                   3131:     $txt=~tr/K-T/0-9/;
                   3132:     $txt=~tr/k-t/0-9/;
                   3133:     $txt=~tr/U-Z/0-5/;
                   3134:     $txt=~tr/u-z/0-5/;
                   3135:     $txt=~s/\D//g;
                   3136:     return int($txt);
                   3137: }    
                   3138: 
1.31      www      3139: sub rndseed {
1.155     albertel 3140:     my ($symb,$courseid,$domain,$username)=@_;
                   3141:     if (!$symb) {
                   3142:       unless ($symb=&symbread()) { return time; }
                   3143:     }
                   3144:     if (!$courseid) { $courseid=$ENV{'request.course.id'};}
                   3145:     if (!$domain) {$domain=$ENV{'user.domain'};}
                   3146:     if (!$username) {$username=$ENV{'user.name'};}
                   3147:     {
1.98      albertel 3148:       use integer;
                   3149:       my $symbchck=unpack("%32C*",$symb) << 27;
1.100     albertel 3150:       my $symbseed=numval($symb) << 22;
1.155     albertel 3151:       my $namechck=unpack("%32C*",$username) << 17;
                   3152:       my $nameseed=numval($username) << 12;
                   3153:       my $domainseed=unpack("%32C*",$domain) << 7;
                   3154:       my $courseseed=unpack("%32C*",$courseid);
1.98      albertel 3155:       my $num=$symbseed+$nameseed+$domainseed+$courseseed+$namechck+$symbchck;
1.99      albertel 3156:       #uncommenting these lines can break things!
                   3157:       #&Apache::lonxml::debug("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   3158:       #&Apache::lonxml::debug("rndseed :$num:$symb");
1.98      albertel 3159:       return $num;
                   3160:     }
1.36      albertel 3161: }
                   3162: 
1.76      www      3163: sub ireceipt {
                   3164:     my ($funame,$fudom,$fucourseid,$fusymb)=@_;
                   3165:     my $cuname=unpack("%32C*",$funame);
                   3166:     my $cudom=unpack("%32C*",$fudom);
                   3167:     my $cucourseid=unpack("%32C*",$fucourseid);
                   3168:     my $cusymb=unpack("%32C*",$fusymb);
1.77      www      3169:     my $cunique=unpack("%32C*",$perlvar{'lonReceipt'});
1.76      www      3170:     return unpack("%32C*",$perlvar{'lonHostID'}).'-'.
                   3171:            ($cunique%$cuname+
                   3172:             $cunique%$cudom+
                   3173:             $cusymb%$cuname+
                   3174:             $cusymb%$cudom+
                   3175:             $cucourseid%$cuname+
                   3176:             $cucourseid%$cudom);
                   3177: }
                   3178: 
                   3179: sub receipt {
1.260     ng       3180:   my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
                   3181:   return &ireceipt($name,$domain,$courseid,$symb);
1.76      www      3182: }
1.260     ng       3183: 
1.36      albertel 3184: # ------------------------------------------------------------ Serves up a file
                   3185: # returns either the contents of the file or a -1
                   3186: sub getfile {
1.269     www      3187:  my $file=shift;
                   3188:  if ($file=~/^\/*uploaded\//) { # user file
                   3189:     my $ua=new LWP::UserAgent;
                   3190:     my $request=new HTTP::Request('GET',&tokenwrapper($file));
                   3191:     my $response=$ua->request($request);
                   3192:     if ($response->is_success()) {
                   3193:        return $response->content;
                   3194:     } else { 
                   3195:        return -1; 
                   3196:     }
                   3197:  } else { # normal file from res space
1.37      www      3198:   &repcopy($file);
1.36      albertel 3199:   if (! -e $file ) { return -1; };
                   3200:   my $fh=Apache::File->new($file);
                   3201:   my $a='';
                   3202:   while (<$fh>) { $a .=$_; }
1.269     www      3203:   return $a;
                   3204:  }
1.36      albertel 3205: }
                   3206: 
                   3207: sub filelocation {
                   3208:   my ($dir,$file) = @_;
                   3209:   my $location;
                   3210:   $file=~ s/^\s*(\S+)\s*$/$1/; ## strip off leading and trailing spaces
1.59      albertel 3211:   if ($file=~m:^/~:) { # is a contruction space reference
                   3212:     $location = $file;
                   3213:     $location =~ s:/~(.*?)/(.*):/home/$1/public_html/$2:;
1.270     www      3214:   } elsif ($file=~/^\/*uploaded/) { # is an uploaded file
                   3215:     $location=$file;
1.36      albertel 3216:   } else {
1.59      albertel 3217:     $file=~s/^$perlvar{'lonDocRoot'}//;
                   3218:     $file=~s:^/*res::;
                   3219:     if ( !( $file =~ m:^/:) ) {
                   3220:       $location = $dir. '/'.$file;
                   3221:     } else {
                   3222:       $location = '/home/httpd/html/res'.$file;
                   3223:     }
1.36      albertel 3224:   }
                   3225:   $location=~s://+:/:g; # remove duplicate /
1.46      www      3226:   while ($location=~m:/\.\./:) {$location=~ s:/[^/]+/\.\./:/:g;} #remove dir/..
                   3227:   return $location;
                   3228: }
1.36      albertel 3229: 
1.46      www      3230: sub hreflocation {
                   3231:     my ($dir,$file)=@_;
1.191     harris41 3232:     unless (($file=~/^http:\/\//i) || ($file=~/^\//)) {
1.46      www      3233:        my $finalpath=filelocation($dir,$file);
                   3234:        $finalpath=~s/^\/home\/httpd\/html//;
1.225     albertel 3235:        $finalpath=~s-/home/(\w+)/public_html/-/~$1/-;
1.46      www      3236:        return $finalpath;
                   3237:     } else {
                   3238:        return $file;
                   3239:     }
1.31      www      3240: }
                   3241: 
                   3242: # ------------------------------------------------------------- Declutters URLs
                   3243: 
                   3244: sub declutter {
                   3245:     my $thisfn=shift;
                   3246:     $thisfn=~s/^$perlvar{'lonDocRoot'}//;
                   3247:     $thisfn=~s/^\///;
                   3248:     $thisfn=~s/^res\///;
1.235     www      3249:     $thisfn=~s/\?.+$//;
1.268     www      3250:     return $thisfn;
                   3251: }
                   3252: 
                   3253: # ------------------------------------------------------------- Clutter up URLs
                   3254: 
                   3255: sub clutter {
                   3256:     my $thisfn='/'.&declutter(shift);
1.270     www      3257:     unless ($thisfn=~/^\/(uploaded|adm|userfiles|ext|raw|priv)\//) { 
                   3258:        $thisfn='/res'.$thisfn; 
                   3259:     }
1.31      www      3260:     return $thisfn;
1.12      www      3261: }
                   3262: 
                   3263: # -------------------------------------------------------- Escape Special Chars
                   3264: 
                   3265: sub escape {
                   3266:     my $str=shift;
                   3267:     $str =~ s/(\W)/"%".unpack('H2',$1)/eg;
                   3268:     return $str;
                   3269: }
                   3270: 
                   3271: # ----------------------------------------------------- Un-Escape Special Chars
                   3272: 
                   3273: sub unescape {
                   3274:     my $str=shift;
                   3275:     $str =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
                   3276:     return $str;
                   3277: }
1.11      www      3278: 
1.1       albertel 3279: # ================================================================ Main Program
                   3280: 
1.184     www      3281: sub goodbye {
1.204     albertel 3282:    &logthis("Starting Shut down");
1.184     www      3283:    &flushcourselogs();
                   3284:    &logthis("Shutting down");
                   3285: }
                   3286: 
1.179     www      3287: BEGIN {
1.228     harris41 3288: # ----------------------------------- Read loncapa.conf and loncapa_apache.conf
1.195     www      3289:     unless ($readit) {
1.217     harris41 3290: {
                   3291:     my $config=Apache::File->new("/etc/httpd/conf/loncapa.conf");
                   3292: 
                   3293:     while (my $configline=<$config>) {
                   3294:         if ($configline =~ /^[^\#]*PerlSetVar/) {
1.1       albertel 3295: 	   my ($dummy,$varname,$varvalue)=split(/\s+/,$configline);
1.8       www      3296:            chomp($varvalue);
1.1       albertel 3297:            $perlvar{$varname}=$varvalue;
                   3298:         }
                   3299:     }
                   3300: }
1.227     harris41 3301: {
                   3302:     my $config=Apache::File->new("/etc/httpd/conf/loncapa_apache.conf");
                   3303: 
                   3304:     while (my $configline=<$config>) {
                   3305:         if ($configline =~ /^[^\#]*PerlSetVar/) {
                   3306: 	   my ($dummy,$varname,$varvalue)=split(/\s+/,$configline);
                   3307:            chomp($varvalue);
                   3308:            $perlvar{$varname}=$varvalue;
                   3309:         }
                   3310:     }
                   3311: }
1.1       albertel 3312: 
                   3313: # ------------------------------------------------------------- Read hosts file
                   3314: {
                   3315:     my $config=Apache::File->new("$perlvar{'lonTabDir'}/hosts.tab");
                   3316: 
                   3317:     while (my $configline=<$config>) {
1.154     www      3318:        chomp($configline);
1.245     www      3319:        my ($id,$domain,$role,$name,$ip,$domdescr)=split(/:/,$configline);
1.252     albertel 3320:        if ($id && $domain && $role && $name && $ip) {
                   3321: 	 $hostname{$id}=$name;
                   3322: 	 $hostdom{$id}=$domain;
                   3323: 	 $hostip{$id}=$ip;
                   3324: 	 if ($domdescr) { $domaindescription{$domain}=$domdescr; }
                   3325: 	 if ($role eq 'library') { $libserv{$id}=$name; }
                   3326:        } else {
                   3327: 	 if ($configline) {
                   3328: 	   &logthis("Skipping hosts.tab line -$configline-");
                   3329: 	 }
1.245     www      3330:        }
1.1       albertel 3331:     }
                   3332: }
                   3333: 
                   3334: # ------------------------------------------------------ Read spare server file
                   3335: {
                   3336:     my $config=Apache::File->new("$perlvar{'lonTabDir'}/spare.tab");
                   3337: 
                   3338:     while (my $configline=<$config>) {
                   3339:        chomp($configline);
1.284     matthew  3340:        if ($configline) {
1.1       albertel 3341:           $spareid{$configline}=1;
                   3342:        }
                   3343:     }
                   3344: }
1.11      www      3345: # ------------------------------------------------------------ Read permissions
                   3346: {
                   3347:     my $config=Apache::File->new("$perlvar{'lonTabDir'}/roles.tab");
                   3348: 
                   3349:     while (my $configline=<$config>) {
                   3350:        chomp($configline);
1.160     www      3351:       if ($configline) {
1.11      www      3352:        my ($role,$perm)=split(/ /,$configline);
                   3353:        if ($perm ne '') { $pr{$role}=$perm; }
1.160     www      3354:       }
1.11      www      3355:     }
                   3356: }
                   3357: 
                   3358: # -------------------------------------------- Read plain texts for permissions
                   3359: {
                   3360:     my $config=Apache::File->new("$perlvar{'lonTabDir'}/rolesplain.tab");
                   3361: 
                   3362:     while (my $configline=<$config>) {
                   3363:        chomp($configline);
1.160     www      3364:       if ($configline) {
1.11      www      3365:        my ($short,$plain)=split(/:/,$configline);
                   3366:        if ($plain ne '') { $prp{$short}=$plain; }
1.160     www      3367:       }
1.135     www      3368:     }
                   3369: }
                   3370: 
                   3371: # ---------------------------------------------------------- Read package table
                   3372: {
                   3373:     my $config=Apache::File->new("$perlvar{'lonTabDir'}/packages.tab");
                   3374: 
                   3375:     while (my $configline=<$config>) {
                   3376:        chomp($configline);
                   3377:        my ($short,$plain)=split(/:/,$configline);
1.143     www      3378:        my ($pack,$name)=split(/\&/,$short);
                   3379:        if ($plain ne '') {
                   3380:           $packagetab{$pack.'&'.$name.'&name'}=$name; 
                   3381:           $packagetab{$short}=$plain; 
1.25      www      3382:        }
1.11      www      3383:     }
                   3384: }
                   3385: 
1.71      www      3386: %metacache=();
1.185     www      3387: 
1.281     www      3388: $processmarker='_'.time.'_'.$perlvar{'lonHostID'};
1.186     www      3389: $dumpcount=0;
1.22      www      3390: 
1.163     harris41 3391: &logtouch();
1.12      www      3392: &logthis('<font color=yellow>INFO: Read configuration</font>');
1.195     www      3393: $readit=1;
                   3394: }
1.1       albertel 3395: }
1.179     www      3396: 
1.1       albertel 3397: 1;
1.191     harris41 3398: __END__
                   3399: 
1.243     albertel 3400: =pod
                   3401: 
1.191     harris41 3402: =head1 NAME
                   3403: 
1.243     albertel 3404: Apache::lonnet - Subroutines to ask questions about things in the network.
1.191     harris41 3405: 
                   3406: =head1 SYNOPSIS
                   3407: 
1.243     albertel 3408: Invoked by other LON-CAPA modules, when they need to talk to or about objects in the network.
1.191     harris41 3409: 
                   3410:  &Apache::lonnet::SUBROUTINENAME(ARGUMENTS);
                   3411: 
1.243     albertel 3412: Common parameters:
                   3413: 
                   3414: =over 4
                   3415: 
                   3416: =item *
                   3417: 
                   3418: $uname : an internal username (if $cname expecting a course Id specifically)
                   3419: 
                   3420: =item *
                   3421: 
                   3422: $udom : a domain (if $cdom expecting a course's domain specifically)
                   3423: 
                   3424: =item *
                   3425: 
                   3426: $symb : a resource instance identifier
                   3427: 
                   3428: =item *
                   3429: 
                   3430: $namespace : the name of a .db file that contains the data needed or
                   3431: being set.
                   3432: 
                   3433: =back
                   3434: 
1.191     harris41 3435: =head1 INTRODUCTION
                   3436: 
                   3437: This module provides subroutines which interact with the
1.243     albertel 3438: lonc/lond (TCP) network layer of LON-CAPA. And Can be used to ask about 
                   3439: - classes
                   3440: - users 
                   3441: - resources
                   3442: 
                   3443: For many of these objects you can also use this to store data about
                   3444: them or modify them in various ways.
1.191     harris41 3445: 
                   3446: This is part of the LearningOnline Network with CAPA project
                   3447: described at http://www.lon-capa.org.
                   3448: 
1.243     albertel 3449: =head1 RETURN MESSAGES
1.191     harris41 3450: 
                   3451: =over 4
                   3452: 
                   3453: =item *
                   3454: 
1.243     albertel 3455: con_lost : unable to contact remote host
1.191     harris41 3456: 
                   3457: =item *
                   3458: 
1.243     albertel 3459: con_delayed : unable to contact remote host, message will be delivered
                   3460: when the connection is brought back up
1.191     harris41 3461: 
                   3462: =item *
                   3463: 
1.243     albertel 3464: con_failed : unable to contact remote host and unable to save message
                   3465: for later delivery
1.191     harris41 3466: 
                   3467: =item *
                   3468: 
1.243     albertel 3469: error: : an error a occured, a description of the error follows the :
1.191     harris41 3470: 
                   3471: =item *
                   3472: 
1.243     albertel 3473: no_such_host : unable to fund a host associated with the user/domain
                   3474: that was requested
1.191     harris41 3475: 
1.243     albertel 3476: =back
1.191     harris41 3477: 
1.243     albertel 3478: =head1 PUBLIC SUBROUTINES
1.191     harris41 3479: 
1.243     albertel 3480: =head2 Session Environment Functions
1.191     harris41 3481: 
1.243     albertel 3482: =over 4
1.191     harris41 3483: 
                   3484: =item *
                   3485: 
1.243     albertel 3486: appenv(%hash) : the value of %hash is written to the user envirnoment
                   3487: file, and will be restored for each access this user makes during this
                   3488: session, also modifies the %ENV for the current process
1.191     harris41 3489: 
                   3490: =item *
                   3491: 
1.243     albertel 3492: delenv($regexp) : removes all items from the session environment file that matches the regular expression in $regexp. The values are also delted from the current processes %ENV.
1.191     harris41 3493: 
1.243     albertel 3494: =back
                   3495: 
                   3496: =head2 User Information
1.191     harris41 3497: 
1.243     albertel 3498: =over 4
1.191     harris41 3499: 
                   3500: =item *
                   3501: 
                   3502: queryauthenticate($uname,$udom) : try to determine user's current
                   3503: authentication scheme
                   3504: 
                   3505: =item *
                   3506: 
                   3507: authenticate($uname,$upass,$udom) : try to authenticate user from domain's lib
1.243     albertel 3508: servers (first use the current one), $upass should be the users password
1.191     harris41 3509: 
                   3510: =item *
                   3511: 
1.243     albertel 3512: homeserver($uname,$udom) : find the server which has the user's
                   3513: directory and files (there must be only one), this caches the answer,
                   3514: and also caches if there is a borken connection.
1.191     harris41 3515: 
                   3516: =item *
                   3517: 
1.243     albertel 3518: idget($udom,@ids) : find the usernames behind a list of IDs (IDs are a
                   3519: unique resource in a domain, there must be only 1 ID per username, and
                   3520: only 1 username per ID in a specific domain) (returns hash:
1.191     harris41 3521: id=>name,id=>name)
                   3522: 
                   3523: =item *
                   3524: 
                   3525: idrget($udom,@unames) : find the IDs behind a list of usernames (returns hash:
                   3526: name=>id,name=>id)
                   3527: 
                   3528: =item *
                   3529: 
                   3530: idput($udom,%ids) : store away a list of names and associated IDs
                   3531: 
                   3532: =item *
                   3533: 
1.243     albertel 3534: rolesinit($udom,$username,$authhost) : get user privileges
                   3535: 
                   3536: =item *
                   3537: 
                   3538: usection($udom,$uname,$cname) : finds the section of student in the
                   3539: course $cname, return section name/number or '' for "not in course"
                   3540: and '-1' for "no section"
                   3541: 
                   3542: =item *
                   3543: 
                   3544: userenvironment($udom,$uname,@what) : gets the values of the keys
                   3545: passed in @what from the requested user's environment, returns a hash
                   3546: 
                   3547: =back
                   3548: 
                   3549: =head2 User Roles
                   3550: 
                   3551: =over 4
                   3552: 
                   3553: =item *
                   3554: 
                   3555: allowed($priv,$uri) : check for a user privilege; returns codes for allowed
                   3556: actions
                   3557:  F: full access
                   3558:  U,I,K: authentication modes (cxx only)
                   3559:  '': forbidden
                   3560:  1: user needs to choose course
                   3561:  2: browse allowed
                   3562: 
                   3563: =item *
                   3564: 
                   3565: definerole($rolename,$sysrole,$domrole,$courole) : define role; define a custom
                   3566: role rolename set privileges in format of lonTabs/roles.tab for system, domain,
                   3567: and course level
                   3568: 
                   3569: =item *
                   3570: 
                   3571: plaintext($short) : return value in %prp hash (rolesplain.tab); plain text
                   3572: explanation of a user role term
                   3573: 
                   3574: =back
                   3575: 
                   3576: =head2 User Modification
                   3577: 
                   3578: =over 4
                   3579: 
                   3580: =item *
                   3581: 
                   3582: assignrole($udom,$uname,$url,$role,$end,$start) : assign role; give a role to a
                   3583: user for the level given by URL.  Optional start and end dates (leave empty
                   3584: string or zero for "no date")
1.191     harris41 3585: 
                   3586: =item *
                   3587: 
1.243     albertel 3588: changepass($uname,$udom,$currentpass,$newpass,$server) : attempts to
                   3589: change a users, password, possible return values are: ok,
                   3590: pwchange_failure, non_authorized, auth_mode_error, unknown_user,
                   3591: refused
1.191     harris41 3592: 
                   3593: =item *
                   3594: 
1.243     albertel 3595: modifyuserauth($udom,$uname,$umode,$upass) : modify user authentication
1.191     harris41 3596: 
                   3597: =item *
                   3598: 
1.243     albertel 3599: modifyuser($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene) : 
                   3600: modify user
1.191     harris41 3601: 
                   3602: =item *
                   3603: 
1.286     matthew  3604: modifystudent
                   3605: 
                   3606: modify a students enrollment and identification information.
                   3607: The course id is resolved based on the current users environment.  
                   3608: This means the envoking user must be a course coordinator or otherwise
                   3609: associated with a course.
                   3610: 
                   3611: This call is essentially a wrapper for lonnet::modifyuser
                   3612: 
                   3613: Inputs: 
                   3614: 
                   3615: =over 4
                   3616: 
                   3617: =item B<$udom> Students loncapa domain
                   3618: 
                   3619: =item B<$uname> Students loncapa login name
                   3620: 
                   3621: =item B<$uid> Students id/student number
                   3622: 
                   3623: =item B<$umode> Students authentication mode
                   3624: 
                   3625: =item B<$upass> Students password
                   3626: 
                   3627: =item B<$first> Students first name
                   3628: 
                   3629: =item B<$middle> Students middle name
                   3630: 
                   3631: =item B<$last> Students last name
                   3632: 
                   3633: =item B<$gene> Students generation
                   3634: 
                   3635: =item B<$usec> Students section in course
                   3636: 
                   3637: =item B<$end> Unix time of the roles expiration
                   3638: 
                   3639: =item B<$start> Unix time of the roles start date
                   3640: 
                   3641: =item B<$forceid> If defined, allow $uid to be changed
                   3642: 
                   3643: =item B<$desiredhome> server to use as home server for student
                   3644: 
                   3645: =back
1.191     harris41 3646: 
                   3647: =item *
                   3648: 
1.243     albertel 3649: assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start) : assign
                   3650: custom role; give a custom role to a user for the level given by URL.  Specify
                   3651: name and domain of role author, and role name
1.191     harris41 3652: 
                   3653: =item *
                   3654: 
1.243     albertel 3655: revokerole($udom,$uname,$url,$role) : revoke a role for url
1.191     harris41 3656: 
                   3657: =item *
                   3658: 
1.243     albertel 3659: revokecustomrole($udom,$uname,$url,$role) : revoke a custom role
                   3660: 
                   3661: =back
                   3662: 
                   3663: =head2 Course Infomation
                   3664: 
                   3665: =over 4
1.191     harris41 3666: 
                   3667: =item *
                   3668: 
1.243     albertel 3669: coursedescription($courseid) : course description
1.191     harris41 3670: 
                   3671: =item *
                   3672: 
1.243     albertel 3673: courseresdata($coursenum,$coursedomain,@which) : request for current
                   3674: parameter setting for a specific course, @what should be a list of
                   3675: parameters to ask about. This routine caches answers for 5 minutes.
                   3676: 
                   3677: =back
                   3678: 
                   3679: =head2 Course Modification
                   3680: 
                   3681: =over 4
1.191     harris41 3682: 
                   3683: =item *
                   3684: 
1.243     albertel 3685: writecoursepref($courseid,%prefs) : write preferences (environment
                   3686: database) for a course
1.191     harris41 3687: 
                   3688: =item *
                   3689: 
1.243     albertel 3690: createcourse($udom,$description,$url) : make/modify course
                   3691: 
                   3692: =back
                   3693: 
                   3694: =head2 Resource Subroutines
                   3695: 
                   3696: =over 4
1.191     harris41 3697: 
                   3698: =item *
                   3699: 
1.243     albertel 3700: subscribe($fname) : subscribe to a resource, returns URL if possible (probably should use repcopy instead)
1.191     harris41 3701: 
                   3702: =item *
                   3703: 
1.243     albertel 3704: repcopy($filename) : subscribes to the requested file, and attempts to
                   3705: replicate from the owning library server, Might return
                   3706: HTTP_SERVICE_UNAVAILABLE, HTTP_NOT_FOUND, FORBIDDEN, OK, or
                   3707: HTTP_BAD_REQUEST, also attempts to grab the metadata for the
                   3708: resource. Expects the local filesystem pathname
                   3709: (/home/httpd/html/res/....)
                   3710: 
                   3711: =back
                   3712: 
                   3713: =head2 Resource Information
                   3714: 
                   3715: =over 4
1.191     harris41 3716: 
                   3717: =item *
                   3718: 
1.243     albertel 3719: EXT($varname,$symb,$udom,$uname) : evaluates and returns the value of
                   3720: a vairety of different possible values, $varname should be a request
                   3721: string, and the other parameters can be used to specify who and what
                   3722: one is asking about.
                   3723: 
                   3724: Possible values for $varname are environment.lastname (or other item
                   3725: from the envirnment hash), user.name (or someother aspect about the
                   3726: user), resource.0.maxtries (or some other part and parameter of a
                   3727: resource)
1.204     albertel 3728: 
                   3729: =item *
                   3730: 
1.243     albertel 3731: directcondval($number) : get current value of a condition; reads from a state
                   3732: string
1.204     albertel 3733: 
                   3734: =item *
                   3735: 
1.243     albertel 3736: condval($condidx) : value of condition index based on state
1.204     albertel 3737: 
                   3738: =item *
                   3739: 
1.243     albertel 3740: metadata($uri,$what,$liburi,$prefix,$depthcount) : request a
                   3741: resource's metadata, $what should be either a specific key, or either
                   3742: 'keys' (to get a list of possible keys) or 'packages' to get a list of
                   3743: packages that this resource currently uses, the last 3 arguments are only used internally for recursive metadata.
                   3744: 
                   3745: this function automatically caches all requests
1.191     harris41 3746: 
                   3747: =item *
                   3748: 
1.243     albertel 3749: metadata_query($query,$custom,$customshow) : make a metadata query against the
                   3750: network of library servers; returns file handle of where SQL and regex results
                   3751: will be stored for query
1.191     harris41 3752: 
                   3753: =item *
                   3754: 
1.243     albertel 3755: symbread($filename) : return symbolic list entry (filename argument optional);
                   3756: returns the data handle
1.191     harris41 3757: 
                   3758: =item *
                   3759: 
1.243     albertel 3760: symbverify($symb,$thisfn) : verifies that $symb actually exists and is
                   3761: a possible symb for the URL in $thisfn, returns a 1 on success, 0 on
                   3762: failure, user must be in a course, as it assumes the existance of the
                   3763: course initi hash, and uses $ENV('request.course.id'}
                   3764: 
1.191     harris41 3765: 
                   3766: =item *
                   3767: 
1.243     albertel 3768: symbclean($symb) : removes versions numbers from a symb, returns the
                   3769: cleaned symb
1.191     harris41 3770: 
                   3771: =item *
                   3772: 
1.243     albertel 3773: is_on_map($uri) : checks if the $uri is somewhere on the current
                   3774: course map, user must be in a course for it to work.
1.191     harris41 3775: 
                   3776: =item *
                   3777: 
1.243     albertel 3778: numval($salt) : return random seed value (addend for rndseed)
1.191     harris41 3779: 
                   3780: =item *
                   3781: 
1.243     albertel 3782: rndseed($symb,$courseid,$udom,$uname) : create a random sum; returns
                   3783: a random seed, all arguments are optional, if they aren't sent it uses the
                   3784: environment to derive them. Note: if symb isn't sent and it can't get one
                   3785: from &symbread it will use the current time as its return value
1.191     harris41 3786: 
                   3787: =item *
                   3788: 
1.243     albertel 3789: ireceipt($funame,$fudom,$fucourseid,$fusymb) : return unique,
                   3790: unfakeable, receipt
1.191     harris41 3791: 
                   3792: =item *
                   3793: 
1.243     albertel 3794: receipt() : API to ireceipt working off of ENV values; given out to users
1.191     harris41 3795: 
                   3796: =item *
                   3797: 
1.243     albertel 3798: countacc($url) : count the number of accesses to a given URL
1.191     harris41 3799: 
                   3800: =item *
                   3801: 
1.243     albertel 3802: checkout($symb,$tuname,$tudom,$tcrsid) :  creates a record of a user having looked at an item, most likely printed out or otherwise using a resource
1.191     harris41 3803: 
                   3804: =item *
                   3805: 
1.243     albertel 3806: checkin($token) : updates that a resource has beeen returned (a hard copy version for instance) and returns the data that $token was Checkout with ($symb, $tuname, $tudom, and $tcrsid)
1.191     harris41 3807: 
                   3808: =item *
                   3809: 
1.243     albertel 3810: expirespread($uname,$udom,$stype,$usymb) : set expire date for spreadsheet
1.191     harris41 3811: 
                   3812: =item *
                   3813: 
1.243     albertel 3814: devalidate($symb) : devalidate temporary spreadsheet calculations,
                   3815: forcing spreadsheet to reevaluate the resource scores next time.
                   3816: 
                   3817: =back
                   3818: 
                   3819: =head2 Storing/Retreiving Data
                   3820: 
                   3821: =over 4
1.191     harris41 3822: 
                   3823: =item *
                   3824: 
1.243     albertel 3825: store($storehash,$symb,$namespace,$udom,$uname) : stores hash permanently
                   3826: for this url; hashref needs to be given and should be a \%hashname; the
                   3827: remaining args aren't required and if they aren't passed or are '' they will
                   3828: be derived from the ENV
1.191     harris41 3829: 
                   3830: =item *
                   3831: 
1.243     albertel 3832: cstore($storehash,$symb,$namespace,$udom,$uname) : same as store but
                   3833: uses critical subroutine
1.191     harris41 3834: 
                   3835: =item *
                   3836: 
1.243     albertel 3837: restore($symb,$namespace,$udom,$uname) : returns hash for this symb;
                   3838: all args are optional
1.191     harris41 3839: 
                   3840: =item *
                   3841: 
1.243     albertel 3842: tmpstore($storehash,$symb,$namespace,$udom,$uname) : storage that
                   3843: works very similar to store/cstore, but all data is stored in a
                   3844: temporary location and can be reset using tmpreset, $storehash should
                   3845: be a hash reference, returns nothing on success
1.191     harris41 3846: 
                   3847: =item *
                   3848: 
1.243     albertel 3849: tmprestore($symb,$namespace,$udom,$uname) : storage that works very
                   3850: similar to restore, but all data is stored in a temporary location and
                   3851: can be reset using tmpreset. Returns a hash of values on success,
                   3852: error string otherwise.
1.191     harris41 3853: 
                   3854: =item *
                   3855: 
1.243     albertel 3856: tmpreset($symb,$namespace,$udom,$uname) : temporary storage reset,
                   3857: deltes all keys for $symb form the temporary storage hash.
1.191     harris41 3858: 
                   3859: =item *
                   3860: 
1.243     albertel 3861: get($namespace,$storearr,$udom,$uname) : returns hash with keys from array
                   3862: reference filled in from namesp ($udom and $uname are optional)
1.191     harris41 3863: 
                   3864: =item *
                   3865: 
1.243     albertel 3866: del($namespace,$storearr,$udom,$uname) : deletes keys out of array from
                   3867: namesp ($udom and $uname are optional)
1.191     harris41 3868: 
                   3869: =item *
                   3870: 
1.243     albertel 3871: dump($namespace,$udom,$uname,$regexp) : 
                   3872: dumps the complete (or key matching regexp) namespace into a hash
                   3873: ($udom, $uname and $regexp are optional)
1.191     harris41 3874: 
                   3875: =item *
                   3876: 
1.243     albertel 3877: put($namespace,$storehash,$udom,$uname) : stores hash in namesp
                   3878: ($udom and $uname are optional)
1.191     harris41 3879: 
                   3880: =item *
                   3881: 
1.243     albertel 3882: cput($namespace,$storehash,$udom,$uname) : critical put
                   3883: ($udom and $uname are optional)
1.191     harris41 3884: 
                   3885: =item *
                   3886: 
1.243     albertel 3887: eget($namespace,$storearr,$udom,$uname) : returns hash with keys from array
                   3888: reference filled in from namesp (encrypts the return communication)
                   3889: ($udom and $uname are optional)
1.191     harris41 3890: 
                   3891: =item *
                   3892: 
1.243     albertel 3893: log($udom,$name,$home,$message) : write to permanent log for user; use
                   3894: critical subroutine
                   3895: 
                   3896: =back
                   3897: 
                   3898: =head2 Network Status Functions
                   3899: 
                   3900: =over 4
1.191     harris41 3901: 
                   3902: =item *
                   3903: 
                   3904: dirlist($uri) : return directory list based on URI
                   3905: 
                   3906: =item *
                   3907: 
1.243     albertel 3908: spareserver() : find server with least workload from spare.tab
                   3909: 
                   3910: =back
                   3911: 
                   3912: =head2 Apache Request
                   3913: 
                   3914: =over 4
1.191     harris41 3915: 
                   3916: =item *
                   3917: 
1.243     albertel 3918: ssi($url,%hash) : server side include, does a complete request cycle on url to
                   3919: localhost, posts hash
                   3920: 
                   3921: =back
                   3922: 
                   3923: =head2 Data to String to Data
                   3924: 
                   3925: =over 4
1.191     harris41 3926: 
                   3927: =item *
                   3928: 
1.243     albertel 3929: hash2str(%hash) : convert a hash into a string complete with escaping and '='
                   3930: and '&' separators, supports elements that are arrayrefs and hashrefs
1.191     harris41 3931: 
                   3932: =item *
                   3933: 
1.243     albertel 3934: hashref2str($hashref) : convert a hashref into a string complete with
                   3935: escaping and '=' and '&' separators, supports elements that are
                   3936: arrayrefs and hashrefs
1.191     harris41 3937: 
                   3938: =item *
                   3939: 
1.243     albertel 3940: arrayref2str($arrayref) : convert an arrayref into a string complete
                   3941: with escaping and '&' separators, supports elements that are arrayrefs
                   3942: and hashrefs
1.191     harris41 3943: 
                   3944: =item *
                   3945: 
1.243     albertel 3946: str2hash($string) : convert string to hash using unescaping and
                   3947: splitting on '=' and '&', supports elements that are arrayrefs and
                   3948: hashrefs
1.191     harris41 3949: 
                   3950: =item *
                   3951: 
1.243     albertel 3952: str2array($string) : convert string to hash using unescaping and
                   3953: splitting on '&', supports elements that are arrayrefs and hashrefs
                   3954: 
                   3955: =back
                   3956: 
                   3957: =head2 Logging Routines
                   3958: 
                   3959: =over 4
                   3960: 
                   3961: These routines allow one to make log messages in the lonnet.log and
                   3962: lonnet.perm logfiles.
1.191     harris41 3963: 
                   3964: =item *
                   3965: 
1.243     albertel 3966: logtouch() : make sure the logfile, lonnet.log, exists
1.191     harris41 3967: 
                   3968: =item *
                   3969: 
1.243     albertel 3970: logthis() : append message to the normal lonnet.log file, it gets
                   3971: preiodically rolled over and deleted.
1.191     harris41 3972: 
                   3973: =item *
                   3974: 
1.243     albertel 3975: logperm() : append a permanent message to lonnet.perm.log, this log
                   3976: file never gets deleted by any automated portion of the system, only
                   3977: messages of critical importance should go in here.
                   3978: 
                   3979: =back
                   3980: 
                   3981: =head2 General File Helper Routines
                   3982: 
                   3983: =over 4
1.191     harris41 3984: 
                   3985: =item *
                   3986: 
1.243     albertel 3987: getfile($file) : returns the entire contents of a file or -1; it
                   3988: properly subscribes to and replicates the file if neccessary.
1.191     harris41 3989: 
                   3990: =item *
                   3991: 
1.243     albertel 3992: filelocation($dir,$file) : returns file system location of a file
                   3993: based on URI; meant to be "fairly clean" absolute reference, $dir is a
                   3994: directory that relative $file lookups are to looked in ($dir of /a/dir
                   3995: and a file of ../bob will become /a/bob)
1.191     harris41 3996: 
                   3997: =item *
                   3998: 
                   3999: hreflocation($dir,$file) : returns file system location or a URL; same as
                   4000: filelocation except for hrefs
                   4001: 
                   4002: =item *
                   4003: 
                   4004: declutter() : declutters URLs (remove docroot, beginning slashes, 'res' etc)
                   4005: 
1.243     albertel 4006: =back
                   4007: 
                   4008: =head2 HTTP Helper Routines
                   4009: 
                   4010: =over 4
                   4011: 
1.191     harris41 4012: =item *
                   4013: 
                   4014: escape() : unpack non-word characters into CGI-compatible hex codes
                   4015: 
                   4016: =item *
                   4017: 
                   4018: unescape() : pack CGI-compatible hex codes into actual non-word ASCII character
                   4019: 
1.243     albertel 4020: =back
                   4021: 
                   4022: =head1 PRIVATE SUBROUTINES
                   4023: 
                   4024: =head2 Underlying communication routines (Shouldn't call)
                   4025: 
                   4026: =over 4
                   4027: 
                   4028: =item *
                   4029: 
                   4030: subreply() : tries to pass a message to lonc, returns con_lost if incapable
                   4031: 
                   4032: =item *
                   4033: 
                   4034: reply() : uses subreply to send a message to remote machine, logs all failures
                   4035: 
                   4036: =item *
                   4037: 
                   4038: critical() : passes a critical message to another server; if cannot
                   4039: get through then place message in connection buffer directory and
                   4040: returns con_delayed, if incapable of saving message, returns
                   4041: con_failed
                   4042: 
                   4043: =item *
                   4044: 
                   4045: reconlonc() : tries to reconnect lonc client processes.
                   4046: 
                   4047: =back
                   4048: 
                   4049: =head2 Resource Access Logging
                   4050: 
                   4051: =over 4
                   4052: 
                   4053: =item *
                   4054: 
                   4055: flushcourselogs() : flush (save) buffer logs and access logs
                   4056: 
                   4057: =item *
                   4058: 
                   4059: courselog($what) : save message for course in hash
                   4060: 
                   4061: =item *
                   4062: 
                   4063: courseacclog($what) : save message for course using &courselog().  Perform
                   4064: special processing for specific resource types (problems, exams, quizzes, etc).
                   4065: 
1.191     harris41 4066: =item *
                   4067: 
                   4068: goodbye() : flush course logs and log shutting down; it is called in srm.conf
                   4069: as a PerlChildExitHandler
1.243     albertel 4070: 
                   4071: =back
                   4072: 
                   4073: =head2 Other
                   4074: 
                   4075: =over 4
                   4076: 
                   4077: =item *
                   4078: 
                   4079: symblist($mapname,%newhash) : update symbolic storage links
1.191     harris41 4080: 
                   4081: =back
                   4082: 
                   4083: =cut

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>