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

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

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