File:  [LON-CAPA] / loncom / lonnet / perl / lonnet.pm
Revision 1.1450: download - view: text, annotated - select for diffs
Mon May 3 15:27:45 2021 UTC (3 years, 2 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Bug 6914
 - Check whether WAF proxy alias should be used when using switchserver.
 - Requests for /adm/dns/hosts and /adm/dns/domain from other nodes in the
   network do not require redirecting to an alias (if in use).

    1: # The LearningOnline Network
    2: # TCP networking package
    3: #
    4: # $Id: lonnet.pm,v 1.1450 2021/05/03 15:27:45 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: use CGI::Cookie;
   77: 
   78: use Encode;
   79: 
   80: use vars qw(%perlvar %spareid %pr %prp $memcache %packagetab $tmpdir $deftex
   81:             $_64bit %env %protocol %loncaparevs %serverhomeIDs %needsrelease
   82:             %managerstab $passwdmin);
   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 Net::CIDR;
  100: use LONCAPA qw(:DEFAULT :match);
  101: use LONCAPA::Configuration;
  102: use LONCAPA::lonmetadata;
  103: use LONCAPA::Lond;
  104: use LONCAPA::LWPReq;
  105: use LONCAPA::transliterate;
  106: 
  107: use File::Copy;
  108: 
  109: my $readit;
  110: my $max_connection_retries = 20;     # Or some such value.
  111: 
  112: require Exporter;
  113: 
  114: our @ISA = qw (Exporter);
  115: our @EXPORT = qw(%env);
  116: 
  117: 
  118: # ------------------------------------ Logging (parameters, docs, slots, roles)
  119: {
  120:     my $logid;
  121:     sub write_log {
  122: 	my ($context,$hash_name,$storehash,$delflag,$uname,$udom,$cnum,$cdom)=@_;
  123:         if ($context eq 'course') {
  124:             if (($cnum eq '') || ($cdom eq '')) {
  125:                 $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
  126:                 $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
  127:             }
  128:         }
  129: 	$logid ++;
  130:         my $now = time();
  131: 	my $id=$now.'00000'.$$.'00000'.$logid;
  132:         my $ip = &get_requestor_ip();
  133:         my $logentry = { 
  134:                           $id => {
  135:                                    'exe_uname' => $env{'user.name'},
  136:                                    'exe_udom'  => $env{'user.domain'},
  137:                                    'exe_time'  => $now,
  138:                                    'exe_ip'    => $ip,
  139:                                    'delflag'   => $delflag,
  140:                                    'logentry'  => $storehash,
  141:                                    'uname'     => $uname,
  142:                                    'udom'      => $udom,
  143:                                   }
  144:                        };
  145: 	return &put('nohist_'.$hash_name,$logentry,$cdom,$cnum);
  146:     }
  147: }
  148: 
  149: sub logtouch {
  150:     my $execdir=$perlvar{'lonDaemons'};
  151:     unless (-e "$execdir/logs/lonnet.log") {	
  152: 	open(my $fh,">>","$execdir/logs/lonnet.log");
  153: 	close $fh;
  154:     }
  155:     my ($wwwuid,$wwwgid)=(getpwnam('www'))[2,3];
  156:     chown($wwwuid,$wwwgid,$execdir.'/logs/lonnet.log');
  157: }
  158: 
  159: sub logthis {
  160:     my $message=shift;
  161:     my $execdir=$perlvar{'lonDaemons'};
  162:     my $now=time;
  163:     my $local=localtime($now);
  164:     if (open(my $fh,">>","$execdir/logs/lonnet.log")) {
  165: 	my $logstring = $local. " ($$): ".$message."\n"; # Keep any \'s in string.
  166: 	print $fh $logstring;
  167: 	close($fh);
  168:     }
  169:     return 1;
  170: }
  171: 
  172: sub logperm {
  173:     my $message=shift;
  174:     my $execdir=$perlvar{'lonDaemons'};
  175:     my $now=time;
  176:     my $local=localtime($now);
  177:     if (open(my $fh,">>","$execdir/logs/lonnet.perm.log")) {
  178: 	print $fh "$now:$message:$local\n";
  179: 	close($fh);
  180:     }
  181:     return 1;
  182: }
  183: 
  184: sub create_connection {
  185:     my ($hostname,$lonid) = @_;
  186:     my $client=IO::Socket::UNIX->new(Peer    => $perlvar{'lonSockCreate'},
  187: 				     Type    => SOCK_STREAM,
  188: 				     Timeout => 10);
  189:     return 0 if (!$client);
  190:     print $client (join(':',$hostname,$lonid,&machine_ids($hostname),$loncaparevs{$lonid})."\n");
  191:     my $result = <$client>;
  192:     chomp($result);
  193:     return 1 if ($result eq 'done');
  194:     return 0;
  195: }
  196: 
  197: sub get_server_timezone {
  198:     my ($cnum,$cdom) = @_;
  199:     my $home=&homeserver($cnum,$cdom);
  200:     if ($home ne 'no_host') {
  201:         my $cachetime = 24*3600;
  202:         my ($timezone,$cached)=&is_cached_new('servertimezone',$home);
  203:         if (defined($cached)) {
  204:             return $timezone;
  205:         } else {
  206:             my $timezone = &reply('servertimezone',$home);
  207:             return &do_cache_new('servertimezone',$home,$timezone,$cachetime);
  208:         }
  209:     }
  210: }
  211: 
  212: sub get_server_distarch {
  213:     my ($lonhost,$ignore_cache) = @_;
  214:     if (defined($lonhost)) {
  215:         if (!defined(&hostname($lonhost))) {
  216:             return;
  217:         }
  218:         my $cachetime = 12*3600;
  219:         if (!$ignore_cache) {
  220:             my ($distarch,$cached)=&is_cached_new('serverdistarch',$lonhost);
  221:             if (defined($cached)) {
  222:                 return $distarch;
  223:             }
  224:         }
  225:         my $rep = &reply('serverdistarch',$lonhost);
  226:         unless ($rep eq 'unknown_command' || $rep eq 'no_such_host' ||
  227:                 $rep eq 'con_lost' || $rep eq 'rejected' || $rep eq 'refused' ||
  228:                 $rep eq '') {
  229:             return &do_cache_new('serverdistarch',$lonhost,$rep,$cachetime);
  230:         }
  231:     }
  232:     return;
  233: }
  234: 
  235: sub get_servercerts_info {
  236:     my ($lonhost,$hostname,$context) = @_;
  237:     return if ($lonhost eq '');
  238:     if ($hostname eq '') {
  239:         $hostname = &hostname($lonhost);
  240:     }
  241:     return if ($hostname eq '');
  242:     my ($rep,$uselocal);
  243:     if ($context eq 'install') {
  244:         $uselocal = 1;
  245:     } elsif (grep { $_ eq $lonhost } &current_machine_ids()) {
  246:         $uselocal = 1;
  247:     }
  248:     if (($context ne 'cgi') && ($context ne 'install') && ($uselocal)) {
  249:         my $distro = (split(/\:/,&get_server_distarch($lonhost)))[0];
  250:         if ($distro eq '') {
  251:             $uselocal = 0;
  252:         } elsif ($distro =~ /^(?:centos|redhat|scientific)(\d+)$/) {
  253:             if ($1 < 6) {
  254:                 $uselocal = 0;
  255:             }
  256:         }  elsif ($distro =~ /^(?:sles)(\d+)$/) {
  257:             if ($1 < 12) {
  258:                 $uselocal = 0;
  259:             }
  260:         }
  261:     }
  262:     if ($uselocal) {
  263:         $rep = LONCAPA::Lond::server_certs(\%perlvar,$lonhost,$hostname);
  264:     } else {
  265:         $rep=&reply('servercerts',$lonhost);
  266:     }
  267:     my ($result,%returnhash);
  268:     if (($rep=~/^(refused|rejected|error)/) || ($rep eq 'con_lost') ||
  269:         ($rep eq 'unknown_cmd')) {
  270:         $result = $rep;
  271:     } else {
  272:         $result = 'ok';
  273:         my @pairs=split(/\&/,$rep);
  274:         foreach my $item (@pairs) {
  275:             my ($key,$value)=split(/=/,$item,2);
  276:             my $what = &unescape($key);
  277:             $returnhash{$what}=&thaw_unescape($value);
  278:         }
  279:     }
  280:     return ($result,\%returnhash);
  281: }
  282: 
  283: sub get_server_loncaparev {
  284:     my ($dom,$lonhost,$ignore_cache,$caller) = @_;
  285:     if (defined($lonhost)) {
  286:         if (!defined(&hostname($lonhost))) {
  287:             undef($lonhost);
  288:         }
  289:     }
  290:     if (!defined($lonhost)) {
  291:         if (defined(&domain($dom,'primary'))) {
  292:             $lonhost=&domain($dom,'primary');
  293:             if ($lonhost eq 'no_host') {
  294:                 undef($lonhost);
  295:             }
  296:         }
  297:     }
  298:     if (defined($lonhost)) {
  299:         my $cachetime = 12*3600;
  300:         if (!$ignore_cache) {
  301:             my ($loncaparev,$cached)=&is_cached_new('serverloncaparev',$lonhost);
  302:             if (defined($cached)) {
  303:                 return $loncaparev;
  304:             }
  305:         }
  306:         my ($answer,$loncaparev);
  307:         my @ids=&current_machine_ids();
  308:         if (grep(/^\Q$lonhost\E$/,@ids)) {
  309:             $answer = $perlvar{'lonVersion'};
  310:             if ($answer =~ /^[\'\"]?([\w.\-]+)[\'\"]?$/) {
  311:                 $loncaparev = $1;
  312:             }
  313:         } else {
  314:             $answer = &reply('serverloncaparev',$lonhost);
  315:             if (($answer eq 'unknown_cmd') || ($answer eq 'con_lost')) {
  316:                 if ($caller eq 'loncron') {
  317:                     my $hostname = &hostname($lonhost);
  318:                     my $protocol = $protocol{$lonhost};
  319:                     $protocol = 'http' if ($protocol ne 'https');
  320:                     my $url = $protocol.'://'.$hostname.'/adm/about.html';
  321:                     my $request=new HTTP::Request('GET',$url);
  322:                     my $response=&LONCAPA::LWPReq::makerequest($lonhost,$request,'',\%perlvar,4,1);
  323:                     unless ($response->is_error()) {
  324:                         my $content = $response->content;
  325:                         if ($content =~ /<p>VERSION\:\s*([\w.\-]+)<\/p>/) {
  326:                             $loncaparev = $1;
  327:                         }
  328:                     }
  329:                 } else {
  330:                     $loncaparev = $loncaparevs{$lonhost};
  331:                 }
  332:             } elsif ($answer =~ /^[\'\"]?([\w.\-]+)[\'\"]?$/) {
  333:                 $loncaparev = $1;
  334:             }
  335:         }
  336:         return &do_cache_new('serverloncaparev',$lonhost,$loncaparev,$cachetime);
  337:     }
  338: }
  339: 
  340: sub get_server_homeID {
  341:     my ($hostname,$ignore_cache,$caller) = @_;
  342:     unless ($ignore_cache) {
  343:         my ($serverhomeID,$cached)=&is_cached_new('serverhomeID',$hostname);
  344:         if (defined($cached)) {
  345:             return $serverhomeID;
  346:         }
  347:     }
  348:     my $cachetime = 12*3600;
  349:     my $serverhomeID;
  350:     if ($caller eq 'loncron') { 
  351:         my @machine_ids = &machine_ids($hostname);
  352:         foreach my $id (@machine_ids) {
  353:             my $response = &reply('serverhomeID',$id);
  354:             unless (($response eq 'unknown_cmd') || ($response eq 'con_lost')) {
  355:                 $serverhomeID = $response;
  356:                 last;
  357:             }
  358:         }
  359:         if ($serverhomeID eq '') {
  360:             $serverhomeID = $machine_ids[-1];
  361:         }
  362:     } else {
  363:         $serverhomeID = $serverhomeIDs{$hostname};
  364:     }
  365:     return &do_cache_new('serverhomeID',$hostname,$serverhomeID,$cachetime);
  366: }
  367: 
  368: sub get_remote_globals {
  369:     my ($lonhost,$whathash,$ignore_cache) = @_;
  370:     my ($result,%returnhash,%whatneeded);
  371:     if (ref($whathash) eq 'HASH') {
  372:         foreach my $what (sort(keys(%{$whathash}))) {
  373:             my $hashid = $lonhost.'-'.$what;
  374:             my ($response,$cached);
  375:             unless ($ignore_cache) {
  376:                 ($response,$cached)=&is_cached_new('lonnetglobal',$hashid);
  377:             }
  378:             if (defined($cached)) {
  379:                 $returnhash{$what} = $response;
  380:             } else {
  381:                 $whatneeded{$what} = 1;
  382:             }
  383:         }
  384:         if (keys(%whatneeded) == 0) {
  385:             $result = 'ok';
  386:         } else {
  387:             my $requested = &freeze_escape(\%whatneeded);
  388:             my $rep=&reply('readlonnetglobal:'.$requested,$lonhost);
  389:             if (($rep=~/^(refused|rejected|error)/) || ($rep eq 'con_lost') ||
  390:                 ($rep eq 'unknown_cmd')) {
  391:                 $result = $rep;
  392:             } else {
  393:                 $result = 'ok';
  394:                 my @pairs=split(/\&/,$rep);
  395:                 foreach my $item (@pairs) {
  396:                     my ($key,$value)=split(/=/,$item,2);
  397:                     my $what = &unescape($key);
  398:                     my $hashid = $lonhost.'-'.$what;
  399:                     $returnhash{$what}=&thaw_unescape($value);
  400:                     &do_cache_new('lonnetglobal',$hashid,$returnhash{$what},600);
  401:                 }
  402:             }
  403:         }
  404:     }
  405:     return ($result,\%returnhash);
  406: }
  407: 
  408: sub remote_devalidate_cache {
  409:     my ($lonhost,$cachekeys) = @_;
  410:     my $items;
  411:     return unless (ref($cachekeys) eq 'ARRAY');
  412:     my $cachestr = join('&',@{$cachekeys});
  413:     my $response = &reply('devalidatecache:'.&escape($cachestr),$lonhost);
  414:     return $response;
  415: }
  416: 
  417: # -------------------------------------------------- Non-critical communication
  418: sub subreply {
  419:     my ($cmd,$server)=@_;
  420:     my $peerfile="$perlvar{'lonSockDir'}/".&hostname($server);
  421:     #
  422:     #  With loncnew process trimming, there's a timing hole between lonc server
  423:     #  process exit and the master server picking up the listen on the AF_UNIX
  424:     #  socket.  In that time interval, a lock file will exist:
  425: 
  426:     my $lockfile=$peerfile.".lock";
  427:     while (-e $lockfile) {	# Need to wait for the lockfile to disappear.
  428: 	sleep(0.1);
  429:     }
  430:     # At this point, either a loncnew parent is listening or an old lonc
  431:     # or loncnew child is listening so we can connect or everything's dead.
  432:     #
  433:     #   We'll give the connection a few tries before abandoning it.  If
  434:     #   connection is not possible, we'll con_lost back to the client.
  435:     #   
  436:     my $client;
  437:     for (my $retries = 0; $retries < $max_connection_retries; $retries++) {
  438: 	$client=IO::Socket::UNIX->new(Peer    =>"$peerfile",
  439: 				      Type    => SOCK_STREAM,
  440: 				      Timeout => 10);
  441: 	if ($client) {
  442: 	    last;		# Connected!
  443: 	} else {
  444: 	    &create_connection(&hostname($server),$server);
  445: 	}
  446:         sleep(0.1);	# Try again later if failed connection.
  447:     }
  448:     my $answer;
  449:     if ($client) {
  450: 	print $client "sethost:$server:$cmd\n";
  451: 	$answer=<$client>;
  452: 	if (!$answer) { $answer="con_lost"; }
  453: 	chomp($answer);
  454:     } else {
  455: 	$answer = 'con_lost';	# Failed connection.
  456:     }
  457:     return $answer;
  458: }
  459: 
  460: sub reply {
  461:     my ($cmd,$server)=@_;
  462:     unless (defined(&hostname($server))) { return 'no_such_host'; }
  463:     my $answer=subreply($cmd,$server);
  464:     if (($answer=~/^refused/) || ($answer=~/^rejected/)) {
  465:         my $logged = $cmd;
  466:         if ($cmd =~ /^encrypt:([^:]+):/) {
  467:             my $subcmd = $1;
  468:             if (($subcmd eq 'auth') || ($subcmd eq 'passwd') ||
  469:                 ($subcmd eq 'changeuserauth') || ($subcmd eq 'makeuser') ||
  470:                 ($subcmd eq 'putdom') || ($subcmd eq 'autoexportgrades')) {
  471:                 (undef,undef,my @rest) = split(/:/,$cmd);
  472:                 if (($subcmd eq 'auth') || ($subcmd eq 'putdom')) {
  473:                     splice(@rest,2,1,'Hidden');
  474:                 } elsif ($subcmd eq 'passwd') {
  475:                     splice(@rest,2,2,('Hidden','Hidden'));
  476:                 } elsif (($subcmd eq 'changeuserauth') || ($subcmd eq 'makeuser') ||
  477:                          ($subcmd eq 'autoexportgrades')) {
  478:                     splice(@rest,3,1,'Hidden');
  479:                 }
  480:                 $logged = join(':',('encrypt:'.$subcmd,@rest));
  481:             }
  482:         }
  483:         &logthis("<font color=\"blue\">WARNING:".
  484:                  " $logged to $server returned $answer</font>");
  485:     }
  486:     return $answer;
  487: }
  488: 
  489: # ----------------------------------------------------------- Send USR1 to lonc
  490: 
  491: sub reconlonc {
  492:     my ($lonid) = @_;
  493:     if ($lonid) {
  494:         my $hostname = &hostname($lonid);
  495: 	my $peerfile="$perlvar{'lonSockDir'}/$hostname";
  496: 	if ($hostname && -e $peerfile) {
  497: 	    &logthis("Trying to reconnect lonc for $lonid ($hostname)");
  498: 	    my $client=IO::Socket::UNIX->new(Peer    => $peerfile,
  499: 					     Type    => SOCK_STREAM,
  500: 					     Timeout => 10);
  501: 	    if ($client) {
  502: 		print $client ("reset_retries\n");
  503: 		my $answer=<$client>;
  504: 		#reset just this one.
  505: 	    }
  506: 	}
  507: 	return;
  508:     }
  509: 
  510:     &logthis("Trying to reconnect lonc");
  511:     my $loncfile="$perlvar{'lonDaemons'}/logs/lonc.pid";
  512:     if (open(my $fh,"<",$loncfile)) {
  513: 	my $loncpid=<$fh>;
  514:         chomp($loncpid);
  515:         if (kill 0 => $loncpid) {
  516: 	    &logthis("lonc at pid $loncpid responding, sending USR1");
  517:             kill USR1 => $loncpid;
  518:             sleep 1;
  519:         } else {
  520: 	    &logthis(
  521:                "<font color=\"blue\">WARNING:".
  522:                " lonc at pid $loncpid not responding, giving up</font>");
  523:         }
  524:     } else {
  525: 	&logthis('<font color="blue">WARNING: lonc not running, giving up</font>');
  526:     }
  527: }
  528: 
  529: # ------------------------------------------------------ Critical communication
  530: 
  531: sub critical {
  532:     my ($cmd,$server)=@_;
  533:     unless (&hostname($server)) {
  534:         &logthis("<font color=\"blue\">WARNING:".
  535:                " Critical message to unknown server ($server)</font>");
  536:         return 'no_such_host';
  537:     }
  538:     my $answer=reply($cmd,$server);
  539:     if ($answer eq 'con_lost') {
  540: 	&reconlonc($server);
  541: 	my $answer=reply($cmd,$server);
  542:         if ($answer eq 'con_lost') {
  543:             my $now=time;
  544:             my $middlename=$cmd;
  545:             $middlename=substr($middlename,0,16);
  546:             $middlename=~s/\W//g;
  547:             my $dfilename=
  548:       "$perlvar{'lonSockDir'}/delayed/$now.$dumpcount.$$.$middlename.$server";
  549:             $dumpcount++;
  550:             {
  551: 		my $dfh;
  552: 		if (open($dfh,">",$dfilename)) {
  553: 		    print $dfh "$cmd\n"; 
  554: 		    close($dfh);
  555: 		}
  556:             }
  557:             sleep 1;
  558:             my $wcmd='';
  559:             {
  560: 		my $dfh;
  561: 		if (open($dfh,"<",$dfilename)) {
  562: 		    $wcmd=<$dfh>; 
  563: 		    close($dfh);
  564: 		}
  565:             }
  566:             chomp($wcmd);
  567:             if ($wcmd eq $cmd) {
  568: 		&logthis("<font color=\"blue\">WARNING: ".
  569:                          "Connection buffer $dfilename: $cmd</font>");
  570:                 &logperm("D:$server:$cmd");
  571: 	        return 'con_delayed';
  572:             } else {
  573:                 &logthis("<font color=\"red\">CRITICAL:"
  574:                         ." Critical connection failed: $server $cmd</font>");
  575:                 &logperm("F:$server:$cmd");
  576:                 return 'con_failed';
  577:             }
  578:         }
  579:     }
  580:     return $answer;
  581: }
  582: 
  583: # ------------------------------------------- check if return value is an error
  584: 
  585: sub error {
  586:     my ($result) = @_;
  587:     if ($result =~ /^(con_lost|no_such_host|error: (\d+) (.*))/) {
  588: 	if ($2 == 2) { return undef; }
  589: 	return $1;
  590:     }
  591:     return undef;
  592: }
  593: 
  594: sub convert_and_load_session_env {
  595:     my ($lonidsdir,$handle)=@_;
  596:     my @profile;
  597:     {
  598: 	my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
  599: 	if (!$opened) {
  600: 	    return 0;
  601: 	}
  602: 	flock($idf,LOCK_SH);
  603: 	@profile=<$idf>;
  604: 	close($idf);
  605:     }
  606:     my %temp_env;
  607:     foreach my $line (@profile) {
  608: 	if ($line !~ m/=/) {
  609: 	    return 0;
  610: 	}
  611: 	chomp($line);
  612: 	my ($envname,$envvalue)=split(/=/,$line,2);
  613: 	$temp_env{&unescape($envname)} = &unescape($envvalue);
  614:     }
  615:     unlink("$lonidsdir/$handle.id");
  616:     if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",&GDBM_WRCREAT(),
  617: 	    0640)) {
  618: 	%disk_env = %temp_env;
  619: 	@env{keys(%temp_env)} = @disk_env{keys(%temp_env)};
  620: 	untie(%disk_env);
  621:     }
  622:     return 1;
  623: }
  624: 
  625: # ------------------------------------------- Transfer profile into environment
  626: my $env_loaded;
  627: sub transfer_profile_to_env {
  628:     my ($lonidsdir,$handle,$force_transfer) = @_;
  629:     if (!$force_transfer && $env_loaded) { return; } 
  630: 
  631:     if (!defined($lonidsdir)) {
  632: 	$lonidsdir = $perlvar{'lonIDsDir'};
  633:     }
  634:     if (!defined($handle)) {
  635:         ($handle) = ($env{'user.environment'} =~m|/([^/]+)\.id$| );
  636:     }
  637: 
  638:     my $convert;
  639:     {
  640:     	my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
  641: 	if (!$opened) {
  642: 	    return;
  643: 	}
  644: 	flock($idf,LOCK_SH);
  645: 	if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",
  646: 		&GDBM_READER(),0640)) {
  647: 	    @env{keys(%disk_env)} = @disk_env{keys(%disk_env)};
  648: 	    untie(%disk_env);
  649: 	} else {
  650: 	    $convert = 1;
  651: 	}
  652:     }
  653:     if ($convert) {
  654: 	if (!&convert_and_load_session_env($lonidsdir,$handle)) {
  655: 	    &logthis("Failed to load session, or convert session.");
  656: 	}
  657:     }
  658: 
  659:     my %remove;
  660:     while ( my $envname = each(%env) ) {
  661:         if (my ($key,$time) = ($envname =~ /^(cgi\.(\d+)_\d+\.)/)) {
  662:             if ($time < time-300) {
  663:                 $remove{$key}++;
  664:             }
  665:         }
  666:     }
  667: 
  668:     $env{'user.environment'} = "$lonidsdir/$handle.id";
  669:     $env_loaded=1;
  670:     foreach my $expired_key (keys(%remove)) {
  671:         &delenv($expired_key);
  672:     }
  673: }
  674: 
  675: # ---------------------------------------------------- Check for valid session 
  676: sub check_for_valid_session {
  677:     my ($r,$name,$userhashref,$domref) = @_;
  678:     my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
  679:     my ($lonidsdir,$linkname,$pubname,$secure,$lonid);
  680:     if ($name eq 'lonDAV') {
  681:         $lonidsdir=$r->dir_config('lonDAVsessDir');
  682:     } else {
  683:         $lonidsdir=$r->dir_config('lonIDsDir');
  684:         if ($name eq '') {
  685:             $name = 'lonID';
  686:         }
  687:     }
  688:     if ($name eq 'lonID') {
  689:         $secure = 'lonSID';
  690:         $linkname = 'lonLinkID';
  691:         $pubname = 'lonPubID';
  692:         if (exists($cookies{$secure})) {
  693:             $lonid=$cookies{$secure};
  694:         } elsif (exists($cookies{$name})) {
  695:             $lonid=$cookies{$name};
  696:         } elsif ((exists($cookies{$linkname})) && ($ENV{'SERVER_PORT'} != 443)) {
  697:             $lonid=$cookies{$linkname};
  698:         } elsif (exists($cookies{$pubname})) {
  699:             $lonid=$cookies{$pubname};
  700:         }
  701:     } else {
  702:         $lonid=$cookies{$name};
  703:     }
  704:     return undef if (!$lonid);
  705: 
  706:     my $handle=&LONCAPA::clean_handle($lonid->value);
  707:     if (-l "$lonidsdir/$handle.id") {
  708:         my $link = readlink("$lonidsdir/$handle.id");
  709:         if ((-e $link) && ($link =~ m{^\Q$lonidsdir\E/(.+)\.id$})) {
  710:             $handle = $1;
  711:         }
  712:     }
  713:     if (!-e "$lonidsdir/$handle.id") {
  714:         if ((ref($domref)) && ($name eq 'lonID') && 
  715:             ($handle =~ /^($match_username)\_\d+\_($match_domain)\_(.+)$/)) {
  716:             my ($possuname,$possudom,$possuhome) = ($1,$2,$3);
  717:             if ((&domain($possudom) ne '') && (&homeserver($possuname,$possudom) eq $possuhome)) {
  718:                 $$domref = $possudom;
  719:             }
  720:         }
  721:         return undef;
  722:     }
  723: 
  724:     my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
  725:     return undef if (!$opened);
  726: 
  727:     flock($idf,LOCK_SH);
  728:     my %disk_env;
  729:     if (!tie(%disk_env,'GDBM_File',"$lonidsdir/$handle.id",
  730: 	    &GDBM_READER(),0640)) {
  731: 	return undef;	
  732:     }
  733: 
  734:     if (!defined($disk_env{'user.name'})
  735: 	|| !defined($disk_env{'user.domain'})) {
  736:         untie(%disk_env);
  737: 	return undef;
  738:     }
  739: 
  740:     if (ref($userhashref) eq 'HASH') {
  741:         $userhashref->{'name'} = $disk_env{'user.name'};
  742:         $userhashref->{'domain'} = $disk_env{'user.domain'};
  743:         if ($disk_env{'request.role'}) {
  744:             $userhashref->{'role'} = $disk_env{'request.role'};
  745:         }
  746:         $userhashref->{'lti'} = $disk_env{'request.lti.login'};
  747:         if ($userhashref->{'lti'}) {
  748:             $userhashref->{'ltitarget'} = $disk_env{'request.lti.target'};
  749:             $userhashref->{'ltiuri'} = $disk_env{'request.lti.uri'};
  750:         }
  751:     }
  752:     untie(%disk_env);
  753: 
  754:     return $handle;
  755: }
  756: 
  757: sub timed_flock {
  758:     my ($file,$lock_type) = @_;
  759:     my $failed=0;
  760:     eval {
  761: 	local $SIG{__DIE__}='DEFAULT';
  762: 	local $SIG{ALRM}=sub {
  763: 	    $failed=1;
  764: 	    die("failed lock");
  765: 	};
  766: 	alarm(13);
  767: 	flock($file,$lock_type);
  768: 	alarm(0);
  769:     };
  770:     if ($failed) {
  771: 	return undef;
  772:     } else {
  773: 	return 1;
  774:     }
  775: }
  776: 
  777: sub get_sessionfile_vars {
  778:     my ($handle,$lonidsdir,$storearr) = @_;
  779:     my %returnhash;
  780:     unless (ref($storearr) eq 'ARRAY') {
  781:         return %returnhash;
  782:     }
  783:     if (-l "$lonidsdir/$handle.id") {
  784:         my $link = readlink("$lonidsdir/$handle.id");
  785:         if ((-e $link) && ($link =~ m{^\Q$lonidsdir\E/(.+)\.id$})) {
  786:             $handle = $1;
  787:         }
  788:     }
  789:     if ((-e "$lonidsdir/$handle.id") &&
  790:         ($handle =~ /^($match_username)\_\d+\_($match_domain)\_(.+)$/)) {
  791:         my ($possuname,$possudom,$possuhome) = ($1,$2,$3);
  792:         if ((&domain($possudom) ne '') && (&homeserver($possuname,$possudom) eq $possuhome)) {
  793:             if (open(my $idf,'+<',"$lonidsdir/$handle.id")) {
  794:                 flock($idf,LOCK_SH);
  795:                 if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",
  796:                         &GDBM_READER(),0640)) {
  797:                     foreach my $item (@{$storearr}) {
  798:                         $returnhash{$item} = $disk_env{$item};
  799:                     }
  800:                     untie(%disk_env);
  801:                 }
  802:             }
  803:         }
  804:     }
  805:     return %returnhash;
  806: }
  807: 
  808: # ---------------------------------------------------------- Append Environment
  809: 
  810: sub appenv {
  811:     my ($newenv,$roles) = @_;
  812:     if (ref($newenv) eq 'HASH') {
  813:         foreach my $key (keys(%{$newenv})) {
  814:             my $refused = 0;
  815: 	    if (($key =~ /^user\.role/) || ($key =~ /^user\.priv/)) {
  816:                 $refused = 1;
  817:                 if (ref($roles) eq 'ARRAY') {
  818:                     my ($type,$role) = ($key =~ m{^user\.(role|priv)\.(.+?)\./});
  819:                     if (grep(/^\Q$role\E$/,@{$roles})) {
  820:                         $refused = 0;
  821:                     }
  822:                 }
  823:             }
  824:             if ($refused) {
  825:                 &logthis("<font color=\"blue\">WARNING: ".
  826:                          "Attempt to modify environment ".$key." to ".$newenv->{$key}
  827:                          .'</font>');
  828: 	        delete($newenv->{$key});
  829:             } else {
  830:                 $env{$key}=$newenv->{$key};
  831:             }
  832:         }
  833:         my $lonids = $perlvar{'lonIDsDir'};
  834:         if ($env{'user.environment'} =~ m{^\Q$lonids/\E$match_username\_\d+\_$match_domain\_[\w\-.]+\.id$}) {
  835:             my $opened = open(my $env_file,'+<',$env{'user.environment'});
  836:             if ($opened
  837: 	        && &timed_flock($env_file,LOCK_EX)
  838: 	        &&
  839: 	        tie(my %disk_env,'GDBM_File',$env{'user.environment'},
  840: 	            (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
  841: 	        while (my ($key,$value) = each(%{$newenv})) {
  842: 	            $disk_env{$key} = $value;
  843: 	        }
  844: 	        untie(%disk_env);
  845:             }
  846:         }
  847:     }
  848:     return 'ok';
  849: }
  850: # ----------------------------------------------------- Delete from Environment
  851: 
  852: sub delenv {
  853:     my ($delthis,$regexp,$roles) = @_;
  854:     if (($delthis=~/^user\.role/) || ($delthis=~/^user\.priv/)) {
  855:         my $refused = 1;
  856:         if (ref($roles) eq 'ARRAY') {
  857:             my ($type,$role) = ($delthis =~ /^user\.(role|priv)\.([^.]+)\./);
  858:             if (grep(/^\Q$role\E$/,@{$roles})) {
  859:                 $refused = 0;
  860:             }
  861:         }
  862:         if ($refused) {
  863:             &logthis("<font color=\"blue\">WARNING: ".
  864:                      "Attempt to delete from environment ".$delthis);
  865:             return 'error';
  866:         }
  867:     }
  868:     my $opened = open(my $env_file,'+<',$env{'user.environment'});
  869:     if ($opened
  870: 	&& &timed_flock($env_file,LOCK_EX)
  871: 	&&
  872: 	tie(my %disk_env,'GDBM_File',$env{'user.environment'},
  873: 	    (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
  874: 	foreach my $key (keys(%disk_env)) {
  875: 	    if ($regexp) {
  876:                 if ($key=~/^$delthis/) {
  877:                     delete($env{$key});
  878:                     delete($disk_env{$key});
  879:                 } 
  880:             } else {
  881:                 if ($key=~/^\Q$delthis\E/) {
  882: 		    delete($env{$key});
  883: 		    delete($disk_env{$key});
  884: 	        }
  885:             }
  886: 	}
  887: 	untie(%disk_env);
  888:     }
  889:     return 'ok';
  890: }
  891: 
  892: sub get_env_multiple {
  893:     my ($name) = @_;
  894:     my @values;
  895:     if (defined($env{$name})) {
  896:         # exists is it an array
  897:         if (ref($env{$name})) {
  898:             @values=@{ $env{$name} };
  899:         } else {
  900:             $values[0]=$env{$name};
  901:         }
  902:     }
  903:     return(@values);
  904: }
  905: 
  906: # ------------------------------------------------------------------- Locking
  907: 
  908: sub set_lock {
  909:     my ($text)=@_;
  910:     $locknum++;
  911:     my $id=$$.'-'.$locknum;
  912:     &appenv({'session.locks' => $env{'session.locks'}.','.$id,
  913:              'session.lock.'.$id => $text});
  914:     return $id;
  915: }
  916: 
  917: sub get_locks {
  918:     my $num=0;
  919:     my %texts=();
  920:     foreach my $lock (split(/\,/,$env{'session.locks'})) {
  921:        if ($lock=~/\w/) {
  922:           $num++;
  923:           $texts{$lock}=$env{'session.lock.'.$lock};
  924:        }
  925:    }
  926:    return ($num,%texts);
  927: }
  928: 
  929: sub remove_lock {
  930:     my ($id)=@_;
  931:     my $newlocks='';
  932:     foreach my $lock (split(/\,/,$env{'session.locks'})) {
  933:        if (($lock=~/\w/) && ($lock ne $id)) {
  934:           $newlocks.=','.$lock;
  935:        }
  936:     }
  937:     &appenv({'session.locks' => $newlocks});
  938:     &delenv('session.lock.'.$id);
  939: }
  940: 
  941: sub remove_all_locks {
  942:     my $activelocks=$env{'session.locks'};
  943:     foreach my $lock (split(/\,/,$env{'session.locks'})) {
  944:        if ($lock=~/\w/) {
  945:           &remove_lock($lock);
  946:        }
  947:     }
  948: }
  949: 
  950: 
  951: # ------------------------------------------ Find out current server userload
  952: sub userload {
  953:     my $numusers=0;
  954:     {
  955: 	opendir(LONIDS,$perlvar{'lonIDsDir'});
  956: 	my $filename;
  957: 	my $curtime=time;
  958: 	while ($filename=readdir(LONIDS)) {
  959: 	    next if ($filename eq '.' || $filename eq '..');
  960: 	    next if ($filename =~ /publicuser_\d+\.id/);
  961:             next if ($filename =~ /^[a-f0-9]+_linked\.id$/);
  962: 	    my ($mtime)=(stat($perlvar{'lonIDsDir'}.'/'.$filename))[9];
  963: 	    if ($curtime-$mtime < 1800) { $numusers++; }
  964: 	}
  965: 	closedir(LONIDS);
  966:     }
  967:     my $userloadpercent=0;
  968:     my $maxuserload=$perlvar{'lonUserLoadLim'};
  969:     if ($maxuserload) {
  970: 	$userloadpercent=100*$numusers/$maxuserload;
  971:     }
  972:     $userloadpercent=sprintf("%.2f",$userloadpercent);
  973:     return $userloadpercent;
  974: }
  975: 
  976: # ------------------------------ Find server with least workload from spare.tab
  977: 
  978: sub spareserver {
  979:     my ($loadpercent,$userloadpercent,$want_server_name,$udom) = @_;
  980:     my $spare_server;
  981:     if ($userloadpercent !~ /\d/) { $userloadpercent=0; }
  982:     my $lowest_load=($loadpercent > $userloadpercent) ? $loadpercent 
  983:                                                      :  $userloadpercent;
  984:     my ($uint_dom,$remotesessions);
  985:     if (($udom ne '') && (&domain($udom) ne '')) {
  986:         my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
  987:         $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
  988:         my %udomdefaults = &Apache::lonnet::get_domain_defaults($udom);
  989:         $remotesessions = $udomdefaults{'remotesessions'};
  990:     }
  991:     my $spareshash = &this_host_spares($udom);
  992:     if (ref($spareshash) eq 'HASH') {
  993:         if (ref($spareshash->{'primary'}) eq 'ARRAY') {
  994:             foreach my $try_server (@{ $spareshash->{'primary'} }) {
  995:                 next unless (&spare_can_host($udom,$uint_dom,$remotesessions,
  996:                                              $try_server));
  997: 	        ($spare_server, $lowest_load) =
  998: 	            &compare_server_load($try_server, $spare_server, $lowest_load);
  999:             }
 1000:         }
 1001: 
 1002:         my $found_server = ($spare_server ne '' && $lowest_load < 100);
 1003: 
 1004:         if (!$found_server) {
 1005:             if (ref($spareshash->{'default'}) eq 'ARRAY') { 
 1006: 	        foreach my $try_server (@{ $spareshash->{'default'} }) {
 1007:                     next unless (&spare_can_host($udom,$uint_dom,
 1008:                                                  $remotesessions,$try_server));
 1009: 	            ($spare_server, $lowest_load) =
 1010: 		        &compare_server_load($try_server, $spare_server, $lowest_load);
 1011:                 }
 1012: 	    }
 1013:         }
 1014:     }
 1015: 
 1016:     if (!$want_server_name) {
 1017:         if (defined($spare_server)) {
 1018:             my $hostname = &hostname($spare_server);
 1019:             if (defined($hostname)) {
 1020:                 my $protocol = 'http';
 1021:                 if ($protocol{$spare_server} eq 'https') {
 1022:                     $protocol = $protocol{$spare_server};
 1023:                 }
 1024: 	        $spare_server = $protocol.'://'.$hostname;
 1025:             }
 1026:         }
 1027:     }
 1028:     return $spare_server;
 1029: }
 1030: 
 1031: sub compare_server_load {
 1032:     my ($try_server, $spare_server, $lowest_load, $required) = @_;
 1033: 
 1034:     if ($required) {
 1035:         my ($reqdmajor,$reqdminor) = ($required =~ /^(\d+)\.(\d+)$/);
 1036:         my $remoterev = &get_server_loncaparev(undef,$try_server);
 1037:         my ($major,$minor) = ($remoterev =~ /^\'?(\d+)\.(\d+)\.[\w.\-]+\'?$/);
 1038:         if (($major eq '' && $minor eq '') ||
 1039:             (($reqdmajor > $major) || (($reqdmajor == $major) && ($reqdminor > $minor)))) {
 1040:             return ($spare_server,$lowest_load);
 1041:         }
 1042:     }
 1043: 
 1044:     my $loadans     = &reply('load',    $try_server);
 1045:     my $userloadans = &reply('userload',$try_server);
 1046: 
 1047:     if ($loadans !~ /\d/ && $userloadans !~ /\d/) {
 1048: 	return ($spare_server, $lowest_load); #didn't get a number from the server
 1049:     }
 1050: 
 1051:     my $load;
 1052:     if ($loadans =~ /\d/) {
 1053: 	if ($userloadans =~ /\d/) {
 1054: 	    #both are numbers, pick the bigger one
 1055: 	    $load = ($loadans > $userloadans) ? $loadans 
 1056: 		                              : $userloadans;
 1057: 	} else {
 1058: 	    $load = $loadans;
 1059: 	}
 1060:     } else {
 1061: 	$load = $userloadans;
 1062:     }
 1063: 
 1064:     if (($load =~ /\d/) && ($load < $lowest_load)) {
 1065: 	$spare_server = $try_server;
 1066: 	$lowest_load  = $load;
 1067:     }
 1068:     return ($spare_server,$lowest_load);
 1069: }
 1070: 
 1071: # --------------------------- ask offload servers if user already has a session
 1072: sub find_existing_session {
 1073:     my ($udom,$uname) = @_;
 1074:     my $spareshash = &this_host_spares($udom);
 1075:     if (ref($spareshash) eq 'HASH') {
 1076:         if (ref($spareshash->{'primary'}) eq 'ARRAY') {
 1077:             foreach my $try_server (@{ $spareshash->{'primary'} }) {
 1078:                 return $try_server if (&has_user_session($try_server, $udom, $uname));
 1079:             }
 1080:         }
 1081:         if (ref($spareshash->{'default'}) eq 'ARRAY') {
 1082:             foreach my $try_server (@{ $spareshash->{'default'} }) {
 1083:                 return $try_server if (&has_user_session($try_server, $udom, $uname));
 1084:             }
 1085:         }
 1086:     }
 1087:     return;
 1088: }
 1089: 
 1090: sub delusersession {
 1091:     my ($lonid,$udom,$uname) = @_;
 1092:     my $uprimary_id = &domain($udom,'primary');
 1093:     my $uintdom = &internet_dom($uprimary_id);
 1094:     my $intdom = &internet_dom($lonid);
 1095:     my $serverhomedom = &host_domain($lonid);
 1096:     if (($uintdom ne '') && ($uintdom eq $intdom)) {
 1097:         return &reply(join(':','delusersession',
 1098:                             map {&escape($_)} ($udom,$uname)),$lonid);
 1099:     }
 1100:     return;
 1101: }
 1102: 
 1103: # check if user's browser sent load balancer cookie and server still has session
 1104: # and is not overloaded.
 1105: sub check_for_balancer_cookie {
 1106:     my ($r,$update_mtime) = @_;
 1107:     my ($otherserver,$cookie);
 1108:     my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
 1109:     if (exists($cookies{'balanceID'})) {
 1110:         my $balid = $cookies{'balanceID'};
 1111:         $cookie=&LONCAPA::clean_handle($balid->value);
 1112:         my $balancedir=$r->dir_config('lonBalanceDir');
 1113:         if ((-d $balancedir) && (-e "$balancedir/$cookie.id")) {
 1114:             if ($cookie =~ /^($match_domain)_($match_username)_[a-f0-9]+$/) {
 1115:                 my ($possudom,$possuname) = ($1,$2);
 1116:                 my $has_session = 0;
 1117:                 if ((&domain($possudom) ne '') &&
 1118:                     (&homeserver($possuname,$possudom) ne 'no_host')) {
 1119:                     my $try_server;
 1120:                     my $opened = open(my $idf,'+<',"$balancedir/$cookie.id");
 1121:                     if ($opened) {
 1122:                         flock($idf,LOCK_SH);
 1123:                         while (my $line = <$idf>) {
 1124:                             chomp($line);
 1125:                             if (&hostname($line) ne '') {
 1126:                                 $try_server = $line;
 1127:                                 last;
 1128:                             }
 1129:                         }
 1130:                         close($idf);
 1131:                         if (($try_server) &&
 1132:                             (&has_user_session($try_server,$possudom,$possuname))) {
 1133:                             my $lowest_load = 30000;
 1134:                             ($otherserver,$lowest_load) =
 1135:                                 &compare_server_load($try_server,undef,$lowest_load);
 1136:                             if ($otherserver ne '' && $lowest_load < 100) {
 1137:                                 $has_session = 1;
 1138:                             } else {
 1139:                                 undef($otherserver);
 1140:                             }
 1141:                         }
 1142:                     }
 1143:                 }
 1144:                 if ($has_session) {
 1145:                     if ($update_mtime) {
 1146:                         my $atime = my $mtime = time;
 1147:                         utime($atime,$mtime,"$balancedir/$cookie.id");
 1148:                     }
 1149:                 } else {
 1150:                     unlink("$balancedir/$cookie.id");
 1151:                 }
 1152:             }
 1153:         }
 1154:     }
 1155:     return ($otherserver,$cookie);
 1156: }
 1157: 
 1158: sub updatebalcookie {
 1159:     my ($cookie,$balancer,$lastentry)=@_;
 1160:     if ($cookie =~ /^($match_domain)\_($match_username)\_[a-f0-9]{32}$/) {
 1161:         my ($udom,$uname) = ($1,$2);
 1162:         my $uprimary_id = &domain($udom,'primary');
 1163:         my $uintdom = &internet_dom($uprimary_id);
 1164:         my $intdom = &internet_dom($balancer);
 1165:         my $serverhomedom = &host_domain($balancer);
 1166:         if (($uintdom ne '') && ($uintdom eq $intdom)) {
 1167:             return &reply('updatebalcookie:'.&escape($cookie).':'.&escape($lastentry),$balancer);
 1168:         }
 1169:     }
 1170:     return;
 1171: }
 1172: 
 1173: sub delbalcookie {
 1174:     my ($cookie,$balancer) =@_;
 1175:     if ($cookie =~ /^($match_domain)\_($match_username)\_[a-f0-9]{32}$/) {
 1176:         my ($udom,$uname) = ($1,$2);
 1177:         my $uprimary_id = &domain($udom,'primary');
 1178:         my $uintdom = &internet_dom($uprimary_id);
 1179:         my $intdom = &internet_dom($balancer);
 1180:         my $serverhomedom = &host_domain($balancer);
 1181:         if (($uintdom ne '') && ($uintdom eq $intdom)) {
 1182:             return &reply('delbalcookie:'.&escape($cookie),$balancer);
 1183:         }
 1184:     }
 1185: }
 1186: 
 1187: # -------------------------------- ask if server already has a session for user
 1188: sub has_user_session {
 1189:     my ($lonid,$udom,$uname) = @_;
 1190:     my $result = &reply(join(':','userhassession',
 1191: 			     map {&escape($_)} ($udom,$uname)),$lonid);
 1192:     return 1 if ($result eq 'ok');
 1193: 
 1194:     return 0;
 1195: }
 1196: 
 1197: # --------- determine least loaded server in a user's domain which allows login
 1198: 
 1199: sub choose_server {
 1200:     my ($udom,$checkloginvia,$required,$skiploadbal) = @_;
 1201:     my %domconfhash = &Apache::loncommon::get_domainconf($udom);
 1202:     my %servers = &get_servers($udom);
 1203:     my $lowest_load = 30000;
 1204:     my ($login_host,$hostname,$portal_path,$isredirect,$balancers);
 1205:     if ($skiploadbal) {
 1206:         ($balancers,my $cached)=&is_cached_new('loadbalancing',$udom);
 1207:         unless (defined($cached)) {
 1208:             my $cachetime = 60*60*24;
 1209:             my %domconfig =
 1210:                 &Apache::lonnet::get_dom('configuration',['loadbalancing'],$udom);
 1211:             if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 1212:                 $balancers = &do_cache_new('loadbalancing',$udom,$domconfig{'loadbalancing'},
 1213:                                            $cachetime);
 1214:             }
 1215:         }
 1216:     }
 1217:     foreach my $lonhost (keys(%servers)) {
 1218:         if ($skiploadbal) {
 1219:             if (ref($balancers) eq 'HASH') {
 1220:                 next if (exists($balancers->{$lonhost}));
 1221:             }
 1222:         }
 1223:         my $loginvia;
 1224:         if ($checkloginvia) {
 1225:             $loginvia = $domconfhash{$udom.'.login.loginvia_'.$lonhost};
 1226:             if ($loginvia) {
 1227:                 my ($server,$path) = split(/:/,$loginvia);
 1228:                 ($login_host, $lowest_load) =
 1229:                     &compare_server_load($server, $login_host, $lowest_load, $required);
 1230:                 if ($login_host eq $server) {
 1231:                     $portal_path = $path;
 1232:                     $isredirect = 1;
 1233:                 }
 1234:             } else {
 1235:                 ($login_host, $lowest_load) =
 1236:                     &compare_server_load($lonhost, $login_host, $lowest_load, $required);
 1237:                 if ($login_host eq $lonhost) {
 1238:                     $portal_path = '';
 1239:                     $isredirect = ''; 
 1240:                 }
 1241:             }
 1242:         } else {
 1243:             ($login_host, $lowest_load) =
 1244:                 &compare_server_load($lonhost, $login_host, $lowest_load, $required);
 1245:         }
 1246:     }
 1247:     if ($login_host ne '') {
 1248:         $hostname = &hostname($login_host);
 1249:     }
 1250:     return ($login_host,$hostname,$portal_path,$isredirect,$lowest_load);
 1251: }
 1252: 
 1253: sub get_course_sessions {
 1254:     my ($cnum,$cdom,$lastactivity) = @_;
 1255:     my %servers = &internet_dom_servers($cdom);
 1256:     my %returnhash;
 1257:     foreach my $server (sort(keys(%servers))) {
 1258:         my $rep = &reply("coursesessions:$cdom:$cnum:$lastactivity",$server);
 1259:         my @pairs=split(/\&/,$rep);
 1260:         unless (($rep eq 'unknown_cmd') || ($rep =~ /^error/)) {
 1261:             foreach my $item (@pairs) {
 1262:                 my ($key,$value)=split(/=/,$item,2);
 1263:                 $key = &unescape($key);
 1264:                 next if ($key =~ /^error: 2 /);
 1265:                 if (exists($returnhash{$key})) {
 1266:                     next if ($value < $returnhash{$key});
 1267:                 }
 1268:                 $returnhash{$key}=$value;
 1269:             }
 1270:         }
 1271:     }
 1272:     return %returnhash;
 1273: }
 1274: 
 1275: # --------------------------------------------- Try to change a user's password
 1276: 
 1277: sub changepass {
 1278:     my ($uname,$udom,$currentpass,$newpass,$server,$context)=@_;
 1279:     $currentpass = &escape($currentpass);
 1280:     $newpass     = &escape($newpass);
 1281:     my $lonhost = $perlvar{'lonHostID'};
 1282:     my $answer = reply("encrypt:passwd:$udom:$uname:$currentpass:$newpass:$context:$lonhost",
 1283: 		       $server);
 1284:     if (! $answer) {
 1285: 	&logthis("No reply on password change request to $server ".
 1286: 		 "by $uname in domain $udom.");
 1287:     } elsif ($answer =~ "^ok") {
 1288:         &logthis("$uname in $udom successfully changed their password ".
 1289: 		 "on $server.");
 1290:     } elsif ($answer =~ "^pwchange_failure") {
 1291: 	&logthis("$uname in $udom was unable to change their password ".
 1292: 		 "on $server.  The action was blocked by either lcpasswd ".
 1293: 		 "or pwchange");
 1294:     } elsif ($answer =~ "^non_authorized") {
 1295:         &logthis("$uname in $udom did not get their password correct when ".
 1296: 		 "attempting to change it on $server.");
 1297:     } elsif ($answer =~ "^auth_mode_error") {
 1298:         &logthis("$uname in $udom attempted to change their password despite ".
 1299: 		 "not being locally or internally authenticated on $server.");
 1300:     } elsif ($answer =~ "^unknown_user") {
 1301:         &logthis("$uname in $udom attempted to change their password ".
 1302: 		 "on $server but were unable to because $server is not ".
 1303: 		 "their home server.");
 1304:     } elsif ($answer =~ "^refused") {
 1305: 	&logthis("$server refused to change $uname in $udom password because ".
 1306: 		 "it was sent an unencrypted request to change the password.");
 1307:     } elsif ($answer =~ "invalid_client") {
 1308:         &logthis("$server refused to change $uname in $udom password because ".
 1309:                  "it was a reset by e-mail originating from an invalid server.");
 1310:     } elsif ($answer =~ "^prioruse") {
 1311:        &logthis("$server refused to change $uname in $udom password because ".
 1312:                 "the password had been used before");
 1313:     }
 1314:     return $answer;
 1315: }
 1316: 
 1317: # ----------------------- Try to determine user's current authentication scheme
 1318: 
 1319: sub queryauthenticate {
 1320:     my ($uname,$udom)=@_;
 1321:     my $uhome=&homeserver($uname,$udom);
 1322:     if (!$uhome) {
 1323: 	&logthis("User $uname at $udom is unknown when looking for authentication mechanism");
 1324: 	return 'no_host';
 1325:     }
 1326:     my $answer=reply("encrypt:currentauth:$udom:$uname",$uhome);
 1327:     if ($answer =~ /^(unknown_user|refused|con_lost)/) {
 1328: 	&logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
 1329:     }
 1330:     return $answer;
 1331: }
 1332: 
 1333: # --------- Try to authenticate user from domain's lib servers (first this one)
 1334: 
 1335: sub authenticate {
 1336:     my ($uname,$upass,$udom,$checkdefauth,$clientcancheckhost)=@_;
 1337:     $upass=&escape($upass);
 1338:     $uname= &LONCAPA::clean_username($uname);
 1339:     my $uhome=&homeserver($uname,$udom,1);
 1340:     my $newhome;
 1341:     if ((!$uhome) || ($uhome eq 'no_host')) {
 1342: # Maybe the machine was offline and only re-appeared again recently?
 1343:         &reconlonc();
 1344: # One more
 1345: 	$uhome=&homeserver($uname,$udom,1);
 1346:         if (($uhome eq 'no_host') && $checkdefauth) {
 1347:             if (defined(&domain($udom,'primary'))) {
 1348:                 $newhome=&domain($udom,'primary');
 1349:             }
 1350:             if ($newhome ne '') {
 1351:                 $uhome = $newhome;
 1352:             }
 1353:         }
 1354: 	if ((!$uhome) || ($uhome eq 'no_host')) {
 1355: 	    &logthis("User $uname at $udom is unknown in authenticate");
 1356: 	    return 'no_host';
 1357:         }
 1358:     }
 1359:     my $answer=reply("encrypt:auth:$udom:$uname:$upass:$checkdefauth:$clientcancheckhost",$uhome);
 1360:     if ($answer eq 'authorized') {
 1361:         if ($newhome) {
 1362:             &logthis("User $uname at $udom authorized by $uhome, but needs account");
 1363:             return 'no_account_on_host'; 
 1364:         } else {
 1365:             &logthis("User $uname at $udom authorized by $uhome");
 1366:             return $uhome;
 1367:         }
 1368:     }
 1369:     if ($answer eq 'non_authorized') {
 1370: 	&logthis("User $uname at $udom rejected by $uhome");
 1371: 	return 'no_host'; 
 1372:     }
 1373:     &logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
 1374:     return 'no_host';
 1375: }
 1376: 
 1377: sub can_host_session {
 1378:     my ($udom,$lonhost,$remoterev,$remotesessions,$hostedsessions) = @_;
 1379:     my $canhost = 1;
 1380:     my $host_idn = &Apache::lonnet::internet_dom($lonhost);
 1381:     if (ref($remotesessions) eq 'HASH') {
 1382:         if (ref($remotesessions->{'excludedomain'}) eq 'ARRAY') {
 1383:             if (grep(/^\Q$host_idn\E$/,@{$remotesessions->{'excludedomain'}})) {
 1384:                 $canhost = 0;
 1385:             } else {
 1386:                 $canhost = 1;
 1387:             }
 1388:         }
 1389:         if (ref($remotesessions->{'includedomain'}) eq 'ARRAY') {
 1390:             if (grep(/^\Q$host_idn\E$/,@{$remotesessions->{'includedomain'}})) {
 1391:                 $canhost = 1;
 1392:             } else {
 1393:                 $canhost = 0;
 1394:             }
 1395:         }
 1396:         if ($canhost) {
 1397:             if ($remotesessions->{'version'} ne '') {
 1398:                 my ($reqmajor,$reqminor) = ($remotesessions->{'version'} =~ /^(\d+)\.(\d+)$/);
 1399:                 if ($reqmajor ne '' && $reqminor ne '') {
 1400:                     if ($remoterev =~ /^\'?(\d+)\.(\d+)/) {
 1401:                         my $major = $1;
 1402:                         my $minor = $2;
 1403:                         if (($major < $reqmajor ) ||
 1404:                             (($major == $reqmajor) && ($minor < $reqminor))) {
 1405:                             $canhost = 0;
 1406:                         }
 1407:                     } else {
 1408:                         $canhost = 0;
 1409:                     }
 1410:                 }
 1411:             }
 1412:         }
 1413:     }
 1414:     if ($canhost) {
 1415:         if (ref($hostedsessions) eq 'HASH') {
 1416:             my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
 1417:             my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 1418:             if (ref($hostedsessions->{'excludedomain'}) eq 'ARRAY') {
 1419:                 if (($uint_dom ne '') && 
 1420:                     (grep(/^\Q$uint_dom\E$/,@{$hostedsessions->{'excludedomain'}}))) {
 1421:                     $canhost = 0;
 1422:                 } else {
 1423:                     $canhost = 1;
 1424:                 }
 1425:             }
 1426:             if (ref($hostedsessions->{'includedomain'}) eq 'ARRAY') {
 1427:                 if (($uint_dom ne '') && 
 1428:                     (grep(/^\Q$uint_dom\E$/,@{$hostedsessions->{'includedomain'}}))) {
 1429:                     $canhost = 1;
 1430:                 } else {
 1431:                     $canhost = 0;
 1432:                 }
 1433:             }
 1434:         }
 1435:     }
 1436:     return $canhost;
 1437: }
 1438: 
 1439: sub spare_can_host {
 1440:     my ($udom,$uint_dom,$remotesessions,$try_server)=@_;
 1441:     my $canhost=1;
 1442:     my $try_server_hostname = &hostname($try_server);
 1443:     my $serverhomeID = &get_server_homeID($try_server_hostname);
 1444:     my $serverhomedom = &host_domain($serverhomeID);
 1445:     my %defdomdefaults = &get_domain_defaults($serverhomedom);
 1446:     if (ref($defdomdefaults{'offloadnow'}) eq 'HASH') {
 1447:         if ($defdomdefaults{'offloadnow'}{$try_server}) {
 1448:             $canhost = 0;
 1449:         }
 1450:     }
 1451:     if ($canhost) {
 1452:         if (ref($defdomdefaults{'offloadoth'}) eq 'HASH') {
 1453:             if ($defdomdefaults{'offloadoth'}{$try_server}) {
 1454:                 unless (&shared_institution($udom,$try_server)) {
 1455:                     $canhost = 0;
 1456:                 }
 1457:             }
 1458:         }
 1459:     }
 1460:     if (($canhost) && ($uint_dom)) {
 1461:         my @intdoms;
 1462:         my $internet_names = &get_internet_names($try_server);
 1463:         if (ref($internet_names) eq 'ARRAY') {
 1464:             @intdoms = @{$internet_names};
 1465:         }
 1466:         unless (grep(/^\Q$uint_dom\E$/,@intdoms)) {
 1467:             my $remoterev = &get_server_loncaparev(undef,$try_server);
 1468:             $canhost = &can_host_session($udom,$try_server,$remoterev,
 1469:                                          $remotesessions,
 1470:                                          $defdomdefaults{'hostedsessions'});
 1471:         }
 1472:     }
 1473:     return $canhost;
 1474: }
 1475: 
 1476: sub this_host_spares {
 1477:     my ($dom) = @_;
 1478:     my ($dom_in_use,$lonhost_in_use,$result);
 1479:     my @hosts = &current_machine_ids();
 1480:     foreach my $lonhost (@hosts) {
 1481:         if (&host_domain($lonhost) eq $dom) {
 1482:             $dom_in_use = $dom;
 1483:             $lonhost_in_use = $lonhost;
 1484:             last;
 1485:         }
 1486:     }
 1487:     if ($dom_in_use ne '') {
 1488:         $result = &spares_for_offload($dom_in_use,$lonhost_in_use);
 1489:     }
 1490:     if (ref($result) ne 'HASH') {
 1491:         $lonhost_in_use = $perlvar{'lonHostID'};
 1492:         $dom_in_use = &host_domain($lonhost_in_use);
 1493:         $result = &spares_for_offload($dom_in_use,$lonhost_in_use);
 1494:         if (ref($result) ne 'HASH') {
 1495:             $result = \%spareid;
 1496:         }
 1497:     }
 1498:     return $result;
 1499: }
 1500: 
 1501: sub spares_for_offload  {
 1502:     my ($dom_in_use,$lonhost_in_use) = @_;
 1503:     my ($result,$cached)=&is_cached_new('spares',$dom_in_use);
 1504:     if (defined($cached)) {
 1505:         return $result;
 1506:     } else {
 1507:         my $cachetime = 60*60*24;
 1508:         my %domconfig =
 1509:             &Apache::lonnet::get_dom('configuration',['usersessions'],$dom_in_use);
 1510:         if (ref($domconfig{'usersessions'}) eq 'HASH') {
 1511:             if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
 1512:                 if (ref($domconfig{'usersessions'}{'spares'}{$lonhost_in_use}) eq 'HASH') {
 1513:                     return &do_cache_new('spares',$dom_in_use,$domconfig{'usersessions'}{'spares'}{$lonhost_in_use},$cachetime);
 1514:                 }
 1515:             }
 1516:         }
 1517:     }
 1518:     return;
 1519: }
 1520: 
 1521: sub get_lonbalancer_config {
 1522:     my ($servers) = @_;
 1523:     my ($currbalancer,$currtargets);
 1524:     if (ref($servers) eq 'HASH') {
 1525:         foreach my $server (keys(%{$servers})) {
 1526:             my %what = (
 1527:                          spareid => 1,
 1528:                          perlvar => 1,
 1529:                        );
 1530:             my ($result,$returnhash) = &get_remote_globals($server,\%what);
 1531:             if ($result eq 'ok') {
 1532:                 if (ref($returnhash) eq 'HASH') {
 1533:                     if (ref($returnhash->{'perlvar'}) eq 'HASH') {
 1534:                         if ($returnhash->{'perlvar'}->{'lonBalancer'} eq 'yes') {
 1535:                             $currbalancer = $server;
 1536:                             $currtargets = {};
 1537:                             if (ref($returnhash->{'spareid'}) eq 'HASH') {
 1538:                                 if (ref($returnhash->{'spareid'}->{'primary'}) eq 'ARRAY') {
 1539:                                     $currtargets->{'primary'} = $returnhash->{'spareid'}->{'primary'};
 1540:                                 }
 1541:                                 if (ref($returnhash->{'spareid'}->{'default'}) eq 'ARRAY') {
 1542:                                     $currtargets->{'default'} = $returnhash->{'spareid'}->{'default'};
 1543:                                 }
 1544:                             }
 1545:                             last;
 1546:                         }
 1547:                     }
 1548:                 }
 1549:             }
 1550:         }
 1551:     }
 1552:     return ($currbalancer,$currtargets);
 1553: }
 1554: 
 1555: sub check_loadbalancing {
 1556:     my ($uname,$udom,$caller) = @_;
 1557:     my ($is_balancer,$currtargets,$currrules,$dom_in_use,$homeintdom,
 1558:         $rule_in_effect,$offloadto,$otherserver,$setcookie,$dom_balancers);
 1559:     my $lonhost = $perlvar{'lonHostID'};
 1560:     my @hosts = &current_machine_ids();
 1561:     my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
 1562:     my $uintdom = &Apache::lonnet::internet_dom($uprimary_id);
 1563:     my $intdom = &Apache::lonnet::internet_dom($lonhost);
 1564:     my $serverhomedom = &host_domain($lonhost);
 1565:     my $domneedscache;
 1566:     my $cachetime = 60*60*24;
 1567: 
 1568:     if (($uintdom ne '') && ($uintdom eq $intdom)) {
 1569:         $dom_in_use = $udom;
 1570:         $homeintdom = 1;
 1571:     } else {
 1572:         $dom_in_use = $serverhomedom;
 1573:     }
 1574:     my ($result,$cached)=&is_cached_new('loadbalancing',$dom_in_use);
 1575:     unless (defined($cached)) {
 1576:         my %domconfig =
 1577:             &Apache::lonnet::get_dom('configuration',['loadbalancing'],$dom_in_use);
 1578:         if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 1579:             $result = &do_cache_new('loadbalancing',$dom_in_use,$domconfig{'loadbalancing'},$cachetime);
 1580:         } else {
 1581:             $domneedscache = $dom_in_use;
 1582:         }
 1583:     }
 1584:     if (ref($result) eq 'HASH') {
 1585:         ($is_balancer,$currtargets,$currrules,$setcookie,$dom_balancers) =
 1586:             &check_balancer_result($result,@hosts);
 1587:         if ($is_balancer) {
 1588:             if (ref($currrules) eq 'HASH') {
 1589:                 if ($homeintdom) {
 1590:                     if ($uname ne '') {
 1591:                         if (($currrules->{'_LC_adv'} ne '') || ($currrules->{'_LC_author'} ne '')) {
 1592:                             my ($is_adv,$is_author) = &is_advanced_user($udom,$uname);
 1593:                             if (($currrules->{'_LC_author'} ne '') && ($is_author)) {
 1594:                                 $rule_in_effect = $currrules->{'_LC_author'};
 1595:                             } elsif (($currrules->{'_LC_adv'} ne '') && ($is_adv)) {
 1596:                                 $rule_in_effect = $currrules->{'_LC_adv'}
 1597:                             }
 1598:                         }
 1599:                         if ($rule_in_effect eq '') {
 1600:                             my %userenv = &userenvironment($udom,$uname,'inststatus');
 1601:                             if ($userenv{'inststatus'} ne '') {
 1602:                                 my @statuses = map { &unescape($_); } split(/:/,$userenv{'inststatus'});
 1603:                                 my ($othertitle,$usertypes,$types) =
 1604:                                     &Apache::loncommon::sorted_inst_types($udom);
 1605:                                 if (ref($types) eq 'ARRAY') {
 1606:                                     foreach my $type (@{$types}) {
 1607:                                         if (grep(/^\Q$type\E$/,@statuses)) {
 1608:                                             if (exists($currrules->{$type})) {
 1609:                                                 $rule_in_effect = $currrules->{$type};
 1610:                                             }
 1611:                                         }
 1612:                                     }
 1613:                                 }
 1614:                             } else {
 1615:                                 if (exists($currrules->{'default'})) {
 1616:                                     $rule_in_effect = $currrules->{'default'};
 1617:                                 }
 1618:                             }
 1619:                         }
 1620:                     } else {
 1621:                         if (exists($currrules->{'default'})) {
 1622:                             $rule_in_effect = $currrules->{'default'};
 1623:                         }
 1624:                     }
 1625:                 } else {
 1626:                     if ($currrules->{'_LC_external'} ne '') {
 1627:                         $rule_in_effect = $currrules->{'_LC_external'};
 1628:                     }
 1629:                 }
 1630:                 $offloadto = &get_loadbalancer_targets($rule_in_effect,$currtargets,
 1631:                                                        $uname,$udom);
 1632:             }
 1633:         }
 1634:     } elsif (($homeintdom) && ($udom ne $serverhomedom)) {
 1635:         ($result,$cached)=&is_cached_new('loadbalancing',$serverhomedom);
 1636:         unless (defined($cached)) {
 1637:             my %domconfig =
 1638:                 &Apache::lonnet::get_dom('configuration',['loadbalancing'],$serverhomedom);
 1639:             if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 1640:                 $result = &do_cache_new('loadbalancing',$serverhomedom,$domconfig{'loadbalancing'},$cachetime);
 1641:             } else {
 1642:                 $domneedscache = $serverhomedom;
 1643:             }
 1644:         }
 1645:         if (ref($result) eq 'HASH') {
 1646:             ($is_balancer,$currtargets,$currrules,$setcookie,$dom_balancers) =
 1647:                 &check_balancer_result($result,@hosts);
 1648:             if ($is_balancer) {
 1649:                 if (ref($currrules) eq 'HASH') {
 1650:                     if ($currrules->{'_LC_internetdom'} ne '') {
 1651:                         $rule_in_effect = $currrules->{'_LC_internetdom'};
 1652:                     }
 1653:                 }
 1654:                 $offloadto = &get_loadbalancer_targets($rule_in_effect,$currtargets,
 1655:                                                        $uname,$udom);
 1656:             }
 1657:         } else {
 1658:             if ($perlvar{'lonBalancer'} eq 'yes') {
 1659:                 $is_balancer = 1;
 1660:                 $offloadto = &this_host_spares($dom_in_use);
 1661:             }
 1662:             unless (defined($cached)) {
 1663:                 $domneedscache = $serverhomedom;
 1664:             }
 1665:         }
 1666:     } else {
 1667:         if ($perlvar{'lonBalancer'} eq 'yes') {
 1668:             $is_balancer = 1;
 1669:             $offloadto = &this_host_spares($dom_in_use);
 1670:         }
 1671:         unless (defined($cached)) {
 1672:             $domneedscache = $serverhomedom;
 1673:         }
 1674:     }
 1675:     if ($domneedscache) {
 1676:         &do_cache_new('loadbalancing',$domneedscache,$is_balancer,$cachetime);
 1677:     }
 1678:     if (($is_balancer) && ($caller ne 'switchserver')) {
 1679:         my $lowest_load = 30000;
 1680:         if (ref($offloadto) eq 'HASH') {
 1681:             if (ref($offloadto->{'primary'}) eq 'ARRAY') {
 1682:                 foreach my $try_server (@{$offloadto->{'primary'}}) {
 1683:                     ($otherserver,$lowest_load) =
 1684:                         &compare_server_load($try_server,$otherserver,$lowest_load);
 1685:                 }
 1686:             }
 1687:             my $found_server = ($otherserver ne '' && $lowest_load < 100);
 1688: 
 1689:             if (!$found_server) {
 1690:                 if (ref($offloadto->{'default'}) eq 'ARRAY') {
 1691:                     foreach my $try_server (@{$offloadto->{'default'}}) {
 1692:                         ($otherserver,$lowest_load) =
 1693:                             &compare_server_load($try_server,$otherserver,$lowest_load);
 1694:                     }
 1695:                 }
 1696:             }
 1697:         } elsif (ref($offloadto) eq 'ARRAY') {
 1698:             if (@{$offloadto} == 1) {
 1699:                 $otherserver = $offloadto->[0];
 1700:             } elsif (@{$offloadto} > 1) {
 1701:                 foreach my $try_server (@{$offloadto}) {
 1702:                     ($otherserver,$lowest_load) =
 1703:                         &compare_server_load($try_server,$otherserver,$lowest_load);
 1704:                 }
 1705:             }
 1706:         }
 1707:         unless ($caller eq 'login') {
 1708:             if (($otherserver ne '') && (grep(/^\Q$otherserver\E$/,@hosts))) {
 1709:                 $is_balancer = 0;
 1710:                 if ($uname ne '' && $udom ne '') {
 1711:                     if (($env{'user.name'} eq $uname) && ($env{'user.domain'} eq $udom)) {
 1712:                         &appenv({'user.loadbalexempt'     => $lonhost,
 1713:                                  'user.loadbalcheck.time' => time});
 1714:                     }
 1715:                 }
 1716:             }
 1717:         }
 1718:     }
 1719:     if (($is_balancer) && (!$homeintdom)) {
 1720:         undef($setcookie);
 1721:     }
 1722:     return ($is_balancer,$otherserver,$setcookie,$offloadto,$dom_balancers);
 1723: }
 1724: 
 1725: sub check_balancer_result {
 1726:     my ($result,@hosts) = @_;
 1727:     my ($is_balancer,$currtargets,$currrules,$setcookie,$dom_balancers);
 1728:     if (ref($result) eq 'HASH') {
 1729:         if ($result->{'lonhost'} ne '') {
 1730:             my $currbalancer = $result->{'lonhost'};
 1731:             if (grep(/^\Q$currbalancer\E$/,@hosts)) {
 1732:                 $is_balancer = 1;
 1733:                 $currtargets = $result->{'targets'};
 1734:                 $currrules = $result->{'rules'};
 1735:             }
 1736:             $dom_balancers = $currbalancer;
 1737:         } else {
 1738:             if (keys(%{$result})) {
 1739:                 foreach my $key (keys(%{$result})) {
 1740:                     if (($key ne '') && (grep(/^\Q$key\E$/,@hosts)) &&
 1741:                         (ref($result->{$key}) eq 'HASH')) {
 1742:                         $is_balancer = 1;
 1743:                         $currrules = $result->{$key}{'rules'};
 1744:                         $currtargets = $result->{$key}{'targets'};
 1745:                         $setcookie = $result->{$key}{'cookie'};
 1746:                         last;
 1747:                     }
 1748:                 }
 1749:                 $dom_balancers = join(',',sort(keys(%{$result})));
 1750:             }
 1751:         }
 1752:     }
 1753:     return ($is_balancer,$currtargets,$currrules,$setcookie,$dom_balancers);
 1754: }
 1755: 
 1756: sub get_loadbalancer_targets {
 1757:     my ($rule_in_effect,$currtargets,$uname,$udom) = @_;
 1758:     my $offloadto;
 1759:     if ($rule_in_effect eq 'none') {
 1760:         return [$perlvar{'lonHostID'}];
 1761:     } elsif ($rule_in_effect eq '') {
 1762:         $offloadto = $currtargets;
 1763:     } else {
 1764:         if ($rule_in_effect eq 'homeserver') {
 1765:             my $homeserver = &homeserver($uname,$udom);
 1766:             if ($homeserver ne 'no_host') {
 1767:                 $offloadto = [$homeserver];
 1768:             }
 1769:         } elsif ($rule_in_effect eq 'externalbalancer') {
 1770:             my %domconfig =
 1771:                 &Apache::lonnet::get_dom('configuration',['loadbalancing'],$udom);
 1772:             if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 1773:                 if ($domconfig{'loadbalancing'}{'lonhost'} ne '') {
 1774:                     if (&hostname($domconfig{'loadbalancing'}{'lonhost'}) ne '') {
 1775:                         $offloadto = [$domconfig{'loadbalancing'}{'lonhost'}];
 1776:                     }
 1777:                 }
 1778:             } else {
 1779:                 my %servers = &internet_dom_servers($udom);
 1780:                 my ($remotebalancer,$remotetargets) = &get_lonbalancer_config(\%servers);
 1781:                 if (&hostname($remotebalancer) ne '') {
 1782:                     $offloadto = [$remotebalancer];
 1783:                 }
 1784:             }
 1785:         } elsif (&hostname($rule_in_effect) ne '') {
 1786:             $offloadto = [$rule_in_effect];
 1787:         }
 1788:     }
 1789:     return $offloadto;
 1790: }
 1791: 
 1792: sub internet_dom_servers {
 1793:     my ($dom) = @_;
 1794:     my (%uniqservers,%servers);
 1795:     my $primaryserver = &hostname(&domain($dom,'primary'));
 1796:     my @machinedoms = &machine_domains($primaryserver);
 1797:     foreach my $mdom (@machinedoms) {
 1798:         my %currservers = %servers;
 1799:         my %server = &get_servers($mdom);
 1800:         %servers = (%currservers,%server);
 1801:     }
 1802:     my %by_hostname;
 1803:     foreach my $id (keys(%servers)) {
 1804:         push(@{$by_hostname{$servers{$id}}},$id);
 1805:     }
 1806:     foreach my $hostname (sort(keys(%by_hostname))) {
 1807:         if (@{$by_hostname{$hostname}} > 1) {
 1808:             my $match = 0;
 1809:             foreach my $id (@{$by_hostname{$hostname}}) {
 1810:                 if (&host_domain($id) eq $dom) {
 1811:                     $uniqservers{$id} = $hostname;
 1812:                     $match = 1;
 1813:                 }
 1814:             }
 1815:             unless ($match) {
 1816:                 $uniqservers{$by_hostname{$hostname}[0]} = $hostname;
 1817:             }
 1818:         } else {
 1819:             $uniqservers{$by_hostname{$hostname}[0]} = $hostname;
 1820:         }
 1821:     }
 1822:     return %uniqservers;
 1823: }
 1824: 
 1825: sub trusted_domains {
 1826:     my ($cmdtype,$calldom) = @_;
 1827:     my ($trusted,$untrusted);
 1828:     if (&domain($calldom) eq '') {
 1829:         return ($trusted,$untrusted);
 1830:     }
 1831:     unless ($cmdtype =~ /^(content|shared|enroll|coaurem|othcoau|domroles|catalog|reqcrs|msg)$/) {
 1832:         return ($trusted,$untrusted);
 1833:     }
 1834:     my $callprimary = &domain($calldom,'primary');
 1835:     my $intcalldom = &Apache::lonnet::internet_dom($callprimary);
 1836:     if ($intcalldom eq '') {
 1837:         return ($trusted,$untrusted);
 1838:     }
 1839: 
 1840:     my ($trustconfig,$cached)=&Apache::lonnet::is_cached_new('trust',$calldom);
 1841:     unless (defined($cached)) {
 1842:         my %domconfig = &Apache::lonnet::get_dom('configuration',['trust'],$calldom);
 1843:         &Apache::lonnet::do_cache_new('trust',$calldom,$domconfig{'trust'},3600);
 1844:         $trustconfig = $domconfig{'trust'};
 1845:     }
 1846:     if (ref($trustconfig)) {
 1847:         my (%possexc,%possinc,@allexc,@allinc); 
 1848:         if (ref($trustconfig->{$cmdtype}) eq 'HASH') {
 1849:             if (ref($trustconfig->{$cmdtype}->{'exc'}) eq 'ARRAY') {
 1850:                 map { $possexc{$_} = 1; } @{$trustconfig->{$cmdtype}->{'exc'}}; 
 1851:             }
 1852:             if (ref($trustconfig->{$cmdtype}->{'inc'}) eq 'ARRAY') {
 1853:                 $possinc{$intcalldom} = 1;
 1854:                 map { $possinc{$_} = 1; } @{$trustconfig->{$cmdtype}->{'inc'}};
 1855:             }
 1856:         }
 1857:         if (keys(%possexc)) {
 1858:             if (keys(%possinc)) {
 1859:                 foreach my $key (sort(keys(%possexc))) {
 1860:                     next if ($key eq $intcalldom);
 1861:                     unless ($possinc{$key}) {
 1862:                         push(@allexc,$key);
 1863:                     }
 1864:                 }
 1865:             } else {
 1866:                 @allexc = sort(keys(%possexc));
 1867:             }
 1868:         }
 1869:         if (keys(%possinc)) {
 1870:             $possinc{$intcalldom} = 1;
 1871:             @allinc = sort(keys(%possinc));
 1872:         }
 1873:         if ((@allexc > 0) || (@allinc > 0)) {
 1874:             my %doms_by_intdom;
 1875:             my %allintdoms = &all_host_intdom();
 1876:             my %alldoms = &all_host_domain();
 1877:             foreach my $key (%allintdoms) {
 1878:                 if (ref($doms_by_intdom{$allintdoms{$key}}) eq 'ARRAY') {
 1879:                     unless (grep(/^\Q$alldoms{$key}\E$/,@{$doms_by_intdom{$allintdoms{$key}}})) {
 1880:                         push(@{$doms_by_intdom{$allintdoms{$key}}},$alldoms{$key});
 1881:                     }
 1882:                 } else {
 1883:                     $doms_by_intdom{$allintdoms{$key}} = [$alldoms{$key}]; 
 1884:                 }
 1885:             }
 1886:             foreach my $exc (@allexc) {
 1887:                 if (ref($doms_by_intdom{$exc}) eq 'ARRAY') {
 1888:                     push(@{$untrusted},@{$doms_by_intdom{$exc}});
 1889:                 }
 1890:             }
 1891:             foreach my $inc (@allinc) {
 1892:                 if (ref($doms_by_intdom{$inc}) eq 'ARRAY') {
 1893:                     push(@{$trusted},@{$doms_by_intdom{$inc}});
 1894:                 }
 1895:             }
 1896:         }
 1897:     }
 1898:     return ($trusted,$untrusted);
 1899: }
 1900: 
 1901: sub will_trust {
 1902:     my ($cmdtype,$domain,$possdom) = @_;
 1903:     return 1 if ($domain eq $possdom);
 1904:     my ($trustedref,$untrustedref) = &trusted_domains($cmdtype,$possdom);
 1905:     my $willtrust; 
 1906:     if ((ref($trustedref) eq 'ARRAY') && (@{$trustedref} > 0)) {
 1907:         if (grep(/^\Q$domain\E$/,@{$trustedref})) {
 1908:             $willtrust = 1;
 1909:         }
 1910:     } elsif ((ref($untrustedref) eq 'ARRAY') && (@{$untrustedref} > 0)) {
 1911:         unless (grep(/^\Q$domain\E$/,@{$untrustedref})) {
 1912:             $willtrust = 1;
 1913:         }
 1914:     } else {
 1915:         $willtrust = 1;
 1916:     }
 1917:     return $willtrust;
 1918: }
 1919: 
 1920: # ---------------------- Find the homebase for a user from domain's lib servers
 1921: 
 1922: my %homecache;
 1923: sub homeserver {
 1924:     my ($uname,$udom,$ignoreBadCache)=@_;
 1925:     my $index="$uname:$udom";
 1926: 
 1927:     if (exists($homecache{$index})) { return $homecache{$index}; }
 1928: 
 1929:     my %servers = &get_servers($udom,'library');
 1930:     foreach my $tryserver (keys(%servers)) {
 1931:         next if ($ignoreBadCache ne 'true' && 
 1932: 		 exists($badServerCache{$tryserver}));
 1933: 
 1934: 	my $answer=reply("home:$udom:$uname",$tryserver);
 1935: 	if ($answer eq 'found') {
 1936: 	    delete($badServerCache{$tryserver}); 
 1937: 	    return $homecache{$index}=$tryserver;
 1938: 	} elsif ($answer eq 'no_host') {
 1939: 	    $badServerCache{$tryserver}=1;
 1940: 	}
 1941:     }    
 1942:     return 'no_host';
 1943: }
 1944: 
 1945: # ----- Find the usernames behind a list of student/employee IDs or clicker IDs
 1946: 
 1947: sub idget {
 1948:     my ($udom,$idsref,$namespace)=@_;
 1949:     my %returnhash=();
 1950:     my @ids=(); 
 1951:     if (ref($idsref) eq 'ARRAY') {
 1952:         @ids = @{$idsref};
 1953:     } else {
 1954:         return %returnhash; 
 1955:     }
 1956:     if ($namespace eq '') {
 1957:         $namespace = 'ids';
 1958:     }
 1959:     
 1960:     my %servers = &get_servers($udom,'library');
 1961:     foreach my $tryserver (keys(%servers)) {
 1962: 	my $idlist=join('&', map { &escape($_); } @ids);
 1963: 	if ($namespace eq 'ids') {
 1964: 	    $idlist=~tr/A-Z/a-z/;
 1965: 	}
 1966: 	my $reply;
 1967: 	if ($namespace eq 'ids') {
 1968: 	    $reply=&reply("idget:$udom:".$idlist,$tryserver);
 1969: 	} else {
 1970: 	    $reply=&reply("getdom:$udom:$namespace:$idlist",$tryserver);
 1971: 	}
 1972: 	my @answer=();
 1973: 	if (($reply ne 'con_lost') && ($reply!~/^error\:/)) {
 1974: 	    @answer=split(/\&/,$reply);
 1975: 	}                    ;
 1976: 	my $i;
 1977: 	for ($i=0;$i<=$#ids;$i++) {
 1978: 	    if ($answer[$i]) {
 1979: 		$returnhash{$ids[$i]}=&unescape($answer[$i]);
 1980: 	    }
 1981: 	}
 1982:     }
 1983:     return %returnhash;
 1984: }
 1985: 
 1986: # ------------------------------------- Find the IDs behind a list of usernames
 1987: 
 1988: sub idrget {
 1989:     my ($udom,@unames)=@_;
 1990:     my %returnhash=();
 1991:     foreach my $uname (@unames) {
 1992:         $returnhash{$uname}=(&userenvironment($udom,$uname,'id'))[1];
 1993:     }
 1994:     return %returnhash;
 1995: }
 1996: 
 1997: # Store away a list of names and associated student/employee IDs or clicker IDs
 1998: 
 1999: sub idput {
 2000:     my ($udom,$idsref,$uhom,$namespace)=@_;
 2001:     my %servers=();
 2002:     my %ids=();
 2003:     my %byid = ();
 2004:     if (ref($idsref) eq 'HASH') {
 2005:         %ids=%{$idsref};
 2006:     }
 2007:     if ($namespace eq '') {
 2008:         $namespace = 'ids'; 
 2009:     }
 2010:     foreach my $uname (keys(%ids)) {
 2011: 	&cput('environment',{'id'=>$ids{$uname}},$udom,$uname);
 2012:         if ($uhom eq '') {
 2013:             $uhom=&homeserver($uname,$udom);
 2014:         }
 2015:         if ($uhom ne 'no_host') {
 2016:             my $esc_unam=&escape($uname);
 2017:             if ($namespace eq 'ids') {
 2018:                 my $id=&escape($ids{$uname});
 2019:                 $id=~tr/A-Z/a-z/;
 2020:                 my $esc_unam=&escape($uname);
 2021:                 $servers{$uhom}.=$id.'='.$esc_unam.'&';
 2022:             } else {
 2023:                 my @currids = split(/,/,$ids{$uname});
 2024:                 foreach my $id (@currids) {
 2025:                     $byid{$uhom}{$id} .= $uname.',';
 2026:                 }
 2027:             }
 2028:         }
 2029:     }
 2030:     if ($namespace eq 'clickers') {
 2031:         foreach my $server (keys(%byid)) {
 2032:             if (ref($byid{$server}) eq 'HASH') {
 2033:                 foreach my $id (keys(%{$byid{$server}})) {
 2034:                     $byid{$server} =~ s/,$//;
 2035:                     $servers{$uhom}.=&escape($id).'='.&escape($byid{$server}).'&'; 
 2036:                 }
 2037:             }
 2038:         }
 2039:     }
 2040:     foreach my $server (keys(%servers)) {
 2041:         $servers{$server} =~ s/\&$//;
 2042:         if ($namespace eq 'ids') {     
 2043:             &critical('idput:'.$udom.':'.$servers{$server},$server);
 2044:         } else {
 2045:             &critical('updateclickers:'.$udom.':add:'.$servers{$server},$server);
 2046:         }
 2047:     }
 2048: }
 2049: 
 2050: # ------------- Delete unwanted student/employee IDs or clicker IDs from domain
 2051: 
 2052: sub iddel {
 2053:     my ($udom,$idshashref,$uhome,$namespace)=@_;
 2054:     my %result=();
 2055:     my %ids=();
 2056:     my %byid = ();
 2057:     if (ref($idshashref) eq 'HASH') {
 2058:         %ids=%{$idshashref};
 2059:     } else {
 2060:         return %result;
 2061:     }
 2062:     if ($namespace eq '') {
 2063:         $namespace = 'ids';
 2064:     }
 2065:     my %servers=();
 2066:     while (my ($id,$unamestr) = each(%ids)) {
 2067:         if ($namespace eq 'ids') {
 2068:             my $uhom = $uhome;
 2069:             if ($uhom eq '') { 
 2070:                 $uhom=&homeserver($unamestr,$udom);
 2071:             }
 2072:             if ($uhom ne 'no_host') {
 2073:                 $servers{$uhom}.='&'.&escape($id);
 2074:             }
 2075:          } else {
 2076:             my @curritems = split(/,/,$ids{$id});
 2077:             foreach my $uname (@curritems) {
 2078:                 my $uhom = $uhome;
 2079:                 if ($uhom eq '') {
 2080:                     $uhom=&homeserver($uname,$udom);
 2081:                 }
 2082:                 if ($uhom ne 'no_host') { 
 2083:                     $byid{$uhom}{$id} .= $uname.',';
 2084:                 }
 2085:             }
 2086:         }
 2087:     }
 2088:     if ($namespace eq 'clickers') {
 2089:         foreach my $server (keys(%byid)) {
 2090:             if (ref($byid{$server}) eq 'HASH') {
 2091:                 foreach my $id (keys(%{$byid{$server}})) {
 2092:                     $byid{$server}{$id} =~ s/,$//;
 2093:                     $servers{$server}.=&escape($id).'='.&escape($byid{$server}{$id}).'&';
 2094:                 }
 2095:             }
 2096:         }
 2097:     }
 2098:     foreach my $server (keys(%servers)) {
 2099:         $servers{$server} =~ s/\&$//;
 2100:         if ($namespace eq 'ids') {
 2101:             $result{$server} = &critical('iddel:'.$udom.':'.$servers{$server},$uhome);
 2102:         } elsif ($namespace eq 'clickers') {
 2103:             $result{$server} = &critical('updateclickers:'.$udom.':del:'.$servers{$server},$server);
 2104:         }
 2105:     }
 2106:     return %result;
 2107: }
 2108: 
 2109: # ----- Update clicker ID-to-username look-ups in clickers.db on library server 
 2110: 
 2111: sub updateclickers {
 2112:     my ($udom,$action,$idshashref,$uhome,$critical) = @_;
 2113:     my %clickers;
 2114:     if (ref($idshashref) eq 'HASH') {
 2115:         %clickers=%{$idshashref};
 2116:     } else {
 2117:         return;
 2118:     }
 2119:     my $items='';
 2120:     foreach my $item (keys(%clickers)) {
 2121:         $items.=&escape($item).'='.&escape($clickers{$item}).'&';
 2122:     }
 2123:     $items=~s/\&$//;
 2124:     my $request = "updateclickers:$udom:$action:$items";
 2125:     if ($critical) {
 2126:         return &critical($request,$uhome);
 2127:     } else {
 2128:         return &reply($request,$uhome);
 2129:     }
 2130: }
 2131: 
 2132: # ------------------------------dump from db file owned by domainconfig user
 2133: sub dump_dom {
 2134:     my ($namespace, $udom, $regexp) = @_;
 2135: 
 2136:     $udom ||= $env{'user.domain'};
 2137: 
 2138:     return () unless $udom;
 2139: 
 2140:     return &dump($namespace, $udom, &get_domainconfiguser($udom), $regexp);
 2141: }
 2142: 
 2143: # ------------------------------------------ get items from domain db files   
 2144: 
 2145: sub get_dom {
 2146:     my ($namespace,$storearr,$udom,$uhome)=@_;
 2147:     return if ($udom eq 'public');
 2148:     my $items='';
 2149:     foreach my $item (@$storearr) {
 2150:         $items.=&escape($item).'&';
 2151:     }
 2152:     $items=~s/\&$//;
 2153:     if (!$udom) {
 2154:         $udom=$env{'user.domain'};
 2155:         return if ($udom eq 'public');
 2156:         if (defined(&domain($udom,'primary'))) {
 2157:             $uhome=&domain($udom,'primary');
 2158:         } else {
 2159:             undef($uhome);
 2160:         }
 2161:     } else {
 2162:         if (!$uhome) {
 2163:             if (defined(&domain($udom,'primary'))) {
 2164:                 $uhome=&domain($udom,'primary');
 2165:             }
 2166:         }
 2167:     }
 2168:     if ($udom && $uhome && ($uhome ne 'no_host')) {
 2169:         my $rep;
 2170:         if (grep { $_ eq $uhome } &current_machine_ids()) {
 2171:             # domain information is hosted on this machine
 2172:             my $cmd = 'getdom';
 2173:             if ($namespace =~ /^enc/) {
 2174:                 $cmd = 'egetdom';
 2175:             }
 2176:             $rep = &LONCAPA::Lond::get_dom("$cmd:$udom:$namespace:$items");
 2177:         } else {
 2178:             if ($namespace =~ /^enc/) {
 2179:                 $rep=&reply("encrypt:egetdom:$udom:$namespace:$items",$uhome);
 2180:             } else {
 2181:                 $rep=&reply("getdom:$udom:$namespace:$items",$uhome);
 2182:             }
 2183:         }
 2184:         my %returnhash;
 2185:         if ($rep eq '' || $rep =~ /^error: 2 /) {
 2186:             return %returnhash;
 2187:         }
 2188:         my @pairs=split(/\&/,$rep);
 2189:         if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
 2190:             return @pairs;
 2191:         }
 2192:         my $i=0;
 2193:         foreach my $item (@$storearr) {
 2194:             $returnhash{$item}=&thaw_unescape($pairs[$i]);
 2195:             $i++;
 2196:         }
 2197:         return %returnhash;
 2198:     } else {
 2199:         &logthis("get_dom failed - no homeserver and/or domain ($udom) ($uhome)");
 2200:     }
 2201: }
 2202: 
 2203: # -------------------------------------------- put items in domain db files 
 2204: 
 2205: sub put_dom {
 2206:     my ($namespace,$storehash,$udom,$uhome)=@_;
 2207:     if (!$udom) {
 2208:         $udom=$env{'user.domain'};
 2209:         if (defined(&domain($udom,'primary'))) {
 2210:             $uhome=&domain($udom,'primary');
 2211:         } else {
 2212:             undef($uhome);
 2213:         }
 2214:     } else {
 2215:         if (!$uhome) {
 2216:             if (defined(&domain($udom,'primary'))) {
 2217:                 $uhome=&domain($udom,'primary');
 2218:             }
 2219:         }
 2220:     } 
 2221:     if ($udom && $uhome && ($uhome ne 'no_host')) {
 2222:         my $items='';
 2223:         foreach my $item (keys(%$storehash)) {
 2224:             $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
 2225:         }
 2226:         $items=~s/\&$//;
 2227:         if ($namespace =~ /^enc/) {
 2228:             return &reply("encrypt:putdom:$udom:$namespace:$items",$uhome);
 2229:         } else {
 2230:             return &reply("putdom:$udom:$namespace:$items",$uhome);
 2231:         }
 2232:     } else {
 2233:         &logthis("put_dom failed - no homeserver and/or domain");
 2234:     }
 2235: }
 2236: 
 2237: # --------------------- newput for items in db file owned by domainconfig user
 2238: sub newput_dom {
 2239:     my ($namespace,$storehash,$udom) = @_;
 2240:     my $result;
 2241:     if (!$udom) {
 2242:         $udom=$env{'user.domain'};
 2243:     }
 2244:     if ($udom) {
 2245:         my $uname = &get_domainconfiguser($udom);
 2246:         $result = &newput($namespace,$storehash,$udom,$uname);
 2247:     }
 2248:     return $result;
 2249: }
 2250: 
 2251: # --------------------- delete for items in db file owned by domainconfig user
 2252: sub del_dom {
 2253:     my ($namespace,$storearr,$udom)=@_;
 2254:     if (ref($storearr) eq 'ARRAY') {
 2255:         if (!$udom) {
 2256:             $udom=$env{'user.domain'};
 2257:         }
 2258:         if ($udom) {
 2259:             my $uname = &get_domainconfiguser($udom); 
 2260:             return &del($namespace,$storearr,$udom,$uname);
 2261:         }
 2262:     }
 2263: }
 2264: 
 2265: # ----------------------------------construct domainconfig user for a domain 
 2266: sub get_domainconfiguser {
 2267:     my ($udom) = @_;
 2268:     return $udom.'-domainconfig';
 2269: }
 2270: 
 2271: sub retrieve_inst_usertypes {
 2272:     my ($udom) = @_;
 2273:     my (%returnhash,@order);
 2274:     my %domdefs = &Apache::lonnet::get_domain_defaults($udom);
 2275:     if ((ref($domdefs{'inststatustypes'}) eq 'HASH') && 
 2276:         (ref($domdefs{'inststatusorder'}) eq 'ARRAY')) {
 2277:         return ($domdefs{'inststatustypes'},$domdefs{'inststatusorder'});
 2278:     } else {
 2279:         if (defined(&domain($udom,'primary'))) {
 2280:             my $uhome=&domain($udom,'primary');
 2281:             my $rep=&reply("inst_usertypes:$udom",$uhome);
 2282:             if ($rep =~ /^(con_lost|error|no_such_host|refused)/) {
 2283:                 &logthis("retrieve_inst_usertypes failed - $rep returned from $uhome in domain: $udom");
 2284:                 return (\%returnhash,\@order);
 2285:             }
 2286:             my ($hashitems,$orderitems) = split(/:/,$rep); 
 2287:             my @pairs=split(/\&/,$hashitems);
 2288:             foreach my $item (@pairs) {
 2289:                 my ($key,$value)=split(/=/,$item,2);
 2290:                 $key = &unescape($key);
 2291:                 next if ($key =~ /^error: 2 /);
 2292:                 $returnhash{$key}=&thaw_unescape($value);
 2293:             }
 2294:             my @esc_order = split(/\&/,$orderitems);
 2295:             foreach my $item (@esc_order) {
 2296:                 push(@order,&unescape($item));
 2297:             }
 2298:         } else {
 2299:             &logthis("retrieve_inst_usertypes failed - no primary domain server for $udom");
 2300:         }
 2301:         return (\%returnhash,\@order);
 2302:     }
 2303: }
 2304: 
 2305: sub is_domainimage {
 2306:     my ($url) = @_;
 2307:     if ($url=~m-^/+res/+($match_domain)/+\1\-domainconfig/+(img|logo|domlogo)/+[^/]-) {
 2308:         if (&domain($1) ne '') {
 2309:             return '1';
 2310:         }
 2311:     }
 2312:     return;
 2313: }
 2314: 
 2315: sub inst_directory_query {
 2316:     my ($srch) = @_;
 2317:     my $udom = $srch->{'srchdomain'};
 2318:     my %results;
 2319:     my $homeserver = &domain($udom,'primary');
 2320:     my $outcome;
 2321:     if ($homeserver ne '') {
 2322:         unless ($homeserver eq $perlvar{'lonHostID'}) {
 2323:             if ($srch->{'srchby'} eq 'email') {
 2324:                 my $lcrev = &get_server_loncaparev($udom,$homeserver);
 2325:                 my ($major,$minor) = ($lcrev =~ /^\'?(\d+)\.(\d+)\.[\w.\-]+\'?$/);
 2326:                 if (($major eq '' && $minor eq '') || ($major < 2) ||
 2327:                     (($major == 2) && ($minor < 12))) {
 2328:                     return;
 2329:                 }
 2330:             }
 2331:         }
 2332: 	my $queryid=&reply("querysend:instdirsearch:".
 2333: 			   &escape($srch->{'srchby'}).':'.
 2334: 			   &escape($srch->{'srchterm'}).':'.
 2335: 			   &escape($srch->{'srchtype'}),$homeserver);
 2336: 	my $host=&hostname($homeserver);
 2337: 	if ($queryid !~/^\Q$host\E\_/) {
 2338: 	    &logthis('institutional directory search invalid queryid: '.$queryid.' for host: '.$homeserver.' in domain '.$udom);
 2339: 	    return;
 2340: 	}
 2341: 	my $response = &get_query_reply($queryid);
 2342: 	my $maxtries = 5;
 2343: 	my $tries = 1;
 2344: 	while (($response=~/^timeout/) && ($tries < $maxtries)) {
 2345: 	    $response = &get_query_reply($queryid);
 2346: 	    $tries ++;
 2347: 	}
 2348: 
 2349:         if (!&error($response) && $response ne 'refused') {
 2350:             if ($response eq 'unavailable') {
 2351:                 $outcome = $response;
 2352:             } else {
 2353:                 $outcome = 'ok';
 2354:                 my @matches = split(/\n/,$response);
 2355:                 foreach my $match (@matches) {
 2356:                     my ($key,$value) = split(/=/,$match);
 2357:                     $results{&unescape($key).':'.$udom} = &thaw_unescape($value);
 2358:                 }
 2359:             }
 2360:         }
 2361:     }
 2362:     return ($outcome,%results);
 2363: }
 2364: 
 2365: sub usersearch {
 2366:     my ($srch) = @_;
 2367:     my $dom = $srch->{'srchdomain'};
 2368:     my %results;
 2369:     my %libserv = &all_library();
 2370:     my $query = 'usersearch';
 2371:     foreach my $tryserver (keys(%libserv)) {
 2372:         if (&host_domain($tryserver) eq $dom) {
 2373:             unless ($tryserver eq $perlvar{'lonHostID'}) {
 2374:                 if ($srch->{'srchby'} eq 'email') {
 2375:                     my $lcrev = &get_server_loncaparev($dom,$tryserver);
 2376:                     my ($major,$minor) = ($lcrev =~ /^\'?(\d+)\.(\d+)\.[\w.\-]+\'?$/);
 2377:                     next if (($major eq '' && $minor eq '') || ($major < 2) ||
 2378:                              (($major == 2) && ($minor < 12)));
 2379:                 }
 2380:             }
 2381:             my $host=&hostname($tryserver);
 2382:             my $queryid=
 2383:                 &reply("querysend:".&escape($query).':'.
 2384:                        &escape($srch->{'srchby'}).':'.
 2385:                        &escape($srch->{'srchtype'}).':'.
 2386:                        &escape($srch->{'srchterm'}),$tryserver);
 2387:             if ($queryid !~/^\Q$host\E\_/) {
 2388:                 &logthis('usersearch: invalid queryid: '.$queryid.' for host: '.$host.'in domain '.$dom.' and server: '.$tryserver);
 2389:                 next;
 2390:             }
 2391:             my $reply = &get_query_reply($queryid);
 2392:             my $maxtries = 1;
 2393:             my $tries = 1;
 2394:             while (($reply=~/^timeout/) && ($tries < $maxtries)) {
 2395:                 $reply = &get_query_reply($queryid);
 2396:                 $tries ++;
 2397:             }
 2398:             if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
 2399:                 &logthis('usersrch error: '.$reply.' for '.$dom.' - searching for : '.$srch->{'srchterm'}.' by '.$srch->{'srchby'}.' ('.$srch->{'srchtype'}.') -  maxtries: '.$maxtries.' tries: '.$tries);
 2400:             } else {
 2401:                 my @matches;
 2402:                 if ($reply =~ /\n/) {
 2403:                     @matches = split(/\n/,$reply);
 2404:                 } else {
 2405:                     @matches = split(/\&/,$reply);
 2406:                 }
 2407:                 foreach my $match (@matches) {
 2408:                     my ($uname,$udom,%userhash);
 2409:                     foreach my $entry (split(/:/,$match)) {
 2410:                         my ($key,$value) =
 2411:                             map {&unescape($_);} split(/=/,$entry);
 2412:                         $userhash{$key} = $value;
 2413:                         if ($key eq 'username') {
 2414:                             $uname = $value;
 2415:                         } elsif ($key eq 'domain') {
 2416:                             $udom = $value;
 2417:                         }
 2418:                     }
 2419:                     $results{$uname.':'.$udom} = \%userhash;
 2420:                 }
 2421:             }
 2422:         }
 2423:     }
 2424:     return %results;
 2425: }
 2426: 
 2427: sub get_instuser {
 2428:     my ($udom,$uname,$id) = @_;
 2429:     my $homeserver = &domain($udom,'primary');
 2430:     my ($outcome,%results);
 2431:     if ($homeserver ne '') {
 2432:         my $queryid=&reply("querysend:getinstuser:".&escape($uname).':'.
 2433:                            &escape($id).':'.&escape($udom),$homeserver);
 2434:         my $host=&hostname($homeserver);
 2435:         if ($queryid !~/^\Q$host\E\_/) {
 2436:             &logthis('get_instuser invalid queryid: '.$queryid.' for host: '.$homeserver.'in domain '.$udom);
 2437:             return;
 2438:         }
 2439:         my $response = &get_query_reply($queryid);
 2440:         my $maxtries = 5;
 2441:         my $tries = 1;
 2442:         while (($response=~/^timeout/) && ($tries < $maxtries)) {
 2443:             $response = &get_query_reply($queryid);
 2444:             $tries ++;
 2445:         }
 2446:         if (!&error($response) && $response ne 'refused') {
 2447:             if ($response eq 'unavailable') {
 2448:                 $outcome = $response;
 2449:             } else {
 2450:                 $outcome = 'ok';
 2451:                 my @matches = split(/\n/,$response);
 2452:                 foreach my $match (@matches) {
 2453:                     my ($key,$value) = split(/=/,$match);
 2454:                     $results{&unescape($key)} = &thaw_unescape($value);
 2455:                 }
 2456:             }
 2457:         }
 2458:     }
 2459:     my %userinfo;
 2460:     if (ref($results{$uname}) eq 'HASH') {
 2461:         %userinfo = %{$results{$uname}};
 2462:     } 
 2463:     return ($outcome,%userinfo);
 2464: }
 2465: 
 2466: sub get_multiple_instusers {
 2467:     my ($udom,$users,$caller) = @_;
 2468:     my ($outcome,$results);
 2469:     if (ref($users) eq 'HASH') {
 2470:         my $count = keys(%{$users}); 
 2471:         my $requested = &freeze_escape($users);
 2472:         my $homeserver = &domain($udom,'primary');
 2473:         if ($homeserver ne '') {
 2474:             my $queryid=&reply('querysend:getmultinstusers:::'.$caller.'='.$requested,$homeserver);
 2475:             my $host=&hostname($homeserver);
 2476:             if ($queryid !~/^\Q$host\E\_/) {
 2477:                 &logthis('get_multiple_instusers invalid queryid: '.$queryid.
 2478:                          ' for host: '.$homeserver.'in domain '.$udom);
 2479:                 return ($outcome,$results);
 2480:             }
 2481:             my $response = &get_query_reply($queryid);
 2482:             my $maxtries = 5;
 2483:             if ($count > 100) {
 2484:                 $maxtries = 1+int($count/20);
 2485:             }
 2486:             my $tries = 1;
 2487:             while (($response=~/^timeout/) && ($tries <= $maxtries)) {
 2488:                 $response = &get_query_reply($queryid);
 2489:                 $tries ++;
 2490:             }
 2491:             if ($response eq '') {
 2492:                 $results = {};
 2493:                 foreach my $key (keys(%{$users})) {
 2494:                     my ($uname,$id);
 2495:                     if ($caller eq 'id') {
 2496:                         $id = $key;
 2497:                     } else {
 2498:                         $uname = $key;
 2499:                     }
 2500:                     my ($resp,%info) = &get_instuser($udom,$uname,$id);
 2501:                     $outcome = $resp;
 2502:                     if ($resp eq 'ok') {
 2503:                         %{$results} = (%{$results}, %info);
 2504:                     } else {
 2505:                         last;
 2506:                     }
 2507:                 }
 2508:             } elsif(!&error($response) && ($response ne 'refused')) {
 2509:                 if (($response eq 'unavailable') || ($response eq 'invalid') || ($response eq 'timeout')) {
 2510:                     $outcome = $response;
 2511:                 } else {
 2512:                     ($outcome,my $userdata) = split(/=/,$response,2);
 2513:                     if ($outcome eq 'ok') {
 2514:                         $results = &thaw_unescape($userdata); 
 2515:                     }
 2516:                 }
 2517:             }
 2518:         }
 2519:     }
 2520:     return ($outcome,$results);
 2521: }
 2522: 
 2523: sub inst_rulecheck {
 2524:     my ($udom,$uname,$id,$item,$rules) = @_;
 2525:     my %returnhash;
 2526:     if ($udom ne '') {
 2527:         if (ref($rules) eq 'ARRAY') {
 2528:             @{$rules} = map {&escape($_);} (@{$rules});
 2529:             my $rulestr = join(':',@{$rules});
 2530:             my $homeserver=&domain($udom,'primary');
 2531:             if (($homeserver ne '') && ($homeserver ne 'no_host')) {
 2532:                 my $response;
 2533:                 if ($item eq 'username') {                
 2534:                     $response=&unescape(&reply('instrulecheck:'.&escape($udom).
 2535:                                               ':'.&escape($uname).':'.$rulestr,
 2536:                                               $homeserver));
 2537:                 } elsif ($item eq 'id') {
 2538:                     $response=&unescape(&reply('instidrulecheck:'.&escape($udom).
 2539:                                               ':'.&escape($id).':'.$rulestr,
 2540:                                               $homeserver));
 2541:                 } elsif ($item eq 'selfcreate') {
 2542:                     $response=&unescape(&reply('instselfcreatecheck:'.
 2543:                                                &escape($udom).':'.&escape($uname).
 2544:                                               ':'.$rulestr,$homeserver));
 2545:                 }
 2546:                 if ($response ne 'refused') {
 2547:                     my @pairs=split(/\&/,$response);
 2548:                     foreach my $item (@pairs) {
 2549:                         my ($key,$value)=split(/=/,$item,2);
 2550:                         $key = &unescape($key);
 2551:                         next if ($key =~ /^error: 2 /);
 2552:                         $returnhash{$key}=&thaw_unescape($value);
 2553:                     }
 2554:                 }
 2555:             }
 2556:         }
 2557:     }
 2558:     return %returnhash;
 2559: }
 2560: 
 2561: sub inst_userrules {
 2562:     my ($udom,$check) = @_;
 2563:     my (%ruleshash,@ruleorder);
 2564:     if ($udom ne '') {
 2565:         my $homeserver=&domain($udom,'primary');
 2566:         if (($homeserver ne '') && ($homeserver ne 'no_host')) {
 2567:             my $response;
 2568:             if ($check eq 'id') {
 2569:                 $response=&reply('instidrules:'.&escape($udom),
 2570:                                  $homeserver);
 2571:             } elsif ($check eq 'email') {
 2572:                 $response=&reply('instemailrules:'.&escape($udom),
 2573:                                  $homeserver);
 2574:             } else {
 2575:                 $response=&reply('instuserrules:'.&escape($udom),
 2576:                                  $homeserver);
 2577:             }
 2578:             if (($response ne 'refused') && ($response ne 'error') && 
 2579:                 ($response ne 'unknown_cmd') && 
 2580:                 ($response ne 'no_such_host')) {
 2581:                 my ($hashitems,$orderitems) = split(/:/,$response);
 2582:                 my @pairs=split(/\&/,$hashitems);
 2583:                 foreach my $item (@pairs) {
 2584:                     my ($key,$value)=split(/=/,$item,2);
 2585:                     $key = &unescape($key);
 2586:                     next if ($key =~ /^error: 2 /);
 2587:                     $ruleshash{$key}=&thaw_unescape($value);
 2588:                 }
 2589:                 my @esc_order = split(/\&/,$orderitems);
 2590:                 foreach my $item (@esc_order) {
 2591:                     push(@ruleorder,&unescape($item));
 2592:                 }
 2593:             }
 2594:         }
 2595:     }
 2596:     return (\%ruleshash,\@ruleorder);
 2597: }
 2598: 
 2599: # ------------- Get Authentication, Language and User Tools Defaults for Domain
 2600: 
 2601: sub get_domain_defaults {
 2602:     my ($domain,$ignore_cache) = @_;
 2603:     return if (($domain eq '') || ($domain eq 'public'));
 2604:     my $cachetime = 60*60*24;
 2605:     unless ($ignore_cache) {
 2606:         my ($result,$cached)=&is_cached_new('domdefaults',$domain);
 2607:         if (defined($cached)) {
 2608:             if (ref($result) eq 'HASH') {
 2609:                 return %{$result};
 2610:             }
 2611:         }
 2612:     }
 2613:     my %domdefaults;
 2614:     my %domconfig =
 2615:          &Apache::lonnet::get_dom('configuration',['defaults','quotas',
 2616:                                   'requestcourses','inststatus',
 2617:                                   'coursedefaults','usersessions',
 2618:                                   'requestauthor','selfenrollment',
 2619:                                   'coursecategories','ssl','autoenroll',
 2620:                                   'trust','helpsettings','wafproxy'],$domain);
 2621:     my @coursetypes = ('official','unofficial','community','textbook','placement');
 2622:     if (ref($domconfig{'defaults'}) eq 'HASH') {
 2623:         $domdefaults{'lang_def'} = $domconfig{'defaults'}{'lang_def'}; 
 2624:         $domdefaults{'auth_def'} = $domconfig{'defaults'}{'auth_def'};
 2625:         $domdefaults{'auth_arg_def'} = $domconfig{'defaults'}{'auth_arg_def'};
 2626:         $domdefaults{'timezone_def'} = $domconfig{'defaults'}{'timezone_def'};
 2627:         $domdefaults{'datelocale_def'} = $domconfig{'defaults'}{'datelocale_def'};
 2628:         $domdefaults{'portal_def'} = $domconfig{'defaults'}{'portal_def'};
 2629:         $domdefaults{'intauth_cost'} = $domconfig{'defaults'}{'intauth_cost'};
 2630:         $domdefaults{'intauth_switch'} = $domconfig{'defaults'}{'intauth_switch'};
 2631:         $domdefaults{'intauth_check'} = $domconfig{'defaults'}{'intauth_check'};
 2632:     } else {
 2633:         $domdefaults{'lang_def'} = &domain($domain,'lang_def');
 2634:         $domdefaults{'auth_def'} = &domain($domain,'auth_def');
 2635:         $domdefaults{'auth_arg_def'} = &domain($domain,'auth_arg_def');
 2636:     }
 2637:     if (ref($domconfig{'quotas'}) eq 'HASH') {
 2638:         if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 2639:             $domdefaults{'defaultquota'} = $domconfig{'quotas'}{'defaultquota'};
 2640:         } else {
 2641:             $domdefaults{'defaultquota'} = $domconfig{'quotas'};
 2642:         }
 2643:         my @usertools = ('aboutme','blog','webdav','portfolio');
 2644:         foreach my $item (@usertools) {
 2645:             if (ref($domconfig{'quotas'}{$item}) eq 'HASH') {
 2646:                 $domdefaults{$item} = $domconfig{'quotas'}{$item};
 2647:             }
 2648:         }
 2649:         if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 2650:             $domdefaults{'authorquota'} = $domconfig{'quotas'}{'authorquota'};
 2651:         }
 2652:     }
 2653:     if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 2654:         foreach my $item ('official','unofficial','community','textbook','placement') {
 2655:             $domdefaults{$item} = $domconfig{'requestcourses'}{$item};
 2656:         }
 2657:     }
 2658:     if (ref($domconfig{'requestauthor'}) eq 'HASH') {
 2659:         $domdefaults{'requestauthor'} = $domconfig{'requestauthor'};
 2660:     }
 2661:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
 2662:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
 2663:             $domdefaults{$item} = $domconfig{'inststatus'}{$item};
 2664:         }
 2665:     }
 2666:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
 2667:         $domdefaults{'canuse_pdfforms'} = $domconfig{'coursedefaults'}{'canuse_pdfforms'};
 2668:         $domdefaults{'usejsme'} = $domconfig{'coursedefaults'}{'usejsme'};
 2669:         $domdefaults{'uselcmath'} = $domconfig{'coursedefaults'}{'uselcmath'};
 2670:         if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
 2671:             $domdefaults{'postsubmit'} = $domconfig{'coursedefaults'}{'postsubmit'}{'client'};
 2672:         }
 2673:         foreach my $type (@coursetypes) {
 2674:             if (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
 2675:                 unless ($type eq 'community') {
 2676:                     $domdefaults{$type.'credits'} = $domconfig{'coursedefaults'}{'coursecredits'}{$type};
 2677:                 }
 2678:             }
 2679:             if (ref($domconfig{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
 2680:                 $domdefaults{$type.'quota'} = $domconfig{'coursedefaults'}{'uploadquota'}{$type};
 2681:             }
 2682:             if ($domdefaults{'postsubmit'} eq 'on') {
 2683:                 if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
 2684:                     $domdefaults{$type.'postsubtimeout'} = 
 2685:                         $domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}{$type}; 
 2686:                 }
 2687:             }
 2688:         }
 2689:         if (ref($domconfig{'coursedefaults'}{'canclone'}) eq 'HASH') {
 2690:             if (ref($domconfig{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
 2691:                 my @clonecodes = @{$domconfig{'coursedefaults'}{'canclone'}{'instcode'}};
 2692:                 if (@clonecodes) {
 2693:                     $domdefaults{'canclone'} = join('+',@clonecodes);
 2694:                 }
 2695:             }
 2696:         } elsif ($domconfig{'coursedefaults'}{'canclone'}) {
 2697:             $domdefaults{'canclone'}=$domconfig{'coursedefaults'}{'canclone'};
 2698:         }
 2699:         if ($domconfig{'coursedefaults'}{'texengine'}) {
 2700:             $domdefaults{'texengine'} = $domconfig{'coursedefaults'}{'texengine'};
 2701:         } 
 2702:     }
 2703:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
 2704:         if (ref($domconfig{'usersessions'}{'remote'}) eq 'HASH') {
 2705:             $domdefaults{'remotesessions'} = $domconfig{'usersessions'}{'remote'};
 2706:         }
 2707:         if (ref($domconfig{'usersessions'}{'hosted'}) eq 'HASH') {
 2708:             $domdefaults{'hostedsessions'} = $domconfig{'usersessions'}{'hosted'};
 2709:         }
 2710:         if (ref($domconfig{'usersessions'}{'offloadnow'}) eq 'HASH') {
 2711:             $domdefaults{'offloadnow'} = $domconfig{'usersessions'}{'offloadnow'};
 2712:         }
 2713:         if (ref($domconfig{'usersessions'}{'offloadoth'}) eq 'HASH') {
 2714:             $domdefaults{'offloadoth'} = $domconfig{'usersessions'}{'offloadoth'};
 2715:         }
 2716:     }
 2717:     if (ref($domconfig{'selfenrollment'}) eq 'HASH') {
 2718:         if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
 2719:             my @settings = ('types','registered','enroll_dates','access_dates','section',
 2720:                             'approval','limit');
 2721:             foreach my $type (@coursetypes) {
 2722:                 if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
 2723:                     my @mgrdc = ();
 2724:                     foreach my $item (@settings) {
 2725:                         if ($domconfig{'selfenrollment'}{'admin'}{$type}{$item} eq '0') {
 2726:                             push(@mgrdc,$item);
 2727:                         }
 2728:                     }
 2729:                     if (@mgrdc) {
 2730:                         $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
 2731:                     }
 2732:                 }
 2733:             }
 2734:         }
 2735:         if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
 2736:             foreach my $type (@coursetypes) {
 2737:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
 2738:                     foreach my $item (keys(%{$domconfig{'selfenrollment'}{'default'}{$type}})) {
 2739:                         $domdefaults{$type.'selfenroll'.$item} = $domconfig{'selfenrollment'}{'default'}{$type}{$item};
 2740:                     }
 2741:                 }
 2742:             }
 2743:         }
 2744:     }
 2745:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 2746:         $domdefaults{'catauth'} = 'std';
 2747:         $domdefaults{'catunauth'} = 'std';
 2748:         if ($domconfig{'coursecategories'}{'auth'}) {
 2749:             $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
 2750:         }
 2751:         if ($domconfig{'coursecategories'}{'unauth'}) {
 2752:             $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
 2753:         }
 2754:     }
 2755:     if (ref($domconfig{'ssl'}) eq 'HASH') {
 2756:         if (ref($domconfig{'ssl'}{'replication'}) eq 'HASH') {
 2757:             $domdefaults{'replication'} = $domconfig{'ssl'}{'replication'};
 2758:         }
 2759:         if (ref($domconfig{'ssl'}{'connto'}) eq 'HASH') {
 2760:             $domdefaults{'connect'} = $domconfig{'ssl'}{'connto'};
 2761:         }
 2762:         if (ref($domconfig{'ssl'}{'connfrom'}) eq 'HASH') {
 2763:             $domdefaults{'connect'} = $domconfig{'ssl'}{'connfrom'};
 2764:         }
 2765:     }
 2766:     if (ref($domconfig{'trust'}) eq 'HASH') {
 2767:         my @prefixes = qw(content shared enroll othcoau coaurem domroles catalog reqcrs msg);
 2768:         foreach my $prefix (@prefixes) {
 2769:             if (ref($domconfig{'trust'}{$prefix}) eq 'HASH') {
 2770:                 $domdefaults{'trust'.$prefix} = $domconfig{'trust'}{$prefix};
 2771:             }
 2772:         }
 2773:     }
 2774:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 2775:         $domdefaults{'autofailsafe'} = $domconfig{'autoenroll'}{'autofailsafe'};
 2776:     }
 2777:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
 2778:         $domdefaults{'submitbugs'} = $domconfig{'helpsettings'}{'submitbugs'};
 2779:         if (ref($domconfig{'helpsettings'}{'adhoc'}) eq 'HASH') {
 2780:             $domdefaults{'adhocroles'} = $domconfig{'helpsettings'}{'adhoc'};
 2781:         }
 2782:     }
 2783:     if (ref($domconfig{'wafproxy'}) eq 'HASH') {
 2784:         foreach my $item ('ipheader','trusted','vpnint','vpnext','sslopt') {
 2785:             if ($domconfig{'wafproxy'}{$item}) {
 2786:                 $domdefaults{'waf_'.$item} = $domconfig{'wafproxy'}{$item};
 2787:             }
 2788:         }
 2789:     } 
 2790:     &do_cache_new('domdefaults',$domain,\%domdefaults,$cachetime);
 2791:     return %domdefaults;
 2792: }
 2793: 
 2794: sub get_dom_cats {
 2795:     my ($dom) = @_;
 2796:     return unless (&domain($dom));
 2797:     my ($cats,$cached)=&is_cached_new('cats',$dom);
 2798:     unless (defined($cached)) {
 2799:         my %domconfig = &get_dom('configuration',['coursecategories'],$dom);
 2800:         if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 2801:             if (ref($domconfig{'coursecategories'}{'cats'}) eq 'HASH') {
 2802:                 %{$cats} = %{$domconfig{'coursecategories'}{'cats'}};
 2803:             } else {
 2804:                 $cats = {};
 2805:             }
 2806:         } else {
 2807:             $cats = {};
 2808:         }
 2809:         &Apache::lonnet::do_cache_new('cats',$dom,$cats,3600);
 2810:     }
 2811:     return $cats;
 2812: }
 2813: 
 2814: sub get_dom_instcats {
 2815:     my ($dom) = @_;
 2816:     return unless (&domain($dom));
 2817:     my ($instcats,$cached)=&is_cached_new('instcats',$dom);
 2818:     unless (defined($cached)) {
 2819:         my (%coursecodes,%codes,@codetitles,%cat_titles,%cat_order);
 2820:         my $totcodes = &retrieve_instcodes(\%coursecodes,$dom);
 2821:         if ($totcodes > 0) {
 2822:             my $caller = 'global';
 2823:             if (&auto_instcode_format($caller,$dom,\%coursecodes,\%codes,
 2824:                                       \@codetitles,\%cat_titles,\%cat_order) eq 'ok') {
 2825:                 $instcats = {
 2826:                                 codes => \%codes,
 2827:                                 codetitles => \@codetitles,
 2828:                                 cat_titles => \%cat_titles,
 2829:                                 cat_order => \%cat_order,
 2830:                             };
 2831:                 &do_cache_new('instcats',$dom,$instcats,3600);
 2832:             }
 2833:         }
 2834:     }
 2835:     return $instcats;
 2836: }
 2837: 
 2838: sub retrieve_instcodes {
 2839:     my ($coursecodes,$dom) = @_;
 2840:     my $totcodes;
 2841:     my %courses = &courseiddump($dom,'.',1,'.','.','.',undef,undef,'Course');
 2842:     foreach my $course (keys(%courses)) {
 2843:         if (ref($courses{$course}) eq 'HASH') {
 2844:             if ($courses{$course}{'inst_code'} ne '') {
 2845:                 $$coursecodes{$course} = $courses{$course}{'inst_code'};
 2846:                 $totcodes ++;
 2847:             }
 2848:         }
 2849:     }
 2850:     return $totcodes;
 2851: }
 2852: 
 2853: sub course_portal_url {
 2854:     my ($cnum,$cdom) = @_;
 2855:     my $chome = &homeserver($cnum,$cdom);
 2856:     my $hostname = &hostname($chome);
 2857:     my $protocol = $protocol{$chome};
 2858:     $protocol = 'http' if ($protocol ne 'https');
 2859:     my %domdefaults = &get_domain_defaults($cdom);
 2860:     my $firsturl;
 2861:     if ($domdefaults{'portal_def'}) {
 2862:         $firsturl = $domdefaults{'portal_def'};
 2863:     } else {
 2864:         $firsturl = $protocol.'://'.$hostname;
 2865:     }
 2866:     return $firsturl;
 2867: }
 2868: 
 2869: # --------------------------------------------- Get domain config for passwords
 2870: 
 2871: sub get_passwdconf {
 2872:     my ($dom) = @_;
 2873:     my (%passwdconf,$gotconf,$lookup);
 2874:     my ($result,$cached)=&is_cached_new('passwdconf',$dom);
 2875:     if (defined($cached)) {
 2876:         if (ref($result) eq 'HASH') {
 2877:             %passwdconf = %{$result};
 2878:             $gotconf = 1;
 2879:         }
 2880:     }
 2881:     unless ($gotconf) {
 2882:         my %domconfig = &get_dom('configuration',['passwords'],$dom);
 2883:         if (ref($domconfig{'passwords'}) eq 'HASH') {
 2884:             %passwdconf = %{$domconfig{'passwords'}};
 2885:         }
 2886:         my $cachetime = 24*60*60;
 2887:         &do_cache_new('passwdconf',$dom,\%passwdconf,$cachetime);
 2888:     }
 2889:     return %passwdconf;
 2890: }
 2891: 
 2892: # --------------------------------------------------- Assign a key to a student
 2893: 
 2894: sub assign_access_key {
 2895: #
 2896: # a valid key looks like uname:udom#comments
 2897: # comments are being appended
 2898: #
 2899:     my ($ckey,$kdom,$knum,$cdom,$cnum,$udom,$uname,$logentry)=@_;
 2900:     $kdom=
 2901:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($kdom));
 2902:     $knum=
 2903:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($knum));
 2904:     $cdom=
 2905:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
 2906:     $cnum=
 2907:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
 2908:     $udom=$env{'user.name'} unless (defined($udom));
 2909:     $uname=$env{'user.domain'} unless (defined($uname));
 2910:     my %existing=&get('accesskeys',[$ckey],$kdom,$knum);
 2911:     if (($existing{$ckey}=~/^\#(.*)$/) || # - new key
 2912:         ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#(.*)$/)) { 
 2913:                                                   # assigned to this person
 2914:                                                   # - this should not happen,
 2915:                                                   # unless something went wrong
 2916:                                                   # the first time around
 2917: # ready to assign
 2918:         $logentry=$1.'; '.$logentry;
 2919:         if (&put('accesskeys',{$ckey=>$uname.':'.$udom.'#'.$logentry},
 2920:                                                  $kdom,$knum) eq 'ok') {
 2921: # key now belongs to user
 2922: 	    my $envkey='key.'.$cdom.'_'.$cnum;
 2923:             if (&put('environment',{$envkey => $ckey}) eq 'ok') {
 2924:                 &appenv({'environment.'.$envkey => $ckey});
 2925:                 return 'ok';
 2926:             } else {
 2927:                 return 
 2928:   'error: Count not permanently assign key, will need to be re-entered later.';
 2929: 	    }
 2930:         } else {
 2931:             return 'error: Could not assign key, try again later.';
 2932:         }
 2933:     } elsif (!$existing{$ckey}) {
 2934: # the key does not exist
 2935: 	return 'error: The key does not exist';
 2936:     } else {
 2937: # the key is somebody else's
 2938: 	return 'error: The key is already in use';
 2939:     }
 2940: }
 2941: 
 2942: # ------------------------------------------ put an additional comment on a key
 2943: 
 2944: sub comment_access_key {
 2945: #
 2946: # a valid key looks like uname:udom#comments
 2947: # comments are being appended
 2948: #
 2949:     my ($ckey,$cdom,$cnum,$logentry)=@_;
 2950:     $cdom=
 2951:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
 2952:     $cnum=
 2953:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
 2954:     my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
 2955:     if ($existing{$ckey}) {
 2956:         $existing{$ckey}.='; '.$logentry;
 2957: # ready to assign
 2958:         if (&put('accesskeys',{$ckey=>$existing{$ckey}},
 2959:                                                  $cdom,$cnum) eq 'ok') {
 2960: 	    return 'ok';
 2961:         } else {
 2962: 	    return 'error: Count not store comment.';
 2963:         }
 2964:     } else {
 2965: # the key does not exist
 2966: 	return 'error: The key does not exist';
 2967:     }
 2968: }
 2969: 
 2970: # ------------------------------------------------------ Generate a set of keys
 2971: 
 2972: sub generate_access_keys {
 2973:     my ($number,$cdom,$cnum,$logentry)=@_;
 2974:     $cdom=
 2975:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
 2976:     $cnum=
 2977:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
 2978:     unless (&allowed('mky',$cdom)) { return 0; }
 2979:     unless (($cdom) && ($cnum)) { return 0; }
 2980:     if ($number>10000) { return 0; }
 2981:     sleep(2); # make sure don't get same seed twice
 2982:     srand(time()^($$+($$<<15))); # from "Programming Perl"
 2983:     my $total=0;
 2984:     for (my $i=1;$i<=$number;$i++) {
 2985:        my $newkey=sprintf("%lx",int(100000*rand)).'-'.
 2986:                   sprintf("%lx",int(100000*rand)).'-'.
 2987:                   sprintf("%lx",int(100000*rand));
 2988:        $newkey=~s/1/g/g; # folks mix up 1 and l
 2989:        $newkey=~s/0/h/g; # and also 0 and O
 2990:        my %existing=&get('accesskeys',[$newkey],$cdom,$cnum);
 2991:        if ($existing{$newkey}) {
 2992:            $i--;
 2993:        } else {
 2994: 	  if (&put('accesskeys',
 2995:               { $newkey => '# generated '.localtime().
 2996:                            ' by '.$env{'user.name'}.'@'.$env{'user.domain'}.
 2997:                            '; '.$logentry },
 2998: 		   $cdom,$cnum) eq 'ok') {
 2999:               $total++;
 3000: 	  }
 3001:        }
 3002:     }
 3003:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
 3004:          'Generated '.$total.' keys for '.$cnum.' at '.$cdom);
 3005:     return $total;
 3006: }
 3007: 
 3008: # ------------------------------------------------------- Validate an accesskey
 3009: 
 3010: sub validate_access_key {
 3011:     my ($ckey,$cdom,$cnum,$udom,$uname)=@_;
 3012:     $cdom=
 3013:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
 3014:     $cnum=
 3015:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
 3016:     $udom=$env{'user.domain'} unless (defined($udom));
 3017:     $uname=$env{'user.name'} unless (defined($uname));
 3018:     my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
 3019:     return ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#/);
 3020: }
 3021: 
 3022: # ------------------------------------- Find the section of student in a course
 3023: sub devalidate_getsection_cache {
 3024:     my ($udom,$unam,$courseid)=@_;
 3025:     my $hashid="$udom:$unam:$courseid";
 3026:     &devalidate_cache_new('getsection',$hashid);
 3027: }
 3028: 
 3029: sub courseid_to_courseurl {
 3030:     my ($courseid) = @_;
 3031:     #already url style courseid
 3032:     return $courseid if ($courseid =~ m{^/});
 3033: 
 3034:     if (exists($env{'course.'.$courseid.'.num'})) {
 3035: 	my $cnum = $env{'course.'.$courseid.'.num'};
 3036: 	my $cdom = $env{'course.'.$courseid.'.domain'};
 3037: 	return "/$cdom/$cnum";
 3038:     }
 3039: 
 3040:     my %courseinfo=&Apache::lonnet::coursedescription($courseid);
 3041:     if (exists($courseinfo{'num'})) {
 3042: 	return "/$courseinfo{'domain'}/$courseinfo{'num'}";
 3043:     }
 3044: 
 3045:     return undef;
 3046: }
 3047: 
 3048: sub getsection {
 3049:     my ($udom,$unam,$courseid)=@_;
 3050:     my $cachetime=1800;
 3051: 
 3052:     my $hashid="$udom:$unam:$courseid";
 3053:     my ($result,$cached)=&is_cached_new('getsection',$hashid);
 3054:     if (defined($cached)) { return $result; }
 3055: 
 3056:     my %Pending; 
 3057:     my %Expired;
 3058:     #
 3059:     # Each role can either have not started yet (pending), be active, 
 3060:     #    or have expired.
 3061:     #
 3062:     # If there is an active role, we are done.
 3063:     #
 3064:     # If there is more than one role which has not started yet, 
 3065:     #     choose the one which will start sooner
 3066:     # If there is one role which has not started yet, return it.
 3067:     #
 3068:     # If there is more than one expired role, choose the one which ended last.
 3069:     # If there is a role which has expired, return it.
 3070:     #
 3071:     $courseid = &courseid_to_courseurl($courseid);
 3072:     my %roleshash = &dump('roles',$udom,$unam,$courseid);
 3073:     foreach my $key (keys(%roleshash)) {
 3074:         next if ($key !~/^\Q$courseid\E(?:\/)*(\w+)*\_st$/);
 3075:         my $section=$1;
 3076:         if ($key eq $courseid.'_st') { $section=''; }
 3077:         my ($dummy,$end,$start)=split(/\_/,&unescape($roleshash{$key}));
 3078:         my $now=time;
 3079:         if (defined($end) && $end && ($now > $end)) {
 3080:             $Expired{$end}=$section;
 3081:             next;
 3082:         }
 3083:         if (defined($start) && $start && ($now < $start)) {
 3084:             $Pending{$start}=$section;
 3085:             next;
 3086:         }
 3087:         return &do_cache_new('getsection',$hashid,$section,$cachetime);
 3088:     }
 3089:     #
 3090:     # Presumedly there will be few matching roles from the above
 3091:     # loop and the sorting time will be negligible.
 3092:     if (scalar(keys(%Pending))) {
 3093:         my ($time) = sort {$a <=> $b} keys(%Pending);
 3094:         return &do_cache_new('getsection',$hashid,$Pending{$time},$cachetime);
 3095:     } 
 3096:     if (scalar(keys(%Expired))) {
 3097:         my @sorted = sort {$a <=> $b} keys(%Expired);
 3098:         my $time = pop(@sorted);
 3099:         return &do_cache_new('getsection',$hashid,$Expired{$time},$cachetime);
 3100:     }
 3101:     return &do_cache_new('getsection',$hashid,'-1',$cachetime);
 3102: }
 3103: 
 3104: sub save_cache {
 3105:     &purge_remembered();
 3106:     #&Apache::loncommon::validate_page();
 3107:     undef(%env);
 3108:     undef($env_loaded);
 3109: }
 3110: 
 3111: my $to_remember=-1;
 3112: my %remembered;
 3113: my %accessed;
 3114: my $kicks=0;
 3115: my $hits=0;
 3116: sub make_key {
 3117:     my ($name,$id) = @_;
 3118:     if (length($id) > 65 
 3119: 	&& length(&escape($id)) > 200) {
 3120: 	$id=length($id).':'.&Digest::MD5::md5_hex($id);
 3121:     }
 3122:     return &escape($name.':'.$id);
 3123: }
 3124: 
 3125: sub devalidate_cache_new {
 3126:     my ($name,$id,$debug) = @_;
 3127:     if ($debug) { &Apache::lonnet::logthis("deleting $name:$id"); }
 3128:     my $remembered_id=$name.':'.$id;
 3129:     $id=&make_key($name,$id);
 3130:     $memcache->delete($id);
 3131:     delete($remembered{$remembered_id});
 3132:     delete($accessed{$remembered_id});
 3133: }
 3134: 
 3135: sub is_cached_new {
 3136:     my ($name,$id,$debug) = @_;
 3137:     my $remembered_id=$name.':'.$id; # this is to avoid make_key (which is slow) whenever possible
 3138:     if (exists($remembered{$remembered_id})) {
 3139: 	if ($debug) { &Apache::lonnet::logthis("Early return $remembered_id of $remembered{$remembered_id} "); }
 3140: 	$accessed{$remembered_id}=[&gettimeofday()];
 3141: 	$hits++;
 3142: 	return ($remembered{$remembered_id},1);
 3143:     }
 3144:     $id=&make_key($name,$id);
 3145:     my $value = $memcache->get($id);
 3146:     if (!(defined($value))) {
 3147: 	if ($debug) { &Apache::lonnet::logthis("getting $id is not defined"); }
 3148: 	return (undef,undef);
 3149:     }
 3150:     if ($value eq '__undef__') {
 3151: 	if ($debug) { &Apache::lonnet::logthis("getting $id is __undef__"); }
 3152: 	$value=undef;
 3153:     }
 3154:     &make_room($remembered_id,$value,$debug);
 3155:     if ($debug) { &Apache::lonnet::logthis("getting $id is $value"); }
 3156:     return ($value,1);
 3157: }
 3158: 
 3159: sub do_cache_new {
 3160:     my ($name,$id,$value,$time,$debug) = @_;
 3161:     my $remembered_id=$name.':'.$id;
 3162:     $id=&make_key($name,$id);
 3163:     my $setvalue=$value;
 3164:     if (!defined($setvalue)) {
 3165: 	$setvalue='__undef__';
 3166:     }
 3167:     if (!defined($time) ) {
 3168: 	$time=600;
 3169:     }
 3170:     if ($debug) { &Apache::lonnet::logthis("Setting $id to $value"); }
 3171:     my $result = $memcache->set($id,$setvalue,$time);
 3172:     if (! $result) {
 3173: 	&logthis("caching of id -> $id  failed");
 3174: 	$memcache->disconnect_all();
 3175:     }
 3176:     # need to make a copy of $value
 3177:     &make_room($remembered_id,$value,$debug);
 3178:     return $value;
 3179: }
 3180: 
 3181: sub make_room {
 3182:     my ($remembered_id,$value,$debug)=@_;
 3183: 
 3184:     $remembered{$remembered_id}= (ref($value)) ? &Storable::dclone($value)
 3185:                                     : $value;
 3186:     if ($to_remember<0) { return; }
 3187:     $accessed{$remembered_id}=[&gettimeofday()];
 3188:     if (scalar(keys(%remembered)) <= $to_remember) { return; }
 3189:     my $to_kick;
 3190:     my $max_time=0;
 3191:     foreach my $other (keys(%accessed)) {
 3192: 	if (&tv_interval($accessed{$other}) > $max_time) {
 3193: 	    $to_kick=$other;
 3194: 	    $max_time=&tv_interval($accessed{$other});
 3195: 	}
 3196:     }
 3197:     delete($remembered{$to_kick});
 3198:     delete($accessed{$to_kick});
 3199:     $kicks++;
 3200:     if ($debug) { &logthis("kicking $to_kick $max_time $kicks\n"); }
 3201:     return;
 3202: }
 3203: 
 3204: sub purge_remembered {
 3205:     #&logthis("Tossing ".scalar(keys(%remembered)));
 3206:     #&logthis(sprintf("%-20s is %s",'%remembered',length(&freeze(\%remembered))));
 3207:     undef(%remembered);
 3208:     undef(%accessed);
 3209: }
 3210: # ------------------------------------- Read an entry from a user's environment
 3211: 
 3212: sub userenvironment {
 3213:     my ($udom,$unam,@what)=@_;
 3214:     my $items;
 3215:     foreach my $item (@what) {
 3216:         $items.=&escape($item).'&';
 3217:     }
 3218:     $items=~s/\&$//;
 3219:     my %returnhash=();
 3220:     my $uhome = &homeserver($unam,$udom);
 3221:     unless ($uhome eq 'no_host') {
 3222:         my @answer=split(/\&/, 
 3223:             &reply('get:'.$udom.':'.$unam.':environment:'.$items,$uhome));
 3224:         if ($#answer==0 && $answer[0] =~ /^(con_lost|error:|no_such_host)/i) {
 3225:             return %returnhash;
 3226:         }
 3227:         my $i;
 3228:         for ($i=0;$i<=$#what;$i++) {
 3229: 	    $returnhash{$what[$i]}=&unescape($answer[$i]);
 3230:         }
 3231:     }
 3232:     return %returnhash;
 3233: }
 3234: 
 3235: # ---------------------------------------------------------- Get a studentphoto
 3236: sub studentphoto {
 3237:     my ($udom,$unam,$ext) = @_;
 3238:     my $home=&Apache::lonnet::homeserver($unam,$udom);
 3239:     if (defined($env{'request.course.id'})) {
 3240:         if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
 3241:             if ($udom eq $env{'course.'.$env{'request.course.id'}.'.domain'}) {
 3242:                 return(&retrievestudentphoto($udom,$unam,$ext)); 
 3243:             } else {
 3244:                 my ($result,$perm_reqd)=
 3245: 		    &Apache::lonnet::auto_photo_permission($unam,$udom);
 3246:                 if ($result eq 'ok') {
 3247:                     if (!($perm_reqd eq 'yes')) {
 3248:                         return(&retrievestudentphoto($udom,$unam,$ext));
 3249:                     }
 3250:                 }
 3251:             }
 3252:         }
 3253:     } else {
 3254:         my ($result,$perm_reqd) = 
 3255: 	    &Apache::lonnet::auto_photo_permission($unam,$udom);
 3256:         if ($result eq 'ok') {
 3257:             if (!($perm_reqd eq 'yes')) {
 3258:                 return(&retrievestudentphoto($udom,$unam,$ext));
 3259:             }
 3260:         }
 3261:     }
 3262:     return '/adm/lonKaputt/lonlogo_broken.gif';
 3263: }
 3264: 
 3265: sub retrievestudentphoto {
 3266:     my ($udom,$unam,$ext,$type) = @_;
 3267:     my $home=&Apache::lonnet::homeserver($unam,$udom);
 3268:     my $ret=&Apache::lonnet::reply("studentphoto:$udom:$unam:$ext:$type",$home);
 3269:     if ($ret eq 'ok') {
 3270:         my $url="/uploaded/$udom/$unam/internal/studentphoto.$ext";
 3271:         if ($type eq 'thumbnail') {
 3272:             $url="/uploaded/$udom/$unam/internal/studentphoto_tn.$ext"; 
 3273:         }
 3274:         my $tokenurl=&Apache::lonnet::tokenwrapper($url);
 3275:         return $tokenurl;
 3276:     } else {
 3277:         if ($type eq 'thumbnail') {
 3278:             return '/adm/lonKaputt/genericstudent_tn.gif';
 3279:         } else { 
 3280:             return '/adm/lonKaputt/lonlogo_broken.gif';
 3281:         }
 3282:     }
 3283: }
 3284: 
 3285: # -------------------------------------------------------------------- New chat
 3286: 
 3287: sub chatsend {
 3288:     my ($newentry,$anon,$group)=@_;
 3289:     my $cnum=$env{'course.'.$env{'request.course.id'}.'.num'};
 3290:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 3291:     my $chome=$env{'course.'.$env{'request.course.id'}.'.home'};
 3292:     &reply('chatsend:'.$cdom.':'.$cnum.':'.
 3293: 	   &escape($env{'user.domain'}.':'.$env{'user.name'}.':'.$anon.':'.
 3294: 		   &escape($newentry)).':'.$group,$chome);
 3295: }
 3296: 
 3297: # ------------------------------------------ Find current version of a resource
 3298: 
 3299: sub getversion {
 3300:     my $fname=&clutter(shift);
 3301:     unless ($fname=~m{^(/adm/wrapper|)/res/}) { return -1; }
 3302:     return &currentversion(&filelocation('',$fname));
 3303: }
 3304: 
 3305: sub currentversion {
 3306:     my $fname=shift;
 3307:     my $author=$fname;
 3308:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
 3309:     my ($udom,$uname)=split(/\//,$author);
 3310:     my $home=&homeserver($uname,$udom);
 3311:     if ($home eq 'no_host') { 
 3312:         return -1; 
 3313:     }
 3314:     my $answer=&reply("currentversion:$fname",$home);
 3315:     if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
 3316: 	return -1;
 3317:     }
 3318:     return $answer;
 3319: }
 3320: 
 3321: #
 3322: # Return special version number of resource if set by override, empty otherwise
 3323: #
 3324: sub usedversion {
 3325:     my $fname=shift;
 3326:     unless ($fname) { $fname=$env{'request.uri'}; }
 3327:     my ($urlversion)=($fname=~/\.(\d+)\.\w+$/);
 3328:     if ($urlversion) { return $urlversion; }
 3329:     return '';
 3330: }
 3331: 
 3332: # ----------------------------- Subscribe to a resource, return URL if possible
 3333: 
 3334: sub subscribe {
 3335:     my $fname=shift;
 3336:     if ($fname=~/\/(aboutme|syllabus|bulletinboard|smppg)$/) { return ''; }
 3337:     $fname=~s/[\n\r]//g;
 3338:     my $author=$fname;
 3339:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
 3340:     my ($udom,$uname)=split(/\//,$author);
 3341:     my $home=homeserver($uname,$udom);
 3342:     if ($home eq 'no_host') {
 3343:         return 'not_found';
 3344:     }
 3345:     my $answer=reply("sub:$fname",$home);
 3346:     if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
 3347: 	$answer.=' by '.$home;
 3348:     }
 3349:     return $answer;
 3350: }
 3351:     
 3352: # -------------------------------------------------------------- Replicate file
 3353: 
 3354: sub repcopy {
 3355:     my $filename=shift;
 3356:     $filename=~s/\/+/\//g;
 3357:     my $londocroot = $perlvar{'lonDocRoot'};
 3358:     if ($filename=~m{^\Q$londocroot/adm/\E}) { return 'ok'; }
 3359:     if ($filename=~m{^\Q/home/httpd/lonUsers/\E}) { return 'ok'; }
 3360:     if ($filename=~m{^\Q$londocroot/userfiles/\E} or
 3361: 	$filename=~m{^/*(uploaded|editupload)/}) {
 3362: 	return &repcopy_userfile($filename);
 3363:     }
 3364:     $filename=~s/[\n\r]//g;
 3365:     my $transname="$filename.in.transfer";
 3366: # FIXME: this should flock
 3367:     if ((-e $filename) || (-e $transname)) { return 'ok'; }
 3368:     my $remoteurl=subscribe($filename);
 3369:     if ($remoteurl =~ /^con_lost by/) {
 3370: 	   &logthis("Subscribe returned $remoteurl: $filename");
 3371:            return 'unavailable';
 3372:     } elsif ($remoteurl eq 'not_found') {
 3373: 	   #&logthis("Subscribe returned not_found: $filename");
 3374: 	   return 'not_found';
 3375:     } elsif ($remoteurl =~ /^rejected by/) {
 3376: 	   &logthis("Subscribe returned $remoteurl: $filename");
 3377:            return 'forbidden';
 3378:     } elsif ($remoteurl eq 'directory') {
 3379:            return 'ok';
 3380:     } else {
 3381:         my $author=$filename;
 3382:         $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
 3383:         my ($udom,$uname)=split(/\//,$author);
 3384:         my $home=homeserver($uname,$udom);
 3385:         unless ($home eq $perlvar{'lonHostID'}) {
 3386:            my @parts=split(/\//,$filename);
 3387:            my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 3388:            if ($path ne "$londocroot/res") {
 3389:                &logthis("Malconfiguration for replication: $filename");
 3390: 	       return 'bad_request';
 3391:            }
 3392:            my $count;
 3393:            for ($count=5;$count<$#parts;$count++) {
 3394:                $path.="/$parts[$count]";
 3395:                if ((-e $path)!=1) {
 3396: 		   mkdir($path,0777);
 3397:                }
 3398:            }
 3399:            my $request=new HTTP::Request('GET',"$remoteurl");
 3400:            my $response;
 3401:            if ($remoteurl =~ m{/raw/}) {
 3402:                $response=&LONCAPA::LWPReq::makerequest($home,$request,$transname,\%perlvar,'',0,1);
 3403:            } else {
 3404:                $response=&LONCAPA::LWPReq::makerequest($home,$request,$transname,\%perlvar,'',1);
 3405:            }
 3406:            if ($response->is_error()) {
 3407: 	       unlink($transname);
 3408:                my $message=$response->status_line;
 3409:                &logthis("<font color=\"blue\">WARNING:"
 3410:                        ." LWP get: $message: $filename</font>");
 3411:                return 'unavailable';
 3412:            } else {
 3413: 	       if ($remoteurl!~/\.meta$/) {
 3414:                   my $mrequest=new HTTP::Request('GET',$remoteurl.'.meta');
 3415:                   my $mresponse;
 3416:                   if ($remoteurl =~ m{/raw/}) {
 3417:                       $mresponse = &LONCAPA::LWPReq::makerequest($home,$mrequest,$filename.'.meta',\%perlvar,'',0,1);
 3418:                   } else {
 3419:                       $mresponse = &LONCAPA::LWPReq::makerequest($home,$mrequest,$filename.'.meta',\%perlvar,'',1);
 3420:                   }
 3421:                   if ($mresponse->is_error()) {
 3422: 		      unlink($filename.'.meta');
 3423:                       &logthis(
 3424:                      "<font color=\"yellow\">INFO: No metadata: $filename</font>");
 3425:                   }
 3426: 	       }
 3427:                rename($transname,$filename);
 3428:                return 'ok';
 3429:            }
 3430:        }
 3431:     }
 3432: }
 3433: 
 3434: # ------------------------------------------------- Unsubscribe from a resource
 3435: 
 3436: sub unsubscribe {
 3437:     my ($fname) = @_;
 3438:     my $answer;
 3439:     if ($fname=~/\/(aboutme|syllabus|bulletinboard|smppg)$/) { return $answer; }
 3440:     $fname=~s/[\n\r]//g;
 3441:     my $author=$fname;
 3442:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
 3443:     my ($udom,$uname)=split(/\//,$author);
 3444:     my $home=homeserver($uname,$udom);
 3445:     if ($home eq 'no_host') {
 3446:         $answer = 'no_host';
 3447:     } elsif (grep { $_ eq $home } &current_machine_ids()) {
 3448:         $answer = 'home';
 3449:     } else {
 3450:         my $defdom = $perlvar{'lonDefDomain'};
 3451:         if (&will_trust('content',$defdom,$udom)) {
 3452:             $answer = reply("unsub:$fname",$home);
 3453:         } else {
 3454:             $answer = 'untrusted';
 3455:         }
 3456:     }
 3457:     return $answer;
 3458: }
 3459: 
 3460: # ------------------------------------------------ Get server side include body
 3461: sub ssi_body {
 3462:     my ($filelink,%form)=@_;
 3463:     if (! exists($form{'LONCAPA_INTERNAL_no_discussion'})) {
 3464:         $form{'LONCAPA_INTERNAL_no_discussion'}='true';
 3465:     }
 3466:     my $output='';
 3467:     my $response;
 3468:     if ($filelink=~/^https?\:/) {
 3469:        ($output,$response)=&externalssi($filelink);
 3470:     } else {
 3471:        $filelink .= $filelink=~/\?/ ? '&' : '?';
 3472:        $filelink .= 'inhibitmenu=yes';
 3473:        ($output,$response)=&ssi($filelink,%form);
 3474:     }
 3475:     $output=~s|//(\s*<!--)? BEGIN LON-CAPA Internal.+?// END LON-CAPA Internal\s*(-->)?\s||gs;
 3476:     $output=~s/^.*?\<body[^\>]*\>//si;
 3477:     $output=~s/\<\/body\s*\>.*?$//si;
 3478:     if (wantarray) {
 3479:         return ($output, $response);
 3480:     } else {
 3481:         return $output;
 3482:     }
 3483: }
 3484: 
 3485: # --------------------------------------------------------- Server Side Include
 3486: 
 3487: sub absolute_url {
 3488:     my ($host_name,$unalias,$keep_proto) = @_;
 3489:     my $protocol = ($ENV{'SERVER_PORT'} == 443?'https://':'http://');
 3490:     if ($host_name eq '') {
 3491: 	$host_name = $ENV{'SERVER_NAME'};
 3492:     }
 3493:     if ($unalias) {
 3494:         my $alias = &get_proxy_alias();
 3495:         if ($alias eq $host_name) {
 3496:             my $lonhost = $perlvar{'lonHostID'};
 3497:             my $hostname = &hostname($lonhost);
 3498:             my $lcproto; 
 3499:             if (($keep_proto) || ($hostname eq '')) {
 3500:                 $lcproto = $protocol;
 3501:             } else {
 3502:                 $lcproto = $protocol{$lonhost};
 3503:                 $lcproto = 'http' if ($lcproto ne 'https');
 3504:                 $lcproto .= '://';
 3505:             }
 3506:             unless ($hostname eq '') {
 3507:                 return $lcproto.$hostname;
 3508:             }
 3509:         }
 3510:     }
 3511:     return $protocol.$host_name;
 3512: }
 3513: 
 3514: #
 3515: #   Server side include.
 3516: # Parameters:
 3517: #  fn     Possibly encrypted resource name/id.
 3518: #  form   Hash that describes how the rendering should be done
 3519: #         and other things.
 3520: # Returns:
 3521: #   Scalar context: The content of the response.
 3522: #   Array context:  2 element list of the content and the full response object.
 3523: #     
 3524: sub ssi {
 3525: 
 3526:     my ($fn,%form)=@_;
 3527:     my ($host,$request,$response);
 3528:     $host = &absolute_url('',1);
 3529: 
 3530:     $form{'no_update_last_known'}=1;
 3531:     &Apache::lonenc::check_encrypt(\$fn);
 3532:     if (%form) {
 3533:       $request=new HTTP::Request('POST',$host.$fn);
 3534:       $request->content(join('&',map { 
 3535:             my $name = escape($_);
 3536:             "$name=" . ( ref($form{$_}) eq 'ARRAY' 
 3537:             ? join("&$name=", map {escape($_) } @{$form{$_}}) 
 3538:             : &escape($form{$_}) );    
 3539:         } keys(%form)));
 3540:     } else {
 3541:       $request=new HTTP::Request('GET',$host.$fn);
 3542:     }
 3543: 
 3544:     $request->header(Cookie => $ENV{'HTTP_COOKIE'});
 3545:     my $lonhost = $perlvar{'lonHostID'};
 3546:     my $islocal;
 3547:     if (($env{'request.course.id'}) &&
 3548:         ($form{'grade_courseid'} eq $env{'request.course.id'}) &&
 3549:         ($form{'grade_username'} ne '') && ($form{'grade_domain'} ne '') &&
 3550:         ($form{'grade_symb'} ne '') &&
 3551:         (&Apache::lonnet::allowed('mgr',$env{'request.course.id'}.
 3552:                                  ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:'')))) {
 3553:         $islocal = 1;
 3554:     }
 3555:     $response= &LONCAPA::LWPReq::makerequest($lonhost,$request,'',\%perlvar,
 3556:                                              '','','',$islocal);
 3557: 
 3558:     if (wantarray) {
 3559: 	return ($response->content, $response);
 3560:     } else {
 3561: 	return $response->content;
 3562:     }
 3563: }
 3564: 
 3565: sub externalssi {
 3566:     my ($url)=@_;
 3567:     my $request=new HTTP::Request('GET',$url);
 3568:     my $response = &LONCAPA::LWPReq::makerequest('',$request,'',\%perlvar);
 3569:     if (wantarray) {
 3570:         return ($response->content, $response);
 3571:     } else {
 3572:         return $response->content;
 3573:     }
 3574: }
 3575: 
 3576: 
 3577: # If the local copy of a replicated resource is outdated, trigger a  
 3578: # connection from the homeserver to flush the delayed queue. If no update 
 3579: # happens, remove local copies of outdated resource (and corresponding
 3580: # metadata file).
 3581: 
 3582: sub remove_stale_resfile {
 3583:     my ($url) = @_;
 3584:     my $removed;
 3585:     if ($url=~m{^/res/($match_domain)/($match_username)/}) {
 3586:         my $audom = $1;
 3587:         my $auname = $2;
 3588:         unless (($url =~ /\.\d+\.\w+$/) || ($url =~ m{^/res/lib/templates/})) {
 3589:             my $homeserver = &homeserver($auname,$audom);
 3590:             unless (($homeserver eq 'no_host') ||
 3591:                     (grep { $_ eq $homeserver } &current_machine_ids())) {
 3592:                 my $fname = &filelocation('',$url);
 3593:                 if (-e $fname) {
 3594:                     my $hostname = &hostname($homeserver);
 3595:                     if ($hostname) {
 3596:                         my $protocol = $protocol{$homeserver};
 3597:                         $protocol = 'http' if ($protocol ne 'https');
 3598:                         my $uri = &declutter($url);
 3599:                         my $request=new HTTP::Request('HEAD',$protocol.'://'.$hostname.'/raw/'.$uri);
 3600:                         my $response = &LONCAPA::LWPReq::makerequest($homeserver,$request,'',\%perlvar,5,0,1);
 3601:                         if ($response->is_success()) {
 3602:                             my $remmodtime = &HTTP::Date::str2time( $response->header('Last-modified') );
 3603:                             my $locmodtime = (stat($fname))[9];
 3604:                             if ($locmodtime < $remmodtime) {
 3605:                                 my $stale;
 3606:                                 my $answer = &reply('pong',$homeserver);
 3607:                                 if ($answer eq $homeserver.':'.$perlvar{'lonHostID'}) {
 3608:                                     sleep(0.2);
 3609:                                     $locmodtime = (stat($fname))[9];
 3610:                                     if ($locmodtime < $remmodtime) {
 3611:                                         my $posstransfer = $fname.'.in.transfer';
 3612:                                         if ((-e $posstransfer) && ($remmodtime < (stat($posstransfer))[9])) {
 3613:                                             $removed = 1;
 3614:                                         } else {
 3615:                                             $stale = 1;
 3616:                                         }
 3617:                                     } else {
 3618:                                         $removed = 1;
 3619:                                     }
 3620:                                 } else {
 3621:                                     $stale = 1;
 3622:                                 }
 3623:                                 if ($stale) {
 3624:                                     if (unlink($fname)) {
 3625:                                         if ($uri!~/\.meta$/) {
 3626:                                             if (-e $fname.'.meta') {
 3627:                                                 unlink($fname.'.meta');
 3628:                                             }
 3629:                                         }
 3630:                                         my $unsubresult = &unsubscribe($fname);
 3631:                                         unless ($unsubresult eq 'ok') {
 3632:                                             &logthis("no unsub of $fname from $homeserver, reason: $unsubresult");
 3633:                                         }
 3634:                                         $removed = 1;
 3635:                                     }
 3636:                                 }
 3637:                             }
 3638:                         }
 3639:                     }
 3640:                 }
 3641:             }
 3642:         }
 3643:     }
 3644:     return $removed;
 3645: }
 3646: 
 3647: # -------------------------------- Allow a /uploaded/ URI to be vouched for
 3648: 
 3649: sub allowuploaded {
 3650:     my ($srcurl,$url)=@_;
 3651:     $url=&clutter(&declutter($url));
 3652:     my $dir=$url;
 3653:     $dir=~s/\/[^\/]+$//;
 3654:     my %httpref=();
 3655:     my $httpurl=&hreflocation('',$url);
 3656:     $httpref{'httpref.'.$httpurl}=$srcurl;
 3657:     &Apache::lonnet::appenv(\%httpref);
 3658: }
 3659: 
 3660: #
 3661: # Determine if the current user should be able to edit a particular resource,
 3662: # when viewing in course context.
 3663: # (a) When viewing resource used to determine if "Edit" item is included in 
 3664: #     Functions.
 3665: # (b) When displaying folder contents in course editor, used to determine if
 3666: #     "Edit" link will be displayed alongside resource.
 3667: #
 3668: #  input: six args -- filename (decluttered), course number, course domain,
 3669: #                   url, symb (if registered) and group (if this is a group
 3670: #                   item -- e.g., bulletin board, group page etc.).
 3671: #  output: array of five scalars -- 
 3672: #          $cfile -- url for file editing if editable on current server
 3673: #          $home -- homeserver of resource (i.e., for author if published,
 3674: #                                           or course if uploaded.).
 3675: #          $switchserver --  1 if server switch will be needed.
 3676: #          $forceedit -- 1 if icon/link should be to go to edit mode 
 3677: #          $forceview -- 1 if icon/link should be to go to view mode
 3678: #
 3679: 
 3680: sub can_edit_resource {
 3681:     my ($file,$cnum,$cdom,$resurl,$symb,$group) = @_;
 3682:     my ($cfile,$home,$switchserver,$forceedit,$forceview,$uploaded,$incourse);
 3683: #
 3684: # For aboutme pages user can only edit his/her own.
 3685: #
 3686:     if ($resurl =~ m{^/?adm/($match_domain)/($match_username)/aboutme$}) {
 3687:         my ($sdom,$sname) = ($1,$2);
 3688:         if (($sdom eq $env{'user.domain'}) && ($sname eq $env{'user.name'})) {
 3689:             $home = $env{'user.home'};
 3690:             $cfile = $resurl;
 3691:             if ($env{'form.forceedit'}) {
 3692:                 $forceview = 1;
 3693:             } else {
 3694:                 $forceedit = 1;
 3695:             }
 3696:             return ($cfile,$home,$switchserver,$forceedit,$forceview);
 3697:         } else {
 3698:             return;
 3699:         }
 3700:     }
 3701: 
 3702:     if ($env{'request.course.id'}) {
 3703:         my $crsedit = &Apache::lonnet::allowed('mdc',$env{'request.course.id'});
 3704:         if ($group ne '') {
 3705: # if this is a group homepage or group bulletin board, check group privs
 3706:             my $allowed = 0;
 3707:             if ($resurl =~ m{^/?adm/$cdom/$cnum/$group/smppg$}) {
 3708:                 if ((&allowed('mdg',$env{'request.course.id'}.
 3709:                               ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''))) ||
 3710:                         (&allowed('mgh',$env{'request.course.id'}.'/'.$group)) || $crsedit) {
 3711:                     $allowed = 1;
 3712:                 }
 3713:             } elsif ($resurl =~ m{^/?adm/$cdom/$cnum/\d+/bulletinboard$}) {
 3714:                 if ((&allowed('mdg',$env{'request.course.id'}.($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''))) ||
 3715:                         (&allowed('cgb',$env{'request.course.id'}.'/'.$group)) || $crsedit) {
 3716:                     $allowed = 1;
 3717:                 }
 3718:             }
 3719:             if ($allowed) {
 3720:                 $home=&homeserver($cnum,$cdom);
 3721:                 if ($env{'form.forceedit'}) {
 3722:                     $forceview = 1;
 3723:                 } else {
 3724:                     $forceedit = 1;
 3725:                 }
 3726:                 $cfile = $resurl;
 3727:             } else {
 3728:                 return;
 3729:             }
 3730:         } else {
 3731:             if ($resurl =~ m{^/?adm/viewclasslist$}) {
 3732:                 unless (&Apache::lonnet::allowed('opa',$env{'request.course.id'})) {
 3733:                     return;
 3734:                 }
 3735:             } elsif (!$crsedit) {
 3736: #
 3737: # No edit allowed where CC has switched to student role.
 3738: #
 3739:                 return;
 3740:             }
 3741:         }
 3742:     }
 3743: 
 3744:     if ($file ne '') {
 3745:         if (($cnum =~ /$match_courseid/) && ($cdom =~ /$match_domain/)) {
 3746:             if (&is_course_upload($file,$cnum,$cdom)) {
 3747:                 $uploaded = 1;
 3748:                 $incourse = 1;
 3749:                 if ($file =~/\.(htm|html|css|js|txt)$/) {
 3750:                     $cfile = &hreflocation('',$file);
 3751:                     if ($env{'form.forceedit'}) {
 3752:                         $forceview = 1;
 3753:                     } else {
 3754:                         $forceedit = 1;
 3755:                     }
 3756:                 }
 3757:             } elsif ($resurl =~ m{^/public/$cdom/$cnum/syllabus}) {
 3758:                 $incourse = 1;
 3759:                 if ($env{'form.forceedit'}) {
 3760:                     $forceview = 1;
 3761:                 } else {
 3762:                     $forceedit = 1;
 3763:                 }
 3764:                 $cfile = $resurl;
 3765:             } elsif (($resurl ne '') && (&is_on_map($resurl))) { 
 3766:                 if ($resurl =~ m{^/adm/$match_domain/$match_username/\d+/smppg|bulletinboard$}) {
 3767:                     $incourse = 1;
 3768:                     if ($env{'form.forceedit'}) {
 3769:                         $forceview = 1;
 3770:                     } else {
 3771:                         $forceedit = 1;
 3772:                     }
 3773:                     $cfile = $resurl;
 3774:                 } elsif ($resurl eq '/res/lib/templates/simpleproblem.problem') {
 3775:                     $incourse = 1;
 3776:                     $cfile = $resurl.'/smpedit';
 3777:                 } elsif ($resurl =~ m{^/adm/wrapper/ext/}) {
 3778:                     $incourse = 1;
 3779:                     if ($env{'form.forceedit'}) {
 3780:                         $forceview = 1;
 3781:                     } else {
 3782:                         $forceedit = 1;
 3783:                     }
 3784:                     $cfile = $resurl;
 3785:                 } elsif (($resurl =~ m{^/ext/}) && ($symb ne '')) {
 3786:                     my ($map,$id,$res) = &decode_symb($symb);
 3787:                     if ($map =~ /\.page$/) {
 3788:                         $incourse = 1;
 3789:                         if ($env{'form.forceedit'}) {
 3790:                             $forceview = 1;
 3791:                             $cfile = $map;
 3792:                         } else {
 3793:                             $forceedit = 1;
 3794:                             $cfile =  '/adm/wrapper'.$resurl;
 3795:                         }
 3796:                     }
 3797:                 } elsif ($resurl =~ m{^/adm/wrapper/adm/$cdom/$cnum/\d+/ext\.tool$}) {
 3798:                     $incourse = 1;
 3799:                     if ($env{'form.forceedit'}) {
 3800:                         $forceview = 1;
 3801:                     } else {
 3802:                         $forceedit = 1;
 3803:                     }
 3804:                     $cfile = $resurl;
 3805:                 } elsif ($resurl =~ m{^/?adm/viewclasslist$}) {
 3806:                     $incourse = 1;
 3807:                     if ($env{'form.forceedit'}) {
 3808:                         $forceview = 1;
 3809:                     } else {
 3810:                         $forceedit = 1;
 3811:                     }
 3812:                     $cfile = ($resurl =~ m{^/} ? $resurl : "/$resurl");
 3813:                 }
 3814:             } elsif ($resurl eq '/res/lib/templates/simpleproblem.problem/smpedit') {
 3815:                 my $template = '/res/lib/templates/simpleproblem.problem';
 3816:                 if (&is_on_map($template)) { 
 3817:                     $incourse = 1;
 3818:                     $forceview = 1;
 3819:                     $cfile = $template;
 3820:                 }
 3821:             } elsif (($resurl =~ m{^/adm/wrapper/ext/}) && ($env{'form.folderpath'} =~ /^supplemental/)) {
 3822:                 $incourse = 1;
 3823:                 if ($env{'form.forceedit'}) {
 3824:                     $forceview = 1;
 3825:                 } else {
 3826:                     $forceedit = 1;
 3827:                 }
 3828:                 $cfile = $resurl;
 3829:             } elsif (($resurl =~ m{^/adm/wrapper/adm/$cdom/$cnum/\d+/ext\.tool$}) && ($env{'form.folderpath'} =~ /^supplemental/)) {
 3830:                 $incourse = 1;
 3831:                 if ($env{'form.forceedit'}) {
 3832:                     $forceview = 1;
 3833:                 } else {
 3834:                     $forceedit = 1;
 3835:                 }
 3836:                 $cfile = $resurl;
 3837:             } elsif (($resurl eq '/adm/extresedit') && ($symb || $env{'form.folderpath'})) {
 3838:                 $incourse = 1;
 3839:                 $forceview = 1;
 3840:                 if ($symb) {
 3841:                     my ($map,$id,$res)=&decode_symb($symb);
 3842:                     $env{'request.symb'} = $symb;
 3843:                     $cfile = &clutter($res);
 3844:                 } else {
 3845:                     $cfile = $env{'form.suppurl'};
 3846:                     my $escfile = &unescape($cfile);
 3847:                     if ($escfile =~ m{^/adm/$cdom/$cnum/\d+/ext\.tool$}) {
 3848:                         $cfile = '/adm/wrapper'.$escfile;
 3849:                     } else {
 3850:                         $escfile =~ s{^http://}{};
 3851:                         $cfile = &escape("/adm/wrapper/ext/$escfile");
 3852:                     }
 3853:                 }
 3854:             } elsif ($resurl =~ m{^/?adm/viewclasslist$}) {
 3855:                 if ($env{'form.forceedit'}) {
 3856:                     $forceview = 1;
 3857:                 } else {
 3858:                     $forceedit = 1;
 3859:                 }
 3860:                 $cfile = ($resurl =~ m{^/} ? $resurl : "/$resurl");
 3861:             }
 3862:         }
 3863:         if ($uploaded || $incourse) {
 3864:             $home=&homeserver($cnum,$cdom);
 3865:         } elsif ($file !~ m{/$}) {
 3866:             $file=~s{^(priv/$match_domain/$match_username)}{/$1};
 3867:             $file=~s{^($match_domain/$match_username)}{/priv/$1};
 3868:             # Check that the user has permission to edit this resource
 3869:             my $setpriv = 1;
 3870:             my ($cfuname,$cfudom)=&constructaccess($file,$setpriv);
 3871:             if (defined($cfudom)) {
 3872:                 $home=&homeserver($cfuname,$cfudom);
 3873:                 $cfile=$file;
 3874:             }
 3875:         }
 3876:         if (($cfile ne '') && (!$incourse || $uploaded) && 
 3877:             (($home ne '') && ($home ne 'no_host'))) {
 3878:             my @ids=&current_machine_ids();
 3879:             unless (grep(/^\Q$home\E$/,@ids)) {
 3880:                 $switchserver=1;
 3881:             }
 3882:         }
 3883:     }
 3884:     return ($cfile,$home,$switchserver,$forceedit,$forceview);
 3885: }
 3886: 
 3887: sub is_course_upload {
 3888:     my ($file,$cnum,$cdom) = @_;
 3889:     my $uploadpath = &LONCAPA::propath($cdom,$cnum);
 3890:     $uploadpath =~ s{^\/}{};
 3891:     if (($file =~ m{^\Q$uploadpath\E/userfiles/(docs|supplemental)/}) ||
 3892:         ($file =~ m{^userfiles/\Q$cdom\E/\Q$cnum\E/(docs|supplemental)/})) {
 3893:         return 1;
 3894:     }
 3895:     return;
 3896: }
 3897: 
 3898: sub in_course {
 3899:     my ($udom,$uname,$cdom,$cnum,$type,$hideprivileged) = @_;
 3900:     if ($hideprivileged) {
 3901:         my $skipuser;
 3902:         my %coursehash = &coursedescription($cdom.'_'.$cnum);
 3903:         my @possdoms = ($cdom);  
 3904:         if ($coursehash{'checkforpriv'}) { 
 3905:             push(@possdoms,split(/,/,$coursehash{'checkforpriv'})); 
 3906:         }
 3907:         if (&privileged($uname,$udom,\@possdoms)) {
 3908:             $skipuser = 1;
 3909:             if ($coursehash{'nothideprivileged'}) {
 3910:                 foreach my $item (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
 3911:                     my $user;
 3912:                     if ($item =~ /:/) {
 3913:                         $user = $item;
 3914:                     } else {
 3915:                         $user = join(':',split(/[\@]/,$item));
 3916:                     }
 3917:                     if ($user eq $uname.':'.$udom) {
 3918:                         undef($skipuser);
 3919:                         last;
 3920:                     }
 3921:                 }
 3922:             }
 3923:             if ($skipuser) {
 3924:                 return 0;
 3925:             }
 3926:         }
 3927:     }
 3928:     $type ||= 'any';
 3929:     if (!defined($cdom) || !defined($cnum)) {
 3930:         my $cid  = $env{'request.course.id'};
 3931:         $cdom = $env{'course.'.$cid.'.domain'};
 3932:         $cnum = $env{'course.'.$cid.'.num'};
 3933:     }
 3934:     my $typesref;
 3935:     if (($type eq 'any') || ($type eq 'all')) {
 3936:         $typesref = ['active','previous','future'];
 3937:     } elsif ($type eq 'previous' || $type eq 'future') {
 3938:         $typesref = [$type];
 3939:     }
 3940:     my %roles = &get_my_roles($uname,$udom,'userroles',
 3941:                               $typesref,undef,[$cdom]);
 3942:     my ($tmp) = keys(%roles);
 3943:     return 0 if ($tmp =~ /^(con_lost|error|no_such_host)/i);
 3944:     my @course_roles = grep(/^\Q$cnum\E:\Q$cdom\E:/, keys(%roles));
 3945:     if (@course_roles > 0) {
 3946:         return 1;
 3947:     }
 3948:     return 0;
 3949: }
 3950: 
 3951: # --------- File operations in /home/httpd/html/userfiles/$domain/1/2/3/$course
 3952: # input: action, courseID, current domain, intended
 3953: #        path to file, source of file, instruction to parse file for objects,
 3954: #        ref to hash for embedded objects,
 3955: #        ref to hash for codebase of java objects.
 3956: #        reference to scalar to accommodate mime type determined
 3957: #          from File::MMagic if $parser = parse.
 3958: #
 3959: # output: url to file (if action was uploaddoc), 
 3960: #         ok if successful, or diagnostic message otherwise (if action was propagate or copy)
 3961: #
 3962: # Allows directory structure to be used within lonUsers/../userfiles/ for a 
 3963: # course.
 3964: #
 3965: # action = propagate - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
 3966: #          will be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles in
 3967: #          course's home server.
 3968: #
 3969: # action = copy - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file will
 3970: #          be copied from $source (current location) to 
 3971: #          /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
 3972: #         and will then be copied to
 3973: #          /home/httpd/lonUsers/$domain/1/2/3/$course/userfiles/$file in
 3974: #         course's home server.
 3975: #
 3976: # action = uploaddoc - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
 3977: #         will be retrived from $env{form.uploaddoc} (from DOCS interface) to
 3978: #         /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
 3979: #         and will then be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles/$file
 3980: #         in course's home server.
 3981: #
 3982: 
 3983: sub process_coursefile {
 3984:     my ($action,$docuname,$docudom,$file,$source,$parser,$allfiles,$codebase,
 3985:         $mimetype)=@_;
 3986:     my $fetchresult;
 3987:     my $home=&homeserver($docuname,$docudom);
 3988:     if ($action eq 'propagate') {
 3989:         $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
 3990: 			     $home);
 3991:     } else {
 3992:         my $fpath = '';
 3993:         my $fname = $file;
 3994:         ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
 3995:         $fpath=$docudom.'/'.$docuname.'/'.$fpath;
 3996:         my $filepath = &build_filepath($fpath);
 3997:         if ($action eq 'copy') {
 3998:             if ($source eq '') {
 3999:                 $fetchresult = 'no source file';
 4000:                 return $fetchresult;
 4001:             } else {
 4002:                 my $destination = $filepath.'/'.$fname;
 4003:                 rename($source,$destination);
 4004:                 $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
 4005:                                  $home);
 4006:             }
 4007:         } elsif ($action eq 'uploaddoc') {
 4008:             open(my $fh,'>',$filepath.'/'.$fname);
 4009:             print $fh $env{'form.'.$source};
 4010:             close($fh);
 4011:             if ($parser eq 'parse') {
 4012:                 my $mm = new File::MMagic;
 4013:                 my $type = $mm->checktype_filename($filepath.'/'.$fname);
 4014:                 if ($type eq 'text/html') {
 4015:                     my $parse_result = &extract_embedded_items($filepath.'/'.$fname,$allfiles,$codebase);
 4016:                     unless ($parse_result eq 'ok') {
 4017:                         &logthis('Failed to parse '.$filepath.'/'.$fname.' for embedded media: '.$parse_result);
 4018:                     }
 4019:                 }
 4020:                 if (ref($mimetype)) {
 4021:                     $$mimetype = $type;
 4022:                 } 
 4023:             }
 4024:             $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
 4025:                                  $home);
 4026:             if ($fetchresult eq 'ok') {
 4027:                 return '/uploaded/'.$fpath.'/'.$fname;
 4028:             } else {
 4029:                 &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
 4030:                         ' to host '.$home.': '.$fetchresult);
 4031:                 return '/adm/notfound.html';
 4032:             }
 4033:         }
 4034:     }
 4035:     unless ( $fetchresult eq 'ok') {
 4036:         &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
 4037:              ' to host '.$home.': '.$fetchresult);
 4038:     }
 4039:     return $fetchresult;
 4040: }
 4041: 
 4042: sub build_filepath {
 4043:     my ($fpath) = @_;
 4044:     my $filepath=$perlvar{'lonDocRoot'}.'/userfiles';
 4045:     unless ($fpath eq '') {
 4046:         my @parts=split('/',$fpath);
 4047:         foreach my $part (@parts) {
 4048:             $filepath.= '/'.$part;
 4049:             if ((-e $filepath)!=1) {
 4050:                 mkdir($filepath,0777);
 4051:             }
 4052:         }
 4053:     }
 4054:     return $filepath;
 4055: }
 4056: 
 4057: sub store_edited_file {
 4058:     my ($primary_url,$content,$docudom,$docuname,$fetchresult) = @_;
 4059:     my $file = $primary_url;
 4060:     $file =~ s#^/uploaded/$docudom/$docuname/##;
 4061:     my $fpath = '';
 4062:     my $fname = $file;
 4063:     ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
 4064:     $fpath=$docudom.'/'.$docuname.'/'.$fpath;
 4065:     my $filepath = &build_filepath($fpath);
 4066:     open(my $fh,'>',$filepath.'/'.$fname);
 4067:     print $fh $content;
 4068:     close($fh);
 4069:     my $home=&homeserver($docuname,$docudom);
 4070:     $$fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
 4071: 			  $home);
 4072:     if ($$fetchresult eq 'ok') {
 4073:         return '/uploaded/'.$fpath.'/'.$fname;
 4074:     } else {
 4075:         &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
 4076: 		 ' to host '.$home.': '.$$fetchresult);
 4077:         return '/adm/notfound.html';
 4078:     }
 4079: }
 4080: 
 4081: sub clean_filename {
 4082:     my ($fname,$args)=@_;
 4083: # Replace Windows backslashes by forward slashes
 4084:     $fname=~s/\\/\//g;
 4085:     if (!$args->{'keep_path'}) {
 4086:         # Get rid of everything but the actual filename
 4087: 	$fname=~s/^.*\/([^\/]+)$/$1/;
 4088:     }
 4089: # Replace spaces by underscores
 4090:     $fname=~s/\s+/\_/g;
 4091: # Transliterate non-ascii text to ascii
 4092:     my $lang = &Apache::lonlocal::current_language();
 4093:     $fname = &LONCAPA::transliterate::fname_to_ascii($fname,$lang);
 4094: # Replace all other weird characters by nothing
 4095:     $fname=~s{[^/\w\.\-]}{}g;
 4096: # Replace all .\d. sequences with _\d. so they no longer look like version
 4097: # numbers
 4098:     $fname=~s/\.(\d+)(?=\.)/_$1/g;
 4099: # Replace three or more adjacent underscores with one for consistency 
 4100: # with loncfile::filename_check() so complete url can be extracted by
 4101: # lonnet::decode_symb()
 4102:     $fname=~s/_{3,}/_/g;
 4103:     return $fname;
 4104: }
 4105: 
 4106: # This Function checks if an Image's dimensions exceed either $resizewidth (width) 
 4107: # or $resizeheight (height) - both pixels. If so, the image is scaled to produce an 
 4108: # image with the same aspect ratio as the original, but with dimensions which do 
 4109: # not exceed $resizewidth and $resizeheight.
 4110:  
 4111: sub resizeImage {
 4112:     my ($img_path,$resizewidth,$resizeheight) = @_;
 4113:     my $ima = Image::Magick->new;
 4114:     my $resized;
 4115:     if (-e $img_path) {
 4116:         $ima->Read($img_path);
 4117:         if (($resizewidth =~ /^\d+$/) && ($resizeheight > 0)) {
 4118:             my $width = $ima->Get('width');
 4119:             my $height = $ima->Get('height');
 4120:             if ($width > $resizewidth) {
 4121: 	        my $factor = $width/$resizewidth;
 4122:                 my $newheight = $height/$factor;
 4123:                 $ima->Scale(width=>$resizewidth,height=>$newheight);
 4124:                 $resized = 1;
 4125:             }
 4126:         }
 4127:         if (($resizeheight =~ /^\d+$/) && ($resizeheight > 0)) {
 4128:             my $width = $ima->Get('width');
 4129:             my $height = $ima->Get('height');
 4130:             if ($height > $resizeheight) {
 4131:                 my $factor = $height/$resizeheight;
 4132:                 my $newwidth = $width/$factor;
 4133:                 $ima->Scale(width=>$newwidth,height=>$resizeheight);
 4134:                 $resized = 1;
 4135:             }
 4136:         }
 4137:         if ($resized) {
 4138:             $ima->Write($img_path);
 4139:         }
 4140:     }
 4141:     return;
 4142: }
 4143: 
 4144: # --------------- Take an uploaded file and put it into the userfiles directory
 4145: # input: $formname - the contents of the file are in $env{"form.$formname"}
 4146: #                    the desired filename is in $env{"form.$formname.filename"}
 4147: #        $context - possible values: coursedoc, existingfile, overwrite, 
 4148: #                                    canceloverwrite, scantron or ''.
 4149: #                   if 'coursedoc': upload to the current course
 4150: #                   if 'existingfile': write file to tmp/overwrites directory 
 4151: #                   if 'canceloverwrite': delete file written to tmp/overwrites directory
 4152: #                   $context is passed as argument to &finishuserfileupload
 4153: #        $subdir - directory in userfile to store the file into
 4154: #        $parser - instruction to parse file for objects ($parser = parse) or
 4155: #                  if context is 'scantron', $parser is hashref of csv column mapping
 4156: #                  (e.g.,{ PaperID => 0, LastName => 1, FirstName => 2, ID => 3, 
 4157: #                          Section => 4, CODE => 5, FirstQuestion => 9 }).
 4158: #        $allfiles - reference to hash for embedded objects
 4159: #        $codebase - reference to hash for codebase of java objects
 4160: #        $desuname - username for permanent storage of uploaded file
 4161: #        $dsetudom - domain for permanaent storage of uploaded file
 4162: #        $thumbwidth - width (pixels) of thumbnail to make for uploaded image 
 4163: #        $thumbheight - height (pixels) of thumbnail to make for uploaded image
 4164: #        $resizewidth - width (pixels) to which to resize uploaded image
 4165: #        $resizeheight - height (pixels) to which to resize uploaded image
 4166: #        $mimetype - reference to scalar to accommodate mime type determined
 4167: #                    from File::MMagic.
 4168: # 
 4169: # output: url of file in userspace, or error: <message> 
 4170: #             or /adm/notfound.html if failure to upload occurse
 4171: 
 4172: sub userfileupload {
 4173:     my ($formname,$context,$subdir,$parser,$allfiles,$codebase,$destuname,
 4174:         $destudom,$thumbwidth,$thumbheight,$resizewidth,$resizeheight,$mimetype)=@_;
 4175:     if (!defined($subdir)) { $subdir='unknown'; }
 4176:     my $fname=$env{'form.'.$formname.'.filename'};
 4177:     $fname=&clean_filename($fname);
 4178:     # See if there is anything left
 4179:     unless ($fname) { return 'error: no uploaded file'; }
 4180:     # If filename now begins with a . prepend unix timestamp _ milliseconds
 4181:     if ($fname =~ /^\./) {
 4182:         my ($s,$usec) = &gettimeofday();
 4183:         while (length($usec) < 6) {
 4184:             $usec = '0'.$usec;
 4185:         }
 4186:         $fname = $s.'_'.substr($usec,0,3).$fname;
 4187:     }
 4188:     # Files uploaded to help request form, or uploaded to "create course" page are handled differently
 4189:     if ((($formname eq 'screenshot') && ($subdir eq 'helprequests')) ||
 4190:         (($formname eq 'coursecreatorxml') && ($subdir eq 'batchupload')) ||
 4191:          ($context eq 'existingfile') || ($context eq 'canceloverwrite')) {
 4192:         my $now = time;
 4193:         my $filepath;
 4194:         if (($formname eq 'screenshot') && ($subdir eq 'helprequests')) {
 4195:              $filepath = 'tmp/helprequests/'.$now;
 4196:         } elsif (($formname eq 'coursecreatorxml') && ($subdir eq 'batchupload')) {
 4197:              $filepath = 'tmp/addcourse/'.$destudom.'/web/'.$env{'user.name'}.
 4198:                          '_'.$env{'user.domain'}.'/pending';
 4199:         } elsif (($context eq 'existingfile') || ($context eq 'canceloverwrite')) {
 4200:             my ($docuname,$docudom);
 4201:             if ($destudom =~ /^$match_domain$/) {
 4202:                 $docudom = $destudom;
 4203:             } else {
 4204:                 $docudom = $env{'user.domain'};
 4205:             }
 4206:             if ($destuname =~ /^$match_username$/) {
 4207:                 $docuname = $destuname;
 4208:             } else {
 4209:                 $docuname = $env{'user.name'};
 4210:             }
 4211:             if (exists($env{'form.group'})) {
 4212:                 $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
 4213:                 $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 4214:             }
 4215:             $filepath = 'tmp/overwrites/'.$docudom.'/'.$docuname.'/'.$subdir;
 4216:             if ($context eq 'canceloverwrite') {
 4217:                 my $tempfile =  $perlvar{'lonDaemons'}.'/'.$filepath.'/'.$fname;
 4218:                 if (-e  $tempfile) {
 4219:                     my @info = stat($tempfile);
 4220:                     if ($info[9] eq $env{'form.timestamp'}) {
 4221:                         unlink($tempfile);
 4222:                     }
 4223:                 }
 4224:                 return;
 4225:             }
 4226:         }
 4227:         # Create the directory if not present
 4228:         my @parts=split(/\//,$filepath);
 4229:         my $fullpath = $perlvar{'lonDaemons'};
 4230:         for (my $i=0;$i<@parts;$i++) {
 4231:             $fullpath .= '/'.$parts[$i];
 4232:             if ((-e $fullpath)!=1) {
 4233:                 mkdir($fullpath,0777);
 4234:             }
 4235:         }
 4236:         open(my $fh,'>',$fullpath.'/'.$fname);
 4237:         print $fh $env{'form.'.$formname};
 4238:         close($fh);
 4239:         if ($context eq 'existingfile') {
 4240:             my @info = stat($fullpath.'/'.$fname);
 4241:             return ($fullpath.'/'.$fname,$info[9]);
 4242:         } else {
 4243:             return $fullpath.'/'.$fname;
 4244:         }
 4245:     }
 4246:     if ($subdir eq 'scantron') {
 4247:         $fname = 'scantron_orig_'.$fname;
 4248:     } else {
 4249:         $fname="$subdir/$fname";
 4250:     }
 4251:     if ($context eq 'coursedoc') {
 4252: 	my $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
 4253: 	my $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 4254:         if ($env{'form.folder'} =~ m/^(default|supplemental)/) {
 4255:             return &finishuserfileupload($docuname,$docudom,
 4256: 					 $formname,$fname,$parser,$allfiles,
 4257: 					 $codebase,$thumbwidth,$thumbheight,
 4258:                                          $resizewidth,$resizeheight,$context,$mimetype);
 4259:         } else {
 4260:             if ($env{'form.folder'}) {
 4261:                 $fname=$env{'form.folder'}.'/'.$fname;
 4262:             }
 4263:             return &process_coursefile('uploaddoc',$docuname,$docudom,
 4264: 				       $fname,$formname,$parser,
 4265: 				       $allfiles,$codebase,$mimetype);
 4266:         }
 4267:     } elsif (defined($destuname)) {
 4268:         my $docuname=$destuname;
 4269:         my $docudom=$destudom;
 4270: 	return &finishuserfileupload($docuname,$docudom,$formname,$fname,
 4271: 				     $parser,$allfiles,$codebase,
 4272:                                      $thumbwidth,$thumbheight,
 4273:                                      $resizewidth,$resizeheight,$context,$mimetype);
 4274:     } else {
 4275:         my $docuname=$env{'user.name'};
 4276:         my $docudom=$env{'user.domain'};
 4277:         if ((exists($env{'form.group'})) || ($context eq 'syllabus')) {
 4278:             $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
 4279:             $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 4280:         }
 4281: 	return &finishuserfileupload($docuname,$docudom,$formname,$fname,
 4282: 				     $parser,$allfiles,$codebase,
 4283:                                      $thumbwidth,$thumbheight,
 4284:                                      $resizewidth,$resizeheight,$context,$mimetype);
 4285:     }
 4286: }
 4287: 
 4288: sub finishuserfileupload {
 4289:     my ($docuname,$docudom,$formname,$fname,$parser,$allfiles,$codebase,
 4290:         $thumbwidth,$thumbheight,$resizewidth,$resizeheight,$context,$mimetype) = @_;
 4291:     my $path=$docudom.'/'.$docuname.'/';
 4292:     my $filepath=$perlvar{'lonDocRoot'};
 4293:   
 4294:     my ($fnamepath,$file,$fetchthumb);
 4295:     $file=$fname;
 4296:     if ($fname=~m|/|) {
 4297:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 4298: 	$path.=$fnamepath.'/';
 4299:     }
 4300:     my @parts=split(/\//,$filepath.'/userfiles/'.$path);
 4301:     my $count;
 4302:     for ($count=4;$count<=$#parts;$count++) {
 4303:         $filepath.="/$parts[$count]";
 4304:         if ((-e $filepath)!=1) {
 4305: 	    mkdir($filepath,0777);
 4306:         }
 4307:     }
 4308: 
 4309: # Save the file
 4310:     {
 4311: 	if (!open(FH,'>',$filepath.'/'.$file)) {
 4312: 	    &logthis('Failed to create '.$filepath.'/'.$file);
 4313: 	    print STDERR ('Failed to create '.$filepath.'/'.$file."\n");
 4314: 	    return '/adm/notfound.html';
 4315: 	}
 4316:         if ($context eq 'overwrite') {
 4317:             my $source =  LONCAPA::tempdir().'/overwrites/'.$docudom.'/'.$docuname.'/'.$fname;
 4318:             my $target = $filepath.'/'.$file;
 4319:             if (-e $source) {
 4320:                 my @info = stat($source);
 4321:                 if ($info[9] eq $env{'form.timestamp'}) {   
 4322:                     unless (&File::Copy::move($source,$target)) {
 4323:                         &logthis('Failed to overwrite '.$filepath.'/'.$file);
 4324:                         return "Moving from $source failed";
 4325:                     }
 4326:                 } else {
 4327:                     return "Temporary file: $source had unexpected date/time for last modification";
 4328:                 }
 4329:             } else {
 4330:                 return "Temporary file: $source missing";
 4331:             }
 4332:         } elsif (!print FH ($env{'form.'.$formname})) {
 4333: 	    &logthis('Failed to write to '.$filepath.'/'.$file);
 4334: 	    print STDERR ('Failed to write to '.$filepath.'/'.$file."\n");
 4335: 	    return '/adm/notfound.html';
 4336: 	}
 4337: 	close(FH);
 4338:         if ($resizewidth && $resizeheight) {
 4339:             my $mm = new File::MMagic;
 4340:             my $mime_type = $mm->checktype_filename($filepath.'/'.$file);
 4341:             if ($mime_type =~ m{^image/}) {
 4342: 	        &resizeImage($filepath.'/'.$file,$resizewidth,$resizeheight);
 4343:             }  
 4344: 	}
 4345:     }
 4346:     if (($context eq 'coursedoc') || ($parser eq 'parse')) {
 4347:         if (ref($mimetype)) {
 4348:             if ($$mimetype eq '') {
 4349:                 my $mm = new File::MMagic;
 4350:                 my $type = $mm->checktype_filename($filepath.'/'.$file);
 4351:                 $$mimetype = $type;
 4352:             }
 4353:         }
 4354:     }
 4355:     if (($context ne 'scantron') && ($parser eq 'parse')) {
 4356:         if ((ref($mimetype)) && ($$mimetype eq 'text/html')) {
 4357:             my $parse_result = &extract_embedded_items($filepath.'/'.$file,
 4358:                                                        $allfiles,$codebase);
 4359:             unless ($parse_result eq 'ok') {
 4360:                 &logthis('Failed to parse '.$filepath.$file.
 4361: 	   	         ' for embedded media: '.$parse_result); 
 4362:             }
 4363:         }
 4364:     } elsif (($context eq 'scantron') && (ref($parser) eq 'HASH')) {
 4365:         my $format = $env{'form.scantron_format'};
 4366:         &bubblesheet_converter($docudom,$filepath.'/'.$file,$parser,$format);
 4367:     }
 4368:     if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 4369:         my $input = $filepath.'/'.$file;
 4370:         my $output = $filepath.'/'.'tn-'.$file;
 4371:         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 4372:         my @args = ('convert','-sample',$thumbsize,$input,$output);
 4373:         system({$args[0]} @args);
 4374:         if (-e $filepath.'/'.'tn-'.$file) {
 4375:             $fetchthumb  = 1; 
 4376:         }
 4377:     }
 4378:  
 4379: # Notify homeserver to grep it
 4380: #
 4381:     my $docuhome=&homeserver($docuname,$docudom);	
 4382:     my $fetchresult= &reply('fetchuserfile:'.$path.$file,$docuhome);
 4383:     if ($fetchresult eq 'ok') {
 4384:         if ($fetchthumb) {
 4385:             my $thumbresult= &reply('fetchuserfile:'.$path.'tn-'.$file,$docuhome);
 4386:             if ($thumbresult ne 'ok') {
 4387:                 &logthis('Failed to transfer '.$path.'tn-'.$file.' to host '.
 4388:                          $docuhome.': '.$thumbresult);
 4389:             }
 4390:         }
 4391: #
 4392: # Return the URL to it
 4393:         return '/uploaded/'.$path.$file;
 4394:     } else {
 4395:         &logthis('Failed to transfer '.$path.$file.' to host '.$docuhome.
 4396: 		 ': '.$fetchresult);
 4397:         return '/adm/notfound.html';
 4398:     }
 4399: }
 4400: 
 4401: sub extract_embedded_items {
 4402:     my ($fullpath,$allfiles,$codebase,$content) = @_;
 4403:     my @state = ();
 4404:     my (%lastids,%related,%shockwave,%flashvars);
 4405:     my %javafiles = (
 4406:                       codebase => '',
 4407:                       code => '',
 4408:                       archive => ''
 4409:                     );
 4410:     my %mediafiles = (
 4411:                       src => '',
 4412:                       movie => '',
 4413:                      );
 4414:     my $p;
 4415:     if ($content) {
 4416:         $p = HTML::LCParser->new($content);
 4417:     } else {
 4418:         $p = HTML::LCParser->new($fullpath);
 4419:     }
 4420:     while (my $t=$p->get_token()) {
 4421: 	if ($t->[0] eq 'S') {
 4422: 	    my ($tagname, $attr) = ($t->[1],$t->[2]);
 4423: 	    push(@state, $tagname);
 4424:             if (lc($tagname) eq 'allow') {
 4425:                 &add_filetype($allfiles,$attr->{'src'},'src');
 4426:             }
 4427: 	    if (lc($tagname) eq 'img') {
 4428: 		&add_filetype($allfiles,$attr->{'src'},'src');
 4429: 	    }
 4430: 	    if (lc($tagname) eq 'a') {
 4431:                 unless (($attr->{'href'} =~ /^#/) || ($attr->{'href'} eq '')) {
 4432:                     &add_filetype($allfiles,$attr->{'href'},'href');
 4433:                 }
 4434: 	    }
 4435:             if (lc($tagname) eq 'script') {
 4436:                 my $src;
 4437:                 if ($attr->{'archive'} =~ /\.jar$/i) {
 4438:                     &add_filetype($allfiles,$attr->{'archive'},'archive');
 4439:                 } else {
 4440:                     if ($attr->{'src'} ne '') {
 4441:                         $src = $attr->{'src'};
 4442:                         &add_filetype($allfiles,$src,'src');
 4443:                     }
 4444:                 }
 4445:                 my $text = $p->get_trimmed_text();
 4446:                 if ($text =~ /\Qswfobject.registerObject(\E([^\)]+)\)/) {
 4447:                     my @swfargs = split(/,/,$1);
 4448:                     foreach my $item (@swfargs) {
 4449:                         $item =~ s/["']//g;
 4450:                         $item =~ s/^\s+//;
 4451:                         $item =~ s/\s+$//;
 4452:                     }
 4453:                     if (($swfargs[0] ne'') && ($swfargs[2] ne '')) {
 4454:                         if (ref($related{$swfargs[0]}) eq 'ARRAY') {
 4455:                             push(@{$related{$swfargs[0]}},$swfargs[2]);
 4456:                         } else {
 4457:                             $related{$swfargs[0]} = [$swfargs[2]];
 4458:                         }
 4459:                     }
 4460:                 }
 4461:             }
 4462:             if (lc($tagname) eq 'link') {
 4463:                 if (lc($attr->{'rel'}) eq 'stylesheet') { 
 4464:                     &add_filetype($allfiles,$attr->{'href'},'href');
 4465:                 }
 4466:             }
 4467: 	    if (lc($tagname) eq 'object' ||
 4468: 		(lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')) {
 4469: 		foreach my $item (keys(%javafiles)) {
 4470: 		    $javafiles{$item} = '';
 4471: 		}
 4472:                 if ((lc($tagname) eq 'object') && (lc($state[-2]) ne 'object')) {
 4473:                     $lastids{lc($tagname)} = $attr->{'id'};
 4474:                 }
 4475: 	    }
 4476: 	    if (lc($state[-2]) eq 'object' && lc($tagname) eq 'param') {
 4477: 		my $name = lc($attr->{'name'});
 4478: 		foreach my $item (keys(%javafiles)) {
 4479: 		    if ($name eq $item) {
 4480: 			$javafiles{$item} = $attr->{'value'};
 4481: 			last;
 4482: 		    }
 4483: 		}
 4484:                 my $pathfrom;
 4485: 		foreach my $item (keys(%mediafiles)) {
 4486: 		    if ($name eq $item) {
 4487:                         $pathfrom = $attr->{'value'};
 4488:                         $shockwave{$lastids{lc($state[-2])}} = $pathfrom;
 4489: 			&add_filetype($allfiles,$pathfrom,$name);
 4490: 			last;
 4491: 		    }
 4492: 		}
 4493:                 if ($name eq 'flashvars') {
 4494:                     $flashvars{$lastids{lc($state[-2])}} = $attr->{'value'};
 4495:                 }
 4496:                 if ($pathfrom ne '') {
 4497:                     &embedded_dependency($allfiles,\%related,$lastids{lc($state[-2])},
 4498:                                          $pathfrom);
 4499:                 }
 4500: 	    }
 4501: 	    if (lc($tagname) eq 'embed' || lc($tagname) eq 'applet') {
 4502: 		foreach my $item (keys(%javafiles)) {
 4503: 		    if ($attr->{$item}) {
 4504: 			$javafiles{$item} = $attr->{$item};
 4505: 			last;
 4506: 		    }
 4507: 		}
 4508: 		foreach my $item (keys(%mediafiles)) {
 4509: 		    if ($attr->{$item}) {
 4510: 			&add_filetype($allfiles,$attr->{$item},$item);
 4511: 			last;
 4512: 		    }
 4513: 		}
 4514:                 if (lc($tagname) eq 'embed') {
 4515:                     if (($attr->{'name'} ne '') && ($attr->{'src'} ne '')) {
 4516:                         &embedded_dependency($allfiles,\%related,$attr->{'name'},
 4517:                                              $attr->{'src'});
 4518:                     }
 4519:                 }
 4520: 	    }
 4521:             if (lc($tagname) eq 'iframe') {
 4522:                 my $src = $attr->{'src'} ;
 4523:                 if (($src ne '') && ($src !~ m{^(/|https?://)})) {
 4524:                     &add_filetype($allfiles,$src,'src');
 4525:                 } elsif ($src =~ m{^/}) {
 4526:                     if ($env{'request.course.id'}) {
 4527:                         my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 4528:                         my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 4529:                         my $url = &hreflocation('',$fullpath);
 4530:                         if ($url =~ m{^/uploaded/$cdom/$cnum/docs/(\w+/\d+)/}) {
 4531:                             my $relpath = $1;
 4532:                             if ($src =~ m{^/uploaded/$cdom/$cnum/docs/\Q$relpath\E/(.+)$}) {
 4533:                                 &add_filetype($allfiles,$1,'src');
 4534:                             }
 4535:                         }
 4536:                     }
 4537:                 }
 4538:             }
 4539:             if ($t->[4] =~ m{/>$}) {
 4540:                 pop(@state);
 4541:             }
 4542: 	} elsif ($t->[0] eq 'E') {
 4543: 	    my ($tagname) = ($t->[1]);
 4544: 	    if ($javafiles{'codebase'} ne '') {
 4545: 		$javafiles{'codebase'} .= '/';
 4546: 	    }  
 4547: 	    if (lc($tagname) eq 'applet' ||
 4548: 		lc($tagname) eq 'object' ||
 4549: 		(lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')
 4550: 		) {
 4551: 		foreach my $item (keys(%javafiles)) {
 4552: 		    if ($item ne 'codebase' && $javafiles{$item} ne '') {
 4553: 			my $file=$javafiles{'codebase'}.$javafiles{$item};
 4554: 			&add_filetype($allfiles,$file,$item);
 4555: 		    }
 4556: 		}
 4557: 	    } 
 4558: 	    pop @state;
 4559: 	}
 4560:     }
 4561:     foreach my $id (sort(keys(%flashvars))) {
 4562:         if ($shockwave{$id} ne '') {
 4563:             my @pairs = split(/\&/,$flashvars{$id});
 4564:             foreach my $pair (@pairs) {
 4565:                 my ($key,$value) = split(/\=/,$pair);
 4566:                 if ($key eq 'thumb') {
 4567:                     &add_filetype($allfiles,$value,$key);
 4568:                 } elsif ($key eq 'content') {
 4569:                     my ($path) = ($shockwave{$id} =~ m{^(.+/)[^/]+$});
 4570:                     my ($ext) = ($value =~ /\.([^.]+)$/);
 4571:                     if ($ext ne '') {
 4572:                         &add_filetype($allfiles,$path.$value,$ext);
 4573:                     }
 4574:                 }
 4575:             }
 4576:         }
 4577:     }
 4578:     return 'ok';
 4579: }
 4580: 
 4581: sub add_filetype {
 4582:     my ($allfiles,$file,$type)=@_;
 4583:     if (exists($allfiles->{$file})) {
 4584: 	unless (grep/^\Q$type\E$/, @{$allfiles->{$file}}) {
 4585: 	    push(@{$allfiles->{$file}}, &escape($type));
 4586: 	}
 4587:     } else {
 4588: 	@{$allfiles->{$file}} = (&escape($type));
 4589:     }
 4590: }
 4591: 
 4592: sub embedded_dependency {
 4593:     my ($allfiles,$related,$identifier,$pathfrom) = @_;
 4594:     if ((ref($allfiles) eq 'HASH') && (ref($related) eq 'HASH')) {
 4595:         if (($identifier ne '') &&
 4596:             (ref($related->{$identifier}) eq 'ARRAY') &&
 4597:             ($pathfrom ne '')) {
 4598:             my ($path) = ($pathfrom =~ m{^(.+/)[^/]+$});
 4599:             foreach my $dep (@{$related->{$identifier}}) {
 4600:                 &add_filetype($allfiles,$path.$dep,'object');
 4601:             }
 4602:         }
 4603:     }
 4604:     return;
 4605: }
 4606: 
 4607: sub bubblesheet_converter {
 4608:     my ($cdom,$fullpath,$config,$format) = @_;
 4609:     if ((&domain($cdom) ne '') &&
 4610:         ($fullpath =~ m{^\Q$perlvar{'lonDocRoot'}/userfiles/$cdom/\E$match_courseid/scantron_orig}) &&
 4611:         (-e $fullpath) && (ref($config) eq 'HASH') && ($format ne '')) {
 4612:         my (%csvcols,%csvoptions);
 4613:         if (ref($config->{'fields'}) eq 'HASH') {  
 4614:             %csvcols = %{$config->{'fields'}};
 4615:         }
 4616:         if (ref($config->{'options'}) eq 'HASH') {
 4617:             %csvoptions = %{$config->{'options'}};
 4618:         }
 4619:         my %csvbynum = reverse(%csvcols);
 4620:         my %scantronconf = &get_scantron_config($format,$cdom);
 4621:         if (keys(%scantronconf)) {
 4622:             my %bynum = (
 4623:                           $scantronconf{CODEstart} => 'CODEstart',
 4624:                           $scantronconf{IDstart}   => 'IDstart',
 4625:                           $scantronconf{PaperID}   => 'PaperID',
 4626:                           $scantronconf{FirstName} => 'FirstName',
 4627:                           $scantronconf{LastName}  => 'LastName',
 4628:                           $scantronconf{Qstart}    => 'Qstart',
 4629:                         );
 4630:             my @ordered;
 4631:             foreach my $item (sort { $a <=> $b } keys(%bynum)) {
 4632:                 push(@ordered,$bynum{$item});
 4633:             }
 4634:             my %mapstart = (
 4635:                               CODEstart => 'CODE',
 4636:                               IDstart   => 'ID',
 4637:                               PaperID   => 'PaperID',
 4638:                               FirstName => 'FirstName',
 4639:                               LastName  => 'LastName',
 4640:                               Qstart    => 'FirstQuestion',
 4641:                            );
 4642:             my %maplength = (
 4643:                               CODEstart => 'CODElength',
 4644:                               IDstart   => 'IDlength',
 4645:                               PaperID   => 'PaperIDlength',
 4646:                               FirstName => 'FirstNamelength',
 4647:                               LastName  => 'LastNamelength',
 4648:             );
 4649:             if (open(my $fh,'<',$fullpath)) {
 4650:                 my $output;
 4651:                 my %lettdig = &letter_to_digits();
 4652:                 my %diglett = reverse(%lettdig);
 4653:                 my $numletts = scalar(keys(%lettdig));
 4654:                 my $num = 0;
 4655:                 while (my $line=<$fh>) {
 4656:                     $num ++;
 4657:                     next if (($num == 1) && ($csvoptions{'hdr'} == 1));
 4658:                     $line =~ s{[\r\n]+$}{};
 4659:                     my %found;
 4660:                     my @values = split(/,/,$line);
 4661:                     my ($qstart,$record);
 4662:                     for (my $i=0; $i<@values; $i++) {
 4663:                         if ((($qstart ne '') && ($i > $qstart)) ||
 4664:                             ($csvbynum{$i} eq 'FirstQuestion')) {
 4665:                             if ($values[$i] eq '') {
 4666:                                 $values[$i] = $scantronconf{'Qoff'};
 4667:                             } elsif ($scantronconf{'Qon'} eq 'number') {
 4668:                                 if ($values[$i] =~ /^[A-Ja-j]$/) {
 4669:                                     $values[$i] = $lettdig{uc($values[$i])};
 4670:                                 }
 4671:                             } elsif ($scantronconf{'Qon'} eq 'letter') {
 4672:                                 if ($values[$i] =~ /^[0-9]$/) {
 4673:                                     $values[$i] = $diglett{$values[$i]};
 4674:                                 }
 4675:                             } else {
 4676:                                 if ($values[$i] =~ /^[0-9A-Ja-j]$/) {
 4677:                                     my $digit;
 4678:                                     if ($values[$i] =~ /^[A-Ja-j]$/) {
 4679:                                         $digit = $lettdig{uc($values[$i])}-1;
 4680:                                         if ($values[$i] eq 'J') {
 4681:                                             $digit += $numletts;
 4682:                                         }
 4683:                                     } elsif ($values[$i] =~ /^[0-9]$/) {
 4684:                                         $digit = $values[$i]-1;
 4685:                                         if ($values[$i] eq '0') {
 4686:                                             $digit += $numletts;
 4687:                                         }
 4688:                                     }
 4689:                                     my $qval='';
 4690:                                     for (my $j=0; $j<$scantronconf{'Qlength'}; $j++) {
 4691:                                         if ($j == $digit) {
 4692:                                             $qval .= $scantronconf{'Qon'};
 4693:                                         } else {
 4694:                                             $qval .= $scantronconf{'Qoff'};
 4695:                                         }
 4696:                                     }
 4697:                                     $values[$i] = $qval;
 4698:                                 }
 4699:                             }
 4700:                             if (length($values[$i]) > $scantronconf{'Qlength'}) {
 4701:                                 $values[$i] = substr($values[$i],0,$scantronconf{'Qlength'});
 4702:                             }
 4703:                             my $numblank = $scantronconf{'Qlength'} - length($values[$i]);
 4704:                             if ($numblank > 0) {
 4705:                                  $values[$i] .= ($scantronconf{'Qoff'} x $numblank);
 4706:                             }
 4707:                             if ($csvbynum{$i} eq 'FirstQuestion') {
 4708:                                 $qstart = $i;
 4709:                                 $found{$csvbynum{$i}} = $values[$i];
 4710:                             } else {
 4711:                                 $found{'FirstQuestion'} .= $values[$i];
 4712:                             }
 4713:                         } elsif (exists($csvbynum{$i})) {
 4714:                             if ($csvoptions{'rem'}) {
 4715:                                 $values[$i] =~ s/^\s+//;
 4716:                             }
 4717:                             if (($csvbynum{$i} eq 'PaperID') && ($csvoptions{'pad'})) {
 4718:                                 while (length($values[$i]) < $scantronconf{$maplength{$csvbynum{$i}}}) {
 4719:                                     $values[$i] = '0'.$values[$i];
 4720:                                 }
 4721:                             }
 4722:                             $found{$csvbynum{$i}} = $values[$i];
 4723:                         }
 4724:                     }
 4725:                     foreach my $item (@ordered) {
 4726:                         my $currlength = 1+length($record);
 4727:                         my $numspaces = $scantronconf{$item} - $currlength;
 4728:                         if ($numspaces > 0) {
 4729:                             $record .= (' ' x $numspaces);
 4730:                         }
 4731:                         if (($mapstart{$item} ne '') && (exists($found{$mapstart{$item}}))) {
 4732:                             unless ($item eq 'Qstart') {
 4733:                                 if (length($found{$mapstart{$item}}) > $scantronconf{$maplength{$item}}) {
 4734:                                     $found{$mapstart{$item}} = substr($found{$mapstart{$item}},0,$scantronconf{$maplength{$item}});
 4735:                                 }
 4736:                             }
 4737:                             $record .= $found{$mapstart{$item}};
 4738:                         }
 4739:                     }
 4740:                     $output .= "$record\n";
 4741:                 }
 4742:                 close($fh);
 4743:                 if ($output) {
 4744:                     if (open(my $fh,'>',$fullpath)) {
 4745:                         print $fh $output;
 4746:                         close($fh);
 4747:                     }
 4748:                 }
 4749:             }
 4750:         }
 4751:         return;
 4752:     }
 4753: }
 4754: 
 4755: sub letter_to_digits {
 4756:     my %lettdig = (
 4757:                     A => 1,
 4758:                     B => 2,
 4759:                     C => 3,
 4760:                     D => 4,
 4761:                     E => 5,
 4762:                     F => 6,
 4763:                     G => 7,
 4764:                     H => 8,
 4765:                     I => 9,
 4766:                     J => 0,
 4767:                   );
 4768:     return %lettdig;
 4769: }
 4770: 
 4771: sub get_scantron_config {
 4772:     my ($which,$cdom) = @_;
 4773:     my @lines = &get_scantronformat_file($cdom);
 4774:     my %config;
 4775:     #FIXME probably should move to XML it has already gotten a bit much now
 4776:     foreach my $line (@lines) {
 4777:         my ($name,$descrip)=split(/:/,$line);
 4778:         if ($name ne $which ) { next; }
 4779:         chomp($line);
 4780:         my @config=split(/:/,$line);
 4781:         $config{'name'}=$config[0];
 4782:         $config{'description'}=$config[1];
 4783:         $config{'CODElocation'}=$config[2];
 4784:         $config{'CODEstart'}=$config[3];
 4785:         $config{'CODElength'}=$config[4];
 4786:         $config{'IDstart'}=$config[5];
 4787:         $config{'IDlength'}=$config[6];
 4788:         $config{'Qstart'}=$config[7];
 4789:         $config{'Qlength'}=$config[8];
 4790:         $config{'Qoff'}=$config[9];
 4791:         $config{'Qon'}=$config[10];
 4792:         $config{'PaperID'}=$config[11];
 4793:         $config{'PaperIDlength'}=$config[12];
 4794:         $config{'FirstName'}=$config[13];
 4795:         $config{'FirstNamelength'}=$config[14];
 4796:         $config{'LastName'}=$config[15];
 4797:         $config{'LastNamelength'}=$config[16];
 4798:         $config{'BubblesPerRow'}=$config[17];
 4799:         last;
 4800:     }
 4801:     return %config;
 4802: }
 4803: 
 4804: sub get_scantronformat_file {
 4805:     my ($cdom) = @_;
 4806:     if ($cdom eq '') {
 4807:         $cdom= $env{'course.'.$env{'request.course.id'}.'.domain'};
 4808:     }
 4809:     my %domconfig = &get_dom('configuration',['scantron'],$cdom);
 4810:     my $gottab = 0;
 4811:     my @lines;
 4812:     if (ref($domconfig{'scantron'}) eq 'HASH') {
 4813:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
 4814:             my $formatfile = &getfile($perlvar{'lonDocRoot'}.$domconfig{'scantron'}{'scantronformat'});
 4815:             if ($formatfile ne '-1') {
 4816:                 @lines = split("\n",$formatfile,-1);
 4817:                 $gottab = 1;
 4818:             }
 4819:         }
 4820:     }
 4821:     if (!$gottab) {
 4822:         my $confname = $cdom.'-domainconfig';
 4823:         my $default = $perlvar{'lonDocRoot'}.'/res/'.$cdom.'/'.$confname.'/default.tab';
 4824:         my $formatfile = &getfile($default);
 4825:         if ($formatfile ne '-1') {
 4826:             @lines = split("\n",$formatfile,-1);
 4827:             $gottab = 1;
 4828:         }
 4829:     }
 4830:     if (!$gottab) {
 4831:         my @domains = &current_machine_domains();
 4832:         if (grep(/^\Q$cdom\E$/,@domains)) {
 4833:             if (open(my $fh,'<',$perlvar{'lonTabDir'}.'/scantronformat.tab')) {
 4834:                 @lines = <$fh>;
 4835:                 close($fh);
 4836:             }
 4837:         } else {
 4838:             if (open(my $fh,'<',$perlvar{'lonTabDir'}.'/default_scantronformat.tab')) {
 4839:                 @lines = <$fh>;
 4840:                 close($fh);
 4841:             }
 4842:         }
 4843:     }
 4844:     return @lines;
 4845: }
 4846: 
 4847: sub removeuploadedurl {
 4848:     my ($url)=@_;	
 4849:     my (undef,undef,$udom,$uname,$fname)=split('/',$url,5);    
 4850:     return &removeuserfile($uname,$udom,$fname);
 4851: }
 4852: 
 4853: sub removeuserfile {
 4854:     my ($docuname,$docudom,$fname)=@_;
 4855:     my $home=&homeserver($docuname,$docudom);    
 4856:     my $result = &reply("removeuserfile:$docudom/$docuname/$fname",$home);
 4857:     if ($result eq 'ok') {	
 4858:         if (($fname !~ /\.meta$/) && (&is_portfolio_file($fname))) {
 4859:             my $metafile = $fname.'.meta';
 4860:             my $metaresult = &removeuserfile($docuname,$docudom,$metafile); 
 4861: 	    my $url = "/uploaded/$docudom/$docuname/$fname";
 4862:             my ($file,$group) = (&parse_portfolio_url($url))[3,4];	   
 4863:             my $sqlresult = 
 4864:                 &update_portfolio_table($docuname,$docudom,$file,
 4865:                                         'portfolio_metadata',$group,
 4866:                                         'delete');
 4867:         }
 4868:     }
 4869:     return $result;
 4870: }
 4871: 
 4872: sub mkdiruserfile {
 4873:     my ($docuname,$docudom,$dir)=@_;
 4874:     my $home=&homeserver($docuname,$docudom);
 4875:     return &reply("mkdiruserfile:".&escape("$docudom/$docuname/$dir"),$home);
 4876: }
 4877: 
 4878: sub renameuserfile {
 4879:     my ($docuname,$docudom,$old,$new)=@_;
 4880:     my $home=&homeserver($docuname,$docudom);
 4881:     my $result = &reply("renameuserfile:$docudom:$docuname:".
 4882:                         &escape("$old").':'.&escape("$new"),$home);
 4883:     if ($result eq 'ok') {
 4884:         if (($old !~ /\.meta$/) && (&is_portfolio_file($old))) {
 4885:             my $oldmeta = $old.'.meta';
 4886:             my $newmeta = $new.'.meta';
 4887:             my $metaresult = 
 4888:                 &renameuserfile($docuname,$docudom,$oldmeta,$newmeta);
 4889: 	    my $url = "/uploaded/$docudom/$docuname/$old";
 4890:             my ($file,$group) = (&parse_portfolio_url($url))[3,4];
 4891:             my $sqlresult = 
 4892:                 &update_portfolio_table($docuname,$docudom,$file,
 4893:                                         'portfolio_metadata',$group,
 4894:                                         'delete');
 4895:         }
 4896:     }
 4897:     return $result;
 4898: }
 4899: 
 4900: # ------------------------------------------------------------------------- Log
 4901: 
 4902: sub log {
 4903:     my ($dom,$nam,$hom,$what)=@_;
 4904:     return critical("log:$dom:$nam:$what",$hom);
 4905: }
 4906: 
 4907: # ------------------------------------------------------------------ Course Log
 4908: #
 4909: # This routine flushes several buffers of non-mission-critical nature
 4910: #
 4911: 
 4912: sub flushcourselogs {
 4913:     &logthis('Flushing log buffers');
 4914: #
 4915: # course logs
 4916: # This is a log of all transactions in a course, which can be used
 4917: # for data mining purposes
 4918: #
 4919: # It also collects the courseid database, which lists last transaction
 4920: # times and course titles for all courseids
 4921: #
 4922:     my %courseidbuffer=();
 4923:     foreach my $crsid (keys(%courselogs)) {
 4924:         if (&reply('log:'.$coursedombuf{$crsid}.':'.$coursenumbuf{$crsid}.':'.
 4925: 		          &escape($courselogs{$crsid}),
 4926: 		          $coursehombuf{$crsid}) eq 'ok') {
 4927: 	    delete $courselogs{$crsid};
 4928:         } else {
 4929:             &logthis('Failed to flush log buffer for '.$crsid);
 4930:             if (length($courselogs{$crsid})>40000) {
 4931:                &logthis("<font color=\"blue\">WARNING: Buffer for ".$crsid.
 4932:                         " exceeded maximum size, deleting.</font>");
 4933:                delete $courselogs{$crsid};
 4934:             }
 4935:         }
 4936:         $courseidbuffer{$coursehombuf{$crsid}}{$crsid} = {
 4937:             'description' => $coursedescrbuf{$crsid},
 4938:             'inst_code'    => $courseinstcodebuf{$crsid},
 4939:             'type'        => $coursetypebuf{$crsid},
 4940:             'owner'       => $courseownerbuf{$crsid},
 4941:         };
 4942:     }
 4943: #
 4944: # Write course id database (reverse lookup) to homeserver of courses 
 4945: # Is used in pickcourse
 4946: #
 4947:     foreach my $crs_home (keys(%courseidbuffer)) {
 4948:         my $response = &courseidput(&host_domain($crs_home),
 4949:                                     $courseidbuffer{$crs_home},
 4950:                                     $crs_home,'timeonly');
 4951:     }
 4952: #
 4953: # File accesses
 4954: # Writes to the dynamic metadata of resources to get hit counts, etc.
 4955: #
 4956:     foreach my $entry (keys(%accesshash)) {
 4957:         if ($entry =~ /___count$/) {
 4958:             my ($dom,$name);
 4959:             ($dom,$name,undef)=
 4960: 		($entry=~m{___($match_domain)/($match_name)/(.*)___count$});
 4961:             if (! defined($dom) || $dom eq '' || 
 4962:                 ! defined($name) || $name eq '') {
 4963:                 my $cid = $env{'request.course.id'};
 4964:                 $dom  = $env{'request.'.$cid.'.domain'};
 4965:                 $name = $env{'request.'.$cid.'.num'};
 4966:             }
 4967:             my $value = $accesshash{$entry};
 4968:             my (undef,$url,undef) = ($entry =~ /^(.*)___(.*)___count$/);
 4969:             my %temphash=($url => $value);
 4970:             my $result = &inc('nohist_accesscount',\%temphash,$dom,$name);
 4971:             if ($result eq 'ok') {
 4972:                 delete $accesshash{$entry};
 4973:             }
 4974:         } else {
 4975:             my ($dom,$name) = ($entry=~m{___($match_domain)/($match_name)/(.*)___(\w+)$});
 4976:             if (($dom eq 'uploaded') || ($dom eq 'adm')) { next; }
 4977:             my %temphash=($entry => $accesshash{$entry});
 4978:             if (&put('nohist_resevaldata',\%temphash,$dom,$name) eq 'ok') {
 4979:                 delete $accesshash{$entry};
 4980:             }
 4981:         }
 4982:     }
 4983: #
 4984: # Roles
 4985: # Reverse lookup of user roles for course faculty/staff and co-authorship
 4986: #
 4987:     foreach my $entry (keys(%userrolehash)) {
 4988:         my ($role,$uname,$udom,$runame,$rudom,$rsec)=
 4989: 	    split(/\:/,$entry);
 4990:         if (&Apache::lonnet::put('nohist_userroles',
 4991:              { $role.':'.$uname.':'.$udom.':'.$rsec => $userrolehash{$entry} },
 4992:                 $rudom,$runame) eq 'ok') {
 4993: 	    delete $userrolehash{$entry};
 4994:         }
 4995:     }
 4996: #
 4997: # Reverse lookup of domain roles (dc, ad, li, sc, dh, da, au)
 4998: #
 4999:     my %domrolebuffer = ();
 5000:     foreach my $entry (keys(%domainrolehash)) {
 5001:         my ($role,$uname,$udom,$runame,$rudom,$rsec)=split(/:/,$entry);
 5002:         if ($domrolebuffer{$rudom}) {
 5003:             $domrolebuffer{$rudom}.='&'.&escape($entry).
 5004:                       '='.&escape($domainrolehash{$entry});
 5005:         } else {
 5006:             $domrolebuffer{$rudom}.=&escape($entry).
 5007:                       '='.&escape($domainrolehash{$entry});
 5008:         }
 5009:         delete $domainrolehash{$entry};
 5010:     }
 5011:     foreach my $dom (keys(%domrolebuffer)) {
 5012: 	my %servers;
 5013: 	if (defined(&domain($dom,'primary'))) {
 5014: 	    my $primary=&domain($dom,'primary');
 5015: 	    my $hostname=&hostname($primary);
 5016: 	    $servers{$primary} = $hostname;
 5017: 	} else { 
 5018: 	    %servers = &get_servers($dom,'library');
 5019: 	}
 5020: 	foreach my $tryserver (keys(%servers)) {
 5021: 	    if (&reply('domroleput:'.$dom.':'.
 5022: 		       $domrolebuffer{$dom},$tryserver) eq 'ok') {
 5023: 		last;
 5024: 	    } else {  
 5025: 		&logthis('Put of domain roles failed for '.$dom.' and  '.$tryserver);
 5026: 	    }
 5027:         }
 5028:     }
 5029:     $dumpcount++;
 5030: }
 5031: 
 5032: sub courselog {
 5033:     my $what=shift;
 5034:     $what=time.':'.$what;
 5035:     unless ($env{'request.course.id'}) { return ''; }
 5036:     $coursedombuf{$env{'request.course.id'}}=
 5037:        $env{'course.'.$env{'request.course.id'}.'.domain'};
 5038:     $coursenumbuf{$env{'request.course.id'}}=
 5039:        $env{'course.'.$env{'request.course.id'}.'.num'};
 5040:     $coursehombuf{$env{'request.course.id'}}=
 5041:        $env{'course.'.$env{'request.course.id'}.'.home'};
 5042:     $coursedescrbuf{$env{'request.course.id'}}=
 5043:        $env{'course.'.$env{'request.course.id'}.'.description'};
 5044:     $courseinstcodebuf{$env{'request.course.id'}}=
 5045:        $env{'course.'.$env{'request.course.id'}.'.internal.coursecode'};
 5046:     $courseownerbuf{$env{'request.course.id'}}=
 5047:        $env{'course.'.$env{'request.course.id'}.'.internal.courseowner'};
 5048:     $coursetypebuf{$env{'request.course.id'}}=
 5049:        $env{'course.'.$env{'request.course.id'}.'.type'};
 5050:     if (defined $courselogs{$env{'request.course.id'}}) {
 5051: 	$courselogs{$env{'request.course.id'}}.='&'.$what;
 5052:     } else {
 5053: 	$courselogs{$env{'request.course.id'}}.=$what;
 5054:     }
 5055:     if (length($courselogs{$env{'request.course.id'}})>4048) {
 5056: 	&flushcourselogs();
 5057:     }
 5058: }
 5059: 
 5060: sub courseacclog {
 5061:     my $fnsymb=shift;
 5062:     unless ($env{'request.course.id'}) { return ''; }
 5063:     my $what=$fnsymb.':'.$env{'user.name'}.':'.$env{'user.domain'};
 5064:     if ($fnsymb=~/$LONCAPA::assess_re/) {
 5065:         $what.=':POST';
 5066:         # FIXME: Probably ought to escape things....
 5067: 	foreach my $key (keys(%env)) {
 5068:             if ($key=~/^form\.(.*)/) {
 5069:                 my $formitem = $1;
 5070:                 if ($formitem =~ /^HWFILE(?:SIZE|TOOBIG)/) {
 5071:                     $what.=':'.$formitem.'='.$env{$key};
 5072:                 } elsif ($formitem !~ /^HWFILE(?:[^.]+)$/) {
 5073:                     if ($formitem eq 'proctorpassword') {
 5074:                         $what.=':'.$formitem.'=' . '*' x length($env{$key});
 5075:                     } else {
 5076:                         $what.=':'.$formitem.'='.$env{$key};
 5077:                     }
 5078:                 }
 5079:             }
 5080:         }
 5081:     } elsif ($fnsymb =~ m:^/adm/searchcat:) {
 5082:         # FIXME: We should not be depending on a form parameter that someone
 5083:         # editing lonsearchcat.pm might change in the future.
 5084:         if ($env{'form.phase'} eq 'course_search') {
 5085:             $what.= ':POST';
 5086:             # FIXME: Probably ought to escape things....
 5087:             foreach my $element ('courseexp','crsfulltext','crsrelated',
 5088:                                  'crsdiscuss') {
 5089:                 $what.=':'.$element.'='.$env{'form.'.$element};
 5090:             }
 5091:         }
 5092:     }
 5093:     &courselog($what);
 5094: }
 5095: 
 5096: sub countacc {
 5097:     my $url=&declutter(shift);
 5098:     return if (! defined($url) || $url eq '');
 5099:     unless ($env{'request.course.id'}) { return ''; }
 5100: #
 5101: # Mark that this url was used in this course
 5102: #
 5103:     $accesshash{$env{'request.course.id'}.'___'.$url.'___course'}=1;
 5104: #
 5105: # Increase the access count for this resource in this child process
 5106: #
 5107:     my $key=$$.$processmarker.'_'.$dumpcount.'___'.$url.'___count';
 5108:     $accesshash{$key}++;
 5109: }
 5110: 
 5111: sub linklog {
 5112:     my ($from,$to)=@_;
 5113:     $from=&declutter($from);
 5114:     $to=&declutter($to);
 5115:     $accesshash{$from.'___'.$to.'___comefrom'}=1;
 5116:     $accesshash{$to.'___'.$from.'___goto'}=1;
 5117: }
 5118: 
 5119: sub statslog {
 5120:     my ($symb,$part,$users,$av_attempts,$degdiff)=@_;
 5121:     if ($users<2) { return; }
 5122:     my %dynstore=&LONCAPA::lonmetadata::dynamic_metadata_storage({
 5123:             'course'       => $env{'request.course.id'},
 5124:             'sections'     => '"all"',
 5125:             'num_students' => $users,
 5126:             'part'         => $part,
 5127:             'symb'         => $symb,
 5128:             'mean_tries'   => $av_attempts,
 5129:             'deg_of_diff'  => $degdiff});
 5130:     foreach my $key (keys(%dynstore)) {
 5131:         $accesshash{$key}=$dynstore{$key};
 5132:     }
 5133: }
 5134:   
 5135: sub userrolelog {
 5136:     my ($trole,$username,$domain,$area,$tstart,$tend)=@_;
 5137:     if ( $trole =~ /^(ca|aa|in|cc|ep|cr|ta|co)/ ) {
 5138:        my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
 5139:        $userrolehash
 5140:          {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
 5141:                     =$tend.':'.$tstart;
 5142:     }
 5143:     if ($env{'request.role'} =~ /dc\./ && $trole =~ /^(au|in|cc|ep|cr|ta|co)/) {
 5144:        $userrolehash
 5145:          {$trole.':'.$username.':'.$domain.':'.$env{'user.name'}.':'.$env{'user.domain'}.':'}
 5146:                     =$tend.':'.$tstart;
 5147:     }
 5148:     if ($trole =~ /^(dc|ad|li|au|dg|sc|dh|da)/ ) {
 5149:        my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
 5150:        $domainrolehash
 5151:          {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
 5152:                     = $tend.':'.$tstart;
 5153:     }
 5154: }
 5155: 
 5156: sub courserolelog {
 5157:     my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$selfenroll,$context)=@_;
 5158:     if ($area =~ m-^/($match_domain)/($match_courseid)/?([^/]*)-) {
 5159:         my $cdom = $1;
 5160:         my $cnum = $2;
 5161:         my $sec = $3;
 5162:         my $namespace = 'rolelog';
 5163:         my %storehash = (
 5164:                            role    => $trole,
 5165:                            start   => $tstart,
 5166:                            end     => $tend,
 5167:                            selfenroll => $selfenroll,
 5168:                            context    => $context,
 5169:                         );
 5170:         if ($trole eq 'gr') {
 5171:             $namespace = 'groupslog';
 5172:             $storehash{'group'} = $sec;
 5173:         } else {
 5174:             $storehash{'section'} = $sec;
 5175:         }
 5176:         &write_log('course',$namespace,\%storehash,$delflag,$username,
 5177:                    $domain,$cnum,$cdom);
 5178:         if (($trole ne 'st') || ($sec ne '')) {
 5179:             &devalidate_cache_new('getcourseroles',$cdom.'_'.$cnum);
 5180:         }
 5181:     }
 5182:     return;
 5183: }
 5184: 
 5185: sub domainrolelog {
 5186:     my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$context)=@_;
 5187:     if ($area =~ m{^/($match_domain)/$}) {
 5188:         my $cdom = $1;
 5189:         my $domconfiguser = &Apache::lonnet::get_domainconfiguser($cdom);
 5190:         my $namespace = 'rolelog';
 5191:         my %storehash = (
 5192:                            role    => $trole,
 5193:                            start   => $tstart,
 5194:                            end     => $tend,
 5195:                            context => $context,
 5196:                         );
 5197:         &write_log('domain',$namespace,\%storehash,$delflag,$username,
 5198:                    $domain,$domconfiguser,$cdom);
 5199:     }
 5200:     return;
 5201: 
 5202: }
 5203: 
 5204: sub coauthorrolelog {
 5205:     my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$context)=@_;
 5206:     if ($area =~ m{^/($match_domain)/($match_username)$}) {
 5207:         my $audom = $1;
 5208:         my $auname = $2;
 5209:         my $namespace = 'rolelog';
 5210:         my %storehash = (
 5211:                            role    => $trole,
 5212:                            start   => $tstart,
 5213:                            end     => $tend,
 5214:                            context => $context,
 5215:                         );
 5216:         &write_log('author',$namespace,\%storehash,$delflag,$username,
 5217:                    $domain,$auname,$audom);
 5218:     }
 5219:     return;
 5220: }
 5221: 
 5222: sub get_course_adv_roles {
 5223:     my ($cid,$codes) = @_;
 5224:     $cid=$env{'request.course.id'} unless (defined($cid));
 5225:     my %coursehash=&coursedescription($cid);
 5226:     my $crstype = &Apache::loncommon::course_type($cid);
 5227:     my %nothide=();
 5228:     foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
 5229:         if ($user !~ /:/) {
 5230: 	    $nothide{join(':',split(/[\@]/,$user))}=1;
 5231:         } else {
 5232:             $nothide{$user}=1;
 5233:         }
 5234:     }
 5235:     my @possdoms = ($coursehash{'domain'});
 5236:     if ($coursehash{'checkforpriv'}) {
 5237:         push(@possdoms,split(/,/,$coursehash{'checkforpriv'}));
 5238:     }
 5239:     my %returnhash=();
 5240:     my %dumphash=
 5241:             &dump('nohist_userroles',$coursehash{'domain'},$coursehash{'num'});
 5242:     my $now=time;
 5243:     my %privileged;
 5244:     foreach my $entry (keys(%dumphash)) {
 5245: 	my ($tend,$tstart)=split(/\:/,$dumphash{$entry});
 5246:         if (($tstart) && ($tstart<0)) { next; }
 5247:         if (($tend) && ($tend<$now)) { next; }
 5248:         if (($tstart) && ($now<$tstart)) { next; }
 5249:         my ($role,$username,$domain,$section)=split(/\:/,$entry);
 5250: 	if ($username eq '' || $domain eq '') { next; }
 5251:         if ((&privileged($username,$domain,\@possdoms)) &&
 5252:             (!$nothide{$username.':'.$domain})) { next; }
 5253: 	if ($role eq 'cr') { next; }
 5254:         if ($codes) {
 5255:             if ($section) { $role .= ':'.$section; }
 5256:             if ($returnhash{$role}) {
 5257:                 $returnhash{$role}.=','.$username.':'.$domain;
 5258:             } else {
 5259:                 $returnhash{$role}=$username.':'.$domain;
 5260:             }
 5261:         } else {
 5262:             my $key=&plaintext($role,$crstype);
 5263:             if ($section) { $key.=' ('.&Apache::lonlocal::mt('Section [_1]',$section).')'; }
 5264:             if ($returnhash{$key}) {
 5265: 	        $returnhash{$key}.=','.$username.':'.$domain;
 5266:             } else {
 5267:                 $returnhash{$key}=$username.':'.$domain;
 5268:             }
 5269:         }
 5270:     }
 5271:     return %returnhash;
 5272: }
 5273: 
 5274: sub get_my_roles {
 5275:     my ($uname,$udom,$context,$types,$roles,$roledoms,$withsec,$hidepriv)=@_;
 5276:     unless (defined($uname)) { $uname=$env{'user.name'}; }
 5277:     unless (defined($udom)) { $udom=$env{'user.domain'}; }
 5278:     my (%dumphash,%nothide);
 5279:     if ($context eq 'userroles') {
 5280:         %dumphash = &dump('roles',$udom,$uname);
 5281:     } else {
 5282:         %dumphash = &dump('nohist_userroles',$udom,$uname);
 5283:         if ($hidepriv) {
 5284:             my %coursehash=&coursedescription($udom.'_'.$uname);
 5285:             foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
 5286:                 if ($user !~ /:/) {
 5287:                     $nothide{join(':',split(/[\@]/,$user))} = 1;
 5288:                 } else {
 5289:                     $nothide{$user} = 1;
 5290:                 }
 5291:             }
 5292:         }
 5293:     }
 5294:     my %returnhash=();
 5295:     my $now=time;
 5296:     my %privileged;
 5297:     foreach my $entry (keys(%dumphash)) {
 5298:         my ($role,$tend,$tstart);
 5299:         if ($context eq 'userroles') {
 5300:             next if ($entry =~ /^rolesdef/);
 5301: 	    ($role,$tend,$tstart)=split(/_/,$dumphash{$entry});
 5302:         } else {
 5303:             ($tend,$tstart)=split(/\:/,$dumphash{$entry});
 5304:         }
 5305:         if (($tstart) && ($tstart<0)) { next; }
 5306:         my $status = 'active';
 5307:         if (($tend) && ($tend<=$now)) {
 5308:             $status = 'previous';
 5309:         } 
 5310:         if (($tstart) && ($now<$tstart)) {
 5311:             $status = 'future';
 5312:         }
 5313:         if (ref($types) eq 'ARRAY') {
 5314:             if (!grep(/^\Q$status\E$/,@{$types})) {
 5315:                 next;
 5316:             } 
 5317:         } else {
 5318:             if ($status ne 'active') {
 5319:                 next;
 5320:             }
 5321:         }
 5322:         my ($rolecode,$username,$domain,$section,$area);
 5323:         if ($context eq 'userroles') {
 5324:             ($area,$rolecode) = ($entry =~ /^(.+)_([^_]+)$/);
 5325:             (undef,$domain,$username,$section) = split(/\//,$area);
 5326:         } else {
 5327:             ($role,$username,$domain,$section) = split(/\:/,$entry);
 5328:         }
 5329:         if (ref($roledoms) eq 'ARRAY') {
 5330:             if (!grep(/^\Q$domain\E$/,@{$roledoms})) {
 5331:                 next;
 5332:             }
 5333:         }
 5334:         if (ref($roles) eq 'ARRAY') {
 5335:             if (!grep(/^\Q$role\E$/,@{$roles})) {
 5336:                 if ($role =~ /^cr\//) {
 5337:                     if (!grep(/^cr$/,@{$roles})) {
 5338:                         next;
 5339:                     }
 5340:                 } elsif ($role =~ /^gr\//) {
 5341:                     if (!grep(/^gr$/,@{$roles})) {
 5342:                         next;
 5343:                     }
 5344:                 } else {
 5345:                     next;
 5346:                 }
 5347:             }
 5348:         }
 5349:         if ($hidepriv) {
 5350:             my @privroles = ('dc','su');
 5351:             if ($context eq 'userroles') {
 5352:                 next if (grep(/^\Q$role\E$/,@privroles));
 5353:             } else {
 5354:                 my $possdoms = [$domain];
 5355:                 if (ref($roledoms) eq 'ARRAY') {
 5356:                    push(@{$possdoms},@{$roledoms}); 
 5357:                 }
 5358:                 if (&privileged($username,$domain,$possdoms,\@privroles)) {
 5359:                     if (!$nothide{$username.':'.$domain}) {
 5360:                         next;
 5361:                     }
 5362:                 }
 5363:             }
 5364:         }
 5365:         if ($withsec) {
 5366:             $returnhash{$username.':'.$domain.':'.$role.':'.$section} =
 5367:                 $tstart.':'.$tend;
 5368:         } else {
 5369:             $returnhash{$username.':'.$domain.':'.$role}=$tstart.':'.$tend;
 5370:         }
 5371:     }
 5372:     return %returnhash;
 5373: }
 5374: 
 5375: sub get_all_adhocroles {
 5376:     my ($dom) = @_;
 5377:     my @roles_by_num = ();
 5378:     my %domdefaults = &get_domain_defaults($dom);
 5379:     my (%description,%access_in_dom,%access_info);
 5380:     if (ref($domdefaults{'adhocroles'}) eq 'HASH') {
 5381:         my $count = 0;
 5382:         my %domcurrent = %{$domdefaults{'adhocroles'}};
 5383:         my %ordered;
 5384:         foreach my $role (sort(keys(%domcurrent))) {
 5385:             my ($order,$desc,$access_in_dom);
 5386:             if (ref($domcurrent{$role}) eq 'HASH') {
 5387:                 $order = $domcurrent{$role}{'order'};
 5388:                 $desc = $domcurrent{$role}{'desc'};
 5389:                 $access_in_dom{$role} = $domcurrent{$role}{'access'};
 5390:                 $access_info{$role} = $domcurrent{$role}{$access_in_dom{$role}};
 5391:             }
 5392:             if ($order eq '') {
 5393:                 $order = $count;
 5394:             }
 5395:             $ordered{$order} = $role;
 5396:             if ($desc ne '') {
 5397:                 $description{$role} = $desc;
 5398:             } else {
 5399:                 $description{$role}= $role;
 5400:             }
 5401:             $count++;
 5402:         }
 5403:         foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 5404:             push(@roles_by_num,$ordered{$item});
 5405:         }
 5406:     }
 5407:     return (\@roles_by_num,\%description,\%access_in_dom,\%access_info);
 5408: }
 5409: 
 5410: sub get_my_adhocroles {
 5411:     my ($cid,$checkreg) = @_;
 5412:     my ($cdom,$cnum,%info,@possroles,$description,$roles_by_num);
 5413:     if ($env{'request.course.id'} eq $cid) {
 5414:         $cdom = $env{'course.'.$cid.'.domain'};
 5415:         $cnum = $env{'course.'.$cid.'.num'};
 5416:         $info{'internal.coursecode'} = $env{'course.'.$cid.'.internal.coursecode'};
 5417:     } elsif ($cid =~ /^($match_domain)_($match_courseid)$/) {
 5418:         $cdom = $1;
 5419:         $cnum = $2;
 5420:         %info = &Apache::lonnet::get('environment',['internal.coursecode'],
 5421:                                      $cdom,$cnum);
 5422:     }
 5423:     if (($info{'internal.coursecode'} ne '') && ($checkreg)) {
 5424:         my $user = $env{'user.name'}.':'.$env{'user.domain'};
 5425:         my %rosterhash = &get('classlist',[$user],$cdom,$cnum);
 5426:         if ($rosterhash{$user} ne '') {
 5427:             my $type = (split(/:/,$rosterhash{$user}))[5];
 5428:             return ([],{}) if ($type eq 'auto');
 5429:         }
 5430:     }
 5431:     if (($cdom ne '') && ($cnum ne ''))  {
 5432:         if (($env{"user.role.dh./$cdom/"}) || ($env{"user.role.da./$cdom/"})) {
 5433:             my $then=$env{'user.login.time'};
 5434:             my $update=$env{'user.update.time'};
 5435:             if (!$update) {
 5436:                 $update = $then;
 5437:             }
 5438:             my @liveroles;
 5439:             foreach my $role ('dh','da') {
 5440:                 if ($env{"user.role.$role./$cdom/"}) {
 5441:                     my ($tstart,$tend)=split(/\./,$env{"user.role.$role./$cdom/"});
 5442:                     my $limit = $update;
 5443:                     if ($env{'request.role'} eq "$role./$cdom/") {
 5444:                         $limit = $then;
 5445:                     }
 5446:                     my $activerole = 1;
 5447:                     if ($tstart && $tstart>$limit) { $activerole = 0; }
 5448:                     if ($tend   && $tend  <$limit) { $activerole = 0; }
 5449:                     if ($activerole) {
 5450:                         push(@liveroles,$role);
 5451:                     }
 5452:                 }
 5453:             }
 5454:             if (@liveroles) {
 5455:                 if (&homeserver($cnum,$cdom) ne 'no_host') {
 5456:                     my ($accessref,$accessinfo,%access_in_dom);
 5457:                     ($roles_by_num,$description,$accessref,$accessinfo) = &get_all_adhocroles($cdom);
 5458:                     if (ref($roles_by_num) eq 'ARRAY') {
 5459:                         if (@{$roles_by_num}) {
 5460:                             my %settings;
 5461:                             if ($env{'request.course.id'} eq $cid) {
 5462:                                 foreach my $envkey (keys(%env)) {
 5463:                                     if ($envkey =~ /^\Qcourse.$cid.\E(internal\.adhoc.+)$/) {
 5464:                                         $settings{$1} = $env{$envkey};
 5465:                                     }
 5466:                                 }
 5467:                             } else {
 5468:                                 %settings = &dump('environment',$cdom,$cnum,'internal\.adhoc');
 5469:                             }
 5470:                             my %setincrs;
 5471:                             if ($settings{'internal.adhocaccess'}) {
 5472:                                 map { $setincrs{$_} = 1; } split(/,/,$settings{'internal.adhocaccess'});
 5473:                             }
 5474:                             my @statuses;
 5475:                             if ($env{'environment.inststatus'}) {
 5476:                                 @statuses = split(/,/,$env{'environment.inststatus'});
 5477:                             }
 5478:                             my $user = $env{'user.name'}.':'.$env{'user.domain'};
 5479:                             if (ref($accessref) eq 'HASH') {
 5480:                                 %access_in_dom = %{$accessref};
 5481:                             }
 5482:                             foreach my $role (@{$roles_by_num}) {
 5483:                                 my ($curraccess,@okstatus,@personnel);
 5484:                                 if ($setincrs{$role}) {
 5485:                                     ($curraccess,my $rest) = split(/=/,$settings{'internal.adhoc.'.$role});
 5486:                                     if ($curraccess eq 'status') {
 5487:                                         @okstatus = split(/\&/,$rest);
 5488:                                     } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
 5489:                                         @personnel = split(/\&/,$rest);
 5490:                                     }
 5491:                                 } else {
 5492:                                     $curraccess = $access_in_dom{$role};
 5493:                                     if (ref($accessinfo) eq 'HASH') {
 5494:                                         if ($curraccess eq 'status') {
 5495:                                             if (ref($accessinfo->{$role}) eq 'ARRAY') {
 5496:                                                 @okstatus = @{$accessinfo->{$role}};
 5497:                                             }
 5498:                                         } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
 5499:                                             if (ref($accessinfo->{$role}) eq 'ARRAY') {
 5500:                                                 @personnel = @{$accessinfo->{$role}};
 5501:                                             }
 5502:                                         }
 5503:                                     }
 5504:                                 }
 5505:                                 if ($curraccess eq 'none') {
 5506:                                     next;
 5507:                                 } elsif ($curraccess eq 'all') {
 5508:                                     push(@possroles,$role);
 5509:                                 } elsif ($curraccess eq 'dh') {
 5510:                                     if (grep(/^dh$/,@liveroles)) {
 5511:                                         push(@possroles,$role);
 5512:                                     } else {
 5513:                                         next;
 5514:                                     }
 5515:                                 } elsif ($curraccess eq 'da') {
 5516:                                     if (grep(/^da$/,@liveroles)) {
 5517:                                         push(@possroles,$role);
 5518:                                     } else {
 5519:                                         next;
 5520:                                     }
 5521:                                 } elsif ($curraccess eq 'status') {
 5522:                                     if (@okstatus) {
 5523:                                         if (!@statuses) {
 5524:                                             if (grep(/^default$/,@okstatus)) {
 5525:                                                 push(@possroles,$role);
 5526:                                             }
 5527:                                         } else {
 5528:                                             foreach my $status (@okstatus) {
 5529:                                                 if (grep(/^\Q$status\E$/,@statuses)) {
 5530:                                                     push(@possroles,$role);
 5531:                                                     last;
 5532:                                                 }
 5533:                                             }
 5534:                                         }
 5535:                                     }
 5536:                                 } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
 5537:                                     if (grep(/^\Q$user\E$/,@personnel)) {
 5538:                                         if ($curraccess eq 'exc') {
 5539:                                             push(@possroles,$role);
 5540:                                         }
 5541:                                     } elsif ($curraccess eq 'inc') {
 5542:                                         push(@possroles,$role);
 5543:                                     }
 5544:                                 }
 5545:                             }
 5546:                         }
 5547:                     }
 5548:                 }
 5549:             }
 5550:         }
 5551:     }
 5552:     unless (ref($description) eq 'HASH') {
 5553:         if (ref($roles_by_num) eq 'ARRAY') {
 5554:             my %desc;
 5555:             map { $desc{$_} = $_; } (@{$roles_by_num});
 5556:             $description = \%desc;
 5557:         } else {
 5558:             $description = {};
 5559:         }
 5560:     }
 5561:     return (\@possroles,$description);
 5562: }
 5563: 
 5564: # ----------------------------------------------------- Frontpage Announcements
 5565: #
 5566: #
 5567: 
 5568: sub postannounce {
 5569:     my ($server,$text)=@_;
 5570:     unless (&allowed('psa',&host_domain($server))) { return 'refused'; }
 5571:     unless ($text=~/\w/) { $text=''; }
 5572:     return &reply('setannounce:'.&escape($text),$server);
 5573: }
 5574: 
 5575: sub getannounce {
 5576: 
 5577:     if (open(my $fh,"<",$perlvar{'lonDocRoot'}.'/announcement.txt')) {
 5578: 	my $announcement='';
 5579: 	while (my $line = <$fh>) { $announcement .= $line; }
 5580: 	close($fh);
 5581: 	if ($announcement=~/\w/) { 
 5582: 	    return 
 5583:    '<table bgcolor="#FF5555" cellpadding="5" cellspacing="3">'.
 5584:    '<tr><td bgcolor="#FFFFFF"><tt>'.$announcement.'</tt></td></tr></table>'; 
 5585: 	} else {
 5586: 	    return '';
 5587: 	}
 5588:     } else {
 5589: 	return '';
 5590:     }
 5591: }
 5592: 
 5593: # ---------------------------------------------------------- Course ID routines
 5594: # Deal with domain's nohist_courseid.db files
 5595: #
 5596: 
 5597: sub courseidput {
 5598:     my ($domain,$storehash,$coursehome,$caller) = @_;
 5599:     return unless (ref($storehash) eq 'HASH');
 5600:     my $outcome;
 5601:     if ($caller eq 'timeonly') {
 5602:         my $cids = '';
 5603:         foreach my $item (keys(%$storehash)) {
 5604:             $cids.=&escape($item).'&';
 5605:         }
 5606:         $cids=~s/\&$//;
 5607:         $outcome = &reply('courseidputhash:'.$domain.':'.$caller.':'.$cids,
 5608:                           $coursehome);       
 5609:     } else {
 5610:         my $items = '';
 5611:         foreach my $item (keys(%$storehash)) {
 5612:             $items.= &escape($item).'='.
 5613:                      &freeze_escape($$storehash{$item}).'&';
 5614:         }
 5615:         $items=~s/\&$//;
 5616:         $outcome = &reply('courseidputhash:'.$domain.':'.$caller.':'.$items,
 5617:                           $coursehome);
 5618:     }
 5619:     if ($outcome eq 'unknown_cmd') {
 5620:         my $what;
 5621:         foreach my $cid (keys(%$storehash)) {
 5622:             $what .= &escape($cid).'=';
 5623:             foreach my $item ('description','inst_code','owner','type') {
 5624:                 $what .= &escape($storehash->{$cid}{$item}).':';
 5625:             }
 5626:             $what =~ s/\:$/&/;
 5627:         }
 5628:         $what =~ s/\&$//;  
 5629:         return &reply('courseidput:'.$domain.':'.$what,$coursehome);
 5630:     } else {
 5631:         return $outcome;
 5632:     }
 5633: }
 5634: 
 5635: sub courseiddump {
 5636:     my ($domfilter,$descfilter,$sincefilter,$instcodefilter,$ownerfilter,
 5637:         $coursefilter,$hostidflag,$hostidref,$typefilter,$regexp_ok,
 5638:         $selfenrollonly,$catfilter,$showhidden,$caller,$cloner,$cc_clone,
 5639:         $cloneonly,$createdbefore,$createdafter,$creationcontext,$domcloner,
 5640:         $hasuniquecode,$reqcrsdom,$reqinstcode)=@_;
 5641:     my $as_hash = 1;
 5642:     my %returnhash;
 5643:     if (!$domfilter) { $domfilter=''; }
 5644:     my %libserv = &all_library();
 5645:     foreach my $tryserver (keys(%libserv)) {
 5646:         if ( (  $hostidflag == 1 
 5647: 	        && grep(/^\Q$tryserver\E$/,@{$hostidref}) ) 
 5648: 	     || (!defined($hostidflag)) ) {
 5649: 
 5650: 	    if (($domfilter eq '') ||
 5651: 		(&host_domain($tryserver) eq $domfilter)) {
 5652:                 my $rep;
 5653:                 if (grep { $_ eq $tryserver } current_machine_ids()) {
 5654:                     $rep = LONCAPA::Lond::dump_course_id_handler(
 5655:                         join(":", (&host_domain($tryserver), $sincefilter, 
 5656:                                 &escape($descfilter), &escape($instcodefilter), 
 5657:                                 &escape($ownerfilter), &escape($coursefilter),
 5658:                                 &escape($typefilter), &escape($regexp_ok), 
 5659:                                 $as_hash, &escape($selfenrollonly), 
 5660:                                 &escape($catfilter), $showhidden, $caller, 
 5661:                                 &escape($cloner), &escape($cc_clone), $cloneonly, 
 5662:                                 &escape($createdbefore), &escape($createdafter), 
 5663:                                 &escape($creationcontext),$domcloner,$hasuniquecode,
 5664:                                 $reqcrsdom,&escape($reqinstcode))));
 5665:                 } else {
 5666:                     $rep = &reply('courseiddump:'.&host_domain($tryserver).':'.
 5667:                              $sincefilter.':'.&escape($descfilter).':'.
 5668:                              &escape($instcodefilter).':'.&escape($ownerfilter).
 5669:                              ':'.&escape($coursefilter).':'.&escape($typefilter).
 5670:                              ':'.&escape($regexp_ok).':'.$as_hash.':'.
 5671:                              &escape($selfenrollonly).':'.&escape($catfilter).':'.
 5672:                              $showhidden.':'.$caller.':'.&escape($cloner).':'.
 5673:                              &escape($cc_clone).':'.$cloneonly.':'.
 5674:                              &escape($createdbefore).':'.&escape($createdafter).':'.
 5675:                              &escape($creationcontext).':'.$domcloner.':'.$hasuniquecode.
 5676:                              ':'.$reqcrsdom.':'.&escape($reqinstcode),$tryserver);
 5677:                 }
 5678:                      
 5679:                 my @pairs=split(/\&/,$rep);
 5680:                 foreach my $item (@pairs) {
 5681:                     my ($key,$value)=split(/\=/,$item,2);
 5682:                     $key = &unescape($key);
 5683:                     next if ($key =~ /^error: 2 /);
 5684:                     my $result = &thaw_unescape($value);
 5685:                     if (ref($result) eq 'HASH') {
 5686:                         $returnhash{$key}=$result;
 5687:                     } else {
 5688:                         my @responses = split(/:/,$value);
 5689:                         my @items = ('description','inst_code','owner','type');
 5690:                         for (my $i=0; $i<@responses; $i++) {
 5691:                             $returnhash{$key}{$items[$i]} = &unescape($responses[$i]);
 5692:                         }
 5693:                     }
 5694:                 }
 5695:             }
 5696:         }
 5697:     }
 5698:     return %returnhash;
 5699: }
 5700: 
 5701: sub courselastaccess {
 5702:     my ($cdom,$cnum,$hostidref) = @_;
 5703:     my %returnhash;
 5704:     if ($cdom && $cnum) {
 5705:         my $chome = &homeserver($cnum,$cdom);
 5706:         if ($chome ne 'no_host') {
 5707:             my $rep = &reply('courselastaccess:'.$cdom.':'.$cnum,$chome);
 5708:             &extract_lastaccess(\%returnhash,$rep);
 5709:         }
 5710:     } else {
 5711:         if (!$cdom) { $cdom=''; }
 5712:         my %libserv = &all_library();
 5713:         foreach my $tryserver (keys(%libserv)) {
 5714:             if (ref($hostidref) eq 'ARRAY') {
 5715:                 next unless (grep(/^\Q$tryserver\E$/,@{$hostidref}));
 5716:             } 
 5717:             if (($cdom eq '') || (&host_domain($tryserver) eq $cdom)) {
 5718:                 my $rep = &reply('courselastaccess:'.&host_domain($tryserver).':',$tryserver);
 5719:                 &extract_lastaccess(\%returnhash,$rep);
 5720:             }
 5721:         }
 5722:     }
 5723:     return %returnhash;
 5724: }
 5725: 
 5726: sub extract_lastaccess {
 5727:     my ($returnhash,$rep) = @_;
 5728:     if (ref($returnhash) eq 'HASH') {
 5729:         unless ($rep eq 'unknown_command' || $rep eq 'no_such_host' || 
 5730:                 $rep eq 'con_lost' || $rep eq 'rejected' || $rep eq 'refused' ||
 5731:                  $rep eq '') {
 5732:             my @pairs=split(/\&/,$rep);
 5733:             foreach my $item (@pairs) {
 5734:                 my ($key,$value)=split(/\=/,$item,2);
 5735:                 $key = &unescape($key);
 5736:                 next if ($key =~ /^error: 2 /);
 5737:                 $returnhash->{$key} = &thaw_unescape($value);
 5738:             }
 5739:         }
 5740:     }
 5741:     return;
 5742: }
 5743: 
 5744: # ---------------------------------------------------------- DC e-mail
 5745: 
 5746: sub dcmailput {
 5747:     my ($domain,$msgid,$message,$server)=@_;
 5748:     my $status = &Apache::lonnet::critical(
 5749:        'dcmailput:'.$domain.':'.&escape($msgid).'='.
 5750:        &escape($message),$server);
 5751:     return $status;
 5752: }
 5753: 
 5754: sub dcmaildump {
 5755:     my ($dom,$startdate,$enddate,$senders) = @_;
 5756:     my %returnhash=();
 5757: 
 5758:     if (defined(&domain($dom,'primary'))) {
 5759:         my $cmd='dcmaildump:'.$dom.':'.&escape($startdate).':'.
 5760:                                                          &escape($enddate).':';
 5761: 	my @esc_senders=map { &escape($_)} @$senders;
 5762: 	$cmd.=&escape(join('&',@esc_senders));
 5763: 	foreach my $line (split(/\&/,&reply($cmd,&domain($dom,'primary')))) {
 5764:             my ($key,$value) = split(/\=/,$line,2);
 5765:             if (($key) && ($value)) {
 5766:                 $returnhash{&unescape($key)} = &unescape($value);
 5767:             }
 5768:         }
 5769:     }
 5770:     return %returnhash;
 5771: }
 5772: # ---------------------------------------------------------- Domain roles
 5773: 
 5774: sub get_domain_roles {
 5775:     my ($dom,$roles,$startdate,$enddate)=@_;
 5776:     if ((!defined($startdate)) || ($startdate eq '')) {
 5777:         $startdate = '.';
 5778:     }
 5779:     if ((!defined($enddate)) || ($enddate eq '')) {
 5780:         $enddate = '.';
 5781:     }
 5782:     my $rolelist;
 5783:     if (ref($roles) eq 'ARRAY') {
 5784:         $rolelist = join('&',@{$roles});
 5785:     }
 5786:     my %personnel = ();
 5787: 
 5788:     my %servers = &get_servers($dom,'library');
 5789:     foreach my $tryserver (keys(%servers)) {
 5790: 	%{$personnel{$tryserver}}=();
 5791: 	foreach my $line (split(/\&/,&reply('domrolesdump:'.$dom.':'.
 5792: 					    &escape($startdate).':'.
 5793: 					    &escape($enddate).':'.
 5794: 					    &escape($rolelist), $tryserver))) {
 5795: 	    my ($key,$value) = split(/\=/,$line,2);
 5796: 	    if (($key) && ($value)) {
 5797: 		$personnel{$tryserver}{&unescape($key)} = &unescape($value);
 5798: 	    }
 5799: 	}
 5800:     }
 5801:     return %personnel;
 5802: }
 5803: 
 5804: sub get_active_domroles {
 5805:     my ($dom,$roles) = @_;
 5806:     return () unless (ref($roles) eq 'ARRAY');
 5807:     my $now = time;
 5808:     my %dompersonnel = &get_domain_roles($dom,$roles,$now,$now);
 5809:     my %domroles;
 5810:     foreach my $server (keys(%dompersonnel)) {
 5811:         foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
 5812:             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
 5813:             $domroles{$uname.':'.$udom} = $dompersonnel{$server}{$user};
 5814:         }
 5815:     }
 5816:     return %domroles;
 5817: }
 5818: 
 5819: # ----------------------------------------------------------- Interval timing 
 5820: 
 5821: {
 5822: # Caches needed for speedup of navmaps
 5823: # We don't want to cache this for very long at all (5 seconds at most)
 5824: # 
 5825: # The user for whom we cache
 5826: my $cachedkey='';
 5827: # The cached times for this user
 5828: my %cachedtimes=();
 5829: # When this was last done
 5830: my $cachedtime='';
 5831: 
 5832: sub load_all_first_access {
 5833:     my ($uname,$udom,$ignorecache)=@_;
 5834:     if (($cachedkey eq $uname.':'.$udom) &&
 5835:         (abs($cachedtime-time)<5) && (!$env{'form.markaccess'}) &&
 5836:         (!$ignorecache)) {
 5837:         return;
 5838:     }
 5839:     $cachedtime=time;
 5840:     $cachedkey=$uname.':'.$udom;
 5841:     %cachedtimes=&dump('firstaccesstimes',$udom,$uname);
 5842: }
 5843: 
 5844: sub get_first_access {
 5845:     my ($type,$argsymb,$argmap,$ignorecache)=@_;
 5846:     my ($symb,$courseid,$udom,$uname)=&whichuser();
 5847:     if ($argsymb) { $symb=$argsymb; }
 5848:     my ($map,$id,$res)=&decode_symb($symb);
 5849:     if ($argmap) { $map = $argmap; }
 5850:     if ($type eq 'course') {
 5851: 	$res='course';
 5852:     } elsif ($type eq 'map') {
 5853: 	$res=&symbread($map);
 5854:     } else {
 5855: 	$res=$symb;
 5856:     }
 5857:     &load_all_first_access($uname,$udom,$ignorecache);
 5858:     return $cachedtimes{"$courseid\0$res"};
 5859: }
 5860: 
 5861: sub set_first_access {
 5862:     my ($type,$interval)=@_;
 5863:     my ($symb,$courseid,$udom,$uname)=&whichuser();
 5864:     my ($map,$id,$res)=&decode_symb($symb);
 5865:     if ($type eq 'course') {
 5866: 	$res='course';
 5867:     } elsif ($type eq 'map') {
 5868: 	$res=&symbread($map);
 5869:     } else {
 5870: 	$res=$symb;
 5871:     }
 5872:     $cachedkey='';
 5873:     my $firstaccess=&get_first_access($type,$symb,$map);
 5874:     if ($firstaccess) {
 5875:         &logthis("First access time already set ($firstaccess) when attempting ".
 5876:                  "to set new value (type: $type, extent: $res) for $uname:$udom ".
 5877:                  "in $courseid");
 5878:         return 'already_set';
 5879:     } else {
 5880:         my $start = time;
 5881: 	my $putres = &put('firstaccesstimes',{"$courseid\0$res"=>$start},
 5882:                           $udom,$uname);
 5883:         if ($putres eq 'ok') {
 5884:             &put('timerinterval',{"$courseid\0$res"=>$interval},
 5885:                  $udom,$uname); 
 5886:             &appenv(
 5887:                      {
 5888:                         'course.'.$courseid.'.firstaccess.'.$res   => $start,
 5889:                         'course.'.$courseid.'.timerinterval.'.$res => $interval,
 5890:                      }
 5891:                   );
 5892:             if (($cachedtime) && (abs($start-$cachedtime) < 5)) {
 5893:                 $cachedtimes{"$courseid\0$res"} = $start;
 5894:             }
 5895:         } elsif ($putres ne 'refused') {
 5896:             &logthis("Result: $putres when attempting to set first access time ".
 5897:                      "(type: $type, extent: $res) for $uname:$udom in $courseid");
 5898:         }
 5899:         return $putres;
 5900:     }
 5901:     return 'already_set';
 5902: }
 5903: }
 5904: 
 5905: # --------------------------------------------- Set Expire Date for Spreadsheet
 5906: 
 5907: sub expirespread {
 5908:     my ($uname,$udom,$stype,$usymb)=@_;
 5909:     my $cid=$env{'request.course.id'}; 
 5910:     if ($cid) {
 5911:        my $now=time;
 5912:        my $key=$uname.':'.$udom.':'.$stype.':'.$usymb;
 5913:        return &reply('put:'.$env{'course.'.$cid.'.domain'}.':'.
 5914:                             $env{'course.'.$cid.'.num'}.
 5915: 	        	    ':nohist_expirationdates:'.
 5916:                             &escape($key).'='.$now,
 5917:                             $env{'course.'.$cid.'.home'})
 5918:     }
 5919:     return 'ok';
 5920: }
 5921: 
 5922: # ----------------------------------------------------- Devalidate Spreadsheets
 5923: 
 5924: sub devalidate {
 5925:     my ($symb,$uname,$udom)=@_;
 5926:     my $cid=$env{'request.course.id'}; 
 5927:     if ($cid) {
 5928:         # delete the stored spreadsheets for
 5929:         # - the student level sheet of this user in course's homespace
 5930:         # - the assessment level sheet for this resource 
 5931:         #   for this user in user's homespace
 5932: 	# - current conditional state info
 5933: 	my $key=$uname.':'.$udom.':';
 5934:         my $status=
 5935: 	    &del('nohist_calculatedsheets',
 5936: 		 [$key.'studentcalc:'],
 5937: 		 $env{'course.'.$cid.'.domain'},
 5938: 		 $env{'course.'.$cid.'.num'})
 5939: 		.' '.
 5940: 	    &del('nohist_calculatedsheets_'.$cid,
 5941: 		 [$key.'assesscalc:'.$symb],$udom,$uname);
 5942:         unless ($status eq 'ok ok') {
 5943:            &logthis('Could not devalidate spreadsheet '.
 5944:                     $uname.' at '.$udom.' for '.
 5945: 		    $symb.': '.$status);
 5946:         }
 5947: 	&delenv('user.state.'.$cid);
 5948:     }
 5949: }
 5950: 
 5951: sub get_scalar {
 5952:     my ($string,$end) = @_;
 5953:     my $value;
 5954:     if ($$string =~ s/^([^&]*?)($end)/$2/) {
 5955: 	$value = $1;
 5956:     } elsif ($$string =~ s/^([^&]*?)&//) {
 5957: 	$value = $1;
 5958:     }
 5959:     return &unescape($value);
 5960: }
 5961: 
 5962: sub array2str {
 5963:   my (@array) = @_;
 5964:   my $result=&arrayref2str(\@array);
 5965:   $result=~s/^__ARRAY_REF__//;
 5966:   $result=~s/__END_ARRAY_REF__$//;
 5967:   return $result;
 5968: }
 5969: 
 5970: sub arrayref2str {
 5971:   my ($arrayref) = @_;
 5972:   my $result='__ARRAY_REF__';
 5973:   foreach my $elem (@$arrayref) {
 5974:     if(ref($elem) eq 'ARRAY') {
 5975:       $result.=&arrayref2str($elem).'&';
 5976:     } elsif(ref($elem) eq 'HASH') {
 5977:       $result.=&hashref2str($elem).'&';
 5978:     } elsif(ref($elem)) {
 5979:       #print("Got a ref of ".(ref($elem))." skipping.");
 5980:     } else {
 5981:       $result.=&escape($elem).'&';
 5982:     }
 5983:   }
 5984:   $result=~s/\&$//;
 5985:   $result .= '__END_ARRAY_REF__';
 5986:   return $result;
 5987: }
 5988: 
 5989: sub hash2str {
 5990:   my (%hash) = @_;
 5991:   my $result=&hashref2str(\%hash);
 5992:   $result=~s/^__HASH_REF__//;
 5993:   $result=~s/__END_HASH_REF__$//;
 5994:   return $result;
 5995: }
 5996: 
 5997: sub hashref2str {
 5998:   my ($hashref)=@_;
 5999:   my $result='__HASH_REF__';
 6000:   foreach my $key (sort(keys(%$hashref))) {
 6001:     if (ref($key) eq 'ARRAY') {
 6002:       $result.=&arrayref2str($key).'=';
 6003:     } elsif (ref($key) eq 'HASH') {
 6004:       $result.=&hashref2str($key).'=';
 6005:     } elsif (ref($key)) {
 6006:       $result.='=';
 6007:       #print("Got a ref of ".(ref($key))." skipping.");
 6008:     } else {
 6009: 	if (defined($key)) {$result.=&escape($key).'=';} else { last; }
 6010:     }
 6011: 
 6012:     if(ref($hashref->{$key}) eq 'ARRAY') {
 6013:       $result.=&arrayref2str($hashref->{$key}).'&';
 6014:     } elsif(ref($hashref->{$key}) eq 'HASH') {
 6015:       $result.=&hashref2str($hashref->{$key}).'&';
 6016:     } elsif(ref($hashref->{$key})) {
 6017:        $result.='&';
 6018:       #print("Got a ref of ".(ref($hashref->{$key}))." skipping.");
 6019:     } else {
 6020:       $result.=&escape($hashref->{$key}).'&';
 6021:     }
 6022:   }
 6023:   $result=~s/\&$//;
 6024:   $result .= '__END_HASH_REF__';
 6025:   return $result;
 6026: }
 6027: 
 6028: sub str2hash {
 6029:     my ($string)=@_;
 6030:     my ($hash)=&str2hashref('__HASH_REF__'.$string.'__END_HASH_REF__');
 6031:     return %$hash;
 6032: }
 6033: 
 6034: sub str2hashref {
 6035:   my ($string) = @_;
 6036: 
 6037:   my %hash;
 6038: 
 6039:   if($string !~ /^__HASH_REF__/) {
 6040:       if (! ($string eq '' || !defined($string))) {
 6041: 	  $hash{'error'}='Not hash reference';
 6042:       }
 6043:       return (\%hash, $string);
 6044:   }
 6045: 
 6046:   $string =~ s/^__HASH_REF__//;
 6047: 
 6048:   while($string !~ /^__END_HASH_REF__/) {
 6049:       #key
 6050:       my $key='';
 6051:       if($string =~ /^__HASH_REF__/) {
 6052:           ($key, $string)=&str2hashref($string);
 6053:           if(defined($key->{'error'})) {
 6054:               $hash{'error'}='Bad data';
 6055:               return (\%hash, $string);
 6056:           }
 6057:       } elsif($string =~ /^__ARRAY_REF__/) {
 6058:           ($key, $string)=&str2arrayref($string);
 6059:           if($key->[0] eq 'Array reference error') {
 6060:               $hash{'error'}='Bad data';
 6061:               return (\%hash, $string);
 6062:           }
 6063:       } else {
 6064:           $string =~ s/^(.*?)=//;
 6065: 	  $key=&unescape($1);
 6066:       }
 6067:       $string =~ s/^=//;
 6068: 
 6069:       #value
 6070:       my $value='';
 6071:       if($string =~ /^__HASH_REF__/) {
 6072:           ($value, $string)=&str2hashref($string);
 6073:           if(defined($value->{'error'})) {
 6074:               $hash{'error'}='Bad data';
 6075:               return (\%hash, $string);
 6076:           }
 6077:       } elsif($string =~ /^__ARRAY_REF__/) {
 6078:           ($value, $string)=&str2arrayref($string);
 6079:           if($value->[0] eq 'Array reference error') {
 6080:               $hash{'error'}='Bad data';
 6081:               return (\%hash, $string);
 6082:           }
 6083:       } else {
 6084: 	  $value=&get_scalar(\$string,'__END_HASH_REF__');
 6085:       }
 6086:       $string =~ s/^&//;
 6087: 
 6088:       $hash{$key}=$value;
 6089:   }
 6090: 
 6091:   $string =~ s/^__END_HASH_REF__//;
 6092: 
 6093:   return (\%hash, $string);
 6094: }
 6095: 
 6096: sub str2array {
 6097:     my ($string)=@_;
 6098:     my ($array)=&str2arrayref('__ARRAY_REF__'.$string.'__END_ARRAY_REF__');
 6099:     return @$array;
 6100: }
 6101: 
 6102: sub str2arrayref {
 6103:   my ($string) = @_;
 6104:   my @array;
 6105: 
 6106:   if($string !~ /^__ARRAY_REF__/) {
 6107:       if (! ($string eq '' || !defined($string))) {
 6108: 	  $array[0]='Array reference error';
 6109:       }
 6110:       return (\@array, $string);
 6111:   }
 6112: 
 6113:   $string =~ s/^__ARRAY_REF__//;
 6114: 
 6115:   while($string !~ /^__END_ARRAY_REF__/) {
 6116:       my $value='';
 6117:       if($string =~ /^__HASH_REF__/) {
 6118:           ($value, $string)=&str2hashref($string);
 6119:           if(defined($value->{'error'})) {
 6120:               $array[0] ='Array reference error';
 6121:               return (\@array, $string);
 6122:           }
 6123:       } elsif($string =~ /^__ARRAY_REF__/) {
 6124:           ($value, $string)=&str2arrayref($string);
 6125:           if($value->[0] eq 'Array reference error') {
 6126:               $array[0] ='Array reference error';
 6127:               return (\@array, $string);
 6128:           }
 6129:       } else {
 6130: 	  $value=&get_scalar(\$string,'__END_ARRAY_REF__');
 6131:       }
 6132:       $string =~ s/^&//;
 6133: 
 6134:       push(@array, $value);
 6135:   }
 6136: 
 6137:   $string =~ s/^__END_ARRAY_REF__//;
 6138: 
 6139:   return (\@array, $string);
 6140: }
 6141: 
 6142: # -------------------------------------------------------------------Temp Store
 6143: 
 6144: sub tmpreset {
 6145:   my ($symb,$namespace,$domain,$stuname) = @_;
 6146:   if (!$symb) {
 6147:     $symb=&symbread();
 6148:     if (!$symb) { $symb= $env{'request.url'}; }
 6149:   }
 6150:   $symb=escape($symb);
 6151: 
 6152:   if (!$namespace) { $namespace=$env{'request.state'}; }
 6153:   $namespace=~s/\//\_/g;
 6154:   $namespace=~s/\W//g;
 6155: 
 6156:   if (!$domain) { $domain=$env{'user.domain'}; }
 6157:   if (!$stuname) { $stuname=$env{'user.name'}; }
 6158:   if ($domain eq 'public' && $stuname eq 'public') {
 6159:       $stuname=&get_requestor_ip();
 6160:   }
 6161:   my $path=LONCAPA::tempdir();
 6162:   my %hash;
 6163:   if (tie(%hash,'GDBM_File',
 6164: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
 6165: 	  &GDBM_WRCREAT(),0640)) {
 6166:     foreach my $key (keys(%hash)) {
 6167:       if ($key=~ /:$symb/) {
 6168: 	delete($hash{$key});
 6169:       }
 6170:     }
 6171:   }
 6172: }
 6173: 
 6174: sub tmpstore {
 6175:   my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
 6176: 
 6177:   if (!$symb) {
 6178:     $symb=&symbread();
 6179:     if (!$symb) { $symb= $env{'request.url'}; }
 6180:   }
 6181:   $symb=escape($symb);
 6182: 
 6183:   if (!$namespace) {
 6184:     # I don't think we would ever want to store this for a course.
 6185:     # it seems this will only be used if we don't have a course.
 6186:     #$namespace=$env{'request.course.id'};
 6187:     #if (!$namespace) {
 6188:       $namespace=$env{'request.state'};
 6189:     #}
 6190:   }
 6191:   $namespace=~s/\//\_/g;
 6192:   $namespace=~s/\W//g;
 6193:   if (!$domain) { $domain=$env{'user.domain'}; }
 6194:   if (!$stuname) { $stuname=$env{'user.name'}; }
 6195:   if ($domain eq 'public' && $stuname eq 'public') {
 6196:       $stuname=&get_requestor_ip();
 6197:   }
 6198:   my $now=time;
 6199:   my %hash;
 6200:   my $path=LONCAPA::tempdir();
 6201:   if (tie(%hash,'GDBM_File',
 6202: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
 6203: 	  &GDBM_WRCREAT(),0640)) {
 6204:     $hash{"version:$symb"}++;
 6205:     my $version=$hash{"version:$symb"};
 6206:     my $allkeys=''; 
 6207:     foreach my $key (keys(%$storehash)) {
 6208:       $allkeys.=$key.':';
 6209:       $hash{"$version:$symb:$key"}=&freeze_escape($$storehash{$key});
 6210:     }
 6211:     $hash{"$version:$symb:timestamp"}=$now;
 6212:     $allkeys.='timestamp';
 6213:     $hash{"$version:keys:$symb"}=$allkeys;
 6214:     if (untie(%hash)) {
 6215:       return 'ok';
 6216:     } else {
 6217:       return "error:$!";
 6218:     }
 6219:   } else {
 6220:     return "error:$!";
 6221:   }
 6222: }
 6223: 
 6224: # -----------------------------------------------------------------Temp Restore
 6225: 
 6226: sub tmprestore {
 6227:   my ($symb,$namespace,$domain,$stuname) = @_;
 6228: 
 6229:   if (!$symb) {
 6230:     $symb=&symbread();
 6231:     if (!$symb) { $symb= $env{'request.url'}; }
 6232:   }
 6233:   $symb=escape($symb);
 6234: 
 6235:   if (!$namespace) { $namespace=$env{'request.state'}; }
 6236: 
 6237:   if (!$domain) { $domain=$env{'user.domain'}; }
 6238:   if (!$stuname) { $stuname=$env{'user.name'}; }
 6239:   if ($domain eq 'public' && $stuname eq 'public') {
 6240:       $stuname=&get_requestor_ip();
 6241:   }
 6242:   my %returnhash;
 6243:   $namespace=~s/\//\_/g;
 6244:   $namespace=~s/\W//g;
 6245:   my %hash;
 6246:   my $path=LONCAPA::tempdir();
 6247:   if (tie(%hash,'GDBM_File',
 6248: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
 6249: 	  &GDBM_READER(),0640)) {
 6250:     my $version=$hash{"version:$symb"};
 6251:     $returnhash{'version'}=$version;
 6252:     my $scope;
 6253:     for ($scope=1;$scope<=$version;$scope++) {
 6254:       my $vkeys=$hash{"$scope:keys:$symb"};
 6255:       my @keys=split(/:/,$vkeys);
 6256:       my $key;
 6257:       $returnhash{"$scope:keys"}=$vkeys;
 6258:       foreach $key (@keys) {
 6259: 	$returnhash{"$scope:$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
 6260: 	$returnhash{"$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
 6261:       }
 6262:     }
 6263:     if (!(untie(%hash))) {
 6264:       return "error:$!";
 6265:     }
 6266:   } else {
 6267:     return "error:$!";
 6268:   }
 6269:   return %returnhash;
 6270: }
 6271: 
 6272: # ----------------------------------------------------------------------- Store
 6273: 
 6274: sub store {
 6275:     my ($storehash,$symb,$namespace,$domain,$stuname,$laststore) = @_;
 6276:     my $home='';
 6277: 
 6278:     if ($stuname) { $home=&homeserver($stuname,$domain); }
 6279: 
 6280:     $symb=&symbclean($symb);
 6281:     if (!$symb) { unless ($symb=&symbread()) { return ''; } }
 6282: 
 6283:     if (!$domain) { $domain=$env{'user.domain'}; }
 6284:     if (!$stuname) { $stuname=$env{'user.name'}; }
 6285: 
 6286:     &devalidate($symb,$stuname,$domain);
 6287: 
 6288:     $symb=escape($symb);
 6289:     if (!$namespace) { 
 6290:        unless ($namespace=$env{'request.course.id'}) { 
 6291:           return ''; 
 6292:        } 
 6293:     }
 6294:     if (!$home) { $home=$env{'user.home'}; }
 6295: 
 6296:     $$storehash{'ip'}=&get_requestor_ip();
 6297:     $$storehash{'host'}=$perlvar{'lonHostID'};
 6298: 
 6299:     my $namevalue='';
 6300:     foreach my $key (keys(%$storehash)) {
 6301:         $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
 6302:     }
 6303:     $namevalue=~s/\&$//;
 6304:     &courselog($symb.':'.$stuname.':'.$domain.':STORE:'.$namevalue);
 6305:     return reply("store:$domain:$stuname:$namespace:$symb:$namevalue:$laststore","$home");
 6306: }
 6307: 
 6308: # -------------------------------------------------------------- Critical Store
 6309: 
 6310: sub cstore {
 6311:     my ($storehash,$symb,$namespace,$domain,$stuname,$laststore) = @_;
 6312:     my $home='';
 6313: 
 6314:     if ($stuname) { $home=&homeserver($stuname,$domain); }
 6315: 
 6316:     $symb=&symbclean($symb);
 6317:     if (!$symb) { unless ($symb=&symbread()) { return ''; } }
 6318: 
 6319:     if (!$domain) { $domain=$env{'user.domain'}; }
 6320:     if (!$stuname) { $stuname=$env{'user.name'}; }
 6321: 
 6322:     &devalidate($symb,$stuname,$domain);
 6323: 
 6324:     $symb=escape($symb);
 6325:     if (!$namespace) { 
 6326:        unless ($namespace=$env{'request.course.id'}) { 
 6327:           return ''; 
 6328:        } 
 6329:     }
 6330:     if (!$home) { $home=$env{'user.home'}; }
 6331: 
 6332:     $$storehash{'ip'}=&get_requestor_ip();
 6333:     $$storehash{'host'}=$perlvar{'lonHostID'};
 6334: 
 6335:     my $namevalue='';
 6336:     foreach my $key (keys(%$storehash)) {
 6337:         $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
 6338:     }
 6339:     $namevalue=~s/\&$//;
 6340:     &courselog($symb.':'.$stuname.':'.$domain.':CSTORE:'.$namevalue);
 6341:     return critical
 6342:                 ("store:$domain:$stuname:$namespace:$symb:$namevalue:$laststore","$home");
 6343: }
 6344: 
 6345: # --------------------------------------------------------------------- Restore
 6346: 
 6347: sub restore {
 6348:     my ($symb,$namespace,$domain,$stuname) = @_;
 6349:     my $home='';
 6350: 
 6351:     if ($stuname) { $home=&homeserver($stuname,$domain); }
 6352: 
 6353:     if (!$symb) {
 6354:         return if ($namespace eq 'courserequests');
 6355:         unless ($symb=escape(&symbread())) { return ''; }
 6356:     } else {
 6357:         unless ($namespace eq 'courserequests') {
 6358:             $symb=&escape(&symbclean($symb));
 6359:         }
 6360:     }
 6361:     if (!$namespace) { 
 6362:        unless ($namespace=$env{'request.course.id'}) { 
 6363:           return ''; 
 6364:        } 
 6365:     }
 6366:     if (!$domain) { $domain=$env{'user.domain'}; }
 6367:     if (!$stuname) { $stuname=$env{'user.name'}; }
 6368:     if (!$home) { $home=$env{'user.home'}; }
 6369:     my $answer=&reply("restore:$domain:$stuname:$namespace:$symb","$home");
 6370: 
 6371:     my %returnhash=();
 6372:     foreach my $line (split(/\&/,$answer)) {
 6373: 	my ($name,$value)=split(/\=/,$line);
 6374:         $returnhash{&unescape($name)}=&thaw_unescape($value);
 6375:     }
 6376:     my $version;
 6377:     for ($version=1;$version<=$returnhash{'version'};$version++) {
 6378:        foreach my $item (split(/\:/,$returnhash{$version.':keys'})) {
 6379:           $returnhash{$item}=$returnhash{$version.':'.$item};
 6380:        }
 6381:     }
 6382:     return %returnhash;
 6383: }
 6384: 
 6385: # ---------------------------------------------------------- Course Description
 6386: #
 6387: #  
 6388: 
 6389: sub coursedescription {
 6390:     my ($courseid,$args)=@_;
 6391:     $courseid=~s/^\///;
 6392:     $courseid=~s/\_/\//g;
 6393:     my ($cdomain,$cnum)=split(/\//,$courseid);
 6394:     my $chome=&homeserver($cnum,$cdomain);
 6395:     my $normalid=$cdomain.'_'.$cnum;
 6396:     # need to always cache even if we get errors otherwise we keep 
 6397:     # trying and trying and trying to get the course description.
 6398:     my %envhash=();
 6399:     my %returnhash=();
 6400:     
 6401:     my $expiretime=600;
 6402:     if ($env{'request.course.id'} eq $normalid) {
 6403: 	$expiretime=120;
 6404:     }
 6405: 
 6406:     my $prefix='course.'.$cdomain.'_'.$cnum.'.';
 6407:     if (!$args->{'freshen_cache'}
 6408: 	&& ((time-$env{$prefix.'last_cache'}) < $expiretime) ) {
 6409: 	foreach my $key (keys(%env)) {
 6410: 	    next if ($key !~ /^\Q$prefix\E(.*)/);
 6411: 	    my ($setting) = $1;
 6412: 	    $returnhash{$setting} = $env{$key};
 6413: 	}
 6414: 	return %returnhash;
 6415:     }
 6416: 
 6417:     # get the data again
 6418: 
 6419:     if (!$args->{'one_time'}) {
 6420: 	$envhash{'course.'.$normalid.'.last_cache'}=time;
 6421:     }
 6422: 
 6423:     if ($chome ne 'no_host') {
 6424:        %returnhash=&dump('environment',$cdomain,$cnum);
 6425:        if (!exists($returnhash{'con_lost'})) {
 6426: 	   my $username = $env{'user.name'}; # Defult username
 6427: 	   if(defined $args->{'user'}) {
 6428: 	       $username = $args->{'user'};
 6429: 	   }
 6430:            $returnhash{'home'}= $chome;
 6431: 	   $returnhash{'domain'} = $cdomain;
 6432: 	   $returnhash{'num'} = $cnum;
 6433:            if (!defined($returnhash{'type'})) {
 6434:                $returnhash{'type'} = 'Course';
 6435:            }
 6436:            while (my ($name,$value) = each %returnhash) {
 6437:                $envhash{'course.'.$normalid.'.'.$name}=$value;
 6438:            }
 6439:            $returnhash{'url'}=&clutter($returnhash{'url'});
 6440:            $returnhash{'fn'}=LONCAPA::tempdir() .
 6441: 	       $username.'_'.$cdomain.'_'.$cnum;
 6442:            $envhash{'course.'.$normalid.'.home'}=$chome;
 6443:            $envhash{'course.'.$normalid.'.domain'}=$cdomain;
 6444:            $envhash{'course.'.$normalid.'.num'}=$cnum;
 6445:        }
 6446:     }
 6447:     if (!$args->{'one_time'}) {
 6448: 	&appenv(\%envhash);
 6449:     }
 6450:     return %returnhash;
 6451: }
 6452: 
 6453: sub update_released_required {
 6454:     my ($needsrelease,$cdom,$cnum,$chome,$cid) = @_;
 6455:     if ($cdom eq '' || $cnum eq '' || $chome eq '' || $cid eq '') {
 6456:         $cid = $env{'request.course.id'};
 6457:         $cdom = $env{'course.'.$cid.'.domain'};
 6458:         $cnum = $env{'course.'.$cid.'.num'};
 6459:         $chome = $env{'course.'.$cid.'.home'};
 6460:     }
 6461:     if ($needsrelease) {
 6462:         my %curr_reqd_hash = &userenvironment($cdom,$cnum,'internal.releaserequired');
 6463:         my $needsupdate;
 6464:         if ($curr_reqd_hash{'internal.releaserequired'} eq '') {
 6465:             $needsupdate = 1;
 6466:         } else {
 6467:             my ($currmajor,$currminor) = split(/\./,$curr_reqd_hash{'internal.releaserequired'});
 6468:             my ($needsmajor,$needsminor) = split(/\./,$needsrelease);
 6469:             if (($currmajor < $needsmajor) || ($currmajor == $needsmajor && $currminor < $needsminor)) {
 6470:                 $needsupdate = 1;
 6471:             }
 6472:         }
 6473:         if ($needsupdate) {
 6474:             my %needshash = (
 6475:                              'internal.releaserequired' => $needsrelease,
 6476:                             );
 6477:             my $putresult = &put('environment',\%needshash,$cdom,$cnum);
 6478:             if ($putresult eq 'ok') {
 6479:                 &appenv({'course.'.$cid.'.internal.releaserequired' => $needsrelease});
 6480:                 my %crsinfo = &courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
 6481:                 if (ref($crsinfo{$cid}) eq 'HASH') {
 6482:                     $crsinfo{$cid}{'releaserequired'} = $needsrelease;
 6483:                     &courseidput($cdom,\%crsinfo,$chome,'notime');
 6484:                 }
 6485:             }
 6486:         }
 6487:     }
 6488:     return;
 6489: }
 6490: 
 6491: # -------------------------------------------------See if a user is privileged
 6492: 
 6493: sub privileged {
 6494:     my ($username,$domain,$possdomains,$possroles)=@_;
 6495:     my $now = time;
 6496:     my $roles;
 6497:     if (ref($possroles) eq 'ARRAY') {
 6498:         $roles = $possroles; 
 6499:     } else {
 6500:         $roles = ['dc','su'];
 6501:     }
 6502:     if (ref($possdomains) eq 'ARRAY') {
 6503:         my %privileged = &privileged_by_domain($possdomains,$roles);
 6504:         foreach my $dom (@{$possdomains}) {
 6505:             if (($username =~ /^$match_username$/) && ($domain =~ /^$match_domain$/) &&
 6506:                 (ref($privileged{$dom}) eq 'HASH')) {
 6507:                 foreach my $role (@{$roles}) {
 6508:                     if (ref($privileged{$dom}{$role}) eq 'HASH') {
 6509:                         if (exists($privileged{$dom}{$role}{$username.':'.$domain})) {
 6510:                             my ($end,$start) = split(/:/,$privileged{$dom}{$role}{$username.':'.$domain});
 6511:                             return 1 unless (($end && $end < $now) ||
 6512:                                              ($start && $start > $now));
 6513:                         }
 6514:                     }
 6515:                 }
 6516:             }
 6517:         }
 6518:     } else {
 6519:         my %rolesdump = &dump("roles", $domain, $username) or return 0;
 6520:         my $now = time;
 6521: 
 6522:         for my $role (@rolesdump{grep { ! /^rolesdef_/ } keys(%rolesdump)}) {
 6523:             my ($trole, $tend, $tstart) = split(/_/, $role);
 6524:             if (grep(/^\Q$trole\E$/,@{$roles})) {
 6525:                 return 1 unless ($tend && $tend < $now) 
 6526:                         or ($tstart && $tstart > $now);
 6527:             }
 6528:         }
 6529:     }
 6530:     return 0;
 6531: }
 6532: 
 6533: sub privileged_by_domain {
 6534:     my ($domains,$roles) = @_;
 6535:     my %privileged = ();
 6536:     my $cachetime = 60*60*24;
 6537:     my $now = time;
 6538:     unless ((ref($domains) eq 'ARRAY') && (ref($roles) eq 'ARRAY')) {
 6539:         return %privileged;
 6540:     }
 6541:     foreach my $dom (@{$domains}) {
 6542:         next if (ref($privileged{$dom}) eq 'HASH');
 6543:         my $needroles;
 6544:         foreach my $role (@{$roles}) {
 6545:             my ($result,$cached)=&is_cached_new('priv_'.$role,$dom);
 6546:             if (defined($cached)) {
 6547:                 if (ref($result) eq 'HASH') {
 6548:                     $privileged{$dom}{$role} = $result;
 6549:                 }
 6550:             } else {
 6551:                 $needroles = 1;
 6552:             }
 6553:         }
 6554:         if ($needroles) {
 6555:             my %dompersonnel = &get_domain_roles($dom,$roles);
 6556:             $privileged{$dom} = {};
 6557:             foreach my $server (keys(%dompersonnel)) {
 6558:                 if (ref($dompersonnel{$server}) eq 'HASH') {
 6559:                     foreach my $item (keys(%{$dompersonnel{$server}})) {
 6560:                         my ($trole,$uname,$udom,$rest) = split(/:/,$item,4);
 6561:                         my ($end,$start) = split(/:/,$dompersonnel{$server}{$item});
 6562:                         next if ($end && $end < $now);
 6563:                         $privileged{$dom}{$trole}{$uname.':'.$udom} = 
 6564:                             $dompersonnel{$server}{$item};
 6565:                     }
 6566:                 }
 6567:             }
 6568:             if (ref($privileged{$dom}) eq 'HASH') {
 6569:                 foreach my $role (@{$roles}) {
 6570:                     if (ref($privileged{$dom}{$role}) eq 'HASH') {
 6571:                         &do_cache_new('priv_'.$role,$dom,$privileged{$dom}{$role},$cachetime);
 6572:                     } else {
 6573:                         my %hash = ();
 6574:                         &do_cache_new('priv_'.$role,$dom,\%hash,$cachetime);
 6575:                     }
 6576:                 }
 6577:             }
 6578:         }
 6579:     }
 6580:     return %privileged;
 6581: }
 6582: 
 6583: # -------------------------------------------------------- Get user privileges
 6584: 
 6585: sub rolesinit {
 6586:     my ($domain, $username) = @_;
 6587:     my %userroles = ('user.login.time' => time);
 6588:     my %rolesdump = &dump("roles", $domain, $username) or return \%userroles;
 6589: 
 6590:     # firstaccess and timerinterval are related to timed maps/resources. 
 6591:     # also, blocking can be triggered by an activating timer
 6592:     # it's saved in the user's %env.
 6593:     my %firstaccess = &dump('firstaccesstimes', $domain, $username);
 6594:     my %timerinterval = &dump('timerinterval', $domain, $username);
 6595:     my (%coursetimerstarts, %firstaccchk, %firstaccenv, %coursetimerintervals,
 6596:         %timerintchk, %timerintenv);
 6597: 
 6598:     foreach my $key (keys(%firstaccess)) {
 6599:         my ($cid, $rest) = split(/\0/, $key);
 6600:         $coursetimerstarts{$cid}{$rest} = $firstaccess{$key};
 6601:     }
 6602: 
 6603:     foreach my $key (keys(%timerinterval)) {
 6604:         my ($cid,$rest) = split(/\0/,$key);
 6605:         $coursetimerintervals{$cid}{$rest} = $timerinterval{$key};
 6606:     }
 6607: 
 6608:     my %allroles=();
 6609:     my %allgroups=();
 6610: 
 6611:     for my $area (grep { ! /^rolesdef_/ } keys(%rolesdump)) {
 6612:         my $role = $rolesdump{$area};
 6613:         $area =~ s/\_\w\w$//;
 6614: 
 6615:         my ($trole, $tend, $tstart, $group_privs);
 6616: 
 6617:         if ($role =~ /^cr/) {
 6618:         # Custom role, defined by a user 
 6619:         # e.g., user.role.cr/msu/smith/mynewrole
 6620:             if ($role =~ m|^(cr/$match_domain/$match_username/[a-zA-Z0-9]+)_(.*)$|) {
 6621:                 $trole = $1;
 6622:                 ($tend, $tstart) = split('_', $2);
 6623:             } else {
 6624:                 $trole = $role;
 6625:             }
 6626:         } elsif ($role =~ m|^gr/|) {
 6627:         # Role of member in a group, defined within a course/community
 6628:         # e.g., user.role.gr/msu/04935610a19ee4a5fmsul1/leopards
 6629:             ($trole, $tend, $tstart) = split(/_/, $role);
 6630:             next if $tstart eq '-1';
 6631:             ($trole, $group_privs) = split(/\//, $trole);
 6632:             $group_privs = &unescape($group_privs);
 6633:         } else {
 6634:         # Just a normal role, defined in roles.tab
 6635:             ($trole, $tend, $tstart) = split(/_/,$role);
 6636:         }
 6637: 
 6638:         my %new_role = &set_arearole($trole,$area,$tstart,$tend,$domain,
 6639:                  $username);
 6640:         @userroles{keys(%new_role)} = @new_role{keys(%new_role)};
 6641: 
 6642:         # role expired or not available yet?
 6643:         $trole = '' if ($tend != 0 && $tend < $userroles{'user.login.time'}) or 
 6644:             ($tstart != 0 && $tstart > $userroles{'user.login.time'});
 6645: 
 6646:         next if $area eq '' or $trole eq '';
 6647: 
 6648:         my $spec = "$trole.$area";
 6649:         my ($tdummy, $tdomain, $trest) = split(/\//, $area);
 6650: 
 6651:         if ($trole =~ /^cr\//) {
 6652:         # Custom role, defined by a user
 6653:             &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
 6654:         } elsif ($trole eq 'gr') {
 6655:         # Role of a member in a group, defined within a course/community
 6656:             &group_roleprivs(\%allgroups,$area,$group_privs,$tend,$tstart);
 6657:             next;
 6658:         } else {
 6659:         # Normal role, defined in roles.tab
 6660:             &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
 6661:         }
 6662: 
 6663:         my $cid = $tdomain.'_'.$trest;
 6664:         unless ($firstaccchk{$cid}) {
 6665:             if (ref($coursetimerstarts{$cid}) eq 'HASH') {
 6666:                 foreach my $item (keys(%{$coursetimerstarts{$cid}})) {
 6667:                     $firstaccenv{'course.'.$cid.'.firstaccess.'.$item} = 
 6668:                         $coursetimerstarts{$cid}{$item}; 
 6669:                 }
 6670:             }
 6671:             $firstaccchk{$cid} = 1;
 6672:         }
 6673:         unless ($timerintchk{$cid}) {
 6674:             if (ref($coursetimerintervals{$cid}) eq 'HASH') {
 6675:                 foreach my $item (keys(%{$coursetimerintervals{$cid}})) {
 6676:                     $timerintenv{'course.'.$cid.'.timerinterval.'.$item} =
 6677:                        $coursetimerintervals{$cid}{$item};
 6678:                 }
 6679:             }
 6680:             $timerintchk{$cid} = 1;
 6681:         }
 6682:     }
 6683: 
 6684:     @userroles{'user.author','user.adv','user.rar'} = &set_userprivs(\%userroles,
 6685:                                                           \%allroles, \%allgroups);
 6686:     $env{'user.adv'} = $userroles{'user.adv'};
 6687:     $env{'user.rar'} = $userroles{'user.rar'};
 6688: 
 6689:     return (\%userroles,\%firstaccenv,\%timerintenv);
 6690: }
 6691: 
 6692: sub set_arearole {
 6693:     my ($trole,$area,$tstart,$tend,$domain,$username,$nolog) = @_;
 6694:     unless ($nolog) {
 6695: # log the associated role with the area
 6696:         &userrolelog($trole,$username,$domain,$area,$tstart,$tend);
 6697:     }
 6698:     return ('user.role.'.$trole.'.'.$area => $tstart.'.'.$tend);
 6699: }
 6700: 
 6701: sub custom_roleprivs {
 6702:     my ($allroles,$trole,$tdomain,$trest,$spec,$area) = @_;
 6703:     my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$trole);
 6704:     my $homsvr = &homeserver($rauthor,$rdomain);
 6705:     if (&hostname($homsvr) ne '') {
 6706:         my ($rdummy,$roledef)=
 6707:             &get('roles',["rolesdef_$rrole"],$rdomain,$rauthor);
 6708:         if (($rdummy ne 'con_lost') && ($roledef ne '')) {
 6709:             my ($syspriv,$dompriv,$coursepriv)=split(/\_/,$roledef);
 6710:             if (defined($syspriv)) {
 6711:                 if ($trest =~ /^$match_community$/) {
 6712:                     $syspriv =~ s/bre\&S//; 
 6713:                 }
 6714:                 $$allroles{'cm./'}.=':'.$syspriv;
 6715:                 $$allroles{$spec.'./'}.=':'.$syspriv;
 6716:             }
 6717:             if ($tdomain ne '') {
 6718:                 if (defined($dompriv)) {
 6719:                     $$allroles{'cm./'.$tdomain.'/'}.=':'.$dompriv;
 6720:                     $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$dompriv;
 6721:                 }
 6722:                 if (($trest ne '') && (defined($coursepriv))) {
 6723:                     if ($trole =~ m{^cr/$tdomain/$tdomain\Q-domainconfig\E/([^/]+)$}) {
 6724:                         my $rolename = $1;
 6725:                         $coursepriv = &course_adhocrole_privs($rolename,$tdomain,$trest,$coursepriv);
 6726:                     }
 6727:                     $$allroles{'cm.'.$area}.=':'.$coursepriv;
 6728:                     $$allroles{$spec.'.'.$area}.=':'.$coursepriv;
 6729:                 }
 6730:             }
 6731:         }
 6732:     }
 6733: }
 6734: 
 6735: sub course_adhocrole_privs {
 6736:     my ($rolename,$cdom,$cnum,$coursepriv) = @_;
 6737:     my %overrides = &get('environment',["internal.adhocpriv.$rolename"],$cdom,$cnum);
 6738:     if ($overrides{"internal.adhocpriv.$rolename"}) {
 6739:         my (%currprivs,%storeprivs);
 6740:         foreach my $item (split(/:/,$coursepriv)) {
 6741:             my ($priv,$restrict) = split(/\&/,$item);
 6742:             $currprivs{$priv} = $restrict;
 6743:         }
 6744:         my (%possadd,%possremove,%full);
 6745:         foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:c'})) {
 6746:             my ($priv,$restrict)=split(/\&/,$item);
 6747:             $full{$priv} = $restrict;
 6748:         }
 6749:         foreach my $item (split(/,/,$overrides{"internal.adhocpriv.$rolename"})) {
 6750:              next if ($item eq '');
 6751:              my ($rule,$rest) = split(/=/,$item);
 6752:              next unless (($rule eq 'off') || ($rule eq 'on'));
 6753:              foreach my $priv (split(/:/,$rest)) {
 6754:                  if ($priv ne '') {
 6755:                      if ($rule eq 'off') {
 6756:                          $possremove{$priv} = 1;
 6757:                      } else {
 6758:                          $possadd{$priv} = 1;
 6759:                      }
 6760:                  }
 6761:              }
 6762:          }
 6763:          foreach my $priv (sort(keys(%full))) {
 6764:              if (exists($currprivs{$priv})) {
 6765:                  unless (exists($possremove{$priv})) {
 6766:                      $storeprivs{$priv} = $currprivs{$priv};
 6767:                  }
 6768:              } elsif (exists($possadd{$priv})) {
 6769:                  $storeprivs{$priv} = $full{$priv};
 6770:              }
 6771:          }
 6772:          $coursepriv = ':'.join(':',map { $_.'&'.$storeprivs{$_}; } sort(keys(%storeprivs)));
 6773:      }
 6774:      return $coursepriv;
 6775: }
 6776: 
 6777: sub group_roleprivs {
 6778:     my ($allgroups,$area,$group_privs,$tend,$tstart) = @_;
 6779:     my $access = 1;
 6780:     my $now = time;
 6781:     if (($tend!=0) && ($tend<$now)) { $access = 0; }
 6782:     if (($tstart!=0) && ($tstart>$now)) { $access=0; }
 6783:     if ($access) {
 6784:         my ($course,$group) = ($area =~ m|(/$match_domain/$match_courseid)/([^/]+)$|);
 6785:         $$allgroups{$course}{$group} .=':'.$group_privs;
 6786:     }
 6787: }
 6788: 
 6789: sub standard_roleprivs {
 6790:     my ($allroles,$trole,$tdomain,$spec,$trest,$area) = @_;
 6791:     if (defined($pr{$trole.':s'})) {
 6792:         $$allroles{'cm./'}.=':'.$pr{$trole.':s'};
 6793:         $$allroles{$spec.'./'}.=':'.$pr{$trole.':s'};
 6794:     }
 6795:     if ($tdomain ne '') {
 6796:         if (defined($pr{$trole.':d'})) {
 6797:             $$allroles{'cm./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
 6798:             $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
 6799:         }
 6800:         if (($trest ne '') && (defined($pr{$trole.':c'}))) {
 6801:             $$allroles{'cm.'.$area}.=':'.$pr{$trole.':c'};
 6802:             $$allroles{$spec.'.'.$area}.=':'.$pr{$trole.':c'};
 6803:         }
 6804:     }
 6805: }
 6806: 
 6807: sub set_userprivs {
 6808:     my ($userroles,$allroles,$allgroups,$groups_roles) = @_; 
 6809:     my $author=0;
 6810:     my $adv=0;
 6811:     my $rar=0;
 6812:     my %grouproles = ();
 6813:     if (keys(%{$allgroups}) > 0) {
 6814:         my @groupkeys; 
 6815:         foreach my $role (keys(%{$allroles})) {
 6816:             push(@groupkeys,$role);
 6817:         }
 6818:         if (ref($groups_roles) eq 'HASH') {
 6819:             foreach my $key (keys(%{$groups_roles})) {
 6820:                 unless (grep(/^\Q$key\E$/,@groupkeys)) {
 6821:                     push(@groupkeys,$key);
 6822:                 }
 6823:             }
 6824:         }
 6825:         if (@groupkeys > 0) {
 6826:             foreach my $role (@groupkeys) {
 6827:                 my ($trole,$area,$sec,$extendedarea);
 6828:                 if ($role =~ m-^(\w+|cr/$match_domain/$match_username/\w+)\.(/$match_domain/$match_courseid)(/?\w*)\.-) {
 6829:                     $trole = $1;
 6830:                     $area = $2;
 6831:                     $sec = $3;
 6832:                     $extendedarea = $area.$sec;
 6833:                     if (exists($$allgroups{$area})) {
 6834:                         foreach my $group (keys(%{$$allgroups{$area}})) {
 6835:                             my $spec = $trole.'.'.$extendedarea;
 6836:                             $grouproles{$spec.'.'.$area.'/'.$group} = 
 6837:                                                 $$allgroups{$area}{$group};
 6838:                         }
 6839:                     }
 6840:                 }
 6841:             }
 6842:         }
 6843:     }
 6844:     foreach my $group (keys(%grouproles)) {
 6845:         $$allroles{$group} = $grouproles{$group};
 6846:     }
 6847:     foreach my $role (keys(%{$allroles})) {
 6848:         my %thesepriv;
 6849:         if (($role=~/^au/) || ($role=~/^ca/) || ($role=~/^aa/)) { $author=1; }
 6850:         foreach my $item (split(/:/,$$allroles{$role})) {
 6851:             if ($item ne '') {
 6852:                 my ($privilege,$restrictions)=split(/&/,$item);
 6853:                 if ($restrictions eq '') {
 6854:                     $thesepriv{$privilege}='F';
 6855:                 } elsif ($thesepriv{$privilege} ne 'F') {
 6856:                     $thesepriv{$privilege}.=$restrictions;
 6857:                 }
 6858:                 if ($thesepriv{'adv'} eq 'F') { $adv=1; }
 6859:                 if ($thesepriv{'rar'} eq 'F') { $rar=1; }
 6860:             }
 6861:         }
 6862:         my $thesestr='';
 6863:         foreach my $priv (sort(keys(%thesepriv))) {
 6864: 	    $thesestr.=':'.$priv.'&'.$thesepriv{$priv};
 6865: 	}
 6866:         $userroles->{'user.priv.'.$role} = $thesestr;
 6867:     }
 6868:     return ($author,$adv,$rar);
 6869: }
 6870: 
 6871: sub role_status {
 6872:     my ($rolekey,$update,$refresh,$now,$role,$where,$trolecode,$tstatus,$tstart,$tend) = @_;
 6873:     if (exists($env{$rolekey}) && $env{$rolekey} ne '') {
 6874:         my ($one,$two) = split(m{\./},$rolekey,2);
 6875:         (undef,undef,$$role) = split(/\./,$one,3);
 6876:         unless (!defined($$role) || $$role eq '') {
 6877:             $$where = '/'.$two;
 6878:             $$trolecode=$$role.'.'.$$where;
 6879:             ($$tstart,$$tend)=split(/\./,$env{$rolekey});
 6880:             $$tstatus='is';
 6881:             if ($$tstart && $$tstart>$update) {
 6882:                 $$tstatus='future';
 6883:                 if ($$tstart<$now) {
 6884:                     if ($$tstart && $$tstart>$refresh) {
 6885:                         if (($$where ne '') && ($$role ne '')) {
 6886:                             my (%allroles,%allgroups,$group_privs,
 6887:                                 %groups_roles,@rolecodes);
 6888:                             my %userroles = (
 6889:                                 'user.role.'.$$role.'.'.$$where => $$tstart.'.'.$$tend
 6890:                             );
 6891:                             @rolecodes = ('cm'); 
 6892:                             my $spec=$$role.'.'.$$where;
 6893:                             my ($tdummy,$tdomain,$trest)=split(/\//,$$where);
 6894:                             if ($$role =~ /^cr\//) {
 6895:                                 &custom_roleprivs(\%allroles,$$role,$tdomain,$trest,$spec,$$where);
 6896:                                 push(@rolecodes,'cr');
 6897:                             } elsif ($$role eq 'gr') {
 6898:                                 push(@rolecodes,$$role);
 6899:                                 my %rolehash = &get('roles',[$$where.'_'.$$role],$env{'user.domain'},
 6900:                                                     $env{'user.name'});
 6901:                                 my ($trole) = split('_',$rolehash{$$where.'_'.$$role},2);
 6902:                                 (undef,my $group_privs) = split(/\//,$trole);
 6903:                                 $group_privs = &unescape($group_privs);
 6904:                                 &group_roleprivs(\%allgroups,$$where,$group_privs,$$tend,$$tstart);
 6905:                                 my %course_roles = &get_my_roles($env{'user.name'},$env{'user.domain'},'userroles',['active'],['cc','co','in','ta','ep','ad','st','cr'],[$tdomain],1);
 6906:                                 &get_groups_roles($tdomain,$trest,
 6907:                                                   \%course_roles,\@rolecodes,
 6908:                                                   \%groups_roles);
 6909:                             } else {
 6910:                                 push(@rolecodes,$$role);
 6911:                                 &standard_roleprivs(\%allroles,$$role,$tdomain,$spec,$trest,$$where);
 6912:                             }
 6913:                             my ($author,$adv,$rar)= &set_userprivs(\%userroles,\%allroles,\%allgroups,
 6914:                                                                    \%groups_roles);
 6915:                             &appenv(\%userroles,\@rolecodes);
 6916:                             &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},"Role ".$spec);
 6917:                         }
 6918:                     }
 6919:                     $$tstatus = 'is';
 6920:                 }
 6921:             }
 6922:             if ($$tend) {
 6923:                 if ($$tend<$update) {
 6924:                     $$tstatus='expired';
 6925:                 } elsif ($$tend<$now) {
 6926:                     $$tstatus='will_not';
 6927:                 }
 6928:             }
 6929:         }
 6930:     }
 6931: }
 6932: 
 6933: sub get_groups_roles {
 6934:     my ($cdom,$rest,$cdom_courseroles,$rolecodes,$groups_roles) = @_;
 6935:     return unless((ref($cdom_courseroles) eq 'HASH') && 
 6936:                   (ref($rolecodes) eq 'ARRAY') && 
 6937:                   (ref($groups_roles) eq 'HASH')); 
 6938:     if (keys(%{$cdom_courseroles}) > 0) {
 6939:         my ($cnum) = ($rest =~ /^($match_courseid)/);
 6940:         if ($cdom ne '' && $cnum ne '') {
 6941:             foreach my $key (keys(%{$cdom_courseroles})) {
 6942:                 if ($key =~ /^\Q$cnum\E:\Q$cdom\E:([^:]+):?([^:]*)/) {
 6943:                     my $crsrole = $1;
 6944:                     my $crssec = $2;
 6945:                     if ($crsrole =~ /^cr/) {
 6946:                         unless (grep(/^cr$/,@{$rolecodes})) {
 6947:                             push(@{$rolecodes},'cr');
 6948:                         }
 6949:                     } else {
 6950:                         unless(grep(/^\Q$crsrole\E$/,@{$rolecodes})) {
 6951:                             push(@{$rolecodes},$crsrole);
 6952:                         }
 6953:                     }
 6954:                     my $rolekey = "$crsrole./$cdom/$cnum";
 6955:                     if ($crssec ne '') {
 6956:                         $rolekey .= "/$crssec";
 6957:                     }
 6958:                     $rolekey .= './';
 6959:                     $groups_roles->{$rolekey} = $rolecodes;
 6960:                 }
 6961:             }
 6962:         }
 6963:     }
 6964:     return;
 6965: }
 6966: 
 6967: sub delete_env_groupprivs {
 6968:     my ($where,$courseroles,$possroles) = @_;
 6969:     return unless((ref($courseroles) eq 'HASH') && (ref($possroles) eq 'ARRAY'));
 6970:     my ($dummy,$udom,$uname,$group) = split(/\//,$where);
 6971:     unless (ref($courseroles->{$udom}) eq 'HASH') {
 6972:         %{$courseroles->{$udom}} =
 6973:             &get_my_roles('','','userroles',['active'],
 6974:                           $possroles,[$udom],1);
 6975:     }
 6976:     if (ref($courseroles->{$udom}) eq 'HASH') {
 6977:         foreach my $item (keys(%{$courseroles->{$udom}})) {
 6978:             my ($cnum,$cdom,$crsrole,$crssec) = split(/:/,$item);
 6979:             my $area = '/'.$cdom.'/'.$cnum;
 6980:             my $privkey = "user.priv.$crsrole.$area";
 6981:             if ($crssec ne '') {
 6982:                 $privkey .= '/'.$crssec;
 6983:             }
 6984:             $privkey .= ".$area/$group";
 6985:             &Apache::lonnet::delenv($privkey,undef,[$crsrole]);
 6986:         }
 6987:     }
 6988:     return;
 6989: }
 6990: 
 6991: sub check_adhoc_privs {
 6992:     my ($cdom,$cnum,$update,$refresh,$now,$checkrole,$caller,$sec) = @_;
 6993:     my $cckey = 'user.role.'.$checkrole.'./'.$cdom.'/'.$cnum;
 6994:     if ($sec) {
 6995:         $cckey .= '/'.$sec;
 6996:     } 
 6997:     my $setprivs;
 6998:     if ($env{$cckey}) {
 6999:         my ($role,$where,$trolecode,$tstart,$tend,$tremark,$tstatus,$tpstart,$tpend);
 7000:         &role_status($cckey,$update,$refresh,$now,\$role,\$where,\$trolecode,\$tstatus,\$tstart,\$tend);
 7001:         unless (($tstatus eq 'is') || ($tstatus eq 'will_not')) {
 7002:             &set_adhoc_privileges($cdom,$cnum,$checkrole,$caller,$sec);
 7003:             $setprivs = 1;
 7004:         }
 7005:     } else {
 7006:         &set_adhoc_privileges($cdom,$cnum,$checkrole,$caller,$sec);
 7007:         $setprivs = 1;
 7008:     }
 7009:     return $setprivs;
 7010: }
 7011: 
 7012: sub set_adhoc_privileges {
 7013: # role can be cc, ca, or cr/<dom>/<dom>-domainconfig/role
 7014:     my ($dcdom,$pickedcourse,$role,$caller,$sec) = @_;
 7015:     my $area = '/'.$dcdom.'/'.$pickedcourse;
 7016:     if ($sec ne '') {
 7017:         $area .= '/'.$sec;
 7018:     }
 7019:     my $spec = $role.'.'.$area;
 7020:     my %userroles = &set_arearole($role,$area,'','',$env{'user.domain'},
 7021:                                   $env{'user.name'},1);
 7022:     my %rolehash = ();
 7023:     if ($role =~ m{^\Qcr/$dcdom/$dcdom\E\-domainconfig/(\w+)$}) {
 7024:         my $rolename = $1;
 7025:         &custom_roleprivs(\%rolehash,$role,$dcdom,$pickedcourse,$spec,$area);
 7026:         my %domdef = &get_domain_defaults($dcdom);
 7027:         if (ref($domdef{'adhocroles'}) eq 'HASH') {
 7028:             if (ref($domdef{'adhocroles'}{$rolename}) eq 'HASH') {
 7029:                 &appenv({'request.role.desc' => $domdef{'adhocroles'}{$rolename}{'desc'},});
 7030:             }
 7031:         }
 7032:     } else {
 7033:         &standard_roleprivs(\%rolehash,$role,$dcdom,$spec,$pickedcourse,$area);
 7034:     }
 7035:     my ($author,$adv,$rar)= &set_userprivs(\%userroles,\%rolehash);
 7036:     &appenv(\%userroles,[$role,'cm']);
 7037:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},"Role ".$spec);
 7038:     unless (($caller eq 'constructaccess' && $env{'request.course.id'}) ||
 7039:             ($caller eq 'tiny')) {
 7040:         &appenv( {'request.role'        => $spec,
 7041:                   'request.role.domain' => $dcdom,
 7042:                   'request.course.sec'  => $sec,
 7043:                  }
 7044:                );
 7045:         my $tadv=0;
 7046:         if (&allowed('adv') eq 'F') { $tadv=1; }
 7047:         &appenv({'request.role.adv'    => $tadv});
 7048:     }
 7049: }
 7050: 
 7051: # --------------------------------------------------------------- get interface
 7052: 
 7053: sub get {
 7054:    my ($namespace,$storearr,$udomain,$uname)=@_;
 7055:    my $items='';
 7056:    foreach my $item (@$storearr) {
 7057:        $items.=&escape($item).'&';
 7058:    }
 7059:    $items=~s/\&$//;
 7060:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7061:    if (!$uname) { $uname=$env{'user.name'}; }
 7062:    my $uhome=&homeserver($uname,$udomain);
 7063: 
 7064:    my $rep=&reply("get:$udomain:$uname:$namespace:$items",$uhome);
 7065:    my @pairs=split(/\&/,$rep);
 7066:    if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
 7067:      return @pairs;
 7068:    }
 7069:    my %returnhash=();
 7070:    my $i=0;
 7071:    foreach my $item (@$storearr) {
 7072:       $returnhash{$item}=&thaw_unescape($pairs[$i]);
 7073:       $i++;
 7074:    }
 7075:    return %returnhash;
 7076: }
 7077: 
 7078: # --------------------------------------------------------------- del interface
 7079: 
 7080: sub del {
 7081:    my ($namespace,$storearr,$udomain,$uname)=@_;
 7082:    my $items='';
 7083:    foreach my $item (@$storearr) {
 7084:        $items.=&escape($item).'&';
 7085:    }
 7086: 
 7087:    $items=~s/\&$//;
 7088:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7089:    if (!$uname) { $uname=$env{'user.name'}; }
 7090:    my $uhome=&homeserver($uname,$udomain);
 7091:    return &reply("del:$udomain:$uname:$namespace:$items",$uhome);
 7092: }
 7093: 
 7094: # -------------------------------------------------------------- dump interface
 7095: 
 7096: sub unserialize {
 7097:     my ($rep, $escapedkeys) = @_;
 7098: 
 7099:     return {} if $rep =~ /^error/;
 7100: 
 7101:     my %returnhash=();
 7102: 	foreach my $item (split(/\&/,$rep)) {
 7103: 	    my ($key, $value) = split(/=/, $item, 2);
 7104: 	    $key = unescape($key) unless $escapedkeys;
 7105: 	    next if $key =~ /^error: 2 /;
 7106: 	    $returnhash{$key} = &thaw_unescape($value);
 7107: 	}
 7108:     #return %returnhash;
 7109:     return \%returnhash;
 7110: }        
 7111: 
 7112: # see Lond::dump_with_regexp
 7113: # if $escapedkeys hash keys won't get unescaped.
 7114: sub dump {
 7115:     my ($namespace,$udomain,$uname,$regexp,$range,$escapedkeys)=@_;
 7116:     if (!$udomain) { $udomain=$env{'user.domain'}; }
 7117:     if (!$uname) { $uname=$env{'user.name'}; }
 7118:     my $uhome=&homeserver($uname,$udomain);
 7119: 
 7120:     if ($regexp) {
 7121:         $regexp=&escape($regexp);
 7122:     } else {
 7123:         $regexp='.';
 7124:     }
 7125:     if (grep { $_ eq $uhome } current_machine_ids()) {
 7126:         # user is hosted on this machine
 7127:         my $reply = LONCAPA::Lond::dump_with_regexp(join(":", ($udomain,
 7128:                     $uname, $namespace, $regexp, $range)), $perlvar{'lonVersion'});
 7129:         return %{unserialize($reply, $escapedkeys)};
 7130:     }
 7131:     my $rep=&reply("dump:$udomain:$uname:$namespace:$regexp:$range",$uhome);
 7132:     my @pairs=split(/\&/,$rep);
 7133:     my %returnhash=();
 7134:     if (!($rep =~ /^error/ )) {
 7135: 	foreach my $item (@pairs) {
 7136: 	    my ($key,$value)=split(/=/,$item,2);
 7137:         $key = unescape($key) unless $escapedkeys;
 7138:         #$key = &unescape($key);
 7139: 	    next if ($key =~ /^error: 2 /);
 7140: 	    $returnhash{$key}=&thaw_unescape($value);
 7141: 	}
 7142:     }
 7143:     return %returnhash;
 7144: }
 7145: 
 7146: 
 7147: # --------------------------------------------------------- dumpstore interface
 7148: 
 7149: sub dumpstore {
 7150:    my ($namespace,$udomain,$uname,$regexp,$range)=@_;
 7151:    # same as dump but keys must be escaped. They may contain colon separated
 7152:    # lists of values that may themself contain colons (e.g. symbs).
 7153:    return &dump($namespace, $udomain, $uname, $regexp, $range, 1);
 7154: }
 7155: 
 7156: # -------------------------------------------------------------- keys interface
 7157: 
 7158: sub getkeys {
 7159:    my ($namespace,$udomain,$uname)=@_;
 7160:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7161:    if (!$uname) { $uname=$env{'user.name'}; }
 7162:    my $uhome=&homeserver($uname,$udomain);
 7163:    my $rep=reply("keys:$udomain:$uname:$namespace",$uhome);
 7164:    my @keyarray=();
 7165:    foreach my $key (split(/\&/,$rep)) {
 7166:       next if ($key =~ /^error: 2 /);
 7167:       push(@keyarray,&unescape($key));
 7168:    }
 7169:    return @keyarray;
 7170: }
 7171: 
 7172: # --------------------------------------------------------------- currentdump
 7173: sub currentdump {
 7174:    my ($courseid,$sdom,$sname)=@_;
 7175:    $courseid = $env{'request.course.id'} if (! defined($courseid));
 7176:    $sdom     = $env{'user.domain'}       if (! defined($sdom));
 7177:    $sname    = $env{'user.name'}         if (! defined($sname));
 7178:    my $uhome = &homeserver($sname,$sdom);
 7179:    my $rep;
 7180: 
 7181:    if (grep { $_ eq $uhome } current_machine_ids()) {
 7182:        $rep = LONCAPA::Lond::dump_profile_database(join(":", ($sdom, $sname, 
 7183:                    $courseid)));
 7184:    } else {
 7185:        $rep = reply('currentdump:'.$sdom.':'.$sname.':'.$courseid,$uhome);
 7186:    }
 7187: 
 7188:    return if ($rep =~ /^(error:|no_such_host)/);
 7189:    #
 7190:    my %returnhash=();
 7191:    #
 7192:    if ($rep eq 'unknown_cmd') {
 7193:        # an old lond will not know currentdump
 7194:        # Do a dump and make it look like a currentdump
 7195:        my @tmp = &dumpstore($courseid,$sdom,$sname,'.');
 7196:        return if ($tmp[0] =~ /^(error:|no_such_host)/);
 7197:        my %hash = @tmp;
 7198:        @tmp=();
 7199:        %returnhash = %{&convert_dump_to_currentdump(\%hash)};
 7200:    } else {
 7201:        my @pairs=split(/\&/,$rep);
 7202:        foreach my $pair (@pairs) {
 7203:            my ($key,$value)=split(/=/,$pair,2);
 7204:            my ($symb,$param) = split(/:/,$key);
 7205:            $returnhash{&unescape($symb)}->{&unescape($param)} = 
 7206:                                                         &thaw_unescape($value);
 7207:        }
 7208:    }
 7209:    return %returnhash;
 7210: }
 7211: 
 7212: sub convert_dump_to_currentdump{
 7213:     my %hash = %{shift()};
 7214:     my %returnhash;
 7215:     # Code ripped from lond, essentially.  The only difference
 7216:     # here is the unescaping done by lonnet::dump().  Conceivably
 7217:     # we might run in to problems with parameter names =~ /^v\./
 7218:     while (my ($key,$value) = each(%hash)) {
 7219:         my ($v,$symb,$param) = split(/:/,$key);
 7220: 	$symb  = &unescape($symb);
 7221: 	$param = &unescape($param);
 7222:         next if ($v eq 'version' || $symb eq 'keys');
 7223:         next if (exists($returnhash{$symb}) &&
 7224:                  exists($returnhash{$symb}->{$param}) &&
 7225:                  $returnhash{$symb}->{'v.'.$param} > $v);
 7226:         $returnhash{$symb}->{$param}=$value;
 7227:         $returnhash{$symb}->{'v.'.$param}=$v;
 7228:     }
 7229:     #
 7230:     # Remove all of the keys in the hashes which keep track of
 7231:     # the version of the parameter.
 7232:     while (my ($symb,$param_hash) = each(%returnhash)) {
 7233:         # use a foreach because we are going to delete from the hash.
 7234:         foreach my $key (keys(%$param_hash)) {
 7235:             delete($param_hash->{$key}) if ($key =~ /^v\./);
 7236:         }
 7237:     }
 7238:     return \%returnhash;
 7239: }
 7240: 
 7241: # ------------------------------------------------------ critical inc interface
 7242: 
 7243: sub cinc {
 7244:     return &inc(@_,'critical');
 7245: }
 7246: 
 7247: # --------------------------------------------------------------- inc interface
 7248: 
 7249: sub inc {
 7250:     my ($namespace,$store,$udomain,$uname,$critical) = @_;
 7251:     if (!$udomain) { $udomain=$env{'user.domain'}; }
 7252:     if (!$uname) { $uname=$env{'user.name'}; }
 7253:     my $uhome=&homeserver($uname,$udomain);
 7254:     my $items='';
 7255:     if (! ref($store)) {
 7256:         # got a single value, so use that instead
 7257:         $items = &escape($store).'=&';
 7258:     } elsif (ref($store) eq 'SCALAR') {
 7259:         $items = &escape($$store).'=&';        
 7260:     } elsif (ref($store) eq 'ARRAY') {
 7261:         $items = join('=&',map {&escape($_);} @{$store});
 7262:     } elsif (ref($store) eq 'HASH') {
 7263:         while (my($key,$value) = each(%{$store})) {
 7264:             $items.= &escape($key).'='.&escape($value).'&';
 7265:         }
 7266:     }
 7267:     $items=~s/\&$//;
 7268:     if ($critical) {
 7269: 	return &critical("inc:$udomain:$uname:$namespace:$items",$uhome);
 7270:     } else {
 7271: 	return &reply("inc:$udomain:$uname:$namespace:$items",$uhome);
 7272:     }
 7273: }
 7274: 
 7275: # --------------------------------------------------------------- put interface
 7276: 
 7277: sub put {
 7278:    my ($namespace,$storehash,$udomain,$uname)=@_;
 7279:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7280:    if (!$uname) { $uname=$env{'user.name'}; }
 7281:    my $uhome=&homeserver($uname,$udomain);
 7282:    my $items='';
 7283:    foreach my $item (keys(%$storehash)) {
 7284:        $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
 7285:    }
 7286:    $items=~s/\&$//;
 7287:    return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
 7288: }
 7289: 
 7290: # ------------------------------------------------------------ newput interface
 7291: 
 7292: sub newput {
 7293:    my ($namespace,$storehash,$udomain,$uname)=@_;
 7294:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7295:    if (!$uname) { $uname=$env{'user.name'}; }
 7296:    my $uhome=&homeserver($uname,$udomain);
 7297:    my $items='';
 7298:    foreach my $key (keys(%$storehash)) {
 7299:        $items.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
 7300:    }
 7301:    $items=~s/\&$//;
 7302:    return &reply("newput:$udomain:$uname:$namespace:$items",$uhome);
 7303: }
 7304: 
 7305: # ---------------------------------------------------------  putstore interface
 7306: 
 7307: sub putstore {
 7308:    my ($namespace,$symb,$version,$storehash,$udomain,$uname,$tolog)=@_;
 7309:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7310:    if (!$uname) { $uname=$env{'user.name'}; }
 7311:    my $uhome=&homeserver($uname,$udomain);
 7312:    my $items='';
 7313:    foreach my $key (keys(%$storehash)) {
 7314:        $items.= &escape($key).'='.&freeze_escape($storehash->{$key}).'&';
 7315:    }
 7316:    $items=~s/\&$//;
 7317:    my $esc_symb=&escape($symb);
 7318:    my $esc_v=&escape($version);
 7319:    my $reply =
 7320:        &reply("putstore:$udomain:$uname:$namespace:$esc_symb:$esc_v:$items",
 7321: 	      $uhome);
 7322:    if (($tolog) && ($reply eq 'ok')) {
 7323:        my $namevalue='';
 7324:        foreach my $key (keys(%{$storehash})) {
 7325:            $namevalue.=&escape($key).'='.&freeze_escape($storehash->{$key}).'&';
 7326:        }
 7327:        my $ip = &get_requestor_ip();
 7328:        $namevalue .= 'ip='.&escape($ip).
 7329:                      '&host='.&escape($perlvar{'lonHostID'}).
 7330:                      '&version='.$esc_v.
 7331:                      '&by='.&escape($env{'user.name'}.':'.$env{'user.domain'});
 7332:        &Apache::lonnet::courselog($symb.':'.$uname.':'.$udomain.':PUTSTORE:'.$namevalue);
 7333:    }
 7334:    if ($reply eq 'unknown_cmd') {
 7335:        # gfall back to way things use to be done
 7336:        return &old_putstore($namespace,$symb,$version,$storehash,$udomain,
 7337: 			    $uname);
 7338:    }
 7339:    return $reply;
 7340: }
 7341: 
 7342: sub old_putstore {
 7343:     my ($namespace,$symb,$version,$storehash,$udomain,$uname)=@_;
 7344:     if (!$udomain) { $udomain=$env{'user.domain'}; }
 7345:     if (!$uname) { $uname=$env{'user.name'}; }
 7346:     my $uhome=&homeserver($uname,$udomain);
 7347:     my %newstorehash;
 7348:     foreach my $item (keys(%$storehash)) {
 7349: 	my $key = $version.':'.&escape($symb).':'.$item;
 7350: 	$newstorehash{$key} = $storehash->{$item};
 7351:     }
 7352:     my $items='';
 7353:     my %allitems = ();
 7354:     foreach my $item (keys(%newstorehash)) {
 7355: 	if ($item =~ m/^([^\:]+):([^\:]+):([^\:]+)$/) {
 7356: 	    my $key = $1.':keys:'.$2;
 7357: 	    $allitems{$key} .= $3.':';
 7358: 	}
 7359: 	$items.=$item.'='.&freeze_escape($newstorehash{$item}).'&';
 7360:     }
 7361:     foreach my $item (keys(%allitems)) {
 7362: 	$allitems{$item} =~ s/\:$//;
 7363: 	$items.= $item.'='.$allitems{$item}.'&';
 7364:     }
 7365:     $items=~s/\&$//;
 7366:     return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
 7367: }
 7368: 
 7369: # ------------------------------------------------------ critical put interface
 7370: 
 7371: sub cput {
 7372:    my ($namespace,$storehash,$udomain,$uname)=@_;
 7373:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7374:    if (!$uname) { $uname=$env{'user.name'}; }
 7375:    my $uhome=&homeserver($uname,$udomain);
 7376:    my $items='';
 7377:    foreach my $item (keys(%$storehash)) {
 7378:        $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
 7379:    }
 7380:    $items=~s/\&$//;
 7381:    return &critical("put:$udomain:$uname:$namespace:$items",$uhome);
 7382: }
 7383: 
 7384: # -------------------------------------------------------------- eget interface
 7385: 
 7386: sub eget {
 7387:    my ($namespace,$storearr,$udomain,$uname)=@_;
 7388:    my $items='';
 7389:    foreach my $item (@$storearr) {
 7390:        $items.=&escape($item).'&';
 7391:    }
 7392:    $items=~s/\&$//;
 7393:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7394:    if (!$uname) { $uname=$env{'user.name'}; }
 7395:    my $uhome=&homeserver($uname,$udomain);
 7396:    my $rep=&reply("eget:$udomain:$uname:$namespace:$items",$uhome);
 7397:    my @pairs=split(/\&/,$rep);
 7398:    my %returnhash=();
 7399:    my $i=0;
 7400:    foreach my $item (@$storearr) {
 7401:       $returnhash{$item}=&thaw_unescape($pairs[$i]);
 7402:       $i++;
 7403:    }
 7404:    return %returnhash;
 7405: }
 7406: 
 7407: # ------------------------------------------------------------ tmpput interface
 7408: sub tmpput {
 7409:     my ($storehash,$server,$context)=@_;
 7410:     my $items='';
 7411:     foreach my $item (keys(%$storehash)) {
 7412: 	$items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
 7413:     }
 7414:     $items=~s/\&$//;
 7415:     if (defined($context)) {
 7416:         $items .= ':'.&escape($context);
 7417:     }
 7418:     return &reply("tmpput:$items",$server);
 7419: }
 7420: 
 7421: # ------------------------------------------------------------ tmpget interface
 7422: sub tmpget {
 7423:     my ($token,$server)=@_;
 7424:     if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
 7425:     my $rep=&reply("tmpget:$token",$server);
 7426:     my %returnhash;
 7427:     if ($rep =~ /^(con_lost|error|no_such_host)/i) {
 7428:         return %returnhash;
 7429:     }
 7430:     foreach my $item (split(/\&/,$rep)) {
 7431: 	my ($key,$value)=split(/=/,$item);
 7432: 	$returnhash{&unescape($key)}=&thaw_unescape($value);
 7433:     }
 7434:     return %returnhash;
 7435: }
 7436: 
 7437: # ------------------------------------------------------------ tmpdel interface
 7438: sub tmpdel {
 7439:     my ($token,$server)=@_;
 7440:     if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
 7441:     return &reply("tmpdel:$token",$server);
 7442: }
 7443: 
 7444: # ------------------------------------------------------------ get_timebased_id 
 7445: 
 7446: sub get_timebased_id {
 7447:     my ($prefix,$keyid,$namespace,$cdom,$cnum,$idtype,$who,$locktries,
 7448:         $maxtries) = @_;
 7449:     my ($newid,$error,$dellock);
 7450:     unless (($prefix =~ /^\w+$/) && ($keyid =~ /^\w+$/) && ($namespace ne '')) {  
 7451:         return ('','ok','invalid call to get suffix');
 7452:     }
 7453: 
 7454: # set defaults for any optional args for which values were not supplied
 7455:     if ($who eq '') {
 7456:         $who = $env{'user.name'}.':'.$env{'user.domain'};
 7457:     }
 7458:     if (!$locktries) {
 7459:         $locktries = 3;
 7460:     }
 7461:     if (!$maxtries) {
 7462:         $maxtries = 10;
 7463:     }
 7464:     
 7465:     if (($cdom eq '') || ($cnum eq '')) {
 7466:         if ($env{'request.course.id'}) {
 7467:             $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 7468:             $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 7469:         }
 7470:         if (($cdom eq '') || ($cnum eq '')) {
 7471:             return ('','ok','call to get suffix not in course context');
 7472:         }
 7473:     }
 7474: 
 7475: # construct locking item
 7476:     my $lockhash = {
 7477:                       $prefix."\0".'locked_'.$keyid => $who,
 7478:                    };
 7479:     my $tries = 0;
 7480: 
 7481: # attempt to get lock on nohist_$namespace file
 7482:     my $gotlock = &Apache::lonnet::newput('nohist_'.$namespace,$lockhash,$cdom,$cnum);
 7483:     while (($gotlock ne 'ok') && $tries <$locktries) {
 7484:         $tries ++;
 7485:         sleep 1;
 7486:         $gotlock = &Apache::lonnet::newput('nohist_'.$namespace,$lockhash,$cdom,$cnum);
 7487:     }
 7488: 
 7489: # attempt to get unique identifier, based on current timestamp
 7490:     if ($gotlock eq 'ok') {
 7491:         my %inuse = &Apache::lonnet::dump('nohist_'.$namespace,$cdom,$cnum,$prefix);
 7492:         my $id = time;
 7493:         $newid = $id;
 7494:         if ($idtype eq 'addcode') {
 7495:             $newid .= &sixnum_code();
 7496:         }
 7497:         my $idtries = 0;
 7498:         while (exists($inuse{$prefix."\0".$newid}) && $idtries < $maxtries) {
 7499:             if ($idtype eq 'concat') {
 7500:                 $newid = $id.$idtries;
 7501:             } elsif ($idtype eq 'addcode') {
 7502:                 $newid = $newid.&sixnum_code();
 7503:             } else {
 7504:                 $newid ++;
 7505:             }
 7506:             $idtries ++;
 7507:         }
 7508:         if (!exists($inuse{$prefix."\0".$newid})) {
 7509:             my %new_item =  (
 7510:                               $prefix."\0".$newid => $who,
 7511:                             );
 7512:             my $putresult = &Apache::lonnet::put('nohist_'.$namespace,\%new_item,
 7513:                                                  $cdom,$cnum);
 7514:             if ($putresult ne 'ok') {
 7515:                 undef($newid);
 7516:                 $error = 'error saving new item: '.$putresult;
 7517:             }
 7518:         } else {
 7519:              undef($newid);
 7520:              $error = ('error: no unique suffix available for the new item ');
 7521:         }
 7522: #  remove lock
 7523:         my @del_lock = ($prefix."\0".'locked_'.$keyid);
 7524:         $dellock = &Apache::lonnet::del('nohist_'.$namespace,\@del_lock,$cdom,$cnum);
 7525:     } else {
 7526:         $error = "error: could not obtain lockfile\n";
 7527:         $dellock = 'ok';
 7528:         if (($prefix eq 'paste') && ($namespace eq 'courseeditor') && ($keyid eq 'num')) {
 7529:             $dellock = 'nolock';
 7530:         }
 7531:     }
 7532:     return ($newid,$dellock,$error);
 7533: }
 7534: 
 7535: sub sixnum_code {
 7536:     my $code;
 7537:     for (0..6) {
 7538:         $code .= int( rand(9) );
 7539:     }
 7540:     return $code;
 7541: }
 7542: 
 7543: # -------------------------------------------------- portfolio access checking
 7544: 
 7545: sub portfolio_access {
 7546:     my ($requrl,$clientip) = @_;
 7547:     my (undef,$udom,$unum,$file_name,$group) = &parse_portfolio_url($requrl);
 7548:     my $result = &get_portfolio_access($udom,$unum,$file_name,$group,$clientip);
 7549:     if ($result) {
 7550:         my %setters;
 7551:         if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
 7552:             my ($startblock,$endblock) =
 7553:                 &Apache::loncommon::blockcheck(\%setters,'port',$unum,$udom);
 7554:             if ($startblock && $endblock) {
 7555:                 return 'B';
 7556:             }
 7557:         } else {
 7558:             my ($startblock,$endblock) =
 7559:                 &Apache::loncommon::blockcheck(\%setters,'port');
 7560:             if ($startblock && $endblock) {
 7561:                 return 'B';
 7562:             }
 7563:         }
 7564:     }
 7565:     if ($result eq 'ok') {
 7566:        return 'F';
 7567:     } elsif ($result =~ /^[^:]+:guest_/) {
 7568:        return 'A';
 7569:     }
 7570:     return '';
 7571: }
 7572: 
 7573: sub get_portfolio_access {
 7574:     my ($udom,$unum,$file_name,$group,$clientip,$access_hash) = @_;
 7575: 
 7576:     if (!ref($access_hash)) {
 7577: 	my $current_perms = &get_portfile_permissions($udom,$unum);
 7578: 	my %access_controls = &get_access_controls($current_perms,$group,
 7579: 						   $file_name);
 7580: 	$access_hash = $access_controls{$file_name};
 7581:     }
 7582: 
 7583:     my ($public,$guest,@domains,@users,@courses,@groups,@ips);
 7584:     my $now = time;
 7585:     if (ref($access_hash) eq 'HASH') {
 7586:         foreach my $key (keys(%{$access_hash})) {
 7587:             my ($num,$scope,$end,$start) = ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
 7588:             if ($start > $now) {
 7589:                 next;
 7590:             }
 7591:             if ($end && $end<$now) {
 7592:                 next;
 7593:             }
 7594:             if ($scope eq 'public') {
 7595:                 $public = $key;
 7596:                 last;
 7597:             } elsif ($scope eq 'guest') {
 7598:                 $guest = $key;
 7599:             } elsif ($scope eq 'domains') {
 7600:                 push(@domains,$key);
 7601:             } elsif ($scope eq 'users') {
 7602:                 push(@users,$key);
 7603:             } elsif ($scope eq 'course') {
 7604:                 push(@courses,$key);
 7605:             } elsif ($scope eq 'group') {
 7606:                 push(@groups,$key);
 7607:             } elsif ($scope eq 'ip') {
 7608:                 push(@ips,$key);
 7609:             }
 7610:         }
 7611:         if ($public) {
 7612:             return 'ok';
 7613:         } elsif (@ips > 0) {
 7614:             my $allowed;
 7615:             foreach my $ipkey (@ips) {
 7616:                 if (ref($access_hash->{$ipkey}{'ip'}) eq 'ARRAY') {
 7617:                     if (&Apache::loncommon::check_ip_acc(join(',',@{$access_hash->{$ipkey}{'ip'}}),$clientip)) {
 7618:                         $allowed = 1;
 7619:                         last; 
 7620:                     }
 7621:                 }
 7622:             }
 7623:             if ($allowed) {
 7624:                 return 'ok';
 7625:             }
 7626:         }
 7627:         if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
 7628:             if ($guest) {
 7629:                 return $guest;
 7630:             }
 7631:         } else {
 7632:             if (@domains > 0) {
 7633:                 foreach my $domkey (@domains) {
 7634:                     if (ref($access_hash->{$domkey}{'dom'}) eq 'ARRAY') {
 7635:                         if (grep(/^\Q$env{'user.domain'}\E$/,@{$access_hash->{$domkey}{'dom'}})) {
 7636:                             return 'ok';
 7637:                         }
 7638:                     }
 7639:                 }
 7640:             }
 7641:             if (@users > 0) {
 7642:                 foreach my $userkey (@users) {
 7643:                     if (ref($access_hash->{$userkey}{'users'}) eq 'ARRAY') {
 7644:                         foreach my $item (@{$access_hash->{$userkey}{'users'}}) {
 7645:                             if (ref($item) eq 'HASH') {
 7646:                                 if (($item->{'uname'} eq $env{'user.name'}) &&
 7647:                                     ($item->{'udom'} eq $env{'user.domain'})) {
 7648:                                     return 'ok';
 7649:                                 }
 7650:                             }
 7651:                         }
 7652:                     } 
 7653:                 }
 7654:             }
 7655:             my %roleshash;
 7656:             my @courses_and_groups = @courses;
 7657:             push(@courses_and_groups,@groups); 
 7658:             if (@courses_and_groups > 0) {
 7659:                 my (%allgroups,%allroles); 
 7660:                 my ($start,$end,$role,$sec,$group);
 7661:                 foreach my $envkey (%env) {
 7662:                     if ($envkey =~ m-^user\.role\.(gr|cc|co|in|ta|ep|ad|st)\./($match_domain)/($match_courseid)/?([^/]*)$-) {
 7663:                         my $cid = $2.'_'.$3; 
 7664:                         if ($1 eq 'gr') {
 7665:                             $group = $4;
 7666:                             $allgroups{$cid}{$group} = $env{$envkey};
 7667:                         } else {
 7668:                             if ($4 eq '') {
 7669:                                 $sec = 'none';
 7670:                             } else {
 7671:                                 $sec = $4;
 7672:                             }
 7673:                             $allroles{$cid}{$1}{$sec} = $env{$envkey};
 7674:                         }
 7675:                     } elsif ($envkey =~ m-^user\.role\./cr/($match_domain/$match_username/\w*)./($match_domain)/($match_courseid)/?([^/]*)$-) {
 7676:                         my $cid = $2.'_'.$3;
 7677:                         if ($4 eq '') {
 7678:                             $sec = 'none';
 7679:                         } else {
 7680:                             $sec = $4;
 7681:                         }
 7682:                         $allroles{$cid}{$1}{$sec} = $env{$envkey};
 7683:                     }
 7684:                 }
 7685:                 if (keys(%allroles) == 0) {
 7686:                     return;
 7687:                 }
 7688:                 foreach my $key (@courses_and_groups) {
 7689:                     my %content = %{$$access_hash{$key}};
 7690:                     my $cnum = $content{'number'};
 7691:                     my $cdom = $content{'domain'};
 7692:                     my $cid = $cdom.'_'.$cnum;
 7693:                     if (!exists($allroles{$cid})) {
 7694:                         next;
 7695:                     }    
 7696:                     foreach my $role_id (keys(%{$content{'roles'}})) {
 7697:                         my @sections = @{$content{'roles'}{$role_id}{'section'}};
 7698:                         my @groups = @{$content{'roles'}{$role_id}{'group'}};
 7699:                         my @status = @{$content{'roles'}{$role_id}{'access'}};
 7700:                         my @roles = @{$content{'roles'}{$role_id}{'role'}};
 7701:                         foreach my $role (keys(%{$allroles{$cid}})) {
 7702:                             if ((grep/^all$/,@roles) || (grep/^\Q$role\E$/,@roles)) {
 7703:                                 foreach my $sec (keys(%{$allroles{$cid}{$role}})) {
 7704:                                     if (&course_group_datechecker($allroles{$cid}{$role}{$sec},$now,\@status) eq 'ok') {
 7705:                                         if (grep/^all$/,@sections) {
 7706:                                             return 'ok';
 7707:                                         } else {
 7708:                                             if (grep/^$sec$/,@sections) {
 7709:                                                 return 'ok';
 7710:                                             }
 7711:                                         }
 7712:                                     }
 7713:                                 }
 7714:                                 if (keys(%{$allgroups{$cid}}) == 0) {
 7715:                                     if (grep/^none$/,@groups) {
 7716:                                         return 'ok';
 7717:                                     }
 7718:                                 } else {
 7719:                                     if (grep/^all$/,@groups) {
 7720:                                         return 'ok';
 7721:                                     } 
 7722:                                     foreach my $group (keys(%{$allgroups{$cid}})) {
 7723:                                         if (grep/^$group$/,@groups) {
 7724:                                             return 'ok';
 7725:                                         }
 7726:                                     }
 7727:                                 } 
 7728:                             }
 7729:                         }
 7730:                     }
 7731:                 }
 7732:             }
 7733:             if ($guest) {
 7734:                 return $guest;
 7735:             }
 7736:         }
 7737:     }
 7738:     return;
 7739: }
 7740: 
 7741: sub course_group_datechecker {
 7742:     my ($dates,$now,$status) = @_;
 7743:     my ($start,$end) = split(/\./,$dates);
 7744:     if (!$start && !$end) {
 7745:         return 'ok';
 7746:     }
 7747:     if (grep/^active$/,@{$status}) {
 7748:         if (((!$start) || ($start && $start <= $now)) && ((!$end) || ($end && $end >= $now))) {
 7749:             return 'ok';
 7750:         }
 7751:     }
 7752:     if (grep/^previous$/,@{$status}) {
 7753:         if ($end > $now ) {
 7754:             return 'ok';
 7755:         }
 7756:     }
 7757:     if (grep/^future$/,@{$status}) {
 7758:         if ($start > $now) {
 7759:             return 'ok';
 7760:         }
 7761:     }
 7762:     return; 
 7763: }
 7764: 
 7765: sub parse_portfolio_url {
 7766:     my ($url) = @_;
 7767: 
 7768:     my ($type,$udom,$unum,$group,$file_name);
 7769:     
 7770:     if ($url =~  m-^/*(?:uploaded|editupload)/($match_domain)/($match_username)/portfolio(/.+)$-) {
 7771: 	$type = 1;
 7772:         $udom = $1;
 7773:         $unum = $2;
 7774:         $file_name = $3;
 7775:     } elsif ($url =~ m-^/*(?:uploaded|editupload)/($match_domain)/($match_courseid)/groups/([^/]+)/portfolio/(.+)$-) {
 7776: 	$type = 2;
 7777:         $udom = $1;
 7778:         $unum = $2;
 7779:         $group = $3;
 7780:         $file_name = $3.'/'.$4;
 7781:     }
 7782:     if (wantarray) {
 7783: 	return ($type,$udom,$unum,$file_name,$group);
 7784:     }
 7785:     return $type;
 7786: }
 7787: 
 7788: sub is_portfolio_url {
 7789:     my ($url) = @_;
 7790:     return scalar(&parse_portfolio_url($url));
 7791: }
 7792: 
 7793: sub is_portfolio_file {
 7794:     my ($file) = @_;
 7795:     if (($file =~ /^portfolio/) || ($file =~ /^groups\/\w+\/portfolio/)) {
 7796:         return 1;
 7797:     }
 7798:     return;
 7799: }
 7800: 
 7801: sub usertools_access {
 7802:     my ($uname,$udom,$tool,$action,$context,$userenvref,$domdefref,$is_advref)=@_;
 7803:     my ($access,%tools);
 7804:     if ($context eq '') {
 7805:         $context = 'tools';
 7806:     }
 7807:     if ($context eq 'requestcourses') {
 7808:         %tools = (
 7809:                       official   => 1,
 7810:                       unofficial => 1,
 7811:                       community  => 1,
 7812:                       textbook   => 1,
 7813:                       placement  => 1,
 7814:                       lti        => 1,
 7815:                  );
 7816:     } elsif ($context eq 'requestauthor') {
 7817:         %tools = (
 7818:                       requestauthor => 1,
 7819:                  );
 7820:     } else {
 7821:         %tools = (
 7822:                       aboutme   => 1,
 7823:                       blog      => 1,
 7824:                       webdav    => 1,
 7825:                       portfolio => 1,
 7826:                  );
 7827:     }
 7828:     return if (!defined($tools{$tool}));
 7829: 
 7830:     if (($udom eq '') || ($uname eq '')) {
 7831:         $udom = $env{'user.domain'};
 7832:         $uname = $env{'user.name'};
 7833:     }
 7834: 
 7835:     if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
 7836:         if ($action ne 'reload') {
 7837:             if ($context eq 'requestcourses') {
 7838:                 return $env{'environment.canrequest.'.$tool};
 7839:             } elsif ($context eq 'requestauthor') {
 7840:                 return $env{'environment.canrequest.author'};
 7841:             } else {
 7842:                 return $env{'environment.availabletools.'.$tool};
 7843:             }
 7844:         }
 7845:     }
 7846: 
 7847:     my ($toolstatus,$inststatus,$envkey);
 7848:     if ($context eq 'requestauthor') {
 7849:         $envkey = $context; 
 7850:     } else {
 7851:         $envkey = $context.'.'.$tool;
 7852:     }
 7853: 
 7854:     if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'}) &&
 7855:          ($action ne 'reload')) {
 7856:         $toolstatus = $env{'environment.'.$envkey};
 7857:         $inststatus = $env{'environment.inststatus'};
 7858:     } else {
 7859:         if (ref($userenvref) eq 'HASH') {
 7860:             $toolstatus = $userenvref->{$envkey};
 7861:             $inststatus = $userenvref->{'inststatus'};
 7862:         } else {
 7863:             my %userenv = &userenvironment($udom,$uname,$envkey,'inststatus');
 7864:             $toolstatus = $userenv{$envkey};
 7865:             $inststatus = $userenv{'inststatus'};
 7866:         }
 7867:     }
 7868: 
 7869:     if ($toolstatus ne '') {
 7870:         if ($toolstatus) {
 7871:             $access = 1;
 7872:         } else {
 7873:             $access = 0;
 7874:         }
 7875:         return $access;
 7876:     }
 7877: 
 7878:     my ($is_adv,%domdef);
 7879:     if (ref($is_advref) eq 'HASH') {
 7880:         $is_adv = $is_advref->{'is_adv'};
 7881:     } else {
 7882:         $is_adv = &is_advanced_user($udom,$uname);
 7883:     }
 7884:     if (ref($domdefref) eq 'HASH') {
 7885:         %domdef = %{$domdefref};
 7886:     } else {
 7887:         %domdef = &get_domain_defaults($udom);
 7888:     }
 7889:     if (ref($domdef{$tool}) eq 'HASH') {
 7890:         if ($is_adv) {
 7891:             if ($domdef{$tool}{'_LC_adv'} ne '') {
 7892:                 if ($domdef{$tool}{'_LC_adv'}) { 
 7893:                     $access = 1;
 7894:                 } else {
 7895:                     $access = 0;
 7896:                 }
 7897:                 return $access;
 7898:             }
 7899:         }
 7900:         if ($inststatus ne '') {
 7901:             my ($hasaccess,$hasnoaccess);
 7902:             foreach my $affiliation (split(/:/,$inststatus)) {
 7903:                 if ($domdef{$tool}{$affiliation} ne '') { 
 7904:                     if ($domdef{$tool}{$affiliation}) {
 7905:                         $hasaccess = 1;
 7906:                     } else {
 7907:                         $hasnoaccess = 1;
 7908:                     }
 7909:                 }
 7910:             }
 7911:             if ($hasaccess || $hasnoaccess) {
 7912:                 if ($hasaccess) {
 7913:                     $access = 1;
 7914:                 } elsif ($hasnoaccess) {
 7915:                     $access = 0; 
 7916:                 }
 7917:                 return $access;
 7918:             }
 7919:         } else {
 7920:             if ($domdef{$tool}{'default'} ne '') {
 7921:                 if ($domdef{$tool}{'default'}) {
 7922:                     $access = 1;
 7923:                 } elsif ($domdef{$tool}{'default'} == 0) {
 7924:                     $access = 0;
 7925:                 }
 7926:                 return $access;
 7927:             }
 7928:         }
 7929:     } else {
 7930:         if (($context eq 'tools') && ($tool ne 'webdav')) {
 7931:             $access = 1;
 7932:         } else {
 7933:             $access = 0;
 7934:         }
 7935:         return $access;
 7936:     }
 7937: }
 7938: 
 7939: sub is_course_owner {
 7940:     my ($cdom,$cnum,$udom,$uname) = @_;
 7941:     if (($udom eq '') || ($uname eq '')) {
 7942:         $udom = $env{'user.domain'};
 7943:         $uname = $env{'user.name'};
 7944:     }
 7945:     unless (($udom eq '') || ($uname eq '')) {
 7946:         if (exists($env{'course.'.$cdom.'_'.$cnum.'.internal.courseowner'})) {
 7947:             if ($env{'course.'.$cdom.'_'.$cnum.'.internal.courseowner'} eq $uname.':'.$udom) {
 7948:                 return 1;
 7949:             } else {
 7950:                 my %courseinfo = &Apache::lonnet::coursedescription($cdom.'/'.$cnum);
 7951:                 if ($courseinfo{'internal.courseowner'} eq $uname.':'.$udom) {
 7952:                     return 1;
 7953:                 }
 7954:             }
 7955:         }
 7956:     }
 7957:     return;
 7958: }
 7959: 
 7960: sub is_advanced_user {
 7961:     my ($udom,$uname) = @_;
 7962:     if ($udom ne '' && $uname ne '') {
 7963:         if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
 7964:             if (wantarray) {
 7965:                 return ($env{'user.adv'},$env{'user.author'});
 7966:             } else {
 7967:                 return $env{'user.adv'};
 7968:             }
 7969:         }
 7970:     }
 7971:     my %roleshash = &get_my_roles($uname,$udom,'userroles',undef,undef,undef,1);
 7972:     my %allroles;
 7973:     my ($is_adv,$is_author);
 7974:     foreach my $role (keys(%roleshash)) {
 7975:         my ($trest,$tdomain,$trole,$sec) = split(/:/,$role);
 7976:         my $area = '/'.$tdomain.'/'.$trest;
 7977:         if ($sec ne '') {
 7978:             $area .= '/'.$sec;
 7979:         }
 7980:         if (($area ne '') && ($trole ne '')) {
 7981:             my $spec=$trole.'.'.$area;
 7982:             if ($trole =~ /^cr\//) {
 7983:                 &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
 7984:             } elsif ($trole ne 'gr') {
 7985:                 &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
 7986:             }
 7987:             if ($trole eq 'au') {
 7988:                 $is_author = 1;
 7989:             }
 7990:         }
 7991:     }
 7992:     foreach my $role (keys(%allroles)) {
 7993:         last if ($is_adv);
 7994:         foreach my $item (split(/:/,$allroles{$role})) {
 7995:             if ($item ne '') {
 7996:                 my ($privilege,$restrictions)=split(/&/,$item);
 7997:                 if ($privilege eq 'adv') {
 7998:                     $is_adv = 1;
 7999:                     last;
 8000:                 }
 8001:             }
 8002:         }
 8003:     }
 8004:     if (wantarray) {
 8005:         return ($is_adv,$is_author);
 8006:     }
 8007:     return $is_adv;
 8008: }
 8009: 
 8010: sub check_can_request {
 8011:     my ($dom,$can_request,$request_domains,$uname,$udom) = @_;
 8012:     my $canreq = 0;
 8013:     if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '')) {
 8014:         $uname = $env{'user.name'};
 8015:         $udom = $env{'user.domain'};
 8016:     }
 8017:     my ($types,$typename) = &Apache::loncommon::course_types();
 8018:     my @options = ('approval','validate','autolimit');
 8019:     my $optregex = join('|',@options);
 8020:     if ((ref($can_request) eq 'HASH') && (ref($types) eq 'ARRAY')) {
 8021:         foreach my $type (@{$types}) {
 8022:             if (&usertools_access($uname,$udom,$type,undef,
 8023:                                   'requestcourses')) {
 8024:                 $canreq ++;
 8025:                 if (ref($request_domains) eq 'HASH') {
 8026:                     push(@{$request_domains->{$type}},$udom);
 8027:                 }
 8028:                 if ($dom eq $udom) {
 8029:                     $can_request->{$type} = 1;
 8030:                 }
 8031:             }
 8032:             if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '') &&
 8033:                 ($env{'environment.reqcrsotherdom.'.$type} ne '')) {
 8034:                 my @curr = split(',',$env{'environment.reqcrsotherdom.'.$type});
 8035:                 if (@curr > 0) {
 8036:                     foreach my $item (@curr) {
 8037:                         if (ref($request_domains) eq 'HASH') {
 8038:                             my ($otherdom) = ($item =~ /^($match_domain):($optregex)(=?\d*)$/);
 8039:                             if ($otherdom ne '') {
 8040:                                 if (ref($request_domains->{$type}) eq 'ARRAY') {
 8041:                                     unless (grep(/^\Q$otherdom\E$/,@{$request_domains->{$type}})) {
 8042:                                         push(@{$request_domains->{$type}},$otherdom);
 8043:                                     }
 8044:                                 } else {
 8045:                                     push(@{$request_domains->{$type}},$otherdom);
 8046:                                 }
 8047:                             }
 8048:                         }
 8049:                     }
 8050:                     unless ($dom eq $env{'user.domain'}) {
 8051:                         $canreq ++;
 8052:                         if (grep(/^\Q$dom\E:($optregex)(=?\d*)$/,@curr)) {
 8053:                             $can_request->{$type} = 1;
 8054:                         }
 8055:                     }
 8056:                 }
 8057:             }
 8058:         }
 8059:     }
 8060:     return $canreq;
 8061: }
 8062: 
 8063: # ---------------------------------------------- Custom access rule evaluation
 8064: 
 8065: sub customaccess {
 8066:     my ($priv,$uri)=@_;
 8067:     my ($urole,$urealm)=split(/\./,$env{'request.role'},2);
 8068:     my (undef,$udom,$ucrs,$usec)=split(/\//,$urealm);
 8069:     $udom = &LONCAPA::clean_domain($udom);
 8070:     $ucrs = &LONCAPA::clean_username($ucrs);
 8071:     my $access=0;
 8072:     foreach my $right (split(/\s*\,\s*/,&metadata($uri,'rule_rights'))) {
 8073: 	my ($effect,$realm,$role,$type)=split(/\:/,$right);
 8074: 	if ($type eq 'user') {
 8075: 	    foreach my $scope (split(/\s*\,\s*/,$realm)) {
 8076: 		my ($tdom,$tuname)=split(m{/},$scope);
 8077: 		if ($tdom) {
 8078: 		    if ($tdom ne $env{'user.domain'}) { next; }
 8079: 		}
 8080: 		if ($tuname) {
 8081: 		    if ($tuname ne $env{'user.name'}) { next; }
 8082: 		}
 8083: 		$access=($effect eq 'allow');
 8084: 		last;
 8085: 	    }
 8086: 	} else {
 8087: 	    if ($role) {
 8088: 		if ($role ne $urole) { next; }
 8089: 	    }
 8090: 	    foreach my $scope (split(/\s*\,\s*/,$realm)) {
 8091: 		my ($tdom,$tcrs,$tsec)=split(/\_/,$scope);
 8092: 		if ($tdom) {
 8093: 		    if ($tdom ne $udom) { next; }
 8094: 		}
 8095: 		if ($tcrs) {
 8096: 		    if ($tcrs ne $ucrs) { next; }
 8097: 		}
 8098: 		if ($tsec) {
 8099: 		    if ($tsec ne $usec) { next; }
 8100: 		}
 8101: 		$access=($effect eq 'allow');
 8102: 		last;
 8103: 	    }
 8104: 	    if ($realm eq '' && $role eq '') {
 8105: 		$access=($effect eq 'allow');
 8106: 	    }
 8107: 	}
 8108:     }
 8109:     return $access;
 8110: }
 8111: 
 8112: # ------------------------------------------------- Check for a user privilege
 8113: 
 8114: sub allowed {
 8115:     my ($priv,$uri,$symb,$role,$clientip,$noblockcheck,$ignorecache)=@_;
 8116:     my $ver_orguri=$uri;
 8117:     $uri=&deversion($uri);
 8118:     my $orguri=$uri;
 8119:     $uri=&declutter($uri);
 8120: 
 8121:     if ($priv eq 'evb') {
 8122: # Evade communication block restrictions for specified role in a course
 8123:         if ($env{'user.priv.'.$role} =~/evb\&([^\:]*)/) {
 8124:             return $1;
 8125:         } else {
 8126:             return;
 8127:         }
 8128:     }
 8129: 
 8130:     if (defined($env{'allowed.'.$priv})) { return $env{'allowed.'.$priv}; }
 8131: # Free bre access to adm and meta resources
 8132:     if (((($uri=~/^adm\//) && ($uri !~ m{/(?:smppg|bulletinboard|viewclasslist|aboutme|ext\.tool)$})) 
 8133: 	 || (($uri=~/\.meta$/) && ($uri!~m|^uploaded/|) )) 
 8134: 	&& ($priv eq 'bre')) {
 8135: 	return 'F';
 8136:     }
 8137: 
 8138: # Free bre access to user's own portfolio contents
 8139:     my ($space,$domain,$name,@dir)=split('/',$uri);
 8140:     if (($space=~/^(uploaded|editupload)$/) && ($env{'user.name'} eq $name) && 
 8141: 	($env{'user.domain'} eq $domain) && ('portfolio' eq $dir[0])) {
 8142:         my %setters;
 8143:         my ($startblock,$endblock) = 
 8144:             &Apache::loncommon::blockcheck(\%setters,'port');
 8145:         if ($startblock && $endblock) {
 8146:             return 'B';
 8147:         } else {
 8148:             return 'F';
 8149:         }
 8150:     }
 8151: 
 8152: # bre access to group portfolio for rgf priv in group, or mdg or vcg in course.
 8153:     if (($space=~/^(uploaded|editupload)$/) && ($dir[0] eq 'groups') 
 8154:          && ($dir[2] eq 'portfolio') && ($priv eq 'bre')) {
 8155:         if (exists($env{'request.course.id'})) {
 8156:             my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 8157:             my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 8158:             if (($domain eq $cdom) && ($name eq $cnum)) {
 8159:                 my $courseprivid=$env{'request.course.id'};
 8160:                 $courseprivid=~s/\_/\//;
 8161:                 if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid
 8162:                     .'/'.$dir[1]} =~/rgf\&([^\:]*)/) {
 8163:                     return $1; 
 8164:                 } else {
 8165:                     if ($env{'request.course.sec'}) {
 8166:                         $courseprivid.='/'.$env{'request.course.sec'};
 8167:                     }
 8168:                     if ($env{'user.priv.'.$env{'request.role'}.'./'.
 8169:                         $courseprivid} =~/(mdg|vcg)\&([^\:]*)/) {
 8170:                         return $2;
 8171:                     }
 8172:                 }
 8173:             }
 8174:         }
 8175:     }
 8176: 
 8177: # Free bre to public access
 8178: 
 8179:     if ($priv eq 'bre') {
 8180:         my $copyright;
 8181:         unless ($uri =~ /ext\.tool/) {
 8182:             $copyright=&metadata($uri,'copyright');
 8183:         }
 8184: 	if (($copyright eq 'public') && (!$env{'request.course.id'})) { 
 8185:            return 'F'; 
 8186:         }
 8187:         if ($copyright eq 'priv') {
 8188:             $uri=~/([^\/]+)\/([^\/]+)\//;
 8189: 	    unless (($env{'user.name'} eq $2) && ($env{'user.domain'} eq $1)) {
 8190: 		return '';
 8191:             }
 8192:         }
 8193:         if ($copyright eq 'domain') {
 8194:             $uri=~/([^\/]+)\/([^\/]+)\//;
 8195: 	    unless (($env{'user.domain'} eq $1) ||
 8196:                  ($env{'course.'.$env{'request.course.id'}.'.domain'} eq $1)) {
 8197: 		return '';
 8198:             }
 8199:         }
 8200:         if ($env{'request.role'}=~ /li\.\//) {
 8201:             # Library role, so allow browsing of resources in this domain.
 8202:             return 'F';
 8203:         }
 8204:         if ($copyright eq 'custom') {
 8205: 	    unless (&customaccess($priv,$uri)) { return ''; }
 8206:         }
 8207:     }
 8208:     # Domain coordinator is trying to create a course
 8209:     if (($priv eq 'ccc') && ($env{'request.role'} =~ /^dc\./)) {
 8210:         # uri is the requested domain in this case.
 8211:         # comparison to 'request.role.domain' shows if the user has selected
 8212:         # a role of dc for the domain in question.
 8213:         return 'F' if ($uri eq $env{'request.role.domain'});
 8214:     }
 8215: 
 8216:     my $thisallowed='';
 8217:     my $statecond=0;
 8218:     my $courseprivid='';
 8219: 
 8220:     my $ownaccess;
 8221:     # Community Coordinator or Assistant Co-author browsing resource space.
 8222:     if (($priv eq 'bro') && ($env{'user.author'})) {
 8223:         if ($uri eq '') {
 8224:             $ownaccess = 1;
 8225:         } else {
 8226:             if (($env{'user.domain'} ne '') && ($env{'user.name'} ne '')) {
 8227:                 my $udom = $env{'user.domain'};
 8228:                 my $uname = $env{'user.name'};
 8229:                 if ($uri =~ m{^\Q$udom\E/?$}) {
 8230:                     $ownaccess = 1;
 8231:                 } elsif ($uri =~ m{^\Q$udom\E/\Q$uname\E/?}) {
 8232:                     unless ($uri =~ m{\.\./}) {
 8233:                         $ownaccess = 1;
 8234:                     }
 8235:                 } elsif (($udom ne 'public') && ($uname ne 'public')) {
 8236:                     my $now = time;
 8237:                     if ($uri =~ m{^([^/]+)/?$}) {
 8238:                         my $adom = $1;
 8239:                         foreach my $key (keys(%env)) {
 8240:                             if ($key =~ m{^user\.role\.(ca|aa)/\Q$adom\E}) {
 8241:                                 my ($start,$end) = split('.',$env{$key});
 8242:                                 if (($now >= $start) && (!$end || $end < $now)) {
 8243:                                     $ownaccess = 1;
 8244:                                     last;
 8245:                                 }
 8246:                             }
 8247:                         }
 8248:                     } elsif ($uri =~ m{^([^/]+)/([^/]+)/?}) {
 8249:                         my $adom = $1;
 8250:                         my $aname = $2;
 8251:                         foreach my $role ('ca','aa') { 
 8252:                             if ($env{"user.role.$role./$adom/$aname"}) {
 8253:                                 my ($start,$end) =
 8254:                                     split('.',$env{"user.role.$role./$adom/$aname"});
 8255:                                 if (($now >= $start) && (!$end || $end < $now)) {
 8256:                                     $ownaccess = 1;
 8257:                                     last;
 8258:                                 }
 8259:                             }
 8260:                         }
 8261:                     }
 8262:                 }
 8263:             }
 8264:         }
 8265:     }
 8266: 
 8267: # Course
 8268: 
 8269:     if ($env{'user.priv.'.$env{'request.role'}.'./'}=~/\Q$priv\E\&([^\:]*)/) {
 8270:         unless (($priv eq 'bro') && (!$ownaccess)) {
 8271:             $thisallowed.=$1;
 8272:         }
 8273:     }
 8274: 
 8275: # Domain
 8276: 
 8277:     if ($env{'user.priv.'.$env{'request.role'}.'./'.(split(/\//,$uri))[0].'/'}
 8278:        =~/\Q$priv\E\&([^\:]*)/) {
 8279:         unless (($priv eq 'bro') && (!$ownaccess)) {
 8280:             $thisallowed.=$1;
 8281:         }
 8282:     }
 8283: 
 8284: # User who is not author or co-author might still be able to edit
 8285: # resource of an author in the domain (e.g., if Domain Coordinator).
 8286:     if (($priv eq 'eco') && ($thisallowed eq '') && ($env{'request.course.id'}) &&
 8287:         (&allowed('mdc',$env{'request.course.id'}))) {
 8288:         if ($env{"user.priv.cm./$uri/"}=~/\Q$priv\E\&([^\:]*)/) {
 8289:             $thisallowed.=$1;
 8290:         }
 8291:     }
 8292: 
 8293: # Course: uri itself is a course
 8294:     my $courseuri=$uri;
 8295:     $courseuri=~s/\_(\d)/\/$1/;
 8296:     $courseuri=~s/^([^\/])/\/$1/;
 8297: 
 8298:     if ($env{'user.priv.'.$env{'request.role'}.'.'.$courseuri}
 8299:        =~/\Q$priv\E\&([^\:]*)/) {
 8300:         if ($priv eq 'mip') {
 8301:             my $rem = $1;
 8302:             if (($uri ne '') && ($env{'request.course.id'} eq $uri) &&
 8303:                 ($env{'course.'.$env{'request.course.id'}.'.internal.courseowner'} eq $env{'user.name'}.':'.$env{'user.domain'})) {
 8304:                 my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 8305:                 if ($cdom ne '') {
 8306:                     my %passwdconf = &get_passwdconf($cdom);
 8307:                     if (ref($passwdconf{'crsownerchg'}) eq 'HASH') {
 8308:                         if (ref($passwdconf{'crsownerchg'}{'by'}) eq 'ARRAY') {
 8309:                             if (@{$passwdconf{'crsownerchg'}{'by'}}) {
 8310:                                 my @inststatuses = split(':',$env{'environment.inststatus'});
 8311:                                 unless (@inststatuses) {
 8312:                                     @inststatuses = ('default');
 8313:                                 }
 8314:                                 foreach my $status (@inststatuses) {
 8315:                                     if (grep(/^\Q$status\E$/,@{$passwdconf{'crsownerchg'}{'by'}})) {
 8316:                                         $thisallowed.=$rem;
 8317:                                     }
 8318:                                 }
 8319:                             }
 8320:                         }
 8321:                     }
 8322:                 }
 8323:             }
 8324:         } else {
 8325:             unless (($priv eq 'bro') && (!$ownaccess)) {
 8326:                 $thisallowed.=$1;
 8327:             }
 8328:         }
 8329:     }
 8330: 
 8331: # URI is an uploaded document for this course, default permissions don't matter
 8332: # not allowing 'edit' access (editupload) to uploaded course docs
 8333:     if (($priv eq 'bre') && ($uri=~m|^uploaded/|)) {
 8334: 	$thisallowed='';
 8335:         my ($match)=&is_on_map($uri);
 8336:         if ($match) {
 8337:             if ($env{'user.priv.'.$env{'request.role'}.'./'}
 8338:                   =~/\Q$priv\E\&([^\:]*)/) {
 8339:                 my $value = $1;
 8340:                 my $deeplinkblock = &deeplink_check($priv,$symb,$uri);
 8341:                 if ($deeplinkblock) {
 8342:                     $thisallowed='D';
 8343:                 } elsif ($noblockcheck) {
 8344:                     $thisallowed.=$value;
 8345:                 } else {
 8346:                     my @blockers = &has_comm_blocking($priv,$symb,$uri,$ignorecache);
 8347:                     if (@blockers > 0) {
 8348:                         $thisallowed = 'B';
 8349:                     } else {
 8350:                         $thisallowed.=$value;
 8351:                     }
 8352:                 }
 8353:             }
 8354:         } else {
 8355:             my $refuri = $env{'httpref.'.$orguri} || $env{'httpref.'.$ver_orguri};
 8356:             if ($refuri) {
 8357:                 if ($refuri =~ m|^/adm/|) {
 8358:                     $thisallowed='F';
 8359:                 } else {
 8360:                     $refuri=&declutter($refuri);
 8361:                     my ($match) = &is_on_map($refuri);
 8362:                     if ($match) {
 8363:                         my $deeplinkblock = &deeplink_check($priv,$symb,$refuri);
 8364:                         if ($deeplinkblock) {
 8365:                             $thisallowed='D';
 8366:                         } elsif ($noblockcheck) {
 8367:                             $thisallowed='F';
 8368:                         } else {
 8369:                             my @blockers = &has_comm_blocking($priv,'',$refuri,'',1);
 8370:                             if (@blockers > 0) {
 8371:                                 $thisallowed = 'B';
 8372:                             } else {
 8373:                                 $thisallowed='F';
 8374:                             }
 8375:                         }
 8376:                     }
 8377:                 }
 8378:             }
 8379:         }
 8380:     }
 8381: 
 8382:     if ($priv eq 'bre'
 8383: 	&& $thisallowed ne 'F' 
 8384: 	&& $thisallowed ne '2'
 8385: 	&& &is_portfolio_url($uri)) {
 8386: 	$thisallowed = &portfolio_access($uri,$clientip);
 8387:     }
 8388: 
 8389: # Full access at system, domain or course-wide level? Exit.
 8390:     if ($thisallowed=~/F/) {
 8391: 	return 'F';
 8392:     }
 8393: 
 8394: # If this is generating or modifying users, exit with special codes
 8395: 
 8396:     if (':csu:cdc:ccc:cin:cta:cep:ccr:cst:cad:cli:cau:cdg:cca:caa:'=~/\:\Q$priv\E\:/) {
 8397: 	if (($priv eq 'cca') || ($priv eq 'caa')) {
 8398: 	    my ($audom,$auname)=split('/',$uri);
 8399: # no author name given, so this just checks on the general right to make a co-author in this domain
 8400: 	    unless ($auname) { return $thisallowed; }
 8401: # an author name is given, so we are about to actually make a co-author for a certain account
 8402: 	    if (($auname ne $env{'user.name'} && $env{'request.role'} !~ /^dc\./) ||
 8403: 		(($audom ne $env{'user.domain'} && $env{'request.role'} !~ /^dc\./) &&
 8404: 		 ($audom ne $env{'request.role.domain'}))) { return ''; }
 8405: 	}
 8406: 	return $thisallowed;
 8407:     }
 8408: #
 8409: # Gathered so far: system, domain and course wide privileges
 8410: #
 8411: # Course: See if uri or referer is an individual resource that is part of 
 8412: # the course
 8413: 
 8414:     if ($env{'request.course.id'}) {
 8415: 
 8416: # If this is modifying password (internal auth) domains must match for user and user's role.
 8417: 
 8418:         if ($priv eq 'mip') {
 8419:             if ($env{'user.domain'} eq $env{'request.role.domain'}) {
 8420:                 return $thisallowed;
 8421:             } else {
 8422:                 return '';
 8423:             }
 8424:         }
 8425: 
 8426:        $courseprivid=$env{'request.course.id'};
 8427:        if ($env{'request.course.sec'}) {
 8428:           $courseprivid.='/'.$env{'request.course.sec'};
 8429:        }
 8430:        $courseprivid=~s/\_/\//;
 8431:        my $checkreferer=1;
 8432:        my ($match,$cond)=&is_on_map($uri);
 8433:        if ($match) {
 8434:            $statecond=$cond;
 8435:            if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
 8436:                =~/\Q$priv\E\&([^\:]*)/) {
 8437:                my $value = $1;
 8438:                if ($priv eq 'bre') {
 8439:                    if ($noblockcheck) {
 8440:                        $thisallowed.=$value;
 8441:                    } else {
 8442:                        my @blockers = &has_comm_blocking($priv,$symb,$uri,$ignorecache);
 8443:                        if (@blockers > 0) {
 8444:                            $thisallowed = 'B';
 8445:                        } else {
 8446:                            $thisallowed.=$value;
 8447:                        }
 8448:                    }
 8449:                } else {
 8450:                    $thisallowed.=$value;
 8451:                }
 8452:                $checkreferer=0;
 8453:            }
 8454:        }
 8455: 
 8456:        if ($checkreferer) {
 8457: 	  my $refuri=$env{'httpref.'.$orguri};
 8458:             unless ($refuri) {
 8459:                 foreach my $key (keys(%env)) {
 8460: 		    if ($key=~/^httpref\..*\*/) {
 8461: 			my $pattern=$key;
 8462:                         $pattern=~s/^httpref\.\/res\///;
 8463:                         $pattern=~s/\*/\[\^\/\]\+/g;
 8464:                         $pattern=~s/\//\\\//g;
 8465:                         if ($orguri=~/$pattern/) {
 8466: 			    $refuri=$env{$key};
 8467:                         }
 8468:                     }
 8469:                 }
 8470:             }
 8471: 
 8472:          if ($refuri) { 
 8473: 	  $refuri=&declutter($refuri);
 8474:           my ($match,$cond)=&is_on_map($refuri);
 8475:             if ($match) {
 8476:               my $refstatecond=$cond;
 8477:               if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
 8478:                   =~/\Q$priv\E\&([^\:]*)/) {
 8479:                   my $value = $1;
 8480:                   if ($priv eq 'bre') {
 8481:                       my $deeplinkblock = &deeplink_check($priv,$symb,$refuri);
 8482:                       if ($deeplinkblock) {
 8483:                           $thisallowed = 'D';
 8484:                       } elsif ($noblockcheck) {
 8485:                           $thisallowed.=$value;
 8486:                       } else {
 8487:                           my @blockers = &has_comm_blocking($priv,'',$refuri,'',1);
 8488:                           if (@blockers > 0) {
 8489:                               $thisallowed = 'B';
 8490:                           } else {
 8491:                               $thisallowed.=$value;
 8492:                           }
 8493:                       }
 8494:                   } else {
 8495:                       $thisallowed.=$value;
 8496:                   }
 8497:                   $uri=$refuri;
 8498:                   $statecond=$refstatecond;
 8499:               }
 8500:           }
 8501:         }
 8502:        }
 8503:    }
 8504: 
 8505: #
 8506: # Gathered now: all privileges that could apply, and condition number
 8507: # 
 8508: #
 8509: # Full or no access?
 8510: #
 8511: 
 8512:     if ($thisallowed=~/F/) {
 8513: 	return 'F';
 8514:     }
 8515: 
 8516:     unless ($thisallowed) {
 8517:         return '';
 8518:     }
 8519: 
 8520: # Restrictions exist, deal with them
 8521: #
 8522: #   C:according to course preferences
 8523: #   R:according to resource settings
 8524: #   L:unless locked
 8525: #   X:according to user session state
 8526: #
 8527: 
 8528: # Possibly locked functionality, check all courses
 8529: # Locks might take effect only after 10 minutes cache expiration for other
 8530: # courses, and 2 minutes for current course
 8531: 
 8532:     my $envkey;
 8533:     if ($thisallowed=~/L/) {
 8534:         foreach $envkey (keys(%env)) {
 8535:            if ($envkey=~/^user\.role\.(st|ta)\.([^\.]*)/) {
 8536:                my $courseid=$2;
 8537:                my $roleid=$1.'.'.$2;
 8538:                $courseid=~s/^\///;
 8539:                my $expiretime=600;
 8540:                if ($env{'request.role'} eq $roleid) {
 8541: 		  $expiretime=120;
 8542:                }
 8543: 	       my ($cdom,$cnum,$csec)=split(/\//,$courseid);
 8544:                my $prefix='course.'.$cdom.'_'.$cnum.'.';
 8545:                if ((time-$env{$prefix.'last_cache'})>$expiretime) {
 8546: 		   &coursedescription($courseid,{'freshen_cache' => 1});
 8547:                }
 8548:                if (($env{$prefix.'res.'.$uri.'.lock.sections'}=~/\,\Q$csec\E\,/)
 8549:                 || ($env{$prefix.'res.'.$uri.'.lock.sections'} eq 'all')) {
 8550: 		   if ($env{$prefix.'res.'.$uri.'.lock.expire'}>time) {
 8551:                        &log($env{'user.domain'},$env{'user.name'},
 8552:                             $env{'user.home'},
 8553:                             'Locked by res: '.$priv.' for '.$uri.' due to '.
 8554:                             $cdom.'/'.$cnum.'/'.$csec.' expire '.
 8555:                             $env{$prefix.'priv.'.$priv.'.lock.expire'});
 8556: 		       return '';
 8557:                    }
 8558:                }
 8559:                if (($env{$prefix.'priv.'.$priv.'.lock.sections'}=~/\,\Q$csec\E\,/)
 8560:                 || ($env{$prefix.'priv.'.$priv.'.lock.sections'} eq 'all')) {
 8561: 		   if ($env{'priv.'.$priv.'.lock.expire'}>time) {
 8562:                        &log($env{'user.domain'},$env{'user.name'},
 8563:                             $env{'user.home'},
 8564:                             'Locked by priv: '.$priv.' for '.$uri.' due to '.
 8565:                             $cdom.'/'.$cnum.'/'.$csec.' expire '.
 8566:                             $env{$prefix.'priv.'.$priv.'.lock.expire'});
 8567: 		       return '';
 8568:                    }
 8569:                }
 8570: 	   }
 8571:        }
 8572:     }
 8573: 
 8574: #
 8575: # Rest of the restrictions depend on selected course
 8576: #
 8577: 
 8578:     unless ($env{'request.course.id'}) {
 8579: 	if ($thisallowed eq 'A') {
 8580: 	    return 'A';
 8581:         } elsif ($thisallowed eq 'B') {
 8582:             return 'B';
 8583: 	} else {
 8584: 	    return '1';
 8585: 	}
 8586:     }
 8587: 
 8588: #
 8589: # Now user is definitely in a course
 8590: #
 8591: 
 8592: 
 8593: # Course preferences
 8594: 
 8595:    if ($thisallowed=~/C/) {
 8596:        my $rolecode=(split(/\./,$env{'request.role'}))[0];
 8597:        my $unamedom=$env{'user.name'}.':'.$env{'user.domain'};
 8598:        if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.roles.denied'}
 8599: 	   =~/\Q$rolecode\E/) {
 8600: 	   if (($priv ne 'pch') && ($priv ne 'plc') && ($priv ne 'pac')) {
 8601: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
 8602: 			'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode.' in '.
 8603: 			$env{'request.course.id'});
 8604: 	   }
 8605:            return '';
 8606:        }
 8607: 
 8608:        if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.users.denied'}
 8609: 	   =~/\Q$unamedom\E/) {
 8610: 	   if (($priv ne 'pch') && ($priv ne 'plc') && ($priv ne 'pac')) {
 8611: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.
 8612: 			'Denied by user: '.$priv.' for '.$uri.' as '.$unamedom.' in '.
 8613: 			$env{'request.course.id'});
 8614: 	   }
 8615:            return '';
 8616:        }
 8617:    }
 8618: 
 8619: # Resource preferences
 8620: 
 8621:    if ($thisallowed=~/R/) {
 8622:        my $rolecode=(split(/\./,$env{'request.role'}))[0];
 8623:        if (&metadata($uri,'roledeny')=~/\Q$rolecode\E/) {
 8624: 	   if (($priv ne 'pch') && ($priv ne 'plc')) { 
 8625: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
 8626: 			'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode);
 8627: 	   }
 8628: 	   return '';
 8629:        }
 8630:    }
 8631: 
 8632: # Restricted by state or randomout?
 8633: 
 8634:    if ($thisallowed=~/X/) {
 8635:       if ($env{'acc.randomout'}) {
 8636: 	 if (!$symb) { $symb=&symbread($uri,1); }
 8637:          if (($symb) && ($env{'acc.randomout'}=~/\&\Q$symb\E\&/)) { 
 8638:             return ''; 
 8639:          }
 8640:       }
 8641:       if (&condval($statecond)) {
 8642: 	 return '2';
 8643:       } else {
 8644:          return '';
 8645:       }
 8646:    }
 8647: 
 8648:     if ($thisallowed eq 'A') {
 8649: 	return 'A';
 8650:     } elsif ($thisallowed eq 'B') {
 8651:         return 'B';
 8652:     } elsif ($thisallowed eq 'D') {
 8653:         return 'D';
 8654:     }
 8655:    return 'F';
 8656: }
 8657: 
 8658: # ------------------------------------------- Check construction space access
 8659: 
 8660: sub constructaccess {
 8661:     my ($url,$setpriv)=@_;
 8662: 
 8663: # We do not allow editing of previous versions of files
 8664:     if ($url=~/\.(\d+)\.(\w+)$/) { return ''; }
 8665: 
 8666: # Get username and domain from URL
 8667:     my ($ownername,$ownerdomain,$ownerhome);
 8668: 
 8669:     ($ownerdomain,$ownername) =
 8670:         ($url=~ m{^(?:\Q$perlvar{'lonDocRoot'}\E|)(?:/daxepage|/daxeopen)?/priv/($match_domain)/($match_username)(?:/|$)});
 8671: 
 8672: # The URL does not really point to any authorspace, forget it
 8673:     unless (($ownername) && ($ownerdomain)) { return ''; }
 8674: 
 8675: # Now we need to see if the user has access to the authorspace of
 8676: # $ownername at $ownerdomain
 8677: 
 8678:     if (($ownername eq $env{'user.name'}) && ($ownerdomain eq $env{'user.domain'})) {
 8679: # Real author for this?
 8680:        $ownerhome = $env{'user.home'};
 8681:        if (exists($env{'user.priv.au./'.$ownerdomain.'/./'})) {
 8682:           return ($ownername,$ownerdomain,$ownerhome);
 8683:        }
 8684:     } else {
 8685: # Co-author for this?
 8686:         if (exists($env{'user.priv.ca./'.$ownerdomain.'/'.$ownername.'./'}) ||
 8687:             exists($env{'user.priv.aa./'.$ownerdomain.'/'.$ownername.'./'}) ) {
 8688:             $ownerhome = &homeserver($ownername,$ownerdomain);
 8689:             return ($ownername,$ownerdomain,$ownerhome);
 8690:         }
 8691:         if ($env{'request.course.id'}) {
 8692:             if (($ownername eq $env{'course.'.$env{'request.course.id'}.'.num'}) &&
 8693:                 ($ownerdomain eq $env{'course.'.$env{'request.course.id'}.'.domain'})) {
 8694:                 if (&allowed('mdc',$env{'request.course.id'})) {
 8695:                     $ownerhome = $env{'course.'.$env{'request.course.id'}.'.home'};
 8696:                     return ($ownername,$ownerdomain,$ownerhome);
 8697:                 }
 8698:             }
 8699:         }
 8700:     }
 8701: 
 8702: # We don't have any access right now. If we are not possibly going to do anything about this,
 8703: # we might as well leave
 8704:    unless ($setpriv) { return ''; }
 8705: 
 8706: # Backdoor access?
 8707:     my $allowed=&allowed('eco',$ownerdomain);
 8708: # Nope
 8709:     unless ($allowed) { return ''; }
 8710: # Looks like we may have access, but could be locked by the owner of the construction space
 8711:     if ($allowed eq 'U') {
 8712:         my %blocked=&get('environment',['domcoord.author'],
 8713:                          $ownerdomain,$ownername);
 8714: # Is blocked by owner
 8715:         if ($blocked{'domcoord.author'} eq 'blocked') { return ''; }
 8716:     }
 8717:     if (($allowed eq 'F') || ($allowed eq 'U')) {
 8718: # Grant temporary access
 8719:         my $then=$env{'user.login.time'};
 8720:         my $update=$env{'user.update.time'};
 8721:         if (!$update) { $update = $then; }
 8722:         my $refresh=$env{'user.refresh.time'};
 8723:         if (!$refresh) { $refresh = $update; }
 8724:         my $now = time;
 8725:         &check_adhoc_privs($ownerdomain,$ownername,$update,$refresh,
 8726:                            $now,'ca','constructaccess');
 8727:         $ownerhome = &homeserver($ownername,$ownerdomain);
 8728:         return($ownername,$ownerdomain,$ownerhome);
 8729:     }
 8730: # No business here
 8731:     return '';
 8732: }
 8733: 
 8734: # ----------------------------------------------------------- Content Blocking
 8735: 
 8736: {
 8737: # Caches for faster Course Contents display where content blocking
 8738: # is in operation (i.e., interval param set) for timed quiz.
 8739: #
 8740: # User for whom data are being temporarily cached.
 8741: my $cacheduser='';
 8742: # Course for which data are being temporarily cached.
 8743: my $cachedcid='';
 8744: # Cached blockers for this user (a hash of blocking items). 
 8745: my %cachedblockers=();
 8746: # When the data were last cached.
 8747: my $cachedlast='';
 8748: 
 8749: sub load_all_blockers {
 8750:     my ($uname,$udom)=@_;
 8751:     if (($uname ne '') && ($udom ne '')) { 
 8752:         if (($cacheduser eq $uname.':'.$udom) &&
 8753:             ($cachedcid eq $env{'request.course.id'}) &&
 8754:             (abs($cachedlast-time)<5)) {
 8755:             return;
 8756:         }
 8757:     }
 8758:     $cachedlast=time;
 8759:     $cacheduser=$uname.':'.$udom;
 8760:     $cachedcid=$env{'request.course.id'};
 8761:     %cachedblockers = &get_commblock_resources();
 8762:     return;
 8763: }
 8764: 
 8765: sub get_comm_blocks {
 8766:     my ($cdom,$cnum) = @_;
 8767:     if ($cdom eq '' || $cnum eq '') {
 8768:         return unless ($env{'request.course.id'});
 8769:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 8770:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 8771:     }
 8772:     my %commblocks;
 8773:     my $hashid=$cdom.'_'.$cnum;
 8774:     my ($blocksref,$cached)=&is_cached_new('comm_block',$hashid);
 8775:     if ((defined($cached)) && (ref($blocksref) eq 'HASH')) {
 8776:         %commblocks = %{$blocksref};
 8777:     } else {
 8778:         %commblocks = &Apache::lonnet::dump('comm_block',$cdom,$cnum);
 8779:         my $cachetime = 600;
 8780:         &do_cache_new('comm_block',$hashid,\%commblocks,$cachetime);
 8781:     }
 8782:     return %commblocks;
 8783: }
 8784: 
 8785: sub get_commblock_resources {
 8786:     my ($blocks) = @_;
 8787:     my %blockers = ();
 8788:     return %blockers unless ($env{'request.course.id'});
 8789:     return %blockers if ($env{'user.priv.'.$env{'request.role'}} =~/evb\&([^\:]*)/);
 8790:     my %commblocks;
 8791:     if (ref($blocks) eq 'HASH') {
 8792:         %commblocks = %{$blocks};
 8793:     } else {
 8794:         %commblocks = &get_comm_blocks();
 8795:     }
 8796:     return %blockers unless (keys(%commblocks) > 0); 
 8797:     my $navmap = Apache::lonnavmaps::navmap->new();
 8798:     return %blockers unless (ref($navmap));
 8799:     my $now = time;
 8800:     foreach my $block (keys(%commblocks)) {
 8801:         if ($block =~ /^(\d+)____(\d+)$/) {
 8802:             my ($start,$end) = ($1,$2);
 8803:             if ($start <= $now && $end >= $now) {
 8804:                 if (ref($commblocks{$block}{'blocks'}) eq 'HASH') {
 8805:                     if (ref($commblocks{$block}{'blocks'}{'docs'}) eq 'HASH') {
 8806:                         if (ref($commblocks{$block}{'blocks'}{'docs'}{'maps'}) eq 'HASH') {
 8807:                             if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'maps'}})) {
 8808:                                 $blockers{$block}{maps} = $commblocks{$block}{'blocks'}{'docs'}{'maps'}; 
 8809:                             }
 8810:                         }
 8811:                         if (ref($commblocks{$block}{'blocks'}{'docs'}{'resources'}) eq 'HASH') {
 8812:                             if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'resources'}})) {
 8813:                                 $blockers{$block}{'resources'} = $commblocks{$block}{'blocks'}{'docs'}{'resources'};
 8814:                             }
 8815:                         }
 8816:                     }
 8817:                 }
 8818:             }
 8819:         } elsif ($block =~ /^firstaccess____(.+)$/) {
 8820:             my $item = $1;
 8821:             my @to_test;
 8822:             if (ref($commblocks{$block}{'blocks'}) eq 'HASH') {
 8823:                 if (ref($commblocks{$block}{'blocks'}{'docs'}) eq 'HASH') {
 8824:                     my @interval;
 8825:                     my $type = 'map';
 8826:                     if ($item eq 'course') {
 8827:                         $type = 'course';
 8828:                         @interval=&EXT("resource.0.interval");
 8829:                     } else {
 8830:                         if ($item =~ /___\d+___/) {
 8831:                             $type = 'resource';
 8832:                             @interval=&EXT("resource.0.interval",$item);
 8833:                             if (ref($navmap)) {                        
 8834:                                 my $res = $navmap->getBySymb($item); 
 8835:                                 push(@to_test,$res);
 8836:                             }
 8837:                         } else {
 8838:                             my $mapsymb = &symbread($item,1);
 8839:                             if ($mapsymb) {
 8840:                                 if (ref($navmap)) {
 8841:                                     my $mapres = $navmap->getBySymb($mapsymb);
 8842:                                     if (ref($mapres)) {
 8843:                                         my $first = $mapres->map_start();
 8844:                                         my $finish = $mapres->map_finish();
 8845:                                         my $it = $navmap->getIterator($first,$finish,undef,0,0);
 8846:                                         if (ref($it)) {
 8847:                                             my $res;
 8848:                                             while ($res = $it->next(undef,1)) {
 8849:                                                 next unless (ref($res));
 8850:                                                 my $symb = $res->symb();
 8851:                                                 next if (($symb eq $mapsymb) || ($symb eq ''));
 8852:                                                 @interval=&EXT("resource.0.interval",$symb);
 8853:                                                 if ($interval[1] eq 'map') {
 8854:                                                     if ($res->answerable()) {
 8855:                                                         push(@to_test,$res);
 8856:                                                         last;
 8857:                                                     }
 8858:                                                 }
 8859:                                             }
 8860:                                         }
 8861:                                     }
 8862:                                 }
 8863:                             }
 8864:                         }
 8865:                     }
 8866:                     if ($interval[0] =~ /^(\d+)/) {
 8867:                         my $timelimit = $1; 
 8868:                         my $first_access;
 8869:                         if ($type eq 'resource') {
 8870:                             $first_access=&get_first_access($interval[1],$item);
 8871:                         } elsif ($type eq 'map') {
 8872:                             $first_access=&get_first_access($interval[1],undef,$item);
 8873:                         } else {
 8874:                             $first_access=&get_first_access($interval[1]);
 8875:                         }
 8876:                         if ($first_access) {
 8877:                             my $timesup = $first_access+$timelimit;
 8878:                             if ($timesup > $now) {
 8879:                                 my $activeblock;
 8880:                                 foreach my $res (@to_test) {
 8881:                                     if ($res->answerable()) {
 8882:                                         $activeblock = 1;
 8883:                                         last;
 8884:                                     }
 8885:                                 }
 8886:                                 if ($activeblock) {
 8887:                                     if (ref($commblocks{$block}{'blocks'}{'docs'}{'maps'}) eq 'HASH') {
 8888:                                          if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'maps'}})) {
 8889:                                              $blockers{$block}{'maps'} = $commblocks{$block}{'blocks'}{'docs'}{'maps'};
 8890:                                          }
 8891:                                     }
 8892:                                     if (ref($commblocks{$block}{'blocks'}{'docs'}{'resources'}) eq 'HASH') {
 8893:                                         if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'resources'}})) {
 8894:                                             $blockers{$block}{'resources'} = $commblocks{$block}{'blocks'}{'docs'}{'resources'};
 8895:                                         }
 8896:                                     }
 8897:                                 }
 8898:                             }
 8899:                         }
 8900:                     }
 8901:                 }
 8902:             }
 8903:         }
 8904:     }
 8905:     return %blockers;
 8906: }
 8907: 
 8908: sub has_comm_blocking {
 8909:     my ($priv,$symb,$uri,$ignoresymbdb,$noenccheck,$blocked,$blocks) = @_;
 8910:     my @blockers;
 8911:     return unless ($env{'request.course.id'});
 8912:     return unless ($priv eq 'bre');
 8913:     return if ($env{'user.priv.'.$env{'request.role'}} =~/evb\&([^\:]*)/);
 8914:     return if ($env{'request.state'} eq 'construct');
 8915:     my %blockinfo;
 8916:     if (ref($blocks) eq 'HASH') {
 8917:         %blockinfo = &get_commblock_resources($blocks);
 8918:     } else {
 8919:         &load_all_blockers($env{'user.name'},$env{'user.domain'});
 8920:         %blockinfo = %cachedblockers;
 8921:     }
 8922:     return unless (keys(%blockinfo) > 0);
 8923:     my (%possibles,@symbs);
 8924:     if (!$symb) {
 8925:         $symb = &symbread($uri,1,1,1,\%possibles,$ignoresymbdb,$noenccheck);
 8926:     }
 8927:     if ($symb) {
 8928:         @symbs = ($symb);
 8929:     } elsif (keys(%possibles)) { 
 8930:         @symbs = keys(%possibles);
 8931:     }
 8932:     my $noblock;
 8933:     foreach my $symb (@symbs) {
 8934:         last if ($noblock);
 8935:         my ($map,$resid,$resurl)=&decode_symb($symb);
 8936:         foreach my $block (keys(%blockinfo)) {
 8937:             if ($block =~ /^firstaccess____(.+)$/) {
 8938:                 my $item = $1;
 8939:                 unless ($blocked) {
 8940:                     if (($item eq $map) || ($item eq $symb)) {
 8941:                         $noblock = 1;
 8942:                         last;
 8943:                     }
 8944:                 }
 8945:             }
 8946:             if (ref($blockinfo{$block}) eq 'HASH') {
 8947:                 if (ref($blockinfo{$block}{'resources'}) eq 'HASH') {
 8948:                     if ($blockinfo{$block}{'resources'}{$symb}) {
 8949:                         unless (grep(/^\Q$block\E$/,@blockers)) {
 8950:                             push(@blockers,$block);
 8951:                         }
 8952:                     }
 8953:                 }
 8954:                 if (ref($blockinfo{$block}{'maps'}) eq 'HASH') {
 8955:                     if ($blockinfo{$block}{'maps'}{$map}) {
 8956:                         unless (grep(/^\Q$block\E$/,@blockers)) {
 8957:                             push(@blockers,$block);
 8958:                         }
 8959:                     }
 8960:                 }
 8961:             }
 8962:         }
 8963:     }
 8964:     unless ($noblock) { 
 8965:         return @blockers;
 8966:     }
 8967:     return;
 8968: }
 8969: }
 8970: 
 8971: sub deeplink_check {
 8972:     my ($priv,$symb,$uri) = @_;
 8973:     return unless ($env{'request.course.id'});
 8974:     return unless ($priv eq 'bre');
 8975:     return if ($env{'request.state'} eq 'construct');
 8976:     return if ($env{'request.role.adv'});
 8977:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 8978:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 8979:     my (%possibles,@symbs);
 8980:     if (!$symb) {
 8981:         $symb = &symbread($uri,1,1,1,\%possibles);
 8982:     }
 8983:     if ($symb) {
 8984:         @symbs = ($symb);
 8985:     } elsif (keys(%possibles)) {
 8986:         @symbs = keys(%possibles);
 8987:     }
 8988: 
 8989:     my ($login,$switchrole,$allow);
 8990:     if ($env{'request.deeplink.login'} =~ m{^\Q/tiny/$cdom/\E(\w+)$}) {
 8991:         my $key = $1;
 8992:         my $tinyurl;
 8993:         my ($result,$cached)=&Apache::lonnet::is_cached_new('tiny',$cdom."\0".$key);
 8994:         if (defined($cached)) {
 8995:              $tinyurl = $result;
 8996:         } else {
 8997:              my $configuname = &Apache::lonnet::get_domainconfiguser($cdom);
 8998:              my %currtiny = &Apache::lonnet::get('tiny',[$key],$cdom,$configuname);
 8999:              if ($currtiny{$key} ne '') {
 9000:                  $tinyurl = $currtiny{$key};
 9001:                  &Apache::lonnet::do_cache_new('tiny',$cdom."\0".$key,$currtiny{$key},600);
 9002:              }
 9003:         }
 9004:         if ($tinyurl ne '') {
 9005:             my ($cnumreq,$posslogin) = split(/\&/,$tinyurl);
 9006:             if ($cnumreq eq $cnum) {
 9007:                 $login = $posslogin;
 9008:             } else {
 9009:                 $switchrole = 1;
 9010:             }
 9011:         }
 9012:     }
 9013:     foreach my $symb (@symbs) {
 9014:         last if ($allow);
 9015:         my $deeplink = &EXT("resource.0.deeplink",$symb);
 9016:         if ($deeplink eq '') {
 9017:             $allow = 1;
 9018:         } else {
 9019:             my ($listed,$scope,$access) = split(/,/,$deeplink);
 9020:             if ($access eq 'any') {
 9021:                 $allow = 1;
 9022:             } elsif ($login) {
 9023:                 if ($access eq 'only') {
 9024:                     if ($scope eq 'res') {
 9025:                         if ($symb eq $login) {
 9026:                             $allow = 1;
 9027:                         }
 9028:                     } elsif ($scope eq 'map') {
 9029: #FIXME Compare map for $env{'request.deeplink.login'} with map for $symb
 9030:                     } elsif ($scope eq 'rec') {
 9031: #FIXME Recurse up for $env{'request.deeplink.login'} with map for $symb
 9032:                     }
 9033:                 } else {
 9034:                     my ($acctype,$item) = split(/:/,$access);
 9035:                     if (($acctype eq 'lti') && ($env{'user.linkprotector'})) {
 9036:                         if (grep(/^\Q$item\E$/,split(/,/,$env{'user.linkprotector'}))) {
 9037:                             my %tinyurls = &get('tiny',[$symb],$cdom,$cnum);
 9038:                             if (grep(/\Q$tinyurls{$symb}\E$/,split(/,/,$env{'user.linkproturis'}))) {
 9039:                                 $allow = 1;
 9040:                             }
 9041:                         }
 9042:                     } elsif (($acctype eq 'key') && ($env{'user.deeplinkkey'})) {
 9043:                         if (grep(/^\Q$item\E$/,split(/,/,$env{'user.deeplinkkey'}))) {
 9044:                             my %tinyurls = &get('tiny',[$symb],$cdom,$cnum);
 9045:                             if (grep(/\Q$tinyurls{$symb}\E$/,split(/,/,$env{'user.keyedlinkuri'}))) {
 9046:                                 $allow = 1;
 9047:                             }
 9048:                         }
 9049:                     }
 9050:                 }
 9051:             }
 9052:         }
 9053:     }
 9054:     return if ($allow);
 9055:     return 1;
 9056: }
 9057: 
 9058: # -------------------------------- Deversion and split uri into path an filename   
 9059: 
 9060: #
 9061: #   Removes the version from a URI and
 9062: #   splits it in to its filename and path to the filename.
 9063: #   Seems like File::Basename could have done this more clearly.
 9064: #   Parameters:
 9065: #      $uri   - input URI
 9066: #   Returns:
 9067: #     Two element list consisting of 
 9068: #     $pathname  - the URI up to and excluding the trailing /
 9069: #     $filename  - The part of the URI following the last /
 9070: #  NOTE:
 9071: #    Another realization of this is simply:
 9072: #    use File::Basename;
 9073: #    ...
 9074: #    $uri = shift;
 9075: #    $filename = basename($uri);
 9076: #    $path     = dirname($uri);
 9077: #    return ($filename, $path);
 9078: #
 9079: #     The implementation below is probably faster however.
 9080: #
 9081: sub split_uri_for_cond {
 9082:     my $uri=&deversion(&declutter(shift));
 9083:     my @uriparts=split(/\//,$uri);
 9084:     my $filename=pop(@uriparts);
 9085:     my $pathname=join('/',@uriparts);
 9086:     return ($pathname,$filename);
 9087: }
 9088: # --------------------------------------------------- Is a resource on the map?
 9089: 
 9090: sub is_on_map {
 9091:     my ($pathname,$filename) = &split_uri_for_cond(shift);
 9092:     #Trying to find the conditional for the file
 9093:     my $match=($env{'acc.res.'.$env{'request.course.id'}.'.'.$pathname}=~
 9094: 	       /\&\Q$filename\E\:([\d\|]+)\&/);
 9095:     if ($match) {
 9096: 	return (1,$1);
 9097:     } else {
 9098: 	return (0,0);
 9099:     }
 9100: }
 9101: 
 9102: # --------------------------------------------------------- Get symb from alias
 9103: 
 9104: sub get_symb_from_alias {
 9105:     my $symb=shift;
 9106:     my ($map,$resid,$url)=&decode_symb($symb);
 9107: # Already is a symb
 9108:     if ($url) { return $symb; }
 9109: # Must be an alias
 9110:     my $aliassymb='';
 9111:     my %bighash;
 9112:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
 9113:                             &GDBM_READER(),0640)) {
 9114:         my $rid=$bighash{'mapalias_'.$symb};
 9115: 	if ($rid) {
 9116: 	    my ($mapid,$resid)=split(/\./,$rid);
 9117: 	    $aliassymb=&encode_symb($bighash{'map_id_'.$mapid},
 9118: 				    $resid,$bighash{'src_'.$rid});
 9119: 	}
 9120:         untie %bighash;
 9121:     }
 9122:     return $aliassymb;
 9123: }
 9124: 
 9125: # ----------------------------------------------------------------- Define Role
 9126: 
 9127: sub definerole {
 9128:   if (allowed('mcr','/')) {
 9129:     my ($rolename,$sysrole,$domrole,$courole,$uname,$udom)=@_;
 9130:     foreach my $role (split(':',$sysrole)) {
 9131: 	my ($crole,$cqual)=split(/\&/,$role);
 9132:         if ($pr{'cr:s'}!~/\Q$crole\E/) { return "refused:s:$crole"; }
 9133:         if ($pr{'cr:s'}=~/\Q$crole\E\&/) {
 9134: 	    if ($pr{'cr:s'}!~/\Q$crole\E\&\w*\Q$cqual\E/) { 
 9135:                return "refused:s:$crole&$cqual"; 
 9136:             }
 9137:         }
 9138:     }
 9139:     foreach my $role (split(':',$domrole)) {
 9140: 	my ($crole,$cqual)=split(/\&/,$role);
 9141:         if ($pr{'cr:d'}!~/\Q$crole\E/) { return "refused:d:$crole"; }
 9142:         if ($pr{'cr:d'}=~/\Q$crole\E\&/) {
 9143: 	    if ($pr{'cr:d'}!~/\Q$crole\W\&\w*\Q$cqual\E/) { 
 9144:                return "refused:d:$crole&$cqual"; 
 9145:             }
 9146:         }
 9147:     }
 9148:     foreach my $role (split(':',$courole)) {
 9149: 	my ($crole,$cqual)=split(/\&/,$role);
 9150:         if ($pr{'cr:c'}!~/\Q$crole\E/) { return "refused:c:$crole"; }
 9151:         if ($pr{'cr:c'}=~/\Q$crole\E\&/) {
 9152: 	    if ($pr{'cr:c'}!~/\Q$crole\E\&\w*\Q$cqual\E/) { 
 9153:                return "refused:c:$crole&$cqual"; 
 9154:             }
 9155:         }
 9156:     }
 9157:     my $uhome;
 9158:     if (($uname ne '') && ($udom ne '')) {
 9159:         $uhome = &homeserver($uname,$udom);
 9160:         return $uhome if ($uhome eq 'no_host');
 9161:     } else {
 9162:         $uname = $env{'user.name'};
 9163:         $udom = $env{'user.domain'};
 9164:         $uhome = $env{'user.home'};
 9165:     }
 9166:     my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
 9167:                 "$udom:$uname:rolesdef_$rolename=".
 9168:                 escape($sysrole.'_'.$domrole.'_'.$courole);
 9169:     return reply($command,$uhome);
 9170:   } else {
 9171:     return 'refused';
 9172:   }
 9173: }
 9174: 
 9175: # ---------------- Make a metadata query against the network of library servers
 9176: 
 9177: sub metadata_query {
 9178:     my ($query,$custom,$customshow,$server_array,$domains_hash)=@_;
 9179:     my %rhash;
 9180:     my %libserv = &all_library();
 9181:     my @server_list = (defined($server_array) ? @$server_array
 9182:                                               : keys(%libserv) );
 9183:     for my $server (@server_list) {
 9184:         my $domains = ''; 
 9185:         if (ref($domains_hash) eq 'HASH') {
 9186:             $domains = $domains_hash->{$server}; 
 9187:         }
 9188: 	unless ($custom or $customshow) {
 9189: 	    my $reply=&reply("querysend:".&escape($query).':::'.&escape($domains),$server);
 9190: 	    $rhash{$server}=$reply;
 9191: 	}
 9192: 	else {
 9193: 	    my $reply=&reply("querysend:".&escape($query).':'.
 9194: 			     &escape($custom).':'.&escape($customshow).':'.&escape($domains),
 9195: 			     $server);
 9196: 	    $rhash{$server}=$reply;
 9197: 	}
 9198:     }
 9199:     return \%rhash;
 9200: }
 9201: 
 9202: # ----------------------------------------- Send log queries and wait for reply
 9203: 
 9204: sub log_query {
 9205:     my ($uname,$udom,$query,%filters)=@_;
 9206:     my $uhome=&homeserver($uname,$udom);
 9207:     if ($uhome eq 'no_host') { return 'error: no_host'; }
 9208:     my $uhost=&hostname($uhome);
 9209:     my $command=&escape(join(':',map{$_.'='.$filters{$_}} keys(%filters)));
 9210:     my $queryid=&reply("querysend:".$query.':'.$udom.':'.$uname.':'.$command,
 9211:                        $uhome);
 9212:     unless ($queryid=~/^\Q$uhost\E\_/) { return 'error: '.$queryid; }
 9213:     return get_query_reply($queryid);
 9214: }
 9215: 
 9216: # -------------------------- Update MySQL table for portfolio file
 9217: 
 9218: sub update_portfolio_table {
 9219:     my ($uname,$udom,$file_name,$query,$group,$action) = @_;
 9220:     if ($group ne '') {
 9221:         $file_name =~s /^\Q$group\E//;
 9222:     }
 9223:     my $homeserver = &homeserver($uname,$udom);
 9224:     my $queryid=
 9225:         &reply("querysend:".$query.':'.&escape($uname.':'.$udom.':'.$group).
 9226:                ':'.&escape($file_name).':'.$action,$homeserver);
 9227:     my $reply = &get_query_reply($queryid);
 9228:     return $reply;
 9229: }
 9230: 
 9231: # -------------------------- Update MySQL allusers table
 9232: 
 9233: sub update_allusers_table {
 9234:     my ($uname,$udom,$names) = @_;
 9235:     my $homeserver = &homeserver($uname,$udom);
 9236:     my $queryid=
 9237:         &reply('querysend:allusers:'.&escape($uname).':'.&escape($udom).':'.
 9238:                'lastname='.&escape($names->{'lastname'}).'%%'.
 9239:                'firstname='.&escape($names->{'firstname'}).'%%'.
 9240:                'middlename='.&escape($names->{'middlename'}).'%%'.
 9241:                'generation='.&escape($names->{'generation'}).'%%'.
 9242:                'permanentemail='.&escape($names->{'permanentemail'}).'%%'.
 9243:                'id='.&escape($names->{'id'}),$homeserver);
 9244:     return;
 9245: }
 9246: 
 9247: # ------- Request retrieval of institutional classlists for course(s)
 9248: 
 9249: sub fetch_enrollment_query {
 9250:     my ($context,$affiliatesref,$replyref,$dom,$cnum) = @_;
 9251:     my ($homeserver,$sleep,$loopmax);
 9252:     my $maxtries = 1;
 9253:     if ($context eq 'automated') {
 9254:         $homeserver = $perlvar{'lonHostID'};
 9255:         $sleep = 2;
 9256:         $loopmax = 100;
 9257:         $maxtries = 10; # will wait for up to 2000s for retrieval of classlist data before timeout
 9258:     } else {
 9259:         $homeserver = &homeserver($cnum,$dom);
 9260:     }
 9261:     my $host=&hostname($homeserver);
 9262:     my $cmd = '';
 9263:     foreach my $affiliate (keys(%{$affiliatesref})) {
 9264:         $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
 9265:     }
 9266:     $cmd =~ s/%%$//;
 9267:     $cmd = &escape($cmd);
 9268:     my $query = 'fetchenrollment';
 9269:     my $queryid=&reply("querysend:".$query.':'.$dom.':'.$env{'user.name'}.':'.$cmd,$homeserver);
 9270:     unless ($queryid=~/^\Q$host\E\_/) { 
 9271:         &logthis('fetch_enrollment_query: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' context: '.$context.' '.$cnum); 
 9272:         return 'error: '.$queryid;
 9273:     }
 9274:     my $reply = &get_query_reply($queryid,$sleep,$loopmax);
 9275:     my $tries = 1;
 9276:     while (($reply=~/^timeout/) && ($tries < $maxtries)) {
 9277:         $reply = &get_query_reply($queryid,$sleep,$loopmax);
 9278:         $tries ++;
 9279:     }
 9280:     if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
 9281:         &logthis('fetch_enrollment_query error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' context: '.$context.' '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
 9282:     } else {
 9283:         my @responses = split(/:/,$reply);
 9284:         if (grep { $_ eq $homeserver } &current_machine_ids()) {
 9285:             foreach my $line (@responses) {
 9286:                 my ($key,$value) = split(/=/,$line,2);
 9287:                 $$replyref{$key} = $value;
 9288:             }
 9289:         } else {
 9290:             my $pathname = LONCAPA::tempdir();
 9291:             foreach my $line (@responses) {
 9292:                 my ($key,$value) = split(/=/,$line);
 9293:                 $$replyref{$key} = $value;
 9294:                 if ($value > 0) {
 9295:                     foreach my $item (@{$$affiliatesref{$key}}) {
 9296:                         my $filename = $dom.'_'.$key.'_'.$item.'_classlist.xml';
 9297:                         my $destname = $pathname.'/'.$filename;
 9298:                         my $xml_classlist = &reply("autoretrieve:".$filename,$homeserver);
 9299:                         if ($xml_classlist =~ /^error/) {
 9300:                             &logthis('fetch_enrollment_query - autoretrieve error: '.$xml_classlist.' for '.$filename.' from server: '.$homeserver.' '.$context.' '.$cnum);
 9301:                         } else {
 9302:                             if ( open(FILE,">",$destname) ) {
 9303:                                 print FILE &unescape($xml_classlist);
 9304:                                 close(FILE);
 9305:                             } else {
 9306:                                 &logthis('fetch_enrollment_query - error opening classlist file '.$destname.' '.$context.' '.$cnum);
 9307:                             }
 9308:                         }
 9309:                     }
 9310:                 }
 9311:             }
 9312:         }
 9313:         return 'ok';
 9314:     }
 9315:     return 'error';
 9316: }
 9317: 
 9318: sub get_query_reply {
 9319:     my ($queryid,$sleep,$loopmax) = @_;;
 9320:     if (($sleep eq '') || ($sleep !~ /^\d+\.?\d*$/)) {
 9321:         $sleep = 0.2;
 9322:     }
 9323:     if (($loopmax eq '') || ($loopmax =~ /\D/)) {
 9324:         $loopmax = 100;
 9325:     }
 9326:     my $replyfile=LONCAPA::tempdir().$queryid;
 9327:     my $reply='';
 9328:     for (1..$loopmax) {
 9329: 	sleep($sleep);
 9330:         if (-e $replyfile.'.end') {
 9331: 	    if (open(my $fh,"<",$replyfile)) {
 9332: 		$reply = join('',<$fh>);
 9333: 		close($fh);
 9334: 	   } else { return 'error: reply_file_error'; }
 9335:            return &unescape($reply);
 9336: 	}
 9337:     }
 9338:     return 'timeout:'.$queryid;
 9339: }
 9340: 
 9341: sub courselog_query {
 9342: #
 9343: # possible filters:
 9344: # url: url or symb
 9345: # username
 9346: # domain
 9347: # action: view, submit, grade
 9348: # start: timestamp
 9349: # end: timestamp
 9350: #
 9351:     my (%filters)=@_;
 9352:     unless ($env{'request.course.id'}) { return 'no_course'; }
 9353:     if ($filters{'url'}) {
 9354: 	$filters{'url'}=&symbclean(&declutter($filters{'url'}));
 9355:         $filters{'url'}=~s/\.(\w+)$/(\\.\\d+)*\\.$1/;
 9356:         $filters{'url'}=~s/\.(\w+)\_\_\_/(\\.\\d+)*\\.$1/;
 9357:     }
 9358:     my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
 9359:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 9360:     return &log_query($cname,$cdom,'courselog',%filters);
 9361: }
 9362: 
 9363: sub userlog_query {
 9364: #
 9365: # possible filters:
 9366: # action: log check role
 9367: # start: timestamp
 9368: # end: timestamp
 9369: #
 9370:     my ($uname,$udom,%filters)=@_;
 9371:     return &log_query($uname,$udom,'userlog',%filters);
 9372: }
 9373: 
 9374: #--------- Call auto-enrollment subs in localenroll.pm for homeserver for course 
 9375: 
 9376: sub auto_run {
 9377:     my ($cnum,$cdom) = @_;
 9378:     my $response = 0;
 9379:     my $settings;
 9380:     my %domconfig = &get_dom('configuration',['autoenroll'],$cdom);
 9381:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 9382:         $settings = $domconfig{'autoenroll'};
 9383:         if ($settings->{'run'} eq '1') {
 9384:             $response = 1;
 9385:         }
 9386:     } else {
 9387:         my $homeserver;
 9388:         if (&is_course($cdom,$cnum)) {
 9389:             $homeserver = &homeserver($cnum,$cdom);
 9390:         } else {
 9391:             $homeserver = &domain($cdom,'primary');
 9392:         }
 9393:         if ($homeserver ne 'no_host') {
 9394:             $response = &reply('autorun:'.$cdom,$homeserver);
 9395:         }
 9396:     }
 9397:     return $response;
 9398: }
 9399: 
 9400: sub auto_get_sections {
 9401:     my ($cnum,$cdom,$inst_coursecode) = @_;
 9402:     my $homeserver;
 9403:     if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) { 
 9404:         $homeserver = &homeserver($cnum,$cdom);
 9405:     }
 9406:     if (!defined($homeserver)) { 
 9407:         if ($cdom =~ /^$match_domain$/) {
 9408:             $homeserver = &domain($cdom,'primary');
 9409:         }
 9410:     }
 9411:     my @secs;
 9412:     if (defined($homeserver)) {
 9413:         my $response=&unescape(&reply('autogetsections:'.$inst_coursecode.':'.$cdom,$homeserver));
 9414:         unless ($response eq 'refused') {
 9415:             @secs = split(/:/,$response);
 9416:         }
 9417:     }
 9418:     return @secs;
 9419: }
 9420: 
 9421: sub auto_new_course {
 9422:     my ($cnum,$cdom,$inst_course_id,$owner,$coowners) = @_;
 9423:     my $homeserver = &homeserver($cnum,$cdom);
 9424:     my $response=&unescape(&reply('autonewcourse:'.$inst_course_id.':'.&escape($owner).':'.$cdom.':'.&escape($coowners),$homeserver));
 9425:     return $response;
 9426: }
 9427: 
 9428: sub auto_validate_courseID {
 9429:     my ($cnum,$cdom,$inst_course_id) = @_;
 9430:     my $homeserver = &homeserver($cnum,$cdom);
 9431:     my $response=&unescape(&reply('autovalidatecourse:'.$inst_course_id.':'.$cdom,$homeserver));
 9432:     return $response;
 9433: }
 9434: 
 9435: sub auto_validate_instcode {
 9436:     my ($cnum,$cdom,$instcode,$owner) = @_;
 9437:     my ($homeserver,$response);
 9438:     if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
 9439:         $homeserver = &homeserver($cnum,$cdom);
 9440:     }
 9441:     if (!defined($homeserver)) {
 9442:         if ($cdom =~ /^$match_domain$/) {
 9443:             $homeserver = &domain($cdom,'primary');
 9444:         }
 9445:     }
 9446:     $response=&unescape(&reply('autovalidateinstcode:'.$cdom.':'.
 9447:                         &escape($instcode).':'.&escape($owner),$homeserver));
 9448:     my ($outcome,$description,$defaultcredits) = map { &unescape($_); } split('&',$response,3);
 9449:     return ($outcome,$description,$defaultcredits);
 9450: }
 9451: 
 9452: sub auto_validate_inst_crosslist {
 9453:     my ($cnum,$cdom,$instcode,$inst_xlist,$coowner) = @_;
 9454:     my ($homeserver,$response);
 9455:     if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
 9456:         $homeserver = &homeserver($cnum,$cdom);
 9457:     }
 9458:     if (!defined($homeserver)) {
 9459:         if ($cdom =~ /^$match_domain$/) {
 9460:             $homeserver = &domain($cdom,'primary');
 9461:         }
 9462:     }
 9463:     unless (($homeserver eq '') || ($homeserver eq 'no_host')) {
 9464:         $response=&reply('autovalidateinstcrosslist:'.$cdom.':'.
 9465:                          &escape($instcode).':'.&escape($inst_xlist).':'.
 9466:                          &escape($coowner),$homeserver);
 9467:     }
 9468:     return $response;
 9469: }
 9470: 
 9471: sub auto_create_password {
 9472:     my ($cnum,$cdom,$authparam,$udom) = @_;
 9473:     my ($homeserver,$response);
 9474:     my $create_passwd = 0;
 9475:     my $authchk = '';
 9476:     if ($udom =~ /^$match_domain$/) {
 9477:         $homeserver = &domain($udom,'primary');
 9478:     }
 9479:     if ($homeserver eq '') {
 9480:         if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
 9481:             $homeserver = &homeserver($cnum,$cdom);
 9482:         }
 9483:     }
 9484:     if ($homeserver eq '') {
 9485:         $authchk = 'nodomain';
 9486:     } else {
 9487:         $response=&unescape(&reply('autocreatepassword:'.$authparam.':'.$cdom,$homeserver));
 9488:         if ($response eq 'refused') {
 9489:             $authchk = 'refused';
 9490:         } else {
 9491:             ($authparam,$create_passwd,$authchk) = split(/:/,$response);
 9492:         }
 9493:     }
 9494:     return ($authparam,$create_passwd,$authchk);
 9495: }
 9496: 
 9497: sub auto_photo_permission {
 9498:     my ($cnum,$cdom,$students) = @_;
 9499:     my $homeserver = &homeserver($cnum,$cdom);
 9500:     my ($outcome,$perm_reqd,$conditions) = 
 9501: 	split(/:/,&unescape(&reply('autophotopermission:'.$cdom,$homeserver)),3);
 9502:     if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
 9503: 	return (undef,undef);
 9504:     }
 9505:     return ($outcome,$perm_reqd,$conditions);
 9506: }
 9507: 
 9508: sub auto_checkphotos {
 9509:     my ($uname,$udom,$pid) = @_;
 9510:     my $homeserver = &homeserver($uname,$udom);
 9511:     my ($result,$resulttype);
 9512:     my $outcome = &unescape(&reply('autophotocheck:'.&escape($udom).':'.
 9513: 				   &escape($uname).':'.&escape($pid),
 9514: 				   $homeserver));
 9515:     if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
 9516: 	return (undef,undef);
 9517:     }
 9518:     if ($outcome) {
 9519:         ($result,$resulttype) = split(/:/,$outcome);
 9520:     } 
 9521:     return ($result,$resulttype);
 9522: }
 9523: 
 9524: sub auto_photochoice {
 9525:     my ($cnum,$cdom) = @_;
 9526:     my $homeserver = &homeserver($cnum,$cdom);
 9527:     my ($update,$comment) = split(/:/,&unescape(&reply('autophotochoice:'.
 9528: 						       &escape($cdom),
 9529: 						       $homeserver)));
 9530:     if ($update =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
 9531: 	return (undef,undef);
 9532:     }
 9533:     return ($update,$comment);
 9534: }
 9535: 
 9536: sub auto_photoupdate {
 9537:     my ($affiliatesref,$dom,$cnum,$photo) = @_;
 9538:     my $homeserver = &homeserver($cnum,$dom);
 9539:     my $host=&hostname($homeserver);
 9540:     my $cmd = '';
 9541:     my $maxtries = 1;
 9542:     foreach my $affiliate (keys(%{$affiliatesref})) {
 9543:         $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
 9544:     }
 9545:     $cmd =~ s/%%$//;
 9546:     $cmd = &escape($cmd);
 9547:     my $query = 'institutionalphotos';
 9548:     my $queryid=&reply("querysend:".$query.':'.$dom.':'.$cnum.':'.$cmd,$homeserver);
 9549:     unless ($queryid=~/^\Q$host\E\_/) {
 9550:         &logthis('institutionalphotos: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' and course: '.$cnum);
 9551:         return 'error: '.$queryid;
 9552:     }
 9553:     my $reply = &get_query_reply($queryid);
 9554:     my $tries = 1;
 9555:     while (($reply=~/^timeout/) && ($tries < $maxtries)) {
 9556:         $reply = &get_query_reply($queryid);
 9557:         $tries ++;
 9558:     }
 9559:     if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
 9560:         &logthis('institutionalphotos error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' course: '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
 9561:     } else {
 9562:         my @responses = split(/:/,$reply);
 9563:         my $outcome = shift(@responses); 
 9564:         foreach my $item (@responses) {
 9565:             my ($key,$value) = split(/=/,$item);
 9566:             $$photo{$key} = $value;
 9567:         }
 9568:         return $outcome;
 9569:     }
 9570:     return 'error';
 9571: }
 9572: 
 9573: sub auto_instcode_format {
 9574:     my ($caller,$codedom,$instcodes,$codes,$codetitles,$cat_titles,
 9575: 	$cat_order) = @_;
 9576:     my $courses = '';
 9577:     my @homeservers;
 9578:     if ($caller eq 'global') {
 9579: 	my %servers = &get_servers($codedom,'library');
 9580: 	foreach my $tryserver (keys(%servers)) {
 9581: 	    if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
 9582: 		push(@homeservers,$tryserver);
 9583: 	    }
 9584:         }
 9585:     } elsif ($caller eq 'requests') {
 9586:         if ($codedom =~ /^$match_domain$/) {
 9587:             my $chome = &domain($codedom,'primary');
 9588:             unless ($chome eq 'no_host') {
 9589:                 push(@homeservers,$chome);
 9590:             }
 9591:         }
 9592:     } else {
 9593:         push(@homeservers,&homeserver($caller,$codedom));
 9594:     }
 9595:     foreach my $code (keys(%{$instcodes})) {
 9596:         $courses .= &escape($code).'='.&escape($$instcodes{$code}).'&';
 9597:     }
 9598:     chop($courses);
 9599:     my $ok_response = 0;
 9600:     my $response;
 9601:     while (@homeservers > 0 && $ok_response == 0) {
 9602:         my $server = shift(@homeservers); 
 9603:         $response=&reply('autoinstcodeformat:'.$codedom.':'.$courses,$server);
 9604:         if ($response !~ /(con_lost|error|no_such_host|refused)/) {
 9605:             my ($codes_str,$codetitles_str,$cat_titles_str,$cat_order_str) = 
 9606: 		split(/:/,$response);
 9607:             %{$codes} = (%{$codes},&str2hash($codes_str));
 9608:             push(@{$codetitles},&str2array($codetitles_str));
 9609:             %{$cat_titles} = (%{$cat_titles},&str2hash($cat_titles_str));
 9610:             %{$cat_order} = (%{$cat_order},&str2hash($cat_order_str));
 9611:             $ok_response = 1;
 9612:         }
 9613:     }
 9614:     if ($ok_response) {
 9615:         return 'ok';
 9616:     } else {
 9617:         return $response;
 9618:     }
 9619: }
 9620: 
 9621: sub auto_instcode_defaults {
 9622:     my ($domain,$returnhash,$code_order) = @_;
 9623:     my @homeservers;
 9624: 
 9625:     my %servers = &get_servers($domain,'library');
 9626:     foreach my $tryserver (keys(%servers)) {
 9627: 	if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
 9628: 	    push(@homeservers,$tryserver);
 9629: 	}
 9630:     }
 9631: 
 9632:     my $response;
 9633:     foreach my $server (@homeservers) {
 9634:         $response=&reply('autoinstcodedefaults:'.$domain,$server);
 9635:         next if ($response =~ /(con_lost|error|no_such_host|refused)/);
 9636: 	
 9637: 	foreach my $pair (split(/\&/,$response)) {
 9638: 	    my ($name,$value)=split(/\=/,$pair);
 9639: 	    if ($name eq 'code_order') {
 9640: 		@{$code_order} = split(/\&/,&unescape($value));
 9641: 	    } else {
 9642: 		$returnhash->{&unescape($name)}=&unescape($value);
 9643: 	    }
 9644: 	}
 9645: 	return 'ok';
 9646:     }
 9647: 
 9648:     return $response;
 9649: }
 9650: 
 9651: sub auto_possible_instcodes {
 9652:     my ($domain,$codetitles,$cat_titles,$cat_orders,$code_order) = @_;
 9653:     unless ((ref($codetitles) eq 'ARRAY') && (ref($cat_titles) eq 'HASH') && 
 9654:             (ref($cat_orders) eq 'HASH') && (ref($code_order) eq 'ARRAY')) {
 9655:         return;
 9656:     }
 9657:     my (@homeservers,$uhome);
 9658:     if (defined(&domain($domain,'primary'))) {
 9659:         $uhome=&domain($domain,'primary');
 9660:         push(@homeservers,&domain($domain,'primary'));
 9661:     } else {
 9662:         my %servers = &get_servers($domain,'library');
 9663:         foreach my $tryserver (keys(%servers)) {
 9664:             if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
 9665:                 push(@homeservers,$tryserver);
 9666:             }
 9667:         }
 9668:     }
 9669:     my $response;
 9670:     foreach my $server (@homeservers) {
 9671:         $response=&reply('autopossibleinstcodes:'.$domain,$server);
 9672:         next if ($response =~ /(con_lost|error|no_such_host|refused)/);
 9673:         my ($codetitlestr,$codeorderstr,$cat_title,$cat_order) = 
 9674:             split(':',$response);
 9675:         @{$codetitles} = map { &unescape($_); } (split('&',$codetitlestr));
 9676:         @{$code_order} = map { &unescape($_); } (split('&',$codeorderstr));
 9677:         foreach my $item (split('&',$cat_title)) {   
 9678:             my ($name,$value)=split('=',$item);
 9679:             $cat_titles->{&unescape($name)}=&thaw_unescape($value);
 9680:         }
 9681:         foreach my $item (split('&',$cat_order)) {
 9682:             my ($name,$value)=split('=',$item);
 9683:             $cat_orders->{&unescape($name)}=&thaw_unescape($value);
 9684:         }
 9685:         return 'ok';
 9686:     }
 9687:     return $response;
 9688: }
 9689: 
 9690: sub auto_courserequest_checks {
 9691:     my ($dom) = @_;
 9692:     my ($homeserver,%validations);
 9693:     if ($dom =~ /^$match_domain$/) {
 9694:         $homeserver = &domain($dom,'primary');
 9695:     }
 9696:     unless ($homeserver eq 'no_host') {
 9697:         my $response=&reply('autocrsreqchecks:'.$dom,$homeserver);
 9698:         unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
 9699:             my @items = split(/&/,$response);
 9700:             foreach my $item (@items) {
 9701:                 my ($key,$value) = split('=',$item);
 9702:                 $validations{&unescape($key)} = &thaw_unescape($value);
 9703:             }
 9704:         }
 9705:     }
 9706:     return %validations; 
 9707: }
 9708: 
 9709: sub auto_courserequest_validation {
 9710:     my ($dom,$owner,$crstype,$inststatuslist,$instcode,$instseclist,$custominfo) = @_;
 9711:     my ($homeserver,$response);
 9712:     if ($dom =~ /^$match_domain$/) {
 9713:         $homeserver = &domain($dom,'primary');
 9714:     }
 9715:     unless ($homeserver eq 'no_host') {
 9716:         my $customdata;
 9717:         if (ref($custominfo) eq 'HASH') {
 9718:             $customdata = &freeze_escape($custominfo);
 9719:         }
 9720:         $response=&unescape(&reply('autocrsreqvalidation:'.$dom.':'.&escape($owner).
 9721:                                     ':'.&escape($crstype).':'.&escape($inststatuslist).
 9722:                                     ':'.&escape($instcode).':'.&escape($instseclist).':'.
 9723:                                     $customdata,$homeserver));
 9724:     }
 9725:     return $response;
 9726: }
 9727: 
 9728: sub auto_validate_class_sec {
 9729:     my ($cdom,$cnum,$owners,$inst_class) = @_;
 9730:     my $homeserver = &homeserver($cnum,$cdom);
 9731:     my $ownerlist;
 9732:     if (ref($owners) eq 'ARRAY') {
 9733:         $ownerlist = join(',',@{$owners});
 9734:     } else {
 9735:         $ownerlist = $owners;
 9736:     }
 9737:     my $response=&reply('autovalidateclass_sec:'.$inst_class.':'.
 9738:                         &escape($ownerlist).':'.$cdom,$homeserver);
 9739:     return $response;
 9740: }
 9741: 
 9742: sub auto_validate_instclasses {
 9743:     my ($cdom,$cnum,$owners,$classesref) = @_;
 9744:     my ($homeserver,%validations);
 9745:     $homeserver = &homeserver($cnum,$cdom);
 9746:     unless ($homeserver eq 'no_host') {
 9747:         my $ownerlist;
 9748:         if (ref($owners) eq 'ARRAY') {
 9749:             $ownerlist = join(',',@{$owners});
 9750:         } else {
 9751:             $ownerlist = $owners;
 9752:         }
 9753:         if (ref($classesref) eq 'HASH') {
 9754:             my $classes = &freeze_escape($classesref);
 9755:             my $response=&reply('autovalidateinstclasses:'.&escape($ownerlist).
 9756:                                 ':'.$cdom.':'.$classes,$homeserver);
 9757:             unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
 9758:                 my @items = split(/&/,$response);
 9759:                 foreach my $item (@items) {
 9760:                     my ($key,$value) = split('=',$item);
 9761:                     $validations{&unescape($key)} = &thaw_unescape($value);
 9762:                 }
 9763:             }
 9764:         }
 9765:     }
 9766:     return %validations;
 9767: }
 9768: 
 9769: sub auto_crsreq_update {
 9770:     my ($cdom,$cnum,$crstype,$action,$ownername,$ownerdomain,$fullname,$title,
 9771:         $code,$accessstart,$accessend,$inbound) = @_;
 9772:     my ($homeserver,%crsreqresponse);
 9773:     if ($cdom =~ /^$match_domain$/) {
 9774:         $homeserver = &domain($cdom,'primary');
 9775:     }
 9776:     unless (($homeserver eq 'no_host') || ($homeserver eq '')) {
 9777:         my $info;
 9778:         if (ref($inbound) eq 'HASH') {
 9779:             $info = &freeze_escape($inbound);
 9780:         }
 9781:         my $response=&reply('autocrsrequpdate:'.$cdom.':'.$cnum.':'.&escape($crstype).
 9782:                             ':'.&escape($action).':'.&escape($ownername).':'.
 9783:                             &escape($ownerdomain).':'.&escape($fullname).':'.
 9784:                             &escape($title).':'.&escape($code).':'.
 9785:                             &escape($accessstart).':'.&escape($accessend).':'.$info,
 9786:                             $homeserver);
 9787:         unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
 9788:             my @items = split(/&/,$response);
 9789:             foreach my $item (@items) {
 9790:                 my ($key,$value) = split('=',$item);
 9791:                 $crsreqresponse{&unescape($key)} = &thaw_unescape($value);
 9792:             }
 9793:         }
 9794:     }
 9795:     return \%crsreqresponse;
 9796: }
 9797: 
 9798: sub auto_export_grades {
 9799:     my ($cdom,$cnum,$inforef,$gradesref) = @_;
 9800:     my ($homeserver,%exportresponse);
 9801:     if ($cdom =~ /^$match_domain$/) {
 9802:         $homeserver = &domain($cdom,'primary');
 9803:     }
 9804:     unless (($homeserver eq 'no_host') || ($homeserver eq '')) {
 9805:         my $info;
 9806:         if (ref($inforef) eq 'HASH') {
 9807:             $info = &freeze_escape($inforef);
 9808:         }
 9809:         if (ref($gradesref) eq 'HASH') {
 9810:             my $grades = &freeze_escape($gradesref);
 9811:             my $response=&reply('encrypt:autoexportgrades:'.$cdom.':'.$cnum.':'.
 9812:                                 $info.':'.$grades,$homeserver);
 9813:             unless ($response =~ /(con_lost|error|no_such_host|refused|unknown_command)/) {
 9814:                 my @items = split(/&/,$response);
 9815:                 foreach my $item (@items) {
 9816:                     my ($key,$value) = split('=',$item);
 9817:                     $exportresponse{&unescape($key)} = &thaw_unescape($value);
 9818:                 }
 9819:             }
 9820:         }
 9821:     }
 9822:     return \%exportresponse;
 9823: }
 9824: 
 9825: sub check_instcode_cloning {
 9826:     my ($codedefaults,$code_order,$cloner,$clonefromcode,$clonetocode) = @_;
 9827:     unless ((ref($codedefaults) eq 'HASH') && (ref($code_order) eq 'ARRAY')) {
 9828:         return;
 9829:     }
 9830:     my $canclone;
 9831:     if (@{$code_order} > 0) {
 9832:         my $instcoderegexp ='^';
 9833:         my @clonecodes = split(/\&/,$cloner);
 9834:         foreach my $item (@{$code_order}) {
 9835:             if (grep(/^\Q$item\E=/,@clonecodes)) {
 9836:                 foreach my $pair (@clonecodes) {
 9837:                     my ($key,$val) = split(/\=/,$pair,2);
 9838:                     $val = &unescape($val);
 9839:                     if ($key eq $item) {
 9840:                         $instcoderegexp .= '('.$val.')';
 9841:                         last;
 9842:                     }
 9843:                 }
 9844:             } else {
 9845:                 $instcoderegexp .= $codedefaults->{$item};
 9846:             }
 9847:         }
 9848:         $instcoderegexp .= '$';
 9849:         my (@from,@to);
 9850:         eval {
 9851:                (@from) = ($clonefromcode =~ /$instcoderegexp/);
 9852:                (@to) = ($clonetocode =~ /$instcoderegexp/);
 9853:         };
 9854:         if ((@from > 0) && (@to > 0)) {
 9855:             my @diffs = &Apache::loncommon::compare_arrays(\@from,\@to);
 9856:             if (!@diffs) {
 9857:                 $canclone = 1;
 9858:             }
 9859:         }
 9860:     }
 9861:     return $canclone;
 9862: }
 9863: 
 9864: sub default_instcode_cloning {
 9865:     my ($clonedom,$domdefclone,$clonefromcode,$clonetocode,$codedefaultsref,$codeorderref) = @_;
 9866:     my (%codedefaults,@code_order,$canclone);
 9867:     if ((ref($codedefaultsref) eq 'HASH') && (ref($codeorderref) eq 'ARRAY')) {
 9868:         %codedefaults = %{$codedefaultsref};
 9869:         @code_order = @{$codeorderref};
 9870:     } elsif ($clonedom) {
 9871:         &auto_instcode_defaults($clonedom,\%codedefaults,\@code_order);
 9872:     }
 9873:     if (($domdefclone) && (@code_order)) {
 9874:         my @clonecodes = split(/\+/,$domdefclone);
 9875:         my $instcoderegexp ='^';
 9876:         foreach my $item (@code_order) {
 9877:             if (grep(/^\Q$item\E$/,@clonecodes)) {
 9878:                 $instcoderegexp .= '('.$codedefaults{$item}.')';
 9879:             } else {
 9880:                 $instcoderegexp .= $codedefaults{$item};
 9881:             }
 9882:         }
 9883:         $instcoderegexp .= '$';
 9884:         my (@from,@to);
 9885:         eval {
 9886:             (@from) = ($clonefromcode =~ /$instcoderegexp/);
 9887:             (@to) = ($clonetocode =~ /$instcoderegexp/);
 9888:         };
 9889:         if ((@from > 0) && (@to > 0)) {
 9890:             my @diffs = &Apache::loncommon::compare_arrays(\@from,\@to);
 9891:             if (!@diffs) {
 9892:                 $canclone = 1;
 9893:             }
 9894:         }
 9895:     }
 9896:     return $canclone;
 9897: }
 9898: 
 9899: # ------------------------------------------------------- Course Group routines
 9900: 
 9901: sub get_coursegroups {
 9902:     my ($cdom,$cnum,$group,$namespace) = @_;
 9903:     return(&dump($namespace,$cdom,$cnum,$group));
 9904: }
 9905: 
 9906: sub modify_coursegroup {
 9907:     my ($cdom,$cnum,$groupsettings) = @_;
 9908:     return(&put('coursegroups',$groupsettings,$cdom,$cnum));
 9909: }
 9910: 
 9911: sub toggle_coursegroup_status {
 9912:     my ($cdom,$cnum,$group,$action) = @_;
 9913:     my ($from_namespace,$to_namespace);
 9914:     if ($action eq 'delete') {
 9915:         $from_namespace = 'coursegroups';
 9916:         $to_namespace = 'deleted_groups';
 9917:     } else {
 9918:         $from_namespace = 'deleted_groups';
 9919:         $to_namespace = 'coursegroups';
 9920:     }
 9921:     my %curr_group = &get_coursegroups($cdom,$cnum,$group,$from_namespace);
 9922:     if (my $tmp = &error(%curr_group)) {
 9923:         &Apache::lonnet::logthis('Error retrieving group: '.$tmp.' in '.$cnum.':'.$cdom);
 9924:         return ('read error',$tmp);
 9925:     } else {
 9926:         my %savedsettings = %curr_group; 
 9927:         my $result = &put($to_namespace,\%savedsettings,$cdom,$cnum);
 9928:         my $deloutcome;
 9929:         if ($result eq 'ok') {
 9930:             $deloutcome = &del($from_namespace,[$group],$cdom,$cnum);
 9931:         } else {
 9932:             return ('write error',$result);
 9933:         }
 9934:         if ($deloutcome eq 'ok') {
 9935:             return 'ok';
 9936:         } else {
 9937:             return ('delete error',$deloutcome);
 9938:         }
 9939:     }
 9940: }
 9941: 
 9942: sub modify_group_roles {
 9943:     my ($cdom,$cnum,$group_id,$user,$end,$start,$userprivs,$selfenroll,$context) = @_;
 9944:     my $url = '/'.$cdom.'/'.$cnum.'/'.$group_id;
 9945:     my $role = 'gr/'.&escape($userprivs);
 9946:     my ($uname,$udom) = split(/:/,$user);
 9947:     my $result = &assignrole($udom,$uname,$url,$role,$end,$start,'',$selfenroll,$context);
 9948:     if ($result eq 'ok') {
 9949:         &devalidate_getgroups_cache($udom,$uname,$cdom,$cnum);
 9950:     }
 9951:     return $result;
 9952: }
 9953: 
 9954: sub modify_coursegroup_membership {
 9955:     my ($cdom,$cnum,$membership) = @_;
 9956:     my $result = &put('groupmembership',$membership,$cdom,$cnum);
 9957:     return $result;
 9958: }
 9959: 
 9960: sub get_active_groups {
 9961:     my ($udom,$uname,$cdom,$cnum) = @_;
 9962:     my $now = time;
 9963:     my %groups = ();
 9964:     foreach my $key (keys(%env)) {
 9965:         if ($key =~ m-user\.role\.gr\./($match_domain)/($match_courseid)/(\w+)$-) {
 9966:             my ($start,$end) = split(/\./,$env{$key});
 9967:             if (($end!=0) && ($end<$now)) { next; }
 9968:             if (($start!=0) && ($start>$now)) { next; }
 9969:             if ($1 eq $cdom && $2 eq $cnum) {
 9970:                 $groups{$3} = $env{$key} ;
 9971:             }
 9972:         }
 9973:     }
 9974:     return %groups;
 9975: }
 9976: 
 9977: sub get_group_membership {
 9978:     my ($cdom,$cnum,$group) = @_;
 9979:     return(&dump('groupmembership',$cdom,$cnum,$group));
 9980: }
 9981: 
 9982: sub get_users_groups {
 9983:     my ($udom,$uname,$courseid) = @_;
 9984:     my @usersgroups;
 9985:     my $cachetime=1800;
 9986: 
 9987:     my $hashid="$udom:$uname:$courseid";
 9988:     my ($grouplist,$cached)=&is_cached_new('getgroups',$hashid);
 9989:     if (defined($cached)) {
 9990:         @usersgroups = split(/:/,$grouplist);
 9991:     } else {  
 9992:         $grouplist = '';
 9993:         my $courseurl = &courseid_to_courseurl($courseid);
 9994:         my %roleshash = &dump('roles',$udom,$uname,$courseurl);
 9995:         my $access_end = $env{'course.'.$courseid.
 9996:                               '.default_enrollment_end_date'};
 9997:         my $now = time;
 9998:         foreach my $key (keys(%roleshash)) {
 9999:             if ($key =~ /^\Q$courseurl\E\/(\w+)\_gr$/) {
10000:                 my $group = $1;
10001:                 if ($roleshash{$key} =~ /_(\d+)_(\d+)$/) {
10002:                     my $start = $2;
10003:                     my $end = $1;
10004:                     if ($start == -1) { next; } # deleted from group
10005:                     if (($start!=0) && ($start>$now)) { next; }
10006:                     if (($end!=0) && ($end<$now)) {
10007:                         if ($access_end && $access_end < $now) {
10008:                             if ($access_end - $end < 86400) {
10009:                                 push(@usersgroups,$group);
10010:                             }
10011:                         }
10012:                         next;
10013:                     }
10014:                     push(@usersgroups,$group);
10015:                 }
10016:             }
10017:         }
10018:         @usersgroups = &sort_course_groups($courseid,@usersgroups);
10019:         $grouplist = join(':',@usersgroups);
10020:         &do_cache_new('getgroups',$hashid,$grouplist,$cachetime);
10021:     }
10022:     return @usersgroups;
10023: }
10024: 
10025: sub devalidate_getgroups_cache {
10026:     my ($udom,$uname,$cdom,$cnum)=@_;
10027:     my $courseid = $cdom.'_'.$cnum;
10028: 
10029:     my $hashid="$udom:$uname:$courseid";
10030:     &devalidate_cache_new('getgroups',$hashid);
10031: }
10032: 
10033: # ------------------------------------------------------------------ Plain Text
10034: 
10035: sub plaintext {
10036:     my ($short,$type,$cid,$forcedefault) = @_;
10037:     if ($short =~ m{^cr/}) {
10038: 	return (split('/',$short))[-1];
10039:     }
10040:     if (!defined($cid)) {
10041:         $cid = $env{'request.course.id'};
10042:     }
10043:     my %rolenames = (
10044:                       Course    => 'std',
10045:                       Community => 'alt1',
10046:                       Placement => 'std',
10047:                     );
10048:     if ($cid ne '') {
10049:         if ($env{'course.'.$cid.'.'.$short.'.plaintext'} ne '') {
10050:             unless ($forcedefault) {
10051:                 my $roletext = $env{'course.'.$cid.'.'.$short.'.plaintext'}; 
10052:                 &Apache::lonlocal::mt_escape(\$roletext);
10053:                 return &Apache::lonlocal::mt($roletext);
10054:             }
10055:         }
10056:     }
10057:     if ((defined($type)) && (defined($rolenames{$type})) &&
10058:         (defined($rolenames{$type})) && 
10059:         (defined($prp{$short}{$rolenames{$type}}))) {
10060:         return &Apache::lonlocal::mt($prp{$short}{$rolenames{$type}});
10061:     } elsif ($cid ne '') {
10062:         my $crstype = $env{'course.'.$cid.'.type'};
10063:         if (($crstype ne '') && (defined($rolenames{$crstype})) &&
10064:             (defined($prp{$short}{$rolenames{$crstype}}))) {
10065:             return &Apache::lonlocal::mt($prp{$short}{$rolenames{$crstype}});
10066:         }
10067:     }
10068:     return &Apache::lonlocal::mt($prp{$short}{'std'});
10069: }
10070: 
10071: # ----------------------------------------------------------------- Assign Role
10072: 
10073: sub assignrole {
10074:     my ($udom,$uname,$url,$role,$end,$start,$deleteflag,$selfenroll,
10075:         $context)=@_;
10076:     my $mrole;
10077:     if ($role =~ /^cr\//) {
10078:         my $cwosec=$url;
10079:         $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
10080: 	unless (&allowed('ccr',$cwosec)) {
10081:            my $refused = 1;
10082:            if ($context eq 'requestcourses') {
10083:                if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '')) {
10084:                    if ($role =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
10085:                        if (($1 eq $env{'user.domain'}) && ($2 eq $env{'user.name'})) {
10086:                            my ($cdom,$cnum) = ($cwosec =~ m{^/?($match_domain)/($match_courseid)$});
10087:                            my %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
10088:                            if ($crsenv{'internal.courseowner'} eq
10089:                                $env{'user.name'}.':'.$env{'user.domain'}) {
10090:                                $refused = '';
10091:                            }
10092:                        }
10093:                    }
10094:                }
10095:            }
10096:            if ($refused) {
10097:                &logthis('Refused custom assignrole: '.
10098:                         $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.
10099:                         ' by '.$env{'user.name'}.' at '.$env{'user.domain'});
10100:                return 'refused';
10101:            }
10102:         }
10103:         $mrole='cr';
10104:     } elsif ($role =~ /^gr\//) {
10105:         my $cwogrp=$url;
10106:         $cwogrp=~s{^/($match_domain)/($match_courseid)/.*}{$1/$2};
10107:         unless (&allowed('mdg',$cwogrp)) {
10108:             &logthis('Refused group assignrole: '.
10109:               $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
10110:                     $env{'user.name'}.' at '.$env{'user.domain'});
10111:             return 'refused';
10112:         }
10113:         $mrole='gr';
10114:     } else {
10115:         my $cwosec=$url;
10116:         $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
10117:         if (!(&allowed('c'.$role,$cwosec)) && !(&allowed('c'.$role,$udom))) {
10118:             my $refused;
10119:             if (($env{'request.course.sec'}  ne '') && ($role eq 'st')) {
10120:                 if (!(&allowed('c'.$role,$url))) {
10121:                     $refused = 1;
10122:                 }
10123:             } else {
10124:                 $refused = 1;
10125:             }
10126:             if ($refused) {
10127:                 my ($cdom,$cnum) = ($cwosec =~ m{^/?($match_domain)/($match_courseid)$});
10128:                 if (!$selfenroll && (($context eq 'course') || ($context eq 'ltienroll' && $env{'request.lti.login'}))) {
10129:                     my %crsenv;
10130:                     if ($role eq 'cc' || $role eq 'co') {
10131:                         %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
10132:                         if (($role eq 'cc') && ($cnum !~ /^$match_community$/)) {
10133:                             if ($env{'request.role'} eq 'cc./'.$cdom.'/'.$cnum) {
10134:                                 if ($crsenv{'internal.courseowner'} eq 
10135:                                     $env{'user.name'}.':'.$env{'user.domain'}) {
10136:                                     $refused = '';
10137:                                 }
10138:                             }
10139:                         } elsif (($role eq 'co') && ($cnum =~ /^$match_community$/)) { 
10140:                             if ($env{'request.role'} eq 'co./'.$cdom.'/'.$cnum) {
10141:                                 if ($crsenv{'internal.courseowner'} eq 
10142:                                     $env{'user.name'}.':'.$env{'user.domain'}) {
10143:                                     $refused = '';
10144:                                 }
10145:                             }
10146:                         }
10147:                     }
10148:                 } elsif (($selfenroll == 1) && ($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
10149:                     if ($role eq 'st') {
10150:                         $refused = '';
10151:                     } elsif (($context eq 'ltienroll') && ($env{'request.lti.login'})) {
10152:                         $refused = '';
10153:                     }
10154:                 } elsif ($context eq 'requestcourses') {
10155:                     my @possroles = ('st','ta','ep','in','cc','co');
10156:                     if ((grep(/^\Q$role\E$/,@possroles)) && ($env{'user.name'} ne '' && $env{'user.domain'} ne '')) {
10157:                         my $wrongcc;
10158:                         if ($cnum =~ /^$match_community$/) {
10159:                             $wrongcc = 1 if ($role eq 'cc');
10160:                         } else {
10161:                             $wrongcc = 1 if ($role eq 'co');
10162:                         }
10163:                         unless ($wrongcc) {
10164:                             my %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
10165:                             if ($crsenv{'internal.courseowner'} eq 
10166:                                  $env{'user.name'}.':'.$env{'user.domain'}) {
10167:                                 $refused = '';
10168:                             }
10169:                         }
10170:                     }
10171:                 } elsif ($context eq 'requestauthor') {
10172:                     if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'}) && 
10173:                         ($url eq '/'.$udom.'/') && ($role eq 'au')) {
10174:                         if ($env{'environment.requestauthor'} eq 'automatic') {
10175:                             $refused = '';
10176:                         } else {
10177:                             my %domdefaults = &get_domain_defaults($udom);
10178:                             if (ref($domdefaults{'requestauthor'}) eq 'HASH') {
10179:                                 my $checkbystatus;
10180:                                 if ($env{'user.adv'}) { 
10181:                                     my $disposition = $domdefaults{'requestauthor'}{'_LC_adv'};
10182:                                     if ($disposition eq 'automatic') {
10183:                                         $refused = '';
10184:                                     } elsif ($disposition eq '') {
10185:                                         $checkbystatus = 1;
10186:                                     } 
10187:                                 } else {
10188:                                     $checkbystatus = 1;
10189:                                 }
10190:                                 if ($checkbystatus) {
10191:                                     if ($env{'environment.inststatus'}) {
10192:                                         my @inststatuses = split(/,/,$env{'environment.inststatus'});
10193:                                         foreach my $type (@inststatuses) {
10194:                                             if (($type ne '') &&
10195:                                                 ($domdefaults{'requestauthor'}{$type} eq 'automatic')) {
10196:                                                 $refused = '';
10197:                                             }
10198:                                         }
10199:                                     } elsif ($domdefaults{'requestauthor'}{'default'} eq 'automatic') {
10200:                                         $refused = '';
10201:                                     }
10202:                                 }
10203:                             }
10204:                         }
10205:                     }
10206:                 }
10207:                 if ($refused) {
10208:                     &logthis('Refused assignrole: '.$udom.' '.$uname.' '.$url.
10209:                              ' '.$role.' '.$end.' '.$start.' by '.
10210: 	  	             $env{'user.name'}.' at '.$env{'user.domain'});
10211:                     return 'refused';
10212:                 }
10213:             }
10214:         } elsif ($role eq 'au') {
10215:             if ($url ne '/'.$udom.'/') {
10216:                 &logthis('Attempt by '.$env{'user.name'}.':'.$env{'user.domain'}.
10217:                          ' to assign author role for '.$uname.':'.$udom.
10218:                          ' in domain: '.$url.' refused (wrong domain).');
10219:                 return 'refused';
10220:             }
10221:         }
10222:         $mrole=$role;
10223:     }
10224:     my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
10225:                 "$udom:$uname:$url".'_'."$mrole=$role";
10226:     if ($end) { $command.='_'.$end; }
10227:     if ($start) {
10228: 	if ($end) { 
10229:            $command.='_'.$start; 
10230:         } else {
10231:            $command.='_0_'.$start;
10232:         }
10233:     }
10234:     my $origstart = $start;
10235:     my $origend = $end;
10236:     my $delflag;
10237: # actually delete
10238:     if ($deleteflag) {
10239: 	if ((&allowed('dro',$udom)) || (&allowed('dro',$url))) {
10240: # modify command to delete the role
10241:            $command="encrypt:rolesdel:$env{'user.domain'}:$env{'user.name'}:".
10242:                 "$udom:$uname:$url".'_'."$mrole";
10243: 	   &logthis("$env{'user.name'} at $env{'user.domain'} deletes $mrole in $url for $uname at $udom"); 
10244: # set start and finish to negative values for userrolelog
10245:            $start=-1;
10246:            $end=-1;
10247:            $delflag = 1;
10248:         }
10249:     }
10250: # send command
10251:     my $answer=&reply($command,&homeserver($uname,$udom));
10252: # log new user role if status is ok
10253:     if ($answer eq 'ok') {
10254: 	&userrolelog($role,$uname,$udom,$url,$start,$end);
10255:         if (($role eq 'cc') || ($role eq 'in') ||
10256:             ($role eq 'ep') || ($role eq 'ad') ||
10257:             ($role eq 'ta') || ($role eq 'st') ||
10258:             ($role=~/^cr/) || ($role eq 'gr') ||
10259:             ($role eq 'co')) {
10260: # for course roles, perform group memberships changes triggered by role change.
10261:             unless ($role =~ /^gr/) {
10262:                 &Apache::longroup::group_changes($udom,$uname,$url,$role,$origend,
10263:                                                  $origstart,$selfenroll,$context);
10264:             }
10265:             &courserolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
10266:                            $selfenroll,$context);
10267:         } elsif (($role eq 'li') || ($role eq 'dg') || ($role eq 'sc') ||
10268:                  ($role eq 'au') || ($role eq 'dc') || ($role eq 'dh') ||
10269:                  ($role eq 'da')) {
10270:             &domainrolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
10271:                            $context);
10272:         } elsif (($role eq 'ca') || ($role eq 'aa')) {
10273:             &coauthorrolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
10274:                              $context); 
10275:         }
10276:         if ($role eq 'cc') {
10277:             &autoupdate_coowners($url,$end,$start,$uname,$udom);
10278:         }
10279:     }
10280:     return $answer;
10281: }
10282: 
10283: sub autoupdate_coowners {
10284:     my ($url,$end,$start,$uname,$udom) = @_;
10285:     my ($cdom,$cnum) = ($url =~ m{^/($match_domain)/($match_courseid)});
10286:     if (($cdom ne '') && ($cnum ne '')) {
10287:         my $now = time;
10288:         my %domdesign = &Apache::loncommon::get_domainconf($cdom);
10289:         if ($domdesign{$cdom.'.autoassign.co-owners'}) {
10290:             my %coursehash = &coursedescription($cdom.'_'.$cnum);
10291:             my $instcode = $coursehash{'internal.coursecode'};
10292:             my $xlists = $coursehash{'internal.crosslistings'};
10293:             if ($instcode ne '') {
10294:                 if (($start && $start <= $now) && ($end == 0) || ($end > $now)) {
10295:                     unless ($coursehash{'internal.courseowner'} eq $uname.':'.$udom) {
10296:                         my ($delcoowners,@newcoowners,$putresult,$delresult,$coowners);
10297:                         my ($result,$desc) = &auto_validate_instcode($cnum,$cdom,$instcode,$uname.':'.$udom);
10298:                         unless ($result eq 'valid') {
10299:                             if ($xlists ne '') {
10300:                                 foreach my $xlist (split(',',$xlists)) {
10301:                                     my ($inst_crosslist,$lcsec) = split(':',$xlist);
10302:                                     $result =
10303:                                         &auto_validate_inst_crosslist($cnum,$cdom,$instcode,
10304:                                                                       $inst_crosslist,$uname.':'.$udom);
10305:                                     last if ($result eq 'valid');
10306:                                 }
10307:                             }
10308:                         }
10309:                         if ($result eq 'valid') {
10310:                             if ($coursehash{'internal.co-owners'}) {
10311:                                 foreach my $coowner (split(',',$coursehash{'internal.co-owners'})) {
10312:                                     push(@newcoowners,$coowner);
10313:                                 }
10314:                                 unless (grep(/^\Q$uname\E:\Q$udom\E$/,@newcoowners)) {
10315:                                     push(@newcoowners,$uname.':'.$udom);
10316:                                 }
10317:                                 @newcoowners = sort(@newcoowners);
10318:                             } else {
10319:                                 push(@newcoowners,$uname.':'.$udom);
10320:                             }
10321:                         } elsif ($coursehash{'internal.co-owners'}) {
10322:                             foreach my $coowner (split(',',$coursehash{'internal.co-owners'})) {
10323:                                 unless ($coowner eq $uname.':'.$udom) {
10324:                                     push(@newcoowners,$coowner);
10325:                                 }
10326:                             }
10327:                             unless (@newcoowners > 0) {
10328:                                 $delcoowners = 1;
10329:                                 $coowners = '';
10330:                             }
10331:                         }
10332:                         if (@newcoowners || $delcoowners) {
10333:                             &store_coowners($cdom,$cnum,$coursehash{'home'},
10334:                                             $delcoowners,@newcoowners);
10335:                         }
10336:                     }
10337:                 }
10338:             }
10339:         }
10340:     }
10341: }
10342: 
10343: sub store_coowners {
10344:     my ($cdom,$cnum,$chome,$delcoowners,@newcoowners) = @_;
10345:     my $cid = $cdom.'_'.$cnum;
10346:     my ($coowners,$delresult,$putresult);
10347:     if (@newcoowners) {
10348:         $coowners = join(',',@newcoowners);
10349:         my %coownershash = (
10350:                             'internal.co-owners' => $coowners,
10351:                            );
10352:         $putresult = &put('environment',\%coownershash,$cdom,$cnum);
10353:         if ($putresult eq 'ok') {
10354:             if ($env{'course.'.$cid.'.num'} eq $cnum) {
10355:                 &appenv({'course.'.$cid.'.internal.co-owners' => $coowners});
10356:             }
10357:         }
10358:     }
10359:     if ($delcoowners) {
10360:         $delresult = &Apache::lonnet::del('environment',['internal.co-owners'],$cdom,$cnum);
10361:         if ($delresult eq 'ok') {
10362:             if ($env{'course.'.$cid.'.internal.co-owners'}) {
10363:                 &Apache::lonnet::delenv('course.'.$cid.'.internal.co-owners');
10364:             }
10365:         }
10366:     }
10367:     if (($putresult eq 'ok') || ($delresult eq 'ok')) {
10368:         my %crsinfo =
10369:             &Apache::lonnet::courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
10370:         if (ref($crsinfo{$cid}) eq 'HASH') {
10371:             $crsinfo{$cid}{'co-owners'} = \@newcoowners;
10372:             my $cidput = &Apache::lonnet::courseidput($cdom,\%crsinfo,$chome,'notime');
10373:         }
10374:     }
10375: }
10376: 
10377: # -------------------------------------------------- Modify user authentication
10378: # Overrides without validation
10379: 
10380: sub modifyuserauth {
10381:     my ($udom,$uname,$umode,$upass)=@_;
10382:     my $uhome=&homeserver($uname,$udom);
10383:     my $allowed;
10384:     if (&allowed('mau',$udom)) {
10385:         $allowed = 1;
10386:     } elsif (($umode eq 'internal') && ($udom eq $env{'user.domain'}) &&
10387:              ($env{'request.course.id'}) && (&allowed('mip',$env{'request.course.id'})) &&
10388:              (!$env{'course.'.$env{'request.course.id'}.'.internal.nopasswdchg'})) {
10389:         my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
10390:         my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
10391:         if (($cdom ne '') && ($cnum ne '')) {
10392:             my $is_owner = &is_course_owner($cdom,$cnum);
10393:             if ($is_owner) {
10394:                 $allowed = 1;
10395:             }
10396:         }
10397:     }
10398:     unless ($allowed) { return 'refused'; }
10399:     &logthis('Call to modify user authentication '.$udom.', '.$uname.', '.
10400:              $umode.' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
10401:              ' in domain '.$env{'request.role.domain'});  
10402:     my $reply=&reply('encrypt:changeuserauth:'.$udom.':'.$uname.':'.$umode.':'.
10403: 		     &escape($upass),$uhome);
10404:     my $ip = &get_requestor_ip();
10405:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
10406:         'Authentication changed for '.$udom.', '.$uname.', '.$umode.
10407:          '(Remote '.$ip.'): '.$reply);
10408:     &log($udom,,$uname,$uhome,
10409:         'Authentication changed by '.$env{'user.domain'}.', '.
10410:                                      $env{'user.name'}.', '.$umode.
10411:          '(Remote '.$ip.'): '.$reply);
10412:     unless ($reply eq 'ok') {
10413:         &logthis('Authentication mode error: '.$reply);
10414: 	return 'error: '.$reply;
10415:     }   
10416:     return 'ok';
10417: }
10418: 
10419: # --------------------------------------------------------------- Modify a user
10420: 
10421: sub modifyuser {
10422:     my ($udom,    $uname, $uid,
10423:         $umode,   $upass, $first,
10424:         $middle,  $last,  $gene,
10425:         $forceid, $desiredhome, $email, $inststatus, $candelete)=@_;
10426:     $udom= &LONCAPA::clean_domain($udom);
10427:     $uname=&LONCAPA::clean_username($uname);
10428:     my $showcandelete = 'none';
10429:     if (ref($candelete) eq 'ARRAY') {
10430:         if (@{$candelete} > 0) {
10431:             $showcandelete = join(', ',@{$candelete});
10432:         }
10433:     }
10434:     &logthis('Call to modify user '.$udom.', '.$uname.', '.$uid.', '.
10435:              $umode.', '.$first.', '.$middle.', '.
10436: 	     $last.', '.$gene.'(forceid: '.$forceid.'; candelete: '.$showcandelete.')'.
10437:              (defined($desiredhome) ? ' desiredhome = '.$desiredhome :
10438:                                      ' desiredhome not specified'). 
10439:              ' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
10440:              ' in domain '.$env{'request.role.domain'});
10441:     my $uhome=&homeserver($uname,$udom,'true');
10442:     my $newuser;
10443:     if ($uhome eq 'no_host') {
10444:         $newuser = 1;
10445:         unless (($umode && ($upass ne '')) || ($umode eq 'localauth') ||
10446:                 ($umode eq 'lti')) {
10447:             return 'error: more information needed to create new user';
10448:         }
10449:     }
10450: # ----------------------------------------------------------------- Create User
10451:     if (($uhome eq 'no_host') && 
10452: 	(($umode && $upass) || ($umode eq 'localauth') || ($umode eq 'lti'))) {
10453:         my $unhome='';
10454:         if (defined($desiredhome) && &host_domain($desiredhome) eq $udom) { 
10455:             $unhome = $desiredhome;
10456: 	} elsif($env{'course.'.$env{'request.course.id'}.'.domain'} eq $udom) {
10457: 	    $unhome=$env{'course.'.$env{'request.course.id'}.'.home'};
10458:         } else { # load balancing routine for determining $unhome
10459:             my $loadm=10000000;
10460: 	    my %servers = &get_servers($udom,'library');
10461: 	    foreach my $tryserver (keys(%servers)) {
10462: 		my $answer=reply('load',$tryserver);
10463: 		if (($answer=~/\d+/) && ($answer<$loadm)) {
10464: 		    $loadm=$answer;
10465: 		    $unhome=$tryserver;
10466: 		}
10467: 	    }
10468:         }
10469:         if (($unhome eq '') || ($unhome eq 'no_host')) {
10470: 	    return 'error: unable to find a home server for '.$uname.
10471:                    ' in domain '.$udom;
10472:         }
10473:         my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':'.$umode.':'.
10474:                          &escape($upass),$unhome);
10475: 	unless ($reply eq 'ok') {
10476:             return 'error: '.$reply;
10477:         }   
10478:         $uhome=&homeserver($uname,$udom,'true');
10479:         if (($uhome eq '') || ($uhome eq 'no_host') || ($uhome ne $unhome)) {
10480: 	    return 'error: unable verify users home machine.';
10481:         }
10482:     }   # End of creation of new user
10483: # ---------------------------------------------------------------------- Add ID
10484:     if ($uid) {
10485:        $uid=~tr/A-Z/a-z/;
10486:        my %uidhash=&idrget($udom,$uname);
10487:        if (($uidhash{$uname}) && ($uidhash{$uname}!~/error\:/) 
10488:          && (!$forceid)) {
10489: 	  unless ($uid eq $uidhash{$uname}) {
10490: 	      return 'error: user id "'.$uid.'" does not match '.
10491:                   'current user id "'.$uidhash{$uname}.'".';
10492:           }
10493:        } else {
10494: 	  &idput($udom,{$uname => $uid},$uhome,'ids');
10495:        }
10496:     }
10497: # -------------------------------------------------------------- Add names, etc
10498:     my @tmp=&get('environment',
10499: 		   ['firstname','middlename','lastname','generation','id',
10500:                     'permanentemail','inststatus'],
10501: 		   $udom,$uname);
10502:     my (%names,%oldnames);
10503:     if ($tmp[0] =~ m/^error:.*/) { 
10504:         %names=(); 
10505:     } else {
10506:         %names = @tmp;
10507:         %oldnames = %names;
10508:     }
10509: #
10510: # If name, email and/or uid are blank (e.g., because an uploaded file
10511: # of users did not contain them), do not overwrite existing values
10512: # unless field is in $candelete array ref.  
10513: #
10514: 
10515:     my @fields = ('firstname','middlename','lastname','generation',
10516:                   'permanentemail','id');
10517:     my %newvalues;
10518:     if (ref($candelete) eq 'ARRAY') {
10519:         foreach my $field (@fields) {
10520:             if (grep(/^\Q$field\E$/,@{$candelete})) {
10521:                 if ($field eq 'firstname') {
10522:                     $names{$field} = $first;
10523:                 } elsif ($field eq 'middlename') {
10524:                     $names{$field} = $middle;
10525:                 } elsif ($field eq 'lastname') {
10526:                     $names{$field} = $last;
10527:                 } elsif ($field eq 'generation') { 
10528:                     $names{$field} = $gene;
10529:                 } elsif ($field eq 'permanentemail') {
10530:                     $names{$field} = $email;
10531:                 } elsif ($field eq 'id') {
10532:                     $names{$field}  = $uid;
10533:                 }
10534:             }
10535:         }
10536:     }
10537:     if ($first)  { $names{'firstname'}  = $first; }
10538:     if (defined($middle)) { $names{'middlename'} = $middle; }
10539:     if ($last)   { $names{'lastname'}   = $last; }
10540:     if (defined($gene))   { $names{'generation'} = $gene; }
10541:     if ($email) {
10542:        $email=~s/[^\w\@\.\-\,]//gs;
10543:        if ($email=~/\@/) { $names{'permanentemail'} = $email; }
10544:     }
10545:     if ($uid) { $names{'id'}  = $uid; }
10546:     if (defined($inststatus)) {
10547:         $names{'inststatus'} = '';
10548:         my ($usertypes,$typesorder) = &retrieve_inst_usertypes($udom);
10549:         if (ref($usertypes) eq 'HASH') {
10550:             my @okstatuses; 
10551:             foreach my $item (split(/:/,$inststatus)) {
10552:                 if (defined($usertypes->{$item})) {
10553:                     push(@okstatuses,$item);  
10554:                 }
10555:             }
10556:             if (@okstatuses) {
10557:                 $names{'inststatus'} = join(':', map { &escape($_); } @okstatuses);
10558:             }
10559:         }
10560:     }
10561:     my $logmsg = $udom.', '.$uname.', '.$uid.', '.
10562:                  $umode.', '.$first.', '.$middle.', '.
10563:                  $last.', '.$gene.', '.$email.', '.$inststatus;
10564:     if ($env{'user.name'} ne '' && $env{'user.domain'}) {
10565:         $logmsg .= ' by '.$env{'user.name'}.' at '.$env{'user.domain'};
10566:     } else {
10567:         $logmsg .= ' during self creation';
10568:     }
10569:     my $changed;
10570:     if ($newuser) {
10571:         $changed = 1;
10572:     } else {
10573:         foreach my $field (@fields) {
10574:             if ($names{$field} ne $oldnames{$field}) {
10575:                 $changed = 1;
10576:                 last;
10577:             }
10578:         }
10579:     }
10580:     unless ($changed) {
10581:         $logmsg = 'No changes in user information needed for: '.$logmsg;
10582:         &logthis($logmsg);
10583:         return 'ok';
10584:     }
10585:     my $reply = &put('environment', \%names, $udom,$uname);
10586:     if ($reply ne 'ok') { 
10587:         return 'error: '.$reply;
10588:     }
10589:     if ($names{'permanentemail'} ne $oldnames{'permanentemail'}) {
10590:         &Apache::lonnet::devalidate_cache_new('emailscache',$uname.':'.$udom);
10591:     }
10592:     my $sqlresult = &update_allusers_table($uname,$udom,\%names);
10593:     &devalidate_cache_new('namescache',$uname.':'.$udom);
10594:     $logmsg = 'Success modifying user '.$logmsg;
10595:     &logthis($logmsg);
10596:     return 'ok';
10597: }
10598: 
10599: # -------------------------------------------------------------- Modify student
10600: 
10601: sub modifystudent {
10602:     my ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$usec,
10603:         $end,$start,$forceid,$desiredhome,$email,$type,$locktype,$cid,
10604:         $selfenroll,$context,$inststatus,$credits,$instsec)=@_;
10605:     if (!$cid) {
10606: 	unless ($cid=$env{'request.course.id'}) {
10607: 	    return 'not_in_class';
10608: 	}
10609:     }
10610: # --------------------------------------------------------------- Make the user
10611:     my $reply=&modifyuser
10612: 	($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$forceid,
10613:          $desiredhome,$email,$inststatus);
10614:     unless ($reply eq 'ok') { return $reply; }
10615:     # This will cause &modify_student_enrollment to get the uid from the
10616:     # student's environment
10617:     $uid = undef if (!$forceid);
10618:     $reply = &modify_student_enrollment($udom,$uname,$uid,$first,$middle,$last,
10619:                                         $gene,$usec,$end,$start,$type,$locktype,
10620:                                         $cid,$selfenroll,$context,$credits,$instsec);
10621:     return $reply;
10622: }
10623: 
10624: sub modify_student_enrollment {
10625:     my ($udom,$uname,$uid,$first,$middle,$last,$gene,$usec,$end,$start,$type,
10626:         $locktype,$cid,$selfenroll,$context,$credits,$instsec) = @_;
10627:     my ($cdom,$cnum,$chome);
10628:     if (!$cid) {
10629: 	unless ($cid=$env{'request.course.id'}) {
10630: 	    return 'not_in_class';
10631: 	}
10632: 	$cdom=$env{'course.'.$cid.'.domain'};
10633: 	$cnum=$env{'course.'.$cid.'.num'};
10634:     } else {
10635: 	($cdom,$cnum)=split(/_/,$cid);
10636:     }
10637:     $chome=$env{'course.'.$cid.'.home'};
10638:     if (!$chome) {
10639: 	$chome=&homeserver($cnum,$cdom);
10640:     }
10641:     if (!$chome) { return 'unknown_course'; }
10642:     # Make sure the user exists
10643:     my $uhome=&homeserver($uname,$udom);
10644:     if (($uhome eq '') || ($uhome eq 'no_host')) { 
10645: 	return 'error: no such user';
10646:     }
10647:     # Get student data if we were not given enough information
10648:     if (!defined($first)  || $first  eq '' || 
10649:         !defined($last)   || $last   eq '' || 
10650:         !defined($uid)    || $uid    eq '' || 
10651:         !defined($middle) || $middle eq '' || 
10652:         !defined($gene)   || $gene   eq '') {
10653:         # They did not supply us with enough data to enroll the student, so
10654:         # we need to pick up more information.
10655:         my %tmp = &get('environment',
10656:                        ['firstname','middlename','lastname', 'generation','id']
10657:                        ,$udom,$uname);
10658: 
10659:         #foreach my $key (keys(%tmp)) {
10660:         #    &logthis("key $key = ".$tmp{$key});
10661:         #}
10662:         $first  = $tmp{'firstname'}  if (!defined($first)  || $first  eq '');
10663:         $middle = $tmp{'middlename'} if (!defined($middle) || $middle eq '');
10664:         $last   = $tmp{'lastname'}   if (!defined($last)   || $last eq '');
10665:         $gene   = $tmp{'generation'} if (!defined($gene)   || $gene eq '');
10666:         $uid    = $tmp{'id'}         if (!defined($uid)    || $uid  eq '');
10667:     }
10668:     my $fullname = &format_name($first,$middle,$last,$gene,'lastname');
10669:     my $user = "$uname:$udom";
10670:     my %old_entry = &Apache::lonnet::get('classlist',[$user],$cdom,$cnum);
10671:     my $reply=cput('classlist',
10672: 		   {$user => 
10673: 			join(':',$end,$start,$uid,$usec,$fullname,$type,$locktype,$credits,$instsec) },
10674: 		   $cdom,$cnum);
10675:     if (($reply eq 'ok') || ($reply eq 'delayed')) {
10676:         &devalidate_getsection_cache($udom,$uname,$cid);
10677:     } else { 
10678: 	return 'error: '.$reply;
10679:     }
10680:     # Add student role to user
10681:     my $uurl='/'.$cid;
10682:     $uurl=~s/\_/\//g;
10683:     if ($usec) {
10684: 	$uurl.='/'.$usec;
10685:     }
10686:     my $result = &assignrole($udom,$uname,$uurl,'st',$end,$start,undef,
10687:                              $selfenroll,$context);
10688:     if ($result ne 'ok') {
10689:         if ($old_entry{$user} ne '') {
10690:             $reply = &cput('classlist',\%old_entry,$cdom,$cnum);
10691:         } else {
10692:             $reply = &del('classlist',[$user],$cdom,$cnum);
10693:         }
10694:     }
10695:     return $result; 
10696: }
10697: 
10698: sub format_name {
10699:     my ($firstname,$middlename,$lastname,$generation,$first)=@_;
10700:     my $name;
10701:     if ($first ne 'lastname') {
10702: 	$name=$firstname.' '.$middlename.' '.$lastname.' '.$generation;
10703:     } else {
10704: 	if ($lastname=~/\S/) {
10705: 	    $name.= $lastname.' '.$generation.', '.$firstname.' '.$middlename;
10706: 	    $name=~s/\s+,/,/;
10707: 	} else {
10708: 	    $name.= $firstname.' '.$middlename.' '.$generation;
10709: 	}
10710:     }
10711:     $name=~s/^\s+//;
10712:     $name=~s/\s+$//;
10713:     $name=~s/\s+/ /g;
10714:     return $name;
10715: }
10716: 
10717: # ------------------------------------------------- Write to course preferences
10718: 
10719: sub writecoursepref {
10720:     my ($courseid,%prefs)=@_;
10721:     $courseid=~s/^\///;
10722:     $courseid=~s/\_/\//g;
10723:     my ($cdomain,$cnum)=split(/\//,$courseid);
10724:     my $chome=homeserver($cnum,$cdomain);
10725:     if (($chome eq '') || ($chome eq 'no_host')) { 
10726: 	return 'error: no such course';
10727:     }
10728:     my $cstring='';
10729:     foreach my $pref (keys(%prefs)) {
10730: 	$cstring.=&escape($pref).'='.&escape($prefs{$pref}).'&';
10731:     }
10732:     $cstring=~s/\&$//;
10733:     return reply('put:'.$cdomain.':'.$cnum.':environment:'.$cstring,$chome);
10734: }
10735: 
10736: # ---------------------------------------------------------- Make/modify course
10737: 
10738: sub createcourse {
10739:     my ($udom,$description,$url,$course_server,$nonstandard,$inst_code,
10740:         $course_owner,$crstype,$cnum,$context,$category,$callercontext)=@_;
10741:     $url=&declutter($url);
10742:     my $cid='';
10743:     if ($context eq 'requestcourses') {
10744:         my $can_create = 0;
10745:         my ($ownername,$ownerdom) = split(':',$course_owner);
10746:         if ($udom eq $ownerdom) {
10747:             my $reload;
10748:             if (($callercontext eq 'auto') &&
10749:                ($ownerdom eq $env{'user.domain'}) && ($ownername eq $env{'user.name'})) {
10750:                 $reload = 'reload';
10751:             }
10752:             if (&usertools_access($ownername,$ownerdom,$category,$reload,
10753:                                   $context)) {
10754:                 $can_create = 1;
10755:             }
10756:         } else {
10757:             my %userenv = &userenvironment($ownerdom,$ownername,'reqcrsotherdom.'.
10758:                                            $category);
10759:             if ($userenv{'reqcrsotherdom.'.$category} ne '') {
10760:                 my @curr = split(',',$userenv{'reqcrsotherdom.'.$category});
10761:                 if (@curr > 0) {
10762:                     my @options = qw(approval validate autolimit);
10763:                     my $optregex = join('|',@options);
10764:                     if (grep(/^\Q$udom\E:($optregex)(=?\d*)$/,@curr)) {
10765:                         $can_create = 1;
10766:                     }
10767:                 }
10768:             }
10769:         }
10770:         if ($can_create) {
10771:             unless ($ownername eq $env{'user.name'} && $ownerdom eq $env{'user.domain'}) {
10772:                 unless (&allowed('ccc',$udom)) {
10773:                     return 'refused'; 
10774:                 }
10775:             }
10776:         } else {
10777:             return 'refused';
10778:         }
10779:     } elsif (!&allowed('ccc',$udom)) {
10780:         return 'refused';
10781:     }
10782: # --------------------------------------------------------------- Get Unique ID
10783:     my $uname;
10784:     if ($cnum =~ /^$match_courseid$/) {
10785:         my $chome=&homeserver($cnum,$udom,'true');
10786:         if (($chome eq '') || ($chome eq 'no_host')) {
10787:             $uname = $cnum;
10788:         } else {
10789:             $uname = &generate_coursenum($udom,$crstype);
10790:         }
10791:     } else {
10792:         $uname = &generate_coursenum($udom,$crstype);
10793:     }
10794:     return $uname if ($uname =~ /^error/);
10795: # -------------------------------------------------- Check supplied server name
10796:     if (!defined($course_server)) {
10797:         if (defined(&domain($udom,'primary'))) {
10798:             $course_server = &domain($udom,'primary');
10799:         } else {
10800:             $course_server = $env{'user.home'}; 
10801:         }
10802:     }
10803:     my %host_servers =
10804:         &Apache::lonnet::get_servers($udom,'library');
10805:     unless ($host_servers{$course_server}) {
10806:         return 'error: invalid home server for course: '.$course_server;
10807:     }
10808: # ------------------------------------------------------------- Make the course
10809:     my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':none::',
10810:                       $course_server);
10811:     unless ($reply eq 'ok') { return 'error: '.$reply; }
10812:     my $uhome=&homeserver($uname,$udom,'true');
10813:     if (($uhome eq '') || ($uhome eq 'no_host')) { 
10814: 	return 'error: no such course';
10815:     }
10816: # ----------------------------------------------------------------- Course made
10817: # log existence
10818:     my $now = time;
10819:     my $newcourse = {
10820:                     $udom.'_'.$uname => {
10821:                                      description => $description,
10822:                                      inst_code   => $inst_code,
10823:                                      owner       => $course_owner,
10824:                                      type        => $crstype,
10825:                                      creator     => $env{'user.name'}.':'.
10826:                                                     $env{'user.domain'},
10827:                                      created     => $now,
10828:                                      context     => $context,
10829:                                                 },
10830:                     };
10831:     &courseidput($udom,$newcourse,$uhome,'notime');
10832: # set toplevel url
10833:     my $topurl=$url;
10834:     unless ($nonstandard) {
10835: # ------------------------------------------ For standard courses, make top url
10836:         my $mapurl=&clutter($url);
10837:         if ($mapurl eq '/res/') { $mapurl=''; }
10838:         $env{'form.initmap'}=(<<ENDINITMAP);
10839: <map>
10840: <resource id="1" type="start"></resource>
10841: <resource id="2" src="$mapurl"></resource>
10842: <resource id="3" type="finish"></resource>
10843: <link index="1" from="1" to="2"></link>
10844: <link index="2" from="2" to="3"></link>
10845: </map>
10846: ENDINITMAP
10847:         $topurl=&declutter(
10848:         &finishuserfileupload($uname,$udom,'initmap','default.sequence')
10849:                           );
10850:     }
10851: # ----------------------------------------------------------- Write preferences
10852:     &writecoursepref($udom.'_'.$uname,
10853:                      ('description'              => $description,
10854:                       'url'                      => $topurl,
10855:                       'internal.creator'         => $env{'user.name'}.':'.
10856:                                                     $env{'user.domain'},
10857:                       'internal.created'         => $now,
10858:                       'internal.creationcontext' => $context)
10859:                     );
10860:     return '/'.$udom.'/'.$uname;
10861: }
10862: 
10863: # ------------------------------------------------------------------- Create ID
10864: sub generate_coursenum {
10865:     my ($udom,$crstype) = @_;
10866:     my $domdesc = &domain($udom);
10867:     return 'error: invalid domain' if ($domdesc eq '');
10868:     my $first;
10869:     if ($crstype eq 'Community') {
10870:         $first = '0';
10871:     } else {
10872:         $first = int(1+rand(9)); 
10873:     } 
10874:     my $uname=$first.
10875:         ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
10876:         substr($$.time,0,5).unpack("H8",pack("I32",time)).
10877:         unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
10878: # ----------------------------------------------- Make sure that does not exist
10879:     my $uhome=&homeserver($uname,$udom,'true');
10880:     unless (($uhome eq '') || ($uhome eq 'no_host')) {
10881:         if ($crstype eq 'Community') {
10882:             $first = '0';
10883:         } else {
10884:             $first = int(1+rand(9));
10885:         }
10886:         $uname=$first.
10887:                ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
10888:                substr($$.time,0,5).unpack("H8",pack("I32",time)).
10889:                unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
10890:         $uhome=&homeserver($uname,$udom,'true');
10891:         unless (($uhome eq '') || ($uhome eq 'no_host')) {
10892:             return 'error: unable to generate unique course-ID';
10893:         }
10894:     }
10895:     return $uname;
10896: }
10897: 
10898: sub is_course {
10899:     my ($cdom, $cnum) = scalar(@_) == 1 ? 
10900:          ($_[0] =~ /^($match_domain)_($match_courseid)$/)  :  @_;
10901: 
10902:     return unless (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/));
10903:     my $uhome=&homeserver($cnum,$cdom);
10904:     my $iscourse;
10905:     if (grep { $_ eq $uhome } current_machine_ids()) {
10906:         $iscourse = &LONCAPA::Lond::is_course($cdom,$cnum);
10907:     } else {
10908:         my $hashid = $cdom.':'.$cnum;
10909:         ($iscourse,my $cached) = &is_cached_new('iscourse',$hashid);
10910:         unless (defined($cached)) {
10911:             my %courses = &courseiddump($cdom, '.', 1, '.', '.',
10912:                                         $cnum,undef,undef,'.');
10913:             $iscourse = 0;
10914:             if (exists($courses{$cdom.'_'.$cnum})) {
10915:                 $iscourse = 1;
10916:             }
10917:             &do_cache_new('iscourse',$hashid,$iscourse,3600);
10918:         }
10919:     }
10920:     return unless ($iscourse);
10921:     return wantarray ? ($cdom, $cnum) : $cdom.'_'.$cnum;
10922: }
10923: 
10924: sub store_userdata {
10925:     my ($storehash,$datakey,$namespace,$udom,$uname) = @_;
10926:     my $result;
10927:     if ($datakey ne '') {
10928:         if (ref($storehash) eq 'HASH') {
10929:             if ($udom eq '' || $uname eq '') {
10930:                 $udom = $env{'user.domain'};
10931:                 $uname = $env{'user.name'};
10932:             }
10933:             my $uhome=&homeserver($uname,$udom);
10934:             if (($uhome eq '') || ($uhome eq 'no_host')) {
10935:                 $result = 'error: no_host';
10936:             } else {
10937:                 $storehash->{'ip'} = &get_requestor_ip();
10938:                 $storehash->{'host'} = $perlvar{'lonHostID'};
10939: 
10940:                 my $namevalue='';
10941:                 foreach my $key (keys(%{$storehash})) {
10942:                     $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
10943:                 }
10944:                 $namevalue=~s/\&$//;
10945:                 unless ($namespace eq 'courserequests') {
10946:                     $datakey = &escape($datakey);
10947:                 }
10948:                 $result =  &reply("store:$udom:$uname:$namespace:$datakey:".
10949:                                   $namevalue,$uhome);
10950:             }
10951:         } else {
10952:             $result = 'error: data to store was not a hash reference'; 
10953:         }
10954:     } else {
10955:         $result= 'error: invalid requestkey'; 
10956:     }
10957:     return $result;
10958: }
10959: 
10960: # ---------------------------------------------------------- Assign Custom Role
10961: 
10962: sub assigncustomrole {
10963:     my ($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start,$deleteflag,$selfenroll,$context)=@_;
10964:     return &assignrole($udom,$uname,$url,'cr/'.$rdom.'/'.$rnam.'/'.$rolename,
10965:                        $end,$start,$deleteflag,$selfenroll,$context);
10966: }
10967: 
10968: # ----------------------------------------------------------------- Revoke Role
10969: 
10970: sub revokerole {
10971:     my ($udom,$uname,$url,$role,$deleteflag,$selfenroll,$context)=@_;
10972:     my $now=time;
10973:     return &assignrole($udom,$uname,$url,$role,$now,undef,$deleteflag,$selfenroll,$context);
10974: }
10975: 
10976: # ---------------------------------------------------------- Revoke Custom Role
10977: 
10978: sub revokecustomrole {
10979:     my ($udom,$uname,$url,$rdom,$rnam,$rolename,$deleteflag,$selfenroll,$context)=@_;
10980:     my $now=time;
10981:     return &assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$now,
10982:            $deleteflag,$selfenroll,$context);
10983: }
10984: 
10985: # ------------------------------------------------------------ Disk usage
10986: sub diskusage {
10987:     my ($udom,$uname,$directorypath,$getpropath)=@_;
10988:     $directorypath =~ s/\/$//;
10989:     my $listing=&reply('du2:'.&escape($directorypath).':'
10990:                        .&escape($getpropath).':'.&escape($uname).':'
10991:                        .&escape($udom),homeserver($uname,$udom));
10992:     if ($listing eq 'unknown_cmd') {
10993:         if ($getpropath) {
10994:             $directorypath = &propath($udom,$uname).'/'.$directorypath; 
10995:         }
10996:         $listing = &reply('du:'.$directorypath,homeserver($uname,$udom));
10997:     }
10998:     return $listing;
10999: }
11000: 
11001: sub is_locked {
11002:     my ($file_name, $domain, $user, $which) = @_;
11003:     my @check;
11004:     my $is_locked;
11005:     push (@check,$file_name);
11006:     my %locked = &get('file_permissions',\@check,
11007: 		      $env{'user.domain'},$env{'user.name'});
11008:     my ($tmp)=keys(%locked);
11009:     if ($tmp=~/^error:/) { undef(%locked); }
11010:     
11011:     if (ref($locked{$file_name}) eq 'ARRAY') {
11012:         $is_locked = 'false';
11013:         foreach my $entry (@{$locked{$file_name}}) {
11014:            if (ref($entry) eq 'ARRAY') {
11015:                $is_locked = 'true';
11016:                if (ref($which) eq 'ARRAY') {
11017:                    push(@{$which},$entry);
11018:                } else {
11019:                    last;
11020:                }
11021:            }
11022:        }
11023:     } else {
11024:         $is_locked = 'false';
11025:     }
11026:     return $is_locked;
11027: }
11028: 
11029: sub declutter_portfile {
11030:     my ($file) = @_;
11031:     $file =~ s{^(/portfolio/|portfolio/)}{/};
11032:     return $file;
11033: }
11034: 
11035: # ------------------------------------------------------------- Mark as Read Only
11036: 
11037: sub mark_as_readonly {
11038:     my ($domain,$user,$files,$what) = @_;
11039:     my %current_permissions = &dump('file_permissions',$domain,$user);
11040:     my ($tmp)=keys(%current_permissions);
11041:     if ($tmp=~/^error:/) { undef(%current_permissions); }
11042:     foreach my $file (@{$files}) {
11043: 	$file = &declutter_portfile($file);
11044:         push(@{$current_permissions{$file}},$what);
11045:     }
11046:     &put('file_permissions',\%current_permissions,$domain,$user);
11047:     return;
11048: }
11049: 
11050: # ------------------------------------------------------------Save Selected Files
11051: 
11052: sub save_selected_files {
11053:     my ($user, $path, @files) = @_;
11054:     my $filename = $user."savedfiles";
11055:     my @other_files = &files_not_in_path($user, $path);
11056:     open (OUT,'>',LONCAPA::tempdir().$filename);
11057:     foreach my $file (@files) {
11058:         print (OUT $env{'form.currentpath'}.$file."\n");
11059:     }
11060:     foreach my $file (@other_files) {
11061:         print (OUT $file."\n");
11062:     }
11063:     close (OUT);
11064:     return 'ok';
11065: }
11066: 
11067: sub clear_selected_files {
11068:     my ($user) = @_;
11069:     my $filename = $user."savedfiles";
11070:     open (OUT,'>',LONCAPA::tempdir().$filename);
11071:     print (OUT undef);
11072:     close (OUT);
11073:     return ("ok");    
11074: }
11075: 
11076: sub files_in_path {
11077:     my ($user, $path) = @_;
11078:     my $filename = $user."savedfiles";
11079:     my %return_files;
11080:     open (IN,'<',LONCAPA::tempdir().$filename);
11081:     while (my $line_in = <IN>) {
11082:         chomp ($line_in);
11083:         my @paths_and_file = split (m!/!, $line_in);
11084:         my $file_part = pop (@paths_and_file);
11085:         my $path_part = join ('/', @paths_and_file);
11086:         $path_part.='/';
11087:         my $path_and_file = $path_part.$file_part;
11088:         if ($path_part eq $path) {
11089:             $return_files{$file_part}= 'selected';
11090:         }
11091:     }
11092:     close (IN);
11093:     return (\%return_files);
11094: }
11095: 
11096: # called in portfolio select mode, to show files selected NOT in current directory
11097: sub files_not_in_path {
11098:     my ($user, $path) = @_;
11099:     my $filename = $user."savedfiles";
11100:     my @return_files;
11101:     my $path_part;
11102:     open(IN, '<',LONCAPA::tempdir().$filename);
11103:     while (my $line = <IN>) {
11104:         #ok, I know it's clunky, but I want it to work
11105:         my @paths_and_file = split(m|/|, $line);
11106:         my $file_part = pop(@paths_and_file);
11107:         chomp($file_part);
11108:         my $path_part = join('/', @paths_and_file);
11109:         $path_part .= '/';
11110:         my $path_and_file = $path_part.$file_part;
11111:         if ($path_part ne $path) {
11112:             push(@return_files, ($path_and_file));
11113:         }
11114:     }
11115:     close(OUT);
11116:     return (@return_files);
11117: }
11118: 
11119: #------------------------------Submitted/Handedback Portfolio Files Versioning
11120:  
11121: sub portfiles_versioning {
11122:     my ($symb,$domain,$stu_name,$portfiles,$versioned_portfiles) = @_;
11123:     my $portfolio_root = '/userfiles/portfolio';
11124:     return unless ((ref($portfiles) eq 'ARRAY') && (ref($versioned_portfiles) eq 'ARRAY'));
11125:     foreach my $file (@{$portfiles}) {
11126:         &unmark_as_readonly($domain,$stu_name,[$symb,$env{'request.course.id'}],$file);
11127:         my ($directory,$answer_file) =($file =~ /^(.*?)([^\/]*)$/);
11128:         my ($answer_name,$answer_ver,$answer_ext) = &file_name_version_ext($answer_file);
11129:         my $getpropath = 1;
11130:         my ($dir_list,$listerror) = &dirlist($portfolio_root.$directory,$domain,
11131:                                              $stu_name,$getpropath);
11132:         my $version = &get_next_version($answer_name,$answer_ext,$dir_list);
11133:         my $new_answer = 
11134:             &version_selected_portfile($domain,$stu_name,$directory,$answer_file,$version);
11135:         if ($new_answer ne 'problem getting file') {
11136:             push(@{$versioned_portfiles}, $directory.$new_answer);
11137:             &mark_as_readonly($domain,$stu_name,[$directory.$new_answer],
11138:                               [$symb,$env{'request.course.id'},'graded']);
11139:         }
11140:     }
11141: }
11142: 
11143: sub get_next_version {
11144:     my ($answer_name, $answer_ext, $dir_list) = @_;
11145:     my $version;
11146:     if (ref($dir_list) eq 'ARRAY') {
11147:         foreach my $row (@{$dir_list}) {
11148:             my ($file) = split(/\&/,$row,2);
11149:             my ($file_name,$file_version,$file_ext) =
11150:                 &file_name_version_ext($file);
11151:             if (($file_name eq $answer_name) &&
11152:                 ($file_ext eq $answer_ext)) {
11153:                      # gets here if filename and extension match,
11154:                      # regardless of version
11155:                 if ($file_version ne '') {
11156:                     # a versioned file is found  so save it for later
11157:                     if ($file_version > $version) {
11158:                         $version = $file_version;
11159:                     }
11160:                 }
11161:             }
11162:         }
11163:     }
11164:     $version ++;
11165:     return($version);
11166: }
11167: 
11168: sub version_selected_portfile {
11169:     my ($domain,$stu_name,$directory,$file_name,$version) = @_;
11170:     my ($answer_name,$answer_ver,$answer_ext) =
11171:         &file_name_version_ext($file_name);
11172:     my $new_answer;
11173:     $env{'form.copy'} =
11174:         &getfile("/uploaded/$domain/$stu_name/portfolio$directory$file_name");
11175:     if($env{'form.copy'} eq '-1') {
11176:         $new_answer = 'problem getting file';
11177:     } else {
11178:         $new_answer = $answer_name.'.'.$version.'.'.$answer_ext;
11179:         my $copy_result = 
11180:             &finishuserfileupload($stu_name,$domain,'copy',
11181:                                   '/portfolio'.$directory.$new_answer);
11182:     }
11183:     undef($env{'form.copy'});
11184:     return ($new_answer);
11185: }
11186: 
11187: sub file_name_version_ext {
11188:     my ($file)=@_;
11189:     my @file_parts = split(/\./, $file);
11190:     my ($name,$version,$ext);
11191:     if (@file_parts > 1) {
11192:         $ext=pop(@file_parts);
11193:         if (@file_parts > 1 && $file_parts[-1] =~ /^\d+$/) {
11194:             $version=pop(@file_parts);
11195:         }
11196:         $name=join('.',@file_parts);
11197:     } else {
11198:         $name=join('.',@file_parts);
11199:     }
11200:     return($name,$version,$ext);
11201: }
11202: 
11203: #----------------------------------------------Get portfolio file permissions
11204: 
11205: sub get_portfile_permissions {
11206:     my ($domain,$user) = @_;
11207:     my %current_permissions = &dump('file_permissions',$domain,$user);
11208:     my ($tmp)=keys(%current_permissions);
11209:     if ($tmp=~/^error:/) { undef(%current_permissions); }
11210:     return \%current_permissions;
11211: }
11212: 
11213: #---------------------------------------------Get portfolio file access controls
11214: 
11215: sub get_access_controls {
11216:     my ($current_permissions,$group,$file) = @_;
11217:     my %access;
11218:     my $real_file = $file;
11219:     $file =~ s/\.meta$//;
11220:     if (defined($file)) {
11221:         if (ref($$current_permissions{$file."\0".'accesscontrol'}) eq 'HASH') {
11222:             foreach my $control (keys(%{$$current_permissions{$file."\0".'accesscontrol'}})) {
11223:                 $access{$real_file}{$control} = $$current_permissions{$file."\0".$control};
11224:             }
11225:         }
11226:     } else {
11227:         foreach my $key (keys(%{$current_permissions})) {
11228:             if ($key =~ /\0accesscontrol$/) {
11229:                 if (defined($group)) {
11230:                     if ($key !~ m-^\Q$group\E/-) {
11231:                         next;
11232:                     }
11233:                 }
11234:                 my ($fullpath) = split(/\0/,$key);
11235:                 if (ref($$current_permissions{$key}) eq 'HASH') {
11236:                     foreach my $control (keys(%{$$current_permissions{$key}})) {
11237:                         $access{$fullpath}{$control}=$$current_permissions{$fullpath."\0".$control};
11238:                     }
11239:                 }
11240:             }
11241:         }
11242:     }
11243:     return %access;
11244: }
11245: 
11246: sub modify_access_controls {
11247:     my ($file_name,$changes,$domain,$user)=@_;
11248:     my ($outcome,$deloutcome);
11249:     my %store_permissions;
11250:     my %new_values;
11251:     my %new_control;
11252:     my %translation;
11253:     my @deletions = ();
11254:     my $now = time;
11255:     if (exists($$changes{'activate'})) {
11256:         if (ref($$changes{'activate'}) eq 'HASH') {
11257:             my @newitems = sort(keys(%{$$changes{'activate'}}));
11258:             my $numnew = scalar(@newitems);
11259:             for (my $i=0; $i<$numnew; $i++) {
11260:                 my $newkey = $newitems[$i];
11261:                 my $newid = &Apache::loncommon::get_cgi_id();
11262:                 if ($newkey =~ /^\d+:/) { 
11263:                     $newkey =~ s/^(\d+)/$newid/;
11264:                     $translation{$1} = $newid;
11265:                 } elsif ($newkey =~ /^\d+_\d+_\d+:/) {
11266:                     $newkey =~ s/^(\d+_\d+_\d+)/$newid/;
11267:                     $translation{$1} = $newid;
11268:                 }
11269:                 $new_values{$file_name."\0".$newkey} = 
11270:                                           $$changes{'activate'}{$newitems[$i]};
11271:                 $new_control{$newkey} = $now;
11272:             }
11273:         }
11274:     }
11275:     my %todelete;
11276:     my %changed_items;
11277:     foreach my $action ('delete','update') {
11278:         if (exists($$changes{$action})) {
11279:             if (ref($$changes{$action}) eq 'HASH') {
11280:                 foreach my $key (keys(%{$$changes{$action}})) {
11281:                     my ($itemnum) = ($key =~ /^([^:]+):/);
11282:                     if ($action eq 'delete') { 
11283:                         $todelete{$itemnum} = 1;
11284:                     } else {
11285:                         $changed_items{$itemnum} = $key;
11286:                     }
11287:                 }
11288:             }
11289:         }
11290:     }
11291:     # get lock on access controls for file.
11292:     my $lockhash = {
11293:                   $file_name."\0".'locked_access_records' => $env{'user.name'}.
11294:                                                        ':'.$env{'user.domain'},
11295:                    }; 
11296:     my $tries = 0;
11297:     my $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
11298:    
11299:     while (($gotlock ne 'ok') && $tries < 10) {
11300:         $tries ++;
11301:         sleep(0.1);
11302:         $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
11303:     }
11304:     if ($gotlock eq 'ok') {
11305:         my %curr_permissions = &dump('file_permissions',$domain,$user,$file_name);
11306:         my ($tmp)=keys(%curr_permissions);
11307:         if ($tmp=~/^error:/) { undef(%curr_permissions); }
11308:         if (exists($curr_permissions{$file_name."\0".'accesscontrol'})) {
11309:             my $curr_controls = $curr_permissions{$file_name."\0".'accesscontrol'};
11310:             if (ref($curr_controls) eq 'HASH') {
11311:                 foreach my $control_item (keys(%{$curr_controls})) {
11312:                     my ($itemnum) = ($control_item =~ /^([^:]+):/);
11313:                     if (defined($todelete{$itemnum})) {
11314:                         push(@deletions,$file_name."\0".$control_item);
11315:                     } else {
11316:                         if (defined($changed_items{$itemnum})) {
11317:                             $new_control{$changed_items{$itemnum}} = $now;
11318:                             push(@deletions,$file_name."\0".$control_item);
11319:                             $new_values{$file_name."\0".$changed_items{$itemnum}} = $$changes{'update'}{$changed_items{$itemnum}};
11320:                         } else {
11321:                             $new_control{$control_item} = $$curr_controls{$control_item};
11322:                         }
11323:                     }
11324:                 }
11325:             }
11326:         }
11327:         my ($group);
11328:         if (&is_course($domain,$user)) {
11329:             ($group,my $file) = split(/\//,$file_name,2);
11330:         }
11331:         $deloutcome = &del('file_permissions',\@deletions,$domain,$user);
11332:         $new_values{$file_name."\0".'accesscontrol'} = \%new_control;
11333:         $outcome = &put('file_permissions',\%new_values,$domain,$user);
11334:         #  remove lock
11335:         my @del_lock = ($file_name."\0".'locked_access_records');
11336:         my $dellockoutcome = &del('file_permissions',\@del_lock,$domain,$user);
11337:         my $sqlresult =
11338:             &update_portfolio_table($user,$domain,$file_name,'portfolio_access',
11339:                                     $group);
11340:     } else {
11341:         $outcome = "error: could not obtain lockfile\n";  
11342:     }
11343:     return ($outcome,$deloutcome,\%new_values,\%translation);
11344: }
11345: 
11346: sub make_public_indefinitely {
11347:     my (@requrl) = @_;
11348:     return &automated_portfile_access('public',\@requrl);
11349: }
11350: 
11351: sub automated_portfile_access {
11352:     my ($accesstype,$addsref,$delsref,$info) = @_;
11353:     unless (($accesstype eq 'public') || ($accesstype eq 'ip')) {
11354:         return 'invalid';
11355:     }
11356:     my %urls;
11357:     if (ref($addsref) eq 'ARRAY') {
11358:         foreach my $requrl (@{$addsref}) {
11359:             if (&is_portfolio_url($requrl)) {
11360:                 unless (exists($urls{$requrl})) {
11361:                     $urls{$requrl} = 'add';
11362:                 }
11363:             }
11364:         }
11365:     }
11366:     if (ref($delsref) eq 'ARRAY') {
11367:         foreach my $requrl (@{$delsref}) { 
11368:             if (&is_portfolio_url($requrl)) {
11369:                 unless (exists($urls{$requrl})) {
11370:                     $urls{$requrl} = 'delete'; 
11371:                 }
11372:             }
11373:         }
11374:     }
11375:     unless (keys(%urls)) {
11376:         return 'invalid';
11377:     }
11378:     my $ip;
11379:     if ($accesstype eq 'ip') {
11380:         if (ref($info) eq 'HASH') {
11381:             if ($info->{'ip'} ne '') {
11382:                 $ip = $info->{'ip'};
11383:             }
11384:         }
11385:         if ($ip eq '') {
11386:             return 'invalid';
11387:         }
11388:     }
11389:     my $errors;
11390:     my $now = time;
11391:     my %current_perms;
11392:     foreach my $requrl (sort(keys(%urls))) {
11393:         my $action;
11394:         if ($urls{$requrl} eq 'add') {
11395:             $action = 'activate';
11396:         } else {
11397:             $action = 'none';
11398:         }
11399:         my $aclnum = 0;
11400:         my (undef,$udom,$unum,$file_name,$group) =
11401:             &parse_portfolio_url($requrl);
11402:         unless (exists($current_perms{$unum.':'.$udom})) {
11403:             $current_perms{$unum.':'.$udom} = &get_portfile_permissions($udom,$unum);
11404:         }
11405:         my %access_controls = &get_access_controls($current_perms{$unum.':'.$udom},
11406:                                                    $group,$file_name);
11407:         foreach my $key (keys(%{$access_controls{$file_name}})) {
11408:             my ($num,$scope,$end,$start) = 
11409:                 ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
11410:             if ($scope eq $accesstype) {
11411:                 if (($start <= $now) && ($end == 0)) {
11412:                     if ($accesstype eq 'ip') {
11413:                         if (ref($access_controls{$file_name}{$key}) eq 'HASH') {
11414:                             if (ref($access_controls{$file_name}{$key}{'ip'}) eq 'ARRAY') {
11415:                                 if (grep(/^\Q$ip\E$/,@{$access_controls{$file_name}{$key}{'ip'}})) {
11416:                                     if ($urls{$requrl} eq 'add') {
11417:                                         $action = 'none';
11418:                                         last;
11419:                                     } else {
11420:                                         $action = 'delete';
11421:                                         $aclnum = $num;
11422:                                         last;
11423:                                     }
11424:                                 }
11425:                             }
11426:                         }
11427:                     } elsif ($accesstype eq 'public') {
11428:                         if ($urls{$requrl} eq 'add') {
11429:                             $action = 'none';
11430:                             last;
11431:                         } else {
11432:                             $action = 'delete';
11433:                             $aclnum = $num;
11434:                             last;
11435:                         }
11436:                     }
11437:                 } elsif ($accesstype eq 'public') {
11438:                     $action = 'update';
11439:                     $aclnum = $num;
11440:                     last;
11441:                 }
11442:             }
11443:         }
11444:         if ($action eq 'none') {
11445:             next;
11446:         } else {
11447:             my %changes;
11448:             my $newend = 0;
11449:             my $newstart = $now;
11450:             my $newkey = $aclnum.':'.$accesstype.'_'.$newend.'_'.$newstart;
11451:             $changes{$action}{$newkey} = {
11452:                 type => $accesstype,
11453:                 time => {
11454:                     start => $newstart,
11455:                     end   => $newend,
11456:                 },
11457:             };
11458:             if ($accesstype eq 'ip') {
11459:                 $changes{$action}{$newkey}{'ip'} = [$ip];
11460:             }
11461:             my ($outcome,$deloutcome,$new_values,$translation) =
11462:                 &modify_access_controls($file_name,\%changes,$udom,$unum);
11463:             unless ($outcome eq 'ok') {
11464:                 $errors .= $outcome.' ';
11465:             }
11466:         }
11467:     }
11468:     if ($errors) {
11469:         $errors =~ s/\s$//;
11470:         return $errors;
11471:     } else {
11472:         return 'ok';
11473:     }
11474: }
11475: 
11476: #------------------------------------------------------Get Marked as Read Only
11477: 
11478: sub get_marked_as_readonly {
11479:     my ($domain,$user,$what,$group) = @_;
11480:     my $current_permissions = &get_portfile_permissions($domain,$user);
11481:     my @readonly_files;
11482:     my $cmp1=$what;
11483:     if (ref($what)) { $cmp1=join('',@{$what}) };
11484:     while (my ($file_name,$value) = each(%{$current_permissions})) {
11485:         if (defined($group)) {
11486:             if ($file_name !~ m-^\Q$group\E/-) {
11487:                 next;
11488:             }
11489:         }
11490:         if (ref($value) eq "ARRAY"){
11491:             foreach my $stored_what (@{$value}) {
11492:                 my $cmp2=$stored_what;
11493:                 if (ref($stored_what) eq 'ARRAY') {
11494:                     $cmp2=join('',@{$stored_what});
11495:                 }
11496:                 if ($cmp1 eq $cmp2) {
11497:                     push(@readonly_files, $file_name);
11498:                     last;
11499:                 } elsif (!defined($what)) {
11500:                     push(@readonly_files, $file_name);
11501:                     last;
11502:                 }
11503:             }
11504:         }
11505:     }
11506:     return @readonly_files;
11507: }
11508: #-----------------------------------------------------------Get Marked as Read Only Hash
11509: 
11510: sub get_marked_as_readonly_hash {
11511:     my ($current_permissions,$group,$what) = @_;
11512:     my %readonly_files;
11513:     while (my ($file_name,$value) = each(%{$current_permissions})) {
11514:         if (defined($group)) {
11515:             if ($file_name !~ m-^\Q$group\E/-) {
11516:                 next;
11517:             }
11518:         }
11519:         if (ref($value) eq "ARRAY"){
11520:             foreach my $stored_what (@{$value}) {
11521:                 if (ref($stored_what) eq 'ARRAY') {
11522:                     foreach my $lock_descriptor(@{$stored_what}) {
11523:                         if ($lock_descriptor eq 'graded') {
11524:                             $readonly_files{$file_name} = 'graded';
11525:                         } elsif ($lock_descriptor eq 'handback') {
11526:                             $readonly_files{$file_name} = 'handback';
11527:                         } else {
11528:                             if (!exists($readonly_files{$file_name})) {
11529:                                 $readonly_files{$file_name} = 'locked';
11530:                             }
11531:                         }
11532:                     }
11533:                 } 
11534:             }
11535:         } 
11536:     }
11537:     return %readonly_files;
11538: }
11539: # ------------------------------------------------------------ Unmark as Read Only
11540: 
11541: sub unmark_as_readonly {
11542:     # unmarks $file_name (if $file_name is defined), or all files locked by $what 
11543:     # for portfolio submissions, $what contains [$symb,$crsid] 
11544:     my ($domain,$user,$what,$file_name,$group) = @_;
11545:     $file_name = &declutter_portfile($file_name);
11546:     my $symb_crs = $what;
11547:     if (ref($what)) { $symb_crs=join('',@$what); }
11548:     my %current_permissions = &dump('file_permissions',$domain,$user,$group);
11549:     my ($tmp)=keys(%current_permissions);
11550:     if ($tmp=~/^error:/) { undef(%current_permissions); }
11551:     my @readonly_files = &get_marked_as_readonly($domain,$user,$what,$group);
11552:     foreach my $file (@readonly_files) {
11553: 	my $clean_file = &declutter_portfile($file);
11554: 	if (defined($file_name) && ($file_name ne $clean_file)) { next; }
11555: 	my $current_locks = $current_permissions{$file};
11556:         my @new_locks;
11557:         my @del_keys;
11558:         if (ref($current_locks) eq "ARRAY"){
11559:             foreach my $locker (@{$current_locks}) {
11560:                 my $compare=$locker;
11561:                 if (ref($locker) eq 'ARRAY') {
11562:                     $compare=join('',@{$locker});
11563:                     if ($compare ne $symb_crs) {
11564:                         push(@new_locks, $locker);
11565:                     }
11566:                 }
11567:             }
11568:             if (scalar(@new_locks) > 0) {
11569:                 $current_permissions{$file} = \@new_locks;
11570:             } else {
11571:                 push(@del_keys, $file);
11572:                 &del('file_permissions',\@del_keys, $domain, $user);
11573:                 delete($current_permissions{$file});
11574:             }
11575:         }
11576:     }
11577:     &put('file_permissions',\%current_permissions,$domain,$user);
11578:     return;
11579: }
11580: 
11581: # ------------------------------------------------------------ Directory lister
11582: 
11583: sub dirlist {
11584:     my ($uri,$userdomain,$username,$getpropath,$getuserdir,$alternateRoot)=@_;
11585:     $uri=~s/^\///;
11586:     $uri=~s/\/$//;
11587:     my ($udom, $uname);
11588:     if ($getuserdir) {
11589:         $udom = $userdomain;
11590:         $uname = $username;
11591:     } else {
11592:         (undef,$udom,$uname)=split(/\//,$uri);
11593:         if(defined($userdomain)) {
11594:             $udom = $userdomain;
11595:         }
11596:         if(defined($username)) {
11597:             $uname = $username;
11598:         }
11599:     }
11600:     my ($dirRoot,$listing,@listing_results);
11601: 
11602:     $dirRoot = $perlvar{'lonDocRoot'};
11603:     if (defined($getpropath)) {
11604:         $dirRoot = &propath($udom,$uname);
11605:         $dirRoot =~ s/\/$//;
11606:     } elsif (defined($getuserdir)) {
11607:         my $subdir=$uname.'__';
11608:         $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
11609:         $dirRoot = $Apache::lonnet::perlvar{'lonUsersDir'}
11610:                    ."/$udom/$subdir/$uname";
11611:     } elsif (defined($alternateRoot)) {
11612:         $dirRoot = $alternateRoot;
11613:     }
11614: 
11615:     if($udom) {
11616:         if($uname) {
11617:             my $uhome = &homeserver($uname,$udom);
11618:             if ($uhome eq 'no_host') {
11619:                 return ([],'no_host');
11620:             }
11621:             $listing = &reply('ls3:'.&escape('/'.$uri).':'.$getpropath.':'
11622:                               .$getuserdir.':'.&escape($dirRoot)
11623:                               .':'.&escape($uname).':'.&escape($udom),$uhome);
11624:             if ($listing eq 'unknown_cmd') {
11625:                 $listing = &reply('ls2:'.$dirRoot.'/'.$uri,$uhome);
11626:             } else {
11627:                 @listing_results = map { &unescape($_); } split(/:/,$listing);
11628:             }
11629:             if ($listing eq 'unknown_cmd') {
11630:                 $listing = &reply('ls:'.$dirRoot.'/'.$uri,$uhome);
11631:                 @listing_results = split(/:/,$listing);
11632:             } else {
11633:                 @listing_results = map { &unescape($_); } split(/:/,$listing);
11634:             }
11635:             if (($listing eq 'no_such_host') || ($listing eq 'con_lost') || 
11636:                 ($listing eq 'rejected') || ($listing eq 'refused') ||
11637:                 ($listing eq 'no_such_dir') || ($listing eq 'empty')) {
11638:                 return ([],$listing);
11639:             } else {
11640:                 return (\@listing_results);
11641:             }
11642:         } elsif(!$alternateRoot) {
11643:             my (%allusers,%listerror);
11644: 	    my %servers = &get_servers($udom,'library');
11645:  	    foreach my $tryserver (keys(%servers)) {
11646:                 $listing = &reply('ls3:'.&escape("/res/$udom").':::::'.
11647:                                   &escape($udom),$tryserver);
11648:                 if ($listing eq 'unknown_cmd') {
11649: 		    $listing = &reply('ls2:'.$perlvar{'lonDocRoot'}.'/res/'.
11650: 				      $udom, $tryserver);
11651:                 } else {
11652:                     @listing_results = map { &unescape($_); } split(/:/,$listing);
11653:                 }
11654: 		if ($listing eq 'unknown_cmd') {
11655: 		    $listing = &reply('ls:'.$perlvar{'lonDocRoot'}.'/res/'.
11656: 				      $udom, $tryserver);
11657: 		    @listing_results = split(/:/,$listing);
11658: 		} else {
11659: 		    @listing_results =
11660: 			map { &unescape($_); } split(/:/,$listing);
11661: 		}
11662:                 if (($listing eq 'no_such_host') || ($listing eq 'con_lost') ||
11663:                     ($listing eq 'rejected') || ($listing eq 'refused') ||
11664:                     ($listing eq 'no_such_dir') || ($listing eq 'empty')) {
11665:                     $listerror{$tryserver} = $listing;
11666:                 } else {
11667: 		    foreach my $line (@listing_results) {
11668: 			my ($entry) = split(/&/,$line,2);
11669: 			$allusers{$entry} = 1;
11670: 		    }
11671: 		}
11672:             }
11673:             my @alluserslist=();
11674:             foreach my $user (sort(keys(%allusers))) {
11675:                 push(@alluserslist,$user.'&user');
11676:             }
11677: 
11678:             if (!%listerror) {
11679:                 # no errors
11680:                 return (\@alluserslist);
11681:             } elsif (scalar(keys(%servers)) == 1) {
11682:                 # one library server, one error 
11683:                 my ($key) = keys(%listerror);
11684:                 return (\@alluserslist, $listerror{$key});
11685:             } elsif ( grep { $_ eq 'con_lost' } values(%listerror) ) {
11686:                 # con_lost indicates that we might miss data from at least one
11687:                 # library server
11688:                 return (\@alluserslist, 'con_lost');
11689:             } else {
11690:                 # multiple library servers and no con_lost -> data should be
11691:                 # complete. 
11692:                 return (\@alluserslist);
11693:             }
11694: 
11695:         } else {
11696:             return ([],'missing username');
11697:         }
11698:     } elsif(!defined($getpropath)) {
11699:         my $path = $perlvar{'lonDocRoot'}.'/res/'; 
11700:         my @all_domains = map { $path.$_.'/&domain'; } (sort(&all_domains()));
11701:         return (\@all_domains);
11702:     } else {
11703:         return ([],'missing domain');
11704:     }
11705: }
11706: 
11707: # --------------------------------------------- GetFileTimestamp
11708: # This function utilizes dirlist and returns the date stamp for
11709: # when it was last modified.  It will also return an error of -1
11710: # if an error occurs
11711: 
11712: sub GetFileTimestamp {
11713:     my ($studentDomain,$studentName,$filename,$getuserdir)=@_;
11714:     $studentDomain = &LONCAPA::clean_domain($studentDomain);
11715:     $studentName   = &LONCAPA::clean_username($studentName);
11716:     my ($fileref,$error) = &dirlist($filename,$studentDomain,$studentName,
11717:                                     undef,$getuserdir);
11718:     if (($error eq 'empty') || ($error eq 'no_such_dir')) {
11719:         return -1;
11720:     }
11721:     if (ref($fileref) eq 'ARRAY') {
11722:         my @stats = split('&',$fileref->[0]);
11723:         # @stats contains first the filename, then the stat output
11724:         return $stats[10]; # so this is 10 instead of 9.
11725:     } else {
11726:         return -1;
11727:     }
11728: }
11729: 
11730: sub stat_file {
11731:     my ($uri) = @_;
11732:     $uri = &clutter_with_no_wrapper($uri);
11733: 
11734:     my ($udom,$uname,$file);
11735:     if ($uri =~ m-^/(uploaded|editupload)/-) {
11736: 	($udom,$uname,$file) =
11737: 	    ($uri =~ m-/(?:uploaded|editupload)/?($match_domain)/?($match_name)/?(.*)-);
11738: 	$file = 'userfiles/'.$file;
11739:     }
11740:     if ($uri =~ m-^/res/-) {
11741: 	($udom,$uname) = 
11742: 	    ($uri =~ m-/(?:res)/?($match_domain)/?($match_username)/-);
11743: 	$file = $uri;
11744:     }
11745: 
11746:     if (!$udom || !$uname || !$file) {
11747: 	# unable to handle the uri
11748: 	return ();
11749:     }
11750:     my $getpropath;
11751:     if ($file =~ /^userfiles\//) {
11752:         $getpropath = 1;
11753:     }
11754:     my ($listref,$error) = &dirlist($file,$udom,$uname,$getpropath);
11755:     if (($error eq 'empty') || ($error eq 'no_such_dir')) {
11756:         return ();
11757:     } else {
11758:         if (ref($listref) eq 'ARRAY') {
11759:             my @stats = split('&',$listref->[0]);
11760: 	    shift(@stats); #filename is first
11761: 	    return @stats;
11762:         }
11763:     }
11764:     return ();
11765: }
11766: 
11767: # --------------------------------------------------------- recursedirs
11768: # Recursive function to traverse either a specific user's Authoring Space
11769: # or corresponding Published Resource Space, and populate the hash ref:
11770: # $dirhashref with URLs of all directories, and if $filehashref hash
11771: # ref arg is provided, the URLs of any files, excluding versioned, .meta,
11772: # or .rights files in resource space, and .meta, .save, .log, and .bak
11773: # files in Authoring Space.
11774: #
11775: # Inputs:
11776: #
11777: # $is_home - true if current server is home server for user's space
11778: # $context - either: priv, or res respectively for Authoring or Resource Space.
11779: # $docroot - Document root (i.e., /home/httpd/html
11780: # $toppath - Top level directory (i.e., /res/$dom/$uname or /priv/$dom/$uname
11781: # $relpath - Current path (relative to top level).
11782: # $dirhashref - reference to hash to populate with URLs of directories (Required)
11783: # $filehashref - reference to hash to populate with URLs of files (Optional)
11784: #
11785: # Returns: nothing
11786: #
11787: # Side Effects: populates $dirhashref, and $filehashref (if provided).
11788: #
11789: # Currently used by interface/londocs.pm to create linked select boxes for
11790: # directory and filename to import a Course "Author" resource into a course, and
11791: # also to create linked select boxes for Authoring Space and Directory to choose
11792: # save location for creation of a new "standard" problem from the Course Editor.
11793: #
11794: 
11795: sub recursedirs {
11796:     my ($is_home,$context,$docroot,$toppath,$relpath,$dirhashref,$filehashref) = @_;
11797:     return unless (ref($dirhashref) eq 'HASH');
11798:     my $currpath = $docroot.$toppath;
11799:     if ($relpath) {
11800:         $currpath .= "/$relpath";
11801:     }
11802:     my $savefile;
11803:     if (ref($filehashref)) {
11804:         $savefile = 1;
11805:     }
11806:     if ($is_home) {
11807:         if (opendir(my $dirh,$currpath)) {
11808:             foreach my $item (sort { lc($a) cmp lc($b) } grep(!/^\.+$/,readdir($dirh))) {
11809:                 next if ($item eq '');
11810:                 if (-d "$currpath/$item") {
11811:                     my $newpath;
11812:                     if ($relpath) {
11813:                         $newpath = "$relpath/$item";
11814:                     } else {
11815:                         $newpath = $item;
11816:                     }
11817:                     $dirhashref->{&Apache::lonlocal::js_escape($newpath)} = 1;
11818:                     &recursedirs($is_home,$context,$docroot,$toppath,$newpath,$dirhashref,$filehashref);
11819:                 } elsif ($savefile) {
11820:                     if ($context eq 'priv') {
11821:                         unless ($item =~ /\.(meta|save|log|bak|DS_Store)$/) {
11822:                             $filehashref->{&Apache::lonlocal::js_escape($relpath)}{$item} = 1;
11823:                         }
11824:                     } else {
11825:                         unless (($item =~ /\.meta$/) || ($item =~ /\.\d+\.\w+$/) || ($item =~ /\.rights$/)) {
11826:                             $filehashref->{&Apache::lonlocal::js_escape($relpath)}{$item} = 1;
11827:                         }
11828:                     }
11829:                 }
11830:             }
11831:             closedir($dirh);
11832:         }
11833:     } else {
11834:         my ($dirlistref,$listerror) =
11835:             &dirlist($toppath.$relpath);
11836:         my @dir_lines;
11837:         my $dirptr=16384;
11838:         if (ref($dirlistref) eq 'ARRAY') {
11839:             foreach my $dir_line (sort
11840:                               {
11841:                                   my ($afile)=split('&',$a,2);
11842:                                   my ($bfile)=split('&',$b,2);
11843:                                   return (lc($afile) cmp lc($bfile));
11844:                               } (@{$dirlistref})) {
11845:                 my ($item,$dom,undef,$testdir,undef,undef,undef,undef,$size,undef,$mtime,undef,undef,undef,$obs,undef) =
11846:                     split(/\&/,$dir_line,16);
11847:                 $item =~ s/\s+$//;
11848:                 next if (($item =~ /^\.\.?$/) || ($obs));
11849:                 if ($dirptr&$testdir) {
11850:                     my $newpath;
11851:                     if ($relpath) {
11852:                         $newpath = "$relpath/$item";
11853:                     } else {
11854:                         $relpath = '/';
11855:                         $newpath = $item;
11856:                     }
11857:                     $dirhashref->{&Apache::lonlocal::js_escape($newpath)} = 1;
11858:                     &recursedirs($is_home,$context,$docroot,$toppath,$newpath,$dirhashref,$filehashref);
11859:                 } elsif ($savefile) {
11860:                     if ($context eq 'priv') {
11861:                         unless ($item =~ /\.(meta|save|log|bak|DS_Store)$/) {
11862:                             $filehashref->{$relpath}{$item} = 1;
11863:                         }
11864:                     } else {
11865:                         unless (($item =~ /\.meta$/) || ($item =~ /\.\d+\.\w+$/)) {
11866:                             $filehashref->{$relpath}{$item} = 1;
11867:                         }
11868:                     }
11869:                 }
11870:             }
11871:         }
11872:     }
11873:     return;
11874: }
11875: 
11876: # -------------------------------------------------------- Value of a Condition
11877: 
11878: # gets the value of a specific preevaluated condition
11879: #    stored in the string  $env{user.state.<cid>}
11880: # or looks up a condition reference in the bighash and if if hasn't
11881: # already been evaluated recurses into docondval to get the value of
11882: # the condition, then memoizing it to 
11883: #   $env{user.state.<cid>.<condition>}
11884: sub directcondval {
11885:     my $number=shift;
11886:     if (!defined($env{'user.state.'.$env{'request.course.id'}})) {
11887: 	&Apache::lonuserstate::evalstate();
11888:     }
11889:     if (exists($env{'user.state.'.$env{'request.course.id'}.".$number"})) {
11890: 	return $env{'user.state.'.$env{'request.course.id'}.".$number"};
11891:     } elsif ($number =~ /^_/) {
11892: 	my $sub_condition;
11893: 	if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
11894: 		&GDBM_READER(),0640)) {
11895: 	    $sub_condition=$bighash{'conditions'.$number};
11896: 	    untie(%bighash);
11897: 	}
11898: 	my $value = &docondval($sub_condition);
11899: 	&appenv({'user.state.'.$env{'request.course.id'}.".$number" => $value});
11900: 	return $value;
11901:     }
11902:     if ($env{'user.state.'.$env{'request.course.id'}}) {
11903:        return substr($env{'user.state.'.$env{'request.course.id'}},$number,1);
11904:     } else {
11905:        return 2;
11906:     }
11907: }
11908: 
11909: # get the collection of conditions for this resource
11910: sub condval {
11911:     my $condidx=shift;
11912:     my $allpathcond='';
11913:     foreach my $cond (split(/\|/,$condidx)) {
11914: 	if (defined($env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond})) {
11915: 	    $allpathcond.=
11916: 		'('.$env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond}.')|';
11917: 	}
11918:     }
11919:     $allpathcond=~s/\|$//;
11920:     return &docondval($allpathcond);
11921: }
11922: 
11923: #evaluates an expression of conditions
11924: sub docondval {
11925:     my ($allpathcond) = @_;
11926:     my $result=0;
11927:     if ($env{'request.course.id'}
11928: 	&& defined($allpathcond)) {
11929: 	my $operand='|';
11930: 	my @stack;
11931: 	foreach my $chunk ($allpathcond=~/(\d+|_\d+\.\d+|\(|\)|\&|\|)/g) {
11932: 	    if ($chunk eq '(') {
11933: 		push @stack,($operand,$result);
11934: 	    } elsif ($chunk eq ')') {
11935: 		my $before=pop @stack;
11936: 		if (pop @stack eq '&') {
11937: 		    $result=$result>$before?$before:$result;
11938: 		} else {
11939: 		    $result=$result>$before?$result:$before;
11940: 		}
11941: 	    } elsif (($chunk eq '&') || ($chunk eq '|')) {
11942: 		$operand=$chunk;
11943: 	    } else {
11944: 		my $new=directcondval($chunk);
11945: 		if ($operand eq '&') {
11946: 		    $result=$result>$new?$new:$result;
11947: 		} else {
11948: 		    $result=$result>$new?$result:$new;
11949: 		}
11950: 	    }
11951: 	}
11952:     }
11953:     return $result;
11954: }
11955: 
11956: # ---------------------------------------------------- Devalidate courseresdata
11957: 
11958: sub devalidatecourseresdata {
11959:     my ($coursenum,$coursedomain)=@_;
11960:     my $hashid=$coursenum.':'.$coursedomain;
11961:     &devalidate_cache_new('courseres',$hashid);
11962: }
11963: 
11964: 
11965: # --------------------------------------------------- Course Resourcedata Query
11966: #
11967: #  Parameters:
11968: #      $coursenum    - Number of the course.
11969: #      $coursedomain - Domain at which the course was created.
11970: #  Returns:
11971: #     A hash of the course parameters along (I think) with timestamps
11972: #     and version info.
11973: 
11974: sub get_courseresdata {
11975:     my ($coursenum,$coursedomain)=@_;
11976:     my $coursehom=&homeserver($coursenum,$coursedomain);
11977:     my $hashid=$coursenum.':'.$coursedomain;
11978:     my ($result,$cached)=&is_cached_new('courseres',$hashid);
11979:     my %dumpreply;
11980:     unless (defined($cached)) {
11981: 	%dumpreply=&dump('resourcedata',$coursedomain,$coursenum);
11982: 	$result=\%dumpreply;
11983: 	my ($tmp) = keys(%dumpreply);
11984: 	if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
11985: 	    &do_cache_new('courseres',$hashid,$result,600);
11986: 	} elsif ($tmp =~ /^(con_lost|no_such_host)/) {
11987: 	    return $tmp;
11988: 	} elsif ($tmp =~ /^(error)/) {
11989: 	    $result=undef;
11990: 	    &do_cache_new('courseres',$hashid,$result,600);
11991: 	}
11992:     }
11993:     return $result;
11994: }
11995: 
11996: sub devalidateuserresdata {
11997:     my ($uname,$udom)=@_;
11998:     my $hashid="$udom:$uname";
11999:     &devalidate_cache_new('userres',$hashid);
12000: }
12001: 
12002: sub get_userresdata {
12003:     my ($uname,$udom)=@_;
12004:     #most student don\'t have any data set, check if there is some data
12005:     if (&EXT_cache_status($udom,$uname)) { return undef; }
12006: 
12007:     my $hashid="$udom:$uname";
12008:     my ($result,$cached)=&is_cached_new('userres',$hashid);
12009:     if (!defined($cached)) {
12010: 	my %resourcedata=&dump('resourcedata',$udom,$uname);
12011: 	$result=\%resourcedata;
12012: 	&do_cache_new('userres',$hashid,$result,600);
12013:     }
12014:     my ($tmp)=keys(%$result);
12015:     if (($tmp!~/^error\:/) && ($tmp!~/^con_lost/)) {
12016: 	return $result;
12017:     }
12018:     #error 2 occurs when the .db doesn't exist
12019:     if ($tmp!~/error: 2 /) {
12020:         if ((!defined($cached)) || ($tmp ne 'con_lost')) {
12021: 	    &logthis("<font color=\"blue\">WARNING:".
12022: 		     " Trying to get resource data for ".
12023: 		     $uname." at ".$udom.": ".
12024: 		     $tmp."</font>");
12025:         }
12026:     } elsif ($tmp=~/error: 2 /) {
12027: 	#&EXT_cache_set($udom,$uname);
12028: 	&do_cache_new('userres',$hashid,undef,600);
12029: 	undef($tmp); # not really an error so don't send it back
12030:     }
12031:     return $tmp;
12032: }
12033: #----------------------------------------------- resdata - return resource data
12034: #  Purpose:
12035: #    Return resource data for either users or for a course.
12036: #  Parameters:
12037: #     $name      - Course/user name.
12038: #     $domain    - Name of the domain the user/course is registered on.
12039: #     $type      - Type of thing $name is (must be 'course' or 'user')
12040: #     $mapp      - decluttered URL of enclosing map  
12041: #     $recursed  - Ref to scalar -- set to 1, if nested maps have been recursed.
12042: #     $recurseup - Ref to array of map URLs, starting with map containing
12043: #                  $mapp up through hierarchy of nested maps to top level map.  
12044: #     $courseid  - CourseID (first part of param identifier).
12045: #     $modifier  - Middle part of param identifier.
12046: #     $what      - Last part of param identifier.
12047: #     @which     - Array of names of resources desired.
12048: #  Returns:
12049: #     The value of the first reasource in @which that is found in the
12050: #     resource hash.
12051: #  Exceptional Conditions:
12052: #     If the $type passed in is not valid (not the string 'course' or 
12053: #     'user', an undefined  reference is returned.
12054: #     If none of the resources are found, an undef is returned
12055: sub resdata {
12056:     my ($name,$domain,$type,$mapp,$recursed,$recurseup,$courseid,
12057:         $modifier,$what,@which)=@_;
12058:     my $result;
12059:     if ($type eq 'course') {
12060: 	$result=&get_courseresdata($name,$domain);
12061:     } elsif ($type eq 'user') {
12062: 	$result=&get_userresdata($name,$domain);
12063:     }
12064:     if (!ref($result)) { return $result; }    
12065:     foreach my $item (@which) {
12066:         if ($item->[1] eq 'course') {
12067:             if ((ref($recurseup) eq 'ARRAY') && (ref($recursed) eq 'SCALAR')) {
12068:                 unless ($$recursed) {
12069:                     @{$recurseup} = &get_map_hierarchy($mapp,$courseid);
12070:                     $$recursed = 1;
12071:                 }
12072:                 foreach my $item (@${recurseup}) {
12073:                     my $norecursechk=$courseid.$modifier.$item.'___(all).'.$what;
12074:                     last if (defined($result->{$norecursechk}));
12075:                     my $recursechk=$courseid.$modifier.$item.'___(rec).'.$what;
12076:                     if (defined($result->{$recursechk})) { return [$result->{$recursechk},'map']; }
12077:                 }
12078:             }
12079:         }
12080:         if (defined($result->{$item->[0]})) {
12081: 	    return [$result->{$item->[0]},$item->[1]];
12082: 	}
12083:     }
12084:     return undef;
12085: }
12086: 
12087: sub get_domain_lti {
12088:     my ($cdom,$context) = @_;
12089:     my ($name,%lti);
12090:     if ($context eq 'consumer') {
12091:         $name = 'ltitools';
12092:     } elsif ($context eq 'provider') {
12093:         $name = 'lti';
12094:     } else {
12095:         return %lti;
12096:     }
12097:     my ($result,$cached)=&is_cached_new($name,$cdom);
12098:     if (defined($cached)) {
12099:         if (ref($result) eq 'HASH') {
12100:             %lti = %{$result};
12101:         }
12102:     } else {
12103:         my %domconfig = &get_dom('configuration',[$name],$cdom);
12104:         if (ref($domconfig{$name}) eq 'HASH') {
12105:             %lti = %{$domconfig{$name}};
12106:             my %encdomconfig = &get_dom('encconfig',[$name],$cdom);
12107:             if (ref($encdomconfig{$name}) eq 'HASH') {
12108:                 foreach my $id (keys(%lti)) {
12109:                     if (ref($encdomconfig{$name}{$id}) eq 'HASH') {
12110:                         foreach my $item ('key','secret') {
12111:                             $lti{$id}{$item} = $encdomconfig{$name}{$id}{$item};
12112:                         }
12113:                     }
12114:                 }
12115:             }
12116:         }
12117:         my $cachetime = 24*60*60;
12118:         &do_cache_new($name,$cdom,\%lti,$cachetime);
12119:     }
12120:     return %lti;
12121: }
12122: 
12123: sub get_numsuppfiles {
12124:     my ($cnum,$cdom,$ignorecache)=@_;
12125:     my $hashid=$cnum.':'.$cdom;
12126:     my ($suppcount,$cached);
12127:     unless ($ignorecache) {
12128:         ($suppcount,$cached) = &is_cached_new('suppcount',$hashid);
12129:     }
12130:     unless (defined($cached)) {
12131:         my $chome=&homeserver($cnum,$cdom);
12132:         unless ($chome eq 'no_host') {
12133:             ($suppcount,my $supptools,my $errors) = (0,0,0);
12134:             my $suppmap = 'supplemental.sequence';
12135:             ($suppcount,$supptools,$errors) =
12136:                 &Apache::loncommon::recurse_supplemental($cnum,$cdom,$suppmap,$suppcount,
12137:                                                          $supptools,$errors);
12138:         }
12139:         &do_cache_new('suppcount',$hashid,$suppcount,600);
12140:     }
12141:     return $suppcount;
12142: }
12143: 
12144: #
12145: # EXT resource caching routines
12146: #
12147: 
12148: {
12149: # Cache (5 seconds) of map hierarchy for speedup of navmaps display
12150: #
12151: # The course for which we cache
12152: my $cachedmapkey='';
12153: # The cached recursive maps for this course
12154: my %cachedmaps=();
12155: # When this was last done
12156: my $cachedmaptime='';
12157: 
12158: sub clear_EXT_cache_status {
12159:     &delenv('cache.EXT.');
12160: }
12161: 
12162: sub EXT_cache_status {
12163:     my ($target_domain,$target_user) = @_;
12164:     my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
12165:     if (exists($env{$cachename}) && ($env{$cachename}+600) > time) {
12166:         # We know already the user has no data
12167:         return 1;
12168:     } else {
12169:         return 0;
12170:     }
12171: }
12172: 
12173: sub EXT_cache_set {
12174:     my ($target_domain,$target_user) = @_;
12175:     my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
12176:     #&appenv({$cachename => time});
12177: }
12178: 
12179: # --------------------------------------------------------- Value of a Variable
12180: sub EXT {
12181: 
12182:     my ($varname,$symbparm,$udom,$uname,$usection,$recurse,$cid)=@_;
12183:     unless ($varname) { return ''; }
12184:     #get real user name/domain, courseid and symb
12185:     my $courseid;
12186:     my $publicuser;
12187:     if ($symbparm) {
12188: 	$symbparm=&get_symb_from_alias($symbparm);
12189:     }
12190:     if (!($uname && $udom)) {
12191:       (my $cursymb,$courseid,$udom,$uname,$publicuser)= &whichuser($symbparm);
12192:       if (!$symbparm) {	$symbparm=$cursymb; }
12193:     } else {
12194: 	$courseid=$env{'request.course.id'};
12195:     }
12196:     my ($realm,$space,$qualifier,@therest)=split(/\./,$varname);
12197:     my $rest;
12198:     if (defined($therest[0])) {
12199:        $rest=join('.',@therest);
12200:     } else {
12201:        $rest='';
12202:     }
12203: 
12204:     my $qualifierrest=$qualifier;
12205:     if ($rest) { $qualifierrest.='.'.$rest; }
12206:     my $spacequalifierrest=$space;
12207:     if ($qualifierrest) { $spacequalifierrest.='.'.$qualifierrest; }
12208:     if ($realm eq 'user') {
12209: # --------------------------------------------------------------- user.resource
12210: 	if ($space eq 'resource') {
12211: 	    if ( (defined($Apache::lonhomework::parsing_a_problem)
12212: 		  || defined($Apache::lonhomework::parsing_a_task))
12213: 		 &&
12214: 		 ($symbparm eq &symbread()) ) {	
12215: 		# if we are in the middle of processing the resource the
12216: 		# get the value we are planning on committing
12217:                 if (defined($Apache::lonhomework::results{$qualifierrest})) {
12218:                     return $Apache::lonhomework::results{$qualifierrest};
12219:                 } else {
12220:                     return $Apache::lonhomework::history{$qualifierrest};
12221:                 }
12222: 	    } else {
12223: 		my %restored;
12224: 		if ($publicuser || $env{'request.state'} eq 'construct') {
12225: 		    %restored=&tmprestore($symbparm,$courseid,$udom,$uname);
12226: 		} else {
12227: 		    %restored=&restore($symbparm,$courseid,$udom,$uname);
12228: 		}
12229: 		return $restored{$qualifierrest};
12230: 	    }
12231: # ----------------------------------------------------------------- user.access
12232:         } elsif ($space eq 'access') {
12233: 	    # FIXME - not supporting calls for a specific user
12234:             return &allowed($qualifier,$rest);
12235: # ------------------------------------------ user.preferences, user.environment
12236:         } elsif (($space eq 'preferences') || ($space eq 'environment')) {
12237: 	    if (($uname eq $env{'user.name'}) &&
12238: 		($udom eq $env{'user.domain'})) {
12239: 		return $env{join('.',('environment',$qualifierrest))};
12240: 	    } else {
12241: 		my %returnhash;
12242: 		if (!$publicuser) {
12243: 		    %returnhash=&userenvironment($udom,$uname,
12244: 						 $qualifierrest);
12245: 		}
12246: 		return $returnhash{$qualifierrest};
12247: 	    }
12248: # ----------------------------------------------------------------- user.course
12249:         } elsif ($space eq 'course') {
12250: 	    # FIXME - not supporting calls for a specific user
12251:             return $env{join('.',('request.course',$qualifier))};
12252: # ------------------------------------------------------------------- user.role
12253:         } elsif ($space eq 'role') {
12254: 	    # FIXME - not supporting calls for a specific user
12255:             my ($role,$where)=split(/\./,$env{'request.role'});
12256:             if ($qualifier eq 'value') {
12257: 		return $role;
12258:             } elsif ($qualifier eq 'extent') {
12259:                 return $where;
12260:             }
12261: # ----------------------------------------------------------------- user.domain
12262:         } elsif ($space eq 'domain') {
12263:             return $udom;
12264: # ------------------------------------------------------------------- user.name
12265:         } elsif ($space eq 'name') {
12266:             return $uname;
12267: # ---------------------------------------------------- Any other user namespace
12268:         } else {
12269: 	    my %reply;
12270: 	    if (!$publicuser) {
12271: 		%reply=&get($space,[$qualifierrest],$udom,$uname);
12272: 	    }
12273: 	    return $reply{$qualifierrest};
12274:         }
12275:     } elsif ($realm eq 'query') {
12276: # ---------------------------------------------- pull stuff out of query string
12277:         &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
12278: 						[$spacequalifierrest]);
12279: 	return $env{'form.'.$spacequalifierrest}; 
12280:    } elsif ($realm eq 'request') {
12281: # ------------------------------------------------------------- request.browser
12282:         if ($space eq 'browser') {
12283:             return $env{'browser.'.$qualifier};
12284: # ------------------------------------------------------------ request.filename
12285:         } else {
12286:             return $env{'request.'.$spacequalifierrest};
12287:         }
12288:     } elsif ($realm eq 'course') {
12289: # ---------------------------------------------------------- course.description
12290:         return $env{'course.'.$courseid.'.'.$spacequalifierrest};
12291:     } elsif ($realm eq 'resource') {
12292: 
12293: 	if (defined($courseid) && $courseid eq $env{'request.course.id'}) {
12294: 	    if (!$symbparm) { $symbparm=&symbread(); }
12295: 	}
12296: 
12297:         if ($qualifier eq '') {
12298: 	    if ($space eq 'title') {
12299: 	        if (!$symbparm) { $symbparm = $env{'request.filename'}; }
12300: 	        return &gettitle($symbparm);
12301: 	    }
12302: 	
12303: 	    if ($space eq 'map') {
12304: 	        my ($map) = &decode_symb($symbparm);
12305: 	        return &symbread($map);
12306: 	    }
12307:             if ($space eq 'maptitle') {
12308:                 my ($map) = &decode_symb($symbparm);
12309:                 return &gettitle($map);
12310:             }
12311: 	    if ($space eq 'filename') {
12312: 	        if ($symbparm) {
12313: 		    return &clutter((&decode_symb($symbparm))[2]);
12314: 	        }
12315: 	        return &hreflocation('',$env{'request.filename'});
12316: 	    }
12317: 
12318:             if ((defined($courseid)) && ($courseid eq $env{'request.course.id'}) && $symbparm) {
12319:                 if ($space eq 'visibleparts') {
12320:                     my $navmap = Apache::lonnavmaps::navmap->new();
12321:                     my $item;
12322:                     if (ref($navmap)) {
12323:                         my $res = $navmap->getBySymb($symbparm);
12324:                         my $parts = $res->parts();
12325:                         if (ref($parts) eq 'ARRAY') {
12326:                             $item = join(',',@{$parts});
12327:                         }
12328:                         undef($navmap);
12329:                     }
12330:                     return $item;
12331:                 }
12332:             }
12333:         }
12334: 
12335: 	my ($section, $group, @groups, @recurseup, $recursed);
12336: 	my ($courselevelm,$courseleveli,$courselevel,$mapp);
12337:         if (($courseid eq '') && ($cid)) {
12338:             $courseid = $cid;
12339:         }
12340: 	if (($symbparm && $courseid) && 
12341: 	    (($courseid eq $env{'request.course.id'}) || ($courseid eq $cid)))  {
12342: 
12343: 	    #print '<br>'.$space.' - '.$qualifier.' - '.$spacequalifierrest;
12344: 
12345: # ----------------------------------------------------- Cascading lookup scheme
12346: 	    my $symbp=$symbparm;
12347: 	    $mapp=&deversion((&decode_symb($symbp))[0]);
12348: 	    my $symbparm=$symbp.'.'.$spacequalifierrest;
12349:             my $recurseparm=$mapp.'___(rec).'.$spacequalifierrest;
12350: 	    my $mapparm=$mapp.'___(all).'.$spacequalifierrest;
12351: 	    if (($env{'user.name'} eq $uname) &&
12352: 		($env{'user.domain'} eq $udom)) {
12353: 		$section=$env{'request.course.sec'};
12354:                 @groups = split(/:/,$env{'request.course.groups'});  
12355:                 @groups=&sort_course_groups($courseid,@groups); 
12356: 	    } else {
12357: 		if (! defined($usection)) {
12358: 		    $section=&getsection($udom,$uname,$courseid);
12359: 		} else {
12360: 		    $section = $usection;
12361: 		}
12362:                 @groups = &get_users_groups($udom,$uname,$courseid);
12363: 	    }
12364: 
12365: 	    my $seclevel=$courseid.'.['.$section.'].'.$spacequalifierrest;
12366: 	    my $seclevelr=$courseid.'.['.$section.'].'.$symbparm;
12367:             my $secleveli=$courseid.'.['.$section.'].'.$recurseparm;
12368: 	    my $seclevelm=$courseid.'.['.$section.'].'.$mapparm;
12369: 
12370: 	    $courselevel=$courseid.'.'.$spacequalifierrest;
12371: 	    my $courselevelr=$courseid.'.'.$symbparm;
12372:             $courseleveli=$courseid.'.'.$recurseparm;
12373: 	    $courselevelm=$courseid.'.'.$mapparm;
12374: 
12375: # ----------------------------------------------------------- first, check user
12376: 
12377: 	    my $userreply=&resdata($uname,$udom,'user',$mapp,\$recursed,
12378:                                    \@recurseup,$courseid,'.',$spacequalifierrest, 
12379: 				       ([$courselevelr,'resource'],
12380: 					[$courselevelm,'map'     ],
12381:                                         [$courseleveli,'map'     ],
12382: 					[$courselevel, 'course'  ]));
12383: 	    if (defined($userreply)) { return &get_reply($userreply); }
12384: 
12385: # ------------------------------------------------ second, check some of course
12386:             my $coursereply;
12387:             if (@groups > 0) {
12388:                 $coursereply = &check_group_parms($courseid,\@groups,$symbparm,
12389:                                        $recurseparm,$mapparm,$spacequalifierrest,
12390:                                        $mapp,\$recursed,\@recurseup);
12391:                 if (defined($coursereply)) { return &get_reply($coursereply); } 
12392:             }
12393: 
12394: 	    $coursereply=&resdata($env{'course.'.$courseid.'.num'},
12395: 				  $env{'course.'.$courseid.'.domain'},
12396: 				  'course',$mapp,\$recursed,\@recurseup,
12397:                                   $courseid,'.['.$section.'].',$spacequalifierrest,
12398: 				  ([$seclevelr,   'resource'],
12399: 				   [$seclevelm,   'map'     ],
12400:                                    [$secleveli,   'map'     ],
12401: 				   [$seclevel,    'course'  ],
12402: 				   [$courselevelr,'resource']));
12403: 	    if (defined($coursereply)) { return &get_reply($coursereply); }
12404: 
12405: # ------------------------------------------------------ third, check map parms
12406: 	    my %parmhash=();
12407: 	    my $thisparm='';
12408: 	    if (tie(%parmhash,'GDBM_File',
12409: 		    $env{'request.course.fn'}.'_parms.db',
12410: 		    &GDBM_READER(),0640)) {
12411: 		$thisparm=$parmhash{$symbparm};
12412: 		untie(%parmhash);
12413: 	    }
12414: 	    if ($thisparm) { return &get_reply([$thisparm,'resource']); }
12415: 	}
12416: # ------------------------------------------ fourth, look in resource metadata
12417:  
12418:         my $what = $spacequalifierrest;
12419: 	$what=~s/\./\_/;
12420: 	my $filename;
12421: 	if (!$symbparm) { $symbparm=&symbread(); }
12422: 	if ($symbparm) {
12423: 	    $filename=(&decode_symb($symbparm))[2];
12424: 	} else {
12425: 	    $filename=$env{'request.filename'};
12426: 	}
12427:         my $toolsymb;
12428:         if (($filename =~ /ext\.tool$/) && ($what ne '0_gradable')) {
12429:             $toolsymb = $symbparm;
12430:         }
12431: 	my $metadata=&metadata($filename,$what,$toolsymb);
12432: 	if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
12433: 	$metadata=&metadata($filename,'parameter_'.$what,$toolsymb);
12434: 	if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
12435: 
12436: # ----------------------------------------------- fifth, look in rest of course
12437: 	if ($symbparm && defined($courseid) && 
12438: 	    $courseid eq $env{'request.course.id'}) {
12439: 	    my $coursereply=&resdata($env{'course.'.$courseid.'.num'},
12440: 				     $env{'course.'.$courseid.'.domain'},
12441: 				     'course',$mapp,\$recursed,\@recurseup,
12442:                                      $courseid,'.',$spacequalifierrest,
12443: 				     ([$courselevelm,'map'   ],
12444:                                       [$courseleveli,'map'   ],
12445: 				      [$courselevel, 'course']));
12446: 	    if (defined($coursereply)) { return &get_reply($coursereply); }
12447: 	}
12448: # ------------------------------------------------------------------ Cascade up
12449: 	unless ($space eq '0') {
12450: 	    my @parts=split(/_/,$space);
12451: 	    my $id=pop(@parts);
12452: 	    my $part=join('_',@parts);
12453: 	    if ($part eq '') { $part='0'; }
12454: 	    my @partgeneral=&EXT('resource.'.$part.'.'.$qualifierrest,
12455: 				 $symbparm,$udom,$uname,$section,1);
12456: 	    if (defined($partgeneral[0])) { return &get_reply(\@partgeneral); }
12457: 	}
12458: 	if ($recurse) { return undef; }
12459: 	my $pack_def=&packages_tab_default($filename,$varname,$toolsymb);
12460: 	if (defined($pack_def)) { return &get_reply([$pack_def,'resource']); }
12461: # ---------------------------------------------------- Any other user namespace
12462:     } elsif ($realm eq 'environment') {
12463: # ----------------------------------------------------------------- environment
12464: 	if (($uname eq $env{'user.name'})&&($udom eq $env{'user.domain'})) {
12465: 	    return $env{'environment.'.$spacequalifierrest};
12466: 	} else {
12467: 	    if ($uname eq 'anonymous' && $udom eq '') {
12468: 		return '';
12469: 	    }
12470: 	    my %returnhash=&userenvironment($udom,$uname,
12471: 					    $spacequalifierrest);
12472: 	    return $returnhash{$spacequalifierrest};
12473: 	}
12474:     } elsif ($realm eq 'system') {
12475: # ----------------------------------------------------------------- system.time
12476: 	if ($space eq 'time') {
12477: 	    return time;
12478:         }
12479:     } elsif ($realm eq 'server') {
12480: # ----------------------------------------------------------------- system.time
12481: 	if ($space eq 'name') {
12482: 	    return $ENV{'SERVER_NAME'};
12483:         }
12484:     } elsif ($realm eq 'client') {
12485:         if ($space eq 'remote_addr') {
12486:             return &get_requestor_ip();
12487:         }
12488:     }
12489:     return '';
12490: }
12491: 
12492: sub get_reply {
12493:     my ($reply_value) = @_;
12494:     if (ref($reply_value) eq 'ARRAY') {
12495:         if (wantarray) {
12496: 	    return @$reply_value;
12497:         }
12498:         return $reply_value->[0];
12499:     } else {
12500:         return $reply_value;
12501:     }
12502: }
12503: 
12504: sub check_group_parms {
12505:     my ($courseid,$groups,$symbparm,$recurseparm,$mapparm,$what,$mapp,
12506:         $recursed,$recurseupref) = @_;
12507:     my @levels = ([$symbparm,'resource'],[$mapparm,'map'],[$recurseparm,'map'],
12508:                   [$what,'course']);
12509:     my $coursereply;
12510:     foreach my $group (@{$groups}) {
12511:         my @groupitems = ();
12512:         foreach my $level (@levels) {
12513:              my $item = $courseid.'.['.$group.'].'.$level->[0];
12514:              push(@groupitems,[$item,$level->[1]]);
12515:         }
12516:         my $coursereply = &resdata($env{'course.'.$courseid.'.num'},
12517:                                    $env{'course.'.$courseid.'.domain'},
12518:                                    'course',$mapp,$recursed,$recurseupref,
12519:                                    $courseid,'.['.$group.'].',$what,
12520:                                    @groupitems);
12521:         last if (defined($coursereply));
12522:     }
12523:     return $coursereply;
12524: }
12525: 
12526: sub get_map_hierarchy {
12527:     my ($mapname,$courseid) = @_;
12528:     my @recurseup = ();
12529:     if ($mapname) {
12530:         if (($cachedmapkey eq $courseid) &&
12531:             (abs($cachedmaptime-time)<5)) {
12532:             if (ref($cachedmaps{$mapname}) eq 'ARRAY') {
12533:                 return @{$cachedmaps{$mapname}};
12534:             }
12535:         }
12536:         my $navmap = Apache::lonnavmaps::navmap->new();
12537:         if (ref($navmap)) {
12538:             @recurseup = $navmap->recurseup_maps($mapname);
12539:             undef($navmap);
12540:             $cachedmaps{$mapname} = \@recurseup;
12541:             $cachedmaptime=time;
12542:             $cachedmapkey=$courseid;
12543:         }
12544:     }
12545:     return @recurseup;
12546: }
12547: 
12548: }
12549: 
12550: sub sort_course_groups { # Sort groups based on defined rankings. Default is sort().
12551:     my ($courseid,@groups) = @_;
12552:     @groups = sort(@groups);
12553:     return @groups;
12554: }
12555: 
12556: sub packages_tab_default {
12557:     my ($uri,$varname,$toolsymb)=@_;
12558:     my (undef,$part,$name)=split(/\./,$varname);
12559: 
12560:     my (@extension,@specifics,$do_default);
12561:     foreach my $package (split(/,/,&metadata($uri,'packages',$toolsymb))) {
12562: 	my ($pack_type,$pack_part)=split(/_/,$package,2);
12563: 	if ($pack_type eq 'default') {
12564: 	    $do_default=1;
12565: 	} elsif ($pack_type eq 'extension') {
12566: 	    push(@extension,[$package,$pack_type,$pack_part]);
12567: 	} elsif ($pack_part eq $part || $pack_type eq 'part') {
12568: 	    # only look at packages defaults for packages that this id is
12569: 	    push(@specifics,[$package,$pack_type,$pack_part]);
12570: 	}
12571:     }
12572:     # first look for a package that matches the requested part id
12573:     foreach my $package (@specifics) {
12574: 	my (undef,$pack_type,$pack_part)=@{$package};
12575: 	next if ($pack_part ne $part);
12576: 	if (defined($packagetab{"$pack_type&$name&default"})) {
12577: 	    return $packagetab{"$pack_type&$name&default"};
12578: 	}
12579:     }
12580:     # look for any possible matching non extension_ package
12581:     foreach my $package (@specifics) {
12582: 	my (undef,$pack_type,$pack_part)=@{$package};
12583: 	if (defined($packagetab{"$pack_type&$name&default"})) {
12584: 	    return $packagetab{"$pack_type&$name&default"};
12585: 	}
12586: 	if ($pack_type eq 'part') { $pack_part='0'; }
12587: 	if (defined($packagetab{$pack_type."_".$pack_part."&$name&default"})) {
12588: 	    return $packagetab{$pack_type."_".$pack_part."&$name&default"};
12589: 	}
12590:     }
12591:     # look for any posible extension_ match
12592:     foreach my $package (@extension) {
12593: 	my ($package,$pack_type)=@{$package};
12594: 	if (defined($packagetab{"$pack_type&$name&default"})) {
12595: 	    return $packagetab{"$pack_type&$name&default"};
12596: 	}
12597: 	if (defined($packagetab{$package."&$name&default"})) {
12598: 	    return $packagetab{$package."&$name&default"};
12599: 	}
12600:     }
12601:     # look for a global default setting
12602:     if ($do_default && defined($packagetab{"default&$name&default"})) {
12603: 	return $packagetab{"default&$name&default"};
12604:     }
12605:     return undef;
12606: }
12607: 
12608: sub add_prefix_and_part {
12609:     my ($prefix,$part)=@_;
12610:     my $keyroot;
12611:     if (defined($prefix) && $prefix !~ /^__/) {
12612: 	# prefix that has a part already
12613: 	$keyroot=$prefix;
12614:     } elsif (defined($prefix)) {
12615: 	# prefix that is missing a part
12616: 	if (defined($part)) { $keyroot='_'.$part.substr($prefix,1); }
12617:     } else {
12618: 	# no prefix at all
12619: 	if (defined($part)) { $keyroot='_'.$part; }
12620:     }
12621:     return $keyroot;
12622: }
12623: 
12624: # ---------------------------------------------------------------- Get metadata
12625: 
12626: my %metaentry;
12627: my %importedpartids;
12628: my %importedrespids;
12629: sub metadata {
12630:     my ($uri,$what,$toolsymb,$liburi,$prefix,$depthcount)=@_;
12631:     $uri=&declutter($uri);
12632:     # if it is a non metadata possible uri return quickly
12633:     if (($uri eq '') || 
12634: 	(($uri =~ m|^/*adm/|) && 
12635: 	     ($uri !~ m|^adm/includes|) && ($uri !~ m{/(smppg|bulletinboard|ext\.tool)$})) ||
12636:         ($uri =~ m|/$|) || ($uri =~ m|/.meta$|) || ($uri =~ m{^/*uploaded/.+\.sequence$})) {
12637: 	return undef;
12638:     }
12639:     if (($uri =~ /^priv/ || $uri=~m{^home/httpd/html/priv}) 
12640: 	&& &Apache::lonxml::get_state('target') =~ /^(|meta)$/) {
12641: 	return undef;
12642:     }
12643:     my $filename=$uri;
12644:     $uri=~s/\.meta$//;
12645: #
12646: # Is the metadata already cached?
12647: # Look at timestamp of caching
12648: # Everything is cached by the main uri, libraries are never directly cached
12649: #
12650:     if (!defined($liburi)) {
12651: 	my ($result,$cached)=&is_cached_new('meta',$uri);
12652: 	if (defined($cached)) { return $result->{':'.$what}; }
12653:     }
12654: 
12655: #
12656: # If the uri is for an external tool the file from
12657: # which metadata should be retrieved depends on whether
12658: # the tool had been configured to be gradable (set in the Course
12659: # Editor or Resource Editor).
12660: #
12661: # If a valid symb has been included as the third arg in the call
12662: # to &metadata() that can be used to retrieve the value of
12663: # parameter_0_gradable set for the resource, and included in the
12664: # uploaded map containing the tool. The value is retrieved via
12665: # &EXT(), if a valid symb is available.  Otherwise the value of
12666: # gradable in the exttool_$marker.db file for the tool instance
12667: # is retrieved via &get().
12668: #
12669: # When lonuserstate::traceroute() calls lonnet::EXT() for 
12670: # hiddenresource and encrypturl (during course initialization)
12671: # the map-level parameter for resource.0.gradable included in the 
12672: # uploaded map containing the tool will not yet have been stored
12673: # in the user_course_parms.db file for the user's session, so in 
12674: # this case fall back to retrieving gradable status from the
12675: # exttool_$marker.db file.
12676: #
12677: # In order to avoid an infinite loop, &metadata() will return
12678: # before a call to &EXT(), if the uri is for an external tool
12679: # and the $what for which metadata is being requested is
12680: # parameter_0_gradable or 0_gradable.
12681: #
12682: 
12683:     if ($uri =~ /ext\.tool$/) {
12684:         if (($what eq 'parameter_0_gradable') || ($what eq '0_gradable')) {
12685:             return;
12686:         } else {
12687:             my ($checked,$use_passback);
12688:             if ($toolsymb ne '') {
12689:                 (undef,undef,my $tooluri) = &decode_symb($toolsymb);
12690:                 if (($tooluri eq $uri) && (&EXT('resource.0.gradable',$toolsymb))) {
12691:                     $checked = 1;
12692:                     if (&EXT('resource.0.gradable',$toolsymb) =~ /^yes$/i) {
12693:                         $use_passback = 1;
12694:                     }
12695:                 }
12696:             }
12697:             unless ($checked) {
12698:                 my ($ignore,$cdom,$cnum,$marker) = split(m{/},$uri);
12699:                 $marker=~s/\D//g;
12700:                 if ($marker) {
12701:                     my %toolsettings=&get('exttool_'.$marker,['gradable'],$cdom,$cnum);
12702:                     $use_passback = $toolsettings{'gradable'};
12703:                 }
12704:             }
12705:             if ($use_passback) {
12706:                 $filename = '/home/httpd/html/res/lib/templates/LTIpassback.tool';
12707:             } else {
12708:                 $filename = '/home/httpd/html/res/lib/templates/LTIstandard.tool';
12709:             }
12710:         }
12711:     }
12712: 
12713:     {
12714: # Imported parts would go here
12715:         my @origfiletagids=();
12716:         my $importedparts=0;
12717: 
12718: # Imported responseids would go here
12719:         my $importedresponses=0;
12720: #
12721: # Is this a recursive call for a library?
12722: #
12723: #	if (! exists($metacache{$uri})) {
12724: #	    $metacache{$uri}={};
12725: #	}
12726: 	my $cachetime = 60*60;
12727:         if ($liburi) {
12728: 	    $liburi=&declutter($liburi);
12729:             $filename=$liburi;
12730:         } else {
12731: 	    &devalidate_cache_new('meta',$uri);
12732: 	    undef(%metaentry);
12733: 	}
12734:         my %metathesekeys=();
12735:         unless ($filename=~/\.meta$/) { $filename.='.meta'; }
12736: 	my $metastring;
12737: 	if ($uri =~ /^priv/ || $uri=~/home\/httpd\/html\/priv/) {
12738: 	    my $which = &hreflocation('','/'.($liburi || $uri));
12739: 	    $metastring = 
12740: 		&Apache::lonnet::ssi_body($which,
12741: 					  ('grade_target' => 'meta'));
12742: 	    $cachetime = 1; # only want this cached in the child not long term
12743: 	} elsif (($uri !~ m -^(editupload)/-) && 
12744:                  ($uri !~ m{^/*uploaded/$match_domain/$match_courseid/docs/})) {
12745: 	    my $file=&filelocation('',&clutter($filename));
12746: 	    #push(@{$metaentry{$uri.'.file'}},$file);
12747: 	    $metastring=&getfile($file);
12748: 	}
12749:         my $parser=HTML::LCParser->new(\$metastring);
12750:         my $token;
12751:         undef %metathesekeys;
12752:         while ($token=$parser->get_token) {
12753: 	    if ($token->[0] eq 'S') {
12754: 		if (defined($token->[2]->{'package'})) {
12755: #
12756: # This is a package - get package info
12757: #
12758: 		    my $package=$token->[2]->{'package'};
12759: 		    my $keyroot=&add_prefix_and_part($prefix,$token->[2]->{'part'});
12760: 		    if (defined($token->[2]->{'id'})) { 
12761: 			$keyroot.='_'.$token->[2]->{'id'}; 
12762: 		    }
12763: 		    if ($metaentry{':packages'}) {
12764: 			$metaentry{':packages'}.=','.$package.$keyroot;
12765: 		    } else {
12766: 			$metaentry{':packages'}=$package.$keyroot;
12767: 		    }
12768: 		    foreach my $pack_entry (keys(%packagetab)) {
12769: 			my $part=$keyroot;
12770: 			$part=~s/^\_//;
12771: 			if ($pack_entry=~/^\Q$package\E\&/ || 
12772: 			    $pack_entry=~/^\Q$package\E_0\&/) {
12773: 			    my ($pack,$name,$subp)=split(/\&/,$pack_entry);
12774: 			    # ignore package.tab specified default values
12775:                             # here &package_tab_default() will fetch those
12776: 			    if ($subp eq 'default') { next; }
12777: 			    my $value=$packagetab{$pack_entry};
12778: 			    my $unikey;
12779: 			    if ($pack =~ /_0$/) {
12780: 				$unikey='parameter_0_'.$name;
12781: 				$part=0;
12782: 			    } else {
12783: 				$unikey='parameter'.$keyroot.'_'.$name;
12784: 			    }
12785: 			    if ($subp eq 'display') {
12786: 				$value.=' [Part: '.$part.']';
12787: 			    }
12788: 			    $metaentry{':'.$unikey.'.part'}=$part;
12789: 			    $metathesekeys{$unikey}=1;
12790: 			    unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
12791: 				$metaentry{':'.$unikey.'.'.$subp}=$value;
12792: 			    }
12793: 			    if (defined($metaentry{':'.$unikey.'.default'})) {
12794: 				$metaentry{':'.$unikey}=
12795: 				    $metaentry{':'.$unikey.'.default'};
12796: 			    }
12797: 			}
12798: 		    }
12799: 		} else {
12800: #
12801: # This is not a package - some other kind of start tag
12802: #
12803: 		    my $entry=$token->[1];
12804: 		    my $unikey='';
12805: 
12806: 		    if ($entry eq 'import') {
12807: #
12808: # Importing a library here
12809: #
12810:                         my $location=$parser->get_text('/import');
12811:                         my $dir=$filename;
12812:                         $dir=~s|[^/]*$||;
12813:                         $location=&filelocation($dir,$location);
12814: 
12815:                         my $importid=$token->[2]->{'id'};
12816:                         my $importmode=$token->[2]->{'importmode'};
12817: #
12818: # Check metadata for imported file to
12819: # see if it contained response items
12820: #
12821:                         my ($origfile,@libfilekeys);
12822:                         my %currmetaentry = %metaentry;
12823:                         @libfilekeys = split(/,/,&metadata($location,'keys',undef,undef,undef,
12824:                                                            $depthcount+1));
12825:                         if (grep(/^responseorder$/,@libfilekeys)) {
12826:                             my $libresponseorder = &metadata($location,'responseorder',undef,undef,
12827:                                                              undef,$depthcount+1);
12828:                             if ($libresponseorder ne '') {
12829:                                 if ($#origfiletagids<0) {
12830:                                     undef(%importedrespids);
12831:                                     undef(%importedpartids);
12832:                                 }
12833:                                 my @respids = split(/\s*,\s*/,$libresponseorder);
12834:                                 if (@respids) {
12835:                                     $importedrespids{$importid} = join(',',map { $importid.'_'.$_ } @respids);
12836:                                 }
12837:                                 if ($importedrespids{$importid} ne '') {
12838:                                     $importedresponses = 1;
12839: # We need to get the original file and the imported file to get the response order correct
12840: # Load and inspect original file
12841:                                     if ($#origfiletagids<0) {
12842:                                         my $origfilelocation=$perlvar{'lonDocRoot'}.&clutter($uri);
12843:                                         $origfile=&getfile($origfilelocation);
12844:                                         @origfiletagids=($origfile=~/<((?:\w+)response|import|part)[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
12845:                                     }
12846:                                 }
12847:                             }
12848:                         }
12849: # Do not overwrite contents of %metaentry hash for resource itself with 
12850: # hash populated for imported library file
12851:                         %metaentry = %currmetaentry;
12852:                         undef(%currmetaentry);
12853:                         if ($importmode eq 'part') {
12854: # Import as part(s)
12855:                            $importedparts=1;
12856: # We need to get the original file and the imported file to get the part order correct
12857: # Good news: we do not need to worry about nested libraries, since parts cannot be nested
12858: # Load and inspect original file if we didn't do that already
12859:                            if ($#origfiletagids<0) {
12860:                                undef(%importedrespids);
12861:                                undef(%importedpartids);
12862:                                if ($origfile eq '') {
12863:                                    my $origfilelocation=$perlvar{'lonDocRoot'}.&clutter($uri);
12864:                                    $origfile=&getfile($origfilelocation);
12865:                                    @origfiletagids=($origfile=~/<(part|import)[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
12866:                                }
12867:                            }
12868:                            my @impfilepartids;
12869: # If <partorder> tag is included in metadata for the imported file
12870: # get the parts in the imported file from that.
12871:                            if (grep(/^partorder$/,@libfilekeys)) {
12872:                                %currmetaentry = %metaentry;
12873:                                my $libpartorder = &metadata($location,'partorder',undef,undef,undef,
12874:                                                             $depthcount+1);
12875:                                %metaentry = %currmetaentry;
12876:                                undef(%currmetaentry);
12877:                                if ($libpartorder ne '') {
12878:                                    @impfilepartids=split(/\s*,\s*/,$libpartorder);
12879:                                }
12880:                            } else {
12881: # If no <partorder> tag available, load and inspect imported file
12882:                                my $impfile=&getfile($location);
12883:                                @impfilepartids=($impfile=~/<part[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
12884:                            }
12885:                            if ($#impfilepartids>=0) {
12886: # This problem had parts
12887:                                $importedpartids{$token->[2]->{'id'}}=join(',',@impfilepartids);
12888:                            } else {
12889: # Importing by turning a single problem into a problem part
12890: # It gets the import-tags ID as part-ID
12891:                                $unikey=&add_prefix_and_part($prefix,$token->[2]->{'id'});
12892:                                $importedpartids{$token->[2]->{'id'}}=$token->[2]->{'id'};
12893:                            }
12894:                         } else {
12895: # Import as problem or as normal import
12896:                             $unikey=&add_prefix_and_part($prefix,$token->[2]->{'part'});
12897:                             unless ($importmode eq 'problem') {
12898: # Normal import
12899:                                 if (defined($token->[2]->{'id'})) {
12900:                                     $unikey.='_'.$token->[2]->{'id'};
12901:                                 }
12902:                             }
12903: # Check metadata for imported file to
12904: # see if it contained parts
12905:                             if (grep(/^partorder$/,@libfilekeys)) {
12906:                                 %currmetaentry = %metaentry;
12907:                                 my $libpartorder = &metadata($location,'partorder',undef,undef,undef,
12908:                                                              $depthcount+1);
12909:                                 %metaentry = %currmetaentry;
12910:                                 undef(%currmetaentry);
12911:                                 if ($libpartorder ne '') {
12912:                                     $importedparts = 1;
12913:                                     $importedpartids{$token->[2]->{'id'}}=$libpartorder;
12914:                                 }
12915:                             }
12916:                         }
12917: 			if ($depthcount<20) {
12918: 			    my $metadata = 
12919: 				&metadata($uri,'keys',$toolsymb,$location,$unikey,
12920: 					  $depthcount+1);
12921: 			    foreach my $meta (split(',',$metadata)) {
12922: 				$metaentry{':'.$meta}=$metaentry{':'.$meta};
12923: 				$metathesekeys{$meta}=1;
12924: 			    }
12925:                         }
12926: 		    } else {
12927: #
12928: # Not importing, some other kind of non-package, non-library start tag
12929: # 
12930:                         $unikey=$entry.&add_prefix_and_part($prefix,$token->[2]->{'part'});
12931:                         if (defined($token->[2]->{'id'})) {
12932:                             $unikey.='_'.$token->[2]->{'id'};
12933:                         }
12934: 			if (defined($token->[2]->{'name'})) { 
12935: 			    $unikey.='_'.$token->[2]->{'name'}; 
12936: 			}
12937: 			$metathesekeys{$unikey}=1;
12938: 			foreach my $param (@{$token->[3]}) {
12939: 			    $metaentry{':'.$unikey.'.'.$param} =
12940: 				$token->[2]->{$param};
12941: 			}
12942: 			my $internaltext=&HTML::Entities::decode($parser->get_text('/'.$entry));
12943: 			my $default=$metaentry{':'.$unikey.'.default'};
12944: 			if ( $internaltext =~ /^\s*$/ && $default !~ /^\s*$/) {
12945: 		 # only ws inside the tag, and not in default, so use default
12946: 		 # as value
12947: 			    $metaentry{':'.$unikey}=$default;
12948: 			} elsif ( $internaltext =~ /\S/ ) {
12949: 		  # something interesting inside the tag
12950: 			    $metaentry{':'.$unikey}=$internaltext;
12951: 			} else {
12952: 		  # no interesting values, don't set a default
12953: 			}
12954: # end of not-a-package not-a-library import
12955: 		    }
12956: # end of not-a-package start tag
12957: 		}
12958: # the next is the end of "start tag"
12959: 	    }
12960: 	}
12961: 	my ($extension) = ($uri =~ /\.(\w+)$/);
12962: 	$extension = lc($extension);
12963: 	if ($extension eq 'htm') { $extension='html'; }
12964: 
12965: 	foreach my $key (keys(%packagetab)) {
12966: 	    #no specific packages #how's our extension
12967: 	    if ($key!~/^extension_\Q$extension\E&/) { next; }
12968: 	    &metadata_create_package_def($uri,$key,'extension_'.$extension,
12969: 					 \%metathesekeys);
12970: 	}
12971: 
12972: 	if (!exists($metaentry{':packages'})
12973: 	    || $packagetab{"import_defaults&extension_$extension"}) {
12974: 	    foreach my $key (keys(%packagetab)) {
12975: 		#no specific packages well let's get default then
12976: 		if ($key!~/^default&/) { next; }
12977: 		&metadata_create_package_def($uri,$key,'default',
12978: 					     \%metathesekeys);
12979: 	    }
12980: 	}
12981: # are there custom rights to evaluate
12982: 	if ($metaentry{':copyright'} eq 'custom') {
12983: 
12984:     #
12985:     # Importing a rights file here
12986:     #
12987: 	    unless ($depthcount) {
12988: 		my $location=$metaentry{':customdistributionfile'};
12989: 		my $dir=$filename;
12990: 		$dir=~s|[^/]*$||;
12991: 		$location=&filelocation($dir,$location);
12992: 		my $rights_metadata =
12993: 		    &metadata($uri,'keys',$toolsymb,$location,'_rights',
12994: 			      $depthcount+1);
12995: 		foreach my $rights (split(',',$rights_metadata)) {
12996: 		    #$metaentry{':'.$rights}=$metacache{$uri}->{':'.$rights};
12997: 		    $metathesekeys{$rights}=1;
12998: 		}
12999: 	    }
13000: 	}
13001: 	# uniqifiy package listing
13002: 	my %seen;
13003: 	my @uniq_packages =
13004: 	    grep { ! $seen{$_} ++ } (split(',',$metaentry{':packages'}));
13005: 	$metaentry{':packages'} = join(',',@uniq_packages);
13006: 
13007:         if (($importedresponses) || ($importedparts)) {
13008:             if ($importedparts) {
13009: # We had imported parts and need to rebuild partorder
13010:                 $metaentry{':partorder'}='';
13011:                 $metathesekeys{'partorder'}=1;
13012:             }
13013:             if ($importedresponses) {
13014: # We had imported responses and need to rebuil responseorder
13015:                 $metaentry{':responseorder'}='';
13016:                 $metathesekeys{'responseorder'}=1;
13017:             }
13018:             for (my $index=0;$index<$#origfiletagids;$index+=2) {
13019:                 my $origid = $origfiletagids[$index+1];
13020:                 if ($origfiletagids[$index] eq 'part') {
13021: # Original part, part of the problem
13022:                     if ($importedparts) {
13023:                         $metaentry{':partorder'}.=','.$origid;
13024:                     }
13025:                 } elsif ($origfiletagids[$index] eq 'import') {
13026:                     if ($importedparts) {
13027: # We have imported parts at this position
13028:                         if ($importedpartids{$origid} ne '') {
13029:                             $metaentry{':partorder'}.=','.$importedpartids{$origid};
13030:                         }
13031:                     }
13032:                     if ($importedresponses) {
13033: # We have imported responses at this position
13034:                         if ($importedrespids{$origid} ne '') {
13035:                             $metaentry{':responseorder'}.=','.$importedrespids{$origid};
13036:                         }
13037:                     }
13038:                 } else {
13039: # Original response item, part of the problem
13040:                     if ($importedresponses) {
13041:                         $metaentry{':responseorder'}.=','.$origid;
13042:                     }
13043:                 }
13044:             }
13045:             if ($importedparts) {
13046:                 $metaentry{':partorder'}=~s/^\,//;
13047:             }
13048:             if ($importedresponses) {
13049:                 $metaentry{':responseorder'}=~s/^\,//;
13050:             }
13051:         }
13052: 	$metaentry{':keys'} = join(',',keys(%metathesekeys));
13053: 	&metadata_generate_part0(\%metathesekeys,\%metaentry,$uri);
13054: 	$metaentry{':allpossiblekeys'}=join(',',keys(%metathesekeys));
13055:         unless ($liburi) {
13056: 	    &do_cache_new('meta',$uri,\%metaentry,$cachetime);
13057:         }
13058: # this is the end of "was not already recently cached
13059:     }
13060:     return $metaentry{':'.$what};
13061: }
13062: 
13063: sub metadata_create_package_def {
13064:     my ($uri,$key,$package,$metathesekeys)=@_;
13065:     my ($pack,$name,$subp)=split(/\&/,$key);
13066:     if ($subp eq 'default') { next; }
13067:     
13068:     if (defined($metaentry{':packages'})) {
13069: 	$metaentry{':packages'}.=','.$package;
13070:     } else {
13071: 	$metaentry{':packages'}=$package;
13072:     }
13073:     my $value=$packagetab{$key};
13074:     my $unikey;
13075:     $unikey='parameter_0_'.$name;
13076:     $metaentry{':'.$unikey.'.part'}=0;
13077:     $$metathesekeys{$unikey}=1;
13078:     unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
13079: 	$metaentry{':'.$unikey.'.'.$subp}=$value;
13080:     }
13081:     if (defined($metaentry{':'.$unikey.'.default'})) {
13082: 	$metaentry{':'.$unikey}=
13083: 	    $metaentry{':'.$unikey.'.default'};
13084:     }
13085: }
13086: 
13087: sub metadata_generate_part0 {
13088:     my ($metadata,$metacache,$uri) = @_;
13089:     my %allnames;
13090:     foreach my $metakey (keys(%$metadata)) {
13091: 	if ($metakey=~/^parameter\_(.*)/) {
13092: 	  my $part=$$metacache{':'.$metakey.'.part'};
13093: 	  my $name=$$metacache{':'.$metakey.'.name'};
13094: 	  if (! exists($$metadata{'parameter_0_'.$name.'.name'})) {
13095: 	    $allnames{$name}=$part;
13096: 	  }
13097: 	}
13098:     }
13099:     foreach my $name (keys(%allnames)) {
13100:       $$metadata{"parameter_0_$name"}=1;
13101:       my $key=":parameter_0_$name";
13102:       $$metacache{"$key.part"}='0';
13103:       $$metacache{"$key.name"}=$name;
13104:       $$metacache{"$key.type"}=$$metacache{':parameter_'.
13105: 					   $allnames{$name}.'_'.$name.
13106: 					   '.type'};
13107:       my $olddis=$$metacache{':parameter_'.$allnames{$name}.'_'.$name.
13108: 			     '.display'};
13109:       my $expr='[Part: '.$allnames{$name}.']';
13110:       $olddis=~s/\Q$expr\E/\[Part: 0\]/;
13111:       $$metacache{"$key.display"}=$olddis;
13112:     }
13113: }
13114: 
13115: # ------------------------------------------------------ Devalidate title cache
13116: 
13117: sub devalidate_title_cache {
13118:     my ($url)=@_;
13119:     if (!$env{'request.course.id'}) { return; }
13120:     my $symb=&symbread($url);
13121:     if (!$symb) { return; }
13122:     my $key=$env{'request.course.id'}."\0".$symb;
13123:     &devalidate_cache_new('title',$key);
13124: }
13125: 
13126: # ------------------------------------------------- Get the title of a course
13127: 
13128: sub current_course_title {
13129:     return $env{ 'course.' . $env{'request.course.id'} . '.description' };
13130: }
13131: # ------------------------------------------------- Get the title of a resource
13132: 
13133: sub gettitle {
13134:     my $urlsymb=shift;
13135:     my $symb=&symbread($urlsymb);
13136:     if ($symb) {
13137: 	my $key=$env{'request.course.id'}."\0".$symb;
13138: 	my ($result,$cached)=&is_cached_new('title',$key);
13139: 	if (defined($cached)) { 
13140: 	    return $result;
13141: 	}
13142: 	my ($map,$resid,$url)=&decode_symb($symb);
13143: 	my $title='';
13144: 	if (!$map && $resid == 0 && $url =~/default\.sequence$/) {
13145: 	    $title = $env{'course.'.$env{'request.course.id'}.'.description'};
13146: 	} else {
13147: 	    if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
13148: 		    &GDBM_READER(),0640)) {
13149: 		my $mapid=$bighash{'map_pc_'.&clutter($map)};
13150: 		$title=$bighash{'title_'.$mapid.'.'.$resid};
13151: 		untie(%bighash);
13152: 	    }
13153: 	}
13154: 	$title=~s/\&colon\;/\:/gs;
13155: 	if ($title) {
13156: # Remember both $symb and $title for dynamic metadata
13157:             $accesshash{$symb.'___crstitle'}=$title;
13158:             $accesshash{&declutter($map).'___'.&declutter($url).'___usage'}=time;
13159: # Cache this title and then return it
13160: 	    return &do_cache_new('title',$key,$title,600);
13161: 	}
13162: 	$urlsymb=$url;
13163:     }
13164:     my $title=&metadata($urlsymb,'title');
13165:     if (!$title) { $title=(split('/',$urlsymb))[-1]; }    
13166:     return $title;
13167: }
13168: 
13169: sub get_slot {
13170:     my ($which,$cnum,$cdom)=@_;
13171:     if (!$cnum || !$cdom) {
13172: 	(undef,my $courseid)=&whichuser();
13173: 	$cdom=$env{'course.'.$courseid.'.domain'};
13174: 	$cnum=$env{'course.'.$courseid.'.num'};
13175:     }
13176:     my $key=join("\0",'slots',$cdom,$cnum,$which);
13177:     my %slotinfo;
13178:     if (exists($remembered{$key})) {
13179: 	$slotinfo{$which} = $remembered{$key};
13180:     } else {
13181: 	%slotinfo=&get('slots',[$which],$cdom,$cnum);
13182: 	&Apache::lonhomework::showhash(%slotinfo);
13183: 	my ($tmp)=keys(%slotinfo);
13184: 	if ($tmp=~/^error:/) { return (); }
13185: 	$remembered{$key} = $slotinfo{$which};
13186:     }
13187:     if (ref($slotinfo{$which}) eq 'HASH') {
13188: 	return %{$slotinfo{$which}};
13189:     }
13190:     return $slotinfo{$which};
13191: }
13192: 
13193: sub get_reservable_slots {
13194:     my ($cnum,$cdom,$uname,$udom) = @_;
13195:     my $now = time;
13196:     my $reservable_info;
13197:     my $key=join("\0",'reservableslots',$cdom,$cnum,$uname,$udom);
13198:     if (exists($remembered{$key})) {
13199:         $reservable_info = $remembered{$key};
13200:     } else {
13201:         my %resv;
13202:         ($resv{'now_order'},$resv{'now'},$resv{'future_order'},$resv{'future'}) =
13203:         &Apache::loncommon::get_future_slots($cnum,$cdom,$now);
13204:         $reservable_info = \%resv;
13205:         $remembered{$key} = $reservable_info;
13206:     }
13207:     return $reservable_info;
13208: }
13209: 
13210: sub get_course_slots {
13211:     my ($cnum,$cdom) = @_;
13212:     my $hashid=$cnum.':'.$cdom;
13213:     my ($result,$cached) = &Apache::lonnet::is_cached_new('allslots',$hashid);
13214:     if (defined($cached)) {
13215:         if (ref($result) eq 'HASH') {
13216:             return %{$result};
13217:         }
13218:     } else {
13219:         my %slots=&Apache::lonnet::dump('slots',$cdom,$cnum);
13220:         my ($tmp) = keys(%slots);
13221:         if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
13222:             &do_cache_new('allslots',$hashid,\%slots,600);
13223:             return %slots;
13224:         }
13225:     }
13226:     return;
13227: }
13228: 
13229: sub devalidate_slots_cache {
13230:     my ($cnum,$cdom)=@_;
13231:     my $hashid=$cnum.':'.$cdom;
13232:     &devalidate_cache_new('allslots',$hashid);
13233: }
13234: 
13235: sub get_coursechange {
13236:     my ($cdom,$cnum) = @_;
13237:     if ($cdom eq '' || $cnum eq '') {
13238:         return unless ($env{'request.course.id'});
13239:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
13240:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
13241:     }
13242:     my $hashid=$cdom.'_'.$cnum;
13243:     my ($change,$cached)=&is_cached_new('crschange',$hashid);
13244:     if ((defined($cached)) && ($change ne '')) {
13245:         return $change;
13246:     } else {
13247:         my %crshash;
13248:         %crshash = &get('environment',['internal.contentchange'],$cdom,$cnum);
13249:         if ($crshash{'internal.contentchange'} eq '') {
13250:             $change = $env{'course.'.$cdom.'_'.$cnum.'.internal.created'};
13251:             if ($change eq '') {
13252:                 %crshash = &get('environment',['internal.created'],$cdom,$cnum);
13253:                 $change = $crshash{'internal.created'};
13254:             }
13255:         } else {
13256:             $change = $crshash{'internal.contentchange'};
13257:         }
13258:         my $cachetime = 600;
13259:         &do_cache_new('crschange',$hashid,$change,$cachetime);
13260:     }
13261:     return $change;
13262: }
13263: 
13264: sub devalidate_coursechange_cache {
13265:     my ($cnum,$cdom)=@_;
13266:     my $hashid=$cnum.':'.$cdom;
13267:     &devalidate_cache_new('crschange',$hashid);
13268: }
13269: 
13270: # ------------------------------------------------- Update symbolic store links
13271: 
13272: sub symblist {
13273:     my ($mapname,%newhash)=@_;
13274:     $mapname=&deversion(&declutter($mapname));
13275:     my %hash;
13276:     if (($env{'request.course.fn'}) && (%newhash)) {
13277:         if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
13278:                       &GDBM_WRCREAT(),0640)) {
13279: 	    foreach my $url (keys(%newhash)) {
13280: 		next if ($url eq 'last_known'
13281: 			 && $env{'form.no_update_last_known'});
13282: 		$hash{declutter($url)}=&encode_symb($mapname,
13283: 						    $newhash{$url}->[1],
13284: 						    $newhash{$url}->[0]);
13285:             }
13286:             if (untie(%hash)) {
13287: 		return 'ok';
13288:             }
13289:         }
13290:     }
13291:     return 'error';
13292: }
13293: 
13294: # --------------------------------------------------------------- Verify a symb
13295: 
13296: sub symbverify {
13297:     my ($symb,$thisurl,$encstate)=@_;
13298:     my $thisfn=$thisurl;
13299:     $thisfn=&declutter($thisfn);
13300: # direct jump to resource in page or to a sequence - will construct own symbs
13301:     if ($thisfn=~/\.(page|sequence)$/) { return 1; }
13302: # check URL part
13303:     my ($map,$resid,$url)=&decode_symb($symb);
13304: 
13305:     unless ($url eq $thisfn) { return 0; }
13306: 
13307:     $symb=&symbclean($symb);
13308:     $thisurl=&deversion($thisurl);
13309:     $thisfn=&deversion($thisfn);
13310: 
13311:     my %bighash;
13312:     my $okay=0;
13313: 
13314:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
13315:                             &GDBM_READER(),0640)) {
13316:         if (($thisurl =~ m{^/adm/wrapper/ext/}) || ($thisurl =~ m{^ext/})) {
13317:             $thisurl =~ s/\?.+$//;
13318:             if ($map =~ m{^uploaded/.+\.page$}) {
13319:                 $thisurl =~ s{^(/adm/wrapper|)/ext/}{http://};
13320:                 $thisurl =~ s{^\Qhttp://https://\E}{https://};
13321:             }
13322:         }
13323:         my $ids;
13324:         if ($map =~ m{^uploaded/.+\.page$}) {
13325:             $ids=$bighash{'ids_'.&clutter_with_no_wrapper($thisurl)};
13326:         } else {
13327:             $ids=$bighash{'ids_'.&clutter($thisurl)};
13328:         }
13329:         unless ($ids) {
13330:             my $idkey = 'ids_'.($thisurl =~ m{^/}? '' : '/').$thisurl;  
13331:             $ids=$bighash{$idkey};
13332:         }
13333:         if ($ids) {
13334: # ------------------------------------------------------------------- Has ID(s)
13335:             if ($thisfn =~ m{^/adm/wrapper/ext/}) {
13336:                 $symb =~ s/\?.+$//;
13337:             }
13338: 	    foreach my $id (split(/\,/,$ids)) {
13339: 	       my ($mapid,$resid)=split(/\./,$id);
13340:                if (
13341:   &symbclean(&declutter($bighash{'map_id_'.$mapid}).'___'.$resid.'___'.$thisfn)
13342:    eq $symb) {
13343:                    if (ref($encstate)) {
13344:                        $$encstate = $bighash{'encrypted_'.$id};
13345:                    }
13346: 		   if (($env{'request.role.adv'}) ||
13347: 		       ($bighash{'encrypted_'.$id} eq $env{'request.enc'}) ||
13348:                        ($thisurl eq '/adm/navmaps')) {
13349: 		       $okay=1;
13350:                        last;
13351: 		   }
13352: 	       }
13353: 	   }
13354:         }
13355: 	untie(%bighash);
13356:     }
13357:     return $okay;
13358: }
13359: 
13360: # --------------------------------------------------------------- Clean-up symb
13361: 
13362: sub symbclean {
13363:     my $symb=shift;
13364:     if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
13365: # remove version from map
13366:     $symb=~s/\.(\d+)\.(\w+)\_\_\_/\.$2\_\_\_/;
13367: 
13368: # remove version from URL
13369:     $symb=~s/\.(\d+)\.(\w+)$/\.$2/;
13370: 
13371: # remove wrapper
13372: 
13373:     $symb=~s/(\_\_\_\d+\_\_\_)adm\/wrapper\/(res\/)*/$1/;
13374:     $symb=~s/(\_\_\_\d+\_\_\_)adm\/coursedocs\/showdoc\/(res\/)*/$1/;
13375:     return $symb;
13376: }
13377: 
13378: # ---------------------------------------------- Split symb to find map and url
13379: 
13380: sub encode_symb {
13381:     my ($map,$resid,$url)=@_;
13382:     return &symbclean(&declutter($map).'___'.$resid.'___'.&declutter($url));
13383: }
13384: 
13385: sub decode_symb {
13386:     my $symb=shift;
13387:     if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
13388:     my ($map,$resid,$url)=split(/___/,$symb);
13389:     return (&fixversion($map),$resid,&fixversion($url));
13390: }
13391: 
13392: sub fixversion {
13393:     my $fn=shift;
13394:     if ($fn=~/^(adm|uploaded|editupload|public)/) { return $fn; }
13395:     my %bighash;
13396:     my $uri=&clutter($fn);
13397:     my $key=$env{'request.course.id'}.'_'.$uri;
13398: # is this cached?
13399:     my ($result,$cached)=&is_cached_new('courseresversion',$key);
13400:     if (defined($cached)) { return $result; }
13401: # unfortunately not cached, or expired
13402:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
13403: 	    &GDBM_READER(),0640)) {
13404:  	if ($bighash{'version_'.$uri}) {
13405:  	    my $version=$bighash{'version_'.$uri};
13406:  	    unless (($version eq 'mostrecent') || 
13407: 		    ($version==&getversion($uri))) {
13408:  		$uri=~s/\.(\w+)$/\.$version\.$1/;
13409:  	    }
13410:  	}
13411:  	untie %bighash;
13412:     }
13413:     return &do_cache_new('courseresversion',$key,&declutter($uri),600);
13414: }
13415: 
13416: sub deversion {
13417:     my $url=shift;
13418:     $url=~s/\.\d+\.(\w+)$/\.$1/;
13419:     return $url;
13420: }
13421: 
13422: # ------------------------------------------------------ Return symb list entry
13423: 
13424: sub symbread {
13425:     my ($thisfn,$donotrecurse,$ignorecachednull,$checkforblock,$possibles,
13426:         $ignoresymbdb,$noenccheck)=@_;
13427:     my $cache_str='request.symbread.cached.'.$thisfn;
13428:     if (defined($env{$cache_str})) {
13429:         unless (ref($possibles) eq 'HASH') {
13430:             if ($ignorecachednull) {
13431:                 return $env{$cache_str} unless ($env{$cache_str} eq '');
13432:             } else {
13433:                 return $env{$cache_str};
13434:             }
13435:         }
13436:     }
13437: # no filename provided? try from environment
13438:     unless ($thisfn) {
13439:         if ($env{'request.symb'}) {
13440:             return $env{$cache_str}=&symbclean($env{'request.symb'});
13441: 	}
13442: 	$thisfn=$env{'request.filename'};
13443:     }
13444:     if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
13445: # is that filename actually a symb? Verify, clean, and return
13446:     if ($thisfn=~/\_\_\_\d+\_\_\_(.*)$/) {
13447: 	if (&symbverify($thisfn,$1)) {
13448: 	    return $env{$cache_str}=&symbclean($thisfn);
13449: 	}
13450:     }
13451:     $thisfn=declutter($thisfn);
13452:     my %hash;
13453:     my %bighash;
13454:     my $syval='';
13455:     if (($env{'request.course.fn'}) && ($thisfn)) {
13456:         my $targetfn = $thisfn;
13457:         if ( ($thisfn =~ m/^(uploaded|editupload)\//) && ($thisfn !~ m/\.(page|sequence)$/) ) {
13458:             $targetfn = 'adm/wrapper/'.$thisfn;
13459:         }
13460: 	if ($targetfn =~ m|^adm/wrapper/(ext/.*)|) {
13461: 	    $targetfn=$1;
13462: 	}
13463:         unless ($ignoresymbdb) {
13464:             if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
13465:                           &GDBM_READER(),0640)) {
13466: 	        $syval=$hash{$targetfn};
13467:                 untie(%hash);
13468:             }
13469:             if ($syval && $checkforblock) {
13470:                 my @blockers = &has_comm_blocking('bre',$syval,$thisfn,$ignoresymbdb,$noenccheck);
13471:                 if (@blockers) {
13472:                     $syval='';
13473:                 }
13474:             }
13475:         }
13476: # ---------------------------------------------------------- There was an entry
13477:         if ($syval) {
13478: 	    #unless ($syval=~/\_\d+$/) {
13479: 		#unless ($env{'form.request.prefix'}=~/\.(\d+)\_$/) {
13480: 		    #&appenv({'request.ambiguous' => $thisfn});
13481: 		    #return $env{$cache_str}='';
13482: 		#}    
13483: 		#$syval.=$1;
13484: 	    #}
13485:         } else {
13486: # ------------------------------------------------------- Was not in symb table
13487:            if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
13488:                             &GDBM_READER(),0640)) {
13489: # ---------------------------------------------- Get ID(s) for current resource
13490:               my $ids=$bighash{'ids_'.&clutter($thisfn)};
13491:               unless ($ids) { 
13492:                  $ids=$bighash{'ids_/'.$thisfn};
13493:               }
13494:               unless ($ids) {
13495: # alias?
13496: 		  $ids=$bighash{'mapalias_'.$thisfn};
13497:               }
13498:               if ($ids) {
13499: # ------------------------------------------------------------------- Has ID(s)
13500:                  my @possibilities=split(/\,/,$ids);
13501:                  if ($#possibilities==0) {
13502: # ----------------------------------------------- There is only one possibility
13503: 		     my ($mapid,$resid)=split(/\./,$ids);
13504: 		     $syval=&encode_symb($bighash{'map_id_'.$mapid},
13505: 						    $resid,$thisfn);
13506:                      if (ref($possibles) eq 'HASH') {
13507:                          unless ($bighash{'randomout_'.$ids} || $env{'request.role.adv'}) {
13508:                              $possibles->{$syval} = 1;
13509:                          }
13510:                      }
13511:                      if ($checkforblock) {
13512:                          unless ($bighash{'randomout_'.$ids} || $env{'request.role.adv'}) {
13513:                              my @blockers = &has_comm_blocking('bre',$syval,$bighash{'src_'.$ids},'',$noenccheck);
13514:                              if (@blockers) {
13515:                                  $syval = '';
13516:                                  untie(%bighash);
13517:                                  return $env{$cache_str}='';
13518:                              }
13519:                          }
13520:                      }
13521:                  } elsif ((!$donotrecurse) || ($checkforblock) || (ref($possibles) eq 'HASH')) { 
13522: # ------------------------------------------ There is more than one possibility
13523:                      my $realpossible=0;
13524:                      foreach my $id (@possibilities) {
13525: 			 my $file=$bighash{'src_'.$id};
13526:                          my $canaccess;
13527:                          if (($donotrecurse) || ($checkforblock) || (ref($possibles) eq 'HASH')) {
13528:                              $canaccess = 1;
13529:                          } else { 
13530:                              $canaccess = &allowed('bre',$file);
13531:                          }
13532:                          if ($canaccess) {
13533:          		     my ($mapid,$resid)=split(/\./,$id);
13534:                              if ($bighash{'map_type_'.$mapid} ne 'page') {
13535:                                  my $poss_syval=&encode_symb($bighash{'map_id_'.$mapid},
13536: 						             $resid,$thisfn);
13537:                                  next if ($bighash{'randomout_'.$id} && !$env{'request.role.adv'});
13538:                                  next unless (($noenccheck) || ($bighash{'encrypted_'.$id} eq $env{'request.enc'}));
13539:                                  if ($checkforblock) {
13540:                                      my @blockers = &has_comm_blocking('bre',$poss_syval,$file,'',$noenccheck);
13541:                                      if (@blockers > 0) {
13542:                                          $syval = '';
13543:                                      } else {
13544:                                          $syval = $poss_syval;
13545:                                          $realpossible++;
13546:                                      }
13547:                                  } else {
13548:                                      $syval = $poss_syval;
13549:                                      $realpossible++;
13550:                                  }
13551:                                  if ($syval) {
13552:                                      if (ref($possibles) eq 'HASH') {
13553:                                          $possibles->{$syval} = 1;
13554:                                      }
13555:                                  }
13556:                              }
13557: 			 }
13558:                      }
13559: 		     if ($realpossible!=1) { $syval=''; }
13560:                  } else {
13561:                      $syval='';
13562:                  }
13563: 	      }
13564:               untie(%bighash);
13565:            }
13566:         }
13567:         if ($syval) {
13568: 	    return $env{$cache_str}=$syval;
13569:         }
13570:     }
13571:     &appenv({'request.ambiguous' => $thisfn});
13572:     return $env{$cache_str}='';
13573: }
13574: 
13575: # ---------------------------------------------------------- Return random seed
13576: 
13577: sub numval {
13578:     my $txt=shift;
13579:     $txt=~tr/A-J/0-9/;
13580:     $txt=~tr/a-j/0-9/;
13581:     $txt=~tr/K-T/0-9/;
13582:     $txt=~tr/k-t/0-9/;
13583:     $txt=~tr/U-Z/0-5/;
13584:     $txt=~tr/u-z/0-5/;
13585:     $txt=~s/\D//g;
13586:     if ($_64bit) { if ($txt > 2**32) { return -1; } }
13587:     return int($txt);
13588: }
13589: 
13590: sub numval2 {
13591:     my $txt=shift;
13592:     $txt=~tr/A-J/0-9/;
13593:     $txt=~tr/a-j/0-9/;
13594:     $txt=~tr/K-T/0-9/;
13595:     $txt=~tr/k-t/0-9/;
13596:     $txt=~tr/U-Z/0-5/;
13597:     $txt=~tr/u-z/0-5/;
13598:     $txt=~s/\D//g;
13599:     my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
13600:     my $total;
13601:     foreach my $val (@txts) { $total+=$val; }
13602:     if ($_64bit) { if ($total > 2**32) { return -1; } }
13603:     return int($total);
13604: }
13605: 
13606: sub numval3 {
13607:     use integer;
13608:     my $txt=shift;
13609:     $txt=~tr/A-J/0-9/;
13610:     $txt=~tr/a-j/0-9/;
13611:     $txt=~tr/K-T/0-9/;
13612:     $txt=~tr/k-t/0-9/;
13613:     $txt=~tr/U-Z/0-5/;
13614:     $txt=~tr/u-z/0-5/;
13615:     $txt=~s/\D//g;
13616:     my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
13617:     my $total;
13618:     foreach my $val (@txts) { $total+=$val; }
13619:     if ($_64bit) { $total=(($total<<32)>>32); }
13620:     return $total;
13621: }
13622: 
13623: sub digest {
13624:     my ($data)=@_;
13625:     my $digest=&Digest::MD5::md5($data);
13626:     my ($a,$b,$c,$d)=unpack("iiii",$digest);
13627:     my ($e,$f);
13628:     {
13629:         use integer;
13630:         $e=($a+$b);
13631:         $f=($c+$d);
13632:         if ($_64bit) {
13633:             $e=(($e<<32)>>32);
13634:             $f=(($f<<32)>>32);
13635:         }
13636:     }
13637:     if (wantarray) {
13638: 	return ($e,$f);
13639:     } else {
13640: 	my $g;
13641: 	{
13642: 	    use integer;
13643: 	    $g=($e+$f);
13644: 	    if ($_64bit) {
13645: 		$g=(($g<<32)>>32);
13646: 	    }
13647: 	}
13648: 	return $g;
13649:     }
13650: }
13651: 
13652: sub latest_rnd_algorithm_id {
13653:     return '64bit5';
13654: }
13655: 
13656: sub get_rand_alg {
13657:     my ($courseid)=@_;
13658:     if (!$courseid) { $courseid=(&whichuser())[1]; }
13659:     if ($courseid) {
13660: 	return $env{"course.$courseid.rndseed"};
13661:     }
13662:     return &latest_rnd_algorithm_id();
13663: }
13664: 
13665: sub validCODE {
13666:     my ($CODE)=@_;
13667:     if (defined($CODE) && $CODE ne '' && $CODE =~ /^\w+$/) { return 1; }
13668:     return 0;
13669: }
13670: 
13671: sub getCODE {
13672:     if (&validCODE($env{'form.CODE'})) { return $env{'form.CODE'}; }
13673:     if ( (defined($Apache::lonhomework::parsing_a_problem) ||
13674: 	  defined($Apache::lonhomework::parsing_a_task) ) &&
13675: 	 &validCODE($Apache::lonhomework::history{'resource.CODE'})) {
13676: 	return $Apache::lonhomework::history{'resource.CODE'};
13677:     }
13678:     return undef;
13679: }
13680: #
13681: #  Determines the random seed for a specific context:
13682: #
13683: # parameters:
13684: #   symb      - in course context the symb for the seed.
13685: #   course_id - The course id of the form domain_coursenum.
13686: #   domain    - Domain for the user.
13687: #   course    - Course for the user.
13688: #   cenv      - environment of the course.
13689: #
13690: # NOTE:
13691: #   All parameters are picked out of the environment if missing
13692: #   or not defined.
13693: #   If a symb cannot be determined the current time is used instead.
13694: #
13695: #  For a given well defined symb, courside, domain, username,
13696: #  and course environment, the seed is reproducible.
13697: #
13698: sub rndseed {
13699:     my ($symb,$courseid,$domain,$username, $cenv)=@_;
13700:     my ($wsymb,$wcourseid,$wdomain,$wusername)=&whichuser();
13701:     if (!defined($symb)) {
13702: 	unless ($symb=$wsymb) { return time; }
13703:     }
13704:     if (!defined $courseid) { 
13705: 	$courseid=$wcourseid; 
13706:     }
13707:     if (!defined $domain) { $domain=$wdomain; }
13708:     if (!defined $username) { $username=$wusername }
13709: 
13710:     my $which;
13711:     if (defined($cenv->{'rndseed'})) {
13712: 	$which = $cenv->{'rndseed'};
13713:     } else {
13714: 	$which =&get_rand_alg($courseid);
13715:     }
13716:     if (defined(&getCODE())) {
13717: 
13718: 	if ($which eq '64bit5') {
13719: 	    return &rndseed_CODE_64bit5($symb,$courseid,$domain,$username);
13720: 	} elsif ($which eq '64bit4') {
13721: 	    return &rndseed_CODE_64bit4($symb,$courseid,$domain,$username);
13722: 	} else {
13723: 	    return &rndseed_CODE_64bit($symb,$courseid,$domain,$username);
13724: 	}
13725:     } elsif ($which eq '64bit5') {
13726: 	return &rndseed_64bit5($symb,$courseid,$domain,$username);
13727:     } elsif ($which eq '64bit4') {
13728: 	return &rndseed_64bit4($symb,$courseid,$domain,$username);
13729:     } elsif ($which eq '64bit3') {
13730: 	return &rndseed_64bit3($symb,$courseid,$domain,$username);
13731:     } elsif ($which eq '64bit2') {
13732: 	return &rndseed_64bit2($symb,$courseid,$domain,$username);
13733:     } elsif ($which eq '64bit') {
13734: 	return &rndseed_64bit($symb,$courseid,$domain,$username);
13735:     }
13736:     return &rndseed_32bit($symb,$courseid,$domain,$username);
13737: }
13738: 
13739: sub rndseed_32bit {
13740:     my ($symb,$courseid,$domain,$username)=@_;
13741:     {
13742: 	use integer;
13743: 	my $symbchck=unpack("%32C*",$symb) << 27;
13744: 	my $symbseed=numval($symb) << 22;
13745: 	my $namechck=unpack("%32C*",$username) << 17;
13746: 	my $nameseed=numval($username) << 12;
13747: 	my $domainseed=unpack("%32C*",$domain) << 7;
13748: 	my $courseseed=unpack("%32C*",$courseid);
13749: 	my $num=$symbseed+$nameseed+$domainseed+$courseseed+$namechck+$symbchck;
13750: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
13751: 	#&logthis("rndseed :$num:$symb");
13752: 	if ($_64bit) { $num=(($num<<32)>>32); }
13753: 	return $num;
13754:     }
13755: }
13756: 
13757: sub rndseed_64bit {
13758:     my ($symb,$courseid,$domain,$username)=@_;
13759:     {
13760: 	use integer;
13761: 	my $symbchck=unpack("%32S*",$symb) << 21;
13762: 	my $symbseed=numval($symb) << 10;
13763: 	my $namechck=unpack("%32S*",$username);
13764: 	
13765: 	my $nameseed=numval($username) << 21;
13766: 	my $domainseed=unpack("%32S*",$domain) << 10;
13767: 	my $courseseed=unpack("%32S*",$courseid);
13768: 	
13769: 	my $num1=$symbchck+$symbseed+$namechck;
13770: 	my $num2=$nameseed+$domainseed+$courseseed;
13771: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
13772: 	#&logthis("rndseed :$num:$symb");
13773: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
13774: 	return "$num1,$num2";
13775:     }
13776: }
13777: 
13778: sub rndseed_64bit2 {
13779:     my ($symb,$courseid,$domain,$username)=@_;
13780:     {
13781: 	use integer;
13782: 	# strings need to be an even # of cahracters long, it it is odd the
13783:         # last characters gets thrown away
13784: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
13785: 	my $symbseed=numval($symb) << 10;
13786: 	my $namechck=unpack("%32S*",$username.' ');
13787: 	
13788: 	my $nameseed=numval($username) << 21;
13789: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
13790: 	my $courseseed=unpack("%32S*",$courseid.' ');
13791: 	
13792: 	my $num1=$symbchck+$symbseed+$namechck;
13793: 	my $num2=$nameseed+$domainseed+$courseseed;
13794: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
13795: 	#&logthis("rndseed :$num:$symb");
13796: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
13797: 	return "$num1,$num2";
13798:     }
13799: }
13800: 
13801: sub rndseed_64bit3 {
13802:     my ($symb,$courseid,$domain,$username)=@_;
13803:     {
13804: 	use integer;
13805: 	# strings need to be an even # of cahracters long, it it is odd the
13806:         # last characters gets thrown away
13807: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
13808: 	my $symbseed=numval2($symb) << 10;
13809: 	my $namechck=unpack("%32S*",$username.' ');
13810: 	
13811: 	my $nameseed=numval2($username) << 21;
13812: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
13813: 	my $courseseed=unpack("%32S*",$courseid.' ');
13814: 	
13815: 	my $num1=$symbchck+$symbseed+$namechck;
13816: 	my $num2=$nameseed+$domainseed+$courseseed;
13817: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
13818: 	#&logthis("rndseed :$num1:$num2:$_64bit");
13819: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
13820: 	
13821: 	return "$num1:$num2";
13822:     }
13823: }
13824: 
13825: sub rndseed_64bit4 {
13826:     my ($symb,$courseid,$domain,$username)=@_;
13827:     {
13828: 	use integer;
13829: 	# strings need to be an even # of cahracters long, it it is odd the
13830:         # last characters gets thrown away
13831: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
13832: 	my $symbseed=numval3($symb) << 10;
13833: 	my $namechck=unpack("%32S*",$username.' ');
13834: 	
13835: 	my $nameseed=numval3($username) << 21;
13836: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
13837: 	my $courseseed=unpack("%32S*",$courseid.' ');
13838: 	
13839: 	my $num1=$symbchck+$symbseed+$namechck;
13840: 	my $num2=$nameseed+$domainseed+$courseseed;
13841: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
13842: 	#&logthis("rndseed :$num1:$num2:$_64bit");
13843: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
13844: 	
13845: 	return "$num1:$num2";
13846:     }
13847: }
13848: 
13849: sub rndseed_64bit5 {
13850:     my ($symb,$courseid,$domain,$username)=@_;
13851:     my ($num1,$num2)=&digest("$symb,$courseid,$domain,$username");
13852:     return "$num1:$num2";
13853: }
13854: 
13855: sub rndseed_CODE_64bit {
13856:     my ($symb,$courseid,$domain,$username)=@_;
13857:     {
13858: 	use integer;
13859: 	my $symbchck=unpack("%32S*",$symb.' ') << 16;
13860: 	my $symbseed=numval2($symb);
13861: 	my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
13862: 	my $CODEseed=numval(&getCODE());
13863: 	my $courseseed=unpack("%32S*",$courseid.' ');
13864: 	my $num1=$symbseed+$CODEchck;
13865: 	my $num2=$CODEseed+$courseseed+$symbchck;
13866: 	#&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
13867: 	#&logthis("rndseed :$num1:$num2:$symb");
13868: 	if ($_64bit) { $num1=(($num1<<32)>>32); }
13869: 	if ($_64bit) { $num2=(($num2<<32)>>32); }
13870: 	return "$num1:$num2";
13871:     }
13872: }
13873: 
13874: sub rndseed_CODE_64bit4 {
13875:     my ($symb,$courseid,$domain,$username)=@_;
13876:     {
13877: 	use integer;
13878: 	my $symbchck=unpack("%32S*",$symb.' ') << 16;
13879: 	my $symbseed=numval3($symb);
13880: 	my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
13881: 	my $CODEseed=numval3(&getCODE());
13882: 	my $courseseed=unpack("%32S*",$courseid.' ');
13883: 	my $num1=$symbseed+$CODEchck;
13884: 	my $num2=$CODEseed+$courseseed+$symbchck;
13885: 	#&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
13886: 	#&logthis("rndseed :$num1:$num2:$symb");
13887: 	if ($_64bit) { $num1=(($num1<<32)>>32); }
13888: 	if ($_64bit) { $num2=(($num2<<32)>>32); }
13889: 	return "$num1:$num2";
13890:     }
13891: }
13892: 
13893: sub rndseed_CODE_64bit5 {
13894:     my ($symb,$courseid,$domain,$username)=@_;
13895:     my $code = &getCODE();
13896:     my ($num1,$num2)=&digest("$symb,$courseid,$code");
13897:     return "$num1:$num2";
13898: }
13899: 
13900: sub setup_random_from_rndseed {
13901:     my ($rndseed)=@_;
13902:     if ($rndseed =~/([,:])/) {
13903:         my ($num1,$num2) = map { abs($_); } (split(/[,:]/,$rndseed));
13904:         if ((!$num1) || (!$num2) || ($num1 > 2147483562) || ($num2 > 2147483398)) {
13905:             &Math::Random::random_set_seed_from_phrase($rndseed);
13906:         } else {
13907:             &Math::Random::random_set_seed($num1,$num2);
13908:         }
13909:     } else {
13910: 	&Math::Random::random_set_seed_from_phrase($rndseed);
13911:     }
13912: }
13913: 
13914: sub latest_receipt_algorithm_id {
13915:     return 'receipt3';
13916: }
13917: 
13918: sub recunique {
13919:     my $fucourseid=shift;
13920:     my $unique;
13921:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2' ||
13922: 	$env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
13923: 	$unique=$env{"course.$fucourseid.internal.encseed"};
13924:     } else {
13925: 	$unique=$perlvar{'lonReceipt'};
13926:     }
13927:     return unpack("%32C*",$unique);
13928: }
13929: 
13930: sub recprefix {
13931:     my $fucourseid=shift;
13932:     my $prefix;
13933:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2'||
13934: 	$env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
13935: 	$prefix=$env{"course.$fucourseid.internal.encpref"};
13936:     } else {
13937: 	$prefix=$perlvar{'lonHostID'};
13938:     }
13939:     return unpack("%32C*",$prefix);
13940: }
13941: 
13942: sub ireceipt {
13943:     my ($funame,$fudom,$fucourseid,$fusymb,$part)=@_;
13944: 
13945:     my $return =&recprefix($fucourseid).'-';
13946: 
13947:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt3' ||
13948: 	$env{'request.state'} eq 'construct') {
13949: 	$return .= (&digest("$funame,$fudom,$fucourseid,$fusymb,$part")%10000);
13950: 	return $return;
13951:     }
13952: 
13953:     my $cuname=unpack("%32C*",$funame);
13954:     my $cudom=unpack("%32C*",$fudom);
13955:     my $cucourseid=unpack("%32C*",$fucourseid);
13956:     my $cusymb=unpack("%32C*",$fusymb);
13957:     my $cunique=&recunique($fucourseid);
13958:     my $cpart=unpack("%32S*",$part);
13959:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2') {
13960: 
13961: 	#&logthis("doing receipt2  using parts $cpart, uname $cuname and udom $cudom gets  ".($cpart%$cuname)." and ".($cpart%$cudom));
13962: 			       
13963: 	$return.= ($cunique%$cuname+
13964: 		   $cunique%$cudom+
13965: 		   $cusymb%$cuname+
13966: 		   $cusymb%$cudom+
13967: 		   $cucourseid%$cuname+
13968: 		   $cucourseid%$cudom+
13969: 		   $cpart%$cuname+
13970: 		   $cpart%$cudom);
13971:     } else {
13972: 	$return.= ($cunique%$cuname+
13973: 		   $cunique%$cudom+
13974: 		   $cusymb%$cuname+
13975: 		   $cusymb%$cudom+
13976: 		   $cucourseid%$cuname+
13977: 		   $cucourseid%$cudom);
13978:     }
13979:     return $return;
13980: }
13981: 
13982: sub receipt {
13983:     my ($part)=@_;
13984:     my ($symb,$courseid,$domain,$name) = &whichuser();
13985:     return &ireceipt($name,$domain,$courseid,$symb,$part);
13986: }
13987: 
13988: sub whichuser {
13989:     my ($passedsymb)=@_;
13990:     my ($symb,$courseid,$domain,$name,$publicuser);
13991:     if (defined($env{'form.grade_symb'})) {
13992: 	my ($tmp_courseid)=&get_env_multiple('form.grade_courseid');
13993: 	my $allowed=&allowed('vgr',$tmp_courseid);
13994: 	if (!$allowed &&
13995: 	    exists($env{'request.course.sec'}) &&
13996: 	    $env{'request.course.sec'} !~ /^\s*$/) {
13997: 	    $allowed=&allowed('vgr',$tmp_courseid.
13998: 			      '/'.$env{'request.course.sec'});
13999: 	}
14000: 	if ($allowed) {
14001: 	    ($symb)=&get_env_multiple('form.grade_symb');
14002: 	    $courseid=$tmp_courseid;
14003: 	    ($domain)=&get_env_multiple('form.grade_domain');
14004: 	    ($name)=&get_env_multiple('form.grade_username');
14005: 	    return ($symb,$courseid,$domain,$name,$publicuser);
14006: 	}
14007:     }
14008:     if (!$passedsymb) {
14009: 	$symb=&symbread();
14010:     } else {
14011: 	$symb=$passedsymb;
14012:     }
14013:     $courseid=$env{'request.course.id'};
14014:     $domain=$env{'user.domain'};
14015:     $name=$env{'user.name'};
14016:     if ($name eq 'public' && $domain eq 'public') {
14017: 	if (!defined($env{'form.username'})) {
14018: 	    $env{'form.username'}.=time.rand(10000000);
14019: 	}
14020: 	$name.=$env{'form.username'};
14021:     }
14022:     return ($symb,$courseid,$domain,$name,$publicuser);
14023: 
14024: }
14025: 
14026: # ------------------------------------------------------------ Serves up a file
14027: # returns either the contents of the file or 
14028: # -1 if the file doesn't exist
14029: #
14030: # if the target is a file that was uploaded via DOCS, 
14031: # a check will be made to see if a current copy exists on the local server,
14032: # if it does this will be served, otherwise a copy will be retrieved from
14033: # the home server for the course and stored in /home/httpd/html/userfiles on
14034: # the local server.   
14035: 
14036: sub getfile {
14037:     my ($file) = @_;
14038:     if ($file =~ m -^/*(uploaded|editupload)/-) { $file=&filelocation("",$file); }
14039:     &repcopy($file);
14040:     return &readfile($file);
14041: }
14042: 
14043: sub repcopy_userfile {
14044:     my ($file)=@_;
14045:     my $londocroot = $perlvar{'lonDocRoot'};
14046:     if ($file =~ m{^/*(uploaded|editupload)/}) { $file=&filelocation("",$file); }
14047:     if ($file =~ m{^\Q/home/httpd/lonUsers/\E}) { return 'ok'; }
14048:     my ($cdom,$cnum,$filename) = 
14049: 	($file=~m|^\Q$perlvar{'lonDocRoot'}\E/+userfiles/+($match_domain)/+($match_name)/+(.*)|);
14050:     my $uri="/uploaded/$cdom/$cnum/$filename";
14051:     if (-e "$file") {
14052: # we already have a local copy, check it out
14053: 	my @fileinfo = stat($file);
14054: 	my $rtncode;
14055: 	my $info;
14056: 	my $lwpresp = &getuploaded('HEAD',$uri,$cdom,$cnum,\$info,\$rtncode);
14057: 	if ($lwpresp ne 'ok') {
14058: # there is no such file anymore, even though we had a local copy
14059: 	    if ($rtncode eq '404') {
14060: 		unlink($file);
14061: 	    }
14062: 	    return -1;
14063: 	}
14064: 	if ($info < $fileinfo[9]) {
14065: # nice, the file we have is up-to-date, just say okay
14066: 	    return 'ok';
14067: 	} else {
14068: # the file is outdated, get rid of it
14069: 	    unlink($file);
14070: 	}
14071:     }
14072: # one way or the other, at this point, we don't have the file
14073: # construct the correct path for the file
14074:     my @parts = ($cdom,$cnum); 
14075:     if ($filename =~ m|^(.+)/[^/]+$|) {
14076: 	push @parts, split(/\//,$1);
14077:     }
14078:     my $path = $perlvar{'lonDocRoot'}.'/userfiles';
14079:     foreach my $part (@parts) {
14080: 	$path .= '/'.$part;
14081: 	if (!-e $path) {
14082: 	    mkdir($path,0770);
14083: 	}
14084:     }
14085: # now the path exists for sure
14086: # get a user agent
14087:     my $transferfile=$file.'.in.transfer';
14088: # FIXME: this should flock
14089:     if (-e $transferfile) { return 'ok'; }
14090:     my $request;
14091:     $uri=~s/^\///;
14092:     my $homeserver = &homeserver($cnum,$cdom);
14093:     my $hostname = &hostname($homeserver);
14094:     my $protocol = $protocol{$homeserver};
14095:     $protocol = 'http' if ($protocol ne 'https');
14096:     $request=new HTTP::Request('GET',$protocol.'://'.$hostname.'/raw/'.$uri);
14097:     my $response = &LONCAPA::LWPReq::makerequest($homeserver,$request,$transferfile,\%perlvar,'',0,1);
14098: # did it work?
14099:     if ($response->is_error()) {
14100: 	unlink($transferfile);
14101: 	&logthis("Userfile repcopy failed for $uri");
14102: 	return -1;
14103:     }
14104: # worked, rename the transfer file
14105:     rename($transferfile,$file);
14106:     return 'ok';
14107: }
14108: 
14109: sub tokenwrapper {
14110:     my $uri=shift;
14111:     $uri=~s|^https?\://([^/]+)||;
14112:     $uri=~s|^/||;
14113:     $env{'user.environment'}=~/\/([^\/]+)\.id/;
14114:     my $token=$1;
14115:     my (undef,$udom,$uname,$file)=split('/',$uri,4);
14116:     if ($udom && $uname && $file) {
14117: 	$file=~s|(\?\.*)*$||;
14118:         &appenv({"userfile.$udom/$uname/$file" => $env{'request.course.id'}});
14119:         my $homeserver = &homeserver($uname,$udom);
14120:         my $hostname = &hostname($homeserver);
14121:         my $protocol = $protocol{$homeserver};
14122:         $protocol = 'http' if ($protocol ne 'https');
14123:         return $protocol.'://'.$hostname.'/'.$uri.
14124:                (($uri=~/\?/)?'&':'?').'token='.$token.
14125:                                '&tokenissued='.$perlvar{'lonHostID'};
14126:     } else {
14127:         return '/adm/notfound.html';
14128:     }
14129: }
14130: 
14131: # call with reqtype HEAD: get last modification time
14132: # call with reqtype GET: get the file contents
14133: # Do not call this with reqtype GET for large files! It loads everything into memory
14134: #
14135: sub getuploaded {
14136:     my ($reqtype,$uri,$cdom,$cnum,$info,$rtncode) = @_;
14137:     $uri=~s/^\///;
14138:     my $homeserver = &homeserver($cnum,$cdom);
14139:     my $hostname = &hostname($homeserver);
14140:     my $protocol = $protocol{$homeserver};
14141:     $protocol = 'http' if ($protocol ne 'https');
14142:     $uri = $protocol.'://'.$hostname.'/raw/'.$uri;
14143:     my $request=new HTTP::Request($reqtype,$uri);
14144:     my $response=&LONCAPA::LWPReq::makerequest($homeserver,$request,'',\%perlvar,'',0,1);
14145:     $$rtncode = $response->code;
14146:     if (! $response->is_success()) {
14147: 	return 'failed';
14148:     }      
14149:     if ($reqtype eq 'HEAD') {
14150: 	$$info = &HTTP::Date::str2time( $response->header('Last-modified') );
14151:     } elsif ($reqtype eq 'GET') {
14152: 	$$info = $response->content;
14153:     }
14154:     return 'ok';
14155: }
14156: 
14157: sub readfile {
14158:     my $file = shift;
14159:     if ( (! -e $file ) || ($file eq '') ) { return -1; };
14160:     my $fh;
14161:     open($fh,"<",$file);
14162:     my $a='';
14163:     while (my $line = <$fh>) { $a .= $line; }
14164:     return $a;
14165: }
14166: 
14167: sub filelocation {
14168:     my ($dir,$file) = @_;
14169:     my $location;
14170:     $file=~ s/^\s*(\S+)\s*$/$1/; ## strip off leading and trailing spaces
14171: 
14172:     if ($file =~ m-^/adm/-) {
14173: 	$file=~s-^/adm/wrapper/-/-;
14174: 	$file=~s-^/adm/coursedocs/showdoc/-/-;
14175:     }
14176: 
14177:     if ($file =~ m-^\Q$Apache::lonnet::perlvar{'lonTabDir'}\E/-) {
14178:         $location = $file;
14179:     } elsif ($file=~/^\/*(uploaded|editupload)/) { # is an uploaded file
14180:         my ($udom,$uname,$filename)=
14181:   	    ($file=~m -^/+(?:uploaded|editupload)/+($match_domain)/+($match_name)/+(.*)$-);
14182:         my $home=&homeserver($uname,$udom);
14183:         my $is_me=0;
14184:         my @ids=&current_machine_ids();
14185:         foreach my $id (@ids) { if ($id eq $home) { $is_me=1; } }
14186:         if ($is_me) {
14187:   	    $location=propath($udom,$uname).'/userfiles/'.$filename;
14188:         } else {
14189:   	  $location=$Apache::lonnet::perlvar{'lonDocRoot'}.'/userfiles/'.
14190:   	      $udom.'/'.$uname.'/'.$filename;
14191:         }
14192:     } elsif ($file =~ m-^/adm/-) {
14193: 	$location = $perlvar{'lonDocRoot'}.'/'.$file;
14194:     } else {
14195:         $file=~s/^\Q$perlvar{'lonDocRoot'}\E//;
14196:         $file=~s:^/(res|priv)/:/:;
14197:         my $space=$1;
14198:         if ( !( $file =~ m:^/:) ) {
14199:             $location = $dir. '/'.$file;
14200:         } else {
14201:             $location = $perlvar{'lonDocRoot'}.'/'.$space.$file;
14202:         }
14203:     }
14204:     $location=~s://+:/:g; # remove duplicate /
14205:     while ($location=~m{/\.\./}) {
14206: 	if ($location =~ m{/[^/]+/\.\./}) {
14207: 	    $location=~ s{/[^/]+/\.\./}{/}g;
14208: 	} else {
14209: 	    $location=~ s{/\.\./}{/}g;
14210: 	}
14211:     } #remove dir/..
14212:     while ($location=~m:/\./:) {$location=~ s:/\./:/:g;} #remove /./
14213:     return $location;
14214: }
14215: 
14216: sub hreflocation {
14217:     my ($dir,$file)=@_;
14218:     unless (($file=~m-^https?\://-i) || ($file=~m-^/-)) {
14219: 	$file=filelocation($dir,$file);
14220:     } elsif ($file=~m-^/adm/-) {
14221: 	$file=~s-^/adm/wrapper/-/-;
14222: 	$file=~s-^/adm/coursedocs/showdoc/-/-;
14223:     }
14224:     if ($file=~m-^\Q$perlvar{'lonDocRoot'}\E-) {
14225: 	$file=~s-^\Q$perlvar{'lonDocRoot'}\E--;
14226:     } elsif ($file=~m-^\Q$perlvar{'lonUsersDir'}\E-) {
14227: 	$file=~s{^/home/httpd/lonUsers/($match_domain)/./././($match_name)/userfiles/}
14228: 	        {/uploaded/$1/$2/}x;
14229:     }
14230:     if ($file=~ m{^/userfiles/}) {
14231: 	$file =~ s{^/userfiles/}{/uploaded/};
14232:     }
14233:     return $file;
14234: }
14235: 
14236: 
14237: 
14238: 
14239: 
14240: sub current_machine_domains {
14241:     return &machine_domains(&hostname($perlvar{'lonHostID'}));
14242: }
14243: 
14244: sub machine_domains {
14245:     my ($hostname) = @_;
14246:     my @domains;
14247:     my %hostname = &all_hostnames();
14248:     while( my($id, $name) = each(%hostname)) {
14249: #	&logthis("-$id-$name-$hostname-");
14250: 	if ($hostname eq $name) {
14251: 	    push(@domains,&host_domain($id));
14252: 	}
14253:     }
14254:     return @domains;
14255: }
14256: 
14257: sub current_machine_ids {
14258:     return &machine_ids(&hostname($perlvar{'lonHostID'}));
14259: }
14260: 
14261: sub machine_ids {
14262:     my ($hostname) = @_;
14263:     $hostname ||= &hostname($perlvar{'lonHostID'});
14264:     my @ids;
14265:     my %name_to_host = &all_names();
14266:     if (ref($name_to_host{$hostname}) eq 'ARRAY') {
14267: 	return @{ $name_to_host{$hostname} };
14268:     }
14269:     return;
14270: }
14271: 
14272: sub additional_machine_domains {
14273:     my @domains;
14274:     open(my $fh,"<","$perlvar{'lonTabDir'}/expected_domains.tab");
14275:     while( my $line = <$fh>) {
14276:         $line =~ s/\s//g;
14277:         push(@domains,$line);
14278:     }
14279:     return @domains;
14280: }
14281: 
14282: sub default_login_domain {
14283:     my $domain = $perlvar{'lonDefDomain'};
14284:     my $testdomain=(split(/\./,$ENV{'HTTP_HOST'}))[0];
14285:     foreach my $posdom (&current_machine_domains(),
14286:                         &additional_machine_domains()) {
14287:         if (lc($posdom) eq lc($testdomain)) {
14288:             $domain=$posdom;
14289:             last;
14290:         }
14291:     }
14292:     return $domain;
14293: }
14294: 
14295: sub shared_institution {
14296:     my ($dom,$lonhost) = @_;
14297:     if ($lonhost eq '') {
14298:         $lonhost = $perlvar{'lonHostID'};
14299:     }
14300:     my $same_intdom;
14301:     my $hostintdom = &internet_dom($lonhost);
14302:     if ($hostintdom ne '') {
14303:         my %iphost = &get_iphost();
14304:         my $primary_id = &domain($dom,'primary');
14305:         my $primary_ip = &get_host_ip($primary_id);
14306:         if (ref($iphost{$primary_ip}) eq 'ARRAY') {
14307:             foreach my $id (@{$iphost{$primary_ip}}) {
14308:                 my $intdom = &internet_dom($id);
14309:                 if ($intdom eq $hostintdom) {
14310:                     $same_intdom = 1;
14311:                     last;
14312:                 }
14313:             }
14314:         }
14315:     }
14316:     return $same_intdom;
14317: }
14318: 
14319: sub uses_sts {
14320:     my ($ignore_cache) = @_;
14321:     my $lonhost = $perlvar{'lonHostID'};
14322:     my $hostname = &hostname($lonhost);
14323:     my $sts_on;
14324:     if ($protocol{$lonhost} eq 'https') {
14325:         my $cachetime = 12*3600;
14326:         if (!$ignore_cache) {
14327:             ($sts_on,my $cached)=&is_cached_new('stspolicy',$lonhost);
14328:             if (defined($cached)) {
14329:                 return $sts_on;
14330:             }
14331:         }
14332:         my $url = $protocol{$lonhost}.'://'.$hostname.'/index.html';
14333:         my $request=new HTTP::Request('HEAD',$url);
14334:         my $response=&LONCAPA::LWPReq::makerequest($lonhost,$request,'',\%perlvar,'','','',1);
14335:         if ($response->is_success) {
14336:             my $has_sts = $response->header('Strict-Transport-Security');
14337:             if ($has_sts eq '') {
14338:                 $sts_on = 0;
14339:             } else {
14340:                 if ($has_sts =~ /\Qmax-age=\E(\d+)/) {
14341:                     my $maxage = $1;
14342:                     if ($maxage) {
14343:                         $sts_on = 1;
14344:                     } else {
14345:                         $sts_on = 0;
14346:                     }
14347:                 } else {
14348:                     $sts_on = 0;
14349:                 }
14350:             }
14351:             return &do_cache_new('stspolicy',$lonhost,$sts_on,$cachetime);
14352:         }
14353:     }
14354:     return;
14355: }
14356: 
14357: sub waf_allssl {
14358:     my ($host_name) = @_;
14359:     my $alias = &get_proxy_alias();
14360:     if ($host_name eq '') {
14361:         $host_name = $ENV{'SERVER_NAME'};
14362:     }
14363:     if (($host_name ne '') && ($alias eq $host_name)) {
14364:         my $serverhomedom = &host_domain($perlvar{'lonHostID'});
14365:         my %defdomdefaults = &get_domain_defaults($serverhomedom);
14366:         if ($defdomdefaults{'waf_sslopt'}) {
14367:             return $defdomdefaults{'waf_sslopt'};
14368:         }
14369:     }
14370:     return;
14371: }
14372: 
14373: sub get_requestor_ip {
14374:     my ($r,$nolookup,$noproxy) = @_;
14375:     my $from_ip;
14376:     if (ref($r)) {
14377:         if ($r->can('useragent_ip')) {
14378:             if ($noproxy && $r->can('client_ip')) {
14379:                 $from_ip = $r->client_ip();
14380:             } else {
14381:                 $from_ip = $r->useragent_ip();
14382:             }
14383:         } elsif ($r->connection->can('remote_ip')) {
14384:             $from_ip = $r->connection->remote_ip();
14385:         } else {
14386:             $from_ip = $r->get_remote_host($nolookup);
14387:         }
14388:     } else {
14389:         $from_ip = $ENV{'REMOTE_ADDR'};
14390:     }
14391:     return $from_ip if ($noproxy); 
14392:     # Who controls proxy settings for server
14393:     my $dom_in_use = $Apache::lonnet::perlvar{'lonDefDomain'};
14394:     my $proxyinfo = &get_proxy_settings($dom_in_use);
14395:     if ((ref($proxyinfo) eq 'HASH') && ($from_ip)) {
14396:         if ($proxyinfo->{'vpnint'}) {
14397:             if (&ip_match($from_ip,$proxyinfo->{'vpnint'})) {
14398:                 return $from_ip;
14399:             }
14400:         }
14401:         if ($proxyinfo->{'trusted'}) {
14402:             if (&ip_match($from_ip,$proxyinfo->{'trusted'})) {
14403:                 my $ipheader = $proxyinfo->{'ipheader'};
14404:                 my ($ip,$xfor);
14405:                 if (ref($r)) {
14406:                     if ($ipheader) {
14407:                         $ip = $r->headers_in->{$ipheader};
14408:                     }
14409:                     $xfor = $r->headers_in->{'X-Forwarded-For'};
14410:                 } else {
14411:                     if ($ipheader) {
14412:                         $ip = $ENV{'HTTP_'.uc($ipheader)};
14413:                     }
14414:                     $xfor = $ENV{'HTTP_X_FORWARDED_FOR'};
14415:                 }
14416:                 if (($ip eq '') && ($xfor ne '')) {
14417:                     foreach my $poss_ip (reverse(split(/\s*,\s*/,$xfor))) {
14418:                         unless (&ip_match($poss_ip,$proxyinfo->{'trusted'})) {
14419:                             $ip = $poss_ip;
14420:                             last;
14421:                         }
14422:                     }
14423:                 }
14424:                 if ($ip ne '') {
14425:                     return $ip;
14426:                 }
14427:             }
14428:         }
14429:     }
14430:     return $from_ip;
14431: }
14432: 
14433: sub get_proxy_settings {
14434:     my ($dom_in_use) = @_;
14435:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom_in_use);
14436:     my $proxyinfo = {
14437:                        ipheader => $domdefaults{'waf_ipheader'},
14438:                        trusted  => $domdefaults{'waf_trusted'},
14439:                        vpnint   => $domdefaults{'waf_vpnint'},
14440:                        vpnext   => $domdefaults{'waf_vpnext'},
14441:                        sslopt   => $domdefaults{'waf_sslopt'},
14442:                     };
14443:     return $proxyinfo;
14444: }
14445: 
14446: sub ip_match {
14447:     my ($ip,$pattern_str) = @_;
14448:     $ip=Net::CIDR::cidrvalidate($ip);
14449:     if ($ip) {
14450:         return Net::CIDR::cidrlookup($ip,split(/\s*,\s*/,$pattern_str));
14451:     }
14452:     return;
14453: }
14454: 
14455: sub get_proxy_alias {
14456:     my ($lonid) = @_;
14457:     if ($lonid eq '') {
14458:         $lonid = $perlvar{'lonHostID'};
14459:     }
14460:     if (!defined(&hostname($lonid))) {
14461:         return;
14462:     }
14463:     if ($lonid ne '') {
14464:         my ($alias,$cached) = &is_cached_new('proxyalias',$lonid);
14465:         if ($cached) {
14466:             return $alias;
14467:         }
14468:         my $dom = &Apache::lonnet::host_domain($lonid);
14469:         if ($dom ne '') {
14470:             my $cachetime = 60*60*24;
14471:             my %domconfig =
14472:                 &Apache::lonnet::get_dom('configuration',['wafproxy'],$dom);
14473:             my $alias;
14474:             if (ref($domconfig{'wafproxy'}) eq 'HASH') {
14475:                 if (ref($domconfig{'wafproxy'}{'alias'}) eq 'HASH') {
14476:                     $alias = $domconfig{'wafproxy'}{'alias'}{$lonid};
14477:                 }
14478:             }
14479:             return &do_cache_new('proxyalias',$lonid,$alias,$cachetime);
14480:         }
14481:     }
14482:     return;
14483: }
14484: 
14485: sub use_proxy_alias {
14486:     my ($r,$lonid) = @_;
14487:     my $alias = &get_proxy_alias($lonid);
14488:     if ($alias) {
14489:         my $dom = &host_domain($lonid);
14490:         if ($dom ne '') {
14491:             my $proxyinfo = &get_proxy_settings($dom );
14492:             my ($vpnint,$remote_ip);
14493:             if (ref($proxyinfo) eq 'HASH') {
14494:                 $vpnint = $proxyinfo->{'vpnint'};
14495:                 if ($vpnint) {
14496:                     $remote_ip = &get_requestor_ip($r,1,1);
14497:                 }
14498:             }
14499:             unless ($vpnint && &ip_match($remote_ip,$vpnint)) {
14500:                 return $alias;
14501:             }
14502:         }
14503:     }
14504:     return;
14505: }
14506: 
14507: # ------------------------------------------------------------- Declutters URLs
14508: 
14509: sub declutter {
14510:     my $thisfn=shift;
14511:     if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
14512:     unless ($thisfn=~m{^/home/httpd/html/priv/}) {
14513:         $thisfn=~s{^/home/httpd/html}{};
14514:     }
14515:     $thisfn=~s/^\///;
14516:     $thisfn=~s|^adm/wrapper/||;
14517:     $thisfn=~s|^adm/coursedocs/showdoc/||;
14518:     $thisfn=~s/^res\///;
14519:     $thisfn=~s/^priv\///;
14520:     unless (($thisfn =~ /^ext/) || ($thisfn =~ /\.(page|sequence)___\d+___ext/)) {
14521:         $thisfn=~s/\?.+$//;
14522:     }
14523:     return $thisfn;
14524: }
14525: 
14526: # ------------------------------------------------------------- Clutter up URLs
14527: 
14528: sub clutter {
14529:     my $thisfn='/'.&declutter(shift);
14530:     if ($thisfn !~ m{^/(uploaded|editupload|adm|userfiles|ext|raw|priv|public)/}
14531: 	|| $thisfn =~ m{^/adm/(includes|pages)} ) { 
14532:        $thisfn='/res'.$thisfn; 
14533:     }
14534:     if ($thisfn !~m|^/adm|) {
14535: 	if ($thisfn =~ m|^/ext/|) {
14536: 	    $thisfn='/adm/wrapper'.$thisfn;
14537: 	} else {
14538: 	    my ($ext) = ($thisfn =~ /\.(\w+)$/);
14539: 	    my $embstyle=&Apache::loncommon::fileembstyle($ext);
14540: 	    if ($embstyle eq 'ssi'
14541: 		|| ($embstyle eq 'hdn')
14542: 		|| ($embstyle eq 'rat')
14543: 		|| ($embstyle eq 'prv')
14544: 		|| ($embstyle eq 'ign')) {
14545: 		#do nothing with these
14546: 	    } elsif (($embstyle eq 'img') 
14547: 		|| ($embstyle eq 'emb')
14548: 		|| ($embstyle eq 'wrp')) {
14549: 		$thisfn='/adm/wrapper'.$thisfn;
14550: 	    } elsif ($embstyle eq 'unk'
14551: 		     && $thisfn!~/\.(sequence|page)$/) {
14552: 		$thisfn='/adm/coursedocs/showdoc'.$thisfn;
14553: 	    } else {
14554: #		&logthis("Got a blank emb style");
14555: 	    }
14556: 	}
14557:     } elsif ($thisfn =~ m{^/adm/$match_domain/$match_courseid/\d+/ext\.tool$}) {
14558:         $thisfn='/adm/wrapper'.$thisfn;
14559:     }
14560:     return $thisfn;
14561: }
14562: 
14563: sub clutter_with_no_wrapper {
14564:     my $uri = &clutter(shift);
14565:     if ($uri =~ m-^/adm/-) {
14566: 	$uri =~ s-^/adm/wrapper/-/-;
14567: 	$uri =~ s-^/adm/coursedocs/showdoc/-/-;
14568:     }
14569:     return $uri;
14570: }
14571: 
14572: sub freeze_escape {
14573:     my ($value)=@_;
14574:     if (ref($value)) {
14575: 	$value=&nfreeze($value);
14576: 	return '__FROZEN__'.&escape($value);
14577:     }
14578:     return &escape($value);
14579: }
14580: 
14581: 
14582: sub thaw_unescape {
14583:     my ($value)=@_;
14584:     if ($value =~ /^__FROZEN__/) {
14585: 	substr($value,0,10,undef);
14586: 	$value=&unescape($value);
14587: 	return &thaw($value);
14588:     }
14589:     return &unescape($value);
14590: }
14591: 
14592: sub correct_line_ends {
14593:     my ($result)=@_;
14594:     $$result =~s/\r\n/\n/mg;
14595:     $$result =~s/\r/\n/mg;
14596: }
14597: # ================================================================ Main Program
14598: 
14599: sub goodbye {
14600:    &logthis("Starting Shut down");
14601: #not converted to using infrastruture and probably shouldn't be
14602:    &logthis(sprintf("%-20s is %s",'%badServerCache',length(&nfreeze(\%badServerCache))));
14603: #converted
14604: #   &logthis(sprintf("%-20s is %s",'%metacache',scalar(%metacache)));
14605:    &logthis(sprintf("%-20s is %s",'%homecache',length(&nfreeze(\%homecache))));
14606: #   &logthis(sprintf("%-20s is %s",'%titlecache',length(&nfreeze(\%titlecache))));
14607: #   &logthis(sprintf("%-20s is %s",'%courseresdatacache',length(&nfreeze(\%courseresdatacache))));
14608: #1.1 only
14609: #   &logthis(sprintf("%-20s is %s",'%userresdatacache',length(&nfreeze(\%userresdatacache))));
14610: #   &logthis(sprintf("%-20s is %s",'%getsectioncache',length(&nfreeze(\%getsectioncache))));
14611: #   &logthis(sprintf("%-20s is %s",'%courseresversioncache',length(&nfreeze(\%courseresversioncache))));
14612: #   &logthis(sprintf("%-20s is %s",'%resversioncache',length(&nfreeze(\%resversioncache))));
14613:    &logthis(sprintf("%-20s is %s",'%remembered',length(&nfreeze(\%remembered))));
14614:    &logthis(sprintf("%-20s is %s",'kicks',$kicks));
14615:    &logthis(sprintf("%-20s is %s",'hits',$hits));
14616:    &flushcourselogs();
14617:    &logthis("Shutting down");
14618: }
14619: 
14620: sub get_dns {
14621:     my ($url,$func,$ignore_cache,$nocache,$hashref) = @_;
14622:     if (!$ignore_cache) {
14623: 	my ($content,$cached)=
14624: 	    &Apache::lonnet::is_cached_new('dns',$url);
14625: 	if ($cached) {
14626: 	    &$func($content,$hashref);
14627: 	    return;
14628: 	}
14629:     }
14630: 
14631:     my %alldns;
14632:     if (open(my $config,"<","$perlvar{'lonTabDir'}/hosts.tab")) {
14633:         foreach my $dns (<$config>) {
14634: 	    next if ($dns !~ /^\^(\S*)/x);
14635:             my $line = $1;
14636:             my ($host,$protocol) = split(/:/,$line);
14637:             if ($protocol ne 'https') {
14638:                 $protocol = 'http';
14639:             }
14640: 	    $alldns{$host} = $protocol;
14641:         }
14642:         close($config);
14643:     }
14644:     while (%alldns) {
14645: 	my ($dns) = sort { $b cmp $a } keys(%alldns);
14646: 	my $request=new HTTP::Request('GET',"$alldns{$dns}://$dns$url");
14647:         my $response = &LONCAPA::LWPReq::makerequest('',$request,'',\%perlvar,30,0);
14648:         delete($alldns{$dns});
14649: 	next if ($response->is_error());
14650:         if ($url eq '/adm/dns/loncapaCRL') {
14651:             return &$func($response);
14652:         } else {
14653: 	    my @content = split("\n",$response->content);
14654: 	    unless ($nocache) {
14655: 	        &do_cache_new('dns',$url,\@content,30*24*60*60);
14656: 	    }
14657: 	    &$func(\@content,$hashref);
14658:             return;
14659:         }
14660:     }
14661:     my $which = (split('/',$url,4))[3];
14662:     if ($which eq 'loncapaCRL') {
14663:         my $diskfile = "$perlvar{'lonCertificateDirectory'}/$perlvar{'lonnetCertRevocationList'}";
14664:         if (-e $diskfile) {
14665:             &logthis("unable to contact DNS, on disk file $diskfile not updated");
14666:         } else {
14667:             &logthis("unable to contact DNS, no on disk file $diskfile available");
14668:         }
14669:     } else {
14670:         &logthis("unable to contact DNS defaulting to on disk file dns_$which.tab\n");
14671:         if (open(my $config,"<","$perlvar{'lonTabDir'}/dns_$which.tab")) {
14672:             my @content = <$config>;
14673:             close($config);
14674:             &$func(\@content,$hashref);
14675:         }
14676:     }
14677:     return;
14678: }
14679: 
14680: # ------------------------------------------------------Get DNS checksums file
14681: sub parse_dns_checksums_tab {
14682:     my ($lines,$hashref) = @_;
14683:     my $lonhost = $perlvar{'lonHostID'};
14684:     my $machine_dom = &Apache::lonnet::host_domain($lonhost);
14685:     my $loncaparev = &get_server_loncaparev($machine_dom);
14686:     my $distro = (split(/\:/,&get_server_distarch($lonhost)))[0];
14687:     my $webconfdir = '/etc/httpd/conf';
14688:     if ($distro =~ /^(ubuntu|debian)(\d+)$/) {
14689:         $webconfdir = '/etc/apache2';
14690:     } elsif ($distro =~ /^sles(\d+)$/) {
14691:         if ($1 >= 10) {
14692:             $webconfdir = '/etc/apache2';
14693:         }
14694:     } elsif ($distro =~ /^suse(\d+\.\d+)$/) {
14695:         if ($1 >= 10.0) {
14696:             $webconfdir = '/etc/apache2';
14697:         }
14698:     }
14699:     my ($release,$timestamp) = split(/\-/,$loncaparev);
14700:     my (%chksum,%revnum);
14701:     if (ref($lines) eq 'ARRAY') {
14702:         chomp(@{$lines});
14703:         my $version = shift(@{$lines});
14704:         if ($version eq $release) {  
14705:             foreach my $line (@{$lines}) {
14706:                 my ($file,$version,$shasum) = split(/,/,$line);
14707:                 if ($file =~ m{^/etc/httpd/conf}) {
14708:                     if ($webconfdir eq '/etc/apache2') {
14709:                         $file =~ s{^\Q/etc/httpd/conf/\E}{$webconfdir/};
14710:                     }
14711:                 }
14712:                 $chksum{$file} = $shasum;
14713:                 $revnum{$file} = $version;
14714:             }
14715:             if (ref($hashref) eq 'HASH') {
14716:                 %{$hashref} = (
14717:                                 sums     => \%chksum,
14718:                                 versions => \%revnum,
14719:                               );
14720:             }
14721:         }
14722:     }
14723:     return;
14724: }
14725: 
14726: sub fetch_dns_checksums {
14727:     my %checksums;
14728:     my $machine_dom = &Apache::lonnet::host_domain($perlvar{'lonHostID'});
14729:     my $loncaparev = &get_server_loncaparev($machine_dom,$perlvar{'lonHostID'});
14730:     my ($release,$timestamp) = split(/\-/,$loncaparev);
14731:     &get_dns("/adm/dns/checksums/$release",\&parse_dns_checksums_tab,1,1,
14732:              \%checksums);
14733:     return \%checksums;
14734: }
14735: 
14736: sub fetch_crl_pemfile {
14737:     return &get_dns("/adm/dns/loncapaCRL",\&save_crl_pem,1,1);
14738: }
14739: 
14740: sub save_crl_pem {
14741:     my ($response) = @_;
14742:     my ($msg,$hadchanges);
14743:     if (ref($response)) {
14744:         my $now = time;
14745:         my $lonca = $perlvar{'lonCertificateDirectory'}.'/'.$perlvar{'lonnetCertificateAuthority'};
14746:         my $tmpcrl = $tmpdir.'/'.$perlvar{'lonnetCertRevocationList'}.'_'.$now.'.'.$$.'.tmp';
14747:         if (open(my $fh,'>',"$tmpcrl")) {
14748:             print $fh $response->content;
14749:             close($fh);
14750:             if (-e $lonca) {
14751:                 if (open(PIPE,"openssl crl -in $tmpcrl -inform pem -CAfile $lonca -noout 2>&1 |")) {
14752:                     my $check = <PIPE>;
14753:                     close(PIPE);
14754:                     chomp($check);
14755:                     if ($check eq 'verify OK') {
14756:                         my $dest = "$perlvar{'lonCertificateDirectory'}/$perlvar{'lonnetCertRevocationList'}";
14757:                         my $backup;
14758:                         if (-e $dest) {
14759:                             if (&File::Copy::move($dest,"$dest.bak")) {
14760:                                 $backup = 'ok';
14761:                             }
14762:                         }
14763:                         if (&File::Copy::move($tmpcrl,$dest)) {
14764:                             $msg = 'ok';
14765:                             if ($backup) {
14766:                                 my (%oldnums,%newnums);
14767:                                 if (open(PIPE, "openssl crl -inform PEM -text -noout -in $dest.bak |grep 'Serial Number' |")) {
14768:                                     while (<PIPE>) {
14769:                                         $oldnums{(split(/:/))[1]} = 1;
14770:                                     }
14771:                                     close(PIPE);
14772:                                 }
14773:                                 if (open(PIPE, "openssl crl -inform PEM -text -noout -in $dest |grep 'Serial Number' |")) {
14774:                                     while(<PIPE>) {
14775:                                         $newnums{(split(/:/))[1]} = 1;
14776:                                     }
14777:                                     close(PIPE);
14778:                                 }
14779:                                 foreach my $key (sort {$b <=> $a } (keys(%newnums))) {
14780:                                     unless (exists($oldnums{$key})) {
14781:                                         $hadchanges = 1;
14782:                                         last;
14783:                                     }
14784:                                 }
14785:                                 unless ($hadchanges) {
14786:                                     foreach my $key (sort {$b <=> $a } (keys(%oldnums))) {
14787:                                         unless (exists($newnums{$key})) {
14788:                                             $hadchanges = 1;
14789:                                             last;
14790:                                         }
14791:                                     }
14792:                                 }
14793:                             }
14794:                         }
14795:                     } else {
14796:                         unlink($tmpcrl);
14797:                     }
14798:                 } else {
14799:                     unlink($tmpcrl);
14800:                 }
14801:             } else {
14802:                 unlink($tmpcrl);
14803:             }
14804:         }
14805:     }
14806:     return ($msg,$hadchanges);
14807: }
14808: 
14809: # ------------------------------------------------------------ Read domain file
14810: {
14811:     my $loaded;
14812:     my %domain;
14813: 
14814:     sub parse_domain_tab {
14815: 	my ($lines) = @_;
14816: 	foreach my $line (@$lines) {
14817: 	    next if ($line =~ /^(\#|\s*$ )/x);
14818: 
14819: 	    chomp($line);
14820: 	    my ($name,@elements) = split(/:/,$line,9);
14821: 	    my %this_domain;
14822: 	    foreach my $field ('description', 'auth_def', 'auth_arg_def',
14823: 			       'lang_def', 'city', 'longi', 'lati',
14824: 			       'primary') {
14825: 		$this_domain{$field} = shift(@elements);
14826: 	    }
14827: 	    $domain{$name} = \%this_domain;
14828: 	}
14829:     }
14830: 
14831:     sub reset_domain_info {
14832: 	undef($loaded);
14833: 	undef(%domain);
14834:     }
14835: 
14836:     sub load_domain_tab {
14837: 	my ($ignore_cache,$nocache) = @_;
14838: 	&get_dns('/adm/dns/domain',\&parse_domain_tab,$ignore_cache,$nocache);
14839: 	my $fh;
14840: 	if (open($fh,"<",$perlvar{'lonTabDir'}.'/domain.tab')) {
14841: 	    my @lines = <$fh>;
14842: 	    &parse_domain_tab(\@lines);
14843: 	}
14844: 	close($fh);
14845: 	$loaded = 1;
14846:     }
14847: 
14848:     sub domain {
14849: 	&load_domain_tab() if (!$loaded);
14850: 
14851: 	my ($name,$what) = @_;
14852: 	return if ( !exists($domain{$name}) );
14853: 
14854: 	if (!$what) {
14855: 	    return $domain{$name}{'description'};
14856: 	}
14857: 	return $domain{$name}{$what};
14858:     }
14859: 
14860:     sub domain_info {
14861:         &load_domain_tab() if (!$loaded);
14862:         return %domain;
14863:     }
14864: 
14865: }
14866: 
14867: 
14868: # ------------------------------------------------------------- Read hosts file
14869: {
14870:     my %hostname;
14871:     my %hostdom;
14872:     my %libserv;
14873:     my $loaded;
14874:     my %name_to_host;
14875:     my %internetdom;
14876:     my %LC_dns_serv;
14877: 
14878:     sub parse_hosts_tab {
14879: 	my ($file) = @_;
14880: 	foreach my $configline (@$file) {
14881: 	    next if ($configline =~ /^(\#|\s*$ )/x);
14882:             chomp($configline);
14883: 	    if ($configline =~ /^\^/) {
14884:                 if ($configline =~ /^\^([\w.\-]+)/) {
14885:                     $LC_dns_serv{$1} = 1;
14886:                 }
14887:                 next;
14888:             }
14889: 	    my ($id,$domain,$role,$name,$protocol,$intdom)=split(/:/,$configline);
14890: 	    $name=~s/\s//g;
14891: 	    if ($id && $domain && $role && $name) {
14892:                 if ((exists($hostname{$id})) && ($hostname{$id} ne '')) {
14893:                     my $curr = $hostname{$id};
14894:                     my $skip;
14895:                     if (ref($name_to_host{$curr}) eq 'ARRAY') {
14896:                         if (($curr eq $name) && (@{$name_to_host{$curr}} == 1)) {
14897:                             $skip = 1;
14898:                         } else {
14899:                             @{$name_to_host{$curr}} = grep { $_ ne $id } @{$name_to_host{$curr}};
14900:                         }
14901:                     }
14902:                     unless ($skip) {
14903:                         push(@{$name_to_host{$name}},$id);
14904:                     }
14905:                 } else {
14906:                     push(@{$name_to_host{$name}},$id);
14907:                 }
14908: 		$hostname{$id}=$name;
14909: 		$hostdom{$id}=$domain;
14910: 		if ($role eq 'library') { $libserv{$id}=$name; }
14911:                 if (defined($protocol)) {
14912:                     if ($protocol eq 'https') {
14913:                         $protocol{$id} = $protocol;
14914:                     } else {
14915:                         $protocol{$id} = 'http'; 
14916:                     }
14917:                 } else {
14918:                     $protocol{$id} = 'http';
14919:                 }
14920:                 if (defined($intdom)) {
14921:                     $internetdom{$id} = $intdom;
14922:                 }
14923: 	    }
14924: 	}
14925:     }
14926:     
14927:     sub reset_hosts_info {
14928: 	&purge_remembered();
14929: 	&reset_domain_info();
14930: 	&reset_hosts_ip_info();
14931:         undef(%internetdom);
14932: 	undef(%name_to_host);
14933: 	undef(%hostname);
14934: 	undef(%hostdom);
14935: 	undef(%libserv);
14936: 	undef($loaded);
14937:     }
14938: 
14939:     sub load_hosts_tab {
14940: 	my ($ignore_cache,$nocache) = @_;
14941: 	&get_dns('/adm/dns/hosts',\&parse_hosts_tab,$ignore_cache,$nocache);
14942: 	open(my $config,"<","$perlvar{'lonTabDir'}/hosts.tab");
14943: 	my @config = <$config>;
14944: 	&parse_hosts_tab(\@config);
14945: 	close($config);
14946: 	$loaded=1;
14947:     }
14948: 
14949:     sub hostname {
14950: 	&load_hosts_tab() if (!$loaded);
14951: 
14952: 	my ($lonid) = @_;
14953: 	return $hostname{$lonid};
14954:     }
14955: 
14956:     sub all_hostnames {
14957: 	&load_hosts_tab() if (!$loaded);
14958: 
14959: 	return %hostname;
14960:     }
14961: 
14962:     sub all_names {
14963:         my ($ignore_cache,$nocache) = @_;
14964: 	&load_hosts_tab($ignore_cache,$nocache) if (!$loaded);
14965: 
14966: 	return %name_to_host;
14967:     }
14968: 
14969:     sub all_host_domain {
14970:         &load_hosts_tab() if (!$loaded);
14971:         return %hostdom;
14972:     }
14973: 
14974:     sub all_host_intdom {
14975:         &load_hosts_tab() if (!$loaded);
14976:         return %internetdom;
14977:     }
14978: 
14979:     sub is_library {
14980: 	&load_hosts_tab() if (!$loaded);
14981: 
14982: 	return exists($libserv{$_[0]});
14983:     }
14984: 
14985:     sub all_library {
14986: 	&load_hosts_tab() if (!$loaded);
14987: 
14988: 	return %libserv;
14989:     }
14990: 
14991:     sub unique_library {
14992: 	#2x reverse removes all hostnames that appear more than once
14993:         my %unique = reverse &all_library();
14994:         return reverse %unique;
14995:     }
14996: 
14997:     sub get_servers {
14998: 	&load_hosts_tab() if (!$loaded);
14999: 
15000: 	my ($domain,$type) = @_;
15001: 	my %possible_hosts = ($type eq 'library') ? %libserv
15002: 	                                          : %hostname;
15003: 	my %result;
15004: 	if (ref($domain) eq 'ARRAY') {
15005: 	    while ( my ($host,$hostname) = each(%possible_hosts)) {
15006: 		if (grep(/^\Q$hostdom{$host}\E$/,@$domain)) {
15007: 		    $result{$host} = $hostname;
15008: 		}
15009: 	    }
15010: 	} else {
15011: 	    while ( my ($host,$hostname) = each(%possible_hosts)) {
15012: 		if ($hostdom{$host} eq $domain) {
15013: 		    $result{$host} = $hostname;
15014: 		}
15015: 	    }
15016: 	}
15017: 	return %result;
15018:     }
15019: 
15020:     sub get_unique_servers {
15021:         my %unique = reverse &get_servers(@_);
15022: 	return reverse %unique;
15023:     }
15024: 
15025:     sub host_domain {
15026: 	&load_hosts_tab() if (!$loaded);
15027: 
15028: 	my ($lonid) = @_;
15029: 	return $hostdom{$lonid};
15030:     }
15031: 
15032:     sub all_domains {
15033: 	&load_hosts_tab() if (!$loaded);
15034: 
15035: 	my %seen;
15036: 	my @uniq = grep(!$seen{$_}++, values(%hostdom));
15037: 	return @uniq;
15038:     }
15039: 
15040:     sub internet_dom {
15041:         &load_hosts_tab() if (!$loaded);
15042: 
15043:         my ($lonid) = @_;
15044:         return $internetdom{$lonid};
15045:     }
15046: 
15047:     sub is_LC_dns {
15048:         &load_hosts_tab() if (!$loaded);
15049: 
15050:         my ($hostname) = @_;
15051:         return exists($LC_dns_serv{$hostname});
15052:     }
15053: 
15054: }
15055: 
15056: { 
15057:     my %iphost;
15058:     my %name_to_ip;
15059:     my %lonid_to_ip;
15060: 
15061:     sub get_hosts_from_ip {
15062: 	my ($ip) = @_;
15063: 	my %iphosts = &get_iphost();
15064: 	if (ref($iphosts{$ip})) {
15065: 	    return @{$iphosts{$ip}};
15066: 	}
15067: 	return;
15068:     }
15069:     
15070:     sub reset_hosts_ip_info {
15071: 	undef(%iphost);
15072: 	undef(%name_to_ip);
15073: 	undef(%lonid_to_ip);
15074:     }
15075: 
15076:     sub get_host_ip {
15077: 	my ($lonid) = @_;
15078: 	if (exists($lonid_to_ip{$lonid})) {
15079: 	    return $lonid_to_ip{$lonid};
15080: 	}
15081: 	my $name=&hostname($lonid);
15082:    	my $ip = gethostbyname($name);
15083: 	return if (!$ip || length($ip) ne 4);
15084: 	$ip=inet_ntoa($ip);
15085: 	$name_to_ip{$name}   = $ip;
15086: 	$lonid_to_ip{$lonid} = $ip;
15087: 	return $ip;
15088:     }
15089:     
15090:     sub get_iphost {
15091: 	my ($ignore_cache,$nocache) = @_;
15092: 
15093: 	if (!$ignore_cache) {
15094: 	    if (%iphost) {
15095: 		return %iphost;
15096: 	    }
15097: 	    my ($ip_info,$cached)=
15098: 		&Apache::lonnet::is_cached_new('iphost','iphost');
15099: 	    if ($cached) {
15100: 		%iphost      = %{$ip_info->[0]};
15101: 		%name_to_ip  = %{$ip_info->[1]};
15102: 		%lonid_to_ip = %{$ip_info->[2]};
15103: 		return %iphost;
15104: 	    }
15105: 	}
15106: 
15107: 	# get yesterday's info for fallback
15108: 	my %old_name_to_ip;
15109: 	my ($ip_info,$cached)=
15110: 	    &Apache::lonnet::is_cached_new('iphost','iphost');
15111: 	if ($cached) {
15112: 	    %old_name_to_ip = %{$ip_info->[1]};
15113: 	}
15114: 
15115: 	my %name_to_host = &all_names($ignore_cache,$nocache);
15116: 	foreach my $name (keys(%name_to_host)) {
15117: 	    my $ip;
15118: 	    if (!exists($name_to_ip{$name})) {
15119: 		$ip = gethostbyname($name);
15120: 		if (!$ip || length($ip) ne 4) {
15121: 		    if (defined($old_name_to_ip{$name})) {
15122: 			$ip = $old_name_to_ip{$name};
15123: 			&logthis("Can't find $name defaulting to old $ip");
15124: 		    } else {
15125: 			&logthis("Name $name no IP found");
15126: 			next;
15127: 		    }
15128: 		} else {
15129: 		    $ip=inet_ntoa($ip);
15130: 		}
15131: 		$name_to_ip{$name} = $ip;
15132: 	    } else {
15133: 		$ip = $name_to_ip{$name};
15134: 	    }
15135: 	    foreach my $id (@{ $name_to_host{$name} }) {
15136: 		$lonid_to_ip{$id} = $ip;
15137: 	    }
15138: 	    push(@{$iphost{$ip}},@{$name_to_host{$name}});
15139: 	}
15140:         unless ($nocache) {
15141: 	    &do_cache_new('iphost','iphost',
15142: 		          [\%iphost,\%name_to_ip,\%lonid_to_ip],
15143: 		          48*60*60);
15144:         }
15145: 
15146: 	return %iphost;
15147:     }
15148: 
15149:     #
15150:     #  Given a DNS returns the loncapa host name for that DNS 
15151:     # 
15152:     sub host_from_dns {
15153:         my ($dns) = @_;
15154:         my @hosts;
15155:         my $ip;
15156: 
15157:         if (exists($name_to_ip{$dns})) {
15158:             $ip = $name_to_ip{$dns};
15159:         }
15160:         if (!$ip) {
15161:             $ip = gethostbyname($dns); # Initial translation to IP is in net order.
15162:             if (length($ip) == 4) { 
15163: 	        $ip   = &IO::Socket::inet_ntoa($ip);
15164:             }
15165:         }
15166:         if ($ip) {
15167: 	    @hosts = get_hosts_from_ip($ip);
15168: 	    return $hosts[0];
15169:         }
15170:         return undef;
15171:     }
15172: 
15173:     sub get_internet_names {
15174:         my ($lonid) = @_;
15175:         return if ($lonid eq '');
15176:         my ($idnref,$cached)=
15177:             &Apache::lonnet::is_cached_new('internetnames',$lonid);
15178:         if ($cached) {
15179:             return $idnref;
15180:         }
15181:         my $ip = &get_host_ip($lonid);
15182:         my @hosts = &get_hosts_from_ip($ip);
15183:         my %iphost = &get_iphost();
15184:         my (@idns,%seen);
15185:         foreach my $id (@hosts) {
15186:             my $dom = &host_domain($id);
15187:             my $prim_id = &domain($dom,'primary');
15188:             my $prim_ip = &get_host_ip($prim_id);
15189:             next if ($seen{$prim_ip});
15190:             if (ref($iphost{$prim_ip}) eq 'ARRAY') {
15191:                 foreach my $id (@{$iphost{$prim_ip}}) {
15192:                     my $intdom = &internet_dom($id);
15193:                     unless (grep(/^\Q$intdom\E$/,@idns)) {
15194:                         push(@idns,$intdom);
15195:                     }
15196:                 }
15197:             }
15198:             $seen{$prim_ip} = 1;
15199:         }
15200:         return &do_cache_new('internetnames',$lonid,\@idns,12*60*60);
15201:     }
15202: 
15203: }
15204: 
15205: sub all_loncaparevs {
15206:     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);
15207: }
15208: 
15209: # ---------------------------------------------------------- Read loncaparev table
15210: {
15211:     sub load_loncaparevs { 
15212:         if (-e "$perlvar{'lonTabDir'}/loncaparevs.tab") {
15213:             if (open(my $config,"<","$perlvar{'lonTabDir'}/loncaparevs.tab")) {
15214:                 while (my $configline=<$config>) {
15215:                     chomp($configline);
15216:                     my ($hostid,$loncaparev)=split(/:/,$configline);
15217:                     $loncaparevs{$hostid}=$loncaparev;
15218:                 }
15219:                 close($config);
15220:             }
15221:         }
15222:     }
15223: }
15224: 
15225: # ---------------------------------------------------------- Read serverhostID table
15226: {
15227:     sub load_serverhomeIDs {
15228:         if (-e "$perlvar{'lonTabDir'}/serverhomeIDs.tab") {
15229:             if (open(my $config,"<","$perlvar{'lonTabDir'}/serverhomeIDs.tab")) {
15230:                 while (my $configline=<$config>) {
15231:                     chomp($configline);
15232:                     my ($name,$id)=split(/:/,$configline);
15233:                     $serverhomeIDs{$name}=$id;
15234:                 }
15235:                 close($config);
15236:             }
15237:         }
15238:     }
15239: }
15240: 
15241: 
15242: BEGIN {
15243: 
15244: # ----------------------------------- Read loncapa.conf and loncapa_apache.conf
15245:     unless ($readit) {
15246: {
15247:     my $configvars = LONCAPA::Configuration::read_conf('loncapa.conf');
15248:     %perlvar = (%perlvar,%{$configvars});
15249: }
15250: 
15251: 
15252: # ------------------------------------------------------ Read spare server file
15253: {
15254:     open(my $config,"<","$perlvar{'lonTabDir'}/spare.tab");
15255: 
15256:     while (my $configline=<$config>) {
15257:        chomp($configline);
15258:        if ($configline) {
15259: 	   my ($host,$type) = split(':',$configline,2);
15260: 	   if (!defined($type) || $type eq '') { $type = 'default' };
15261: 	   push(@{ $spareid{$type} }, $host);
15262:        }
15263:     }
15264:     close($config);
15265: }
15266: # ------------------------------------------------------------ Read permissions
15267: {
15268:     open(my $config,"<","$perlvar{'lonTabDir'}/roles.tab");
15269: 
15270:     while (my $configline=<$config>) {
15271: 	chomp($configline);
15272: 	if ($configline) {
15273: 	    my ($role,$perm)=split(/ /,$configline);
15274: 	    if ($perm ne '') { $pr{$role}=$perm; }
15275: 	}
15276:     }
15277:     close($config);
15278: }
15279: 
15280: # -------------------------------------------- Read plain texts for permissions
15281: {
15282:     open(my $config,"<","$perlvar{'lonTabDir'}/rolesplain.tab");
15283: 
15284:     while (my $configline=<$config>) {
15285: 	chomp($configline);
15286: 	if ($configline) {
15287: 	    my ($short,@plain)=split(/:/,$configline);
15288:             %{$prp{$short}} = ();
15289: 	    if (@plain > 0) {
15290:                 $prp{$short}{'std'} = $plain[0];
15291:                 for (my $i=1; $i<@plain; $i++) {
15292:                     $prp{$short}{'alt'.$i} = $plain[$i];  
15293:                 }
15294:             }
15295: 	}
15296:     }
15297:     close($config);
15298: }
15299: 
15300: # ---------------------------------------------------------- Read package table
15301: {
15302:     open(my $config,"<","$perlvar{'lonTabDir'}/packages.tab");
15303: 
15304:     while (my $configline=<$config>) {
15305: 	if ($configline !~ /\S/ || $configline=~/^#/) { next; }
15306: 	chomp($configline);
15307: 	my ($short,$plain)=split(/:/,$configline);
15308: 	my ($pack,$name)=split(/\&/,$short);
15309: 	if ($plain ne '') {
15310: 	    $packagetab{$pack.'&'.$name.'&name'}=$name; 
15311: 	    $packagetab{$short}=$plain; 
15312: 	}
15313:     }
15314:     close($config);
15315: }
15316: 
15317: # ---------------------------------------------------------- Read loncaparev table
15318: 
15319: &load_loncaparevs();
15320: 
15321: # ---------------------------------------------------------- Read serverhostID table
15322: 
15323: &load_serverhomeIDs();
15324: 
15325: # ---------------------------------------------------------- Read releaseslist XML
15326: {
15327:     my $file = $Apache::lonnet::perlvar{'lonTabDir'}.'/releaseslist.xml';
15328:     if (-e $file) {
15329:         my $parser = HTML::LCParser->new($file);
15330:         while (my $token = $parser->get_token()) {
15331:             if ($token->[0] eq 'S') {
15332:                 my $item = $token->[1];
15333:                 my $name = $token->[2]{'name'};
15334:                 my $value = $token->[2]{'value'};
15335:                 my $valuematch = $token->[2]{'valuematch'};
15336:                 my $namematch = $token->[2]{'namematch'};
15337:                 if ($item eq 'parameter') {
15338:                     if (($namematch ne '') || (($name ne '') && ($value ne '' || $valuematch ne ''))) {
15339:                         my $release = $parser->get_text();
15340:                         $release =~ s/(^\s*|\s*$ )//gx;
15341:                         $needsrelease{$item.':'.$name.':'.$value.':'.$valuematch.':'.$namematch} = $release;
15342:                     }
15343:                 } elsif ($item ne '' && $name ne '') {
15344:                     my $release = $parser->get_text();
15345:                     $release =~ s/(^\s*|\s*$ )//gx;
15346:                     $needsrelease{$item.':'.$name.':'.$value} = $release;
15347:                 }
15348:             }
15349:         }
15350:     }
15351: }
15352: 
15353: # ---------------------------------------------------------- Read managers table
15354: {
15355:     if (-e "$perlvar{'lonTabDir'}/managers.tab") {
15356:         if (open(my $config,"<","$perlvar{'lonTabDir'}/managers.tab")) {
15357:             while (my $configline=<$config>) {
15358:                 chomp($configline);
15359:                 next if ($configline =~ /^\#/);
15360:                 if (($configline =~ /^[\w\-]+$/) || ($configline =~ /^[\w\-]+\:[\w\-]+$/)) {
15361:                     $managerstab{$configline} = 1;
15362:                 }
15363:             }
15364:             close($config);
15365:         }
15366:     }
15367: }
15368: 
15369: # ------------- set up temporary directory
15370: {
15371:     $tmpdir = LONCAPA::tempdir();
15372: 
15373: }
15374: 
15375: # ------------- set default texengine (domain default overrides this)
15376: {
15377:     $deftex = LONCAPA::texengine();
15378: }
15379: 
15380: # ------------- set default minimum length for passwords for internal auth users
15381: {
15382:     $passwdmin = LONCAPA::passwd_min();
15383: }
15384: 
15385: $memcache=new Cache::Memcached({'servers'           => ['127.0.0.1:11211'],
15386: 				'compress_threshold'=> 20_000,
15387:  			        });
15388: 
15389: $processmarker='_'.time.'_'.$perlvar{'lonHostID'};
15390: $dumpcount=0;
15391: $locknum=0;
15392: 
15393: &logtouch();
15394: &logthis('<font color="yellow">INFO: Read configuration</font>');
15395: $readit=1;
15396:     {
15397: 	use integer;
15398: 	my $test=(2**32)+1;
15399: 	if ($test != 0) { $_64bit=1; } else { $_64bit=0; }
15400: 	&logthis(" Detected 64bit platform ($_64bit)");
15401:     }
15402: }
15403: }
15404: 
15405: 1;
15406: __END__
15407: 
15408: =pod
15409: 
15410: =head1 NAME
15411: 
15412: Apache::lonnet - Subroutines to ask questions about things in the network.
15413: 
15414: =head1 SYNOPSIS
15415: 
15416: Invoked by other LON-CAPA modules, when they need to talk to or about objects in the network.
15417: 
15418:  &Apache::lonnet::SUBROUTINENAME(ARGUMENTS);
15419: 
15420: Common parameters:
15421: 
15422: =over 4
15423: 
15424: =item *
15425: 
15426: $uname : an internal username (if $cname expecting a course Id specifically)
15427: 
15428: =item *
15429: 
15430: $udom : a domain (if $cdom expecting a course's domain specifically)
15431: 
15432: =item *
15433: 
15434: $symb : a resource instance identifier
15435: 
15436: =item *
15437: 
15438: $namespace : the name of a .db file that contains the data needed or
15439: being set.
15440: 
15441: =back
15442: 
15443: =head1 OVERVIEW
15444: 
15445: lonnet provides subroutines which interact with the
15446: lonc/lond (TCP) network layer of LON-CAPA. They can be used to ask
15447: about classes, users, and resources.
15448: 
15449: For many of these objects you can also use this to store data about
15450: them or modify them in various ways.
15451: 
15452: =head2 Symbs
15453: 
15454: To identify a specific instance of a resource, LON-CAPA uses symbols
15455: or "symbs"X<symb>. These identifiers are built from the URL of the
15456: map, the resource number of the resource in the map, and the URL of
15457: the resource itself. The latter is somewhat redundant, but might help
15458: if maps change.
15459: 
15460: An example is
15461: 
15462:  msu/korte/parts/part1.sequence___19___msu/korte/tests/part12.problem
15463: 
15464: The respective map entry is
15465: 
15466:  <resource id="19" src="/res/msu/korte/tests/part12.problem"
15467:   title="Problem 2">
15468:  </resource>
15469: 
15470: Symbs are used by the random number generator, as well as to store and
15471: restore data specific to a certain instance of for example a problem.
15472: 
15473: =head2 Storing And Retrieving Data
15474: 
15475: X<store()>X<cstore()>X<restore()>Three of the most important functions
15476: in C<lonnet.pm> are C<&Apache::lonnet::cstore()>,
15477: C<&Apache::lonnet:restore()>, and C<&Apache::lonnet::store()>, which
15478: is is the non-critical message twin of cstore. These functions are for
15479: handlers to store a perl hash to a user's permanent data space in an
15480: easy manner, and to retrieve it again on another call. It is expected
15481: that a handler would use this once at the beginning to retrieve data,
15482: and then again once at the end to send only the new data back.
15483: 
15484: The data is stored in the user's data directory on the user's
15485: homeserver under the ID of the course.
15486: 
15487: The hash that is returned by restore will have all of the previous
15488: value for all of the elements of the hash.
15489: 
15490: Example:
15491: 
15492:  #creating a hash
15493:  my %hash;
15494:  $hash{'foo'}='bar';
15495: 
15496:  #storing it
15497:  &Apache::lonnet::cstore(\%hash);
15498: 
15499:  #changing a value
15500:  $hash{'foo'}='notbar';
15501: 
15502:  #adding a new value
15503:  $hash{'bar'}='foo';
15504:  &Apache::lonnet::cstore(\%hash);
15505: 
15506:  #retrieving the hash
15507:  my %history=&Apache::lonnet::restore();
15508: 
15509:  #print the hash
15510:  foreach my $key (sort(keys(%history))) {
15511:    print("\%history{$key} = $history{$key}");
15512:  }
15513: 
15514: Will print out:
15515: 
15516:  %history{1:foo} = bar
15517:  %history{1:keys} = foo:timestamp
15518:  %history{1:timestamp} = 990455579
15519:  %history{2:bar} = foo
15520:  %history{2:foo} = notbar
15521:  %history{2:keys} = foo:bar:timestamp
15522:  %history{2:timestamp} = 990455580
15523:  %history{bar} = foo
15524:  %history{foo} = notbar
15525:  %history{timestamp} = 990455580
15526:  %history{version} = 2
15527: 
15528: Note that the special hash entries C<keys>, C<version> and
15529: C<timestamp> were added to the hash. C<version> will be equal to the
15530: total number of versions of the data that have been stored. The
15531: C<timestamp> attribute will be the UNIX time the hash was
15532: stored. C<keys> is available in every historical section to list which
15533: keys were added or changed at a specific historical revision of a
15534: hash.
15535: 
15536: B<Warning>: do not store the hash that restore returns directly. This
15537: will cause a mess since it will restore the historical keys as if the
15538: were new keys. I.E. 1:foo will become 1:1:foo etc.
15539: 
15540: Calling convention:
15541: 
15542:  my %record=&Apache::lonnet::restore($symb,$courseid,$domain,$uname);
15543:  &Apache::lonnet::cstore(\%newrecord,$symb,$courseid,$domain,$uname,$laststore);
15544: 
15545: For more detailed information, see lonnet specific documentation.
15546: 
15547: =head1 RETURN MESSAGES
15548: 
15549: =over 4
15550: 
15551: =item * B<con_lost>: unable to contact remote host
15552: 
15553: =item * B<con_delayed>: unable to contact remote host, message will be delivered
15554: when the connection is brought back up
15555: 
15556: =item * B<con_failed>: unable to contact remote host and unable to save message
15557: for later delivery
15558: 
15559: =item * B<error:>: an error a occurred, a description of the error follows the :
15560: 
15561: =item * B<no_such_host>: unable to fund a host associated with the user/domain
15562: that was requested
15563: 
15564: =back
15565: 
15566: =head1 PUBLIC SUBROUTINES
15567: 
15568: =head2 Session Environment Functions
15569: 
15570: =over 4
15571: 
15572: =item * 
15573: X<appenv()>
15574: B<appenv($hashref,$rolesarrayref)>: the value of %{$hashref} is written to
15575: the user envirnoment file, and will be restored for each access this
15576: user makes during this session, also modifies the %env for the current
15577: process. Optional rolesarrayref - if defined contains a reference to an array
15578: of roles which are exempt from the restriction on modifying user.role entries 
15579: in the user's environment.db and in %env.    
15580: 
15581: =item *
15582: X<delenv()>
15583: B<delenv($delthis,$regexp)>: removes all items from the session
15584: environment file that begin with $delthis. If the 
15585: optional second arg - $regexp - is true, $delthis is treated as a 
15586: regular expression, otherwise \Q$delthis\E is used. 
15587: The values are also deleted from the current processes %env.
15588: 
15589: =item * get_env_multiple($name) 
15590: 
15591: gets $name from the %env hash, it seemlessly handles the cases where multiple
15592: values may be defined and end up as an array ref.
15593: 
15594: returns an array of values
15595: 
15596: =back
15597: 
15598: =head2 User Information
15599: 
15600: =over 4
15601: 
15602: =item *
15603: X<queryauthenticate()>
15604: B<queryauthenticate($uname,$udom)>: try to determine user's current 
15605: authentication scheme
15606: 
15607: =item *
15608: X<authenticate()>
15609: B<authenticate($uname,$upass,$udom,$checkdefauth,$clientcancheckhost)>: try to
15610: authenticate user from domain's lib servers (first use the current
15611: one). C<$upass> should be the users password.
15612: $checkdefauth is optional (value is 1 if a check should be made to
15613:    authenticate user using default authentication method, and allow
15614:    account creation if username does not have account in the domain).
15615: $clientcancheckhost is optional (value is 1 if checking whether the
15616:    server can host will occur on the client side in lonauth.pm).   
15617: 
15618: =item *
15619: X<homeserver()>
15620: B<homeserver($uname,$udom)>: find the server which has
15621: the user's directory and files (there must be only one), this caches
15622: the answer, and also caches if there is a borken connection.
15623: 
15624: =item *
15625: X<idget()>
15626: B<idget($udom,$idsref,$namespace)>: find the usernames behind either 
15627: a list of student/employee IDs or clicker IDs
15628: (student/employee IDs are a unique resource in a domain, there must be 
15629: only 1 ID per username, and only 1 username per ID in a specific domain).
15630: clickerIDs are not necessarily unique, as students might share clickers.
15631: (returns hash: id=>name,id=>name)
15632: 
15633: =item *
15634: X<idrget()>
15635: B<idrget($udom,@unames)>: find the IDs behind a list of
15636: usernames (returns hash: name=>id,name=>id)
15637: 
15638: =item *
15639: X<idput()>
15640: B<idput($udom,$idsref,$uhome,$namespace)>: store away a list of 
15641: names and associated student/employee IDs or clicker IDs.
15642: 
15643: =item *
15644: X<iddel()>
15645: B<iddel($udom,$idshashref,$uhome,$namespace)>: delete unwanted 
15646: student/employee ID or clicker ID username look-ups from domain.
15647: The homeserver ($uhome) and namespace ($namespace) are optional.
15648: If no $uhome is provided, it will be determined usig &homeserver()
15649: for each user.  If no $namespace is provided, the default is ids.
15650: 
15651: =item *
15652: X<updateclickers()>
15653: B<updateclickers($udom,$action,$idshashref,$uhome,$critical)>: update 
15654: clicker ID-to-username look-ups in clickers.db on library server.
15655: Permitted actions are add or del (i.e., add or delete). The 
15656: clickers.db contains clickerID as keys (escaped), and each corresponding
15657: value is an escaped comma-separated list of usernames (for whom the
15658: library server is the homeserver), who registered that particular ID.
15659: If $critical is true, the update will be sent via &critical, otherwise
15660: &reply() will be used.
15661: 
15662: =item *
15663: X<rolesinit()>
15664: B<rolesinit($udom,$username)>: get user privileges.
15665: returns user role, first access and timer interval hashes
15666: 
15667: =item *
15668: X<privileged()>
15669: B<privileged($username,$domain)>: returns a true if user has a
15670: privileged and active role (i.e. su or dc), false otherwise.
15671: 
15672: =item *
15673: X<getsection()>
15674: B<getsection($udom,$uname,$cname)>: finds the section of student in the
15675: course $cname, return section name/number or '' for "not in course"
15676: and '-1' for "no section"
15677: 
15678: =item *
15679: X<userenvironment()>
15680: B<userenvironment($udom,$uname,@what)>: gets the values of the keys
15681: passed in @what from the requested user's environment, returns a hash
15682: 
15683: =item * 
15684: X<userlog_query()>
15685: B<userlog_query($uname,$udom,%filters)>: retrieves data from a user's
15686: activity.log file. %filters defines filters applied when parsing the
15687: log file. These can be start or end timestamps, or the type of action
15688: - log to look for Login or Logout events, check for Checkin or
15689: Checkout, role for role selection. The response is in the form
15690: timestamp1:hostid1:event1&timestamp2:hostid2:event2 where events are
15691: escaped strings of the action recorded in the activity.log file.
15692: 
15693: =back
15694: 
15695: =head2 User Roles
15696: 
15697: =over 4
15698: 
15699: =item *
15700: 
15701: allowed($priv,$uri,$symb,$role,$clientip,$noblockcheck) : check for a user privilege; 
15702: returns codes for allowed actions.
15703: 
15704: The first argument is required, all others are optional.
15705: 
15706: $priv is the privilege being checked.
15707: $uri contains additional information about what is being checked for access (e.g.,
15708: URL, course ID etc.). 
15709: $symb is the unique resource instance identifier in a course; if needed,
15710: but not provided, it will be retrieved via a call to &symbread(). 
15711: $role is the role for which a priv is being checked (only used if priv is evb). 
15712: $clientip is the user's IP address (only used when checking for access to portfolio 
15713: files).
15714: $noblockcheck, if true, skips calls to &has_comm_blocking() for the bre priv. This 
15715: prevents recursive calls to &allowed.
15716: 
15717:  F: full access
15718:  U,I,K: authentication modes (cxx only)
15719:  '': forbidden
15720:  1: user needs to choose course
15721:  2: browse allowed
15722:  A: passphrase authentication needed
15723:  B: access temporarily blocked because of a blocking event in a course.
15724:  D: access blocked because access is required via session initiated via deep-link 
15725: 
15726: =item *
15727: 
15728: constructaccess($url,$setpriv) : check for access to construction space URL
15729: 
15730: See if the owner domain and name in the URL match those in the
15731: expected environment.  If so, return three element list
15732: ($ownername,$ownerdomain,$ownerhome).
15733: 
15734: Otherwise return the null string.
15735: 
15736: If second argument 'setpriv' is true, it assigns the privileges,
15737: and returns the same three element list, unless the owner has
15738: blocked "ad hoc" Domain Coordinator access to the Author Space,
15739: in which case the null string is returned.
15740: 
15741: =item *
15742: 
15743: definerole($rolename,$sysrole,$domrole,$courole,$uname,$udom) : define role;
15744: define a custom role rolename set privileges in format of lonTabs/roles.tab
15745: for system, domain, and course level. $uname and $udom are optional (current
15746: user's username and domain will be used when either of $uname or $udom are absent.
15747: 
15748: =item *
15749: 
15750: plaintext($short,$type,$cid,$forcedefault) : return value in %prp hash 
15751: (rolesplain.tab); plain text explanation of a user role term.
15752: $type is Course (default) or Community.
15753: If $forcedefault evaluates to true, text returned will be default 
15754: text for $type. Otherwise, if this is a course, the text returned 
15755: will be a custom name for the role (if defined in the course's 
15756: environment).  If no custom name is defined the default is returned.
15757:    
15758: =item *
15759: 
15760: get_my_roles($uname,$udom,$context,$types,$roles,$roledoms,$withsec,$hidepriv) :
15761: All arguments are optional. Returns a hash of a roles, either for
15762: co-author/assistant author roles for a user's Construction Space
15763: (default), or if $context is 'userroles', roles for the user himself,
15764: In the hash, keys are set to colon-separated $uname,$udom,$role, and
15765: (optionally) if $withsec is true, a fourth colon-separated item - $section.
15766: For each key, value is set to colon-separated start and end times for
15767: the role.  If no username and domain are specified, will default to
15768: current user/domain. Types, roles, and roledoms are references to arrays
15769: of role statuses (active, future or previous), roles 
15770: (e.g., cc,in, st etc.) and domains of the roles which can be used
15771: to restrict the list of roles reported. If no array ref is 
15772: provided for types, will default to return only active roles.
15773: 
15774: =item *
15775: 
15776: in_course($udom,$uname,$cdom,$cnum,$type,$hideprivileged) : determine if
15777: user: $uname:$udom has a role in the course: $cdom_$cnum. 
15778: 
15779: Additional optional arguments are: $type (if role checking is to be restricted 
15780: to certain user status types -- previous (expired roles), active (currently
15781: available roles) or future (roles available in the future), and
15782: $hideprivileged -- if true will not report course roles for users who
15783: have active Domain Coordinator role in course's domain or in additional
15784: domains (specified in 'Domains to check for privileged users' in course
15785: environment -- set via:  Course Settings -> Classlists and staff listing).
15786: 
15787: =item *
15788: 
15789: privileged($username,$domain,$possdomains,$possroles) : returns 1 if user
15790: $username:$domain is a privileged user (e.g., Domain Coordinator or Super User)
15791: $possdomains and $possroles are optional array refs -- to domains to check and
15792: roles to check.  If $possdomains is not specified, a dump will be done of the
15793: users' roles.db to check for a dc or su role in any domain. This can be
15794: time consuming if &privileged is called repeatedly (e.g., when displaying a
15795: classlist), so in such cases, supplying a $possdomains array is preferred, as
15796: this then allows &privileged_by_domain() to be used, which caches the identity
15797: of privileged users, eliminating the need for repeated calls to &dump().
15798: 
15799: =item *
15800: 
15801: privileged_by_domain($possdomains,$roles) : returns a hash of a hash of a hash,
15802: where the outer hash keys are domains specified in the $possdomains array ref,
15803: next inner hash keys are privileged roles specified in the $roles array ref,
15804: and the innermost hash contains key = value pairs for username:domain = end:start
15805: for active or future "privileged" users with that role in that domain. To avoid
15806: repeated dumps of domain roles -- via &get_domain_roles() -- contents of the
15807: innerhash are cached using priv_$role and $dom as the identifiers.
15808: 
15809: =back
15810: 
15811: =head2 User Modification
15812: 
15813: =over 4
15814: 
15815: =item *
15816: 
15817: assignrole($udom,$uname,$url,$role,$end,$start,$deleteflag,$selfenroll,$context) : assign role; give a role to a
15818: user for the level given by URL.  Optional start and end dates (leave empty
15819: string or zero for "no date")
15820: 
15821: =item *
15822: 
15823: changepass($uname,$udom,$currentpass,$newpass,$server) : attempts to
15824: change a users, password, possible return values are: ok,
15825: pwchange_failure, non_authorized, auth_mode_error, unknown_user,
15826: refused
15827: 
15828: =item *
15829: 
15830: modifyuserauth($udom,$uname,$umode,$upass) : modify user authentication
15831: 
15832: =item *
15833: 
15834: modifyuser($udom,$uname,$uid,$umode,$upass,$first,$middle,$last, $gene,
15835:            $forceid,$desiredhome,$email,$inststatus,$candelete) :
15836: 
15837: will update user information (firstname,middlename,lastname,generation,
15838: permanentemail), and if forceid is true, student/employee ID also.
15839: A user's institutional affiliation(s) can also be updated.
15840: User information fields will not be overwritten with empty entries 
15841: unless the field is included in the $candelete array reference.
15842: This array is included when a single user is modified via "Manage Users",
15843: or when Autoupdate.pl is run by cron in a domain.
15844: 
15845: =item *
15846: 
15847: modifystudent
15848: 
15849: modify a student's enrollment and identification information.
15850: The course id is resolved based on the current user's environment.  
15851: This means the invoking user must be a course coordinator or otherwise
15852: associated with a course.
15853: 
15854: This call is essentially a wrapper for lonnet::modifyuser and
15855: lonnet::modify_student_enrollment
15856: 
15857: Inputs: 
15858: 
15859: =over 4
15860: 
15861: =item B<$udom> Student's loncapa domain
15862: 
15863: =item B<$uname> Student's loncapa login name
15864: 
15865: =item B<$uid> Student/Employee ID
15866: 
15867: =item B<$umode> Student's authentication mode
15868: 
15869: =item B<$upass> Student's password
15870: 
15871: =item B<$first> Student's first name
15872: 
15873: =item B<$middle> Student's middle name
15874: 
15875: =item B<$last> Student's last name
15876: 
15877: =item B<$gene> Student's generation
15878: 
15879: =item B<$usec> Student's section in course
15880: 
15881: =item B<$end> Unix time of the roles expiration
15882: 
15883: =item B<$start> Unix time of the roles start date
15884: 
15885: =item B<$forceid> If defined, allow $uid to be changed
15886: 
15887: =item B<$desiredhome> server to use as home server for student
15888: 
15889: =item B<$email> Student's permanent e-mail address
15890: 
15891: =item B<$type> Type of enrollment (auto or manual)
15892: 
15893: =item B<$locktype> boolean - enrollment type locked to prevent Autoenroll.pl changing manual to auto    
15894: 
15895: =item B<$cid> courseID - needed if a course role is assigned by a user whose current role is DC
15896: 
15897: =item B<$selfenroll> boolean - 1 if user role change occurred via self-enrollment
15898: 
15899: =item B<$context> role change context (shown in User Management Logs display in a course)
15900: 
15901: =item B<$inststatus> institutional status of user - : separated string of escaped status types
15902: 
15903: =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.
15904: 
15905: =back
15906: 
15907: =item *
15908: 
15909: modify_student_enrollment
15910: 
15911: Change a student's enrollment status in a class.  The environment variable
15912: 'role.request.course' must be defined for this function to proceed.
15913: 
15914: Inputs:
15915: 
15916: =over 4
15917: 
15918: =item $udom, student's domain
15919: 
15920: =item $uname, student's name
15921: 
15922: =item $uid, student's user id
15923: 
15924: =item $first, student's first name
15925: 
15926: =item $middle
15927: 
15928: =item $last
15929: 
15930: =item $gene
15931: 
15932: =item $usec
15933: 
15934: =item $end
15935: 
15936: =item $start
15937: 
15938: =item $type
15939: 
15940: =item $locktype
15941: 
15942: =item $cid
15943: 
15944: =item $selfenroll
15945: 
15946: =item $context
15947: 
15948: =item $credits, number of credits student will earn from this class
15949: 
15950: =item $instsec, institutional course section code for student
15951: 
15952: =back
15953: 
15954: 
15955: =item *
15956: 
15957: assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start) : assign
15958: custom role; give a custom role to a user for the level given by URL.  Specify
15959: name and domain of role author, and role name
15960: 
15961: =item *
15962: 
15963: revokerole($udom,$uname,$url,$role) : revoke a role for url
15964: 
15965: =item *
15966: 
15967: revokecustomrole($udom,$uname,$url,$role) : revoke a custom role
15968: 
15969: =back
15970: 
15971: =head2 Course Infomation
15972: 
15973: =over 4
15974: 
15975: =item *
15976: 
15977: coursedescription($courseid,$options) : returns a hash of information about the
15978: specified course id, including all environment settings for the
15979: course, the description of the course will be in the hash under the
15980: key 'description'
15981: 
15982: $options is an optional parameter that if supplied is a hash reference that controls
15983: what how this function works.  It has the following key/values:
15984: 
15985: =over 4
15986: 
15987: =item freshen_cache
15988: 
15989: If defined, and the environment cache for the course is valid, it is 
15990: returned in the returned hash.
15991: 
15992: =item one_time
15993: 
15994: If defined, the last cache time is set to _now_
15995: 
15996: =item user
15997: 
15998: If defined, the supplied username is used instead of the current user.
15999: 
16000: 
16001: =back
16002: 
16003: =item *
16004: 
16005: resdata($name,$domain,$type,@which) : request for current parameter
16006: setting for a specific $type, where $type is either 'course' or 'user',
16007: @what should be a list of parameters to ask about. This routine caches
16008: answers for 10 minutes.
16009: 
16010: =item *
16011: 
16012: get_courseresdata($courseid, $domain) : dump the entire course resource
16013: data base, returning a hash that is keyed by the resource name and has
16014: values that are the resource value.  I believe that the timestamps and
16015: versions are also returned.
16016: 
16017: get_numsuppfiles($cnum,$cdom) : retrieve number of files in a course's
16018: supplemental content area. This routine caches the number of files for 
16019: 10 minutes.
16020: 
16021: =back
16022: 
16023: =head2 Course Modification
16024: 
16025: =over 4
16026: 
16027: =item *
16028: 
16029: writecoursepref($courseid,%prefs) : write preferences (environment
16030: database) for a course
16031: 
16032: =item *
16033: 
16034: createcourse($udom,$description,$url,$course_server,$nonstandard,$inst_code,$course_owner,$crstype,$cnum) : make course
16035: 
16036: =item *
16037: 
16038: generate_coursenum($udom,$crstype) : get a unique (unused) course number in domain $udom for course type $crstype (Course or Community).
16039: 
16040: =item *
16041: 
16042: is_course($courseid), is_course($cdom, $cnum)
16043: 
16044: Accepts either a combined $courseid (in the form of domain_courseid) or the
16045: two component version $cdom, $cnum. It checks if the specified course exists.
16046: 
16047: Returns:
16048:     undef if the course doesn't exist, otherwise
16049:     in scalar context the combined courseid.
16050:     in list context the two components of the course identifier, domain and 
16051:     courseid.    
16052: 
16053: =back
16054: 
16055: =head2 Bubblesheet Configuration
16056: 
16057: =over 4
16058: 
16059: =item *
16060: 
16061: get_scantron_config($which)
16062: 
16063: $which - the name of the configuration to parse from the file.
16064: 
16065: Parses and returns the bubblesheet configuration line selected as a
16066: hash of configuration file fields.
16067: 
16068: 
16069: Returns:
16070:     If the named configuration is not in the file, an empty
16071:     hash is returned.
16072: 
16073:     a hash with the fields
16074:       name         - internal name for the this configuration setup
16075:       description  - text to display to operator that describes this config
16076:       CODElocation - if 0 or the string 'none'
16077:                           - no CODE exists for this config
16078:                      if -1 || the string 'letter'
16079:                           - a CODE exists for this config and is
16080:                             a string of letters
16081:                      Unsupported value (but planned for future support)
16082:                           if a positive integer
16083:                                - The CODE exists as the first n items from
16084:                                  the question section of the form
16085:                           if the string 'number'
16086:                                - The CODE exists for this config and is
16087:                                  a string of numbers
16088:       CODEstart   - (only matter if a CODE exists) column in the line where
16089:                      the CODE starts
16090:       CODElength  - length of the CODE
16091:       IDstart     - column where the student/employee ID starts
16092:       IDlength    - length of the student/employee ID info
16093:       Qstart      - column where the information from the bubbled
16094:                     'questions' start
16095:       Qlength     - number of columns comprising a single bubble line from
16096:                     the sheet. (usually either 1 or 10)
16097:       Qon         - either a single character representing the character used
16098:                     to signal a bubble was chosen in the positional setup, or
16099:                     the string 'letter' if the letter of the chosen bubble is
16100:                     in the final, or 'number' if a number representing the
16101:                     chosen bubble is in the file (1->A 0->J)
16102:       Qoff        - the character used to represent that a bubble was
16103:                     left blank
16104:       PaperID     - if the scanning process generates a unique number for each
16105:                     sheet scanned the column that this ID number starts in
16106:       PaperIDlength - number of columns that comprise the unique ID number
16107:                       for the sheet of paper
16108:       FirstName   - column that the first name starts in
16109:       FirstNameLength - number of columns that the first name spans
16110:       LastName    - column that the last name starts in
16111:       LastNameLength - number of columns that the last name spans
16112:       BubblesPerRow - number of bubbles available in each row used to
16113:                       bubble an answer. (If not specified, 10 assumed).
16114: 
16115: 
16116: =item *
16117: 
16118: get_scantronformat_file($cdom)
16119: 
16120: $cdom - the course's domain (optional); if not supplied, uses
16121: domain for current $env{'request.course.id'}.
16122: 
16123: Returns an array containing lines from the scantron format file for
16124: the domain of the course.
16125: 
16126: If a url for a custom.tab file is listed in domain's configuration.db,
16127: lines are from this file.
16128: 
16129: Otherwise, if a default.tab has been published in RES space by the
16130: domainconfig user, lines are from this file.
16131: 
16132: Otherwise, fall back to getting lines from the legacy file on the
16133: local server:  /home/httpd/lonTabs/default_scantronformat.tab
16134: 
16135: =back
16136: 
16137: =head2 Resource Subroutines
16138: 
16139: =over 4
16140: 
16141: =item *
16142: 
16143: subscribe($fname) : subscribe to a resource, returns URL if possible (probably should use repcopy instead)
16144: 
16145: =item *
16146: 
16147: repcopy($filename) : subscribes to the requested file, and attempts to
16148: replicate from the owning library server, Might return
16149: 'unavailable', 'not_found', 'forbidden', 'ok', or
16150: 'bad_request', also attempts to grab the metadata for the
16151: resource. Expects the local filesystem pathname
16152: (/home/httpd/html/res/....)
16153: 
16154: =back
16155: 
16156: =head2 Resource Information
16157: 
16158: =over 4
16159: 
16160: =item *
16161: 
16162: EXT($varname,$symb,$udom,$uname,$usection,$recurse,$cid) : evaluates 
16163: and returns the value of a variety of different possible values,
16164: $varname should be a request string, and the other parameters can be
16165: used to specify who and what one is asking about. Ordinarily, $cid 
16166: does not need to be specified, as it is retrived from 
16167: $env{'request.course.id'}, but &Apache::lonnet::EXT() is called
16168: within lonuserstate::loadmap() when initializing a course, before
16169: $env{'request.course.id'} has been set, so it needs to be provided
16170: in that one case.
16171: 
16172: Possible values for $varname are environment.lastname (or other item
16173: from the envirnment hash), user.name (or someother aspect about the
16174: user), resource.0.maxtries (or some other part and parameter of a
16175: resource)
16176: 
16177: =item *
16178: 
16179: directcondval($number) : get current value of a condition; reads from a state
16180: string
16181: 
16182: =item *
16183: 
16184: condval($condidx) : value of condition index based on state
16185: 
16186: =item *
16187: 
16188: metadata($uri,$what,$toolsymb,$liburi,$prefix,$depthcount) : request a
16189: resource's metadata, $what should be either a specific key, or either
16190: 'keys' (to get a list of possible keys) or 'packages' to get a list of
16191: packages that this resource currently uses, the last 3 arguments are 
16192: only used internally for recursive metadata.
16193: 
16194: the toolsymb is only used where the uri is for an external tool (for which
16195: the uri as well as the symb are guaranteed to be unique).
16196: 
16197: this function automatically caches all requests except any made recursively
16198: to retrieve a list of metadata keys for an imported library file ($liburi is 
16199: defined).
16200: 
16201: =item *
16202: 
16203: metadata_query($query,$custom,$customshow) : make a metadata query against the
16204: network of library servers; returns file handle of where SQL and regex results
16205: will be stored for query
16206: 
16207: =item *
16208: 
16209: symbread($filename,$donotrecurse,$ignorecachednull,$checkforblock,$possibles) : 
16210: return symbolic list entry (all arguments optional). 
16211: 
16212: Args: filename is the filename (including path) for the file for which a symb 
16213: is required; donotrecurse, if true will prevent calls to allowed() being made 
16214: to check access status if more than one resource was found in the bighash 
16215: (see rev. 1.249) to avoid an infinite loop if an ambiguous resource is part of 
16216: a randompick); ignorecachednull, if true will prevent a symb of '' being 
16217: returned if $env{$cache_str} is defined as ''; checkforblock if true will
16218: cause possible symbs to be checked to determine if they are subject to content
16219: blocking, if so they will not be included as possible symbs; possibles is a
16220: ref to a hash, which, as a side effect, will be populated with all possible 
16221: symbs (content blocking not tested).
16222:  
16223: returns the data handle
16224: 
16225: =item *
16226: 
16227: symbverify($symb,$thisfn,$encstate) : verifies that $symb actually exists
16228: and is a possible symb for the URL in $thisfn, and if is an encrypted
16229: resource that the user accessed using /enc/ returns a 1 on success, 0
16230: on failure, user must be in a course, as it assumes the existence of
16231: the course initial hash, and uses $env('request.course.id'}.  The third
16232: arg is an optional reference to a scalar.  If this arg is passed in the 
16233: call to symbverify, it will be set to 1 if the symb has been set to be 
16234: encrypted; otherwise it will be null.  
16235: 
16236: =item *
16237: 
16238: symbclean($symb) : removes versions numbers from a symb, returns the
16239: cleaned symb
16240: 
16241: =item *
16242: 
16243: is_on_map($uri) : checks if the $uri is somewhere on the current
16244: course map, user must be in a course for it to work.
16245: 
16246: =item *
16247: 
16248: numval($salt) : return random seed value (addend for rndseed)
16249: 
16250: =item *
16251: 
16252: rndseed($symb,$courseid,$udom,$uname) : create a random sum; returns
16253: a random seed, all arguments are optional, if they aren't sent it uses the
16254: environment to derive them. Note: if symb isn't sent and it can't get one
16255: from &symbread it will use the current time as its return value
16256: 
16257: =item *
16258: 
16259: ireceipt($funame,$fudom,$fucourseid,$fusymb) : return unique,
16260: unfakeable, receipt
16261: 
16262: =item *
16263: 
16264: receipt() : API to ireceipt working off of env values; given out to users
16265: 
16266: =item *
16267: 
16268: countacc($url) : count the number of accesses to a given URL
16269: 
16270: =item *
16271: 
16272: 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
16273: 
16274: =item *
16275: 
16276: 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)
16277: 
16278: =item *
16279: 
16280: expirespread($uname,$udom,$stype,$usymb) : set expire date for spreadsheet
16281: 
16282: =item *
16283: 
16284: devalidate($symb) : devalidate temporary spreadsheet calculations,
16285: forcing spreadsheet to reevaluate the resource scores next time.
16286: 
16287: =item * 
16288: 
16289: can_edit_resource($file,$cnum,$cdom,$resurl,$symb,$group) : determine if current user can edit a particular resource,
16290: when viewing in course context.
16291: 
16292:  input: six args -- filename (decluttered), course number, course domain,
16293:                     url, symb (if registered) and group (if this is a 
16294:                     group item -- e.g., bulletin board, group page etc.).
16295: 
16296:  output: array of five scalars --
16297:          $cfile -- url for file editing if editable on current server
16298:          $home -- homeserver of resource (i.e., for author if published,
16299:                                           or course if uploaded.).
16300:          $switchserver --  1 if server switch will be needed.
16301:          $forceedit -- 1 if icon/link should be to go to edit mode 
16302:          $forceview -- 1 if icon/link should be to go to view mode
16303: 
16304: =item *
16305: 
16306: is_course_upload($file,$cnum,$cdom)
16307: 
16308: Used in course context to determine if current file was uploaded to 
16309: the course (i.e., would be found in /userfiles/docs on the course's 
16310: homeserver.
16311: 
16312:   input: 3 args -- filename (decluttered), course number and course domain.
16313:   output: boolean -- 1 if file was uploaded.
16314: 
16315: =back
16316: 
16317: =head2 Storing/Retreiving Data
16318: 
16319: =over 4
16320: 
16321: =item *
16322: 
16323: store($storehash,$symb,$namespace,$udom,$uname,$laststore) : stores hash
16324: permanently for this url; hashref needs to be given and should be a \%hashname;
16325: the remaining args aren't required and if they aren't passed or are '' they will
16326: be derived from the env (with the exception of $laststore, which is an 
16327: optional arg used when a user's submission is stored in grading).
16328: $laststore is $version=$timestamp, where $version is the most recent version
16329: number retrieved for the corresponding $symb in the $namespace db file, and
16330: $timestamp is the timestamp for that transaction (UNIX time).
16331: $laststore is currently only passed when cstore() is called by 
16332: structuretags::finalize_storage().
16333: 
16334: =item *
16335: 
16336: cstore($storehash,$symb,$namespace,$udom,$uname,$laststore) : same as store
16337: but uses critical subroutine
16338: 
16339: =item *
16340: 
16341: restore($symb,$namespace,$udom,$uname) : returns hash for this symb;
16342: all args are optional
16343: 
16344: =item *
16345: 
16346: dumpstore($namespace,$udom,$uname,$regexp,$range) : 
16347: dumps the complete (or key matching regexp) namespace into a hash
16348: ($udom, $uname, $regexp, $range are optional) for a namespace that is
16349: normally &store()ed into
16350: 
16351: $range should be either an integer '100' (give me the first 100
16352:                                            matching records)
16353:               or be  two integers sperated by a - with no spaces
16354:                  '30-50' (give me the 30th through the 50th matching
16355:                           records)
16356: 
16357: 
16358: =item *
16359: 
16360: putstore($namespace,$symb,$version,$storehash,$udomain,$uname,$tolog) :
16361: replaces a &store() version of data with a replacement set of data
16362: for a particular resource in a namespace passed in the $storehash hash 
16363: reference. If $tolog is true, the transaction is logged in the courselog
16364: with an action=PUTSTORE.
16365: 
16366: =item *
16367: 
16368: tmpstore($storehash,$symb,$namespace,$udom,$uname) : storage that
16369: works very similar to store/cstore, but all data is stored in a
16370: temporary location and can be reset using tmpreset, $storehash should
16371: be a hash reference, returns nothing on success
16372: 
16373: =item *
16374: 
16375: tmprestore($symb,$namespace,$udom,$uname) : storage that works very
16376: similar to restore, but all data is stored in a temporary location and
16377: can be reset using tmpreset. Returns a hash of values on success,
16378: error string otherwise.
16379: 
16380: =item *
16381: 
16382: tmpreset($symb,$namespace,$udom,$uname) : temporary storage reset,
16383: deltes all keys for $symb form the temporary storage hash.
16384: 
16385: =item *
16386: 
16387: get($namespace,$storearr,$udom,$uname) : returns hash with keys from array
16388: reference filled in from namesp ($udom and $uname are optional)
16389: 
16390: =item *
16391: 
16392: del($namespace,$storearr,$udom,$uname) : deletes keys out of array from
16393: namesp ($udom and $uname are optional)
16394: 
16395: =item *
16396: 
16397: dump($namespace,$udom,$uname,$regexp,$range) : 
16398: dumps the complete (or key matching regexp) namespace into a hash
16399: ($udom, $uname, $regexp, $range are optional)
16400: 
16401: $range should be either an integer '100' (give me the first 100
16402:                                            matching records)
16403:               or be  two integers sperated by a - with no spaces
16404:                  '30-50' (give me the 30th through the 50th matching
16405:                           records)
16406: =item *
16407: 
16408: inc($namespace,$store,$udom,$uname) : increments $store in $namespace.
16409: $store can be a scalar, an array reference, or if the amount to be 
16410: incremented is > 1, a hash reference.
16411: 
16412: ($udom and $uname are optional)
16413: 
16414: =item *
16415: 
16416: put($namespace,$storehash,$udom,$uname) : stores hash in namesp
16417: ($udom and $uname are optional)
16418: 
16419: =item *
16420: 
16421: cput($namespace,$storehash,$udom,$uname) : critical put
16422: ($udom and $uname are optional)
16423: 
16424: =item *
16425: 
16426: newput($namespace,$storehash,$udom,$uname) :
16427: 
16428: Attempts to store the items in the $storehash, but only if they don't
16429: currently exist, if this succeeds you can be certain that you have 
16430: successfully created a new key value pair in the $namespace db.
16431: 
16432: 
16433: Args:
16434:  $namespace: name of database to store values to
16435:  $storehash: hashref to store to the db
16436:  $udom: (optional) domain of user containing the db
16437:  $uname: (optional) name of user caontaining the db
16438: 
16439: Returns:
16440:  'ok' -> succeeded in storing all keys of $storehash
16441:  'key_exists: <key>' -> failed to anything out of $storehash, as at
16442:                         least <key> already existed in the db (other
16443:                         requested keys may also already exist)
16444:  'error: <msg>' -> unable to tie the DB or other error occurred
16445:  'con_lost' -> unable to contact request server
16446:  'refused' -> action was not allowed by remote machine
16447: 
16448: 
16449: =item *
16450: 
16451: eget($namespace,$storearr,$udom,$uname) : returns hash with keys from array
16452: reference filled in from namesp (encrypts the return communication)
16453: ($udom and $uname are optional)
16454: 
16455: =item *
16456: 
16457: log($udom,$name,$home,$message) : write to permanent log for user; use
16458: critical subroutine
16459: 
16460: =item *
16461: 
16462: get_dom($namespace,$storearr,$udom,$uhome) : returns hash with keys from
16463: array reference filled in from namespace found in domain level on either
16464: specified domain server ($uhome) or primary domain server ($udom and $uhome are optional).
16465: 
16466: =item *
16467: 
16468: put_dom($namespace,$storehash,$udom,$uhome) :  stores hash in namespace at 
16469: domain level either on specified domain server ($uhome) or primary domain 
16470: server ($udom and $uhome are optional)
16471: 
16472: =item * 
16473: 
16474: get_domain_defaults($target_domain,$ignore_cache) : returns hash with defaults 
16475: for: authentication, language, quotas, timezone, date locale, and portal URL in
16476: the target domain.
16477: 
16478: May also include additional key => value pairs for the following groups:
16479: 
16480: =over
16481: 
16482: =item
16483: disk quotas (MB allocated by default to portfolios and authoring spaces).
16484: 
16485: =over
16486: 
16487: =item defaultquota, authorquota
16488: 
16489: =back
16490: 
16491: =item
16492: tools (availability of aboutme page, blog, webDAV access for authoring spaces,
16493: portfolio for users).
16494: 
16495: =over
16496: 
16497: =item
16498: aboutme, blog, webdav, portfolio
16499: 
16500: =back
16501: 
16502: =item
16503: requestcourses: ability to request courses, and how requests are processed.
16504: 
16505: =over
16506: 
16507: =item
16508: official, unofficial, community, textbook, placement
16509: 
16510: =back
16511: 
16512: =item
16513: inststatus: types of institutional affiliation, and order in which they are displayed.
16514: 
16515: =over
16516: 
16517: =item
16518: inststatustypes, inststatusorder, inststatusguest
16519: 
16520: =back
16521: 
16522: =item
16523: coursedefaults: can PDF forms can be created, default credits for courses, default quotas (MB)
16524: for course's uploaded content.
16525: 
16526: =over
16527: 
16528: =item
16529: canuse_pdfforms, officialcredits, unofficialcredits, textbookcredits, officialquota, unofficialquota, 
16530: communityquota, textbookquota, placementquota
16531: 
16532: =back
16533: 
16534: =item
16535: usersessions: set options for hosting of your users in other domains, and hosting of users from other domains
16536: on your servers.
16537: 
16538: =over
16539: 
16540: =item 
16541: remotesessions, hostedsessions
16542: 
16543: =back
16544: 
16545: =back
16546: 
16547: In cases where a domain coordinator has never used the "Set Domain Configuration"
16548: utility to create a configuration.db file on a domain's primary library server 
16549: only the following domain defaults: auth_def, auth_arg_def, lang_def
16550: -- corresponding values are authentication type (internal, krb4, krb5,
16551: or localauth), initial password or a kerberos realm, language (e.g., en-us) -- 
16552: will be available. Values are retrieved from cache (if current), unless the
16553: optional $ignore_cache arg is true, or from domain's configuration.db (if available),
16554: or lastly from values in lonTabs/dns_domain,tab, or lonTabs/domain.tab.
16555: 
16556: Typical usage:
16557: 
16558: %domdefaults = &get_domain_defaults($target_domain);
16559: 
16560: =back
16561: 
16562: =head2 Network Status Functions
16563: 
16564: =over 4
16565: 
16566: =item *
16567: 
16568: dirlist() : return directory list based on URI (first arg).
16569: 
16570: Inputs: 1 required, 5 optional.
16571: 
16572: =over
16573: 
16574: =item 
16575: $uri - path to file in filesystem (starts: /res or /userfiles/). Required.
16576: 
16577: =item
16578: $userdomain - domain of user/course to be listed. Extracted from $uri if absent. 
16579: 
16580: =item
16581: $username -  username of user/course to be listed. Extracted from $uri if absent. 
16582: 
16583: =item
16584: $getpropath - boolean: 1 if prepend path using &propath(). 
16585: 
16586: =item
16587: $getuserdir - boolean: 1 if prepend path for "userfiles".
16588: 
16589: =item 
16590: $alternateRoot - path to prepend in place of path from $uri.
16591: 
16592: =back
16593: 
16594: Returns: Array of up to two items.
16595: 
16596: =over
16597: 
16598: a reference to an array of files/subdirectories
16599: 
16600: =over
16601: 
16602: Each element in the array of files/subdirectories is a & separated list of
16603: item name and the result of running stat on the item.  If dirlist was requested
16604: for a file instead of a directory, the item name will be ''. For a directory 
16605: listing, if the item is a metadata file, the element will end &N&M 
16606: (where N amd M are either 0 or 1, corresponding to obsolete set (1), or
16607: default copyright set (1).  
16608: 
16609: =back
16610: 
16611: a scalar containing error condition (if encountered).
16612: 
16613: =over
16614: 
16615: =item 
16616: no_host (no homeserver identified for $username:$domain).
16617: 
16618: =item 
16619: no_such_host (server contacted for listing not identified as valid host).
16620: 
16621: =item 
16622: con_lost (connection to remote server failed).
16623: 
16624: =item 
16625: refused (invalid $username:$domain received on lond side).
16626: 
16627: =item 
16628: no_such_dir (directory at specified path on lond side does not exist). 
16629: 
16630: =item 
16631: empty (directory at specified path on lond side is empty).
16632: 
16633: =over
16634: 
16635: This is currently not encountered because the &ls3, &ls2, 
16636: &ls (_handler) routines on the lond side do not filter out
16637: . and .. from a directory listing. 
16638: 
16639: =back
16640: 
16641: =back
16642: 
16643: =back
16644: 
16645: =item *
16646: 
16647: spareserver() : find server with least workload from spare.tab
16648: 
16649: 
16650: =item *
16651: 
16652: host_from_dns($dns) : Returns the loncapa hostname corresponding to a DNS name or undef
16653: if there is no corresponding loncapa host.
16654: 
16655: =back
16656: 
16657: 
16658: =head2 Apache Request
16659: 
16660: =over 4
16661: 
16662: =item *
16663: 
16664: ssi($url,%hash) : server side include, does a complete request cycle on url to
16665: localhost, posts hash
16666: 
16667: =back
16668: 
16669: =head2 Data to String to Data
16670: 
16671: =over 4
16672: 
16673: =item *
16674: 
16675: hash2str(%hash) : convert a hash into a string complete with escaping and '='
16676: and '&' separators, supports elements that are arrayrefs and hashrefs
16677: 
16678: =item *
16679: 
16680: hashref2str($hashref) : convert a hashref into a string complete with
16681: escaping and '=' and '&' separators, supports elements that are
16682: arrayrefs and hashrefs
16683: 
16684: =item *
16685: 
16686: arrayref2str($arrayref) : convert an arrayref into a string complete
16687: with escaping and '&' separators, supports elements that are arrayrefs
16688: and hashrefs
16689: 
16690: =item *
16691: 
16692: str2hash($string) : convert string to hash using unescaping and
16693: splitting on '=' and '&', supports elements that are arrayrefs and
16694: hashrefs
16695: 
16696: =item *
16697: 
16698: str2array($string) : convert string to hash using unescaping and
16699: splitting on '&', supports elements that are arrayrefs and hashrefs
16700: 
16701: =back
16702: 
16703: =head2 Logging Routines
16704: 
16705: 
16706: These routines allow one to make log messages in the lonnet.log and
16707: lonnet.perm logfiles.
16708: 
16709: =over 4
16710: 
16711: =item *
16712: 
16713: logtouch() : make sure the logfile, lonnet.log, exists
16714: 
16715: =item *
16716: 
16717: logthis() : append message to the normal lonnet.log file, it gets
16718: preiodically rolled over and deleted.
16719: 
16720: =item *
16721: 
16722: logperm() : append a permanent message to lonnet.perm.log, this log
16723: file never gets deleted by any automated portion of the system, only
16724: messages of critical importance should go in here.
16725: 
16726: 
16727: =back
16728: 
16729: =head2 General File Helper Routines
16730: 
16731: =over 4
16732: 
16733: =item *
16734: 
16735: getfile($file,$caller) : two cases - requests for files in /res or in /uploaded.
16736: (a) files in /uploaded
16737:   (i) If a local copy of the file exists - 
16738:       compares modification date of local copy with last-modified date for 
16739:       definitive version stored on home server for course. If local copy is 
16740:       stale, requests a new version from the home server and stores it. 
16741:       If the original has been removed from the home server, then local copy 
16742:       is unlinked.
16743:   (ii) If local copy does not exist -
16744:       requests the file from the home server and stores it. 
16745:   
16746:   If $caller is 'uploadrep':  
16747:     This indicates a call from lonuploadrep.pm (PerlHeaderParserHandler phase)
16748:     for request for files originally uploaded via DOCS. 
16749:      - returns 'ok' if fresh local copy now available, -1 otherwise.
16750:   
16751:   Otherwise:
16752:      This indicates a call from the content generation phase of the request.
16753:      -  returns the entire contents of the file or -1.
16754:      
16755: (b) files in /res
16756:    - returns the entire contents of a file or -1; 
16757:    it properly subscribes to and replicates the file if neccessary.
16758: 
16759: 
16760: =item *
16761: 
16762: stat_file($url) : $url is expected to be a /res/ or /uploaded/ style file
16763:                   reference
16764: 
16765: returns either a stat() list of data about the file or an empty list
16766: if the file doesn't exist or couldn't find out about it (connection
16767: problems or user unknown)
16768: 
16769: =item *
16770: 
16771: filelocation($dir,$file) : returns file system location of a file
16772: based on URI; meant to be "fairly clean" absolute reference, $dir is a
16773: directory that relative $file lookups are to looked in ($dir of /a/dir
16774: and a file of ../bob will become /a/bob)
16775: 
16776: =item *
16777: 
16778: hreflocation($dir,$file) : returns file system location or a URL; same as
16779: filelocation except for hrefs
16780: 
16781: =item *
16782: 
16783: declutter() : declutters URLs -- remove beginning slashes, 'res' etc.
16784: also removes beginning /home/httpd/html unless /priv/ follows it.
16785: 
16786: =back
16787: 
16788: =head2 Usererfile file routines (/uploaded*)
16789: 
16790: =over 4
16791: 
16792: =item *
16793: 
16794: userfileupload(): main rotine for putting a file in a user or course's
16795:                   filespace, arguments are,
16796: 
16797:  formname - required - this is the name of the element in $env where the
16798:            filename, and the contents of the file to create/modifed exist
16799:            the filename is in $env{'form.'.$formname.'.filename'} and the
16800:            contents of the file is located in $env{'form.'.$formname}
16801:  context - if coursedoc, store the file in the course of the active role
16802:              of the current user; 
16803:            if 'existingfile': store in 'overwrites' in /home/httpd/perl/tmp
16804:            if 'canceloverwrite': delete file in tmp/overwrites directory
16805:  subdir - required - subdirectory to put the file in under ../userfiles/
16806:          if undefined, it will be placed in "unknown"
16807: 
16808:  (This routine calls clean_filename() to remove any dangerous
16809:  characters from the filename, and then calls finuserfileupload() to
16810:  complete the transaction)
16811: 
16812:  returns either the url of the uploaded file (/uploaded/....) if successful
16813:  and /adm/notfound.html if unsuccessful
16814: 
16815: =item *
16816: 
16817: clean_filename(): routine for cleaing a filename up for storage in
16818:                  userfile space, argument is:
16819: 
16820:  filename - proposed filename
16821: 
16822: returns: the new clean filename
16823: 
16824: =item *
16825: 
16826: finishuserfileupload(): routine that creates and sends the file to
16827: userspace, probably shouldn't be called directly
16828: 
16829:   docuname: username or courseid of destination for the file
16830:   docudom: domain of user/course of destination for the file
16831:   formname: same as for userfileupload()
16832:   fname: filename (including subdirectories) for the file
16833:   parser: if 'parse', will parse (html) file to extract references to objects, links etc.
16834:           if hashref, and context is scantron, will convert csv format to standard format
16835:   allfiles: reference to hash used to store objects found by parser
16836:   codebase: reference to hash used for codebases of java objects found by parser
16837:   thumbwidth: width (pixels) of thumbnail to be created for uploaded image
16838:   thumbheight: height (pixels) of thumbnail to be created for uploaded image
16839:   resizewidth: width to be used to resize image using resizeImage from ImageMagick
16840:   resizeheight: height to be used to resize image using resizeImage from ImageMagick
16841:   context: if 'overwrite', will move the uploaded file from its temporary location to
16842:             userfiles to facilitate overwriting a previously uploaded file with same name.
16843:   mimetype: reference to scalar to accommodate mime type determined
16844:             from File::MMagic if $parser = parse.
16845: 
16846:  returns either the url of the uploaded file (/uploaded/....) if successful
16847:  and /adm/notfound.html if unsuccessful (or an error message if context 
16848:  was 'overwrite').
16849:  
16850: 
16851: =item *
16852: 
16853: renameuserfile(): renames an existing userfile to a new name
16854: 
16855:   Args:
16856:    docuname: username or courseid of destination for the file
16857:    docudom: domain of user/course of destination for the file
16858:    old: current file name (including any subdirs under userfiles)
16859:    new: desired file name (including any subdirs under userfiles)
16860: 
16861: =item *
16862: 
16863: mkdiruserfile(): creates a directory is a userfiles dir
16864: 
16865:   Args:
16866:    docuname: username or courseid of destination for the file
16867:    docudom: domain of user/course of destination for the file
16868:    dir: dir to create (including any subdirs under userfiles)
16869: 
16870: =item *
16871: 
16872: removeuserfile(): removes a file that exists in userfiles
16873: 
16874:   Args:
16875:    docuname: username or courseid of destination for the file
16876:    docudom: domain of user/course of destination for the file
16877:    fname: filname to delete (including any subdirs under userfiles)
16878: 
16879: =item *
16880: 
16881: removeuploadedurl(): convience function for removeuserfile()
16882: 
16883:   Args:
16884:    url:  a full /uploaded/... url to delete
16885: 
16886: =item * 
16887: 
16888: get_portfile_permissions():
16889:   Args:
16890:     domain: domain of user or course contain the portfolio files
16891:     user: name of user or num of course contain the portfolio files
16892:   Returns:
16893:     hashref of a dump of the proper file_permissions.db
16894:    
16895: 
16896: =item * 
16897: 
16898: get_access_controls():
16899: 
16900: Args:
16901:   current_permissions: the hash ref returned from get_portfile_permissions()
16902:   group: (optional) the group you want the files associated with
16903:   file: (optional) the file you want access info on
16904: 
16905: Returns:
16906:     a hash (keys are file names) of hashes containing
16907:         keys are: path to file/file_name\0uniqueID:scope_end_start (see below)
16908:         values are XML containing access control settings (see below) 
16909: 
16910: Internal notes:
16911: 
16912:  access controls are stored in file_permissions.db as key=value pairs.
16913:     key -> path to file/file_name\0uniqueID:scope_end_start
16914:         where scope -> public,guest,course,group,domains or users.
16915:               end -> UNIX time for end of access (0 -> no end date)
16916:               start -> UNIX time for start of access
16917: 
16918:     value -> XML description of access control
16919:            <scope type=""> (type =1 of: public,guest,course,group,domains,users">
16920:             <start></start>
16921:             <end></end>
16922: 
16923:             <password></password>  for scope type = guest
16924: 
16925:             <domain></domain>     for scope type = course or group
16926:             <number></number>
16927:             <roles id="">
16928:              <role></role>
16929:              <access></access>
16930:              <section></section>
16931:              <group></group>
16932:             </roles>
16933: 
16934:             <dom></dom>         for scope type = domains
16935: 
16936:             <users>             for scope type = users
16937:              <user>
16938:               <uname></uname>
16939:               <udom></udom>
16940:              </user>
16941:             </users>
16942:            </scope> 
16943:               
16944:  Access data is also aggregated for each file in an additional key=value pair:
16945:  key -> path to file/file_name\0accesscontrol 
16946:  value -> reference to hash
16947:           hash contains key = value pairs
16948:           where key = uniqueID:scope_end_start
16949:                 value = UNIX time record was last updated
16950: 
16951:           Used to improve speed of look-ups of access controls for each file.  
16952:  
16953:  Locks on files (resulting from submission of portfolio file to a homework problem stored in array of arrays.
16954: 
16955: =item *
16956: 
16957: modify_access_controls():
16958: 
16959: Modifies access controls for a portfolio file
16960: Args
16961: 1. file name
16962: 2. reference to hash of required changes,
16963: 3. domain
16964: 4. username
16965:   where domain,username are the domain of the portfolio owner 
16966:   (either a user or a course) 
16967: 
16968: Returns:
16969: 1. result of additions or updates ('ok' or 'error', with error message). 
16970: 2. result of deletions ('ok' or 'error', with error message).
16971: 3. reference to hash of any new or updated access controls.
16972: 4. reference to hash used to map incoming IDs to uniqueIDs assigned to control.
16973:    key = integer (inbound ID)
16974:    value = uniqueID
16975: 
16976: =item *
16977: 
16978: get_timebased_id():
16979: 
16980: Attempts to get a unique timestamp-based suffix for use with items added to a 
16981: course via the Course Editor (e.g., folders, composite pages, 
16982: group bulletin boards).
16983: 
16984: Args: (first three required; six others optional)
16985: 
16986: 1. prefix (alphanumeric): of keys in hash, e.g., suppsequence, docspage,
16987:    docssequence, or name of group
16988: 
16989: 2. keyid (alphanumeric): name of temporary locking key in hash,
16990:    e.g., num, boardids
16991: 
16992: 3. namespace: name of gdbm file used to store suffixes already assigned;  
16993:    file will be named nohist_namespace.db
16994: 
16995: 4. cdom: domain of course; default is current course domain from %env
16996: 
16997: 5. cnum: course number; default is current course number from %env
16998: 
16999: 6. idtype: set to concat if an additional digit is to be appended to the 
17000:    unix timestamp to form the suffix, if the plain timestamp is already
17001:    in use.  Default is to not do this, but simply increment the unix 
17002:    timestamp by 1 until a unique key is obtained.
17003: 
17004: 7. who: holder of locking key; defaults to user:domain for user.
17005: 
17006: 8. locktries: number of attempts to obtain a lock (sleep of 1s before 
17007:    retrying); default is 3.
17008: 
17009: 9. maxtries: number of attempts to obtain a unique suffix; default is 20.  
17010: 
17011: Returns:
17012: 
17013: 1. suffix obtained (numeric)
17014: 
17015: 2. result of deleting locking key (ok if deleted, or lock never obtained)
17016: 
17017: 3. error: contains (localized) error message if an error occurred.
17018: 
17019: 
17020: =back
17021: 
17022: =head2 HTTP Helper Routines
17023: 
17024: =over 4
17025: 
17026: =item *
17027: 
17028: escape() : unpack non-word characters into CGI-compatible hex codes
17029: 
17030: =item *
17031: 
17032: unescape() : pack CGI-compatible hex codes into actual non-word ASCII character
17033: 
17034: =back
17035: 
17036: =head1 PRIVATE SUBROUTINES
17037: 
17038: =head2 Underlying communication routines (Shouldn't call)
17039: 
17040: =over 4
17041: 
17042: =item *
17043: 
17044: subreply() : tries to pass a message to lonc, returns con_lost if incapable
17045: 
17046: =item *
17047: 
17048: reply() : uses subreply to send a message to remote machine, logs all failures
17049: 
17050: =item *
17051: 
17052: critical() : passes a critical message to another server; if cannot
17053: get through then place message in connection buffer directory and
17054: returns con_delayed, if incapable of saving message, returns
17055: con_failed
17056: 
17057: =item *
17058: 
17059: reconlonc() : tries to reconnect lonc client processes.
17060: 
17061: =back
17062: 
17063: =head2 Resource Access Logging
17064: 
17065: =over 4
17066: 
17067: =item *
17068: 
17069: flushcourselogs() : flush (save) buffer logs and access logs
17070: 
17071: =item *
17072: 
17073: courselog($what) : save message for course in hash
17074: 
17075: =item *
17076: 
17077: courseacclog($what) : save message for course using &courselog().  Perform
17078: special processing for specific resource types (problems, exams, quizzes, etc).
17079: 
17080: =item *
17081: 
17082: goodbye() : flush course logs and log shutting down; it is called in srm.conf
17083: as a PerlChildExitHandler
17084: 
17085: =back
17086: 
17087: =head2 Other
17088: 
17089: =over 4
17090: 
17091: =item *
17092: 
17093: symblist($mapname,%newhash) : update symbolic storage links
17094: 
17095: =back
17096: 
17097: =cut
17098: 

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