File:  [LON-CAPA] / loncom / lonnet / perl / lonnet.pm
Revision 1.942: download - view: text, annotated - select for diffs
Thu Feb 21 10:04:35 2008 UTC (16 years, 5 months ago) by foxr
Branches: MAIN
CVS tags: HEAD
BZ5631:

Make lonnet::ssi capable of returning the entire request object.
The idea is to allow the caller to analyze it to do
error detection,
error recovery,
implement retry policies.

    1: # The LearningOnline Network
    2: # TCP networking package
    3: #
    4: # $Id: lonnet.pm,v 1.942 2008/02/21 10:04:35 foxr 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::Date;
   35: # use Date::Parse;
   36: use vars qw(%perlvar %spareid %pr %prp $memcache %packagetab $tmpdir
   37:             $_64bit %env);
   38: 
   39: my (%badServerCache, $memcache, %courselogs, %accesshash, %domainrolehash,
   40:     %userrolehash, $processmarker, $dumpcount, %coursedombuf,
   41:     %coursenumbuf, %coursehombuf, %coursedescrbuf, %courseinstcodebuf,
   42:     %courseownerbuf, %coursetypebuf);
   43: 
   44: use IO::Socket;
   45: use GDBM_File;
   46: use HTML::LCParser;
   47: use Fcntl qw(:flock);
   48: use Storable qw(thaw nfreeze);
   49: use Time::HiRes qw( gettimeofday tv_interval );
   50: use Cache::Memcached;
   51: use Digest::MD5;
   52: use Math::Random;
   53: use LONCAPA qw(:DEFAULT :match);
   54: use LONCAPA::Configuration;
   55: 
   56: my $readit;
   57: my $max_connection_retries = 10;     # Or some such value.
   58: 
   59: require Exporter;
   60: 
   61: our @ISA = qw (Exporter);
   62: our @EXPORT = qw(%env);
   63: 
   64: =pod
   65: 
   66: =head1 Package Variables
   67: 
   68: These are largely undocumented, so if you decipher one please note it here.
   69: 
   70: =over 4
   71: 
   72: =item $processmarker
   73: 
   74: Contains the time this process was started and this servers host id.
   75: 
   76: =item $dumpcount
   77: 
   78: Counts the number of times a message log flush has been attempted (regardless
   79: of success) by this process.  Used as part of the filename when messages are
   80: delayed.
   81: 
   82: =back
   83: 
   84: =cut
   85: 
   86: 
   87: # --------------------------------------------------------------------- Logging
   88: {
   89:     my $logid;
   90:     sub instructor_log {
   91: 	my ($hash_name,$storehash,$delflag,$uname,$udom)=@_;
   92: 	$logid++;
   93: 	my $id=time().'00000'.$$.'00000'.$logid;
   94: 	return &Apache::lonnet::put('nohist_'.$hash_name,
   95: 				    { $id => {
   96: 					'exe_uname' => $env{'user.name'},
   97: 					'exe_udom'  => $env{'user.domain'},
   98: 					'exe_time'  => time(),
   99: 					'exe_ip'    => $ENV{'REMOTE_ADDR'},
  100: 					'delflag'   => $delflag,
  101: 					'logentry'  => $storehash,
  102: 					'uname'     => $uname,
  103: 					'udom'      => $udom,
  104: 				    }
  105: 				  },
  106: 				    $env{'course.'.$env{'request.course.id'}.'.domain'},
  107: 				    $env{'course.'.$env{'request.course.id'}.'.num'}
  108: 				    );
  109:     }
  110: }
  111: 
  112: sub logtouch {
  113:     my $execdir=$perlvar{'lonDaemons'};
  114:     unless (-e "$execdir/logs/lonnet.log") {	
  115: 	open(my $fh,">>$execdir/logs/lonnet.log");
  116: 	close $fh;
  117:     }
  118:     my ($wwwuid,$wwwgid)=(getpwnam('www'))[2,3];
  119:     chown($wwwuid,$wwwgid,$execdir.'/logs/lonnet.log');
  120: }
  121: 
  122: sub logthis {
  123:     my $message=shift;
  124:     my $execdir=$perlvar{'lonDaemons'};
  125:     my $now=time;
  126:     my $local=localtime($now);
  127:     if (open(my $fh,">>$execdir/logs/lonnet.log")) {
  128: 	print $fh "$local ($$): $message\n";
  129: 	close($fh);
  130:     }
  131:     return 1;
  132: }
  133: 
  134: sub logperm {
  135:     my $message=shift;
  136:     my $execdir=$perlvar{'lonDaemons'};
  137:     my $now=time;
  138:     my $local=localtime($now);
  139:     if (open(my $fh,">>$execdir/logs/lonnet.perm.log")) {
  140: 	print $fh "$now:$message:$local\n";
  141: 	close($fh);
  142:     }
  143:     return 1;
  144: }
  145: 
  146: sub create_connection {
  147:     my ($hostname,$lonid) = @_;
  148:     my $client=IO::Socket::UNIX->new(Peer    => $perlvar{'lonSockCreate'},
  149: 				     Type    => SOCK_STREAM,
  150: 				     Timeout => 10);
  151:     return 0 if (!$client);
  152:     print $client (join(':',$hostname,$lonid,&machine_ids($hostname))."\n");
  153:     my $result = <$client>;
  154:     chomp($result);
  155:     return 1 if ($result eq 'done');
  156:     return 0;
  157: }
  158: 
  159: 
  160: # -------------------------------------------------- Non-critical communication
  161: sub subreply {
  162:     my ($cmd,$server)=@_;
  163:     my $peerfile="$perlvar{'lonSockDir'}/".&hostname($server);
  164:     #
  165:     #  With loncnew process trimming, there's a timing hole between lonc server
  166:     #  process exit and the master server picking up the listen on the AF_UNIX
  167:     #  socket.  In that time interval, a lock file will exist:
  168: 
  169:     my $lockfile=$peerfile.".lock";
  170:     while (-e $lockfile) {	# Need to wait for the lockfile to disappear.
  171: 	sleep(1);
  172:     }
  173:     # At this point, either a loncnew parent is listening or an old lonc
  174:     # or loncnew child is listening so we can connect or everything's dead.
  175:     #
  176:     #   We'll give the connection a few tries before abandoning it.  If
  177:     #   connection is not possible, we'll con_lost back to the client.
  178:     #   
  179:     my $client;
  180:     for (my $retries = 0; $retries < $max_connection_retries; $retries++) {
  181: 	$client=IO::Socket::UNIX->new(Peer    =>"$peerfile",
  182: 				      Type    => SOCK_STREAM,
  183: 				      Timeout => 10);
  184: 	if ($client) {
  185: 	    last;		# Connected!
  186: 	} else {
  187: 	    &create_connection(&hostname($server),$server);
  188: 	}
  189:         sleep(1);		# Try again later if failed connection.
  190:     }
  191:     my $answer;
  192:     if ($client) {
  193: 	print $client "sethost:$server:$cmd\n";
  194: 	$answer=<$client>;
  195: 	if (!$answer) { $answer="con_lost"; }
  196: 	chomp($answer);
  197:     } else {
  198: 	$answer = 'con_lost';	# Failed connection.
  199:     }
  200:     return $answer;
  201: }
  202: 
  203: sub reply {
  204:     my ($cmd,$server)=@_;
  205:     unless (defined(&hostname($server))) { return 'no_such_host'; }
  206:     my $answer=subreply($cmd,$server);
  207:     if (($answer=~/^refused/) || ($answer=~/^rejected/)) {
  208:        &logthis("<font color=\"blue\">WARNING:".
  209:                 " $cmd to $server returned $answer</font>");
  210:     }
  211:     return $answer;
  212: }
  213: 
  214: # ----------------------------------------------------------- Send USR1 to lonc
  215: 
  216: sub reconlonc {
  217:     my ($lonid) = @_;
  218:     my $hostname = &hostname($lonid);
  219:     if ($lonid) {
  220: 	my $peerfile="$perlvar{'lonSockDir'}/$hostname";
  221: 	if ($hostname && -e $peerfile) {
  222: 	    &logthis("Trying to reconnect lonc for $lonid ($hostname)");
  223: 	    my $client=IO::Socket::UNIX->new(Peer    => $peerfile,
  224: 					     Type    => SOCK_STREAM,
  225: 					     Timeout => 10);
  226: 	    if ($client) {
  227: 		print $client ("reset_retries\n");
  228: 		my $answer=<$client>;
  229: 		#reset just this one.
  230: 	    }
  231: 	}
  232: 	return;
  233:     }
  234: 
  235:     &logthis("Trying to reconnect lonc");
  236:     my $loncfile="$perlvar{'lonDaemons'}/logs/lonc.pid";
  237:     if (open(my $fh,"<$loncfile")) {
  238: 	my $loncpid=<$fh>;
  239:         chomp($loncpid);
  240:         if (kill 0 => $loncpid) {
  241: 	    &logthis("lonc at pid $loncpid responding, sending USR1");
  242:             kill USR1 => $loncpid;
  243:             sleep 1;
  244:          } else {
  245: 	    &logthis(
  246:                "<font color=\"blue\">WARNING:".
  247:                " lonc at pid $loncpid not responding, giving up</font>");
  248:         }
  249:     } else {
  250: 	&logthis('<font color="blue">WARNING: lonc not running, giving up</font>');
  251:     }
  252: }
  253: 
  254: # ------------------------------------------------------ Critical communication
  255: 
  256: sub critical {
  257:     my ($cmd,$server)=@_;
  258:     unless (&hostname($server)) {
  259:         &logthis("<font color=\"blue\">WARNING:".
  260:                " Critical message to unknown server ($server)</font>");
  261:         return 'no_such_host';
  262:     }
  263:     my $answer=reply($cmd,$server);
  264:     if ($answer eq 'con_lost') {
  265: 	&reconlonc("$perlvar{'lonSockDir'}/$server");
  266: 	my $answer=reply($cmd,$server);
  267:         if ($answer eq 'con_lost') {
  268:             my $now=time;
  269:             my $middlename=$cmd;
  270:             $middlename=substr($middlename,0,16);
  271:             $middlename=~s/\W//g;
  272:             my $dfilename=
  273:       "$perlvar{'lonSockDir'}/delayed/$now.$dumpcount.$$.$middlename.$server";
  274:             $dumpcount++;
  275:             {
  276: 		my $dfh;
  277: 		if (open($dfh,">$dfilename")) {
  278: 		    print $dfh "$cmd\n"; 
  279: 		    close($dfh);
  280: 		}
  281:             }
  282:             sleep 2;
  283:             my $wcmd='';
  284:             {
  285: 		my $dfh;
  286: 		if (open($dfh,"<$dfilename")) {
  287: 		    $wcmd=<$dfh>; 
  288: 		    close($dfh);
  289: 		}
  290:             }
  291:             chomp($wcmd);
  292:             if ($wcmd eq $cmd) {
  293: 		&logthis("<font color=\"blue\">WARNING: ".
  294:                          "Connection buffer $dfilename: $cmd</font>");
  295:                 &logperm("D:$server:$cmd");
  296: 	        return 'con_delayed';
  297:             } else {
  298:                 &logthis("<font color=\"red\">CRITICAL:"
  299:                         ." Critical connection failed: $server $cmd</font>");
  300:                 &logperm("F:$server:$cmd");
  301:                 return 'con_failed';
  302:             }
  303:         }
  304:     }
  305:     return $answer;
  306: }
  307: 
  308: # ------------------------------------------- check if return value is an error
  309: 
  310: sub error {
  311:     my ($result) = @_;
  312:     if ($result =~ /^(con_lost|no_such_host|error: (\d+) (.*))/) {
  313: 	if ($2 == 2) { return undef; }
  314: 	return $1;
  315:     }
  316:     return undef;
  317: }
  318: 
  319: sub convert_and_load_session_env {
  320:     my ($lonidsdir,$handle)=@_;
  321:     my @profile;
  322:     {
  323: 	my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
  324: 	if (!$opened) {
  325: 	    return 0;
  326: 	}
  327: 	flock($idf,LOCK_SH);
  328: 	@profile=<$idf>;
  329: 	close($idf);
  330:     }
  331:     my %temp_env;
  332:     foreach my $line (@profile) {
  333: 	if ($line !~ m/=/) {
  334: 	    return 0;
  335: 	}
  336: 	chomp($line);
  337: 	my ($envname,$envvalue)=split(/=/,$line,2);
  338: 	$temp_env{&unescape($envname)} = &unescape($envvalue);
  339:     }
  340:     unlink("$lonidsdir/$handle.id");
  341:     if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",&GDBM_WRCREAT(),
  342: 	    0640)) {
  343: 	%disk_env = %temp_env;
  344: 	@env{keys(%temp_env)} = @disk_env{keys(%temp_env)};
  345: 	untie(%disk_env);
  346:     }
  347:     return 1;
  348: }
  349: 
  350: # ------------------------------------------- Transfer profile into environment
  351: my $env_loaded;
  352: sub transfer_profile_to_env {
  353:     my ($lonidsdir,$handle,$force_transfer) = @_;
  354:     if (!$force_transfer && $env_loaded) { return; } 
  355: 
  356:     if (!defined($lonidsdir)) {
  357: 	$lonidsdir = $perlvar{'lonIDsDir'};
  358:     }
  359:     if (!defined($handle)) {
  360:         ($handle) = ($env{'user.environment'} =~m|/([^/]+)\.id$| );
  361:     }
  362: 
  363:     my $convert;
  364:     {
  365:     	my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
  366: 	if (!$opened) {
  367: 	    return;
  368: 	}
  369: 	flock($idf,LOCK_SH);
  370: 	if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",
  371: 		&GDBM_READER(),0640)) {
  372: 	    @env{keys(%disk_env)} = @disk_env{keys(%disk_env)};
  373: 	    untie(%disk_env);
  374: 	} else {
  375: 	    $convert = 1;
  376: 	}
  377:     }
  378:     if ($convert) {
  379: 	if (!&convert_and_load_session_env($lonidsdir,$handle)) {
  380: 	    &logthis("Failed to load session, or convert session.");
  381: 	}
  382:     }
  383: 
  384:     my %remove;
  385:     while ( my $envname = each(%env) ) {
  386:         if (my ($key,$time) = ($envname =~ /^(cgi\.(\d+)_\d+\.)/)) {
  387:             if ($time < time-300) {
  388:                 $remove{$key}++;
  389:             }
  390:         }
  391:     }
  392: 
  393:     $env{'user.environment'} = "$lonidsdir/$handle.id";
  394:     $env_loaded=1;
  395:     foreach my $expired_key (keys(%remove)) {
  396:         &delenv($expired_key);
  397:     }
  398: }
  399: 
  400: # ---------------------------------------------------- Check for valid session 
  401: sub check_for_valid_session {
  402:     my ($r) = @_;
  403:     my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
  404:     my $lonid=$cookies{'lonID'};
  405:     return undef if (!$lonid);
  406: 
  407:     my $handle=&LONCAPA::clean_handle($lonid->value);
  408:     my $lonidsdir=$r->dir_config('lonIDsDir');
  409:     return undef if (!-e "$lonidsdir/$handle.id");
  410: 
  411:     my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
  412:     return undef if (!$opened);
  413: 
  414:     flock($idf,LOCK_SH);
  415:     my %disk_env;
  416:     if (!tie(%disk_env,'GDBM_File',"$lonidsdir/$handle.id",
  417: 	    &GDBM_READER(),0640)) {
  418: 	return undef;	
  419:     }
  420: 
  421:     if (!defined($disk_env{'user.name'})
  422: 	|| !defined($disk_env{'user.domain'})) {
  423: 	return undef;
  424:     }
  425:     return $handle;
  426: }
  427: 
  428: sub timed_flock {
  429:     my ($file,$lock_type) = @_;
  430:     my $failed=0;
  431:     eval {
  432: 	local $SIG{__DIE__}='DEFAULT';
  433: 	local $SIG{ALRM}=sub {
  434: 	    $failed=1;
  435: 	    die("failed lock");
  436: 	};
  437: 	alarm(13);
  438: 	flock($file,$lock_type);
  439: 	alarm(0);
  440:     };
  441:     if ($failed) {
  442: 	return undef;
  443:     } else {
  444: 	return 1;
  445:     }
  446: }
  447: 
  448: # ---------------------------------------------------------- Append Environment
  449: 
  450: sub appenv {
  451:     my %newenv=@_;
  452:     foreach my $key (keys(%newenv)) {
  453: 	if (($newenv{$key}=~/^user\.role/) || ($newenv{$key}=~/^user\.priv/)) {
  454:             &logthis("<font color=\"blue\">WARNING: ".
  455:                 "Attempt to modify environment ".$key." to ".$newenv{$key}
  456:                 .'</font>');
  457: 	    delete($newenv{$key});
  458:         } else {
  459:             $env{$key}=$newenv{$key};
  460:         }
  461:     }
  462:     my $opened = open(my $env_file,'+<',$env{'user.environment'});
  463:     if ($opened
  464: 	&& &timed_flock($env_file,LOCK_EX)
  465: 	&&
  466: 	tie(my %disk_env,'GDBM_File',$env{'user.environment'},
  467: 	    (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
  468: 	while (my ($key,$value) = each(%newenv)) {
  469: 	    $disk_env{$key} = $value;
  470: 	}
  471: 	untie(%disk_env);
  472:     }
  473:     return 'ok';
  474: }
  475: # ----------------------------------------------------- Delete from Environment
  476: 
  477: sub delenv {
  478:     my $delthis=shift;
  479:     if (($delthis=~/user\.role/) || ($delthis=~/user\.priv/)) {
  480:         &logthis("<font color=\"blue\">WARNING: ".
  481:                 "Attempt to delete from environment ".$delthis);
  482:         return 'error';
  483:     }
  484:     my $opened = open(my $env_file,'+<',$env{'user.environment'});
  485:     if ($opened
  486: 	&& &timed_flock($env_file,LOCK_EX)
  487: 	&&
  488: 	tie(my %disk_env,'GDBM_File',$env{'user.environment'},
  489: 	    (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
  490: 	foreach my $key (keys(%disk_env)) {
  491: 	    if ($key=~/^$delthis/) { 
  492: 		delete($env{$key});
  493: 		delete($disk_env{$key});
  494: 	    }
  495: 	}
  496: 	untie(%disk_env);
  497:     }
  498:     return 'ok';
  499: }
  500: 
  501: sub get_env_multiple {
  502:     my ($name) = @_;
  503:     my @values;
  504:     if (defined($env{$name})) {
  505:         # exists is it an array
  506:         if (ref($env{$name})) {
  507:             @values=@{ $env{$name} };
  508:         } else {
  509:             $values[0]=$env{$name};
  510:         }
  511:     }
  512:     return(@values);
  513: }
  514: 
  515: # ------------------------------------------ Find out current server userload
  516: sub userload {
  517:     my $numusers=0;
  518:     {
  519: 	opendir(LONIDS,$perlvar{'lonIDsDir'});
  520: 	my $filename;
  521: 	my $curtime=time;
  522: 	while ($filename=readdir(LONIDS)) {
  523: 	    next if ($filename eq '.' || $filename eq '..');
  524: 	    next if ($filename =~ /publicuser_\d+\.id/);
  525: 	    my ($mtime)=(stat($perlvar{'lonIDsDir'}.'/'.$filename))[9];
  526: 	    if ($curtime-$mtime < 1800) { $numusers++; }
  527: 	}
  528: 	closedir(LONIDS);
  529:     }
  530:     my $userloadpercent=0;
  531:     my $maxuserload=$perlvar{'lonUserLoadLim'};
  532:     if ($maxuserload) {
  533: 	$userloadpercent=100*$numusers/$maxuserload;
  534:     }
  535:     $userloadpercent=sprintf("%.2f",$userloadpercent);
  536:     return $userloadpercent;
  537: }
  538: 
  539: # ------------------------------------------ Fight off request when overloaded
  540: 
  541: sub overloaderror {
  542:     my ($r,$checkserver)=@_;
  543:     unless ($checkserver) { $checkserver=$perlvar{'lonHostID'}; }
  544:     my $loadavg;
  545:     if ($checkserver eq $perlvar{'lonHostID'}) {
  546:        open(my $loadfile,'/proc/loadavg');
  547:        $loadavg=<$loadfile>;
  548:        $loadavg =~ s/\s.*//g;
  549:        $loadavg = 100*$loadavg/$perlvar{'lonLoadLim'};
  550:        close($loadfile);
  551:     } else {
  552:        $loadavg=&reply('load',$checkserver);
  553:     }
  554:     my $overload=$loadavg-100;
  555:     if ($overload>0) {
  556: 	$r->err_headers_out->{'Retry-After'}=$overload;
  557:         $r->log_error('Overload of '.$overload.' on '.$checkserver);
  558:         return 413;
  559:     }    
  560:     return '';
  561: }
  562: 
  563: # ------------------------------ Find server with least workload from spare.tab
  564: 
  565: sub spareserver {
  566:     my ($loadpercent,$userloadpercent,$want_server_name) = @_;
  567:     my $spare_server;
  568:     if ($userloadpercent !~ /\d/) { $userloadpercent=0; }
  569:     my $lowest_load=($loadpercent > $userloadpercent) ? $loadpercent 
  570:                                                      :  $userloadpercent;
  571:     
  572:     foreach my $try_server (@{ $spareid{'primary'} }) {
  573: 	($spare_server, $lowest_load) =
  574: 	    &compare_server_load($try_server, $spare_server, $lowest_load);
  575:     }
  576: 
  577:     my $found_server = ($spare_server ne '' && $lowest_load < 100);
  578: 
  579:     if (!$found_server) {
  580: 	foreach my $try_server (@{ $spareid{'default'} }) {
  581: 	    ($spare_server, $lowest_load) =
  582: 		&compare_server_load($try_server, $spare_server, $lowest_load);
  583: 	}
  584:     }
  585: 
  586:     if (!$want_server_name) {
  587: 	$spare_server="http://".&hostname($spare_server);
  588:     }
  589:     return $spare_server;
  590: }
  591: 
  592: sub compare_server_load {
  593:     my ($try_server, $spare_server, $lowest_load) = @_;
  594: 
  595:     my $loadans     = &reply('load',    $try_server);
  596:     my $userloadans = &reply('userload',$try_server);
  597: 
  598:     if ($loadans !~ /\d/ && $userloadans !~ /\d/) {
  599: 	next; #didn't get a number from the server
  600:     }
  601: 
  602:     my $load;
  603:     if ($loadans =~ /\d/) {
  604: 	if ($userloadans =~ /\d/) {
  605: 	    #both are numbers, pick the bigger one
  606: 	    $load = ($loadans > $userloadans) ? $loadans 
  607: 		                              : $userloadans;
  608: 	} else {
  609: 	    $load = $loadans;
  610: 	}
  611:     } else {
  612: 	$load = $userloadans;
  613:     }
  614: 
  615:     if (($load =~ /\d/) && ($load < $lowest_load)) {
  616: 	$spare_server = $try_server;
  617: 	$lowest_load  = $load;
  618:     }
  619:     return ($spare_server,$lowest_load);
  620: }
  621: 
  622: # --------------------------- ask offload servers if user already has a session
  623: sub find_existing_session {
  624:     my ($udom,$uname) = @_;
  625:     foreach my $try_server (@{ $spareid{'primary'} },
  626: 			    @{ $spareid{'default'} }) {
  627: 	return $try_server if (&has_user_session($try_server, $udom, $uname));
  628:     }
  629:     return;
  630: }
  631: 
  632: # -------------------------------- ask if server already has a session for user
  633: sub has_user_session {
  634:     my ($lonid,$udom,$uname) = @_;
  635:     my $result = &reply(join(':','userhassession',
  636: 			     map {&escape($_)} ($udom,$uname)),$lonid);
  637:     return 1 if ($result eq 'ok');
  638: 
  639:     return 0;
  640: }
  641: 
  642: # --------------------------------------------- Try to change a user's password
  643: 
  644: sub changepass {
  645:     my ($uname,$udom,$currentpass,$newpass,$server,$context)=@_;
  646:     $currentpass = &escape($currentpass);
  647:     $newpass     = &escape($newpass);
  648:     my $answer = reply("encrypt:passwd:$udom:$uname:$currentpass:$newpass:$context",
  649: 		       $server);
  650:     if (! $answer) {
  651: 	&logthis("No reply on password change request to $server ".
  652: 		 "by $uname in domain $udom.");
  653:     } elsif ($answer =~ "^ok") {
  654:         &logthis("$uname in $udom successfully changed their password ".
  655: 		 "on $server.");
  656:     } elsif ($answer =~ "^pwchange_failure") {
  657: 	&logthis("$uname in $udom was unable to change their password ".
  658: 		 "on $server.  The action was blocked by either lcpasswd ".
  659: 		 "or pwchange");
  660:     } elsif ($answer =~ "^non_authorized") {
  661:         &logthis("$uname in $udom did not get their password correct when ".
  662: 		 "attempting to change it on $server.");
  663:     } elsif ($answer =~ "^auth_mode_error") {
  664:         &logthis("$uname in $udom attempted to change their password despite ".
  665: 		 "not being locally or internally authenticated on $server.");
  666:     } elsif ($answer =~ "^unknown_user") {
  667:         &logthis("$uname in $udom attempted to change their password ".
  668: 		 "on $server but were unable to because $server is not ".
  669: 		 "their home server.");
  670:     } elsif ($answer =~ "^refused") {
  671: 	&logthis("$server refused to change $uname in $udom password because ".
  672: 		 "it was sent an unencrypted request to change the password.");
  673:     }
  674:     return $answer;
  675: }
  676: 
  677: # ----------------------- Try to determine user's current authentication scheme
  678: 
  679: sub queryauthenticate {
  680:     my ($uname,$udom)=@_;
  681:     my $uhome=&homeserver($uname,$udom);
  682:     if (!$uhome) {
  683: 	&logthis("User $uname at $udom is unknown when looking for authentication mechanism");
  684: 	return 'no_host';
  685:     }
  686:     my $answer=reply("encrypt:currentauth:$udom:$uname",$uhome);
  687:     if ($answer =~ /^(unknown_user|refused|con_lost)/) {
  688: 	&logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
  689:     }
  690:     return $answer;
  691: }
  692: 
  693: # --------- Try to authenticate user from domain's lib servers (first this one)
  694: 
  695: sub authenticate {
  696:     my ($uname,$upass,$udom)=@_;
  697:     $upass=&escape($upass);
  698:     $uname= &LONCAPA::clean_username($uname);
  699:     my $uhome=&homeserver($uname,$udom,1);
  700:     if ((!$uhome) || ($uhome eq 'no_host')) {
  701: # Maybe the machine was offline and only re-appeared again recently?
  702:         &reconlonc();
  703: # One more
  704: 	my $uhome=&homeserver($uname,$udom,1);
  705: 	if ((!$uhome) || ($uhome eq 'no_host')) {
  706: 	    &logthis("User $uname at $udom is unknown in authenticate");
  707: 	}
  708: 	return 'no_host';
  709:     }
  710:     my $answer=reply("encrypt:auth:$udom:$uname:$upass",$uhome);
  711:     if ($answer eq 'authorized') {
  712: 	&logthis("User $uname at $udom authorized by $uhome"); 
  713: 	return $uhome; 
  714:     }
  715:     if ($answer eq 'non_authorized') {
  716: 	&logthis("User $uname at $udom rejected by $uhome");
  717: 	return 'no_host'; 
  718:     }
  719:     &logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
  720:     return 'no_host';
  721: }
  722: 
  723: # ---------------------- Find the homebase for a user from domain's lib servers
  724: 
  725: my %homecache;
  726: sub homeserver {
  727:     my ($uname,$udom,$ignoreBadCache)=@_;
  728:     my $index="$uname:$udom";
  729: 
  730:     if (exists($homecache{$index})) { return $homecache{$index}; }
  731: 
  732:     my %servers = &get_servers($udom,'library');
  733:     foreach my $tryserver (keys(%servers)) {
  734:         next if ($ignoreBadCache ne 'true' && 
  735: 		 exists($badServerCache{$tryserver}));
  736: 
  737: 	my $answer=reply("home:$udom:$uname",$tryserver);
  738: 	if ($answer eq 'found') {
  739: 	    delete($badServerCache{$tryserver}); 
  740: 	    return $homecache{$index}=$tryserver;
  741: 	} elsif ($answer eq 'no_host') {
  742: 	    $badServerCache{$tryserver}=1;
  743: 	}
  744:     }    
  745:     return 'no_host';
  746: }
  747: 
  748: # ------------------------------------- Find the usernames behind a list of IDs
  749: 
  750: sub idget {
  751:     my ($udom,@ids)=@_;
  752:     my %returnhash=();
  753:     
  754:     my %servers = &get_servers($udom,'library');
  755:     foreach my $tryserver (keys(%servers)) {
  756: 	my $idlist=join('&',@ids);
  757: 	$idlist=~tr/A-Z/a-z/; 
  758: 	my $reply=&reply("idget:$udom:".$idlist,$tryserver);
  759: 	my @answer=();
  760: 	if (($reply ne 'con_lost') && ($reply!~/^error\:/)) {
  761: 	    @answer=split(/\&/,$reply);
  762: 	}                    ;
  763: 	my $i;
  764: 	for ($i=0;$i<=$#ids;$i++) {
  765: 	    if ($answer[$i]) {
  766: 		$returnhash{$ids[$i]}=$answer[$i];
  767: 	    } 
  768: 	}
  769:     } 
  770:     return %returnhash;
  771: }
  772: 
  773: # ------------------------------------- Find the IDs behind a list of usernames
  774: 
  775: sub idrget {
  776:     my ($udom,@unames)=@_;
  777:     my %returnhash=();
  778:     foreach my $uname (@unames) {
  779:         $returnhash{$uname}=(&userenvironment($udom,$uname,'id'))[1];
  780:     }
  781:     return %returnhash;
  782: }
  783: 
  784: # ------------------------------- Store away a list of names and associated IDs
  785: 
  786: sub idput {
  787:     my ($udom,%ids)=@_;
  788:     my %servers=();
  789:     foreach my $uname (keys(%ids)) {
  790: 	&cput('environment',{'id'=>$ids{$uname}},$udom,$uname);
  791:         my $uhom=&homeserver($uname,$udom);
  792:         if ($uhom ne 'no_host') {
  793:             my $id=&escape($ids{$uname});
  794:             $id=~tr/A-Z/a-z/;
  795:             my $esc_unam=&escape($uname);
  796: 	    if ($servers{$uhom}) {
  797: 		$servers{$uhom}.='&'.$id.'='.$esc_unam;
  798:             } else {
  799:                 $servers{$uhom}=$id.'='.$esc_unam;
  800:             }
  801:         }
  802:     }
  803:     foreach my $server (keys(%servers)) {
  804:         &critical('idput:'.$udom.':'.$servers{$server},$server);
  805:     }
  806: }
  807: 
  808: # ------------------------------------------- get items from domain db files   
  809: 
  810: sub get_dom {
  811:     my ($namespace,$storearr,$udom,$uhome)=@_;
  812:     my $items='';
  813:     foreach my $item (@$storearr) {
  814:         $items.=&escape($item).'&';
  815:     }
  816:     $items=~s/\&$//;
  817:     if (!$udom) {
  818:         $udom=$env{'user.domain'};
  819:         if (defined(&domain($udom,'primary'))) {
  820:             $uhome=&domain($udom,'primary');
  821:         } else {
  822:             undef($uhome);
  823:         }
  824:     } else {
  825:         if (!$uhome) {
  826:             if (defined(&domain($udom,'primary'))) {
  827:                 $uhome=&domain($udom,'primary');
  828:             }
  829:         }
  830:     }
  831:     if ($udom && $uhome && ($uhome ne 'no_host')) {
  832:         my $rep=&reply("getdom:$udom:$namespace:$items",$uhome);
  833:         my %returnhash;
  834:         if ($rep eq '' || $rep =~ /^error: 2 /) {
  835:             return %returnhash;
  836:         }
  837:         my @pairs=split(/\&/,$rep);
  838:         if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
  839:             return @pairs;
  840:         }
  841:         my $i=0;
  842:         foreach my $item (@$storearr) {
  843:             $returnhash{$item}=&thaw_unescape($pairs[$i]);
  844:             $i++;
  845:         }
  846:         return %returnhash;
  847:     } else {
  848:         &logthis("get_dom failed - no homeserver and/or domain ($udom) ($uhome)");
  849:     }
  850: }
  851: 
  852: # -------------------------------------------- put items in domain db files 
  853: 
  854: sub put_dom {
  855:     my ($namespace,$storehash,$udom,$uhome)=@_;
  856:     if (!$udom) {
  857:         $udom=$env{'user.domain'};
  858:         if (defined(&domain($udom,'primary'))) {
  859:             $uhome=&domain($udom,'primary');
  860:         } else {
  861:             undef($uhome);
  862:         }
  863:     } else {
  864:         if (!$uhome) {
  865:             if (defined(&domain($udom,'primary'))) {
  866:                 $uhome=&domain($udom,'primary');
  867:             }
  868:         }
  869:     } 
  870:     if ($udom && $uhome && ($uhome ne 'no_host')) {
  871:         my $items='';
  872:         foreach my $item (keys(%$storehash)) {
  873:             $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
  874:         }
  875:         $items=~s/\&$//;
  876:         return &reply("putdom:$udom:$namespace:$items",$uhome);
  877:     } else {
  878:         &logthis("put_dom failed - no homeserver and/or domain");
  879:     }
  880: }
  881: 
  882: sub retrieve_inst_usertypes {
  883:     my ($udom) = @_;
  884:     my (%returnhash,@order);
  885:     if (defined(&domain($udom,'primary'))) {
  886:         my $uhome=&domain($udom,'primary');
  887:         my $rep=&reply("inst_usertypes:$udom",$uhome);
  888:         my ($hashitems,$orderitems) = split(/:/,$rep); 
  889:         my @pairs=split(/\&/,$hashitems);
  890:         foreach my $item (@pairs) {
  891:             my ($key,$value)=split(/=/,$item,2);
  892:             $key = &unescape($key);
  893:             next if ($key =~ /^error: 2 /);
  894:             $returnhash{$key}=&thaw_unescape($value);
  895:         }
  896:         my @esc_order = split(/\&/,$orderitems);
  897:         foreach my $item (@esc_order) {
  898:             push(@order,&unescape($item));
  899:         }
  900:     } else {
  901:         &logthis("get_dom failed - no primary domain server for $udom");
  902:     }
  903:     return (\%returnhash,\@order);
  904: }
  905: 
  906: sub is_domainimage {
  907:     my ($url) = @_;
  908:     if ($url=~m-^/+res/+($match_domain)/+\1\-domainconfig/+(img|logo|domlogo)/+-) {
  909:         if (&domain($1) ne '') {
  910:             return '1';
  911:         }
  912:     }
  913:     return;
  914: }
  915: 
  916: sub inst_directory_query {
  917:     my ($srch) = @_;
  918:     my $udom = $srch->{'srchdomain'};
  919:     my %results;
  920:     my $homeserver = &domain($udom,'primary');
  921:     my $outcome;
  922:     if ($homeserver ne '') {
  923: 	my $queryid=&reply("querysend:instdirsearch:".
  924: 			   &escape($srch->{'srchby'}).':'.
  925: 			   &escape($srch->{'srchterm'}).':'.
  926: 			   &escape($srch->{'srchtype'}),$homeserver);
  927: 	my $host=&hostname($homeserver);
  928: 	if ($queryid !~/^\Q$host\E\_/) {
  929: 	    &logthis('instituional directory search invalid queryid: '.$queryid.' for host: '.$homeserver.'in domain '.$udom);
  930: 	    return;
  931: 	}
  932: 	my $response = &get_query_reply($queryid);
  933: 	my $maxtries = 5;
  934: 	my $tries = 1;
  935: 	while (($response=~/^timeout/) && ($tries < $maxtries)) {
  936: 	    $response = &get_query_reply($queryid);
  937: 	    $tries ++;
  938: 	}
  939: 
  940:         if (!&error($response) && $response ne 'refused') {
  941:             if ($response eq 'unavailable') {
  942:                 $outcome = $response;
  943:             } else {
  944:                 $outcome = 'ok';
  945:                 my @matches = split(/\n/,$response);
  946:                 foreach my $match (@matches) {
  947:                     my ($key,$value) = split(/=/,$match);
  948:                     $results{&unescape($key).':'.$udom} = &thaw_unescape($value);
  949:                 }
  950:             }
  951:         }
  952:     }
  953:     return ($outcome,%results);
  954: }
  955: 
  956: sub usersearch {
  957:     my ($srch) = @_;
  958:     my $dom = $srch->{'srchdomain'};
  959:     my %results;
  960:     my %libserv = &all_library();
  961:     my $query = 'usersearch';
  962:     foreach my $tryserver (keys(%libserv)) {
  963:         if (&host_domain($tryserver) eq $dom) {
  964:             my $host=&hostname($tryserver);
  965:             my $queryid=
  966:                 &reply("querysend:".&escape($query).':'.
  967:                        &escape($srch->{'srchby'}).':'.
  968:                        &escape($srch->{'srchtype'}).':'.
  969:                        &escape($srch->{'srchterm'}),$tryserver);
  970:             if ($queryid !~/^\Q$host\E\_/) {
  971:                 &logthis('usersearch: invalid queryid: '.$queryid.' for host: '.$host.'in domain '.$dom.' and server: '.$tryserver);
  972:                 next;
  973:             }
  974:             my $reply = &get_query_reply($queryid);
  975:             my $maxtries = 1;
  976:             my $tries = 1;
  977:             while (($reply=~/^timeout/) && ($tries < $maxtries)) {
  978:                 $reply = &get_query_reply($queryid);
  979:                 $tries ++;
  980:             }
  981:             if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
  982:                 &logthis('usersrch error: '.$reply.' for '.$dom.' - searching for : '.$srch->{'srchterm'}.' by '.$srch->{'srchby'}.' ('.$srch->{'srchtype'}.') -  maxtries: '.$maxtries.' tries: '.$tries);
  983:             } else {
  984:                 my @matches;
  985:                 if ($reply =~ /\n/) {
  986:                     @matches = split(/\n/,$reply);
  987:                 } else {
  988:                     @matches = split(/\&/,$reply);
  989:                 }
  990:                 foreach my $match (@matches) {
  991:                     my ($uname,$udom,%userhash);
  992:                     foreach my $entry (split(/:/,$match)) {
  993:                         my ($key,$value) =
  994:                             map {&unescape($_);} split(/=/,$entry);
  995:                         $userhash{$key} = $value;
  996:                         if ($key eq 'username') {
  997:                             $uname = $value;
  998:                         } elsif ($key eq 'domain') {
  999:                             $udom = $value;
 1000:                         }
 1001:                     }
 1002:                     $results{$uname.':'.$udom} = \%userhash;
 1003:                 }
 1004:             }
 1005:         }
 1006:     }
 1007:     return %results;
 1008: }
 1009: 
 1010: sub get_instuser {
 1011:     my ($udom,$uname,$id) = @_;
 1012:     my $homeserver = &domain($udom,'primary');
 1013:     my ($outcome,%results);
 1014:     if ($homeserver ne '') {
 1015:         my $queryid=&reply("querysend:getinstuser:".&escape($uname).':'.
 1016:                            &escape($id).':'.&escape($udom),$homeserver);
 1017:         my $host=&hostname($homeserver);
 1018:         if ($queryid !~/^\Q$host\E\_/) {
 1019:             &logthis('get_instuser invalid queryid: '.$queryid.' for host: '.$homeserver.'in domain '.$udom);
 1020:             return;
 1021:         }
 1022:         my $response = &get_query_reply($queryid);
 1023:         my $maxtries = 5;
 1024:         my $tries = 1;
 1025:         while (($response=~/^timeout/) && ($tries < $maxtries)) {
 1026:             $response = &get_query_reply($queryid);
 1027:             $tries ++;
 1028:         }
 1029:         if (!&error($response) && $response ne 'refused') {
 1030:             if ($response eq 'unavailable') {
 1031:                 $outcome = $response;
 1032:             } else {
 1033:                 $outcome = 'ok';
 1034:                 my @matches = split(/\n/,$response);
 1035:                 foreach my $match (@matches) {
 1036:                     my ($key,$value) = split(/=/,$match);
 1037:                     $results{&unescape($key)} = &thaw_unescape($value);
 1038:                 }
 1039:             }
 1040:         }
 1041:     }
 1042:     my %userinfo;
 1043:     if (ref($results{$uname}) eq 'HASH') {
 1044:         %userinfo = %{$results{$uname}};
 1045:     } 
 1046:     return ($outcome,%userinfo);
 1047: }
 1048: 
 1049: sub inst_rulecheck {
 1050:     my ($udom,$uname,$id,$item,$rules) = @_;
 1051:     my %returnhash;
 1052:     if ($udom ne '') {
 1053:         if (ref($rules) eq 'ARRAY') {
 1054:             @{$rules} = map {&escape($_);} (@{$rules});
 1055:             my $rulestr = join(':',@{$rules});
 1056:             my $homeserver=&domain($udom,'primary');
 1057:             if (($homeserver ne '') && ($homeserver ne 'no_host')) {
 1058:                 my $response;
 1059:                 if ($item eq 'username') {                
 1060:                     $response=&unescape(&reply('instrulecheck:'.&escape($udom).
 1061:                                               ':'.&escape($uname).':'.$rulestr,
 1062:                                               $homeserver));
 1063:                 } elsif ($item eq 'id') {
 1064:                     $response=&unescape(&reply('instidrulecheck:'.&escape($udom).
 1065:                                               ':'.&escape($id).':'.$rulestr,
 1066:                                               $homeserver));
 1067:                 }
 1068:                 if ($response ne 'refused') {
 1069:                     my @pairs=split(/\&/,$response);
 1070:                     foreach my $item (@pairs) {
 1071:                         my ($key,$value)=split(/=/,$item,2);
 1072:                         $key = &unescape($key);
 1073:                         next if ($key =~ /^error: 2 /);
 1074:                         $returnhash{$key}=&thaw_unescape($value);
 1075:                     }
 1076:                 }
 1077:             }
 1078:         }
 1079:     }
 1080:     return %returnhash;
 1081: }
 1082: 
 1083: sub inst_userrules {
 1084:     my ($udom,$check) = @_;
 1085:     my (%ruleshash,@ruleorder);
 1086:     if ($udom ne '') {
 1087:         my $homeserver=&domain($udom,'primary');
 1088:         if (($homeserver ne '') && ($homeserver ne 'no_host')) {
 1089:             my $response;
 1090:             if ($check eq 'id') {
 1091:                 $response=&reply('instidrules:'.&escape($udom),
 1092:                                  $homeserver);
 1093:             } else {
 1094:                 $response=&reply('instuserrules:'.&escape($udom),
 1095:                                  $homeserver);
 1096:             }
 1097:             if (($response ne 'refused') && ($response ne 'error') && 
 1098:                 ($response ne 'unknown_cmd') && 
 1099:                 ($response ne 'no_such_host')) {
 1100:                 my ($hashitems,$orderitems) = split(/:/,$response);
 1101:                 my @pairs=split(/\&/,$hashitems);
 1102:                 foreach my $item (@pairs) {
 1103:                     my ($key,$value)=split(/=/,$item,2);
 1104:                     $key = &unescape($key);
 1105:                     next if ($key =~ /^error: 2 /);
 1106:                     $ruleshash{$key}=&thaw_unescape($value);
 1107:                 }
 1108:                 my @esc_order = split(/\&/,$orderitems);
 1109:                 foreach my $item (@esc_order) {
 1110:                     push(@ruleorder,&unescape($item));
 1111:                 }
 1112:             }
 1113:         }
 1114:     }
 1115:     return (\%ruleshash,\@ruleorder);
 1116: }
 1117: 
 1118: # --------------------------------------------------- Assign a key to a student
 1119: 
 1120: sub assign_access_key {
 1121: #
 1122: # a valid key looks like uname:udom#comments
 1123: # comments are being appended
 1124: #
 1125:     my ($ckey,$kdom,$knum,$cdom,$cnum,$udom,$uname,$logentry)=@_;
 1126:     $kdom=
 1127:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($kdom));
 1128:     $knum=
 1129:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($knum));
 1130:     $cdom=
 1131:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
 1132:     $cnum=
 1133:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
 1134:     $udom=$env{'user.name'} unless (defined($udom));
 1135:     $uname=$env{'user.domain'} unless (defined($uname));
 1136:     my %existing=&get('accesskeys',[$ckey],$kdom,$knum);
 1137:     if (($existing{$ckey}=~/^\#(.*)$/) || # - new key
 1138:         ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#(.*)$/)) { 
 1139:                                                   # assigned to this person
 1140:                                                   # - this should not happen,
 1141:                                                   # unless something went wrong
 1142:                                                   # the first time around
 1143: # ready to assign
 1144:         $logentry=$1.'; '.$logentry;
 1145:         if (&put('accesskeys',{$ckey=>$uname.':'.$udom.'#'.$logentry},
 1146:                                                  $kdom,$knum) eq 'ok') {
 1147: # key now belongs to user
 1148: 	    my $envkey='key.'.$cdom.'_'.$cnum;
 1149:             if (&put('environment',{$envkey => $ckey}) eq 'ok') {
 1150:                 &appenv('environment.'.$envkey => $ckey);
 1151:                 return 'ok';
 1152:             } else {
 1153:                 return 
 1154:   'error: Count not permanently assign key, will need to be re-entered later.';
 1155: 	    }
 1156:         } else {
 1157:             return 'error: Could not assign key, try again later.';
 1158:         }
 1159:     } elsif (!$existing{$ckey}) {
 1160: # the key does not exist
 1161: 	return 'error: The key does not exist';
 1162:     } else {
 1163: # the key is somebody else's
 1164: 	return 'error: The key is already in use';
 1165:     }
 1166: }
 1167: 
 1168: # ------------------------------------------ put an additional comment on a key
 1169: 
 1170: sub comment_access_key {
 1171: #
 1172: # a valid key looks like uname:udom#comments
 1173: # comments are being appended
 1174: #
 1175:     my ($ckey,$cdom,$cnum,$logentry)=@_;
 1176:     $cdom=
 1177:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
 1178:     $cnum=
 1179:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
 1180:     my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
 1181:     if ($existing{$ckey}) {
 1182:         $existing{$ckey}.='; '.$logentry;
 1183: # ready to assign
 1184:         if (&put('accesskeys',{$ckey=>$existing{$ckey}},
 1185:                                                  $cdom,$cnum) eq 'ok') {
 1186: 	    return 'ok';
 1187:         } else {
 1188: 	    return 'error: Count not store comment.';
 1189:         }
 1190:     } else {
 1191: # the key does not exist
 1192: 	return 'error: The key does not exist';
 1193:     }
 1194: }
 1195: 
 1196: # ------------------------------------------------------ Generate a set of keys
 1197: 
 1198: sub generate_access_keys {
 1199:     my ($number,$cdom,$cnum,$logentry)=@_;
 1200:     $cdom=
 1201:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
 1202:     $cnum=
 1203:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
 1204:     unless (&allowed('mky',$cdom)) { return 0; }
 1205:     unless (($cdom) && ($cnum)) { return 0; }
 1206:     if ($number>10000) { return 0; }
 1207:     sleep(2); # make sure don't get same seed twice
 1208:     srand(time()^($$+($$<<15))); # from "Programming Perl"
 1209:     my $total=0;
 1210:     for (my $i=1;$i<=$number;$i++) {
 1211:        my $newkey=sprintf("%lx",int(100000*rand)).'-'.
 1212:                   sprintf("%lx",int(100000*rand)).'-'.
 1213:                   sprintf("%lx",int(100000*rand));
 1214:        $newkey=~s/1/g/g; # folks mix up 1 and l
 1215:        $newkey=~s/0/h/g; # and also 0 and O
 1216:        my %existing=&get('accesskeys',[$newkey],$cdom,$cnum);
 1217:        if ($existing{$newkey}) {
 1218:            $i--;
 1219:        } else {
 1220: 	  if (&put('accesskeys',
 1221:               { $newkey => '# generated '.localtime().
 1222:                            ' by '.$env{'user.name'}.'@'.$env{'user.domain'}.
 1223:                            '; '.$logentry },
 1224: 		   $cdom,$cnum) eq 'ok') {
 1225:               $total++;
 1226: 	  }
 1227:        }
 1228:     }
 1229:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
 1230:          'Generated '.$total.' keys for '.$cnum.' at '.$cdom);
 1231:     return $total;
 1232: }
 1233: 
 1234: # ------------------------------------------------------- Validate an accesskey
 1235: 
 1236: sub validate_access_key {
 1237:     my ($ckey,$cdom,$cnum,$udom,$uname)=@_;
 1238:     $cdom=
 1239:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
 1240:     $cnum=
 1241:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
 1242:     $udom=$env{'user.domain'} unless (defined($udom));
 1243:     $uname=$env{'user.name'} unless (defined($uname));
 1244:     my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
 1245:     return ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#/);
 1246: }
 1247: 
 1248: # ------------------------------------- Find the section of student in a course
 1249: sub devalidate_getsection_cache {
 1250:     my ($udom,$unam,$courseid)=@_;
 1251:     my $hashid="$udom:$unam:$courseid";
 1252:     &devalidate_cache_new('getsection',$hashid);
 1253: }
 1254: 
 1255: sub courseid_to_courseurl {
 1256:     my ($courseid) = @_;
 1257:     #already url style courseid
 1258:     return $courseid if ($courseid =~ m{^/});
 1259: 
 1260:     if (exists($env{'course.'.$courseid.'.num'})) {
 1261: 	my $cnum = $env{'course.'.$courseid.'.num'};
 1262: 	my $cdom = $env{'course.'.$courseid.'.domain'};
 1263: 	return "/$cdom/$cnum";
 1264:     }
 1265: 
 1266:     my %courseinfo=&Apache::lonnet::coursedescription($courseid);
 1267:     if (exists($courseinfo{'num'})) {
 1268: 	return "/$courseinfo{'domain'}/$courseinfo{'num'}";
 1269:     }
 1270: 
 1271:     return undef;
 1272: }
 1273: 
 1274: sub getsection {
 1275:     my ($udom,$unam,$courseid)=@_;
 1276:     my $cachetime=1800;
 1277: 
 1278:     my $hashid="$udom:$unam:$courseid";
 1279:     my ($result,$cached)=&is_cached_new('getsection',$hashid);
 1280:     if (defined($cached)) { return $result; }
 1281: 
 1282:     my %Pending; 
 1283:     my %Expired;
 1284:     #
 1285:     # Each role can either have not started yet (pending), be active, 
 1286:     #    or have expired.
 1287:     #
 1288:     # If there is an active role, we are done.
 1289:     #
 1290:     # If there is more than one role which has not started yet, 
 1291:     #     choose the one which will start sooner
 1292:     # If there is one role which has not started yet, return it.
 1293:     #
 1294:     # If there is more than one expired role, choose the one which ended last.
 1295:     # If there is a role which has expired, return it.
 1296:     #
 1297:     $courseid = &courseid_to_courseurl($courseid);
 1298:     my %roleshash = &dump('roles',$udom,$unam,$courseid);
 1299:     foreach my $key (keys(%roleshash)) {
 1300:         next if ($key !~/^\Q$courseid\E(?:\/)*(\w+)*\_st$/);
 1301:         my $section=$1;
 1302:         if ($key eq $courseid.'_st') { $section=''; }
 1303:         my ($dummy,$end,$start)=split(/\_/,&unescape($roleshash{$key}));
 1304:         my $now=time;
 1305:         if (defined($end) && $end && ($now > $end)) {
 1306:             $Expired{$end}=$section;
 1307:             next;
 1308:         }
 1309:         if (defined($start) && $start && ($now < $start)) {
 1310:             $Pending{$start}=$section;
 1311:             next;
 1312:         }
 1313:         return &do_cache_new('getsection',$hashid,$section,$cachetime);
 1314:     }
 1315:     #
 1316:     # Presumedly there will be few matching roles from the above
 1317:     # loop and the sorting time will be negligible.
 1318:     if (scalar(keys(%Pending))) {
 1319:         my ($time) = sort {$a <=> $b} keys(%Pending);
 1320:         return &do_cache_new('getsection',$hashid,$Pending{$time},$cachetime);
 1321:     } 
 1322:     if (scalar(keys(%Expired))) {
 1323:         my @sorted = sort {$a <=> $b} keys(%Expired);
 1324:         my $time = pop(@sorted);
 1325:         return &do_cache_new('getsection',$hashid,$Expired{$time},$cachetime);
 1326:     }
 1327:     return &do_cache_new('getsection',$hashid,'-1',$cachetime);
 1328: }
 1329: 
 1330: sub save_cache {
 1331:     &purge_remembered();
 1332:     #&Apache::loncommon::validate_page();
 1333:     undef(%env);
 1334:     undef($env_loaded);
 1335: }
 1336: 
 1337: my $to_remember=-1;
 1338: my %remembered;
 1339: my %accessed;
 1340: my $kicks=0;
 1341: my $hits=0;
 1342: sub make_key {
 1343:     my ($name,$id) = @_;
 1344:     if (length($id) > 65 
 1345: 	&& length(&escape($id)) > 200) {
 1346: 	$id=length($id).':'.&Digest::MD5::md5_hex($id);
 1347:     }
 1348:     return &escape($name.':'.$id);
 1349: }
 1350: 
 1351: sub devalidate_cache_new {
 1352:     my ($name,$id,$debug) = @_;
 1353:     if ($debug) { &Apache::lonnet::logthis("deleting $name:$id"); }
 1354:     $id=&make_key($name,$id);
 1355:     $memcache->delete($id);
 1356:     delete($remembered{$id});
 1357:     delete($accessed{$id});
 1358: }
 1359: 
 1360: sub is_cached_new {
 1361:     my ($name,$id,$debug) = @_;
 1362:     $id=&make_key($name,$id);
 1363:     if (exists($remembered{$id})) {
 1364: 	if ($debug) { &Apache::lonnet::logthis("Earyl return $id of $remembered{$id} "); }
 1365: 	$accessed{$id}=[&gettimeofday()];
 1366: 	$hits++;
 1367: 	return ($remembered{$id},1);
 1368:     }
 1369:     my $value = $memcache->get($id);
 1370:     if (!(defined($value))) {
 1371: 	if ($debug) { &Apache::lonnet::logthis("getting $id is not defined"); }
 1372: 	return (undef,undef);
 1373:     }
 1374:     if ($value eq '__undef__') {
 1375: 	if ($debug) { &Apache::lonnet::logthis("getting $id is __undef__"); }
 1376: 	$value=undef;
 1377:     }
 1378:     &make_room($id,$value,$debug);
 1379:     if ($debug) { &Apache::lonnet::logthis("getting $id is $value"); }
 1380:     return ($value,1);
 1381: }
 1382: 
 1383: sub do_cache_new {
 1384:     my ($name,$id,$value,$time,$debug) = @_;
 1385:     $id=&make_key($name,$id);
 1386:     my $setvalue=$value;
 1387:     if (!defined($setvalue)) {
 1388: 	$setvalue='__undef__';
 1389:     }
 1390:     if (!defined($time) ) {
 1391: 	$time=600;
 1392:     }
 1393:     if ($debug) { &Apache::lonnet::logthis("Setting $id to $value"); }
 1394:     my $result = $memcache->set($id,$setvalue,$time);
 1395:     if (! $result) {
 1396: 	&logthis("caching of id -> $id  failed");
 1397: 	$memcache->disconnect_all();
 1398:     }
 1399:     # need to make a copy of $value
 1400:     &make_room($id,$value,$debug);
 1401:     return $value;
 1402: }
 1403: 
 1404: sub make_room {
 1405:     my ($id,$value,$debug)=@_;
 1406: 
 1407:     $remembered{$id}= (ref($value)) ? &Storable::dclone($value)
 1408:                                     : $value;
 1409:     if ($to_remember<0) { return; }
 1410:     $accessed{$id}=[&gettimeofday()];
 1411:     if (scalar(keys(%remembered)) <= $to_remember) { return; }
 1412:     my $to_kick;
 1413:     my $max_time=0;
 1414:     foreach my $other (keys(%accessed)) {
 1415: 	if (&tv_interval($accessed{$other}) > $max_time) {
 1416: 	    $to_kick=$other;
 1417: 	    $max_time=&tv_interval($accessed{$other});
 1418: 	}
 1419:     }
 1420:     delete($remembered{$to_kick});
 1421:     delete($accessed{$to_kick});
 1422:     $kicks++;
 1423:     if ($debug) { &logthis("kicking $to_kick $max_time $kicks\n"); }
 1424:     return;
 1425: }
 1426: 
 1427: sub purge_remembered {
 1428:     #&logthis("Tossing ".scalar(keys(%remembered)));
 1429:     #&logthis(sprintf("%-20s is %s",'%remembered',length(&freeze(\%remembered))));
 1430:     undef(%remembered);
 1431:     undef(%accessed);
 1432: }
 1433: # ------------------------------------- Read an entry from a user's environment
 1434: 
 1435: sub userenvironment {
 1436:     my ($udom,$unam,@what)=@_;
 1437:     my %returnhash=();
 1438:     my @answer=split(/\&/,
 1439:                 &reply('get:'.$udom.':'.$unam.':environment:'.join('&',@what),
 1440:                       &homeserver($unam,$udom)));
 1441:     my $i;
 1442:     for ($i=0;$i<=$#what;$i++) {
 1443: 	$returnhash{$what[$i]}=&unescape($answer[$i]);
 1444:     }
 1445:     return %returnhash;
 1446: }
 1447: 
 1448: # ---------------------------------------------------------- Get a studentphoto
 1449: sub studentphoto {
 1450:     my ($udom,$unam,$ext) = @_;
 1451:     my $home=&Apache::lonnet::homeserver($unam,$udom);
 1452:     if (defined($env{'request.course.id'})) {
 1453:         if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
 1454:             if ($udom eq $env{'course.'.$env{'request.course.id'}.'.domain'}) {
 1455:                 return(&retrievestudentphoto($udom,$unam,$ext)); 
 1456:             } else {
 1457:                 my ($result,$perm_reqd)=
 1458: 		    &Apache::lonnet::auto_photo_permission($unam,$udom);
 1459:                 if ($result eq 'ok') {
 1460:                     if (!($perm_reqd eq 'yes')) {
 1461:                         return(&retrievestudentphoto($udom,$unam,$ext));
 1462:                     }
 1463:                 }
 1464:             }
 1465:         }
 1466:     } else {
 1467:         my ($result,$perm_reqd) = 
 1468: 	    &Apache::lonnet::auto_photo_permission($unam,$udom);
 1469:         if ($result eq 'ok') {
 1470:             if (!($perm_reqd eq 'yes')) {
 1471:                 return(&retrievestudentphoto($udom,$unam,$ext));
 1472:             }
 1473:         }
 1474:     }
 1475:     return '/adm/lonKaputt/lonlogo_broken.gif';
 1476: }
 1477: 
 1478: sub retrievestudentphoto {
 1479:     my ($udom,$unam,$ext,$type) = @_;
 1480:     my $home=&Apache::lonnet::homeserver($unam,$udom);
 1481:     my $ret=&Apache::lonnet::reply("studentphoto:$udom:$unam:$ext:$type",$home);
 1482:     if ($ret eq 'ok') {
 1483:         my $url="/uploaded/$udom/$unam/internal/studentphoto.$ext";
 1484:         if ($type eq 'thumbnail') {
 1485:             $url="/uploaded/$udom/$unam/internal/studentphoto_tn.$ext"; 
 1486:         }
 1487:         my $tokenurl=&Apache::lonnet::tokenwrapper($url);
 1488:         return $tokenurl;
 1489:     } else {
 1490:         if ($type eq 'thumbnail') {
 1491:             return '/adm/lonKaputt/genericstudent_tn.gif';
 1492:         } else { 
 1493:             return '/adm/lonKaputt/lonlogo_broken.gif';
 1494:         }
 1495:     }
 1496: }
 1497: 
 1498: # -------------------------------------------------------------------- New chat
 1499: 
 1500: sub chatsend {
 1501:     my ($newentry,$anon,$group)=@_;
 1502:     my $cnum=$env{'course.'.$env{'request.course.id'}.'.num'};
 1503:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 1504:     my $chome=$env{'course.'.$env{'request.course.id'}.'.home'};
 1505:     &reply('chatsend:'.$cdom.':'.$cnum.':'.
 1506: 	   &escape($env{'user.domain'}.':'.$env{'user.name'}.':'.$anon.':'.
 1507: 		   &escape($newentry)).':'.$group,$chome);
 1508: }
 1509: 
 1510: # ------------------------------------------ Find current version of a resource
 1511: 
 1512: sub getversion {
 1513:     my $fname=&clutter(shift);
 1514:     unless ($fname=~/^\/res\//) { return -1; }
 1515:     return &currentversion(&filelocation('',$fname));
 1516: }
 1517: 
 1518: sub currentversion {
 1519:     my $fname=shift;
 1520:     my ($result,$cached)=&is_cached_new('resversion',$fname);
 1521:     if (defined($cached)) { return $result; }
 1522:     my $author=$fname;
 1523:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
 1524:     my ($udom,$uname)=split(/\//,$author);
 1525:     my $home=homeserver($uname,$udom);
 1526:     if ($home eq 'no_host') { 
 1527:         return -1; 
 1528:     }
 1529:     my $answer=reply("currentversion:$fname",$home);
 1530:     if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
 1531: 	return -1;
 1532:     }
 1533:     return &do_cache_new('resversion',$fname,$answer,600);
 1534: }
 1535: 
 1536: # ----------------------------- Subscribe to a resource, return URL if possible
 1537: 
 1538: sub subscribe {
 1539:     my $fname=shift;
 1540:     if ($fname=~/\/(aboutme|syllabus|bulletinboard|smppg)$/) { return ''; }
 1541:     $fname=~s/[\n\r]//g;
 1542:     my $author=$fname;
 1543:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
 1544:     my ($udom,$uname)=split(/\//,$author);
 1545:     my $home=homeserver($uname,$udom);
 1546:     if ($home eq 'no_host') {
 1547:         return 'not_found';
 1548:     }
 1549:     my $answer=reply("sub:$fname",$home);
 1550:     if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
 1551: 	$answer.=' by '.$home;
 1552:     }
 1553:     return $answer;
 1554: }
 1555:     
 1556: # -------------------------------------------------------------- Replicate file
 1557: 
 1558: sub repcopy {
 1559:     my $filename=shift;
 1560:     $filename=~s/\/+/\//g;
 1561:     if ($filename=~m|^/home/httpd/html/adm/|) { return 'ok'; }
 1562:     if ($filename=~m|^/home/httpd/html/lonUsers/|) { return 'ok'; }
 1563:     if ($filename=~m|^/home/httpd/html/userfiles/| or
 1564: 	$filename=~m -^/*(uploaded|editupload)/-) { 
 1565: 	return &repcopy_userfile($filename);
 1566:     }
 1567:     $filename=~s/[\n\r]//g;
 1568:     my $transname="$filename.in.transfer";
 1569: # FIXME: this should flock
 1570:     if ((-e $filename) || (-e $transname)) { return 'ok'; }
 1571:     my $remoteurl=subscribe($filename);
 1572:     if ($remoteurl =~ /^con_lost by/) {
 1573: 	   &logthis("Subscribe returned $remoteurl: $filename");
 1574:            return 'unavailable';
 1575:     } elsif ($remoteurl eq 'not_found') {
 1576: 	   #&logthis("Subscribe returned not_found: $filename");
 1577: 	   return 'not_found';
 1578:     } elsif ($remoteurl =~ /^rejected by/) {
 1579: 	   &logthis("Subscribe returned $remoteurl: $filename");
 1580:            return 'forbidden';
 1581:     } elsif ($remoteurl eq 'directory') {
 1582:            return 'ok';
 1583:     } else {
 1584:         my $author=$filename;
 1585:         $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
 1586:         my ($udom,$uname)=split(/\//,$author);
 1587:         my $home=homeserver($uname,$udom);
 1588:         unless ($home eq $perlvar{'lonHostID'}) {
 1589:            my @parts=split(/\//,$filename);
 1590:            my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 1591:            if ($path ne "$perlvar{'lonDocRoot'}/res") {
 1592:                &logthis("Malconfiguration for replication: $filename");
 1593: 	       return 'bad_request';
 1594:            }
 1595:            my $count;
 1596:            for ($count=5;$count<$#parts;$count++) {
 1597:                $path.="/$parts[$count]";
 1598:                if ((-e $path)!=1) {
 1599: 		   mkdir($path,0777);
 1600:                }
 1601:            }
 1602:            my $ua=new LWP::UserAgent;
 1603:            my $request=new HTTP::Request('GET',"$remoteurl");
 1604:            my $response=$ua->request($request,$transname);
 1605:            if ($response->is_error()) {
 1606: 	       unlink($transname);
 1607:                my $message=$response->status_line;
 1608:                &logthis("<font color=\"blue\">WARNING:"
 1609:                        ." LWP get: $message: $filename</font>");
 1610:                return 'unavailable';
 1611:            } else {
 1612: 	       if ($remoteurl!~/\.meta$/) {
 1613:                   my $mrequest=new HTTP::Request('GET',$remoteurl.'.meta');
 1614:                   my $mresponse=$ua->request($mrequest,$filename.'.meta');
 1615:                   if ($mresponse->is_error()) {
 1616: 		      unlink($filename.'.meta');
 1617:                       &logthis(
 1618:                      "<font color=\"yellow\">INFO: No metadata: $filename</font>");
 1619:                   }
 1620: 	       }
 1621:                rename($transname,$filename);
 1622:                return 'ok';
 1623:            }
 1624:        }
 1625:     }
 1626: }
 1627: 
 1628: # ------------------------------------------------ Get server side include body
 1629: sub ssi_body {
 1630:     my ($filelink,%form)=@_;
 1631:     if (! exists($form{'LONCAPA_INTERNAL_no_discussion'})) {
 1632:         $form{'LONCAPA_INTERNAL_no_discussion'}='true';
 1633:     }
 1634:     my $output=($filelink=~/^http\:/?&externalssi($filelink):
 1635:                                      &ssi($filelink,%form));
 1636:     $output=~s|//(\s*<!--)? BEGIN LON-CAPA Internal.+?// END LON-CAPA Internal\s*(-->)?\s||gs;
 1637:     $output=~s/^.*?\<body[^\>]*\>//si;
 1638:     $output=~s/\<\/body\s*\>.*?$//si;
 1639:     return $output;
 1640: }
 1641: 
 1642: # --------------------------------------------------------- Server Side Include
 1643: 
 1644: sub absolute_url {
 1645:     my ($host_name) = @_;
 1646:     my $protocol = ($ENV{'SERVER_PORT'} == 443?'https://':'http://');
 1647:     if ($host_name eq '') {
 1648: 	$host_name = $ENV{'SERVER_NAME'};
 1649:     }
 1650:     return $protocol.$host_name;
 1651: }
 1652: 
 1653: #
 1654: #   Server side include.
 1655: # Parameters:
 1656: #  fn     Possibly encrypted resource name/id.
 1657: #  form   Hash that describes how the rendering should be done
 1658: #         and other things.
 1659: #  r      Optional reference that will be given the response.
 1660: #         This is mostly provided so that the caller can implement
 1661: #         error detection, recovery and retry policies.
 1662: #     
 1663: # Returns:
 1664: #    The content of the response.
 1665: sub ssi {
 1666: 
 1667:     my ($fn,%form, $r)=@_;
 1668: 
 1669:     my $ua=new LWP::UserAgent;
 1670:     
 1671:     my $request;
 1672: 
 1673:     $form{'no_update_last_known'}=1;
 1674:     &Apache::lonenc::check_encrypt(\$fn);
 1675:     if (%form) {
 1676:       $request=new HTTP::Request('POST',&absolute_url().$fn);
 1677:       $request->content(join('&',map { &escape($_).'='.&escape($form{$_}) } keys %form));
 1678:     } else {
 1679:       $request=new HTTP::Request('GET',&absolute_url().$fn);
 1680:     }
 1681: 
 1682:     $request->header(Cookie => $ENV{'HTTP_COOKIE'});
 1683:     my $response=$ua->request($request);
 1684: 
 1685:     if ($r) {
 1686: 	$$r = $response;
 1687:     }
 1688: 
 1689:     return $response->content;
 1690: }
 1691: 
 1692: sub externalssi {
 1693:     my ($url)=@_;
 1694:     my $ua=new LWP::UserAgent;
 1695:     my $request=new HTTP::Request('GET',$url);
 1696:     my $response=$ua->request($request);
 1697:     return $response->content;
 1698: }
 1699: 
 1700: # -------------------------------- Allow a /uploaded/ URI to be vouched for
 1701: 
 1702: sub allowuploaded {
 1703:     my ($srcurl,$url)=@_;
 1704:     $url=&clutter(&declutter($url));
 1705:     my $dir=$url;
 1706:     $dir=~s/\/[^\/]+$//;
 1707:     my %httpref=();
 1708:     my $httpurl=&hreflocation('',$url);
 1709:     $httpref{'httpref.'.$httpurl}=$srcurl;
 1710:     &Apache::lonnet::appenv(%httpref);
 1711: }
 1712: 
 1713: # --------- File operations in /home/httpd/html/userfiles/$domain/1/2/3/$course
 1714: # input: action, courseID, current domain, intended
 1715: #        path to file, source of file, instruction to parse file for objects,
 1716: #        ref to hash for embedded objects,
 1717: #        ref to hash for codebase of java objects.
 1718: #
 1719: # output: url to file (if action was uploaddoc), 
 1720: #         ok if successful, or diagnostic message otherwise (if action was propagate or copy)
 1721: #
 1722: # Allows directory structure to be used within lonUsers/../userfiles/ for a 
 1723: # course.
 1724: #
 1725: # action = propagate - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
 1726: #          will be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles in
 1727: #          course's home server.
 1728: #
 1729: # action = copy - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file will
 1730: #          be copied from $source (current location) to 
 1731: #          /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
 1732: #         and will then be copied to
 1733: #          /home/httpd/lonUsers/$domain/1/2/3/$course/userfiles/$file in
 1734: #         course's home server.
 1735: #
 1736: # action = uploaddoc - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
 1737: #         will be retrived from $env{form.uploaddoc} (from DOCS interface) to
 1738: #         /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
 1739: #         and will then be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles/$file
 1740: #         in course's home server.
 1741: #
 1742: 
 1743: sub process_coursefile {
 1744:     my ($action,$docuname,$docudom,$file,$source,$parser,$allfiles,$codebase)=@_;
 1745:     my $fetchresult;
 1746:     my $home=&homeserver($docuname,$docudom);
 1747:     if ($action eq 'propagate') {
 1748:         $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
 1749: 			     $home);
 1750:     } else {
 1751:         my $fpath = '';
 1752:         my $fname = $file;
 1753:         ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
 1754:         $fpath=$docudom.'/'.$docuname.'/'.$fpath;
 1755:         my $filepath = &build_filepath($fpath);
 1756:         if ($action eq 'copy') {
 1757:             if ($source eq '') {
 1758:                 $fetchresult = 'no source file';
 1759:                 return $fetchresult;
 1760:             } else {
 1761:                 my $destination = $filepath.'/'.$fname;
 1762:                 rename($source,$destination);
 1763:                 $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
 1764:                                  $home);
 1765:             }
 1766:         } elsif ($action eq 'uploaddoc') {
 1767:             open(my $fh,'>'.$filepath.'/'.$fname);
 1768:             print $fh $env{'form.'.$source};
 1769:             close($fh);
 1770:             if ($parser eq 'parse') {
 1771:                 my $parse_result = &extract_embedded_items($filepath,$fname,$allfiles,$codebase);
 1772:                 unless ($parse_result eq 'ok') {
 1773:                     &logthis('Failed to parse '.$filepath.'/'.$fname.' for embedded media: '.$parse_result);
 1774:                 }
 1775:             }
 1776:             $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
 1777:                                  $home);
 1778:             if ($fetchresult eq 'ok') {
 1779:                 return '/uploaded/'.$fpath.'/'.$fname;
 1780:             } else {
 1781:                 &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
 1782:                         ' to host '.$home.': '.$fetchresult);
 1783:                 return '/adm/notfound.html';
 1784:             }
 1785:         }
 1786:     }
 1787:     unless ( $fetchresult eq 'ok') {
 1788:         &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
 1789:              ' to host '.$home.': '.$fetchresult);
 1790:     }
 1791:     return $fetchresult;
 1792: }
 1793: 
 1794: sub build_filepath {
 1795:     my ($fpath) = @_;
 1796:     my $filepath=$perlvar{'lonDocRoot'}.'/userfiles';
 1797:     unless ($fpath eq '') {
 1798:         my @parts=split('/',$fpath);
 1799:         foreach my $part (@parts) {
 1800:             $filepath.= '/'.$part;
 1801:             if ((-e $filepath)!=1) {
 1802:                 mkdir($filepath,0777);
 1803:             }
 1804:         }
 1805:     }
 1806:     return $filepath;
 1807: }
 1808: 
 1809: sub store_edited_file {
 1810:     my ($primary_url,$content,$docudom,$docuname,$fetchresult) = @_;
 1811:     my $file = $primary_url;
 1812:     $file =~ s#^/uploaded/$docudom/$docuname/##;
 1813:     my $fpath = '';
 1814:     my $fname = $file;
 1815:     ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
 1816:     $fpath=$docudom.'/'.$docuname.'/'.$fpath;
 1817:     my $filepath = &build_filepath($fpath);
 1818:     open(my $fh,'>'.$filepath.'/'.$fname);
 1819:     print $fh $content;
 1820:     close($fh);
 1821:     my $home=&homeserver($docuname,$docudom);
 1822:     $$fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
 1823: 			  $home);
 1824:     if ($$fetchresult eq 'ok') {
 1825:         return '/uploaded/'.$fpath.'/'.$fname;
 1826:     } else {
 1827:         &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
 1828: 		 ' to host '.$home.': '.$$fetchresult);
 1829:         return '/adm/notfound.html';
 1830:     }
 1831: }
 1832: 
 1833: sub clean_filename {
 1834:     my ($fname,$args)=@_;
 1835: # Replace Windows backslashes by forward slashes
 1836:     $fname=~s/\\/\//g;
 1837:     if (!$args->{'keep_path'}) {
 1838:         # Get rid of everything but the actual filename
 1839: 	$fname=~s/^.*\/([^\/]+)$/$1/;
 1840:     }
 1841: # Replace spaces by underscores
 1842:     $fname=~s/\s+/\_/g;
 1843: # Replace all other weird characters by nothing
 1844:     $fname=~s{[^/\w\.\-]}{}g;
 1845: # Replace all .\d. sequences with _\d. so they no longer look like version
 1846: # numbers
 1847:     $fname=~s/\.(\d+)(?=\.)/_$1/g;
 1848:     return $fname;
 1849: }
 1850: 
 1851: # --------------- Take an uploaded file and put it into the userfiles directory
 1852: # input: $formname - the contents of the file are in $env{"form.$formname"}
 1853: #                    the desired filenam is in $env{"form.$formname.filename"}
 1854: #        $coursedoc - if true up to the current course
 1855: #                     if false
 1856: #        $subdir - directory in userfile to store the file into
 1857: #        $parser - instruction to parse file for objects ($parser = parse)    
 1858: #        $allfiles - reference to hash for embedded objects
 1859: #        $codebase - reference to hash for codebase of java objects
 1860: #        $desuname - username for permanent storage of uploaded file
 1861: #        $dsetudom - domain for permanaent storage of uploaded file
 1862: #        $thumbwidth - width (pixels) of thumbnail to make for uploaded image 
 1863: #        $thumbheight - height (pixels) of thumbnail to make for uploaded image
 1864: # 
 1865: # output: url of file in userspace, or error: <message> 
 1866: #             or /adm/notfound.html if failure to upload occurse
 1867: 
 1868: 
 1869: sub userfileupload {
 1870:     my ($formname,$coursedoc,$subdir,$parser,$allfiles,$codebase,$destuname,
 1871:         $destudom,$thumbwidth,$thumbheight)=@_;
 1872:     if (!defined($subdir)) { $subdir='unknown'; }
 1873:     my $fname=$env{'form.'.$formname.'.filename'};
 1874:     $fname=&clean_filename($fname);
 1875: # See if there is anything left
 1876:     unless ($fname) { return 'error: no uploaded file'; }
 1877:     chop($env{'form.'.$formname});
 1878:     if (($formname eq 'screenshot') && ($subdir eq 'helprequests')) { #files uploaded to help request form are handled differently
 1879:         my $now = time;
 1880:         my $filepath = 'tmp/helprequests/'.$now;
 1881:         my @parts=split(/\//,$filepath);
 1882:         my $fullpath = $perlvar{'lonDaemons'};
 1883:         for (my $i=0;$i<@parts;$i++) {
 1884:             $fullpath .= '/'.$parts[$i];
 1885:             if ((-e $fullpath)!=1) {
 1886:                 mkdir($fullpath,0777);
 1887:             }
 1888:         }
 1889:         open(my $fh,'>'.$fullpath.'/'.$fname);
 1890:         print $fh $env{'form.'.$formname};
 1891:         close($fh);
 1892:         return $fullpath.'/'.$fname;
 1893:     } elsif (($formname eq 'coursecreatorxml') && ($subdir eq 'batchupload')) { #files uploaded to create course page are handled differently
 1894:         my $filepath = 'tmp/addcourse/'.$destudom.'/web/'.$env{'user.name'}.
 1895:                        '_'.$env{'user.domain'}.'/pending';
 1896:         my @parts=split(/\//,$filepath);
 1897:         my $fullpath = $perlvar{'lonDaemons'};
 1898:         for (my $i=0;$i<@parts;$i++) {
 1899:             $fullpath .= '/'.$parts[$i];
 1900:             if ((-e $fullpath)!=1) {
 1901:                 mkdir($fullpath,0777);
 1902:             }
 1903:         }
 1904:         open(my $fh,'>'.$fullpath.'/'.$fname);
 1905:         print $fh $env{'form.'.$formname};
 1906:         close($fh);
 1907:         return $fullpath.'/'.$fname;
 1908:     }
 1909:     
 1910: # Create the directory if not present
 1911:     $fname="$subdir/$fname";
 1912:     if ($coursedoc) {
 1913: 	my $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
 1914: 	my $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 1915:         if ($env{'form.folder'} =~ m/^(default|supplemental)/) {
 1916:             return &finishuserfileupload($docuname,$docudom,
 1917: 					 $formname,$fname,$parser,$allfiles,
 1918: 					 $codebase,$thumbwidth,$thumbheight);
 1919:         } else {
 1920:             $fname=$env{'form.folder'}.'/'.$fname;
 1921:             return &process_coursefile('uploaddoc',$docuname,$docudom,
 1922: 				       $fname,$formname,$parser,
 1923: 				       $allfiles,$codebase);
 1924:         }
 1925:     } elsif (defined($destuname)) {
 1926:         my $docuname=$destuname;
 1927:         my $docudom=$destudom;
 1928: 	return &finishuserfileupload($docuname,$docudom,$formname,$fname,
 1929: 				     $parser,$allfiles,$codebase,
 1930:                                      $thumbwidth,$thumbheight);
 1931:         
 1932:     } else {
 1933:         my $docuname=$env{'user.name'};
 1934:         my $docudom=$env{'user.domain'};
 1935:         if (exists($env{'form.group'})) {
 1936:             $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
 1937:             $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 1938:         }
 1939: 	return &finishuserfileupload($docuname,$docudom,$formname,$fname,
 1940: 				     $parser,$allfiles,$codebase,
 1941:                                      $thumbwidth,$thumbheight);
 1942:     }
 1943: }
 1944: 
 1945: sub finishuserfileupload {
 1946:     my ($docuname,$docudom,$formname,$fname,$parser,$allfiles,$codebase,
 1947:         $thumbwidth,$thumbheight) = @_;
 1948:     my $path=$docudom.'/'.$docuname.'/';
 1949:     my $filepath=$perlvar{'lonDocRoot'};
 1950:     my ($fnamepath,$file,$fetchthumb);
 1951:     $file=$fname;
 1952:     if ($fname=~m|/|) {
 1953:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 1954: 	$path.=$fnamepath.'/';
 1955:     }
 1956:     my @parts=split(/\//,$filepath.'/userfiles/'.$path);
 1957:     my $count;
 1958:     for ($count=4;$count<=$#parts;$count++) {
 1959:         $filepath.="/$parts[$count]";
 1960:         if ((-e $filepath)!=1) {
 1961: 	    mkdir($filepath,0777);
 1962:         }
 1963:     }
 1964: # Save the file
 1965:     {
 1966: 	if (!open(FH,'>'.$filepath.'/'.$file)) {
 1967: 	    &logthis('Failed to create '.$filepath.'/'.$file);
 1968: 	    print STDERR ('Failed to create '.$filepath.'/'.$file."\n");
 1969: 	    return '/adm/notfound.html';
 1970: 	}
 1971: 	if (!print FH ($env{'form.'.$formname})) {
 1972: 	    &logthis('Failed to write to '.$filepath.'/'.$file);
 1973: 	    print STDERR ('Failed to write to '.$filepath.'/'.$file."\n");
 1974: 	    return '/adm/notfound.html';
 1975: 	}
 1976: 	close(FH);
 1977:     }
 1978:     if ($parser eq 'parse') {
 1979:         my $parse_result = &extract_embedded_items($filepath,$file,$allfiles,
 1980: 						   $codebase);
 1981:         unless ($parse_result eq 'ok') {
 1982:             &logthis('Failed to parse '.$filepath.$file.
 1983: 		     ' for embedded media: '.$parse_result); 
 1984:         }
 1985:     }
 1986:     if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 1987:         my $input = $filepath.'/'.$file;
 1988:         my $output = $filepath.'/'.'tn-'.$file;
 1989:         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 1990:         system("convert -sample $thumbsize $input $output");
 1991:         if (-e $filepath.'/'.'tn-'.$file) {
 1992:             $fetchthumb  = 1; 
 1993:         }
 1994:     }
 1995:  
 1996: # Notify homeserver to grep it
 1997: #
 1998:     my $docuhome=&homeserver($docuname,$docudom);
 1999:     my $fetchresult= &reply('fetchuserfile:'.$path.$file,$docuhome);
 2000:     if ($fetchresult eq 'ok') {
 2001:         if ($fetchthumb) {
 2002:             my $thumbresult= &reply('fetchuserfile:'.$path.'tn-'.$file,$docuhome);
 2003:             if ($thumbresult ne 'ok') {
 2004:                 &logthis('Failed to transfer '.$path.'tn-'.$file.' to host '.
 2005:                          $docuhome.': '.$thumbresult);
 2006:             }
 2007:         }
 2008: #
 2009: # Return the URL to it
 2010:         return '/uploaded/'.$path.$file;
 2011:     } else {
 2012:         &logthis('Failed to transfer '.$path.$file.' to host '.$docuhome.
 2013: 		 ': '.$fetchresult);
 2014:         return '/adm/notfound.html';
 2015:     }
 2016: }
 2017: 
 2018: sub extract_embedded_items {
 2019:     my ($filepath,$file,$allfiles,$codebase,$content) = @_;
 2020:     my @state = ();
 2021:     my %javafiles = (
 2022:                       codebase => '',
 2023:                       code => '',
 2024:                       archive => ''
 2025:                     );
 2026:     my %mediafiles = (
 2027:                       src => '',
 2028:                       movie => '',
 2029:                      );
 2030:     my $p;
 2031:     if ($content) {
 2032:         $p = HTML::LCParser->new($content);
 2033:     } else {
 2034:         $p = HTML::LCParser->new($filepath.'/'.$file);
 2035:     }
 2036:     while (my $t=$p->get_token()) {
 2037: 	if ($t->[0] eq 'S') {
 2038: 	    my ($tagname, $attr) = ($t->[1],$t->[2]);
 2039: 	    push(@state, $tagname);
 2040:             if (lc($tagname) eq 'allow') {
 2041:                 &add_filetype($allfiles,$attr->{'src'},'src');
 2042:             }
 2043: 	    if (lc($tagname) eq 'img') {
 2044: 		&add_filetype($allfiles,$attr->{'src'},'src');
 2045: 	    }
 2046: 	    if (lc($tagname) eq 'a') {
 2047: 		&add_filetype($allfiles,$attr->{'href'},'href');
 2048: 	    }
 2049:             if (lc($tagname) eq 'script') {
 2050:                 if ($attr->{'archive'} =~ /\.jar$/i) {
 2051:                     &add_filetype($allfiles,$attr->{'archive'},'archive');
 2052:                 } else {
 2053:                     &add_filetype($allfiles,$attr->{'src'},'src');
 2054:                 }
 2055:             }
 2056:             if (lc($tagname) eq 'link') {
 2057:                 if (lc($attr->{'rel'}) eq 'stylesheet') { 
 2058:                     &add_filetype($allfiles,$attr->{'href'},'href');
 2059:                 }
 2060:             }
 2061: 	    if (lc($tagname) eq 'object' ||
 2062: 		(lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')) {
 2063: 		foreach my $item (keys(%javafiles)) {
 2064: 		    $javafiles{$item} = '';
 2065: 		}
 2066: 	    }
 2067: 	    if (lc($state[-2]) eq 'object' && lc($tagname) eq 'param') {
 2068: 		my $name = lc($attr->{'name'});
 2069: 		foreach my $item (keys(%javafiles)) {
 2070: 		    if ($name eq $item) {
 2071: 			$javafiles{$item} = $attr->{'value'};
 2072: 			last;
 2073: 		    }
 2074: 		}
 2075: 		foreach my $item (keys(%mediafiles)) {
 2076: 		    if ($name eq $item) {
 2077: 			&add_filetype($allfiles, $attr->{'value'}, 'value');
 2078: 			last;
 2079: 		    }
 2080: 		}
 2081: 	    }
 2082: 	    if (lc($tagname) eq 'embed' || lc($tagname) eq 'applet') {
 2083: 		foreach my $item (keys(%javafiles)) {
 2084: 		    if ($attr->{$item}) {
 2085: 			$javafiles{$item} = $attr->{$item};
 2086: 			last;
 2087: 		    }
 2088: 		}
 2089: 		foreach my $item (keys(%mediafiles)) {
 2090: 		    if ($attr->{$item}) {
 2091: 			&add_filetype($allfiles,$attr->{$item},$item);
 2092: 			last;
 2093: 		    }
 2094: 		}
 2095: 	    }
 2096: 	} elsif ($t->[0] eq 'E') {
 2097: 	    my ($tagname) = ($t->[1]);
 2098: 	    if ($javafiles{'codebase'} ne '') {
 2099: 		$javafiles{'codebase'} .= '/';
 2100: 	    }  
 2101: 	    if (lc($tagname) eq 'applet' ||
 2102: 		lc($tagname) eq 'object' ||
 2103: 		(lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')
 2104: 		) {
 2105: 		foreach my $item (keys(%javafiles)) {
 2106: 		    if ($item ne 'codebase' && $javafiles{$item} ne '') {
 2107: 			my $file=$javafiles{'codebase'}.$javafiles{$item};
 2108: 			&add_filetype($allfiles,$file,$item);
 2109: 		    }
 2110: 		}
 2111: 	    } 
 2112: 	    pop @state;
 2113: 	}
 2114:     }
 2115:     return 'ok';
 2116: }
 2117: 
 2118: sub add_filetype {
 2119:     my ($allfiles,$file,$type)=@_;
 2120:     if (exists($allfiles->{$file})) {
 2121: 	unless (grep/^\Q$type\E$/, @{$allfiles->{$file}}) {
 2122: 	    push(@{$allfiles->{$file}}, &escape($type));
 2123: 	}
 2124:     } else {
 2125: 	@{$allfiles->{$file}} = (&escape($type));
 2126:     }
 2127: }
 2128: 
 2129: sub removeuploadedurl {
 2130:     my ($url)=@_;
 2131:     my (undef,undef,$udom,$uname,$fname)=split('/',$url,5);
 2132:     return &removeuserfile($uname,$udom,$fname);
 2133: }
 2134: 
 2135: sub removeuserfile {
 2136:     my ($docuname,$docudom,$fname)=@_;
 2137:     my $home=&homeserver($docuname,$docudom);
 2138:     my $result = &reply("removeuserfile:$docudom/$docuname/$fname",$home);
 2139:     if ($result eq 'ok') {
 2140:         if (($fname !~ /\.meta$/) && (&is_portfolio_file($fname))) {
 2141:             my $metafile = $fname.'.meta';
 2142:             my $metaresult = &removeuserfile($docuname,$docudom,$metafile); 
 2143: 	    my $url = "/uploaded/$docudom/$docuname/$fname";
 2144:             my ($file,$group) = (&parse_portfolio_url($url))[3,4];
 2145:             my $sqlresult = 
 2146:                 &update_portfolio_table($docuname,$docudom,$file,
 2147:                                         'portfolio_metadata',$group,
 2148:                                         'delete');
 2149:         }
 2150:     }
 2151:     return $result;
 2152: }
 2153: 
 2154: sub mkdiruserfile {
 2155:     my ($docuname,$docudom,$dir)=@_;
 2156:     my $home=&homeserver($docuname,$docudom);
 2157:     return &reply("mkdiruserfile:".&escape("$docudom/$docuname/$dir"),$home);
 2158: }
 2159: 
 2160: sub renameuserfile {
 2161:     my ($docuname,$docudom,$old,$new)=@_;
 2162:     my $home=&homeserver($docuname,$docudom);
 2163:     my $result = &reply("renameuserfile:$docudom:$docuname:".
 2164:                         &escape("$old").':'.&escape("$new"),$home);
 2165:     if ($result eq 'ok') {
 2166:         if (($old !~ /\.meta$/) && (&is_portfolio_file($old))) {
 2167:             my $oldmeta = $old.'.meta';
 2168:             my $newmeta = $new.'.meta';
 2169:             my $metaresult = 
 2170:                 &renameuserfile($docuname,$docudom,$oldmeta,$newmeta);
 2171: 	    my $url = "/uploaded/$docudom/$docuname/$old";
 2172:             my ($file,$group) = (&parse_portfolio_url($url))[3,4];
 2173:             my $sqlresult = 
 2174:                 &update_portfolio_table($docuname,$docudom,$file,
 2175:                                         'portfolio_metadata',$group,
 2176:                                         'delete');
 2177:         }
 2178:     }
 2179:     return $result;
 2180: }
 2181: 
 2182: # ------------------------------------------------------------------------- Log
 2183: 
 2184: sub log {
 2185:     my ($dom,$nam,$hom,$what)=@_;
 2186:     return critical("log:$dom:$nam:$what",$hom);
 2187: }
 2188: 
 2189: # ------------------------------------------------------------------ Course Log
 2190: #
 2191: # This routine flushes several buffers of non-mission-critical nature
 2192: #
 2193: 
 2194: sub flushcourselogs {
 2195:     &logthis('Flushing log buffers');
 2196: #
 2197: # course logs
 2198: # This is a log of all transactions in a course, which can be used
 2199: # for data mining purposes
 2200: #
 2201: # It also collects the courseid database, which lists last transaction
 2202: # times and course titles for all courseids
 2203: #
 2204:     my %courseidbuffer=();
 2205:     foreach my $crsid (keys(%courselogs)) {
 2206:         if (&reply('log:'.$coursedombuf{$crsid}.':'.$coursenumbuf{$crsid}.':'.
 2207: 		          &escape($courselogs{$crsid}),
 2208: 		          $coursehombuf{$crsid}) eq 'ok') {
 2209: 	    delete $courselogs{$crsid};
 2210:         } else {
 2211:             &logthis('Failed to flush log buffer for '.$crsid);
 2212:             if (length($courselogs{$crsid})>40000) {
 2213:                &logthis("<font color=\"blue\">WARNING: Buffer for ".$crsid.
 2214:                         " exceeded maximum size, deleting.</font>");
 2215:                delete $courselogs{$crsid};
 2216:             }
 2217:         }
 2218:         $courseidbuffer{$coursehombuf{$crsid}}{$crsid} = {
 2219:             'description' => $coursedescrbuf{$crsid},
 2220:             'inst_code'    => $courseinstcodebuf{$crsid},
 2221:             'type'        => $coursetypebuf{$crsid},
 2222:             'owner'       => $courseownerbuf{$crsid},
 2223:         };
 2224:     }
 2225: #
 2226: # Write course id database (reverse lookup) to homeserver of courses 
 2227: # Is used in pickcourse
 2228: #
 2229:     foreach my $crs_home (keys(%courseidbuffer)) {
 2230:         my $response = &courseidput(&host_domain($crs_home),
 2231:                                     $courseidbuffer{$crs_home},
 2232:                                     $crs_home,'timeonly');
 2233:     }
 2234: #
 2235: # File accesses
 2236: # Writes to the dynamic metadata of resources to get hit counts, etc.
 2237: #
 2238:     foreach my $entry (keys(%accesshash)) {
 2239:         if ($entry =~ /___count$/) {
 2240:             my ($dom,$name);
 2241:             ($dom,$name,undef)=
 2242: 		($entry=~m{___($match_domain)/($match_name)/(.*)___count$});
 2243:             if (! defined($dom) || $dom eq '' || 
 2244:                 ! defined($name) || $name eq '') {
 2245:                 my $cid = $env{'request.course.id'};
 2246:                 $dom  = $env{'request.'.$cid.'.domain'};
 2247:                 $name = $env{'request.'.$cid.'.num'};
 2248:             }
 2249:             my $value = $accesshash{$entry};
 2250:             my (undef,$url,undef) = ($entry =~ /^(.*)___(.*)___count$/);
 2251:             my %temphash=($url => $value);
 2252:             my $result = &inc('nohist_accesscount',\%temphash,$dom,$name);
 2253:             if ($result eq 'ok') {
 2254:                 delete $accesshash{$entry};
 2255:             } elsif ($result eq 'unknown_cmd') {
 2256:                 # Target server has old code running on it.
 2257:                 my %temphash=($entry => $value);
 2258:                 if (&put('nohist_resevaldata',\%temphash,$dom,$name) eq 'ok') {
 2259:                     delete $accesshash{$entry};
 2260:                 }
 2261:             }
 2262:         } else {
 2263:             my ($dom,$name) = ($entry=~m{___($match_domain)/($match_name)/(.*)___(\w+)$});
 2264:             my %temphash=($entry => $accesshash{$entry});
 2265:             if (&put('nohist_resevaldata',\%temphash,$dom,$name) eq 'ok') {
 2266:                 delete $accesshash{$entry};
 2267:             }
 2268:         }
 2269:     }
 2270: #
 2271: # Roles
 2272: # Reverse lookup of user roles for course faculty/staff and co-authorship
 2273: #
 2274:     foreach my $entry (keys(%userrolehash)) {
 2275:         my ($role,$uname,$udom,$runame,$rudom,$rsec)=
 2276: 	    split(/\:/,$entry);
 2277:         if (&Apache::lonnet::put('nohist_userroles',
 2278:              { $role.':'.$uname.':'.$udom.':'.$rsec => $userrolehash{$entry} },
 2279:                 $rudom,$runame) eq 'ok') {
 2280: 	    delete $userrolehash{$entry};
 2281:         }
 2282:     }
 2283: #
 2284: # Reverse lookup of domain roles (dc, ad, li, sc, au)
 2285: #
 2286:     my %domrolebuffer = ();
 2287:     foreach my $entry (keys %domainrolehash) {
 2288:         my ($role,$uname,$udom,$runame,$rudom,$rsec)=split(/:/,$entry);
 2289:         if ($domrolebuffer{$rudom}) {
 2290:             $domrolebuffer{$rudom}.='&'.&escape($entry).
 2291:                       '='.&escape($domainrolehash{$entry});
 2292:         } else {
 2293:             $domrolebuffer{$rudom}.=&escape($entry).
 2294:                       '='.&escape($domainrolehash{$entry});
 2295:         }
 2296:         delete $domainrolehash{$entry};
 2297:     }
 2298:     foreach my $dom (keys(%domrolebuffer)) {
 2299: 	my %servers = &get_servers($dom,'library');
 2300: 	foreach my $tryserver (keys(%servers)) {
 2301: 	    unless (&reply('domroleput:'.$dom.':'.
 2302: 			   $domrolebuffer{$dom},$tryserver) eq 'ok') {
 2303: 		&logthis('Put of domain roles failed for '.$dom.' and  '.$tryserver);
 2304: 	    }
 2305:         }
 2306:     }
 2307:     $dumpcount++;
 2308: }
 2309: 
 2310: sub courselog {
 2311:     my $what=shift;
 2312:     $what=time.':'.$what;
 2313:     unless ($env{'request.course.id'}) { return ''; }
 2314:     $coursedombuf{$env{'request.course.id'}}=
 2315:        $env{'course.'.$env{'request.course.id'}.'.domain'};
 2316:     $coursenumbuf{$env{'request.course.id'}}=
 2317:        $env{'course.'.$env{'request.course.id'}.'.num'};
 2318:     $coursehombuf{$env{'request.course.id'}}=
 2319:        $env{'course.'.$env{'request.course.id'}.'.home'};
 2320:     $coursedescrbuf{$env{'request.course.id'}}=
 2321:        $env{'course.'.$env{'request.course.id'}.'.description'};
 2322:     $courseinstcodebuf{$env{'request.course.id'}}=
 2323:        $env{'course.'.$env{'request.course.id'}.'.internal.coursecode'};
 2324:     $courseownerbuf{$env{'request.course.id'}}=
 2325:        $env{'course.'.$env{'request.course.id'}.'.internal.courseowner'};
 2326:     $coursetypebuf{$env{'request.course.id'}}=
 2327:        $env{'course.'.$env{'request.course.id'}.'.type'};
 2328:     if (defined $courselogs{$env{'request.course.id'}}) {
 2329: 	$courselogs{$env{'request.course.id'}}.='&'.$what;
 2330:     } else {
 2331: 	$courselogs{$env{'request.course.id'}}.=$what;
 2332:     }
 2333:     if (length($courselogs{$env{'request.course.id'}})>4048) {
 2334: 	&flushcourselogs();
 2335:     }
 2336: }
 2337: 
 2338: sub courseacclog {
 2339:     my $fnsymb=shift;
 2340:     unless ($env{'request.course.id'}) { return ''; }
 2341:     my $what=$fnsymb.':'.$env{'user.name'}.':'.$env{'user.domain'};
 2342:     if ($fnsymb=~/(problem|exam|quiz|assess|survey|form|task|page)$/) {
 2343:         $what.=':POST';
 2344:         # FIXME: Probably ought to escape things....
 2345: 	foreach my $key (keys(%env)) {
 2346:             if ($key=~/^form\.(.*)/) {
 2347: 		$what.=':'.$1.'='.$env{$key};
 2348:             }
 2349:         }
 2350:     } elsif ($fnsymb =~ m:^/adm/searchcat:) {
 2351:         # FIXME: We should not be depending on a form parameter that someone
 2352:         # editing lonsearchcat.pm might change in the future.
 2353:         if ($env{'form.phase'} eq 'course_search') {
 2354:             $what.= ':POST';
 2355:             # FIXME: Probably ought to escape things....
 2356:             foreach my $element ('courseexp','crsfulltext','crsrelated',
 2357:                                  'crsdiscuss') {
 2358:                 $what.=':'.$element.'='.$env{'form.'.$element};
 2359:             }
 2360:         }
 2361:     }
 2362:     &courselog($what);
 2363: }
 2364: 
 2365: sub countacc {
 2366:     my $url=&declutter(shift);
 2367:     return if (! defined($url) || $url eq '');
 2368:     unless ($env{'request.course.id'}) { return ''; }
 2369:     $accesshash{$env{'request.course.id'}.'___'.$url.'___course'}=1;
 2370:     my $key=$$.$processmarker.'_'.$dumpcount.'___'.$url.'___count';
 2371:     $accesshash{$key}++;
 2372: }
 2373: 
 2374: sub linklog {
 2375:     my ($from,$to)=@_;
 2376:     $from=&declutter($from);
 2377:     $to=&declutter($to);
 2378:     $accesshash{$from.'___'.$to.'___comefrom'}=1;
 2379:     $accesshash{$to.'___'.$from.'___goto'}=1;
 2380: }
 2381:   
 2382: sub userrolelog {
 2383:     my ($trole,$username,$domain,$area,$tstart,$tend)=@_;
 2384:     if (($trole=~/^ca/) || ($trole=~/^aa/) ||
 2385:         ($trole=~/^in/) || ($trole=~/^cc/) ||
 2386:         ($trole=~/^ep/) || ($trole=~/^cr/) ||
 2387:         ($trole=~/^ta/)) {
 2388:        my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
 2389:        $userrolehash
 2390:          {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
 2391:                     =$tend.':'.$tstart;
 2392:     }
 2393:     if (($env{'request.role'} =~ /dc\./) &&
 2394: 	(($trole=~/^au/) || ($trole=~/^in/) ||
 2395: 	 ($trole=~/^cc/) || ($trole=~/^ep/) ||
 2396: 	 ($trole=~/^cr/) || ($trole=~/^ta/))) {
 2397:        $userrolehash
 2398:          {$trole.':'.$username.':'.$domain.':'.$env{'user.name'}.':'.$env{'user.domain'}.':'}
 2399:                     =$tend.':'.$tstart;
 2400:     }
 2401:     if (($trole=~/^dc/) || ($trole=~/^ad/) ||
 2402:         ($trole=~/^li/) || ($trole=~/^li/) ||
 2403:         ($trole=~/^au/) || ($trole=~/^dg/) ||
 2404:         ($trole=~/^sc/)) {
 2405:        my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
 2406:        $domainrolehash
 2407:          {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
 2408:                     = $tend.':'.$tstart;
 2409:     }
 2410: }
 2411: 
 2412: sub get_course_adv_roles {
 2413:     my $cid=shift;
 2414:     $cid=$env{'request.course.id'} unless (defined($cid));
 2415:     my %coursehash=&coursedescription($cid);
 2416:     my %nothide=();
 2417:     foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
 2418:         if ($user !~ /:/) {
 2419: 	    $nothide{join(':',split(/[\@]/,$user))}=1;
 2420:         } else {
 2421:             $nothide{$user}=1;
 2422:         }
 2423:     }
 2424:     my %returnhash=();
 2425:     my %dumphash=
 2426:             &dump('nohist_userroles',$coursehash{'domain'},$coursehash{'num'});
 2427:     my $now=time;
 2428:     foreach my $entry (keys %dumphash) {
 2429: 	my ($tend,$tstart)=split(/\:/,$dumphash{$entry});
 2430:         if (($tstart) && ($tstart<0)) { next; }
 2431:         if (($tend) && ($tend<$now)) { next; }
 2432:         if (($tstart) && ($now<$tstart)) { next; }
 2433:         my ($role,$username,$domain,$section)=split(/\:/,$entry);
 2434: 	if ($username eq '' || $domain eq '') { next; }
 2435: 	if ((&privileged($username,$domain)) && 
 2436: 	    (!$nothide{$username.':'.$domain})) { next; }
 2437: 	if ($role eq 'cr') { next; }
 2438:         my $key=&plaintext($role);
 2439:         if ($section) { $key.=' (Sec/Grp '.$section.')'; }
 2440:         if ($returnhash{$key}) {
 2441: 	    $returnhash{$key}.=','.$username.':'.$domain;
 2442:         } else {
 2443:             $returnhash{$key}=$username.':'.$domain;
 2444:         }
 2445:      }
 2446:     return %returnhash;
 2447: }
 2448: 
 2449: sub get_my_roles {
 2450:     my ($uname,$udom,$context,$types,$roles,$roledoms,$withsec,$hidepriv)=@_;
 2451:     unless (defined($uname)) { $uname=$env{'user.name'}; }
 2452:     unless (defined($udom)) { $udom=$env{'user.domain'}; }
 2453:     my (%dumphash,%nothide);
 2454:     if ($context eq 'userroles') { 
 2455:         %dumphash = &dump('roles',$udom,$uname);
 2456:     } else {
 2457:         %dumphash=
 2458:             &dump('nohist_userroles',$udom,$uname);
 2459:         if ($hidepriv) {
 2460:             my %coursehash=&coursedescription($udom.'_'.$uname);
 2461:             foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
 2462:                 if ($user !~ /:/) {
 2463:                     $nothide{join(':',split(/[\@]/,$user))} = 1;
 2464:                 } else {
 2465:                     $nothide{$user} = 1;
 2466:                 }
 2467:             }
 2468:         }
 2469:     }
 2470:     my %returnhash=();
 2471:     my $now=time;
 2472:     foreach my $entry (keys(%dumphash)) {
 2473:         my ($role,$tend,$tstart);
 2474:         if ($context eq 'userroles') {
 2475: 	    ($role,$tend,$tstart)=split(/_/,$dumphash{$entry});
 2476:         } else {
 2477:             ($tend,$tstart)=split(/\:/,$dumphash{$entry});
 2478:         }
 2479:         if (($tstart) && ($tstart<0)) { next; }
 2480:         my $status = 'active';
 2481:         if (($tend) && ($tend<=$now)) {
 2482:             $status = 'previous';
 2483:         } 
 2484:         if (($tstart) && ($now<$tstart)) {
 2485:             $status = 'future';
 2486:         }
 2487:         if (ref($types) eq 'ARRAY') {
 2488:             if (!grep(/^\Q$status\E$/,@{$types})) {
 2489:                 next;
 2490:             } 
 2491:         } else {
 2492:             if ($status ne 'active') {
 2493:                 next;
 2494:             }
 2495:         }
 2496:         my ($rolecode,$username,$domain,$section,$area);
 2497:         if ($context eq 'userroles') {
 2498:             ($area,$rolecode) = split(/_/,$entry);
 2499:             (undef,$domain,$username,$section) = split(/\//,$area);
 2500:         } else {
 2501:             ($role,$username,$domain,$section) = split(/\:/,$entry);
 2502:         }
 2503:         if (ref($roledoms) eq 'ARRAY') {
 2504:             if (!grep(/^\Q$domain\E$/,@{$roledoms})) {
 2505:                 next;
 2506:             }
 2507:         }
 2508:         if (ref($roles) eq 'ARRAY') {
 2509:             if (!grep(/^\Q$role\E$/,@{$roles})) {
 2510:                 if ($role =~ /^cr\//) {
 2511:                     if (!grep(/^cr$/,@{$roles})) {
 2512:                         next;
 2513:                     }
 2514:                 } else {
 2515:                     next;
 2516:                 }
 2517:             }
 2518:         }
 2519:         if ($hidepriv) {
 2520:             if ((&privileged($username,$domain)) &&
 2521:                 (!$nothide{$username.':'.$domain})) { 
 2522:                 next;
 2523:             }
 2524:         }
 2525:         if ($withsec) {
 2526:             $returnhash{$username.':'.$domain.':'.$role.':'.$section} =
 2527:                 $tstart.':'.$tend;
 2528:         } else {
 2529:             $returnhash{$username.':'.$domain.':'.$role}=$tstart.':'.$tend;
 2530:         }
 2531:     }
 2532:     return %returnhash;
 2533: }
 2534: 
 2535: # ----------------------------------------------------- Frontpage Announcements
 2536: #
 2537: #
 2538: 
 2539: sub postannounce {
 2540:     my ($server,$text)=@_;
 2541:     unless (&allowed('psa',&host_domain($server))) { return 'refused'; }
 2542:     unless ($text=~/\w/) { $text=''; }
 2543:     return &reply('setannounce:'.&escape($text),$server);
 2544: }
 2545: 
 2546: sub getannounce {
 2547: 
 2548:     if (open(my $fh,$perlvar{'lonDocRoot'}.'/announcement.txt')) {
 2549: 	my $announcement='';
 2550: 	while (my $line = <$fh>) { $announcement .= $line; }
 2551: 	close($fh);
 2552: 	if ($announcement=~/\w/) { 
 2553: 	    return 
 2554:    '<table bgcolor="#FF5555" cellpadding="5" cellspacing="3">'.
 2555:    '<tr><td bgcolor="#FFFFFF"><tt>'.$announcement.'</tt></td></tr></table>'; 
 2556: 	} else {
 2557: 	    return '';
 2558: 	}
 2559:     } else {
 2560: 	return '';
 2561:     }
 2562: }
 2563: 
 2564: # ---------------------------------------------------------- Course ID routines
 2565: # Deal with domain's nohist_courseid.db files
 2566: #
 2567: 
 2568: sub courseidput {
 2569:     my ($domain,$storehash,$coursehome,$caller) = @_;
 2570:     my $outcome;
 2571:     if ($caller eq 'timeonly') {
 2572:         my $cids = '';
 2573:         foreach my $item (keys(%$storehash)) {
 2574:             $cids.=&escape($item).'&';
 2575:         }
 2576:         $cids=~s/\&$//;
 2577:         $outcome = &reply('courseidputhash:'.$domain.':'.$caller.':'.$cids,
 2578:                           $coursehome);       
 2579:     } else {
 2580:         my $items = '';
 2581:         foreach my $item (keys(%$storehash)) {
 2582:             $items.= &escape($item).'='.
 2583:                      &freeze_escape($$storehash{$item}).'&';
 2584:         }
 2585:         $items=~s/\&$//;
 2586:         $outcome = &reply('courseidputhash:'.$domain.':'.$caller.':'.$items,
 2587:                           $coursehome);
 2588:     }
 2589:     if ($outcome eq 'unknown_cmd') {
 2590:         my $what;
 2591:         foreach my $cid (keys(%$storehash)) {
 2592:             $what .= &escape($cid).'=';
 2593:             foreach my $item ('description','inst_code','owner','type') {
 2594:                 $what .= &escape($storehash->{$cid}{$item}).':';
 2595:             }
 2596:             $what =~ s/\:$/&/;
 2597:         }
 2598:         $what =~ s/\&$//;  
 2599:         return &reply('courseidput:'.$domain.':'.$what,$coursehome);
 2600:     } else {
 2601:         return $outcome;
 2602:     }
 2603: }
 2604: 
 2605: sub courseiddump {
 2606:     my ($domfilter,$descfilter,$sincefilter,$instcodefilter,$ownerfilter,
 2607:         $coursefilter,$hostidflag,$hostidref,$typefilter,$regexp_ok)=@_;
 2608:     my $as_hash = 1;
 2609:     my %returnhash;
 2610:     if (!$domfilter) { $domfilter=''; }
 2611:     my %libserv = &all_library();
 2612:     foreach my $tryserver (keys(%libserv)) {
 2613:         if ( (  $hostidflag == 1 
 2614: 	        && grep(/^\Q$tryserver\E$/,@{$hostidref}) ) 
 2615: 	     || (!defined($hostidflag)) ) {
 2616: 
 2617: 	    if (($domfilter eq '') ||
 2618: 		(&host_domain($tryserver) eq $domfilter)) {
 2619:                 my $rep = 
 2620:                   &reply('courseiddump:'.&host_domain($tryserver).':'.
 2621:                          $sincefilter.':'.&escape($descfilter).':'.
 2622:                          &escape($instcodefilter).':'.&escape($ownerfilter).
 2623:                          ':'.&escape($coursefilter).':'.&escape($typefilter).
 2624:                          ':'.&escape($regexp_ok).':'.$as_hash,$tryserver);
 2625:                 my @pairs=split(/\&/,$rep);
 2626:                 foreach my $item (@pairs) {
 2627:                     my ($key,$value)=split(/\=/,$item,2);
 2628:                     $key = &unescape($key);
 2629:                     next if ($key =~ /^error: 2 /);
 2630:                     my $result = &thaw_unescape($value);
 2631:                     if (ref($result) eq 'HASH') {
 2632:                         $returnhash{$key}=$result;
 2633:                     } else {
 2634:                         my @responses = split(/:/,$value);
 2635:                         my @items = ('description','inst_code','owner','type');
 2636:                         for (my $i=0; $i<@responses; $i++) {
 2637:                             $returnhash{$key}{$items[$i]} = &unescape($responses[$i]);
 2638:                         }
 2639:                     } 
 2640:                 }
 2641:             }
 2642:         }
 2643:     }
 2644:     return %returnhash;
 2645: }
 2646: 
 2647: # ---------------------------------------------------------- DC e-mail
 2648: 
 2649: sub dcmailput {
 2650:     my ($domain,$msgid,$message,$server)=@_;
 2651:     my $status = &Apache::lonnet::critical(
 2652:        'dcmailput:'.$domain.':'.&escape($msgid).'='.
 2653:        &escape($message),$server);
 2654:     return $status;
 2655: }
 2656: 
 2657: sub dcmaildump {
 2658:     my ($dom,$startdate,$enddate,$senders) = @_;
 2659:     my %returnhash=();
 2660: 
 2661:     if (defined(&domain($dom,'primary'))) {
 2662:         my $cmd='dcmaildump:'.$dom.':'.&escape($startdate).':'.
 2663:                                                          &escape($enddate).':';
 2664: 	my @esc_senders=map { &escape($_)} @$senders;
 2665: 	$cmd.=&escape(join('&',@esc_senders));
 2666: 	foreach my $line (split(/\&/,&reply($cmd,&domain($dom,'primary')))) {
 2667:             my ($key,$value) = split(/\=/,$line,2);
 2668:             if (($key) && ($value)) {
 2669:                 $returnhash{&unescape($key)} = &unescape($value);
 2670:             }
 2671:         }
 2672:     }
 2673:     return %returnhash;
 2674: }
 2675: # ---------------------------------------------------------- Domain roles
 2676: 
 2677: sub get_domain_roles {
 2678:     my ($dom,$roles,$startdate,$enddate)=@_;
 2679:     if (undef($startdate) || $startdate eq '') {
 2680:         $startdate = '.';
 2681:     }
 2682:     if (undef($enddate) || $enddate eq '') {
 2683:         $enddate = '.';
 2684:     }
 2685:     my $rolelist;
 2686:     if (ref($roles) eq 'ARRAY') {
 2687:         $rolelist = join(':',@{$roles});
 2688:     }
 2689:     my %personnel = ();
 2690: 
 2691:     my %servers = &get_servers($dom,'library');
 2692:     foreach my $tryserver (keys(%servers)) {
 2693: 	%{$personnel{$tryserver}}=();
 2694: 	foreach my $line (split(/\&/,&reply('domrolesdump:'.$dom.':'.
 2695: 					    &escape($startdate).':'.
 2696: 					    &escape($enddate).':'.
 2697: 					    &escape($rolelist), $tryserver))) {
 2698: 	    my ($key,$value) = split(/\=/,$line,2);
 2699: 	    if (($key) && ($value)) {
 2700: 		$personnel{$tryserver}{&unescape($key)} = &unescape($value);
 2701: 	    }
 2702: 	}
 2703:     }
 2704:     return %personnel;
 2705: }
 2706: 
 2707: # ----------------------------------------------------------- Check out an item
 2708: 
 2709: sub get_first_access {
 2710:     my ($type,$argsymb)=@_;
 2711:     my ($symb,$courseid,$udom,$uname)=&whichuser();
 2712:     if ($argsymb) { $symb=$argsymb; }
 2713:     my ($map,$id,$res)=&decode_symb($symb);
 2714:     if ($type eq 'course') {
 2715: 	$res='course';
 2716:     } elsif ($type eq 'map') {
 2717: 	$res=&symbread($map);
 2718:     } else {
 2719: 	$res=$symb;
 2720:     }
 2721:     my %times=&get('firstaccesstimes',["$courseid\0$res"],$udom,$uname);
 2722:     return $times{"$courseid\0$res"};
 2723: }
 2724: 
 2725: sub set_first_access {
 2726:     my ($type)=@_;
 2727:     my ($symb,$courseid,$udom,$uname)=&whichuser();
 2728:     my ($map,$id,$res)=&decode_symb($symb);
 2729:     if ($type eq 'course') {
 2730: 	$res='course';
 2731:     } elsif ($type eq 'map') {
 2732: 	$res=&symbread($map);
 2733:     } else {
 2734: 	$res=$symb;
 2735:     }
 2736:     my $firstaccess=&get_first_access($type,$symb);
 2737:     if (!$firstaccess) {
 2738: 	return &put('firstaccesstimes',{"$courseid\0$res"=>time},$udom,$uname);
 2739:     }
 2740:     return 'already_set';
 2741: }
 2742: 
 2743: sub checkout {
 2744:     my ($symb,$tuname,$tudom,$tcrsid)=@_;
 2745:     my $now=time;
 2746:     my $lonhost=$perlvar{'lonHostID'};
 2747:     my $infostr=&escape(
 2748:                  'CHECKOUTTOKEN&'.
 2749:                  $tuname.'&'.
 2750:                  $tudom.'&'.
 2751:                  $tcrsid.'&'.
 2752:                  $symb.'&'.
 2753: 		 $now.'&'.$ENV{'REMOTE_ADDR'});
 2754:     my $token=&reply('tmpput:'.$infostr,$lonhost);
 2755:     if ($token=~/^error\:/) { 
 2756:         &logthis("<font color=\"blue\">WARNING: ".
 2757:                 "Checkout tmpput failed ".$tudom.' - '.$tuname.' - '.$symb.
 2758:                  "</font>");
 2759:         return ''; 
 2760:     }
 2761: 
 2762:     $token=~s/^(\d+)\_.*\_(\d+)$/$1\*$2\*$lonhost/;
 2763:     $token=~tr/a-z/A-Z/;
 2764: 
 2765:     my %infohash=('resource.0.outtoken' => $token,
 2766:                   'resource.0.checkouttime' => $now,
 2767:                   'resource.0.outremote' => $ENV{'REMOTE_ADDR'});
 2768: 
 2769:     unless (&cstore(\%infohash,$symb,$tcrsid,$tudom,$tuname) eq 'ok') {
 2770:        return '';
 2771:     } else {
 2772:         &logthis("<font color=\"blue\">WARNING: ".
 2773:                 "Checkout cstore failed ".$tudom.' - '.$tuname.' - '.$symb.
 2774:                  "</font>");
 2775:     }    
 2776: 
 2777:     if (&log($tudom,$tuname,&homeserver($tuname,$tudom),
 2778:                          &escape('Checkout '.$infostr.' - '.
 2779:                                                  $token)) ne 'ok') {
 2780: 	return '';
 2781:     } else {
 2782:         &logthis("<font color=\"blue\">WARNING: ".
 2783:                 "Checkout log failed ".$tudom.' - '.$tuname.' - '.$symb.
 2784:                  "</font>");
 2785:     }
 2786:     return $token;
 2787: }
 2788: 
 2789: # ------------------------------------------------------------ Check in an item
 2790: 
 2791: sub checkin {
 2792:     my $token=shift;
 2793:     my $now=time;
 2794:     my ($ta,$tb,$lonhost)=split(/\*/,$token);
 2795:     $lonhost=~tr/A-Z/a-z/;
 2796:     my $dtoken=$ta.'_'.&hostname($lonhost).'_'.$tb;
 2797:     $dtoken=~s/\W/\_/g;
 2798:     my ($dummy,$tuname,$tudom,$tcrsid,$symb,$chtim,$rmaddr)=
 2799:                  split(/\&/,&unescape(&reply('tmpget:'.$dtoken,$lonhost)));
 2800: 
 2801:     unless (($tuname) && ($tudom)) {
 2802:         &logthis('Check in '.$token.' ('.$dtoken.') failed');
 2803:         return '';
 2804:     }
 2805:     
 2806:     unless (&allowed('mgr',$tcrsid)) {
 2807:         &logthis('Check in '.$token.' ('.$dtoken.') unauthorized: '.
 2808:                  $env{'user.name'}.' - '.$env{'user.domain'});
 2809:         return '';
 2810:     }
 2811: 
 2812:     my %infohash=('resource.0.intoken' => $token,
 2813:                   'resource.0.checkintime' => $now,
 2814:                   'resource.0.inremote' => $ENV{'REMOTE_ADDR'});
 2815: 
 2816:     unless (&cstore(\%infohash,$symb,$tcrsid,$tudom,$tuname) eq 'ok') {
 2817:        return '';
 2818:     }    
 2819: 
 2820:     if (&log($tudom,$tuname,&homeserver($tuname,$tudom),
 2821:                          &escape('Checkin - '.$token)) ne 'ok') {
 2822: 	return '';
 2823:     }
 2824: 
 2825:     return ($symb,$tuname,$tudom,$tcrsid);    
 2826: }
 2827: 
 2828: # --------------------------------------------- Set Expire Date for Spreadsheet
 2829: 
 2830: sub expirespread {
 2831:     my ($uname,$udom,$stype,$usymb)=@_;
 2832:     my $cid=$env{'request.course.id'}; 
 2833:     if ($cid) {
 2834:        my $now=time;
 2835:        my $key=$uname.':'.$udom.':'.$stype.':'.$usymb;
 2836:        return &reply('put:'.$env{'course.'.$cid.'.domain'}.':'.
 2837:                             $env{'course.'.$cid.'.num'}.
 2838: 	        	    ':nohist_expirationdates:'.
 2839:                             &escape($key).'='.$now,
 2840:                             $env{'course.'.$cid.'.home'})
 2841:     }
 2842:     return 'ok';
 2843: }
 2844: 
 2845: # ----------------------------------------------------- Devalidate Spreadsheets
 2846: 
 2847: sub devalidate {
 2848:     my ($symb,$uname,$udom)=@_;
 2849:     my $cid=$env{'request.course.id'}; 
 2850:     if ($cid) {
 2851:         # delete the stored spreadsheets for
 2852:         # - the student level sheet of this user in course's homespace
 2853:         # - the assessment level sheet for this resource 
 2854:         #   for this user in user's homespace
 2855: 	# - current conditional state info
 2856: 	my $key=$uname.':'.$udom.':';
 2857:         my $status=
 2858: 	    &del('nohist_calculatedsheets',
 2859: 		 [$key.'studentcalc:'],
 2860: 		 $env{'course.'.$cid.'.domain'},
 2861: 		 $env{'course.'.$cid.'.num'})
 2862: 		.' '.
 2863: 	    &del('nohist_calculatedsheets_'.$cid,
 2864: 		 [$key.'assesscalc:'.$symb],$udom,$uname);
 2865:         unless ($status eq 'ok ok') {
 2866:            &logthis('Could not devalidate spreadsheet '.
 2867:                     $uname.' at '.$udom.' for '.
 2868: 		    $symb.': '.$status);
 2869:         }
 2870: 	&delenv('user.state.'.$cid);
 2871:     }
 2872: }
 2873: 
 2874: sub get_scalar {
 2875:     my ($string,$end) = @_;
 2876:     my $value;
 2877:     if ($$string =~ s/^([^&]*?)($end)/$2/) {
 2878: 	$value = $1;
 2879:     } elsif ($$string =~ s/^([^&]*?)&//) {
 2880: 	$value = $1;
 2881:     }
 2882:     return &unescape($value);
 2883: }
 2884: 
 2885: sub array2str {
 2886:   my (@array) = @_;
 2887:   my $result=&arrayref2str(\@array);
 2888:   $result=~s/^__ARRAY_REF__//;
 2889:   $result=~s/__END_ARRAY_REF__$//;
 2890:   return $result;
 2891: }
 2892: 
 2893: sub arrayref2str {
 2894:   my ($arrayref) = @_;
 2895:   my $result='__ARRAY_REF__';
 2896:   foreach my $elem (@$arrayref) {
 2897:     if(ref($elem) eq 'ARRAY') {
 2898:       $result.=&arrayref2str($elem).'&';
 2899:     } elsif(ref($elem) eq 'HASH') {
 2900:       $result.=&hashref2str($elem).'&';
 2901:     } elsif(ref($elem)) {
 2902:       #print("Got a ref of ".(ref($elem))." skipping.");
 2903:     } else {
 2904:       $result.=&escape($elem).'&';
 2905:     }
 2906:   }
 2907:   $result=~s/\&$//;
 2908:   $result .= '__END_ARRAY_REF__';
 2909:   return $result;
 2910: }
 2911: 
 2912: sub hash2str {
 2913:   my (%hash) = @_;
 2914:   my $result=&hashref2str(\%hash);
 2915:   $result=~s/^__HASH_REF__//;
 2916:   $result=~s/__END_HASH_REF__$//;
 2917:   return $result;
 2918: }
 2919: 
 2920: sub hashref2str {
 2921:   my ($hashref)=@_;
 2922:   my $result='__HASH_REF__';
 2923:   foreach my $key (sort(keys(%$hashref))) {
 2924:     if (ref($key) eq 'ARRAY') {
 2925:       $result.=&arrayref2str($key).'=';
 2926:     } elsif (ref($key) eq 'HASH') {
 2927:       $result.=&hashref2str($key).'=';
 2928:     } elsif (ref($key)) {
 2929:       $result.='=';
 2930:       #print("Got a ref of ".(ref($key))." skipping.");
 2931:     } else {
 2932: 	if ($key) {$result.=&escape($key).'=';} else { last; }
 2933:     }
 2934: 
 2935:     if(ref($hashref->{$key}) eq 'ARRAY') {
 2936:       $result.=&arrayref2str($hashref->{$key}).'&';
 2937:     } elsif(ref($hashref->{$key}) eq 'HASH') {
 2938:       $result.=&hashref2str($hashref->{$key}).'&';
 2939:     } elsif(ref($hashref->{$key})) {
 2940:        $result.='&';
 2941:       #print("Got a ref of ".(ref($hashref->{$key}))." skipping.");
 2942:     } else {
 2943:       $result.=&escape($hashref->{$key}).'&';
 2944:     }
 2945:   }
 2946:   $result=~s/\&$//;
 2947:   $result .= '__END_HASH_REF__';
 2948:   return $result;
 2949: }
 2950: 
 2951: sub str2hash {
 2952:     my ($string)=@_;
 2953:     my ($hash)=&str2hashref('__HASH_REF__'.$string.'__END_HASH_REF__');
 2954:     return %$hash;
 2955: }
 2956: 
 2957: sub str2hashref {
 2958:   my ($string) = @_;
 2959: 
 2960:   my %hash;
 2961: 
 2962:   if($string !~ /^__HASH_REF__/) {
 2963:       if (! ($string eq '' || !defined($string))) {
 2964: 	  $hash{'error'}='Not hash reference';
 2965:       }
 2966:       return (\%hash, $string);
 2967:   }
 2968: 
 2969:   $string =~ s/^__HASH_REF__//;
 2970: 
 2971:   while($string !~ /^__END_HASH_REF__/) {
 2972:       #key
 2973:       my $key='';
 2974:       if($string =~ /^__HASH_REF__/) {
 2975:           ($key, $string)=&str2hashref($string);
 2976:           if(defined($key->{'error'})) {
 2977:               $hash{'error'}='Bad data';
 2978:               return (\%hash, $string);
 2979:           }
 2980:       } elsif($string =~ /^__ARRAY_REF__/) {
 2981:           ($key, $string)=&str2arrayref($string);
 2982:           if($key->[0] eq 'Array reference error') {
 2983:               $hash{'error'}='Bad data';
 2984:               return (\%hash, $string);
 2985:           }
 2986:       } else {
 2987:           $string =~ s/^(.*?)=//;
 2988: 	  $key=&unescape($1);
 2989:       }
 2990:       $string =~ s/^=//;
 2991: 
 2992:       #value
 2993:       my $value='';
 2994:       if($string =~ /^__HASH_REF__/) {
 2995:           ($value, $string)=&str2hashref($string);
 2996:           if(defined($value->{'error'})) {
 2997:               $hash{'error'}='Bad data';
 2998:               return (\%hash, $string);
 2999:           }
 3000:       } elsif($string =~ /^__ARRAY_REF__/) {
 3001:           ($value, $string)=&str2arrayref($string);
 3002:           if($value->[0] eq 'Array reference error') {
 3003:               $hash{'error'}='Bad data';
 3004:               return (\%hash, $string);
 3005:           }
 3006:       } else {
 3007: 	  $value=&get_scalar(\$string,'__END_HASH_REF__');
 3008:       }
 3009:       $string =~ s/^&//;
 3010: 
 3011:       $hash{$key}=$value;
 3012:   }
 3013: 
 3014:   $string =~ s/^__END_HASH_REF__//;
 3015: 
 3016:   return (\%hash, $string);
 3017: }
 3018: 
 3019: sub str2array {
 3020:     my ($string)=@_;
 3021:     my ($array)=&str2arrayref('__ARRAY_REF__'.$string.'__END_ARRAY_REF__');
 3022:     return @$array;
 3023: }
 3024: 
 3025: sub str2arrayref {
 3026:   my ($string) = @_;
 3027:   my @array;
 3028: 
 3029:   if($string !~ /^__ARRAY_REF__/) {
 3030:       if (! ($string eq '' || !defined($string))) {
 3031: 	  $array[0]='Array reference error';
 3032:       }
 3033:       return (\@array, $string);
 3034:   }
 3035: 
 3036:   $string =~ s/^__ARRAY_REF__//;
 3037: 
 3038:   while($string !~ /^__END_ARRAY_REF__/) {
 3039:       my $value='';
 3040:       if($string =~ /^__HASH_REF__/) {
 3041:           ($value, $string)=&str2hashref($string);
 3042:           if(defined($value->{'error'})) {
 3043:               $array[0] ='Array reference error';
 3044:               return (\@array, $string);
 3045:           }
 3046:       } elsif($string =~ /^__ARRAY_REF__/) {
 3047:           ($value, $string)=&str2arrayref($string);
 3048:           if($value->[0] eq 'Array reference error') {
 3049:               $array[0] ='Array reference error';
 3050:               return (\@array, $string);
 3051:           }
 3052:       } else {
 3053: 	  $value=&get_scalar(\$string,'__END_ARRAY_REF__');
 3054:       }
 3055:       $string =~ s/^&//;
 3056: 
 3057:       push(@array, $value);
 3058:   }
 3059: 
 3060:   $string =~ s/^__END_ARRAY_REF__//;
 3061: 
 3062:   return (\@array, $string);
 3063: }
 3064: 
 3065: # -------------------------------------------------------------------Temp Store
 3066: 
 3067: sub tmpreset {
 3068:   my ($symb,$namespace,$domain,$stuname) = @_;
 3069:   if (!$symb) {
 3070:     $symb=&symbread();
 3071:     if (!$symb) { $symb= $env{'request.url'}; }
 3072:   }
 3073:   $symb=escape($symb);
 3074: 
 3075:   if (!$namespace) { $namespace=$env{'request.state'}; }
 3076:   $namespace=~s/\//\_/g;
 3077:   $namespace=~s/\W//g;
 3078: 
 3079:   if (!$domain) { $domain=$env{'user.domain'}; }
 3080:   if (!$stuname) { $stuname=$env{'user.name'}; }
 3081:   if ($domain eq 'public' && $stuname eq 'public') {
 3082:       $stuname=$ENV{'REMOTE_ADDR'};
 3083:   }
 3084:   my $path=$perlvar{'lonDaemons'}.'/tmp';
 3085:   my %hash;
 3086:   if (tie(%hash,'GDBM_File',
 3087: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
 3088: 	  &GDBM_WRCREAT(),0640)) {
 3089:     foreach my $key (keys %hash) {
 3090:       if ($key=~ /:$symb/) {
 3091: 	delete($hash{$key});
 3092:       }
 3093:     }
 3094:   }
 3095: }
 3096: 
 3097: sub tmpstore {
 3098:   my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
 3099: 
 3100:   if (!$symb) {
 3101:     $symb=&symbread();
 3102:     if (!$symb) { $symb= $env{'request.url'}; }
 3103:   }
 3104:   $symb=escape($symb);
 3105: 
 3106:   if (!$namespace) {
 3107:     # I don't think we would ever want to store this for a course.
 3108:     # it seems this will only be used if we don't have a course.
 3109:     #$namespace=$env{'request.course.id'};
 3110:     #if (!$namespace) {
 3111:       $namespace=$env{'request.state'};
 3112:     #}
 3113:   }
 3114:   $namespace=~s/\//\_/g;
 3115:   $namespace=~s/\W//g;
 3116:   if (!$domain) { $domain=$env{'user.domain'}; }
 3117:   if (!$stuname) { $stuname=$env{'user.name'}; }
 3118:   if ($domain eq 'public' && $stuname eq 'public') {
 3119:       $stuname=$ENV{'REMOTE_ADDR'};
 3120:   }
 3121:   my $now=time;
 3122:   my %hash;
 3123:   my $path=$perlvar{'lonDaemons'}.'/tmp';
 3124:   if (tie(%hash,'GDBM_File',
 3125: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
 3126: 	  &GDBM_WRCREAT(),0640)) {
 3127:     $hash{"version:$symb"}++;
 3128:     my $version=$hash{"version:$symb"};
 3129:     my $allkeys=''; 
 3130:     foreach my $key (keys(%$storehash)) {
 3131:       $allkeys.=$key.':';
 3132:       $hash{"$version:$symb:$key"}=&freeze_escape($$storehash{$key});
 3133:     }
 3134:     $hash{"$version:$symb:timestamp"}=$now;
 3135:     $allkeys.='timestamp';
 3136:     $hash{"$version:keys:$symb"}=$allkeys;
 3137:     if (untie(%hash)) {
 3138:       return 'ok';
 3139:     } else {
 3140:       return "error:$!";
 3141:     }
 3142:   } else {
 3143:     return "error:$!";
 3144:   }
 3145: }
 3146: 
 3147: # -----------------------------------------------------------------Temp Restore
 3148: 
 3149: sub tmprestore {
 3150:   my ($symb,$namespace,$domain,$stuname) = @_;
 3151: 
 3152:   if (!$symb) {
 3153:     $symb=&symbread();
 3154:     if (!$symb) { $symb= $env{'request.url'}; }
 3155:   }
 3156:   $symb=escape($symb);
 3157: 
 3158:   if (!$namespace) { $namespace=$env{'request.state'}; }
 3159: 
 3160:   if (!$domain) { $domain=$env{'user.domain'}; }
 3161:   if (!$stuname) { $stuname=$env{'user.name'}; }
 3162:   if ($domain eq 'public' && $stuname eq 'public') {
 3163:       $stuname=$ENV{'REMOTE_ADDR'};
 3164:   }
 3165:   my %returnhash;
 3166:   $namespace=~s/\//\_/g;
 3167:   $namespace=~s/\W//g;
 3168:   my %hash;
 3169:   my $path=$perlvar{'lonDaemons'}.'/tmp';
 3170:   if (tie(%hash,'GDBM_File',
 3171: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
 3172: 	  &GDBM_READER(),0640)) {
 3173:     my $version=$hash{"version:$symb"};
 3174:     $returnhash{'version'}=$version;
 3175:     my $scope;
 3176:     for ($scope=1;$scope<=$version;$scope++) {
 3177:       my $vkeys=$hash{"$scope:keys:$symb"};
 3178:       my @keys=split(/:/,$vkeys);
 3179:       my $key;
 3180:       $returnhash{"$scope:keys"}=$vkeys;
 3181:       foreach $key (@keys) {
 3182: 	$returnhash{"$scope:$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
 3183: 	$returnhash{"$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
 3184:       }
 3185:     }
 3186:     if (!(untie(%hash))) {
 3187:       return "error:$!";
 3188:     }
 3189:   } else {
 3190:     return "error:$!";
 3191:   }
 3192:   return %returnhash;
 3193: }
 3194: 
 3195: # ----------------------------------------------------------------------- Store
 3196: 
 3197: sub store {
 3198:     my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
 3199:     my $home='';
 3200: 
 3201:     if ($stuname) { $home=&homeserver($stuname,$domain); }
 3202: 
 3203:     $symb=&symbclean($symb);
 3204:     if (!$symb) { unless ($symb=&symbread()) { return ''; } }
 3205: 
 3206:     if (!$domain) { $domain=$env{'user.domain'}; }
 3207:     if (!$stuname) { $stuname=$env{'user.name'}; }
 3208: 
 3209:     &devalidate($symb,$stuname,$domain);
 3210: 
 3211:     $symb=escape($symb);
 3212:     if (!$namespace) { 
 3213:        unless ($namespace=$env{'request.course.id'}) { 
 3214:           return ''; 
 3215:        } 
 3216:     }
 3217:     if (!$home) { $home=$env{'user.home'}; }
 3218: 
 3219:     $$storehash{'ip'}=$ENV{'REMOTE_ADDR'};
 3220:     $$storehash{'host'}=$perlvar{'lonHostID'};
 3221: 
 3222:     my $namevalue='';
 3223:     foreach my $key (keys(%$storehash)) {
 3224:         $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
 3225:     }
 3226:     $namevalue=~s/\&$//;
 3227:     &courselog($symb.':'.$stuname.':'.$domain.':STORE:'.$namevalue);
 3228:     return reply("store:$domain:$stuname:$namespace:$symb:$namevalue","$home");
 3229: }
 3230: 
 3231: # -------------------------------------------------------------- Critical Store
 3232: 
 3233: sub cstore {
 3234:     my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
 3235:     my $home='';
 3236: 
 3237:     if ($stuname) { $home=&homeserver($stuname,$domain); }
 3238: 
 3239:     $symb=&symbclean($symb);
 3240:     if (!$symb) { unless ($symb=&symbread()) { return ''; } }
 3241: 
 3242:     if (!$domain) { $domain=$env{'user.domain'}; }
 3243:     if (!$stuname) { $stuname=$env{'user.name'}; }
 3244: 
 3245:     &devalidate($symb,$stuname,$domain);
 3246: 
 3247:     $symb=escape($symb);
 3248:     if (!$namespace) { 
 3249:        unless ($namespace=$env{'request.course.id'}) { 
 3250:           return ''; 
 3251:        } 
 3252:     }
 3253:     if (!$home) { $home=$env{'user.home'}; }
 3254: 
 3255:     $$storehash{'ip'}=$ENV{'REMOTE_ADDR'};
 3256:     $$storehash{'host'}=$perlvar{'lonHostID'};
 3257: 
 3258:     my $namevalue='';
 3259:     foreach my $key (keys(%$storehash)) {
 3260:         $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
 3261:     }
 3262:     $namevalue=~s/\&$//;
 3263:     &courselog($symb.':'.$stuname.':'.$domain.':CSTORE:'.$namevalue);
 3264:     return critical
 3265:                 ("store:$domain:$stuname:$namespace:$symb:$namevalue","$home");
 3266: }
 3267: 
 3268: # --------------------------------------------------------------------- Restore
 3269: 
 3270: sub restore {
 3271:     my ($symb,$namespace,$domain,$stuname) = @_;
 3272:     my $home='';
 3273: 
 3274:     if ($stuname) { $home=&homeserver($stuname,$domain); }
 3275: 
 3276:     if (!$symb) {
 3277:       unless ($symb=escape(&symbread())) { return ''; }
 3278:     } else {
 3279:       $symb=&escape(&symbclean($symb));
 3280:     }
 3281:     if (!$namespace) { 
 3282:        unless ($namespace=$env{'request.course.id'}) { 
 3283:           return ''; 
 3284:        } 
 3285:     }
 3286:     if (!$domain) { $domain=$env{'user.domain'}; }
 3287:     if (!$stuname) { $stuname=$env{'user.name'}; }
 3288:     if (!$home) { $home=$env{'user.home'}; }
 3289:     my $answer=&reply("restore:$domain:$stuname:$namespace:$symb","$home");
 3290: 
 3291:     my %returnhash=();
 3292:     foreach my $line (split(/\&/,$answer)) {
 3293: 	my ($name,$value)=split(/\=/,$line);
 3294:         $returnhash{&unescape($name)}=&thaw_unescape($value);
 3295:     }
 3296:     my $version;
 3297:     for ($version=1;$version<=$returnhash{'version'};$version++) {
 3298:        foreach my $item (split(/\:/,$returnhash{$version.':keys'})) {
 3299:           $returnhash{$item}=$returnhash{$version.':'.$item};
 3300:        }
 3301:     }
 3302:     return %returnhash;
 3303: }
 3304: 
 3305: # ---------------------------------------------------------- Course Description
 3306: 
 3307: sub coursedescription {
 3308:     my ($courseid,$args)=@_;
 3309:     $courseid=~s/^\///;
 3310:     $courseid=~s/\_/\//g;
 3311:     my ($cdomain,$cnum)=split(/\//,$courseid);
 3312:     my $chome=&homeserver($cnum,$cdomain);
 3313:     my $normalid=$cdomain.'_'.$cnum;
 3314:     # need to always cache even if we get errors otherwise we keep 
 3315:     # trying and trying and trying to get the course description.
 3316:     my %envhash=();
 3317:     my %returnhash=();
 3318:     
 3319:     my $expiretime=600;
 3320:     if ($env{'request.course.id'} eq $normalid) {
 3321: 	$expiretime=120;
 3322:     }
 3323: 
 3324:     my $prefix='course.'.$cdomain.'_'.$cnum.'.';
 3325:     if (!$args->{'freshen_cache'}
 3326: 	&& ((time-$env{$prefix.'last_cache'}) < $expiretime) ) {
 3327: 	foreach my $key (keys(%env)) {
 3328: 	    next if ($key !~ /^\Q$prefix\E(.*)/);
 3329: 	    my ($setting) = $1;
 3330: 	    $returnhash{$setting} = $env{$key};
 3331: 	}
 3332: 	return %returnhash;
 3333:     }
 3334: 
 3335:     # get the data agin
 3336:     if (!$args->{'one_time'}) {
 3337: 	$envhash{'course.'.$normalid.'.last_cache'}=time;
 3338:     }
 3339: 
 3340:     if ($chome ne 'no_host') {
 3341:        %returnhash=&dump('environment',$cdomain,$cnum);
 3342:        if (!exists($returnhash{'con_lost'})) {
 3343:            $returnhash{'home'}= $chome;
 3344: 	   $returnhash{'domain'} = $cdomain;
 3345: 	   $returnhash{'num'} = $cnum;
 3346:            if (!defined($returnhash{'type'})) {
 3347:                $returnhash{'type'} = 'Course';
 3348:            }
 3349:            while (my ($name,$value) = each %returnhash) {
 3350:                $envhash{'course.'.$normalid.'.'.$name}=$value;
 3351:            }
 3352:            $returnhash{'url'}=&clutter($returnhash{'url'});
 3353:            $returnhash{'fn'}=$perlvar{'lonDaemons'}.'/tmp/'.
 3354: 	       $env{'user.name'}.'_'.$cdomain.'_'.$cnum;
 3355:            $envhash{'course.'.$normalid.'.home'}=$chome;
 3356:            $envhash{'course.'.$normalid.'.domain'}=$cdomain;
 3357:            $envhash{'course.'.$normalid.'.num'}=$cnum;
 3358:        }
 3359:     }
 3360:     if (!$args->{'one_time'}) {
 3361: 	&appenv(%envhash);
 3362:     }
 3363:     return %returnhash;
 3364: }
 3365: 
 3366: # -------------------------------------------------See if a user is privileged
 3367: 
 3368: sub privileged {
 3369:     my ($username,$domain)=@_;
 3370:     my $rolesdump=&reply("dump:$domain:$username:roles",
 3371: 			&homeserver($username,$domain));
 3372:     if (($rolesdump eq 'con_lost') || ($rolesdump eq '')) { return 0; }
 3373:     my $now=time;
 3374:     if ($rolesdump ne '') {
 3375:         foreach my $entry (split(/&/,$rolesdump)) {
 3376: 	    if ($entry!~/^rolesdef_/) {
 3377: 		my ($area,$role)=split(/=/,$entry);
 3378: 		$area=~s/\_\w\w$//;
 3379: 		my ($trole,$tend,$tstart)=split(/_/,$role);
 3380: 		if (($trole eq 'dc') || ($trole eq 'su')) {
 3381: 		    my $active=1;
 3382: 		    if ($tend) {
 3383: 			if ($tend<$now) { $active=0; }
 3384: 		    }
 3385: 		    if ($tstart) {
 3386: 			if ($tstart>$now) { $active=0; }
 3387: 		    }
 3388: 		    if ($active) { return 1; }
 3389: 		}
 3390: 	    }
 3391: 	}
 3392:     }
 3393:     return 0;
 3394: }
 3395: 
 3396: # -------------------------------------------------------- Get user privileges
 3397: 
 3398: sub rolesinit {
 3399:     my ($domain,$username,$authhost)=@_;
 3400:     my $rolesdump=reply("dump:$domain:$username:roles",$authhost);
 3401:     if (($rolesdump eq 'con_lost') || ($rolesdump eq '')) { return ''; }
 3402:     my %allroles=();
 3403:     my %allgroups=();   
 3404:     my $now=time;
 3405:     my %userroles = ('user.login.time' => $now);
 3406:     my $group_privs;
 3407: 
 3408:     if ($rolesdump ne '') {
 3409:         foreach my $entry (split(/&/,$rolesdump)) {
 3410: 	  if ($entry!~/^rolesdef_/) {
 3411:             my ($area,$role)=split(/=/,$entry);
 3412: 	    $area=~s/\_\w\w$//;
 3413:             my ($trole,$tend,$tstart,$group_privs);
 3414: 	    if ($role=~/^cr/) { 
 3415: 		if ($role=~m|^(cr/$match_domain/$match_username/[a-zA-Z0-9]+)_(.*)$|) {
 3416: 		    ($trole,my $trest)=($role=~m|^(cr/$match_domain/$match_username/[a-zA-Z0-9]+)_(.*)$|);
 3417: 		    ($tend,$tstart)=split('_',$trest);
 3418: 		} else {
 3419: 		    $trole=$role;
 3420: 		}
 3421:             } elsif ($role =~ m|^gr/|) {
 3422:                 ($trole,$tend,$tstart) = split(/_/,$role);
 3423:                 ($trole,$group_privs) = split(/\//,$trole);
 3424:                 $group_privs = &unescape($group_privs);
 3425: 	    } else {
 3426: 		($trole,$tend,$tstart)=split(/_/,$role);
 3427: 	    }
 3428: 	    my %new_role = &set_arearole($trole,$area,$tstart,$tend,$domain,
 3429: 					 $username);
 3430: 	    @userroles{keys(%new_role)} = @new_role{keys(%new_role)};
 3431:             if (($tend!=0) && ($tend<$now)) { $trole=''; }
 3432:             if (($tstart!=0) && ($tstart>$now)) { $trole=''; }
 3433:             if (($area ne '') && ($trole ne '')) {
 3434: 		my $spec=$trole.'.'.$area;
 3435: 		my ($tdummy,$tdomain,$trest)=split(/\//,$area);
 3436: 		if ($trole =~ /^cr\//) {
 3437:                     &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
 3438:                 } elsif ($trole eq 'gr') {
 3439:                     &group_roleprivs(\%allgroups,$area,$group_privs,$tend,$tstart);
 3440: 		} else {
 3441:                     &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
 3442: 		}
 3443:             }
 3444:           }
 3445:         }
 3446:         my ($author,$adv) = &set_userprivs(\%userroles,\%allroles,\%allgroups);
 3447:         $userroles{'user.adv'}    = $adv;
 3448: 	$userroles{'user.author'} = $author;
 3449:         $env{'user.adv'}=$adv;
 3450:     }
 3451:     return \%userroles;  
 3452: }
 3453: 
 3454: sub set_arearole {
 3455:     my ($trole,$area,$tstart,$tend,$domain,$username) = @_;
 3456: # log the associated role with the area
 3457:     &userrolelog($trole,$username,$domain,$area,$tstart,$tend);
 3458:     return ('user.role.'.$trole.'.'.$area => $tstart.'.'.$tend);
 3459: }
 3460: 
 3461: sub custom_roleprivs {
 3462:     my ($allroles,$trole,$tdomain,$trest,$spec,$area) = @_;
 3463:     my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$trole);
 3464:     my $homsvr=homeserver($rauthor,$rdomain);
 3465:     if (&hostname($homsvr) ne '') {
 3466:         my ($rdummy,$roledef)=
 3467:             &get('roles',["rolesdef_$rrole"],$rdomain,$rauthor);
 3468:         if (($rdummy ne 'con_lost') && ($roledef ne '')) {
 3469:             my ($syspriv,$dompriv,$coursepriv)=split(/\_/,$roledef);
 3470:             if (defined($syspriv)) {
 3471:                 $$allroles{'cm./'}.=':'.$syspriv;
 3472:                 $$allroles{$spec.'./'}.=':'.$syspriv;
 3473:             }
 3474:             if ($tdomain ne '') {
 3475:                 if (defined($dompriv)) {
 3476:                     $$allroles{'cm./'.$tdomain.'/'}.=':'.$dompriv;
 3477:                     $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$dompriv;
 3478:                 }
 3479:                 if (($trest ne '') && (defined($coursepriv))) {
 3480:                     $$allroles{'cm.'.$area}.=':'.$coursepriv;
 3481:                     $$allroles{$spec.'.'.$area}.=':'.$coursepriv;
 3482:                 }
 3483:             }
 3484:         }
 3485:     }
 3486: }
 3487: 
 3488: sub group_roleprivs {
 3489:     my ($allgroups,$area,$group_privs,$tend,$tstart) = @_;
 3490:     my $access = 1;
 3491:     my $now = time;
 3492:     if (($tend!=0) && ($tend<$now)) { $access = 0; }
 3493:     if (($tstart!=0) && ($tstart>$now)) { $access=0; }
 3494:     if ($access) {
 3495:         my ($course,$group) = ($area =~ m|(/$match_domain/$match_courseid)/([^/]+)$|);
 3496:         $$allgroups{$course}{$group} .=':'.$group_privs;
 3497:     }
 3498: }
 3499: 
 3500: sub standard_roleprivs {
 3501:     my ($allroles,$trole,$tdomain,$spec,$trest,$area) = @_;
 3502:     if (defined($pr{$trole.':s'})) {
 3503:         $$allroles{'cm./'}.=':'.$pr{$trole.':s'};
 3504:         $$allroles{$spec.'./'}.=':'.$pr{$trole.':s'};
 3505:     }
 3506:     if ($tdomain ne '') {
 3507:         if (defined($pr{$trole.':d'})) {
 3508:             $$allroles{'cm./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
 3509:             $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
 3510:         }
 3511:         if (($trest ne '') && (defined($pr{$trole.':c'}))) {
 3512:             $$allroles{'cm.'.$area}.=':'.$pr{$trole.':c'};
 3513:             $$allroles{$spec.'.'.$area}.=':'.$pr{$trole.':c'};
 3514:         }
 3515:     }
 3516: }
 3517: 
 3518: sub set_userprivs {
 3519:     my ($userroles,$allroles,$allgroups) = @_; 
 3520:     my $author=0;
 3521:     my $adv=0;
 3522:     my %grouproles = ();
 3523:     if (keys(%{$allgroups}) > 0) {
 3524:         foreach my $role (keys %{$allroles}) {
 3525:             my ($trole,$area,$sec,$extendedarea);
 3526:             if ($role =~ m-^(\w+|cr/$match_domain/$match_username/\w+)\.(/$match_domain/$match_courseid)(/?\w*)\.-) {
 3527:                 $trole = $1;
 3528:                 $area = $2;
 3529:                 $sec = $3;
 3530:                 $extendedarea = $area.$sec;
 3531:                 if (exists($$allgroups{$area})) {
 3532:                     foreach my $group (keys(%{$$allgroups{$area}})) {
 3533:                         my $spec = $trole.'.'.$extendedarea;
 3534:                         $grouproles{$spec.'.'.$area.'/'.$group} = 
 3535:                                                 $$allgroups{$area}{$group};
 3536:                     }
 3537:                 }
 3538:             }
 3539:         }
 3540:     }
 3541:     foreach my $group (keys(%grouproles)) {
 3542:         $$allroles{$group} = $grouproles{$group};
 3543:     }
 3544:     foreach my $role (keys(%{$allroles})) {
 3545:         my %thesepriv;
 3546:         if (($role=~/^au/) || ($role=~/^ca/) || ($role=~/^aa/)) { $author=1; }
 3547:         foreach my $item (split(/:/,$$allroles{$role})) {
 3548:             if ($item ne '') {
 3549:                 my ($privilege,$restrictions)=split(/&/,$item);
 3550:                 if ($restrictions eq '') {
 3551:                     $thesepriv{$privilege}='F';
 3552:                 } elsif ($thesepriv{$privilege} ne 'F') {
 3553:                     $thesepriv{$privilege}.=$restrictions;
 3554:                 }
 3555:                 if ($thesepriv{'adv'} eq 'F') { $adv=1; }
 3556:             }
 3557:         }
 3558:         my $thesestr='';
 3559:         foreach my $priv (keys(%thesepriv)) {
 3560: 	    $thesestr.=':'.$priv.'&'.$thesepriv{$priv};
 3561: 	}
 3562:         $userroles->{'user.priv.'.$role} = $thesestr;
 3563:     }
 3564:     return ($author,$adv);
 3565: }
 3566: 
 3567: # --------------------------------------------------------------- get interface
 3568: 
 3569: sub get {
 3570:    my ($namespace,$storearr,$udomain,$uname)=@_;
 3571:    my $items='';
 3572:    foreach my $item (@$storearr) {
 3573:        $items.=&escape($item).'&';
 3574:    }
 3575:    $items=~s/\&$//;
 3576:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 3577:    if (!$uname) { $uname=$env{'user.name'}; }
 3578:    my $uhome=&homeserver($uname,$udomain);
 3579: 
 3580:    my $rep=&reply("get:$udomain:$uname:$namespace:$items",$uhome);
 3581:    my @pairs=split(/\&/,$rep);
 3582:    if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
 3583:      return @pairs;
 3584:    }
 3585:    my %returnhash=();
 3586:    my $i=0;
 3587:    foreach my $item (@$storearr) {
 3588:       $returnhash{$item}=&thaw_unescape($pairs[$i]);
 3589:       $i++;
 3590:    }
 3591:    return %returnhash;
 3592: }
 3593: 
 3594: # --------------------------------------------------------------- del interface
 3595: 
 3596: sub del {
 3597:    my ($namespace,$storearr,$udomain,$uname)=@_;
 3598:    my $items='';
 3599:    foreach my $item (@$storearr) {
 3600:        $items.=&escape($item).'&';
 3601:    }
 3602:    $items=~s/\&$//;
 3603:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 3604:    if (!$uname) { $uname=$env{'user.name'}; }
 3605:    my $uhome=&homeserver($uname,$udomain);
 3606: 
 3607:    return &reply("del:$udomain:$uname:$namespace:$items",$uhome);
 3608: }
 3609: 
 3610: # -------------------------------------------------------------- dump interface
 3611: 
 3612: sub dump {
 3613:     my ($namespace,$udomain,$uname,$regexp,$range)=@_;
 3614:     if (!$udomain) { $udomain=$env{'user.domain'}; }
 3615:     if (!$uname) { $uname=$env{'user.name'}; }
 3616:     my $uhome=&homeserver($uname,$udomain);
 3617:     if ($regexp) {
 3618: 	$regexp=&escape($regexp);
 3619:     } else {
 3620: 	$regexp='.';
 3621:     }
 3622:     my $rep=&reply("dump:$udomain:$uname:$namespace:$regexp:$range",$uhome);
 3623:     my @pairs=split(/\&/,$rep);
 3624:     my %returnhash=();
 3625:     foreach my $item (@pairs) {
 3626: 	my ($key,$value)=split(/=/,$item,2);
 3627: 	$key = &unescape($key);
 3628: 	next if ($key =~ /^error: 2 /);
 3629: 	$returnhash{$key}=&thaw_unescape($value);
 3630:     }
 3631:     return %returnhash;
 3632: }
 3633: 
 3634: # --------------------------------------------------------- dumpstore interface
 3635: 
 3636: sub dumpstore {
 3637:    my ($namespace,$udomain,$uname,$regexp,$range)=@_;
 3638:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 3639:    if (!$uname) { $uname=$env{'user.name'}; }
 3640:    my $uhome=&homeserver($uname,$udomain);
 3641:    if ($regexp) {
 3642:        $regexp=&escape($regexp);
 3643:    } else {
 3644:        $regexp='.';
 3645:    }
 3646:    my $rep=&reply("dump:$udomain:$uname:$namespace:$regexp:$range",$uhome);
 3647:    my @pairs=split(/\&/,$rep);
 3648:    my %returnhash=();
 3649:    foreach my $item (@pairs) {
 3650:        my ($key,$value)=split(/=/,$item,2);
 3651:        next if ($key =~ /^error: 2 /);
 3652:        $returnhash{$key}=&thaw_unescape($value);
 3653:    }
 3654:    return %returnhash;
 3655: }
 3656: 
 3657: # -------------------------------------------------------------- keys interface
 3658: 
 3659: sub getkeys {
 3660:    my ($namespace,$udomain,$uname)=@_;
 3661:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 3662:    if (!$uname) { $uname=$env{'user.name'}; }
 3663:    my $uhome=&homeserver($uname,$udomain);
 3664:    my $rep=reply("keys:$udomain:$uname:$namespace",$uhome);
 3665:    my @keyarray=();
 3666:    foreach my $key (split(/\&/,$rep)) {
 3667:       next if ($key =~ /^error: 2 /);
 3668:       push(@keyarray,&unescape($key));
 3669:    }
 3670:    return @keyarray;
 3671: }
 3672: 
 3673: # --------------------------------------------------------------- currentdump
 3674: sub currentdump {
 3675:    my ($courseid,$sdom,$sname)=@_;
 3676:    $courseid = $env{'request.course.id'} if (! defined($courseid));
 3677:    $sdom     = $env{'user.domain'}       if (! defined($sdom));
 3678:    $sname    = $env{'user.name'}         if (! defined($sname));
 3679:    my $uhome = &homeserver($sname,$sdom);
 3680:    my $rep=reply('currentdump:'.$sdom.':'.$sname.':'.$courseid,$uhome);
 3681:    return if ($rep =~ /^(error:|no_such_host)/);
 3682:    #
 3683:    my %returnhash=();
 3684:    #
 3685:    if ($rep eq "unknown_cmd") { 
 3686:        # an old lond will not know currentdump
 3687:        # Do a dump and make it look like a currentdump
 3688:        my @tmp = &dumpstore($courseid,$sdom,$sname,'.');
 3689:        return if ($tmp[0] =~ /^(error:|no_such_host)/);
 3690:        my %hash = @tmp;
 3691:        @tmp=();
 3692:        %returnhash = %{&convert_dump_to_currentdump(\%hash)};
 3693:    } else {
 3694:        my @pairs=split(/\&/,$rep);
 3695:        foreach my $pair (@pairs) {
 3696:            my ($key,$value)=split(/=/,$pair,2);
 3697:            my ($symb,$param) = split(/:/,$key);
 3698:            $returnhash{&unescape($symb)}->{&unescape($param)} = 
 3699:                                                         &thaw_unescape($value);
 3700:        }
 3701:    }
 3702:    return %returnhash;
 3703: }
 3704: 
 3705: sub convert_dump_to_currentdump{
 3706:     my %hash = %{shift()};
 3707:     my %returnhash;
 3708:     # Code ripped from lond, essentially.  The only difference
 3709:     # here is the unescaping done by lonnet::dump().  Conceivably
 3710:     # we might run in to problems with parameter names =~ /^v\./
 3711:     while (my ($key,$value) = each(%hash)) {
 3712:         my ($v,$symb,$param) = split(/:/,$key);
 3713: 	$symb  = &unescape($symb);
 3714: 	$param = &unescape($param);
 3715:         next if ($v eq 'version' || $symb eq 'keys');
 3716:         next if (exists($returnhash{$symb}) &&
 3717:                  exists($returnhash{$symb}->{$param}) &&
 3718:                  $returnhash{$symb}->{'v.'.$param} > $v);
 3719:         $returnhash{$symb}->{$param}=$value;
 3720:         $returnhash{$symb}->{'v.'.$param}=$v;
 3721:     }
 3722:     #
 3723:     # Remove all of the keys in the hashes which keep track of
 3724:     # the version of the parameter.
 3725:     while (my ($symb,$param_hash) = each(%returnhash)) {
 3726:         # use a foreach because we are going to delete from the hash.
 3727:         foreach my $key (keys(%$param_hash)) {
 3728:             delete($param_hash->{$key}) if ($key =~ /^v\./);
 3729:         }
 3730:     }
 3731:     return \%returnhash;
 3732: }
 3733: 
 3734: # ------------------------------------------------------ critical inc interface
 3735: 
 3736: sub cinc {
 3737:     return &inc(@_,'critical');
 3738: }
 3739: 
 3740: # --------------------------------------------------------------- inc interface
 3741: 
 3742: sub inc {
 3743:     my ($namespace,$store,$udomain,$uname,$critical) = @_;
 3744:     if (!$udomain) { $udomain=$env{'user.domain'}; }
 3745:     if (!$uname) { $uname=$env{'user.name'}; }
 3746:     my $uhome=&homeserver($uname,$udomain);
 3747:     my $items='';
 3748:     if (! ref($store)) {
 3749:         # got a single value, so use that instead
 3750:         $items = &escape($store).'=&';
 3751:     } elsif (ref($store) eq 'SCALAR') {
 3752:         $items = &escape($$store).'=&';        
 3753:     } elsif (ref($store) eq 'ARRAY') {
 3754:         $items = join('=&',map {&escape($_);} @{$store});
 3755:     } elsif (ref($store) eq 'HASH') {
 3756:         while (my($key,$value) = each(%{$store})) {
 3757:             $items.= &escape($key).'='.&escape($value).'&';
 3758:         }
 3759:     }
 3760:     $items=~s/\&$//;
 3761:     if ($critical) {
 3762: 	return &critical("inc:$udomain:$uname:$namespace:$items",$uhome);
 3763:     } else {
 3764: 	return &reply("inc:$udomain:$uname:$namespace:$items",$uhome);
 3765:     }
 3766: }
 3767: 
 3768: # --------------------------------------------------------------- put interface
 3769: 
 3770: sub put {
 3771:    my ($namespace,$storehash,$udomain,$uname)=@_;
 3772:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 3773:    if (!$uname) { $uname=$env{'user.name'}; }
 3774:    my $uhome=&homeserver($uname,$udomain);
 3775:    my $items='';
 3776:    foreach my $item (keys(%$storehash)) {
 3777:        $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
 3778:    }
 3779:    $items=~s/\&$//;
 3780:    return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
 3781: }
 3782: 
 3783: # ------------------------------------------------------------ newput interface
 3784: 
 3785: sub newput {
 3786:    my ($namespace,$storehash,$udomain,$uname)=@_;
 3787:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 3788:    if (!$uname) { $uname=$env{'user.name'}; }
 3789:    my $uhome=&homeserver($uname,$udomain);
 3790:    my $items='';
 3791:    foreach my $key (keys(%$storehash)) {
 3792:        $items.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
 3793:    }
 3794:    $items=~s/\&$//;
 3795:    return &reply("newput:$udomain:$uname:$namespace:$items",$uhome);
 3796: }
 3797: 
 3798: # ---------------------------------------------------------  putstore interface
 3799: 
 3800: sub putstore {
 3801:    my ($namespace,$symb,$version,$storehash,$udomain,$uname)=@_;
 3802:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 3803:    if (!$uname) { $uname=$env{'user.name'}; }
 3804:    my $uhome=&homeserver($uname,$udomain);
 3805:    my $items='';
 3806:    foreach my $key (keys(%$storehash)) {
 3807:        $items.= &escape($key).'='.&freeze_escape($storehash->{$key}).'&';
 3808:    }
 3809:    $items=~s/\&$//;
 3810:    my $esc_symb=&escape($symb);
 3811:    my $esc_v=&escape($version);
 3812:    my $reply =
 3813:        &reply("putstore:$udomain:$uname:$namespace:$esc_symb:$esc_v:$items",
 3814: 	      $uhome);
 3815:    if ($reply eq 'unknown_cmd') {
 3816:        # gfall back to way things use to be done
 3817:        return &old_putstore($namespace,$symb,$version,$storehash,$udomain,
 3818: 			    $uname);
 3819:    }
 3820:    return $reply;
 3821: }
 3822: 
 3823: sub old_putstore {
 3824:     my ($namespace,$symb,$version,$storehash,$udomain,$uname)=@_;
 3825:     if (!$udomain) { $udomain=$env{'user.domain'}; }
 3826:     if (!$uname) { $uname=$env{'user.name'}; }
 3827:     my $uhome=&homeserver($uname,$udomain);
 3828:     my %newstorehash;
 3829:     foreach my $item (keys(%$storehash)) {
 3830: 	my $key = $version.':'.&escape($symb).':'.$item;
 3831: 	$newstorehash{$key} = $storehash->{$item};
 3832:     }
 3833:     my $items='';
 3834:     my %allitems = ();
 3835:     foreach my $item (keys(%newstorehash)) {
 3836: 	if ($item =~ m/^([^\:]+):([^\:]+):([^\:]+)$/) {
 3837: 	    my $key = $1.':keys:'.$2;
 3838: 	    $allitems{$key} .= $3.':';
 3839: 	}
 3840: 	$items.=$item.'='.&freeze_escape($newstorehash{$item}).'&';
 3841:     }
 3842:     foreach my $item (keys(%allitems)) {
 3843: 	$allitems{$item} =~ s/\:$//;
 3844: 	$items.= $item.'='.$allitems{$item}.'&';
 3845:     }
 3846:     $items=~s/\&$//;
 3847:     return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
 3848: }
 3849: 
 3850: # ------------------------------------------------------ critical put interface
 3851: 
 3852: sub cput {
 3853:    my ($namespace,$storehash,$udomain,$uname)=@_;
 3854:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 3855:    if (!$uname) { $uname=$env{'user.name'}; }
 3856:    my $uhome=&homeserver($uname,$udomain);
 3857:    my $items='';
 3858:    foreach my $item (keys(%$storehash)) {
 3859:        $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
 3860:    }
 3861:    $items=~s/\&$//;
 3862:    return &critical("put:$udomain:$uname:$namespace:$items",$uhome);
 3863: }
 3864: 
 3865: # -------------------------------------------------------------- eget interface
 3866: 
 3867: sub eget {
 3868:    my ($namespace,$storearr,$udomain,$uname)=@_;
 3869:    my $items='';
 3870:    foreach my $item (@$storearr) {
 3871:        $items.=&escape($item).'&';
 3872:    }
 3873:    $items=~s/\&$//;
 3874:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 3875:    if (!$uname) { $uname=$env{'user.name'}; }
 3876:    my $uhome=&homeserver($uname,$udomain);
 3877:    my $rep=&reply("eget:$udomain:$uname:$namespace:$items",$uhome);
 3878:    my @pairs=split(/\&/,$rep);
 3879:    my %returnhash=();
 3880:    my $i=0;
 3881:    foreach my $item (@$storearr) {
 3882:       $returnhash{$item}=&thaw_unescape($pairs[$i]);
 3883:       $i++;
 3884:    }
 3885:    return %returnhash;
 3886: }
 3887: 
 3888: # ------------------------------------------------------------ tmpput interface
 3889: sub tmpput {
 3890:     my ($storehash,$server,$context)=@_;
 3891:     my $items='';
 3892:     foreach my $item (keys(%$storehash)) {
 3893: 	$items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
 3894:     }
 3895:     $items=~s/\&$//;
 3896:     if (defined($context)) {
 3897:         $items .= ':'.&escape($context);
 3898:     }
 3899:     return &reply("tmpput:$items",$server);
 3900: }
 3901: 
 3902: # ------------------------------------------------------------ tmpget interface
 3903: sub tmpget {
 3904:     my ($token,$server)=@_;
 3905:     if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
 3906:     my $rep=&reply("tmpget:$token",$server);
 3907:     my %returnhash;
 3908:     foreach my $item (split(/\&/,$rep)) {
 3909: 	my ($key,$value)=split(/=/,$item);
 3910: 	$returnhash{&unescape($key)}=&thaw_unescape($value);
 3911:     }
 3912:     return %returnhash;
 3913: }
 3914: 
 3915: # ------------------------------------------------------------ tmpget interface
 3916: sub tmpdel {
 3917:     my ($token,$server)=@_;
 3918:     if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
 3919:     return &reply("tmpdel:$token",$server);
 3920: }
 3921: 
 3922: # -------------------------------------------------- portfolio access checking
 3923: 
 3924: sub portfolio_access {
 3925:     my ($requrl) = @_;
 3926:     my (undef,$udom,$unum,$file_name,$group) = &parse_portfolio_url($requrl);
 3927:     my $result = &get_portfolio_access($udom,$unum,$file_name,$group);
 3928:     if ($result) {
 3929:         my %setters;
 3930:         if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
 3931:             my ($startblock,$endblock) =
 3932:                 &Apache::loncommon::blockcheck(\%setters,'port',$unum,$udom);
 3933:             if ($startblock && $endblock) {
 3934:                 return 'B';
 3935:             }
 3936:         } else {
 3937:             my ($startblock,$endblock) =
 3938:                 &Apache::loncommon::blockcheck(\%setters,'port');
 3939:             if ($startblock && $endblock) {
 3940:                 return 'B';
 3941:             }
 3942:         }
 3943:     }
 3944:     if ($result eq 'ok') {
 3945:        return 'F';
 3946:     } elsif ($result =~ /^[^:]+:guest_/) {
 3947:        return 'A';
 3948:     }
 3949:     return '';
 3950: }
 3951: 
 3952: sub get_portfolio_access {
 3953:     my ($udom,$unum,$file_name,$group,$access_hash) = @_;
 3954: 
 3955:     if (!ref($access_hash)) {
 3956: 	my $current_perms = &get_portfile_permissions($udom,$unum);
 3957: 	my %access_controls = &get_access_controls($current_perms,$group,
 3958: 						   $file_name);
 3959: 	$access_hash = $access_controls{$file_name};
 3960:     }
 3961: 
 3962:     my ($public,$guest,@domains,@users,@courses,@groups);
 3963:     my $now = time;
 3964:     if (ref($access_hash) eq 'HASH') {
 3965:         foreach my $key (keys(%{$access_hash})) {
 3966:             my ($num,$scope,$end,$start) = ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
 3967:             if ($start > $now) {
 3968:                 next;
 3969:             }
 3970:             if ($end && $end<$now) {
 3971:                 next;
 3972:             }
 3973:             if ($scope eq 'public') {
 3974:                 $public = $key;
 3975:                 last;
 3976:             } elsif ($scope eq 'guest') {
 3977:                 $guest = $key;
 3978:             } elsif ($scope eq 'domains') {
 3979:                 push(@domains,$key);
 3980:             } elsif ($scope eq 'users') {
 3981:                 push(@users,$key);
 3982:             } elsif ($scope eq 'course') {
 3983:                 push(@courses,$key);
 3984:             } elsif ($scope eq 'group') {
 3985:                 push(@groups,$key);
 3986:             }
 3987:         }
 3988:         if ($public) {
 3989:             return 'ok';
 3990:         }
 3991:         if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
 3992:             if ($guest) {
 3993:                 return $guest;
 3994:             }
 3995:         } else {
 3996:             if (@domains > 0) {
 3997:                 foreach my $domkey (@domains) {
 3998:                     if (ref($access_hash->{$domkey}{'dom'}) eq 'ARRAY') {
 3999:                         if (grep(/^\Q$env{'user.domain'}\E$/,@{$access_hash->{$domkey}{'dom'}})) {
 4000:                             return 'ok';
 4001:                         }
 4002:                     }
 4003:                 }
 4004:             }
 4005:             if (@users > 0) {
 4006:                 foreach my $userkey (@users) {
 4007:                     if (ref($access_hash->{$userkey}{'users'}) eq 'ARRAY') {
 4008:                         foreach my $item (@{$access_hash->{$userkey}{'users'}}) {
 4009:                             if (ref($item) eq 'HASH') {
 4010:                                 if (($item->{'uname'} eq $env{'user.name'}) &&
 4011:                                     ($item->{'udom'} eq $env{'user.domain'})) {
 4012:                                     return 'ok';
 4013:                                 }
 4014:                             }
 4015:                         }
 4016:                     } 
 4017:                 }
 4018:             }
 4019:             my %roleshash;
 4020:             my @courses_and_groups = @courses;
 4021:             push(@courses_and_groups,@groups); 
 4022:             if (@courses_and_groups > 0) {
 4023:                 my (%allgroups,%allroles); 
 4024:                 my ($start,$end,$role,$sec,$group);
 4025:                 foreach my $envkey (%env) {
 4026:                     if ($envkey =~ m-^user\.role\.(gr|cc|in|ta|ep|st)\./($match_domain)/($match_courseid)/?([^/]*)$-) {
 4027:                         my $cid = $2.'_'.$3; 
 4028:                         if ($1 eq 'gr') {
 4029:                             $group = $4;
 4030:                             $allgroups{$cid}{$group} = $env{$envkey};
 4031:                         } else {
 4032:                             if ($4 eq '') {
 4033:                                 $sec = 'none';
 4034:                             } else {
 4035:                                 $sec = $4;
 4036:                             }
 4037:                             $allroles{$cid}{$1}{$sec} = $env{$envkey};
 4038:                         }
 4039:                     } elsif ($envkey =~ m-^user\.role\./cr/($match_domain/$match_username/\w*)./($match_domain)/($match_courseid)/?([^/]*)$-) {
 4040:                         my $cid = $2.'_'.$3;
 4041:                         if ($4 eq '') {
 4042:                             $sec = 'none';
 4043:                         } else {
 4044:                             $sec = $4;
 4045:                         }
 4046:                         $allroles{$cid}{$1}{$sec} = $env{$envkey};
 4047:                     }
 4048:                 }
 4049:                 if (keys(%allroles) == 0) {
 4050:                     return;
 4051:                 }
 4052:                 foreach my $key (@courses_and_groups) {
 4053:                     my %content = %{$$access_hash{$key}};
 4054:                     my $cnum = $content{'number'};
 4055:                     my $cdom = $content{'domain'};
 4056:                     my $cid = $cdom.'_'.$cnum;
 4057:                     if (!exists($allroles{$cid})) {
 4058:                         next;
 4059:                     }    
 4060:                     foreach my $role_id (keys(%{$content{'roles'}})) {
 4061:                         my @sections = @{$content{'roles'}{$role_id}{'section'}};
 4062:                         my @groups = @{$content{'roles'}{$role_id}{'group'}};
 4063:                         my @status = @{$content{'roles'}{$role_id}{'access'}};
 4064:                         my @roles = @{$content{'roles'}{$role_id}{'role'}};
 4065:                         foreach my $role (keys(%{$allroles{$cid}})) {
 4066:                             if ((grep/^all$/,@roles) || (grep/^\Q$role\E$/,@roles)) {
 4067:                                 foreach my $sec (keys(%{$allroles{$cid}{$role}})) {
 4068:                                     if (&course_group_datechecker($allroles{$cid}{$role}{$sec},$now,\@status) eq 'ok') {
 4069:                                         if (grep/^all$/,@sections) {
 4070:                                             return 'ok';
 4071:                                         } else {
 4072:                                             if (grep/^$sec$/,@sections) {
 4073:                                                 return 'ok';
 4074:                                             }
 4075:                                         }
 4076:                                     }
 4077:                                 }
 4078:                                 if (keys(%{$allgroups{$cid}}) == 0) {
 4079:                                     if (grep/^none$/,@groups) {
 4080:                                         return 'ok';
 4081:                                     }
 4082:                                 } else {
 4083:                                     if (grep/^all$/,@groups) {
 4084:                                         return 'ok';
 4085:                                     } 
 4086:                                     foreach my $group (keys(%{$allgroups{$cid}})) {
 4087:                                         if (grep/^$group$/,@groups) {
 4088:                                             return 'ok';
 4089:                                         }
 4090:                                     }
 4091:                                 } 
 4092:                             }
 4093:                         }
 4094:                     }
 4095:                 }
 4096:             }
 4097:             if ($guest) {
 4098:                 return $guest;
 4099:             }
 4100:         }
 4101:     }
 4102:     return;
 4103: }
 4104: 
 4105: sub course_group_datechecker {
 4106:     my ($dates,$now,$status) = @_;
 4107:     my ($start,$end) = split(/\./,$dates);
 4108:     if (!$start && !$end) {
 4109:         return 'ok';
 4110:     }
 4111:     if (grep/^active$/,@{$status}) {
 4112:         if (((!$start) || ($start && $start <= $now)) && ((!$end) || ($end && $end >= $now))) {
 4113:             return 'ok';
 4114:         }
 4115:     }
 4116:     if (grep/^previous$/,@{$status}) {
 4117:         if ($end > $now ) {
 4118:             return 'ok';
 4119:         }
 4120:     }
 4121:     if (grep/^future$/,@{$status}) {
 4122:         if ($start > $now) {
 4123:             return 'ok';
 4124:         }
 4125:     }
 4126:     return; 
 4127: }
 4128: 
 4129: sub parse_portfolio_url {
 4130:     my ($url) = @_;
 4131: 
 4132:     my ($type,$udom,$unum,$group,$file_name);
 4133:     
 4134:     if ($url =~  m-^/*(?:uploaded|editupload)/($match_domain)/($match_username)/portfolio(/.+)$-) {
 4135: 	$type = 1;
 4136:         $udom = $1;
 4137:         $unum = $2;
 4138:         $file_name = $3;
 4139:     } elsif ($url =~ m-^/*(?:uploaded|editupload)/($match_domain)/($match_courseid)/groups/([^/]+)/portfolio/(.+)$-) {
 4140: 	$type = 2;
 4141:         $udom = $1;
 4142:         $unum = $2;
 4143:         $group = $3;
 4144:         $file_name = $3.'/'.$4;
 4145:     }
 4146:     if (wantarray) {
 4147: 	return ($type,$udom,$unum,$file_name,$group);
 4148:     }
 4149:     return $type;
 4150: }
 4151: 
 4152: sub is_portfolio_url {
 4153:     my ($url) = @_;
 4154:     return scalar(&parse_portfolio_url($url));
 4155: }
 4156: 
 4157: sub is_portfolio_file {
 4158:     my ($file) = @_;
 4159:     if (($file =~ /^portfolio/) || ($file =~ /^groups\/\w+\/portfolio/)) {
 4160:         return 1;
 4161:     }
 4162:     return;
 4163: }
 4164: 
 4165: 
 4166: # ---------------------------------------------- Custom access rule evaluation
 4167: 
 4168: sub customaccess {
 4169:     my ($priv,$uri)=@_;
 4170:     my ($urole,$urealm)=split(/\./,$env{'request.role'},2);
 4171:     my (undef,$udom,$ucrs,$usec)=split(/\//,$urealm);
 4172:     $udom = &LONCAPA::clean_domain($udom);
 4173:     $ucrs = &LONCAPA::clean_username($ucrs);
 4174:     my $access=0;
 4175:     foreach my $right (split(/\s*\,\s*/,&metadata($uri,'rule_rights'))) {
 4176: 	my ($effect,$realm,$role,$type)=split(/\:/,$right);
 4177: 	if ($type eq 'user') {
 4178: 	    foreach my $scope (split(/\s*\,\s*/,$realm)) {
 4179: 		my ($tdom,$tuname)=split(m{/},$scope);
 4180: 		if ($tdom) {
 4181: 		    if ($tdom ne $env{'user.domain'}) { next; }
 4182: 		}
 4183: 		if ($tuname) {
 4184: 		    if ($tuname ne $env{'user.name'}) { next; }
 4185: 		}
 4186: 		$access=($effect eq 'allow');
 4187: 		last;
 4188: 	    }
 4189: 	} else {
 4190: 	    if ($role) {
 4191: 		if ($role ne $urole) { next; }
 4192: 	    }
 4193: 	    foreach my $scope (split(/\s*\,\s*/,$realm)) {
 4194: 		my ($tdom,$tcrs,$tsec)=split(/\_/,$scope);
 4195: 		if ($tdom) {
 4196: 		    if ($tdom ne $udom) { next; }
 4197: 		}
 4198: 		if ($tcrs) {
 4199: 		    if ($tcrs ne $ucrs) { next; }
 4200: 		}
 4201: 		if ($tsec) {
 4202: 		    if ($tsec ne $usec) { next; }
 4203: 		}
 4204: 		$access=($effect eq 'allow');
 4205: 		last;
 4206: 	    }
 4207: 	    if ($realm eq '' && $role eq '') {
 4208: 		$access=($effect eq 'allow');
 4209: 	    }
 4210: 	}
 4211:     }
 4212:     return $access;
 4213: }
 4214: 
 4215: # ------------------------------------------------- Check for a user privilege
 4216: 
 4217: sub allowed {
 4218:     my ($priv,$uri,$symb,$role)=@_;
 4219:     my $ver_orguri=$uri;
 4220:     $uri=&deversion($uri);
 4221:     my $orguri=$uri;
 4222:     $uri=&declutter($uri);
 4223: 
 4224:     if ($priv eq 'evb') {
 4225: # Evade communication block restrictions for specified role in a course
 4226:         if ($env{'user.priv.'.$role} =~/evb\&([^\:]*)/) {
 4227:             return $1;
 4228:         } else {
 4229:             return;
 4230:         }
 4231:     }
 4232: 
 4233:     if (defined($env{'allowed.'.$priv})) { return $env{'allowed.'.$priv}; }
 4234: # Free bre access to adm and meta resources
 4235:     if (((($uri=~/^adm\//) && ($uri !~ m{/(?:smppg|bulletinboard)$})) 
 4236: 	 || (($uri=~/\.meta$/) && ($uri!~m|^uploaded/|) )) 
 4237: 	&& ($priv eq 'bre')) {
 4238: 	return 'F';
 4239:     }
 4240: 
 4241: # Free bre access to user's own portfolio contents
 4242:     my ($space,$domain,$name,@dir)=split('/',$uri);
 4243:     if (($space=~/^(uploaded|editupload)$/) && ($env{'user.name'} eq $name) && 
 4244: 	($env{'user.domain'} eq $domain) && ('portfolio' eq $dir[0])) {
 4245:         my %setters;
 4246:         my ($startblock,$endblock) = 
 4247:             &Apache::loncommon::blockcheck(\%setters,'port');
 4248:         if ($startblock && $endblock) {
 4249:             return 'B';
 4250:         } else {
 4251:             return 'F';
 4252:         }
 4253:     }
 4254: 
 4255: # bre access to group portfolio for rgf priv in group, or mdg or vcg in course.
 4256:     if (($space=~/^(uploaded|editupload)$/) && ($dir[0] eq 'groups') 
 4257:          && ($dir[2] eq 'portfolio') && ($priv eq 'bre')) {
 4258:         if (exists($env{'request.course.id'})) {
 4259:             my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 4260:             my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 4261:             if (($domain eq $cdom) && ($name eq $cnum)) {
 4262:                 my $courseprivid=$env{'request.course.id'};
 4263:                 $courseprivid=~s/\_/\//;
 4264:                 if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid
 4265:                     .'/'.$dir[1]} =~/rgf\&([^\:]*)/) {
 4266:                     return $1; 
 4267:                 } else {
 4268:                     if ($env{'request.course.sec'}) {
 4269:                         $courseprivid.='/'.$env{'request.course.sec'};
 4270:                     }
 4271:                     if ($env{'user.priv.'.$env{'request.role'}.'./'.
 4272:                         $courseprivid} =~/(mdg|vcg)\&([^\:]*)/) {
 4273:                         return $2;
 4274:                     }
 4275:                 }
 4276:             }
 4277:         }
 4278:     }
 4279: 
 4280: # Free bre to public access
 4281: 
 4282:     if ($priv eq 'bre') {
 4283:         my $copyright=&metadata($uri,'copyright');
 4284: 	if (($copyright eq 'public') && (!$env{'request.course.id'})) { 
 4285:            return 'F'; 
 4286:         }
 4287:         if ($copyright eq 'priv') {
 4288:             $uri=~/([^\/]+)\/([^\/]+)\//;
 4289: 	    unless (($env{'user.name'} eq $2) && ($env{'user.domain'} eq $1)) {
 4290: 		return '';
 4291:             }
 4292:         }
 4293:         if ($copyright eq 'domain') {
 4294:             $uri=~/([^\/]+)\/([^\/]+)\//;
 4295: 	    unless (($env{'user.domain'} eq $1) ||
 4296:                  ($env{'course.'.$env{'request.course.id'}.'.domain'} eq $1)) {
 4297: 		return '';
 4298:             }
 4299:         }
 4300:         if ($env{'request.role'}=~ /li\.\//) {
 4301:             # Library role, so allow browsing of resources in this domain.
 4302:             return 'F';
 4303:         }
 4304:         if ($copyright eq 'custom') {
 4305: 	    unless (&customaccess($priv,$uri)) { return ''; }
 4306:         }
 4307:     }
 4308:     # Domain coordinator is trying to create a course
 4309:     if (($priv eq 'ccc') && ($env{'request.role'} =~ /^dc\./)) {
 4310:         # uri is the requested domain in this case.
 4311:         # comparison to 'request.role.domain' shows if the user has selected
 4312:         # a role of dc for the domain in question.
 4313:         return 'F' if ($uri eq $env{'request.role.domain'});
 4314:     }
 4315: 
 4316:     my $thisallowed='';
 4317:     my $statecond=0;
 4318:     my $courseprivid='';
 4319: 
 4320: # Course
 4321: 
 4322:     if ($env{'user.priv.'.$env{'request.role'}.'./'}=~/\Q$priv\E\&([^\:]*)/) {
 4323:        $thisallowed.=$1;
 4324:     }
 4325: 
 4326: # Domain
 4327: 
 4328:     if ($env{'user.priv.'.$env{'request.role'}.'./'.(split(/\//,$uri))[0].'/'}
 4329:        =~/\Q$priv\E\&([^\:]*)/) {
 4330:        $thisallowed.=$1;
 4331:     }
 4332: 
 4333: # Course: uri itself is a course
 4334:     my $courseuri=$uri;
 4335:     $courseuri=~s/\_(\d)/\/$1/;
 4336:     $courseuri=~s/^([^\/])/\/$1/;
 4337: 
 4338:     if ($env{'user.priv.'.$env{'request.role'}.'.'.$courseuri}
 4339:        =~/\Q$priv\E\&([^\:]*)/) {
 4340:        $thisallowed.=$1;
 4341:     }
 4342: 
 4343: # URI is an uploaded document for this course, default permissions don't matter
 4344: # not allowing 'edit' access (editupload) to uploaded course docs
 4345:     if (($priv eq 'bre') && ($uri=~m|^uploaded/|)) {
 4346: 	$thisallowed='';
 4347:         my ($match)=&is_on_map($uri);
 4348:         if ($match) {
 4349:             if ($env{'user.priv.'.$env{'request.role'}.'./'}
 4350:                   =~/\Q$priv\E\&([^\:]*)/) {
 4351:                 $thisallowed.=$1;
 4352:             }
 4353:         } else {
 4354:             my $refuri = $env{'httpref.'.$orguri} || $env{'httpref.'.$ver_orguri};
 4355:             if ($refuri) {
 4356:                 if ($refuri =~ m|^/adm/|) {
 4357:                     $thisallowed='F';
 4358:                 } else {
 4359:                     $refuri=&declutter($refuri);
 4360:                     my ($match) = &is_on_map($refuri);
 4361:                     if ($match) {
 4362:                         $thisallowed='F';
 4363:                     }
 4364:                 }
 4365:             }
 4366:         }
 4367:     }
 4368: 
 4369:     if ($priv eq 'bre'
 4370: 	&& $thisallowed ne 'F' 
 4371: 	&& $thisallowed ne '2'
 4372: 	&& &is_portfolio_url($uri)) {
 4373: 	$thisallowed = &portfolio_access($uri);
 4374:     }
 4375:     
 4376: # Full access at system, domain or course-wide level? Exit.
 4377: 
 4378:     if ($thisallowed=~/F/) {
 4379: 	return 'F';
 4380:     }
 4381: 
 4382: # If this is generating or modifying users, exit with special codes
 4383: 
 4384:     if (':csu:cdc:ccc:cin:cta:cep:ccr:cst:cad:cli:cau:cdg:cca:caa:'=~/\:\Q$priv\E\:/) {
 4385: 	if (($priv eq 'cca') || ($priv eq 'caa')) {
 4386: 	    my ($audom,$auname)=split('/',$uri);
 4387: # no author name given, so this just checks on the general right to make a co-author in this domain
 4388: 	    unless ($auname) { return $thisallowed; }
 4389: # an author name is given, so we are about to actually make a co-author for a certain account
 4390: 	    if (($auname ne $env{'user.name'} && $env{'request.role'} !~ /^dc\./) ||
 4391: 		(($audom ne $env{'user.domain'} && $env{'request.role'} !~ /^dc\./) &&
 4392: 		 ($audom ne $env{'request.role.domain'}))) { return ''; }
 4393: 	}
 4394: 	return $thisallowed;
 4395:     }
 4396: #
 4397: # Gathered so far: system, domain and course wide privileges
 4398: #
 4399: # Course: See if uri or referer is an individual resource that is part of 
 4400: # the course
 4401: 
 4402:     if ($env{'request.course.id'}) {
 4403: 
 4404:        $courseprivid=$env{'request.course.id'};
 4405:        if ($env{'request.course.sec'}) {
 4406:           $courseprivid.='/'.$env{'request.course.sec'};
 4407:        }
 4408:        $courseprivid=~s/\_/\//;
 4409:        my $checkreferer=1;
 4410:        my ($match,$cond)=&is_on_map($uri);
 4411:        if ($match) {
 4412:            $statecond=$cond;
 4413:            if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
 4414:                =~/\Q$priv\E\&([^\:]*)/) {
 4415:                $thisallowed.=$1;
 4416:                $checkreferer=0;
 4417:            }
 4418:        }
 4419:        
 4420:        if ($checkreferer) {
 4421: 	  my $refuri=$env{'httpref.'.$orguri};
 4422:             unless ($refuri) {
 4423:                 foreach my $key (keys(%env)) {
 4424: 		    if ($key=~/^httpref\..*\*/) {
 4425: 			my $pattern=$key;
 4426:                         $pattern=~s/^httpref\.\/res\///;
 4427:                         $pattern=~s/\*/\[\^\/\]\+/g;
 4428:                         $pattern=~s/\//\\\//g;
 4429:                         if ($orguri=~/$pattern/) {
 4430: 			    $refuri=$env{$key};
 4431:                         }
 4432:                     }
 4433:                 }
 4434:             }
 4435: 
 4436:          if ($refuri) { 
 4437: 	  $refuri=&declutter($refuri);
 4438:           my ($match,$cond)=&is_on_map($refuri);
 4439:             if ($match) {
 4440:               my $refstatecond=$cond;
 4441:               if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
 4442:                   =~/\Q$priv\E\&([^\:]*)/) {
 4443:                   $thisallowed.=$1;
 4444:                   $uri=$refuri;
 4445:                   $statecond=$refstatecond;
 4446:               }
 4447:           }
 4448:         }
 4449:        }
 4450:    }
 4451: 
 4452: #
 4453: # Gathered now: all privileges that could apply, and condition number
 4454: # 
 4455: #
 4456: # Full or no access?
 4457: #
 4458: 
 4459:     if ($thisallowed=~/F/) {
 4460: 	return 'F';
 4461:     }
 4462: 
 4463:     unless ($thisallowed) {
 4464:         return '';
 4465:     }
 4466: 
 4467: # Restrictions exist, deal with them
 4468: #
 4469: #   C:according to course preferences
 4470: #   R:according to resource settings
 4471: #   L:unless locked
 4472: #   X:according to user session state
 4473: #
 4474: 
 4475: # Possibly locked functionality, check all courses
 4476: # Locks might take effect only after 10 minutes cache expiration for other
 4477: # courses, and 2 minutes for current course
 4478: 
 4479:     my $envkey;
 4480:     if ($thisallowed=~/L/) {
 4481:         foreach $envkey (keys %env) {
 4482:            if ($envkey=~/^user\.role\.(st|ta)\.([^\.]*)/) {
 4483:                my $courseid=$2;
 4484:                my $roleid=$1.'.'.$2;
 4485:                $courseid=~s/^\///;
 4486:                my $expiretime=600;
 4487:                if ($env{'request.role'} eq $roleid) {
 4488: 		  $expiretime=120;
 4489:                }
 4490: 	       my ($cdom,$cnum,$csec)=split(/\//,$courseid);
 4491:                my $prefix='course.'.$cdom.'_'.$cnum.'.';
 4492:                if ((time-$env{$prefix.'last_cache'})>$expiretime) {
 4493: 		   &coursedescription($courseid,{'freshen_cache' => 1});
 4494:                }
 4495:                if (($env{$prefix.'res.'.$uri.'.lock.sections'}=~/\,\Q$csec\E\,/)
 4496:                 || ($env{$prefix.'res.'.$uri.'.lock.sections'} eq 'all')) {
 4497: 		   if ($env{$prefix.'res.'.$uri.'.lock.expire'}>time) {
 4498:                        &log($env{'user.domain'},$env{'user.name'},
 4499:                             $env{'user.home'},
 4500:                             'Locked by res: '.$priv.' for '.$uri.' due to '.
 4501:                             $cdom.'/'.$cnum.'/'.$csec.' expire '.
 4502:                             $env{$prefix.'priv.'.$priv.'.lock.expire'});
 4503: 		       return '';
 4504:                    }
 4505:                }
 4506:                if (($env{$prefix.'priv.'.$priv.'.lock.sections'}=~/\,\Q$csec\E\,/)
 4507:                 || ($env{$prefix.'priv.'.$priv.'.lock.sections'} eq 'all')) {
 4508: 		   if ($env{'priv.'.$priv.'.lock.expire'}>time) {
 4509:                        &log($env{'user.domain'},$env{'user.name'},
 4510:                             $env{'user.home'},
 4511:                             'Locked by priv: '.$priv.' for '.$uri.' due to '.
 4512:                             $cdom.'/'.$cnum.'/'.$csec.' expire '.
 4513:                             $env{$prefix.'priv.'.$priv.'.lock.expire'});
 4514: 		       return '';
 4515:                    }
 4516:                }
 4517: 	   }
 4518:        }
 4519:     }
 4520:    
 4521: #
 4522: # Rest of the restrictions depend on selected course
 4523: #
 4524: 
 4525:     unless ($env{'request.course.id'}) {
 4526: 	if ($thisallowed eq 'A') {
 4527: 	    return 'A';
 4528:         } elsif ($thisallowed eq 'B') {
 4529:             return 'B';
 4530: 	} else {
 4531: 	    return '1';
 4532: 	}
 4533:     }
 4534: 
 4535: #
 4536: # Now user is definitely in a course
 4537: #
 4538: 
 4539: 
 4540: # Course preferences
 4541: 
 4542:    if ($thisallowed=~/C/) {
 4543:        my $rolecode=(split(/\./,$env{'request.role'}))[0];
 4544:        my $unamedom=$env{'user.name'}.':'.$env{'user.domain'};
 4545:        if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.roles.denied'}
 4546: 	   =~/\Q$rolecode\E/) {
 4547: 	   if ($priv ne 'pch') { 
 4548: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
 4549: 			'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode.' in '.
 4550: 			$env{'request.course.id'});
 4551: 	   }
 4552:            return '';
 4553:        }
 4554: 
 4555:        if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.users.denied'}
 4556: 	   =~/\Q$unamedom\E/) {
 4557: 	   if ($priv ne 'pch') { 
 4558: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.
 4559: 			'Denied by user: '.$priv.' for '.$uri.' as '.$unamedom.' in '.
 4560: 			$env{'request.course.id'});
 4561: 	   }
 4562:            return '';
 4563:        }
 4564:    }
 4565: 
 4566: # Resource preferences
 4567: 
 4568:    if ($thisallowed=~/R/) {
 4569:        my $rolecode=(split(/\./,$env{'request.role'}))[0];
 4570:        if (&metadata($uri,'roledeny')=~/\Q$rolecode\E/) {
 4571: 	   if ($priv ne 'pch') { 
 4572: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
 4573: 			'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode);
 4574: 	   }
 4575: 	   return '';
 4576:        }
 4577:    }
 4578: 
 4579: # Restricted by state or randomout?
 4580: 
 4581:    if ($thisallowed=~/X/) {
 4582:       if ($env{'acc.randomout'}) {
 4583: 	 if (!$symb) { $symb=&symbread($uri,1); }
 4584:          if (($symb) && ($env{'acc.randomout'}=~/\&\Q$symb\E\&/)) { 
 4585:             return ''; 
 4586:          }
 4587:       }
 4588:       if (&condval($statecond)) {
 4589: 	 return '2';
 4590:       } else {
 4591:          return '';
 4592:       }
 4593:    }
 4594: 
 4595:     if ($thisallowed eq 'A') {
 4596: 	return 'A';
 4597:     } elsif ($thisallowed eq 'B') {
 4598:         return 'B';
 4599:     }
 4600:    return 'F';
 4601: }
 4602: 
 4603: sub split_uri_for_cond {
 4604:     my $uri=&deversion(&declutter(shift));
 4605:     my @uriparts=split(/\//,$uri);
 4606:     my $filename=pop(@uriparts);
 4607:     my $pathname=join('/',@uriparts);
 4608:     return ($pathname,$filename);
 4609: }
 4610: # --------------------------------------------------- Is a resource on the map?
 4611: 
 4612: sub is_on_map {
 4613:     my ($pathname,$filename) = &split_uri_for_cond(shift);
 4614:     #Trying to find the conditional for the file
 4615:     my $match=($env{'acc.res.'.$env{'request.course.id'}.'.'.$pathname}=~
 4616: 	       /\&\Q$filename\E\:([\d\|]+)\&/);
 4617:     if ($match) {
 4618: 	return (1,$1);
 4619:     } else {
 4620: 	return (0,0);
 4621:     }
 4622: }
 4623: 
 4624: # --------------------------------------------------------- Get symb from alias
 4625: 
 4626: sub get_symb_from_alias {
 4627:     my $symb=shift;
 4628:     my ($map,$resid,$url)=&decode_symb($symb);
 4629: # Already is a symb
 4630:     if ($url) { return $symb; }
 4631: # Must be an alias
 4632:     my $aliassymb='';
 4633:     my %bighash;
 4634:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
 4635:                             &GDBM_READER(),0640)) {
 4636:         my $rid=$bighash{'mapalias_'.$symb};
 4637: 	if ($rid) {
 4638: 	    my ($mapid,$resid)=split(/\./,$rid);
 4639: 	    $aliassymb=&encode_symb($bighash{'map_id_'.$mapid},
 4640: 				    $resid,$bighash{'src_'.$rid});
 4641: 	}
 4642:         untie %bighash;
 4643:     }
 4644:     return $aliassymb;
 4645: }
 4646: 
 4647: # ----------------------------------------------------------------- Define Role
 4648: 
 4649: sub definerole {
 4650:   if (allowed('mcr','/')) {
 4651:     my ($rolename,$sysrole,$domrole,$courole)=@_;
 4652:     foreach my $role (split(':',$sysrole)) {
 4653: 	my ($crole,$cqual)=split(/\&/,$role);
 4654:         if ($pr{'cr:s'}!~/\Q$crole\E/) { return "refused:s:$crole"; }
 4655:         if ($pr{'cr:s'}=~/\Q$crole\E\&/) {
 4656: 	    if ($pr{'cr:s'}!~/\Q$crole\E\&\w*\Q$cqual\E/) { 
 4657:                return "refused:s:$crole&$cqual"; 
 4658:             }
 4659:         }
 4660:     }
 4661:     foreach my $role (split(':',$domrole)) {
 4662: 	my ($crole,$cqual)=split(/\&/,$role);
 4663:         if ($pr{'cr:d'}!~/\Q$crole\E/) { return "refused:d:$crole"; }
 4664:         if ($pr{'cr:d'}=~/\Q$crole\E\&/) {
 4665: 	    if ($pr{'cr:d'}!~/\Q$crole\W\&\w*\Q$cqual\E/) { 
 4666:                return "refused:d:$crole&$cqual"; 
 4667:             }
 4668:         }
 4669:     }
 4670:     foreach my $role (split(':',$courole)) {
 4671: 	my ($crole,$cqual)=split(/\&/,$role);
 4672:         if ($pr{'cr:c'}!~/\Q$crole\E/) { return "refused:c:$crole"; }
 4673:         if ($pr{'cr:c'}=~/\Q$crole\E\&/) {
 4674: 	    if ($pr{'cr:c'}!~/\Q$crole\E\&\w*\Q$cqual\E/) { 
 4675:                return "refused:c:$crole&$cqual"; 
 4676:             }
 4677:         }
 4678:     }
 4679:     my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
 4680:                 "$env{'user.domain'}:$env{'user.name'}:".
 4681: 	        "rolesdef_$rolename=".
 4682:                 escape($sysrole.'_'.$domrole.'_'.$courole);
 4683:     return reply($command,$env{'user.home'});
 4684:   } else {
 4685:     return 'refused';
 4686:   }
 4687: }
 4688: 
 4689: # ---------------- Make a metadata query against the network of library servers
 4690: 
 4691: sub metadata_query {
 4692:     my ($query,$custom,$customshow,$server_array)=@_;
 4693:     my %rhash;
 4694:     my %libserv = &all_library();
 4695:     my @server_list = (defined($server_array) ? @$server_array
 4696:                                               : keys(%libserv) );
 4697:     for my $server (@server_list) {
 4698: 	unless ($custom or $customshow) {
 4699: 	    my $reply=&reply("querysend:".&escape($query),$server);
 4700: 	    $rhash{$server}=$reply;
 4701: 	}
 4702: 	else {
 4703: 	    my $reply=&reply("querysend:".&escape($query).':'.
 4704: 			     &escape($custom).':'.&escape($customshow),
 4705: 			     $server);
 4706: 	    $rhash{$server}=$reply;
 4707: 	}
 4708:     }
 4709:     return \%rhash;
 4710: }
 4711: 
 4712: # ----------------------------------------- Send log queries and wait for reply
 4713: 
 4714: sub log_query {
 4715:     my ($uname,$udom,$query,%filters)=@_;
 4716:     my $uhome=&homeserver($uname,$udom);
 4717:     if ($uhome eq 'no_host') { return 'error: no_host'; }
 4718:     my $uhost=&hostname($uhome);
 4719:     my $command=&escape(join(':',map{$_.'='.$filters{$_}} keys(%filters)));
 4720:     my $queryid=&reply("querysend:".$query.':'.$udom.':'.$uname.':'.$command,
 4721:                        $uhome);
 4722:     unless ($queryid=~/^\Q$uhost\E\_/) { return 'error: '.$queryid; }
 4723:     return get_query_reply($queryid);
 4724: }
 4725: 
 4726: # -------------------------- Update MySQL table for portfolio file
 4727: 
 4728: sub update_portfolio_table {
 4729:     my ($uname,$udom,$file_name,$query,$group,$action) = @_;
 4730:     my $homeserver = &homeserver($uname,$udom);
 4731:     my $queryid=
 4732:         &reply("querysend:".$query.':'.&escape($uname.':'.$udom.':'.$group).
 4733:                ':'.&escape($file_name).':'.$action,$homeserver);
 4734:     my $reply = &get_query_reply($queryid);
 4735:     return $reply;
 4736: }
 4737: 
 4738: # -------------------------- Update MySQL allusers table
 4739: 
 4740: sub update_allusers_table {
 4741:     my ($uname,$udom,$names) = @_;
 4742:     my $homeserver = &homeserver($uname,$udom);
 4743:     my $queryid=
 4744:         &reply('querysend:allusers:'.&escape($uname).':'.&escape($udom).':'.
 4745:                'lastname='.&escape($names->{'lastname'}).'%%'.
 4746:                'firstname='.&escape($names->{'firstname'}).'%%'.
 4747:                'middlename='.&escape($names->{'middlename'}).'%%'.
 4748:                'generation='.&escape($names->{'generation'}).'%%'.
 4749:                'permanentemail='.&escape($names->{'permanentemail'}).'%%'.
 4750:                'id='.&escape($names->{'id'}),$homeserver);
 4751:     my $reply = &get_query_reply($queryid);
 4752:     return $reply;
 4753: }
 4754: 
 4755: # ------- Request retrieval of institutional classlists for course(s)
 4756: 
 4757: sub fetch_enrollment_query {
 4758:     my ($context,$affiliatesref,$replyref,$dom,$cnum) = @_;
 4759:     my $homeserver;
 4760:     my $maxtries = 1;
 4761:     if ($context eq 'automated') {
 4762:         $homeserver = $perlvar{'lonHostID'};
 4763:         $maxtries = 10; # will wait for up to 2000s for retrieval of classlist data before timeout
 4764:     } else {
 4765:         $homeserver = &homeserver($cnum,$dom);
 4766:     }
 4767:     my $host=&hostname($homeserver);
 4768:     my $cmd = '';
 4769:     foreach my $affiliate (keys %{$affiliatesref}) {
 4770:         $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
 4771:     }
 4772:     $cmd =~ s/%%$//;
 4773:     $cmd = &escape($cmd);
 4774:     my $query = 'fetchenrollment';
 4775:     my $queryid=&reply("querysend:".$query.':'.$dom.':'.$env{'user.name'}.':'.$cmd,$homeserver);
 4776:     unless ($queryid=~/^\Q$host\E\_/) { 
 4777:         &logthis('fetch_enrollment_query: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' context: '.$context.' '.$cnum); 
 4778:         return 'error: '.$queryid;
 4779:     }
 4780:     my $reply = &get_query_reply($queryid);
 4781:     my $tries = 1;
 4782:     while (($reply=~/^timeout/) && ($tries < $maxtries)) {
 4783:         $reply = &get_query_reply($queryid);
 4784:         $tries ++;
 4785:     }
 4786:     if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
 4787:         &logthis('fetch_enrollment_query error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' context: '.$context.' '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
 4788:     } else {
 4789:         my @responses = split(/:/,$reply);
 4790:         if ($homeserver eq $perlvar{'lonHostID'}) {
 4791:             foreach my $line (@responses) {
 4792:                 my ($key,$value) = split(/=/,$line,2);
 4793:                 $$replyref{$key} = $value;
 4794:             }
 4795:         } else {
 4796:             my $pathname = $perlvar{'lonDaemons'}.'/tmp';
 4797:             foreach my $line (@responses) {
 4798:                 my ($key,$value) = split(/=/,$line);
 4799:                 $$replyref{$key} = $value;
 4800:                 if ($value > 0) {
 4801:                     foreach my $item (@{$$affiliatesref{$key}}) {
 4802:                         my $filename = $dom.'_'.$key.'_'.$item.'_classlist.xml';
 4803:                         my $destname = $pathname.'/'.$filename;
 4804:                         my $xml_classlist = &reply("autoretrieve:".$filename,$homeserver);
 4805:                         if ($xml_classlist =~ /^error/) {
 4806:                             &logthis('fetch_enrollment_query - autoretrieve error: '.$xml_classlist.' for '.$filename.' from server: '.$homeserver.' '.$context.' '.$cnum);
 4807:                         } else {
 4808:                             if ( open(FILE,">$destname") ) {
 4809:                                 print FILE &unescape($xml_classlist);
 4810:                                 close(FILE);
 4811:                             } else {
 4812:                                 &logthis('fetch_enrollment_query - error opening classlist file '.$destname.' '.$context.' '.$cnum);
 4813:                             }
 4814:                         }
 4815:                     }
 4816:                 }
 4817:             }
 4818:         }
 4819:         return 'ok';
 4820:     }
 4821:     return 'error';
 4822: }
 4823: 
 4824: sub get_query_reply {
 4825:     my $queryid=shift;
 4826:     my $replyfile=$perlvar{'lonDaemons'}.'/tmp/'.$queryid;
 4827:     my $reply='';
 4828:     for (1..100) {
 4829: 	sleep 2;
 4830:         if (-e $replyfile.'.end') {
 4831: 	    if (open(my $fh,$replyfile)) {
 4832: 		$reply = join('',<$fh>);
 4833: 		close($fh);
 4834: 	   } else { return 'error: reply_file_error'; }
 4835:            return &unescape($reply);
 4836: 	}
 4837:     }
 4838:     return 'timeout:'.$queryid;
 4839: }
 4840: 
 4841: sub courselog_query {
 4842: #
 4843: # possible filters:
 4844: # url: url or symb
 4845: # username
 4846: # domain
 4847: # action: view, submit, grade
 4848: # start: timestamp
 4849: # end: timestamp
 4850: #
 4851:     my (%filters)=@_;
 4852:     unless ($env{'request.course.id'}) { return 'no_course'; }
 4853:     if ($filters{'url'}) {
 4854: 	$filters{'url'}=&symbclean(&declutter($filters{'url'}));
 4855:         $filters{'url'}=~s/\.(\w+)$/(\\.\\d+)*\\.$1/;
 4856:         $filters{'url'}=~s/\.(\w+)\_\_\_/(\\.\\d+)*\\.$1/;
 4857:     }
 4858:     my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
 4859:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 4860:     return &log_query($cname,$cdom,'courselog',%filters);
 4861: }
 4862: 
 4863: sub userlog_query {
 4864: #
 4865: # possible filters:
 4866: # action: log check role
 4867: # start: timestamp
 4868: # end: timestamp
 4869: #
 4870:     my ($uname,$udom,%filters)=@_;
 4871:     return &log_query($uname,$udom,'userlog',%filters);
 4872: }
 4873: 
 4874: #--------- Call auto-enrollment subs in localenroll.pm for homeserver for course 
 4875: 
 4876: sub auto_run {
 4877:     my ($cnum,$cdom) = @_;
 4878:     my $response = 0;
 4879:     my $settings;
 4880:     my %domconfig = &get_dom('configuration',['autoenroll'],$cdom);
 4881:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 4882:         $settings = $domconfig{'autoenroll'};
 4883:         if ($settings->{'run'} eq '1') {
 4884:             $response = 1;
 4885:         }
 4886:     } else {
 4887:         my $homeserver;
 4888:         if (&is_course($cdom,$cnum)) {
 4889:             $homeserver = &homeserver($cnum,$cdom);
 4890:         } else {
 4891:             $homeserver = &domain($cdom,'primary');
 4892:         }
 4893:         if ($homeserver ne 'no_host') {
 4894:             $response = &reply('autorun:'.$cdom,$homeserver);
 4895:         }
 4896:     }
 4897:     return $response;
 4898: }
 4899: 
 4900: sub auto_get_sections {
 4901:     my ($cnum,$cdom,$inst_coursecode) = @_;
 4902:     my $homeserver = &homeserver($cnum,$cdom);
 4903:     my @secs = ();
 4904:     my $response=&unescape(&reply('autogetsections:'.$inst_coursecode.':'.$cdom,$homeserver));
 4905:     unless ($response eq 'refused') {
 4906:         @secs = split(/:/,$response);
 4907:     }
 4908:     return @secs;
 4909: }
 4910: 
 4911: sub auto_new_course {
 4912:     my ($cnum,$cdom,$inst_course_id,$owner) = @_;
 4913:     my $homeserver = &homeserver($cnum,$cdom);
 4914:     my $response=&unescape(&reply('autonewcourse:'.$inst_course_id.':'.$owner.':'.$cdom,$homeserver));
 4915:     return $response;
 4916: }
 4917: 
 4918: sub auto_validate_courseID {
 4919:     my ($cnum,$cdom,$inst_course_id) = @_;
 4920:     my $homeserver = &homeserver($cnum,$cdom);
 4921:     my $response=&unescape(&reply('autovalidatecourse:'.$inst_course_id.':'.$cdom,$homeserver));
 4922:     return $response;
 4923: }
 4924: 
 4925: sub auto_create_password {
 4926:     my ($cnum,$cdom,$authparam,$udom) = @_;
 4927:     my ($homeserver,$response);
 4928:     my $create_passwd = 0;
 4929:     my $authchk = '';
 4930:     if ($udom =~ /^$match_domain$/) {
 4931:         $homeserver = &domain($udom,'primary');
 4932:     }
 4933:     if ($homeserver eq '') {
 4934:         if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
 4935:             $homeserver = &homeserver($cnum,$cdom);
 4936:         }
 4937:     }
 4938:     if ($homeserver eq '') {
 4939:         $authchk = 'nodomain';
 4940:     } else {
 4941:         $response=&unescape(&reply('autocreatepassword:'.$authparam.':'.$cdom,$homeserver));
 4942:         if ($response eq 'refused') {
 4943:             $authchk = 'refused';
 4944:         } else {
 4945:             ($authparam,$create_passwd,$authchk) = split(/:/,$response);
 4946:         }
 4947:     }
 4948:     return ($authparam,$create_passwd,$authchk);
 4949: }
 4950: 
 4951: sub auto_photo_permission {
 4952:     my ($cnum,$cdom,$students) = @_;
 4953:     my $homeserver = &homeserver($cnum,$cdom);
 4954:     my ($outcome,$perm_reqd,$conditions) = 
 4955: 	split(/:/,&unescape(&reply('autophotopermission:'.$cdom,$homeserver)),3);
 4956:     if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
 4957: 	return (undef,undef);
 4958:     }
 4959:     return ($outcome,$perm_reqd,$conditions);
 4960: }
 4961: 
 4962: sub auto_checkphotos {
 4963:     my ($uname,$udom,$pid) = @_;
 4964:     my $homeserver = &homeserver($uname,$udom);
 4965:     my ($result,$resulttype);
 4966:     my $outcome = &unescape(&reply('autophotocheck:'.&escape($udom).':'.
 4967: 				   &escape($uname).':'.&escape($pid),
 4968: 				   $homeserver));
 4969:     if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
 4970: 	return (undef,undef);
 4971:     }
 4972:     if ($outcome) {
 4973:         ($result,$resulttype) = split(/:/,$outcome);
 4974:     } 
 4975:     return ($result,$resulttype);
 4976: }
 4977: 
 4978: sub auto_photochoice {
 4979:     my ($cnum,$cdom) = @_;
 4980:     my $homeserver = &homeserver($cnum,$cdom);
 4981:     my ($update,$comment) = split(/:/,&unescape(&reply('autophotochoice:'.
 4982: 						       &escape($cdom),
 4983: 						       $homeserver)));
 4984:     if ($update =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
 4985: 	return (undef,undef);
 4986:     }
 4987:     return ($update,$comment);
 4988: }
 4989: 
 4990: sub auto_photoupdate {
 4991:     my ($affiliatesref,$dom,$cnum,$photo) = @_;
 4992:     my $homeserver = &homeserver($cnum,$dom);
 4993:     my $host=&hostname($homeserver);
 4994:     my $cmd = '';
 4995:     my $maxtries = 1;
 4996:     foreach my $affiliate (keys(%{$affiliatesref})) {
 4997:         $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
 4998:     }
 4999:     $cmd =~ s/%%$//;
 5000:     $cmd = &escape($cmd);
 5001:     my $query = 'institutionalphotos';
 5002:     my $queryid=&reply("querysend:".$query.':'.$dom.':'.$cnum.':'.$cmd,$homeserver);
 5003:     unless ($queryid=~/^\Q$host\E\_/) {
 5004:         &logthis('institutionalphotos: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' and course: '.$cnum);
 5005:         return 'error: '.$queryid;
 5006:     }
 5007:     my $reply = &get_query_reply($queryid);
 5008:     my $tries = 1;
 5009:     while (($reply=~/^timeout/) && ($tries < $maxtries)) {
 5010:         $reply = &get_query_reply($queryid);
 5011:         $tries ++;
 5012:     }
 5013:     if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
 5014:         &logthis('institutionalphotos error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' course: '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
 5015:     } else {
 5016:         my @responses = split(/:/,$reply);
 5017:         my $outcome = shift(@responses); 
 5018:         foreach my $item (@responses) {
 5019:             my ($key,$value) = split(/=/,$item);
 5020:             $$photo{$key} = $value;
 5021:         }
 5022:         return $outcome;
 5023:     }
 5024:     return 'error';
 5025: }
 5026: 
 5027: sub auto_instcode_format {
 5028:     my ($caller,$codedom,$instcodes,$codes,$codetitles,$cat_titles,
 5029: 	$cat_order) = @_;
 5030:     my $courses = '';
 5031:     my @homeservers;
 5032:     if ($caller eq 'global') {
 5033: 	my %servers = &get_servers($codedom,'library');
 5034: 	foreach my $tryserver (keys(%servers)) {
 5035: 	    if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
 5036: 		push(@homeservers,$tryserver);
 5037: 	    }
 5038:         }
 5039:     } else {
 5040:         push(@homeservers,&homeserver($caller,$codedom));
 5041:     }
 5042:     foreach my $code (keys(%{$instcodes})) {
 5043:         $courses .= &escape($code).'='.&escape($$instcodes{$code}).'&';
 5044:     }
 5045:     chop($courses);
 5046:     my $ok_response = 0;
 5047:     my $response;
 5048:     while (@homeservers > 0 && $ok_response == 0) {
 5049:         my $server = shift(@homeservers); 
 5050:         $response=&reply('autoinstcodeformat:'.$codedom.':'.$courses,$server);
 5051:         if ($response !~ /(con_lost|error|no_such_host|refused)/) {
 5052:             my ($codes_str,$codetitles_str,$cat_titles_str,$cat_order_str) = 
 5053: 		split(/:/,$response);
 5054:             %{$codes} = (%{$codes},&str2hash($codes_str));
 5055:             push(@{$codetitles},&str2array($codetitles_str));
 5056:             %{$cat_titles} = (%{$cat_titles},&str2hash($cat_titles_str));
 5057:             %{$cat_order} = (%{$cat_order},&str2hash($cat_order_str));
 5058:             $ok_response = 1;
 5059:         }
 5060:     }
 5061:     if ($ok_response) {
 5062:         return 'ok';
 5063:     } else {
 5064:         return $response;
 5065:     }
 5066: }
 5067: 
 5068: sub auto_instcode_defaults {
 5069:     my ($domain,$returnhash,$code_order) = @_;
 5070:     my @homeservers;
 5071: 
 5072:     my %servers = &get_servers($domain,'library');
 5073:     foreach my $tryserver (keys(%servers)) {
 5074: 	if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
 5075: 	    push(@homeservers,$tryserver);
 5076: 	}
 5077:     }
 5078: 
 5079:     my $response;
 5080:     foreach my $server (@homeservers) {
 5081:         $response=&reply('autoinstcodedefaults:'.$domain,$server);
 5082:         next if ($response =~ /(con_lost|error|no_such_host|refused)/);
 5083: 	
 5084: 	foreach my $pair (split(/\&/,$response)) {
 5085: 	    my ($name,$value)=split(/\=/,$pair);
 5086: 	    if ($name eq 'code_order') {
 5087: 		@{$code_order} = split(/\&/,&unescape($value));
 5088: 	    } else {
 5089: 		$returnhash->{&unescape($name)}=&unescape($value);
 5090: 	    }
 5091: 	}
 5092: 	return 'ok';
 5093:     }
 5094: 
 5095:     return $response;
 5096: } 
 5097: 
 5098: sub auto_validate_class_sec {
 5099:     my ($cdom,$cnum,$owners,$inst_class) = @_;
 5100:     my $homeserver = &homeserver($cnum,$cdom);
 5101:     my $ownerlist;
 5102:     if (ref($owners) eq 'ARRAY') {
 5103:         $ownerlist = join(',',@{$owners});
 5104:     } else {
 5105:         $ownerlist = $owners;
 5106:     }
 5107:     my $response=&reply('autovalidateclass_sec:'.$inst_class.':'.
 5108:                         &escape($ownerlist).':'.$cdom,$homeserver);
 5109:     return $response;
 5110: }
 5111: 
 5112: # ------------------------------------------------------- Course Group routines
 5113: 
 5114: sub get_coursegroups {
 5115:     my ($cdom,$cnum,$group,$namespace) = @_;
 5116:     return(&dump($namespace,$cdom,$cnum,$group));
 5117: }
 5118: 
 5119: sub modify_coursegroup {
 5120:     my ($cdom,$cnum,$groupsettings) = @_;
 5121:     return(&put('coursegroups',$groupsettings,$cdom,$cnum));
 5122: }
 5123: 
 5124: sub toggle_coursegroup_status {
 5125:     my ($cdom,$cnum,$group,$action) = @_;
 5126:     my ($from_namespace,$to_namespace);
 5127:     if ($action eq 'delete') {
 5128:         $from_namespace = 'coursegroups';
 5129:         $to_namespace = 'deleted_groups';
 5130:     } else {
 5131:         $from_namespace = 'deleted_groups';
 5132:         $to_namespace = 'coursegroups';
 5133:     }
 5134:     my %curr_group = &get_coursegroups($cdom,$cnum,$group,$from_namespace);
 5135:     if (my $tmp = &error(%curr_group)) {
 5136:         &Apache::lonnet::logthis('Error retrieving group: '.$tmp.' in '.$cnum.':'.$cdom);
 5137:         return ('read error',$tmp);
 5138:     } else {
 5139:         my %savedsettings = %curr_group; 
 5140:         my $result = &put($to_namespace,\%savedsettings,$cdom,$cnum);
 5141:         my $deloutcome;
 5142:         if ($result eq 'ok') {
 5143:             $deloutcome = &del($from_namespace,[$group],$cdom,$cnum);
 5144:         } else {
 5145:             return ('write error',$result);
 5146:         }
 5147:         if ($deloutcome eq 'ok') {
 5148:             return 'ok';
 5149:         } else {
 5150:             return ('delete error',$deloutcome);
 5151:         }
 5152:     }
 5153: }
 5154: 
 5155: sub modify_group_roles {
 5156:     my ($cdom,$cnum,$group_id,$user,$end,$start,$userprivs) = @_;
 5157:     my $url = '/'.$cdom.'/'.$cnum.'/'.$group_id;
 5158:     my $role = 'gr/'.&escape($userprivs);
 5159:     my ($uname,$udom) = split(/:/,$user);
 5160:     my $result = &assignrole($udom,$uname,$url,$role,$end,$start);
 5161:     if ($result eq 'ok') {
 5162:         &devalidate_getgroups_cache($udom,$uname,$cdom,$cnum);
 5163:     }
 5164:     return $result;
 5165: }
 5166: 
 5167: sub modify_coursegroup_membership {
 5168:     my ($cdom,$cnum,$membership) = @_;
 5169:     my $result = &put('groupmembership',$membership,$cdom,$cnum);
 5170:     return $result;
 5171: }
 5172: 
 5173: sub get_active_groups {
 5174:     my ($udom,$uname,$cdom,$cnum) = @_;
 5175:     my $now = time;
 5176:     my %groups = ();
 5177:     foreach my $key (keys(%env)) {
 5178:         if ($key =~ m-user\.role\.gr\./($match_domain)/($match_courseid)/(\w+)$-) {
 5179:             my ($start,$end) = split(/\./,$env{$key});
 5180:             if (($end!=0) && ($end<$now)) { next; }
 5181:             if (($start!=0) && ($start>$now)) { next; }
 5182:             if ($1 eq $cdom && $2 eq $cnum) {
 5183:                 $groups{$3} = $env{$key} ;
 5184:             }
 5185:         }
 5186:     }
 5187:     return %groups;
 5188: }
 5189: 
 5190: sub get_group_membership {
 5191:     my ($cdom,$cnum,$group) = @_;
 5192:     return(&dump('groupmembership',$cdom,$cnum,$group));
 5193: }
 5194: 
 5195: sub get_users_groups {
 5196:     my ($udom,$uname,$courseid) = @_;
 5197:     my @usersgroups;
 5198:     my $cachetime=1800;
 5199: 
 5200:     my $hashid="$udom:$uname:$courseid";
 5201:     my ($grouplist,$cached)=&is_cached_new('getgroups',$hashid);
 5202:     if (defined($cached)) {
 5203:         @usersgroups = split(/:/,$grouplist);
 5204:     } else {  
 5205:         $grouplist = '';
 5206:         my $courseurl = &courseid_to_courseurl($courseid);
 5207:         my %roleshash = &dump('roles',$udom,$uname,$courseurl);
 5208:         my $access_end = $env{'course.'.$courseid.
 5209:                               '.default_enrollment_end_date'};
 5210:         my $now = time;
 5211:         foreach my $key (keys(%roleshash)) {
 5212:             if ($key =~ /^\Q$courseurl\E\/(\w+)\_gr$/) {
 5213:                 my $group = $1;
 5214:                 if ($roleshash{$key} =~ /_(\d+)_(\d+)$/) {
 5215:                     my $start = $2;
 5216:                     my $end = $1;
 5217:                     if ($start == -1) { next; } # deleted from group
 5218:                     if (($start!=0) && ($start>$now)) { next; }
 5219:                     if (($end!=0) && ($end<$now)) {
 5220:                         if ($access_end && $access_end < $now) {
 5221:                             if ($access_end - $end < 86400) {
 5222:                                 push(@usersgroups,$group);
 5223:                             }
 5224:                         }
 5225:                         next;
 5226:                     }
 5227:                     push(@usersgroups,$group);
 5228:                 }
 5229:             }
 5230:         }
 5231:         @usersgroups = &sort_course_groups($courseid,@usersgroups);
 5232:         $grouplist = join(':',@usersgroups);
 5233:         &do_cache_new('getgroups',$hashid,$grouplist,$cachetime);
 5234:     }
 5235:     return @usersgroups;
 5236: }
 5237: 
 5238: sub devalidate_getgroups_cache {
 5239:     my ($udom,$uname,$cdom,$cnum)=@_;
 5240:     my $courseid = $cdom.'_'.$cnum;
 5241: 
 5242:     my $hashid="$udom:$uname:$courseid";
 5243:     &devalidate_cache_new('getgroups',$hashid);
 5244: }
 5245: 
 5246: # ------------------------------------------------------------------ Plain Text
 5247: 
 5248: sub plaintext {
 5249:     my ($short,$type,$cid) = @_;
 5250:     if ($short =~ /^cr/) {
 5251: 	return (split('/',$short))[-1];
 5252:     }
 5253:     if (!defined($cid)) {
 5254:         $cid = $env{'request.course.id'};
 5255:     }
 5256:     if (defined($cid) && defined($env{'course.'.$cid.'.'.$short.'.plaintext'})) {
 5257:         return &Apache::lonlocal::mt($env{'course.'.$cid.'.'.$short.
 5258:                                           '.plaintext'});
 5259:     }
 5260:     my %rolenames = (
 5261:                       Course => 'std',
 5262:                       Group => 'alt1',
 5263:                     );
 5264:     if (defined($type) && 
 5265:          defined($rolenames{$type}) && 
 5266:          defined($prp{$short}{$rolenames{$type}})) {
 5267:         return &Apache::lonlocal::mt($prp{$short}{$rolenames{$type}});
 5268:     } else {
 5269:         return &Apache::lonlocal::mt($prp{$short}{'std'});
 5270:     }
 5271: }
 5272: 
 5273: # ----------------------------------------------------------------- Assign Role
 5274: 
 5275: sub assignrole {
 5276:     my ($udom,$uname,$url,$role,$end,$start,$deleteflag)=@_;
 5277:     my $mrole;
 5278:     if ($role =~ /^cr\//) {
 5279:         my $cwosec=$url;
 5280:         $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
 5281: 	unless (&allowed('ccr',$cwosec)) {
 5282:            &logthis('Refused custom assignrole: '.
 5283:              $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
 5284: 		    $env{'user.name'}.' at '.$env{'user.domain'});
 5285:            return 'refused'; 
 5286:         }
 5287:         $mrole='cr';
 5288:     } elsif ($role =~ /^gr\//) {
 5289:         my $cwogrp=$url;
 5290:         $cwogrp=~s{^/($match_domain)/($match_courseid)/.*}{$1/$2};
 5291:         unless (&allowed('mdg',$cwogrp)) {
 5292:             &logthis('Refused group assignrole: '.
 5293:               $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
 5294:                     $env{'user.name'}.' at '.$env{'user.domain'});
 5295:             return 'refused';
 5296:         }
 5297:         $mrole='gr';
 5298:     } else {
 5299:         my $cwosec=$url;
 5300:         $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
 5301:         if (!(&allowed('c'.$role,$cwosec)) && !(&allowed('c'.$role,$udom))) {
 5302:             my $refused;
 5303:             if (($env{'request.course.sec'}  ne '') && ($role eq 'st')) {
 5304:                 if (!(&allowed('c'.$role,$url))) {
 5305:                     $refused = 1;
 5306:                 }
 5307:             } else {
 5308:                 $refused = 1;
 5309:             }
 5310:             if ($refused) { 
 5311:                 &logthis('Refused assignrole: '.$udom.' '.$uname.' '.$url.
 5312:                          ' '.$role.' '.$end.' '.$start.' by '.
 5313: 	  	         $env{'user.name'}.' at '.$env{'user.domain'});
 5314:                 return 'refused';
 5315:             }
 5316:         }
 5317:         $mrole=$role;
 5318:     }
 5319:     my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
 5320:                 "$udom:$uname:$url".'_'."$mrole=$role";
 5321:     if ($end) { $command.='_'.$end; }
 5322:     if ($start) {
 5323: 	if ($end) { 
 5324:            $command.='_'.$start; 
 5325:         } else {
 5326:            $command.='_0_'.$start;
 5327:         }
 5328:     }
 5329:     my $origstart = $start;
 5330:     my $origend = $end;
 5331: # actually delete
 5332:     if ($deleteflag) {
 5333: 	if ((&allowed('dro',$udom)) || (&allowed('dro',$url))) {
 5334: # modify command to delete the role
 5335:            $command="encrypt:rolesdel:$env{'user.domain'}:$env{'user.name'}:".
 5336:                 "$udom:$uname:$url".'_'."$mrole";
 5337: 	   &logthis("$env{'user.name'} at $env{'user.domain'} deletes $mrole in $url for $uname at $udom"); 
 5338: # set start and finish to negative values for userrolelog
 5339:            $start=-1;
 5340:            $end=-1;
 5341:         }
 5342:     }
 5343: # send command
 5344:     my $answer=&reply($command,&homeserver($uname,$udom));
 5345: # log new user role if status is ok
 5346:     if ($answer eq 'ok') {
 5347: 	&userrolelog($role,$uname,$udom,$url,$start,$end);
 5348: # for course roles, perform group memberships changes triggered by role change.
 5349:         unless ($role =~ /^gr/) {
 5350:             &Apache::longroup::group_changes($udom,$uname,$url,$role,$origend,
 5351:                                              $origstart);
 5352:         }
 5353:     }
 5354:     return $answer;
 5355: }
 5356: 
 5357: # -------------------------------------------------- Modify user authentication
 5358: # Overrides without validation
 5359: 
 5360: sub modifyuserauth {
 5361:     my ($udom,$uname,$umode,$upass)=@_;
 5362:     my $uhome=&homeserver($uname,$udom);
 5363:     unless (&allowed('mau',$udom)) { return 'refused'; }
 5364:     &logthis('Call to modify user authentication '.$udom.', '.$uname.', '.
 5365:              $umode.' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
 5366:              ' in domain '.$env{'request.role.domain'});  
 5367:     my $reply=&reply('encrypt:changeuserauth:'.$udom.':'.$uname.':'.$umode.':'.
 5368: 		     &escape($upass),$uhome);
 5369:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
 5370:         'Authentication changed for '.$udom.', '.$uname.', '.$umode.
 5371:          '(Remote '.$ENV{'REMOTE_ADDR'}.'): '.$reply);
 5372:     &log($udom,,$uname,$uhome,
 5373:         'Authentication changed by '.$env{'user.domain'}.', '.
 5374:                                      $env{'user.name'}.', '.$umode.
 5375:          '(Remote '.$ENV{'REMOTE_ADDR'}.'): '.$reply);
 5376:     unless ($reply eq 'ok') {
 5377:         &logthis('Authentication mode error: '.$reply);
 5378: 	return 'error: '.$reply;
 5379:     }   
 5380:     return 'ok';
 5381: }
 5382: 
 5383: # --------------------------------------------------------------- Modify a user
 5384: 
 5385: sub modifyuser {
 5386:     my ($udom,    $uname, $uid,
 5387:         $umode,   $upass, $first,
 5388:         $middle,  $last,  $gene,
 5389:         $forceid, $desiredhome, $email)=@_;
 5390:     $udom= &LONCAPA::clean_domain($udom);
 5391:     $uname=&LONCAPA::clean_username($uname);
 5392:     &logthis('Call to modify user '.$udom.', '.$uname.', '.$uid.', '.
 5393:              $umode.', '.$first.', '.$middle.', '.
 5394: 	     $last.', '.$gene.'(forceid: '.$forceid.')'.
 5395:              (defined($desiredhome) ? ' desiredhome = '.$desiredhome :
 5396:                                      ' desiredhome not specified'). 
 5397:              ' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
 5398:              ' in domain '.$env{'request.role.domain'});
 5399:     my $uhome=&homeserver($uname,$udom,'true');
 5400: # ----------------------------------------------------------------- Create User
 5401:     if (($uhome eq 'no_host') && 
 5402: 	(($umode && $upass) || ($umode eq 'localauth'))) {
 5403:         my $unhome='';
 5404:         if (defined($desiredhome) && &host_domain($desiredhome) eq $udom) { 
 5405:             $unhome = $desiredhome;
 5406: 	} elsif($env{'course.'.$env{'request.course.id'}.'.domain'} eq $udom) {
 5407: 	    $unhome=$env{'course.'.$env{'request.course.id'}.'.home'};
 5408:         } else { # load balancing routine for determining $unhome
 5409:             my $loadm=10000000;
 5410: 	    my %servers = &get_servers($udom,'library');
 5411: 	    foreach my $tryserver (keys(%servers)) {
 5412: 		my $answer=reply('load',$tryserver);
 5413: 		if (($answer=~/\d+/) && ($answer<$loadm)) {
 5414: 		    $loadm=$answer;
 5415: 		    $unhome=$tryserver;
 5416: 		}
 5417: 	    }
 5418:         }
 5419:         if (($unhome eq '') || ($unhome eq 'no_host')) {
 5420: 	    return 'error: unable to find a home server for '.$uname.
 5421:                    ' in domain '.$udom;
 5422:         }
 5423:         my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':'.$umode.':'.
 5424:                          &escape($upass),$unhome);
 5425: 	unless ($reply eq 'ok') {
 5426:             return 'error: '.$reply;
 5427:         }   
 5428:         $uhome=&homeserver($uname,$udom,'true');
 5429:         if (($uhome eq '') || ($uhome eq 'no_host') || ($uhome ne $unhome)) {
 5430: 	    return 'error: unable verify users home machine.';
 5431:         }
 5432:     }   # End of creation of new user
 5433: # ---------------------------------------------------------------------- Add ID
 5434:     if ($uid) {
 5435:        $uid=~tr/A-Z/a-z/;
 5436:        my %uidhash=&idrget($udom,$uname);
 5437:        if (($uidhash{$uname}) && ($uidhash{$uname}!~/error\:/) 
 5438:          && (!$forceid)) {
 5439: 	  unless ($uid eq $uidhash{$uname}) {
 5440: 	      return 'error: user id "'.$uid.'" does not match '.
 5441:                   'current user id "'.$uidhash{$uname}.'".';
 5442:           }
 5443:        } else {
 5444: 	  &idput($udom,($uname => $uid));
 5445:        }
 5446:     }
 5447: # -------------------------------------------------------------- Add names, etc
 5448:     my @tmp=&get('environment',
 5449: 		   ['firstname','middlename','lastname','generation','id',
 5450:                     'permanentemail'],
 5451: 		   $udom,$uname);
 5452:     my %names;
 5453:     if ($tmp[0] =~ m/^error:.*/) { 
 5454:         %names=(); 
 5455:     } else {
 5456:         %names = @tmp;
 5457:     }
 5458: #
 5459: # Make sure to not trash student environment if instructor does not bother
 5460: # to supply name and email information
 5461: #
 5462:     if ($first)  { $names{'firstname'}  = $first; }
 5463:     if (defined($middle)) { $names{'middlename'} = $middle; }
 5464:     if ($last)   { $names{'lastname'}   = $last; }
 5465:     if (defined($gene))   { $names{'generation'} = $gene; }
 5466:     if ($email) {
 5467:        $email=~s/[^\w\@\.\-\,]//gs;
 5468:        if ($email=~/\@/) { $names{'notification'} = $email;
 5469: 			   $names{'critnotification'} = $email;
 5470: 			   $names{'permanentemail'} = $email; }
 5471:     }
 5472:     if ($uid) { $names{'id'}  = $uid; }
 5473:     my $reply = &put('environment', \%names, $udom,$uname);
 5474:     if ($reply ne 'ok') { return 'error: '.$reply; }
 5475:     my $sqlresult = &update_allusers_table($uname,$udom,\%names);
 5476:     &devalidate_cache_new('namescache',$uname.':'.$udom);
 5477:     &logthis('Success modifying user '.$udom.', '.$uname.', '.$uid.', '.
 5478:              $umode.', '.$first.', '.$middle.', '.
 5479: 	     $last.', '.$gene.' by '.
 5480:              $env{'user.name'}.' at '.$env{'user.domain'});
 5481:     return 'ok';
 5482: }
 5483: 
 5484: # -------------------------------------------------------------- Modify student
 5485: 
 5486: sub modifystudent {
 5487:     my ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$usec,
 5488:         $end,$start,$forceid,$desiredhome,$email,$type,$locktype,$cid)=@_;
 5489:     if (!$cid) {
 5490: 	unless ($cid=$env{'request.course.id'}) {
 5491: 	    return 'not_in_class';
 5492: 	}
 5493:     }
 5494: # --------------------------------------------------------------- Make the user
 5495:     my $reply=&modifyuser
 5496: 	($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$forceid,
 5497:          $desiredhome,$email);
 5498:     unless ($reply eq 'ok') { return $reply; }
 5499:     # This will cause &modify_student_enrollment to get the uid from the
 5500:     # students environment
 5501:     $uid = undef if (!$forceid);
 5502:     $reply = &modify_student_enrollment($udom,$uname,$uid,$first,$middle,$last,
 5503: 					$gene,$usec,$end,$start,$type,$locktype,$cid);
 5504:     return $reply;
 5505: }
 5506: 
 5507: sub modify_student_enrollment {
 5508:     my ($udom,$uname,$uid,$first,$middle,$last,$gene,$usec,$end,$start,$type,$locktype,$cid) = @_;
 5509:     my ($cdom,$cnum,$chome);
 5510:     if (!$cid) {
 5511: 	unless ($cid=$env{'request.course.id'}) {
 5512: 	    return 'not_in_class';
 5513: 	}
 5514: 	$cdom=$env{'course.'.$cid.'.domain'};
 5515: 	$cnum=$env{'course.'.$cid.'.num'};
 5516:     } else {
 5517: 	($cdom,$cnum)=split(/_/,$cid);
 5518:     }
 5519:     $chome=$env{'course.'.$cid.'.home'};
 5520:     if (!$chome) {
 5521: 	$chome=&homeserver($cnum,$cdom);
 5522:     }
 5523:     if (!$chome) { return 'unknown_course'; }
 5524:     # Make sure the user exists
 5525:     my $uhome=&homeserver($uname,$udom);
 5526:     if (($uhome eq '') || ($uhome eq 'no_host')) { 
 5527: 	return 'error: no such user';
 5528:     }
 5529:     # Get student data if we were not given enough information
 5530:     if (!defined($first)  || $first  eq '' || 
 5531:         !defined($last)   || $last   eq '' || 
 5532:         !defined($uid)    || $uid    eq '' || 
 5533:         !defined($middle) || $middle eq '' || 
 5534:         !defined($gene)   || $gene   eq '') {
 5535:         # They did not supply us with enough data to enroll the student, so
 5536:         # we need to pick up more information.
 5537:         my %tmp = &get('environment',
 5538:                        ['firstname','middlename','lastname', 'generation','id']
 5539:                        ,$udom,$uname);
 5540: 
 5541:         #foreach my $key (keys(%tmp)) {
 5542:         #    &logthis("key $key = ".$tmp{$key});
 5543:         #}
 5544:         $first  = $tmp{'firstname'}  if (!defined($first)  || $first  eq '');
 5545:         $middle = $tmp{'middlename'} if (!defined($middle) || $middle eq '');
 5546:         $last   = $tmp{'lastname'}   if (!defined($last)   || $last eq '');
 5547:         $gene   = $tmp{'generation'} if (!defined($gene)   || $gene eq '');
 5548:         $uid    = $tmp{'id'}         if (!defined($uid)    || $uid  eq '');
 5549:     }
 5550:     my $fullname = &format_name($first,$middle,$last,$gene,'lastname');
 5551:     my $reply=cput('classlist',
 5552: 		   {"$uname:$udom" => 
 5553: 			join(':',$end,$start,$uid,$usec,$fullname,$type,$locktype) },
 5554: 		   $cdom,$cnum);
 5555:     unless (($reply eq 'ok') || ($reply eq 'delayed')) {
 5556: 	return 'error: '.$reply;
 5557:     } else {
 5558: 	&devalidate_getsection_cache($udom,$uname,$cid);
 5559:     }
 5560:     # Add student role to user
 5561:     my $uurl='/'.$cid;
 5562:     $uurl=~s/\_/\//g;
 5563:     if ($usec) {
 5564: 	$uurl.='/'.$usec;
 5565:     }
 5566:     return &assignrole($udom,$uname,$uurl,'st',$end,$start);
 5567: }
 5568: 
 5569: sub format_name {
 5570:     my ($firstname,$middlename,$lastname,$generation,$first)=@_;
 5571:     my $name;
 5572:     if ($first ne 'lastname') {
 5573: 	$name=$firstname.' '.$middlename.' '.$lastname.' '.$generation;
 5574:     } else {
 5575: 	if ($lastname=~/\S/) {
 5576: 	    $name.= $lastname.' '.$generation.', '.$firstname.' '.$middlename;
 5577: 	    $name=~s/\s+,/,/;
 5578: 	} else {
 5579: 	    $name.= $firstname.' '.$middlename.' '.$generation;
 5580: 	}
 5581:     }
 5582:     $name=~s/^\s+//;
 5583:     $name=~s/\s+$//;
 5584:     $name=~s/\s+/ /g;
 5585:     return $name;
 5586: }
 5587: 
 5588: # ------------------------------------------------- Write to course preferences
 5589: 
 5590: sub writecoursepref {
 5591:     my ($courseid,%prefs)=@_;
 5592:     $courseid=~s/^\///;
 5593:     $courseid=~s/\_/\//g;
 5594:     my ($cdomain,$cnum)=split(/\//,$courseid);
 5595:     my $chome=homeserver($cnum,$cdomain);
 5596:     if (($chome eq '') || ($chome eq 'no_host')) { 
 5597: 	return 'error: no such course';
 5598:     }
 5599:     my $cstring='';
 5600:     foreach my $pref (keys(%prefs)) {
 5601: 	$cstring.=&escape($pref).'='.&escape($prefs{$pref}).'&';
 5602:     }
 5603:     $cstring=~s/\&$//;
 5604:     return reply('put:'.$cdomain.':'.$cnum.':environment:'.$cstring,$chome);
 5605: }
 5606: 
 5607: # ---------------------------------------------------------- Make/modify course
 5608: 
 5609: sub createcourse {
 5610:     my ($udom,$description,$url,$course_server,$nonstandard,$inst_code,
 5611:         $course_owner,$crstype)=@_;
 5612:     $url=&declutter($url);
 5613:     my $cid='';
 5614:     unless (&allowed('ccc',$udom)) {
 5615:         return 'refused';
 5616:     }
 5617: # ------------------------------------------------------------------- Create ID
 5618:    my $uname=int(1+rand(9)).
 5619:        ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
 5620:        substr($$.time,0,5).unpack("H8",pack("I32",time)).
 5621:        unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
 5622: # ----------------------------------------------- Make sure that does not exist
 5623:    my $uhome=&homeserver($uname,$udom,'true');
 5624:    unless (($uhome eq '') || ($uhome eq 'no_host')) {
 5625:        $uname=substr($$.time,0,5).unpack("H8",pack("I32",time)).
 5626:         unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
 5627:        $uhome=&homeserver($uname,$udom,'true');       
 5628:        unless (($uhome eq '') || ($uhome eq 'no_host')) {
 5629:            return 'error: unable to generate unique course-ID';
 5630:        } 
 5631:    }
 5632: # ------------------------------------------------ Check supplied server name
 5633:     $course_server = $env{'user.homeserver'} if (! defined($course_server));
 5634:     if (! &is_library($course_server)) {
 5635:         return 'error:bad server name '.$course_server;
 5636:     }
 5637: # ------------------------------------------------------------- Make the course
 5638:     my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':none::',
 5639:                       $course_server);
 5640:     unless ($reply eq 'ok') { return 'error: '.$reply; }
 5641:     $uhome=&homeserver($uname,$udom,'true');
 5642:     if (($uhome eq '') || ($uhome eq 'no_host')) { 
 5643: 	return 'error: no such course';
 5644:     }
 5645: # ----------------------------------------------------------------- Course made
 5646: # log existence
 5647:     my $newcourse = {
 5648:                     $udom.'_'.$uname => {
 5649:                                      description => $description,
 5650:                                      inst_code   => $inst_code,
 5651:                                      owner       => $course_owner,
 5652:                                      type        => $crstype,
 5653:                                                 },
 5654:                     };
 5655:     &courseidput($udom,$newcourse,$uhome,'notime');
 5656: # set toplevel url
 5657:     my $topurl=$url;
 5658:     unless ($nonstandard) {
 5659: # ------------------------------------------ For standard courses, make top url
 5660:         my $mapurl=&clutter($url);
 5661:         if ($mapurl eq '/res/') { $mapurl=''; }
 5662:         $env{'form.initmap'}=(<<ENDINITMAP);
 5663: <map>
 5664: <resource id="1" type="start"></resource>
 5665: <resource id="2" src="$mapurl"></resource>
 5666: <resource id="3" type="finish"></resource>
 5667: <link index="1" from="1" to="2"></link>
 5668: <link index="2" from="2" to="3"></link>
 5669: </map>
 5670: ENDINITMAP
 5671:         $topurl=&declutter(
 5672:         &finishuserfileupload($uname,$udom,'initmap','default.sequence')
 5673:                           );
 5674:     }
 5675: # ----------------------------------------------------------- Write preferences
 5676:     &writecoursepref($udom.'_'.$uname,
 5677:                      ('description' => $description,
 5678:                       'url'         => $topurl));
 5679:     return '/'.$udom.'/'.$uname;
 5680: }
 5681: 
 5682: sub is_course {
 5683:     my ($cdom,$cnum) = @_;
 5684:     my %courses = &courseiddump($cdom,'.',1,'.','.',$cnum,undef,
 5685: 				undef,'.',undef,1);
 5686:     if (exists($courses{$cdom.'_'.$cnum})) {
 5687:         return 1;
 5688:     }
 5689:     return 0;
 5690: }
 5691: 
 5692: # ---------------------------------------------------------- Assign Custom Role
 5693: 
 5694: sub assigncustomrole {
 5695:     my ($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start,$deleteflag)=@_;
 5696:     return &assignrole($udom,$uname,$url,'cr/'.$rdom.'/'.$rnam.'/'.$rolename,
 5697:                        $end,$start,$deleteflag);
 5698: }
 5699: 
 5700: # ----------------------------------------------------------------- Revoke Role
 5701: 
 5702: sub revokerole {
 5703:     my ($udom,$uname,$url,$role,$deleteflag)=@_;
 5704:     my $now=time;
 5705:     return &assignrole($udom,$uname,$url,$role,$now,$deleteflag);
 5706: }
 5707: 
 5708: # ---------------------------------------------------------- Revoke Custom Role
 5709: 
 5710: sub revokecustomrole {
 5711:     my ($udom,$uname,$url,$rdom,$rnam,$rolename,$deleteflag)=@_;
 5712:     my $now=time;
 5713:     return &assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$now,
 5714:            $deleteflag);
 5715: }
 5716: 
 5717: # ------------------------------------------------------------ Disk usage
 5718: sub diskusage {
 5719:     my ($udom,$uname,$directoryRoot)=@_;
 5720:     $directoryRoot =~ s/\/$//;
 5721:     my $listing=&reply('du:'.$directoryRoot,homeserver($uname,$udom));
 5722:     return $listing;
 5723: }
 5724: 
 5725: sub is_locked {
 5726:     my ($file_name, $domain, $user) = @_;
 5727:     my @check;
 5728:     my $is_locked;
 5729:     push @check, $file_name;
 5730:     my %locked = &get('file_permissions',\@check,
 5731: 		      $env{'user.domain'},$env{'user.name'});
 5732:     my ($tmp)=keys(%locked);
 5733:     if ($tmp=~/^error:/) { undef(%locked); }
 5734:     
 5735:     if (ref($locked{$file_name}) eq 'ARRAY') {
 5736:         $is_locked = 'false';
 5737:         foreach my $entry (@{$locked{$file_name}}) {
 5738:            if (ref($entry) eq 'ARRAY') { 
 5739:                $is_locked = 'true';
 5740:                last;
 5741:            }
 5742:        }
 5743:     } else {
 5744:         $is_locked = 'false';
 5745:     }
 5746: }
 5747: 
 5748: sub declutter_portfile {
 5749:     my ($file) = @_;
 5750:     $file =~ s{^(/portfolio/|portfolio/)}{/};
 5751:     return $file;
 5752: }
 5753: 
 5754: # ------------------------------------------------------------- Mark as Read Only
 5755: 
 5756: sub mark_as_readonly {
 5757:     my ($domain,$user,$files,$what) = @_;
 5758:     my %current_permissions = &dump('file_permissions',$domain,$user);
 5759:     my ($tmp)=keys(%current_permissions);
 5760:     if ($tmp=~/^error:/) { undef(%current_permissions); }
 5761:     foreach my $file (@{$files}) {
 5762: 	$file = &declutter_portfile($file);
 5763:         push(@{$current_permissions{$file}},$what);
 5764:     }
 5765:     &put('file_permissions',\%current_permissions,$domain,$user);
 5766:     return;
 5767: }
 5768: 
 5769: # ------------------------------------------------------------Save Selected Files
 5770: 
 5771: sub save_selected_files {
 5772:     my ($user, $path, @files) = @_;
 5773:     my $filename = $user."savedfiles";
 5774:     my @other_files = &files_not_in_path($user, $path);
 5775:     open (OUT, '>'.$tmpdir.$filename);
 5776:     foreach my $file (@files) {
 5777:         print (OUT $env{'form.currentpath'}.$file."\n");
 5778:     }
 5779:     foreach my $file (@other_files) {
 5780:         print (OUT $file."\n");
 5781:     }
 5782:     close (OUT);
 5783:     return 'ok';
 5784: }
 5785: 
 5786: sub clear_selected_files {
 5787:     my ($user) = @_;
 5788:     my $filename = $user."savedfiles";
 5789:     open (OUT, '>'.$Apache::lonnet::perlvar{'lonDaemons'}.'/tmp/'.$filename);
 5790:     print (OUT undef);
 5791:     close (OUT);
 5792:     return ("ok");    
 5793: }
 5794: 
 5795: sub files_in_path {
 5796:     my ($user, $path) = @_;
 5797:     my $filename = $user."savedfiles";
 5798:     my %return_files;
 5799:     open (IN, '<'.$Apache::lonnet::perlvar{'lonDaemons'}.'/tmp/'.$filename);
 5800:     while (my $line_in = <IN>) {
 5801:         chomp ($line_in);
 5802:         my @paths_and_file = split (m!/!, $line_in);
 5803:         my $file_part = pop (@paths_and_file);
 5804:         my $path_part = join ('/', @paths_and_file);
 5805:         $path_part.='/';
 5806:         my $path_and_file = $path_part.$file_part;
 5807:         if ($path_part eq $path) {
 5808:             $return_files{$file_part}= 'selected';
 5809:         }
 5810:     }
 5811:     close (IN);
 5812:     return (\%return_files);
 5813: }
 5814: 
 5815: # called in portfolio select mode, to show files selected NOT in current directory
 5816: sub files_not_in_path {
 5817:     my ($user, $path) = @_;
 5818:     my $filename = $user."savedfiles";
 5819:     my @return_files;
 5820:     my $path_part;
 5821:     open(IN, '<'.$Apache::lonnet::perlvar{'lonDaemons'}.'/tmp/'.$filename);
 5822:     while (my $line = <IN>) {
 5823:         #ok, I know it's clunky, but I want it to work
 5824:         my @paths_and_file = split(m|/|, $line);
 5825:         my $file_part = pop(@paths_and_file);
 5826:         chomp($file_part);
 5827:         my $path_part = join('/', @paths_and_file);
 5828:         $path_part .= '/';
 5829:         my $path_and_file = $path_part.$file_part;
 5830:         if ($path_part ne $path) {
 5831:             push(@return_files, ($path_and_file));
 5832:         }
 5833:     }
 5834:     close(OUT);
 5835:     return (@return_files);
 5836: }
 5837: 
 5838: #----------------------------------------------Get portfolio file permissions
 5839: 
 5840: sub get_portfile_permissions {
 5841:     my ($domain,$user) = @_;
 5842:     my %current_permissions = &dump('file_permissions',$domain,$user);
 5843:     my ($tmp)=keys(%current_permissions);
 5844:     if ($tmp=~/^error:/) { undef(%current_permissions); }
 5845:     return \%current_permissions;
 5846: }
 5847: 
 5848: #---------------------------------------------Get portfolio file access controls
 5849: 
 5850: sub get_access_controls {
 5851:     my ($current_permissions,$group,$file) = @_;
 5852:     my %access;
 5853:     my $real_file = $file;
 5854:     $file =~ s/\.meta$//;
 5855:     if (defined($file)) {
 5856:         if (ref($$current_permissions{$file."\0".'accesscontrol'}) eq 'HASH') {
 5857:             foreach my $control (keys(%{$$current_permissions{$file."\0".'accesscontrol'}})) {
 5858:                 $access{$real_file}{$control} = $$current_permissions{$file."\0".$control};
 5859:             }
 5860:         }
 5861:     } else {
 5862:         foreach my $key (keys(%{$current_permissions})) {
 5863:             if ($key =~ /\0accesscontrol$/) {
 5864:                 if (defined($group)) {
 5865:                     if ($key !~ m-^\Q$group\E/-) {
 5866:                         next;
 5867:                     }
 5868:                 }
 5869:                 my ($fullpath) = split(/\0/,$key);
 5870:                 if (ref($$current_permissions{$key}) eq 'HASH') {
 5871:                     foreach my $control (keys(%{$$current_permissions{$key}})) {
 5872:                         $access{$fullpath}{$control}=$$current_permissions{$fullpath."\0".$control};
 5873:                     }
 5874:                 }
 5875:             }
 5876:         }
 5877:     }
 5878:     return %access;
 5879: }
 5880: 
 5881: sub modify_access_controls {
 5882:     my ($file_name,$changes,$domain,$user)=@_;
 5883:     my ($outcome,$deloutcome);
 5884:     my %store_permissions;
 5885:     my %new_values;
 5886:     my %new_control;
 5887:     my %translation;
 5888:     my @deletions = ();
 5889:     my $now = time;
 5890:     if (exists($$changes{'activate'})) {
 5891:         if (ref($$changes{'activate'}) eq 'HASH') {
 5892:             my @newitems = sort(keys(%{$$changes{'activate'}}));
 5893:             my $numnew = scalar(@newitems);
 5894:             for (my $i=0; $i<$numnew; $i++) {
 5895:                 my $newkey = $newitems[$i];
 5896:                 my $newid = &Apache::loncommon::get_cgi_id();
 5897:                 if ($newkey =~ /^\d+:/) { 
 5898:                     $newkey =~ s/^(\d+)/$newid/;
 5899:                     $translation{$1} = $newid;
 5900:                 } elsif ($newkey =~ /^\d+_\d+_\d+:/) {
 5901:                     $newkey =~ s/^(\d+_\d+_\d+)/$newid/;
 5902:                     $translation{$1} = $newid;
 5903:                 }
 5904:                 $new_values{$file_name."\0".$newkey} = 
 5905:                                           $$changes{'activate'}{$newitems[$i]};
 5906:                 $new_control{$newkey} = $now;
 5907:             }
 5908:         }
 5909:     }
 5910:     my %todelete;
 5911:     my %changed_items;
 5912:     foreach my $action ('delete','update') {
 5913:         if (exists($$changes{$action})) {
 5914:             if (ref($$changes{$action}) eq 'HASH') {
 5915:                 foreach my $key (keys(%{$$changes{$action}})) {
 5916:                     my ($itemnum) = ($key =~ /^([^:]+):/);
 5917:                     if ($action eq 'delete') { 
 5918:                         $todelete{$itemnum} = 1;
 5919:                     } else {
 5920:                         $changed_items{$itemnum} = $key;
 5921:                     }
 5922:                 }
 5923:             }
 5924:         }
 5925:     }
 5926:     # get lock on access controls for file.
 5927:     my $lockhash = {
 5928:                   $file_name."\0".'locked_access_records' => $env{'user.name'}.
 5929:                                                        ':'.$env{'user.domain'},
 5930:                    }; 
 5931:     my $tries = 0;
 5932:     my $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
 5933:    
 5934:     while (($gotlock ne 'ok') && $tries <3) {
 5935:         $tries ++;
 5936:         sleep 1;
 5937:         $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
 5938:     }
 5939:     if ($gotlock eq 'ok') {
 5940:         my %curr_permissions = &dump('file_permissions',$domain,$user,$file_name);
 5941:         my ($tmp)=keys(%curr_permissions);
 5942:         if ($tmp=~/^error:/) { undef(%curr_permissions); }
 5943:         if (exists($curr_permissions{$file_name."\0".'accesscontrol'})) {
 5944:             my $curr_controls = $curr_permissions{$file_name."\0".'accesscontrol'};
 5945:             if (ref($curr_controls) eq 'HASH') {
 5946:                 foreach my $control_item (keys(%{$curr_controls})) {
 5947:                     my ($itemnum) = ($control_item =~ /^([^:]+):/);
 5948:                     if (defined($todelete{$itemnum})) {
 5949:                         push(@deletions,$file_name."\0".$control_item);
 5950:                     } else {
 5951:                         if (defined($changed_items{$itemnum})) {
 5952:                             $new_control{$changed_items{$itemnum}} = $now;
 5953:                             push(@deletions,$file_name."\0".$control_item);
 5954:                             $new_values{$file_name."\0".$changed_items{$itemnum}} = $$changes{'update'}{$changed_items{$itemnum}};
 5955:                         } else {
 5956:                             $new_control{$control_item} = $$curr_controls{$control_item};
 5957:                         }
 5958:                     }
 5959:                 }
 5960:             }
 5961:         }
 5962:         $deloutcome = &del('file_permissions',\@deletions,$domain,$user);
 5963:         $new_values{$file_name."\0".'accesscontrol'} = \%new_control;
 5964:         $outcome = &put('file_permissions',\%new_values,$domain,$user);
 5965:         #  remove lock
 5966:         my @del_lock = ($file_name."\0".'locked_access_records');
 5967:         my $dellockoutcome = &del('file_permissions',\@del_lock,$domain,$user);
 5968:         my ($file,$group);
 5969:         if (&is_course($domain,$user)) {
 5970:             ($group,$file) = split(/\//,$file_name,2);
 5971:         } else {
 5972:             $file = $file_name;
 5973:         }
 5974:         my $sqlresult =
 5975:             &update_portfolio_table($user,$domain,$file,'portfolio_access',
 5976:                                     $group);
 5977:     } else {
 5978:         $outcome = "error: could not obtain lockfile\n";  
 5979:     }
 5980:     return ($outcome,$deloutcome,\%new_values,\%translation);
 5981: }
 5982: 
 5983: sub make_public_indefinitely {
 5984:     my ($requrl) = @_;
 5985:     my $now = time;
 5986:     my $action = 'activate';
 5987:     my $aclnum = 0;
 5988:     if (&is_portfolio_url($requrl)) {
 5989:         my (undef,$udom,$unum,$file_name,$group) =
 5990:             &parse_portfolio_url($requrl);
 5991:         my $current_perms = &get_portfile_permissions($udom,$unum);
 5992:         my %access_controls = &get_access_controls($current_perms,
 5993:                                                    $group,$file_name);
 5994:         foreach my $key (keys(%{$access_controls{$file_name}})) {
 5995:             my ($num,$scope,$end,$start) = 
 5996:                 ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
 5997:             if ($scope eq 'public') {
 5998:                 if ($start <= $now && $end == 0) {
 5999:                     $action = 'none';
 6000:                 } else {
 6001:                     $action = 'update';
 6002:                     $aclnum = $num;
 6003:                 }
 6004:                 last;
 6005:             }
 6006:         }
 6007:         if ($action eq 'none') {
 6008:              return 'ok';
 6009:         } else {
 6010:             my %changes;
 6011:             my $newend = 0;
 6012:             my $newstart = $now;
 6013:             my $newkey = $aclnum.':public_'.$newend.'_'.$newstart;
 6014:             $changes{$action}{$newkey} = {
 6015:                 type => 'public',
 6016:                 time => {
 6017:                     start => $newstart,
 6018:                     end   => $newend,
 6019:                 },
 6020:             };
 6021:             my ($outcome,$deloutcome,$new_values,$translation) =
 6022:                 &modify_access_controls($file_name,\%changes,$udom,$unum);
 6023:             return $outcome;
 6024:         }
 6025:     } else {
 6026:         return 'invalid';
 6027:     }
 6028: }
 6029: 
 6030: #------------------------------------------------------Get Marked as Read Only
 6031: 
 6032: sub get_marked_as_readonly {
 6033:     my ($domain,$user,$what,$group) = @_;
 6034:     my $current_permissions = &get_portfile_permissions($domain,$user);
 6035:     my @readonly_files;
 6036:     my $cmp1=$what;
 6037:     if (ref($what)) { $cmp1=join('',@{$what}) };
 6038:     while (my ($file_name,$value) = each(%{$current_permissions})) {
 6039:         if (defined($group)) {
 6040:             if ($file_name !~ m-^\Q$group\E/-) {
 6041:                 next;
 6042:             }
 6043:         }
 6044:         if (ref($value) eq "ARRAY"){
 6045:             foreach my $stored_what (@{$value}) {
 6046:                 my $cmp2=$stored_what;
 6047:                 if (ref($stored_what) eq 'ARRAY') {
 6048:                     $cmp2=join('',@{$stored_what});
 6049:                 }
 6050:                 if ($cmp1 eq $cmp2) {
 6051:                     push(@readonly_files, $file_name);
 6052:                     last;
 6053:                 } elsif (!defined($what)) {
 6054:                     push(@readonly_files, $file_name);
 6055:                     last;
 6056:                 }
 6057:             }
 6058:         }
 6059:     }
 6060:     return @readonly_files;
 6061: }
 6062: #-----------------------------------------------------------Get Marked as Read Only Hash
 6063: 
 6064: sub get_marked_as_readonly_hash {
 6065:     my ($current_permissions,$group,$what) = @_;
 6066:     my %readonly_files;
 6067:     while (my ($file_name,$value) = each(%{$current_permissions})) {
 6068:         if (defined($group)) {
 6069:             if ($file_name !~ m-^\Q$group\E/-) {
 6070:                 next;
 6071:             }
 6072:         }
 6073:         if (ref($value) eq "ARRAY"){
 6074:             foreach my $stored_what (@{$value}) {
 6075:                 if (ref($stored_what) eq 'ARRAY') {
 6076:                     foreach my $lock_descriptor(@{$stored_what}) {
 6077:                         if ($lock_descriptor eq 'graded') {
 6078:                             $readonly_files{$file_name} = 'graded';
 6079:                         } elsif ($lock_descriptor eq 'handback') {
 6080:                             $readonly_files{$file_name} = 'handback';
 6081:                         } else {
 6082:                             if (!exists($readonly_files{$file_name})) {
 6083:                                 $readonly_files{$file_name} = 'locked';
 6084:                             }
 6085:                         }
 6086:                     }
 6087:                 } 
 6088:             }
 6089:         } 
 6090:     }
 6091:     return %readonly_files;
 6092: }
 6093: # ------------------------------------------------------------ Unmark as Read Only
 6094: 
 6095: sub unmark_as_readonly {
 6096:     # unmarks $file_name (if $file_name is defined), or all files locked by $what 
 6097:     # for portfolio submissions, $what contains [$symb,$crsid] 
 6098:     my ($domain,$user,$what,$file_name,$group) = @_;
 6099:     $file_name = &declutter_portfile($file_name);
 6100:     my $symb_crs = $what;
 6101:     if (ref($what)) { $symb_crs=join('',@$what); }
 6102:     my %current_permissions = &dump('file_permissions',$domain,$user,$group);
 6103:     my ($tmp)=keys(%current_permissions);
 6104:     if ($tmp=~/^error:/) { undef(%current_permissions); }
 6105:     my @readonly_files = &get_marked_as_readonly($domain,$user,$what,$group);
 6106:     foreach my $file (@readonly_files) {
 6107: 	my $clean_file = &declutter_portfile($file);
 6108: 	if (defined($file_name) && ($file_name ne $clean_file)) { next; }
 6109: 	my $current_locks = $current_permissions{$file};
 6110:         my @new_locks;
 6111:         my @del_keys;
 6112:         if (ref($current_locks) eq "ARRAY"){
 6113:             foreach my $locker (@{$current_locks}) {
 6114:                 my $compare=$locker;
 6115:                 if (ref($locker) eq 'ARRAY') {
 6116:                     $compare=join('',@{$locker});
 6117:                     if ($compare ne $symb_crs) {
 6118:                         push(@new_locks, $locker);
 6119:                     }
 6120:                 }
 6121:             }
 6122:             if (scalar(@new_locks) > 0) {
 6123:                 $current_permissions{$file} = \@new_locks;
 6124:             } else {
 6125:                 push(@del_keys, $file);
 6126:                 &del('file_permissions',\@del_keys, $domain, $user);
 6127:                 delete($current_permissions{$file});
 6128:             }
 6129:         }
 6130:     }
 6131:     &put('file_permissions',\%current_permissions,$domain,$user);
 6132:     return;
 6133: }
 6134: 
 6135: # ------------------------------------------------------------ Directory lister
 6136: 
 6137: sub dirlist {
 6138:     my ($uri,$userdomain,$username,$alternateDirectoryRoot)=@_;
 6139: 
 6140:     $uri=~s/^\///;
 6141:     $uri=~s/\/$//;
 6142:     my ($udom, $uname);
 6143:     (undef,$udom,$uname)=split(/\//,$uri);
 6144:     if(defined($userdomain)) {
 6145:         $udom = $userdomain;
 6146:     }
 6147:     if(defined($username)) {
 6148:         $uname = $username;
 6149:     }
 6150: 
 6151:     my $dirRoot = $perlvar{'lonDocRoot'};
 6152:     if(defined($alternateDirectoryRoot)) {
 6153:         $dirRoot = $alternateDirectoryRoot;
 6154:         $dirRoot =~ s/\/$//;
 6155:     }
 6156: 
 6157:     if($udom) {
 6158:         if($uname) {
 6159:             my $listing = &reply('ls2:'.$dirRoot.'/'.$uri,
 6160: 				 &homeserver($uname,$udom));
 6161:             my @listing_results;
 6162:             if ($listing eq 'unknown_cmd') {
 6163:                 $listing = &reply('ls:'.$dirRoot.'/'.$uri,
 6164: 				  &homeserver($uname,$udom));
 6165:                 @listing_results = split(/:/,$listing);
 6166:             } else {
 6167:                 @listing_results = map { &unescape($_); } split(/:/,$listing);
 6168:             }
 6169:             return @listing_results;
 6170:         } elsif(!defined($alternateDirectoryRoot)) {
 6171:             my %allusers;
 6172: 	    my %servers = &get_servers($udom,'library');
 6173: 	    foreach my $tryserver (keys(%servers)) {
 6174: 		my $listing = &reply('ls2:'.$perlvar{'lonDocRoot'}.'/res/'.
 6175: 				     $udom, $tryserver);
 6176: 		my @listing_results;
 6177: 		if ($listing eq 'unknown_cmd') {
 6178: 		    $listing = &reply('ls:'.$perlvar{'lonDocRoot'}.'/res/'.
 6179: 				      $udom, $tryserver);
 6180: 		    @listing_results = split(/:/,$listing);
 6181: 		} else {
 6182: 		    @listing_results =
 6183: 			map { &unescape($_); } split(/:/,$listing);
 6184: 		}
 6185: 		if ($listing_results[0] ne 'no_such_dir' && 
 6186: 		    $listing_results[0] ne 'empty'       &&
 6187: 		    $listing_results[0] ne 'con_lost') {
 6188: 		    foreach my $line (@listing_results) {
 6189: 			my ($entry) = split(/&/,$line,2);
 6190: 			$allusers{$entry} = 1;
 6191: 		    }
 6192: 		}
 6193:             }
 6194:             my $alluserstr='';
 6195:             foreach my $user (sort(keys(%allusers))) {
 6196:                 $alluserstr.=$user.'&user:';
 6197:             }
 6198:             $alluserstr=~s/:$//;
 6199:             return split(/:/,$alluserstr);
 6200:         } else {
 6201:             return ('missing user name');
 6202:         }
 6203:     } elsif(!defined($alternateDirectoryRoot)) {
 6204:         my @all_domains = sort(&all_domains());
 6205:          foreach my $domain (@all_domains) {
 6206:              $domain = $perlvar{'lonDocRoot'}.'/res/'.$domain.'/&domain';
 6207:          }
 6208:          return @all_domains;
 6209:      } else {
 6210:         return ('missing domain');
 6211:     }
 6212: }
 6213: 
 6214: # --------------------------------------------- GetFileTimestamp
 6215: # This function utilizes dirlist and returns the date stamp for
 6216: # when it was last modified.  It will also return an error of -1
 6217: # if an error occurs
 6218: 
 6219: ##
 6220: ## FIXME: This subroutine assumes its caller knows something about the
 6221: ## directory structure of the home server for the student ($root).
 6222: ## Not a good assumption to make.  Since this is for looking up files
 6223: ## in user directories, the full path should be constructed by lond, not
 6224: ## whatever machine we request data from.
 6225: ##
 6226: sub GetFileTimestamp {
 6227:     my ($studentDomain,$studentName,$filename,$root)=@_;
 6228:     $studentDomain = &LONCAPA::clean_domain($studentDomain);
 6229:     $studentName   = &LONCAPA::clean_username($studentName);
 6230:     my $subdir=$studentName.'__';
 6231:     $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
 6232:     my $proname="$studentDomain/$subdir/$studentName";
 6233:     $proname .= '/'.$filename;
 6234:     my ($fileStat) = &Apache::lonnet::dirlist($proname, $studentDomain, 
 6235:                                               $studentName, $root);
 6236:     my @stats = split('&', $fileStat);
 6237:     if($stats[0] ne 'empty' && $stats[0] ne 'no_such_dir') {
 6238:         # @stats contains first the filename, then the stat output
 6239:         return $stats[10]; # so this is 10 instead of 9.
 6240:     } else {
 6241:         return -1;
 6242:     }
 6243: }
 6244: 
 6245: sub stat_file {
 6246:     my ($uri) = @_;
 6247:     $uri = &clutter_with_no_wrapper($uri);
 6248: 
 6249:     my ($udom,$uname,$file,$dir);
 6250:     if ($uri =~ m-^/(uploaded|editupload)/-) {
 6251: 	($udom,$uname,$file) =
 6252: 	    ($uri =~ m-/(?:uploaded|editupload)/?($match_domain)/?($match_name)/?(.*)-);
 6253: 	$file = 'userfiles/'.$file;
 6254: 	$dir = &propath($udom,$uname);
 6255:     }
 6256:     if ($uri =~ m-^/res/-) {
 6257: 	($udom,$uname) = 
 6258: 	    ($uri =~ m-/(?:res)/?($match_domain)/?($match_username)/-);
 6259: 	$file = $uri;
 6260:     }
 6261: 
 6262:     if (!$udom || !$uname || !$file) {
 6263: 	# unable to handle the uri
 6264: 	return ();
 6265:     }
 6266: 
 6267:     my ($result) = &dirlist($file,$udom,$uname,$dir);
 6268:     my @stats = split('&', $result);
 6269:     
 6270:     if($stats[0] ne 'empty' && $stats[0] ne 'no_such_dir') {
 6271: 	shift(@stats); #filename is first
 6272: 	return @stats;
 6273:     }
 6274:     return ();
 6275: }
 6276: 
 6277: # -------------------------------------------------------- Value of a Condition
 6278: 
 6279: # gets the value of a specific preevaluated condition
 6280: #    stored in the string  $env{user.state.<cid>}
 6281: # or looks up a condition reference in the bighash and if if hasn't
 6282: # already been evaluated recurses into docondval to get the value of
 6283: # the condition, then memoizing it to 
 6284: #   $env{user.state.<cid>.<condition>}
 6285: sub directcondval {
 6286:     my $number=shift;
 6287:     if (!defined($env{'user.state.'.$env{'request.course.id'}})) {
 6288: 	&Apache::lonuserstate::evalstate();
 6289:     }
 6290:     if (exists($env{'user.state.'.$env{'request.course.id'}.".$number"})) {
 6291: 	return $env{'user.state.'.$env{'request.course.id'}.".$number"};
 6292:     } elsif ($number =~ /^_/) {
 6293: 	my $sub_condition;
 6294: 	if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
 6295: 		&GDBM_READER(),0640)) {
 6296: 	    $sub_condition=$bighash{'conditions'.$number};
 6297: 	    untie(%bighash);
 6298: 	}
 6299: 	my $value = &docondval($sub_condition);
 6300: 	&appenv('user.state.'.$env{'request.course.id'}.".$number" => $value);
 6301: 	return $value;
 6302:     }
 6303:     if ($env{'user.state.'.$env{'request.course.id'}}) {
 6304:        return substr($env{'user.state.'.$env{'request.course.id'}},$number,1);
 6305:     } else {
 6306:        return 2;
 6307:     }
 6308: }
 6309: 
 6310: # get the collection of conditions for this resource
 6311: sub condval {
 6312:     my $condidx=shift;
 6313:     my $allpathcond='';
 6314:     foreach my $cond (split(/\|/,$condidx)) {
 6315: 	if (defined($env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond})) {
 6316: 	    $allpathcond.=
 6317: 		'('.$env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond}.')|';
 6318: 	}
 6319:     }
 6320:     $allpathcond=~s/\|$//;
 6321:     return &docondval($allpathcond);
 6322: }
 6323: 
 6324: #evaluates an expression of conditions
 6325: sub docondval {
 6326:     my ($allpathcond) = @_;
 6327:     my $result=0;
 6328:     if ($env{'request.course.id'}
 6329: 	&& defined($allpathcond)) {
 6330: 	my $operand='|';
 6331: 	my @stack;
 6332: 	foreach my $chunk ($allpathcond=~/(\d+|_\d+\.\d+|\(|\)|\&|\|)/g) {
 6333: 	    if ($chunk eq '(') {
 6334: 		push @stack,($operand,$result);
 6335: 	    } elsif ($chunk eq ')') {
 6336: 		my $before=pop @stack;
 6337: 		if (pop @stack eq '&') {
 6338: 		    $result=$result>$before?$before:$result;
 6339: 		} else {
 6340: 		    $result=$result>$before?$result:$before;
 6341: 		}
 6342: 	    } elsif (($chunk eq '&') || ($chunk eq '|')) {
 6343: 		$operand=$chunk;
 6344: 	    } else {
 6345: 		my $new=directcondval($chunk);
 6346: 		if ($operand eq '&') {
 6347: 		    $result=$result>$new?$new:$result;
 6348: 		} else {
 6349: 		    $result=$result>$new?$result:$new;
 6350: 		}
 6351: 	    }
 6352: 	}
 6353:     }
 6354:     return $result;
 6355: }
 6356: 
 6357: # ---------------------------------------------------- Devalidate courseresdata
 6358: 
 6359: sub devalidatecourseresdata {
 6360:     my ($coursenum,$coursedomain)=@_;
 6361:     my $hashid=$coursenum.':'.$coursedomain;
 6362:     &devalidate_cache_new('courseres',$hashid);
 6363: }
 6364: 
 6365: 
 6366: # --------------------------------------------------- Course Resourcedata Query
 6367: #
 6368: #  Parameters:
 6369: #      $coursenum    - Number of the course.
 6370: #      $coursedomain - Domain at which the course was created.
 6371: #  Returns:
 6372: #     A hash of the course parameters along (I think) with timestamps
 6373: #     and version info.
 6374: 
 6375: sub get_courseresdata {
 6376:     my ($coursenum,$coursedomain)=@_;
 6377:     my $coursehom=&homeserver($coursenum,$coursedomain);
 6378:     my $hashid=$coursenum.':'.$coursedomain;
 6379:     my ($result,$cached)=&is_cached_new('courseres',$hashid);
 6380:     my %dumpreply;
 6381:     unless (defined($cached)) {
 6382: 	%dumpreply=&dump('resourcedata',$coursedomain,$coursenum);
 6383: 	$result=\%dumpreply;
 6384: 	my ($tmp) = keys(%dumpreply);
 6385: 	if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
 6386: 	    &do_cache_new('courseres',$hashid,$result,600);
 6387: 	} elsif ($tmp =~ /^(con_lost|no_such_host)/) {
 6388: 	    return $tmp;
 6389: 	} elsif ($tmp =~ /^(error)/) {
 6390: 	    $result=undef;
 6391: 	    &do_cache_new('courseres',$hashid,$result,600);
 6392: 	}
 6393:     }
 6394:     return $result;
 6395: }
 6396: 
 6397: sub devalidateuserresdata {
 6398:     my ($uname,$udom)=@_;
 6399:     my $hashid="$udom:$uname";
 6400:     &devalidate_cache_new('userres',$hashid);
 6401: }
 6402: 
 6403: sub get_userresdata {
 6404:     my ($uname,$udom)=@_;
 6405:     #most student don\'t have any data set, check if there is some data
 6406:     if (&EXT_cache_status($udom,$uname)) { return undef; }
 6407: 
 6408:     my $hashid="$udom:$uname";
 6409:     my ($result,$cached)=&is_cached_new('userres',$hashid);
 6410:     if (!defined($cached)) {
 6411: 	my %resourcedata=&dump('resourcedata',$udom,$uname);
 6412: 	$result=\%resourcedata;
 6413: 	&do_cache_new('userres',$hashid,$result,600);
 6414:     }
 6415:     my ($tmp)=keys(%$result);
 6416:     if (($tmp!~/^error\:/) && ($tmp!~/^con_lost/)) {
 6417: 	return $result;
 6418:     }
 6419:     #error 2 occurs when the .db doesn't exist
 6420:     if ($tmp!~/error: 2 /) {
 6421: 	&logthis("<font color=\"blue\">WARNING:".
 6422: 		 " Trying to get resource data for ".
 6423: 		 $uname." at ".$udom.": ".
 6424: 		 $tmp."</font>");
 6425:     } elsif ($tmp=~/error: 2 /) {
 6426: 	#&EXT_cache_set($udom,$uname);
 6427: 	&do_cache_new('userres',$hashid,undef,600);
 6428: 	undef($tmp); # not really an error so don't send it back
 6429:     }
 6430:     return $tmp;
 6431: }
 6432: #----------------------------------------------- resdata - return resource data
 6433: #  Purpose:
 6434: #    Return resource data for either users or for a course.
 6435: #  Parameters:
 6436: #     $name      - Course/user name.
 6437: #     $domain    - Name of the domain the user/course is registered on.
 6438: #     $type      - Type of thing $name is (must be 'course' or 'user'
 6439: #     @which     - Array of names of resources desired.
 6440: #  Returns:
 6441: #     The value of the first reasource in @which that is found in the
 6442: #     resource hash.
 6443: #  Exceptional Conditions:
 6444: #     If the $type passed in is not valid (not the string 'course' or 
 6445: #     'user', an undefined  reference is returned.
 6446: #     If none of the resources are found, an undef is returned
 6447: sub resdata {
 6448:     my ($name,$domain,$type,@which)=@_;
 6449:     my $result;
 6450:     if ($type eq 'course') {
 6451: 	$result=&get_courseresdata($name,$domain);
 6452:     } elsif ($type eq 'user') {
 6453: 	$result=&get_userresdata($name,$domain);
 6454:     }
 6455:     if (!ref($result)) { return $result; }    
 6456:     foreach my $item (@which) {
 6457: 	if (defined($result->{$item->[0]})) {
 6458: 	    return [$result->{$item->[0]},$item->[1]];
 6459: 	}
 6460:     }
 6461:     return undef;
 6462: }
 6463: 
 6464: #
 6465: # EXT resource caching routines
 6466: #
 6467: 
 6468: sub clear_EXT_cache_status {
 6469:     &delenv('cache.EXT.');
 6470: }
 6471: 
 6472: sub EXT_cache_status {
 6473:     my ($target_domain,$target_user) = @_;
 6474:     my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
 6475:     if (exists($env{$cachename}) && ($env{$cachename}+600) > time) {
 6476:         # We know already the user has no data
 6477:         return 1;
 6478:     } else {
 6479:         return 0;
 6480:     }
 6481: }
 6482: 
 6483: sub EXT_cache_set {
 6484:     my ($target_domain,$target_user) = @_;
 6485:     my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
 6486:     #&appenv($cachename => time);
 6487: }
 6488: 
 6489: # --------------------------------------------------------- Value of a Variable
 6490: sub EXT {
 6491: 
 6492:     my ($varname,$symbparm,$udom,$uname,$usection,$recurse)=@_;
 6493:     unless ($varname) { return ''; }
 6494:     #get real user name/domain, courseid and symb
 6495:     my $courseid;
 6496:     my $publicuser;
 6497:     if ($symbparm) {
 6498: 	$symbparm=&get_symb_from_alias($symbparm);
 6499:     }
 6500:     if (!($uname && $udom)) {
 6501:       (my $cursymb,$courseid,$udom,$uname,$publicuser)= &whichuser($symbparm);
 6502:       if (!$symbparm) {	$symbparm=$cursymb; }
 6503:     } else {
 6504: 	$courseid=$env{'request.course.id'};
 6505:     }
 6506:     my ($realm,$space,$qualifier,@therest)=split(/\./,$varname);
 6507:     my $rest;
 6508:     if (defined($therest[0])) {
 6509:        $rest=join('.',@therest);
 6510:     } else {
 6511:        $rest='';
 6512:     }
 6513: 
 6514:     my $qualifierrest=$qualifier;
 6515:     if ($rest) { $qualifierrest.='.'.$rest; }
 6516:     my $spacequalifierrest=$space;
 6517:     if ($qualifierrest) { $spacequalifierrest.='.'.$qualifierrest; }
 6518:     if ($realm eq 'user') {
 6519: # --------------------------------------------------------------- user.resource
 6520: 	if ($space eq 'resource') {
 6521: 	    if ( (defined($Apache::lonhomework::parsing_a_problem)
 6522: 		  || defined($Apache::lonhomework::parsing_a_task))
 6523: 		 &&
 6524: 		 ($symbparm eq &symbread()) ) {	
 6525: 		# if we are in the middle of processing the resource the
 6526: 		# get the value we are planning on committing
 6527:                 if (defined($Apache::lonhomework::results{$qualifierrest})) {
 6528:                     return $Apache::lonhomework::results{$qualifierrest};
 6529:                 } else {
 6530:                     return $Apache::lonhomework::history{$qualifierrest};
 6531:                 }
 6532: 	    } else {
 6533: 		my %restored;
 6534: 		if ($publicuser || $env{'request.state'} eq 'construct') {
 6535: 		    %restored=&tmprestore($symbparm,$courseid,$udom,$uname);
 6536: 		} else {
 6537: 		    %restored=&restore($symbparm,$courseid,$udom,$uname);
 6538: 		}
 6539: 		return $restored{$qualifierrest};
 6540: 	    }
 6541: # ----------------------------------------------------------------- user.access
 6542:         } elsif ($space eq 'access') {
 6543: 	    # FIXME - not supporting calls for a specific user
 6544:             return &allowed($qualifier,$rest);
 6545: # ------------------------------------------ user.preferences, user.environment
 6546:         } elsif (($space eq 'preferences') || ($space eq 'environment')) {
 6547: 	    if (($uname eq $env{'user.name'}) &&
 6548: 		($udom eq $env{'user.domain'})) {
 6549: 		return $env{join('.',('environment',$qualifierrest))};
 6550: 	    } else {
 6551: 		my %returnhash;
 6552: 		if (!$publicuser) {
 6553: 		    %returnhash=&userenvironment($udom,$uname,
 6554: 						 $qualifierrest);
 6555: 		}
 6556: 		return $returnhash{$qualifierrest};
 6557: 	    }
 6558: # ----------------------------------------------------------------- user.course
 6559:         } elsif ($space eq 'course') {
 6560: 	    # FIXME - not supporting calls for a specific user
 6561:             return $env{join('.',('request.course',$qualifier))};
 6562: # ------------------------------------------------------------------- user.role
 6563:         } elsif ($space eq 'role') {
 6564: 	    # FIXME - not supporting calls for a specific user
 6565:             my ($role,$where)=split(/\./,$env{'request.role'});
 6566:             if ($qualifier eq 'value') {
 6567: 		return $role;
 6568:             } elsif ($qualifier eq 'extent') {
 6569:                 return $where;
 6570:             }
 6571: # ----------------------------------------------------------------- user.domain
 6572:         } elsif ($space eq 'domain') {
 6573:             return $udom;
 6574: # ------------------------------------------------------------------- user.name
 6575:         } elsif ($space eq 'name') {
 6576:             return $uname;
 6577: # ---------------------------------------------------- Any other user namespace
 6578:         } else {
 6579: 	    my %reply;
 6580: 	    if (!$publicuser) {
 6581: 		%reply=&get($space,[$qualifierrest],$udom,$uname);
 6582: 	    }
 6583: 	    return $reply{$qualifierrest};
 6584:         }
 6585:     } elsif ($realm eq 'query') {
 6586: # ---------------------------------------------- pull stuff out of query string
 6587:         &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
 6588: 						[$spacequalifierrest]);
 6589: 	return $env{'form.'.$spacequalifierrest}; 
 6590:    } elsif ($realm eq 'request') {
 6591: # ------------------------------------------------------------- request.browser
 6592:         if ($space eq 'browser') {
 6593: 	    if ($qualifier eq 'textremote') {
 6594: 		if (&Apache::lonlocal::mt('textual_remote_display') eq 'on') {
 6595: 		    return 1;
 6596: 		} else {
 6597: 		    return 0;
 6598: 		}
 6599: 	    } else {
 6600: 		return $env{'browser.'.$qualifier};
 6601: 	    }
 6602: # ------------------------------------------------------------ request.filename
 6603:         } else {
 6604:             return $env{'request.'.$spacequalifierrest};
 6605:         }
 6606:     } elsif ($realm eq 'course') {
 6607: # ---------------------------------------------------------- course.description
 6608:         return $env{'course.'.$courseid.'.'.$spacequalifierrest};
 6609:     } elsif ($realm eq 'resource') {
 6610: 
 6611: 	if (defined($courseid) && $courseid eq $env{'request.course.id'}) {
 6612: 	    if (!$symbparm) { $symbparm=&symbread(); }
 6613: 	}
 6614: 
 6615: 	if ($space eq 'title') {
 6616: 	    if (!$symbparm) { $symbparm = $env{'request.filename'}; }
 6617: 	    return &gettitle($symbparm);
 6618: 	}
 6619: 	
 6620: 	if ($space eq 'map') {
 6621: 	    my ($map) = &decode_symb($symbparm);
 6622: 	    return &symbread($map);
 6623: 	}
 6624: 	if ($space eq 'filename') {
 6625: 	    if ($symbparm) {
 6626: 		return &clutter((&decode_symb($symbparm))[2]);
 6627: 	    }
 6628: 	    return &hreflocation('',$env{'request.filename'});
 6629: 	}
 6630: 
 6631: 	my ($section, $group, @groups);
 6632: 	my ($courselevelm,$courselevel);
 6633: 	if ($symbparm && defined($courseid) && 
 6634: 	    $courseid eq $env{'request.course.id'}) {
 6635: 
 6636: 	    #print '<br>'.$space.' - '.$qualifier.' - '.$spacequalifierrest;
 6637: 
 6638: # ----------------------------------------------------- Cascading lookup scheme
 6639: 	    my $symbp=$symbparm;
 6640: 	    my $mapp=&deversion((&decode_symb($symbp))[0]);
 6641: 
 6642: 	    my $symbparm=$symbp.'.'.$spacequalifierrest;
 6643: 	    my $mapparm=$mapp.'___(all).'.$spacequalifierrest;
 6644: 
 6645: 	    if (($env{'user.name'} eq $uname) &&
 6646: 		($env{'user.domain'} eq $udom)) {
 6647: 		$section=$env{'request.course.sec'};
 6648:                 @groups = split(/:/,$env{'request.course.groups'});  
 6649:                 @groups=&sort_course_groups($courseid,@groups); 
 6650: 	    } else {
 6651: 		if (! defined($usection)) {
 6652: 		    $section=&getsection($udom,$uname,$courseid);
 6653: 		} else {
 6654: 		    $section = $usection;
 6655: 		}
 6656:                 @groups = &get_users_groups($udom,$uname,$courseid);
 6657: 	    }
 6658: 
 6659: 	    my $seclevel=$courseid.'.['.$section.'].'.$spacequalifierrest;
 6660: 	    my $seclevelr=$courseid.'.['.$section.'].'.$symbparm;
 6661: 	    my $seclevelm=$courseid.'.['.$section.'].'.$mapparm;
 6662: 
 6663: 	    $courselevel=$courseid.'.'.$spacequalifierrest;
 6664: 	    my $courselevelr=$courseid.'.'.$symbparm;
 6665: 	    $courselevelm=$courseid.'.'.$mapparm;
 6666: 
 6667: # ----------------------------------------------------------- first, check user
 6668: 
 6669: 	    my $userreply=&resdata($uname,$udom,'user',
 6670: 				       ([$courselevelr,'resource'],
 6671: 					[$courselevelm,'map'     ],
 6672: 					[$courselevel, 'course'  ]));
 6673: 	    if (defined($userreply)) { return &get_reply($userreply); }
 6674: 
 6675: # ------------------------------------------------ second, check some of course
 6676:             my $coursereply;
 6677:             if (@groups > 0) {
 6678:                 $coursereply = &check_group_parms($courseid,\@groups,$symbparm,
 6679:                                        $mapparm,$spacequalifierrest);
 6680:                 if (defined($coursereply)) { return &get_reply($coursereply); }
 6681:             }
 6682: 
 6683: 	    $coursereply=&resdata($env{'course.'.$courseid.'.num'},
 6684: 				  $env{'course.'.$courseid.'.domain'},
 6685: 				  'course',
 6686: 				  ([$seclevelr,   'resource'],
 6687: 				   [$seclevelm,   'map'     ],
 6688: 				   [$seclevel,    'course'  ],
 6689: 				   [$courselevelr,'resource']));
 6690: 	    if (defined($coursereply)) { return &get_reply($coursereply); }
 6691: 
 6692: # ------------------------------------------------------ third, check map parms
 6693: 	    my %parmhash=();
 6694: 	    my $thisparm='';
 6695: 	    if (tie(%parmhash,'GDBM_File',
 6696: 		    $env{'request.course.fn'}.'_parms.db',
 6697: 		    &GDBM_READER(),0640)) {
 6698: 		$thisparm=$parmhash{$symbparm};
 6699: 		untie(%parmhash);
 6700: 	    }
 6701: 	    if ($thisparm) { return &get_reply([$thisparm,'resource']); }
 6702: 	}
 6703: # ------------------------------------------ fourth, look in resource metadata
 6704: 
 6705: 	$spacequalifierrest=~s/\./\_/;
 6706: 	my $filename;
 6707: 	if (!$symbparm) { $symbparm=&symbread(); }
 6708: 	if ($symbparm) {
 6709: 	    $filename=(&decode_symb($symbparm))[2];
 6710: 	} else {
 6711: 	    $filename=$env{'request.filename'};
 6712: 	}
 6713: 	my $metadata=&metadata($filename,$spacequalifierrest);
 6714: 	if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
 6715: 	$metadata=&metadata($filename,'parameter_'.$spacequalifierrest);
 6716: 	if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
 6717: 
 6718: # ---------------------------------------------- fourth, look in rest of course
 6719: 	if ($symbparm && defined($courseid) && 
 6720: 	    $courseid eq $env{'request.course.id'}) {
 6721: 	    my $coursereply=&resdata($env{'course.'.$courseid.'.num'},
 6722: 				     $env{'course.'.$courseid.'.domain'},
 6723: 				     'course',
 6724: 				     ([$courselevelm,'map'   ],
 6725: 				      [$courselevel, 'course']));
 6726: 	    if (defined($coursereply)) { return &get_reply($coursereply); }
 6727: 	}
 6728: # ------------------------------------------------------------------ Cascade up
 6729: 	unless ($space eq '0') {
 6730: 	    my @parts=split(/_/,$space);
 6731: 	    my $id=pop(@parts);
 6732: 	    my $part=join('_',@parts);
 6733: 	    if ($part eq '') { $part='0'; }
 6734: 	    my @partgeneral=&EXT('resource.'.$part.'.'.$qualifierrest,
 6735: 				 $symbparm,$udom,$uname,$section,1);
 6736: 	    if (defined($partgeneral[0])) { return &get_reply(\@partgeneral); }
 6737: 	}
 6738: 	if ($recurse) { return undef; }
 6739: 	my $pack_def=&packages_tab_default($filename,$varname);
 6740: 	if (defined($pack_def)) { return &get_reply([$pack_def,'resource']); }
 6741: # ---------------------------------------------------- Any other user namespace
 6742:     } elsif ($realm eq 'environment') {
 6743: # ----------------------------------------------------------------- environment
 6744: 	if (($uname eq $env{'user.name'})&&($udom eq $env{'user.domain'})) {
 6745: 	    return $env{'environment.'.$spacequalifierrest};
 6746: 	} else {
 6747: 	    if ($uname eq 'anonymous' && $udom eq '') {
 6748: 		return '';
 6749: 	    }
 6750: 	    my %returnhash=&userenvironment($udom,$uname,
 6751: 					    $spacequalifierrest);
 6752: 	    return $returnhash{$spacequalifierrest};
 6753: 	}
 6754:     } elsif ($realm eq 'system') {
 6755: # ----------------------------------------------------------------- system.time
 6756: 	if ($space eq 'time') {
 6757: 	    return time;
 6758:         }
 6759:     } elsif ($realm eq 'server') {
 6760: # ----------------------------------------------------------------- system.time
 6761: 	if ($space eq 'name') {
 6762: 	    return $ENV{'SERVER_NAME'};
 6763:         }
 6764:     }
 6765:     return '';
 6766: }
 6767: 
 6768: sub get_reply {
 6769:     my ($reply_value) = @_;
 6770:     if (ref($reply_value) eq 'ARRAY') {
 6771:         if (wantarray) {
 6772: 	    return @$reply_value;
 6773:         }
 6774:         return $reply_value->[0];
 6775:     } else {
 6776:         return $reply_value;
 6777:     }
 6778: }
 6779: 
 6780: sub check_group_parms {
 6781:     my ($courseid,$groups,$symbparm,$mapparm,$what) = @_;
 6782:     my @groupitems = ();
 6783:     my $resultitem;
 6784:     my @levels = ([$symbparm,'resource'],[$mapparm,'map'],[$what,'course']);
 6785:     foreach my $group (@{$groups}) {
 6786:         foreach my $level (@levels) {
 6787:              my $item = $courseid.'.['.$group.'].'.$level->[0];
 6788:              push(@groupitems,[$item,$level->[1]]);
 6789:         }
 6790:     }
 6791:     my $coursereply = &resdata($env{'course.'.$courseid.'.num'},
 6792:                             $env{'course.'.$courseid.'.domain'},
 6793:                                      'course',@groupitems);
 6794:     return $coursereply;
 6795: }
 6796: 
 6797: sub sort_course_groups { # Sort groups based on defined rankings. Default is sort().
 6798:     my ($courseid,@groups) = @_;
 6799:     @groups = sort(@groups);
 6800:     return @groups;
 6801: }
 6802: 
 6803: sub packages_tab_default {
 6804:     my ($uri,$varname)=@_;
 6805:     my (undef,$part,$name)=split(/\./,$varname);
 6806: 
 6807:     my (@extension,@specifics,$do_default);
 6808:     foreach my $package (split(/,/,&metadata($uri,'packages'))) {
 6809: 	my ($pack_type,$pack_part)=split(/_/,$package,2);
 6810: 	if ($pack_type eq 'default') {
 6811: 	    $do_default=1;
 6812: 	} elsif ($pack_type eq 'extension') {
 6813: 	    push(@extension,[$package,$pack_type,$pack_part]);
 6814: 	} elsif ($pack_part eq $part || $pack_type eq 'part') {
 6815: 	    # only look at packages defaults for packages that this id is
 6816: 	    push(@specifics,[$package,$pack_type,$pack_part]);
 6817: 	}
 6818:     }
 6819:     # first look for a package that matches the requested part id
 6820:     foreach my $package (@specifics) {
 6821: 	my (undef,$pack_type,$pack_part)=@{$package};
 6822: 	next if ($pack_part ne $part);
 6823: 	if (defined($packagetab{"$pack_type&$name&default"})) {
 6824: 	    return $packagetab{"$pack_type&$name&default"};
 6825: 	}
 6826:     }
 6827:     # look for any possible matching non extension_ package
 6828:     foreach my $package (@specifics) {
 6829: 	my (undef,$pack_type,$pack_part)=@{$package};
 6830: 	if (defined($packagetab{"$pack_type&$name&default"})) {
 6831: 	    return $packagetab{"$pack_type&$name&default"};
 6832: 	}
 6833: 	if ($pack_type eq 'part') { $pack_part='0'; }
 6834: 	if (defined($packagetab{$pack_type."_".$pack_part."&$name&default"})) {
 6835: 	    return $packagetab{$pack_type."_".$pack_part."&$name&default"};
 6836: 	}
 6837:     }
 6838:     # look for any posible extension_ match
 6839:     foreach my $package (@extension) {
 6840: 	my ($package,$pack_type)=@{$package};
 6841: 	if (defined($packagetab{"$pack_type&$name&default"})) {
 6842: 	    return $packagetab{"$pack_type&$name&default"};
 6843: 	}
 6844: 	if (defined($packagetab{$package."&$name&default"})) {
 6845: 	    return $packagetab{$package."&$name&default"};
 6846: 	}
 6847:     }
 6848:     # look for a global default setting
 6849:     if ($do_default && defined($packagetab{"default&$name&default"})) {
 6850: 	return $packagetab{"default&$name&default"};
 6851:     }
 6852:     return undef;
 6853: }
 6854: 
 6855: sub add_prefix_and_part {
 6856:     my ($prefix,$part)=@_;
 6857:     my $keyroot;
 6858:     if (defined($prefix) && $prefix !~ /^__/) {
 6859: 	# prefix that has a part already
 6860: 	$keyroot=$prefix;
 6861:     } elsif (defined($prefix)) {
 6862: 	# prefix that is missing a part
 6863: 	if (defined($part)) { $keyroot='_'.$part.substr($prefix,1); }
 6864:     } else {
 6865: 	# no prefix at all
 6866: 	if (defined($part)) { $keyroot='_'.$part; }
 6867:     }
 6868:     return $keyroot;
 6869: }
 6870: 
 6871: # ---------------------------------------------------------------- Get metadata
 6872: 
 6873: my %metaentry;
 6874: sub metadata {
 6875:     my ($uri,$what,$liburi,$prefix,$depthcount)=@_;
 6876:     $uri=&declutter($uri);
 6877:     # if it is a non metadata possible uri return quickly
 6878:     if (($uri eq '') || 
 6879: 	(($uri =~ m|^/*adm/|) && 
 6880: 	     ($uri !~ m|^adm/includes|) && ($uri !~ m|/bulletinboard$|)) ||
 6881:         ($uri =~ m|/$|) || ($uri =~ m|/.meta$|) ) {
 6882: 	return undef;
 6883:     }
 6884:     if (($uri =~ /^~/ || $uri =~ m{home/$match_username/public_html/}) 
 6885: 	&& &Apache::lonxml::get_state('target') =~ /^(|meta)$/) {
 6886: 	return undef;
 6887:     }
 6888:     my $filename=$uri;
 6889:     $uri=~s/\.meta$//;
 6890: #
 6891: # Is the metadata already cached?
 6892: # Look at timestamp of caching
 6893: # Everything is cached by the main uri, libraries are never directly cached
 6894: #
 6895:     if (!defined($liburi)) {
 6896: 	my ($result,$cached)=&is_cached_new('meta',$uri);
 6897: 	if (defined($cached)) { return $result->{':'.$what}; }
 6898:     }
 6899:     {
 6900: #
 6901: # Is this a recursive call for a library?
 6902: #
 6903: #	if (! exists($metacache{$uri})) {
 6904: #	    $metacache{$uri}={};
 6905: #	}
 6906: 	my $cachetime = 60*60;
 6907:         if ($liburi) {
 6908: 	    $liburi=&declutter($liburi);
 6909:             $filename=$liburi;
 6910:         } else {
 6911: 	    &devalidate_cache_new('meta',$uri);
 6912: 	    undef(%metaentry);
 6913: 	}
 6914:         my %metathesekeys=();
 6915:         unless ($filename=~/\.meta$/) { $filename.='.meta'; }
 6916: 	my $metastring;
 6917: 	if ($uri =~ /^~/ || $uri =~ m{home/$match_username/public_html/}) {
 6918: 	    my $which = &hreflocation('','/'.($liburi || $uri));
 6919: 	    $metastring = 
 6920: 		&Apache::lonnet::ssi_body($which,
 6921: 					  ('grade_target' => 'meta'));
 6922: 	    $cachetime = 1; # only want this cached in the child not long term
 6923: 	} elsif ($uri !~ m -^(editupload)/-) {
 6924: 	    my $file=&filelocation('',&clutter($filename));
 6925: 	    #push(@{$metaentry{$uri.'.file'}},$file);
 6926: 	    $metastring=&getfile($file);
 6927: 	}
 6928:         my $parser=HTML::LCParser->new(\$metastring);
 6929:         my $token;
 6930:         undef %metathesekeys;
 6931:         while ($token=$parser->get_token) {
 6932: 	    if ($token->[0] eq 'S') {
 6933: 		if (defined($token->[2]->{'package'})) {
 6934: #
 6935: # This is a package - get package info
 6936: #
 6937: 		    my $package=$token->[2]->{'package'};
 6938: 		    my $keyroot=&add_prefix_and_part($prefix,$token->[2]->{'part'});
 6939: 		    if (defined($token->[2]->{'id'})) { 
 6940: 			$keyroot.='_'.$token->[2]->{'id'}; 
 6941: 		    }
 6942: 		    if ($metaentry{':packages'}) {
 6943: 			$metaentry{':packages'}.=','.$package.$keyroot;
 6944: 		    } else {
 6945: 			$metaentry{':packages'}=$package.$keyroot;
 6946: 		    }
 6947: 		    foreach my $pack_entry (keys(%packagetab)) {
 6948: 			my $part=$keyroot;
 6949: 			$part=~s/^\_//;
 6950: 			if ($pack_entry=~/^\Q$package\E\&/ || 
 6951: 			    $pack_entry=~/^\Q$package\E_0\&/) {
 6952: 			    my ($pack,$name,$subp)=split(/\&/,$pack_entry);
 6953: 			    # ignore package.tab specified default values
 6954:                             # here &package_tab_default() will fetch those
 6955: 			    if ($subp eq 'default') { next; }
 6956: 			    my $value=$packagetab{$pack_entry};
 6957: 			    my $unikey;
 6958: 			    if ($pack =~ /_0$/) {
 6959: 				$unikey='parameter_0_'.$name;
 6960: 				$part=0;
 6961: 			    } else {
 6962: 				$unikey='parameter'.$keyroot.'_'.$name;
 6963: 			    }
 6964: 			    if ($subp eq 'display') {
 6965: 				$value.=' [Part: '.$part.']';
 6966: 			    }
 6967: 			    $metaentry{':'.$unikey.'.part'}=$part;
 6968: 			    $metathesekeys{$unikey}=1;
 6969: 			    unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
 6970: 				$metaentry{':'.$unikey.'.'.$subp}=$value;
 6971: 			    }
 6972: 			    if (defined($metaentry{':'.$unikey.'.default'})) {
 6973: 				$metaentry{':'.$unikey}=
 6974: 				    $metaentry{':'.$unikey.'.default'};
 6975: 			    }
 6976: 			}
 6977: 		    }
 6978: 		} else {
 6979: #
 6980: # This is not a package - some other kind of start tag
 6981: #
 6982: 		    my $entry=$token->[1];
 6983: 		    my $unikey;
 6984: 		    if ($entry eq 'import') {
 6985: 			$unikey='';
 6986: 		    } else {
 6987: 			$unikey=$entry;
 6988: 		    }
 6989: 		    $unikey.=&add_prefix_and_part($prefix,$token->[2]->{'part'});
 6990: 
 6991: 		    if (defined($token->[2]->{'id'})) { 
 6992: 			$unikey.='_'.$token->[2]->{'id'}; 
 6993: 		    }
 6994: 
 6995: 		    if ($entry eq 'import') {
 6996: #
 6997: # Importing a library here
 6998: #
 6999: 			if ($depthcount<20) {
 7000: 			    my $location=$parser->get_text('/import');
 7001: 			    my $dir=$filename;
 7002: 			    $dir=~s|[^/]*$||;
 7003: 			    $location=&filelocation($dir,$location);
 7004: 			    my $metadata = 
 7005: 				&metadata($uri,'keys', $location,$unikey,
 7006: 					  $depthcount+1);
 7007: 			    foreach my $meta (split(',',$metadata)) {
 7008: 				$metaentry{':'.$meta}=$metaentry{':'.$meta};
 7009: 				$metathesekeys{$meta}=1;
 7010: 			    }
 7011: 			}
 7012: 		    } else { 
 7013: 			
 7014: 			if (defined($token->[2]->{'name'})) { 
 7015: 			    $unikey.='_'.$token->[2]->{'name'}; 
 7016: 			}
 7017: 			$metathesekeys{$unikey}=1;
 7018: 			foreach my $param (@{$token->[3]}) {
 7019: 			    $metaentry{':'.$unikey.'.'.$param} =
 7020: 				$token->[2]->{$param};
 7021: 			}
 7022: 			my $internaltext=&HTML::Entities::decode($parser->get_text('/'.$entry));
 7023: 			my $default=$metaentry{':'.$unikey.'.default'};
 7024: 			if ( $internaltext =~ /^\s*$/ && $default !~ /^\s*$/) {
 7025: 		 # only ws inside the tag, and not in default, so use default
 7026: 		 # as value
 7027: 			    $metaentry{':'.$unikey}=$default;
 7028: 			} elsif ( $internaltext =~ /\S/ ) {
 7029: 		  # something interesting inside the tag
 7030: 			    $metaentry{':'.$unikey}=$internaltext;
 7031: 			} else {
 7032: 		  # no interesting values, don't set a default
 7033: 			}
 7034: # end of not-a-package not-a-library import
 7035: 		    }
 7036: # end of not-a-package start tag
 7037: 		}
 7038: # the next is the end of "start tag"
 7039: 	    }
 7040: 	}
 7041: 	my ($extension) = ($uri =~ /\.(\w+)$/);
 7042: 	$extension = lc($extension);
 7043: 	if ($extension eq 'htm') { $extension='html'; }
 7044: 
 7045: 	foreach my $key (keys(%packagetab)) {
 7046: 	    #no specific packages #how's our extension
 7047: 	    if ($key!~/^extension_\Q$extension\E&/) { next; }
 7048: 	    &metadata_create_package_def($uri,$key,'extension_'.$extension,
 7049: 					 \%metathesekeys);
 7050: 	}
 7051: 
 7052: 	if (!exists($metaentry{':packages'})
 7053: 	    || $packagetab{"import_defaults&extension_$extension"}) {
 7054: 	    foreach my $key (keys(%packagetab)) {
 7055: 		#no specific packages well let's get default then
 7056: 		if ($key!~/^default&/) { next; }
 7057: 		&metadata_create_package_def($uri,$key,'default',
 7058: 					     \%metathesekeys);
 7059: 	    }
 7060: 	}
 7061: # are there custom rights to evaluate
 7062: 	if ($metaentry{':copyright'} eq 'custom') {
 7063: 
 7064:     #
 7065:     # Importing a rights file here
 7066:     #
 7067: 	    unless ($depthcount) {
 7068: 		my $location=$metaentry{':customdistributionfile'};
 7069: 		my $dir=$filename;
 7070: 		$dir=~s|[^/]*$||;
 7071: 		$location=&filelocation($dir,$location);
 7072: 		my $rights_metadata =
 7073: 		    &metadata($uri,'keys',$location,'_rights',
 7074: 			      $depthcount+1);
 7075: 		foreach my $rights (split(',',$rights_metadata)) {
 7076: 		    #$metaentry{':'.$rights}=$metacache{$uri}->{':'.$rights};
 7077: 		    $metathesekeys{$rights}=1;
 7078: 		}
 7079: 	    }
 7080: 	}
 7081: 	# uniqifiy package listing
 7082: 	my %seen;
 7083: 	my @uniq_packages =
 7084: 	    grep { ! $seen{$_} ++ } (split(',',$metaentry{':packages'}));
 7085: 	$metaentry{':packages'} = join(',',@uniq_packages);
 7086: 
 7087: 	$metaentry{':keys'} = join(',',keys(%metathesekeys));
 7088: 	&metadata_generate_part0(\%metathesekeys,\%metaentry,$uri);
 7089: 	$metaentry{':allpossiblekeys'}=join(',',keys %metathesekeys);
 7090: 	&do_cache_new('meta',$uri,\%metaentry,$cachetime);
 7091: # this is the end of "was not already recently cached
 7092:     }
 7093:     return $metaentry{':'.$what};
 7094: }
 7095: 
 7096: sub metadata_create_package_def {
 7097:     my ($uri,$key,$package,$metathesekeys)=@_;
 7098:     my ($pack,$name,$subp)=split(/\&/,$key);
 7099:     if ($subp eq 'default') { next; }
 7100:     
 7101:     if (defined($metaentry{':packages'})) {
 7102: 	$metaentry{':packages'}.=','.$package;
 7103:     } else {
 7104: 	$metaentry{':packages'}=$package;
 7105:     }
 7106:     my $value=$packagetab{$key};
 7107:     my $unikey;
 7108:     $unikey='parameter_0_'.$name;
 7109:     $metaentry{':'.$unikey.'.part'}=0;
 7110:     $$metathesekeys{$unikey}=1;
 7111:     unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
 7112: 	$metaentry{':'.$unikey.'.'.$subp}=$value;
 7113:     }
 7114:     if (defined($metaentry{':'.$unikey.'.default'})) {
 7115: 	$metaentry{':'.$unikey}=
 7116: 	    $metaentry{':'.$unikey.'.default'};
 7117:     }
 7118: }
 7119: 
 7120: sub metadata_generate_part0 {
 7121:     my ($metadata,$metacache,$uri) = @_;
 7122:     my %allnames;
 7123:     foreach my $metakey (keys(%$metadata)) {
 7124: 	if ($metakey=~/^parameter\_(.*)/) {
 7125: 	  my $part=$$metacache{':'.$metakey.'.part'};
 7126: 	  my $name=$$metacache{':'.$metakey.'.name'};
 7127: 	  if (! exists($$metadata{'parameter_0_'.$name.'.name'})) {
 7128: 	    $allnames{$name}=$part;
 7129: 	  }
 7130: 	}
 7131:     }
 7132:     foreach my $name (keys(%allnames)) {
 7133:       $$metadata{"parameter_0_$name"}=1;
 7134:       my $key=":parameter_0_$name";
 7135:       $$metacache{"$key.part"}='0';
 7136:       $$metacache{"$key.name"}=$name;
 7137:       $$metacache{"$key.type"}=$$metacache{':parameter_'.
 7138: 					   $allnames{$name}.'_'.$name.
 7139: 					   '.type'};
 7140:       my $olddis=$$metacache{':parameter_'.$allnames{$name}.'_'.$name.
 7141: 			     '.display'};
 7142:       my $expr='[Part: '.$allnames{$name}.']';
 7143:       $olddis=~s/\Q$expr\E/\[Part: 0\]/;
 7144:       $$metacache{"$key.display"}=$olddis;
 7145:     }
 7146: }
 7147: 
 7148: # ------------------------------------------------------ Devalidate title cache
 7149: 
 7150: sub devalidate_title_cache {
 7151:     my ($url)=@_;
 7152:     if (!$env{'request.course.id'}) { return; }
 7153:     my $symb=&symbread($url);
 7154:     if (!$symb) { return; }
 7155:     my $key=$env{'request.course.id'}."\0".$symb;
 7156:     &devalidate_cache_new('title',$key);
 7157: }
 7158: 
 7159: # ------------------------------------------------- Get the title of a resource
 7160: 
 7161: sub gettitle {
 7162:     my $urlsymb=shift;
 7163:     my $symb=&symbread($urlsymb);
 7164:     if ($symb) {
 7165: 	my $key=$env{'request.course.id'}."\0".$symb;
 7166: 	my ($result,$cached)=&is_cached_new('title',$key);
 7167: 	if (defined($cached)) { 
 7168: 	    return $result;
 7169: 	}
 7170: 	my ($map,$resid,$url)=&decode_symb($symb);
 7171: 	my $title='';
 7172: 	if (!$map && $resid == 0 && $url =~/default\.sequence$/) {
 7173: 	    $title = $env{'course.'.$env{'request.course.id'}.'.description'};
 7174: 	} else {
 7175: 	    if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
 7176: 		    &GDBM_READER(),0640)) {
 7177: 		my $mapid=$bighash{'map_pc_'.&clutter($map)};
 7178: 		$title=$bighash{'title_'.$mapid.'.'.$resid};
 7179: 		untie(%bighash);
 7180: 	    }
 7181: 	}
 7182: 	$title=~s/\&colon\;/\:/gs;
 7183: 	if ($title) {
 7184: 	    return &do_cache_new('title',$key,$title,600);
 7185: 	}
 7186: 	$urlsymb=$url;
 7187:     }
 7188:     my $title=&metadata($urlsymb,'title');
 7189:     if (!$title) { $title=(split('/',$urlsymb))[-1]; }    
 7190:     return $title;
 7191: }
 7192: 
 7193: sub get_slot {
 7194:     my ($which,$cnum,$cdom)=@_;
 7195:     if (!$cnum || !$cdom) {
 7196: 	(undef,my $courseid)=&whichuser();
 7197: 	$cdom=$env{'course.'.$courseid.'.domain'};
 7198: 	$cnum=$env{'course.'.$courseid.'.num'};
 7199:     }
 7200:     my $key=join("\0",'slots',$cdom,$cnum,$which);
 7201:     my %slotinfo;
 7202:     if (exists($remembered{$key})) {
 7203: 	$slotinfo{$which} = $remembered{$key};
 7204:     } else {
 7205: 	%slotinfo=&get('slots',[$which],$cdom,$cnum);
 7206: 	&Apache::lonhomework::showhash(%slotinfo);
 7207: 	my ($tmp)=keys(%slotinfo);
 7208: 	if ($tmp=~/^error:/) { return (); }
 7209: 	$remembered{$key} = $slotinfo{$which};
 7210:     }
 7211:     if (ref($slotinfo{$which}) eq 'HASH') {
 7212: 	return %{$slotinfo{$which}};
 7213:     }
 7214:     return $slotinfo{$which};
 7215: }
 7216: # ------------------------------------------------- Update symbolic store links
 7217: 
 7218: sub symblist {
 7219:     my ($mapname,%newhash)=@_;
 7220:     $mapname=&deversion(&declutter($mapname));
 7221:     my %hash;
 7222:     if (($env{'request.course.fn'}) && (%newhash)) {
 7223:         if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
 7224:                       &GDBM_WRCREAT(),0640)) {
 7225: 	    foreach my $url (keys %newhash) {
 7226: 		next if ($url eq 'last_known'
 7227: 			 && $env{'form.no_update_last_known'});
 7228: 		$hash{declutter($url)}=&encode_symb($mapname,
 7229: 						    $newhash{$url}->[1],
 7230: 						    $newhash{$url}->[0]);
 7231:             }
 7232:             if (untie(%hash)) {
 7233: 		return 'ok';
 7234:             }
 7235:         }
 7236:     }
 7237:     return 'error';
 7238: }
 7239: 
 7240: # --------------------------------------------------------------- Verify a symb
 7241: 
 7242: sub symbverify {
 7243:     my ($symb,$thisurl)=@_;
 7244:     my $thisfn=$thisurl;
 7245:     $thisfn=&declutter($thisfn);
 7246: # direct jump to resource in page or to a sequence - will construct own symbs
 7247:     if ($thisfn=~/\.(page|sequence)$/) { return 1; }
 7248: # check URL part
 7249:     my ($map,$resid,$url)=&decode_symb($symb);
 7250: 
 7251:     unless ($url eq $thisfn) { return 0; }
 7252: 
 7253:     $symb=&symbclean($symb);
 7254:     $thisurl=&deversion($thisurl);
 7255:     $thisfn=&deversion($thisfn);
 7256: 
 7257:     my %bighash;
 7258:     my $okay=0;
 7259: 
 7260:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
 7261:                             &GDBM_READER(),0640)) {
 7262:         my $ids=$bighash{'ids_'.&clutter($thisurl)};
 7263:         unless ($ids) { 
 7264:            $ids=$bighash{'ids_/'.$thisurl};
 7265:         }
 7266:         if ($ids) {
 7267: # ------------------------------------------------------------------- Has ID(s)
 7268: 	    foreach my $id (split(/\,/,$ids)) {
 7269: 	       my ($mapid,$resid)=split(/\./,$id);
 7270:                if (
 7271:   &symbclean(&declutter($bighash{'map_id_'.$mapid}).'___'.$resid.'___'.$thisfn)
 7272:    eq $symb) { 
 7273: 		   if (($env{'request.role.adv'}) ||
 7274: 		       $bighash{'encrypted_'.$id} eq $env{'request.enc'}) {
 7275: 		       $okay=1; 
 7276: 		   }
 7277: 	       }
 7278: 	   }
 7279:         }
 7280: 	untie(%bighash);
 7281:     }
 7282:     return $okay;
 7283: }
 7284: 
 7285: # --------------------------------------------------------------- Clean-up symb
 7286: 
 7287: sub symbclean {
 7288:     my $symb=shift;
 7289:     if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
 7290: # remove version from map
 7291:     $symb=~s/\.(\d+)\.(\w+)\_\_\_/\.$2\_\_\_/;
 7292: 
 7293: # remove version from URL
 7294:     $symb=~s/\.(\d+)\.(\w+)$/\.$2/;
 7295: 
 7296: # remove wrapper
 7297: 
 7298:     $symb=~s/(\_\_\_\d+\_\_\_)adm\/wrapper\/(res\/)*/$1/;
 7299:     $symb=~s/(\_\_\_\d+\_\_\_)adm\/coursedocs\/showdoc\/(res\/)*/$1/;
 7300:     return $symb;
 7301: }
 7302: 
 7303: # ---------------------------------------------- Split symb to find map and url
 7304: 
 7305: sub encode_symb {
 7306:     my ($map,$resid,$url)=@_;
 7307:     return &symbclean(&declutter($map).'___'.$resid.'___'.&declutter($url));
 7308: }
 7309: 
 7310: sub decode_symb {
 7311:     my $symb=shift;
 7312:     if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
 7313:     my ($map,$resid,$url)=split(/___/,$symb);
 7314:     return (&fixversion($map),$resid,&fixversion($url));
 7315: }
 7316: 
 7317: sub fixversion {
 7318:     my $fn=shift;
 7319:     if ($fn=~/^(adm|uploaded|editupload|public)/) { return $fn; }
 7320:     my %bighash;
 7321:     my $uri=&clutter($fn);
 7322:     my $key=$env{'request.course.id'}.'_'.$uri;
 7323: # is this cached?
 7324:     my ($result,$cached)=&is_cached_new('courseresversion',$key);
 7325:     if (defined($cached)) { return $result; }
 7326: # unfortunately not cached, or expired
 7327:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
 7328: 	    &GDBM_READER(),0640)) {
 7329:  	if ($bighash{'version_'.$uri}) {
 7330:  	    my $version=$bighash{'version_'.$uri};
 7331:  	    unless (($version eq 'mostrecent') || 
 7332: 		    ($version==&getversion($uri))) {
 7333:  		$uri=~s/\.(\w+)$/\.$version\.$1/;
 7334:  	    }
 7335:  	}
 7336:  	untie %bighash;
 7337:     }
 7338:     return &do_cache_new('courseresversion',$key,&declutter($uri),600);
 7339: }
 7340: 
 7341: sub deversion {
 7342:     my $url=shift;
 7343:     $url=~s/\.\d+\.(\w+)$/\.$1/;
 7344:     return $url;
 7345: }
 7346: 
 7347: # ------------------------------------------------------ Return symb list entry
 7348: 
 7349: sub symbread {
 7350:     my ($thisfn,$donotrecurse)=@_;
 7351:     my $cache_str='request.symbread.cached.'.$thisfn;
 7352:     if (defined($env{$cache_str})) { return $env{$cache_str}; }
 7353: # no filename provided? try from environment
 7354:     unless ($thisfn) {
 7355:         if ($env{'request.symb'}) {
 7356: 	    return $env{$cache_str}=&symbclean($env{'request.symb'});
 7357: 	}
 7358: 	$thisfn=$env{'request.filename'};
 7359:     }
 7360:     if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
 7361: # is that filename actually a symb? Verify, clean, and return
 7362:     if ($thisfn=~/\_\_\_\d+\_\_\_(.*)$/) {
 7363: 	if (&symbverify($thisfn,$1)) {
 7364: 	    return $env{$cache_str}=&symbclean($thisfn);
 7365: 	}
 7366:     }
 7367:     $thisfn=declutter($thisfn);
 7368:     my %hash;
 7369:     my %bighash;
 7370:     my $syval='';
 7371:     if (($env{'request.course.fn'}) && ($thisfn)) {
 7372:         my $targetfn = $thisfn;
 7373:         if ( ($thisfn =~ m/^(uploaded|editupload)\//) && ($thisfn !~ m/\.(page|sequence)$/) ) {
 7374:             $targetfn = 'adm/wrapper/'.$thisfn;
 7375:         }
 7376: 	if ($targetfn =~ m|^adm/wrapper/(ext/.*)|) {
 7377: 	    $targetfn=$1;
 7378: 	}
 7379:         if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
 7380:                       &GDBM_READER(),0640)) {
 7381: 	    $syval=$hash{$targetfn};
 7382:             untie(%hash);
 7383:         }
 7384: # ---------------------------------------------------------- There was an entry
 7385:         if ($syval) {
 7386: 	    #unless ($syval=~/\_\d+$/) {
 7387: 		#unless ($env{'form.request.prefix'}=~/\.(\d+)\_$/) {
 7388: 		    #&appenv('request.ambiguous' => $thisfn);
 7389: 		    #return $env{$cache_str}='';
 7390: 		#}    
 7391: 		#$syval.=$1;
 7392: 	    #}
 7393:         } else {
 7394: # ------------------------------------------------------- Was not in symb table
 7395:            if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
 7396:                             &GDBM_READER(),0640)) {
 7397: # ---------------------------------------------- Get ID(s) for current resource
 7398:               my $ids=$bighash{'ids_'.&clutter($thisfn)};
 7399:               unless ($ids) { 
 7400:                  $ids=$bighash{'ids_/'.$thisfn};
 7401:               }
 7402:               unless ($ids) {
 7403: # alias?
 7404: 		  $ids=$bighash{'mapalias_'.$thisfn};
 7405:               }
 7406:               if ($ids) {
 7407: # ------------------------------------------------------------------- Has ID(s)
 7408:                  my @possibilities=split(/\,/,$ids);
 7409:                  if ($#possibilities==0) {
 7410: # ----------------------------------------------- There is only one possibility
 7411: 		     my ($mapid,$resid)=split(/\./,$ids);
 7412: 		     $syval=&encode_symb($bighash{'map_id_'.$mapid},
 7413: 						    $resid,$thisfn);
 7414:                  } elsif (!$donotrecurse) {
 7415: # ------------------------------------------ There is more than one possibility
 7416:                      my $realpossible=0;
 7417:                      foreach my $id (@possibilities) {
 7418: 			 my $file=$bighash{'src_'.$id};
 7419:                          if (&allowed('bre',$file)) {
 7420:          		    my ($mapid,$resid)=split(/\./,$id);
 7421:                             if ($bighash{'map_type_'.$mapid} ne 'page') {
 7422: 				$realpossible++;
 7423:                                 $syval=&encode_symb($bighash{'map_id_'.$mapid},
 7424: 						    $resid,$thisfn);
 7425:                             }
 7426: 			 }
 7427:                      }
 7428: 		     if ($realpossible!=1) { $syval=''; }
 7429:                  } else {
 7430:                      $syval='';
 7431:                  }
 7432: 	      }
 7433:               untie(%bighash)
 7434:            }
 7435:         }
 7436:         if ($syval) {
 7437: 	    return $env{$cache_str}=$syval;
 7438:         }
 7439:     }
 7440:     &appenv('request.ambiguous' => $thisfn);
 7441:     return $env{$cache_str}='';
 7442: }
 7443: 
 7444: # ---------------------------------------------------------- Return random seed
 7445: 
 7446: sub numval {
 7447:     my $txt=shift;
 7448:     $txt=~tr/A-J/0-9/;
 7449:     $txt=~tr/a-j/0-9/;
 7450:     $txt=~tr/K-T/0-9/;
 7451:     $txt=~tr/k-t/0-9/;
 7452:     $txt=~tr/U-Z/0-5/;
 7453:     $txt=~tr/u-z/0-5/;
 7454:     $txt=~s/\D//g;
 7455:     if ($_64bit) { if ($txt > 2**32) { return -1; } }
 7456:     return int($txt);
 7457: }
 7458: 
 7459: sub numval2 {
 7460:     my $txt=shift;
 7461:     $txt=~tr/A-J/0-9/;
 7462:     $txt=~tr/a-j/0-9/;
 7463:     $txt=~tr/K-T/0-9/;
 7464:     $txt=~tr/k-t/0-9/;
 7465:     $txt=~tr/U-Z/0-5/;
 7466:     $txt=~tr/u-z/0-5/;
 7467:     $txt=~s/\D//g;
 7468:     my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
 7469:     my $total;
 7470:     foreach my $val (@txts) { $total+=$val; }
 7471:     if ($_64bit) { if ($total > 2**32) { return -1; } }
 7472:     return int($total);
 7473: }
 7474: 
 7475: sub numval3 {
 7476:     use integer;
 7477:     my $txt=shift;
 7478:     $txt=~tr/A-J/0-9/;
 7479:     $txt=~tr/a-j/0-9/;
 7480:     $txt=~tr/K-T/0-9/;
 7481:     $txt=~tr/k-t/0-9/;
 7482:     $txt=~tr/U-Z/0-5/;
 7483:     $txt=~tr/u-z/0-5/;
 7484:     $txt=~s/\D//g;
 7485:     my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
 7486:     my $total;
 7487:     foreach my $val (@txts) { $total+=$val; }
 7488:     if ($_64bit) { $total=(($total<<32)>>32); }
 7489:     return $total;
 7490: }
 7491: 
 7492: sub digest {
 7493:     my ($data)=@_;
 7494:     my $digest=&Digest::MD5::md5($data);
 7495:     my ($a,$b,$c,$d)=unpack("iiii",$digest);
 7496:     my ($e,$f);
 7497:     {
 7498:         use integer;
 7499:         $e=($a+$b);
 7500:         $f=($c+$d);
 7501:         if ($_64bit) {
 7502:             $e=(($e<<32)>>32);
 7503:             $f=(($f<<32)>>32);
 7504:         }
 7505:     }
 7506:     if (wantarray) {
 7507: 	return ($e,$f);
 7508:     } else {
 7509: 	my $g;
 7510: 	{
 7511: 	    use integer;
 7512: 	    $g=($e+$f);
 7513: 	    if ($_64bit) {
 7514: 		$g=(($g<<32)>>32);
 7515: 	    }
 7516: 	}
 7517: 	return $g;
 7518:     }
 7519: }
 7520: 
 7521: sub latest_rnd_algorithm_id {
 7522:     return '64bit5';
 7523: }
 7524: 
 7525: sub get_rand_alg {
 7526:     my ($courseid)=@_;
 7527:     if (!$courseid) { $courseid=(&whichuser())[1]; }
 7528:     if ($courseid) {
 7529: 	return $env{"course.$courseid.rndseed"};
 7530:     }
 7531:     return &latest_rnd_algorithm_id();
 7532: }
 7533: 
 7534: sub validCODE {
 7535:     my ($CODE)=@_;
 7536:     if (defined($CODE) && $CODE ne '' && $CODE =~ /^\w+$/) { return 1; }
 7537:     return 0;
 7538: }
 7539: 
 7540: sub getCODE {
 7541:     if (&validCODE($env{'form.CODE'})) { return $env{'form.CODE'}; }
 7542:     if ( (defined($Apache::lonhomework::parsing_a_problem) ||
 7543: 	  defined($Apache::lonhomework::parsing_a_task) ) &&
 7544: 	 &validCODE($Apache::lonhomework::history{'resource.CODE'})) {
 7545: 	return $Apache::lonhomework::history{'resource.CODE'};
 7546:     }
 7547:     return undef;
 7548: }
 7549: 
 7550: sub rndseed {
 7551:     my ($symb,$courseid,$domain,$username)=@_;
 7552:     my ($wsymb,$wcourseid,$wdomain,$wusername)=&whichuser();
 7553:     if (!defined($symb)) {
 7554: 	unless ($symb=$wsymb) { return time; }
 7555:     }
 7556:     if (!$courseid) { $courseid=$wcourseid; }
 7557:     if (!$domain) { $domain=$wdomain; }
 7558:     if (!$username) { $username=$wusername }
 7559:     my $which=&get_rand_alg();
 7560: 
 7561:     if (defined(&getCODE())) {
 7562: 	if ($which eq '64bit5') {
 7563: 	    return &rndseed_CODE_64bit5($symb,$courseid,$domain,$username);
 7564: 	} elsif ($which eq '64bit4') {
 7565: 	    return &rndseed_CODE_64bit4($symb,$courseid,$domain,$username);
 7566: 	} else {
 7567: 	    return &rndseed_CODE_64bit($symb,$courseid,$domain,$username);
 7568: 	}
 7569:     } elsif ($which eq '64bit5') {
 7570: 	return &rndseed_64bit5($symb,$courseid,$domain,$username);
 7571:     } elsif ($which eq '64bit4') {
 7572: 	return &rndseed_64bit4($symb,$courseid,$domain,$username);
 7573:     } elsif ($which eq '64bit3') {
 7574: 	return &rndseed_64bit3($symb,$courseid,$domain,$username);
 7575:     } elsif ($which eq '64bit2') {
 7576: 	return &rndseed_64bit2($symb,$courseid,$domain,$username);
 7577:     } elsif ($which eq '64bit') {
 7578: 	return &rndseed_64bit($symb,$courseid,$domain,$username);
 7579:     }
 7580:     return &rndseed_32bit($symb,$courseid,$domain,$username);
 7581: }
 7582: 
 7583: sub rndseed_32bit {
 7584:     my ($symb,$courseid,$domain,$username)=@_;
 7585:     {
 7586: 	use integer;
 7587: 	my $symbchck=unpack("%32C*",$symb) << 27;
 7588: 	my $symbseed=numval($symb) << 22;
 7589: 	my $namechck=unpack("%32C*",$username) << 17;
 7590: 	my $nameseed=numval($username) << 12;
 7591: 	my $domainseed=unpack("%32C*",$domain) << 7;
 7592: 	my $courseseed=unpack("%32C*",$courseid);
 7593: 	my $num=$symbseed+$nameseed+$domainseed+$courseseed+$namechck+$symbchck;
 7594: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
 7595: 	#&logthis("rndseed :$num:$symb");
 7596: 	if ($_64bit) { $num=(($num<<32)>>32); }
 7597: 	return $num;
 7598:     }
 7599: }
 7600: 
 7601: sub rndseed_64bit {
 7602:     my ($symb,$courseid,$domain,$username)=@_;
 7603:     {
 7604: 	use integer;
 7605: 	my $symbchck=unpack("%32S*",$symb) << 21;
 7606: 	my $symbseed=numval($symb) << 10;
 7607: 	my $namechck=unpack("%32S*",$username);
 7608: 	
 7609: 	my $nameseed=numval($username) << 21;
 7610: 	my $domainseed=unpack("%32S*",$domain) << 10;
 7611: 	my $courseseed=unpack("%32S*",$courseid);
 7612: 	
 7613: 	my $num1=$symbchck+$symbseed+$namechck;
 7614: 	my $num2=$nameseed+$domainseed+$courseseed;
 7615: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
 7616: 	#&logthis("rndseed :$num:$symb");
 7617: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
 7618: 	return "$num1,$num2";
 7619:     }
 7620: }
 7621: 
 7622: sub rndseed_64bit2 {
 7623:     my ($symb,$courseid,$domain,$username)=@_;
 7624:     {
 7625: 	use integer;
 7626: 	# strings need to be an even # of cahracters long, it it is odd the
 7627:         # last characters gets thrown away
 7628: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
 7629: 	my $symbseed=numval($symb) << 10;
 7630: 	my $namechck=unpack("%32S*",$username.' ');
 7631: 	
 7632: 	my $nameseed=numval($username) << 21;
 7633: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
 7634: 	my $courseseed=unpack("%32S*",$courseid.' ');
 7635: 	
 7636: 	my $num1=$symbchck+$symbseed+$namechck;
 7637: 	my $num2=$nameseed+$domainseed+$courseseed;
 7638: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
 7639: 	#&logthis("rndseed :$num:$symb");
 7640: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
 7641: 	return "$num1,$num2";
 7642:     }
 7643: }
 7644: 
 7645: sub rndseed_64bit3 {
 7646:     my ($symb,$courseid,$domain,$username)=@_;
 7647:     {
 7648: 	use integer;
 7649: 	# strings need to be an even # of cahracters long, it it is odd the
 7650:         # last characters gets thrown away
 7651: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
 7652: 	my $symbseed=numval2($symb) << 10;
 7653: 	my $namechck=unpack("%32S*",$username.' ');
 7654: 	
 7655: 	my $nameseed=numval2($username) << 21;
 7656: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
 7657: 	my $courseseed=unpack("%32S*",$courseid.' ');
 7658: 	
 7659: 	my $num1=$symbchck+$symbseed+$namechck;
 7660: 	my $num2=$nameseed+$domainseed+$courseseed;
 7661: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
 7662: 	#&logthis("rndseed :$num1:$num2:$_64bit");
 7663: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
 7664: 	
 7665: 	return "$num1:$num2";
 7666:     }
 7667: }
 7668: 
 7669: sub rndseed_64bit4 {
 7670:     my ($symb,$courseid,$domain,$username)=@_;
 7671:     {
 7672: 	use integer;
 7673: 	# strings need to be an even # of cahracters long, it it is odd the
 7674:         # last characters gets thrown away
 7675: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
 7676: 	my $symbseed=numval3($symb) << 10;
 7677: 	my $namechck=unpack("%32S*",$username.' ');
 7678: 	
 7679: 	my $nameseed=numval3($username) << 21;
 7680: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
 7681: 	my $courseseed=unpack("%32S*",$courseid.' ');
 7682: 	
 7683: 	my $num1=$symbchck+$symbseed+$namechck;
 7684: 	my $num2=$nameseed+$domainseed+$courseseed;
 7685: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
 7686: 	#&logthis("rndseed :$num1:$num2:$_64bit");
 7687: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
 7688: 	
 7689: 	return "$num1:$num2";
 7690:     }
 7691: }
 7692: 
 7693: sub rndseed_64bit5 {
 7694:     my ($symb,$courseid,$domain,$username)=@_;
 7695:     my ($num1,$num2)=&digest("$symb,$courseid,$domain,$username");
 7696:     return "$num1:$num2";
 7697: }
 7698: 
 7699: sub rndseed_CODE_64bit {
 7700:     my ($symb,$courseid,$domain,$username)=@_;
 7701:     {
 7702: 	use integer;
 7703: 	my $symbchck=unpack("%32S*",$symb.' ') << 16;
 7704: 	my $symbseed=numval2($symb);
 7705: 	my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
 7706: 	my $CODEseed=numval(&getCODE());
 7707: 	my $courseseed=unpack("%32S*",$courseid.' ');
 7708: 	my $num1=$symbseed+$CODEchck;
 7709: 	my $num2=$CODEseed+$courseseed+$symbchck;
 7710: 	#&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
 7711: 	#&logthis("rndseed :$num1:$num2:$symb");
 7712: 	if ($_64bit) { $num1=(($num1<<32)>>32); }
 7713: 	if ($_64bit) { $num2=(($num2<<32)>>32); }
 7714: 	return "$num1:$num2";
 7715:     }
 7716: }
 7717: 
 7718: sub rndseed_CODE_64bit4 {
 7719:     my ($symb,$courseid,$domain,$username)=@_;
 7720:     {
 7721: 	use integer;
 7722: 	my $symbchck=unpack("%32S*",$symb.' ') << 16;
 7723: 	my $symbseed=numval3($symb);
 7724: 	my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
 7725: 	my $CODEseed=numval3(&getCODE());
 7726: 	my $courseseed=unpack("%32S*",$courseid.' ');
 7727: 	my $num1=$symbseed+$CODEchck;
 7728: 	my $num2=$CODEseed+$courseseed+$symbchck;
 7729: 	#&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
 7730: 	#&logthis("rndseed :$num1:$num2:$symb");
 7731: 	if ($_64bit) { $num1=(($num1<<32)>>32); }
 7732: 	if ($_64bit) { $num2=(($num2<<32)>>32); }
 7733: 	return "$num1:$num2";
 7734:     }
 7735: }
 7736: 
 7737: sub rndseed_CODE_64bit5 {
 7738:     my ($symb,$courseid,$domain,$username)=@_;
 7739:     my $code = &getCODE();
 7740:     my ($num1,$num2)=&digest("$symb,$courseid,$code");
 7741:     return "$num1:$num2";
 7742: }
 7743: 
 7744: sub setup_random_from_rndseed {
 7745:     my ($rndseed)=@_;
 7746:     if ($rndseed =~/([,:])/) {
 7747: 	my ($num1,$num2)=split(/[,:]/,$rndseed);
 7748: 	&Math::Random::random_set_seed(abs($num1),abs($num2));
 7749:     } else {
 7750: 	&Math::Random::random_set_seed_from_phrase($rndseed);
 7751:     }
 7752: }
 7753: 
 7754: sub latest_receipt_algorithm_id {
 7755:     return 'receipt3';
 7756: }
 7757: 
 7758: sub recunique {
 7759:     my $fucourseid=shift;
 7760:     my $unique;
 7761:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2' ||
 7762: 	$env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
 7763: 	$unique=$env{"course.$fucourseid.internal.encseed"};
 7764:     } else {
 7765: 	$unique=$perlvar{'lonReceipt'};
 7766:     }
 7767:     return unpack("%32C*",$unique);
 7768: }
 7769: 
 7770: sub recprefix {
 7771:     my $fucourseid=shift;
 7772:     my $prefix;
 7773:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2'||
 7774: 	$env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
 7775: 	$prefix=$env{"course.$fucourseid.internal.encpref"};
 7776:     } else {
 7777: 	$prefix=$perlvar{'lonHostID'};
 7778:     }
 7779:     return unpack("%32C*",$prefix);
 7780: }
 7781: 
 7782: sub ireceipt {
 7783:     my ($funame,$fudom,$fucourseid,$fusymb,$part)=@_;
 7784: 
 7785:     my $return =&recprefix($fucourseid).'-';
 7786: 
 7787:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt3' ||
 7788: 	$env{'request.state'} eq 'construct') {
 7789: 	$return .= (&digest("$funame,$fudom,$fucourseid,$fusymb,$part")%10000);
 7790: 	return $return;
 7791:     }
 7792: 
 7793:     my $cuname=unpack("%32C*",$funame);
 7794:     my $cudom=unpack("%32C*",$fudom);
 7795:     my $cucourseid=unpack("%32C*",$fucourseid);
 7796:     my $cusymb=unpack("%32C*",$fusymb);
 7797:     my $cunique=&recunique($fucourseid);
 7798:     my $cpart=unpack("%32S*",$part);
 7799:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2') {
 7800: 
 7801: 	#&logthis("doing receipt2  using parts $cpart, uname $cuname and udom $cudom gets  ".($cpart%$cuname)." and ".($cpart%$cudom));
 7802: 			       
 7803: 	$return.= ($cunique%$cuname+
 7804: 		   $cunique%$cudom+
 7805: 		   $cusymb%$cuname+
 7806: 		   $cusymb%$cudom+
 7807: 		   $cucourseid%$cuname+
 7808: 		   $cucourseid%$cudom+
 7809: 		   $cpart%$cuname+
 7810: 		   $cpart%$cudom);
 7811:     } else {
 7812: 	$return.= ($cunique%$cuname+
 7813: 		   $cunique%$cudom+
 7814: 		   $cusymb%$cuname+
 7815: 		   $cusymb%$cudom+
 7816: 		   $cucourseid%$cuname+
 7817: 		   $cucourseid%$cudom);
 7818:     }
 7819:     return $return;
 7820: }
 7821: 
 7822: sub receipt {
 7823:     my ($part)=@_;
 7824:     my ($symb,$courseid,$domain,$name) = &whichuser();
 7825:     return &ireceipt($name,$domain,$courseid,$symb,$part);
 7826: }
 7827: 
 7828: sub whichuser {
 7829:     my ($passedsymb)=@_;
 7830:     my ($symb,$courseid,$domain,$name,$publicuser);
 7831:     if (defined($env{'form.grade_symb'})) {
 7832: 	my ($tmp_courseid)=&get_env_multiple('form.grade_courseid');
 7833: 	my $allowed=&allowed('vgr',$tmp_courseid);
 7834: 	if (!$allowed &&
 7835: 	    exists($env{'request.course.sec'}) &&
 7836: 	    $env{'request.course.sec'} !~ /^\s*$/) {
 7837: 	    $allowed=&allowed('vgr',$tmp_courseid.
 7838: 			      '/'.$env{'request.course.sec'});
 7839: 	}
 7840: 	if ($allowed) {
 7841: 	    ($symb)=&get_env_multiple('form.grade_symb');
 7842: 	    $courseid=$tmp_courseid;
 7843: 	    ($domain)=&get_env_multiple('form.grade_domain');
 7844: 	    ($name)=&get_env_multiple('form.grade_username');
 7845: 	    return ($symb,$courseid,$domain,$name,$publicuser);
 7846: 	}
 7847:     }
 7848:     if (!$passedsymb) {
 7849: 	$symb=&symbread();
 7850:     } else {
 7851: 	$symb=$passedsymb;
 7852:     }
 7853:     $courseid=$env{'request.course.id'};
 7854:     $domain=$env{'user.domain'};
 7855:     $name=$env{'user.name'};
 7856:     if ($name eq 'public' && $domain eq 'public') {
 7857: 	if (!defined($env{'form.username'})) {
 7858: 	    $env{'form.username'}.=time.rand(10000000);
 7859: 	}
 7860: 	$name.=$env{'form.username'};
 7861:     }
 7862:     return ($symb,$courseid,$domain,$name,$publicuser);
 7863: 
 7864: }
 7865: 
 7866: # ------------------------------------------------------------ Serves up a file
 7867: # returns either the contents of the file or 
 7868: # -1 if the file doesn't exist
 7869: #
 7870: # if the target is a file that was uploaded via DOCS, 
 7871: # a check will be made to see if a current copy exists on the local server,
 7872: # if it does this will be served, otherwise a copy will be retrieved from
 7873: # the home server for the course and stored in /home/httpd/html/userfiles on
 7874: # the local server.   
 7875: 
 7876: sub getfile {
 7877:     my ($file) = @_;
 7878:     if ($file =~ m -^/*(uploaded|editupload)/-) { $file=&filelocation("",$file); }
 7879:     &repcopy($file);
 7880:     return &readfile($file);
 7881: }
 7882: 
 7883: sub repcopy_userfile {
 7884:     my ($file)=@_;
 7885:     if ($file =~ m -^/*(uploaded|editupload)/-) { $file=&filelocation("",$file); }
 7886:     if ($file =~ m|^/home/httpd/html/lonUsers/|) { return 'ok'; }
 7887:     my ($cdom,$cnum,$filename) = 
 7888: 	($file=~m|^\Q$perlvar{'lonDocRoot'}\E/+userfiles/+($match_domain)/+($match_name)/+(.*)|);
 7889:     my $uri="/uploaded/$cdom/$cnum/$filename";
 7890:     if (-e "$file") {
 7891: # we already have a local copy, check it out
 7892: 	my @fileinfo = stat($file);
 7893: 	my $rtncode;
 7894: 	my $info;
 7895: 	my $lwpresp = &getuploaded('HEAD',$uri,$cdom,$cnum,\$info,\$rtncode);
 7896: 	if ($lwpresp ne 'ok') {
 7897: # there is no such file anymore, even though we had a local copy
 7898: 	    if ($rtncode eq '404') {
 7899: 		unlink($file);
 7900: 	    }
 7901: 	    return -1;
 7902: 	}
 7903: 	if ($info < $fileinfo[9]) {
 7904: # nice, the file we have is up-to-date, just say okay
 7905: 	    return 'ok';
 7906: 	} else {
 7907: # the file is outdated, get rid of it
 7908: 	    unlink($file);
 7909: 	}
 7910:     }
 7911: # one way or the other, at this point, we don't have the file
 7912: # construct the correct path for the file
 7913:     my @parts = ($cdom,$cnum); 
 7914:     if ($filename =~ m|^(.+)/[^/]+$|) {
 7915: 	push @parts, split(/\//,$1);
 7916:     }
 7917:     my $path = $perlvar{'lonDocRoot'}.'/userfiles';
 7918:     foreach my $part (@parts) {
 7919: 	$path .= '/'.$part;
 7920: 	if (!-e $path) {
 7921: 	    mkdir($path,0770);
 7922: 	}
 7923:     }
 7924: # now the path exists for sure
 7925: # get a user agent
 7926:     my $ua=new LWP::UserAgent;
 7927:     my $transferfile=$file.'.in.transfer';
 7928: # FIXME: this should flock
 7929:     if (-e $transferfile) { return 'ok'; }
 7930:     my $request;
 7931:     $uri=~s/^\///;
 7932:     $request=new HTTP::Request('GET','http://'.&hostname(&homeserver($cnum,$cdom)).'/raw/'.$uri);
 7933:     my $response=$ua->request($request,$transferfile);
 7934: # did it work?
 7935:     if ($response->is_error()) {
 7936: 	unlink($transferfile);
 7937: 	&logthis("Userfile repcopy failed for $uri");
 7938: 	return -1;
 7939:     }
 7940: # worked, rename the transfer file
 7941:     rename($transferfile,$file);
 7942:     return 'ok';
 7943: }
 7944: 
 7945: sub tokenwrapper {
 7946:     my $uri=shift;
 7947:     $uri=~s|^http\://([^/]+)||;
 7948:     $uri=~s|^/||;
 7949:     $env{'user.environment'}=~/\/([^\/]+)\.id/;
 7950:     my $token=$1;
 7951:     my (undef,$udom,$uname,$file)=split('/',$uri,4);
 7952:     if ($udom && $uname && $file) {
 7953: 	$file=~s|(\?\.*)*$||;
 7954:         &appenv("userfile.$udom/$uname/$file" => $env{'request.course.id'});
 7955:         return 'http://'.&hostname(&homeserver($uname,$udom)).'/'.$uri.
 7956:                (($uri=~/\?/)?'&':'?').'token='.$token.
 7957:                                '&tokenissued='.$perlvar{'lonHostID'};
 7958:     } else {
 7959:         return '/adm/notfound.html';
 7960:     }
 7961: }
 7962: 
 7963: # call with reqtype HEAD: get last modification time
 7964: # call with reqtype GET: get the file contents
 7965: # Do not call this with reqtype GET for large files! It loads everything into memory
 7966: #
 7967: sub getuploaded {
 7968:     my ($reqtype,$uri,$cdom,$cnum,$info,$rtncode) = @_;
 7969:     $uri=~s/^\///;
 7970:     $uri = 'http://'.&hostname(&homeserver($cnum,$cdom)).'/raw/'.$uri;
 7971:     my $ua=new LWP::UserAgent;
 7972:     my $request=new HTTP::Request($reqtype,$uri);
 7973:     my $response=$ua->request($request);
 7974:     $$rtncode = $response->code;
 7975:     if (! $response->is_success()) {
 7976: 	return 'failed';
 7977:     }      
 7978:     if ($reqtype eq 'HEAD') {
 7979: 	$$info = &HTTP::Date::str2time( $response->header('Last-modified') );
 7980:     } elsif ($reqtype eq 'GET') {
 7981: 	$$info = $response->content;
 7982:     }
 7983:     return 'ok';
 7984: }
 7985: 
 7986: sub readfile {
 7987:     my $file = shift;
 7988:     if ( (! -e $file ) || ($file eq '') ) { return -1; };
 7989:     my $fh;
 7990:     open($fh,"<$file");
 7991:     my $a='';
 7992:     while (my $line = <$fh>) { $a .= $line; }
 7993:     return $a;
 7994: }
 7995: 
 7996: sub filelocation {
 7997:     my ($dir,$file) = @_;
 7998:     my $location;
 7999:     $file=~ s/^\s*(\S+)\s*$/$1/; ## strip off leading and trailing spaces
 8000: 
 8001:     if ($file =~ m-^/adm/-) {
 8002: 	$file=~s-^/adm/wrapper/-/-;
 8003: 	$file=~s-^/adm/coursedocs/showdoc/-/-;
 8004:     }
 8005: 
 8006:     if ($file=~m:^/~:) { # is a contruction space reference
 8007:         $location = $file;
 8008:         $location =~ s:/~(.*?)/(.*):/home/$1/public_html/$2:;
 8009:     } elsif ($file=~m{^/home/$match_username/public_html/}) {
 8010: 	# is a correct contruction space reference
 8011:         $location = $file;
 8012:     } elsif ($file=~/^\/*(uploaded|editupload)/) { # is an uploaded file
 8013:         my ($udom,$uname,$filename)=
 8014:   	    ($file=~m -^/+(?:uploaded|editupload)/+($match_domain)/+($match_name)/+(.*)$-);
 8015:         my $home=&homeserver($uname,$udom);
 8016:         my $is_me=0;
 8017:         my @ids=&current_machine_ids();
 8018:         foreach my $id (@ids) { if ($id eq $home) { $is_me=1; } }
 8019:         if ($is_me) {
 8020:   	    $location=&propath($udom,$uname).
 8021:   	      '/userfiles/'.$filename;
 8022:         } else {
 8023:   	  $location=$Apache::lonnet::perlvar{'lonDocRoot'}.'/userfiles/'.
 8024:   	      $udom.'/'.$uname.'/'.$filename;
 8025:         }
 8026:     } elsif ($file =~ m-^/adm/-) {
 8027: 	$location = $perlvar{'lonDocRoot'}.'/'.$file;
 8028:     } else {
 8029:         $file=~s/^\Q$perlvar{'lonDocRoot'}\E//;
 8030:         $file=~s:^/res/:/:;
 8031:         if ( !( $file =~ m:^/:) ) {
 8032:             $location = $dir. '/'.$file;
 8033:         } else {
 8034:             $location = '/home/httpd/html/res'.$file;
 8035:         }
 8036:     }
 8037:     $location=~s://+:/:g; # remove duplicate /
 8038:     while ($location=~m{/\.\./}) {
 8039: 	if ($location =~ m{/[^/]+/\.\./}) {
 8040: 	    $location=~ s{/[^/]+/\.\./}{/}g;
 8041: 	} else {
 8042: 	    $location=~ s{/\.\./}{/}g;
 8043: 	}
 8044:     } #remove dir/..
 8045:     while ($location=~m:/\./:) {$location=~ s:/\./:/:g;} #remove /./
 8046:     return $location;
 8047: }
 8048: 
 8049: sub hreflocation {
 8050:     my ($dir,$file)=@_;
 8051:     unless (($file=~m-^http://-i) || ($file=~m-^/-)) {
 8052: 	$file=filelocation($dir,$file);
 8053:     } elsif ($file=~m-^/adm/-) {
 8054: 	$file=~s-^/adm/wrapper/-/-;
 8055: 	$file=~s-^/adm/coursedocs/showdoc/-/-;
 8056:     }
 8057:     if ($file=~m-^\Q$perlvar{'lonDocRoot'}\E-) {
 8058: 	$file=~s-^\Q$perlvar{'lonDocRoot'}\E--;
 8059:     } elsif ($file=~m-/home/($match_username)/public_html/-) {
 8060: 	$file=~s-^/home/($match_username)/public_html/-/~$1/-;
 8061:     } elsif ($file=~m-^\Q$perlvar{'lonUsersDir'}\E-) {
 8062: 	$file=~s-^/home/httpd/lonUsers/($match_domain)/./././($match_name)/userfiles/
 8063: 	    -/uploaded/$1/$2/-x;
 8064:     }
 8065:     if ($file=~ m{^/userfiles/}) {
 8066: 	$file =~ s{^/userfiles/}{/uploaded/};
 8067:     }
 8068:     return $file;
 8069: }
 8070: 
 8071: sub current_machine_domains {
 8072:     return &machine_domains(&hostname($perlvar{'lonHostID'}));
 8073: }
 8074: 
 8075: sub machine_domains {
 8076:     my ($hostname) = @_;
 8077:     my @domains;
 8078:     my %hostname = &all_hostnames();
 8079:     while( my($id, $name) = each(%hostname)) {
 8080: #	&logthis("-$id-$name-$hostname-");
 8081: 	if ($hostname eq $name) {
 8082: 	    push(@domains,&host_domain($id));
 8083: 	}
 8084:     }
 8085:     return @domains;
 8086: }
 8087: 
 8088: sub current_machine_ids {
 8089:     return &machine_ids(&hostname($perlvar{'lonHostID'}));
 8090: }
 8091: 
 8092: sub machine_ids {
 8093:     my ($hostname) = @_;
 8094:     $hostname ||= &hostname($perlvar{'lonHostID'});
 8095:     my @ids;
 8096:     my %name_to_host = &all_names();
 8097:     if (ref($name_to_host{$hostname}) eq 'ARRAY') {
 8098: 	return @{ $name_to_host{$hostname} };
 8099:     }
 8100:     return;
 8101: }
 8102: 
 8103: sub additional_machine_domains {
 8104:     my @domains;
 8105:     open(my $fh,"<$perlvar{'lonTabDir'}/expected_domains.tab");
 8106:     while( my $line = <$fh>) {
 8107:         $line =~ s/\s//g;
 8108:         push(@domains,$line);
 8109:     }
 8110:     return @domains;
 8111: }
 8112: 
 8113: sub default_login_domain {
 8114:     my $domain = $perlvar{'lonDefDomain'};
 8115:     my $testdomain=(split(/\./,$ENV{'HTTP_HOST'}))[0];
 8116:     foreach my $posdom (&current_machine_domains(),
 8117:                         &additional_machine_domains()) {
 8118:         if (lc($posdom) eq lc($testdomain)) {
 8119:             $domain=$posdom;
 8120:             last;
 8121:         }
 8122:     }
 8123:     return $domain;
 8124: }
 8125: 
 8126: # ------------------------------------------------------------- Declutters URLs
 8127: 
 8128: sub declutter {
 8129:     my $thisfn=shift;
 8130:     if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
 8131:     $thisfn=~s/^\Q$perlvar{'lonDocRoot'}\E//;
 8132:     $thisfn=~s/^\///;
 8133:     $thisfn=~s|^adm/wrapper/||;
 8134:     $thisfn=~s|^adm/coursedocs/showdoc/||;
 8135:     $thisfn=~s/^res\///;
 8136:     $thisfn=~s/\?.+$//;
 8137:     return $thisfn;
 8138: }
 8139: 
 8140: # ------------------------------------------------------------- Clutter up URLs
 8141: 
 8142: sub clutter {
 8143:     my $thisfn='/'.&declutter(shift);
 8144:     if ($thisfn !~ m{^/(uploaded|editupload|adm|userfiles|ext|raw|priv|public)/}
 8145: 	|| $thisfn =~ m{^/adm/(includes|pages)} ) { 
 8146:        $thisfn='/res'.$thisfn; 
 8147:     }
 8148:     if ($thisfn !~m|/adm|) {
 8149: 	if ($thisfn =~ m|/ext/|) {
 8150: 	    $thisfn='/adm/wrapper'.$thisfn;
 8151: 	} else {
 8152: 	    my ($ext) = ($thisfn =~ /\.(\w+)$/);
 8153: 	    my $embstyle=&Apache::loncommon::fileembstyle($ext);
 8154: 	    if ($embstyle eq 'ssi'
 8155: 		|| ($embstyle eq 'hdn')
 8156: 		|| ($embstyle eq 'rat')
 8157: 		|| ($embstyle eq 'prv')
 8158: 		|| ($embstyle eq 'ign')) {
 8159: 		#do nothing with these
 8160: 	    } elsif (($embstyle eq 'img') 
 8161: 		|| ($embstyle eq 'emb')
 8162: 		|| ($embstyle eq 'wrp')) {
 8163: 		$thisfn='/adm/wrapper'.$thisfn;
 8164: 	    } elsif ($embstyle eq 'unk'
 8165: 		     && $thisfn!~/\.(sequence|page)$/) {
 8166: 		$thisfn='/adm/coursedocs/showdoc'.$thisfn;
 8167: 	    } else {
 8168: #		&logthis("Got a blank emb style");
 8169: 	    }
 8170: 	}
 8171:     }
 8172:     return $thisfn;
 8173: }
 8174: 
 8175: sub clutter_with_no_wrapper {
 8176:     my $uri = &clutter(shift);
 8177:     if ($uri =~ m-^/adm/-) {
 8178: 	$uri =~ s-^/adm/wrapper/-/-;
 8179: 	$uri =~ s-^/adm/coursedocs/showdoc/-/-;
 8180:     }
 8181:     return $uri;
 8182: }
 8183: 
 8184: sub freeze_escape {
 8185:     my ($value)=@_;
 8186:     if (ref($value)) {
 8187: 	$value=&nfreeze($value);
 8188: 	return '__FROZEN__'.&escape($value);
 8189:     }
 8190:     return &escape($value);
 8191: }
 8192: 
 8193: 
 8194: sub thaw_unescape {
 8195:     my ($value)=@_;
 8196:     if ($value =~ /^__FROZEN__/) {
 8197: 	substr($value,0,10,undef);
 8198: 	$value=&unescape($value);
 8199: 	return &thaw($value);
 8200:     }
 8201:     return &unescape($value);
 8202: }
 8203: 
 8204: sub correct_line_ends {
 8205:     my ($result)=@_;
 8206:     $$result =~s/\r\n/\n/mg;
 8207:     $$result =~s/\r/\n/mg;
 8208: }
 8209: # ================================================================ Main Program
 8210: 
 8211: sub goodbye {
 8212:    &logthis("Starting Shut down");
 8213: #not converted to using infrastruture and probably shouldn't be
 8214:    &logthis(sprintf("%-20s is %s",'%badServerCache',length(&nfreeze(\%badServerCache))));
 8215: #converted
 8216: #   &logthis(sprintf("%-20s is %s",'%metacache',scalar(%metacache)));
 8217:    &logthis(sprintf("%-20s is %s",'%homecache',length(&nfreeze(\%homecache))));
 8218: #   &logthis(sprintf("%-20s is %s",'%titlecache',length(&nfreeze(\%titlecache))));
 8219: #   &logthis(sprintf("%-20s is %s",'%courseresdatacache',length(&nfreeze(\%courseresdatacache))));
 8220: #1.1 only
 8221: #   &logthis(sprintf("%-20s is %s",'%userresdatacache',length(&nfreeze(\%userresdatacache))));
 8222: #   &logthis(sprintf("%-20s is %s",'%getsectioncache',length(&nfreeze(\%getsectioncache))));
 8223: #   &logthis(sprintf("%-20s is %s",'%courseresversioncache',length(&nfreeze(\%courseresversioncache))));
 8224: #   &logthis(sprintf("%-20s is %s",'%resversioncache',length(&nfreeze(\%resversioncache))));
 8225:    &logthis(sprintf("%-20s is %s",'%remembered',length(&nfreeze(\%remembered))));
 8226:    &logthis(sprintf("%-20s is %s",'kicks',$kicks));
 8227:    &logthis(sprintf("%-20s is %s",'hits',$hits));
 8228:    &flushcourselogs();
 8229:    &logthis("Shutting down");
 8230: }
 8231: 
 8232: sub get_dns {
 8233:     my ($url,$func,$ignore_cache) = @_;
 8234:     if (!$ignore_cache) {
 8235: 	my ($content,$cached)=
 8236: 	    &Apache::lonnet::is_cached_new('dns',$url);
 8237: 	if ($cached) {
 8238: 	    &$func($content);
 8239: 	    return;
 8240: 	}
 8241:     }
 8242: 
 8243:     my %alldns;
 8244:     open(my $config,"<$perlvar{'lonTabDir'}/hosts.tab");
 8245:     foreach my $dns (<$config>) {
 8246: 	next if ($dns !~ /^\^(\S*)/x);
 8247: 	$alldns{$1} = 1;
 8248:     }
 8249:     while (%alldns) {
 8250: 	my ($dns) = keys(%alldns);
 8251: 	delete($alldns{$dns});
 8252: 	my $ua=new LWP::UserAgent;
 8253: 	my $request=new HTTP::Request('GET',"http://$dns$url");
 8254: 	my $response=$ua->request($request);
 8255: 	next if ($response->is_error());
 8256: 	my @content = split("\n",$response->content);
 8257: 	&Apache::lonnet::do_cache_new('dns',$url,\@content,30*24*60*60);
 8258: 	&$func(\@content);
 8259: 	return;
 8260:     }
 8261:     close($config);
 8262:     my $which = (split('/',$url))[3];
 8263:     &logthis("unable to contact DNS defaulting to on disk file dns_$which.tab\n");
 8264:     open($config,"<$perlvar{'lonTabDir'}/dns_$which.tab");
 8265:     my @content = <$config>;
 8266:     &$func(\@content);
 8267:     return;
 8268: }
 8269: # ------------------------------------------------------------ Read domain file
 8270: {
 8271:     my $loaded;
 8272:     my %domain;
 8273: 
 8274:     sub parse_domain_tab {
 8275: 	my ($lines) = @_;
 8276: 	foreach my $line (@$lines) {
 8277: 	    next if ($line =~ /^(\#|\s*$ )/x);
 8278: 
 8279: 	    chomp($line);
 8280: 	    my ($name,@elements) = split(/:/,$line,9);
 8281: 	    my %this_domain;
 8282: 	    foreach my $field ('description', 'auth_def', 'auth_arg_def',
 8283: 			       'lang_def', 'city', 'longi', 'lati',
 8284: 			       'primary') {
 8285: 		$this_domain{$field} = shift(@elements);
 8286: 	    }
 8287: 	    $domain{$name} = \%this_domain;
 8288: 	}
 8289:     }
 8290: 
 8291:     sub reset_domain_info {
 8292: 	undef($loaded);
 8293: 	undef(%domain);
 8294:     }
 8295: 
 8296:     sub load_domain_tab {
 8297: 	my ($ignore_cache) = @_;
 8298: 	&get_dns('/adm/dns/domain',\&parse_domain_tab,$ignore_cache);
 8299: 	my $fh;
 8300: 	if (open($fh,"<".$perlvar{'lonTabDir'}.'/domain.tab')) {
 8301: 	    my @lines = <$fh>;
 8302: 	    &parse_domain_tab(\@lines);
 8303: 	}
 8304: 	close($fh);
 8305: 	$loaded = 1;
 8306:     }
 8307: 
 8308:     sub domain {
 8309: 	&load_domain_tab() if (!$loaded);
 8310: 
 8311: 	my ($name,$what) = @_;
 8312: 	return if ( !exists($domain{$name}) );
 8313: 
 8314: 	if (!$what) {
 8315: 	    return $domain{$name}{'description'};
 8316: 	}
 8317: 	return $domain{$name}{$what};
 8318:     }
 8319: }
 8320: 
 8321: 
 8322: # ------------------------------------------------------------- Read hosts file
 8323: {
 8324:     my %hostname;
 8325:     my %hostdom;
 8326:     my %libserv;
 8327:     my $loaded;
 8328:     my %name_to_host;
 8329: 
 8330:     sub parse_hosts_tab {
 8331: 	my ($file) = @_;
 8332: 	foreach my $configline (@$file) {
 8333: 	    next if ($configline =~ /^(\#|\s*$ )/x);
 8334: 	    next if ($configline =~ /^\^/);
 8335: 	    chomp($configline);
 8336: 	    my ($id,$domain,$role,$name)=split(/:/,$configline);
 8337: 	    $name=~s/\s//g;
 8338: 	    if ($id && $domain && $role && $name) {
 8339: 		$hostname{$id}=$name;
 8340: 		push(@{$name_to_host{$name}}, $id);
 8341: 		$hostdom{$id}=$domain;
 8342: 		if ($role eq 'library') { $libserv{$id}=$name; }
 8343: 	    }
 8344: 	}
 8345:     }
 8346:     
 8347:     sub reset_hosts_info {
 8348: 	&purge_remembered();
 8349: 	&reset_domain_info();
 8350: 	&reset_hosts_ip_info();
 8351: 	undef(%name_to_host);
 8352: 	undef(%hostname);
 8353: 	undef(%hostdom);
 8354: 	undef(%libserv);
 8355: 	undef($loaded);
 8356:     }
 8357: 
 8358:     sub load_hosts_tab {
 8359: 	my ($ignore_cache) = @_;
 8360: 	&get_dns('/adm/dns/hosts',\&parse_hosts_tab,$ignore_cache);
 8361: 	open(my $config,"<$perlvar{'lonTabDir'}/hosts.tab");
 8362: 	my @config = <$config>;
 8363: 	&parse_hosts_tab(\@config);
 8364: 	close($config);
 8365: 	$loaded=1;
 8366:     }
 8367: 
 8368:     sub hostname {
 8369: 	&load_hosts_tab() if (!$loaded);
 8370: 
 8371: 	my ($lonid) = @_;
 8372: 	return $hostname{$lonid};
 8373:     }
 8374: 
 8375:     sub all_hostnames {
 8376: 	&load_hosts_tab() if (!$loaded);
 8377: 
 8378: 	return %hostname;
 8379:     }
 8380: 
 8381:     sub all_names {
 8382: 	&load_hosts_tab() if (!$loaded);
 8383: 
 8384: 	return %name_to_host;
 8385:     }
 8386: 
 8387:     sub is_library {
 8388: 	&load_hosts_tab() if (!$loaded);
 8389: 
 8390: 	return exists($libserv{$_[0]});
 8391:     }
 8392: 
 8393:     sub all_library {
 8394: 	&load_hosts_tab() if (!$loaded);
 8395: 
 8396: 	return %libserv;
 8397:     }
 8398: 
 8399:     sub get_servers {
 8400: 	&load_hosts_tab() if (!$loaded);
 8401: 
 8402: 	my ($domain,$type) = @_;
 8403: 	my %possible_hosts = ($type eq 'library') ? %libserv
 8404: 	                                          : %hostname;
 8405: 	my %result;
 8406: 	if (ref($domain) eq 'ARRAY') {
 8407: 	    while ( my ($host,$hostname) = each(%possible_hosts)) {
 8408: 		if (grep(/^\Q$hostdom{$host}\E$/,@$domain)) {
 8409: 		    $result{$host} = $hostname;
 8410: 		}
 8411: 	    }
 8412: 	} else {
 8413: 	    while ( my ($host,$hostname) = each(%possible_hosts)) {
 8414: 		if ($hostdom{$host} eq $domain) {
 8415: 		    $result{$host} = $hostname;
 8416: 		}
 8417: 	    }
 8418: 	}
 8419: 	return %result;
 8420:     }
 8421: 
 8422:     sub host_domain {
 8423: 	&load_hosts_tab() if (!$loaded);
 8424: 
 8425: 	my ($lonid) = @_;
 8426: 	return $hostdom{$lonid};
 8427:     }
 8428: 
 8429:     sub all_domains {
 8430: 	&load_hosts_tab() if (!$loaded);
 8431: 
 8432: 	my %seen;
 8433: 	my @uniq = grep(!$seen{$_}++, values(%hostdom));
 8434: 	return @uniq;
 8435:     }
 8436: }
 8437: 
 8438: { 
 8439:     my %iphost;
 8440:     my %name_to_ip;
 8441:     my %lonid_to_ip;
 8442: 
 8443:     sub get_hosts_from_ip {
 8444: 	my ($ip) = @_;
 8445: 	my %iphosts = &get_iphost();
 8446: 	if (ref($iphosts{$ip})) {
 8447: 	    return @{$iphosts{$ip}};
 8448: 	}
 8449: 	return;
 8450:     }
 8451:     
 8452:     sub reset_hosts_ip_info {
 8453: 	undef(%iphost);
 8454: 	undef(%name_to_ip);
 8455: 	undef(%lonid_to_ip);
 8456:     }
 8457: 
 8458:     sub get_host_ip {
 8459: 	my ($lonid) = @_;
 8460: 	if (exists($lonid_to_ip{$lonid})) {
 8461: 	    return $lonid_to_ip{$lonid};
 8462: 	}
 8463: 	my $name=&hostname($lonid);
 8464:    	my $ip = gethostbyname($name);
 8465: 	return if (!$ip || length($ip) ne 4);
 8466: 	$ip=inet_ntoa($ip);
 8467: 	$name_to_ip{$name}   = $ip;
 8468: 	$lonid_to_ip{$lonid} = $ip;
 8469: 	return $ip;
 8470:     }
 8471:     
 8472:     sub get_iphost {
 8473: 	my ($ignore_cache) = @_;
 8474: 
 8475: 	if (!$ignore_cache) {
 8476: 	    if (%iphost) {
 8477: 		return %iphost;
 8478: 	    }
 8479: 	    my ($ip_info,$cached)=
 8480: 		&Apache::lonnet::is_cached_new('iphost','iphost');
 8481: 	    if ($cached) {
 8482: 		%iphost      = %{$ip_info->[0]};
 8483: 		%name_to_ip  = %{$ip_info->[1]};
 8484: 		%lonid_to_ip = %{$ip_info->[2]};
 8485: 		return %iphost;
 8486: 	    }
 8487: 	}
 8488: 
 8489: 	# get yesterday's info for fallback
 8490: 	my %old_name_to_ip;
 8491: 	my ($ip_info,$cached)=
 8492: 	    &Apache::lonnet::is_cached_new('iphost','iphost');
 8493: 	if ($cached) {
 8494: 	    %old_name_to_ip = %{$ip_info->[1]};
 8495: 	}
 8496: 
 8497: 	my %name_to_host = &all_names();
 8498: 	foreach my $name (keys(%name_to_host)) {
 8499: 	    my $ip;
 8500: 	    if (!exists($name_to_ip{$name})) {
 8501: 		$ip = gethostbyname($name);
 8502: 		if (!$ip || length($ip) ne 4) {
 8503: 		    if (defined($old_name_to_ip{$name})) {
 8504: 			$ip = $old_name_to_ip{$name};
 8505: 			&logthis("Can't find $name defaulting to old $ip");
 8506: 		    } else {
 8507: 			&logthis("Name $name no IP found");
 8508: 			next;
 8509: 		    }
 8510: 		} else {
 8511: 		    $ip=inet_ntoa($ip);
 8512: 		}
 8513: 		$name_to_ip{$name} = $ip;
 8514: 	    } else {
 8515: 		$ip = $name_to_ip{$name};
 8516: 	    }
 8517: 	    foreach my $id (@{ $name_to_host{$name} }) {
 8518: 		$lonid_to_ip{$id} = $ip;
 8519: 	    }
 8520: 	    push(@{$iphost{$ip}},@{$name_to_host{$name}});
 8521: 	}
 8522: 	&Apache::lonnet::do_cache_new('iphost','iphost',
 8523: 				      [\%iphost,\%name_to_ip,\%lonid_to_ip],
 8524: 				      48*60*60);
 8525: 
 8526: 	return %iphost;
 8527:     }
 8528: }
 8529: 
 8530: BEGIN {
 8531: 
 8532: # ----------------------------------- Read loncapa.conf and loncapa_apache.conf
 8533:     unless ($readit) {
 8534: {
 8535:     my $configvars = LONCAPA::Configuration::read_conf('loncapa.conf');
 8536:     %perlvar = (%perlvar,%{$configvars});
 8537: }
 8538: 
 8539: 
 8540: # ------------------------------------------------------ Read spare server file
 8541: {
 8542:     open(my $config,"<$perlvar{'lonTabDir'}/spare.tab");
 8543: 
 8544:     while (my $configline=<$config>) {
 8545:        chomp($configline);
 8546:        if ($configline) {
 8547: 	   my ($host,$type) = split(':',$configline,2);
 8548: 	   if (!defined($type) || $type eq '') { $type = 'default' };
 8549: 	   push(@{ $spareid{$type} }, $host);
 8550:        }
 8551:     }
 8552:     close($config);
 8553: }
 8554: # ------------------------------------------------------------ Read permissions
 8555: {
 8556:     open(my $config,"<$perlvar{'lonTabDir'}/roles.tab");
 8557: 
 8558:     while (my $configline=<$config>) {
 8559: 	chomp($configline);
 8560: 	if ($configline) {
 8561: 	    my ($role,$perm)=split(/ /,$configline);
 8562: 	    if ($perm ne '') { $pr{$role}=$perm; }
 8563: 	}
 8564:     }
 8565:     close($config);
 8566: }
 8567: 
 8568: # -------------------------------------------- Read plain texts for permissions
 8569: {
 8570:     open(my $config,"<$perlvar{'lonTabDir'}/rolesplain.tab");
 8571: 
 8572:     while (my $configline=<$config>) {
 8573: 	chomp($configline);
 8574: 	if ($configline) {
 8575: 	    my ($short,@plain)=split(/:/,$configline);
 8576:             %{$prp{$short}} = ();
 8577: 	    if (@plain > 0) {
 8578:                 $prp{$short}{'std'} = $plain[0];
 8579:                 for (my $i=1; $i<@plain; $i++) {
 8580:                     $prp{$short}{'alt'.$i} = $plain[$i];  
 8581:                 }
 8582:             }
 8583: 	}
 8584:     }
 8585:     close($config);
 8586: }
 8587: 
 8588: # ---------------------------------------------------------- Read package table
 8589: {
 8590:     open(my $config,"<$perlvar{'lonTabDir'}/packages.tab");
 8591: 
 8592:     while (my $configline=<$config>) {
 8593: 	if ($configline !~ /\S/ || $configline=~/^#/) { next; }
 8594: 	chomp($configline);
 8595: 	my ($short,$plain)=split(/:/,$configline);
 8596: 	my ($pack,$name)=split(/\&/,$short);
 8597: 	if ($plain ne '') {
 8598: 	    $packagetab{$pack.'&'.$name.'&name'}=$name; 
 8599: 	    $packagetab{$short}=$plain; 
 8600: 	}
 8601:     }
 8602:     close($config);
 8603: }
 8604: 
 8605: # ------------- set up temporary directory
 8606: {
 8607:     $tmpdir = $perlvar{'lonDaemons'}.'/tmp/';
 8608: 
 8609: }
 8610: 
 8611: $memcache=new Cache::Memcached({'servers'           => ['127.0.0.1:11211'],
 8612: 				'compress_threshold'=> 20_000,
 8613:  			        });
 8614: 
 8615: $processmarker='_'.time.'_'.$perlvar{'lonHostID'};
 8616: $dumpcount=0;
 8617: 
 8618: &logtouch();
 8619: &logthis('<font color="yellow">INFO: Read configuration</font>');
 8620: $readit=1;
 8621:     {
 8622: 	use integer;
 8623: 	my $test=(2**32)+1;
 8624: 	if ($test != 0) { $_64bit=1; } else { $_64bit=0; }
 8625: 	&logthis(" Detected 64bit platform ($_64bit)");
 8626:     }
 8627: }
 8628: }
 8629: 
 8630: 1;
 8631: __END__
 8632: 
 8633: =pod
 8634: 
 8635: =head1 NAME
 8636: 
 8637: Apache::lonnet - Subroutines to ask questions about things in the network.
 8638: 
 8639: =head1 SYNOPSIS
 8640: 
 8641: Invoked by other LON-CAPA modules, when they need to talk to or about objects in the network.
 8642: 
 8643:  &Apache::lonnet::SUBROUTINENAME(ARGUMENTS);
 8644: 
 8645: Common parameters:
 8646: 
 8647: =over 4
 8648: 
 8649: =item *
 8650: 
 8651: $uname : an internal username (if $cname expecting a course Id specifically)
 8652: 
 8653: =item *
 8654: 
 8655: $udom : a domain (if $cdom expecting a course's domain specifically)
 8656: 
 8657: =item *
 8658: 
 8659: $symb : a resource instance identifier
 8660: 
 8661: =item *
 8662: 
 8663: $namespace : the name of a .db file that contains the data needed or
 8664: being set.
 8665: 
 8666: =back
 8667: 
 8668: =head1 OVERVIEW
 8669: 
 8670: lonnet provides subroutines which interact with the
 8671: lonc/lond (TCP) network layer of LON-CAPA. They can be used to ask
 8672: about classes, users, and resources.
 8673: 
 8674: For many of these objects you can also use this to store data about
 8675: them or modify them in various ways.
 8676: 
 8677: =head2 Symbs
 8678: 
 8679: To identify a specific instance of a resource, LON-CAPA uses symbols
 8680: or "symbs"X<symb>. These identifiers are built from the URL of the
 8681: map, the resource number of the resource in the map, and the URL of
 8682: the resource itself. The latter is somewhat redundant, but might help
 8683: if maps change.
 8684: 
 8685: An example is
 8686: 
 8687:  msu/korte/parts/part1.sequence___19___msu/korte/tests/part12.problem
 8688: 
 8689: The respective map entry is
 8690: 
 8691:  <resource id="19" src="/res/msu/korte/tests/part12.problem"
 8692:   title="Problem 2">
 8693:  </resource>
 8694: 
 8695: Symbs are used by the random number generator, as well as to store and
 8696: restore data specific to a certain instance of for example a problem.
 8697: 
 8698: =head2 Storing And Retrieving Data
 8699: 
 8700: X<store()>X<cstore()>X<restore()>Three of the most important functions
 8701: in C<lonnet.pm> are C<&Apache::lonnet::cstore()>,
 8702: C<&Apache::lonnet:restore()>, and C<&Apache::lonnet::store()>, which
 8703: is is the non-critical message twin of cstore. These functions are for
 8704: handlers to store a perl hash to a user's permanent data space in an
 8705: easy manner, and to retrieve it again on another call. It is expected
 8706: that a handler would use this once at the beginning to retrieve data,
 8707: and then again once at the end to send only the new data back.
 8708: 
 8709: The data is stored in the user's data directory on the user's
 8710: homeserver under the ID of the course.
 8711: 
 8712: The hash that is returned by restore will have all of the previous
 8713: value for all of the elements of the hash.
 8714: 
 8715: Example:
 8716: 
 8717:  #creating a hash
 8718:  my %hash;
 8719:  $hash{'foo'}='bar';
 8720: 
 8721:  #storing it
 8722:  &Apache::lonnet::cstore(\%hash);
 8723: 
 8724:  #changing a value
 8725:  $hash{'foo'}='notbar';
 8726: 
 8727:  #adding a new value
 8728:  $hash{'bar'}='foo';
 8729:  &Apache::lonnet::cstore(\%hash);
 8730: 
 8731:  #retrieving the hash
 8732:  my %history=&Apache::lonnet::restore();
 8733: 
 8734:  #print the hash
 8735:  foreach my $key (sort(keys(%history))) {
 8736:    print("\%history{$key} = $history{$key}");
 8737:  }
 8738: 
 8739: Will print out:
 8740: 
 8741:  %history{1:foo} = bar
 8742:  %history{1:keys} = foo:timestamp
 8743:  %history{1:timestamp} = 990455579
 8744:  %history{2:bar} = foo
 8745:  %history{2:foo} = notbar
 8746:  %history{2:keys} = foo:bar:timestamp
 8747:  %history{2:timestamp} = 990455580
 8748:  %history{bar} = foo
 8749:  %history{foo} = notbar
 8750:  %history{timestamp} = 990455580
 8751:  %history{version} = 2
 8752: 
 8753: Note that the special hash entries C<keys>, C<version> and
 8754: C<timestamp> were added to the hash. C<version> will be equal to the
 8755: total number of versions of the data that have been stored. The
 8756: C<timestamp> attribute will be the UNIX time the hash was
 8757: stored. C<keys> is available in every historical section to list which
 8758: keys were added or changed at a specific historical revision of a
 8759: hash.
 8760: 
 8761: B<Warning>: do not store the hash that restore returns directly. This
 8762: will cause a mess since it will restore the historical keys as if the
 8763: were new keys. I.E. 1:foo will become 1:1:foo etc.
 8764: 
 8765: Calling convention:
 8766: 
 8767:  my %record=&Apache::lonnet::restore($symb,$courseid,$domain,$uname,$home);
 8768:  &Apache::lonnet::cstore(\%newrecord,$symb,$courseid,$domain,$uname,$home);
 8769: 
 8770: For more detailed information, see lonnet specific documentation.
 8771: 
 8772: =head1 RETURN MESSAGES
 8773: 
 8774: =over 4
 8775: 
 8776: =item * B<con_lost>: unable to contact remote host
 8777: 
 8778: =item * B<con_delayed>: unable to contact remote host, message will be delivered
 8779: when the connection is brought back up
 8780: 
 8781: =item * B<con_failed>: unable to contact remote host and unable to save message
 8782: for later delivery
 8783: 
 8784: =item * B<error:>: an error a occured, a description of the error follows the :
 8785: 
 8786: =item * B<no_such_host>: unable to fund a host associated with the user/domain
 8787: that was requested
 8788: 
 8789: =back
 8790: 
 8791: =head1 PUBLIC SUBROUTINES
 8792: 
 8793: =head2 Session Environment Functions
 8794: 
 8795: =over 4
 8796: 
 8797: =item * 
 8798: X<appenv()>
 8799: B<appenv(%hash)>: the value of %hash is written to
 8800: the user envirnoment file, and will be restored for each access this
 8801: user makes during this session, also modifies the %env for the current
 8802: process
 8803: 
 8804: =item *
 8805: X<delenv()>
 8806: B<delenv($regexp)>: removes all items from the session
 8807: environment file that matches the regular expression in $regexp. The
 8808: values are also delted from the current processes %env.
 8809: 
 8810: =item * get_env_multiple($name) 
 8811: 
 8812: gets $name from the %env hash, it seemlessly handles the cases where multiple
 8813: values may be defined and end up as an array ref.
 8814: 
 8815: returns an array of values
 8816: 
 8817: =back
 8818: 
 8819: =head2 User Information
 8820: 
 8821: =over 4
 8822: 
 8823: =item *
 8824: X<queryauthenticate()>
 8825: B<queryauthenticate($uname,$udom)>: try to determine user's current 
 8826: authentication scheme
 8827: 
 8828: =item *
 8829: X<authenticate()>
 8830: B<authenticate($uname,$upass,$udom)>: try to
 8831: authenticate user from domain's lib servers (first use the current
 8832: one). C<$upass> should be the users password.
 8833: 
 8834: =item *
 8835: X<homeserver()>
 8836: B<homeserver($uname,$udom)>: find the server which has
 8837: the user's directory and files (there must be only one), this caches
 8838: the answer, and also caches if there is a borken connection.
 8839: 
 8840: =item *
 8841: X<idget()>
 8842: B<idget($udom,@ids)>: find the usernames behind a list of IDs
 8843: (IDs are a unique resource in a domain, there must be only 1 ID per
 8844: username, and only 1 username per ID in a specific domain) (returns
 8845: hash: id=>name,id=>name)
 8846: 
 8847: =item *
 8848: X<idrget()>
 8849: B<idrget($udom,@unames)>: find the IDs behind a list of
 8850: usernames (returns hash: name=>id,name=>id)
 8851: 
 8852: =item *
 8853: X<idput()>
 8854: B<idput($udom,%ids)>: store away a list of names and associated IDs
 8855: 
 8856: =item *
 8857: X<rolesinit()>
 8858: B<rolesinit($udom,$username,$authhost)>: get user privileges
 8859: 
 8860: =item *
 8861: X<getsection()>
 8862: B<getsection($udom,$uname,$cname)>: finds the section of student in the
 8863: course $cname, return section name/number or '' for "not in course"
 8864: and '-1' for "no section"
 8865: 
 8866: =item *
 8867: X<userenvironment()>
 8868: B<userenvironment($udom,$uname,@what)>: gets the values of the keys
 8869: passed in @what from the requested user's environment, returns a hash
 8870: 
 8871: =item * 
 8872: X<userlog_query()>
 8873: B<userlog_query($uname,$udom,%filters)>: retrieves data from a user's
 8874: activity.log file. %filters defines filters applied when parsing the
 8875: log file. These can be start or end timestamps, or the type of action
 8876: - log to look for Login or Logout events, check for Checkin or
 8877: Checkout, role for role selection. The response is in the form
 8878: timestamp1:hostid1:event1&timestamp2:hostid2:event2 where events are
 8879: escaped strings of the action recorded in the activity.log file.
 8880: 
 8881: =back
 8882: 
 8883: =head2 User Roles
 8884: 
 8885: =over 4
 8886: 
 8887: =item *
 8888: 
 8889: allowed($priv,$uri,$symb,$role) : check for a user privilege; returns codes for allowed actions
 8890:  F: full access
 8891:  U,I,K: authentication modes (cxx only)
 8892:  '': forbidden
 8893:  1: user needs to choose course
 8894:  2: browse allowed
 8895:  A: passphrase authentication needed
 8896: 
 8897: =item *
 8898: 
 8899: definerole($rolename,$sysrole,$domrole,$courole) : define role; define a custom
 8900: role rolename set privileges in format of lonTabs/roles.tab for system, domain,
 8901: and course level
 8902: 
 8903: =item *
 8904: 
 8905: plaintext($short) : return value in %prp hash (rolesplain.tab); plain text
 8906: explanation of a user role term
 8907: 
 8908: =item *
 8909: 
 8910: get_my_roles($uname,$udom,$context,$types,$roles,$roledoms,$withsec) :
 8911: All arguments are optional. Returns a hash of a roles, either for
 8912: co-author/assistant author roles for a user's Construction Space
 8913: (default), or if $context is 'userroles', roles for the user himself,
 8914: In the hash, keys are set to colon-separated $uname,$udom,$role, and
 8915: (optionally) if $withsec is true, a fourth colon-separated item - $section.
 8916: For each key, value is set to colon-separated start and end times for
 8917: the role.  If no username and domain are specified, will default to
 8918: current user/domain. Types, roles, and roledoms are references to arrays
 8919: of role statuses (active, future or previous), roles 
 8920: (e.g., cc,in, st etc.) and domains of the roles which can be used
 8921: to restrict the list of roles reported. If no array ref is 
 8922: provided for types, will default to return only active roles.
 8923: 
 8924: =back
 8925: 
 8926: =head2 User Modification
 8927: 
 8928: =over 4
 8929: 
 8930: =item *
 8931: 
 8932: assignrole($udom,$uname,$url,$role,$end,$start) : assign role; give a role to a
 8933: user for the level given by URL.  Optional start and end dates (leave empty
 8934: string or zero for "no date")
 8935: 
 8936: =item *
 8937: 
 8938: changepass($uname,$udom,$currentpass,$newpass,$server) : attempts to
 8939: change a users, password, possible return values are: ok,
 8940: pwchange_failure, non_authorized, auth_mode_error, unknown_user,
 8941: refused
 8942: 
 8943: =item *
 8944: 
 8945: modifyuserauth($udom,$uname,$umode,$upass) : modify user authentication
 8946: 
 8947: =item *
 8948: 
 8949: modifyuser($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene) : 
 8950: modify user
 8951: 
 8952: =item *
 8953: 
 8954: modifystudent
 8955: 
 8956: modify a students enrollment and identification information.
 8957: The course id is resolved based on the current users environment.  
 8958: This means the envoking user must be a course coordinator or otherwise
 8959: associated with a course.
 8960: 
 8961: This call is essentially a wrapper for lonnet::modifyuser and
 8962: lonnet::modify_student_enrollment
 8963: 
 8964: Inputs: 
 8965: 
 8966: =over 4
 8967: 
 8968: =item B<$udom> Students loncapa domain
 8969: 
 8970: =item B<$uname> Students loncapa login name
 8971: 
 8972: =item B<$uid> Students id/student number
 8973: 
 8974: =item B<$umode> Students authentication mode
 8975: 
 8976: =item B<$upass> Students password
 8977: 
 8978: =item B<$first> Students first name
 8979: 
 8980: =item B<$middle> Students middle name
 8981: 
 8982: =item B<$last> Students last name
 8983: 
 8984: =item B<$gene> Students generation
 8985: 
 8986: =item B<$usec> Students section in course
 8987: 
 8988: =item B<$end> Unix time of the roles expiration
 8989: 
 8990: =item B<$start> Unix time of the roles start date
 8991: 
 8992: =item B<$forceid> If defined, allow $uid to be changed
 8993: 
 8994: =item B<$desiredhome> server to use as home server for student
 8995: 
 8996: =back
 8997: 
 8998: =item *
 8999: 
 9000: modify_student_enrollment
 9001: 
 9002: Change a students enrollment status in a class.  The environment variable
 9003: 'role.request.course' must be defined for this function to proceed.
 9004: 
 9005: Inputs:
 9006: 
 9007: =over 4
 9008: 
 9009: =item $udom, students domain
 9010: 
 9011: =item $uname, students name
 9012: 
 9013: =item $uid, students user id
 9014: 
 9015: =item $first, students first name
 9016: 
 9017: =item $middle
 9018: 
 9019: =item $last
 9020: 
 9021: =item $gene
 9022: 
 9023: =item $usec
 9024: 
 9025: =item $end
 9026: 
 9027: =item $start
 9028: 
 9029: =back
 9030: 
 9031: 
 9032: =item *
 9033: 
 9034: assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start) : assign
 9035: custom role; give a custom role to a user for the level given by URL.  Specify
 9036: name and domain of role author, and role name
 9037: 
 9038: =item *
 9039: 
 9040: revokerole($udom,$uname,$url,$role) : revoke a role for url
 9041: 
 9042: =item *
 9043: 
 9044: revokecustomrole($udom,$uname,$url,$role) : revoke a custom role
 9045: 
 9046: =back
 9047: 
 9048: =head2 Course Infomation
 9049: 
 9050: =over 4
 9051: 
 9052: =item *
 9053: 
 9054: coursedescription($courseid) : returns a hash of information about the
 9055: specified course id, including all environment settings for the
 9056: course, the description of the course will be in the hash under the
 9057: key 'description'
 9058: 
 9059: =item *
 9060: 
 9061: resdata($name,$domain,$type,@which) : request for current parameter
 9062: setting for a specific $type, where $type is either 'course' or 'user',
 9063: @what should be a list of parameters to ask about. This routine caches
 9064: answers for 5 minutes.
 9065: 
 9066: =item *
 9067: 
 9068: get_courseresdata($courseid, $domain) : dump the entire course resource
 9069: data base, returning a hash that is keyed by the resource name and has
 9070: values that are the resource value.  I believe that the timestamps and
 9071: versions are also returned.
 9072: 
 9073: 
 9074: =back
 9075: 
 9076: =head2 Course Modification
 9077: 
 9078: =over 4
 9079: 
 9080: =item *
 9081: 
 9082: writecoursepref($courseid,%prefs) : write preferences (environment
 9083: database) for a course
 9084: 
 9085: =item *
 9086: 
 9087: createcourse($udom,$description,$url) : make/modify course
 9088: 
 9089: =back
 9090: 
 9091: =head2 Resource Subroutines
 9092: 
 9093: =over 4
 9094: 
 9095: =item *
 9096: 
 9097: subscribe($fname) : subscribe to a resource, returns URL if possible (probably should use repcopy instead)
 9098: 
 9099: =item *
 9100: 
 9101: repcopy($filename) : subscribes to the requested file, and attempts to
 9102: replicate from the owning library server, Might return
 9103: 'unavailable', 'not_found', 'forbidden', 'ok', or
 9104: 'bad_request', also attempts to grab the metadata for the
 9105: resource. Expects the local filesystem pathname
 9106: (/home/httpd/html/res/....)
 9107: 
 9108: =back
 9109: 
 9110: =head2 Resource Information
 9111: 
 9112: =over 4
 9113: 
 9114: =item *
 9115: 
 9116: EXT($varname,$symb,$udom,$uname) : evaluates and returns the value of
 9117: a vairety of different possible values, $varname should be a request
 9118: string, and the other parameters can be used to specify who and what
 9119: one is asking about.
 9120: 
 9121: Possible values for $varname are environment.lastname (or other item
 9122: from the envirnment hash), user.name (or someother aspect about the
 9123: user), resource.0.maxtries (or some other part and parameter of a
 9124: resource)
 9125: 
 9126: =item *
 9127: 
 9128: directcondval($number) : get current value of a condition; reads from a state
 9129: string
 9130: 
 9131: =item *
 9132: 
 9133: condval($condidx) : value of condition index based on state
 9134: 
 9135: =item *
 9136: 
 9137: metadata($uri,$what,$liburi,$prefix,$depthcount) : request a
 9138: resource's metadata, $what should be either a specific key, or either
 9139: 'keys' (to get a list of possible keys) or 'packages' to get a list of
 9140: packages that this resource currently uses, the last 3 arguments are only used internally for recursive metadata.
 9141: 
 9142: this function automatically caches all requests
 9143: 
 9144: =item *
 9145: 
 9146: metadata_query($query,$custom,$customshow) : make a metadata query against the
 9147: network of library servers; returns file handle of where SQL and regex results
 9148: will be stored for query
 9149: 
 9150: =item *
 9151: 
 9152: symbread($filename) : return symbolic list entry (filename argument optional);
 9153: returns the data handle
 9154: 
 9155: =item *
 9156: 
 9157: symbverify($symb,$thisfn) : verifies that $symb actually exists and is
 9158: a possible symb for the URL in $thisfn, and if is an encryypted
 9159: resource that the user accessed using /enc/ returns a 1 on success, 0
 9160: on failure, user must be in a course, as it assumes the existance of
 9161: the course initial hash, and uses $env('request.course.id'}
 9162: 
 9163: 
 9164: =item *
 9165: 
 9166: symbclean($symb) : removes versions numbers from a symb, returns the
 9167: cleaned symb
 9168: 
 9169: =item *
 9170: 
 9171: is_on_map($uri) : checks if the $uri is somewhere on the current
 9172: course map, user must be in a course for it to work.
 9173: 
 9174: =item *
 9175: 
 9176: numval($salt) : return random seed value (addend for rndseed)
 9177: 
 9178: =item *
 9179: 
 9180: rndseed($symb,$courseid,$udom,$uname) : create a random sum; returns
 9181: a random seed, all arguments are optional, if they aren't sent it uses the
 9182: environment to derive them. Note: if symb isn't sent and it can't get one
 9183: from &symbread it will use the current time as its return value
 9184: 
 9185: =item *
 9186: 
 9187: ireceipt($funame,$fudom,$fucourseid,$fusymb) : return unique,
 9188: unfakeable, receipt
 9189: 
 9190: =item *
 9191: 
 9192: receipt() : API to ireceipt working off of env values; given out to users
 9193: 
 9194: =item *
 9195: 
 9196: countacc($url) : count the number of accesses to a given URL
 9197: 
 9198: =item *
 9199: 
 9200: 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
 9201: 
 9202: =item *
 9203: 
 9204: 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)
 9205: 
 9206: =item *
 9207: 
 9208: expirespread($uname,$udom,$stype,$usymb) : set expire date for spreadsheet
 9209: 
 9210: =item *
 9211: 
 9212: devalidate($symb) : devalidate temporary spreadsheet calculations,
 9213: forcing spreadsheet to reevaluate the resource scores next time.
 9214: 
 9215: =back
 9216: 
 9217: =head2 Storing/Retreiving Data
 9218: 
 9219: =over 4
 9220: 
 9221: =item *
 9222: 
 9223: store($storehash,$symb,$namespace,$udom,$uname) : stores hash permanently
 9224: for this url; hashref needs to be given and should be a \%hashname; the
 9225: remaining args aren't required and if they aren't passed or are '' they will
 9226: be derived from the env
 9227: 
 9228: =item *
 9229: 
 9230: cstore($storehash,$symb,$namespace,$udom,$uname) : same as store but
 9231: uses critical subroutine
 9232: 
 9233: =item *
 9234: 
 9235: restore($symb,$namespace,$udom,$uname) : returns hash for this symb;
 9236: all args are optional
 9237: 
 9238: =item *
 9239: 
 9240: dumpstore($namespace,$udom,$uname,$regexp,$range) : 
 9241: dumps the complete (or key matching regexp) namespace into a hash
 9242: ($udom, $uname, $regexp, $range are optional) for a namespace that is
 9243: normally &store()ed into
 9244: 
 9245: $range should be either an integer '100' (give me the first 100
 9246:                                            matching records)
 9247:               or be  two integers sperated by a - with no spaces
 9248:                  '30-50' (give me the 30th through the 50th matching
 9249:                           records)
 9250: 
 9251: 
 9252: =item *
 9253: 
 9254: putstore($namespace,$symb,$version,$storehash,$udomain,$uname) :
 9255: replaces a &store() version of data with a replacement set of data
 9256: for a particular resource in a namespace passed in the $storehash hash 
 9257: reference
 9258: 
 9259: =item *
 9260: 
 9261: tmpstore($storehash,$symb,$namespace,$udom,$uname) : storage that
 9262: works very similar to store/cstore, but all data is stored in a
 9263: temporary location and can be reset using tmpreset, $storehash should
 9264: be a hash reference, returns nothing on success
 9265: 
 9266: =item *
 9267: 
 9268: tmprestore($symb,$namespace,$udom,$uname) : storage that works very
 9269: similar to restore, but all data is stored in a temporary location and
 9270: can be reset using tmpreset. Returns a hash of values on success,
 9271: error string otherwise.
 9272: 
 9273: =item *
 9274: 
 9275: tmpreset($symb,$namespace,$udom,$uname) : temporary storage reset,
 9276: deltes all keys for $symb form the temporary storage hash.
 9277: 
 9278: =item *
 9279: 
 9280: get($namespace,$storearr,$udom,$uname) : returns hash with keys from array
 9281: reference filled in from namesp ($udom and $uname are optional)
 9282: 
 9283: =item *
 9284: 
 9285: del($namespace,$storearr,$udom,$uname) : deletes keys out of array from
 9286: namesp ($udom and $uname are optional)
 9287: 
 9288: =item *
 9289: 
 9290: dump($namespace,$udom,$uname,$regexp,$range) : 
 9291: dumps the complete (or key matching regexp) namespace into a hash
 9292: ($udom, $uname, $regexp, $range are optional)
 9293: 
 9294: $range should be either an integer '100' (give me the first 100
 9295:                                            matching records)
 9296:               or be  two integers sperated by a - with no spaces
 9297:                  '30-50' (give me the 30th through the 50th matching
 9298:                           records)
 9299: =item *
 9300: 
 9301: inc($namespace,$store,$udom,$uname) : increments $store in $namespace.
 9302: $store can be a scalar, an array reference, or if the amount to be 
 9303: incremented is > 1, a hash reference.
 9304: 
 9305: ($udom and $uname are optional)
 9306: 
 9307: =item *
 9308: 
 9309: put($namespace,$storehash,$udom,$uname) : stores hash in namesp
 9310: ($udom and $uname are optional)
 9311: 
 9312: =item *
 9313: 
 9314: cput($namespace,$storehash,$udom,$uname) : critical put
 9315: ($udom and $uname are optional)
 9316: 
 9317: =item *
 9318: 
 9319: newput($namespace,$storehash,$udom,$uname) :
 9320: 
 9321: Attempts to store the items in the $storehash, but only if they don't
 9322: currently exist, if this succeeds you can be certain that you have 
 9323: successfully created a new key value pair in the $namespace db.
 9324: 
 9325: 
 9326: Args:
 9327:  $namespace: name of database to store values to
 9328:  $storehash: hashref to store to the db
 9329:  $udom: (optional) domain of user containing the db
 9330:  $uname: (optional) name of user caontaining the db
 9331: 
 9332: Returns:
 9333:  'ok' -> succeeded in storing all keys of $storehash
 9334:  'key_exists: <key>' -> failed to anything out of $storehash, as at
 9335:                         least <key> already existed in the db (other
 9336:                         requested keys may also already exist)
 9337:  'error: <msg>' -> unable to tie the DB or other erorr occured
 9338:  'con_lost' -> unable to contact request server
 9339:  'refused' -> action was not allowed by remote machine
 9340: 
 9341: 
 9342: =item *
 9343: 
 9344: eget($namespace,$storearr,$udom,$uname) : returns hash with keys from array
 9345: reference filled in from namesp (encrypts the return communication)
 9346: ($udom and $uname are optional)
 9347: 
 9348: =item *
 9349: 
 9350: log($udom,$name,$home,$message) : write to permanent log for user; use
 9351: critical subroutine
 9352: 
 9353: =item *
 9354: 
 9355: get_dom($namespace,$storearr,$udom,$uhome) : returns hash with keys from
 9356: array reference filled in from namespace found in domain level on either
 9357: specified domain server ($uhome) or primary domain server ($udom and $uhome are optional).
 9358: 
 9359: =item *
 9360: 
 9361: put_dom($namespace,$storehash,$udom,$uhome) :  stores hash in namespace at 
 9362: domain level either on specified domain server ($uhome) or primary domain 
 9363: server ($udom and $uhome are optional)
 9364: 
 9365: =back
 9366: 
 9367: =head2 Network Status Functions
 9368: 
 9369: =over 4
 9370: 
 9371: =item *
 9372: 
 9373: dirlist($uri) : return directory list based on URI
 9374: 
 9375: =item *
 9376: 
 9377: spareserver() : find server with least workload from spare.tab
 9378: 
 9379: =back
 9380: 
 9381: =head2 Apache Request
 9382: 
 9383: =over 4
 9384: 
 9385: =item *
 9386: 
 9387: ssi($url,%hash) : server side include, does a complete request cycle on url to
 9388: localhost, posts hash
 9389: 
 9390: =back
 9391: 
 9392: =head2 Data to String to Data
 9393: 
 9394: =over 4
 9395: 
 9396: =item *
 9397: 
 9398: hash2str(%hash) : convert a hash into a string complete with escaping and '='
 9399: and '&' separators, supports elements that are arrayrefs and hashrefs
 9400: 
 9401: =item *
 9402: 
 9403: hashref2str($hashref) : convert a hashref into a string complete with
 9404: escaping and '=' and '&' separators, supports elements that are
 9405: arrayrefs and hashrefs
 9406: 
 9407: =item *
 9408: 
 9409: arrayref2str($arrayref) : convert an arrayref into a string complete
 9410: with escaping and '&' separators, supports elements that are arrayrefs
 9411: and hashrefs
 9412: 
 9413: =item *
 9414: 
 9415: str2hash($string) : convert string to hash using unescaping and
 9416: splitting on '=' and '&', supports elements that are arrayrefs and
 9417: hashrefs
 9418: 
 9419: =item *
 9420: 
 9421: str2array($string) : convert string to hash using unescaping and
 9422: splitting on '&', supports elements that are arrayrefs and hashrefs
 9423: 
 9424: =back
 9425: 
 9426: =head2 Logging Routines
 9427: 
 9428: =over 4
 9429: 
 9430: These routines allow one to make log messages in the lonnet.log and
 9431: lonnet.perm logfiles.
 9432: 
 9433: =item *
 9434: 
 9435: logtouch() : make sure the logfile, lonnet.log, exists
 9436: 
 9437: =item *
 9438: 
 9439: logthis() : append message to the normal lonnet.log file, it gets
 9440: preiodically rolled over and deleted.
 9441: 
 9442: =item *
 9443: 
 9444: logperm() : append a permanent message to lonnet.perm.log, this log
 9445: file never gets deleted by any automated portion of the system, only
 9446: messages of critical importance should go in here.
 9447: 
 9448: =back
 9449: 
 9450: =head2 General File Helper Routines
 9451: 
 9452: =over 4
 9453: 
 9454: =item *
 9455: 
 9456: getfile($file,$caller) : two cases - requests for files in /res or in /uploaded.
 9457: (a) files in /uploaded
 9458:   (i) If a local copy of the file exists - 
 9459:       compares modification date of local copy with last-modified date for 
 9460:       definitive version stored on home server for course. If local copy is 
 9461:       stale, requests a new version from the home server and stores it. 
 9462:       If the original has been removed from the home server, then local copy 
 9463:       is unlinked.
 9464:   (ii) If local copy does not exist -
 9465:       requests the file from the home server and stores it. 
 9466:   
 9467:   If $caller is 'uploadrep':  
 9468:     This indicates a call from lonuploadrep.pm (PerlHeaderParserHandler phase)
 9469:     for request for files originally uploaded via DOCS. 
 9470:      - returns 'ok' if fresh local copy now available, -1 otherwise.
 9471:   
 9472:   Otherwise:
 9473:      This indicates a call from the content generation phase of the request.
 9474:      -  returns the entire contents of the file or -1.
 9475:      
 9476: (b) files in /res
 9477:    - returns the entire contents of a file or -1; 
 9478:    it properly subscribes to and replicates the file if neccessary.
 9479: 
 9480: 
 9481: =item *
 9482: 
 9483: stat_file($url) : $url is expected to be a /res/ or /uploaded/ style file
 9484:                   reference
 9485: 
 9486: returns either a stat() list of data about the file or an empty list
 9487: if the file doesn't exist or couldn't find out about it (connection
 9488: problems or user unknown)
 9489: 
 9490: =item *
 9491: 
 9492: filelocation($dir,$file) : returns file system location of a file
 9493: based on URI; meant to be "fairly clean" absolute reference, $dir is a
 9494: directory that relative $file lookups are to looked in ($dir of /a/dir
 9495: and a file of ../bob will become /a/bob)
 9496: 
 9497: =item *
 9498: 
 9499: hreflocation($dir,$file) : returns file system location or a URL; same as
 9500: filelocation except for hrefs
 9501: 
 9502: =item *
 9503: 
 9504: declutter() : declutters URLs (remove docroot, beginning slashes, 'res' etc)
 9505: 
 9506: =back
 9507: 
 9508: =head2 Usererfile file routines (/uploaded*)
 9509: 
 9510: =over 4
 9511: 
 9512: =item *
 9513: 
 9514: userfileupload(): main rotine for putting a file in a user or course's
 9515:                   filespace, arguments are,
 9516: 
 9517:  formname - required - this is the name of the element in $env where the
 9518:            filename, and the contents of the file to create/modifed exist
 9519:            the filename is in $env{'form.'.$formname.'.filename'} and the
 9520:            contents of the file is located in $env{'form.'.$formname}
 9521:  coursedoc - if true, store the file in the course of the active role
 9522:              of the current user
 9523:  subdir - required - subdirectory to put the file in under ../userfiles/
 9524:          if undefined, it will be placed in "unknown"
 9525: 
 9526:  (This routine calls clean_filename() to remove any dangerous
 9527:  characters from the filename, and then calls finuserfileupload() to
 9528:  complete the transaction)
 9529: 
 9530:  returns either the url of the uploaded file (/uploaded/....) if successful
 9531:  and /adm/notfound.html if unsuccessful
 9532: 
 9533: =item *
 9534: 
 9535: clean_filename(): routine for cleaing a filename up for storage in
 9536:                  userfile space, argument is:
 9537: 
 9538:  filename - proposed filename
 9539: 
 9540: returns: the new clean filename
 9541: 
 9542: =item *
 9543: 
 9544: finishuserfileupload(): routine that creaes and sends the file to
 9545: userspace, probably shouldn't be called directly
 9546: 
 9547:   docuname: username or courseid of destination for the file
 9548:   docudom: domain of user/course of destination for the file
 9549:   formname: same as for userfileupload()
 9550:   fname: filename (inculding subdirectories) for the file
 9551: 
 9552:  returns either the url of the uploaded file (/uploaded/....) if successful
 9553:  and /adm/notfound.html if unsuccessful
 9554: 
 9555: =item *
 9556: 
 9557: renameuserfile(): renames an existing userfile to a new name
 9558: 
 9559:   Args:
 9560:    docuname: username or courseid of destination for the file
 9561:    docudom: domain of user/course of destination for the file
 9562:    old: current file name (including any subdirs under userfiles)
 9563:    new: desired file name (including any subdirs under userfiles)
 9564: 
 9565: =item *
 9566: 
 9567: mkdiruserfile(): creates a directory is a userfiles dir
 9568: 
 9569:   Args:
 9570:    docuname: username or courseid of destination for the file
 9571:    docudom: domain of user/course of destination for the file
 9572:    dir: dir to create (including any subdirs under userfiles)
 9573: 
 9574: =item *
 9575: 
 9576: removeuserfile(): removes a file that exists in userfiles
 9577: 
 9578:   Args:
 9579:    docuname: username or courseid of destination for the file
 9580:    docudom: domain of user/course of destination for the file
 9581:    fname: filname to delete (including any subdirs under userfiles)
 9582: 
 9583: =item *
 9584: 
 9585: removeuploadedurl(): convience function for removeuserfile()
 9586: 
 9587:   Args:
 9588:    url:  a full /uploaded/... url to delete
 9589: 
 9590: =item * 
 9591: 
 9592: get_portfile_permissions():
 9593:   Args:
 9594:     domain: domain of user or course contain the portfolio files
 9595:     user: name of user or num of course contain the portfolio files
 9596:   Returns:
 9597:     hashref of a dump of the proper file_permissions.db
 9598:    
 9599: 
 9600: =item * 
 9601: 
 9602: get_access_controls():
 9603: 
 9604: Args:
 9605:   current_permissions: the hash ref returned from get_portfile_permissions()
 9606:   group: (optional) the group you want the files associated with
 9607:   file: (optional) the file you want access info on
 9608: 
 9609: Returns:
 9610:     a hash (keys are file names) of hashes containing
 9611:         keys are: path to file/file_name\0uniqueID:scope_end_start (see below)
 9612:         values are XML containing access control settings (see below) 
 9613: 
 9614: Internal notes:
 9615: 
 9616:  access controls are stored in file_permissions.db as key=value pairs.
 9617:     key -> path to file/file_name\0uniqueID:scope_end_start
 9618:         where scope -> public,guest,course,group,domains or users.
 9619:               end -> UNIX time for end of access (0 -> no end date)
 9620:               start -> UNIX time for start of access
 9621: 
 9622:     value -> XML description of access control
 9623:            <scope type=""> (type =1 of: public,guest,course,group,domains,users">
 9624:             <start></start>
 9625:             <end></end>
 9626: 
 9627:             <password></password>  for scope type = guest
 9628: 
 9629:             <domain></domain>     for scope type = course or group
 9630:             <number></number>
 9631:             <roles id="">
 9632:              <role></role>
 9633:              <access></access>
 9634:              <section></section>
 9635:              <group></group>
 9636:             </roles>
 9637: 
 9638:             <dom></dom>         for scope type = domains
 9639: 
 9640:             <users>             for scope type = users
 9641:              <user>
 9642:               <uname></uname>
 9643:               <udom></udom>
 9644:              </user>
 9645:             </users>
 9646:            </scope> 
 9647:               
 9648:  Access data is also aggregated for each file in an additional key=value pair:
 9649:  key -> path to file/file_name\0accesscontrol 
 9650:  value -> reference to hash
 9651:           hash contains key = value pairs
 9652:           where key = uniqueID:scope_end_start
 9653:                 value = UNIX time record was last updated
 9654: 
 9655:           Used to improve speed of look-ups of access controls for each file.  
 9656:  
 9657:  Locks on files (resulting from submission of portfolio file to a homework problem stored in array of arrays.
 9658: 
 9659: modify_access_controls():
 9660: 
 9661: Modifies access controls for a portfolio file
 9662: Args
 9663: 1. file name
 9664: 2. reference to hash of required changes,
 9665: 3. domain
 9666: 4. username
 9667:   where domain,username are the domain of the portfolio owner 
 9668:   (either a user or a course) 
 9669: 
 9670: Returns:
 9671: 1. result of additions or updates ('ok' or 'error', with error message). 
 9672: 2. result of deletions ('ok' or 'error', with error message).
 9673: 3. reference to hash of any new or updated access controls.
 9674: 4. reference to hash used to map incoming IDs to uniqueIDs assigned to control.
 9675:    key = integer (inbound ID)
 9676:    value = uniqueID  
 9677: 
 9678: =back
 9679: 
 9680: =head2 HTTP Helper Routines
 9681: 
 9682: =over 4
 9683: 
 9684: =item *
 9685: 
 9686: escape() : unpack non-word characters into CGI-compatible hex codes
 9687: 
 9688: =item *
 9689: 
 9690: unescape() : pack CGI-compatible hex codes into actual non-word ASCII character
 9691: 
 9692: =back
 9693: 
 9694: =head1 PRIVATE SUBROUTINES
 9695: 
 9696: =head2 Underlying communication routines (Shouldn't call)
 9697: 
 9698: =over 4
 9699: 
 9700: =item *
 9701: 
 9702: subreply() : tries to pass a message to lonc, returns con_lost if incapable
 9703: 
 9704: =item *
 9705: 
 9706: reply() : uses subreply to send a message to remote machine, logs all failures
 9707: 
 9708: =item *
 9709: 
 9710: critical() : passes a critical message to another server; if cannot
 9711: get through then place message in connection buffer directory and
 9712: returns con_delayed, if incapable of saving message, returns
 9713: con_failed
 9714: 
 9715: =item *
 9716: 
 9717: reconlonc() : tries to reconnect lonc client processes.
 9718: 
 9719: =back
 9720: 
 9721: =head2 Resource Access Logging
 9722: 
 9723: =over 4
 9724: 
 9725: =item *
 9726: 
 9727: flushcourselogs() : flush (save) buffer logs and access logs
 9728: 
 9729: =item *
 9730: 
 9731: courselog($what) : save message for course in hash
 9732: 
 9733: =item *
 9734: 
 9735: courseacclog($what) : save message for course using &courselog().  Perform
 9736: special processing for specific resource types (problems, exams, quizzes, etc).
 9737: 
 9738: =item *
 9739: 
 9740: goodbye() : flush course logs and log shutting down; it is called in srm.conf
 9741: as a PerlChildExitHandler
 9742: 
 9743: =back
 9744: 
 9745: =head2 Other
 9746: 
 9747: =over 4
 9748: 
 9749: =item *
 9750: 
 9751: symblist($mapname,%newhash) : update symbolic storage links
 9752: 
 9753: =back
 9754: 
 9755: =cut
 9756: 

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