File:  [LON-CAPA] / loncom / lonnet / perl / lonnet.pm
Revision 1.1356: download - view: text, annotated - select for diffs
Sat Oct 7 00:50:52 2017 UTC (6 years, 9 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Bug 6867
  - Domain configuration to set default math renderer for <m> tag content.
  - Default renderer for unconfigured domains changed from tth to MathJax.

    1: # The LearningOnline Network
    2: # TCP networking package
    3: #
    4: # $Id: lonnet.pm,v 1.1356 2017/10/07 00:50:52 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: =pod
   31: 
   32: =head1 NAME
   33: 
   34: Apache::lonnet.pm
   35: 
   36: =head1 SYNOPSIS
   37: 
   38: This file is an interface to the lonc processes of
   39: the LON-CAPA network as well as set of elaborated functions for handling information
   40: necessary for navigating through a given cluster of LON-CAPA machines within a
   41: domain. There are over 40 specialized functions in this module which handle the
   42: reading and transmission of metadata, user information (ids, names, environments, roles,
   43: logs), file information (storage, reading, directories, extensions, replication, embedded
   44: styles and descriptors), educational resources (course descriptions, section names and
   45: numbers), url hashing (to assign roles on a url basis), and translating abbreviated symbols to
   46: and from more descriptive phrases or explanations.
   47: 
   48: This is part of the LearningOnline Network with CAPA project
   49: described at http://www.lon-capa.org.
   50: 
   51: =head1 Package Variables
   52: 
   53: These are largely undocumented, so if you decipher one please note it here.
   54: 
   55: =over 4
   56: 
   57: =item $processmarker
   58: 
   59: Contains the time this process was started and this servers host id.
   60: 
   61: =item $dumpcount
   62: 
   63: Counts the number of times a message log flush has been attempted (regardless
   64: of success) by this process.  Used as part of the filename when messages are
   65: delayed.
   66: 
   67: =back
   68: 
   69: =cut
   70: 
   71: package Apache::lonnet;
   72: 
   73: use strict;
   74: use HTTP::Date;
   75: use Image::Magick;
   76: 
   77: 
   78: use Encode;
   79: 
   80: use vars qw(%perlvar %spareid %pr %prp $memcache %packagetab $tmpdir
   81:             $_64bit %env %protocol %loncaparevs %serverhomeIDs %needsrelease
   82:             %managerstab);
   83: 
   84: my (%badServerCache, $memcache, %courselogs, %accesshash, %domainrolehash,
   85:     %userrolehash, $processmarker, $dumpcount, %coursedombuf,
   86:     %coursenumbuf, %coursehombuf, %coursedescrbuf, %courseinstcodebuf,
   87:     %courseownerbuf, %coursetypebuf,$locknum);
   88: 
   89: use IO::Socket;
   90: use GDBM_File;
   91: use HTML::LCParser;
   92: use Fcntl qw(:flock);
   93: use Storable qw(thaw nfreeze);
   94: use Time::HiRes qw( sleep gettimeofday tv_interval );
   95: use Cache::Memcached;
   96: use Digest::MD5;
   97: use Math::Random;
   98: use File::MMagic;
   99: use LONCAPA qw(:DEFAULT :match);
  100: use LONCAPA::Configuration;
  101: use LONCAPA::lonmetadata;
  102: use LONCAPA::Lond;
  103: use LONCAPA::LWPReq;
  104: 
  105: use File::Copy;
  106: 
  107: my $readit;
  108: my $max_connection_retries = 20;     # Or some such value.
  109: 
  110: require Exporter;
  111: 
  112: our @ISA = qw (Exporter);
  113: our @EXPORT = qw(%env);
  114: 
  115: 
  116: # ------------------------------------ Logging (parameters, docs, slots, roles)
  117: {
  118:     my $logid;
  119:     sub write_log {
  120: 	my ($context,$hash_name,$storehash,$delflag,$uname,$udom,$cnum,$cdom)=@_;
  121:         if ($context eq 'course') {
  122:             if (($cnum eq '') || ($cdom eq '')) {
  123:                 $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
  124:                 $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
  125:             }
  126:         }
  127: 	$logid ++;
  128:         my $now = time();
  129: 	my $id=$now.'00000'.$$.'00000'.$logid;
  130:         my $logentry = { 
  131:                           $id => {
  132:                                    'exe_uname' => $env{'user.name'},
  133:                                    'exe_udom'  => $env{'user.domain'},
  134:                                    'exe_time'  => $now,
  135:                                    'exe_ip'    => $ENV{'REMOTE_ADDR'},
  136:                                    'delflag'   => $delflag,
  137:                                    'logentry'  => $storehash,
  138:                                    'uname'     => $uname,
  139:                                    'udom'      => $udom,
  140:                                   }
  141:                        };
  142: 	return &put('nohist_'.$hash_name,$logentry,$cdom,$cnum);
  143:     }
  144: }
  145: 
  146: sub logtouch {
  147:     my $execdir=$perlvar{'lonDaemons'};
  148:     unless (-e "$execdir/logs/lonnet.log") {	
  149: 	open(my $fh,">>$execdir/logs/lonnet.log");
  150: 	close $fh;
  151:     }
  152:     my ($wwwuid,$wwwgid)=(getpwnam('www'))[2,3];
  153:     chown($wwwuid,$wwwgid,$execdir.'/logs/lonnet.log');
  154: }
  155: 
  156: sub logthis {
  157:     my $message=shift;
  158:     my $execdir=$perlvar{'lonDaemons'};
  159:     my $now=time;
  160:     my $local=localtime($now);
  161:     if (open(my $fh,">>$execdir/logs/lonnet.log")) {
  162: 	my $logstring = $local. " ($$): ".$message."\n"; # Keep any \'s in string.
  163: 	print $fh $logstring;
  164: 	close($fh);
  165:     }
  166:     return 1;
  167: }
  168: 
  169: sub logperm {
  170:     my $message=shift;
  171:     my $execdir=$perlvar{'lonDaemons'};
  172:     my $now=time;
  173:     my $local=localtime($now);
  174:     if (open(my $fh,">>$execdir/logs/lonnet.perm.log")) {
  175: 	print $fh "$now:$message:$local\n";
  176: 	close($fh);
  177:     }
  178:     return 1;
  179: }
  180: 
  181: sub create_connection {
  182:     my ($hostname,$lonid) = @_;
  183:     my $client=IO::Socket::UNIX->new(Peer    => $perlvar{'lonSockCreate'},
  184: 				     Type    => SOCK_STREAM,
  185: 				     Timeout => 10);
  186:     return 0 if (!$client);
  187:     print $client (join(':',$hostname,$lonid,&machine_ids($hostname))."\n");
  188:     my $result = <$client>;
  189:     chomp($result);
  190:     return 1 if ($result eq 'done');
  191:     return 0;
  192: }
  193: 
  194: sub get_server_timezone {
  195:     my ($cnum,$cdom) = @_;
  196:     my $home=&homeserver($cnum,$cdom);
  197:     if ($home ne 'no_host') {
  198:         my $cachetime = 24*3600;
  199:         my ($timezone,$cached)=&is_cached_new('servertimezone',$home);
  200:         if (defined($cached)) {
  201:             return $timezone;
  202:         } else {
  203:             my $timezone = &reply('servertimezone',$home);
  204:             return &do_cache_new('servertimezone',$home,$timezone,$cachetime);
  205:         }
  206:     }
  207: }
  208: 
  209: sub get_server_distarch {
  210:     my ($lonhost,$ignore_cache) = @_;
  211:     if (defined($lonhost)) {
  212:         if (!defined(&hostname($lonhost))) {
  213:             return;
  214:         }
  215:         my $cachetime = 12*3600;
  216:         if (!$ignore_cache) {
  217:             my ($distarch,$cached)=&is_cached_new('serverdistarch',$lonhost);
  218:             if (defined($cached)) {
  219:                 return $distarch;
  220:             }
  221:         }
  222:         my $rep = &reply('serverdistarch',$lonhost);
  223:         unless ($rep eq 'unknown_command' || $rep eq 'no_such_host' ||
  224:                 $rep eq 'con_lost' || $rep eq 'rejected' || $rep eq 'refused' ||
  225:                 $rep eq '') {
  226:             return &do_cache_new('serverdistarch',$lonhost,$rep,$cachetime);
  227:         }
  228:     }
  229:     return;
  230: }
  231: 
  232: sub get_servercerts_info {
  233:     my ($lonhost,$context) = @_;
  234:     my ($rep,$uselocal);
  235:     if (grep { $_ eq $lonhost } &current_machine_ids()) {
  236:         $uselocal = 1;
  237:     }
  238:     if (($context ne 'cgi') && ($uselocal)) {
  239:         my $distro = (split(/\:/,&get_server_distarch($lonhost)))[0];
  240:         if ($distro eq '') {
  241:             $uselocal = 0;
  242:         } elsif ($distro =~ /^(?:centos|redhat|scientific)(\d+)$/) {
  243:             if ($1 < 6) {
  244:                 $uselocal = 0;
  245:             }
  246:         }  elsif ($distro =~ /^(?:sles)(\d+)$/) {
  247:             if ($1 < 12) {
  248:                 $uselocal = 0;
  249:             }
  250:         }
  251:     }
  252:     if ($uselocal) {
  253:         $rep = LONCAPA::Lond::server_certs(\%perlvar);
  254:     } else {
  255:         $rep=&reply('servercerts',$lonhost);
  256:     }
  257:     my ($result,%returnhash);
  258:     if (defined($lonhost)) {
  259:         if (!defined(&hostname($lonhost))) {
  260:             return;
  261:         }
  262:     }
  263:     if (($rep=~/^(refused|rejected|error)/) || ($rep eq 'con_lost') ||
  264:         ($rep eq 'unknown_cmd')) {
  265:         $result = $rep;
  266:     } else {
  267:         $result = 'ok';
  268:         my @pairs=split(/\&/,$rep);
  269:         foreach my $item (@pairs) {
  270:             my ($key,$value)=split(/=/,$item,2);
  271:             my $what = &unescape($key);
  272:             $returnhash{$what}=&thaw_unescape($value);
  273:         }
  274:     }
  275:     return ($result,\%returnhash);
  276: }
  277: 
  278: sub get_server_loncaparev {
  279:     my ($dom,$lonhost,$ignore_cache,$caller) = @_;
  280:     if (defined($lonhost)) {
  281:         if (!defined(&hostname($lonhost))) {
  282:             undef($lonhost);
  283:         }
  284:     }
  285:     if (!defined($lonhost)) {
  286:         if (defined(&domain($dom,'primary'))) {
  287:             $lonhost=&domain($dom,'primary');
  288:             if ($lonhost eq 'no_host') {
  289:                 undef($lonhost);
  290:             }
  291:         }
  292:     }
  293:     if (defined($lonhost)) {
  294:         my $cachetime = 12*3600;
  295:         if (!$ignore_cache) {
  296:             my ($loncaparev,$cached)=&is_cached_new('serverloncaparev',$lonhost);
  297:             if (defined($cached)) {
  298:                 return $loncaparev;
  299:             }
  300:         }
  301:         my ($answer,$loncaparev);
  302:         my @ids=&current_machine_ids();
  303:         if (grep(/^\Q$lonhost\E$/,@ids)) {
  304:             $answer = $perlvar{'lonVersion'};
  305:             if ($answer =~ /^[\'\"]?([\w.\-]+)[\'\"]?$/) {
  306:                 $loncaparev = $1;
  307:             }
  308:         } else {
  309:             $answer = &reply('serverloncaparev',$lonhost);
  310:             if (($answer eq 'unknown_cmd') || ($answer eq 'con_lost')) {
  311:                 if ($caller eq 'loncron') {
  312:                     my $protocol = $protocol{$lonhost};
  313:                     $protocol = 'http' if ($protocol ne 'https');
  314:                     my $url = $protocol.'://'.&hostname($lonhost).'/adm/about.html';
  315:                     my $request=new HTTP::Request('GET',$url);
  316:                     my $response=&LONCAPA::LWPReq::makerequest($lonhost,$request,'',\%perlvar,4,1);
  317:                     unless ($response->is_error()) {
  318:                         my $content = $response->content;
  319:                         if ($content =~ /<p>VERSION\:\s*([\w.\-]+)<\/p>/) {
  320:                             $loncaparev = $1;
  321:                         }
  322:                     }
  323:                 } else {
  324:                     $loncaparev = $loncaparevs{$lonhost};
  325:                 }
  326:             } elsif ($answer =~ /^[\'\"]?([\w.\-]+)[\'\"]?$/) {
  327:                 $loncaparev = $1;
  328:             }
  329:         }
  330:         return &do_cache_new('serverloncaparev',$lonhost,$loncaparev,$cachetime);
  331:     }
  332: }
  333: 
  334: sub get_server_homeID {
  335:     my ($hostname,$ignore_cache,$caller) = @_;
  336:     unless ($ignore_cache) {
  337:         my ($serverhomeID,$cached)=&is_cached_new('serverhomeID',$hostname);
  338:         if (defined($cached)) {
  339:             return $serverhomeID;
  340:         }
  341:     }
  342:     my $cachetime = 12*3600;
  343:     my $serverhomeID;
  344:     if ($caller eq 'loncron') { 
  345:         my @machine_ids = &machine_ids($hostname);
  346:         foreach my $id (@machine_ids) {
  347:             my $response = &reply('serverhomeID',$id);
  348:             unless (($response eq 'unknown_cmd') || ($response eq 'con_lost')) {
  349:                 $serverhomeID = $response;
  350:                 last;
  351:             }
  352:         }
  353:         if ($serverhomeID eq '') {
  354:             $serverhomeID = $machine_ids[-1];
  355:         }
  356:     } else {
  357:         $serverhomeID = $serverhomeIDs{$hostname};
  358:     }
  359:     return &do_cache_new('serverhomeID',$hostname,$serverhomeID,$cachetime);
  360: }
  361: 
  362: sub get_remote_globals {
  363:     my ($lonhost,$whathash,$ignore_cache) = @_;
  364:     my ($result,%returnhash,%whatneeded);
  365:     if (ref($whathash) eq 'HASH') {
  366:         foreach my $what (sort(keys(%{$whathash}))) {
  367:             my $hashid = $lonhost.'-'.$what;
  368:             my ($response,$cached);
  369:             unless ($ignore_cache) {
  370:                 ($response,$cached)=&is_cached_new('lonnetglobal',$hashid);
  371:             }
  372:             if (defined($cached)) {
  373:                 $returnhash{$what} = $response;
  374:             } else {
  375:                 $whatneeded{$what} = 1;
  376:             }
  377:         }
  378:         if (keys(%whatneeded) == 0) {
  379:             $result = 'ok';
  380:         } else {
  381:             my $requested = &freeze_escape(\%whatneeded);
  382:             my $rep=&reply('readlonnetglobal:'.$requested,$lonhost);
  383:             if (($rep=~/^(refused|rejected|error)/) || ($rep eq 'con_lost') ||
  384:                 ($rep eq 'unknown_cmd')) {
  385:                 $result = $rep;
  386:             } else {
  387:                 $result = 'ok';
  388:                 my @pairs=split(/\&/,$rep);
  389:                 foreach my $item (@pairs) {
  390:                     my ($key,$value)=split(/=/,$item,2);
  391:                     my $what = &unescape($key);
  392:                     my $hashid = $lonhost.'-'.$what;
  393:                     $returnhash{$what}=&thaw_unescape($value);
  394:                     &do_cache_new('lonnetglobal',$hashid,$returnhash{$what},600);
  395:                 }
  396:             }
  397:         }
  398:     }
  399:     return ($result,\%returnhash);
  400: }
  401: 
  402: sub remote_devalidate_cache {
  403:     my ($lonhost,$cachekeys) = @_;
  404:     my $items;
  405:     return unless (ref($cachekeys) eq 'ARRAY');
  406:     my $cachestr = join('&',@{$cachekeys});
  407:     my $response = &reply('devalidatecache:'.&escape($cachestr),$lonhost);
  408:     return $response;
  409: }
  410: 
  411: # -------------------------------------------------- Non-critical communication
  412: sub subreply {
  413:     my ($cmd,$server)=@_;
  414:     my $peerfile="$perlvar{'lonSockDir'}/".&hostname($server);
  415:     #
  416:     #  With loncnew process trimming, there's a timing hole between lonc server
  417:     #  process exit and the master server picking up the listen on the AF_UNIX
  418:     #  socket.  In that time interval, a lock file will exist:
  419: 
  420:     my $lockfile=$peerfile.".lock";
  421:     while (-e $lockfile) {	# Need to wait for the lockfile to disappear.
  422: 	sleep(0.1);
  423:     }
  424:     # At this point, either a loncnew parent is listening or an old lonc
  425:     # or loncnew child is listening so we can connect or everything's dead.
  426:     #
  427:     #   We'll give the connection a few tries before abandoning it.  If
  428:     #   connection is not possible, we'll con_lost back to the client.
  429:     #   
  430:     my $client;
  431:     for (my $retries = 0; $retries < $max_connection_retries; $retries++) {
  432: 	$client=IO::Socket::UNIX->new(Peer    =>"$peerfile",
  433: 				      Type    => SOCK_STREAM,
  434: 				      Timeout => 10);
  435: 	if ($client) {
  436: 	    last;		# Connected!
  437: 	} else {
  438: 	    &create_connection(&hostname($server),$server);
  439: 	}
  440:         sleep(0.1);	# Try again later if failed connection.
  441:     }
  442:     my $answer;
  443:     if ($client) {
  444: 	print $client "sethost:$server:$cmd\n";
  445: 	$answer=<$client>;
  446: 	if (!$answer) { $answer="con_lost"; }
  447: 	chomp($answer);
  448:     } else {
  449: 	$answer = 'con_lost';	# Failed connection.
  450:     }
  451:     return $answer;
  452: }
  453: 
  454: sub reply {
  455:     my ($cmd,$server)=@_;
  456:     unless (defined(&hostname($server))) { return 'no_such_host'; }
  457:     my $answer=subreply($cmd,$server);
  458:     if (($answer=~/^refused/) || ($answer=~/^rejected/)) {
  459:        &logthis("<font color=\"blue\">WARNING:".
  460:                 " $cmd to $server returned $answer</font>");
  461:     }
  462:     return $answer;
  463: }
  464: 
  465: # ----------------------------------------------------------- Send USR1 to lonc
  466: 
  467: sub reconlonc {
  468:     my ($lonid) = @_;
  469:     if ($lonid) {
  470:         my $hostname = &hostname($lonid);
  471: 	my $peerfile="$perlvar{'lonSockDir'}/$hostname";
  472: 	if ($hostname && -e $peerfile) {
  473: 	    &logthis("Trying to reconnect lonc for $lonid ($hostname)");
  474: 	    my $client=IO::Socket::UNIX->new(Peer    => $peerfile,
  475: 					     Type    => SOCK_STREAM,
  476: 					     Timeout => 10);
  477: 	    if ($client) {
  478: 		print $client ("reset_retries\n");
  479: 		my $answer=<$client>;
  480: 		#reset just this one.
  481: 	    }
  482: 	}
  483: 	return;
  484:     }
  485: 
  486:     &logthis("Trying to reconnect lonc");
  487:     my $loncfile="$perlvar{'lonDaemons'}/logs/lonc.pid";
  488:     if (open(my $fh,"<$loncfile")) {
  489: 	my $loncpid=<$fh>;
  490:         chomp($loncpid);
  491:         if (kill 0 => $loncpid) {
  492: 	    &logthis("lonc at pid $loncpid responding, sending USR1");
  493:             kill USR1 => $loncpid;
  494:             sleep 1;
  495:         } else {
  496: 	    &logthis(
  497:                "<font color=\"blue\">WARNING:".
  498:                " lonc at pid $loncpid not responding, giving up</font>");
  499:         }
  500:     } else {
  501: 	&logthis('<font color="blue">WARNING: lonc not running, giving up</font>');
  502:     }
  503: }
  504: 
  505: # ------------------------------------------------------ Critical communication
  506: 
  507: sub critical {
  508:     my ($cmd,$server)=@_;
  509:     unless (&hostname($server)) {
  510:         &logthis("<font color=\"blue\">WARNING:".
  511:                " Critical message to unknown server ($server)</font>");
  512:         return 'no_such_host';
  513:     }
  514:     my $answer=reply($cmd,$server);
  515:     if ($answer eq 'con_lost') {
  516: 	&reconlonc($server);
  517: 	my $answer=reply($cmd,$server);
  518:         if ($answer eq 'con_lost') {
  519:             my $now=time;
  520:             my $middlename=$cmd;
  521:             $middlename=substr($middlename,0,16);
  522:             $middlename=~s/\W//g;
  523:             my $dfilename=
  524:       "$perlvar{'lonSockDir'}/delayed/$now.$dumpcount.$$.$middlename.$server";
  525:             $dumpcount++;
  526:             {
  527: 		my $dfh;
  528: 		if (open($dfh,">$dfilename")) {
  529: 		    print $dfh "$cmd\n"; 
  530: 		    close($dfh);
  531: 		}
  532:             }
  533:             sleep 1;
  534:             my $wcmd='';
  535:             {
  536: 		my $dfh;
  537: 		if (open($dfh,"<$dfilename")) {
  538: 		    $wcmd=<$dfh>; 
  539: 		    close($dfh);
  540: 		}
  541:             }
  542:             chomp($wcmd);
  543:             if ($wcmd eq $cmd) {
  544: 		&logthis("<font color=\"blue\">WARNING: ".
  545:                          "Connection buffer $dfilename: $cmd</font>");
  546:                 &logperm("D:$server:$cmd");
  547: 	        return 'con_delayed';
  548:             } else {
  549:                 &logthis("<font color=\"red\">CRITICAL:"
  550:                         ." Critical connection failed: $server $cmd</font>");
  551:                 &logperm("F:$server:$cmd");
  552:                 return 'con_failed';
  553:             }
  554:         }
  555:     }
  556:     return $answer;
  557: }
  558: 
  559: # ------------------------------------------- check if return value is an error
  560: 
  561: sub error {
  562:     my ($result) = @_;
  563:     if ($result =~ /^(con_lost|no_such_host|error: (\d+) (.*))/) {
  564: 	if ($2 == 2) { return undef; }
  565: 	return $1;
  566:     }
  567:     return undef;
  568: }
  569: 
  570: sub convert_and_load_session_env {
  571:     my ($lonidsdir,$handle)=@_;
  572:     my @profile;
  573:     {
  574: 	my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
  575: 	if (!$opened) {
  576: 	    return 0;
  577: 	}
  578: 	flock($idf,LOCK_SH);
  579: 	@profile=<$idf>;
  580: 	close($idf);
  581:     }
  582:     my %temp_env;
  583:     foreach my $line (@profile) {
  584: 	if ($line !~ m/=/) {
  585: 	    return 0;
  586: 	}
  587: 	chomp($line);
  588: 	my ($envname,$envvalue)=split(/=/,$line,2);
  589: 	$temp_env{&unescape($envname)} = &unescape($envvalue);
  590:     }
  591:     unlink("$lonidsdir/$handle.id");
  592:     if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",&GDBM_WRCREAT(),
  593: 	    0640)) {
  594: 	%disk_env = %temp_env;
  595: 	@env{keys(%temp_env)} = @disk_env{keys(%temp_env)};
  596: 	untie(%disk_env);
  597:     }
  598:     return 1;
  599: }
  600: 
  601: # ------------------------------------------- Transfer profile into environment
  602: my $env_loaded;
  603: sub transfer_profile_to_env {
  604:     my ($lonidsdir,$handle,$force_transfer) = @_;
  605:     if (!$force_transfer && $env_loaded) { return; } 
  606: 
  607:     if (!defined($lonidsdir)) {
  608: 	$lonidsdir = $perlvar{'lonIDsDir'};
  609:     }
  610:     if (!defined($handle)) {
  611:         ($handle) = ($env{'user.environment'} =~m|/([^/]+)\.id$| );
  612:     }
  613: 
  614:     my $convert;
  615:     {
  616:     	my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
  617: 	if (!$opened) {
  618: 	    return;
  619: 	}
  620: 	flock($idf,LOCK_SH);
  621: 	if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",
  622: 		&GDBM_READER(),0640)) {
  623: 	    @env{keys(%disk_env)} = @disk_env{keys(%disk_env)};
  624: 	    untie(%disk_env);
  625: 	} else {
  626: 	    $convert = 1;
  627: 	}
  628:     }
  629:     if ($convert) {
  630: 	if (!&convert_and_load_session_env($lonidsdir,$handle)) {
  631: 	    &logthis("Failed to load session, or convert session.");
  632: 	}
  633:     }
  634: 
  635:     my %remove;
  636:     while ( my $envname = each(%env) ) {
  637:         if (my ($key,$time) = ($envname =~ /^(cgi\.(\d+)_\d+\.)/)) {
  638:             if ($time < time-300) {
  639:                 $remove{$key}++;
  640:             }
  641:         }
  642:     }
  643: 
  644:     $env{'user.environment'} = "$lonidsdir/$handle.id";
  645:     $env_loaded=1;
  646:     foreach my $expired_key (keys(%remove)) {
  647:         &delenv($expired_key);
  648:     }
  649: }
  650: 
  651: # ---------------------------------------------------- Check for valid session 
  652: sub check_for_valid_session {
  653:     my ($r,$name,$userhashref,$domref) = @_;
  654:     my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
  655:     my ($linkname,$pubname);
  656:     if ($name eq '') {
  657:         $name = 'lonID';
  658:         $linkname = 'lonLinkID';
  659:         $pubname = 'lonPubID';
  660:     }
  661:     my $lonid=$cookies{$name};
  662:     if (!$lonid) {
  663:         if (($name eq 'lonID') && ($ENV{'SERVER_PORT'} != 443) && ($linkname)) {
  664:             $lonid=$cookies{$linkname};
  665:         }
  666:         if (!$lonid) {
  667:             if (($name eq 'lonID') && ($pubname)) {
  668:                 $lonid=$cookies{$pubname};
  669:             }
  670:         }
  671:     }
  672:     return undef if (!$lonid);
  673: 
  674:     my $handle=&LONCAPA::clean_handle($lonid->value);
  675:     my $lonidsdir;
  676:     if ($name eq 'lonDAV') {
  677:         $lonidsdir=$r->dir_config('lonDAVsessDir');
  678:     } else {
  679:         $lonidsdir=$r->dir_config('lonIDsDir');
  680:     }
  681:     if (!-e "$lonidsdir/$handle.id") {
  682:         if ((ref($domref)) && ($name eq 'lonID') && 
  683:             ($handle =~ /^($match_username)\_\d+\_($match_domain)\_(.+)$/)) {
  684:             my ($possuname,$possudom,$possuhome) = ($1,$2,$3);
  685:             if ((&domain($possudom) ne '') && (&homeserver($possuname,$possudom) eq $possuhome)) {
  686:                 $$domref = $possudom;
  687:             }
  688:         }
  689:         return undef;
  690:     }
  691: 
  692:     my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
  693:     return undef if (!$opened);
  694: 
  695:     flock($idf,LOCK_SH);
  696:     my %disk_env;
  697:     if (!tie(%disk_env,'GDBM_File',"$lonidsdir/$handle.id",
  698: 	    &GDBM_READER(),0640)) {
  699: 	return undef;	
  700:     }
  701: 
  702:     if (!defined($disk_env{'user.name'})
  703: 	|| !defined($disk_env{'user.domain'})) {
  704: 	return undef;
  705:     }
  706: 
  707:     if (ref($userhashref) eq 'HASH') {
  708:         $userhashref->{'name'} = $disk_env{'user.name'};
  709:         $userhashref->{'domain'} = $disk_env{'user.domain'};
  710:     }
  711: 
  712:     return $handle;
  713: }
  714: 
  715: sub timed_flock {
  716:     my ($file,$lock_type) = @_;
  717:     my $failed=0;
  718:     eval {
  719: 	local $SIG{__DIE__}='DEFAULT';
  720: 	local $SIG{ALRM}=sub {
  721: 	    $failed=1;
  722: 	    die("failed lock");
  723: 	};
  724: 	alarm(13);
  725: 	flock($file,$lock_type);
  726: 	alarm(0);
  727:     };
  728:     if ($failed) {
  729: 	return undef;
  730:     } else {
  731: 	return 1;
  732:     }
  733: }
  734: 
  735: # ---------------------------------------------------------- Append Environment
  736: 
  737: sub appenv {
  738:     my ($newenv,$roles) = @_;
  739:     if (ref($newenv) eq 'HASH') {
  740:         foreach my $key (keys(%{$newenv})) {
  741:             my $refused = 0;
  742: 	    if (($key =~ /^user\.role/) || ($key =~ /^user\.priv/)) {
  743:                 $refused = 1;
  744:                 if (ref($roles) eq 'ARRAY') {
  745:                     my ($type,$role) = ($key =~ m{^user\.(role|priv)\.(.+?)\./});
  746:                     if (grep(/^\Q$role\E$/,@{$roles})) {
  747:                         $refused = 0;
  748:                     }
  749:                 }
  750:             }
  751:             if ($refused) {
  752:                 &logthis("<font color=\"blue\">WARNING: ".
  753:                          "Attempt to modify environment ".$key." to ".$newenv->{$key}
  754:                          .'</font>');
  755: 	        delete($newenv->{$key});
  756:             } else {
  757:                 $env{$key}=$newenv->{$key};
  758:             }
  759:         }
  760:         my $opened = open(my $env_file,'+<',$env{'user.environment'});
  761:         if ($opened
  762: 	    && &timed_flock($env_file,LOCK_EX)
  763: 	    &&
  764: 	    tie(my %disk_env,'GDBM_File',$env{'user.environment'},
  765: 	        (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
  766: 	    while (my ($key,$value) = each(%{$newenv})) {
  767: 	        $disk_env{$key} = $value;
  768: 	    }
  769: 	    untie(%disk_env);
  770:         }
  771:     }
  772:     return 'ok';
  773: }
  774: # ----------------------------------------------------- Delete from Environment
  775: 
  776: sub delenv {
  777:     my ($delthis,$regexp,$roles) = @_;
  778:     if (($delthis=~/^user\.role/) || ($delthis=~/^user\.priv/)) {
  779:         my $refused = 1;
  780:         if (ref($roles) eq 'ARRAY') {
  781:             my ($type,$role) = ($delthis =~ /^user\.(role|priv)\.([^.]+)\./);
  782:             if (grep(/^\Q$role\E$/,@{$roles})) {
  783:                 $refused = 0;
  784:             }
  785:         }
  786:         if ($refused) {
  787:             &logthis("<font color=\"blue\">WARNING: ".
  788:                      "Attempt to delete from environment ".$delthis);
  789:             return 'error';
  790:         }
  791:     }
  792:     my $opened = open(my $env_file,'+<',$env{'user.environment'});
  793:     if ($opened
  794: 	&& &timed_flock($env_file,LOCK_EX)
  795: 	&&
  796: 	tie(my %disk_env,'GDBM_File',$env{'user.environment'},
  797: 	    (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
  798: 	foreach my $key (keys(%disk_env)) {
  799: 	    if ($regexp) {
  800:                 if ($key=~/^$delthis/) {
  801:                     delete($env{$key});
  802:                     delete($disk_env{$key});
  803:                 } 
  804:             } else {
  805:                 if ($key=~/^\Q$delthis\E/) {
  806: 		    delete($env{$key});
  807: 		    delete($disk_env{$key});
  808: 	        }
  809:             }
  810: 	}
  811: 	untie(%disk_env);
  812:     }
  813:     return 'ok';
  814: }
  815: 
  816: sub get_env_multiple {
  817:     my ($name) = @_;
  818:     my @values;
  819:     if (defined($env{$name})) {
  820:         # exists is it an array
  821:         if (ref($env{$name})) {
  822:             @values=@{ $env{$name} };
  823:         } else {
  824:             $values[0]=$env{$name};
  825:         }
  826:     }
  827:     return(@values);
  828: }
  829: 
  830: # ------------------------------------------------------------------- Locking
  831: 
  832: sub set_lock {
  833:     my ($text)=@_;
  834:     $locknum++;
  835:     my $id=$$.'-'.$locknum;
  836:     &appenv({'session.locks' => $env{'session.locks'}.','.$id,
  837:              'session.lock.'.$id => $text});
  838:     return $id;
  839: }
  840: 
  841: sub get_locks {
  842:     my $num=0;
  843:     my %texts=();
  844:     foreach my $lock (split(/\,/,$env{'session.locks'})) {
  845:        if ($lock=~/\w/) {
  846:           $num++;
  847:           $texts{$lock}=$env{'session.lock.'.$lock};
  848:        }
  849:    }
  850:    return ($num,%texts);
  851: }
  852: 
  853: sub remove_lock {
  854:     my ($id)=@_;
  855:     my $newlocks='';
  856:     foreach my $lock (split(/\,/,$env{'session.locks'})) {
  857:        if (($lock=~/\w/) && ($lock ne $id)) {
  858:           $newlocks.=','.$lock;
  859:        }
  860:     }
  861:     &appenv({'session.locks' => $newlocks});
  862:     &delenv('session.lock.'.$id);
  863: }
  864: 
  865: sub remove_all_locks {
  866:     my $activelocks=$env{'session.locks'};
  867:     foreach my $lock (split(/\,/,$env{'session.locks'})) {
  868:        if ($lock=~/\w/) {
  869:           &remove_lock($lock);
  870:        }
  871:     }
  872: }
  873: 
  874: 
  875: # ------------------------------------------ Find out current server userload
  876: sub userload {
  877:     my $numusers=0;
  878:     {
  879: 	opendir(LONIDS,$perlvar{'lonIDsDir'});
  880: 	my $filename;
  881: 	my $curtime=time;
  882: 	while ($filename=readdir(LONIDS)) {
  883: 	    next if ($filename eq '.' || $filename eq '..');
  884: 	    next if ($filename =~ /publicuser_\d+\.id/);
  885: 	    my ($mtime)=(stat($perlvar{'lonIDsDir'}.'/'.$filename))[9];
  886: 	    if ($curtime-$mtime < 1800) { $numusers++; }
  887: 	}
  888: 	closedir(LONIDS);
  889:     }
  890:     my $userloadpercent=0;
  891:     my $maxuserload=$perlvar{'lonUserLoadLim'};
  892:     if ($maxuserload) {
  893: 	$userloadpercent=100*$numusers/$maxuserload;
  894:     }
  895:     $userloadpercent=sprintf("%.2f",$userloadpercent);
  896:     return $userloadpercent;
  897: }
  898: 
  899: # ------------------------------ Find server with least workload from spare.tab
  900: 
  901: sub spareserver {
  902:     my ($loadpercent,$userloadpercent,$want_server_name,$udom) = @_;
  903:     my $spare_server;
  904:     if ($userloadpercent !~ /\d/) { $userloadpercent=0; }
  905:     my $lowest_load=($loadpercent > $userloadpercent) ? $loadpercent 
  906:                                                      :  $userloadpercent;
  907:     my ($uint_dom,$remotesessions);
  908:     if (($udom ne '') && (&domain($udom) ne '')) {
  909:         my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
  910:         $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
  911:         my %udomdefaults = &Apache::lonnet::get_domain_defaults($udom);
  912:         $remotesessions = $udomdefaults{'remotesessions'};
  913:     }
  914:     my $spareshash = &this_host_spares($udom);
  915:     if (ref($spareshash) eq 'HASH') {
  916:         if (ref($spareshash->{'primary'}) eq 'ARRAY') {
  917:             foreach my $try_server (@{ $spareshash->{'primary'} }) {
  918:                 next unless (&spare_can_host($udom,$uint_dom,$remotesessions,
  919:                                              $try_server));
  920: 	        ($spare_server, $lowest_load) =
  921: 	            &compare_server_load($try_server, $spare_server, $lowest_load);
  922:             }
  923:         }
  924: 
  925:         my $found_server = ($spare_server ne '' && $lowest_load < 100);
  926: 
  927:         if (!$found_server) {
  928:             if (ref($spareshash->{'default'}) eq 'ARRAY') { 
  929: 	        foreach my $try_server (@{ $spareshash->{'default'} }) {
  930:                     next unless (&spare_can_host($udom,$uint_dom,
  931:                                                  $remotesessions,$try_server));
  932: 	            ($spare_server, $lowest_load) =
  933: 		        &compare_server_load($try_server, $spare_server, $lowest_load);
  934:                 }
  935: 	    }
  936:         }
  937:     }
  938: 
  939:     if (!$want_server_name) {
  940:         my $protocol = 'http';
  941:         if ($protocol{$spare_server} eq 'https') {
  942:             $protocol = $protocol{$spare_server};
  943:         }
  944:         if (defined($spare_server)) {
  945:             my $hostname = &hostname($spare_server);
  946:             if (defined($hostname)) {
  947: 	        $spare_server = $protocol.'://'.$hostname;
  948:             }
  949:         }
  950:     }
  951:     return $spare_server;
  952: }
  953: 
  954: sub compare_server_load {
  955:     my ($try_server, $spare_server, $lowest_load, $required) = @_;
  956: 
  957:     if ($required) {
  958:         my ($reqdmajor,$reqdminor) = ($required =~ /^(\d+)\.(\d+)$/);
  959:         my $remoterev = &get_server_loncaparev(undef,$try_server);
  960:         my ($major,$minor) = ($remoterev =~ /^\'?(\d+)\.(\d+)\.[\w.\-]+\'?$/);
  961:         if (($major eq '' && $minor eq '') ||
  962:             (($reqdmajor > $major) || (($reqdmajor == $major) && ($reqdminor > $minor)))) {
  963:             return ($spare_server,$lowest_load);
  964:         }
  965:     }
  966: 
  967:     my $loadans     = &reply('load',    $try_server);
  968:     my $userloadans = &reply('userload',$try_server);
  969: 
  970:     if ($loadans !~ /\d/ && $userloadans !~ /\d/) {
  971: 	return ($spare_server, $lowest_load); #didn't get a number from the server
  972:     }
  973: 
  974:     my $load;
  975:     if ($loadans =~ /\d/) {
  976: 	if ($userloadans =~ /\d/) {
  977: 	    #both are numbers, pick the bigger one
  978: 	    $load = ($loadans > $userloadans) ? $loadans 
  979: 		                              : $userloadans;
  980: 	} else {
  981: 	    $load = $loadans;
  982: 	}
  983:     } else {
  984: 	$load = $userloadans;
  985:     }
  986: 
  987:     if (($load =~ /\d/) && ($load < $lowest_load)) {
  988: 	$spare_server = $try_server;
  989: 	$lowest_load  = $load;
  990:     }
  991:     return ($spare_server,$lowest_load);
  992: }
  993: 
  994: # --------------------------- ask offload servers if user already has a session
  995: sub find_existing_session {
  996:     my ($udom,$uname) = @_;
  997:     my $spareshash = &this_host_spares($udom);
  998:     if (ref($spareshash) eq 'HASH') {
  999:         if (ref($spareshash->{'primary'}) eq 'ARRAY') {
 1000:             foreach my $try_server (@{ $spareshash->{'primary'} }) {
 1001:                 return $try_server if (&has_user_session($try_server, $udom, $uname));
 1002:             }
 1003:         }
 1004:         if (ref($spareshash->{'default'}) eq 'ARRAY') {
 1005:             foreach my $try_server (@{ $spareshash->{'default'} }) {
 1006:                 return $try_server if (&has_user_session($try_server, $udom, $uname));
 1007:             }
 1008:         }
 1009:     }
 1010:     return;
 1011: }
 1012: 
 1013: # -------------------------------- ask if server already has a session for user
 1014: sub has_user_session {
 1015:     my ($lonid,$udom,$uname) = @_;
 1016:     my $result = &reply(join(':','userhassession',
 1017: 			     map {&escape($_)} ($udom,$uname)),$lonid);
 1018:     return 1 if ($result eq 'ok');
 1019: 
 1020:     return 0;
 1021: }
 1022: 
 1023: # --------- determine least loaded server in a user's domain which allows login
 1024: 
 1025: sub choose_server {
 1026:     my ($udom,$checkloginvia,$required,$skiploadbal) = @_;
 1027:     my %domconfhash = &Apache::loncommon::get_domainconf($udom);
 1028:     my %servers = &get_servers($udom);
 1029:     my $lowest_load = 30000;
 1030:     my ($login_host,$hostname,$portal_path,$isredirect,$balancers);
 1031:     if ($skiploadbal) {
 1032:         ($balancers,my $cached)=&is_cached_new('loadbalancing',$udom);
 1033:         unless (defined($cached)) {
 1034:             my $cachetime = 60*60*24;
 1035:             my %domconfig =
 1036:                 &Apache::lonnet::get_dom('configuration',['loadbalancing'],$udom);
 1037:             if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 1038:                 $balancers = &do_cache_new('loadbalancing',$udom,$domconfig{'loadbalancing'},
 1039:                                            $cachetime);
 1040:             }
 1041:         }
 1042:     }
 1043:     foreach my $lonhost (keys(%servers)) {
 1044:         if ($skiploadbal) {
 1045:             if (ref($balancers) eq 'HASH') {
 1046:                 next if (exists($balancers->{$lonhost}));
 1047:             }
 1048:         }   
 1049:         my $loginvia;
 1050:         if ($checkloginvia) {
 1051:             $loginvia = $domconfhash{$udom.'.login.loginvia_'.$lonhost};
 1052:             if ($loginvia) {
 1053:                 my ($server,$path) = split(/:/,$loginvia);
 1054:                 ($login_host, $lowest_load) =
 1055:                     &compare_server_load($server, $login_host, $lowest_load, $required);
 1056:                 if ($login_host eq $server) {
 1057:                     $portal_path = $path;
 1058:                     $isredirect = 1;
 1059:                 }
 1060:             } else {
 1061:                 ($login_host, $lowest_load) =
 1062:                     &compare_server_load($lonhost, $login_host, $lowest_load, $required);
 1063:                 if ($login_host eq $lonhost) {
 1064:                     $portal_path = '';
 1065:                     $isredirect = ''; 
 1066:                 }
 1067:             }
 1068:         } else {
 1069:             ($login_host, $lowest_load) =
 1070:                 &compare_server_load($lonhost, $login_host, $lowest_load, $required);
 1071:         }
 1072:     }
 1073:     if ($login_host ne '') {
 1074:         $hostname = &hostname($login_host);
 1075:     }
 1076:     return ($login_host,$hostname,$portal_path,$isredirect,$lowest_load);
 1077: }
 1078: 
 1079: # --------------------------------------------- Try to change a user's password
 1080: 
 1081: sub changepass {
 1082:     my ($uname,$udom,$currentpass,$newpass,$server,$context)=@_;
 1083:     $currentpass = &escape($currentpass);
 1084:     $newpass     = &escape($newpass);
 1085:     my $lonhost = $perlvar{'lonHostID'};
 1086:     my $answer = reply("encrypt:passwd:$udom:$uname:$currentpass:$newpass:$context:$lonhost",
 1087: 		       $server);
 1088:     if (! $answer) {
 1089: 	&logthis("No reply on password change request to $server ".
 1090: 		 "by $uname in domain $udom.");
 1091:     } elsif ($answer =~ "^ok") {
 1092:         &logthis("$uname in $udom successfully changed their password ".
 1093: 		 "on $server.");
 1094:     } elsif ($answer =~ "^pwchange_failure") {
 1095: 	&logthis("$uname in $udom was unable to change their password ".
 1096: 		 "on $server.  The action was blocked by either lcpasswd ".
 1097: 		 "or pwchange");
 1098:     } elsif ($answer =~ "^non_authorized") {
 1099:         &logthis("$uname in $udom did not get their password correct when ".
 1100: 		 "attempting to change it on $server.");
 1101:     } elsif ($answer =~ "^auth_mode_error") {
 1102:         &logthis("$uname in $udom attempted to change their password despite ".
 1103: 		 "not being locally or internally authenticated on $server.");
 1104:     } elsif ($answer =~ "^unknown_user") {
 1105:         &logthis("$uname in $udom attempted to change their password ".
 1106: 		 "on $server but were unable to because $server is not ".
 1107: 		 "their home server.");
 1108:     } elsif ($answer =~ "^refused") {
 1109: 	&logthis("$server refused to change $uname in $udom password because ".
 1110: 		 "it was sent an unencrypted request to change the password.");
 1111:     } elsif ($answer =~ "invalid_client") {
 1112:         &logthis("$server refused to change $uname in $udom password because ".
 1113:                  "it was a reset by e-mail originating from an invalid server.");
 1114:     }
 1115:     return $answer;
 1116: }
 1117: 
 1118: # ----------------------- Try to determine user's current authentication scheme
 1119: 
 1120: sub queryauthenticate {
 1121:     my ($uname,$udom)=@_;
 1122:     my $uhome=&homeserver($uname,$udom);
 1123:     if (!$uhome) {
 1124: 	&logthis("User $uname at $udom is unknown when looking for authentication mechanism");
 1125: 	return 'no_host';
 1126:     }
 1127:     my $answer=reply("encrypt:currentauth:$udom:$uname",$uhome);
 1128:     if ($answer =~ /^(unknown_user|refused|con_lost)/) {
 1129: 	&logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
 1130:     }
 1131:     return $answer;
 1132: }
 1133: 
 1134: # --------- Try to authenticate user from domain's lib servers (first this one)
 1135: 
 1136: sub authenticate {
 1137:     my ($uname,$upass,$udom,$checkdefauth,$clientcancheckhost)=@_;
 1138:     $upass=&escape($upass);
 1139:     $uname= &LONCAPA::clean_username($uname);
 1140:     my $uhome=&homeserver($uname,$udom,1);
 1141:     my $newhome;
 1142:     if ((!$uhome) || ($uhome eq 'no_host')) {
 1143: # Maybe the machine was offline and only re-appeared again recently?
 1144:         &reconlonc();
 1145: # One more
 1146: 	$uhome=&homeserver($uname,$udom,1);
 1147:         if (($uhome eq 'no_host') && $checkdefauth) {
 1148:             if (defined(&domain($udom,'primary'))) {
 1149:                 $newhome=&domain($udom,'primary');
 1150:             }
 1151:             if ($newhome ne '') {
 1152:                 $uhome = $newhome;
 1153:             }
 1154:         }
 1155: 	if ((!$uhome) || ($uhome eq 'no_host')) {
 1156: 	    &logthis("User $uname at $udom is unknown in authenticate");
 1157: 	    return 'no_host';
 1158:         }
 1159:     }
 1160:     my $answer=reply("encrypt:auth:$udom:$uname:$upass:$checkdefauth:$clientcancheckhost",$uhome);
 1161:     if ($answer eq 'authorized') {
 1162:         if ($newhome) {
 1163:             &logthis("User $uname at $udom authorized by $uhome, but needs account");
 1164:             return 'no_account_on_host'; 
 1165:         } else {
 1166:             &logthis("User $uname at $udom authorized by $uhome");
 1167:             return $uhome;
 1168:         }
 1169:     }
 1170:     if ($answer eq 'non_authorized') {
 1171: 	&logthis("User $uname at $udom rejected by $uhome");
 1172: 	return 'no_host'; 
 1173:     }
 1174:     &logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
 1175:     return 'no_host';
 1176: }
 1177: 
 1178: sub can_host_session {
 1179:     my ($udom,$lonhost,$remoterev,$remotesessions,$hostedsessions) = @_;
 1180:     my $canhost = 1;
 1181:     my $host_idn = &Apache::lonnet::internet_dom($lonhost);
 1182:     if (ref($remotesessions) eq 'HASH') {
 1183:         if (ref($remotesessions->{'excludedomain'}) eq 'ARRAY') {
 1184:             if (grep(/^\Q$host_idn\E$/,@{$remotesessions->{'excludedomain'}})) {
 1185:                 $canhost = 0;
 1186:             } else {
 1187:                 $canhost = 1;
 1188:             }
 1189:         }
 1190:         if (ref($remotesessions->{'includedomain'}) eq 'ARRAY') {
 1191:             if (grep(/^\Q$host_idn\E$/,@{$remotesessions->{'includedomain'}})) {
 1192:                 $canhost = 1;
 1193:             } else {
 1194:                 $canhost = 0;
 1195:             }
 1196:         }
 1197:         if ($canhost) {
 1198:             if ($remotesessions->{'version'} ne '') {
 1199:                 my ($reqmajor,$reqminor) = ($remotesessions->{'version'} =~ /^(\d+)\.(\d+)$/);
 1200:                 if ($reqmajor ne '' && $reqminor ne '') {
 1201:                     if ($remoterev =~ /^\'?(\d+)\.(\d+)/) {
 1202:                         my $major = $1;
 1203:                         my $minor = $2;
 1204:                         if (($major < $reqmajor ) ||
 1205:                             (($major == $reqmajor) && ($minor < $reqminor))) {
 1206:                             $canhost = 0;
 1207:                         }
 1208:                     } else {
 1209:                         $canhost = 0;
 1210:                     }
 1211:                 }
 1212:             }
 1213:         }
 1214:     }
 1215:     if ($canhost) {
 1216:         if (ref($hostedsessions) eq 'HASH') {
 1217:             my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
 1218:             my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 1219:             if (ref($hostedsessions->{'excludedomain'}) eq 'ARRAY') {
 1220:                 if (($uint_dom ne '') && 
 1221:                     (grep(/^\Q$uint_dom\E$/,@{$hostedsessions->{'excludedomain'}}))) {
 1222:                     $canhost = 0;
 1223:                 } else {
 1224:                     $canhost = 1;
 1225:                 }
 1226:             }
 1227:             if (ref($hostedsessions->{'includedomain'}) eq 'ARRAY') {
 1228:                 if (($uint_dom ne '') && 
 1229:                     (grep(/^\Q$uint_dom\E$/,@{$hostedsessions->{'includedomain'}}))) {
 1230:                     $canhost = 1;
 1231:                 } else {
 1232:                     $canhost = 0;
 1233:                 }
 1234:             }
 1235:         }
 1236:     }
 1237:     return $canhost;
 1238: }
 1239: 
 1240: sub spare_can_host {
 1241:     my ($udom,$uint_dom,$remotesessions,$try_server)=@_;
 1242:     my $canhost=1;
 1243:     my $try_server_hostname = &hostname($try_server);
 1244:     my $serverhomeID = &get_server_homeID($try_server_hostname);
 1245:     my $serverhomedom = &host_domain($serverhomeID);
 1246:     my %defdomdefaults = &get_domain_defaults($serverhomedom);
 1247:     if (ref($defdomdefaults{'offloadnow'}) eq 'HASH') {
 1248:         if ($defdomdefaults{'offloadnow'}{$try_server}) {
 1249:             $canhost = 0;
 1250:         }
 1251:     }
 1252:     if (($canhost) && ($uint_dom)) {
 1253:         my @intdoms;
 1254:         my $internet_names = &get_internet_names($try_server);
 1255:         if (ref($internet_names) eq 'ARRAY') {
 1256:             @intdoms = @{$internet_names};
 1257:         }
 1258:         unless (grep(/^\Q$uint_dom\E$/,@intdoms)) {
 1259:             my $remoterev = &get_server_loncaparev(undef,$try_server);
 1260:             $canhost = &can_host_session($udom,$try_server,$remoterev,
 1261:                                          $remotesessions,
 1262:                                          $defdomdefaults{'hostedsessions'});
 1263:         }
 1264:     }
 1265:     return $canhost;
 1266: }
 1267: 
 1268: sub this_host_spares {
 1269:     my ($dom) = @_;
 1270:     my ($dom_in_use,$lonhost_in_use,$result);
 1271:     my @hosts = &current_machine_ids();
 1272:     foreach my $lonhost (@hosts) {
 1273:         if (&host_domain($lonhost) eq $dom) {
 1274:             $dom_in_use = $dom;
 1275:             $lonhost_in_use = $lonhost;
 1276:             last;
 1277:         }
 1278:     }
 1279:     if ($dom_in_use ne '') {
 1280:         $result = &spares_for_offload($dom_in_use,$lonhost_in_use);
 1281:     }
 1282:     if (ref($result) ne 'HASH') {
 1283:         $lonhost_in_use = $perlvar{'lonHostID'};
 1284:         $dom_in_use = &host_domain($lonhost_in_use);
 1285:         $result = &spares_for_offload($dom_in_use,$lonhost_in_use);
 1286:         if (ref($result) ne 'HASH') {
 1287:             $result = \%spareid;
 1288:         }
 1289:     }
 1290:     return $result;
 1291: }
 1292: 
 1293: sub spares_for_offload  {
 1294:     my ($dom_in_use,$lonhost_in_use) = @_;
 1295:     my ($result,$cached)=&is_cached_new('spares',$dom_in_use);
 1296:     if (defined($cached)) {
 1297:         return $result;
 1298:     } else {
 1299:         my $cachetime = 60*60*24;
 1300:         my %domconfig =
 1301:             &Apache::lonnet::get_dom('configuration',['usersessions'],$dom_in_use);
 1302:         if (ref($domconfig{'usersessions'}) eq 'HASH') {
 1303:             if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
 1304:                 if (ref($domconfig{'usersessions'}{'spares'}{$lonhost_in_use}) eq 'HASH') {
 1305:                     return &do_cache_new('spares',$dom_in_use,$domconfig{'usersessions'}{'spares'}{$lonhost_in_use},$cachetime);
 1306:                 }
 1307:             }
 1308:         }
 1309:     }
 1310:     return;
 1311: }
 1312: 
 1313: sub get_lonbalancer_config {
 1314:     my ($servers) = @_;
 1315:     my ($currbalancer,$currtargets);
 1316:     if (ref($servers) eq 'HASH') {
 1317:         foreach my $server (keys(%{$servers})) {
 1318:             my %what = (
 1319:                          spareid => 1,
 1320:                          perlvar => 1,
 1321:                        );
 1322:             my ($result,$returnhash) = &get_remote_globals($server,\%what);
 1323:             if ($result eq 'ok') {
 1324:                 if (ref($returnhash) eq 'HASH') {
 1325:                     if (ref($returnhash->{'perlvar'}) eq 'HASH') {
 1326:                         if ($returnhash->{'perlvar'}->{'lonBalancer'} eq 'yes') {
 1327:                             $currbalancer = $server;
 1328:                             $currtargets = {};
 1329:                             if (ref($returnhash->{'spareid'}) eq 'HASH') {
 1330:                                 if (ref($returnhash->{'spareid'}->{'primary'}) eq 'ARRAY') {
 1331:                                     $currtargets->{'primary'} = $returnhash->{'spareid'}->{'primary'};
 1332:                                 }
 1333:                                 if (ref($returnhash->{'spareid'}->{'default'}) eq 'ARRAY') {
 1334:                                     $currtargets->{'default'} = $returnhash->{'spareid'}->{'default'};
 1335:                                 }
 1336:                             }
 1337:                             last;
 1338:                         }
 1339:                     }
 1340:                 }
 1341:             }
 1342:         }
 1343:     }
 1344:     return ($currbalancer,$currtargets);
 1345: }
 1346: 
 1347: sub check_loadbalancing {
 1348:     my ($uname,$udom,$caller) = @_;
 1349:     my ($is_balancer,$currtargets,$currrules,$dom_in_use,$homeintdom,
 1350:         $rule_in_effect,$offloadto,$otherserver);
 1351:     my $lonhost = $perlvar{'lonHostID'};
 1352:     my @hosts = &current_machine_ids();
 1353:     my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
 1354:     my $uintdom = &Apache::lonnet::internet_dom($uprimary_id);
 1355:     my $intdom = &Apache::lonnet::internet_dom($lonhost);
 1356:     my $serverhomedom = &host_domain($lonhost);
 1357:     my $domneedscache;
 1358:     my $cachetime = 60*60*24;
 1359: 
 1360:     if (($uintdom ne '') && ($uintdom eq $intdom)) {
 1361:         $dom_in_use = $udom;
 1362:         $homeintdom = 1;
 1363:     } else {
 1364:         $dom_in_use = $serverhomedom;
 1365:     }
 1366:     my ($result,$cached)=&is_cached_new('loadbalancing',$dom_in_use);
 1367:     unless (defined($cached)) {
 1368:         my %domconfig =
 1369:             &Apache::lonnet::get_dom('configuration',['loadbalancing'],$dom_in_use);
 1370:         if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 1371:             $result = &do_cache_new('loadbalancing',$dom_in_use,$domconfig{'loadbalancing'},$cachetime);
 1372:         } else {
 1373:             $domneedscache = $dom_in_use;
 1374:         }
 1375:     }
 1376:     if (ref($result) eq 'HASH') {
 1377:         ($is_balancer,$currtargets,$currrules) = 
 1378:             &check_balancer_result($result,@hosts);
 1379:         if ($is_balancer) {
 1380:             if (ref($currrules) eq 'HASH') {
 1381:                 if ($homeintdom) {
 1382:                     if ($uname ne '') {
 1383:                         if (($currrules->{'_LC_adv'} ne '') || ($currrules->{'_LC_author'} ne '')) {
 1384:                             my ($is_adv,$is_author) = &is_advanced_user($udom,$uname);
 1385:                             if (($currrules->{'_LC_author'} ne '') && ($is_author)) {
 1386:                                 $rule_in_effect = $currrules->{'_LC_author'};
 1387:                             } elsif (($currrules->{'_LC_adv'} ne '') && ($is_adv)) {
 1388:                                 $rule_in_effect = $currrules->{'_LC_adv'}
 1389:                             }
 1390:                         }
 1391:                         if ($rule_in_effect eq '') {
 1392:                             my %userenv = &userenvironment($udom,$uname,'inststatus');
 1393:                             if ($userenv{'inststatus'} ne '') {
 1394:                                 my @statuses = map { &unescape($_); } split(/:/,$userenv{'inststatus'});
 1395:                                 my ($othertitle,$usertypes,$types) =
 1396:                                     &Apache::loncommon::sorted_inst_types($udom);
 1397:                                 if (ref($types) eq 'ARRAY') {
 1398:                                     foreach my $type (@{$types}) {
 1399:                                         if (grep(/^\Q$type\E$/,@statuses)) {
 1400:                                             if (exists($currrules->{$type})) {
 1401:                                                 $rule_in_effect = $currrules->{$type};
 1402:                                             }
 1403:                                         }
 1404:                                     }
 1405:                                 }
 1406:                             } else {
 1407:                                 if (exists($currrules->{'default'})) {
 1408:                                     $rule_in_effect = $currrules->{'default'};
 1409:                                 }
 1410:                             }
 1411:                         }
 1412:                     } else {
 1413:                         if (exists($currrules->{'default'})) {
 1414:                             $rule_in_effect = $currrules->{'default'};
 1415:                         }
 1416:                     }
 1417:                 } else {
 1418:                     if ($currrules->{'_LC_external'} ne '') {
 1419:                         $rule_in_effect = $currrules->{'_LC_external'};
 1420:                     }
 1421:                 }
 1422:                 $offloadto = &get_loadbalancer_targets($rule_in_effect,$currtargets,
 1423:                                                        $uname,$udom);
 1424:             }
 1425:         }
 1426:     } elsif (($homeintdom) && ($udom ne $serverhomedom)) {
 1427:         ($result,$cached)=&is_cached_new('loadbalancing',$serverhomedom);
 1428:         unless (defined($cached)) {
 1429:             my %domconfig =
 1430:                 &Apache::lonnet::get_dom('configuration',['loadbalancing'],$serverhomedom);
 1431:             if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 1432:                 $result = &do_cache_new('loadbalancing',$serverhomedom,$domconfig{'loadbalancing'},$cachetime);
 1433:             } else {
 1434:                 $domneedscache = $serverhomedom;
 1435:             }
 1436:         }
 1437:         if (ref($result) eq 'HASH') {
 1438:             ($is_balancer,$currtargets,$currrules) = 
 1439:                 &check_balancer_result($result,@hosts);
 1440:             if ($is_balancer) {
 1441:                 if (ref($currrules) eq 'HASH') {
 1442:                     if ($currrules->{'_LC_internetdom'} ne '') {
 1443:                         $rule_in_effect = $currrules->{'_LC_internetdom'};
 1444:                     }
 1445:                 }
 1446:                 $offloadto = &get_loadbalancer_targets($rule_in_effect,$currtargets,
 1447:                                                        $uname,$udom);
 1448:             }
 1449:         } else {
 1450:             if ($perlvar{'lonBalancer'} eq 'yes') {
 1451:                 $is_balancer = 1;
 1452:                 $offloadto = &this_host_spares($dom_in_use);
 1453:             }
 1454:             unless (defined($cached)) {
 1455:                 $domneedscache = $serverhomedom;
 1456:             }
 1457:         }
 1458:     } else {
 1459:         if ($perlvar{'lonBalancer'} eq 'yes') {
 1460:             $is_balancer = 1;
 1461:             $offloadto = &this_host_spares($dom_in_use);
 1462:         }
 1463:         unless (defined($cached)) {
 1464:             $domneedscache = $serverhomedom;
 1465:         }
 1466:     }
 1467:     if ($domneedscache) {
 1468:         &do_cache_new('loadbalancing',$domneedscache,$is_balancer,$cachetime);
 1469:     }
 1470:     if ($is_balancer) {
 1471:         my $lowest_load = 30000;
 1472:         if (ref($offloadto) eq 'HASH') {
 1473:             if (ref($offloadto->{'primary'}) eq 'ARRAY') {
 1474:                 foreach my $try_server (@{$offloadto->{'primary'}}) {
 1475:                     ($otherserver,$lowest_load) =
 1476:                         &compare_server_load($try_server,$otherserver,$lowest_load);
 1477:                 }
 1478:             }
 1479:             my $found_server = ($otherserver ne '' && $lowest_load < 100);
 1480: 
 1481:             if (!$found_server) {
 1482:                 if (ref($offloadto->{'default'}) eq 'ARRAY') {
 1483:                     foreach my $try_server (@{$offloadto->{'default'}}) {
 1484:                         ($otherserver,$lowest_load) =
 1485:                             &compare_server_load($try_server,$otherserver,$lowest_load);
 1486:                     }
 1487:                 }
 1488:             }
 1489:         } elsif (ref($offloadto) eq 'ARRAY') {
 1490:             if (@{$offloadto} == 1) {
 1491:                 $otherserver = $offloadto->[0];
 1492:             } elsif (@{$offloadto} > 1) {
 1493:                 foreach my $try_server (@{$offloadto}) {
 1494:                     ($otherserver,$lowest_load) =
 1495:                         &compare_server_load($try_server,$otherserver,$lowest_load);
 1496:                 }
 1497:             }
 1498:         }
 1499:         unless ($caller eq 'login') {
 1500:             if (($otherserver ne '') && (grep(/^\Q$otherserver\E$/,@hosts))) {
 1501:                 $is_balancer = 0;
 1502:                 if ($uname ne '' && $udom ne '') {
 1503:                     if (($env{'user.name'} eq $uname) && ($env{'user.domain'} eq $udom)) {
 1504:                     
 1505:                         &appenv({'user.loadbalexempt'     => $lonhost,  
 1506:                                  'user.loadbalcheck.time' => time});
 1507:                     }
 1508:                 }
 1509:             }
 1510:         }
 1511:     }
 1512:     return ($is_balancer,$otherserver);
 1513: }
 1514: 
 1515: sub check_balancer_result {
 1516:     my ($result,@hosts) = @_;
 1517:     my ($is_balancer,$currtargets,$currrules);
 1518:     if (ref($result) eq 'HASH') {
 1519:         if ($result->{'lonhost'} ne '') {
 1520:             my $currbalancer = $result->{'lonhost'};
 1521:             if (grep(/^\Q$currbalancer\E$/,@hosts)) {
 1522:                 $is_balancer = 1;
 1523:                 $currtargets = $result->{'targets'};
 1524:                 $currrules = $result->{'rules'};
 1525:             }
 1526:         } else {
 1527:             foreach my $key (keys(%{$result})) {
 1528:                 if (($key ne '') && (grep(/^\Q$key\E$/,@hosts)) &&
 1529:                     (ref($result->{$key}) eq 'HASH')) {
 1530:                     $is_balancer = 1;
 1531:                     $currrules = $result->{$key}{'rules'};
 1532:                     $currtargets = $result->{$key}{'targets'};
 1533:                     last;
 1534:                 }
 1535:             }
 1536:         }
 1537:     }
 1538:     return ($is_balancer,$currtargets,$currrules);
 1539: }
 1540: 
 1541: sub get_loadbalancer_targets {
 1542:     my ($rule_in_effect,$currtargets,$uname,$udom) = @_;
 1543:     my $offloadto;
 1544:     if ($rule_in_effect eq 'none') {
 1545:         return [$perlvar{'lonHostID'}];
 1546:     } elsif ($rule_in_effect eq '') {
 1547:         $offloadto = $currtargets;
 1548:     } else {
 1549:         if ($rule_in_effect eq 'homeserver') {
 1550:             my $homeserver = &homeserver($uname,$udom);
 1551:             if ($homeserver ne 'no_host') {
 1552:                 $offloadto = [$homeserver];
 1553:             }
 1554:         } elsif ($rule_in_effect eq 'externalbalancer') {
 1555:             my %domconfig =
 1556:                 &Apache::lonnet::get_dom('configuration',['loadbalancing'],$udom);
 1557:             if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 1558:                 if ($domconfig{'loadbalancing'}{'lonhost'} ne '') {
 1559:                     if (&hostname($domconfig{'loadbalancing'}{'lonhost'}) ne '') {
 1560:                         $offloadto = [$domconfig{'loadbalancing'}{'lonhost'}];
 1561:                     }
 1562:                 }
 1563:             } else {
 1564:                 my %servers = &internet_dom_servers($udom);
 1565:                 my ($remotebalancer,$remotetargets) = &get_lonbalancer_config(\%servers);
 1566:                 if (&hostname($remotebalancer) ne '') {
 1567:                     $offloadto = [$remotebalancer];
 1568:                 }
 1569:             }
 1570:         } elsif (&hostname($rule_in_effect) ne '') {
 1571:             $offloadto = [$rule_in_effect];
 1572:         }
 1573:     }
 1574:     return $offloadto;
 1575: }
 1576: 
 1577: sub internet_dom_servers {
 1578:     my ($dom) = @_;
 1579:     my (%uniqservers,%servers);
 1580:     my $primaryserver = &hostname(&domain($dom,'primary'));
 1581:     my @machinedoms = &machine_domains($primaryserver);
 1582:     foreach my $mdom (@machinedoms) {
 1583:         my %currservers = %servers;
 1584:         my %server = &get_servers($mdom);
 1585:         %servers = (%currservers,%server);
 1586:     }
 1587:     my %by_hostname;
 1588:     foreach my $id (keys(%servers)) {
 1589:         push(@{$by_hostname{$servers{$id}}},$id);
 1590:     }
 1591:     foreach my $hostname (sort(keys(%by_hostname))) {
 1592:         if (@{$by_hostname{$hostname}} > 1) {
 1593:             my $match = 0;
 1594:             foreach my $id (@{$by_hostname{$hostname}}) {
 1595:                 if (&host_domain($id) eq $dom) {
 1596:                     $uniqservers{$id} = $hostname;
 1597:                     $match = 1;
 1598:                 }
 1599:             }
 1600:             unless ($match) {
 1601:                 $uniqservers{$by_hostname{$hostname}[0]} = $hostname;
 1602:             }
 1603:         } else {
 1604:             $uniqservers{$by_hostname{$hostname}[0]} = $hostname;
 1605:         }
 1606:     }
 1607:     return %uniqservers;
 1608: }
 1609: 
 1610: sub trusted_domains {
 1611:     my ($cmdtype,$calldom) = @_;
 1612:     my ($trusted,$untrusted);
 1613:     if (&domain($calldom) eq '') {
 1614:         return ($trusted,$untrusted);
 1615:     }
 1616:     unless ($cmdtype =~ /^(content|shared|enroll|coaurem|domroles|catalog|reqcrs|msg)$/) {
 1617:         return ($trusted,$untrusted);
 1618:     }
 1619:     my $callprimary = &domain($calldom,'primary');
 1620:     my $intcalldom = &Apache::lonnet::internet_dom($callprimary);
 1621:     if ($intcalldom eq '') {
 1622:         return ($trusted,$untrusted);
 1623:     }
 1624: 
 1625:     my ($trustconfig,$cached)=&Apache::lonnet::is_cached_new('trust',$calldom);
 1626:     unless (defined($cached)) {
 1627:         my %domconfig = &Apache::lonnet::get_dom('configuration',['trust'],$calldom);
 1628:         &Apache::lonnet::do_cache_new('trust',$calldom,$domconfig{'trust'},3600);
 1629:         $trustconfig = $domconfig{'trust'};
 1630:     }
 1631:     if (ref($trustconfig)) {
 1632:         my (%possexc,%possinc,@allexc,@allinc); 
 1633:         if (ref($trustconfig->{$cmdtype}) eq 'HASH') {
 1634:             if (ref($trustconfig->{$cmdtype}->{'exc'}) eq 'ARRAY') {
 1635:                 map { $possexc{$_} = 1; } @{$trustconfig->{$cmdtype}->{'exc'}}; 
 1636:             }
 1637:             if (ref($trustconfig->{$cmdtype}->{'inc'}) eq 'ARRAY') {
 1638:                 map { $possinc{$_} = 1; } @{$trustconfig->{$cmdtype}->{'inc'}};
 1639:             }
 1640:         }
 1641:         if (keys(%possexc)) {
 1642:             if (keys(%possinc)) {
 1643:                 foreach my $key (sort(keys(%possexc))) {
 1644:                     next if ($key eq $intcalldom);
 1645:                     unless ($possinc{$key}) {
 1646:                         push(@allexc,$key);
 1647:                     }
 1648:                 }
 1649:             } else {
 1650:                 @allexc = sort(keys(%possexc));
 1651:             }
 1652:         }
 1653:         if (keys(%possinc)) {
 1654:             $possinc{$intcalldom} = 1;
 1655:             @allinc = sort(keys(%possinc));
 1656:         }
 1657:         if ((@allexc > 0) || (@allinc > 0)) {
 1658:             my %doms_by_intdom;
 1659:             my %allintdoms = &all_host_intdom();
 1660:             my %alldoms = &all_host_domain();
 1661:             foreach my $key (%allintdoms) {
 1662:                 if (ref($doms_by_intdom{$allintdoms{$key}}) eq 'ARRAY') {
 1663:                     unless (grep(/^\Q$alldoms{$key}\E$/,@{$doms_by_intdom{$allintdoms{$key}}})) {
 1664:                         push(@{$doms_by_intdom{$allintdoms{$key}}},$alldoms{$key});
 1665:                     }
 1666:                 } else {
 1667:                     $doms_by_intdom{$allintdoms{$key}} = [$alldoms{$key}]; 
 1668:                 }
 1669:             }
 1670:             foreach my $exc (@allexc) {
 1671:                 if (ref($doms_by_intdom{$exc}) eq 'ARRAY') {
 1672:                     $untrusted = $doms_by_intdom{$exc};
 1673:                 }
 1674:             }
 1675:             foreach my $inc (@allinc) {
 1676:                 if (ref($doms_by_intdom{$inc}) eq 'ARRAY') {
 1677:                     $trusted = $doms_by_intdom{$inc};
 1678:                 }
 1679:             }
 1680:         }
 1681:     }
 1682:     return ($trusted,$untrusted);
 1683: }
 1684: 
 1685: sub will_trust {
 1686:     my ($cmdtype,$domain,$possdom) = @_;
 1687:     return 1 if ($domain eq $possdom);
 1688:     my ($trustedref,$untrustedref) = &trusted_domains($cmdtype,$possdom);
 1689:     my $willtrust; 
 1690:     if ((ref($trustedref) eq 'ARRAY') && (@{$trustedref} > 0)) {
 1691:         if (grep(/^\Q$domain\E$/,@{$trustedref})) {
 1692:             $willtrust = 1;
 1693:         }
 1694:     } elsif ((ref($untrustedref) eq 'ARRAY') && (@{$untrustedref} > 0)) {
 1695:         unless (grep(/^\Q$domain\E$/,@{$untrustedref})) {
 1696:             $willtrust = 1;
 1697:         }
 1698:     } else {
 1699:         $willtrust = 1;
 1700:     }
 1701:     return $willtrust;
 1702: }
 1703: 
 1704: # ---------------------- Find the homebase for a user from domain's lib servers
 1705: 
 1706: my %homecache;
 1707: sub homeserver {
 1708:     my ($uname,$udom,$ignoreBadCache)=@_;
 1709:     my $index="$uname:$udom";
 1710: 
 1711:     if (exists($homecache{$index})) { return $homecache{$index}; }
 1712: 
 1713:     my %servers = &get_servers($udom,'library');
 1714:     foreach my $tryserver (keys(%servers)) {
 1715:         next if ($ignoreBadCache ne 'true' && 
 1716: 		 exists($badServerCache{$tryserver}));
 1717: 
 1718: 	my $answer=reply("home:$udom:$uname",$tryserver);
 1719: 	if ($answer eq 'found') {
 1720: 	    delete($badServerCache{$tryserver}); 
 1721: 	    return $homecache{$index}=$tryserver;
 1722: 	} elsif ($answer eq 'no_host') {
 1723: 	    $badServerCache{$tryserver}=1;
 1724: 	}
 1725:     }    
 1726:     return 'no_host';
 1727: }
 1728: 
 1729: # ----- Find the usernames behind a list of student/employee IDs or clicker IDs
 1730: 
 1731: sub idget {
 1732:     my ($udom,$idsref,$namespace)=@_;
 1733:     my %returnhash=();
 1734:     my @ids=(); 
 1735:     if (ref($idsref) eq 'ARRAY') {
 1736:         @ids = @{$idsref};
 1737:     } else {
 1738:         return %returnhash; 
 1739:     }
 1740:     if ($namespace eq '') {
 1741:         $namespace = 'ids';
 1742:     }
 1743:     
 1744:     my %servers = &get_servers($udom,'library');
 1745:     foreach my $tryserver (keys(%servers)) {
 1746: 	my $idlist=join('&', map { &escape($_); } @ids);
 1747: 	if ($namespace eq 'ids') {
 1748: 	    $idlist=~tr/A-Z/a-z/;
 1749: 	}
 1750: 	my $reply;
 1751: 	if ($namespace eq 'ids') {
 1752: 	    $reply=&reply("idget:$udom:".$idlist,$tryserver);
 1753: 	} else {
 1754: 	    $reply=&reply("getdom:$udom:$namespace:$idlist",$tryserver);
 1755: 	}
 1756: 	my @answer=();
 1757: 	if (($reply ne 'con_lost') && ($reply!~/^error\:/)) {
 1758: 	    @answer=split(/\&/,$reply);
 1759: 	}                    ;
 1760: 	my $i;
 1761: 	for ($i=0;$i<=$#ids;$i++) {
 1762: 	    if ($answer[$i]) {
 1763: 		$returnhash{$ids[$i]}=&unescape($answer[$i]);
 1764: 	    }
 1765: 	}
 1766:     }
 1767:     return %returnhash;
 1768: }
 1769: 
 1770: # ------------------------------------- Find the IDs behind a list of usernames
 1771: 
 1772: sub idrget {
 1773:     my ($udom,@unames)=@_;
 1774:     my %returnhash=();
 1775:     foreach my $uname (@unames) {
 1776:         $returnhash{$uname}=(&userenvironment($udom,$uname,'id'))[1];
 1777:     }
 1778:     return %returnhash;
 1779: }
 1780: 
 1781: # Store away a list of names and associated student/employee IDs or clicker IDs
 1782: 
 1783: sub idput {
 1784:     my ($udom,$idsref,$uhom,$namespace)=@_;
 1785:     my %servers=();
 1786:     my %ids=();
 1787:     my %byid = ();
 1788:     if (ref($idsref) eq 'HASH') {
 1789:         %ids=%{$idsref};
 1790:     }
 1791:     if ($namespace eq '') {
 1792:         $namespace = 'ids'; 
 1793:     }
 1794:     foreach my $uname (keys(%ids)) {
 1795: 	&cput('environment',{'id'=>$ids{$uname}},$udom,$uname);
 1796:         if ($uhom eq '') {
 1797:             $uhom=&homeserver($uname,$udom);
 1798:         }
 1799:         if ($uhom ne 'no_host') {
 1800:             my $esc_unam=&escape($uname);
 1801:             if ($namespace eq 'ids') {
 1802:                 my $id=&escape($ids{$uname});
 1803:                 $id=~tr/A-Z/a-z/;
 1804:                 my $esc_unam=&escape($uname);
 1805:                 $servers{$uhom}.=$id.'='.$esc_unam.'&';
 1806:             } else {
 1807:                 my @currids = split(/,/,$ids{$uname});
 1808:                 foreach my $id (@currids) {
 1809:                     $byid{$uhom}{$id} .= $uname.',';
 1810:                 }
 1811:             }
 1812:         }
 1813:     }
 1814:     if ($namespace eq 'clickers') {
 1815:         foreach my $server (keys(%byid)) {
 1816:             if (ref($byid{$server}) eq 'HASH') {
 1817:                 foreach my $id (keys(%{$byid{$server}})) {
 1818:                     $byid{$server} =~ s/,$//;
 1819:                     $servers{$uhom}.=&escape($id).'='.&escape($byid{$server}).'&'; 
 1820:                 }
 1821:             }
 1822:         }
 1823:     }
 1824:     foreach my $server (keys(%servers)) {
 1825:         $servers{$server} =~ s/\&$//;
 1826:         if ($namespace eq 'ids') {     
 1827:             &critical('idput:'.$udom.':'.$servers{$server},$server);
 1828:         } else {
 1829:             &critical('updateclickers:'.$udom.':add:'.$servers{$server},$server);
 1830:         }
 1831:     }
 1832: }
 1833: 
 1834: # ------------- Delete unwanted student/employee IDs or clicker IDs from domain
 1835: 
 1836: sub iddel {
 1837:     my ($udom,$idshashref,$uhome,$namespace)=@_;
 1838:     my %result=();
 1839:     my %ids=();
 1840:     my %byid = ();
 1841:     if (ref($idshashref) eq 'HASH') {
 1842:         %ids=%{$idshashref};
 1843:     } else {
 1844:         return %result;
 1845:     }
 1846:     if ($namespace eq '') {
 1847:         $namespace = 'ids';
 1848:     }
 1849:     my %servers=();
 1850:     while (my ($id,$unamestr) = each(%ids)) {
 1851:         if ($namespace eq 'ids') {
 1852:             my $uhom = $uhome;
 1853:             if ($uhom eq '') { 
 1854:                 $uhom=&homeserver($unamestr,$udom);
 1855:             }
 1856:             if ($uhom ne 'no_host') {
 1857:                 $servers{$uhom}.='&'.&escape($id);
 1858:             }
 1859:          } else {
 1860:             my @curritems = split(/,/,$ids{$id});
 1861:             foreach my $uname (@curritems) {
 1862:                 my $uhom = $uhome;
 1863:                 if ($uhom eq '') {
 1864:                     $uhom=&homeserver($uname,$udom);
 1865:                 }
 1866:                 if ($uhom ne 'no_host') { 
 1867:                     $byid{$uhom}{$id} .= $uname.',';
 1868:                 }
 1869:             }
 1870:         }
 1871:     }
 1872:     if ($namespace eq 'clickers') {
 1873:         foreach my $server (keys(%byid)) {
 1874:             if (ref($byid{$server}) eq 'HASH') {
 1875:                 foreach my $id (keys(%{$byid{$server}})) {
 1876:                     $byid{$server}{$id} =~ s/,$//;
 1877:                     $servers{$server}.=&escape($id).'='.&escape($byid{$server}{$id}).'&';
 1878:                 }
 1879:             }
 1880:         }
 1881:     }
 1882:     foreach my $server (keys(%servers)) {
 1883:         $servers{$server} =~ s/\&$//;
 1884:         if ($namespace eq 'ids') {
 1885:             $result{$server} = &critical('iddel:'.$udom.':'.$servers{$server},$uhome);
 1886:         } elsif ($namespace eq 'clickers') {
 1887:             $result{$server} = &critical('updateclickers:'.$udom.':del:'.$servers{$server},$server);
 1888:         }
 1889:     }
 1890:     return %result;
 1891: }
 1892: 
 1893: # ----- Update clicker ID-to-username look-ups in clickers.db on library server 
 1894: 
 1895: sub updateclickers {
 1896:     my ($udom,$action,$idshashref,$uhome,$critical) = @_;
 1897:     my %clickers;
 1898:     if (ref($idshashref) eq 'HASH') {
 1899:         %clickers=%{$idshashref};
 1900:     } else {
 1901:         return;
 1902:     }
 1903:     my $items='';
 1904:     foreach my $item (keys(%clickers)) {
 1905:         $items.=&escape($item).'='.&escape($clickers{$item}).'&';
 1906:     }
 1907:     $items=~s/\&$//;
 1908:     my $request = "updateclickers:$udom:$action:$items";
 1909:     if ($critical) {
 1910:         return &critical($request,$uhome);
 1911:     } else {
 1912:         return &reply($request,$uhome);
 1913:     }
 1914: }
 1915: 
 1916: # ------------------------------dump from db file owned by domainconfig user
 1917: sub dump_dom {
 1918:     my ($namespace, $udom, $regexp) = @_;
 1919: 
 1920:     $udom ||= $env{'user.domain'};
 1921: 
 1922:     return () unless $udom;
 1923: 
 1924:     return &dump($namespace, $udom, &get_domainconfiguser($udom), $regexp);
 1925: }
 1926: 
 1927: # ------------------------------------------ get items from domain db files   
 1928: 
 1929: sub get_dom {
 1930:     my ($namespace,$storearr,$udom,$uhome)=@_;
 1931:     return if ($udom eq 'public');
 1932:     my $items='';
 1933:     foreach my $item (@$storearr) {
 1934:         $items.=&escape($item).'&';
 1935:     }
 1936:     $items=~s/\&$//;
 1937:     if (!$udom) {
 1938:         $udom=$env{'user.domain'};
 1939:         return if ($udom eq 'public');
 1940:         if (defined(&domain($udom,'primary'))) {
 1941:             $uhome=&domain($udom,'primary');
 1942:         } else {
 1943:             undef($uhome);
 1944:         }
 1945:     } else {
 1946:         if (!$uhome) {
 1947:             if (defined(&domain($udom,'primary'))) {
 1948:                 $uhome=&domain($udom,'primary');
 1949:             }
 1950:         }
 1951:     }
 1952:     if ($udom && $uhome && ($uhome ne 'no_host')) {
 1953:         my $rep;
 1954:         if ($namespace =~ /^enc/) {
 1955:             $rep=&reply("encrypt:egetdom:$udom:$namespace:$items",$uhome);
 1956:         } else {
 1957:             $rep=&reply("getdom:$udom:$namespace:$items",$uhome);
 1958:         }
 1959:         my %returnhash;
 1960:         if ($rep eq '' || $rep =~ /^error: 2 /) {
 1961:             return %returnhash;
 1962:         }
 1963:         my @pairs=split(/\&/,$rep);
 1964:         if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
 1965:             return @pairs;
 1966:         }
 1967:         my $i=0;
 1968:         foreach my $item (@$storearr) {
 1969:             $returnhash{$item}=&thaw_unescape($pairs[$i]);
 1970:             $i++;
 1971:         }
 1972:         return %returnhash;
 1973:     } else {
 1974:         &logthis("get_dom failed - no homeserver and/or domain ($udom) ($uhome)");
 1975:     }
 1976: }
 1977: 
 1978: # -------------------------------------------- put items in domain db files 
 1979: 
 1980: sub put_dom {
 1981:     my ($namespace,$storehash,$udom,$uhome)=@_;
 1982:     if (!$udom) {
 1983:         $udom=$env{'user.domain'};
 1984:         if (defined(&domain($udom,'primary'))) {
 1985:             $uhome=&domain($udom,'primary');
 1986:         } else {
 1987:             undef($uhome);
 1988:         }
 1989:     } else {
 1990:         if (!$uhome) {
 1991:             if (defined(&domain($udom,'primary'))) {
 1992:                 $uhome=&domain($udom,'primary');
 1993:             }
 1994:         }
 1995:     } 
 1996:     if ($udom && $uhome && ($uhome ne 'no_host')) {
 1997:         my $items='';
 1998:         foreach my $item (keys(%$storehash)) {
 1999:             $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
 2000:         }
 2001:         $items=~s/\&$//;
 2002:         if ($namespace =~ /^enc/) {
 2003:             return &reply("encrypt:putdom:$udom:$namespace:$items",$uhome);
 2004:         } else {
 2005:             return &reply("putdom:$udom:$namespace:$items",$uhome);
 2006:         }
 2007:     } else {
 2008:         &logthis("put_dom failed - no homeserver and/or domain");
 2009:     }
 2010: }
 2011: 
 2012: # --------------------- newput for items in db file owned by domainconfig user
 2013: sub newput_dom {
 2014:     my ($namespace,$storehash,$udom) = @_;
 2015:     my $result;
 2016:     if (!$udom) {
 2017:         $udom=$env{'user.domain'};
 2018:     }
 2019:     if ($udom) {
 2020:         my $uname = &get_domainconfiguser($udom);
 2021:         $result = &newput($namespace,$storehash,$udom,$uname);
 2022:     }
 2023:     return $result;
 2024: }
 2025: 
 2026: # --------------------- delete for items in db file owned by domainconfig user
 2027: sub del_dom {
 2028:     my ($namespace,$storearr,$udom)=@_;
 2029:     if (ref($storearr) eq 'ARRAY') {
 2030:         if (!$udom) {
 2031:             $udom=$env{'user.domain'};
 2032:         }
 2033:         if ($udom) {
 2034:             my $uname = &get_domainconfiguser($udom); 
 2035:             return &del($namespace,$storearr,$udom,$uname);
 2036:         }
 2037:     }
 2038: }
 2039: 
 2040: # ----------------------------------construct domainconfig user for a domain 
 2041: sub get_domainconfiguser {
 2042:     my ($udom) = @_;
 2043:     return $udom.'-domainconfig';
 2044: }
 2045: 
 2046: sub retrieve_inst_usertypes {
 2047:     my ($udom) = @_;
 2048:     my (%returnhash,@order);
 2049:     my %domdefs = &Apache::lonnet::get_domain_defaults($udom);
 2050:     if ((ref($domdefs{'inststatustypes'}) eq 'HASH') && 
 2051:         (ref($domdefs{'inststatusorder'}) eq 'ARRAY')) {
 2052:         return ($domdefs{'inststatustypes'},$domdefs{'inststatusorder'});
 2053:     } else {
 2054:         if (defined(&domain($udom,'primary'))) {
 2055:             my $uhome=&domain($udom,'primary');
 2056:             my $rep=&reply("inst_usertypes:$udom",$uhome);
 2057:             if ($rep =~ /^(con_lost|error|no_such_host|refused)/) {
 2058:                 &logthis("retrieve_inst_usertypes failed - $rep returned from $uhome in domain: $udom");
 2059:                 return (\%returnhash,\@order);
 2060:             }
 2061:             my ($hashitems,$orderitems) = split(/:/,$rep); 
 2062:             my @pairs=split(/\&/,$hashitems);
 2063:             foreach my $item (@pairs) {
 2064:                 my ($key,$value)=split(/=/,$item,2);
 2065:                 $key = &unescape($key);
 2066:                 next if ($key =~ /^error: 2 /);
 2067:                 $returnhash{$key}=&thaw_unescape($value);
 2068:             }
 2069:             my @esc_order = split(/\&/,$orderitems);
 2070:             foreach my $item (@esc_order) {
 2071:                 push(@order,&unescape($item));
 2072:             }
 2073:         } else {
 2074:             &logthis("retrieve_inst_usertypes failed - no primary domain server for $udom");
 2075:         }
 2076:         return (\%returnhash,\@order);
 2077:     }
 2078: }
 2079: 
 2080: sub is_domainimage {
 2081:     my ($url) = @_;
 2082:     if ($url=~m-^/+res/+($match_domain)/+\1\-domainconfig/+(img|logo|domlogo)/+[^/]-) {
 2083:         if (&domain($1) ne '') {
 2084:             return '1';
 2085:         }
 2086:     }
 2087:     return;
 2088: }
 2089: 
 2090: sub inst_directory_query {
 2091:     my ($srch) = @_;
 2092:     my $udom = $srch->{'srchdomain'};
 2093:     my %results;
 2094:     my $homeserver = &domain($udom,'primary');
 2095:     my $outcome;
 2096:     if ($homeserver ne '') {
 2097: 	my $queryid=&reply("querysend:instdirsearch:".
 2098: 			   &escape($srch->{'srchby'}).':'.
 2099: 			   &escape($srch->{'srchterm'}).':'.
 2100: 			   &escape($srch->{'srchtype'}),$homeserver);
 2101: 	my $host=&hostname($homeserver);
 2102: 	if ($queryid !~/^\Q$host\E\_/) {
 2103: 	    &logthis('institutional directory search invalid queryid: '.$queryid.' for host: '.$homeserver.' in domain '.$udom);
 2104: 	    return;
 2105: 	}
 2106: 	my $response = &get_query_reply($queryid);
 2107: 	my $maxtries = 5;
 2108: 	my $tries = 1;
 2109: 	while (($response=~/^timeout/) && ($tries < $maxtries)) {
 2110: 	    $response = &get_query_reply($queryid);
 2111: 	    $tries ++;
 2112: 	}
 2113: 
 2114:         if (!&error($response) && $response ne 'refused') {
 2115:             if ($response eq 'unavailable') {
 2116:                 $outcome = $response;
 2117:             } else {
 2118:                 $outcome = 'ok';
 2119:                 my @matches = split(/\n/,$response);
 2120:                 foreach my $match (@matches) {
 2121:                     my ($key,$value) = split(/=/,$match);
 2122:                     $results{&unescape($key).':'.$udom} = &thaw_unescape($value);
 2123:                 }
 2124:             }
 2125:         }
 2126:     }
 2127:     return ($outcome,%results);
 2128: }
 2129: 
 2130: sub usersearch {
 2131:     my ($srch) = @_;
 2132:     my $dom = $srch->{'srchdomain'};
 2133:     my %results;
 2134:     my %libserv = &all_library();
 2135:     my $query = 'usersearch';
 2136:     foreach my $tryserver (keys(%libserv)) {
 2137:         if (&host_domain($tryserver) eq $dom) {
 2138:             my $host=&hostname($tryserver);
 2139:             my $queryid=
 2140:                 &reply("querysend:".&escape($query).':'.
 2141:                        &escape($srch->{'srchby'}).':'.
 2142:                        &escape($srch->{'srchtype'}).':'.
 2143:                        &escape($srch->{'srchterm'}),$tryserver);
 2144:             if ($queryid !~/^\Q$host\E\_/) {
 2145:                 &logthis('usersearch: invalid queryid: '.$queryid.' for host: '.$host.'in domain '.$dom.' and server: '.$tryserver);
 2146:                 next;
 2147:             }
 2148:             my $reply = &get_query_reply($queryid);
 2149:             my $maxtries = 1;
 2150:             my $tries = 1;
 2151:             while (($reply=~/^timeout/) && ($tries < $maxtries)) {
 2152:                 $reply = &get_query_reply($queryid);
 2153:                 $tries ++;
 2154:             }
 2155:             if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
 2156:                 &logthis('usersrch error: '.$reply.' for '.$dom.' - searching for : '.$srch->{'srchterm'}.' by '.$srch->{'srchby'}.' ('.$srch->{'srchtype'}.') -  maxtries: '.$maxtries.' tries: '.$tries);
 2157:             } else {
 2158:                 my @matches;
 2159:                 if ($reply =~ /\n/) {
 2160:                     @matches = split(/\n/,$reply);
 2161:                 } else {
 2162:                     @matches = split(/\&/,$reply);
 2163:                 }
 2164:                 foreach my $match (@matches) {
 2165:                     my ($uname,$udom,%userhash);
 2166:                     foreach my $entry (split(/:/,$match)) {
 2167:                         my ($key,$value) =
 2168:                             map {&unescape($_);} split(/=/,$entry);
 2169:                         $userhash{$key} = $value;
 2170:                         if ($key eq 'username') {
 2171:                             $uname = $value;
 2172:                         } elsif ($key eq 'domain') {
 2173:                             $udom = $value;
 2174:                         }
 2175:                     }
 2176:                     $results{$uname.':'.$udom} = \%userhash;
 2177:                 }
 2178:             }
 2179:         }
 2180:     }
 2181:     return %results;
 2182: }
 2183: 
 2184: sub get_instuser {
 2185:     my ($udom,$uname,$id) = @_;
 2186:     my $homeserver = &domain($udom,'primary');
 2187:     my ($outcome,%results);
 2188:     if ($homeserver ne '') {
 2189:         my $queryid=&reply("querysend:getinstuser:".&escape($uname).':'.
 2190:                            &escape($id).':'.&escape($udom),$homeserver);
 2191:         my $host=&hostname($homeserver);
 2192:         if ($queryid !~/^\Q$host\E\_/) {
 2193:             &logthis('get_instuser invalid queryid: '.$queryid.' for host: '.$homeserver.'in domain '.$udom);
 2194:             return;
 2195:         }
 2196:         my $response = &get_query_reply($queryid);
 2197:         my $maxtries = 5;
 2198:         my $tries = 1;
 2199:         while (($response=~/^timeout/) && ($tries < $maxtries)) {
 2200:             $response = &get_query_reply($queryid);
 2201:             $tries ++;
 2202:         }
 2203:         if (!&error($response) && $response ne 'refused') {
 2204:             if ($response eq 'unavailable') {
 2205:                 $outcome = $response;
 2206:             } else {
 2207:                 $outcome = 'ok';
 2208:                 my @matches = split(/\n/,$response);
 2209:                 foreach my $match (@matches) {
 2210:                     my ($key,$value) = split(/=/,$match);
 2211:                     $results{&unescape($key)} = &thaw_unescape($value);
 2212:                 }
 2213:             }
 2214:         }
 2215:     }
 2216:     my %userinfo;
 2217:     if (ref($results{$uname}) eq 'HASH') {
 2218:         %userinfo = %{$results{$uname}};
 2219:     } 
 2220:     return ($outcome,%userinfo);
 2221: }
 2222: 
 2223: sub get_multiple_instusers {
 2224:     my ($udom,$users,$caller) = @_;
 2225:     my ($outcome,$results);
 2226:     if (ref($users) eq 'HASH') {
 2227:         my $count = keys(%{$users}); 
 2228:         my $requested = &freeze_escape($users);
 2229:         my $homeserver = &domain($udom,'primary');
 2230:         if ($homeserver ne '') {
 2231:             my $queryid=&reply('querysend:getmultinstusers:::'.$caller.'='.$requested,$homeserver);
 2232:             my $host=&hostname($homeserver);
 2233:             if ($queryid !~/^\Q$host\E\_/) {
 2234:                 &logthis('get_multiple_instusers invalid queryid: '.$queryid.
 2235:                          ' for host: '.$homeserver.'in domain '.$udom);
 2236:                 return ($outcome,$results);
 2237:             }
 2238:             my $response = &get_query_reply($queryid);
 2239:             my $maxtries = 5;
 2240:             if ($count > 100) {
 2241:                 $maxtries = 1+int($count/20);
 2242:             }
 2243:             my $tries = 1;
 2244:             while (($response=~/^timeout/) && ($tries <= $maxtries)) {
 2245:                 $response = &get_query_reply($queryid);
 2246:                 $tries ++;
 2247:             }
 2248:             if ($response eq '') {
 2249:                 $results = {};
 2250:                 foreach my $key (keys(%{$users})) {
 2251:                     my ($uname,$id);
 2252:                     if ($caller eq 'id') {
 2253:                         $id = $key;
 2254:                     } else {
 2255:                         $uname = $key;
 2256:                     }
 2257:                     my ($resp,%info) = &get_instuser($udom,$uname,$id);
 2258:                     $outcome = $resp;
 2259:                     if ($resp eq 'ok') {
 2260:                         %{$results} = (%{$results}, %info);
 2261:                     } else {
 2262:                         last;
 2263:                     }
 2264:                 }
 2265:             } elsif(!&error($response) && ($response ne 'refused')) {
 2266:                 if (($response eq 'unavailable') || ($response eq 'invalid') || ($response eq 'timeout')) {
 2267:                     $outcome = $response;
 2268:                 } else {
 2269:                     ($outcome,my $userdata) = split(/=/,$response,2);
 2270:                     if ($outcome eq 'ok') {
 2271:                         $results = &thaw_unescape($userdata); 
 2272:                     }
 2273:                 }
 2274:             }
 2275:         }
 2276:     }
 2277:     return ($outcome,$results);
 2278: }
 2279: 
 2280: sub inst_rulecheck {
 2281:     my ($udom,$uname,$id,$item,$rules) = @_;
 2282:     my %returnhash;
 2283:     if ($udom ne '') {
 2284:         if (ref($rules) eq 'ARRAY') {
 2285:             @{$rules} = map {&escape($_);} (@{$rules});
 2286:             my $rulestr = join(':',@{$rules});
 2287:             my $homeserver=&domain($udom,'primary');
 2288:             if (($homeserver ne '') && ($homeserver ne 'no_host')) {
 2289:                 my $response;
 2290:                 if ($item eq 'username') {                
 2291:                     $response=&unescape(&reply('instrulecheck:'.&escape($udom).
 2292:                                               ':'.&escape($uname).':'.$rulestr,
 2293:                                               $homeserver));
 2294:                 } elsif ($item eq 'id') {
 2295:                     $response=&unescape(&reply('instidrulecheck:'.&escape($udom).
 2296:                                               ':'.&escape($id).':'.$rulestr,
 2297:                                               $homeserver));
 2298:                 } elsif ($item eq 'selfcreate') {
 2299:                     $response=&unescape(&reply('instselfcreatecheck:'.
 2300:                                                &escape($udom).':'.&escape($uname).
 2301:                                               ':'.$rulestr,$homeserver));
 2302:                 }
 2303:                 if ($response ne 'refused') {
 2304:                     my @pairs=split(/\&/,$response);
 2305:                     foreach my $item (@pairs) {
 2306:                         my ($key,$value)=split(/=/,$item,2);
 2307:                         $key = &unescape($key);
 2308:                         next if ($key =~ /^error: 2 /);
 2309:                         $returnhash{$key}=&thaw_unescape($value);
 2310:                     }
 2311:                 }
 2312:             }
 2313:         }
 2314:     }
 2315:     return %returnhash;
 2316: }
 2317: 
 2318: sub inst_userrules {
 2319:     my ($udom,$check) = @_;
 2320:     my (%ruleshash,@ruleorder);
 2321:     if ($udom ne '') {
 2322:         my $homeserver=&domain($udom,'primary');
 2323:         if (($homeserver ne '') && ($homeserver ne 'no_host')) {
 2324:             my $response;
 2325:             if ($check eq 'id') {
 2326:                 $response=&reply('instidrules:'.&escape($udom),
 2327:                                  $homeserver);
 2328:             } elsif ($check eq 'email') {
 2329:                 $response=&reply('instemailrules:'.&escape($udom),
 2330:                                  $homeserver);
 2331:             } else {
 2332:                 $response=&reply('instuserrules:'.&escape($udom),
 2333:                                  $homeserver);
 2334:             }
 2335:             if (($response ne 'refused') && ($response ne 'error') && 
 2336:                 ($response ne 'unknown_cmd') && 
 2337:                 ($response ne 'no_such_host')) {
 2338:                 my ($hashitems,$orderitems) = split(/:/,$response);
 2339:                 my @pairs=split(/\&/,$hashitems);
 2340:                 foreach my $item (@pairs) {
 2341:                     my ($key,$value)=split(/=/,$item,2);
 2342:                     $key = &unescape($key);
 2343:                     next if ($key =~ /^error: 2 /);
 2344:                     $ruleshash{$key}=&thaw_unescape($value);
 2345:                 }
 2346:                 my @esc_order = split(/\&/,$orderitems);
 2347:                 foreach my $item (@esc_order) {
 2348:                     push(@ruleorder,&unescape($item));
 2349:                 }
 2350:             }
 2351:         }
 2352:     }
 2353:     return (\%ruleshash,\@ruleorder);
 2354: }
 2355: 
 2356: # ------------- Get Authentication, Language and User Tools Defaults for Domain
 2357: 
 2358: sub get_domain_defaults {
 2359:     my ($domain,$ignore_cache) = @_;
 2360:     return if (($domain eq '') || ($domain eq 'public'));
 2361:     my $cachetime = 60*60*24;
 2362:     unless ($ignore_cache) {
 2363:         my ($result,$cached)=&is_cached_new('domdefaults',$domain);
 2364:         if (defined($cached)) {
 2365:             if (ref($result) eq 'HASH') {
 2366:                 return %{$result};
 2367:             }
 2368:         }
 2369:     }
 2370:     my %domdefaults;
 2371:     my %domconfig =
 2372:          &Apache::lonnet::get_dom('configuration',['defaults','quotas',
 2373:                                   'requestcourses','inststatus',
 2374:                                   'coursedefaults','usersessions',
 2375:                                   'requestauthor','selfenrollment',
 2376:                                   'coursecategories','ssl','autoenroll',
 2377:                                   'trust','helpsettings'],$domain);
 2378:     my @coursetypes = ('official','unofficial','community','textbook','placement');
 2379:     if (ref($domconfig{'defaults'}) eq 'HASH') {
 2380:         $domdefaults{'lang_def'} = $domconfig{'defaults'}{'lang_def'}; 
 2381:         $domdefaults{'auth_def'} = $domconfig{'defaults'}{'auth_def'};
 2382:         $domdefaults{'auth_arg_def'} = $domconfig{'defaults'}{'auth_arg_def'};
 2383:         $domdefaults{'timezone_def'} = $domconfig{'defaults'}{'timezone_def'};
 2384:         $domdefaults{'datelocale_def'} = $domconfig{'defaults'}{'datelocale_def'};
 2385:         $domdefaults{'portal_def'} = $domconfig{'defaults'}{'portal_def'};
 2386:         $domdefaults{'intauth_cost'} = $domconfig{'defaults'}{'intauth_cost'};
 2387:         $domdefaults{'intauth_switch'} = $domconfig{'defaults'}{'intauth_switch'};
 2388:         $domdefaults{'intauth_check'} = $domconfig{'defaults'}{'intauth_check'};
 2389:     } else {
 2390:         $domdefaults{'lang_def'} = &domain($domain,'lang_def');
 2391:         $domdefaults{'auth_def'} = &domain($domain,'auth_def');
 2392:         $domdefaults{'auth_arg_def'} = &domain($domain,'auth_arg_def');
 2393:     }
 2394:     if (ref($domconfig{'quotas'}) eq 'HASH') {
 2395:         if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 2396:             $domdefaults{'defaultquota'} = $domconfig{'quotas'}{'defaultquota'};
 2397:         } else {
 2398:             $domdefaults{'defaultquota'} = $domconfig{'quotas'};
 2399:         }
 2400:         my @usertools = ('aboutme','blog','webdav','portfolio');
 2401:         foreach my $item (@usertools) {
 2402:             if (ref($domconfig{'quotas'}{$item}) eq 'HASH') {
 2403:                 $domdefaults{$item} = $domconfig{'quotas'}{$item};
 2404:             }
 2405:         }
 2406:         if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 2407:             $domdefaults{'authorquota'} = $domconfig{'quotas'}{'authorquota'};
 2408:         }
 2409:     }
 2410:     if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 2411:         foreach my $item ('official','unofficial','community','textbook','placement') {
 2412:             $domdefaults{$item} = $domconfig{'requestcourses'}{$item};
 2413:         }
 2414:     }
 2415:     if (ref($domconfig{'requestauthor'}) eq 'HASH') {
 2416:         $domdefaults{'requestauthor'} = $domconfig{'requestauthor'};
 2417:     }
 2418:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
 2419:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
 2420:             $domdefaults{$item} = $domconfig{'inststatus'}{$item};
 2421:         }
 2422:     }
 2423:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
 2424:         $domdefaults{'canuse_pdfforms'} = $domconfig{'coursedefaults'}{'canuse_pdfforms'};
 2425:         $domdefaults{'usejsme'} = $domconfig{'coursedefaults'}{'usejsme'};
 2426:         $domdefaults{'uselcmath'} = $domconfig{'coursedefaults'}{'uselcmath'};
 2427:         if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
 2428:             $domdefaults{'postsubmit'} = $domconfig{'coursedefaults'}{'postsubmit'}{'client'};
 2429:         }
 2430:         foreach my $type (@coursetypes) {
 2431:             if (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
 2432:                 unless ($type eq 'community') {
 2433:                     $domdefaults{$type.'credits'} = $domconfig{'coursedefaults'}{'coursecredits'}{$type};
 2434:                 }
 2435:             }
 2436:             if (ref($domconfig{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
 2437:                 $domdefaults{$type.'quota'} = $domconfig{'coursedefaults'}{'uploadquota'}{$type};
 2438:             }
 2439:             if ($domdefaults{'postsubmit'} eq 'on') {
 2440:                 if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
 2441:                     $domdefaults{$type.'postsubtimeout'} = 
 2442:                         $domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}{$type}; 
 2443:                 }
 2444:             }
 2445:         }
 2446:         if (ref($domconfig{'coursedefaults'}{'canclone'}) eq 'HASH') {
 2447:             if (ref($domconfig{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
 2448:                 my @clonecodes = @{$domconfig{'coursedefaults'}{'canclone'}{'instcode'}};
 2449:                 if (@clonecodes) {
 2450:                     $domdefaults{'canclone'} = join('+',@clonecodes);
 2451:                 }
 2452:             }
 2453:         } elsif ($domconfig{'coursedefaults'}{'canclone'}) {
 2454:             $domdefaults{'canclone'}=$domconfig{'coursedefaults'}{'canclone'};
 2455:         }
 2456:         if ($domconfig{'coursedefaults'}{'texengine'}) {
 2457:             $domdefaults{'texengine'} = $domconfig{'coursedefaults'}{'texengine'};
 2458:         } 
 2459:     }
 2460:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
 2461:         if (ref($domconfig{'usersessions'}{'remote'}) eq 'HASH') {
 2462:             $domdefaults{'remotesessions'} = $domconfig{'usersessions'}{'remote'};
 2463:         }
 2464:         if (ref($domconfig{'usersessions'}{'hosted'}) eq 'HASH') {
 2465:             $domdefaults{'hostedsessions'} = $domconfig{'usersessions'}{'hosted'};
 2466:         }
 2467:         if (ref($domconfig{'usersessions'}{'offloadnow'}) eq 'HASH') {
 2468:             $domdefaults{'offloadnow'} = $domconfig{'usersessions'}{'offloadnow'};
 2469:         }
 2470:     }
 2471:     if (ref($domconfig{'selfenrollment'}) eq 'HASH') {
 2472:         if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
 2473:             my @settings = ('types','registered','enroll_dates','access_dates','section',
 2474:                             'approval','limit');
 2475:             foreach my $type (@coursetypes) {
 2476:                 if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
 2477:                     my @mgrdc = ();
 2478:                     foreach my $item (@settings) {
 2479:                         if ($domconfig{'selfenrollment'}{'admin'}{$type}{$item} eq '0') {
 2480:                             push(@mgrdc,$item);
 2481:                         }
 2482:                     }
 2483:                     if (@mgrdc) {
 2484:                         $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
 2485:                     }
 2486:                 }
 2487:             }
 2488:         }
 2489:         if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
 2490:             foreach my $type (@coursetypes) {
 2491:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
 2492:                     foreach my $item (keys(%{$domconfig{'selfenrollment'}{'default'}{$type}})) {
 2493:                         $domdefaults{$type.'selfenroll'.$item} = $domconfig{'selfenrollment'}{'default'}{$type}{$item};
 2494:                     }
 2495:                 }
 2496:             }
 2497:         }
 2498:     }
 2499:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 2500:         $domdefaults{'catauth'} = 'std';
 2501:         $domdefaults{'catunauth'} = 'std';
 2502:         if ($domconfig{'coursecategories'}{'auth'}) { 
 2503:             $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
 2504:         }
 2505:         if ($domconfig{'coursecategories'}{'unauth'}) {
 2506:             $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
 2507:         }
 2508:     }
 2509:     if (ref($domconfig{'ssl'}) eq 'HASH') {
 2510:         if (ref($domconfig{'ssl'}{'replication'}) eq 'HASH') {
 2511:             $domdefaults{'replication'} = $domconfig{'ssl'}{'replication'};
 2512:         }
 2513:         if (ref($domconfig{'ssl'}{'connto'}) eq 'HASH') {
 2514:             $domdefaults{'connect'} = $domconfig{'ssl'}{'connto'};
 2515:         }
 2516:         if (ref($domconfig{'ssl'}{'connfrom'}) eq 'HASH') {
 2517:             $domdefaults{'connect'} = $domconfig{'ssl'}{'connfrom'};
 2518:         }
 2519:     }
 2520:     if (ref($domconfig{'trust'}) eq 'HASH') {
 2521:         my @prefixes = qw(content shared enroll othcoau coaurem domroles catalog reqcrs msg);
 2522:         foreach my $prefix (@prefixes) {
 2523:             if (ref($domconfig{'trust'}{$prefix}) eq 'HASH') {
 2524:                 $domdefaults{'trust'.$prefix} = $domconfig{'trust'}{$prefix};
 2525:             }
 2526:         }
 2527:     }
 2528:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 2529:         $domdefaults{'autofailsafe'} = $domconfig{'autoenroll'}{'autofailsafe'};
 2530:     }
 2531:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
 2532:         $domdefaults{'submitbugs'} = $domconfig{'helpsettings'}{'submitbugs'};
 2533:         if (ref($domconfig{'helpsettings'}{'adhoc'}) eq 'HASH') {
 2534:             $domdefaults{'adhocroles'} = $domconfig{'helpsettings'}{'adhoc'};
 2535:         }
 2536:     }
 2537:     &do_cache_new('domdefaults',$domain,\%domdefaults,$cachetime);
 2538:     return %domdefaults;
 2539: }
 2540: 
 2541: sub course_portal_url {
 2542:     my ($cnum,$cdom) = @_;
 2543:     my $chome = &homeserver($cnum,$cdom);
 2544:     my $hostname = &hostname($chome);
 2545:     my $protocol = $protocol{$chome};
 2546:     $protocol = 'http' if ($protocol ne 'https');
 2547:     my %domdefaults = &get_domain_defaults($cdom);
 2548:     my $firsturl;
 2549:     if ($domdefaults{'portal_def'}) {
 2550:         $firsturl = $domdefaults{'portal_def'};
 2551:     } else {
 2552:         $firsturl = $protocol.'://'.$hostname;
 2553:     }
 2554:     return $firsturl;
 2555: }
 2556: 
 2557: # --------------------------------------------------- Assign a key to a student
 2558: 
 2559: sub assign_access_key {
 2560: #
 2561: # a valid key looks like uname:udom#comments
 2562: # comments are being appended
 2563: #
 2564:     my ($ckey,$kdom,$knum,$cdom,$cnum,$udom,$uname,$logentry)=@_;
 2565:     $kdom=
 2566:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($kdom));
 2567:     $knum=
 2568:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($knum));
 2569:     $cdom=
 2570:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
 2571:     $cnum=
 2572:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
 2573:     $udom=$env{'user.name'} unless (defined($udom));
 2574:     $uname=$env{'user.domain'} unless (defined($uname));
 2575:     my %existing=&get('accesskeys',[$ckey],$kdom,$knum);
 2576:     if (($existing{$ckey}=~/^\#(.*)$/) || # - new key
 2577:         ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#(.*)$/)) { 
 2578:                                                   # assigned to this person
 2579:                                                   # - this should not happen,
 2580:                                                   # unless something went wrong
 2581:                                                   # the first time around
 2582: # ready to assign
 2583:         $logentry=$1.'; '.$logentry;
 2584:         if (&put('accesskeys',{$ckey=>$uname.':'.$udom.'#'.$logentry},
 2585:                                                  $kdom,$knum) eq 'ok') {
 2586: # key now belongs to user
 2587: 	    my $envkey='key.'.$cdom.'_'.$cnum;
 2588:             if (&put('environment',{$envkey => $ckey}) eq 'ok') {
 2589:                 &appenv({'environment.'.$envkey => $ckey});
 2590:                 return 'ok';
 2591:             } else {
 2592:                 return 
 2593:   'error: Count not permanently assign key, will need to be re-entered later.';
 2594: 	    }
 2595:         } else {
 2596:             return 'error: Could not assign key, try again later.';
 2597:         }
 2598:     } elsif (!$existing{$ckey}) {
 2599: # the key does not exist
 2600: 	return 'error: The key does not exist';
 2601:     } else {
 2602: # the key is somebody else's
 2603: 	return 'error: The key is already in use';
 2604:     }
 2605: }
 2606: 
 2607: # ------------------------------------------ put an additional comment on a key
 2608: 
 2609: sub comment_access_key {
 2610: #
 2611: # a valid key looks like uname:udom#comments
 2612: # comments are being appended
 2613: #
 2614:     my ($ckey,$cdom,$cnum,$logentry)=@_;
 2615:     $cdom=
 2616:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
 2617:     $cnum=
 2618:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
 2619:     my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
 2620:     if ($existing{$ckey}) {
 2621:         $existing{$ckey}.='; '.$logentry;
 2622: # ready to assign
 2623:         if (&put('accesskeys',{$ckey=>$existing{$ckey}},
 2624:                                                  $cdom,$cnum) eq 'ok') {
 2625: 	    return 'ok';
 2626:         } else {
 2627: 	    return 'error: Count not store comment.';
 2628:         }
 2629:     } else {
 2630: # the key does not exist
 2631: 	return 'error: The key does not exist';
 2632:     }
 2633: }
 2634: 
 2635: # ------------------------------------------------------ Generate a set of keys
 2636: 
 2637: sub generate_access_keys {
 2638:     my ($number,$cdom,$cnum,$logentry)=@_;
 2639:     $cdom=
 2640:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
 2641:     $cnum=
 2642:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
 2643:     unless (&allowed('mky',$cdom)) { return 0; }
 2644:     unless (($cdom) && ($cnum)) { return 0; }
 2645:     if ($number>10000) { return 0; }
 2646:     sleep(2); # make sure don't get same seed twice
 2647:     srand(time()^($$+($$<<15))); # from "Programming Perl"
 2648:     my $total=0;
 2649:     for (my $i=1;$i<=$number;$i++) {
 2650:        my $newkey=sprintf("%lx",int(100000*rand)).'-'.
 2651:                   sprintf("%lx",int(100000*rand)).'-'.
 2652:                   sprintf("%lx",int(100000*rand));
 2653:        $newkey=~s/1/g/g; # folks mix up 1 and l
 2654:        $newkey=~s/0/h/g; # and also 0 and O
 2655:        my %existing=&get('accesskeys',[$newkey],$cdom,$cnum);
 2656:        if ($existing{$newkey}) {
 2657:            $i--;
 2658:        } else {
 2659: 	  if (&put('accesskeys',
 2660:               { $newkey => '# generated '.localtime().
 2661:                            ' by '.$env{'user.name'}.'@'.$env{'user.domain'}.
 2662:                            '; '.$logentry },
 2663: 		   $cdom,$cnum) eq 'ok') {
 2664:               $total++;
 2665: 	  }
 2666:        }
 2667:     }
 2668:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
 2669:          'Generated '.$total.' keys for '.$cnum.' at '.$cdom);
 2670:     return $total;
 2671: }
 2672: 
 2673: # ------------------------------------------------------- Validate an accesskey
 2674: 
 2675: sub validate_access_key {
 2676:     my ($ckey,$cdom,$cnum,$udom,$uname)=@_;
 2677:     $cdom=
 2678:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
 2679:     $cnum=
 2680:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
 2681:     $udom=$env{'user.domain'} unless (defined($udom));
 2682:     $uname=$env{'user.name'} unless (defined($uname));
 2683:     my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
 2684:     return ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#/);
 2685: }
 2686: 
 2687: # ------------------------------------- Find the section of student in a course
 2688: sub devalidate_getsection_cache {
 2689:     my ($udom,$unam,$courseid)=@_;
 2690:     my $hashid="$udom:$unam:$courseid";
 2691:     &devalidate_cache_new('getsection',$hashid);
 2692: }
 2693: 
 2694: sub courseid_to_courseurl {
 2695:     my ($courseid) = @_;
 2696:     #already url style courseid
 2697:     return $courseid if ($courseid =~ m{^/});
 2698: 
 2699:     if (exists($env{'course.'.$courseid.'.num'})) {
 2700: 	my $cnum = $env{'course.'.$courseid.'.num'};
 2701: 	my $cdom = $env{'course.'.$courseid.'.domain'};
 2702: 	return "/$cdom/$cnum";
 2703:     }
 2704: 
 2705:     my %courseinfo=&Apache::lonnet::coursedescription($courseid);
 2706:     if (exists($courseinfo{'num'})) {
 2707: 	return "/$courseinfo{'domain'}/$courseinfo{'num'}";
 2708:     }
 2709: 
 2710:     return undef;
 2711: }
 2712: 
 2713: sub getsection {
 2714:     my ($udom,$unam,$courseid)=@_;
 2715:     my $cachetime=1800;
 2716: 
 2717:     my $hashid="$udom:$unam:$courseid";
 2718:     my ($result,$cached)=&is_cached_new('getsection',$hashid);
 2719:     if (defined($cached)) { return $result; }
 2720: 
 2721:     my %Pending; 
 2722:     my %Expired;
 2723:     #
 2724:     # Each role can either have not started yet (pending), be active, 
 2725:     #    or have expired.
 2726:     #
 2727:     # If there is an active role, we are done.
 2728:     #
 2729:     # If there is more than one role which has not started yet, 
 2730:     #     choose the one which will start sooner
 2731:     # If there is one role which has not started yet, return it.
 2732:     #
 2733:     # If there is more than one expired role, choose the one which ended last.
 2734:     # If there is a role which has expired, return it.
 2735:     #
 2736:     $courseid = &courseid_to_courseurl($courseid);
 2737:     my %roleshash = &dump('roles',$udom,$unam,$courseid);
 2738:     foreach my $key (keys(%roleshash)) {
 2739:         next if ($key !~/^\Q$courseid\E(?:\/)*(\w+)*\_st$/);
 2740:         my $section=$1;
 2741:         if ($key eq $courseid.'_st') { $section=''; }
 2742:         my ($dummy,$end,$start)=split(/\_/,&unescape($roleshash{$key}));
 2743:         my $now=time;
 2744:         if (defined($end) && $end && ($now > $end)) {
 2745:             $Expired{$end}=$section;
 2746:             next;
 2747:         }
 2748:         if (defined($start) && $start && ($now < $start)) {
 2749:             $Pending{$start}=$section;
 2750:             next;
 2751:         }
 2752:         return &do_cache_new('getsection',$hashid,$section,$cachetime);
 2753:     }
 2754:     #
 2755:     # Presumedly there will be few matching roles from the above
 2756:     # loop and the sorting time will be negligible.
 2757:     if (scalar(keys(%Pending))) {
 2758:         my ($time) = sort {$a <=> $b} keys(%Pending);
 2759:         return &do_cache_new('getsection',$hashid,$Pending{$time},$cachetime);
 2760:     } 
 2761:     if (scalar(keys(%Expired))) {
 2762:         my @sorted = sort {$a <=> $b} keys(%Expired);
 2763:         my $time = pop(@sorted);
 2764:         return &do_cache_new('getsection',$hashid,$Expired{$time},$cachetime);
 2765:     }
 2766:     return &do_cache_new('getsection',$hashid,'-1',$cachetime);
 2767: }
 2768: 
 2769: sub save_cache {
 2770:     &purge_remembered();
 2771:     #&Apache::loncommon::validate_page();
 2772:     undef(%env);
 2773:     undef($env_loaded);
 2774: }
 2775: 
 2776: my $to_remember=-1;
 2777: my %remembered;
 2778: my %accessed;
 2779: my $kicks=0;
 2780: my $hits=0;
 2781: sub make_key {
 2782:     my ($name,$id) = @_;
 2783:     if (length($id) > 65 
 2784: 	&& length(&escape($id)) > 200) {
 2785: 	$id=length($id).':'.&Digest::MD5::md5_hex($id);
 2786:     }
 2787:     return &escape($name.':'.$id);
 2788: }
 2789: 
 2790: sub devalidate_cache_new {
 2791:     my ($name,$id,$debug) = @_;
 2792:     if ($debug) { &Apache::lonnet::logthis("deleting $name:$id"); }
 2793:     my $remembered_id=$name.':'.$id;
 2794:     $id=&make_key($name,$id);
 2795:     $memcache->delete($id);
 2796:     delete($remembered{$remembered_id});
 2797:     delete($accessed{$remembered_id});
 2798: }
 2799: 
 2800: sub is_cached_new {
 2801:     my ($name,$id,$debug) = @_;
 2802:     my $remembered_id=$name.':'.$id; # this is to avoid make_key (which is slow) whenever possible
 2803:     if (exists($remembered{$remembered_id})) {
 2804: 	if ($debug) { &Apache::lonnet::logthis("Early return $remembered_id of $remembered{$remembered_id} "); }
 2805: 	$accessed{$remembered_id}=[&gettimeofday()];
 2806: 	$hits++;
 2807: 	return ($remembered{$remembered_id},1);
 2808:     }
 2809:     $id=&make_key($name,$id);
 2810:     my $value = $memcache->get($id);
 2811:     if (!(defined($value))) {
 2812: 	if ($debug) { &Apache::lonnet::logthis("getting $id is not defined"); }
 2813: 	return (undef,undef);
 2814:     }
 2815:     if ($value eq '__undef__') {
 2816: 	if ($debug) { &Apache::lonnet::logthis("getting $id is __undef__"); }
 2817: 	$value=undef;
 2818:     }
 2819:     &make_room($remembered_id,$value,$debug);
 2820:     if ($debug) { &Apache::lonnet::logthis("getting $id is $value"); }
 2821:     return ($value,1);
 2822: }
 2823: 
 2824: sub do_cache_new {
 2825:     my ($name,$id,$value,$time,$debug) = @_;
 2826:     my $remembered_id=$name.':'.$id;
 2827:     $id=&make_key($name,$id);
 2828:     my $setvalue=$value;
 2829:     if (!defined($setvalue)) {
 2830: 	$setvalue='__undef__';
 2831:     }
 2832:     if (!defined($time) ) {
 2833: 	$time=600;
 2834:     }
 2835:     if ($debug) { &Apache::lonnet::logthis("Setting $id to $value"); }
 2836:     my $result = $memcache->set($id,$setvalue,$time);
 2837:     if (! $result) {
 2838: 	&logthis("caching of id -> $id  failed");
 2839: 	$memcache->disconnect_all();
 2840:     }
 2841:     # need to make a copy of $value
 2842:     &make_room($remembered_id,$value,$debug);
 2843:     return $value;
 2844: }
 2845: 
 2846: sub make_room {
 2847:     my ($remembered_id,$value,$debug)=@_;
 2848: 
 2849:     $remembered{$remembered_id}= (ref($value)) ? &Storable::dclone($value)
 2850:                                     : $value;
 2851:     if ($to_remember<0) { return; }
 2852:     $accessed{$remembered_id}=[&gettimeofday()];
 2853:     if (scalar(keys(%remembered)) <= $to_remember) { return; }
 2854:     my $to_kick;
 2855:     my $max_time=0;
 2856:     foreach my $other (keys(%accessed)) {
 2857: 	if (&tv_interval($accessed{$other}) > $max_time) {
 2858: 	    $to_kick=$other;
 2859: 	    $max_time=&tv_interval($accessed{$other});
 2860: 	}
 2861:     }
 2862:     delete($remembered{$to_kick});
 2863:     delete($accessed{$to_kick});
 2864:     $kicks++;
 2865:     if ($debug) { &logthis("kicking $to_kick $max_time $kicks\n"); }
 2866:     return;
 2867: }
 2868: 
 2869: sub purge_remembered {
 2870:     #&logthis("Tossing ".scalar(keys(%remembered)));
 2871:     #&logthis(sprintf("%-20s is %s",'%remembered',length(&freeze(\%remembered))));
 2872:     undef(%remembered);
 2873:     undef(%accessed);
 2874: }
 2875: # ------------------------------------- Read an entry from a user's environment
 2876: 
 2877: sub userenvironment {
 2878:     my ($udom,$unam,@what)=@_;
 2879:     my $items;
 2880:     foreach my $item (@what) {
 2881:         $items.=&escape($item).'&';
 2882:     }
 2883:     $items=~s/\&$//;
 2884:     my %returnhash=();
 2885:     my $uhome = &homeserver($unam,$udom);
 2886:     unless ($uhome eq 'no_host') {
 2887:         my @answer=split(/\&/, 
 2888:             &reply('get:'.$udom.':'.$unam.':environment:'.$items,$uhome));
 2889:         if ($#answer==0 && $answer[0] =~ /^(con_lost|error:|no_such_host)/i) {
 2890:             return %returnhash;
 2891:         }
 2892:         my $i;
 2893:         for ($i=0;$i<=$#what;$i++) {
 2894: 	    $returnhash{$what[$i]}=&unescape($answer[$i]);
 2895:         }
 2896:     }
 2897:     return %returnhash;
 2898: }
 2899: 
 2900: # ---------------------------------------------------------- Get a studentphoto
 2901: sub studentphoto {
 2902:     my ($udom,$unam,$ext) = @_;
 2903:     my $home=&Apache::lonnet::homeserver($unam,$udom);
 2904:     if (defined($env{'request.course.id'})) {
 2905:         if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
 2906:             if ($udom eq $env{'course.'.$env{'request.course.id'}.'.domain'}) {
 2907:                 return(&retrievestudentphoto($udom,$unam,$ext)); 
 2908:             } else {
 2909:                 my ($result,$perm_reqd)=
 2910: 		    &Apache::lonnet::auto_photo_permission($unam,$udom);
 2911:                 if ($result eq 'ok') {
 2912:                     if (!($perm_reqd eq 'yes')) {
 2913:                         return(&retrievestudentphoto($udom,$unam,$ext));
 2914:                     }
 2915:                 }
 2916:             }
 2917:         }
 2918:     } else {
 2919:         my ($result,$perm_reqd) = 
 2920: 	    &Apache::lonnet::auto_photo_permission($unam,$udom);
 2921:         if ($result eq 'ok') {
 2922:             if (!($perm_reqd eq 'yes')) {
 2923:                 return(&retrievestudentphoto($udom,$unam,$ext));
 2924:             }
 2925:         }
 2926:     }
 2927:     return '/adm/lonKaputt/lonlogo_broken.gif';
 2928: }
 2929: 
 2930: sub retrievestudentphoto {
 2931:     my ($udom,$unam,$ext,$type) = @_;
 2932:     my $home=&Apache::lonnet::homeserver($unam,$udom);
 2933:     my $ret=&Apache::lonnet::reply("studentphoto:$udom:$unam:$ext:$type",$home);
 2934:     if ($ret eq 'ok') {
 2935:         my $url="/uploaded/$udom/$unam/internal/studentphoto.$ext";
 2936:         if ($type eq 'thumbnail') {
 2937:             $url="/uploaded/$udom/$unam/internal/studentphoto_tn.$ext"; 
 2938:         }
 2939:         my $tokenurl=&Apache::lonnet::tokenwrapper($url);
 2940:         return $tokenurl;
 2941:     } else {
 2942:         if ($type eq 'thumbnail') {
 2943:             return '/adm/lonKaputt/genericstudent_tn.gif';
 2944:         } else { 
 2945:             return '/adm/lonKaputt/lonlogo_broken.gif';
 2946:         }
 2947:     }
 2948: }
 2949: 
 2950: # -------------------------------------------------------------------- New chat
 2951: 
 2952: sub chatsend {
 2953:     my ($newentry,$anon,$group)=@_;
 2954:     my $cnum=$env{'course.'.$env{'request.course.id'}.'.num'};
 2955:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 2956:     my $chome=$env{'course.'.$env{'request.course.id'}.'.home'};
 2957:     &reply('chatsend:'.$cdom.':'.$cnum.':'.
 2958: 	   &escape($env{'user.domain'}.':'.$env{'user.name'}.':'.$anon.':'.
 2959: 		   &escape($newentry)).':'.$group,$chome);
 2960: }
 2961: 
 2962: # ------------------------------------------ Find current version of a resource
 2963: 
 2964: sub getversion {
 2965:     my $fname=&clutter(shift);
 2966:     unless ($fname=~m{^(/adm/wrapper|)/res/}) { return -1; }
 2967:     return &currentversion(&filelocation('',$fname));
 2968: }
 2969: 
 2970: sub currentversion {
 2971:     my $fname=shift;
 2972:     my $author=$fname;
 2973:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
 2974:     my ($udom,$uname)=split(/\//,$author);
 2975:     my $home=&homeserver($uname,$udom);
 2976:     if ($home eq 'no_host') { 
 2977:         return -1; 
 2978:     }
 2979:     my $answer=&reply("currentversion:$fname",$home);
 2980:     if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
 2981: 	return -1;
 2982:     }
 2983:     return $answer;
 2984: }
 2985: 
 2986: #
 2987: # Return special version number of resource if set by override, empty otherwise
 2988: #
 2989: sub usedversion {
 2990:     my $fname=shift;
 2991:     unless ($fname) { $fname=$env{'request.uri'}; }
 2992:     my ($urlversion)=($fname=~/\.(\d+)\.\w+$/);
 2993:     if ($urlversion) { return $urlversion; }
 2994:     return '';
 2995: }
 2996: 
 2997: # ----------------------------- Subscribe to a resource, return URL if possible
 2998: 
 2999: sub subscribe {
 3000:     my $fname=shift;
 3001:     if ($fname=~/\/(aboutme|syllabus|bulletinboard|smppg)$/) { return ''; }
 3002:     $fname=~s/[\n\r]//g;
 3003:     my $author=$fname;
 3004:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
 3005:     my ($udom,$uname)=split(/\//,$author);
 3006:     my $home=homeserver($uname,$udom);
 3007:     if ($home eq 'no_host') {
 3008:         return 'not_found';
 3009:     }
 3010:     my $answer=reply("sub:$fname",$home);
 3011:     if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
 3012: 	$answer.=' by '.$home;
 3013:     }
 3014:     return $answer;
 3015: }
 3016:     
 3017: # -------------------------------------------------------------- Replicate file
 3018: 
 3019: sub repcopy {
 3020:     my $filename=shift;
 3021:     $filename=~s/\/+/\//g;
 3022:     my $londocroot = $perlvar{'lonDocRoot'};
 3023:     if ($filename=~m{^\Q$londocroot/adm/\E}) { return 'ok'; }
 3024:     if ($filename=~m{^\Q/home/httpd/lonUsers/\E}) { return 'ok'; }
 3025:     if ($filename=~m{^\Q$londocroot/userfiles/\E} or
 3026: 	$filename=~m{^/*(uploaded|editupload)/}) {
 3027: 	return &repcopy_userfile($filename);
 3028:     }
 3029:     $filename=~s/[\n\r]//g;
 3030:     my $transname="$filename.in.transfer";
 3031: # FIXME: this should flock
 3032:     if ((-e $filename) || (-e $transname)) { return 'ok'; }
 3033:     my $remoteurl=subscribe($filename);
 3034:     if ($remoteurl =~ /^con_lost by/) {
 3035: 	   &logthis("Subscribe returned $remoteurl: $filename");
 3036:            return 'unavailable';
 3037:     } elsif ($remoteurl eq 'not_found') {
 3038: 	   #&logthis("Subscribe returned not_found: $filename");
 3039: 	   return 'not_found';
 3040:     } elsif ($remoteurl =~ /^rejected by/) {
 3041: 	   &logthis("Subscribe returned $remoteurl: $filename");
 3042:            return 'forbidden';
 3043:     } elsif ($remoteurl eq 'directory') {
 3044:            return 'ok';
 3045:     } else {
 3046:         my $author=$filename;
 3047:         $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
 3048:         my ($udom,$uname)=split(/\//,$author);
 3049:         my $home=homeserver($uname,$udom);
 3050:         unless ($home eq $perlvar{'lonHostID'}) {
 3051:            my @parts=split(/\//,$filename);
 3052:            my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 3053:            if ($path ne "$londocroot/res") {
 3054:                &logthis("Malconfiguration for replication: $filename");
 3055: 	       return 'bad_request';
 3056:            }
 3057:            my $count;
 3058:            for ($count=5;$count<$#parts;$count++) {
 3059:                $path.="/$parts[$count]";
 3060:                if ((-e $path)!=1) {
 3061: 		   mkdir($path,0777);
 3062:                }
 3063:            }
 3064:            my $request=new HTTP::Request('GET',"$remoteurl");
 3065:            my $response;
 3066:            if ($remoteurl =~ m{/raw/}) {
 3067:                $response=&LONCAPA::LWPReq::makerequest($home,$request,$transname,\%perlvar,'',0,1);
 3068:            } else {
 3069:                $response=&LONCAPA::LWPReq::makerequest($home,$request,$transname,\%perlvar,'',1);
 3070:            }
 3071:            if ($response->is_error()) {
 3072: 	       unlink($transname);
 3073:                my $message=$response->status_line;
 3074:                &logthis("<font color=\"blue\">WARNING:"
 3075:                        ." LWP get: $message: $filename</font>");
 3076:                return 'unavailable';
 3077:            } else {
 3078: 	       if ($remoteurl!~/\.meta$/) {
 3079:                   my $mrequest=new HTTP::Request('GET',$remoteurl.'.meta');
 3080:                   my $mresponse;
 3081:                   if ($remoteurl =~ m{/raw/}) {
 3082:                       $mresponse = &LONCAPA::LWPReq::makerequest($home,$mrequest,$filename.'.meta',\%perlvar,'',0,1);
 3083:                   } else {
 3084:                       $mresponse = &LONCAPA::LWPReq::makerequest($home,$mrequest,$filename.'.meta',\%perlvar,'',1);
 3085:                   }
 3086:                   if ($mresponse->is_error()) {
 3087: 		      unlink($filename.'.meta');
 3088:                       &logthis(
 3089:                      "<font color=\"yellow\">INFO: No metadata: $filename</font>");
 3090:                   }
 3091: 	       }
 3092:                rename($transname,$filename);
 3093:                return 'ok';
 3094:            }
 3095:        }
 3096:     }
 3097: }
 3098: 
 3099: # ------------------------------------------------ Get server side include body
 3100: sub ssi_body {
 3101:     my ($filelink,%form)=@_;
 3102:     if (! exists($form{'LONCAPA_INTERNAL_no_discussion'})) {
 3103:         $form{'LONCAPA_INTERNAL_no_discussion'}='true';
 3104:     }
 3105:     my $output='';
 3106:     my $response;
 3107:     if ($filelink=~/^https?\:/) {
 3108:        ($output,$response)=&externalssi($filelink);
 3109:     } else {
 3110:        $filelink .= $filelink=~/\?/ ? '&' : '?';
 3111:        $filelink .= 'inhibitmenu=yes';
 3112:        ($output,$response)=&ssi($filelink,%form);
 3113:     }
 3114:     $output=~s|//(\s*<!--)? BEGIN LON-CAPA Internal.+?// END LON-CAPA Internal\s*(-->)?\s||gs;
 3115:     $output=~s/^.*?\<body[^\>]*\>//si;
 3116:     $output=~s/\<\/body\s*\>.*?$//si;
 3117:     if (wantarray) {
 3118:         return ($output, $response);
 3119:     } else {
 3120:         return $output;
 3121:     }
 3122: }
 3123: 
 3124: # --------------------------------------------------------- Server Side Include
 3125: 
 3126: sub absolute_url {
 3127:     my ($host_name) = @_;
 3128:     my $protocol = ($ENV{'SERVER_PORT'} == 443?'https://':'http://');
 3129:     if ($host_name eq '') {
 3130: 	$host_name = $ENV{'SERVER_NAME'};
 3131:     }
 3132:     return $protocol.$host_name;
 3133: }
 3134: 
 3135: #
 3136: #   Server side include.
 3137: # Parameters:
 3138: #  fn     Possibly encrypted resource name/id.
 3139: #  form   Hash that describes how the rendering should be done
 3140: #         and other things.
 3141: # Returns:
 3142: #   Scalar context: The content of the response.
 3143: #   Array context:  2 element list of the content and the full response object.
 3144: #     
 3145: sub ssi {
 3146: 
 3147:     my ($fn,%form)=@_;
 3148:     my $request;
 3149: 
 3150:     $form{'no_update_last_known'}=1;
 3151:     &Apache::lonenc::check_encrypt(\$fn);
 3152:     if (%form) {
 3153:       $request=new HTTP::Request('POST',&absolute_url().$fn);
 3154:       $request->content(join('&',map { 
 3155:             my $name = escape($_);
 3156:             "$name=" . ( ref($form{$_}) eq 'ARRAY' 
 3157:             ? join("&$name=", map {escape($_) } @{$form{$_}}) 
 3158:             : &escape($form{$_}) );    
 3159:         } keys(%form)));
 3160:     } else {
 3161:       $request=new HTTP::Request('GET',&absolute_url().$fn);
 3162:     }
 3163: 
 3164:     $request->header(Cookie => $ENV{'HTTP_COOKIE'});
 3165:     my $lonhost = $perlvar{'lonHostID'};
 3166:     my $response= &LONCAPA::LWPReq::makerequest($lonhost,$request,'',\%perlvar);
 3167: 
 3168:     if (wantarray) {
 3169: 	return ($response->content, $response);
 3170:     } else {
 3171: 	return $response->content;
 3172:     }
 3173: }
 3174: 
 3175: sub externalssi {
 3176:     my ($url)=@_;
 3177:     my $request=new HTTP::Request('GET',$url);
 3178:     my $response = &LONCAPA::LWPReq::makerequest('',$request,'',\%perlvar);
 3179:     if (wantarray) {
 3180:         return ($response->content, $response);
 3181:     } else {
 3182:         return $response->content;
 3183:     }
 3184: }
 3185: 
 3186: 
 3187: # If the local copy of a replicated resource is outdated, trigger a  
 3188: # connection from the homeserver to flush the delayed queue. If no update 
 3189: # happens, remove local copies of outdated resource (and corresponding
 3190: # metadata file).
 3191: 
 3192: sub remove_stale_resfile {
 3193:     my ($url) = @_;
 3194:     my $removed;
 3195:     if ($url=~m{^/res/($match_domain)/($match_username)/}) {
 3196:         my $audom = $1;
 3197:         my $auname = $2;
 3198:         unless (($url =~ /\.\d+\.\w+$/) || ($url =~ m{^/res/lib/templates/})) {
 3199:             my $homeserver = &homeserver($auname,$audom);
 3200:             unless (($homeserver eq 'no_host') ||
 3201:                     (grep { $_ eq $homeserver } &current_machine_ids())) {
 3202:                 my $fname = &filelocation('',$url);
 3203:                 if (-e $fname) {
 3204:                     my $protocol = $protocol{$homeserver};
 3205:                     $protocol = 'http' if ($protocol ne 'https');
 3206:                     my $hostname = &hostname($homeserver);
 3207:                     if ($hostname) {
 3208:                         my $uri = &declutter($url);
 3209:                         my $request=new HTTP::Request('HEAD',$protocol.'://'.$hostname.'/raw/'.$uri);
 3210:                         my $response = &LONCAPA::LWPReq::makerequest($homeserver,$request,'',\%perlvar,5,0,1);
 3211:                         if ($response->is_success()) {
 3212:                             my $remmodtime = &HTTP::Date::str2time( $response->header('Last-modified') );
 3213:                             my $locmodtime = (stat($fname))[9];
 3214:                             if ($locmodtime < $remmodtime) {
 3215:                                 my $stale;
 3216:                                 my $answer = &reply('pong',$homeserver);
 3217:                                 if ($answer eq $homeserver.':'.$perlvar{'lonHostID'}) {
 3218:                                     sleep(0.2);
 3219:                                     $locmodtime = (stat($fname))[9];
 3220:                                     if ($locmodtime < $remmodtime) {
 3221:                                         my $posstransfer = $fname.'.in.transfer';
 3222:                                         if ((-e $posstransfer) && ($remmodtime < (stat($posstransfer))[9])) {
 3223:                                             $removed = 1;
 3224:                                         } else {
 3225:                                             $stale = 1;
 3226:                                         }
 3227:                                     } else {
 3228:                                         $removed = 1;
 3229:                                     }
 3230:                                 } else {
 3231:                                     $stale = 1;
 3232:                                 }
 3233:                                 if ($stale) {
 3234:                                     unlink($fname);
 3235:                                     if ($uri!~/\.meta$/) {
 3236:                                         unlink($fname.'.meta');
 3237:                                     }
 3238:                                     &reply("unsub:$fname",$homeserver);
 3239:                                     $removed = 1;
 3240:                                 }
 3241:                             }
 3242:                         }
 3243:                     }
 3244:                 }
 3245:             }
 3246:         }
 3247:     }
 3248:     return $removed;
 3249: }
 3250: 
 3251: # -------------------------------- Allow a /uploaded/ URI to be vouched for
 3252: 
 3253: sub allowuploaded {
 3254:     my ($srcurl,$url)=@_;
 3255:     $url=&clutter(&declutter($url));
 3256:     my $dir=$url;
 3257:     $dir=~s/\/[^\/]+$//;
 3258:     my %httpref=();
 3259:     my $httpurl=&hreflocation('',$url);
 3260:     $httpref{'httpref.'.$httpurl}=$srcurl;
 3261:     &Apache::lonnet::appenv(\%httpref);
 3262: }
 3263: 
 3264: #
 3265: # Determine if the current user should be able to edit a particular resource,
 3266: # when viewing in course context.
 3267: # (a) When viewing resource used to determine if "Edit" item is included in 
 3268: #     Functions.
 3269: # (b) When displaying folder contents in course editor, used to determine if
 3270: #     "Edit" link will be displayed alongside resource.
 3271: #
 3272: #  input: six args -- filename (decluttered), course number, course domain,
 3273: #                   url, symb (if registered) and group (if this is a group
 3274: #                   item -- e.g., bulletin board, group page etc.).
 3275: #  output: array of five scalars -- 
 3276: #          $cfile -- url for file editing if editable on current server
 3277: #          $home -- homeserver of resource (i.e., for author if published,
 3278: #                                           or course if uploaded.).
 3279: #          $switchserver --  1 if server switch will be needed.
 3280: #          $forceedit -- 1 if icon/link should be to go to edit mode 
 3281: #          $forceview -- 1 if icon/link should be to go to view mode
 3282: #
 3283: 
 3284: sub can_edit_resource {
 3285:     my ($file,$cnum,$cdom,$resurl,$symb,$group) = @_;
 3286:     my ($cfile,$home,$switchserver,$forceedit,$forceview,$uploaded,$incourse);
 3287: #
 3288: # For aboutme pages user can only edit his/her own.
 3289: #
 3290:     if ($resurl =~ m{^/?adm/($match_domain)/($match_username)/aboutme$}) {
 3291:         my ($sdom,$sname) = ($1,$2);
 3292:         if (($sdom eq $env{'user.domain'}) && ($sname eq $env{'user.name'})) {
 3293:             $home = $env{'user.home'};
 3294:             $cfile = $resurl;
 3295:             if ($env{'form.forceedit'}) {
 3296:                 $forceview = 1;
 3297:             } else {
 3298:                 $forceedit = 1;
 3299:             }
 3300:             return ($cfile,$home,$switchserver,$forceedit,$forceview);
 3301:         } else {
 3302:             return;
 3303:         }
 3304:     }
 3305: 
 3306:     if ($env{'request.course.id'}) {
 3307:         my $crsedit = &Apache::lonnet::allowed('mdc',$env{'request.course.id'});
 3308:         if ($group ne '') {
 3309: # if this is a group homepage or group bulletin board, check group privs
 3310:             my $allowed = 0;
 3311:             if ($resurl =~ m{^/?adm/$cdom/$cnum/$group/smppg$}) {
 3312:                 if ((&allowed('mdg',$env{'request.course.id'}.
 3313:                               ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''))) ||
 3314:                         (&allowed('mgh',$env{'request.course.id'}.'/'.$group)) || $crsedit) {
 3315:                     $allowed = 1;
 3316:                 }
 3317:             } elsif ($resurl =~ m{^/?adm/$cdom/$cnum/\d+/bulletinboard$}) {
 3318:                 if ((&allowed('mdg',$env{'request.course.id'}.($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''))) ||
 3319:                         (&allowed('cgb',$env{'request.course.id'}.'/'.$group)) || $crsedit) {
 3320:                     $allowed = 1;
 3321:                 }
 3322:             }
 3323:             if ($allowed) {
 3324:                 $home=&homeserver($cnum,$cdom);
 3325:                 if ($env{'form.forceedit'}) {
 3326:                     $forceview = 1;
 3327:                 } else {
 3328:                     $forceedit = 1;
 3329:                 }
 3330:                 $cfile = $resurl;
 3331:             } else {
 3332:                 return;
 3333:             }
 3334:         } else {
 3335:             if ($resurl =~ m{^/?adm/viewclasslist$}) {
 3336:                 unless (&Apache::lonnet::allowed('opa',$env{'request.course.id'})) {
 3337:                     return;
 3338:                 }
 3339:             } elsif (!$crsedit) {
 3340: #
 3341: # No edit allowed where CC has switched to student role.
 3342: #
 3343:                 return;
 3344:             }
 3345:         }
 3346:     }
 3347: 
 3348:     if ($file ne '') {
 3349:         if (($cnum =~ /$match_courseid/) && ($cdom =~ /$match_domain/)) {
 3350:             if (&is_course_upload($file,$cnum,$cdom)) {
 3351:                 $uploaded = 1;
 3352:                 $incourse = 1;
 3353:                 if ($file =~/\.(htm|html|css|js|txt)$/) {
 3354:                     $cfile = &hreflocation('',$file);
 3355:                     if ($env{'form.forceedit'}) {
 3356:                         $forceview = 1;
 3357:                     } else {
 3358:                         $forceedit = 1;
 3359:                     }
 3360:                 }
 3361:             } elsif ($resurl =~ m{^/public/$cdom/$cnum/syllabus}) {
 3362:                 $incourse = 1;
 3363:                 if ($env{'form.forceedit'}) {
 3364:                     $forceview = 1;
 3365:                 } else {
 3366:                     $forceedit = 1;
 3367:                 }
 3368:                 $cfile = $resurl;
 3369:             } elsif (($resurl ne '') && (&is_on_map($resurl))) { 
 3370:                 if ($resurl =~ m{^/adm/$match_domain/$match_username/\d+/smppg|bulletinboard$}) {
 3371:                     $incourse = 1;
 3372:                     if ($env{'form.forceedit'}) {
 3373:                         $forceview = 1;
 3374:                     } else {
 3375:                         $forceedit = 1;
 3376:                     }
 3377:                     $cfile = $resurl;
 3378:                 } elsif ($resurl eq '/res/lib/templates/simpleproblem.problem') {
 3379:                     $incourse = 1;
 3380:                     $cfile = $resurl.'/smpedit';
 3381:                 } elsif ($resurl =~ m{^/adm/wrapper/ext/}) {
 3382:                     $incourse = 1;
 3383:                     if ($env{'form.forceedit'}) {
 3384:                         $forceview = 1;
 3385:                     } else {
 3386:                         $forceedit = 1;
 3387:                     }
 3388:                     $cfile = $resurl;
 3389:                 } elsif ($resurl =~ m{^/adm/wrapper/adm/$cdom/$cnum/\d+/ext\.tool$}) {
 3390:                     $incourse = 1;
 3391:                     if ($env{'form.forceedit'}) {
 3392:                         $forceview = 1;
 3393:                     } else {
 3394:                         $forceedit = 1;
 3395:                     }
 3396:                     $cfile = $resurl;
 3397:                 } elsif ($resurl =~ m{^/?adm/viewclasslist$}) {
 3398:                     $incourse = 1;
 3399:                     if ($env{'form.forceedit'}) {
 3400:                         $forceview = 1;
 3401:                     } else {
 3402:                         $forceedit = 1;
 3403:                     }
 3404:                     $cfile = ($resurl =~ m{^/} ? $resurl : "/$resurl");
 3405:                 }
 3406:             } elsif ($resurl eq '/res/lib/templates/simpleproblem.problem/smpedit') {
 3407:                 my $template = '/res/lib/templates/simpleproblem.problem';
 3408:                 if (&is_on_map($template)) { 
 3409:                     $incourse = 1;
 3410:                     $forceview = 1;
 3411:                     $cfile = $template;
 3412:                 }
 3413:             } elsif (($resurl =~ m{^/adm/wrapper/ext/}) && ($env{'form.folderpath'} =~ /^supplemental/)) {
 3414:                     $incourse = 1;
 3415:                     if ($env{'form.forceedit'}) {
 3416:                         $forceview = 1;
 3417:                     } else {
 3418:                         $forceedit = 1;
 3419:                     }
 3420:                     $cfile = $resurl;
 3421:             } elsif (($resurl =~ m{^/adm/wrapper/adm/$cdom/$cnum/\d+/ext\.tool$}) && ($env{'form.folderpath'} =~ /^supplemental/)) {
 3422:                 $incourse = 1;
 3423:                 if ($env{'form.forceedit'}) {
 3424:                     $forceview = 1;
 3425:                 } else {
 3426:                     $forceedit = 1;
 3427:                 }
 3428:                 $cfile = $resurl;
 3429:             } elsif (($resurl eq '/adm/extresedit') && ($symb || $env{'form.folderpath'})) {
 3430:                 $incourse = 1;
 3431:                 $forceview = 1;
 3432:                 if ($symb) {
 3433:                     my ($map,$id,$res)=&decode_symb($symb);
 3434:                     $env{'request.symb'} = $symb;
 3435:                     $cfile = &clutter($res);
 3436:                 } else {
 3437:                     $cfile = $env{'form.suppurl'};
 3438:                     my $escfile = &unescape($cfile);
 3439:                     if ($escfile =~ m{^/adm/$cdom/$cnum/\d+/ext\.tool$}) {
 3440:                         $cfile = '/adm/wrapper'.$escfile;
 3441:                     } else {
 3442:                         $escfile =~ s{^http://}{};
 3443:                         $cfile = &escape("/adm/wrapper/ext/$escfile");
 3444:                     }
 3445:                 }
 3446:             } elsif ($resurl =~ m{^/?adm/viewclasslist$}) {
 3447:                 if ($env{'form.forceedit'}) {
 3448:                     $forceview = 1;
 3449:                 } else {
 3450:                     $forceedit = 1;
 3451:                 }
 3452:                 $cfile = ($resurl =~ m{^/} ? $resurl : "/$resurl");
 3453:             }
 3454:         }
 3455:         if ($uploaded || $incourse) {
 3456:             $home=&homeserver($cnum,$cdom);
 3457:         } elsif ($file !~ m{/$}) {
 3458:             $file=~s{^(priv/$match_domain/$match_username)}{/$1};
 3459:             $file=~s{^($match_domain/$match_username)}{/priv/$1};
 3460:             # Check that the user has permission to edit this resource
 3461:             my $setpriv = 1;
 3462:             my ($cfuname,$cfudom)=&constructaccess($file,$setpriv);
 3463:             if (defined($cfudom)) {
 3464:                 $home=&homeserver($cfuname,$cfudom);
 3465:                 $cfile=$file;
 3466:             }
 3467:         }
 3468:         if (($cfile ne '') && (!$incourse || $uploaded) && 
 3469:             (($home ne '') && ($home ne 'no_host'))) {
 3470:             my @ids=&current_machine_ids();
 3471:             unless (grep(/^\Q$home\E$/,@ids)) {
 3472:                 $switchserver=1;
 3473:             }
 3474:         }
 3475:     }
 3476:     return ($cfile,$home,$switchserver,$forceedit,$forceview);
 3477: }
 3478: 
 3479: sub is_course_upload {
 3480:     my ($file,$cnum,$cdom) = @_;
 3481:     my $uploadpath = &LONCAPA::propath($cdom,$cnum);
 3482:     $uploadpath =~ s{^\/}{};
 3483:     if (($file =~ m{^\Q$uploadpath\E/userfiles/(docs|supplemental)/}) ||
 3484:         ($file =~ m{^userfiles/\Q$cdom\E/\Q$cnum\E/(docs|supplemental)/})) {
 3485:         return 1;
 3486:     }
 3487:     return;
 3488: }
 3489: 
 3490: sub in_course {
 3491:     my ($udom,$uname,$cdom,$cnum,$type,$hideprivileged) = @_;
 3492:     if ($hideprivileged) {
 3493:         my $skipuser;
 3494:         my %coursehash = &coursedescription($cdom.'_'.$cnum);
 3495:         my @possdoms = ($cdom);  
 3496:         if ($coursehash{'checkforpriv'}) { 
 3497:             push(@possdoms,split(/,/,$coursehash{'checkforpriv'})); 
 3498:         }
 3499:         if (&privileged($uname,$udom,\@possdoms)) {
 3500:             $skipuser = 1;
 3501:             if ($coursehash{'nothideprivileged'}) {
 3502:                 foreach my $item (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
 3503:                     my $user;
 3504:                     if ($item =~ /:/) {
 3505:                         $user = $item;
 3506:                     } else {
 3507:                         $user = join(':',split(/[\@]/,$item));
 3508:                     }
 3509:                     if ($user eq $uname.':'.$udom) {
 3510:                         undef($skipuser);
 3511:                         last;
 3512:                     }
 3513:                 }
 3514:             }
 3515:             if ($skipuser) {
 3516:                 return 0;
 3517:             }
 3518:         }
 3519:     }
 3520:     $type ||= 'any';
 3521:     if (!defined($cdom) || !defined($cnum)) {
 3522:         my $cid  = $env{'request.course.id'};
 3523:         $cdom = $env{'course.'.$cid.'.domain'};
 3524:         $cnum = $env{'course.'.$cid.'.num'};
 3525:     }
 3526:     my $typesref;
 3527:     if (($type eq 'any') || ($type eq 'all')) {
 3528:         $typesref = ['active','previous','future'];
 3529:     } elsif ($type eq 'previous' || $type eq 'future') {
 3530:         $typesref = [$type];
 3531:     }
 3532:     my %roles = &get_my_roles($uname,$udom,'userroles',
 3533:                               $typesref,undef,[$cdom]);
 3534:     my ($tmp) = keys(%roles);
 3535:     return 0 if ($tmp =~ /^(con_lost|error|no_such_host)/i);
 3536:     my @course_roles = grep(/^\Q$cnum\E:\Q$cdom\E:/, keys(%roles));
 3537:     if (@course_roles > 0) {
 3538:         return 1;
 3539:     }
 3540:     return 0;
 3541: }
 3542: 
 3543: # --------- File operations in /home/httpd/html/userfiles/$domain/1/2/3/$course
 3544: # input: action, courseID, current domain, intended
 3545: #        path to file, source of file, instruction to parse file for objects,
 3546: #        ref to hash for embedded objects,
 3547: #        ref to hash for codebase of java objects.
 3548: #        reference to scalar to accommodate mime type determined
 3549: #          from File::MMagic if $parser = parse.
 3550: #
 3551: # output: url to file (if action was uploaddoc), 
 3552: #         ok if successful, or diagnostic message otherwise (if action was propagate or copy)
 3553: #
 3554: # Allows directory structure to be used within lonUsers/../userfiles/ for a 
 3555: # course.
 3556: #
 3557: # action = propagate - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
 3558: #          will be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles in
 3559: #          course's home server.
 3560: #
 3561: # action = copy - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file will
 3562: #          be copied from $source (current location) to 
 3563: #          /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
 3564: #         and will then be copied to
 3565: #          /home/httpd/lonUsers/$domain/1/2/3/$course/userfiles/$file in
 3566: #         course's home server.
 3567: #
 3568: # action = uploaddoc - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
 3569: #         will be retrived from $env{form.uploaddoc} (from DOCS interface) to
 3570: #         /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
 3571: #         and will then be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles/$file
 3572: #         in course's home server.
 3573: #
 3574: 
 3575: sub process_coursefile {
 3576:     my ($action,$docuname,$docudom,$file,$source,$parser,$allfiles,$codebase,
 3577:         $mimetype)=@_;
 3578:     my $fetchresult;
 3579:     my $home=&homeserver($docuname,$docudom);
 3580:     if ($action eq 'propagate') {
 3581:         $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
 3582: 			     $home);
 3583:     } else {
 3584:         my $fpath = '';
 3585:         my $fname = $file;
 3586:         ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
 3587:         $fpath=$docudom.'/'.$docuname.'/'.$fpath;
 3588:         my $filepath = &build_filepath($fpath);
 3589:         if ($action eq 'copy') {
 3590:             if ($source eq '') {
 3591:                 $fetchresult = 'no source file';
 3592:                 return $fetchresult;
 3593:             } else {
 3594:                 my $destination = $filepath.'/'.$fname;
 3595:                 rename($source,$destination);
 3596:                 $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
 3597:                                  $home);
 3598:             }
 3599:         } elsif ($action eq 'uploaddoc') {
 3600:             open(my $fh,'>'.$filepath.'/'.$fname);
 3601:             print $fh $env{'form.'.$source};
 3602:             close($fh);
 3603:             if ($parser eq 'parse') {
 3604:                 my $mm = new File::MMagic;
 3605:                 my $type = $mm->checktype_filename($filepath.'/'.$fname);
 3606:                 if ($type eq 'text/html') {
 3607:                     my $parse_result = &extract_embedded_items($filepath.'/'.$fname,$allfiles,$codebase);
 3608:                     unless ($parse_result eq 'ok') {
 3609:                         &logthis('Failed to parse '.$filepath.'/'.$fname.' for embedded media: '.$parse_result);
 3610:                     }
 3611:                 }
 3612:                 if (ref($mimetype)) {
 3613:                     $$mimetype = $type;
 3614:                 } 
 3615:             }
 3616:             $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
 3617:                                  $home);
 3618:             if ($fetchresult eq 'ok') {
 3619:                 return '/uploaded/'.$fpath.'/'.$fname;
 3620:             } else {
 3621:                 &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
 3622:                         ' to host '.$home.': '.$fetchresult);
 3623:                 return '/adm/notfound.html';
 3624:             }
 3625:         }
 3626:     }
 3627:     unless ( $fetchresult eq 'ok') {
 3628:         &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
 3629:              ' to host '.$home.': '.$fetchresult);
 3630:     }
 3631:     return $fetchresult;
 3632: }
 3633: 
 3634: sub build_filepath {
 3635:     my ($fpath) = @_;
 3636:     my $filepath=$perlvar{'lonDocRoot'}.'/userfiles';
 3637:     unless ($fpath eq '') {
 3638:         my @parts=split('/',$fpath);
 3639:         foreach my $part (@parts) {
 3640:             $filepath.= '/'.$part;
 3641:             if ((-e $filepath)!=1) {
 3642:                 mkdir($filepath,0777);
 3643:             }
 3644:         }
 3645:     }
 3646:     return $filepath;
 3647: }
 3648: 
 3649: sub store_edited_file {
 3650:     my ($primary_url,$content,$docudom,$docuname,$fetchresult) = @_;
 3651:     my $file = $primary_url;
 3652:     $file =~ s#^/uploaded/$docudom/$docuname/##;
 3653:     my $fpath = '';
 3654:     my $fname = $file;
 3655:     ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
 3656:     $fpath=$docudom.'/'.$docuname.'/'.$fpath;
 3657:     my $filepath = &build_filepath($fpath);
 3658:     open(my $fh,'>'.$filepath.'/'.$fname);
 3659:     print $fh $content;
 3660:     close($fh);
 3661:     my $home=&homeserver($docuname,$docudom);
 3662:     $$fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
 3663: 			  $home);
 3664:     if ($$fetchresult eq 'ok') {
 3665:         return '/uploaded/'.$fpath.'/'.$fname;
 3666:     } else {
 3667:         &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
 3668: 		 ' to host '.$home.': '.$$fetchresult);
 3669:         return '/adm/notfound.html';
 3670:     }
 3671: }
 3672: 
 3673: sub clean_filename {
 3674:     my ($fname,$args)=@_;
 3675: # Replace Windows backslashes by forward slashes
 3676:     $fname=~s/\\/\//g;
 3677:     if (!$args->{'keep_path'}) {
 3678:         # Get rid of everything but the actual filename
 3679: 	$fname=~s/^.*\/([^\/]+)$/$1/;
 3680:     }
 3681: # Replace spaces by underscores
 3682:     $fname=~s/\s+/\_/g;
 3683: # Replace all other weird characters by nothing
 3684:     $fname=~s{[^/\w\.\-]}{}g;
 3685: # Replace all .\d. sequences with _\d. so they no longer look like version
 3686: # numbers
 3687:     $fname=~s/\.(\d+)(?=\.)/_$1/g;
 3688:     return $fname;
 3689: }
 3690: # This Function checks if an Image's dimensions exceed either $resizewidth (width) 
 3691: # or $resizeheight (height) - both pixels. If so, the image is scaled to produce an 
 3692: # image with the same aspect ratio as the original, but with dimensions which do 
 3693: # not exceed $resizewidth and $resizeheight.
 3694:  
 3695: sub resizeImage {
 3696:     my ($img_path,$resizewidth,$resizeheight) = @_;
 3697:     my $ima = Image::Magick->new;
 3698:     my $resized;
 3699:     if (-e $img_path) {
 3700:         $ima->Read($img_path);
 3701:         if (($resizewidth =~ /^\d+$/) && ($resizeheight > 0)) {
 3702:             my $width = $ima->Get('width');
 3703:             my $height = $ima->Get('height');
 3704:             if ($width > $resizewidth) {
 3705: 	        my $factor = $width/$resizewidth;
 3706:                 my $newheight = $height/$factor;
 3707:                 $ima->Scale(width=>$resizewidth,height=>$newheight);
 3708:                 $resized = 1;
 3709:             }
 3710:         }
 3711:         if (($resizeheight =~ /^\d+$/) && ($resizeheight > 0)) {
 3712:             my $width = $ima->Get('width');
 3713:             my $height = $ima->Get('height');
 3714:             if ($height > $resizeheight) {
 3715:                 my $factor = $height/$resizeheight;
 3716:                 my $newwidth = $width/$factor;
 3717:                 $ima->Scale(width=>$newwidth,height=>$resizeheight);
 3718:                 $resized = 1;
 3719:             }
 3720:         }
 3721:         if ($resized) {
 3722:             $ima->Write($img_path);
 3723:         }
 3724:     }
 3725:     return;
 3726: }
 3727: 
 3728: # --------------- Take an uploaded file and put it into the userfiles directory
 3729: # input: $formname - the contents of the file are in $env{"form.$formname"}
 3730: #                    the desired filename is in $env{"form.$formname.filename"}
 3731: #        $context - possible values: coursedoc, existingfile, overwrite, 
 3732: #                                    canceloverwrite, or ''. 
 3733: #                   if 'coursedoc': upload to the current course
 3734: #                   if 'existingfile': write file to tmp/overwrites directory 
 3735: #                   if 'canceloverwrite': delete file written to tmp/overwrites directory
 3736: #                   $context is passed as argument to &finishuserfileupload
 3737: #        $subdir - directory in userfile to store the file into
 3738: #        $parser - instruction to parse file for objects ($parser = parse)    
 3739: #        $allfiles - reference to hash for embedded objects
 3740: #        $codebase - reference to hash for codebase of java objects
 3741: #        $desuname - username for permanent storage of uploaded file
 3742: #        $dsetudom - domain for permanaent storage of uploaded file
 3743: #        $thumbwidth - width (pixels) of thumbnail to make for uploaded image 
 3744: #        $thumbheight - height (pixels) of thumbnail to make for uploaded image
 3745: #        $resizewidth - width (pixels) to which to resize uploaded image
 3746: #        $resizeheight - height (pixels) to which to resize uploaded image
 3747: #        $mimetype - reference to scalar to accommodate mime type determined
 3748: #                    from File::MMagic.
 3749: # 
 3750: # output: url of file in userspace, or error: <message> 
 3751: #             or /adm/notfound.html if failure to upload occurse
 3752: 
 3753: sub userfileupload {
 3754:     my ($formname,$context,$subdir,$parser,$allfiles,$codebase,$destuname,
 3755:         $destudom,$thumbwidth,$thumbheight,$resizewidth,$resizeheight,$mimetype)=@_;
 3756:     if (!defined($subdir)) { $subdir='unknown'; }
 3757:     my $fname=$env{'form.'.$formname.'.filename'};
 3758:     $fname=&clean_filename($fname);
 3759:     # See if there is anything left
 3760:     unless ($fname) { return 'error: no uploaded file'; }
 3761:     # Files uploaded to help request form, or uploaded to "create course" page are handled differently
 3762:     if ((($formname eq 'screenshot') && ($subdir eq 'helprequests')) ||
 3763:         (($formname eq 'coursecreatorxml') && ($subdir eq 'batchupload')) ||
 3764:          ($context eq 'existingfile') || ($context eq 'canceloverwrite')) {
 3765:         my $now = time;
 3766:         my $filepath;
 3767:         if (($formname eq 'screenshot') && ($subdir eq 'helprequests')) {
 3768:              $filepath = 'tmp/helprequests/'.$now;
 3769:         } elsif (($formname eq 'coursecreatorxml') && ($subdir eq 'batchupload')) {
 3770:              $filepath = 'tmp/addcourse/'.$destudom.'/web/'.$env{'user.name'}.
 3771:                          '_'.$env{'user.domain'}.'/pending';
 3772:         } elsif (($context eq 'existingfile') || ($context eq 'canceloverwrite')) {
 3773:             my ($docuname,$docudom);
 3774:             if ($destudom =~ /^$match_domain$/) {
 3775:                 $docudom = $destudom;
 3776:             } else {
 3777:                 $docudom = $env{'user.domain'};
 3778:             }
 3779:             if ($destuname =~ /^$match_username$/) {
 3780:                 $docuname = $destuname;
 3781:             } else {
 3782:                 $docuname = $env{'user.name'};
 3783:             }
 3784:             if (exists($env{'form.group'})) {
 3785:                 $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
 3786:                 $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 3787:             }
 3788:             $filepath = 'tmp/overwrites/'.$docudom.'/'.$docuname.'/'.$subdir;
 3789:             if ($context eq 'canceloverwrite') {
 3790:                 my $tempfile =  $perlvar{'lonDaemons'}.'/'.$filepath.'/'.$fname;
 3791:                 if (-e  $tempfile) {
 3792:                     my @info = stat($tempfile);
 3793:                     if ($info[9] eq $env{'form.timestamp'}) {
 3794:                         unlink($tempfile);
 3795:                     }
 3796:                 }
 3797:                 return;
 3798:             }
 3799:         }
 3800:         # Create the directory if not present
 3801:         my @parts=split(/\//,$filepath);
 3802:         my $fullpath = $perlvar{'lonDaemons'};
 3803:         for (my $i=0;$i<@parts;$i++) {
 3804:             $fullpath .= '/'.$parts[$i];
 3805:             if ((-e $fullpath)!=1) {
 3806:                 mkdir($fullpath,0777);
 3807:             }
 3808:         }
 3809:         open(my $fh,'>'.$fullpath.'/'.$fname);
 3810:         print $fh $env{'form.'.$formname};
 3811:         close($fh);
 3812:         if ($context eq 'existingfile') {
 3813:             my @info = stat($fullpath.'/'.$fname);
 3814:             return ($fullpath.'/'.$fname,$info[9]);
 3815:         } else {
 3816:             return $fullpath.'/'.$fname;
 3817:         }
 3818:     }
 3819:     if ($subdir eq 'scantron') {
 3820:         $fname = 'scantron_orig_'.$fname;
 3821:     } else {
 3822:         $fname="$subdir/$fname";
 3823:     }
 3824:     if ($context eq 'coursedoc') {
 3825: 	my $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
 3826: 	my $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 3827:         if ($env{'form.folder'} =~ m/^(default|supplemental)/) {
 3828:             return &finishuserfileupload($docuname,$docudom,
 3829: 					 $formname,$fname,$parser,$allfiles,
 3830: 					 $codebase,$thumbwidth,$thumbheight,
 3831:                                          $resizewidth,$resizeheight,$context,$mimetype);
 3832:         } else {
 3833:             if ($env{'form.folder'}) {
 3834:                 $fname=$env{'form.folder'}.'/'.$fname;
 3835:             }
 3836:             return &process_coursefile('uploaddoc',$docuname,$docudom,
 3837: 				       $fname,$formname,$parser,
 3838: 				       $allfiles,$codebase,$mimetype);
 3839:         }
 3840:     } elsif (defined($destuname)) {
 3841:         my $docuname=$destuname;
 3842:         my $docudom=$destudom;
 3843: 	return &finishuserfileupload($docuname,$docudom,$formname,$fname,
 3844: 				     $parser,$allfiles,$codebase,
 3845:                                      $thumbwidth,$thumbheight,
 3846:                                      $resizewidth,$resizeheight,$context,$mimetype);
 3847:     } else {
 3848:         my $docuname=$env{'user.name'};
 3849:         my $docudom=$env{'user.domain'};
 3850:         if ((exists($env{'form.group'})) || ($context eq 'syllabus')) {
 3851:             $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
 3852:             $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 3853:         }
 3854: 	return &finishuserfileupload($docuname,$docudom,$formname,$fname,
 3855: 				     $parser,$allfiles,$codebase,
 3856:                                      $thumbwidth,$thumbheight,
 3857:                                      $resizewidth,$resizeheight,$context,$mimetype);
 3858:     }
 3859: }
 3860: 
 3861: sub finishuserfileupload {
 3862:     my ($docuname,$docudom,$formname,$fname,$parser,$allfiles,$codebase,
 3863:         $thumbwidth,$thumbheight,$resizewidth,$resizeheight,$context,$mimetype) = @_;
 3864:     my $path=$docudom.'/'.$docuname.'/';
 3865:     my $filepath=$perlvar{'lonDocRoot'};
 3866:   
 3867:     my ($fnamepath,$file,$fetchthumb);
 3868:     $file=$fname;
 3869:     if ($fname=~m|/|) {
 3870:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 3871: 	$path.=$fnamepath.'/';
 3872:     }
 3873:     my @parts=split(/\//,$filepath.'/userfiles/'.$path);
 3874:     my $count;
 3875:     for ($count=4;$count<=$#parts;$count++) {
 3876:         $filepath.="/$parts[$count]";
 3877:         if ((-e $filepath)!=1) {
 3878: 	    mkdir($filepath,0777);
 3879:         }
 3880:     }
 3881: 
 3882: # Save the file
 3883:     {
 3884: 	if (!open(FH,'>'.$filepath.'/'.$file)) {
 3885: 	    &logthis('Failed to create '.$filepath.'/'.$file);
 3886: 	    print STDERR ('Failed to create '.$filepath.'/'.$file."\n");
 3887: 	    return '/adm/notfound.html';
 3888: 	}
 3889:         if ($context eq 'overwrite') {
 3890:             my $source =  LONCAPA::tempdir().'/overwrites/'.$docudom.'/'.$docuname.'/'.$fname;
 3891:             my $target = $filepath.'/'.$file;
 3892:             if (-e $source) {
 3893:                 my @info = stat($source);
 3894:                 if ($info[9] eq $env{'form.timestamp'}) {   
 3895:                     unless (&File::Copy::move($source,$target)) {
 3896:                         &logthis('Failed to overwrite '.$filepath.'/'.$file);
 3897:                         return "Moving from $source failed";
 3898:                     }
 3899:                 } else {
 3900:                     return "Temporary file: $source had unexpected date/time for last modification";
 3901:                 }
 3902:             } else {
 3903:                 return "Temporary file: $source missing";
 3904:             }
 3905:         } elsif (!print FH ($env{'form.'.$formname})) {
 3906: 	    &logthis('Failed to write to '.$filepath.'/'.$file);
 3907: 	    print STDERR ('Failed to write to '.$filepath.'/'.$file."\n");
 3908: 	    return '/adm/notfound.html';
 3909: 	}
 3910: 	close(FH);
 3911:         if ($resizewidth && $resizeheight) {
 3912:             my $mm = new File::MMagic;
 3913:             my $mime_type = $mm->checktype_filename($filepath.'/'.$file);
 3914:             if ($mime_type =~ m{^image/}) {
 3915: 	        &resizeImage($filepath.'/'.$file,$resizewidth,$resizeheight);
 3916:             }  
 3917: 	}
 3918:     }
 3919:     if (($context eq 'coursedoc') || ($parser eq 'parse')) {
 3920:         if (ref($mimetype)) {
 3921:             if ($$mimetype eq '') {
 3922:                 my $mm = new File::MMagic;
 3923:                 my $type = $mm->checktype_filename($filepath.'/'.$file);
 3924:                 $$mimetype = $type;
 3925:             }
 3926:         }
 3927:     }
 3928:     if ($parser eq 'parse') {
 3929:         if ((ref($mimetype)) && ($$mimetype eq 'text/html')) {
 3930:             my $parse_result = &extract_embedded_items($filepath.'/'.$file,
 3931:                                                        $allfiles,$codebase);
 3932:             unless ($parse_result eq 'ok') {
 3933:                 &logthis('Failed to parse '.$filepath.$file.
 3934: 	   	         ' for embedded media: '.$parse_result); 
 3935:             }
 3936:         }
 3937:     }
 3938:     if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 3939:         my $input = $filepath.'/'.$file;
 3940:         my $output = $filepath.'/'.'tn-'.$file;
 3941:         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 3942:         system("convert -sample $thumbsize $input $output");
 3943:         if (-e $filepath.'/'.'tn-'.$file) {
 3944:             $fetchthumb  = 1; 
 3945:         }
 3946:     }
 3947:  
 3948: # Notify homeserver to grep it
 3949: #
 3950:     my $docuhome=&homeserver($docuname,$docudom);	
 3951:     my $fetchresult= &reply('fetchuserfile:'.$path.$file,$docuhome);
 3952:     if ($fetchresult eq 'ok') {
 3953:         if ($fetchthumb) {
 3954:             my $thumbresult= &reply('fetchuserfile:'.$path.'tn-'.$file,$docuhome);
 3955:             if ($thumbresult ne 'ok') {
 3956:                 &logthis('Failed to transfer '.$path.'tn-'.$file.' to host '.
 3957:                          $docuhome.': '.$thumbresult);
 3958:             }
 3959:         }
 3960: #
 3961: # Return the URL to it
 3962:         return '/uploaded/'.$path.$file;
 3963:     } else {
 3964:         &logthis('Failed to transfer '.$path.$file.' to host '.$docuhome.
 3965: 		 ': '.$fetchresult);
 3966:         return '/adm/notfound.html';
 3967:     }
 3968: }
 3969: 
 3970: sub extract_embedded_items {
 3971:     my ($fullpath,$allfiles,$codebase,$content) = @_;
 3972:     my @state = ();
 3973:     my (%lastids,%related,%shockwave,%flashvars);
 3974:     my %javafiles = (
 3975:                       codebase => '',
 3976:                       code => '',
 3977:                       archive => ''
 3978:                     );
 3979:     my %mediafiles = (
 3980:                       src => '',
 3981:                       movie => '',
 3982:                      );
 3983:     my $p;
 3984:     if ($content) {
 3985:         $p = HTML::LCParser->new($content);
 3986:     } else {
 3987:         $p = HTML::LCParser->new($fullpath);
 3988:     }
 3989:     while (my $t=$p->get_token()) {
 3990: 	if ($t->[0] eq 'S') {
 3991: 	    my ($tagname, $attr) = ($t->[1],$t->[2]);
 3992: 	    push(@state, $tagname);
 3993:             if (lc($tagname) eq 'allow') {
 3994:                 &add_filetype($allfiles,$attr->{'src'},'src');
 3995:             }
 3996: 	    if (lc($tagname) eq 'img') {
 3997: 		&add_filetype($allfiles,$attr->{'src'},'src');
 3998: 	    }
 3999: 	    if (lc($tagname) eq 'a') {
 4000:                 unless (($attr->{'href'} =~ /^#/) || ($attr->{'href'} eq '')) {
 4001:                     &add_filetype($allfiles,$attr->{'href'},'href');
 4002:                 }
 4003: 	    }
 4004:             if (lc($tagname) eq 'script') {
 4005:                 my $src;
 4006:                 if ($attr->{'archive'} =~ /\.jar$/i) {
 4007:                     &add_filetype($allfiles,$attr->{'archive'},'archive');
 4008:                 } else {
 4009:                     if ($attr->{'src'} ne '') {
 4010:                         $src = $attr->{'src'};
 4011:                         &add_filetype($allfiles,$src,'src');
 4012:                     }
 4013:                 }
 4014:                 my $text = $p->get_trimmed_text();
 4015:                 if ($text =~ /\Qswfobject.registerObject(\E([^\)]+)\)/) {
 4016:                     my @swfargs = split(/,/,$1);
 4017:                     foreach my $item (@swfargs) {
 4018:                         $item =~ s/["']//g;
 4019:                         $item =~ s/^\s+//;
 4020:                         $item =~ s/\s+$//;
 4021:                     }
 4022:                     if (($swfargs[0] ne'') && ($swfargs[2] ne '')) {
 4023:                         if (ref($related{$swfargs[0]}) eq 'ARRAY') {
 4024:                             push(@{$related{$swfargs[0]}},$swfargs[2]);
 4025:                         } else {
 4026:                             $related{$swfargs[0]} = [$swfargs[2]];
 4027:                         }
 4028:                     }
 4029:                 }
 4030:             }
 4031:             if (lc($tagname) eq 'link') {
 4032:                 if (lc($attr->{'rel'}) eq 'stylesheet') { 
 4033:                     &add_filetype($allfiles,$attr->{'href'},'href');
 4034:                 }
 4035:             }
 4036: 	    if (lc($tagname) eq 'object' ||
 4037: 		(lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')) {
 4038: 		foreach my $item (keys(%javafiles)) {
 4039: 		    $javafiles{$item} = '';
 4040: 		}
 4041:                 if ((lc($tagname) eq 'object') && (lc($state[-2]) ne 'object')) {
 4042:                     $lastids{lc($tagname)} = $attr->{'id'};
 4043:                 }
 4044: 	    }
 4045: 	    if (lc($state[-2]) eq 'object' && lc($tagname) eq 'param') {
 4046: 		my $name = lc($attr->{'name'});
 4047: 		foreach my $item (keys(%javafiles)) {
 4048: 		    if ($name eq $item) {
 4049: 			$javafiles{$item} = $attr->{'value'};
 4050: 			last;
 4051: 		    }
 4052: 		}
 4053:                 my $pathfrom;
 4054: 		foreach my $item (keys(%mediafiles)) {
 4055: 		    if ($name eq $item) {
 4056:                         $pathfrom = $attr->{'value'};
 4057:                         $shockwave{$lastids{lc($state[-2])}} = $pathfrom;
 4058: 			&add_filetype($allfiles,$pathfrom,$name);
 4059: 			last;
 4060: 		    }
 4061: 		}
 4062:                 if ($name eq 'flashvars') {
 4063:                     $flashvars{$lastids{lc($state[-2])}} = $attr->{'value'};
 4064:                 }
 4065:                 if ($pathfrom ne '') {
 4066:                     &embedded_dependency($allfiles,\%related,$lastids{lc($state[-2])},
 4067:                                          $pathfrom);
 4068:                 }
 4069: 	    }
 4070: 	    if (lc($tagname) eq 'embed' || lc($tagname) eq 'applet') {
 4071: 		foreach my $item (keys(%javafiles)) {
 4072: 		    if ($attr->{$item}) {
 4073: 			$javafiles{$item} = $attr->{$item};
 4074: 			last;
 4075: 		    }
 4076: 		}
 4077: 		foreach my $item (keys(%mediafiles)) {
 4078: 		    if ($attr->{$item}) {
 4079: 			&add_filetype($allfiles,$attr->{$item},$item);
 4080: 			last;
 4081: 		    }
 4082: 		}
 4083:                 if (lc($tagname) eq 'embed') {
 4084:                     if (($attr->{'name'} ne '') && ($attr->{'src'} ne '')) {
 4085:                         &embedded_dependency($allfiles,\%related,$attr->{'name'},
 4086:                                              $attr->{'src'});
 4087:                     }
 4088:                 }
 4089: 	    }
 4090:             if (lc($tagname) eq 'iframe') {
 4091:                 my $src = $attr->{'src'} ;
 4092:                 if (($src ne '') && ($src !~ m{^(/|https?://)})) {
 4093:                     &add_filetype($allfiles,$src,'src');
 4094:                 } elsif ($src =~ m{^/}) {
 4095:                     if ($env{'request.course.id'}) {
 4096:                         my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 4097:                         my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 4098:                         my $url = &hreflocation('',$fullpath);
 4099:                         if ($url =~ m{^/uploaded/$cdom/$cnum/docs/(\w+/\d+)/}) {
 4100:                             my $relpath = $1;
 4101:                             if ($src =~ m{^/uploaded/$cdom/$cnum/docs/\Q$relpath\E/(.+)$}) {
 4102:                                 &add_filetype($allfiles,$1,'src');
 4103:                             }
 4104:                         }
 4105:                     }
 4106:                 }
 4107:             }
 4108:             if ($t->[4] =~ m{/>$}) {
 4109:                 pop(@state);
 4110:             }
 4111: 	} elsif ($t->[0] eq 'E') {
 4112: 	    my ($tagname) = ($t->[1]);
 4113: 	    if ($javafiles{'codebase'} ne '') {
 4114: 		$javafiles{'codebase'} .= '/';
 4115: 	    }  
 4116: 	    if (lc($tagname) eq 'applet' ||
 4117: 		lc($tagname) eq 'object' ||
 4118: 		(lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')
 4119: 		) {
 4120: 		foreach my $item (keys(%javafiles)) {
 4121: 		    if ($item ne 'codebase' && $javafiles{$item} ne '') {
 4122: 			my $file=$javafiles{'codebase'}.$javafiles{$item};
 4123: 			&add_filetype($allfiles,$file,$item);
 4124: 		    }
 4125: 		}
 4126: 	    } 
 4127: 	    pop @state;
 4128: 	}
 4129:     }
 4130:     foreach my $id (sort(keys(%flashvars))) {
 4131:         if ($shockwave{$id} ne '') {
 4132:             my @pairs = split(/\&/,$flashvars{$id});
 4133:             foreach my $pair (@pairs) {
 4134:                 my ($key,$value) = split(/\=/,$pair);
 4135:                 if ($key eq 'thumb') {
 4136:                     &add_filetype($allfiles,$value,$key);
 4137:                 } elsif ($key eq 'content') {
 4138:                     my ($path) = ($shockwave{$id} =~ m{^(.+/)[^/]+$});
 4139:                     my ($ext) = ($value =~ /\.([^.]+)$/);
 4140:                     if ($ext ne '') {
 4141:                         &add_filetype($allfiles,$path.$value,$ext);
 4142:                     }
 4143:                 }
 4144:             }
 4145:         }
 4146:     }
 4147:     return 'ok';
 4148: }
 4149: 
 4150: sub add_filetype {
 4151:     my ($allfiles,$file,$type)=@_;
 4152:     if (exists($allfiles->{$file})) {
 4153: 	unless (grep/^\Q$type\E$/, @{$allfiles->{$file}}) {
 4154: 	    push(@{$allfiles->{$file}}, &escape($type));
 4155: 	}
 4156:     } else {
 4157: 	@{$allfiles->{$file}} = (&escape($type));
 4158:     }
 4159: }
 4160: 
 4161: sub embedded_dependency {
 4162:     my ($allfiles,$related,$identifier,$pathfrom) = @_;
 4163:     if ((ref($allfiles) eq 'HASH') && (ref($related) eq 'HASH')) {
 4164:         if (($identifier ne '') &&
 4165:             (ref($related->{$identifier}) eq 'ARRAY') &&
 4166:             ($pathfrom ne '')) {
 4167:             my ($path) = ($pathfrom =~ m{^(.+/)[^/]+$});
 4168:             foreach my $dep (@{$related->{$identifier}}) {
 4169:                 &add_filetype($allfiles,$path.$dep,'object');
 4170:             }
 4171:         }
 4172:     }
 4173:     return;
 4174: }
 4175: 
 4176: sub removeuploadedurl {
 4177:     my ($url)=@_;	
 4178:     my (undef,undef,$udom,$uname,$fname)=split('/',$url,5);    
 4179:     return &removeuserfile($uname,$udom,$fname);
 4180: }
 4181: 
 4182: sub removeuserfile {
 4183:     my ($docuname,$docudom,$fname)=@_;
 4184:     my $home=&homeserver($docuname,$docudom);    
 4185:     my $result = &reply("removeuserfile:$docudom/$docuname/$fname",$home);
 4186:     if ($result eq 'ok') {	
 4187:         if (($fname !~ /\.meta$/) && (&is_portfolio_file($fname))) {
 4188:             my $metafile = $fname.'.meta';
 4189:             my $metaresult = &removeuserfile($docuname,$docudom,$metafile); 
 4190: 	    my $url = "/uploaded/$docudom/$docuname/$fname";
 4191:             my ($file,$group) = (&parse_portfolio_url($url))[3,4];	   
 4192:             my $sqlresult = 
 4193:                 &update_portfolio_table($docuname,$docudom,$file,
 4194:                                         'portfolio_metadata',$group,
 4195:                                         'delete');
 4196:         }
 4197:     }
 4198:     return $result;
 4199: }
 4200: 
 4201: sub mkdiruserfile {
 4202:     my ($docuname,$docudom,$dir)=@_;
 4203:     my $home=&homeserver($docuname,$docudom);
 4204:     return &reply("mkdiruserfile:".&escape("$docudom/$docuname/$dir"),$home);
 4205: }
 4206: 
 4207: sub renameuserfile {
 4208:     my ($docuname,$docudom,$old,$new)=@_;
 4209:     my $home=&homeserver($docuname,$docudom);
 4210:     my $result = &reply("renameuserfile:$docudom:$docuname:".
 4211:                         &escape("$old").':'.&escape("$new"),$home);
 4212:     if ($result eq 'ok') {
 4213:         if (($old !~ /\.meta$/) && (&is_portfolio_file($old))) {
 4214:             my $oldmeta = $old.'.meta';
 4215:             my $newmeta = $new.'.meta';
 4216:             my $metaresult = 
 4217:                 &renameuserfile($docuname,$docudom,$oldmeta,$newmeta);
 4218: 	    my $url = "/uploaded/$docudom/$docuname/$old";
 4219:             my ($file,$group) = (&parse_portfolio_url($url))[3,4];
 4220:             my $sqlresult = 
 4221:                 &update_portfolio_table($docuname,$docudom,$file,
 4222:                                         'portfolio_metadata',$group,
 4223:                                         'delete');
 4224:         }
 4225:     }
 4226:     return $result;
 4227: }
 4228: 
 4229: # ------------------------------------------------------------------------- Log
 4230: 
 4231: sub log {
 4232:     my ($dom,$nam,$hom,$what)=@_;
 4233:     return critical("log:$dom:$nam:$what",$hom);
 4234: }
 4235: 
 4236: # ------------------------------------------------------------------ Course Log
 4237: #
 4238: # This routine flushes several buffers of non-mission-critical nature
 4239: #
 4240: 
 4241: sub flushcourselogs {
 4242:     &logthis('Flushing log buffers');
 4243: #
 4244: # course logs
 4245: # This is a log of all transactions in a course, which can be used
 4246: # for data mining purposes
 4247: #
 4248: # It also collects the courseid database, which lists last transaction
 4249: # times and course titles for all courseids
 4250: #
 4251:     my %courseidbuffer=();
 4252:     foreach my $crsid (keys(%courselogs)) {
 4253:         if (&reply('log:'.$coursedombuf{$crsid}.':'.$coursenumbuf{$crsid}.':'.
 4254: 		          &escape($courselogs{$crsid}),
 4255: 		          $coursehombuf{$crsid}) eq 'ok') {
 4256: 	    delete $courselogs{$crsid};
 4257:         } else {
 4258:             &logthis('Failed to flush log buffer for '.$crsid);
 4259:             if (length($courselogs{$crsid})>40000) {
 4260:                &logthis("<font color=\"blue\">WARNING: Buffer for ".$crsid.
 4261:                         " exceeded maximum size, deleting.</font>");
 4262:                delete $courselogs{$crsid};
 4263:             }
 4264:         }
 4265:         $courseidbuffer{$coursehombuf{$crsid}}{$crsid} = {
 4266:             'description' => $coursedescrbuf{$crsid},
 4267:             'inst_code'    => $courseinstcodebuf{$crsid},
 4268:             'type'        => $coursetypebuf{$crsid},
 4269:             'owner'       => $courseownerbuf{$crsid},
 4270:         };
 4271:     }
 4272: #
 4273: # Write course id database (reverse lookup) to homeserver of courses 
 4274: # Is used in pickcourse
 4275: #
 4276:     foreach my $crs_home (keys(%courseidbuffer)) {
 4277:         my $response = &courseidput(&host_domain($crs_home),
 4278:                                     $courseidbuffer{$crs_home},
 4279:                                     $crs_home,'timeonly');
 4280:     }
 4281: #
 4282: # File accesses
 4283: # Writes to the dynamic metadata of resources to get hit counts, etc.
 4284: #
 4285:     foreach my $entry (keys(%accesshash)) {
 4286:         if ($entry =~ /___count$/) {
 4287:             my ($dom,$name);
 4288:             ($dom,$name,undef)=
 4289: 		($entry=~m{___($match_domain)/($match_name)/(.*)___count$});
 4290:             if (! defined($dom) || $dom eq '' || 
 4291:                 ! defined($name) || $name eq '') {
 4292:                 my $cid = $env{'request.course.id'};
 4293:                 $dom  = $env{'request.'.$cid.'.domain'};
 4294:                 $name = $env{'request.'.$cid.'.num'};
 4295:             }
 4296:             my $value = $accesshash{$entry};
 4297:             my (undef,$url,undef) = ($entry =~ /^(.*)___(.*)___count$/);
 4298:             my %temphash=($url => $value);
 4299:             my $result = &inc('nohist_accesscount',\%temphash,$dom,$name);
 4300:             if ($result eq 'ok') {
 4301:                 delete $accesshash{$entry};
 4302:             }
 4303:         } else {
 4304:             my ($dom,$name) = ($entry=~m{___($match_domain)/($match_name)/(.*)___(\w+)$});
 4305:             if (($dom eq 'uploaded') || ($dom eq 'adm')) { next; }
 4306:             my %temphash=($entry => $accesshash{$entry});
 4307:             if (&put('nohist_resevaldata',\%temphash,$dom,$name) eq 'ok') {
 4308:                 delete $accesshash{$entry};
 4309:             }
 4310:         }
 4311:     }
 4312: #
 4313: # Roles
 4314: # Reverse lookup of user roles for course faculty/staff and co-authorship
 4315: #
 4316:     foreach my $entry (keys(%userrolehash)) {
 4317:         my ($role,$uname,$udom,$runame,$rudom,$rsec)=
 4318: 	    split(/\:/,$entry);
 4319:         if (&Apache::lonnet::put('nohist_userroles',
 4320:              { $role.':'.$uname.':'.$udom.':'.$rsec => $userrolehash{$entry} },
 4321:                 $rudom,$runame) eq 'ok') {
 4322: 	    delete $userrolehash{$entry};
 4323:         }
 4324:     }
 4325: #
 4326: # Reverse lookup of domain roles (dc, ad, li, sc, dh, da, au)
 4327: #
 4328:     my %domrolebuffer = ();
 4329:     foreach my $entry (keys(%domainrolehash)) {
 4330:         my ($role,$uname,$udom,$runame,$rudom,$rsec)=split(/:/,$entry);
 4331:         if ($domrolebuffer{$rudom}) {
 4332:             $domrolebuffer{$rudom}.='&'.&escape($entry).
 4333:                       '='.&escape($domainrolehash{$entry});
 4334:         } else {
 4335:             $domrolebuffer{$rudom}.=&escape($entry).
 4336:                       '='.&escape($domainrolehash{$entry});
 4337:         }
 4338:         delete $domainrolehash{$entry};
 4339:     }
 4340:     foreach my $dom (keys(%domrolebuffer)) {
 4341: 	my %servers;
 4342: 	if (defined(&domain($dom,'primary'))) {
 4343: 	    my $primary=&domain($dom,'primary');
 4344: 	    my $hostname=&hostname($primary);
 4345: 	    $servers{$primary} = $hostname;
 4346: 	} else { 
 4347: 	    %servers = &get_servers($dom,'library');
 4348: 	}
 4349: 	foreach my $tryserver (keys(%servers)) {
 4350: 	    if (&reply('domroleput:'.$dom.':'.
 4351: 		       $domrolebuffer{$dom},$tryserver) eq 'ok') {
 4352: 		last;
 4353: 	    } else {  
 4354: 		&logthis('Put of domain roles failed for '.$dom.' and  '.$tryserver);
 4355: 	    }
 4356:         }
 4357:     }
 4358:     $dumpcount++;
 4359: }
 4360: 
 4361: sub courselog {
 4362:     my $what=shift;
 4363:     $what=time.':'.$what;
 4364:     unless ($env{'request.course.id'}) { return ''; }
 4365:     $coursedombuf{$env{'request.course.id'}}=
 4366:        $env{'course.'.$env{'request.course.id'}.'.domain'};
 4367:     $coursenumbuf{$env{'request.course.id'}}=
 4368:        $env{'course.'.$env{'request.course.id'}.'.num'};
 4369:     $coursehombuf{$env{'request.course.id'}}=
 4370:        $env{'course.'.$env{'request.course.id'}.'.home'};
 4371:     $coursedescrbuf{$env{'request.course.id'}}=
 4372:        $env{'course.'.$env{'request.course.id'}.'.description'};
 4373:     $courseinstcodebuf{$env{'request.course.id'}}=
 4374:        $env{'course.'.$env{'request.course.id'}.'.internal.coursecode'};
 4375:     $courseownerbuf{$env{'request.course.id'}}=
 4376:        $env{'course.'.$env{'request.course.id'}.'.internal.courseowner'};
 4377:     $coursetypebuf{$env{'request.course.id'}}=
 4378:        $env{'course.'.$env{'request.course.id'}.'.type'};
 4379:     if (defined $courselogs{$env{'request.course.id'}}) {
 4380: 	$courselogs{$env{'request.course.id'}}.='&'.$what;
 4381:     } else {
 4382: 	$courselogs{$env{'request.course.id'}}.=$what;
 4383:     }
 4384:     if (length($courselogs{$env{'request.course.id'}})>4048) {
 4385: 	&flushcourselogs();
 4386:     }
 4387: }
 4388: 
 4389: sub courseacclog {
 4390:     my $fnsymb=shift;
 4391:     unless ($env{'request.course.id'}) { return ''; }
 4392:     my $what=$fnsymb.':'.$env{'user.name'}.':'.$env{'user.domain'};
 4393:     if ($fnsymb=~/$LONCAPA::assess_re/) {
 4394:         $what.=':POST';
 4395:         # FIXME: Probably ought to escape things....
 4396: 	foreach my $key (keys(%env)) {
 4397:             if ($key=~/^form\.(.*)/) {
 4398:                 my $formitem = $1;
 4399:                 if ($formitem =~ /^HWFILE(?:SIZE|TOOBIG)/) {
 4400:                     $what.=':'.$formitem.'='.$env{$key};
 4401:                 } elsif ($formitem !~ /^HWFILE(?:[^.]+)$/) {
 4402:                     $what.=':'.$formitem.'='.$env{$key};
 4403:                 }
 4404:             }
 4405:         }
 4406:     } elsif ($fnsymb =~ m:^/adm/searchcat:) {
 4407:         # FIXME: We should not be depending on a form parameter that someone
 4408:         # editing lonsearchcat.pm might change in the future.
 4409:         if ($env{'form.phase'} eq 'course_search') {
 4410:             $what.= ':POST';
 4411:             # FIXME: Probably ought to escape things....
 4412:             foreach my $element ('courseexp','crsfulltext','crsrelated',
 4413:                                  'crsdiscuss') {
 4414:                 $what.=':'.$element.'='.$env{'form.'.$element};
 4415:             }
 4416:         }
 4417:     }
 4418:     &courselog($what);
 4419: }
 4420: 
 4421: sub countacc {
 4422:     my $url=&declutter(shift);
 4423:     return if (! defined($url) || $url eq '');
 4424:     unless ($env{'request.course.id'}) { return ''; }
 4425: #
 4426: # Mark that this url was used in this course
 4427: #
 4428:     $accesshash{$env{'request.course.id'}.'___'.$url.'___course'}=1;
 4429: #
 4430: # Increase the access count for this resource in this child process
 4431: #
 4432:     my $key=$$.$processmarker.'_'.$dumpcount.'___'.$url.'___count';
 4433:     $accesshash{$key}++;
 4434: }
 4435: 
 4436: sub linklog {
 4437:     my ($from,$to)=@_;
 4438:     $from=&declutter($from);
 4439:     $to=&declutter($to);
 4440:     $accesshash{$from.'___'.$to.'___comefrom'}=1;
 4441:     $accesshash{$to.'___'.$from.'___goto'}=1;
 4442: }
 4443: 
 4444: sub statslog {
 4445:     my ($symb,$part,$users,$av_attempts,$degdiff)=@_;
 4446:     if ($users<2) { return; }
 4447:     my %dynstore=&LONCAPA::lonmetadata::dynamic_metadata_storage({
 4448:             'course'       => $env{'request.course.id'},
 4449:             'sections'     => '"all"',
 4450:             'num_students' => $users,
 4451:             'part'         => $part,
 4452:             'symb'         => $symb,
 4453:             'mean_tries'   => $av_attempts,
 4454:             'deg_of_diff'  => $degdiff});
 4455:     foreach my $key (keys(%dynstore)) {
 4456:         $accesshash{$key}=$dynstore{$key};
 4457:     }
 4458: }
 4459:   
 4460: sub userrolelog {
 4461:     my ($trole,$username,$domain,$area,$tstart,$tend)=@_;
 4462:     if ( $trole =~ /^(ca|aa|in|cc|ep|cr|ta|co)/ ) {
 4463:        my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
 4464:        $userrolehash
 4465:          {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
 4466:                     =$tend.':'.$tstart;
 4467:     }
 4468:     if ($env{'request.role'} =~ /dc\./ && $trole =~ /^(au|in|cc|ep|cr|ta|co)/) {
 4469:        $userrolehash
 4470:          {$trole.':'.$username.':'.$domain.':'.$env{'user.name'}.':'.$env{'user.domain'}.':'}
 4471:                     =$tend.':'.$tstart;
 4472:     }
 4473:     if ($trole =~ /^(dc|ad|li|au|dg|sc|dh|da)/ ) {
 4474:        my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
 4475:        $domainrolehash
 4476:          {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
 4477:                     = $tend.':'.$tstart;
 4478:     }
 4479: }
 4480: 
 4481: sub courserolelog {
 4482:     my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$selfenroll,$context)=@_;
 4483:     if ($area =~ m-^/($match_domain)/($match_courseid)/?([^/]*)-) {
 4484:         my $cdom = $1;
 4485:         my $cnum = $2;
 4486:         my $sec = $3;
 4487:         my $namespace = 'rolelog';
 4488:         my %storehash = (
 4489:                            role    => $trole,
 4490:                            start   => $tstart,
 4491:                            end     => $tend,
 4492:                            selfenroll => $selfenroll,
 4493:                            context    => $context,
 4494:                         );
 4495:         if ($trole eq 'gr') {
 4496:             $namespace = 'groupslog';
 4497:             $storehash{'group'} = $sec;
 4498:         } else {
 4499:             $storehash{'section'} = $sec;
 4500:         }
 4501:         &write_log('course',$namespace,\%storehash,$delflag,$username,
 4502:                    $domain,$cnum,$cdom);
 4503:         if (($trole ne 'st') || ($sec ne '')) {
 4504:             &devalidate_cache_new('getcourseroles',$cdom.'_'.$cnum);
 4505:         }
 4506:     }
 4507:     return;
 4508: }
 4509: 
 4510: sub domainrolelog {
 4511:     my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$context)=@_;
 4512:     if ($area =~ m{^/($match_domain)/$}) {
 4513:         my $cdom = $1;
 4514:         my $domconfiguser = &Apache::lonnet::get_domainconfiguser($cdom);
 4515:         my $namespace = 'rolelog';
 4516:         my %storehash = (
 4517:                            role    => $trole,
 4518:                            start   => $tstart,
 4519:                            end     => $tend,
 4520:                            context => $context,
 4521:                         );
 4522:         &write_log('domain',$namespace,\%storehash,$delflag,$username,
 4523:                    $domain,$domconfiguser,$cdom);
 4524:     }
 4525:     return;
 4526: 
 4527: }
 4528: 
 4529: sub coauthorrolelog {
 4530:     my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$context)=@_;
 4531:     if ($area =~ m{^/($match_domain)/($match_username)$}) {
 4532:         my $audom = $1;
 4533:         my $auname = $2;
 4534:         my $namespace = 'rolelog';
 4535:         my %storehash = (
 4536:                            role    => $trole,
 4537:                            start   => $tstart,
 4538:                            end     => $tend,
 4539:                            context => $context,
 4540:                         );
 4541:         &write_log('author',$namespace,\%storehash,$delflag,$username,
 4542:                    $domain,$auname,$audom);
 4543:     }
 4544:     return;
 4545: }
 4546: 
 4547: sub get_course_adv_roles {
 4548:     my ($cid,$codes) = @_;
 4549:     $cid=$env{'request.course.id'} unless (defined($cid));
 4550:     my %coursehash=&coursedescription($cid);
 4551:     my $crstype = &Apache::loncommon::course_type($cid);
 4552:     my %nothide=();
 4553:     foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
 4554:         if ($user !~ /:/) {
 4555: 	    $nothide{join(':',split(/[\@]/,$user))}=1;
 4556:         } else {
 4557:             $nothide{$user}=1;
 4558:         }
 4559:     }
 4560:     my @possdoms = ($coursehash{'domain'});
 4561:     if ($coursehash{'checkforpriv'}) {
 4562:         push(@possdoms,split(/,/,$coursehash{'checkforpriv'}));
 4563:     }
 4564:     my %returnhash=();
 4565:     my %dumphash=
 4566:             &dump('nohist_userroles',$coursehash{'domain'},$coursehash{'num'});
 4567:     my $now=time;
 4568:     my %privileged;
 4569:     foreach my $entry (keys(%dumphash)) {
 4570: 	my ($tend,$tstart)=split(/\:/,$dumphash{$entry});
 4571:         if (($tstart) && ($tstart<0)) { next; }
 4572:         if (($tend) && ($tend<$now)) { next; }
 4573:         if (($tstart) && ($now<$tstart)) { next; }
 4574:         my ($role,$username,$domain,$section)=split(/\:/,$entry);
 4575: 	if ($username eq '' || $domain eq '') { next; }
 4576:         if ((&privileged($username,$domain,\@possdoms)) &&
 4577:             (!$nothide{$username.':'.$domain})) { next; }
 4578: 	if ($role eq 'cr') { next; }
 4579:         if ($codes) {
 4580:             if ($section) { $role .= ':'.$section; }
 4581:             if ($returnhash{$role}) {
 4582:                 $returnhash{$role}.=','.$username.':'.$domain;
 4583:             } else {
 4584:                 $returnhash{$role}=$username.':'.$domain;
 4585:             }
 4586:         } else {
 4587:             my $key=&plaintext($role,$crstype);
 4588:             if ($section) { $key.=' ('.&Apache::lonlocal::mt('Section [_1]',$section).')'; }
 4589:             if ($returnhash{$key}) {
 4590: 	        $returnhash{$key}.=','.$username.':'.$domain;
 4591:             } else {
 4592:                 $returnhash{$key}=$username.':'.$domain;
 4593:             }
 4594:         }
 4595:     }
 4596:     return %returnhash;
 4597: }
 4598: 
 4599: sub get_my_roles {
 4600:     my ($uname,$udom,$context,$types,$roles,$roledoms,$withsec,$hidepriv)=@_;
 4601:     unless (defined($uname)) { $uname=$env{'user.name'}; }
 4602:     unless (defined($udom)) { $udom=$env{'user.domain'}; }
 4603:     my (%dumphash,%nothide);
 4604:     if ($context eq 'userroles') {
 4605:         %dumphash = &dump('roles',$udom,$uname);
 4606:     } else {
 4607:         %dumphash = &dump('nohist_userroles',$udom,$uname);
 4608:         if ($hidepriv) {
 4609:             my %coursehash=&coursedescription($udom.'_'.$uname);
 4610:             foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
 4611:                 if ($user !~ /:/) {
 4612:                     $nothide{join(':',split(/[\@]/,$user))} = 1;
 4613:                 } else {
 4614:                     $nothide{$user} = 1;
 4615:                 }
 4616:             }
 4617:         }
 4618:     }
 4619:     my %returnhash=();
 4620:     my $now=time;
 4621:     my %privileged;
 4622:     foreach my $entry (keys(%dumphash)) {
 4623:         my ($role,$tend,$tstart);
 4624:         if ($context eq 'userroles') {
 4625:             next if ($entry =~ /^rolesdef/);
 4626: 	    ($role,$tend,$tstart)=split(/_/,$dumphash{$entry});
 4627:         } else {
 4628:             ($tend,$tstart)=split(/\:/,$dumphash{$entry});
 4629:         }
 4630:         if (($tstart) && ($tstart<0)) { next; }
 4631:         my $status = 'active';
 4632:         if (($tend) && ($tend<=$now)) {
 4633:             $status = 'previous';
 4634:         } 
 4635:         if (($tstart) && ($now<$tstart)) {
 4636:             $status = 'future';
 4637:         }
 4638:         if (ref($types) eq 'ARRAY') {
 4639:             if (!grep(/^\Q$status\E$/,@{$types})) {
 4640:                 next;
 4641:             } 
 4642:         } else {
 4643:             if ($status ne 'active') {
 4644:                 next;
 4645:             }
 4646:         }
 4647:         my ($rolecode,$username,$domain,$section,$area);
 4648:         if ($context eq 'userroles') {
 4649:             ($area,$rolecode) = ($entry =~ /^(.+)_([^_]+)$/);
 4650:             (undef,$domain,$username,$section) = split(/\//,$area);
 4651:         } else {
 4652:             ($role,$username,$domain,$section) = split(/\:/,$entry);
 4653:         }
 4654:         if (ref($roledoms) eq 'ARRAY') {
 4655:             if (!grep(/^\Q$domain\E$/,@{$roledoms})) {
 4656:                 next;
 4657:             }
 4658:         }
 4659:         if (ref($roles) eq 'ARRAY') {
 4660:             if (!grep(/^\Q$role\E$/,@{$roles})) {
 4661:                 if ($role =~ /^cr\//) {
 4662:                     if (!grep(/^cr$/,@{$roles})) {
 4663:                         next;
 4664:                     }
 4665:                 } elsif ($role =~ /^gr\//) {
 4666:                     if (!grep(/^gr$/,@{$roles})) {
 4667:                         next;
 4668:                     }
 4669:                 } else {
 4670:                     next;
 4671:                 }
 4672:             }
 4673:         }
 4674:         if ($hidepriv) {
 4675:             my @privroles = ('dc','su');
 4676:             if ($context eq 'userroles') {
 4677:                 next if (grep(/^\Q$role\E$/,@privroles));
 4678:             } else {
 4679:                 my $possdoms = [$domain];
 4680:                 if (ref($roledoms) eq 'ARRAY') {
 4681:                    push(@{$possdoms},@{$roledoms}); 
 4682:                 }
 4683:                 if (&privileged($username,$domain,$possdoms,\@privroles)) {
 4684:                     if (!$nothide{$username.':'.$domain}) {
 4685:                         next;
 4686:                     }
 4687:                 }
 4688:             }
 4689:         }
 4690:         if ($withsec) {
 4691:             $returnhash{$username.':'.$domain.':'.$role.':'.$section} =
 4692:                 $tstart.':'.$tend;
 4693:         } else {
 4694:             $returnhash{$username.':'.$domain.':'.$role}=$tstart.':'.$tend;
 4695:         }
 4696:     }
 4697:     return %returnhash;
 4698: }
 4699: 
 4700: sub get_all_adhocroles {
 4701:     my ($dom) = @_;
 4702:     my @roles_by_num = ();
 4703:     my %domdefaults = &get_domain_defaults($dom);
 4704:     my (%description,%access_in_dom,%access_info);
 4705:     if (ref($domdefaults{'adhocroles'}) eq 'HASH') {
 4706:         my $count = 0;
 4707:         my %domcurrent = %{$domdefaults{'adhocroles'}};
 4708:         my %ordered;
 4709:         foreach my $role (sort(keys(%domcurrent))) {
 4710:             my ($order,$desc,$access_in_dom);
 4711:             if (ref($domcurrent{$role}) eq 'HASH') {
 4712:                 $order = $domcurrent{$role}{'order'};
 4713:                 $desc = $domcurrent{$role}{'desc'};
 4714:                 $access_in_dom{$role} = $domcurrent{$role}{'access'};
 4715:                 $access_info{$role} = $domcurrent{$role}{$access_in_dom{$role}};
 4716:             }
 4717:             if ($order eq '') {
 4718:                 $order = $count;
 4719:             }
 4720:             $ordered{$order} = $role;
 4721:             if ($desc ne '') {
 4722:                 $description{$role} = $desc;
 4723:             } else {
 4724:                 $description{$role}= $role;
 4725:             }
 4726:             $count++;
 4727:         }
 4728:         foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 4729:             push(@roles_by_num,$ordered{$item});
 4730:         }
 4731:     }
 4732:     return (\@roles_by_num,\%description,\%access_in_dom,\%access_info);
 4733: }
 4734: 
 4735: sub get_my_adhocroles {
 4736:     my ($cid,$checkreg) = @_;
 4737:     my ($cdom,$cnum,%info,@possroles,$description,$roles_by_num);
 4738:     if ($env{'request.course.id'} eq $cid) {
 4739:         $cdom = $env{'course.'.$cid.'.domain'};
 4740:         $cnum = $env{'course.'.$cid.'.num'};
 4741:         $info{'internal.coursecode'} = $env{'course.'.$cid.'.internal.coursecode'};
 4742:     } elsif ($cid =~ /^($match_domain)_($match_courseid)$/) {
 4743:         $cdom = $1;
 4744:         $cnum = $2;
 4745:         %info = &Apache::lonnet::get('environment',['internal.coursecode'],
 4746:                                      $cdom,$cnum);
 4747:     }
 4748:     if (($info{'internal.coursecode'} ne '') && ($checkreg)) {
 4749:         my $user = $env{'user.name'}.':'.$env{'user.domain'};
 4750:         my %rosterhash = &get('classlist',[$user],$cdom,$cnum);
 4751:         if ($rosterhash{$user} ne '') {
 4752:             my $type = (split(/:/,$rosterhash{$user}))[5];
 4753:             return ([],{}) if ($type eq 'auto');
 4754:         }
 4755:     }
 4756:     if (($cdom ne '') && ($cnum ne ''))  {
 4757:         if (($env{"user.role.dh./$cdom/"}) || ($env{"user.role.da./$cdom/"})) {
 4758:             my $then=$env{'user.login.time'};
 4759:             my $update=$env{'user.update.time'};
 4760:             if (!$update) {
 4761:                 $update = $then;
 4762:             }
 4763:             my @liveroles;
 4764:             foreach my $role ('dh','da') {
 4765:                 if ($env{"user.role.$role./$cdom/"}) {
 4766:                     my ($tstart,$tend)=split(/\./,$env{"user.role.$role./$cdom/"});
 4767:                     my $limit = $update;
 4768:                     if ($env{'request.role'} eq "$role./$cdom/") {
 4769:                         $limit = $then;
 4770:                     }
 4771:                     my $activerole = 1;
 4772:                     if ($tstart && $tstart>$limit) { $activerole = 0; }
 4773:                     if ($tend   && $tend  <$limit) { $activerole = 0; }
 4774:                     if ($activerole) {
 4775:                         push(@liveroles,$role);
 4776:                     }
 4777:                 }
 4778:             }
 4779:             if (@liveroles) {
 4780:                 if (&homeserver($cnum,$cdom) ne 'no_host') {
 4781:                     my ($accessref,$accessinfo,%access_in_dom);
 4782:                     ($roles_by_num,$description,$accessref,$accessinfo) = &get_all_adhocroles($cdom);
 4783:                     if (ref($roles_by_num) eq 'ARRAY') {
 4784:                         if (@{$roles_by_num}) {
 4785:                             my %settings;
 4786:                             if ($env{'request.course.id'} eq $cid) {
 4787:                                 foreach my $envkey (keys(%env)) {
 4788:                                     if ($envkey =~ /^\Qcourse.$cid.\E(internal\.adhoc.+)$/) {
 4789:                                         $settings{$1} = $env{$envkey};
 4790:                                     }
 4791:                                 }
 4792:                             } else {
 4793:                                 %settings = &dump('environment',$cdom,$cnum,'internal\.adhoc');
 4794:                             }
 4795:                             my %setincrs;
 4796:                             if ($settings{'internal.adhocaccess'}) {
 4797:                                 map { $setincrs{$_} = 1; } split(/,/,$settings{'internal.adhocaccess'});
 4798:                             }
 4799:                             my @statuses;
 4800:                             if ($env{'environment.inststatus'}) {
 4801:                                 @statuses = split(/,/,$env{'environment.inststatus'});
 4802:                             }
 4803:                             my $user = $env{'user.name'}.':'.$env{'user.domain'};
 4804:                             if (ref($accessref) eq 'HASH') {
 4805:                                 %access_in_dom = %{$accessref};
 4806:                             }
 4807:                             foreach my $role (@{$roles_by_num}) {
 4808:                                 my ($curraccess,@okstatus,@personnel);
 4809:                                 if ($setincrs{$role}) {
 4810:                                     ($curraccess,my $rest) = split(/=/,$settings{'internal.adhoc.'.$role});
 4811:                                     if ($curraccess eq 'status') {
 4812:                                         @okstatus = split(/\&/,$rest);
 4813:                                     } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
 4814:                                         @personnel = split(/\&/,$rest);
 4815:                                     }
 4816:                                 } else {
 4817:                                     $curraccess = $access_in_dom{$role};
 4818:                                     if (ref($accessinfo) eq 'HASH') {
 4819:                                         if ($curraccess eq 'status') {
 4820:                                             if (ref($accessinfo->{$role}) eq 'ARRAY') {
 4821:                                                 @okstatus = @{$accessinfo->{$role}};
 4822:                                             }
 4823:                                         } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
 4824:                                             if (ref($accessinfo->{$role}) eq 'ARRAY') {
 4825:                                                 @personnel = @{$accessinfo->{$role}};
 4826:                                             }
 4827:                                         }
 4828:                                     }
 4829:                                 }
 4830:                                 if ($curraccess eq 'none') {
 4831:                                     next;
 4832:                                 } elsif ($curraccess eq 'all') {
 4833:                                     push(@possroles,$role);
 4834:                                 } elsif ($curraccess eq 'dh') {
 4835:                                     if (grep(/^dh$/,@liveroles)) {
 4836:                                         push(@possroles,$role);
 4837:                                     } else {
 4838:                                         next;
 4839:                                     }
 4840:                                 } elsif ($curraccess eq 'da') {
 4841:                                     if (grep(/^da$/,@liveroles)) {
 4842:                                         push(@possroles,$role);
 4843:                                     } else {
 4844:                                         next;
 4845:                                     }
 4846:                                 } elsif ($curraccess eq 'status') {
 4847:                                     if (@okstatus) {
 4848:                                         if (!@statuses) {
 4849:                                             if (grep(/^default$/,@okstatus)) {
 4850:                                                 push(@possroles,$role);
 4851:                                             }
 4852:                                         } else {
 4853:                                             foreach my $status (@okstatus) {
 4854:                                                 if (grep(/^\Q$status\E$/,@statuses)) {
 4855:                                                     push(@possroles,$role);
 4856:                                                     last;
 4857:                                                 }
 4858:                                             }
 4859:                                         }
 4860:                                     }
 4861:                                 } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
 4862:                                     if (grep(/^\Q$user\E$/,@personnel)) {
 4863:                                         if ($curraccess eq 'exc') {
 4864:                                             push(@possroles,$role);
 4865:                                         }
 4866:                                     } elsif ($curraccess eq 'inc') {
 4867:                                         push(@possroles,$role);
 4868:                                     }
 4869:                                 }
 4870:                             }
 4871:                         }
 4872:                     }
 4873:                 }
 4874:             }
 4875:         }
 4876:     }
 4877:     unless (ref($description) eq 'HASH') {
 4878:         if (ref($roles_by_num) eq 'ARRAY') {
 4879:             my %desc;
 4880:             map { $desc{$_} = $_; } (@{$roles_by_num});
 4881:             $description = \%desc;
 4882:         } else {
 4883:             $description = {};
 4884:         }
 4885:     }
 4886:     return (\@possroles,$description);
 4887: }
 4888: 
 4889: # ----------------------------------------------------- Frontpage Announcements
 4890: #
 4891: #
 4892: 
 4893: sub postannounce {
 4894:     my ($server,$text)=@_;
 4895:     unless (&allowed('psa',&host_domain($server))) { return 'refused'; }
 4896:     unless ($text=~/\w/) { $text=''; }
 4897:     return &reply('setannounce:'.&escape($text),$server);
 4898: }
 4899: 
 4900: sub getannounce {
 4901: 
 4902:     if (open(my $fh,$perlvar{'lonDocRoot'}.'/announcement.txt')) {
 4903: 	my $announcement='';
 4904: 	while (my $line = <$fh>) { $announcement .= $line; }
 4905: 	close($fh);
 4906: 	if ($announcement=~/\w/) { 
 4907: 	    return 
 4908:    '<table bgcolor="#FF5555" cellpadding="5" cellspacing="3">'.
 4909:    '<tr><td bgcolor="#FFFFFF"><tt>'.$announcement.'</tt></td></tr></table>'; 
 4910: 	} else {
 4911: 	    return '';
 4912: 	}
 4913:     } else {
 4914: 	return '';
 4915:     }
 4916: }
 4917: 
 4918: # ---------------------------------------------------------- Course ID routines
 4919: # Deal with domain's nohist_courseid.db files
 4920: #
 4921: 
 4922: sub courseidput {
 4923:     my ($domain,$storehash,$coursehome,$caller) = @_;
 4924:     return unless (ref($storehash) eq 'HASH');
 4925:     my $outcome;
 4926:     if ($caller eq 'timeonly') {
 4927:         my $cids = '';
 4928:         foreach my $item (keys(%$storehash)) {
 4929:             $cids.=&escape($item).'&';
 4930:         }
 4931:         $cids=~s/\&$//;
 4932:         $outcome = &reply('courseidputhash:'.$domain.':'.$caller.':'.$cids,
 4933:                           $coursehome);       
 4934:     } else {
 4935:         my $items = '';
 4936:         foreach my $item (keys(%$storehash)) {
 4937:             $items.= &escape($item).'='.
 4938:                      &freeze_escape($$storehash{$item}).'&';
 4939:         }
 4940:         $items=~s/\&$//;
 4941:         $outcome = &reply('courseidputhash:'.$domain.':'.$caller.':'.$items,
 4942:                           $coursehome);
 4943:     }
 4944:     if ($outcome eq 'unknown_cmd') {
 4945:         my $what;
 4946:         foreach my $cid (keys(%$storehash)) {
 4947:             $what .= &escape($cid).'=';
 4948:             foreach my $item ('description','inst_code','owner','type') {
 4949:                 $what .= &escape($storehash->{$cid}{$item}).':';
 4950:             }
 4951:             $what =~ s/\:$/&/;
 4952:         }
 4953:         $what =~ s/\&$//;  
 4954:         return &reply('courseidput:'.$domain.':'.$what,$coursehome);
 4955:     } else {
 4956:         return $outcome;
 4957:     }
 4958: }
 4959: 
 4960: sub courseiddump {
 4961:     my ($domfilter,$descfilter,$sincefilter,$instcodefilter,$ownerfilter,
 4962:         $coursefilter,$hostidflag,$hostidref,$typefilter,$regexp_ok,
 4963:         $selfenrollonly,$catfilter,$showhidden,$caller,$cloner,$cc_clone,
 4964:         $cloneonly,$createdbefore,$createdafter,$creationcontext,$domcloner,
 4965:         $hasuniquecode,$reqcrsdom,$reqinstcode)=@_;
 4966:     my $as_hash = 1;
 4967:     my %returnhash;
 4968:     if (!$domfilter) { $domfilter=''; }
 4969:     my %libserv = &all_library();
 4970:     foreach my $tryserver (keys(%libserv)) {
 4971:         if ( (  $hostidflag == 1 
 4972: 	        && grep(/^\Q$tryserver\E$/,@{$hostidref}) ) 
 4973: 	     || (!defined($hostidflag)) ) {
 4974: 
 4975: 	    if (($domfilter eq '') ||
 4976: 		(&host_domain($tryserver) eq $domfilter)) {
 4977:                 my $rep;
 4978:                 if (grep { $_ eq $tryserver } current_machine_ids()) {
 4979:                     $rep = LONCAPA::Lond::dump_course_id_handler(
 4980:                         join(":", (&host_domain($tryserver), $sincefilter, 
 4981:                                 &escape($descfilter), &escape($instcodefilter), 
 4982:                                 &escape($ownerfilter), &escape($coursefilter),
 4983:                                 &escape($typefilter), &escape($regexp_ok), 
 4984:                                 $as_hash, &escape($selfenrollonly), 
 4985:                                 &escape($catfilter), $showhidden, $caller, 
 4986:                                 &escape($cloner), &escape($cc_clone), $cloneonly, 
 4987:                                 &escape($createdbefore), &escape($createdafter), 
 4988:                                 &escape($creationcontext),$domcloner,$hasuniquecode,
 4989:                                 $reqcrsdom,&escape($reqinstcode))));
 4990:                 } else {
 4991:                     $rep = &reply('courseiddump:'.&host_domain($tryserver).':'.
 4992:                              $sincefilter.':'.&escape($descfilter).':'.
 4993:                              &escape($instcodefilter).':'.&escape($ownerfilter).
 4994:                              ':'.&escape($coursefilter).':'.&escape($typefilter).
 4995:                              ':'.&escape($regexp_ok).':'.$as_hash.':'.
 4996:                              &escape($selfenrollonly).':'.&escape($catfilter).':'.
 4997:                              $showhidden.':'.$caller.':'.&escape($cloner).':'.
 4998:                              &escape($cc_clone).':'.$cloneonly.':'.
 4999:                              &escape($createdbefore).':'.&escape($createdafter).':'.
 5000:                              &escape($creationcontext).':'.$domcloner.':'.$hasuniquecode.
 5001:                              ':'.$reqcrsdom.':'.&escape($reqinstcode),$tryserver);
 5002:                 }
 5003:                      
 5004:                 my @pairs=split(/\&/,$rep);
 5005:                 foreach my $item (@pairs) {
 5006:                     my ($key,$value)=split(/\=/,$item,2);
 5007:                     $key = &unescape($key);
 5008:                     next if ($key =~ /^error: 2 /);
 5009:                     my $result = &thaw_unescape($value);
 5010:                     if (ref($result) eq 'HASH') {
 5011:                         $returnhash{$key}=$result;
 5012:                     } else {
 5013:                         my @responses = split(/:/,$value);
 5014:                         my @items = ('description','inst_code','owner','type');
 5015:                         for (my $i=0; $i<@responses; $i++) {
 5016:                             $returnhash{$key}{$items[$i]} = &unescape($responses[$i]);
 5017:                         }
 5018:                     }
 5019:                 }
 5020:             }
 5021:         }
 5022:     }
 5023:     return %returnhash;
 5024: }
 5025: 
 5026: sub courselastaccess {
 5027:     my ($cdom,$cnum,$hostidref) = @_;
 5028:     my %returnhash;
 5029:     if ($cdom && $cnum) {
 5030:         my $chome = &homeserver($cnum,$cdom);
 5031:         if ($chome ne 'no_host') {
 5032:             my $rep = &reply('courselastaccess:'.$cdom.':'.$cnum,$chome);
 5033:             &extract_lastaccess(\%returnhash,$rep);
 5034:         }
 5035:     } else {
 5036:         if (!$cdom) { $cdom=''; }
 5037:         my %libserv = &all_library();
 5038:         foreach my $tryserver (keys(%libserv)) {
 5039:             if (ref($hostidref) eq 'ARRAY') {
 5040:                 next unless (grep(/^\Q$tryserver\E$/,@{$hostidref}));
 5041:             } 
 5042:             if (($cdom eq '') || (&host_domain($tryserver) eq $cdom)) {
 5043:                 my $rep = &reply('courselastaccess:'.&host_domain($tryserver).':',$tryserver);
 5044:                 &extract_lastaccess(\%returnhash,$rep);
 5045:             }
 5046:         }
 5047:     }
 5048:     return %returnhash;
 5049: }
 5050: 
 5051: sub extract_lastaccess {
 5052:     my ($returnhash,$rep) = @_;
 5053:     if (ref($returnhash) eq 'HASH') {
 5054:         unless ($rep eq 'unknown_command' || $rep eq 'no_such_host' || 
 5055:                 $rep eq 'con_lost' || $rep eq 'rejected' || $rep eq 'refused' ||
 5056:                  $rep eq '') {
 5057:             my @pairs=split(/\&/,$rep);
 5058:             foreach my $item (@pairs) {
 5059:                 my ($key,$value)=split(/\=/,$item,2);
 5060:                 $key = &unescape($key);
 5061:                 next if ($key =~ /^error: 2 /);
 5062:                 $returnhash->{$key} = &thaw_unescape($value);
 5063:             }
 5064:         }
 5065:     }
 5066:     return;
 5067: }
 5068: 
 5069: # ---------------------------------------------------------- DC e-mail
 5070: 
 5071: sub dcmailput {
 5072:     my ($domain,$msgid,$message,$server)=@_;
 5073:     my $status = &Apache::lonnet::critical(
 5074:        'dcmailput:'.$domain.':'.&escape($msgid).'='.
 5075:        &escape($message),$server);
 5076:     return $status;
 5077: }
 5078: 
 5079: sub dcmaildump {
 5080:     my ($dom,$startdate,$enddate,$senders) = @_;
 5081:     my %returnhash=();
 5082: 
 5083:     if (defined(&domain($dom,'primary'))) {
 5084:         my $cmd='dcmaildump:'.$dom.':'.&escape($startdate).':'.
 5085:                                                          &escape($enddate).':';
 5086: 	my @esc_senders=map { &escape($_)} @$senders;
 5087: 	$cmd.=&escape(join('&',@esc_senders));
 5088: 	foreach my $line (split(/\&/,&reply($cmd,&domain($dom,'primary')))) {
 5089:             my ($key,$value) = split(/\=/,$line,2);
 5090:             if (($key) && ($value)) {
 5091:                 $returnhash{&unescape($key)} = &unescape($value);
 5092:             }
 5093:         }
 5094:     }
 5095:     return %returnhash;
 5096: }
 5097: # ---------------------------------------------------------- Domain roles
 5098: 
 5099: sub get_domain_roles {
 5100:     my ($dom,$roles,$startdate,$enddate)=@_;
 5101:     if ((!defined($startdate)) || ($startdate eq '')) {
 5102:         $startdate = '.';
 5103:     }
 5104:     if ((!defined($enddate)) || ($enddate eq '')) {
 5105:         $enddate = '.';
 5106:     }
 5107:     my $rolelist;
 5108:     if (ref($roles) eq 'ARRAY') {
 5109:         $rolelist = join('&',@{$roles});
 5110:     }
 5111:     my %personnel = ();
 5112: 
 5113:     my %servers = &get_servers($dom,'library');
 5114:     foreach my $tryserver (keys(%servers)) {
 5115: 	%{$personnel{$tryserver}}=();
 5116: 	foreach my $line (split(/\&/,&reply('domrolesdump:'.$dom.':'.
 5117: 					    &escape($startdate).':'.
 5118: 					    &escape($enddate).':'.
 5119: 					    &escape($rolelist), $tryserver))) {
 5120: 	    my ($key,$value) = split(/\=/,$line,2);
 5121: 	    if (($key) && ($value)) {
 5122: 		$personnel{$tryserver}{&unescape($key)} = &unescape($value);
 5123: 	    }
 5124: 	}
 5125:     }
 5126:     return %personnel;
 5127: }
 5128: 
 5129: sub get_active_domroles {
 5130:     my ($dom,$roles) = @_;
 5131:     return () unless (ref($roles) eq 'ARRAY');
 5132:     my $now = time;
 5133:     my %dompersonnel = &get_domain_roles($dom,$roles,$now,$now);
 5134:     my %domroles;
 5135:     foreach my $server (keys(%dompersonnel)) {
 5136:         foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
 5137:             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
 5138:             $domroles{$uname.':'.$udom} = $dompersonnel{$server}{$user};
 5139:         }
 5140:     }
 5141:     return %domroles;
 5142: }
 5143: 
 5144: # ----------------------------------------------------------- Interval timing 
 5145: 
 5146: {
 5147: # Caches needed for speedup of navmaps
 5148: # We don't want to cache this for very long at all (5 seconds at most)
 5149: # 
 5150: # The user for whom we cache
 5151: my $cachedkey='';
 5152: # The cached times for this user
 5153: my %cachedtimes=();
 5154: # When this was last done
 5155: my $cachedtime='';
 5156: 
 5157: sub load_all_first_access {
 5158:     my ($uname,$udom,$ignorecache)=@_;
 5159:     if (($cachedkey eq $uname.':'.$udom) &&
 5160:         (abs($cachedtime-time)<5) && (!$env{'form.markaccess'}) &&
 5161:         (!$ignorecache)) {
 5162:         return;
 5163:     }
 5164:     $cachedtime=time;
 5165:     $cachedkey=$uname.':'.$udom;
 5166:     %cachedtimes=&dump('firstaccesstimes',$udom,$uname);
 5167: }
 5168: 
 5169: sub get_first_access {
 5170:     my ($type,$argsymb,$argmap,$ignorecache)=@_;
 5171:     my ($symb,$courseid,$udom,$uname)=&whichuser();
 5172:     if ($argsymb) { $symb=$argsymb; }
 5173:     my ($map,$id,$res)=&decode_symb($symb);
 5174:     if ($argmap) { $map = $argmap; }
 5175:     if ($type eq 'course') {
 5176: 	$res='course';
 5177:     } elsif ($type eq 'map') {
 5178: 	$res=&symbread($map);
 5179:     } else {
 5180: 	$res=$symb;
 5181:     }
 5182:     &load_all_first_access($uname,$udom,$ignorecache);
 5183:     return $cachedtimes{"$courseid\0$res"};
 5184: }
 5185: 
 5186: sub set_first_access {
 5187:     my ($type,$interval)=@_;
 5188:     my ($symb,$courseid,$udom,$uname)=&whichuser();
 5189:     my ($map,$id,$res)=&decode_symb($symb);
 5190:     if ($type eq 'course') {
 5191: 	$res='course';
 5192:     } elsif ($type eq 'map') {
 5193: 	$res=&symbread($map);
 5194:     } else {
 5195: 	$res=$symb;
 5196:     }
 5197:     $cachedkey='';
 5198:     my $firstaccess=&get_first_access($type,$symb,$map);
 5199:     if (!$firstaccess) {
 5200:         my $start = time;
 5201: 	my $putres = &put('firstaccesstimes',{"$courseid\0$res"=>$start},
 5202:                           $udom,$uname);
 5203:         if ($putres eq 'ok') {
 5204:             &put('timerinterval',{"$courseid\0$res"=>$interval},
 5205:                  $udom,$uname); 
 5206:             &appenv(
 5207:                      {
 5208:                         'course.'.$courseid.'.firstaccess.'.$res   => $start,
 5209:                         'course.'.$courseid.'.timerinterval.'.$res => $interval,
 5210:                      }
 5211:                   );
 5212:         }
 5213:         return $putres;
 5214:     }
 5215:     return 'already_set';
 5216: }
 5217: }
 5218: 
 5219: # --------------------------------------------- Set Expire Date for Spreadsheet
 5220: 
 5221: sub expirespread {
 5222:     my ($uname,$udom,$stype,$usymb)=@_;
 5223:     my $cid=$env{'request.course.id'}; 
 5224:     if ($cid) {
 5225:        my $now=time;
 5226:        my $key=$uname.':'.$udom.':'.$stype.':'.$usymb;
 5227:        return &reply('put:'.$env{'course.'.$cid.'.domain'}.':'.
 5228:                             $env{'course.'.$cid.'.num'}.
 5229: 	        	    ':nohist_expirationdates:'.
 5230:                             &escape($key).'='.$now,
 5231:                             $env{'course.'.$cid.'.home'})
 5232:     }
 5233:     return 'ok';
 5234: }
 5235: 
 5236: # ----------------------------------------------------- Devalidate Spreadsheets
 5237: 
 5238: sub devalidate {
 5239:     my ($symb,$uname,$udom)=@_;
 5240:     my $cid=$env{'request.course.id'}; 
 5241:     if ($cid) {
 5242:         # delete the stored spreadsheets for
 5243:         # - the student level sheet of this user in course's homespace
 5244:         # - the assessment level sheet for this resource 
 5245:         #   for this user in user's homespace
 5246: 	# - current conditional state info
 5247: 	my $key=$uname.':'.$udom.':';
 5248:         my $status=
 5249: 	    &del('nohist_calculatedsheets',
 5250: 		 [$key.'studentcalc:'],
 5251: 		 $env{'course.'.$cid.'.domain'},
 5252: 		 $env{'course.'.$cid.'.num'})
 5253: 		.' '.
 5254: 	    &del('nohist_calculatedsheets_'.$cid,
 5255: 		 [$key.'assesscalc:'.$symb],$udom,$uname);
 5256:         unless ($status eq 'ok ok') {
 5257:            &logthis('Could not devalidate spreadsheet '.
 5258:                     $uname.' at '.$udom.' for '.
 5259: 		    $symb.': '.$status);
 5260:         }
 5261: 	&delenv('user.state.'.$cid);
 5262:     }
 5263: }
 5264: 
 5265: sub get_scalar {
 5266:     my ($string,$end) = @_;
 5267:     my $value;
 5268:     if ($$string =~ s/^([^&]*?)($end)/$2/) {
 5269: 	$value = $1;
 5270:     } elsif ($$string =~ s/^([^&]*?)&//) {
 5271: 	$value = $1;
 5272:     }
 5273:     return &unescape($value);
 5274: }
 5275: 
 5276: sub array2str {
 5277:   my (@array) = @_;
 5278:   my $result=&arrayref2str(\@array);
 5279:   $result=~s/^__ARRAY_REF__//;
 5280:   $result=~s/__END_ARRAY_REF__$//;
 5281:   return $result;
 5282: }
 5283: 
 5284: sub arrayref2str {
 5285:   my ($arrayref) = @_;
 5286:   my $result='__ARRAY_REF__';
 5287:   foreach my $elem (@$arrayref) {
 5288:     if(ref($elem) eq 'ARRAY') {
 5289:       $result.=&arrayref2str($elem).'&';
 5290:     } elsif(ref($elem) eq 'HASH') {
 5291:       $result.=&hashref2str($elem).'&';
 5292:     } elsif(ref($elem)) {
 5293:       #print("Got a ref of ".(ref($elem))." skipping.");
 5294:     } else {
 5295:       $result.=&escape($elem).'&';
 5296:     }
 5297:   }
 5298:   $result=~s/\&$//;
 5299:   $result .= '__END_ARRAY_REF__';
 5300:   return $result;
 5301: }
 5302: 
 5303: sub hash2str {
 5304:   my (%hash) = @_;
 5305:   my $result=&hashref2str(\%hash);
 5306:   $result=~s/^__HASH_REF__//;
 5307:   $result=~s/__END_HASH_REF__$//;
 5308:   return $result;
 5309: }
 5310: 
 5311: sub hashref2str {
 5312:   my ($hashref)=@_;
 5313:   my $result='__HASH_REF__';
 5314:   foreach my $key (sort(keys(%$hashref))) {
 5315:     if (ref($key) eq 'ARRAY') {
 5316:       $result.=&arrayref2str($key).'=';
 5317:     } elsif (ref($key) eq 'HASH') {
 5318:       $result.=&hashref2str($key).'=';
 5319:     } elsif (ref($key)) {
 5320:       $result.='=';
 5321:       #print("Got a ref of ".(ref($key))." skipping.");
 5322:     } else {
 5323: 	if (defined($key)) {$result.=&escape($key).'=';} else { last; }
 5324:     }
 5325: 
 5326:     if(ref($hashref->{$key}) eq 'ARRAY') {
 5327:       $result.=&arrayref2str($hashref->{$key}).'&';
 5328:     } elsif(ref($hashref->{$key}) eq 'HASH') {
 5329:       $result.=&hashref2str($hashref->{$key}).'&';
 5330:     } elsif(ref($hashref->{$key})) {
 5331:        $result.='&';
 5332:       #print("Got a ref of ".(ref($hashref->{$key}))." skipping.");
 5333:     } else {
 5334:       $result.=&escape($hashref->{$key}).'&';
 5335:     }
 5336:   }
 5337:   $result=~s/\&$//;
 5338:   $result .= '__END_HASH_REF__';
 5339:   return $result;
 5340: }
 5341: 
 5342: sub str2hash {
 5343:     my ($string)=@_;
 5344:     my ($hash)=&str2hashref('__HASH_REF__'.$string.'__END_HASH_REF__');
 5345:     return %$hash;
 5346: }
 5347: 
 5348: sub str2hashref {
 5349:   my ($string) = @_;
 5350: 
 5351:   my %hash;
 5352: 
 5353:   if($string !~ /^__HASH_REF__/) {
 5354:       if (! ($string eq '' || !defined($string))) {
 5355: 	  $hash{'error'}='Not hash reference';
 5356:       }
 5357:       return (\%hash, $string);
 5358:   }
 5359: 
 5360:   $string =~ s/^__HASH_REF__//;
 5361: 
 5362:   while($string !~ /^__END_HASH_REF__/) {
 5363:       #key
 5364:       my $key='';
 5365:       if($string =~ /^__HASH_REF__/) {
 5366:           ($key, $string)=&str2hashref($string);
 5367:           if(defined($key->{'error'})) {
 5368:               $hash{'error'}='Bad data';
 5369:               return (\%hash, $string);
 5370:           }
 5371:       } elsif($string =~ /^__ARRAY_REF__/) {
 5372:           ($key, $string)=&str2arrayref($string);
 5373:           if($key->[0] eq 'Array reference error') {
 5374:               $hash{'error'}='Bad data';
 5375:               return (\%hash, $string);
 5376:           }
 5377:       } else {
 5378:           $string =~ s/^(.*?)=//;
 5379: 	  $key=&unescape($1);
 5380:       }
 5381:       $string =~ s/^=//;
 5382: 
 5383:       #value
 5384:       my $value='';
 5385:       if($string =~ /^__HASH_REF__/) {
 5386:           ($value, $string)=&str2hashref($string);
 5387:           if(defined($value->{'error'})) {
 5388:               $hash{'error'}='Bad data';
 5389:               return (\%hash, $string);
 5390:           }
 5391:       } elsif($string =~ /^__ARRAY_REF__/) {
 5392:           ($value, $string)=&str2arrayref($string);
 5393:           if($value->[0] eq 'Array reference error') {
 5394:               $hash{'error'}='Bad data';
 5395:               return (\%hash, $string);
 5396:           }
 5397:       } else {
 5398: 	  $value=&get_scalar(\$string,'__END_HASH_REF__');
 5399:       }
 5400:       $string =~ s/^&//;
 5401: 
 5402:       $hash{$key}=$value;
 5403:   }
 5404: 
 5405:   $string =~ s/^__END_HASH_REF__//;
 5406: 
 5407:   return (\%hash, $string);
 5408: }
 5409: 
 5410: sub str2array {
 5411:     my ($string)=@_;
 5412:     my ($array)=&str2arrayref('__ARRAY_REF__'.$string.'__END_ARRAY_REF__');
 5413:     return @$array;
 5414: }
 5415: 
 5416: sub str2arrayref {
 5417:   my ($string) = @_;
 5418:   my @array;
 5419: 
 5420:   if($string !~ /^__ARRAY_REF__/) {
 5421:       if (! ($string eq '' || !defined($string))) {
 5422: 	  $array[0]='Array reference error';
 5423:       }
 5424:       return (\@array, $string);
 5425:   }
 5426: 
 5427:   $string =~ s/^__ARRAY_REF__//;
 5428: 
 5429:   while($string !~ /^__END_ARRAY_REF__/) {
 5430:       my $value='';
 5431:       if($string =~ /^__HASH_REF__/) {
 5432:           ($value, $string)=&str2hashref($string);
 5433:           if(defined($value->{'error'})) {
 5434:               $array[0] ='Array reference error';
 5435:               return (\@array, $string);
 5436:           }
 5437:       } elsif($string =~ /^__ARRAY_REF__/) {
 5438:           ($value, $string)=&str2arrayref($string);
 5439:           if($value->[0] eq 'Array reference error') {
 5440:               $array[0] ='Array reference error';
 5441:               return (\@array, $string);
 5442:           }
 5443:       } else {
 5444: 	  $value=&get_scalar(\$string,'__END_ARRAY_REF__');
 5445:       }
 5446:       $string =~ s/^&//;
 5447: 
 5448:       push(@array, $value);
 5449:   }
 5450: 
 5451:   $string =~ s/^__END_ARRAY_REF__//;
 5452: 
 5453:   return (\@array, $string);
 5454: }
 5455: 
 5456: # -------------------------------------------------------------------Temp Store
 5457: 
 5458: sub tmpreset {
 5459:   my ($symb,$namespace,$domain,$stuname) = @_;
 5460:   if (!$symb) {
 5461:     $symb=&symbread();
 5462:     if (!$symb) { $symb= $env{'request.url'}; }
 5463:   }
 5464:   $symb=escape($symb);
 5465: 
 5466:   if (!$namespace) { $namespace=$env{'request.state'}; }
 5467:   $namespace=~s/\//\_/g;
 5468:   $namespace=~s/\W//g;
 5469: 
 5470:   if (!$domain) { $domain=$env{'user.domain'}; }
 5471:   if (!$stuname) { $stuname=$env{'user.name'}; }
 5472:   if ($domain eq 'public' && $stuname eq 'public') {
 5473:       $stuname=$ENV{'REMOTE_ADDR'};
 5474:   }
 5475:   my $path=LONCAPA::tempdir();
 5476:   my %hash;
 5477:   if (tie(%hash,'GDBM_File',
 5478: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
 5479: 	  &GDBM_WRCREAT(),0640)) {
 5480:     foreach my $key (keys(%hash)) {
 5481:       if ($key=~ /:$symb/) {
 5482: 	delete($hash{$key});
 5483:       }
 5484:     }
 5485:   }
 5486: }
 5487: 
 5488: sub tmpstore {
 5489:   my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
 5490: 
 5491:   if (!$symb) {
 5492:     $symb=&symbread();
 5493:     if (!$symb) { $symb= $env{'request.url'}; }
 5494:   }
 5495:   $symb=escape($symb);
 5496: 
 5497:   if (!$namespace) {
 5498:     # I don't think we would ever want to store this for a course.
 5499:     # it seems this will only be used if we don't have a course.
 5500:     #$namespace=$env{'request.course.id'};
 5501:     #if (!$namespace) {
 5502:       $namespace=$env{'request.state'};
 5503:     #}
 5504:   }
 5505:   $namespace=~s/\//\_/g;
 5506:   $namespace=~s/\W//g;
 5507:   if (!$domain) { $domain=$env{'user.domain'}; }
 5508:   if (!$stuname) { $stuname=$env{'user.name'}; }
 5509:   if ($domain eq 'public' && $stuname eq 'public') {
 5510:       $stuname=$ENV{'REMOTE_ADDR'};
 5511:   }
 5512:   my $now=time;
 5513:   my %hash;
 5514:   my $path=LONCAPA::tempdir();
 5515:   if (tie(%hash,'GDBM_File',
 5516: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
 5517: 	  &GDBM_WRCREAT(),0640)) {
 5518:     $hash{"version:$symb"}++;
 5519:     my $version=$hash{"version:$symb"};
 5520:     my $allkeys=''; 
 5521:     foreach my $key (keys(%$storehash)) {
 5522:       $allkeys.=$key.':';
 5523:       $hash{"$version:$symb:$key"}=&freeze_escape($$storehash{$key});
 5524:     }
 5525:     $hash{"$version:$symb:timestamp"}=$now;
 5526:     $allkeys.='timestamp';
 5527:     $hash{"$version:keys:$symb"}=$allkeys;
 5528:     if (untie(%hash)) {
 5529:       return 'ok';
 5530:     } else {
 5531:       return "error:$!";
 5532:     }
 5533:   } else {
 5534:     return "error:$!";
 5535:   }
 5536: }
 5537: 
 5538: # -----------------------------------------------------------------Temp Restore
 5539: 
 5540: sub tmprestore {
 5541:   my ($symb,$namespace,$domain,$stuname) = @_;
 5542: 
 5543:   if (!$symb) {
 5544:     $symb=&symbread();
 5545:     if (!$symb) { $symb= $env{'request.url'}; }
 5546:   }
 5547:   $symb=escape($symb);
 5548: 
 5549:   if (!$namespace) { $namespace=$env{'request.state'}; }
 5550: 
 5551:   if (!$domain) { $domain=$env{'user.domain'}; }
 5552:   if (!$stuname) { $stuname=$env{'user.name'}; }
 5553:   if ($domain eq 'public' && $stuname eq 'public') {
 5554:       $stuname=$ENV{'REMOTE_ADDR'};
 5555:   }
 5556:   my %returnhash;
 5557:   $namespace=~s/\//\_/g;
 5558:   $namespace=~s/\W//g;
 5559:   my %hash;
 5560:   my $path=LONCAPA::tempdir();
 5561:   if (tie(%hash,'GDBM_File',
 5562: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
 5563: 	  &GDBM_READER(),0640)) {
 5564:     my $version=$hash{"version:$symb"};
 5565:     $returnhash{'version'}=$version;
 5566:     my $scope;
 5567:     for ($scope=1;$scope<=$version;$scope++) {
 5568:       my $vkeys=$hash{"$scope:keys:$symb"};
 5569:       my @keys=split(/:/,$vkeys);
 5570:       my $key;
 5571:       $returnhash{"$scope:keys"}=$vkeys;
 5572:       foreach $key (@keys) {
 5573: 	$returnhash{"$scope:$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
 5574: 	$returnhash{"$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
 5575:       }
 5576:     }
 5577:     if (!(untie(%hash))) {
 5578:       return "error:$!";
 5579:     }
 5580:   } else {
 5581:     return "error:$!";
 5582:   }
 5583:   return %returnhash;
 5584: }
 5585: 
 5586: # ----------------------------------------------------------------------- Store
 5587: 
 5588: sub store {
 5589:     my ($storehash,$symb,$namespace,$domain,$stuname,$laststore) = @_;
 5590:     my $home='';
 5591: 
 5592:     if ($stuname) { $home=&homeserver($stuname,$domain); }
 5593: 
 5594:     $symb=&symbclean($symb);
 5595:     if (!$symb) { unless ($symb=&symbread()) { return ''; } }
 5596: 
 5597:     if (!$domain) { $domain=$env{'user.domain'}; }
 5598:     if (!$stuname) { $stuname=$env{'user.name'}; }
 5599: 
 5600:     &devalidate($symb,$stuname,$domain);
 5601: 
 5602:     $symb=escape($symb);
 5603:     if (!$namespace) { 
 5604:        unless ($namespace=$env{'request.course.id'}) { 
 5605:           return ''; 
 5606:        } 
 5607:     }
 5608:     if (!$home) { $home=$env{'user.home'}; }
 5609: 
 5610:     $$storehash{'ip'}=$ENV{'REMOTE_ADDR'};
 5611:     $$storehash{'host'}=$perlvar{'lonHostID'};
 5612: 
 5613:     my $namevalue='';
 5614:     foreach my $key (keys(%$storehash)) {
 5615:         $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
 5616:     }
 5617:     $namevalue=~s/\&$//;
 5618:     &courselog($symb.':'.$stuname.':'.$domain.':STORE:'.$namevalue);
 5619:     return reply("store:$domain:$stuname:$namespace:$symb:$namevalue:$laststore","$home");
 5620: }
 5621: 
 5622: # -------------------------------------------------------------- Critical Store
 5623: 
 5624: sub cstore {
 5625:     my ($storehash,$symb,$namespace,$domain,$stuname,$laststore) = @_;
 5626:     my $home='';
 5627: 
 5628:     if ($stuname) { $home=&homeserver($stuname,$domain); }
 5629: 
 5630:     $symb=&symbclean($symb);
 5631:     if (!$symb) { unless ($symb=&symbread()) { return ''; } }
 5632: 
 5633:     if (!$domain) { $domain=$env{'user.domain'}; }
 5634:     if (!$stuname) { $stuname=$env{'user.name'}; }
 5635: 
 5636:     &devalidate($symb,$stuname,$domain);
 5637: 
 5638:     $symb=escape($symb);
 5639:     if (!$namespace) { 
 5640:        unless ($namespace=$env{'request.course.id'}) { 
 5641:           return ''; 
 5642:        } 
 5643:     }
 5644:     if (!$home) { $home=$env{'user.home'}; }
 5645: 
 5646:     $$storehash{'ip'}=$ENV{'REMOTE_ADDR'};
 5647:     $$storehash{'host'}=$perlvar{'lonHostID'};
 5648: 
 5649:     my $namevalue='';
 5650:     foreach my $key (keys(%$storehash)) {
 5651:         $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
 5652:     }
 5653:     $namevalue=~s/\&$//;
 5654:     &courselog($symb.':'.$stuname.':'.$domain.':CSTORE:'.$namevalue);
 5655:     return critical
 5656:                 ("store:$domain:$stuname:$namespace:$symb:$namevalue:$laststore","$home");
 5657: }
 5658: 
 5659: # --------------------------------------------------------------------- Restore
 5660: 
 5661: sub restore {
 5662:     my ($symb,$namespace,$domain,$stuname) = @_;
 5663:     my $home='';
 5664: 
 5665:     if ($stuname) { $home=&homeserver($stuname,$domain); }
 5666: 
 5667:     if (!$symb) {
 5668:         return if ($namespace eq 'courserequests');
 5669:         unless ($symb=escape(&symbread())) { return ''; }
 5670:     } else {
 5671:         unless ($namespace eq 'courserequests') {
 5672:             $symb=&escape(&symbclean($symb));
 5673:         }
 5674:     }
 5675:     if (!$namespace) { 
 5676:        unless ($namespace=$env{'request.course.id'}) { 
 5677:           return ''; 
 5678:        } 
 5679:     }
 5680:     if (!$domain) { $domain=$env{'user.domain'}; }
 5681:     if (!$stuname) { $stuname=$env{'user.name'}; }
 5682:     if (!$home) { $home=$env{'user.home'}; }
 5683:     my $answer=&reply("restore:$domain:$stuname:$namespace:$symb","$home");
 5684: 
 5685:     my %returnhash=();
 5686:     foreach my $line (split(/\&/,$answer)) {
 5687: 	my ($name,$value)=split(/\=/,$line);
 5688:         $returnhash{&unescape($name)}=&thaw_unescape($value);
 5689:     }
 5690:     my $version;
 5691:     for ($version=1;$version<=$returnhash{'version'};$version++) {
 5692:        foreach my $item (split(/\:/,$returnhash{$version.':keys'})) {
 5693:           $returnhash{$item}=$returnhash{$version.':'.$item};
 5694:        }
 5695:     }
 5696:     return %returnhash;
 5697: }
 5698: 
 5699: # ---------------------------------------------------------- Course Description
 5700: #
 5701: #  
 5702: 
 5703: sub coursedescription {
 5704:     my ($courseid,$args)=@_;
 5705:     $courseid=~s/^\///;
 5706:     $courseid=~s/\_/\//g;
 5707:     my ($cdomain,$cnum)=split(/\//,$courseid);
 5708:     my $chome=&homeserver($cnum,$cdomain);
 5709:     my $normalid=$cdomain.'_'.$cnum;
 5710:     # need to always cache even if we get errors otherwise we keep 
 5711:     # trying and trying and trying to get the course description.
 5712:     my %envhash=();
 5713:     my %returnhash=();
 5714:     
 5715:     my $expiretime=600;
 5716:     if ($env{'request.course.id'} eq $normalid) {
 5717: 	$expiretime=120;
 5718:     }
 5719: 
 5720:     my $prefix='course.'.$cdomain.'_'.$cnum.'.';
 5721:     if (!$args->{'freshen_cache'}
 5722: 	&& ((time-$env{$prefix.'last_cache'}) < $expiretime) ) {
 5723: 	foreach my $key (keys(%env)) {
 5724: 	    next if ($key !~ /^\Q$prefix\E(.*)/);
 5725: 	    my ($setting) = $1;
 5726: 	    $returnhash{$setting} = $env{$key};
 5727: 	}
 5728: 	return %returnhash;
 5729:     }
 5730: 
 5731:     # get the data again
 5732: 
 5733:     if (!$args->{'one_time'}) {
 5734: 	$envhash{'course.'.$normalid.'.last_cache'}=time;
 5735:     }
 5736: 
 5737:     if ($chome ne 'no_host') {
 5738:        %returnhash=&dump('environment',$cdomain,$cnum);
 5739:        if (!exists($returnhash{'con_lost'})) {
 5740: 	   my $username = $env{'user.name'}; # Defult username
 5741: 	   if(defined $args->{'user'}) {
 5742: 	       $username = $args->{'user'};
 5743: 	   }
 5744:            $returnhash{'home'}= $chome;
 5745: 	   $returnhash{'domain'} = $cdomain;
 5746: 	   $returnhash{'num'} = $cnum;
 5747:            if (!defined($returnhash{'type'})) {
 5748:                $returnhash{'type'} = 'Course';
 5749:            }
 5750:            while (my ($name,$value) = each %returnhash) {
 5751:                $envhash{'course.'.$normalid.'.'.$name}=$value;
 5752:            }
 5753:            $returnhash{'url'}=&clutter($returnhash{'url'});
 5754:            $returnhash{'fn'}=LONCAPA::tempdir() .
 5755: 	       $username.'_'.$cdomain.'_'.$cnum;
 5756:            $envhash{'course.'.$normalid.'.home'}=$chome;
 5757:            $envhash{'course.'.$normalid.'.domain'}=$cdomain;
 5758:            $envhash{'course.'.$normalid.'.num'}=$cnum;
 5759:        }
 5760:     }
 5761:     if (!$args->{'one_time'}) {
 5762: 	&appenv(\%envhash);
 5763:     }
 5764:     return %returnhash;
 5765: }
 5766: 
 5767: sub update_released_required {
 5768:     my ($needsrelease,$cdom,$cnum,$chome,$cid) = @_;
 5769:     if ($cdom eq '' || $cnum eq '' || $chome eq '' || $cid eq '') {
 5770:         $cid = $env{'request.course.id'};
 5771:         $cdom = $env{'course.'.$cid.'.domain'};
 5772:         $cnum = $env{'course.'.$cid.'.num'};
 5773:         $chome = $env{'course.'.$cid.'.home'};
 5774:     }
 5775:     if ($needsrelease) {
 5776:         my %curr_reqd_hash = &userenvironment($cdom,$cnum,'internal.releaserequired');
 5777:         my $needsupdate;
 5778:         if ($curr_reqd_hash{'internal.releaserequired'} eq '') {
 5779:             $needsupdate = 1;
 5780:         } else {
 5781:             my ($currmajor,$currminor) = split(/\./,$curr_reqd_hash{'internal.releaserequired'});
 5782:             my ($needsmajor,$needsminor) = split(/\./,$needsrelease);
 5783:             if (($currmajor < $needsmajor) || ($currmajor == $needsmajor && $currminor < $needsminor)) {
 5784:                 $needsupdate = 1;
 5785:             }
 5786:         }
 5787:         if ($needsupdate) {
 5788:             my %needshash = (
 5789:                              'internal.releaserequired' => $needsrelease,
 5790:                             );
 5791:             my $putresult = &put('environment',\%needshash,$cdom,$cnum);
 5792:             if ($putresult eq 'ok') {
 5793:                 &appenv({'course.'.$cid.'.internal.releaserequired' => $needsrelease});
 5794:                 my %crsinfo = &courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
 5795:                 if (ref($crsinfo{$cid}) eq 'HASH') {
 5796:                     $crsinfo{$cid}{'releaserequired'} = $needsrelease;
 5797:                     &courseidput($cdom,\%crsinfo,$chome,'notime');
 5798:                 }
 5799:             }
 5800:         }
 5801:     }
 5802:     return;
 5803: }
 5804: 
 5805: # -------------------------------------------------See if a user is privileged
 5806: 
 5807: sub privileged {
 5808:     my ($username,$domain,$possdomains,$possroles)=@_;
 5809:     my $now = time;
 5810:     my $roles;
 5811:     if (ref($possroles) eq 'ARRAY') {
 5812:         $roles = $possroles; 
 5813:     } else {
 5814:         $roles = ['dc','su'];
 5815:     }
 5816:     if (ref($possdomains) eq 'ARRAY') {
 5817:         my %privileged = &privileged_by_domain($possdomains,$roles);
 5818:         foreach my $dom (@{$possdomains}) {
 5819:             if (($username =~ /^$match_username$/) && ($domain =~ /^$match_domain$/) &&
 5820:                 (ref($privileged{$dom}) eq 'HASH')) {
 5821:                 foreach my $role (@{$roles}) {
 5822:                     if (ref($privileged{$dom}{$role}) eq 'HASH') {
 5823:                         if (exists($privileged{$dom}{$role}{$username.':'.$domain})) {
 5824:                             my ($end,$start) = split(/:/,$privileged{$dom}{$role}{$username.':'.$domain});
 5825:                             return 1 unless (($end && $end < $now) ||
 5826:                                              ($start && $start > $now));
 5827:                         }
 5828:                     }
 5829:                 }
 5830:             }
 5831:         }
 5832:     } else {
 5833:         my %rolesdump = &dump("roles", $domain, $username) or return 0;
 5834:         my $now = time;
 5835: 
 5836:         for my $role (@rolesdump{grep { ! /^rolesdef_/ } keys(%rolesdump)}) {
 5837:             my ($trole, $tend, $tstart) = split(/_/, $role);
 5838:             if (grep(/^\Q$trole\E$/,@{$roles})) {
 5839:                 return 1 unless ($tend && $tend < $now) 
 5840:                         or ($tstart && $tstart > $now);
 5841:             }
 5842:         }
 5843:     }
 5844:     return 0;
 5845: }
 5846: 
 5847: sub privileged_by_domain {
 5848:     my ($domains,$roles) = @_;
 5849:     my %privileged = ();
 5850:     my $cachetime = 60*60*24;
 5851:     my $now = time;
 5852:     unless ((ref($domains) eq 'ARRAY') && (ref($roles) eq 'ARRAY')) {
 5853:         return %privileged;
 5854:     }
 5855:     foreach my $dom (@{$domains}) {
 5856:         next if (ref($privileged{$dom}) eq 'HASH');
 5857:         my $needroles;
 5858:         foreach my $role (@{$roles}) {
 5859:             my ($result,$cached)=&is_cached_new('priv_'.$role,$dom);
 5860:             if (defined($cached)) {
 5861:                 if (ref($result) eq 'HASH') {
 5862:                     $privileged{$dom}{$role} = $result;
 5863:                 }
 5864:             } else {
 5865:                 $needroles = 1;
 5866:             }
 5867:         }
 5868:         if ($needroles) {
 5869:             my %dompersonnel = &get_domain_roles($dom,$roles);
 5870:             $privileged{$dom} = {};
 5871:             foreach my $server (keys(%dompersonnel)) {
 5872:                 if (ref($dompersonnel{$server}) eq 'HASH') {
 5873:                     foreach my $item (keys(%{$dompersonnel{$server}})) {
 5874:                         my ($trole,$uname,$udom,$rest) = split(/:/,$item,4);
 5875:                         my ($end,$start) = split(/:/,$dompersonnel{$server}{$item});
 5876:                         next if ($end && $end < $now);
 5877:                         $privileged{$dom}{$trole}{$uname.':'.$udom} = 
 5878:                             $dompersonnel{$server}{$item};
 5879:                     }
 5880:                 }
 5881:             }
 5882:             if (ref($privileged{$dom}) eq 'HASH') {
 5883:                 foreach my $role (@{$roles}) {
 5884:                     if (ref($privileged{$dom}{$role}) eq 'HASH') {
 5885:                         &do_cache_new('priv_'.$role,$dom,$privileged{$dom}{$role},$cachetime);
 5886:                     } else {
 5887:                         my %hash = ();
 5888:                         &do_cache_new('priv_'.$role,$dom,\%hash,$cachetime);
 5889:                     }
 5890:                 }
 5891:             }
 5892:         }
 5893:     }
 5894:     return %privileged;
 5895: }
 5896: 
 5897: # -------------------------------------------------------- Get user privileges
 5898: 
 5899: sub rolesinit {
 5900:     my ($domain, $username) = @_;
 5901:     my %userroles = ('user.login.time' => time);
 5902:     my %rolesdump = &dump("roles", $domain, $username) or return \%userroles;
 5903: 
 5904:     # firstaccess and timerinterval are related to timed maps/resources. 
 5905:     # also, blocking can be triggered by an activating timer
 5906:     # it's saved in the user's %env.
 5907:     my %firstaccess = &dump('firstaccesstimes', $domain, $username);
 5908:     my %timerinterval = &dump('timerinterval', $domain, $username);
 5909:     my (%coursetimerstarts, %firstaccchk, %firstaccenv, %coursetimerintervals,
 5910:         %timerintchk, %timerintenv);
 5911: 
 5912:     foreach my $key (keys(%firstaccess)) {
 5913:         my ($cid, $rest) = split(/\0/, $key);
 5914:         $coursetimerstarts{$cid}{$rest} = $firstaccess{$key};
 5915:     }
 5916: 
 5917:     foreach my $key (keys(%timerinterval)) {
 5918:         my ($cid,$rest) = split(/\0/,$key);
 5919:         $coursetimerintervals{$cid}{$rest} = $timerinterval{$key};
 5920:     }
 5921: 
 5922:     my %allroles=();
 5923:     my %allgroups=();
 5924: 
 5925:     for my $area (grep { ! /^rolesdef_/ } keys(%rolesdump)) {
 5926:         my $role = $rolesdump{$area};
 5927:         $area =~ s/\_\w\w$//;
 5928: 
 5929:         my ($trole, $tend, $tstart, $group_privs);
 5930: 
 5931:         if ($role =~ /^cr/) {
 5932:         # Custom role, defined by a user 
 5933:         # e.g., user.role.cr/msu/smith/mynewrole
 5934:             if ($role =~ m|^(cr/$match_domain/$match_username/[a-zA-Z0-9]+)_(.*)$|) {
 5935:                 $trole = $1;
 5936:                 ($tend, $tstart) = split('_', $2);
 5937:             } else {
 5938:                 $trole = $role;
 5939:             }
 5940:         } elsif ($role =~ m|^gr/|) {
 5941:         # Role of member in a group, defined within a course/community
 5942:         # e.g., user.role.gr/msu/04935610a19ee4a5fmsul1/leopards
 5943:             ($trole, $tend, $tstart) = split(/_/, $role);
 5944:             next if $tstart eq '-1';
 5945:             ($trole, $group_privs) = split(/\//, $trole);
 5946:             $group_privs = &unescape($group_privs);
 5947:         } else {
 5948:         # Just a normal role, defined in roles.tab
 5949:             ($trole, $tend, $tstart) = split(/_/,$role);
 5950:         }
 5951: 
 5952:         my %new_role = &set_arearole($trole,$area,$tstart,$tend,$domain,
 5953:                  $username);
 5954:         @userroles{keys(%new_role)} = @new_role{keys(%new_role)};
 5955: 
 5956:         # role expired or not available yet?
 5957:         $trole = '' if ($tend != 0 && $tend < $userroles{'user.login.time'}) or 
 5958:             ($tstart != 0 && $tstart > $userroles{'user.login.time'});
 5959: 
 5960:         next if $area eq '' or $trole eq '';
 5961: 
 5962:         my $spec = "$trole.$area";
 5963:         my ($tdummy, $tdomain, $trest) = split(/\//, $area);
 5964: 
 5965:         if ($trole =~ /^cr\//) {
 5966:         # Custom role, defined by a user
 5967:             &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
 5968:         } elsif ($trole eq 'gr') {
 5969:         # Role of a member in a group, defined within a course/community
 5970:             &group_roleprivs(\%allgroups,$area,$group_privs,$tend,$tstart);
 5971:             next;
 5972:         } else {
 5973:         # Normal role, defined in roles.tab
 5974:             &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
 5975:         }
 5976: 
 5977:         my $cid = $tdomain.'_'.$trest;
 5978:         unless ($firstaccchk{$cid}) {
 5979:             if (ref($coursetimerstarts{$cid}) eq 'HASH') {
 5980:                 foreach my $item (keys(%{$coursetimerstarts{$cid}})) {
 5981:                     $firstaccenv{'course.'.$cid.'.firstaccess.'.$item} = 
 5982:                         $coursetimerstarts{$cid}{$item}; 
 5983:                 }
 5984:             }
 5985:             $firstaccchk{$cid} = 1;
 5986:         }
 5987:         unless ($timerintchk{$cid}) {
 5988:             if (ref($coursetimerintervals{$cid}) eq 'HASH') {
 5989:                 foreach my $item (keys(%{$coursetimerintervals{$cid}})) {
 5990:                     $timerintenv{'course.'.$cid.'.timerinterval.'.$item} =
 5991:                        $coursetimerintervals{$cid}{$item};
 5992:                 }
 5993:             }
 5994:             $timerintchk{$cid} = 1;
 5995:         }
 5996:     }
 5997: 
 5998:     @userroles{'user.author','user.adv','user.rar'} = &set_userprivs(\%userroles,
 5999:                                                           \%allroles, \%allgroups);
 6000:     $env{'user.adv'} = $userroles{'user.adv'};
 6001:     $env{'user.rar'} = $userroles{'user.rar'};
 6002: 
 6003:     return (\%userroles,\%firstaccenv,\%timerintenv);
 6004: }
 6005: 
 6006: sub set_arearole {
 6007:     my ($trole,$area,$tstart,$tend,$domain,$username,$nolog) = @_;
 6008:     unless ($nolog) {
 6009: # log the associated role with the area
 6010:         &userrolelog($trole,$username,$domain,$area,$tstart,$tend);
 6011:     }
 6012:     return ('user.role.'.$trole.'.'.$area => $tstart.'.'.$tend);
 6013: }
 6014: 
 6015: sub custom_roleprivs {
 6016:     my ($allroles,$trole,$tdomain,$trest,$spec,$area) = @_;
 6017:     my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$trole);
 6018:     my $homsvr = &homeserver($rauthor,$rdomain);
 6019:     if (&hostname($homsvr) ne '') {
 6020:         my ($rdummy,$roledef)=
 6021:             &get('roles',["rolesdef_$rrole"],$rdomain,$rauthor);
 6022:         if (($rdummy ne 'con_lost') && ($roledef ne '')) {
 6023:             my ($syspriv,$dompriv,$coursepriv)=split(/\_/,$roledef);
 6024:             if (defined($syspriv)) {
 6025:                 if ($trest =~ /^$match_community$/) {
 6026:                     $syspriv =~ s/bre\&S//; 
 6027:                 }
 6028:                 $$allroles{'cm./'}.=':'.$syspriv;
 6029:                 $$allroles{$spec.'./'}.=':'.$syspriv;
 6030:             }
 6031:             if ($tdomain ne '') {
 6032:                 if (defined($dompriv)) {
 6033:                     $$allroles{'cm./'.$tdomain.'/'}.=':'.$dompriv;
 6034:                     $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$dompriv;
 6035:                 }
 6036:                 if (($trest ne '') && (defined($coursepriv))) {
 6037:                     if ($trole =~ m{^cr/$tdomain/$tdomain\Q-domainconfig\E/([^/]+)$}) {
 6038:                         my $rolename = $1;
 6039:                         $coursepriv = &course_adhocrole_privs($rolename,$tdomain,$trest,$coursepriv);
 6040:                     }
 6041:                     $$allroles{'cm.'.$area}.=':'.$coursepriv;
 6042:                     $$allroles{$spec.'.'.$area}.=':'.$coursepriv;
 6043:                 }
 6044:             }
 6045:         }
 6046:     }
 6047: }
 6048: 
 6049: sub course_adhocrole_privs {
 6050:     my ($rolename,$cdom,$cnum,$coursepriv) = @_;
 6051:     my %overrides = &get('environment',["internal.adhocpriv.$rolename"],$cdom,$cnum);
 6052:     if ($overrides{"internal.adhocpriv.$rolename"}) {
 6053:         my (%currprivs,%storeprivs);
 6054:         foreach my $item (split(/:/,$coursepriv)) {
 6055:             my ($priv,$restrict) = split(/\&/,$item);
 6056:             $currprivs{$priv} = $restrict;
 6057:         }
 6058:         my (%possadd,%possremove,%full);
 6059:         foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:c'})) {
 6060:             my ($priv,$restrict)=split(/\&/,$item);
 6061:             $full{$priv} = $restrict;
 6062:         }
 6063:         foreach my $item (split(/,/,$overrides{"internal.adhocpriv.$rolename"})) {
 6064:              next if ($item eq '');
 6065:              my ($rule,$rest) = split(/=/,$item);
 6066:              next unless (($rule eq 'off') || ($rule eq 'on'));
 6067:              foreach my $priv (split(/:/,$rest)) {
 6068:                  if ($priv ne '') {
 6069:                      if ($rule eq 'off') {
 6070:                          $possremove{$priv} = 1;
 6071:                      } else {
 6072:                          $possadd{$priv} = 1;
 6073:                      }
 6074:                  }
 6075:              }
 6076:          }
 6077:          foreach my $priv (sort(keys(%full))) {
 6078:              if (exists($currprivs{$priv})) {
 6079:                  unless (exists($possremove{$priv})) {
 6080:                      $storeprivs{$priv} = $currprivs{$priv};
 6081:                  }
 6082:              } elsif (exists($possadd{$priv})) {
 6083:                  $storeprivs{$priv} = $full{$priv};
 6084:              }
 6085:          }
 6086:          $coursepriv = ':'.join(':',map { $_.'&'.$storeprivs{$_}; } sort(keys(%storeprivs)));
 6087:      }
 6088:      return $coursepriv;
 6089: }
 6090: 
 6091: sub group_roleprivs {
 6092:     my ($allgroups,$area,$group_privs,$tend,$tstart) = @_;
 6093:     my $access = 1;
 6094:     my $now = time;
 6095:     if (($tend!=0) && ($tend<$now)) { $access = 0; }
 6096:     if (($tstart!=0) && ($tstart>$now)) { $access=0; }
 6097:     if ($access) {
 6098:         my ($course,$group) = ($area =~ m|(/$match_domain/$match_courseid)/([^/]+)$|);
 6099:         $$allgroups{$course}{$group} .=':'.$group_privs;
 6100:     }
 6101: }
 6102: 
 6103: sub standard_roleprivs {
 6104:     my ($allroles,$trole,$tdomain,$spec,$trest,$area) = @_;
 6105:     if (defined($pr{$trole.':s'})) {
 6106:         $$allroles{'cm./'}.=':'.$pr{$trole.':s'};
 6107:         $$allroles{$spec.'./'}.=':'.$pr{$trole.':s'};
 6108:     }
 6109:     if ($tdomain ne '') {
 6110:         if (defined($pr{$trole.':d'})) {
 6111:             $$allroles{'cm./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
 6112:             $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
 6113:         }
 6114:         if (($trest ne '') && (defined($pr{$trole.':c'}))) {
 6115:             $$allroles{'cm.'.$area}.=':'.$pr{$trole.':c'};
 6116:             $$allroles{$spec.'.'.$area}.=':'.$pr{$trole.':c'};
 6117:         }
 6118:     }
 6119: }
 6120: 
 6121: sub set_userprivs {
 6122:     my ($userroles,$allroles,$allgroups,$groups_roles) = @_; 
 6123:     my $author=0;
 6124:     my $adv=0;
 6125:     my $rar=0;
 6126:     my %grouproles = ();
 6127:     if (keys(%{$allgroups}) > 0) {
 6128:         my @groupkeys; 
 6129:         foreach my $role (keys(%{$allroles})) {
 6130:             push(@groupkeys,$role);
 6131:         }
 6132:         if (ref($groups_roles) eq 'HASH') {
 6133:             foreach my $key (keys(%{$groups_roles})) {
 6134:                 unless (grep(/^\Q$key\E$/,@groupkeys)) {
 6135:                     push(@groupkeys,$key);
 6136:                 }
 6137:             }
 6138:         }
 6139:         if (@groupkeys > 0) {
 6140:             foreach my $role (@groupkeys) {
 6141:                 my ($trole,$area,$sec,$extendedarea);
 6142:                 if ($role =~ m-^(\w+|cr/$match_domain/$match_username/\w+)\.(/$match_domain/$match_courseid)(/?\w*)\.-) {
 6143:                     $trole = $1;
 6144:                     $area = $2;
 6145:                     $sec = $3;
 6146:                     $extendedarea = $area.$sec;
 6147:                     if (exists($$allgroups{$area})) {
 6148:                         foreach my $group (keys(%{$$allgroups{$area}})) {
 6149:                             my $spec = $trole.'.'.$extendedarea;
 6150:                             $grouproles{$spec.'.'.$area.'/'.$group} = 
 6151:                                                 $$allgroups{$area}{$group};
 6152:                         }
 6153:                     }
 6154:                 }
 6155:             }
 6156:         }
 6157:     }
 6158:     foreach my $group (keys(%grouproles)) {
 6159:         $$allroles{$group} = $grouproles{$group};
 6160:     }
 6161:     foreach my $role (keys(%{$allroles})) {
 6162:         my %thesepriv;
 6163:         if (($role=~/^au/) || ($role=~/^ca/) || ($role=~/^aa/)) { $author=1; }
 6164:         foreach my $item (split(/:/,$$allroles{$role})) {
 6165:             if ($item ne '') {
 6166:                 my ($privilege,$restrictions)=split(/&/,$item);
 6167:                 if ($restrictions eq '') {
 6168:                     $thesepriv{$privilege}='F';
 6169:                 } elsif ($thesepriv{$privilege} ne 'F') {
 6170:                     $thesepriv{$privilege}.=$restrictions;
 6171:                 }
 6172:                 if ($thesepriv{'adv'} eq 'F') { $adv=1; }
 6173:                 if ($thesepriv{'rar'} eq 'F') { $rar=1; }
 6174:             }
 6175:         }
 6176:         my $thesestr='';
 6177:         foreach my $priv (sort(keys(%thesepriv))) {
 6178: 	    $thesestr.=':'.$priv.'&'.$thesepriv{$priv};
 6179: 	}
 6180:         $userroles->{'user.priv.'.$role} = $thesestr;
 6181:     }
 6182:     return ($author,$adv,$rar);
 6183: }
 6184: 
 6185: sub role_status {
 6186:     my ($rolekey,$update,$refresh,$now,$role,$where,$trolecode,$tstatus,$tstart,$tend) = @_;
 6187:     if (exists($env{$rolekey}) && $env{$rolekey} ne '') {
 6188:         my ($one,$two) = split(m{\./},$rolekey,2);
 6189:         (undef,undef,$$role) = split(/\./,$one,3);
 6190:         unless (!defined($$role) || $$role eq '') {
 6191:             $$where = '/'.$two;
 6192:             $$trolecode=$$role.'.'.$$where;
 6193:             ($$tstart,$$tend)=split(/\./,$env{$rolekey});
 6194:             $$tstatus='is';
 6195:             if ($$tstart && $$tstart>$update) {
 6196:                 $$tstatus='future';
 6197:                 if ($$tstart<$now) {
 6198:                     if ($$tstart && $$tstart>$refresh) {
 6199:                         if (($$where ne '') && ($$role ne '')) {
 6200:                             my (%allroles,%allgroups,$group_privs,
 6201:                                 %groups_roles,@rolecodes);
 6202:                             my %userroles = (
 6203:                                 'user.role.'.$$role.'.'.$$where => $$tstart.'.'.$$tend
 6204:                             );
 6205:                             @rolecodes = ('cm'); 
 6206:                             my $spec=$$role.'.'.$$where;
 6207:                             my ($tdummy,$tdomain,$trest)=split(/\//,$$where);
 6208:                             if ($$role =~ /^cr\//) {
 6209:                                 &custom_roleprivs(\%allroles,$$role,$tdomain,$trest,$spec,$$where);
 6210:                                 push(@rolecodes,'cr');
 6211:                             } elsif ($$role eq 'gr') {
 6212:                                 push(@rolecodes,$$role);
 6213:                                 my %rolehash = &get('roles',[$$where.'_'.$$role],$env{'user.domain'},
 6214:                                                     $env{'user.name'});
 6215:                                 my ($trole) = split('_',$rolehash{$$where.'_'.$$role},2);
 6216:                                 (undef,my $group_privs) = split(/\//,$trole);
 6217:                                 $group_privs = &unescape($group_privs);
 6218:                                 &group_roleprivs(\%allgroups,$$where,$group_privs,$$tend,$$tstart);
 6219:                                 my %course_roles = &get_my_roles($env{'user.name'},$env{'user.domain'},'userroles',['active'],['cc','co','in','ta','ep','ad','st','cr'],[$tdomain],1);
 6220:                                 &get_groups_roles($tdomain,$trest,
 6221:                                                   \%course_roles,\@rolecodes,
 6222:                                                   \%groups_roles);
 6223:                             } else {
 6224:                                 push(@rolecodes,$$role);
 6225:                                 &standard_roleprivs(\%allroles,$$role,$tdomain,$spec,$trest,$$where);
 6226:                             }
 6227:                             my ($author,$adv,$rar)= &set_userprivs(\%userroles,\%allroles,\%allgroups,
 6228:                                                                    \%groups_roles);
 6229:                             &appenv(\%userroles,\@rolecodes);
 6230:                             &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},"Role ".$spec);
 6231:                         }
 6232:                     }
 6233:                     $$tstatus = 'is';
 6234:                 }
 6235:             }
 6236:             if ($$tend) {
 6237:                 if ($$tend<$update) {
 6238:                     $$tstatus='expired';
 6239:                 } elsif ($$tend<$now) {
 6240:                     $$tstatus='will_not';
 6241:                 }
 6242:             }
 6243:         }
 6244:     }
 6245: }
 6246: 
 6247: sub get_groups_roles {
 6248:     my ($cdom,$rest,$cdom_courseroles,$rolecodes,$groups_roles) = @_;
 6249:     return unless((ref($cdom_courseroles) eq 'HASH') && 
 6250:                   (ref($rolecodes) eq 'ARRAY') && 
 6251:                   (ref($groups_roles) eq 'HASH')); 
 6252:     if (keys(%{$cdom_courseroles}) > 0) {
 6253:         my ($cnum) = ($rest =~ /^($match_courseid)/);
 6254:         if ($cdom ne '' && $cnum ne '') {
 6255:             foreach my $key (keys(%{$cdom_courseroles})) {
 6256:                 if ($key =~ /^\Q$cnum\E:\Q$cdom\E:([^:]+):?([^:]*)/) {
 6257:                     my $crsrole = $1;
 6258:                     my $crssec = $2;
 6259:                     if ($crsrole =~ /^cr/) {
 6260:                         unless (grep(/^cr$/,@{$rolecodes})) {
 6261:                             push(@{$rolecodes},'cr');
 6262:                         }
 6263:                     } else {
 6264:                         unless(grep(/^\Q$crsrole\E$/,@{$rolecodes})) {
 6265:                             push(@{$rolecodes},$crsrole);
 6266:                         }
 6267:                     }
 6268:                     my $rolekey = "$crsrole./$cdom/$cnum";
 6269:                     if ($crssec ne '') {
 6270:                         $rolekey .= "/$crssec";
 6271:                     }
 6272:                     $rolekey .= './';
 6273:                     $groups_roles->{$rolekey} = $rolecodes;
 6274:                 }
 6275:             }
 6276:         }
 6277:     }
 6278:     return;
 6279: }
 6280: 
 6281: sub delete_env_groupprivs {
 6282:     my ($where,$courseroles,$possroles) = @_;
 6283:     return unless((ref($courseroles) eq 'HASH') && (ref($possroles) eq 'ARRAY'));
 6284:     my ($dummy,$udom,$uname,$group) = split(/\//,$where);
 6285:     unless (ref($courseroles->{$udom}) eq 'HASH') {
 6286:         %{$courseroles->{$udom}} =
 6287:             &get_my_roles('','','userroles',['active'],
 6288:                           $possroles,[$udom],1);
 6289:     }
 6290:     if (ref($courseroles->{$udom}) eq 'HASH') {
 6291:         foreach my $item (keys(%{$courseroles->{$udom}})) {
 6292:             my ($cnum,$cdom,$crsrole,$crssec) = split(/:/,$item);
 6293:             my $area = '/'.$cdom.'/'.$cnum;
 6294:             my $privkey = "user.priv.$crsrole.$area";
 6295:             if ($crssec ne '') {
 6296:                 $privkey .= '/'.$crssec;
 6297:             }
 6298:             $privkey .= ".$area/$group";
 6299:             &Apache::lonnet::delenv($privkey,undef,[$crsrole]);
 6300:         }
 6301:     }
 6302:     return;
 6303: }
 6304: 
 6305: sub check_adhoc_privs {
 6306:     my ($cdom,$cnum,$update,$refresh,$now,$checkrole,$caller,$sec) = @_;
 6307:     my $cckey = 'user.role.'.$checkrole.'./'.$cdom.'/'.$cnum;
 6308:     if ($sec) {
 6309:         $cckey .= '/'.$sec;
 6310:     } 
 6311:     my $setprivs;
 6312:     if ($env{$cckey}) {
 6313:         my ($role,$where,$trolecode,$tstart,$tend,$tremark,$tstatus,$tpstart,$tpend);
 6314:         &role_status($cckey,$update,$refresh,$now,\$role,\$where,\$trolecode,\$tstatus,\$tstart,\$tend);
 6315:         unless (($tstatus eq 'is') || ($tstatus eq 'will_not')) {
 6316:             &set_adhoc_privileges($cdom,$cnum,$checkrole,$caller,$sec);
 6317:             $setprivs = 1;
 6318:         }
 6319:     } else {
 6320:         &set_adhoc_privileges($cdom,$cnum,$checkrole,$caller,$sec);
 6321:         $setprivs = 1;
 6322:     }
 6323:     return $setprivs;
 6324: }
 6325: 
 6326: sub set_adhoc_privileges {
 6327: # role can be cc, ca, or cr/<dom>/<dom>-domainconfig/role
 6328:     my ($dcdom,$pickedcourse,$role,$caller,$sec) = @_;
 6329:     my $area = '/'.$dcdom.'/'.$pickedcourse;
 6330:     if ($sec ne '') {
 6331:         $area .= '/'.$sec;
 6332:     }
 6333:     my $spec = $role.'.'.$area;
 6334:     my %userroles = &set_arearole($role,$area,'','',$env{'user.domain'},
 6335:                                   $env{'user.name'},1);
 6336:     my %rolehash = ();
 6337:     if ($role =~ m{^\Qcr/$dcdom/$dcdom\E\-domainconfig/(\w+)$}) {
 6338:         my $rolename = $1;
 6339:         &custom_roleprivs(\%rolehash,$role,$dcdom,$pickedcourse,$spec,$area);
 6340:         my %domdef = &get_domain_defaults($dcdom);
 6341:         if (ref($domdef{'adhocroles'}) eq 'HASH') {
 6342:             if (ref($domdef{'adhocroles'}{$rolename}) eq 'HASH') {
 6343:                 &appenv({'request.role.desc' => $domdef{'adhocroles'}{$rolename}{'desc'},});
 6344:             }
 6345:         }
 6346:     } else {
 6347:         &standard_roleprivs(\%rolehash,$role,$dcdom,$spec,$pickedcourse,$area);
 6348:     }
 6349:     my ($author,$adv,$rar)= &set_userprivs(\%userroles,\%rolehash);
 6350:     &appenv(\%userroles,[$role,'cm']);
 6351:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},"Role ".$spec);
 6352:     unless ($caller eq 'constructaccess' && $env{'request.course.id'}) {
 6353:         &appenv( {'request.role'        => $spec,
 6354:                   'request.role.domain' => $dcdom,
 6355:                   'request.course.sec'  => $sec,
 6356:                  }
 6357:                );
 6358:         my $tadv=0;
 6359:         if (&allowed('adv') eq 'F') { $tadv=1; }
 6360:         &appenv({'request.role.adv'    => $tadv});
 6361:     }
 6362: }
 6363: 
 6364: # --------------------------------------------------------------- get interface
 6365: 
 6366: sub get {
 6367:    my ($namespace,$storearr,$udomain,$uname)=@_;
 6368:    my $items='';
 6369:    foreach my $item (@$storearr) {
 6370:        $items.=&escape($item).'&';
 6371:    }
 6372:    $items=~s/\&$//;
 6373:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 6374:    if (!$uname) { $uname=$env{'user.name'}; }
 6375:    my $uhome=&homeserver($uname,$udomain);
 6376: 
 6377:    my $rep=&reply("get:$udomain:$uname:$namespace:$items",$uhome);
 6378:    my @pairs=split(/\&/,$rep);
 6379:    if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
 6380:      return @pairs;
 6381:    }
 6382:    my %returnhash=();
 6383:    my $i=0;
 6384:    foreach my $item (@$storearr) {
 6385:       $returnhash{$item}=&thaw_unescape($pairs[$i]);
 6386:       $i++;
 6387:    }
 6388:    return %returnhash;
 6389: }
 6390: 
 6391: # --------------------------------------------------------------- del interface
 6392: 
 6393: sub del {
 6394:    my ($namespace,$storearr,$udomain,$uname)=@_;
 6395:    my $items='';
 6396:    foreach my $item (@$storearr) {
 6397:        $items.=&escape($item).'&';
 6398:    }
 6399: 
 6400:    $items=~s/\&$//;
 6401:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 6402:    if (!$uname) { $uname=$env{'user.name'}; }
 6403:    my $uhome=&homeserver($uname,$udomain);
 6404:    return &reply("del:$udomain:$uname:$namespace:$items",$uhome);
 6405: }
 6406: 
 6407: # -------------------------------------------------------------- dump interface
 6408: 
 6409: sub unserialize {
 6410:     my ($rep, $escapedkeys) = @_;
 6411: 
 6412:     return {} if $rep =~ /^error/;
 6413: 
 6414:     my %returnhash=();
 6415: 	foreach my $item (split(/\&/,$rep)) {
 6416: 	    my ($key, $value) = split(/=/, $item, 2);
 6417: 	    $key = unescape($key) unless $escapedkeys;
 6418: 	    next if $key =~ /^error: 2 /;
 6419: 	    $returnhash{$key} = &thaw_unescape($value);
 6420: 	}
 6421:     #return %returnhash;
 6422:     return \%returnhash;
 6423: }        
 6424: 
 6425: # see Lond::dump_with_regexp
 6426: # if $escapedkeys hash keys won't get unescaped.
 6427: sub dump {
 6428:     my ($namespace,$udomain,$uname,$regexp,$range,$escapedkeys)=@_;
 6429:     if (!$udomain) { $udomain=$env{'user.domain'}; }
 6430:     if (!$uname) { $uname=$env{'user.name'}; }
 6431:     my $uhome=&homeserver($uname,$udomain);
 6432: 
 6433:     if ($regexp) {
 6434:         $regexp=&escape($regexp);
 6435:     } else {
 6436:         $regexp='.';
 6437:     }
 6438:     if (grep { $_ eq $uhome } current_machine_ids()) {
 6439:         # user is hosted on this machine
 6440:         my $reply = LONCAPA::Lond::dump_with_regexp(join(":", ($udomain,
 6441:                     $uname, $namespace, $regexp, $range)), $perlvar{'lonVersion'});
 6442:         return %{unserialize($reply, $escapedkeys)};
 6443:     }
 6444:     my $rep=&reply("dump:$udomain:$uname:$namespace:$regexp:$range",$uhome);
 6445:     my @pairs=split(/\&/,$rep);
 6446:     my %returnhash=();
 6447:     if (!($rep =~ /^error/ )) {
 6448: 	foreach my $item (@pairs) {
 6449: 	    my ($key,$value)=split(/=/,$item,2);
 6450:         $key = unescape($key) unless $escapedkeys;
 6451:         #$key = &unescape($key);
 6452: 	    next if ($key =~ /^error: 2 /);
 6453: 	    $returnhash{$key}=&thaw_unescape($value);
 6454: 	}
 6455:     }
 6456:     return %returnhash;
 6457: }
 6458: 
 6459: 
 6460: # --------------------------------------------------------- dumpstore interface
 6461: 
 6462: sub dumpstore {
 6463:    my ($namespace,$udomain,$uname,$regexp,$range)=@_;
 6464:    # same as dump but keys must be escaped. They may contain colon separated
 6465:    # lists of values that may themself contain colons (e.g. symbs).
 6466:    return &dump($namespace, $udomain, $uname, $regexp, $range, 1);
 6467: }
 6468: 
 6469: # -------------------------------------------------------------- keys interface
 6470: 
 6471: sub getkeys {
 6472:    my ($namespace,$udomain,$uname)=@_;
 6473:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 6474:    if (!$uname) { $uname=$env{'user.name'}; }
 6475:    my $uhome=&homeserver($uname,$udomain);
 6476:    my $rep=reply("keys:$udomain:$uname:$namespace",$uhome);
 6477:    my @keyarray=();
 6478:    foreach my $key (split(/\&/,$rep)) {
 6479:       next if ($key =~ /^error: 2 /);
 6480:       push(@keyarray,&unescape($key));
 6481:    }
 6482:    return @keyarray;
 6483: }
 6484: 
 6485: # --------------------------------------------------------------- currentdump
 6486: sub currentdump {
 6487:    my ($courseid,$sdom,$sname)=@_;
 6488:    $courseid = $env{'request.course.id'} if (! defined($courseid));
 6489:    $sdom     = $env{'user.domain'}       if (! defined($sdom));
 6490:    $sname    = $env{'user.name'}         if (! defined($sname));
 6491:    my $uhome = &homeserver($sname,$sdom);
 6492:    my $rep;
 6493: 
 6494:    if (grep { $_ eq $uhome } current_machine_ids()) {
 6495:        $rep = LONCAPA::Lond::dump_profile_database(join(":", ($sdom, $sname, 
 6496:                    $courseid)));
 6497:    } else {
 6498:        $rep = reply('currentdump:'.$sdom.':'.$sname.':'.$courseid,$uhome);
 6499:    }
 6500: 
 6501:    return if ($rep =~ /^(error:|no_such_host)/);
 6502:    #
 6503:    my %returnhash=();
 6504:    #
 6505:    if ($rep eq 'unknown_cmd') {
 6506:        # an old lond will not know currentdump
 6507:        # Do a dump and make it look like a currentdump
 6508:        my @tmp = &dumpstore($courseid,$sdom,$sname,'.');
 6509:        return if ($tmp[0] =~ /^(error:|no_such_host)/);
 6510:        my %hash = @tmp;
 6511:        @tmp=();
 6512:        %returnhash = %{&convert_dump_to_currentdump(\%hash)};
 6513:    } else {
 6514:        my @pairs=split(/\&/,$rep);
 6515:        foreach my $pair (@pairs) {
 6516:            my ($key,$value)=split(/=/,$pair,2);
 6517:            my ($symb,$param) = split(/:/,$key);
 6518:            $returnhash{&unescape($symb)}->{&unescape($param)} = 
 6519:                                                         &thaw_unescape($value);
 6520:        }
 6521:    }
 6522:    return %returnhash;
 6523: }
 6524: 
 6525: sub convert_dump_to_currentdump{
 6526:     my %hash = %{shift()};
 6527:     my %returnhash;
 6528:     # Code ripped from lond, essentially.  The only difference
 6529:     # here is the unescaping done by lonnet::dump().  Conceivably
 6530:     # we might run in to problems with parameter names =~ /^v\./
 6531:     while (my ($key,$value) = each(%hash)) {
 6532:         my ($v,$symb,$param) = split(/:/,$key);
 6533: 	$symb  = &unescape($symb);
 6534: 	$param = &unescape($param);
 6535:         next if ($v eq 'version' || $symb eq 'keys');
 6536:         next if (exists($returnhash{$symb}) &&
 6537:                  exists($returnhash{$symb}->{$param}) &&
 6538:                  $returnhash{$symb}->{'v.'.$param} > $v);
 6539:         $returnhash{$symb}->{$param}=$value;
 6540:         $returnhash{$symb}->{'v.'.$param}=$v;
 6541:     }
 6542:     #
 6543:     # Remove all of the keys in the hashes which keep track of
 6544:     # the version of the parameter.
 6545:     while (my ($symb,$param_hash) = each(%returnhash)) {
 6546:         # use a foreach because we are going to delete from the hash.
 6547:         foreach my $key (keys(%$param_hash)) {
 6548:             delete($param_hash->{$key}) if ($key =~ /^v\./);
 6549:         }
 6550:     }
 6551:     return \%returnhash;
 6552: }
 6553: 
 6554: # ------------------------------------------------------ critical inc interface
 6555: 
 6556: sub cinc {
 6557:     return &inc(@_,'critical');
 6558: }
 6559: 
 6560: # --------------------------------------------------------------- inc interface
 6561: 
 6562: sub inc {
 6563:     my ($namespace,$store,$udomain,$uname,$critical) = @_;
 6564:     if (!$udomain) { $udomain=$env{'user.domain'}; }
 6565:     if (!$uname) { $uname=$env{'user.name'}; }
 6566:     my $uhome=&homeserver($uname,$udomain);
 6567:     my $items='';
 6568:     if (! ref($store)) {
 6569:         # got a single value, so use that instead
 6570:         $items = &escape($store).'=&';
 6571:     } elsif (ref($store) eq 'SCALAR') {
 6572:         $items = &escape($$store).'=&';        
 6573:     } elsif (ref($store) eq 'ARRAY') {
 6574:         $items = join('=&',map {&escape($_);} @{$store});
 6575:     } elsif (ref($store) eq 'HASH') {
 6576:         while (my($key,$value) = each(%{$store})) {
 6577:             $items.= &escape($key).'='.&escape($value).'&';
 6578:         }
 6579:     }
 6580:     $items=~s/\&$//;
 6581:     if ($critical) {
 6582: 	return &critical("inc:$udomain:$uname:$namespace:$items",$uhome);
 6583:     } else {
 6584: 	return &reply("inc:$udomain:$uname:$namespace:$items",$uhome);
 6585:     }
 6586: }
 6587: 
 6588: # --------------------------------------------------------------- put interface
 6589: 
 6590: sub put {
 6591:    my ($namespace,$storehash,$udomain,$uname)=@_;
 6592:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 6593:    if (!$uname) { $uname=$env{'user.name'}; }
 6594:    my $uhome=&homeserver($uname,$udomain);
 6595:    my $items='';
 6596:    foreach my $item (keys(%$storehash)) {
 6597:        $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
 6598:    }
 6599:    $items=~s/\&$//;
 6600:    return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
 6601: }
 6602: 
 6603: # ------------------------------------------------------------ newput interface
 6604: 
 6605: sub newput {
 6606:    my ($namespace,$storehash,$udomain,$uname)=@_;
 6607:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 6608:    if (!$uname) { $uname=$env{'user.name'}; }
 6609:    my $uhome=&homeserver($uname,$udomain);
 6610:    my $items='';
 6611:    foreach my $key (keys(%$storehash)) {
 6612:        $items.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
 6613:    }
 6614:    $items=~s/\&$//;
 6615:    return &reply("newput:$udomain:$uname:$namespace:$items",$uhome);
 6616: }
 6617: 
 6618: # ---------------------------------------------------------  putstore interface
 6619: 
 6620: sub putstore {
 6621:    my ($namespace,$symb,$version,$storehash,$udomain,$uname,$tolog)=@_;
 6622:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 6623:    if (!$uname) { $uname=$env{'user.name'}; }
 6624:    my $uhome=&homeserver($uname,$udomain);
 6625:    my $items='';
 6626:    foreach my $key (keys(%$storehash)) {
 6627:        $items.= &escape($key).'='.&freeze_escape($storehash->{$key}).'&';
 6628:    }
 6629:    $items=~s/\&$//;
 6630:    my $esc_symb=&escape($symb);
 6631:    my $esc_v=&escape($version);
 6632:    my $reply =
 6633:        &reply("putstore:$udomain:$uname:$namespace:$esc_symb:$esc_v:$items",
 6634: 	      $uhome);
 6635:    if (($tolog) && ($reply eq 'ok')) {
 6636:        my $namevalue='';
 6637:        foreach my $key (keys(%{$storehash})) {
 6638:            $namevalue.=&escape($key).'='.&freeze_escape($storehash->{$key}).'&';
 6639:        }
 6640:        $namevalue .= 'ip='.&escape($ENV{'REMOTE_ADDR'}).
 6641:                      '&host='.&escape($perlvar{'lonHostID'}).
 6642:                      '&version='.$esc_v.
 6643:                      '&by='.&escape($env{'user.name'}.':'.$env{'user.domain'});
 6644:        &Apache::lonnet::courselog($symb.':'.$uname.':'.$udomain.':PUTSTORE:'.$namevalue);
 6645:    }
 6646:    if ($reply eq 'unknown_cmd') {
 6647:        # gfall back to way things use to be done
 6648:        return &old_putstore($namespace,$symb,$version,$storehash,$udomain,
 6649: 			    $uname);
 6650:    }
 6651:    return $reply;
 6652: }
 6653: 
 6654: sub old_putstore {
 6655:     my ($namespace,$symb,$version,$storehash,$udomain,$uname)=@_;
 6656:     if (!$udomain) { $udomain=$env{'user.domain'}; }
 6657:     if (!$uname) { $uname=$env{'user.name'}; }
 6658:     my $uhome=&homeserver($uname,$udomain);
 6659:     my %newstorehash;
 6660:     foreach my $item (keys(%$storehash)) {
 6661: 	my $key = $version.':'.&escape($symb).':'.$item;
 6662: 	$newstorehash{$key} = $storehash->{$item};
 6663:     }
 6664:     my $items='';
 6665:     my %allitems = ();
 6666:     foreach my $item (keys(%newstorehash)) {
 6667: 	if ($item =~ m/^([^\:]+):([^\:]+):([^\:]+)$/) {
 6668: 	    my $key = $1.':keys:'.$2;
 6669: 	    $allitems{$key} .= $3.':';
 6670: 	}
 6671: 	$items.=$item.'='.&freeze_escape($newstorehash{$item}).'&';
 6672:     }
 6673:     foreach my $item (keys(%allitems)) {
 6674: 	$allitems{$item} =~ s/\:$//;
 6675: 	$items.= $item.'='.$allitems{$item}.'&';
 6676:     }
 6677:     $items=~s/\&$//;
 6678:     return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
 6679: }
 6680: 
 6681: # ------------------------------------------------------ critical put interface
 6682: 
 6683: sub cput {
 6684:    my ($namespace,$storehash,$udomain,$uname)=@_;
 6685:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 6686:    if (!$uname) { $uname=$env{'user.name'}; }
 6687:    my $uhome=&homeserver($uname,$udomain);
 6688:    my $items='';
 6689:    foreach my $item (keys(%$storehash)) {
 6690:        $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
 6691:    }
 6692:    $items=~s/\&$//;
 6693:    return &critical("put:$udomain:$uname:$namespace:$items",$uhome);
 6694: }
 6695: 
 6696: # -------------------------------------------------------------- eget interface
 6697: 
 6698: sub eget {
 6699:    my ($namespace,$storearr,$udomain,$uname)=@_;
 6700:    my $items='';
 6701:    foreach my $item (@$storearr) {
 6702:        $items.=&escape($item).'&';
 6703:    }
 6704:    $items=~s/\&$//;
 6705:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 6706:    if (!$uname) { $uname=$env{'user.name'}; }
 6707:    my $uhome=&homeserver($uname,$udomain);
 6708:    my $rep=&reply("eget:$udomain:$uname:$namespace:$items",$uhome);
 6709:    my @pairs=split(/\&/,$rep);
 6710:    my %returnhash=();
 6711:    my $i=0;
 6712:    foreach my $item (@$storearr) {
 6713:       $returnhash{$item}=&thaw_unescape($pairs[$i]);
 6714:       $i++;
 6715:    }
 6716:    return %returnhash;
 6717: }
 6718: 
 6719: # ------------------------------------------------------------ tmpput interface
 6720: sub tmpput {
 6721:     my ($storehash,$server,$context)=@_;
 6722:     my $items='';
 6723:     foreach my $item (keys(%$storehash)) {
 6724: 	$items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
 6725:     }
 6726:     $items=~s/\&$//;
 6727:     if (defined($context)) {
 6728:         $items .= ':'.&escape($context);
 6729:     }
 6730:     return &reply("tmpput:$items",$server);
 6731: }
 6732: 
 6733: # ------------------------------------------------------------ tmpget interface
 6734: sub tmpget {
 6735:     my ($token,$server)=@_;
 6736:     if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
 6737:     my $rep=&reply("tmpget:$token",$server);
 6738:     my %returnhash;
 6739:     if ($rep =~ /^(con_lost|error|no_such_host)/i) {
 6740:         return %returnhash;
 6741:     }
 6742:     foreach my $item (split(/\&/,$rep)) {
 6743: 	my ($key,$value)=split(/=/,$item);
 6744: 	$returnhash{&unescape($key)}=&thaw_unescape($value);
 6745:     }
 6746:     return %returnhash;
 6747: }
 6748: 
 6749: # ------------------------------------------------------------ tmpdel interface
 6750: sub tmpdel {
 6751:     my ($token,$server)=@_;
 6752:     if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
 6753:     return &reply("tmpdel:$token",$server);
 6754: }
 6755: 
 6756: # ------------------------------------------------------------ get_timebased_id 
 6757: 
 6758: sub get_timebased_id {
 6759:     my ($prefix,$keyid,$namespace,$cdom,$cnum,$idtype,$who,$locktries,
 6760:         $maxtries) = @_;
 6761:     my ($newid,$error,$dellock);
 6762:     unless (($prefix =~ /^\w+$/) && ($keyid =~ /^\w+$/) && ($namespace ne '')) {  
 6763:         return ('','ok','invalid call to get suffix');
 6764:     }
 6765: 
 6766: # set defaults for any optional args for which values were not supplied
 6767:     if ($who eq '') {
 6768:         $who = $env{'user.name'}.':'.$env{'user.domain'};
 6769:     }
 6770:     if (!$locktries) {
 6771:         $locktries = 3;
 6772:     }
 6773:     if (!$maxtries) {
 6774:         $maxtries = 10;
 6775:     }
 6776:     
 6777:     if (($cdom eq '') || ($cnum eq '')) {
 6778:         if ($env{'request.course.id'}) {
 6779:             $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 6780:             $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 6781:         }
 6782:         if (($cdom eq '') || ($cnum eq '')) {
 6783:             return ('','ok','call to get suffix not in course context');
 6784:         }
 6785:     }
 6786: 
 6787: # construct locking item
 6788:     my $lockhash = {
 6789:                       $prefix."\0".'locked_'.$keyid => $who,
 6790:                    };
 6791:     my $tries = 0;
 6792: 
 6793: # attempt to get lock on nohist_$namespace file
 6794:     my $gotlock = &Apache::lonnet::newput('nohist_'.$namespace,$lockhash,$cdom,$cnum);
 6795:     while (($gotlock ne 'ok') && $tries <$locktries) {
 6796:         $tries ++;
 6797:         sleep 1;
 6798:         $gotlock = &Apache::lonnet::newput('nohist_'.$namespace,$lockhash,$cdom,$cnum);
 6799:     }
 6800: 
 6801: # attempt to get unique identifier, based on current timestamp
 6802:     if ($gotlock eq 'ok') {
 6803:         my %inuse = &Apache::lonnet::dump('nohist_'.$namespace,$cdom,$cnum,$prefix);
 6804:         my $id = time;
 6805:         $newid = $id;
 6806:         if ($idtype eq 'addcode') {
 6807:             $newid .= &sixnum_code();
 6808:         }
 6809:         my $idtries = 0;
 6810:         while (exists($inuse{$prefix."\0".$newid}) && $idtries < $maxtries) {
 6811:             if ($idtype eq 'concat') {
 6812:                 $newid = $id.$idtries;
 6813:             } elsif ($idtype eq 'addcode') {
 6814:                 $newid = $newid.&sixnum_code();
 6815:             } else {
 6816:                 $newid ++;
 6817:             }
 6818:             $idtries ++;
 6819:         }
 6820:         if (!exists($inuse{$prefix."\0".$newid})) {
 6821:             my %new_item =  (
 6822:                               $prefix."\0".$newid => $who,
 6823:                             );
 6824:             my $putresult = &Apache::lonnet::put('nohist_'.$namespace,\%new_item,
 6825:                                                  $cdom,$cnum);
 6826:             if ($putresult ne 'ok') {
 6827:                 undef($newid);
 6828:                 $error = 'error saving new item: '.$putresult;
 6829:             }
 6830:         } else {
 6831:              undef($newid);
 6832:              $error = ('error: no unique suffix available for the new item ');
 6833:         }
 6834: #  remove lock
 6835:         my @del_lock = ($prefix."\0".'locked_'.$keyid);
 6836:         $dellock = &Apache::lonnet::del('nohist_'.$namespace,\@del_lock,$cdom,$cnum);
 6837:     } else {
 6838:         $error = "error: could not obtain lockfile\n";
 6839:         $dellock = 'ok';
 6840:         if (($prefix eq 'paste') && ($namespace eq 'courseeditor') && ($keyid eq 'num')) {
 6841:             $dellock = 'nolock';
 6842:         }
 6843:     }
 6844:     return ($newid,$dellock,$error);
 6845: }
 6846: 
 6847: sub sixnum_code {
 6848:     my $code;
 6849:     for (0..6) {
 6850:         $code .= int( rand(9) );
 6851:     }
 6852:     return $code;
 6853: }
 6854: 
 6855: # -------------------------------------------------- portfolio access checking
 6856: 
 6857: sub portfolio_access {
 6858:     my ($requrl,$clientip) = @_;
 6859:     my (undef,$udom,$unum,$file_name,$group) = &parse_portfolio_url($requrl);
 6860:     my $result = &get_portfolio_access($udom,$unum,$file_name,$group,$clientip);
 6861:     if ($result) {
 6862:         my %setters;
 6863:         if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
 6864:             my ($startblock,$endblock) =
 6865:                 &Apache::loncommon::blockcheck(\%setters,'port',$unum,$udom);
 6866:             if ($startblock && $endblock) {
 6867:                 return 'B';
 6868:             }
 6869:         } else {
 6870:             my ($startblock,$endblock) =
 6871:                 &Apache::loncommon::blockcheck(\%setters,'port');
 6872:             if ($startblock && $endblock) {
 6873:                 return 'B';
 6874:             }
 6875:         }
 6876:     }
 6877:     if ($result eq 'ok') {
 6878:        return 'F';
 6879:     } elsif ($result =~ /^[^:]+:guest_/) {
 6880:        return 'A';
 6881:     }
 6882:     return '';
 6883: }
 6884: 
 6885: sub get_portfolio_access {
 6886:     my ($udom,$unum,$file_name,$group,$clientip,$access_hash) = @_;
 6887: 
 6888:     if (!ref($access_hash)) {
 6889: 	my $current_perms = &get_portfile_permissions($udom,$unum);
 6890: 	my %access_controls = &get_access_controls($current_perms,$group,
 6891: 						   $file_name);
 6892: 	$access_hash = $access_controls{$file_name};
 6893:     }
 6894: 
 6895:     my ($public,$guest,@domains,@users,@courses,@groups,@ips);
 6896:     my $now = time;
 6897:     if (ref($access_hash) eq 'HASH') {
 6898:         foreach my $key (keys(%{$access_hash})) {
 6899:             my ($num,$scope,$end,$start) = ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
 6900:             if ($start > $now) {
 6901:                 next;
 6902:             }
 6903:             if ($end && $end<$now) {
 6904:                 next;
 6905:             }
 6906:             if ($scope eq 'public') {
 6907:                 $public = $key;
 6908:                 last;
 6909:             } elsif ($scope eq 'guest') {
 6910:                 $guest = $key;
 6911:             } elsif ($scope eq 'domains') {
 6912:                 push(@domains,$key);
 6913:             } elsif ($scope eq 'users') {
 6914:                 push(@users,$key);
 6915:             } elsif ($scope eq 'course') {
 6916:                 push(@courses,$key);
 6917:             } elsif ($scope eq 'group') {
 6918:                 push(@groups,$key);
 6919:             } elsif ($scope eq 'ip') {
 6920:                 push(@ips,$key);
 6921:             }
 6922:         }
 6923:         if ($public) {
 6924:             return 'ok';
 6925:         } elsif (@ips > 0) {
 6926:             my $allowed;
 6927:             foreach my $ipkey (@ips) {
 6928:                 if (ref($access_hash->{$ipkey}{'ip'}) eq 'ARRAY') {
 6929:                     if (&Apache::loncommon::check_ip_acc(join(',',@{$access_hash->{$ipkey}{'ip'}}),$clientip)) {
 6930:                         $allowed = 1;
 6931:                         last; 
 6932:                     }
 6933:                 }
 6934:             }
 6935:             if ($allowed) {
 6936:                 return 'ok';
 6937:             }
 6938:         }
 6939:         if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
 6940:             if ($guest) {
 6941:                 return $guest;
 6942:             }
 6943:         } else {
 6944:             if (@domains > 0) {
 6945:                 foreach my $domkey (@domains) {
 6946:                     if (ref($access_hash->{$domkey}{'dom'}) eq 'ARRAY') {
 6947:                         if (grep(/^\Q$env{'user.domain'}\E$/,@{$access_hash->{$domkey}{'dom'}})) {
 6948:                             return 'ok';
 6949:                         }
 6950:                     }
 6951:                 }
 6952:             }
 6953:             if (@users > 0) {
 6954:                 foreach my $userkey (@users) {
 6955:                     if (ref($access_hash->{$userkey}{'users'}) eq 'ARRAY') {
 6956:                         foreach my $item (@{$access_hash->{$userkey}{'users'}}) {
 6957:                             if (ref($item) eq 'HASH') {
 6958:                                 if (($item->{'uname'} eq $env{'user.name'}) &&
 6959:                                     ($item->{'udom'} eq $env{'user.domain'})) {
 6960:                                     return 'ok';
 6961:                                 }
 6962:                             }
 6963:                         }
 6964:                     } 
 6965:                 }
 6966:             }
 6967:             my %roleshash;
 6968:             my @courses_and_groups = @courses;
 6969:             push(@courses_and_groups,@groups); 
 6970:             if (@courses_and_groups > 0) {
 6971:                 my (%allgroups,%allroles); 
 6972:                 my ($start,$end,$role,$sec,$group);
 6973:                 foreach my $envkey (%env) {
 6974:                     if ($envkey =~ m-^user\.role\.(gr|cc|co|in|ta|ep|ad|st)\./($match_domain)/($match_courseid)/?([^/]*)$-) {
 6975:                         my $cid = $2.'_'.$3; 
 6976:                         if ($1 eq 'gr') {
 6977:                             $group = $4;
 6978:                             $allgroups{$cid}{$group} = $env{$envkey};
 6979:                         } else {
 6980:                             if ($4 eq '') {
 6981:                                 $sec = 'none';
 6982:                             } else {
 6983:                                 $sec = $4;
 6984:                             }
 6985:                             $allroles{$cid}{$1}{$sec} = $env{$envkey};
 6986:                         }
 6987:                     } elsif ($envkey =~ m-^user\.role\./cr/($match_domain/$match_username/\w*)./($match_domain)/($match_courseid)/?([^/]*)$-) {
 6988:                         my $cid = $2.'_'.$3;
 6989:                         if ($4 eq '') {
 6990:                             $sec = 'none';
 6991:                         } else {
 6992:                             $sec = $4;
 6993:                         }
 6994:                         $allroles{$cid}{$1}{$sec} = $env{$envkey};
 6995:                     }
 6996:                 }
 6997:                 if (keys(%allroles) == 0) {
 6998:                     return;
 6999:                 }
 7000:                 foreach my $key (@courses_and_groups) {
 7001:                     my %content = %{$$access_hash{$key}};
 7002:                     my $cnum = $content{'number'};
 7003:                     my $cdom = $content{'domain'};
 7004:                     my $cid = $cdom.'_'.$cnum;
 7005:                     if (!exists($allroles{$cid})) {
 7006:                         next;
 7007:                     }    
 7008:                     foreach my $role_id (keys(%{$content{'roles'}})) {
 7009:                         my @sections = @{$content{'roles'}{$role_id}{'section'}};
 7010:                         my @groups = @{$content{'roles'}{$role_id}{'group'}};
 7011:                         my @status = @{$content{'roles'}{$role_id}{'access'}};
 7012:                         my @roles = @{$content{'roles'}{$role_id}{'role'}};
 7013:                         foreach my $role (keys(%{$allroles{$cid}})) {
 7014:                             if ((grep/^all$/,@roles) || (grep/^\Q$role\E$/,@roles)) {
 7015:                                 foreach my $sec (keys(%{$allroles{$cid}{$role}})) {
 7016:                                     if (&course_group_datechecker($allroles{$cid}{$role}{$sec},$now,\@status) eq 'ok') {
 7017:                                         if (grep/^all$/,@sections) {
 7018:                                             return 'ok';
 7019:                                         } else {
 7020:                                             if (grep/^$sec$/,@sections) {
 7021:                                                 return 'ok';
 7022:                                             }
 7023:                                         }
 7024:                                     }
 7025:                                 }
 7026:                                 if (keys(%{$allgroups{$cid}}) == 0) {
 7027:                                     if (grep/^none$/,@groups) {
 7028:                                         return 'ok';
 7029:                                     }
 7030:                                 } else {
 7031:                                     if (grep/^all$/,@groups) {
 7032:                                         return 'ok';
 7033:                                     } 
 7034:                                     foreach my $group (keys(%{$allgroups{$cid}})) {
 7035:                                         if (grep/^$group$/,@groups) {
 7036:                                             return 'ok';
 7037:                                         }
 7038:                                     }
 7039:                                 } 
 7040:                             }
 7041:                         }
 7042:                     }
 7043:                 }
 7044:             }
 7045:             if ($guest) {
 7046:                 return $guest;
 7047:             }
 7048:         }
 7049:     }
 7050:     return;
 7051: }
 7052: 
 7053: sub course_group_datechecker {
 7054:     my ($dates,$now,$status) = @_;
 7055:     my ($start,$end) = split(/\./,$dates);
 7056:     if (!$start && !$end) {
 7057:         return 'ok';
 7058:     }
 7059:     if (grep/^active$/,@{$status}) {
 7060:         if (((!$start) || ($start && $start <= $now)) && ((!$end) || ($end && $end >= $now))) {
 7061:             return 'ok';
 7062:         }
 7063:     }
 7064:     if (grep/^previous$/,@{$status}) {
 7065:         if ($end > $now ) {
 7066:             return 'ok';
 7067:         }
 7068:     }
 7069:     if (grep/^future$/,@{$status}) {
 7070:         if ($start > $now) {
 7071:             return 'ok';
 7072:         }
 7073:     }
 7074:     return; 
 7075: }
 7076: 
 7077: sub parse_portfolio_url {
 7078:     my ($url) = @_;
 7079: 
 7080:     my ($type,$udom,$unum,$group,$file_name);
 7081:     
 7082:     if ($url =~  m-^/*(?:uploaded|editupload)/($match_domain)/($match_username)/portfolio(/.+)$-) {
 7083: 	$type = 1;
 7084:         $udom = $1;
 7085:         $unum = $2;
 7086:         $file_name = $3;
 7087:     } elsif ($url =~ m-^/*(?:uploaded|editupload)/($match_domain)/($match_courseid)/groups/([^/]+)/portfolio/(.+)$-) {
 7088: 	$type = 2;
 7089:         $udom = $1;
 7090:         $unum = $2;
 7091:         $group = $3;
 7092:         $file_name = $3.'/'.$4;
 7093:     }
 7094:     if (wantarray) {
 7095: 	return ($type,$udom,$unum,$file_name,$group);
 7096:     }
 7097:     return $type;
 7098: }
 7099: 
 7100: sub is_portfolio_url {
 7101:     my ($url) = @_;
 7102:     return scalar(&parse_portfolio_url($url));
 7103: }
 7104: 
 7105: sub is_portfolio_file {
 7106:     my ($file) = @_;
 7107:     if (($file =~ /^portfolio/) || ($file =~ /^groups\/\w+\/portfolio/)) {
 7108:         return 1;
 7109:     }
 7110:     return;
 7111: }
 7112: 
 7113: sub usertools_access {
 7114:     my ($uname,$udom,$tool,$action,$context,$userenvref,$domdefref,$is_advref)=@_;
 7115:     my ($access,%tools);
 7116:     if ($context eq '') {
 7117:         $context = 'tools';
 7118:     }
 7119:     if ($context eq 'requestcourses') {
 7120:         %tools = (
 7121:                       official   => 1,
 7122:                       unofficial => 1,
 7123:                       community  => 1,
 7124:                       textbook   => 1,
 7125:                       placement  => 1,
 7126:                  );
 7127:     } elsif ($context eq 'requestauthor') {
 7128:         %tools = (
 7129:                       requestauthor => 1,
 7130:                  );
 7131:     } else {
 7132:         %tools = (
 7133:                       aboutme   => 1,
 7134:                       blog      => 1,
 7135:                       webdav    => 1,
 7136:                       portfolio => 1,
 7137:                  );
 7138:     }
 7139:     return if (!defined($tools{$tool}));
 7140: 
 7141:     if (($udom eq '') || ($uname eq '')) {
 7142:         $udom = $env{'user.domain'};
 7143:         $uname = $env{'user.name'};
 7144:     }
 7145: 
 7146:     if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
 7147:         if ($action ne 'reload') {
 7148:             if ($context eq 'requestcourses') {
 7149:                 return $env{'environment.canrequest.'.$tool};
 7150:             } elsif ($context eq 'requestauthor') {
 7151:                 return $env{'environment.canrequest.author'};
 7152:             } else {
 7153:                 return $env{'environment.availabletools.'.$tool};
 7154:             }
 7155:         }
 7156:     }
 7157: 
 7158:     my ($toolstatus,$inststatus,$envkey);
 7159:     if ($context eq 'requestauthor') {
 7160:         $envkey = $context; 
 7161:     } else {
 7162:         $envkey = $context.'.'.$tool;
 7163:     }
 7164: 
 7165:     if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'}) &&
 7166:          ($action ne 'reload')) {
 7167:         $toolstatus = $env{'environment.'.$envkey};
 7168:         $inststatus = $env{'environment.inststatus'};
 7169:     } else {
 7170:         if (ref($userenvref) eq 'HASH') {
 7171:             $toolstatus = $userenvref->{$envkey};
 7172:             $inststatus = $userenvref->{'inststatus'};
 7173:         } else {
 7174:             my %userenv = &userenvironment($udom,$uname,$envkey,'inststatus');
 7175:             $toolstatus = $userenv{$envkey};
 7176:             $inststatus = $userenv{'inststatus'};
 7177:         }
 7178:     }
 7179: 
 7180:     if ($toolstatus ne '') {
 7181:         if ($toolstatus) {
 7182:             $access = 1;
 7183:         } else {
 7184:             $access = 0;
 7185:         }
 7186:         return $access;
 7187:     }
 7188: 
 7189:     my ($is_adv,%domdef);
 7190:     if (ref($is_advref) eq 'HASH') {
 7191:         $is_adv = $is_advref->{'is_adv'};
 7192:     } else {
 7193:         $is_adv = &is_advanced_user($udom,$uname);
 7194:     }
 7195:     if (ref($domdefref) eq 'HASH') {
 7196:         %domdef = %{$domdefref};
 7197:     } else {
 7198:         %domdef = &get_domain_defaults($udom);
 7199:     }
 7200:     if (ref($domdef{$tool}) eq 'HASH') {
 7201:         if ($is_adv) {
 7202:             if ($domdef{$tool}{'_LC_adv'} ne '') {
 7203:                 if ($domdef{$tool}{'_LC_adv'}) { 
 7204:                     $access = 1;
 7205:                 } else {
 7206:                     $access = 0;
 7207:                 }
 7208:                 return $access;
 7209:             }
 7210:         }
 7211:         if ($inststatus ne '') {
 7212:             my ($hasaccess,$hasnoaccess);
 7213:             foreach my $affiliation (split(/:/,$inststatus)) {
 7214:                 if ($domdef{$tool}{$affiliation} ne '') { 
 7215:                     if ($domdef{$tool}{$affiliation}) {
 7216:                         $hasaccess = 1;
 7217:                     } else {
 7218:                         $hasnoaccess = 1;
 7219:                     }
 7220:                 }
 7221:             }
 7222:             if ($hasaccess || $hasnoaccess) {
 7223:                 if ($hasaccess) {
 7224:                     $access = 1;
 7225:                 } elsif ($hasnoaccess) {
 7226:                     $access = 0; 
 7227:                 }
 7228:                 return $access;
 7229:             }
 7230:         } else {
 7231:             if ($domdef{$tool}{'default'} ne '') {
 7232:                 if ($domdef{$tool}{'default'}) {
 7233:                     $access = 1;
 7234:                 } elsif ($domdef{$tool}{'default'} == 0) {
 7235:                     $access = 0;
 7236:                 }
 7237:                 return $access;
 7238:             }
 7239:         }
 7240:     } else {
 7241:         if (($context eq 'tools') && ($tool ne 'webdav')) {
 7242:             $access = 1;
 7243:         } else {
 7244:             $access = 0;
 7245:         }
 7246:         return $access;
 7247:     }
 7248: }
 7249: 
 7250: sub is_course_owner {
 7251:     my ($cdom,$cnum,$udom,$uname) = @_;
 7252:     if (($udom eq '') || ($uname eq '')) {
 7253:         $udom = $env{'user.domain'};
 7254:         $uname = $env{'user.name'};
 7255:     }
 7256:     unless (($udom eq '') || ($uname eq '')) {
 7257:         if (exists($env{'course.'.$cdom.'_'.$cnum.'.internal.courseowner'})) {
 7258:             if ($env{'course.'.$cdom.'_'.$cnum.'.internal.courseowner'} eq $uname.':'.$udom) {
 7259:                 return 1;
 7260:             } else {
 7261:                 my %courseinfo = &Apache::lonnet::coursedescription($cdom.'/'.$cnum);
 7262:                 if ($courseinfo{'internal.courseowner'} eq $uname.':'.$udom) {
 7263:                     return 1;
 7264:                 }
 7265:             }
 7266:         }
 7267:     }
 7268:     return;
 7269: }
 7270: 
 7271: sub is_advanced_user {
 7272:     my ($udom,$uname) = @_;
 7273:     if ($udom ne '' && $uname ne '') {
 7274:         if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
 7275:             if (wantarray) {
 7276:                 return ($env{'user.adv'},$env{'user.author'});
 7277:             } else {
 7278:                 return $env{'user.adv'};
 7279:             }
 7280:         }
 7281:     }
 7282:     my %roleshash = &get_my_roles($uname,$udom,'userroles',undef,undef,undef,1);
 7283:     my %allroles;
 7284:     my ($is_adv,$is_author);
 7285:     foreach my $role (keys(%roleshash)) {
 7286:         my ($trest,$tdomain,$trole,$sec) = split(/:/,$role);
 7287:         my $area = '/'.$tdomain.'/'.$trest;
 7288:         if ($sec ne '') {
 7289:             $area .= '/'.$sec;
 7290:         }
 7291:         if (($area ne '') && ($trole ne '')) {
 7292:             my $spec=$trole.'.'.$area;
 7293:             if ($trole =~ /^cr\//) {
 7294:                 &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
 7295:             } elsif ($trole ne 'gr') {
 7296:                 &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
 7297:             }
 7298:             if ($trole eq 'au') {
 7299:                 $is_author = 1;
 7300:             }
 7301:         }
 7302:     }
 7303:     foreach my $role (keys(%allroles)) {
 7304:         last if ($is_adv);
 7305:         foreach my $item (split(/:/,$allroles{$role})) {
 7306:             if ($item ne '') {
 7307:                 my ($privilege,$restrictions)=split(/&/,$item);
 7308:                 if ($privilege eq 'adv') {
 7309:                     $is_adv = 1;
 7310:                     last;
 7311:                 }
 7312:             }
 7313:         }
 7314:     }
 7315:     if (wantarray) {
 7316:         return ($is_adv,$is_author);
 7317:     }
 7318:     return $is_adv;
 7319: }
 7320: 
 7321: sub check_can_request {
 7322:     my ($dom,$can_request,$request_domains) = @_;
 7323:     my $canreq = 0;
 7324:     my ($types,$typename) = &Apache::loncommon::course_types();
 7325:     my @options = ('approval','validate','autolimit');
 7326:     my $optregex = join('|',@options);
 7327:     if ((ref($can_request) eq 'HASH') && (ref($types) eq 'ARRAY')) {
 7328:         foreach my $type (@{$types}) {
 7329:             if (&usertools_access($env{'user.name'},
 7330:                                   $env{'user.domain'},
 7331:                                   $type,undef,'requestcourses')) {
 7332:                 $canreq ++;
 7333:                 if (ref($request_domains) eq 'HASH') {
 7334:                     push(@{$request_domains->{$type}},$env{'user.domain'});
 7335:                 }
 7336:                 if ($dom eq $env{'user.domain'}) {
 7337:                     $can_request->{$type} = 1;
 7338:                 }
 7339:             }
 7340:             if ($env{'environment.reqcrsotherdom.'.$type} ne '') {
 7341:                 my @curr = split(',',$env{'environment.reqcrsotherdom.'.$type});
 7342:                 if (@curr > 0) {
 7343:                     foreach my $item (@curr) {
 7344:                         if (ref($request_domains) eq 'HASH') {
 7345:                             my ($otherdom) = ($item =~ /^($match_domain):($optregex)(=?\d*)$/);
 7346:                             if ($otherdom ne '') {
 7347:                                 if (ref($request_domains->{$type}) eq 'ARRAY') {
 7348:                                     unless (grep(/^\Q$otherdom\E$/,@{$request_domains->{$type}})) {
 7349:                                         push(@{$request_domains->{$type}},$otherdom);
 7350:                                     }
 7351:                                 } else {
 7352:                                     push(@{$request_domains->{$type}},$otherdom);
 7353:                                 }
 7354:                             }
 7355:                         }
 7356:                     }
 7357:                     unless($dom eq $env{'user.domain'}) {
 7358:                         $canreq ++;
 7359:                         if (grep(/^\Q$dom\E:($optregex)(=?\d*)$/,@curr)) {
 7360:                             $can_request->{$type} = 1;
 7361:                         }
 7362:                     }
 7363:                 }
 7364:             }
 7365:         }
 7366:     }
 7367:     return $canreq;
 7368: }
 7369: 
 7370: # ---------------------------------------------- Custom access rule evaluation
 7371: 
 7372: sub customaccess {
 7373:     my ($priv,$uri)=@_;
 7374:     my ($urole,$urealm)=split(/\./,$env{'request.role'},2);
 7375:     my (undef,$udom,$ucrs,$usec)=split(/\//,$urealm);
 7376:     $udom = &LONCAPA::clean_domain($udom);
 7377:     $ucrs = &LONCAPA::clean_username($ucrs);
 7378:     my $access=0;
 7379:     foreach my $right (split(/\s*\,\s*/,&metadata($uri,'rule_rights'))) {
 7380: 	my ($effect,$realm,$role,$type)=split(/\:/,$right);
 7381: 	if ($type eq 'user') {
 7382: 	    foreach my $scope (split(/\s*\,\s*/,$realm)) {
 7383: 		my ($tdom,$tuname)=split(m{/},$scope);
 7384: 		if ($tdom) {
 7385: 		    if ($tdom ne $env{'user.domain'}) { next; }
 7386: 		}
 7387: 		if ($tuname) {
 7388: 		    if ($tuname ne $env{'user.name'}) { next; }
 7389: 		}
 7390: 		$access=($effect eq 'allow');
 7391: 		last;
 7392: 	    }
 7393: 	} else {
 7394: 	    if ($role) {
 7395: 		if ($role ne $urole) { next; }
 7396: 	    }
 7397: 	    foreach my $scope (split(/\s*\,\s*/,$realm)) {
 7398: 		my ($tdom,$tcrs,$tsec)=split(/\_/,$scope);
 7399: 		if ($tdom) {
 7400: 		    if ($tdom ne $udom) { next; }
 7401: 		}
 7402: 		if ($tcrs) {
 7403: 		    if ($tcrs ne $ucrs) { next; }
 7404: 		}
 7405: 		if ($tsec) {
 7406: 		    if ($tsec ne $usec) { next; }
 7407: 		}
 7408: 		$access=($effect eq 'allow');
 7409: 		last;
 7410: 	    }
 7411: 	    if ($realm eq '' && $role eq '') {
 7412: 		$access=($effect eq 'allow');
 7413: 	    }
 7414: 	}
 7415:     }
 7416:     return $access;
 7417: }
 7418: 
 7419: # ------------------------------------------------- Check for a user privilege
 7420: 
 7421: sub allowed {
 7422:     my ($priv,$uri,$symb,$role,$clientip,$noblockcheck)=@_;
 7423:     my $ver_orguri=$uri;
 7424:     $uri=&deversion($uri);
 7425:     my $orguri=$uri;
 7426:     $uri=&declutter($uri);
 7427: 
 7428:     if ($priv eq 'evb') {
 7429: # Evade communication block restrictions for specified role in a course
 7430:         if ($env{'user.priv.'.$role} =~/evb\&([^\:]*)/) {
 7431:             return $1;
 7432:         } else {
 7433:             return;
 7434:         }
 7435:     }
 7436: 
 7437:     if (defined($env{'allowed.'.$priv})) { return $env{'allowed.'.$priv}; }
 7438: # Free bre access to adm and meta resources
 7439:     if (((($uri=~/^adm\//) && ($uri !~ m{/(?:smppg|bulletinboard|ext\.tool)$})) 
 7440: 	 || (($uri=~/\.meta$/) && ($uri!~m|^uploaded/|) )) 
 7441: 	&& ($priv eq 'bre')) {
 7442: 	return 'F';
 7443:     }
 7444: 
 7445: # Free bre access to user's own portfolio contents
 7446:     my ($space,$domain,$name,@dir)=split('/',$uri);
 7447:     if (($space=~/^(uploaded|editupload)$/) && ($env{'user.name'} eq $name) && 
 7448: 	($env{'user.domain'} eq $domain) && ('portfolio' eq $dir[0])) {
 7449:         my %setters;
 7450:         my ($startblock,$endblock) = 
 7451:             &Apache::loncommon::blockcheck(\%setters,'port');
 7452:         if ($startblock && $endblock) {
 7453:             return 'B';
 7454:         } else {
 7455:             return 'F';
 7456:         }
 7457:     }
 7458: 
 7459: # bre access to group portfolio for rgf priv in group, or mdg or vcg in course.
 7460:     if (($space=~/^(uploaded|editupload)$/) && ($dir[0] eq 'groups') 
 7461:          && ($dir[2] eq 'portfolio') && ($priv eq 'bre')) {
 7462:         if (exists($env{'request.course.id'})) {
 7463:             my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 7464:             my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 7465:             if (($domain eq $cdom) && ($name eq $cnum)) {
 7466:                 my $courseprivid=$env{'request.course.id'};
 7467:                 $courseprivid=~s/\_/\//;
 7468:                 if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid
 7469:                     .'/'.$dir[1]} =~/rgf\&([^\:]*)/) {
 7470:                     return $1; 
 7471:                 } else {
 7472:                     if ($env{'request.course.sec'}) {
 7473:                         $courseprivid.='/'.$env{'request.course.sec'};
 7474:                     }
 7475:                     if ($env{'user.priv.'.$env{'request.role'}.'./'.
 7476:                         $courseprivid} =~/(mdg|vcg)\&([^\:]*)/) {
 7477:                         return $2;
 7478:                     }
 7479:                 }
 7480:             }
 7481:         }
 7482:     }
 7483: 
 7484: # Free bre to public access
 7485: 
 7486:     if ($priv eq 'bre') {
 7487:         my $copyright=&metadata($uri,'copyright');
 7488: 	if (($copyright eq 'public') && (!$env{'request.course.id'})) { 
 7489:            return 'F'; 
 7490:         }
 7491:         if ($copyright eq 'priv') {
 7492:             $uri=~/([^\/]+)\/([^\/]+)\//;
 7493: 	    unless (($env{'user.name'} eq $2) && ($env{'user.domain'} eq $1)) {
 7494: 		return '';
 7495:             }
 7496:         }
 7497:         if ($copyright eq 'domain') {
 7498:             $uri=~/([^\/]+)\/([^\/]+)\//;
 7499: 	    unless (($env{'user.domain'} eq $1) ||
 7500:                  ($env{'course.'.$env{'request.course.id'}.'.domain'} eq $1)) {
 7501: 		return '';
 7502:             }
 7503:         }
 7504:         if ($env{'request.role'}=~ /li\.\//) {
 7505:             # Library role, so allow browsing of resources in this domain.
 7506:             return 'F';
 7507:         }
 7508:         if ($copyright eq 'custom') {
 7509: 	    unless (&customaccess($priv,$uri)) { return ''; }
 7510:         }
 7511:     }
 7512:     # Domain coordinator is trying to create a course
 7513:     if (($priv eq 'ccc') && ($env{'request.role'} =~ /^dc\./)) {
 7514:         # uri is the requested domain in this case.
 7515:         # comparison to 'request.role.domain' shows if the user has selected
 7516:         # a role of dc for the domain in question.
 7517:         return 'F' if ($uri eq $env{'request.role.domain'});
 7518:     }
 7519: 
 7520:     my $thisallowed='';
 7521:     my $statecond=0;
 7522:     my $courseprivid='';
 7523: 
 7524:     my $ownaccess;
 7525:     # Community Coordinator or Assistant Co-author browsing resource space.
 7526:     if (($priv eq 'bro') && ($env{'user.author'})) {
 7527:         if ($uri eq '') {
 7528:             $ownaccess = 1;
 7529:         } else {
 7530:             if (($env{'user.domain'} ne '') && ($env{'user.name'} ne '')) {
 7531:                 my $udom = $env{'user.domain'};
 7532:                 my $uname = $env{'user.name'};
 7533:                 if ($uri =~ m{^\Q$udom\E/?$}) {
 7534:                     $ownaccess = 1;
 7535:                 } elsif ($uri =~ m{^\Q$udom\E/\Q$uname\E/?}) {
 7536:                     unless ($uri =~ m{\.\./}) {
 7537:                         $ownaccess = 1;
 7538:                     }
 7539:                 } elsif (($udom ne 'public') && ($uname ne 'public')) {
 7540:                     my $now = time;
 7541:                     if ($uri =~ m{^([^/]+)/?$}) {
 7542:                         my $adom = $1;
 7543:                         foreach my $key (keys(%env)) {
 7544:                             if ($key =~ m{^user\.role\.(ca|aa)/\Q$adom\E}) {
 7545:                                 my ($start,$end) = split('.',$env{$key});
 7546:                                 if (($now >= $start) && (!$end || $end < $now)) {
 7547:                                     $ownaccess = 1;
 7548:                                     last;
 7549:                                 }
 7550:                             }
 7551:                         }
 7552:                     } elsif ($uri =~ m{^([^/]+)/([^/]+)/?}) {
 7553:                         my $adom = $1;
 7554:                         my $aname = $2;
 7555:                         foreach my $role ('ca','aa') { 
 7556:                             if ($env{"user.role.$role./$adom/$aname"}) {
 7557:                                 my ($start,$end) =
 7558:                                     split('.',$env{"user.role.$role./$adom/$aname"});
 7559:                                 if (($now >= $start) && (!$end || $end < $now)) {
 7560:                                     $ownaccess = 1;
 7561:                                     last;
 7562:                                 }
 7563:                             }
 7564:                         }
 7565:                     }
 7566:                 }
 7567:             }
 7568:         }
 7569:     }
 7570: 
 7571: # Course
 7572: 
 7573:     if ($env{'user.priv.'.$env{'request.role'}.'./'}=~/\Q$priv\E\&([^\:]*)/) {
 7574:         unless (($priv eq 'bro') && (!$ownaccess)) {
 7575:             $thisallowed.=$1;
 7576:         }
 7577:     }
 7578: 
 7579: # Domain
 7580: 
 7581:     if ($env{'user.priv.'.$env{'request.role'}.'./'.(split(/\//,$uri))[0].'/'}
 7582:        =~/\Q$priv\E\&([^\:]*)/) {
 7583:         unless (($priv eq 'bro') && (!$ownaccess)) {
 7584:             $thisallowed.=$1;
 7585:         }
 7586:     }
 7587: 
 7588: # User who is not author or co-author might still be able to edit
 7589: # resource of an author in the domain (e.g., if Domain Coordinator).
 7590:     if (($priv eq 'eco') && ($thisallowed eq '') && ($env{'request.course.id'}) &&
 7591:         (&allowed('mdc',$env{'request.course.id'}))) {
 7592:         if ($env{"user.priv.cm./$uri/"}=~/\Q$priv\E\&([^\:]*)/) {
 7593:             $thisallowed.=$1;
 7594:         }
 7595:     }
 7596: 
 7597: # Course: uri itself is a course
 7598:     my $courseuri=$uri;
 7599:     $courseuri=~s/\_(\d)/\/$1/;
 7600:     $courseuri=~s/^([^\/])/\/$1/;
 7601: 
 7602:     if ($env{'user.priv.'.$env{'request.role'}.'.'.$courseuri}
 7603:        =~/\Q$priv\E\&([^\:]*)/) {
 7604:         unless (($priv eq 'bro') && (!$ownaccess)) {
 7605:             $thisallowed.=$1;
 7606:         }
 7607:     }
 7608: 
 7609: # URI is an uploaded document for this course, default permissions don't matter
 7610: # not allowing 'edit' access (editupload) to uploaded course docs
 7611:     if (($priv eq 'bre') && ($uri=~m|^uploaded/|)) {
 7612: 	$thisallowed='';
 7613:         my ($match)=&is_on_map($uri);
 7614:         if ($match) {
 7615:             if ($env{'user.priv.'.$env{'request.role'}.'./'}
 7616:                   =~/\Q$priv\E\&([^\:]*)/) {
 7617:                 my $value = $1;
 7618:                 if ($noblockcheck) {
 7619:                     $thisallowed.=$value;
 7620:                 } else {
 7621:                     my @blockers = &has_comm_blocking($priv,$symb,$uri);
 7622:                     if (@blockers > 0) {
 7623:                         $thisallowed = 'B';
 7624:                     } else {
 7625:                         $thisallowed.=$value;
 7626:                     }
 7627:                 }
 7628:             }
 7629:         } else {
 7630:             my $refuri = $env{'httpref.'.$orguri} || $env{'httpref.'.$ver_orguri};
 7631:             if ($refuri) {
 7632:                 if ($refuri =~ m|^/adm/|) {
 7633:                     $thisallowed='F';
 7634:                 } else {
 7635:                     $refuri=&declutter($refuri);
 7636:                     my ($match) = &is_on_map($refuri);
 7637:                     if ($match) {
 7638:                         if ($noblockcheck) {
 7639:                             $thisallowed='F';
 7640:                         } else {
 7641:                             my @blockers = &has_comm_blocking($priv,$symb,$refuri);
 7642:                             if (@blockers > 0) {
 7643:                                 $thisallowed = 'B';
 7644:                             } else {
 7645:                                 $thisallowed='F';
 7646:                             }
 7647:                         }
 7648:                     }
 7649:                 }
 7650:             }
 7651:         }
 7652:     }
 7653: 
 7654:     if ($priv eq 'bre'
 7655: 	&& $thisallowed ne 'F' 
 7656: 	&& $thisallowed ne '2'
 7657: 	&& &is_portfolio_url($uri)) {
 7658: 	$thisallowed = &portfolio_access($uri,$clientip);
 7659:     }
 7660: 
 7661: # Full access at system, domain or course-wide level? Exit.
 7662:     if ($thisallowed=~/F/) {
 7663: 	return 'F';
 7664:     }
 7665: 
 7666: # If this is generating or modifying users, exit with special codes
 7667: 
 7668:     if (':csu:cdc:ccc:cin:cta:cep:ccr:cst:cad:cli:cau:cdg:cca:caa:'=~/\:\Q$priv\E\:/) {
 7669: 	if (($priv eq 'cca') || ($priv eq 'caa')) {
 7670: 	    my ($audom,$auname)=split('/',$uri);
 7671: # no author name given, so this just checks on the general right to make a co-author in this domain
 7672: 	    unless ($auname) { return $thisallowed; }
 7673: # an author name is given, so we are about to actually make a co-author for a certain account
 7674: 	    if (($auname ne $env{'user.name'} && $env{'request.role'} !~ /^dc\./) ||
 7675: 		(($audom ne $env{'user.domain'} && $env{'request.role'} !~ /^dc\./) &&
 7676: 		 ($audom ne $env{'request.role.domain'}))) { return ''; }
 7677: 	}
 7678: 	return $thisallowed;
 7679:     }
 7680: #
 7681: # Gathered so far: system, domain and course wide privileges
 7682: #
 7683: # Course: See if uri or referer is an individual resource that is part of 
 7684: # the course
 7685: 
 7686:     if ($env{'request.course.id'}) {
 7687: 
 7688:        $courseprivid=$env{'request.course.id'};
 7689:        if ($env{'request.course.sec'}) {
 7690:           $courseprivid.='/'.$env{'request.course.sec'};
 7691:        }
 7692:        $courseprivid=~s/\_/\//;
 7693:        my $checkreferer=1;
 7694:        my ($match,$cond)=&is_on_map($uri);
 7695:        if ($match) {
 7696:            $statecond=$cond;
 7697:            if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
 7698:                =~/\Q$priv\E\&([^\:]*)/) {
 7699:                my $value = $1;
 7700:                if ($priv eq 'bre') {
 7701:                    if ($noblockcheck) {
 7702:                        $thisallowed.=$value;
 7703:                    } else {
 7704:                        my @blockers = &has_comm_blocking($priv,$symb,$uri);
 7705:                        if (@blockers > 0) {
 7706:                            $thisallowed = 'B';
 7707:                        } else {
 7708:                            $thisallowed.=$value;
 7709:                        }
 7710:                    }
 7711:                } else {
 7712:                    $thisallowed.=$value;
 7713:                }
 7714:                $checkreferer=0;
 7715:            }
 7716:        }
 7717:        
 7718:        if ($checkreferer) {
 7719: 	  my $refuri=$env{'httpref.'.$orguri};
 7720:             unless ($refuri) {
 7721:                 foreach my $key (keys(%env)) {
 7722: 		    if ($key=~/^httpref\..*\*/) {
 7723: 			my $pattern=$key;
 7724:                         $pattern=~s/^httpref\.\/res\///;
 7725:                         $pattern=~s/\*/\[\^\/\]\+/g;
 7726:                         $pattern=~s/\//\\\//g;
 7727:                         if ($orguri=~/$pattern/) {
 7728: 			    $refuri=$env{$key};
 7729:                         }
 7730:                     }
 7731:                 }
 7732:             }
 7733: 
 7734:          if ($refuri) { 
 7735: 	  $refuri=&declutter($refuri);
 7736:           my ($match,$cond)=&is_on_map($refuri);
 7737:             if ($match) {
 7738:               my $refstatecond=$cond;
 7739:               if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
 7740:                   =~/\Q$priv\E\&([^\:]*)/) {
 7741:                   my $value = $1;
 7742:                   if ($priv eq 'bre') {
 7743:                       if ($noblockcheck) {
 7744:                           $thisallowed.=$value;
 7745:                       } else {
 7746:                           my @blockers = &has_comm_blocking($priv,$symb,$refuri);
 7747:                           if (@blockers > 0) {
 7748:                               $thisallowed = 'B';
 7749:                           } else {
 7750:                               $thisallowed.=$value;
 7751:                           }
 7752:                       }
 7753:                   } else {
 7754:                       $thisallowed.=$value;
 7755:                   }
 7756:                   $uri=$refuri;
 7757:                   $statecond=$refstatecond;
 7758:               }
 7759:           }
 7760:         }
 7761:        }
 7762:    }
 7763: 
 7764: #
 7765: # Gathered now: all privileges that could apply, and condition number
 7766: # 
 7767: #
 7768: # Full or no access?
 7769: #
 7770: 
 7771:     if ($thisallowed=~/F/) {
 7772: 	return 'F';
 7773:     }
 7774: 
 7775:     unless ($thisallowed) {
 7776:         return '';
 7777:     }
 7778: 
 7779: # Restrictions exist, deal with them
 7780: #
 7781: #   C:according to course preferences
 7782: #   R:according to resource settings
 7783: #   L:unless locked
 7784: #   X:according to user session state
 7785: #
 7786: 
 7787: # Possibly locked functionality, check all courses
 7788: # Locks might take effect only after 10 minutes cache expiration for other
 7789: # courses, and 2 minutes for current course
 7790: 
 7791:     my $envkey;
 7792:     if ($thisallowed=~/L/) {
 7793:         foreach $envkey (keys(%env)) {
 7794:            if ($envkey=~/^user\.role\.(st|ta)\.([^\.]*)/) {
 7795:                my $courseid=$2;
 7796:                my $roleid=$1.'.'.$2;
 7797:                $courseid=~s/^\///;
 7798:                my $expiretime=600;
 7799:                if ($env{'request.role'} eq $roleid) {
 7800: 		  $expiretime=120;
 7801:                }
 7802: 	       my ($cdom,$cnum,$csec)=split(/\//,$courseid);
 7803:                my $prefix='course.'.$cdom.'_'.$cnum.'.';
 7804:                if ((time-$env{$prefix.'last_cache'})>$expiretime) {
 7805: 		   &coursedescription($courseid,{'freshen_cache' => 1});
 7806:                }
 7807:                if (($env{$prefix.'res.'.$uri.'.lock.sections'}=~/\,\Q$csec\E\,/)
 7808:                 || ($env{$prefix.'res.'.$uri.'.lock.sections'} eq 'all')) {
 7809: 		   if ($env{$prefix.'res.'.$uri.'.lock.expire'}>time) {
 7810:                        &log($env{'user.domain'},$env{'user.name'},
 7811:                             $env{'user.home'},
 7812:                             'Locked by res: '.$priv.' for '.$uri.' due to '.
 7813:                             $cdom.'/'.$cnum.'/'.$csec.' expire '.
 7814:                             $env{$prefix.'priv.'.$priv.'.lock.expire'});
 7815: 		       return '';
 7816:                    }
 7817:                }
 7818:                if (($env{$prefix.'priv.'.$priv.'.lock.sections'}=~/\,\Q$csec\E\,/)
 7819:                 || ($env{$prefix.'priv.'.$priv.'.lock.sections'} eq 'all')) {
 7820: 		   if ($env{'priv.'.$priv.'.lock.expire'}>time) {
 7821:                        &log($env{'user.domain'},$env{'user.name'},
 7822:                             $env{'user.home'},
 7823:                             'Locked by priv: '.$priv.' for '.$uri.' due to '.
 7824:                             $cdom.'/'.$cnum.'/'.$csec.' expire '.
 7825:                             $env{$prefix.'priv.'.$priv.'.lock.expire'});
 7826: 		       return '';
 7827:                    }
 7828:                }
 7829: 	   }
 7830:        }
 7831:     }
 7832:    
 7833: #
 7834: # Rest of the restrictions depend on selected course
 7835: #
 7836: 
 7837:     unless ($env{'request.course.id'}) {
 7838: 	if ($thisallowed eq 'A') {
 7839: 	    return 'A';
 7840:         } elsif ($thisallowed eq 'B') {
 7841:             return 'B';
 7842: 	} else {
 7843: 	    return '1';
 7844: 	}
 7845:     }
 7846: 
 7847: #
 7848: # Now user is definitely in a course
 7849: #
 7850: 
 7851: 
 7852: # Course preferences
 7853: 
 7854:    if ($thisallowed=~/C/) {
 7855:        my $rolecode=(split(/\./,$env{'request.role'}))[0];
 7856:        my $unamedom=$env{'user.name'}.':'.$env{'user.domain'};
 7857:        if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.roles.denied'}
 7858: 	   =~/\Q$rolecode\E/) {
 7859: 	   if (($priv ne 'pch') && ($priv ne 'plc') && ($priv ne 'pac')) {
 7860: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
 7861: 			'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode.' in '.
 7862: 			$env{'request.course.id'});
 7863: 	   }
 7864:            return '';
 7865:        }
 7866: 
 7867:        if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.users.denied'}
 7868: 	   =~/\Q$unamedom\E/) {
 7869: 	   if (($priv ne 'pch') && ($priv ne 'plc') && ($priv ne 'pac')) {
 7870: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.
 7871: 			'Denied by user: '.$priv.' for '.$uri.' as '.$unamedom.' in '.
 7872: 			$env{'request.course.id'});
 7873: 	   }
 7874:            return '';
 7875:        }
 7876:    }
 7877: 
 7878: # Resource preferences
 7879: 
 7880:    if ($thisallowed=~/R/) {
 7881:        my $rolecode=(split(/\./,$env{'request.role'}))[0];
 7882:        if (&metadata($uri,'roledeny')=~/\Q$rolecode\E/) {
 7883: 	   if (($priv ne 'pch') && ($priv ne 'plc')) { 
 7884: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
 7885: 			'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode);
 7886: 	   }
 7887: 	   return '';
 7888:        }
 7889:    }
 7890: 
 7891: # Restricted by state or randomout?
 7892: 
 7893:    if ($thisallowed=~/X/) {
 7894:       if ($env{'acc.randomout'}) {
 7895: 	 if (!$symb) { $symb=&symbread($uri,1); }
 7896:          if (($symb) && ($env{'acc.randomout'}=~/\&\Q$symb\E\&/)) { 
 7897:             return ''; 
 7898:          }
 7899:       }
 7900:       if (&condval($statecond)) {
 7901: 	 return '2';
 7902:       } else {
 7903:          return '';
 7904:       }
 7905:    }
 7906: 
 7907:     if ($thisallowed eq 'A') {
 7908: 	return 'A';
 7909:     } elsif ($thisallowed eq 'B') {
 7910:         return 'B';
 7911:     }
 7912:    return 'F';
 7913: }
 7914: 
 7915: # ------------------------------------------- Check construction space access
 7916: 
 7917: sub constructaccess {
 7918:     my ($url,$setpriv)=@_;
 7919: 
 7920: # We do not allow editing of previous versions of files
 7921:     if ($url=~/\.(\d+)\.(\w+)$/) { return ''; }
 7922: 
 7923: # Get username and domain from URL
 7924:     my ($ownername,$ownerdomain,$ownerhome);
 7925: 
 7926:     ($ownerdomain,$ownername) =
 7927:         ($url=~ m{^(?:\Q$perlvar{'lonDocRoot'}\E|)(?:/daxepage|/daxeopen)?/priv/($match_domain)/($match_username)(?:/|$)});
 7928: 
 7929: # The URL does not really point to any authorspace, forget it
 7930:     unless (($ownername) && ($ownerdomain)) { return ''; }
 7931: 
 7932: # Now we need to see if the user has access to the authorspace of
 7933: # $ownername at $ownerdomain
 7934: 
 7935:     if (($ownername eq $env{'user.name'}) && ($ownerdomain eq $env{'user.domain'})) {
 7936: # Real author for this?
 7937:        $ownerhome = $env{'user.home'};
 7938:        if (exists($env{'user.priv.au./'.$ownerdomain.'/./'})) {
 7939:           return ($ownername,$ownerdomain,$ownerhome);
 7940:        }
 7941:     } else {
 7942: # Co-author for this?
 7943:         if (exists($env{'user.priv.ca./'.$ownerdomain.'/'.$ownername.'./'}) ||
 7944:             exists($env{'user.priv.aa./'.$ownerdomain.'/'.$ownername.'./'}) ) {
 7945:             $ownerhome = &homeserver($ownername,$ownerdomain);
 7946:             return ($ownername,$ownerdomain,$ownerhome);
 7947:         }
 7948:         if ($env{'request.course.id'}) {
 7949:             if (($ownername eq $env{'course.'.$env{'request.course.id'}.'.num'}) &&
 7950:                 ($ownerdomain eq $env{'course.'.$env{'request.course.id'}.'.domain'})) {
 7951:                 if (&allowed('mdc',$env{'request.course.id'})) {
 7952:                     $ownerhome = $env{'course.'.$env{'request.course.id'}.'.home'};
 7953:                     return ($ownername,$ownerdomain,$ownerhome);
 7954:                 }
 7955:             }
 7956:         }
 7957:     }
 7958: 
 7959: # We don't have any access right now. If we are not possibly going to do anything about this,
 7960: # we might as well leave
 7961:    unless ($setpriv) { return ''; }
 7962: 
 7963: # Backdoor access?
 7964:     my $allowed=&allowed('eco',$ownerdomain);
 7965: # Nope
 7966:     unless ($allowed) { return ''; }
 7967: # Looks like we may have access, but could be locked by the owner of the construction space
 7968:     if ($allowed eq 'U') {
 7969:         my %blocked=&get('environment',['domcoord.author'],
 7970:                          $ownerdomain,$ownername);
 7971: # Is blocked by owner
 7972:         if ($blocked{'domcoord.author'} eq 'blocked') { return ''; }
 7973:     }
 7974:     if (($allowed eq 'F') || ($allowed eq 'U')) {
 7975: # Grant temporary access
 7976:         my $then=$env{'user.login.time'};
 7977:         my $update=$env{'user.update.time'};
 7978:         if (!$update) { $update = $then; }
 7979:         my $refresh=$env{'user.refresh.time'};
 7980:         if (!$refresh) { $refresh = $update; }
 7981:         my $now = time;
 7982:         &check_adhoc_privs($ownerdomain,$ownername,$update,$refresh,
 7983:                            $now,'ca','constructaccess');
 7984:         $ownerhome = &homeserver($ownername,$ownerdomain);
 7985:         return($ownername,$ownerdomain,$ownerhome);
 7986:     }
 7987: # No business here
 7988:     return '';
 7989: }
 7990: 
 7991: # ----------------------------------------------------------- Content Blocking
 7992: 
 7993: {
 7994: # Caches for faster Course Contents display where content blocking
 7995: # is in operation (i.e., interval param set) for timed quiz.
 7996: #
 7997: # User for whom data are being temporarily cached.
 7998: my $cacheduser='';
 7999: # Cached blockers for this user (a hash of blocking items). 
 8000: my %cachedblockers=();
 8001: # When the data were last cached.
 8002: my $cachedlast='';
 8003: 
 8004: sub load_all_blockers {
 8005:     my ($uname,$udom,$blocks)=@_;
 8006:     if (($uname ne '') && ($udom ne '')) { 
 8007:         if (($cacheduser eq $uname.':'.$udom) &&
 8008:             (abs($cachedlast-time)<5)) {
 8009:             return;
 8010:         }
 8011:     }
 8012:     $cachedlast=time;
 8013:     $cacheduser=$uname.':'.$udom;
 8014:     %cachedblockers = &get_commblock_resources($blocks);
 8015: }
 8016: 
 8017: sub get_comm_blocks {
 8018:     my ($cdom,$cnum) = @_;
 8019:     if ($cdom eq '' || $cnum eq '') {
 8020:         return unless ($env{'request.course.id'});
 8021:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 8022:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 8023:     }
 8024:     my %commblocks;
 8025:     my $hashid=$cdom.'_'.$cnum;
 8026:     my ($blocksref,$cached)=&is_cached_new('comm_block',$hashid);
 8027:     if ((defined($cached)) && (ref($blocksref) eq 'HASH')) {
 8028:         %commblocks = %{$blocksref};
 8029:     } else {
 8030:         %commblocks = &Apache::lonnet::dump('comm_block',$cdom,$cnum);
 8031:         my $cachetime = 600;
 8032:         &do_cache_new('comm_block',$hashid,\%commblocks,$cachetime);
 8033:     }
 8034:     return %commblocks;
 8035: }
 8036: 
 8037: sub get_commblock_resources {
 8038:     my ($blocks) = @_;
 8039:     my %blockers = ();
 8040:     return %blockers unless ($env{'request.course.id'});
 8041:     return %blockers if ($env{'user.priv.'.$env{'request.role'}} =~/evb\&([^\:]*)/);
 8042:     my %commblocks;
 8043:     if (ref($blocks) eq 'HASH') {
 8044:         %commblocks = %{$blocks};
 8045:     } else {
 8046:         %commblocks = &get_comm_blocks();
 8047:     }
 8048:     return %blockers unless (keys(%commblocks) > 0); 
 8049:     my $navmap = Apache::lonnavmaps::navmap->new();
 8050:     return %blockers unless (ref($navmap));
 8051:     my $now = time;
 8052:     foreach my $block (keys(%commblocks)) {
 8053:         if ($block =~ /^(\d+)____(\d+)$/) {
 8054:             my ($start,$end) = ($1,$2);
 8055:             if ($start <= $now && $end >= $now) {
 8056:                 if (ref($commblocks{$block}{'blocks'}) eq 'HASH') {
 8057:                     if (ref($commblocks{$block}{'blocks'}{'docs'}) eq 'HASH') {
 8058:                         if (ref($commblocks{$block}{'blocks'}{'docs'}{'maps'}) eq 'HASH') {
 8059:                             if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'maps'}})) {
 8060:                                 $blockers{$block}{maps} = $commblocks{$block}{'blocks'}{'docs'}{'maps'}; 
 8061:                             }
 8062:                         }
 8063:                         if (ref($commblocks{$block}{'blocks'}{'docs'}{'resources'}) eq 'HASH') {
 8064:                             if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'resources'}})) {
 8065:                                 $blockers{$block}{'resources'} = $commblocks{$block}{'blocks'}{'docs'}{'resources'};
 8066:                             }
 8067:                         }
 8068:                     }
 8069:                 }
 8070:             }
 8071:         } elsif ($block =~ /^firstaccess____(.+)$/) {
 8072:             my $item = $1;
 8073:             my @to_test;
 8074:             if (ref($commblocks{$block}{'blocks'}) eq 'HASH') {
 8075:                 if (ref($commblocks{$block}{'blocks'}{'docs'}) eq 'HASH') {
 8076:                     my @interval;
 8077:                     my $type = 'map';
 8078:                     if ($item eq 'course') {
 8079:                         $type = 'course';
 8080:                         @interval=&EXT("resource.0.interval");
 8081:                     } else {
 8082:                         if ($item =~ /___\d+___/) {
 8083:                             $type = 'resource';
 8084:                             @interval=&EXT("resource.0.interval",$item);
 8085:                             if (ref($navmap)) {                        
 8086:                                 my $res = $navmap->getBySymb($item); 
 8087:                                 push(@to_test,$res);
 8088:                             }
 8089:                         } else {
 8090:                             my $mapsymb = &symbread($item,1);
 8091:                             if ($mapsymb) {
 8092:                                 if (ref($navmap)) {
 8093:                                     my $mapres = $navmap->getBySymb($mapsymb);
 8094:                                     @to_test = $mapres->retrieveResources($mapres,undef,0,0,0,1);
 8095:                                     foreach my $res (@to_test) {
 8096:                                         my $symb = $res->symb();
 8097:                                         next if ($symb eq $mapsymb);
 8098:                                         if ($symb ne '') {
 8099:                                             @interval=&EXT("resource.0.interval",$symb);
 8100:                                             if ($interval[1] eq 'map') {
 8101:                                                 last;
 8102:                                             }
 8103:                                         }
 8104:                                     }
 8105:                                 }
 8106:                             }
 8107:                         }
 8108:                     }
 8109:                     if ($interval[0] =~ /^(\d+)/) {
 8110:                         my $timelimit = $1; 
 8111:                         my $first_access;
 8112:                         if ($type eq 'resource') {
 8113:                             $first_access=&get_first_access($interval[1],$item);
 8114:                         } elsif ($type eq 'map') {
 8115:                             $first_access=&get_first_access($interval[1],undef,$item);
 8116:                         } else {
 8117:                             $first_access=&get_first_access($interval[1]);
 8118:                         }
 8119:                         if ($first_access) {
 8120:                             my $timesup = $first_access+$timelimit;
 8121:                             if ($timesup > $now) {
 8122:                                 my $activeblock;
 8123:                                 foreach my $res (@to_test) {
 8124:                                     if ($res->answerable()) {
 8125:                                         $activeblock = 1;
 8126:                                         last;
 8127:                                     }
 8128:                                 }
 8129:                                 if ($activeblock) {
 8130:                                     if (ref($commblocks{$block}{'blocks'}{'docs'}{'maps'}) eq 'HASH') {
 8131:                                          if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'maps'}})) {
 8132:                                              $blockers{$block}{'maps'} = $commblocks{$block}{'blocks'}{'docs'}{'maps'};
 8133:                                          }
 8134:                                     }
 8135:                                     if (ref($commblocks{$block}{'blocks'}{'docs'}{'resources'}) eq 'HASH') {
 8136:                                         if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'resources'}})) {
 8137:                                             $blockers{$block}{'resources'} = $commblocks{$block}{'blocks'}{'docs'}{'resources'};
 8138:                                         }
 8139:                                     }
 8140:                                 }
 8141:                             }
 8142:                         }
 8143:                     }
 8144:                 }
 8145:             }
 8146:         }
 8147:     }
 8148:     return %blockers;
 8149: }
 8150: 
 8151: sub has_comm_blocking {
 8152:     my ($priv,$symb,$uri,$blocks) = @_;
 8153:     my @blockers;
 8154:     return unless ($env{'request.course.id'});
 8155:     return unless ($priv eq 'bre');
 8156:     return if ($env{'user.priv.'.$env{'request.role'}} =~/evb\&([^\:]*)/);
 8157:     return if ($env{'request.state'} eq 'construct');
 8158:     &load_all_blockers($env{'user.name'},$env{'user.domain'},$blocks);
 8159:     return unless (keys(%cachedblockers) > 0);
 8160:     my (%possibles,@symbs);
 8161:     if (!$symb) {
 8162:         $symb = &symbread($uri,1,1,1,\%possibles);
 8163:     }
 8164:     if ($symb) {
 8165:         @symbs = ($symb);
 8166:     } elsif (keys(%possibles)) { 
 8167:         @symbs = keys(%possibles);
 8168:     }
 8169:     my $noblock;
 8170:     foreach my $symb (@symbs) {
 8171:         last if ($noblock);
 8172:         my ($map,$resid,$resurl)=&decode_symb($symb);
 8173:         foreach my $block (keys(%cachedblockers)) {
 8174:             if ($block =~ /^firstaccess____(.+)$/) {
 8175:                 my $item = $1;
 8176:                 if (($item eq $map) || ($item eq $symb)) {
 8177:                     $noblock = 1;
 8178:                     last;
 8179:                 }
 8180:             }
 8181:             if (ref($cachedblockers{$block}) eq 'HASH') {
 8182:                 if (ref($cachedblockers{$block}{'resources'}) eq 'HASH') {
 8183:                     if ($cachedblockers{$block}{'resources'}{$symb}) {
 8184:                         unless (grep(/^\Q$block\E$/,@blockers)) {
 8185:                             push(@blockers,$block);
 8186:                         }
 8187:                     }
 8188:                 }
 8189:             }
 8190:             if (ref($cachedblockers{$block}{'maps'}) eq 'HASH') {
 8191:                 if ($cachedblockers{$block}{'maps'}{$map}) {
 8192:                     unless (grep(/^\Q$block\E$/,@blockers)) {
 8193:                         push(@blockers,$block);
 8194:                     }
 8195:                 }
 8196:             }
 8197:         }
 8198:     }
 8199:     return if ($noblock);
 8200:     return @blockers;
 8201: }
 8202: }
 8203: 
 8204: # -------------------------------- Deversion and split uri into path an filename   
 8205: 
 8206: #
 8207: #   Removes the version from a URI and
 8208: #   splits it in to its filename and path to the filename.
 8209: #   Seems like File::Basename could have done this more clearly.
 8210: #   Parameters:
 8211: #      $uri   - input URI
 8212: #   Returns:
 8213: #     Two element list consisting of 
 8214: #     $pathname  - the URI up to and excluding the trailing /
 8215: #     $filename  - The part of the URI following the last /
 8216: #  NOTE:
 8217: #    Another realization of this is simply:
 8218: #    use File::Basename;
 8219: #    ...
 8220: #    $uri = shift;
 8221: #    $filename = basename($uri);
 8222: #    $path     = dirname($uri);
 8223: #    return ($filename, $path);
 8224: #
 8225: #     The implementation below is probably faster however.
 8226: #
 8227: sub split_uri_for_cond {
 8228:     my $uri=&deversion(&declutter(shift));
 8229:     my @uriparts=split(/\//,$uri);
 8230:     my $filename=pop(@uriparts);
 8231:     my $pathname=join('/',@uriparts);
 8232:     return ($pathname,$filename);
 8233: }
 8234: # --------------------------------------------------- Is a resource on the map?
 8235: 
 8236: sub is_on_map {
 8237:     my ($pathname,$filename) = &split_uri_for_cond(shift);
 8238:     #Trying to find the conditional for the file
 8239:     my $match=($env{'acc.res.'.$env{'request.course.id'}.'.'.$pathname}=~
 8240: 	       /\&\Q$filename\E\:([\d\|]+)\&/);
 8241:     if ($match) {
 8242: 	return (1,$1);
 8243:     } else {
 8244: 	return (0,0);
 8245:     }
 8246: }
 8247: 
 8248: # --------------------------------------------------------- Get symb from alias
 8249: 
 8250: sub get_symb_from_alias {
 8251:     my $symb=shift;
 8252:     my ($map,$resid,$url)=&decode_symb($symb);
 8253: # Already is a symb
 8254:     if ($url) { return $symb; }
 8255: # Must be an alias
 8256:     my $aliassymb='';
 8257:     my %bighash;
 8258:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
 8259:                             &GDBM_READER(),0640)) {
 8260:         my $rid=$bighash{'mapalias_'.$symb};
 8261: 	if ($rid) {
 8262: 	    my ($mapid,$resid)=split(/\./,$rid);
 8263: 	    $aliassymb=&encode_symb($bighash{'map_id_'.$mapid},
 8264: 				    $resid,$bighash{'src_'.$rid});
 8265: 	}
 8266:         untie %bighash;
 8267:     }
 8268:     return $aliassymb;
 8269: }
 8270: 
 8271: # ----------------------------------------------------------------- Define Role
 8272: 
 8273: sub definerole {
 8274:   if (allowed('mcr','/')) {
 8275:     my ($rolename,$sysrole,$domrole,$courole,$uname,$udom)=@_;
 8276:     foreach my $role (split(':',$sysrole)) {
 8277: 	my ($crole,$cqual)=split(/\&/,$role);
 8278:         if ($pr{'cr:s'}!~/\Q$crole\E/) { return "refused:s:$crole"; }
 8279:         if ($pr{'cr:s'}=~/\Q$crole\E\&/) {
 8280: 	    if ($pr{'cr:s'}!~/\Q$crole\E\&\w*\Q$cqual\E/) { 
 8281:                return "refused:s:$crole&$cqual"; 
 8282:             }
 8283:         }
 8284:     }
 8285:     foreach my $role (split(':',$domrole)) {
 8286: 	my ($crole,$cqual)=split(/\&/,$role);
 8287:         if ($pr{'cr:d'}!~/\Q$crole\E/) { return "refused:d:$crole"; }
 8288:         if ($pr{'cr:d'}=~/\Q$crole\E\&/) {
 8289: 	    if ($pr{'cr:d'}!~/\Q$crole\W\&\w*\Q$cqual\E/) { 
 8290:                return "refused:d:$crole&$cqual"; 
 8291:             }
 8292:         }
 8293:     }
 8294:     foreach my $role (split(':',$courole)) {
 8295: 	my ($crole,$cqual)=split(/\&/,$role);
 8296:         if ($pr{'cr:c'}!~/\Q$crole\E/) { return "refused:c:$crole"; }
 8297:         if ($pr{'cr:c'}=~/\Q$crole\E\&/) {
 8298: 	    if ($pr{'cr:c'}!~/\Q$crole\E\&\w*\Q$cqual\E/) { 
 8299:                return "refused:c:$crole&$cqual"; 
 8300:             }
 8301:         }
 8302:     }
 8303:     my $uhome;
 8304:     if (($uname ne '') && ($udom ne '')) {
 8305:         $uhome = &homeserver($uname,$udom);
 8306:         return $uhome if ($uhome eq 'no_host');
 8307:     } else {
 8308:         $uname = $env{'user.name'};
 8309:         $udom = $env{'user.domain'};
 8310:         $uhome = $env{'user.home'};
 8311:     }
 8312:     my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
 8313:                 "$udom:$uname:rolesdef_$rolename=".
 8314:                 escape($sysrole.'_'.$domrole.'_'.$courole);
 8315:     return reply($command,$uhome);
 8316:   } else {
 8317:     return 'refused';
 8318:   }
 8319: }
 8320: 
 8321: # ---------------- Make a metadata query against the network of library servers
 8322: 
 8323: sub metadata_query {
 8324:     my ($query,$custom,$customshow,$server_array,$domains_hash)=@_;
 8325:     my %rhash;
 8326:     my %libserv = &all_library();
 8327:     my @server_list = (defined($server_array) ? @$server_array
 8328:                                               : keys(%libserv) );
 8329:     for my $server (@server_list) {
 8330:         my $domains = ''; 
 8331:         if (ref($domains_hash) eq 'HASH') {
 8332:             $domains = $domains_hash->{$server}; 
 8333:         }
 8334: 	unless ($custom or $customshow) {
 8335: 	    my $reply=&reply("querysend:".&escape($query).':::'.&escape($domains),$server);
 8336: 	    $rhash{$server}=$reply;
 8337: 	}
 8338: 	else {
 8339: 	    my $reply=&reply("querysend:".&escape($query).':'.
 8340: 			     &escape($custom).':'.&escape($customshow).':'.&escape($domains),
 8341: 			     $server);
 8342: 	    $rhash{$server}=$reply;
 8343: 	}
 8344:     }
 8345:     return \%rhash;
 8346: }
 8347: 
 8348: # ----------------------------------------- Send log queries and wait for reply
 8349: 
 8350: sub log_query {
 8351:     my ($uname,$udom,$query,%filters)=@_;
 8352:     my $uhome=&homeserver($uname,$udom);
 8353:     if ($uhome eq 'no_host') { return 'error: no_host'; }
 8354:     my $uhost=&hostname($uhome);
 8355:     my $command=&escape(join(':',map{$_.'='.$filters{$_}} keys(%filters)));
 8356:     my $queryid=&reply("querysend:".$query.':'.$udom.':'.$uname.':'.$command,
 8357:                        $uhome);
 8358:     unless ($queryid=~/^\Q$uhost\E\_/) { return 'error: '.$queryid; }
 8359:     return get_query_reply($queryid);
 8360: }
 8361: 
 8362: # -------------------------- Update MySQL table for portfolio file
 8363: 
 8364: sub update_portfolio_table {
 8365:     my ($uname,$udom,$file_name,$query,$group,$action) = @_;
 8366:     if ($group ne '') {
 8367:         $file_name =~s /^\Q$group\E//;
 8368:     }
 8369:     my $homeserver = &homeserver($uname,$udom);
 8370:     my $queryid=
 8371:         &reply("querysend:".$query.':'.&escape($uname.':'.$udom.':'.$group).
 8372:                ':'.&escape($file_name).':'.$action,$homeserver);
 8373:     my $reply = &get_query_reply($queryid);
 8374:     return $reply;
 8375: }
 8376: 
 8377: # -------------------------- Update MySQL allusers table
 8378: 
 8379: sub update_allusers_table {
 8380:     my ($uname,$udom,$names) = @_;
 8381:     my $homeserver = &homeserver($uname,$udom);
 8382:     my $queryid=
 8383:         &reply('querysend:allusers:'.&escape($uname).':'.&escape($udom).':'.
 8384:                'lastname='.&escape($names->{'lastname'}).'%%'.
 8385:                'firstname='.&escape($names->{'firstname'}).'%%'.
 8386:                'middlename='.&escape($names->{'middlename'}).'%%'.
 8387:                'generation='.&escape($names->{'generation'}).'%%'.
 8388:                'permanentemail='.&escape($names->{'permanentemail'}).'%%'.
 8389:                'id='.&escape($names->{'id'}),$homeserver);
 8390:     return;
 8391: }
 8392: 
 8393: # ------- Request retrieval of institutional classlists for course(s)
 8394: 
 8395: sub fetch_enrollment_query {
 8396:     my ($context,$affiliatesref,$replyref,$dom,$cnum) = @_;
 8397:     my ($homeserver,$sleep,$loopmax);
 8398:     my $maxtries = 1;
 8399:     if ($context eq 'automated') {
 8400:         $homeserver = $perlvar{'lonHostID'};
 8401:         $sleep = 2;
 8402:         $loopmax = 100;
 8403:         $maxtries = 10; # will wait for up to 2000s for retrieval of classlist data before timeout
 8404:     } else {
 8405:         $homeserver = &homeserver($cnum,$dom);
 8406:     }
 8407:     my $host=&hostname($homeserver);
 8408:     my $cmd = '';
 8409:     foreach my $affiliate (keys(%{$affiliatesref})) {
 8410:         $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
 8411:     }
 8412:     $cmd =~ s/%%$//;
 8413:     $cmd = &escape($cmd);
 8414:     my $query = 'fetchenrollment';
 8415:     my $queryid=&reply("querysend:".$query.':'.$dom.':'.$env{'user.name'}.':'.$cmd,$homeserver);
 8416:     unless ($queryid=~/^\Q$host\E\_/) { 
 8417:         &logthis('fetch_enrollment_query: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' context: '.$context.' '.$cnum); 
 8418:         return 'error: '.$queryid;
 8419:     }
 8420:     my $reply = &get_query_reply($queryid,$sleep,$loopmax);
 8421:     my $tries = 1;
 8422:     while (($reply=~/^timeout/) && ($tries < $maxtries)) {
 8423:         $reply = &get_query_reply($queryid,$sleep,$loopmax);
 8424:         $tries ++;
 8425:     }
 8426:     if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
 8427:         &logthis('fetch_enrollment_query error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' context: '.$context.' '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
 8428:     } else {
 8429:         my @responses = split(/:/,$reply);
 8430:         if (grep { $_ eq $homeserver } &current_machine_ids()) {
 8431:             foreach my $line (@responses) {
 8432:                 my ($key,$value) = split(/=/,$line,2);
 8433:                 $$replyref{$key} = $value;
 8434:             }
 8435:         } else {
 8436:             my $pathname = LONCAPA::tempdir();
 8437:             foreach my $line (@responses) {
 8438:                 my ($key,$value) = split(/=/,$line);
 8439:                 $$replyref{$key} = $value;
 8440:                 if ($value > 0) {
 8441:                     foreach my $item (@{$$affiliatesref{$key}}) {
 8442:                         my $filename = $dom.'_'.$key.'_'.$item.'_classlist.xml';
 8443:                         my $destname = $pathname.'/'.$filename;
 8444:                         my $xml_classlist = &reply("autoretrieve:".$filename,$homeserver);
 8445:                         if ($xml_classlist =~ /^error/) {
 8446:                             &logthis('fetch_enrollment_query - autoretrieve error: '.$xml_classlist.' for '.$filename.' from server: '.$homeserver.' '.$context.' '.$cnum);
 8447:                         } else {
 8448:                             if ( open(FILE,">$destname") ) {
 8449:                                 print FILE &unescape($xml_classlist);
 8450:                                 close(FILE);
 8451:                             } else {
 8452:                                 &logthis('fetch_enrollment_query - error opening classlist file '.$destname.' '.$context.' '.$cnum);
 8453:                             }
 8454:                         }
 8455:                     }
 8456:                 }
 8457:             }
 8458:         }
 8459:         return 'ok';
 8460:     }
 8461:     return 'error';
 8462: }
 8463: 
 8464: sub get_query_reply {
 8465:     my ($queryid,$sleep,$loopmax) = @_;;
 8466:     if (($sleep eq '') || ($sleep !~ /^\d+\.?\d*$/)) {
 8467:         $sleep = 0.2;
 8468:     }
 8469:     if (($loopmax eq '') || ($loopmax =~ /\D/)) {
 8470:         $loopmax = 100;
 8471:     }
 8472:     my $replyfile=LONCAPA::tempdir().$queryid;
 8473:     my $reply='';
 8474:     for (1..$loopmax) {
 8475: 	sleep($sleep);
 8476:         if (-e $replyfile.'.end') {
 8477: 	    if (open(my $fh,$replyfile)) {
 8478: 		$reply = join('',<$fh>);
 8479: 		close($fh);
 8480: 	   } else { return 'error: reply_file_error'; }
 8481:            return &unescape($reply);
 8482: 	}
 8483:     }
 8484:     return 'timeout:'.$queryid;
 8485: }
 8486: 
 8487: sub courselog_query {
 8488: #
 8489: # possible filters:
 8490: # url: url or symb
 8491: # username
 8492: # domain
 8493: # action: view, submit, grade
 8494: # start: timestamp
 8495: # end: timestamp
 8496: #
 8497:     my (%filters)=@_;
 8498:     unless ($env{'request.course.id'}) { return 'no_course'; }
 8499:     if ($filters{'url'}) {
 8500: 	$filters{'url'}=&symbclean(&declutter($filters{'url'}));
 8501:         $filters{'url'}=~s/\.(\w+)$/(\\.\\d+)*\\.$1/;
 8502:         $filters{'url'}=~s/\.(\w+)\_\_\_/(\\.\\d+)*\\.$1/;
 8503:     }
 8504:     my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
 8505:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 8506:     return &log_query($cname,$cdom,'courselog',%filters);
 8507: }
 8508: 
 8509: sub userlog_query {
 8510: #
 8511: # possible filters:
 8512: # action: log check role
 8513: # start: timestamp
 8514: # end: timestamp
 8515: #
 8516:     my ($uname,$udom,%filters)=@_;
 8517:     return &log_query($uname,$udom,'userlog',%filters);
 8518: }
 8519: 
 8520: #--------- Call auto-enrollment subs in localenroll.pm for homeserver for course 
 8521: 
 8522: sub auto_run {
 8523:     my ($cnum,$cdom) = @_;
 8524:     my $response = 0;
 8525:     my $settings;
 8526:     my %domconfig = &get_dom('configuration',['autoenroll'],$cdom);
 8527:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 8528:         $settings = $domconfig{'autoenroll'};
 8529:         if ($settings->{'run'} eq '1') {
 8530:             $response = 1;
 8531:         }
 8532:     } else {
 8533:         my $homeserver;
 8534:         if (&is_course($cdom,$cnum)) {
 8535:             $homeserver = &homeserver($cnum,$cdom);
 8536:         } else {
 8537:             $homeserver = &domain($cdom,'primary');
 8538:         }
 8539:         if ($homeserver ne 'no_host') {
 8540:             $response = &reply('autorun:'.$cdom,$homeserver);
 8541:         }
 8542:     }
 8543:     return $response;
 8544: }
 8545: 
 8546: sub auto_get_sections {
 8547:     my ($cnum,$cdom,$inst_coursecode) = @_;
 8548:     my $homeserver;
 8549:     if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) { 
 8550:         $homeserver = &homeserver($cnum,$cdom);
 8551:     }
 8552:     if (!defined($homeserver)) { 
 8553:         if ($cdom =~ /^$match_domain$/) {
 8554:             $homeserver = &domain($cdom,'primary');
 8555:         }
 8556:     }
 8557:     my @secs;
 8558:     if (defined($homeserver)) {
 8559:         my $response=&unescape(&reply('autogetsections:'.$inst_coursecode.':'.$cdom,$homeserver));
 8560:         unless ($response eq 'refused') {
 8561:             @secs = split(/:/,$response);
 8562:         }
 8563:     }
 8564:     return @secs;
 8565: }
 8566: 
 8567: sub auto_new_course {
 8568:     my ($cnum,$cdom,$inst_course_id,$owner,$coowners) = @_;
 8569:     my $homeserver = &homeserver($cnum,$cdom);
 8570:     my $response=&unescape(&reply('autonewcourse:'.$inst_course_id.':'.&escape($owner).':'.$cdom.':'.&escape($coowners),$homeserver));
 8571:     return $response;
 8572: }
 8573: 
 8574: sub auto_validate_courseID {
 8575:     my ($cnum,$cdom,$inst_course_id) = @_;
 8576:     my $homeserver = &homeserver($cnum,$cdom);
 8577:     my $response=&unescape(&reply('autovalidatecourse:'.$inst_course_id.':'.$cdom,$homeserver));
 8578:     return $response;
 8579: }
 8580: 
 8581: sub auto_validate_instcode {
 8582:     my ($cnum,$cdom,$instcode,$owner) = @_;
 8583:     my ($homeserver,$response);
 8584:     if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
 8585:         $homeserver = &homeserver($cnum,$cdom);
 8586:     }
 8587:     if (!defined($homeserver)) {
 8588:         if ($cdom =~ /^$match_domain$/) {
 8589:             $homeserver = &domain($cdom,'primary');
 8590:         }
 8591:     }
 8592:     $response=&unescape(&reply('autovalidateinstcode:'.$cdom.':'.
 8593:                         &escape($instcode).':'.&escape($owner),$homeserver));
 8594:     my ($outcome,$description,$defaultcredits) = map { &unescape($_); } split('&',$response,3);
 8595:     return ($outcome,$description,$defaultcredits);
 8596: }
 8597: 
 8598: sub auto_create_password {
 8599:     my ($cnum,$cdom,$authparam,$udom) = @_;
 8600:     my ($homeserver,$response);
 8601:     my $create_passwd = 0;
 8602:     my $authchk = '';
 8603:     if ($udom =~ /^$match_domain$/) {
 8604:         $homeserver = &domain($udom,'primary');
 8605:     }
 8606:     if ($homeserver eq '') {
 8607:         if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
 8608:             $homeserver = &homeserver($cnum,$cdom);
 8609:         }
 8610:     }
 8611:     if ($homeserver eq '') {
 8612:         $authchk = 'nodomain';
 8613:     } else {
 8614:         $response=&unescape(&reply('autocreatepassword:'.$authparam.':'.$cdom,$homeserver));
 8615:         if ($response eq 'refused') {
 8616:             $authchk = 'refused';
 8617:         } else {
 8618:             ($authparam,$create_passwd,$authchk) = split(/:/,$response);
 8619:         }
 8620:     }
 8621:     return ($authparam,$create_passwd,$authchk);
 8622: }
 8623: 
 8624: sub auto_photo_permission {
 8625:     my ($cnum,$cdom,$students) = @_;
 8626:     my $homeserver = &homeserver($cnum,$cdom);
 8627:     my ($outcome,$perm_reqd,$conditions) = 
 8628: 	split(/:/,&unescape(&reply('autophotopermission:'.$cdom,$homeserver)),3);
 8629:     if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
 8630: 	return (undef,undef);
 8631:     }
 8632:     return ($outcome,$perm_reqd,$conditions);
 8633: }
 8634: 
 8635: sub auto_checkphotos {
 8636:     my ($uname,$udom,$pid) = @_;
 8637:     my $homeserver = &homeserver($uname,$udom);
 8638:     my ($result,$resulttype);
 8639:     my $outcome = &unescape(&reply('autophotocheck:'.&escape($udom).':'.
 8640: 				   &escape($uname).':'.&escape($pid),
 8641: 				   $homeserver));
 8642:     if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
 8643: 	return (undef,undef);
 8644:     }
 8645:     if ($outcome) {
 8646:         ($result,$resulttype) = split(/:/,$outcome);
 8647:     } 
 8648:     return ($result,$resulttype);
 8649: }
 8650: 
 8651: sub auto_photochoice {
 8652:     my ($cnum,$cdom) = @_;
 8653:     my $homeserver = &homeserver($cnum,$cdom);
 8654:     my ($update,$comment) = split(/:/,&unescape(&reply('autophotochoice:'.
 8655: 						       &escape($cdom),
 8656: 						       $homeserver)));
 8657:     if ($update =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
 8658: 	return (undef,undef);
 8659:     }
 8660:     return ($update,$comment);
 8661: }
 8662: 
 8663: sub auto_photoupdate {
 8664:     my ($affiliatesref,$dom,$cnum,$photo) = @_;
 8665:     my $homeserver = &homeserver($cnum,$dom);
 8666:     my $host=&hostname($homeserver);
 8667:     my $cmd = '';
 8668:     my $maxtries = 1;
 8669:     foreach my $affiliate (keys(%{$affiliatesref})) {
 8670:         $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
 8671:     }
 8672:     $cmd =~ s/%%$//;
 8673:     $cmd = &escape($cmd);
 8674:     my $query = 'institutionalphotos';
 8675:     my $queryid=&reply("querysend:".$query.':'.$dom.':'.$cnum.':'.$cmd,$homeserver);
 8676:     unless ($queryid=~/^\Q$host\E\_/) {
 8677:         &logthis('institutionalphotos: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' and course: '.$cnum);
 8678:         return 'error: '.$queryid;
 8679:     }
 8680:     my $reply = &get_query_reply($queryid);
 8681:     my $tries = 1;
 8682:     while (($reply=~/^timeout/) && ($tries < $maxtries)) {
 8683:         $reply = &get_query_reply($queryid);
 8684:         $tries ++;
 8685:     }
 8686:     if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
 8687:         &logthis('institutionalphotos error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' course: '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
 8688:     } else {
 8689:         my @responses = split(/:/,$reply);
 8690:         my $outcome = shift(@responses); 
 8691:         foreach my $item (@responses) {
 8692:             my ($key,$value) = split(/=/,$item);
 8693:             $$photo{$key} = $value;
 8694:         }
 8695:         return $outcome;
 8696:     }
 8697:     return 'error';
 8698: }
 8699: 
 8700: sub auto_instcode_format {
 8701:     my ($caller,$codedom,$instcodes,$codes,$codetitles,$cat_titles,
 8702: 	$cat_order) = @_;
 8703:     my $courses = '';
 8704:     my @homeservers;
 8705:     if ($caller eq 'global') {
 8706: 	my %servers = &get_servers($codedom,'library');
 8707: 	foreach my $tryserver (keys(%servers)) {
 8708: 	    if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
 8709: 		push(@homeservers,$tryserver);
 8710: 	    }
 8711:         }
 8712:     } elsif ($caller eq 'requests') {
 8713:         if ($codedom =~ /^$match_domain$/) {
 8714:             my $chome = &domain($codedom,'primary');
 8715:             unless ($chome eq 'no_host') {
 8716:                 push(@homeservers,$chome);
 8717:             }
 8718:         }
 8719:     } else {
 8720:         push(@homeservers,&homeserver($caller,$codedom));
 8721:     }
 8722:     foreach my $code (keys(%{$instcodes})) {
 8723:         $courses .= &escape($code).'='.&escape($$instcodes{$code}).'&';
 8724:     }
 8725:     chop($courses);
 8726:     my $ok_response = 0;
 8727:     my $response;
 8728:     while (@homeservers > 0 && $ok_response == 0) {
 8729:         my $server = shift(@homeservers); 
 8730:         $response=&reply('autoinstcodeformat:'.$codedom.':'.$courses,$server);
 8731:         if ($response !~ /(con_lost|error|no_such_host|refused)/) {
 8732:             my ($codes_str,$codetitles_str,$cat_titles_str,$cat_order_str) = 
 8733: 		split(/:/,$response);
 8734:             %{$codes} = (%{$codes},&str2hash($codes_str));
 8735:             push(@{$codetitles},&str2array($codetitles_str));
 8736:             %{$cat_titles} = (%{$cat_titles},&str2hash($cat_titles_str));
 8737:             %{$cat_order} = (%{$cat_order},&str2hash($cat_order_str));
 8738:             $ok_response = 1;
 8739:         }
 8740:     }
 8741:     if ($ok_response) {
 8742:         return 'ok';
 8743:     } else {
 8744:         return $response;
 8745:     }
 8746: }
 8747: 
 8748: sub auto_instcode_defaults {
 8749:     my ($domain,$returnhash,$code_order) = @_;
 8750:     my @homeservers;
 8751: 
 8752:     my %servers = &get_servers($domain,'library');
 8753:     foreach my $tryserver (keys(%servers)) {
 8754: 	if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
 8755: 	    push(@homeservers,$tryserver);
 8756: 	}
 8757:     }
 8758: 
 8759:     my $response;
 8760:     foreach my $server (@homeservers) {
 8761:         $response=&reply('autoinstcodedefaults:'.$domain,$server);
 8762:         next if ($response =~ /(con_lost|error|no_such_host|refused)/);
 8763: 	
 8764: 	foreach my $pair (split(/\&/,$response)) {
 8765: 	    my ($name,$value)=split(/\=/,$pair);
 8766: 	    if ($name eq 'code_order') {
 8767: 		@{$code_order} = split(/\&/,&unescape($value));
 8768: 	    } else {
 8769: 		$returnhash->{&unescape($name)}=&unescape($value);
 8770: 	    }
 8771: 	}
 8772: 	return 'ok';
 8773:     }
 8774: 
 8775:     return $response;
 8776: }
 8777: 
 8778: sub auto_possible_instcodes {
 8779:     my ($domain,$codetitles,$cat_titles,$cat_orders,$code_order) = @_;
 8780:     unless ((ref($codetitles) eq 'ARRAY') && (ref($cat_titles) eq 'HASH') && 
 8781:             (ref($cat_orders) eq 'HASH') && (ref($code_order) eq 'ARRAY')) {
 8782:         return;
 8783:     }
 8784:     my (@homeservers,$uhome);
 8785:     if (defined(&domain($domain,'primary'))) {
 8786:         $uhome=&domain($domain,'primary');
 8787:         push(@homeservers,&domain($domain,'primary'));
 8788:     } else {
 8789:         my %servers = &get_servers($domain,'library');
 8790:         foreach my $tryserver (keys(%servers)) {
 8791:             if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
 8792:                 push(@homeservers,$tryserver);
 8793:             }
 8794:         }
 8795:     }
 8796:     my $response;
 8797:     foreach my $server (@homeservers) {
 8798:         $response=&reply('autopossibleinstcodes:'.$domain,$server);
 8799:         next if ($response =~ /(con_lost|error|no_such_host|refused)/);
 8800:         my ($codetitlestr,$codeorderstr,$cat_title,$cat_order) = 
 8801:             split(':',$response);
 8802:         @{$codetitles} = map { &unescape($_); } (split('&',$codetitlestr));
 8803:         @{$code_order} = map { &unescape($_); } (split('&',$codeorderstr));
 8804:         foreach my $item (split('&',$cat_title)) {   
 8805:             my ($name,$value)=split('=',$item);
 8806:             $cat_titles->{&unescape($name)}=&thaw_unescape($value);
 8807:         }
 8808:         foreach my $item (split('&',$cat_order)) {
 8809:             my ($name,$value)=split('=',$item);
 8810:             $cat_orders->{&unescape($name)}=&thaw_unescape($value);
 8811:         }
 8812:         return 'ok';
 8813:     }
 8814:     return $response;
 8815: }
 8816: 
 8817: sub auto_courserequest_checks {
 8818:     my ($dom) = @_;
 8819:     my ($homeserver,%validations);
 8820:     if ($dom =~ /^$match_domain$/) {
 8821:         $homeserver = &domain($dom,'primary');
 8822:     }
 8823:     unless ($homeserver eq 'no_host') {
 8824:         my $response=&reply('autocrsreqchecks:'.$dom,$homeserver);
 8825:         unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
 8826:             my @items = split(/&/,$response);
 8827:             foreach my $item (@items) {
 8828:                 my ($key,$value) = split('=',$item);
 8829:                 $validations{&unescape($key)} = &thaw_unescape($value);
 8830:             }
 8831:         }
 8832:     }
 8833:     return %validations; 
 8834: }
 8835: 
 8836: sub auto_courserequest_validation {
 8837:     my ($dom,$owner,$crstype,$inststatuslist,$instcode,$instseclist,$custominfo) = @_;
 8838:     my ($homeserver,$response);
 8839:     if ($dom =~ /^$match_domain$/) {
 8840:         $homeserver = &domain($dom,'primary');
 8841:     }
 8842:     unless ($homeserver eq 'no_host') {
 8843:         my $customdata;
 8844:         if (ref($custominfo) eq 'HASH') {
 8845:             $customdata = &freeze_escape($custominfo);
 8846:         }
 8847:         $response=&unescape(&reply('autocrsreqvalidation:'.$dom.':'.&escape($owner).
 8848:                                     ':'.&escape($crstype).':'.&escape($inststatuslist).
 8849:                                     ':'.&escape($instcode).':'.&escape($instseclist).':'.
 8850:                                     $customdata,$homeserver));
 8851:     }
 8852:     return $response;
 8853: }
 8854: 
 8855: sub auto_validate_class_sec {
 8856:     my ($cdom,$cnum,$owners,$inst_class) = @_;
 8857:     my $homeserver = &homeserver($cnum,$cdom);
 8858:     my $ownerlist;
 8859:     if (ref($owners) eq 'ARRAY') {
 8860:         $ownerlist = join(',',@{$owners});
 8861:     } else {
 8862:         $ownerlist = $owners;
 8863:     }
 8864:     my $response=&reply('autovalidateclass_sec:'.$inst_class.':'.
 8865:                         &escape($ownerlist).':'.$cdom,$homeserver);
 8866:     return $response;
 8867: }
 8868: 
 8869: sub auto_crsreq_update {
 8870:     my ($cdom,$cnum,$crstype,$action,$ownername,$ownerdomain,$fullname,$title,
 8871:         $code,$accessstart,$accessend,$inbound) = @_;
 8872:     my ($homeserver,%crsreqresponse);
 8873:     if ($cdom =~ /^$match_domain$/) {
 8874:         $homeserver = &domain($cdom,'primary');
 8875:     }
 8876:     unless (($homeserver eq 'no_host') || ($homeserver eq '')) {
 8877:         my $info;
 8878:         if (ref($inbound) eq 'HASH') {
 8879:             $info = &freeze_escape($inbound);
 8880:         }
 8881:         my $response=&reply('autocrsrequpdate:'.$cdom.':'.$cnum.':'.&escape($crstype).
 8882:                             ':'.&escape($action).':'.&escape($ownername).':'.
 8883:                             &escape($ownerdomain).':'.&escape($fullname).':'.
 8884:                             &escape($title).':'.&escape($code).':'.
 8885:                             &escape($accessstart).':'.&escape($accessend).':'.$info,
 8886:                             $homeserver);
 8887:         unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
 8888:             my @items = split(/&/,$response);
 8889:             foreach my $item (@items) {
 8890:                 my ($key,$value) = split('=',$item);
 8891:                 $crsreqresponse{&unescape($key)} = &thaw_unescape($value);
 8892:             }
 8893:         }
 8894:     }
 8895:     return \%crsreqresponse;
 8896: }
 8897: 
 8898: sub auto_export_grades {
 8899:     my ($cdom,$cnum,$inforef,$gradesref) = @_;
 8900:     my ($homeserver,%exportresponse);
 8901:     if ($cdom =~ /^$match_domain$/) {
 8902:         $homeserver = &domain($cdom,'primary');
 8903:     }
 8904:     unless (($homeserver eq 'no_host') || ($homeserver eq '')) {
 8905:         my $info;
 8906:         if (ref($inforef) eq 'HASH') {
 8907:             $info = &freeze_escape($inforef);
 8908:         }
 8909:         if (ref($gradesref) eq 'HASH') {
 8910:             my $grades = &freeze_escape($gradesref);
 8911:             my $response=&reply('encrypt:autoexportgrades:'.$cdom.':'.$cnum.':'.
 8912:                                 $info.':'.$grades,$homeserver);
 8913:             unless ($response =~ /(con_lost|error|no_such_host|refused|unknown_command)/) {
 8914:                 my @items = split(/&/,$response);
 8915:                 foreach my $item (@items) {
 8916:                     my ($key,$value) = split('=',$item);
 8917:                     $exportresponse{&unescape($key)} = &thaw_unescape($value);
 8918:                 }
 8919:             }
 8920:         }
 8921:     }
 8922:     return \%exportresponse;
 8923: }
 8924: 
 8925: sub check_instcode_cloning {
 8926:     my ($codedefaults,$code_order,$cloner,$clonefromcode,$clonetocode) = @_;
 8927:     unless ((ref($codedefaults) eq 'HASH') && (ref($code_order) eq 'ARRAY')) {
 8928:         return;
 8929:     }
 8930:     my $canclone;
 8931:     if (@{$code_order} > 0) {
 8932:         my $instcoderegexp ='^';
 8933:         my @clonecodes = split(/\&/,$cloner);
 8934:         foreach my $item (@{$code_order}) {
 8935:             if (grep(/^\Q$item\E=/,@clonecodes)) {
 8936:                 foreach my $pair (@clonecodes) {
 8937:                     my ($key,$val) = split(/\=/,$pair,2);
 8938:                     $val = &unescape($val);
 8939:                     if ($key eq $item) {
 8940:                         $instcoderegexp .= '('.$val.')';
 8941:                         last;
 8942:                     }
 8943:                 }
 8944:             } else {
 8945:                 $instcoderegexp .= $codedefaults->{$item};
 8946:             }
 8947:         }
 8948:         $instcoderegexp .= '$';
 8949:         my (@from,@to);
 8950:         eval {
 8951:                (@from) = ($clonefromcode =~ /$instcoderegexp/);
 8952:                (@to) = ($clonetocode =~ /$instcoderegexp/);
 8953:         };
 8954:         if ((@from > 0) && (@to > 0)) {
 8955:             my @diffs = &Apache::loncommon::compare_arrays(\@from,\@to);
 8956:             if (!@diffs) {
 8957:                 $canclone = 1;
 8958:             }
 8959:         }
 8960:     }
 8961:     return $canclone;
 8962: }
 8963: 
 8964: sub default_instcode_cloning {
 8965:     my ($clonedom,$domdefclone,$clonefromcode,$clonetocode,$codedefaultsref,$codeorderref) = @_;
 8966:     my (%codedefaults,@code_order,$canclone);
 8967:     if ((ref($codedefaultsref) eq 'HASH') && (ref($codeorderref) eq 'ARRAY')) {
 8968:         %codedefaults = %{$codedefaultsref};
 8969:         @code_order = @{$codeorderref};
 8970:     } elsif ($clonedom) {
 8971:         &auto_instcode_defaults($clonedom,\%codedefaults,\@code_order);
 8972:     }
 8973:     if (($domdefclone) && (@code_order)) {
 8974:         my @clonecodes = split(/\+/,$domdefclone);
 8975:         my $instcoderegexp ='^';
 8976:         foreach my $item (@code_order) {
 8977:             if (grep(/^\Q$item\E$/,@clonecodes)) {
 8978:                 $instcoderegexp .= '('.$codedefaults{$item}.')';
 8979:             } else {
 8980:                 $instcoderegexp .= $codedefaults{$item};
 8981:             }
 8982:         }
 8983:         $instcoderegexp .= '$';
 8984:         my (@from,@to);
 8985:         eval {
 8986:             (@from) = ($clonefromcode =~ /$instcoderegexp/);
 8987:             (@to) = ($clonetocode =~ /$instcoderegexp/);
 8988:         };
 8989:         if ((@from > 0) && (@to > 0)) {
 8990:             my @diffs = &Apache::loncommon::compare_arrays(\@from,\@to);
 8991:             if (!@diffs) {
 8992:                 $canclone = 1;
 8993:             }
 8994:         }
 8995:     }
 8996:     return $canclone;
 8997: }
 8998: 
 8999: # ------------------------------------------------------- Course Group routines
 9000: 
 9001: sub get_coursegroups {
 9002:     my ($cdom,$cnum,$group,$namespace) = @_;
 9003:     return(&dump($namespace,$cdom,$cnum,$group));
 9004: }
 9005: 
 9006: sub modify_coursegroup {
 9007:     my ($cdom,$cnum,$groupsettings) = @_;
 9008:     return(&put('coursegroups',$groupsettings,$cdom,$cnum));
 9009: }
 9010: 
 9011: sub toggle_coursegroup_status {
 9012:     my ($cdom,$cnum,$group,$action) = @_;
 9013:     my ($from_namespace,$to_namespace);
 9014:     if ($action eq 'delete') {
 9015:         $from_namespace = 'coursegroups';
 9016:         $to_namespace = 'deleted_groups';
 9017:     } else {
 9018:         $from_namespace = 'deleted_groups';
 9019:         $to_namespace = 'coursegroups';
 9020:     }
 9021:     my %curr_group = &get_coursegroups($cdom,$cnum,$group,$from_namespace);
 9022:     if (my $tmp = &error(%curr_group)) {
 9023:         &Apache::lonnet::logthis('Error retrieving group: '.$tmp.' in '.$cnum.':'.$cdom);
 9024:         return ('read error',$tmp);
 9025:     } else {
 9026:         my %savedsettings = %curr_group; 
 9027:         my $result = &put($to_namespace,\%savedsettings,$cdom,$cnum);
 9028:         my $deloutcome;
 9029:         if ($result eq 'ok') {
 9030:             $deloutcome = &del($from_namespace,[$group],$cdom,$cnum);
 9031:         } else {
 9032:             return ('write error',$result);
 9033:         }
 9034:         if ($deloutcome eq 'ok') {
 9035:             return 'ok';
 9036:         } else {
 9037:             return ('delete error',$deloutcome);
 9038:         }
 9039:     }
 9040: }
 9041: 
 9042: sub modify_group_roles {
 9043:     my ($cdom,$cnum,$group_id,$user,$end,$start,$userprivs,$selfenroll,$context) = @_;
 9044:     my $url = '/'.$cdom.'/'.$cnum.'/'.$group_id;
 9045:     my $role = 'gr/'.&escape($userprivs);
 9046:     my ($uname,$udom) = split(/:/,$user);
 9047:     my $result = &assignrole($udom,$uname,$url,$role,$end,$start,'',$selfenroll,$context);
 9048:     if ($result eq 'ok') {
 9049:         &devalidate_getgroups_cache($udom,$uname,$cdom,$cnum);
 9050:     }
 9051:     return $result;
 9052: }
 9053: 
 9054: sub modify_coursegroup_membership {
 9055:     my ($cdom,$cnum,$membership) = @_;
 9056:     my $result = &put('groupmembership',$membership,$cdom,$cnum);
 9057:     return $result;
 9058: }
 9059: 
 9060: sub get_active_groups {
 9061:     my ($udom,$uname,$cdom,$cnum) = @_;
 9062:     my $now = time;
 9063:     my %groups = ();
 9064:     foreach my $key (keys(%env)) {
 9065:         if ($key =~ m-user\.role\.gr\./($match_domain)/($match_courseid)/(\w+)$-) {
 9066:             my ($start,$end) = split(/\./,$env{$key});
 9067:             if (($end!=0) && ($end<$now)) { next; }
 9068:             if (($start!=0) && ($start>$now)) { next; }
 9069:             if ($1 eq $cdom && $2 eq $cnum) {
 9070:                 $groups{$3} = $env{$key} ;
 9071:             }
 9072:         }
 9073:     }
 9074:     return %groups;
 9075: }
 9076: 
 9077: sub get_group_membership {
 9078:     my ($cdom,$cnum,$group) = @_;
 9079:     return(&dump('groupmembership',$cdom,$cnum,$group));
 9080: }
 9081: 
 9082: sub get_users_groups {
 9083:     my ($udom,$uname,$courseid) = @_;
 9084:     my @usersgroups;
 9085:     my $cachetime=1800;
 9086: 
 9087:     my $hashid="$udom:$uname:$courseid";
 9088:     my ($grouplist,$cached)=&is_cached_new('getgroups',$hashid);
 9089:     if (defined($cached)) {
 9090:         @usersgroups = split(/:/,$grouplist);
 9091:     } else {  
 9092:         $grouplist = '';
 9093:         my $courseurl = &courseid_to_courseurl($courseid);
 9094:         my %roleshash = &dump('roles',$udom,$uname,$courseurl);
 9095:         my $access_end = $env{'course.'.$courseid.
 9096:                               '.default_enrollment_end_date'};
 9097:         my $now = time;
 9098:         foreach my $key (keys(%roleshash)) {
 9099:             if ($key =~ /^\Q$courseurl\E\/(\w+)\_gr$/) {
 9100:                 my $group = $1;
 9101:                 if ($roleshash{$key} =~ /_(\d+)_(\d+)$/) {
 9102:                     my $start = $2;
 9103:                     my $end = $1;
 9104:                     if ($start == -1) { next; } # deleted from group
 9105:                     if (($start!=0) && ($start>$now)) { next; }
 9106:                     if (($end!=0) && ($end<$now)) {
 9107:                         if ($access_end && $access_end < $now) {
 9108:                             if ($access_end - $end < 86400) {
 9109:                                 push(@usersgroups,$group);
 9110:                             }
 9111:                         }
 9112:                         next;
 9113:                     }
 9114:                     push(@usersgroups,$group);
 9115:                 }
 9116:             }
 9117:         }
 9118:         @usersgroups = &sort_course_groups($courseid,@usersgroups);
 9119:         $grouplist = join(':',@usersgroups);
 9120:         &do_cache_new('getgroups',$hashid,$grouplist,$cachetime);
 9121:     }
 9122:     return @usersgroups;
 9123: }
 9124: 
 9125: sub devalidate_getgroups_cache {
 9126:     my ($udom,$uname,$cdom,$cnum)=@_;
 9127:     my $courseid = $cdom.'_'.$cnum;
 9128: 
 9129:     my $hashid="$udom:$uname:$courseid";
 9130:     &devalidate_cache_new('getgroups',$hashid);
 9131: }
 9132: 
 9133: # ------------------------------------------------------------------ Plain Text
 9134: 
 9135: sub plaintext {
 9136:     my ($short,$type,$cid,$forcedefault) = @_;
 9137:     if ($short =~ m{^cr/}) {
 9138: 	return (split('/',$short))[-1];
 9139:     }
 9140:     if (!defined($cid)) {
 9141:         $cid = $env{'request.course.id'};
 9142:     }
 9143:     my %rolenames = (
 9144:                       Course    => 'std',
 9145:                       Community => 'alt1',
 9146:                       Placement => 'std',
 9147:                     );
 9148:     if ($cid ne '') {
 9149:         if ($env{'course.'.$cid.'.'.$short.'.plaintext'} ne '') {
 9150:             unless ($forcedefault) {
 9151:                 my $roletext = $env{'course.'.$cid.'.'.$short.'.plaintext'}; 
 9152:                 &Apache::lonlocal::mt_escape(\$roletext);
 9153:                 return &Apache::lonlocal::mt($roletext);
 9154:             }
 9155:         }
 9156:     }
 9157:     if ((defined($type)) && (defined($rolenames{$type})) &&
 9158:         (defined($rolenames{$type})) && 
 9159:         (defined($prp{$short}{$rolenames{$type}}))) {
 9160:         return &Apache::lonlocal::mt($prp{$short}{$rolenames{$type}});
 9161:     } elsif ($cid ne '') {
 9162:         my $crstype = $env{'course.'.$cid.'.type'};
 9163:         if (($crstype ne '') && (defined($rolenames{$crstype})) &&
 9164:             (defined($prp{$short}{$rolenames{$crstype}}))) {
 9165:             return &Apache::lonlocal::mt($prp{$short}{$rolenames{$crstype}});
 9166:         }
 9167:     }
 9168:     return &Apache::lonlocal::mt($prp{$short}{'std'});
 9169: }
 9170: 
 9171: # ----------------------------------------------------------------- Assign Role
 9172: 
 9173: sub assignrole {
 9174:     my ($udom,$uname,$url,$role,$end,$start,$deleteflag,$selfenroll,
 9175:         $context)=@_;
 9176:     my $mrole;
 9177:     if ($role =~ /^cr\//) {
 9178:         my $cwosec=$url;
 9179:         $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
 9180: 	unless (&allowed('ccr',$cwosec)) {
 9181:            my $refused = 1;
 9182:            if ($context eq 'requestcourses') {
 9183:                if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '')) {
 9184:                    if ($role =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
 9185:                        if (($1 eq $env{'user.domain'}) && ($2 eq $env{'user.name'})) {
 9186:                            my ($cdom,$cnum) = ($cwosec =~ m{^/?($match_domain)/($match_courseid)$});
 9187:                            my %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
 9188:                            if ($crsenv{'internal.courseowner'} eq
 9189:                                $env{'user.name'}.':'.$env{'user.domain'}) {
 9190:                                $refused = '';
 9191:                            }
 9192:                        }
 9193:                    }
 9194:                }
 9195:            }
 9196:            if ($refused) {
 9197:                &logthis('Refused custom assignrole: '.
 9198:                         $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.
 9199:                         ' by '.$env{'user.name'}.' at '.$env{'user.domain'});
 9200:                return 'refused';
 9201:            }
 9202:         }
 9203:         $mrole='cr';
 9204:     } elsif ($role =~ /^gr\//) {
 9205:         my $cwogrp=$url;
 9206:         $cwogrp=~s{^/($match_domain)/($match_courseid)/.*}{$1/$2};
 9207:         unless (&allowed('mdg',$cwogrp)) {
 9208:             &logthis('Refused group assignrole: '.
 9209:               $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
 9210:                     $env{'user.name'}.' at '.$env{'user.domain'});
 9211:             return 'refused';
 9212:         }
 9213:         $mrole='gr';
 9214:     } else {
 9215:         my $cwosec=$url;
 9216:         $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
 9217:         if (!(&allowed('c'.$role,$cwosec)) && !(&allowed('c'.$role,$udom))) {
 9218:             my $refused;
 9219:             if (($env{'request.course.sec'}  ne '') && ($role eq 'st')) {
 9220:                 if (!(&allowed('c'.$role,$url))) {
 9221:                     $refused = 1;
 9222:                 }
 9223:             } else {
 9224:                 $refused = 1;
 9225:             }
 9226:             if ($refused) {
 9227:                 my ($cdom,$cnum) = ($cwosec =~ m{^/?($match_domain)/($match_courseid)$});
 9228:                 if (!$selfenroll && $context eq 'course') {
 9229:                     my %crsenv;
 9230:                     if ($role eq 'cc' || $role eq 'co') {
 9231:                         %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
 9232:                         if (($role eq 'cc') && ($cnum !~ /^$match_community$/)) {
 9233:                             if ($env{'request.role'} eq 'cc./'.$cdom.'/'.$cnum) {
 9234:                                 if ($crsenv{'internal.courseowner'} eq 
 9235:                                     $env{'user.name'}.':'.$env{'user.domain'}) {
 9236:                                     $refused = '';
 9237:                                 }
 9238:                             }
 9239:                         } elsif (($role eq 'co') && ($cnum =~ /^$match_community$/)) { 
 9240:                             if ($env{'request.role'} eq 'co./'.$cdom.'/'.$cnum) {
 9241:                                 if ($crsenv{'internal.courseowner'} eq 
 9242:                                     $env{'user.name'}.':'.$env{'user.domain'}) {
 9243:                                     $refused = '';
 9244:                                 }
 9245:                             }
 9246:                         }
 9247:                     }
 9248:                 } elsif (($selfenroll == 1) && ($role eq 'st') && ($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
 9249:                     $refused = '';
 9250:                 } elsif ($context eq 'requestcourses') {
 9251:                     my @possroles = ('st','ta','ep','in','cc','co');
 9252:                     if ((grep(/^\Q$role\E$/,@possroles)) && ($env{'user.name'} ne '' && $env{'user.domain'} ne '')) {
 9253:                         my $wrongcc;
 9254:                         if ($cnum =~ /^$match_community$/) {
 9255:                             $wrongcc = 1 if ($role eq 'cc');
 9256:                         } else {
 9257:                             $wrongcc = 1 if ($role eq 'co');
 9258:                         }
 9259:                         unless ($wrongcc) {
 9260:                             my %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
 9261:                             if ($crsenv{'internal.courseowner'} eq 
 9262:                                  $env{'user.name'}.':'.$env{'user.domain'}) {
 9263:                                 $refused = '';
 9264:                             }
 9265:                         }
 9266:                     }
 9267:                 } elsif ($context eq 'requestauthor') {
 9268:                     if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'}) && 
 9269:                         ($url eq '/'.$udom.'/') && ($role eq 'au')) {
 9270:                         if ($env{'environment.requestauthor'} eq 'automatic') {
 9271:                             $refused = '';
 9272:                         } else {
 9273:                             my %domdefaults = &get_domain_defaults($udom);
 9274:                             if (ref($domdefaults{'requestauthor'}) eq 'HASH') {
 9275:                                 my $checkbystatus;
 9276:                                 if ($env{'user.adv'}) { 
 9277:                                     my $disposition = $domdefaults{'requestauthor'}{'_LC_adv'};
 9278:                                     if ($disposition eq 'automatic') {
 9279:                                         $refused = '';
 9280:                                     } elsif ($disposition eq '') {
 9281:                                         $checkbystatus = 1;
 9282:                                     } 
 9283:                                 } else {
 9284:                                     $checkbystatus = 1;
 9285:                                 }
 9286:                                 if ($checkbystatus) {
 9287:                                     if ($env{'environment.inststatus'}) {
 9288:                                         my @inststatuses = split(/,/,$env{'environment.inststatus'});
 9289:                                         foreach my $type (@inststatuses) {
 9290:                                             if (($type ne '') &&
 9291:                                                 ($domdefaults{'requestauthor'}{$type} eq 'automatic')) {
 9292:                                                 $refused = '';
 9293:                                             }
 9294:                                         }
 9295:                                     } elsif ($domdefaults{'requestauthor'}{'default'} eq 'automatic') {
 9296:                                         $refused = '';
 9297:                                     }
 9298:                                 }
 9299:                             }
 9300:                         }
 9301:                     }
 9302:                 }
 9303:                 if ($refused) {
 9304:                     &logthis('Refused assignrole: '.$udom.' '.$uname.' '.$url.
 9305:                              ' '.$role.' '.$end.' '.$start.' by '.
 9306: 	  	             $env{'user.name'}.' at '.$env{'user.domain'});
 9307:                     return 'refused';
 9308:                 }
 9309:             }
 9310:         } elsif ($role eq 'au') {
 9311:             if ($url ne '/'.$udom.'/') {
 9312:                 &logthis('Attempt by '.$env{'user.name'}.':'.$env{'user.domain'}.
 9313:                          ' to assign author role for '.$uname.':'.$udom.
 9314:                          ' in domain: '.$url.' refused (wrong domain).');
 9315:                 return 'refused';
 9316:             }
 9317:         }
 9318:         $mrole=$role;
 9319:     }
 9320:     my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
 9321:                 "$udom:$uname:$url".'_'."$mrole=$role";
 9322:     if ($end) { $command.='_'.$end; }
 9323:     if ($start) {
 9324: 	if ($end) { 
 9325:            $command.='_'.$start; 
 9326:         } else {
 9327:            $command.='_0_'.$start;
 9328:         }
 9329:     }
 9330:     my $origstart = $start;
 9331:     my $origend = $end;
 9332:     my $delflag;
 9333: # actually delete
 9334:     if ($deleteflag) {
 9335: 	if ((&allowed('dro',$udom)) || (&allowed('dro',$url))) {
 9336: # modify command to delete the role
 9337:            $command="encrypt:rolesdel:$env{'user.domain'}:$env{'user.name'}:".
 9338:                 "$udom:$uname:$url".'_'."$mrole";
 9339: 	   &logthis("$env{'user.name'} at $env{'user.domain'} deletes $mrole in $url for $uname at $udom"); 
 9340: # set start and finish to negative values for userrolelog
 9341:            $start=-1;
 9342:            $end=-1;
 9343:            $delflag = 1;
 9344:         }
 9345:     }
 9346: # send command
 9347:     my $answer=&reply($command,&homeserver($uname,$udom));
 9348: # log new user role if status is ok
 9349:     if ($answer eq 'ok') {
 9350: 	&userrolelog($role,$uname,$udom,$url,$start,$end);
 9351:         if (($role eq 'cc') || ($role eq 'in') ||
 9352:             ($role eq 'ep') || ($role eq 'ad') ||
 9353:             ($role eq 'ta') || ($role eq 'st') ||
 9354:             ($role=~/^cr/) || ($role eq 'gr') ||
 9355:             ($role eq 'co')) {
 9356: # for course roles, perform group memberships changes triggered by role change.
 9357:             unless ($role =~ /^gr/) {
 9358:                 &Apache::longroup::group_changes($udom,$uname,$url,$role,$origend,
 9359:                                                  $origstart,$selfenroll,$context);
 9360:             }
 9361:             &courserolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
 9362:                            $selfenroll,$context);
 9363:         } elsif (($role eq 'li') || ($role eq 'dg') || ($role eq 'sc') ||
 9364:                  ($role eq 'au') || ($role eq 'dc') || ($role eq 'dh') ||
 9365:                  ($role eq 'da')) {
 9366:             &domainrolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
 9367:                            $context);
 9368:         } elsif (($role eq 'ca') || ($role eq 'aa')) {
 9369:             &coauthorrolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
 9370:                              $context); 
 9371:         }
 9372:         if ($role eq 'cc') {
 9373:             &autoupdate_coowners($url,$end,$start,$uname,$udom);
 9374:         }
 9375:     }
 9376:     return $answer;
 9377: }
 9378: 
 9379: sub autoupdate_coowners {
 9380:     my ($url,$end,$start,$uname,$udom) = @_;
 9381:     my ($cdom,$cnum) = ($url =~ m{^/($match_domain)/($match_courseid)});
 9382:     if (($cdom ne '') && ($cnum ne '')) {
 9383:         my $now = time;
 9384:         my %domdesign = &Apache::loncommon::get_domainconf($cdom);
 9385:         if ($domdesign{$cdom.'.autoassign.co-owners'}) {
 9386:             my %coursehash = &coursedescription($cdom.'_'.$cnum);
 9387:             my $instcode = $coursehash{'internal.coursecode'};
 9388:             if ($instcode ne '') {
 9389:                 if (($start && $start <= $now) && ($end == 0) || ($end > $now)) {
 9390:                     unless ($coursehash{'internal.courseowner'} eq $uname.':'.$udom) {
 9391:                         my ($delcoowners,@newcoowners,$putresult,$delresult,$coowners);
 9392:                         my ($result,$desc) = &auto_validate_instcode($cnum,$cdom,$instcode,$uname.':'.$udom);
 9393:                         if ($result eq 'valid') {
 9394:                             if ($coursehash{'internal.co-owners'}) {
 9395:                                 foreach my $coowner (split(',',$coursehash{'internal.co-owners'})) {
 9396:                                     push(@newcoowners,$coowner);
 9397:                                 }
 9398:                                 unless (grep(/^\Q$uname\E:\Q$udom\E$/,@newcoowners)) {
 9399:                                     push(@newcoowners,$uname.':'.$udom);
 9400:                                 }
 9401:                                 @newcoowners = sort(@newcoowners);
 9402:                             } else {
 9403:                                 push(@newcoowners,$uname.':'.$udom);
 9404:                             }
 9405:                         } else {
 9406:                             if ($coursehash{'internal.co-owners'}) {
 9407:                                 foreach my $coowner (split(',',$coursehash{'internal.co-owners'})) {
 9408:                                     unless ($coowner eq $uname.':'.$udom) {
 9409:                                         push(@newcoowners,$coowner);
 9410:                                     }
 9411:                                 }
 9412:                                 unless (@newcoowners > 0) {
 9413:                                     $delcoowners = 1;
 9414:                                     $coowners = '';
 9415:                                 }
 9416:                             }
 9417:                         }
 9418:                         if (@newcoowners || $delcoowners) {
 9419:                             &store_coowners($cdom,$cnum,$coursehash{'home'},
 9420:                                             $delcoowners,@newcoowners);
 9421:                         }
 9422:                     }
 9423:                 }
 9424:             }
 9425:         }
 9426:     }
 9427: }
 9428: 
 9429: sub store_coowners {
 9430:     my ($cdom,$cnum,$chome,$delcoowners,@newcoowners) = @_;
 9431:     my $cid = $cdom.'_'.$cnum;
 9432:     my ($coowners,$delresult,$putresult);
 9433:     if (@newcoowners) {
 9434:         $coowners = join(',',@newcoowners);
 9435:         my %coownershash = (
 9436:                             'internal.co-owners' => $coowners,
 9437:                            );
 9438:         $putresult = &put('environment',\%coownershash,$cdom,$cnum);
 9439:         if ($putresult eq 'ok') {
 9440:             if ($env{'course.'.$cid.'.num'} eq $cnum) {
 9441:                 &appenv({'course.'.$cid.'.internal.co-owners' => $coowners});
 9442:             }
 9443:         }
 9444:     }
 9445:     if ($delcoowners) {
 9446:         $delresult = &Apache::lonnet::del('environment',['internal.co-owners'],$cdom,$cnum);
 9447:         if ($delresult eq 'ok') {
 9448:             if ($env{'course.'.$cid.'.internal.co-owners'}) {
 9449:                 &Apache::lonnet::delenv('course.'.$cid.'.internal.co-owners');
 9450:             }
 9451:         }
 9452:     }
 9453:     if (($putresult eq 'ok') || ($delresult eq 'ok')) {
 9454:         my %crsinfo =
 9455:             &Apache::lonnet::courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
 9456:         if (ref($crsinfo{$cid}) eq 'HASH') {
 9457:             $crsinfo{$cid}{'co-owners'} = \@newcoowners;
 9458:             my $cidput = &Apache::lonnet::courseidput($cdom,\%crsinfo,$chome,'notime');
 9459:         }
 9460:     }
 9461: }
 9462: 
 9463: # -------------------------------------------------- Modify user authentication
 9464: # Overrides without validation
 9465: 
 9466: sub modifyuserauth {
 9467:     my ($udom,$uname,$umode,$upass)=@_;
 9468:     my $uhome=&homeserver($uname,$udom);
 9469:     unless (&allowed('mau',$udom)) { return 'refused'; }
 9470:     &logthis('Call to modify user authentication '.$udom.', '.$uname.', '.
 9471:              $umode.' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
 9472:              ' in domain '.$env{'request.role.domain'});  
 9473:     my $reply=&reply('encrypt:changeuserauth:'.$udom.':'.$uname.':'.$umode.':'.
 9474: 		     &escape($upass),$uhome);
 9475:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
 9476:         'Authentication changed for '.$udom.', '.$uname.', '.$umode.
 9477:          '(Remote '.$ENV{'REMOTE_ADDR'}.'): '.$reply);
 9478:     &log($udom,,$uname,$uhome,
 9479:         'Authentication changed by '.$env{'user.domain'}.', '.
 9480:                                      $env{'user.name'}.', '.$umode.
 9481:          '(Remote '.$ENV{'REMOTE_ADDR'}.'): '.$reply);
 9482:     unless ($reply eq 'ok') {
 9483:         &logthis('Authentication mode error: '.$reply);
 9484: 	return 'error: '.$reply;
 9485:     }   
 9486:     return 'ok';
 9487: }
 9488: 
 9489: # --------------------------------------------------------------- Modify a user
 9490: 
 9491: sub modifyuser {
 9492:     my ($udom,    $uname, $uid,
 9493:         $umode,   $upass, $first,
 9494:         $middle,  $last,  $gene,
 9495:         $forceid, $desiredhome, $email, $inststatus, $candelete)=@_;
 9496:     $udom= &LONCAPA::clean_domain($udom);
 9497:     $uname=&LONCAPA::clean_username($uname);
 9498:     my $showcandelete = 'none';
 9499:     if (ref($candelete) eq 'ARRAY') {
 9500:         if (@{$candelete} > 0) {
 9501:             $showcandelete = join(', ',@{$candelete});
 9502:         }
 9503:     }
 9504:     &logthis('Call to modify user '.$udom.', '.$uname.', '.$uid.', '.
 9505:              $umode.', '.$first.', '.$middle.', '.
 9506: 	     $last.', '.$gene.'(forceid: '.$forceid.'; candelete: '.$showcandelete.')'.
 9507:              (defined($desiredhome) ? ' desiredhome = '.$desiredhome :
 9508:                                      ' desiredhome not specified'). 
 9509:              ' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
 9510:              ' in domain '.$env{'request.role.domain'});
 9511:     my $uhome=&homeserver($uname,$udom,'true');
 9512:     my $newuser;
 9513:     if ($uhome eq 'no_host') {
 9514:         $newuser = 1;
 9515:     }
 9516: # ----------------------------------------------------------------- Create User
 9517:     if (($uhome eq 'no_host') && 
 9518: 	(($umode && $upass) || ($umode eq 'localauth'))) {
 9519:         my $unhome='';
 9520:         if (defined($desiredhome) && &host_domain($desiredhome) eq $udom) { 
 9521:             $unhome = $desiredhome;
 9522: 	} elsif($env{'course.'.$env{'request.course.id'}.'.domain'} eq $udom) {
 9523: 	    $unhome=$env{'course.'.$env{'request.course.id'}.'.home'};
 9524:         } else { # load balancing routine for determining $unhome
 9525:             my $loadm=10000000;
 9526: 	    my %servers = &get_servers($udom,'library');
 9527: 	    foreach my $tryserver (keys(%servers)) {
 9528: 		my $answer=reply('load',$tryserver);
 9529: 		if (($answer=~/\d+/) && ($answer<$loadm)) {
 9530: 		    $loadm=$answer;
 9531: 		    $unhome=$tryserver;
 9532: 		}
 9533: 	    }
 9534:         }
 9535:         if (($unhome eq '') || ($unhome eq 'no_host')) {
 9536: 	    return 'error: unable to find a home server for '.$uname.
 9537:                    ' in domain '.$udom;
 9538:         }
 9539:         my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':'.$umode.':'.
 9540:                          &escape($upass),$unhome);
 9541: 	unless ($reply eq 'ok') {
 9542:             return 'error: '.$reply;
 9543:         }   
 9544:         $uhome=&homeserver($uname,$udom,'true');
 9545:         if (($uhome eq '') || ($uhome eq 'no_host') || ($uhome ne $unhome)) {
 9546: 	    return 'error: unable verify users home machine.';
 9547:         }
 9548:     }   # End of creation of new user
 9549: # ---------------------------------------------------------------------- Add ID
 9550:     if ($uid) {
 9551:        $uid=~tr/A-Z/a-z/;
 9552:        my %uidhash=&idrget($udom,$uname);
 9553:        if (($uidhash{$uname}) && ($uidhash{$uname}!~/error\:/) 
 9554:          && (!$forceid)) {
 9555: 	  unless ($uid eq $uidhash{$uname}) {
 9556: 	      return 'error: user id "'.$uid.'" does not match '.
 9557:                   'current user id "'.$uidhash{$uname}.'".';
 9558:           }
 9559:        } else {
 9560: 	  &idput($udom,{$uname => $uid},$uhome,'ids');
 9561:        }
 9562:     }
 9563: # -------------------------------------------------------------- Add names, etc
 9564:     my @tmp=&get('environment',
 9565: 		   ['firstname','middlename','lastname','generation','id',
 9566:                     'permanentemail','inststatus'],
 9567: 		   $udom,$uname);
 9568:     my (%names,%oldnames);
 9569:     if ($tmp[0] =~ m/^error:.*/) { 
 9570:         %names=(); 
 9571:     } else {
 9572:         %names = @tmp;
 9573:         %oldnames = %names;
 9574:     }
 9575: #
 9576: # If name, email and/or uid are blank (e.g., because an uploaded file
 9577: # of users did not contain them), do not overwrite existing values
 9578: # unless field is in $candelete array ref.  
 9579: #
 9580: 
 9581:     my @fields = ('firstname','middlename','lastname','generation',
 9582:                   'permanentemail','id');
 9583:     my %newvalues;
 9584:     if (ref($candelete) eq 'ARRAY') {
 9585:         foreach my $field (@fields) {
 9586:             if (grep(/^\Q$field\E$/,@{$candelete})) {
 9587:                 if ($field eq 'firstname') {
 9588:                     $names{$field} = $first;
 9589:                 } elsif ($field eq 'middlename') {
 9590:                     $names{$field} = $middle;
 9591:                 } elsif ($field eq 'lastname') {
 9592:                     $names{$field} = $last;
 9593:                 } elsif ($field eq 'generation') { 
 9594:                     $names{$field} = $gene;
 9595:                 } elsif ($field eq 'permanentemail') {
 9596:                     $names{$field} = $email;
 9597:                 } elsif ($field eq 'id') {
 9598:                     $names{$field}  = $uid;
 9599:                 }
 9600:             }
 9601:         }
 9602:     }
 9603:     if ($first)  { $names{'firstname'}  = $first; }
 9604:     if (defined($middle)) { $names{'middlename'} = $middle; }
 9605:     if ($last)   { $names{'lastname'}   = $last; }
 9606:     if (defined($gene))   { $names{'generation'} = $gene; }
 9607:     if ($email) {
 9608:        $email=~s/[^\w\@\.\-\,]//gs;
 9609:        if ($email=~/\@/) { $names{'permanentemail'} = $email; }
 9610:     }
 9611:     if ($uid) { $names{'id'}  = $uid; }
 9612:     if (defined($inststatus)) {
 9613:         $names{'inststatus'} = '';
 9614:         my ($usertypes,$typesorder) = &retrieve_inst_usertypes($udom);
 9615:         if (ref($usertypes) eq 'HASH') {
 9616:             my @okstatuses; 
 9617:             foreach my $item (split(/:/,$inststatus)) {
 9618:                 if (defined($usertypes->{$item})) {
 9619:                     push(@okstatuses,$item);  
 9620:                 }
 9621:             }
 9622:             if (@okstatuses) {
 9623:                 $names{'inststatus'} = join(':', map { &escape($_); } @okstatuses);
 9624:             }
 9625:         }
 9626:     }
 9627:     my $logmsg = $udom.', '.$uname.', '.$uid.', '.
 9628:                  $umode.', '.$first.', '.$middle.', '.
 9629:                  $last.', '.$gene.', '.$email.', '.$inststatus;
 9630:     if ($env{'user.name'} ne '' && $env{'user.domain'}) {
 9631:         $logmsg .= ' by '.$env{'user.name'}.' at '.$env{'user.domain'};
 9632:     } else {
 9633:         $logmsg .= ' during self creation';
 9634:     }
 9635:     my $changed;
 9636:     if ($newuser) {
 9637:         $changed = 1;
 9638:     } else {
 9639:         foreach my $field (@fields) {
 9640:             if ($names{$field} ne $oldnames{$field}) {
 9641:                 $changed = 1;
 9642:                 last;
 9643:             }
 9644:         }
 9645:     }
 9646:     unless ($changed) {
 9647:         $logmsg = 'No changes in user information needed for: '.$logmsg;
 9648:         &logthis($logmsg);
 9649:         return 'ok';
 9650:     }
 9651:     my $reply = &put('environment', \%names, $udom,$uname);
 9652:     if ($reply ne 'ok') { 
 9653:         return 'error: '.$reply;
 9654:     }
 9655:     if ($names{'permanentemail'} ne $oldnames{'permanentemail'}) {
 9656:         &Apache::lonnet::devalidate_cache_new('emailscache',$uname.':'.$udom);
 9657:     }
 9658:     my $sqlresult = &update_allusers_table($uname,$udom,\%names);
 9659:     &devalidate_cache_new('namescache',$uname.':'.$udom);
 9660:     $logmsg = 'Success modifying user '.$logmsg;
 9661:     &logthis($logmsg);
 9662:     return 'ok';
 9663: }
 9664: 
 9665: # -------------------------------------------------------------- Modify student
 9666: 
 9667: sub modifystudent {
 9668:     my ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$usec,
 9669:         $end,$start,$forceid,$desiredhome,$email,$type,$locktype,$cid,
 9670:         $selfenroll,$context,$inststatus,$credits,$instsec)=@_;
 9671:     if (!$cid) {
 9672: 	unless ($cid=$env{'request.course.id'}) {
 9673: 	    return 'not_in_class';
 9674: 	}
 9675:     }
 9676: # --------------------------------------------------------------- Make the user
 9677:     my $reply=&modifyuser
 9678: 	($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$forceid,
 9679:          $desiredhome,$email,$inststatus);
 9680:     unless ($reply eq 'ok') { return $reply; }
 9681:     # This will cause &modify_student_enrollment to get the uid from the
 9682:     # student's environment
 9683:     $uid = undef if (!$forceid);
 9684:     $reply = &modify_student_enrollment($udom,$uname,$uid,$first,$middle,$last,
 9685:                                         $gene,$usec,$end,$start,$type,$locktype,
 9686:                                         $cid,$selfenroll,$context,$credits,$instsec);
 9687:     return $reply;
 9688: }
 9689: 
 9690: sub modify_student_enrollment {
 9691:     my ($udom,$uname,$uid,$first,$middle,$last,$gene,$usec,$end,$start,$type,
 9692:         $locktype,$cid,$selfenroll,$context,$credits,$instsec) = @_;
 9693:     my ($cdom,$cnum,$chome);
 9694:     if (!$cid) {
 9695: 	unless ($cid=$env{'request.course.id'}) {
 9696: 	    return 'not_in_class';
 9697: 	}
 9698: 	$cdom=$env{'course.'.$cid.'.domain'};
 9699: 	$cnum=$env{'course.'.$cid.'.num'};
 9700:     } else {
 9701: 	($cdom,$cnum)=split(/_/,$cid);
 9702:     }
 9703:     $chome=$env{'course.'.$cid.'.home'};
 9704:     if (!$chome) {
 9705: 	$chome=&homeserver($cnum,$cdom);
 9706:     }
 9707:     if (!$chome) { return 'unknown_course'; }
 9708:     # Make sure the user exists
 9709:     my $uhome=&homeserver($uname,$udom);
 9710:     if (($uhome eq '') || ($uhome eq 'no_host')) { 
 9711: 	return 'error: no such user';
 9712:     }
 9713:     # Get student data if we were not given enough information
 9714:     if (!defined($first)  || $first  eq '' || 
 9715:         !defined($last)   || $last   eq '' || 
 9716:         !defined($uid)    || $uid    eq '' || 
 9717:         !defined($middle) || $middle eq '' || 
 9718:         !defined($gene)   || $gene   eq '') {
 9719:         # They did not supply us with enough data to enroll the student, so
 9720:         # we need to pick up more information.
 9721:         my %tmp = &get('environment',
 9722:                        ['firstname','middlename','lastname', 'generation','id']
 9723:                        ,$udom,$uname);
 9724: 
 9725:         #foreach my $key (keys(%tmp)) {
 9726:         #    &logthis("key $key = ".$tmp{$key});
 9727:         #}
 9728:         $first  = $tmp{'firstname'}  if (!defined($first)  || $first  eq '');
 9729:         $middle = $tmp{'middlename'} if (!defined($middle) || $middle eq '');
 9730:         $last   = $tmp{'lastname'}   if (!defined($last)   || $last eq '');
 9731:         $gene   = $tmp{'generation'} if (!defined($gene)   || $gene eq '');
 9732:         $uid    = $tmp{'id'}         if (!defined($uid)    || $uid  eq '');
 9733:     }
 9734:     my $fullname = &format_name($first,$middle,$last,$gene,'lastname');
 9735:     my $user = "$uname:$udom";
 9736:     my %old_entry = &Apache::lonnet::get('classlist',[$user],$cdom,$cnum);
 9737:     my $reply=cput('classlist',
 9738: 		   {$user => 
 9739: 			join(':',$end,$start,$uid,$usec,$fullname,$type,$locktype,$credits,$instsec) },
 9740: 		   $cdom,$cnum);
 9741:     if (($reply eq 'ok') || ($reply eq 'delayed')) {
 9742:         &devalidate_getsection_cache($udom,$uname,$cid);
 9743:     } else { 
 9744: 	return 'error: '.$reply;
 9745:     }
 9746:     # Add student role to user
 9747:     my $uurl='/'.$cid;
 9748:     $uurl=~s/\_/\//g;
 9749:     if ($usec) {
 9750: 	$uurl.='/'.$usec;
 9751:     }
 9752:     my $result = &assignrole($udom,$uname,$uurl,'st',$end,$start,undef,
 9753:                              $selfenroll,$context);
 9754:     if ($result ne 'ok') {
 9755:         if ($old_entry{$user} ne '') {
 9756:             $reply = &cput('classlist',\%old_entry,$cdom,$cnum);
 9757:         } else {
 9758:             $reply = &del('classlist',[$user],$cdom,$cnum);
 9759:         }
 9760:     }
 9761:     return $result; 
 9762: }
 9763: 
 9764: sub format_name {
 9765:     my ($firstname,$middlename,$lastname,$generation,$first)=@_;
 9766:     my $name;
 9767:     if ($first ne 'lastname') {
 9768: 	$name=$firstname.' '.$middlename.' '.$lastname.' '.$generation;
 9769:     } else {
 9770: 	if ($lastname=~/\S/) {
 9771: 	    $name.= $lastname.' '.$generation.', '.$firstname.' '.$middlename;
 9772: 	    $name=~s/\s+,/,/;
 9773: 	} else {
 9774: 	    $name.= $firstname.' '.$middlename.' '.$generation;
 9775: 	}
 9776:     }
 9777:     $name=~s/^\s+//;
 9778:     $name=~s/\s+$//;
 9779:     $name=~s/\s+/ /g;
 9780:     return $name;
 9781: }
 9782: 
 9783: # ------------------------------------------------- Write to course preferences
 9784: 
 9785: sub writecoursepref {
 9786:     my ($courseid,%prefs)=@_;
 9787:     $courseid=~s/^\///;
 9788:     $courseid=~s/\_/\//g;
 9789:     my ($cdomain,$cnum)=split(/\//,$courseid);
 9790:     my $chome=homeserver($cnum,$cdomain);
 9791:     if (($chome eq '') || ($chome eq 'no_host')) { 
 9792: 	return 'error: no such course';
 9793:     }
 9794:     my $cstring='';
 9795:     foreach my $pref (keys(%prefs)) {
 9796: 	$cstring.=&escape($pref).'='.&escape($prefs{$pref}).'&';
 9797:     }
 9798:     $cstring=~s/\&$//;
 9799:     return reply('put:'.$cdomain.':'.$cnum.':environment:'.$cstring,$chome);
 9800: }
 9801: 
 9802: # ---------------------------------------------------------- Make/modify course
 9803: 
 9804: sub createcourse {
 9805:     my ($udom,$description,$url,$course_server,$nonstandard,$inst_code,
 9806:         $course_owner,$crstype,$cnum,$context,$category)=@_;
 9807:     $url=&declutter($url);
 9808:     my $cid='';
 9809:     if ($context eq 'requestcourses') {
 9810:         my $can_create = 0;
 9811:         my ($ownername,$ownerdom) = split(':',$course_owner);
 9812:         if ($udom eq $ownerdom) {
 9813:             if (&usertools_access($ownername,$ownerdom,$category,undef,
 9814:                                   $context)) {
 9815:                 $can_create = 1;
 9816:             }
 9817:         } else {
 9818:             my %userenv = &userenvironment($ownerdom,$ownername,'reqcrsotherdom.'.
 9819:                                            $category);
 9820:             if ($userenv{'reqcrsotherdom.'.$category} ne '') {
 9821:                 my @curr = split(',',$userenv{'reqcrsotherdom.'.$category});
 9822:                 if (@curr > 0) {
 9823:                     my @options = qw(approval validate autolimit);
 9824:                     my $optregex = join('|',@options);
 9825:                     if (grep(/^\Q$udom\E:($optregex)(=?\d*)$/,@curr)) {
 9826:                         $can_create = 1;
 9827:                     }
 9828:                 }
 9829:             }
 9830:         }
 9831:         if ($can_create) {
 9832:             unless ($ownername eq $env{'user.name'} && $ownerdom eq $env{'user.domain'}) {
 9833:                 unless (&allowed('ccc',$udom)) {
 9834:                     return 'refused'; 
 9835:                 }
 9836:             }
 9837:         } else {
 9838:             return 'refused';
 9839:         }
 9840:     } elsif (!&allowed('ccc',$udom)) {
 9841:         return 'refused';
 9842:     }
 9843: # --------------------------------------------------------------- Get Unique ID
 9844:     my $uname;
 9845:     if ($cnum =~ /^$match_courseid$/) {
 9846:         my $chome=&homeserver($cnum,$udom,'true');
 9847:         if (($chome eq '') || ($chome eq 'no_host')) {
 9848:             $uname = $cnum;
 9849:         } else {
 9850:             $uname = &generate_coursenum($udom,$crstype);
 9851:         }
 9852:     } else {
 9853:         $uname = &generate_coursenum($udom,$crstype);
 9854:     }
 9855:     return $uname if ($uname =~ /^error/);
 9856: # -------------------------------------------------- Check supplied server name
 9857:     if (!defined($course_server)) {
 9858:         if (defined(&domain($udom,'primary'))) {
 9859:             $course_server = &domain($udom,'primary');
 9860:         } else {
 9861:             $course_server = $env{'user.home'}; 
 9862:         }
 9863:     }
 9864:     my %host_servers =
 9865:         &Apache::lonnet::get_servers($udom,'library');
 9866:     unless ($host_servers{$course_server}) {
 9867:         return 'error: invalid home server for course: '.$course_server;
 9868:     }
 9869: # ------------------------------------------------------------- Make the course
 9870:     my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':none::',
 9871:                       $course_server);
 9872:     unless ($reply eq 'ok') { return 'error: '.$reply; }
 9873:     my $uhome=&homeserver($uname,$udom,'true');
 9874:     if (($uhome eq '') || ($uhome eq 'no_host')) { 
 9875: 	return 'error: no such course';
 9876:     }
 9877: # ----------------------------------------------------------------- Course made
 9878: # log existence
 9879:     my $now = time;
 9880:     my $newcourse = {
 9881:                     $udom.'_'.$uname => {
 9882:                                      description => $description,
 9883:                                      inst_code   => $inst_code,
 9884:                                      owner       => $course_owner,
 9885:                                      type        => $crstype,
 9886:                                      creator     => $env{'user.name'}.':'.
 9887:                                                     $env{'user.domain'},
 9888:                                      created     => $now,
 9889:                                      context     => $context,
 9890:                                                 },
 9891:                     };
 9892:     &courseidput($udom,$newcourse,$uhome,'notime');
 9893: # set toplevel url
 9894:     my $topurl=$url;
 9895:     unless ($nonstandard) {
 9896: # ------------------------------------------ For standard courses, make top url
 9897:         my $mapurl=&clutter($url);
 9898:         if ($mapurl eq '/res/') { $mapurl=''; }
 9899:         $env{'form.initmap'}=(<<ENDINITMAP);
 9900: <map>
 9901: <resource id="1" type="start"></resource>
 9902: <resource id="2" src="$mapurl"></resource>
 9903: <resource id="3" type="finish"></resource>
 9904: <link index="1" from="1" to="2"></link>
 9905: <link index="2" from="2" to="3"></link>
 9906: </map>
 9907: ENDINITMAP
 9908:         $topurl=&declutter(
 9909:         &finishuserfileupload($uname,$udom,'initmap','default.sequence')
 9910:                           );
 9911:     }
 9912: # ----------------------------------------------------------- Write preferences
 9913:     &writecoursepref($udom.'_'.$uname,
 9914:                      ('description'              => $description,
 9915:                       'url'                      => $topurl,
 9916:                       'internal.creator'         => $env{'user.name'}.':'.
 9917:                                                     $env{'user.domain'},
 9918:                       'internal.created'         => $now,
 9919:                       'internal.creationcontext' => $context)
 9920:                     );
 9921:     return '/'.$udom.'/'.$uname;
 9922: }
 9923: 
 9924: # ------------------------------------------------------------------- Create ID
 9925: sub generate_coursenum {
 9926:     my ($udom,$crstype) = @_;
 9927:     my $domdesc = &domain($udom);
 9928:     return 'error: invalid domain' if ($domdesc eq '');
 9929:     my $first;
 9930:     if ($crstype eq 'Community') {
 9931:         $first = '0';
 9932:     } else {
 9933:         $first = int(1+rand(9)); 
 9934:     } 
 9935:     my $uname=$first.
 9936:         ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
 9937:         substr($$.time,0,5).unpack("H8",pack("I32",time)).
 9938:         unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
 9939: # ----------------------------------------------- Make sure that does not exist
 9940:     my $uhome=&homeserver($uname,$udom,'true');
 9941:     unless (($uhome eq '') || ($uhome eq 'no_host')) {
 9942:         if ($crstype eq 'Community') {
 9943:             $first = '0';
 9944:         } else {
 9945:             $first = int(1+rand(9));
 9946:         }
 9947:         $uname=$first.
 9948:                ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
 9949:                substr($$.time,0,5).unpack("H8",pack("I32",time)).
 9950:                unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
 9951:         $uhome=&homeserver($uname,$udom,'true');
 9952:         unless (($uhome eq '') || ($uhome eq 'no_host')) {
 9953:             return 'error: unable to generate unique course-ID';
 9954:         }
 9955:     }
 9956:     return $uname;
 9957: }
 9958: 
 9959: sub is_course {
 9960:     my ($cdom, $cnum) = scalar(@_) == 1 ? 
 9961:          ($_[0] =~ /^($match_domain)_($match_courseid)$/)  :  @_;
 9962: 
 9963:     return unless $cdom and $cnum;
 9964: 
 9965:     my %courses = &courseiddump($cdom, '.', 1, '.', '.', $cnum, undef, undef,
 9966:         '.');
 9967: 
 9968:     return unless(exists($courses{$cdom.'_'.$cnum}));
 9969:     return wantarray ? ($cdom, $cnum) : $cdom.'_'.$cnum;
 9970: }
 9971: 
 9972: sub store_userdata {
 9973:     my ($storehash,$datakey,$namespace,$udom,$uname) = @_;
 9974:     my $result;
 9975:     if ($datakey ne '') {
 9976:         if (ref($storehash) eq 'HASH') {
 9977:             if ($udom eq '' || $uname eq '') {
 9978:                 $udom = $env{'user.domain'};
 9979:                 $uname = $env{'user.name'};
 9980:             }
 9981:             my $uhome=&homeserver($uname,$udom);
 9982:             if (($uhome eq '') || ($uhome eq 'no_host')) {
 9983:                 $result = 'error: no_host';
 9984:             } else {
 9985:                 $storehash->{'ip'} = $ENV{'REMOTE_ADDR'};
 9986:                 $storehash->{'host'} = $perlvar{'lonHostID'};
 9987: 
 9988:                 my $namevalue='';
 9989:                 foreach my $key (keys(%{$storehash})) {
 9990:                     $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
 9991:                 }
 9992:                 $namevalue=~s/\&$//;
 9993:                 unless ($namespace eq 'courserequests') {
 9994:                     $datakey = &escape($datakey);
 9995:                 }
 9996:                 $result =  &reply("store:$udom:$uname:$namespace:$datakey:".
 9997:                                   $namevalue,$uhome);
 9998:             }
 9999:         } else {
10000:             $result = 'error: data to store was not a hash reference'; 
10001:         }
10002:     } else {
10003:         $result= 'error: invalid requestkey'; 
10004:     }
10005:     return $result;
10006: }
10007: 
10008: # ---------------------------------------------------------- Assign Custom Role
10009: 
10010: sub assigncustomrole {
10011:     my ($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start,$deleteflag,$selfenroll,$context)=@_;
10012:     return &assignrole($udom,$uname,$url,'cr/'.$rdom.'/'.$rnam.'/'.$rolename,
10013:                        $end,$start,$deleteflag,$selfenroll,$context);
10014: }
10015: 
10016: # ----------------------------------------------------------------- Revoke Role
10017: 
10018: sub revokerole {
10019:     my ($udom,$uname,$url,$role,$deleteflag,$selfenroll,$context)=@_;
10020:     my $now=time;
10021:     return &assignrole($udom,$uname,$url,$role,$now,undef,$deleteflag,$selfenroll,$context);
10022: }
10023: 
10024: # ---------------------------------------------------------- Revoke Custom Role
10025: 
10026: sub revokecustomrole {
10027:     my ($udom,$uname,$url,$rdom,$rnam,$rolename,$deleteflag,$selfenroll,$context)=@_;
10028:     my $now=time;
10029:     return &assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$now,
10030:            $deleteflag,$selfenroll,$context);
10031: }
10032: 
10033: # ------------------------------------------------------------ Disk usage
10034: sub diskusage {
10035:     my ($udom,$uname,$directorypath,$getpropath)=@_;
10036:     $directorypath =~ s/\/$//;
10037:     my $listing=&reply('du2:'.&escape($directorypath).':'
10038:                        .&escape($getpropath).':'.&escape($uname).':'
10039:                        .&escape($udom),homeserver($uname,$udom));
10040:     if ($listing eq 'unknown_cmd') {
10041:         if ($getpropath) {
10042:             $directorypath = &propath($udom,$uname).'/'.$directorypath; 
10043:         }
10044:         $listing = &reply('du:'.$directorypath,homeserver($uname,$udom));
10045:     }
10046:     return $listing;
10047: }
10048: 
10049: sub is_locked {
10050:     my ($file_name, $domain, $user, $which) = @_;
10051:     my @check;
10052:     my $is_locked;
10053:     push (@check,$file_name);
10054:     my %locked = &get('file_permissions',\@check,
10055: 		      $env{'user.domain'},$env{'user.name'});
10056:     my ($tmp)=keys(%locked);
10057:     if ($tmp=~/^error:/) { undef(%locked); }
10058:     
10059:     if (ref($locked{$file_name}) eq 'ARRAY') {
10060:         $is_locked = 'false';
10061:         foreach my $entry (@{$locked{$file_name}}) {
10062:            if (ref($entry) eq 'ARRAY') {
10063:                $is_locked = 'true';
10064:                if (ref($which) eq 'ARRAY') {
10065:                    push(@{$which},$entry);
10066:                } else {
10067:                    last;
10068:                }
10069:            }
10070:        }
10071:     } else {
10072:         $is_locked = 'false';
10073:     }
10074:     return $is_locked;
10075: }
10076: 
10077: sub declutter_portfile {
10078:     my ($file) = @_;
10079:     $file =~ s{^(/portfolio/|portfolio/)}{/};
10080:     return $file;
10081: }
10082: 
10083: # ------------------------------------------------------------- Mark as Read Only
10084: 
10085: sub mark_as_readonly {
10086:     my ($domain,$user,$files,$what) = @_;
10087:     my %current_permissions = &dump('file_permissions',$domain,$user);
10088:     my ($tmp)=keys(%current_permissions);
10089:     if ($tmp=~/^error:/) { undef(%current_permissions); }
10090:     foreach my $file (@{$files}) {
10091: 	$file = &declutter_portfile($file);
10092:         push(@{$current_permissions{$file}},$what);
10093:     }
10094:     &put('file_permissions',\%current_permissions,$domain,$user);
10095:     return;
10096: }
10097: 
10098: # ------------------------------------------------------------Save Selected Files
10099: 
10100: sub save_selected_files {
10101:     my ($user, $path, @files) = @_;
10102:     my $filename = $user."savedfiles";
10103:     my @other_files = &files_not_in_path($user, $path);
10104:     open (OUT, '>'.$tmpdir.$filename);
10105:     foreach my $file (@files) {
10106:         print (OUT $env{'form.currentpath'}.$file."\n");
10107:     }
10108:     foreach my $file (@other_files) {
10109:         print (OUT $file."\n");
10110:     }
10111:     close (OUT);
10112:     return 'ok';
10113: }
10114: 
10115: sub clear_selected_files {
10116:     my ($user) = @_;
10117:     my $filename = $user."savedfiles";
10118:     open (OUT, '>'.LONCAPA::tempdir().$filename);
10119:     print (OUT undef);
10120:     close (OUT);
10121:     return ("ok");    
10122: }
10123: 
10124: sub files_in_path {
10125:     my ($user, $path) = @_;
10126:     my $filename = $user."savedfiles";
10127:     my %return_files;
10128:     open (IN, '<'.LONCAPA::tempdir().$filename);
10129:     while (my $line_in = <IN>) {
10130:         chomp ($line_in);
10131:         my @paths_and_file = split (m!/!, $line_in);
10132:         my $file_part = pop (@paths_and_file);
10133:         my $path_part = join ('/', @paths_and_file);
10134:         $path_part.='/';
10135:         my $path_and_file = $path_part.$file_part;
10136:         if ($path_part eq $path) {
10137:             $return_files{$file_part}= 'selected';
10138:         }
10139:     }
10140:     close (IN);
10141:     return (\%return_files);
10142: }
10143: 
10144: # called in portfolio select mode, to show files selected NOT in current directory
10145: sub files_not_in_path {
10146:     my ($user, $path) = @_;
10147:     my $filename = $user."savedfiles";
10148:     my @return_files;
10149:     my $path_part;
10150:     open(IN, '<'.LONCAPA::.$filename);
10151:     while (my $line = <IN>) {
10152:         #ok, I know it's clunky, but I want it to work
10153:         my @paths_and_file = split(m|/|, $line);
10154:         my $file_part = pop(@paths_and_file);
10155:         chomp($file_part);
10156:         my $path_part = join('/', @paths_and_file);
10157:         $path_part .= '/';
10158:         my $path_and_file = $path_part.$file_part;
10159:         if ($path_part ne $path) {
10160:             push(@return_files, ($path_and_file));
10161:         }
10162:     }
10163:     close(OUT);
10164:     return (@return_files);
10165: }
10166: 
10167: #------------------------------Submitted/Handedback Portfolio Files Versioning
10168:  
10169: sub portfiles_versioning {
10170:     my ($symb,$domain,$stu_name,$portfiles,$versioned_portfiles) = @_;
10171:     my $portfolio_root = '/userfiles/portfolio';
10172:     return unless ((ref($portfiles) eq 'ARRAY') && (ref($versioned_portfiles) eq 'ARRAY'));
10173:     foreach my $file (@{$portfiles}) {
10174:         &unmark_as_readonly($domain,$stu_name,[$symb,$env{'request.course.id'}],$file);
10175:         my ($directory,$answer_file) =($file =~ /^(.*?)([^\/]*)$/);
10176:         my ($answer_name,$answer_ver,$answer_ext) = &file_name_version_ext($answer_file);
10177:         my $getpropath = 1;
10178:         my ($dir_list,$listerror) = &dirlist($portfolio_root.$directory,$domain,
10179:                                              $stu_name,$getpropath);
10180:         my $version = &get_next_version($answer_name,$answer_ext,$dir_list);
10181:         my $new_answer = 
10182:             &version_selected_portfile($domain,$stu_name,$directory,$answer_file,$version);
10183:         if ($new_answer ne 'problem getting file') {
10184:             push(@{$versioned_portfiles}, $directory.$new_answer);
10185:             &mark_as_readonly($domain,$stu_name,[$directory.$new_answer],
10186:                               [$symb,$env{'request.course.id'},'graded']);
10187:         }
10188:     }
10189: }
10190: 
10191: sub get_next_version {
10192:     my ($answer_name, $answer_ext, $dir_list) = @_;
10193:     my $version;
10194:     if (ref($dir_list) eq 'ARRAY') {
10195:         foreach my $row (@{$dir_list}) {
10196:             my ($file) = split(/\&/,$row,2);
10197:             my ($file_name,$file_version,$file_ext) =
10198:                 &file_name_version_ext($file);
10199:             if (($file_name eq $answer_name) &&
10200:                 ($file_ext eq $answer_ext)) {
10201:                      # gets here if filename and extension match,
10202:                      # regardless of version
10203:                 if ($file_version ne '') {
10204:                     # a versioned file is found  so save it for later
10205:                     if ($file_version > $version) {
10206:                         $version = $file_version;
10207:                     }
10208:                 }
10209:             }
10210:         }
10211:     }
10212:     $version ++;
10213:     return($version);
10214: }
10215: 
10216: sub version_selected_portfile {
10217:     my ($domain,$stu_name,$directory,$file_name,$version) = @_;
10218:     my ($answer_name,$answer_ver,$answer_ext) =
10219:         &file_name_version_ext($file_name);
10220:     my $new_answer;
10221:     $env{'form.copy'} =
10222:         &getfile("/uploaded/$domain/$stu_name/portfolio$directory$file_name");
10223:     if($env{'form.copy'} eq '-1') {
10224:         $new_answer = 'problem getting file';
10225:     } else {
10226:         $new_answer = $answer_name.'.'.$version.'.'.$answer_ext;
10227:         my $copy_result = 
10228:             &finishuserfileupload($stu_name,$domain,'copy',
10229:                                   '/portfolio'.$directory.$new_answer);
10230:     }
10231:     undef($env{'form.copy'});
10232:     return ($new_answer);
10233: }
10234: 
10235: sub file_name_version_ext {
10236:     my ($file)=@_;
10237:     my @file_parts = split(/\./, $file);
10238:     my ($name,$version,$ext);
10239:     if (@file_parts > 1) {
10240:         $ext=pop(@file_parts);
10241:         if (@file_parts > 1 && $file_parts[-1] =~ /^\d+$/) {
10242:             $version=pop(@file_parts);
10243:         }
10244:         $name=join('.',@file_parts);
10245:     } else {
10246:         $name=join('.',@file_parts);
10247:     }
10248:     return($name,$version,$ext);
10249: }
10250: 
10251: #----------------------------------------------Get portfolio file permissions
10252: 
10253: sub get_portfile_permissions {
10254:     my ($domain,$user) = @_;
10255:     my %current_permissions = &dump('file_permissions',$domain,$user);
10256:     my ($tmp)=keys(%current_permissions);
10257:     if ($tmp=~/^error:/) { undef(%current_permissions); }
10258:     return \%current_permissions;
10259: }
10260: 
10261: #---------------------------------------------Get portfolio file access controls
10262: 
10263: sub get_access_controls {
10264:     my ($current_permissions,$group,$file) = @_;
10265:     my %access;
10266:     my $real_file = $file;
10267:     $file =~ s/\.meta$//;
10268:     if (defined($file)) {
10269:         if (ref($$current_permissions{$file."\0".'accesscontrol'}) eq 'HASH') {
10270:             foreach my $control (keys(%{$$current_permissions{$file."\0".'accesscontrol'}})) {
10271:                 $access{$real_file}{$control} = $$current_permissions{$file."\0".$control};
10272:             }
10273:         }
10274:     } else {
10275:         foreach my $key (keys(%{$current_permissions})) {
10276:             if ($key =~ /\0accesscontrol$/) {
10277:                 if (defined($group)) {
10278:                     if ($key !~ m-^\Q$group\E/-) {
10279:                         next;
10280:                     }
10281:                 }
10282:                 my ($fullpath) = split(/\0/,$key);
10283:                 if (ref($$current_permissions{$key}) eq 'HASH') {
10284:                     foreach my $control (keys(%{$$current_permissions{$key}})) {
10285:                         $access{$fullpath}{$control}=$$current_permissions{$fullpath."\0".$control};
10286:                     }
10287:                 }
10288:             }
10289:         }
10290:     }
10291:     return %access;
10292: }
10293: 
10294: sub modify_access_controls {
10295:     my ($file_name,$changes,$domain,$user)=@_;
10296:     my ($outcome,$deloutcome);
10297:     my %store_permissions;
10298:     my %new_values;
10299:     my %new_control;
10300:     my %translation;
10301:     my @deletions = ();
10302:     my $now = time;
10303:     if (exists($$changes{'activate'})) {
10304:         if (ref($$changes{'activate'}) eq 'HASH') {
10305:             my @newitems = sort(keys(%{$$changes{'activate'}}));
10306:             my $numnew = scalar(@newitems);
10307:             for (my $i=0; $i<$numnew; $i++) {
10308:                 my $newkey = $newitems[$i];
10309:                 my $newid = &Apache::loncommon::get_cgi_id();
10310:                 if ($newkey =~ /^\d+:/) { 
10311:                     $newkey =~ s/^(\d+)/$newid/;
10312:                     $translation{$1} = $newid;
10313:                 } elsif ($newkey =~ /^\d+_\d+_\d+:/) {
10314:                     $newkey =~ s/^(\d+_\d+_\d+)/$newid/;
10315:                     $translation{$1} = $newid;
10316:                 }
10317:                 $new_values{$file_name."\0".$newkey} = 
10318:                                           $$changes{'activate'}{$newitems[$i]};
10319:                 $new_control{$newkey} = $now;
10320:             }
10321:         }
10322:     }
10323:     my %todelete;
10324:     my %changed_items;
10325:     foreach my $action ('delete','update') {
10326:         if (exists($$changes{$action})) {
10327:             if (ref($$changes{$action}) eq 'HASH') {
10328:                 foreach my $key (keys(%{$$changes{$action}})) {
10329:                     my ($itemnum) = ($key =~ /^([^:]+):/);
10330:                     if ($action eq 'delete') { 
10331:                         $todelete{$itemnum} = 1;
10332:                     } else {
10333:                         $changed_items{$itemnum} = $key;
10334:                     }
10335:                 }
10336:             }
10337:         }
10338:     }
10339:     # get lock on access controls for file.
10340:     my $lockhash = {
10341:                   $file_name."\0".'locked_access_records' => $env{'user.name'}.
10342:                                                        ':'.$env{'user.domain'},
10343:                    }; 
10344:     my $tries = 0;
10345:     my $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
10346:    
10347:     while (($gotlock ne 'ok') && $tries < 10) {
10348:         $tries ++;
10349:         sleep(0.1);
10350:         $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
10351:     }
10352:     if ($gotlock eq 'ok') {
10353:         my %curr_permissions = &dump('file_permissions',$domain,$user,$file_name);
10354:         my ($tmp)=keys(%curr_permissions);
10355:         if ($tmp=~/^error:/) { undef(%curr_permissions); }
10356:         if (exists($curr_permissions{$file_name."\0".'accesscontrol'})) {
10357:             my $curr_controls = $curr_permissions{$file_name."\0".'accesscontrol'};
10358:             if (ref($curr_controls) eq 'HASH') {
10359:                 foreach my $control_item (keys(%{$curr_controls})) {
10360:                     my ($itemnum) = ($control_item =~ /^([^:]+):/);
10361:                     if (defined($todelete{$itemnum})) {
10362:                         push(@deletions,$file_name."\0".$control_item);
10363:                     } else {
10364:                         if (defined($changed_items{$itemnum})) {
10365:                             $new_control{$changed_items{$itemnum}} = $now;
10366:                             push(@deletions,$file_name."\0".$control_item);
10367:                             $new_values{$file_name."\0".$changed_items{$itemnum}} = $$changes{'update'}{$changed_items{$itemnum}};
10368:                         } else {
10369:                             $new_control{$control_item} = $$curr_controls{$control_item};
10370:                         }
10371:                     }
10372:                 }
10373:             }
10374:         }
10375:         my ($group);
10376:         if (&is_course($domain,$user)) {
10377:             ($group,my $file) = split(/\//,$file_name,2);
10378:         }
10379:         $deloutcome = &del('file_permissions',\@deletions,$domain,$user);
10380:         $new_values{$file_name."\0".'accesscontrol'} = \%new_control;
10381:         $outcome = &put('file_permissions',\%new_values,$domain,$user);
10382:         #  remove lock
10383:         my @del_lock = ($file_name."\0".'locked_access_records');
10384:         my $dellockoutcome = &del('file_permissions',\@del_lock,$domain,$user);
10385:         my $sqlresult =
10386:             &update_portfolio_table($user,$domain,$file_name,'portfolio_access',
10387:                                     $group);
10388:     } else {
10389:         $outcome = "error: could not obtain lockfile\n";  
10390:     }
10391:     return ($outcome,$deloutcome,\%new_values,\%translation);
10392: }
10393: 
10394: sub make_public_indefinitely {
10395:     my (@requrl) = @_;
10396:     return &automated_portfile_access('public',\@requrl);
10397: }
10398: 
10399: sub automated_portfile_access {
10400:     my ($accesstype,$addsref,$delsref,$info) = @_;
10401:     unless (($accesstype eq 'public') || ($accesstype eq 'ip')) {
10402:         return 'invalid';
10403:     }
10404:     my %urls;
10405:     if (ref($addsref) eq 'ARRAY') {
10406:         foreach my $requrl (@{$addsref}) {
10407:             if (&is_portfolio_url($requrl)) {
10408:                 unless (exists($urls{$requrl})) {
10409:                     $urls{$requrl} = 'add';
10410:                 }
10411:             }
10412:         }
10413:     }
10414:     if (ref($delsref) eq 'ARRAY') {
10415:         foreach my $requrl (@{$delsref}) { 
10416:             if (&is_portfolio_url($requrl)) {
10417:                 unless (exists($urls{$requrl})) {
10418:                     $urls{$requrl} = 'delete'; 
10419:                 }
10420:             }
10421:         }
10422:     }
10423:     unless (keys(%urls)) {
10424:         return 'invalid';
10425:     }
10426:     my $ip;
10427:     if ($accesstype eq 'ip') {
10428:         if (ref($info) eq 'HASH') {
10429:             if ($info->{'ip'} ne '') {
10430:                 $ip = $info->{'ip'};
10431:             }
10432:         }
10433:         if ($ip eq '') {
10434:             return 'invalid';
10435:         }
10436:     }
10437:     my $errors;
10438:     my $now = time;
10439:     my %current_perms;
10440:     foreach my $requrl (sort(keys(%urls))) {
10441:         my $action;
10442:         if ($urls{$requrl} eq 'add') {
10443:             $action = 'activate';
10444:         } else {
10445:             $action = 'none';
10446:         }
10447:         my $aclnum = 0;
10448:         my (undef,$udom,$unum,$file_name,$group) =
10449:             &parse_portfolio_url($requrl);
10450:         unless (exists($current_perms{$unum.':'.$udom})) {
10451:             $current_perms{$unum.':'.$udom} = &get_portfile_permissions($udom,$unum);
10452:         }
10453:         my %access_controls = &get_access_controls($current_perms{$unum.':'.$udom},
10454:                                                    $group,$file_name);
10455:         foreach my $key (keys(%{$access_controls{$file_name}})) {
10456:             my ($num,$scope,$end,$start) = 
10457:                 ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
10458:             if ($scope eq $accesstype) {
10459:                 if (($start <= $now) && ($end == 0)) {
10460:                     if ($accesstype eq 'ip') {
10461:                         if (ref($access_controls{$file_name}{$key}) eq 'HASH') {
10462:                             if (ref($access_controls{$file_name}{$key}{'ip'}) eq 'ARRAY') {
10463:                                 if (grep(/^\Q$ip\E$/,@{$access_controls{$file_name}{$key}{'ip'}})) {
10464:                                     if ($urls{$requrl} eq 'add') {
10465:                                         $action = 'none';
10466:                                         last;
10467:                                     } else {
10468:                                         $action = 'delete';
10469:                                         $aclnum = $num;
10470:                                         last;
10471:                                     }
10472:                                 }
10473:                             }
10474:                         }
10475:                     } elsif ($accesstype eq 'public') {
10476:                         if ($urls{$requrl} eq 'add') {
10477:                             $action = 'none';
10478:                             last;
10479:                         } else {
10480:                             $action = 'delete';
10481:                             $aclnum = $num;
10482:                             last;
10483:                         }
10484:                     }
10485:                 } elsif ($accesstype eq 'public') {
10486:                     $action = 'update';
10487:                     $aclnum = $num;
10488:                     last;
10489:                 }
10490:             }
10491:         }
10492:         if ($action eq 'none') {
10493:             next;
10494:         } else {
10495:             my %changes;
10496:             my $newend = 0;
10497:             my $newstart = $now;
10498:             my $newkey = $aclnum.':'.$accesstype.'_'.$newend.'_'.$newstart;
10499:             $changes{$action}{$newkey} = {
10500:                 type => $accesstype,
10501:                 time => {
10502:                     start => $newstart,
10503:                     end   => $newend,
10504:                 },
10505:             };
10506:             if ($accesstype eq 'ip') {
10507:                 $changes{$action}{$newkey}{'ip'} = [$ip];
10508:             }
10509:             my ($outcome,$deloutcome,$new_values,$translation) =
10510:                 &modify_access_controls($file_name,\%changes,$udom,$unum);
10511:             unless ($outcome eq 'ok') {
10512:                 $errors .= $outcome.' ';
10513:             }
10514:         }
10515:     }
10516:     if ($errors) {
10517:         $errors =~ s/\s$//;
10518:         return $errors;
10519:     } else {
10520:         return 'ok';
10521:     }
10522: }
10523: 
10524: #------------------------------------------------------Get Marked as Read Only
10525: 
10526: sub get_marked_as_readonly {
10527:     my ($domain,$user,$what,$group) = @_;
10528:     my $current_permissions = &get_portfile_permissions($domain,$user);
10529:     my @readonly_files;
10530:     my $cmp1=$what;
10531:     if (ref($what)) { $cmp1=join('',@{$what}) };
10532:     while (my ($file_name,$value) = each(%{$current_permissions})) {
10533:         if (defined($group)) {
10534:             if ($file_name !~ m-^\Q$group\E/-) {
10535:                 next;
10536:             }
10537:         }
10538:         if (ref($value) eq "ARRAY"){
10539:             foreach my $stored_what (@{$value}) {
10540:                 my $cmp2=$stored_what;
10541:                 if (ref($stored_what) eq 'ARRAY') {
10542:                     $cmp2=join('',@{$stored_what});
10543:                 }
10544:                 if ($cmp1 eq $cmp2) {
10545:                     push(@readonly_files, $file_name);
10546:                     last;
10547:                 } elsif (!defined($what)) {
10548:                     push(@readonly_files, $file_name);
10549:                     last;
10550:                 }
10551:             }
10552:         }
10553:     }
10554:     return @readonly_files;
10555: }
10556: #-----------------------------------------------------------Get Marked as Read Only Hash
10557: 
10558: sub get_marked_as_readonly_hash {
10559:     my ($current_permissions,$group,$what) = @_;
10560:     my %readonly_files;
10561:     while (my ($file_name,$value) = each(%{$current_permissions})) {
10562:         if (defined($group)) {
10563:             if ($file_name !~ m-^\Q$group\E/-) {
10564:                 next;
10565:             }
10566:         }
10567:         if (ref($value) eq "ARRAY"){
10568:             foreach my $stored_what (@{$value}) {
10569:                 if (ref($stored_what) eq 'ARRAY') {
10570:                     foreach my $lock_descriptor(@{$stored_what}) {
10571:                         if ($lock_descriptor eq 'graded') {
10572:                             $readonly_files{$file_name} = 'graded';
10573:                         } elsif ($lock_descriptor eq 'handback') {
10574:                             $readonly_files{$file_name} = 'handback';
10575:                         } else {
10576:                             if (!exists($readonly_files{$file_name})) {
10577:                                 $readonly_files{$file_name} = 'locked';
10578:                             }
10579:                         }
10580:                     }
10581:                 } 
10582:             }
10583:         } 
10584:     }
10585:     return %readonly_files;
10586: }
10587: # ------------------------------------------------------------ Unmark as Read Only
10588: 
10589: sub unmark_as_readonly {
10590:     # unmarks $file_name (if $file_name is defined), or all files locked by $what 
10591:     # for portfolio submissions, $what contains [$symb,$crsid] 
10592:     my ($domain,$user,$what,$file_name,$group) = @_;
10593:     $file_name = &declutter_portfile($file_name);
10594:     my $symb_crs = $what;
10595:     if (ref($what)) { $symb_crs=join('',@$what); }
10596:     my %current_permissions = &dump('file_permissions',$domain,$user,$group);
10597:     my ($tmp)=keys(%current_permissions);
10598:     if ($tmp=~/^error:/) { undef(%current_permissions); }
10599:     my @readonly_files = &get_marked_as_readonly($domain,$user,$what,$group);
10600:     foreach my $file (@readonly_files) {
10601: 	my $clean_file = &declutter_portfile($file);
10602: 	if (defined($file_name) && ($file_name ne $clean_file)) { next; }
10603: 	my $current_locks = $current_permissions{$file};
10604:         my @new_locks;
10605:         my @del_keys;
10606:         if (ref($current_locks) eq "ARRAY"){
10607:             foreach my $locker (@{$current_locks}) {
10608:                 my $compare=$locker;
10609:                 if (ref($locker) eq 'ARRAY') {
10610:                     $compare=join('',@{$locker});
10611:                     if ($compare ne $symb_crs) {
10612:                         push(@new_locks, $locker);
10613:                     }
10614:                 }
10615:             }
10616:             if (scalar(@new_locks) > 0) {
10617:                 $current_permissions{$file} = \@new_locks;
10618:             } else {
10619:                 push(@del_keys, $file);
10620:                 &del('file_permissions',\@del_keys, $domain, $user);
10621:                 delete($current_permissions{$file});
10622:             }
10623:         }
10624:     }
10625:     &put('file_permissions',\%current_permissions,$domain,$user);
10626:     return;
10627: }
10628: 
10629: # ------------------------------------------------------------ Directory lister
10630: 
10631: sub dirlist {
10632:     my ($uri,$userdomain,$username,$getpropath,$getuserdir,$alternateRoot)=@_;
10633:     $uri=~s/^\///;
10634:     $uri=~s/\/$//;
10635:     my ($udom, $uname);
10636:     if ($getuserdir) {
10637:         $udom = $userdomain;
10638:         $uname = $username;
10639:     } else {
10640:         (undef,$udom,$uname)=split(/\//,$uri);
10641:         if(defined($userdomain)) {
10642:             $udom = $userdomain;
10643:         }
10644:         if(defined($username)) {
10645:             $uname = $username;
10646:         }
10647:     }
10648:     my ($dirRoot,$listing,@listing_results);
10649: 
10650:     $dirRoot = $perlvar{'lonDocRoot'};
10651:     if (defined($getpropath)) {
10652:         $dirRoot = &propath($udom,$uname);
10653:         $dirRoot =~ s/\/$//;
10654:     } elsif (defined($getuserdir)) {
10655:         my $subdir=$uname.'__';
10656:         $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
10657:         $dirRoot = $Apache::lonnet::perlvar{'lonUsersDir'}
10658:                    ."/$udom/$subdir/$uname";
10659:     } elsif (defined($alternateRoot)) {
10660:         $dirRoot = $alternateRoot;
10661:     }
10662: 
10663:     if($udom) {
10664:         if($uname) {
10665:             my $uhome = &homeserver($uname,$udom);
10666:             if ($uhome eq 'no_host') {
10667:                 return ([],'no_host');
10668:             }
10669:             $listing = &reply('ls3:'.&escape('/'.$uri).':'.$getpropath.':'
10670:                               .$getuserdir.':'.&escape($dirRoot)
10671:                               .':'.&escape($uname).':'.&escape($udom),$uhome);
10672:             if ($listing eq 'unknown_cmd') {
10673:                 $listing = &reply('ls2:'.$dirRoot.'/'.$uri,$uhome);
10674:             } else {
10675:                 @listing_results = map { &unescape($_); } split(/:/,$listing);
10676:             }
10677:             if ($listing eq 'unknown_cmd') {
10678:                 $listing = &reply('ls:'.$dirRoot.'/'.$uri,$uhome);
10679:                 @listing_results = split(/:/,$listing);
10680:             } else {
10681:                 @listing_results = map { &unescape($_); } split(/:/,$listing);
10682:             }
10683:             if (($listing eq 'no_such_host') || ($listing eq 'con_lost') || 
10684:                 ($listing eq 'rejected') || ($listing eq 'refused') ||
10685:                 ($listing eq 'no_such_dir') || ($listing eq 'empty')) {
10686:                 return ([],$listing);
10687:             } else {
10688:                 return (\@listing_results);
10689:             }
10690:         } elsif(!$alternateRoot) {
10691:             my (%allusers,%listerror);
10692: 	    my %servers = &get_servers($udom,'library');
10693:  	    foreach my $tryserver (keys(%servers)) {
10694:                 $listing = &reply('ls3:'.&escape("/res/$udom").':::::'.
10695:                                   &escape($udom),$tryserver);
10696:                 if ($listing eq 'unknown_cmd') {
10697: 		    $listing = &reply('ls2:'.$perlvar{'lonDocRoot'}.'/res/'.
10698: 				      $udom, $tryserver);
10699:                 } else {
10700:                     @listing_results = map { &unescape($_); } split(/:/,$listing);
10701:                 }
10702: 		if ($listing eq 'unknown_cmd') {
10703: 		    $listing = &reply('ls:'.$perlvar{'lonDocRoot'}.'/res/'.
10704: 				      $udom, $tryserver);
10705: 		    @listing_results = split(/:/,$listing);
10706: 		} else {
10707: 		    @listing_results =
10708: 			map { &unescape($_); } split(/:/,$listing);
10709: 		}
10710:                 if (($listing eq 'no_such_host') || ($listing eq 'con_lost') ||
10711:                     ($listing eq 'rejected') || ($listing eq 'refused') ||
10712:                     ($listing eq 'no_such_dir') || ($listing eq 'empty')) {
10713:                     $listerror{$tryserver} = $listing;
10714:                 } else {
10715: 		    foreach my $line (@listing_results) {
10716: 			my ($entry) = split(/&/,$line,2);
10717: 			$allusers{$entry} = 1;
10718: 		    }
10719: 		}
10720:             }
10721:             my @alluserslist=();
10722:             foreach my $user (sort(keys(%allusers))) {
10723:                 push(@alluserslist,$user.'&user');
10724:             }
10725: 
10726:             if (!%listerror) {
10727:                 # no errors
10728:                 return (\@alluserslist);
10729:             } elsif (scalar(keys(%servers)) == 1) {
10730:                 # one library server, one error 
10731:                 my ($key) = keys(%listerror);
10732:                 return (\@alluserslist, $listerror{$key});
10733:             } elsif ( grep { $_ eq 'con_lost' } values(%listerror) ) {
10734:                 # con_lost indicates that we might miss data from at least one
10735:                 # library server
10736:                 return (\@alluserslist, 'con_lost');
10737:             } else {
10738:                 # multiple library servers and no con_lost -> data should be
10739:                 # complete. 
10740:                 return (\@alluserslist);
10741:             }
10742: 
10743:         } else {
10744:             return ([],'missing username');
10745:         }
10746:     } elsif(!defined($getpropath)) {
10747:         my $path = $perlvar{'lonDocRoot'}.'/res/'; 
10748:         my @all_domains = map { $path.$_.'/&domain'; } (sort(&all_domains()));
10749:         return (\@all_domains);
10750:     } else {
10751:         return ([],'missing domain');
10752:     }
10753: }
10754: 
10755: # --------------------------------------------- GetFileTimestamp
10756: # This function utilizes dirlist and returns the date stamp for
10757: # when it was last modified.  It will also return an error of -1
10758: # if an error occurs
10759: 
10760: sub GetFileTimestamp {
10761:     my ($studentDomain,$studentName,$filename,$getuserdir)=@_;
10762:     $studentDomain = &LONCAPA::clean_domain($studentDomain);
10763:     $studentName   = &LONCAPA::clean_username($studentName);
10764:     my ($fileref,$error) = &dirlist($filename,$studentDomain,$studentName,
10765:                                     undef,$getuserdir);
10766:     if (($error eq 'empty') || ($error eq 'no_such_dir')) {
10767:         return -1;
10768:     }
10769:     if (ref($fileref) eq 'ARRAY') {
10770:         my @stats = split('&',$fileref->[0]);
10771:         # @stats contains first the filename, then the stat output
10772:         return $stats[10]; # so this is 10 instead of 9.
10773:     } else {
10774:         return -1;
10775:     }
10776: }
10777: 
10778: sub stat_file {
10779:     my ($uri) = @_;
10780:     $uri = &clutter_with_no_wrapper($uri);
10781: 
10782:     my ($udom,$uname,$file);
10783:     if ($uri =~ m-^/(uploaded|editupload)/-) {
10784: 	($udom,$uname,$file) =
10785: 	    ($uri =~ m-/(?:uploaded|editupload)/?($match_domain)/?($match_name)/?(.*)-);
10786: 	$file = 'userfiles/'.$file;
10787:     }
10788:     if ($uri =~ m-^/res/-) {
10789: 	($udom,$uname) = 
10790: 	    ($uri =~ m-/(?:res)/?($match_domain)/?($match_username)/-);
10791: 	$file = $uri;
10792:     }
10793: 
10794:     if (!$udom || !$uname || !$file) {
10795: 	# unable to handle the uri
10796: 	return ();
10797:     }
10798:     my $getpropath;
10799:     if ($file =~ /^userfiles\//) {
10800:         $getpropath = 1;
10801:     }
10802:     my ($listref,$error) = &dirlist($file,$udom,$uname,$getpropath);
10803:     if (($error eq 'empty') || ($error eq 'no_such_dir')) {
10804:         return ();
10805:     } else {
10806:         if (ref($listref) eq 'ARRAY') {
10807:             my @stats = split('&',$listref->[0]);
10808: 	    shift(@stats); #filename is first
10809: 	    return @stats;
10810:         }
10811:     }
10812:     return ();
10813: }
10814: 
10815: # --------------------------------------------------------- recursedirs
10816: # Recursive function to traverse either a specific user's Authoring Space
10817: # or corresponding Published Resource Space, and populate the hash ref:
10818: # $dirhashref with URLs of all directories, and if $filehashref hash
10819: # ref arg is provided, the URLs of any files, excluding versioned, .meta,
10820: # or .rights files in resource space, and .meta, .save, .log, and .bak
10821: # files in Authoring Space.
10822: #
10823: # Inputs:
10824: #
10825: # $is_home - true if current server is home server for user's space
10826: # $context - either: priv, or res respectively for Authoring or Resource Space.
10827: # $docroot - Document root (i.e., /home/httpd/html
10828: # $toppath - Top level directory (i.e., /res/$dom/$uname or /priv/$dom/$uname
10829: # $relpath - Current path (relative to top level).
10830: # $dirhashref - reference to hash to populate with URLs of directories (Required)
10831: # $filehashref - reference to hash to populate with URLs of files (Optional)
10832: #
10833: # Returns: nothing
10834: #
10835: # Side Effects: populates $dirhashref, and $filehashref (if provided).
10836: #
10837: # Currently used by interface/londocs.pm to create linked select boxes for
10838: # directory and filename to import a Course "Author" resource into a course, and
10839: # also to create linked select boxes for Authoring Space and Directory to choose
10840: # save location for creation of a new "standard" problem from the Course Editor.
10841: #
10842: 
10843: sub recursedirs {
10844:     my ($is_home,$context,$docroot,$toppath,$relpath,$dirhashref,$filehashref) = @_;
10845:     return unless (ref($dirhashref) eq 'HASH');
10846:     my $currpath = $docroot.$toppath;
10847:     if ($relpath) {
10848:         $currpath .= "/$relpath";
10849:     }
10850:     my $savefile;
10851:     if (ref($filehashref)) {
10852:         $savefile = 1;
10853:     }
10854:     if ($is_home) {
10855:         if (opendir(my $dirh,$currpath)) {
10856:             foreach my $item (sort { lc($a) cmp lc($b) } grep(!/^\.+$/,readdir($dirh))) {
10857:                 next if ($item eq '');
10858:                 if (-d "$currpath/$item") {
10859:                     my $newpath;
10860:                     if ($relpath) {
10861:                         $newpath = "$relpath/$item";
10862:                     } else {
10863:                         $newpath = $item;
10864:                     }
10865:                     $dirhashref->{&Apache::lonlocal::js_escape($newpath)} = 1;
10866:                     &recursedirs($is_home,$context,$docroot,$toppath,$newpath,$dirhashref,$filehashref);
10867:                 } elsif ($savefile) {
10868:                     if ($context eq 'priv') {
10869:                         unless ($item =~ /\.(meta|save|log|bak|DS_Store)$/) {
10870:                             $filehashref->{&Apache::lonlocal::js_escape($relpath)}{$item} = 1;
10871:                         }
10872:                     } else {
10873:                         unless (($item =~ /\.meta$/) || ($item =~ /\.\d+\.\w+$/) || ($item =~ /\.rights$/)) {
10874:                             $filehashref->{&Apache::lonlocal::js_escape($relpath)}{$item} = 1;
10875:                         }
10876:                     }
10877:                 }
10878:             }
10879:             closedir($dirh);
10880:         }
10881:     } else {
10882:         my ($dirlistref,$listerror) =
10883:             &dirlist($toppath.$relpath);
10884:         my @dir_lines;
10885:         my $dirptr=16384;
10886:         if (ref($dirlistref) eq 'ARRAY') {
10887:             foreach my $dir_line (sort
10888:                               {
10889:                                   my ($afile)=split('&',$a,2);
10890:                                   my ($bfile)=split('&',$b,2);
10891:                                   return (lc($afile) cmp lc($bfile));
10892:                               } (@{$dirlistref})) {
10893:                 my ($item,$dom,undef,$testdir,undef,undef,undef,undef,$size,undef,$mtime,undef,undef,undef,$obs,undef) =
10894:                     split(/\&/,$dir_line,16);
10895:                 $item =~ s/\s+$//;
10896:                 next if (($item =~ /^\.\.?$/) || ($obs));
10897:                 if ($dirptr&$testdir) {
10898:                     my $newpath;
10899:                     if ($relpath) {
10900:                         $newpath = "$relpath/$item";
10901:                     } else {
10902:                         $relpath = '/';
10903:                         $newpath = $item;
10904:                     }
10905:                     $dirhashref->{&Apache::lonlocal::js_escape($newpath)} = 1;
10906:                     &recursedirs($is_home,$context,$docroot,$toppath,$newpath,$dirhashref,$filehashref);
10907:                 } elsif ($savefile) {
10908:                     if ($context eq 'priv') {
10909:                         unless ($item =~ /\.(meta|save|log|bak|DS_Store)$/) {
10910:                             $filehashref->{$relpath}{$item} = 1;
10911:                         }
10912:                     } else {
10913:                         unless (($item =~ /\.meta$/) || ($item =~ /\.\d+\.\w+$/)) {
10914:                             $filehashref->{$relpath}{$item} = 1;
10915:                         }
10916:                     }
10917:                 }
10918:             }
10919:         }
10920:     }
10921:     return;
10922: }
10923: 
10924: # -------------------------------------------------------- Value of a Condition
10925: 
10926: # gets the value of a specific preevaluated condition
10927: #    stored in the string  $env{user.state.<cid>}
10928: # or looks up a condition reference in the bighash and if if hasn't
10929: # already been evaluated recurses into docondval to get the value of
10930: # the condition, then memoizing it to 
10931: #   $env{user.state.<cid>.<condition>}
10932: sub directcondval {
10933:     my $number=shift;
10934:     if (!defined($env{'user.state.'.$env{'request.course.id'}})) {
10935: 	&Apache::lonuserstate::evalstate();
10936:     }
10937:     if (exists($env{'user.state.'.$env{'request.course.id'}.".$number"})) {
10938: 	return $env{'user.state.'.$env{'request.course.id'}.".$number"};
10939:     } elsif ($number =~ /^_/) {
10940: 	my $sub_condition;
10941: 	if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
10942: 		&GDBM_READER(),0640)) {
10943: 	    $sub_condition=$bighash{'conditions'.$number};
10944: 	    untie(%bighash);
10945: 	}
10946: 	my $value = &docondval($sub_condition);
10947: 	&appenv({'user.state.'.$env{'request.course.id'}.".$number" => $value});
10948: 	return $value;
10949:     }
10950:     if ($env{'user.state.'.$env{'request.course.id'}}) {
10951:        return substr($env{'user.state.'.$env{'request.course.id'}},$number,1);
10952:     } else {
10953:        return 2;
10954:     }
10955: }
10956: 
10957: # get the collection of conditions for this resource
10958: sub condval {
10959:     my $condidx=shift;
10960:     my $allpathcond='';
10961:     foreach my $cond (split(/\|/,$condidx)) {
10962: 	if (defined($env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond})) {
10963: 	    $allpathcond.=
10964: 		'('.$env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond}.')|';
10965: 	}
10966:     }
10967:     $allpathcond=~s/\|$//;
10968:     return &docondval($allpathcond);
10969: }
10970: 
10971: #evaluates an expression of conditions
10972: sub docondval {
10973:     my ($allpathcond) = @_;
10974:     my $result=0;
10975:     if ($env{'request.course.id'}
10976: 	&& defined($allpathcond)) {
10977: 	my $operand='|';
10978: 	my @stack;
10979: 	foreach my $chunk ($allpathcond=~/(\d+|_\d+\.\d+|\(|\)|\&|\|)/g) {
10980: 	    if ($chunk eq '(') {
10981: 		push @stack,($operand,$result);
10982: 	    } elsif ($chunk eq ')') {
10983: 		my $before=pop @stack;
10984: 		if (pop @stack eq '&') {
10985: 		    $result=$result>$before?$before:$result;
10986: 		} else {
10987: 		    $result=$result>$before?$result:$before;
10988: 		}
10989: 	    } elsif (($chunk eq '&') || ($chunk eq '|')) {
10990: 		$operand=$chunk;
10991: 	    } else {
10992: 		my $new=directcondval($chunk);
10993: 		if ($operand eq '&') {
10994: 		    $result=$result>$new?$new:$result;
10995: 		} else {
10996: 		    $result=$result>$new?$result:$new;
10997: 		}
10998: 	    }
10999: 	}
11000:     }
11001:     return $result;
11002: }
11003: 
11004: # ---------------------------------------------------- Devalidate courseresdata
11005: 
11006: sub devalidatecourseresdata {
11007:     my ($coursenum,$coursedomain)=@_;
11008:     my $hashid=$coursenum.':'.$coursedomain;
11009:     &devalidate_cache_new('courseres',$hashid);
11010: }
11011: 
11012: 
11013: # --------------------------------------------------- Course Resourcedata Query
11014: #
11015: #  Parameters:
11016: #      $coursenum    - Number of the course.
11017: #      $coursedomain - Domain at which the course was created.
11018: #  Returns:
11019: #     A hash of the course parameters along (I think) with timestamps
11020: #     and version info.
11021: 
11022: sub get_courseresdata {
11023:     my ($coursenum,$coursedomain)=@_;
11024:     my $coursehom=&homeserver($coursenum,$coursedomain);
11025:     my $hashid=$coursenum.':'.$coursedomain;
11026:     my ($result,$cached)=&is_cached_new('courseres',$hashid);
11027:     my %dumpreply;
11028:     unless (defined($cached)) {
11029: 	%dumpreply=&dump('resourcedata',$coursedomain,$coursenum);
11030: 	$result=\%dumpreply;
11031: 	my ($tmp) = keys(%dumpreply);
11032: 	if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
11033: 	    &do_cache_new('courseres',$hashid,$result,600);
11034: 	} elsif ($tmp =~ /^(con_lost|no_such_host)/) {
11035: 	    return $tmp;
11036: 	} elsif ($tmp =~ /^(error)/) {
11037: 	    $result=undef;
11038: 	    &do_cache_new('courseres',$hashid,$result,600);
11039: 	}
11040:     }
11041:     return $result;
11042: }
11043: 
11044: sub devalidateuserresdata {
11045:     my ($uname,$udom)=@_;
11046:     my $hashid="$udom:$uname";
11047:     &devalidate_cache_new('userres',$hashid);
11048: }
11049: 
11050: sub get_userresdata {
11051:     my ($uname,$udom)=@_;
11052:     #most student don\'t have any data set, check if there is some data
11053:     if (&EXT_cache_status($udom,$uname)) { return undef; }
11054: 
11055:     my $hashid="$udom:$uname";
11056:     my ($result,$cached)=&is_cached_new('userres',$hashid);
11057:     if (!defined($cached)) {
11058: 	my %resourcedata=&dump('resourcedata',$udom,$uname);
11059: 	$result=\%resourcedata;
11060: 	&do_cache_new('userres',$hashid,$result,600);
11061:     }
11062:     my ($tmp)=keys(%$result);
11063:     if (($tmp!~/^error\:/) && ($tmp!~/^con_lost/)) {
11064: 	return $result;
11065:     }
11066:     #error 2 occurs when the .db doesn't exist
11067:     if ($tmp!~/error: 2 /) {
11068:         if ((!defined($cached)) || ($tmp ne 'con_lost')) {
11069: 	    &logthis("<font color=\"blue\">WARNING:".
11070: 		     " Trying to get resource data for ".
11071: 		     $uname." at ".$udom.": ".
11072: 		     $tmp."</font>");
11073:         }
11074:     } elsif ($tmp=~/error: 2 /) {
11075: 	#&EXT_cache_set($udom,$uname);
11076: 	&do_cache_new('userres',$hashid,undef,600);
11077: 	undef($tmp); # not really an error so don't send it back
11078:     }
11079:     return $tmp;
11080: }
11081: #----------------------------------------------- resdata - return resource data
11082: #  Purpose:
11083: #    Return resource data for either users or for a course.
11084: #  Parameters:
11085: #     $name      - Course/user name.
11086: #     $domain    - Name of the domain the user/course is registered on.
11087: #     $type      - Type of thing $name is (must be 'course' or 'user')
11088: #     $mapp      - decluttered URL of enclosing map  
11089: #     $recursed  - Ref to scalar -- set to 1, if nested maps have been recursed.
11090: #     $recurseup - Ref to array of map URLs, starting with map containing
11091: #                  $mapp up through hierarchy of nested maps to top level map.  
11092: #     $courseid  - CourseID (first part of param identifier).
11093: #     $modifier  - Middle part of param identifier.
11094: #     $what      - Last part of param identifier.
11095: #     @which     - Array of names of resources desired.
11096: #  Returns:
11097: #     The value of the first reasource in @which that is found in the
11098: #     resource hash.
11099: #  Exceptional Conditions:
11100: #     If the $type passed in is not valid (not the string 'course' or 
11101: #     'user', an undefined  reference is returned.
11102: #     If none of the resources are found, an undef is returned
11103: sub resdata {
11104:     my ($name,$domain,$type,$mapp,$recursed,$recurseup,$courseid,
11105:         $modifier,$what,@which)=@_;
11106:     my $result;
11107:     if ($type eq 'course') {
11108: 	$result=&get_courseresdata($name,$domain);
11109:     } elsif ($type eq 'user') {
11110: 	$result=&get_userresdata($name,$domain);
11111:     }
11112:     if (!ref($result)) { return $result; }    
11113:     foreach my $item (@which) {
11114:         if ($item->[1] eq 'course') {
11115:             if ((ref($recurseup) eq 'ARRAY') && (ref($recursed) eq 'SCALAR')) {
11116:                 unless ($$recursed) {
11117:                     @{$recurseup} = &get_map_hierarchy($mapp,$courseid);
11118:                     $$recursed = 1;
11119:                 }
11120:                 foreach my $item (@${recurseup}) {
11121:                     my $norecursechk=$courseid.$modifier.$item.'___(all).'.$what;
11122:                     last if (defined($result->{$norecursechk}));
11123:                     my $recursechk=$courseid.$modifier.$item.'___(rec).'.$what;
11124:                     if (defined($result->{$recursechk})) { return [$result->{$recursechk},'map']; }
11125:                 }
11126:             }
11127:         }
11128:         if (defined($result->{$item->[0]})) {
11129: 	    return [$result->{$item->[0]},$item->[1]];
11130: 	}
11131:     }
11132:     return undef;
11133: }
11134: 
11135: sub get_domain_ltitools {
11136:     my ($cdom) = @_;
11137:     my %ltitools;
11138:     my ($result,$cached)=&is_cached_new('ltitools',$cdom);
11139:     if (defined($cached)) {
11140:         if (ref($result) eq 'HASH') {
11141:             %ltitools = %{$result};
11142:         }
11143:     } else {
11144:         my %domconfig = &get_dom('configuration',['ltitools'],$cdom);
11145:         if (ref($domconfig{'ltitools'}) eq 'HASH') {
11146:             %ltitools = %{$domconfig{'ltitools'}};
11147:             my %encdomconfig = &get_dom('encconfig',['ltitools'],$cdom);
11148:             if (ref($encdomconfig{'ltitools'}) eq 'HASH') {
11149:                 foreach my $id (keys(%ltitools)) {
11150:                     if (ref($encdomconfig{'ltitools'}{$id}) eq 'HASH') {
11151:                         foreach my $item ('key','secret') {
11152:                             $ltitools{$id}{$item} = $encdomconfig{'ltitools'}{$id}{$item};
11153:                         }
11154:                     }
11155:                 }
11156:             }
11157:         }
11158:         my $cachetime = 24*60*60;
11159:         &do_cache_new('ltitools',$cdom,\%ltitools,$cachetime);
11160:     }
11161:     return %ltitools;
11162: }
11163: 
11164: sub get_numsuppfiles {
11165:     my ($cnum,$cdom,$ignorecache)=@_;
11166:     my $hashid=$cnum.':'.$cdom;
11167:     my ($suppcount,$cached);
11168:     unless ($ignorecache) {
11169:         ($suppcount,$cached) = &is_cached_new('suppcount',$hashid);
11170:     }
11171:     unless (defined($cached)) {
11172:         my $chome=&homeserver($cnum,$cdom);
11173:         unless ($chome eq 'no_host') {
11174:             ($suppcount,my $errors) = (0,0);
11175:             my $suppmap = 'supplemental.sequence';
11176:             ($suppcount,$errors) = 
11177:                 &Apache::loncommon::recurse_supplemental($cnum,$cdom,$suppmap,$suppcount,$errors);
11178:         }
11179:         &do_cache_new('suppcount',$hashid,$suppcount,600);
11180:     }
11181:     return $suppcount;
11182: }
11183: 
11184: #
11185: # EXT resource caching routines
11186: #
11187: 
11188: {
11189: # Cache (5 seconds) of map hierarchy for speedup of navmaps display
11190: #
11191: # The course for which we cache
11192: my $cachedmapkey='';
11193: # The cached recursive maps for this course
11194: my %cachedmaps=();
11195: # When this was last done
11196: my $cachedmaptime='';
11197: 
11198: sub clear_EXT_cache_status {
11199:     &delenv('cache.EXT.');
11200: }
11201: 
11202: sub EXT_cache_status {
11203:     my ($target_domain,$target_user) = @_;
11204:     my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
11205:     if (exists($env{$cachename}) && ($env{$cachename}+600) > time) {
11206:         # We know already the user has no data
11207:         return 1;
11208:     } else {
11209:         return 0;
11210:     }
11211: }
11212: 
11213: sub EXT_cache_set {
11214:     my ($target_domain,$target_user) = @_;
11215:     my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
11216:     #&appenv({$cachename => time});
11217: }
11218: 
11219: # --------------------------------------------------------- Value of a Variable
11220: sub EXT {
11221: 
11222:     my ($varname,$symbparm,$udom,$uname,$usection,$recurse,$cid)=@_;
11223:     unless ($varname) { return ''; }
11224:     #get real user name/domain, courseid and symb
11225:     my $courseid;
11226:     my $publicuser;
11227:     if ($symbparm) {
11228: 	$symbparm=&get_symb_from_alias($symbparm);
11229:     }
11230:     if (!($uname && $udom)) {
11231:       (my $cursymb,$courseid,$udom,$uname,$publicuser)= &whichuser($symbparm);
11232:       if (!$symbparm) {	$symbparm=$cursymb; }
11233:     } else {
11234: 	$courseid=$env{'request.course.id'};
11235:     }
11236:     my ($realm,$space,$qualifier,@therest)=split(/\./,$varname);
11237:     my $rest;
11238:     if (defined($therest[0])) {
11239:        $rest=join('.',@therest);
11240:     } else {
11241:        $rest='';
11242:     }
11243: 
11244:     my $qualifierrest=$qualifier;
11245:     if ($rest) { $qualifierrest.='.'.$rest; }
11246:     my $spacequalifierrest=$space;
11247:     if ($qualifierrest) { $spacequalifierrest.='.'.$qualifierrest; }
11248:     if ($realm eq 'user') {
11249: # --------------------------------------------------------------- user.resource
11250: 	if ($space eq 'resource') {
11251: 	    if ( (defined($Apache::lonhomework::parsing_a_problem)
11252: 		  || defined($Apache::lonhomework::parsing_a_task))
11253: 		 &&
11254: 		 ($symbparm eq &symbread()) ) {	
11255: 		# if we are in the middle of processing the resource the
11256: 		# get the value we are planning on committing
11257:                 if (defined($Apache::lonhomework::results{$qualifierrest})) {
11258:                     return $Apache::lonhomework::results{$qualifierrest};
11259:                 } else {
11260:                     return $Apache::lonhomework::history{$qualifierrest};
11261:                 }
11262: 	    } else {
11263: 		my %restored;
11264: 		if ($publicuser || $env{'request.state'} eq 'construct') {
11265: 		    %restored=&tmprestore($symbparm,$courseid,$udom,$uname);
11266: 		} else {
11267: 		    %restored=&restore($symbparm,$courseid,$udom,$uname);
11268: 		}
11269: 		return $restored{$qualifierrest};
11270: 	    }
11271: # ----------------------------------------------------------------- user.access
11272:         } elsif ($space eq 'access') {
11273: 	    # FIXME - not supporting calls for a specific user
11274:             return &allowed($qualifier,$rest);
11275: # ------------------------------------------ user.preferences, user.environment
11276:         } elsif (($space eq 'preferences') || ($space eq 'environment')) {
11277: 	    if (($uname eq $env{'user.name'}) &&
11278: 		($udom eq $env{'user.domain'})) {
11279: 		return $env{join('.',('environment',$qualifierrest))};
11280: 	    } else {
11281: 		my %returnhash;
11282: 		if (!$publicuser) {
11283: 		    %returnhash=&userenvironment($udom,$uname,
11284: 						 $qualifierrest);
11285: 		}
11286: 		return $returnhash{$qualifierrest};
11287: 	    }
11288: # ----------------------------------------------------------------- user.course
11289:         } elsif ($space eq 'course') {
11290: 	    # FIXME - not supporting calls for a specific user
11291:             return $env{join('.',('request.course',$qualifier))};
11292: # ------------------------------------------------------------------- user.role
11293:         } elsif ($space eq 'role') {
11294: 	    # FIXME - not supporting calls for a specific user
11295:             my ($role,$where)=split(/\./,$env{'request.role'});
11296:             if ($qualifier eq 'value') {
11297: 		return $role;
11298:             } elsif ($qualifier eq 'extent') {
11299:                 return $where;
11300:             }
11301: # ----------------------------------------------------------------- user.domain
11302:         } elsif ($space eq 'domain') {
11303:             return $udom;
11304: # ------------------------------------------------------------------- user.name
11305:         } elsif ($space eq 'name') {
11306:             return $uname;
11307: # ---------------------------------------------------- Any other user namespace
11308:         } else {
11309: 	    my %reply;
11310: 	    if (!$publicuser) {
11311: 		%reply=&get($space,[$qualifierrest],$udom,$uname);
11312: 	    }
11313: 	    return $reply{$qualifierrest};
11314:         }
11315:     } elsif ($realm eq 'query') {
11316: # ---------------------------------------------- pull stuff out of query string
11317:         &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
11318: 						[$spacequalifierrest]);
11319: 	return $env{'form.'.$spacequalifierrest}; 
11320:    } elsif ($realm eq 'request') {
11321: # ------------------------------------------------------------- request.browser
11322:         if ($space eq 'browser') {
11323:             return $env{'browser.'.$qualifier};
11324: # ------------------------------------------------------------ request.filename
11325:         } else {
11326:             return $env{'request.'.$spacequalifierrest};
11327:         }
11328:     } elsif ($realm eq 'course') {
11329: # ---------------------------------------------------------- course.description
11330:         return $env{'course.'.$courseid.'.'.$spacequalifierrest};
11331:     } elsif ($realm eq 'resource') {
11332: 
11333: 	if (defined($courseid) && $courseid eq $env{'request.course.id'}) {
11334: 	    if (!$symbparm) { $symbparm=&symbread(); }
11335: 	}
11336: 
11337:         if ($qualifier eq '') {
11338: 	    if ($space eq 'title') {
11339: 	        if (!$symbparm) { $symbparm = $env{'request.filename'}; }
11340: 	        return &gettitle($symbparm);
11341: 	    }
11342: 	
11343: 	    if ($space eq 'map') {
11344: 	        my ($map) = &decode_symb($symbparm);
11345: 	        return &symbread($map);
11346: 	    }
11347:             if ($space eq 'maptitle') {
11348:                 my ($map) = &decode_symb($symbparm);
11349:                 return &gettitle($map);
11350:             }
11351: 	    if ($space eq 'filename') {
11352: 	        if ($symbparm) {
11353: 		    return &clutter((&decode_symb($symbparm))[2]);
11354: 	        }
11355: 	        return &hreflocation('',$env{'request.filename'});
11356: 	    }
11357: 
11358:             if ((defined($courseid)) && ($courseid eq $env{'request.course.id'}) && $symbparm) {
11359:                 if ($space eq 'visibleparts') {
11360:                     my $navmap = Apache::lonnavmaps::navmap->new();
11361:                     my $item;
11362:                     if (ref($navmap)) {
11363:                         my $res = $navmap->getBySymb($symbparm);
11364:                         my $parts = $res->parts();
11365:                         if (ref($parts) eq 'ARRAY') {
11366:                             $item = join(',',@{$parts});
11367:                         }
11368:                         undef($navmap);
11369:                     }
11370:                     return $item;
11371:                 }
11372:             }
11373:         }
11374: 
11375: 	my ($section, $group, @groups, @recurseup, $recursed);
11376: 	my ($courselevelm,$courseleveli,$courselevel,$mapp);
11377:         if (($courseid eq '') && ($cid)) {
11378:             $courseid = $cid;
11379:         }
11380: 	if (($symbparm && $courseid) && 
11381: 	    (($courseid eq $env{'request.course.id'}) || ($courseid eq $cid)))  {
11382: 
11383: 	    #print '<br>'.$space.' - '.$qualifier.' - '.$spacequalifierrest;
11384: 
11385: # ----------------------------------------------------- Cascading lookup scheme
11386: 	    my $symbp=$symbparm;
11387: 	    $mapp=&deversion((&decode_symb($symbp))[0]);
11388: 	    my $symbparm=$symbp.'.'.$spacequalifierrest;
11389:             my $recurseparm=$mapp.'___(rec).'.$spacequalifierrest;
11390: 	    my $mapparm=$mapp.'___(all).'.$spacequalifierrest;
11391: 	    if (($env{'user.name'} eq $uname) &&
11392: 		($env{'user.domain'} eq $udom)) {
11393: 		$section=$env{'request.course.sec'};
11394:                 @groups = split(/:/,$env{'request.course.groups'});  
11395:                 @groups=&sort_course_groups($courseid,@groups); 
11396: 	    } else {
11397: 		if (! defined($usection)) {
11398: 		    $section=&getsection($udom,$uname,$courseid);
11399: 		} else {
11400: 		    $section = $usection;
11401: 		}
11402:                 @groups = &get_users_groups($udom,$uname,$courseid);
11403: 	    }
11404: 
11405: 	    my $seclevel=$courseid.'.['.$section.'].'.$spacequalifierrest;
11406: 	    my $seclevelr=$courseid.'.['.$section.'].'.$symbparm;
11407:             my $secleveli=$courseid.'.['.$section.'].'.$recurseparm;
11408: 	    my $seclevelm=$courseid.'.['.$section.'].'.$mapparm;
11409: 
11410: 	    $courselevel=$courseid.'.'.$spacequalifierrest;
11411: 	    my $courselevelr=$courseid.'.'.$symbparm;
11412:             $courseleveli=$courseid.'.'.$recurseparm;
11413: 	    $courselevelm=$courseid.'.'.$mapparm;
11414: 
11415: # ----------------------------------------------------------- first, check user
11416: 
11417: 	    my $userreply=&resdata($uname,$udom,'user',$mapp,\$recursed,
11418:                                    \@recurseup,$courseid,'.',$spacequalifierrest, 
11419: 				       ([$courselevelr,'resource'],
11420: 					[$courselevelm,'map'     ],
11421:                                         [$courseleveli,'map'     ],
11422: 					[$courselevel, 'course'  ]));
11423: 	    if (defined($userreply)) { return &get_reply($userreply); }
11424: 
11425: # ------------------------------------------------ second, check some of course
11426:             my $coursereply;
11427:             if (@groups > 0) {
11428:                 $coursereply = &check_group_parms($courseid,\@groups,$symbparm,
11429:                                        $recurseparm,$mapparm,$spacequalifierrest,
11430:                                        $mapp,\$recursed,\@recurseup);
11431:                 if (defined($coursereply)) { return &get_reply($coursereply); } 
11432:             }
11433: 
11434: 	    $coursereply=&resdata($env{'course.'.$courseid.'.num'},
11435: 				  $env{'course.'.$courseid.'.domain'},
11436: 				  'course',$mapp,\$recursed,\@recurseup,
11437:                                   $courseid,'.['.$section.'].',$spacequalifierrest,
11438: 				  ([$seclevelr,   'resource'],
11439: 				   [$seclevelm,   'map'     ],
11440:                                    [$secleveli,   'map'     ],
11441: 				   [$seclevel,    'course'  ],
11442: 				   [$courselevelr,'resource']));
11443: 	    if (defined($coursereply)) { return &get_reply($coursereply); }
11444: 
11445: # ------------------------------------------------------ third, check map parms
11446: 	    my %parmhash=();
11447: 	    my $thisparm='';
11448: 	    if (tie(%parmhash,'GDBM_File',
11449: 		    $env{'request.course.fn'}.'_parms.db',
11450: 		    &GDBM_READER(),0640)) {
11451: 		$thisparm=$parmhash{$symbparm};
11452: 		untie(%parmhash);
11453: 	    }
11454: 	    if ($thisparm) { return &get_reply([$thisparm,'resource']); }
11455: 	}
11456: # ------------------------------------------ fourth, look in resource metadata
11457:  
11458:         my $what = $spacequalifierrest;
11459: 	$what=~s/\./\_/;
11460: 	my $filename;
11461: 	if (!$symbparm) { $symbparm=&symbread(); }
11462: 	if ($symbparm) {
11463: 	    $filename=(&decode_symb($symbparm))[2];
11464: 	} else {
11465: 	    $filename=$env{'request.filename'};
11466: 	}
11467: 	my $metadata=&metadata($filename,$what);
11468: 	if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
11469: 	$metadata=&metadata($filename,'parameter_'.$what);
11470: 	if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
11471: 
11472: # ----------------------------------------------- fifth, look in rest of course
11473: 	if ($symbparm && defined($courseid) && 
11474: 	    $courseid eq $env{'request.course.id'}) {
11475: 	    my $coursereply=&resdata($env{'course.'.$courseid.'.num'},
11476: 				     $env{'course.'.$courseid.'.domain'},
11477: 				     'course',$mapp,\$recursed,\@recurseup,
11478:                                      $courseid,'.',$spacequalifierrest,
11479: 				     ([$courselevelm,'map'   ],
11480:                                       [$courseleveli,'map'   ],
11481: 				      [$courselevel, 'course']));
11482: 	    if (defined($coursereply)) { return &get_reply($coursereply); }
11483: 	}
11484: # ------------------------------------------------------------------ Cascade up
11485: 	unless ($space eq '0') {
11486: 	    my @parts=split(/_/,$space);
11487: 	    my $id=pop(@parts);
11488: 	    my $part=join('_',@parts);
11489: 	    if ($part eq '') { $part='0'; }
11490: 	    my @partgeneral=&EXT('resource.'.$part.'.'.$qualifierrest,
11491: 				 $symbparm,$udom,$uname,$section,1);
11492: 	    if (defined($partgeneral[0])) { return &get_reply(\@partgeneral); }
11493: 	}
11494: 	if ($recurse) { return undef; }
11495: 	my $pack_def=&packages_tab_default($filename,$varname);
11496: 	if (defined($pack_def)) { return &get_reply([$pack_def,'resource']); }
11497: # ---------------------------------------------------- Any other user namespace
11498:     } elsif ($realm eq 'environment') {
11499: # ----------------------------------------------------------------- environment
11500: 	if (($uname eq $env{'user.name'})&&($udom eq $env{'user.domain'})) {
11501: 	    return $env{'environment.'.$spacequalifierrest};
11502: 	} else {
11503: 	    if ($uname eq 'anonymous' && $udom eq '') {
11504: 		return '';
11505: 	    }
11506: 	    my %returnhash=&userenvironment($udom,$uname,
11507: 					    $spacequalifierrest);
11508: 	    return $returnhash{$spacequalifierrest};
11509: 	}
11510:     } elsif ($realm eq 'system') {
11511: # ----------------------------------------------------------------- system.time
11512: 	if ($space eq 'time') {
11513: 	    return time;
11514:         }
11515:     } elsif ($realm eq 'server') {
11516: # ----------------------------------------------------------------- system.time
11517: 	if ($space eq 'name') {
11518: 	    return $ENV{'SERVER_NAME'};
11519:         }
11520:     }
11521:     return '';
11522: }
11523: 
11524: sub get_reply {
11525:     my ($reply_value) = @_;
11526:     if (ref($reply_value) eq 'ARRAY') {
11527:         if (wantarray) {
11528: 	    return @$reply_value;
11529:         }
11530:         return $reply_value->[0];
11531:     } else {
11532:         return $reply_value;
11533:     }
11534: }
11535: 
11536: sub check_group_parms {
11537:     my ($courseid,$groups,$symbparm,$recurseparm,$mapparm,$what,$mapp,
11538:         $recursed,$recurseupref) = @_;
11539:     my @levels = ([$symbparm,'resource'],[$mapparm,'map'],[$recurseparm,'map'],
11540:                   [$what,'course']);
11541:     my $coursereply;
11542:     foreach my $group (@{$groups}) {
11543:         my @groupitems = ();
11544:         foreach my $level (@levels) {
11545:              my $item = $courseid.'.['.$group.'].'.$level->[0];
11546:              push(@groupitems,[$item,$level->[1]]);
11547:         }
11548:         my $coursereply = &resdata($env{'course.'.$courseid.'.num'},
11549:                                    $env{'course.'.$courseid.'.domain'},
11550:                                    'course',$mapp,$recursed,$recurseupref,
11551:                                    $courseid,'.['.$group.'].',$what,
11552:                                    @groupitems);
11553:         last if (defined($coursereply));
11554:     }
11555:     return $coursereply;
11556: }
11557: 
11558: sub get_map_hierarchy {
11559:     my ($mapname,$courseid) = @_;
11560:     my @recurseup = ();
11561:     if ($mapname) {
11562:         if (($cachedmapkey eq $courseid) &&
11563:             (abs($cachedmaptime-time)<5)) {
11564:             if (ref($cachedmaps{$mapname}) eq 'ARRAY') {
11565:                 return @{$cachedmaps{$mapname}};
11566:             }
11567:         }
11568:         my $navmap = Apache::lonnavmaps::navmap->new();
11569:         if (ref($navmap)) {
11570:             @recurseup = $navmap->recurseup_maps($mapname);
11571:             undef($navmap);
11572:             $cachedmaps{$mapname} = \@recurseup;
11573:             $cachedmaptime=time;
11574:             $cachedmapkey=$courseid;
11575:         }
11576:     }
11577:     return @recurseup;
11578: }
11579: 
11580: }
11581: 
11582: sub sort_course_groups { # Sort groups based on defined rankings. Default is sort().
11583:     my ($courseid,@groups) = @_;
11584:     @groups = sort(@groups);
11585:     return @groups;
11586: }
11587: 
11588: sub packages_tab_default {
11589:     my ($uri,$varname)=@_;
11590:     my (undef,$part,$name)=split(/\./,$varname);
11591: 
11592:     my (@extension,@specifics,$do_default);
11593:     foreach my $package (split(/,/,&metadata($uri,'packages'))) {
11594: 	my ($pack_type,$pack_part)=split(/_/,$package,2);
11595: 	if ($pack_type eq 'default') {
11596: 	    $do_default=1;
11597: 	} elsif ($pack_type eq 'extension') {
11598: 	    push(@extension,[$package,$pack_type,$pack_part]);
11599: 	} elsif ($pack_part eq $part || $pack_type eq 'part') {
11600: 	    # only look at packages defaults for packages that this id is
11601: 	    push(@specifics,[$package,$pack_type,$pack_part]);
11602: 	}
11603:     }
11604:     # first look for a package that matches the requested part id
11605:     foreach my $package (@specifics) {
11606: 	my (undef,$pack_type,$pack_part)=@{$package};
11607: 	next if ($pack_part ne $part);
11608: 	if (defined($packagetab{"$pack_type&$name&default"})) {
11609: 	    return $packagetab{"$pack_type&$name&default"};
11610: 	}
11611:     }
11612:     # look for any possible matching non extension_ package
11613:     foreach my $package (@specifics) {
11614: 	my (undef,$pack_type,$pack_part)=@{$package};
11615: 	if (defined($packagetab{"$pack_type&$name&default"})) {
11616: 	    return $packagetab{"$pack_type&$name&default"};
11617: 	}
11618: 	if ($pack_type eq 'part') { $pack_part='0'; }
11619: 	if (defined($packagetab{$pack_type."_".$pack_part."&$name&default"})) {
11620: 	    return $packagetab{$pack_type."_".$pack_part."&$name&default"};
11621: 	}
11622:     }
11623:     # look for any posible extension_ match
11624:     foreach my $package (@extension) {
11625: 	my ($package,$pack_type)=@{$package};
11626: 	if (defined($packagetab{"$pack_type&$name&default"})) {
11627: 	    return $packagetab{"$pack_type&$name&default"};
11628: 	}
11629: 	if (defined($packagetab{$package."&$name&default"})) {
11630: 	    return $packagetab{$package."&$name&default"};
11631: 	}
11632:     }
11633:     # look for a global default setting
11634:     if ($do_default && defined($packagetab{"default&$name&default"})) {
11635: 	return $packagetab{"default&$name&default"};
11636:     }
11637:     return undef;
11638: }
11639: 
11640: sub add_prefix_and_part {
11641:     my ($prefix,$part)=@_;
11642:     my $keyroot;
11643:     if (defined($prefix) && $prefix !~ /^__/) {
11644: 	# prefix that has a part already
11645: 	$keyroot=$prefix;
11646:     } elsif (defined($prefix)) {
11647: 	# prefix that is missing a part
11648: 	if (defined($part)) { $keyroot='_'.$part.substr($prefix,1); }
11649:     } else {
11650: 	# no prefix at all
11651: 	if (defined($part)) { $keyroot='_'.$part; }
11652:     }
11653:     return $keyroot;
11654: }
11655: 
11656: # ---------------------------------------------------------------- Get metadata
11657: 
11658: my %metaentry;
11659: my %importedpartids;
11660: sub metadata {
11661:     my ($uri,$what,$liburi,$prefix,$depthcount)=@_;
11662:     $uri=&declutter($uri);
11663:     # if it is a non metadata possible uri return quickly
11664:     if (($uri eq '') || 
11665: 	(($uri =~ m|^/*adm/|) && 
11666: 	     ($uri !~ m|^adm/includes|) && ($uri !~ m{/(smppg|bulletinboard|ext\.tool)$})) ||
11667:         ($uri =~ m|/$|) || ($uri =~ m|/.meta$|) || ($uri =~ m{^/*uploaded/.+\.sequence$})) {
11668: 	return undef;
11669:     }
11670:     if (($uri =~ /^priv/ || $uri=~m{^home/httpd/html/priv}) 
11671: 	&& &Apache::lonxml::get_state('target') =~ /^(|meta)$/) {
11672: 	return undef;
11673:     }
11674:     my $filename=$uri;
11675:     $uri=~s/\.meta$//;
11676: #
11677: # Is the metadata already cached?
11678: # Look at timestamp of caching
11679: # Everything is cached by the main uri, libraries are never directly cached
11680: #
11681:     if (!defined($liburi)) {
11682: 	my ($result,$cached)=&is_cached_new('meta',$uri);
11683: 	if (defined($cached)) { return $result->{':'.$what}; }
11684:     }
11685:     {
11686: # Imported parts would go here
11687:         my %importedids=();
11688:         my @origfileimportpartids=();
11689:         my $importedparts=0;
11690: #
11691: # Is this a recursive call for a library?
11692: #
11693: #	if (! exists($metacache{$uri})) {
11694: #	    $metacache{$uri}={};
11695: #	}
11696: 	my $cachetime = 60*60;
11697:         if ($liburi) {
11698: 	    $liburi=&declutter($liburi);
11699:             $filename=$liburi;
11700:         } else {
11701: 	    &devalidate_cache_new('meta',$uri);
11702: 	    undef(%metaentry);
11703: 	}
11704:         my %metathesekeys=();
11705:         unless ($filename=~/\.meta$/) { $filename.='.meta'; }
11706: 	my $metastring;
11707: 	if ($uri =~ /^priv/ || $uri=~/home\/httpd\/html\/priv/) {
11708: 	    my $which = &hreflocation('','/'.($liburi || $uri));
11709: 	    $metastring = 
11710: 		&Apache::lonnet::ssi_body($which,
11711: 					  ('grade_target' => 'meta'));
11712: 	    $cachetime = 1; # only want this cached in the child not long term
11713: 	} elsif (($uri !~ m -^(editupload)/-) && 
11714:                  ($uri !~ m{^/*uploaded/$match_domain/$match_courseid/docs/})) {
11715: 	    my $file=&filelocation('',&clutter($filename));
11716: 	    #push(@{$metaentry{$uri.'.file'}},$file);
11717: 	    $metastring=&getfile($file);
11718: 	}
11719:         my $parser=HTML::LCParser->new(\$metastring);
11720:         my $token;
11721:         undef %metathesekeys;
11722:         while ($token=$parser->get_token) {
11723: 	    if ($token->[0] eq 'S') {
11724: 		if (defined($token->[2]->{'package'})) {
11725: #
11726: # This is a package - get package info
11727: #
11728: 		    my $package=$token->[2]->{'package'};
11729: 		    my $keyroot=&add_prefix_and_part($prefix,$token->[2]->{'part'});
11730: 		    if (defined($token->[2]->{'id'})) { 
11731: 			$keyroot.='_'.$token->[2]->{'id'}; 
11732: 		    }
11733: 		    if ($metaentry{':packages'}) {
11734: 			$metaentry{':packages'}.=','.$package.$keyroot;
11735: 		    } else {
11736: 			$metaentry{':packages'}=$package.$keyroot;
11737: 		    }
11738: 		    foreach my $pack_entry (keys(%packagetab)) {
11739: 			my $part=$keyroot;
11740: 			$part=~s/^\_//;
11741: 			if ($pack_entry=~/^\Q$package\E\&/ || 
11742: 			    $pack_entry=~/^\Q$package\E_0\&/) {
11743: 			    my ($pack,$name,$subp)=split(/\&/,$pack_entry);
11744: 			    # ignore package.tab specified default values
11745:                             # here &package_tab_default() will fetch those
11746: 			    if ($subp eq 'default') { next; }
11747: 			    my $value=$packagetab{$pack_entry};
11748: 			    my $unikey;
11749: 			    if ($pack =~ /_0$/) {
11750: 				$unikey='parameter_0_'.$name;
11751: 				$part=0;
11752: 			    } else {
11753: 				$unikey='parameter'.$keyroot.'_'.$name;
11754: 			    }
11755: 			    if ($subp eq 'display') {
11756: 				$value.=' [Part: '.$part.']';
11757: 			    }
11758: 			    $metaentry{':'.$unikey.'.part'}=$part;
11759: 			    $metathesekeys{$unikey}=1;
11760: 			    unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
11761: 				$metaentry{':'.$unikey.'.'.$subp}=$value;
11762: 			    }
11763: 			    if (defined($metaentry{':'.$unikey.'.default'})) {
11764: 				$metaentry{':'.$unikey}=
11765: 				    $metaentry{':'.$unikey.'.default'};
11766: 			    }
11767: 			}
11768: 		    }
11769: 		} else {
11770: #
11771: # This is not a package - some other kind of start tag
11772: #
11773: 		    my $entry=$token->[1];
11774: 		    my $unikey='';
11775: 
11776: 		    if ($entry eq 'import') {
11777: #
11778: # Importing a library here
11779: #
11780:                         my $location=$parser->get_text('/import');
11781:                         my $dir=$filename;
11782:                         $dir=~s|[^/]*$||;
11783:                         $location=&filelocation($dir,$location);
11784:                        
11785:                         my $importmode=$token->[2]->{'importmode'};
11786:                         if ($importmode eq 'problem') {
11787: # Import as problem/response
11788:                            $unikey=&add_prefix_and_part($prefix,$token->[2]->{'part'});
11789:                         } elsif ($importmode eq 'part') {
11790: # Import as part(s)
11791:                            $importedparts=1;
11792: # We need to get the original file and the imported file to get the part order correct
11793: # Good news: we do not need to worry about nested libraries, since parts cannot be nested
11794: # Load and inspect original file
11795:                            if ($#origfileimportpartids<0) {
11796:                               undef(%importedpartids);
11797:                               my $origfilelocation=$perlvar{'lonDocRoot'}.&clutter($uri);
11798:                               my $origfile=&getfile($origfilelocation);
11799:                               @origfileimportpartids=($origfile=~/<(part|import)[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
11800:                            }
11801: 
11802: # Load and inspect imported file
11803:                            my $impfile=&getfile($location);
11804:                            my @impfilepartids=($impfile=~/<part[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
11805:                            if ($#impfilepartids>=0) {
11806: # This problem had parts
11807:                                $importedpartids{$token->[2]->{'id'}}=join(',',@impfilepartids);
11808:                            } else {
11809: # Importing by turning a single problem into a problem part
11810: # It gets the import-tags ID as part-ID
11811:                                $unikey=&add_prefix_and_part($prefix,$token->[2]->{'id'});
11812:                                $importedpartids{$token->[2]->{'id'}}=$token->[2]->{'id'};
11813:                            }
11814:                         } else {
11815: # Normal import
11816:                            $unikey=&add_prefix_and_part($prefix,$token->[2]->{'part'});
11817:                            if (defined($token->[2]->{'id'})) {
11818:                               $unikey.='_'.$token->[2]->{'id'};
11819:                            }
11820:                         }
11821: 
11822: 			if ($depthcount<20) {
11823: 			    my $metadata = 
11824: 				&metadata($uri,'keys', $location,$unikey,
11825: 					  $depthcount+1);
11826: 			    foreach my $meta (split(',',$metadata)) {
11827: 				$metaentry{':'.$meta}=$metaentry{':'.$meta};
11828: 				$metathesekeys{$meta}=1;
11829: 			    }
11830: 			
11831:                         }
11832: 		    } else {
11833: #
11834: # Not importing, some other kind of non-package, non-library start tag
11835: # 
11836:                         $unikey=$entry.&add_prefix_and_part($prefix,$token->[2]->{'part'});
11837:                         if (defined($token->[2]->{'id'})) {
11838:                             $unikey.='_'.$token->[2]->{'id'};
11839:                         }
11840: 			if (defined($token->[2]->{'name'})) { 
11841: 			    $unikey.='_'.$token->[2]->{'name'}; 
11842: 			}
11843: 			$metathesekeys{$unikey}=1;
11844: 			foreach my $param (@{$token->[3]}) {
11845: 			    $metaentry{':'.$unikey.'.'.$param} =
11846: 				$token->[2]->{$param};
11847: 			}
11848: 			my $internaltext=&HTML::Entities::decode($parser->get_text('/'.$entry));
11849: 			my $default=$metaentry{':'.$unikey.'.default'};
11850: 			if ( $internaltext =~ /^\s*$/ && $default !~ /^\s*$/) {
11851: 		 # only ws inside the tag, and not in default, so use default
11852: 		 # as value
11853: 			    $metaentry{':'.$unikey}=$default;
11854: 			} elsif ( $internaltext =~ /\S/ ) {
11855: 		  # something interesting inside the tag
11856: 			    $metaentry{':'.$unikey}=$internaltext;
11857: 			} else {
11858: 		  # no interesting values, don't set a default
11859: 			}
11860: # end of not-a-package not-a-library import
11861: 		    }
11862: # end of not-a-package start tag
11863: 		}
11864: # the next is the end of "start tag"
11865: 	    }
11866: 	}
11867: 	my ($extension) = ($uri =~ /\.(\w+)$/);
11868: 	$extension = lc($extension);
11869: 	if ($extension eq 'htm') { $extension='html'; }
11870: 
11871: 	foreach my $key (keys(%packagetab)) {
11872: 	    #no specific packages #how's our extension
11873: 	    if ($key!~/^extension_\Q$extension\E&/) { next; }
11874: 	    &metadata_create_package_def($uri,$key,'extension_'.$extension,
11875: 					 \%metathesekeys);
11876: 	}
11877: 
11878: 	if (!exists($metaentry{':packages'})
11879: 	    || $packagetab{"import_defaults&extension_$extension"}) {
11880: 	    foreach my $key (keys(%packagetab)) {
11881: 		#no specific packages well let's get default then
11882: 		if ($key!~/^default&/) { next; }
11883: 		&metadata_create_package_def($uri,$key,'default',
11884: 					     \%metathesekeys);
11885: 	    }
11886: 	}
11887: # are there custom rights to evaluate
11888: 	if ($metaentry{':copyright'} eq 'custom') {
11889: 
11890:     #
11891:     # Importing a rights file here
11892:     #
11893: 	    unless ($depthcount) {
11894: 		my $location=$metaentry{':customdistributionfile'};
11895: 		my $dir=$filename;
11896: 		$dir=~s|[^/]*$||;
11897: 		$location=&filelocation($dir,$location);
11898: 		my $rights_metadata =
11899: 		    &metadata($uri,'keys',$location,'_rights',
11900: 			      $depthcount+1);
11901: 		foreach my $rights (split(',',$rights_metadata)) {
11902: 		    #$metaentry{':'.$rights}=$metacache{$uri}->{':'.$rights};
11903: 		    $metathesekeys{$rights}=1;
11904: 		}
11905: 	    }
11906: 	}
11907: 	# uniqifiy package listing
11908: 	my %seen;
11909: 	my @uniq_packages =
11910: 	    grep { ! $seen{$_} ++ } (split(',',$metaentry{':packages'}));
11911: 	$metaentry{':packages'} = join(',',@uniq_packages);
11912: 
11913:         if ($importedparts) {
11914: # We had imported parts and need to rebuild partorder
11915:            $metaentry{':partorder'}='';
11916:            $metathesekeys{'partorder'}=1;
11917:            for (my $index=0;$index<$#origfileimportpartids;$index+=2) {
11918:                if ($origfileimportpartids[$index] eq 'part') {
11919: # original part, part of the problem
11920:                   $metaentry{':partorder'}.=','.$origfileimportpartids[$index+1];
11921:                } else {
11922: # we have imported parts at this position
11923:                   $metaentry{':partorder'}.=','.$importedpartids{$origfileimportpartids[$index+1]};
11924:                }
11925:            }
11926:            $metaentry{':partorder'}=~s/^\,//;
11927:         }
11928: 
11929: 	$metaentry{':keys'} = join(',',keys(%metathesekeys));
11930: 	&metadata_generate_part0(\%metathesekeys,\%metaentry,$uri);
11931: 	$metaentry{':allpossiblekeys'}=join(',',keys(%metathesekeys));
11932: 	&do_cache_new('meta',$uri,\%metaentry,$cachetime);
11933: # this is the end of "was not already recently cached
11934:     }
11935:     return $metaentry{':'.$what};
11936: }
11937: 
11938: sub metadata_create_package_def {
11939:     my ($uri,$key,$package,$metathesekeys)=@_;
11940:     my ($pack,$name,$subp)=split(/\&/,$key);
11941:     if ($subp eq 'default') { next; }
11942:     
11943:     if (defined($metaentry{':packages'})) {
11944: 	$metaentry{':packages'}.=','.$package;
11945:     } else {
11946: 	$metaentry{':packages'}=$package;
11947:     }
11948:     my $value=$packagetab{$key};
11949:     my $unikey;
11950:     $unikey='parameter_0_'.$name;
11951:     $metaentry{':'.$unikey.'.part'}=0;
11952:     $$metathesekeys{$unikey}=1;
11953:     unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
11954: 	$metaentry{':'.$unikey.'.'.$subp}=$value;
11955:     }
11956:     if (defined($metaentry{':'.$unikey.'.default'})) {
11957: 	$metaentry{':'.$unikey}=
11958: 	    $metaentry{':'.$unikey.'.default'};
11959:     }
11960: }
11961: 
11962: sub metadata_generate_part0 {
11963:     my ($metadata,$metacache,$uri) = @_;
11964:     my %allnames;
11965:     foreach my $metakey (keys(%$metadata)) {
11966: 	if ($metakey=~/^parameter\_(.*)/) {
11967: 	  my $part=$$metacache{':'.$metakey.'.part'};
11968: 	  my $name=$$metacache{':'.$metakey.'.name'};
11969: 	  if (! exists($$metadata{'parameter_0_'.$name.'.name'})) {
11970: 	    $allnames{$name}=$part;
11971: 	  }
11972: 	}
11973:     }
11974:     foreach my $name (keys(%allnames)) {
11975:       $$metadata{"parameter_0_$name"}=1;
11976:       my $key=":parameter_0_$name";
11977:       $$metacache{"$key.part"}='0';
11978:       $$metacache{"$key.name"}=$name;
11979:       $$metacache{"$key.type"}=$$metacache{':parameter_'.
11980: 					   $allnames{$name}.'_'.$name.
11981: 					   '.type'};
11982:       my $olddis=$$metacache{':parameter_'.$allnames{$name}.'_'.$name.
11983: 			     '.display'};
11984:       my $expr='[Part: '.$allnames{$name}.']';
11985:       $olddis=~s/\Q$expr\E/\[Part: 0\]/;
11986:       $$metacache{"$key.display"}=$olddis;
11987:     }
11988: }
11989: 
11990: # ------------------------------------------------------ Devalidate title cache
11991: 
11992: sub devalidate_title_cache {
11993:     my ($url)=@_;
11994:     if (!$env{'request.course.id'}) { return; }
11995:     my $symb=&symbread($url);
11996:     if (!$symb) { return; }
11997:     my $key=$env{'request.course.id'}."\0".$symb;
11998:     &devalidate_cache_new('title',$key);
11999: }
12000: 
12001: # ------------------------------------------------- Get the title of a course
12002: 
12003: sub current_course_title {
12004:     return $env{ 'course.' . $env{'request.course.id'} . '.description' };
12005: }
12006: # ------------------------------------------------- Get the title of a resource
12007: 
12008: sub gettitle {
12009:     my $urlsymb=shift;
12010:     my $symb=&symbread($urlsymb);
12011:     if ($symb) {
12012: 	my $key=$env{'request.course.id'}."\0".$symb;
12013: 	my ($result,$cached)=&is_cached_new('title',$key);
12014: 	if (defined($cached)) { 
12015: 	    return $result;
12016: 	}
12017: 	my ($map,$resid,$url)=&decode_symb($symb);
12018: 	my $title='';
12019: 	if (!$map && $resid == 0 && $url =~/default\.sequence$/) {
12020: 	    $title = $env{'course.'.$env{'request.course.id'}.'.description'};
12021: 	} else {
12022: 	    if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
12023: 		    &GDBM_READER(),0640)) {
12024: 		my $mapid=$bighash{'map_pc_'.&clutter($map)};
12025: 		$title=$bighash{'title_'.$mapid.'.'.$resid};
12026: 		untie(%bighash);
12027: 	    }
12028: 	}
12029: 	$title=~s/\&colon\;/\:/gs;
12030: 	if ($title) {
12031: # Remember both $symb and $title for dynamic metadata
12032:             $accesshash{$symb.'___crstitle'}=$title;
12033:             $accesshash{&declutter($map).'___'.&declutter($url).'___usage'}=time;
12034: # Cache this title and then return it
12035: 	    return &do_cache_new('title',$key,$title,600);
12036: 	}
12037: 	$urlsymb=$url;
12038:     }
12039:     my $title=&metadata($urlsymb,'title');
12040:     if (!$title) { $title=(split('/',$urlsymb))[-1]; }    
12041:     return $title;
12042: }
12043: 
12044: sub get_slot {
12045:     my ($which,$cnum,$cdom)=@_;
12046:     if (!$cnum || !$cdom) {
12047: 	(undef,my $courseid)=&whichuser();
12048: 	$cdom=$env{'course.'.$courseid.'.domain'};
12049: 	$cnum=$env{'course.'.$courseid.'.num'};
12050:     }
12051:     my $key=join("\0",'slots',$cdom,$cnum,$which);
12052:     my %slotinfo;
12053:     if (exists($remembered{$key})) {
12054: 	$slotinfo{$which} = $remembered{$key};
12055:     } else {
12056: 	%slotinfo=&get('slots',[$which],$cdom,$cnum);
12057: 	&Apache::lonhomework::showhash(%slotinfo);
12058: 	my ($tmp)=keys(%slotinfo);
12059: 	if ($tmp=~/^error:/) { return (); }
12060: 	$remembered{$key} = $slotinfo{$which};
12061:     }
12062:     if (ref($slotinfo{$which}) eq 'HASH') {
12063: 	return %{$slotinfo{$which}};
12064:     }
12065:     return $slotinfo{$which};
12066: }
12067: 
12068: sub get_reservable_slots {
12069:     my ($cnum,$cdom,$uname,$udom) = @_;
12070:     my $now = time;
12071:     my $reservable_info;
12072:     my $key=join("\0",'reservableslots',$cdom,$cnum,$uname,$udom);
12073:     if (exists($remembered{$key})) {
12074:         $reservable_info = $remembered{$key};
12075:     } else {
12076:         my %resv;
12077:         ($resv{'now_order'},$resv{'now'},$resv{'future_order'},$resv{'future'}) =
12078:         &Apache::loncommon::get_future_slots($cnum,$cdom,$now);
12079:         $reservable_info = \%resv;
12080:         $remembered{$key} = $reservable_info;
12081:     }
12082:     return $reservable_info;
12083: }
12084: 
12085: sub get_course_slots {
12086:     my ($cnum,$cdom) = @_;
12087:     my $hashid=$cnum.':'.$cdom;
12088:     my ($result,$cached) = &Apache::lonnet::is_cached_new('allslots',$hashid);
12089:     if (defined($cached)) {
12090:         if (ref($result) eq 'HASH') {
12091:             return %{$result};
12092:         }
12093:     } else {
12094:         my %slots=&Apache::lonnet::dump('slots',$cdom,$cnum);
12095:         my ($tmp) = keys(%slots);
12096:         if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
12097:             &do_cache_new('allslots',$hashid,\%slots,600);
12098:             return %slots;
12099:         }
12100:     }
12101:     return;
12102: }
12103: 
12104: sub devalidate_slots_cache {
12105:     my ($cnum,$cdom)=@_;
12106:     my $hashid=$cnum.':'.$cdom;
12107:     &devalidate_cache_new('allslots',$hashid);
12108: }
12109: 
12110: sub get_coursechange {
12111:     my ($cdom,$cnum) = @_;
12112:     if ($cdom eq '' || $cnum eq '') {
12113:         return unless ($env{'request.course.id'});
12114:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
12115:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
12116:     }
12117:     my $hashid=$cdom.'_'.$cnum;
12118:     my ($change,$cached)=&is_cached_new('crschange',$hashid);
12119:     if ((defined($cached)) && ($change ne '')) {
12120:         return $change;
12121:     } else {
12122:         my %crshash;
12123:         %crshash = &get('environment',['internal.contentchange'],$cdom,$cnum);
12124:         if ($crshash{'internal.contentchange'} eq '') {
12125:             $change = $env{'course.'.$cdom.'_'.$cnum.'.internal.created'};
12126:             if ($change eq '') {
12127:                 %crshash = &get('environment',['internal.created'],$cdom,$cnum);
12128:                 $change = $crshash{'internal.created'};
12129:             }
12130:         } else {
12131:             $change = $crshash{'internal.contentchange'};
12132:         }
12133:         my $cachetime = 600;
12134:         &do_cache_new('crschange',$hashid,$change,$cachetime);
12135:     }
12136:     return $change;
12137: }
12138: 
12139: sub devalidate_coursechange_cache {
12140:     my ($cnum,$cdom)=@_;
12141:     my $hashid=$cnum.':'.$cdom;
12142:     &devalidate_cache_new('crschange',$hashid);
12143: }
12144: 
12145: # ------------------------------------------------- Update symbolic store links
12146: 
12147: sub symblist {
12148:     my ($mapname,%newhash)=@_;
12149:     $mapname=&deversion(&declutter($mapname));
12150:     my %hash;
12151:     if (($env{'request.course.fn'}) && (%newhash)) {
12152:         if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
12153:                       &GDBM_WRCREAT(),0640)) {
12154: 	    foreach my $url (keys(%newhash)) {
12155: 		next if ($url eq 'last_known'
12156: 			 && $env{'form.no_update_last_known'});
12157: 		$hash{declutter($url)}=&encode_symb($mapname,
12158: 						    $newhash{$url}->[1],
12159: 						    $newhash{$url}->[0]);
12160:             }
12161:             if (untie(%hash)) {
12162: 		return 'ok';
12163:             }
12164:         }
12165:     }
12166:     return 'error';
12167: }
12168: 
12169: # --------------------------------------------------------------- Verify a symb
12170: 
12171: sub symbverify {
12172:     my ($symb,$thisurl,$encstate)=@_;
12173:     my $thisfn=$thisurl;
12174:     $thisfn=&declutter($thisfn);
12175: # direct jump to resource in page or to a sequence - will construct own symbs
12176:     if ($thisfn=~/\.(page|sequence)$/) { return 1; }
12177: # check URL part
12178:     my ($map,$resid,$url)=&decode_symb($symb);
12179: 
12180:     unless ($url eq $thisfn) { return 0; }
12181: 
12182:     $symb=&symbclean($symb);
12183:     $thisurl=&deversion($thisurl);
12184:     $thisfn=&deversion($thisfn);
12185: 
12186:     my %bighash;
12187:     my $okay=0;
12188: 
12189:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
12190:                             &GDBM_READER(),0640)) {
12191:         my $noclutter;
12192:         if (($thisurl =~ m{^/adm/wrapper/ext/}) || ($thisurl =~ m{^ext/})) {
12193:             $thisurl =~ s/\?.+$//;
12194:             if ($map =~ m{^uploaded/.+\.page$}) {
12195:                 $thisurl =~ s{^(/adm/wrapper|)/ext/}{http://};
12196:                 $thisurl =~ s{^\Qhttp://https://\E}{https://};
12197:                 $noclutter = 1;
12198:             }
12199:         }
12200:         my $ids;
12201:         if ($noclutter) {
12202:             $ids=$bighash{'ids_'.$thisurl};
12203:         } else {
12204:             $ids=$bighash{'ids_'.&clutter($thisurl)};
12205:         }
12206:         unless ($ids) {
12207:             my $idkey = 'ids_'.($thisurl =~ m{^/}? '' : '/').$thisurl;  
12208:             $ids=$bighash{$idkey};
12209:         }
12210:         if ($ids) {
12211: # ------------------------------------------------------------------- Has ID(s)
12212:             if ($thisfn =~ m{^/adm/wrapper/ext/}) {
12213:                 $symb =~ s/\?.+$//;
12214:             }
12215: 	    foreach my $id (split(/\,/,$ids)) {
12216: 	       my ($mapid,$resid)=split(/\./,$id);
12217:                if (
12218:   &symbclean(&declutter($bighash{'map_id_'.$mapid}).'___'.$resid.'___'.$thisfn)
12219:    eq $symb) {
12220:                    if (ref($encstate)) {
12221:                        $$encstate = $bighash{'encrypted_'.$id};
12222:                    }
12223: 		   if (($env{'request.role.adv'}) ||
12224: 		       ($bighash{'encrypted_'.$id} eq $env{'request.enc'}) ||
12225:                        ($thisurl eq '/adm/navmaps')) {
12226: 		       $okay=1;
12227:                        last;
12228: 		   }
12229: 	       }
12230: 	   }
12231:         }
12232: 	untie(%bighash);
12233:     }
12234:     return $okay;
12235: }
12236: 
12237: # --------------------------------------------------------------- Clean-up symb
12238: 
12239: sub symbclean {
12240:     my $symb=shift;
12241:     if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
12242: # remove version from map
12243:     $symb=~s/\.(\d+)\.(\w+)\_\_\_/\.$2\_\_\_/;
12244: 
12245: # remove version from URL
12246:     $symb=~s/\.(\d+)\.(\w+)$/\.$2/;
12247: 
12248: # remove wrapper
12249: 
12250:     $symb=~s/(\_\_\_\d+\_\_\_)adm\/wrapper\/(res\/)*/$1/;
12251:     $symb=~s/(\_\_\_\d+\_\_\_)adm\/coursedocs\/showdoc\/(res\/)*/$1/;
12252:     return $symb;
12253: }
12254: 
12255: # ---------------------------------------------- Split symb to find map and url
12256: 
12257: sub encode_symb {
12258:     my ($map,$resid,$url)=@_;
12259:     return &symbclean(&declutter($map).'___'.$resid.'___'.&declutter($url));
12260: }
12261: 
12262: sub decode_symb {
12263:     my $symb=shift;
12264:     if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
12265:     my ($map,$resid,$url)=split(/___/,$symb);
12266:     return (&fixversion($map),$resid,&fixversion($url));
12267: }
12268: 
12269: sub fixversion {
12270:     my $fn=shift;
12271:     if ($fn=~/^(adm|uploaded|editupload|public)/) { return $fn; }
12272:     my %bighash;
12273:     my $uri=&clutter($fn);
12274:     my $key=$env{'request.course.id'}.'_'.$uri;
12275: # is this cached?
12276:     my ($result,$cached)=&is_cached_new('courseresversion',$key);
12277:     if (defined($cached)) { return $result; }
12278: # unfortunately not cached, or expired
12279:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
12280: 	    &GDBM_READER(),0640)) {
12281:  	if ($bighash{'version_'.$uri}) {
12282:  	    my $version=$bighash{'version_'.$uri};
12283:  	    unless (($version eq 'mostrecent') || 
12284: 		    ($version==&getversion($uri))) {
12285:  		$uri=~s/\.(\w+)$/\.$version\.$1/;
12286:  	    }
12287:  	}
12288:  	untie %bighash;
12289:     }
12290:     return &do_cache_new('courseresversion',$key,&declutter($uri),600);
12291: }
12292: 
12293: sub deversion {
12294:     my $url=shift;
12295:     $url=~s/\.\d+\.(\w+)$/\.$1/;
12296:     return $url;
12297: }
12298: 
12299: # ------------------------------------------------------ Return symb list entry
12300: 
12301: sub symbread {
12302:     my ($thisfn,$donotrecurse,$ignorecachednull,$checkforblock,$possibles)=@_;
12303:     my $cache_str='request.symbread.cached.'.$thisfn;
12304:     if (defined($env{$cache_str})) {
12305:         if ($ignorecachednull) {
12306:             return $env{$cache_str} unless ($env{$cache_str} eq '');
12307:         } else {
12308:             return $env{$cache_str};
12309:         }
12310:     }
12311: # no filename provided? try from environment
12312:     unless ($thisfn) {
12313:         if ($env{'request.symb'}) {
12314: 	    return $env{$cache_str}=&symbclean($env{'request.symb'});
12315: 	}
12316: 	$thisfn=$env{'request.filename'};
12317:     }
12318:     if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
12319: # is that filename actually a symb? Verify, clean, and return
12320:     if ($thisfn=~/\_\_\_\d+\_\_\_(.*)$/) {
12321: 	if (&symbverify($thisfn,$1)) {
12322: 	    return $env{$cache_str}=&symbclean($thisfn);
12323: 	}
12324:     }
12325:     $thisfn=declutter($thisfn);
12326:     my %hash;
12327:     my %bighash;
12328:     my $syval='';
12329:     if (($env{'request.course.fn'}) && ($thisfn)) {
12330:         my $targetfn = $thisfn;
12331:         if ( ($thisfn =~ m/^(uploaded|editupload)\//) && ($thisfn !~ m/\.(page|sequence)$/) ) {
12332:             $targetfn = 'adm/wrapper/'.$thisfn;
12333:         }
12334: 	if ($targetfn =~ m|^adm/wrapper/(ext/.*)|) {
12335: 	    $targetfn=$1;
12336: 	}
12337:         if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
12338:                       &GDBM_READER(),0640)) {
12339: 	    $syval=$hash{$targetfn};
12340:             untie(%hash);
12341:         }
12342: # ---------------------------------------------------------- There was an entry
12343:         if ($syval) {
12344: 	    #unless ($syval=~/\_\d+$/) {
12345: 		#unless ($env{'form.request.prefix'}=~/\.(\d+)\_$/) {
12346: 		    #&appenv({'request.ambiguous' => $thisfn});
12347: 		    #return $env{$cache_str}='';
12348: 		#}    
12349: 		#$syval.=$1;
12350: 	    #}
12351:         } else {
12352: # ------------------------------------------------------- Was not in symb table
12353:            if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
12354:                             &GDBM_READER(),0640)) {
12355: # ---------------------------------------------- Get ID(s) for current resource
12356:               my $ids=$bighash{'ids_'.&clutter($thisfn)};
12357:               unless ($ids) { 
12358:                  $ids=$bighash{'ids_/'.$thisfn};
12359:               }
12360:               unless ($ids) {
12361: # alias?
12362: 		  $ids=$bighash{'mapalias_'.$thisfn};
12363:               }
12364:               if ($ids) {
12365: # ------------------------------------------------------------------- Has ID(s)
12366:                  my @possibilities=split(/\,/,$ids);
12367:                  if ($#possibilities==0) {
12368: # ----------------------------------------------- There is only one possibility
12369: 		     my ($mapid,$resid)=split(/\./,$ids);
12370: 		     $syval=&encode_symb($bighash{'map_id_'.$mapid},
12371: 						    $resid,$thisfn);
12372:                      if (ref($possibles) eq 'HASH') {
12373:                          $possibles->{$syval} = 1;    
12374:                      }
12375:                      if ($checkforblock) {
12376:                          my @blockers = &has_comm_blocking('bre',$syval,$bighash{'src_'.$ids});
12377:                          if (@blockers) {
12378:                              $syval = '';
12379:                              return;
12380:                          }
12381:                      }
12382:                  } elsif ((!$donotrecurse) || ($checkforblock) || (ref($possibles) eq 'HASH')) { 
12383: # ------------------------------------------ There is more than one possibility
12384:                      my $realpossible=0;
12385:                      foreach my $id (@possibilities) {
12386: 			 my $file=$bighash{'src_'.$id};
12387:                          my $canaccess;
12388:                          if (($donotrecurse) || ($checkforblock) || (ref($possibles) eq 'HASH')) {
12389:                              $canaccess = 1;
12390:                          } else { 
12391:                              $canaccess = &allowed('bre',$file);
12392:                          }
12393:                          if ($canaccess) {
12394:          		     my ($mapid,$resid)=split(/\./,$id);
12395:                              if ($bighash{'map_type_'.$mapid} ne 'page') {
12396:                                  my $poss_syval=&encode_symb($bighash{'map_id_'.$mapid},
12397: 						             $resid,$thisfn);
12398:                                  if (ref($possibles) eq 'HASH') {
12399:                                      $possibles->{$syval} = 1;
12400:                                  }
12401:                                  if ($checkforblock) {
12402:                                      my @blockers = &has_comm_blocking('bre',$poss_syval,$file);
12403:                                      unless (@blockers > 0) {
12404:                                          $syval = $poss_syval;
12405:                                          $realpossible++;
12406:                                      }
12407:                                  } else {
12408:                                      $syval = $poss_syval;
12409:                                      $realpossible++;
12410:                                  }
12411:                              }
12412: 			 }
12413:                      }
12414: 		     if ($realpossible!=1) { $syval=''; }
12415:                  } else {
12416:                      $syval='';
12417:                  }
12418: 	      }
12419:               untie(%bighash);
12420:            }
12421:         }
12422:         if ($syval) {
12423: 	    return $env{$cache_str}=$syval;
12424:         }
12425:     }
12426:     &appenv({'request.ambiguous' => $thisfn});
12427:     return $env{$cache_str}='';
12428: }
12429: 
12430: # ---------------------------------------------------------- Return random seed
12431: 
12432: sub numval {
12433:     my $txt=shift;
12434:     $txt=~tr/A-J/0-9/;
12435:     $txt=~tr/a-j/0-9/;
12436:     $txt=~tr/K-T/0-9/;
12437:     $txt=~tr/k-t/0-9/;
12438:     $txt=~tr/U-Z/0-5/;
12439:     $txt=~tr/u-z/0-5/;
12440:     $txt=~s/\D//g;
12441:     if ($_64bit) { if ($txt > 2**32) { return -1; } }
12442:     return int($txt);
12443: }
12444: 
12445: sub numval2 {
12446:     my $txt=shift;
12447:     $txt=~tr/A-J/0-9/;
12448:     $txt=~tr/a-j/0-9/;
12449:     $txt=~tr/K-T/0-9/;
12450:     $txt=~tr/k-t/0-9/;
12451:     $txt=~tr/U-Z/0-5/;
12452:     $txt=~tr/u-z/0-5/;
12453:     $txt=~s/\D//g;
12454:     my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
12455:     my $total;
12456:     foreach my $val (@txts) { $total+=$val; }
12457:     if ($_64bit) { if ($total > 2**32) { return -1; } }
12458:     return int($total);
12459: }
12460: 
12461: sub numval3 {
12462:     use integer;
12463:     my $txt=shift;
12464:     $txt=~tr/A-J/0-9/;
12465:     $txt=~tr/a-j/0-9/;
12466:     $txt=~tr/K-T/0-9/;
12467:     $txt=~tr/k-t/0-9/;
12468:     $txt=~tr/U-Z/0-5/;
12469:     $txt=~tr/u-z/0-5/;
12470:     $txt=~s/\D//g;
12471:     my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
12472:     my $total;
12473:     foreach my $val (@txts) { $total+=$val; }
12474:     if ($_64bit) { $total=(($total<<32)>>32); }
12475:     return $total;
12476: }
12477: 
12478: sub digest {
12479:     my ($data)=@_;
12480:     my $digest=&Digest::MD5::md5($data);
12481:     my ($a,$b,$c,$d)=unpack("iiii",$digest);
12482:     my ($e,$f);
12483:     {
12484:         use integer;
12485:         $e=($a+$b);
12486:         $f=($c+$d);
12487:         if ($_64bit) {
12488:             $e=(($e<<32)>>32);
12489:             $f=(($f<<32)>>32);
12490:         }
12491:     }
12492:     if (wantarray) {
12493: 	return ($e,$f);
12494:     } else {
12495: 	my $g;
12496: 	{
12497: 	    use integer;
12498: 	    $g=($e+$f);
12499: 	    if ($_64bit) {
12500: 		$g=(($g<<32)>>32);
12501: 	    }
12502: 	}
12503: 	return $g;
12504:     }
12505: }
12506: 
12507: sub latest_rnd_algorithm_id {
12508:     return '64bit5';
12509: }
12510: 
12511: sub get_rand_alg {
12512:     my ($courseid)=@_;
12513:     if (!$courseid) { $courseid=(&whichuser())[1]; }
12514:     if ($courseid) {
12515: 	return $env{"course.$courseid.rndseed"};
12516:     }
12517:     return &latest_rnd_algorithm_id();
12518: }
12519: 
12520: sub validCODE {
12521:     my ($CODE)=@_;
12522:     if (defined($CODE) && $CODE ne '' && $CODE =~ /^\w+$/) { return 1; }
12523:     return 0;
12524: }
12525: 
12526: sub getCODE {
12527:     if (&validCODE($env{'form.CODE'})) { return $env{'form.CODE'}; }
12528:     if ( (defined($Apache::lonhomework::parsing_a_problem) ||
12529: 	  defined($Apache::lonhomework::parsing_a_task) ) &&
12530: 	 &validCODE($Apache::lonhomework::history{'resource.CODE'})) {
12531: 	return $Apache::lonhomework::history{'resource.CODE'};
12532:     }
12533:     return undef;
12534: }
12535: #
12536: #  Determines the random seed for a specific context:
12537: #
12538: # parameters:
12539: #   symb      - in course context the symb for the seed.
12540: #   course_id - The course id of the form domain_coursenum.
12541: #   domain    - Domain for the user.
12542: #   course    - Course for the user.
12543: #   cenv      - environment of the course.
12544: #
12545: # NOTE:
12546: #   All parameters are picked out of the environment if missing
12547: #   or not defined.
12548: #   If a symb cannot be determined the current time is used instead.
12549: #
12550: #  For a given well defined symb, courside, domain, username,
12551: #  and course environment, the seed is reproducible.
12552: #
12553: sub rndseed {
12554:     my ($symb,$courseid,$domain,$username, $cenv)=@_;
12555:     my ($wsymb,$wcourseid,$wdomain,$wusername)=&whichuser();
12556:     if (!defined($symb)) {
12557: 	unless ($symb=$wsymb) { return time; }
12558:     }
12559:     if (!defined $courseid) { 
12560: 	$courseid=$wcourseid; 
12561:     }
12562:     if (!defined $domain) { $domain=$wdomain; }
12563:     if (!defined $username) { $username=$wusername }
12564: 
12565:     my $which;
12566:     if (defined($cenv->{'rndseed'})) {
12567: 	$which = $cenv->{'rndseed'};
12568:     } else {
12569: 	$which =&get_rand_alg($courseid);
12570:     }
12571:     if (defined(&getCODE())) {
12572: 
12573: 	if ($which eq '64bit5') {
12574: 	    return &rndseed_CODE_64bit5($symb,$courseid,$domain,$username);
12575: 	} elsif ($which eq '64bit4') {
12576: 	    return &rndseed_CODE_64bit4($symb,$courseid,$domain,$username);
12577: 	} else {
12578: 	    return &rndseed_CODE_64bit($symb,$courseid,$domain,$username);
12579: 	}
12580:     } elsif ($which eq '64bit5') {
12581: 	return &rndseed_64bit5($symb,$courseid,$domain,$username);
12582:     } elsif ($which eq '64bit4') {
12583: 	return &rndseed_64bit4($symb,$courseid,$domain,$username);
12584:     } elsif ($which eq '64bit3') {
12585: 	return &rndseed_64bit3($symb,$courseid,$domain,$username);
12586:     } elsif ($which eq '64bit2') {
12587: 	return &rndseed_64bit2($symb,$courseid,$domain,$username);
12588:     } elsif ($which eq '64bit') {
12589: 	return &rndseed_64bit($symb,$courseid,$domain,$username);
12590:     }
12591:     return &rndseed_32bit($symb,$courseid,$domain,$username);
12592: }
12593: 
12594: sub rndseed_32bit {
12595:     my ($symb,$courseid,$domain,$username)=@_;
12596:     {
12597: 	use integer;
12598: 	my $symbchck=unpack("%32C*",$symb) << 27;
12599: 	my $symbseed=numval($symb) << 22;
12600: 	my $namechck=unpack("%32C*",$username) << 17;
12601: 	my $nameseed=numval($username) << 12;
12602: 	my $domainseed=unpack("%32C*",$domain) << 7;
12603: 	my $courseseed=unpack("%32C*",$courseid);
12604: 	my $num=$symbseed+$nameseed+$domainseed+$courseseed+$namechck+$symbchck;
12605: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
12606: 	#&logthis("rndseed :$num:$symb");
12607: 	if ($_64bit) { $num=(($num<<32)>>32); }
12608: 	return $num;
12609:     }
12610: }
12611: 
12612: sub rndseed_64bit {
12613:     my ($symb,$courseid,$domain,$username)=@_;
12614:     {
12615: 	use integer;
12616: 	my $symbchck=unpack("%32S*",$symb) << 21;
12617: 	my $symbseed=numval($symb) << 10;
12618: 	my $namechck=unpack("%32S*",$username);
12619: 	
12620: 	my $nameseed=numval($username) << 21;
12621: 	my $domainseed=unpack("%32S*",$domain) << 10;
12622: 	my $courseseed=unpack("%32S*",$courseid);
12623: 	
12624: 	my $num1=$symbchck+$symbseed+$namechck;
12625: 	my $num2=$nameseed+$domainseed+$courseseed;
12626: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
12627: 	#&logthis("rndseed :$num:$symb");
12628: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
12629: 	return "$num1,$num2";
12630:     }
12631: }
12632: 
12633: sub rndseed_64bit2 {
12634:     my ($symb,$courseid,$domain,$username)=@_;
12635:     {
12636: 	use integer;
12637: 	# strings need to be an even # of cahracters long, it it is odd the
12638:         # last characters gets thrown away
12639: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
12640: 	my $symbseed=numval($symb) << 10;
12641: 	my $namechck=unpack("%32S*",$username.' ');
12642: 	
12643: 	my $nameseed=numval($username) << 21;
12644: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
12645: 	my $courseseed=unpack("%32S*",$courseid.' ');
12646: 	
12647: 	my $num1=$symbchck+$symbseed+$namechck;
12648: 	my $num2=$nameseed+$domainseed+$courseseed;
12649: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
12650: 	#&logthis("rndseed :$num:$symb");
12651: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
12652: 	return "$num1,$num2";
12653:     }
12654: }
12655: 
12656: sub rndseed_64bit3 {
12657:     my ($symb,$courseid,$domain,$username)=@_;
12658:     {
12659: 	use integer;
12660: 	# strings need to be an even # of cahracters long, it it is odd the
12661:         # last characters gets thrown away
12662: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
12663: 	my $symbseed=numval2($symb) << 10;
12664: 	my $namechck=unpack("%32S*",$username.' ');
12665: 	
12666: 	my $nameseed=numval2($username) << 21;
12667: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
12668: 	my $courseseed=unpack("%32S*",$courseid.' ');
12669: 	
12670: 	my $num1=$symbchck+$symbseed+$namechck;
12671: 	my $num2=$nameseed+$domainseed+$courseseed;
12672: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
12673: 	#&logthis("rndseed :$num1:$num2:$_64bit");
12674: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
12675: 	
12676: 	return "$num1:$num2";
12677:     }
12678: }
12679: 
12680: sub rndseed_64bit4 {
12681:     my ($symb,$courseid,$domain,$username)=@_;
12682:     {
12683: 	use integer;
12684: 	# strings need to be an even # of cahracters long, it it is odd the
12685:         # last characters gets thrown away
12686: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
12687: 	my $symbseed=numval3($symb) << 10;
12688: 	my $namechck=unpack("%32S*",$username.' ');
12689: 	
12690: 	my $nameseed=numval3($username) << 21;
12691: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
12692: 	my $courseseed=unpack("%32S*",$courseid.' ');
12693: 	
12694: 	my $num1=$symbchck+$symbseed+$namechck;
12695: 	my $num2=$nameseed+$domainseed+$courseseed;
12696: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
12697: 	#&logthis("rndseed :$num1:$num2:$_64bit");
12698: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
12699: 	
12700: 	return "$num1:$num2";
12701:     }
12702: }
12703: 
12704: sub rndseed_64bit5 {
12705:     my ($symb,$courseid,$domain,$username)=@_;
12706:     my ($num1,$num2)=&digest("$symb,$courseid,$domain,$username");
12707:     return "$num1:$num2";
12708: }
12709: 
12710: sub rndseed_CODE_64bit {
12711:     my ($symb,$courseid,$domain,$username)=@_;
12712:     {
12713: 	use integer;
12714: 	my $symbchck=unpack("%32S*",$symb.' ') << 16;
12715: 	my $symbseed=numval2($symb);
12716: 	my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
12717: 	my $CODEseed=numval(&getCODE());
12718: 	my $courseseed=unpack("%32S*",$courseid.' ');
12719: 	my $num1=$symbseed+$CODEchck;
12720: 	my $num2=$CODEseed+$courseseed+$symbchck;
12721: 	#&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
12722: 	#&logthis("rndseed :$num1:$num2:$symb");
12723: 	if ($_64bit) { $num1=(($num1<<32)>>32); }
12724: 	if ($_64bit) { $num2=(($num2<<32)>>32); }
12725: 	return "$num1:$num2";
12726:     }
12727: }
12728: 
12729: sub rndseed_CODE_64bit4 {
12730:     my ($symb,$courseid,$domain,$username)=@_;
12731:     {
12732: 	use integer;
12733: 	my $symbchck=unpack("%32S*",$symb.' ') << 16;
12734: 	my $symbseed=numval3($symb);
12735: 	my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
12736: 	my $CODEseed=numval3(&getCODE());
12737: 	my $courseseed=unpack("%32S*",$courseid.' ');
12738: 	my $num1=$symbseed+$CODEchck;
12739: 	my $num2=$CODEseed+$courseseed+$symbchck;
12740: 	#&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
12741: 	#&logthis("rndseed :$num1:$num2:$symb");
12742: 	if ($_64bit) { $num1=(($num1<<32)>>32); }
12743: 	if ($_64bit) { $num2=(($num2<<32)>>32); }
12744: 	return "$num1:$num2";
12745:     }
12746: }
12747: 
12748: sub rndseed_CODE_64bit5 {
12749:     my ($symb,$courseid,$domain,$username)=@_;
12750:     my $code = &getCODE();
12751:     my ($num1,$num2)=&digest("$symb,$courseid,$code");
12752:     return "$num1:$num2";
12753: }
12754: 
12755: sub setup_random_from_rndseed {
12756:     my ($rndseed)=@_;
12757:     if ($rndseed =~/([,:])/) {
12758:         my ($num1,$num2) = map { abs($_); } (split(/[,:]/,$rndseed));
12759:         if ((!$num1) || (!$num2) || ($num1 > 2147483562) || ($num2 > 2147483398)) {
12760:             &Math::Random::random_set_seed_from_phrase($rndseed);
12761:         } else {
12762:             &Math::Random::random_set_seed($num1,$num2);
12763:         }
12764:     } else {
12765: 	&Math::Random::random_set_seed_from_phrase($rndseed);
12766:     }
12767: }
12768: 
12769: sub latest_receipt_algorithm_id {
12770:     return 'receipt3';
12771: }
12772: 
12773: sub recunique {
12774:     my $fucourseid=shift;
12775:     my $unique;
12776:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2' ||
12777: 	$env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
12778: 	$unique=$env{"course.$fucourseid.internal.encseed"};
12779:     } else {
12780: 	$unique=$perlvar{'lonReceipt'};
12781:     }
12782:     return unpack("%32C*",$unique);
12783: }
12784: 
12785: sub recprefix {
12786:     my $fucourseid=shift;
12787:     my $prefix;
12788:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2'||
12789: 	$env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
12790: 	$prefix=$env{"course.$fucourseid.internal.encpref"};
12791:     } else {
12792: 	$prefix=$perlvar{'lonHostID'};
12793:     }
12794:     return unpack("%32C*",$prefix);
12795: }
12796: 
12797: sub ireceipt {
12798:     my ($funame,$fudom,$fucourseid,$fusymb,$part)=@_;
12799: 
12800:     my $return =&recprefix($fucourseid).'-';
12801: 
12802:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt3' ||
12803: 	$env{'request.state'} eq 'construct') {
12804: 	$return .= (&digest("$funame,$fudom,$fucourseid,$fusymb,$part")%10000);
12805: 	return $return;
12806:     }
12807: 
12808:     my $cuname=unpack("%32C*",$funame);
12809:     my $cudom=unpack("%32C*",$fudom);
12810:     my $cucourseid=unpack("%32C*",$fucourseid);
12811:     my $cusymb=unpack("%32C*",$fusymb);
12812:     my $cunique=&recunique($fucourseid);
12813:     my $cpart=unpack("%32S*",$part);
12814:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2') {
12815: 
12816: 	#&logthis("doing receipt2  using parts $cpart, uname $cuname and udom $cudom gets  ".($cpart%$cuname)." and ".($cpart%$cudom));
12817: 			       
12818: 	$return.= ($cunique%$cuname+
12819: 		   $cunique%$cudom+
12820: 		   $cusymb%$cuname+
12821: 		   $cusymb%$cudom+
12822: 		   $cucourseid%$cuname+
12823: 		   $cucourseid%$cudom+
12824: 		   $cpart%$cuname+
12825: 		   $cpart%$cudom);
12826:     } else {
12827: 	$return.= ($cunique%$cuname+
12828: 		   $cunique%$cudom+
12829: 		   $cusymb%$cuname+
12830: 		   $cusymb%$cudom+
12831: 		   $cucourseid%$cuname+
12832: 		   $cucourseid%$cudom);
12833:     }
12834:     return $return;
12835: }
12836: 
12837: sub receipt {
12838:     my ($part)=@_;
12839:     my ($symb,$courseid,$domain,$name) = &whichuser();
12840:     return &ireceipt($name,$domain,$courseid,$symb,$part);
12841: }
12842: 
12843: sub whichuser {
12844:     my ($passedsymb)=@_;
12845:     my ($symb,$courseid,$domain,$name,$publicuser);
12846:     if (defined($env{'form.grade_symb'})) {
12847: 	my ($tmp_courseid)=&get_env_multiple('form.grade_courseid');
12848: 	my $allowed=&allowed('vgr',$tmp_courseid);
12849: 	if (!$allowed &&
12850: 	    exists($env{'request.course.sec'}) &&
12851: 	    $env{'request.course.sec'} !~ /^\s*$/) {
12852: 	    $allowed=&allowed('vgr',$tmp_courseid.
12853: 			      '/'.$env{'request.course.sec'});
12854: 	}
12855: 	if ($allowed) {
12856: 	    ($symb)=&get_env_multiple('form.grade_symb');
12857: 	    $courseid=$tmp_courseid;
12858: 	    ($domain)=&get_env_multiple('form.grade_domain');
12859: 	    ($name)=&get_env_multiple('form.grade_username');
12860: 	    return ($symb,$courseid,$domain,$name,$publicuser);
12861: 	}
12862:     }
12863:     if (!$passedsymb) {
12864: 	$symb=&symbread();
12865:     } else {
12866: 	$symb=$passedsymb;
12867:     }
12868:     $courseid=$env{'request.course.id'};
12869:     $domain=$env{'user.domain'};
12870:     $name=$env{'user.name'};
12871:     if ($name eq 'public' && $domain eq 'public') {
12872: 	if (!defined($env{'form.username'})) {
12873: 	    $env{'form.username'}.=time.rand(10000000);
12874: 	}
12875: 	$name.=$env{'form.username'};
12876:     }
12877:     return ($symb,$courseid,$domain,$name,$publicuser);
12878: 
12879: }
12880: 
12881: # ------------------------------------------------------------ Serves up a file
12882: # returns either the contents of the file or 
12883: # -1 if the file doesn't exist
12884: #
12885: # if the target is a file that was uploaded via DOCS, 
12886: # a check will be made to see if a current copy exists on the local server,
12887: # if it does this will be served, otherwise a copy will be retrieved from
12888: # the home server for the course and stored in /home/httpd/html/userfiles on
12889: # the local server.   
12890: 
12891: sub getfile {
12892:     my ($file) = @_;
12893:     if ($file =~ m -^/*(uploaded|editupload)/-) { $file=&filelocation("",$file); }
12894:     &repcopy($file);
12895:     return &readfile($file);
12896: }
12897: 
12898: sub repcopy_userfile {
12899:     my ($file)=@_;
12900:     my $londocroot = $perlvar{'lonDocRoot'};
12901:     if ($file =~ m{^/*(uploaded|editupload)/}) { $file=&filelocation("",$file); }
12902:     if ($file =~ m{^\Q/home/httpd/lonUsers/\E}) { return 'ok'; }
12903:     my ($cdom,$cnum,$filename) = 
12904: 	($file=~m|^\Q$perlvar{'lonDocRoot'}\E/+userfiles/+($match_domain)/+($match_name)/+(.*)|);
12905:     my $uri="/uploaded/$cdom/$cnum/$filename";
12906:     if (-e "$file") {
12907: # we already have a local copy, check it out
12908: 	my @fileinfo = stat($file);
12909: 	my $rtncode;
12910: 	my $info;
12911: 	my $lwpresp = &getuploaded('HEAD',$uri,$cdom,$cnum,\$info,\$rtncode);
12912: 	if ($lwpresp ne 'ok') {
12913: # there is no such file anymore, even though we had a local copy
12914: 	    if ($rtncode eq '404') {
12915: 		unlink($file);
12916: 	    }
12917: 	    return -1;
12918: 	}
12919: 	if ($info < $fileinfo[9]) {
12920: # nice, the file we have is up-to-date, just say okay
12921: 	    return 'ok';
12922: 	} else {
12923: # the file is outdated, get rid of it
12924: 	    unlink($file);
12925: 	}
12926:     }
12927: # one way or the other, at this point, we don't have the file
12928: # construct the correct path for the file
12929:     my @parts = ($cdom,$cnum); 
12930:     if ($filename =~ m|^(.+)/[^/]+$|) {
12931: 	push @parts, split(/\//,$1);
12932:     }
12933:     my $path = $perlvar{'lonDocRoot'}.'/userfiles';
12934:     foreach my $part (@parts) {
12935: 	$path .= '/'.$part;
12936: 	if (!-e $path) {
12937: 	    mkdir($path,0770);
12938: 	}
12939:     }
12940: # now the path exists for sure
12941: # get a user agent
12942:     my $transferfile=$file.'.in.transfer';
12943: # FIXME: this should flock
12944:     if (-e $transferfile) { return 'ok'; }
12945:     my $request;
12946:     $uri=~s/^\///;
12947:     my $homeserver = &homeserver($cnum,$cdom);
12948:     my $protocol = $protocol{$homeserver};
12949:     $protocol = 'http' if ($protocol ne 'https');
12950:     $request=new HTTP::Request('GET',$protocol.'://'.&hostname($homeserver).'/raw/'.$uri);
12951:     my $response = &LONCAPA::LWPReq::makerequest($homeserver,$request,$transferfile,\%perlvar,'',0,1);
12952: # did it work?
12953:     if ($response->is_error()) {
12954: 	unlink($transferfile);
12955: 	&logthis("Userfile repcopy failed for $uri");
12956: 	return -1;
12957:     }
12958: # worked, rename the transfer file
12959:     rename($transferfile,$file);
12960:     return 'ok';
12961: }
12962: 
12963: sub tokenwrapper {
12964:     my $uri=shift;
12965:     $uri=~s|^https?\://([^/]+)||;
12966:     $uri=~s|^/||;
12967:     $env{'user.environment'}=~/\/([^\/]+)\.id/;
12968:     my $token=$1;
12969:     my (undef,$udom,$uname,$file)=split('/',$uri,4);
12970:     if ($udom && $uname && $file) {
12971: 	$file=~s|(\?\.*)*$||;
12972:         &appenv({"userfile.$udom/$uname/$file" => $env{'request.course.id'}});
12973:         my $homeserver = &homeserver($uname,$udom);
12974:         my $protocol = $protocol{$homeserver};
12975:         $protocol = 'http' if ($protocol ne 'https');
12976:         return $protocol.'://'.&hostname($homeserver).'/'.$uri.
12977:                (($uri=~/\?/)?'&':'?').'token='.$token.
12978:                                '&tokenissued='.$perlvar{'lonHostID'};
12979:     } else {
12980:         return '/adm/notfound.html';
12981:     }
12982: }
12983: 
12984: # call with reqtype HEAD: get last modification time
12985: # call with reqtype GET: get the file contents
12986: # Do not call this with reqtype GET for large files! It loads everything into memory
12987: #
12988: sub getuploaded {
12989:     my ($reqtype,$uri,$cdom,$cnum,$info,$rtncode) = @_;
12990:     $uri=~s/^\///;
12991:     my $homeserver = &homeserver($cnum,$cdom);
12992:     my $protocol = $protocol{$homeserver};
12993:     $protocol = 'http' if ($protocol ne 'https');
12994:     $uri = $protocol.'://'.&hostname($homeserver).'/raw/'.$uri;
12995:     my $request=new HTTP::Request($reqtype,$uri);
12996:     my $response=&LONCAPA::LWPReq::makerequest($homeserver,$request,'',\%perlvar,'',0,1);
12997:     $$rtncode = $response->code;
12998:     if (! $response->is_success()) {
12999: 	return 'failed';
13000:     }      
13001:     if ($reqtype eq 'HEAD') {
13002: 	$$info = &HTTP::Date::str2time( $response->header('Last-modified') );
13003:     } elsif ($reqtype eq 'GET') {
13004: 	$$info = $response->content;
13005:     }
13006:     return 'ok';
13007: }
13008: 
13009: sub readfile {
13010:     my $file = shift;
13011:     if ( (! -e $file ) || ($file eq '') ) { return -1; };
13012:     my $fh;
13013:     open($fh,"<$file");
13014:     my $a='';
13015:     while (my $line = <$fh>) { $a .= $line; }
13016:     return $a;
13017: }
13018: 
13019: sub filelocation {
13020:     my ($dir,$file) = @_;
13021:     my $location;
13022:     $file=~ s/^\s*(\S+)\s*$/$1/; ## strip off leading and trailing spaces
13023: 
13024:     if ($file =~ m-^/adm/-) {
13025: 	$file=~s-^/adm/wrapper/-/-;
13026: 	$file=~s-^/adm/coursedocs/showdoc/-/-;
13027:     }
13028: 
13029:     if ($file =~ m-^\Q$Apache::lonnet::perlvar{'lonTabDir'}\E/-) {
13030:         $location = $file;
13031:     } elsif ($file=~/^\/*(uploaded|editupload)/) { # is an uploaded file
13032:         my ($udom,$uname,$filename)=
13033:   	    ($file=~m -^/+(?:uploaded|editupload)/+($match_domain)/+($match_name)/+(.*)$-);
13034:         my $home=&homeserver($uname,$udom);
13035:         my $is_me=0;
13036:         my @ids=&current_machine_ids();
13037:         foreach my $id (@ids) { if ($id eq $home) { $is_me=1; } }
13038:         if ($is_me) {
13039:   	    $location=propath($udom,$uname).'/userfiles/'.$filename;
13040:         } else {
13041:   	  $location=$Apache::lonnet::perlvar{'lonDocRoot'}.'/userfiles/'.
13042:   	      $udom.'/'.$uname.'/'.$filename;
13043:         }
13044:     } elsif ($file =~ m-^/adm/-) {
13045: 	$location = $perlvar{'lonDocRoot'}.'/'.$file;
13046:     } else {
13047:         $file=~s/^\Q$perlvar{'lonDocRoot'}\E//;
13048:         $file=~s:^/(res|priv)/:/:;
13049:         my $space=$1;
13050:         if ( !( $file =~ m:^/:) ) {
13051:             $location = $dir. '/'.$file;
13052:         } else {
13053:             $location = $perlvar{'lonDocRoot'}.'/'.$space.$file;
13054:         }
13055:     }
13056:     $location=~s://+:/:g; # remove duplicate /
13057:     while ($location=~m{/\.\./}) {
13058: 	if ($location =~ m{/[^/]+/\.\./}) {
13059: 	    $location=~ s{/[^/]+/\.\./}{/}g;
13060: 	} else {
13061: 	    $location=~ s{/\.\./}{/}g;
13062: 	}
13063:     } #remove dir/..
13064:     while ($location=~m:/\./:) {$location=~ s:/\./:/:g;} #remove /./
13065:     return $location;
13066: }
13067: 
13068: sub hreflocation {
13069:     my ($dir,$file)=@_;
13070:     unless (($file=~m-^https?\://-i) || ($file=~m-^/-)) {
13071: 	$file=filelocation($dir,$file);
13072:     } elsif ($file=~m-^/adm/-) {
13073: 	$file=~s-^/adm/wrapper/-/-;
13074: 	$file=~s-^/adm/coursedocs/showdoc/-/-;
13075:     }
13076:     if ($file=~m-^\Q$perlvar{'lonDocRoot'}\E-) {
13077: 	$file=~s-^\Q$perlvar{'lonDocRoot'}\E--;
13078:     } elsif ($file=~m-^\Q$perlvar{'lonUsersDir'}\E-) {
13079: 	$file=~s{^/home/httpd/lonUsers/($match_domain)/./././($match_name)/userfiles/}
13080: 	        {/uploaded/$1/$2/}x;
13081:     }
13082:     if ($file=~ m{^/userfiles/}) {
13083: 	$file =~ s{^/userfiles/}{/uploaded/};
13084:     }
13085:     return $file;
13086: }
13087: 
13088: 
13089: 
13090: 
13091: 
13092: sub current_machine_domains {
13093:     return &machine_domains(&hostname($perlvar{'lonHostID'}));
13094: }
13095: 
13096: sub machine_domains {
13097:     my ($hostname) = @_;
13098:     my @domains;
13099:     my %hostname = &all_hostnames();
13100:     while( my($id, $name) = each(%hostname)) {
13101: #	&logthis("-$id-$name-$hostname-");
13102: 	if ($hostname eq $name) {
13103: 	    push(@domains,&host_domain($id));
13104: 	}
13105:     }
13106:     return @domains;
13107: }
13108: 
13109: sub current_machine_ids {
13110:     return &machine_ids(&hostname($perlvar{'lonHostID'}));
13111: }
13112: 
13113: sub machine_ids {
13114:     my ($hostname) = @_;
13115:     $hostname ||= &hostname($perlvar{'lonHostID'});
13116:     my @ids;
13117:     my %name_to_host = &all_names();
13118:     if (ref($name_to_host{$hostname}) eq 'ARRAY') {
13119: 	return @{ $name_to_host{$hostname} };
13120:     }
13121:     return;
13122: }
13123: 
13124: sub additional_machine_domains {
13125:     my @domains;
13126:     open(my $fh,"<$perlvar{'lonTabDir'}/expected_domains.tab");
13127:     while( my $line = <$fh>) {
13128:         $line =~ s/\s//g;
13129:         push(@domains,$line);
13130:     }
13131:     return @domains;
13132: }
13133: 
13134: sub default_login_domain {
13135:     my $domain = $perlvar{'lonDefDomain'};
13136:     my $testdomain=(split(/\./,$ENV{'HTTP_HOST'}))[0];
13137:     foreach my $posdom (&current_machine_domains(),
13138:                         &additional_machine_domains()) {
13139:         if (lc($posdom) eq lc($testdomain)) {
13140:             $domain=$posdom;
13141:             last;
13142:         }
13143:     }
13144:     return $domain;
13145: }
13146: 
13147: # ------------------------------------------------------------- Declutters URLs
13148: 
13149: sub declutter {
13150:     my $thisfn=shift;
13151:     if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
13152:     unless ($thisfn=~m{^/home/httpd/html/priv/}) {
13153:         $thisfn=~s{^/home/httpd/html}{};
13154:     }
13155:     $thisfn=~s/^\///;
13156:     $thisfn=~s|^adm/wrapper/||;
13157:     $thisfn=~s|^adm/coursedocs/showdoc/||;
13158:     $thisfn=~s/^res\///;
13159:     $thisfn=~s/^priv\///;
13160:     unless (($thisfn =~ /^ext/) || ($thisfn =~ /\.(page|sequence)___\d+___ext/)) {
13161:         $thisfn=~s/\?.+$//;
13162:     }
13163:     return $thisfn;
13164: }
13165: 
13166: # ------------------------------------------------------------- Clutter up URLs
13167: 
13168: sub clutter {
13169:     my $thisfn='/'.&declutter(shift);
13170:     if ($thisfn !~ m{^/(uploaded|editupload|adm|userfiles|ext|raw|priv|public)/}
13171: 	|| $thisfn =~ m{^/adm/(includes|pages)} ) { 
13172:        $thisfn='/res'.$thisfn; 
13173:     }
13174:     if ($thisfn !~m|^/adm|) {
13175: 	if ($thisfn =~ m|^/ext/|) {
13176: 	    $thisfn='/adm/wrapper'.$thisfn;
13177: 	} else {
13178: 	    my ($ext) = ($thisfn =~ /\.(\w+)$/);
13179: 	    my $embstyle=&Apache::loncommon::fileembstyle($ext);
13180: 	    if ($embstyle eq 'ssi'
13181: 		|| ($embstyle eq 'hdn')
13182: 		|| ($embstyle eq 'rat')
13183: 		|| ($embstyle eq 'prv')
13184: 		|| ($embstyle eq 'ign')) {
13185: 		#do nothing with these
13186: 	    } elsif (($embstyle eq 'img') 
13187: 		|| ($embstyle eq 'emb')
13188: 		|| ($embstyle eq 'wrp')) {
13189: 		$thisfn='/adm/wrapper'.$thisfn;
13190: 	    } elsif ($embstyle eq 'unk'
13191: 		     && $thisfn!~/\.(sequence|page)$/) {
13192: 		$thisfn='/adm/coursedocs/showdoc'.$thisfn;
13193: 	    } else {
13194: #		&logthis("Got a blank emb style");
13195: 	    }
13196: 	}
13197:     } elsif ($thisfn =~ m{^/adm/$match_domain/$match_courseid/\d+/ext\.tool$}) {
13198:         $thisfn='/adm/wrapper'.$thisfn;
13199:     }
13200:     return $thisfn;
13201: }
13202: 
13203: sub clutter_with_no_wrapper {
13204:     my $uri = &clutter(shift);
13205:     if ($uri =~ m-^/adm/-) {
13206: 	$uri =~ s-^/adm/wrapper/-/-;
13207: 	$uri =~ s-^/adm/coursedocs/showdoc/-/-;
13208:     }
13209:     return $uri;
13210: }
13211: 
13212: sub freeze_escape {
13213:     my ($value)=@_;
13214:     if (ref($value)) {
13215: 	$value=&nfreeze($value);
13216: 	return '__FROZEN__'.&escape($value);
13217:     }
13218:     return &escape($value);
13219: }
13220: 
13221: 
13222: sub thaw_unescape {
13223:     my ($value)=@_;
13224:     if ($value =~ /^__FROZEN__/) {
13225: 	substr($value,0,10,undef);
13226: 	$value=&unescape($value);
13227: 	return &thaw($value);
13228:     }
13229:     return &unescape($value);
13230: }
13231: 
13232: sub correct_line_ends {
13233:     my ($result)=@_;
13234:     $$result =~s/\r\n/\n/mg;
13235:     $$result =~s/\r/\n/mg;
13236: }
13237: # ================================================================ Main Program
13238: 
13239: sub goodbye {
13240:    &logthis("Starting Shut down");
13241: #not converted to using infrastruture and probably shouldn't be
13242:    &logthis(sprintf("%-20s is %s",'%badServerCache',length(&nfreeze(\%badServerCache))));
13243: #converted
13244: #   &logthis(sprintf("%-20s is %s",'%metacache',scalar(%metacache)));
13245:    &logthis(sprintf("%-20s is %s",'%homecache',length(&nfreeze(\%homecache))));
13246: #   &logthis(sprintf("%-20s is %s",'%titlecache',length(&nfreeze(\%titlecache))));
13247: #   &logthis(sprintf("%-20s is %s",'%courseresdatacache',length(&nfreeze(\%courseresdatacache))));
13248: #1.1 only
13249: #   &logthis(sprintf("%-20s is %s",'%userresdatacache',length(&nfreeze(\%userresdatacache))));
13250: #   &logthis(sprintf("%-20s is %s",'%getsectioncache',length(&nfreeze(\%getsectioncache))));
13251: #   &logthis(sprintf("%-20s is %s",'%courseresversioncache',length(&nfreeze(\%courseresversioncache))));
13252: #   &logthis(sprintf("%-20s is %s",'%resversioncache',length(&nfreeze(\%resversioncache))));
13253:    &logthis(sprintf("%-20s is %s",'%remembered',length(&nfreeze(\%remembered))));
13254:    &logthis(sprintf("%-20s is %s",'kicks',$kicks));
13255:    &logthis(sprintf("%-20s is %s",'hits',$hits));
13256:    &flushcourselogs();
13257:    &logthis("Shutting down");
13258: }
13259: 
13260: sub get_dns {
13261:     my ($url,$func,$ignore_cache,$nocache,$hashref) = @_;
13262:     if (!$ignore_cache) {
13263: 	my ($content,$cached)=
13264: 	    &Apache::lonnet::is_cached_new('dns',$url);
13265: 	if ($cached) {
13266: 	    &$func($content,$hashref);
13267: 	    return;
13268: 	}
13269:     }
13270: 
13271:     my %alldns;
13272:     open(my $config,"<$perlvar{'lonTabDir'}/hosts.tab");
13273:     foreach my $dns (<$config>) {
13274: 	next if ($dns !~ /^\^(\S*)/x);
13275:         my $line = $1;
13276:         my ($host,$protocol) = split(/:/,$line);
13277:         if ($protocol ne 'https') {
13278:             $protocol = 'http';
13279:         }
13280: 	$alldns{$host} = $protocol;
13281:     }
13282:     while (%alldns) {
13283: 	my ($dns) = sort { $b cmp $a } keys(%alldns);
13284: 	my $request=new HTTP::Request('GET',"$alldns{$dns}://$dns$url");
13285:         my $response = &LONCAPA::LWPReq::makerequest('',$request,'',\%perlvar,30,0);
13286:         delete($alldns{$dns});
13287: 	next if ($response->is_error());
13288: 	my @content = split("\n",$response->content);
13289: 	unless ($nocache) {
13290: 	    &do_cache_new('dns',$url,\@content,30*24*60*60);
13291: 	}
13292: 	&$func(\@content,$hashref);
13293: 	return;
13294:     }
13295:     close($config);
13296:     my $which = (split('/',$url))[3];
13297:     &logthis("unable to contact DNS defaulting to on disk file dns_$which.tab\n");
13298:     open($config,"<$perlvar{'lonTabDir'}/dns_$which.tab");
13299:     my @content = <$config>;
13300:     &$func(\@content,$hashref);
13301:     return;
13302: }
13303: 
13304: # ------------------------------------------------------Get DNS checksums file
13305: sub parse_dns_checksums_tab {
13306:     my ($lines,$hashref) = @_;
13307:     my $lonhost = $perlvar{'lonHostID'};
13308:     my $machine_dom = &Apache::lonnet::host_domain($lonhost);
13309:     my $loncaparev = &get_server_loncaparev($machine_dom);
13310:     my $distro = (split(/\:/,&get_server_distarch($lonhost)))[0];
13311:     my $webconfdir = '/etc/httpd/conf';
13312:     if ($distro =~ /^(ubuntu|debian)(\d+)$/) {
13313:         $webconfdir = '/etc/apache2';
13314:     } elsif ($distro =~ /^sles(\d+)$/) {
13315:         if ($1 >= 10) {
13316:             $webconfdir = '/etc/apache2';
13317:         }
13318:     } elsif ($distro =~ /^suse(\d+\.\d+)$/) {
13319:         if ($1 >= 10.0) {
13320:             $webconfdir = '/etc/apache2';
13321:         }
13322:     }
13323:     my ($release,$timestamp) = split(/\-/,$loncaparev);
13324:     my (%chksum,%revnum);
13325:     if (ref($lines) eq 'ARRAY') {
13326:         chomp(@{$lines});
13327:         my $version = shift(@{$lines});
13328:         if ($version eq $release) {  
13329:             foreach my $line (@{$lines}) {
13330:                 my ($file,$version,$shasum) = split(/,/,$line);
13331:                 if ($file =~ m{^/etc/httpd/conf}) {
13332:                     if ($webconfdir eq '/etc/apache2') {
13333:                         $file =~ s{^\Q/etc/httpd/conf/\E}{$webconfdir/};
13334:                     }
13335:                 }
13336:                 $chksum{$file} = $shasum;
13337:                 $revnum{$file} = $version;
13338:             }
13339:             if (ref($hashref) eq 'HASH') {
13340:                 %{$hashref} = (
13341:                                 sums     => \%chksum,
13342:                                 versions => \%revnum,
13343:                               );
13344:             }
13345:         }
13346:     }
13347:     return;
13348: }
13349: 
13350: sub fetch_dns_checksums {
13351:     my %checksums;
13352:     my $machine_dom = &Apache::lonnet::host_domain($perlvar{'lonHostID'});
13353:     my $loncaparev = &get_server_loncaparev($machine_dom,$perlvar{'lonHostID'});
13354:     my ($release,$timestamp) = split(/\-/,$loncaparev);
13355:     &get_dns("/adm/dns/checksums/$release",\&parse_dns_checksums_tab,1,1,
13356:              \%checksums);
13357:     return \%checksums;
13358: }
13359: 
13360: # ------------------------------------------------------------ Read domain file
13361: {
13362:     my $loaded;
13363:     my %domain;
13364: 
13365:     sub parse_domain_tab {
13366: 	my ($lines) = @_;
13367: 	foreach my $line (@$lines) {
13368: 	    next if ($line =~ /^(\#|\s*$ )/x);
13369: 
13370: 	    chomp($line);
13371: 	    my ($name,@elements) = split(/:/,$line,9);
13372: 	    my %this_domain;
13373: 	    foreach my $field ('description', 'auth_def', 'auth_arg_def',
13374: 			       'lang_def', 'city', 'longi', 'lati',
13375: 			       'primary') {
13376: 		$this_domain{$field} = shift(@elements);
13377: 	    }
13378: 	    $domain{$name} = \%this_domain;
13379: 	}
13380:     }
13381: 
13382:     sub reset_domain_info {
13383: 	undef($loaded);
13384: 	undef(%domain);
13385:     }
13386: 
13387:     sub load_domain_tab {
13388: 	my ($ignore_cache,$nocache) = @_;
13389: 	&get_dns('/adm/dns/domain',\&parse_domain_tab,$ignore_cache,$nocache);
13390: 	my $fh;
13391: 	if (open($fh,"<".$perlvar{'lonTabDir'}.'/domain.tab')) {
13392: 	    my @lines = <$fh>;
13393: 	    &parse_domain_tab(\@lines);
13394: 	}
13395: 	close($fh);
13396: 	$loaded = 1;
13397:     }
13398: 
13399:     sub domain {
13400: 	&load_domain_tab() if (!$loaded);
13401: 
13402: 	my ($name,$what) = @_;
13403: 	return if ( !exists($domain{$name}) );
13404: 
13405: 	if (!$what) {
13406: 	    return $domain{$name}{'description'};
13407: 	}
13408: 	return $domain{$name}{$what};
13409:     }
13410: 
13411:     sub domain_info {
13412:         &load_domain_tab() if (!$loaded);
13413:         return %domain;
13414:     }
13415: 
13416: }
13417: 
13418: 
13419: # ------------------------------------------------------------- Read hosts file
13420: {
13421:     my %hostname;
13422:     my %hostdom;
13423:     my %libserv;
13424:     my $loaded;
13425:     my %name_to_host;
13426:     my %internetdom;
13427:     my %LC_dns_serv;
13428: 
13429:     sub parse_hosts_tab {
13430: 	my ($file) = @_;
13431: 	foreach my $configline (@$file) {
13432: 	    next if ($configline =~ /^(\#|\s*$ )/x);
13433:             chomp($configline);
13434: 	    if ($configline =~ /^\^/) {
13435:                 if ($configline =~ /^\^([\w.\-]+)/) {
13436:                     $LC_dns_serv{$1} = 1;
13437:                 }
13438:                 next;
13439:             }
13440: 	    my ($id,$domain,$role,$name,$protocol,$intdom)=split(/:/,$configline);
13441: 	    $name=~s/\s//g;
13442: 	    if ($id && $domain && $role && $name) {
13443:                 if ((exists($hostname{$id})) && ($hostname{$id} ne '')) {
13444:                     my $curr = $hostname{$id};
13445:                     my $skip;
13446:                     if (ref($name_to_host{$curr}) eq 'ARRAY') {
13447:                         if (($curr eq $name) && (@{$name_to_host{$curr}} == 1)) {
13448:                             $skip = 1;
13449:                         } else {
13450:                             @{$name_to_host{$curr}} = grep { $_ ne $id } @{$name_to_host{$curr}};
13451:                         }
13452:                     }
13453:                     unless ($skip) {
13454:                         push(@{$name_to_host{$name}},$id);
13455:                     }
13456:                 } else {
13457:                     push(@{$name_to_host{$name}},$id);
13458:                 }
13459: 		$hostname{$id}=$name;
13460: 		$hostdom{$id}=$domain;
13461: 		if ($role eq 'library') { $libserv{$id}=$name; }
13462:                 if (defined($protocol)) {
13463:                     if ($protocol eq 'https') {
13464:                         $protocol{$id} = $protocol;
13465:                     } else {
13466:                         $protocol{$id} = 'http'; 
13467:                     }
13468:                 } else {
13469:                     $protocol{$id} = 'http';
13470:                 }
13471:                 if (defined($intdom)) {
13472:                     $internetdom{$id} = $intdom;
13473:                 }
13474: 	    }
13475: 	}
13476:     }
13477:     
13478:     sub reset_hosts_info {
13479: 	&purge_remembered();
13480: 	&reset_domain_info();
13481: 	&reset_hosts_ip_info();
13482:         undef(%internetdom);
13483: 	undef(%name_to_host);
13484: 	undef(%hostname);
13485: 	undef(%hostdom);
13486: 	undef(%libserv);
13487: 	undef($loaded);
13488:     }
13489: 
13490:     sub load_hosts_tab {
13491: 	my ($ignore_cache,$nocache) = @_;
13492: 	&get_dns('/adm/dns/hosts',\&parse_hosts_tab,$ignore_cache,$nocache);
13493: 	open(my $config,"<$perlvar{'lonTabDir'}/hosts.tab");
13494: 	my @config = <$config>;
13495: 	&parse_hosts_tab(\@config);
13496: 	close($config);
13497: 	$loaded=1;
13498:     }
13499: 
13500:     sub hostname {
13501: 	&load_hosts_tab() if (!$loaded);
13502: 
13503: 	my ($lonid) = @_;
13504: 	return $hostname{$lonid};
13505:     }
13506: 
13507:     sub all_hostnames {
13508: 	&load_hosts_tab() if (!$loaded);
13509: 
13510: 	return %hostname;
13511:     }
13512: 
13513:     sub all_names {
13514:         my ($ignore_cache,$nocache) = @_;
13515: 	&load_hosts_tab($ignore_cache,$nocache) if (!$loaded);
13516: 
13517: 	return %name_to_host;
13518:     }
13519: 
13520:     sub all_host_domain {
13521:         &load_hosts_tab() if (!$loaded);
13522:         return %hostdom;
13523:     }
13524: 
13525:     sub all_host_intdom {
13526:         &load_hosts_tab() if (!$loaded);
13527:         return %internetdom;
13528:     }
13529: 
13530:     sub is_library {
13531: 	&load_hosts_tab() if (!$loaded);
13532: 
13533: 	return exists($libserv{$_[0]});
13534:     }
13535: 
13536:     sub all_library {
13537: 	&load_hosts_tab() if (!$loaded);
13538: 
13539: 	return %libserv;
13540:     }
13541: 
13542:     sub unique_library {
13543: 	#2x reverse removes all hostnames that appear more than once
13544:         my %unique = reverse &all_library();
13545:         return reverse %unique;
13546:     }
13547: 
13548:     sub get_servers {
13549: 	&load_hosts_tab() if (!$loaded);
13550: 
13551: 	my ($domain,$type) = @_;
13552: 	my %possible_hosts = ($type eq 'library') ? %libserv
13553: 	                                          : %hostname;
13554: 	my %result;
13555: 	if (ref($domain) eq 'ARRAY') {
13556: 	    while ( my ($host,$hostname) = each(%possible_hosts)) {
13557: 		if (grep(/^\Q$hostdom{$host}\E$/,@$domain)) {
13558: 		    $result{$host} = $hostname;
13559: 		}
13560: 	    }
13561: 	} else {
13562: 	    while ( my ($host,$hostname) = each(%possible_hosts)) {
13563: 		if ($hostdom{$host} eq $domain) {
13564: 		    $result{$host} = $hostname;
13565: 		}
13566: 	    }
13567: 	}
13568: 	return %result;
13569:     }
13570: 
13571:     sub get_unique_servers {
13572:         my %unique = reverse &get_servers(@_);
13573: 	return reverse %unique;
13574:     }
13575: 
13576:     sub host_domain {
13577: 	&load_hosts_tab() if (!$loaded);
13578: 
13579: 	my ($lonid) = @_;
13580: 	return $hostdom{$lonid};
13581:     }
13582: 
13583:     sub all_domains {
13584: 	&load_hosts_tab() if (!$loaded);
13585: 
13586: 	my %seen;
13587: 	my @uniq = grep(!$seen{$_}++, values(%hostdom));
13588: 	return @uniq;
13589:     }
13590: 
13591:     sub internet_dom {
13592:         &load_hosts_tab() if (!$loaded);
13593: 
13594:         my ($lonid) = @_;
13595:         return $internetdom{$lonid};
13596:     }
13597: 
13598:     sub is_LC_dns {
13599:         &load_hosts_tab() if (!$loaded);
13600: 
13601:         my ($hostname) = @_;
13602:         return exists($LC_dns_serv{$hostname});
13603:     }
13604: 
13605: }
13606: 
13607: { 
13608:     my %iphost;
13609:     my %name_to_ip;
13610:     my %lonid_to_ip;
13611: 
13612:     sub get_hosts_from_ip {
13613: 	my ($ip) = @_;
13614: 	my %iphosts = &get_iphost();
13615: 	if (ref($iphosts{$ip})) {
13616: 	    return @{$iphosts{$ip}};
13617: 	}
13618: 	return;
13619:     }
13620:     
13621:     sub reset_hosts_ip_info {
13622: 	undef(%iphost);
13623: 	undef(%name_to_ip);
13624: 	undef(%lonid_to_ip);
13625:     }
13626: 
13627:     sub get_host_ip {
13628: 	my ($lonid) = @_;
13629: 	if (exists($lonid_to_ip{$lonid})) {
13630: 	    return $lonid_to_ip{$lonid};
13631: 	}
13632: 	my $name=&hostname($lonid);
13633:    	my $ip = gethostbyname($name);
13634: 	return if (!$ip || length($ip) ne 4);
13635: 	$ip=inet_ntoa($ip);
13636: 	$name_to_ip{$name}   = $ip;
13637: 	$lonid_to_ip{$lonid} = $ip;
13638: 	return $ip;
13639:     }
13640:     
13641:     sub get_iphost {
13642: 	my ($ignore_cache,$nocache) = @_;
13643: 
13644: 	if (!$ignore_cache) {
13645: 	    if (%iphost) {
13646: 		return %iphost;
13647: 	    }
13648: 	    my ($ip_info,$cached)=
13649: 		&Apache::lonnet::is_cached_new('iphost','iphost');
13650: 	    if ($cached) {
13651: 		%iphost      = %{$ip_info->[0]};
13652: 		%name_to_ip  = %{$ip_info->[1]};
13653: 		%lonid_to_ip = %{$ip_info->[2]};
13654: 		return %iphost;
13655: 	    }
13656: 	}
13657: 
13658: 	# get yesterday's info for fallback
13659: 	my %old_name_to_ip;
13660: 	my ($ip_info,$cached)=
13661: 	    &Apache::lonnet::is_cached_new('iphost','iphost');
13662: 	if ($cached) {
13663: 	    %old_name_to_ip = %{$ip_info->[1]};
13664: 	}
13665: 
13666: 	my %name_to_host = &all_names($ignore_cache,$nocache);
13667: 	foreach my $name (keys(%name_to_host)) {
13668: 	    my $ip;
13669: 	    if (!exists($name_to_ip{$name})) {
13670: 		$ip = gethostbyname($name);
13671: 		if (!$ip || length($ip) ne 4) {
13672: 		    if (defined($old_name_to_ip{$name})) {
13673: 			$ip = $old_name_to_ip{$name};
13674: 			&logthis("Can't find $name defaulting to old $ip");
13675: 		    } else {
13676: 			&logthis("Name $name no IP found");
13677: 			next;
13678: 		    }
13679: 		} else {
13680: 		    $ip=inet_ntoa($ip);
13681: 		}
13682: 		$name_to_ip{$name} = $ip;
13683: 	    } else {
13684: 		$ip = $name_to_ip{$name};
13685: 	    }
13686: 	    foreach my $id (@{ $name_to_host{$name} }) {
13687: 		$lonid_to_ip{$id} = $ip;
13688: 	    }
13689: 	    push(@{$iphost{$ip}},@{$name_to_host{$name}});
13690: 	}
13691:         unless ($nocache) {
13692: 	    &do_cache_new('iphost','iphost',
13693: 		          [\%iphost,\%name_to_ip,\%lonid_to_ip],
13694: 		          48*60*60);
13695:         }
13696: 
13697: 	return %iphost;
13698:     }
13699: 
13700:     #
13701:     #  Given a DNS returns the loncapa host name for that DNS 
13702:     # 
13703:     sub host_from_dns {
13704:         my ($dns) = @_;
13705:         my @hosts;
13706:         my $ip;
13707: 
13708:         if (exists($name_to_ip{$dns})) {
13709:             $ip = $name_to_ip{$dns};
13710:         }
13711:         if (!$ip) {
13712:             $ip = gethostbyname($dns); # Initial translation to IP is in net order.
13713:             if (length($ip) == 4) { 
13714: 	        $ip   = &IO::Socket::inet_ntoa($ip);
13715:             }
13716:         }
13717:         if ($ip) {
13718: 	    @hosts = get_hosts_from_ip($ip);
13719: 	    return $hosts[0];
13720:         }
13721:         return undef;
13722:     }
13723: 
13724:     sub get_internet_names {
13725:         my ($lonid) = @_;
13726:         return if ($lonid eq '');
13727:         my ($idnref,$cached)=
13728:             &Apache::lonnet::is_cached_new('internetnames',$lonid);
13729:         if ($cached) {
13730:             return $idnref;
13731:         }
13732:         my $ip = &get_host_ip($lonid);
13733:         my @hosts = &get_hosts_from_ip($ip);
13734:         my %iphost = &get_iphost();
13735:         my (@idns,%seen);
13736:         foreach my $id (@hosts) {
13737:             my $dom = &host_domain($id);
13738:             my $prim_id = &domain($dom,'primary');
13739:             my $prim_ip = &get_host_ip($prim_id);
13740:             next if ($seen{$prim_ip});
13741:             if (ref($iphost{$prim_ip}) eq 'ARRAY') {
13742:                 foreach my $id (@{$iphost{$prim_ip}}) {
13743:                     my $intdom = &internet_dom($id);
13744:                     unless (grep(/^\Q$intdom\E$/,@idns)) {
13745:                         push(@idns,$intdom);
13746:                     }
13747:                 }
13748:             }
13749:             $seen{$prim_ip} = 1;
13750:         }
13751:         return &do_cache_new('internetnames',$lonid,\@idns,12*60*60);
13752:     }
13753: 
13754: }
13755: 
13756: sub all_loncaparevs {
13757:     return qw(1.1 1.2 1.3 2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 2.10 2.11);
13758: }
13759: 
13760: # ---------------------------------------------------------- Read loncaparev table
13761: {
13762:     sub load_loncaparevs { 
13763:         if (-e "$perlvar{'lonTabDir'}/loncaparevs.tab") {
13764:             if (open(my $config,"<$perlvar{'lonTabDir'}/loncaparevs.tab")) {
13765:                 while (my $configline=<$config>) {
13766:                     chomp($configline);
13767:                     my ($hostid,$loncaparev)=split(/:/,$configline);
13768:                     $loncaparevs{$hostid}=$loncaparev;
13769:                 }
13770:                 close($config);
13771:             }
13772:         }
13773:     }
13774: }
13775: 
13776: # ---------------------------------------------------------- Read serverhostID table
13777: {
13778:     sub load_serverhomeIDs {
13779:         if (-e "$perlvar{'lonTabDir'}/serverhomeIDs.tab") {
13780:             if (open(my $config,"<$perlvar{'lonTabDir'}/serverhomeIDs.tab")) {
13781:                 while (my $configline=<$config>) {
13782:                     chomp($configline);
13783:                     my ($name,$id)=split(/:/,$configline);
13784:                     $serverhomeIDs{$name}=$id;
13785:                 }
13786:                 close($config);
13787:             }
13788:         }
13789:     }
13790: }
13791: 
13792: 
13793: BEGIN {
13794: 
13795: # ----------------------------------- Read loncapa.conf and loncapa_apache.conf
13796:     unless ($readit) {
13797: {
13798:     my $configvars = LONCAPA::Configuration::read_conf('loncapa.conf');
13799:     %perlvar = (%perlvar,%{$configvars});
13800: }
13801: 
13802: 
13803: # ------------------------------------------------------ Read spare server file
13804: {
13805:     open(my $config,"<$perlvar{'lonTabDir'}/spare.tab");
13806: 
13807:     while (my $configline=<$config>) {
13808:        chomp($configline);
13809:        if ($configline) {
13810: 	   my ($host,$type) = split(':',$configline,2);
13811: 	   if (!defined($type) || $type eq '') { $type = 'default' };
13812: 	   push(@{ $spareid{$type} }, $host);
13813:        }
13814:     }
13815:     close($config);
13816: }
13817: # ------------------------------------------------------------ Read permissions
13818: {
13819:     open(my $config,"<$perlvar{'lonTabDir'}/roles.tab");
13820: 
13821:     while (my $configline=<$config>) {
13822: 	chomp($configline);
13823: 	if ($configline) {
13824: 	    my ($role,$perm)=split(/ /,$configline);
13825: 	    if ($perm ne '') { $pr{$role}=$perm; }
13826: 	}
13827:     }
13828:     close($config);
13829: }
13830: 
13831: # -------------------------------------------- Read plain texts for permissions
13832: {
13833:     open(my $config,"<$perlvar{'lonTabDir'}/rolesplain.tab");
13834: 
13835:     while (my $configline=<$config>) {
13836: 	chomp($configline);
13837: 	if ($configline) {
13838: 	    my ($short,@plain)=split(/:/,$configline);
13839:             %{$prp{$short}} = ();
13840: 	    if (@plain > 0) {
13841:                 $prp{$short}{'std'} = $plain[0];
13842:                 for (my $i=1; $i<@plain; $i++) {
13843:                     $prp{$short}{'alt'.$i} = $plain[$i];  
13844:                 }
13845:             }
13846: 	}
13847:     }
13848:     close($config);
13849: }
13850: 
13851: # ---------------------------------------------------------- Read package table
13852: {
13853:     open(my $config,"<$perlvar{'lonTabDir'}/packages.tab");
13854: 
13855:     while (my $configline=<$config>) {
13856: 	if ($configline !~ /\S/ || $configline=~/^#/) { next; }
13857: 	chomp($configline);
13858: 	my ($short,$plain)=split(/:/,$configline);
13859: 	my ($pack,$name)=split(/\&/,$short);
13860: 	if ($plain ne '') {
13861: 	    $packagetab{$pack.'&'.$name.'&name'}=$name; 
13862: 	    $packagetab{$short}=$plain; 
13863: 	}
13864:     }
13865:     close($config);
13866: }
13867: 
13868: # ---------------------------------------------------------- Read loncaparev table
13869: 
13870: &load_loncaparevs();
13871: 
13872: # ---------------------------------------------------------- Read serverhostID table
13873: 
13874: &load_serverhomeIDs();
13875: 
13876: # ---------------------------------------------------------- Read releaseslist XML
13877: {
13878:     my $file = $Apache::lonnet::perlvar{'lonTabDir'}.'/releaseslist.xml';
13879:     if (-e $file) {
13880:         my $parser = HTML::LCParser->new($file);
13881:         while (my $token = $parser->get_token()) {
13882:             if ($token->[0] eq 'S') {
13883:                 my $item = $token->[1];
13884:                 my $name = $token->[2]{'name'};
13885:                 my $value = $token->[2]{'value'};
13886:                 my $valuematch = $token->[2]{'valuematch'};
13887:                 my $namematch = $token->[2]{'namematch'};
13888:                 if ($item eq 'parameter') {
13889:                     if (($namematch ne '') || (($name ne '') && ($value ne '' || $valuematch ne ''))) {
13890:                         my $release = $parser->get_text();
13891:                         $release =~ s/(^\s*|\s*$ )//gx;
13892:                         $needsrelease{$item.':'.$name.':'.$value.':'.$valuematch.':'.$namematch} = $release;
13893:                     }
13894:                 } elsif ($item ne '' && $name ne '') {
13895:                     my $release = $parser->get_text();
13896:                     $release =~ s/(^\s*|\s*$ )//gx;
13897:                     $needsrelease{$item.':'.$name.':'.$value} = $release;
13898:                 }
13899:             }
13900:         }
13901:     }
13902: }
13903: 
13904: # ---------------------------------------------------------- Read managers table
13905: {
13906:     if (-e "$perlvar{'lonTabDir'}/managers.tab") {
13907:         if (open(my $config,"<$perlvar{'lonTabDir'}/managers.tab")) {
13908:             while (my $configline=<$config>) {
13909:                 chomp($configline);
13910:                 next if ($configline =~ /^\#/);
13911:                 if (($configline =~ /^[\w\-]+$/) || ($configline =~ /^[\w\-]+\:[\w\-]+$/)) {
13912:                     $managerstab{$configline} = 1;
13913:                 }
13914:             }
13915:             close($config);
13916:         }
13917:     }
13918: }
13919: 
13920: # ------------- set up temporary directory
13921: {
13922:     $tmpdir = LONCAPA::tempdir();
13923: 
13924: }
13925: 
13926: $memcache=new Cache::Memcached({'servers'           => ['127.0.0.1:11211'],
13927: 				'compress_threshold'=> 20_000,
13928:  			        });
13929: 
13930: $processmarker='_'.time.'_'.$perlvar{'lonHostID'};
13931: $dumpcount=0;
13932: $locknum=0;
13933: 
13934: &logtouch();
13935: &logthis('<font color="yellow">INFO: Read configuration</font>');
13936: $readit=1;
13937:     {
13938: 	use integer;
13939: 	my $test=(2**32)+1;
13940: 	if ($test != 0) { $_64bit=1; } else { $_64bit=0; }
13941: 	&logthis(" Detected 64bit platform ($_64bit)");
13942:     }
13943: }
13944: }
13945: 
13946: 1;
13947: __END__
13948: 
13949: =pod
13950: 
13951: =head1 NAME
13952: 
13953: Apache::lonnet - Subroutines to ask questions about things in the network.
13954: 
13955: =head1 SYNOPSIS
13956: 
13957: Invoked by other LON-CAPA modules, when they need to talk to or about objects in the network.
13958: 
13959:  &Apache::lonnet::SUBROUTINENAME(ARGUMENTS);
13960: 
13961: Common parameters:
13962: 
13963: =over 4
13964: 
13965: =item *
13966: 
13967: $uname : an internal username (if $cname expecting a course Id specifically)
13968: 
13969: =item *
13970: 
13971: $udom : a domain (if $cdom expecting a course's domain specifically)
13972: 
13973: =item *
13974: 
13975: $symb : a resource instance identifier
13976: 
13977: =item *
13978: 
13979: $namespace : the name of a .db file that contains the data needed or
13980: being set.
13981: 
13982: =back
13983: 
13984: =head1 OVERVIEW
13985: 
13986: lonnet provides subroutines which interact with the
13987: lonc/lond (TCP) network layer of LON-CAPA. They can be used to ask
13988: about classes, users, and resources.
13989: 
13990: For many of these objects you can also use this to store data about
13991: them or modify them in various ways.
13992: 
13993: =head2 Symbs
13994: 
13995: To identify a specific instance of a resource, LON-CAPA uses symbols
13996: or "symbs"X<symb>. These identifiers are built from the URL of the
13997: map, the resource number of the resource in the map, and the URL of
13998: the resource itself. The latter is somewhat redundant, but might help
13999: if maps change.
14000: 
14001: An example is
14002: 
14003:  msu/korte/parts/part1.sequence___19___msu/korte/tests/part12.problem
14004: 
14005: The respective map entry is
14006: 
14007:  <resource id="19" src="/res/msu/korte/tests/part12.problem"
14008:   title="Problem 2">
14009:  </resource>
14010: 
14011: Symbs are used by the random number generator, as well as to store and
14012: restore data specific to a certain instance of for example a problem.
14013: 
14014: =head2 Storing And Retrieving Data
14015: 
14016: X<store()>X<cstore()>X<restore()>Three of the most important functions
14017: in C<lonnet.pm> are C<&Apache::lonnet::cstore()>,
14018: C<&Apache::lonnet:restore()>, and C<&Apache::lonnet::store()>, which
14019: is is the non-critical message twin of cstore. These functions are for
14020: handlers to store a perl hash to a user's permanent data space in an
14021: easy manner, and to retrieve it again on another call. It is expected
14022: that a handler would use this once at the beginning to retrieve data,
14023: and then again once at the end to send only the new data back.
14024: 
14025: The data is stored in the user's data directory on the user's
14026: homeserver under the ID of the course.
14027: 
14028: The hash that is returned by restore will have all of the previous
14029: value for all of the elements of the hash.
14030: 
14031: Example:
14032: 
14033:  #creating a hash
14034:  my %hash;
14035:  $hash{'foo'}='bar';
14036: 
14037:  #storing it
14038:  &Apache::lonnet::cstore(\%hash);
14039: 
14040:  #changing a value
14041:  $hash{'foo'}='notbar';
14042: 
14043:  #adding a new value
14044:  $hash{'bar'}='foo';
14045:  &Apache::lonnet::cstore(\%hash);
14046: 
14047:  #retrieving the hash
14048:  my %history=&Apache::lonnet::restore();
14049: 
14050:  #print the hash
14051:  foreach my $key (sort(keys(%history))) {
14052:    print("\%history{$key} = $history{$key}");
14053:  }
14054: 
14055: Will print out:
14056: 
14057:  %history{1:foo} = bar
14058:  %history{1:keys} = foo:timestamp
14059:  %history{1:timestamp} = 990455579
14060:  %history{2:bar} = foo
14061:  %history{2:foo} = notbar
14062:  %history{2:keys} = foo:bar:timestamp
14063:  %history{2:timestamp} = 990455580
14064:  %history{bar} = foo
14065:  %history{foo} = notbar
14066:  %history{timestamp} = 990455580
14067:  %history{version} = 2
14068: 
14069: Note that the special hash entries C<keys>, C<version> and
14070: C<timestamp> were added to the hash. C<version> will be equal to the
14071: total number of versions of the data that have been stored. The
14072: C<timestamp> attribute will be the UNIX time the hash was
14073: stored. C<keys> is available in every historical section to list which
14074: keys were added or changed at a specific historical revision of a
14075: hash.
14076: 
14077: B<Warning>: do not store the hash that restore returns directly. This
14078: will cause a mess since it will restore the historical keys as if the
14079: were new keys. I.E. 1:foo will become 1:1:foo etc.
14080: 
14081: Calling convention:
14082: 
14083:  my %record=&Apache::lonnet::restore($symb,$courseid,$domain,$uname);
14084:  &Apache::lonnet::cstore(\%newrecord,$symb,$courseid,$domain,$uname,$laststore);
14085: 
14086: For more detailed information, see lonnet specific documentation.
14087: 
14088: =head1 RETURN MESSAGES
14089: 
14090: =over 4
14091: 
14092: =item * B<con_lost>: unable to contact remote host
14093: 
14094: =item * B<con_delayed>: unable to contact remote host, message will be delivered
14095: when the connection is brought back up
14096: 
14097: =item * B<con_failed>: unable to contact remote host and unable to save message
14098: for later delivery
14099: 
14100: =item * B<error:>: an error a occurred, a description of the error follows the :
14101: 
14102: =item * B<no_such_host>: unable to fund a host associated with the user/domain
14103: that was requested
14104: 
14105: =back
14106: 
14107: =head1 PUBLIC SUBROUTINES
14108: 
14109: =head2 Session Environment Functions
14110: 
14111: =over 4
14112: 
14113: =item * 
14114: X<appenv()>
14115: B<appenv($hashref,$rolesarrayref)>: the value of %{$hashref} is written to
14116: the user envirnoment file, and will be restored for each access this
14117: user makes during this session, also modifies the %env for the current
14118: process. Optional rolesarrayref - if defined contains a reference to an array
14119: of roles which are exempt from the restriction on modifying user.role entries 
14120: in the user's environment.db and in %env.    
14121: 
14122: =item *
14123: X<delenv()>
14124: B<delenv($delthis,$regexp)>: removes all items from the session
14125: environment file that begin with $delthis. If the 
14126: optional second arg - $regexp - is true, $delthis is treated as a 
14127: regular expression, otherwise \Q$delthis\E is used. 
14128: The values are also deleted from the current processes %env.
14129: 
14130: =item * get_env_multiple($name) 
14131: 
14132: gets $name from the %env hash, it seemlessly handles the cases where multiple
14133: values may be defined and end up as an array ref.
14134: 
14135: returns an array of values
14136: 
14137: =back
14138: 
14139: =head2 User Information
14140: 
14141: =over 4
14142: 
14143: =item *
14144: X<queryauthenticate()>
14145: B<queryauthenticate($uname,$udom)>: try to determine user's current 
14146: authentication scheme
14147: 
14148: =item *
14149: X<authenticate()>
14150: B<authenticate($uname,$upass,$udom,$checkdefauth,$clientcancheckhost)>: try to
14151: authenticate user from domain's lib servers (first use the current
14152: one). C<$upass> should be the users password.
14153: $checkdefauth is optional (value is 1 if a check should be made to
14154:    authenticate user using default authentication method, and allow
14155:    account creation if username does not have account in the domain).
14156: $clientcancheckhost is optional (value is 1 if checking whether the
14157:    server can host will occur on the client side in lonauth.pm).   
14158: 
14159: =item *
14160: X<homeserver()>
14161: B<homeserver($uname,$udom)>: find the server which has
14162: the user's directory and files (there must be only one), this caches
14163: the answer, and also caches if there is a borken connection.
14164: 
14165: =item *
14166: X<idget()>
14167: B<idget($udom,$idsref,$namespace)>: find the usernames behind either 
14168: a list of student/employee IDs or clicker IDs
14169: (student/employee IDs are a unique resource in a domain, there must be 
14170: only 1 ID per username, and only 1 username per ID in a specific domain).
14171: clickerIDs are not necessarily unique, as students might share clickers.
14172: (returns hash: id=>name,id=>name)
14173: 
14174: =item *
14175: X<idrget()>
14176: B<idrget($udom,@unames)>: find the IDs behind a list of
14177: usernames (returns hash: name=>id,name=>id)
14178: 
14179: =item *
14180: X<idput()>
14181: B<idput($udom,$idsref,$uhome,$namespace)>: store away a list of 
14182: names and associated student/employee IDs or clicker IDs.
14183: 
14184: =item *
14185: X<iddel()>
14186: B<iddel($udom,$idshashref,$uhome,$namespace)>: delete unwanted 
14187: student/employee ID or clicker ID username look-ups from domain.
14188: The homeserver ($uhome) and namespace ($namespace) are optional.
14189: If no $uhome is provided, it will be determined usig &homeserver()
14190: for each user.  If no $namespace is provided, the default is ids.
14191: 
14192: =item *
14193: X<updateclickers()>
14194: B<updateclickers($udom,$action,$idshashref,$uhome,$critical)>: update 
14195: clicker ID-to-username look-ups in clickers.db on library server.
14196: Permitted actions are add or del (i.e., add or delete). The 
14197: clickers.db contains clickerID as keys (escaped), and each corresponding
14198: value is an escaped comma-separated list of usernames (for whom the
14199: library server is the homeserver), who registered that particular ID.
14200: If $critical is true, the update will be sent via &critical, otherwise
14201: &reply() will be used.
14202: 
14203: =item *
14204: X<rolesinit()>
14205: B<rolesinit($udom,$username)>: get user privileges.
14206: returns user role, first access and timer interval hashes
14207: 
14208: =item *
14209: X<privileged()>
14210: B<privileged($username,$domain)>: returns a true if user has a
14211: privileged and active role (i.e. su or dc), false otherwise.
14212: 
14213: =item *
14214: X<getsection()>
14215: B<getsection($udom,$uname,$cname)>: finds the section of student in the
14216: course $cname, return section name/number or '' for "not in course"
14217: and '-1' for "no section"
14218: 
14219: =item *
14220: X<userenvironment()>
14221: B<userenvironment($udom,$uname,@what)>: gets the values of the keys
14222: passed in @what from the requested user's environment, returns a hash
14223: 
14224: =item * 
14225: X<userlog_query()>
14226: B<userlog_query($uname,$udom,%filters)>: retrieves data from a user's
14227: activity.log file. %filters defines filters applied when parsing the
14228: log file. These can be start or end timestamps, or the type of action
14229: - log to look for Login or Logout events, check for Checkin or
14230: Checkout, role for role selection. The response is in the form
14231: timestamp1:hostid1:event1&timestamp2:hostid2:event2 where events are
14232: escaped strings of the action recorded in the activity.log file.
14233: 
14234: =back
14235: 
14236: =head2 User Roles
14237: 
14238: =over 4
14239: 
14240: =item *
14241: 
14242: allowed($priv,$uri,$symb,$role,$clientip,$noblockcheck) : check for a user privilege; 
14243: returns codes for allowed actions.
14244: 
14245: The first argument is required, all others are optional.
14246: 
14247: $priv is the privilege being checked.
14248: $uri contains additional information about what is being checked for access (e.g.,
14249: URL, course ID etc.). 
14250: $symb is the unique resource instance identifier in a course; if needed,
14251: but not provided, it will be retrieved via a call to &symbread(). 
14252: $role is the role for which a priv is being checked (only used if priv is evb). 
14253: $clientip is the user's IP address (only used when checking for access to portfolio 
14254: files).
14255: $noblockcheck, if true, skips calls to &has_comm_blocking() for the bre priv. This 
14256: prevents recursive calls to &allowed.
14257: 
14258:  F: full access
14259:  U,I,K: authentication modes (cxx only)
14260:  '': forbidden
14261:  1: user needs to choose course
14262:  2: browse allowed
14263:  A: passphrase authentication needed
14264:  B: access temporarily blocked because of a blocking event in a course.
14265: 
14266: =item *
14267: 
14268: constructaccess($url,$setpriv) : check for access to construction space URL
14269: 
14270: See if the owner domain and name in the URL match those in the
14271: expected environment.  If so, return three element list
14272: ($ownername,$ownerdomain,$ownerhome).
14273: 
14274: Otherwise return the null string.
14275: 
14276: If second argument 'setpriv' is true, it assigns the privileges,
14277: and returns the same three element list, unless the owner has
14278: blocked "ad hoc" Domain Coordinator access to the Author Space,
14279: in which case the null string is returned.
14280: 
14281: =item *
14282: 
14283: definerole($rolename,$sysrole,$domrole,$courole,$uname,$udom) : define role;
14284: define a custom role rolename set privileges in format of lonTabs/roles.tab
14285: for system, domain, and course level. $uname and $udom are optional (current
14286: user's username and domain will be used when either of $uname or $udom are absent.
14287: 
14288: =item *
14289: 
14290: plaintext($short,$type,$cid,$forcedefault) : return value in %prp hash 
14291: (rolesplain.tab); plain text explanation of a user role term.
14292: $type is Course (default) or Community.
14293: If $forcedefault evaluates to true, text returned will be default 
14294: text for $type. Otherwise, if this is a course, the text returned 
14295: will be a custom name for the role (if defined in the course's 
14296: environment).  If no custom name is defined the default is returned.
14297:    
14298: =item *
14299: 
14300: get_my_roles($uname,$udom,$context,$types,$roles,$roledoms,$withsec,$hidepriv) :
14301: All arguments are optional. Returns a hash of a roles, either for
14302: co-author/assistant author roles for a user's Construction Space
14303: (default), or if $context is 'userroles', roles for the user himself,
14304: In the hash, keys are set to colon-separated $uname,$udom,$role, and
14305: (optionally) if $withsec is true, a fourth colon-separated item - $section.
14306: For each key, value is set to colon-separated start and end times for
14307: the role.  If no username and domain are specified, will default to
14308: current user/domain. Types, roles, and roledoms are references to arrays
14309: of role statuses (active, future or previous), roles 
14310: (e.g., cc,in, st etc.) and domains of the roles which can be used
14311: to restrict the list of roles reported. If no array ref is 
14312: provided for types, will default to return only active roles.
14313: 
14314: =item *
14315: 
14316: in_course($udom,$uname,$cdom,$cnum,$type,$hideprivileged) : determine if
14317: user: $uname:$udom has a role in the course: $cdom_$cnum. 
14318: 
14319: Additional optional arguments are: $type (if role checking is to be restricted 
14320: to certain user status types -- previous (expired roles), active (currently
14321: available roles) or future (roles available in the future), and
14322: $hideprivileged -- if true will not report course roles for users who
14323: have active Domain Coordinator role in course's domain or in additional
14324: domains (specified in 'Domains to check for privileged users' in course
14325: environment -- set via:  Course Settings -> Classlists and staff listing).
14326: 
14327: =item *
14328: 
14329: privileged($username,$domain,$possdomains,$possroles) : returns 1 if user
14330: $username:$domain is a privileged user (e.g., Domain Coordinator or Super User)
14331: $possdomains and $possroles are optional array refs -- to domains to check and
14332: roles to check.  If $possdomains is not specified, a dump will be done of the
14333: users' roles.db to check for a dc or su role in any domain. This can be
14334: time consuming if &privileged is called repeatedly (e.g., when displaying a
14335: classlist), so in such cases, supplying a $possdomains array is preferred, as
14336: this then allows &privileged_by_domain() to be used, which caches the identity
14337: of privileged users, eliminating the need for repeated calls to &dump().
14338: 
14339: =item *
14340: 
14341: privileged_by_domain($possdomains,$roles) : returns a hash of a hash of a hash,
14342: where the outer hash keys are domains specified in the $possdomains array ref,
14343: next inner hash keys are privileged roles specified in the $roles array ref,
14344: and the innermost hash contains key = value pairs for username:domain = end:start
14345: for active or future "privileged" users with that role in that domain. To avoid
14346: repeated dumps of domain roles -- via &get_domain_roles() -- contents of the
14347: innerhash are cached using priv_$role and $dom as the identifiers.
14348: 
14349: =back
14350: 
14351: =head2 User Modification
14352: 
14353: =over 4
14354: 
14355: =item *
14356: 
14357: assignrole($udom,$uname,$url,$role,$end,$start,$deleteflag,$selfenroll,$context) : assign role; give a role to a
14358: user for the level given by URL.  Optional start and end dates (leave empty
14359: string or zero for "no date")
14360: 
14361: =item *
14362: 
14363: changepass($uname,$udom,$currentpass,$newpass,$server) : attempts to
14364: change a users, password, possible return values are: ok,
14365: pwchange_failure, non_authorized, auth_mode_error, unknown_user,
14366: refused
14367: 
14368: =item *
14369: 
14370: modifyuserauth($udom,$uname,$umode,$upass) : modify user authentication
14371: 
14372: =item *
14373: 
14374: modifyuser($udom,$uname,$uid,$umode,$upass,$first,$middle,$last, $gene,
14375:            $forceid,$desiredhome,$email,$inststatus,$candelete) :
14376: 
14377: will update user information (firstname,middlename,lastname,generation,
14378: permanentemail), and if forceid is true, student/employee ID also.
14379: A user's institutional affiliation(s) can also be updated.
14380: User information fields will not be overwritten with empty entries 
14381: unless the field is included in the $candelete array reference.
14382: This array is included when a single user is modified via "Manage Users",
14383: or when Autoupdate.pl is run by cron in a domain.
14384: 
14385: =item *
14386: 
14387: modifystudent
14388: 
14389: modify a student's enrollment and identification information.
14390: The course id is resolved based on the current user's environment.  
14391: This means the invoking user must be a course coordinator or otherwise
14392: associated with a course.
14393: 
14394: This call is essentially a wrapper for lonnet::modifyuser and
14395: lonnet::modify_student_enrollment
14396: 
14397: Inputs: 
14398: 
14399: =over 4
14400: 
14401: =item B<$udom> Student's loncapa domain
14402: 
14403: =item B<$uname> Student's loncapa login name
14404: 
14405: =item B<$uid> Student/Employee ID
14406: 
14407: =item B<$umode> Student's authentication mode
14408: 
14409: =item B<$upass> Student's password
14410: 
14411: =item B<$first> Student's first name
14412: 
14413: =item B<$middle> Student's middle name
14414: 
14415: =item B<$last> Student's last name
14416: 
14417: =item B<$gene> Student's generation
14418: 
14419: =item B<$usec> Student's section in course
14420: 
14421: =item B<$end> Unix time of the roles expiration
14422: 
14423: =item B<$start> Unix time of the roles start date
14424: 
14425: =item B<$forceid> If defined, allow $uid to be changed
14426: 
14427: =item B<$desiredhome> server to use as home server for student
14428: 
14429: =item B<$email> Student's permanent e-mail address
14430: 
14431: =item B<$type> Type of enrollment (auto or manual)
14432: 
14433: =item B<$locktype> boolean - enrollment type locked to prevent Autoenroll.pl changing manual to auto    
14434: 
14435: =item B<$cid> courseID - needed if a course role is assigned by a user whose current role is DC
14436: 
14437: =item B<$selfenroll> boolean - 1 if user role change occurred via self-enrollment
14438: 
14439: =item B<$context> role change context (shown in User Management Logs display in a course)
14440: 
14441: =item B<$inststatus> institutional status of user - : separated string of escaped status types
14442: 
14443: =item B<$credits> Number of credits student will earn from this class - only needs to be supplied if value needs to be different from default credits for class.
14444: 
14445: =back
14446: 
14447: =item *
14448: 
14449: modify_student_enrollment
14450: 
14451: Change a student's enrollment status in a class.  The environment variable
14452: 'role.request.course' must be defined for this function to proceed.
14453: 
14454: Inputs:
14455: 
14456: =over 4
14457: 
14458: =item $udom, student's domain
14459: 
14460: =item $uname, student's name
14461: 
14462: =item $uid, student's user id
14463: 
14464: =item $first, student's first name
14465: 
14466: =item $middle
14467: 
14468: =item $last
14469: 
14470: =item $gene
14471: 
14472: =item $usec
14473: 
14474: =item $end
14475: 
14476: =item $start
14477: 
14478: =item $type
14479: 
14480: =item $locktype
14481: 
14482: =item $cid
14483: 
14484: =item $selfenroll
14485: 
14486: =item $context
14487: 
14488: =item $credits, number of credits student will earn from this class
14489: 
14490: =item $instsec, institutional course section code for student
14491: 
14492: =back
14493: 
14494: 
14495: =item *
14496: 
14497: assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start) : assign
14498: custom role; give a custom role to a user for the level given by URL.  Specify
14499: name and domain of role author, and role name
14500: 
14501: =item *
14502: 
14503: revokerole($udom,$uname,$url,$role) : revoke a role for url
14504: 
14505: =item *
14506: 
14507: revokecustomrole($udom,$uname,$url,$role) : revoke a custom role
14508: 
14509: =back
14510: 
14511: =head2 Course Infomation
14512: 
14513: =over 4
14514: 
14515: =item *
14516: 
14517: coursedescription($courseid,$options) : returns a hash of information about the
14518: specified course id, including all environment settings for the
14519: course, the description of the course will be in the hash under the
14520: key 'description'
14521: 
14522: $options is an optional parameter that if supplied is a hash reference that controls
14523: what how this function works.  It has the following key/values:
14524: 
14525: =over 4
14526: 
14527: =item freshen_cache
14528: 
14529: If defined, and the environment cache for the course is valid, it is 
14530: returned in the returned hash.
14531: 
14532: =item one_time
14533: 
14534: If defined, the last cache time is set to _now_
14535: 
14536: =item user
14537: 
14538: If defined, the supplied username is used instead of the current user.
14539: 
14540: 
14541: =back
14542: 
14543: =item *
14544: 
14545: resdata($name,$domain,$type,@which) : request for current parameter
14546: setting for a specific $type, where $type is either 'course' or 'user',
14547: @what should be a list of parameters to ask about. This routine caches
14548: answers for 10 minutes.
14549: 
14550: =item *
14551: 
14552: get_courseresdata($courseid, $domain) : dump the entire course resource
14553: data base, returning a hash that is keyed by the resource name and has
14554: values that are the resource value.  I believe that the timestamps and
14555: versions are also returned.
14556: 
14557: get_numsuppfiles($cnum,$cdom) : retrieve number of files in a course's
14558: supplemental content area. This routine caches the number of files for 
14559: 10 minutes.
14560: 
14561: =back
14562: 
14563: =head2 Course Modification
14564: 
14565: =over 4
14566: 
14567: =item *
14568: 
14569: writecoursepref($courseid,%prefs) : write preferences (environment
14570: database) for a course
14571: 
14572: =item *
14573: 
14574: createcourse($udom,$description,$url,$course_server,$nonstandard,$inst_code,$course_owner,$crstype,$cnum) : make course
14575: 
14576: =item *
14577: 
14578: generate_coursenum($udom,$crstype) : get a unique (unused) course number in domain $udom for course type $crstype (Course or Community).
14579: 
14580: =item *
14581: 
14582: is_course($courseid), is_course($cdom, $cnum)
14583: 
14584: Accepts either a combined $courseid (in the form of domain_courseid) or the
14585: two component version $cdom, $cnum. It checks if the specified course exists.
14586: 
14587: Returns:
14588:     undef if the course doesn't exist, otherwise
14589:     in scalar context the combined courseid.
14590:     in list context the two components of the course identifier, domain and 
14591:     courseid.    
14592: 
14593: =back
14594: 
14595: =head2 Resource Subroutines
14596: 
14597: =over 4
14598: 
14599: =item *
14600: 
14601: subscribe($fname) : subscribe to a resource, returns URL if possible (probably should use repcopy instead)
14602: 
14603: =item *
14604: 
14605: repcopy($filename) : subscribes to the requested file, and attempts to
14606: replicate from the owning library server, Might return
14607: 'unavailable', 'not_found', 'forbidden', 'ok', or
14608: 'bad_request', also attempts to grab the metadata for the
14609: resource. Expects the local filesystem pathname
14610: (/home/httpd/html/res/....)
14611: 
14612: =back
14613: 
14614: =head2 Resource Information
14615: 
14616: =over 4
14617: 
14618: =item *
14619: 
14620: EXT($varname,$symb,$udom,$uname,$usection,$recurse,$cid) : evaluates 
14621: and returns the value of a variety of different possible values,
14622: $varname should be a request string, and the other parameters can be
14623: used to specify who and what one is asking about. Ordinarily, $cid 
14624: does not need to be specified, as it is retrived from 
14625: $env{'request.course.id'}, but &Apache::lonnet::EXT() is called
14626: within lonuserstate::loadmap() when initializing a course, before
14627: $env{'request.course.id'} has been set, so it needs to be provided
14628: in that one case.
14629: 
14630: Possible values for $varname are environment.lastname (or other item
14631: from the envirnment hash), user.name (or someother aspect about the
14632: user), resource.0.maxtries (or some other part and parameter of a
14633: resource)
14634: 
14635: =item *
14636: 
14637: directcondval($number) : get current value of a condition; reads from a state
14638: string
14639: 
14640: =item *
14641: 
14642: condval($condidx) : value of condition index based on state
14643: 
14644: =item *
14645: 
14646: metadata($uri,$what,$liburi,$prefix,$depthcount) : request a
14647: resource's metadata, $what should be either a specific key, or either
14648: 'keys' (to get a list of possible keys) or 'packages' to get a list of
14649: packages that this resource currently uses, the last 3 arguments are only used internally for recursive metadata.
14650: 
14651: this function automatically caches all requests
14652: 
14653: =item *
14654: 
14655: metadata_query($query,$custom,$customshow) : make a metadata query against the
14656: network of library servers; returns file handle of where SQL and regex results
14657: will be stored for query
14658: 
14659: =item *
14660: 
14661: symbread($filename,$donotrecurse,$ignorecachednull,$checkforblock,$possibles) : 
14662: return symbolic list entry (all arguments optional). 
14663: 
14664: Args: filename is the filename (including path) for the file for which a symb 
14665: is required; donotrecurse, if true will prevent calls to allowed() being made 
14666: to check access status if more than one resource was found in the bighash 
14667: (see rev. 1.249) to avoid an infinite loop if an ambiguous resource is part of 
14668: a randompick); ignorecachednull, if true will prevent a symb of '' being 
14669: returned if $env{$cache_str} is defined as ''; checkforblock if true will
14670: cause possible symbs to be checked to determine if they are subject to content
14671: blocking, if so they will not be included as possible symbs; possibles is a
14672: ref to a hash, which, as a side effect, will be populated with all possible 
14673: symbs (content blocking not tested).
14674:  
14675: returns the data handle
14676: 
14677: =item *
14678: 
14679: symbverify($symb,$thisfn,$encstate) : verifies that $symb actually exists
14680: and is a possible symb for the URL in $thisfn, and if is an encrypted
14681: resource that the user accessed using /enc/ returns a 1 on success, 0
14682: on failure, user must be in a course, as it assumes the existence of
14683: the course initial hash, and uses $env('request.course.id'}.  The third
14684: arg is an optional reference to a scalar.  If this arg is passed in the 
14685: call to symbverify, it will be set to 1 if the symb has been set to be 
14686: encrypted; otherwise it will be null.  
14687: 
14688: =item *
14689: 
14690: symbclean($symb) : removes versions numbers from a symb, returns the
14691: cleaned symb
14692: 
14693: =item *
14694: 
14695: is_on_map($uri) : checks if the $uri is somewhere on the current
14696: course map, user must be in a course for it to work.
14697: 
14698: =item *
14699: 
14700: numval($salt) : return random seed value (addend for rndseed)
14701: 
14702: =item *
14703: 
14704: rndseed($symb,$courseid,$udom,$uname) : create a random sum; returns
14705: a random seed, all arguments are optional, if they aren't sent it uses the
14706: environment to derive them. Note: if symb isn't sent and it can't get one
14707: from &symbread it will use the current time as its return value
14708: 
14709: =item *
14710: 
14711: ireceipt($funame,$fudom,$fucourseid,$fusymb) : return unique,
14712: unfakeable, receipt
14713: 
14714: =item *
14715: 
14716: receipt() : API to ireceipt working off of env values; given out to users
14717: 
14718: =item *
14719: 
14720: countacc($url) : count the number of accesses to a given URL
14721: 
14722: =item *
14723: 
14724: 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
14725: 
14726: =item *
14727: 
14728: 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)
14729: 
14730: =item *
14731: 
14732: expirespread($uname,$udom,$stype,$usymb) : set expire date for spreadsheet
14733: 
14734: =item *
14735: 
14736: devalidate($symb) : devalidate temporary spreadsheet calculations,
14737: forcing spreadsheet to reevaluate the resource scores next time.
14738: 
14739: =item * 
14740: 
14741: can_edit_resource($file,$cnum,$cdom,$resurl,$symb,$group) : determine if current user can edit a particular resource,
14742: when viewing in course context.
14743: 
14744:  input: six args -- filename (decluttered), course number, course domain,
14745:                     url, symb (if registered) and group (if this is a 
14746:                     group item -- e.g., bulletin board, group page etc.).
14747: 
14748:  output: array of five scalars --
14749:          $cfile -- url for file editing if editable on current server
14750:          $home -- homeserver of resource (i.e., for author if published,
14751:                                           or course if uploaded.).
14752:          $switchserver --  1 if server switch will be needed.
14753:          $forceedit -- 1 if icon/link should be to go to edit mode 
14754:          $forceview -- 1 if icon/link should be to go to view mode
14755: 
14756: =item *
14757: 
14758: is_course_upload($file,$cnum,$cdom)
14759: 
14760: Used in course context to determine if current file was uploaded to 
14761: the course (i.e., would be found in /userfiles/docs on the course's 
14762: homeserver.
14763: 
14764:   input: 3 args -- filename (decluttered), course number and course domain.
14765:   output: boolean -- 1 if file was uploaded.
14766: 
14767: =back
14768: 
14769: =head2 Storing/Retreiving Data
14770: 
14771: =over 4
14772: 
14773: =item *
14774: 
14775: store($storehash,$symb,$namespace,$udom,$uname,$laststore) : stores hash
14776: permanently for this url; hashref needs to be given and should be a \%hashname;
14777: the remaining args aren't required and if they aren't passed or are '' they will
14778: be derived from the env (with the exception of $laststore, which is an 
14779: optional arg used when a user's submission is stored in grading).
14780: $laststore is $version=$timestamp, where $version is the most recent version
14781: number retrieved for the corresponding $symb in the $namespace db file, and
14782: $timestamp is the timestamp for that transaction (UNIX time).
14783: $laststore is currently only passed when cstore() is called by 
14784: structuretags::finalize_storage().
14785: 
14786: =item *
14787: 
14788: cstore($storehash,$symb,$namespace,$udom,$uname,$laststore) : same as store
14789: but uses critical subroutine
14790: 
14791: =item *
14792: 
14793: restore($symb,$namespace,$udom,$uname) : returns hash for this symb;
14794: all args are optional
14795: 
14796: =item *
14797: 
14798: dumpstore($namespace,$udom,$uname,$regexp,$range) : 
14799: dumps the complete (or key matching regexp) namespace into a hash
14800: ($udom, $uname, $regexp, $range are optional) for a namespace that is
14801: normally &store()ed into
14802: 
14803: $range should be either an integer '100' (give me the first 100
14804:                                            matching records)
14805:               or be  two integers sperated by a - with no spaces
14806:                  '30-50' (give me the 30th through the 50th matching
14807:                           records)
14808: 
14809: 
14810: =item *
14811: 
14812: putstore($namespace,$symb,$version,$storehash,$udomain,$uname,$tolog) :
14813: replaces a &store() version of data with a replacement set of data
14814: for a particular resource in a namespace passed in the $storehash hash 
14815: reference. If $tolog is true, the transaction is logged in the courselog
14816: with an action=PUTSTORE.
14817: 
14818: =item *
14819: 
14820: tmpstore($storehash,$symb,$namespace,$udom,$uname) : storage that
14821: works very similar to store/cstore, but all data is stored in a
14822: temporary location and can be reset using tmpreset, $storehash should
14823: be a hash reference, returns nothing on success
14824: 
14825: =item *
14826: 
14827: tmprestore($symb,$namespace,$udom,$uname) : storage that works very
14828: similar to restore, but all data is stored in a temporary location and
14829: can be reset using tmpreset. Returns a hash of values on success,
14830: error string otherwise.
14831: 
14832: =item *
14833: 
14834: tmpreset($symb,$namespace,$udom,$uname) : temporary storage reset,
14835: deltes all keys for $symb form the temporary storage hash.
14836: 
14837: =item *
14838: 
14839: get($namespace,$storearr,$udom,$uname) : returns hash with keys from array
14840: reference filled in from namesp ($udom and $uname are optional)
14841: 
14842: =item *
14843: 
14844: del($namespace,$storearr,$udom,$uname) : deletes keys out of array from
14845: namesp ($udom and $uname are optional)
14846: 
14847: =item *
14848: 
14849: dump($namespace,$udom,$uname,$regexp,$range) : 
14850: dumps the complete (or key matching regexp) namespace into a hash
14851: ($udom, $uname, $regexp, $range are optional)
14852: 
14853: $range should be either an integer '100' (give me the first 100
14854:                                            matching records)
14855:               or be  two integers sperated by a - with no spaces
14856:                  '30-50' (give me the 30th through the 50th matching
14857:                           records)
14858: =item *
14859: 
14860: inc($namespace,$store,$udom,$uname) : increments $store in $namespace.
14861: $store can be a scalar, an array reference, or if the amount to be 
14862: incremented is > 1, a hash reference.
14863: 
14864: ($udom and $uname are optional)
14865: 
14866: =item *
14867: 
14868: put($namespace,$storehash,$udom,$uname) : stores hash in namesp
14869: ($udom and $uname are optional)
14870: 
14871: =item *
14872: 
14873: cput($namespace,$storehash,$udom,$uname) : critical put
14874: ($udom and $uname are optional)
14875: 
14876: =item *
14877: 
14878: newput($namespace,$storehash,$udom,$uname) :
14879: 
14880: Attempts to store the items in the $storehash, but only if they don't
14881: currently exist, if this succeeds you can be certain that you have 
14882: successfully created a new key value pair in the $namespace db.
14883: 
14884: 
14885: Args:
14886:  $namespace: name of database to store values to
14887:  $storehash: hashref to store to the db
14888:  $udom: (optional) domain of user containing the db
14889:  $uname: (optional) name of user caontaining the db
14890: 
14891: Returns:
14892:  'ok' -> succeeded in storing all keys of $storehash
14893:  'key_exists: <key>' -> failed to anything out of $storehash, as at
14894:                         least <key> already existed in the db (other
14895:                         requested keys may also already exist)
14896:  'error: <msg>' -> unable to tie the DB or other error occurred
14897:  'con_lost' -> unable to contact request server
14898:  'refused' -> action was not allowed by remote machine
14899: 
14900: 
14901: =item *
14902: 
14903: eget($namespace,$storearr,$udom,$uname) : returns hash with keys from array
14904: reference filled in from namesp (encrypts the return communication)
14905: ($udom and $uname are optional)
14906: 
14907: =item *
14908: 
14909: log($udom,$name,$home,$message) : write to permanent log for user; use
14910: critical subroutine
14911: 
14912: =item *
14913: 
14914: get_dom($namespace,$storearr,$udom,$uhome) : returns hash with keys from
14915: array reference filled in from namespace found in domain level on either
14916: specified domain server ($uhome) or primary domain server ($udom and $uhome are optional).
14917: 
14918: =item *
14919: 
14920: put_dom($namespace,$storehash,$udom,$uhome) :  stores hash in namespace at 
14921: domain level either on specified domain server ($uhome) or primary domain 
14922: server ($udom and $uhome are optional)
14923: 
14924: =item * 
14925: 
14926: get_domain_defaults($target_domain,$ignore_cache) : returns hash with defaults 
14927: for: authentication, language, quotas, timezone, date locale, and portal URL in
14928: the target domain.
14929: 
14930: May also include additional key => value pairs for the following groups:
14931: 
14932: =over
14933: 
14934: =item
14935: disk quotas (MB allocated by default to portfolios and authoring spaces).
14936: 
14937: =over
14938: 
14939: =item defaultquota, authorquota
14940: 
14941: =back
14942: 
14943: =item
14944: tools (availability of aboutme page, blog, webDAV access for authoring spaces,
14945: portfolio for users).
14946: 
14947: =over
14948: 
14949: =item
14950: aboutme, blog, webdav, portfolio
14951: 
14952: =back
14953: 
14954: =item
14955: requestcourses: ability to request courses, and how requests are processed.
14956: 
14957: =over
14958: 
14959: =item
14960: official, unofficial, community, textbook, placement
14961: 
14962: =back
14963: 
14964: =item
14965: inststatus: types of institutional affiliation, and order in which they are displayed.
14966: 
14967: =over
14968: 
14969: =item
14970: inststatustypes, inststatusorder, inststatusguest
14971: 
14972: =back
14973: 
14974: =item
14975: coursedefaults: can PDF forms can be created, default credits for courses, default quotas (MB)
14976: for course's uploaded content.
14977: 
14978: =over
14979: 
14980: =item
14981: canuse_pdfforms, officialcredits, unofficialcredits, textbookcredits, officialquota, unofficialquota, 
14982: communityquota, textbookquota, placementquota
14983: 
14984: =back
14985: 
14986: =item
14987: usersessions: set options for hosting of your users in other domains, and hosting of users from other domains
14988: on your servers.
14989: 
14990: =over
14991: 
14992: =item 
14993: remotesessions, hostedsessions
14994: 
14995: =back
14996: 
14997: =back
14998: 
14999: In cases where a domain coordinator has never used the "Set Domain Configuration"
15000: utility to create a configuration.db file on a domain's primary library server 
15001: only the following domain defaults: auth_def, auth_arg_def, lang_def
15002: -- corresponding values are authentication type (internal, krb4, krb5,
15003: or localauth), initial password or a kerberos realm, language (e.g., en-us) -- 
15004: will be available. Values are retrieved from cache (if current), unless the
15005: optional $ignore_cache arg is true, or from domain's configuration.db (if available),
15006: or lastly from values in lonTabs/dns_domain,tab, or lonTabs/domain.tab.
15007: 
15008: Typical usage:
15009: 
15010: %domdefaults = &get_domain_defaults($target_domain);
15011: 
15012: =back
15013: 
15014: =head2 Network Status Functions
15015: 
15016: =over 4
15017: 
15018: =item *
15019: 
15020: dirlist() : return directory list based on URI (first arg).
15021: 
15022: Inputs: 1 required, 5 optional.
15023: 
15024: =over
15025: 
15026: =item 
15027: $uri - path to file in filesystem (starts: /res or /userfiles/). Required.
15028: 
15029: =item
15030: $userdomain - domain of user/course to be listed. Extracted from $uri if absent. 
15031: 
15032: =item
15033: $username -  username of user/course to be listed. Extracted from $uri if absent. 
15034: 
15035: =item
15036: $getpropath - boolean: 1 if prepend path using &propath(). 
15037: 
15038: =item
15039: $getuserdir - boolean: 1 if prepend path for "userfiles".
15040: 
15041: =item 
15042: $alternateRoot - path to prepend in place of path from $uri.
15043: 
15044: =back
15045: 
15046: Returns: Array of up to two items.
15047: 
15048: =over
15049: 
15050: a reference to an array of files/subdirectories
15051: 
15052: =over
15053: 
15054: Each element in the array of files/subdirectories is a & separated list of
15055: item name and the result of running stat on the item.  If dirlist was requested
15056: for a file instead of a directory, the item name will be ''. For a directory 
15057: listing, if the item is a metadata file, the element will end &N&M 
15058: (where N amd M are either 0 or 1, corresponding to obsolete set (1), or
15059: default copyright set (1).  
15060: 
15061: =back
15062: 
15063: a scalar containing error condition (if encountered).
15064: 
15065: =over
15066: 
15067: =item 
15068: no_host (no homeserver identified for $username:$domain).
15069: 
15070: =item 
15071: no_such_host (server contacted for listing not identified as valid host).
15072: 
15073: =item 
15074: con_lost (connection to remote server failed).
15075: 
15076: =item 
15077: refused (invalid $username:$domain received on lond side).
15078: 
15079: =item 
15080: no_such_dir (directory at specified path on lond side does not exist). 
15081: 
15082: =item 
15083: empty (directory at specified path on lond side is empty).
15084: 
15085: =over
15086: 
15087: This is currently not encountered because the &ls3, &ls2, 
15088: &ls (_handler) routines on the lond side do not filter out
15089: . and .. from a directory listing. 
15090: 
15091: =back
15092: 
15093: =back
15094: 
15095: =back
15096: 
15097: =item *
15098: 
15099: spareserver() : find server with least workload from spare.tab
15100: 
15101: 
15102: =item *
15103: 
15104: host_from_dns($dns) : Returns the loncapa hostname corresponding to a DNS name or undef
15105: if there is no corresponding loncapa host.
15106: 
15107: =back
15108: 
15109: 
15110: =head2 Apache Request
15111: 
15112: =over 4
15113: 
15114: =item *
15115: 
15116: ssi($url,%hash) : server side include, does a complete request cycle on url to
15117: localhost, posts hash
15118: 
15119: =back
15120: 
15121: =head2 Data to String to Data
15122: 
15123: =over 4
15124: 
15125: =item *
15126: 
15127: hash2str(%hash) : convert a hash into a string complete with escaping and '='
15128: and '&' separators, supports elements that are arrayrefs and hashrefs
15129: 
15130: =item *
15131: 
15132: hashref2str($hashref) : convert a hashref into a string complete with
15133: escaping and '=' and '&' separators, supports elements that are
15134: arrayrefs and hashrefs
15135: 
15136: =item *
15137: 
15138: arrayref2str($arrayref) : convert an arrayref into a string complete
15139: with escaping and '&' separators, supports elements that are arrayrefs
15140: and hashrefs
15141: 
15142: =item *
15143: 
15144: str2hash($string) : convert string to hash using unescaping and
15145: splitting on '=' and '&', supports elements that are arrayrefs and
15146: hashrefs
15147: 
15148: =item *
15149: 
15150: str2array($string) : convert string to hash using unescaping and
15151: splitting on '&', supports elements that are arrayrefs and hashrefs
15152: 
15153: =back
15154: 
15155: =head2 Logging Routines
15156: 
15157: 
15158: These routines allow one to make log messages in the lonnet.log and
15159: lonnet.perm logfiles.
15160: 
15161: =over 4
15162: 
15163: =item *
15164: 
15165: logtouch() : make sure the logfile, lonnet.log, exists
15166: 
15167: =item *
15168: 
15169: logthis() : append message to the normal lonnet.log file, it gets
15170: preiodically rolled over and deleted.
15171: 
15172: =item *
15173: 
15174: logperm() : append a permanent message to lonnet.perm.log, this log
15175: file never gets deleted by any automated portion of the system, only
15176: messages of critical importance should go in here.
15177: 
15178: 
15179: =back
15180: 
15181: =head2 General File Helper Routines
15182: 
15183: =over 4
15184: 
15185: =item *
15186: 
15187: getfile($file,$caller) : two cases - requests for files in /res or in /uploaded.
15188: (a) files in /uploaded
15189:   (i) If a local copy of the file exists - 
15190:       compares modification date of local copy with last-modified date for 
15191:       definitive version stored on home server for course. If local copy is 
15192:       stale, requests a new version from the home server and stores it. 
15193:       If the original has been removed from the home server, then local copy 
15194:       is unlinked.
15195:   (ii) If local copy does not exist -
15196:       requests the file from the home server and stores it. 
15197:   
15198:   If $caller is 'uploadrep':  
15199:     This indicates a call from lonuploadrep.pm (PerlHeaderParserHandler phase)
15200:     for request for files originally uploaded via DOCS. 
15201:      - returns 'ok' if fresh local copy now available, -1 otherwise.
15202:   
15203:   Otherwise:
15204:      This indicates a call from the content generation phase of the request.
15205:      -  returns the entire contents of the file or -1.
15206:      
15207: (b) files in /res
15208:    - returns the entire contents of a file or -1; 
15209:    it properly subscribes to and replicates the file if neccessary.
15210: 
15211: 
15212: =item *
15213: 
15214: stat_file($url) : $url is expected to be a /res/ or /uploaded/ style file
15215:                   reference
15216: 
15217: returns either a stat() list of data about the file or an empty list
15218: if the file doesn't exist or couldn't find out about it (connection
15219: problems or user unknown)
15220: 
15221: =item *
15222: 
15223: filelocation($dir,$file) : returns file system location of a file
15224: based on URI; meant to be "fairly clean" absolute reference, $dir is a
15225: directory that relative $file lookups are to looked in ($dir of /a/dir
15226: and a file of ../bob will become /a/bob)
15227: 
15228: =item *
15229: 
15230: hreflocation($dir,$file) : returns file system location or a URL; same as
15231: filelocation except for hrefs
15232: 
15233: =item *
15234: 
15235: declutter() : declutters URLs -- remove beginning slashes, 'res' etc.
15236: also removes beginning /home/httpd/html unless /priv/ follows it.
15237: 
15238: =back
15239: 
15240: =head2 Usererfile file routines (/uploaded*)
15241: 
15242: =over 4
15243: 
15244: =item *
15245: 
15246: userfileupload(): main rotine for putting a file in a user or course's
15247:                   filespace, arguments are,
15248: 
15249:  formname - required - this is the name of the element in $env where the
15250:            filename, and the contents of the file to create/modifed exist
15251:            the filename is in $env{'form.'.$formname.'.filename'} and the
15252:            contents of the file is located in $env{'form.'.$formname}
15253:  context - if coursedoc, store the file in the course of the active role
15254:              of the current user; 
15255:            if 'existingfile': store in 'overwrites' in /home/httpd/perl/tmp
15256:            if 'canceloverwrite': delete file in tmp/overwrites directory
15257:  subdir - required - subdirectory to put the file in under ../userfiles/
15258:          if undefined, it will be placed in "unknown"
15259: 
15260:  (This routine calls clean_filename() to remove any dangerous
15261:  characters from the filename, and then calls finuserfileupload() to
15262:  complete the transaction)
15263: 
15264:  returns either the url of the uploaded file (/uploaded/....) if successful
15265:  and /adm/notfound.html if unsuccessful
15266: 
15267: =item *
15268: 
15269: clean_filename(): routine for cleaing a filename up for storage in
15270:                  userfile space, argument is:
15271: 
15272:  filename - proposed filename
15273: 
15274: returns: the new clean filename
15275: 
15276: =item *
15277: 
15278: finishuserfileupload(): routine that creates and sends the file to
15279: userspace, probably shouldn't be called directly
15280: 
15281:   docuname: username or courseid of destination for the file
15282:   docudom: domain of user/course of destination for the file
15283:   formname: same as for userfileupload()
15284:   fname: filename (including subdirectories) for the file
15285:   parser: if 'parse', will parse (html) file to extract references to objects, links etc.
15286:   allfiles: reference to hash used to store objects found by parser
15287:   codebase: reference to hash used for codebases of java objects found by parser
15288:   thumbwidth: width (pixels) of thumbnail to be created for uploaded image
15289:   thumbheight: height (pixels) of thumbnail to be created for uploaded image
15290:   resizewidth: width to be used to resize image using resizeImage from ImageMagick
15291:   resizeheight: height to be used to resize image using resizeImage from ImageMagick
15292:   context: if 'overwrite', will move the uploaded file from its temporary location to
15293:             userfiles to facilitate overwriting a previously uploaded file with same name.
15294:   mimetype: reference to scalar to accommodate mime type determined
15295:             from File::MMagic if $parser = parse.
15296: 
15297:  returns either the url of the uploaded file (/uploaded/....) if successful
15298:  and /adm/notfound.html if unsuccessful (or an error message if context 
15299:  was 'overwrite').
15300:  
15301: 
15302: =item *
15303: 
15304: renameuserfile(): renames an existing userfile to a new name
15305: 
15306:   Args:
15307:    docuname: username or courseid of destination for the file
15308:    docudom: domain of user/course of destination for the file
15309:    old: current file name (including any subdirs under userfiles)
15310:    new: desired file name (including any subdirs under userfiles)
15311: 
15312: =item *
15313: 
15314: mkdiruserfile(): creates a directory is a userfiles dir
15315: 
15316:   Args:
15317:    docuname: username or courseid of destination for the file
15318:    docudom: domain of user/course of destination for the file
15319:    dir: dir to create (including any subdirs under userfiles)
15320: 
15321: =item *
15322: 
15323: removeuserfile(): removes a file that exists in userfiles
15324: 
15325:   Args:
15326:    docuname: username or courseid of destination for the file
15327:    docudom: domain of user/course of destination for the file
15328:    fname: filname to delete (including any subdirs under userfiles)
15329: 
15330: =item *
15331: 
15332: removeuploadedurl(): convience function for removeuserfile()
15333: 
15334:   Args:
15335:    url:  a full /uploaded/... url to delete
15336: 
15337: =item * 
15338: 
15339: get_portfile_permissions():
15340:   Args:
15341:     domain: domain of user or course contain the portfolio files
15342:     user: name of user or num of course contain the portfolio files
15343:   Returns:
15344:     hashref of a dump of the proper file_permissions.db
15345:    
15346: 
15347: =item * 
15348: 
15349: get_access_controls():
15350: 
15351: Args:
15352:   current_permissions: the hash ref returned from get_portfile_permissions()
15353:   group: (optional) the group you want the files associated with
15354:   file: (optional) the file you want access info on
15355: 
15356: Returns:
15357:     a hash (keys are file names) of hashes containing
15358:         keys are: path to file/file_name\0uniqueID:scope_end_start (see below)
15359:         values are XML containing access control settings (see below) 
15360: 
15361: Internal notes:
15362: 
15363:  access controls are stored in file_permissions.db as key=value pairs.
15364:     key -> path to file/file_name\0uniqueID:scope_end_start
15365:         where scope -> public,guest,course,group,domains or users.
15366:               end -> UNIX time for end of access (0 -> no end date)
15367:               start -> UNIX time for start of access
15368: 
15369:     value -> XML description of access control
15370:            <scope type=""> (type =1 of: public,guest,course,group,domains,users">
15371:             <start></start>
15372:             <end></end>
15373: 
15374:             <password></password>  for scope type = guest
15375: 
15376:             <domain></domain>     for scope type = course or group
15377:             <number></number>
15378:             <roles id="">
15379:              <role></role>
15380:              <access></access>
15381:              <section></section>
15382:              <group></group>
15383:             </roles>
15384: 
15385:             <dom></dom>         for scope type = domains
15386: 
15387:             <users>             for scope type = users
15388:              <user>
15389:               <uname></uname>
15390:               <udom></udom>
15391:              </user>
15392:             </users>
15393:            </scope> 
15394:               
15395:  Access data is also aggregated for each file in an additional key=value pair:
15396:  key -> path to file/file_name\0accesscontrol 
15397:  value -> reference to hash
15398:           hash contains key = value pairs
15399:           where key = uniqueID:scope_end_start
15400:                 value = UNIX time record was last updated
15401: 
15402:           Used to improve speed of look-ups of access controls for each file.  
15403:  
15404:  Locks on files (resulting from submission of portfolio file to a homework problem stored in array of arrays.
15405: 
15406: =item *
15407: 
15408: modify_access_controls():
15409: 
15410: Modifies access controls for a portfolio file
15411: Args
15412: 1. file name
15413: 2. reference to hash of required changes,
15414: 3. domain
15415: 4. username
15416:   where domain,username are the domain of the portfolio owner 
15417:   (either a user or a course) 
15418: 
15419: Returns:
15420: 1. result of additions or updates ('ok' or 'error', with error message). 
15421: 2. result of deletions ('ok' or 'error', with error message).
15422: 3. reference to hash of any new or updated access controls.
15423: 4. reference to hash used to map incoming IDs to uniqueIDs assigned to control.
15424:    key = integer (inbound ID)
15425:    value = uniqueID
15426: 
15427: =item *
15428: 
15429: get_timebased_id():
15430: 
15431: Attempts to get a unique timestamp-based suffix for use with items added to a 
15432: course via the Course Editor (e.g., folders, composite pages, 
15433: group bulletin boards).
15434: 
15435: Args: (first three required; six others optional)
15436: 
15437: 1. prefix (alphanumeric): of keys in hash, e.g., suppsequence, docspage,
15438:    docssequence, or name of group
15439: 
15440: 2. keyid (alphanumeric): name of temporary locking key in hash,
15441:    e.g., num, boardids
15442: 
15443: 3. namespace: name of gdbm file used to store suffixes already assigned;  
15444:    file will be named nohist_namespace.db
15445: 
15446: 4. cdom: domain of course; default is current course domain from %env
15447: 
15448: 5. cnum: course number; default is current course number from %env
15449: 
15450: 6. idtype: set to concat if an additional digit is to be appended to the 
15451:    unix timestamp to form the suffix, if the plain timestamp is already
15452:    in use.  Default is to not do this, but simply increment the unix 
15453:    timestamp by 1 until a unique key is obtained.
15454: 
15455: 7. who: holder of locking key; defaults to user:domain for user.
15456: 
15457: 8. locktries: number of attempts to obtain a lock (sleep of 1s before 
15458:    retrying); default is 3.
15459: 
15460: 9. maxtries: number of attempts to obtain a unique suffix; default is 20.  
15461: 
15462: Returns:
15463: 
15464: 1. suffix obtained (numeric)
15465: 
15466: 2. result of deleting locking key (ok if deleted, or lock never obtained)
15467: 
15468: 3. error: contains (localized) error message if an error occurred.
15469: 
15470: 
15471: =back
15472: 
15473: =head2 HTTP Helper Routines
15474: 
15475: =over 4
15476: 
15477: =item *
15478: 
15479: escape() : unpack non-word characters into CGI-compatible hex codes
15480: 
15481: =item *
15482: 
15483: unescape() : pack CGI-compatible hex codes into actual non-word ASCII character
15484: 
15485: =back
15486: 
15487: =head1 PRIVATE SUBROUTINES
15488: 
15489: =head2 Underlying communication routines (Shouldn't call)
15490: 
15491: =over 4
15492: 
15493: =item *
15494: 
15495: subreply() : tries to pass a message to lonc, returns con_lost if incapable
15496: 
15497: =item *
15498: 
15499: reply() : uses subreply to send a message to remote machine, logs all failures
15500: 
15501: =item *
15502: 
15503: critical() : passes a critical message to another server; if cannot
15504: get through then place message in connection buffer directory and
15505: returns con_delayed, if incapable of saving message, returns
15506: con_failed
15507: 
15508: =item *
15509: 
15510: reconlonc() : tries to reconnect lonc client processes.
15511: 
15512: =back
15513: 
15514: =head2 Resource Access Logging
15515: 
15516: =over 4
15517: 
15518: =item *
15519: 
15520: flushcourselogs() : flush (save) buffer logs and access logs
15521: 
15522: =item *
15523: 
15524: courselog($what) : save message for course in hash
15525: 
15526: =item *
15527: 
15528: courseacclog($what) : save message for course using &courselog().  Perform
15529: special processing for specific resource types (problems, exams, quizzes, etc).
15530: 
15531: =item *
15532: 
15533: goodbye() : flush course logs and log shutting down; it is called in srm.conf
15534: as a PerlChildExitHandler
15535: 
15536: =back
15537: 
15538: =head2 Other
15539: 
15540: =over 4
15541: 
15542: =item *
15543: 
15544: symblist($mapname,%newhash) : update symbolic storage links
15545: 
15546: =back
15547: 
15548: =cut
15549: 

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