File:  [LON-CAPA] / loncom / lonnet / perl / lonnet.pm
Revision 1.627: download - view: text, annotated - select for diffs
Mon Apr 18 22:28:19 2005 UTC (19 years, 3 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- this whole c?<interface> thing has always annoyed me from a code duplication point of view anyway

    1: # The LearningOnline Network
    2: # TCP networking package
    3: #
    4: # $Id: lonnet.pm,v 1.627 2005/04/18 22:28:19 albertel Exp $
    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: #
   28: ###
   29: 
   30: package Apache::lonnet;
   31: 
   32: use strict;
   33: use LWP::UserAgent();
   34: use HTTP::Headers;
   35: use HTTP::Date;
   36: # use Date::Parse;
   37: use vars 
   38: qw(%perlvar %hostname %badServerCache %iphost %spareid %hostdom 
   39:    %libserv %pr %prp $memcache %packagetab 
   40:    %courselogs %accesshash %userrolehash $processmarker $dumpcount 
   41:    %coursedombuf %coursenumbuf %coursehombuf %coursedescrbuf %courseinstcodebuf %courseownerbuf
   42:    %domaindescription %domain_auth_def %domain_auth_arg_def 
   43:    %domain_lang_def %domain_city %domain_longi %domain_lati $tmpdir $_64bit
   44:    %env);
   45: 
   46: use IO::Socket;
   47: use GDBM_File;
   48: use Apache::Constants qw(:common :http);
   49: use HTML::LCParser;
   50: use Fcntl qw(:flock);
   51: use Apache::lonlocal;
   52: use Storable qw(lock_store lock_nstore lock_retrieve freeze thaw nfreeze);
   53: use Time::HiRes qw( gettimeofday tv_interval );
   54: use Cache::Memcached;
   55: my $readit;
   56: my $max_connection_retries = 10;     # Or some such value.
   57: 
   58: require Exporter;
   59: 
   60: our @ISA = qw (Exporter);
   61: our @EXPORT = qw(%env);
   62: 
   63: =pod
   64: 
   65: =head1 Package Variables
   66: 
   67: These are largely undocumented, so if you decipher one please note it here.
   68: 
   69: =over 4
   70: 
   71: =item $processmarker
   72: 
   73: Contains the time this process was started and this servers host id.
   74: 
   75: =item $dumpcount
   76: 
   77: Counts the number of times a message log flush has been attempted (regardless
   78: of success) by this process.  Used as part of the filename when messages are
   79: delayed.
   80: 
   81: =back
   82: 
   83: =cut
   84: 
   85: 
   86: # --------------------------------------------------------------------- Logging
   87: 
   88: sub logtouch {
   89:     my $execdir=$perlvar{'lonDaemons'};
   90:     unless (-e "$execdir/logs/lonnet.log") {	
   91: 	open(my $fh,">>$execdir/logs/lonnet.log");
   92: 	close $fh;
   93:     }
   94:     my ($wwwuid,$wwwgid)=(getpwnam('www'))[2,3];
   95:     chown($wwwuid,$wwwgid,$execdir.'/logs/lonnet.log');
   96: }
   97: 
   98: sub logthis {
   99:     my $message=shift;
  100:     my $execdir=$perlvar{'lonDaemons'};
  101:     my $now=time;
  102:     my $local=localtime($now);
  103:     if (open(my $fh,">>$execdir/logs/lonnet.log")) {
  104: 	print $fh "$local ($$): $message\n";
  105: 	close($fh);
  106:     }
  107:     return 1;
  108: }
  109: 
  110: sub logperm {
  111:     my $message=shift;
  112:     my $execdir=$perlvar{'lonDaemons'};
  113:     my $now=time;
  114:     my $local=localtime($now);
  115:     if (open(my $fh,">>$execdir/logs/lonnet.perm.log")) {
  116: 	print $fh "$now:$message:$local\n";
  117: 	close($fh);
  118:     }
  119:     return 1;
  120: }
  121: 
  122: # -------------------------------------------------- Non-critical communication
  123: sub subreply {
  124:     my ($cmd,$server)=@_;
  125:     my $peerfile="$perlvar{'lonSockDir'}/$server";
  126:     #
  127:     #  With loncnew process trimming, there's a timing hole between lonc server
  128:     #  process exit and the master server picking up the listen on the AF_UNIX
  129:     #  socket.  In that time interval, a lock file will exist:
  130: 
  131:     my $lockfile=$peerfile.".lock";
  132:     while (-e $lockfile) {	# Need to wait for the lockfile to disappear.
  133: 	sleep(1);
  134:     }
  135:     # At this point, either a loncnew parent is listening or an old lonc
  136:     # or loncnew child is listening so we can connect or everything's dead.
  137:     #
  138:     #   We'll give the connection a few tries before abandoning it.  If
  139:     #   connection is not possible, we'll con_lost back to the client.
  140:     #   
  141:     my $client;
  142:     for (my $retries = 0; $retries < $max_connection_retries; $retries++) {
  143: 	$client=IO::Socket::UNIX->new(Peer    =>"$peerfile",
  144: 				      Type    => SOCK_STREAM,
  145: 				      Timeout => 10);
  146: 	if($client) {
  147: 	    last;		# Connected!
  148: 	}
  149: 	sleep(1);		# Try again later if failed connection.
  150:     }
  151:     my $answer;
  152:     if ($client) {
  153: 	print $client "$cmd\n";
  154: 	$answer=<$client>;
  155: 	if (!$answer) { $answer="con_lost"; }
  156: 	chomp($answer);
  157:     } else {
  158: 	$answer = 'con_lost';	# Failed connection.
  159:     }
  160:     return $answer;
  161: }
  162: 
  163: sub reply {
  164:     my ($cmd,$server)=@_;
  165:     unless (defined($hostname{$server})) { return 'no_such_host'; }
  166:     my $answer=subreply($cmd,$server);
  167:     if (($answer=~/^refused/) || ($answer=~/^rejected/)) {
  168:        &logthis("<font color=blue>WARNING:".
  169:                 " $cmd to $server returned $answer</font>");
  170:     }
  171:     return $answer;
  172: }
  173: 
  174: # ----------------------------------------------------------- Send USR1 to lonc
  175: 
  176: sub reconlonc {
  177:     my $peerfile=shift;
  178:     &logthis("Trying to reconnect for $peerfile");
  179:     my $loncfile="$perlvar{'lonDaemons'}/logs/lonc.pid";
  180:     if (open(my $fh,"<$loncfile")) {
  181: 	my $loncpid=<$fh>;
  182:         chomp($loncpid);
  183:         if (kill 0 => $loncpid) {
  184: 	    &logthis("lonc at pid $loncpid responding, sending USR1");
  185:             kill USR1 => $loncpid;
  186:             sleep 1;
  187:             if (-e "$peerfile") { return; }
  188:             &logthis("$peerfile still not there, give it another try");
  189:             sleep 5;
  190:             if (-e "$peerfile") { return; }
  191:             &logthis(
  192:   "<font color=blue>WARNING: $peerfile still not there, giving up</font>");
  193:         } else {
  194: 	    &logthis(
  195:                "<font color=blue>WARNING:".
  196:                " lonc at pid $loncpid not responding, giving up</font>");
  197:         }
  198:     } else {
  199:      &logthis('<font color=blue>WARNING: lonc not running, giving up</font>');
  200:     }
  201: }
  202: 
  203: # ------------------------------------------------------ Critical communication
  204: 
  205: sub critical {
  206:     my ($cmd,$server)=@_;
  207:     unless ($hostname{$server}) {
  208:         &logthis("<font color=blue>WARNING:".
  209:                " Critical message to unknown server ($server)</font>");
  210:         return 'no_such_host';
  211:     }
  212:     my $answer=reply($cmd,$server);
  213:     if ($answer eq 'con_lost') {
  214: 	&reconlonc("$perlvar{'lonSockDir'}/$server");
  215: 	my $answer=reply($cmd,$server);
  216:         if ($answer eq 'con_lost') {
  217:             my $now=time;
  218:             my $middlename=$cmd;
  219:             $middlename=substr($middlename,0,16);
  220:             $middlename=~s/\W//g;
  221:             my $dfilename=
  222:       "$perlvar{'lonSockDir'}/delayed/$now.$dumpcount.$$.$middlename.$server";
  223:             $dumpcount++;
  224:             {
  225: 		my $dfh;
  226: 		if (open($dfh,">$dfilename")) {
  227: 		    print $dfh "$cmd\n"; 
  228: 		    close($dfh);
  229: 		}
  230:             }
  231:             sleep 2;
  232:             my $wcmd='';
  233:             {
  234: 		my $dfh;
  235: 		if (open($dfh,"<$dfilename")) {
  236: 		    $wcmd=<$dfh>; 
  237: 		    close($dfh);
  238: 		}
  239:             }
  240:             chomp($wcmd);
  241:             if ($wcmd eq $cmd) {
  242: 		&logthis("<font color=blue>WARNING: ".
  243:                          "Connection buffer $dfilename: $cmd</font>");
  244:                 &logperm("D:$server:$cmd");
  245: 	        return 'con_delayed';
  246:             } else {
  247:                 &logthis("<font color=red>CRITICAL:"
  248:                         ." Critical connection failed: $server $cmd</font>");
  249:                 &logperm("F:$server:$cmd");
  250:                 return 'con_failed';
  251:             }
  252:         }
  253:     }
  254:     return $answer;
  255: }
  256: 
  257: # ------------------------------------------- Transfer profile into environment
  258: 
  259: sub transfer_profile_to_env {
  260:     my ($lonidsdir,$handle)=@_;
  261:     my @profile;
  262:     {
  263: 	open(my $idf,"$lonidsdir/$handle.id");
  264: 	flock($idf,LOCK_SH);
  265: 	@profile=<$idf>;
  266: 	close($idf);
  267:     }
  268:     my $envi;
  269:     my %Remove;
  270:     for ($envi=0;$envi<=$#profile;$envi++) {
  271: 	chomp($profile[$envi]);
  272: 	my ($envname,$envvalue)=split(/=/,$profile[$envi]);
  273: 	$env{$envname} = $envvalue;
  274:         if (my ($key,$time) = ($envname =~ /^(cgi\.(\d+)_\d+\.)/)) {
  275:             if ($time < time-300) {
  276:                 $Remove{$key}++;
  277:             }
  278:         }
  279:     }
  280:     $env{'user.environment'} = "$lonidsdir/$handle.id";
  281:     foreach my $expired_key (keys(%Remove)) {
  282:         &delenv($expired_key);
  283:     }
  284: }
  285: 
  286: # ---------------------------------------------------------- Append Environment
  287: 
  288: sub appenv {
  289:     my %newenv=@_;
  290:     foreach (keys %newenv) {
  291: 	if (($newenv{$_}=~/^user\.role/) || ($newenv{$_}=~/^user\.priv/)) {
  292:             &logthis("<font color=blue>WARNING: ".
  293:                 "Attempt to modify environment ".$_." to ".$newenv{$_}
  294:                 .'</font>');
  295: 	    delete($newenv{$_});
  296:         } else {
  297:             $env{$_}=$newenv{$_};
  298:         }
  299:     }
  300: 
  301:     my $lockfh;
  302:     unless (open($lockfh,"$env{'user.environment'}")) {
  303: 	return 'error: '.$!;
  304:     }
  305:     unless (flock($lockfh,LOCK_EX)) {
  306:          &logthis("<font color=blue>WARNING: ".
  307:                   'Could not obtain exclusive lock in appenv: '.$!);
  308:          close($lockfh);
  309:          return 'error: '.$!;
  310:     }
  311: 
  312:     my @oldenv;
  313:     {
  314: 	my $fh;
  315: 	unless (open($fh,"$env{'user.environment'}")) {
  316: 	    return 'error: '.$!;
  317: 	}
  318: 	@oldenv=<$fh>;
  319: 	close($fh);
  320:     }
  321:     for (my $i=0; $i<=$#oldenv; $i++) {
  322:         chomp($oldenv[$i]);
  323:         if ($oldenv[$i] ne '') {
  324: 	    my ($name,$value)=split(/=/,$oldenv[$i]);
  325: 	    unless (defined($newenv{$name})) {
  326: 		$newenv{$name}=$value;
  327: 	    }
  328:         }
  329:     }
  330:     {
  331: 	my $fh;
  332: 	unless (open($fh,">$env{'user.environment'}")) {
  333: 	    return 'error';
  334: 	}
  335: 	my $newname;
  336: 	foreach $newname (keys %newenv) {
  337: 	    print $fh "$newname=$newenv{$newname}\n";
  338: 	}
  339: 	close($fh);
  340:     }
  341: 	
  342:     close($lockfh);
  343:     return 'ok';
  344: }
  345: # ----------------------------------------------------- Delete from Environment
  346: 
  347: sub delenv {
  348:     my $delthis=shift;
  349:     my %newenv=();
  350:     if (($delthis=~/user\.role/) || ($delthis=~/user\.priv/)) {
  351:         &logthis("<font color=blue>WARNING: ".
  352:                 "Attempt to delete from environment ".$delthis);
  353:         return 'error';
  354:     }
  355:     my @oldenv;
  356:     {
  357: 	my $fh;
  358: 	unless (open($fh,"$env{'user.environment'}")) {
  359: 	    return 'error';
  360: 	}
  361: 	unless (flock($fh,LOCK_SH)) {
  362: 	    &logthis("<font color=blue>WARNING: ".
  363: 		     'Could not obtain shared lock in delenv: '.$!);
  364: 	    close($fh);
  365: 	    return 'error: '.$!;
  366: 	}
  367: 	@oldenv=<$fh>;
  368: 	close($fh);
  369:     }
  370:     {
  371: 	my $fh;
  372: 	unless (open($fh,">$env{'user.environment'}")) {
  373: 	    return 'error';
  374: 	}
  375: 	unless (flock($fh,LOCK_EX)) {
  376: 	    &logthis("<font color=blue>WARNING: ".
  377: 		     'Could not obtain exclusive lock in delenv: '.$!);
  378: 	    close($fh);
  379: 	    return 'error: '.$!;
  380: 	}
  381: 	foreach (@oldenv) {
  382: 	    if ($_=~/^$delthis/) { 
  383:                 my ($key,undef) = split('=',$_);
  384:                 delete($env{$key});
  385:             } else {
  386:                 print $fh $_; 
  387:             }
  388: 	}
  389: 	close($fh);
  390:     }
  391:     return 'ok';
  392: }
  393: 
  394: # ------------------------------------------ Find out current server userload
  395: # there is a copy in lond
  396: sub userload {
  397:     my $numusers=0;
  398:     {
  399: 	opendir(LONIDS,$perlvar{'lonIDsDir'});
  400: 	my $filename;
  401: 	my $curtime=time;
  402: 	while ($filename=readdir(LONIDS)) {
  403: 	    if ($filename eq '.' || $filename eq '..') {next;}
  404: 	    my ($mtime)=(stat($perlvar{'lonIDsDir'}.'/'.$filename))[9];
  405: 	    if ($curtime-$mtime < 1800) { $numusers++; }
  406: 	}
  407: 	closedir(LONIDS);
  408:     }
  409:     my $userloadpercent=0;
  410:     my $maxuserload=$perlvar{'lonUserLoadLim'};
  411:     if ($maxuserload) {
  412: 	$userloadpercent=100*$numusers/$maxuserload;
  413:     }
  414:     $userloadpercent=sprintf("%.2f",$userloadpercent);
  415:     return $userloadpercent;
  416: }
  417: 
  418: # ------------------------------------------ Fight off request when overloaded
  419: 
  420: sub overloaderror {
  421:     my ($r,$checkserver)=@_;
  422:     unless ($checkserver) { $checkserver=$perlvar{'lonHostID'}; }
  423:     my $loadavg;
  424:     if ($checkserver eq $perlvar{'lonHostID'}) {
  425:        open(my $loadfile,'/proc/loadavg');
  426:        $loadavg=<$loadfile>;
  427:        $loadavg =~ s/\s.*//g;
  428:        $loadavg = 100*$loadavg/$perlvar{'lonLoadLim'};
  429:        close($loadfile);
  430:     } else {
  431:        $loadavg=&reply('load',$checkserver);
  432:     }
  433:     my $overload=$loadavg-100;
  434:     if ($overload>0) {
  435: 	$r->err_headers_out->{'Retry-After'}=$overload;
  436:         $r->log_error('Overload of '.$overload.' on '.$checkserver);
  437:         return 413;
  438:     }    
  439:     return '';
  440: }
  441: 
  442: # ------------------------------ Find server with least workload from spare.tab
  443: 
  444: sub spareserver {
  445:     my ($loadpercent,$userloadpercent) = @_;
  446:     my $tryserver;
  447:     my $spareserver='';
  448:     if ($userloadpercent !~ /\d/) { $userloadpercent=0; }
  449:     my $lowestserver=$loadpercent > $userloadpercent?
  450: 	             $loadpercent :  $userloadpercent;
  451:     foreach $tryserver (keys %spareid) {
  452: 	my $loadans=reply('load',$tryserver);
  453: 	my $userloadans=reply('userload',$tryserver);
  454: 	if ($loadans !~ /\d/ && $userloadans !~ /\d/) {
  455: 	    next; #didn't get a number from the server
  456: 	}
  457: 	my $answer;
  458: 	if ($loadans =~ /\d/) {
  459: 	    if ($userloadans =~ /\d/) {
  460: 		#both are numbers, pick the bigger one
  461: 		$answer=$loadans > $userloadans?
  462: 		    $loadans :  $userloadans;
  463: 	    } else {
  464: 		$answer = $loadans;
  465: 	    }
  466: 	} else {
  467: 	    $answer = $userloadans;
  468: 	}
  469: 	if (($answer =~ /\d/) && ($answer<$lowestserver)) {
  470: 	    $spareserver="http://$hostname{$tryserver}";
  471: 	    $lowestserver=$answer;
  472: 	}
  473:     }
  474:     return $spareserver;
  475: }
  476: 
  477: # --------------------------------------------- Try to change a user's password
  478: 
  479: sub changepass {
  480:     my ($uname,$udom,$currentpass,$newpass,$server)=@_;
  481:     $currentpass = &escape($currentpass);
  482:     $newpass     = &escape($newpass);
  483:     my $answer = reply("encrypt:passwd:$udom:$uname:$currentpass:$newpass",
  484: 		       $server);
  485:     if (! $answer) {
  486: 	&logthis("No reply on password change request to $server ".
  487: 		 "by $uname in domain $udom.");
  488:     } elsif ($answer =~ "^ok") {
  489:         &logthis("$uname in $udom successfully changed their password ".
  490: 		 "on $server.");
  491:     } elsif ($answer =~ "^pwchange_failure") {
  492: 	&logthis("$uname in $udom was unable to change their password ".
  493: 		 "on $server.  The action was blocked by either lcpasswd ".
  494: 		 "or pwchange");
  495:     } elsif ($answer =~ "^non_authorized") {
  496:         &logthis("$uname in $udom did not get their password correct when ".
  497: 		 "attempting to change it on $server.");
  498:     } elsif ($answer =~ "^auth_mode_error") {
  499:         &logthis("$uname in $udom attempted to change their password despite ".
  500: 		 "not being locally or internally authenticated on $server.");
  501:     } elsif ($answer =~ "^unknown_user") {
  502:         &logthis("$uname in $udom attempted to change their password ".
  503: 		 "on $server but were unable to because $server is not ".
  504: 		 "their home server.");
  505:     } elsif ($answer =~ "^refused") {
  506: 	&logthis("$server refused to change $uname in $udom password because ".
  507: 		 "it was sent an unencrypted request to change the password.");
  508:     }
  509:     return $answer;
  510: }
  511: 
  512: # ----------------------- Try to determine user's current authentication scheme
  513: 
  514: sub queryauthenticate {
  515:     my ($uname,$udom)=@_;
  516:     my $uhome=&homeserver($uname,$udom);
  517:     if (!$uhome) {
  518: 	&logthis("User $uname at $udom is unknown when looking for authentication mechanism");
  519: 	return 'no_host';
  520:     }
  521:     my $answer=reply("encrypt:currentauth:$udom:$uname",$uhome);
  522:     if ($answer =~ /^(unknown_user|refused|con_lost)/) {
  523: 	&logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
  524:     }
  525:     return $answer;
  526: }
  527: 
  528: # --------- Try to authenticate user from domain's lib servers (first this one)
  529: 
  530: sub authenticate {
  531:     my ($uname,$upass,$udom)=@_;
  532:     $upass=escape($upass);
  533:     $uname=~s/\W//g;
  534:     my $uhome=&homeserver($uname,$udom);
  535:     if (!$uhome) {
  536: 	&logthis("User $uname at $udom is unknown in authenticate");
  537: 	return 'no_host';
  538:     }
  539:     my $answer=reply("encrypt:auth:$udom:$uname:$upass",$uhome);
  540:     if ($answer eq 'authorized') {
  541: 	&logthis("User $uname at $udom authorized by $uhome"); 
  542: 	return $uhome; 
  543:     }
  544:     if ($answer eq 'non_authorized') {
  545: 	&logthis("User $uname at $udom rejected by $uhome");
  546: 	return 'no_host'; 
  547:     }
  548:     &logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
  549:     return 'no_host';
  550: }
  551: 
  552: # ---------------------- Find the homebase for a user from domain's lib servers
  553: 
  554: my %homecache;
  555: sub homeserver {
  556:     my ($uname,$udom,$ignoreBadCache)=@_;
  557:     my $index="$uname:$udom";
  558: 
  559:     if (exists($homecache{$index})) { return $homecache{$index}; }
  560:     my $tryserver;
  561:     foreach $tryserver (keys %libserv) {
  562:         next if ($ignoreBadCache ne 'true' && 
  563: 		 exists($badServerCache{$tryserver}));
  564: 	if ($hostdom{$tryserver} eq $udom) {
  565:            my $answer=reply("home:$udom:$uname",$tryserver);
  566:            if ($answer eq 'found') { 
  567: 	       return $homecache{$index}=$tryserver;
  568:            } elsif ($answer eq 'no_host') {
  569: 	       $badServerCache{$tryserver}=1;
  570:            }
  571:        }
  572:     }    
  573:     return 'no_host';
  574: }
  575: 
  576: # ------------------------------------- Find the usernames behind a list of IDs
  577: 
  578: sub idget {
  579:     my ($udom,@ids)=@_;
  580:     my %returnhash=();
  581:     
  582:     my $tryserver;
  583:     foreach $tryserver (keys %libserv) {
  584:        if ($hostdom{$tryserver} eq $udom) {
  585: 	  my $idlist=join('&',@ids);
  586:           $idlist=~tr/A-Z/a-z/; 
  587: 	  my $reply=&reply("idget:$udom:".$idlist,$tryserver);
  588:           my @answer=();
  589:           if (($reply ne 'con_lost') && ($reply!~/^error\:/)) {
  590: 	      @answer=split(/\&/,$reply);
  591:           }                    ;
  592:           my $i;
  593:           for ($i=0;$i<=$#ids;$i++) {
  594:               if ($answer[$i]) {
  595: 		  $returnhash{$ids[$i]}=$answer[$i];
  596:               } 
  597:           }
  598:        }
  599:     }    
  600:     return %returnhash;
  601: }
  602: 
  603: # ------------------------------------- Find the IDs behind a list of usernames
  604: 
  605: sub idrget {
  606:     my ($udom,@unames)=@_;
  607:     my %returnhash=();
  608:     foreach (@unames) {
  609:         $returnhash{$_}=(&userenvironment($udom,$_,'id'))[1];
  610:     }
  611:     return %returnhash;
  612: }
  613: 
  614: # ------------------------------- Store away a list of names and associated IDs
  615: 
  616: sub idput {
  617:     my ($udom,%ids)=@_;
  618:     my %servers=();
  619:     foreach (keys %ids) {
  620: 	&cput('environment',{'id'=>$ids{$_}},$udom,$_);
  621:         my $uhom=&homeserver($_,$udom);
  622:         if ($uhom ne 'no_host') {
  623:             my $id=&escape($ids{$_});
  624:             $id=~tr/A-Z/a-z/;
  625:             my $unam=&escape($_);
  626: 	    if ($servers{$uhom}) {
  627: 		$servers{$uhom}.='&'.$id.'='.$unam;
  628:             } else {
  629:                 $servers{$uhom}=$id.'='.$unam;
  630:             }
  631:         }
  632:     }
  633:     foreach (keys %servers) {
  634:         &critical('idput:'.$udom.':'.$servers{$_},$_);
  635:     }
  636: }
  637: 
  638: # --------------------------------------------------- Assign a key to a student
  639: 
  640: sub assign_access_key {
  641: #
  642: # a valid key looks like uname:udom#comments
  643: # comments are being appended
  644: #
  645:     my ($ckey,$kdom,$knum,$cdom,$cnum,$udom,$uname,$logentry)=@_;
  646:     $kdom=
  647:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($kdom));
  648:     $knum=
  649:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($knum));
  650:     $cdom=
  651:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
  652:     $cnum=
  653:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
  654:     $udom=$env{'user.name'} unless (defined($udom));
  655:     $uname=$env{'user.domain'} unless (defined($uname));
  656:     my %existing=&get('accesskeys',[$ckey],$kdom,$knum);
  657:     if (($existing{$ckey}=~/^\#(.*)$/) || # - new key
  658:         ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#(.*)$/)) { 
  659:                                                   # assigned to this person
  660:                                                   # - this should not happen,
  661:                                                   # unless something went wrong
  662:                                                   # the first time around
  663: # ready to assign
  664:         $logentry=$1.'; '.$logentry;
  665:         if (&put('accesskeys',{$ckey=>$uname.':'.$udom.'#'.$logentry},
  666:                                                  $kdom,$knum) eq 'ok') {
  667: # key now belongs to user
  668: 	    my $envkey='key.'.$cdom.'_'.$cnum;
  669:             if (&put('environment',{$envkey => $ckey}) eq 'ok') {
  670:                 &appenv('environment.'.$envkey => $ckey);
  671:                 return 'ok';
  672:             } else {
  673:                 return 
  674:   'error: Count not permanently assign key, will need to be re-entered later.';
  675: 	    }
  676:         } else {
  677:             return 'error: Could not assign key, try again later.';
  678:         }
  679:     } elsif (!$existing{$ckey}) {
  680: # the key does not exist
  681: 	return 'error: The key does not exist';
  682:     } else {
  683: # the key is somebody else's
  684: 	return 'error: The key is already in use';
  685:     }
  686: }
  687: 
  688: # ------------------------------------------ put an additional comment on a key
  689: 
  690: sub comment_access_key {
  691: #
  692: # a valid key looks like uname:udom#comments
  693: # comments are being appended
  694: #
  695:     my ($ckey,$cdom,$cnum,$logentry)=@_;
  696:     $cdom=
  697:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
  698:     $cnum=
  699:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
  700:     my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
  701:     if ($existing{$ckey}) {
  702:         $existing{$ckey}.='; '.$logentry;
  703: # ready to assign
  704:         if (&put('accesskeys',{$ckey=>$existing{$ckey}},
  705:                                                  $cdom,$cnum) eq 'ok') {
  706: 	    return 'ok';
  707:         } else {
  708: 	    return 'error: Count not store comment.';
  709:         }
  710:     } else {
  711: # the key does not exist
  712: 	return 'error: The key does not exist';
  713:     }
  714: }
  715: 
  716: # ------------------------------------------------------ Generate a set of keys
  717: 
  718: sub generate_access_keys {
  719:     my ($number,$cdom,$cnum,$logentry)=@_;
  720:     $cdom=
  721:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
  722:     $cnum=
  723:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
  724:     unless (&allowed('mky',$cdom)) { return 0; }
  725:     unless (($cdom) && ($cnum)) { return 0; }
  726:     if ($number>10000) { return 0; }
  727:     sleep(2); # make sure don't get same seed twice
  728:     srand(time()^($$+($$<<15))); # from "Programming Perl"
  729:     my $total=0;
  730:     for (my $i=1;$i<=$number;$i++) {
  731:        my $newkey=sprintf("%lx",int(100000*rand)).'-'.
  732:                   sprintf("%lx",int(100000*rand)).'-'.
  733:                   sprintf("%lx",int(100000*rand));
  734:        $newkey=~s/1/g/g; # folks mix up 1 and l
  735:        $newkey=~s/0/h/g; # and also 0 and O
  736:        my %existing=&get('accesskeys',[$newkey],$cdom,$cnum);
  737:        if ($existing{$newkey}) {
  738:            $i--;
  739:        } else {
  740: 	  if (&put('accesskeys',
  741:               { $newkey => '# generated '.localtime().
  742:                            ' by '.$env{'user.name'}.'@'.$env{'user.domain'}.
  743:                            '; '.$logentry },
  744: 		   $cdom,$cnum) eq 'ok') {
  745:               $total++;
  746: 	  }
  747:        }
  748:     }
  749:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
  750:          'Generated '.$total.' keys for '.$cnum.' at '.$cdom);
  751:     return $total;
  752: }
  753: 
  754: # ------------------------------------------------------- Validate an accesskey
  755: 
  756: sub validate_access_key {
  757:     my ($ckey,$cdom,$cnum,$udom,$uname)=@_;
  758:     $cdom=
  759:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
  760:     $cnum=
  761:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
  762:     $udom=$env{'user.domain'} unless (defined($udom));
  763:     $uname=$env{'user.name'} unless (defined($uname));
  764:     my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
  765:     return ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#/);
  766: }
  767: 
  768: # ------------------------------------- Find the section of student in a course
  769: 
  770: sub getsection {
  771:     my ($udom,$unam,$courseid)=@_;
  772:     my $cachetime=1800;
  773:     $courseid=~s/\_/\//g;
  774:     $courseid=~s/^(\w)/\/$1/;
  775: 
  776:     my $hashid="$udom:$unam:$courseid";
  777:     my ($result,$cached)=&is_cached_new('getsection',$hashid);
  778:     if (defined($cached)) { return $result; }
  779: 
  780:     my %Pending; 
  781:     my %Expired;
  782:     #
  783:     # Each role can either have not started yet (pending), be active, 
  784:     #    or have expired.
  785:     #
  786:     # If there is an active role, we are done.
  787:     #
  788:     # If there is more than one role which has not started yet, 
  789:     #     choose the one which will start sooner
  790:     # If there is one role which has not started yet, return it.
  791:     #
  792:     # If there is more than one expired role, choose the one which ended last.
  793:     # If there is a role which has expired, return it.
  794:     #
  795:     foreach (split(/\&/,&reply('dump:'.$udom.':'.$unam.':roles',
  796:                         &homeserver($unam,$udom)))) {
  797:         my ($key,$value)=split(/\=/,$_);
  798:         $key=&unescape($key);
  799:         next if ($key !~/^\Q$courseid\E(?:\/)*(\w+)*\_st$/);
  800:         my $section=$1;
  801:         if ($key eq $courseid.'_st') { $section=''; }
  802:         my ($dummy,$end,$start)=split(/\_/,&unescape($value));
  803:         my $now=time;
  804:         if (defined($end) && $end && ($now > $end)) {
  805:             $Expired{$end}=$section;
  806:             next;
  807:         }
  808:         if (defined($start) && $start && ($now < $start)) {
  809:             $Pending{$start}=$section;
  810:             next;
  811:         }
  812:         return &do_cache_new('getsection',$hashid,$section,$cachetime);
  813:     }
  814:     #
  815:     # Presumedly there will be few matching roles from the above
  816:     # loop and the sorting time will be negligible.
  817:     if (scalar(keys(%Pending))) {
  818:         my ($time) = sort {$a <=> $b} keys(%Pending);
  819:         return &do_cache_new('getsection',$hashid,$Pending{$time},$cachetime);
  820:     } 
  821:     if (scalar(keys(%Expired))) {
  822:         my @sorted = sort {$a <=> $b} keys(%Expired);
  823:         my $time = pop(@sorted);
  824:         return &do_cache_new('getsection',$hashid,$Expired{$time},$cachetime);
  825:     }
  826:     return &do_cache_new('getsection',$hashid,'-1',$cachetime);
  827: }
  828: 
  829: sub save_cache {
  830:     &purge_remembered();
  831:     undef(%env);
  832: }
  833: 
  834: my $to_remember=-1;
  835: my %remembered;
  836: my %accessed;
  837: my $kicks=0;
  838: my $hits=0;
  839: sub devalidate_cache_new {
  840:     my ($name,$id,$debug) = @_;
  841:     if ($debug) { &Apache::lonnet::logthis("deleting $name:$id"); }
  842:     $id=&escape($name.':'.$id);
  843:     $memcache->delete($id);
  844:     delete($remembered{$id});
  845:     delete($accessed{$id});
  846: }
  847: 
  848: sub is_cached_new {
  849:     my ($name,$id,$debug) = @_;
  850:     $id=&escape($name.':'.$id);
  851:     if (exists($remembered{$id})) {
  852: 	if ($debug) { &Apache::lonnet::logthis("Earyl return $id of $remembered{$id} "); }
  853: 	$accessed{$id}=[&gettimeofday()];
  854: 	$hits++;
  855: 	return ($remembered{$id},1);
  856:     }
  857:     my $value = $memcache->get($id);
  858:     if (!(defined($value))) {
  859: 	if ($debug) { &Apache::lonnet::logthis("getting $id is not defined"); }
  860: 	return (undef,undef);
  861:     }
  862:     if ($value eq '__undef__') {
  863: 	if ($debug) { &Apache::lonnet::logthis("getting $id is __undef__"); }
  864: 	$value=undef;
  865:     }
  866:     &make_room($id,$value,$debug);
  867:     if ($debug) { &Apache::lonnet::logthis("getting $id is $value"); }
  868:     return ($value,1);
  869: }
  870: 
  871: sub do_cache_new {
  872:     my ($name,$id,$value,$time,$debug) = @_;
  873:     $id=&escape($name.':'.$id);
  874:     my $setvalue=$value;
  875:     if (!defined($setvalue)) {
  876: 	$setvalue='__undef__';
  877:     }
  878:     if (!defined($time) ) {
  879: 	$time=600;
  880:     }
  881:     if ($debug) { &Apache::lonnet::logthis("Setting $id to $value"); }
  882:     $memcache->set($id,$setvalue,$time);
  883:     # need to make a copy of $value
  884:     #&make_room($id,$value,$debug);
  885:     return $value;
  886: }
  887: 
  888: sub make_room {
  889:     my ($id,$value,$debug)=@_;
  890:     $remembered{$id}=$value;
  891:     if ($to_remember<0) { return; }
  892:     $accessed{$id}=[&gettimeofday()];
  893:     if (scalar(keys(%remembered)) <= $to_remember) { return; }
  894:     my $to_kick;
  895:     my $max_time=0;
  896:     foreach my $other (keys(%accessed)) {
  897: 	if (&tv_interval($accessed{$other}) > $max_time) {
  898: 	    $to_kick=$other;
  899: 	    $max_time=&tv_interval($accessed{$other});
  900: 	}
  901:     }
  902:     delete($remembered{$to_kick});
  903:     delete($accessed{$to_kick});
  904:     $kicks++;
  905:     if ($debug) { &logthis("kicking $to_kick $max_time $kicks\n"); }
  906:     return;
  907: }
  908: 
  909: sub purge_remembered {
  910:     #&logthis("Tossing ".scalar(keys(%remembered)));
  911:     #&logthis(sprintf("%-20s is %s",'%remembered',length(&freeze(\%remembered))));
  912:     undef(%remembered);
  913:     undef(%accessed);
  914: }
  915: # ------------------------------------- Read an entry from a user's environment
  916: 
  917: sub userenvironment {
  918:     my ($udom,$unam,@what)=@_;
  919:     my %returnhash=();
  920:     my @answer=split(/\&/,
  921:                 &reply('get:'.$udom.':'.$unam.':environment:'.join('&',@what),
  922:                       &homeserver($unam,$udom)));
  923:     my $i;
  924:     for ($i=0;$i<=$#what;$i++) {
  925: 	$returnhash{$what[$i]}=&unescape($answer[$i]);
  926:     }
  927:     return %returnhash;
  928: }
  929: 
  930: # ---------------------------------------------------------- Get a studentphoto
  931: sub studentphoto {
  932:     my ($udom,$unam,$ext) = @_;
  933:     my $home=&Apache::lonnet::homeserver($unam,$udom);
  934:     my $ret=&Apache::lonnet::reply("studentphoto:$udom:$unam:$ext",$home);
  935:     my $url="/uploaded/$udom/$unam/internal/studentphoto.".$ext;
  936:     if ($ret ne 'ok') {
  937: 	return '/adm/lonKaputt/lonlogo_broken.gif';
  938:     }
  939:     my $tokenurl=&Apache::lonnet::tokenwrapper($url);
  940:     return $tokenurl;
  941: }
  942: 
  943: # -------------------------------------------------------------------- New chat
  944: 
  945: sub chatsend {
  946:     my ($newentry,$anon)=@_;
  947:     my $cnum=$env{'course.'.$env{'request.course.id'}.'.num'};
  948:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
  949:     my $chome=$env{'course.'.$env{'request.course.id'}.'.home'};
  950:     &reply('chatsend:'.$cdom.':'.$cnum.':'.
  951: 	   &escape($env{'user.domain'}.':'.$env{'user.name'}.':'.$anon.':'.
  952: 		   &escape($newentry)),$chome);
  953: }
  954: 
  955: # ------------------------------------------ Find current version of a resource
  956: 
  957: sub getversion {
  958:     my $fname=&clutter(shift);
  959:     unless ($fname=~/^\/res\//) { return -1; }
  960:     return &currentversion(&filelocation('',$fname));
  961: }
  962: 
  963: sub currentversion {
  964:     my $fname=shift;
  965:     my ($result,$cached)=&is_cached_new('resversion',$fname);
  966:     if (defined($cached)) { return $result; }
  967:     my $author=$fname;
  968:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
  969:     my ($udom,$uname)=split(/\//,$author);
  970:     my $home=homeserver($uname,$udom);
  971:     if ($home eq 'no_host') { 
  972:         return -1; 
  973:     }
  974:     my $answer=reply("currentversion:$fname",$home);
  975:     if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
  976: 	return -1;
  977:     }
  978:     return &do_cache_new('resversion',$fname,$answer,600);
  979: }
  980: 
  981: # ----------------------------- Subscribe to a resource, return URL if possible
  982: 
  983: sub subscribe {
  984:     my $fname=shift;
  985:     if ($fname=~/\/(aboutme|syllabus|bulletinboard|smppg)$/) { return ''; }
  986:     $fname=~s/[\n\r]//g;
  987:     my $author=$fname;
  988:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
  989:     my ($udom,$uname)=split(/\//,$author);
  990:     my $home=homeserver($uname,$udom);
  991:     if ($home eq 'no_host') {
  992:         return 'not_found';
  993:     }
  994:     my $answer=reply("sub:$fname",$home);
  995:     if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
  996: 	$answer.=' by '.$home;
  997:     }
  998:     return $answer;
  999: }
 1000:     
 1001: # -------------------------------------------------------------- Replicate file
 1002: 
 1003: sub repcopy {
 1004:     my $filename=shift;
 1005:     $filename=~s/\/+/\//g;
 1006:     if ($filename=~m|^/home/httpd/html/adm/|) { return 'ok'; }
 1007:     if ($filename=~m|^/home/httpd/html/lonUsers/|) { return 'ok'; }
 1008:     if ($filename=~m|^/home/httpd/html/userfiles/| or
 1009: 	$filename=~m -^/*(uploaded|editupload)/-) { 
 1010: 	return &repcopy_userfile($filename);
 1011:     }
 1012:     $filename=~s/[\n\r]//g;
 1013:     my $transname="$filename.in.transfer";
 1014:     if ((-e $filename) || (-e $transname)) { return 'ok'; }
 1015:     my $remoteurl=subscribe($filename);
 1016:     if ($remoteurl =~ /^con_lost by/) {
 1017: 	   &logthis("Subscribe returned $remoteurl: $filename");
 1018:            return 'unavailable';
 1019:     } elsif ($remoteurl eq 'not_found') {
 1020: 	   #&logthis("Subscribe returned not_found: $filename");
 1021: 	   return 'not_found';
 1022:     } elsif ($remoteurl =~ /^rejected by/) {
 1023: 	   &logthis("Subscribe returned $remoteurl: $filename");
 1024:            return 'forbidden';
 1025:     } elsif ($remoteurl eq 'directory') {
 1026:            return 'ok';
 1027:     } else {
 1028:         my $author=$filename;
 1029:         $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
 1030:         my ($udom,$uname)=split(/\//,$author);
 1031:         my $home=homeserver($uname,$udom);
 1032:         unless ($home eq $perlvar{'lonHostID'}) {
 1033:            my @parts=split(/\//,$filename);
 1034:            my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 1035:            if ($path ne "$perlvar{'lonDocRoot'}/res") {
 1036:                &logthis("Malconfiguration for replication: $filename");
 1037: 	       return 'bad_request';
 1038:            }
 1039:            my $count;
 1040:            for ($count=5;$count<$#parts;$count++) {
 1041:                $path.="/$parts[$count]";
 1042:                if ((-e $path)!=1) {
 1043: 		   mkdir($path,0777);
 1044:                }
 1045:            }
 1046:            my $ua=new LWP::UserAgent;
 1047:            my $request=new HTTP::Request('GET',"$remoteurl");
 1048:            my $response=$ua->request($request,$transname);
 1049:            if ($response->is_error()) {
 1050: 	       unlink($transname);
 1051:                my $message=$response->status_line;
 1052:                &logthis("<font color=blue>WARNING:"
 1053:                        ." LWP get: $message: $filename</font>");
 1054:                return 'unavailable';
 1055:            } else {
 1056: 	       if ($remoteurl!~/\.meta$/) {
 1057:                   my $mrequest=new HTTP::Request('GET',$remoteurl.'.meta');
 1058:                   my $mresponse=$ua->request($mrequest,$filename.'.meta');
 1059:                   if ($mresponse->is_error()) {
 1060: 		      unlink($filename.'.meta');
 1061:                       &logthis(
 1062:                      "<font color=yellow>INFO: No metadata: $filename</font>");
 1063:                   }
 1064: 	       }
 1065:                rename($transname,$filename);
 1066:                return 'ok';
 1067:            }
 1068:        }
 1069:     }
 1070: }
 1071: 
 1072: # ------------------------------------------------ Get server side include body
 1073: sub ssi_body {
 1074:     my ($filelink,%form)=@_;
 1075:     if (! exists($form{'LONCAPA_INTERNAL_no_discussion'})) {
 1076:         $form{'LONCAPA_INTERNAL_no_discussion'}='true';
 1077:     }
 1078:     my $output=($filelink=~/^http\:/?&externalssi($filelink):
 1079:                                      &ssi($filelink,%form));
 1080:     $output=~s|//(\s*<!--)? BEGIN LON-CAPA Internal.+// END LON-CAPA Internal\s*(-->)?\s||gs;
 1081:     $output=~s/^.*?\<body[^\>]*\>//si;
 1082:     $output=~s/(.*)\<\/body\s*\>.*?$/$1/si;
 1083:     return $output;
 1084: }
 1085: 
 1086: # --------------------------------------------------------- Server Side Include
 1087: 
 1088: sub ssi {
 1089: 
 1090:     my ($fn,%form)=@_;
 1091: 
 1092:     my $ua=new LWP::UserAgent;
 1093:     
 1094:     my $request;
 1095:     
 1096:     if (%form) {
 1097:       $request=new HTTP::Request('POST',"http://".$ENV{'HTTP_HOST'}.$fn);
 1098:       $request->content(join('&',map { &escape($_).'='.&escape($form{$_}) } keys %form));
 1099:     } else {
 1100:       $request=new HTTP::Request('GET',"http://".$ENV{'HTTP_HOST'}.$fn);
 1101:     }
 1102: 
 1103:     $request->header(Cookie => $ENV{'HTTP_COOKIE'});
 1104:     my $response=$ua->request($request);
 1105: 
 1106:     return $response->content;
 1107: }
 1108: 
 1109: sub externalssi {
 1110:     my ($url)=@_;
 1111:     my $ua=new LWP::UserAgent;
 1112:     my $request=new HTTP::Request('GET',$url);
 1113:     my $response=$ua->request($request);
 1114:     return $response->content;
 1115: }
 1116: 
 1117: # -------------------------------- Allow a /uploaded/ URI to be vouched for
 1118: 
 1119: sub allowuploaded {
 1120:     my ($srcurl,$url)=@_;
 1121:     $url=&clutter(&declutter($url));
 1122:     my $dir=$url;
 1123:     $dir=~s/\/[^\/]+$//;
 1124:     my %httpref=();
 1125:     my $httpurl=&hreflocation('',$url);
 1126:     $httpref{'httpref.'.$httpurl}=$srcurl;
 1127:     &Apache::lonnet::appenv(%httpref);
 1128: }
 1129: 
 1130: # --------- File operations in /home/httpd/html/userfiles/$domain/1/2/3/$course
 1131: # input: action, courseID, current domain, home server for course, intended
 1132: #        path to file, source of file.
 1133: # output: url to file (if action was uploaddoc), 
 1134: #         ok if successful, or diagnostic message otherwise (if action was propagate or copy)
 1135: #
 1136: # Allows directory structure to be used within lonUsers/../userfiles/ for a 
 1137: # course.
 1138: #
 1139: # action = propagate - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
 1140: #          will be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles in
 1141: #          course's home server.
 1142: #
 1143: # action = copy - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file will
 1144: #          be copied from $source (current location) to 
 1145: #          /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
 1146: #         and will then be copied to
 1147: #          /home/httpd/lonUsers/$domain/1/2/3/$course/userfiles/$file in
 1148: #         course's home server.
 1149: #
 1150: # action = uploaddoc - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
 1151: #         will be retrived from $env{form.uploaddoc} (from DOCS interface) to
 1152: #         /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
 1153: #         and will then be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles/$file
 1154: #         in course's home server.
 1155: 
 1156: 
 1157: sub process_coursefile {
 1158:     my ($action,$docuname,$docudom,$docuhome,$file,$source)=@_;
 1159:     my $fetchresult;
 1160:     if ($action eq 'propagate') {
 1161:         $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file
 1162:                             ,$docuhome);
 1163:     } else {
 1164:         my $fetchresult = '';
 1165:         my $fpath = '';
 1166:         my $fname = $file;
 1167:         ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
 1168:         $fpath=$docudom.'/'.$docuname.'/'.$fpath;
 1169:         my $filepath=$perlvar{'lonDocRoot'}.'/userfiles';
 1170:         unless ($fpath eq '') {
 1171:             my @parts=split('/',$fpath);
 1172:             foreach my $part (@parts) {
 1173:                 $filepath.= '/'.$part;
 1174:                 if ((-e $filepath)!=1) {
 1175:                     mkdir($filepath,0777);
 1176:                 }
 1177:             }
 1178:         }
 1179:         if ($action eq 'copy') {
 1180:             if ($source eq '') {
 1181:                 $fetchresult = 'no source file';
 1182:                 return $fetchresult;
 1183:             } else {
 1184:                 my $destination = $filepath.'/'.$fname;
 1185:                 rename($source,$destination);
 1186:                 $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
 1187:                                  $docuhome);
 1188:             }
 1189:         } elsif ($action eq 'uploaddoc') {
 1190:             open(my $fh,'>'.$filepath.'/'.$fname);
 1191:             print $fh $env{'form.'.$source};
 1192:             close($fh);
 1193:             $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
 1194:                                  $docuhome);
 1195:             if ($fetchresult eq 'ok') {
 1196:                 return '/uploaded/'.$fpath.'/'.$fname;
 1197:             } else {
 1198:                 &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
 1199:                         ' to host '.$docuhome.': '.$fetchresult);
 1200:                 return '/adm/notfound.html';
 1201:             }
 1202:         }
 1203:     }
 1204:     unless ( $fetchresult eq 'ok') {
 1205:         &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
 1206:              ' to host '.$docuhome.': '.$fetchresult);
 1207:     }
 1208:     return $fetchresult;
 1209: }
 1210: 
 1211: sub clean_filename {
 1212:     my ($fname)=@_;
 1213: # Replace Windows backslashes by forward slashes
 1214:     $fname=~s/\\/\//g;
 1215: # Get rid of everything but the actual filename
 1216:     $fname=~s/^.*\/([^\/]+)$/$1/;
 1217: # Replace spaces by underscores
 1218:     $fname=~s/\s+/\_/g;
 1219: # Replace all other weird characters by nothing
 1220:     $fname=~s/[^\w\.\-]//g;
 1221: # Replace all .\d. sequences with _\d. so they no longer look like version
 1222: # numbers
 1223:     $fname=~s/\.(\d+)(?=\.)/_$1/g;
 1224:     return $fname;
 1225: }
 1226: 
 1227: # --------------- Take an uploaded file and put it into the userfiles directory
 1228: # input: name of form element, coursedoc=1 means this is for the course
 1229: # output: url of file in userspace
 1230: 
 1231: 
 1232: sub userfileupload {
 1233:     my ($formname,$coursedoc,$subdir)=@_;
 1234:     if (!defined($subdir)) { $subdir='unknown'; }
 1235:     my $fname=$env{'form.'.$formname.'.filename'};
 1236:     $fname=&clean_filename($fname);
 1237: # See if there is anything left
 1238:     unless ($fname) { return 'error: no uploaded file'; }
 1239:     chop($env{'form.'.$formname});
 1240:     if (($formname eq 'screenshot') && ($subdir eq 'helprequests')) { #files uploaded to help request form are handled differently
 1241:         my $now = time;
 1242:         my $filepath = 'tmp/helprequests/'.$now;
 1243:         my @parts=split(/\//,$filepath);
 1244:         my $fullpath = $perlvar{'lonDaemons'};
 1245:         for (my $i=0;$i<@parts;$i++) {
 1246:             $fullpath .= '/'.$parts[$i];
 1247:             if ((-e $fullpath)!=1) {
 1248:                 mkdir($fullpath,0777);
 1249:             }
 1250:         }
 1251:         open(my $fh,'>'.$fullpath.'/'.$fname);
 1252:         print $fh $env{'form.'.$formname};
 1253:         close($fh);
 1254:         return $fullpath.'/'.$fname; 
 1255:     }
 1256: # Create the directory if not present
 1257:     my $docuname='';
 1258:     my $docudom='';
 1259:     my $docuhome='';
 1260:     $fname="$subdir/$fname";
 1261:     if ($coursedoc) {
 1262: 	$docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
 1263: 	$docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 1264: 	$docuhome=$env{'course.'.$env{'request.course.id'}.'.home'};
 1265:         if ($env{'form.folder'} =~ m/^default/) {
 1266:             return &finishuserfileupload($docuname,$docudom,$docuhome,$formname,$fname);
 1267:         } else {
 1268:             $fname=$env{'form.folder'}.'/'.$fname;
 1269:             return &process_coursefile('uploaddoc',$docuname,$docudom,$docuhome,$fname,$formname);
 1270:         }
 1271:     } else {
 1272:         $docuname=$env{'user.name'};
 1273:         $docudom=$env{'user.domain'};
 1274:         $docuhome=$env{'user.home'};
 1275:         return &finishuserfileupload($docuname,$docudom,$docuhome,$formname,$fname);
 1276:     }
 1277: }
 1278: 
 1279: sub finishuserfileupload {
 1280:     my ($docuname,$docudom,$docuhome,$formname,$fname)=@_;
 1281:     my $path=$docudom.'/'.$docuname.'/';
 1282:     my $filepath=$perlvar{'lonDocRoot'};
 1283:     my ($fnamepath,$file);
 1284:     $file=$fname;
 1285:     if ($fname=~m|/|) {
 1286:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 1287: 	$path.=$fnamepath.'/';
 1288:     }
 1289:     my @parts=split(/\//,$filepath.'/userfiles/'.$path);
 1290:     my $count;
 1291:     for ($count=4;$count<=$#parts;$count++) {
 1292:         $filepath.="/$parts[$count]";
 1293:         if ((-e $filepath)!=1) {
 1294: 	    mkdir($filepath,0777);
 1295:         }
 1296:     }
 1297: # Save the file
 1298:     {
 1299: 	open(FH,'>'.$filepath.'/'.$file);
 1300: 	print FH $env{'form.'.$formname};
 1301: 	close(FH);
 1302:     }
 1303: # Notify homeserver to grep it
 1304: #
 1305:     my $fetchresult= &reply('fetchuserfile:'.$path.$file,$docuhome);
 1306:     if ($fetchresult eq 'ok') {
 1307: #
 1308: # Return the URL to it
 1309:         return '/uploaded/'.$path.$file;
 1310:     } else {
 1311:         &logthis('Failed to transfer '.$path.$file.' to host '.$docuhome.
 1312: 		 ': '.$fetchresult);
 1313:         return '/adm/notfound.html';
 1314:     }    
 1315: }
 1316: 
 1317: sub removeuploadedurl {
 1318:     my ($url)=@_;
 1319:     my (undef,undef,$udom,$uname,$fname)=split('/',$url,5);
 1320:     return &removeuserfile($uname,$udom,$fname);
 1321: }
 1322: 
 1323: sub removeuserfile {
 1324:     my ($docuname,$docudom,$fname)=@_;
 1325:     my $home=&homeserver($docuname,$docudom);
 1326:     return &reply("removeuserfile:$docudom/$docuname/$fname",$home);
 1327: }
 1328: 
 1329: sub mkdiruserfile {
 1330:     my ($docuname,$docudom,$dir)=@_;
 1331:     my $home=&homeserver($docuname,$docudom);
 1332:     return &reply("mkdiruserfile:".&escape("$docudom/$docuname/$dir"),$home);
 1333: }
 1334: 
 1335: sub renameuserfile {
 1336:     my ($docuname,$docudom,$old,$new)=@_;
 1337:     my $home=&homeserver($docuname,$docudom);
 1338:     return &reply("renameuserfile:$docudom:$docuname:".&escape("$old").':'.
 1339: 		  &escape("$new"),$home);
 1340: }
 1341: 
 1342: # ------------------------------------------------------------------------- Log
 1343: 
 1344: sub log {
 1345:     my ($dom,$nam,$hom,$what)=@_;
 1346:     return critical("log:$dom:$nam:$what",$hom);
 1347: }
 1348: 
 1349: # ------------------------------------------------------------------ Course Log
 1350: #
 1351: # This routine flushes several buffers of non-mission-critical nature
 1352: #
 1353: 
 1354: sub flushcourselogs {
 1355:     &logthis('Flushing log buffers');
 1356: #
 1357: # course logs
 1358: # This is a log of all transactions in a course, which can be used
 1359: # for data mining purposes
 1360: #
 1361: # It also collects the courseid database, which lists last transaction
 1362: # times and course titles for all courseids
 1363: #
 1364:     my %courseidbuffer=();
 1365:     foreach (keys %courselogs) {
 1366:         my $crsid=$_;
 1367:         if (&reply('log:'.$coursedombuf{$crsid}.':'.$coursenumbuf{$crsid}.':'.
 1368: 		          &escape($courselogs{$crsid}),
 1369: 		          $coursehombuf{$crsid}) eq 'ok') {
 1370: 	    delete $courselogs{$crsid};
 1371:         } else {
 1372:             &logthis('Failed to flush log buffer for '.$crsid);
 1373:             if (length($courselogs{$crsid})>40000) {
 1374:                &logthis("<font color=blue>WARNING: Buffer for ".$crsid.
 1375:                         " exceeded maximum size, deleting.</font>");
 1376:                delete $courselogs{$crsid};
 1377:             }
 1378:         }
 1379:         if ($courseidbuffer{$coursehombuf{$crsid}}) {
 1380:            $courseidbuffer{$coursehombuf{$crsid}}.='&'.
 1381: 			 &escape($crsid).'='.&escape($coursedescrbuf{$crsid}).
 1382:                          ':'.&escape($courseinstcodebuf{$crsid}).':'.&escape($courseownerbuf{$crsid});
 1383:         } else {
 1384:            $courseidbuffer{$coursehombuf{$crsid}}=
 1385: 			 &escape($crsid).'='.&escape($coursedescrbuf{$crsid}).
 1386:                          ':'.&escape($courseinstcodebuf{$crsid}).':'.&escape($courseownerbuf{$crsid});
 1387:         }
 1388:     }
 1389: #
 1390: # Write course id database (reverse lookup) to homeserver of courses 
 1391: # Is used in pickcourse
 1392: #
 1393:     foreach (keys %courseidbuffer) {
 1394:         &courseidput($hostdom{$_},$courseidbuffer{$_},$_);
 1395:     }
 1396: #
 1397: # File accesses
 1398: # Writes to the dynamic metadata of resources to get hit counts, etc.
 1399: #
 1400:     foreach my $entry (keys(%accesshash)) {
 1401:         if ($entry =~ /___count$/) {
 1402:             my ($dom,$name);
 1403:             ($dom,$name,undef)=($entry=~m:___(\w+)/(\w+)/(.*)___count$:);
 1404:             if (! defined($dom) || $dom eq '' || 
 1405:                 ! defined($name) || $name eq '') {
 1406:                 my $cid = $env{'request.course.id'};
 1407:                 $dom  = $env{'request.'.$cid.'.domain'};
 1408:                 $name = $env{'request.'.$cid.'.num'};
 1409:             }
 1410:             my $value = $accesshash{$entry};
 1411:             my (undef,$url,undef) = ($entry =~ /^(.*)___(.*)___count$/);
 1412:             my %temphash=($url => $value);
 1413:             my $result = &inc('nohist_accesscount',\%temphash,$dom,$name);
 1414:             if ($result eq 'ok') {
 1415:                 delete $accesshash{$entry};
 1416:             } elsif ($result eq 'unknown_cmd') {
 1417:                 # Target server has old code running on it.
 1418:                 my %temphash=($entry => $value);
 1419:                 if (&put('nohist_resevaldata',\%temphash,$dom,$name) eq 'ok') {
 1420:                     delete $accesshash{$entry};
 1421:                 }
 1422:             }
 1423:         } else {
 1424:             my ($dom,$name) = ($entry=~m:___(\w+)/(\w+)/(.*)___(\w+)$:);
 1425:             my %temphash=($entry => $accesshash{$entry});
 1426:             if (&put('nohist_resevaldata',\%temphash,$dom,$name) eq 'ok') {
 1427:                 delete $accesshash{$entry};
 1428:             }
 1429:         }
 1430:     }
 1431: #
 1432: # Roles
 1433: # Reverse lookup of user roles for course faculty/staff and co-authorship
 1434: #
 1435:     foreach (keys %userrolehash) {
 1436:         my $entry=$_;
 1437:         my ($role,$uname,$udom,$runame,$rudom,$rsec)=
 1438: 	    split(/\:/,$entry);
 1439:         if (&Apache::lonnet::put('nohist_userroles',
 1440:              { $role.':'.$uname.':'.$udom.':'.$rsec => $userrolehash{$entry} },
 1441:                 $rudom,$runame) eq 'ok') {
 1442: 	    delete $userrolehash{$entry};
 1443:         }
 1444:     }
 1445:     $dumpcount++;
 1446: }
 1447: 
 1448: sub courselog {
 1449:     my $what=shift;
 1450:     $what=time.':'.$what;
 1451:     unless ($env{'request.course.id'}) { return ''; }
 1452:     $coursedombuf{$env{'request.course.id'}}=
 1453:        $env{'course.'.$env{'request.course.id'}.'.domain'};
 1454:     $coursenumbuf{$env{'request.course.id'}}=
 1455:        $env{'course.'.$env{'request.course.id'}.'.num'};
 1456:     $coursehombuf{$env{'request.course.id'}}=
 1457:        $env{'course.'.$env{'request.course.id'}.'.home'};
 1458:     $coursedescrbuf{$env{'request.course.id'}}=
 1459:        $env{'course.'.$env{'request.course.id'}.'.description'};
 1460:     $courseinstcodebuf{$env{'request.course.id'}}=
 1461:        $env{'course.'.$env{'request.course.id'}.'.internal.coursecode'};
 1462:     $courseownerbuf{$env{'request.course.id'}}=
 1463:        $env{'course.'.$env{'request.course.id'}.'.internal.courseowner'};
 1464:     if (defined $courselogs{$env{'request.course.id'}}) {
 1465: 	$courselogs{$env{'request.course.id'}}.='&'.$what;
 1466:     } else {
 1467: 	$courselogs{$env{'request.course.id'}}.=$what;
 1468:     }
 1469:     if (length($courselogs{$env{'request.course.id'}})>4048) {
 1470: 	&flushcourselogs();
 1471:     }
 1472: }
 1473: 
 1474: sub courseacclog {
 1475:     my $fnsymb=shift;
 1476:     unless ($env{'request.course.id'}) { return ''; }
 1477:     my $what=$fnsymb.':'.$env{'user.name'}.':'.$env{'user.domain'};
 1478:     if ($fnsymb=~/(problem|exam|quiz|assess|survey|form|page)$/) {
 1479:         $what.=':POST';
 1480:         # FIXME: Probably ought to escape things....
 1481: 	foreach (keys %env) {
 1482:             if ($_=~/^form\.(.*)/) {
 1483: 		$what.=':'.$1.'='.$env{$_};
 1484:             }
 1485:         }
 1486:     } elsif ($fnsymb =~ m:^/adm/searchcat:) {
 1487:         # FIXME: We should not be depending on a form parameter that someone
 1488:         # editing lonsearchcat.pm might change in the future.
 1489:         if ($env{'form.phase'} eq 'course_search') {
 1490:             $what.= ':POST';
 1491:             # FIXME: Probably ought to escape things....
 1492:             foreach my $element ('courseexp','crsfulltext','crsrelated',
 1493:                                  'crsdiscuss') {
 1494:                 $what.=':'.$element.'='.$env{'form.'.$element};
 1495:             }
 1496:         }
 1497:     }
 1498:     &courselog($what);
 1499: }
 1500: 
 1501: sub countacc {
 1502:     my $url=&declutter(shift);
 1503:     return if (! defined($url) || $url eq '');
 1504:     unless ($env{'request.course.id'}) { return ''; }
 1505:     $accesshash{$env{'request.course.id'}.'___'.$url.'___course'}=1;
 1506:     my $key=$$.$processmarker.'_'.$dumpcount.'___'.$url.'___count';
 1507:     $accesshash{$key}++;
 1508: }
 1509: 
 1510: sub linklog {
 1511:     my ($from,$to)=@_;
 1512:     $from=&declutter($from);
 1513:     $to=&declutter($to);
 1514:     $accesshash{$from.'___'.$to.'___comefrom'}=1;
 1515:     $accesshash{$to.'___'.$from.'___goto'}=1;
 1516: }
 1517:   
 1518: sub userrolelog {
 1519:     my ($trole,$username,$domain,$area,$tstart,$tend)=@_;
 1520:     if (($trole=~/^ca/) || ($trole=~/^in/) || 
 1521:         ($trole=~/^cc/) || ($trole=~/^ep/) ||
 1522:         ($trole=~/^cr/) || ($trole=~/^ta/)) {
 1523:        my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
 1524:        $userrolehash
 1525:          {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
 1526:                     =$tend.':'.$tstart;
 1527:    }
 1528: }
 1529: 
 1530: sub get_course_adv_roles {
 1531:     my $cid=shift;
 1532:     $cid=$env{'request.course.id'} unless (defined($cid));
 1533:     my %coursehash=&coursedescription($cid);
 1534:     my %nothide=();
 1535:     foreach (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
 1536: 	$nothide{join(':',split(/[\@\:]/,$_))}=1;
 1537:     }
 1538:     my %returnhash=();
 1539:     my %dumphash=
 1540:             &dump('nohist_userroles',$coursehash{'domain'},$coursehash{'num'});
 1541:     my $now=time;
 1542:     foreach (keys %dumphash) {
 1543: 	my ($tend,$tstart)=split(/\:/,$dumphash{$_});
 1544:         if (($tstart) && ($tstart<0)) { next; }
 1545:         if (($tend) && ($tend<$now)) { next; }
 1546:         if (($tstart) && ($now<$tstart)) { next; }
 1547:         my ($role,$username,$domain,$section)=split(/\:/,$_);
 1548: 	if ($username eq '' || $domain eq '') { next; }
 1549: 	if ((&privileged($username,$domain)) && 
 1550: 	    (!$nothide{$username.':'.$domain})) { next; }
 1551:         my $key=&plaintext($role);
 1552:         if ($section) { $key.=' (Sec/Grp '.$section.')'; }
 1553:         if ($returnhash{$key}) {
 1554: 	    $returnhash{$key}.=','.$username.':'.$domain;
 1555:         } else {
 1556:             $returnhash{$key}=$username.':'.$domain;
 1557:         }
 1558:      }
 1559:     return %returnhash;
 1560: }
 1561: 
 1562: sub get_my_roles {
 1563:     my ($uname,$udom)=@_;
 1564:     unless (defined($uname)) { $uname=$env{'user.name'}; }
 1565:     unless (defined($udom)) { $udom=$env{'user.domain'}; }
 1566:     my %dumphash=
 1567:             &dump('nohist_userroles',$udom,$uname);
 1568:     my %returnhash=();
 1569:     my $now=time;
 1570:     foreach (keys %dumphash) {
 1571: 	my ($tend,$tstart)=split(/\:/,$dumphash{$_});
 1572:         if (($tstart) && ($tstart<0)) { next; }
 1573:         if (($tend) && ($tend<$now)) { next; }
 1574:         if (($tstart) && ($now<$tstart)) { next; }
 1575:         my ($role,$username,$domain,$section)=split(/\:/,$_);
 1576: 	$returnhash{$username.':'.$domain.':'.$role}=$tstart.':'.$tend;
 1577:      }
 1578:     return %returnhash;
 1579: }
 1580: 
 1581: # ----------------------------------------------------- Frontpage Announcements
 1582: #
 1583: #
 1584: 
 1585: sub postannounce {
 1586:     my ($server,$text)=@_;
 1587:     unless (&allowed('psa',$hostdom{$server})) { return 'refused'; }
 1588:     unless ($text=~/\w/) { $text=''; }
 1589:     return &reply('setannounce:'.&escape($text),$server);
 1590: }
 1591: 
 1592: sub getannounce {
 1593: 
 1594:     if (open(my $fh,$perlvar{'lonDocRoot'}.'/announcement.txt')) {
 1595: 	my $announcement='';
 1596: 	while (<$fh>) { $announcement .=$_; }
 1597: 	close($fh);
 1598: 	if ($announcement=~/\w/) { 
 1599: 	    return 
 1600:    '<table bgcolor="#FF5555" cellpadding="5" cellspacing="3">'.
 1601:    '<tr><td bgcolor="#FFFFFF"><tt>'.$announcement.'</tt></td></tr></table>'; 
 1602: 	} else {
 1603: 	    return '';
 1604: 	}
 1605:     } else {
 1606: 	return '';
 1607:     }
 1608: }
 1609: 
 1610: # ---------------------------------------------------------- Course ID routines
 1611: # Deal with domain's nohist_courseid.db files
 1612: #
 1613: 
 1614: sub courseidput {
 1615:     my ($domain,$what,$coursehome)=@_;
 1616:     return &reply('courseidput:'.$domain.':'.$what,$coursehome);
 1617: }
 1618: 
 1619: sub courseiddump {
 1620:     my ($domfilter,$descfilter,$sincefilter,$instcodefilter,$ownerfilter,$coursefilter,$hostidflag,$hostidref)=@_;
 1621:     my %returnhash=();
 1622:     unless ($domfilter) { $domfilter=''; }
 1623:     foreach my $tryserver (keys %libserv) {
 1624:         if ( ($hostidflag == 1 && grep/^$tryserver$/,@{$hostidref}) || (!defined($hostidflag)) ) {
 1625: 	    if ((!$domfilter) || ($hostdom{$tryserver} eq $domfilter)) {
 1626: 	        foreach (
 1627:                  split(/\&/,&reply('courseiddump:'.$hostdom{$tryserver}.':'.
 1628: 			       $sincefilter.':'.&escape($descfilter).':'.
 1629:                                &escape($instcodefilter).':'.&escape($ownerfilter).':'.&escape($coursefilter),
 1630:                                $tryserver))) {
 1631: 		    my ($key,$value)=split(/\=/,$_);
 1632:                     if (($key) && ($value)) {
 1633: 		        $returnhash{&unescape($key)}=$value;
 1634:                     }
 1635:                 }
 1636:             }
 1637:         }
 1638:     }
 1639:     return %returnhash;
 1640: }
 1641: 
 1642: #
 1643: # ----------------------------------------------------------- Check out an item
 1644: 
 1645: sub get_first_access {
 1646:     my ($type,$argsymb)=@_;
 1647:     my ($symb,$courseid,$udom,$uname)=&Apache::lonxml::whichuser();
 1648:     if ($argsymb) { $symb=$argsymb; }
 1649:     my ($map,$id,$res)=&decode_symb($symb);
 1650:     if ($type eq 'map') {
 1651: 	$res=&symbread($map);
 1652:     } else {
 1653: 	$res=$symb;
 1654:     }
 1655:     my %times=&get('firstaccesstimes',["$courseid\0$res"],$udom,$uname);
 1656:     return $times{"$courseid\0$res"};
 1657: }
 1658: 
 1659: sub set_first_access {
 1660:     my ($type)=@_;
 1661:     my ($symb,$courseid,$udom,$uname)=&Apache::lonxml::whichuser();
 1662:     my ($map,$id,$res)=&decode_symb($symb);
 1663:     if ($type eq 'map') {
 1664: 	$res=&symbread($map);
 1665:     } else {
 1666: 	$res=$symb;
 1667:     }
 1668:     my $firstaccess=&get_first_access($type,$symb);
 1669:     if (!$firstaccess) {
 1670: 	return &put('firstaccesstimes',{"$courseid\0$res"=>time},$udom,$uname);
 1671:     }
 1672:     return 'already_set';
 1673: }
 1674: 
 1675: sub checkout {
 1676:     my ($symb,$tuname,$tudom,$tcrsid)=@_;
 1677:     my $now=time;
 1678:     my $lonhost=$perlvar{'lonHostID'};
 1679:     my $infostr=&escape(
 1680:                  'CHECKOUTTOKEN&'.
 1681:                  $tuname.'&'.
 1682:                  $tudom.'&'.
 1683:                  $tcrsid.'&'.
 1684:                  $symb.'&'.
 1685: 		 $now.'&'.$ENV{'REMOTE_ADDR'});
 1686:     my $token=&reply('tmpput:'.$infostr,$lonhost);
 1687:     if ($token=~/^error\:/) { 
 1688:         &logthis("<font color=blue>WARNING: ".
 1689:                 "Checkout tmpput failed ".$tudom.' - '.$tuname.' - '.$symb.
 1690:                  "</font>");
 1691:         return ''; 
 1692:     }
 1693: 
 1694:     $token=~s/^(\d+)\_.*\_(\d+)$/$1\*$2\*$lonhost/;
 1695:     $token=~tr/a-z/A-Z/;
 1696: 
 1697:     my %infohash=('resource.0.outtoken' => $token,
 1698:                   'resource.0.checkouttime' => $now,
 1699:                   'resource.0.outremote' => $ENV{'REMOTE_ADDR'});
 1700: 
 1701:     unless (&cstore(\%infohash,$symb,$tcrsid,$tudom,$tuname) eq 'ok') {
 1702:        return '';
 1703:     } else {
 1704:         &logthis("<font color=blue>WARNING: ".
 1705:                 "Checkout cstore failed ".$tudom.' - '.$tuname.' - '.$symb.
 1706:                  "</font>");
 1707:     }    
 1708: 
 1709:     if (&log($tudom,$tuname,&homeserver($tuname,$tudom),
 1710:                          &escape('Checkout '.$infostr.' - '.
 1711:                                                  $token)) ne 'ok') {
 1712: 	return '';
 1713:     } else {
 1714:         &logthis("<font color=blue>WARNING: ".
 1715:                 "Checkout log failed ".$tudom.' - '.$tuname.' - '.$symb.
 1716:                  "</font>");
 1717:     }
 1718:     return $token;
 1719: }
 1720: 
 1721: # ------------------------------------------------------------ Check in an item
 1722: 
 1723: sub checkin {
 1724:     my $token=shift;
 1725:     my $now=time;
 1726:     my ($ta,$tb,$lonhost)=split(/\*/,$token);
 1727:     $lonhost=~tr/A-Z/a-z/;
 1728:     my $dtoken=$ta.'_'.$hostname{$lonhost}.'_'.$tb;
 1729:     $dtoken=~s/\W/\_/g;
 1730:     my ($dummy,$tuname,$tudom,$tcrsid,$symb,$chtim,$rmaddr)=
 1731:                  split(/\&/,&unescape(&reply('tmpget:'.$dtoken,$lonhost)));
 1732: 
 1733:     unless (($tuname) && ($tudom)) {
 1734:         &logthis('Check in '.$token.' ('.$dtoken.') failed');
 1735:         return '';
 1736:     }
 1737:     
 1738:     unless (&allowed('mgr',$tcrsid)) {
 1739:         &logthis('Check in '.$token.' ('.$dtoken.') unauthorized: '.
 1740:                  $env{'user.name'}.' - '.$env{'user.domain'});
 1741:         return '';
 1742:     }
 1743: 
 1744:     my %infohash=('resource.0.intoken' => $token,
 1745:                   'resource.0.checkintime' => $now,
 1746:                   'resource.0.inremote' => $ENV{'REMOTE_ADDR'});
 1747: 
 1748:     unless (&cstore(\%infohash,$symb,$tcrsid,$tudom,$tuname) eq 'ok') {
 1749:        return '';
 1750:     }    
 1751: 
 1752:     if (&log($tudom,$tuname,&homeserver($tuname,$tudom),
 1753:                          &escape('Checkin - '.$token)) ne 'ok') {
 1754: 	return '';
 1755:     }
 1756: 
 1757:     return ($symb,$tuname,$tudom,$tcrsid);    
 1758: }
 1759: 
 1760: # --------------------------------------------- Set Expire Date for Spreadsheet
 1761: 
 1762: sub expirespread {
 1763:     my ($uname,$udom,$stype,$usymb)=@_;
 1764:     my $cid=$env{'request.course.id'}; 
 1765:     if ($cid) {
 1766:        my $now=time;
 1767:        my $key=$uname.':'.$udom.':'.$stype.':'.$usymb;
 1768:        return &reply('put:'.$env{'course.'.$cid.'.domain'}.':'.
 1769:                             $env{'course.'.$cid.'.num'}.
 1770: 	        	    ':nohist_expirationdates:'.
 1771:                             &escape($key).'='.$now,
 1772:                             $env{'course.'.$cid.'.home'})
 1773:     }
 1774:     return 'ok';
 1775: }
 1776: 
 1777: # ----------------------------------------------------- Devalidate Spreadsheets
 1778: 
 1779: sub devalidate {
 1780:     my ($symb,$uname,$udom)=@_;
 1781:     my $cid=$env{'request.course.id'}; 
 1782:     if ($cid) {
 1783:         # delete the stored spreadsheets for
 1784:         # - the student level sheet of this user in course's homespace
 1785:         # - the assessment level sheet for this resource 
 1786:         #   for this user in user's homespace
 1787: 	# - current conditional state info
 1788: 	my $key=$uname.':'.$udom.':';
 1789:         my $status=
 1790: 	    &del('nohist_calculatedsheets',
 1791: 		 [$key.'studentcalc:'],
 1792: 		 $env{'course.'.$cid.'.domain'},
 1793: 		 $env{'course.'.$cid.'.num'})
 1794: 		.' '.
 1795: 	    &del('nohist_calculatedsheets_'.$cid,
 1796: 		 [$key.'assesscalc:'.$symb],$udom,$uname);
 1797:         unless ($status eq 'ok ok') {
 1798:            &logthis('Could not devalidate spreadsheet '.
 1799:                     $uname.' at '.$udom.' for '.
 1800: 		    $symb.': '.$status);
 1801:         }
 1802: 	&delenv('user.state.'.$cid);
 1803:     }
 1804: }
 1805: 
 1806: sub get_scalar {
 1807:     my ($string,$end) = @_;
 1808:     my $value;
 1809:     if ($$string =~ s/^([^&]*?)($end)/$2/) {
 1810: 	$value = $1;
 1811:     } elsif ($$string =~ s/^([^&]*?)&//) {
 1812: 	$value = $1;
 1813:     }
 1814:     return &unescape($value);
 1815: }
 1816: 
 1817: sub array2str {
 1818:   my (@array) = @_;
 1819:   my $result=&arrayref2str(\@array);
 1820:   $result=~s/^__ARRAY_REF__//;
 1821:   $result=~s/__END_ARRAY_REF__$//;
 1822:   return $result;
 1823: }
 1824: 
 1825: sub arrayref2str {
 1826:   my ($arrayref) = @_;
 1827:   my $result='__ARRAY_REF__';
 1828:   foreach my $elem (@$arrayref) {
 1829:     if(ref($elem) eq 'ARRAY') {
 1830:       $result.=&arrayref2str($elem).'&';
 1831:     } elsif(ref($elem) eq 'HASH') {
 1832:       $result.=&hashref2str($elem).'&';
 1833:     } elsif(ref($elem)) {
 1834:       #print("Got a ref of ".(ref($elem))." skipping.");
 1835:     } else {
 1836:       $result.=&escape($elem).'&';
 1837:     }
 1838:   }
 1839:   $result=~s/\&$//;
 1840:   $result .= '__END_ARRAY_REF__';
 1841:   return $result;
 1842: }
 1843: 
 1844: sub hash2str {
 1845:   my (%hash) = @_;
 1846:   my $result=&hashref2str(\%hash);
 1847:   $result=~s/^__HASH_REF__//;
 1848:   $result=~s/__END_HASH_REF__$//;
 1849:   return $result;
 1850: }
 1851: 
 1852: sub hashref2str {
 1853:   my ($hashref)=@_;
 1854:   my $result='__HASH_REF__';
 1855:   foreach (sort(keys(%$hashref))) {
 1856:     if (ref($_) eq 'ARRAY') {
 1857:       $result.=&arrayref2str($_).'=';
 1858:     } elsif (ref($_) eq 'HASH') {
 1859:       $result.=&hashref2str($_).'=';
 1860:     } elsif (ref($_)) {
 1861:       $result.='=';
 1862:       #print("Got a ref of ".(ref($_))." skipping.");
 1863:     } else {
 1864: 	if ($_) {$result.=&escape($_).'=';} else { last; }
 1865:     }
 1866: 
 1867:     if(ref($hashref->{$_}) eq 'ARRAY') {
 1868:       $result.=&arrayref2str($hashref->{$_}).'&';
 1869:     } elsif(ref($hashref->{$_}) eq 'HASH') {
 1870:       $result.=&hashref2str($hashref->{$_}).'&';
 1871:     } elsif(ref($hashref->{$_})) {
 1872:        $result.='&';
 1873:       #print("Got a ref of ".(ref($hashref->{$_}))." skipping.");
 1874:     } else {
 1875:       $result.=&escape($hashref->{$_}).'&';
 1876:     }
 1877:   }
 1878:   $result=~s/\&$//;
 1879:   $result .= '__END_HASH_REF__';
 1880:   return $result;
 1881: }
 1882: 
 1883: sub str2hash {
 1884:     my ($string)=@_;
 1885:     my ($hash)=&str2hashref('__HASH_REF__'.$string.'__END_HASH_REF__');
 1886:     return %$hash;
 1887: }
 1888: 
 1889: sub str2hashref {
 1890:   my ($string) = @_;
 1891: 
 1892:   my %hash;
 1893: 
 1894:   if($string !~ /^__HASH_REF__/) {
 1895:       if (! ($string eq '' || !defined($string))) {
 1896: 	  $hash{'error'}='Not hash reference';
 1897:       }
 1898:       return (\%hash, $string);
 1899:   }
 1900: 
 1901:   $string =~ s/^__HASH_REF__//;
 1902: 
 1903:   while($string !~ /^__END_HASH_REF__/) {
 1904:       #key
 1905:       my $key='';
 1906:       if($string =~ /^__HASH_REF__/) {
 1907:           ($key, $string)=&str2hashref($string);
 1908:           if(defined($key->{'error'})) {
 1909:               $hash{'error'}='Bad data';
 1910:               return (\%hash, $string);
 1911:           }
 1912:       } elsif($string =~ /^__ARRAY_REF__/) {
 1913:           ($key, $string)=&str2arrayref($string);
 1914:           if($key->[0] eq 'Array reference error') {
 1915:               $hash{'error'}='Bad data';
 1916:               return (\%hash, $string);
 1917:           }
 1918:       } else {
 1919:           $string =~ s/^(.*?)=//;
 1920: 	  $key=&unescape($1);
 1921:       }
 1922:       $string =~ s/^=//;
 1923: 
 1924:       #value
 1925:       my $value='';
 1926:       if($string =~ /^__HASH_REF__/) {
 1927:           ($value, $string)=&str2hashref($string);
 1928:           if(defined($value->{'error'})) {
 1929:               $hash{'error'}='Bad data';
 1930:               return (\%hash, $string);
 1931:           }
 1932:       } elsif($string =~ /^__ARRAY_REF__/) {
 1933:           ($value, $string)=&str2arrayref($string);
 1934:           if($value->[0] eq 'Array reference error') {
 1935:               $hash{'error'}='Bad data';
 1936:               return (\%hash, $string);
 1937:           }
 1938:       } else {
 1939: 	  $value=&get_scalar(\$string,'__END_HASH_REF__');
 1940:       }
 1941:       $string =~ s/^&//;
 1942: 
 1943:       $hash{$key}=$value;
 1944:   }
 1945: 
 1946:   $string =~ s/^__END_HASH_REF__//;
 1947: 
 1948:   return (\%hash, $string);
 1949: }
 1950: 
 1951: sub str2array {
 1952:     my ($string)=@_;
 1953:     my ($array)=&str2arrayref('__ARRAY_REF__'.$string.'__END_ARRAY_REF__');
 1954:     return @$array;
 1955: }
 1956: 
 1957: sub str2arrayref {
 1958:   my ($string) = @_;
 1959:   my @array;
 1960: 
 1961:   if($string !~ /^__ARRAY_REF__/) {
 1962:       if (! ($string eq '' || !defined($string))) {
 1963: 	  $array[0]='Array reference error';
 1964:       }
 1965:       return (\@array, $string);
 1966:   }
 1967: 
 1968:   $string =~ s/^__ARRAY_REF__//;
 1969: 
 1970:   while($string !~ /^__END_ARRAY_REF__/) {
 1971:       my $value='';
 1972:       if($string =~ /^__HASH_REF__/) {
 1973:           ($value, $string)=&str2hashref($string);
 1974:           if(defined($value->{'error'})) {
 1975:               $array[0] ='Array reference error';
 1976:               return (\@array, $string);
 1977:           }
 1978:       } elsif($string =~ /^__ARRAY_REF__/) {
 1979:           ($value, $string)=&str2arrayref($string);
 1980:           if($value->[0] eq 'Array reference error') {
 1981:               $array[0] ='Array reference error';
 1982:               return (\@array, $string);
 1983:           }
 1984:       } else {
 1985: 	  $value=&get_scalar(\$string,'__END_ARRAY_REF__');
 1986:       }
 1987:       $string =~ s/^&//;
 1988: 
 1989:       push(@array, $value);
 1990:   }
 1991: 
 1992:   $string =~ s/^__END_ARRAY_REF__//;
 1993: 
 1994:   return (\@array, $string);
 1995: }
 1996: 
 1997: # -------------------------------------------------------------------Temp Store
 1998: 
 1999: sub tmpreset {
 2000:   my ($symb,$namespace,$domain,$stuname) = @_;
 2001:   if (!$symb) {
 2002:     $symb=&symbread();
 2003:     if (!$symb) { $symb= $env{'request.url'}; }
 2004:   }
 2005:   $symb=escape($symb);
 2006: 
 2007:   if (!$namespace) { $namespace=$env{'request.state'}; }
 2008:   $namespace=~s/\//\_/g;
 2009:   $namespace=~s/\W//g;
 2010: 
 2011:   if (!$domain) { $domain=$env{'user.domain'}; }
 2012:   if (!$stuname) { $stuname=$env{'user.name'}; }
 2013:   if ($domain eq 'public' && $stuname eq 'public') {
 2014:       $stuname=$ENV{'REMOTE_ADDR'};
 2015:   }
 2016:   my $path=$perlvar{'lonDaemons'}.'/tmp';
 2017:   my %hash;
 2018:   if (tie(%hash,'GDBM_File',
 2019: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
 2020: 	  &GDBM_WRCREAT(),0640)) {
 2021:     foreach my $key (keys %hash) {
 2022:       if ($key=~ /:$symb/) {
 2023: 	delete($hash{$key});
 2024:       }
 2025:     }
 2026:   }
 2027: }
 2028: 
 2029: sub tmpstore {
 2030:   my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
 2031: 
 2032:   if (!$symb) {
 2033:     $symb=&symbread();
 2034:     if (!$symb) { $symb= $env{'request.url'}; }
 2035:   }
 2036:   $symb=escape($symb);
 2037: 
 2038:   if (!$namespace) {
 2039:     # I don't think we would ever want to store this for a course.
 2040:     # it seems this will only be used if we don't have a course.
 2041:     #$namespace=$env{'request.course.id'};
 2042:     #if (!$namespace) {
 2043:       $namespace=$env{'request.state'};
 2044:     #}
 2045:   }
 2046:   $namespace=~s/\//\_/g;
 2047:   $namespace=~s/\W//g;
 2048:   if (!$domain) { $domain=$env{'user.domain'}; }
 2049:   if (!$stuname) { $stuname=$env{'user.name'}; }
 2050:   if ($domain eq 'public' && $stuname eq 'public') {
 2051:       $stuname=$ENV{'REMOTE_ADDR'};
 2052:   }
 2053:   my $now=time;
 2054:   my %hash;
 2055:   my $path=$perlvar{'lonDaemons'}.'/tmp';
 2056:   if (tie(%hash,'GDBM_File',
 2057: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
 2058: 	  &GDBM_WRCREAT(),0640)) {
 2059:     $hash{"version:$symb"}++;
 2060:     my $version=$hash{"version:$symb"};
 2061:     my $allkeys=''; 
 2062:     foreach my $key (keys(%$storehash)) {
 2063:       $allkeys.=$key.':';
 2064:       $hash{"$version:$symb:$key"}=&freeze_escape($$storehash{$key});
 2065:     }
 2066:     $hash{"$version:$symb:timestamp"}=$now;
 2067:     $allkeys.='timestamp';
 2068:     $hash{"$version:keys:$symb"}=$allkeys;
 2069:     if (untie(%hash)) {
 2070:       return 'ok';
 2071:     } else {
 2072:       return "error:$!";
 2073:     }
 2074:   } else {
 2075:     return "error:$!";
 2076:   }
 2077: }
 2078: 
 2079: # -----------------------------------------------------------------Temp Restore
 2080: 
 2081: sub tmprestore {
 2082:   my ($symb,$namespace,$domain,$stuname) = @_;
 2083: 
 2084:   if (!$symb) {
 2085:     $symb=&symbread();
 2086:     if (!$symb) { $symb= $env{'request.url'}; }
 2087:   }
 2088:   $symb=escape($symb);
 2089: 
 2090:   if (!$namespace) { $namespace=$env{'request.state'}; }
 2091: 
 2092:   if (!$domain) { $domain=$env{'user.domain'}; }
 2093:   if (!$stuname) { $stuname=$env{'user.name'}; }
 2094:   if ($domain eq 'public' && $stuname eq 'public') {
 2095:       $stuname=$ENV{'REMOTE_ADDR'};
 2096:   }
 2097:   my %returnhash;
 2098:   $namespace=~s/\//\_/g;
 2099:   $namespace=~s/\W//g;
 2100:   my %hash;
 2101:   my $path=$perlvar{'lonDaemons'}.'/tmp';
 2102:   if (tie(%hash,'GDBM_File',
 2103: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
 2104: 	  &GDBM_READER(),0640)) {
 2105:     my $version=$hash{"version:$symb"};
 2106:     $returnhash{'version'}=$version;
 2107:     my $scope;
 2108:     for ($scope=1;$scope<=$version;$scope++) {
 2109:       my $vkeys=$hash{"$scope:keys:$symb"};
 2110:       my @keys=split(/:/,$vkeys);
 2111:       my $key;
 2112:       $returnhash{"$scope:keys"}=$vkeys;
 2113:       foreach $key (@keys) {
 2114: 	$returnhash{"$scope:$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
 2115: 	$returnhash{"$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
 2116:       }
 2117:     }
 2118:     if (!(untie(%hash))) {
 2119:       return "error:$!";
 2120:     }
 2121:   } else {
 2122:     return "error:$!";
 2123:   }
 2124:   return %returnhash;
 2125: }
 2126: 
 2127: # ----------------------------------------------------------------------- Store
 2128: 
 2129: sub store {
 2130:     my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
 2131:     my $home='';
 2132: 
 2133:     if ($stuname) { $home=&homeserver($stuname,$domain); }
 2134: 
 2135:     $symb=&symbclean($symb);
 2136:     if (!$symb) { unless ($symb=&symbread()) { return ''; } }
 2137: 
 2138:     if (!$domain) { $domain=$env{'user.domain'}; }
 2139:     if (!$stuname) { $stuname=$env{'user.name'}; }
 2140: 
 2141:     &devalidate($symb,$stuname,$domain);
 2142: 
 2143:     $symb=escape($symb);
 2144:     if (!$namespace) { 
 2145:        unless ($namespace=$env{'request.course.id'}) { 
 2146:           return ''; 
 2147:        } 
 2148:     }
 2149:     if (!$home) { $home=$env{'user.home'}; }
 2150: 
 2151:     $$storehash{'ip'}=$ENV{'REMOTE_ADDR'};
 2152:     $$storehash{'host'}=$perlvar{'lonHostID'};
 2153: 
 2154:     my $namevalue='';
 2155:     foreach (keys %$storehash) {
 2156:         $namevalue.=&escape($_).'='.&freeze_escape($$storehash{$_}).'&';
 2157:     }
 2158:     $namevalue=~s/\&$//;
 2159:     &courselog($symb.':'.$stuname.':'.$domain.':STORE:'.$namevalue);
 2160:     return reply("store:$domain:$stuname:$namespace:$symb:$namevalue","$home");
 2161: }
 2162: 
 2163: # -------------------------------------------------------------- Critical Store
 2164: 
 2165: sub cstore {
 2166:     my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
 2167:     my $home='';
 2168: 
 2169:     if ($stuname) { $home=&homeserver($stuname,$domain); }
 2170: 
 2171:     $symb=&symbclean($symb);
 2172:     if (!$symb) { unless ($symb=&symbread()) { return ''; } }
 2173: 
 2174:     if (!$domain) { $domain=$env{'user.domain'}; }
 2175:     if (!$stuname) { $stuname=$env{'user.name'}; }
 2176: 
 2177:     &devalidate($symb,$stuname,$domain);
 2178: 
 2179:     $symb=escape($symb);
 2180:     if (!$namespace) { 
 2181:        unless ($namespace=$env{'request.course.id'}) { 
 2182:           return ''; 
 2183:        } 
 2184:     }
 2185:     if (!$home) { $home=$env{'user.home'}; }
 2186: 
 2187:     $$storehash{'ip'}=$ENV{'REMOTE_ADDR'};
 2188:     $$storehash{'host'}=$perlvar{'lonHostID'};
 2189: 
 2190:     my $namevalue='';
 2191:     foreach (keys %$storehash) {
 2192:         $namevalue.=&escape($_).'='.&freeze_escape($$storehash{$_}).'&';
 2193:     }
 2194:     $namevalue=~s/\&$//;
 2195:     &courselog($symb.':'.$stuname.':'.$domain.':CSTORE:'.$namevalue);
 2196:     return critical
 2197:                 ("store:$domain:$stuname:$namespace:$symb:$namevalue","$home");
 2198: }
 2199: 
 2200: # --------------------------------------------------------------------- Restore
 2201: 
 2202: sub restore {
 2203:     my ($symb,$namespace,$domain,$stuname) = @_;
 2204:     my $home='';
 2205: 
 2206:     if ($stuname) { $home=&homeserver($stuname,$domain); }
 2207: 
 2208:     if (!$symb) {
 2209:       unless ($symb=escape(&symbread())) { return ''; }
 2210:     } else {
 2211:       $symb=&escape(&symbclean($symb));
 2212:     }
 2213:     if (!$namespace) { 
 2214:        unless ($namespace=$env{'request.course.id'}) { 
 2215:           return ''; 
 2216:        } 
 2217:     }
 2218:     if (!$domain) { $domain=$env{'user.domain'}; }
 2219:     if (!$stuname) { $stuname=$env{'user.name'}; }
 2220:     if (!$home) { $home=$env{'user.home'}; }
 2221:     my $answer=&reply("restore:$domain:$stuname:$namespace:$symb","$home");
 2222: 
 2223:     my %returnhash=();
 2224:     foreach (split(/\&/,$answer)) {
 2225: 	my ($name,$value)=split(/\=/,$_);
 2226:         $returnhash{&unescape($name)}=&thaw_unescape($value);
 2227:     }
 2228:     my $version;
 2229:     for ($version=1;$version<=$returnhash{'version'};$version++) {
 2230:        foreach (split(/\:/,$returnhash{$version.':keys'})) {
 2231:           $returnhash{$_}=$returnhash{$version.':'.$_};
 2232:        }
 2233:     }
 2234:     return %returnhash;
 2235: }
 2236: 
 2237: # ---------------------------------------------------------- Course Description
 2238: 
 2239: sub coursedescription {
 2240:     my $courseid=shift;
 2241:     $courseid=~s/^\///;
 2242:     $courseid=~s/\_/\//g;
 2243:     my ($cdomain,$cnum)=split(/\//,$courseid);
 2244:     my $chome=&homeserver($cnum,$cdomain);
 2245:     my $normalid=$cdomain.'_'.$cnum;
 2246:     # need to always cache even if we get errors otherwise we keep 
 2247:     # trying and trying and trying to get the course description.
 2248:     my %envhash=();
 2249:     my %returnhash=();
 2250:     $envhash{'course.'.$normalid.'.last_cache'}=time;
 2251:     if ($chome ne 'no_host') {
 2252:        %returnhash=&dump('environment',$cdomain,$cnum);
 2253:        if (!exists($returnhash{'con_lost'})) {
 2254:            $returnhash{'home'}= $chome;
 2255: 	   $returnhash{'domain'} = $cdomain;
 2256: 	   $returnhash{'num'} = $cnum;
 2257:            while (my ($name,$value) = each %returnhash) {
 2258:                $envhash{'course.'.$normalid.'.'.$name}=$value;
 2259:            }
 2260:            $returnhash{'url'}=&clutter($returnhash{'url'});
 2261:            $returnhash{'fn'}=$perlvar{'lonDaemons'}.'/tmp/'.
 2262: 	       $env{'user.name'}.'_'.$cdomain.'_'.$cnum;
 2263:            $envhash{'course.'.$normalid.'.home'}=$chome;
 2264:            $envhash{'course.'.$normalid.'.domain'}=$cdomain;
 2265:            $envhash{'course.'.$normalid.'.num'}=$cnum;
 2266:        }
 2267:     }
 2268:     &appenv(%envhash);
 2269:     return %returnhash;
 2270: }
 2271: 
 2272: # -------------------------------------------------See if a user is privileged
 2273: 
 2274: sub privileged {
 2275:     my ($username,$domain)=@_;
 2276:     my $rolesdump=&reply("dump:$domain:$username:roles",
 2277: 			&homeserver($username,$domain));
 2278:     if (($rolesdump eq 'con_lost') || ($rolesdump eq '')) { return 0; }
 2279:     my $now=time;
 2280:     if ($rolesdump ne '') {
 2281:         foreach (split(/&/,$rolesdump)) {
 2282: 	    if ($_!~/^rolesdef_/) {
 2283: 		my ($area,$role)=split(/=/,$_);
 2284: 		$area=~s/\_\w\w$//;
 2285: 		my ($trole,$tend,$tstart)=split(/_/,$role);
 2286: 		if (($trole eq 'dc') || ($trole eq 'su')) {
 2287: 		    my $active=1;
 2288: 		    if ($tend) {
 2289: 			if ($tend<$now) { $active=0; }
 2290: 		    }
 2291: 		    if ($tstart) {
 2292: 			if ($tstart>$now) { $active=0; }
 2293: 		    }
 2294: 		    if ($active) { return 1; }
 2295: 		}
 2296: 	    }
 2297: 	}
 2298:     }
 2299:     return 0;
 2300: }
 2301: 
 2302: # -------------------------------------------------------- Get user privileges
 2303: 
 2304: sub rolesinit {
 2305:     my ($domain,$username,$authhost)=@_;
 2306:     my $rolesdump=reply("dump:$domain:$username:roles",$authhost);
 2307:     if (($rolesdump eq 'con_lost') || ($rolesdump eq '')) { return ''; }
 2308:     my %allroles=();
 2309:     my $now=time;
 2310:     my $userroles="user.login.time=$now\n";
 2311: 
 2312:     if ($rolesdump ne '') {
 2313:         foreach (split(/&/,$rolesdump)) {
 2314: 	  if ($_!~/^rolesdef_/) {
 2315:             my ($area,$role)=split(/=/,$_);
 2316: 	    $area=~s/\_\w\w$//;
 2317: 	    
 2318:             my ($trole,$tend,$tstart);
 2319: 	    if ($role=~/^cr/) { 
 2320: 		($trole,my $trest)=($role=~m|^(cr/\w+/\w+/[a-zA-Z0-9]+)_(.*)$|);
 2321: 		($tend,$tstart)=split('_',$trest);
 2322: 	    } else {
 2323: 		($trole,$tend,$tstart)=split(/_/,$role);
 2324: 	    }
 2325:             $userroles.=&set_arearole($trole,$area,$tstart,$tend,$domain,$username);
 2326:             if (($tend!=0) && ($tend<$now)) { $trole=''; }
 2327:             if (($tstart!=0) && ($tstart>$now)) { $trole=''; }
 2328:             if (($area ne '') && ($trole ne '')) {
 2329: 		my $spec=$trole.'.'.$area;
 2330: 		my ($tdummy,$tdomain,$trest)=split(/\//,$area);
 2331: 		if ($trole =~ /^cr\//) {
 2332:                     &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
 2333: 		} else {
 2334:                     &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
 2335: 		}
 2336:             }
 2337:           } 
 2338:         }
 2339:         my ($author,$adv) = &set_userprivs(\$userroles,\%allroles);
 2340:         $userroles.='user.adv='.$adv."\n".
 2341: 	            'user.author='.$author."\n";
 2342:         $env{'user.adv'}=$adv;
 2343:     }
 2344:     return $userroles;  
 2345: }
 2346: 
 2347: sub set_arearole {
 2348:     my ($trole,$area,$tstart,$tend,$domain,$username) = @_;
 2349: # log the associated role with the area
 2350:     &userrolelog($trole,$username,$domain,$area,$tstart,$tend);
 2351:     return 'user.role.'.$trole.'.'.$area.'='.$tstart.'.'.$tend."\n";
 2352: }
 2353: 
 2354: sub custom_roleprivs {
 2355:     my ($allroles,$trole,$tdomain,$trest,$spec,$area) = @_;
 2356:     my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$trole);
 2357:     my $homsvr=homeserver($rauthor,$rdomain);
 2358:     if ($hostname{$homsvr} ne '') {
 2359:         my ($rdummy,$roledef)=
 2360:             &get('roles',["rolesdef_$rrole"],$rdomain,$rauthor);
 2361:         if (($rdummy ne 'con_lost') && ($roledef ne '')) {
 2362:             my ($syspriv,$dompriv,$coursepriv)=split(/\_/,$roledef);
 2363:             if (defined($syspriv)) {
 2364:                 $$allroles{'cm./'}.=':'.$syspriv;
 2365:                 $$allroles{$spec.'./'}.=':'.$syspriv;
 2366:             }
 2367:             if ($tdomain ne '') {
 2368:                 if (defined($dompriv)) {
 2369:                     $$allroles{'cm./'.$tdomain.'/'}.=':'.$dompriv;
 2370:                     $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$dompriv;
 2371:                 }
 2372:                 if (($trest ne '') && (defined($coursepriv))) {
 2373:                     $$allroles{'cm.'.$area}.=':'.$coursepriv;
 2374:                     $$allroles{$spec.'.'.$area}.=':'.$coursepriv;
 2375:                 }
 2376:             }
 2377:         }
 2378:     }
 2379: }
 2380: 
 2381: 
 2382: sub standard_roleprivs {
 2383:     my ($allroles,$trole,$tdomain,$spec,$trest,$area) = @_;
 2384:     if (defined($pr{$trole.':s'})) {
 2385:         $$allroles{'cm./'}.=':'.$pr{$trole.':s'};
 2386:         $$allroles{$spec.'./'}.=':'.$pr{$trole.':s'};
 2387:     }
 2388:     if ($tdomain ne '') {
 2389:         if (defined($pr{$trole.':d'})) {
 2390:             $$allroles{'cm./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
 2391:             $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
 2392:         }
 2393:         if (($trest ne '') && (defined($pr{$trole.':c'}))) {
 2394:             $$allroles{'cm.'.$area}.=':'.$pr{$trole.':c'};
 2395:             $$allroles{$spec.'.'.$area}.=':'.$pr{$trole.':c'};
 2396:         }
 2397:     }
 2398: }
 2399: 
 2400: sub set_userprivs {
 2401:     my ($userroles,$allroles) = @_; 
 2402:     my $author=0;
 2403:     my $adv=0;
 2404:     foreach (keys %{$allroles}) {
 2405:         my %thesepriv=();
 2406:         if (($_=~/^au/) || ($_=~/^ca/)) { $author=1; }
 2407:         foreach (split(/:/,$$allroles{$_})) {
 2408:             if ($_ ne '') {
 2409:                 my ($privilege,$restrictions)=split(/&/,$_);
 2410:                 if ($restrictions eq '') {
 2411:                     $thesepriv{$privilege}='F';
 2412:                 } elsif ($thesepriv{$privilege} ne 'F') {
 2413:                     $thesepriv{$privilege}.=$restrictions;
 2414:                 }
 2415:                 if ($thesepriv{'adv'} eq 'F') { $adv=1; }
 2416:             }
 2417:         }
 2418:         my $thesestr='';
 2419:         foreach (keys %thesepriv) { $thesestr.=':'.$_.'&'.$thesepriv{$_}; }
 2420:         $$userroles.='user.priv.'.$_.'='.$thesestr."\n";
 2421:     }
 2422:     return ($author,$adv);
 2423: }
 2424: 
 2425: # --------------------------------------------------------------- get interface
 2426: 
 2427: sub get {
 2428:    my ($namespace,$storearr,$udomain,$uname)=@_;
 2429:    my $items='';
 2430:    foreach (@$storearr) {
 2431:        $items.=escape($_).'&';
 2432:    }
 2433:    $items=~s/\&$//;
 2434:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 2435:    if (!$uname) { $uname=$env{'user.name'}; }
 2436:    my $uhome=&homeserver($uname,$udomain);
 2437: 
 2438:    my $rep=&reply("get:$udomain:$uname:$namespace:$items",$uhome);
 2439:    my @pairs=split(/\&/,$rep);
 2440:    if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
 2441:      return @pairs;
 2442:    }
 2443:    my %returnhash=();
 2444:    my $i=0;
 2445:    foreach (@$storearr) {
 2446:       $returnhash{$_}=&thaw_unescape($pairs[$i]);
 2447:       $i++;
 2448:    }
 2449:    return %returnhash;
 2450: }
 2451: 
 2452: # --------------------------------------------------------------- del interface
 2453: 
 2454: sub del {
 2455:    my ($namespace,$storearr,$udomain,$uname)=@_;
 2456:    my $items='';
 2457:    foreach (@$storearr) {
 2458:        $items.=escape($_).'&';
 2459:    }
 2460:    $items=~s/\&$//;
 2461:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 2462:    if (!$uname) { $uname=$env{'user.name'}; }
 2463:    my $uhome=&homeserver($uname,$udomain);
 2464: 
 2465:    return &reply("del:$udomain:$uname:$namespace:$items",$uhome);
 2466: }
 2467: 
 2468: # -------------------------------------------------------------- dump interface
 2469: 
 2470: sub dump {
 2471:    my ($namespace,$udomain,$uname,$regexp)=@_;
 2472:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 2473:    if (!$uname) { $uname=$env{'user.name'}; }
 2474:    my $uhome=&homeserver($uname,$udomain);
 2475:    if ($regexp) {
 2476:        $regexp=&escape($regexp);
 2477:    } else {
 2478:        $regexp='.';
 2479:    }
 2480:    my $rep=reply("dump:$udomain:$uname:$namespace:$regexp",$uhome);
 2481:    my @pairs=split(/\&/,$rep);
 2482:    my %returnhash=();
 2483:    foreach (@pairs) {
 2484:       my ($key,$value)=split(/=/,$_);
 2485:       $returnhash{unescape($key)}=&thaw_unescape($value);
 2486:    }
 2487:    return %returnhash;
 2488: }
 2489: 
 2490: # -------------------------------------------------------------- keys interface
 2491: 
 2492: sub getkeys {
 2493:    my ($namespace,$udomain,$uname)=@_;
 2494:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 2495:    if (!$uname) { $uname=$env{'user.name'}; }
 2496:    my $uhome=&homeserver($uname,$udomain);
 2497:    my $rep=reply("keys:$udomain:$uname:$namespace",$uhome);
 2498:    my @keyarray=();
 2499:    foreach (split(/\&/,$rep)) {
 2500:       push (@keyarray,&unescape($_));
 2501:    }
 2502:    return @keyarray;
 2503: }
 2504: 
 2505: # --------------------------------------------------------------- currentdump
 2506: sub currentdump {
 2507:    my ($courseid,$sdom,$sname)=@_;
 2508:    $courseid = $env{'request.course.id'} if (! defined($courseid));
 2509:    $sdom     = $env{'user.domain'}       if (! defined($sdom));
 2510:    $sname    = $env{'user.name'}         if (! defined($sname));
 2511:    my $uhome = &homeserver($sname,$sdom);
 2512:    my $rep=reply('currentdump:'.$sdom.':'.$sname.':'.$courseid,$uhome);
 2513:    return if ($rep =~ /^(error:|no_such_host)/);
 2514:    #
 2515:    my %returnhash=();
 2516:    #
 2517:    if ($rep eq "unknown_cmd") { 
 2518:        # an old lond will not know currentdump
 2519:        # Do a dump and make it look like a currentdump
 2520:        my @tmp = &dump($courseid,$sdom,$sname,'.');
 2521:        return if ($tmp[0] =~ /^(error:|no_such_host)/);
 2522:        my %hash = @tmp;
 2523:        @tmp=();
 2524:        %returnhash = %{&convert_dump_to_currentdump(\%hash)};
 2525:    } else {
 2526:        my @pairs=split(/\&/,$rep);
 2527:        foreach (@pairs) {
 2528:            my ($key,$value)=split(/=/,$_);
 2529:            my ($symb,$param) = split(/:/,$key);
 2530:            $returnhash{&unescape($symb)}->{&unescape($param)} = 
 2531:                                                         &thaw_unescape($value);
 2532:        }
 2533:    }
 2534:    return %returnhash;
 2535: }
 2536: 
 2537: sub convert_dump_to_currentdump{
 2538:     my %hash = %{shift()};
 2539:     my %returnhash;
 2540:     # Code ripped from lond, essentially.  The only difference
 2541:     # here is the unescaping done by lonnet::dump().  Conceivably
 2542:     # we might run in to problems with parameter names =~ /^v\./
 2543:     while (my ($key,$value) = each(%hash)) {
 2544:         my ($v,$symb,$param) = split(/:/,$key);
 2545:         next if ($v eq 'version' || $symb eq 'keys');
 2546:         next if (exists($returnhash{$symb}) &&
 2547:                  exists($returnhash{$symb}->{$param}) &&
 2548:                  $returnhash{$symb}->{'v.'.$param} > $v);
 2549:         $returnhash{$symb}->{$param}=$value;
 2550:         $returnhash{$symb}->{'v.'.$param}=$v;
 2551:     }
 2552:     #
 2553:     # Remove all of the keys in the hashes which keep track of
 2554:     # the version of the parameter.
 2555:     while (my ($symb,$param_hash) = each(%returnhash)) {
 2556:         # use a foreach because we are going to delete from the hash.
 2557:         foreach my $key (keys(%$param_hash)) {
 2558:             delete($param_hash->{$key}) if ($key =~ /^v\./);
 2559:         }
 2560:     }
 2561:     return \%returnhash;
 2562: }
 2563: 
 2564: # ------------------------------------------------------ critical inc interface
 2565: 
 2566: sub cinc {
 2567:     return &inc(@_,'critical');
 2568: }
 2569: 
 2570: # --------------------------------------------------------------- inc interface
 2571: 
 2572: sub inc {
 2573:     my ($namespace,$store,$udomain,$uname,$critical) = @_;
 2574:     if (!$udomain) { $udomain=$env{'user.domain'}; }
 2575:     if (!$uname) { $uname=$env{'user.name'}; }
 2576:     my $uhome=&homeserver($uname,$udomain);
 2577:     my $items='';
 2578:     if (! ref($store)) {
 2579:         # got a single value, so use that instead
 2580:         $items = &escape($store).'=&';
 2581:     } elsif (ref($store) eq 'SCALAR') {
 2582:         $items = &escape($$store).'=&';        
 2583:     } elsif (ref($store) eq 'ARRAY') {
 2584:         $items = join('=&',map {&escape($_);} @{$store});
 2585:     } elsif (ref($store) eq 'HASH') {
 2586:         while (my($key,$value) = each(%{$store})) {
 2587:             $items.= &escape($key).'='.&escape($value).'&';
 2588:         }
 2589:     }
 2590:     $items=~s/\&$//;
 2591:     if ($critical) {
 2592: 	return &critical("inc:$udomain:$uname:$namespace:$items",$uhome);
 2593:     } else {
 2594: 	return &reply("inc:$udomain:$uname:$namespace:$items",$uhome);
 2595:     }
 2596: }
 2597: 
 2598: # --------------------------------------------------------------- put interface
 2599: 
 2600: sub put {
 2601:    my ($namespace,$storehash,$udomain,$uname)=@_;
 2602:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 2603:    if (!$uname) { $uname=$env{'user.name'}; }
 2604:    my $uhome=&homeserver($uname,$udomain);
 2605:    my $items='';
 2606:    foreach (keys %$storehash) {
 2607:        $items.=&escape($_).'='.&freeze_escape($$storehash{$_}).'&';
 2608:    }
 2609:    $items=~s/\&$//;
 2610:    return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
 2611: }
 2612: 
 2613: # ---------------------------------------------------------- putstore interface
 2614:                                                                                      
 2615: sub putstore {
 2616:    my ($namespace,$storehash,$udomain,$uname)=@_;
 2617:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 2618:    if (!$uname) { $uname=$env{'user.name'}; }
 2619:    my $uhome=&homeserver($uname,$udomain);
 2620:    my $items='';
 2621:    my %allitems = ();
 2622:    foreach (keys %$storehash) {
 2623:        if ($_ =~ m/^([^\:]+):([^\:]+):([^\:]+)$/) {
 2624:            my $key = $1.':keys:'.$2;
 2625:            $allitems{$key} .= $3.':';
 2626:        }
 2627:        $items.=$_.'='.&freeze_escape($$storehash{$_}).'&';
 2628:    }
 2629:    foreach (keys %allitems) {
 2630:        $allitems{$_} =~ s/\:$//;
 2631:        $items.= $_.'='.$allitems{$_}.'&';
 2632:    }
 2633:    $items=~s/\&$//;
 2634:    return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
 2635: }
 2636: 
 2637: # ------------------------------------------------------ critical put interface
 2638: 
 2639: sub cput {
 2640:    my ($namespace,$storehash,$udomain,$uname)=@_;
 2641:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 2642:    if (!$uname) { $uname=$env{'user.name'}; }
 2643:    my $uhome=&homeserver($uname,$udomain);
 2644:    my $items='';
 2645:    foreach (keys %$storehash) {
 2646:        $items.=escape($_).'='.&freeze_escape($$storehash{$_}).'&';
 2647:    }
 2648:    $items=~s/\&$//;
 2649:    return &critical("put:$udomain:$uname:$namespace:$items",$uhome);
 2650: }
 2651: 
 2652: # -------------------------------------------------------------- eget interface
 2653: 
 2654: sub eget {
 2655:    my ($namespace,$storearr,$udomain,$uname)=@_;
 2656:    my $items='';
 2657:    foreach (@$storearr) {
 2658:        $items.=escape($_).'&';
 2659:    }
 2660:    $items=~s/\&$//;
 2661:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 2662:    if (!$uname) { $uname=$env{'user.name'}; }
 2663:    my $uhome=&homeserver($uname,$udomain);
 2664:    my $rep=&reply("eget:$udomain:$uname:$namespace:$items",$uhome);
 2665:    my @pairs=split(/\&/,$rep);
 2666:    my %returnhash=();
 2667:    my $i=0;
 2668:    foreach (@$storearr) {
 2669:       $returnhash{$_}=&thaw_unescape($pairs[$i]);
 2670:       $i++;
 2671:    }
 2672:    return %returnhash;
 2673: }
 2674: 
 2675: # ---------------------------------------------- Custom access rule evaluation
 2676: 
 2677: sub customaccess {
 2678:     my ($priv,$uri)=@_;
 2679:     my ($urole,$urealm)=split(/\./,$env{'request.role'});
 2680:     $urealm=~s/^\W//;
 2681:     my ($udom,$ucrs,$usec)=split(/\//,$urealm);
 2682:     my $access=0;
 2683:     foreach (split(/\s*\,\s*/,&metadata($uri,'rule_rights'))) {
 2684: 	my ($effect,$realm,$role)=split(/\:/,$_);
 2685:         if ($role) {
 2686: 	   if ($role ne $urole) { next; }
 2687:         }
 2688:         foreach (split(/\s*\,\s*/,$realm)) {
 2689:             my ($tdom,$tcrs,$tsec)=split(/\_/,$_);
 2690:             if ($tdom) {
 2691: 		if ($tdom ne $udom) { next; }
 2692:             }
 2693:             if ($tcrs) {
 2694: 		if ($tcrs ne $ucrs) { next; }
 2695:             }
 2696:             if ($tsec) {
 2697: 		if ($tsec ne $usec) { next; }
 2698:             }
 2699:             $access=($effect eq 'allow');
 2700:             last;
 2701:         }
 2702: 	if ($realm eq '' && $role eq '') {
 2703:             $access=($effect eq 'allow');
 2704: 	}
 2705:     }
 2706:     return $access;
 2707: }
 2708: 
 2709: # ------------------------------------------------- Check for a user privilege
 2710: 
 2711: sub allowed {
 2712:     my ($priv,$uri,$symb)=@_;
 2713:     $uri=&deversion($uri);
 2714:     my $orguri=$uri;
 2715:     $uri=&declutter($uri);
 2716:     
 2717:     
 2718:     
 2719:     if (defined($env{'allowed.'.$priv})) { return $env{'allowed.'.$priv}; }
 2720: # Free bre access to adm and meta resources
 2721:     if (((($uri=~/^adm\//) && ($uri !~ m|/bulletinboard$|)) 
 2722: 	 || ($uri=~/\.meta$/)) && ($priv eq 'bre')) {
 2723: 	return 'F';
 2724:     }
 2725: 
 2726: # Free bre access to user's own portfolio contents
 2727:     my ($space,$domain,$name,$dir)=split('/',$uri);
 2728:     if (($space=~/^(uploaded|ediupload)$/) && ($env{'user.name'} eq $name) && 
 2729: 	($env{'user.domain'} eq $domain) && ('portfolio' eq $dir)) {
 2730:         return 'F';
 2731:     }
 2732: 
 2733: # Free bre to public access
 2734: 
 2735:     if ($priv eq 'bre') {
 2736:         my $copyright=&metadata($uri,'copyright');
 2737: 	if (($copyright eq 'public') && (!$env{'request.course.id'})) { 
 2738:            return 'F'; 
 2739:         }
 2740:         if ($copyright eq 'priv') {
 2741:             $uri=~/([^\/]+)\/([^\/]+)\//;
 2742: 	    unless (($env{'user.name'} eq $2) && ($env{'user.domain'} eq $1)) {
 2743: 		return '';
 2744:             }
 2745:         }
 2746:         if ($copyright eq 'domain') {
 2747:             $uri=~/([^\/]+)\/([^\/]+)\//;
 2748: 	    unless (($env{'user.domain'} eq $1) ||
 2749:                  ($env{'course.'.$env{'request.course.id'}.'.domain'} eq $1)) {
 2750: 		return '';
 2751:             }
 2752:         }
 2753:         if ($env{'request.role'}=~ /li\.\//) {
 2754:             # Library role, so allow browsing of resources in this domain.
 2755:             return 'F';
 2756:         }
 2757:         if ($copyright eq 'custom') {
 2758: 	    unless (&customaccess($priv,$uri)) { return ''; }
 2759:         }
 2760:     }
 2761:     # Domain coordinator is trying to create a course
 2762:     if (($priv eq 'ccc') && ($env{'request.role'} =~ /^dc\./)) {
 2763:         # uri is the requested domain in this case.
 2764:         # comparison to 'request.role.domain' shows if the user has selected
 2765:         # a role of dc for the domain in question. 
 2766:         return 'F' if ($uri eq $env{'request.role.domain'});
 2767:     }
 2768: 
 2769:     my $thisallowed='';
 2770:     my $statecond=0;
 2771:     my $courseprivid='';
 2772: 
 2773: # Course
 2774: 
 2775:     if ($env{'user.priv.'.$env{'request.role'}.'./'}=~/\Q$priv\E\&([^\:]*)/) {
 2776:        $thisallowed.=$1;
 2777:     }
 2778: 
 2779: # Domain
 2780: 
 2781:     if ($env{'user.priv.'.$env{'request.role'}.'./'.(split(/\//,$uri))[0].'/'}
 2782:        =~/\Q$priv\E\&([^\:]*)/) {
 2783:        $thisallowed.=$1;
 2784:     }
 2785: 
 2786: # Course: uri itself is a course
 2787:     my $courseuri=$uri;
 2788:     $courseuri=~s/\_(\d)/\/$1/;
 2789:     $courseuri=~s/^([^\/])/\/$1/;
 2790: 
 2791:     if ($env{'user.priv.'.$env{'request.role'}.'.'.$courseuri}
 2792:        =~/\Q$priv\E\&([^\:]*)/) {
 2793:        $thisallowed.=$1;
 2794:     }
 2795: 
 2796: # URI is an uploaded document for this course
 2797: # not allowing 'edit' access (editupload) to uploaded course docs
 2798:     if (($priv eq 'bre') && ($uri=~m|^uploaded/|)) {
 2799: 	my $refuri=$env{'httpref.'.$orguri};
 2800: 	if ($refuri) {
 2801: 	    if ($refuri =~ m|^/adm/|) {
 2802: 		$thisallowed='F';
 2803: 	    }
 2804: 	}
 2805:     }
 2806: 
 2807: # Full access at system, domain or course-wide level? Exit.
 2808: 
 2809:     if ($thisallowed=~/F/) {
 2810: 	return 'F';
 2811:     }
 2812: 
 2813: # If this is generating or modifying users, exit with special codes
 2814: 
 2815:     if (':csu:cdc:ccc:cin:cta:cep:ccr:cst:cad:cli:cau:cdg:cca:'=~/\:\Q$priv\E\:/) {
 2816: 	return $thisallowed;
 2817:     }
 2818: #
 2819: # Gathered so far: system, domain and course wide privileges
 2820: #
 2821: # Course: See if uri or referer is an individual resource that is part of 
 2822: # the course
 2823: 
 2824:     if ($env{'request.course.id'}) {
 2825: 
 2826:        $courseprivid=$env{'request.course.id'};
 2827:        if ($env{'request.course.sec'}) {
 2828:           $courseprivid.='/'.$env{'request.course.sec'};
 2829:        }
 2830:        $courseprivid=~s/\_/\//;
 2831:        my $checkreferer=1;
 2832:        my ($match,$cond)=&is_on_map($uri);
 2833:        if ($match) {
 2834:            $statecond=$cond;
 2835:            if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
 2836:                =~/\Q$priv\E\&([^\:]*)/) {
 2837:                $thisallowed.=$1;
 2838:                $checkreferer=0;
 2839:            }
 2840:        }
 2841:        
 2842:        if ($checkreferer) {
 2843: 	  my $refuri=$env{'httpref.'.$orguri};
 2844:             unless ($refuri) {
 2845:                 foreach (keys %env) {
 2846: 		    if ($_=~/^httpref\..*\*/) {
 2847: 			my $pattern=$_;
 2848:                         $pattern=~s/^httpref\.\/res\///;
 2849:                         $pattern=~s/\*/\[\^\/\]\+/g;
 2850:                         $pattern=~s/\//\\\//g;
 2851:                         if ($orguri=~/$pattern/) {
 2852: 			    $refuri=$env{$_};
 2853:                         }
 2854:                     }
 2855:                 }
 2856:             }
 2857: 
 2858:          if ($refuri) { 
 2859: 	  $refuri=&declutter($refuri);
 2860:           my ($match,$cond)=&is_on_map($refuri);
 2861:             if ($match) {
 2862:               my $refstatecond=$cond;
 2863:               if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
 2864:                   =~/\Q$priv\E\&([^\:]*)/) {
 2865:                   $thisallowed.=$1;
 2866:                   $uri=$refuri;
 2867:                   $statecond=$refstatecond;
 2868:               }
 2869:           }
 2870:         }
 2871:        }
 2872:    }
 2873: 
 2874: #
 2875: # Gathered now: all privileges that could apply, and condition number
 2876: # 
 2877: #
 2878: # Full or no access?
 2879: #
 2880: 
 2881:     if ($thisallowed=~/F/) {
 2882: 	return 'F';
 2883:     }
 2884: 
 2885:     unless ($thisallowed) {
 2886:         return '';
 2887:     }
 2888: 
 2889: # Restrictions exist, deal with them
 2890: #
 2891: #   C:according to course preferences
 2892: #   R:according to resource settings
 2893: #   L:unless locked
 2894: #   X:according to user session state
 2895: #
 2896: 
 2897: # Possibly locked functionality, check all courses
 2898: # Locks might take effect only after 10 minutes cache expiration for other
 2899: # courses, and 2 minutes for current course
 2900: 
 2901:     my $envkey;
 2902:     if ($thisallowed=~/L/) {
 2903:         foreach $envkey (keys %env) {
 2904:            if ($envkey=~/^user\.role\.(st|ta)\.([^\.]*)/) {
 2905:                my $courseid=$2;
 2906:                my $roleid=$1.'.'.$2;
 2907:                $courseid=~s/^\///;
 2908:                my $expiretime=600;
 2909:                if ($env{'request.role'} eq $roleid) {
 2910: 		  $expiretime=120;
 2911:                }
 2912: 	       my ($cdom,$cnum,$csec)=split(/\//,$courseid);
 2913:                my $prefix='course.'.$cdom.'_'.$cnum.'.';
 2914:                if ((time-$env{$prefix.'last_cache'})>$expiretime) {
 2915: 		   &coursedescription($courseid);
 2916:                }
 2917:                if (($env{$prefix.'res.'.$uri.'.lock.sections'}=~/\,\Q$csec\E\,/)
 2918:                 || ($env{$prefix.'res.'.$uri.'.lock.sections'} eq 'all')) {
 2919: 		   if ($env{$prefix.'res.'.$uri.'.lock.expire'}>time) {
 2920:                        &log($env{'user.domain'},$env{'user.name'},
 2921:                             $env{'user.home'},
 2922:                             'Locked by res: '.$priv.' for '.$uri.' due to '.
 2923:                             $cdom.'/'.$cnum.'/'.$csec.' expire '.
 2924:                             $env{$prefix.'priv.'.$priv.'.lock.expire'});
 2925: 		       return '';
 2926:                    }
 2927:                }
 2928:                if (($env{$prefix.'priv.'.$priv.'.lock.sections'}=~/\,\Q$csec\E\,/)
 2929:                 || ($env{$prefix.'priv.'.$priv.'.lock.sections'} eq 'all')) {
 2930: 		   if ($env{'priv.'.$priv.'.lock.expire'}>time) {
 2931:                        &log($env{'user.domain'},$env{'user.name'},
 2932:                             $env{'user.home'},
 2933:                             'Locked by priv: '.$priv.' for '.$uri.' due to '.
 2934:                             $cdom.'/'.$cnum.'/'.$csec.' expire '.
 2935:                             $env{$prefix.'priv.'.$priv.'.lock.expire'});
 2936: 		       return '';
 2937:                    }
 2938:                }
 2939: 	   }
 2940:        }
 2941:     }
 2942:    
 2943: #
 2944: # Rest of the restrictions depend on selected course
 2945: #
 2946: 
 2947:     unless ($env{'request.course.id'}) {
 2948:        return '1';
 2949:     }
 2950: 
 2951: #
 2952: # Now user is definitely in a course
 2953: #
 2954: 
 2955: 
 2956: # Course preferences
 2957: 
 2958:    if ($thisallowed=~/C/) {
 2959:        my $rolecode=(split(/\./,$env{'request.role'}))[0];
 2960:        my $unamedom=$env{'user.name'}.':'.$env{'user.domain'};
 2961:        if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.roles.denied'}
 2962: 	   =~/\Q$rolecode\E/) {
 2963:            &log($env{'user.domain'},$env{'user.name'},$env{'user.host'},
 2964:                 'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode.' in '.
 2965:                 $env{'request.course.id'});
 2966:            return '';
 2967:        }
 2968: 
 2969:        if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.users.denied'}
 2970: 	   =~/\Q$unamedom\E/) {
 2971:            &log($env{'user.domain'},$env{'user.name'},$env{'user.host'},
 2972:                 'Denied by user: '.$priv.' for '.$uri.' as '.$unamedom.' in '.
 2973:                 $env{'request.course.id'});
 2974:            return '';
 2975:        }
 2976:    }
 2977: 
 2978: # Resource preferences
 2979: 
 2980:    if ($thisallowed=~/R/) {
 2981:        my $rolecode=(split(/\./,$env{'request.role'}))[0];
 2982:        if (&metadata($uri,'roledeny')=~/\Q$rolecode\E/) {
 2983: 	  &log($env{'user.domain'},$env{'user.name'},$env{'user.host'},
 2984:                     'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode);
 2985:           return '';
 2986:        }
 2987:    }
 2988: 
 2989: # Restricted by state or randomout?
 2990: 
 2991:    if ($thisallowed=~/X/) {
 2992:       if ($env{'acc.randomout'}) {
 2993: 	 if (!$symb) { $symb=&symbread($uri,1); }
 2994:          if (($symb) && ($env{'acc.randomout'}=~/\&\Q$symb\E\&/)) { 
 2995:             return ''; 
 2996:          }
 2997:       }
 2998:       if (&condval($statecond)) {
 2999: 	 return '2';
 3000:       } else {
 3001:          return '';
 3002:       }
 3003:    }
 3004: 
 3005:    return 'F';
 3006: }
 3007: 
 3008: # --------------------------------------------------- Is a resource on the map?
 3009: 
 3010: sub is_on_map {
 3011:     my $uri=&declutter(shift);
 3012:     $uri=~s/\.\d+\.(\w+)$/\.$1/;
 3013:     my @uriparts=split(/\//,$uri);
 3014:     my $filename=$uriparts[$#uriparts];
 3015:     my $pathname=$uri;
 3016:     $pathname=~s|/\Q$filename\E$||;
 3017:     $pathname=~s/^adm\/wrapper\///;    
 3018:     #Trying to find the conditional for the file
 3019:     my $match=($env{'acc.res.'.$env{'request.course.id'}.'.'.$pathname}=~
 3020: 	       /\&\Q$filename\E\:([\d\|]+)\&/);
 3021:     if ($match) {
 3022: 	return (1,$1);
 3023:     } else {
 3024: 	return (0,0);
 3025:     }
 3026: }
 3027: 
 3028: # --------------------------------------------------------- Get symb from alias
 3029: 
 3030: sub get_symb_from_alias {
 3031:     my $symb=shift;
 3032:     my ($map,$resid,$url)=&decode_symb($symb);
 3033: # Already is a symb
 3034:     if ($url) { return $symb; }
 3035: # Must be an alias
 3036:     my $aliassymb='';
 3037:     my %bighash;
 3038:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
 3039:                             &GDBM_READER(),0640)) {
 3040:         my $rid=$bighash{'mapalias_'.$symb};
 3041: 	if ($rid) {
 3042: 	    my ($mapid,$resid)=split(/\./,$rid);
 3043: 	    $aliassymb=&encode_symb($bighash{'map_id_'.$mapid},
 3044: 				    $resid,$bighash{'src_'.$rid});
 3045: 	}
 3046:         untie %bighash;
 3047:     }
 3048:     return $aliassymb;
 3049: }
 3050: 
 3051: # ----------------------------------------------------------------- Define Role
 3052: 
 3053: sub definerole {
 3054:   if (allowed('mcr','/')) {
 3055:     my ($rolename,$sysrole,$domrole,$courole)=@_;
 3056:     foreach (split(':',$sysrole)) {
 3057: 	my ($crole,$cqual)=split(/\&/,$_);
 3058:         if ($pr{'cr:s'}!~/\Q$crole\E/) { return "refused:s:$crole"; }
 3059:         if ($pr{'cr:s'}=~/\Q$crole\E\&/) {
 3060: 	    if ($pr{'cr:s'}!~/\Q$crole\E\&\w*\Q$cqual\E/) { 
 3061:                return "refused:s:$crole&$cqual"; 
 3062:             }
 3063:         }
 3064:     }
 3065:     foreach (split(':',$domrole)) {
 3066: 	my ($crole,$cqual)=split(/\&/,$_);
 3067:         if ($pr{'cr:d'}!~/\Q$crole\E/) { return "refused:d:$crole"; }
 3068:         if ($pr{'cr:d'}=~/\Q$crole\E\&/) {
 3069: 	    if ($pr{'cr:d'}!~/\Q$crole\W\&\w*\Q$cqual\E/) { 
 3070:                return "refused:d:$crole&$cqual"; 
 3071:             }
 3072:         }
 3073:     }
 3074:     foreach (split(':',$courole)) {
 3075: 	my ($crole,$cqual)=split(/\&/,$_);
 3076:         if ($pr{'cr:c'}!~/\Q$crole\E/) { return "refused:c:$crole"; }
 3077:         if ($pr{'cr:c'}=~/\Q$crole\E\&/) {
 3078: 	    if ($pr{'cr:c'}!~/\Q$crole\E\&\w*\Q$cqual\E/) { 
 3079:                return "refused:c:$crole&$cqual"; 
 3080:             }
 3081:         }
 3082:     }
 3083:     my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
 3084:                 "$env{'user.domain'}:$env{'user.name'}:".
 3085: 	        "rolesdef_$rolename=".
 3086:                 escape($sysrole.'_'.$domrole.'_'.$courole);
 3087:     return reply($command,$env{'user.home'});
 3088:   } else {
 3089:     return 'refused';
 3090:   }
 3091: }
 3092: 
 3093: # ---------------- Make a metadata query against the network of library servers
 3094: 
 3095: sub metadata_query {
 3096:     my ($query,$custom,$customshow,$server_array)=@_;
 3097:     my %rhash;
 3098:     my @server_list = (defined($server_array) ? @$server_array
 3099:                                               : keys(%libserv) );
 3100:     for my $server (@server_list) {
 3101: 	unless ($custom or $customshow) {
 3102: 	    my $reply=&reply("querysend:".&escape($query),$server);
 3103: 	    $rhash{$server}=$reply;
 3104: 	}
 3105: 	else {
 3106: 	    my $reply=&reply("querysend:".&escape($query).':'.
 3107: 			     &escape($custom).':'.&escape($customshow),
 3108: 			     $server);
 3109: 	    $rhash{$server}=$reply;
 3110: 	}
 3111:     }
 3112:     return \%rhash;
 3113: }
 3114: 
 3115: # ----------------------------------------- Send log queries and wait for reply
 3116: 
 3117: sub log_query {
 3118:     my ($uname,$udom,$query,%filters)=@_;
 3119:     my $uhome=&homeserver($uname,$udom);
 3120:     if ($uhome eq 'no_host') { return 'error: no_host'; }
 3121:     my $uhost=$hostname{$uhome};
 3122:     my $command=&escape(join(':',map{$_.'='.$filters{$_}} keys %filters));
 3123:     my $queryid=&reply("querysend:".$query.':'.$udom.':'.$uname.':'.$command,
 3124:                        $uhome);
 3125:     unless ($queryid=~/^\Q$uhost\E\_/) { return 'error: '.$queryid; }
 3126:     return get_query_reply($queryid);
 3127: }
 3128: 
 3129: # ------- Request retrieval of institutional classlists for course(s)
 3130: 
 3131: sub fetch_enrollment_query {
 3132:     my ($context,$affiliatesref,$replyref,$dom,$cnum) = @_;
 3133:     my $homeserver;
 3134:     my $maxtries = 1;
 3135:     if ($context eq 'automated') {
 3136:         $homeserver = $perlvar{'lonHostID'};
 3137:         $maxtries = 10; # will wait for up to 2000s for retrieval of classlist data before timeout
 3138:     } else {
 3139:         $homeserver = &homeserver($cnum,$dom);
 3140:     }
 3141:     my $host=$hostname{$homeserver};
 3142:     my $cmd = '';
 3143:     foreach (keys %{$affiliatesref}) {
 3144:         $cmd .= $_.'='.join(",",@{$$affiliatesref{$_}}).'%%';
 3145:     }
 3146:     $cmd =~ s/%%$//;
 3147:     $cmd = &escape($cmd);
 3148:     my $query = 'fetchenrollment';
 3149:     my $queryid=&reply("querysend:".$query.':'.$dom.':'.$env{'user.name'}.':'.$cmd,$homeserver);
 3150:     unless ($queryid=~/^\Q$host\E\_/) { 
 3151:         &logthis('fetch_enrollment_query: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' context: '.$context.' '.$cnum); 
 3152:         return 'error: '.$queryid;
 3153:     }
 3154:     my $reply = &get_query_reply($queryid);
 3155:     my $tries = 1;
 3156:     while (($reply=~/^timeout/) && ($tries < $maxtries)) {
 3157:         $reply = &get_query_reply($queryid);
 3158:         $tries ++;
 3159:     }
 3160:     if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
 3161:         &logthis('fetch_enrollment_query error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' context: '.$context.' '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
 3162:     } else {
 3163:         my @responses = split/:/,$reply;
 3164:         if ($homeserver eq $perlvar{'lonHostID'}) {
 3165:             foreach (@responses) {
 3166:                 my ($key,$value) = split/=/,$_;
 3167:                 $$replyref{$key} = $value;
 3168:             }
 3169:         } else {
 3170:             my $pathname = $perlvar{'lonDaemons'}.'/tmp';
 3171:             foreach (@responses) {
 3172:                 my ($key,$value) = split/=/,$_;
 3173:                 $$replyref{$key} = $value;
 3174:                 if ($value > 0) {
 3175:                     foreach (@{$$affiliatesref{$key}}) {
 3176:                         my $filename = $dom.'_'.$key.'_'.$_.'_classlist.xml';
 3177:                         my $destname = $pathname.'/'.$filename;
 3178:                         my $xml_classlist = &reply("autoretrieve:".$filename,$homeserver);
 3179:                         if ($xml_classlist =~ /^error/) {
 3180:                             &logthis('fetch_enrollment_query - autoretrieve error: '.$xml_classlist.' for '.$filename.' from server: '.$homeserver.' '.$context.' '.$cnum);
 3181:                         } else {
 3182:                             if ( open(FILE,">$destname") ) {
 3183:                                 print FILE &unescape($xml_classlist);
 3184:                                 close(FILE);
 3185:                             } else {
 3186:                                 &logthis('fetch_enrollment_query - error opening classlist file '.$destname.' '.$context.' '.$cnum);
 3187:                             }
 3188:                         }
 3189:                     }
 3190:                 }
 3191:             }
 3192:         }
 3193:         return 'ok';
 3194:     }
 3195:     return 'error';
 3196: }
 3197: 
 3198: sub get_query_reply {
 3199:     my $queryid=shift;
 3200:     my $replyfile=$perlvar{'lonDaemons'}.'/tmp/'.$queryid;
 3201:     my $reply='';
 3202:     for (1..100) {
 3203: 	sleep 2;
 3204:         if (-e $replyfile.'.end') {
 3205: 	    if (open(my $fh,$replyfile)) {
 3206:                $reply.=<$fh>;
 3207:                close($fh);
 3208: 	   } else { return 'error: reply_file_error'; }
 3209:            return &unescape($reply);
 3210: 	}
 3211:     }
 3212:     return 'timeout:'.$queryid;
 3213: }
 3214: 
 3215: sub courselog_query {
 3216: #
 3217: # possible filters:
 3218: # url: url or symb
 3219: # username
 3220: # domain
 3221: # action: view, submit, grade
 3222: # start: timestamp
 3223: # end: timestamp
 3224: #
 3225:     my (%filters)=@_;
 3226:     unless ($env{'request.course.id'}) { return 'no_course'; }
 3227:     if ($filters{'url'}) {
 3228: 	$filters{'url'}=&symbclean(&declutter($filters{'url'}));
 3229:         $filters{'url'}=~s/\.(\w+)$/(\\.\\d+)*\\.$1/;
 3230:         $filters{'url'}=~s/\.(\w+)\_\_\_/(\\.\\d+)*\\.$1/;
 3231:     }
 3232:     my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
 3233:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 3234:     return &log_query($cname,$cdom,'courselog',%filters);
 3235: }
 3236: 
 3237: sub userlog_query {
 3238:     my ($uname,$udom,%filters)=@_;
 3239:     return &log_query($uname,$udom,'userlog',%filters);
 3240: }
 3241: 
 3242: #--------- Call auto-enrollment subs in localenroll.pm for homeserver for course 
 3243: 
 3244: sub auto_run {
 3245:     my ($cnum,$cdom) = @_;
 3246:     my $homeserver = &homeserver($cnum,$cdom);
 3247:     my $response = &reply('autorun:'.$cdom,$homeserver);
 3248:     return $response;
 3249: }
 3250:                                                                                    
 3251: sub auto_get_sections {
 3252:     my ($cnum,$cdom,$inst_coursecode) = @_;
 3253:     my $homeserver = &homeserver($cnum,$cdom);
 3254:     my @secs = ();
 3255:     my $response=&unescape(&reply('autogetsections:'.$inst_coursecode.':'.$cdom,$homeserver));
 3256:     unless ($response eq 'refused') {
 3257:         @secs = split/:/,$response;
 3258:     }
 3259:     return @secs;
 3260: }
 3261:                                                                                    
 3262: sub auto_new_course {
 3263:     my ($cnum,$cdom,$inst_course_id,$owner) = @_;
 3264:     my $homeserver = &homeserver($cnum,$cdom);
 3265:     my $response=&unescape(&reply('autonewcourse:'.$inst_course_id.':'.$owner.':'.$cdom,$homeserver));
 3266:     return $response;
 3267: }
 3268:                                                                                    
 3269: sub auto_validate_courseID {
 3270:     my ($cnum,$cdom,$inst_course_id) = @_;
 3271:     my $homeserver = &homeserver($cnum,$cdom);
 3272:     my $response=&unescape(&reply('autovalidatecourse:'.$inst_course_id.':'.$cdom,$homeserver));
 3273:     return $response;
 3274: }
 3275:                                                                                    
 3276: sub auto_create_password {
 3277:     my ($cnum,$cdom,$authparam) = @_;
 3278:     my $homeserver = &homeserver($cnum,$cdom); 
 3279:     my $create_passwd = 0;
 3280:     my $authchk = '';
 3281:     my $response=&unescape(&reply('autocreatepassword:'.$authparam.':'.$cdom,$homeserver));
 3282:     if ($response eq 'refused') {
 3283:         $authchk = 'refused';
 3284:     } else {
 3285:         ($authparam,$create_passwd,$authchk) = split/:/,$response;
 3286:     }
 3287:     return ($authparam,$create_passwd,$authchk);
 3288: }
 3289: 
 3290: sub auto_instcode_format {
 3291:     my ($caller,$codedom,$instcodes,$codes,$codetitles,$cat_titles,$cat_order) = @_;
 3292:     my $courses = '';
 3293:     my $homeserver;
 3294:     if ($caller eq 'global') {
 3295:         foreach my $tryserver (keys %libserv) {
 3296:             if ($hostdom{$tryserver} eq $codedom) {
 3297:                 $homeserver = $tryserver;
 3298:                 last;
 3299:             }
 3300:         }
 3301:         if (($env{'user.name'}) && ($env{'user.domain'} eq $codedom)) {
 3302:             $homeserver = &homeserver($env{'user.name'},$codedom);
 3303:         }
 3304:     } else {
 3305:         $homeserver = &homeserver($caller,$codedom);
 3306:     }
 3307:     foreach (keys %{$instcodes}) {
 3308:         $courses .= &escape($_).'='.&escape($$instcodes{$_}).'&';
 3309:     }
 3310:     chop($courses);
 3311:     my $response=&reply('autoinstcodeformat:'.$codedom.':'.$courses,$homeserver);
 3312:     unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
 3313:         my ($codes_str,$codetitles_str,$cat_titles_str,$cat_order_str) = split/:/,$response;
 3314:         %{$codes} = &str2hash($codes_str);
 3315:         @{$codetitles} = &str2array($codetitles_str);
 3316:         %{$cat_titles} = &str2hash($cat_titles_str);
 3317:         %{$cat_order} = &str2hash($cat_order_str);
 3318:         return 'ok';
 3319:     }
 3320:     return $response;
 3321: }
 3322: 
 3323: # ------------------------------------------------------------------ Plain Text
 3324: 
 3325: sub plaintext {
 3326:     my $short=shift;
 3327:     return &mt($prp{$short});
 3328: }
 3329: 
 3330: # ----------------------------------------------------------------- Assign Role
 3331: 
 3332: sub assignrole {
 3333:     my ($udom,$uname,$url,$role,$end,$start,$deleteflag)=@_;
 3334:     my $mrole;
 3335:     if ($role =~ /^cr\//) {
 3336:         my $cwosec=$url;
 3337:         $cwosec=~s/^\/(\w+)\/(\w+)\/.*/$1\/$2/;
 3338: 	unless (&allowed('ccr',$cwosec)) {
 3339:            &logthis('Refused custom assignrole: '.
 3340:              $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
 3341: 		    $env{'user.name'}.' at '.$env{'user.domain'});
 3342:            return 'refused'; 
 3343:         }
 3344:         $mrole='cr';
 3345:     } else {
 3346:         my $cwosec=$url;
 3347:         $cwosec=~s/^\/(\w+)\/(\w+)\/.*/$1\/$2/;
 3348:         unless ((&allowed('c'.$role,$cwosec)) || &allowed('c'.$role,$udom)) { 
 3349:            &logthis('Refused assignrole: '.
 3350:              $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
 3351: 		    $env{'user.name'}.' at '.$env{'user.domain'});
 3352:            return 'refused'; 
 3353:         }
 3354:         $mrole=$role;
 3355:     }
 3356:     my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
 3357:                 "$udom:$uname:$url".'_'."$mrole=$role";
 3358:     if ($end) { $command.='_'.$end; }
 3359:     if ($start) {
 3360: 	if ($end) { 
 3361:            $command.='_'.$start; 
 3362:         } else {
 3363:            $command.='_0_'.$start;
 3364:         }
 3365:     }
 3366: # actually delete
 3367:     if ($deleteflag) {
 3368: 	if ((&allowed('dro',$udom)) || (&allowed('dro',$url))) {
 3369: # modify command to delete the role
 3370:            $command="encrypt:rolesdel:$env{'user.domain'}:$env{'user.name'}:".
 3371:                 "$udom:$uname:$url".'_'."$mrole";
 3372: 	   &logthis("$env{'user.name'} at $env{'user.domain'} deletes $mrole in $url for $uname at $udom"); 
 3373: # set start and finish to negative values for userrolelog
 3374:            $start=-1;
 3375:            $end=-1;
 3376:         }
 3377:     }
 3378: # send command
 3379:     my $answer=&reply($command,&homeserver($uname,$udom));
 3380: # log new user role if status is ok
 3381:     if ($answer eq 'ok') {
 3382: 	&userrolelog($mrole,$uname,$udom,$url,$start,$end);
 3383:     }
 3384:     return $answer;
 3385: }
 3386: 
 3387: # -------------------------------------------------- Modify user authentication
 3388: # Overrides without validation
 3389: 
 3390: sub modifyuserauth {
 3391:     my ($udom,$uname,$umode,$upass)=@_;
 3392:     my $uhome=&homeserver($uname,$udom);
 3393:     unless (&allowed('mau',$udom)) { return 'refused'; }
 3394:     &logthis('Call to modify user authentication '.$udom.', '.$uname.', '.
 3395:              $umode.' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
 3396:              ' in domain '.$env{'request.role.domain'});  
 3397:     my $reply=&reply('encrypt:changeuserauth:'.$udom.':'.$uname.':'.$umode.':'.
 3398: 		     &escape($upass),$uhome);
 3399:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
 3400:         'Authentication changed for '.$udom.', '.$uname.', '.$umode.
 3401:          '(Remote '.$ENV{'REMOTE_ADDR'}.'): '.$reply);
 3402:     &log($udom,,$uname,$uhome,
 3403:         'Authentication changed by '.$env{'user.domain'}.', '.
 3404:                                      $env{'user.name'}.', '.$umode.
 3405:          '(Remote '.$ENV{'REMOTE_ADDR'}.'): '.$reply);
 3406:     unless ($reply eq 'ok') {
 3407:         &logthis('Authentication mode error: '.$reply);
 3408: 	return 'error: '.$reply;
 3409:     }   
 3410:     return 'ok';
 3411: }
 3412: 
 3413: # --------------------------------------------------------------- Modify a user
 3414: 
 3415: sub modifyuser {
 3416:     my ($udom,    $uname, $uid,
 3417:         $umode,   $upass, $first,
 3418:         $middle,  $last,  $gene,
 3419:         $forceid, $desiredhome, $email)=@_;
 3420:     $udom=~s/\W//g;
 3421:     $uname=~s/\W//g;
 3422:     &logthis('Call to modify user '.$udom.', '.$uname.', '.$uid.', '.
 3423:              $umode.', '.$first.', '.$middle.', '.
 3424: 	     $last.', '.$gene.'(forceid: '.$forceid.')'.
 3425:              (defined($desiredhome) ? ' desiredhome = '.$desiredhome :
 3426:                                      ' desiredhome not specified'). 
 3427:              ' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
 3428:              ' in domain '.$env{'request.role.domain'});
 3429:     my $uhome=&homeserver($uname,$udom,'true');
 3430: # ----------------------------------------------------------------- Create User
 3431:     if (($uhome eq 'no_host') && 
 3432: 	(($umode && $upass) || ($umode eq 'localauth'))) {
 3433:         my $unhome='';
 3434:         if (defined($desiredhome) && $hostdom{$desiredhome} eq $udom) { 
 3435:             $unhome = $desiredhome;
 3436: 	} elsif($env{'course.'.$env{'request.course.id'}.'.domain'} eq $udom) {
 3437: 	    $unhome=$env{'course.'.$env{'request.course.id'}.'.home'};
 3438:         } else { # load balancing routine for determining $unhome
 3439:             my $tryserver;
 3440:             my $loadm=10000000;
 3441:             foreach $tryserver (keys %libserv) {
 3442: 	       if ($hostdom{$tryserver} eq $udom) {
 3443:                   my $answer=reply('load',$tryserver);
 3444:                   if (($answer=~/\d+/) && ($answer<$loadm)) {
 3445: 		      $loadm=$answer;
 3446:                       $unhome=$tryserver;
 3447:                   }
 3448: 	       }
 3449: 	    }
 3450:         }
 3451:         if (($unhome eq '') || ($unhome eq 'no_host')) {
 3452: 	    return 'error: unable to find a home server for '.$uname.
 3453:                    ' in domain '.$udom;
 3454:         }
 3455:         my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':'.$umode.':'.
 3456:                          &escape($upass),$unhome);
 3457: 	unless ($reply eq 'ok') {
 3458:             return 'error: '.$reply;
 3459:         }   
 3460:         $uhome=&homeserver($uname,$udom,'true');
 3461:         if (($uhome eq '') || ($uhome eq 'no_host') || ($uhome ne $unhome)) {
 3462: 	    return 'error: unable verify users home machine.';
 3463:         }
 3464:     }   # End of creation of new user
 3465: # ---------------------------------------------------------------------- Add ID
 3466:     if ($uid) {
 3467:        $uid=~tr/A-Z/a-z/;
 3468:        my %uidhash=&idrget($udom,$uname);
 3469:        if (($uidhash{$uname}) && ($uidhash{$uname}!~/error\:/) 
 3470:          && (!$forceid)) {
 3471: 	  unless ($uid eq $uidhash{$uname}) {
 3472: 	      return 'error: user id "'.$uid.'" does not match '.
 3473:                   'current user id "'.$uidhash{$uname}.'".';
 3474:           }
 3475:        } else {
 3476: 	  &idput($udom,($uname => $uid));
 3477:        }
 3478:     }
 3479: # -------------------------------------------------------------- Add names, etc
 3480:     my @tmp=&get('environment',
 3481: 		   ['firstname','middlename','lastname','generation'],
 3482: 		   $udom,$uname);
 3483:     my %names;
 3484:     if ($tmp[0] =~ m/^error:.*/) { 
 3485:         %names=(); 
 3486:     } else {
 3487:         %names = @tmp;
 3488:     }
 3489: #
 3490: # Make sure to not trash student environment if instructor does not bother
 3491: # to supply name and email information
 3492: #
 3493:     if ($first)  { $names{'firstname'}  = $first; }
 3494:     if (defined($middle)) { $names{'middlename'} = $middle; }
 3495:     if ($last)   { $names{'lastname'}   = $last; }
 3496:     if (defined($gene))   { $names{'generation'} = $gene; }
 3497:     if ($email) {
 3498:        $email=~s/[^\w\@\.\-\,]//gs;
 3499:        if ($email=~/\@/) { $names{'notification'} = $email;
 3500: 			   $names{'critnotification'} = $email;
 3501: 			   $names{'permanentemail'} = $email; }
 3502:     }
 3503:     my $reply = &put('environment', \%names, $udom,$uname);
 3504:     if ($reply ne 'ok') { return 'error: '.$reply; }
 3505:     &logthis('Success modifying user '.$udom.', '.$uname.', '.$uid.', '.
 3506:              $umode.', '.$first.', '.$middle.', '.
 3507: 	     $last.', '.$gene.' by '.
 3508:              $env{'user.name'}.' at '.$env{'user.domain'});
 3509:     return 'ok';
 3510: }
 3511: 
 3512: # -------------------------------------------------------------- Modify student
 3513: 
 3514: sub modifystudent {
 3515:     my ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$usec,
 3516:         $end,$start,$forceid,$desiredhome,$email,$type,$locktype,$cid)=@_;
 3517:     if (!$cid) {
 3518: 	unless ($cid=$env{'request.course.id'}) {
 3519: 	    return 'not_in_class';
 3520: 	}
 3521:     }
 3522: # --------------------------------------------------------------- Make the user
 3523:     my $reply=&modifyuser
 3524: 	($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$forceid,
 3525:          $desiredhome,$email);
 3526:     unless ($reply eq 'ok') { return $reply; }
 3527:     # This will cause &modify_student_enrollment to get the uid from the
 3528:     # students environment
 3529:     $uid = undef if (!$forceid);
 3530:     $reply = &modify_student_enrollment($udom,$uname,$uid,$first,$middle,$last,
 3531: 					$gene,$usec,$end,$start,$type,$locktype,$cid);
 3532:     return $reply;
 3533: }
 3534: 
 3535: sub modify_student_enrollment {
 3536:     my ($udom,$uname,$uid,$first,$middle,$last,$gene,$usec,$end,$start,$type,$locktype,$cid) = @_;
 3537:     my ($cdom,$cnum,$chome);
 3538:     if (!$cid) {
 3539: 	unless ($cid=$env{'request.course.id'}) {
 3540: 	    return 'not_in_class';
 3541: 	}
 3542: 	$cdom=$env{'course.'.$cid.'.domain'};
 3543: 	$cnum=$env{'course.'.$cid.'.num'};
 3544:     } else {
 3545: 	($cdom,$cnum)=split(/_/,$cid);
 3546:     }
 3547:     $chome=$env{'course.'.$cid.'.home'};
 3548:     if (!$chome) {
 3549: 	$chome=&homeserver($cnum,$cdom);
 3550:     }
 3551:     if (!$chome) { return 'unknown_course'; }
 3552:     # Make sure the user exists
 3553:     my $uhome=&homeserver($uname,$udom);
 3554:     if (($uhome eq '') || ($uhome eq 'no_host')) { 
 3555: 	return 'error: no such user';
 3556:     }
 3557:     # Get student data if we were not given enough information
 3558:     if (!defined($first)  || $first  eq '' || 
 3559:         !defined($last)   || $last   eq '' || 
 3560:         !defined($uid)    || $uid    eq '' || 
 3561:         !defined($middle) || $middle eq '' || 
 3562:         !defined($gene)   || $gene   eq '') {
 3563:         # They did not supply us with enough data to enroll the student, so
 3564:         # we need to pick up more information.
 3565:         my %tmp = &get('environment',
 3566:                        ['firstname','middlename','lastname', 'generation','id']
 3567:                        ,$udom,$uname);
 3568: 
 3569:         #foreach (keys(%tmp)) {
 3570:         #    &logthis("key $_ = ".$tmp{$_});
 3571:         #}
 3572:         $first  = $tmp{'firstname'}  if (!defined($first)  || $first  eq '');
 3573:         $middle = $tmp{'middlename'} if (!defined($middle) || $middle eq '');
 3574:         $last   = $tmp{'lastname'}   if (!defined($last)   || $last eq '');
 3575:         $gene   = $tmp{'generation'} if (!defined($gene)   || $gene eq '');
 3576:         $uid    = $tmp{'id'}         if (!defined($uid)    || $uid  eq '');
 3577:     }
 3578:     my $fullname = &format_name($first,$middle,$last,$gene,'lastname');
 3579:     my $reply=cput('classlist',
 3580: 		   {"$uname:$udom" => 
 3581: 			join(':',$end,$start,$uid,$usec,$fullname,$type,$locktype) },
 3582: 		   $cdom,$cnum);
 3583:     unless (($reply eq 'ok') || ($reply eq 'delayed')) {
 3584: 	return 'error: '.$reply;
 3585:     }
 3586:     # Add student role to user
 3587:     my $uurl='/'.$cid;
 3588:     $uurl=~s/\_/\//g;
 3589:     if ($usec) {
 3590: 	$uurl.='/'.$usec;
 3591:     }
 3592:     return &assignrole($udom,$uname,$uurl,'st',$end,$start);
 3593: }
 3594: 
 3595: sub format_name {
 3596:     my ($firstname,$middlename,$lastname,$generation,$first)=@_;
 3597:     my $name;
 3598:     if ($first ne 'lastname') {
 3599: 	$name=$firstname.' '.$middlename.' '.$lastname.' '.$generation;
 3600:     } else {
 3601: 	if ($lastname=~/\S/) {
 3602: 	    $name.= $lastname.' '.$generation.', '.$firstname.' '.$middlename;
 3603: 	    $name=~s/\s+,/,/;
 3604: 	} else {
 3605: 	    $name.= $firstname.' '.$middlename.' '.$generation;
 3606: 	}
 3607:     }
 3608:     $name=~s/^\s+//;
 3609:     $name=~s/\s+$//;
 3610:     $name=~s/\s+/ /g;
 3611:     return $name;
 3612: }
 3613: 
 3614: # ------------------------------------------------- Write to course preferences
 3615: 
 3616: sub writecoursepref {
 3617:     my ($courseid,%prefs)=@_;
 3618:     $courseid=~s/^\///;
 3619:     $courseid=~s/\_/\//g;
 3620:     my ($cdomain,$cnum)=split(/\//,$courseid);
 3621:     my $chome=homeserver($cnum,$cdomain);
 3622:     if (($chome eq '') || ($chome eq 'no_host')) { 
 3623: 	return 'error: no such course';
 3624:     }
 3625:     my $cstring='';
 3626:     foreach (keys %prefs) {
 3627: 	$cstring.=escape($_).'='.escape($prefs{$_}).'&';
 3628:     }
 3629:     $cstring=~s/\&$//;
 3630:     return reply('put:'.$cdomain.':'.$cnum.':environment:'.$cstring,$chome);
 3631: }
 3632: 
 3633: # ---------------------------------------------------------- Make/modify course
 3634: 
 3635: sub createcourse {
 3636:     my ($udom,$description,$url,$course_server,$nonstandard,$inst_code,$course_owner)=@_;
 3637:     $url=&declutter($url);
 3638:     my $cid='';
 3639:     unless (&allowed('ccc',$udom)) {
 3640:         return 'refused';
 3641:     }
 3642: # ------------------------------------------------------------------- Create ID
 3643:    my $uname=substr($$.time,0,5).unpack("H8",pack("I32",time)).
 3644:        unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
 3645: # ----------------------------------------------- Make sure that does not exist
 3646:    my $uhome=&homeserver($uname,$udom,'true');
 3647:    unless (($uhome eq '') || ($uhome eq 'no_host')) {
 3648:        $uname=substr($$.time,0,5).unpack("H8",pack("I32",time)).
 3649:         unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
 3650:        $uhome=&homeserver($uname,$udom,'true');       
 3651:        unless (($uhome eq '') || ($uhome eq 'no_host')) {
 3652:            return 'error: unable to generate unique course-ID';
 3653:        } 
 3654:    }
 3655: # ------------------------------------------------ Check supplied server name
 3656:     $course_server = $env{'user.homeserver'} if (! defined($course_server));
 3657:     if (! exists($libserv{$course_server})) {
 3658:         return 'error:bad server name '.$course_server;
 3659:     }
 3660: # ------------------------------------------------------------- Make the course
 3661:     my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':none::',
 3662:                       $course_server);
 3663:     unless ($reply eq 'ok') { return 'error: '.$reply; }
 3664:     $uhome=&homeserver($uname,$udom,'true');
 3665:     if (($uhome eq '') || ($uhome eq 'no_host')) { 
 3666: 	return 'error: no such course';
 3667:     }
 3668: # ----------------------------------------------------------------- Course made
 3669: # log existence
 3670:     &courseidput($udom,&escape($udom.'_'.$uname).'='.&escape($description).
 3671:                  ':'.&escape($inst_code).':'.&escape($course_owner),$uhome);
 3672:     &flushcourselogs();
 3673: # set toplevel url
 3674:     my $topurl=$url;
 3675:     unless ($nonstandard) {
 3676: # ------------------------------------------ For standard courses, make top url
 3677:         my $mapurl=&clutter($url);
 3678:         if ($mapurl eq '/res/') { $mapurl=''; }
 3679:         $env{'form.initmap'}=(<<ENDINITMAP);
 3680: <map>
 3681: <resource id="1" type="start"></resource>
 3682: <resource id="2" src="$mapurl"></resource>
 3683: <resource id="3" type="finish"></resource>
 3684: <link index="1" from="1" to="2"></link>
 3685: <link index="2" from="2" to="3"></link>
 3686: </map>
 3687: ENDINITMAP
 3688:         $topurl=&declutter(
 3689:         &finishuserfileupload($uname,$udom,$uhome,'initmap','default.sequence')
 3690:                           );
 3691:     }
 3692: # ----------------------------------------------------------- Write preferences
 3693:     &writecoursepref($udom.'_'.$uname,
 3694:                      ('description' => $description,
 3695:                       'url'         => $topurl));
 3696:     return '/'.$udom.'/'.$uname;
 3697: }
 3698: 
 3699: # ---------------------------------------------------------- Assign Custom Role
 3700: 
 3701: sub assigncustomrole {
 3702:     my ($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start,$deleteflag)=@_;
 3703:     return &assignrole($udom,$uname,$url,'cr/'.$rdom.'/'.$rnam.'/'.$rolename,
 3704:                        $end,$start,$deleteflag);
 3705: }
 3706: 
 3707: # ----------------------------------------------------------------- Revoke Role
 3708: 
 3709: sub revokerole {
 3710:     my ($udom,$uname,$url,$role,$deleteflag)=@_;
 3711:     my $now=time;
 3712:     return &assignrole($udom,$uname,$url,$role,$now,$deleteflag);
 3713: }
 3714: 
 3715: # ---------------------------------------------------------- Revoke Custom Role
 3716: 
 3717: sub revokecustomrole {
 3718:     my ($udom,$uname,$url,$rdom,$rnam,$rolename,$deleteflag)=@_;
 3719:     my $now=time;
 3720:     return &assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$now,
 3721:            $deleteflag);
 3722: }
 3723: 
 3724: # ------------------------------------------------------------ Disk usage
 3725: sub diskusage {
 3726:     my ($udom,$uname,$directoryRoot)=@_;
 3727:     $directoryRoot =~ s/\/$//;
 3728:     my $listing=&reply('du:'.$directoryRoot,homeserver($uname,$udom));
 3729:     return $listing;
 3730: }
 3731: 
 3732: sub is_locked {
 3733:     my ($file_name, $domain, $user) = @_;
 3734:     my @check;
 3735:     my $is_locked;
 3736:     push @check, $file_name;
 3737:     my %locked = &get('file_permissions',\@check,
 3738: 		      $env{'user.domain'},$env{'user.name'});
 3739:     my ($tmp)=keys(%locked);
 3740:     if ($tmp=~/^error:/) { undef(%locked); }
 3741: 
 3742:     if (ref($locked{$file_name}) eq 'ARRAY') {
 3743:         $is_locked = 'true';
 3744:     } else {
 3745:         $is_locked = 'false';
 3746:     }
 3747: }
 3748: 
 3749: # ------------------------------------------------------------- Mark as Read Only
 3750: 
 3751: sub mark_as_readonly {
 3752:     my ($domain,$user,$files,$what) = @_;
 3753:     my %current_permissions = &dump('file_permissions',$domain,$user);
 3754:     my ($tmp)=keys(%current_permissions);
 3755:     if ($tmp=~/^error:/) { undef(%current_permissions); }
 3756: 
 3757:     foreach my $file (@{$files}) {
 3758:         push(@{$current_permissions{$file}},$what);
 3759:     }
 3760:     &put('file_permissions',\%current_permissions,$domain,$user);
 3761:     return;
 3762: }
 3763: 
 3764: # ------------------------------------------------------------Save Selected Files
 3765: 
 3766: sub save_selected_files {
 3767:     my ($user, $path, @files) = @_;
 3768:     my $filename = $user."savedfiles";
 3769:     my @other_files = &files_not_in_path($user, $path);
 3770:     open (OUT, '>'.$Apache::lonnet::perlvar{'lonDaemons'}.'/tmp/'.$filename);
 3771:     foreach my $file (@files) {
 3772:         print (OUT $env{'form.currentpath'}.$file."\n");
 3773:     }
 3774:     foreach my $file (@other_files) {
 3775:         print (OUT $file."\n");
 3776:     }
 3777:     close (OUT);
 3778:     return 'ok';
 3779: }
 3780: 
 3781: sub clear_selected_files {
 3782:     my ($user) = @_;
 3783:     my $filename = $user."savedfiles";
 3784:     open (OUT, '>'.$Apache::lonnet::perlvar{'lonDaemons'}.'/tmp/'.$filename);
 3785:     print (OUT undef);
 3786:     close (OUT);
 3787:     return ("ok");    
 3788: }
 3789: 
 3790: sub files_in_path {
 3791:     my ($user, $path) = @_;
 3792:     my $filename = $user."savedfiles";
 3793:     my %return_files;
 3794:     open (IN, '<'.$Apache::lonnet::perlvar{'lonDaemons'}.'/tmp/'.$filename);
 3795:     while (my $line_in = <IN>) {
 3796:         chomp ($line_in);
 3797:         my @paths_and_file = split (m!/!, $line_in);
 3798:         my $file_part = pop (@paths_and_file);
 3799:         my $path_part = join ('/', @paths_and_file);
 3800:         $path_part.='/';
 3801:         my $path_and_file = $path_part.$file_part;
 3802:         if ($path_part eq $path) {
 3803:             $return_files{$file_part}= 'selected';
 3804:         }
 3805:     }
 3806:     close (IN);
 3807:     return (\%return_files);
 3808: }
 3809: 
 3810: # called in portfolio select mode, to show files selected NOT in current directory
 3811: sub files_not_in_path {
 3812:     my ($user, $path) = @_;
 3813:     my $filename = $user."savedfiles";
 3814:     my @return_files;
 3815:     my $path_part;
 3816:     open (IN, '<'.$Apache::lonnet::perlvar{'lonDaemons'}.'/tmp/'.$filename);
 3817:     while (<IN>) {
 3818:         #ok, I know it's clunky, but I want it to work
 3819:         my @paths_and_file = split m!/!, $_;
 3820:         my $file_part = pop (@paths_and_file);
 3821:         chomp ($file_part);
 3822:         my $path_part = join ('/', @paths_and_file);
 3823:         $path_part .= '/';
 3824:         my $path_and_file = $path_part.$file_part;
 3825:         if ($path_part ne $path) {
 3826:             push (@return_files, ($path_and_file));
 3827:         }
 3828:     }
 3829:     close (OUT);
 3830:     return (@return_files);
 3831: }
 3832: 
 3833: #--------------------------------------------------------------Get Marked as Read Only
 3834: 
 3835: sub get_marked_as_readonly {
 3836:     my ($domain,$user,$what) = @_;
 3837:     my %current_permissions = &dump('file_permissions',$domain,$user);
 3838:     my ($tmp)=keys(%current_permissions);
 3839:     if ($tmp=~/^error:/) { undef(%current_permissions); }
 3840: 
 3841:     my @readonly_files;
 3842:     while (my ($file_name,$value) = each(%current_permissions)) {
 3843:         if (ref($value) eq "ARRAY"){
 3844:             foreach my $stored_what (@{$value}) {
 3845:                 if ($stored_what eq $what) {
 3846:                     push(@readonly_files, $file_name);
 3847:                 } elsif (!defined($what)) {
 3848:                     push(@readonly_files, $file_name);
 3849:                 }
 3850:             }
 3851:         } 
 3852:     }
 3853:     return @readonly_files;
 3854: }
 3855: #-----------------------------------------------------------Get Marked as Read Only Hash
 3856: 
 3857: sub get_marked_as_readonly_hash {
 3858:     my ($domain,$user,$what) = @_;
 3859:     my %current_permissions = &dump('file_permissions',$domain,$user);
 3860:     my ($tmp)=keys(%current_permissions);
 3861:     if ($tmp=~/^error:/) { undef(%current_permissions); }
 3862: 
 3863:     my %readonly_files;
 3864:     while (my ($file_name,$value) = each(%current_permissions)) {
 3865:         if (ref($value) eq "ARRAY"){
 3866:             foreach my $stored_what (@{$value}) {
 3867:                 if ($stored_what eq $what) {
 3868:                     $readonly_files{$file_name} = 'locked';
 3869:                 } elsif (!defined($what)) {
 3870:                     $readonly_files{$file_name} = 'locked';
 3871:                 }
 3872:             }
 3873:         } 
 3874:     }
 3875:     return %readonly_files;
 3876: }
 3877: # ------------------------------------------------------------ Unmark as Read Only
 3878: 
 3879: sub unmark_as_readonly {
 3880:     # unmarks all files locked by $what 
 3881:     # for portfolio submissions, $what contains $crsid and $symb
 3882:     my ($domain,$user,$what) = @_;
 3883:     my %current_permissions = &dump('file_permissions',$domain,$user);
 3884:     my ($tmp)=keys(%current_permissions);
 3885:     if ($tmp=~/^error:/) { undef(%current_permissions); }
 3886: 
 3887:     my @readonly_files = &get_marked_as_readonly($domain,$user,$what);
 3888:     foreach my $file(@readonly_files){
 3889:         my $current_locks = $current_permissions{$file};
 3890:         my @new_locks;
 3891:         my @del_keys;
 3892:         if (ref($current_locks) eq "ARRAY"){
 3893:             foreach my $locker (@{$current_locks}) {
 3894:                 unless ($locker eq $what) {
 3895:                     push(@new_locks, $what);
 3896:                 }
 3897:             }
 3898:             if (@new_locks > 0) {
 3899:                 $current_permissions{$file} = \@new_locks;
 3900:             } else {
 3901:                 push(@del_keys, $file);
 3902:                 &del('file_permissions',\@del_keys, $domain, $user);
 3903:                 delete $current_permissions{$file};
 3904:             }
 3905:         }
 3906:     }
 3907:     &put('file_permissions',\%current_permissions,$domain,$user);
 3908:     return;
 3909: }
 3910: 
 3911: # ------------------------------------------------------------ Directory lister
 3912: 
 3913: sub dirlist {
 3914:     my ($uri,$userdomain,$username,$alternateDirectoryRoot)=@_;
 3915: 
 3916:     $uri=~s/^\///;
 3917:     $uri=~s/\/$//;
 3918:     my ($udom, $uname);
 3919:     (undef,$udom,$uname)=split(/\//,$uri);
 3920:     if(defined($userdomain)) {
 3921:         $udom = $userdomain;
 3922:     }
 3923:     if(defined($username)) {
 3924:         $uname = $username;
 3925:     }
 3926: 
 3927:     my $dirRoot = $perlvar{'lonDocRoot'};
 3928:     if(defined($alternateDirectoryRoot)) {
 3929:         $dirRoot = $alternateDirectoryRoot;
 3930:         $dirRoot =~ s/\/$//;
 3931:     }
 3932: 
 3933:     if($udom) {
 3934:         if($uname) {
 3935:             my $listing=reply('ls2:'.$dirRoot.'/'.$uri,
 3936:                               homeserver($uname,$udom));
 3937:             my @listing_results;
 3938:             if ($listing eq 'unknown_cmd') {
 3939:                 $listing=reply('ls:'.$dirRoot.'/'.$uri,
 3940:                                homeserver($uname,$udom));
 3941:                 @listing_results = split(/:/,$listing);
 3942:             } else {
 3943:                 @listing_results = map { &unescape($_); } split(/:/,$listing);
 3944:             }
 3945:             return @listing_results;
 3946:         } elsif(!defined($alternateDirectoryRoot)) {
 3947:             my $tryserver;
 3948:             my %allusers=();
 3949:             foreach $tryserver (keys %libserv) {
 3950:                 if($hostdom{$tryserver} eq $udom) {
 3951:                     my $listing=reply('ls2:'.$perlvar{'lonDocRoot'}.'/res/'.
 3952:                                       $udom, $tryserver);
 3953:                     my @listing_results;
 3954:                     if ($listing eq 'unknown_cmd') {
 3955:                         $listing=reply('ls:'.$perlvar{'lonDocRoot'}.'/res/'.
 3956:                                        $udom, $tryserver);
 3957:                         @listing_results = split(/:/,$listing);
 3958:                     } else {
 3959:                         @listing_results =
 3960:                             map { &unescape($_); } split(/:/,$listing);
 3961:                     }
 3962:                     if ($listing_results[0] ne 'no_such_dir' && 
 3963:                         $listing_results[0] ne 'empty'       &&
 3964:                         $listing_results[0] ne 'con_lost') {
 3965:                         foreach (@listing_results) {
 3966:                             my ($entry,@stat)=split(/&/,$_);
 3967:                             $allusers{$entry}=1;
 3968:                         }
 3969:                     }
 3970:                 }
 3971:             }
 3972:             my $alluserstr='';
 3973:             foreach (sort keys %allusers) {
 3974:                 $alluserstr.=$_.'&user:';
 3975:             }
 3976:             $alluserstr=~s/:$//;
 3977:             return split(/:/,$alluserstr);
 3978:         } else {
 3979:             my @emptyResults = ();
 3980:             push(@emptyResults, 'missing user name');
 3981:             return split(':',@emptyResults);
 3982:         }
 3983:     } elsif(!defined($alternateDirectoryRoot)) {
 3984:         my $tryserver;
 3985:         my %alldom=();
 3986:         foreach $tryserver (keys %libserv) {
 3987:             $alldom{$hostdom{$tryserver}}=1;
 3988:         }
 3989:         my $alldomstr='';
 3990:         foreach (sort keys %alldom) {
 3991:             $alldomstr.=$perlvar{'lonDocRoot'}.'/res/'.$_.'/&domain:';
 3992:         }
 3993:         $alldomstr=~s/:$//;
 3994:         return split(/:/,$alldomstr);       
 3995:     } else {
 3996:         my @emptyResults = ();
 3997:         push(@emptyResults, 'missing domain');
 3998:         return split(':',@emptyResults);
 3999:     }
 4000: }
 4001: 
 4002: # --------------------------------------------- GetFileTimestamp
 4003: # This function utilizes dirlist and returns the date stamp for
 4004: # when it was last modified.  It will also return an error of -1
 4005: # if an error occurs
 4006: 
 4007: ##
 4008: ## FIXME: This subroutine assumes its caller knows something about the
 4009: ## directory structure of the home server for the student ($root).
 4010: ## Not a good assumption to make.  Since this is for looking up files
 4011: ## in user directories, the full path should be constructed by lond, not
 4012: ## whatever machine we request data from.
 4013: ##
 4014: sub GetFileTimestamp {
 4015:     my ($studentDomain,$studentName,$filename,$root)=@_;
 4016:     $studentDomain=~s/\W//g;
 4017:     $studentName=~s/\W//g;
 4018:     my $subdir=$studentName.'__';
 4019:     $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
 4020:     my $proname="$studentDomain/$subdir/$studentName";
 4021:     $proname .= '/'.$filename;
 4022:     my ($fileStat) = &Apache::lonnet::dirlist($proname, $studentDomain, 
 4023:                                               $studentName, $root);
 4024:     my @stats = split('&', $fileStat);
 4025:     if($stats[0] ne 'empty' && $stats[0] ne 'no_such_dir') {
 4026:         # @stats contains first the filename, then the stat output
 4027:         return $stats[10]; # so this is 10 instead of 9.
 4028:     } else {
 4029:         return -1;
 4030:     }
 4031: }
 4032: 
 4033: # -------------------------------------------------------- Value of a Condition
 4034: 
 4035: sub directcondval {
 4036:     my $number=shift;
 4037:     if (!defined($env{'user.state.'.$env{'request.course.id'}})) {
 4038: 	&Apache::lonuserstate::evalstate();
 4039:     }
 4040:     if ($env{'user.state.'.$env{'request.course.id'}}) {
 4041:        return substr($env{'user.state.'.$env{'request.course.id'}},$number,1);
 4042:     } else {
 4043:        return 2;
 4044:     }
 4045: }
 4046: 
 4047: sub condval {
 4048:     my $condidx=shift;
 4049:     my $result=0;
 4050:     my $allpathcond='';
 4051:     foreach (split(/\|/,$condidx)) {
 4052:        if (defined($env{'acc.cond.'.$env{'request.course.id'}.'.'.$_})) {
 4053: 	   $allpathcond.=
 4054:                '('.$env{'acc.cond.'.$env{'request.course.id'}.'.'.$_}.')|';
 4055:        }
 4056:     }
 4057:     $allpathcond=~s/\|$//;
 4058:     if ($env{'request.course.id'}) {
 4059:        if ($allpathcond) {
 4060:           my $operand='|';
 4061: 	  my @stack;
 4062:            foreach ($allpathcond=~/(\d+|\(|\)|\&|\|)/g) {
 4063:               if ($_ eq '(') {
 4064:                  push @stack,($operand,$result)
 4065:               } elsif ($_ eq ')') {
 4066:                   my $before=pop @stack;
 4067: 		  if (pop @stack eq '&') {
 4068: 		      $result=$result>$before?$before:$result;
 4069:                   } else {
 4070:                       $result=$result>$before?$result:$before;
 4071:                   }
 4072:               } elsif (($_ eq '&') || ($_ eq '|')) {
 4073:                   $operand=$_;
 4074:               } else {
 4075:                   my $new=directcondval($_);
 4076:                   if ($operand eq '&') {
 4077:                      $result=$result>$new?$new:$result;
 4078:                   } else {
 4079:                      $result=$result>$new?$result:$new;
 4080:                   }
 4081:               }
 4082:           }
 4083:        }
 4084:     }
 4085:     return $result;
 4086: }
 4087: 
 4088: # ---------------------------------------------------- Devalidate courseresdata
 4089: 
 4090: sub devalidatecourseresdata {
 4091:     my ($coursenum,$coursedomain)=@_;
 4092:     my $hashid=$coursenum.':'.$coursedomain;
 4093:     &devalidate_cache_new('courseres',$hashid);
 4094: }
 4095: 
 4096: # --------------------------------------------------- Course Resourcedata Query
 4097: 
 4098: sub get_courseresdata {
 4099:     my ($coursenum,$coursedomain)=@_;
 4100:     my $coursehom=&homeserver($coursenum,$coursedomain);
 4101:     my $hashid=$coursenum.':'.$coursedomain;
 4102:     my ($result,$cached)=&is_cached_new('courseres',$hashid);
 4103:     my %dumpreply;
 4104:     unless (defined($cached)) {
 4105: 	%dumpreply=&dump('resourcedata',$coursedomain,$coursenum);
 4106: 	$result=\%dumpreply;
 4107: 	my ($tmp) = keys(%dumpreply);
 4108: 	if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
 4109: 	    &do_cache_new('courseres',$hashid,$result,600);
 4110: 	} elsif ($tmp =~ /^(con_lost|no_such_host)/) {
 4111: 	    return $tmp;
 4112: 	} elsif ($tmp =~ /^(error)/) {
 4113: 	    $result=undef;
 4114: 	    &do_cache_new('courseres',$hashid,$result,600);
 4115: 	}
 4116:     }
 4117:     return $result;
 4118: }
 4119: 
 4120: sub get_userresdata {
 4121:     my ($uname,$udom)=@_;
 4122:     #most student don\'t have any data set, check if there is some data
 4123:     if (&EXT_cache_status($udom,$uname)) { return undef; }
 4124: 
 4125:     my $hashid="$udom:$uname";
 4126:     my ($result,$cached)=&is_cached_new('userres',$hashid);
 4127:     if (!defined($cached)) {
 4128: 	my %resourcedata=&dump('resourcedata',$udom,$uname);
 4129: 	$result=\%resourcedata;
 4130: 	&do_cache_new('userres',$hashid,$result,600);
 4131:     }
 4132:     my ($tmp)=keys(%$result);
 4133:     if (($tmp!~/^error\:/) && ($tmp!~/^con_lost/)) {
 4134: 	return $result;
 4135:     }
 4136:     #error 2 occurs when the .db doesn't exist
 4137:     if ($tmp!~/error: 2 /) {
 4138: 	&logthis("<font color=blue>WARNING:".
 4139: 		 " Trying to get resource data for ".
 4140: 		 $uname." at ".$udom.": ".
 4141: 		 $tmp."</font>");
 4142:     } elsif ($tmp=~/error: 2 /) {
 4143: 	&EXT_cache_set($udom,$uname);
 4144:     }
 4145:     return $tmp;
 4146: }
 4147: 
 4148: sub resdata {
 4149:     my ($name,$domain,$type,@which)=@_;
 4150:     my $result;
 4151:     if ($type eq 'course') {
 4152: 	$result=&get_courseresdata($name,$domain);
 4153:     } elsif ($type eq 'user') {
 4154: 	$result=&get_userresdata($name,$domain);
 4155:     }
 4156:     if (!ref($result)) { return $result; }    
 4157:     foreach my $item (@which) {
 4158: 	if (defined($result->{$item})) {
 4159: 	    return $result->{$item};
 4160: 	}
 4161:     }
 4162:     return undef;
 4163: }
 4164: 
 4165: #
 4166: # EXT resource caching routines
 4167: #
 4168: 
 4169: sub clear_EXT_cache_status {
 4170:     &delenv('cache.EXT.');
 4171: }
 4172: 
 4173: sub EXT_cache_status {
 4174:     my ($target_domain,$target_user) = @_;
 4175:     my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
 4176:     if (exists($env{$cachename}) && ($env{$cachename}+600) > time) {
 4177:         # We know already the user has no data
 4178:         return 1;
 4179:     } else {
 4180:         return 0;
 4181:     }
 4182: }
 4183: 
 4184: sub EXT_cache_set {
 4185:     my ($target_domain,$target_user) = @_;
 4186:     my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
 4187:     &appenv($cachename => time);
 4188: }
 4189: 
 4190: # --------------------------------------------------------- Value of a Variable
 4191: sub EXT {
 4192:     my ($varname,$symbparm,$udom,$uname,$usection,$recurse)=@_;
 4193: 
 4194:     unless ($varname) { return ''; }
 4195:     #get real user name/domain, courseid and symb
 4196:     my $courseid;
 4197:     my $publicuser;
 4198:     if ($symbparm) {
 4199: 	$symbparm=&get_symb_from_alias($symbparm);
 4200:     }
 4201:     if (!($uname && $udom)) {
 4202:       (my $cursymb,$courseid,$udom,$uname,$publicuser)=
 4203: 	  &Apache::lonxml::whichuser($symbparm);
 4204:       if (!$symbparm) {	$symbparm=$cursymb; }
 4205:     } else {
 4206: 	$courseid=$env{'request.course.id'};
 4207:     }
 4208:     my ($realm,$space,$qualifier,@therest)=split(/\./,$varname);
 4209:     my $rest;
 4210:     if (defined($therest[0])) {
 4211:        $rest=join('.',@therest);
 4212:     } else {
 4213:        $rest='';
 4214:     }
 4215: 
 4216:     my $qualifierrest=$qualifier;
 4217:     if ($rest) { $qualifierrest.='.'.$rest; }
 4218:     my $spacequalifierrest=$space;
 4219:     if ($qualifierrest) { $spacequalifierrest.='.'.$qualifierrest; }
 4220:     if ($realm eq 'user') {
 4221: # --------------------------------------------------------------- user.resource
 4222: 	if ($space eq 'resource') {
 4223: 	    if (defined($Apache::lonhomework::parsing_a_problem) ||
 4224: 		defined($Apache::lonhomework::parsing_a_task)) {
 4225: 		return $Apache::lonhomework::history{$qualifierrest};
 4226: 	    } else {
 4227: 		my %restored;
 4228: 		if ($publicuser || $env{'request.state'} eq 'construct') {
 4229: 		    %restored=&tmprestore($symbparm,$courseid,$udom,$uname);
 4230: 		} else {
 4231: 		    %restored=&restore($symbparm,$courseid,$udom,$uname);
 4232: 		}
 4233: 		return $restored{$qualifierrest};
 4234: 	    }
 4235: # ----------------------------------------------------------------- user.access
 4236:         } elsif ($space eq 'access') {
 4237: 	    # FIXME - not supporting calls for a specific user
 4238:             return &allowed($qualifier,$rest);
 4239: # ------------------------------------------ user.preferences, user.environment
 4240:         } elsif (($space eq 'preferences') || ($space eq 'environment')) {
 4241: 	    if (($uname eq $env{'user.name'}) &&
 4242: 		($udom eq $env{'user.domain'})) {
 4243: 		return $env{join('.',('environment',$qualifierrest))};
 4244: 	    } else {
 4245: 		my %returnhash;
 4246: 		if (!$publicuser) {
 4247: 		    %returnhash=&userenvironment($udom,$uname,
 4248: 						 $qualifierrest);
 4249: 		}
 4250: 		return $returnhash{$qualifierrest};
 4251: 	    }
 4252: # ----------------------------------------------------------------- user.course
 4253:         } elsif ($space eq 'course') {
 4254: 	    # FIXME - not supporting calls for a specific user
 4255:             return $env{join('.',('request.course',$qualifier))};
 4256: # ------------------------------------------------------------------- user.role
 4257:         } elsif ($space eq 'role') {
 4258: 	    # FIXME - not supporting calls for a specific user
 4259:             my ($role,$where)=split(/\./,$env{'request.role'});
 4260:             if ($qualifier eq 'value') {
 4261: 		return $role;
 4262:             } elsif ($qualifier eq 'extent') {
 4263:                 return $where;
 4264:             }
 4265: # ----------------------------------------------------------------- user.domain
 4266:         } elsif ($space eq 'domain') {
 4267:             return $udom;
 4268: # ------------------------------------------------------------------- user.name
 4269:         } elsif ($space eq 'name') {
 4270:             return $uname;
 4271: # ---------------------------------------------------- Any other user namespace
 4272:         } else {
 4273: 	    my %reply;
 4274: 	    if (!$publicuser) {
 4275: 		%reply=&get($space,[$qualifierrest],$udom,$uname);
 4276: 	    }
 4277: 	    return $reply{$qualifierrest};
 4278:         }
 4279:     } elsif ($realm eq 'query') {
 4280: # ---------------------------------------------- pull stuff out of query string
 4281:         &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
 4282: 						[$spacequalifierrest]);
 4283: 	return $env{'form.'.$spacequalifierrest}; 
 4284:    } elsif ($realm eq 'request') {
 4285: # ------------------------------------------------------------- request.browser
 4286:         if ($space eq 'browser') {
 4287: 	    if ($qualifier eq 'textremote') {
 4288: 		if (&mt('textual_remote_display') eq 'on') {
 4289: 		    return 1;
 4290: 		} else {
 4291: 		    return 0;
 4292: 		}
 4293: 	    } else {
 4294: 		return $env{'browser.'.$qualifier};
 4295: 	    }
 4296: # ------------------------------------------------------------ request.filename
 4297:         } else {
 4298:             return $env{'request.'.$spacequalifierrest};
 4299:         }
 4300:     } elsif ($realm eq 'course') {
 4301: # ---------------------------------------------------------- course.description
 4302:         return $env{'course.'.$courseid.'.'.$spacequalifierrest};
 4303:     } elsif ($realm eq 'resource') {
 4304: 
 4305: 	my $section;
 4306: 	if (defined($courseid) && $courseid eq $env{'request.course.id'}) {
 4307: 	    if (!$symbparm) { $symbparm=&symbread(); }
 4308: 	}
 4309: 	my ($courselevelm,$courselevel);
 4310: 	if ($symbparm && defined($courseid) && 
 4311: 	    $courseid eq $env{'request.course.id'}) {
 4312: 
 4313: 	    #print '<br>'.$space.' - '.$qualifier.' - '.$spacequalifierrest;
 4314: 
 4315: # ----------------------------------------------------- Cascading lookup scheme
 4316: 	    my $symbp=$symbparm;
 4317: 	    my $mapp=(&decode_symb($symbp))[0];
 4318: 
 4319: 	    my $symbparm=$symbp.'.'.$spacequalifierrest;
 4320: 	    my $mapparm=$mapp.'___(all).'.$spacequalifierrest;
 4321: 
 4322: 	    if (($env{'user.name'} eq $uname) &&
 4323: 		($env{'user.domain'} eq $udom)) {
 4324: 		$section=$env{'request.course.sec'};
 4325: 	    } else {
 4326: 		if (! defined($usection)) {
 4327: 		    $section=&getsection($udom,$uname,$courseid);
 4328: 		} else {
 4329: 		    $section = $usection;
 4330: 		}
 4331: 	    }
 4332: 
 4333: 	    my $seclevel=$courseid.'.['.$section.'].'.$spacequalifierrest;
 4334: 	    my $seclevelr=$courseid.'.['.$section.'].'.$symbparm;
 4335: 	    my $seclevelm=$courseid.'.['.$section.'].'.$mapparm;
 4336: 
 4337: 	    $courselevel=$courseid.'.'.$spacequalifierrest;
 4338: 	    my $courselevelr=$courseid.'.'.$symbparm;
 4339: 	    $courselevelm=$courseid.'.'.$mapparm;
 4340: 
 4341: # ----------------------------------------------------------- first, check user
 4342: 
 4343: 	    my $userreply=&resdata($uname,$udom,'user',
 4344: 				       ($courselevelr,$courselevelm,
 4345: 					$courselevel));
 4346: 
 4347: 	    if (defined($userreply)) { return $userreply; }
 4348: 
 4349: # ------------------------------------------------ second, check some of course
 4350: 
 4351: 	    my $coursereply=&resdata($env{'course.'.$courseid.'.num'},
 4352: 				     $env{'course.'.$courseid.'.domain'},
 4353: 				     'course',
 4354: 				     ($seclevelr,$seclevelm,$seclevel,
 4355: 				      $courselevelr));
 4356: 	    if (defined($coursereply)) { return $coursereply; }
 4357: 
 4358: # ------------------------------------------------------ third, check map parms
 4359: 	    my %parmhash=();
 4360: 	    my $thisparm='';
 4361: 	    if (tie(%parmhash,'GDBM_File',
 4362: 		    $env{'request.course.fn'}.'_parms.db',
 4363: 		    &GDBM_READER(),0640)) {
 4364: 		$thisparm=$parmhash{$symbparm};
 4365: 		untie(%parmhash);
 4366: 	    }
 4367: 	    if ($thisparm) { return $thisparm; }
 4368: 	}
 4369: # ------------------------------------------ fourth, look in resource metadata
 4370: 
 4371: 	$spacequalifierrest=~s/\./\_/;
 4372: 	my $filename;
 4373: 	if (!$symbparm) { $symbparm=&symbread(); }
 4374: 	if ($symbparm) {
 4375: 	    $filename=(&decode_symb($symbparm))[2];
 4376: 	} else {
 4377: 	    $filename=$env{'request.filename'};
 4378: 	}
 4379: 	my $metadata=&metadata($filename,$spacequalifierrest);
 4380: 	if (defined($metadata)) { return $metadata; }
 4381: 	$metadata=&metadata($filename,'parameter_'.$spacequalifierrest);
 4382: 	if (defined($metadata)) { return $metadata; }
 4383: 
 4384: # ---------------------------------------------- fourth, look in rest pf course
 4385: 	if ($symbparm && defined($courseid) && 
 4386: 	    $courseid eq $env{'request.course.id'}) {
 4387: 	    my $coursereply=&resdata($env{'course.'.$courseid.'.num'},
 4388: 				     $env{'course.'.$courseid.'.domain'},
 4389: 				     'course',
 4390: 				     ($courselevelm,$courselevel));
 4391: 	    if (defined($coursereply)) { return $coursereply; }
 4392: 	}
 4393: # ------------------------------------------------------------------ Cascade up
 4394: 	unless ($space eq '0') {
 4395: 	    my @parts=split(/_/,$space);
 4396: 	    my $id=pop(@parts);
 4397: 	    my $part=join('_',@parts);
 4398: 	    if ($part eq '') { $part='0'; }
 4399: 	    my $partgeneral=&EXT('resource.'.$part.'.'.$qualifierrest,
 4400: 				 $symbparm,$udom,$uname,$section,1);
 4401: 	    if (defined($partgeneral)) { return $partgeneral; }
 4402: 	}
 4403: 	if ($recurse) { return undef; }
 4404: 	my $pack_def=&packages_tab_default($filename,$varname);
 4405: 	if (defined($pack_def)) { return $pack_def; }
 4406: 
 4407: # ---------------------------------------------------- Any other user namespace
 4408:     } elsif ($realm eq 'environment') {
 4409: # ----------------------------------------------------------------- environment
 4410: 	if (($uname eq $env{'user.name'})&&($udom eq $env{'user.domain'})) {
 4411: 	    return $env{'environment.'.$spacequalifierrest};
 4412: 	} else {
 4413: 	    my %returnhash=&userenvironment($udom,$uname,
 4414: 					    $spacequalifierrest);
 4415: 	    return $returnhash{$spacequalifierrest};
 4416: 	}
 4417:     } elsif ($realm eq 'system') {
 4418: # ----------------------------------------------------------------- system.time
 4419: 	if ($space eq 'time') {
 4420: 	    return time;
 4421:         }
 4422:     }
 4423:     return '';
 4424: }
 4425: 
 4426: sub packages_tab_default {
 4427:     my ($uri,$varname)=@_;
 4428:     my (undef,$part,$name)=split(/\./,$varname);
 4429:     my $packages=&metadata($uri,'packages');
 4430:     foreach my $package (split(/,/,$packages)) {
 4431: 	my ($pack_type,$pack_part)=split(/_/,$package,2);
 4432: 	if (defined($packagetab{"$pack_type&$name&default"})) {
 4433: 	    return $packagetab{"$pack_type&$name&default"};
 4434: 	}
 4435: 	if ($pack_type eq 'part') { $pack_part='0'; }
 4436: 	if (defined($packagetab{$pack_type."_".$pack_part."&$name&default"})) {
 4437: 	    return $packagetab{$pack_type."_".$pack_part."&$name&default"};
 4438: 	}
 4439:     }
 4440:     return undef;
 4441: }
 4442: 
 4443: sub add_prefix_and_part {
 4444:     my ($prefix,$part)=@_;
 4445:     my $keyroot;
 4446:     if (defined($prefix) && $prefix !~ /^__/) {
 4447: 	# prefix that has a part already
 4448: 	$keyroot=$prefix;
 4449:     } elsif (defined($prefix)) {
 4450: 	# prefix that is missing a part
 4451: 	if (defined($part)) { $keyroot='_'.$part.substr($prefix,1); }
 4452:     } else {
 4453: 	# no prefix at all
 4454: 	if (defined($part)) { $keyroot='_'.$part; }
 4455:     }
 4456:     return $keyroot;
 4457: }
 4458: 
 4459: # ---------------------------------------------------------------- Get metadata
 4460: 
 4461: my %metaentry;
 4462: sub metadata {
 4463:     my ($uri,$what,$liburi,$prefix,$depthcount)=@_;
 4464:     $uri=&declutter($uri);
 4465:     # if it is a non metadata possible uri return quickly
 4466:     if (($uri eq '') || 
 4467: 	(($uri =~ m|^/*adm/|) && 
 4468: 	     ($uri !~ m|^adm/includes|) && ($uri !~ m|/bulletinboard$|)) ||
 4469:         ($uri =~ m|/$|) || ($uri =~ m|/.meta$|) || ($uri =~ /^~/) ||
 4470: 	($uri =~ m|home/[^/]+/public_html/|)) {
 4471: 	return undef;
 4472:     }
 4473:     my $filename=$uri;
 4474:     $uri=~s/\.meta$//;
 4475: #
 4476: # Is the metadata already cached?
 4477: # Look at timestamp of caching
 4478: # Everything is cached by the main uri, libraries are never directly cached
 4479: #
 4480:     if (!defined($liburi)) {
 4481: 	my ($result,$cached)=&is_cached_new('meta',$uri);
 4482: 	if (defined($cached)) { return $result->{':'.$what}; }
 4483:     }
 4484:     {
 4485: #
 4486: # Is this a recursive call for a library?
 4487: #
 4488: #	if (! exists($metacache{$uri})) {
 4489: #	    $metacache{$uri}={};
 4490: #	}
 4491:         if ($liburi) {
 4492: 	    $liburi=&declutter($liburi);
 4493:             $filename=$liburi;
 4494:         } else {
 4495: 	    &devalidate_cache_new('meta',$uri);
 4496: 	    undef(%metaentry);
 4497: 	}
 4498:         my %metathesekeys=();
 4499:         unless ($filename=~/\.meta$/) { $filename.='.meta'; }
 4500: 	my $metastring;
 4501: 	if ($uri !~ m -^(uploaded|editupload)/-) {
 4502: 	    my $file=&filelocation('',&clutter($filename));
 4503: 	    #push(@{$metaentry{$uri.'.file'}},$file);
 4504: 	    $metastring=&getfile($file);
 4505: 	}
 4506:         my $parser=HTML::LCParser->new(\$metastring);
 4507:         my $token;
 4508:         undef %metathesekeys;
 4509:         while ($token=$parser->get_token) {
 4510: 	    if ($token->[0] eq 'S') {
 4511: 		if (defined($token->[2]->{'package'})) {
 4512: #
 4513: # This is a package - get package info
 4514: #
 4515: 		    my $package=$token->[2]->{'package'};
 4516: 		    my $keyroot=&add_prefix_and_part($prefix,$token->[2]->{'part'});
 4517: 		    if (defined($token->[2]->{'id'})) { 
 4518: 			$keyroot.='_'.$token->[2]->{'id'}; 
 4519: 		    }
 4520: 		    if ($metaentry{':packages'}) {
 4521: 			$metaentry{':packages'}.=','.$package.$keyroot;
 4522: 		    } else {
 4523: 			$metaentry{':packages'}=$package.$keyroot;
 4524: 		    }
 4525: 		    foreach (sort keys %packagetab) {
 4526: 			my $part=$keyroot;
 4527: 			$part=~s/^\_//;
 4528: 			if ($_=~/^\Q$package\E\&/ || 
 4529: 			    $_=~/^\Q$package\E_0\&/) {
 4530: 			    my ($pack,$name,$subp)=split(/\&/,$_);
 4531: 			    # ignore package.tab specified default values
 4532:                             # here &package_tab_default() will fetch those
 4533: 			    if ($subp eq 'default') { next; }
 4534: 			    my $value=$packagetab{$_};
 4535: 			    my $unikey;
 4536: 			    if ($pack =~ /_0$/) {
 4537: 				$unikey='parameter_0_'.$name;
 4538: 				$part=0;
 4539: 			    } else {
 4540: 				$unikey='parameter'.$keyroot.'_'.$name;
 4541: 			    }
 4542: 			    if ($subp eq 'display') {
 4543: 				$value.=' [Part: '.$part.']';
 4544: 			    }
 4545: 			    $metaentry{':'.$unikey.'.part'}=$part;
 4546: 			    $metathesekeys{$unikey}=1;
 4547: 			    unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
 4548: 				$metaentry{':'.$unikey.'.'.$subp}=$value;
 4549: 			    }
 4550: 			    if (defined($metaentry{':'.$unikey.'.default'})) {
 4551: 				$metaentry{':'.$unikey}=
 4552: 				    $metaentry{':'.$unikey.'.default'};
 4553: 			    }
 4554: 			}
 4555: 		    }
 4556: 		} else {
 4557: #
 4558: # This is not a package - some other kind of start tag
 4559: #
 4560: 		    my $entry=$token->[1];
 4561: 		    my $unikey;
 4562: 		    if ($entry eq 'import') {
 4563: 			$unikey='';
 4564: 		    } else {
 4565: 			$unikey=$entry;
 4566: 		    }
 4567: 		    $unikey.=&add_prefix_and_part($prefix,$token->[2]->{'part'});
 4568: 
 4569: 		    if (defined($token->[2]->{'id'})) { 
 4570: 			$unikey.='_'.$token->[2]->{'id'}; 
 4571: 		    }
 4572: 
 4573: 		    if ($entry eq 'import') {
 4574: #
 4575: # Importing a library here
 4576: #
 4577: 			if ($depthcount<20) {
 4578: 			    my $location=$parser->get_text('/import');
 4579: 			    my $dir=$filename;
 4580: 			    $dir=~s|[^/]*$||;
 4581: 			    $location=&filelocation($dir,$location);
 4582: 			    foreach (sort(split(/\,/,&metadata($uri,'keys',
 4583: 							       $location,$unikey,
 4584: 							       $depthcount+1)))) {
 4585: 				$metaentry{':'.$_}=$metaentry{':'.$_};
 4586: 				$metathesekeys{$_}=1;
 4587: 			    }
 4588: 			}
 4589: 		    } else { 
 4590: 			
 4591: 			if (defined($token->[2]->{'name'})) { 
 4592: 			    $unikey.='_'.$token->[2]->{'name'}; 
 4593: 			}
 4594: 			$metathesekeys{$unikey}=1;
 4595: 			foreach (@{$token->[3]}) {
 4596: 			    $metaentry{':'.$unikey.'.'.$_}=$token->[2]->{$_};
 4597: 			}
 4598: 			my $internaltext=&HTML::Entities::decode($parser->get_text('/'.$entry));
 4599: 			my $default=$metaentry{':'.$unikey.'.default'};
 4600: 			if ( $internaltext =~ /^\s*$/ && $default !~ /^\s*$/) {
 4601: 		 # only ws inside the tag, and not in default, so use default
 4602: 		 # as value
 4603: 			    $metaentry{':'.$unikey}=$default;
 4604: 			} else {
 4605: 		  # either something interesting inside the tag or default
 4606:                   # uninteresting
 4607: 			    $metaentry{':'.$unikey}=$internaltext;
 4608: 			}
 4609: # end of not-a-package not-a-library import
 4610: 		    }
 4611: # end of not-a-package start tag
 4612: 		}
 4613: # the next is the end of "start tag"
 4614: 	    }
 4615: 	}
 4616: 	my ($extension) = ($uri =~ /\.(\w+)$/);
 4617: 	foreach my $key (sort(keys(%packagetab))) {
 4618: 	    #&logthis("extsion1 $extension $key !!");
 4619: 	    #no specific packages #how's our extension
 4620: 	    if ($key!~/^extension_\Q$extension\E&/) { next; }
 4621: 	    &metadata_create_package_def($uri,$key,'extension_'.$extension,
 4622: 					 \%metathesekeys);
 4623: 	}
 4624: 	if (!exists($metaentry{':packages'})) {
 4625: 	    foreach my $key (sort(keys(%packagetab))) {
 4626: 		#no specific packages well let's get default then
 4627: 		if ($key!~/^default&/) { next; }
 4628: 		&metadata_create_package_def($uri,$key,'default',
 4629: 					     \%metathesekeys);
 4630: 	    }
 4631: 	}
 4632: # are there custom rights to evaluate
 4633: 	if ($metaentry{':copyright'} eq 'custom') {
 4634: 
 4635:     #
 4636:     # Importing a rights file here
 4637:     #
 4638: 	    unless ($depthcount) {
 4639: 		my $location=$metaentry{':customdistributionfile'};
 4640: 		my $dir=$filename;
 4641: 		$dir=~s|[^/]*$||;
 4642: 		$location=&filelocation($dir,$location);
 4643: 		foreach (sort(split(/\,/,&metadata($uri,'keys',
 4644: 						   $location,'_rights',
 4645: 						   $depthcount+1)))) {
 4646: 		    #$metaentry{':'.$_}=$metacache{$uri}->{':'.$_};
 4647: 		    $metathesekeys{$_}=1;
 4648: 		}
 4649: 	    }
 4650: 	}
 4651: 	$metaentry{':keys'}=join(',',keys %metathesekeys);
 4652: 	&metadata_generate_part0(\%metathesekeys,\%metaentry,$uri);
 4653: 	$metaentry{':allpossiblekeys'}=join(',',keys %metathesekeys);
 4654: 	&do_cache_new('meta',$uri,\%metaentry,60*60*24);
 4655: # this is the end of "was not already recently cached
 4656:     }
 4657:     return $metaentry{':'.$what};
 4658: }
 4659: 
 4660: sub metadata_create_package_def {
 4661:     my ($uri,$key,$package,$metathesekeys)=@_;
 4662:     my ($pack,$name,$subp)=split(/\&/,$key);
 4663:     if ($subp eq 'default') { next; }
 4664:     
 4665:     if (defined($metaentry{':packages'})) {
 4666: 	$metaentry{':packages'}.=','.$package;
 4667:     } else {
 4668: 	$metaentry{':packages'}=$package;
 4669:     }
 4670:     my $value=$packagetab{$key};
 4671:     my $unikey;
 4672:     $unikey='parameter_0_'.$name;
 4673:     $metaentry{':'.$unikey.'.part'}=0;
 4674:     $$metathesekeys{$unikey}=1;
 4675:     unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
 4676: 	$metaentry{':'.$unikey.'.'.$subp}=$value;
 4677:     }
 4678:     if (defined($metaentry{':'.$unikey.'.default'})) {
 4679: 	$metaentry{':'.$unikey}=
 4680: 	    $metaentry{':'.$unikey.'.default'};
 4681:     }
 4682: }
 4683: 
 4684: sub metadata_generate_part0 {
 4685:     my ($metadata,$metacache,$uri) = @_;
 4686:     my %allnames;
 4687:     foreach my $metakey (sort keys %$metadata) {
 4688: 	if ($metakey=~/^parameter\_(.*)/) {
 4689: 	  my $part=$$metacache{':'.$metakey.'.part'};
 4690: 	  my $name=$$metacache{':'.$metakey.'.name'};
 4691: 	  if (! exists($$metadata{'parameter_0_'.$name.'.name'})) {
 4692: 	    $allnames{$name}=$part;
 4693: 	  }
 4694: 	}
 4695:     }
 4696:     foreach my $name (keys(%allnames)) {
 4697:       $$metadata{"parameter_0_$name"}=1;
 4698:       my $key=":parameter_0_$name";
 4699:       $$metacache{"$key.part"}='0';
 4700:       $$metacache{"$key.name"}=$name;
 4701:       $$metacache{"$key.type"}=$$metacache{':parameter_'.
 4702: 					   $allnames{$name}.'_'.$name.
 4703: 					   '.type'};
 4704:       my $olddis=$$metacache{':parameter_'.$allnames{$name}.'_'.$name.
 4705: 			     '.display'};
 4706:       my $expr='\\[Part: '.$allnames{$name}.'\\]';
 4707:       $olddis=~s/\Q$expr\E/\[Part: 0\]/;
 4708:       $$metacache{"$key.display"}=$olddis;
 4709:     }
 4710: }
 4711: 
 4712: # ------------------------------------------------- Get the title of a resource
 4713: 
 4714: sub gettitle {
 4715:     my $urlsymb=shift;
 4716:     my $symb=&symbread($urlsymb);
 4717:     if ($symb) {
 4718: 	my $key=$env{'request.course.id'}."\0".$symb;
 4719: 	my ($result,$cached)=&is_cached_new('title',$key);
 4720: 	if (defined($cached)) { 
 4721: 	    return $result;
 4722: 	}
 4723: 	my ($map,$resid,$url)=&decode_symb($symb);
 4724: 	my $title='';
 4725: 	my %bighash;
 4726: 	if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
 4727: 		&GDBM_READER(),0640)) {
 4728: 	    my $mapid=$bighash{'map_pc_'.&clutter($map)};
 4729: 	    $title=$bighash{'title_'.$mapid.'.'.$resid};
 4730: 	    untie %bighash;
 4731: 	}
 4732: 	$title=~s/\&colon\;/\:/gs;
 4733: 	if ($title) {
 4734: 	    return &do_cache_new('title',$key,$title,600);
 4735: 	}
 4736: 	$urlsymb=$url;
 4737:     }
 4738:     my $title=&metadata($urlsymb,'title');
 4739:     if (!$title) { $title=(split('/',$urlsymb))[-1]; }    
 4740:     return $title;
 4741: }
 4742: 
 4743: sub get_slot {
 4744:     my ($which,$cnum,$cdom)=@_;
 4745:     if (!$cnum || !$cdom) {
 4746: 	(undef,my $courseid)=&Apache::lonxml::whichuser();
 4747: 	$cdom=$env{'course.'.$courseid.'.domain'};
 4748: 	$cnum=$env{'course.'.$courseid.'.num'};
 4749:     }
 4750:     my %slotinfo=&get('slots',[$which],$cdom,$cnum);
 4751:     &Apache::lonhomework::showhash(%slotinfo);
 4752:     my ($tmp)=keys(%slotinfo);
 4753:     if ($tmp=~/^error:/) { return (); }
 4754:     if (ref($slotinfo{$which}) eq 'HASH') {
 4755: 	return %{$slotinfo{$which}};
 4756:     }
 4757:     return $slotinfo{$which};
 4758: }
 4759: # ------------------------------------------------- Update symbolic store links
 4760: 
 4761: sub symblist {
 4762:     my ($mapname,%newhash)=@_;
 4763:     $mapname=&deversion(&declutter($mapname));
 4764:     my %hash;
 4765:     if (($env{'request.course.fn'}) && (%newhash)) {
 4766:         if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
 4767:                       &GDBM_WRCREAT(),0640)) {
 4768: 	    foreach (keys %newhash) {
 4769:                 $hash{declutter($_)}=&encode_symb($mapname,$newhash{$_}->[1],
 4770: 						  $newhash{$_}->[0]);
 4771:             }
 4772:             if (untie(%hash)) {
 4773: 		return 'ok';
 4774:             }
 4775:         }
 4776:     }
 4777:     return 'error';
 4778: }
 4779: 
 4780: # --------------------------------------------------------------- Verify a symb
 4781: 
 4782: sub symbverify {
 4783:     my ($symb,$thisurl)=@_;
 4784:     my $thisfn=$thisurl;
 4785: # wrapper not part of symbs
 4786:     $thisfn=~s/^\/adm\/wrapper//;
 4787:     $thisfn=&declutter($thisfn);
 4788: # direct jump to resource in page or to a sequence - will construct own symbs
 4789:     if ($thisfn=~/\.(page|sequence)$/) { return 1; }
 4790: # check URL part
 4791:     my ($map,$resid,$url)=&decode_symb($symb);
 4792: 
 4793:     unless ($url eq $thisfn) { return 0; }
 4794: 
 4795:     $symb=&symbclean($symb);
 4796:     $thisurl=&deversion($thisurl);
 4797:     $thisfn=&deversion($thisfn);
 4798: 
 4799:     my %bighash;
 4800:     my $okay=0;
 4801: 
 4802:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
 4803:                             &GDBM_READER(),0640)) {
 4804:         my $ids=$bighash{'ids_'.&clutter($thisurl)};
 4805:         unless ($ids) { 
 4806:            $ids=$bighash{'ids_/'.$thisurl};
 4807:         }
 4808:         if ($ids) {
 4809: # ------------------------------------------------------------------- Has ID(s)
 4810: 	    foreach (split(/\,/,$ids)) {
 4811:                my ($mapid,$resid)=split(/\./,$_);
 4812:                if (
 4813:   &symbclean(&declutter($bighash{'map_id_'.$mapid}).'___'.$resid.'___'.$thisfn)
 4814:    eq $symb) { 
 4815: 		   if (($env{'request.role.adv'}) ||
 4816: 		       $bighash{'encrypted_'.$_} eq $env{'request.enc'}) {
 4817: 		       $okay=1; 
 4818: 		   }
 4819: 	       }
 4820: 	   }
 4821:         }
 4822: 	untie(%bighash);
 4823:     }
 4824:     return $okay;
 4825: }
 4826: 
 4827: # --------------------------------------------------------------- Clean-up symb
 4828: 
 4829: sub symbclean {
 4830:     my $symb=shift;
 4831:     if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
 4832: # remove version from map
 4833:     $symb=~s/\.(\d+)\.(\w+)\_\_\_/\.$2\_\_\_/;
 4834: 
 4835: # remove version from URL
 4836:     $symb=~s/\.(\d+)\.(\w+)$/\.$2/;
 4837: 
 4838: # remove wrapper
 4839: 
 4840:     $symb=~s/(\_\_\_\d+\_\_\_)adm\/wrapper\/(res\/)*/$1/;
 4841:     return $symb;
 4842: }
 4843: 
 4844: # ---------------------------------------------- Split symb to find map and url
 4845: 
 4846: sub encode_symb {
 4847:     my ($map,$resid,$url)=@_;
 4848:     return &symbclean(&declutter($map).'___'.$resid.'___'.&declutter($url));
 4849: }
 4850: 
 4851: sub decode_symb {
 4852:     my $symb=shift;
 4853:     if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
 4854:     my ($map,$resid,$url)=split(/___/,$symb);
 4855:     return (&fixversion($map),$resid,&fixversion($url));
 4856: }
 4857: 
 4858: sub fixversion {
 4859:     my $fn=shift;
 4860:     if ($fn=~/^(adm|uploaded|editupload|public)/) { return $fn; }
 4861:     my %bighash;
 4862:     my $uri=&clutter($fn);
 4863:     my $key=$env{'request.course.id'}.'_'.$uri;
 4864: # is this cached?
 4865:     my ($result,$cached)=&is_cached_new('courseresversion',$key);
 4866:     if (defined($cached)) { return $result; }
 4867: # unfortunately not cached, or expired
 4868:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
 4869: 	    &GDBM_READER(),0640)) {
 4870:  	if ($bighash{'version_'.$uri}) {
 4871:  	    my $version=$bighash{'version_'.$uri};
 4872:  	    unless (($version eq 'mostrecent') || 
 4873: 		    ($version==&getversion($uri))) {
 4874:  		$uri=~s/\.(\w+)$/\.$version\.$1/;
 4875:  	    }
 4876:  	}
 4877:  	untie %bighash;
 4878:     }
 4879:     return &do_cache_new('courseresversion',$key,&declutter($uri),600);
 4880: }
 4881: 
 4882: sub deversion {
 4883:     my $url=shift;
 4884:     $url=~s/\.\d+\.(\w+)$/\.$1/;
 4885:     return $url;
 4886: }
 4887: 
 4888: # ------------------------------------------------------ Return symb list entry
 4889: 
 4890: sub symbread {
 4891:     my ($thisfn,$donotrecurse)=@_;
 4892:     my $cache_str='request.symbread.cached.'.$thisfn;
 4893:     if (defined($env{$cache_str})) { return $env{$cache_str}; }
 4894: # no filename provided? try from environment
 4895:     unless ($thisfn) {
 4896:         if ($env{'request.symb'}) {
 4897: 	    return $env{$cache_str}=&symbclean($env{'request.symb'});
 4898: 	}
 4899: 	$thisfn=$env{'request.filename'};
 4900:     }
 4901:     if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
 4902: # is that filename actually a symb? Verify, clean, and return
 4903:     if ($thisfn=~/\_\_\_\d+\_\_\_(.*)$/) {
 4904: 	if (&symbverify($thisfn,$1)) {
 4905: 	    return $env{$cache_str}=&symbclean($thisfn);
 4906: 	}
 4907:     }
 4908:     $thisfn=declutter($thisfn);
 4909:     my %hash;
 4910:     my %bighash;
 4911:     my $syval='';
 4912:     if (($env{'request.course.fn'}) && ($thisfn)) {
 4913:         my $targetfn = $thisfn;
 4914:         if ( ($thisfn =~ m/^(uploaded|editupload)\//) && ($thisfn !~ m/\.(page|sequence)$/) ) {
 4915:             $targetfn = 'adm/wrapper/'.$thisfn;
 4916:         }
 4917:         if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
 4918:                       &GDBM_READER(),0640)) {
 4919: 	    $syval=$hash{$targetfn};
 4920:             untie(%hash);
 4921:         }
 4922: # ---------------------------------------------------------- There was an entry
 4923:         if ($syval) {
 4924: 	    #unless ($syval=~/\_\d+$/) {
 4925: 		#unless ($env{'form.request.prefix'}=~/\.(\d+)\_$/) {
 4926: 		    #&appenv('request.ambiguous' => $thisfn);
 4927: 		    #return $env{$cache_str}='';
 4928: 		#}    
 4929: 		#$syval.=$1;
 4930: 	    #}
 4931:         } else {
 4932: # ------------------------------------------------------- Was not in symb table
 4933:            if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
 4934:                             &GDBM_READER(),0640)) {
 4935: # ---------------------------------------------- Get ID(s) for current resource
 4936:               my $ids=$bighash{'ids_'.&clutter($thisfn)};
 4937:               unless ($ids) { 
 4938:                  $ids=$bighash{'ids_/'.$thisfn};
 4939:               }
 4940:               unless ($ids) {
 4941: # alias?
 4942: 		  $ids=$bighash{'mapalias_'.$thisfn};
 4943:               }
 4944:               if ($ids) {
 4945: # ------------------------------------------------------------------- Has ID(s)
 4946:                  my @possibilities=split(/\,/,$ids);
 4947:                  if ($#possibilities==0) {
 4948: # ----------------------------------------------- There is only one possibility
 4949: 		     my ($mapid,$resid)=split(/\./,$ids);
 4950: 		     $syval=&encode_symb($bighash{'map_id_'.$mapid},
 4951: 						    $resid,$thisfn);
 4952:                  } elsif (!$donotrecurse) {
 4953: # ------------------------------------------ There is more than one possibility
 4954:                      my $realpossible=0;
 4955:                      foreach (@possibilities) {
 4956: 			 my $file=$bighash{'src_'.$_};
 4957:                          if (&allowed('bre',$file)) {
 4958:          		    my ($mapid,$resid)=split(/\./,$_);
 4959:                             if ($bighash{'map_type_'.$mapid} ne 'page') {
 4960: 				$realpossible++;
 4961:                                 $syval=&encode_symb($bighash{'map_id_'.$mapid},
 4962: 						    $resid,$thisfn);
 4963:                             }
 4964: 			 }
 4965:                      }
 4966: 		     if ($realpossible!=1) { $syval=''; }
 4967:                  } else {
 4968:                      $syval='';
 4969:                  }
 4970: 	      }
 4971:               untie(%bighash)
 4972:            }
 4973:         }
 4974:         if ($syval) {
 4975: 	    return $env{$cache_str}=$syval;
 4976:         }
 4977:     }
 4978:     &appenv('request.ambiguous' => $thisfn);
 4979:     return $env{$cache_str}='';
 4980: }
 4981: 
 4982: # ---------------------------------------------------------- Return random seed
 4983: 
 4984: sub numval {
 4985:     my $txt=shift;
 4986:     $txt=~tr/A-J/0-9/;
 4987:     $txt=~tr/a-j/0-9/;
 4988:     $txt=~tr/K-T/0-9/;
 4989:     $txt=~tr/k-t/0-9/;
 4990:     $txt=~tr/U-Z/0-5/;
 4991:     $txt=~tr/u-z/0-5/;
 4992:     $txt=~s/\D//g;
 4993:     if ($_64bit) { if ($txt > 2**32) { return -1; } }
 4994:     return int($txt);
 4995: }
 4996: 
 4997: sub numval2 {
 4998:     my $txt=shift;
 4999:     $txt=~tr/A-J/0-9/;
 5000:     $txt=~tr/a-j/0-9/;
 5001:     $txt=~tr/K-T/0-9/;
 5002:     $txt=~tr/k-t/0-9/;
 5003:     $txt=~tr/U-Z/0-5/;
 5004:     $txt=~tr/u-z/0-5/;
 5005:     $txt=~s/\D//g;
 5006:     my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
 5007:     my $total;
 5008:     foreach my $val (@txts) { $total+=$val; }
 5009:     if ($_64bit) { if ($total > 2**32) { return -1; } }
 5010:     return int($total);
 5011: }
 5012: 
 5013: sub numval3 {
 5014:     use integer;
 5015:     my $txt=shift;
 5016:     $txt=~tr/A-J/0-9/;
 5017:     $txt=~tr/a-j/0-9/;
 5018:     $txt=~tr/K-T/0-9/;
 5019:     $txt=~tr/k-t/0-9/;
 5020:     $txt=~tr/U-Z/0-5/;
 5021:     $txt=~tr/u-z/0-5/;
 5022:     $txt=~s/\D//g;
 5023:     my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
 5024:     my $total;
 5025:     foreach my $val (@txts) { $total+=$val; }
 5026:     if ($_64bit) { $total=(($total<<32)>>32); }
 5027:     return $total;
 5028: }
 5029: 
 5030: sub latest_rnd_algorithm_id {
 5031:     return '64bit4';
 5032: }
 5033: 
 5034: sub get_rand_alg {
 5035:     my ($courseid)=@_;
 5036:     if (!$courseid) { $courseid=(&Apache::lonxml::whichuser())[1]; }
 5037:     if ($courseid) {
 5038: 	return $env{"course.$courseid.rndseed"};
 5039:     }
 5040:     return &latest_rnd_algorithm_id();
 5041: }
 5042: 
 5043: sub validCODE {
 5044:     my ($CODE)=@_;
 5045:     if (defined($CODE) && $CODE ne '' && $CODE =~ /^\w+$/) { return 1; }
 5046:     return 0;
 5047: }
 5048: 
 5049: sub getCODE {
 5050:     if (&validCODE($env{'form.CODE'})) { return $env{'form.CODE'}; }
 5051:     if ( (defined($Apache::lonhomework::parsing_a_problem) ||
 5052: 	  defined($Apache::lonhomework::parsing_a_task) ) &&
 5053: 	 &validCODE($Apache::lonhomework::history{'resource.CODE'})) {
 5054: 	return $Apache::lonhomework::history{'resource.CODE'};
 5055:     }
 5056:     return undef;
 5057: }
 5058: 
 5059: sub rndseed {
 5060:     my ($symb,$courseid,$domain,$username)=@_;
 5061: 
 5062:     my ($wsymb,$wcourseid,$wdomain,$wusername)=&Apache::lonxml::whichuser();
 5063:     if (!$symb) {
 5064: 	unless ($symb=$wsymb) { return time; }
 5065:     }
 5066:     if (!$courseid) { $courseid=$wcourseid; }
 5067:     if (!$domain) { $domain=$wdomain; }
 5068:     if (!$username) { $username=$wusername }
 5069:     my $which=&get_rand_alg();
 5070:     if (defined(&getCODE())) {
 5071: 	if ($which eq '64bit4') {
 5072: 	    return &rndseed_CODE_64bit4($symb,$courseid,$domain,$username);
 5073: 	} else {
 5074: 	    return &rndseed_CODE_64bit($symb,$courseid,$domain,$username);
 5075: 	}
 5076:     } elsif ($which eq '64bit4') {
 5077: 	return &rndseed_64bit4($symb,$courseid,$domain,$username);
 5078:     } elsif ($which eq '64bit3') {
 5079: 	return &rndseed_64bit3($symb,$courseid,$domain,$username);
 5080:     } elsif ($which eq '64bit2') {
 5081: 	return &rndseed_64bit2($symb,$courseid,$domain,$username);
 5082:     } elsif ($which eq '64bit') {
 5083: 	return &rndseed_64bit($symb,$courseid,$domain,$username);
 5084:     }
 5085:     return &rndseed_32bit($symb,$courseid,$domain,$username);
 5086: }
 5087: 
 5088: sub rndseed_32bit {
 5089:     my ($symb,$courseid,$domain,$username)=@_;
 5090:     {
 5091: 	use integer;
 5092: 	my $symbchck=unpack("%32C*",$symb) << 27;
 5093: 	my $symbseed=numval($symb) << 22;
 5094: 	my $namechck=unpack("%32C*",$username) << 17;
 5095: 	my $nameseed=numval($username) << 12;
 5096: 	my $domainseed=unpack("%32C*",$domain) << 7;
 5097: 	my $courseseed=unpack("%32C*",$courseid);
 5098: 	my $num=$symbseed+$nameseed+$domainseed+$courseseed+$namechck+$symbchck;
 5099: 	#&Apache::lonxml::debug("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
 5100: 	#&Apache::lonxml::debug("rndseed :$num:$symb");
 5101: 	if ($_64bit) { $num=(($num<<32)>>32); }
 5102: 	return $num;
 5103:     }
 5104: }
 5105: 
 5106: sub rndseed_64bit {
 5107:     my ($symb,$courseid,$domain,$username)=@_;
 5108:     {
 5109: 	use integer;
 5110: 	my $symbchck=unpack("%32S*",$symb) << 21;
 5111: 	my $symbseed=numval($symb) << 10;
 5112: 	my $namechck=unpack("%32S*",$username);
 5113: 	
 5114: 	my $nameseed=numval($username) << 21;
 5115: 	my $domainseed=unpack("%32S*",$domain) << 10;
 5116: 	my $courseseed=unpack("%32S*",$courseid);
 5117: 	
 5118: 	my $num1=$symbchck+$symbseed+$namechck;
 5119: 	my $num2=$nameseed+$domainseed+$courseseed;
 5120: 	#&Apache::lonxml::debug("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
 5121: 	#&Apache::lonxml::debug("rndseed :$num:$symb");
 5122: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
 5123: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
 5124: 	return "$num1,$num2";
 5125:     }
 5126: }
 5127: 
 5128: sub rndseed_64bit2 {
 5129:     my ($symb,$courseid,$domain,$username)=@_;
 5130:     {
 5131: 	use integer;
 5132: 	# strings need to be an even # of cahracters long, it it is odd the
 5133:         # last characters gets thrown away
 5134: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
 5135: 	my $symbseed=numval($symb) << 10;
 5136: 	my $namechck=unpack("%32S*",$username.' ');
 5137: 	
 5138: 	my $nameseed=numval($username) << 21;
 5139: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
 5140: 	my $courseseed=unpack("%32S*",$courseid.' ');
 5141: 	
 5142: 	my $num1=$symbchck+$symbseed+$namechck;
 5143: 	my $num2=$nameseed+$domainseed+$courseseed;
 5144: 	#&Apache::lonxml::debug("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
 5145: 	#&Apache::lonxml::debug("rndseed :$num:$symb");
 5146: 	return "$num1,$num2";
 5147:     }
 5148: }
 5149: 
 5150: sub rndseed_64bit3 {
 5151:     my ($symb,$courseid,$domain,$username)=@_;
 5152:     {
 5153: 	use integer;
 5154: 	# strings need to be an even # of cahracters long, it it is odd the
 5155:         # last characters gets thrown away
 5156: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
 5157: 	my $symbseed=numval2($symb) << 10;
 5158: 	my $namechck=unpack("%32S*",$username.' ');
 5159: 	
 5160: 	my $nameseed=numval2($username) << 21;
 5161: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
 5162: 	my $courseseed=unpack("%32S*",$courseid.' ');
 5163: 	
 5164: 	my $num1=$symbchck+$symbseed+$namechck;
 5165: 	my $num2=$nameseed+$domainseed+$courseseed;
 5166: 	#&Apache::lonxml::debug("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
 5167: 	#&Apache::lonxml::debug("rndseed :$num1:$num2:$_64bit");
 5168: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
 5169: 	
 5170: 	return "$num1:$num2";
 5171:     }
 5172: }
 5173: 
 5174: sub rndseed_64bit4 {
 5175:     my ($symb,$courseid,$domain,$username)=@_;
 5176:     {
 5177: 	use integer;
 5178: 	# strings need to be an even # of cahracters long, it it is odd the
 5179:         # last characters gets thrown away
 5180: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
 5181: 	my $symbseed=numval3($symb) << 10;
 5182: 	my $namechck=unpack("%32S*",$username.' ');
 5183: 	
 5184: 	my $nameseed=numval3($username) << 21;
 5185: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
 5186: 	my $courseseed=unpack("%32S*",$courseid.' ');
 5187: 	
 5188: 	my $num1=$symbchck+$symbseed+$namechck;
 5189: 	my $num2=$nameseed+$domainseed+$courseseed;
 5190: 	#&Apache::lonxml::debug("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
 5191: 	#&Apache::lonxml::debug("rndseed :$num1:$num2:$_64bit");
 5192: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
 5193: 	
 5194: 	return "$num1:$num2";
 5195:     }
 5196: }
 5197: 
 5198: sub rndseed_CODE_64bit {
 5199:     my ($symb,$courseid,$domain,$username)=@_;
 5200:     {
 5201: 	use integer;
 5202: 	my $symbchck=unpack("%32S*",$symb.' ') << 16;
 5203: 	my $symbseed=numval2($symb);
 5204: 	my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
 5205: 	my $CODEseed=numval(&getCODE());
 5206: 	my $courseseed=unpack("%32S*",$courseid.' ');
 5207: 	my $num1=$symbseed+$CODEchck;
 5208: 	my $num2=$CODEseed+$courseseed+$symbchck;
 5209: 	#&Apache::lonxml::debug("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
 5210: 	#&Apache::lonxml::debug("rndseed :$num1:$num2:$symb");
 5211: 	if ($_64bit) { $num1=(($num1<<32)>>32); }
 5212: 	if ($_64bit) { $num2=(($num2<<32)>>32); }
 5213: 	return "$num1:$num2";
 5214:     }
 5215: }
 5216: 
 5217: sub rndseed_CODE_64bit4 {
 5218:     my ($symb,$courseid,$domain,$username)=@_;
 5219:     {
 5220: 	use integer;
 5221: 	my $symbchck=unpack("%32S*",$symb.' ') << 16;
 5222: 	my $symbseed=numval3($symb);
 5223: 	my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
 5224: 	my $CODEseed=numval3(&getCODE());
 5225: 	my $courseseed=unpack("%32S*",$courseid.' ');
 5226: 	my $num1=$symbseed+$CODEchck;
 5227: 	my $num2=$CODEseed+$courseseed+$symbchck;
 5228: 	#&Apache::lonxml::debug("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
 5229: 	#&Apache::lonxml::debug("rndseed :$num1:$num2:$symb");
 5230: 	if ($_64bit) { $num1=(($num1<<32)>>32); }
 5231: 	if ($_64bit) { $num2=(($num2<<32)>>32); }
 5232: 	return "$num1:$num2";
 5233:     }
 5234: }
 5235: 
 5236: sub setup_random_from_rndseed {
 5237:     my ($rndseed)=@_;
 5238:     if ($rndseed =~/([,:])/) {
 5239: 	my ($num1,$num2)=split(/[,:]/,$rndseed);
 5240: 	&Math::Random::random_set_seed(abs($num1),abs($num2));
 5241:     } else {
 5242: 	&Math::Random::random_set_seed_from_phrase($rndseed);
 5243:     }
 5244: }
 5245: 
 5246: sub latest_receipt_algorithm_id {
 5247:     return 'receipt2';
 5248: }
 5249: 
 5250: sub recunique {
 5251:     my $fucourseid=shift;
 5252:     my $unique;
 5253:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2') {
 5254: 	$unique=$env{"course.$fucourseid.internal.encseed"};
 5255:     } else {
 5256: 	$unique=$perlvar{'lonReceipt'};
 5257:     }
 5258:     return unpack("%32C*",$unique);
 5259: }
 5260: 
 5261: sub recprefix {
 5262:     my $fucourseid=shift;
 5263:     my $prefix;
 5264:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2') {
 5265: 	$prefix=$env{"course.$fucourseid.internal.encpref"};
 5266:     } else {
 5267: 	$prefix=$perlvar{'lonHostID'};
 5268:     }
 5269:     return unpack("%32C*",$prefix);
 5270: }
 5271: 
 5272: sub ireceipt {
 5273:     my ($funame,$fudom,$fucourseid,$fusymb,$part)=@_;
 5274:     my $cuname=unpack("%32C*",$funame);
 5275:     my $cudom=unpack("%32C*",$fudom);
 5276:     my $cucourseid=unpack("%32C*",$fucourseid);
 5277:     my $cusymb=unpack("%32C*",$fusymb);
 5278:     my $cunique=&recunique($fucourseid);
 5279:     my $cpart=unpack("%32S*",$part);
 5280:     my $return =&recprefix($fucourseid).'-';
 5281:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2' ||
 5282: 	$env{'request.state'} eq 'construct') {
 5283: 	&Apache::lonxml::debug("doing receipt2  using parts $cpart, uname $cuname and udom $cudom gets  ".($cpart%$cuname).
 5284: 			       " and ".($cpart%$cudom));
 5285: 			       
 5286: 	$return.= ($cunique%$cuname+
 5287: 		   $cunique%$cudom+
 5288: 		   $cusymb%$cuname+
 5289: 		   $cusymb%$cudom+
 5290: 		   $cucourseid%$cuname+
 5291: 		   $cucourseid%$cudom+
 5292: 		   $cpart%$cuname+
 5293: 		   $cpart%$cudom);
 5294:     } else {
 5295: 	$return.= ($cunique%$cuname+
 5296: 		   $cunique%$cudom+
 5297: 		   $cusymb%$cuname+
 5298: 		   $cusymb%$cudom+
 5299: 		   $cucourseid%$cuname+
 5300: 		   $cucourseid%$cudom);
 5301:     }
 5302:     return $return;
 5303: }
 5304: 
 5305: sub receipt {
 5306:     my ($part)=@_;
 5307:     my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
 5308:     return &ireceipt($name,$domain,$courseid,$symb,$part);
 5309: }
 5310: 
 5311: # ------------------------------------------------------------ Serves up a file
 5312: # returns either the contents of the file or 
 5313: # -1 if the file doesn't exist
 5314: #
 5315: # if the target is a file that was uploaded via DOCS, 
 5316: # a check will be made to see if a current copy exists on the local server,
 5317: # if it does this will be served, otherwise a copy will be retrieved from
 5318: # the home server for the course and stored in /home/httpd/html/userfiles on
 5319: # the local server.   
 5320: 
 5321: sub getfile {
 5322:     my ($file) = @_;
 5323:     if ($file =~ m -^/*(uploaded|editupload)/-) { $file=&filelocation("",$file); }
 5324:     &repcopy($file);
 5325:     return &readfile($file);
 5326: }
 5327: 
 5328: sub repcopy_userfile {
 5329:     my ($file)=@_;
 5330:     if ($file =~ m -^/*(uploaded|editupload)/-) { $file=&filelocation("",$file); }
 5331:     if ($file =~ m|^/home/httpd/html/lonUsers/|) { return 'ok'; }
 5332:     my ($cdom,$cnum,$filename) = 
 5333: 	($file=~m|^\Q$perlvar{'lonDocRoot'}\E/+userfiles/+([^/]+)/+([^/]+)/+(.*)|);
 5334:     my ($info,$rtncode);
 5335:     my $uri="/uploaded/$cdom/$cnum/$filename";
 5336:     if (-e "$file") {
 5337: 	my @fileinfo = stat($file);
 5338: 	my $lwpresp = &getuploaded('HEAD',$uri,$cdom,$cnum,\$info,\$rtncode);
 5339: 	if ($lwpresp ne 'ok') {
 5340: 	    if ($rtncode eq '404') {
 5341: 		unlink($file);
 5342: 	    }
 5343: 	    #my $ua=new LWP::UserAgent;
 5344: 	    #my $request=new HTTP::Request('GET',&tokenwrapper($uri));
 5345: 	    #my $response=$ua->request($request);
 5346: 	    #if ($response->is_success()) {
 5347: 	#	return $response->content;
 5348: 	#    } else {
 5349: 	#	return -1;
 5350: 	#    }
 5351: 	    return -1;
 5352: 	}
 5353: 	if ($info < $fileinfo[9]) {
 5354: 	    return 'ok';
 5355: 	}
 5356: 	$info = '';
 5357: 	$lwpresp = &getuploaded('GET',$uri,$cdom,$cnum,\$info,\$rtncode);
 5358: 	if ($lwpresp ne 'ok') {
 5359: 	    return -1;
 5360: 	}
 5361:     } else {
 5362: 	my $lwpresp = &getuploaded('GET',$uri,$cdom,$cnum,\$info,\$rtncode);
 5363: 	if ($lwpresp ne 'ok') {
 5364: 	    my $ua=new LWP::UserAgent;
 5365: 	    my $request=new HTTP::Request('GET',&tokenwrapper($uri));
 5366: 	    my $response=$ua->request($request);
 5367: 	    if ($response->is_success()) {
 5368: 		$info=$response->content;
 5369: 	    } else {
 5370: 		return -1;
 5371: 	    }
 5372: 	}
 5373: 	my @parts = ($cdom,$cnum); 
 5374: 	if ($filename =~ m|^(.+)/[^/]+$|) {
 5375: 	    push @parts, split(/\//,$1);
 5376: 	}
 5377: 	my $path = $perlvar{'lonDocRoot'}.'/userfiles';
 5378: 	foreach my $part (@parts) {
 5379: 	    $path .= '/'.$part;
 5380: 	    if (!-e $path) {
 5381: 		mkdir($path,0770);
 5382: 	    }
 5383: 	}
 5384:     }
 5385:     open(FILE,">$file");
 5386:     print FILE $info;
 5387:     close(FILE);
 5388:     return 'ok';
 5389: }
 5390: 
 5391: sub tokenwrapper {
 5392:     my $uri=shift;
 5393:     $uri=~s|^http\://([^/]+)||;
 5394:     $uri=~s|^/||;
 5395:     $env{'user.environment'}=~/\/([^\/]+)\.id/;
 5396:     my $token=$1;
 5397:     my (undef,$udom,$uname,$file)=split('/',$uri,4);
 5398:     if ($udom && $uname && $file) {
 5399: 	$file=~s|(\?\.*)*$||;
 5400:         &appenv("userfile.$udom/$uname/$file" => $env{'request.course.id'});
 5401:         return 'http://'.$hostname{ &homeserver($uname,$udom)}.'/'.$uri.
 5402:                (($uri=~/\?/)?'&':'?').'token='.$token.
 5403:                                '&tokenissued='.$perlvar{'lonHostID'};
 5404:     } else {
 5405:         return '/adm/notfound.html';
 5406:     }
 5407: }
 5408: 
 5409: sub getuploaded {
 5410:     my ($reqtype,$uri,$cdom,$cnum,$info,$rtncode) = @_;
 5411:     $uri=~s/^\///;
 5412:     $uri = 'http://'.$hostname{ &homeserver($cnum,$cdom)}.'/raw/'.$uri;
 5413:     my $ua=new LWP::UserAgent;
 5414:     my $request=new HTTP::Request($reqtype,$uri);
 5415:     my $response=$ua->request($request);
 5416:     $$rtncode = $response->code;
 5417:     if (! $response->is_success()) {
 5418: 	return 'failed';
 5419:     }      
 5420:     if ($reqtype eq 'HEAD') {
 5421: 	$$info = &HTTP::Date::str2time( $response->header('Last-modified') );
 5422:     } elsif ($reqtype eq 'GET') {
 5423: 	$$info = $response->content;
 5424:     }
 5425:     return 'ok';
 5426: }
 5427: 
 5428: sub readfile {
 5429:     my $file = shift;
 5430:     if ( (! -e $file ) || ($file eq '') ) { return -1; };
 5431:     my $fh;
 5432:     open($fh,"<$file");
 5433:     my $a='';
 5434:     while (<$fh>) { $a .=$_; }
 5435:     return $a;
 5436: }
 5437: 
 5438: sub filelocation {
 5439:     my ($dir,$file) = @_;
 5440:     my $location;
 5441:     $file=~ s/^\s*(\S+)\s*$/$1/; ## strip off leading and trailing spaces
 5442:     if ($file=~m:^/~:) { # is a contruction space reference
 5443:         $location = $file;
 5444:         $location =~ s:/~(.*?)/(.*):/home/$1/public_html/$2:;
 5445:     } elsif ($file=~/^\/*(uploaded|editupload)/) { # is an uploaded file
 5446:         my ($udom,$uname,$filename)=
 5447:   	    ($file=~m -^/+(?:uploaded|editupload)/+([^/]+)/+([^/]+)/+(.*)$-);
 5448:         my $home=&homeserver($uname,$udom);
 5449:         my $is_me=0;
 5450:         my @ids=&current_machine_ids();
 5451:         foreach my $id (@ids) { if ($id eq $home) { $is_me=1; } }
 5452:         if ($is_me) {
 5453:   	    $location=&Apache::loncommon::propath($udom,$uname).
 5454:   	      '/userfiles/'.$filename;
 5455:         } else {
 5456:   	  $location=$Apache::lonnet::perlvar{'lonDocRoot'}.'/userfiles/'.
 5457:   	      $udom.'/'.$uname.'/'.$filename;
 5458:         }
 5459:     } else {
 5460:         $file=~s/^\Q$perlvar{'lonDocRoot'}\E//;
 5461:         $file=~s:^/res/:/:;
 5462:         if ( !( $file =~ m:^/:) ) {
 5463:             $location = $dir. '/'.$file;
 5464:         } else {
 5465:             $location = '/home/httpd/html/res'.$file;
 5466:         }
 5467:     }
 5468:     $location=~s://+:/:g; # remove duplicate /
 5469:     while ($location=~m:/\.\./:) {$location=~ s:/[^/]+/\.\./:/:g;} #remove dir/..
 5470:     while ($location=~m:/\./:) {$location=~ s:/\./:/:g;} #remove /./
 5471:     return $location;
 5472: }
 5473: 
 5474: sub hreflocation {
 5475:     my ($dir,$file)=@_;
 5476:     unless (($file=~m-^http://-i) || ($file=~m-^/-)) {
 5477: 	my $finalpath=filelocation($dir,$file);
 5478: 	$finalpath=~s-^/home/httpd/html--;
 5479: 	$finalpath=~s-^/home/(\w+)/public_html/-/~$1/-;
 5480: 	return $finalpath;
 5481:     } elsif ($file=~m-^/home-) {
 5482: 	$file=~s-^/home/httpd/html--;
 5483: 	$file=~s-^/home/(\w+)/public_html/-/~$1/-;
 5484: 	return $file;
 5485:     }
 5486:     return $file;
 5487: }
 5488: 
 5489: sub current_machine_domains {
 5490:     my $hostname=$hostname{$perlvar{'lonHostID'}};
 5491:     my @domains;
 5492:     while( my($id, $name) = each(%hostname)) {
 5493: #	&logthis("-$id-$name-$hostname-");
 5494: 	if ($hostname eq $name) {
 5495: 	    push(@domains,$hostdom{$id});
 5496: 	}
 5497:     }
 5498:     return @domains;
 5499: }
 5500: 
 5501: sub current_machine_ids {
 5502:     my $hostname=$hostname{$perlvar{'lonHostID'}};
 5503:     my @ids;
 5504:     while( my($id, $name) = each(%hostname)) {
 5505: #	&logthis("-$id-$name-$hostname-");
 5506: 	if ($hostname eq $name) {
 5507: 	    push(@ids,$id);
 5508: 	}
 5509:     }
 5510:     return @ids;
 5511: }
 5512: 
 5513: # ------------------------------------------------------------- Declutters URLs
 5514: 
 5515: sub declutter {
 5516:     my $thisfn=shift;
 5517:     if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
 5518:     $thisfn=~s/^\Q$perlvar{'lonDocRoot'}\E//;
 5519:     $thisfn=~s/^\///;
 5520:     $thisfn=~s/^res\///;
 5521:     $thisfn=~s/\?.+$//;
 5522:     return $thisfn;
 5523: }
 5524: 
 5525: # ------------------------------------------------------------- Clutter up URLs
 5526: 
 5527: sub clutter {
 5528:     my $thisfn='/'.&declutter(shift);
 5529:     unless ($thisfn=~/^\/(uploaded|editupload|adm|userfiles|ext|raw|priv|public)\//) { 
 5530:        $thisfn='/res'.$thisfn; 
 5531:     }
 5532:     return $thisfn;
 5533: }
 5534: 
 5535: sub freeze_escape {
 5536:     my ($value)=@_;
 5537:     if (ref($value)) {
 5538: 	$value=&nfreeze($value);
 5539: 	return '__FROZEN__'.&escape($value);
 5540:     }
 5541:     return &escape($value);
 5542: }
 5543: 
 5544: # -------------------------------------------------------- Escape Special Chars
 5545: 
 5546: sub escape {
 5547:     my $str=shift;
 5548:     $str =~ s/(\W)/"%".unpack('H2',$1)/eg;
 5549:     return $str;
 5550: }
 5551: 
 5552: # ----------------------------------------------------- Un-Escape Special Chars
 5553: 
 5554: sub unescape {
 5555:     my $str=shift;
 5556:     $str =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
 5557:     return $str;
 5558: }
 5559: 
 5560: sub thaw_unescape {
 5561:     my ($value)=@_;
 5562:     if ($value =~ /^__FROZEN__/) {
 5563: 	substr($value,0,10,undef);
 5564: 	$value=&unescape($value);
 5565: 	return &thaw($value);
 5566:     }
 5567:     return &unescape($value);
 5568: }
 5569: 
 5570: sub mod_perl_version {
 5571:     return 1;
 5572:     if (defined($perlvar{'MODPERL2'})) {
 5573: 	return 2;
 5574:     }
 5575: }
 5576: 
 5577: sub correct_line_ends {
 5578:     my ($result)=@_;
 5579:     $$result =~s/\r\n/\n/mg;
 5580:     $$result =~s/\r/\n/mg;
 5581: }
 5582: # ================================================================ Main Program
 5583: 
 5584: sub goodbye {
 5585:    &logthis("Starting Shut down");
 5586: #not converted to using infrastruture and probably shouldn't be
 5587:    &logthis(sprintf("%-20s is %s",'%badServerCache',length(&freeze(\%badServerCache))));
 5588: #converted
 5589: #   &logthis(sprintf("%-20s is %s",'%metacache',scalar(%metacache)));
 5590:    &logthis(sprintf("%-20s is %s",'%homecache',length(&freeze(\%homecache))));
 5591: #   &logthis(sprintf("%-20s is %s",'%titlecache',length(&freeze(\%titlecache))));
 5592: #   &logthis(sprintf("%-20s is %s",'%courseresdatacache',length(&freeze(\%courseresdatacache))));
 5593: #1.1 only
 5594: #   &logthis(sprintf("%-20s is %s",'%userresdatacache',length(&freeze(\%userresdatacache))));
 5595: #   &logthis(sprintf("%-20s is %s",'%getsectioncache',length(&freeze(\%getsectioncache))));
 5596: #   &logthis(sprintf("%-20s is %s",'%courseresversioncache',length(&freeze(\%courseresversioncache))));
 5597: #   &logthis(sprintf("%-20s is %s",'%resversioncache',length(&freeze(\%resversioncache))));
 5598:    &logthis(sprintf("%-20s is %s",'%remembered',length(&freeze(\%remembered))));
 5599:    &logthis(sprintf("%-20s is %s",'kicks',$kicks));
 5600:    &logthis(sprintf("%-20s is %s",'hits',$hits));
 5601:    &flushcourselogs();
 5602:    &logthis("Shutting down");
 5603:    return DONE;
 5604: }
 5605: 
 5606: BEGIN {
 5607: # ----------------------------------- Read loncapa.conf and loncapa_apache.conf
 5608:     unless ($readit) {
 5609: {
 5610:     # FIXME: Use LONCAPA::Configuration::read_conf here and omit next block
 5611:     open(my $config,"</etc/httpd/conf/loncapa.conf");
 5612: 
 5613:     while (my $configline=<$config>) {
 5614:         if ($configline=~/\S/ && $configline =~ /^[^\#]*PerlSetVar/) {
 5615: 	   my ($dummy,$varname,$varvalue)=split(/\s+/,$configline);
 5616:            chomp($varvalue);
 5617:            $perlvar{$varname}=$varvalue;
 5618:         }
 5619:     }
 5620:     close($config);
 5621: }
 5622: {
 5623:     open(my $config,"</etc/httpd/conf/loncapa_apache.conf");
 5624: 
 5625:     while (my $configline=<$config>) {
 5626:         if ($configline =~ /^[^\#]*PerlSetVar/) {
 5627: 	   my ($dummy,$varname,$varvalue)=split(/\s+/,$configline);
 5628:            chomp($varvalue);
 5629:            $perlvar{$varname}=$varvalue;
 5630:         }
 5631:     }
 5632:     close($config);
 5633: }
 5634: 
 5635: # ------------------------------------------------------------ Read domain file
 5636: {
 5637:     %domaindescription = ();
 5638:     %domain_auth_def = ();
 5639:     %domain_auth_arg_def = ();
 5640:     my $fh;
 5641:     if (open($fh,"<".$Apache::lonnet::perlvar{'lonTabDir'}.'/domain.tab')) {
 5642:        while (<$fh>) {
 5643:            next if (/^(\#|\s*$)/);
 5644: #           next if /^\#/;
 5645:            chomp;
 5646:            my ($domain, $domain_description, $def_auth, $def_auth_arg,
 5647: 	       $def_lang, $city, $longi, $lati) = split(/:/,$_);
 5648: 	   $domain_auth_def{$domain}=$def_auth;
 5649:            $domain_auth_arg_def{$domain}=$def_auth_arg;
 5650: 	   $domaindescription{$domain}=$domain_description;
 5651: 	   $domain_lang_def{$domain}=$def_lang;
 5652: 	   $domain_city{$domain}=$city;
 5653: 	   $domain_longi{$domain}=$longi;
 5654: 	   $domain_lati{$domain}=$lati;
 5655: 
 5656:  #         &logthis("Domain.tab: $domain, $domain_auth_def{$domain}, $domain_auth_arg_def{$domain},$domaindescription{$domain}");
 5657: #          &logthis("Domain.tab: $domain ".$domaindescription{$domain} );
 5658: 	}
 5659:     }
 5660:     close ($fh);
 5661: }
 5662: 
 5663: 
 5664: # ------------------------------------------------------------- Read hosts file
 5665: {
 5666:     open(my $config,"<$perlvar{'lonTabDir'}/hosts.tab");
 5667: 
 5668:     while (my $configline=<$config>) {
 5669:        next if ($configline =~ /^(\#|\s*$)/);
 5670:        chomp($configline);
 5671:        my ($id,$domain,$role,$name)=split(/:/,$configline);
 5672:        $name=~s/\s//g;
 5673:        if ($id && $domain && $role && $name) {
 5674: 	 $hostname{$id}=$name;
 5675: 	 $hostdom{$id}=$domain;
 5676: 	 if ($role eq 'library') { $libserv{$id}=$name; }
 5677:        }
 5678:     }
 5679:     close($config);
 5680:     # FIXME: dev server don't want this, production servers _do_ want this
 5681:     #&get_iphost();
 5682: }
 5683: 
 5684: sub get_iphost {
 5685:     if (%iphost) { return %iphost; }
 5686:     foreach my $id (keys(%hostname)) {
 5687: 	my $name=$hostname{$id};
 5688: 	my $ip = gethostbyname($name);
 5689: 	if (!$ip || length($ip) ne 4) {
 5690: 	    &logthis("Skipping host $id name $name no IP found\n");
 5691: 	    next;
 5692: 	}
 5693: 	$ip=inet_ntoa($ip);
 5694: 	push(@{$iphost{$ip}},$id);
 5695:     }
 5696:     return %iphost;
 5697: }
 5698: 
 5699: # ------------------------------------------------------ Read spare server file
 5700: {
 5701:     open(my $config,"<$perlvar{'lonTabDir'}/spare.tab");
 5702: 
 5703:     while (my $configline=<$config>) {
 5704:        chomp($configline);
 5705:        if ($configline) {
 5706:           $spareid{$configline}=1;
 5707:        }
 5708:     }
 5709:     close($config);
 5710: }
 5711: # ------------------------------------------------------------ Read permissions
 5712: {
 5713:     open(my $config,"<$perlvar{'lonTabDir'}/roles.tab");
 5714: 
 5715:     while (my $configline=<$config>) {
 5716: 	chomp($configline);
 5717: 	if ($configline) {
 5718: 	    my ($role,$perm)=split(/ /,$configline);
 5719: 	    if ($perm ne '') { $pr{$role}=$perm; }
 5720: 	}
 5721:     }
 5722:     close($config);
 5723: }
 5724: 
 5725: # -------------------------------------------- Read plain texts for permissions
 5726: {
 5727:     open(my $config,"<$perlvar{'lonTabDir'}/rolesplain.tab");
 5728: 
 5729:     while (my $configline=<$config>) {
 5730: 	chomp($configline);
 5731: 	if ($configline) {
 5732: 	    my ($short,$plain)=split(/:/,$configline);
 5733: 	    if ($plain ne '') { $prp{$short}=$plain; }
 5734: 	}
 5735:     }
 5736:     close($config);
 5737: }
 5738: 
 5739: # ---------------------------------------------------------- Read package table
 5740: {
 5741:     open(my $config,"<$perlvar{'lonTabDir'}/packages.tab");
 5742: 
 5743:     while (my $configline=<$config>) {
 5744: 	if ($configline !~ /\S/ || $configline=~/^#/) { next; }
 5745: 	chomp($configline);
 5746: 	my ($short,$plain)=split(/:/,$configline);
 5747: 	my ($pack,$name)=split(/\&/,$short);
 5748: 	if ($plain ne '') {
 5749: 	    $packagetab{$pack.'&'.$name.'&name'}=$name; 
 5750: 	    $packagetab{$short}=$plain; 
 5751: 	}
 5752:     }
 5753:     close($config);
 5754: }
 5755: 
 5756: # ------------- set up temporary directory
 5757: {
 5758:     $tmpdir = $perlvar{'lonDaemons'}.'/tmp/';
 5759: 
 5760: }
 5761: 
 5762: $memcache=new Cache::Memcached({'servers'=>['127.0.0.1:11211']});
 5763: 
 5764: $processmarker='_'.time.'_'.$perlvar{'lonHostID'};
 5765: $dumpcount=0;
 5766: 
 5767: &logtouch();
 5768: &logthis('<font color=yellow>INFO: Read configuration</font>');
 5769: $readit=1;
 5770:     {
 5771: 	use integer;
 5772: 	my $test=(2**32)+1;
 5773: 	if ($test != 0) { $_64bit=1; } else { $_64bit=0; }
 5774: 	&logthis(" Detected 64bit platform ($_64bit)");
 5775:     }
 5776: }
 5777: }
 5778: 
 5779: 1;
 5780: __END__
 5781: 
 5782: =pod
 5783: 
 5784: =head1 NAME
 5785: 
 5786: Apache::lonnet - Subroutines to ask questions about things in the network.
 5787: 
 5788: =head1 SYNOPSIS
 5789: 
 5790: Invoked by other LON-CAPA modules, when they need to talk to or about objects in the network.
 5791: 
 5792:  &Apache::lonnet::SUBROUTINENAME(ARGUMENTS);
 5793: 
 5794: Common parameters:
 5795: 
 5796: =over 4
 5797: 
 5798: =item *
 5799: 
 5800: $uname : an internal username (if $cname expecting a course Id specifically)
 5801: 
 5802: =item *
 5803: 
 5804: $udom : a domain (if $cdom expecting a course's domain specifically)
 5805: 
 5806: =item *
 5807: 
 5808: $symb : a resource instance identifier
 5809: 
 5810: =item *
 5811: 
 5812: $namespace : the name of a .db file that contains the data needed or
 5813: being set.
 5814: 
 5815: =back
 5816: 
 5817: =head1 OVERVIEW
 5818: 
 5819: lonnet provides subroutines which interact with the
 5820: lonc/lond (TCP) network layer of LON-CAPA. They can be used to ask
 5821: about classes, users, and resources.
 5822: 
 5823: For many of these objects you can also use this to store data about
 5824: them or modify them in various ways.
 5825: 
 5826: =head2 Symbs
 5827: 
 5828: To identify a specific instance of a resource, LON-CAPA uses symbols
 5829: or "symbs"X<symb>. These identifiers are built from the URL of the
 5830: map, the resource number of the resource in the map, and the URL of
 5831: the resource itself. The latter is somewhat redundant, but might help
 5832: if maps change.
 5833: 
 5834: An example is
 5835: 
 5836:  msu/korte/parts/part1.sequence___19___msu/korte/tests/part12.problem
 5837: 
 5838: The respective map entry is
 5839: 
 5840:  <resource id="19" src="/res/msu/korte/tests/part12.problem"
 5841:   title="Problem 2">
 5842:  </resource>
 5843: 
 5844: Symbs are used by the random number generator, as well as to store and
 5845: restore data specific to a certain instance of for example a problem.
 5846: 
 5847: =head2 Storing And Retrieving Data
 5848: 
 5849: X<store()>X<cstore()>X<restore()>Three of the most important functions
 5850: in C<lonnet.pm> are C<&Apache::lonnet::cstore()>,
 5851: C<&Apache::lonnet:restore()>, and C<&Apache::lonnet::store()>, which
 5852: is is the non-critical message twin of cstore. These functions are for
 5853: handlers to store a perl hash to a user's permanent data space in an
 5854: easy manner, and to retrieve it again on another call. It is expected
 5855: that a handler would use this once at the beginning to retrieve data,
 5856: and then again once at the end to send only the new data back.
 5857: 
 5858: The data is stored in the user's data directory on the user's
 5859: homeserver under the ID of the course.
 5860: 
 5861: The hash that is returned by restore will have all of the previous
 5862: value for all of the elements of the hash.
 5863: 
 5864: Example:
 5865: 
 5866:  #creating a hash
 5867:  my %hash;
 5868:  $hash{'foo'}='bar';
 5869: 
 5870:  #storing it
 5871:  &Apache::lonnet::cstore(\%hash);
 5872: 
 5873:  #changing a value
 5874:  $hash{'foo'}='notbar';
 5875: 
 5876:  #adding a new value
 5877:  $hash{'bar'}='foo';
 5878:  &Apache::lonnet::cstore(\%hash);
 5879: 
 5880:  #retrieving the hash
 5881:  my %history=&Apache::lonnet::restore();
 5882: 
 5883:  #print the hash
 5884:  foreach my $key (sort(keys(%history))) {
 5885:    print("\%history{$key} = $history{$key}");
 5886:  }
 5887: 
 5888: Will print out:
 5889: 
 5890:  %history{1:foo} = bar
 5891:  %history{1:keys} = foo:timestamp
 5892:  %history{1:timestamp} = 990455579
 5893:  %history{2:bar} = foo
 5894:  %history{2:foo} = notbar
 5895:  %history{2:keys} = foo:bar:timestamp
 5896:  %history{2:timestamp} = 990455580
 5897:  %history{bar} = foo
 5898:  %history{foo} = notbar
 5899:  %history{timestamp} = 990455580
 5900:  %history{version} = 2
 5901: 
 5902: Note that the special hash entries C<keys>, C<version> and
 5903: C<timestamp> were added to the hash. C<version> will be equal to the
 5904: total number of versions of the data that have been stored. The
 5905: C<timestamp> attribute will be the UNIX time the hash was
 5906: stored. C<keys> is available in every historical section to list which
 5907: keys were added or changed at a specific historical revision of a
 5908: hash.
 5909: 
 5910: B<Warning>: do not store the hash that restore returns directly. This
 5911: will cause a mess since it will restore the historical keys as if the
 5912: were new keys. I.E. 1:foo will become 1:1:foo etc.
 5913: 
 5914: Calling convention:
 5915: 
 5916:  my %record=&Apache::lonnet::restore($symb,$courseid,$domain,$uname,$home);
 5917:  &Apache::lonnet::cstore(\%newrecord,$symb,$courseid,$domain,$uname,$home);
 5918: 
 5919: For more detailed information, see lonnet specific documentation.
 5920: 
 5921: =head1 RETURN MESSAGES
 5922: 
 5923: =over 4
 5924: 
 5925: =item * B<con_lost>: unable to contact remote host
 5926: 
 5927: =item * B<con_delayed>: unable to contact remote host, message will be delivered
 5928: when the connection is brought back up
 5929: 
 5930: =item * B<con_failed>: unable to contact remote host and unable to save message
 5931: for later delivery
 5932: 
 5933: =item * B<error:>: an error a occured, a description of the error follows the :
 5934: 
 5935: =item * B<no_such_host>: unable to fund a host associated with the user/domain
 5936: that was requested
 5937: 
 5938: =back
 5939: 
 5940: =head1 PUBLIC SUBROUTINES
 5941: 
 5942: =head2 Session Environment Functions
 5943: 
 5944: =over 4
 5945: 
 5946: =item * 
 5947: X<appenv()>
 5948: B<appenv(%hash)>: the value of %hash is written to
 5949: the user envirnoment file, and will be restored for each access this
 5950: user makes during this session, also modifies the %env for the current
 5951: process
 5952: 
 5953: =item *
 5954: X<delenv()>
 5955: B<delenv($regexp)>: removes all items from the session
 5956: environment file that matches the regular expression in $regexp. The
 5957: values are also delted from the current processes %env.
 5958: 
 5959: =back
 5960: 
 5961: =head2 User Information
 5962: 
 5963: =over 4
 5964: 
 5965: =item *
 5966: X<queryauthenticate()>
 5967: B<queryauthenticate($uname,$udom)>: try to determine user's current 
 5968: authentication scheme
 5969: 
 5970: =item *
 5971: X<authenticate()>
 5972: B<authenticate($uname,$upass,$udom)>: try to
 5973: authenticate user from domain's lib servers (first use the current
 5974: one). C<$upass> should be the users password.
 5975: 
 5976: =item *
 5977: X<homeserver()>
 5978: B<homeserver($uname,$udom)>: find the server which has
 5979: the user's directory and files (there must be only one), this caches
 5980: the answer, and also caches if there is a borken connection.
 5981: 
 5982: =item *
 5983: X<idget()>
 5984: B<idget($udom,@ids)>: find the usernames behind a list of IDs
 5985: (IDs are a unique resource in a domain, there must be only 1 ID per
 5986: username, and only 1 username per ID in a specific domain) (returns
 5987: hash: id=>name,id=>name)
 5988: 
 5989: =item *
 5990: X<idrget()>
 5991: B<idrget($udom,@unames)>: find the IDs behind a list of
 5992: usernames (returns hash: name=>id,name=>id)
 5993: 
 5994: =item *
 5995: X<idput()>
 5996: B<idput($udom,%ids)>: store away a list of names and associated IDs
 5997: 
 5998: =item *
 5999: X<rolesinit()>
 6000: B<rolesinit($udom,$username,$authhost)>: get user privileges
 6001: 
 6002: =item *
 6003: X<getsection()>
 6004: B<getsection($udom,$uname,$cname)>: finds the section of student in the
 6005: course $cname, return section name/number or '' for "not in course"
 6006: and '-1' for "no section"
 6007: 
 6008: =item *
 6009: X<userenvironment()>
 6010: B<userenvironment($udom,$uname,@what)>: gets the values of the keys
 6011: passed in @what from the requested user's environment, returns a hash
 6012: 
 6013: =back
 6014: 
 6015: =head2 User Roles
 6016: 
 6017: =over 4
 6018: 
 6019: =item *
 6020: 
 6021: allowed($priv,$uri) : check for a user privilege; returns codes for allowed
 6022: actions
 6023:  F: full access
 6024:  U,I,K: authentication modes (cxx only)
 6025:  '': forbidden
 6026:  1: user needs to choose course
 6027:  2: browse allowed
 6028: 
 6029: =item *
 6030: 
 6031: definerole($rolename,$sysrole,$domrole,$courole) : define role; define a custom
 6032: role rolename set privileges in format of lonTabs/roles.tab for system, domain,
 6033: and course level
 6034: 
 6035: =item *
 6036: 
 6037: plaintext($short) : return value in %prp hash (rolesplain.tab); plain text
 6038: explanation of a user role term
 6039: 
 6040: =back
 6041: 
 6042: =head2 User Modification
 6043: 
 6044: =over 4
 6045: 
 6046: =item *
 6047: 
 6048: assignrole($udom,$uname,$url,$role,$end,$start) : assign role; give a role to a
 6049: user for the level given by URL.  Optional start and end dates (leave empty
 6050: string or zero for "no date")
 6051: 
 6052: =item *
 6053: 
 6054: changepass($uname,$udom,$currentpass,$newpass,$server) : attempts to
 6055: change a users, password, possible return values are: ok,
 6056: pwchange_failure, non_authorized, auth_mode_error, unknown_user,
 6057: refused
 6058: 
 6059: =item *
 6060: 
 6061: modifyuserauth($udom,$uname,$umode,$upass) : modify user authentication
 6062: 
 6063: =item *
 6064: 
 6065: modifyuser($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene) : 
 6066: modify user
 6067: 
 6068: =item *
 6069: 
 6070: modifystudent
 6071: 
 6072: modify a students enrollment and identification information.
 6073: The course id is resolved based on the current users environment.  
 6074: This means the envoking user must be a course coordinator or otherwise
 6075: associated with a course.
 6076: 
 6077: This call is essentially a wrapper for lonnet::modifyuser and
 6078: lonnet::modify_student_enrollment
 6079: 
 6080: Inputs: 
 6081: 
 6082: =over 4
 6083: 
 6084: =item B<$udom> Students loncapa domain
 6085: 
 6086: =item B<$uname> Students loncapa login name
 6087: 
 6088: =item B<$uid> Students id/student number
 6089: 
 6090: =item B<$umode> Students authentication mode
 6091: 
 6092: =item B<$upass> Students password
 6093: 
 6094: =item B<$first> Students first name
 6095: 
 6096: =item B<$middle> Students middle name
 6097: 
 6098: =item B<$last> Students last name
 6099: 
 6100: =item B<$gene> Students generation
 6101: 
 6102: =item B<$usec> Students section in course
 6103: 
 6104: =item B<$end> Unix time of the roles expiration
 6105: 
 6106: =item B<$start> Unix time of the roles start date
 6107: 
 6108: =item B<$forceid> If defined, allow $uid to be changed
 6109: 
 6110: =item B<$desiredhome> server to use as home server for student
 6111: 
 6112: =back
 6113: 
 6114: =item *
 6115: 
 6116: modify_student_enrollment
 6117: 
 6118: Change a students enrollment status in a class.  The environment variable
 6119: 'role.request.course' must be defined for this function to proceed.
 6120: 
 6121: Inputs:
 6122: 
 6123: =over 4
 6124: 
 6125: =item $udom, students domain
 6126: 
 6127: =item $uname, students name
 6128: 
 6129: =item $uid, students user id
 6130: 
 6131: =item $first, students first name
 6132: 
 6133: =item $middle
 6134: 
 6135: =item $last
 6136: 
 6137: =item $gene
 6138: 
 6139: =item $usec
 6140: 
 6141: =item $end
 6142: 
 6143: =item $start
 6144: 
 6145: =back
 6146: 
 6147: 
 6148: =item *
 6149: 
 6150: assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start) : assign
 6151: custom role; give a custom role to a user for the level given by URL.  Specify
 6152: name and domain of role author, and role name
 6153: 
 6154: =item *
 6155: 
 6156: revokerole($udom,$uname,$url,$role) : revoke a role for url
 6157: 
 6158: =item *
 6159: 
 6160: revokecustomrole($udom,$uname,$url,$role) : revoke a custom role
 6161: 
 6162: =back
 6163: 
 6164: =head2 Course Infomation
 6165: 
 6166: =over 4
 6167: 
 6168: =item *
 6169: 
 6170: coursedescription($courseid) : course description
 6171: 
 6172: =item *
 6173: 
 6174: resdata($name,$domain,$type,@which) : request for current parameter
 6175: setting for a specific $type, where $type is either 'course' or 'user',
 6176: @what should be a list of parameters to ask about. This routine caches
 6177: answers for 5 minutes.
 6178: 
 6179: =back
 6180: 
 6181: =head2 Course Modification
 6182: 
 6183: =over 4
 6184: 
 6185: =item *
 6186: 
 6187: writecoursepref($courseid,%prefs) : write preferences (environment
 6188: database) for a course
 6189: 
 6190: =item *
 6191: 
 6192: createcourse($udom,$description,$url) : make/modify course
 6193: 
 6194: =back
 6195: 
 6196: =head2 Resource Subroutines
 6197: 
 6198: =over 4
 6199: 
 6200: =item *
 6201: 
 6202: subscribe($fname) : subscribe to a resource, returns URL if possible (probably should use repcopy instead)
 6203: 
 6204: =item *
 6205: 
 6206: repcopy($filename) : subscribes to the requested file, and attempts to
 6207: replicate from the owning library server, Might return
 6208: 'unavailable', 'not_found', 'forbidden', 'ok', or
 6209: 'bad_request', also attempts to grab the metadata for the
 6210: resource. Expects the local filesystem pathname
 6211: (/home/httpd/html/res/....)
 6212: 
 6213: =back
 6214: 
 6215: =head2 Resource Information
 6216: 
 6217: =over 4
 6218: 
 6219: =item *
 6220: 
 6221: EXT($varname,$symb,$udom,$uname) : evaluates and returns the value of
 6222: a vairety of different possible values, $varname should be a request
 6223: string, and the other parameters can be used to specify who and what
 6224: one is asking about.
 6225: 
 6226: Possible values for $varname are environment.lastname (or other item
 6227: from the envirnment hash), user.name (or someother aspect about the
 6228: user), resource.0.maxtries (or some other part and parameter of a
 6229: resource)
 6230: 
 6231: =item *
 6232: 
 6233: directcondval($number) : get current value of a condition; reads from a state
 6234: string
 6235: 
 6236: =item *
 6237: 
 6238: condval($condidx) : value of condition index based on state
 6239: 
 6240: =item *
 6241: 
 6242: metadata($uri,$what,$liburi,$prefix,$depthcount) : request a
 6243: resource's metadata, $what should be either a specific key, or either
 6244: 'keys' (to get a list of possible keys) or 'packages' to get a list of
 6245: packages that this resource currently uses, the last 3 arguments are only used internally for recursive metadata.
 6246: 
 6247: this function automatically caches all requests
 6248: 
 6249: =item *
 6250: 
 6251: metadata_query($query,$custom,$customshow) : make a metadata query against the
 6252: network of library servers; returns file handle of where SQL and regex results
 6253: will be stored for query
 6254: 
 6255: =item *
 6256: 
 6257: symbread($filename) : return symbolic list entry (filename argument optional);
 6258: returns the data handle
 6259: 
 6260: =item *
 6261: 
 6262: symbverify($symb,$thisfn) : verifies that $symb actually exists and is
 6263: a possible symb for the URL in $thisfn, and if is an encryypted
 6264: resource that the user accessed using /enc/ returns a 1 on success, 0
 6265: on failure, user must be in a course, as it assumes the existance of
 6266: the course initial hash, and uses $env('request.course.id'}
 6267: 
 6268: 
 6269: =item *
 6270: 
 6271: symbclean($symb) : removes versions numbers from a symb, returns the
 6272: cleaned symb
 6273: 
 6274: =item *
 6275: 
 6276: is_on_map($uri) : checks if the $uri is somewhere on the current
 6277: course map, user must be in a course for it to work.
 6278: 
 6279: =item *
 6280: 
 6281: numval($salt) : return random seed value (addend for rndseed)
 6282: 
 6283: =item *
 6284: 
 6285: rndseed($symb,$courseid,$udom,$uname) : create a random sum; returns
 6286: a random seed, all arguments are optional, if they aren't sent it uses the
 6287: environment to derive them. Note: if symb isn't sent and it can't get one
 6288: from &symbread it will use the current time as its return value
 6289: 
 6290: =item *
 6291: 
 6292: ireceipt($funame,$fudom,$fucourseid,$fusymb) : return unique,
 6293: unfakeable, receipt
 6294: 
 6295: =item *
 6296: 
 6297: receipt() : API to ireceipt working off of env values; given out to users
 6298: 
 6299: =item *
 6300: 
 6301: countacc($url) : count the number of accesses to a given URL
 6302: 
 6303: =item *
 6304: 
 6305: 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
 6306: 
 6307: =item *
 6308: 
 6309: 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)
 6310: 
 6311: =item *
 6312: 
 6313: expirespread($uname,$udom,$stype,$usymb) : set expire date for spreadsheet
 6314: 
 6315: =item *
 6316: 
 6317: devalidate($symb) : devalidate temporary spreadsheet calculations,
 6318: forcing spreadsheet to reevaluate the resource scores next time.
 6319: 
 6320: =back
 6321: 
 6322: =head2 Storing/Retreiving Data
 6323: 
 6324: =over 4
 6325: 
 6326: =item *
 6327: 
 6328: store($storehash,$symb,$namespace,$udom,$uname) : stores hash permanently
 6329: for this url; hashref needs to be given and should be a \%hashname; the
 6330: remaining args aren't required and if they aren't passed or are '' they will
 6331: be derived from the env
 6332: 
 6333: =item *
 6334: 
 6335: cstore($storehash,$symb,$namespace,$udom,$uname) : same as store but
 6336: uses critical subroutine
 6337: 
 6338: =item *
 6339: 
 6340: restore($symb,$namespace,$udom,$uname) : returns hash for this symb;
 6341: all args are optional
 6342: 
 6343: =item *
 6344: 
 6345: tmpstore($storehash,$symb,$namespace,$udom,$uname) : storage that
 6346: works very similar to store/cstore, but all data is stored in a
 6347: temporary location and can be reset using tmpreset, $storehash should
 6348: be a hash reference, returns nothing on success
 6349: 
 6350: =item *
 6351: 
 6352: tmprestore($symb,$namespace,$udom,$uname) : storage that works very
 6353: similar to restore, but all data is stored in a temporary location and
 6354: can be reset using tmpreset. Returns a hash of values on success,
 6355: error string otherwise.
 6356: 
 6357: =item *
 6358: 
 6359: tmpreset($symb,$namespace,$udom,$uname) : temporary storage reset,
 6360: deltes all keys for $symb form the temporary storage hash.
 6361: 
 6362: =item *
 6363: 
 6364: get($namespace,$storearr,$udom,$uname) : returns hash with keys from array
 6365: reference filled in from namesp ($udom and $uname are optional)
 6366: 
 6367: =item *
 6368: 
 6369: del($namespace,$storearr,$udom,$uname) : deletes keys out of array from
 6370: namesp ($udom and $uname are optional)
 6371: 
 6372: =item *
 6373: 
 6374: dump($namespace,$udom,$uname,$regexp) : 
 6375: dumps the complete (or key matching regexp) namespace into a hash
 6376: ($udom, $uname and $regexp are optional)
 6377: 
 6378: =item *
 6379: 
 6380: inc($namespace,$store,$udom,$uname) : increments $store in $namespace.
 6381: $store can be a scalar, an array reference, or if the amount to be 
 6382: incremented is > 1, a hash reference.
 6383: 
 6384: ($udom and $uname are optional)
 6385: 
 6386: =item *
 6387: 
 6388: put($namespace,$storehash,$udom,$uname) : stores hash in namesp
 6389: ($udom and $uname are optional)
 6390: 
 6391: =item *
 6392: 
 6393: putstore($namespace,$storehash,$udomain,$uname) : stores hash in namesp
 6394: keys used in storehash include version information (e.g., 1:$symb:message etc.) as
 6395: used in records written by &store and retrieved by &restore.  This function 
 6396: was created for use in editing discussion posts, without incrementing the
 6397: version number included in the key for a particular post. The colon 
 6398: separated list of attribute names (e.g., the value associated with the key 
 6399: 1:keys:$symb) is also generated and passed in the ampersand separated 
 6400: items sent to lonnet::reply().  
 6401: 
 6402: =item *
 6403: 
 6404: cput($namespace,$storehash,$udom,$uname) : critical put
 6405: ($udom and $uname are optional)
 6406: 
 6407: =item *
 6408: 
 6409: eget($namespace,$storearr,$udom,$uname) : returns hash with keys from array
 6410: reference filled in from namesp (encrypts the return communication)
 6411: ($udom and $uname are optional)
 6412: 
 6413: =item *
 6414: 
 6415: log($udom,$name,$home,$message) : write to permanent log for user; use
 6416: critical subroutine
 6417: 
 6418: =back
 6419: 
 6420: =head2 Network Status Functions
 6421: 
 6422: =over 4
 6423: 
 6424: =item *
 6425: 
 6426: dirlist($uri) : return directory list based on URI
 6427: 
 6428: =item *
 6429: 
 6430: spareserver() : find server with least workload from spare.tab
 6431: 
 6432: =back
 6433: 
 6434: =head2 Apache Request
 6435: 
 6436: =over 4
 6437: 
 6438: =item *
 6439: 
 6440: ssi($url,%hash) : server side include, does a complete request cycle on url to
 6441: localhost, posts hash
 6442: 
 6443: =back
 6444: 
 6445: =head2 Data to String to Data
 6446: 
 6447: =over 4
 6448: 
 6449: =item *
 6450: 
 6451: hash2str(%hash) : convert a hash into a string complete with escaping and '='
 6452: and '&' separators, supports elements that are arrayrefs and hashrefs
 6453: 
 6454: =item *
 6455: 
 6456: hashref2str($hashref) : convert a hashref into a string complete with
 6457: escaping and '=' and '&' separators, supports elements that are
 6458: arrayrefs and hashrefs
 6459: 
 6460: =item *
 6461: 
 6462: arrayref2str($arrayref) : convert an arrayref into a string complete
 6463: with escaping and '&' separators, supports elements that are arrayrefs
 6464: and hashrefs
 6465: 
 6466: =item *
 6467: 
 6468: str2hash($string) : convert string to hash using unescaping and
 6469: splitting on '=' and '&', supports elements that are arrayrefs and
 6470: hashrefs
 6471: 
 6472: =item *
 6473: 
 6474: str2array($string) : convert string to hash using unescaping and
 6475: splitting on '&', supports elements that are arrayrefs and hashrefs
 6476: 
 6477: =back
 6478: 
 6479: =head2 Logging Routines
 6480: 
 6481: =over 4
 6482: 
 6483: These routines allow one to make log messages in the lonnet.log and
 6484: lonnet.perm logfiles.
 6485: 
 6486: =item *
 6487: 
 6488: logtouch() : make sure the logfile, lonnet.log, exists
 6489: 
 6490: =item *
 6491: 
 6492: logthis() : append message to the normal lonnet.log file, it gets
 6493: preiodically rolled over and deleted.
 6494: 
 6495: =item *
 6496: 
 6497: logperm() : append a permanent message to lonnet.perm.log, this log
 6498: file never gets deleted by any automated portion of the system, only
 6499: messages of critical importance should go in here.
 6500: 
 6501: =back
 6502: 
 6503: =head2 General File Helper Routines
 6504: 
 6505: =over 4
 6506: 
 6507: =item *
 6508: 
 6509: getfile($file,$caller) : two cases - requests for files in /res or in /uploaded.
 6510: (a) files in /uploaded
 6511:   (i) If a local copy of the file exists - 
 6512:       compares modification date of local copy with last-modified date for 
 6513:       definitive version stored on home server for course. If local copy is 
 6514:       stale, requests a new version from the home server and stores it. 
 6515:       If the original has been removed from the home server, then local copy 
 6516:       is unlinked.
 6517:   (ii) If local copy does not exist -
 6518:       requests the file from the home server and stores it. 
 6519:   
 6520:   If $caller is 'uploadrep':  
 6521:     This indicates a call from lonuploadrep.pm (PerlHeaderParserHandler phase)
 6522:     for request for files originally uploaded via DOCS. 
 6523:      - returns 'ok' if fresh local copy now available, -1 otherwise.
 6524:   
 6525:   Otherwise:
 6526:      This indicates a call from the content generation phase of the request.
 6527:      -  returns the entire contents of the file or -1.
 6528:      
 6529: (b) files in /res
 6530:    - returns the entire contents of a file or -1; 
 6531:    it properly subscribes to and replicates the file if neccessary.
 6532: 
 6533: =item *
 6534: 
 6535: filelocation($dir,$file) : returns file system location of a file
 6536: based on URI; meant to be "fairly clean" absolute reference, $dir is a
 6537: directory that relative $file lookups are to looked in ($dir of /a/dir
 6538: and a file of ../bob will become /a/bob)
 6539: 
 6540: =item *
 6541: 
 6542: hreflocation($dir,$file) : returns file system location or a URL; same as
 6543: filelocation except for hrefs
 6544: 
 6545: =item *
 6546: 
 6547: declutter() : declutters URLs (remove docroot, beginning slashes, 'res' etc)
 6548: 
 6549: =back
 6550: 
 6551: =head2 Usererfile file routines (/uploaded*)
 6552: 
 6553: =over 4
 6554: 
 6555: =item *
 6556: 
 6557: userfileupload(): main rotine for putting a file in a user or course's
 6558:                   filespace, arguments are,
 6559: 
 6560:  formname - required - this is the name of the element in $env where the
 6561:            filename, and the contents of the file to create/modifed exist
 6562:            the filename is in $env{'form.'.$formname.'.filename'} and the
 6563:            contents of the file is located in $env{'form.'.$formname}
 6564:  coursedoc - if true, store the file in the course of the active role
 6565:              of the current user
 6566:  subdir - required - subdirectory to put the file in under ../userfiles/
 6567:          if undefined, it will be placed in "unknown"
 6568: 
 6569:  (This routine calls clean_filename() to remove any dangerous
 6570:  characters from the filename, and then calls finuserfileupload() to
 6571:  complete the transaction)
 6572: 
 6573:  returns either the url of the uploaded file (/uploaded/....) if successful
 6574:  and /adm/notfound.html if unsuccessful
 6575: 
 6576: =item *
 6577: 
 6578: clean_filename(): routine for cleaing a filename up for storage in
 6579:                  userfile space, argument is:
 6580: 
 6581:  filename - proposed filename
 6582: 
 6583: returns: the new clean filename
 6584: 
 6585: =item *
 6586: 
 6587: finishuserfileupload(): routine that creaes and sends the file to
 6588: userspace, probably shouldn't be called directly
 6589: 
 6590:   docuname: username or courseid of destination for the file
 6591:   docudom: domain of user/course of destination for the file
 6592:   docuhome: loncapa id of the library server that is getting the file
 6593:   formname: same as for userfileupload()
 6594:   fname: filename (inculding subdirectories) for the file
 6595: 
 6596:  returns either the url of the uploaded file (/uploaded/....) if successful
 6597:  and /adm/notfound.html if unsuccessful
 6598: 
 6599: =item *
 6600: 
 6601: renameuserfile(): renames an existing userfile to a new name
 6602: 
 6603:   Args:
 6604:    docuname: username or courseid of destination for the file
 6605:    docudom: domain of user/course of destination for the file
 6606:    old: current file name (including any subdirs under userfiles)
 6607:    new: desired file name (including any subdirs under userfiles)
 6608: 
 6609: =item *
 6610: 
 6611: mkdiruserfile(): creates a directory is a userfiles dir
 6612: 
 6613:   Args:
 6614:    docuname: username or courseid of destination for the file
 6615:    docudom: domain of user/course of destination for the file
 6616:    dir: dir to create (including any subdirs under userfiles)
 6617: 
 6618: =item *
 6619: 
 6620: removeuserfile(): removes a file that exists in userfiles
 6621: 
 6622:   Args:
 6623:    docuname: username or courseid of destination for the file
 6624:    docudom: domain of user/course of destination for the file
 6625:    fname: filname to delete (including any subdirs under userfiles)
 6626: 
 6627: =item *
 6628: 
 6629: removeuploadedurl(): convience function for removeuserfile()
 6630: 
 6631:   Args:
 6632:    url:  a full /uploaded/... url to delete
 6633: 
 6634: =back
 6635: 
 6636: =head2 HTTP Helper Routines
 6637: 
 6638: =over 4
 6639: 
 6640: =item *
 6641: 
 6642: escape() : unpack non-word characters into CGI-compatible hex codes
 6643: 
 6644: =item *
 6645: 
 6646: unescape() : pack CGI-compatible hex codes into actual non-word ASCII character
 6647: 
 6648: =back
 6649: 
 6650: =head1 PRIVATE SUBROUTINES
 6651: 
 6652: =head2 Underlying communication routines (Shouldn't call)
 6653: 
 6654: =over 4
 6655: 
 6656: =item *
 6657: 
 6658: subreply() : tries to pass a message to lonc, returns con_lost if incapable
 6659: 
 6660: =item *
 6661: 
 6662: reply() : uses subreply to send a message to remote machine, logs all failures
 6663: 
 6664: =item *
 6665: 
 6666: critical() : passes a critical message to another server; if cannot
 6667: get through then place message in connection buffer directory and
 6668: returns con_delayed, if incapable of saving message, returns
 6669: con_failed
 6670: 
 6671: =item *
 6672: 
 6673: reconlonc() : tries to reconnect lonc client processes.
 6674: 
 6675: =back
 6676: 
 6677: =head2 Resource Access Logging
 6678: 
 6679: =over 4
 6680: 
 6681: =item *
 6682: 
 6683: flushcourselogs() : flush (save) buffer logs and access logs
 6684: 
 6685: =item *
 6686: 
 6687: courselog($what) : save message for course in hash
 6688: 
 6689: =item *
 6690: 
 6691: courseacclog($what) : save message for course using &courselog().  Perform
 6692: special processing for specific resource types (problems, exams, quizzes, etc).
 6693: 
 6694: =item *
 6695: 
 6696: goodbye() : flush course logs and log shutting down; it is called in srm.conf
 6697: as a PerlChildExitHandler
 6698: 
 6699: =back
 6700: 
 6701: =head2 Other
 6702: 
 6703: =over 4
 6704: 
 6705: =item *
 6706: 
 6707: symblist($mapname,%newhash) : update symbolic storage links
 6708: 
 6709: =back
 6710: 
 6711: =cut

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