File:  [LON-CAPA] / loncom / lonnet / perl / lonnet.pm
Revision 1.865: download - view: text, annotated - select for diffs
Wed Apr 4 18:01:11 2007 UTC (17 years, 3 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
Conditional access controls for users are stored in an array of hashes (not a plain hash) within the $access{$key}{'users'} hash.

    1: # The LearningOnline Network
    2: # TCP networking package
    3: #
    4: # $Id: lonnet.pm,v 1.865 2007/04/04 18:01:11 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: ###
   29: 
   30: package Apache::lonnet;
   31: 
   32: use strict;
   33: use LWP::UserAgent();
   34: use HTTP::Headers;
   35: use HTTP::Date;
   36: # use Date::Parse;
   37: use vars 
   38: qw(%perlvar %badServerCache %spareid 
   39:    %pr %prp $memcache %packagetab 
   40:    %courselogs %accesshash %userrolehash %domainrolehash $processmarker $dumpcount 
   41:    %coursedombuf %coursenumbuf %coursehombuf %coursedescrbuf %courseinstcodebuf %courseownerbuf %coursetypebuf
   42:    $tmpdir $_64bit %env);
   43: 
   44: use IO::Socket;
   45: use GDBM_File;
   46: use HTML::LCParser;
   47: use HTML::Parser;
   48: use Fcntl qw(:flock);
   49: use Storable qw(lock_store lock_nstore lock_retrieve freeze thaw nfreeze);
   50: use Time::HiRes qw( gettimeofday tv_interval );
   51: use Cache::Memcached;
   52: use Digest::MD5;
   53: use Math::Random;
   54: use LONCAPA qw(:DEFAULT :match);
   55: use LONCAPA::Configuration;
   56: 
   57: my $readit;
   58: my $max_connection_retries = 10;     # Or some such value.
   59: 
   60: require Exporter;
   61: 
   62: our @ISA = qw (Exporter);
   63: our @EXPORT = qw(%env);
   64: 
   65: =pod
   66: 
   67: =head1 Package Variables
   68: 
   69: These are largely undocumented, so if you decipher one please note it here.
   70: 
   71: =over 4
   72: 
   73: =item $processmarker
   74: 
   75: Contains the time this process was started and this servers host id.
   76: 
   77: =item $dumpcount
   78: 
   79: Counts the number of times a message log flush has been attempted (regardless
   80: of success) by this process.  Used as part of the filename when messages are
   81: delayed.
   82: 
   83: =back
   84: 
   85: =cut
   86: 
   87: 
   88: # --------------------------------------------------------------------- Logging
   89: {
   90:     my $logid;
   91:     sub instructor_log {
   92: 	my ($hash_name,$storehash,$delflag,$uname,$udom)=@_;
   93: 	$logid++;
   94: 	my $id=time().'00000'.$$.'00000'.$logid;
   95: 	return &Apache::lonnet::put('nohist_'.$hash_name,
   96: 				    { $id => {
   97: 					'exe_uname' => $env{'user.name'},
   98: 					'exe_udom'  => $env{'user.domain'},
   99: 					'exe_time'  => time(),
  100: 					'exe_ip'    => $ENV{'REMOTE_ADDR'},
  101: 					'delflag'   => $delflag,
  102: 					'logentry'  => $storehash,
  103: 					'uname'     => $uname,
  104: 					'udom'      => $udom,
  105: 				    }
  106: 				  },
  107: 				    $env{'course.'.$env{'request.course.id'}.'.domain'},
  108: 				    $env{'course.'.$env{'request.course.id'}.'.num'}
  109: 				    );
  110:     }
  111: }
  112: 
  113: sub logtouch {
  114:     my $execdir=$perlvar{'lonDaemons'};
  115:     unless (-e "$execdir/logs/lonnet.log") {	
  116: 	open(my $fh,">>$execdir/logs/lonnet.log");
  117: 	close $fh;
  118:     }
  119:     my ($wwwuid,$wwwgid)=(getpwnam('www'))[2,3];
  120:     chown($wwwuid,$wwwgid,$execdir.'/logs/lonnet.log');
  121: }
  122: 
  123: sub logthis {
  124:     my $message=shift;
  125:     my $execdir=$perlvar{'lonDaemons'};
  126:     my $now=time;
  127:     my $local=localtime($now);
  128:     if (open(my $fh,">>$execdir/logs/lonnet.log")) {
  129: 	print $fh "$local ($$): $message\n";
  130: 	close($fh);
  131:     }
  132:     return 1;
  133: }
  134: 
  135: sub logperm {
  136:     my $message=shift;
  137:     my $execdir=$perlvar{'lonDaemons'};
  138:     my $now=time;
  139:     my $local=localtime($now);
  140:     if (open(my $fh,">>$execdir/logs/lonnet.perm.log")) {
  141: 	print $fh "$now:$message:$local\n";
  142: 	close($fh);
  143:     }
  144:     return 1;
  145: }
  146: 
  147: sub create_connection {
  148:     my ($hostname,$lonid) = @_;
  149:     my $client=IO::Socket::UNIX->new(Peer    => $perlvar{'lonSockCreate'},
  150: 				     Type    => SOCK_STREAM,
  151: 				     Timeout => 10);
  152:     return 0 if (!$client);
  153:     print $client (join(':',$hostname,$lonid,&machine_ids($lonid))."\n");
  154:     my $result = <$client>;
  155:     chomp($result);
  156:     return 1 if ($result eq 'done');
  157:     return 0;
  158: }
  159: 
  160: 
  161: # -------------------------------------------------- Non-critical communication
  162: sub subreply {
  163:     my ($cmd,$server)=@_;
  164:     my $peerfile="$perlvar{'lonSockDir'}/".&hostname($server);
  165:     #
  166:     #  With loncnew process trimming, there's a timing hole between lonc server
  167:     #  process exit and the master server picking up the listen on the AF_UNIX
  168:     #  socket.  In that time interval, a lock file will exist:
  169: 
  170:     my $lockfile=$peerfile.".lock";
  171:     while (-e $lockfile) {	# Need to wait for the lockfile to disappear.
  172: 	sleep(1);
  173:     }
  174:     # At this point, either a loncnew parent is listening or an old lonc
  175:     # or loncnew child is listening so we can connect or everything's dead.
  176:     #
  177:     #   We'll give the connection a few tries before abandoning it.  If
  178:     #   connection is not possible, we'll con_lost back to the client.
  179:     #   
  180:     my $client;
  181:     for (my $retries = 0; $retries < $max_connection_retries; $retries++) {
  182: 	$client=IO::Socket::UNIX->new(Peer    =>"$peerfile",
  183: 				      Type    => SOCK_STREAM,
  184: 				      Timeout => 10);
  185: 	if($client) {
  186: 	    last;		# Connected!
  187: 	} else {
  188: 	    &create_connection(&hostname($server),$server);
  189: 	}
  190:         sleep(1);		# Try again later if failed connection.
  191:     }
  192:     my $answer;
  193:     if ($client) {
  194: 	print $client "sethost:$server:$cmd\n";
  195: 	$answer=<$client>;
  196: 	if (!$answer) { $answer="con_lost"; }
  197: 	chomp($answer);
  198:     } else {
  199: 	$answer = 'con_lost';	# Failed connection.
  200:     }
  201:     return $answer;
  202: }
  203: 
  204: sub reply {
  205:     my ($cmd,$server)=@_;
  206:     unless (defined(&hostname($server))) { return 'no_such_host'; }
  207:     my $answer=subreply($cmd,$server);
  208:     if (($answer=~/^refused/) || ($answer=~/^rejected/)) {
  209:        &logthis("<font color=\"blue\">WARNING:".
  210:                 " $cmd to $server returned $answer</font>");
  211:     }
  212:     return $answer;
  213: }
  214: 
  215: # ----------------------------------------------------------- Send USR1 to lonc
  216: 
  217: sub reconlonc {
  218:     &logthis("Trying to reconnect lonc");
  219:     my $loncfile="$perlvar{'lonDaemons'}/logs/lonc.pid";
  220:     if (open(my $fh,"<$loncfile")) {
  221: 	my $loncpid=<$fh>;
  222:         chomp($loncpid);
  223:         if (kill 0 => $loncpid) {
  224: 	    &logthis("lonc at pid $loncpid responding, sending USR1");
  225:             kill USR1 => $loncpid;
  226:             sleep 1;
  227:          } else {
  228: 	    &logthis(
  229:                "<font color=\"blue\">WARNING:".
  230:                " lonc at pid $loncpid not responding, giving up</font>");
  231:         }
  232:     } else {
  233: 	&logthis('<font color="blue">WARNING: lonc not running, giving up</font>');
  234:     }
  235: }
  236: 
  237: # ------------------------------------------------------ Critical communication
  238: 
  239: sub critical {
  240:     my ($cmd,$server)=@_;
  241:     unless (&hostname($server)) {
  242:         &logthis("<font color=\"blue\">WARNING:".
  243:                " Critical message to unknown server ($server)</font>");
  244:         return 'no_such_host';
  245:     }
  246:     my $answer=reply($cmd,$server);
  247:     if ($answer eq 'con_lost') {
  248: 	&reconlonc("$perlvar{'lonSockDir'}/$server");
  249: 	my $answer=reply($cmd,$server);
  250:         if ($answer eq 'con_lost') {
  251:             my $now=time;
  252:             my $middlename=$cmd;
  253:             $middlename=substr($middlename,0,16);
  254:             $middlename=~s/\W//g;
  255:             my $dfilename=
  256:       "$perlvar{'lonSockDir'}/delayed/$now.$dumpcount.$$.$middlename.$server";
  257:             $dumpcount++;
  258:             {
  259: 		my $dfh;
  260: 		if (open($dfh,">$dfilename")) {
  261: 		    print $dfh "$cmd\n"; 
  262: 		    close($dfh);
  263: 		}
  264:             }
  265:             sleep 2;
  266:             my $wcmd='';
  267:             {
  268: 		my $dfh;
  269: 		if (open($dfh,"<$dfilename")) {
  270: 		    $wcmd=<$dfh>; 
  271: 		    close($dfh);
  272: 		}
  273:             }
  274:             chomp($wcmd);
  275:             if ($wcmd eq $cmd) {
  276: 		&logthis("<font color=\"blue\">WARNING: ".
  277:                          "Connection buffer $dfilename: $cmd</font>");
  278:                 &logperm("D:$server:$cmd");
  279: 	        return 'con_delayed';
  280:             } else {
  281:                 &logthis("<font color=\"red\">CRITICAL:"
  282:                         ." Critical connection failed: $server $cmd</font>");
  283:                 &logperm("F:$server:$cmd");
  284:                 return 'con_failed';
  285:             }
  286:         }
  287:     }
  288:     return $answer;
  289: }
  290: 
  291: # ------------------------------------------- check if return value is an error
  292: 
  293: sub error {
  294:     my ($result) = @_;
  295:     if ($result =~ /^(con_lost|no_such_host|error: (\d+) (.*))/) {
  296: 	if ($2 == 2) { return undef; }
  297: 	return $1;
  298:     }
  299:     return undef;
  300: }
  301: 
  302: sub convert_and_load_session_env {
  303:     my ($lonidsdir,$handle)=@_;
  304:     my @profile;
  305:     {
  306: 	open(my $idf,"$lonidsdir/$handle.id");
  307: 	flock($idf,LOCK_SH);
  308: 	@profile=<$idf>;
  309: 	close($idf);
  310:     }
  311:     my %temp_env;
  312:     foreach my $line (@profile) {
  313: 	if ($line !~ m/=/) {
  314: 	    return 0;
  315: 	}
  316: 	chomp($line);
  317: 	my ($envname,$envvalue)=split(/=/,$line,2);
  318: 	$temp_env{&unescape($envname)} = &unescape($envvalue);
  319:     }
  320:     unlink("$lonidsdir/$handle.id");
  321:     if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",&GDBM_WRCREAT(),
  322: 	    0640)) {
  323: 	%disk_env = %temp_env;
  324: 	@env{keys(%temp_env)} = @disk_env{keys(%temp_env)};
  325: 	untie(%disk_env);
  326:     }
  327:     return 1;
  328: }
  329: 
  330: # ------------------------------------------- Transfer profile into environment
  331: my $env_loaded;
  332: sub transfer_profile_to_env {
  333:     my ($lonidsdir,$handle,$force_transfer) = @_;
  334:     if (!$force_transfer && $env_loaded) { return; } 
  335: 
  336:     if (!defined($lonidsdir)) {
  337: 	$lonidsdir = $perlvar{'lonIDsDir'};
  338:     }
  339:     if (!defined($handle)) {
  340:         ($handle) = ($env{'user.environment'} =~m|/([^/]+)\.id$| );
  341:     }
  342: 
  343:     my $convert;
  344:     {
  345:     	open(my $idf,"$lonidsdir/$handle.id");
  346: 	flock($idf,LOCK_SH);
  347: 	if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",
  348: 		&GDBM_READER(),0640)) {
  349: 	    @env{keys(%disk_env)} = @disk_env{keys(%disk_env)};
  350: 	    untie(%disk_env);
  351: 	} else {
  352: 	    $convert = 1;
  353: 	}
  354:     }
  355:     if ($convert) {
  356: 	if (!&convert_and_load_session_env($lonidsdir,$handle)) {
  357: 	    &logthis("Failed to load session, or convert session.");
  358: 	}
  359:     }
  360: 
  361:     my %remove;
  362:     while ( my $envname = each(%env) ) {
  363:         if (my ($key,$time) = ($envname =~ /^(cgi\.(\d+)_\d+\.)/)) {
  364:             if ($time < time-300) {
  365:                 $remove{$key}++;
  366:             }
  367:         }
  368:     }
  369: 
  370:     $env{'user.environment'} = "$lonidsdir/$handle.id";
  371:     $env_loaded=1;
  372:     foreach my $expired_key (keys(%remove)) {
  373:         &delenv($expired_key);
  374:     }
  375: }
  376: 
  377: sub timed_flock {
  378:     my ($file,$lock_type) = @_;
  379:     my $failed=0;
  380:     eval {
  381: 	local $SIG{__DIE__}='DEFAULT';
  382: 	local $SIG{ALRM}=sub {
  383: 	    $failed=1;
  384: 	    die("failed lock");
  385: 	};
  386: 	alarm(13);
  387: 	flock($file,$lock_type);
  388: 	alarm(0);
  389:     };
  390:     if ($failed) {
  391: 	return undef;
  392:     } else {
  393: 	return 1;
  394:     }
  395: }
  396: 
  397: # ---------------------------------------------------------- Append Environment
  398: 
  399: sub appenv {
  400:     my %newenv=@_;
  401:     foreach my $key (keys(%newenv)) {
  402: 	if (($newenv{$key}=~/^user\.role/) || ($newenv{$key}=~/^user\.priv/)) {
  403:             &logthis("<font color=\"blue\">WARNING: ".
  404:                 "Attempt to modify environment ".$key." to ".$newenv{$key}
  405:                 .'</font>');
  406: 	    delete($newenv{$key});
  407:         } else {
  408:             $env{$key}=$newenv{$key};
  409:         }
  410:     }
  411:     open(my $env_file,$env{'user.environment'});
  412:     if (&timed_flock($env_file,LOCK_EX)
  413: 	&&
  414: 	tie(my %disk_env,'GDBM_File',$env{'user.environment'},
  415: 	    (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
  416: 	while (my ($key,$value) = each(%newenv)) {
  417: 	    $disk_env{$key} = $value;
  418: 	}
  419: 	untie(%disk_env);
  420:     }
  421:     return 'ok';
  422: }
  423: # ----------------------------------------------------- Delete from Environment
  424: 
  425: sub delenv {
  426:     my $delthis=shift;
  427:     if (($delthis=~/user\.role/) || ($delthis=~/user\.priv/)) {
  428:         &logthis("<font color=\"blue\">WARNING: ".
  429:                 "Attempt to delete from environment ".$delthis);
  430:         return 'error';
  431:     }
  432:     open(my $env_file,$env{'user.environment'});
  433:     if (&timed_flock($env_file,LOCK_EX)
  434: 	&&
  435: 	tie(my %disk_env,'GDBM_File',$env{'user.environment'},
  436: 	    (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
  437: 	foreach my $key (keys(%disk_env)) {
  438: 	    if ($key=~/^$delthis/) { 
  439:                 delete($env{$key});
  440:                 delete($disk_env{$key});
  441:             }
  442: 	}
  443: 	untie(%disk_env);
  444:     }
  445:     return 'ok';
  446: }
  447: 
  448: sub get_env_multiple {
  449:     my ($name) = @_;
  450:     my @values;
  451:     if (defined($env{$name})) {
  452:         # exists is it an array
  453:         if (ref($env{$name})) {
  454:             @values=@{ $env{$name} };
  455:         } else {
  456:             $values[0]=$env{$name};
  457:         }
  458:     }
  459:     return(@values);
  460: }
  461: 
  462: # ------------------------------------------ Find out current server userload
  463: # there is a copy in lond
  464: sub userload {
  465:     my $numusers=0;
  466:     {
  467: 	opendir(LONIDS,$perlvar{'lonIDsDir'});
  468: 	my $filename;
  469: 	my $curtime=time;
  470: 	while ($filename=readdir(LONIDS)) {
  471: 	    if ($filename eq '.' || $filename eq '..') {next;}
  472: 	    my ($mtime)=(stat($perlvar{'lonIDsDir'}.'/'.$filename))[9];
  473: 	    if ($curtime-$mtime < 1800) { $numusers++; }
  474: 	}
  475: 	closedir(LONIDS);
  476:     }
  477:     my $userloadpercent=0;
  478:     my $maxuserload=$perlvar{'lonUserLoadLim'};
  479:     if ($maxuserload) {
  480: 	$userloadpercent=100*$numusers/$maxuserload;
  481:     }
  482:     $userloadpercent=sprintf("%.2f",$userloadpercent);
  483:     return $userloadpercent;
  484: }
  485: 
  486: # ------------------------------------------ Fight off request when overloaded
  487: 
  488: sub overloaderror {
  489:     my ($r,$checkserver)=@_;
  490:     unless ($checkserver) { $checkserver=$perlvar{'lonHostID'}; }
  491:     my $loadavg;
  492:     if ($checkserver eq $perlvar{'lonHostID'}) {
  493:        open(my $loadfile,'/proc/loadavg');
  494:        $loadavg=<$loadfile>;
  495:        $loadavg =~ s/\s.*//g;
  496:        $loadavg = 100*$loadavg/$perlvar{'lonLoadLim'};
  497:        close($loadfile);
  498:     } else {
  499:        $loadavg=&reply('load',$checkserver);
  500:     }
  501:     my $overload=$loadavg-100;
  502:     if ($overload>0) {
  503: 	$r->err_headers_out->{'Retry-After'}=$overload;
  504:         $r->log_error('Overload of '.$overload.' on '.$checkserver);
  505:         return 413;
  506:     }    
  507:     return '';
  508: }
  509: 
  510: # ------------------------------ Find server with least workload from spare.tab
  511: 
  512: sub spareserver {
  513:     my ($loadpercent,$userloadpercent,$want_server_name) = @_;
  514:     my $spare_server;
  515:     if ($userloadpercent !~ /\d/) { $userloadpercent=0; }
  516:     my $lowest_load=($loadpercent > $userloadpercent) ? $loadpercent 
  517:                                                      :  $userloadpercent;
  518:     
  519:     foreach my $try_server (@{ $spareid{'primary'} }) {
  520: 	($spare_server, $lowest_load) =
  521: 	    &compare_server_load($try_server, $spare_server, $lowest_load);
  522:     }
  523: 
  524:     my $found_server = ($spare_server ne '' && $lowest_load < 100);
  525: 
  526:     if (!$found_server) {
  527: 	foreach my $try_server (@{ $spareid{'default'} }) {
  528: 	    ($spare_server, $lowest_load) =
  529: 		&compare_server_load($try_server, $spare_server, $lowest_load);
  530: 	}
  531:     }
  532: 
  533:     if (!$want_server_name) {
  534: 	$spare_server="http://".&hostname($spare_server);
  535:     }
  536:     return $spare_server;
  537: }
  538: 
  539: sub compare_server_load {
  540:     my ($try_server, $spare_server, $lowest_load) = @_;
  541: 
  542:     my $loadans     = &reply('load',    $try_server);
  543:     my $userloadans = &reply('userload',$try_server);
  544: 
  545:     if ($loadans !~ /\d/ && $userloadans !~ /\d/) {
  546: 	next; #didn't get a number from the server
  547:     }
  548: 
  549:     my $load;
  550:     if ($loadans =~ /\d/) {
  551: 	if ($userloadans =~ /\d/) {
  552: 	    #both are numbers, pick the bigger one
  553: 	    $load = ($loadans > $userloadans) ? $loadans 
  554: 		                              : $userloadans;
  555: 	} else {
  556: 	    $load = $loadans;
  557: 	}
  558:     } else {
  559: 	$load = $userloadans;
  560:     }
  561: 
  562:     if (($load =~ /\d/) && ($load < $lowest_load)) {
  563: 	$spare_server = $try_server;
  564: 	$lowest_load  = $load;
  565:     }
  566:     return ($spare_server,$lowest_load);
  567: }
  568: # --------------------------------------------- Try to change a user's password
  569: 
  570: sub changepass {
  571:     my ($uname,$udom,$currentpass,$newpass,$server,$context)=@_;
  572:     $currentpass = &escape($currentpass);
  573:     $newpass     = &escape($newpass);
  574:     my $answer = reply("encrypt:passwd:$udom:$uname:$currentpass:$newpass:$context",
  575: 		       $server);
  576:     if (! $answer) {
  577: 	&logthis("No reply on password change request to $server ".
  578: 		 "by $uname in domain $udom.");
  579:     } elsif ($answer =~ "^ok") {
  580:         &logthis("$uname in $udom successfully changed their password ".
  581: 		 "on $server.");
  582:     } elsif ($answer =~ "^pwchange_failure") {
  583: 	&logthis("$uname in $udom was unable to change their password ".
  584: 		 "on $server.  The action was blocked by either lcpasswd ".
  585: 		 "or pwchange");
  586:     } elsif ($answer =~ "^non_authorized") {
  587:         &logthis("$uname in $udom did not get their password correct when ".
  588: 		 "attempting to change it on $server.");
  589:     } elsif ($answer =~ "^auth_mode_error") {
  590:         &logthis("$uname in $udom attempted to change their password despite ".
  591: 		 "not being locally or internally authenticated on $server.");
  592:     } elsif ($answer =~ "^unknown_user") {
  593:         &logthis("$uname in $udom attempted to change their password ".
  594: 		 "on $server but were unable to because $server is not ".
  595: 		 "their home server.");
  596:     } elsif ($answer =~ "^refused") {
  597: 	&logthis("$server refused to change $uname in $udom password because ".
  598: 		 "it was sent an unencrypted request to change the password.");
  599:     }
  600:     return $answer;
  601: }
  602: 
  603: # ----------------------- Try to determine user's current authentication scheme
  604: 
  605: sub queryauthenticate {
  606:     my ($uname,$udom)=@_;
  607:     my $uhome=&homeserver($uname,$udom);
  608:     if (!$uhome) {
  609: 	&logthis("User $uname at $udom is unknown when looking for authentication mechanism");
  610: 	return 'no_host';
  611:     }
  612:     my $answer=reply("encrypt:currentauth:$udom:$uname",$uhome);
  613:     if ($answer =~ /^(unknown_user|refused|con_lost)/) {
  614: 	&logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
  615:     }
  616:     return $answer;
  617: }
  618: 
  619: # --------- Try to authenticate user from domain's lib servers (first this one)
  620: 
  621: sub authenticate {
  622:     my ($uname,$upass,$udom)=@_;
  623:     $upass=&escape($upass);
  624:     $uname= &LONCAPA::clean_username($uname);
  625:     my $uhome=&homeserver($uname,$udom,1);
  626:     if ((!$uhome) || ($uhome eq 'no_host')) {
  627: # Maybe the machine was offline and only re-appeared again recently?
  628:         &reconlonc();
  629: # One more
  630: 	my $uhome=&homeserver($uname,$udom,1);
  631: 	if ((!$uhome) || ($uhome eq 'no_host')) {
  632: 	    &logthis("User $uname at $udom is unknown in authenticate");
  633: 	}
  634: 	return 'no_host';
  635:     }
  636:     my $answer=reply("encrypt:auth:$udom:$uname:$upass",$uhome);
  637:     if ($answer eq 'authorized') {
  638: 	&logthis("User $uname at $udom authorized by $uhome"); 
  639: 	return $uhome; 
  640:     }
  641:     if ($answer eq 'non_authorized') {
  642: 	&logthis("User $uname at $udom rejected by $uhome");
  643: 	return 'no_host'; 
  644:     }
  645:     &logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
  646:     return 'no_host';
  647: }
  648: 
  649: # ---------------------- Find the homebase for a user from domain's lib servers
  650: 
  651: my %homecache;
  652: sub homeserver {
  653:     my ($uname,$udom,$ignoreBadCache)=@_;
  654:     my $index="$uname:$udom";
  655: 
  656:     if (exists($homecache{$index})) { return $homecache{$index}; }
  657: 
  658:     my %servers = &get_servers($udom,'library');
  659:     foreach my $tryserver (keys(%servers)) {
  660:         next if ($ignoreBadCache ne 'true' && 
  661: 		 exists($badServerCache{$tryserver}));
  662: 
  663: 	my $answer=reply("home:$udom:$uname",$tryserver);
  664: 	if ($answer eq 'found') {
  665: 	    delete($badServerCache{$tryserver}); 
  666: 	    return $homecache{$index}=$tryserver;
  667: 	} elsif ($answer eq 'no_host') {
  668: 	    $badServerCache{$tryserver}=1;
  669: 	}
  670:     }    
  671:     return 'no_host';
  672: }
  673: 
  674: # ------------------------------------- Find the usernames behind a list of IDs
  675: 
  676: sub idget {
  677:     my ($udom,@ids)=@_;
  678:     my %returnhash=();
  679:     
  680:     my %servers = &get_servers($udom,'library');
  681:     foreach my $tryserver (keys(%servers)) {
  682: 	my $idlist=join('&',@ids);
  683: 	$idlist=~tr/A-Z/a-z/; 
  684: 	my $reply=&reply("idget:$udom:".$idlist,$tryserver);
  685: 	my @answer=();
  686: 	if (($reply ne 'con_lost') && ($reply!~/^error\:/)) {
  687: 	    @answer=split(/\&/,$reply);
  688: 	}                    ;
  689: 	my $i;
  690: 	for ($i=0;$i<=$#ids;$i++) {
  691: 	    if ($answer[$i]) {
  692: 		$returnhash{$ids[$i]}=$answer[$i];
  693: 	    } 
  694: 	}
  695:     } 
  696:     return %returnhash;
  697: }
  698: 
  699: # ------------------------------------- Find the IDs behind a list of usernames
  700: 
  701: sub idrget {
  702:     my ($udom,@unames)=@_;
  703:     my %returnhash=();
  704:     foreach my $uname (@unames) {
  705:         $returnhash{$uname}=(&userenvironment($udom,$uname,'id'))[1];
  706:     }
  707:     return %returnhash;
  708: }
  709: 
  710: # ------------------------------- Store away a list of names and associated IDs
  711: 
  712: sub idput {
  713:     my ($udom,%ids)=@_;
  714:     my %servers=();
  715:     foreach my $uname (keys(%ids)) {
  716: 	&cput('environment',{'id'=>$ids{$uname}},$udom,$uname);
  717:         my $uhom=&homeserver($uname,$udom);
  718:         if ($uhom ne 'no_host') {
  719:             my $id=&escape($ids{$uname});
  720:             $id=~tr/A-Z/a-z/;
  721:             my $esc_unam=&escape($uname);
  722: 	    if ($servers{$uhom}) {
  723: 		$servers{$uhom}.='&'.$id.'='.$esc_unam;
  724:             } else {
  725:                 $servers{$uhom}=$id.'='.$esc_unam;
  726:             }
  727:         }
  728:     }
  729:     foreach my $server (keys(%servers)) {
  730:         &critical('idput:'.$udom.':'.$servers{$server},$server);
  731:     }
  732: }
  733: 
  734: # ------------------------------------------- get items from domain db files   
  735: 
  736: sub get_dom {
  737:     my ($namespace,$storearr,$udom,$uhome)=@_;
  738:     my $items='';
  739:     foreach my $item (@$storearr) {
  740:         $items.=&escape($item).'&';
  741:     }
  742:     $items=~s/\&$//;
  743:     if (!$udom) {
  744:         $udom=$env{'user.domain'};
  745:         if (defined(&domain($udom,'primary'))) {
  746:             $uhome=&domain($udom,'primary');
  747:         } else {
  748:             $uhome eq '';
  749:         }
  750:     } else {
  751:         if (!$uhome) {
  752:             if (defined(&domain($udom,'primary'))) {
  753:                 $uhome=&domain($udom,'primary');
  754:             }
  755:         }
  756:     }
  757:     if ($udom && $uhome && ($uhome ne 'no_host')) {
  758:         my $rep=&reply("getdom:$udom:$namespace:$items",$uhome);
  759:         my @pairs=split(/\&/,$rep);
  760:         if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
  761:             return @pairs;
  762:         }
  763:         my %returnhash=();
  764:         my $i=0;
  765:         foreach my $item (@$storearr) {
  766:             $returnhash{$item}=&thaw_unescape($pairs[$i]);
  767:             $i++;
  768:         }
  769:         return %returnhash;
  770:     } else {
  771:         &logthis("get_dom failed - no homeserver and/or domain");
  772:     }
  773: }
  774: 
  775: # -------------------------------------------- put items in domain db files 
  776: 
  777: sub put_dom {
  778:     my ($namespace,$storehash,$udom,$uhome)=@_;
  779:     if (!$udom) {
  780:         $udom=$env{'user.domain'};
  781:         if (defined(&domain($udom,'primary'))) {
  782:             $uhome=&domain($udom,'primary');
  783:         } else {
  784:             $uhome eq '';
  785:         }
  786:     } else {
  787:         if (!$uhome) {
  788:             if (defined(&domain($udom,'primary'))) {
  789:                 $uhome=&domain($udom,'primary');
  790:             }
  791:         }
  792:     } 
  793:     if ($udom && $uhome && ($uhome ne 'no_host')) {
  794:         my $items='';
  795:         foreach my $item (keys(%$storehash)) {
  796:             $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
  797:         }
  798:         $items=~s/\&$//;
  799:         return &reply("putdom:$udom:$namespace:$items",$uhome);
  800:     } else {
  801:         &logthis("put_dom failed - no homeserver and/or domain");
  802:     }
  803: }
  804: 
  805: sub retrieve_inst_usertypes {
  806:     my ($udom) = @_;
  807:     my (%returnhash,@order);
  808:     if (defined(&domain($udom,'primary'))) {
  809:         my $uhome=&domain($udom,'primary');
  810:         my $rep=&reply("inst_usertypes:$udom",$uhome);
  811:         my ($hashitems,$orderitems) = split(/:/,$rep); 
  812:         my @pairs=split(/\&/,$hashitems);
  813:         foreach my $item (@pairs) {
  814:             my ($key,$value)=split(/=/,$item,2);
  815:             $key = &unescape($key);
  816:             next if ($key =~ /^error: 2 /);
  817:             $returnhash{$key}=&thaw_unescape($value);
  818:         }
  819:         my @esc_order = split(/\&/,$orderitems);
  820:         foreach my $item (@esc_order) {
  821:             push(@order,&unescape($item));
  822:         }
  823:     } else {
  824:         &logthis("get_dom failed - no primary domain server for $udom");
  825:     }
  826:     return (\%returnhash,\@order);
  827: }
  828: 
  829: # --------------------------------------------------- Assign a key to a student
  830: 
  831: sub assign_access_key {
  832: #
  833: # a valid key looks like uname:udom#comments
  834: # comments are being appended
  835: #
  836:     my ($ckey,$kdom,$knum,$cdom,$cnum,$udom,$uname,$logentry)=@_;
  837:     $kdom=
  838:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($kdom));
  839:     $knum=
  840:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($knum));
  841:     $cdom=
  842:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
  843:     $cnum=
  844:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
  845:     $udom=$env{'user.name'} unless (defined($udom));
  846:     $uname=$env{'user.domain'} unless (defined($uname));
  847:     my %existing=&get('accesskeys',[$ckey],$kdom,$knum);
  848:     if (($existing{$ckey}=~/^\#(.*)$/) || # - new key
  849:         ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#(.*)$/)) { 
  850:                                                   # assigned to this person
  851:                                                   # - this should not happen,
  852:                                                   # unless something went wrong
  853:                                                   # the first time around
  854: # ready to assign
  855:         $logentry=$1.'; '.$logentry;
  856:         if (&put('accesskeys',{$ckey=>$uname.':'.$udom.'#'.$logentry},
  857:                                                  $kdom,$knum) eq 'ok') {
  858: # key now belongs to user
  859: 	    my $envkey='key.'.$cdom.'_'.$cnum;
  860:             if (&put('environment',{$envkey => $ckey}) eq 'ok') {
  861:                 &appenv('environment.'.$envkey => $ckey);
  862:                 return 'ok';
  863:             } else {
  864:                 return 
  865:   'error: Count not permanently assign key, will need to be re-entered later.';
  866: 	    }
  867:         } else {
  868:             return 'error: Could not assign key, try again later.';
  869:         }
  870:     } elsif (!$existing{$ckey}) {
  871: # the key does not exist
  872: 	return 'error: The key does not exist';
  873:     } else {
  874: # the key is somebody else's
  875: 	return 'error: The key is already in use';
  876:     }
  877: }
  878: 
  879: # ------------------------------------------ put an additional comment on a key
  880: 
  881: sub comment_access_key {
  882: #
  883: # a valid key looks like uname:udom#comments
  884: # comments are being appended
  885: #
  886:     my ($ckey,$cdom,$cnum,$logentry)=@_;
  887:     $cdom=
  888:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
  889:     $cnum=
  890:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
  891:     my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
  892:     if ($existing{$ckey}) {
  893:         $existing{$ckey}.='; '.$logentry;
  894: # ready to assign
  895:         if (&put('accesskeys',{$ckey=>$existing{$ckey}},
  896:                                                  $cdom,$cnum) eq 'ok') {
  897: 	    return 'ok';
  898:         } else {
  899: 	    return 'error: Count not store comment.';
  900:         }
  901:     } else {
  902: # the key does not exist
  903: 	return 'error: The key does not exist';
  904:     }
  905: }
  906: 
  907: # ------------------------------------------------------ Generate a set of keys
  908: 
  909: sub generate_access_keys {
  910:     my ($number,$cdom,$cnum,$logentry)=@_;
  911:     $cdom=
  912:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
  913:     $cnum=
  914:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
  915:     unless (&allowed('mky',$cdom)) { return 0; }
  916:     unless (($cdom) && ($cnum)) { return 0; }
  917:     if ($number>10000) { return 0; }
  918:     sleep(2); # make sure don't get same seed twice
  919:     srand(time()^($$+($$<<15))); # from "Programming Perl"
  920:     my $total=0;
  921:     for (my $i=1;$i<=$number;$i++) {
  922:        my $newkey=sprintf("%lx",int(100000*rand)).'-'.
  923:                   sprintf("%lx",int(100000*rand)).'-'.
  924:                   sprintf("%lx",int(100000*rand));
  925:        $newkey=~s/1/g/g; # folks mix up 1 and l
  926:        $newkey=~s/0/h/g; # and also 0 and O
  927:        my %existing=&get('accesskeys',[$newkey],$cdom,$cnum);
  928:        if ($existing{$newkey}) {
  929:            $i--;
  930:        } else {
  931: 	  if (&put('accesskeys',
  932:               { $newkey => '# generated '.localtime().
  933:                            ' by '.$env{'user.name'}.'@'.$env{'user.domain'}.
  934:                            '; '.$logentry },
  935: 		   $cdom,$cnum) eq 'ok') {
  936:               $total++;
  937: 	  }
  938:        }
  939:     }
  940:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
  941:          'Generated '.$total.' keys for '.$cnum.' at '.$cdom);
  942:     return $total;
  943: }
  944: 
  945: # ------------------------------------------------------- Validate an accesskey
  946: 
  947: sub validate_access_key {
  948:     my ($ckey,$cdom,$cnum,$udom,$uname)=@_;
  949:     $cdom=
  950:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
  951:     $cnum=
  952:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
  953:     $udom=$env{'user.domain'} unless (defined($udom));
  954:     $uname=$env{'user.name'} unless (defined($uname));
  955:     my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
  956:     return ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#/);
  957: }
  958: 
  959: # ------------------------------------- Find the section of student in a course
  960: sub devalidate_getsection_cache {
  961:     my ($udom,$unam,$courseid)=@_;
  962:     my $hashid="$udom:$unam:$courseid";
  963:     &devalidate_cache_new('getsection',$hashid);
  964: }
  965: 
  966: sub courseid_to_courseurl {
  967:     my ($courseid) = @_;
  968:     #already url style courseid
  969:     return $courseid if ($courseid =~ m{^/});
  970: 
  971:     if (exists($env{'course.'.$courseid.'.num'})) {
  972: 	my $cnum = $env{'course.'.$courseid.'.num'};
  973: 	my $cdom = $env{'course.'.$courseid.'.domain'};
  974: 	return "/$cdom/$cnum";
  975:     }
  976: 
  977:     my %courseinfo=&Apache::lonnet::coursedescription($courseid);
  978:     if (exists($courseinfo{'num'})) {
  979: 	return "/$courseinfo{'domain'}/$courseinfo{'num'}";
  980:     }
  981: 
  982:     return undef;
  983: }
  984: 
  985: sub getsection {
  986:     my ($udom,$unam,$courseid)=@_;
  987:     my $cachetime=1800;
  988: 
  989:     my $hashid="$udom:$unam:$courseid";
  990:     my ($result,$cached)=&is_cached_new('getsection',$hashid);
  991:     if (defined($cached)) { return $result; }
  992: 
  993:     my %Pending; 
  994:     my %Expired;
  995:     #
  996:     # Each role can either have not started yet (pending), be active, 
  997:     #    or have expired.
  998:     #
  999:     # If there is an active role, we are done.
 1000:     #
 1001:     # If there is more than one role which has not started yet, 
 1002:     #     choose the one which will start sooner
 1003:     # If there is one role which has not started yet, return it.
 1004:     #
 1005:     # If there is more than one expired role, choose the one which ended last.
 1006:     # If there is a role which has expired, return it.
 1007:     #
 1008:     $courseid = &courseid_to_courseurl($courseid);
 1009:     my %roleshash = &dump('roles',$udom,$unam,$courseid);
 1010:     foreach my $key (keys(%roleshash)) {
 1011:         next if ($key !~/^\Q$courseid\E(?:\/)*(\w+)*\_st$/);
 1012:         my $section=$1;
 1013:         if ($key eq $courseid.'_st') { $section=''; }
 1014:         my ($dummy,$end,$start)=split(/\_/,&unescape($roleshash{$key}));
 1015:         my $now=time;
 1016:         if (defined($end) && $end && ($now > $end)) {
 1017:             $Expired{$end}=$section;
 1018:             next;
 1019:         }
 1020:         if (defined($start) && $start && ($now < $start)) {
 1021:             $Pending{$start}=$section;
 1022:             next;
 1023:         }
 1024:         return &do_cache_new('getsection',$hashid,$section,$cachetime);
 1025:     }
 1026:     #
 1027:     # Presumedly there will be few matching roles from the above
 1028:     # loop and the sorting time will be negligible.
 1029:     if (scalar(keys(%Pending))) {
 1030:         my ($time) = sort {$a <=> $b} keys(%Pending);
 1031:         return &do_cache_new('getsection',$hashid,$Pending{$time},$cachetime);
 1032:     } 
 1033:     if (scalar(keys(%Expired))) {
 1034:         my @sorted = sort {$a <=> $b} keys(%Expired);
 1035:         my $time = pop(@sorted);
 1036:         return &do_cache_new('getsection',$hashid,$Expired{$time},$cachetime);
 1037:     }
 1038:     return &do_cache_new('getsection',$hashid,'-1',$cachetime);
 1039: }
 1040: 
 1041: sub save_cache {
 1042:     &purge_remembered();
 1043:     #&Apache::loncommon::validate_page();
 1044:     undef(%env);
 1045:     undef($env_loaded);
 1046: }
 1047: 
 1048: my $to_remember=-1;
 1049: my %remembered;
 1050: my %accessed;
 1051: my $kicks=0;
 1052: my $hits=0;
 1053: sub make_key {
 1054:     my ($name,$id) = @_;
 1055:     if (length($id) > 200) { $id=length($id).':'.&Digest::MD5::md5_hex($id); }
 1056:     return &escape($name.':'.$id);
 1057: }
 1058: 
 1059: sub devalidate_cache_new {
 1060:     my ($name,$id,$debug) = @_;
 1061:     if ($debug) { &Apache::lonnet::logthis("deleting $name:$id"); }
 1062:     $id=&make_key($name,$id);
 1063:     $memcache->delete($id);
 1064:     delete($remembered{$id});
 1065:     delete($accessed{$id});
 1066: }
 1067: 
 1068: sub is_cached_new {
 1069:     my ($name,$id,$debug) = @_;
 1070:     $id=&make_key($name,$id);
 1071:     if (exists($remembered{$id})) {
 1072: 	if ($debug) { &Apache::lonnet::logthis("Earyl return $id of $remembered{$id} "); }
 1073: 	$accessed{$id}=[&gettimeofday()];
 1074: 	$hits++;
 1075: 	return ($remembered{$id},1);
 1076:     }
 1077:     my $value = $memcache->get($id);
 1078:     if (!(defined($value))) {
 1079: 	if ($debug) { &Apache::lonnet::logthis("getting $id is not defined"); }
 1080: 	return (undef,undef);
 1081:     }
 1082:     if ($value eq '__undef__') {
 1083: 	if ($debug) { &Apache::lonnet::logthis("getting $id is __undef__"); }
 1084: 	$value=undef;
 1085:     }
 1086:     &make_room($id,$value,$debug);
 1087:     if ($debug) { &Apache::lonnet::logthis("getting $id is $value"); }
 1088:     return ($value,1);
 1089: }
 1090: 
 1091: sub do_cache_new {
 1092:     my ($name,$id,$value,$time,$debug) = @_;
 1093:     $id=&make_key($name,$id);
 1094:     my $setvalue=$value;
 1095:     if (!defined($setvalue)) {
 1096: 	$setvalue='__undef__';
 1097:     }
 1098:     if (!defined($time) ) {
 1099: 	$time=600;
 1100:     }
 1101:     if ($debug) { &Apache::lonnet::logthis("Setting $id to $value"); }
 1102:     $memcache->set($id,$setvalue,$time);
 1103:     # need to make a copy of $value
 1104:     #&make_room($id,$value,$debug);
 1105:     return $value;
 1106: }
 1107: 
 1108: sub make_room {
 1109:     my ($id,$value,$debug)=@_;
 1110:     $remembered{$id}=$value;
 1111:     if ($to_remember<0) { return; }
 1112:     $accessed{$id}=[&gettimeofday()];
 1113:     if (scalar(keys(%remembered)) <= $to_remember) { return; }
 1114:     my $to_kick;
 1115:     my $max_time=0;
 1116:     foreach my $other (keys(%accessed)) {
 1117: 	if (&tv_interval($accessed{$other}) > $max_time) {
 1118: 	    $to_kick=$other;
 1119: 	    $max_time=&tv_interval($accessed{$other});
 1120: 	}
 1121:     }
 1122:     delete($remembered{$to_kick});
 1123:     delete($accessed{$to_kick});
 1124:     $kicks++;
 1125:     if ($debug) { &logthis("kicking $to_kick $max_time $kicks\n"); }
 1126:     return;
 1127: }
 1128: 
 1129: sub purge_remembered {
 1130:     #&logthis("Tossing ".scalar(keys(%remembered)));
 1131:     #&logthis(sprintf("%-20s is %s",'%remembered',length(&freeze(\%remembered))));
 1132:     undef(%remembered);
 1133:     undef(%accessed);
 1134: }
 1135: # ------------------------------------- Read an entry from a user's environment
 1136: 
 1137: sub userenvironment {
 1138:     my ($udom,$unam,@what)=@_;
 1139:     my %returnhash=();
 1140:     my @answer=split(/\&/,
 1141:                 &reply('get:'.$udom.':'.$unam.':environment:'.join('&',@what),
 1142:                       &homeserver($unam,$udom)));
 1143:     my $i;
 1144:     for ($i=0;$i<=$#what;$i++) {
 1145: 	$returnhash{$what[$i]}=&unescape($answer[$i]);
 1146:     }
 1147:     return %returnhash;
 1148: }
 1149: 
 1150: # ---------------------------------------------------------- Get a studentphoto
 1151: sub studentphoto {
 1152:     my ($udom,$unam,$ext) = @_;
 1153:     my $home=&Apache::lonnet::homeserver($unam,$udom);
 1154:     if (defined($env{'request.course.id'})) {
 1155:         if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
 1156:             if ($udom eq $env{'course.'.$env{'request.course.id'}.'.domain'}) {
 1157:                 return(&retrievestudentphoto($udom,$unam,$ext)); 
 1158:             } else {
 1159:                 my ($result,$perm_reqd)=
 1160: 		    &Apache::lonnet::auto_photo_permission($unam,$udom);
 1161:                 if ($result eq 'ok') {
 1162:                     if (!($perm_reqd eq 'yes')) {
 1163:                         return(&retrievestudentphoto($udom,$unam,$ext));
 1164:                     }
 1165:                 }
 1166:             }
 1167:         }
 1168:     } else {
 1169:         my ($result,$perm_reqd) = 
 1170: 	    &Apache::lonnet::auto_photo_permission($unam,$udom);
 1171:         if ($result eq 'ok') {
 1172:             if (!($perm_reqd eq 'yes')) {
 1173:                 return(&retrievestudentphoto($udom,$unam,$ext));
 1174:             }
 1175:         }
 1176:     }
 1177:     return '/adm/lonKaputt/lonlogo_broken.gif';
 1178: }
 1179: 
 1180: sub retrievestudentphoto {
 1181:     my ($udom,$unam,$ext,$type) = @_;
 1182:     my $home=&Apache::lonnet::homeserver($unam,$udom);
 1183:     my $ret=&Apache::lonnet::reply("studentphoto:$udom:$unam:$ext:$type",$home);
 1184:     if ($ret eq 'ok') {
 1185:         my $url="/uploaded/$udom/$unam/internal/studentphoto.$ext";
 1186:         if ($type eq 'thumbnail') {
 1187:             $url="/uploaded/$udom/$unam/internal/studentphoto_tn.$ext"; 
 1188:         }
 1189:         my $tokenurl=&Apache::lonnet::tokenwrapper($url);
 1190:         return $tokenurl;
 1191:     } else {
 1192:         if ($type eq 'thumbnail') {
 1193:             return '/adm/lonKaputt/genericstudent_tn.gif';
 1194:         } else { 
 1195:             return '/adm/lonKaputt/lonlogo_broken.gif';
 1196:         }
 1197:     }
 1198: }
 1199: 
 1200: # -------------------------------------------------------------------- New chat
 1201: 
 1202: sub chatsend {
 1203:     my ($newentry,$anon,$group)=@_;
 1204:     my $cnum=$env{'course.'.$env{'request.course.id'}.'.num'};
 1205:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 1206:     my $chome=$env{'course.'.$env{'request.course.id'}.'.home'};
 1207:     &reply('chatsend:'.$cdom.':'.$cnum.':'.
 1208: 	   &escape($env{'user.domain'}.':'.$env{'user.name'}.':'.$anon.':'.
 1209: 		   &escape($newentry)).':'.$group,$chome);
 1210: }
 1211: 
 1212: # ------------------------------------------ Find current version of a resource
 1213: 
 1214: sub getversion {
 1215:     my $fname=&clutter(shift);
 1216:     unless ($fname=~/^\/res\//) { return -1; }
 1217:     return &currentversion(&filelocation('',$fname));
 1218: }
 1219: 
 1220: sub currentversion {
 1221:     my $fname=shift;
 1222:     my ($result,$cached)=&is_cached_new('resversion',$fname);
 1223:     if (defined($cached)) { return $result; }
 1224:     my $author=$fname;
 1225:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
 1226:     my ($udom,$uname)=split(/\//,$author);
 1227:     my $home=homeserver($uname,$udom);
 1228:     if ($home eq 'no_host') { 
 1229:         return -1; 
 1230:     }
 1231:     my $answer=reply("currentversion:$fname",$home);
 1232:     if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
 1233: 	return -1;
 1234:     }
 1235:     return &do_cache_new('resversion',$fname,$answer,600);
 1236: }
 1237: 
 1238: # ----------------------------- Subscribe to a resource, return URL if possible
 1239: 
 1240: sub subscribe {
 1241:     my $fname=shift;
 1242:     if ($fname=~/\/(aboutme|syllabus|bulletinboard|smppg)$/) { return ''; }
 1243:     $fname=~s/[\n\r]//g;
 1244:     my $author=$fname;
 1245:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
 1246:     my ($udom,$uname)=split(/\//,$author);
 1247:     my $home=homeserver($uname,$udom);
 1248:     if ($home eq 'no_host') {
 1249:         return 'not_found';
 1250:     }
 1251:     my $answer=reply("sub:$fname",$home);
 1252:     if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
 1253: 	$answer.=' by '.$home;
 1254:     }
 1255:     return $answer;
 1256: }
 1257:     
 1258: # -------------------------------------------------------------- Replicate file
 1259: 
 1260: sub repcopy {
 1261:     my $filename=shift;
 1262:     $filename=~s/\/+/\//g;
 1263:     if ($filename=~m|^/home/httpd/html/adm/|) { return 'ok'; }
 1264:     if ($filename=~m|^/home/httpd/html/lonUsers/|) { return 'ok'; }
 1265:     if ($filename=~m|^/home/httpd/html/userfiles/| or
 1266: 	$filename=~m -^/*(uploaded|editupload)/-) { 
 1267: 	return &repcopy_userfile($filename);
 1268:     }
 1269:     $filename=~s/[\n\r]//g;
 1270:     my $transname="$filename.in.transfer";
 1271: # FIXME: this should flock
 1272:     if ((-e $filename) || (-e $transname)) { return 'ok'; }
 1273:     my $remoteurl=subscribe($filename);
 1274:     if ($remoteurl =~ /^con_lost by/) {
 1275: 	   &logthis("Subscribe returned $remoteurl: $filename");
 1276:            return 'unavailable';
 1277:     } elsif ($remoteurl eq 'not_found') {
 1278: 	   #&logthis("Subscribe returned not_found: $filename");
 1279: 	   return 'not_found';
 1280:     } elsif ($remoteurl =~ /^rejected by/) {
 1281: 	   &logthis("Subscribe returned $remoteurl: $filename");
 1282:            return 'forbidden';
 1283:     } elsif ($remoteurl eq 'directory') {
 1284:            return 'ok';
 1285:     } else {
 1286:         my $author=$filename;
 1287:         $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
 1288:         my ($udom,$uname)=split(/\//,$author);
 1289:         my $home=homeserver($uname,$udom);
 1290:         unless ($home eq $perlvar{'lonHostID'}) {
 1291:            my @parts=split(/\//,$filename);
 1292:            my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 1293:            if ($path ne "$perlvar{'lonDocRoot'}/res") {
 1294:                &logthis("Malconfiguration for replication: $filename");
 1295: 	       return 'bad_request';
 1296:            }
 1297:            my $count;
 1298:            for ($count=5;$count<$#parts;$count++) {
 1299:                $path.="/$parts[$count]";
 1300:                if ((-e $path)!=1) {
 1301: 		   mkdir($path,0777);
 1302:                }
 1303:            }
 1304:            my $ua=new LWP::UserAgent;
 1305:            my $request=new HTTP::Request('GET',"$remoteurl");
 1306:            my $response=$ua->request($request,$transname);
 1307:            if ($response->is_error()) {
 1308: 	       unlink($transname);
 1309:                my $message=$response->status_line;
 1310:                &logthis("<font color=\"blue\">WARNING:"
 1311:                        ." LWP get: $message: $filename</font>");
 1312:                return 'unavailable';
 1313:            } else {
 1314: 	       if ($remoteurl!~/\.meta$/) {
 1315:                   my $mrequest=new HTTP::Request('GET',$remoteurl.'.meta');
 1316:                   my $mresponse=$ua->request($mrequest,$filename.'.meta');
 1317:                   if ($mresponse->is_error()) {
 1318: 		      unlink($filename.'.meta');
 1319:                       &logthis(
 1320:                      "<font color=\"yellow\">INFO: No metadata: $filename</font>");
 1321:                   }
 1322: 	       }
 1323:                rename($transname,$filename);
 1324:                return 'ok';
 1325:            }
 1326:        }
 1327:     }
 1328: }
 1329: 
 1330: # ------------------------------------------------ Get server side include body
 1331: sub ssi_body {
 1332:     my ($filelink,%form)=@_;
 1333:     if (! exists($form{'LONCAPA_INTERNAL_no_discussion'})) {
 1334:         $form{'LONCAPA_INTERNAL_no_discussion'}='true';
 1335:     }
 1336:     my $output=($filelink=~/^http\:/?&externalssi($filelink):
 1337:                                      &ssi($filelink,%form));
 1338:     $output=~s|//(\s*<!--)? BEGIN LON-CAPA Internal.+?// END LON-CAPA Internal\s*(-->)?\s||gs;
 1339:     $output=~s/^.*?\<body[^\>]*\>//si;
 1340:     $output=~s/(.*)\<\/body\s*\>.*?$/$1/si;
 1341:     return $output;
 1342: }
 1343: 
 1344: # --------------------------------------------------------- Server Side Include
 1345: 
 1346: sub absolute_url {
 1347:     my ($host_name) = @_;
 1348:     my $protocol = ($ENV{'SERVER_PORT'} == 443?'https://':'http://');
 1349:     if ($host_name eq '') {
 1350: 	$host_name = $ENV{'SERVER_NAME'};
 1351:     }
 1352:     return $protocol.$host_name;
 1353: }
 1354: 
 1355: sub ssi {
 1356: 
 1357:     my ($fn,%form)=@_;
 1358: 
 1359:     my $ua=new LWP::UserAgent;
 1360:     
 1361:     my $request;
 1362: 
 1363:     $form{'no_update_last_known'}=1;
 1364: 
 1365:     if (%form) {
 1366:       $request=new HTTP::Request('POST',&absolute_url().$fn);
 1367:       $request->content(join('&',map { &escape($_).'='.&escape($form{$_}) } keys %form));
 1368:     } else {
 1369:       $request=new HTTP::Request('GET',&absolute_url().$fn);
 1370:     }
 1371: 
 1372:     $request->header(Cookie => $ENV{'HTTP_COOKIE'});
 1373:     my $response=$ua->request($request);
 1374: 
 1375:     return $response->content;
 1376: }
 1377: 
 1378: sub externalssi {
 1379:     my ($url)=@_;
 1380:     my $ua=new LWP::UserAgent;
 1381:     my $request=new HTTP::Request('GET',$url);
 1382:     my $response=$ua->request($request);
 1383:     return $response->content;
 1384: }
 1385: 
 1386: # -------------------------------- Allow a /uploaded/ URI to be vouched for
 1387: 
 1388: sub allowuploaded {
 1389:     my ($srcurl,$url)=@_;
 1390:     $url=&clutter(&declutter($url));
 1391:     my $dir=$url;
 1392:     $dir=~s/\/[^\/]+$//;
 1393:     my %httpref=();
 1394:     my $httpurl=&hreflocation('',$url);
 1395:     $httpref{'httpref.'.$httpurl}=$srcurl;
 1396:     &Apache::lonnet::appenv(%httpref);
 1397: }
 1398: 
 1399: # --------- File operations in /home/httpd/html/userfiles/$domain/1/2/3/$course
 1400: # input: action, courseID, current domain, intended
 1401: #        path to file, source of file, instruction to parse file for objects,
 1402: #        ref to hash for embedded objects,
 1403: #        ref to hash for codebase of java objects.
 1404: #
 1405: # output: url to file (if action was uploaddoc), 
 1406: #         ok if successful, or diagnostic message otherwise (if action was propagate or copy)
 1407: #
 1408: # Allows directory structure to be used within lonUsers/../userfiles/ for a 
 1409: # course.
 1410: #
 1411: # action = propagate - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
 1412: #          will be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles in
 1413: #          course's home server.
 1414: #
 1415: # action = copy - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file will
 1416: #          be copied from $source (current location) to 
 1417: #          /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
 1418: #         and will then be copied to
 1419: #          /home/httpd/lonUsers/$domain/1/2/3/$course/userfiles/$file in
 1420: #         course's home server.
 1421: #
 1422: # action = uploaddoc - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
 1423: #         will be retrived from $env{form.uploaddoc} (from DOCS interface) to
 1424: #         /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
 1425: #         and will then be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles/$file
 1426: #         in course's home server.
 1427: #
 1428: 
 1429: sub process_coursefile {
 1430:     my ($action,$docuname,$docudom,$file,$source,$parser,$allfiles,$codebase)=@_;
 1431:     my $fetchresult;
 1432:     my $home=&homeserver($docuname,$docudom);
 1433:     if ($action eq 'propagate') {
 1434:         $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
 1435: 			     $home);
 1436:     } else {
 1437:         my $fpath = '';
 1438:         my $fname = $file;
 1439:         ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
 1440:         $fpath=$docudom.'/'.$docuname.'/'.$fpath;
 1441:         my $filepath = &build_filepath($fpath);
 1442:         if ($action eq 'copy') {
 1443:             if ($source eq '') {
 1444:                 $fetchresult = 'no source file';
 1445:                 return $fetchresult;
 1446:             } else {
 1447:                 my $destination = $filepath.'/'.$fname;
 1448:                 rename($source,$destination);
 1449:                 $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
 1450:                                  $home);
 1451:             }
 1452:         } elsif ($action eq 'uploaddoc') {
 1453:             open(my $fh,'>'.$filepath.'/'.$fname);
 1454:             print $fh $env{'form.'.$source};
 1455:             close($fh);
 1456:             if ($parser eq 'parse') {
 1457:                 my $parse_result = &extract_embedded_items($filepath,$fname,$allfiles,$codebase);
 1458:                 unless ($parse_result eq 'ok') {
 1459:                     &logthis('Failed to parse '.$filepath.'/'.$fname.' for embedded media: '.$parse_result);
 1460:                 }
 1461:             }
 1462:             $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
 1463:                                  $home);
 1464:             if ($fetchresult eq 'ok') {
 1465:                 return '/uploaded/'.$fpath.'/'.$fname;
 1466:             } else {
 1467:                 &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
 1468:                         ' to host '.$home.': '.$fetchresult);
 1469:                 return '/adm/notfound.html';
 1470:             }
 1471:         }
 1472:     }
 1473:     unless ( $fetchresult eq 'ok') {
 1474:         &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
 1475:              ' to host '.$home.': '.$fetchresult);
 1476:     }
 1477:     return $fetchresult;
 1478: }
 1479: 
 1480: sub build_filepath {
 1481:     my ($fpath) = @_;
 1482:     my $filepath=$perlvar{'lonDocRoot'}.'/userfiles';
 1483:     unless ($fpath eq '') {
 1484:         my @parts=split('/',$fpath);
 1485:         foreach my $part (@parts) {
 1486:             $filepath.= '/'.$part;
 1487:             if ((-e $filepath)!=1) {
 1488:                 mkdir($filepath,0777);
 1489:             }
 1490:         }
 1491:     }
 1492:     return $filepath;
 1493: }
 1494: 
 1495: sub store_edited_file {
 1496:     my ($primary_url,$content,$docudom,$docuname,$fetchresult) = @_;
 1497:     my $file = $primary_url;
 1498:     $file =~ s#^/uploaded/$docudom/$docuname/##;
 1499:     my $fpath = '';
 1500:     my $fname = $file;
 1501:     ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
 1502:     $fpath=$docudom.'/'.$docuname.'/'.$fpath;
 1503:     my $filepath = &build_filepath($fpath);
 1504:     open(my $fh,'>'.$filepath.'/'.$fname);
 1505:     print $fh $content;
 1506:     close($fh);
 1507:     my $home=&homeserver($docuname,$docudom);
 1508:     $$fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
 1509: 			  $home);
 1510:     if ($$fetchresult eq 'ok') {
 1511:         return '/uploaded/'.$fpath.'/'.$fname;
 1512:     } else {
 1513:         &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
 1514: 		 ' to host '.$home.': '.$$fetchresult);
 1515:         return '/adm/notfound.html';
 1516:     }
 1517: }
 1518: 
 1519: sub clean_filename {
 1520:     my ($fname,$args)=@_;
 1521: # Replace Windows backslashes by forward slashes
 1522:     $fname=~s/\\/\//g;
 1523:     if (!$args->{'keep_path'}) {
 1524:         # Get rid of everything but the actual filename
 1525: 	$fname=~s/^.*\/([^\/]+)$/$1/;
 1526:     }
 1527: # Replace spaces by underscores
 1528:     $fname=~s/\s+/\_/g;
 1529: # Replace all other weird characters by nothing
 1530:     $fname=~s{[^/\w\.\-]}{}g;
 1531: # Replace all .\d. sequences with _\d. so they no longer look like version
 1532: # numbers
 1533:     $fname=~s/\.(\d+)(?=\.)/_$1/g;
 1534:     return $fname;
 1535: }
 1536: 
 1537: # --------------- Take an uploaded file and put it into the userfiles directory
 1538: # input: $formname - the contents of the file are in $env{"form.$formname"}
 1539: #                    the desired filenam is in $env{"form.$formname.filename"}
 1540: #        $coursedoc - if true up to the current course
 1541: #                     if false
 1542: #        $subdir - directory in userfile to store the file into
 1543: #        $parser - instruction to parse file for objects ($parser = parse)    
 1544: #        $allfiles - reference to hash for embedded objects
 1545: #        $codebase - reference to hash for codebase of java objects
 1546: #        $desuname - username for permanent storage of uploaded file
 1547: #        $dsetudom - domain for permanaent storage of uploaded file
 1548: #        $thumbwidth - width (pixels) of thumbnail to make for uploaded image 
 1549: #        $thumbheight - height (pixels) of thumbnail to make for uploaded image
 1550: # 
 1551: # output: url of file in userspace, or error: <message> 
 1552: #             or /adm/notfound.html if failure to upload occurse
 1553: 
 1554: 
 1555: sub userfileupload {
 1556:     my ($formname,$coursedoc,$subdir,$parser,$allfiles,$codebase,$destuname,
 1557:         $destudom,$thumbwidth,$thumbheight)=@_;
 1558:     if (!defined($subdir)) { $subdir='unknown'; }
 1559:     my $fname=$env{'form.'.$formname.'.filename'};
 1560:     $fname=&clean_filename($fname);
 1561: # See if there is anything left
 1562:     unless ($fname) { return 'error: no uploaded file'; }
 1563:     chop($env{'form.'.$formname});
 1564:     if (($formname eq 'screenshot') && ($subdir eq 'helprequests')) { #files uploaded to help request form are handled differently
 1565:         my $now = time;
 1566:         my $filepath = 'tmp/helprequests/'.$now;
 1567:         my @parts=split(/\//,$filepath);
 1568:         my $fullpath = $perlvar{'lonDaemons'};
 1569:         for (my $i=0;$i<@parts;$i++) {
 1570:             $fullpath .= '/'.$parts[$i];
 1571:             if ((-e $fullpath)!=1) {
 1572:                 mkdir($fullpath,0777);
 1573:             }
 1574:         }
 1575:         open(my $fh,'>'.$fullpath.'/'.$fname);
 1576:         print $fh $env{'form.'.$formname};
 1577:         close($fh);
 1578:         return $fullpath.'/'.$fname;
 1579:     } elsif (($formname eq 'coursecreatorxml') && ($subdir eq 'batchupload')) { #files uploaded to create course page are handled differently
 1580:         my $filepath = 'tmp/addcourse/'.$destudom.'/web/'.$env{'user.name'}.
 1581:                        '_'.$env{'user.domain'}.'/pending';
 1582:         my @parts=split(/\//,$filepath);
 1583:         my $fullpath = $perlvar{'lonDaemons'};
 1584:         for (my $i=0;$i<@parts;$i++) {
 1585:             $fullpath .= '/'.$parts[$i];
 1586:             if ((-e $fullpath)!=1) {
 1587:                 mkdir($fullpath,0777);
 1588:             }
 1589:         }
 1590:         open(my $fh,'>'.$fullpath.'/'.$fname);
 1591:         print $fh $env{'form.'.$formname};
 1592:         close($fh);
 1593:         return $fullpath.'/'.$fname;
 1594:     }
 1595:     
 1596: # Create the directory if not present
 1597:     $fname="$subdir/$fname";
 1598:     if ($coursedoc) {
 1599: 	my $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
 1600: 	my $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 1601:         if ($env{'form.folder'} =~ m/^(default|supplemental)/) {
 1602:             return &finishuserfileupload($docuname,$docudom,
 1603: 					 $formname,$fname,$parser,$allfiles,
 1604: 					 $codebase,$thumbwidth,$thumbheight);
 1605:         } else {
 1606:             $fname=$env{'form.folder'}.'/'.$fname;
 1607:             return &process_coursefile('uploaddoc',$docuname,$docudom,
 1608: 				       $fname,$formname,$parser,
 1609: 				       $allfiles,$codebase);
 1610:         }
 1611:     } elsif (defined($destuname)) {
 1612:         my $docuname=$destuname;
 1613:         my $docudom=$destudom;
 1614: 	return &finishuserfileupload($docuname,$docudom,$formname,$fname,
 1615: 				     $parser,$allfiles,$codebase,
 1616:                                      $thumbwidth,$thumbheight);
 1617:         
 1618:     } else {
 1619:         my $docuname=$env{'user.name'};
 1620:         my $docudom=$env{'user.domain'};
 1621:         if (exists($env{'form.group'})) {
 1622:             $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
 1623:             $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 1624:         }
 1625: 	return &finishuserfileupload($docuname,$docudom,$formname,$fname,
 1626: 				     $parser,$allfiles,$codebase,
 1627:                                      $thumbwidth,$thumbheight);
 1628:     }
 1629: }
 1630: 
 1631: sub finishuserfileupload {
 1632:     my ($docuname,$docudom,$formname,$fname,$parser,$allfiles,$codebase,
 1633:         $thumbwidth,$thumbheight) = @_;
 1634:     my $path=$docudom.'/'.$docuname.'/';
 1635:     my $filepath=$perlvar{'lonDocRoot'};
 1636:     my ($fnamepath,$file,$fetchthumb);
 1637:     $file=$fname;
 1638:     if ($fname=~m|/|) {
 1639:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 1640: 	$path.=$fnamepath.'/';
 1641:     }
 1642:     my @parts=split(/\//,$filepath.'/userfiles/'.$path);
 1643:     my $count;
 1644:     for ($count=4;$count<=$#parts;$count++) {
 1645:         $filepath.="/$parts[$count]";
 1646:         if ((-e $filepath)!=1) {
 1647: 	    mkdir($filepath,0777);
 1648:         }
 1649:     }
 1650: # Save the file
 1651:     {
 1652: 	if (!open(FH,'>'.$filepath.'/'.$file)) {
 1653: 	    &logthis('Failed to create '.$filepath.'/'.$file);
 1654: 	    print STDERR ('Failed to create '.$filepath.'/'.$file."\n");
 1655: 	    return '/adm/notfound.html';
 1656: 	}
 1657: 	if (!print FH ($env{'form.'.$formname})) {
 1658: 	    &logthis('Failed to write to '.$filepath.'/'.$file);
 1659: 	    print STDERR ('Failed to write to '.$filepath.'/'.$file."\n");
 1660: 	    return '/adm/notfound.html';
 1661: 	}
 1662: 	close(FH);
 1663:     }
 1664:     if ($parser eq 'parse') {
 1665:         my $parse_result = &extract_embedded_items($filepath,$file,$allfiles,
 1666: 						   $codebase);
 1667:         unless ($parse_result eq 'ok') {
 1668:             &logthis('Failed to parse '.$filepath.$file.
 1669: 		     ' for embedded media: '.$parse_result); 
 1670:         }
 1671:     }
 1672:     if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 1673:         my $input = $filepath.'/'.$file;
 1674:         my $output = $filepath.'/'.'tn-'.$file;
 1675:         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 1676:         system("convert -sample $thumbsize $input $output");
 1677:         if (-e $filepath.'/'.'tn-'.$file) {
 1678:             $fetchthumb  = 1; 
 1679:         }
 1680:     }
 1681:  
 1682: # Notify homeserver to grep it
 1683: #
 1684:     my $docuhome=&homeserver($docuname,$docudom);
 1685:     my $fetchresult= &reply('fetchuserfile:'.$path.$file,$docuhome);
 1686:     if ($fetchresult eq 'ok') {
 1687:         if ($fetchthumb) {
 1688:             my $thumbresult= &reply('fetchuserfile:'.$path.'tn-'.$file,$docuhome);
 1689:             if ($thumbresult ne 'ok') {
 1690:                 &logthis('Failed to transfer '.$path.'tn-'.$file.' to host '.
 1691:                          $docuhome.': '.$thumbresult);
 1692:             }
 1693:         }
 1694: #
 1695: # Return the URL to it
 1696:         return '/uploaded/'.$path.$file;
 1697:     } else {
 1698:         &logthis('Failed to transfer '.$path.$file.' to host '.$docuhome.
 1699: 		 ': '.$fetchresult);
 1700:         return '/adm/notfound.html';
 1701:     }
 1702: }
 1703: 
 1704: sub extract_embedded_items {
 1705:     my ($filepath,$file,$allfiles,$codebase,$content) = @_;
 1706:     my @state = ();
 1707:     my %javafiles = (
 1708:                       codebase => '',
 1709:                       code => '',
 1710:                       archive => ''
 1711:                     );
 1712:     my %mediafiles = (
 1713:                       src => '',
 1714:                       movie => '',
 1715:                      );
 1716:     my $p;
 1717:     if ($content) {
 1718:         $p = HTML::LCParser->new($content);
 1719:     } else {
 1720:         $p = HTML::LCParser->new($filepath.'/'.$file);
 1721:     }
 1722:     while (my $t=$p->get_token()) {
 1723: 	if ($t->[0] eq 'S') {
 1724: 	    my ($tagname, $attr) = ($t->[1],$t->[2]);
 1725: 	    push (@state, $tagname);
 1726:             if (lc($tagname) eq 'allow') {
 1727:                 &add_filetype($allfiles,$attr->{'src'},'src');
 1728:             }
 1729: 	    if (lc($tagname) eq 'img') {
 1730: 		&add_filetype($allfiles,$attr->{'src'},'src');
 1731: 	    }
 1732:             if (lc($tagname) eq 'script') {
 1733:                 if ($attr->{'archive'} =~ /\.jar$/i) {
 1734:                     &add_filetype($allfiles,$attr->{'archive'},'archive');
 1735:                 } else {
 1736:                     &add_filetype($allfiles,$attr->{'src'},'src');
 1737:                 }
 1738:             }
 1739:             if (lc($tagname) eq 'link') {
 1740:                 if (lc($attr->{'rel'}) eq 'stylesheet') { 
 1741:                     &add_filetype($allfiles,$attr->{'href'},'href');
 1742:                 }
 1743:             }
 1744: 	    if (lc($tagname) eq 'object' ||
 1745: 		(lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')) {
 1746: 		foreach my $item (keys(%javafiles)) {
 1747: 		    $javafiles{$item} = '';
 1748: 		}
 1749: 	    }
 1750: 	    if (lc($state[-2]) eq 'object' && lc($tagname) eq 'param') {
 1751: 		my $name = lc($attr->{'name'});
 1752: 		foreach my $item (keys(%javafiles)) {
 1753: 		    if ($name eq $item) {
 1754: 			$javafiles{$item} = $attr->{'value'};
 1755: 			last;
 1756: 		    }
 1757: 		}
 1758: 		foreach my $item (keys(%mediafiles)) {
 1759: 		    if ($name eq $item) {
 1760: 			&add_filetype($allfiles, $attr->{'value'}, 'value');
 1761: 			last;
 1762: 		    }
 1763: 		}
 1764: 	    }
 1765: 	    if (lc($tagname) eq 'embed' || lc($tagname) eq 'applet') {
 1766: 		foreach my $item (keys(%javafiles)) {
 1767: 		    if ($attr->{$item}) {
 1768: 			$javafiles{$item} = $attr->{$item};
 1769: 			last;
 1770: 		    }
 1771: 		}
 1772: 		foreach my $item (keys(%mediafiles)) {
 1773: 		    if ($attr->{$item}) {
 1774: 			&add_filetype($allfiles,$attr->{$item},$item);
 1775: 			last;
 1776: 		    }
 1777: 		}
 1778: 	    }
 1779: 	} elsif ($t->[0] eq 'E') {
 1780: 	    my ($tagname) = ($t->[1]);
 1781: 	    if ($javafiles{'codebase'} ne '') {
 1782: 		$javafiles{'codebase'} .= '/';
 1783: 	    }  
 1784: 	    if (lc($tagname) eq 'applet' ||
 1785: 		lc($tagname) eq 'object' ||
 1786: 		(lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')
 1787: 		) {
 1788: 		foreach my $item (keys(%javafiles)) {
 1789: 		    if ($item ne 'codebase' && $javafiles{$item} ne '') {
 1790: 			my $file=$javafiles{'codebase'}.$javafiles{$item};
 1791: 			&add_filetype($allfiles,$file,$item);
 1792: 		    }
 1793: 		}
 1794: 	    } 
 1795: 	    pop @state;
 1796: 	}
 1797:     }
 1798:     return 'ok';
 1799: }
 1800: 
 1801: sub add_filetype {
 1802:     my ($allfiles,$file,$type)=@_;
 1803:     if (exists($allfiles->{$file})) {
 1804: 	unless (grep/^\Q$type\E$/, @{$allfiles->{$file}}) {
 1805: 	    push(@{$allfiles->{$file}}, &escape($type));
 1806: 	}
 1807:     } else {
 1808: 	@{$allfiles->{$file}} = (&escape($type));
 1809:     }
 1810: }
 1811: 
 1812: sub removeuploadedurl {
 1813:     my ($url)=@_;
 1814:     my (undef,undef,$udom,$uname,$fname)=split('/',$url,5);
 1815:     return &removeuserfile($uname,$udom,$fname);
 1816: }
 1817: 
 1818: sub removeuserfile {
 1819:     my ($docuname,$docudom,$fname)=@_;
 1820:     my $home=&homeserver($docuname,$docudom);
 1821:     my $result = &reply("removeuserfile:$docudom/$docuname/$fname",$home);
 1822:     if ($result eq 'ok') {
 1823:         if (($fname !~ /\.meta$/) && (&is_portfolio_file($fname))) {
 1824:             my $metafile = $fname.'.meta';
 1825:             my $metaresult = &removeuserfile($docuname,$docudom,$metafile); 
 1826: 	    my $url = "/uploaded/$docudom/$docuname/$fname";
 1827:             my ($file,$group) = (&parse_portfolio_url($url))[3,4];
 1828:             my $sqlresult = 
 1829:                 &update_portfolio_table($docuname,$docudom,$file,
 1830:                                         'portfolio_metadata',$group,
 1831:                                         'delete');
 1832:         }
 1833:     }
 1834:     return $result;
 1835: }
 1836: 
 1837: sub mkdiruserfile {
 1838:     my ($docuname,$docudom,$dir)=@_;
 1839:     my $home=&homeserver($docuname,$docudom);
 1840:     return &reply("mkdiruserfile:".&escape("$docudom/$docuname/$dir"),$home);
 1841: }
 1842: 
 1843: sub renameuserfile {
 1844:     my ($docuname,$docudom,$old,$new)=@_;
 1845:     my $home=&homeserver($docuname,$docudom);
 1846:     my $result = &reply("renameuserfile:$docudom:$docuname:".
 1847:                         &escape("$old").':'.&escape("$new"),$home);
 1848:     if ($result eq 'ok') {
 1849:         if (($old !~ /\.meta$/) && (&is_portfolio_file($old))) {
 1850:             my $oldmeta = $old.'.meta';
 1851:             my $newmeta = $new.'.meta';
 1852:             my $metaresult = 
 1853:                 &renameuserfile($docuname,$docudom,$oldmeta,$newmeta);
 1854: 	    my $url = "/uploaded/$docudom/$docuname/$old";
 1855:             my ($file,$group) = (&parse_portfolio_url($url))[3,4];
 1856:             my $sqlresult = 
 1857:                 &update_portfolio_table($docuname,$docudom,$file,
 1858:                                         'portfolio_metadata',$group,
 1859:                                         'delete');
 1860:         }
 1861:     }
 1862:     return $result;
 1863: }
 1864: 
 1865: # ------------------------------------------------------------------------- Log
 1866: 
 1867: sub log {
 1868:     my ($dom,$nam,$hom,$what)=@_;
 1869:     return critical("log:$dom:$nam:$what",$hom);
 1870: }
 1871: 
 1872: # ------------------------------------------------------------------ Course Log
 1873: #
 1874: # This routine flushes several buffers of non-mission-critical nature
 1875: #
 1876: 
 1877: sub flushcourselogs {
 1878:     &logthis('Flushing log buffers');
 1879: #
 1880: # course logs
 1881: # This is a log of all transactions in a course, which can be used
 1882: # for data mining purposes
 1883: #
 1884: # It also collects the courseid database, which lists last transaction
 1885: # times and course titles for all courseids
 1886: #
 1887:     my %courseidbuffer=();
 1888:     foreach my $crsid (keys %courselogs) {
 1889:         if (&reply('log:'.$coursedombuf{$crsid}.':'.$coursenumbuf{$crsid}.':'.
 1890: 		          &escape($courselogs{$crsid}),
 1891: 		          $coursehombuf{$crsid}) eq 'ok') {
 1892: 	    delete $courselogs{$crsid};
 1893:         } else {
 1894:             &logthis('Failed to flush log buffer for '.$crsid);
 1895:             if (length($courselogs{$crsid})>40000) {
 1896:                &logthis("<font color=\"blue\">WARNING: Buffer for ".$crsid.
 1897:                         " exceeded maximum size, deleting.</font>");
 1898:                delete $courselogs{$crsid};
 1899:             }
 1900:         }
 1901:         if ($courseidbuffer{$coursehombuf{$crsid}}) {
 1902:            $courseidbuffer{$coursehombuf{$crsid}}.='&'.
 1903: 			 &escape($crsid).'='.&escape($coursedescrbuf{$crsid}).
 1904:                          ':'.&escape($courseinstcodebuf{$crsid}).':'.&escape($courseownerbuf{$crsid}).':'.&escape($coursetypebuf{$crsid});
 1905:         } else {
 1906:            $courseidbuffer{$coursehombuf{$crsid}}=
 1907: 			 &escape($crsid).'='.&escape($coursedescrbuf{$crsid}).
 1908:                          ':'.&escape($courseinstcodebuf{$crsid}).':'.&escape($courseownerbuf{$crsid}).':'.&escape($coursetypebuf{$crsid});
 1909:         }
 1910:     }
 1911: #
 1912: # Write course id database (reverse lookup) to homeserver of courses 
 1913: # Is used in pickcourse
 1914: #
 1915:     foreach my $crs_home (keys(%courseidbuffer)) {
 1916:         &courseidput(&host_domain($crs_home),$courseidbuffer{$crs_home},
 1917: 		     $crs_home);
 1918:     }
 1919: #
 1920: # File accesses
 1921: # Writes to the dynamic metadata of resources to get hit counts, etc.
 1922: #
 1923:     foreach my $entry (keys(%accesshash)) {
 1924:         if ($entry =~ /___count$/) {
 1925:             my ($dom,$name);
 1926:             ($dom,$name,undef)=
 1927: 		($entry=~m{___($match_domain)/($match_name)/(.*)___count$});
 1928:             if (! defined($dom) || $dom eq '' || 
 1929:                 ! defined($name) || $name eq '') {
 1930:                 my $cid = $env{'request.course.id'};
 1931:                 $dom  = $env{'request.'.$cid.'.domain'};
 1932:                 $name = $env{'request.'.$cid.'.num'};
 1933:             }
 1934:             my $value = $accesshash{$entry};
 1935:             my (undef,$url,undef) = ($entry =~ /^(.*)___(.*)___count$/);
 1936:             my %temphash=($url => $value);
 1937:             my $result = &inc('nohist_accesscount',\%temphash,$dom,$name);
 1938:             if ($result eq 'ok') {
 1939:                 delete $accesshash{$entry};
 1940:             } elsif ($result eq 'unknown_cmd') {
 1941:                 # Target server has old code running on it.
 1942:                 my %temphash=($entry => $value);
 1943:                 if (&put('nohist_resevaldata',\%temphash,$dom,$name) eq 'ok') {
 1944:                     delete $accesshash{$entry};
 1945:                 }
 1946:             }
 1947:         } else {
 1948:             my ($dom,$name) = ($entry=~m{___($match_domain)/($match_name)/(.*)___(\w+)$});
 1949:             my %temphash=($entry => $accesshash{$entry});
 1950:             if (&put('nohist_resevaldata',\%temphash,$dom,$name) eq 'ok') {
 1951:                 delete $accesshash{$entry};
 1952:             }
 1953:         }
 1954:     }
 1955: #
 1956: # Roles
 1957: # Reverse lookup of user roles for course faculty/staff and co-authorship
 1958: #
 1959:     foreach my $entry (keys(%userrolehash)) {
 1960:         my ($role,$uname,$udom,$runame,$rudom,$rsec)=
 1961: 	    split(/\:/,$entry);
 1962:         if (&Apache::lonnet::put('nohist_userroles',
 1963:              { $role.':'.$uname.':'.$udom.':'.$rsec => $userrolehash{$entry} },
 1964:                 $rudom,$runame) eq 'ok') {
 1965: 	    delete $userrolehash{$entry};
 1966:         }
 1967:     }
 1968: #
 1969: # Reverse lookup of domain roles (dc, ad, li, sc, au)
 1970: #
 1971:     my %domrolebuffer = ();
 1972:     foreach my $entry (keys %domainrolehash) {
 1973:         my ($role,$uname,$udom,$runame,$rudom,$rsec)=split/:/,$entry;
 1974:         if ($domrolebuffer{$rudom}) {
 1975:             $domrolebuffer{$rudom}.='&'.&escape($entry).
 1976:                       '='.&escape($domainrolehash{$entry});
 1977:         } else {
 1978:             $domrolebuffer{$rudom}.=&escape($entry).
 1979:                       '='.&escape($domainrolehash{$entry});
 1980:         }
 1981:         delete $domainrolehash{$entry};
 1982:     }
 1983:     foreach my $dom (keys(%domrolebuffer)) {
 1984: 	my %servers = &get_servers($dom,'library');
 1985: 	foreach my $tryserver (keys(%servers)) {
 1986: 	    unless (&reply('domroleput:'.$dom.':'.
 1987: 			   $domrolebuffer{$dom},$tryserver) eq 'ok') {
 1988: 		&logthis('Put of domain roles failed for '.$dom.' and  '.$tryserver);
 1989: 	    }
 1990:         }
 1991:     }
 1992:     $dumpcount++;
 1993: }
 1994: 
 1995: sub courselog {
 1996:     my $what=shift;
 1997:     $what=time.':'.$what;
 1998:     unless ($env{'request.course.id'}) { return ''; }
 1999:     $coursedombuf{$env{'request.course.id'}}=
 2000:        $env{'course.'.$env{'request.course.id'}.'.domain'};
 2001:     $coursenumbuf{$env{'request.course.id'}}=
 2002:        $env{'course.'.$env{'request.course.id'}.'.num'};
 2003:     $coursehombuf{$env{'request.course.id'}}=
 2004:        $env{'course.'.$env{'request.course.id'}.'.home'};
 2005:     $coursedescrbuf{$env{'request.course.id'}}=
 2006:        $env{'course.'.$env{'request.course.id'}.'.description'};
 2007:     $courseinstcodebuf{$env{'request.course.id'}}=
 2008:        $env{'course.'.$env{'request.course.id'}.'.internal.coursecode'};
 2009:     $courseownerbuf{$env{'request.course.id'}}=
 2010:        $env{'course.'.$env{'request.course.id'}.'.internal.courseowner'};
 2011:     $coursetypebuf{$env{'request.course.id'}}=
 2012:        $env{'course.'.$env{'request.course.id'}.'.type'};
 2013:     if (defined $courselogs{$env{'request.course.id'}}) {
 2014: 	$courselogs{$env{'request.course.id'}}.='&'.$what;
 2015:     } else {
 2016: 	$courselogs{$env{'request.course.id'}}.=$what;
 2017:     }
 2018:     if (length($courselogs{$env{'request.course.id'}})>4048) {
 2019: 	&flushcourselogs();
 2020:     }
 2021: }
 2022: 
 2023: sub courseacclog {
 2024:     my $fnsymb=shift;
 2025:     unless ($env{'request.course.id'}) { return ''; }
 2026:     my $what=$fnsymb.':'.$env{'user.name'}.':'.$env{'user.domain'};
 2027:     if ($fnsymb=~/(problem|exam|quiz|assess|survey|form|task|page)$/) {
 2028:         $what.=':POST';
 2029:         # FIXME: Probably ought to escape things....
 2030: 	foreach my $key (keys(%env)) {
 2031:             if ($key=~/^form\.(.*)/) {
 2032: 		$what.=':'.$1.'='.$env{$key};
 2033:             }
 2034:         }
 2035:     } elsif ($fnsymb =~ m:^/adm/searchcat:) {
 2036:         # FIXME: We should not be depending on a form parameter that someone
 2037:         # editing lonsearchcat.pm might change in the future.
 2038:         if ($env{'form.phase'} eq 'course_search') {
 2039:             $what.= ':POST';
 2040:             # FIXME: Probably ought to escape things....
 2041:             foreach my $element ('courseexp','crsfulltext','crsrelated',
 2042:                                  'crsdiscuss') {
 2043:                 $what.=':'.$element.'='.$env{'form.'.$element};
 2044:             }
 2045:         }
 2046:     }
 2047:     &courselog($what);
 2048: }
 2049: 
 2050: sub countacc {
 2051:     my $url=&declutter(shift);
 2052:     return if (! defined($url) || $url eq '');
 2053:     unless ($env{'request.course.id'}) { return ''; }
 2054:     $accesshash{$env{'request.course.id'}.'___'.$url.'___course'}=1;
 2055:     my $key=$$.$processmarker.'_'.$dumpcount.'___'.$url.'___count';
 2056:     $accesshash{$key}++;
 2057: }
 2058: 
 2059: sub linklog {
 2060:     my ($from,$to)=@_;
 2061:     $from=&declutter($from);
 2062:     $to=&declutter($to);
 2063:     $accesshash{$from.'___'.$to.'___comefrom'}=1;
 2064:     $accesshash{$to.'___'.$from.'___goto'}=1;
 2065: }
 2066:   
 2067: sub userrolelog {
 2068:     my ($trole,$username,$domain,$area,$tstart,$tend)=@_;
 2069:     if (($trole=~/^ca/) || ($trole=~/^aa/) ||
 2070:         ($trole=~/^in/) || ($trole=~/^cc/) ||
 2071:         ($trole=~/^ep/) || ($trole=~/^cr/) ||
 2072:         ($trole=~/^ta/)) {
 2073:        my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
 2074:        $userrolehash
 2075:          {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
 2076:                     =$tend.':'.$tstart;
 2077:     }
 2078:     if (($trole=~/^dc/) || ($trole=~/^ad/) ||
 2079:         ($trole=~/^li/) || ($trole=~/^li/) ||
 2080:         ($trole=~/^au/) || ($trole=~/^dg/) ||
 2081:         ($trole=~/^sc/)) {
 2082:        my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
 2083:        $domainrolehash
 2084:          {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
 2085:                     = $tend.':'.$tstart;
 2086:     }
 2087: }
 2088: 
 2089: sub get_course_adv_roles {
 2090:     my $cid=shift;
 2091:     $cid=$env{'request.course.id'} unless (defined($cid));
 2092:     my %coursehash=&coursedescription($cid);
 2093:     my %nothide=();
 2094:     foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
 2095: 	$nothide{join(':',split(/[\@\:]/,$user))}=1;
 2096:     }
 2097:     my %returnhash=();
 2098:     my %dumphash=
 2099:             &dump('nohist_userroles',$coursehash{'domain'},$coursehash{'num'});
 2100:     my $now=time;
 2101:     foreach my $entry (keys %dumphash) {
 2102: 	my ($tend,$tstart)=split(/\:/,$dumphash{$entry});
 2103:         if (($tstart) && ($tstart<0)) { next; }
 2104:         if (($tend) && ($tend<$now)) { next; }
 2105:         if (($tstart) && ($now<$tstart)) { next; }
 2106:         my ($role,$username,$domain,$section)=split(/\:/,$entry);
 2107: 	if ($username eq '' || $domain eq '') { next; }
 2108: 	if ((&privileged($username,$domain)) && 
 2109: 	    (!$nothide{$username.':'.$domain})) { next; }
 2110: 	if ($role eq 'cr') { next; }
 2111:         my $key=&plaintext($role);
 2112:         if ($section) { $key.=' (Sec/Grp '.$section.')'; }
 2113:         if ($returnhash{$key}) {
 2114: 	    $returnhash{$key}.=','.$username.':'.$domain;
 2115:         } else {
 2116:             $returnhash{$key}=$username.':'.$domain;
 2117:         }
 2118:      }
 2119:     return %returnhash;
 2120: }
 2121: 
 2122: sub get_my_roles {
 2123:     my ($uname,$udom,$context,$types,$roles,$roledoms)=@_;
 2124:     unless (defined($uname)) { $uname=$env{'user.name'}; }
 2125:     unless (defined($udom)) { $udom=$env{'user.domain'}; }
 2126:     my %dumphash;
 2127:     if ($context eq 'userroles') { 
 2128:         %dumphash = &dump('roles',$udom,$uname);
 2129:     } else {
 2130:         %dumphash=
 2131:             &dump('nohist_userroles',$udom,$uname);
 2132:     }
 2133:     my %returnhash=();
 2134:     my $now=time;
 2135:     foreach my $entry (keys(%dumphash)) {
 2136: 	my ($tend,$tstart)=split(/\:/,$dumphash{$entry});
 2137:         if (($tstart) && ($tstart<0)) { next; }
 2138:         my $status = 'active';
 2139:         if (($tend) && ($tend<$now)) {
 2140:             $status = 'previous';
 2141:         } 
 2142:         if (($tstart) && ($now<$tstart)) {
 2143:             $status = 'future';
 2144:         }
 2145:         if (ref($types) eq 'ARRAY') {
 2146:             if (!grep(/^\Q$status\E$/,@{$types})) {
 2147:                 next;
 2148:             } 
 2149:         } else {
 2150:             if ($status ne 'active') {
 2151:                 next;
 2152:             }
 2153:         }
 2154:         my ($role,$username,$domain,$section)=split(/\:/,$entry);
 2155:         if (ref($roledoms) eq 'ARRAY') {
 2156:             if (!grep(/^\Q$domain\E$/,@{$roledoms})) {
 2157:                 next;
 2158:             }
 2159:         }
 2160:         if (ref($roles) eq 'ARRAY') {
 2161:             if (!grep(/^\Q$role\E$/,@{$roles})) {
 2162:                 next;
 2163:             }
 2164:         } 
 2165: 	$returnhash{$username.':'.$domain.':'.$role}=$tstart.':'.$tend;
 2166:     }
 2167:     return %returnhash;
 2168: }
 2169: 
 2170: # ----------------------------------------------------- Frontpage Announcements
 2171: #
 2172: #
 2173: 
 2174: sub postannounce {
 2175:     my ($server,$text)=@_;
 2176:     unless (&allowed('psa',&host_domain($server))) { return 'refused'; }
 2177:     unless ($text=~/\w/) { $text=''; }
 2178:     return &reply('setannounce:'.&escape($text),$server);
 2179: }
 2180: 
 2181: sub getannounce {
 2182: 
 2183:     if (open(my $fh,$perlvar{'lonDocRoot'}.'/announcement.txt')) {
 2184: 	my $announcement='';
 2185: 	while (my $line = <$fh>) { $announcement .= $line; }
 2186: 	close($fh);
 2187: 	if ($announcement=~/\w/) { 
 2188: 	    return 
 2189:    '<table bgcolor="#FF5555" cellpadding="5" cellspacing="3">'.
 2190:    '<tr><td bgcolor="#FFFFFF"><tt>'.$announcement.'</tt></td></tr></table>'; 
 2191: 	} else {
 2192: 	    return '';
 2193: 	}
 2194:     } else {
 2195: 	return '';
 2196:     }
 2197: }
 2198: 
 2199: # ---------------------------------------------------------- Course ID routines
 2200: # Deal with domain's nohist_courseid.db files
 2201: #
 2202: 
 2203: sub courseidput {
 2204:     my ($domain,$what,$coursehome)=@_;
 2205:     return &reply('courseidput:'.$domain.':'.$what,$coursehome);
 2206: }
 2207: 
 2208: sub courseiddump {
 2209:     my ($domfilter,$descfilter,$sincefilter,$instcodefilter,$ownerfilter,$coursefilter,$hostidflag,$hostidref,$typefilter,$regexp_ok)=@_;
 2210:     my %returnhash=();
 2211:     unless ($domfilter) { $domfilter=''; }
 2212:     my %libserv = &all_library();
 2213:     foreach my $tryserver (keys(%libserv)) {
 2214:         if ( (  $hostidflag == 1 
 2215: 	        && grep(/^\Q$tryserver\E$/,@{$hostidref}) ) 
 2216: 	     || (!defined($hostidflag)) ) {
 2217: 
 2218: 	    if ($domfilter eq ''
 2219: 		|| (&host_domain($tryserver) eq $domfilter)) {
 2220: 	        foreach my $line (
 2221:                  split(/\&/,&reply('courseiddump:'.&host_domain($tryserver).':'.
 2222: 			       $sincefilter.':'.&escape($descfilter).':'.
 2223:                                &escape($instcodefilter).':'.&escape($ownerfilter).':'.&escape($coursefilter).':'.&escape($typefilter).':'.&escape($regexp_ok),
 2224:                                $tryserver))) {
 2225: 		    my ($key,$value)=split(/\=/,$line,2);
 2226:                     if (($key) && ($value)) {
 2227: 		        $returnhash{&unescape($key)}=$value;
 2228:                     }
 2229:                 }
 2230:             }
 2231:         }
 2232:     }
 2233:     return %returnhash;
 2234: }
 2235: 
 2236: # ---------------------------------------------------------- DC e-mail
 2237: 
 2238: sub dcmailput {
 2239:     my ($domain,$msgid,$message,$server)=@_;
 2240:     my $status = &Apache::lonnet::critical(
 2241:        'dcmailput:'.$domain.':'.&escape($msgid).'='.
 2242:        &escape($message),$server);
 2243:     return $status;
 2244: }
 2245: 
 2246: sub dcmaildump {
 2247:     my ($dom,$startdate,$enddate,$senders) = @_;
 2248:     my %returnhash=();
 2249: 
 2250:     if (defined(&domain($dom,'primary'))) {
 2251:         my $cmd='dcmaildump:'.$dom.':'.&escape($startdate).':'.
 2252:                                                          &escape($enddate).':';
 2253: 	my @esc_senders=map { &escape($_)} @$senders;
 2254: 	$cmd.=&escape(join('&',@esc_senders));
 2255: 	foreach my $line (split(/\&/,&reply($cmd,&domain($dom,'primary')))) {
 2256:             my ($key,$value) = split(/\=/,$line,2);
 2257:             if (($key) && ($value)) {
 2258:                 $returnhash{&unescape($key)} = &unescape($value);
 2259:             }
 2260:         }
 2261:     }
 2262:     return %returnhash;
 2263: }
 2264: # ---------------------------------------------------------- Domain roles
 2265: 
 2266: sub get_domain_roles {
 2267:     my ($dom,$roles,$startdate,$enddate)=@_;
 2268:     if (undef($startdate) || $startdate eq '') {
 2269:         $startdate = '.';
 2270:     }
 2271:     if (undef($enddate) || $enddate eq '') {
 2272:         $enddate = '.';
 2273:     }
 2274:     my $rolelist = join(':',@{$roles});
 2275:     my %personnel = ();
 2276: 
 2277:     my %servers = &get_servers($dom,'library');
 2278:     foreach my $tryserver (keys(%servers)) {
 2279: 	%{$personnel{$tryserver}}=();
 2280: 	foreach my $line (split(/\&/,&reply('domrolesdump:'.$dom.':'.
 2281: 					    &escape($startdate).':'.
 2282: 					    &escape($enddate).':'.
 2283: 					    &escape($rolelist), $tryserver))) {
 2284: 	    my ($key,$value) = split(/\=/,$line,2);
 2285: 	    if (($key) && ($value)) {
 2286: 		$personnel{$tryserver}{&unescape($key)} = &unescape($value);
 2287: 	    }
 2288: 	}
 2289:     }
 2290:     return %personnel;
 2291: }
 2292: 
 2293: # ----------------------------------------------------------- Check out an item
 2294: 
 2295: sub get_first_access {
 2296:     my ($type,$argsymb)=@_;
 2297:     my ($symb,$courseid,$udom,$uname)=&whichuser();
 2298:     if ($argsymb) { $symb=$argsymb; }
 2299:     my ($map,$id,$res)=&decode_symb($symb);
 2300:     if ($type eq 'map') {
 2301: 	$res=&symbread($map);
 2302:     } else {
 2303: 	$res=$symb;
 2304:     }
 2305:     my %times=&get('firstaccesstimes',["$courseid\0$res"],$udom,$uname);
 2306:     return $times{"$courseid\0$res"};
 2307: }
 2308: 
 2309: sub set_first_access {
 2310:     my ($type)=@_;
 2311:     my ($symb,$courseid,$udom,$uname)=&whichuser();
 2312:     my ($map,$id,$res)=&decode_symb($symb);
 2313:     if ($type eq 'map') {
 2314: 	$res=&symbread($map);
 2315:     } else {
 2316: 	$res=$symb;
 2317:     }
 2318:     my $firstaccess=&get_first_access($type,$symb);
 2319:     if (!$firstaccess) {
 2320: 	return &put('firstaccesstimes',{"$courseid\0$res"=>time},$udom,$uname);
 2321:     }
 2322:     return 'already_set';
 2323: }
 2324: 
 2325: sub checkout {
 2326:     my ($symb,$tuname,$tudom,$tcrsid)=@_;
 2327:     my $now=time;
 2328:     my $lonhost=$perlvar{'lonHostID'};
 2329:     my $infostr=&escape(
 2330:                  'CHECKOUTTOKEN&'.
 2331:                  $tuname.'&'.
 2332:                  $tudom.'&'.
 2333:                  $tcrsid.'&'.
 2334:                  $symb.'&'.
 2335: 		 $now.'&'.$ENV{'REMOTE_ADDR'});
 2336:     my $token=&reply('tmpput:'.$infostr,$lonhost);
 2337:     if ($token=~/^error\:/) { 
 2338:         &logthis("<font color=\"blue\">WARNING: ".
 2339:                 "Checkout tmpput failed ".$tudom.' - '.$tuname.' - '.$symb.
 2340:                  "</font>");
 2341:         return ''; 
 2342:     }
 2343: 
 2344:     $token=~s/^(\d+)\_.*\_(\d+)$/$1\*$2\*$lonhost/;
 2345:     $token=~tr/a-z/A-Z/;
 2346: 
 2347:     my %infohash=('resource.0.outtoken' => $token,
 2348:                   'resource.0.checkouttime' => $now,
 2349:                   'resource.0.outremote' => $ENV{'REMOTE_ADDR'});
 2350: 
 2351:     unless (&cstore(\%infohash,$symb,$tcrsid,$tudom,$tuname) eq 'ok') {
 2352:        return '';
 2353:     } else {
 2354:         &logthis("<font color=\"blue\">WARNING: ".
 2355:                 "Checkout cstore failed ".$tudom.' - '.$tuname.' - '.$symb.
 2356:                  "</font>");
 2357:     }    
 2358: 
 2359:     if (&log($tudom,$tuname,&homeserver($tuname,$tudom),
 2360:                          &escape('Checkout '.$infostr.' - '.
 2361:                                                  $token)) ne 'ok') {
 2362: 	return '';
 2363:     } else {
 2364:         &logthis("<font color=\"blue\">WARNING: ".
 2365:                 "Checkout log failed ".$tudom.' - '.$tuname.' - '.$symb.
 2366:                  "</font>");
 2367:     }
 2368:     return $token;
 2369: }
 2370: 
 2371: # ------------------------------------------------------------ Check in an item
 2372: 
 2373: sub checkin {
 2374:     my $token=shift;
 2375:     my $now=time;
 2376:     my ($ta,$tb,$lonhost)=split(/\*/,$token);
 2377:     $lonhost=~tr/A-Z/a-z/;
 2378:     my $dtoken=$ta.'_'.&hostname($lonhost).'_'.$tb;
 2379:     $dtoken=~s/\W/\_/g;
 2380:     my ($dummy,$tuname,$tudom,$tcrsid,$symb,$chtim,$rmaddr)=
 2381:                  split(/\&/,&unescape(&reply('tmpget:'.$dtoken,$lonhost)));
 2382: 
 2383:     unless (($tuname) && ($tudom)) {
 2384:         &logthis('Check in '.$token.' ('.$dtoken.') failed');
 2385:         return '';
 2386:     }
 2387:     
 2388:     unless (&allowed('mgr',$tcrsid)) {
 2389:         &logthis('Check in '.$token.' ('.$dtoken.') unauthorized: '.
 2390:                  $env{'user.name'}.' - '.$env{'user.domain'});
 2391:         return '';
 2392:     }
 2393: 
 2394:     my %infohash=('resource.0.intoken' => $token,
 2395:                   'resource.0.checkintime' => $now,
 2396:                   'resource.0.inremote' => $ENV{'REMOTE_ADDR'});
 2397: 
 2398:     unless (&cstore(\%infohash,$symb,$tcrsid,$tudom,$tuname) eq 'ok') {
 2399:        return '';
 2400:     }    
 2401: 
 2402:     if (&log($tudom,$tuname,&homeserver($tuname,$tudom),
 2403:                          &escape('Checkin - '.$token)) ne 'ok') {
 2404: 	return '';
 2405:     }
 2406: 
 2407:     return ($symb,$tuname,$tudom,$tcrsid);    
 2408: }
 2409: 
 2410: # --------------------------------------------- Set Expire Date for Spreadsheet
 2411: 
 2412: sub expirespread {
 2413:     my ($uname,$udom,$stype,$usymb)=@_;
 2414:     my $cid=$env{'request.course.id'}; 
 2415:     if ($cid) {
 2416:        my $now=time;
 2417:        my $key=$uname.':'.$udom.':'.$stype.':'.$usymb;
 2418:        return &reply('put:'.$env{'course.'.$cid.'.domain'}.':'.
 2419:                             $env{'course.'.$cid.'.num'}.
 2420: 	        	    ':nohist_expirationdates:'.
 2421:                             &escape($key).'='.$now,
 2422:                             $env{'course.'.$cid.'.home'})
 2423:     }
 2424:     return 'ok';
 2425: }
 2426: 
 2427: # ----------------------------------------------------- Devalidate Spreadsheets
 2428: 
 2429: sub devalidate {
 2430:     my ($symb,$uname,$udom)=@_;
 2431:     my $cid=$env{'request.course.id'}; 
 2432:     if ($cid) {
 2433:         # delete the stored spreadsheets for
 2434:         # - the student level sheet of this user in course's homespace
 2435:         # - the assessment level sheet for this resource 
 2436:         #   for this user in user's homespace
 2437: 	# - current conditional state info
 2438: 	my $key=$uname.':'.$udom.':';
 2439:         my $status=
 2440: 	    &del('nohist_calculatedsheets',
 2441: 		 [$key.'studentcalc:'],
 2442: 		 $env{'course.'.$cid.'.domain'},
 2443: 		 $env{'course.'.$cid.'.num'})
 2444: 		.' '.
 2445: 	    &del('nohist_calculatedsheets_'.$cid,
 2446: 		 [$key.'assesscalc:'.$symb],$udom,$uname);
 2447:         unless ($status eq 'ok ok') {
 2448:            &logthis('Could not devalidate spreadsheet '.
 2449:                     $uname.' at '.$udom.' for '.
 2450: 		    $symb.': '.$status);
 2451:         }
 2452: 	&delenv('user.state.'.$cid);
 2453:     }
 2454: }
 2455: 
 2456: sub get_scalar {
 2457:     my ($string,$end) = @_;
 2458:     my $value;
 2459:     if ($$string =~ s/^([^&]*?)($end)/$2/) {
 2460: 	$value = $1;
 2461:     } elsif ($$string =~ s/^([^&]*?)&//) {
 2462: 	$value = $1;
 2463:     }
 2464:     return &unescape($value);
 2465: }
 2466: 
 2467: sub array2str {
 2468:   my (@array) = @_;
 2469:   my $result=&arrayref2str(\@array);
 2470:   $result=~s/^__ARRAY_REF__//;
 2471:   $result=~s/__END_ARRAY_REF__$//;
 2472:   return $result;
 2473: }
 2474: 
 2475: sub arrayref2str {
 2476:   my ($arrayref) = @_;
 2477:   my $result='__ARRAY_REF__';
 2478:   foreach my $elem (@$arrayref) {
 2479:     if(ref($elem) eq 'ARRAY') {
 2480:       $result.=&arrayref2str($elem).'&';
 2481:     } elsif(ref($elem) eq 'HASH') {
 2482:       $result.=&hashref2str($elem).'&';
 2483:     } elsif(ref($elem)) {
 2484:       #print("Got a ref of ".(ref($elem))." skipping.");
 2485:     } else {
 2486:       $result.=&escape($elem).'&';
 2487:     }
 2488:   }
 2489:   $result=~s/\&$//;
 2490:   $result .= '__END_ARRAY_REF__';
 2491:   return $result;
 2492: }
 2493: 
 2494: sub hash2str {
 2495:   my (%hash) = @_;
 2496:   my $result=&hashref2str(\%hash);
 2497:   $result=~s/^__HASH_REF__//;
 2498:   $result=~s/__END_HASH_REF__$//;
 2499:   return $result;
 2500: }
 2501: 
 2502: sub hashref2str {
 2503:   my ($hashref)=@_;
 2504:   my $result='__HASH_REF__';
 2505:   foreach my $key (sort(keys(%$hashref))) {
 2506:     if (ref($key) eq 'ARRAY') {
 2507:       $result.=&arrayref2str($key).'=';
 2508:     } elsif (ref($key) eq 'HASH') {
 2509:       $result.=&hashref2str($key).'=';
 2510:     } elsif (ref($key)) {
 2511:       $result.='=';
 2512:       #print("Got a ref of ".(ref($key))." skipping.");
 2513:     } else {
 2514: 	if ($key) {$result.=&escape($key).'=';} else { last; }
 2515:     }
 2516: 
 2517:     if(ref($hashref->{$key}) eq 'ARRAY') {
 2518:       $result.=&arrayref2str($hashref->{$key}).'&';
 2519:     } elsif(ref($hashref->{$key}) eq 'HASH') {
 2520:       $result.=&hashref2str($hashref->{$key}).'&';
 2521:     } elsif(ref($hashref->{$key})) {
 2522:        $result.='&';
 2523:       #print("Got a ref of ".(ref($hashref->{$key}))." skipping.");
 2524:     } else {
 2525:       $result.=&escape($hashref->{$key}).'&';
 2526:     }
 2527:   }
 2528:   $result=~s/\&$//;
 2529:   $result .= '__END_HASH_REF__';
 2530:   return $result;
 2531: }
 2532: 
 2533: sub str2hash {
 2534:     my ($string)=@_;
 2535:     my ($hash)=&str2hashref('__HASH_REF__'.$string.'__END_HASH_REF__');
 2536:     return %$hash;
 2537: }
 2538: 
 2539: sub str2hashref {
 2540:   my ($string) = @_;
 2541: 
 2542:   my %hash;
 2543: 
 2544:   if($string !~ /^__HASH_REF__/) {
 2545:       if (! ($string eq '' || !defined($string))) {
 2546: 	  $hash{'error'}='Not hash reference';
 2547:       }
 2548:       return (\%hash, $string);
 2549:   }
 2550: 
 2551:   $string =~ s/^__HASH_REF__//;
 2552: 
 2553:   while($string !~ /^__END_HASH_REF__/) {
 2554:       #key
 2555:       my $key='';
 2556:       if($string =~ /^__HASH_REF__/) {
 2557:           ($key, $string)=&str2hashref($string);
 2558:           if(defined($key->{'error'})) {
 2559:               $hash{'error'}='Bad data';
 2560:               return (\%hash, $string);
 2561:           }
 2562:       } elsif($string =~ /^__ARRAY_REF__/) {
 2563:           ($key, $string)=&str2arrayref($string);
 2564:           if($key->[0] eq 'Array reference error') {
 2565:               $hash{'error'}='Bad data';
 2566:               return (\%hash, $string);
 2567:           }
 2568:       } else {
 2569:           $string =~ s/^(.*?)=//;
 2570: 	  $key=&unescape($1);
 2571:       }
 2572:       $string =~ s/^=//;
 2573: 
 2574:       #value
 2575:       my $value='';
 2576:       if($string =~ /^__HASH_REF__/) {
 2577:           ($value, $string)=&str2hashref($string);
 2578:           if(defined($value->{'error'})) {
 2579:               $hash{'error'}='Bad data';
 2580:               return (\%hash, $string);
 2581:           }
 2582:       } elsif($string =~ /^__ARRAY_REF__/) {
 2583:           ($value, $string)=&str2arrayref($string);
 2584:           if($value->[0] eq 'Array reference error') {
 2585:               $hash{'error'}='Bad data';
 2586:               return (\%hash, $string);
 2587:           }
 2588:       } else {
 2589: 	  $value=&get_scalar(\$string,'__END_HASH_REF__');
 2590:       }
 2591:       $string =~ s/^&//;
 2592: 
 2593:       $hash{$key}=$value;
 2594:   }
 2595: 
 2596:   $string =~ s/^__END_HASH_REF__//;
 2597: 
 2598:   return (\%hash, $string);
 2599: }
 2600: 
 2601: sub str2array {
 2602:     my ($string)=@_;
 2603:     my ($array)=&str2arrayref('__ARRAY_REF__'.$string.'__END_ARRAY_REF__');
 2604:     return @$array;
 2605: }
 2606: 
 2607: sub str2arrayref {
 2608:   my ($string) = @_;
 2609:   my @array;
 2610: 
 2611:   if($string !~ /^__ARRAY_REF__/) {
 2612:       if (! ($string eq '' || !defined($string))) {
 2613: 	  $array[0]='Array reference error';
 2614:       }
 2615:       return (\@array, $string);
 2616:   }
 2617: 
 2618:   $string =~ s/^__ARRAY_REF__//;
 2619: 
 2620:   while($string !~ /^__END_ARRAY_REF__/) {
 2621:       my $value='';
 2622:       if($string =~ /^__HASH_REF__/) {
 2623:           ($value, $string)=&str2hashref($string);
 2624:           if(defined($value->{'error'})) {
 2625:               $array[0] ='Array reference error';
 2626:               return (\@array, $string);
 2627:           }
 2628:       } elsif($string =~ /^__ARRAY_REF__/) {
 2629:           ($value, $string)=&str2arrayref($string);
 2630:           if($value->[0] eq 'Array reference error') {
 2631:               $array[0] ='Array reference error';
 2632:               return (\@array, $string);
 2633:           }
 2634:       } else {
 2635: 	  $value=&get_scalar(\$string,'__END_ARRAY_REF__');
 2636:       }
 2637:       $string =~ s/^&//;
 2638: 
 2639:       push(@array, $value);
 2640:   }
 2641: 
 2642:   $string =~ s/^__END_ARRAY_REF__//;
 2643: 
 2644:   return (\@array, $string);
 2645: }
 2646: 
 2647: # -------------------------------------------------------------------Temp Store
 2648: 
 2649: sub tmpreset {
 2650:   my ($symb,$namespace,$domain,$stuname) = @_;
 2651:   if (!$symb) {
 2652:     $symb=&symbread();
 2653:     if (!$symb) { $symb= $env{'request.url'}; }
 2654:   }
 2655:   $symb=escape($symb);
 2656: 
 2657:   if (!$namespace) { $namespace=$env{'request.state'}; }
 2658:   $namespace=~s/\//\_/g;
 2659:   $namespace=~s/\W//g;
 2660: 
 2661:   if (!$domain) { $domain=$env{'user.domain'}; }
 2662:   if (!$stuname) { $stuname=$env{'user.name'}; }
 2663:   if ($domain eq 'public' && $stuname eq 'public') {
 2664:       $stuname=$ENV{'REMOTE_ADDR'};
 2665:   }
 2666:   my $path=$perlvar{'lonDaemons'}.'/tmp';
 2667:   my %hash;
 2668:   if (tie(%hash,'GDBM_File',
 2669: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
 2670: 	  &GDBM_WRCREAT(),0640)) {
 2671:     foreach my $key (keys %hash) {
 2672:       if ($key=~ /:$symb/) {
 2673: 	delete($hash{$key});
 2674:       }
 2675:     }
 2676:   }
 2677: }
 2678: 
 2679: sub tmpstore {
 2680:   my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
 2681: 
 2682:   if (!$symb) {
 2683:     $symb=&symbread();
 2684:     if (!$symb) { $symb= $env{'request.url'}; }
 2685:   }
 2686:   $symb=escape($symb);
 2687: 
 2688:   if (!$namespace) {
 2689:     # I don't think we would ever want to store this for a course.
 2690:     # it seems this will only be used if we don't have a course.
 2691:     #$namespace=$env{'request.course.id'};
 2692:     #if (!$namespace) {
 2693:       $namespace=$env{'request.state'};
 2694:     #}
 2695:   }
 2696:   $namespace=~s/\//\_/g;
 2697:   $namespace=~s/\W//g;
 2698:   if (!$domain) { $domain=$env{'user.domain'}; }
 2699:   if (!$stuname) { $stuname=$env{'user.name'}; }
 2700:   if ($domain eq 'public' && $stuname eq 'public') {
 2701:       $stuname=$ENV{'REMOTE_ADDR'};
 2702:   }
 2703:   my $now=time;
 2704:   my %hash;
 2705:   my $path=$perlvar{'lonDaemons'}.'/tmp';
 2706:   if (tie(%hash,'GDBM_File',
 2707: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
 2708: 	  &GDBM_WRCREAT(),0640)) {
 2709:     $hash{"version:$symb"}++;
 2710:     my $version=$hash{"version:$symb"};
 2711:     my $allkeys=''; 
 2712:     foreach my $key (keys(%$storehash)) {
 2713:       $allkeys.=$key.':';
 2714:       $hash{"$version:$symb:$key"}=&freeze_escape($$storehash{$key});
 2715:     }
 2716:     $hash{"$version:$symb:timestamp"}=$now;
 2717:     $allkeys.='timestamp';
 2718:     $hash{"$version:keys:$symb"}=$allkeys;
 2719:     if (untie(%hash)) {
 2720:       return 'ok';
 2721:     } else {
 2722:       return "error:$!";
 2723:     }
 2724:   } else {
 2725:     return "error:$!";
 2726:   }
 2727: }
 2728: 
 2729: # -----------------------------------------------------------------Temp Restore
 2730: 
 2731: sub tmprestore {
 2732:   my ($symb,$namespace,$domain,$stuname) = @_;
 2733: 
 2734:   if (!$symb) {
 2735:     $symb=&symbread();
 2736:     if (!$symb) { $symb= $env{'request.url'}; }
 2737:   }
 2738:   $symb=escape($symb);
 2739: 
 2740:   if (!$namespace) { $namespace=$env{'request.state'}; }
 2741: 
 2742:   if (!$domain) { $domain=$env{'user.domain'}; }
 2743:   if (!$stuname) { $stuname=$env{'user.name'}; }
 2744:   if ($domain eq 'public' && $stuname eq 'public') {
 2745:       $stuname=$ENV{'REMOTE_ADDR'};
 2746:   }
 2747:   my %returnhash;
 2748:   $namespace=~s/\//\_/g;
 2749:   $namespace=~s/\W//g;
 2750:   my %hash;
 2751:   my $path=$perlvar{'lonDaemons'}.'/tmp';
 2752:   if (tie(%hash,'GDBM_File',
 2753: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
 2754: 	  &GDBM_READER(),0640)) {
 2755:     my $version=$hash{"version:$symb"};
 2756:     $returnhash{'version'}=$version;
 2757:     my $scope;
 2758:     for ($scope=1;$scope<=$version;$scope++) {
 2759:       my $vkeys=$hash{"$scope:keys:$symb"};
 2760:       my @keys=split(/:/,$vkeys);
 2761:       my $key;
 2762:       $returnhash{"$scope:keys"}=$vkeys;
 2763:       foreach $key (@keys) {
 2764: 	$returnhash{"$scope:$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
 2765: 	$returnhash{"$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
 2766:       }
 2767:     }
 2768:     if (!(untie(%hash))) {
 2769:       return "error:$!";
 2770:     }
 2771:   } else {
 2772:     return "error:$!";
 2773:   }
 2774:   return %returnhash;
 2775: }
 2776: 
 2777: # ----------------------------------------------------------------------- Store
 2778: 
 2779: sub store {
 2780:     my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
 2781:     my $home='';
 2782: 
 2783:     if ($stuname) { $home=&homeserver($stuname,$domain); }
 2784: 
 2785:     $symb=&symbclean($symb);
 2786:     if (!$symb) { unless ($symb=&symbread()) { return ''; } }
 2787: 
 2788:     if (!$domain) { $domain=$env{'user.domain'}; }
 2789:     if (!$stuname) { $stuname=$env{'user.name'}; }
 2790: 
 2791:     &devalidate($symb,$stuname,$domain);
 2792: 
 2793:     $symb=escape($symb);
 2794:     if (!$namespace) { 
 2795:        unless ($namespace=$env{'request.course.id'}) { 
 2796:           return ''; 
 2797:        } 
 2798:     }
 2799:     if (!$home) { $home=$env{'user.home'}; }
 2800: 
 2801:     $$storehash{'ip'}=$ENV{'REMOTE_ADDR'};
 2802:     $$storehash{'host'}=$perlvar{'lonHostID'};
 2803: 
 2804:     my $namevalue='';
 2805:     foreach my $key (keys(%$storehash)) {
 2806:         $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
 2807:     }
 2808:     $namevalue=~s/\&$//;
 2809:     &courselog($symb.':'.$stuname.':'.$domain.':STORE:'.$namevalue);
 2810:     return reply("store:$domain:$stuname:$namespace:$symb:$namevalue","$home");
 2811: }
 2812: 
 2813: # -------------------------------------------------------------- Critical Store
 2814: 
 2815: sub cstore {
 2816:     my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
 2817:     my $home='';
 2818: 
 2819:     if ($stuname) { $home=&homeserver($stuname,$domain); }
 2820: 
 2821:     $symb=&symbclean($symb);
 2822:     if (!$symb) { unless ($symb=&symbread()) { return ''; } }
 2823: 
 2824:     if (!$domain) { $domain=$env{'user.domain'}; }
 2825:     if (!$stuname) { $stuname=$env{'user.name'}; }
 2826: 
 2827:     &devalidate($symb,$stuname,$domain);
 2828: 
 2829:     $symb=escape($symb);
 2830:     if (!$namespace) { 
 2831:        unless ($namespace=$env{'request.course.id'}) { 
 2832:           return ''; 
 2833:        } 
 2834:     }
 2835:     if (!$home) { $home=$env{'user.home'}; }
 2836: 
 2837:     $$storehash{'ip'}=$ENV{'REMOTE_ADDR'};
 2838:     $$storehash{'host'}=$perlvar{'lonHostID'};
 2839: 
 2840:     my $namevalue='';
 2841:     foreach my $key (keys(%$storehash)) {
 2842:         $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
 2843:     }
 2844:     $namevalue=~s/\&$//;
 2845:     &courselog($symb.':'.$stuname.':'.$domain.':CSTORE:'.$namevalue);
 2846:     return critical
 2847:                 ("store:$domain:$stuname:$namespace:$symb:$namevalue","$home");
 2848: }
 2849: 
 2850: # --------------------------------------------------------------------- Restore
 2851: 
 2852: sub restore {
 2853:     my ($symb,$namespace,$domain,$stuname) = @_;
 2854:     my $home='';
 2855: 
 2856:     if ($stuname) { $home=&homeserver($stuname,$domain); }
 2857: 
 2858:     if (!$symb) {
 2859:       unless ($symb=escape(&symbread())) { return ''; }
 2860:     } else {
 2861:       $symb=&escape(&symbclean($symb));
 2862:     }
 2863:     if (!$namespace) { 
 2864:        unless ($namespace=$env{'request.course.id'}) { 
 2865:           return ''; 
 2866:        } 
 2867:     }
 2868:     if (!$domain) { $domain=$env{'user.domain'}; }
 2869:     if (!$stuname) { $stuname=$env{'user.name'}; }
 2870:     if (!$home) { $home=$env{'user.home'}; }
 2871:     my $answer=&reply("restore:$domain:$stuname:$namespace:$symb","$home");
 2872: 
 2873:     my %returnhash=();
 2874:     foreach my $line (split(/\&/,$answer)) {
 2875: 	my ($name,$value)=split(/\=/,$line);
 2876:         $returnhash{&unescape($name)}=&thaw_unescape($value);
 2877:     }
 2878:     my $version;
 2879:     for ($version=1;$version<=$returnhash{'version'};$version++) {
 2880:        foreach my $item (split(/\:/,$returnhash{$version.':keys'})) {
 2881:           $returnhash{$item}=$returnhash{$version.':'.$item};
 2882:        }
 2883:     }
 2884:     return %returnhash;
 2885: }
 2886: 
 2887: # ---------------------------------------------------------- Course Description
 2888: 
 2889: sub coursedescription {
 2890:     my ($courseid,$args)=@_;
 2891:     $courseid=~s/^\///;
 2892:     $courseid=~s/\_/\//g;
 2893:     my ($cdomain,$cnum)=split(/\//,$courseid);
 2894:     my $chome=&homeserver($cnum,$cdomain);
 2895:     my $normalid=$cdomain.'_'.$cnum;
 2896:     # need to always cache even if we get errors otherwise we keep 
 2897:     # trying and trying and trying to get the course description.
 2898:     my %envhash=();
 2899:     my %returnhash=();
 2900:     
 2901:     my $expiretime=600;
 2902:     if ($env{'request.course.id'} eq $normalid) {
 2903: 	$expiretime=120;
 2904:     }
 2905: 
 2906:     my $prefix='course.'.$cdomain.'_'.$cnum.'.';
 2907:     if (!$args->{'freshen_cache'}
 2908: 	&& ((time-$env{$prefix.'last_cache'}) < $expiretime) ) {
 2909: 	foreach my $key (keys(%env)) {
 2910: 	    next if ($key !~ /^\Q$prefix\E(.*)/);
 2911: 	    my ($setting) = $1;
 2912: 	    $returnhash{$setting} = $env{$key};
 2913: 	}
 2914: 	return %returnhash;
 2915:     }
 2916: 
 2917:     # get the data agin
 2918:     if (!$args->{'one_time'}) {
 2919: 	$envhash{'course.'.$normalid.'.last_cache'}=time;
 2920:     }
 2921: 
 2922:     if ($chome ne 'no_host') {
 2923:        %returnhash=&dump('environment',$cdomain,$cnum);
 2924:        if (!exists($returnhash{'con_lost'})) {
 2925:            $returnhash{'home'}= $chome;
 2926: 	   $returnhash{'domain'} = $cdomain;
 2927: 	   $returnhash{'num'} = $cnum;
 2928:            if (!defined($returnhash{'type'})) {
 2929:                $returnhash{'type'} = 'Course';
 2930:            }
 2931:            while (my ($name,$value) = each %returnhash) {
 2932:                $envhash{'course.'.$normalid.'.'.$name}=$value;
 2933:            }
 2934:            $returnhash{'url'}=&clutter($returnhash{'url'});
 2935:            $returnhash{'fn'}=$perlvar{'lonDaemons'}.'/tmp/'.
 2936: 	       $env{'user.name'}.'_'.$cdomain.'_'.$cnum;
 2937:            $envhash{'course.'.$normalid.'.home'}=$chome;
 2938:            $envhash{'course.'.$normalid.'.domain'}=$cdomain;
 2939:            $envhash{'course.'.$normalid.'.num'}=$cnum;
 2940:        }
 2941:     }
 2942:     if (!$args->{'one_time'}) {
 2943: 	&appenv(%envhash);
 2944:     }
 2945:     return %returnhash;
 2946: }
 2947: 
 2948: # -------------------------------------------------See if a user is privileged
 2949: 
 2950: sub privileged {
 2951:     my ($username,$domain)=@_;
 2952:     my $rolesdump=&reply("dump:$domain:$username:roles",
 2953: 			&homeserver($username,$domain));
 2954:     if (($rolesdump eq 'con_lost') || ($rolesdump eq '')) { return 0; }
 2955:     my $now=time;
 2956:     if ($rolesdump ne '') {
 2957:         foreach my $entry (split(/&/,$rolesdump)) {
 2958: 	    if ($entry!~/^rolesdef_/) {
 2959: 		my ($area,$role)=split(/=/,$entry);
 2960: 		$area=~s/\_\w\w$//;
 2961: 		my ($trole,$tend,$tstart)=split(/_/,$role);
 2962: 		if (($trole eq 'dc') || ($trole eq 'su')) {
 2963: 		    my $active=1;
 2964: 		    if ($tend) {
 2965: 			if ($tend<$now) { $active=0; }
 2966: 		    }
 2967: 		    if ($tstart) {
 2968: 			if ($tstart>$now) { $active=0; }
 2969: 		    }
 2970: 		    if ($active) { return 1; }
 2971: 		}
 2972: 	    }
 2973: 	}
 2974:     }
 2975:     return 0;
 2976: }
 2977: 
 2978: # -------------------------------------------------------- Get user privileges
 2979: 
 2980: sub rolesinit {
 2981:     my ($domain,$username,$authhost)=@_;
 2982:     my $rolesdump=reply("dump:$domain:$username:roles",$authhost);
 2983:     if (($rolesdump eq 'con_lost') || ($rolesdump eq '')) { return ''; }
 2984:     my %allroles=();
 2985:     my %allgroups=();   
 2986:     my $now=time;
 2987:     my %userroles = ('user.login.time' => $now);
 2988:     my $group_privs;
 2989: 
 2990:     if ($rolesdump ne '') {
 2991:         foreach my $entry (split(/&/,$rolesdump)) {
 2992: 	  if ($entry!~/^rolesdef_/) {
 2993:             my ($area,$role)=split(/=/,$entry);
 2994: 	    $area=~s/\_\w\w$//;
 2995:             my ($trole,$tend,$tstart,$group_privs);
 2996: 	    if ($role=~/^cr/) { 
 2997: 		if ($role=~m|^(cr/$match_domain/$match_username/[a-zA-Z0-9]+)_(.*)$|) {
 2998: 		    ($trole,my $trest)=($role=~m|^(cr/$match_domain/$match_username/[a-zA-Z0-9]+)_(.*)$|);
 2999: 		    ($tend,$tstart)=split('_',$trest);
 3000: 		} else {
 3001: 		    $trole=$role;
 3002: 		}
 3003:             } elsif ($role =~ m|^gr/|) {
 3004:                 ($trole,$tend,$tstart) = split(/_/,$role);
 3005:                 ($trole,$group_privs) = split(/\//,$trole);
 3006:                 $group_privs = &unescape($group_privs);
 3007: 	    } else {
 3008: 		($trole,$tend,$tstart)=split(/_/,$role);
 3009: 	    }
 3010: 	    my %new_role = &set_arearole($trole,$area,$tstart,$tend,$domain,
 3011: 					 $username);
 3012: 	    @userroles{keys(%new_role)} = @new_role{keys(%new_role)};
 3013:             if (($tend!=0) && ($tend<$now)) { $trole=''; }
 3014:             if (($tstart!=0) && ($tstart>$now)) { $trole=''; }
 3015:             if (($area ne '') && ($trole ne '')) {
 3016: 		my $spec=$trole.'.'.$area;
 3017: 		my ($tdummy,$tdomain,$trest)=split(/\//,$area);
 3018: 		if ($trole =~ /^cr\//) {
 3019:                     &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
 3020:                 } elsif ($trole eq 'gr') {
 3021:                     &group_roleprivs(\%allgroups,$area,$group_privs,$tend,$tstart);
 3022: 		} else {
 3023:                     &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
 3024: 		}
 3025:             }
 3026:           }
 3027:         }
 3028:         my ($author,$adv) = &set_userprivs(\%userroles,\%allroles,\%allgroups);
 3029:         $userroles{'user.adv'}    = $adv;
 3030: 	$userroles{'user.author'} = $author;
 3031:         $env{'user.adv'}=$adv;
 3032:     }
 3033:     return \%userroles;  
 3034: }
 3035: 
 3036: sub set_arearole {
 3037:     my ($trole,$area,$tstart,$tend,$domain,$username) = @_;
 3038: # log the associated role with the area
 3039:     &userrolelog($trole,$username,$domain,$area,$tstart,$tend);
 3040:     return ('user.role.'.$trole.'.'.$area => $tstart.'.'.$tend);
 3041: }
 3042: 
 3043: sub custom_roleprivs {
 3044:     my ($allroles,$trole,$tdomain,$trest,$spec,$area) = @_;
 3045:     my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$trole);
 3046:     my $homsvr=homeserver($rauthor,$rdomain);
 3047:     if (&hostname($homsvr) ne '') {
 3048:         my ($rdummy,$roledef)=
 3049:             &get('roles',["rolesdef_$rrole"],$rdomain,$rauthor);
 3050:         if (($rdummy ne 'con_lost') && ($roledef ne '')) {
 3051:             my ($syspriv,$dompriv,$coursepriv)=split(/\_/,$roledef);
 3052:             if (defined($syspriv)) {
 3053:                 $$allroles{'cm./'}.=':'.$syspriv;
 3054:                 $$allroles{$spec.'./'}.=':'.$syspriv;
 3055:             }
 3056:             if ($tdomain ne '') {
 3057:                 if (defined($dompriv)) {
 3058:                     $$allroles{'cm./'.$tdomain.'/'}.=':'.$dompriv;
 3059:                     $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$dompriv;
 3060:                 }
 3061:                 if (($trest ne '') && (defined($coursepriv))) {
 3062:                     $$allroles{'cm.'.$area}.=':'.$coursepriv;
 3063:                     $$allroles{$spec.'.'.$area}.=':'.$coursepriv;
 3064:                 }
 3065:             }
 3066:         }
 3067:     }
 3068: }
 3069: 
 3070: sub group_roleprivs {
 3071:     my ($allgroups,$area,$group_privs,$tend,$tstart) = @_;
 3072:     my $access = 1;
 3073:     my $now = time;
 3074:     if (($tend!=0) && ($tend<$now)) { $access = 0; }
 3075:     if (($tstart!=0) && ($tstart>$now)) { $access=0; }
 3076:     if ($access) {
 3077:         my ($course,$group) = ($area =~ m|(/$match_domain/$match_courseid)/([^/]+)$|);
 3078:         $$allgroups{$course}{$group} .=':'.$group_privs;
 3079:     }
 3080: }
 3081: 
 3082: sub standard_roleprivs {
 3083:     my ($allroles,$trole,$tdomain,$spec,$trest,$area) = @_;
 3084:     if (defined($pr{$trole.':s'})) {
 3085:         $$allroles{'cm./'}.=':'.$pr{$trole.':s'};
 3086:         $$allroles{$spec.'./'}.=':'.$pr{$trole.':s'};
 3087:     }
 3088:     if ($tdomain ne '') {
 3089:         if (defined($pr{$trole.':d'})) {
 3090:             $$allroles{'cm./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
 3091:             $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
 3092:         }
 3093:         if (($trest ne '') && (defined($pr{$trole.':c'}))) {
 3094:             $$allroles{'cm.'.$area}.=':'.$pr{$trole.':c'};
 3095:             $$allroles{$spec.'.'.$area}.=':'.$pr{$trole.':c'};
 3096:         }
 3097:     }
 3098: }
 3099: 
 3100: sub set_userprivs {
 3101:     my ($userroles,$allroles,$allgroups) = @_; 
 3102:     my $author=0;
 3103:     my $adv=0;
 3104:     my %grouproles = ();
 3105:     if (keys(%{$allgroups}) > 0) {
 3106:         foreach my $role (keys %{$allroles}) {
 3107:             my ($trole,$area,$sec,$extendedarea);
 3108:             if ($role =~ m-^(\w+|cr/$match_domain/$match_username/\w+)\.(/$match_domain/$match_courseid)(/?\w*)-) {
 3109:                 $trole = $1;
 3110:                 $area = $2;
 3111:                 $sec = $3;
 3112:                 $extendedarea = $area.$sec;
 3113:                 if (exists($$allgroups{$area})) {
 3114:                     foreach my $group (keys(%{$$allgroups{$area}})) {
 3115:                         my $spec = $trole.'.'.$extendedarea;
 3116:                         $grouproles{$spec.'.'.$area.'/'.$group} = 
 3117:                                                 $$allgroups{$area}{$group};
 3118:                     }
 3119:                 }
 3120:             }
 3121:         }
 3122:     }
 3123:     foreach my $group (keys(%grouproles)) {
 3124:         $$allroles{$group} = $grouproles{$group};
 3125:     }
 3126:     foreach my $role (keys(%{$allroles})) {
 3127:         my %thesepriv;
 3128:         if (($role=~/^au/) || ($role=~/^ca/)) { $author=1; }
 3129:         foreach my $item (split(/:/,$$allroles{$role})) {
 3130:             if ($item ne '') {
 3131:                 my ($privilege,$restrictions)=split(/&/,$item);
 3132:                 if ($restrictions eq '') {
 3133:                     $thesepriv{$privilege}='F';
 3134:                 } elsif ($thesepriv{$privilege} ne 'F') {
 3135:                     $thesepriv{$privilege}.=$restrictions;
 3136:                 }
 3137:                 if ($thesepriv{'adv'} eq 'F') { $adv=1; }
 3138:             }
 3139:         }
 3140:         my $thesestr='';
 3141:         foreach my $priv (keys(%thesepriv)) {
 3142: 	    $thesestr.=':'.$priv.'&'.$thesepriv{$priv};
 3143: 	}
 3144:         $userroles->{'user.priv.'.$role} = $thesestr;
 3145:     }
 3146:     return ($author,$adv);
 3147: }
 3148: 
 3149: # --------------------------------------------------------------- get interface
 3150: 
 3151: sub get {
 3152:    my ($namespace,$storearr,$udomain,$uname)=@_;
 3153:    my $items='';
 3154:    foreach my $item (@$storearr) {
 3155:        $items.=&escape($item).'&';
 3156:    }
 3157:    $items=~s/\&$//;
 3158:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 3159:    if (!$uname) { $uname=$env{'user.name'}; }
 3160:    my $uhome=&homeserver($uname,$udomain);
 3161: 
 3162:    my $rep=&reply("get:$udomain:$uname:$namespace:$items",$uhome);
 3163:    my @pairs=split(/\&/,$rep);
 3164:    if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
 3165:      return @pairs;
 3166:    }
 3167:    my %returnhash=();
 3168:    my $i=0;
 3169:    foreach my $item (@$storearr) {
 3170:       $returnhash{$item}=&thaw_unescape($pairs[$i]);
 3171:       $i++;
 3172:    }
 3173:    return %returnhash;
 3174: }
 3175: 
 3176: # --------------------------------------------------------------- del interface
 3177: 
 3178: sub del {
 3179:    my ($namespace,$storearr,$udomain,$uname)=@_;
 3180:    my $items='';
 3181:    foreach my $item (@$storearr) {
 3182:        $items.=&escape($item).'&';
 3183:    }
 3184:    $items=~s/\&$//;
 3185:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 3186:    if (!$uname) { $uname=$env{'user.name'}; }
 3187:    my $uhome=&homeserver($uname,$udomain);
 3188: 
 3189:    return &reply("del:$udomain:$uname:$namespace:$items",$uhome);
 3190: }
 3191: 
 3192: # -------------------------------------------------------------- dump interface
 3193: 
 3194: sub dump {
 3195:     my ($namespace,$udomain,$uname,$regexp,$range)=@_;
 3196:     if (!$udomain) { $udomain=$env{'user.domain'}; }
 3197:     if (!$uname) { $uname=$env{'user.name'}; }
 3198:     my $uhome=&homeserver($uname,$udomain);
 3199:     if ($regexp) {
 3200: 	$regexp=&escape($regexp);
 3201:     } else {
 3202: 	$regexp='.';
 3203:     }
 3204:     my $rep=&reply("dump:$udomain:$uname:$namespace:$regexp:$range",$uhome);
 3205:     my @pairs=split(/\&/,$rep);
 3206:     my %returnhash=();
 3207:     foreach my $item (@pairs) {
 3208: 	my ($key,$value)=split(/=/,$item,2);
 3209: 	$key = &unescape($key);
 3210: 	next if ($key =~ /^error: 2 /);
 3211: 	$returnhash{$key}=&thaw_unescape($value);
 3212:     }
 3213:     return %returnhash;
 3214: }
 3215: 
 3216: # --------------------------------------------------------- dumpstore interface
 3217: 
 3218: sub dumpstore {
 3219:    my ($namespace,$udomain,$uname,$regexp,$range)=@_;
 3220:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 3221:    if (!$uname) { $uname=$env{'user.name'}; }
 3222:    my $uhome=&homeserver($uname,$udomain);
 3223:    if ($regexp) {
 3224:        $regexp=&escape($regexp);
 3225:    } else {
 3226:        $regexp='.';
 3227:    }
 3228:    my $rep=&reply("dump:$udomain:$uname:$namespace:$regexp:$range",$uhome);
 3229:    my @pairs=split(/\&/,$rep);
 3230:    my %returnhash=();
 3231:    foreach my $item (@pairs) {
 3232:        my ($key,$value)=split(/=/,$item,2);
 3233:        next if ($key =~ /^error: 2 /);
 3234:        $returnhash{$key}=&thaw_unescape($value);
 3235:    }
 3236:    return %returnhash;
 3237: }
 3238: 
 3239: # -------------------------------------------------------------- keys interface
 3240: 
 3241: sub getkeys {
 3242:    my ($namespace,$udomain,$uname)=@_;
 3243:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 3244:    if (!$uname) { $uname=$env{'user.name'}; }
 3245:    my $uhome=&homeserver($uname,$udomain);
 3246:    my $rep=reply("keys:$udomain:$uname:$namespace",$uhome);
 3247:    my @keyarray=();
 3248:    foreach my $key (split(/\&/,$rep)) {
 3249:       next if ($key =~ /^error: 2 /);
 3250:       push(@keyarray,&unescape($key));
 3251:    }
 3252:    return @keyarray;
 3253: }
 3254: 
 3255: # --------------------------------------------------------------- currentdump
 3256: sub currentdump {
 3257:    my ($courseid,$sdom,$sname)=@_;
 3258:    $courseid = $env{'request.course.id'} if (! defined($courseid));
 3259:    $sdom     = $env{'user.domain'}       if (! defined($sdom));
 3260:    $sname    = $env{'user.name'}         if (! defined($sname));
 3261:    my $uhome = &homeserver($sname,$sdom);
 3262:    my $rep=reply('currentdump:'.$sdom.':'.$sname.':'.$courseid,$uhome);
 3263:    return if ($rep =~ /^(error:|no_such_host)/);
 3264:    #
 3265:    my %returnhash=();
 3266:    #
 3267:    if ($rep eq "unknown_cmd") { 
 3268:        # an old lond will not know currentdump
 3269:        # Do a dump and make it look like a currentdump
 3270:        my @tmp = &dumpstore($courseid,$sdom,$sname,'.');
 3271:        return if ($tmp[0] =~ /^(error:|no_such_host)/);
 3272:        my %hash = @tmp;
 3273:        @tmp=();
 3274:        %returnhash = %{&convert_dump_to_currentdump(\%hash)};
 3275:    } else {
 3276:        my @pairs=split(/\&/,$rep);
 3277:        foreach my $pair (@pairs) {
 3278:            my ($key,$value)=split(/=/,$pair,2);
 3279:            my ($symb,$param) = split(/:/,$key);
 3280:            $returnhash{&unescape($symb)}->{&unescape($param)} = 
 3281:                                                         &thaw_unescape($value);
 3282:        }
 3283:    }
 3284:    return %returnhash;
 3285: }
 3286: 
 3287: sub convert_dump_to_currentdump{
 3288:     my %hash = %{shift()};
 3289:     my %returnhash;
 3290:     # Code ripped from lond, essentially.  The only difference
 3291:     # here is the unescaping done by lonnet::dump().  Conceivably
 3292:     # we might run in to problems with parameter names =~ /^v\./
 3293:     while (my ($key,$value) = each(%hash)) {
 3294:         my ($v,$symb,$param) = split(/:/,$key);
 3295: 	$symb  = &unescape($symb);
 3296: 	$param = &unescape($param);
 3297:         next if ($v eq 'version' || $symb eq 'keys');
 3298:         next if (exists($returnhash{$symb}) &&
 3299:                  exists($returnhash{$symb}->{$param}) &&
 3300:                  $returnhash{$symb}->{'v.'.$param} > $v);
 3301:         $returnhash{$symb}->{$param}=$value;
 3302:         $returnhash{$symb}->{'v.'.$param}=$v;
 3303:     }
 3304:     #
 3305:     # Remove all of the keys in the hashes which keep track of
 3306:     # the version of the parameter.
 3307:     while (my ($symb,$param_hash) = each(%returnhash)) {
 3308:         # use a foreach because we are going to delete from the hash.
 3309:         foreach my $key (keys(%$param_hash)) {
 3310:             delete($param_hash->{$key}) if ($key =~ /^v\./);
 3311:         }
 3312:     }
 3313:     return \%returnhash;
 3314: }
 3315: 
 3316: # ------------------------------------------------------ critical inc interface
 3317: 
 3318: sub cinc {
 3319:     return &inc(@_,'critical');
 3320: }
 3321: 
 3322: # --------------------------------------------------------------- inc interface
 3323: 
 3324: sub inc {
 3325:     my ($namespace,$store,$udomain,$uname,$critical) = @_;
 3326:     if (!$udomain) { $udomain=$env{'user.domain'}; }
 3327:     if (!$uname) { $uname=$env{'user.name'}; }
 3328:     my $uhome=&homeserver($uname,$udomain);
 3329:     my $items='';
 3330:     if (! ref($store)) {
 3331:         # got a single value, so use that instead
 3332:         $items = &escape($store).'=&';
 3333:     } elsif (ref($store) eq 'SCALAR') {
 3334:         $items = &escape($$store).'=&';        
 3335:     } elsif (ref($store) eq 'ARRAY') {
 3336:         $items = join('=&',map {&escape($_);} @{$store});
 3337:     } elsif (ref($store) eq 'HASH') {
 3338:         while (my($key,$value) = each(%{$store})) {
 3339:             $items.= &escape($key).'='.&escape($value).'&';
 3340:         }
 3341:     }
 3342:     $items=~s/\&$//;
 3343:     if ($critical) {
 3344: 	return &critical("inc:$udomain:$uname:$namespace:$items",$uhome);
 3345:     } else {
 3346: 	return &reply("inc:$udomain:$uname:$namespace:$items",$uhome);
 3347:     }
 3348: }
 3349: 
 3350: # --------------------------------------------------------------- put interface
 3351: 
 3352: sub put {
 3353:    my ($namespace,$storehash,$udomain,$uname)=@_;
 3354:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 3355:    if (!$uname) { $uname=$env{'user.name'}; }
 3356:    my $uhome=&homeserver($uname,$udomain);
 3357:    my $items='';
 3358:    foreach my $item (keys(%$storehash)) {
 3359:        $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
 3360:    }
 3361:    $items=~s/\&$//;
 3362:    return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
 3363: }
 3364: 
 3365: # ------------------------------------------------------------ newput interface
 3366: 
 3367: sub newput {
 3368:    my ($namespace,$storehash,$udomain,$uname)=@_;
 3369:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 3370:    if (!$uname) { $uname=$env{'user.name'}; }
 3371:    my $uhome=&homeserver($uname,$udomain);
 3372:    my $items='';
 3373:    foreach my $key (keys(%$storehash)) {
 3374:        $items.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
 3375:    }
 3376:    $items=~s/\&$//;
 3377:    return &reply("newput:$udomain:$uname:$namespace:$items",$uhome);
 3378: }
 3379: 
 3380: # ---------------------------------------------------------  putstore interface
 3381: 
 3382: sub putstore {
 3383:    my ($namespace,$symb,$version,$storehash,$udomain,$uname)=@_;
 3384:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 3385:    if (!$uname) { $uname=$env{'user.name'}; }
 3386:    my $uhome=&homeserver($uname,$udomain);
 3387:    my $items='';
 3388:    foreach my $key (keys(%$storehash)) {
 3389:        $items.= &escape($key).'='.&freeze_escape($storehash->{$key}).'&';
 3390:    }
 3391:    $items=~s/\&$//;
 3392:    my $esc_symb=&escape($symb);
 3393:    my $esc_v=&escape($version);
 3394:    my $reply =
 3395:        &reply("putstore:$udomain:$uname:$namespace:$esc_symb:$esc_v:$items",
 3396: 	      $uhome);
 3397:    if ($reply eq 'unknown_cmd') {
 3398:        # gfall back to way things use to be done
 3399:        return &old_putstore($namespace,$symb,$version,$storehash,$udomain,
 3400: 			    $uname);
 3401:    }
 3402:    return $reply;
 3403: }
 3404: 
 3405: sub old_putstore {
 3406:     my ($namespace,$symb,$version,$storehash,$udomain,$uname)=@_;
 3407:     if (!$udomain) { $udomain=$env{'user.domain'}; }
 3408:     if (!$uname) { $uname=$env{'user.name'}; }
 3409:     my $uhome=&homeserver($uname,$udomain);
 3410:     my %newstorehash;
 3411:     foreach my $item (keys(%$storehash)) {
 3412: 	my $key = $version.':'.&escape($symb).':'.$item;
 3413: 	$newstorehash{$key} = $storehash->{$item};
 3414:     }
 3415:     my $items='';
 3416:     my %allitems = ();
 3417:     foreach my $item (keys(%newstorehash)) {
 3418: 	if ($item =~ m/^([^\:]+):([^\:]+):([^\:]+)$/) {
 3419: 	    my $key = $1.':keys:'.$2;
 3420: 	    $allitems{$key} .= $3.':';
 3421: 	}
 3422: 	$items.=$item.'='.&freeze_escape($newstorehash{$item}).'&';
 3423:     }
 3424:     foreach my $item (keys(%allitems)) {
 3425: 	$allitems{$item} =~ s/\:$//;
 3426: 	$items.= $item.'='.$allitems{$item}.'&';
 3427:     }
 3428:     $items=~s/\&$//;
 3429:     return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
 3430: }
 3431: 
 3432: # ------------------------------------------------------ critical put interface
 3433: 
 3434: sub cput {
 3435:    my ($namespace,$storehash,$udomain,$uname)=@_;
 3436:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 3437:    if (!$uname) { $uname=$env{'user.name'}; }
 3438:    my $uhome=&homeserver($uname,$udomain);
 3439:    my $items='';
 3440:    foreach my $item (keys(%$storehash)) {
 3441:        $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
 3442:    }
 3443:    $items=~s/\&$//;
 3444:    return &critical("put:$udomain:$uname:$namespace:$items",$uhome);
 3445: }
 3446: 
 3447: # -------------------------------------------------------------- eget interface
 3448: 
 3449: sub eget {
 3450:    my ($namespace,$storearr,$udomain,$uname)=@_;
 3451:    my $items='';
 3452:    foreach my $item (@$storearr) {
 3453:        $items.=&escape($item).'&';
 3454:    }
 3455:    $items=~s/\&$//;
 3456:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 3457:    if (!$uname) { $uname=$env{'user.name'}; }
 3458:    my $uhome=&homeserver($uname,$udomain);
 3459:    my $rep=&reply("eget:$udomain:$uname:$namespace:$items",$uhome);
 3460:    my @pairs=split(/\&/,$rep);
 3461:    my %returnhash=();
 3462:    my $i=0;
 3463:    foreach my $item (@$storearr) {
 3464:       $returnhash{$item}=&thaw_unescape($pairs[$i]);
 3465:       $i++;
 3466:    }
 3467:    return %returnhash;
 3468: }
 3469: 
 3470: # ------------------------------------------------------------ tmpput interface
 3471: sub tmpput {
 3472:     my ($storehash,$server,$context)=@_;
 3473:     my $items='';
 3474:     foreach my $item (keys(%$storehash)) {
 3475: 	$items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
 3476:     }
 3477:     $items=~s/\&$//;
 3478:     if (defined($context)) {
 3479:         $items .= ':'.&escape($context);
 3480:     }
 3481:     return &reply("tmpput:$items",$server);
 3482: }
 3483: 
 3484: # ------------------------------------------------------------ tmpget interface
 3485: sub tmpget {
 3486:     my ($token,$server)=@_;
 3487:     if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
 3488:     my $rep=&reply("tmpget:$token",$server);
 3489:     my %returnhash;
 3490:     foreach my $item (split(/\&/,$rep)) {
 3491: 	my ($key,$value)=split(/=/,$item);
 3492: 	$returnhash{&unescape($key)}=&thaw_unescape($value);
 3493:     }
 3494:     return %returnhash;
 3495: }
 3496: 
 3497: # ------------------------------------------------------------ tmpget interface
 3498: sub tmpdel {
 3499:     my ($token,$server)=@_;
 3500:     if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
 3501:     return &reply("tmpdel:$token",$server);
 3502: }
 3503: 
 3504: # -------------------------------------------------- portfolio access checking
 3505: 
 3506: sub portfolio_access {
 3507:     my ($requrl) = @_;
 3508:     my (undef,$udom,$unum,$file_name,$group) = &parse_portfolio_url($requrl);
 3509:     my $result = &get_portfolio_access($udom,$unum,$file_name,$group);
 3510:     if ($result) {
 3511:         my %setters;
 3512:         if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
 3513:             my ($startblock,$endblock) =
 3514:                 &Apache::loncommon::blockcheck(\%setters,'port',$unum,$udom);
 3515:             if ($startblock && $endblock) {
 3516:                 return 'B';
 3517:             }
 3518:         } else {
 3519:             my ($startblock,$endblock) =
 3520:                 &Apache::loncommon::blockcheck(\%setters,'port');
 3521:             if ($startblock && $endblock) {
 3522:                 return 'B';
 3523:             }
 3524:         }
 3525:     }
 3526:     if ($result eq 'ok') {
 3527:        return 'F';
 3528:     } elsif ($result =~ /^[^:]+:guest_/) {
 3529:        return 'A';
 3530:     }
 3531:     return '';
 3532: }
 3533: 
 3534: sub get_portfolio_access {
 3535:     my ($udom,$unum,$file_name,$group,$access_hash) = @_;
 3536: 
 3537:     if (!ref($access_hash)) {
 3538: 	my $current_perms = &get_portfile_permissions($udom,$unum);
 3539: 	my %access_controls = &get_access_controls($current_perms,$group,
 3540: 						   $file_name);
 3541: 	$access_hash = $access_controls{$file_name};
 3542:     }
 3543: 
 3544:     my ($public,$guest,@domains,@users,@courses,@groups);
 3545:     my $now = time;
 3546:     if (ref($access_hash) eq 'HASH') {
 3547:         foreach my $key (keys(%{$access_hash})) {
 3548:             my ($num,$scope,$end,$start) = ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
 3549:             if ($start > $now) {
 3550:                 next;
 3551:             }
 3552:             if ($end && $end<$now) {
 3553:                 next;
 3554:             }
 3555:             if ($scope eq 'public') {
 3556:                 $public = $key;
 3557:                 last;
 3558:             } elsif ($scope eq 'guest') {
 3559:                 $guest = $key;
 3560:             } elsif ($scope eq 'domains') {
 3561:                 push(@domains,$key);
 3562:             } elsif ($scope eq 'users') {
 3563:                 push(@users,$key);
 3564:             } elsif ($scope eq 'course') {
 3565:                 push(@courses,$key);
 3566:             } elsif ($scope eq 'group') {
 3567:                 push(@groups,$key);
 3568:             }
 3569:         }
 3570:         if ($public) {
 3571:             return 'ok';
 3572:         }
 3573:         if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
 3574:             if ($guest) {
 3575:                 return $guest;
 3576:             }
 3577:         } else {
 3578:             if (@domains > 0) {
 3579:                 foreach my $domkey (@domains) {
 3580:                     if (ref($access_hash->{$domkey}{'dom'}) eq 'ARRAY') {
 3581:                         if (grep(/^\Q$env{'user.domain'}\E$/,@{$access_hash->{$domkey}{'dom'}})) {
 3582:                             return 'ok';
 3583:                         }
 3584:                     }
 3585:                 }
 3586:             }
 3587:             if (@users > 0) {
 3588:                 foreach my $userkey (@users) {
 3589:                     if (ref($access_hash->{$userkey}{'users'}) eq 'ARRAY') {
 3590:                         foreach my $item (@{$access_hash->{$userkey}{'users'}}) {
 3591:                             if (ref($item) eq 'HASH') {
 3592:                                 if (($item->{'uname'} eq $env{'user.name'}) &&
 3593:                                     ($item->{'udom'} eq $env{'user.domain'})) {
 3594:                                     return 'ok';
 3595:                                 }
 3596:                             }
 3597:                         }
 3598:                     } 
 3599:                 }
 3600:             }
 3601:             my %roleshash;
 3602:             my @courses_and_groups = @courses;
 3603:             push(@courses_and_groups,@groups); 
 3604:             if (@courses_and_groups > 0) {
 3605:                 my (%allgroups,%allroles); 
 3606:                 my ($start,$end,$role,$sec,$group);
 3607:                 foreach my $envkey (%env) {
 3608:                     if ($envkey =~ m-^user\.role\.(gr|cc|in|ta|ep|st)\./($match_domain)/($match_courseid)/?([^/]*)$-) {
 3609:                         my $cid = $2.'_'.$3; 
 3610:                         if ($1 eq 'gr') {
 3611:                             $group = $4;
 3612:                             $allgroups{$cid}{$group} = $env{$envkey};
 3613:                         } else {
 3614:                             if ($4 eq '') {
 3615:                                 $sec = 'none';
 3616:                             } else {
 3617:                                 $sec = $4;
 3618:                             }
 3619:                             $allroles{$cid}{$1}{$sec} = $env{$envkey};
 3620:                         }
 3621:                     } elsif ($envkey =~ m-^user\.role\./cr/($match_domain/$match_username/\w*)./($match_domain)/($match_courseid)/?([^/]*)$-) {
 3622:                         my $cid = $2.'_'.$3;
 3623:                         if ($4 eq '') {
 3624:                             $sec = 'none';
 3625:                         } else {
 3626:                             $sec = $4;
 3627:                         }
 3628:                         $allroles{$cid}{$1}{$sec} = $env{$envkey};
 3629:                     }
 3630:                 }
 3631:                 if (keys(%allroles) == 0) {
 3632:                     return;
 3633:                 }
 3634:                 foreach my $key (@courses_and_groups) {
 3635:                     my %content = %{$$access_hash{$key}};
 3636:                     my $cnum = $content{'number'};
 3637:                     my $cdom = $content{'domain'};
 3638:                     my $cid = $cdom.'_'.$cnum;
 3639:                     if (!exists($allroles{$cid})) {
 3640:                         next;
 3641:                     }    
 3642:                     foreach my $role_id (keys(%{$content{'roles'}})) {
 3643:                         my @sections = @{$content{'roles'}{$role_id}{'section'}};
 3644:                         my @groups = @{$content{'roles'}{$role_id}{'group'}};
 3645:                         my @status = @{$content{'roles'}{$role_id}{'access'}};
 3646:                         my @roles = @{$content{'roles'}{$role_id}{'role'}};
 3647:                         foreach my $role (keys(%{$allroles{$cid}})) {
 3648:                             if ((grep/^all$/,@roles) || (grep/^\Q$role\E$/,@roles)) {
 3649:                                 foreach my $sec (keys(%{$allroles{$cid}{$role}})) {
 3650:                                     if (&course_group_datechecker($allroles{$cid}{$role}{$sec},$now,\@status) eq 'ok') {
 3651:                                         if (grep/^all$/,@sections) {
 3652:                                             return 'ok';
 3653:                                         } else {
 3654:                                             if (grep/^$sec$/,@sections) {
 3655:                                                 return 'ok';
 3656:                                             }
 3657:                                         }
 3658:                                     }
 3659:                                 }
 3660:                                 if (keys(%{$allgroups{$cid}}) == 0) {
 3661:                                     if (grep/^none$/,@groups) {
 3662:                                         return 'ok';
 3663:                                     }
 3664:                                 } else {
 3665:                                     if (grep/^all$/,@groups) {
 3666:                                         return 'ok';
 3667:                                     } 
 3668:                                     foreach my $group (keys(%{$allgroups{$cid}})) {
 3669:                                         if (grep/^$group$/,@groups) {
 3670:                                             return 'ok';
 3671:                                         }
 3672:                                     }
 3673:                                 } 
 3674:                             }
 3675:                         }
 3676:                     }
 3677:                 }
 3678:             }
 3679:             if ($guest) {
 3680:                 return $guest;
 3681:             }
 3682:         }
 3683:     }
 3684:     return;
 3685: }
 3686: 
 3687: sub course_group_datechecker {
 3688:     my ($dates,$now,$status) = @_;
 3689:     my ($start,$end) = split(/\./,$dates);
 3690:     if (!$start && !$end) {
 3691:         return 'ok';
 3692:     }
 3693:     if (grep/^active$/,@{$status}) {
 3694:         if (((!$start) || ($start && $start <= $now)) && ((!$end) || ($end && $end >= $now))) {
 3695:             return 'ok';
 3696:         }
 3697:     }
 3698:     if (grep/^previous$/,@{$status}) {
 3699:         if ($end > $now ) {
 3700:             return 'ok';
 3701:         }
 3702:     }
 3703:     if (grep/^future$/,@{$status}) {
 3704:         if ($start > $now) {
 3705:             return 'ok';
 3706:         }
 3707:     }
 3708:     return; 
 3709: }
 3710: 
 3711: sub parse_portfolio_url {
 3712:     my ($url) = @_;
 3713: 
 3714:     my ($type,$udom,$unum,$group,$file_name);
 3715:     
 3716:     if ($url =~  m-^/*(?:uploaded|editupload)/($match_domain)/($match_username)/portfolio(/.+)$-) {
 3717: 	$type = 1;
 3718:         $udom = $1;
 3719:         $unum = $2;
 3720:         $file_name = $3;
 3721:     } elsif ($url =~ m-^/*(?:uploaded|editupload)/($match_domain)/($match_courseid)/groups/([^/]+)/portfolio/(.+)$-) {
 3722: 	$type = 2;
 3723:         $udom = $1;
 3724:         $unum = $2;
 3725:         $group = $3;
 3726:         $file_name = $3.'/'.$4;
 3727:     }
 3728:     if (wantarray) {
 3729: 	return ($type,$udom,$unum,$file_name,$group);
 3730:     }
 3731:     return $type;
 3732: }
 3733: 
 3734: sub is_portfolio_url {
 3735:     my ($url) = @_;
 3736:     return scalar(&parse_portfolio_url($url));
 3737: }
 3738: 
 3739: sub is_portfolio_file {
 3740:     my ($file) = @_;
 3741:     if (($file =~ /^portfolio/) || ($file =~ /^groups\/\w+\/portfolio/)) {
 3742:         return 1;
 3743:     }
 3744:     return;
 3745: }
 3746: 
 3747: 
 3748: # ---------------------------------------------- Custom access rule evaluation
 3749: 
 3750: sub customaccess {
 3751:     my ($priv,$uri)=@_;
 3752:     my ($urole,$urealm)=split(/\./,$env{'request.role'},2);
 3753:     my (undef,$udom,$ucrs,$usec)=split(/\//,$urealm);
 3754:     $udom = &LONCAPA::clean_domain($udom);
 3755:     $ucrs = &LONCAPA::clean_username($ucrs);
 3756:     my $access=0;
 3757:     foreach my $right (split(/\s*\,\s*/,&metadata($uri,'rule_rights'))) {
 3758: 	my ($effect,$realm,$role)=split(/\:/,$right);
 3759:         if ($role) {
 3760: 	   if ($role ne $urole) { next; }
 3761:         }
 3762:         foreach my $scope (split(/\s*\,\s*/,$realm)) {
 3763:             my ($tdom,$tcrs,$tsec)=split(/\_/,$scope);
 3764:             if ($tdom) {
 3765: 		if ($tdom ne $udom) { next; }
 3766:             }
 3767:             if ($tcrs) {
 3768: 		if ($tcrs ne $ucrs) { next; }
 3769:             }
 3770:             if ($tsec) {
 3771: 		if ($tsec ne $usec) { next; }
 3772:             }
 3773:             $access=($effect eq 'allow');
 3774:             last;
 3775:         }
 3776: 	if ($realm eq '' && $role eq '') {
 3777:             $access=($effect eq 'allow');
 3778: 	}
 3779:     }
 3780:     return $access;
 3781: }
 3782: 
 3783: # ------------------------------------------------- Check for a user privilege
 3784: 
 3785: sub allowed {
 3786:     my ($priv,$uri,$symb,$role)=@_;
 3787:     my $ver_orguri=$uri;
 3788:     $uri=&deversion($uri);
 3789:     my $orguri=$uri;
 3790:     $uri=&declutter($uri);
 3791: 
 3792:     if ($priv eq 'evb') {
 3793: # Evade communication block restrictions for specified role in a course
 3794:         if ($env{'user.priv.'.$role} =~/evb\&([^\:]*)/) {
 3795:             return $1;
 3796:         } else {
 3797:             return;
 3798:         }
 3799:     }
 3800: 
 3801:     if (defined($env{'allowed.'.$priv})) { return $env{'allowed.'.$priv}; }
 3802: # Free bre access to adm and meta resources
 3803:     if (((($uri=~/^adm\//) && ($uri !~ m{/(?:smppg|bulletinboard)$})) 
 3804: 	 || (($uri=~/\.meta$/) && ($uri!~m|^uploaded/|) )) 
 3805: 	&& ($priv eq 'bre')) {
 3806: 	return 'F';
 3807:     }
 3808: 
 3809: # Free bre access to user's own portfolio contents
 3810:     my ($space,$domain,$name,@dir)=split('/',$uri);
 3811:     if (($space=~/^(uploaded|editupload)$/) && ($env{'user.name'} eq $name) && 
 3812: 	($env{'user.domain'} eq $domain) && ('portfolio' eq $dir[0])) {
 3813:         my %setters;
 3814:         my ($startblock,$endblock) = 
 3815:             &Apache::loncommon::blockcheck(\%setters,'port');
 3816:         if ($startblock && $endblock) {
 3817:             return 'B';
 3818:         } else {
 3819:             return 'F';
 3820:         }
 3821:     }
 3822: 
 3823: # bre access to group portfolio for rgf priv in group, or mdg or vcg in course.
 3824:     if (($space=~/^(uploaded|editupload)$/) && ($dir[0] eq 'groups') 
 3825:          && ($dir[2] eq 'portfolio') && ($priv eq 'bre')) {
 3826:         if (exists($env{'request.course.id'})) {
 3827:             my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 3828:             my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 3829:             if (($domain eq $cdom) && ($name eq $cnum)) {
 3830:                 my $courseprivid=$env{'request.course.id'};
 3831:                 $courseprivid=~s/\_/\//;
 3832:                 if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid
 3833:                     .'/'.$dir[1]} =~/rgf\&([^\:]*)/) {
 3834:                     return $1; 
 3835:                 } else {
 3836:                     if ($env{'request.course.sec'}) {
 3837:                         $courseprivid.='/'.$env{'request.course.sec'};
 3838:                     }
 3839:                     if ($env{'user.priv.'.$env{'request.role'}.'./'.
 3840:                         $courseprivid} =~/(mdg|vcg)\&([^\:]*)/) {
 3841:                         return $2;
 3842:                     }
 3843:                 }
 3844:             }
 3845:         }
 3846:     }
 3847: 
 3848: # Free bre to public access
 3849: 
 3850:     if ($priv eq 'bre') {
 3851:         my $copyright=&metadata($uri,'copyright');
 3852: 	if (($copyright eq 'public') && (!$env{'request.course.id'})) { 
 3853:            return 'F'; 
 3854:         }
 3855:         if ($copyright eq 'priv') {
 3856:             $uri=~/([^\/]+)\/([^\/]+)\//;
 3857: 	    unless (($env{'user.name'} eq $2) && ($env{'user.domain'} eq $1)) {
 3858: 		return '';
 3859:             }
 3860:         }
 3861:         if ($copyright eq 'domain') {
 3862:             $uri=~/([^\/]+)\/([^\/]+)\//;
 3863: 	    unless (($env{'user.domain'} eq $1) ||
 3864:                  ($env{'course.'.$env{'request.course.id'}.'.domain'} eq $1)) {
 3865: 		return '';
 3866:             }
 3867:         }
 3868:         if ($env{'request.role'}=~ /li\.\//) {
 3869:             # Library role, so allow browsing of resources in this domain.
 3870:             return 'F';
 3871:         }
 3872:         if ($copyright eq 'custom') {
 3873: 	    unless (&customaccess($priv,$uri)) { return ''; }
 3874:         }
 3875:     }
 3876:     # Domain coordinator is trying to create a course
 3877:     if (($priv eq 'ccc') && ($env{'request.role'} =~ /^dc\./)) {
 3878:         # uri is the requested domain in this case.
 3879:         # comparison to 'request.role.domain' shows if the user has selected
 3880:         # a role of dc for the domain in question.
 3881:         return 'F' if ($uri eq $env{'request.role.domain'});
 3882:     }
 3883: 
 3884:     my $thisallowed='';
 3885:     my $statecond=0;
 3886:     my $courseprivid='';
 3887: 
 3888: # Course
 3889: 
 3890:     if ($env{'user.priv.'.$env{'request.role'}.'./'}=~/\Q$priv\E\&([^\:]*)/) {
 3891:        $thisallowed.=$1;
 3892:     }
 3893: 
 3894: # Domain
 3895: 
 3896:     if ($env{'user.priv.'.$env{'request.role'}.'./'.(split(/\//,$uri))[0].'/'}
 3897:        =~/\Q$priv\E\&([^\:]*)/) {
 3898:        $thisallowed.=$1;
 3899:     }
 3900: 
 3901: # Course: uri itself is a course
 3902:     my $courseuri=$uri;
 3903:     $courseuri=~s/\_(\d)/\/$1/;
 3904:     $courseuri=~s/^([^\/])/\/$1/;
 3905: 
 3906:     if ($env{'user.priv.'.$env{'request.role'}.'.'.$courseuri}
 3907:        =~/\Q$priv\E\&([^\:]*)/) {
 3908:        $thisallowed.=$1;
 3909:     }
 3910: 
 3911: # URI is an uploaded document for this course, default permissions don't matter
 3912: # not allowing 'edit' access (editupload) to uploaded course docs
 3913:     if (($priv eq 'bre') && ($uri=~m|^uploaded/|)) {
 3914: 	$thisallowed='';
 3915:         my ($match)=&is_on_map($uri);
 3916:         if ($match) {
 3917:             if ($env{'user.priv.'.$env{'request.role'}.'./'}
 3918:                   =~/\Q$priv\E\&([^\:]*)/) {
 3919:                 $thisallowed.=$1;
 3920:             }
 3921:         } else {
 3922:             my $refuri = $env{'httpref.'.$orguri} || $env{'httpref.'.$ver_orguri};
 3923:             if ($refuri) {
 3924:                 if ($refuri =~ m|^/adm/|) {
 3925:                     $thisallowed='F';
 3926:                 } else {
 3927:                     $refuri=&declutter($refuri);
 3928:                     my ($match) = &is_on_map($refuri);
 3929:                     if ($match) {
 3930:                         $thisallowed='F';
 3931:                     }
 3932:                 }
 3933:             }
 3934:         }
 3935:     }
 3936: 
 3937:     if ($priv eq 'bre'
 3938: 	&& $thisallowed ne 'F' 
 3939: 	&& $thisallowed ne '2'
 3940: 	&& &is_portfolio_url($uri)) {
 3941: 	$thisallowed = &portfolio_access($uri);
 3942:     }
 3943:     
 3944: # Full access at system, domain or course-wide level? Exit.
 3945: 
 3946:     if ($thisallowed=~/F/) {
 3947: 	return 'F';
 3948:     }
 3949: 
 3950: # If this is generating or modifying users, exit with special codes
 3951: 
 3952:     if (':csu:cdc:ccc:cin:cta:cep:ccr:cst:cad:cli:cau:cdg:cca:caa:'=~/\:\Q$priv\E\:/) {
 3953: 	if (($priv eq 'cca') || ($priv eq 'caa')) {
 3954: 	    my ($audom,$auname)=split('/',$uri);
 3955: # no author name given, so this just checks on the general right to make a co-author in this domain
 3956: 	    unless ($auname) { return $thisallowed; }
 3957: # an author name is given, so we are about to actually make a co-author for a certain account
 3958: 	    if (($auname ne $env{'user.name'} && $env{'request.role'} !~ /^dc\./) ||
 3959: 		(($audom ne $env{'user.domain'} && $env{'request.role'} !~ /^dc\./) &&
 3960: 		 ($audom ne $env{'request.role.domain'}))) { return ''; }
 3961: 	}
 3962: 	return $thisallowed;
 3963:     }
 3964: #
 3965: # Gathered so far: system, domain and course wide privileges
 3966: #
 3967: # Course: See if uri or referer is an individual resource that is part of 
 3968: # the course
 3969: 
 3970:     if ($env{'request.course.id'}) {
 3971: 
 3972:        $courseprivid=$env{'request.course.id'};
 3973:        if ($env{'request.course.sec'}) {
 3974:           $courseprivid.='/'.$env{'request.course.sec'};
 3975:        }
 3976:        $courseprivid=~s/\_/\//;
 3977:        my $checkreferer=1;
 3978:        my ($match,$cond)=&is_on_map($uri);
 3979:        if ($match) {
 3980:            $statecond=$cond;
 3981:            if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
 3982:                =~/\Q$priv\E\&([^\:]*)/) {
 3983:                $thisallowed.=$1;
 3984:                $checkreferer=0;
 3985:            }
 3986:        }
 3987:        
 3988:        if ($checkreferer) {
 3989: 	  my $refuri=$env{'httpref.'.$orguri};
 3990:             unless ($refuri) {
 3991:                 foreach my $key (keys(%env)) {
 3992: 		    if ($key=~/^httpref\..*\*/) {
 3993: 			my $pattern=$key;
 3994:                         $pattern=~s/^httpref\.\/res\///;
 3995:                         $pattern=~s/\*/\[\^\/\]\+/g;
 3996:                         $pattern=~s/\//\\\//g;
 3997:                         if ($orguri=~/$pattern/) {
 3998: 			    $refuri=$env{$key};
 3999:                         }
 4000:                     }
 4001:                 }
 4002:             }
 4003: 
 4004:          if ($refuri) { 
 4005: 	  $refuri=&declutter($refuri);
 4006:           my ($match,$cond)=&is_on_map($refuri);
 4007:             if ($match) {
 4008:               my $refstatecond=$cond;
 4009:               if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
 4010:                   =~/\Q$priv\E\&([^\:]*)/) {
 4011:                   $thisallowed.=$1;
 4012:                   $uri=$refuri;
 4013:                   $statecond=$refstatecond;
 4014:               }
 4015:           }
 4016:         }
 4017:        }
 4018:    }
 4019: 
 4020: #
 4021: # Gathered now: all privileges that could apply, and condition number
 4022: # 
 4023: #
 4024: # Full or no access?
 4025: #
 4026: 
 4027:     if ($thisallowed=~/F/) {
 4028: 	return 'F';
 4029:     }
 4030: 
 4031:     unless ($thisallowed) {
 4032:         return '';
 4033:     }
 4034: 
 4035: # Restrictions exist, deal with them
 4036: #
 4037: #   C:according to course preferences
 4038: #   R:according to resource settings
 4039: #   L:unless locked
 4040: #   X:according to user session state
 4041: #
 4042: 
 4043: # Possibly locked functionality, check all courses
 4044: # Locks might take effect only after 10 minutes cache expiration for other
 4045: # courses, and 2 minutes for current course
 4046: 
 4047:     my $envkey;
 4048:     if ($thisallowed=~/L/) {
 4049:         foreach $envkey (keys %env) {
 4050:            if ($envkey=~/^user\.role\.(st|ta)\.([^\.]*)/) {
 4051:                my $courseid=$2;
 4052:                my $roleid=$1.'.'.$2;
 4053:                $courseid=~s/^\///;
 4054:                my $expiretime=600;
 4055:                if ($env{'request.role'} eq $roleid) {
 4056: 		  $expiretime=120;
 4057:                }
 4058: 	       my ($cdom,$cnum,$csec)=split(/\//,$courseid);
 4059:                my $prefix='course.'.$cdom.'_'.$cnum.'.';
 4060:                if ((time-$env{$prefix.'last_cache'})>$expiretime) {
 4061: 		   &coursedescription($courseid,{'freshen_cache' => 1});
 4062:                }
 4063:                if (($env{$prefix.'res.'.$uri.'.lock.sections'}=~/\,\Q$csec\E\,/)
 4064:                 || ($env{$prefix.'res.'.$uri.'.lock.sections'} eq 'all')) {
 4065: 		   if ($env{$prefix.'res.'.$uri.'.lock.expire'}>time) {
 4066:                        &log($env{'user.domain'},$env{'user.name'},
 4067:                             $env{'user.home'},
 4068:                             'Locked by res: '.$priv.' for '.$uri.' due to '.
 4069:                             $cdom.'/'.$cnum.'/'.$csec.' expire '.
 4070:                             $env{$prefix.'priv.'.$priv.'.lock.expire'});
 4071: 		       return '';
 4072:                    }
 4073:                }
 4074:                if (($env{$prefix.'priv.'.$priv.'.lock.sections'}=~/\,\Q$csec\E\,/)
 4075:                 || ($env{$prefix.'priv.'.$priv.'.lock.sections'} eq 'all')) {
 4076: 		   if ($env{'priv.'.$priv.'.lock.expire'}>time) {
 4077:                        &log($env{'user.domain'},$env{'user.name'},
 4078:                             $env{'user.home'},
 4079:                             'Locked by priv: '.$priv.' for '.$uri.' due to '.
 4080:                             $cdom.'/'.$cnum.'/'.$csec.' expire '.
 4081:                             $env{$prefix.'priv.'.$priv.'.lock.expire'});
 4082: 		       return '';
 4083:                    }
 4084:                }
 4085: 	   }
 4086:        }
 4087:     }
 4088:    
 4089: #
 4090: # Rest of the restrictions depend on selected course
 4091: #
 4092: 
 4093:     unless ($env{'request.course.id'}) {
 4094: 	if ($thisallowed eq 'A') {
 4095: 	    return 'A';
 4096:         } elsif ($thisallowed eq 'B') {
 4097:             return 'B';
 4098: 	} else {
 4099: 	    return '1';
 4100: 	}
 4101:     }
 4102: 
 4103: #
 4104: # Now user is definitely in a course
 4105: #
 4106: 
 4107: 
 4108: # Course preferences
 4109: 
 4110:    if ($thisallowed=~/C/) {
 4111:        my $rolecode=(split(/\./,$env{'request.role'}))[0];
 4112:        my $unamedom=$env{'user.name'}.':'.$env{'user.domain'};
 4113:        if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.roles.denied'}
 4114: 	   =~/\Q$rolecode\E/) {
 4115: 	   if ($priv ne 'pch') { 
 4116: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
 4117: 			'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode.' in '.
 4118: 			$env{'request.course.id'});
 4119: 	   }
 4120:            return '';
 4121:        }
 4122: 
 4123:        if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.users.denied'}
 4124: 	   =~/\Q$unamedom\E/) {
 4125: 	   if ($priv ne 'pch') { 
 4126: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.
 4127: 			'Denied by user: '.$priv.' for '.$uri.' as '.$unamedom.' in '.
 4128: 			$env{'request.course.id'});
 4129: 	   }
 4130:            return '';
 4131:        }
 4132:    }
 4133: 
 4134: # Resource preferences
 4135: 
 4136:    if ($thisallowed=~/R/) {
 4137:        my $rolecode=(split(/\./,$env{'request.role'}))[0];
 4138:        if (&metadata($uri,'roledeny')=~/\Q$rolecode\E/) {
 4139: 	   if ($priv ne 'pch') { 
 4140: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
 4141: 			'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode);
 4142: 	   }
 4143: 	   return '';
 4144:        }
 4145:    }
 4146: 
 4147: # Restricted by state or randomout?
 4148: 
 4149:    if ($thisallowed=~/X/) {
 4150:       if ($env{'acc.randomout'}) {
 4151: 	 if (!$symb) { $symb=&symbread($uri,1); }
 4152:          if (($symb) && ($env{'acc.randomout'}=~/\&\Q$symb\E\&/)) { 
 4153:             return ''; 
 4154:          }
 4155:       }
 4156:       if (&condval($statecond)) {
 4157: 	 return '2';
 4158:       } else {
 4159:          return '';
 4160:       }
 4161:    }
 4162: 
 4163:     if ($thisallowed eq 'A') {
 4164: 	return 'A';
 4165:     } elsif ($thisallowed eq 'B') {
 4166:         return 'B';
 4167:     }
 4168:    return 'F';
 4169: }
 4170: 
 4171: sub split_uri_for_cond {
 4172:     my $uri=&deversion(&declutter(shift));
 4173:     my @uriparts=split(/\//,$uri);
 4174:     my $filename=pop(@uriparts);
 4175:     my $pathname=join('/',@uriparts);
 4176:     return ($pathname,$filename);
 4177: }
 4178: # --------------------------------------------------- Is a resource on the map?
 4179: 
 4180: sub is_on_map {
 4181:     my ($pathname,$filename) = &split_uri_for_cond(shift);
 4182:     #Trying to find the conditional for the file
 4183:     my $match=($env{'acc.res.'.$env{'request.course.id'}.'.'.$pathname}=~
 4184: 	       /\&\Q$filename\E\:([\d\|]+)\&/);
 4185:     if ($match) {
 4186: 	return (1,$1);
 4187:     } else {
 4188: 	return (0,0);
 4189:     }
 4190: }
 4191: 
 4192: # --------------------------------------------------------- Get symb from alias
 4193: 
 4194: sub get_symb_from_alias {
 4195:     my $symb=shift;
 4196:     my ($map,$resid,$url)=&decode_symb($symb);
 4197: # Already is a symb
 4198:     if ($url) { return $symb; }
 4199: # Must be an alias
 4200:     my $aliassymb='';
 4201:     my %bighash;
 4202:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
 4203:                             &GDBM_READER(),0640)) {
 4204:         my $rid=$bighash{'mapalias_'.$symb};
 4205: 	if ($rid) {
 4206: 	    my ($mapid,$resid)=split(/\./,$rid);
 4207: 	    $aliassymb=&encode_symb($bighash{'map_id_'.$mapid},
 4208: 				    $resid,$bighash{'src_'.$rid});
 4209: 	}
 4210:         untie %bighash;
 4211:     }
 4212:     return $aliassymb;
 4213: }
 4214: 
 4215: # ----------------------------------------------------------------- Define Role
 4216: 
 4217: sub definerole {
 4218:   if (allowed('mcr','/')) {
 4219:     my ($rolename,$sysrole,$domrole,$courole)=@_;
 4220:     foreach my $role (split(':',$sysrole)) {
 4221: 	my ($crole,$cqual)=split(/\&/,$role);
 4222:         if ($pr{'cr:s'}!~/\Q$crole\E/) { return "refused:s:$crole"; }
 4223:         if ($pr{'cr:s'}=~/\Q$crole\E\&/) {
 4224: 	    if ($pr{'cr:s'}!~/\Q$crole\E\&\w*\Q$cqual\E/) { 
 4225:                return "refused:s:$crole&$cqual"; 
 4226:             }
 4227:         }
 4228:     }
 4229:     foreach my $role (split(':',$domrole)) {
 4230: 	my ($crole,$cqual)=split(/\&/,$role);
 4231:         if ($pr{'cr:d'}!~/\Q$crole\E/) { return "refused:d:$crole"; }
 4232:         if ($pr{'cr:d'}=~/\Q$crole\E\&/) {
 4233: 	    if ($pr{'cr:d'}!~/\Q$crole\W\&\w*\Q$cqual\E/) { 
 4234:                return "refused:d:$crole&$cqual"; 
 4235:             }
 4236:         }
 4237:     }
 4238:     foreach my $role (split(':',$courole)) {
 4239: 	my ($crole,$cqual)=split(/\&/,$role);
 4240:         if ($pr{'cr:c'}!~/\Q$crole\E/) { return "refused:c:$crole"; }
 4241:         if ($pr{'cr:c'}=~/\Q$crole\E\&/) {
 4242: 	    if ($pr{'cr:c'}!~/\Q$crole\E\&\w*\Q$cqual\E/) { 
 4243:                return "refused:c:$crole&$cqual"; 
 4244:             }
 4245:         }
 4246:     }
 4247:     my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
 4248:                 "$env{'user.domain'}:$env{'user.name'}:".
 4249: 	        "rolesdef_$rolename=".
 4250:                 escape($sysrole.'_'.$domrole.'_'.$courole);
 4251:     return reply($command,$env{'user.home'});
 4252:   } else {
 4253:     return 'refused';
 4254:   }
 4255: }
 4256: 
 4257: # ---------------- Make a metadata query against the network of library servers
 4258: 
 4259: sub metadata_query {
 4260:     my ($query,$custom,$customshow,$server_array)=@_;
 4261:     my %rhash;
 4262:     my %libserv = &all_library();
 4263:     my @server_list = (defined($server_array) ? @$server_array
 4264:                                               : keys(%libserv) );
 4265:     for my $server (@server_list) {
 4266: 	unless ($custom or $customshow) {
 4267: 	    my $reply=&reply("querysend:".&escape($query),$server);
 4268: 	    $rhash{$server}=$reply;
 4269: 	}
 4270: 	else {
 4271: 	    my $reply=&reply("querysend:".&escape($query).':'.
 4272: 			     &escape($custom).':'.&escape($customshow),
 4273: 			     $server);
 4274: 	    $rhash{$server}=$reply;
 4275: 	}
 4276:     }
 4277:     return \%rhash;
 4278: }
 4279: 
 4280: # ----------------------------------------- Send log queries and wait for reply
 4281: 
 4282: sub log_query {
 4283:     my ($uname,$udom,$query,%filters)=@_;
 4284:     my $uhome=&homeserver($uname,$udom);
 4285:     if ($uhome eq 'no_host') { return 'error: no_host'; }
 4286:     my $uhost=&hostname($uhome);
 4287:     my $command=&escape(join(':',map{$_.'='.$filters{$_}} keys(%filters)));
 4288:     my $queryid=&reply("querysend:".$query.':'.$udom.':'.$uname.':'.$command,
 4289:                        $uhome);
 4290:     unless ($queryid=~/^\Q$uhost\E\_/) { return 'error: '.$queryid; }
 4291:     return get_query_reply($queryid);
 4292: }
 4293: 
 4294: # -------------------------- Update MySQL table for portfolio file
 4295: 
 4296: sub update_portfolio_table {
 4297:     my ($uname,$udom,$file_name,$query,$group,$action) = @_;
 4298:     my $homeserver = &homeserver($uname,$udom);
 4299:     my $queryid=
 4300:         &reply("querysend:".$query.':'.&escape($uname.':'.$udom.':'.$group).
 4301:                ':'.&escape($file_name).':'.$action,$homeserver);
 4302:     my $reply = &get_query_reply($queryid);
 4303:     return $reply;
 4304: }
 4305: 
 4306: # ------- Request retrieval of institutional classlists for course(s)
 4307: 
 4308: sub fetch_enrollment_query {
 4309:     my ($context,$affiliatesref,$replyref,$dom,$cnum) = @_;
 4310:     my $homeserver;
 4311:     my $maxtries = 1;
 4312:     if ($context eq 'automated') {
 4313:         $homeserver = $perlvar{'lonHostID'};
 4314:         $maxtries = 10; # will wait for up to 2000s for retrieval of classlist data before timeout
 4315:     } else {
 4316:         $homeserver = &homeserver($cnum,$dom);
 4317:     }
 4318:     my $host=&hostname($homeserver);
 4319:     my $cmd = '';
 4320:     foreach my $affiliate (keys %{$affiliatesref}) {
 4321:         $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
 4322:     }
 4323:     $cmd =~ s/%%$//;
 4324:     $cmd = &escape($cmd);
 4325:     my $query = 'fetchenrollment';
 4326:     my $queryid=&reply("querysend:".$query.':'.$dom.':'.$env{'user.name'}.':'.$cmd,$homeserver);
 4327:     unless ($queryid=~/^\Q$host\E\_/) { 
 4328:         &logthis('fetch_enrollment_query: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' context: '.$context.' '.$cnum); 
 4329:         return 'error: '.$queryid;
 4330:     }
 4331:     my $reply = &get_query_reply($queryid);
 4332:     my $tries = 1;
 4333:     while (($reply=~/^timeout/) && ($tries < $maxtries)) {
 4334:         $reply = &get_query_reply($queryid);
 4335:         $tries ++;
 4336:     }
 4337:     if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
 4338:         &logthis('fetch_enrollment_query error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' context: '.$context.' '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
 4339:     } else {
 4340:         my @responses = split/:/,$reply;
 4341:         if ($homeserver eq $perlvar{'lonHostID'}) {
 4342:             foreach my $line (@responses) {
 4343:                 my ($key,$value) = split(/=/,$line,2);
 4344:                 $$replyref{$key} = $value;
 4345:             }
 4346:         } else {
 4347:             my $pathname = $perlvar{'lonDaemons'}.'/tmp';
 4348:             foreach my $line (@responses) {
 4349:                 my ($key,$value) = split(/=/,$line);
 4350:                 $$replyref{$key} = $value;
 4351:                 if ($value > 0) {
 4352:                     foreach my $item (@{$$affiliatesref{$key}}) {
 4353:                         my $filename = $dom.'_'.$key.'_'.$item.'_classlist.xml';
 4354:                         my $destname = $pathname.'/'.$filename;
 4355:                         my $xml_classlist = &reply("autoretrieve:".$filename,$homeserver);
 4356:                         if ($xml_classlist =~ /^error/) {
 4357:                             &logthis('fetch_enrollment_query - autoretrieve error: '.$xml_classlist.' for '.$filename.' from server: '.$homeserver.' '.$context.' '.$cnum);
 4358:                         } else {
 4359:                             if ( open(FILE,">$destname") ) {
 4360:                                 print FILE &unescape($xml_classlist);
 4361:                                 close(FILE);
 4362:                             } else {
 4363:                                 &logthis('fetch_enrollment_query - error opening classlist file '.$destname.' '.$context.' '.$cnum);
 4364:                             }
 4365:                         }
 4366:                     }
 4367:                 }
 4368:             }
 4369:         }
 4370:         return 'ok';
 4371:     }
 4372:     return 'error';
 4373: }
 4374: 
 4375: sub get_query_reply {
 4376:     my $queryid=shift;
 4377:     my $replyfile=$perlvar{'lonDaemons'}.'/tmp/'.$queryid;
 4378:     my $reply='';
 4379:     for (1..100) {
 4380: 	sleep 2;
 4381:         if (-e $replyfile.'.end') {
 4382: 	    if (open(my $fh,$replyfile)) {
 4383:                $reply.=<$fh>;
 4384:                close($fh);
 4385: 	   } else { return 'error: reply_file_error'; }
 4386:            return &unescape($reply);
 4387: 	}
 4388:     }
 4389:     return 'timeout:'.$queryid;
 4390: }
 4391: 
 4392: sub courselog_query {
 4393: #
 4394: # possible filters:
 4395: # url: url or symb
 4396: # username
 4397: # domain
 4398: # action: view, submit, grade
 4399: # start: timestamp
 4400: # end: timestamp
 4401: #
 4402:     my (%filters)=@_;
 4403:     unless ($env{'request.course.id'}) { return 'no_course'; }
 4404:     if ($filters{'url'}) {
 4405: 	$filters{'url'}=&symbclean(&declutter($filters{'url'}));
 4406:         $filters{'url'}=~s/\.(\w+)$/(\\.\\d+)*\\.$1/;
 4407:         $filters{'url'}=~s/\.(\w+)\_\_\_/(\\.\\d+)*\\.$1/;
 4408:     }
 4409:     my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
 4410:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 4411:     return &log_query($cname,$cdom,'courselog',%filters);
 4412: }
 4413: 
 4414: sub userlog_query {
 4415: #
 4416: # possible filters:
 4417: # action: log check role
 4418: # start: timestamp
 4419: # end: timestamp
 4420: #
 4421:     my ($uname,$udom,%filters)=@_;
 4422:     return &log_query($uname,$udom,'userlog',%filters);
 4423: }
 4424: 
 4425: #--------- Call auto-enrollment subs in localenroll.pm for homeserver for course 
 4426: 
 4427: sub auto_run {
 4428:     my ($cnum,$cdom) = @_;
 4429:     my $homeserver = &homeserver($cnum,$cdom);
 4430:     my $response = &reply('autorun:'.$cdom,$homeserver);
 4431:     return $response;
 4432: }
 4433: 
 4434: sub auto_get_sections {
 4435:     my ($cnum,$cdom,$inst_coursecode) = @_;
 4436:     my $homeserver = &homeserver($cnum,$cdom);
 4437:     my @secs = ();
 4438:     my $response=&unescape(&reply('autogetsections:'.$inst_coursecode.':'.$cdom,$homeserver));
 4439:     unless ($response eq 'refused') {
 4440:         @secs = split/:/,$response;
 4441:     }
 4442:     return @secs;
 4443: }
 4444: 
 4445: sub auto_new_course {
 4446:     my ($cnum,$cdom,$inst_course_id,$owner) = @_;
 4447:     my $homeserver = &homeserver($cnum,$cdom);
 4448:     my $response=&unescape(&reply('autonewcourse:'.$inst_course_id.':'.$owner.':'.$cdom,$homeserver));
 4449:     return $response;
 4450: }
 4451: 
 4452: sub auto_validate_courseID {
 4453:     my ($cnum,$cdom,$inst_course_id) = @_;
 4454:     my $homeserver = &homeserver($cnum,$cdom);
 4455:     my $response=&unescape(&reply('autovalidatecourse:'.$inst_course_id.':'.$cdom,$homeserver));
 4456:     return $response;
 4457: }
 4458: 
 4459: sub auto_create_password {
 4460:     my ($cnum,$cdom,$authparam) = @_;
 4461:     my $homeserver = &homeserver($cnum,$cdom); 
 4462:     my $create_passwd = 0;
 4463:     my $authchk = '';
 4464:     my $response=&unescape(&reply('autocreatepassword:'.$authparam.':'.$cdom,$homeserver));
 4465:     if ($response eq 'refused') {
 4466:         $authchk = 'refused';
 4467:     } else {
 4468:         ($authparam,$create_passwd,$authchk) = split/:/,$response;
 4469:     }
 4470:     return ($authparam,$create_passwd,$authchk);
 4471: }
 4472: 
 4473: sub auto_photo_permission {
 4474:     my ($cnum,$cdom,$students) = @_;
 4475:     my $homeserver = &homeserver($cnum,$cdom);
 4476:     my ($outcome,$perm_reqd,$conditions) = 
 4477: 	split(/:/,&unescape(&reply('autophotopermission:'.$cdom,$homeserver)),3);
 4478:     if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
 4479: 	return (undef,undef);
 4480:     }
 4481:     return ($outcome,$perm_reqd,$conditions);
 4482: }
 4483: 
 4484: sub auto_checkphotos {
 4485:     my ($uname,$udom,$pid) = @_;
 4486:     my $homeserver = &homeserver($uname,$udom);
 4487:     my ($result,$resulttype);
 4488:     my $outcome = &unescape(&reply('autophotocheck:'.&escape($udom).':'.
 4489: 				   &escape($uname).':'.&escape($pid),
 4490: 				   $homeserver));
 4491:     if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
 4492: 	return (undef,undef);
 4493:     }
 4494:     if ($outcome) {
 4495:         ($result,$resulttype) = split(/:/,$outcome);
 4496:     } 
 4497:     return ($result,$resulttype);
 4498: }
 4499: 
 4500: sub auto_photochoice {
 4501:     my ($cnum,$cdom) = @_;
 4502:     my $homeserver = &homeserver($cnum,$cdom);
 4503:     my ($update,$comment) = split(/:/,&unescape(&reply('autophotochoice:'.
 4504: 						       &escape($cdom),
 4505: 						       $homeserver)));
 4506:     if ($update =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
 4507: 	return (undef,undef);
 4508:     }
 4509:     return ($update,$comment);
 4510: }
 4511: 
 4512: sub auto_photoupdate {
 4513:     my ($affiliatesref,$dom,$cnum,$photo) = @_;
 4514:     my $homeserver = &homeserver($cnum,$dom);
 4515:     my $host=&hostname($homeserver);
 4516:     my $cmd = '';
 4517:     my $maxtries = 1;
 4518:     foreach my $affiliate (keys(%{$affiliatesref})) {
 4519:         $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
 4520:     }
 4521:     $cmd =~ s/%%$//;
 4522:     $cmd = &escape($cmd);
 4523:     my $query = 'institutionalphotos';
 4524:     my $queryid=&reply("querysend:".$query.':'.$dom.':'.$cnum.':'.$cmd,$homeserver);
 4525:     unless ($queryid=~/^\Q$host\E\_/) {
 4526:         &logthis('institutionalphotos: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' and course: '.$cnum);
 4527:         return 'error: '.$queryid;
 4528:     }
 4529:     my $reply = &get_query_reply($queryid);
 4530:     my $tries = 1;
 4531:     while (($reply=~/^timeout/) && ($tries < $maxtries)) {
 4532:         $reply = &get_query_reply($queryid);
 4533:         $tries ++;
 4534:     }
 4535:     if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
 4536:         &logthis('institutionalphotos error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' course: '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
 4537:     } else {
 4538:         my @responses = split(/:/,$reply);
 4539:         my $outcome = shift(@responses); 
 4540:         foreach my $item (@responses) {
 4541:             my ($key,$value) = split(/=/,$item);
 4542:             $$photo{$key} = $value;
 4543:         }
 4544:         return $outcome;
 4545:     }
 4546:     return 'error';
 4547: }
 4548: 
 4549: sub auto_instcode_format {
 4550:     my ($caller,$codedom,$instcodes,$codes,$codetitles,$cat_titles,
 4551: 	$cat_order) = @_;
 4552:     my $courses = '';
 4553:     my @homeservers;
 4554:     if ($caller eq 'global') {
 4555: 	my %servers = &get_servers($codedom,'library');
 4556: 	foreach my $tryserver (keys(%servers)) {
 4557: 	    if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
 4558: 		push(@homeservers,$tryserver);
 4559: 	    }
 4560:         }
 4561:     } else {
 4562:         push(@homeservers,&homeserver($caller,$codedom));
 4563:     }
 4564:     foreach my $code (keys(%{$instcodes})) {
 4565:         $courses .= &escape($code).'='.&escape($$instcodes{$code}).'&';
 4566:     }
 4567:     chop($courses);
 4568:     my $ok_response = 0;
 4569:     my $response;
 4570:     while (@homeservers > 0 && $ok_response == 0) {
 4571:         my $server = shift(@homeservers); 
 4572:         $response=&reply('autoinstcodeformat:'.$codedom.':'.$courses,$server);
 4573:         if ($response !~ /(con_lost|error|no_such_host|refused)/) {
 4574:             my ($codes_str,$codetitles_str,$cat_titles_str,$cat_order_str) = 
 4575: 		split/:/,$response;
 4576:             %{$codes} = (%{$codes},&str2hash($codes_str));
 4577:             push(@{$codetitles},&str2array($codetitles_str));
 4578:             %{$cat_titles} = (%{$cat_titles},&str2hash($cat_titles_str));
 4579:             %{$cat_order} = (%{$cat_order},&str2hash($cat_order_str));
 4580:             $ok_response = 1;
 4581:         }
 4582:     }
 4583:     if ($ok_response) {
 4584:         return 'ok';
 4585:     } else {
 4586:         return $response;
 4587:     }
 4588: }
 4589: 
 4590: sub auto_instcode_defaults {
 4591:     my ($domain,$returnhash,$code_order) = @_;
 4592:     my @homeservers;
 4593: 
 4594:     my %servers = &get_servers($domain,'library');
 4595:     foreach my $tryserver (keys(%servers)) {
 4596: 	if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
 4597: 	    push(@homeservers,$tryserver);
 4598: 	}
 4599:     }
 4600: 
 4601:     my $response;
 4602:     foreach my $server (@homeservers) {
 4603:         $response=&reply('autoinstcodedefaults:'.$domain,$server);
 4604:         next if ($response =~ /(con_lost|error|no_such_host|refused)/);
 4605: 	
 4606: 	foreach my $pair (split(/\&/,$response)) {
 4607: 	    my ($name,$value)=split(/\=/,$pair);
 4608: 	    if ($name eq 'code_order') {
 4609: 		@{$code_order} = split(/\&/,&unescape($value));
 4610: 	    } else {
 4611: 		$returnhash->{&unescape($name)}=&unescape($value);
 4612: 	    }
 4613: 	}
 4614: 	return 'ok';
 4615:     }
 4616: 
 4617:     return $response;
 4618: } 
 4619: 
 4620: sub auto_validate_class_sec {
 4621:     my ($cdom,$cnum,$owner,$inst_class) = @_;
 4622:     my $homeserver = &homeserver($cnum,$cdom);
 4623:     my $response=&reply('autovalidateclass_sec:'.$inst_class.':'.
 4624:                         &escape($owner).':'.$cdom,$homeserver);
 4625:     return $response;
 4626: }
 4627: 
 4628: # ------------------------------------------------------- Course Group routines
 4629: 
 4630: sub get_coursegroups {
 4631:     my ($cdom,$cnum,$group,$namespace) = @_;
 4632:     return(&dump($namespace,$cdom,$cnum,$group));
 4633: }
 4634: 
 4635: sub modify_coursegroup {
 4636:     my ($cdom,$cnum,$groupsettings) = @_;
 4637:     return(&put('coursegroups',$groupsettings,$cdom,$cnum));
 4638: }
 4639: 
 4640: sub toggle_coursegroup_status {
 4641:     my ($cdom,$cnum,$group,$action) = @_;
 4642:     my ($from_namespace,$to_namespace);
 4643:     if ($action eq 'delete') {
 4644:         $from_namespace = 'coursegroups';
 4645:         $to_namespace = 'deleted_groups';
 4646:     } else {
 4647:         $from_namespace = 'deleted_groups';
 4648:         $to_namespace = 'coursegroups';
 4649:     }
 4650:     my %curr_group = &get_coursegroups($cdom,$cnum,$group,$from_namespace);
 4651:     if (my $tmp = &error(%curr_group)) {
 4652:         &Apache::lonnet::logthis('Error retrieving group: '.$tmp.' in '.$cnum.':'.$cdom);
 4653:         return ('read error',$tmp);
 4654:     } else {
 4655:         my %savedsettings = %curr_group; 
 4656:         my $result = &put($to_namespace,\%savedsettings,$cdom,$cnum);
 4657:         my $deloutcome;
 4658:         if ($result eq 'ok') {
 4659:             $deloutcome = &del($from_namespace,[$group],$cdom,$cnum);
 4660:         } else {
 4661:             return ('write error',$result);
 4662:         }
 4663:         if ($deloutcome eq 'ok') {
 4664:             return 'ok';
 4665:         } else {
 4666:             return ('delete error',$deloutcome);
 4667:         }
 4668:     }
 4669: }
 4670: 
 4671: sub modify_group_roles {
 4672:     my ($cdom,$cnum,$group_id,$user,$end,$start,$userprivs) = @_;
 4673:     my $url = '/'.$cdom.'/'.$cnum.'/'.$group_id;
 4674:     my $role = 'gr/'.&escape($userprivs);
 4675:     my ($uname,$udom) = split(/:/,$user);
 4676:     my $result = &assignrole($udom,$uname,$url,$role,$end,$start);
 4677:     if ($result eq 'ok') {
 4678:         &devalidate_getgroups_cache($udom,$uname,$cdom,$cnum);
 4679:     }
 4680:     return $result;
 4681: }
 4682: 
 4683: sub modify_coursegroup_membership {
 4684:     my ($cdom,$cnum,$membership) = @_;
 4685:     my $result = &put('groupmembership',$membership,$cdom,$cnum);
 4686:     return $result;
 4687: }
 4688: 
 4689: sub get_active_groups {
 4690:     my ($udom,$uname,$cdom,$cnum) = @_;
 4691:     my $now = time;
 4692:     my %groups = ();
 4693:     foreach my $key (keys(%env)) {
 4694:         if ($key =~ m-user\.role\.gr\./($match_domain)/($match_courseid)/(\w+)$-) {
 4695:             my ($start,$end) = split(/\./,$env{$key});
 4696:             if (($end!=0) && ($end<$now)) { next; }
 4697:             if (($start!=0) && ($start>$now)) { next; }
 4698:             if ($1 eq $cdom && $2 eq $cnum) {
 4699:                 $groups{$3} = $env{$key} ;
 4700:             }
 4701:         }
 4702:     }
 4703:     return %groups;
 4704: }
 4705: 
 4706: sub get_group_membership {
 4707:     my ($cdom,$cnum,$group) = @_;
 4708:     return(&dump('groupmembership',$cdom,$cnum,$group));
 4709: }
 4710: 
 4711: sub get_users_groups {
 4712:     my ($udom,$uname,$courseid) = @_;
 4713:     my @usersgroups;
 4714:     my $cachetime=1800;
 4715: 
 4716:     my $hashid="$udom:$uname:$courseid";
 4717:     my ($grouplist,$cached)=&is_cached_new('getgroups',$hashid);
 4718:     if (defined($cached)) {
 4719:         @usersgroups = split(/:/,$grouplist);
 4720:     } else {  
 4721:         $grouplist = '';
 4722:         my $courseurl = &courseid_to_courseurl($courseid);
 4723:         my %roleshash = &dump('roles',$udom,$uname,$courseurl);
 4724:         my $access_end = $env{'course.'.$courseid.
 4725:                               '.default_enrollment_end_date'};
 4726:         my $now = time;
 4727:         foreach my $key (keys(%roleshash)) {
 4728:             if ($key =~ /^\Q$courseurl\E\/(\w+)\_gr$/) {
 4729:                 my $group = $1;
 4730:                 if ($roleshash{$key} =~ /_(\d+)_(\d+)$/) {
 4731:                     my $start = $2;
 4732:                     my $end = $1;
 4733:                     if ($start == -1) { next; } # deleted from group
 4734:                     if (($start!=0) && ($start>$now)) { next; }
 4735:                     if (($end!=0) && ($end<$now)) {
 4736:                         if ($access_end && $access_end < $now) {
 4737:                             if ($access_end - $end < 86400) {
 4738:                                 push(@usersgroups,$group);
 4739:                             }
 4740:                         }
 4741:                         next;
 4742:                     }
 4743:                     push(@usersgroups,$group);
 4744:                 }
 4745:             }
 4746:         }
 4747:         @usersgroups = &sort_course_groups($courseid,@usersgroups);
 4748:         $grouplist = join(':',@usersgroups);
 4749:         &do_cache_new('getgroups',$hashid,$grouplist,$cachetime);
 4750:     }
 4751:     return @usersgroups;
 4752: }
 4753: 
 4754: sub devalidate_getgroups_cache {
 4755:     my ($udom,$uname,$cdom,$cnum)=@_;
 4756:     my $courseid = $cdom.'_'.$cnum;
 4757: 
 4758:     my $hashid="$udom:$uname:$courseid";
 4759:     &devalidate_cache_new('getgroups',$hashid);
 4760: }
 4761: 
 4762: # ------------------------------------------------------------------ Plain Text
 4763: 
 4764: sub plaintext {
 4765:     my ($short,$type,$cid) = @_;
 4766:     if ($short =~ /^cr/) {
 4767: 	return (split('/',$short))[-1];
 4768:     }
 4769:     if (!defined($cid)) {
 4770:         $cid = $env{'request.course.id'};
 4771:     }
 4772:     if (defined($cid) && defined($env{'course.'.$cid.'.'.$short.'.plaintext'})) {
 4773:         return &Apache::lonlocal::mt($env{'course.'.$cid.'.'.$short.
 4774:                                           '.plaintext'});
 4775:     }
 4776:     my %rolenames = (
 4777:                       Course => 'std',
 4778:                       Group => 'alt1',
 4779:                     );
 4780:     if (defined($type) && 
 4781:          defined($rolenames{$type}) && 
 4782:          defined($prp{$short}{$rolenames{$type}})) {
 4783:         return &Apache::lonlocal::mt($prp{$short}{$rolenames{$type}});
 4784:     } else {
 4785:         return &Apache::lonlocal::mt($prp{$short}{'std'});
 4786:     }
 4787: }
 4788: 
 4789: # ----------------------------------------------------------------- Assign Role
 4790: 
 4791: sub assignrole {
 4792:     my ($udom,$uname,$url,$role,$end,$start,$deleteflag)=@_;
 4793:     my $mrole;
 4794:     if ($role =~ /^cr\//) {
 4795:         my $cwosec=$url;
 4796:         $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
 4797: 	unless (&allowed('ccr',$cwosec)) {
 4798:            &logthis('Refused custom assignrole: '.
 4799:              $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
 4800: 		    $env{'user.name'}.' at '.$env{'user.domain'});
 4801:            return 'refused'; 
 4802:         }
 4803:         $mrole='cr';
 4804:     } elsif ($role =~ /^gr\//) {
 4805:         my $cwogrp=$url;
 4806:         $cwogrp=~s{^/($match_domain)/($match_courseid)/.*}{$1/$2};
 4807:         unless (&allowed('mdg',$cwogrp)) {
 4808:             &logthis('Refused group assignrole: '.
 4809:               $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
 4810:                     $env{'user.name'}.' at '.$env{'user.domain'});
 4811:             return 'refused';
 4812:         }
 4813:         $mrole='gr';
 4814:     } else {
 4815:         my $cwosec=$url;
 4816:         $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
 4817:         unless ((&allowed('c'.$role,$cwosec)) || &allowed('c'.$role,$udom)) { 
 4818:            &logthis('Refused assignrole: '.
 4819:              $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
 4820: 		    $env{'user.name'}.' at '.$env{'user.domain'});
 4821:            return 'refused'; 
 4822:         }
 4823:         $mrole=$role;
 4824:     }
 4825:     my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
 4826:                 "$udom:$uname:$url".'_'."$mrole=$role";
 4827:     if ($end) { $command.='_'.$end; }
 4828:     if ($start) {
 4829: 	if ($end) { 
 4830:            $command.='_'.$start; 
 4831:         } else {
 4832:            $command.='_0_'.$start;
 4833:         }
 4834:     }
 4835:     my $origstart = $start;
 4836:     my $origend = $end;
 4837: # actually delete
 4838:     if ($deleteflag) {
 4839: 	if ((&allowed('dro',$udom)) || (&allowed('dro',$url))) {
 4840: # modify command to delete the role
 4841:            $command="encrypt:rolesdel:$env{'user.domain'}:$env{'user.name'}:".
 4842:                 "$udom:$uname:$url".'_'."$mrole";
 4843: 	   &logthis("$env{'user.name'} at $env{'user.domain'} deletes $mrole in $url for $uname at $udom"); 
 4844: # set start and finish to negative values for userrolelog
 4845:            $start=-1;
 4846:            $end=-1;
 4847:         }
 4848:     }
 4849: # send command
 4850:     my $answer=&reply($command,&homeserver($uname,$udom));
 4851: # log new user role if status is ok
 4852:     if ($answer eq 'ok') {
 4853: 	&userrolelog($role,$uname,$udom,$url,$start,$end);
 4854: # for course roles, perform group memberships changes triggered by role change.
 4855:         unless ($role =~ /^gr/) {
 4856:             &Apache::longroup::group_changes($udom,$uname,$url,$role,$origend,
 4857:                                              $origstart);
 4858:         }
 4859:     }
 4860:     return $answer;
 4861: }
 4862: 
 4863: # -------------------------------------------------- Modify user authentication
 4864: # Overrides without validation
 4865: 
 4866: sub modifyuserauth {
 4867:     my ($udom,$uname,$umode,$upass)=@_;
 4868:     my $uhome=&homeserver($uname,$udom);
 4869:     unless (&allowed('mau',$udom)) { return 'refused'; }
 4870:     &logthis('Call to modify user authentication '.$udom.', '.$uname.', '.
 4871:              $umode.' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
 4872:              ' in domain '.$env{'request.role.domain'});  
 4873:     my $reply=&reply('encrypt:changeuserauth:'.$udom.':'.$uname.':'.$umode.':'.
 4874: 		     &escape($upass),$uhome);
 4875:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
 4876:         'Authentication changed for '.$udom.', '.$uname.', '.$umode.
 4877:          '(Remote '.$ENV{'REMOTE_ADDR'}.'): '.$reply);
 4878:     &log($udom,,$uname,$uhome,
 4879:         'Authentication changed by '.$env{'user.domain'}.', '.
 4880:                                      $env{'user.name'}.', '.$umode.
 4881:          '(Remote '.$ENV{'REMOTE_ADDR'}.'): '.$reply);
 4882:     unless ($reply eq 'ok') {
 4883:         &logthis('Authentication mode error: '.$reply);
 4884: 	return 'error: '.$reply;
 4885:     }   
 4886:     return 'ok';
 4887: }
 4888: 
 4889: # --------------------------------------------------------------- Modify a user
 4890: 
 4891: sub modifyuser {
 4892:     my ($udom,    $uname, $uid,
 4893:         $umode,   $upass, $first,
 4894:         $middle,  $last,  $gene,
 4895:         $forceid, $desiredhome, $email)=@_;
 4896:     $udom= &LONCAPA::clean_domain($udom);
 4897:     $uname=&LONCAPA::clean_username($uname);
 4898:     &logthis('Call to modify user '.$udom.', '.$uname.', '.$uid.', '.
 4899:              $umode.', '.$first.', '.$middle.', '.
 4900: 	     $last.', '.$gene.'(forceid: '.$forceid.')'.
 4901:              (defined($desiredhome) ? ' desiredhome = '.$desiredhome :
 4902:                                      ' desiredhome not specified'). 
 4903:              ' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
 4904:              ' in domain '.$env{'request.role.domain'});
 4905:     my $uhome=&homeserver($uname,$udom,'true');
 4906: # ----------------------------------------------------------------- Create User
 4907:     if (($uhome eq 'no_host') && 
 4908: 	(($umode && $upass) || ($umode eq 'localauth'))) {
 4909:         my $unhome='';
 4910:         if (defined($desiredhome) && &host_domain($desiredhome) eq $udom) { 
 4911:             $unhome = $desiredhome;
 4912: 	} elsif($env{'course.'.$env{'request.course.id'}.'.domain'} eq $udom) {
 4913: 	    $unhome=$env{'course.'.$env{'request.course.id'}.'.home'};
 4914:         } else { # load balancing routine for determining $unhome
 4915:             my $loadm=10000000;
 4916: 	    my %servers = &get_servers($udom,'library');
 4917: 	    foreach my $tryserver (keys(%servers)) {
 4918: 		my $answer=reply('load',$tryserver);
 4919: 		if (($answer=~/\d+/) && ($answer<$loadm)) {
 4920: 		    $loadm=$answer;
 4921: 		    $unhome=$tryserver;
 4922: 		}
 4923: 	    }
 4924:         }
 4925:         if (($unhome eq '') || ($unhome eq 'no_host')) {
 4926: 	    return 'error: unable to find a home server for '.$uname.
 4927:                    ' in domain '.$udom;
 4928:         }
 4929:         my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':'.$umode.':'.
 4930:                          &escape($upass),$unhome);
 4931: 	unless ($reply eq 'ok') {
 4932:             return 'error: '.$reply;
 4933:         }   
 4934:         $uhome=&homeserver($uname,$udom,'true');
 4935:         if (($uhome eq '') || ($uhome eq 'no_host') || ($uhome ne $unhome)) {
 4936: 	    return 'error: unable verify users home machine.';
 4937:         }
 4938:     }   # End of creation of new user
 4939: # ---------------------------------------------------------------------- Add ID
 4940:     if ($uid) {
 4941:        $uid=~tr/A-Z/a-z/;
 4942:        my %uidhash=&idrget($udom,$uname);
 4943:        if (($uidhash{$uname}) && ($uidhash{$uname}!~/error\:/) 
 4944:          && (!$forceid)) {
 4945: 	  unless ($uid eq $uidhash{$uname}) {
 4946: 	      return 'error: user id "'.$uid.'" does not match '.
 4947:                   'current user id "'.$uidhash{$uname}.'".';
 4948:           }
 4949:        } else {
 4950: 	  &idput($udom,($uname => $uid));
 4951:        }
 4952:     }
 4953: # -------------------------------------------------------------- Add names, etc
 4954:     my @tmp=&get('environment',
 4955: 		   ['firstname','middlename','lastname','generation'],
 4956: 		   $udom,$uname);
 4957:     my %names;
 4958:     if ($tmp[0] =~ m/^error:.*/) { 
 4959:         %names=(); 
 4960:     } else {
 4961:         %names = @tmp;
 4962:     }
 4963: #
 4964: # Make sure to not trash student environment if instructor does not bother
 4965: # to supply name and email information
 4966: #
 4967:     if ($first)  { $names{'firstname'}  = $first; }
 4968:     if (defined($middle)) { $names{'middlename'} = $middle; }
 4969:     if ($last)   { $names{'lastname'}   = $last; }
 4970:     if (defined($gene))   { $names{'generation'} = $gene; }
 4971:     if ($email) {
 4972:        $email=~s/[^\w\@\.\-\,]//gs;
 4973:        if ($email=~/\@/) { $names{'notification'} = $email;
 4974: 			   $names{'critnotification'} = $email;
 4975: 			   $names{'permanentemail'} = $email; }
 4976:     }
 4977:     my $reply = &put('environment', \%names, $udom,$uname);
 4978:     if ($reply ne 'ok') { return 'error: '.$reply; }
 4979:     &devalidate_cache_new('namescache',$uname.':'.$udom);
 4980:     &logthis('Success modifying user '.$udom.', '.$uname.', '.$uid.', '.
 4981:              $umode.', '.$first.', '.$middle.', '.
 4982: 	     $last.', '.$gene.' by '.
 4983:              $env{'user.name'}.' at '.$env{'user.domain'});
 4984:     return 'ok';
 4985: }
 4986: 
 4987: # -------------------------------------------------------------- Modify student
 4988: 
 4989: sub modifystudent {
 4990:     my ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$usec,
 4991:         $end,$start,$forceid,$desiredhome,$email,$type,$locktype,$cid)=@_;
 4992:     if (!$cid) {
 4993: 	unless ($cid=$env{'request.course.id'}) {
 4994: 	    return 'not_in_class';
 4995: 	}
 4996:     }
 4997: # --------------------------------------------------------------- Make the user
 4998:     my $reply=&modifyuser
 4999: 	($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$forceid,
 5000:          $desiredhome,$email);
 5001:     unless ($reply eq 'ok') { return $reply; }
 5002:     # This will cause &modify_student_enrollment to get the uid from the
 5003:     # students environment
 5004:     $uid = undef if (!$forceid);
 5005:     $reply = &modify_student_enrollment($udom,$uname,$uid,$first,$middle,$last,
 5006: 					$gene,$usec,$end,$start,$type,$locktype,$cid);
 5007:     return $reply;
 5008: }
 5009: 
 5010: sub modify_student_enrollment {
 5011:     my ($udom,$uname,$uid,$first,$middle,$last,$gene,$usec,$end,$start,$type,$locktype,$cid) = @_;
 5012:     my ($cdom,$cnum,$chome);
 5013:     if (!$cid) {
 5014: 	unless ($cid=$env{'request.course.id'}) {
 5015: 	    return 'not_in_class';
 5016: 	}
 5017: 	$cdom=$env{'course.'.$cid.'.domain'};
 5018: 	$cnum=$env{'course.'.$cid.'.num'};
 5019:     } else {
 5020: 	($cdom,$cnum)=split(/_/,$cid);
 5021:     }
 5022:     $chome=$env{'course.'.$cid.'.home'};
 5023:     if (!$chome) {
 5024: 	$chome=&homeserver($cnum,$cdom);
 5025:     }
 5026:     if (!$chome) { return 'unknown_course'; }
 5027:     # Make sure the user exists
 5028:     my $uhome=&homeserver($uname,$udom);
 5029:     if (($uhome eq '') || ($uhome eq 'no_host')) { 
 5030: 	return 'error: no such user';
 5031:     }
 5032:     # Get student data if we were not given enough information
 5033:     if (!defined($first)  || $first  eq '' || 
 5034:         !defined($last)   || $last   eq '' || 
 5035:         !defined($uid)    || $uid    eq '' || 
 5036:         !defined($middle) || $middle eq '' || 
 5037:         !defined($gene)   || $gene   eq '') {
 5038:         # They did not supply us with enough data to enroll the student, so
 5039:         # we need to pick up more information.
 5040:         my %tmp = &get('environment',
 5041:                        ['firstname','middlename','lastname', 'generation','id']
 5042:                        ,$udom,$uname);
 5043: 
 5044:         #foreach my $key (keys(%tmp)) {
 5045:         #    &logthis("key $key = ".$tmp{$key});
 5046:         #}
 5047:         $first  = $tmp{'firstname'}  if (!defined($first)  || $first  eq '');
 5048:         $middle = $tmp{'middlename'} if (!defined($middle) || $middle eq '');
 5049:         $last   = $tmp{'lastname'}   if (!defined($last)   || $last eq '');
 5050:         $gene   = $tmp{'generation'} if (!defined($gene)   || $gene eq '');
 5051:         $uid    = $tmp{'id'}         if (!defined($uid)    || $uid  eq '');
 5052:     }
 5053:     my $fullname = &format_name($first,$middle,$last,$gene,'lastname');
 5054:     my $reply=cput('classlist',
 5055: 		   {"$uname:$udom" => 
 5056: 			join(':',$end,$start,$uid,$usec,$fullname,$type,$locktype) },
 5057: 		   $cdom,$cnum);
 5058:     unless (($reply eq 'ok') || ($reply eq 'delayed')) {
 5059: 	return 'error: '.$reply;
 5060:     } else {
 5061: 	&devalidate_getsection_cache($udom,$uname,$cid);
 5062:     }
 5063:     # Add student role to user
 5064:     my $uurl='/'.$cid;
 5065:     $uurl=~s/\_/\//g;
 5066:     if ($usec) {
 5067: 	$uurl.='/'.$usec;
 5068:     }
 5069:     return &assignrole($udom,$uname,$uurl,'st',$end,$start);
 5070: }
 5071: 
 5072: sub format_name {
 5073:     my ($firstname,$middlename,$lastname,$generation,$first)=@_;
 5074:     my $name;
 5075:     if ($first ne 'lastname') {
 5076: 	$name=$firstname.' '.$middlename.' '.$lastname.' '.$generation;
 5077:     } else {
 5078: 	if ($lastname=~/\S/) {
 5079: 	    $name.= $lastname.' '.$generation.', '.$firstname.' '.$middlename;
 5080: 	    $name=~s/\s+,/,/;
 5081: 	} else {
 5082: 	    $name.= $firstname.' '.$middlename.' '.$generation;
 5083: 	}
 5084:     }
 5085:     $name=~s/^\s+//;
 5086:     $name=~s/\s+$//;
 5087:     $name=~s/\s+/ /g;
 5088:     return $name;
 5089: }
 5090: 
 5091: # ------------------------------------------------- Write to course preferences
 5092: 
 5093: sub writecoursepref {
 5094:     my ($courseid,%prefs)=@_;
 5095:     $courseid=~s/^\///;
 5096:     $courseid=~s/\_/\//g;
 5097:     my ($cdomain,$cnum)=split(/\//,$courseid);
 5098:     my $chome=homeserver($cnum,$cdomain);
 5099:     if (($chome eq '') || ($chome eq 'no_host')) { 
 5100: 	return 'error: no such course';
 5101:     }
 5102:     my $cstring='';
 5103:     foreach my $pref (keys(%prefs)) {
 5104: 	$cstring.=&escape($pref).'='.&escape($prefs{$pref}).'&';
 5105:     }
 5106:     $cstring=~s/\&$//;
 5107:     return reply('put:'.$cdomain.':'.$cnum.':environment:'.$cstring,$chome);
 5108: }
 5109: 
 5110: # ---------------------------------------------------------- Make/modify course
 5111: 
 5112: sub createcourse {
 5113:     my ($udom,$description,$url,$course_server,$nonstandard,$inst_code,
 5114:         $course_owner,$crstype)=@_;
 5115:     $url=&declutter($url);
 5116:     my $cid='';
 5117:     unless (&allowed('ccc',$udom)) {
 5118:         return 'refused';
 5119:     }
 5120: # ------------------------------------------------------------------- Create ID
 5121:    my $uname=int(1+rand(9)).
 5122:        ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
 5123:        substr($$.time,0,5).unpack("H8",pack("I32",time)).
 5124:        unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
 5125: # ----------------------------------------------- Make sure that does not exist
 5126:    my $uhome=&homeserver($uname,$udom,'true');
 5127:    unless (($uhome eq '') || ($uhome eq 'no_host')) {
 5128:        $uname=substr($$.time,0,5).unpack("H8",pack("I32",time)).
 5129:         unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
 5130:        $uhome=&homeserver($uname,$udom,'true');       
 5131:        unless (($uhome eq '') || ($uhome eq 'no_host')) {
 5132:            return 'error: unable to generate unique course-ID';
 5133:        } 
 5134:    }
 5135: # ------------------------------------------------ Check supplied server name
 5136:     $course_server = $env{'user.homeserver'} if (! defined($course_server));
 5137:     if (! &is_library($course_server)) {
 5138:         return 'error:bad server name '.$course_server;
 5139:     }
 5140: # ------------------------------------------------------------- Make the course
 5141:     my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':none::',
 5142:                       $course_server);
 5143:     unless ($reply eq 'ok') { return 'error: '.$reply; }
 5144:     $uhome=&homeserver($uname,$udom,'true');
 5145:     if (($uhome eq '') || ($uhome eq 'no_host')) { 
 5146: 	return 'error: no such course';
 5147:     }
 5148: # ----------------------------------------------------------------- Course made
 5149: # log existence
 5150:     &courseidput($udom,&escape($udom.'_'.$uname).'='.&escape($description).
 5151:                  ':'.&escape($inst_code).':'.&escape($course_owner).':'.
 5152:                   &escape($crstype),$uhome);
 5153:     &flushcourselogs();
 5154: # set toplevel url
 5155:     my $topurl=$url;
 5156:     unless ($nonstandard) {
 5157: # ------------------------------------------ For standard courses, make top url
 5158:         my $mapurl=&clutter($url);
 5159:         if ($mapurl eq '/res/') { $mapurl=''; }
 5160:         $env{'form.initmap'}=(<<ENDINITMAP);
 5161: <map>
 5162: <resource id="1" type="start"></resource>
 5163: <resource id="2" src="$mapurl"></resource>
 5164: <resource id="3" type="finish"></resource>
 5165: <link index="1" from="1" to="2"></link>
 5166: <link index="2" from="2" to="3"></link>
 5167: </map>
 5168: ENDINITMAP
 5169:         $topurl=&declutter(
 5170:         &finishuserfileupload($uname,$udom,'initmap','default.sequence')
 5171:                           );
 5172:     }
 5173: # ----------------------------------------------------------- Write preferences
 5174:     &writecoursepref($udom.'_'.$uname,
 5175:                      ('description' => $description,
 5176:                       'url'         => $topurl));
 5177:     return '/'.$udom.'/'.$uname;
 5178: }
 5179: 
 5180: sub is_course {
 5181:     my ($cdom,$cnum) = @_;
 5182:     my %courses = &courseiddump($cdom,'.',1,'.','.',$cnum,undef,
 5183: 				undef,'.');
 5184:     if (exists($courses{$cdom.'_'.$cnum})) {
 5185:         return 1;
 5186:     }
 5187:     return 0;
 5188: }
 5189: 
 5190: # ---------------------------------------------------------- Assign Custom Role
 5191: 
 5192: sub assigncustomrole {
 5193:     my ($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start,$deleteflag)=@_;
 5194:     return &assignrole($udom,$uname,$url,'cr/'.$rdom.'/'.$rnam.'/'.$rolename,
 5195:                        $end,$start,$deleteflag);
 5196: }
 5197: 
 5198: # ----------------------------------------------------------------- Revoke Role
 5199: 
 5200: sub revokerole {
 5201:     my ($udom,$uname,$url,$role,$deleteflag)=@_;
 5202:     my $now=time;
 5203:     return &assignrole($udom,$uname,$url,$role,$now,$deleteflag);
 5204: }
 5205: 
 5206: # ---------------------------------------------------------- Revoke Custom Role
 5207: 
 5208: sub revokecustomrole {
 5209:     my ($udom,$uname,$url,$rdom,$rnam,$rolename,$deleteflag)=@_;
 5210:     my $now=time;
 5211:     return &assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$now,
 5212:            $deleteflag);
 5213: }
 5214: 
 5215: # ------------------------------------------------------------ Disk usage
 5216: sub diskusage {
 5217:     my ($udom,$uname,$directoryRoot)=@_;
 5218:     $directoryRoot =~ s/\/$//;
 5219:     my $listing=&reply('du:'.$directoryRoot,homeserver($uname,$udom));
 5220:     return $listing;
 5221: }
 5222: 
 5223: sub is_locked {
 5224:     my ($file_name, $domain, $user) = @_;
 5225:     my @check;
 5226:     my $is_locked;
 5227:     push @check, $file_name;
 5228:     my %locked = &get('file_permissions',\@check,
 5229: 		      $env{'user.domain'},$env{'user.name'});
 5230:     my ($tmp)=keys(%locked);
 5231:     if ($tmp=~/^error:/) { undef(%locked); }
 5232:     
 5233:     if (ref($locked{$file_name}) eq 'ARRAY') {
 5234:         $is_locked = 'false';
 5235:         foreach my $entry (@{$locked{$file_name}}) {
 5236:            if (ref($entry) eq 'ARRAY') { 
 5237:                $is_locked = 'true';
 5238:                last;
 5239:            }
 5240:        }
 5241:     } else {
 5242:         $is_locked = 'false';
 5243:     }
 5244: }
 5245: 
 5246: sub declutter_portfile {
 5247:     my ($file) = @_;
 5248:     $file =~ s{^(/portfolio/|portfolio/)}{/};
 5249:     return $file;
 5250: }
 5251: 
 5252: # ------------------------------------------------------------- Mark as Read Only
 5253: 
 5254: sub mark_as_readonly {
 5255:     my ($domain,$user,$files,$what) = @_;
 5256:     my %current_permissions = &dump('file_permissions',$domain,$user);
 5257:     my ($tmp)=keys(%current_permissions);
 5258:     if ($tmp=~/^error:/) { undef(%current_permissions); }
 5259:     foreach my $file (@{$files}) {
 5260: 	$file = &declutter_portfile($file);
 5261:         push(@{$current_permissions{$file}},$what);
 5262:     }
 5263:     &put('file_permissions',\%current_permissions,$domain,$user);
 5264:     return;
 5265: }
 5266: 
 5267: # ------------------------------------------------------------Save Selected Files
 5268: 
 5269: sub save_selected_files {
 5270:     my ($user, $path, @files) = @_;
 5271:     my $filename = $user."savedfiles";
 5272:     my @other_files = &files_not_in_path($user, $path);
 5273:     open (OUT, '>'.$Apache::lonnet::perlvar{'lonDaemons'}.'/tmp/'.$filename);
 5274:     foreach my $file (@files) {
 5275:         print (OUT $env{'form.currentpath'}.$file."\n");
 5276:     }
 5277:     foreach my $file (@other_files) {
 5278:         print (OUT $file."\n");
 5279:     }
 5280:     close (OUT);
 5281:     return 'ok';
 5282: }
 5283: 
 5284: sub clear_selected_files {
 5285:     my ($user) = @_;
 5286:     my $filename = $user."savedfiles";
 5287:     open (OUT, '>'.$Apache::lonnet::perlvar{'lonDaemons'}.'/tmp/'.$filename);
 5288:     print (OUT undef);
 5289:     close (OUT);
 5290:     return ("ok");    
 5291: }
 5292: 
 5293: sub files_in_path {
 5294:     my ($user, $path) = @_;
 5295:     my $filename = $user."savedfiles";
 5296:     my %return_files;
 5297:     open (IN, '<'.$Apache::lonnet::perlvar{'lonDaemons'}.'/tmp/'.$filename);
 5298:     while (my $line_in = <IN>) {
 5299:         chomp ($line_in);
 5300:         my @paths_and_file = split (m!/!, $line_in);
 5301:         my $file_part = pop (@paths_and_file);
 5302:         my $path_part = join ('/', @paths_and_file);
 5303:         $path_part.='/';
 5304:         my $path_and_file = $path_part.$file_part;
 5305:         if ($path_part eq $path) {
 5306:             $return_files{$file_part}= 'selected';
 5307:         }
 5308:     }
 5309:     close (IN);
 5310:     return (\%return_files);
 5311: }
 5312: 
 5313: # called in portfolio select mode, to show files selected NOT in current directory
 5314: sub files_not_in_path {
 5315:     my ($user, $path) = @_;
 5316:     my $filename = $user."savedfiles";
 5317:     my @return_files;
 5318:     my $path_part;
 5319:     open(IN, '<'.$Apache::lonnet::perlvar{'lonDaemons'}.'/tmp/'.$filename);
 5320:     while (my $line = <IN>) {
 5321:         #ok, I know it's clunky, but I want it to work
 5322:         my @paths_and_file = split(m|/|, $line);
 5323:         my $file_part = pop(@paths_and_file);
 5324:         chomp($file_part);
 5325:         my $path_part = join('/', @paths_and_file);
 5326:         $path_part .= '/';
 5327:         my $path_and_file = $path_part.$file_part;
 5328:         if ($path_part ne $path) {
 5329:             push(@return_files, ($path_and_file));
 5330:         }
 5331:     }
 5332:     close(OUT);
 5333:     return (@return_files);
 5334: }
 5335: 
 5336: #----------------------------------------------Get portfolio file permissions
 5337: 
 5338: sub get_portfile_permissions {
 5339:     my ($domain,$user) = @_;
 5340:     my %current_permissions = &dump('file_permissions',$domain,$user);
 5341:     my ($tmp)=keys(%current_permissions);
 5342:     if ($tmp=~/^error:/) { undef(%current_permissions); }
 5343:     return \%current_permissions;
 5344: }
 5345: 
 5346: #---------------------------------------------Get portfolio file access controls
 5347: 
 5348: sub get_access_controls {
 5349:     my ($current_permissions,$group,$file) = @_;
 5350:     my %access;
 5351:     my $real_file = $file;
 5352:     $file =~ s/\.meta$//;
 5353:     if (defined($file)) {
 5354:         if (ref($$current_permissions{$file."\0".'accesscontrol'}) eq 'HASH') {
 5355:             foreach my $control (keys(%{$$current_permissions{$file."\0".'accesscontrol'}})) {
 5356:                 $access{$real_file}{$control} = $$current_permissions{$file."\0".$control};
 5357:             }
 5358:         }
 5359:     } else {
 5360:         foreach my $key (keys(%{$current_permissions})) {
 5361:             if ($key =~ /\0accesscontrol$/) {
 5362:                 if (defined($group)) {
 5363:                     if ($key !~ m-^\Q$group\E/-) {
 5364:                         next;
 5365:                     }
 5366:                 }
 5367:                 my ($fullpath) = split(/\0/,$key);
 5368:                 if (ref($$current_permissions{$key}) eq 'HASH') {
 5369:                     foreach my $control (keys(%{$$current_permissions{$key}})) {
 5370:                         $access{$fullpath}{$control}=$$current_permissions{$fullpath."\0".$control};
 5371:                     }
 5372:                 }
 5373:             }
 5374:         }
 5375:     }
 5376:     return %access;
 5377: }
 5378: 
 5379: sub modify_access_controls {
 5380:     my ($file_name,$changes,$domain,$user)=@_;
 5381:     my ($outcome,$deloutcome);
 5382:     my %store_permissions;
 5383:     my %new_values;
 5384:     my %new_control;
 5385:     my %translation;
 5386:     my @deletions = ();
 5387:     my $now = time;
 5388:     if (exists($$changes{'activate'})) {
 5389:         if (ref($$changes{'activate'}) eq 'HASH') {
 5390:             my @newitems = sort(keys(%{$$changes{'activate'}}));
 5391:             my $numnew = scalar(@newitems);
 5392:             for (my $i=0; $i<$numnew; $i++) {
 5393:                 my $newkey = $newitems[$i];
 5394:                 my $newid = &Apache::loncommon::get_cgi_id();
 5395:                 if ($newkey =~ /^\d+:/) { 
 5396:                     $newkey =~ s/^(\d+)/$newid/;
 5397:                     $translation{$1} = $newid;
 5398:                 } elsif ($newkey =~ /^\d+_\d+_\d+:/) {
 5399:                     $newkey =~ s/^(\d+_\d+_\d+)/$newid/;
 5400:                     $translation{$1} = $newid;
 5401:                 }
 5402:                 $new_values{$file_name."\0".$newkey} = 
 5403:                                           $$changes{'activate'}{$newitems[$i]};
 5404:                 $new_control{$newkey} = $now;
 5405:             }
 5406:         }
 5407:     }
 5408:     my %todelete;
 5409:     my %changed_items;
 5410:     foreach my $action ('delete','update') {
 5411:         if (exists($$changes{$action})) {
 5412:             if (ref($$changes{$action}) eq 'HASH') {
 5413:                 foreach my $key (keys(%{$$changes{$action}})) {
 5414:                     my ($itemnum) = ($key =~ /^([^:]+):/);
 5415:                     if ($action eq 'delete') { 
 5416:                         $todelete{$itemnum} = 1;
 5417:                     } else {
 5418:                         $changed_items{$itemnum} = $key;
 5419:                     }
 5420:                 }
 5421:             }
 5422:         }
 5423:     }
 5424:     # get lock on access controls for file.
 5425:     my $lockhash = {
 5426:                   $file_name."\0".'locked_access_records' => $env{'user.name'}.
 5427:                                                        ':'.$env{'user.domain'},
 5428:                    }; 
 5429:     my $tries = 0;
 5430:     my $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
 5431:    
 5432:     while (($gotlock ne 'ok') && $tries <3) {
 5433:         $tries ++;
 5434:         sleep 1;
 5435:         $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
 5436:     }
 5437:     if ($gotlock eq 'ok') {
 5438:         my %curr_permissions = &dump('file_permissions',$domain,$user,$file_name);
 5439:         my ($tmp)=keys(%curr_permissions);
 5440:         if ($tmp=~/^error:/) { undef(%curr_permissions); }
 5441:         if (exists($curr_permissions{$file_name."\0".'accesscontrol'})) {
 5442:             my $curr_controls = $curr_permissions{$file_name."\0".'accesscontrol'};
 5443:             if (ref($curr_controls) eq 'HASH') {
 5444:                 foreach my $control_item (keys(%{$curr_controls})) {
 5445:                     my ($itemnum) = ($control_item =~ /^([^:]+):/);
 5446:                     if (defined($todelete{$itemnum})) {
 5447:                         push(@deletions,$file_name."\0".$control_item);
 5448:                     } else {
 5449:                         if (defined($changed_items{$itemnum})) {
 5450:                             $new_control{$changed_items{$itemnum}} = $now;
 5451:                             push(@deletions,$file_name."\0".$control_item);
 5452:                             $new_values{$file_name."\0".$changed_items{$itemnum}} = $$changes{'update'}{$changed_items{$itemnum}};
 5453:                         } else {
 5454:                             $new_control{$control_item} = $$curr_controls{$control_item};
 5455:                         }
 5456:                     }
 5457:                 }
 5458:             }
 5459:         }
 5460:         $deloutcome = &del('file_permissions',\@deletions,$domain,$user);
 5461:         $new_values{$file_name."\0".'accesscontrol'} = \%new_control;
 5462:         $outcome = &put('file_permissions',\%new_values,$domain,$user);
 5463:         #  remove lock
 5464:         my @del_lock = ($file_name."\0".'locked_access_records');
 5465:         my $dellockoutcome = &del('file_permissions',\@del_lock,$domain,$user);
 5466:         my ($file,$group);
 5467:         if (&is_course($domain,$user)) {
 5468:             ($group,$file) = split(/\//,$file_name,2);
 5469:         } else {
 5470:             $file = $file_name;
 5471:         }
 5472:         my $sqlresult =
 5473:             &update_portfolio_table($user,$domain,$file,'portfolio_access',
 5474:                                     $group);
 5475:     } else {
 5476:         $outcome = "error: could not obtain lockfile\n";  
 5477:     }
 5478:     return ($outcome,$deloutcome,\%new_values,\%translation);
 5479: }
 5480: 
 5481: sub make_public_indefinitely {
 5482:     my ($requrl) = @_;
 5483:     my $now = time;
 5484:     my $action = 'activate';
 5485:     my $aclnum = 0;
 5486:     if (&is_portfolio_url($requrl)) {
 5487:         my (undef,$udom,$unum,$file_name,$group) =
 5488:             &parse_portfolio_url($requrl);
 5489:         my $current_perms = &get_portfile_permissions($udom,$unum);
 5490:         my %access_controls = &get_access_controls($current_perms,
 5491:                                                    $group,$file_name);
 5492:         foreach my $key (keys(%{$access_controls{$file_name}})) {
 5493:             my ($num,$scope,$end,$start) = 
 5494:                 ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
 5495:             if ($scope eq 'public') {
 5496:                 if ($start <= $now && $end == 0) {
 5497:                     $action = 'none';
 5498:                 } else {
 5499:                     $action = 'update';
 5500:                     $aclnum = $num;
 5501:                 }
 5502:                 last;
 5503:             }
 5504:         }
 5505:         if ($action eq 'none') {
 5506:              return 'ok';
 5507:         } else {
 5508:             my %changes;
 5509:             my $newend = 0;
 5510:             my $newstart = $now;
 5511:             my $newkey = $aclnum.':public_'.$newend.'_'.$newstart;
 5512:             $changes{$action}{$newkey} = {
 5513:                 type => 'public',
 5514:                 time => {
 5515:                     start => $newstart,
 5516:                     end   => $newend,
 5517:                 },
 5518:             };
 5519:             my ($outcome,$deloutcome,$new_values,$translation) =
 5520:                 &modify_access_controls($file_name,\%changes,$udom,$unum);
 5521:             return $outcome;
 5522:         }
 5523:     } else {
 5524:         return 'invalid';
 5525:     }
 5526: }
 5527: 
 5528: #------------------------------------------------------Get Marked as Read Only
 5529: 
 5530: sub get_marked_as_readonly {
 5531:     my ($domain,$user,$what,$group) = @_;
 5532:     my $current_permissions = &get_portfile_permissions($domain,$user);
 5533:     my @readonly_files;
 5534:     my $cmp1=$what;
 5535:     if (ref($what)) { $cmp1=join('',@{$what}) };
 5536:     while (my ($file_name,$value) = each(%{$current_permissions})) {
 5537:         if (defined($group)) {
 5538:             if ($file_name !~ m-^\Q$group\E/-) {
 5539:                 next;
 5540:             }
 5541:         }
 5542:         if (ref($value) eq "ARRAY"){
 5543:             foreach my $stored_what (@{$value}) {
 5544:                 my $cmp2=$stored_what;
 5545:                 if (ref($stored_what) eq 'ARRAY') {
 5546:                     $cmp2=join('',@{$stored_what});
 5547:                 }
 5548:                 if ($cmp1 eq $cmp2) {
 5549:                     push(@readonly_files, $file_name);
 5550:                     last;
 5551:                 } elsif (!defined($what)) {
 5552:                     push(@readonly_files, $file_name);
 5553:                     last;
 5554:                 }
 5555:             }
 5556:         }
 5557:     }
 5558:     return @readonly_files;
 5559: }
 5560: #-----------------------------------------------------------Get Marked as Read Only Hash
 5561: 
 5562: sub get_marked_as_readonly_hash {
 5563:     my ($current_permissions,$group,$what) = @_;
 5564:     my %readonly_files;
 5565:     while (my ($file_name,$value) = each(%{$current_permissions})) {
 5566:         if (defined($group)) {
 5567:             if ($file_name !~ m-^\Q$group\E/-) {
 5568:                 next;
 5569:             }
 5570:         }
 5571:         if (ref($value) eq "ARRAY"){
 5572:             foreach my $stored_what (@{$value}) {
 5573:                 if (ref($stored_what) eq 'ARRAY') {
 5574:                     foreach my $lock_descriptor(@{$stored_what}) {
 5575:                         if ($lock_descriptor eq 'graded') {
 5576:                             $readonly_files{$file_name} = 'graded';
 5577:                         } elsif ($lock_descriptor eq 'handback') {
 5578:                             $readonly_files{$file_name} = 'handback';
 5579:                         } else {
 5580:                             if (!exists($readonly_files{$file_name})) {
 5581:                                 $readonly_files{$file_name} = 'locked';
 5582:                             }
 5583:                         }
 5584:                     }
 5585:                 } 
 5586:             }
 5587:         } 
 5588:     }
 5589:     return %readonly_files;
 5590: }
 5591: # ------------------------------------------------------------ Unmark as Read Only
 5592: 
 5593: sub unmark_as_readonly {
 5594:     # unmarks $file_name (if $file_name is defined), or all files locked by $what 
 5595:     # for portfolio submissions, $what contains [$symb,$crsid] 
 5596:     my ($domain,$user,$what,$file_name,$group) = @_;
 5597:     $file_name = &declutter_portfile($file_name);
 5598:     my $symb_crs = $what;
 5599:     if (ref($what)) { $symb_crs=join('',@$what); }
 5600:     my %current_permissions = &dump('file_permissions',$domain,$user,$group);
 5601:     my ($tmp)=keys(%current_permissions);
 5602:     if ($tmp=~/^error:/) { undef(%current_permissions); }
 5603:     my @readonly_files = &get_marked_as_readonly($domain,$user,$what,$group);
 5604:     foreach my $file (@readonly_files) {
 5605: 	my $clean_file = &declutter_portfile($file);
 5606: 	if (defined($file_name) && ($file_name ne $clean_file)) { next; }
 5607: 	my $current_locks = $current_permissions{$file};
 5608:         my @new_locks;
 5609:         my @del_keys;
 5610:         if (ref($current_locks) eq "ARRAY"){
 5611:             foreach my $locker (@{$current_locks}) {
 5612:                 my $compare=$locker;
 5613:                 if (ref($locker) eq 'ARRAY') {
 5614:                     $compare=join('',@{$locker});
 5615:                     if ($compare ne $symb_crs) {
 5616:                         push(@new_locks, $locker);
 5617:                     }
 5618:                 }
 5619:             }
 5620:             if (scalar(@new_locks) > 0) {
 5621:                 $current_permissions{$file} = \@new_locks;
 5622:             } else {
 5623:                 push(@del_keys, $file);
 5624:                 &del('file_permissions',\@del_keys, $domain, $user);
 5625:                 delete($current_permissions{$file});
 5626:             }
 5627:         }
 5628:     }
 5629:     &put('file_permissions',\%current_permissions,$domain,$user);
 5630:     return;
 5631: }
 5632: 
 5633: # ------------------------------------------------------------ Directory lister
 5634: 
 5635: sub dirlist {
 5636:     my ($uri,$userdomain,$username,$alternateDirectoryRoot)=@_;
 5637: 
 5638:     $uri=~s/^\///;
 5639:     $uri=~s/\/$//;
 5640:     my ($udom, $uname);
 5641:     (undef,$udom,$uname)=split(/\//,$uri);
 5642:     if(defined($userdomain)) {
 5643:         $udom = $userdomain;
 5644:     }
 5645:     if(defined($username)) {
 5646:         $uname = $username;
 5647:     }
 5648: 
 5649:     my $dirRoot = $perlvar{'lonDocRoot'};
 5650:     if(defined($alternateDirectoryRoot)) {
 5651:         $dirRoot = $alternateDirectoryRoot;
 5652:         $dirRoot =~ s/\/$//;
 5653:     }
 5654: 
 5655:     if($udom) {
 5656:         if($uname) {
 5657:             my $listing = &reply('ls2:'.$dirRoot.'/'.$uri,
 5658: 				 &homeserver($uname,$udom));
 5659:             my @listing_results;
 5660:             if ($listing eq 'unknown_cmd') {
 5661:                 $listing = &reply('ls:'.$dirRoot.'/'.$uri,
 5662: 				  &homeserver($uname,$udom));
 5663:                 @listing_results = split(/:/,$listing);
 5664:             } else {
 5665:                 @listing_results = map { &unescape($_); } split(/:/,$listing);
 5666:             }
 5667:             return @listing_results;
 5668:         } elsif(!defined($alternateDirectoryRoot)) {
 5669:             my %allusers;
 5670: 	    my %servers = &get_servers($udom,'library');
 5671: 	    foreach my $tryserver (keys(%servers)) {
 5672: 		my $listing = &reply('ls2:'.$perlvar{'lonDocRoot'}.'/res/'.
 5673: 				     $udom, $tryserver);
 5674: 		my @listing_results;
 5675: 		if ($listing eq 'unknown_cmd') {
 5676: 		    $listing = &reply('ls:'.$perlvar{'lonDocRoot'}.'/res/'.
 5677: 				      $udom, $tryserver);
 5678: 		    @listing_results = split(/:/,$listing);
 5679: 		} else {
 5680: 		    @listing_results =
 5681: 			map { &unescape($_); } split(/:/,$listing);
 5682: 		}
 5683: 		if ($listing_results[0] ne 'no_such_dir' && 
 5684: 		    $listing_results[0] ne 'empty'       &&
 5685: 		    $listing_results[0] ne 'con_lost') {
 5686: 		    foreach my $line (@listing_results) {
 5687: 			my ($entry) = split(/&/,$line,2);
 5688: 			$allusers{$entry} = 1;
 5689: 		    }
 5690: 		}
 5691:             }
 5692:             my $alluserstr='';
 5693:             foreach my $user (sort(keys(%allusers))) {
 5694:                 $alluserstr.=$user.'&user:';
 5695:             }
 5696:             $alluserstr=~s/:$//;
 5697:             return split(/:/,$alluserstr);
 5698:         } else {
 5699:             return ('missing user name');
 5700:         }
 5701:     } elsif(!defined($alternateDirectoryRoot)) {
 5702:         my @all_domains = sort(&all_domains());
 5703:          foreach my $domain (@all_domains) {
 5704:              $domain = $perlvar{'lonDocRoot'}.'/res/'.$domain.'/&domain';
 5705:          }
 5706:          return @all_domains;
 5707:      } else {
 5708:         return ('missing domain');
 5709:     }
 5710: }
 5711: 
 5712: # --------------------------------------------- GetFileTimestamp
 5713: # This function utilizes dirlist and returns the date stamp for
 5714: # when it was last modified.  It will also return an error of -1
 5715: # if an error occurs
 5716: 
 5717: ##
 5718: ## FIXME: This subroutine assumes its caller knows something about the
 5719: ## directory structure of the home server for the student ($root).
 5720: ## Not a good assumption to make.  Since this is for looking up files
 5721: ## in user directories, the full path should be constructed by lond, not
 5722: ## whatever machine we request data from.
 5723: ##
 5724: sub GetFileTimestamp {
 5725:     my ($studentDomain,$studentName,$filename,$root)=@_;
 5726:     $studentDomain = &LONCAPA::clean_domain($studentDomain);
 5727:     $studentName   = &LONCAPA::clean_username($studentName);
 5728:     my $subdir=$studentName.'__';
 5729:     $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
 5730:     my $proname="$studentDomain/$subdir/$studentName";
 5731:     $proname .= '/'.$filename;
 5732:     my ($fileStat) = &Apache::lonnet::dirlist($proname, $studentDomain, 
 5733:                                               $studentName, $root);
 5734:     my @stats = split('&', $fileStat);
 5735:     if($stats[0] ne 'empty' && $stats[0] ne 'no_such_dir') {
 5736:         # @stats contains first the filename, then the stat output
 5737:         return $stats[10]; # so this is 10 instead of 9.
 5738:     } else {
 5739:         return -1;
 5740:     }
 5741: }
 5742: 
 5743: sub stat_file {
 5744:     my ($uri) = @_;
 5745:     $uri = &clutter_with_no_wrapper($uri);
 5746: 
 5747:     my ($udom,$uname,$file,$dir);
 5748:     if ($uri =~ m-^/(uploaded|editupload)/-) {
 5749: 	($udom,$uname,$file) =
 5750: 	    ($uri =~ m-/(?:uploaded|editupload)/?($match_domain)/?($match_name)/?(.*)-);
 5751: 	$file = 'userfiles/'.$file;
 5752: 	$dir = &propath($udom,$uname);
 5753:     }
 5754:     if ($uri =~ m-^/res/-) {
 5755: 	($udom,$uname) = 
 5756: 	    ($uri =~ m-/(?:res)/?($match_domain)/?($match_username)/-);
 5757: 	$file = $uri;
 5758:     }
 5759: 
 5760:     if (!$udom || !$uname || !$file) {
 5761: 	# unable to handle the uri
 5762: 	return ();
 5763:     }
 5764: 
 5765:     my ($result) = &dirlist($file,$udom,$uname,$dir);
 5766:     my @stats = split('&', $result);
 5767:     
 5768:     if($stats[0] ne 'empty' && $stats[0] ne 'no_such_dir') {
 5769: 	shift(@stats); #filename is first
 5770: 	return @stats;
 5771:     }
 5772:     return ();
 5773: }
 5774: 
 5775: # -------------------------------------------------------- Value of a Condition
 5776: 
 5777: # gets the value of a specific preevaluated condition
 5778: #    stored in the string  $env{user.state.<cid>}
 5779: # or looks up a condition reference in the bighash and if if hasn't
 5780: # already been evaluated recurses into docondval to get the value of
 5781: # the condition, then memoizing it to 
 5782: #   $env{user.state.<cid>.<condition>}
 5783: sub directcondval {
 5784:     my $number=shift;
 5785:     if (!defined($env{'user.state.'.$env{'request.course.id'}})) {
 5786: 	&Apache::lonuserstate::evalstate();
 5787:     }
 5788:     if (exists($env{'user.state.'.$env{'request.course.id'}.".$number"})) {
 5789: 	return $env{'user.state.'.$env{'request.course.id'}.".$number"};
 5790:     } elsif ($number =~ /^_/) {
 5791: 	my $sub_condition;
 5792: 	if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
 5793: 		&GDBM_READER(),0640)) {
 5794: 	    $sub_condition=$bighash{'conditions'.$number};
 5795: 	    untie(%bighash);
 5796: 	}
 5797: 	my $value = &docondval($sub_condition);
 5798: 	&appenv('user.state.'.$env{'request.course.id'}.".$number" => $value);
 5799: 	return $value;
 5800:     }
 5801:     if ($env{'user.state.'.$env{'request.course.id'}}) {
 5802:        return substr($env{'user.state.'.$env{'request.course.id'}},$number,1);
 5803:     } else {
 5804:        return 2;
 5805:     }
 5806: }
 5807: 
 5808: # get the collection of conditions for this resource
 5809: sub condval {
 5810:     my $condidx=shift;
 5811:     my $allpathcond='';
 5812:     foreach my $cond (split(/\|/,$condidx)) {
 5813: 	if (defined($env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond})) {
 5814: 	    $allpathcond.=
 5815: 		'('.$env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond}.')|';
 5816: 	}
 5817:     }
 5818:     $allpathcond=~s/\|$//;
 5819:     return &docondval($allpathcond);
 5820: }
 5821: 
 5822: #evaluates an expression of conditions
 5823: sub docondval {
 5824:     my ($allpathcond) = @_;
 5825:     my $result=0;
 5826:     if ($env{'request.course.id'}
 5827: 	&& defined($allpathcond)) {
 5828: 	my $operand='|';
 5829: 	my @stack;
 5830: 	foreach my $chunk ($allpathcond=~/(\d+|_\d+\.\d+|\(|\)|\&|\|)/g) {
 5831: 	    if ($chunk eq '(') {
 5832: 		push @stack,($operand,$result);
 5833: 	    } elsif ($chunk eq ')') {
 5834: 		my $before=pop @stack;
 5835: 		if (pop @stack eq '&') {
 5836: 		    $result=$result>$before?$before:$result;
 5837: 		} else {
 5838: 		    $result=$result>$before?$result:$before;
 5839: 		}
 5840: 	    } elsif (($chunk eq '&') || ($chunk eq '|')) {
 5841: 		$operand=$chunk;
 5842: 	    } else {
 5843: 		my $new=directcondval($chunk);
 5844: 		if ($operand eq '&') {
 5845: 		    $result=$result>$new?$new:$result;
 5846: 		} else {
 5847: 		    $result=$result>$new?$result:$new;
 5848: 		}
 5849: 	    }
 5850: 	}
 5851:     }
 5852:     return $result;
 5853: }
 5854: 
 5855: # ---------------------------------------------------- Devalidate courseresdata
 5856: 
 5857: sub devalidatecourseresdata {
 5858:     my ($coursenum,$coursedomain)=@_;
 5859:     my $hashid=$coursenum.':'.$coursedomain;
 5860:     &devalidate_cache_new('courseres',$hashid);
 5861: }
 5862: 
 5863: 
 5864: # --------------------------------------------------- Course Resourcedata Query
 5865: 
 5866: sub get_courseresdata {
 5867:     my ($coursenum,$coursedomain)=@_;
 5868:     my $coursehom=&homeserver($coursenum,$coursedomain);
 5869:     my $hashid=$coursenum.':'.$coursedomain;
 5870:     my ($result,$cached)=&is_cached_new('courseres',$hashid);
 5871:     my %dumpreply;
 5872:     unless (defined($cached)) {
 5873: 	%dumpreply=&dump('resourcedata',$coursedomain,$coursenum);
 5874: 	$result=\%dumpreply;
 5875: 	my ($tmp) = keys(%dumpreply);
 5876: 	if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
 5877: 	    &do_cache_new('courseres',$hashid,$result,600);
 5878: 	} elsif ($tmp =~ /^(con_lost|no_such_host)/) {
 5879: 	    return $tmp;
 5880: 	} elsif ($tmp =~ /^(error)/) {
 5881: 	    $result=undef;
 5882: 	    &do_cache_new('courseres',$hashid,$result,600);
 5883: 	}
 5884:     }
 5885:     return $result;
 5886: }
 5887: 
 5888: sub devalidateuserresdata {
 5889:     my ($uname,$udom)=@_;
 5890:     my $hashid="$udom:$uname";
 5891:     &devalidate_cache_new('userres',$hashid);
 5892: }
 5893: 
 5894: sub get_userresdata {
 5895:     my ($uname,$udom)=@_;
 5896:     #most student don\'t have any data set, check if there is some data
 5897:     if (&EXT_cache_status($udom,$uname)) { return undef; }
 5898: 
 5899:     my $hashid="$udom:$uname";
 5900:     my ($result,$cached)=&is_cached_new('userres',$hashid);
 5901:     if (!defined($cached)) {
 5902: 	my %resourcedata=&dump('resourcedata',$udom,$uname);
 5903: 	$result=\%resourcedata;
 5904: 	&do_cache_new('userres',$hashid,$result,600);
 5905:     }
 5906:     my ($tmp)=keys(%$result);
 5907:     if (($tmp!~/^error\:/) && ($tmp!~/^con_lost/)) {
 5908: 	return $result;
 5909:     }
 5910:     #error 2 occurs when the .db doesn't exist
 5911:     if ($tmp!~/error: 2 /) {
 5912: 	&logthis("<font color=\"blue\">WARNING:".
 5913: 		 " Trying to get resource data for ".
 5914: 		 $uname." at ".$udom.": ".
 5915: 		 $tmp."</font>");
 5916:     } elsif ($tmp=~/error: 2 /) {
 5917: 	#&EXT_cache_set($udom,$uname);
 5918: 	&do_cache_new('userres',$hashid,undef,600);
 5919: 	undef($tmp); # not really an error so don't send it back
 5920:     }
 5921:     return $tmp;
 5922: }
 5923: 
 5924: sub resdata {
 5925:     my ($name,$domain,$type,@which)=@_;
 5926:     my $result;
 5927:     if ($type eq 'course') {
 5928: 	$result=&get_courseresdata($name,$domain);
 5929:     } elsif ($type eq 'user') {
 5930: 	$result=&get_userresdata($name,$domain);
 5931:     }
 5932:     if (!ref($result)) { return $result; }    
 5933:     foreach my $item (@which) {
 5934: 	if (defined($result->{$item})) {
 5935: 	    return $result->{$item};
 5936: 	}
 5937:     }
 5938:     return undef;
 5939: }
 5940: 
 5941: #
 5942: # EXT resource caching routines
 5943: #
 5944: 
 5945: sub clear_EXT_cache_status {
 5946:     &delenv('cache.EXT.');
 5947: }
 5948: 
 5949: sub EXT_cache_status {
 5950:     my ($target_domain,$target_user) = @_;
 5951:     my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
 5952:     if (exists($env{$cachename}) && ($env{$cachename}+600) > time) {
 5953:         # We know already the user has no data
 5954:         return 1;
 5955:     } else {
 5956:         return 0;
 5957:     }
 5958: }
 5959: 
 5960: sub EXT_cache_set {
 5961:     my ($target_domain,$target_user) = @_;
 5962:     my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
 5963:     #&appenv($cachename => time);
 5964: }
 5965: 
 5966: # --------------------------------------------------------- Value of a Variable
 5967: sub EXT {
 5968: 
 5969:     my ($varname,$symbparm,$udom,$uname,$usection,$recurse)=@_;
 5970:     unless ($varname) { return ''; }
 5971:     #get real user name/domain, courseid and symb
 5972:     my $courseid;
 5973:     my $publicuser;
 5974:     if ($symbparm) {
 5975: 	$symbparm=&get_symb_from_alias($symbparm);
 5976:     }
 5977:     if (!($uname && $udom)) {
 5978:       (my $cursymb,$courseid,$udom,$uname,$publicuser)= &whichuser($symbparm);
 5979:       if (!$symbparm) {	$symbparm=$cursymb; }
 5980:     } else {
 5981: 	$courseid=$env{'request.course.id'};
 5982:     }
 5983:     my ($realm,$space,$qualifier,@therest)=split(/\./,$varname);
 5984:     my $rest;
 5985:     if (defined($therest[0])) {
 5986:        $rest=join('.',@therest);
 5987:     } else {
 5988:        $rest='';
 5989:     }
 5990: 
 5991:     my $qualifierrest=$qualifier;
 5992:     if ($rest) { $qualifierrest.='.'.$rest; }
 5993:     my $spacequalifierrest=$space;
 5994:     if ($qualifierrest) { $spacequalifierrest.='.'.$qualifierrest; }
 5995:     if ($realm eq 'user') {
 5996: # --------------------------------------------------------------- user.resource
 5997: 	if ($space eq 'resource') {
 5998: 	    if ( (defined($Apache::lonhomework::parsing_a_problem)
 5999: 		  || defined($Apache::lonhomework::parsing_a_task))
 6000: 		 &&
 6001: 		 ($symbparm eq &symbread()) ) {	
 6002: 		# if we are in the middle of processing the resource the
 6003: 		# get the value we are planning on committing
 6004:                 if (defined($Apache::lonhomework::results{$qualifierrest})) {
 6005:                     return $Apache::lonhomework::results{$qualifierrest};
 6006:                 } else {
 6007:                     return $Apache::lonhomework::history{$qualifierrest};
 6008:                 }
 6009: 	    } else {
 6010: 		my %restored;
 6011: 		if ($publicuser || $env{'request.state'} eq 'construct') {
 6012: 		    %restored=&tmprestore($symbparm,$courseid,$udom,$uname);
 6013: 		} else {
 6014: 		    %restored=&restore($symbparm,$courseid,$udom,$uname);
 6015: 		}
 6016: 		return $restored{$qualifierrest};
 6017: 	    }
 6018: # ----------------------------------------------------------------- user.access
 6019:         } elsif ($space eq 'access') {
 6020: 	    # FIXME - not supporting calls for a specific user
 6021:             return &allowed($qualifier,$rest);
 6022: # ------------------------------------------ user.preferences, user.environment
 6023:         } elsif (($space eq 'preferences') || ($space eq 'environment')) {
 6024: 	    if (($uname eq $env{'user.name'}) &&
 6025: 		($udom eq $env{'user.domain'})) {
 6026: 		return $env{join('.',('environment',$qualifierrest))};
 6027: 	    } else {
 6028: 		my %returnhash;
 6029: 		if (!$publicuser) {
 6030: 		    %returnhash=&userenvironment($udom,$uname,
 6031: 						 $qualifierrest);
 6032: 		}
 6033: 		return $returnhash{$qualifierrest};
 6034: 	    }
 6035: # ----------------------------------------------------------------- user.course
 6036:         } elsif ($space eq 'course') {
 6037: 	    # FIXME - not supporting calls for a specific user
 6038:             return $env{join('.',('request.course',$qualifier))};
 6039: # ------------------------------------------------------------------- user.role
 6040:         } elsif ($space eq 'role') {
 6041: 	    # FIXME - not supporting calls for a specific user
 6042:             my ($role,$where)=split(/\./,$env{'request.role'});
 6043:             if ($qualifier eq 'value') {
 6044: 		return $role;
 6045:             } elsif ($qualifier eq 'extent') {
 6046:                 return $where;
 6047:             }
 6048: # ----------------------------------------------------------------- user.domain
 6049:         } elsif ($space eq 'domain') {
 6050:             return $udom;
 6051: # ------------------------------------------------------------------- user.name
 6052:         } elsif ($space eq 'name') {
 6053:             return $uname;
 6054: # ---------------------------------------------------- Any other user namespace
 6055:         } else {
 6056: 	    my %reply;
 6057: 	    if (!$publicuser) {
 6058: 		%reply=&get($space,[$qualifierrest],$udom,$uname);
 6059: 	    }
 6060: 	    return $reply{$qualifierrest};
 6061:         }
 6062:     } elsif ($realm eq 'query') {
 6063: # ---------------------------------------------- pull stuff out of query string
 6064:         &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
 6065: 						[$spacequalifierrest]);
 6066: 	return $env{'form.'.$spacequalifierrest}; 
 6067:    } elsif ($realm eq 'request') {
 6068: # ------------------------------------------------------------- request.browser
 6069:         if ($space eq 'browser') {
 6070: 	    if ($qualifier eq 'textremote') {
 6071: 		if (&Apache::lonlocal::mt('textual_remote_display') eq 'on') {
 6072: 		    return 1;
 6073: 		} else {
 6074: 		    return 0;
 6075: 		}
 6076: 	    } else {
 6077: 		return $env{'browser.'.$qualifier};
 6078: 	    }
 6079: # ------------------------------------------------------------ request.filename
 6080:         } else {
 6081:             return $env{'request.'.$spacequalifierrest};
 6082:         }
 6083:     } elsif ($realm eq 'course') {
 6084: # ---------------------------------------------------------- course.description
 6085:         return $env{'course.'.$courseid.'.'.$spacequalifierrest};
 6086:     } elsif ($realm eq 'resource') {
 6087: 
 6088: 	if (defined($courseid) && $courseid eq $env{'request.course.id'}) {
 6089: 	    if (!$symbparm) { $symbparm=&symbread(); }
 6090: 	}
 6091: 
 6092: 	if ($space eq 'title') {
 6093: 	    if (!$symbparm) { $symbparm = $env{'request.filename'}; }
 6094: 	    return &gettitle($symbparm);
 6095: 	}
 6096: 	
 6097: 	if ($space eq 'map') {
 6098: 	    my ($map) = &decode_symb($symbparm);
 6099: 	    return &symbread($map);
 6100: 	}
 6101: 
 6102: 	my ($section, $group, @groups);
 6103: 	my ($courselevelm,$courselevel);
 6104: 	if ($symbparm && defined($courseid) && 
 6105: 	    $courseid eq $env{'request.course.id'}) {
 6106: 
 6107: 	    #print '<br>'.$space.' - '.$qualifier.' - '.$spacequalifierrest;
 6108: 
 6109: # ----------------------------------------------------- Cascading lookup scheme
 6110: 	    my $symbp=$symbparm;
 6111: 	    my $mapp=&deversion((&decode_symb($symbp))[0]);
 6112: 
 6113: 	    my $symbparm=$symbp.'.'.$spacequalifierrest;
 6114: 	    my $mapparm=$mapp.'___(all).'.$spacequalifierrest;
 6115: 
 6116: 	    if (($env{'user.name'} eq $uname) &&
 6117: 		($env{'user.domain'} eq $udom)) {
 6118: 		$section=$env{'request.course.sec'};
 6119:                 @groups = split(/:/,$env{'request.course.groups'});  
 6120:                 @groups=&sort_course_groups($courseid,@groups); 
 6121: 	    } else {
 6122: 		if (! defined($usection)) {
 6123: 		    $section=&getsection($udom,$uname,$courseid);
 6124: 		} else {
 6125: 		    $section = $usection;
 6126: 		}
 6127:                 @groups = &get_users_groups($udom,$uname,$courseid);
 6128: 	    }
 6129: 
 6130: 	    my $seclevel=$courseid.'.['.$section.'].'.$spacequalifierrest;
 6131: 	    my $seclevelr=$courseid.'.['.$section.'].'.$symbparm;
 6132: 	    my $seclevelm=$courseid.'.['.$section.'].'.$mapparm;
 6133: 
 6134: 	    $courselevel=$courseid.'.'.$spacequalifierrest;
 6135: 	    my $courselevelr=$courseid.'.'.$symbparm;
 6136: 	    $courselevelm=$courseid.'.'.$mapparm;
 6137: 
 6138: # ----------------------------------------------------------- first, check user
 6139: 
 6140: 	    my $userreply=&resdata($uname,$udom,'user',
 6141: 				       ($courselevelr,$courselevelm,
 6142: 					$courselevel));
 6143: 	    if (defined($userreply)) { return $userreply; }
 6144: 
 6145: # ------------------------------------------------ second, check some of course
 6146:             my $coursereply;
 6147:             if (@groups > 0) {
 6148:                 $coursereply = &check_group_parms($courseid,\@groups,$symbparm,
 6149:                                        $mapparm,$spacequalifierrest);
 6150:                 if (defined($coursereply)) { return $coursereply; }
 6151:             }
 6152: 
 6153: 	    $coursereply=&resdata($env{'course.'.$courseid.'.num'},
 6154: 				     $env{'course.'.$courseid.'.domain'},
 6155: 				     'course',
 6156: 				     ($seclevelr,$seclevelm,$seclevel,
 6157: 				      $courselevelr));
 6158: 	    if (defined($coursereply)) { return $coursereply; }
 6159: 
 6160: # ------------------------------------------------------ third, check map parms
 6161: 	    my %parmhash=();
 6162: 	    my $thisparm='';
 6163: 	    if (tie(%parmhash,'GDBM_File',
 6164: 		    $env{'request.course.fn'}.'_parms.db',
 6165: 		    &GDBM_READER(),0640)) {
 6166: 		$thisparm=$parmhash{$symbparm};
 6167: 		untie(%parmhash);
 6168: 	    }
 6169: 	    if ($thisparm) { return $thisparm; }
 6170: 	}
 6171: # ------------------------------------------ fourth, look in resource metadata
 6172: 
 6173: 	$spacequalifierrest=~s/\./\_/;
 6174: 	my $filename;
 6175: 	if (!$symbparm) { $symbparm=&symbread(); }
 6176: 	if ($symbparm) {
 6177: 	    $filename=(&decode_symb($symbparm))[2];
 6178: 	} else {
 6179: 	    $filename=$env{'request.filename'};
 6180: 	}
 6181: 	my $metadata=&metadata($filename,$spacequalifierrest);
 6182: 	if (defined($metadata)) { return $metadata; }
 6183: 	$metadata=&metadata($filename,'parameter_'.$spacequalifierrest);
 6184: 	if (defined($metadata)) { return $metadata; }
 6185: 
 6186: # ---------------------------------------------- fourth, look in rest pf course
 6187: 	if ($symbparm && defined($courseid) && 
 6188: 	    $courseid eq $env{'request.course.id'}) {
 6189: 	    my $coursereply=&resdata($env{'course.'.$courseid.'.num'},
 6190: 				     $env{'course.'.$courseid.'.domain'},
 6191: 				     'course',
 6192: 				     ($courselevelm,$courselevel));
 6193: 	    if (defined($coursereply)) { return $coursereply; }
 6194: 	}
 6195: # ------------------------------------------------------------------ Cascade up
 6196: 	unless ($space eq '0') {
 6197: 	    my @parts=split(/_/,$space);
 6198: 	    my $id=pop(@parts);
 6199: 	    my $part=join('_',@parts);
 6200: 	    if ($part eq '') { $part='0'; }
 6201: 	    my $partgeneral=&EXT('resource.'.$part.'.'.$qualifierrest,
 6202: 				 $symbparm,$udom,$uname,$section,1);
 6203: 	    if (defined($partgeneral)) { return $partgeneral; }
 6204: 	}
 6205: 	if ($recurse) { return undef; }
 6206: 	my $pack_def=&packages_tab_default($filename,$varname);
 6207: 	if (defined($pack_def)) { return $pack_def; }
 6208: 
 6209: # ---------------------------------------------------- Any other user namespace
 6210:     } elsif ($realm eq 'environment') {
 6211: # ----------------------------------------------------------------- environment
 6212: 	if (($uname eq $env{'user.name'})&&($udom eq $env{'user.domain'})) {
 6213: 	    return $env{'environment.'.$spacequalifierrest};
 6214: 	} else {
 6215: 	    if ($uname eq 'anonymous' && $udom eq '') {
 6216: 		return '';
 6217: 	    }
 6218: 	    my %returnhash=&userenvironment($udom,$uname,
 6219: 					    $spacequalifierrest);
 6220: 	    return $returnhash{$spacequalifierrest};
 6221: 	}
 6222:     } elsif ($realm eq 'system') {
 6223: # ----------------------------------------------------------------- system.time
 6224: 	if ($space eq 'time') {
 6225: 	    return time;
 6226:         }
 6227:     } elsif ($realm eq 'server') {
 6228: # ----------------------------------------------------------------- system.time
 6229: 	if ($space eq 'name') {
 6230: 	    return $ENV{'SERVER_NAME'};
 6231:         }
 6232:     }
 6233:     return '';
 6234: }
 6235: 
 6236: sub check_group_parms {
 6237:     my ($courseid,$groups,$symbparm,$mapparm,$what) = @_;
 6238:     my @groupitems = ();
 6239:     my $resultitem;
 6240:     my @levels = ($symbparm,$mapparm,$what);
 6241:     foreach my $group (@{$groups}) {
 6242:         foreach my $level (@levels) {
 6243:              my $item = $courseid.'.['.$group.'].'.$level;
 6244:              push(@groupitems,$item);
 6245:         }
 6246:     }
 6247:     my $coursereply = &resdata($env{'course.'.$courseid.'.num'},
 6248:                             $env{'course.'.$courseid.'.domain'},
 6249:                                      'course',@groupitems);
 6250:     return $coursereply;
 6251: }
 6252: 
 6253: sub sort_course_groups { # Sort groups based on defined rankings. Default is sort().
 6254:     my ($courseid,@groups) = @_;
 6255:     @groups = sort(@groups);
 6256:     return @groups;
 6257: }
 6258: 
 6259: sub packages_tab_default {
 6260:     my ($uri,$varname)=@_;
 6261:     my (undef,$part,$name)=split(/\./,$varname);
 6262: 
 6263:     my (@extension,@specifics,$do_default);
 6264:     foreach my $package (split(/,/,&metadata($uri,'packages'))) {
 6265: 	my ($pack_type,$pack_part)=split(/_/,$package,2);
 6266: 	if ($pack_type eq 'default') {
 6267: 	    $do_default=1;
 6268: 	} elsif ($pack_type eq 'extension') {
 6269: 	    push(@extension,[$package,$pack_type,$pack_part]);
 6270: 	} elsif ($pack_part eq $part) {
 6271: 	    # only look at packages defaults for packages that this id is
 6272: 	    push(@specifics,[$package,$pack_type,$pack_part]);
 6273: 	}
 6274:     }
 6275:     # first look for a package that matches the requested part id
 6276:     foreach my $package (@specifics) {
 6277: 	my (undef,$pack_type,$pack_part)=@{$package};
 6278: 	next if ($pack_part ne $part);
 6279: 	if (defined($packagetab{"$pack_type&$name&default"})) {
 6280: 	    return $packagetab{"$pack_type&$name&default"};
 6281: 	}
 6282:     }
 6283:     # look for any possible matching non extension_ package
 6284:     foreach my $package (@specifics) {
 6285: 	my (undef,$pack_type,$pack_part)=@{$package};
 6286: 	if (defined($packagetab{"$pack_type&$name&default"})) {
 6287: 	    return $packagetab{"$pack_type&$name&default"};
 6288: 	}
 6289: 	if ($pack_type eq 'part') { $pack_part='0'; }
 6290: 	if (defined($packagetab{$pack_type."_".$pack_part."&$name&default"})) {
 6291: 	    return $packagetab{$pack_type."_".$pack_part."&$name&default"};
 6292: 	}
 6293:     }
 6294:     # look for any posible extension_ match
 6295:     foreach my $package (@extension) {
 6296: 	my ($package,$pack_type)=@{$package};
 6297: 	if (defined($packagetab{"$pack_type&$name&default"})) {
 6298: 	    return $packagetab{"$pack_type&$name&default"};
 6299: 	}
 6300: 	if (defined($packagetab{$package."&$name&default"})) {
 6301: 	    return $packagetab{$package."&$name&default"};
 6302: 	}
 6303:     }
 6304:     # look for a global default setting
 6305:     if ($do_default && defined($packagetab{"default&$name&default"})) {
 6306: 	return $packagetab{"default&$name&default"};
 6307:     }
 6308:     return undef;
 6309: }
 6310: 
 6311: sub add_prefix_and_part {
 6312:     my ($prefix,$part)=@_;
 6313:     my $keyroot;
 6314:     if (defined($prefix) && $prefix !~ /^__/) {
 6315: 	# prefix that has a part already
 6316: 	$keyroot=$prefix;
 6317:     } elsif (defined($prefix)) {
 6318: 	# prefix that is missing a part
 6319: 	if (defined($part)) { $keyroot='_'.$part.substr($prefix,1); }
 6320:     } else {
 6321: 	# no prefix at all
 6322: 	if (defined($part)) { $keyroot='_'.$part; }
 6323:     }
 6324:     return $keyroot;
 6325: }
 6326: 
 6327: # ---------------------------------------------------------------- Get metadata
 6328: 
 6329: my %metaentry;
 6330: sub metadata {
 6331:     my ($uri,$what,$liburi,$prefix,$depthcount)=@_;
 6332:     $uri=&declutter($uri);
 6333:     # if it is a non metadata possible uri return quickly
 6334:     if (($uri eq '') || 
 6335: 	(($uri =~ m|^/*adm/|) && 
 6336: 	     ($uri !~ m|^adm/includes|) && ($uri !~ m|/bulletinboard$|)) ||
 6337:         ($uri =~ m|/$|) || ($uri =~ m|/.meta$|) || ($uri =~ /^~/) ||
 6338: 	($uri =~ m|home/$match_username/public_html/|)) {
 6339: 	return undef;
 6340:     }
 6341:     my $filename=$uri;
 6342:     $uri=~s/\.meta$//;
 6343: #
 6344: # Is the metadata already cached?
 6345: # Look at timestamp of caching
 6346: # Everything is cached by the main uri, libraries are never directly cached
 6347: #
 6348:     if (!defined($liburi)) {
 6349: 	my ($result,$cached)=&is_cached_new('meta',$uri);
 6350: 	if (defined($cached)) { return $result->{':'.$what}; }
 6351:     }
 6352:     {
 6353: #
 6354: # Is this a recursive call for a library?
 6355: #
 6356: #	if (! exists($metacache{$uri})) {
 6357: #	    $metacache{$uri}={};
 6358: #	}
 6359:         if ($liburi) {
 6360: 	    $liburi=&declutter($liburi);
 6361:             $filename=$liburi;
 6362:         } else {
 6363: 	    &devalidate_cache_new('meta',$uri);
 6364: 	    undef(%metaentry);
 6365: 	}
 6366:         my %metathesekeys=();
 6367:         unless ($filename=~/\.meta$/) { $filename.='.meta'; }
 6368: 	my $metastring;
 6369: 	if ($uri !~ m -^(editupload)/-) {
 6370: 	    my $file=&filelocation('',&clutter($filename));
 6371: 	    #push(@{$metaentry{$uri.'.file'}},$file);
 6372: 	    $metastring=&getfile($file);
 6373: 	}
 6374:         my $parser=HTML::LCParser->new(\$metastring);
 6375:         my $token;
 6376:         undef %metathesekeys;
 6377:         while ($token=$parser->get_token) {
 6378: 	    if ($token->[0] eq 'S') {
 6379: 		if (defined($token->[2]->{'package'})) {
 6380: #
 6381: # This is a package - get package info
 6382: #
 6383: 		    my $package=$token->[2]->{'package'};
 6384: 		    my $keyroot=&add_prefix_and_part($prefix,$token->[2]->{'part'});
 6385: 		    if (defined($token->[2]->{'id'})) { 
 6386: 			$keyroot.='_'.$token->[2]->{'id'}; 
 6387: 		    }
 6388: 		    if ($metaentry{':packages'}) {
 6389: 			$metaentry{':packages'}.=','.$package.$keyroot;
 6390: 		    } else {
 6391: 			$metaentry{':packages'}=$package.$keyroot;
 6392: 		    }
 6393: 		    foreach my $pack_entry (keys(%packagetab)) {
 6394: 			my $part=$keyroot;
 6395: 			$part=~s/^\_//;
 6396: 			if ($pack_entry=~/^\Q$package\E\&/ || 
 6397: 			    $pack_entry=~/^\Q$package\E_0\&/) {
 6398: 			    my ($pack,$name,$subp)=split(/\&/,$pack_entry);
 6399: 			    # ignore package.tab specified default values
 6400:                             # here &package_tab_default() will fetch those
 6401: 			    if ($subp eq 'default') { next; }
 6402: 			    my $value=$packagetab{$pack_entry};
 6403: 			    my $unikey;
 6404: 			    if ($pack =~ /_0$/) {
 6405: 				$unikey='parameter_0_'.$name;
 6406: 				$part=0;
 6407: 			    } else {
 6408: 				$unikey='parameter'.$keyroot.'_'.$name;
 6409: 			    }
 6410: 			    if ($subp eq 'display') {
 6411: 				$value.=' [Part: '.$part.']';
 6412: 			    }
 6413: 			    $metaentry{':'.$unikey.'.part'}=$part;
 6414: 			    $metathesekeys{$unikey}=1;
 6415: 			    unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
 6416: 				$metaentry{':'.$unikey.'.'.$subp}=$value;
 6417: 			    }
 6418: 			    if (defined($metaentry{':'.$unikey.'.default'})) {
 6419: 				$metaentry{':'.$unikey}=
 6420: 				    $metaentry{':'.$unikey.'.default'};
 6421: 			    }
 6422: 			}
 6423: 		    }
 6424: 		} else {
 6425: #
 6426: # This is not a package - some other kind of start tag
 6427: #
 6428: 		    my $entry=$token->[1];
 6429: 		    my $unikey;
 6430: 		    if ($entry eq 'import') {
 6431: 			$unikey='';
 6432: 		    } else {
 6433: 			$unikey=$entry;
 6434: 		    }
 6435: 		    $unikey.=&add_prefix_and_part($prefix,$token->[2]->{'part'});
 6436: 
 6437: 		    if (defined($token->[2]->{'id'})) { 
 6438: 			$unikey.='_'.$token->[2]->{'id'}; 
 6439: 		    }
 6440: 
 6441: 		    if ($entry eq 'import') {
 6442: #
 6443: # Importing a library here
 6444: #
 6445: 			if ($depthcount<20) {
 6446: 			    my $location=$parser->get_text('/import');
 6447: 			    my $dir=$filename;
 6448: 			    $dir=~s|[^/]*$||;
 6449: 			    $location=&filelocation($dir,$location);
 6450: 			    my $metadata = 
 6451: 				&metadata($uri,'keys', $location,$unikey,
 6452: 					  $depthcount+1);
 6453: 			    foreach my $meta (split(',',$metadata)) {
 6454: 				$metaentry{':'.$meta}=$metaentry{':'.$meta};
 6455: 				$metathesekeys{$meta}=1;
 6456: 			    }
 6457: 			}
 6458: 		    } else { 
 6459: 			
 6460: 			if (defined($token->[2]->{'name'})) { 
 6461: 			    $unikey.='_'.$token->[2]->{'name'}; 
 6462: 			}
 6463: 			$metathesekeys{$unikey}=1;
 6464: 			foreach my $param (@{$token->[3]}) {
 6465: 			    $metaentry{':'.$unikey.'.'.$param} =
 6466: 				$token->[2]->{$param};
 6467: 			}
 6468: 			my $internaltext=&HTML::Entities::decode($parser->get_text('/'.$entry));
 6469: 			my $default=$metaentry{':'.$unikey.'.default'};
 6470: 			if ( $internaltext =~ /^\s*$/ && $default !~ /^\s*$/) {
 6471: 		 # only ws inside the tag, and not in default, so use default
 6472: 		 # as value
 6473: 			    $metaentry{':'.$unikey}=$default;
 6474: 			} else {
 6475: 		  # either something interesting inside the tag or default
 6476:                   # uninteresting
 6477: 			    $metaentry{':'.$unikey}=$internaltext;
 6478: 			}
 6479: # end of not-a-package not-a-library import
 6480: 		    }
 6481: # end of not-a-package start tag
 6482: 		}
 6483: # the next is the end of "start tag"
 6484: 	    }
 6485: 	}
 6486: 	my ($extension) = ($uri =~ /\.(\w+)$/);
 6487: 	foreach my $key (keys(%packagetab)) {
 6488: 	    #no specific packages #how's our extension
 6489: 	    if ($key!~/^extension_\Q$extension\E&/) { next; }
 6490: 	    &metadata_create_package_def($uri,$key,'extension_'.$extension,
 6491: 					 \%metathesekeys);
 6492: 	}
 6493: 	if (!exists($metaentry{':packages'})) {
 6494: 	    foreach my $key (keys(%packagetab)) {
 6495: 		#no specific packages well let's get default then
 6496: 		if ($key!~/^default&/) { next; }
 6497: 		&metadata_create_package_def($uri,$key,'default',
 6498: 					     \%metathesekeys);
 6499: 	    }
 6500: 	}
 6501: # are there custom rights to evaluate
 6502: 	if ($metaentry{':copyright'} eq 'custom') {
 6503: 
 6504:     #
 6505:     # Importing a rights file here
 6506:     #
 6507: 	    unless ($depthcount) {
 6508: 		my $location=$metaentry{':customdistributionfile'};
 6509: 		my $dir=$filename;
 6510: 		$dir=~s|[^/]*$||;
 6511: 		$location=&filelocation($dir,$location);
 6512: 		my $rights_metadata =
 6513: 		    &metadata($uri,'keys',$location,'_rights',
 6514: 			      $depthcount+1);
 6515: 		foreach my $rights (split(',',$rights_metadata)) {
 6516: 		    #$metaentry{':'.$rights}=$metacache{$uri}->{':'.$rights};
 6517: 		    $metathesekeys{$rights}=1;
 6518: 		}
 6519: 	    }
 6520: 	}
 6521: 	# uniqifiy package listing
 6522: 	my %seen;
 6523: 	my @uniq_packages =
 6524: 	    grep { ! $seen{$_} ++ } (split(',',$metaentry{':packages'}));
 6525: 	$metaentry{':packages'} = join(',',@uniq_packages);
 6526: 
 6527: 	$metaentry{':keys'} = join(',',keys(%metathesekeys));
 6528: 	&metadata_generate_part0(\%metathesekeys,\%metaentry,$uri);
 6529: 	$metaentry{':allpossiblekeys'}=join(',',keys %metathesekeys);
 6530: 	&do_cache_new('meta',$uri,\%metaentry,60*60);
 6531: # this is the end of "was not already recently cached
 6532:     }
 6533:     return $metaentry{':'.$what};
 6534: }
 6535: 
 6536: sub metadata_create_package_def {
 6537:     my ($uri,$key,$package,$metathesekeys)=@_;
 6538:     my ($pack,$name,$subp)=split(/\&/,$key);
 6539:     if ($subp eq 'default') { next; }
 6540:     
 6541:     if (defined($metaentry{':packages'})) {
 6542: 	$metaentry{':packages'}.=','.$package;
 6543:     } else {
 6544: 	$metaentry{':packages'}=$package;
 6545:     }
 6546:     my $value=$packagetab{$key};
 6547:     my $unikey;
 6548:     $unikey='parameter_0_'.$name;
 6549:     $metaentry{':'.$unikey.'.part'}=0;
 6550:     $$metathesekeys{$unikey}=1;
 6551:     unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
 6552: 	$metaentry{':'.$unikey.'.'.$subp}=$value;
 6553:     }
 6554:     if (defined($metaentry{':'.$unikey.'.default'})) {
 6555: 	$metaentry{':'.$unikey}=
 6556: 	    $metaentry{':'.$unikey.'.default'};
 6557:     }
 6558: }
 6559: 
 6560: sub metadata_generate_part0 {
 6561:     my ($metadata,$metacache,$uri) = @_;
 6562:     my %allnames;
 6563:     foreach my $metakey (keys(%$metadata)) {
 6564: 	if ($metakey=~/^parameter\_(.*)/) {
 6565: 	  my $part=$$metacache{':'.$metakey.'.part'};
 6566: 	  my $name=$$metacache{':'.$metakey.'.name'};
 6567: 	  if (! exists($$metadata{'parameter_0_'.$name.'.name'})) {
 6568: 	    $allnames{$name}=$part;
 6569: 	  }
 6570: 	}
 6571:     }
 6572:     foreach my $name (keys(%allnames)) {
 6573:       $$metadata{"parameter_0_$name"}=1;
 6574:       my $key=":parameter_0_$name";
 6575:       $$metacache{"$key.part"}='0';
 6576:       $$metacache{"$key.name"}=$name;
 6577:       $$metacache{"$key.type"}=$$metacache{':parameter_'.
 6578: 					   $allnames{$name}.'_'.$name.
 6579: 					   '.type'};
 6580:       my $olddis=$$metacache{':parameter_'.$allnames{$name}.'_'.$name.
 6581: 			     '.display'};
 6582:       my $expr='[Part: '.$allnames{$name}.']';
 6583:       $olddis=~s/\Q$expr\E/\[Part: 0\]/;
 6584:       $$metacache{"$key.display"}=$olddis;
 6585:     }
 6586: }
 6587: 
 6588: # ------------------------------------------------------ Devalidate title cache
 6589: 
 6590: sub devalidate_title_cache {
 6591:     my ($url)=@_;
 6592:     if (!$env{'request.course.id'}) { return; }
 6593:     my $symb=&symbread($url);
 6594:     if (!$symb) { return; }
 6595:     my $key=$env{'request.course.id'}."\0".$symb;
 6596:     &devalidate_cache_new('title',$key);
 6597: }
 6598: 
 6599: # ------------------------------------------------- Get the title of a resource
 6600: 
 6601: sub gettitle {
 6602:     my $urlsymb=shift;
 6603:     my $symb=&symbread($urlsymb);
 6604:     if ($symb) {
 6605: 	my $key=$env{'request.course.id'}."\0".$symb;
 6606: 	my ($result,$cached)=&is_cached_new('title',$key);
 6607: 	if (defined($cached)) { 
 6608: 	    return $result;
 6609: 	}
 6610: 	my ($map,$resid,$url)=&decode_symb($symb);
 6611: 	my $title='';
 6612: 	my %bighash;
 6613: 	if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
 6614: 		&GDBM_READER(),0640)) {
 6615: 	    my $mapid=$bighash{'map_pc_'.&clutter($map)};
 6616: 	    $title=$bighash{'title_'.$mapid.'.'.$resid};
 6617: 	    untie %bighash;
 6618: 	}
 6619: 	$title=~s/\&colon\;/\:/gs;
 6620: 	if ($title) {
 6621: 	    return &do_cache_new('title',$key,$title,600);
 6622: 	}
 6623: 	$urlsymb=$url;
 6624:     }
 6625:     my $title=&metadata($urlsymb,'title');
 6626:     if (!$title) { $title=(split('/',$urlsymb))[-1]; }    
 6627:     return $title;
 6628: }
 6629: 
 6630: sub get_slot {
 6631:     my ($which,$cnum,$cdom)=@_;
 6632:     if (!$cnum || !$cdom) {
 6633: 	(undef,my $courseid)=&whichuser();
 6634: 	$cdom=$env{'course.'.$courseid.'.domain'};
 6635: 	$cnum=$env{'course.'.$courseid.'.num'};
 6636:     }
 6637:     my $key=join("\0",'slots',$cdom,$cnum,$which);
 6638:     my %slotinfo;
 6639:     if (exists($remembered{$key})) {
 6640: 	$slotinfo{$which} = $remembered{$key};
 6641:     } else {
 6642: 	%slotinfo=&get('slots',[$which],$cdom,$cnum);
 6643: 	&Apache::lonhomework::showhash(%slotinfo);
 6644: 	my ($tmp)=keys(%slotinfo);
 6645: 	if ($tmp=~/^error:/) { return (); }
 6646: 	$remembered{$key} = $slotinfo{$which};
 6647:     }
 6648:     if (ref($slotinfo{$which}) eq 'HASH') {
 6649: 	return %{$slotinfo{$which}};
 6650:     }
 6651:     return $slotinfo{$which};
 6652: }
 6653: # ------------------------------------------------- Update symbolic store links
 6654: 
 6655: sub symblist {
 6656:     my ($mapname,%newhash)=@_;
 6657:     $mapname=&deversion(&declutter($mapname));
 6658:     my %hash;
 6659:     if (($env{'request.course.fn'}) && (%newhash)) {
 6660:         if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
 6661:                       &GDBM_WRCREAT(),0640)) {
 6662: 	    foreach my $url (keys %newhash) {
 6663: 		next if ($url eq 'last_known'
 6664: 			 && $env{'form.no_update_last_known'});
 6665: 		$hash{declutter($url)}=&encode_symb($mapname,
 6666: 						    $newhash{$url}->[1],
 6667: 						    $newhash{$url}->[0]);
 6668:             }
 6669:             if (untie(%hash)) {
 6670: 		return 'ok';
 6671:             }
 6672:         }
 6673:     }
 6674:     return 'error';
 6675: }
 6676: 
 6677: # --------------------------------------------------------------- Verify a symb
 6678: 
 6679: sub symbverify {
 6680:     my ($symb,$thisurl)=@_;
 6681:     my $thisfn=$thisurl;
 6682:     $thisfn=&declutter($thisfn);
 6683: # direct jump to resource in page or to a sequence - will construct own symbs
 6684:     if ($thisfn=~/\.(page|sequence)$/) { return 1; }
 6685: # check URL part
 6686:     my ($map,$resid,$url)=&decode_symb($symb);
 6687: 
 6688:     unless ($url eq $thisfn) { return 0; }
 6689: 
 6690:     $symb=&symbclean($symb);
 6691:     $thisurl=&deversion($thisurl);
 6692:     $thisfn=&deversion($thisfn);
 6693: 
 6694:     my %bighash;
 6695:     my $okay=0;
 6696: 
 6697:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
 6698:                             &GDBM_READER(),0640)) {
 6699:         my $ids=$bighash{'ids_'.&clutter($thisurl)};
 6700:         unless ($ids) { 
 6701:            $ids=$bighash{'ids_/'.$thisurl};
 6702:         }
 6703:         if ($ids) {
 6704: # ------------------------------------------------------------------- Has ID(s)
 6705: 	    foreach my $id (split(/\,/,$ids)) {
 6706: 	       my ($mapid,$resid)=split(/\./,$id);
 6707:                if (
 6708:   &symbclean(&declutter($bighash{'map_id_'.$mapid}).'___'.$resid.'___'.$thisfn)
 6709:    eq $symb) { 
 6710: 		   if (($env{'request.role.adv'}) ||
 6711: 		       $bighash{'encrypted_'.$id} eq $env{'request.enc'}) {
 6712: 		       $okay=1; 
 6713: 		   }
 6714: 	       }
 6715: 	   }
 6716:         }
 6717: 	untie(%bighash);
 6718:     }
 6719:     return $okay;
 6720: }
 6721: 
 6722: # --------------------------------------------------------------- Clean-up symb
 6723: 
 6724: sub symbclean {
 6725:     my $symb=shift;
 6726:     if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
 6727: # remove version from map
 6728:     $symb=~s/\.(\d+)\.(\w+)\_\_\_/\.$2\_\_\_/;
 6729: 
 6730: # remove version from URL
 6731:     $symb=~s/\.(\d+)\.(\w+)$/\.$2/;
 6732: 
 6733: # remove wrapper
 6734: 
 6735:     $symb=~s/(\_\_\_\d+\_\_\_)adm\/wrapper\/(res\/)*/$1/;
 6736:     $symb=~s/(\_\_\_\d+\_\_\_)adm\/coursedocs\/showdoc\/(res\/)*/$1/;
 6737:     return $symb;
 6738: }
 6739: 
 6740: # ---------------------------------------------- Split symb to find map and url
 6741: 
 6742: sub encode_symb {
 6743:     my ($map,$resid,$url)=@_;
 6744:     return &symbclean(&declutter($map).'___'.$resid.'___'.&declutter($url));
 6745: }
 6746: 
 6747: sub decode_symb {
 6748:     my $symb=shift;
 6749:     if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
 6750:     my ($map,$resid,$url)=split(/___/,$symb);
 6751:     return (&fixversion($map),$resid,&fixversion($url));
 6752: }
 6753: 
 6754: sub fixversion {
 6755:     my $fn=shift;
 6756:     if ($fn=~/^(adm|uploaded|editupload|public)/) { return $fn; }
 6757:     my %bighash;
 6758:     my $uri=&clutter($fn);
 6759:     my $key=$env{'request.course.id'}.'_'.$uri;
 6760: # is this cached?
 6761:     my ($result,$cached)=&is_cached_new('courseresversion',$key);
 6762:     if (defined($cached)) { return $result; }
 6763: # unfortunately not cached, or expired
 6764:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
 6765: 	    &GDBM_READER(),0640)) {
 6766:  	if ($bighash{'version_'.$uri}) {
 6767:  	    my $version=$bighash{'version_'.$uri};
 6768:  	    unless (($version eq 'mostrecent') || 
 6769: 		    ($version==&getversion($uri))) {
 6770:  		$uri=~s/\.(\w+)$/\.$version\.$1/;
 6771:  	    }
 6772:  	}
 6773:  	untie %bighash;
 6774:     }
 6775:     return &do_cache_new('courseresversion',$key,&declutter($uri),600);
 6776: }
 6777: 
 6778: sub deversion {
 6779:     my $url=shift;
 6780:     $url=~s/\.\d+\.(\w+)$/\.$1/;
 6781:     return $url;
 6782: }
 6783: 
 6784: # ------------------------------------------------------ Return symb list entry
 6785: 
 6786: sub symbread {
 6787:     my ($thisfn,$donotrecurse)=@_;
 6788:     my $cache_str='request.symbread.cached.'.$thisfn;
 6789:     if (defined($env{$cache_str})) { return $env{$cache_str}; }
 6790: # no filename provided? try from environment
 6791:     unless ($thisfn) {
 6792:         if ($env{'request.symb'}) {
 6793: 	    return $env{$cache_str}=&symbclean($env{'request.symb'});
 6794: 	}
 6795: 	$thisfn=$env{'request.filename'};
 6796:     }
 6797:     if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
 6798: # is that filename actually a symb? Verify, clean, and return
 6799:     if ($thisfn=~/\_\_\_\d+\_\_\_(.*)$/) {
 6800: 	if (&symbverify($thisfn,$1)) {
 6801: 	    return $env{$cache_str}=&symbclean($thisfn);
 6802: 	}
 6803:     }
 6804:     $thisfn=declutter($thisfn);
 6805:     my %hash;
 6806:     my %bighash;
 6807:     my $syval='';
 6808:     if (($env{'request.course.fn'}) && ($thisfn)) {
 6809:         my $targetfn = $thisfn;
 6810:         if ( ($thisfn =~ m/^(uploaded|editupload)\//) && ($thisfn !~ m/\.(page|sequence)$/) ) {
 6811:             $targetfn = 'adm/wrapper/'.$thisfn;
 6812:         }
 6813: 	if ($targetfn =~ m|^adm/wrapper/(ext/.*)|) {
 6814: 	    $targetfn=$1;
 6815: 	}
 6816:         if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
 6817:                       &GDBM_READER(),0640)) {
 6818: 	    $syval=$hash{$targetfn};
 6819:             untie(%hash);
 6820:         }
 6821: # ---------------------------------------------------------- There was an entry
 6822:         if ($syval) {
 6823: 	    #unless ($syval=~/\_\d+$/) {
 6824: 		#unless ($env{'form.request.prefix'}=~/\.(\d+)\_$/) {
 6825: 		    #&appenv('request.ambiguous' => $thisfn);
 6826: 		    #return $env{$cache_str}='';
 6827: 		#}    
 6828: 		#$syval.=$1;
 6829: 	    #}
 6830:         } else {
 6831: # ------------------------------------------------------- Was not in symb table
 6832:            if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
 6833:                             &GDBM_READER(),0640)) {
 6834: # ---------------------------------------------- Get ID(s) for current resource
 6835:               my $ids=$bighash{'ids_'.&clutter($thisfn)};
 6836:               unless ($ids) { 
 6837:                  $ids=$bighash{'ids_/'.$thisfn};
 6838:               }
 6839:               unless ($ids) {
 6840: # alias?
 6841: 		  $ids=$bighash{'mapalias_'.$thisfn};
 6842:               }
 6843:               if ($ids) {
 6844: # ------------------------------------------------------------------- Has ID(s)
 6845:                  my @possibilities=split(/\,/,$ids);
 6846:                  if ($#possibilities==0) {
 6847: # ----------------------------------------------- There is only one possibility
 6848: 		     my ($mapid,$resid)=split(/\./,$ids);
 6849: 		     $syval=&encode_symb($bighash{'map_id_'.$mapid},
 6850: 						    $resid,$thisfn);
 6851:                  } elsif (!$donotrecurse) {
 6852: # ------------------------------------------ There is more than one possibility
 6853:                      my $realpossible=0;
 6854:                      foreach my $id (@possibilities) {
 6855: 			 my $file=$bighash{'src_'.$id};
 6856:                          if (&allowed('bre',$file)) {
 6857:          		    my ($mapid,$resid)=split(/\./,$id);
 6858:                             if ($bighash{'map_type_'.$mapid} ne 'page') {
 6859: 				$realpossible++;
 6860:                                 $syval=&encode_symb($bighash{'map_id_'.$mapid},
 6861: 						    $resid,$thisfn);
 6862:                             }
 6863: 			 }
 6864:                      }
 6865: 		     if ($realpossible!=1) { $syval=''; }
 6866:                  } else {
 6867:                      $syval='';
 6868:                  }
 6869: 	      }
 6870:               untie(%bighash)
 6871:            }
 6872:         }
 6873:         if ($syval) {
 6874: 	    return $env{$cache_str}=$syval;
 6875:         }
 6876:     }
 6877:     &appenv('request.ambiguous' => $thisfn);
 6878:     return $env{$cache_str}='';
 6879: }
 6880: 
 6881: # ---------------------------------------------------------- Return random seed
 6882: 
 6883: sub numval {
 6884:     my $txt=shift;
 6885:     $txt=~tr/A-J/0-9/;
 6886:     $txt=~tr/a-j/0-9/;
 6887:     $txt=~tr/K-T/0-9/;
 6888:     $txt=~tr/k-t/0-9/;
 6889:     $txt=~tr/U-Z/0-5/;
 6890:     $txt=~tr/u-z/0-5/;
 6891:     $txt=~s/\D//g;
 6892:     if ($_64bit) { if ($txt > 2**32) { return -1; } }
 6893:     return int($txt);
 6894: }
 6895: 
 6896: sub numval2 {
 6897:     my $txt=shift;
 6898:     $txt=~tr/A-J/0-9/;
 6899:     $txt=~tr/a-j/0-9/;
 6900:     $txt=~tr/K-T/0-9/;
 6901:     $txt=~tr/k-t/0-9/;
 6902:     $txt=~tr/U-Z/0-5/;
 6903:     $txt=~tr/u-z/0-5/;
 6904:     $txt=~s/\D//g;
 6905:     my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
 6906:     my $total;
 6907:     foreach my $val (@txts) { $total+=$val; }
 6908:     if ($_64bit) { if ($total > 2**32) { return -1; } }
 6909:     return int($total);
 6910: }
 6911: 
 6912: sub numval3 {
 6913:     use integer;
 6914:     my $txt=shift;
 6915:     $txt=~tr/A-J/0-9/;
 6916:     $txt=~tr/a-j/0-9/;
 6917:     $txt=~tr/K-T/0-9/;
 6918:     $txt=~tr/k-t/0-9/;
 6919:     $txt=~tr/U-Z/0-5/;
 6920:     $txt=~tr/u-z/0-5/;
 6921:     $txt=~s/\D//g;
 6922:     my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
 6923:     my $total;
 6924:     foreach my $val (@txts) { $total+=$val; }
 6925:     if ($_64bit) { $total=(($total<<32)>>32); }
 6926:     return $total;
 6927: }
 6928: 
 6929: sub digest {
 6930:     my ($data)=@_;
 6931:     my $digest=&Digest::MD5::md5($data);
 6932:     my ($a,$b,$c,$d)=unpack("iiii",$digest);
 6933:     my ($e,$f);
 6934:     {
 6935:         use integer;
 6936:         $e=($a+$b);
 6937:         $f=($c+$d);
 6938:         if ($_64bit) {
 6939:             $e=(($e<<32)>>32);
 6940:             $f=(($f<<32)>>32);
 6941:         }
 6942:     }
 6943:     if (wantarray) {
 6944: 	return ($e,$f);
 6945:     } else {
 6946: 	my $g;
 6947: 	{
 6948: 	    use integer;
 6949: 	    $g=($e+$f);
 6950: 	    if ($_64bit) {
 6951: 		$g=(($g<<32)>>32);
 6952: 	    }
 6953: 	}
 6954: 	return $g;
 6955:     }
 6956: }
 6957: 
 6958: sub latest_rnd_algorithm_id {
 6959:     return '64bit5';
 6960: }
 6961: 
 6962: sub get_rand_alg {
 6963:     my ($courseid)=@_;
 6964:     if (!$courseid) { $courseid=(&whichuser())[1]; }
 6965:     if ($courseid) {
 6966: 	return $env{"course.$courseid.rndseed"};
 6967:     }
 6968:     return &latest_rnd_algorithm_id();
 6969: }
 6970: 
 6971: sub validCODE {
 6972:     my ($CODE)=@_;
 6973:     if (defined($CODE) && $CODE ne '' && $CODE =~ /^\w+$/) { return 1; }
 6974:     return 0;
 6975: }
 6976: 
 6977: sub getCODE {
 6978:     if (&validCODE($env{'form.CODE'})) { return $env{'form.CODE'}; }
 6979:     if ( (defined($Apache::lonhomework::parsing_a_problem) ||
 6980: 	  defined($Apache::lonhomework::parsing_a_task) ) &&
 6981: 	 &validCODE($Apache::lonhomework::history{'resource.CODE'})) {
 6982: 	return $Apache::lonhomework::history{'resource.CODE'};
 6983:     }
 6984:     return undef;
 6985: }
 6986: 
 6987: sub rndseed {
 6988:     my ($symb,$courseid,$domain,$username)=@_;
 6989:     my ($wsymb,$wcourseid,$wdomain,$wusername)=&whichuser();
 6990:     if (!$symb) {
 6991: 	unless ($symb=$wsymb) { return time; }
 6992:     }
 6993:     if (!$courseid) { $courseid=$wcourseid; }
 6994:     if (!$domain) { $domain=$wdomain; }
 6995:     if (!$username) { $username=$wusername }
 6996:     my $which=&get_rand_alg();
 6997: 
 6998:     if (defined(&getCODE())) {
 6999: 	if ($which eq '64bit5') {
 7000: 	    return &rndseed_CODE_64bit5($symb,$courseid,$domain,$username);
 7001: 	} elsif ($which eq '64bit4') {
 7002: 	    return &rndseed_CODE_64bit4($symb,$courseid,$domain,$username);
 7003: 	} else {
 7004: 	    return &rndseed_CODE_64bit($symb,$courseid,$domain,$username);
 7005: 	}
 7006:     } elsif ($which eq '64bit5') {
 7007: 	return &rndseed_64bit5($symb,$courseid,$domain,$username);
 7008:     } elsif ($which eq '64bit4') {
 7009: 	return &rndseed_64bit4($symb,$courseid,$domain,$username);
 7010:     } elsif ($which eq '64bit3') {
 7011: 	return &rndseed_64bit3($symb,$courseid,$domain,$username);
 7012:     } elsif ($which eq '64bit2') {
 7013: 	return &rndseed_64bit2($symb,$courseid,$domain,$username);
 7014:     } elsif ($which eq '64bit') {
 7015: 	return &rndseed_64bit($symb,$courseid,$domain,$username);
 7016:     }
 7017:     return &rndseed_32bit($symb,$courseid,$domain,$username);
 7018: }
 7019: 
 7020: sub rndseed_32bit {
 7021:     my ($symb,$courseid,$domain,$username)=@_;
 7022:     {
 7023: 	use integer;
 7024: 	my $symbchck=unpack("%32C*",$symb) << 27;
 7025: 	my $symbseed=numval($symb) << 22;
 7026: 	my $namechck=unpack("%32C*",$username) << 17;
 7027: 	my $nameseed=numval($username) << 12;
 7028: 	my $domainseed=unpack("%32C*",$domain) << 7;
 7029: 	my $courseseed=unpack("%32C*",$courseid);
 7030: 	my $num=$symbseed+$nameseed+$domainseed+$courseseed+$namechck+$symbchck;
 7031: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
 7032: 	#&logthis("rndseed :$num:$symb");
 7033: 	if ($_64bit) { $num=(($num<<32)>>32); }
 7034: 	return $num;
 7035:     }
 7036: }
 7037: 
 7038: sub rndseed_64bit {
 7039:     my ($symb,$courseid,$domain,$username)=@_;
 7040:     {
 7041: 	use integer;
 7042: 	my $symbchck=unpack("%32S*",$symb) << 21;
 7043: 	my $symbseed=numval($symb) << 10;
 7044: 	my $namechck=unpack("%32S*",$username);
 7045: 	
 7046: 	my $nameseed=numval($username) << 21;
 7047: 	my $domainseed=unpack("%32S*",$domain) << 10;
 7048: 	my $courseseed=unpack("%32S*",$courseid);
 7049: 	
 7050: 	my $num1=$symbchck+$symbseed+$namechck;
 7051: 	my $num2=$nameseed+$domainseed+$courseseed;
 7052: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
 7053: 	#&logthis("rndseed :$num:$symb");
 7054: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
 7055: 	return "$num1,$num2";
 7056:     }
 7057: }
 7058: 
 7059: sub rndseed_64bit2 {
 7060:     my ($symb,$courseid,$domain,$username)=@_;
 7061:     {
 7062: 	use integer;
 7063: 	# strings need to be an even # of cahracters long, it it is odd the
 7064:         # last characters gets thrown away
 7065: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
 7066: 	my $symbseed=numval($symb) << 10;
 7067: 	my $namechck=unpack("%32S*",$username.' ');
 7068: 	
 7069: 	my $nameseed=numval($username) << 21;
 7070: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
 7071: 	my $courseseed=unpack("%32S*",$courseid.' ');
 7072: 	
 7073: 	my $num1=$symbchck+$symbseed+$namechck;
 7074: 	my $num2=$nameseed+$domainseed+$courseseed;
 7075: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
 7076: 	#&logthis("rndseed :$num:$symb");
 7077: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
 7078: 	return "$num1,$num2";
 7079:     }
 7080: }
 7081: 
 7082: sub rndseed_64bit3 {
 7083:     my ($symb,$courseid,$domain,$username)=@_;
 7084:     {
 7085: 	use integer;
 7086: 	# strings need to be an even # of cahracters long, it it is odd the
 7087:         # last characters gets thrown away
 7088: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
 7089: 	my $symbseed=numval2($symb) << 10;
 7090: 	my $namechck=unpack("%32S*",$username.' ');
 7091: 	
 7092: 	my $nameseed=numval2($username) << 21;
 7093: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
 7094: 	my $courseseed=unpack("%32S*",$courseid.' ');
 7095: 	
 7096: 	my $num1=$symbchck+$symbseed+$namechck;
 7097: 	my $num2=$nameseed+$domainseed+$courseseed;
 7098: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
 7099: 	#&logthis("rndseed :$num1:$num2:$_64bit");
 7100: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
 7101: 	
 7102: 	return "$num1:$num2";
 7103:     }
 7104: }
 7105: 
 7106: sub rndseed_64bit4 {
 7107:     my ($symb,$courseid,$domain,$username)=@_;
 7108:     {
 7109: 	use integer;
 7110: 	# strings need to be an even # of cahracters long, it it is odd the
 7111:         # last characters gets thrown away
 7112: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
 7113: 	my $symbseed=numval3($symb) << 10;
 7114: 	my $namechck=unpack("%32S*",$username.' ');
 7115: 	
 7116: 	my $nameseed=numval3($username) << 21;
 7117: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
 7118: 	my $courseseed=unpack("%32S*",$courseid.' ');
 7119: 	
 7120: 	my $num1=$symbchck+$symbseed+$namechck;
 7121: 	my $num2=$nameseed+$domainseed+$courseseed;
 7122: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
 7123: 	#&logthis("rndseed :$num1:$num2:$_64bit");
 7124: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
 7125: 	
 7126: 	return "$num1:$num2";
 7127:     }
 7128: }
 7129: 
 7130: sub rndseed_64bit5 {
 7131:     my ($symb,$courseid,$domain,$username)=@_;
 7132:     my ($num1,$num2)=&digest("$symb,$courseid,$domain,$username");
 7133:     return "$num1:$num2";
 7134: }
 7135: 
 7136: sub rndseed_CODE_64bit {
 7137:     my ($symb,$courseid,$domain,$username)=@_;
 7138:     {
 7139: 	use integer;
 7140: 	my $symbchck=unpack("%32S*",$symb.' ') << 16;
 7141: 	my $symbseed=numval2($symb);
 7142: 	my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
 7143: 	my $CODEseed=numval(&getCODE());
 7144: 	my $courseseed=unpack("%32S*",$courseid.' ');
 7145: 	my $num1=$symbseed+$CODEchck;
 7146: 	my $num2=$CODEseed+$courseseed+$symbchck;
 7147: 	#&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
 7148: 	#&logthis("rndseed :$num1:$num2:$symb");
 7149: 	if ($_64bit) { $num1=(($num1<<32)>>32); }
 7150: 	if ($_64bit) { $num2=(($num2<<32)>>32); }
 7151: 	return "$num1:$num2";
 7152:     }
 7153: }
 7154: 
 7155: sub rndseed_CODE_64bit4 {
 7156:     my ($symb,$courseid,$domain,$username)=@_;
 7157:     {
 7158: 	use integer;
 7159: 	my $symbchck=unpack("%32S*",$symb.' ') << 16;
 7160: 	my $symbseed=numval3($symb);
 7161: 	my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
 7162: 	my $CODEseed=numval3(&getCODE());
 7163: 	my $courseseed=unpack("%32S*",$courseid.' ');
 7164: 	my $num1=$symbseed+$CODEchck;
 7165: 	my $num2=$CODEseed+$courseseed+$symbchck;
 7166: 	#&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
 7167: 	#&logthis("rndseed :$num1:$num2:$symb");
 7168: 	if ($_64bit) { $num1=(($num1<<32)>>32); }
 7169: 	if ($_64bit) { $num2=(($num2<<32)>>32); }
 7170: 	return "$num1:$num2";
 7171:     }
 7172: }
 7173: 
 7174: sub rndseed_CODE_64bit5 {
 7175:     my ($symb,$courseid,$domain,$username)=@_;
 7176:     my $code = &getCODE();
 7177:     my ($num1,$num2)=&digest("$symb,$courseid,$code");
 7178:     return "$num1:$num2";
 7179: }
 7180: 
 7181: sub setup_random_from_rndseed {
 7182:     my ($rndseed)=@_;
 7183:     if ($rndseed =~/([,:])/) {
 7184: 	my ($num1,$num2)=split(/[,:]/,$rndseed);
 7185: 	&Math::Random::random_set_seed(abs($num1),abs($num2));
 7186:     } else {
 7187: 	&Math::Random::random_set_seed_from_phrase($rndseed);
 7188:     }
 7189: }
 7190: 
 7191: sub latest_receipt_algorithm_id {
 7192:     return 'receipt3';
 7193: }
 7194: 
 7195: sub recunique {
 7196:     my $fucourseid=shift;
 7197:     my $unique;
 7198:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2' ||
 7199: 	$env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
 7200: 	$unique=$env{"course.$fucourseid.internal.encseed"};
 7201:     } else {
 7202: 	$unique=$perlvar{'lonReceipt'};
 7203:     }
 7204:     return unpack("%32C*",$unique);
 7205: }
 7206: 
 7207: sub recprefix {
 7208:     my $fucourseid=shift;
 7209:     my $prefix;
 7210:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2'||
 7211: 	$env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
 7212: 	$prefix=$env{"course.$fucourseid.internal.encpref"};
 7213:     } else {
 7214: 	$prefix=$perlvar{'lonHostID'};
 7215:     }
 7216:     return unpack("%32C*",$prefix);
 7217: }
 7218: 
 7219: sub ireceipt {
 7220:     my ($funame,$fudom,$fucourseid,$fusymb,$part)=@_;
 7221: 
 7222:     my $return =&recprefix($fucourseid).'-';
 7223: 
 7224:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt3' ||
 7225: 	$env{'request.state'} eq 'construct') {
 7226: 	$return .= (&digest("$funame,$fudom,$fucourseid,$fusymb,$part")%10000);
 7227: 	return $return;
 7228:     }
 7229: 
 7230:     my $cuname=unpack("%32C*",$funame);
 7231:     my $cudom=unpack("%32C*",$fudom);
 7232:     my $cucourseid=unpack("%32C*",$fucourseid);
 7233:     my $cusymb=unpack("%32C*",$fusymb);
 7234:     my $cunique=&recunique($fucourseid);
 7235:     my $cpart=unpack("%32S*",$part);
 7236:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2') {
 7237: 
 7238: 	#&logthis("doing receipt2  using parts $cpart, uname $cuname and udom $cudom gets  ".($cpart%$cuname)." and ".($cpart%$cudom));
 7239: 			       
 7240: 	$return.= ($cunique%$cuname+
 7241: 		   $cunique%$cudom+
 7242: 		   $cusymb%$cuname+
 7243: 		   $cusymb%$cudom+
 7244: 		   $cucourseid%$cuname+
 7245: 		   $cucourseid%$cudom+
 7246: 		   $cpart%$cuname+
 7247: 		   $cpart%$cudom);
 7248:     } else {
 7249: 	$return.= ($cunique%$cuname+
 7250: 		   $cunique%$cudom+
 7251: 		   $cusymb%$cuname+
 7252: 		   $cusymb%$cudom+
 7253: 		   $cucourseid%$cuname+
 7254: 		   $cucourseid%$cudom);
 7255:     }
 7256:     return $return;
 7257: }
 7258: 
 7259: sub receipt {
 7260:     my ($part)=@_;
 7261:     my ($symb,$courseid,$domain,$name) = &whichuser();
 7262:     return &ireceipt($name,$domain,$courseid,$symb,$part);
 7263: }
 7264: 
 7265: sub whichuser {
 7266:     my ($passedsymb)=@_;
 7267:     my ($symb,$courseid,$domain,$name,$publicuser);
 7268:     if (defined($env{'form.grade_symb'})) {
 7269: 	my ($tmp_courseid)=&get_env_multiple('form.grade_courseid');
 7270: 	my $allowed=&allowed('vgr',$tmp_courseid);
 7271: 	if (!$allowed &&
 7272: 	    exists($env{'request.course.sec'}) &&
 7273: 	    $env{'request.course.sec'} !~ /^\s*$/) {
 7274: 	    $allowed=&allowed('vgr',$tmp_courseid.
 7275: 			      '/'.$env{'request.course.sec'});
 7276: 	}
 7277: 	if ($allowed) {
 7278: 	    ($symb)=&get_env_multiple('form.grade_symb');
 7279: 	    $courseid=$tmp_courseid;
 7280: 	    ($domain)=&get_env_multiple('form.grade_domain');
 7281: 	    ($name)=&get_env_multiple('form.grade_username');
 7282: 	    return ($symb,$courseid,$domain,$name,$publicuser);
 7283: 	}
 7284:     }
 7285:     if (!$passedsymb) {
 7286: 	$symb=&symbread();
 7287:     } else {
 7288: 	$symb=$passedsymb;
 7289:     }
 7290:     $courseid=$env{'request.course.id'};
 7291:     $domain=$env{'user.domain'};
 7292:     $name=$env{'user.name'};
 7293:     if ($name eq 'public' && $domain eq 'public') {
 7294: 	if (!defined($env{'form.username'})) {
 7295: 	    $env{'form.username'}.=time.rand(10000000);
 7296: 	}
 7297: 	$name.=$env{'form.username'};
 7298:     }
 7299:     return ($symb,$courseid,$domain,$name,$publicuser);
 7300: 
 7301: }
 7302: 
 7303: # ------------------------------------------------------------ Serves up a file
 7304: # returns either the contents of the file or 
 7305: # -1 if the file doesn't exist
 7306: #
 7307: # if the target is a file that was uploaded via DOCS, 
 7308: # a check will be made to see if a current copy exists on the local server,
 7309: # if it does this will be served, otherwise a copy will be retrieved from
 7310: # the home server for the course and stored in /home/httpd/html/userfiles on
 7311: # the local server.   
 7312: 
 7313: sub getfile {
 7314:     my ($file) = @_;
 7315:     if ($file =~ m -^/*(uploaded|editupload)/-) { $file=&filelocation("",$file); }
 7316:     &repcopy($file);
 7317:     return &readfile($file);
 7318: }
 7319: 
 7320: sub repcopy_userfile {
 7321:     my ($file)=@_;
 7322:     if ($file =~ m -^/*(uploaded|editupload)/-) { $file=&filelocation("",$file); }
 7323:     if ($file =~ m|^/home/httpd/html/lonUsers/|) { return 'ok'; }
 7324:     my ($cdom,$cnum,$filename) = 
 7325: 	($file=~m|^\Q$perlvar{'lonDocRoot'}\E/+userfiles/+($match_domain)/+($match_name)/+(.*)|);
 7326:     my $uri="/uploaded/$cdom/$cnum/$filename";
 7327:     if (-e "$file") {
 7328: # we already have a local copy, check it out
 7329: 	my @fileinfo = stat($file);
 7330: 	my $rtncode;
 7331: 	my $info;
 7332: 	my $lwpresp = &getuploaded('HEAD',$uri,$cdom,$cnum,\$info,\$rtncode);
 7333: 	if ($lwpresp ne 'ok') {
 7334: # there is no such file anymore, even though we had a local copy
 7335: 	    if ($rtncode eq '404') {
 7336: 		unlink($file);
 7337: 	    }
 7338: 	    return -1;
 7339: 	}
 7340: 	if ($info < $fileinfo[9]) {
 7341: # nice, the file we have is up-to-date, just say okay
 7342: 	    return 'ok';
 7343: 	} else {
 7344: # the file is outdated, get rid of it
 7345: 	    unlink($file);
 7346: 	}
 7347:     }
 7348: # one way or the other, at this point, we don't have the file
 7349: # construct the correct path for the file
 7350:     my @parts = ($cdom,$cnum); 
 7351:     if ($filename =~ m|^(.+)/[^/]+$|) {
 7352: 	push @parts, split(/\//,$1);
 7353:     }
 7354:     my $path = $perlvar{'lonDocRoot'}.'/userfiles';
 7355:     foreach my $part (@parts) {
 7356: 	$path .= '/'.$part;
 7357: 	if (!-e $path) {
 7358: 	    mkdir($path,0770);
 7359: 	}
 7360:     }
 7361: # now the path exists for sure
 7362: # get a user agent
 7363:     my $ua=new LWP::UserAgent;
 7364:     my $transferfile=$file.'.in.transfer';
 7365: # FIXME: this should flock
 7366:     if (-e $transferfile) { return 'ok'; }
 7367:     my $request;
 7368:     $uri=~s/^\///;
 7369:     $request=new HTTP::Request('GET','http://'.&hostname(&homeserver($cnum,$cdom)).'/raw/'.$uri);
 7370:     my $response=$ua->request($request,$transferfile);
 7371: # did it work?
 7372:     if ($response->is_error()) {
 7373: 	unlink($transferfile);
 7374: 	&logthis("Userfile repcopy failed for $uri");
 7375: 	return -1;
 7376:     }
 7377: # worked, rename the transfer file
 7378:     rename($transferfile,$file);
 7379:     return 'ok';
 7380: }
 7381: 
 7382: sub tokenwrapper {
 7383:     my $uri=shift;
 7384:     $uri=~s|^http\://([^/]+)||;
 7385:     $uri=~s|^/||;
 7386:     $env{'user.environment'}=~/\/([^\/]+)\.id/;
 7387:     my $token=$1;
 7388:     my (undef,$udom,$uname,$file)=split('/',$uri,4);
 7389:     if ($udom && $uname && $file) {
 7390: 	$file=~s|(\?\.*)*$||;
 7391:         &appenv("userfile.$udom/$uname/$file" => $env{'request.course.id'});
 7392:         return 'http://'.&hostname(&homeserver($uname,$udom)).'/'.$uri.
 7393:                (($uri=~/\?/)?'&':'?').'token='.$token.
 7394:                                '&tokenissued='.$perlvar{'lonHostID'};
 7395:     } else {
 7396:         return '/adm/notfound.html';
 7397:     }
 7398: }
 7399: 
 7400: # call with reqtype HEAD: get last modification time
 7401: # call with reqtype GET: get the file contents
 7402: # Do not call this with reqtype GET for large files! It loads everything into memory
 7403: #
 7404: sub getuploaded {
 7405:     my ($reqtype,$uri,$cdom,$cnum,$info,$rtncode) = @_;
 7406:     $uri=~s/^\///;
 7407:     $uri = 'http://'.&hostname(&homeserver($cnum,$cdom)).'/raw/'.$uri;
 7408:     my $ua=new LWP::UserAgent;
 7409:     my $request=new HTTP::Request($reqtype,$uri);
 7410:     my $response=$ua->request($request);
 7411:     $$rtncode = $response->code;
 7412:     if (! $response->is_success()) {
 7413: 	return 'failed';
 7414:     }      
 7415:     if ($reqtype eq 'HEAD') {
 7416: 	$$info = &HTTP::Date::str2time( $response->header('Last-modified') );
 7417:     } elsif ($reqtype eq 'GET') {
 7418: 	$$info = $response->content;
 7419:     }
 7420:     return 'ok';
 7421: }
 7422: 
 7423: sub readfile {
 7424:     my $file = shift;
 7425:     if ( (! -e $file ) || ($file eq '') ) { return -1; };
 7426:     my $fh;
 7427:     open($fh,"<$file");
 7428:     my $a='';
 7429:     while (my $line = <$fh>) { $a .= $line; }
 7430:     return $a;
 7431: }
 7432: 
 7433: sub filelocation {
 7434:     my ($dir,$file) = @_;
 7435:     my $location;
 7436:     $file=~ s/^\s*(\S+)\s*$/$1/; ## strip off leading and trailing spaces
 7437: 
 7438:     if ($file =~ m-^/adm/-) {
 7439: 	$file=~s-^/adm/wrapper/-/-;
 7440: 	$file=~s-^/adm/coursedocs/showdoc/-/-;
 7441:     }
 7442:     if ($file=~m:^/~:) { # is a contruction space reference
 7443:         $location = $file;
 7444:         $location =~ s:/~(.*?)/(.*):/home/$1/public_html/$2:;
 7445:     } elsif ($file=~m{^/home/$match_username/public_html/}) {
 7446: 	# is a correct contruction space reference
 7447:         $location = $file;
 7448:     } elsif ($file=~/^\/*(uploaded|editupload)/) { # is an uploaded file
 7449:         my ($udom,$uname,$filename)=
 7450:   	    ($file=~m -^/+(?:uploaded|editupload)/+($match_domain)/+($match_name)/+(.*)$-);
 7451:         my $home=&homeserver($uname,$udom);
 7452:         my $is_me=0;
 7453:         my @ids=&current_machine_ids();
 7454:         foreach my $id (@ids) { if ($id eq $home) { $is_me=1; } }
 7455:         if ($is_me) {
 7456:   	    $location=&propath($udom,$uname).
 7457:   	      '/userfiles/'.$filename;
 7458:         } else {
 7459:   	  $location=$Apache::lonnet::perlvar{'lonDocRoot'}.'/userfiles/'.
 7460:   	      $udom.'/'.$uname.'/'.$filename;
 7461:         }
 7462:     } else {
 7463:         $file=~s/^\Q$perlvar{'lonDocRoot'}\E//;
 7464:         $file=~s:^/res/:/:;
 7465:         if ( !( $file =~ m:^/:) ) {
 7466:             $location = $dir. '/'.$file;
 7467:         } else {
 7468:             $location = '/home/httpd/html/res'.$file;
 7469:         }
 7470:     }
 7471:     $location=~s://+:/:g; # remove duplicate /
 7472:     while ($location=~m:/\.\./:) {$location=~ s:/[^/]+/\.\./:/:g;} #remove dir/..
 7473:     while ($location=~m:/\./:) {$location=~ s:/\./:/:g;} #remove /./
 7474:     return $location;
 7475: }
 7476: 
 7477: sub hreflocation {
 7478:     my ($dir,$file)=@_;
 7479:     unless (($file=~m-^http://-i) || ($file=~m-^/-)) {
 7480: 	$file=filelocation($dir,$file);
 7481:     } elsif ($file=~m-^/adm/-) {
 7482: 	$file=~s-^/adm/wrapper/-/-;
 7483: 	$file=~s-^/adm/coursedocs/showdoc/-/-;
 7484:     }
 7485:     if ($file=~m-^\Q$perlvar{'lonDocRoot'}\E-) {
 7486: 	$file=~s-^\Q$perlvar{'lonDocRoot'}\E--;
 7487:     } elsif ($file=~m-/home/($match_username)/public_html/-) {
 7488: 	$file=~s-^/home/($match_username)/public_html/-/~$1/-;
 7489:     } elsif ($file=~m-^\Q$perlvar{'lonUsersDir'}\E-) {
 7490: 	$file=~s-^/home/httpd/lonUsers/($match_domain)/./././($match_name)/userfiles/
 7491: 	    -/uploaded/$1/$2/-x;
 7492:     }
 7493:     return $file;
 7494: }
 7495: 
 7496: sub current_machine_domains {
 7497:     return &machine_domains(&hostname($perlvar{'lonHostID'}));
 7498: }
 7499: 
 7500: sub machine_domains {
 7501:     my ($hostname) = @_;
 7502:     my @domains;
 7503:     my %hostname = &all_hostnames();
 7504:     while( my($id, $name) = each(%hostname)) {
 7505: #	&logthis("-$id-$name-$hostname-");
 7506: 	if ($hostname eq $name) {
 7507: 	    push(@domains,&host_domain($id));
 7508: 	}
 7509:     }
 7510:     return @domains;
 7511: }
 7512: 
 7513: sub current_machine_ids {
 7514:     return &machine_ids(&hostname($perlvar{'lonHostID'}));
 7515: }
 7516: 
 7517: sub machine_ids {
 7518:     my ($hostname) = @_;
 7519:     $hostname ||= &hostname($perlvar{'lonHostID'});
 7520:     my @ids;
 7521:     my %hostname = &all_hostnames();
 7522:     while( my($id, $name) = each(%hostname)) {
 7523: #	&logthis("-$id-$name-$hostname-");
 7524: 	if ($hostname eq $name) {
 7525: 	    push(@ids,$id);
 7526: 	}
 7527:     }
 7528:     return @ids;
 7529: }
 7530: 
 7531: sub additional_machine_domains {
 7532:     my @domains;
 7533:     open(my $fh,"<$perlvar{'lonTabDir'}/expected_domains.tab");
 7534:     while( my $line = <$fh>) {
 7535:         $line =~ s/\s//g;
 7536:         push(@domains,$line);
 7537:     }
 7538:     return @domains;
 7539: }
 7540: 
 7541: sub default_login_domain {
 7542:     my $domain = $perlvar{'lonDefDomain'};
 7543:     my $testdomain=(split(/\./,$ENV{'HTTP_HOST'}))[0];
 7544:     foreach my $posdom (&current_machine_domains(),
 7545:                         &additional_machine_domains()) {
 7546:         if (lc($posdom) eq lc($testdomain)) {
 7547:             $domain=$posdom;
 7548:             last;
 7549:         }
 7550:     }
 7551:     return $domain;
 7552: }
 7553: 
 7554: # ------------------------------------------------------------- Declutters URLs
 7555: 
 7556: sub declutter {
 7557:     my $thisfn=shift;
 7558:     if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
 7559:     $thisfn=~s/^\Q$perlvar{'lonDocRoot'}\E//;
 7560:     $thisfn=~s/^\///;
 7561:     $thisfn=~s|^adm/wrapper/||;
 7562:     $thisfn=~s|^adm/coursedocs/showdoc/||;
 7563:     $thisfn=~s/^res\///;
 7564:     $thisfn=~s/\?.+$//;
 7565:     return $thisfn;
 7566: }
 7567: 
 7568: # ------------------------------------------------------------- Clutter up URLs
 7569: 
 7570: sub clutter {
 7571:     my $thisfn='/'.&declutter(shift);
 7572:     unless ($thisfn=~/^\/(uploaded|editupload|adm|userfiles|ext|raw|priv|public)\//) { 
 7573:        $thisfn='/res'.$thisfn; 
 7574:     }
 7575:     if ($thisfn !~m|/adm|) {
 7576: 	if ($thisfn =~ m|/ext/|) {
 7577: 	    $thisfn='/adm/wrapper'.$thisfn;
 7578: 	} else {
 7579: 	    my ($ext) = ($thisfn =~ /\.(\w+)$/);
 7580: 	    my $embstyle=&Apache::loncommon::fileembstyle($ext);
 7581: 	    if ($embstyle eq 'ssi'
 7582: 		|| ($embstyle eq 'hdn')
 7583: 		|| ($embstyle eq 'rat')
 7584: 		|| ($embstyle eq 'prv')
 7585: 		|| ($embstyle eq 'ign')) {
 7586: 		#do nothing with these
 7587: 	    } elsif (($embstyle eq 'img') 
 7588: 		|| ($embstyle eq 'emb')
 7589: 		|| ($embstyle eq 'wrp')) {
 7590: 		$thisfn='/adm/wrapper'.$thisfn;
 7591: 	    } elsif ($embstyle eq 'unk'
 7592: 		     && $thisfn!~/\.(sequence|page)$/) {
 7593: 		$thisfn='/adm/coursedocs/showdoc'.$thisfn;
 7594: 	    } else {
 7595: #		&logthis("Got a blank emb style");
 7596: 	    }
 7597: 	}
 7598:     }
 7599:     return $thisfn;
 7600: }
 7601: 
 7602: sub clutter_with_no_wrapper {
 7603:     my $uri = &clutter(shift);
 7604:     if ($uri =~ m-^/adm/-) {
 7605: 	$uri =~ s-^/adm/wrapper/-/-;
 7606: 	$uri =~ s-^/adm/coursedocs/showdoc/-/-;
 7607:     }
 7608:     return $uri;
 7609: }
 7610: 
 7611: sub freeze_escape {
 7612:     my ($value)=@_;
 7613:     if (ref($value)) {
 7614: 	$value=&nfreeze($value);
 7615: 	return '__FROZEN__'.&escape($value);
 7616:     }
 7617:     return &escape($value);
 7618: }
 7619: 
 7620: 
 7621: sub thaw_unescape {
 7622:     my ($value)=@_;
 7623:     if ($value =~ /^__FROZEN__/) {
 7624: 	substr($value,0,10,undef);
 7625: 	$value=&unescape($value);
 7626: 	return &thaw($value);
 7627:     }
 7628:     return &unescape($value);
 7629: }
 7630: 
 7631: sub correct_line_ends {
 7632:     my ($result)=@_;
 7633:     $$result =~s/\r\n/\n/mg;
 7634:     $$result =~s/\r/\n/mg;
 7635: }
 7636: # ================================================================ Main Program
 7637: 
 7638: sub goodbye {
 7639:    &logthis("Starting Shut down");
 7640: #not converted to using infrastruture and probably shouldn't be
 7641:    &logthis(sprintf("%-20s is %s",'%badServerCache',length(&freeze(\%badServerCache))));
 7642: #converted
 7643: #   &logthis(sprintf("%-20s is %s",'%metacache',scalar(%metacache)));
 7644:    &logthis(sprintf("%-20s is %s",'%homecache',length(&freeze(\%homecache))));
 7645: #   &logthis(sprintf("%-20s is %s",'%titlecache',length(&freeze(\%titlecache))));
 7646: #   &logthis(sprintf("%-20s is %s",'%courseresdatacache',length(&freeze(\%courseresdatacache))));
 7647: #1.1 only
 7648: #   &logthis(sprintf("%-20s is %s",'%userresdatacache',length(&freeze(\%userresdatacache))));
 7649: #   &logthis(sprintf("%-20s is %s",'%getsectioncache',length(&freeze(\%getsectioncache))));
 7650: #   &logthis(sprintf("%-20s is %s",'%courseresversioncache',length(&freeze(\%courseresversioncache))));
 7651: #   &logthis(sprintf("%-20s is %s",'%resversioncache',length(&freeze(\%resversioncache))));
 7652:    &logthis(sprintf("%-20s is %s",'%remembered',length(&freeze(\%remembered))));
 7653:    &logthis(sprintf("%-20s is %s",'kicks',$kicks));
 7654:    &logthis(sprintf("%-20s is %s",'hits',$hits));
 7655:    &flushcourselogs();
 7656:    &logthis("Shutting down");
 7657: }
 7658: 
 7659: sub get_dns {
 7660:     my ($url,$func) = @_;
 7661:     open(my $config,"<$perlvar{'lonTabDir'}/hosts.tab");
 7662:     foreach my $dns (<$config>) {
 7663: 	next if ($dns !~ /^\^(\S*)/x);
 7664: 	$dns = $1;
 7665: 	my $ua=new LWP::UserAgent;
 7666: 	my $request=new HTTP::Request('GET',"http://$dns$url");
 7667: 	my $response=$ua->request($request);
 7668: 	next if ($response->is_error());
 7669: 	my @content = split("\n",$response->content);
 7670: 	&$func(\@content);
 7671:     }
 7672:     close($config);
 7673: }
 7674: # ------------------------------------------------------------ Read domain file
 7675: {
 7676:     my $loaded;
 7677:     my %domain;
 7678: 
 7679:     sub parse_domain_tab {
 7680: 	my ($lines) = @_;
 7681: 	foreach my $line (@$lines) {
 7682: 	    next if ($line =~ /^(\#|\s*$ )/x);
 7683: 
 7684: 	    chomp($line);
 7685: 	    my ($name,@elements) = split(/:/,$line,9);
 7686: 	    my %this_domain;
 7687: 	    foreach my $field ('description', 'auth_def', 'auth_arg_def',
 7688: 			       'lang_def', 'city', 'longi', 'lati',
 7689: 			       'primary') {
 7690: 		$this_domain{$field} = shift(@elements);
 7691: 	    }
 7692: 	    $domain{$name} = \%this_domain;
 7693: 	}
 7694:     }
 7695: 
 7696:     sub reset_domain_info {
 7697: 	undef($loaded);
 7698: 	undef(%domain);
 7699:     }
 7700: 
 7701:     sub load_domain_tab {
 7702: 	&get_dns('/adm/dns/domain',\&parse_domain_tab);
 7703: 	my $fh;
 7704: 	if (open($fh,"<".$perlvar{'lonTabDir'}.'/domain.tab')) {
 7705: 	    my @lines = <$fh>;
 7706: 	    &parse_domain_tab(\@lines);
 7707: 	}
 7708: 	close($fh);
 7709: 	$loaded = 1;
 7710:     }
 7711: 
 7712:     sub domain {
 7713: 	&load_domain_tab() if (!$loaded);
 7714: 
 7715: 	my ($name,$what) = @_;
 7716: 	return if ( !exists($domain{$name}) );
 7717: 
 7718: 	if (!$what) {
 7719: 	    return $domain{$name}{'description'};
 7720: 	}
 7721: 	return $domain{$name}{$what};
 7722:     }
 7723: }
 7724: 
 7725: 
 7726: # ------------------------------------------------------------- Read hosts file
 7727: {
 7728:     my %hostname;
 7729:     my %hostdom;
 7730:     my %libserv;
 7731:     my $loaded;
 7732: 
 7733:     sub parse_hosts_tab {
 7734: 	my ($file) = @_;
 7735: 	foreach my $configline (@$file) {
 7736: 	    next if ($configline =~ /^(\#|\s*$ )/x);
 7737: 	    next if ($configline =~ /^\^/);
 7738: 	    chomp($configline);
 7739: 	    my ($id,$domain,$role,$name)=split(/:/,$configline);
 7740: 	    $name=~s/\s//g;
 7741: 	    if ($id && $domain && $role && $name) {
 7742: 		$hostname{$id}=$name;
 7743: 		$hostdom{$id}=$domain;
 7744: 		if ($role eq 'library') { $libserv{$id}=$name; }
 7745: 	    }
 7746: 	}
 7747:     }
 7748:     
 7749:     sub reset_hosts_info {
 7750: 	&reset_domain_info();
 7751: 	&reset_hosts_ip_info();
 7752: 	undef(%hostname);
 7753: 	undef(%hostdom);
 7754: 	undef(%libserv);
 7755: 	undef($loaded);
 7756:     }
 7757: 
 7758:     sub load_hosts_tab {
 7759: 	&get_dns('/adm/dns/hosts',\&parse_hosts_tab);
 7760: 	open(my $config,"<$perlvar{'lonTabDir'}/hosts.tab");
 7761: 	my @config = <$config>;
 7762: 	&parse_hosts_tab(\@config);
 7763: 	close($config);
 7764: 	$loaded=1;
 7765:     }
 7766: 
 7767:     sub hostname {
 7768: 	&load_hosts_tab() if (!$loaded);
 7769: 
 7770: 	my ($lonid) = @_;
 7771: 	return $hostname{$lonid};
 7772:     }
 7773: 
 7774:     sub all_hostnames {
 7775: 	&load_hosts_tab() if (!$loaded);
 7776: 
 7777: 	return %hostname;
 7778:     }
 7779: 
 7780:     sub is_library {
 7781: 	&load_hosts_tab() if (!$loaded);
 7782: 
 7783: 	return exists($libserv{$_[0]});
 7784:     }
 7785: 
 7786:     sub all_library {
 7787: 	&load_hosts_tab() if (!$loaded);
 7788: 
 7789: 	return %libserv;
 7790:     }
 7791: 
 7792:     sub get_servers {
 7793: 	&load_hosts_tab() if (!$loaded);
 7794: 
 7795: 	my ($domain,$type) = @_;
 7796: 	my %possible_hosts = ($type eq 'library') ? %libserv
 7797: 	                                          : %hostname;
 7798: 	my %result;
 7799: 	if (ref($domain) eq 'ARRAY') {
 7800: 	    while ( my ($host,$hostname) = each(%possible_hosts)) {
 7801: 		if (grep(/^\Q$hostdom{$host}\E$/,@$domain)) {
 7802: 		    $result{$host} = $hostname;
 7803: 		}
 7804: 	    }
 7805: 	} else {
 7806: 	    while ( my ($host,$hostname) = each(%possible_hosts)) {
 7807: 		if ($hostdom{$host} eq $domain) {
 7808: 		    $result{$host} = $hostname;
 7809: 		}
 7810: 	    }
 7811: 	}
 7812: 	return %result;
 7813:     }
 7814: 
 7815:     sub host_domain {
 7816: 	&load_hosts_tab() if (!$loaded);
 7817: 
 7818: 	my ($lonid) = @_;
 7819: 	return $hostdom{$lonid};
 7820:     }
 7821: 
 7822:     sub all_domains {
 7823: 	&load_hosts_tab() if (!$loaded);
 7824: 
 7825: 	my %seen;
 7826: 	my @uniq = grep(!$seen{$_}++, values(%hostdom));
 7827: 	return @uniq;
 7828:     }
 7829: }
 7830: 
 7831: { 
 7832:     my %iphost;
 7833:     my %name_to_ip;
 7834:     my %lonid_to_ip;
 7835:     sub get_hosts_from_ip {
 7836: 	my ($ip) = @_;
 7837: 	my %iphosts = &get_iphost();
 7838: 	if (ref($iphosts{$ip})) {
 7839: 	    return @{$iphosts{$ip}};
 7840: 	}
 7841: 	return;
 7842:     }
 7843:     
 7844:     sub reset_hosts_ip_info {
 7845: 	undef(%iphost);
 7846: 	undef(%name_to_ip);
 7847: 	undef(%lonid_to_ip);
 7848:     }
 7849: 
 7850:     sub get_host_ip {
 7851: 	my ($lonid) = @_;
 7852: 	if (exists($lonid_to_ip{$lonid})) {
 7853: 	    return $lonid_to_ip{$lonid};
 7854: 	}
 7855: 	my $name=&hostname($lonid);
 7856:    	my $ip = gethostbyname($name);
 7857: 	return if (!$ip || length($ip) ne 4);
 7858: 	$ip=inet_ntoa($ip);
 7859: 	$name_to_ip{$name}   = $ip;
 7860: 	$lonid_to_ip{$lonid} = $ip;
 7861: 	return $ip;
 7862:     }
 7863:     
 7864:     sub get_iphost {
 7865: 	if (%iphost) { return %iphost; }
 7866: 	my %hostname = &all_hostnames();
 7867: 	foreach my $id (keys(%hostname)) {
 7868: 	    my $name=&hostname($id);
 7869: 	    my $ip;
 7870: 	    if (!exists($name_to_ip{$name})) {
 7871: 		$ip = gethostbyname($name);
 7872: 		if (!$ip || length($ip) ne 4) {
 7873: 		    &logthis("Skipping host $id name $name no IP found");
 7874: 		    next;
 7875: 		}
 7876: 		$ip=inet_ntoa($ip);
 7877: 		$name_to_ip{$name} = $ip;
 7878: 	    } else {
 7879: 		$ip = $name_to_ip{$name};
 7880: 	    }
 7881: 	    $lonid_to_ip{$id} = $ip;
 7882: 	    push(@{$iphost{$ip}},$id);
 7883: 	}
 7884: 	return %iphost;
 7885:     }
 7886: }
 7887: 
 7888: BEGIN {
 7889: 
 7890: # ----------------------------------- Read loncapa.conf and loncapa_apache.conf
 7891:     unless ($readit) {
 7892: {
 7893:     my $configvars = LONCAPA::Configuration::read_conf('loncapa.conf');
 7894:     %perlvar = (%perlvar,%{$configvars});
 7895: }
 7896: 
 7897: 
 7898: # ------------------------------------------------------ Read spare server file
 7899: {
 7900:     open(my $config,"<$perlvar{'lonTabDir'}/spare.tab");
 7901: 
 7902:     while (my $configline=<$config>) {
 7903:        chomp($configline);
 7904:        if ($configline) {
 7905: 	   my ($host,$type) = split(':',$configline,2);
 7906: 	   if (!defined($type) || $type eq '') { $type = 'default' };
 7907: 	   push(@{ $spareid{$type} }, $host);
 7908:        }
 7909:     }
 7910:     close($config);
 7911: }
 7912: # ------------------------------------------------------------ Read permissions
 7913: {
 7914:     open(my $config,"<$perlvar{'lonTabDir'}/roles.tab");
 7915: 
 7916:     while (my $configline=<$config>) {
 7917: 	chomp($configline);
 7918: 	if ($configline) {
 7919: 	    my ($role,$perm)=split(/ /,$configline);
 7920: 	    if ($perm ne '') { $pr{$role}=$perm; }
 7921: 	}
 7922:     }
 7923:     close($config);
 7924: }
 7925: 
 7926: # -------------------------------------------- Read plain texts for permissions
 7927: {
 7928:     open(my $config,"<$perlvar{'lonTabDir'}/rolesplain.tab");
 7929: 
 7930:     while (my $configline=<$config>) {
 7931: 	chomp($configline);
 7932: 	if ($configline) {
 7933: 	    my ($short,@plain)=split(/:/,$configline);
 7934:             %{$prp{$short}} = ();
 7935: 	    if (@plain > 0) {
 7936:                 $prp{$short}{'std'} = $plain[0];
 7937:                 for (my $i=1; $i<@plain; $i++) {
 7938:                     $prp{$short}{'alt'.$i} = $plain[$i];  
 7939:                 }
 7940:             }
 7941: 	}
 7942:     }
 7943:     close($config);
 7944: }
 7945: 
 7946: # ---------------------------------------------------------- Read package table
 7947: {
 7948:     open(my $config,"<$perlvar{'lonTabDir'}/packages.tab");
 7949: 
 7950:     while (my $configline=<$config>) {
 7951: 	if ($configline !~ /\S/ || $configline=~/^#/) { next; }
 7952: 	chomp($configline);
 7953: 	my ($short,$plain)=split(/:/,$configline);
 7954: 	my ($pack,$name)=split(/\&/,$short);
 7955: 	if ($plain ne '') {
 7956: 	    $packagetab{$pack.'&'.$name.'&name'}=$name; 
 7957: 	    $packagetab{$short}=$plain; 
 7958: 	}
 7959:     }
 7960:     close($config);
 7961: }
 7962: 
 7963: # ------------- set up temporary directory
 7964: {
 7965:     $tmpdir = $perlvar{'lonDaemons'}.'/tmp/';
 7966: 
 7967: }
 7968: 
 7969: $memcache=new Cache::Memcached({'servers'           => ['127.0.0.1:11211'],
 7970: 				'compress_threshold'=> 20_000,
 7971:  			        });
 7972: 
 7973: $processmarker='_'.time.'_'.$perlvar{'lonHostID'};
 7974: $dumpcount=0;
 7975: 
 7976: &logtouch();
 7977: &logthis('<font color="yellow">INFO: Read configuration</font>');
 7978: $readit=1;
 7979:     {
 7980: 	use integer;
 7981: 	my $test=(2**32)+1;
 7982: 	if ($test != 0) { $_64bit=1; } else { $_64bit=0; }
 7983: 	&logthis(" Detected 64bit platform ($_64bit)");
 7984:     }
 7985: }
 7986: }
 7987: 
 7988: 1;
 7989: __END__
 7990: 
 7991: =pod
 7992: 
 7993: =head1 NAME
 7994: 
 7995: Apache::lonnet - Subroutines to ask questions about things in the network.
 7996: 
 7997: =head1 SYNOPSIS
 7998: 
 7999: Invoked by other LON-CAPA modules, when they need to talk to or about objects in the network.
 8000: 
 8001:  &Apache::lonnet::SUBROUTINENAME(ARGUMENTS);
 8002: 
 8003: Common parameters:
 8004: 
 8005: =over 4
 8006: 
 8007: =item *
 8008: 
 8009: $uname : an internal username (if $cname expecting a course Id specifically)
 8010: 
 8011: =item *
 8012: 
 8013: $udom : a domain (if $cdom expecting a course's domain specifically)
 8014: 
 8015: =item *
 8016: 
 8017: $symb : a resource instance identifier
 8018: 
 8019: =item *
 8020: 
 8021: $namespace : the name of a .db file that contains the data needed or
 8022: being set.
 8023: 
 8024: =back
 8025: 
 8026: =head1 OVERVIEW
 8027: 
 8028: lonnet provides subroutines which interact with the
 8029: lonc/lond (TCP) network layer of LON-CAPA. They can be used to ask
 8030: about classes, users, and resources.
 8031: 
 8032: For many of these objects you can also use this to store data about
 8033: them or modify them in various ways.
 8034: 
 8035: =head2 Symbs
 8036: 
 8037: To identify a specific instance of a resource, LON-CAPA uses symbols
 8038: or "symbs"X<symb>. These identifiers are built from the URL of the
 8039: map, the resource number of the resource in the map, and the URL of
 8040: the resource itself. The latter is somewhat redundant, but might help
 8041: if maps change.
 8042: 
 8043: An example is
 8044: 
 8045:  msu/korte/parts/part1.sequence___19___msu/korte/tests/part12.problem
 8046: 
 8047: The respective map entry is
 8048: 
 8049:  <resource id="19" src="/res/msu/korte/tests/part12.problem"
 8050:   title="Problem 2">
 8051:  </resource>
 8052: 
 8053: Symbs are used by the random number generator, as well as to store and
 8054: restore data specific to a certain instance of for example a problem.
 8055: 
 8056: =head2 Storing And Retrieving Data
 8057: 
 8058: X<store()>X<cstore()>X<restore()>Three of the most important functions
 8059: in C<lonnet.pm> are C<&Apache::lonnet::cstore()>,
 8060: C<&Apache::lonnet:restore()>, and C<&Apache::lonnet::store()>, which
 8061: is is the non-critical message twin of cstore. These functions are for
 8062: handlers to store a perl hash to a user's permanent data space in an
 8063: easy manner, and to retrieve it again on another call. It is expected
 8064: that a handler would use this once at the beginning to retrieve data,
 8065: and then again once at the end to send only the new data back.
 8066: 
 8067: The data is stored in the user's data directory on the user's
 8068: homeserver under the ID of the course.
 8069: 
 8070: The hash that is returned by restore will have all of the previous
 8071: value for all of the elements of the hash.
 8072: 
 8073: Example:
 8074: 
 8075:  #creating a hash
 8076:  my %hash;
 8077:  $hash{'foo'}='bar';
 8078: 
 8079:  #storing it
 8080:  &Apache::lonnet::cstore(\%hash);
 8081: 
 8082:  #changing a value
 8083:  $hash{'foo'}='notbar';
 8084: 
 8085:  #adding a new value
 8086:  $hash{'bar'}='foo';
 8087:  &Apache::lonnet::cstore(\%hash);
 8088: 
 8089:  #retrieving the hash
 8090:  my %history=&Apache::lonnet::restore();
 8091: 
 8092:  #print the hash
 8093:  foreach my $key (sort(keys(%history))) {
 8094:    print("\%history{$key} = $history{$key}");
 8095:  }
 8096: 
 8097: Will print out:
 8098: 
 8099:  %history{1:foo} = bar
 8100:  %history{1:keys} = foo:timestamp
 8101:  %history{1:timestamp} = 990455579
 8102:  %history{2:bar} = foo
 8103:  %history{2:foo} = notbar
 8104:  %history{2:keys} = foo:bar:timestamp
 8105:  %history{2:timestamp} = 990455580
 8106:  %history{bar} = foo
 8107:  %history{foo} = notbar
 8108:  %history{timestamp} = 990455580
 8109:  %history{version} = 2
 8110: 
 8111: Note that the special hash entries C<keys>, C<version> and
 8112: C<timestamp> were added to the hash. C<version> will be equal to the
 8113: total number of versions of the data that have been stored. The
 8114: C<timestamp> attribute will be the UNIX time the hash was
 8115: stored. C<keys> is available in every historical section to list which
 8116: keys were added or changed at a specific historical revision of a
 8117: hash.
 8118: 
 8119: B<Warning>: do not store the hash that restore returns directly. This
 8120: will cause a mess since it will restore the historical keys as if the
 8121: were new keys. I.E. 1:foo will become 1:1:foo etc.
 8122: 
 8123: Calling convention:
 8124: 
 8125:  my %record=&Apache::lonnet::restore($symb,$courseid,$domain,$uname,$home);
 8126:  &Apache::lonnet::cstore(\%newrecord,$symb,$courseid,$domain,$uname,$home);
 8127: 
 8128: For more detailed information, see lonnet specific documentation.
 8129: 
 8130: =head1 RETURN MESSAGES
 8131: 
 8132: =over 4
 8133: 
 8134: =item * B<con_lost>: unable to contact remote host
 8135: 
 8136: =item * B<con_delayed>: unable to contact remote host, message will be delivered
 8137: when the connection is brought back up
 8138: 
 8139: =item * B<con_failed>: unable to contact remote host and unable to save message
 8140: for later delivery
 8141: 
 8142: =item * B<error:>: an error a occured, a description of the error follows the :
 8143: 
 8144: =item * B<no_such_host>: unable to fund a host associated with the user/domain
 8145: that was requested
 8146: 
 8147: =back
 8148: 
 8149: =head1 PUBLIC SUBROUTINES
 8150: 
 8151: =head2 Session Environment Functions
 8152: 
 8153: =over 4
 8154: 
 8155: =item * 
 8156: X<appenv()>
 8157: B<appenv(%hash)>: the value of %hash is written to
 8158: the user envirnoment file, and will be restored for each access this
 8159: user makes during this session, also modifies the %env for the current
 8160: process
 8161: 
 8162: =item *
 8163: X<delenv()>
 8164: B<delenv($regexp)>: removes all items from the session
 8165: environment file that matches the regular expression in $regexp. The
 8166: values are also delted from the current processes %env.
 8167: 
 8168: =item * get_env_multiple($name) 
 8169: 
 8170: gets $name from the %env hash, it seemlessly handles the cases where multiple
 8171: values may be defined and end up as an array ref.
 8172: 
 8173: returns an array of values
 8174: 
 8175: =back
 8176: 
 8177: =head2 User Information
 8178: 
 8179: =over 4
 8180: 
 8181: =item *
 8182: X<queryauthenticate()>
 8183: B<queryauthenticate($uname,$udom)>: try to determine user's current 
 8184: authentication scheme
 8185: 
 8186: =item *
 8187: X<authenticate()>
 8188: B<authenticate($uname,$upass,$udom)>: try to
 8189: authenticate user from domain's lib servers (first use the current
 8190: one). C<$upass> should be the users password.
 8191: 
 8192: =item *
 8193: X<homeserver()>
 8194: B<homeserver($uname,$udom)>: find the server which has
 8195: the user's directory and files (there must be only one), this caches
 8196: the answer, and also caches if there is a borken connection.
 8197: 
 8198: =item *
 8199: X<idget()>
 8200: B<idget($udom,@ids)>: find the usernames behind a list of IDs
 8201: (IDs are a unique resource in a domain, there must be only 1 ID per
 8202: username, and only 1 username per ID in a specific domain) (returns
 8203: hash: id=>name,id=>name)
 8204: 
 8205: =item *
 8206: X<idrget()>
 8207: B<idrget($udom,@unames)>: find the IDs behind a list of
 8208: usernames (returns hash: name=>id,name=>id)
 8209: 
 8210: =item *
 8211: X<idput()>
 8212: B<idput($udom,%ids)>: store away a list of names and associated IDs
 8213: 
 8214: =item *
 8215: X<rolesinit()>
 8216: B<rolesinit($udom,$username,$authhost)>: get user privileges
 8217: 
 8218: =item *
 8219: X<getsection()>
 8220: B<getsection($udom,$uname,$cname)>: finds the section of student in the
 8221: course $cname, return section name/number or '' for "not in course"
 8222: and '-1' for "no section"
 8223: 
 8224: =item *
 8225: X<userenvironment()>
 8226: B<userenvironment($udom,$uname,@what)>: gets the values of the keys
 8227: passed in @what from the requested user's environment, returns a hash
 8228: 
 8229: =item * 
 8230: X<userlog_query()>
 8231: B<userlog_query($uname,$udom,%filters)>: retrieves data from a user's
 8232: activity.log file. %filters defines filters applied when parsing the
 8233: log file. These can be start or end timestamps, or the type of action
 8234: - log to look for Login or Logout events, check for Checkin or
 8235: Checkout, role for role selection. The response is in the form
 8236: timestamp1:hostid1:event1&timestamp2:hostid2:event2 where events are
 8237: escaped strings of the action recorded in the activity.log file.
 8238: 
 8239: =back
 8240: 
 8241: =head2 User Roles
 8242: 
 8243: =over 4
 8244: 
 8245: =item *
 8246: 
 8247: allowed($priv,$uri,$symb,$role) : check for a user privilege; returns codes for allowed actions
 8248:  F: full access
 8249:  U,I,K: authentication modes (cxx only)
 8250:  '': forbidden
 8251:  1: user needs to choose course
 8252:  2: browse allowed
 8253:  A: passphrase authentication needed
 8254: 
 8255: =item *
 8256: 
 8257: definerole($rolename,$sysrole,$domrole,$courole) : define role; define a custom
 8258: role rolename set privileges in format of lonTabs/roles.tab for system, domain,
 8259: and course level
 8260: 
 8261: =item *
 8262: 
 8263: plaintext($short) : return value in %prp hash (rolesplain.tab); plain text
 8264: explanation of a user role term
 8265: 
 8266: =item *
 8267: 
 8268: get_my_roles($uname,$udom,$context,$types,$roles,$roledoms) :
 8269: All arguments are optional. Returns a hash of a roles, either for
 8270: co-author/assistant author roles for a user's Construction Space
 8271: (default), or if $context is 'user', roles for the user himself,
 8272: In the hash, keys are set to colon-sparated $uname,$udom,and $role,
 8273: and value is set to colon-separated start and end times for the role.
 8274: If no username and domain are specified, will default to current
 8275: user/domain. Types, roles, and roledoms are references to arrays,
 8276: of role statuses (active, future or previous), roles 
 8277: (e.g., cc,in, st etc.) and domains of the roles which can be used
 8278: to restrict the list of roles reported. If no array ref is 
 8279: provided for types, will default to return only active roles.
 8280: 
 8281: =back
 8282: 
 8283: =head2 User Modification
 8284: 
 8285: =over 4
 8286: 
 8287: =item *
 8288: 
 8289: assignrole($udom,$uname,$url,$role,$end,$start) : assign role; give a role to a
 8290: user for the level given by URL.  Optional start and end dates (leave empty
 8291: string or zero for "no date")
 8292: 
 8293: =item *
 8294: 
 8295: changepass($uname,$udom,$currentpass,$newpass,$server) : attempts to
 8296: change a users, password, possible return values are: ok,
 8297: pwchange_failure, non_authorized, auth_mode_error, unknown_user,
 8298: refused
 8299: 
 8300: =item *
 8301: 
 8302: modifyuserauth($udom,$uname,$umode,$upass) : modify user authentication
 8303: 
 8304: =item *
 8305: 
 8306: modifyuser($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene) : 
 8307: modify user
 8308: 
 8309: =item *
 8310: 
 8311: modifystudent
 8312: 
 8313: modify a students enrollment and identification information.
 8314: The course id is resolved based on the current users environment.  
 8315: This means the envoking user must be a course coordinator or otherwise
 8316: associated with a course.
 8317: 
 8318: This call is essentially a wrapper for lonnet::modifyuser and
 8319: lonnet::modify_student_enrollment
 8320: 
 8321: Inputs: 
 8322: 
 8323: =over 4
 8324: 
 8325: =item B<$udom> Students loncapa domain
 8326: 
 8327: =item B<$uname> Students loncapa login name
 8328: 
 8329: =item B<$uid> Students id/student number
 8330: 
 8331: =item B<$umode> Students authentication mode
 8332: 
 8333: =item B<$upass> Students password
 8334: 
 8335: =item B<$first> Students first name
 8336: 
 8337: =item B<$middle> Students middle name
 8338: 
 8339: =item B<$last> Students last name
 8340: 
 8341: =item B<$gene> Students generation
 8342: 
 8343: =item B<$usec> Students section in course
 8344: 
 8345: =item B<$end> Unix time of the roles expiration
 8346: 
 8347: =item B<$start> Unix time of the roles start date
 8348: 
 8349: =item B<$forceid> If defined, allow $uid to be changed
 8350: 
 8351: =item B<$desiredhome> server to use as home server for student
 8352: 
 8353: =back
 8354: 
 8355: =item *
 8356: 
 8357: modify_student_enrollment
 8358: 
 8359: Change a students enrollment status in a class.  The environment variable
 8360: 'role.request.course' must be defined for this function to proceed.
 8361: 
 8362: Inputs:
 8363: 
 8364: =over 4
 8365: 
 8366: =item $udom, students domain
 8367: 
 8368: =item $uname, students name
 8369: 
 8370: =item $uid, students user id
 8371: 
 8372: =item $first, students first name
 8373: 
 8374: =item $middle
 8375: 
 8376: =item $last
 8377: 
 8378: =item $gene
 8379: 
 8380: =item $usec
 8381: 
 8382: =item $end
 8383: 
 8384: =item $start
 8385: 
 8386: =back
 8387: 
 8388: 
 8389: =item *
 8390: 
 8391: assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start) : assign
 8392: custom role; give a custom role to a user for the level given by URL.  Specify
 8393: name and domain of role author, and role name
 8394: 
 8395: =item *
 8396: 
 8397: revokerole($udom,$uname,$url,$role) : revoke a role for url
 8398: 
 8399: =item *
 8400: 
 8401: revokecustomrole($udom,$uname,$url,$role) : revoke a custom role
 8402: 
 8403: =back
 8404: 
 8405: =head2 Course Infomation
 8406: 
 8407: =over 4
 8408: 
 8409: =item *
 8410: 
 8411: coursedescription($courseid) : returns a hash of information about the
 8412: specified course id, including all environment settings for the
 8413: course, the description of the course will be in the hash under the
 8414: key 'description'
 8415: 
 8416: =item *
 8417: 
 8418: resdata($name,$domain,$type,@which) : request for current parameter
 8419: setting for a specific $type, where $type is either 'course' or 'user',
 8420: @what should be a list of parameters to ask about. This routine caches
 8421: answers for 5 minutes.
 8422: 
 8423: =back
 8424: 
 8425: =head2 Course Modification
 8426: 
 8427: =over 4
 8428: 
 8429: =item *
 8430: 
 8431: writecoursepref($courseid,%prefs) : write preferences (environment
 8432: database) for a course
 8433: 
 8434: =item *
 8435: 
 8436: createcourse($udom,$description,$url) : make/modify course
 8437: 
 8438: =back
 8439: 
 8440: =head2 Resource Subroutines
 8441: 
 8442: =over 4
 8443: 
 8444: =item *
 8445: 
 8446: subscribe($fname) : subscribe to a resource, returns URL if possible (probably should use repcopy instead)
 8447: 
 8448: =item *
 8449: 
 8450: repcopy($filename) : subscribes to the requested file, and attempts to
 8451: replicate from the owning library server, Might return
 8452: 'unavailable', 'not_found', 'forbidden', 'ok', or
 8453: 'bad_request', also attempts to grab the metadata for the
 8454: resource. Expects the local filesystem pathname
 8455: (/home/httpd/html/res/....)
 8456: 
 8457: =back
 8458: 
 8459: =head2 Resource Information
 8460: 
 8461: =over 4
 8462: 
 8463: =item *
 8464: 
 8465: EXT($varname,$symb,$udom,$uname) : evaluates and returns the value of
 8466: a vairety of different possible values, $varname should be a request
 8467: string, and the other parameters can be used to specify who and what
 8468: one is asking about.
 8469: 
 8470: Possible values for $varname are environment.lastname (or other item
 8471: from the envirnment hash), user.name (or someother aspect about the
 8472: user), resource.0.maxtries (or some other part and parameter of a
 8473: resource)
 8474: 
 8475: =item *
 8476: 
 8477: directcondval($number) : get current value of a condition; reads from a state
 8478: string
 8479: 
 8480: =item *
 8481: 
 8482: condval($condidx) : value of condition index based on state
 8483: 
 8484: =item *
 8485: 
 8486: metadata($uri,$what,$liburi,$prefix,$depthcount) : request a
 8487: resource's metadata, $what should be either a specific key, or either
 8488: 'keys' (to get a list of possible keys) or 'packages' to get a list of
 8489: packages that this resource currently uses, the last 3 arguments are only used internally for recursive metadata.
 8490: 
 8491: this function automatically caches all requests
 8492: 
 8493: =item *
 8494: 
 8495: metadata_query($query,$custom,$customshow) : make a metadata query against the
 8496: network of library servers; returns file handle of where SQL and regex results
 8497: will be stored for query
 8498: 
 8499: =item *
 8500: 
 8501: symbread($filename) : return symbolic list entry (filename argument optional);
 8502: returns the data handle
 8503: 
 8504: =item *
 8505: 
 8506: symbverify($symb,$thisfn) : verifies that $symb actually exists and is
 8507: a possible symb for the URL in $thisfn, and if is an encryypted
 8508: resource that the user accessed using /enc/ returns a 1 on success, 0
 8509: on failure, user must be in a course, as it assumes the existance of
 8510: the course initial hash, and uses $env('request.course.id'}
 8511: 
 8512: 
 8513: =item *
 8514: 
 8515: symbclean($symb) : removes versions numbers from a symb, returns the
 8516: cleaned symb
 8517: 
 8518: =item *
 8519: 
 8520: is_on_map($uri) : checks if the $uri is somewhere on the current
 8521: course map, user must be in a course for it to work.
 8522: 
 8523: =item *
 8524: 
 8525: numval($salt) : return random seed value (addend for rndseed)
 8526: 
 8527: =item *
 8528: 
 8529: rndseed($symb,$courseid,$udom,$uname) : create a random sum; returns
 8530: a random seed, all arguments are optional, if they aren't sent it uses the
 8531: environment to derive them. Note: if symb isn't sent and it can't get one
 8532: from &symbread it will use the current time as its return value
 8533: 
 8534: =item *
 8535: 
 8536: ireceipt($funame,$fudom,$fucourseid,$fusymb) : return unique,
 8537: unfakeable, receipt
 8538: 
 8539: =item *
 8540: 
 8541: receipt() : API to ireceipt working off of env values; given out to users
 8542: 
 8543: =item *
 8544: 
 8545: countacc($url) : count the number of accesses to a given URL
 8546: 
 8547: =item *
 8548: 
 8549: 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
 8550: 
 8551: =item *
 8552: 
 8553: 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)
 8554: 
 8555: =item *
 8556: 
 8557: expirespread($uname,$udom,$stype,$usymb) : set expire date for spreadsheet
 8558: 
 8559: =item *
 8560: 
 8561: devalidate($symb) : devalidate temporary spreadsheet calculations,
 8562: forcing spreadsheet to reevaluate the resource scores next time.
 8563: 
 8564: =back
 8565: 
 8566: =head2 Storing/Retreiving Data
 8567: 
 8568: =over 4
 8569: 
 8570: =item *
 8571: 
 8572: store($storehash,$symb,$namespace,$udom,$uname) : stores hash permanently
 8573: for this url; hashref needs to be given and should be a \%hashname; the
 8574: remaining args aren't required and if they aren't passed or are '' they will
 8575: be derived from the env
 8576: 
 8577: =item *
 8578: 
 8579: cstore($storehash,$symb,$namespace,$udom,$uname) : same as store but
 8580: uses critical subroutine
 8581: 
 8582: =item *
 8583: 
 8584: restore($symb,$namespace,$udom,$uname) : returns hash for this symb;
 8585: all args are optional
 8586: 
 8587: =item *
 8588: 
 8589: dumpstore($namespace,$udom,$uname,$regexp,$range) : 
 8590: dumps the complete (or key matching regexp) namespace into a hash
 8591: ($udom, $uname, $regexp, $range are optional) for a namespace that is
 8592: normally &store()ed into
 8593: 
 8594: $range should be either an integer '100' (give me the first 100
 8595:                                            matching records)
 8596:               or be  two integers sperated by a - with no spaces
 8597:                  '30-50' (give me the 30th through the 50th matching
 8598:                           records)
 8599: 
 8600: 
 8601: =item *
 8602: 
 8603: putstore($namespace,$symb,$version,$storehash,$udomain,$uname) :
 8604: replaces a &store() version of data with a replacement set of data
 8605: for a particular resource in a namespace passed in the $storehash hash 
 8606: reference
 8607: 
 8608: =item *
 8609: 
 8610: tmpstore($storehash,$symb,$namespace,$udom,$uname) : storage that
 8611: works very similar to store/cstore, but all data is stored in a
 8612: temporary location and can be reset using tmpreset, $storehash should
 8613: be a hash reference, returns nothing on success
 8614: 
 8615: =item *
 8616: 
 8617: tmprestore($symb,$namespace,$udom,$uname) : storage that works very
 8618: similar to restore, but all data is stored in a temporary location and
 8619: can be reset using tmpreset. Returns a hash of values on success,
 8620: error string otherwise.
 8621: 
 8622: =item *
 8623: 
 8624: tmpreset($symb,$namespace,$udom,$uname) : temporary storage reset,
 8625: deltes all keys for $symb form the temporary storage hash.
 8626: 
 8627: =item *
 8628: 
 8629: get($namespace,$storearr,$udom,$uname) : returns hash with keys from array
 8630: reference filled in from namesp ($udom and $uname are optional)
 8631: 
 8632: =item *
 8633: 
 8634: del($namespace,$storearr,$udom,$uname) : deletes keys out of array from
 8635: namesp ($udom and $uname are optional)
 8636: 
 8637: =item *
 8638: 
 8639: dump($namespace,$udom,$uname,$regexp,$range) : 
 8640: dumps the complete (or key matching regexp) namespace into a hash
 8641: ($udom, $uname, $regexp, $range are optional)
 8642: 
 8643: $range should be either an integer '100' (give me the first 100
 8644:                                            matching records)
 8645:               or be  two integers sperated by a - with no spaces
 8646:                  '30-50' (give me the 30th through the 50th matching
 8647:                           records)
 8648: =item *
 8649: 
 8650: inc($namespace,$store,$udom,$uname) : increments $store in $namespace.
 8651: $store can be a scalar, an array reference, or if the amount to be 
 8652: incremented is > 1, a hash reference.
 8653: 
 8654: ($udom and $uname are optional)
 8655: 
 8656: =item *
 8657: 
 8658: put($namespace,$storehash,$udom,$uname) : stores hash in namesp
 8659: ($udom and $uname are optional)
 8660: 
 8661: =item *
 8662: 
 8663: cput($namespace,$storehash,$udom,$uname) : critical put
 8664: ($udom and $uname are optional)
 8665: 
 8666: =item *
 8667: 
 8668: newput($namespace,$storehash,$udom,$uname) :
 8669: 
 8670: Attempts to store the items in the $storehash, but only if they don't
 8671: currently exist, if this succeeds you can be certain that you have 
 8672: successfully created a new key value pair in the $namespace db.
 8673: 
 8674: 
 8675: Args:
 8676:  $namespace: name of database to store values to
 8677:  $storehash: hashref to store to the db
 8678:  $udom: (optional) domain of user containing the db
 8679:  $uname: (optional) name of user caontaining the db
 8680: 
 8681: Returns:
 8682:  'ok' -> succeeded in storing all keys of $storehash
 8683:  'key_exists: <key>' -> failed to anything out of $storehash, as at
 8684:                         least <key> already existed in the db (other
 8685:                         requested keys may also already exist)
 8686:  'error: <msg>' -> unable to tie the DB or other erorr occured
 8687:  'con_lost' -> unable to contact request server
 8688:  'refused' -> action was not allowed by remote machine
 8689: 
 8690: 
 8691: =item *
 8692: 
 8693: eget($namespace,$storearr,$udom,$uname) : returns hash with keys from array
 8694: reference filled in from namesp (encrypts the return communication)
 8695: ($udom and $uname are optional)
 8696: 
 8697: =item *
 8698: 
 8699: log($udom,$name,$home,$message) : write to permanent log for user; use
 8700: critical subroutine
 8701: 
 8702: =item *
 8703: 
 8704: get_dom($namespace,$storearr,$udom,$uhome) : returns hash with keys from
 8705: array reference filled in from namespace found in domain level on either
 8706: specified domain server ($uhome) or primary domain server ($udom and $uhome are optional).
 8707: 
 8708: =item *
 8709: 
 8710: put_dom($namespace,$storehash,$udom,$uhome) :  stores hash in namespace at 
 8711: domain level either on specified domain server ($uhome) or primary domain 
 8712: server ($udom and $uhome are optional)
 8713: 
 8714: =back
 8715: 
 8716: =head2 Network Status Functions
 8717: 
 8718: =over 4
 8719: 
 8720: =item *
 8721: 
 8722: dirlist($uri) : return directory list based on URI
 8723: 
 8724: =item *
 8725: 
 8726: spareserver() : find server with least workload from spare.tab
 8727: 
 8728: =back
 8729: 
 8730: =head2 Apache Request
 8731: 
 8732: =over 4
 8733: 
 8734: =item *
 8735: 
 8736: ssi($url,%hash) : server side include, does a complete request cycle on url to
 8737: localhost, posts hash
 8738: 
 8739: =back
 8740: 
 8741: =head2 Data to String to Data
 8742: 
 8743: =over 4
 8744: 
 8745: =item *
 8746: 
 8747: hash2str(%hash) : convert a hash into a string complete with escaping and '='
 8748: and '&' separators, supports elements that are arrayrefs and hashrefs
 8749: 
 8750: =item *
 8751: 
 8752: hashref2str($hashref) : convert a hashref into a string complete with
 8753: escaping and '=' and '&' separators, supports elements that are
 8754: arrayrefs and hashrefs
 8755: 
 8756: =item *
 8757: 
 8758: arrayref2str($arrayref) : convert an arrayref into a string complete
 8759: with escaping and '&' separators, supports elements that are arrayrefs
 8760: and hashrefs
 8761: 
 8762: =item *
 8763: 
 8764: str2hash($string) : convert string to hash using unescaping and
 8765: splitting on '=' and '&', supports elements that are arrayrefs and
 8766: hashrefs
 8767: 
 8768: =item *
 8769: 
 8770: str2array($string) : convert string to hash using unescaping and
 8771: splitting on '&', supports elements that are arrayrefs and hashrefs
 8772: 
 8773: =back
 8774: 
 8775: =head2 Logging Routines
 8776: 
 8777: =over 4
 8778: 
 8779: These routines allow one to make log messages in the lonnet.log and
 8780: lonnet.perm logfiles.
 8781: 
 8782: =item *
 8783: 
 8784: logtouch() : make sure the logfile, lonnet.log, exists
 8785: 
 8786: =item *
 8787: 
 8788: logthis() : append message to the normal lonnet.log file, it gets
 8789: preiodically rolled over and deleted.
 8790: 
 8791: =item *
 8792: 
 8793: logperm() : append a permanent message to lonnet.perm.log, this log
 8794: file never gets deleted by any automated portion of the system, only
 8795: messages of critical importance should go in here.
 8796: 
 8797: =back
 8798: 
 8799: =head2 General File Helper Routines
 8800: 
 8801: =over 4
 8802: 
 8803: =item *
 8804: 
 8805: getfile($file,$caller) : two cases - requests for files in /res or in /uploaded.
 8806: (a) files in /uploaded
 8807:   (i) If a local copy of the file exists - 
 8808:       compares modification date of local copy with last-modified date for 
 8809:       definitive version stored on home server for course. If local copy is 
 8810:       stale, requests a new version from the home server and stores it. 
 8811:       If the original has been removed from the home server, then local copy 
 8812:       is unlinked.
 8813:   (ii) If local copy does not exist -
 8814:       requests the file from the home server and stores it. 
 8815:   
 8816:   If $caller is 'uploadrep':  
 8817:     This indicates a call from lonuploadrep.pm (PerlHeaderParserHandler phase)
 8818:     for request for files originally uploaded via DOCS. 
 8819:      - returns 'ok' if fresh local copy now available, -1 otherwise.
 8820:   
 8821:   Otherwise:
 8822:      This indicates a call from the content generation phase of the request.
 8823:      -  returns the entire contents of the file or -1.
 8824:      
 8825: (b) files in /res
 8826:    - returns the entire contents of a file or -1; 
 8827:    it properly subscribes to and replicates the file if neccessary.
 8828: 
 8829: 
 8830: =item *
 8831: 
 8832: stat_file($url) : $url is expected to be a /res/ or /uploaded/ style file
 8833:                   reference
 8834: 
 8835: returns either a stat() list of data about the file or an empty list
 8836: if the file doesn't exist or couldn't find out about it (connection
 8837: problems or user unknown)
 8838: 
 8839: =item *
 8840: 
 8841: filelocation($dir,$file) : returns file system location of a file
 8842: based on URI; meant to be "fairly clean" absolute reference, $dir is a
 8843: directory that relative $file lookups are to looked in ($dir of /a/dir
 8844: and a file of ../bob will become /a/bob)
 8845: 
 8846: =item *
 8847: 
 8848: hreflocation($dir,$file) : returns file system location or a URL; same as
 8849: filelocation except for hrefs
 8850: 
 8851: =item *
 8852: 
 8853: declutter() : declutters URLs (remove docroot, beginning slashes, 'res' etc)
 8854: 
 8855: =back
 8856: 
 8857: =head2 Usererfile file routines (/uploaded*)
 8858: 
 8859: =over 4
 8860: 
 8861: =item *
 8862: 
 8863: userfileupload(): main rotine for putting a file in a user or course's
 8864:                   filespace, arguments are,
 8865: 
 8866:  formname - required - this is the name of the element in $env where the
 8867:            filename, and the contents of the file to create/modifed exist
 8868:            the filename is in $env{'form.'.$formname.'.filename'} and the
 8869:            contents of the file is located in $env{'form.'.$formname}
 8870:  coursedoc - if true, store the file in the course of the active role
 8871:              of the current user
 8872:  subdir - required - subdirectory to put the file in under ../userfiles/
 8873:          if undefined, it will be placed in "unknown"
 8874: 
 8875:  (This routine calls clean_filename() to remove any dangerous
 8876:  characters from the filename, and then calls finuserfileupload() to
 8877:  complete the transaction)
 8878: 
 8879:  returns either the url of the uploaded file (/uploaded/....) if successful
 8880:  and /adm/notfound.html if unsuccessful
 8881: 
 8882: =item *
 8883: 
 8884: clean_filename(): routine for cleaing a filename up for storage in
 8885:                  userfile space, argument is:
 8886: 
 8887:  filename - proposed filename
 8888: 
 8889: returns: the new clean filename
 8890: 
 8891: =item *
 8892: 
 8893: finishuserfileupload(): routine that creaes and sends the file to
 8894: userspace, probably shouldn't be called directly
 8895: 
 8896:   docuname: username or courseid of destination for the file
 8897:   docudom: domain of user/course of destination for the file
 8898:   formname: same as for userfileupload()
 8899:   fname: filename (inculding subdirectories) for the file
 8900: 
 8901:  returns either the url of the uploaded file (/uploaded/....) if successful
 8902:  and /adm/notfound.html if unsuccessful
 8903: 
 8904: =item *
 8905: 
 8906: renameuserfile(): renames an existing userfile to a new name
 8907: 
 8908:   Args:
 8909:    docuname: username or courseid of destination for the file
 8910:    docudom: domain of user/course of destination for the file
 8911:    old: current file name (including any subdirs under userfiles)
 8912:    new: desired file name (including any subdirs under userfiles)
 8913: 
 8914: =item *
 8915: 
 8916: mkdiruserfile(): creates a directory is a userfiles dir
 8917: 
 8918:   Args:
 8919:    docuname: username or courseid of destination for the file
 8920:    docudom: domain of user/course of destination for the file
 8921:    dir: dir to create (including any subdirs under userfiles)
 8922: 
 8923: =item *
 8924: 
 8925: removeuserfile(): removes a file that exists in userfiles
 8926: 
 8927:   Args:
 8928:    docuname: username or courseid of destination for the file
 8929:    docudom: domain of user/course of destination for the file
 8930:    fname: filname to delete (including any subdirs under userfiles)
 8931: 
 8932: =item *
 8933: 
 8934: removeuploadedurl(): convience function for removeuserfile()
 8935: 
 8936:   Args:
 8937:    url:  a full /uploaded/... url to delete
 8938: 
 8939: =item * 
 8940: 
 8941: get_portfile_permissions():
 8942:   Args:
 8943:     domain: domain of user or course contain the portfolio files
 8944:     user: name of user or num of course contain the portfolio files
 8945:   Returns:
 8946:     hashref of a dump of the proper file_permissions.db
 8947:    
 8948: 
 8949: =item * 
 8950: 
 8951: get_access_controls():
 8952: 
 8953: Args:
 8954:   current_permissions: the hash ref returned from get_portfile_permissions()
 8955:   group: (optional) the group you want the files associated with
 8956:   file: (optional) the file you want access info on
 8957: 
 8958: Returns:
 8959:     a hash (keys are file names) of hashes containing
 8960:         keys are: path to file/file_name\0uniqueID:scope_end_start (see below)
 8961:         values are XML containing access control settings (see below) 
 8962: 
 8963: Internal notes:
 8964: 
 8965:  access controls are stored in file_permissions.db as key=value pairs.
 8966:     key -> path to file/file_name\0uniqueID:scope_end_start
 8967:         where scope -> public,guest,course,group,domains or users.
 8968:               end -> UNIX time for end of access (0 -> no end date)
 8969:               start -> UNIX time for start of access
 8970: 
 8971:     value -> XML description of access control
 8972:            <scope type=""> (type =1 of: public,guest,course,group,domains,users">
 8973:             <start></start>
 8974:             <end></end>
 8975: 
 8976:             <password></password>  for scope type = guest
 8977: 
 8978:             <domain></domain>     for scope type = course or group
 8979:             <number></number>
 8980:             <roles id="">
 8981:              <role></role>
 8982:              <access></access>
 8983:              <section></section>
 8984:              <group></group>
 8985:             </roles>
 8986: 
 8987:             <dom></dom>         for scope type = domains
 8988: 
 8989:             <users>             for scope type = users
 8990:              <user>
 8991:               <uname></uname>
 8992:               <udom></udom>
 8993:              </user>
 8994:             </users>
 8995:            </scope> 
 8996:               
 8997:  Access data is also aggregated for each file in an additional key=value pair:
 8998:  key -> path to file/file_name\0accesscontrol 
 8999:  value -> reference to hash
 9000:           hash contains key = value pairs
 9001:           where key = uniqueID:scope_end_start
 9002:                 value = UNIX time record was last updated
 9003: 
 9004:           Used to improve speed of look-ups of access controls for each file.  
 9005:  
 9006:  Locks on files (resulting from submission of portfolio file to a homework problem stored in array of arrays.
 9007: 
 9008: modify_access_controls():
 9009: 
 9010: Modifies access controls for a portfolio file
 9011: Args
 9012: 1. file name
 9013: 2. reference to hash of required changes,
 9014: 3. domain
 9015: 4. username
 9016:   where domain,username are the domain of the portfolio owner 
 9017:   (either a user or a course) 
 9018: 
 9019: Returns:
 9020: 1. result of additions or updates ('ok' or 'error', with error message). 
 9021: 2. result of deletions ('ok' or 'error', with error message).
 9022: 3. reference to hash of any new or updated access controls.
 9023: 4. reference to hash used to map incoming IDs to uniqueIDs assigned to control.
 9024:    key = integer (inbound ID)
 9025:    value = uniqueID  
 9026: 
 9027: =back
 9028: 
 9029: =head2 HTTP Helper Routines
 9030: 
 9031: =over 4
 9032: 
 9033: =item *
 9034: 
 9035: escape() : unpack non-word characters into CGI-compatible hex codes
 9036: 
 9037: =item *
 9038: 
 9039: unescape() : pack CGI-compatible hex codes into actual non-word ASCII character
 9040: 
 9041: =back
 9042: 
 9043: =head1 PRIVATE SUBROUTINES
 9044: 
 9045: =head2 Underlying communication routines (Shouldn't call)
 9046: 
 9047: =over 4
 9048: 
 9049: =item *
 9050: 
 9051: subreply() : tries to pass a message to lonc, returns con_lost if incapable
 9052: 
 9053: =item *
 9054: 
 9055: reply() : uses subreply to send a message to remote machine, logs all failures
 9056: 
 9057: =item *
 9058: 
 9059: critical() : passes a critical message to another server; if cannot
 9060: get through then place message in connection buffer directory and
 9061: returns con_delayed, if incapable of saving message, returns
 9062: con_failed
 9063: 
 9064: =item *
 9065: 
 9066: reconlonc() : tries to reconnect lonc client processes.
 9067: 
 9068: =back
 9069: 
 9070: =head2 Resource Access Logging
 9071: 
 9072: =over 4
 9073: 
 9074: =item *
 9075: 
 9076: flushcourselogs() : flush (save) buffer logs and access logs
 9077: 
 9078: =item *
 9079: 
 9080: courselog($what) : save message for course in hash
 9081: 
 9082: =item *
 9083: 
 9084: courseacclog($what) : save message for course using &courselog().  Perform
 9085: special processing for specific resource types (problems, exams, quizzes, etc).
 9086: 
 9087: =item *
 9088: 
 9089: goodbye() : flush course logs and log shutting down; it is called in srm.conf
 9090: as a PerlChildExitHandler
 9091: 
 9092: =back
 9093: 
 9094: =head2 Other
 9095: 
 9096: =over 4
 9097: 
 9098: =item *
 9099: 
 9100: symblist($mapname,%newhash) : update symbolic storage links
 9101: 
 9102: =back
 9103: 
 9104: =cut

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